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