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 | // |
Chris Lattner | f3ebc3f | 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 | 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 |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 29 | // the SSAUpdater to construct the appropriate SSA form for the value. |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 30 | // |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 1c790bf | 2005-03-23 21:00:12 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "licm" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/AliasSetTracker.h" |
Chris Lattner | 030f020 | 2010-08-31 23:00:16 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ConstantFolding.h" |
| 39 | #include "llvm/Analysis/LoopInfo.h" |
| 40 | #include "llvm/Analysis/LoopPass.h" |
Chandler Carruth | abfa3e5 | 2014-01-24 01:59:49 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/ScalarEvolution.h" |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame^] | 43 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Constants.h" |
| 45 | #include "llvm/IR/DataLayout.h" |
| 46 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 47 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Instructions.h" |
| 49 | #include "llvm/IR/IntrinsicInst.h" |
| 50 | #include "llvm/IR/LLVMContext.h" |
Chris Lattner | 473988c | 2013-01-05 16:44:07 +0000 | [diff] [blame] | 51 | #include "llvm/IR/Metadata.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
| 53 | #include "llvm/Support/Debug.h" |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 54 | #include "llvm/Support/PredIteratorCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Support/raw_ostream.h" |
| 56 | #include "llvm/Target/TargetLibraryInfo.h" |
| 57 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 59 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 60 | #include <algorithm> |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 61 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | STATISTIC(NumSunk , "Number of instructions sunk out of loop"); |
| 64 | STATISTIC(NumHoisted , "Number of instructions hoisted out of loop"); |
| 65 | STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk"); |
| 66 | STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk"); |
| 67 | STATISTIC(NumPromoted , "Number of memory locations promoted to registers"); |
| 68 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | static cl::opt<bool> |
| 70 | DisablePromotion("disable-licm-promotion", cl::Hidden, |
| 71 | cl::desc("Disable memory promotion in LICM pass")); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 72 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 73 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 74 | struct LICM : public LoopPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 75 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 76 | LICM() : LoopPass(ID) { |
| 77 | initializeLICMPass(*PassRegistry::getPassRegistry()); |
| 78 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 79 | |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 80 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 81 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 82 | /// This transformation requires natural loop information & requires that |
| 83 | /// loop preheaders be inserted into the CFG... |
| 84 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 85 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 86 | AU.setPreservesCFG(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 87 | AU.addRequired<DominatorTreeWrapperPass>(); |
Dan Gohman | efd7f9c | 2010-07-16 17:58:45 +0000 | [diff] [blame] | 88 | AU.addRequired<LoopInfo>(); |
| 89 | AU.addRequiredID(LoopSimplifyID); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 90 | AU.addPreservedID(LoopSimplifyID); |
| 91 | AU.addRequiredID(LCSSAID); |
| 92 | AU.addPreservedID(LCSSAID); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 93 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | f94f6bb | 2010-08-29 07:02:56 +0000 | [diff] [blame] | 94 | AU.addPreserved<AliasAnalysis>(); |
Chandler Carruth | abfa3e5 | 2014-01-24 01:59:49 +0000 | [diff] [blame] | 95 | AU.addPreserved<ScalarEvolution>(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 96 | AU.addRequired<TargetLibraryInfo>(); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 99 | using llvm::Pass::doFinalization; |
| 100 | |
Dan Gohman | 2ce1116 | 2007-04-17 18:21:36 +0000 | [diff] [blame] | 101 | bool doFinalization() { |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 102 | assert(LoopToAliasSetMap.empty() && "Didn't free loop alias sets"); |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 106 | private: |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 107 | AliasAnalysis *AA; // Current AliasAnalysis information |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 108 | LoopInfo *LI; // Current LoopInfo |
Chris Lattner | abe61ef | 2010-08-29 06:49:44 +0000 | [diff] [blame] | 109 | DominatorTree *DT; // Dominator Tree for the current Loop. |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 110 | |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 111 | const DataLayout *DL; // DataLayout for constant folding. |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 112 | TargetLibraryInfo *TLI; // TargetLibraryInfo for constant folding. |
| 113 | |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 114 | // State that is updated as we process loops. |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 115 | bool Changed; // Set to true when we change anything. |
| 116 | BasicBlock *Preheader; // The preheader block of the current loop... |
| 117 | Loop *CurLoop; // The current loop we are working on... |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 118 | AliasSetTracker *CurAST; // AliasSet information for the current loop... |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 119 | bool MayThrow; // The current loop contains an instruction which |
| 120 | // may throw, thus preventing code motion of |
| 121 | // instructions with side effects. |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 122 | DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 123 | |
Devang Patel | b98a097 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 124 | /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. |
| 125 | void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L); |
| 126 | |
| 127 | /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias |
| 128 | /// set. |
| 129 | void deleteAnalysisValue(Value *V, Loop *L); |
| 130 | |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 131 | /// SinkRegion - Walk the specified region of the CFG (defined by all blocks |
| 132 | /// dominated by the specified block, and that are in the current loop) in |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 133 | /// reverse depth first order w.r.t the DominatorTree. This allows us to |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 134 | /// visit uses before definitions, allowing us to sink a loop body in one |
| 135 | /// pass without iteration. |
| 136 | /// |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 137 | void SinkRegion(DomTreeNode *N); |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 139 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 140 | /// blocks dominated by the specified block, and that are in the current |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 141 | /// 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] | 142 | /// 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] | 143 | /// pass without iteration. |
| 144 | /// |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 145 | void HoistRegion(DomTreeNode *N); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 147 | /// inSubLoop - Little predicate that returns true if the specified basic |
| 148 | /// 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] | 149 | /// |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 150 | bool inSubLoop(BasicBlock *BB) { |
| 151 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
Chris Lattner | 0cdc6f6 | 2011-01-02 18:53:08 +0000 | [diff] [blame] | 152 | return LI->getLoopFor(BB) != CurLoop; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 155 | /// sink - When an instruction is found to only be used outside of the loop, |
| 156 | /// this function moves it to the exit blocks and patches up SSA form as |
| 157 | /// needed. |
| 158 | /// |
| 159 | void sink(Instruction &I); |
| 160 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 161 | /// hoist - When an instruction is found to only use loop invariant operands |
| 162 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 163 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 164 | void hoist(Instruction &I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 165 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 166 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it |
| 167 | /// is not a trapping instruction or if it is a trapping instruction and is |
| 168 | /// guaranteed to execute. |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 169 | /// |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 170 | bool isSafeToExecuteUnconditionally(Instruction &I); |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 171 | |
Eli Friedman | 0cdc148 | 2011-07-20 21:37:47 +0000 | [diff] [blame] | 172 | /// isGuaranteedToExecute - Check that the instruction is guaranteed to |
| 173 | /// execute. |
| 174 | /// |
| 175 | bool isGuaranteedToExecute(Instruction &I); |
| 176 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 177 | /// pointerInvalidatedByLoop - Return true if the body of this loop may |
| 178 | /// store into the memory location pointed to by V. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 179 | /// |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 180 | bool pointerInvalidatedByLoop(Value *V, uint64_t Size, |
Dan Gohman | 71af9db | 2010-10-18 20:44:50 +0000 | [diff] [blame] | 181 | const MDNode *TBAAInfo) { |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 182 | // Check to see if any of the basic blocks in CurLoop invalidate *V. |
Dan Gohman | 71af9db | 2010-10-18 20:44:50 +0000 | [diff] [blame] | 183 | return CurAST->getAliasSetForPointer(V, Size, TBAAInfo).isMod(); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 185 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 186 | bool canSinkOrHoistInst(Instruction &I); |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 187 | bool isNotUsedInLoop(Instruction &I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 188 | |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 189 | void PromoteAliasSet(AliasSet &AS, |
| 190 | SmallVectorImpl<BasicBlock*> &ExitBlocks, |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 191 | SmallVectorImpl<Instruction*> &InsertPts, |
| 192 | PredIteratorCache &PIC); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 193 | }; |
| 194 | } |
| 195 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 196 | char LICM::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 197 | INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 198 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 199 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 200 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 201 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 202 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 203 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 204 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 205 | INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 206 | |
Daniel Dunbar | 7f39e2d | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 207 | Pass *llvm::createLICMPass() { return new LICM(); } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 208 | |
Devang Patel | d8b1ceb | 2007-07-31 16:52:25 +0000 | [diff] [blame] | 209 | /// Hoist expressions out of the specified loop. Note, alias info for inner |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 210 | /// loop is not preserved so it is not a good idea to run LICM multiple |
Devang Patel | d8b1ceb | 2007-07-31 16:52:25 +0000 | [diff] [blame] | 211 | /// times on one loop. |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 212 | /// |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 213 | bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 214 | if (skipOptnoneFunction(L)) |
| 215 | return false; |
| 216 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 217 | Changed = false; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 219 | // Get our Loop and Alias Analysis information... |
| 220 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 221 | AA = &getAnalysis<AliasAnalysis>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 222 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 224 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 225 | DL = DLP ? &DLP->getDataLayout() : 0; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 226 | TLI = &getAnalysis<TargetLibraryInfo>(); |
| 227 | |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 228 | assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form."); |
| 229 | |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 230 | CurAST = new AliasSetTracker(*AA); |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 231 | // Collect Alias info from subloops. |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 232 | for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end(); |
| 233 | LoopItr != LoopItrE; ++LoopItr) { |
| 234 | Loop *InnerL = *LoopItr; |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 235 | AliasSetTracker *InnerAST = LoopToAliasSetMap[InnerL]; |
| 236 | assert(InnerAST && "Where is my AST?"); |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 237 | |
| 238 | // What if InnerLoop was modified by other passes ? |
| 239 | CurAST->add(*InnerAST); |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 240 | |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 241 | // Once we've incorporated the inner loop's AST into ours, we don't need the |
| 242 | // subloop's anymore. |
| 243 | delete InnerAST; |
| 244 | LoopToAliasSetMap.erase(InnerL); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 245 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 247 | CurLoop = L; |
| 248 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 249 | // Get the preheader block to move instructions into... |
| 250 | Preheader = L->getLoopPreheader(); |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 252 | // 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] | 253 | // Because subloops have already been incorporated into AST, we skip blocks in |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 254 | // subloops. |
| 255 | // |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 256 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 257 | I != E; ++I) { |
| 258 | BasicBlock *BB = *I; |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 259 | if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops. |
Dan Gohman | 9007107 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 260 | CurAST->add(*BB); // Incorporate the specified basic block |
| 261 | } |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 262 | |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 263 | MayThrow = false; |
| 264 | // TODO: We've already searched for instructions which may throw in subloops. |
| 265 | // We may want to reuse this information. |
| 266 | for (Loop::block_iterator BB = L->block_begin(), BBE = L->block_end(); |
| 267 | (BB != BBE) && !MayThrow ; ++BB) |
| 268 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); |
| 269 | (I != E) && !MayThrow; ++I) |
| 270 | MayThrow |= I->mayThrow(); |
| 271 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 272 | // We want to visit all of the instructions in this loop... that are not parts |
| 273 | // of our subloops (they have already had their invariants hoisted out of |
| 274 | // their loop, into this loop, so there is no need to process the BODIES of |
| 275 | // the subloops). |
| 276 | // |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 277 | // Traverse the body of the loop in depth first order on the dominator tree so |
| 278 | // that we are guaranteed to see definitions before we see uses. This allows |
Nick Lewycky | a0d49da | 2007-08-18 15:08:56 +0000 | [diff] [blame] | 279 | // us to sink instructions in one pass, without iteration. After sinking |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 280 | // instructions, we perform another pass to hoist them out of the loop. |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 281 | // |
Dan Gohman | a83ac2d | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 282 | if (L->hasDedicatedExits()) |
| 283 | SinkRegion(DT->getNode(L->getHeader())); |
| 284 | if (Preheader) |
| 285 | HoistRegion(DT->getNode(L->getHeader())); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 287 | // Now that all loop invariants have been removed from the loop, promote any |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 288 | // memory references to scalars that we can. |
Chandler Carruth | cc497b6 | 2014-01-24 02:24:47 +0000 | [diff] [blame] | 289 | if (!DisablePromotion && (Preheader || L->hasDedicatedExits())) { |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 290 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 291 | SmallVector<Instruction *, 8> InsertPts; |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 292 | PredIteratorCache PIC; |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 294 | // Loop over all of the alias sets in the tracker object. |
| 295 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 296 | I != E; ++I) |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 297 | PromoteAliasSet(*I, ExitBlocks, InsertPts, PIC); |
Chandler Carruth | 1665152 | 2014-02-01 13:35:14 +0000 | [diff] [blame] | 298 | |
| 299 | // Once we have promoted values across the loop body we have to recursively |
| 300 | // reform LCSSA as any nested loop may now have values defined within the |
| 301 | // loop used in the outer loop. |
| 302 | // FIXME: This is really heavy handed. It would be a bit better to use an |
| 303 | // SSAUpdater strategy during promotion that was LCSSA aware and reformed |
| 304 | // it as it went. |
| 305 | if (Changed) |
| 306 | formLCSSARecursively(*L, *DT, getAnalysisIfAvailable<ScalarEvolution>()); |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 307 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 308 | |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 309 | // Check that neither this loop nor its parent have had LCSSA broken. LICM is |
| 310 | // specifically moving instructions across the loop boundary and so it is |
| 311 | // especially in need of sanity checking here. |
| 312 | assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!"); |
| 313 | assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) && |
| 314 | "Parent loop not left in LCSSA form after LICM!"); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 316 | // Clear out loops state information for the next iteration |
| 317 | CurLoop = 0; |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 318 | Preheader = 0; |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 319 | |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 320 | // If this loop is nested inside of another one, save the alias information |
| 321 | // for when we process the outer loop. |
| 322 | if (L->getParentLoop()) |
| 323 | LoopToAliasSetMap[L] = CurAST; |
| 324 | else |
| 325 | delete CurAST; |
Devang Patel | 69730c9 | 2007-03-07 04:41:30 +0000 | [diff] [blame] | 326 | return Changed; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 329 | /// SinkRegion - Walk the specified region of the CFG (defined by all blocks |
| 330 | /// dominated by the specified block, and that are in the current loop) in |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 331 | /// reverse depth first order w.r.t the DominatorTree. This allows us to visit |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 332 | /// uses before definitions, allowing us to sink a loop body in one pass without |
| 333 | /// iteration. |
| 334 | /// |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 335 | void LICM::SinkRegion(DomTreeNode *N) { |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 336 | assert(N != 0 && "Null dominator tree node?"); |
| 337 | BasicBlock *BB = N->getBlock(); |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 338 | |
| 339 | // If this subregion is not in the top level loop at all, exit. |
| 340 | if (!CurLoop->contains(BB)) return; |
| 341 | |
Chris Lattner | 263f804 | 2010-08-29 18:22:25 +0000 | [diff] [blame] | 342 | // We are processing blocks in reverse dfo, so process children first. |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 343 | const std::vector<DomTreeNode*> &Children = N->getChildren(); |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 344 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 345 | SinkRegion(Children[i]); |
| 346 | |
| 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). |
| 349 | if (inSubLoop(BB)) return; |
| 350 | |
Chris Lattner | 9184601 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 351 | for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) { |
| 352 | Instruction &I = *--II; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 263f804 | 2010-08-29 18:22:25 +0000 | [diff] [blame] | 354 | // If the instruction is dead, we would try to sink it because it isn't used |
| 355 | // in the loop, instead, just delete it. |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 356 | if (isInstructionTriviallyDead(&I, TLI)) { |
Chris Lattner | f58382e | 2010-08-29 18:42:23 +0000 | [diff] [blame] | 357 | DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n'); |
Chris Lattner | 263f804 | 2010-08-29 18:22:25 +0000 | [diff] [blame] | 358 | ++II; |
| 359 | CurAST->deleteValue(&I); |
| 360 | I.eraseFromParent(); |
| 361 | Changed = true; |
| 362 | continue; |
| 363 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 365 | // Check to see if we can sink this instruction to the exit blocks |
| 366 | // of the loop. We can do this if the all users of the instruction are |
| 367 | // outside of the loop. In this case, it doesn't even matter if the |
| 368 | // operands of the instruction are loop invariant. |
| 369 | // |
Chris Lattner | faf7791 | 2005-03-25 00:22:36 +0000 | [diff] [blame] | 370 | if (isNotUsedInLoop(I) && canSinkOrHoistInst(I)) { |
Chris Lattner | 9184601 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 371 | ++II; |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 372 | sink(I); |
Chris Lattner | 9184601 | 2003-12-19 08:18:16 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 377 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 378 | /// dominated by the specified block, and that are in the current loop) in depth |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 379 | /// 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] | 380 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 381 | /// |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 382 | void LICM::HoistRegion(DomTreeNode *N) { |
Owen Anderson | c24701e | 2007-04-24 06:40:39 +0000 | [diff] [blame] | 383 | assert(N != 0 && "Null dominator tree node?"); |
| 384 | BasicBlock *BB = N->getBlock(); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 386 | // 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] | 387 | if (!CurLoop->contains(BB)) return; |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 388 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 389 | // Only need to process the contents of this block if it is not part of a |
| 390 | // subloop (which would already have been processed). |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 391 | if (!inSubLoop(BB)) |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 392 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) { |
| 393 | Instruction &I = *II++; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 030f020 | 2010-08-31 23:00:16 +0000 | [diff] [blame] | 395 | // Try constant folding this instruction. If all the operands are |
| 396 | // constants, it is technically hoistable, but it would be better to just |
| 397 | // fold it. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 398 | if (Constant *C = ConstantFoldInstruction(&I, DL, TLI)) { |
Chris Lattner | 030f020 | 2010-08-31 23:00:16 +0000 | [diff] [blame] | 399 | DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'); |
| 400 | CurAST->copyValue(&I, C); |
| 401 | CurAST->deleteValue(&I); |
| 402 | I.replaceAllUsesWith(C); |
| 403 | I.eraseFromParent(); |
| 404 | continue; |
| 405 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 407 | // Try hoisting the instruction out to the preheader. We can only do this |
| 408 | // if all of the operands of the instruction are loop invariant and if it |
| 409 | // is safe to hoist the instruction. |
| 410 | // |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 411 | if (CurLoop->hasLoopInvariantOperands(&I) && canSinkOrHoistInst(I) && |
Chris Lattner | 547192d6 | 2003-12-19 07:22:45 +0000 | [diff] [blame] | 412 | isSafeToExecuteUnconditionally(I)) |
Chris Lattner | 49771a0 | 2006-06-26 19:10:05 +0000 | [diff] [blame] | 413 | hoist(I); |
Chris Lattner | 030f020 | 2010-08-31 23:00:16 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 415 | |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 416 | const std::vector<DomTreeNode*> &Children = N->getChildren(); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 417 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
Chris Lattner | 42ad646 | 2004-06-19 20:23:35 +0000 | [diff] [blame] | 418 | HoistRegion(Children[i]); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 421 | /// canSinkOrHoistInst - Return true if the hoister and sinker can handle this |
| 422 | /// instruction. |
| 423 | /// |
| 424 | bool LICM::canSinkOrHoistInst(Instruction &I) { |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 425 | // Loads have extra constraints we have to verify before we can hoist them. |
| 426 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { |
Eli Friedman | 91386c7 | 2011-08-15 20:52:09 +0000 | [diff] [blame] | 427 | if (!LI->isUnordered()) |
| 428 | return false; // Don't hoist volatile/atomic loads! |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 8a8fb90 | 2008-07-23 05:06:28 +0000 | [diff] [blame] | 430 | // Loads from constant memory are always safe to move, even if they end up |
| 431 | // in the same alias set as something that ends up being modified. |
Dan Gohman | cbc6ebb | 2009-11-19 19:00:10 +0000 | [diff] [blame] | 432 | if (AA->pointsToConstantMemory(LI->getOperand(0))) |
Chris Lattner | 8a8fb90 | 2008-07-23 05:06:28 +0000 | [diff] [blame] | 433 | return true; |
Benjamin Kramer | b3bd019 | 2011-12-06 11:50:26 +0000 | [diff] [blame] | 434 | if (LI->getMetadata("invariant.load")) |
Pete Cooper | 9ee2209 | 2011-11-08 19:30:00 +0000 | [diff] [blame] | 435 | return true; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 437 | // Don't hoist loads which have may-aliased stores in loop. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 438 | uint64_t Size = 0; |
Chris Lattner | b137409 | 2004-11-26 21:20:09 +0000 | [diff] [blame] | 439 | if (LI->getType()->isSized()) |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 440 | Size = AA->getTypeStoreSize(LI->getType()); |
Dan Gohman | 71af9db | 2010-10-18 20:44:50 +0000 | [diff] [blame] | 441 | return !pointerInvalidatedByLoop(LI->getOperand(0), Size, |
| 442 | LI->getMetadata(LLVMContext::MD_tbaa)); |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 443 | } else if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
Eli Friedman | 942e1c1 | 2011-05-27 18:37:52 +0000 | [diff] [blame] | 444 | // Don't sink or hoist dbg info; it's legal, but not useful. |
| 445 | if (isa<DbgInfoIntrinsic>(I)) |
| 446 | return false; |
| 447 | |
| 448 | // Handle simple cases by querying alias analysis. |
Duncan Sands | 68b6f50 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 449 | AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI); |
| 450 | if (Behavior == AliasAnalysis::DoesNotAccessMemory) |
| 451 | return true; |
Dan Gohman | 0f17507 | 2010-11-09 19:58:21 +0000 | [diff] [blame] | 452 | if (AliasAnalysis::onlyReadsMemory(Behavior)) { |
Duncan Sands | 68b6f50 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 453 | // If this call only reads from memory and there are no writes to memory |
| 454 | // in the loop, we can hoist or sink the call as appropriate. |
| 455 | bool FoundMod = false; |
| 456 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 457 | I != E; ++I) { |
| 458 | AliasSet &AS = *I; |
| 459 | if (!AS.isForwardingAliasSet() && AS.isMod()) { |
| 460 | FoundMod = true; |
| 461 | break; |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 462 | } |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 463 | } |
Duncan Sands | 68b6f50 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 464 | if (!FoundMod) return true; |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 467 | // FIXME: This should use mod/ref information to see if we can hoist or |
| 468 | // sink the call. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 470 | return false; |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 473 | // Only these instructions are hoistable/sinkable. |
Benjamin Kramer | 130fcde | 2013-01-09 18:12:03 +0000 | [diff] [blame] | 474 | if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) && |
| 475 | !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) && |
| 476 | !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) && |
| 477 | !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) && |
| 478 | !isa<InsertValueInst>(I)) |
| 479 | return false; |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 480 | |
| 481 | return isSafeToExecuteUnconditionally(I); |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 484 | /// \brief Returns true if a PHINode is a trivially replaceable with an |
| 485 | /// Instruction. |
| 486 | /// |
| 487 | /// This is true when all incoming values are that instruction. This pattern |
| 488 | /// occurs most often with LCSSA PHI nodes. |
| 489 | static bool isTriviallyReplacablePHI(PHINode &PN, Instruction &I) { |
| 490 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 491 | if (PN.getIncomingValue(i) != &I) |
| 492 | return false; |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 497 | /// isNotUsedInLoop - Return true if the only users of this instruction are |
| 498 | /// outside of the loop. If this is true, we can sink the instruction to the |
| 499 | /// exit blocks of the loop. |
| 500 | /// |
| 501 | bool LICM::isNotUsedInLoop(Instruction &I) { |
Chris Lattner | 34399dd | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 502 | for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) { |
| 503 | Instruction *User = cast<Instruction>(*UI); |
| 504 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 505 | // A PHI node where all of the incoming values are this instruction are |
| 506 | // special -- they can just be RAUW'ed with the instruction and thus |
| 507 | // don't require a use in the predecessor. This is a particular important |
| 508 | // special case because it is the pattern found in LCSSA form. |
| 509 | if (isTriviallyReplacablePHI(*PN, I)) { |
| 510 | if (CurLoop->contains(PN)) |
| 511 | return false; |
| 512 | else |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | // Otherwise, PHI node uses occur in predecessor blocks if the incoming |
| 517 | // values. Check for such a use being inside the loop. |
Chris Lattner | 34399dd | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 518 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 519 | if (PN->getIncomingValue(i) == &I) |
| 520 | if (CurLoop->contains(PN->getIncomingBlock(i))) |
| 521 | return false; |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 522 | |
| 523 | continue; |
Chris Lattner | 34399dd | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 524 | } |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 525 | |
| 526 | if (CurLoop->contains(User)) |
| 527 | return false; |
Chris Lattner | 34399dd | 2003-12-11 22:23:32 +0000 | [diff] [blame] | 528 | } |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 529 | return true; |
| 530 | } |
| 531 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 532 | /// 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] | 533 | /// this function moves it to the exit blocks and patches up SSA form as needed. |
| 534 | /// This method is guaranteed to remove the original instruction from its |
| 535 | /// 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] | 536 | /// |
| 537 | void LICM::sink(Instruction &I) { |
Nick Lewycky | 299c6df | 2010-07-30 20:27:01 +0000 | [diff] [blame] | 538 | DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n"); |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 55c2113 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 540 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 541 | else if (isa<CallInst>(I)) ++NumMovedCalls; |
Chris Lattner | 55c2113 | 2003-12-10 20:43:29 +0000 | [diff] [blame] | 542 | ++NumSunk; |
| 543 | Changed = true; |
| 544 | |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 545 | #ifndef NDEBUG |
| 546 | SmallVector<BasicBlock *, 32> ExitBlocks; |
| 547 | CurLoop->getUniqueExitBlocks(ExitBlocks); |
| 548 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), ExitBlocks.end()); |
| 549 | #endif |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 550 | |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 551 | // If this instruction is only used outside of the loop, then all users are |
| 552 | // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of |
| 553 | // the instruction. |
| 554 | while (!I.use_empty()) { |
| 555 | // The user must be a PHI node. |
| 556 | PHINode *PN = cast<PHINode>(I.use_back()); |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 557 | |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 558 | BasicBlock *ExitBlock = PN->getParent(); |
| 559 | assert(ExitBlockSet.count(ExitBlock) && |
| 560 | "The LCSSA PHI is not in an exit block!"); |
| 561 | |
| 562 | Instruction *New = I.clone(); |
| 563 | ExitBlock->getInstList().insert(ExitBlock->getFirstInsertionPt(), New); |
| 564 | if (!I.getName().empty()) |
| 565 | New->setName(I.getName() + ".le"); |
| 566 | |
| 567 | // Build LCSSA PHI nodes for any in-loop operands. Note that this is |
| 568 | // particularly cheap because we can rip off the PHI node that we're |
| 569 | // replacing for the number and blocks of the predecessors. |
| 570 | // OPT: If this shows up in a profile, we can instead finish sinking all |
| 571 | // invariant instructions, and then walk their operands to re-establish |
| 572 | // LCSSA. That will eliminate creating PHI nodes just to nuke them when |
| 573 | // sinking bottom-up. |
| 574 | for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE; |
| 575 | ++OI) |
| 576 | if (Instruction *OInst = dyn_cast<Instruction>(*OI)) |
| 577 | if (Loop *OLoop = LI->getLoopFor(OInst->getParent())) |
| 578 | if (!OLoop->contains(PN)) { |
| 579 | PHINode *OpPN = PHINode::Create( |
| 580 | OInst->getType(), PN->getNumIncomingValues(), |
| 581 | OInst->getName() + ".lcssa", ExitBlock->begin()); |
| 582 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 583 | OpPN->addIncoming(OInst, PN->getIncomingBlock(i)); |
| 584 | *OI = OpPN; |
| 585 | } |
| 586 | |
| 587 | PN->replaceAllUsesWith(New); |
| 588 | PN->eraseFromParent(); |
Chris Lattner | cd96b4d | 2010-08-29 04:28:20 +0000 | [diff] [blame] | 589 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 1a1ed69 | 2010-08-29 18:00:00 +0000 | [diff] [blame] | 591 | CurAST->deleteValue(&I); |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 592 | I.eraseFromParent(); |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 593 | } |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 594 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 595 | /// hoist - When an instruction is found to only use loop invariant operands |
| 596 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 597 | /// |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 598 | void LICM::hoist(Instruction &I) { |
David Greene | 0fd8622 | 2010-01-05 01:27:30 +0000 | [diff] [blame] | 599 | DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " |
Evan Cheng | f815861 | 2009-10-12 22:25:23 +0000 | [diff] [blame] | 600 | << I << "\n"); |
Chris Lattner | 65c1193 | 2003-12-09 19:32:44 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 6ac0659 | 2010-08-29 18:18:40 +0000 | [diff] [blame] | 602 | // Move the new node to the Preheader, before its terminator. |
| 603 | I.moveBefore(Preheader->getTerminator()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 604 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 605 | if (isa<LoadInst>(I)) ++NumMovedLoads; |
Chris Lattner | 20cda26 | 2004-03-15 04:11:30 +0000 | [diff] [blame] | 606 | else if (isa<CallInst>(I)) ++NumMovedCalls; |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 607 | ++NumHoisted; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 608 | Changed = true; |
| 609 | } |
| 610 | |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 611 | /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is |
| 612 | /// not a trapping instruction or if it is a trapping instruction and is |
| 613 | /// guaranteed to execute. |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 614 | /// |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 615 | bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 616 | // If it is not a trapping instruction, it is always safe to hoist. |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 617 | if (isSafeToSpeculativelyExecute(&Inst)) |
Eli Friedman | b8f6a4f | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 618 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 619 | |
Eli Friedman | 0cdc148 | 2011-07-20 21:37:47 +0000 | [diff] [blame] | 620 | return isGuaranteedToExecute(Inst); |
| 621 | } |
| 622 | |
| 623 | bool LICM::isGuaranteedToExecute(Instruction &Inst) { |
Nadav Rotem | 03dcd85 | 2012-09-04 10:25:04 +0000 | [diff] [blame] | 624 | |
| 625 | // Somewhere in this loop there is an instruction which may throw and make us |
| 626 | // exit the loop. |
| 627 | if (MayThrow) |
| 628 | return false; |
| 629 | |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 630 | // Otherwise we have to check to make sure that the instruction dominates all |
| 631 | // of the exit blocks. If it doesn't, then there is a path out of the loop |
| 632 | // which does not execute this instruction, so we can't hoist it. |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 633 | |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 634 | // If the instruction is in the header block for the loop (which is very |
| 635 | // common), it is always guaranteed to dominate the exit blocks. Since this |
| 636 | // is a common case, and can save some work, check it now. |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 637 | if (Inst.getParent() == CurLoop->getHeader()) |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 638 | return true; |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 639 | |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 640 | // Get the exit blocks for the current loop. |
Devang Patel | b5933bb | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 641 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 35eaa55 | 2004-04-18 22:15:13 +0000 | [diff] [blame] | 642 | CurLoop->getExitBlocks(ExitBlocks); |
Chris Lattner | c051768 | 2003-12-09 17:18:00 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 27497ec | 2011-01-02 18:45:39 +0000 | [diff] [blame] | 644 | // Verify that the block dominates each of the exit blocks of the loop. |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 645 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
Chris Lattner | 27497ec | 2011-01-02 18:45:39 +0000 | [diff] [blame] | 646 | if (!DT->dominates(Inst.getParent(), ExitBlocks[i])) |
Chris Lattner | aaaea51 | 2003-12-10 06:41:05 +0000 | [diff] [blame] | 647 | return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 648 | |
Nick Lewycky | 78ee67e | 2012-05-01 04:03:01 +0000 | [diff] [blame] | 649 | // As a degenerate case, if the loop is statically infinite then we haven't |
| 650 | // proven anything since there are no exit blocks. |
| 651 | if (ExitBlocks.empty()) |
| 652 | return false; |
| 653 | |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 654 | return true; |
| 655 | } |
| 656 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 657 | namespace { |
| 658 | class LoopPromoter : public LoadAndStorePromoter { |
| 659 | Value *SomePtr; // Designated pointer to store to. |
| 660 | SmallPtrSet<Value*, 4> &PointerMustAliases; |
| 661 | SmallVectorImpl<BasicBlock*> &LoopExitBlocks; |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 662 | SmallVectorImpl<Instruction*> &LoopInsertPts; |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 663 | PredIteratorCache &PredCache; |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 664 | AliasSetTracker &AST; |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 665 | LoopInfo &LI; |
Eli Friedman | ddf7f55 | 2011-05-27 20:31:51 +0000 | [diff] [blame] | 666 | DebugLoc DL; |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 667 | int Alignment; |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 668 | MDNode *TBAATag; |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 669 | |
| 670 | Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { |
| 671 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 672 | if (Loop *L = LI.getLoopFor(I->getParent())) |
| 673 | if (!L->contains(BB)) { |
| 674 | // We need to create an LCSSA PHI node for the incoming value and |
| 675 | // store that. |
| 676 | PHINode *PN = PHINode::Create( |
| 677 | I->getType(), PredCache.GetNumPreds(BB), |
| 678 | I->getName() + ".lcssa", BB->begin()); |
| 679 | for (BasicBlock **PI = PredCache.GetPreds(BB); *PI; ++PI) |
| 680 | PN->addIncoming(I, *PI); |
| 681 | return PN; |
| 682 | } |
| 683 | return V; |
| 684 | } |
| 685 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 686 | public: |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 687 | LoopPromoter(Value *SP, const SmallVectorImpl<Instruction *> &Insts, |
| 688 | SSAUpdater &S, SmallPtrSet<Value *, 4> &PMA, |
| 689 | SmallVectorImpl<BasicBlock *> &LEB, |
| 690 | SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC, |
| 691 | AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment, |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 692 | MDNode *TBAATag) |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 693 | : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), |
| 694 | LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast), |
| 695 | LI(li), DL(dl), Alignment(alignment), TBAATag(TBAATag) {} |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 696 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 697 | virtual bool isInstInList(Instruction *I, |
| 698 | const SmallVectorImpl<Instruction*> &) const { |
| 699 | Value *Ptr; |
| 700 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 701 | Ptr = LI->getOperand(0); |
| 702 | else |
| 703 | Ptr = cast<StoreInst>(I)->getPointerOperand(); |
| 704 | return PointerMustAliases.count(Ptr); |
| 705 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 706 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 707 | virtual void doExtraRewritesBeforeFinalDeletion() const { |
| 708 | // Insert stores after in the loop exit blocks. Each exit block gets a |
| 709 | // store of the live-out values that feed them. Since we've already told |
| 710 | // the SSA updater about the defs in the loop and the preheader |
| 711 | // definition, it is all set and we can start using it. |
| 712 | for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { |
| 713 | BasicBlock *ExitBlock = LoopExitBlocks[i]; |
| 714 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 715 | LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); |
| 716 | Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 717 | Instruction *InsertPos = LoopInsertPts[i]; |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 718 | StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 719 | NewSI->setAlignment(Alignment); |
Eli Friedman | ddf7f55 | 2011-05-27 20:31:51 +0000 | [diff] [blame] | 720 | NewSI->setDebugLoc(DL); |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 721 | if (TBAATag) NewSI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const { |
| 726 | // Update alias analysis. |
| 727 | AST.copyValue(LI, V); |
| 728 | } |
| 729 | virtual void instructionDeleted(Instruction *I) const { |
| 730 | AST.deleteValue(I); |
| 731 | } |
| 732 | }; |
| 733 | } // end anon namespace |
| 734 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 735 | /// PromoteAliasSet - Try to promote memory values to scalars by sinking |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 736 | /// stores out of the loop and moving loads to before the loop. We do this by |
| 737 | /// looping over the stores in the loop, looking for stores to Must pointers |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 738 | /// which are loop invariant. |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 739 | /// |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 740 | void LICM::PromoteAliasSet(AliasSet &AS, |
| 741 | SmallVectorImpl<BasicBlock*> &ExitBlocks, |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 742 | SmallVectorImpl<Instruction*> &InsertPts, |
| 743 | PredIteratorCache &PIC) { |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 744 | // We can promote this alias set if it has a store, if it is a "Must" alias |
| 745 | // set, if the pointer is loop invariant, and if we are not eliminating any |
| 746 | // volatile loads or stores. |
| 747 | if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || |
| 748 | AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue())) |
| 749 | return; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 751 | assert(!AS.empty() && |
| 752 | "Must alias set should have at least one pointer element in it!"); |
| 753 | Value *SomePtr = AS.begin()->getValue(); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 755 | // It isn't safe to promote a load/store from the loop if the load/store is |
| 756 | // conditional. For example, turning: |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 757 | // |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 758 | // for () { if (c) *P += 1; } |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 759 | // |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 760 | // into: |
| 761 | // |
| 762 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; |
| 763 | // |
| 764 | // is not safe, because *P may only be valid to access if 'c' is true. |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 765 | // |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 766 | // It is safe to promote P if all uses are direct load/stores and if at |
| 767 | // least one is guaranteed to be executed. |
| 768 | bool GuaranteedToExecute = false; |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 770 | SmallVector<Instruction*, 64> LoopUses; |
| 771 | SmallPtrSet<Value*, 4> PointerMustAliases; |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 772 | |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 773 | // We start with an alignment of one and try to find instructions that allow |
| 774 | // us to prove better alignment. |
| 775 | unsigned Alignment = 1; |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 776 | MDNode *TBAATag = 0; |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 778 | // Check that all of the pointers in the alias set have the same type. We |
| 779 | // cannot (yet) promote a memory location that is loaded and stored in |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 780 | // different sizes. While we are at it, collect alignment and TBAA info. |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 781 | for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) { |
| 782 | Value *ASIV = ASI->getValue(); |
| 783 | PointerMustAliases.insert(ASIV); |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 784 | |
Chris Lattner | f12c08d | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 785 | // Check that all of the pointers in the alias set have the same type. We |
| 786 | // cannot (yet) promote a memory location that is loaded and stored in |
| 787 | // different sizes. |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 788 | if (SomePtr->getType() != ASIV->getType()) |
| 789 | return; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 791 | for (Value::use_iterator UI = ASIV->use_begin(), UE = ASIV->use_end(); |
Chris Lattner | c5ec1e1 | 2008-05-22 03:22:42 +0000 | [diff] [blame] | 792 | UI != UE; ++UI) { |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 793 | // Ignore instructions that are outside the loop. |
Chris Lattner | f12c08d | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 794 | Instruction *Use = dyn_cast<Instruction>(*UI); |
Dan Gohman | 18fa568 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 795 | if (!Use || !CurLoop->contains(Use)) |
Chris Lattner | f12c08d | 2008-05-22 00:53:38 +0000 | [diff] [blame] | 796 | continue; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 798 | // If there is an non-load/store instruction in the loop, we can't promote |
| 799 | // it. |
Eli Friedman | 91386c7 | 2011-08-15 20:52:09 +0000 | [diff] [blame] | 800 | if (LoadInst *load = dyn_cast<LoadInst>(Use)) { |
| 801 | assert(!load->isVolatile() && "AST broken"); |
| 802 | if (!load->isSimple()) |
| 803 | return; |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 804 | } else if (StoreInst *store = dyn_cast<StoreInst>(Use)) { |
Chris Lattner | 408a684 | 2010-12-19 05:57:25 +0000 | [diff] [blame] | 805 | // Stores *of* the pointer are not interesting, only stores *to* the |
| 806 | // pointer. |
| 807 | if (Use->getOperand(1) != ASIV) |
| 808 | continue; |
Eli Friedman | 91386c7 | 2011-08-15 20:52:09 +0000 | [diff] [blame] | 809 | assert(!store->isVolatile() && "AST broken"); |
| 810 | if (!store->isSimple()) |
| 811 | return; |
Eli Friedman | 0cdc148 | 2011-07-20 21:37:47 +0000 | [diff] [blame] | 812 | |
| 813 | // Note that we only check GuaranteedToExecute inside the store case |
| 814 | // so that we do not introduce stores where they did not exist before |
| 815 | // (which would break the LLVM concurrency model). |
| 816 | |
| 817 | // If the alignment of this instruction allows us to specify a more |
| 818 | // restrictive (and performant) alignment and if we are sure this |
| 819 | // instruction will be executed, update the alignment. |
| 820 | // Larger is better, with the exception of 0 being the best alignment. |
Eli Friedman | 91386c7 | 2011-08-15 20:52:09 +0000 | [diff] [blame] | 821 | unsigned InstAlignment = store->getAlignment(); |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 822 | if ((InstAlignment > Alignment || InstAlignment == 0) && Alignment != 0) |
Eli Friedman | 0cdc148 | 2011-07-20 21:37:47 +0000 | [diff] [blame] | 823 | if (isGuaranteedToExecute(*Use)) { |
| 824 | GuaranteedToExecute = true; |
| 825 | Alignment = InstAlignment; |
| 826 | } |
| 827 | |
| 828 | if (!GuaranteedToExecute) |
| 829 | GuaranteedToExecute = isGuaranteedToExecute(*Use); |
| 830 | |
Chris Lattner | be90190 | 2010-09-06 05:11:24 +0000 | [diff] [blame] | 831 | } else |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 832 | return; // Not a load or store. |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 833 | |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 834 | // Merge the TBAA tags. |
| 835 | if (LoopUses.empty()) { |
| 836 | // On the first load/store, just take its TBAA tag. |
| 837 | TBAATag = Use->getMetadata(LLVMContext::MD_tbaa); |
Chris Lattner | 473988c | 2013-01-05 16:44:07 +0000 | [diff] [blame] | 838 | } else if (TBAATag) { |
| 839 | TBAATag = MDNode::getMostGenericTBAA(TBAATag, |
| 840 | Use->getMetadata(LLVMContext::MD_tbaa)); |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 841 | } |
Chris Lattner | 473988c | 2013-01-05 16:44:07 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 843 | LoopUses.push_back(Use); |
| 844 | } |
| 845 | } |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 847 | // If there isn't a guaranteed-to-execute instruction, we can't promote. |
| 848 | if (!GuaranteedToExecute) |
| 849 | return; |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 850 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 851 | // Otherwise, this is safe to promote, lets do it! |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 852 | DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " <<*SomePtr<<'\n'); |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 853 | Changed = true; |
| 854 | ++NumPromoted; |
| 855 | |
Eli Friedman | ddf7f55 | 2011-05-27 20:31:51 +0000 | [diff] [blame] | 856 | // Grab a debug location for the inserted loads/stores; given that the |
| 857 | // inserted loads/stores have little relation to the original loads/stores, |
| 858 | // this code just arbitrarily picks a location from one, since any debug |
| 859 | // location is better than none. |
| 860 | DebugLoc DL = LoopUses[0]->getDebugLoc(); |
| 861 | |
Dan Gohman | b948736 | 2012-08-08 00:00:26 +0000 | [diff] [blame] | 862 | // Figure out the loop exits and their insertion points, if this is the |
| 863 | // first promotion. |
| 864 | if (ExitBlocks.empty()) { |
| 865 | CurLoop->getUniqueExitBlocks(ExitBlocks); |
| 866 | InsertPts.resize(ExitBlocks.size()); |
| 867 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 868 | InsertPts[i] = ExitBlocks[i]->getFirstInsertionPt(); |
| 869 | } |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 870 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 871 | // We use the SSAUpdater interface to insert phi nodes as required. |
| 872 | SmallVector<PHINode*, 16> NewPHIs; |
| 873 | SSAUpdater SSA(&NewPHIs); |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 874 | LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, |
Chandler Carruth | fc25854 | 2014-02-11 12:52:27 +0000 | [diff] [blame] | 875 | InsertPts, PIC, *CurAST, *LI, DL, Alignment, TBAATag); |
Tobias Grosser | a3928f5 | 2011-07-06 19:20:02 +0000 | [diff] [blame] | 876 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 877 | // Set up the preheader to have a definition of the value. It is the live-out |
| 878 | // value from the preheader that uses in the loop will use. |
| 879 | LoadInst *PreheaderLoad = |
| 880 | new LoadInst(SomePtr, SomePtr->getName()+".promoted", |
| 881 | Preheader->getTerminator()); |
Tobias Grosser | 4a5d9a9 | 2011-07-06 19:19:55 +0000 | [diff] [blame] | 882 | PreheaderLoad->setAlignment(Alignment); |
Eli Friedman | ddf7f55 | 2011-05-27 20:31:51 +0000 | [diff] [blame] | 883 | PreheaderLoad->setDebugLoc(DL); |
Chris Lattner | f5cca68 | 2012-12-31 08:37:17 +0000 | [diff] [blame] | 884 | if (TBAATag) PreheaderLoad->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 885 | SSA.AddAvailableValue(Preheader, PreheaderLoad); |
| 886 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 887 | // Rewrite all the loads in the loop and remember all the definitions from |
| 888 | // stores in the loop. |
| 889 | Promoter.run(LoopUses); |
Eli Friedman | c5f22a7 | 2011-04-07 01:35:06 +0000 | [diff] [blame] | 890 | |
| 891 | // If the SSAUpdater didn't use the load in the preheader, just zap it now. |
| 892 | if (PreheaderLoad->use_empty()) |
| 893 | PreheaderLoad->eraseFromParent(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 894 | } |
Devang Patel | b98a097 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 1dc98b4 | 2010-08-29 06:43:52 +0000 | [diff] [blame] | 896 | |
Devang Patel | b98a097 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 897 | /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. |
| 898 | void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) { |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 899 | AliasSetTracker *AST = LoopToAliasSetMap.lookup(L); |
Devang Patel | b98a097 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 900 | if (!AST) |
| 901 | return; |
| 902 | |
| 903 | AST->copyValue(From, To); |
| 904 | } |
| 905 | |
| 906 | /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias |
| 907 | /// set. |
| 908 | void LICM::deleteAnalysisValue(Value *V, Loop *L) { |
Chris Lattner | cc9cbc6 | 2010-08-29 17:46:00 +0000 | [diff] [blame] | 909 | AliasSetTracker *AST = LoopToAliasSetMap.lookup(L); |
Devang Patel | b98a097 | 2007-07-31 08:01:41 +0000 | [diff] [blame] | 910 | if (!AST) |
| 911 | return; |
| 912 | |
| 913 | AST->deleteValue(V); |
| 914 | } |