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