1 //===- LoopLoadElimination.cpp - Loop Load Elimination Pass ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implement a loop-aware load elimination pass.
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.
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
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Analysis/AssumptionCache.h"
31 #include "llvm/Analysis/BlockFrequencyInfo.h"
32 #include "llvm/Analysis/GlobalsModRef.h"
33 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
34 #include "llvm/Analysis/LoopAccessAnalysis.h"
35 #include "llvm/Analysis/LoopAnalysisManager.h"
36 #include "llvm/Analysis/LoopInfo.h"
37 #include "llvm/Analysis/ProfileSummaryInfo.h"
38 #include "llvm/Analysis/ScalarEvolution.h"
39 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
40 #include "llvm/Analysis/TargetLibraryInfo.h"
41 #include "llvm/Analysis/TargetTransformInfo.h"
42 #include "llvm/IR/DataLayout.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/PassManager.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/Value.h"
49 #include "llvm/Support/Casting.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Transforms/Utils.h"
54 #include "llvm/Transforms/Utils/LoopSimplify.h"
55 #include "llvm/Transforms/Utils/LoopVersioning.h"
56 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
57 #include "llvm/Transforms/Utils/SizeOpts.h"
60 #include <forward_list>
66 #define LLE_OPTION "loop-load-elim"
67 #define DEBUG_TYPE LLE_OPTION
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"),
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 "
79 STATISTIC(NumLoopLoadEliminted
, "Number of loads eliminated by LLE");
83 /// Represent a store-to-forwarding candidate.
84 struct StoreToLoadForwardingCandidate
{
88 StoreToLoadForwardingCandidate(LoadInst
*Load
, StoreInst
*Store
)
89 : Load(Load
), Store(Store
) {}
91 /// Return true if the dependence from the store to the load has an
92 /// absolute distance of one.
93 /// E.g. A[i+1] = A[i] (or A[i-1] = A[i] for descending loop)
94 bool isDependenceDistanceOfOne(PredicatedScalarEvolution
&PSE
,
96 Value
*LoadPtr
= Load
->getPointerOperand();
97 Value
*StorePtr
= Store
->getPointerOperand();
98 Type
*LoadType
= getLoadStoreType(Load
);
99 auto &DL
= Load
->getParent()->getModule()->getDataLayout();
101 assert(LoadPtr
->getType()->getPointerAddressSpace() ==
102 StorePtr
->getType()->getPointerAddressSpace() &&
103 DL
.getTypeSizeInBits(LoadType
) ==
104 DL
.getTypeSizeInBits(getLoadStoreType(Store
)) &&
105 "Should be a known dependence");
107 int64_t StrideLoad
= getPtrStride(PSE
, LoadType
, LoadPtr
, L
).value_or(0);
108 int64_t StrideStore
= getPtrStride(PSE
, LoadType
, StorePtr
, L
).value_or(0);
109 if (!StrideLoad
|| !StrideStore
|| StrideLoad
!= StrideStore
)
112 // TODO: This check for stride values other than 1 and -1 can be eliminated.
113 // However, doing so may cause the LoopAccessAnalysis to overcompensate,
114 // generating numerous non-wrap runtime checks that may undermine the
115 // benefits of load elimination. To safely implement support for non-unit
116 // strides, we would need to ensure either that the processed case does not
117 // require these additional checks, or improve the LAA to handle them more
118 // efficiently, or potentially both.
119 if (std::abs(StrideLoad
) != 1)
122 unsigned TypeByteSize
= DL
.getTypeAllocSize(const_cast<Type
*>(LoadType
));
124 auto *LoadPtrSCEV
= cast
<SCEVAddRecExpr
>(PSE
.getSCEV(LoadPtr
));
125 auto *StorePtrSCEV
= cast
<SCEVAddRecExpr
>(PSE
.getSCEV(StorePtr
));
127 // We don't need to check non-wrapping here because forward/backward
128 // dependence wouldn't be valid if these weren't monotonic accesses.
129 auto *Dist
= cast
<SCEVConstant
>(
130 PSE
.getSE()->getMinusSCEV(StorePtrSCEV
, LoadPtrSCEV
));
131 const APInt
&Val
= Dist
->getAPInt();
132 return Val
== TypeByteSize
* StrideLoad
;
135 Value
*getLoadPtr() const { return Load
->getPointerOperand(); }
138 friend raw_ostream
&operator<<(raw_ostream
&OS
,
139 const StoreToLoadForwardingCandidate
&Cand
) {
140 OS
<< *Cand
.Store
<< " -->\n";
141 OS
.indent(2) << *Cand
.Load
<< "\n";
147 } // end anonymous namespace
149 /// Check if the store dominates all latches, so as long as there is no
150 /// intervening store this value will be loaded in the next iteration.
151 static bool doesStoreDominatesAllLatches(BasicBlock
*StoreBlock
, Loop
*L
,
153 SmallVector
<BasicBlock
*, 8> Latches
;
154 L
->getLoopLatches(Latches
);
155 return llvm::all_of(Latches
, [&](const BasicBlock
*Latch
) {
156 return DT
->dominates(StoreBlock
, Latch
);
160 /// Return true if the load is not executed on all paths in the loop.
161 static bool isLoadConditional(LoadInst
*Load
, Loop
*L
) {
162 return Load
->getParent() != L
->getHeader();
167 /// The per-loop class that does most of the work.
168 class LoadEliminationForLoop
{
170 LoadEliminationForLoop(Loop
*L
, LoopInfo
*LI
, const LoopAccessInfo
&LAI
,
171 DominatorTree
*DT
, BlockFrequencyInfo
*BFI
,
172 ProfileSummaryInfo
* PSI
)
173 : L(L
), LI(LI
), LAI(LAI
), DT(DT
), BFI(BFI
), PSI(PSI
), PSE(LAI
.getPSE()) {}
175 /// Look through the loop-carried and loop-independent dependences in
176 /// this loop and find store->load dependences.
178 /// Note that no candidate is returned if LAA has failed to analyze the loop
179 /// (e.g. if it's not bottom-tested, contains volatile memops, etc.)
180 std::forward_list
<StoreToLoadForwardingCandidate
>
181 findStoreToLoadDependences(const LoopAccessInfo
&LAI
) {
182 std::forward_list
<StoreToLoadForwardingCandidate
> Candidates
;
184 const auto *Deps
= LAI
.getDepChecker().getDependences();
188 // Find store->load dependences (consequently true dep). Both lexically
189 // forward and backward dependences qualify. Disqualify loads that have
190 // other unknown dependences.
192 SmallPtrSet
<Instruction
*, 4> LoadsWithUnknownDepedence
;
194 for (const auto &Dep
: *Deps
) {
195 Instruction
*Source
= Dep
.getSource(LAI
);
196 Instruction
*Destination
= Dep
.getDestination(LAI
);
198 if (Dep
.Type
== MemoryDepChecker::Dependence::Unknown
||
199 Dep
.Type
== MemoryDepChecker::Dependence::IndirectUnsafe
) {
200 if (isa
<LoadInst
>(Source
))
201 LoadsWithUnknownDepedence
.insert(Source
);
202 if (isa
<LoadInst
>(Destination
))
203 LoadsWithUnknownDepedence
.insert(Destination
);
207 if (Dep
.isBackward())
208 // Note that the designations source and destination follow the program
209 // order, i.e. source is always first. (The direction is given by the
211 std::swap(Source
, Destination
);
213 assert(Dep
.isForward() && "Needs to be a forward dependence");
215 auto *Store
= dyn_cast
<StoreInst
>(Source
);
218 auto *Load
= dyn_cast
<LoadInst
>(Destination
);
222 // Only propagate if the stored values are bit/pointer castable.
223 if (!CastInst::isBitOrNoopPointerCastable(
224 getLoadStoreType(Store
), getLoadStoreType(Load
),
225 Store
->getParent()->getModule()->getDataLayout()))
228 Candidates
.emplace_front(Load
, Store
);
231 if (!LoadsWithUnknownDepedence
.empty())
232 Candidates
.remove_if([&](const StoreToLoadForwardingCandidate
&C
) {
233 return LoadsWithUnknownDepedence
.count(C
.Load
);
239 /// Return the index of the instruction according to program order.
240 unsigned getInstrIndex(Instruction
*Inst
) {
241 auto I
= InstOrder
.find(Inst
);
242 assert(I
!= InstOrder
.end() && "No index for instruction");
246 /// If a load has multiple candidates associated (i.e. different
247 /// stores), it means that it could be forwarding from multiple stores
248 /// depending on control flow. Remove these candidates.
250 /// Here, we rely on LAA to include the relevant loop-independent dependences.
251 /// LAA is known to omit these in the very simple case when the read and the
252 /// write within an alias set always takes place using the *same* pointer.
254 /// However, we know that this is not the case here, i.e. we can rely on LAA
255 /// to provide us with loop-independent dependences for the cases we're
256 /// interested. Consider the case for example where a loop-independent
257 /// dependece S1->S2 invalidates the forwarding S3->S2.
261 /// A[i+1] = ... (S3)
263 /// LAA will perform dependence analysis here because there are two
264 /// *different* pointers involved in the same alias set (&A[i] and &A[i+1]).
265 void removeDependencesFromMultipleStores(
266 std::forward_list
<StoreToLoadForwardingCandidate
> &Candidates
) {
267 // If Store is nullptr it means that we have multiple stores forwarding to
269 using LoadToSingleCandT
=
270 DenseMap
<LoadInst
*, const StoreToLoadForwardingCandidate
*>;
271 LoadToSingleCandT LoadToSingleCand
;
273 for (const auto &Cand
: Candidates
) {
275 LoadToSingleCandT::iterator Iter
;
277 std::tie(Iter
, NewElt
) =
278 LoadToSingleCand
.insert(std::make_pair(Cand
.Load
, &Cand
));
280 const StoreToLoadForwardingCandidate
*&OtherCand
= Iter
->second
;
281 // Already multiple stores forward to this load.
282 if (OtherCand
== nullptr)
285 // Handle the very basic case when the two stores are in the same block
286 // so deciding which one forwards is easy. The later one forwards as
287 // long as they both have a dependence distance of one to the load.
288 if (Cand
.Store
->getParent() == OtherCand
->Store
->getParent() &&
289 Cand
.isDependenceDistanceOfOne(PSE
, L
) &&
290 OtherCand
->isDependenceDistanceOfOne(PSE
, L
)) {
291 // They are in the same block, the later one will forward to the load.
292 if (getInstrIndex(OtherCand
->Store
) < getInstrIndex(Cand
.Store
))
299 Candidates
.remove_if([&](const StoreToLoadForwardingCandidate
&Cand
) {
300 if (LoadToSingleCand
[Cand
.Load
] != &Cand
) {
302 dbgs() << "Removing from candidates: \n"
304 << " The load may have multiple stores forwarding to "
312 /// Given two pointers operations by their RuntimePointerChecking
313 /// indices, return true if they require an alias check.
315 /// We need a check if one is a pointer for a candidate load and the other is
316 /// a pointer for a possibly intervening store.
317 bool needsChecking(unsigned PtrIdx1
, unsigned PtrIdx2
,
318 const SmallPtrSetImpl
<Value
*> &PtrsWrittenOnFwdingPath
,
319 const SmallPtrSetImpl
<Value
*> &CandLoadPtrs
) {
321 LAI
.getRuntimePointerChecking()->getPointerInfo(PtrIdx1
).PointerValue
;
323 LAI
.getRuntimePointerChecking()->getPointerInfo(PtrIdx2
).PointerValue
;
324 return ((PtrsWrittenOnFwdingPath
.count(Ptr1
) && CandLoadPtrs
.count(Ptr2
)) ||
325 (PtrsWrittenOnFwdingPath
.count(Ptr2
) && CandLoadPtrs
.count(Ptr1
)));
328 /// Return pointers that are possibly written to on the path from a
329 /// forwarding store to a load.
331 /// These pointers need to be alias-checked against the forwarding candidates.
332 SmallPtrSet
<Value
*, 4> findPointersWrittenOnForwardingPath(
333 const SmallVectorImpl
<StoreToLoadForwardingCandidate
> &Candidates
) {
334 // From FirstStore to LastLoad neither of the elimination candidate loads
335 // should overlap with any of the stores.
340 // ld1 B[i] <-------,
341 // ld0 A[i] <----, | * LastLoad
344 // st3 B[i+1] -- | -' * FirstStore
348 // st0 forwards to ld0 if the accesses in st4 and st1 don't overlap with
352 std::max_element(Candidates
.begin(), Candidates
.end(),
353 [&](const StoreToLoadForwardingCandidate
&A
,
354 const StoreToLoadForwardingCandidate
&B
) {
355 return getInstrIndex(A
.Load
) < getInstrIndex(B
.Load
);
358 StoreInst
*FirstStore
=
359 std::min_element(Candidates
.begin(), Candidates
.end(),
360 [&](const StoreToLoadForwardingCandidate
&A
,
361 const StoreToLoadForwardingCandidate
&B
) {
362 return getInstrIndex(A
.Store
) <
363 getInstrIndex(B
.Store
);
367 // We're looking for stores after the first forwarding store until the end
368 // of the loop, then from the beginning of the loop until the last
369 // forwarded-to load. Collect the pointer for the stores.
370 SmallPtrSet
<Value
*, 4> PtrsWrittenOnFwdingPath
;
372 auto InsertStorePtr
= [&](Instruction
*I
) {
373 if (auto *S
= dyn_cast
<StoreInst
>(I
))
374 PtrsWrittenOnFwdingPath
.insert(S
->getPointerOperand());
376 const auto &MemInstrs
= LAI
.getDepChecker().getMemoryInstructions();
377 std::for_each(MemInstrs
.begin() + getInstrIndex(FirstStore
) + 1,
378 MemInstrs
.end(), InsertStorePtr
);
379 std::for_each(MemInstrs
.begin(), &MemInstrs
[getInstrIndex(LastLoad
)],
382 return PtrsWrittenOnFwdingPath
;
385 /// Determine the pointer alias checks to prove that there are no
386 /// intervening stores.
387 SmallVector
<RuntimePointerCheck
, 4> collectMemchecks(
388 const SmallVectorImpl
<StoreToLoadForwardingCandidate
> &Candidates
) {
390 SmallPtrSet
<Value
*, 4> PtrsWrittenOnFwdingPath
=
391 findPointersWrittenOnForwardingPath(Candidates
);
393 // Collect the pointers of the candidate loads.
394 SmallPtrSet
<Value
*, 4> CandLoadPtrs
;
395 for (const auto &Candidate
: Candidates
)
396 CandLoadPtrs
.insert(Candidate
.getLoadPtr());
398 const auto &AllChecks
= LAI
.getRuntimePointerChecking()->getChecks();
399 SmallVector
<RuntimePointerCheck
, 4> Checks
;
401 copy_if(AllChecks
, std::back_inserter(Checks
),
402 [&](const RuntimePointerCheck
&Check
) {
403 for (auto PtrIdx1
: Check
.first
->Members
)
404 for (auto PtrIdx2
: Check
.second
->Members
)
405 if (needsChecking(PtrIdx1
, PtrIdx2
, PtrsWrittenOnFwdingPath
,
411 LLVM_DEBUG(dbgs() << "\nPointer Checks (count: " << Checks
.size()
413 LLVM_DEBUG(LAI
.getRuntimePointerChecking()->printChecks(dbgs(), Checks
));
418 /// Perform the transformation for a candidate.
420 propagateStoredValueToLoadUsers(const StoreToLoadForwardingCandidate
&Cand
,
425 // store %y, %gep_i_plus_1
430 // %x.initial = load %gep_0
432 // %x.storeforward = phi [%x.initial, %ph] [%y, %loop]
433 // %x = load %gep_i <---- now dead
434 // = ... %x.storeforward
435 // store %y, %gep_i_plus_1
437 Value
*Ptr
= Cand
.Load
->getPointerOperand();
438 auto *PtrSCEV
= cast
<SCEVAddRecExpr
>(PSE
.getSCEV(Ptr
));
439 auto *PH
= L
->getLoopPreheader();
440 assert(PH
&& "Preheader should exist!");
441 Value
*InitialPtr
= SEE
.expandCodeFor(PtrSCEV
->getStart(), Ptr
->getType(),
442 PH
->getTerminator());
443 Value
*Initial
= new LoadInst(
444 Cand
.Load
->getType(), InitialPtr
, "load_initial",
445 /* isVolatile */ false, Cand
.Load
->getAlign(), PH
->getTerminator());
447 PHINode
*PHI
= PHINode::Create(Initial
->getType(), 2, "store_forwarded");
448 PHI
->insertBefore(L
->getHeader()->begin());
449 PHI
->addIncoming(Initial
, PH
);
451 Type
*LoadType
= Initial
->getType();
452 Type
*StoreType
= Cand
.Store
->getValueOperand()->getType();
453 auto &DL
= Cand
.Load
->getParent()->getModule()->getDataLayout();
456 assert(DL
.getTypeSizeInBits(LoadType
) == DL
.getTypeSizeInBits(StoreType
) &&
457 "The type sizes should match!");
459 Value
*StoreValue
= Cand
.Store
->getValueOperand();
460 if (LoadType
!= StoreType
)
461 StoreValue
= CastInst::CreateBitOrPointerCast(
462 StoreValue
, LoadType
, "store_forward_cast", Cand
.Store
);
464 PHI
->addIncoming(StoreValue
, L
->getLoopLatch());
466 Cand
.Load
->replaceAllUsesWith(PHI
);
469 /// Top-level driver for each loop: find store->load forwarding
470 /// candidates, add run-time checks and perform transformation.
472 LLVM_DEBUG(dbgs() << "\nIn \"" << L
->getHeader()->getParent()->getName()
473 << "\" checking " << *L
<< "\n");
475 // Look for store-to-load forwarding cases across the
481 // store %y, %gep_i_plus_1
486 // %x.initial = load %gep_0
488 // %x.storeforward = phi [%x.initial, %ph] [%y, %loop]
489 // %x = load %gep_i <---- now dead
490 // = ... %x.storeforward
491 // store %y, %gep_i_plus_1
493 // First start with store->load dependences.
494 auto StoreToLoadDependences
= findStoreToLoadDependences(LAI
);
495 if (StoreToLoadDependences
.empty())
498 // Generate an index for each load and store according to the original
499 // program order. This will be used later.
500 InstOrder
= LAI
.getDepChecker().generateInstructionOrderMap();
502 // To keep things simple for now, remove those where the load is potentially
503 // fed by multiple stores.
504 removeDependencesFromMultipleStores(StoreToLoadDependences
);
505 if (StoreToLoadDependences
.empty())
508 // Filter the candidates further.
509 SmallVector
<StoreToLoadForwardingCandidate
, 4> Candidates
;
510 for (const StoreToLoadForwardingCandidate
&Cand
: StoreToLoadDependences
) {
511 LLVM_DEBUG(dbgs() << "Candidate " << Cand
);
513 // Make sure that the stored values is available everywhere in the loop in
514 // the next iteration.
515 if (!doesStoreDominatesAllLatches(Cand
.Store
->getParent(), L
, DT
))
518 // If the load is conditional we can't hoist its 0-iteration instance to
519 // the preheader because that would make it unconditional. Thus we would
520 // access a memory location that the original loop did not access.
521 if (isLoadConditional(Cand
.Load
, L
))
524 // Check whether the SCEV difference is the same as the induction step,
525 // thus we load the value in the next iteration.
526 if (!Cand
.isDependenceDistanceOfOne(PSE
, L
))
529 assert(isa
<SCEVAddRecExpr
>(PSE
.getSCEV(Cand
.Load
->getPointerOperand())) &&
530 "Loading from something other than indvar?");
532 isa
<SCEVAddRecExpr
>(PSE
.getSCEV(Cand
.Store
->getPointerOperand())) &&
533 "Storing to something other than indvar?");
535 Candidates
.push_back(Cand
);
539 << ". Valid store-to-load forwarding across the loop backedge\n");
541 if (Candidates
.empty())
544 // Check intervening may-alias stores. These need runtime checks for alias
546 SmallVector
<RuntimePointerCheck
, 4> Checks
= collectMemchecks(Candidates
);
548 // Too many checks are likely to outweigh the benefits of forwarding.
549 if (Checks
.size() > Candidates
.size() * CheckPerElim
) {
550 LLVM_DEBUG(dbgs() << "Too many run-time checks needed.\n");
554 if (LAI
.getPSE().getPredicate().getComplexity() >
555 LoadElimSCEVCheckThreshold
) {
556 LLVM_DEBUG(dbgs() << "Too many SCEV run-time checks needed.\n");
560 if (!L
->isLoopSimplifyForm()) {
561 LLVM_DEBUG(dbgs() << "Loop is not is loop-simplify form");
565 if (!Checks
.empty() || !LAI
.getPSE().getPredicate().isAlwaysTrue()) {
566 if (LAI
.hasConvergentOp()) {
567 LLVM_DEBUG(dbgs() << "Versioning is needed but not allowed with "
568 "convergent calls\n");
572 auto *HeaderBB
= L
->getHeader();
573 auto *F
= HeaderBB
->getParent();
574 bool OptForSize
= F
->hasOptSize() ||
575 llvm::shouldOptimizeForSize(HeaderBB
, PSI
, BFI
,
576 PGSOQueryType::IRPass
);
579 dbgs() << "Versioning is needed but not allowed when optimizing "
584 // Point of no-return, start the transformation. First, version the loop
587 LoopVersioning
LV(LAI
, Checks
, L
, LI
, DT
, PSE
.getSE());
590 // After versioning, some of the candidates' pointers could stop being
591 // SCEVAddRecs. We need to filter them out.
592 auto NoLongerGoodCandidate
= [this](
593 const StoreToLoadForwardingCandidate
&Cand
) {
594 return !isa
<SCEVAddRecExpr
>(
595 PSE
.getSCEV(Cand
.Load
->getPointerOperand())) ||
596 !isa
<SCEVAddRecExpr
>(
597 PSE
.getSCEV(Cand
.Store
->getPointerOperand()));
599 llvm::erase_if(Candidates
, NoLongerGoodCandidate
);
602 // Next, propagate the value stored by the store to the users of the load.
603 // Also for the first iteration, generate the initial value of the load.
604 SCEVExpander
SEE(*PSE
.getSE(), L
->getHeader()->getModule()->getDataLayout(),
606 for (const auto &Cand
: Candidates
)
607 propagateStoredValueToLoadUsers(Cand
, SEE
);
608 NumLoopLoadEliminted
+= Candidates
.size();
616 /// Maps the load/store instructions to their index according to
618 DenseMap
<Instruction
*, unsigned> InstOrder
;
622 const LoopAccessInfo
&LAI
;
624 BlockFrequencyInfo
*BFI
;
625 ProfileSummaryInfo
*PSI
;
626 PredicatedScalarEvolution PSE
;
629 } // end anonymous namespace
631 static bool eliminateLoadsAcrossLoops(Function
&F
, LoopInfo
&LI
,
633 BlockFrequencyInfo
*BFI
,
634 ProfileSummaryInfo
*PSI
,
635 ScalarEvolution
*SE
, AssumptionCache
*AC
,
636 LoopAccessInfoManager
&LAIs
) {
637 // Build up a worklist of inner-loops to transform to avoid iterator
639 // FIXME: This logic comes from other passes that actually change the loop
640 // nest structure. It isn't clear this is necessary (or useful) for a pass
641 // which merely optimizes the use of loads in a loop.
642 SmallVector
<Loop
*, 8> Worklist
;
644 bool Changed
= false;
646 for (Loop
*TopLevelLoop
: LI
)
647 for (Loop
*L
: depth_first(TopLevelLoop
)) {
648 Changed
|= simplifyLoop(L
, &DT
, &LI
, SE
, AC
, /*MSSAU*/ nullptr, false);
649 // We only handle inner-most loops.
650 if (L
->isInnermost())
651 Worklist
.push_back(L
);
654 // Now walk the identified inner loops.
655 for (Loop
*L
: Worklist
) {
656 // Match historical behavior
657 if (!L
->isRotatedForm() || !L
->getExitingBlock())
659 // The actual work is performed by LoadEliminationForLoop.
660 LoadEliminationForLoop
LEL(L
, &LI
, LAIs
.getInfo(*L
), &DT
, BFI
, PSI
);
661 Changed
|= LEL
.processLoop();
668 PreservedAnalyses
LoopLoadEliminationPass::run(Function
&F
,
669 FunctionAnalysisManager
&AM
) {
670 auto &LI
= AM
.getResult
<LoopAnalysis
>(F
);
671 // There are no loops in the function. Return before computing other expensive
674 return PreservedAnalyses::all();
675 auto &SE
= AM
.getResult
<ScalarEvolutionAnalysis
>(F
);
676 auto &DT
= AM
.getResult
<DominatorTreeAnalysis
>(F
);
677 auto &AC
= AM
.getResult
<AssumptionAnalysis
>(F
);
678 auto &MAMProxy
= AM
.getResult
<ModuleAnalysisManagerFunctionProxy
>(F
);
679 auto *PSI
= MAMProxy
.getCachedResult
<ProfileSummaryAnalysis
>(*F
.getParent());
680 auto *BFI
= (PSI
&& PSI
->hasProfileSummary()) ?
681 &AM
.getResult
<BlockFrequencyAnalysis
>(F
) : nullptr;
682 LoopAccessInfoManager
&LAIs
= AM
.getResult
<LoopAccessAnalysis
>(F
);
684 bool Changed
= eliminateLoadsAcrossLoops(F
, LI
, DT
, BFI
, PSI
, &SE
, &AC
, LAIs
);
687 return PreservedAnalyses::all();
689 PreservedAnalyses PA
;
690 PA
.preserve
<DominatorTreeAnalysis
>();
691 PA
.preserve
<LoopAnalysis
>();