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