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