LICM.cpp revision 4a650af59912110a246c0a041bff38c92f0e56a2
1e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// 2e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner// 32e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// This pass is a simple loop invariant code motion pass. An interesting aspect 42e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// of this pass is that it uses alias analysis for two purposes: 52e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// 62e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// 1. Moving loop invariant loads out of loops. If we can determine that a 72e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// load inside of a loop never aliases anything stored to, we can hoist it 82e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// like any other instruction. 92e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// 2. Scalar Promotion of Memory - If there is a store instruction inside of 102e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// the loop, we try to move the store to happen AFTER the loop instead of 112e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// inside of the loop. This can only happen if a few conditions are true: 122e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// A. The pointer stored through is loop invariant 132e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// B. There are no stores or loads in the loop which _may_ alias the 142e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// pointer. There are no calls in the loop which mod/ref the pointer. 152e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// If these conditions are true, we can promote the loads and stores in the 162e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// loop of the pointer to use a temporary alloca'd variable. We then use 172e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// the mem2reg functionality to construct the appropriate SSA form for the 182e6e741b737960ecd0b68610875050019aac0f07Chris Lattner// variable. 19e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner// 20e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner//===----------------------------------------------------------------------===// 21e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 22e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Transforms/Scalar.h" 232e6e741b737960ecd0b68610875050019aac0f07Chris Lattner#include "llvm/Transforms/Utils/PromoteMemToReg.h" 24e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Transforms/Utils/Local.h" 25e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Analysis/LoopInfo.h" 26f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner#include "llvm/Analysis/AliasAnalysis.h" 270252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner#include "llvm/Analysis/AliasSetTracker.h" 28952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner#include "llvm/Analysis/Dominators.h" 292e6e741b737960ecd0b68610875050019aac0f07Chris Lattner#include "llvm/Instructions.h" 302e6e741b737960ecd0b68610875050019aac0f07Chris Lattner#include "llvm/DerivedTypes.h" 31fb743a937f6856e3ab1f8ed599677038750a550eChris Lattner#include "llvm/Target/TargetData.h" 32e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include "llvm/Support/InstVisitor.h" 332e6e741b737960ecd0b68610875050019aac0f07Chris Lattner#include "llvm/Support/CFG.h" 342e6e741b737960ecd0b68610875050019aac0f07Chris Lattner#include "Support/CommandLine.h" 356806f5614d2ec260fda954c951d33f58e77ed610Chris Lattner#include "Support/Debug.h" 366806f5614d2ec260fda954c951d33f58e77ed610Chris Lattner#include "Support/Statistic.h" 37b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner#include "llvm/Assembly/Writer.h" 38e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner#include <algorithm> 39e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 40e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattnernamespace { 414a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner cl::opt<bool> 424a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner DisablePromotion("disable-licm-promotion", cl::Hidden, 434a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner cl::desc("Disable memory promotion in LICM pass")); 442e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 45a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner Statistic<> NumHoisted("licm", "Number of instructions hoisted out of loop"); 46a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner Statistic<> NumHoistedLoads("licm", "Number of load insts hoisted"); 474a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner Statistic<> NumPromoted("licm", 484a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner "Number of memory locations promoted to registers"); 492e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 50e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner struct LICM : public FunctionPass, public InstVisitor<LICM> { 517e70829632f82de15db187845666aaca6e04b792Chris Lattner virtual bool runOnFunction(Function &F); 52e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 5394170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// This transformation requires natural loop information & requires that 5494170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// loop preheaders be inserted into the CFG... 5594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 56e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 57cb2610ea037a17115ef3a01a6bdaab4e3cfdca27Chris Lattner AU.setPreservesCFG(); 5898bf436e2e2ab463d79c54a42a46b12028905330Chris Lattner AU.addRequiredID(LoopSimplifyID); 595f0eb8da62308126d5b61e3eee5bee75b9dc5194Chris Lattner AU.addRequired<LoopInfo>(); 60952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner AU.addRequired<DominatorTree>(); 610252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AU.addRequired<DominanceFrontier>(); // For scalar promotion (mem2reg) 62f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner AU.addRequired<AliasAnalysis>(); 63e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 64e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 65e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner private: 662e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LoopInfo *LI; // Current LoopInfo 672e6e741b737960ecd0b68610875050019aac0f07Chris Lattner AliasAnalysis *AA; // Current AliasAnalysis information 6843f820d1f7638656be2158efac7dd8f5b08b8b77Chris Lattner DominanceFrontier *DF; // Current Dominance Frontier 692e6e741b737960ecd0b68610875050019aac0f07Chris Lattner bool Changed; // Set to true when we change anything. 702e6e741b737960ecd0b68610875050019aac0f07Chris Lattner BasicBlock *Preheader; // The preheader block of the current loop... 712e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Loop *CurLoop; // The current loop we are working on... 720252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSetTracker *CurAST; // AliasSet information for the current loop... 7311a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner DominatorTree *DT; // Dominator Tree for the current Loop... 74e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 7594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// visitLoop - Hoist expressions out of the specified loop... 7694170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 770252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner void visitLoop(Loop *L, AliasSetTracker &AST); 78e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 79952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// HoistRegion - Walk the specified region of the CFG (defined by all 80952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// blocks dominated by the specified block, and that are in the current 81952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// loop) in depth first order w.r.t the DominatorTree. This allows us to 82cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman /// visit definitions before uses, allowing us to hoist a loop body in one 83952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// pass without iteration. 84952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner /// 85952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner void HoistRegion(DominatorTree::Node *N); 86952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 87b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner /// inSubLoop - Little predicate that returns true if the specified basic 88b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner /// block is in a subloop of the current one, not the current one itself. 8994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 90b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner bool inSubLoop(BasicBlock *BB) { 91b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); 92e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i) 93e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner if (CurLoop->getSubLoops()[i]->contains(BB)) 94b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner return true; // A subloop actually contains this block! 95b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner return false; 96e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 97e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 9894170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// hoist - When an instruction is found to only use loop invariant operands 9994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// that is safe to hoist, this instruction is called to do the dirty work. 10094170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 1017e70829632f82de15db187845666aaca6e04b792Chris Lattner void hoist(Instruction &I); 102e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 1034a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner /// SafeToHoist - Only hoist an instruction if it is not a trapping 1044a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner /// instruction or if it is a trapping instruction and is guaranteed to 1054a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner /// execute. 1069966c03aad031f738718bed3bc00371e358beb5dTanya Lattner /// 1079966c03aad031f738718bed3bc00371e358beb5dTanya Lattner bool SafeToHoist(Instruction &I); 1089966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 10994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// pointerInvalidatedByLoop - Return true if the body of this loop may 11094170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// store into the memory location pointed to by V. 11194170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 1122e6e741b737960ecd0b68610875050019aac0f07Chris Lattner bool pointerInvalidatedByLoop(Value *V) { 1130252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Check to see if any of the basic blocks in CurLoop invalidate *V. 1140252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner return CurAST->getAliasSetForPointer(V, 0).isMod(); 1152e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 116f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner 11794170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// isLoopInvariant - Return true if the specified value is loop invariant 11894170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 119e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner inline bool isLoopInvariant(Value *V) { 120e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner if (Instruction *I = dyn_cast<Instruction>(V)) 121e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner return !CurLoop->contains(I->getParent()); 122e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner return true; // All non-instructions are loop invariant 123e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 124e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 1252e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// PromoteValuesInLoop - Look at the stores in the loop and promote as many 1262e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// to scalars as we can. 1272e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// 1282e6e741b737960ecd0b68610875050019aac0f07Chris Lattner void PromoteValuesInLoop(); 1292e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 1302e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// findPromotableValuesInLoop - Check the current loop for stores to 131352361b409d16045e703d986aa7fd77295173ef3Misha Brukman /// definite pointers, which are not loaded and stored through may aliases. 1322e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// If these are found, create an alloca for the value, add it to the 1332e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// PromotedValues list, and keep track of the mapping from value to 1342e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// alloca... 1352e6e741b737960ecd0b68610875050019aac0f07Chris Lattner /// 1362e6e741b737960ecd0b68610875050019aac0f07Chris Lattner void findPromotableValuesInLoop( 1372e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, 1382e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> &Val2AlMap); 1392e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 1402e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 14194170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// Instruction visitation handlers... these basically control whether or 14294170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// not the specified instruction types are hoisted. 14394170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner /// 144e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner friend class InstVisitor<LICM>; 1457e70829632f82de15db187845666aaca6e04b792Chris Lattner void visitBinaryOperator(Instruction &I) { 1464a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner if (isLoopInvariant(I.getOperand(0)) && 1474a650af59912110a246c0a041bff38c92f0e56a2Chris Lattner isLoopInvariant(I.getOperand(1)) && SafeToHoist(I)) 148e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner hoist(I); 149e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 1509b2b80fd48b10396be85a71735ffda0c155e5f72Chris Lattner void visitCastInst(CastInst &CI) { 1519b2b80fd48b10396be85a71735ffda0c155e5f72Chris Lattner Instruction &I = (Instruction&)CI; 1529966c03aad031f738718bed3bc00371e358beb5dTanya Lattner if (isLoopInvariant(I.getOperand(0)) && SafeToHoist(CI)) hoist(I); 1530513e9fe03f6e3c7d0272b8f4f82359e8d1a2e44Chris Lattner } 1547e70829632f82de15db187845666aaca6e04b792Chris Lattner void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); } 155e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 156eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner void visitLoadInst(LoadInst &LI); 157f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner 1587e70829632f82de15db187845666aaca6e04b792Chris Lattner void visitGetElementPtrInst(GetElementPtrInst &GEPI) { 1597e70829632f82de15db187845666aaca6e04b792Chris Lattner Instruction &I = (Instruction&)GEPI; 1607e70829632f82de15db187845666aaca6e04b792Chris Lattner for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1617e70829632f82de15db187845666aaca6e04b792Chris Lattner if (!isLoopInvariant(I.getOperand(i))) return; 1629966c03aad031f738718bed3bc00371e358beb5dTanya Lattner if(SafeToHoist(GEPI)) 1639966c03aad031f738718bed3bc00371e358beb5dTanya Lattner hoist(I); 164e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner } 165e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner }; 166f629309f74cf1a64aa7fd1cd5784fd7db9a8f59eChris Lattner 167a6275ccdf5e1aa208afde56c498e2b13e16442f0Chris Lattner RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion"); 168e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 169e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 170e0e734eea052a4e8372e6f430ef41149128ba0a6Chris LattnerPass *createLICMPass() { return new LICM(); } 171e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 17294170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// runOnFunction - For LICM, this simply traverses the loop structure of the 17394170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// function, hoisting expressions out of loops if possible. 17494170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// 1757e70829632f82de15db187845666aaca6e04b792Chris Lattnerbool LICM::runOnFunction(Function &) { 1762e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Changed = false; 177e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 1782e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Get our Loop and Alias Analysis information... 1792e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LI = &getAnalysis<LoopInfo>(); 180f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner AA = &getAnalysis<AliasAnalysis>(); 18143f820d1f7638656be2158efac7dd8f5b08b8b77Chris Lattner DF = &getAnalysis<DominanceFrontier>(); 18211a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner DT = &getAnalysis<DominatorTree>(); 183f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner 1842e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Hoist expressions out of all of the top-level loops. 1852e6e741b737960ecd0b68610875050019aac0f07Chris Lattner const std::vector<Loop*> &TopLevelLoops = LI->getTopLevelLoops(); 1862e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (std::vector<Loop*>::const_iterator I = TopLevelLoops.begin(), 1872e6e741b737960ecd0b68610875050019aac0f07Chris Lattner E = TopLevelLoops.end(); I != E; ++I) { 1880252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSetTracker AST(*AA); 1890252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner LICM::visitLoop(*I, AST); 1902e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 191e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner return Changed; 192e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 193e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 19494170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner 19594170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// visitLoop - Hoist expressions out of the specified loop... 19694170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// 1970252e49f6d6973b6f347c8feafc49e28af27163aChris Lattnervoid LICM::visitLoop(Loop *L, AliasSetTracker &AST) { 198e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // Recurse through all subloops before we process this loop... 1992e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (std::vector<Loop*>::const_iterator I = L->getSubLoops().begin(), 2002e6e741b737960ecd0b68610875050019aac0f07Chris Lattner E = L->getSubLoops().end(); I != E; ++I) { 2010252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSetTracker SubAST(*AA); 2020252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner LICM::visitLoop(*I, SubAST); 2032e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 2042e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Incorporate information about the subloops into this loop... 2050252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AST.add(SubAST); 2062e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 207e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner CurLoop = L; 2080252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner CurAST = &AST; 209e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 21099a57216a935a512c418032f590a4660bad9d846Chris Lattner // Get the preheader block to move instructions into... 21199a57216a935a512c418032f590a4660bad9d846Chris Lattner Preheader = L->getLoopPreheader(); 21299a57216a935a512c418032f590a4660bad9d846Chris Lattner assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!"); 21399a57216a935a512c418032f590a4660bad9d846Chris Lattner 2142e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Loop over the body of this loop, looking for calls, invokes, and stores. 2150252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Because subloops have already been incorporated into AST, we skip blocks in 2162e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // subloops. 2172e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 2182e6e741b737960ecd0b68610875050019aac0f07Chris Lattner const std::vector<BasicBlock*> &LoopBBs = L->getBlocks(); 2192e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (std::vector<BasicBlock*>::const_iterator I = LoopBBs.begin(), 2202e6e741b737960ecd0b68610875050019aac0f07Chris Lattner E = LoopBBs.end(); I != E; ++I) 2212e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (LI->getLoopFor(*I) == L) // Ignore blocks in subloops... 2220252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AST.add(**I); // Incorporate the specified basic block 2232e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 224e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // We want to visit all of the instructions in this loop... that are not parts 225e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // of our subloops (they have already had their invariants hoisted out of 226e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // their loop, into this loop, so there is no need to process the BODIES of 227e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // the subloops). 228e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // 229952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // Traverse the body of the loop in depth first order on the dominator tree so 230952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // that we are guaranteed to see definitions before we see uses. This allows 231952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // us to perform the LICM transformation in one pass, without iteration. 232952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner // 23311a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner HoistRegion(DT->getNode(L->getHeader())); 234e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2352e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Now that all loop invariants have been removed from the loop, promote any 2362e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // memory references to scalars that we can... 2372e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (!DisablePromotion) 2382e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromoteValuesInLoop(); 2392e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 240e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner // Clear out loops state information for the next iteration 241e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner CurLoop = 0; 24299a57216a935a512c418032f590a4660bad9d846Chris Lattner Preheader = 0; 243e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 244e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 245952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// HoistRegion - Walk the specified region of the CFG (defined by all blocks 246952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// dominated by the specified block, and that are in the current loop) in depth 247cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman/// first order w.r.t the DominatorTree. This allows us to visit definitions 248952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// before uses, allowing us to hoist a loop body in one pass without iteration. 249952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner/// 250952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattnervoid LICM::HoistRegion(DominatorTree::Node *N) { 251952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner assert(N != 0 && "Null dominator tree node?"); 252952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 253b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner // If this subregion is not in the top level loop at all, exit. 254c444a4228f31656f854d15eac671b450df557346Chris Lattner if (!CurLoop->contains(N->getBlock())) return; 255952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 256b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner // Only need to hoist the contents of this block if it is not part of a 257b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner // subloop (which would already have been hoisted) 258c444a4228f31656f854d15eac671b450df557346Chris Lattner if (!inSubLoop(N->getBlock())) 259c444a4228f31656f854d15eac671b450df557346Chris Lattner visit(*N->getBlock()); 260952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 261952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner const std::vector<DominatorTree::Node*> &Children = N->getChildren(); 262952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner for (unsigned i = 0, e = Children.size(); i != e; ++i) 263952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner HoistRegion(Children[i]); 264952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner} 265952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 266952eaee239d6c38f5121ed68277555344c2d6bf0Chris Lattner 26794170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// hoist - When an instruction is found to only use loop invariant operands 26894170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// that is safe to hoist, this instruction is called to do the dirty work. 26994170596b715928794a55bc5a1671e3992fd2dc9Chris Lattner/// 2707e70829632f82de15db187845666aaca6e04b792Chris Lattnervoid LICM::hoist(Instruction &Inst) { 271b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner DEBUG(std::cerr << "LICM hoisting to"; 272b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner WriteAsOperand(std::cerr, Preheader, false); 273b461373fbcdc5909cc7331fa64791cd039de4bc8Chris Lattner std::cerr << ": " << Inst); 274e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 27599a57216a935a512c418032f590a4660bad9d846Chris Lattner // Remove the instruction from its current basic block... but don't delete the 27699a57216a935a512c418032f590a4660bad9d846Chris Lattner // instruction. 27799a57216a935a512c418032f590a4660bad9d846Chris Lattner Inst.getParent()->getInstList().remove(&Inst); 278e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2799646e6b6af1f924f9366a2bffc6dde102f84d231Chris Lattner // Insert the new node in Preheader, before the terminator. 28099a57216a935a512c418032f590a4660bad9d846Chris Lattner Preheader->getInstList().insert(Preheader->getTerminator(), &Inst); 2819646e6b6af1f924f9366a2bffc6dde102f84d231Chris Lattner 2829646e6b6af1f924f9366a2bffc6dde102f84d231Chris Lattner ++NumHoisted; 283e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner Changed = true; 284e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner} 285e0e734eea052a4e8372e6f430ef41149128ba0a6Chris Lattner 2869966c03aad031f738718bed3bc00371e358beb5dTanya Lattner/// SafeToHoist - Only hoist an instruction if it is not a trapping instruction 2879966c03aad031f738718bed3bc00371e358beb5dTanya Lattner/// or if it is a trapping instruction and is guaranteed to execute 2889966c03aad031f738718bed3bc00371e358beb5dTanya Lattner/// 2899966c03aad031f738718bed3bc00371e358beb5dTanya Lattnerbool LICM::SafeToHoist(Instruction &Inst) { 2909966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 2919966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //If it is a trapping instruction, then check if its guaranteed to execute. 2929966c03aad031f738718bed3bc00371e358beb5dTanya Lattner if(Inst.isTrapping()) { 2939966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 2949966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //Get the instruction's basic block. 2959966c03aad031f738718bed3bc00371e358beb5dTanya Lattner BasicBlock *InstBB = Inst.getParent(); 2969966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 2979966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //Get the Dominator Tree Node for the instruction's basic block/ 29811a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner DominatorTree::Node *InstDTNode = DT->getNode(InstBB); 2999966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 3009966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //Get the exit blocks for the current loop. 30111a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner const std::vector<BasicBlock* > &ExitBlocks = CurLoop->getExitBlocks(); 3029966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 3039966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //For each exit block, get the DT node and walk up the DT until 3049966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //the instruction's basic block is found or we exit the loop. 3059966c03aad031f738718bed3bc00371e358beb5dTanya Lattner for(unsigned i=0; i < ExitBlocks.size(); ++i) { 30611a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner DominatorTree::Node *IDom = DT->getNode(ExitBlocks[i]); 3079966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 30811a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner while(IDom != InstDTNode) { 3099966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 3109966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //Get next Immediate Dominator. 3119966c03aad031f738718bed3bc00371e358beb5dTanya Lattner IDom = IDom->getIDom(); 3129966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 3139966c03aad031f738718bed3bc00371e358beb5dTanya Lattner //See if we exited the loop. 314c444a4228f31656f854d15eac671b450df557346Chris Lattner if(!CurLoop->contains(IDom->getBlock())) 31511a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner return false; 3169966c03aad031f738718bed3bc00371e358beb5dTanya Lattner } 3179966c03aad031f738718bed3bc00371e358beb5dTanya Lattner } 3189966c03aad031f738718bed3bc00371e358beb5dTanya Lattner } 31911a49a722f657294f38865019af51f82dc31c1c3Tanya Lattner 3209966c03aad031f738718bed3bc00371e358beb5dTanya Lattner return true; 3219966c03aad031f738718bed3bc00371e358beb5dTanya Lattner} 3229966c03aad031f738718bed3bc00371e358beb5dTanya Lattner 323eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner 324eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattnervoid LICM::visitLoadInst(LoadInst &LI) { 32561c1e7e32def2978441c699dcf9943dbe14aad98Chris Lattner if (isLoopInvariant(LI.getOperand(0)) && !LI.isVolatile() && 3269966c03aad031f738718bed3bc00371e358beb5dTanya Lattner !pointerInvalidatedByLoop(LI.getOperand(0)) && SafeToHoist(LI)) { 327eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner hoist(LI); 32899a57216a935a512c418032f590a4660bad9d846Chris Lattner ++NumHoistedLoads; 32999a57216a935a512c418032f590a4660bad9d846Chris Lattner } 330eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner} 331eb53ae4f2dc39e75e725b21b52d77d29cf1c11c9Chris Lattner 3322e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// PromoteValuesInLoop - Try to promote memory values to scalars by sinking 3332e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// stores out of the loop and moving loads to before the loop. We do this by 3342e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// looping over the stores in the loop, looking for stores to Must pointers 3352e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// which are loop invariant. We promote these memory locations to use allocas 3362e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// instead. These allocas can easily be raised to register values by the 3372e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// PromoteMem2Reg functionality. 3382e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// 3392e6e741b737960ecd0b68610875050019aac0f07Chris Lattnervoid LICM::PromoteValuesInLoop() { 3402e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // PromotedValues - List of values that are promoted out of the loop. Each 341065a616adad624152618b1b0084ff074e5b03bbbChris Lattner // value has an alloca instruction for it, and a canonical version of the 3422e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // pointer. 3432e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > PromotedValues; 3442e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca 3452e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 3462e6e741b737960ecd0b68610875050019aac0f07Chris Lattner findPromotableValuesInLoop(PromotedValues, ValueToAllocaMap); 3472e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (ValueToAllocaMap.empty()) return; // If there are values to promote... 3482e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 3492e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Changed = true; 3502e6e741b737960ecd0b68610875050019aac0f07Chris Lattner NumPromoted += PromotedValues.size(); 3512e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 3522e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Emit a copy from the value into the alloca'd value in the loop preheader 3532e6e741b737960ecd0b68610875050019aac0f07Chris Lattner TerminatorInst *LoopPredInst = Preheader->getTerminator(); 3542e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { 3552e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Load from the memory we are promoting... 3562e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LoadInst *LI = new LoadInst(PromotedValues[i].second, 3572e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromotedValues[i].second->getName()+".promoted", 3582e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LoopPredInst); 3592e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Store into the temporary alloca... 3602e6e741b737960ecd0b68610875050019aac0f07Chris Lattner new StoreInst(LI, PromotedValues[i].first, LoopPredInst); 3612e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 3622e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 3632e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Scan the basic blocks in the loop, replacing uses of our pointers with 3642e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // uses of the allocas in question. If we find a branch that exits the 3652e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // loop, make sure to put reload code into all of the successors of the 3662e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // loop. 3672e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 3682e6e741b737960ecd0b68610875050019aac0f07Chris Lattner const std::vector<BasicBlock*> &LoopBBs = CurLoop->getBlocks(); 3692e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (std::vector<BasicBlock*>::const_iterator I = LoopBBs.begin(), 3702e6e741b737960ecd0b68610875050019aac0f07Chris Lattner E = LoopBBs.end(); I != E; ++I) { 3712e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Rewrite all loads and stores in the block of the pointer... 3722e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (BasicBlock::iterator II = (*I)->begin(), E = (*I)->end(); 3732e6e741b737960ecd0b68610875050019aac0f07Chris Lattner II != E; ++II) { 374e408e25132b8de8c757db1e3ddcd70432dfeb24dChris Lattner if (LoadInst *L = dyn_cast<LoadInst>(II)) { 3752e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*>::iterator 3762e6e741b737960ecd0b68610875050019aac0f07Chris Lattner I = ValueToAllocaMap.find(L->getOperand(0)); 3772e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (I != ValueToAllocaMap.end()) 3782e6e741b737960ecd0b68610875050019aac0f07Chris Lattner L->setOperand(0, I->second); // Rewrite load instruction... 379e408e25132b8de8c757db1e3ddcd70432dfeb24dChris Lattner } else if (StoreInst *S = dyn_cast<StoreInst>(II)) { 3802e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*>::iterator 3812e6e741b737960ecd0b68610875050019aac0f07Chris Lattner I = ValueToAllocaMap.find(S->getOperand(1)); 3822e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (I != ValueToAllocaMap.end()) 3832e6e741b737960ecd0b68610875050019aac0f07Chris Lattner S->setOperand(1, I->second); // Rewrite store instruction... 3842e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 3852e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 3862e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 3872e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Check to see if any successors of this block are outside of the loop. 3882e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // If so, we need to copy the value from the alloca back into the memory 3892e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // location... 3902e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 3912e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI) 3922e6e741b737960ecd0b68610875050019aac0f07Chris Lattner if (!CurLoop->contains(*SI)) { 3932e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Copy all of the allocas into their memory locations... 3948601a9bf54049389e0e0b6a907dc51069dcadc60Chris Lattner BasicBlock::iterator BI = (*SI)->begin(); 3958601a9bf54049389e0e0b6a907dc51069dcadc60Chris Lattner while (isa<PHINode>(*BI)) 3968601a9bf54049389e0e0b6a907dc51069dcadc60Chris Lattner ++BI; // Skip over all of the phi nodes in the block... 3978601a9bf54049389e0e0b6a907dc51069dcadc60Chris Lattner Instruction *InsertPos = BI; 3982e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { 3992e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Load from the alloca... 4002e6e741b737960ecd0b68610875050019aac0f07Chris Lattner LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos); 4012e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Store into the memory we promoted... 4022e6e741b737960ecd0b68610875050019aac0f07Chris Lattner new StoreInst(LI, PromotedValues[i].second, InsertPos); 4032e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4042e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4052e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4062e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 4072e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // Now that we have done the deed, use the mem2reg functionality to promote 4082e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // all of the new allocas we just created into real SSA registers... 4092e6e741b737960ecd0b68610875050019aac0f07Chris Lattner // 4102e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<AllocaInst*> PromotedAllocas; 4112e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromotedAllocas.reserve(PromotedValues.size()); 4122e6e741b737960ecd0b68610875050019aac0f07Chris Lattner for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) 4132e6e741b737960ecd0b68610875050019aac0f07Chris Lattner PromotedAllocas.push_back(PromotedValues[i].first); 41443f820d1f7638656be2158efac7dd8f5b08b8b77Chris Lattner PromoteMemToReg(PromotedAllocas, *DT, *DF, AA->getTargetData()); 4152e6e741b737960ecd0b68610875050019aac0f07Chris Lattner} 4162e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 417352361b409d16045e703d986aa7fd77295173ef3Misha Brukman/// findPromotableValuesInLoop - Check the current loop for stores to definite 4182e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// pointers, which are not loaded and stored through may aliases. If these are 4192e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// found, create an alloca for the value, add it to the PromotedValues list, 4202e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// and keep track of the mapping from value to alloca... 4212e6e741b737960ecd0b68610875050019aac0f07Chris Lattner/// 4222e6e741b737960ecd0b68610875050019aac0f07Chris Lattnervoid LICM::findPromotableValuesInLoop( 4232e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, 4242e6e741b737960ecd0b68610875050019aac0f07Chris Lattner std::map<Value*, AllocaInst*> &ValueToAllocaMap) { 4252e6e741b737960ecd0b68610875050019aac0f07Chris Lattner Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); 4262e6e741b737960ecd0b68610875050019aac0f07Chris Lattner 4270252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Loop over all of the alias sets in the tracker object... 4280252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); 4290252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner I != E; ++I) { 4300252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AliasSet &AS = *I; 4310252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // We can promote this alias set if it has a store, if it is a "Must" alias 4320252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // set, and if the pointer is loop invariant. 4330252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias() && 4340252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner isLoopInvariant(AS.begin()->first)) { 4350252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner assert(AS.begin() != AS.end() && 4360252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner "Must alias set should have at least one pointer element in it!"); 4370252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner Value *V = AS.begin()->first; 4380252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner 4390252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // Check that all of the pointers in the alias set have the same type. We 4400252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // cannot (yet) promote a memory location that is loaded and stored in 4410252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner // different sizes. 4420252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner bool PointerOk = true; 4430252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) 4440252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner if (V->getType() != I->first->getType()) { 4450252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner PointerOk = false; 4460252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner break; 4472e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4480252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner 4490252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner if (PointerOk) { 4500252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner const Type *Ty = cast<PointerType>(V->getType())->getElementType(); 4510252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart); 4520252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner PromotedValues.push_back(std::make_pair(AI, V)); 4530252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner 4540252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) 4550252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner ValueToAllocaMap.insert(std::make_pair(I->first, AI)); 4560252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner 4570252e49f6d6973b6f347c8feafc49e28af27163aChris Lattner DEBUG(std::cerr << "LICM: Promoting value: " << *V << "\n"); 4582e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4592e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 4602e6e741b737960ecd0b68610875050019aac0f07Chris Lattner } 461f5e84aa0887d3fcd752d4a4fa1bb0e526be49f20Chris Lattner} 462