1 //===- LoopLoadElimination.cpp - Loop Load Elimination Pass ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implement a loop-aware load elimination pass.
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.
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
21 //===----------------------------------------------------------------------===//
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/LoopAccessAnalysis.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/ScalarEvolutionExpander.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Transforms/Scalar.h"
32 #include "llvm/Transforms/Utils/LoopVersioning.h"
33 #include <forward_list>
35 #define LLE_OPTION "loop-load-elim"
36 #define DEBUG_TYPE LLE_OPTION
40 static cl::opt<unsigned> CheckPerElim(
41 "runtime-check-per-loop-load-elim", cl::Hidden,
42 cl::desc("Max number of memchecks allowed per eliminated load on average"),
45 static cl::opt<unsigned> LoadElimSCEVCheckThreshold(
46 "loop-load-elimination-scev-check-threshold", cl::init(8), cl::Hidden,
47 cl::desc("The maximum number of SCEV checks allowed for Loop "
51 STATISTIC(NumLoopLoadEliminted, "Number of loads eliminated by LLE");
55 /// \brief Represent a store-to-forwarding candidate.
56 struct StoreToLoadForwardingCandidate {
60 StoreToLoadForwardingCandidate(LoadInst *Load, StoreInst *Store)
61 : Load(Load), Store(Store) {}
63 /// \brief Return true if the dependence from the store to the load has a
64 /// distance of one. E.g. A[i+1] = A[i]
65 bool isDependenceDistanceOfOne(PredicatedScalarEvolution &PSE,
67 Value *LoadPtr = Load->getPointerOperand();
68 Value *StorePtr = Store->getPointerOperand();
69 Type *LoadPtrType = LoadPtr->getType();
70 Type *LoadType = LoadPtrType->getPointerElementType();
72 assert(LoadPtrType->getPointerAddressSpace() ==
73 StorePtr->getType()->getPointerAddressSpace() &&
74 LoadType == StorePtr->getType()->getPointerElementType() &&
75 "Should be a known dependence");
77 // Currently we only support accesses with unit stride. FIXME: we should be
78 // able to handle non unit stirde as well as long as the stride is equal to
79 // the dependence distance.
80 if (getPtrStride(PSE, LoadPtr, L) != 1 ||
81 getPtrStride(PSE, StorePtr, L) != 1)
84 auto &DL = Load->getParent()->getModule()->getDataLayout();
85 unsigned TypeByteSize = DL.getTypeAllocSize(const_cast<Type *>(LoadType));
87 auto *LoadPtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(LoadPtr));
88 auto *StorePtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(StorePtr));
90 // We don't need to check non-wrapping here because forward/backward
91 // dependence wouldn't be valid if these weren't monotonic accesses.
92 auto *Dist = cast<SCEVConstant>(
93 PSE.getSE()->getMinusSCEV(StorePtrSCEV, LoadPtrSCEV));
94 const APInt &Val = Dist->getAPInt();
95 return Val == TypeByteSize;
98 Value *getLoadPtr() const { return Load->getPointerOperand(); }
101 friend raw_ostream &operator<<(raw_ostream &OS,
102 const StoreToLoadForwardingCandidate &Cand) {
103 OS << *Cand.Store << " -->\n";
104 OS.indent(2) << *Cand.Load << "\n";
110 /// \brief Check if the store dominates all latches, so as long as there is no
111 /// intervening store this value will be loaded in the next iteration.
112 bool doesStoreDominatesAllLatches(BasicBlock *StoreBlock, Loop *L,
114 SmallVector<BasicBlock *, 8> Latches;
115 L->getLoopLatches(Latches);
116 return std::all_of(Latches.begin(), Latches.end(),
117 [&](const BasicBlock *Latch) {
118 return DT->dominates(StoreBlock, Latch);
122 /// \brief Return true if the load is not executed on all paths in the loop.
123 static bool isLoadConditional(LoadInst *Load, Loop *L) {
124 return Load->getParent() != L->getHeader();
127 /// \brief The per-loop class that does most of the work.
128 class LoadEliminationForLoop {
130 LoadEliminationForLoop(Loop *L, LoopInfo *LI, const LoopAccessInfo &LAI,
132 : L(L), LI(LI), LAI(LAI), DT(DT), PSE(LAI.getPSE()) {}
134 /// \brief Look through the loop-carried and loop-independent dependences in
135 /// this loop and find store->load dependences.
137 /// Note that no candidate is returned if LAA has failed to analyze the loop
138 /// (e.g. if it's not bottom-tested, contains volatile memops, etc.)
139 std::forward_list<StoreToLoadForwardingCandidate>
140 findStoreToLoadDependences(const LoopAccessInfo &LAI) {
141 std::forward_list<StoreToLoadForwardingCandidate> Candidates;
143 const auto *Deps = LAI.getDepChecker().getDependences();
147 // Find store->load dependences (consequently true dep). Both lexically
148 // forward and backward dependences qualify. Disqualify loads that have
149 // other unknown dependences.
151 SmallSet<Instruction *, 4> LoadsWithUnknownDepedence;
153 for (const auto &Dep : *Deps) {
154 Instruction *Source = Dep.getSource(LAI);
155 Instruction *Destination = Dep.getDestination(LAI);
157 if (Dep.Type == MemoryDepChecker::Dependence::Unknown) {
158 if (isa<LoadInst>(Source))
159 LoadsWithUnknownDepedence.insert(Source);
160 if (isa<LoadInst>(Destination))
161 LoadsWithUnknownDepedence.insert(Destination);
165 if (Dep.isBackward())
166 // Note that the designations source and destination follow the program
167 // order, i.e. source is always first. (The direction is given by the
169 std::swap(Source, Destination);
171 assert(Dep.isForward() && "Needs to be a forward dependence");
173 auto *Store = dyn_cast<StoreInst>(Source);
176 auto *Load = dyn_cast<LoadInst>(Destination);
180 // Only progagate the value if they are of the same type.
181 if (Store->getPointerOperand()->getType() !=
182 Load->getPointerOperand()->getType())
185 Candidates.emplace_front(Load, Store);
188 if (!LoadsWithUnknownDepedence.empty())
189 Candidates.remove_if([&](const StoreToLoadForwardingCandidate &C) {
190 return LoadsWithUnknownDepedence.count(C.Load);
196 /// \brief Return the index of the instruction according to program order.
197 unsigned getInstrIndex(Instruction *Inst) {
198 auto I = InstOrder.find(Inst);
199 assert(I != InstOrder.end() && "No index for instruction");
203 /// \brief If a load has multiple candidates associated (i.e. different
204 /// stores), it means that it could be forwarding from multiple stores
205 /// depending on control flow. Remove these candidates.
207 /// Here, we rely on LAA to include the relevant loop-independent dependences.
208 /// LAA is known to omit these in the very simple case when the read and the
209 /// write within an alias set always takes place using the *same* pointer.
211 /// However, we know that this is not the case here, i.e. we can rely on LAA
212 /// to provide us with loop-independent dependences for the cases we're
213 /// interested. Consider the case for example where a loop-independent
214 /// dependece S1->S2 invalidates the forwarding S3->S2.
218 /// A[i+1] = ... (S3)
220 /// LAA will perform dependence analysis here because there are two
221 /// *different* pointers involved in the same alias set (&A[i] and &A[i+1]).
222 void removeDependencesFromMultipleStores(
223 std::forward_list<StoreToLoadForwardingCandidate> &Candidates) {
224 // If Store is nullptr it means that we have multiple stores forwarding to
226 typedef DenseMap<LoadInst *, const StoreToLoadForwardingCandidate *>
228 LoadToSingleCandT LoadToSingleCand;
230 for (const auto &Cand : Candidates) {
232 LoadToSingleCandT::iterator Iter;
234 std::tie(Iter, NewElt) =
235 LoadToSingleCand.insert(std::make_pair(Cand.Load, &Cand));
237 const StoreToLoadForwardingCandidate *&OtherCand = Iter->second;
238 // Already multiple stores forward to this load.
239 if (OtherCand == nullptr)
242 // Handle the very basic case when the two stores are in the same block
243 // so deciding which one forwards is easy. The later one forwards as
244 // long as they both have a dependence distance of one to the load.
245 if (Cand.Store->getParent() == OtherCand->Store->getParent() &&
246 Cand.isDependenceDistanceOfOne(PSE, L) &&
247 OtherCand->isDependenceDistanceOfOne(PSE, L)) {
248 // They are in the same block, the later one will forward to the load.
249 if (getInstrIndex(OtherCand->Store) < getInstrIndex(Cand.Store))
256 Candidates.remove_if([&](const StoreToLoadForwardingCandidate &Cand) {
257 if (LoadToSingleCand[Cand.Load] != &Cand) {
258 DEBUG(dbgs() << "Removing from candidates: \n" << Cand
259 << " The load may have multiple stores forwarding to "
267 /// \brief Given two pointers operations by their RuntimePointerChecking
268 /// indices, return true if they require an alias check.
270 /// We need a check if one is a pointer for a candidate load and the other is
271 /// a pointer for a possibly intervening store.
272 bool needsChecking(unsigned PtrIdx1, unsigned PtrIdx2,
273 const SmallSet<Value *, 4> &PtrsWrittenOnFwdingPath,
274 const std::set<Value *> &CandLoadPtrs) {
276 LAI.getRuntimePointerChecking()->getPointerInfo(PtrIdx1).PointerValue;
278 LAI.getRuntimePointerChecking()->getPointerInfo(PtrIdx2).PointerValue;
279 return ((PtrsWrittenOnFwdingPath.count(Ptr1) && CandLoadPtrs.count(Ptr2)) ||
280 (PtrsWrittenOnFwdingPath.count(Ptr2) && CandLoadPtrs.count(Ptr1)));
283 /// \brief Return pointers that are possibly written to on the path from a
284 /// forwarding store to a load.
286 /// These pointers need to be alias-checked against the forwarding candidates.
287 SmallSet<Value *, 4> findPointersWrittenOnForwardingPath(
288 const SmallVectorImpl<StoreToLoadForwardingCandidate> &Candidates) {
289 // From FirstStore to LastLoad neither of the elimination candidate loads
290 // should overlap with any of the stores.
295 // ld1 B[i] <-------,
296 // ld0 A[i] <----, | * LastLoad
299 // st3 B[i+1] -- | -' * FirstStore
303 // st0 forwards to ld0 if the accesses in st4 and st1 don't overlap with
307 std::max_element(Candidates.begin(), Candidates.end(),
308 [&](const StoreToLoadForwardingCandidate &A,
309 const StoreToLoadForwardingCandidate &B) {
310 return getInstrIndex(A.Load) < getInstrIndex(B.Load);
313 StoreInst *FirstStore =
314 std::min_element(Candidates.begin(), Candidates.end(),
315 [&](const StoreToLoadForwardingCandidate &A,
316 const StoreToLoadForwardingCandidate &B) {
317 return getInstrIndex(A.Store) <
318 getInstrIndex(B.Store);
322 // We're looking for stores after the first forwarding store until the end
323 // of the loop, then from the beginning of the loop until the last
324 // forwarded-to load. Collect the pointer for the stores.
325 SmallSet<Value *, 4> PtrsWrittenOnFwdingPath;
327 auto InsertStorePtr = [&](Instruction *I) {
328 if (auto *S = dyn_cast<StoreInst>(I))
329 PtrsWrittenOnFwdingPath.insert(S->getPointerOperand());
331 const auto &MemInstrs = LAI.getDepChecker().getMemoryInstructions();
332 std::for_each(MemInstrs.begin() + getInstrIndex(FirstStore) + 1,
333 MemInstrs.end(), InsertStorePtr);
334 std::for_each(MemInstrs.begin(), &MemInstrs[getInstrIndex(LastLoad)],
337 return PtrsWrittenOnFwdingPath;
340 /// \brief Determine the pointer alias checks to prove that there are no
341 /// intervening stores.
342 SmallVector<RuntimePointerChecking::PointerCheck, 4> collectMemchecks(
343 const SmallVectorImpl<StoreToLoadForwardingCandidate> &Candidates) {
345 SmallSet<Value *, 4> PtrsWrittenOnFwdingPath =
346 findPointersWrittenOnForwardingPath(Candidates);
348 // Collect the pointers of the candidate loads.
349 // FIXME: SmallSet does not work with std::inserter.
350 std::set<Value *> CandLoadPtrs;
351 std::transform(Candidates.begin(), Candidates.end(),
352 std::inserter(CandLoadPtrs, CandLoadPtrs.begin()),
353 std::mem_fn(&StoreToLoadForwardingCandidate::getLoadPtr));
355 const auto &AllChecks = LAI.getRuntimePointerChecking()->getChecks();
356 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks;
358 std::copy_if(AllChecks.begin(), AllChecks.end(), std::back_inserter(Checks),
359 [&](const RuntimePointerChecking::PointerCheck &Check) {
360 for (auto PtrIdx1 : Check.first->Members)
361 for (auto PtrIdx2 : Check.second->Members)
362 if (needsChecking(PtrIdx1, PtrIdx2,
363 PtrsWrittenOnFwdingPath, CandLoadPtrs))
368 DEBUG(dbgs() << "\nPointer Checks (count: " << Checks.size() << "):\n");
369 DEBUG(LAI.getRuntimePointerChecking()->printChecks(dbgs(), Checks));
374 /// \brief Perform the transformation for a candidate.
376 propagateStoredValueToLoadUsers(const StoreToLoadForwardingCandidate &Cand,
382 // store %y, %gep_i_plus_1
387 // %x.initial = load %gep_0
389 // %x.storeforward = phi [%x.initial, %ph] [%y, %loop]
390 // %x = load %gep_i <---- now dead
391 // = ... %x.storeforward
392 // store %y, %gep_i_plus_1
394 Value *Ptr = Cand.Load->getPointerOperand();
395 auto *PtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(Ptr));
396 auto *PH = L->getLoopPreheader();
397 Value *InitialPtr = SEE.expandCodeFor(PtrSCEV->getStart(), Ptr->getType(),
398 PH->getTerminator());
400 new LoadInst(InitialPtr, "load_initial", PH->getTerminator());
401 PHINode *PHI = PHINode::Create(Initial->getType(), 2, "store_forwarded",
402 &L->getHeader()->front());
403 PHI->addIncoming(Initial, PH);
404 PHI->addIncoming(Cand.Store->getOperand(0), L->getLoopLatch());
406 Cand.Load->replaceAllUsesWith(PHI);
409 /// \brief Top-level driver for each loop: find store->load forwarding
410 /// candidates, add run-time checks and perform transformation.
412 DEBUG(dbgs() << "\nIn \"" << L->getHeader()->getParent()->getName()
413 << "\" checking " << *L << "\n");
414 // Look for store-to-load forwarding cases across the
420 // store %y, %gep_i_plus_1
425 // %x.initial = load %gep_0
427 // %x.storeforward = phi [%x.initial, %ph] [%y, %loop]
428 // %x = load %gep_i <---- now dead
429 // = ... %x.storeforward
430 // store %y, %gep_i_plus_1
432 // First start with store->load dependences.
433 auto StoreToLoadDependences = findStoreToLoadDependences(LAI);
434 if (StoreToLoadDependences.empty())
437 // Generate an index for each load and store according to the original
438 // program order. This will be used later.
439 InstOrder = LAI.getDepChecker().generateInstructionOrderMap();
441 // To keep things simple for now, remove those where the load is potentially
442 // fed by multiple stores.
443 removeDependencesFromMultipleStores(StoreToLoadDependences);
444 if (StoreToLoadDependences.empty())
447 // Filter the candidates further.
448 SmallVector<StoreToLoadForwardingCandidate, 4> Candidates;
449 unsigned NumForwarding = 0;
450 for (const StoreToLoadForwardingCandidate Cand : StoreToLoadDependences) {
451 DEBUG(dbgs() << "Candidate " << Cand);
453 // Make sure that the stored values is available everywhere in the loop in
454 // the next iteration.
455 if (!doesStoreDominatesAllLatches(Cand.Store->getParent(), L, DT))
458 // If the load is conditional we can't hoist its 0-iteration instance to
459 // the preheader because that would make it unconditional. Thus we would
460 // access a memory location that the original loop did not access.
461 if (isLoadConditional(Cand.Load, L))
464 // Check whether the SCEV difference is the same as the induction step,
465 // thus we load the value in the next iteration.
466 if (!Cand.isDependenceDistanceOfOne(PSE, L))
472 << ". Valid store-to-load forwarding across the loop backedge\n");
473 Candidates.push_back(Cand);
475 if (Candidates.empty())
478 // Check intervening may-alias stores. These need runtime checks for alias
480 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks =
481 collectMemchecks(Candidates);
483 // Too many checks are likely to outweigh the benefits of forwarding.
484 if (Checks.size() > Candidates.size() * CheckPerElim) {
485 DEBUG(dbgs() << "Too many run-time checks needed.\n");
489 if (LAI.getPSE().getUnionPredicate().getComplexity() >
490 LoadElimSCEVCheckThreshold) {
491 DEBUG(dbgs() << "Too many SCEV run-time checks needed.\n");
495 if (!Checks.empty() || !LAI.getPSE().getUnionPredicate().isAlwaysTrue()) {
496 if (L->getHeader()->getParent()->optForSize()) {
497 DEBUG(dbgs() << "Versioning is needed but not allowed when optimizing "
502 // Point of no-return, start the transformation. First, version the loop
505 LoopVersioning LV(LAI, L, LI, DT, PSE.getSE(), false);
506 LV.setAliasChecks(std::move(Checks));
507 LV.setSCEVChecks(LAI.getPSE().getUnionPredicate());
511 // Next, propagate the value stored by the store to the users of the load.
512 // Also for the first iteration, generate the initial value of the load.
513 SCEVExpander SEE(*PSE.getSE(), L->getHeader()->getModule()->getDataLayout(),
515 for (const auto &Cand : Candidates)
516 propagateStoredValueToLoadUsers(Cand, SEE);
517 NumLoopLoadEliminted += NumForwarding;
525 /// \brief Maps the load/store instructions to their index according to
527 DenseMap<Instruction *, unsigned> InstOrder;
531 const LoopAccessInfo &LAI;
533 PredicatedScalarEvolution PSE;
536 /// \brief The pass. Most of the work is delegated to the per-loop
537 /// LoadEliminationForLoop class.
538 class LoopLoadElimination : public FunctionPass {
540 LoopLoadElimination() : FunctionPass(ID) {
541 initializeLoopLoadEliminationPass(*PassRegistry::getPassRegistry());
544 bool runOnFunction(Function &F) override {
548 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
549 auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>();
550 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
552 // Build up a worklist of inner-loops to vectorize. This is necessary as the
553 // act of distributing a loop creates new loops and can invalidate iterators
555 SmallVector<Loop *, 8> Worklist;
557 for (Loop *TopLevelLoop : *LI)
558 for (Loop *L : depth_first(TopLevelLoop))
559 // We only handle inner-most loops.
561 Worklist.push_back(L);
563 // Now walk the identified inner loops.
564 bool Changed = false;
565 for (Loop *L : Worklist) {
566 const LoopAccessInfo &LAI = LAA->getInfo(L);
567 // The actual work is performed by LoadEliminationForLoop.
568 LoadEliminationForLoop LEL(L, LI, LAI, DT);
569 Changed |= LEL.processLoop();
572 // Process each loop nest in the function.
576 void getAnalysisUsage(AnalysisUsage &AU) const override {
577 AU.addRequiredID(LoopSimplifyID);
578 AU.addRequired<LoopInfoWrapperPass>();
579 AU.addPreserved<LoopInfoWrapperPass>();
580 AU.addRequired<LoopAccessLegacyAnalysis>();
581 AU.addRequired<ScalarEvolutionWrapperPass>();
582 AU.addRequired<DominatorTreeWrapperPass>();
583 AU.addPreserved<DominatorTreeWrapperPass>();
590 char LoopLoadElimination::ID;
591 static const char LLE_name[] = "Loop Load Elimination";
593 INITIALIZE_PASS_BEGIN(LoopLoadElimination, LLE_OPTION, LLE_name, false, false)
594 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
595 INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
596 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
597 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
598 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
599 INITIALIZE_PASS_END(LoopLoadElimination, LLE_OPTION, LLE_name, false, false)
602 FunctionPass *createLoopLoadEliminationPass() {
603 return new LoopLoadElimination();