Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 10 | // This pass performs loop invariant code motion, attempting to remove as much |
| 11 | // code from the body of a loop as possible. It does this by either hoisting |
| 12 | // code into the preheader block, or by sinking code to the exit blocks if it is |
| 13 | // safe. This pass also promotes must-aliased memory locations in the loop to |
| 14 | // live in registers. |
| 15 | // |
| 16 | // This pass uses alias analysis for two purposes: |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 17 | // |
| 18 | // 1. Moving loop invariant loads out of loops. If we can determine that a |
| 19 | // load inside of a loop never aliases anything stored to, we can hoist it |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 20 | // or sink it like any other instruction. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 21 | // 2. Scalar Promotion of Memory - If there is a store instruction inside of |
| 22 | // the loop, we try to move the store to happen AFTER the loop instead of |
| 23 | // inside of the loop. This can only happen if a few conditions are true: |
| 24 | // A. The pointer stored through is loop invariant |
| 25 | // B. There are no stores or loads in the loop which _may_ alias the |
| 26 | // pointer. There are no calls in the loop which mod/ref the pointer. |
| 27 | // If these conditions are true, we can promote the loads and stores in the |
| 28 | // loop of the pointer to use a temporary alloca'd variable. We then use |
| 29 | // the mem2reg functionality to construct the appropriate SSA form for the |
| 30 | // variable. |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/Local.h" |
| 37 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/AliasSetTracker.h" |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 41 | #include "llvm/Instructions.h" |
| 42 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CFG.h" |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 45 | #include "Support/CommandLine.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 46 | #include "Support/Debug.h" |
| 47 | #include "Support/Statistic.h" |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 48 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 49 | #include <algorithm> |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 50 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 51 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 52 | namespace { |
Chris Lattner | 4a650af | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 53 | cl::opt<bool> |
| 54 | DisablePromotion("disable-licm-promotion", cl::Hidden, |
| 55 | cl::desc("Disable memory promotion in LICM pass")); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 56 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 57 | Statistic<> NumSunk("licm", "Number of instructions sunk out of loop"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 58 | Statistic<> NumHoisted("licm", "Number of instructions hoisted out of loop"); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 59 | Statistic<> NumMovedLoads("licm", "Number of load insts hoisted or sunk"); |
Chris Lattner | 4a650af | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 60 | Statistic<> NumPromoted("licm", |
| 61 | "Number of memory locations promoted to registers"); |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 62 | |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 63 | struct LICM : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | virtual bool runOnFunction(Function &F); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 66 | /// This transformation requires natural loop information & requires that |
| 67 | /// loop preheaders be inserted into the CFG... |
| 68 | /// |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 70 | AU.setPreservesCFG(); |
Chris Lattner | 98bf436 | 2003-10-12 21:52:28 +0000 | [diff] [blame] | 71 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 72 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 73 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 74 | AU.addRequired<DominanceFrontier>(); // For scalar promotion (mem2reg) |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 75 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 79 | // Various analyses that we use... |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 80 | AliasAnalysis *AA; // Current AliasAnalysis information |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 81 | LoopInfo *LI; // Current LoopInfo |
| 82 | DominatorTree *DT; // Dominator Tree for the current Loop... |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 83 | DominanceFrontier *DF; // Current Dominance Frontier |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 84 | |
| 85 | // State that is updated as we process loops |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 86 | bool Changed; // Set to true when we change anything. |
| 87 | BasicBlock *Preheader; // The preheader block of the current loop... |
| 88 | Loop *CurLoop; // The current loop we are working on... |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 89 | AliasSetTracker *CurAST; // AliasSet information for the current loop... |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 91 | /// visitLoop - Hoist expressions out of the specified loop... |
| 92 | /// |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 93 | void visitLoop(Loop *L, AliasSetTracker &AST); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 95 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 96 | /// blocks dominated by the specified block, and that are in the current |
| 97 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 98 | /// visit definitions before uses, allowing us to hoist a loop body in one |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 99 | /// pass without iteration. |
| 100 | /// |
| 101 | void HoistRegion(DominatorTree::Node *N); |
| 102 | |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 103 | /// inSubLoop - Little predicate that returns true if the specified basic |
| 104 | /// block is in a subloop of the current one, not the current one itself. |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 105 | /// |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 106 | bool inSubLoop(BasicBlock *BB) { |
| 107 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 108 | for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i) |
| 109 | if (CurLoop->getSubLoops()[i]->contains(BB)) |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 110 | return true; // A subloop actually contains this block! |
| 111 | return false; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 114 | /// isExitBlockDominatedByBlockInLoop - This method checks to see if the |
| 115 | /// specified exit block of the loop is dominated by the specified block |
| 116 | /// that is in the body of the loop. We use these constraints to |
| 117 | /// dramatically limit the amount of the dominator tree that needs to be |
| 118 | /// searched. |
| 119 | bool isExitBlockDominatedByBlockInLoop(BasicBlock *ExitBlock, |
| 120 | BasicBlock *BlockInLoop) const { |
| 121 | // If the block in the loop is the loop header, it must be dominated! |
| 122 | BasicBlock *LoopHeader = CurLoop->getHeader(); |
| 123 | if (BlockInLoop == LoopHeader) |
| 124 | return true; |
| 125 | |
| 126 | DominatorTree::Node *BlockInLoopNode = DT->getNode(BlockInLoop); |
| 127 | DominatorTree::Node *IDom = DT->getNode(ExitBlock); |
| 128 | |
| 129 | // Because the exit block is not in the loop, we know we have to get _at |
| 130 | // least_ it's immediate dominator. |
| 131 | do { |
| 132 | // Get next Immediate Dominator. |
| 133 | IDom = IDom->getIDom(); |
| 134 | |
| 135 | // If we have got to the header of the loop, then the instructions block |
| 136 | // did not dominate the exit node, so we can't hoist it. |
| 137 | if (IDom->getBlock() == LoopHeader) |
| 138 | return false; |
| 139 | |
| 140 | } while (IDom != BlockInLoopNode); |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | /// sink - When an instruction is found to only be used outside of the loop, |
| 146 | /// this function moves it to the exit blocks and patches up SSA form as |
| 147 | /// needed. |
| 148 | /// |
| 149 | void sink(Instruction &I); |
| 150 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 151 | /// hoist - When an instruction is found to only use loop invariant operands |
| 152 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 153 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 154 | void hoist(Instruction &I); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 155 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 156 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it |
| 157 | /// is not a trapping instruction or if it is a trapping instruction and is |
| 158 | /// guaranteed to execute. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 159 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 160 | bool isSafeToExecuteUnconditionally(Instruction &I); |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 162 | /// pointerInvalidatedByLoop - Return true if the body of this loop may |
| 163 | /// store into the memory location pointed to by V. |
| 164 | /// |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 165 | bool pointerInvalidatedByLoop(Value *V) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 166 | // Check to see if any of the basic blocks in CurLoop invalidate *V. |
| 167 | return CurAST->getAliasSetForPointer(V, 0).isMod(); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 170 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 171 | /// |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 172 | inline bool isLoopInvariant(Value *V) { |
| 173 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 174 | return !CurLoop->contains(I->getParent()); |
| 175 | return true; // All non-instructions are loop invariant |
| 176 | } |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 177 | |
| 178 | bool canSinkOrHoistInst(Instruction &I); |
| 179 | bool isLoopInvariantInst(Instruction &I); |
| 180 | bool isNotUsedInLoop(Instruction &I); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 182 | /// PromoteValuesInLoop - Look at the stores in the loop and promote as many |
| 183 | /// to scalars as we can. |
| 184 | /// |
| 185 | void PromoteValuesInLoop(); |
| 186 | |
| 187 | /// findPromotableValuesInLoop - Check the current loop for stores to |
Misha Brukman | 352361b | 2003-09-11 15:32:37 +0000 | [diff] [blame] | 188 | /// definite pointers, which are not loaded and stored through may aliases. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 189 | /// If these are found, create an alloca for the value, add it to the |
| 190 | /// PromotedValues list, and keep track of the mapping from value to |
| 191 | /// alloca... |
| 192 | /// |
| 193 | void findPromotableValuesInLoop( |
| 194 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
| 195 | std::map<Value*, AllocaInst*> &Val2AlMap); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 196 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 197 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 198 | RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion"); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 201 | FunctionPass *llvm::createLICMPass() { return new LICM(); } |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 203 | /// runOnFunction - For LICM, this simply traverses the loop structure of the |
| 204 | /// function, hoisting expressions out of loops if possible. |
| 205 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 206 | bool LICM::runOnFunction(Function &) { |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 207 | Changed = false; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 209 | // Get our Loop and Alias Analysis information... |
| 210 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 211 | AA = &getAnalysis<AliasAnalysis>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 212 | DF = &getAnalysis<DominanceFrontier>(); |
Tanya Lattner | 11a49a7 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 213 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 215 | // Hoist expressions out of all of the top-level loops. |
| 216 | const std::vector<Loop*> &TopLevelLoops = LI->getTopLevelLoops(); |
| 217 | for (std::vector<Loop*>::const_iterator I = TopLevelLoops.begin(), |
| 218 | E = TopLevelLoops.end(); I != E; ++I) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 219 | AliasSetTracker AST(*AA); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 220 | visitLoop(*I, AST); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 222 | return Changed; |
| 223 | } |
| 224 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 225 | |
| 226 | /// visitLoop - Hoist expressions out of the specified loop... |
| 227 | /// |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 228 | void LICM::visitLoop(Loop *L, AliasSetTracker &AST) { |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 229 | // Recurse through all subloops before we process this loop... |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 230 | for (std::vector<Loop*>::const_iterator I = L->getSubLoops().begin(), |
| 231 | E = L->getSubLoops().end(); I != E; ++I) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 232 | AliasSetTracker SubAST(*AA); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 233 | visitLoop(*I, SubAST); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 234 | |
| 235 | // Incorporate information about the subloops into this loop... |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 236 | AST.add(SubAST); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 238 | CurLoop = L; |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 239 | CurAST = &AST; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 241 | // Get the preheader block to move instructions into... |
| 242 | Preheader = L->getLoopPreheader(); |
| 243 | assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!"); |
| 244 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 245 | // Loop over the body of this loop, looking for calls, invokes, and stores. |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 246 | // Because subloops have already been incorporated into AST, we skip blocks in |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 247 | // subloops. |
| 248 | // |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 249 | for (std::vector<BasicBlock*>::const_iterator I = L->getBlocks().begin(), |
| 250 | E = L->getBlocks().end(); I != E; ++I) |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 251 | if (LI->getLoopFor(*I) == L) // Ignore blocks in subloops... |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 252 | AST.add(**I); // Incorporate the specified basic block |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 253 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 254 | // We want to visit all of the instructions in this loop... that are not parts |
| 255 | // of our subloops (they have already had their invariants hoisted out of |
| 256 | // their loop, into this loop, so there is no need to process the BODIES of |
| 257 | // the subloops). |
| 258 | // |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 259 | // Traverse the body of the loop in depth first order on the dominator tree so |
| 260 | // that we are guaranteed to see definitions before we see uses. This allows |
| 261 | // us to perform the LICM transformation in one pass, without iteration. |
| 262 | // |
Tanya Lattner | 11a49a7 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 263 | HoistRegion(DT->getNode(L->getHeader())); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 265 | // Now that all loop invariants have been removed from the loop, promote any |
| 266 | // memory references to scalars that we can... |
| 267 | if (!DisablePromotion) |
| 268 | PromoteValuesInLoop(); |
| 269 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 270 | // Clear out loops state information for the next iteration |
| 271 | CurLoop = 0; |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 272 | Preheader = 0; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 275 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 276 | /// dominated by the specified block, and that are in the current loop) in depth |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 277 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 278 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 279 | /// |
| 280 | void LICM::HoistRegion(DominatorTree::Node *N) { |
| 281 | assert(N != 0 && "Null dominator tree node?"); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 282 | BasicBlock *BB = N->getBlock(); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 283 | |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 284 | // If this subregion is not in the top level loop at all, exit. |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 285 | if (!CurLoop->contains(BB)) return; |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 286 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 287 | // Only need to process the contents of this block if it is not part of a |
| 288 | // subloop (which would already have been processed). |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 289 | if (!inSubLoop(BB)) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 290 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) { |
| 291 | Instruction &I = *II++; |
| 292 | |
| 293 | // We can only handle simple expressions and loads with this code. |
| 294 | if (canSinkOrHoistInst(I)) { |
| 295 | // First check to see if we can sink this instruction to the exit blocks |
| 296 | // of the loop. We can do this if the only users of the instruction are |
| 297 | // outside of the loop. In this case, it doesn't even matter if the |
| 298 | // operands of the instruction are loop invariant. |
| 299 | // |
| 300 | if (isNotUsedInLoop(I)) |
| 301 | sink(I); |
| 302 | |
| 303 | // If we can't sink the instruction, try hoisting it out to the |
| 304 | // preheader. We can only do this if all of the operands of the |
| 305 | // instruction are loop invariant and if it is safe to hoist the |
| 306 | // instruction. |
| 307 | // |
| 308 | else if (isLoopInvariantInst(I) && isSafeToExecuteUnconditionally(I)) |
| 309 | hoist(I); |
| 310 | } |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 312 | |
| 313 | const std::vector<DominatorTree::Node*> &Children = N->getChildren(); |
| 314 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 315 | HoistRegion(Children[i]); |
| 316 | } |
| 317 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 318 | /// canSinkOrHoistInst - Return true if the hoister and sinker can handle this |
| 319 | /// instruction. |
| 320 | /// |
| 321 | bool LICM::canSinkOrHoistInst(Instruction &I) { |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 322 | // Loads have extra constraints we have to verify before we can hoist them. |
| 323 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { |
| 324 | if (LI->isVolatile()) |
| 325 | return false; // Don't hoist volatile loads! |
| 326 | |
| 327 | // Don't hoist loads which have may-aliased stores in loop. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 328 | return !pointerInvalidatedByLoop(LI->getOperand(0)); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 331 | return isa<BinaryOperator>(I) || isa<ShiftInst>(I) || isa<CastInst>(I) || |
| 332 | isa<GetElementPtrInst>(I) || isa<VANextInst>(I) || isa<VAArgInst>(I); |
| 333 | } |
| 334 | |
| 335 | /// isNotUsedInLoop - Return true if the only users of this instruction are |
| 336 | /// outside of the loop. If this is true, we can sink the instruction to the |
| 337 | /// exit blocks of the loop. |
| 338 | /// |
| 339 | bool LICM::isNotUsedInLoop(Instruction &I) { |
Chris Lattner | ea9403f | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 340 | for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) { |
| 341 | Instruction *User = cast<Instruction>(*UI); |
| 342 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 343 | // PHI node uses occur in predecessor blocks! |
| 344 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 345 | if (PN->getIncomingValue(i) == &I) |
| 346 | if (CurLoop->contains(PN->getIncomingBlock(i))) |
| 347 | return false; |
| 348 | } else if (CurLoop->contains(User->getParent())) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 349 | return false; |
Chris Lattner | ea9403f | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /// isLoopInvariantInst - Return true if all operands of this instruction are |
| 357 | /// loop invariant. We also filter out non-hoistable instructions here just for |
| 358 | /// efficiency. |
| 359 | /// |
| 360 | bool LICM::isLoopInvariantInst(Instruction &I) { |
| 361 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 362 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 363 | if (!isLoopInvariant(I.getOperand(i))) |
| 364 | return false; |
| 365 | |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 366 | // If we got this far, the instruction is loop invariant! |
| 367 | return true; |
| 368 | } |
| 369 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 370 | /// sink - When an instruction is found to only be used outside of the loop, |
| 371 | /// this function moves it to the exit blocks and patches up SSA form as |
| 372 | /// needed. |
| 373 | /// |
| 374 | void LICM::sink(Instruction &I) { |
| 375 | DEBUG(std::cerr << "LICM sinking instruction: " << I); |
| 376 | |
| 377 | const std::vector<BasicBlock*> &ExitBlocks = CurLoop->getExitBlocks(); |
Chris Lattner | df45bd3 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 378 | std::vector<Value*> Operands(I.op_begin(), I.op_end()); |
| 379 | |
| 380 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
| 381 | ++NumSunk; |
| 382 | Changed = true; |
| 383 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 384 | // The case where there is only a single exit node of this loop is common |
| 385 | // enough that we handle it as a special (more efficient) case. It is more |
| 386 | // efficient to handle because there are no PHI nodes that need to be placed. |
| 387 | if (ExitBlocks.size() == 1) { |
| 388 | if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[0], I.getParent())) { |
| 389 | // Instruction is not used, just delete it. |
Chris Lattner | ba7df4c | 2003-12-18 08:12:32 +0000 | [diff] [blame^] | 390 | CurAST->remove(&I); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 391 | I.getParent()->getInstList().erase(&I); |
| 392 | } else { |
| 393 | // Move the instruction to the start of the exit block, after any PHI |
| 394 | // nodes in it. |
| 395 | I.getParent()->getInstList().remove(&I); |
| 396 | |
| 397 | BasicBlock::iterator InsertPt = ExitBlocks[0]->begin(); |
| 398 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 399 | ExitBlocks[0]->getInstList().insert(InsertPt, &I); |
| 400 | } |
| 401 | } else if (ExitBlocks.size() == 0) { |
| 402 | // The instruction is actually dead if there ARE NO exit blocks. |
Chris Lattner | ba7df4c | 2003-12-18 08:12:32 +0000 | [diff] [blame^] | 403 | CurAST->remove(&I); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 404 | I.getParent()->getInstList().erase(&I); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 405 | } else { |
| 406 | // Otherwise, if we have multiple exits, use the PromoteMem2Reg function to |
| 407 | // do all of the hard work of inserting PHI nodes as necessary. We convert |
| 408 | // the value into a stack object to get it to do this. |
| 409 | |
| 410 | // Firstly, we create a stack object to hold the value... |
| 411 | AllocaInst *AI = new AllocaInst(I.getType(), 0, I.getName(), |
| 412 | I.getParent()->getParent()->front().begin()); |
| 413 | |
| 414 | // Secondly, insert load instructions for each use of the instruction |
| 415 | // outside of the loop. |
| 416 | while (!I.use_empty()) { |
| 417 | Instruction *U = cast<Instruction>(I.use_back()); |
| 418 | |
| 419 | // If the user is a PHI Node, we actually have to insert load instructions |
| 420 | // in all predecessor blocks, not in the PHI block itself! |
| 421 | if (PHINode *UPN = dyn_cast<PHINode>(U)) { |
| 422 | // Only insert into each predecessor once, so that we don't have |
| 423 | // different incoming values from the same block! |
| 424 | std::map<BasicBlock*, Value*> InsertedBlocks; |
| 425 | for (unsigned i = 0, e = UPN->getNumIncomingValues(); i != e; ++i) |
| 426 | if (UPN->getIncomingValue(i) == &I) { |
| 427 | BasicBlock *Pred = UPN->getIncomingBlock(i); |
| 428 | Value *&PredVal = InsertedBlocks[Pred]; |
| 429 | if (!PredVal) { |
| 430 | // Insert a new load instruction right before the terminator in |
| 431 | // the predecessor block. |
| 432 | PredVal = new LoadInst(AI, "", Pred->getTerminator()); |
| 433 | } |
| 434 | |
| 435 | UPN->setIncomingValue(i, PredVal); |
| 436 | } |
| 437 | |
| 438 | } else { |
| 439 | LoadInst *L = new LoadInst(AI, "", U); |
| 440 | U->replaceUsesOfWith(&I, L); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Thirdly, insert a copy of the instruction in each exit block of the loop |
| 445 | // that is dominated by the instruction, storing the result into the memory |
| 446 | // location. Be careful not to insert the instruction into any particular |
| 447 | // basic block more than once. |
| 448 | std::set<BasicBlock*> InsertedBlocks; |
| 449 | BasicBlock *InstOrigBB = I.getParent(); |
| 450 | |
| 451 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 452 | BasicBlock *ExitBlock = ExitBlocks[i]; |
| 453 | |
| 454 | if (isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB)) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 455 | // If we haven't already processed this exit block, do so now. |
Chris Lattner | e3cfe8d | 2003-12-10 16:58:24 +0000 | [diff] [blame] | 456 | if (InsertedBlocks.insert(ExitBlock).second) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 457 | // Insert the code after the last PHI node... |
| 458 | BasicBlock::iterator InsertPt = ExitBlock->begin(); |
| 459 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 460 | |
| 461 | // If this is the first exit block processed, just move the original |
| 462 | // instruction, otherwise clone the original instruction and insert |
| 463 | // the copy. |
| 464 | Instruction *New; |
Chris Lattner | 7d3ced9 | 2003-12-10 22:35:56 +0000 | [diff] [blame] | 465 | if (InsertedBlocks.size() == 1) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 466 | I.getParent()->getInstList().remove(&I); |
| 467 | ExitBlock->getInstList().insert(InsertPt, &I); |
| 468 | New = &I; |
| 469 | } else { |
| 470 | New = I.clone(); |
| 471 | New->setName(I.getName()+".le"); |
| 472 | ExitBlock->getInstList().insert(InsertPt, New); |
| 473 | } |
| 474 | |
| 475 | // Now that we have inserted the instruction, store it into the alloca |
| 476 | new StoreInst(New, AI, InsertPt); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Finally, promote the fine value to SSA form. |
| 482 | std::vector<AllocaInst*> Allocas; |
| 483 | Allocas.push_back(AI); |
| 484 | PromoteMemToReg(Allocas, *DT, *DF, AA->getTargetData()); |
| 485 | } |
| 486 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 487 | // Since we just sunk an instruction, check to see if any other instructions |
| 488 | // used by this instruction are now sinkable. If so, sink them too. |
Chris Lattner | df45bd3 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 489 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 490 | if (Instruction *OpI = dyn_cast<Instruction>(Operands[i])) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 491 | if (CurLoop->contains(OpI->getParent()) && canSinkOrHoistInst(*OpI) && |
Chris Lattner | df45bd3 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 492 | isNotUsedInLoop(*OpI) && isSafeToExecuteUnconditionally(*OpI)) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 493 | sink(*OpI); |
| 494 | } |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 496 | /// hoist - When an instruction is found to only use loop invariant operands |
| 497 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 498 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 499 | void LICM::hoist(Instruction &I) { |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 500 | DEBUG(std::cerr << "LICM hoisting to"; |
| 501 | WriteAsOperand(std::cerr, Preheader, false); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 502 | std::cerr << ": " << I); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 504 | // Remove the instruction from its current basic block... but don't delete the |
| 505 | // instruction. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 506 | I.getParent()->getInstList().remove(&I); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 508 | // Insert the new node in Preheader, before the terminator. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 509 | Preheader->getInstList().insert(Preheader->getTerminator(), &I); |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 510 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 511 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 512 | ++NumHoisted; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 513 | Changed = true; |
| 514 | } |
| 515 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 516 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is |
| 517 | /// not a trapping instruction or if it is a trapping instruction and is |
| 518 | /// guaranteed to execute. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 519 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 520 | bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 521 | // If it is not a trapping instruction, it is always safe to hoist. |
| 522 | if (!Inst.isTrapping()) return true; |
| 523 | |
| 524 | // Otherwise we have to check to make sure that the instruction dominates all |
| 525 | // of the exit blocks. If it doesn't, then there is a path out of the loop |
| 526 | // which does not execute this instruction, so we can't hoist it. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 528 | // If the instruction is in the header block for the loop (which is very |
| 529 | // common), it is always guaranteed to dominate the exit blocks. Since this |
| 530 | // is a common case, and can save some work, check it now. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 531 | if (Inst.getParent() == CurLoop->getHeader()) |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 532 | return true; |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 534 | // Get the exit blocks for the current loop. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 535 | const std::vector<BasicBlock*> &ExitBlocks = CurLoop->getExitBlocks(); |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 536 | |
| 537 | // For each exit block, get the DT node and walk up the DT until the |
| 538 | // instruction's basic block is found or we exit the loop. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 539 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 540 | if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[i], Inst.getParent())) |
| 541 | return false; |
Tanya Lattner | 11a49a7 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 542 | |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
Chris Lattner | eb53ae4 | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 547 | /// PromoteValuesInLoop - Try to promote memory values to scalars by sinking |
| 548 | /// stores out of the loop and moving loads to before the loop. We do this by |
| 549 | /// looping over the stores in the loop, looking for stores to Must pointers |
| 550 | /// which are loop invariant. We promote these memory locations to use allocas |
| 551 | /// instead. These allocas can easily be raised to register values by the |
| 552 | /// PromoteMem2Reg functionality. |
| 553 | /// |
| 554 | void LICM::PromoteValuesInLoop() { |
| 555 | // PromotedValues - List of values that are promoted out of the loop. Each |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 556 | // value has an alloca instruction for it, and a canonical version of the |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 557 | // pointer. |
| 558 | std::vector<std::pair<AllocaInst*, Value*> > PromotedValues; |
| 559 | std::map<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca |
| 560 | |
| 561 | findPromotableValuesInLoop(PromotedValues, ValueToAllocaMap); |
| 562 | if (ValueToAllocaMap.empty()) return; // If there are values to promote... |
| 563 | |
| 564 | Changed = true; |
| 565 | NumPromoted += PromotedValues.size(); |
| 566 | |
| 567 | // Emit a copy from the value into the alloca'd value in the loop preheader |
| 568 | TerminatorInst *LoopPredInst = Preheader->getTerminator(); |
| 569 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
| 570 | // Load from the memory we are promoting... |
| 571 | LoadInst *LI = new LoadInst(PromotedValues[i].second, |
| 572 | PromotedValues[i].second->getName()+".promoted", |
| 573 | LoopPredInst); |
| 574 | // Store into the temporary alloca... |
| 575 | new StoreInst(LI, PromotedValues[i].first, LoopPredInst); |
| 576 | } |
| 577 | |
| 578 | // Scan the basic blocks in the loop, replacing uses of our pointers with |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 579 | // uses of the allocas in question. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 580 | // |
| 581 | const std::vector<BasicBlock*> &LoopBBs = CurLoop->getBlocks(); |
| 582 | for (std::vector<BasicBlock*>::const_iterator I = LoopBBs.begin(), |
| 583 | E = LoopBBs.end(); I != E; ++I) { |
| 584 | // Rewrite all loads and stores in the block of the pointer... |
| 585 | for (BasicBlock::iterator II = (*I)->begin(), E = (*I)->end(); |
| 586 | II != E; ++II) { |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 587 | if (LoadInst *L = dyn_cast<LoadInst>(II)) { |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 588 | std::map<Value*, AllocaInst*>::iterator |
| 589 | I = ValueToAllocaMap.find(L->getOperand(0)); |
| 590 | if (I != ValueToAllocaMap.end()) |
| 591 | L->setOperand(0, I->second); // Rewrite load instruction... |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 592 | } else if (StoreInst *S = dyn_cast<StoreInst>(II)) { |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 593 | std::map<Value*, AllocaInst*>::iterator |
| 594 | I = ValueToAllocaMap.find(S->getOperand(1)); |
| 595 | if (I != ValueToAllocaMap.end()) |
| 596 | S->setOperand(1, I->second); // Rewrite store instruction... |
| 597 | } |
| 598 | } |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 601 | // Now that the body of the loop uses the allocas instead of the original |
| 602 | // memory locations, insert code to copy the alloca value back into the |
| 603 | // original memory location on all exits from the loop. Note that we only |
| 604 | // want to insert one copy of the code in each exit block, though the loop may |
| 605 | // exit to the same block more than once. |
| 606 | // |
| 607 | std::set<BasicBlock*> ProcessedBlocks; |
| 608 | |
| 609 | const std::vector<BasicBlock*> &ExitBlocks = CurLoop->getExitBlocks(); |
| 610 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
Chris Lattner | f594a03 | 2003-12-10 16:57:24 +0000 | [diff] [blame] | 611 | if (ProcessedBlocks.insert(ExitBlocks[i]).second) { |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 612 | // Copy all of the allocas into their memory locations... |
| 613 | BasicBlock::iterator BI = ExitBlocks[i]->begin(); |
| 614 | while (isa<PHINode>(*BI)) |
| 615 | ++BI; // Skip over all of the phi nodes in the block... |
| 616 | Instruction *InsertPos = BI; |
| 617 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
| 618 | // Load from the alloca... |
| 619 | LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos); |
| 620 | // Store into the memory we promoted... |
| 621 | new StoreInst(LI, PromotedValues[i].second, InsertPos); |
| 622 | } |
| 623 | } |
| 624 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 625 | // Now that we have done the deed, use the mem2reg functionality to promote |
| 626 | // all of the new allocas we just created into real SSA registers... |
| 627 | // |
| 628 | std::vector<AllocaInst*> PromotedAllocas; |
| 629 | PromotedAllocas.reserve(PromotedValues.size()); |
| 630 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) |
| 631 | PromotedAllocas.push_back(PromotedValues[i].first); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 632 | PromoteMemToReg(PromotedAllocas, *DT, *DF, AA->getTargetData()); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Misha Brukman | 352361b | 2003-09-11 15:32:37 +0000 | [diff] [blame] | 635 | /// findPromotableValuesInLoop - Check the current loop for stores to definite |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 636 | /// pointers, which are not loaded and stored through may aliases. If these are |
| 637 | /// found, create an alloca for the value, add it to the PromotedValues list, |
| 638 | /// and keep track of the mapping from value to alloca... |
| 639 | /// |
| 640 | void LICM::findPromotableValuesInLoop( |
| 641 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
| 642 | std::map<Value*, AllocaInst*> &ValueToAllocaMap) { |
| 643 | Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); |
| 644 | |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 645 | // Loop over all of the alias sets in the tracker object... |
| 646 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 647 | I != E; ++I) { |
| 648 | AliasSet &AS = *I; |
| 649 | // We can promote this alias set if it has a store, if it is a "Must" alias |
| 650 | // set, and if the pointer is loop invariant. |
| 651 | if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias() && |
Chris Lattner | 00ad4a2 | 2003-12-14 04:52:31 +0000 | [diff] [blame] | 652 | !AS.isVolatile() && isLoopInvariant(AS.begin()->first)) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 653 | assert(AS.begin() != AS.end() && |
| 654 | "Must alias set should have at least one pointer element in it!"); |
| 655 | Value *V = AS.begin()->first; |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 657 | // Check that all of the pointers in the alias set have the same type. We |
| 658 | // cannot (yet) promote a memory location that is loaded and stored in |
| 659 | // different sizes. |
| 660 | bool PointerOk = true; |
| 661 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
| 662 | if (V->getType() != I->first->getType()) { |
| 663 | PointerOk = false; |
| 664 | break; |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 665 | } |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 666 | |
| 667 | if (PointerOk) { |
| 668 | const Type *Ty = cast<PointerType>(V->getType())->getElementType(); |
| 669 | AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart); |
| 670 | PromotedValues.push_back(std::make_pair(AI, V)); |
| 671 | |
| 672 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
| 673 | ValueToAllocaMap.insert(std::make_pair(I->first, AI)); |
| 674 | |
| 675 | DEBUG(std::cerr << "LICM: Promoting value: " << *V << "\n"); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 679 | } |