Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 10 | // This pass performs loop invariant code motion, attempting to remove as much |
| 11 | // code from the body of a loop as possible. It does this by either hoisting |
| 12 | // code into the preheader block, or by sinking code to the exit blocks if it is |
| 13 | // safe. This pass also promotes must-aliased memory locations in the loop to |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 14 | // live in registers, thus hoisting and sinking "invariant" loads and stores. |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 15 | // |
| 16 | // This pass uses alias analysis for two purposes: |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 17 | // |
Chris Lattner | 2741c97 | 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 | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 21 | // 2. Scalar Promotion of Memory - If there is a store instruction inside of |
| 22 | // the loop, we try to move the store to happen AFTER the loop instead of |
| 23 | // inside of the loop. This can only happen if a few conditions are true: |
| 24 | // A. The pointer stored through is loop invariant |
| 25 | // B. There are no stores or loads in the loop which _may_ alias the |
| 26 | // pointer. There are no calls in the loop which mod/ref the pointer. |
| 27 | // If these conditions are true, we can promote the loads and stores in the |
| 28 | // loop of the pointer to use a temporary alloca'd variable. We then use |
| 29 | // the mem2reg functionality to construct the appropriate SSA form for the |
| 30 | // variable. |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | 1f309c1 | 2005-03-23 21:00:12 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "licm" |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 36 | #include "llvm/Constants.h" |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 37 | #include "llvm/DerivedTypes.h" |
Torok Edwin | 9289ae8 | 2009-10-11 19:15:54 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 39 | #include "llvm/Instructions.h" |
| 40 | #include "llvm/Target/TargetData.h" |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/AliasSetTracker.h" |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/Dominators.h" |
Devang Patel | 96b651c | 2007-07-30 20:19:59 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/ScalarEvolution.h" |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 50 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
| 52 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 53 | #include <algorithm> |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 54 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 56 | STATISTIC(NumSunk , "Number of instructions sunk out of loop"); |
| 57 | STATISTIC(NumHoisted , "Number of instructions hoisted out of loop"); |
| 58 | STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk"); |
| 59 | STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk"); |
| 60 | STATISTIC(NumPromoted , "Number of memory locations promoted to registers"); |
| 61 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 62 | static cl::opt<bool> |
| 63 | DisablePromotion("disable-licm-promotion", cl::Hidden, |
| 64 | cl::desc("Disable memory promotion in LICM pass")); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 65 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 67 | struct LICM : public LoopPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 68 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 69 | LICM() : LoopPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 70 | |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 71 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 73 | /// This transformation requires natural loop information & requires that |
| 74 | /// loop preheaders be inserted into the CFG... |
| 75 | /// |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 76 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 77 | AU.setPreservesCFG(); |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 78 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 79 | AU.addRequired<DominanceFrontier>(); // For scalar promotion (mem2reg) |
Dan Gohman | 1e381fc | 2010-07-16 17:58:45 +0000 | [diff] [blame] | 80 | AU.addRequired<LoopInfo>(); |
| 81 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 82 | AU.addRequired<AliasAnalysis>(); |
Devang Patel | 96b651c | 2007-07-30 20:19:59 +0000 | [diff] [blame] | 83 | AU.addPreserved<ScalarEvolution>(); |
| 84 | AU.addPreserved<DominanceFrontier>(); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 85 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dan Gohman | 747603e | 2007-04-17 18:21:36 +0000 | [diff] [blame] | 88 | bool doFinalization() { |
Anton Korobeynikov | fd94dd5 | 2007-11-25 23:52:02 +0000 | [diff] [blame] | 89 | // Free the values stored in the map |
| 90 | for (std::map<Loop *, AliasSetTracker *>::iterator |
| 91 | I = LoopToAliasMap.begin(), E = LoopToAliasMap.end(); I != E; ++I) |
| 92 | delete I->second; |
| 93 | |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 94 | LoopToAliasMap.clear(); |
| 95 | return false; |
| 96 | } |
| 97 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 98 | private: |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 99 | // Various analyses that we use... |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 100 | AliasAnalysis *AA; // Current AliasAnalysis information |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 101 | LoopInfo *LI; // Current LoopInfo |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 102 | DominatorTree *DT; // Dominator Tree for the current Loop... |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 103 | DominanceFrontier *DF; // Current Dominance Frontier |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 104 | |
| 105 | // State that is updated as we process loops |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 106 | bool Changed; // Set to true when we change anything. |
| 107 | BasicBlock *Preheader; // The preheader block of the current loop... |
| 108 | Loop *CurLoop; // The current loop we are working on... |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 109 | AliasSetTracker *CurAST; // AliasSet information for the current loop... |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 110 | std::map<Loop *, AliasSetTracker *> LoopToAliasMap; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 111 | |
Devang Patel | 91d22c8 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 112 | /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. |
| 113 | void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L); |
| 114 | |
| 115 | /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias |
| 116 | /// set. |
| 117 | void deleteAnalysisValue(Value *V, Loop *L); |
| 118 | |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 119 | /// SinkRegion - Walk the specified region of the CFG (defined by all blocks |
| 120 | /// dominated by the specified block, and that are in the current loop) in |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 121 | /// reverse depth first order w.r.t the DominatorTree. This allows us to |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 122 | /// visit uses before definitions, allowing us to sink a loop body in one |
| 123 | /// pass without iteration. |
| 124 | /// |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 125 | void SinkRegion(DomTreeNode *N); |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 127 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 128 | /// blocks dominated by the specified block, and that are in the current |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 129 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 130 | /// visit definitions before uses, allowing us to hoist a loop body in one |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 131 | /// pass without iteration. |
| 132 | /// |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 133 | void HoistRegion(DomTreeNode *N); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 134 | |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 135 | /// inSubLoop - Little predicate that returns true if the specified basic |
| 136 | /// block is in a subloop of the current one, not the current one itself. |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 137 | /// |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 138 | bool inSubLoop(BasicBlock *BB) { |
| 139 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 140 | for (Loop::iterator I = CurLoop->begin(), E = CurLoop->end(); I != E; ++I) |
| 141 | if ((*I)->contains(BB)) |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 142 | return true; // A subloop actually contains this block! |
| 143 | return false; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 146 | /// isExitBlockDominatedByBlockInLoop - This method checks to see if the |
| 147 | /// specified exit block of the loop is dominated by the specified block |
| 148 | /// that is in the body of the loop. We use these constraints to |
| 149 | /// dramatically limit the amount of the dominator tree that needs to be |
| 150 | /// searched. |
| 151 | bool isExitBlockDominatedByBlockInLoop(BasicBlock *ExitBlock, |
| 152 | BasicBlock *BlockInLoop) const { |
| 153 | // If the block in the loop is the loop header, it must be dominated! |
| 154 | BasicBlock *LoopHeader = CurLoop->getHeader(); |
| 155 | if (BlockInLoop == LoopHeader) |
| 156 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 157 | |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 158 | DomTreeNode *BlockInLoopNode = DT->getNode(BlockInLoop); |
| 159 | DomTreeNode *IDom = DT->getNode(ExitBlock); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 160 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 161 | // Because the exit block is not in the loop, we know we have to get _at |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 162 | // least_ its immediate dominator. |
Eric Christopher | 3472ae1 | 2009-12-10 00:25:41 +0000 | [diff] [blame] | 163 | IDom = IDom->getIDom(); |
| 164 | |
| 165 | while (IDom && IDom != BlockInLoopNode) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 166 | // If we have got to the header of the loop, then the instructions block |
| 167 | // did not dominate the exit node, so we can't hoist it. |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 168 | if (IDom->getBlock() == LoopHeader) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 169 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 170 | |
Eric Christopher | 3472ae1 | 2009-12-10 00:25:41 +0000 | [diff] [blame] | 171 | // Get next Immediate Dominator. |
| 172 | IDom = IDom->getIDom(); |
| 173 | }; |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | /// sink - When an instruction is found to only be used outside of the loop, |
| 179 | /// this function moves it to the exit blocks and patches up SSA form as |
| 180 | /// needed. |
| 181 | /// |
| 182 | void sink(Instruction &I); |
| 183 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 184 | /// hoist - When an instruction is found to only use loop invariant operands |
| 185 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 186 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 187 | void hoist(Instruction &I); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 188 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 189 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it |
| 190 | /// is not a trapping instruction or if it is a trapping instruction and is |
| 191 | /// guaranteed to execute. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 192 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 193 | bool isSafeToExecuteUnconditionally(Instruction &I); |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 195 | /// pointerInvalidatedByLoop - Return true if the body of this loop may |
| 196 | /// store into the memory location pointed to by V. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 197 | /// |
Chris Lattner | f3e1d69 | 2004-11-26 21:20:09 +0000 | [diff] [blame] | 198 | bool pointerInvalidatedByLoop(Value *V, unsigned Size) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 199 | // Check to see if any of the basic blocks in CurLoop invalidate *V. |
Chris Lattner | f3e1d69 | 2004-11-26 21:20:09 +0000 | [diff] [blame] | 200 | return CurAST->getAliasSetForPointer(V, Size).isMod(); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 202 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 203 | bool canSinkOrHoistInst(Instruction &I); |
| 204 | bool isLoopInvariantInst(Instruction &I); |
| 205 | bool isNotUsedInLoop(Instruction &I); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 207 | /// PromoteValuesInLoop - Look at the stores in the loop and promote as many |
| 208 | /// to scalars as we can. |
| 209 | /// |
| 210 | void PromoteValuesInLoop(); |
| 211 | |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 212 | /// FindPromotableValuesInLoop - Check the current loop for stores to |
Misha Brukman | 352361b | 2003-09-11 15:32:37 +0000 | [diff] [blame] | 213 | /// definite pointers, which are not loaded and stored through may aliases. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 214 | /// If these are found, create an alloca for the value, add it to the |
| 215 | /// PromotedValues list, and keep track of the mapping from value to |
| 216 | /// alloca... |
| 217 | /// |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 218 | void FindPromotableValuesInLoop( |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 219 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 220 | DenseMap<Value*, AllocaInst*> &Val2AlMap); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 221 | }; |
| 222 | } |
| 223 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 224 | char LICM::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 225 | INITIALIZE_PASS(LICM, "licm", "Loop Invariant Code Motion", false, false); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 227 | Pass *llvm::createLICMPass() { return new LICM(); } |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 228 | |
Devang Patel | 8d246f0 | 2007-07-31 16:52:25 +0000 | [diff] [blame] | 229 | /// Hoist expressions out of the specified loop. Note, alias info for inner |
| 230 | /// loop is not preserved so it is not a good idea to run LICM multiple |
| 231 | /// times on one loop. |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 232 | /// |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 233 | bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 234 | Changed = false; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 236 | // Get our Loop and Alias Analysis information... |
| 237 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 238 | AA = &getAnalysis<AliasAnalysis>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 239 | DF = &getAnalysis<DominanceFrontier>(); |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 240 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 241 | |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 242 | CurAST = new AliasSetTracker(*AA); |
Devang Patel | f38ac5d | 2007-05-30 15:29:37 +0000 | [diff] [blame] | 243 | // Collect Alias info from subloops |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 244 | for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end(); |
| 245 | LoopItr != LoopItrE; ++LoopItr) { |
| 246 | Loop *InnerL = *LoopItr; |
| 247 | AliasSetTracker *InnerAST = LoopToAliasMap[InnerL]; |
| 248 | assert (InnerAST && "Where is my AST?"); |
| 249 | |
| 250 | // What if InnerLoop was modified by other passes ? |
| 251 | CurAST->add(*InnerAST); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 252 | } |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 253 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 254 | CurLoop = L; |
| 255 | |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 256 | // Get the preheader block to move instructions into... |
| 257 | Preheader = L->getLoopPreheader(); |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 259 | // Loop over the body of this loop, looking for calls, invokes, and stores. |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 260 | // Because subloops have already been incorporated into AST, we skip blocks in |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 261 | // subloops. |
| 262 | // |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 263 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 264 | I != E; ++I) { |
| 265 | BasicBlock *BB = *I; |
| 266 | if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops... |
| 267 | CurAST->add(*BB); // Incorporate the specified basic block |
| 268 | } |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 269 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 270 | // We want to visit all of the instructions in this loop... that are not parts |
| 271 | // of our subloops (they have already had their invariants hoisted out of |
| 272 | // their loop, into this loop, so there is no need to process the BODIES of |
| 273 | // the subloops). |
| 274 | // |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 275 | // Traverse the body of the loop in depth first order on the dominator tree so |
| 276 | // that we are guaranteed to see definitions before we see uses. This allows |
Nick Lewycky | af5cbc8 | 2007-08-18 15:08:56 +0000 | [diff] [blame] | 277 | // us to sink instructions in one pass, without iteration. After sinking |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 278 | // instructions, we perform another pass to hoist them out of the loop. |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 279 | // |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 280 | if (L->hasDedicatedExits()) |
| 281 | SinkRegion(DT->getNode(L->getHeader())); |
| 282 | if (Preheader) |
| 283 | HoistRegion(DT->getNode(L->getHeader())); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 285 | // Now that all loop invariants have been removed from the loop, promote any |
| 286 | // memory references to scalars that we can... |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 287 | if (!DisablePromotion && Preheader && L->hasDedicatedExits()) |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 288 | PromoteValuesInLoop(); |
| 289 | |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 290 | // Clear out loops state information for the next iteration |
| 291 | CurLoop = 0; |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 292 | Preheader = 0; |
Devang Patel | 54959d6 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 293 | |
| 294 | LoopToAliasMap[L] = CurAST; |
| 295 | return Changed; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 298 | /// SinkRegion - Walk the specified region of the CFG (defined by all blocks |
| 299 | /// dominated by the specified block, and that are in the current loop) in |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 300 | /// reverse depth first order w.r.t the DominatorTree. This allows us to visit |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 301 | /// uses before definitions, allowing us to sink a loop body in one pass without |
| 302 | /// iteration. |
| 303 | /// |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 304 | void LICM::SinkRegion(DomTreeNode *N) { |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 305 | assert(N != 0 && "Null dominator tree node?"); |
| 306 | BasicBlock *BB = N->getBlock(); |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 307 | |
| 308 | // If this subregion is not in the top level loop at all, exit. |
| 309 | if (!CurLoop->contains(BB)) return; |
| 310 | |
| 311 | // We are processing blocks in reverse dfo, so process children first... |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 312 | const std::vector<DomTreeNode*> &Children = N->getChildren(); |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 313 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 314 | SinkRegion(Children[i]); |
| 315 | |
| 316 | // Only need to process the contents of this block if it is not part of a |
| 317 | // subloop (which would already have been processed). |
| 318 | if (inSubLoop(BB)) return; |
| 319 | |
Chris Lattner | a3df8a9 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 320 | for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) { |
| 321 | Instruction &I = *--II; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 322 | |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 323 | // Check to see if we can sink this instruction to the exit blocks |
| 324 | // of the loop. We can do this if the all users of the instruction are |
| 325 | // outside of the loop. In this case, it doesn't even matter if the |
| 326 | // operands of the instruction are loop invariant. |
| 327 | // |
Chris Lattner | 70ac2dc | 2005-03-25 00:22:36 +0000 | [diff] [blame] | 328 | if (isNotUsedInLoop(I) && canSinkOrHoistInst(I)) { |
Chris Lattner | a3df8a9 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 329 | ++II; |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 330 | sink(I); |
Chris Lattner | a3df8a9 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 335 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 336 | /// dominated by the specified block, and that are in the current loop) in depth |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 337 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 338 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 339 | /// |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 340 | void LICM::HoistRegion(DomTreeNode *N) { |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 341 | assert(N != 0 && "Null dominator tree node?"); |
| 342 | BasicBlock *BB = N->getBlock(); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 343 | |
Chris Lattner | b461373 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 344 | // If this subregion is not in the top level loop at all, exit. |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 345 | if (!CurLoop->contains(BB)) return; |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 346 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 347 | // Only need to process the contents of this block if it is not part of a |
| 348 | // subloop (which would already have been processed). |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 349 | if (!inSubLoop(BB)) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 350 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) { |
| 351 | Instruction &I = *II++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 352 | |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 353 | // Try hoisting the instruction out to the preheader. We can only do this |
| 354 | // if all of the operands of the instruction are loop invariant and if it |
| 355 | // is safe to hoist the instruction. |
| 356 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 357 | if (isLoopInvariantInst(I) && canSinkOrHoistInst(I) && |
Chris Lattner | e4365b2 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 358 | isSafeToExecuteUnconditionally(I)) |
Chris Lattner | 3c80a51 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 359 | hoist(I); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 361 | |
Devang Patel | 2604242 | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 362 | const std::vector<DomTreeNode*> &Children = N->getChildren(); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 363 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
Chris Lattner | 56b7ee2 | 2004-06-19 20:23:35 +0000 | [diff] [blame] | 364 | HoistRegion(Children[i]); |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 367 | /// canSinkOrHoistInst - Return true if the hoister and sinker can handle this |
| 368 | /// instruction. |
| 369 | /// |
| 370 | bool LICM::canSinkOrHoistInst(Instruction &I) { |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 371 | // Loads have extra constraints we have to verify before we can hoist them. |
| 372 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { |
| 373 | if (LI->isVolatile()) |
| 374 | return false; // Don't hoist volatile loads! |
| 375 | |
Chris Lattner | 967948b | 2008-07-23 05:06:28 +0000 | [diff] [blame] | 376 | // Loads from constant memory are always safe to move, even if they end up |
| 377 | // in the same alias set as something that ends up being modified. |
Dan Gohman | dab249b | 2009-11-19 19:00:10 +0000 | [diff] [blame] | 378 | if (AA->pointsToConstantMemory(LI->getOperand(0))) |
Chris Lattner | 967948b | 2008-07-23 05:06:28 +0000 | [diff] [blame] | 379 | return true; |
| 380 | |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 381 | // Don't hoist loads which have may-aliased stores in loop. |
Chris Lattner | f3e1d69 | 2004-11-26 21:20:09 +0000 | [diff] [blame] | 382 | unsigned Size = 0; |
| 383 | if (LI->getType()->isSized()) |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 384 | Size = AA->getTypeStoreSize(LI->getType()); |
Chris Lattner | f3e1d69 | 2004-11-26 21:20:09 +0000 | [diff] [blame] | 385 | return !pointerInvalidatedByLoop(LI->getOperand(0), Size); |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 386 | } else if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 387 | // Handle obvious cases efficiently. |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 388 | AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI); |
| 389 | if (Behavior == AliasAnalysis::DoesNotAccessMemory) |
| 390 | return true; |
| 391 | else if (Behavior == AliasAnalysis::OnlyReadsMemory) { |
| 392 | // If this call only reads from memory and there are no writes to memory |
| 393 | // in the loop, we can hoist or sink the call as appropriate. |
| 394 | bool FoundMod = false; |
| 395 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 396 | I != E; ++I) { |
| 397 | AliasSet &AS = *I; |
| 398 | if (!AS.isForwardingAliasSet() && AS.isMod()) { |
| 399 | FoundMod = true; |
| 400 | break; |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 401 | } |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 402 | } |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 403 | if (!FoundMod) return true; |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // FIXME: This should use mod/ref information to see if we can hoist or sink |
| 407 | // the call. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 409 | return false; |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 412 | // Otherwise these instructions are hoistable/sinkable |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 413 | return isa<BinaryOperator>(I) || isa<CastInst>(I) || |
Dan Gohman | d8af90c | 2007-06-05 16:05:55 +0000 | [diff] [blame] | 414 | isa<SelectInst>(I) || isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || |
| 415 | isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || |
| 416 | isa<ShuffleVectorInst>(I); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// isNotUsedInLoop - Return true if the only users of this instruction are |
| 420 | /// outside of the loop. If this is true, we can sink the instruction to the |
| 421 | /// exit blocks of the loop. |
| 422 | /// |
| 423 | bool LICM::isNotUsedInLoop(Instruction &I) { |
Chris Lattner | ea9403f | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 424 | for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) { |
| 425 | Instruction *User = cast<Instruction>(*UI); |
| 426 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 427 | // PHI node uses occur in predecessor blocks! |
| 428 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 429 | if (PN->getIncomingValue(i) == &I) |
| 430 | if (CurLoop->contains(PN->getIncomingBlock(i))) |
| 431 | return false; |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 432 | } else if (CurLoop->contains(User)) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 433 | return false; |
Chris Lattner | ea9403f | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /// isLoopInvariantInst - Return true if all operands of this instruction are |
| 441 | /// loop invariant. We also filter out non-hoistable instructions here just for |
| 442 | /// efficiency. |
| 443 | /// |
| 444 | bool LICM::isLoopInvariantInst(Instruction &I) { |
| 445 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 446 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
Chris Lattner | 3280f7b | 2004-04-18 22:46:08 +0000 | [diff] [blame] | 447 | if (!CurLoop->isLoopInvariant(I.getOperand(i))) |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 448 | return false; |
| 449 | |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 450 | // If we got this far, the instruction is loop invariant! |
| 451 | return true; |
| 452 | } |
| 453 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 454 | /// sink - When an instruction is found to only be used outside of the loop, |
Chris Lattner | a3df8a9 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 455 | /// this function moves it to the exit blocks and patches up SSA form as needed. |
| 456 | /// This method is guaranteed to remove the original instruction from its |
| 457 | /// position, and may either delete it or move it to outside of the loop. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 458 | /// |
| 459 | void LICM::sink(Instruction &I) { |
Nick Lewycky | 39a3831 | 2010-07-30 20:27:01 +0000 | [diff] [blame] | 460 | DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n"); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 461 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 462 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 463 | CurLoop->getUniqueExitBlocks(ExitBlocks); |
Chris Lattner | df45bd3 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 464 | |
| 465 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 466 | else if (isa<CallInst>(I)) ++NumMovedCalls; |
Chris Lattner | df45bd3 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 467 | ++NumSunk; |
| 468 | Changed = true; |
| 469 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 470 | // The case where there is only a single exit node of this loop is common |
| 471 | // enough that we handle it as a special (more efficient) case. It is more |
| 472 | // efficient to handle because there are no PHI nodes that need to be placed. |
| 473 | if (ExitBlocks.size() == 1) { |
| 474 | if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[0], I.getParent())) { |
| 475 | // Instruction is not used, just delete it. |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 476 | CurAST->deleteValue(&I); |
Devang Patel | 1bf5ebc | 2009-10-13 21:41:20 +0000 | [diff] [blame] | 477 | // If I has users in unreachable blocks, eliminate. |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 478 | // If I is not void type then replaceAllUsesWith undef. |
| 479 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 480 | if (!I.getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 481 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 3c80a51 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 482 | I.eraseFromParent(); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 483 | } else { |
| 484 | // Move the instruction to the start of the exit block, after any PHI |
| 485 | // nodes in it. |
Chris Lattner | 3c80a51 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 486 | I.removeFromParent(); |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 487 | BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI(); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 488 | ExitBlocks[0]->getInstList().insert(InsertPt, &I); |
| 489 | } |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | if (ExitBlocks.empty()) { |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 494 | // The instruction is actually dead if there ARE NO exit blocks. |
Chris Lattner | 2741c97 | 2004-05-23 21:20:19 +0000 | [diff] [blame] | 495 | CurAST->deleteValue(&I); |
Devang Patel | 1bf5ebc | 2009-10-13 21:41:20 +0000 | [diff] [blame] | 496 | // If I has users in unreachable blocks, eliminate. |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 497 | // If I is not void type then replaceAllUsesWith undef. |
| 498 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 499 | if (!I.getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 500 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 3c80a51 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 501 | I.eraseFromParent(); |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 502 | return; |
| 503 | } |
| 504 | |
| 505 | // Otherwise, if we have multiple exits, use the PromoteMem2Reg function to |
| 506 | // do all of the hard work of inserting PHI nodes as necessary. We convert |
| 507 | // the value into a stack object to get it to do this. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 508 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 509 | // Firstly, we create a stack object to hold the value... |
| 510 | AllocaInst *AI = 0; |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 511 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 512 | if (!I.getType()->isVoidTy()) { |
| 513 | AI = new AllocaInst(I.getType(), 0, I.getName(), |
| 514 | I.getParent()->getParent()->getEntryBlock().begin()); |
| 515 | CurAST->add(AI); |
| 516 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 517 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 518 | // Secondly, insert load instructions for each use of the instruction |
| 519 | // outside of the loop. |
| 520 | while (!I.use_empty()) { |
| 521 | Instruction *U = cast<Instruction>(I.use_back()); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 522 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 523 | // If the user is a PHI Node, we actually have to insert load instructions |
| 524 | // in all predecessor blocks, not in the PHI block itself! |
| 525 | if (PHINode *UPN = dyn_cast<PHINode>(U)) { |
| 526 | // Only insert into each predecessor once, so that we don't have |
| 527 | // different incoming values from the same block! |
| 528 | DenseMap<BasicBlock*, Value*> InsertedBlocks; |
| 529 | for (unsigned i = 0, e = UPN->getNumIncomingValues(); i != e; ++i) { |
| 530 | if (UPN->getIncomingValue(i) != &I) continue; |
| 531 | |
| 532 | BasicBlock *Pred = UPN->getIncomingBlock(i); |
| 533 | Value *&PredVal = InsertedBlocks[Pred]; |
| 534 | if (!PredVal) { |
| 535 | // Insert a new load instruction right before the terminator in |
| 536 | // the predecessor block. |
| 537 | PredVal = new LoadInst(AI, "", Pred->getTerminator()); |
| 538 | CurAST->add(cast<LoadInst>(PredVal)); |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 541 | UPN->setIncomingValue(i, PredVal); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 543 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 544 | } else { |
| 545 | LoadInst *L = new LoadInst(AI, "", U); |
| 546 | U->replaceUsesOfWith(&I, L); |
| 547 | CurAST->add(L); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 548 | } |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 549 | } |
Chris Lattner | a3df8a9 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 550 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 551 | // Thirdly, insert a copy of the instruction in each exit block of the loop |
| 552 | // that is dominated by the instruction, storing the result into the memory |
| 553 | // location. Each exit block is known to only be in the ExitBlocks list once. |
| 554 | SmallPtrSet<BasicBlock*, 16> InsertedBlocks; |
| 555 | BasicBlock *InstOrigBB = I.getParent(); |
| 556 | |
| 557 | unsigned NumInserted = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 558 | |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 559 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 560 | BasicBlock *ExitBlock = ExitBlocks[i]; |
| 561 | |
| 562 | if (!isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB)) |
| 563 | continue; |
| 564 | |
| 565 | // Insert the code after the last PHI node... |
| 566 | BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI(); |
| 567 | |
| 568 | // If this is the first exit block processed, just move the original |
| 569 | // instruction, otherwise clone the original instruction and insert |
| 570 | // the copy. |
| 571 | Instruction *New; |
| 572 | if (NumInserted == 0) { |
| 573 | I.removeFromParent(); |
| 574 | ExitBlock->getInstList().insert(InsertPt, &I); |
| 575 | New = &I; |
| 576 | } else { |
| 577 | New = I.clone(); |
| 578 | CurAST->copyValue(&I, New); |
| 579 | if (!I.getName().empty()) |
| 580 | New->setName(I.getName()+".le"); |
| 581 | ExitBlock->getInstList().insert(InsertPt, New); |
Chris Lattner | eaa2430 | 2004-07-27 07:38:32 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | d7bc19d | 2010-08-29 04:28:20 +0000 | [diff] [blame^] | 583 | |
| 584 | ++NumInserted; |
| 585 | |
| 586 | // Now that we have inserted the instruction, store it into the alloca |
| 587 | if (AI) new StoreInst(New, AI, InsertPt); |
| 588 | } |
| 589 | |
| 590 | // If the instruction doesn't dominate any exit blocks, it must be dead. |
| 591 | if (NumInserted == 0) { |
| 592 | CurAST->deleteValue(&I); |
| 593 | I.eraseFromParent(); |
| 594 | } |
| 595 | |
| 596 | // Finally, promote the fine value to SSA form. |
| 597 | if (AI) { |
| 598 | std::vector<AllocaInst*> Allocas; |
| 599 | Allocas.push_back(AI); |
| 600 | PromoteMemToReg(Allocas, *DT, *DF, CurAST); |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | 952eaee | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 9417059 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 604 | /// hoist - When an instruction is found to only use loop invariant operands |
| 605 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 606 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 607 | void LICM::hoist(Instruction &I) { |
David Greene | f158269 | 2010-01-05 01:27:30 +0000 | [diff] [blame] | 608 | DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " |
Evan Cheng | 67d1d1f | 2009-10-12 22:25:23 +0000 | [diff] [blame] | 609 | << I << "\n"); |
Chris Lattner | ed6dfc2 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 99a5721 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 611 | // Remove the instruction from its current basic block... but don't delete the |
| 612 | // instruction. |
Chris Lattner | 3c80a51 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 613 | I.removeFromParent(); |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 615 | // Insert the new node in Preheader, before the terminator. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 616 | Preheader->getInstList().insert(Preheader->getTerminator(), &I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 617 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 618 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 619 | else if (isa<CallInst>(I)) ++NumMovedCalls; |
Chris Lattner | 9646e6b | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 620 | ++NumHoisted; |
Chris Lattner | e0e734e | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 621 | Changed = true; |
| 622 | } |
| 623 | |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 624 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is |
| 625 | /// not a trapping instruction or if it is a trapping instruction and is |
| 626 | /// guaranteed to execute. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 627 | /// |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 628 | bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 629 | // If it is not a trapping instruction, it is always safe to hoist. |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 630 | if (Inst.isSafeToSpeculativelyExecute()) |
| 631 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 633 | // Otherwise we have to check to make sure that the instruction dominates all |
| 634 | // of the exit blocks. If it doesn't, then there is a path out of the loop |
| 635 | // which does not execute this instruction, so we can't hoist it. |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 636 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 637 | // If the instruction is in the header block for the loop (which is very |
| 638 | // common), it is always guaranteed to dominate the exit blocks. Since this |
| 639 | // is a common case, and can save some work, check it now. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 640 | if (Inst.getParent() == CurLoop->getHeader()) |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 641 | return true; |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 643 | // Get the exit blocks for the current loop. |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 644 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 5fa802f | 2004-04-18 22:15:13 +0000 | [diff] [blame] | 645 | CurLoop->getExitBlocks(ExitBlocks); |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 646 | |
Owen Anderson | 3a2b58f | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 647 | // For each exit block, get the DT node and walk up the DT until the |
Chris Lattner | 92094b4 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 648 | // instruction's basic block is found or we exit the loop. |
Chris Lattner | a270651 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 649 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 650 | if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[i], Inst.getParent())) |
| 651 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 652 | |
Tanya Lattner | 9966c03 | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 653 | return true; |
| 654 | } |
| 655 | |
Chris Lattner | eb53ae4 | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 657 | /// PromoteValuesInLoop - Try to promote memory values to scalars by sinking |
| 658 | /// stores out of the loop and moving loads to before the loop. We do this by |
| 659 | /// looping over the stores in the loop, looking for stores to Must pointers |
| 660 | /// which are loop invariant. We promote these memory locations to use allocas |
| 661 | /// instead. These allocas can easily be raised to register values by the |
| 662 | /// PromoteMem2Reg functionality. |
| 663 | /// |
| 664 | void LICM::PromoteValuesInLoop() { |
| 665 | // PromotedValues - List of values that are promoted out of the loop. Each |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 666 | // value has an alloca instruction for it, and a canonical version of the |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 667 | // pointer. |
| 668 | std::vector<std::pair<AllocaInst*, Value*> > PromotedValues; |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 669 | DenseMap<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 670 | |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 671 | FindPromotableValuesInLoop(PromotedValues, ValueToAllocaMap); |
| 672 | if (ValueToAllocaMap.empty()) return; // If there are values to promote. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 673 | |
| 674 | Changed = true; |
| 675 | NumPromoted += PromotedValues.size(); |
| 676 | |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 677 | std::vector<Value*> PointerValueNumbers; |
| 678 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 679 | // Emit a copy from the value into the alloca'd value in the loop preheader |
| 680 | TerminatorInst *LoopPredInst = Preheader->getTerminator(); |
| 681 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 682 | Value *Ptr = PromotedValues[i].second; |
| 683 | |
| 684 | // If we are promoting a pointer value, update alias information for the |
| 685 | // inserted load. |
| 686 | Value *LoadValue = 0; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 687 | if (cast<PointerType>(Ptr->getType())->getElementType()->isPointerTy()) { |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 688 | // Locate a load or store through the pointer, and assign the same value |
| 689 | // to LI as we are loading or storing. Since we know that the value is |
| 690 | // stored in this loop, this will always succeed. |
| 691 | for (Value::use_iterator UI = Ptr->use_begin(), E = Ptr->use_end(); |
Gabor Greif | 488d442 | 2010-04-14 16:13:56 +0000 | [diff] [blame] | 692 | UI != E; ++UI) { |
| 693 | User *U = *UI; |
| 694 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 695 | LoadValue = LI; |
| 696 | break; |
Gabor Greif | 488d442 | 2010-04-14 16:13:56 +0000 | [diff] [blame] | 697 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | bdacd87 | 2004-09-15 02:34:40 +0000 | [diff] [blame] | 698 | if (SI->getOperand(1) == Ptr) { |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 699 | LoadValue = SI->getOperand(0); |
| 700 | break; |
| 701 | } |
| 702 | } |
Gabor Greif | 488d442 | 2010-04-14 16:13:56 +0000 | [diff] [blame] | 703 | } |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 704 | assert(LoadValue && "No store through the pointer found!"); |
| 705 | PointerValueNumbers.push_back(LoadValue); // Remember this for later. |
| 706 | } |
| 707 | |
| 708 | // Load from the memory we are promoting. |
| 709 | LoadInst *LI = new LoadInst(Ptr, Ptr->getName()+".promoted", LoopPredInst); |
| 710 | |
| 711 | if (LoadValue) CurAST->copyValue(LoadValue, LI); |
| 712 | |
| 713 | // Store into the temporary alloca. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 714 | new StoreInst(LI, PromotedValues[i].first, LoopPredInst); |
| 715 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 717 | // Scan the basic blocks in the loop, replacing uses of our pointers with |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 718 | // uses of the allocas in question. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 719 | // |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 720 | for (Loop::block_iterator I = CurLoop->block_begin(), |
| 721 | E = CurLoop->block_end(); I != E; ++I) { |
| 722 | BasicBlock *BB = *I; |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 723 | // Rewrite all loads and stores in the block of the pointer... |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 724 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 725 | if (LoadInst *L = dyn_cast<LoadInst>(II)) { |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 726 | DenseMap<Value*, AllocaInst*>::iterator |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 727 | I = ValueToAllocaMap.find(L->getOperand(0)); |
| 728 | if (I != ValueToAllocaMap.end()) |
| 729 | L->setOperand(0, I->second); // Rewrite load instruction... |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 730 | } else if (StoreInst *S = dyn_cast<StoreInst>(II)) { |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 731 | DenseMap<Value*, AllocaInst*>::iterator |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 732 | I = ValueToAllocaMap.find(S->getOperand(1)); |
| 733 | if (I != ValueToAllocaMap.end()) |
| 734 | S->setOperand(1, I->second); // Rewrite store instruction... |
| 735 | } |
| 736 | } |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 739 | // Now that the body of the loop uses the allocas instead of the original |
| 740 | // memory locations, insert code to copy the alloca value back into the |
| 741 | // original memory location on all exits from the loop. Note that we only |
| 742 | // want to insert one copy of the code in each exit block, though the loop may |
| 743 | // exit to the same block more than once. |
| 744 | // |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 745 | SmallPtrSet<BasicBlock*, 16> ProcessedBlocks; |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 746 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 747 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 5fa802f | 2004-04-18 22:15:13 +0000 | [diff] [blame] | 748 | CurLoop->getExitBlocks(ExitBlocks); |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 749 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 750 | if (!ProcessedBlocks.insert(ExitBlocks[i])) |
| 751 | continue; |
| 752 | |
| 753 | // Copy all of the allocas into their memory locations. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 754 | BasicBlock::iterator BI = ExitBlocks[i]->getFirstNonPHI(); |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 755 | Instruction *InsertPos = BI; |
| 756 | unsigned PVN = 0; |
| 757 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
| 758 | // Load from the alloca. |
| 759 | LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos); |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 761 | // If this is a pointer type, update alias info appropriately. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 762 | if (LI->getType()->isPointerTy()) |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 763 | CurAST->copyValue(PointerValueNumbers[PVN++], LI); |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 765 | // Store into the memory we promoted. |
| 766 | new StoreInst(LI, PromotedValues[i].second, InsertPos); |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 768 | } |
Chris Lattner | 0ed2da9 | 2003-12-10 15:56:24 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 770 | // Now that we have done the deed, use the mem2reg functionality to promote |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 771 | // all of the new allocas we just created into real SSA registers. |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 772 | // |
| 773 | std::vector<AllocaInst*> PromotedAllocas; |
| 774 | PromotedAllocas.reserve(PromotedValues.size()); |
| 775 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) |
| 776 | PromotedAllocas.push_back(PromotedValues[i].first); |
Nick Lewycky | ce2c51b | 2009-11-23 03:50:44 +0000 | [diff] [blame] | 777 | PromoteMemToReg(PromotedAllocas, *DT, *DF, CurAST); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 780 | /// FindPromotableValuesInLoop - Check the current loop for stores to definite |
Devang Patel | f2038b1 | 2007-09-19 20:18:51 +0000 | [diff] [blame] | 781 | /// pointers, which are not loaded and stored through may aliases and are safe |
| 782 | /// for promotion. If these are found, create an alloca for the value, add it |
| 783 | /// to the PromotedValues list, and keep track of the mapping from value to |
| 784 | /// alloca. |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 785 | void LICM::FindPromotableValuesInLoop( |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 786 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 787 | DenseMap<Value*, AllocaInst*> &ValueToAllocaMap) { |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 788 | Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); |
| 789 | |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 790 | // Loop over all of the alias sets in the tracker object. |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 791 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 792 | I != E; ++I) { |
| 793 | AliasSet &AS = *I; |
| 794 | // We can promote this alias set if it has a store, if it is a "Must" alias |
Chris Lattner | fb3ee19 | 2004-09-15 01:04:07 +0000 | [diff] [blame] | 795 | // set, if the pointer is loop invariant, and if we are not eliminating any |
Chris Lattner | 118dd0c | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 796 | // volatile loads or stores. |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 797 | if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || |
Chris Lattner | d7168dd | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 798 | AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue())) |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 799 | continue; |
| 800 | |
| 801 | assert(!AS.empty() && |
| 802 | "Must alias set should have at least one pointer element in it!"); |
Chris Lattner | d7168dd | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 803 | Value *V = AS.begin()->getValue(); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 805 | // Check that all of the pointers in the alias set have the same type. We |
| 806 | // cannot (yet) promote a memory location that is loaded and stored in |
| 807 | // different sizes. |
| 808 | { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 809 | bool PointerOk = true; |
| 810 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
Chris Lattner | d7168dd | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 811 | if (V->getType() != I->getValue()->getType()) { |
Chris Lattner | 0252e49 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 812 | PointerOk = false; |
| 813 | break; |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 815 | if (!PointerOk) |
| 816 | continue; |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 817 | } |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 818 | |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 819 | // It isn't safe to promote a load/store from the loop if the load/store is |
| 820 | // conditional. For example, turning: |
| 821 | // |
| 822 | // for () { if (c) *P += 1; } |
| 823 | // |
| 824 | // into: |
| 825 | // |
| 826 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; |
| 827 | // |
| 828 | // is not safe, because *P may only be valid to access if 'c' is true. |
| 829 | // |
| 830 | // It is safe to promote P if all uses are direct load/stores and if at |
| 831 | // least one is guaranteed to be executed. |
| 832 | bool GuaranteedToExecute = false; |
| 833 | bool InvalidInst = false; |
| 834 | for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 835 | UI != UE; ++UI) { |
| 836 | // Ignore instructions not in this loop. |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 837 | Instruction *Use = dyn_cast<Instruction>(*UI); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 838 | if (!Use || !CurLoop->contains(Use)) |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 839 | continue; |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 840 | |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 841 | if (!isa<LoadInst>(Use) && !isa<StoreInst>(Use)) { |
| 842 | InvalidInst = true; |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 843 | break; |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (!GuaranteedToExecute) |
| 847 | GuaranteedToExecute = isSafeToExecuteUnconditionally(*Use); |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | 19d9d43 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 850 | // If there is an non-load/store instruction in the loop, we can't promote |
| 851 | // it. If there isn't a guaranteed-to-execute instruction, we can't |
| 852 | // promote. |
| 853 | if (InvalidInst || !GuaranteedToExecute) |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 854 | continue; |
| 855 | |
| 856 | const Type *Ty = cast<PointerType>(V->getType())->getElementType(); |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 857 | AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart); |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 858 | PromotedValues.push_back(std::make_pair(AI, V)); |
| 859 | |
| 860 | // Update the AST and alias analysis. |
| 861 | CurAST->copyValue(V, AI); |
| 862 | |
| 863 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
Chris Lattner | dc1ceb3 | 2010-08-29 04:23:04 +0000 | [diff] [blame] | 864 | ValueToAllocaMap[I->getValue()] = AI; |
Chris Lattner | 29d9293 | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 865 | |
David Greene | f158269 | 2010-01-05 01:27:30 +0000 | [diff] [blame] | 866 | DEBUG(dbgs() << "LICM: Promoting value: " << *V << "\n"); |
Chris Lattner | 2e6e741 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 867 | } |
Chris Lattner | f5e84aa | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 868 | } |
Devang Patel | 91d22c8 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 869 | |
| 870 | /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. |
| 871 | void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) { |
| 872 | AliasSetTracker *AST = LoopToAliasMap[L]; |
| 873 | if (!AST) |
| 874 | return; |
| 875 | |
| 876 | AST->copyValue(From, To); |
| 877 | } |
| 878 | |
| 879 | /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias |
| 880 | /// set. |
| 881 | void LICM::deleteAnalysisValue(Value *V, Loop *L) { |
| 882 | AliasSetTracker *AST = LoopToAliasMap[L]; |
| 883 | if (!AST) |
| 884 | return; |
| 885 | |
| 886 | AST->deleteValue(V); |
| 887 | } |