Bug Summary

File:llvm/lib/Analysis/LoopCacheAnalysis.cpp
Warning:line 407, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name LoopCacheAnalysis.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Analysis -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp
1//===- LoopCacheAnalysis.cpp - Loop Cache Analysis -------------------------==//
2//
3// The LLVM Compiler Infrastructure
4//
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
8//
9//===----------------------------------------------------------------------===//
10///
11/// \file
12/// This file defines the implementation for the loop cache analysis.
13/// The implementation is largely based on the following paper:
14///
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
18///
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//===----------------------------------------------------------------------===//
27
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"
34
35using namespace llvm;
36
37#define DEBUG_TYPE"loop-cache-cost" "loop-cache-cost"
38
39static 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"));
42
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.
46static 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 "
50 "temporal reuse"));
51
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
55/// order.
56static Loop *getInnerMostLoop(const LoopVectorTy &Loops) {
57 assert(!Loops.empty() && "Expecting a non-empy loop vector")((!Loops.empty() && "Expecting a non-empy loop vector"
) ? static_cast<void> (0) : __assert_fail ("!Loops.empty() && \"Expecting a non-empy loop vector\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 57, __PRETTY_FUNCTION__))
;
58
59 Loop *LastLoop = Loops.back();
60 Loop *ParentLoop = LastLoop->getParentLoop();
61
62 if (ParentLoop == nullptr) {
63 assert(Loops.size() == 1 && "Expecting a single loop")((Loops.size() == 1 && "Expecting a single loop") ? static_cast
<void> (0) : __assert_fail ("Loops.size() == 1 && \"Expecting a single loop\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 63, __PRETTY_FUNCTION__))
;
64 return LastLoop;
65 }
66
67 return (std::is_sorted(Loops.begin(), Loops.end(),
68 [](const Loop *L1, const Loop *L2) {
69 return L1->getLoopDepth() < L2->getLoopDepth();
70 }))
71 ? LastLoop
72 : nullptr;
73}
74
75static 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())
79 return false;
80
81 assert(AR->getLoop() && "AR should have a loop")((AR->getLoop() && "AR should have a loop") ? static_cast
<void> (0) : __assert_fail ("AR->getLoop() && \"AR should have a loop\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 81, __PRETTY_FUNCTION__))
;
82
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))
87 return false;
88
89 // Check that start and increment are both invariant in the loop.
90 if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
91 return false;
92
93 return AR->getStepRecurrence(SE) == &ElemSize;
94}
95
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.
98static const SCEV *computeTripCount(const Loop &L, ScalarEvolution &SE) {
99 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(&L);
100 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) ||
101 !isa<SCEVConstant>(BackedgeTakenCount))
102 return nullptr;
103
104 return SE.getAddExpr(BackedgeTakenCount,
105 SE.getOne(BackedgeTakenCount->getType()));
106}
107
108//===----------------------------------------------------------------------===//
109// IndexedReference implementation
110//
111raw_ostream &llvm::operator<<(raw_ostream &OS, const IndexedReference &R) {
112 if (!R.IsValid) {
113 OS << R.StoreOrLoadInst;
114 OS << ", IsValid=false.";
115 return OS;
116 }
117
118 OS << *R.BasePointer;
119 for (const SCEV *Subscript : R.Subscripts)
120 OS << "[" << *Subscript << "]";
121
122 OS << ", Sizes: ";
123 for (const SCEV *Size : R.Sizes)
124 OS << "[" << *Size << "]";
125
126 return OS;
127}
128
129IndexedReference::IndexedReference(Instruction &StoreOrLoadInst,
130 const LoopInfo &LI, ScalarEvolution &SE)
131 : StoreOrLoadInst(StoreOrLoadInst), SE(SE) {
132 assert((isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>(StoreOrLoadInst)) &&(((isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>
(StoreOrLoadInst)) && "Expecting a load or store instruction"
) ? static_cast<void> (0) : __assert_fail ("(isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>(StoreOrLoadInst)) && \"Expecting a load or store instruction\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 133, __PRETTY_FUNCTION__))
133 "Expecting a load or store instruction")(((isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>
(StoreOrLoadInst)) && "Expecting a load or store instruction"
) ? static_cast<void> (0) : __assert_fail ("(isa<StoreInst>(StoreOrLoadInst) || isa<LoadInst>(StoreOrLoadInst)) && \"Expecting a load or store instruction\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 133, __PRETTY_FUNCTION__))
;
134
135 IsValid = delinearize(LI);
136 if (IsValid)
137 LLVM_DEBUG(dbgs().indent(2) << "Succesfully delinearized: " << *thisdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Succesfully delinearized: "
<< *this << "\n"; } } while (false)
138 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Succesfully delinearized: "
<< *this << "\n"; } } while (false)
;
139}
140
141Optional<bool> IndexedReference::hasSpacialReuse(const IndexedReference &Other,
142 unsigned CLS,
143 AliasAnalysis &AA) const {
144 assert(IsValid && "Expecting a valid reference")((IsValid && "Expecting a valid reference") ? static_cast
<void> (0) : __assert_fail ("IsValid && \"Expecting a valid reference\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 144, __PRETTY_FUNCTION__))
;
145
146 if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
147 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse: different base pointers\n"
; } } while (false)
148 << "No spacial reuse: different base pointers\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse: different base pointers\n"
; } } while (false)
;
149 return false;
150 }
151
152 unsigned NumSubscripts = getNumSubscripts();
153 if (NumSubscripts != Other.getNumSubscripts()) {
154 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse: different number of subscripts\n"
; } } while (false)
155 << "No spacial reuse: different number of subscripts\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse: different number of subscripts\n"
; } } while (false)
;
156 return false;
157 }
158
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: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, different subscripts: "
<< "\n\t" << *getSubscript(SubNum) << "\n\t"
<< *Other.getSubscript(SubNum) << "\n"; } } while
(false)
163 << "\n\t" << *getSubscript(SubNum) << "\n\t"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, different subscripts: "
<< "\n\t" << *getSubscript(SubNum) << "\n\t"
<< *Other.getSubscript(SubNum) << "\n"; } } while
(false)
164 << *Other.getSubscript(SubNum) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, different subscripts: "
<< "\n\t" << *getSubscript(SubNum) << "\n\t"
<< *Other.getSubscript(SubNum) << "\n"; } } while
(false)
;
165 return false;
166 }
167 }
168
169 // the difference between the last subscripts must be less than the cache line
170 // size.
171 const SCEV *LastSubscript = getLastSubscript();
172 const SCEV *OtherLastSubscript = Other.getLastSubscript();
173 const SCEVConstant *Diff = dyn_cast<SCEVConstant>(
174 SE.getMinusSCEV(LastSubscript, OtherLastSubscript));
175
176 if (Diff == nullptr) {
177 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, difference between subscript:\n\t"
<< *LastSubscript << "\n\t" << OtherLastSubscript
<< "\nis not constant.\n"; } } while (false)
178 << "No spacial reuse, difference between subscript:\n\t"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, difference between subscript:\n\t"
<< *LastSubscript << "\n\t" << OtherLastSubscript
<< "\nis not constant.\n"; } } while (false)
179 << *LastSubscript << "\n\t" << OtherLastSubscriptdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, difference between subscript:\n\t"
<< *LastSubscript << "\n\t" << OtherLastSubscript
<< "\nis not constant.\n"; } } while (false)
180 << "\nis not constant.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No spacial reuse, difference between subscript:\n\t"
<< *LastSubscript << "\n\t" << OtherLastSubscript
<< "\nis not constant.\n"; } } while (false)
;
181 return None;
182 }
183
184 bool InSameCacheLine = (Diff->getValue()->getSExtValue() < CLS);
185
186 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
187 if (InSameCacheLine)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
188 dbgs().indent(2) << "Found spacial reuse.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
189 elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
190 dbgs().indent(2) << "No spacial reuse.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
191 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { if (InSameCacheLine) dbgs().indent(2
) << "Found spacial reuse.\n"; else dbgs().indent(2) <<
"No spacial reuse.\n"; }; } } while (false)
;
192
193 return InSameCacheLine;
194}
195
196Optional<bool> IndexedReference::hasTemporalReuse(const IndexedReference &Other,
197 unsigned MaxDistance,
198 const Loop &L,
199 DependenceInfo &DI,
200 AliasAnalysis &AA) const {
201 assert(IsValid && "Expecting a valid reference")((IsValid && "Expecting a valid reference") ? static_cast
<void> (0) : __assert_fail ("IsValid && \"Expecting a valid reference\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 201, __PRETTY_FUNCTION__))
;
202
203 if (BasePointer != Other.getBasePointer() && !isAliased(Other, AA)) {
204 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: different base pointer\n"
; } } while (false)
205 << "No temporal reuse: different base pointer\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: different base pointer\n"
; } } while (false)
;
206 return false;
207 }
208
209 std::unique_ptr<Dependence> D =
210 DI.depends(&StoreOrLoadInst, &Other.StoreOrLoadInst, true);
211
212 if (D == nullptr) {
213 LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: no dependence\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: no dependence\n"
; } } while (false)
;
214 return false;
215 }
216
217 if (D->isLoopIndependent()) {
218 LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Found temporal reuse\n"
; } } while (false)
;
219 return true;
220 }
221
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);
230
231 if (SCEVConst == nullptr) {
232 LLVM_DEBUG(dbgs().indent(2) << "No temporal reuse: distance unknown\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance unknown\n"
; } } while (false)
;
233 return None;
234 }
235
236 const ConstantInt &CI = *SCEVConst->getValue();
237 if (Level != LoopDepth && !CI.isZero()) {
238 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is not zero at depth="
<< Level << "\n"; } } while (false)
239 << "No temporal reuse: distance is not zero at depth=" << Leveldo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is not zero at depth="
<< Level << "\n"; } } while (false)
240 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is not zero at depth="
<< Level << "\n"; } } while (false)
;
241 return false;
242 } else if (Level == LoopDepth && CI.getSExtValue() > MaxDistance) {
243 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is greater than MaxDistance at depth="
<< Level << "\n"; } } while (false)
244 dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is greater than MaxDistance at depth="
<< Level << "\n"; } } while (false)
245 << "No temporal reuse: distance is greater than MaxDistance at depth="do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is greater than MaxDistance at depth="
<< Level << "\n"; } } while (false)
246 << Level << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "No temporal reuse: distance is greater than MaxDistance at depth="
<< Level << "\n"; } } while (false)
;
247 return false;
248 }
249 }
250
251 LLVM_DEBUG(dbgs().indent(2) << "Found temporal reuse\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Found temporal reuse\n"
; } } while (false)
;
252 return true;
253}
254
255CacheCostTy IndexedReference::computeRefCost(const Loop &L,
256 unsigned CLS) const {
257 assert(IsValid && "Expecting a valid reference")((IsValid && "Expecting a valid reference") ? static_cast
<void> (0) : __assert_fail ("IsValid && \"Expecting a valid reference\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 257, __PRETTY_FUNCTION__))
;
3
Assuming field 'IsValid' is true
4
'?' condition is true
258 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs().indent(2) << "Computing cache cost for:\n"
; dbgs().indent(4) << *this << "\n"; }; } } while
(false)
5
Assuming 'DebugFlag' is false
6
Loop condition is false. Exiting loop
259 dbgs().indent(2) << "Computing cache cost for:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs().indent(2) << "Computing cache cost for:\n"
; dbgs().indent(4) << *this << "\n"; }; } } while
(false)
260 dbgs().indent(4) << *this << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs().indent(2) << "Computing cache cost for:\n"
; dbgs().indent(4) << *this << "\n"; }; } } while
(false)
261 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs().indent(2) << "Computing cache cost for:\n"
; dbgs().indent(4) << *this << "\n"; }; } } while
(false)
;
262
263 // If the indexed reference is loop invariant the cost is one.
264 if (isLoopInvariant(L)) {
7
Taking false branch
265 LLVM_DEBUG(dbgs().indent(4) << "Reference is loop invariant: RefCost=1\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Reference is loop invariant: RefCost=1\n"
; } } while (false)
;
266 return 1;
267 }
268
269 const SCEV *TripCount = computeTripCount(L, SE);
270 if (!TripCount
7.1
'TripCount' is null
) {
8
Taking true branch
271 LLVM_DEBUG(dbgs() << "Trip count of loop " << L.getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Trip count of loop " <<
L.getName() << " could not be computed, using DefaultTripCount\n"
; } } while (false)
9
Assuming 'DebugFlag' is false
10
Loop condition is false. Exiting loop
272 << " could not be computed, using DefaultTripCount\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Trip count of loop " <<
L.getName() << " could not be computed, using DefaultTripCount\n"
; } } while (false)
;
273 const SCEV *ElemSize = Sizes.back();
274 TripCount = SE.getConstant(ElemSize->getType(), DefaultTripCount);
275 }
276 LLVM_DEBUG(dbgs() << "TripCount=" << *TripCount << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "TripCount=" << *
TripCount << "\n"; } } while (false)
;
11
Assuming 'DebugFlag' is false
12
Loop condition is false. Exiting loop
277
278 // If the indexed reference is 'consecutive' the cost is
279 // (TripCount*Stride)/CLS, otherwise the cost is TripCount.
280 const SCEV *RefCost = TripCount;
281
282 if (isConsecutive(L, CLS)) {
13
Assuming the condition is true
14
Taking true branch
283 const SCEV *Coeff = getLastCoefficient();
15
Calling 'IndexedReference::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)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="
<< *RefCost << "\n"; } } while (false)
293 << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="
<< *RefCost << "\n"; } } while (false)
294 << *RefCost << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is consecutive: RefCost=(TripCount*Stride)/CLS="
<< *RefCost << "\n"; } } while (false)
;
295 } else
296 LLVM_DEBUG(dbgs().indent(4)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is not consecutive: RefCost=TripCount="
<< *RefCost << "\n"; } } while (false)
297 << "Access is not consecutive: RefCost=TripCount=" << *RefCostdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is not consecutive: RefCost=TripCount="
<< *RefCost << "\n"; } } while (false)
298 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "Access is not consecutive: RefCost=TripCount="
<< *RefCost << "\n"; } } while (false)
;
299
300 // Attempt to fold RefCost into a constant.
301 if (auto ConstantCost = dyn_cast<SCEVConstant>(RefCost))
302 return ConstantCost->getValue()->getSExtValue();
303
304 LLVM_DEBUG(dbgs().indent(4)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "RefCost is not a constant! Setting to RefCost=InvalidCost "
"(invalid value).\n"; } } while (false)
305 << "RefCost is not a constant! Setting to RefCost=InvalidCost "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "RefCost is not a constant! Setting to RefCost=InvalidCost "
"(invalid value).\n"; } } while (false)
306 "(invalid value).\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(4) << "RefCost is not a constant! Setting to RefCost=InvalidCost "
"(invalid value).\n"; } } while (false)
;
307
308 return CacheCost::InvalidCost;
309}
310
311bool IndexedReference::delinearize(const LoopInfo &LI) {
312 assert(Subscripts.empty() && "Subscripts should be empty")((Subscripts.empty() && "Subscripts should be empty")
? static_cast<void> (0) : __assert_fail ("Subscripts.empty() && \"Subscripts should be empty\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 312, __PRETTY_FUNCTION__))
;
313 assert(Sizes.empty() && "Sizes should be empty")((Sizes.empty() && "Sizes should be empty") ? static_cast
<void> (0) : __assert_fail ("Sizes.empty() && \"Sizes should be empty\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 313, __PRETTY_FUNCTION__))
;
314 assert(!IsValid && "Should be called once from the constructor")((!IsValid && "Should be called once from the constructor"
) ? static_cast<void> (0) : __assert_fail ("!IsValid && \"Should be called once from the constructor\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 314, __PRETTY_FUNCTION__))
;
315 LLVM_DEBUG(dbgs() << "Delinearizing: " << StoreOrLoadInst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Delinearizing: " <<
StoreOrLoadInst << "\n"; } } while (false)
;
316
317 const SCEV *ElemSize = SE.getElementSize(&StoreOrLoadInst);
318 const BasicBlock *BB = StoreOrLoadInst.getParent();
319
320 if (Loop *L = LI.getLoopFor(BB)) {
321 const SCEV *AccessFn =
322 SE.getSCEVAtScope(getPointerOperand(&StoreOrLoadInst), L);
323
324 BasePointer = dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFn));
325 if (BasePointer == nullptr) {
326 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "ERROR: failed to delinearize, can't identify base pointer\n"
; } } while (false)
327 dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "ERROR: failed to delinearize, can't identify base pointer\n"
; } } while (false)
328 << "ERROR: failed to delinearize, can't identify base pointer\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "ERROR: failed to delinearize, can't identify base pointer\n"
; } } while (false)
;
329 return false;
330 }
331
332 AccessFn = SE.getMinusSCEV(AccessFn, BasePointer);
333
334 LLVM_DEBUG(dbgs().indent(2) << "In Loop '" << L->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "In Loop '" <<
L->getName() << "', AccessFn: " << *AccessFn <<
"\n"; } } while (false)
335 << "', AccessFn: " << *AccessFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "In Loop '" <<
L->getName() << "', AccessFn: " << *AccessFn <<
"\n"; } } while (false)
;
336
337 SE.delinearize(AccessFn, Subscripts, Sizes,
338 SE.getElementSize(&StoreOrLoadInst));
339
340 if (Subscripts.empty() || Sizes.empty() ||
341 Subscripts.size() != Sizes.size()) {
342 // Attempt to determine whether we have a single dimensional array access.
343 // before giving up.
344 if (!isOneDimensionalArray(*AccessFn, *ElemSize, *L, SE)) {
345 LLVM_DEBUG(dbgs().indent(2)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "ERROR: failed to delinearize reference\n"
; } } while (false)
346 << "ERROR: failed to delinearize reference\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "ERROR: failed to delinearize reference\n"
; } } while (false)
;
347 Subscripts.clear();
348 Sizes.clear();
349 return false;
350 }
351
352 const SCEV *Div = SE.getUDivExactExpr(AccessFn, ElemSize);
353 Subscripts.push_back(Div);
354 Sizes.push_back(ElemSize);
355 }
356
357 return all_of(Subscripts, [&](const SCEV *Subscript) {
358 return isSimpleAddRecurrence(*Subscript, *L);
359 });
360 }
361
362 return false;
363}
364
365bool IndexedReference::isLoopInvariant(const Loop &L) const {
366 Value *Addr = getPointerOperand(&StoreOrLoadInst);
367 assert(Addr != nullptr && "Expecting either a load or a store instruction")((Addr != nullptr && "Expecting either a load or a store instruction"
) ? static_cast<void> (0) : __assert_fail ("Addr != nullptr && \"Expecting either a load or a store instruction\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 367, __PRETTY_FUNCTION__))
;
368 assert(SE.isSCEVable(Addr->getType()) && "Addr should be SCEVable")((SE.isSCEVable(Addr->getType()) && "Addr should be SCEVable"
) ? static_cast<void> (0) : __assert_fail ("SE.isSCEVable(Addr->getType()) && \"Addr should be SCEVable\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 368, __PRETTY_FUNCTION__))
;
369
370 if (SE.isLoopInvariant(SE.getSCEV(Addr), &L))
371 return true;
372
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);
377 });
378
379 return allCoeffForLoopAreZero;
380}
381
382bool 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)
388 continue;
389 if (!isCoeffForLoopZeroOrInvariant(*Subscript, L))
390 return false;
391 }
392
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);
398
399 return SE.isKnownPredicate(ICmpInst::ICMP_ULT, Stride, CacheLineSize);
400}
401
402const SCEV *IndexedReference::getLastCoefficient() const {
403 const SCEV *LastSubscript = getLastSubscript();
404 assert(isa<SCEVAddRecExpr>(LastSubscript) &&((isa<SCEVAddRecExpr>(LastSubscript) && "Expecting a SCEV add recurrence expression"
) ? static_cast<void> (0) : __assert_fail ("isa<SCEVAddRecExpr>(LastSubscript) && \"Expecting a SCEV add recurrence expression\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 405, __PRETTY_FUNCTION__))
16
Assuming 'LastSubscript' is a 'SCEVAddRecExpr'
17
'?' condition is true
405 "Expecting a SCEV add recurrence expression")((isa<SCEVAddRecExpr>(LastSubscript) && "Expecting a SCEV add recurrence expression"
) ? static_cast<void> (0) : __assert_fail ("isa<SCEVAddRecExpr>(LastSubscript) && \"Expecting a SCEV add recurrence expression\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 405, __PRETTY_FUNCTION__))
;
406 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LastSubscript);
18
Assuming 'LastSubscript' is not a 'SCEVAddRecExpr'
19
'AR' initialized to a null pointer value
407 return AR->getStepRecurrence(SE);
20
Called C++ object pointer is null
408}
409
410bool 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);
415}
416
417bool IndexedReference::isSimpleAddRecurrence(const SCEV &Subscript,
418 const Loop &L) const {
419 if (!isa<SCEVAddRecExpr>(Subscript))
420 return false;
421
422 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(&Subscript);
423 assert(AR->getLoop() && "AR should have a loop")((AR->getLoop() && "AR should have a loop") ? static_cast
<void> (0) : __assert_fail ("AR->getLoop() && \"AR should have a loop\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 423, __PRETTY_FUNCTION__))
;
424
425 if (!AR->isAffine())
426 return false;
427
428 const SCEV *Start = AR->getStart();
429 const SCEV *Step = AR->getStepRecurrence(SE);
430
431 if (!SE.isLoopInvariant(Start, &L) || !SE.isLoopInvariant(Step, &L))
432 return false;
433
434 return true;
435}
436
437bool 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);
442}
443
444//===----------------------------------------------------------------------===//
445// CacheCost implementation
446//
447raw_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";
451 }
452 return OS;
453}
454
455CacheCost::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.")((!Loops.empty() && "Expecting a non-empty loop vector."
) ? static_cast<void> (0) : __assert_fail ("!Loops.empty() && \"Expecting a non-empty loop vector.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 462, __PRETTY_FUNCTION__))
;
463
464 for (const Loop *L : Loops) {
465 unsigned TripCount = SE.getSmallConstantTripCount(L);
466 TripCount = (TripCount == 0) ? DefaultTripCount : TripCount;
467 TripCounts.push_back({L, TripCount});
468 }
469
470 calculateCacheFootprint();
471}
472
473std::unique_ptr<CacheCost>
474CacheCost::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")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Expecting the outermost loop in a loop nest\n"
; } } while (false)
;
478 return nullptr;
479 }
480
481 LoopVectorTy Loops;
482 for (Loop *L : breadth_first(&Root))
483 Loops.push_back(L);
484
485 if (!getInnerMostLoop(Loops)) {
486 LLVM_DEBUG(dbgs() << "Cannot compute cache cost of loop nest with more "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Cannot compute cache cost of loop nest with more "
"than one innermost loop\n"; } } while (false)
487 "than one innermost loop\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Cannot compute cache cost of loop nest with more "
"than one innermost loop\n"; } } while (false)
;
488 return nullptr;
489 }
490
491 return std::make_unique<CacheCost>(Loops, AR.LI, AR.SE, AR.TTI, AR.AA, DI, TRT);
492}
493
494void CacheCost::calculateCacheFootprint() {
495 LLVM_DEBUG(dbgs() << "POPULATING REFERENCE GROUPS\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "POPULATING REFERENCE GROUPS\n"
; } } while (false)
;
496 ReferenceGroupsTy RefGroups;
497 if (!populateReferenceGroups(RefGroups))
498 return;
499
500 LLVM_DEBUG(dbgs() << "COMPUTING LOOP CACHE COSTS\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "COMPUTING LOOP CACHE COSTS\n"
; } } while (false)
;
501 for (const Loop *L : Loops) {
502 assert((std::find_if(LoopCosts.begin(), LoopCosts.end(),(((std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const
LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts
.end()) && "Should not add duplicate element") ? static_cast
<void> (0) : __assert_fail ("(std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts.end()) && \"Should not add duplicate element\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 506, __PRETTY_FUNCTION__))
503 [L](const LoopCacheCostTy &LCC) {(((std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const
LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts
.end()) && "Should not add duplicate element") ? static_cast
<void> (0) : __assert_fail ("(std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts.end()) && \"Should not add duplicate element\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 506, __PRETTY_FUNCTION__))
504 return LCC.first == L;(((std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const
LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts
.end()) && "Should not add duplicate element") ? static_cast
<void> (0) : __assert_fail ("(std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts.end()) && \"Should not add duplicate element\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 506, __PRETTY_FUNCTION__))
505 }) == LoopCosts.end()) &&(((std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const
LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts
.end()) && "Should not add duplicate element") ? static_cast
<void> (0) : __assert_fail ("(std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts.end()) && \"Should not add duplicate element\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 506, __PRETTY_FUNCTION__))
506 "Should not add duplicate element")(((std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const
LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts
.end()) && "Should not add duplicate element") ? static_cast
<void> (0) : __assert_fail ("(std::find_if(LoopCosts.begin(), LoopCosts.end(), [L](const LoopCacheCostTy &LCC) { return LCC.first == L; }) == LoopCosts.end()) && \"Should not add duplicate element\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 506, __PRETTY_FUNCTION__))
;
507 CacheCostTy LoopCost = computeLoopCacheCost(*L, RefGroups);
508 LoopCosts.push_back(std::make_pair(L, LoopCost));
509 }
510
511 sortLoopCosts();
512 RefGroups.clear();
513}
514
515bool CacheCost::populateReferenceGroups(ReferenceGroupsTy &RefGroups) const {
516 assert(RefGroups.empty() && "Reference groups should be empty")((RefGroups.empty() && "Reference groups should be empty"
) ? static_cast<void> (0) : __assert_fail ("RefGroups.empty() && \"Reference groups should be empty\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 516, __PRETTY_FUNCTION__))
;
517
518 unsigned CLS = TTI.getCacheLineSize();
519 Loop *InnerMostLoop = getInnerMostLoop(Loops);
520 assert(InnerMostLoop != nullptr && "Expecting a valid innermost loop")((InnerMostLoop != nullptr && "Expecting a valid innermost loop"
) ? static_cast<void> (0) : __assert_fail ("InnerMostLoop != nullptr && \"Expecting a valid innermost loop\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 520, __PRETTY_FUNCTION__))
;
521
522 for (BasicBlock *BB : InnerMostLoop->getBlocks()) {
523 for (Instruction &I : *BB) {
524 if (!isa<StoreInst>(I) && !isa<LoadInst>(I))
525 continue;
526
527 std::unique_ptr<IndexedReference> R(new IndexedReference(I, LI, SE));
528 if (!R->isValid())
529 continue;
530
531 bool Added = false;
532 for (ReferenceGroupTy &RefGroup : RefGroups) {
533 const IndexedReference &Representative = *RefGroup.front().get();
534 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "References:\n"; dbgs
().indent(2) << *R << "\n"; dbgs().indent(2) <<
Representative << "\n"; }; } } while (false)
535 dbgs() << "References:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "References:\n"; dbgs
().indent(2) << *R << "\n"; dbgs().indent(2) <<
Representative << "\n"; }; } } while (false)
536 dbgs().indent(2) << *R << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "References:\n"; dbgs
().indent(2) << *R << "\n"; dbgs().indent(2) <<
Representative << "\n"; }; } } while (false)
537 dbgs().indent(2) << Representative << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "References:\n"; dbgs
().indent(2) << *R << "\n"; dbgs().indent(2) <<
Representative << "\n"; }; } } while (false)
538 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "References:\n"; dbgs
().indent(2) << *R << "\n"; dbgs().indent(2) <<
Representative << "\n"; }; } } while (false)
;
539
540 Optional<bool> HasTemporalReuse =
541 R->hasTemporalReuse(Representative, *TRT, *InnerMostLoop, DI, AA);
542 Optional<bool> HasSpacialReuse =
543 R->hasSpacialReuse(Representative, CLS, AA);
544
545 if ((HasTemporalReuse.hasValue() && *HasTemporalReuse) ||
546 (HasSpacialReuse.hasValue() && *HasSpacialReuse)) {
547 RefGroup.push_back(std::move(R));
548 Added = true;
549 break;
550 }
551 }
552
553 if (!Added) {
554 ReferenceGroupTy RG;
555 RG.push_back(std::move(R));
556 RefGroups.push_back(std::move(RG));
557 }
558 }
559 }
560
561 if (RefGroups.empty())
562 return false;
563
564 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
565 dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
566 int n = 1;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
567 for (const ReferenceGroupTy &RG : RefGroups) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
568 dbgs().indent(2) << "RefGroup " << n << ":\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
569 for (const auto &IR : RG)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
570 dbgs().indent(4) << *IR << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
571 n++;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
572 }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
573 dbgs() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
574 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { { dbgs() << "\nIDENTIFIED REFERENCE GROUPS:\n"
; int n = 1; for (const ReferenceGroupTy &RG : RefGroups)
{ dbgs().indent(2) << "RefGroup " << n << ":\n"
; for (const auto &IR : RG) dbgs().indent(4) << *IR
<< "\n"; n++; } dbgs() << "\n"; }; } } while (false
)
;
575
576 return true;
577}
578
579CacheCostTy
580CacheCost::computeLoopCacheCost(const Loop &L,
581 const ReferenceGroupsTy &RefGroups) const {
582 if (!L.isLoopSimplifyForm())
583 return InvalidCost;
584
585 LLVM_DEBUG(dbgs() << "Considering loop '" << L.getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Considering loop '" <<
L.getName() << "' as innermost loop.\n"; } } while (false
)
586 << "' as innermost loop.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs() << "Considering loop '" <<
L.getName() << "' as innermost loop.\n"; } } while (false
)
;
587
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) {
591 if (TC.first == &L)
592 continue;
593 TripCountsProduct *= TC.second;
594 }
595
596 CacheCostTy LoopCost = 0;
597 for (const ReferenceGroupTy &RG : RefGroups) {
598 CacheCostTy RefGroupCost = computeRefGroupCacheCost(RG, L);
599 LoopCost += RefGroupCost * TripCountsProduct;
600 }
601
602 LLVM_DEBUG(dbgs().indent(2) << "Loop '" << L.getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Loop '" <<
L.getName() << "' has cost=" << LoopCost <<
"\n"; } } while (false)
603 << "' has cost=" << LoopCost << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-cache-cost")) { dbgs().indent(2) << "Loop '" <<
L.getName() << "' has cost=" << LoopCost <<
"\n"; } } while (false)
;
604
605 return LoopCost;
606}
607
608CacheCostTy CacheCost::computeRefGroupCacheCost(const ReferenceGroupTy &RG,
609 const Loop &L) const {
610 assert(!RG.empty() && "Reference group should have at least one member.")((!RG.empty() && "Reference group should have at least one member."
) ? static_cast<void> (0) : __assert_fail ("!RG.empty() && \"Reference group should have at least one member.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Analysis/LoopCacheAnalysis.cpp"
, 610, __PRETTY_FUNCTION__))
;
1
'?' condition is true
611
612 const IndexedReference *Representative = RG.front().get();
613 return Representative->computeRefCost(L, TTI.getCacheLineSize());
2
Calling 'IndexedReference::computeRefCost'
614}
615
616//===----------------------------------------------------------------------===//
617// LoopCachePrinterPass implementation
618//
619PreservedAnalyses LoopCachePrinterPass::run(Loop &L, LoopAnalysisManager &AM,
620 LoopStandardAnalysisResults &AR,
621 LPMUpdater &U) {
622 Function *F = L.getHeader()->getParent();
623 DependenceInfo DI(F, &AR.AA, &AR.SE, &AR.LI);
624
625 if (auto CC = CacheCost::getCacheCost(L, AR, DI))
626 OS << *CC;
627
628 return PreservedAnalyses::all();
629}