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