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