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