blob: becbae4c5b50eee21fbaf70c3e3a083d38594e60 [file] [log] [blame]
Adam Nemet04563272015-02-01 16:56:15 +00001//===- LoopAccessAnalysis.cpp - Loop Access Analysis Implementation --------==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// The implementation for the loop memory dependence that was originally
11// developed for the loop vectorizer.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LoopAccessAnalysis.h"
16#include "llvm/Analysis/LoopInfo.h"
Adam Nemet7206d7a2015-02-06 18:31:04 +000017#include "llvm/Analysis/ScalarEvolutionExpander.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Adam Nemet04563272015-02-01 16:56:15 +000019#include "llvm/Analysis/ValueTracking.h"
20#include "llvm/IR/DiagnosticInfo.h"
21#include "llvm/IR/Dominators.h"
Adam Nemet7206d7a2015-02-06 18:31:04 +000022#include "llvm/IR/IRBuilder.h"
Adam Nemet04563272015-02-01 16:56:15 +000023#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000024#include "llvm/Support/raw_ostream.h"
David Blaikieb447ac62015-06-26 18:02:52 +000025#include "llvm/Analysis/VectorUtils.h"
Adam Nemet04563272015-02-01 16:56:15 +000026using namespace llvm;
27
Adam Nemet339f42b2015-02-19 19:15:07 +000028#define DEBUG_TYPE "loop-accesses"
Adam Nemet04563272015-02-01 16:56:15 +000029
Adam Nemetf219c642015-02-19 19:14:52 +000030static cl::opt<unsigned, true>
31VectorizationFactor("force-vector-width", cl::Hidden,
32 cl::desc("Sets the SIMD width. Zero is autoselect."),
33 cl::location(VectorizerParams::VectorizationFactor));
Adam Nemet1d862af2015-02-26 04:39:09 +000034unsigned VectorizerParams::VectorizationFactor;
Adam Nemetf219c642015-02-19 19:14:52 +000035
36static cl::opt<unsigned, true>
37VectorizationInterleave("force-vector-interleave", cl::Hidden,
38 cl::desc("Sets the vectorization interleave count. "
39 "Zero is autoselect."),
40 cl::location(
41 VectorizerParams::VectorizationInterleave));
Adam Nemet1d862af2015-02-26 04:39:09 +000042unsigned VectorizerParams::VectorizationInterleave;
Adam Nemetf219c642015-02-19 19:14:52 +000043
Adam Nemet1d862af2015-02-26 04:39:09 +000044static cl::opt<unsigned, true> RuntimeMemoryCheckThreshold(
45 "runtime-memory-check-threshold", cl::Hidden,
46 cl::desc("When performing memory disambiguation checks at runtime do not "
47 "generate more than this number of comparisons (default = 8)."),
48 cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8));
49unsigned VectorizerParams::RuntimeMemoryCheckThreshold;
Adam Nemetf219c642015-02-19 19:14:52 +000050
Silviu Baranga1b6b50a2015-07-08 09:16:33 +000051/// \brief The maximum iterations used to merge memory checks
52static cl::opt<unsigned> MemoryCheckMergeThreshold(
53 "memory-check-merge-threshold", cl::Hidden,
54 cl::desc("Maximum number of comparisons done when trying to merge "
55 "runtime memory checks. (default = 100)"),
56 cl::init(100));
57
Adam Nemetf219c642015-02-19 19:14:52 +000058/// Maximum SIMD width.
59const unsigned VectorizerParams::MaxVectorWidth = 64;
60
Adam Nemet9c926572015-03-10 17:40:37 +000061/// \brief We collect interesting dependences up to this threshold.
62static cl::opt<unsigned> MaxInterestingDependence(
63 "max-interesting-dependences", cl::Hidden,
64 cl::desc("Maximum number of interesting dependences collected by "
65 "loop-access analysis (default = 100)"),
66 cl::init(100));
67
Adam Nemetf219c642015-02-19 19:14:52 +000068bool VectorizerParams::isInterleaveForced() {
69 return ::VectorizationInterleave.getNumOccurrences() > 0;
70}
71
Adam Nemet2bd6e982015-02-19 19:15:15 +000072void LoopAccessReport::emitAnalysis(const LoopAccessReport &Message,
73 const Function *TheFunction,
74 const Loop *TheLoop,
75 const char *PassName) {
Adam Nemet04563272015-02-01 16:56:15 +000076 DebugLoc DL = TheLoop->getStartLoc();
Adam Nemet3e876342015-02-19 19:15:13 +000077 if (const Instruction *I = Message.getInstr())
Adam Nemet04563272015-02-01 16:56:15 +000078 DL = I->getDebugLoc();
Adam Nemet339f42b2015-02-19 19:15:07 +000079 emitOptimizationRemarkAnalysis(TheFunction->getContext(), PassName,
Adam Nemet04563272015-02-01 16:56:15 +000080 *TheFunction, DL, Message.str());
81}
82
83Value *llvm::stripIntegerCast(Value *V) {
84 if (CastInst *CI = dyn_cast<CastInst>(V))
85 if (CI->getOperand(0)->getType()->isIntegerTy())
86 return CI->getOperand(0);
87 return V;
88}
89
90const SCEV *llvm::replaceSymbolicStrideSCEV(ScalarEvolution *SE,
Adam Nemet8bc61df2015-02-24 00:41:59 +000091 const ValueToValueMap &PtrToStride,
Adam Nemet04563272015-02-01 16:56:15 +000092 Value *Ptr, Value *OrigPtr) {
93
94 const SCEV *OrigSCEV = SE->getSCEV(Ptr);
95
96 // If there is an entry in the map return the SCEV of the pointer with the
97 // symbolic stride replaced by one.
Adam Nemet8bc61df2015-02-24 00:41:59 +000098 ValueToValueMap::const_iterator SI =
99 PtrToStride.find(OrigPtr ? OrigPtr : Ptr);
Adam Nemet04563272015-02-01 16:56:15 +0000100 if (SI != PtrToStride.end()) {
101 Value *StrideVal = SI->second;
102
103 // Strip casts.
104 StrideVal = stripIntegerCast(StrideVal);
105
106 // Replace symbolic stride by one.
107 Value *One = ConstantInt::get(StrideVal->getType(), 1);
108 ValueToValueMap RewriteMap;
109 RewriteMap[StrideVal] = One;
110
111 const SCEV *ByOne =
112 SCEVParameterRewriter::rewrite(OrigSCEV, *SE, RewriteMap, true);
Adam Nemet339f42b2015-02-19 19:15:07 +0000113 DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *ByOne
Adam Nemet04563272015-02-01 16:56:15 +0000114 << "\n");
115 return ByOne;
116 }
117
118 // Otherwise, just return the SCEV of the original pointer.
119 return SE->getSCEV(Ptr);
120}
121
Adam Nemet7cdebac2015-07-14 22:32:44 +0000122void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, bool WritePtr,
123 unsigned DepSetId, unsigned ASId,
124 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000125 // Get the stride replaced scev.
126 const SCEV *Sc = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
127 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc);
128 assert(AR && "Invalid addrec expression");
129 const SCEV *Ex = SE->getBackedgeTakenCount(Lp);
130 const SCEV *ScEnd = AR->evaluateAtIteration(Ex, *SE);
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000131 Pointers.emplace_back(Ptr, AR->getStart(), ScEnd, WritePtr, DepSetId, ASId,
132 Sc);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000133}
134
Adam Nemet7cdebac2015-07-14 22:32:44 +0000135bool RuntimePointerChecking::needsChecking(
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000136 const CheckingPtrGroup &M, const CheckingPtrGroup &N,
137 const SmallVectorImpl<int> *PtrPartition) const {
138 for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I)
139 for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J)
140 if (needsChecking(M.Members[I], N.Members[J], PtrPartition))
141 return true;
142 return false;
143}
144
145/// Compare \p I and \p J and return the minimum.
146/// Return nullptr in case we couldn't find an answer.
147static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
148 ScalarEvolution *SE) {
149 const SCEV *Diff = SE->getMinusSCEV(J, I);
150 const SCEVConstant *C = dyn_cast<const SCEVConstant>(Diff);
151
152 if (!C)
153 return nullptr;
154 if (C->getValue()->isNegative())
155 return J;
156 return I;
157}
158
Adam Nemet7cdebac2015-07-14 22:32:44 +0000159bool RuntimePointerChecking::CheckingPtrGroup::addPointer(unsigned Index) {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000160 const SCEV *Start = RtCheck.Pointers[Index].Start;
161 const SCEV *End = RtCheck.Pointers[Index].End;
162
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000163 // Compare the starts and ends with the known minimum and maximum
164 // of this set. We need to know how we compare against the min/max
165 // of the set in order to be able to emit memchecks.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000166 const SCEV *Min0 = getMinFromExprs(Start, Low, RtCheck.SE);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000167 if (!Min0)
168 return false;
169
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000170 const SCEV *Min1 = getMinFromExprs(End, High, RtCheck.SE);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000171 if (!Min1)
172 return false;
173
174 // Update the low bound expression if we've found a new min value.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000175 if (Min0 == Start)
176 Low = Start;
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000177
178 // Update the high bound expression if we've found a new max value.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000179 if (Min1 != End)
180 High = End;
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000181
182 Members.push_back(Index);
183 return true;
184}
185
Adam Nemet7cdebac2015-07-14 22:32:44 +0000186void RuntimePointerChecking::groupChecks(
187 MemoryDepChecker::DepCandidates &DepCands, bool UseDependencies) {
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000188 // We build the groups from dependency candidates equivalence classes
189 // because:
190 // - We know that pointers in the same equivalence class share
191 // the same underlying object and therefore there is a chance
192 // that we can compare pointers
193 // - We wouldn't be able to merge two pointers for which we need
194 // to emit a memcheck. The classes in DepCands are already
195 // conveniently built such that no two pointers in the same
196 // class need checking against each other.
197
198 // We use the following (greedy) algorithm to construct the groups
199 // For every pointer in the equivalence class:
200 // For each existing group:
201 // - if the difference between this pointer and the min/max bounds
202 // of the group is a constant, then make the pointer part of the
203 // group and update the min/max bounds of that group as required.
204
205 CheckingGroups.clear();
206
207 // If we don't have the dependency partitions, construct a new
208 // checking pointer group for each pointer.
209 if (!UseDependencies) {
210 for (unsigned I = 0; I < Pointers.size(); ++I)
211 CheckingGroups.push_back(CheckingPtrGroup(I, *this));
212 return;
213 }
214
215 unsigned TotalComparisons = 0;
216
217 DenseMap<Value *, unsigned> PositionMap;
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000218 for (unsigned Index = 0; Index < Pointers.size(); ++Index)
219 PositionMap[Pointers[Index].PointerValue] = Index;
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000220
Silviu Barangace3877f2015-07-09 15:18:25 +0000221 // We need to keep track of what pointers we've already seen so we
222 // don't process them twice.
223 SmallSet<unsigned, 2> Seen;
224
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000225 // Go through all equivalence classes, get the the "pointer check groups"
Silviu Barangace3877f2015-07-09 15:18:25 +0000226 // and add them to the overall solution. We use the order in which accesses
227 // appear in 'Pointers' to enforce determinism.
228 for (unsigned I = 0; I < Pointers.size(); ++I) {
229 // We've seen this pointer before, and therefore already processed
230 // its equivalence class.
231 if (Seen.count(I))
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000232 continue;
233
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000234 MemoryDepChecker::MemAccessInfo Access(Pointers[I].PointerValue,
235 Pointers[I].IsWritePtr);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000236
Silviu Barangace3877f2015-07-09 15:18:25 +0000237 SmallVector<CheckingPtrGroup, 2> Groups;
238 auto LeaderI = DepCands.findValue(DepCands.getLeaderValue(Access));
239
Silviu Barangaa647c302015-07-13 14:48:24 +0000240 // Because DepCands is constructed by visiting accesses in the order in
241 // which they appear in alias sets (which is deterministic) and the
242 // iteration order within an equivalence class member is only dependent on
243 // the order in which unions and insertions are performed on the
244 // equivalence class, the iteration order is deterministic.
Silviu Barangace3877f2015-07-09 15:18:25 +0000245 for (auto MI = DepCands.member_begin(LeaderI), ME = DepCands.member_end();
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000246 MI != ME; ++MI) {
247 unsigned Pointer = PositionMap[MI->getPointer()];
248 bool Merged = false;
Silviu Barangace3877f2015-07-09 15:18:25 +0000249 // Mark this pointer as seen.
250 Seen.insert(Pointer);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000251
252 // Go through all the existing sets and see if we can find one
253 // which can include this pointer.
254 for (CheckingPtrGroup &Group : Groups) {
255 // Don't perform more than a certain amount of comparisons.
256 // This should limit the cost of grouping the pointers to something
257 // reasonable. If we do end up hitting this threshold, the algorithm
258 // will create separate groups for all remaining pointers.
259 if (TotalComparisons > MemoryCheckMergeThreshold)
260 break;
261
262 TotalComparisons++;
263
264 if (Group.addPointer(Pointer)) {
265 Merged = true;
266 break;
267 }
268 }
269
270 if (!Merged)
271 // We couldn't add this pointer to any existing set or the threshold
272 // for the number of comparisons has been reached. Create a new group
273 // to hold the current pointer.
274 Groups.push_back(CheckingPtrGroup(Pointer, *this));
275 }
276
277 // We've computed the grouped checks for this partition.
278 // Save the results and continue with the next one.
279 std::copy(Groups.begin(), Groups.end(), std::back_inserter(CheckingGroups));
280 }
Adam Nemet04563272015-02-01 16:56:15 +0000281}
282
Adam Nemet7cdebac2015-07-14 22:32:44 +0000283bool RuntimePointerChecking::needsChecking(
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000284 unsigned I, unsigned J, const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000285 const PointerInfo &PointerI = Pointers[I];
286 const PointerInfo &PointerJ = Pointers[J];
287
Adam Nemeta8945b72015-02-18 03:43:58 +0000288 // No need to check if two readonly pointers intersect.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000289 if (!PointerI.IsWritePtr && !PointerJ.IsWritePtr)
Adam Nemeta8945b72015-02-18 03:43:58 +0000290 return false;
291
292 // Only need to check pointers between two different dependency sets.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000293 if (PointerI.DependencySetId == PointerJ.DependencySetId)
Adam Nemeta8945b72015-02-18 03:43:58 +0000294 return false;
295
296 // Only need to check pointers in the same alias set.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000297 if (PointerI.AliasSetId != PointerJ.AliasSetId)
Adam Nemeta8945b72015-02-18 03:43:58 +0000298 return false;
299
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000300 // If PtrPartition is set omit checks between pointers of the same partition.
301 // Partition number -1 means that the pointer is used in multiple partitions.
302 // In this case we can't omit the check.
303 if (PtrPartition && (*PtrPartition)[I] != -1 &&
304 (*PtrPartition)[I] == (*PtrPartition)[J])
305 return false;
306
Adam Nemeta8945b72015-02-18 03:43:58 +0000307 return true;
308}
309
Adam Nemet7cdebac2015-07-14 22:32:44 +0000310void RuntimePointerChecking::print(
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000311 raw_ostream &OS, unsigned Depth,
312 const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemete91cc6e2015-02-19 19:15:19 +0000313
314 OS.indent(Depth) << "Run-time memory checks:\n";
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000315
Adam Nemete91cc6e2015-02-19 19:15:19 +0000316 unsigned N = 0;
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000317 for (unsigned I = 0; I < CheckingGroups.size(); ++I)
318 for (unsigned J = I + 1; J < CheckingGroups.size(); ++J)
319 if (needsChecking(CheckingGroups[I], CheckingGroups[J], PtrPartition)) {
320 OS.indent(Depth) << "Check " << N++ << ":\n";
321 OS.indent(Depth + 2) << "Comparing group " << I << ":\n";
322
323 for (unsigned K = 0; K < CheckingGroups[I].Members.size(); ++K) {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000324 OS.indent(Depth + 2)
325 << *Pointers[CheckingGroups[I].Members[K]].PointerValue << "\n";
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000326 if (PtrPartition)
327 OS << " (Partition: "
328 << (*PtrPartition)[CheckingGroups[I].Members[K]] << ")"
329 << "\n";
330 }
331
332 OS.indent(Depth + 2) << "Against group " << J << ":\n";
333
334 for (unsigned K = 0; K < CheckingGroups[J].Members.size(); ++K) {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000335 OS.indent(Depth + 2)
336 << *Pointers[CheckingGroups[J].Members[K]].PointerValue << "\n";
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000337 if (PtrPartition)
338 OS << " (Partition: "
339 << (*PtrPartition)[CheckingGroups[J].Members[K]] << ")"
340 << "\n";
341 }
Adam Nemete91cc6e2015-02-19 19:15:19 +0000342 }
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000343
344 OS.indent(Depth) << "Grouped accesses:\n";
345 for (unsigned I = 0; I < CheckingGroups.size(); ++I) {
346 OS.indent(Depth + 2) << "Group " << I << ":\n";
347 OS.indent(Depth + 4) << "(Low: " << *CheckingGroups[I].Low
348 << " High: " << *CheckingGroups[I].High << ")\n";
349 for (unsigned J = 0; J < CheckingGroups[I].Members.size(); ++J) {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000350 OS.indent(Depth + 6) << "Member: "
351 << *Pointers[CheckingGroups[I].Members[J]].Expr
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000352 << "\n";
353 }
354 }
Adam Nemete91cc6e2015-02-19 19:15:19 +0000355}
356
Adam Nemet7cdebac2015-07-14 22:32:44 +0000357unsigned RuntimePointerChecking::getNumberOfChecks(
Adam Nemet51870d12015-04-07 03:35:26 +0000358 const SmallVectorImpl<int> *PtrPartition) const {
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000359
360 unsigned NumPartitions = CheckingGroups.size();
Silviu Baranga98a13712015-06-08 10:27:06 +0000361 unsigned CheckCount = 0;
Adam Nemet51870d12015-04-07 03:35:26 +0000362
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000363 for (unsigned I = 0; I < NumPartitions; ++I)
364 for (unsigned J = I + 1; J < NumPartitions; ++J)
365 if (needsChecking(CheckingGroups[I], CheckingGroups[J], PtrPartition))
Silviu Baranga98a13712015-06-08 10:27:06 +0000366 CheckCount++;
367 return CheckCount;
368}
369
Adam Nemet7cdebac2015-07-14 22:32:44 +0000370bool RuntimePointerChecking::needsAnyChecking(
Silviu Baranga98a13712015-06-08 10:27:06 +0000371 const SmallVectorImpl<int> *PtrPartition) const {
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000372 unsigned NumPointers = Pointers.size();
373
374 for (unsigned I = 0; I < NumPointers; ++I)
375 for (unsigned J = I + 1; J < NumPointers; ++J)
376 if (needsChecking(I, J, PtrPartition))
377 return true;
378 return false;
Adam Nemet51870d12015-04-07 03:35:26 +0000379}
380
Adam Nemet04563272015-02-01 16:56:15 +0000381namespace {
382/// \brief Analyses memory accesses in a loop.
383///
384/// Checks whether run time pointer checks are needed and builds sets for data
385/// dependence checking.
386class AccessAnalysis {
387public:
388 /// \brief Read or write access location.
389 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
390 typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
391
Adam Nemete2b885c2015-04-23 20:09:20 +0000392 AccessAnalysis(const DataLayout &Dl, AliasAnalysis *AA, LoopInfo *LI,
Adam Nemetdee666b2015-03-10 17:40:34 +0000393 MemoryDepChecker::DepCandidates &DA)
Adam Nemet5dc3b2c2015-07-09 06:47:18 +0000394 : DL(Dl), AST(*AA), LI(LI), DepCands(DA),
395 IsRTCheckAnalysisNeeded(false) {}
Adam Nemet04563272015-02-01 16:56:15 +0000396
397 /// \brief Register a load and whether it is only read from.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000398 void addLoad(MemoryLocation &Loc, bool IsReadOnly) {
Adam Nemet04563272015-02-01 16:56:15 +0000399 Value *Ptr = const_cast<Value*>(Loc.Ptr);
Chandler Carruthecbd1682015-06-17 07:21:38 +0000400 AST.add(Ptr, MemoryLocation::UnknownSize, Loc.AATags);
Adam Nemet04563272015-02-01 16:56:15 +0000401 Accesses.insert(MemAccessInfo(Ptr, false));
402 if (IsReadOnly)
403 ReadOnlyPtr.insert(Ptr);
404 }
405
406 /// \brief Register a store.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000407 void addStore(MemoryLocation &Loc) {
Adam Nemet04563272015-02-01 16:56:15 +0000408 Value *Ptr = const_cast<Value*>(Loc.Ptr);
Chandler Carruthecbd1682015-06-17 07:21:38 +0000409 AST.add(Ptr, MemoryLocation::UnknownSize, Loc.AATags);
Adam Nemet04563272015-02-01 16:56:15 +0000410 Accesses.insert(MemAccessInfo(Ptr, true));
411 }
412
413 /// \brief Check whether we can check the pointers at runtime for
Adam Nemetee614742015-07-09 22:17:38 +0000414 /// non-intersection.
415 ///
416 /// Returns true if we need no check or if we do and we can generate them
417 /// (i.e. the pointers have computable bounds).
Adam Nemet7cdebac2015-07-14 22:32:44 +0000418 bool canCheckPtrAtRT(RuntimePointerChecking &RtCheck, ScalarEvolution *SE,
419 Loop *TheLoop, const ValueToValueMap &Strides,
Adam Nemet04563272015-02-01 16:56:15 +0000420 bool ShouldCheckStride = false);
421
422 /// \brief Goes over all memory accesses, checks whether a RT check is needed
423 /// and builds sets of dependent accesses.
424 void buildDependenceSets() {
425 processMemAccesses();
426 }
427
Adam Nemet5dc3b2c2015-07-09 06:47:18 +0000428 /// \brief Initial processing of memory accesses determined that we need to
429 /// perform dependency checking.
430 ///
431 /// Note that this can later be cleared if we retry memcheck analysis without
432 /// dependency checking (i.e. ShouldRetryWithRuntimeCheck).
Adam Nemet04563272015-02-01 16:56:15 +0000433 bool isDependencyCheckNeeded() { return !CheckDeps.empty(); }
Adam Nemetdf3dc5b2015-05-18 15:37:03 +0000434
435 /// We decided that no dependence analysis would be used. Reset the state.
436 void resetDepChecks(MemoryDepChecker &DepChecker) {
437 CheckDeps.clear();
438 DepChecker.clearInterestingDependences();
439 }
Adam Nemet04563272015-02-01 16:56:15 +0000440
441 MemAccessInfoSet &getDependenciesToCheck() { return CheckDeps; }
442
443private:
444 typedef SetVector<MemAccessInfo> PtrAccessSet;
445
446 /// \brief Go over all memory access and check whether runtime pointer checks
Adam Nemetb41d2d32015-07-09 06:47:21 +0000447 /// are needed and build sets of dependency check candidates.
Adam Nemet04563272015-02-01 16:56:15 +0000448 void processMemAccesses();
449
450 /// Set of all accesses.
451 PtrAccessSet Accesses;
452
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000453 const DataLayout &DL;
454
Adam Nemet04563272015-02-01 16:56:15 +0000455 /// Set of accesses that need a further dependence check.
456 MemAccessInfoSet CheckDeps;
457
458 /// Set of pointers that are read only.
459 SmallPtrSet<Value*, 16> ReadOnlyPtr;
460
Adam Nemet04563272015-02-01 16:56:15 +0000461 /// An alias set tracker to partition the access set by underlying object and
462 //intrinsic property (such as TBAA metadata).
463 AliasSetTracker AST;
464
Adam Nemete2b885c2015-04-23 20:09:20 +0000465 LoopInfo *LI;
466
Adam Nemet04563272015-02-01 16:56:15 +0000467 /// Sets of potentially dependent accesses - members of one set share an
468 /// underlying pointer. The set "CheckDeps" identfies which sets really need a
469 /// dependence check.
Adam Nemetdee666b2015-03-10 17:40:34 +0000470 MemoryDepChecker::DepCandidates &DepCands;
Adam Nemet04563272015-02-01 16:56:15 +0000471
Adam Nemet5dc3b2c2015-07-09 06:47:18 +0000472 /// \brief Initial processing of memory accesses determined that we may need
473 /// to add memchecks. Perform the analysis to determine the necessary checks.
474 ///
475 /// Note that, this is different from isDependencyCheckNeeded. When we retry
476 /// memcheck analysis without dependency checking
477 /// (i.e. ShouldRetryWithRuntimeCheck), isDependencyCheckNeeded is cleared
478 /// while this remains set if we have potentially dependent accesses.
479 bool IsRTCheckAnalysisNeeded;
Adam Nemet04563272015-02-01 16:56:15 +0000480};
481
482} // end anonymous namespace
483
484/// \brief Check whether a pointer can participate in a runtime bounds check.
Adam Nemet8bc61df2015-02-24 00:41:59 +0000485static bool hasComputableBounds(ScalarEvolution *SE,
486 const ValueToValueMap &Strides, Value *Ptr) {
Adam Nemet04563272015-02-01 16:56:15 +0000487 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
488 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
489 if (!AR)
490 return false;
491
492 return AR->isAffine();
493}
494
Adam Nemet7cdebac2015-07-14 22:32:44 +0000495bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
496 ScalarEvolution *SE, Loop *TheLoop,
497 const ValueToValueMap &StridesMap,
498 bool ShouldCheckStride) {
Adam Nemet04563272015-02-01 16:56:15 +0000499 // Find pointers with computable bounds. We are going to use this information
500 // to place a runtime bound check.
501 bool CanDoRT = true;
502
Adam Nemetee614742015-07-09 22:17:38 +0000503 bool NeedRTCheck = false;
Adam Nemet5dc3b2c2015-07-09 06:47:18 +0000504 if (!IsRTCheckAnalysisNeeded) return true;
Silviu Baranga98a13712015-06-08 10:27:06 +0000505
Adam Nemet04563272015-02-01 16:56:15 +0000506 bool IsDepCheckNeeded = isDependencyCheckNeeded();
Adam Nemet04563272015-02-01 16:56:15 +0000507
508 // We assign a consecutive id to access from different alias sets.
509 // Accesses between different groups doesn't need to be checked.
510 unsigned ASId = 1;
511 for (auto &AS : AST) {
Adam Nemet424edc62015-07-08 22:58:48 +0000512 int NumReadPtrChecks = 0;
513 int NumWritePtrChecks = 0;
514
Adam Nemet04563272015-02-01 16:56:15 +0000515 // We assign consecutive id to access from different dependence sets.
516 // Accesses within the same set don't need a runtime check.
517 unsigned RunningDepId = 1;
518 DenseMap<Value *, unsigned> DepSetId;
519
520 for (auto A : AS) {
521 Value *Ptr = A.getValue();
522 bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
523 MemAccessInfo Access(Ptr, IsWrite);
524
Adam Nemet424edc62015-07-08 22:58:48 +0000525 if (IsWrite)
526 ++NumWritePtrChecks;
527 else
528 ++NumReadPtrChecks;
529
Adam Nemet04563272015-02-01 16:56:15 +0000530 if (hasComputableBounds(SE, StridesMap, Ptr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000531 // When we run after a failing dependency check we have to make sure
532 // we don't have wrapping pointers.
Adam Nemet04563272015-02-01 16:56:15 +0000533 (!ShouldCheckStride ||
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000534 isStridedPtr(SE, Ptr, TheLoop, StridesMap) == 1)) {
Adam Nemet04563272015-02-01 16:56:15 +0000535 // The id of the dependence set.
536 unsigned DepId;
537
538 if (IsDepCheckNeeded) {
539 Value *Leader = DepCands.getLeaderValue(Access).getPointer();
540 unsigned &LeaderId = DepSetId[Leader];
541 if (!LeaderId)
542 LeaderId = RunningDepId++;
543 DepId = LeaderId;
544 } else
545 // Each access has its own dependence set.
546 DepId = RunningDepId++;
547
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000548 RtCheck.insert(TheLoop, Ptr, IsWrite, DepId, ASId, StridesMap);
Adam Nemet04563272015-02-01 16:56:15 +0000549
Adam Nemet339f42b2015-02-19 19:15:07 +0000550 DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000551 } else {
Adam Nemetf10ca272015-05-18 15:36:52 +0000552 DEBUG(dbgs() << "LAA: Can't find bounds for ptr:" << *Ptr << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000553 CanDoRT = false;
554 }
555 }
556
Adam Nemet424edc62015-07-08 22:58:48 +0000557 // If we have at least two writes or one write and a read then we need to
558 // check them. But there is no need to checks if there is only one
559 // dependence set for this alias set.
560 //
561 // Note that this function computes CanDoRT and NeedRTCheck independently.
562 // For example CanDoRT=false, NeedRTCheck=false means that we have a pointer
563 // for which we couldn't find the bounds but we don't actually need to emit
564 // any checks so it does not matter.
565 if (!(IsDepCheckNeeded && CanDoRT && RunningDepId == 2))
566 NeedRTCheck |= (NumWritePtrChecks >= 2 || (NumReadPtrChecks >= 1 &&
567 NumWritePtrChecks >= 1));
568
Adam Nemet04563272015-02-01 16:56:15 +0000569 ++ASId;
570 }
571
572 // If the pointers that we would use for the bounds comparison have different
573 // address spaces, assume the values aren't directly comparable, so we can't
574 // use them for the runtime check. We also have to assume they could
575 // overlap. In the future there should be metadata for whether address spaces
576 // are disjoint.
577 unsigned NumPointers = RtCheck.Pointers.size();
578 for (unsigned i = 0; i < NumPointers; ++i) {
579 for (unsigned j = i + 1; j < NumPointers; ++j) {
580 // Only need to check pointers between two different dependency sets.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000581 if (RtCheck.Pointers[i].DependencySetId ==
582 RtCheck.Pointers[j].DependencySetId)
Adam Nemet04563272015-02-01 16:56:15 +0000583 continue;
584 // Only need to check pointers in the same alias set.
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000585 if (RtCheck.Pointers[i].AliasSetId != RtCheck.Pointers[j].AliasSetId)
Adam Nemet04563272015-02-01 16:56:15 +0000586 continue;
587
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000588 Value *PtrI = RtCheck.Pointers[i].PointerValue;
589 Value *PtrJ = RtCheck.Pointers[j].PointerValue;
Adam Nemet04563272015-02-01 16:56:15 +0000590
591 unsigned ASi = PtrI->getType()->getPointerAddressSpace();
592 unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
593 if (ASi != ASj) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000594 DEBUG(dbgs() << "LAA: Runtime check would require comparison between"
Adam Nemet04d41632015-02-19 19:14:34 +0000595 " different address spaces\n");
Adam Nemet04563272015-02-01 16:56:15 +0000596 return false;
597 }
598 }
599 }
600
Silviu Baranga1b6b50a2015-07-08 09:16:33 +0000601 if (NeedRTCheck && CanDoRT)
602 RtCheck.groupChecks(DepCands, IsDepCheckNeeded);
603
Adam Nemetee614742015-07-09 22:17:38 +0000604 DEBUG(dbgs() << "LAA: We need to do " << RtCheck.getNumberOfChecks(nullptr)
605 << " pointer comparisons.\n");
606
607 RtCheck.Need = NeedRTCheck;
608
609 bool CanDoRTIfNeeded = !NeedRTCheck || CanDoRT;
610 if (!CanDoRTIfNeeded)
611 RtCheck.reset();
612 return CanDoRTIfNeeded;
Adam Nemet04563272015-02-01 16:56:15 +0000613}
614
615void AccessAnalysis::processMemAccesses() {
616 // We process the set twice: first we process read-write pointers, last we
617 // process read-only pointers. This allows us to skip dependence tests for
618 // read-only pointers.
619
Adam Nemet339f42b2015-02-19 19:15:07 +0000620 DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
Adam Nemet04563272015-02-01 16:56:15 +0000621 DEBUG(dbgs() << " AST: "; AST.dump());
Adam Nemet9c926572015-03-10 17:40:37 +0000622 DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
Adam Nemet04563272015-02-01 16:56:15 +0000623 DEBUG({
624 for (auto A : Accesses)
625 dbgs() << "\t" << *A.getPointer() << " (" <<
626 (A.getInt() ? "write" : (ReadOnlyPtr.count(A.getPointer()) ?
627 "read-only" : "read")) << ")\n";
628 });
629
630 // The AliasSetTracker has nicely partitioned our pointers by metadata
631 // compatibility and potential for underlying-object overlap. As a result, we
632 // only need to check for potential pointer dependencies within each alias
633 // set.
634 for (auto &AS : AST) {
635 // Note that both the alias-set tracker and the alias sets themselves used
636 // linked lists internally and so the iteration order here is deterministic
637 // (matching the original instruction order within each set).
638
639 bool SetHasWrite = false;
640
641 // Map of pointers to last access encountered.
642 typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap;
643 UnderlyingObjToAccessMap ObjToLastAccess;
644
645 // Set of access to check after all writes have been processed.
646 PtrAccessSet DeferredAccesses;
647
648 // Iterate over each alias set twice, once to process read/write pointers,
649 // and then to process read-only pointers.
650 for (int SetIteration = 0; SetIteration < 2; ++SetIteration) {
651 bool UseDeferred = SetIteration > 0;
652 PtrAccessSet &S = UseDeferred ? DeferredAccesses : Accesses;
653
654 for (auto AV : AS) {
655 Value *Ptr = AV.getValue();
656
657 // For a single memory access in AliasSetTracker, Accesses may contain
658 // both read and write, and they both need to be handled for CheckDeps.
659 for (auto AC : S) {
660 if (AC.getPointer() != Ptr)
661 continue;
662
663 bool IsWrite = AC.getInt();
664
665 // If we're using the deferred access set, then it contains only
666 // reads.
667 bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite;
668 if (UseDeferred && !IsReadOnlyPtr)
669 continue;
670 // Otherwise, the pointer must be in the PtrAccessSet, either as a
671 // read or a write.
672 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
673 S.count(MemAccessInfo(Ptr, false))) &&
674 "Alias-set pointer not in the access set?");
675
676 MemAccessInfo Access(Ptr, IsWrite);
677 DepCands.insert(Access);
678
679 // Memorize read-only pointers for later processing and skip them in
680 // the first round (they need to be checked after we have seen all
681 // write pointers). Note: we also mark pointer that are not
682 // consecutive as "read-only" pointers (so that we check
683 // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
684 if (!UseDeferred && IsReadOnlyPtr) {
685 DeferredAccesses.insert(Access);
686 continue;
687 }
688
689 // If this is a write - check other reads and writes for conflicts. If
690 // this is a read only check other writes for conflicts (but only if
691 // there is no other write to the ptr - this is an optimization to
692 // catch "a[i] = a[i] + " without having to do a dependence check).
693 if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
694 CheckDeps.insert(Access);
Adam Nemet5dc3b2c2015-07-09 06:47:18 +0000695 IsRTCheckAnalysisNeeded = true;
Adam Nemet04563272015-02-01 16:56:15 +0000696 }
697
698 if (IsWrite)
699 SetHasWrite = true;
700
701 // Create sets of pointers connected by a shared alias set and
702 // underlying object.
703 typedef SmallVector<Value *, 16> ValueVector;
704 ValueVector TempObjects;
Adam Nemete2b885c2015-04-23 20:09:20 +0000705
706 GetUnderlyingObjects(Ptr, TempObjects, DL, LI);
707 DEBUG(dbgs() << "Underlying objects for pointer " << *Ptr << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000708 for (Value *UnderlyingObj : TempObjects) {
709 UnderlyingObjToAccessMap::iterator Prev =
710 ObjToLastAccess.find(UnderlyingObj);
711 if (Prev != ObjToLastAccess.end())
712 DepCands.unionSets(Access, Prev->second);
713
714 ObjToLastAccess[UnderlyingObj] = Access;
Adam Nemete2b885c2015-04-23 20:09:20 +0000715 DEBUG(dbgs() << " " << *UnderlyingObj << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000716 }
717 }
718 }
719 }
720 }
721}
722
Adam Nemet04563272015-02-01 16:56:15 +0000723static bool isInBoundsGep(Value *Ptr) {
724 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
725 return GEP->isInBounds();
726 return false;
727}
728
Adam Nemetc4866d22015-06-26 17:25:43 +0000729/// \brief Return true if an AddRec pointer \p Ptr is unsigned non-wrapping,
730/// i.e. monotonically increasing/decreasing.
731static bool isNoWrapAddRec(Value *Ptr, const SCEVAddRecExpr *AR,
732 ScalarEvolution *SE, const Loop *L) {
733 // FIXME: This should probably only return true for NUW.
734 if (AR->getNoWrapFlags(SCEV::NoWrapMask))
735 return true;
736
737 // Scalar evolution does not propagate the non-wrapping flags to values that
738 // are derived from a non-wrapping induction variable because non-wrapping
739 // could be flow-sensitive.
740 //
741 // Look through the potentially overflowing instruction to try to prove
742 // non-wrapping for the *specific* value of Ptr.
743
744 // The arithmetic implied by an inbounds GEP can't overflow.
745 auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);
746 if (!GEP || !GEP->isInBounds())
747 return false;
748
749 // Make sure there is only one non-const index and analyze that.
750 Value *NonConstIndex = nullptr;
751 for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
752 if (!isa<ConstantInt>(*Index)) {
753 if (NonConstIndex)
754 return false;
755 NonConstIndex = *Index;
756 }
757 if (!NonConstIndex)
758 // The recurrence is on the pointer, ignore for now.
759 return false;
760
761 // The index in GEP is signed. It is non-wrapping if it's derived from a NSW
762 // AddRec using a NSW operation.
763 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(NonConstIndex))
764 if (OBO->hasNoSignedWrap() &&
765 // Assume constant for other the operand so that the AddRec can be
766 // easily found.
767 isa<ConstantInt>(OBO->getOperand(1))) {
768 auto *OpScev = SE->getSCEV(OBO->getOperand(0));
769
770 if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpScev))
771 return OpAR->getLoop() == L && OpAR->getNoWrapFlags(SCEV::FlagNSW);
772 }
773
774 return false;
775}
776
Adam Nemet04563272015-02-01 16:56:15 +0000777/// \brief Check whether the access through \p Ptr has a constant stride.
Hao Liu32c05392015-06-08 06:39:56 +0000778int llvm::isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
779 const ValueToValueMap &StridesMap) {
Adam Nemet04563272015-02-01 16:56:15 +0000780 const Type *Ty = Ptr->getType();
781 assert(Ty->isPointerTy() && "Unexpected non-ptr");
782
783 // Make sure that the pointer does not point to aggregate types.
784 const PointerType *PtrTy = cast<PointerType>(Ty);
785 if (PtrTy->getElementType()->isAggregateType()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000786 DEBUG(dbgs() << "LAA: Bad stride - Not a pointer to a scalar type"
787 << *Ptr << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000788 return 0;
789 }
790
791 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, StridesMap, Ptr);
792
793 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
794 if (!AR) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000795 DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer "
Adam Nemet04d41632015-02-19 19:14:34 +0000796 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000797 return 0;
798 }
799
800 // The accesss function must stride over the innermost loop.
801 if (Lp != AR->getLoop()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000802 DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop " <<
Adam Nemet04d41632015-02-19 19:14:34 +0000803 *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000804 }
805
806 // The address calculation must not wrap. Otherwise, a dependence could be
807 // inverted.
808 // An inbounds getelementptr that is a AddRec with a unit stride
809 // cannot wrap per definition. The unit stride requirement is checked later.
810 // An getelementptr without an inbounds attribute and unit stride would have
811 // to access the pointer value "0" which is undefined behavior in address
812 // space 0, therefore we can also vectorize this case.
813 bool IsInBoundsGEP = isInBoundsGep(Ptr);
Adam Nemetc4866d22015-06-26 17:25:43 +0000814 bool IsNoWrapAddRec = isNoWrapAddRec(Ptr, AR, SE, Lp);
Adam Nemet04563272015-02-01 16:56:15 +0000815 bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0;
816 if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000817 DEBUG(dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
Adam Nemet04d41632015-02-19 19:14:34 +0000818 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000819 return 0;
820 }
821
822 // Check the step is constant.
823 const SCEV *Step = AR->getStepRecurrence(*SE);
824
Adam Nemet943befe2015-07-09 00:03:22 +0000825 // Calculate the pointer stride and check if it is constant.
Adam Nemet04563272015-02-01 16:56:15 +0000826 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
827 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000828 DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr <<
Adam Nemet04d41632015-02-19 19:14:34 +0000829 " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000830 return 0;
831 }
832
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000833 auto &DL = Lp->getHeader()->getModule()->getDataLayout();
834 int64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
Adam Nemet04563272015-02-01 16:56:15 +0000835 const APInt &APStepVal = C->getValue()->getValue();
836
837 // Huge step value - give up.
838 if (APStepVal.getBitWidth() > 64)
839 return 0;
840
841 int64_t StepVal = APStepVal.getSExtValue();
842
843 // Strided access.
844 int64_t Stride = StepVal / Size;
845 int64_t Rem = StepVal % Size;
846 if (Rem)
847 return 0;
848
849 // If the SCEV could wrap but we have an inbounds gep with a unit stride we
850 // know we can't "wrap around the address space". In case of address space
851 // zero we know that this won't happen without triggering undefined behavior.
852 if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) &&
853 Stride != 1 && Stride != -1)
854 return 0;
855
856 return Stride;
857}
858
Adam Nemet9c926572015-03-10 17:40:37 +0000859bool MemoryDepChecker::Dependence::isSafeForVectorization(DepType Type) {
860 switch (Type) {
861 case NoDep:
862 case Forward:
863 case BackwardVectorizable:
864 return true;
865
866 case Unknown:
867 case ForwardButPreventsForwarding:
868 case Backward:
869 case BackwardVectorizableButPreventsForwarding:
870 return false;
871 }
David Majnemerd388e932015-03-10 20:23:29 +0000872 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000873}
874
875bool MemoryDepChecker::Dependence::isInterestingDependence(DepType Type) {
876 switch (Type) {
877 case NoDep:
878 case Forward:
879 return false;
880
881 case BackwardVectorizable:
882 case Unknown:
883 case ForwardButPreventsForwarding:
884 case Backward:
885 case BackwardVectorizableButPreventsForwarding:
886 return true;
887 }
David Majnemerd388e932015-03-10 20:23:29 +0000888 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000889}
890
891bool MemoryDepChecker::Dependence::isPossiblyBackward() const {
892 switch (Type) {
893 case NoDep:
894 case Forward:
895 case ForwardButPreventsForwarding:
896 return false;
897
898 case Unknown:
899 case BackwardVectorizable:
900 case Backward:
901 case BackwardVectorizableButPreventsForwarding:
902 return true;
903 }
David Majnemerd388e932015-03-10 20:23:29 +0000904 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000905}
906
Adam Nemet04563272015-02-01 16:56:15 +0000907bool MemoryDepChecker::couldPreventStoreLoadForward(unsigned Distance,
908 unsigned TypeByteSize) {
909 // If loads occur at a distance that is not a multiple of a feasible vector
910 // factor store-load forwarding does not take place.
911 // Positive dependences might cause troubles because vectorizing them might
912 // prevent store-load forwarding making vectorized code run a lot slower.
913 // a[i] = a[i-3] ^ a[i-8];
914 // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
915 // hence on your typical architecture store-load forwarding does not take
916 // place. Vectorizing in such cases does not make sense.
917 // Store-load forwarding distance.
918 const unsigned NumCyclesForStoreLoadThroughMemory = 8*TypeByteSize;
919 // Maximum vector factor.
Adam Nemetf219c642015-02-19 19:14:52 +0000920 unsigned MaxVFWithoutSLForwardIssues =
921 VectorizerParams::MaxVectorWidth * TypeByteSize;
Adam Nemet04d41632015-02-19 19:14:34 +0000922 if(MaxSafeDepDistBytes < MaxVFWithoutSLForwardIssues)
Adam Nemet04563272015-02-01 16:56:15 +0000923 MaxVFWithoutSLForwardIssues = MaxSafeDepDistBytes;
924
925 for (unsigned vf = 2*TypeByteSize; vf <= MaxVFWithoutSLForwardIssues;
926 vf *= 2) {
927 if (Distance % vf && Distance / vf < NumCyclesForStoreLoadThroughMemory) {
928 MaxVFWithoutSLForwardIssues = (vf >>=1);
929 break;
930 }
931 }
932
Adam Nemet04d41632015-02-19 19:14:34 +0000933 if (MaxVFWithoutSLForwardIssues< 2*TypeByteSize) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000934 DEBUG(dbgs() << "LAA: Distance " << Distance <<
Adam Nemet04d41632015-02-19 19:14:34 +0000935 " that could cause a store-load forwarding conflict\n");
Adam Nemet04563272015-02-01 16:56:15 +0000936 return true;
937 }
938
939 if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes &&
Adam Nemetf219c642015-02-19 19:14:52 +0000940 MaxVFWithoutSLForwardIssues !=
941 VectorizerParams::MaxVectorWidth * TypeByteSize)
Adam Nemet04563272015-02-01 16:56:15 +0000942 MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues;
943 return false;
944}
945
Hao Liu751004a2015-06-08 04:48:37 +0000946/// \brief Check the dependence for two accesses with the same stride \p Stride.
947/// \p Distance is the positive distance and \p TypeByteSize is type size in
948/// bytes.
949///
950/// \returns true if they are independent.
951static bool areStridedAccessesIndependent(unsigned Distance, unsigned Stride,
952 unsigned TypeByteSize) {
953 assert(Stride > 1 && "The stride must be greater than 1");
954 assert(TypeByteSize > 0 && "The type size in byte must be non-zero");
955 assert(Distance > 0 && "The distance must be non-zero");
956
957 // Skip if the distance is not multiple of type byte size.
958 if (Distance % TypeByteSize)
959 return false;
960
961 unsigned ScaledDist = Distance / TypeByteSize;
962
963 // No dependence if the scaled distance is not multiple of the stride.
964 // E.g.
965 // for (i = 0; i < 1024 ; i += 4)
966 // A[i+2] = A[i] + 1;
967 //
968 // Two accesses in memory (scaled distance is 2, stride is 4):
969 // | A[0] | | | | A[4] | | | |
970 // | | | A[2] | | | | A[6] | |
971 //
972 // E.g.
973 // for (i = 0; i < 1024 ; i += 3)
974 // A[i+4] = A[i] + 1;
975 //
976 // Two accesses in memory (scaled distance is 4, stride is 3):
977 // | A[0] | | | A[3] | | | A[6] | | |
978 // | | | | | A[4] | | | A[7] | |
979 return ScaledDist % Stride;
980}
981
Adam Nemet9c926572015-03-10 17:40:37 +0000982MemoryDepChecker::Dependence::DepType
983MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
984 const MemAccessInfo &B, unsigned BIdx,
985 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000986 assert (AIdx < BIdx && "Must pass arguments in program order");
987
988 Value *APtr = A.getPointer();
989 Value *BPtr = B.getPointer();
990 bool AIsWrite = A.getInt();
991 bool BIsWrite = B.getInt();
992
993 // Two reads are independent.
994 if (!AIsWrite && !BIsWrite)
Adam Nemet9c926572015-03-10 17:40:37 +0000995 return Dependence::NoDep;
Adam Nemet04563272015-02-01 16:56:15 +0000996
997 // We cannot check pointers in different address spaces.
998 if (APtr->getType()->getPointerAddressSpace() !=
999 BPtr->getType()->getPointerAddressSpace())
Adam Nemet9c926572015-03-10 17:40:37 +00001000 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +00001001
1002 const SCEV *AScev = replaceSymbolicStrideSCEV(SE, Strides, APtr);
1003 const SCEV *BScev = replaceSymbolicStrideSCEV(SE, Strides, BPtr);
1004
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001005 int StrideAPtr = isStridedPtr(SE, APtr, InnermostLoop, Strides);
1006 int StrideBPtr = isStridedPtr(SE, BPtr, InnermostLoop, Strides);
Adam Nemet04563272015-02-01 16:56:15 +00001007
1008 const SCEV *Src = AScev;
1009 const SCEV *Sink = BScev;
1010
1011 // If the induction step is negative we have to invert source and sink of the
1012 // dependence.
1013 if (StrideAPtr < 0) {
1014 //Src = BScev;
1015 //Sink = AScev;
1016 std::swap(APtr, BPtr);
1017 std::swap(Src, Sink);
1018 std::swap(AIsWrite, BIsWrite);
1019 std::swap(AIdx, BIdx);
1020 std::swap(StrideAPtr, StrideBPtr);
1021 }
1022
1023 const SCEV *Dist = SE->getMinusSCEV(Sink, Src);
1024
Adam Nemet339f42b2015-02-19 19:15:07 +00001025 DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
Adam Nemet04d41632015-02-19 19:14:34 +00001026 << "(Induction step: " << StrideAPtr << ")\n");
Adam Nemet339f42b2015-02-19 19:15:07 +00001027 DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to "
Adam Nemet04d41632015-02-19 19:14:34 +00001028 << *InstMap[BIdx] << ": " << *Dist << "\n");
Adam Nemet04563272015-02-01 16:56:15 +00001029
Adam Nemet943befe2015-07-09 00:03:22 +00001030 // Need accesses with constant stride. We don't want to vectorize
Adam Nemet04563272015-02-01 16:56:15 +00001031 // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in
1032 // the address space.
1033 if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){
Adam Nemet943befe2015-07-09 00:03:22 +00001034 DEBUG(dbgs() << "Pointer access with non-constant stride\n");
Adam Nemet9c926572015-03-10 17:40:37 +00001035 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +00001036 }
1037
1038 const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist);
1039 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001040 DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n");
Adam Nemet04563272015-02-01 16:56:15 +00001041 ShouldRetryWithRuntimeCheck = true;
Adam Nemet9c926572015-03-10 17:40:37 +00001042 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +00001043 }
1044
1045 Type *ATy = APtr->getType()->getPointerElementType();
1046 Type *BTy = BPtr->getType()->getPointerElementType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001047 auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
1048 unsigned TypeByteSize = DL.getTypeAllocSize(ATy);
Adam Nemet04563272015-02-01 16:56:15 +00001049
1050 // Negative distances are not plausible dependencies.
1051 const APInt &Val = C->getValue()->getValue();
1052 if (Val.isNegative()) {
1053 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
1054 if (IsTrueDataDependence &&
1055 (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) ||
1056 ATy != BTy))
Adam Nemet9c926572015-03-10 17:40:37 +00001057 return Dependence::ForwardButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +00001058
Adam Nemet339f42b2015-02-19 19:15:07 +00001059 DEBUG(dbgs() << "LAA: Dependence is negative: NoDep\n");
Adam Nemet9c926572015-03-10 17:40:37 +00001060 return Dependence::Forward;
Adam Nemet04563272015-02-01 16:56:15 +00001061 }
1062
1063 // Write to the same location with the same size.
1064 // Could be improved to assert type sizes are the same (i32 == float, etc).
1065 if (Val == 0) {
1066 if (ATy == BTy)
Adam Nemet9c926572015-03-10 17:40:37 +00001067 return Dependence::NoDep;
Adam Nemet339f42b2015-02-19 19:15:07 +00001068 DEBUG(dbgs() << "LAA: Zero dependence difference but different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +00001069 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +00001070 }
1071
1072 assert(Val.isStrictlyPositive() && "Expect a positive value");
1073
Adam Nemet04563272015-02-01 16:56:15 +00001074 if (ATy != BTy) {
Adam Nemet04d41632015-02-19 19:14:34 +00001075 DEBUG(dbgs() <<
Adam Nemet339f42b2015-02-19 19:15:07 +00001076 "LAA: ReadWrite-Write positive dependency with different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +00001077 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +00001078 }
1079
1080 unsigned Distance = (unsigned) Val.getZExtValue();
1081
Hao Liu751004a2015-06-08 04:48:37 +00001082 unsigned Stride = std::abs(StrideAPtr);
1083 if (Stride > 1 &&
Adam Nemet0131a562015-07-08 18:47:38 +00001084 areStridedAccessesIndependent(Distance, Stride, TypeByteSize)) {
1085 DEBUG(dbgs() << "LAA: Strided accesses are independent\n");
Hao Liu751004a2015-06-08 04:48:37 +00001086 return Dependence::NoDep;
Adam Nemet0131a562015-07-08 18:47:38 +00001087 }
Hao Liu751004a2015-06-08 04:48:37 +00001088
Adam Nemet04563272015-02-01 16:56:15 +00001089 // Bail out early if passed-in parameters make vectorization not feasible.
Adam Nemetf219c642015-02-19 19:14:52 +00001090 unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ?
1091 VectorizerParams::VectorizationFactor : 1);
1092 unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
1093 VectorizerParams::VectorizationInterleave : 1);
Hao Liu751004a2015-06-08 04:48:37 +00001094 // The minimum number of iterations for a vectorized/unrolled version.
1095 unsigned MinNumIter = std::max(ForcedFactor * ForcedUnroll, 2U);
Adam Nemet04563272015-02-01 16:56:15 +00001096
Hao Liu751004a2015-06-08 04:48:37 +00001097 // It's not vectorizable if the distance is smaller than the minimum distance
1098 // needed for a vectroized/unrolled version. Vectorizing one iteration in
1099 // front needs TypeByteSize * Stride. Vectorizing the last iteration needs
1100 // TypeByteSize (No need to plus the last gap distance).
1101 //
1102 // E.g. Assume one char is 1 byte in memory and one int is 4 bytes.
1103 // foo(int *A) {
1104 // int *B = (int *)((char *)A + 14);
1105 // for (i = 0 ; i < 1024 ; i += 2)
1106 // B[i] = A[i] + 1;
1107 // }
1108 //
1109 // Two accesses in memory (stride is 2):
1110 // | A[0] | | A[2] | | A[4] | | A[6] | |
1111 // | B[0] | | B[2] | | B[4] |
1112 //
1113 // Distance needs for vectorizing iterations except the last iteration:
1114 // 4 * 2 * (MinNumIter - 1). Distance needs for the last iteration: 4.
1115 // So the minimum distance needed is: 4 * 2 * (MinNumIter - 1) + 4.
1116 //
1117 // If MinNumIter is 2, it is vectorizable as the minimum distance needed is
1118 // 12, which is less than distance.
1119 //
1120 // If MinNumIter is 4 (Say if a user forces the vectorization factor to be 4),
1121 // the minimum distance needed is 28, which is greater than distance. It is
1122 // not safe to do vectorization.
1123 unsigned MinDistanceNeeded =
1124 TypeByteSize * Stride * (MinNumIter - 1) + TypeByteSize;
1125 if (MinDistanceNeeded > Distance) {
1126 DEBUG(dbgs() << "LAA: Failure because of positive distance " << Distance
1127 << '\n');
1128 return Dependence::Backward;
1129 }
1130
1131 // Unsafe if the minimum distance needed is greater than max safe distance.
1132 if (MinDistanceNeeded > MaxSafeDepDistBytes) {
1133 DEBUG(dbgs() << "LAA: Failure because it needs at least "
1134 << MinDistanceNeeded << " size in bytes");
Adam Nemet9c926572015-03-10 17:40:37 +00001135 return Dependence::Backward;
Adam Nemet04563272015-02-01 16:56:15 +00001136 }
1137
Adam Nemet9cc0c392015-02-26 17:58:48 +00001138 // Positive distance bigger than max vectorization factor.
Hao Liu751004a2015-06-08 04:48:37 +00001139 // FIXME: Should use max factor instead of max distance in bytes, which could
1140 // not handle different types.
1141 // E.g. Assume one char is 1 byte in memory and one int is 4 bytes.
1142 // void foo (int *A, char *B) {
1143 // for (unsigned i = 0; i < 1024; i++) {
1144 // A[i+2] = A[i] + 1;
1145 // B[i+2] = B[i] + 1;
1146 // }
1147 // }
1148 //
1149 // This case is currently unsafe according to the max safe distance. If we
1150 // analyze the two accesses on array B, the max safe dependence distance
1151 // is 2. Then we analyze the accesses on array A, the minimum distance needed
1152 // is 8, which is less than 2 and forbidden vectorization, But actually
1153 // both A and B could be vectorized by 2 iterations.
1154 MaxSafeDepDistBytes =
1155 Distance < MaxSafeDepDistBytes ? Distance : MaxSafeDepDistBytes;
Adam Nemet04563272015-02-01 16:56:15 +00001156
1157 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
1158 if (IsTrueDataDependence &&
1159 couldPreventStoreLoadForward(Distance, TypeByteSize))
Adam Nemet9c926572015-03-10 17:40:37 +00001160 return Dependence::BackwardVectorizableButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +00001161
Hao Liu751004a2015-06-08 04:48:37 +00001162 DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue()
1163 << " with max VF = "
1164 << MaxSafeDepDistBytes / (TypeByteSize * Stride) << '\n');
Adam Nemet04563272015-02-01 16:56:15 +00001165
Adam Nemet9c926572015-03-10 17:40:37 +00001166 return Dependence::BackwardVectorizable;
Adam Nemet04563272015-02-01 16:56:15 +00001167}
1168
Adam Nemetdee666b2015-03-10 17:40:34 +00001169bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets,
Adam Nemet04563272015-02-01 16:56:15 +00001170 MemAccessInfoSet &CheckDeps,
Adam Nemet8bc61df2015-02-24 00:41:59 +00001171 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +00001172
1173 MaxSafeDepDistBytes = -1U;
1174 while (!CheckDeps.empty()) {
1175 MemAccessInfo CurAccess = *CheckDeps.begin();
1176
1177 // Get the relevant memory access set.
1178 EquivalenceClasses<MemAccessInfo>::iterator I =
1179 AccessSets.findValue(AccessSets.getLeaderValue(CurAccess));
1180
1181 // Check accesses within this set.
1182 EquivalenceClasses<MemAccessInfo>::member_iterator AI, AE;
1183 AI = AccessSets.member_begin(I), AE = AccessSets.member_end();
1184
1185 // Check every access pair.
1186 while (AI != AE) {
1187 CheckDeps.erase(*AI);
1188 EquivalenceClasses<MemAccessInfo>::member_iterator OI = std::next(AI);
1189 while (OI != AE) {
1190 // Check every accessing instruction pair in program order.
1191 for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
1192 I1E = Accesses[*AI].end(); I1 != I1E; ++I1)
1193 for (std::vector<unsigned>::iterator I2 = Accesses[*OI].begin(),
1194 I2E = Accesses[*OI].end(); I2 != I2E; ++I2) {
Adam Nemet9c926572015-03-10 17:40:37 +00001195 auto A = std::make_pair(&*AI, *I1);
1196 auto B = std::make_pair(&*OI, *I2);
1197
1198 assert(*I1 != *I2);
1199 if (*I1 > *I2)
1200 std::swap(A, B);
1201
1202 Dependence::DepType Type =
1203 isDependent(*A.first, A.second, *B.first, B.second, Strides);
1204 SafeForVectorization &= Dependence::isSafeForVectorization(Type);
1205
1206 // Gather dependences unless we accumulated MaxInterestingDependence
1207 // dependences. In that case return as soon as we find the first
1208 // unsafe dependence. This puts a limit on this quadratic
1209 // algorithm.
1210 if (RecordInterestingDependences) {
1211 if (Dependence::isInterestingDependence(Type))
1212 InterestingDependences.push_back(
1213 Dependence(A.second, B.second, Type));
1214
1215 if (InterestingDependences.size() >= MaxInterestingDependence) {
1216 RecordInterestingDependences = false;
1217 InterestingDependences.clear();
1218 DEBUG(dbgs() << "Too many dependences, stopped recording\n");
1219 }
1220 }
1221 if (!RecordInterestingDependences && !SafeForVectorization)
Adam Nemet04563272015-02-01 16:56:15 +00001222 return false;
1223 }
1224 ++OI;
1225 }
1226 AI++;
1227 }
1228 }
Adam Nemet9c926572015-03-10 17:40:37 +00001229
1230 DEBUG(dbgs() << "Total Interesting Dependences: "
1231 << InterestingDependences.size() << "\n");
1232 return SafeForVectorization;
Adam Nemet04563272015-02-01 16:56:15 +00001233}
1234
Adam Nemetec1e2bb2015-03-10 18:54:26 +00001235SmallVector<Instruction *, 4>
1236MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
1237 MemAccessInfo Access(Ptr, isWrite);
1238 auto &IndexVector = Accesses.find(Access)->second;
1239
1240 SmallVector<Instruction *, 4> Insts;
1241 std::transform(IndexVector.begin(), IndexVector.end(),
1242 std::back_inserter(Insts),
1243 [&](unsigned Idx) { return this->InstMap[Idx]; });
1244 return Insts;
1245}
1246
Adam Nemet58913d62015-03-10 17:40:43 +00001247const char *MemoryDepChecker::Dependence::DepName[] = {
1248 "NoDep", "Unknown", "Forward", "ForwardButPreventsForwarding", "Backward",
1249 "BackwardVectorizable", "BackwardVectorizableButPreventsForwarding"};
1250
1251void MemoryDepChecker::Dependence::print(
1252 raw_ostream &OS, unsigned Depth,
1253 const SmallVectorImpl<Instruction *> &Instrs) const {
1254 OS.indent(Depth) << DepName[Type] << ":\n";
1255 OS.indent(Depth + 2) << *Instrs[Source] << " -> \n";
1256 OS.indent(Depth + 2) << *Instrs[Destination] << "\n";
1257}
1258
Adam Nemet929c38e2015-02-19 19:15:10 +00001259bool LoopAccessInfo::canAnalyzeLoop() {
Adam Nemet8dcb3b62015-04-17 22:43:10 +00001260 // We need to have a loop header.
1261 DEBUG(dbgs() << "LAA: Found a loop: " <<
1262 TheLoop->getHeader()->getName() << '\n');
1263
Adam Nemet929c38e2015-02-19 19:15:10 +00001264 // We can only analyze innermost loops.
1265 if (!TheLoop->empty()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +00001266 DEBUG(dbgs() << "LAA: loop is not the innermost loop\n");
Adam Nemet2bd6e982015-02-19 19:15:15 +00001267 emitAnalysis(LoopAccessReport() << "loop is not the innermost loop");
Adam Nemet929c38e2015-02-19 19:15:10 +00001268 return false;
1269 }
1270
1271 // We must have a single backedge.
1272 if (TheLoop->getNumBackEdges() != 1) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +00001273 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +00001274 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +00001275 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +00001276 "loop control flow is not understood by analyzer");
1277 return false;
1278 }
1279
1280 // We must have a single exiting block.
1281 if (!TheLoop->getExitingBlock()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +00001282 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +00001283 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +00001284 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +00001285 "loop control flow is not understood by analyzer");
1286 return false;
1287 }
1288
1289 // We only handle bottom-tested loops, i.e. loop in which the condition is
1290 // checked at the end of each iteration. With that we can assume that all
1291 // instructions in the loop are executed the same number of times.
1292 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +00001293 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +00001294 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +00001295 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +00001296 "loop control flow is not understood by analyzer");
1297 return false;
1298 }
1299
Adam Nemet929c38e2015-02-19 19:15:10 +00001300 // ScalarEvolution needs to be able to find the exit count.
1301 const SCEV *ExitCount = SE->getBackedgeTakenCount(TheLoop);
1302 if (ExitCount == SE->getCouldNotCompute()) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001303 emitAnalysis(LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +00001304 "could not determine number of loop iterations");
1305 DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
1306 return false;
1307 }
1308
1309 return true;
1310}
1311
Adam Nemet8bc61df2015-02-24 00:41:59 +00001312void LoopAccessInfo::analyzeLoop(const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +00001313
1314 typedef SmallVector<Value*, 16> ValueVector;
1315 typedef SmallPtrSet<Value*, 16> ValueSet;
1316
1317 // Holds the Load and Store *instructions*.
1318 ValueVector Loads;
1319 ValueVector Stores;
1320
1321 // Holds all the different accesses in the loop.
1322 unsigned NumReads = 0;
1323 unsigned NumReadWrites = 0;
1324
Adam Nemet7cdebac2015-07-14 22:32:44 +00001325 PtrRtChecking.Pointers.clear();
1326 PtrRtChecking.Need = false;
Adam Nemet04563272015-02-01 16:56:15 +00001327
1328 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
Adam Nemet04563272015-02-01 16:56:15 +00001329
1330 // For each block.
1331 for (Loop::block_iterator bb = TheLoop->block_begin(),
1332 be = TheLoop->block_end(); bb != be; ++bb) {
1333
1334 // Scan the BB and collect legal loads and stores.
1335 for (BasicBlock::iterator it = (*bb)->begin(), e = (*bb)->end(); it != e;
1336 ++it) {
1337
1338 // If this is a load, save it. If this instruction can read from memory
1339 // but is not a load, then we quit. Notice that we don't handle function
1340 // calls that read or write.
1341 if (it->mayReadFromMemory()) {
1342 // Many math library functions read the rounding mode. We will only
1343 // vectorize a loop if it contains known function calls that don't set
1344 // the flag. Therefore, it is safe to ignore this read from memory.
1345 CallInst *Call = dyn_cast<CallInst>(it);
1346 if (Call && getIntrinsicIDForCall(Call, TLI))
1347 continue;
1348
Michael Zolotukhin9b3cf602015-03-17 19:46:50 +00001349 // If the function has an explicit vectorized counterpart, we can safely
1350 // assume that it can be vectorized.
1351 if (Call && !Call->isNoBuiltin() && Call->getCalledFunction() &&
1352 TLI->isFunctionVectorizable(Call->getCalledFunction()->getName()))
1353 continue;
1354
Adam Nemet04563272015-02-01 16:56:15 +00001355 LoadInst *Ld = dyn_cast<LoadInst>(it);
1356 if (!Ld || (!Ld->isSimple() && !IsAnnotatedParallel)) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001357 emitAnalysis(LoopAccessReport(Ld)
Adam Nemet04563272015-02-01 16:56:15 +00001358 << "read with atomic ordering or volatile read");
Adam Nemet339f42b2015-02-19 19:15:07 +00001359 DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001360 CanVecMem = false;
1361 return;
Adam Nemet04563272015-02-01 16:56:15 +00001362 }
1363 NumLoads++;
1364 Loads.push_back(Ld);
1365 DepChecker.addAccess(Ld);
1366 continue;
1367 }
1368
1369 // Save 'store' instructions. Abort if other instructions write to memory.
1370 if (it->mayWriteToMemory()) {
1371 StoreInst *St = dyn_cast<StoreInst>(it);
1372 if (!St) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001373 emitAnalysis(LoopAccessReport(it) <<
Adam Nemet04d41632015-02-19 19:14:34 +00001374 "instruction cannot be vectorized");
Adam Nemet436018c2015-02-19 19:15:00 +00001375 CanVecMem = false;
1376 return;
Adam Nemet04563272015-02-01 16:56:15 +00001377 }
1378 if (!St->isSimple() && !IsAnnotatedParallel) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001379 emitAnalysis(LoopAccessReport(St)
Adam Nemet04563272015-02-01 16:56:15 +00001380 << "write with atomic ordering or volatile write");
Adam Nemet339f42b2015-02-19 19:15:07 +00001381 DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001382 CanVecMem = false;
1383 return;
Adam Nemet04563272015-02-01 16:56:15 +00001384 }
1385 NumStores++;
1386 Stores.push_back(St);
1387 DepChecker.addAccess(St);
1388 }
1389 } // Next instr.
1390 } // Next block.
1391
1392 // Now we have two lists that hold the loads and the stores.
1393 // Next, we find the pointers that they use.
1394
1395 // Check if we see any stores. If there are no stores, then we don't
1396 // care if the pointers are *restrict*.
1397 if (!Stores.size()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001398 DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001399 CanVecMem = true;
1400 return;
Adam Nemet04563272015-02-01 16:56:15 +00001401 }
1402
Adam Nemetdee666b2015-03-10 17:40:34 +00001403 MemoryDepChecker::DepCandidates DependentAccesses;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001404 AccessAnalysis Accesses(TheLoop->getHeader()->getModule()->getDataLayout(),
Adam Nemete2b885c2015-04-23 20:09:20 +00001405 AA, LI, DependentAccesses);
Adam Nemet04563272015-02-01 16:56:15 +00001406
1407 // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects
1408 // multiple times on the same object. If the ptr is accessed twice, once
1409 // for read and once for write, it will only appear once (on the write
1410 // list). This is okay, since we are going to check for conflicts between
1411 // writes and between reads and writes, but not between reads and reads.
1412 ValueSet Seen;
1413
1414 ValueVector::iterator I, IE;
1415 for (I = Stores.begin(), IE = Stores.end(); I != IE; ++I) {
1416 StoreInst *ST = cast<StoreInst>(*I);
1417 Value* Ptr = ST->getPointerOperand();
Adam Nemetce482502015-04-08 17:48:40 +00001418 // Check for store to loop invariant address.
1419 StoreToLoopInvariantAddress |= isUniform(Ptr);
Adam Nemet04563272015-02-01 16:56:15 +00001420 // If we did *not* see this pointer before, insert it to the read-write
1421 // list. At this phase it is only a 'write' list.
1422 if (Seen.insert(Ptr).second) {
1423 ++NumReadWrites;
1424
Chandler Carruthac80dc72015-06-17 07:18:54 +00001425 MemoryLocation Loc = MemoryLocation::get(ST);
Adam Nemet04563272015-02-01 16:56:15 +00001426 // The TBAA metadata could have a control dependency on the predication
1427 // condition, so we cannot rely on it when determining whether or not we
1428 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001429 if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001430 Loc.AATags.TBAA = nullptr;
1431
1432 Accesses.addStore(Loc);
1433 }
1434 }
1435
1436 if (IsAnnotatedParallel) {
Adam Nemet04d41632015-02-19 19:14:34 +00001437 DEBUG(dbgs()
Adam Nemet339f42b2015-02-19 19:15:07 +00001438 << "LAA: A loop annotated parallel, ignore memory dependency "
Adam Nemet04d41632015-02-19 19:14:34 +00001439 << "checks.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001440 CanVecMem = true;
1441 return;
Adam Nemet04563272015-02-01 16:56:15 +00001442 }
1443
1444 for (I = Loads.begin(), IE = Loads.end(); I != IE; ++I) {
1445 LoadInst *LD = cast<LoadInst>(*I);
1446 Value* Ptr = LD->getPointerOperand();
1447 // If we did *not* see this pointer before, insert it to the
1448 // read list. If we *did* see it before, then it is already in
1449 // the read-write list. This allows us to vectorize expressions
1450 // such as A[i] += x; Because the address of A[i] is a read-write
1451 // pointer. This only works if the index of A[i] is consecutive.
1452 // If the address of i is unknown (for example A[B[i]]) then we may
1453 // read a few words, modify, and write a few words, and some of the
1454 // words may be written to the same address.
1455 bool IsReadOnlyPtr = false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001456 if (Seen.insert(Ptr).second || !isStridedPtr(SE, Ptr, TheLoop, Strides)) {
Adam Nemet04563272015-02-01 16:56:15 +00001457 ++NumReads;
1458 IsReadOnlyPtr = true;
1459 }
1460
Chandler Carruthac80dc72015-06-17 07:18:54 +00001461 MemoryLocation Loc = MemoryLocation::get(LD);
Adam Nemet04563272015-02-01 16:56:15 +00001462 // The TBAA metadata could have a control dependency on the predication
1463 // condition, so we cannot rely on it when determining whether or not we
1464 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001465 if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001466 Loc.AATags.TBAA = nullptr;
1467
1468 Accesses.addLoad(Loc, IsReadOnlyPtr);
1469 }
1470
1471 // If we write (or read-write) to a single destination and there are no
1472 // other reads in this loop then is it safe to vectorize.
1473 if (NumReadWrites == 1 && NumReads == 0) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001474 DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001475 CanVecMem = true;
1476 return;
Adam Nemet04563272015-02-01 16:56:15 +00001477 }
1478
1479 // Build dependence sets and check whether we need a runtime pointer bounds
1480 // check.
1481 Accesses.buildDependenceSets();
Adam Nemet04563272015-02-01 16:56:15 +00001482
1483 // Find pointers with computable bounds. We are going to use this information
1484 // to place a runtime bound check.
Adam Nemetee614742015-07-09 22:17:38 +00001485 bool CanDoRTIfNeeded =
Adam Nemet7cdebac2015-07-14 22:32:44 +00001486 Accesses.canCheckPtrAtRT(PtrRtChecking, SE, TheLoop, Strides);
Adam Nemetee614742015-07-09 22:17:38 +00001487 if (!CanDoRTIfNeeded) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001488 emitAnalysis(LoopAccessReport() << "cannot identify array bounds");
Adam Nemetee614742015-07-09 22:17:38 +00001489 DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
1490 << "the array bounds.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001491 CanVecMem = false;
1492 return;
Adam Nemet04563272015-02-01 16:56:15 +00001493 }
1494
Adam Nemetee614742015-07-09 22:17:38 +00001495 DEBUG(dbgs() << "LAA: We can perform a memory runtime check if needed.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001496
Adam Nemet436018c2015-02-19 19:15:00 +00001497 CanVecMem = true;
Adam Nemet04563272015-02-01 16:56:15 +00001498 if (Accesses.isDependencyCheckNeeded()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001499 DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
Adam Nemet04563272015-02-01 16:56:15 +00001500 CanVecMem = DepChecker.areDepsSafe(
1501 DependentAccesses, Accesses.getDependenciesToCheck(), Strides);
1502 MaxSafeDepDistBytes = DepChecker.getMaxSafeDepDistBytes();
1503
1504 if (!CanVecMem && DepChecker.shouldRetryWithRuntimeCheck()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001505 DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
Adam Nemet04563272015-02-01 16:56:15 +00001506
1507 // Clear the dependency checks. We assume they are not needed.
Adam Nemetdf3dc5b2015-05-18 15:37:03 +00001508 Accesses.resetDepChecks(DepChecker);
Adam Nemet04563272015-02-01 16:56:15 +00001509
Adam Nemet7cdebac2015-07-14 22:32:44 +00001510 PtrRtChecking.reset();
1511 PtrRtChecking.Need = true;
Adam Nemet04563272015-02-01 16:56:15 +00001512
Adam Nemetee614742015-07-09 22:17:38 +00001513 CanDoRTIfNeeded =
Adam Nemet7cdebac2015-07-14 22:32:44 +00001514 Accesses.canCheckPtrAtRT(PtrRtChecking, SE, TheLoop, Strides, true);
Silviu Baranga98a13712015-06-08 10:27:06 +00001515
Adam Nemet949e91a2015-03-10 19:12:41 +00001516 // Check that we found the bounds for the pointer.
Adam Nemetee614742015-07-09 22:17:38 +00001517 if (!CanDoRTIfNeeded) {
Adam Nemetb6dc76f2015-03-10 18:54:19 +00001518 emitAnalysis(LoopAccessReport()
1519 << "cannot check memory dependencies at runtime");
1520 DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
Adam Nemetb6dc76f2015-03-10 18:54:19 +00001521 CanVecMem = false;
1522 return;
1523 }
1524
Adam Nemet04563272015-02-01 16:56:15 +00001525 CanVecMem = true;
1526 }
1527 }
1528
Adam Nemet4bb90a72015-03-10 21:47:39 +00001529 if (CanVecMem)
1530 DEBUG(dbgs() << "LAA: No unsafe dependent memory operations in loop. We"
Adam Nemet7cdebac2015-07-14 22:32:44 +00001531 << (PtrRtChecking.Need ? "" : " don't")
Adam Nemet0f67c6c2015-07-09 22:17:41 +00001532 << " need runtime memory checks.\n");
Adam Nemet4bb90a72015-03-10 21:47:39 +00001533 else {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001534 emitAnalysis(LoopAccessReport() <<
Adam Nemet04d41632015-02-19 19:14:34 +00001535 "unsafe dependent memory operations in loop");
Adam Nemet4bb90a72015-03-10 21:47:39 +00001536 DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n");
1537 }
Adam Nemet04563272015-02-01 16:56:15 +00001538}
1539
Adam Nemet01abb2c2015-02-18 03:43:19 +00001540bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
1541 DominatorTree *DT) {
Adam Nemet04563272015-02-01 16:56:15 +00001542 assert(TheLoop->contains(BB) && "Unknown block used");
1543
1544 // Blocks that do not dominate the latch need predication.
1545 BasicBlock* Latch = TheLoop->getLoopLatch();
1546 return !DT->dominates(BB, Latch);
1547}
1548
Adam Nemet2bd6e982015-02-19 19:15:15 +00001549void LoopAccessInfo::emitAnalysis(LoopAccessReport &Message) {
Adam Nemetc9228532015-02-19 19:14:56 +00001550 assert(!Report && "Multiple reports generated");
1551 Report = Message;
Adam Nemet04563272015-02-01 16:56:15 +00001552}
1553
Adam Nemet57ac7662015-02-19 19:15:21 +00001554bool LoopAccessInfo::isUniform(Value *V) const {
Adam Nemet04563272015-02-01 16:56:15 +00001555 return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
1556}
Adam Nemet7206d7a2015-02-06 18:31:04 +00001557
1558// FIXME: this function is currently a duplicate of the one in
1559// LoopVectorize.cpp.
1560static Instruction *getFirstInst(Instruction *FirstInst, Value *V,
1561 Instruction *Loc) {
1562 if (FirstInst)
1563 return FirstInst;
1564 if (Instruction *I = dyn_cast<Instruction>(V))
1565 return I->getParent() == Loc->getParent() ? I : nullptr;
1566 return nullptr;
1567}
1568
Adam Nemetec1e2bb2015-03-10 18:54:26 +00001569std::pair<Instruction *, Instruction *> LoopAccessInfo::addRuntimeCheck(
1570 Instruction *Loc, const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemet7cdebac2015-07-14 22:32:44 +00001571 if (!PtrRtChecking.Need)
Adam Nemet90fec842015-04-02 17:51:57 +00001572 return std::make_pair(nullptr, nullptr);
Adam Nemet7206d7a2015-02-06 18:31:04 +00001573
Silviu Baranga1b6b50a2015-07-08 09:16:33 +00001574 SmallVector<TrackingVH<Value>, 2> Starts;
1575 SmallVector<TrackingVH<Value>, 2> Ends;
Adam Nemet7206d7a2015-02-06 18:31:04 +00001576
1577 LLVMContext &Ctx = Loc->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001578 SCEVExpander Exp(*SE, DL, "induction");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001579 Instruction *FirstInst = nullptr;
1580
Adam Nemet7cdebac2015-07-14 22:32:44 +00001581 for (unsigned i = 0; i < PtrRtChecking.CheckingGroups.size(); ++i) {
1582 const RuntimePointerChecking::CheckingPtrGroup &CG =
1583 PtrRtChecking.CheckingGroups[i];
Adam Nemet9f7dedc2015-07-14 22:32:50 +00001584 Value *Ptr = PtrRtChecking.Pointers[CG.Members[0]].PointerValue;
Adam Nemet7206d7a2015-02-06 18:31:04 +00001585 const SCEV *Sc = SE->getSCEV(Ptr);
1586
1587 if (SE->isLoopInvariant(Sc, TheLoop)) {
Silviu Baranga1b6b50a2015-07-08 09:16:33 +00001588 DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" << *Ptr
1589 << "\n");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001590 Starts.push_back(Ptr);
1591 Ends.push_back(Ptr);
1592 } else {
Adam Nemet7206d7a2015-02-06 18:31:04 +00001593 unsigned AS = Ptr->getType()->getPointerAddressSpace();
1594
1595 // Use this type for pointer arithmetic.
1596 Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
Silviu Baranga1b6b50a2015-07-08 09:16:33 +00001597 Value *Start = nullptr, *End = nullptr;
Adam Nemet7206d7a2015-02-06 18:31:04 +00001598
Silviu Baranga1b6b50a2015-07-08 09:16:33 +00001599 DEBUG(dbgs() << "LAA: Adding RT check for range:\n");
1600 Start = Exp.expandCodeFor(CG.Low, PtrArithTy, Loc);
1601 End = Exp.expandCodeFor(CG.High, PtrArithTy, Loc);
1602 DEBUG(dbgs() << "Start: " << *CG.Low << " End: " << *CG.High << "\n");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001603 Starts.push_back(Start);
1604 Ends.push_back(End);
1605 }
1606 }
1607
1608 IRBuilder<> ChkBuilder(Loc);
1609 // Our instructions might fold to a constant.
1610 Value *MemoryRuntimeCheck = nullptr;
Adam Nemet7cdebac2015-07-14 22:32:44 +00001611 for (unsigned i = 0; i < PtrRtChecking.CheckingGroups.size(); ++i) {
1612 for (unsigned j = i + 1; j < PtrRtChecking.CheckingGroups.size(); ++j) {
1613 const RuntimePointerChecking::CheckingPtrGroup &CGI =
1614 PtrRtChecking.CheckingGroups[i];
1615 const RuntimePointerChecking::CheckingPtrGroup &CGJ =
1616 PtrRtChecking.CheckingGroups[j];
Silviu Baranga1b6b50a2015-07-08 09:16:33 +00001617
Adam Nemet7cdebac2015-07-14 22:32:44 +00001618 if (!PtrRtChecking.needsChecking(CGI, CGJ, PtrPartition))
Adam Nemet7206d7a2015-02-06 18:31:04 +00001619 continue;
1620
1621 unsigned AS0 = Starts[i]->getType()->getPointerAddressSpace();
1622 unsigned AS1 = Starts[j]->getType()->getPointerAddressSpace();
1623
1624 assert((AS0 == Ends[j]->getType()->getPointerAddressSpace()) &&
1625 (AS1 == Ends[i]->getType()->getPointerAddressSpace()) &&
1626 "Trying to bounds check pointers with different address spaces");
1627
1628 Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0);
1629 Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1);
1630
1631 Value *Start0 = ChkBuilder.CreateBitCast(Starts[i], PtrArithTy0, "bc");
1632 Value *Start1 = ChkBuilder.CreateBitCast(Starts[j], PtrArithTy1, "bc");
1633 Value *End0 = ChkBuilder.CreateBitCast(Ends[i], PtrArithTy1, "bc");
1634 Value *End1 = ChkBuilder.CreateBitCast(Ends[j], PtrArithTy0, "bc");
1635
1636 Value *Cmp0 = ChkBuilder.CreateICmpULE(Start0, End1, "bound0");
1637 FirstInst = getFirstInst(FirstInst, Cmp0, Loc);
1638 Value *Cmp1 = ChkBuilder.CreateICmpULE(Start1, End0, "bound1");
1639 FirstInst = getFirstInst(FirstInst, Cmp1, Loc);
1640 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
1641 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1642 if (MemoryRuntimeCheck) {
1643 IsConflict = ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,
1644 "conflict.rdx");
1645 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1646 }
1647 MemoryRuntimeCheck = IsConflict;
1648 }
1649 }
1650
Adam Nemet90fec842015-04-02 17:51:57 +00001651 if (!MemoryRuntimeCheck)
1652 return std::make_pair(nullptr, nullptr);
1653
Adam Nemet7206d7a2015-02-06 18:31:04 +00001654 // We have to do this trickery because the IRBuilder might fold the check to a
1655 // constant expression in which case there is no Instruction anchored in a
1656 // the block.
1657 Instruction *Check = BinaryOperator::CreateAnd(MemoryRuntimeCheck,
1658 ConstantInt::getTrue(Ctx));
1659 ChkBuilder.Insert(Check, "memcheck.conflict");
1660 FirstInst = getFirstInst(FirstInst, Check, Loc);
1661 return std::make_pair(FirstInst, Check);
1662}
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001663
1664LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001665 const DataLayout &DL,
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001666 const TargetLibraryInfo *TLI, AliasAnalysis *AA,
Adam Nemete2b885c2015-04-23 20:09:20 +00001667 DominatorTree *DT, LoopInfo *LI,
Adam Nemet8bc61df2015-02-24 00:41:59 +00001668 const ValueToValueMap &Strides)
Adam Nemet7cdebac2015-07-14 22:32:44 +00001669 : PtrRtChecking(SE), DepChecker(SE, L), TheLoop(L), SE(SE), DL(DL),
1670 TLI(TLI), AA(AA), DT(DT), LI(LI), NumLoads(0), NumStores(0),
Adam Nemetce482502015-04-08 17:48:40 +00001671 MaxSafeDepDistBytes(-1U), CanVecMem(false),
1672 StoreToLoopInvariantAddress(false) {
Adam Nemet929c38e2015-02-19 19:15:10 +00001673 if (canAnalyzeLoop())
1674 analyzeLoop(Strides);
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001675}
1676
Adam Nemete91cc6e2015-02-19 19:15:19 +00001677void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
1678 if (CanVecMem) {
Adam Nemet7cdebac2015-07-14 22:32:44 +00001679 if (PtrRtChecking.Need)
Adam Nemete91cc6e2015-02-19 19:15:19 +00001680 OS.indent(Depth) << "Memory dependences are safe with run-time checks\n";
Adam Nemet26da8e92015-04-14 01:12:55 +00001681 else
1682 OS.indent(Depth) << "Memory dependences are safe\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +00001683 }
1684
1685 if (Report)
1686 OS.indent(Depth) << "Report: " << Report->str() << "\n";
1687
Adam Nemet58913d62015-03-10 17:40:43 +00001688 if (auto *InterestingDependences = DepChecker.getInterestingDependences()) {
1689 OS.indent(Depth) << "Interesting Dependences:\n";
1690 for (auto &Dep : *InterestingDependences) {
1691 Dep.print(OS, Depth + 2, DepChecker.getMemoryInstructions());
1692 OS << "\n";
1693 }
1694 } else
1695 OS.indent(Depth) << "Too many interesting dependences, not recorded\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +00001696
1697 // List the pair of accesses need run-time checks to prove independence.
Adam Nemet7cdebac2015-07-14 22:32:44 +00001698 PtrRtChecking.print(OS, Depth);
Adam Nemete91cc6e2015-02-19 19:15:19 +00001699 OS << "\n";
Adam Nemetc3384322015-05-18 15:36:57 +00001700
1701 OS.indent(Depth) << "Store to invariant address was "
1702 << (StoreToLoopInvariantAddress ? "" : "not ")
1703 << "found in loop.\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +00001704}
1705
Adam Nemet8bc61df2015-02-24 00:41:59 +00001706const LoopAccessInfo &
1707LoopAccessAnalysis::getInfo(Loop *L, const ValueToValueMap &Strides) {
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001708 auto &LAI = LoopAccessInfoMap[L];
1709
1710#ifndef NDEBUG
1711 assert((!LAI || LAI->NumSymbolicStrides == Strides.size()) &&
1712 "Symbolic strides changed for loop");
1713#endif
1714
1715 if (!LAI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001716 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Adam Nemete2b885c2015-04-23 20:09:20 +00001717 LAI = llvm::make_unique<LoopAccessInfo>(L, SE, DL, TLI, AA, DT, LI,
1718 Strides);
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001719#ifndef NDEBUG
1720 LAI->NumSymbolicStrides = Strides.size();
1721#endif
1722 }
1723 return *LAI.get();
1724}
1725
Adam Nemete91cc6e2015-02-19 19:15:19 +00001726void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const {
1727 LoopAccessAnalysis &LAA = *const_cast<LoopAccessAnalysis *>(this);
1728
Adam Nemete91cc6e2015-02-19 19:15:19 +00001729 ValueToValueMap NoSymbolicStrides;
1730
1731 for (Loop *TopLevelLoop : *LI)
1732 for (Loop *L : depth_first(TopLevelLoop)) {
1733 OS.indent(2) << L->getHeader()->getName() << ":\n";
1734 auto &LAI = LAA.getInfo(L, NoSymbolicStrides);
1735 LAI.print(OS, 4);
1736 }
1737}
1738
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001739bool LoopAccessAnalysis::runOnFunction(Function &F) {
1740 SE = &getAnalysis<ScalarEvolution>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001741 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
1742 TLI = TLIP ? &TLIP->getTLI() : nullptr;
1743 AA = &getAnalysis<AliasAnalysis>();
1744 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Adam Nemete2b885c2015-04-23 20:09:20 +00001745 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001746
1747 return false;
1748}
1749
1750void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
1751 AU.addRequired<ScalarEvolution>();
1752 AU.addRequired<AliasAnalysis>();
1753 AU.addRequired<DominatorTreeWrapperPass>();
Adam Nemete91cc6e2015-02-19 19:15:19 +00001754 AU.addRequired<LoopInfoWrapperPass>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001755
1756 AU.setPreservesAll();
1757}
1758
1759char LoopAccessAnalysis::ID = 0;
1760static const char laa_name[] = "Loop Access Analysis";
1761#define LAA_NAME "loop-accesses"
1762
1763INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1764INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
1765INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1766INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Adam Nemete91cc6e2015-02-19 19:15:19 +00001767INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001768INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1769
1770namespace llvm {
1771 Pass *createLAAPass() {
1772 return new LoopAccessAnalysis();
1773 }
1774}