1 //===- LoopCacheAnalysis.cpp - Loop Cache Analysis -------------------------==//
3 // The LLVM Compiler Infrastructure
5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 // See https://llvm.org/LICENSE.txt for license information.
7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 //===----------------------------------------------------------------------===//
12 /// This file defines the implementation for the loop cache analysis.
13 /// The implementation is largely based on the following paper:
15 /// Compiler Optimizations for Improving Data Locality
16 /// By: Steve Carr, Katherine S. McKinley, Chau-Wen Tseng
17 /// http://www.cs.utexas.edu/users/mckinley/papers/asplos-1994.pdf
19 /// The general approach taken to estimate the number of cache lines used by the
20 /// memory references in an inner loop is:
21 /// 1. Partition memory references that exhibit temporal or spacial reuse
22 /// into reference groups.
23 /// 2. For each loop L in the a loop nest LN:
24 /// a. Compute the cost of the reference group
25 /// b. Compute the loop cost by summing up the reference groups costs
26 //===----------------------------------------------------------------------===//
28 #include "llvm/Analysis/LoopCacheAnalysis.h"
29 #include "llvm/ADT/BreadthFirstIterator.h"
30 #include "llvm/ADT/Sequence.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
37 #define DEBUG_TYPE "loop-cache-cost"
39 static cl::opt<unsigned> DefaultTripCount(
40 "default-trip-count", cl::init(100), cl::Hidden,
41 cl::desc("Use this to specify the default trip count of a loop"));
43 // In this analysis two array references are considered to exhibit temporal
44 // reuse if they access either the same memory location, or a memory location
45 // with distance smaller than a configurable threshold.
46 static cl::opt<unsigned> TemporalReuseThreshold(
47 "temporal-reuse-threshold", cl::init(2), cl::Hidden,
48 cl::desc("Use this to specify the max. distance between array elements "
49 "accessed in a loop so that the elements are classified to have "
52 /// Retrieve the innermost loop in the given loop nest \p Loops. It returns a
53 /// nullptr if any loops in the loop vector supplied has more than one sibling.
54 /// The loop vector is expected to contain loops collected in breadth-first
56 static Loop *getInnerMostLoop(const LoopVectorTy &Loops) {
57 assert(!Loops.empty() && "Expecting a non-empy loop vector");
59 Loop *LastLoop = Loops.back();
60 Loop *ParentLoop = LastLoop->getParentLoop();
62 if (ParentLoop == nullptr) {
63 assert(Loops.size() == 1 && "Expecting a single loop");
67 return (std::is_sorted(Loops.begin(), Loops.end(),
68 [](const Loop *L1, const Loop *L2) {
69 return L1->getLoopDepth() < L2->getLoopDepth();
75 static bool isOneDimensionalArray(const SCEV &AccessFn, const SCEV &ElemSize,
76 const Loop &L, ScalarEvolution &SE) {
77 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(&AccessFn);
78 if (!AR || !AR->isAffine())
81 assert(AR->getLoop() && "AR should have a loop");
83 // Check that start and increment are not add recurrences.
84 const SCEV *Start = AR->getStart();
85 const SCEV *Step = AR->getStepRecurrence(SE);
86 if (isa<SCEVAddRecExpr>(Start) || isa<SCEVAddRecExpr>(Step))
89 // Check that start and increment are both invariant in the loop.
90 if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
93 return AR->getStepRecurrence(SE) == &ElemSize;
96 /// Compute the trip count for the given loop \p L. Return the SCEV expression
97 /// for the trip count or nullptr if it cannot be computed.
98 static const SCEV *computeTripCount(const Loop &L, ScalarEvolution &SE) {
99 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(&L);
100 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) ||
101 !isa<SCEVConstant>(BackedgeTakenCount))
104 return SE.getAddExpr(BackedgeTakenCount,
105 SE.getOne(BackedgeTakenCount->getType()));
108 //===----------------------------------------------------------------------===//
109 // IndexedReference implementation
111 raw_ostream &llvm::operator<<(raw_ostream &OS, const IndexedReference &R) {
113 OS << R.StoreOrLoadInst;
114 OS << ", IsValid=false.";
118 OS << *R.BasePointer;
119 for (const SCEV *Subscript : R.Subscripts)
120 OS << "[" << *Subscript << "]";
123 for (const SCEV *Size : R.Sizes)
124 OS << "[" << *Size << "]";
129 IndexedReference::IndexedReference(Instruction &StoreOrLoadInst,
130 const LoopInfo &LI, ScalarEvolution &SE)
131 : StoreOrLoadInst(StoreOrLoadInst), SE(SE) {
132 assert((isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>(StoreOrLoadInst)) &&
133 "Expecting a load or store instruction");
135 IsValid = delinearize(LI);
137 LLVM_DEBUG(dbgs().indent(2) << "Succesfully delinearized: " << *this
141 Optional<bool> IndexedReference::hasSpacialReuse(const IndexedReference &Other,
143 AliasAnalysis &AA) const {
144 assert(IsValid && "Expecting a valid reference");
146 if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
147 LLVM_DEBUG(dbgs().indent(2)
148 << "No spacial reuse: different base pointers\n");
152 unsigned NumSubscripts = getNumSubscripts();
153 if (NumSubscripts != Other.getNumSubscripts()) {
154 LLVM_DEBUG(dbgs().indent(2)
155 << "No spacial reuse: different number of subscripts\n");
159 // all subscripts must be equal, except the leftmost one (the last one).
160 for (auto SubNum : seq<unsigned>(0, NumSubscripts - 1)) {
161 if (getSubscript(SubNum) != Other.getSubscript(SubNum)) {
162 LLVM_DEBUG(dbgs().indent(2) << "No spacial reuse, different subscripts: "
163 << "\n\t" << *getSubscript(SubNum) << "\n\t"
164 << *Other.getSubscript(SubNum) << "\n");
169 // the difference between the last subscripts must be less than the cache line
171 const SCEV *LastSubscript = getLastSubscript();
172 const SCEV *OtherLastSubscript = Other.getLastSubscript();
173 const SCEVConstant *Diff = dyn_cast<SCEVConstant>(
174 SE.getMinusSCEV(LastSubscript, OtherLastSubscript));
176 if (Diff == nullptr) {
177 LLVM_DEBUG(dbgs().indent(2)
178 << "No spacial reuse, difference between subscript:\n\t"
179 << *LastSubscript << "\n\t" << OtherLastSubscript
180 << "\nis not constant.\n");
184 bool InSameCacheLine = (Diff->getValue()->getSExtValue() < CLS);
188 dbgs().indent(2) << "Found spacial reuse.\n";
190 dbgs().indent(2) << "No spacial reuse.\n";
193 return InSameCacheLine;
196 Optional<bool> IndexedReference::hasTemporalReuse(const IndexedReference &Other,
197 unsigned MaxDistance,
200 AliasAnalysis &AA) const {
201 assert(IsValid && "Expecting a valid reference");
203 if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
204 LLVM_DEBUG(dbgs().indent(2)
205 << "No temporal reuse: different base pointer\n");
209 std::unique_ptr<Dependence> D =
210 DI.depends(&StoreOrLoadInst, &Other.StoreOrLoadInst, true);
213 LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: no dependence\n");
217 if (D->isLoopIndependent()) {
218 LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n");
222 // Check the dependence distance at every loop level. There is temporal reuse
223 // if the distance at the given loop's depth is small (|d| <= MaxDistance) and
224 // it is zero at every other loop level.
225 int LoopDepth = L.getLoopDepth();
226 int Levels = D->getLevels();
227 for (int Level = 1; Level <= Levels; ++Level) {
228 const SCEV *Distance = D->getDistance(Level);
229 const SCEVConstant *SCEVConst = dyn_cast_or_null<SCEVConstant>(Distance);
231 if (SCEVConst == nullptr) {
232 LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: distance unknown\n");
236 const ConstantInt &CI = *SCEVConst->getValue();
237 if (Level != LoopDepth && !CI.isZero()) {
238 LLVM_DEBUG(dbgs().indent(2)
239 << "No temporal reuse: distance is not zero at depth=" << Level
242 } else if (Level == LoopDepth && CI.getSExtValue() > MaxDistance) {
245 << "No temporal reuse: distance is greater than MaxDistance at depth="
251 LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n");
255 CacheCostTy IndexedReference::computeRefCost(const Loop &L,
256 unsigned CLS) const {
257 assert(IsValid && "Expecting a valid reference");
259 dbgs().indent(2) << "Computing cache cost for:\n";
260 dbgs().indent(4) << *this << "\n";
263 // If the indexed reference is loop invariant the cost is one.
264 if (isLoopInvariant(L)) {
265 LLVM_DEBUG(dbgs().indent(4) << "Reference is loop invariant: RefCost=1\n");
269 const SCEV *TripCount = computeTripCount(L, SE);
271 LLVM_DEBUG(dbgs() << "Trip count of loop " << L.getName()
272 << " could not be computed, using DefaultTripCount\n");
273 const SCEV *ElemSize = Sizes.back();
274 TripCount = SE.getConstant(ElemSize->getType(), DefaultTripCount);
276 LLVM_DEBUG(dbgs() << "TripCount=" << *TripCount << "\n");
278 // If the indexed reference is 'consecutive' the cost is
279 // (TripCount*Stride)/CLS, otherwise the cost is TripCount.
280 const SCEV *RefCost = TripCount;
282 if (isConsecutive(L, CLS)) {
283 const SCEV *Coeff = getLastCoefficient();
284 const SCEV *ElemSize = Sizes.back();
285 const SCEV *Stride = SE.getMulExpr(Coeff, ElemSize);
286 const SCEV *CacheLineSize = SE.getConstant(Stride->getType(), CLS);
287 Type *WiderType = SE.getWiderType(Stride->getType(), TripCount->getType());
288 Stride = SE.getNoopOrSignExtend(Stride, WiderType);
289 TripCount = SE.getNoopOrAnyExtend(TripCount, WiderType);
290 const SCEV *Numerator = SE.getMulExpr(Stride, TripCount);
291 RefCost = SE.getUDivExpr(Numerator, CacheLineSize);
292 LLVM_DEBUG(dbgs().indent(4)
293 << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="
294 << *RefCost << "\n");
296 LLVM_DEBUG(dbgs().indent(4)
297 << "Access is not consecutive: RefCost=TripCount=" << *RefCost
300 // Attempt to fold RefCost into a constant.
301 if (auto ConstantCost = dyn_cast<SCEVConstant>(RefCost))
302 return ConstantCost->getValue()->getSExtValue();
304 LLVM_DEBUG(dbgs().indent(4)
305 << "RefCost is not a constant! Setting to RefCost=InvalidCost "
306 "(invalid value).\n");
308 return CacheCost::InvalidCost;
311 bool IndexedReference::delinearize(const LoopInfo &LI) {
312 assert(Subscripts.empty() && "Subscripts should be empty");
313 assert(Sizes.empty() && "Sizes should be empty");
314 assert(!IsValid && "Should be called once from the constructor");
315 LLVM_DEBUG(dbgs() << "Delinearizing: " << StoreOrLoadInst << "\n");
317 const SCEV *ElemSize = SE.getElementSize(&StoreOrLoadInst);
318 const BasicBlock *BB = StoreOrLoadInst.getParent();
320 if (Loop *L = LI.getLoopFor(BB)) {
321 const SCEV *AccessFn =
322 SE.getSCEVAtScope(getPointerOperand(&StoreOrLoadInst), L);
324 BasePointer = dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFn));
325 if (BasePointer == nullptr) {
328 << "ERROR: failed to delinearize, can't identify base pointer\n");
332 AccessFn = SE.getMinusSCEV(AccessFn, BasePointer);
334 LLVM_DEBUG(dbgs().indent(2) << "In Loop '" << L->getName()
335 << "', AccessFn: " << *AccessFn << "\n");
337 SE.delinearize(AccessFn, Subscripts, Sizes,
338 SE.getElementSize(&StoreOrLoadInst));
340 if (Subscripts.empty() || Sizes.empty() ||
341 Subscripts.size() != Sizes.size()) {
342 // Attempt to determine whether we have a single dimensional array access.
344 if (!isOneDimensionalArray(*AccessFn, *ElemSize, *L, SE)) {
345 LLVM_DEBUG(dbgs().indent(2)
346 << "ERROR: failed to delinearize reference\n");
352 const SCEV *Div = SE.getUDivExactExpr(AccessFn, ElemSize);
353 Subscripts.push_back(Div);
354 Sizes.push_back(ElemSize);
357 return all_of(Subscripts, [&](const SCEV *Subscript) {
358 return isSimpleAddRecurrence(*Subscript, *L);
365 bool IndexedReference::isLoopInvariant(const Loop &L) const {
366 Value *Addr = getPointerOperand(&StoreOrLoadInst);
367 assert(Addr != nullptr && "Expecting either a load or a store instruction");
368 assert(SE.isSCEVable(Addr->getType()) && "Addr should be SCEVable");
370 if (SE.isLoopInvariant(SE.getSCEV(Addr), &L))
373 // The indexed reference is loop invariant if none of the coefficients use
374 // the loop induction variable.
375 bool allCoeffForLoopAreZero = all_of(Subscripts, [&](const SCEV *Subscript) {
376 return isCoeffForLoopZeroOrInvariant(*Subscript, L);
379 return allCoeffForLoopAreZero;
382 bool IndexedReference::isConsecutive(const Loop &L, unsigned CLS) const {
383 // The indexed reference is 'consecutive' if the only coefficient that uses
384 // the loop induction variable is the last one...
385 const SCEV *LastSubscript = Subscripts.back();
386 for (const SCEV *Subscript : Subscripts) {
387 if (Subscript == LastSubscript)
389 if (!isCoeffForLoopZeroOrInvariant(*Subscript, L))
393 // ...and the access stride is less than the cache line size.
394 const SCEV *Coeff = getLastCoefficient();
395 const SCEV *ElemSize = Sizes.back();
396 const SCEV *Stride = SE.getMulExpr(Coeff, ElemSize);
397 const SCEV *CacheLineSize = SE.getConstant(Stride->getType(), CLS);
399 return SE.isKnownPredicate(ICmpInst::ICMP_ULT, Stride, CacheLineSize);
402 const SCEV *IndexedReference::getLastCoefficient() const {
403 const SCEV *LastSubscript = getLastSubscript();
404 assert(isa<SCEVAddRecExpr>(LastSubscript) &&
405 "Expecting a SCEV add recurrence expression");
406 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LastSubscript);
407 return AR->getStepRecurrence(SE);
410 bool IndexedReference::isCoeffForLoopZeroOrInvariant(const SCEV &Subscript,
411 const Loop &L) const {
412 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(&Subscript);
413 return (AR != nullptr) ? AR->getLoop() != &L
414 : SE.isLoopInvariant(&Subscript, &L);
417 bool IndexedReference::isSimpleAddRecurrence(const SCEV &Subscript,
418 const Loop &L) const {
419 if (!isa<SCEVAddRecExpr>(Subscript))
422 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(&Subscript);
423 assert(AR->getLoop() && "AR should have a loop");
428 const SCEV *Start = AR->getStart();
429 const SCEV *Step = AR->getStepRecurrence(SE);
431 if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
437 bool IndexedReference::isAliased(const IndexedReference &Other,
438 AliasAnalysis &AA) const {
439 const auto &Loc1 = MemoryLocation::get(&StoreOrLoadInst);
440 const auto &Loc2 = MemoryLocation::get(&Other.StoreOrLoadInst);
441 return AA.isMustAlias(Loc1, Loc2);
444 //===----------------------------------------------------------------------===//
445 // CacheCost implementation
447 raw_ostream &llvm::operator<<(raw_ostream &OS, const CacheCost &CC) {
448 for (const auto &LC : CC.LoopCosts) {
449 const Loop *L = LC.first;
450 OS << "Loop '" << L->getName() << "' has cost = " << LC.second << "\n";
455 CacheCost::CacheCost(const LoopVectorTy &Loops, const LoopInfo &LI,
456 ScalarEvolution &SE, TargetTransformInfo &TTI,
457 AliasAnalysis &AA, DependenceInfo &DI,
458 Optional<unsigned> TRT)
459 : Loops(Loops), TripCounts(), LoopCosts(),
460 TRT((TRT == None) ? Optional<unsigned>(TemporalReuseThreshold) : TRT),
461 LI(LI), SE(SE), TTI(TTI), AA(AA), DI(DI) {
462 assert(!Loops.empty() && "Expecting a non-empty loop vector.");
464 for (const Loop *L : Loops) {
465 unsigned TripCount = SE.getSmallConstantTripCount(L);
466 TripCount = (TripCount == 0) ? DefaultTripCount : TripCount;
467 TripCounts.push_back({L, TripCount});
470 calculateCacheFootprint();
473 std::unique_ptr<CacheCost>
474 CacheCost::getCacheCost(Loop &Root, LoopStandardAnalysisResults &AR,
475 DependenceInfo &DI, Optional<unsigned> TRT) {
476 if (Root.getParentLoop()) {
477 LLVM_DEBUG(dbgs() << "Expecting the outermost loop in a loop nest\n");
482 for (Loop *L : breadth_first(&Root))
485 if (!getInnerMostLoop(Loops)) {
486 LLVM_DEBUG(dbgs() << "Cannot compute cache cost of loop nest with more "
487 "than one innermost loop\n");
491 return std::make_unique<CacheCost>(Loops, AR.LI, AR.SE, AR.TTI, AR.AA, DI, TRT);
494 void CacheCost::calculateCacheFootprint() {
495 LLVM_DEBUG(dbgs() << "POPULATING REFERENCE GROUPS\n");
496 ReferenceGroupsTy RefGroups;
497 if (!populateReferenceGroups(RefGroups))
500 LLVM_DEBUG(dbgs() << "COMPUTING LOOP CACHE COSTS\n");
501 for (const Loop *L : Loops) {
502 assert((std::find_if(LoopCosts.begin(), LoopCosts.end(),
503 [L](const LoopCacheCostTy &LCC) {
504 return LCC.first == L;
505 }) == LoopCosts.end()) &&
506 "Should not add duplicate element");
507 CacheCostTy LoopCost = computeLoopCacheCost(*L, RefGroups);
508 LoopCosts.push_back(std::make_pair(L, LoopCost));
515 bool CacheCost::populateReferenceGroups(ReferenceGroupsTy &RefGroups) const {
516 assert(RefGroups.empty() && "Reference groups should be empty");
518 unsigned CLS = TTI.getCacheLineSize();
519 Loop *InnerMostLoop = getInnerMostLoop(Loops);
520 assert(InnerMostLoop != nullptr && "Expecting a valid innermost loop");
522 for (BasicBlock *BB : InnerMostLoop->getBlocks()) {
523 for (Instruction &I : *BB) {
524 if (!isa<StoreInst>(I) && !isa<LoadInst>(I))
527 std::unique_ptr<IndexedReference> R(new IndexedReference(I, LI, SE));
532 for (ReferenceGroupTy &RefGroup : RefGroups) {
533 const IndexedReference &Representative = *RefGroup.front().get();
535 dbgs() << "References:\n";
536 dbgs().indent(2) << *R << "\n";
537 dbgs().indent(2) << Representative << "\n";
540 Optional<bool> HasTemporalReuse =
541 R->hasTemporalReuse(Representative, *TRT, *InnerMostLoop, DI, AA);
542 Optional<bool> HasSpacialReuse =
543 R->hasSpacialReuse(Representative, CLS, AA);
545 if ((HasTemporalReuse.hasValue() && *HasTemporalReuse) ||
546 (HasSpacialReuse.hasValue() && *HasSpacialReuse)) {
547 RefGroup.push_back(std::move(R));
555 RG.push_back(std::move(R));
556 RefGroups.push_back(std::move(RG));
561 if (RefGroups.empty())
565 dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n";
567 for (const ReferenceGroupTy &RG : RefGroups) {
568 dbgs().indent(2) << "RefGroup " << n << ":\n";
569 for (const auto &IR : RG)
570 dbgs().indent(4) << *IR << "\n";
580 CacheCost::computeLoopCacheCost(const Loop &L,
581 const ReferenceGroupsTy &RefGroups) const {
582 if (!L.isLoopSimplifyForm())
585 LLVM_DEBUG(dbgs() << "Considering loop '" << L.getName()
586 << "' as innermost loop.\n");
588 // Compute the product of the trip counts of each other loop in the nest.
589 CacheCostTy TripCountsProduct = 1;
590 for (const auto &TC : TripCounts) {
593 TripCountsProduct *= TC.second;
596 CacheCostTy LoopCost = 0;
597 for (const ReferenceGroupTy &RG : RefGroups) {
598 CacheCostTy RefGroupCost = computeRefGroupCacheCost(RG, L);
599 LoopCost += RefGroupCost * TripCountsProduct;
602 LLVM_DEBUG(dbgs().indent(2) << "Loop '" << L.getName()
603 << "' has cost=" << LoopCost << "\n");
608 CacheCostTy CacheCost::computeRefGroupCacheCost(const ReferenceGroupTy &RG,
609 const Loop &L) const {
610 assert(!RG.empty() && "Reference group should have at least one member.");
612 const IndexedReference *Representative = RG.front().get();
613 return Representative->computeRefCost(L, TTI.getCacheLineSize());
616 //===----------------------------------------------------------------------===//
617 // LoopCachePrinterPass implementation
619 PreservedAnalyses LoopCachePrinterPass::run(Loop &L, LoopAnalysisManager &AM,
620 LoopStandardAnalysisResults &AR,
622 Function *F = L.getHeader()->getParent();
623 DependenceInfo DI(F, &AR.AA, &AR.SE, &AR.LI);
625 if (auto CC = CacheCost::getCacheCost(L, AR, DI))
628 return PreservedAnalyses::all();