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