LICM.cpp revision 90c579de5a383cee278acc3f7e7b9d0a656e6a35
1e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===// 9e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner// 1092094b4d928119eceac1484331acffe950f4651cChris Lattner// This pass performs loop invariant code motion, attempting to remove as much 1192094b4d928119eceac1484331acffe950f4651cChris Lattner// code from the body of a loop as possible. It does this by either hoisting 1292094b4d928119eceac1484331acffe950f4651cChris Lattner// code into the preheader block, or by sinking code to the exit blocks if it is 1392094b4d928119eceac1484331acffe950f4651cChris Lattner// safe. This pass also promotes must-aliased memory locations in the loop to 14e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner// live in registers, thus hoisting and sinking "invariant" loads and stores. 1592094b4d928119eceac1484331acffe950f4651cChris Lattner// 1692094b4d928119eceac1484331acffe950f4651cChris Lattner// This pass uses alias analysis for two purposes: 172e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// 182741c971044d2165be572749b94398043caccfebChris Lattner// 1. Moving loop invariant loads and calls out of loops. If we can determine 192741c971044d2165be572749b94398043caccfebChris Lattner// that a load or call inside of a loop never aliases anything stored to, 202741c971044d2165be572749b94398043caccfebChris Lattner// we can hoist it or sink it like any other instruction. 212e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// 2. Scalar Promotion of Memory - If there is a store instruction inside of 222e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// the loop, we try to move the store to happen AFTER the loop instead of 232e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// inside of the loop. This can only happen if a few conditions are true: 242e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// A. The pointer stored through is loop invariant 252e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// B. There are no stores or loads in the loop which _may_ alias the 262e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// pointer. There are no calls in the loop which mod/ref the pointer. 272e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// If these conditions are true, we can promote the loads and stores in the 282e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// loop of the pointer to use a temporary alloca'd variable. We then use 292e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// the mem2reg functionality to construct the appropriate SSA form for the 302e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// variable. 31e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner// 32e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner//===----------------------------------------------------------------------===// 33e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 341f309c1a4e8068a64627c612a895d9b403dbcb26Chris Lattner#define DEBUG_TYPE "licm" 35e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Transforms/Scalar.h" 3679066fa6acce02c97c714a5a6e151ed8a249721cChris Lattner#include "llvm/Constants.h" 372741c971044d2165be572749b94398043caccfebChris Lattner#include "llvm/DerivedTypes.h" 389289ae85b41eef62ae1fadb93e99d33f723fb682Torok Edwin#include "llvm/IntrinsicInst.h" 392741c971044d2165be572749b94398043caccfebChris Lattner#include "llvm/Instructions.h" 402741c971044d2165be572749b94398043caccfebChris Lattner#include "llvm/Target/TargetData.h" 41e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Analysis/LoopInfo.h" 4254959d6cf68a9b575c98c074babe9867682a7271Devang Patel#include "llvm/Analysis/LoopPass.h" 43f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner#include "llvm/Analysis/AliasAnalysis.h" 440252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner#include "llvm/Analysis/AliasSetTracker.h" 45952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner#include "llvm/Analysis/Dominators.h" 4696b651c627160e1ea0f1bb86d352d697e6c1972dDevang Patel#include "llvm/Analysis/ScalarEvolution.h" 472741c971044d2165be572749b94398043caccfebChris Lattner#include "llvm/Transforms/Utils/PromoteMemToReg.h" 489133fe28954d498fc4de13064c7d65bd811de02cReid Spencer#include "llvm/Support/CFG.h" 49551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 50ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h" 51551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 52551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 53e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include <algorithm> 5492094b4d928119eceac1484331acffe950f4651cChris Lattnerusing namespace llvm; 55d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 560e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumSunk , "Number of instructions sunk out of loop"); 570e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumHoisted , "Number of instructions hoisted out of loop"); 580e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk"); 590e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk"); 600e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumPromoted , "Number of memory locations promoted to registers"); 610e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattner 62844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<bool> 63844731a7f1909f55935e3514c9e713a62d67662eDan GohmanDisablePromotion("disable-licm-promotion", cl::Hidden, 64844731a7f1909f55935e3514c9e713a62d67662eDan Gohman cl::desc("Disable memory promotion in LICM pass")); 652e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 66844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace { 673e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner struct LICM : public LoopPass { 68ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky static char ID; // Pass identification, replacement for typeid 6990c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson LICM() : LoopPass(ID) {} 70794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel 7154959d6cf68a9b575c98c074babe9867682a7271Devang Patel virtual bool runOnLoop(Loop *L, LPPassManager &LPM); 72e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 7394170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// This transformation requires natural loop information & requires that 7494170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// loop preheaders be inserted into the CFG... 7594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 76e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 77cb2610ea037a17115ef3a01a6bdaab4e3cfdca27Chris Lattner AU.setPreservesCFG(); 783a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson AU.addRequired<DominatorTree>(); 790252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AU.addRequired<DominanceFrontier>(); // For scalar promotion (mem2reg) 801e381fcd553a3955a10338fd305efc023d7d22e1Dan Gohman AU.addRequired<LoopInfo>(); 811e381fcd553a3955a10338fd305efc023d7d22e1Dan Gohman AU.addRequiredID(LoopSimplifyID); 82f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner AU.addRequired<AliasAnalysis>(); 8396b651c627160e1ea0f1bb86d352d697e6c1972dDevang Patel AU.addPreserved<ScalarEvolution>(); 8496b651c627160e1ea0f1bb86d352d697e6c1972dDevang Patel AU.addPreserved<DominanceFrontier>(); 855c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman AU.addPreservedID(LoopSimplifyID); 86e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 87e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 88747603e39ea3f29add9ba6c07bbefcc5c2432612Dan Gohman bool doFinalization() { 89fd94dd58ffef1be3597ef8cb64f3b1d476d7d5ecAnton Korobeynikov // Free the values stored in the map 90fd94dd58ffef1be3597ef8cb64f3b1d476d7d5ecAnton Korobeynikov for (std::map<Loop *, AliasSetTracker *>::iterator 91fd94dd58ffef1be3597ef8cb64f3b1d476d7d5ecAnton Korobeynikov I = LoopToAliasMap.begin(), E = LoopToAliasMap.end(); I != E; ++I) 92fd94dd58ffef1be3597ef8cb64f3b1d476d7d5ecAnton Korobeynikov delete I->second; 93fd94dd58ffef1be3597ef8cb64f3b1d476d7d5ecAnton Korobeynikov 9454959d6cf68a9b575c98c074babe9867682a7271Devang Patel LoopToAliasMap.clear(); 9554959d6cf68a9b575c98c074babe9867682a7271Devang Patel return false; 9654959d6cf68a9b575c98c074babe9867682a7271Devang Patel } 9754959d6cf68a9b575c98c074babe9867682a7271Devang Patel 98e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner private: 9992094b4d928119eceac1484331acffe950f4651cChris Lattner // Various analyses that we use... 1002e6e741b737960ecd0b68610875050019aac0f07Chris Lattner AliasAnalysis *AA; // Current AliasAnalysis information 10192094b4d928119eceac1484331acffe950f4651cChris Lattner LoopInfo *LI; // Current LoopInfo 1023a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson DominatorTree *DT; // Dominator Tree for the current Loop... 10343f820d1f7638656be2158efac7dd8f5b08b8b77Chris Lattner DominanceFrontier *DF; // Current Dominance Frontier 10492094b4d928119eceac1484331acffe950f4651cChris Lattner 10592094b4d928119eceac1484331acffe950f4651cChris Lattner // State that is updated as we process loops 1062e6e741b737960ecd0b68610875050019aac0f07Chris Lattner bool Changed; // Set to true when we change anything. 1072e6e741b737960ecd0b68610875050019aac0f07Chris Lattner BasicBlock *Preheader; // The preheader block of the current loop... 1082e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Loop *CurLoop; // The current loop we are working on... 1090252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSetTracker *CurAST; // AliasSet information for the current loop... 11054959d6cf68a9b575c98c074babe9867682a7271Devang Patel std::map<Loop *, AliasSetTracker *> LoopToAliasMap; 111e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 11291d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. 11391d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L); 11491d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 11591d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias 11691d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel /// set. 11791d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel void deleteAnalysisValue(Value *V, Loop *L); 11891d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 119e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner /// SinkRegion - Walk the specified region of the CFG (defined by all blocks 120e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner /// dominated by the specified block, and that are in the current loop) in 1213a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson /// reverse depth first order w.r.t the DominatorTree. This allows us to 122e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner /// visit uses before definitions, allowing us to sink a loop body in one 123e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner /// pass without iteration. 124e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner /// 12526042420d642e810f5cdfb2da6156b74aaf80945Devang Patel void SinkRegion(DomTreeNode *N); 126e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 127952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// HoistRegion - Walk the specified region of the CFG (defined by all 128952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// blocks dominated by the specified block, and that are in the current 1293a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson /// loop) in depth first order w.r.t the DominatorTree. This allows us to 130cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman /// visit definitions before uses, allowing us to hoist a loop body in one 131952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// pass without iteration. 132952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// 13326042420d642e810f5cdfb2da6156b74aaf80945Devang Patel void HoistRegion(DomTreeNode *N); 134952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 135b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner /// inSubLoop - Little predicate that returns true if the specified basic 136b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner /// block is in a subloop of the current one, not the current one itself. 13794170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 138b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner bool inSubLoop(BasicBlock *BB) { 139b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); 140329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner for (Loop::iterator I = CurLoop->begin(), E = CurLoop->end(); I != E; ++I) 141329c1c6c949d07e3fe9722ec633b4258217fd99dChris Lattner if ((*I)->contains(BB)) 142b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner return true; // A subloop actually contains this block! 143b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner return false; 144e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 145e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 146a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// isExitBlockDominatedByBlockInLoop - This method checks to see if the 147a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// specified exit block of the loop is dominated by the specified block 148a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// that is in the body of the loop. We use these constraints to 149a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// dramatically limit the amount of the dominator tree that needs to be 150a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// searched. 151a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner bool isExitBlockDominatedByBlockInLoop(BasicBlock *ExitBlock, 152a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner BasicBlock *BlockInLoop) const { 153a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // If the block in the loop is the loop header, it must be dominated! 154a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner BasicBlock *LoopHeader = CurLoop->getHeader(); 155a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (BlockInLoop == LoopHeader) 156a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return true; 157fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 15826042420d642e810f5cdfb2da6156b74aaf80945Devang Patel DomTreeNode *BlockInLoopNode = DT->getNode(BlockInLoop); 15926042420d642e810f5cdfb2da6156b74aaf80945Devang Patel DomTreeNode *IDom = DT->getNode(ExitBlock); 160fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 161a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Because the exit block is not in the loop, we know we have to get _at 1622741c971044d2165be572749b94398043caccfebChris Lattner // least_ its immediate dominator. 1633472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher IDom = IDom->getIDom(); 1643472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher 1653472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher while (IDom && IDom != BlockInLoopNode) { 166a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // If we have got to the header of the loop, then the instructions block 167a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // did not dominate the exit node, so we can't hoist it. 1683a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson if (IDom->getBlock() == LoopHeader) 169a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return false; 170fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 1713472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher // Get next Immediate Dominator. 1723472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher IDom = IDom->getIDom(); 1733472ae1e7b0f38c4ac0802ffc2900f71a5470c3bEric Christopher }; 174a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 175a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return true; 176a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 177a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 178a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// sink - When an instruction is found to only be used outside of the loop, 179a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// this function moves it to the exit blocks and patches up SSA form as 180a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// needed. 181a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// 182a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner void sink(Instruction &I); 183a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 18494170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// hoist - When an instruction is found to only use loop invariant operands 18594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// that is safe to hoist, this instruction is called to do the dirty work. 18694170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 1877e70829632f82de15db187845666aaca6e04b792Chris Lattner void hoist(Instruction &I); 188e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 189a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it 190a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// is not a trapping instruction or if it is a trapping instruction and is 191a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner /// guaranteed to execute. 1929966c03aad031f738718bed3bc00371e358beb5dTanya Lattner /// 193a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner bool isSafeToExecuteUnconditionally(Instruction &I); 1949966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 19594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// pointerInvalidatedByLoop - Return true if the body of this loop may 19694170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// store into the memory location pointed to by V. 197fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman /// 198f3e1d6977ee5fb6b66df314d65e28e2c35d6e244Chris Lattner bool pointerInvalidatedByLoop(Value *V, unsigned Size) { 1990252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Check to see if any of the basic blocks in CurLoop invalidate *V. 200f3e1d6977ee5fb6b66df314d65e28e2c35d6e244Chris Lattner return CurAST->getAliasSetForPointer(V, Size).isMod(); 2012e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 202f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner 203a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner bool canSinkOrHoistInst(Instruction &I); 204a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner bool isLoopInvariantInst(Instruction &I); 205a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner bool isNotUsedInLoop(Instruction &I); 206e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2072e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// PromoteValuesInLoop - Look at the stores in the loop and promote as many 2082e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// to scalars as we can. 2092e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// 2102e6e741b737960ecd0b68610875050019aac0f07Chris Lattner void PromoteValuesInLoop(); 2112e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 212fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner /// FindPromotableValuesInLoop - Check the current loop for stores to 213352361b409d16045e703d986aa7fd77295173ef3Misha Brukman /// definite pointers, which are not loaded and stored through may aliases. 2142e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// If these are found, create an alloca for the value, add it to the 2152e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// PromotedValues list, and keep track of the mapping from value to 2162e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// alloca... 2172e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// 218fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner void FindPromotableValuesInLoop( 2192e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, 2202e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> &Val2AlMap); 221e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner }; 222e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 223e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 224844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar LICM::ID = 0; 225d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(LICM, "licm", "Loop Invariant Code Motion", false, false); 226844731a7f1909f55935e3514c9e713a62d67662eDan Gohman 227394f0441e06dafca29f0752cf400990a5b8fe4b1Daniel DunbarPass *llvm::createLICMPass() { return new LICM(); } 228e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2298d246f09cb46fe38aa71d2a4edcd84f30c4bf80eDevang Patel/// Hoist expressions out of the specified loop. Note, alias info for inner 2308d246f09cb46fe38aa71d2a4edcd84f30c4bf80eDevang Patel/// loop is not preserved so it is not a good idea to run LICM multiple 2318d246f09cb46fe38aa71d2a4edcd84f30c4bf80eDevang Patel/// times on one loop. 23294170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// 23354959d6cf68a9b575c98c074babe9867682a7271Devang Patelbool LICM::runOnLoop(Loop *L, LPPassManager &LPM) { 2342e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Changed = false; 235e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2362e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Get our Loop and Alias Analysis information... 2372e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LI = &getAnalysis<LoopInfo>(); 238f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner AA = &getAnalysis<AliasAnalysis>(); 23943f820d1f7638656be2158efac7dd8f5b08b8b77Chris Lattner DF = &getAnalysis<DominanceFrontier>(); 2403a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson DT = &getAnalysis<DominatorTree>(); 241f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner 24254959d6cf68a9b575c98c074babe9867682a7271Devang Patel CurAST = new AliasSetTracker(*AA); 243f38ac5dd390ca583b6e6d1b40b378d07e49e5097Devang Patel // Collect Alias info from subloops 24454959d6cf68a9b575c98c074babe9867682a7271Devang Patel for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end(); 24554959d6cf68a9b575c98c074babe9867682a7271Devang Patel LoopItr != LoopItrE; ++LoopItr) { 24654959d6cf68a9b575c98c074babe9867682a7271Devang Patel Loop *InnerL = *LoopItr; 24754959d6cf68a9b575c98c074babe9867682a7271Devang Patel AliasSetTracker *InnerAST = LoopToAliasMap[InnerL]; 24854959d6cf68a9b575c98c074babe9867682a7271Devang Patel assert (InnerAST && "Where is my AST?"); 24994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner 25054959d6cf68a9b575c98c074babe9867682a7271Devang Patel // What if InnerLoop was modified by other passes ? 25154959d6cf68a9b575c98c074babe9867682a7271Devang Patel CurAST->add(*InnerAST); 2522e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 25354959d6cf68a9b575c98c074babe9867682a7271Devang Patel 254e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner CurLoop = L; 255e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 25699a57216a935a512c418032f590a4660bad9d846Chris Lattner // Get the preheader block to move instructions into... 25799a57216a935a512c418032f590a4660bad9d846Chris Lattner Preheader = L->getLoopPreheader(); 25899a57216a935a512c418032f590a4660bad9d846Chris Lattner 2592e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Loop over the body of this loop, looking for calls, invokes, and stores. 2600252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Because subloops have already been incorporated into AST, we skip blocks in 2612e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // subloops. 2622e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 2639b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 2649b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman I != E; ++I) { 2659b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman BasicBlock *BB = *I; 2669b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops... 2679b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman CurAST->add(*BB); // Incorporate the specified basic block 2689b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman } 2692e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 270e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // We want to visit all of the instructions in this loop... that are not parts 271e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // of our subloops (they have already had their invariants hoisted out of 272e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // their loop, into this loop, so there is no need to process the BODIES of 273e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // the subloops). 274e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // 275952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // Traverse the body of the loop in depth first order on the dominator tree so 276952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // that we are guaranteed to see definitions before we see uses. This allows 277af5cbc82bb6410129425c98bbfc1ffc4c3d0f411Nick Lewycky // us to sink instructions in one pass, without iteration. After sinking 278e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // instructions, we perform another pass to hoist them out of the loop. 279952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // 28003e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman if (L->hasDedicatedExits()) 28103e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman SinkRegion(DT->getNode(L->getHeader())); 28203e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman if (Preheader) 28303e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman HoistRegion(DT->getNode(L->getHeader())); 284e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2852e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Now that all loop invariants have been removed from the loop, promote any 2862e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // memory references to scalars that we can... 28703e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman if (!DisablePromotion && Preheader && L->hasDedicatedExits()) 2882e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromoteValuesInLoop(); 2892e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 290e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // Clear out loops state information for the next iteration 291e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner CurLoop = 0; 29299a57216a935a512c418032f590a4660bad9d846Chris Lattner Preheader = 0; 29354959d6cf68a9b575c98c074babe9867682a7271Devang Patel 29454959d6cf68a9b575c98c074babe9867682a7271Devang Patel LoopToAliasMap[L] = CurAST; 29554959d6cf68a9b575c98c074babe9867682a7271Devang Patel return Changed; 296e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 297e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 298e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner/// SinkRegion - Walk the specified region of the CFG (defined by all blocks 299e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner/// dominated by the specified block, and that are in the current loop) in 3003a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson/// reverse depth first order w.r.t the DominatorTree. This allows us to visit 301e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner/// uses before definitions, allowing us to sink a loop body in one pass without 302e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner/// iteration. 303e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner/// 30426042420d642e810f5cdfb2da6156b74aaf80945Devang Patelvoid LICM::SinkRegion(DomTreeNode *N) { 3053a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson assert(N != 0 && "Null dominator tree node?"); 3063a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson BasicBlock *BB = N->getBlock(); 307e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 308e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // If this subregion is not in the top level loop at all, exit. 309e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner if (!CurLoop->contains(BB)) return; 310e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 311e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // We are processing blocks in reverse dfo, so process children first... 31226042420d642e810f5cdfb2da6156b74aaf80945Devang Patel const std::vector<DomTreeNode*> &Children = N->getChildren(); 313e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner for (unsigned i = 0, e = Children.size(); i != e; ++i) 314e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner SinkRegion(Children[i]); 315e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 316e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // Only need to process the contents of this block if it is not part of a 317e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // subloop (which would already have been processed). 318e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner if (inSubLoop(BB)) return; 319e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 320a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) { 321a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner Instruction &I = *--II; 322fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 323e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // Check to see if we can sink this instruction to the exit blocks 324e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // of the loop. We can do this if the all users of the instruction are 325e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // outside of the loop. In this case, it doesn't even matter if the 326e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // operands of the instruction are loop invariant. 327e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // 32870ac2dcb841dc62f08e16f0b0e2cefbf01baa4c5Chris Lattner if (isNotUsedInLoop(I) && canSinkOrHoistInst(I)) { 329a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner ++II; 330e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner sink(I); 331a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner } 332e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner } 333e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner} 334e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner 335952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// HoistRegion - Walk the specified region of the CFG (defined by all blocks 336952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// dominated by the specified block, and that are in the current loop) in depth 3373a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson/// first order w.r.t the DominatorTree. This allows us to visit definitions 338952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// before uses, allowing us to hoist a loop body in one pass without iteration. 339952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// 34026042420d642e810f5cdfb2da6156b74aaf80945Devang Patelvoid LICM::HoistRegion(DomTreeNode *N) { 3413a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson assert(N != 0 && "Null dominator tree node?"); 3423a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson BasicBlock *BB = N->getBlock(); 343952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 344b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner // If this subregion is not in the top level loop at all, exit. 345ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner if (!CurLoop->contains(BB)) return; 346952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 347a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Only need to process the contents of this block if it is not part of a 348a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // subloop (which would already have been processed). 349ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner if (!inSubLoop(BB)) 350a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) { 351a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner Instruction &I = *II++; 352fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 353e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // Try hoisting the instruction out to the preheader. We can only do this 354e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // if all of the operands of the instruction are loop invariant and if it 355e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // is safe to hoist the instruction. 356e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner // 357fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman if (isLoopInvariantInst(I) && canSinkOrHoistInst(I) && 358e4365b2e8c79f9b2d564f31fdcc0568da8e5d60fChris Lattner isSafeToExecuteUnconditionally(I)) 3593c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner hoist(I); 360a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 361952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 36226042420d642e810f5cdfb2da6156b74aaf80945Devang Patel const std::vector<DomTreeNode*> &Children = N->getChildren(); 363952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner for (unsigned i = 0, e = Children.size(); i != e; ++i) 36456b7ee20dad7160a117ea0d102fc5432074ce2d0Chris Lattner HoistRegion(Children[i]); 365952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner} 366952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 367a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// canSinkOrHoistInst - Return true if the hoister and sinker can handle this 368a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// instruction. 369a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// 370a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnerbool LICM::canSinkOrHoistInst(Instruction &I) { 371ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner // Loads have extra constraints we have to verify before we can hoist them. 372ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 373ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner if (LI->isVolatile()) 374ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner return false; // Don't hoist volatile loads! 375ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner 376967948b4e25cae3de68f38fddaa75d838e676decChris Lattner // Loads from constant memory are always safe to move, even if they end up 377967948b4e25cae3de68f38fddaa75d838e676decChris Lattner // in the same alias set as something that ends up being modified. 378dab249b3b762cff0ff16069b3233e4c885e75347Dan Gohman if (AA->pointsToConstantMemory(LI->getOperand(0))) 379967948b4e25cae3de68f38fddaa75d838e676decChris Lattner return true; 380967948b4e25cae3de68f38fddaa75d838e676decChris Lattner 381ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner // Don't hoist loads which have may-aliased stores in loop. 382f3e1d6977ee5fb6b66df314d65e28e2c35d6e244Chris Lattner unsigned Size = 0; 383f3e1d6977ee5fb6b66df314d65e28e2c35d6e244Chris Lattner if (LI->getType()->isSized()) 384fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman Size = AA->getTypeStoreSize(LI->getType()); 385f3e1d6977ee5fb6b66df314d65e28e2c35d6e244Chris Lattner return !pointerInvalidatedByLoop(LI->getOperand(0), Size); 386118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner } else if (CallInst *CI = dyn_cast<CallInst>(&I)) { 387118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner // Handle obvious cases efficiently. 388dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI); 389dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands if (Behavior == AliasAnalysis::DoesNotAccessMemory) 390dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return true; 391dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands else if (Behavior == AliasAnalysis::OnlyReadsMemory) { 392dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands // If this call only reads from memory and there are no writes to memory 393dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands // in the loop, we can hoist or sink the call as appropriate. 394dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands bool FoundMod = false; 395dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); 396dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands I != E; ++I) { 397dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands AliasSet &AS = *I; 398dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands if (!AS.isForwardingAliasSet() && AS.isMod()) { 399dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands FoundMod = true; 400dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands break; 401118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner } 402118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner } 403dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands if (!FoundMod) return true; 404118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner } 405118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner 406118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner // FIXME: This should use mod/ref information to see if we can hoist or sink 407118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner // the call. 408fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 409118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner return false; 410ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner } 411ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner 4123da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer // Otherwise these instructions are hoistable/sinkable 413832254e1c2387c0cbeb0a820b8315fbe85cb003aReid Spencer return isa<BinaryOperator>(I) || isa<CastInst>(I) || 414d8af90c1da64b81a7aa167b5adcc1e81b0741252Dan Gohman isa<SelectInst>(I) || isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || 415d8af90c1da64b81a7aa167b5adcc1e81b0741252Dan Gohman isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || 416d8af90c1da64b81a7aa167b5adcc1e81b0741252Dan Gohman isa<ShuffleVectorInst>(I); 417a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner} 418a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 419a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// isNotUsedInLoop - Return true if the only users of this instruction are 420a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// outside of the loop. If this is true, we can sink the instruction to the 421a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// exit blocks of the loop. 422a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// 423a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnerbool LICM::isNotUsedInLoop(Instruction &I) { 424ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) { 425ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner Instruction *User = cast<Instruction>(*UI); 426ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner if (PHINode *PN = dyn_cast<PHINode>(User)) { 427ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner // PHI node uses occur in predecessor blocks! 428ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 429ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner if (PN->getIncomingValue(i) == &I) 430ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner if (CurLoop->contains(PN->getIncomingBlock(i))) 431ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner return false; 43292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman } else if (CurLoop->contains(User)) { 433a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return false; 434ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner } 435ea9403f2aa078e840a49792d906390395f0cd2f6Chris Lattner } 436a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return true; 437a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner} 438a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 439a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 440a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// isLoopInvariantInst - Return true if all operands of this instruction are 441a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// loop invariant. We also filter out non-hoistable instructions here just for 442a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// efficiency. 443a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// 444a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnerbool LICM::isLoopInvariantInst(Instruction &I) { 445a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // The instruction is loop invariant if all of its operands are loop-invariant 446a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 4473280f7bd557250a448eb56edbddfc90c94a406e6Chris Lattner if (!CurLoop->isLoopInvariant(I.getOperand(i))) 448a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return false; 449a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 450ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner // If we got this far, the instruction is loop invariant! 451ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner return true; 452ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner} 453ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner 454a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// sink - When an instruction is found to only be used outside of the loop, 455a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner/// this function moves it to the exit blocks and patches up SSA form as needed. 456a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner/// This method is guaranteed to remove the original instruction from its 457a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner/// position, and may either delete it or move it to outside of the loop. 458a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// 459a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnervoid LICM::sink(Instruction &I) { 46039a383124b70dda1fea18288235114029ede76cfNick Lewycky DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n"); 461a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 462b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel SmallVector<BasicBlock*, 8> ExitBlocks; 4635fa802fe279f10f402e039bc931aa57c0e424bf5Chris Lattner CurLoop->getExitBlocks(ExitBlocks); 464df45bd3803ae0965a83cd1f30ee051b9f43e73ddChris Lattner 465df45bd3803ae0965a83cd1f30ee051b9f43e73ddChris Lattner if (isa<LoadInst>(I)) ++NumMovedLoads; 466118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner else if (isa<CallInst>(I)) ++NumMovedCalls; 467df45bd3803ae0965a83cd1f30ee051b9f43e73ddChris Lattner ++NumSunk; 468df45bd3803ae0965a83cd1f30ee051b9f43e73ddChris Lattner Changed = true; 469df45bd3803ae0965a83cd1f30ee051b9f43e73ddChris Lattner 470a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // The case where there is only a single exit node of this loop is common 471a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // enough that we handle it as a special (more efficient) case. It is more 472a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // efficient to handle because there are no PHI nodes that need to be placed. 473a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (ExitBlocks.size() == 1) { 474a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[0], I.getParent())) { 475a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Instruction is not used, just delete it. 4762741c971044d2165be572749b94398043caccfebChris Lattner CurAST->deleteValue(&I); 4771bf5ebc7be0d5b05e4175c7adb767b38896adef1Devang Patel // If I has users in unreachable blocks, eliminate. 478228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel // If I is not void type then replaceAllUsesWith undef. 479228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel // This allows ValueHandlers and custom metadata to adjust itself. 4809674d15120d657c9a9e681a7dc668349fa3a8de0Devang Patel if (!I.getType()->isVoidTy()) 481228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel I.replaceAllUsesWith(UndefValue::get(I.getType())); 4823c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.eraseFromParent(); 483a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } else { 484a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Move the instruction to the start of the exit block, after any PHI 485a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // nodes in it. 4863c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.removeFromParent(); 48702dea8b39f3acad5de1df36273444d149145e7fcDan Gohman BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI(); 488a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner ExitBlocks[0]->getInstList().insert(InsertPt, &I); 489a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 490303595942502f17c087fa28874c2b89117148c45Dan Gohman } else if (ExitBlocks.empty()) { 491a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // The instruction is actually dead if there ARE NO exit blocks. 4922741c971044d2165be572749b94398043caccfebChris Lattner CurAST->deleteValue(&I); 4931bf5ebc7be0d5b05e4175c7adb767b38896adef1Devang Patel // If I has users in unreachable blocks, eliminate. 494228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel // If I is not void type then replaceAllUsesWith undef. 495228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel // This allows ValueHandlers and custom metadata to adjust itself. 4969674d15120d657c9a9e681a7dc668349fa3a8de0Devang Patel if (!I.getType()->isVoidTy()) 497228ebd0f4cf9207d32d61ef4b11b81736895dc09Devang Patel I.replaceAllUsesWith(UndefValue::get(I.getType())); 4983c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.eraseFromParent(); 499a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } else { 500a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Otherwise, if we have multiple exits, use the PromoteMem2Reg function to 501a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // do all of the hard work of inserting PHI nodes as necessary. We convert 502a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // the value into a stack object to get it to do this. 503a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 504a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Firstly, we create a stack object to hold the value... 505eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner AllocaInst *AI = 0; 506a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 5079674d15120d657c9a9e681a7dc668349fa3a8de0Devang Patel if (!I.getType()->isVoidTy()) { 50850dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson AI = new AllocaInst(I.getType(), 0, I.getName(), 509ecb7a77885b174cf4d001a9b48533b3979e7810dDan Gohman I.getParent()->getParent()->getEntryBlock().begin()); 510e7ae1a9dee1b023dbd5f7d6d1fbdb165fbbcb26cDevang Patel CurAST->add(AI); 511e7ae1a9dee1b023dbd5f7d6d1fbdb165fbbcb26cDevang Patel } 512fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 513a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Secondly, insert load instructions for each use of the instruction 514a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // outside of the loop. 515a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner while (!I.use_empty()) { 516a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner Instruction *U = cast<Instruction>(I.use_back()); 517a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 518a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // If the user is a PHI Node, we actually have to insert load instructions 519a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // in all predecessor blocks, not in the PHI block itself! 520a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (PHINode *UPN = dyn_cast<PHINode>(U)) { 521a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Only insert into each predecessor once, so that we don't have 522a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // different incoming values from the same block! 523a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner std::map<BasicBlock*, Value*> InsertedBlocks; 524a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner for (unsigned i = 0, e = UPN->getNumIncomingValues(); i != e; ++i) 525a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (UPN->getIncomingValue(i) == &I) { 526a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner BasicBlock *Pred = UPN->getIncomingBlock(i); 527a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner Value *&PredVal = InsertedBlocks[Pred]; 528a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (!PredVal) { 529a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Insert a new load instruction right before the terminator in 530a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // the predecessor block. 531a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner PredVal = new LoadInst(AI, "", Pred->getTerminator()); 532e7ae1a9dee1b023dbd5f7d6d1fbdb165fbbcb26cDevang Patel CurAST->add(cast<LoadInst>(PredVal)); 533a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 534a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 535a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner UPN->setIncomingValue(i, PredVal); 536a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 537a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 538a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } else { 539a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner LoadInst *L = new LoadInst(AI, "", U); 540a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner U->replaceUsesOfWith(&I, L); 541e7ae1a9dee1b023dbd5f7d6d1fbdb165fbbcb26cDevang Patel CurAST->add(L); 542a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 543a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 544a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 545a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Thirdly, insert a copy of the instruction in each exit block of the loop 546a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // that is dominated by the instruction, storing the result into the memory 547a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // location. Be careful not to insert the instruction into any particular 548a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // basic block more than once. 549a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner std::set<BasicBlock*> InsertedBlocks; 550a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner BasicBlock *InstOrigBB = I.getParent(); 551a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 552a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { 553a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner BasicBlock *ExitBlock = ExitBlocks[i]; 554a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner 555a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB)) { 556a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // If we haven't already processed this exit block, do so now. 557e3cfe8d563fa06b274587aa38b1359ff707f33deChris Lattner if (InsertedBlocks.insert(ExitBlock).second) { 558a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Insert the code after the last PHI node... 55902dea8b39f3acad5de1df36273444d149145e7fcDan Gohman BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI(); 560fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 561a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // If this is the first exit block processed, just move the original 562a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // instruction, otherwise clone the original instruction and insert 563a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // the copy. 564a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner Instruction *New; 5657d3ced934f1bb2c6845676c7333ef879d5219e88Chris Lattner if (InsertedBlocks.size() == 1) { 5663c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.removeFromParent(); 567a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner ExitBlock->getInstList().insert(InsertPt, &I); 568a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner New = &I; 569a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } else { 5706776064d190701c5bae4d5403939eed2e480d1cdNick Lewycky New = I.clone(); 57170ac2dcb841dc62f08e16f0b0e2cefbf01baa4c5Chris Lattner CurAST->copyValue(&I, New); 572eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner if (!I.getName().empty()) 573eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner New->setName(I.getName()+".le"); 574a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner ExitBlock->getInstList().insert(InsertPt, New); 575a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 576fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 577a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Now that we have inserted the instruction, store it into the alloca 578eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner if (AI) new StoreInst(New, AI, InsertPt); 579a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 580a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 581a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 582a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner 583a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner // If the instruction doesn't dominate any exit blocks, it must be dead. 584a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner if (InsertedBlocks.empty()) { 5852741c971044d2165be572749b94398043caccfebChris Lattner CurAST->deleteValue(&I); 5863c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.eraseFromParent(); 587a3df8a964aa9d0d171fd4f6e491c7d1cf9dee500Chris Lattner } 588fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 589a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner // Finally, promote the fine value to SSA form. 590eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner if (AI) { 591eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner std::vector<AllocaInst*> Allocas; 592eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner Allocas.push_back(AI); 593ce2c51b6701c06f701efa2871794b7710cb64b41Nick Lewycky PromoteMemToReg(Allocas, *DT, *DF, CurAST); 594eaa243039b6a83f8915f4f96ced58fabb520c851Chris Lattner } 595a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner } 596a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner} 597952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 59894170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// hoist - When an instruction is found to only use loop invariant operands 59994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// that is safe to hoist, this instruction is called to do the dirty work. 60094170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// 601a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnervoid LICM::hoist(Instruction &I) { 602f15826999bc560ee6f67cff5ecd71bf9cce7a7aaDavid Greene DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " 60367d1d1f8322bdc460158888cd233be8d1cb1d4abEvan Cheng << I << "\n"); 604ed6dfc2856cd44a8aa608a9074eabbf42dd6cadcChris Lattner 60599a57216a935a512c418032f590a4660bad9d846Chris Lattner // Remove the instruction from its current basic block... but don't delete the 60699a57216a935a512c418032f590a4660bad9d846Chris Lattner // instruction. 6073c80a51cfd5e7187439ce4c051da77ee46e0ff9aChris Lattner I.removeFromParent(); 608e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 6099646e6b6af1f924f9366a2bffc6dde102f84d231Chris Lattner // Insert the new node in Preheader, before the terminator. 610a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner Preheader->getInstList().insert(Preheader->getTerminator(), &I); 611fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 612a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (isa<LoadInst>(I)) ++NumMovedLoads; 613118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner else if (isa<CallInst>(I)) ++NumMovedCalls; 6149646e6b6af1f924f9366a2bffc6dde102f84d231Chris Lattner ++NumHoisted; 615e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner Changed = true; 616e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 617e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 618a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is 619a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// not a trapping instruction or if it is a trapping instruction and is 620a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner/// guaranteed to execute. 6219966c03aad031f738718bed3bc00371e358beb5dTanya Lattner/// 622a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattnerbool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { 62392094b4d928119eceac1484331acffe950f4651cChris Lattner // If it is not a trapping instruction, it is always safe to hoist. 6240b79a7727d68a507837e827803859424cf3d997bEli Friedman if (Inst.isSafeToSpeculativelyExecute()) 6250b79a7727d68a507837e827803859424cf3d997bEli Friedman return true; 626fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 62792094b4d928119eceac1484331acffe950f4651cChris Lattner // Otherwise we have to check to make sure that the instruction dominates all 62892094b4d928119eceac1484331acffe950f4651cChris Lattner // of the exit blocks. If it doesn't, then there is a path out of the loop 62992094b4d928119eceac1484331acffe950f4651cChris Lattner // which does not execute this instruction, so we can't hoist it. 63092094b4d928119eceac1484331acffe950f4651cChris Lattner 63192094b4d928119eceac1484331acffe950f4651cChris Lattner // If the instruction is in the header block for the loop (which is very 63292094b4d928119eceac1484331acffe950f4651cChris Lattner // common), it is always guaranteed to dominate the exit blocks. Since this 63392094b4d928119eceac1484331acffe950f4651cChris Lattner // is a common case, and can save some work, check it now. 634a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (Inst.getParent() == CurLoop->getHeader()) 63592094b4d928119eceac1484331acffe950f4651cChris Lattner return true; 63692094b4d928119eceac1484331acffe950f4651cChris Lattner 63792094b4d928119eceac1484331acffe950f4651cChris Lattner // Get the exit blocks for the current loop. 638b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel SmallVector<BasicBlock*, 8> ExitBlocks; 6395fa802fe279f10f402e039bc931aa57c0e424bf5Chris Lattner CurLoop->getExitBlocks(ExitBlocks); 6409966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 6413a2b58f3adc5e9f9bffac7ce193f0570aa5a721fOwen Anderson // For each exit block, get the DT node and walk up the DT until the 64292094b4d928119eceac1484331acffe950f4651cChris Lattner // instruction's basic block is found or we exit the loop. 643a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) 644a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[i], Inst.getParent())) 645a2706518f9cc34358cba1072f78235aa091d6d93Chris Lattner return false; 646fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 6479966c03aad031f738718bed3bc00371e358beb5dTanya Lattner return true; 6489966c03aad031f738718bed3bc00371e358beb5dTanya Lattner} 6499966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 650eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner 6512e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// PromoteValuesInLoop - Try to promote memory values to scalars by sinking 6522e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// stores out of the loop and moving loads to before the loop. We do this by 6532e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// looping over the stores in the loop, looking for stores to Must pointers 6542e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// which are loop invariant. We promote these memory locations to use allocas 6552e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// instead. These allocas can easily be raised to register values by the 6562e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// PromoteMem2Reg functionality. 6572e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// 6582e6e741b737960ecd0b68610875050019aac0f07Chris Lattnervoid LICM::PromoteValuesInLoop() { 6592e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // PromotedValues - List of values that are promoted out of the loop. Each 660065a616adad624152618b1b0084ff074e5b03bbbChris Lattner // value has an alloca instruction for it, and a canonical version of the 6612e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // pointer. 6622e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > PromotedValues; 6632e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca 6642e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 665fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner FindPromotableValuesInLoop(PromotedValues, ValueToAllocaMap); 666fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner if (ValueToAllocaMap.empty()) return; // If there are values to promote. 6672e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 6682e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Changed = true; 6692e6e741b737960ecd0b68610875050019aac0f07Chris Lattner NumPromoted += PromotedValues.size(); 6702e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 671fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner std::vector<Value*> PointerValueNumbers; 672fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 6732e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Emit a copy from the value into the alloca'd value in the loop preheader 6742e6e741b737960ecd0b68610875050019aac0f07Chris Lattner TerminatorInst *LoopPredInst = Preheader->getTerminator(); 6752e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { 676fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner Value *Ptr = PromotedValues[i].second; 677fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 678fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // If we are promoting a pointer value, update alias information for the 679fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // inserted load. 680fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner Value *LoadValue = 0; 6811df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (cast<PointerType>(Ptr->getType())->getElementType()->isPointerTy()) { 682fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // Locate a load or store through the pointer, and assign the same value 683fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // to LI as we are loading or storing. Since we know that the value is 684fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // stored in this loop, this will always succeed. 685fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner for (Value::use_iterator UI = Ptr->use_begin(), E = Ptr->use_end(); 686488d4421c67aa5045b60379beaca2f8341ac6380Gabor Greif UI != E; ++UI) { 687488d4421c67aa5045b60379beaca2f8341ac6380Gabor Greif User *U = *UI; 688488d4421c67aa5045b60379beaca2f8341ac6380Gabor Greif if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 689fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner LoadValue = LI; 690fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner break; 691488d4421c67aa5045b60379beaca2f8341ac6380Gabor Greif } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 692bdacd879578943efc98b75639cb8e7b4aab037f9Chris Lattner if (SI->getOperand(1) == Ptr) { 693fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner LoadValue = SI->getOperand(0); 694fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner break; 695fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner } 696fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner } 697488d4421c67aa5045b60379beaca2f8341ac6380Gabor Greif } 698fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner assert(LoadValue && "No store through the pointer found!"); 699fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner PointerValueNumbers.push_back(LoadValue); // Remember this for later. 700fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner } 701fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 702fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // Load from the memory we are promoting. 703fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner LoadInst *LI = new LoadInst(Ptr, Ptr->getName()+".promoted", LoopPredInst); 704fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 705fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner if (LoadValue) CurAST->copyValue(LoadValue, LI); 706fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 707fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // Store into the temporary alloca. 7082e6e741b737960ecd0b68610875050019aac0f07Chris Lattner new StoreInst(LI, PromotedValues[i].first, LoopPredInst); 7092e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 710fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 7112e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Scan the basic blocks in the loop, replacing uses of our pointers with 7120ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // uses of the allocas in question. 7132e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 7149b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman for (Loop::block_iterator I = CurLoop->block_begin(), 7159b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman E = CurLoop->block_end(); I != E; ++I) { 7169b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman BasicBlock *BB = *I; 7172e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Rewrite all loads and stores in the block of the pointer... 7189b78763fce4cb418e7a2e672efb84bac25559b79Dan Gohman for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { 719e408e25132b8de8c757db1e3ddcd70432dfeb24dChris Lattner if (LoadInst *L = dyn_cast<LoadInst>(II)) { 7202e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*>::iterator 7212e6e741b737960ecd0b68610875050019aac0f07Chris Lattner I = ValueToAllocaMap.find(L->getOperand(0)); 7222e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (I != ValueToAllocaMap.end()) 7232e6e741b737960ecd0b68610875050019aac0f07Chris Lattner L->setOperand(0, I->second); // Rewrite load instruction... 724e408e25132b8de8c757db1e3ddcd70432dfeb24dChris Lattner } else if (StoreInst *S = dyn_cast<StoreInst>(II)) { 7252e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*>::iterator 7262e6e741b737960ecd0b68610875050019aac0f07Chris Lattner I = ValueToAllocaMap.find(S->getOperand(1)); 7272e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (I != ValueToAllocaMap.end()) 7282e6e741b737960ecd0b68610875050019aac0f07Chris Lattner S->setOperand(1, I->second); // Rewrite store instruction... 7292e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 7302e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 7310ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner } 7322e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 7330ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // Now that the body of the loop uses the allocas instead of the original 7340ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // memory locations, insert code to copy the alloca value back into the 7350ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // original memory location on all exits from the loop. Note that we only 7360ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // want to insert one copy of the code in each exit block, though the loop may 7370ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // exit to the same block more than once. 7380ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner // 73919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner SmallPtrSet<BasicBlock*, 16> ProcessedBlocks; 7400ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner 741b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel SmallVector<BasicBlock*, 8> ExitBlocks; 7425fa802fe279f10f402e039bc931aa57c0e424bf5Chris Lattner CurLoop->getExitBlocks(ExitBlocks); 74319d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { 74419d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner if (!ProcessedBlocks.insert(ExitBlocks[i])) 74519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner continue; 74619d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner 74719d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // Copy all of the allocas into their memory locations. 74802dea8b39f3acad5de1df36273444d149145e7fcDan Gohman BasicBlock::iterator BI = ExitBlocks[i]->getFirstNonPHI(); 74919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner Instruction *InsertPos = BI; 75019d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner unsigned PVN = 0; 75119d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { 75219d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // Load from the alloca. 75319d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos); 75419d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner 75519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // If this is a pointer type, update alias info appropriately. 7561df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (LI->getType()->isPointerTy()) 75719d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner CurAST->copyValue(PointerValueNumbers[PVN++], LI); 75819d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner 75919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // Store into the memory we promoted. 76019d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner new StoreInst(LI, PromotedValues[i].second, InsertPos); 7610ed2da9ac733c51ba004c067d3b552c1fa54613dChris Lattner } 76219d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner } 7632e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 7642e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Now that we have done the deed, use the mem2reg functionality to promote 765fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // all of the new allocas we just created into real SSA registers. 7662e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 7672e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<AllocaInst*> PromotedAllocas; 7682e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromotedAllocas.reserve(PromotedValues.size()); 7692e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) 7702e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromotedAllocas.push_back(PromotedValues[i].first); 771ce2c51b6701c06f701efa2871794b7710cb64b41Nick Lewycky PromoteMemToReg(PromotedAllocas, *DT, *DF, CurAST); 7722e6e741b737960ecd0b68610875050019aac0f07Chris Lattner} 7732e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 774fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner/// FindPromotableValuesInLoop - Check the current loop for stores to definite 775f2038b1d9325b23e73a20b57fcb3508f4dda1817Devang Patel/// pointers, which are not loaded and stored through may aliases and are safe 776f2038b1d9325b23e73a20b57fcb3508f4dda1817Devang Patel/// for promotion. If these are found, create an alloca for the value, add it 777f2038b1d9325b23e73a20b57fcb3508f4dda1817Devang Patel/// to the PromotedValues list, and keep track of the mapping from value to 778f2038b1d9325b23e73a20b57fcb3508f4dda1817Devang Patel/// alloca. 779fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattnervoid LICM::FindPromotableValuesInLoop( 7802e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, 7812e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> &ValueToAllocaMap) { 7822e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); 7832e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 784fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // Loop over all of the alias sets in the tracker object. 7850252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); 7860252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner I != E; ++I) { 7870252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSet &AS = *I; 7880252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // We can promote this alias set if it has a store, if it is a "Must" alias 789fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner // set, if the pointer is loop invariant, and if we are not eliminating any 790118dd0ce3d8e4b0a945afc95c9538d5005abacdeChris Lattner // volatile loads or stores. 79129d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || 792d7168ddb116c2e9aa1f8325ae887eb63d6003037Chris Lattner AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue())) 79329d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner continue; 79429d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner 79529d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner assert(!AS.empty() && 79629d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner "Must alias set should have at least one pointer element in it!"); 797d7168ddb116c2e9aa1f8325ae887eb63d6003037Chris Lattner Value *V = AS.begin()->getValue(); 79829d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner 79929d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner // Check that all of the pointers in the alias set have the same type. We 80029d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner // cannot (yet) promote a memory location that is loaded and stored in 80129d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner // different sizes. 80229d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner { 8030252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner bool PointerOk = true; 8040252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) 805d7168ddb116c2e9aa1f8325ae887eb63d6003037Chris Lattner if (V->getType() != I->getValue()->getType()) { 8060252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner PointerOk = false; 8070252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner break; 8082e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 80929d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner if (!PointerOk) 81029d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner continue; 81129d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner } 8120252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner 81319d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // It isn't safe to promote a load/store from the loop if the load/store is 81419d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // conditional. For example, turning: 81519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // 81619d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // for () { if (c) *P += 1; } 81719d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // 81819d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // into: 81919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // 82019d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; 82119d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // 82219d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // is not safe, because *P may only be valid to access if 'c' is true. 82319d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // 82419d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // It is safe to promote P if all uses are direct load/stores and if at 82519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // least one is guaranteed to be executed. 82619d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner bool GuaranteedToExecute = false; 82719d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner bool InvalidInst = false; 82819d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); 82919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner UI != UE; ++UI) { 83019d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // Ignore instructions not in this loop. 83129d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner Instruction *Use = dyn_cast<Instruction>(*UI); 83292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman if (!Use || !CurLoop->contains(Use)) 83329d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner continue; 834bc2265abe1d1b830995bb2f6322409bc7d7c5740Devang Patel 83519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner if (!isa<LoadInst>(Use) && !isa<StoreInst>(Use)) { 83619d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner InvalidInst = true; 83729d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner break; 83819d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner } 83919d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner 84019d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner if (!GuaranteedToExecute) 84119d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner GuaranteedToExecute = isSafeToExecuteUnconditionally(*Use); 84229d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner } 843bc2265abe1d1b830995bb2f6322409bc7d7c5740Devang Patel 84419d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // If there is an non-load/store instruction in the loop, we can't promote 84519d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // it. If there isn't a guaranteed-to-execute instruction, we can't 84619d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner // promote. 84719d9d4364e514f316f8757b577bb8bb33c22dbfcChris Lattner if (InvalidInst || !GuaranteedToExecute) 84829d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner continue; 84929d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner 85029d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner const Type *Ty = cast<PointerType>(V->getType())->getElementType(); 85150dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart); 85229d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner PromotedValues.push_back(std::make_pair(AI, V)); 853fb3ee1930bfd0902f5fa3e6526775e3791e38d3cChris Lattner 85429d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner // Update the AST and alias analysis. 85529d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner CurAST->copyValue(V, AI); 856fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 85729d929363d9b8a2c0d40c5d50fec022c736cb654Chris Lattner for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) 858d7168ddb116c2e9aa1f8325ae887eb63d6003037Chris Lattner ValueToAllocaMap.insert(std::make_pair(I->getValue(), AI)); 859fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 860f15826999bc560ee6f67cff5ecd71bf9cce7a7aaDavid Greene DEBUG(dbgs() << "LICM: Promoting value: " << *V << "\n"); 8612e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 862f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner} 86391d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 86491d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel/// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. 86591d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patelvoid LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) { 86691d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel AliasSetTracker *AST = LoopToAliasMap[L]; 86791d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel if (!AST) 86891d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel return; 86991d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 87091d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel AST->copyValue(From, To); 87191d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel} 87291d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 87391d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel/// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias 87491d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel/// set. 87591d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patelvoid LICM::deleteAnalysisValue(Value *V, Loop *L) { 87691d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel AliasSetTracker *AST = LoopToAliasMap[L]; 87791d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel if (!AST) 87891d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel return; 87991d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel 88091d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel AST->deleteValue(V); 88191d22c8b1ec2ad8f2f29804b729473ccf720fb3eDevang Patel} 882