| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 1 | //===- LoopLoadElimination.cpp - Loop Load Elimination Pass ---------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file implement a loop-aware load elimination pass. | 
|  | 11 | // | 
|  | 12 | // It uses LoopAccessAnalysis to identify loop-carried dependences with a | 
|  | 13 | // distance of one between stores and loads.  These form the candidates for the | 
|  | 14 | // transformation.  The source value of each store then propagated to the user | 
|  | 15 | // of the corresponding load.  This makes the load dead. | 
|  | 16 | // | 
|  | 17 | // The pass can also version the loop and add memchecks in order to prove that | 
|  | 18 | // may-aliasing stores can't change the value in memory before it's read by the | 
|  | 19 | // load. | 
|  | 20 | // | 
|  | 21 | //===----------------------------------------------------------------------===// | 
|  | 22 |  | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar/LoopLoadElimination.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" | 
|  | 25 | #include "llvm/ADT/DenseMap.h" | 
|  | 26 | #include "llvm/ADT/DepthFirstIterator.h" | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Statistic.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/AliasAnalysis.h" | 
|  | 32 | #include "llvm/Analysis/AssumptionCache.h" | 
| Eli Friedman | 02d48be | 2016-09-16 17:58:07 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/GlobalsModRef.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/LoopAccessAnalysis.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/LoopAnalysisManager.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/LoopInfo.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/ScalarEvolution.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ScalarEvolutionExpander.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/TargetLibraryInfo.h" | 
|  | 41 | #include "llvm/Analysis/TargetTransformInfo.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 42 | #include "llvm/IR/DataLayout.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Dominators.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Instructions.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Module.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 46 | #include "llvm/IR/PassManager.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 47 | #include "llvm/IR/Type.h" | 
|  | 48 | #include "llvm/IR/Value.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 49 | #include "llvm/Pass.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Casting.h" | 
|  | 51 | #include "llvm/Support/CommandLine.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 53 | #include "llvm/Support/raw_ostream.h" | 
| Adam Nemet | efb2341 | 2016-03-10 23:54:39 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Scalar.h" | 
| David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Utils.h" | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/Utils/LoopVersioning.h" | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 57 | #include <algorithm> | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 58 | #include <cassert> | 
|  | 59 | #include <forward_list> | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 60 | #include <set> | 
|  | 61 | #include <tuple> | 
|  | 62 | #include <utility> | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 63 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 64 | using namespace llvm; | 
|  | 65 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 66 | #define LLE_OPTION "loop-load-elim" | 
|  | 67 | #define DEBUG_TYPE LLE_OPTION | 
|  | 68 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 69 | static cl::opt<unsigned> CheckPerElim( | 
|  | 70 | "runtime-check-per-loop-load-elim", cl::Hidden, | 
|  | 71 | cl::desc("Max number of memchecks allowed per eliminated load on average"), | 
|  | 72 | cl::init(1)); | 
|  | 73 |  | 
| Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 74 | static cl::opt<unsigned> LoadElimSCEVCheckThreshold( | 
|  | 75 | "loop-load-elimination-scev-check-threshold", cl::init(8), cl::Hidden, | 
|  | 76 | cl::desc("The maximum number of SCEV checks allowed for Loop " | 
|  | 77 | "Load Elimination")); | 
|  | 78 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 79 | STATISTIC(NumLoopLoadEliminted, "Number of loads eliminated by LLE"); | 
|  | 80 |  | 
|  | 81 | namespace { | 
|  | 82 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 83 | /// Represent a store-to-forwarding candidate. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 84 | struct StoreToLoadForwardingCandidate { | 
|  | 85 | LoadInst *Load; | 
|  | 86 | StoreInst *Store; | 
|  | 87 |  | 
|  | 88 | StoreToLoadForwardingCandidate(LoadInst *Load, StoreInst *Store) | 
|  | 89 | : Load(Load), Store(Store) {} | 
|  | 90 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 91 | /// Return true if the dependence from the store to the load has a | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 92 | /// distance of one.  E.g. A[i+1] = A[i] | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 93 | bool isDependenceDistanceOfOne(PredicatedScalarEvolution &PSE, | 
|  | 94 | Loop *L) const { | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 95 | Value *LoadPtr = Load->getPointerOperand(); | 
|  | 96 | Value *StorePtr = Store->getPointerOperand(); | 
|  | 97 | Type *LoadPtrType = LoadPtr->getType(); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 98 | Type *LoadType = LoadPtrType->getPointerElementType(); | 
|  | 99 |  | 
|  | 100 | assert(LoadPtrType->getPointerAddressSpace() == | 
| Adam Nemet | 7c94c9b | 2015-11-04 00:10:33 +0000 | [diff] [blame] | 101 | StorePtr->getType()->getPointerAddressSpace() && | 
|  | 102 | LoadType == StorePtr->getType()->getPointerElementType() && | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 103 | "Should be a known dependence"); | 
|  | 104 |  | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 105 | // Currently we only support accesses with unit stride.  FIXME: we should be | 
|  | 106 | // able to handle non unit stirde as well as long as the stride is equal to | 
|  | 107 | // the dependence distance. | 
| Denis Zobnin | 15d1e64 | 2016-05-10 05:55:16 +0000 | [diff] [blame] | 108 | if (getPtrStride(PSE, LoadPtr, L) != 1 || | 
|  | 109 | getPtrStride(PSE, StorePtr, L) != 1) | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 110 | return false; | 
|  | 111 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 112 | auto &DL = Load->getParent()->getModule()->getDataLayout(); | 
|  | 113 | unsigned TypeByteSize = DL.getTypeAllocSize(const_cast<Type *>(LoadType)); | 
|  | 114 |  | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 115 | auto *LoadPtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(LoadPtr)); | 
|  | 116 | auto *StorePtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(StorePtr)); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 117 |  | 
|  | 118 | // We don't need to check non-wrapping here because forward/backward | 
|  | 119 | // dependence wouldn't be valid if these weren't monotonic accesses. | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 120 | auto *Dist = cast<SCEVConstant>( | 
|  | 121 | PSE.getSE()->getMinusSCEV(StorePtrSCEV, LoadPtrSCEV)); | 
| Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 122 | const APInt &Val = Dist->getAPInt(); | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 123 | return Val == TypeByteSize; | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
|  | 126 | Value *getLoadPtr() const { return Load->getPointerOperand(); } | 
|  | 127 |  | 
|  | 128 | #ifndef NDEBUG | 
|  | 129 | friend raw_ostream &operator<<(raw_ostream &OS, | 
|  | 130 | const StoreToLoadForwardingCandidate &Cand) { | 
|  | 131 | OS << *Cand.Store << " -->\n"; | 
|  | 132 | OS.indent(2) << *Cand.Load << "\n"; | 
|  | 133 | return OS; | 
|  | 134 | } | 
|  | 135 | #endif | 
|  | 136 | }; | 
|  | 137 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 138 | } // end anonymous namespace | 
|  | 139 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 140 | /// Check if the store dominates all latches, so as long as there is no | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 141 | /// intervening store this value will be loaded in the next iteration. | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 142 | static bool doesStoreDominatesAllLatches(BasicBlock *StoreBlock, Loop *L, | 
|  | 143 | DominatorTree *DT) { | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 144 | SmallVector<BasicBlock *, 8> Latches; | 
|  | 145 | L->getLoopLatches(Latches); | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 146 | return llvm::all_of(Latches, [&](const BasicBlock *Latch) { | 
| David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 147 | return DT->dominates(StoreBlock, Latch); | 
|  | 148 | }); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 151 | /// Return true if the load is not executed on all paths in the loop. | 
| Adam Nemet | bd861ac | 2016-06-28 04:02:47 +0000 | [diff] [blame] | 152 | static bool isLoadConditional(LoadInst *Load, Loop *L) { | 
|  | 153 | return Load->getParent() != L->getHeader(); | 
|  | 154 | } | 
|  | 155 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 156 | namespace { | 
|  | 157 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 158 | /// The per-loop class that does most of the work. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 159 | class LoadEliminationForLoop { | 
|  | 160 | public: | 
|  | 161 | LoadEliminationForLoop(Loop *L, LoopInfo *LI, const LoopAccessInfo &LAI, | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 162 | DominatorTree *DT) | 
| Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 163 | : L(L), LI(LI), LAI(LAI), DT(DT), PSE(LAI.getPSE()) {} | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 164 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 165 | /// Look through the loop-carried and loop-independent dependences in | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 166 | /// this loop and find store->load dependences. | 
|  | 167 | /// | 
|  | 168 | /// Note that no candidate is returned if LAA has failed to analyze the loop | 
|  | 169 | /// (e.g. if it's not bottom-tested, contains volatile memops, etc.) | 
|  | 170 | std::forward_list<StoreToLoadForwardingCandidate> | 
|  | 171 | findStoreToLoadDependences(const LoopAccessInfo &LAI) { | 
|  | 172 | std::forward_list<StoreToLoadForwardingCandidate> Candidates; | 
|  | 173 |  | 
|  | 174 | const auto *Deps = LAI.getDepChecker().getDependences(); | 
|  | 175 | if (!Deps) | 
|  | 176 | return Candidates; | 
|  | 177 |  | 
|  | 178 | // Find store->load dependences (consequently true dep).  Both lexically | 
|  | 179 | // forward and backward dependences qualify.  Disqualify loads that have | 
|  | 180 | // other unknown dependences. | 
|  | 181 |  | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 182 | SmallPtrSet<Instruction *, 4> LoadsWithUnknownDepedence; | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 183 |  | 
|  | 184 | for (const auto &Dep : *Deps) { | 
|  | 185 | Instruction *Source = Dep.getSource(LAI); | 
|  | 186 | Instruction *Destination = Dep.getDestination(LAI); | 
|  | 187 |  | 
|  | 188 | if (Dep.Type == MemoryDepChecker::Dependence::Unknown) { | 
|  | 189 | if (isa<LoadInst>(Source)) | 
|  | 190 | LoadsWithUnknownDepedence.insert(Source); | 
|  | 191 | if (isa<LoadInst>(Destination)) | 
|  | 192 | LoadsWithUnknownDepedence.insert(Destination); | 
|  | 193 | continue; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | if (Dep.isBackward()) | 
|  | 197 | // Note that the designations source and destination follow the program | 
|  | 198 | // order, i.e. source is always first.  (The direction is given by the | 
|  | 199 | // DepType.) | 
|  | 200 | std::swap(Source, Destination); | 
|  | 201 | else | 
|  | 202 | assert(Dep.isForward() && "Needs to be a forward dependence"); | 
|  | 203 |  | 
|  | 204 | auto *Store = dyn_cast<StoreInst>(Source); | 
|  | 205 | if (!Store) | 
|  | 206 | continue; | 
|  | 207 | auto *Load = dyn_cast<LoadInst>(Destination); | 
|  | 208 | if (!Load) | 
|  | 209 | continue; | 
| Adam Nemet | 7aba60c | 2016-03-24 17:59:26 +0000 | [diff] [blame] | 210 |  | 
|  | 211 | // Only progagate the value if they are of the same type. | 
| Sanjoy Das | f09c1e3 | 2017-04-18 22:00:54 +0000 | [diff] [blame] | 212 | if (Store->getPointerOperandType() != Load->getPointerOperandType()) | 
| Adam Nemet | 7aba60c | 2016-03-24 17:59:26 +0000 | [diff] [blame] | 213 | continue; | 
|  | 214 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 215 | Candidates.emplace_front(Load, Store); | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | if (!LoadsWithUnknownDepedence.empty()) | 
|  | 219 | Candidates.remove_if([&](const StoreToLoadForwardingCandidate &C) { | 
|  | 220 | return LoadsWithUnknownDepedence.count(C.Load); | 
|  | 221 | }); | 
|  | 222 |  | 
|  | 223 | return Candidates; | 
|  | 224 | } | 
|  | 225 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 226 | /// Return the index of the instruction according to program order. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 227 | unsigned getInstrIndex(Instruction *Inst) { | 
|  | 228 | auto I = InstOrder.find(Inst); | 
|  | 229 | assert(I != InstOrder.end() && "No index for instruction"); | 
|  | 230 | return I->second; | 
|  | 231 | } | 
|  | 232 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 233 | /// If a load has multiple candidates associated (i.e. different | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 234 | /// stores), it means that it could be forwarding from multiple stores | 
|  | 235 | /// depending on control flow.  Remove these candidates. | 
|  | 236 | /// | 
|  | 237 | /// Here, we rely on LAA to include the relevant loop-independent dependences. | 
|  | 238 | /// LAA is known to omit these in the very simple case when the read and the | 
|  | 239 | /// write within an alias set always takes place using the *same* pointer. | 
|  | 240 | /// | 
|  | 241 | /// However, we know that this is not the case here, i.e. we can rely on LAA | 
|  | 242 | /// to provide us with loop-independent dependences for the cases we're | 
|  | 243 | /// interested.  Consider the case for example where a loop-independent | 
|  | 244 | /// dependece S1->S2 invalidates the forwarding S3->S2. | 
|  | 245 | /// | 
|  | 246 | ///         A[i]   = ...   (S1) | 
|  | 247 | ///         ...    = A[i]  (S2) | 
|  | 248 | ///         A[i+1] = ...   (S3) | 
|  | 249 | /// | 
|  | 250 | /// LAA will perform dependence analysis here because there are two | 
|  | 251 | /// *different* pointers involved in the same alias set (&A[i] and &A[i+1]). | 
|  | 252 | void removeDependencesFromMultipleStores( | 
|  | 253 | std::forward_list<StoreToLoadForwardingCandidate> &Candidates) { | 
|  | 254 | // If Store is nullptr it means that we have multiple stores forwarding to | 
|  | 255 | // this store. | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 256 | using LoadToSingleCandT = | 
|  | 257 | DenseMap<LoadInst *, const StoreToLoadForwardingCandidate *>; | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 258 | LoadToSingleCandT LoadToSingleCand; | 
|  | 259 |  | 
|  | 260 | for (const auto &Cand : Candidates) { | 
|  | 261 | bool NewElt; | 
|  | 262 | LoadToSingleCandT::iterator Iter; | 
|  | 263 |  | 
|  | 264 | std::tie(Iter, NewElt) = | 
|  | 265 | LoadToSingleCand.insert(std::make_pair(Cand.Load, &Cand)); | 
|  | 266 | if (!NewElt) { | 
|  | 267 | const StoreToLoadForwardingCandidate *&OtherCand = Iter->second; | 
|  | 268 | // Already multiple stores forward to this load. | 
|  | 269 | if (OtherCand == nullptr) | 
|  | 270 | continue; | 
|  | 271 |  | 
| Adam Nemet | efc091f | 2016-02-29 23:21:12 +0000 | [diff] [blame] | 272 | // Handle the very basic case when the two stores are in the same block | 
|  | 273 | // so deciding which one forwards is easy.  The later one forwards as | 
|  | 274 | // long as they both have a dependence distance of one to the load. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 275 | if (Cand.Store->getParent() == OtherCand->Store->getParent() && | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 276 | Cand.isDependenceDistanceOfOne(PSE, L) && | 
|  | 277 | OtherCand->isDependenceDistanceOfOne(PSE, L)) { | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 278 | // They are in the same block, the later one will forward to the load. | 
|  | 279 | if (getInstrIndex(OtherCand->Store) < getInstrIndex(Cand.Store)) | 
|  | 280 | OtherCand = &Cand; | 
|  | 281 | } else | 
|  | 282 | OtherCand = nullptr; | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | Candidates.remove_if([&](const StoreToLoadForwardingCandidate &Cand) { | 
|  | 287 | if (LoadToSingleCand[Cand.Load] != &Cand) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 288 | LLVM_DEBUG( | 
|  | 289 | dbgs() << "Removing from candidates: \n" | 
|  | 290 | << Cand | 
|  | 291 | << "  The load may have multiple stores forwarding to " | 
|  | 292 | << "it\n"); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 293 | return true; | 
|  | 294 | } | 
|  | 295 | return false; | 
|  | 296 | }); | 
|  | 297 | } | 
|  | 298 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 299 | /// Given two pointers operations by their RuntimePointerChecking | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 300 | /// indices, return true if they require an alias check. | 
|  | 301 | /// | 
|  | 302 | /// We need a check if one is a pointer for a candidate load and the other is | 
|  | 303 | /// a pointer for a possibly intervening store. | 
|  | 304 | bool needsChecking(unsigned PtrIdx1, unsigned PtrIdx2, | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 305 | const SmallPtrSet<Value *, 4> &PtrsWrittenOnFwdingPath, | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 306 | const std::set<Value *> &CandLoadPtrs) { | 
|  | 307 | Value *Ptr1 = | 
|  | 308 | LAI.getRuntimePointerChecking()->getPointerInfo(PtrIdx1).PointerValue; | 
|  | 309 | Value *Ptr2 = | 
|  | 310 | LAI.getRuntimePointerChecking()->getPointerInfo(PtrIdx2).PointerValue; | 
|  | 311 | return ((PtrsWrittenOnFwdingPath.count(Ptr1) && CandLoadPtrs.count(Ptr2)) || | 
|  | 312 | (PtrsWrittenOnFwdingPath.count(Ptr2) && CandLoadPtrs.count(Ptr1))); | 
|  | 313 | } | 
|  | 314 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 315 | /// Return pointers that are possibly written to on the path from a | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 316 | /// forwarding store to a load. | 
|  | 317 | /// | 
|  | 318 | /// These pointers need to be alias-checked against the forwarding candidates. | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 319 | SmallPtrSet<Value *, 4> findPointersWrittenOnForwardingPath( | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 320 | const SmallVectorImpl<StoreToLoadForwardingCandidate> &Candidates) { | 
|  | 321 | // From FirstStore to LastLoad neither of the elimination candidate loads | 
|  | 322 | // should overlap with any of the stores. | 
|  | 323 | // | 
|  | 324 | // E.g.: | 
|  | 325 | // | 
|  | 326 | // st1 C[i] | 
|  | 327 | // ld1 B[i] <-------, | 
|  | 328 | // ld0 A[i] <----,  |              * LastLoad | 
|  | 329 | // ...           |  | | 
|  | 330 | // st2 E[i]      |  | | 
|  | 331 | // st3 B[i+1] -- | -'              * FirstStore | 
|  | 332 | // st0 A[i+1] ---' | 
|  | 333 | // st4 D[i] | 
|  | 334 | // | 
|  | 335 | // st0 forwards to ld0 if the accesses in st4 and st1 don't overlap with | 
|  | 336 | // ld0. | 
|  | 337 |  | 
|  | 338 | LoadInst *LastLoad = | 
|  | 339 | std::max_element(Candidates.begin(), Candidates.end(), | 
|  | 340 | [&](const StoreToLoadForwardingCandidate &A, | 
|  | 341 | const StoreToLoadForwardingCandidate &B) { | 
|  | 342 | return getInstrIndex(A.Load) < getInstrIndex(B.Load); | 
|  | 343 | }) | 
|  | 344 | ->Load; | 
|  | 345 | StoreInst *FirstStore = | 
|  | 346 | std::min_element(Candidates.begin(), Candidates.end(), | 
|  | 347 | [&](const StoreToLoadForwardingCandidate &A, | 
|  | 348 | const StoreToLoadForwardingCandidate &B) { | 
|  | 349 | return getInstrIndex(A.Store) < | 
|  | 350 | getInstrIndex(B.Store); | 
|  | 351 | }) | 
|  | 352 | ->Store; | 
|  | 353 |  | 
|  | 354 | // We're looking for stores after the first forwarding store until the end | 
|  | 355 | // of the loop, then from the beginning of the loop until the last | 
|  | 356 | // forwarded-to load.  Collect the pointer for the stores. | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 357 | SmallPtrSet<Value *, 4> PtrsWrittenOnFwdingPath; | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 358 |  | 
|  | 359 | auto InsertStorePtr = [&](Instruction *I) { | 
|  | 360 | if (auto *S = dyn_cast<StoreInst>(I)) | 
|  | 361 | PtrsWrittenOnFwdingPath.insert(S->getPointerOperand()); | 
|  | 362 | }; | 
|  | 363 | const auto &MemInstrs = LAI.getDepChecker().getMemoryInstructions(); | 
|  | 364 | std::for_each(MemInstrs.begin() + getInstrIndex(FirstStore) + 1, | 
|  | 365 | MemInstrs.end(), InsertStorePtr); | 
|  | 366 | std::for_each(MemInstrs.begin(), &MemInstrs[getInstrIndex(LastLoad)], | 
|  | 367 | InsertStorePtr); | 
|  | 368 |  | 
|  | 369 | return PtrsWrittenOnFwdingPath; | 
|  | 370 | } | 
|  | 371 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 372 | /// Determine the pointer alias checks to prove that there are no | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 373 | /// intervening stores. | 
|  | 374 | SmallVector<RuntimePointerChecking::PointerCheck, 4> collectMemchecks( | 
|  | 375 | const SmallVectorImpl<StoreToLoadForwardingCandidate> &Candidates) { | 
|  | 376 |  | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 377 | SmallPtrSet<Value *, 4> PtrsWrittenOnFwdingPath = | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 378 | findPointersWrittenOnForwardingPath(Candidates); | 
|  | 379 |  | 
|  | 380 | // Collect the pointers of the candidate loads. | 
| Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 381 | // FIXME: SmallPtrSet does not work with std::inserter. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 382 | std::set<Value *> CandLoadPtrs; | 
| David Majnemer | 2d006e7 | 2016-08-12 04:32:42 +0000 | [diff] [blame] | 383 | transform(Candidates, | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 384 | std::inserter(CandLoadPtrs, CandLoadPtrs.begin()), | 
|  | 385 | std::mem_fn(&StoreToLoadForwardingCandidate::getLoadPtr)); | 
|  | 386 |  | 
|  | 387 | const auto &AllChecks = LAI.getRuntimePointerChecking()->getChecks(); | 
|  | 388 | SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks; | 
|  | 389 |  | 
| Sanjoy Das | 9020872 | 2017-02-21 00:38:44 +0000 | [diff] [blame] | 390 | copy_if(AllChecks, std::back_inserter(Checks), | 
|  | 391 | [&](const RuntimePointerChecking::PointerCheck &Check) { | 
|  | 392 | for (auto PtrIdx1 : Check.first->Members) | 
|  | 393 | for (auto PtrIdx2 : Check.second->Members) | 
|  | 394 | if (needsChecking(PtrIdx1, PtrIdx2, PtrsWrittenOnFwdingPath, | 
|  | 395 | CandLoadPtrs)) | 
|  | 396 | return true; | 
|  | 397 | return false; | 
|  | 398 | }); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 399 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 400 | LLVM_DEBUG(dbgs() << "\nPointer Checks (count: " << Checks.size() | 
|  | 401 | << "):\n"); | 
|  | 402 | LLVM_DEBUG(LAI.getRuntimePointerChecking()->printChecks(dbgs(), Checks)); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 403 |  | 
|  | 404 | return Checks; | 
|  | 405 | } | 
|  | 406 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 407 | /// Perform the transformation for a candidate. | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 408 | void | 
|  | 409 | propagateStoredValueToLoadUsers(const StoreToLoadForwardingCandidate &Cand, | 
|  | 410 | SCEVExpander &SEE) { | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 411 | // loop: | 
|  | 412 | //      %x = load %gep_i | 
|  | 413 | //         = ... %x | 
|  | 414 | //      store %y, %gep_i_plus_1 | 
|  | 415 | // | 
|  | 416 | // => | 
|  | 417 | // | 
|  | 418 | // ph: | 
|  | 419 | //      %x.initial = load %gep_0 | 
|  | 420 | // loop: | 
|  | 421 | //      %x.storeforward = phi [%x.initial, %ph] [%y, %loop] | 
|  | 422 | //      %x = load %gep_i            <---- now dead | 
|  | 423 | //         = ... %x.storeforward | 
|  | 424 | //      store %y, %gep_i_plus_1 | 
|  | 425 |  | 
|  | 426 | Value *Ptr = Cand.Load->getPointerOperand(); | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 427 | auto *PtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(Ptr)); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 428 | auto *PH = L->getLoopPreheader(); | 
|  | 429 | Value *InitialPtr = SEE.expandCodeFor(PtrSCEV->getStart(), Ptr->getType(), | 
|  | 430 | PH->getTerminator()); | 
|  | 431 | Value *Initial = | 
| Mehdi Amini | 27d224f | 2017-01-06 21:06:51 +0000 | [diff] [blame] | 432 | new LoadInst(InitialPtr, "load_initial", /* isVolatile */ false, | 
|  | 433 | Cand.Load->getAlignment(), PH->getTerminator()); | 
|  | 434 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 435 | PHINode *PHI = PHINode::Create(Initial->getType(), 2, "store_forwarded", | 
| Duncan P. N. Exon Smith | 83c4b68 | 2015-11-07 00:01:16 +0000 | [diff] [blame] | 436 | &L->getHeader()->front()); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 437 | PHI->addIncoming(Initial, PH); | 
|  | 438 | PHI->addIncoming(Cand.Store->getOperand(0), L->getLoopLatch()); | 
|  | 439 |  | 
|  | 440 | Cand.Load->replaceAllUsesWith(PHI); | 
|  | 441 | } | 
|  | 442 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 443 | /// Top-level driver for each loop: find store->load forwarding | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 444 | /// candidates, add run-time checks and perform transformation. | 
|  | 445 | bool processLoop() { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 446 | LLVM_DEBUG(dbgs() << "\nIn \"" << L->getHeader()->getParent()->getName() | 
|  | 447 | << "\" checking " << *L << "\n"); | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 448 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 449 | // Look for store-to-load forwarding cases across the | 
|  | 450 | // backedge. E.g.: | 
|  | 451 | // | 
|  | 452 | // loop: | 
|  | 453 | //      %x = load %gep_i | 
|  | 454 | //         = ... %x | 
|  | 455 | //      store %y, %gep_i_plus_1 | 
|  | 456 | // | 
|  | 457 | // => | 
|  | 458 | // | 
|  | 459 | // ph: | 
|  | 460 | //      %x.initial = load %gep_0 | 
|  | 461 | // loop: | 
|  | 462 | //      %x.storeforward = phi [%x.initial, %ph] [%y, %loop] | 
|  | 463 | //      %x = load %gep_i            <---- now dead | 
|  | 464 | //         = ... %x.storeforward | 
|  | 465 | //      store %y, %gep_i_plus_1 | 
|  | 466 |  | 
|  | 467 | // First start with store->load dependences. | 
|  | 468 | auto StoreToLoadDependences = findStoreToLoadDependences(LAI); | 
|  | 469 | if (StoreToLoadDependences.empty()) | 
|  | 470 | return false; | 
|  | 471 |  | 
|  | 472 | // Generate an index for each load and store according to the original | 
|  | 473 | // program order.  This will be used later. | 
|  | 474 | InstOrder = LAI.getDepChecker().generateInstructionOrderMap(); | 
|  | 475 |  | 
|  | 476 | // To keep things simple for now, remove those where the load is potentially | 
|  | 477 | // fed by multiple stores. | 
|  | 478 | removeDependencesFromMultipleStores(StoreToLoadDependences); | 
|  | 479 | if (StoreToLoadDependences.empty()) | 
|  | 480 | return false; | 
|  | 481 |  | 
|  | 482 | // Filter the candidates further. | 
|  | 483 | SmallVector<StoreToLoadForwardingCandidate, 4> Candidates; | 
|  | 484 | unsigned NumForwarding = 0; | 
|  | 485 | for (const StoreToLoadForwardingCandidate Cand : StoreToLoadDependences) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 486 | LLVM_DEBUG(dbgs() << "Candidate " << Cand); | 
| Adam Nemet | 83be06e | 2016-02-29 22:53:59 +0000 | [diff] [blame] | 487 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 488 | // Make sure that the stored values is available everywhere in the loop in | 
|  | 489 | // the next iteration. | 
|  | 490 | if (!doesStoreDominatesAllLatches(Cand.Store->getParent(), L, DT)) | 
|  | 491 | continue; | 
|  | 492 |  | 
| Adam Nemet | bd861ac | 2016-06-28 04:02:47 +0000 | [diff] [blame] | 493 | // If the load is conditional we can't hoist its 0-iteration instance to | 
|  | 494 | // the preheader because that would make it unconditional.  Thus we would | 
|  | 495 | // access a memory location that the original loop did not access. | 
|  | 496 | if (isLoadConditional(Cand.Load, L)) | 
|  | 497 | continue; | 
|  | 498 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 499 | // Check whether the SCEV difference is the same as the induction step, | 
|  | 500 | // thus we load the value in the next iteration. | 
| Adam Nemet | 660748c | 2016-03-09 20:47:55 +0000 | [diff] [blame] | 501 | if (!Cand.isDependenceDistanceOfOne(PSE, L)) | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 502 | continue; | 
|  | 503 |  | 
|  | 504 | ++NumForwarding; | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 505 | LLVM_DEBUG( | 
|  | 506 | dbgs() | 
|  | 507 | << NumForwarding | 
|  | 508 | << ". Valid store-to-load forwarding across the loop backedge\n"); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 509 | Candidates.push_back(Cand); | 
|  | 510 | } | 
|  | 511 | if (Candidates.empty()) | 
|  | 512 | return false; | 
|  | 513 |  | 
|  | 514 | // Check intervening may-alias stores.  These need runtime checks for alias | 
|  | 515 | // disambiguation. | 
|  | 516 | SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks = | 
|  | 517 | collectMemchecks(Candidates); | 
|  | 518 |  | 
|  | 519 | // Too many checks are likely to outweigh the benefits of forwarding. | 
|  | 520 | if (Checks.size() > Candidates.size() * CheckPerElim) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 521 | LLVM_DEBUG(dbgs() << "Too many run-time checks needed.\n"); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 522 | return false; | 
|  | 523 | } | 
|  | 524 |  | 
| Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 525 | if (LAI.getPSE().getUnionPredicate().getComplexity() > | 
| Silviu Baranga | 9cd9a7e | 2015-12-09 16:06:28 +0000 | [diff] [blame] | 526 | LoadElimSCEVCheckThreshold) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 527 | LLVM_DEBUG(dbgs() << "Too many SCEV run-time checks needed.\n"); | 
| Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 528 | return false; | 
|  | 529 | } | 
|  | 530 |  | 
| Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 531 | if (!Checks.empty() || !LAI.getPSE().getUnionPredicate().isAlwaysTrue()) { | 
| Adam Nemet | 9455c1d | 2016-02-05 01:14:05 +0000 | [diff] [blame] | 532 | if (L->getHeader()->getParent()->optForSize()) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 533 | LLVM_DEBUG( | 
|  | 534 | dbgs() << "Versioning is needed but not allowed when optimizing " | 
|  | 535 | "for size.\n"); | 
| Adam Nemet | 9455c1d | 2016-02-05 01:14:05 +0000 | [diff] [blame] | 536 | return false; | 
|  | 537 | } | 
|  | 538 |  | 
| Florian Hahn | 2e03213 | 2016-12-19 17:13:37 +0000 | [diff] [blame] | 539 | if (!L->isLoopSimplifyForm()) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 540 | LLVM_DEBUG(dbgs() << "Loop is not is loop-simplify form"); | 
| Florian Hahn | 2e03213 | 2016-12-19 17:13:37 +0000 | [diff] [blame] | 541 | return false; | 
|  | 542 | } | 
|  | 543 |  | 
| Adam Nemet | 9455c1d | 2016-02-05 01:14:05 +0000 | [diff] [blame] | 544 | // Point of no-return, start the transformation.  First, version the loop | 
|  | 545 | // if necessary. | 
|  | 546 |  | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 547 | LoopVersioning LV(LAI, L, LI, DT, PSE.getSE(), false); | 
| Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 548 | LV.setAliasChecks(std::move(Checks)); | 
| Xinliang David Li | 94734ee | 2016-07-01 05:59:55 +0000 | [diff] [blame] | 549 | LV.setSCEVChecks(LAI.getPSE().getUnionPredicate()); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 550 | LV.versionLoop(); | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 | // Next, propagate the value stored by the store to the users of the load. | 
|  | 554 | // Also for the first iteration, generate the initial value of the load. | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 555 | SCEVExpander SEE(*PSE.getSE(), L->getHeader()->getModule()->getDataLayout(), | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 556 | "storeforward"); | 
|  | 557 | for (const auto &Cand : Candidates) | 
|  | 558 | propagateStoredValueToLoadUsers(Cand, SEE); | 
|  | 559 | NumLoopLoadEliminted += NumForwarding; | 
|  | 560 |  | 
|  | 561 | return true; | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | private: | 
|  | 565 | Loop *L; | 
|  | 566 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 567 | /// Maps the load/store instructions to their index according to | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 568 | /// program order. | 
|  | 569 | DenseMap<Instruction *, unsigned> InstOrder; | 
|  | 570 |  | 
|  | 571 | // Analyses used. | 
|  | 572 | LoopInfo *LI; | 
|  | 573 | const LoopAccessInfo &LAI; | 
|  | 574 | DominatorTree *DT; | 
| Silviu Baranga | 86de80d | 2015-12-10 11:07:18 +0000 | [diff] [blame] | 575 | PredicatedScalarEvolution PSE; | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 576 | }; | 
|  | 577 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 578 | } // end anonymous namespace | 
|  | 579 |  | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 580 | static bool | 
|  | 581 | eliminateLoadsAcrossLoops(Function &F, LoopInfo &LI, DominatorTree &DT, | 
|  | 582 | function_ref<const LoopAccessInfo &(Loop &)> GetLAI) { | 
|  | 583 | // Build up a worklist of inner-loops to transform to avoid iterator | 
|  | 584 | // invalidation. | 
|  | 585 | // FIXME: This logic comes from other passes that actually change the loop | 
|  | 586 | // nest structure. It isn't clear this is necessary (or useful) for a pass | 
|  | 587 | // which merely optimizes the use of loads in a loop. | 
|  | 588 | SmallVector<Loop *, 8> Worklist; | 
|  | 589 |  | 
|  | 590 | for (Loop *TopLevelLoop : LI) | 
|  | 591 | for (Loop *L : depth_first(TopLevelLoop)) | 
|  | 592 | // We only handle inner-most loops. | 
|  | 593 | if (L->empty()) | 
|  | 594 | Worklist.push_back(L); | 
|  | 595 |  | 
|  | 596 | // Now walk the identified inner loops. | 
|  | 597 | bool Changed = false; | 
|  | 598 | for (Loop *L : Worklist) { | 
|  | 599 | // The actual work is performed by LoadEliminationForLoop. | 
|  | 600 | LoadEliminationForLoop LEL(L, &LI, GetLAI(*L), &DT); | 
|  | 601 | Changed |= LEL.processLoop(); | 
|  | 602 | } | 
|  | 603 | return Changed; | 
|  | 604 | } | 
|  | 605 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 606 | namespace { | 
|  | 607 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 608 | /// The pass.  Most of the work is delegated to the per-loop | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 609 | /// LoadEliminationForLoop class. | 
|  | 610 | class LoopLoadElimination : public FunctionPass { | 
|  | 611 | public: | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 612 | static char ID; | 
|  | 613 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 614 | LoopLoadElimination() : FunctionPass(ID) { | 
|  | 615 | initializeLoopLoadEliminationPass(*PassRegistry::getPassRegistry()); | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | bool runOnFunction(Function &F) override { | 
| Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 619 | if (skipFunction(F)) | 
|  | 620 | return false; | 
|  | 621 |  | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 622 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); | 
|  | 623 | auto &LAA = getAnalysis<LoopAccessLegacyAnalysis>(); | 
|  | 624 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 625 |  | 
|  | 626 | // Process each loop nest in the function. | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 627 | return eliminateLoadsAcrossLoops( | 
|  | 628 | F, LI, DT, | 
|  | 629 | [&LAA](Loop &L) -> const LoopAccessInfo & { return LAA.getInfo(&L); }); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 630 | } | 
|  | 631 |  | 
|  | 632 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
| Adam Nemet | efb2341 | 2016-03-10 23:54:39 +0000 | [diff] [blame] | 633 | AU.addRequiredID(LoopSimplifyID); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 634 | AU.addRequired<LoopInfoWrapperPass>(); | 
|  | 635 | AU.addPreserved<LoopInfoWrapperPass>(); | 
| Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 636 | AU.addRequired<LoopAccessLegacyAnalysis>(); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 637 | AU.addRequired<ScalarEvolutionWrapperPass>(); | 
|  | 638 | AU.addRequired<DominatorTreeWrapperPass>(); | 
|  | 639 | AU.addPreserved<DominatorTreeWrapperPass>(); | 
| Eli Friedman | 02d48be | 2016-09-16 17:58:07 +0000 | [diff] [blame] | 640 | AU.addPreserved<GlobalsAAWrapperPass>(); | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 641 | } | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 642 | }; | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 643 |  | 
|  | 644 | } // end anonymous namespace | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 645 |  | 
|  | 646 | char LoopLoadElimination::ID; | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 647 |  | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 648 | static const char LLE_name[] = "Loop Load Elimination"; | 
|  | 649 |  | 
|  | 650 | INITIALIZE_PASS_BEGIN(LoopLoadElimination, LLE_OPTION, LLE_name, false, false) | 
|  | 651 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) | 
| Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 652 | INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis) | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 653 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) | 
|  | 654 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) | 
| Adam Nemet | efb2341 | 2016-03-10 23:54:39 +0000 | [diff] [blame] | 655 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 656 | INITIALIZE_PASS_END(LoopLoadElimination, LLE_OPTION, LLE_name, false, false) | 
|  | 657 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 658 | FunctionPass *llvm::createLoopLoadEliminationPass() { | 
| Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 659 | return new LoopLoadElimination(); | 
|  | 660 | } | 
| Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 661 |  | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 662 | PreservedAnalyses LoopLoadEliminationPass::run(Function &F, | 
|  | 663 | FunctionAnalysisManager &AM) { | 
|  | 664 | auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F); | 
|  | 665 | auto &LI = AM.getResult<LoopAnalysis>(F); | 
|  | 666 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); | 
|  | 667 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); | 
|  | 668 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); | 
|  | 669 | auto &AA = AM.getResult<AAManager>(F); | 
|  | 670 | auto &AC = AM.getResult<AssumptionAnalysis>(F); | 
|  | 671 |  | 
|  | 672 | auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager(); | 
|  | 673 | bool Changed = eliminateLoadsAcrossLoops( | 
|  | 674 | F, LI, DT, [&](Loop &L) -> const LoopAccessInfo & { | 
| Alina Sbirlea | ff8b8ae | 2017-11-21 15:45:46 +0000 | [diff] [blame] | 675 | LoopStandardAnalysisResults AR = {AA, AC,  DT,  LI, | 
|  | 676 | SE, TLI, TTI, nullptr}; | 
| Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 677 | return LAM.getResult<LoopAccessAnalysis>(L, AR); | 
|  | 678 | }); | 
|  | 679 |  | 
|  | 680 | if (!Changed) | 
|  | 681 | return PreservedAnalyses::all(); | 
|  | 682 |  | 
|  | 683 | PreservedAnalyses PA; | 
|  | 684 | return PA; | 
|  | 685 | } |