18bcb0991SDimitry Andric //===- LoopCacheAnalysis.cpp - Loop Cache Analysis -------------------------==//
28bcb0991SDimitry Andric //
38bcb0991SDimitry Andric // The LLVM Compiler Infrastructure
48bcb0991SDimitry Andric //
58bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
68bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
78bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
88bcb0991SDimitry Andric //
98bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
108bcb0991SDimitry Andric ///
118bcb0991SDimitry Andric /// \file
128bcb0991SDimitry Andric /// This file defines the implementation for the loop cache analysis.
138bcb0991SDimitry Andric /// The implementation is largely based on the following paper:
148bcb0991SDimitry Andric ///
158bcb0991SDimitry Andric /// Compiler Optimizations for Improving Data Locality
168bcb0991SDimitry Andric /// By: Steve Carr, Katherine S. McKinley, Chau-Wen Tseng
178bcb0991SDimitry Andric /// http://www.cs.utexas.edu/users/mckinley/papers/asplos-1994.pdf
188bcb0991SDimitry Andric ///
198bcb0991SDimitry Andric /// The general approach taken to estimate the number of cache lines used by the
208bcb0991SDimitry Andric /// memory references in an inner loop is:
218bcb0991SDimitry Andric /// 1. Partition memory references that exhibit temporal or spacial reuse
228bcb0991SDimitry Andric /// into reference groups.
238bcb0991SDimitry Andric /// 2. For each loop L in the a loop nest LN:
248bcb0991SDimitry Andric /// a. Compute the cost of the reference group
258bcb0991SDimitry Andric /// b. Compute the loop cost by summing up the reference groups costs
268bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
278bcb0991SDimitry Andric
288bcb0991SDimitry Andric #include "llvm/Analysis/LoopCacheAnalysis.h"
298bcb0991SDimitry Andric #include "llvm/ADT/BreadthFirstIterator.h"
308bcb0991SDimitry Andric #include "llvm/ADT/Sequence.h"
318bcb0991SDimitry Andric #include "llvm/ADT/SmallVector.h"
32af732203SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
33af732203SDimitry Andric #include "llvm/Analysis/DependenceAnalysis.h"
34af732203SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
355ffd83dbSDimitry Andric #include "llvm/Analysis/ScalarEvolutionExpressions.h"
36af732203SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
37480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
388bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
398bcb0991SDimitry Andric
408bcb0991SDimitry Andric using namespace llvm;
418bcb0991SDimitry Andric
428bcb0991SDimitry Andric #define DEBUG_TYPE "loop-cache-cost"
438bcb0991SDimitry Andric
448bcb0991SDimitry Andric static cl::opt<unsigned> DefaultTripCount(
458bcb0991SDimitry Andric "default-trip-count", cl::init(100), cl::Hidden,
468bcb0991SDimitry Andric cl::desc("Use this to specify the default trip count of a loop"));
478bcb0991SDimitry Andric
488bcb0991SDimitry Andric // In this analysis two array references are considered to exhibit temporal
498bcb0991SDimitry Andric // reuse if they access either the same memory location, or a memory location
508bcb0991SDimitry Andric // with distance smaller than a configurable threshold.
518bcb0991SDimitry Andric static cl::opt<unsigned> TemporalReuseThreshold(
528bcb0991SDimitry Andric "temporal-reuse-threshold", cl::init(2), cl::Hidden,
538bcb0991SDimitry Andric cl::desc("Use this to specify the max. distance between array elements "
548bcb0991SDimitry Andric "accessed in a loop so that the elements are classified to have "
558bcb0991SDimitry Andric "temporal reuse"));
568bcb0991SDimitry Andric
578bcb0991SDimitry Andric /// Retrieve the innermost loop in the given loop nest \p Loops. It returns a
588bcb0991SDimitry Andric /// nullptr if any loops in the loop vector supplied has more than one sibling.
598bcb0991SDimitry Andric /// The loop vector is expected to contain loops collected in breadth-first
608bcb0991SDimitry Andric /// order.
getInnerMostLoop(const LoopVectorTy & Loops)618bcb0991SDimitry Andric static Loop *getInnerMostLoop(const LoopVectorTy &Loops) {
628bcb0991SDimitry Andric assert(!Loops.empty() && "Expecting a non-empy loop vector");
638bcb0991SDimitry Andric
648bcb0991SDimitry Andric Loop *LastLoop = Loops.back();
658bcb0991SDimitry Andric Loop *ParentLoop = LastLoop->getParentLoop();
668bcb0991SDimitry Andric
678bcb0991SDimitry Andric if (ParentLoop == nullptr) {
688bcb0991SDimitry Andric assert(Loops.size() == 1 && "Expecting a single loop");
698bcb0991SDimitry Andric return LastLoop;
708bcb0991SDimitry Andric }
718bcb0991SDimitry Andric
725ffd83dbSDimitry Andric return (llvm::is_sorted(Loops,
738bcb0991SDimitry Andric [](const Loop *L1, const Loop *L2) {
748bcb0991SDimitry Andric return L1->getLoopDepth() < L2->getLoopDepth();
758bcb0991SDimitry Andric }))
768bcb0991SDimitry Andric ? LastLoop
778bcb0991SDimitry Andric : nullptr;
788bcb0991SDimitry Andric }
798bcb0991SDimitry Andric
isOneDimensionalArray(const SCEV & AccessFn,const SCEV & ElemSize,const Loop & L,ScalarEvolution & SE)808bcb0991SDimitry Andric static bool isOneDimensionalArray(const SCEV &AccessFn, const SCEV &ElemSize,
818bcb0991SDimitry Andric const Loop &L, ScalarEvolution &SE) {
828bcb0991SDimitry Andric const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(&AccessFn);
838bcb0991SDimitry Andric if (!AR || !AR->isAffine())
848bcb0991SDimitry Andric return false;
858bcb0991SDimitry Andric
868bcb0991SDimitry Andric assert(AR->getLoop() && "AR should have a loop");
878bcb0991SDimitry Andric
888bcb0991SDimitry Andric // Check that start and increment are not add recurrences.
898bcb0991SDimitry Andric const SCEV *Start = AR->getStart();
908bcb0991SDimitry Andric const SCEV *Step = AR->getStepRecurrence(SE);
918bcb0991SDimitry Andric if (isa<SCEVAddRecExpr>(Start) || isa<SCEVAddRecExpr>(Step))
928bcb0991SDimitry Andric return false;
938bcb0991SDimitry Andric
948bcb0991SDimitry Andric // Check that start and increment are both invariant in the loop.
958bcb0991SDimitry Andric if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
968bcb0991SDimitry Andric return false;
978bcb0991SDimitry Andric
985ffd83dbSDimitry Andric const SCEV *StepRec = AR->getStepRecurrence(SE);
995ffd83dbSDimitry Andric if (StepRec && SE.isKnownNegative(StepRec))
1005ffd83dbSDimitry Andric StepRec = SE.getNegativeSCEV(StepRec);
1015ffd83dbSDimitry Andric
1025ffd83dbSDimitry Andric return StepRec == &ElemSize;
1038bcb0991SDimitry Andric }
1048bcb0991SDimitry Andric
1058bcb0991SDimitry Andric /// Compute the trip count for the given loop \p L. Return the SCEV expression
1068bcb0991SDimitry Andric /// for the trip count or nullptr if it cannot be computed.
computeTripCount(const Loop & L,ScalarEvolution & SE)1078bcb0991SDimitry Andric static const SCEV *computeTripCount(const Loop &L, ScalarEvolution &SE) {
1088bcb0991SDimitry Andric const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(&L);
1098bcb0991SDimitry Andric if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) ||
1108bcb0991SDimitry Andric !isa<SCEVConstant>(BackedgeTakenCount))
1118bcb0991SDimitry Andric return nullptr;
112*5f7ddb14SDimitry Andric return SE.getTripCountFromExitCount(BackedgeTakenCount);
1138bcb0991SDimitry Andric }
1148bcb0991SDimitry Andric
1158bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
1168bcb0991SDimitry Andric // IndexedReference implementation
1178bcb0991SDimitry Andric //
operator <<(raw_ostream & OS,const IndexedReference & R)1188bcb0991SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const IndexedReference &R) {
1198bcb0991SDimitry Andric if (!R.IsValid) {
1208bcb0991SDimitry Andric OS << R.StoreOrLoadInst;
1218bcb0991SDimitry Andric OS << ", IsValid=false.";
1228bcb0991SDimitry Andric return OS;
1238bcb0991SDimitry Andric }
1248bcb0991SDimitry Andric
1258bcb0991SDimitry Andric OS << *R.BasePointer;
1268bcb0991SDimitry Andric for (const SCEV *Subscript : R.Subscripts)
1278bcb0991SDimitry Andric OS << "[" << *Subscript << "]";
1288bcb0991SDimitry Andric
1298bcb0991SDimitry Andric OS << ", Sizes: ";
1308bcb0991SDimitry Andric for (const SCEV *Size : R.Sizes)
1318bcb0991SDimitry Andric OS << "[" << *Size << "]";
1328bcb0991SDimitry Andric
1338bcb0991SDimitry Andric return OS;
1348bcb0991SDimitry Andric }
1358bcb0991SDimitry Andric
IndexedReference(Instruction & StoreOrLoadInst,const LoopInfo & LI,ScalarEvolution & SE)1368bcb0991SDimitry Andric IndexedReference::IndexedReference(Instruction &StoreOrLoadInst,
1378bcb0991SDimitry Andric const LoopInfo &LI, ScalarEvolution &SE)
1388bcb0991SDimitry Andric : StoreOrLoadInst(StoreOrLoadInst), SE(SE) {
1398bcb0991SDimitry Andric assert((isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>(StoreOrLoadInst)) &&
1408bcb0991SDimitry Andric "Expecting a load or store instruction");
1418bcb0991SDimitry Andric
1428bcb0991SDimitry Andric IsValid = delinearize(LI);
1438bcb0991SDimitry Andric if (IsValid)
1448bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "Succesfully delinearized: " << *this
1458bcb0991SDimitry Andric << "\n");
1468bcb0991SDimitry Andric }
1478bcb0991SDimitry Andric
hasSpacialReuse(const IndexedReference & Other,unsigned CLS,AAResults & AA) const1488bcb0991SDimitry Andric Optional<bool> IndexedReference::hasSpacialReuse(const IndexedReference &Other,
1498bcb0991SDimitry Andric unsigned CLS,
150af732203SDimitry Andric AAResults &AA) const {
1518bcb0991SDimitry Andric assert(IsValid && "Expecting a valid reference");
1528bcb0991SDimitry Andric
1538bcb0991SDimitry Andric if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
1548bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
1558bcb0991SDimitry Andric << "No spacial reuse: different base pointers\n");
1568bcb0991SDimitry Andric return false;
1578bcb0991SDimitry Andric }
1588bcb0991SDimitry Andric
1598bcb0991SDimitry Andric unsigned NumSubscripts = getNumSubscripts();
1608bcb0991SDimitry Andric if (NumSubscripts != Other.getNumSubscripts()) {
1618bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
1628bcb0991SDimitry Andric << "No spacial reuse: different number of subscripts\n");
1638bcb0991SDimitry Andric return false;
1648bcb0991SDimitry Andric }
1658bcb0991SDimitry Andric
1668bcb0991SDimitry Andric // all subscripts must be equal, except the leftmost one (the last one).
1678bcb0991SDimitry Andric for (auto SubNum : seq<unsigned>(0, NumSubscripts - 1)) {
1688bcb0991SDimitry Andric if (getSubscript(SubNum) != Other.getSubscript(SubNum)) {
1698bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "No spacial reuse, different subscripts: "
1708bcb0991SDimitry Andric << "\n\t" << *getSubscript(SubNum) << "\n\t"
1718bcb0991SDimitry Andric << *Other.getSubscript(SubNum) << "\n");
1728bcb0991SDimitry Andric return false;
1738bcb0991SDimitry Andric }
1748bcb0991SDimitry Andric }
1758bcb0991SDimitry Andric
1768bcb0991SDimitry Andric // the difference between the last subscripts must be less than the cache line
1778bcb0991SDimitry Andric // size.
1788bcb0991SDimitry Andric const SCEV *LastSubscript = getLastSubscript();
1798bcb0991SDimitry Andric const SCEV *OtherLastSubscript = Other.getLastSubscript();
1808bcb0991SDimitry Andric const SCEVConstant *Diff = dyn_cast<SCEVConstant>(
1818bcb0991SDimitry Andric SE.getMinusSCEV(LastSubscript, OtherLastSubscript));
1828bcb0991SDimitry Andric
1838bcb0991SDimitry Andric if (Diff == nullptr) {
1848bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
1858bcb0991SDimitry Andric << "No spacial reuse, difference between subscript:\n\t"
1868bcb0991SDimitry Andric << *LastSubscript << "\n\t" << OtherLastSubscript
1878bcb0991SDimitry Andric << "\nis not constant.\n");
1888bcb0991SDimitry Andric return None;
1898bcb0991SDimitry Andric }
1908bcb0991SDimitry Andric
1918bcb0991SDimitry Andric bool InSameCacheLine = (Diff->getValue()->getSExtValue() < CLS);
1928bcb0991SDimitry Andric
1938bcb0991SDimitry Andric LLVM_DEBUG({
1948bcb0991SDimitry Andric if (InSameCacheLine)
1958bcb0991SDimitry Andric dbgs().indent(2) << "Found spacial reuse.\n";
1968bcb0991SDimitry Andric else
1978bcb0991SDimitry Andric dbgs().indent(2) << "No spacial reuse.\n";
1988bcb0991SDimitry Andric });
1998bcb0991SDimitry Andric
2008bcb0991SDimitry Andric return InSameCacheLine;
2018bcb0991SDimitry Andric }
2028bcb0991SDimitry Andric
hasTemporalReuse(const IndexedReference & Other,unsigned MaxDistance,const Loop & L,DependenceInfo & DI,AAResults & AA) const2038bcb0991SDimitry Andric Optional<bool> IndexedReference::hasTemporalReuse(const IndexedReference &Other,
2048bcb0991SDimitry Andric unsigned MaxDistance,
2058bcb0991SDimitry Andric const Loop &L,
2068bcb0991SDimitry Andric DependenceInfo &DI,
207af732203SDimitry Andric AAResults &AA) const {
2088bcb0991SDimitry Andric assert(IsValid && "Expecting a valid reference");
2098bcb0991SDimitry Andric
2108bcb0991SDimitry Andric if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
2118bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
2128bcb0991SDimitry Andric << "No temporal reuse: different base pointer\n");
2138bcb0991SDimitry Andric return false;
2148bcb0991SDimitry Andric }
2158bcb0991SDimitry Andric
2168bcb0991SDimitry Andric std::unique_ptr<Dependence> D =
2178bcb0991SDimitry Andric DI.depends(&StoreOrLoadInst, &Other.StoreOrLoadInst, true);
2188bcb0991SDimitry Andric
2198bcb0991SDimitry Andric if (D == nullptr) {
2208bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: no dependence\n");
2218bcb0991SDimitry Andric return false;
2228bcb0991SDimitry Andric }
2238bcb0991SDimitry Andric
2248bcb0991SDimitry Andric if (D->isLoopIndependent()) {
2258bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n");
2268bcb0991SDimitry Andric return true;
2278bcb0991SDimitry Andric }
2288bcb0991SDimitry Andric
2298bcb0991SDimitry Andric // Check the dependence distance at every loop level. There is temporal reuse
2308bcb0991SDimitry Andric // if the distance at the given loop's depth is small (|d| <= MaxDistance) and
2318bcb0991SDimitry Andric // it is zero at every other loop level.
2328bcb0991SDimitry Andric int LoopDepth = L.getLoopDepth();
2338bcb0991SDimitry Andric int Levels = D->getLevels();
2348bcb0991SDimitry Andric for (int Level = 1; Level <= Levels; ++Level) {
2358bcb0991SDimitry Andric const SCEV *Distance = D->getDistance(Level);
2368bcb0991SDimitry Andric const SCEVConstant *SCEVConst = dyn_cast_or_null<SCEVConstant>(Distance);
2378bcb0991SDimitry Andric
2388bcb0991SDimitry Andric if (SCEVConst == nullptr) {
2398bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: distance unknown\n");
2408bcb0991SDimitry Andric return None;
2418bcb0991SDimitry Andric }
2428bcb0991SDimitry Andric
2438bcb0991SDimitry Andric const ConstantInt &CI = *SCEVConst->getValue();
2448bcb0991SDimitry Andric if (Level != LoopDepth && !CI.isZero()) {
2458bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
2468bcb0991SDimitry Andric << "No temporal reuse: distance is not zero at depth=" << Level
2478bcb0991SDimitry Andric << "\n");
2488bcb0991SDimitry Andric return false;
2498bcb0991SDimitry Andric } else if (Level == LoopDepth && CI.getSExtValue() > MaxDistance) {
2508bcb0991SDimitry Andric LLVM_DEBUG(
2518bcb0991SDimitry Andric dbgs().indent(2)
2528bcb0991SDimitry Andric << "No temporal reuse: distance is greater than MaxDistance at depth="
2538bcb0991SDimitry Andric << Level << "\n");
2548bcb0991SDimitry Andric return false;
2558bcb0991SDimitry Andric }
2568bcb0991SDimitry Andric }
2578bcb0991SDimitry Andric
2588bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n");
2598bcb0991SDimitry Andric return true;
2608bcb0991SDimitry Andric }
2618bcb0991SDimitry Andric
computeRefCost(const Loop & L,unsigned CLS) const2628bcb0991SDimitry Andric CacheCostTy IndexedReference::computeRefCost(const Loop &L,
2638bcb0991SDimitry Andric unsigned CLS) const {
2648bcb0991SDimitry Andric assert(IsValid && "Expecting a valid reference");
2658bcb0991SDimitry Andric LLVM_DEBUG({
2668bcb0991SDimitry Andric dbgs().indent(2) << "Computing cache cost for:\n";
2678bcb0991SDimitry Andric dbgs().indent(4) << *this << "\n";
2688bcb0991SDimitry Andric });
2698bcb0991SDimitry Andric
2708bcb0991SDimitry Andric // If the indexed reference is loop invariant the cost is one.
2718bcb0991SDimitry Andric if (isLoopInvariant(L)) {
2728bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(4) << "Reference is loop invariant: RefCost=1\n");
2738bcb0991SDimitry Andric return 1;
2748bcb0991SDimitry Andric }
2758bcb0991SDimitry Andric
2768bcb0991SDimitry Andric const SCEV *TripCount = computeTripCount(L, SE);
2778bcb0991SDimitry Andric if (!TripCount) {
2788bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Trip count of loop " << L.getName()
2798bcb0991SDimitry Andric << " could not be computed, using DefaultTripCount\n");
2808bcb0991SDimitry Andric const SCEV *ElemSize = Sizes.back();
2818bcb0991SDimitry Andric TripCount = SE.getConstant(ElemSize->getType(), DefaultTripCount);
2828bcb0991SDimitry Andric }
2838bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "TripCount=" << *TripCount << "\n");
2848bcb0991SDimitry Andric
2858bcb0991SDimitry Andric // If the indexed reference is 'consecutive' the cost is
2868bcb0991SDimitry Andric // (TripCount*Stride)/CLS, otherwise the cost is TripCount.
2878bcb0991SDimitry Andric const SCEV *RefCost = TripCount;
2888bcb0991SDimitry Andric
2898bcb0991SDimitry Andric if (isConsecutive(L, CLS)) {
2908bcb0991SDimitry Andric const SCEV *Coeff = getLastCoefficient();
2918bcb0991SDimitry Andric const SCEV *ElemSize = Sizes.back();
2928bcb0991SDimitry Andric const SCEV *Stride = SE.getMulExpr(Coeff, ElemSize);
2938bcb0991SDimitry Andric const SCEV *CacheLineSize = SE.getConstant(Stride->getType(), CLS);
294480093f4SDimitry Andric Type *WiderType = SE.getWiderType(Stride->getType(), TripCount->getType());
2955ffd83dbSDimitry Andric if (SE.isKnownNegative(Stride))
2965ffd83dbSDimitry Andric Stride = SE.getNegativeSCEV(Stride);
2975ffd83dbSDimitry Andric Stride = SE.getNoopOrAnyExtend(Stride, WiderType);
298480093f4SDimitry Andric TripCount = SE.getNoopOrAnyExtend(TripCount, WiderType);
2998bcb0991SDimitry Andric const SCEV *Numerator = SE.getMulExpr(Stride, TripCount);
3008bcb0991SDimitry Andric RefCost = SE.getUDivExpr(Numerator, CacheLineSize);
3015ffd83dbSDimitry Andric
3028bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(4)
3038bcb0991SDimitry Andric << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="
3048bcb0991SDimitry Andric << *RefCost << "\n");
3058bcb0991SDimitry Andric } else
3068bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(4)
3078bcb0991SDimitry Andric << "Access is not consecutive: RefCost=TripCount=" << *RefCost
3088bcb0991SDimitry Andric << "\n");
3098bcb0991SDimitry Andric
3108bcb0991SDimitry Andric // Attempt to fold RefCost into a constant.
3118bcb0991SDimitry Andric if (auto ConstantCost = dyn_cast<SCEVConstant>(RefCost))
3128bcb0991SDimitry Andric return ConstantCost->getValue()->getSExtValue();
3138bcb0991SDimitry Andric
3148bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(4)
3158bcb0991SDimitry Andric << "RefCost is not a constant! Setting to RefCost=InvalidCost "
3168bcb0991SDimitry Andric "(invalid value).\n");
3178bcb0991SDimitry Andric
3188bcb0991SDimitry Andric return CacheCost::InvalidCost;
3198bcb0991SDimitry Andric }
3208bcb0991SDimitry Andric
delinearize(const LoopInfo & LI)3218bcb0991SDimitry Andric bool IndexedReference::delinearize(const LoopInfo &LI) {
3228bcb0991SDimitry Andric assert(Subscripts.empty() && "Subscripts should be empty");
3238bcb0991SDimitry Andric assert(Sizes.empty() && "Sizes should be empty");
3248bcb0991SDimitry Andric assert(!IsValid && "Should be called once from the constructor");
3258bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Delinearizing: " << StoreOrLoadInst << "\n");
3268bcb0991SDimitry Andric
3278bcb0991SDimitry Andric const SCEV *ElemSize = SE.getElementSize(&StoreOrLoadInst);
3288bcb0991SDimitry Andric const BasicBlock *BB = StoreOrLoadInst.getParent();
3298bcb0991SDimitry Andric
330480093f4SDimitry Andric if (Loop *L = LI.getLoopFor(BB)) {
3318bcb0991SDimitry Andric const SCEV *AccessFn =
3328bcb0991SDimitry Andric SE.getSCEVAtScope(getPointerOperand(&StoreOrLoadInst), L);
3338bcb0991SDimitry Andric
3348bcb0991SDimitry Andric BasePointer = dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFn));
3358bcb0991SDimitry Andric if (BasePointer == nullptr) {
3368bcb0991SDimitry Andric LLVM_DEBUG(
3378bcb0991SDimitry Andric dbgs().indent(2)
3388bcb0991SDimitry Andric << "ERROR: failed to delinearize, can't identify base pointer\n");
3398bcb0991SDimitry Andric return false;
3408bcb0991SDimitry Andric }
3418bcb0991SDimitry Andric
3428bcb0991SDimitry Andric AccessFn = SE.getMinusSCEV(AccessFn, BasePointer);
3438bcb0991SDimitry Andric
3448bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "In Loop '" << L->getName()
3458bcb0991SDimitry Andric << "', AccessFn: " << *AccessFn << "\n");
3468bcb0991SDimitry Andric
3478bcb0991SDimitry Andric SE.delinearize(AccessFn, Subscripts, Sizes,
3488bcb0991SDimitry Andric SE.getElementSize(&StoreOrLoadInst));
3498bcb0991SDimitry Andric
3508bcb0991SDimitry Andric if (Subscripts.empty() || Sizes.empty() ||
3518bcb0991SDimitry Andric Subscripts.size() != Sizes.size()) {
3528bcb0991SDimitry Andric // Attempt to determine whether we have a single dimensional array access.
3538bcb0991SDimitry Andric // before giving up.
3548bcb0991SDimitry Andric if (!isOneDimensionalArray(*AccessFn, *ElemSize, *L, SE)) {
3558bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2)
3568bcb0991SDimitry Andric << "ERROR: failed to delinearize reference\n");
3578bcb0991SDimitry Andric Subscripts.clear();
3588bcb0991SDimitry Andric Sizes.clear();
359480093f4SDimitry Andric return false;
3608bcb0991SDimitry Andric }
3618bcb0991SDimitry Andric
3625ffd83dbSDimitry Andric // The array may be accessed in reverse, for example:
3635ffd83dbSDimitry Andric // for (i = N; i > 0; i--)
3645ffd83dbSDimitry Andric // A[i] = 0;
3655ffd83dbSDimitry Andric // In this case, reconstruct the access function using the absolute value
3665ffd83dbSDimitry Andric // of the step recurrence.
3675ffd83dbSDimitry Andric const SCEVAddRecExpr *AccessFnAR = dyn_cast<SCEVAddRecExpr>(AccessFn);
3685ffd83dbSDimitry Andric const SCEV *StepRec = AccessFnAR ? AccessFnAR->getStepRecurrence(SE) : nullptr;
3695ffd83dbSDimitry Andric
3705ffd83dbSDimitry Andric if (StepRec && SE.isKnownNegative(StepRec))
3715ffd83dbSDimitry Andric AccessFn = SE.getAddRecExpr(AccessFnAR->getStart(),
3725ffd83dbSDimitry Andric SE.getNegativeSCEV(StepRec),
3735ffd83dbSDimitry Andric AccessFnAR->getLoop(),
3745ffd83dbSDimitry Andric AccessFnAR->getNoWrapFlags());
3758bcb0991SDimitry Andric const SCEV *Div = SE.getUDivExactExpr(AccessFn, ElemSize);
3768bcb0991SDimitry Andric Subscripts.push_back(Div);
3778bcb0991SDimitry Andric Sizes.push_back(ElemSize);
3788bcb0991SDimitry Andric }
3798bcb0991SDimitry Andric
3808bcb0991SDimitry Andric return all_of(Subscripts, [&](const SCEV *Subscript) {
3818bcb0991SDimitry Andric return isSimpleAddRecurrence(*Subscript, *L);
3828bcb0991SDimitry Andric });
3838bcb0991SDimitry Andric }
3848bcb0991SDimitry Andric
3858bcb0991SDimitry Andric return false;
3868bcb0991SDimitry Andric }
3878bcb0991SDimitry Andric
isLoopInvariant(const Loop & L) const3888bcb0991SDimitry Andric bool IndexedReference::isLoopInvariant(const Loop &L) const {
3898bcb0991SDimitry Andric Value *Addr = getPointerOperand(&StoreOrLoadInst);
3908bcb0991SDimitry Andric assert(Addr != nullptr && "Expecting either a load or a store instruction");
3918bcb0991SDimitry Andric assert(SE.isSCEVable(Addr->getType()) && "Addr should be SCEVable");
3928bcb0991SDimitry Andric
3938bcb0991SDimitry Andric if (SE.isLoopInvariant(SE.getSCEV(Addr), &L))
3948bcb0991SDimitry Andric return true;
3958bcb0991SDimitry Andric
3968bcb0991SDimitry Andric // The indexed reference is loop invariant if none of the coefficients use
3978bcb0991SDimitry Andric // the loop induction variable.
3988bcb0991SDimitry Andric bool allCoeffForLoopAreZero = all_of(Subscripts, [&](const SCEV *Subscript) {
3998bcb0991SDimitry Andric return isCoeffForLoopZeroOrInvariant(*Subscript, L);
4008bcb0991SDimitry Andric });
4018bcb0991SDimitry Andric
4028bcb0991SDimitry Andric return allCoeffForLoopAreZero;
4038bcb0991SDimitry Andric }
4048bcb0991SDimitry Andric
isConsecutive(const Loop & L,unsigned CLS) const4058bcb0991SDimitry Andric bool IndexedReference::isConsecutive(const Loop &L, unsigned CLS) const {
4068bcb0991SDimitry Andric // The indexed reference is 'consecutive' if the only coefficient that uses
4078bcb0991SDimitry Andric // the loop induction variable is the last one...
4088bcb0991SDimitry Andric const SCEV *LastSubscript = Subscripts.back();
4098bcb0991SDimitry Andric for (const SCEV *Subscript : Subscripts) {
4108bcb0991SDimitry Andric if (Subscript == LastSubscript)
4118bcb0991SDimitry Andric continue;
4128bcb0991SDimitry Andric if (!isCoeffForLoopZeroOrInvariant(*Subscript, L))
4138bcb0991SDimitry Andric return false;
4148bcb0991SDimitry Andric }
4158bcb0991SDimitry Andric
4168bcb0991SDimitry Andric // ...and the access stride is less than the cache line size.
4178bcb0991SDimitry Andric const SCEV *Coeff = getLastCoefficient();
4188bcb0991SDimitry Andric const SCEV *ElemSize = Sizes.back();
4198bcb0991SDimitry Andric const SCEV *Stride = SE.getMulExpr(Coeff, ElemSize);
4208bcb0991SDimitry Andric const SCEV *CacheLineSize = SE.getConstant(Stride->getType(), CLS);
4218bcb0991SDimitry Andric
4225ffd83dbSDimitry Andric Stride = SE.isKnownNegative(Stride) ? SE.getNegativeSCEV(Stride) : Stride;
4238bcb0991SDimitry Andric return SE.isKnownPredicate(ICmpInst::ICMP_ULT, Stride, CacheLineSize);
4248bcb0991SDimitry Andric }
4258bcb0991SDimitry Andric
getLastCoefficient() const4268bcb0991SDimitry Andric const SCEV *IndexedReference::getLastCoefficient() const {
4278bcb0991SDimitry Andric const SCEV *LastSubscript = getLastSubscript();
4288bcb0991SDimitry Andric assert(isa<SCEVAddRecExpr>(LastSubscript) &&
4298bcb0991SDimitry Andric "Expecting a SCEV add recurrence expression");
4308bcb0991SDimitry Andric const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LastSubscript);
4318bcb0991SDimitry Andric return AR->getStepRecurrence(SE);
4328bcb0991SDimitry Andric }
4338bcb0991SDimitry Andric
isCoeffForLoopZeroOrInvariant(const SCEV & Subscript,const Loop & L) const4348bcb0991SDimitry Andric bool IndexedReference::isCoeffForLoopZeroOrInvariant(const SCEV &Subscript,
4358bcb0991SDimitry Andric const Loop &L) const {
4368bcb0991SDimitry Andric const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(&Subscript);
4378bcb0991SDimitry Andric return (AR != nullptr) ? AR->getLoop() != &L
4388bcb0991SDimitry Andric : SE.isLoopInvariant(&Subscript, &L);
4398bcb0991SDimitry Andric }
4408bcb0991SDimitry Andric
isSimpleAddRecurrence(const SCEV & Subscript,const Loop & L) const4418bcb0991SDimitry Andric bool IndexedReference::isSimpleAddRecurrence(const SCEV &Subscript,
4428bcb0991SDimitry Andric const Loop &L) const {
4438bcb0991SDimitry Andric if (!isa<SCEVAddRecExpr>(Subscript))
4448bcb0991SDimitry Andric return false;
4458bcb0991SDimitry Andric
4468bcb0991SDimitry Andric const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(&Subscript);
4478bcb0991SDimitry Andric assert(AR->getLoop() && "AR should have a loop");
4488bcb0991SDimitry Andric
4498bcb0991SDimitry Andric if (!AR->isAffine())
4508bcb0991SDimitry Andric return false;
4518bcb0991SDimitry Andric
4528bcb0991SDimitry Andric const SCEV *Start = AR->getStart();
4538bcb0991SDimitry Andric const SCEV *Step = AR->getStepRecurrence(SE);
4548bcb0991SDimitry Andric
4558bcb0991SDimitry Andric if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
4568bcb0991SDimitry Andric return false;
4578bcb0991SDimitry Andric
4588bcb0991SDimitry Andric return true;
4598bcb0991SDimitry Andric }
4608bcb0991SDimitry Andric
isAliased(const IndexedReference & Other,AAResults & AA) const4618bcb0991SDimitry Andric bool IndexedReference::isAliased(const IndexedReference &Other,
462af732203SDimitry Andric AAResults &AA) const {
4638bcb0991SDimitry Andric const auto &Loc1 = MemoryLocation::get(&StoreOrLoadInst);
4648bcb0991SDimitry Andric const auto &Loc2 = MemoryLocation::get(&Other.StoreOrLoadInst);
4658bcb0991SDimitry Andric return AA.isMustAlias(Loc1, Loc2);
4668bcb0991SDimitry Andric }
4678bcb0991SDimitry Andric
4688bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
4698bcb0991SDimitry Andric // CacheCost implementation
4708bcb0991SDimitry Andric //
operator <<(raw_ostream & OS,const CacheCost & CC)4718bcb0991SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, const CacheCost &CC) {
4728bcb0991SDimitry Andric for (const auto &LC : CC.LoopCosts) {
4738bcb0991SDimitry Andric const Loop *L = LC.first;
4748bcb0991SDimitry Andric OS << "Loop '" << L->getName() << "' has cost = " << LC.second << "\n";
4758bcb0991SDimitry Andric }
4768bcb0991SDimitry Andric return OS;
4778bcb0991SDimitry Andric }
4788bcb0991SDimitry Andric
CacheCost(const LoopVectorTy & Loops,const LoopInfo & LI,ScalarEvolution & SE,TargetTransformInfo & TTI,AAResults & AA,DependenceInfo & DI,Optional<unsigned> TRT)4798bcb0991SDimitry Andric CacheCost::CacheCost(const LoopVectorTy &Loops, const LoopInfo &LI,
4808bcb0991SDimitry Andric ScalarEvolution &SE, TargetTransformInfo &TTI,
481af732203SDimitry Andric AAResults &AA, DependenceInfo &DI,
4828bcb0991SDimitry Andric Optional<unsigned> TRT)
4838bcb0991SDimitry Andric : Loops(Loops), TripCounts(), LoopCosts(),
484480093f4SDimitry Andric TRT((TRT == None) ? Optional<unsigned>(TemporalReuseThreshold) : TRT),
4858bcb0991SDimitry Andric LI(LI), SE(SE), TTI(TTI), AA(AA), DI(DI) {
4868bcb0991SDimitry Andric assert(!Loops.empty() && "Expecting a non-empty loop vector.");
4878bcb0991SDimitry Andric
4888bcb0991SDimitry Andric for (const Loop *L : Loops) {
4898bcb0991SDimitry Andric unsigned TripCount = SE.getSmallConstantTripCount(L);
4908bcb0991SDimitry Andric TripCount = (TripCount == 0) ? DefaultTripCount : TripCount;
4918bcb0991SDimitry Andric TripCounts.push_back({L, TripCount});
4928bcb0991SDimitry Andric }
4938bcb0991SDimitry Andric
4948bcb0991SDimitry Andric calculateCacheFootprint();
4958bcb0991SDimitry Andric }
4968bcb0991SDimitry Andric
4978bcb0991SDimitry Andric std::unique_ptr<CacheCost>
getCacheCost(Loop & Root,LoopStandardAnalysisResults & AR,DependenceInfo & DI,Optional<unsigned> TRT)4988bcb0991SDimitry Andric CacheCost::getCacheCost(Loop &Root, LoopStandardAnalysisResults &AR,
4998bcb0991SDimitry Andric DependenceInfo &DI, Optional<unsigned> TRT) {
500af732203SDimitry Andric if (!Root.isOutermost()) {
5018bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Expecting the outermost loop in a loop nest\n");
5028bcb0991SDimitry Andric return nullptr;
5038bcb0991SDimitry Andric }
5048bcb0991SDimitry Andric
5058bcb0991SDimitry Andric LoopVectorTy Loops;
506af732203SDimitry Andric append_range(Loops, breadth_first(&Root));
5078bcb0991SDimitry Andric
5088bcb0991SDimitry Andric if (!getInnerMostLoop(Loops)) {
5098bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Cannot compute cache cost of loop nest with more "
5108bcb0991SDimitry Andric "than one innermost loop\n");
5118bcb0991SDimitry Andric return nullptr;
5128bcb0991SDimitry Andric }
5138bcb0991SDimitry Andric
5148bcb0991SDimitry Andric return std::make_unique<CacheCost>(Loops, AR.LI, AR.SE, AR.TTI, AR.AA, DI, TRT);
5158bcb0991SDimitry Andric }
5168bcb0991SDimitry Andric
calculateCacheFootprint()5178bcb0991SDimitry Andric void CacheCost::calculateCacheFootprint() {
5188bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "POPULATING REFERENCE GROUPS\n");
5198bcb0991SDimitry Andric ReferenceGroupsTy RefGroups;
5208bcb0991SDimitry Andric if (!populateReferenceGroups(RefGroups))
5218bcb0991SDimitry Andric return;
5228bcb0991SDimitry Andric
5238bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "COMPUTING LOOP CACHE COSTS\n");
5248bcb0991SDimitry Andric for (const Loop *L : Loops) {
5258bcb0991SDimitry Andric assert((std::find_if(LoopCosts.begin(), LoopCosts.end(),
5268bcb0991SDimitry Andric [L](const LoopCacheCostTy &LCC) {
5278bcb0991SDimitry Andric return LCC.first == L;
5288bcb0991SDimitry Andric }) == LoopCosts.end()) &&
5298bcb0991SDimitry Andric "Should not add duplicate element");
5308bcb0991SDimitry Andric CacheCostTy LoopCost = computeLoopCacheCost(*L, RefGroups);
5318bcb0991SDimitry Andric LoopCosts.push_back(std::make_pair(L, LoopCost));
5328bcb0991SDimitry Andric }
5338bcb0991SDimitry Andric
5348bcb0991SDimitry Andric sortLoopCosts();
5358bcb0991SDimitry Andric RefGroups.clear();
5368bcb0991SDimitry Andric }
5378bcb0991SDimitry Andric
populateReferenceGroups(ReferenceGroupsTy & RefGroups) const5388bcb0991SDimitry Andric bool CacheCost::populateReferenceGroups(ReferenceGroupsTy &RefGroups) const {
5398bcb0991SDimitry Andric assert(RefGroups.empty() && "Reference groups should be empty");
5408bcb0991SDimitry Andric
5418bcb0991SDimitry Andric unsigned CLS = TTI.getCacheLineSize();
5428bcb0991SDimitry Andric Loop *InnerMostLoop = getInnerMostLoop(Loops);
5438bcb0991SDimitry Andric assert(InnerMostLoop != nullptr && "Expecting a valid innermost loop");
5448bcb0991SDimitry Andric
5458bcb0991SDimitry Andric for (BasicBlock *BB : InnerMostLoop->getBlocks()) {
5468bcb0991SDimitry Andric for (Instruction &I : *BB) {
5478bcb0991SDimitry Andric if (!isa<StoreInst>(I) && !isa<LoadInst>(I))
5488bcb0991SDimitry Andric continue;
5498bcb0991SDimitry Andric
5508bcb0991SDimitry Andric std::unique_ptr<IndexedReference> R(new IndexedReference(I, LI, SE));
5518bcb0991SDimitry Andric if (!R->isValid())
5528bcb0991SDimitry Andric continue;
5538bcb0991SDimitry Andric
5548bcb0991SDimitry Andric bool Added = false;
5558bcb0991SDimitry Andric for (ReferenceGroupTy &RefGroup : RefGroups) {
5568bcb0991SDimitry Andric const IndexedReference &Representative = *RefGroup.front().get();
5578bcb0991SDimitry Andric LLVM_DEBUG({
5588bcb0991SDimitry Andric dbgs() << "References:\n";
5598bcb0991SDimitry Andric dbgs().indent(2) << *R << "\n";
5608bcb0991SDimitry Andric dbgs().indent(2) << Representative << "\n";
5618bcb0991SDimitry Andric });
5628bcb0991SDimitry Andric
5635ffd83dbSDimitry Andric
5645ffd83dbSDimitry Andric // FIXME: Both positive and negative access functions will be placed
5655ffd83dbSDimitry Andric // into the same reference group, resulting in a bi-directional array
5665ffd83dbSDimitry Andric // access such as:
5675ffd83dbSDimitry Andric // for (i = N; i > 0; i--)
5685ffd83dbSDimitry Andric // A[i] = A[N - i];
5695ffd83dbSDimitry Andric // having the same cost calculation as a single dimention access pattern
5705ffd83dbSDimitry Andric // for (i = 0; i < N; i++)
5715ffd83dbSDimitry Andric // A[i] = A[i];
5725ffd83dbSDimitry Andric // when in actuality, depending on the array size, the first example
5735ffd83dbSDimitry Andric // should have a cost closer to 2x the second due to the two cache
5745ffd83dbSDimitry Andric // access per iteration from opposite ends of the array
5758bcb0991SDimitry Andric Optional<bool> HasTemporalReuse =
5768bcb0991SDimitry Andric R->hasTemporalReuse(Representative, *TRT, *InnerMostLoop, DI, AA);
5778bcb0991SDimitry Andric Optional<bool> HasSpacialReuse =
5788bcb0991SDimitry Andric R->hasSpacialReuse(Representative, CLS, AA);
5798bcb0991SDimitry Andric
5808bcb0991SDimitry Andric if ((HasTemporalReuse.hasValue() && *HasTemporalReuse) ||
5818bcb0991SDimitry Andric (HasSpacialReuse.hasValue() && *HasSpacialReuse)) {
5828bcb0991SDimitry Andric RefGroup.push_back(std::move(R));
5838bcb0991SDimitry Andric Added = true;
5848bcb0991SDimitry Andric break;
5858bcb0991SDimitry Andric }
5868bcb0991SDimitry Andric }
5878bcb0991SDimitry Andric
5888bcb0991SDimitry Andric if (!Added) {
5898bcb0991SDimitry Andric ReferenceGroupTy RG;
5908bcb0991SDimitry Andric RG.push_back(std::move(R));
5918bcb0991SDimitry Andric RefGroups.push_back(std::move(RG));
5928bcb0991SDimitry Andric }
5938bcb0991SDimitry Andric }
5948bcb0991SDimitry Andric }
5958bcb0991SDimitry Andric
5968bcb0991SDimitry Andric if (RefGroups.empty())
5978bcb0991SDimitry Andric return false;
5988bcb0991SDimitry Andric
5998bcb0991SDimitry Andric LLVM_DEBUG({
6008bcb0991SDimitry Andric dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n";
6018bcb0991SDimitry Andric int n = 1;
6028bcb0991SDimitry Andric for (const ReferenceGroupTy &RG : RefGroups) {
6038bcb0991SDimitry Andric dbgs().indent(2) << "RefGroup " << n << ":\n";
6048bcb0991SDimitry Andric for (const auto &IR : RG)
6058bcb0991SDimitry Andric dbgs().indent(4) << *IR << "\n";
6068bcb0991SDimitry Andric n++;
6078bcb0991SDimitry Andric }
6088bcb0991SDimitry Andric dbgs() << "\n";
6098bcb0991SDimitry Andric });
6108bcb0991SDimitry Andric
6118bcb0991SDimitry Andric return true;
6128bcb0991SDimitry Andric }
6138bcb0991SDimitry Andric
6148bcb0991SDimitry Andric CacheCostTy
computeLoopCacheCost(const Loop & L,const ReferenceGroupsTy & RefGroups) const6158bcb0991SDimitry Andric CacheCost::computeLoopCacheCost(const Loop &L,
6168bcb0991SDimitry Andric const ReferenceGroupsTy &RefGroups) const {
6178bcb0991SDimitry Andric if (!L.isLoopSimplifyForm())
6188bcb0991SDimitry Andric return InvalidCost;
6198bcb0991SDimitry Andric
6208bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Considering loop '" << L.getName()
6218bcb0991SDimitry Andric << "' as innermost loop.\n");
6228bcb0991SDimitry Andric
6238bcb0991SDimitry Andric // Compute the product of the trip counts of each other loop in the nest.
6248bcb0991SDimitry Andric CacheCostTy TripCountsProduct = 1;
6258bcb0991SDimitry Andric for (const auto &TC : TripCounts) {
6268bcb0991SDimitry Andric if (TC.first == &L)
6278bcb0991SDimitry Andric continue;
6288bcb0991SDimitry Andric TripCountsProduct *= TC.second;
6298bcb0991SDimitry Andric }
6308bcb0991SDimitry Andric
6318bcb0991SDimitry Andric CacheCostTy LoopCost = 0;
6328bcb0991SDimitry Andric for (const ReferenceGroupTy &RG : RefGroups) {
6338bcb0991SDimitry Andric CacheCostTy RefGroupCost = computeRefGroupCacheCost(RG, L);
6348bcb0991SDimitry Andric LoopCost += RefGroupCost * TripCountsProduct;
6358bcb0991SDimitry Andric }
6368bcb0991SDimitry Andric
6378bcb0991SDimitry Andric LLVM_DEBUG(dbgs().indent(2) << "Loop '" << L.getName()
6388bcb0991SDimitry Andric << "' has cost=" << LoopCost << "\n");
6398bcb0991SDimitry Andric
6408bcb0991SDimitry Andric return LoopCost;
6418bcb0991SDimitry Andric }
6428bcb0991SDimitry Andric
computeRefGroupCacheCost(const ReferenceGroupTy & RG,const Loop & L) const6438bcb0991SDimitry Andric CacheCostTy CacheCost::computeRefGroupCacheCost(const ReferenceGroupTy &RG,
6448bcb0991SDimitry Andric const Loop &L) const {
6458bcb0991SDimitry Andric assert(!RG.empty() && "Reference group should have at least one member.");
6468bcb0991SDimitry Andric
6478bcb0991SDimitry Andric const IndexedReference *Representative = RG.front().get();
6488bcb0991SDimitry Andric return Representative->computeRefCost(L, TTI.getCacheLineSize());
6498bcb0991SDimitry Andric }
6508bcb0991SDimitry Andric
6518bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
6528bcb0991SDimitry Andric // LoopCachePrinterPass implementation
6538bcb0991SDimitry Andric //
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater & U)6548bcb0991SDimitry Andric PreservedAnalyses LoopCachePrinterPass::run(Loop &L, LoopAnalysisManager &AM,
6558bcb0991SDimitry Andric LoopStandardAnalysisResults &AR,
6568bcb0991SDimitry Andric LPMUpdater &U) {
6578bcb0991SDimitry Andric Function *F = L.getHeader()->getParent();
6588bcb0991SDimitry Andric DependenceInfo DI(F, &AR.AA, &AR.SE, &AR.LI);
6598bcb0991SDimitry Andric
6608bcb0991SDimitry Andric if (auto CC = CacheCost::getCacheCost(L, AR, DI))
6618bcb0991SDimitry Andric OS << *CC;
6628bcb0991SDimitry Andric
6638bcb0991SDimitry Andric return PreservedAnalyses::all();
6648bcb0991SDimitry Andric }
665