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