blob: 72ee33c41c35613ba31340ccafbaf9f07e12c32d [file] [log] [blame]
Sebastian Pop59b61b92012-10-11 07:32:34 +00001//===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
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// DependenceAnalysis is an LLVM pass that analyses dependences between memory
11// accesses. Currently, it is an (incomplete) implementation of the approach
12// described in
13//
14// Practical Dependence Testing
15// Goff, Kennedy, Tseng
16// PLDI 1991
17//
18// There's a single entry point that analyzes the dependence between a pair
19// of memory references in a function, returning either NULL, for no dependence,
20// or a more-or-less detailed description of the dependence between them.
21//
22// Currently, the implementation cannot propagate constraints between
23// coupled RDIV subscripts and lacks a multi-subscript MIV test.
24// Both of these are conservative weaknesses;
25// that is, not a source of correctness problems.
26//
Sebastian Popbf6e1c22018-03-06 21:55:59 +000027// Since Clang linearizes some array subscripts, the dependence
Sebastian Pop7ee14722013-11-13 22:37:58 +000028// analysis is using SCEV->delinearize to recover the representation of multiple
29// subscripts, and thus avoid the more expensive and less precise MIV tests. The
30// delinearization is controlled by the flag -da-delinearize.
Sebastian Pop59b61b92012-10-11 07:32:34 +000031//
32// We should pay some careful attention to the possibility of integer overflow
33// in the implementation of the various tests. This could happen with Add,
34// Subtract, or Multiply, with both APInt's and SCEV's.
35//
36// Some non-linear subscript pairs can be handled by the GCD test
37// (and perhaps other tests).
38// Should explore how often these things occur.
39//
40// Finally, it seems like certain test cases expose weaknesses in the SCEV
41// simplification, especially in the handling of sign and zero extensions.
42// It could be useful to spend time exploring these.
43//
44// Please note that this is work in progress and the interface is subject to
45// change.
46//
47//===----------------------------------------------------------------------===//
48// //
49// In memory of Ken Kennedy, 1945 - 2007 //
50// //
51//===----------------------------------------------------------------------===//
52
Sebastian Pop59b61b92012-10-11 07:32:34 +000053#include "llvm/Analysis/DependenceAnalysis.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000054#include "llvm/ADT/STLExtras.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000055#include "llvm/ADT/Statistic.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000056#include "llvm/Analysis/AliasAnalysis.h"
57#include "llvm/Analysis/LoopInfo.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000058#include "llvm/Analysis/ScalarEvolution.h"
59#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000060#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth83948572014-03-04 10:30:26 +000061#include "llvm/IR/InstIterator.h"
Mehdi Aminia28d91d2015-03-10 02:37:25 +000062#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000063#include "llvm/IR/Operator.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000064#include "llvm/Support/CommandLine.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000065#include "llvm/Support/Debug.h"
66#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000067#include "llvm/Support/raw_ostream.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000068
69using namespace llvm;
70
Chandler Carruthf1221bd2014-04-22 02:48:03 +000071#define DEBUG_TYPE "da"
72
Sebastian Pop59b61b92012-10-11 07:32:34 +000073//===----------------------------------------------------------------------===//
74// statistics
75
76STATISTIC(TotalArrayPairs, "Array pairs tested");
77STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
78STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
79STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
80STATISTIC(ZIVapplications, "ZIV applications");
81STATISTIC(ZIVindependence, "ZIV independence");
82STATISTIC(StrongSIVapplications, "Strong SIV applications");
83STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
84STATISTIC(StrongSIVindependence, "Strong SIV independence");
85STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
86STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
87STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
88STATISTIC(ExactSIVapplications, "Exact SIV applications");
89STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
90STATISTIC(ExactSIVindependence, "Exact SIV independence");
91STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
92STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
93STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
94STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
95STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
96STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
97STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
98STATISTIC(DeltaApplications, "Delta applications");
99STATISTIC(DeltaSuccesses, "Delta successes");
100STATISTIC(DeltaIndependence, "Delta independence");
101STATISTIC(DeltaPropagations, "Delta propagations");
102STATISTIC(GCDapplications, "GCD applications");
103STATISTIC(GCDsuccesses, "GCD successes");
104STATISTIC(GCDindependence, "GCD independence");
105STATISTIC(BanerjeeApplications, "Banerjee applications");
106STATISTIC(BanerjeeIndependence, "Banerjee independence");
107STATISTIC(BanerjeeSuccesses, "Banerjee successes");
108
Sebastian Popc62c6792013-11-12 22:47:20 +0000109static cl::opt<bool>
110Delinearize("da-delinearize", cl::init(false), cl::Hidden, cl::ZeroOrMore,
111 cl::desc("Try to delinearize array references."));
112
Sebastian Pop59b61b92012-10-11 07:32:34 +0000113//===----------------------------------------------------------------------===//
114// basics
115
Chandler Carruth49c22192016-05-12 22:19:39 +0000116DependenceAnalysis::Result
117DependenceAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
118 auto &AA = FAM.getResult<AAManager>(F);
119 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
120 auto &LI = FAM.getResult<LoopAnalysis>(F);
121 return DependenceInfo(&F, &AA, &SE, &LI);
122}
123
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000124AnalysisKey DependenceAnalysis::Key;
Chandler Carruth49c22192016-05-12 22:19:39 +0000125
126INITIALIZE_PASS_BEGIN(DependenceAnalysisWrapperPass, "da",
Sebastian Pop59b61b92012-10-11 07:32:34 +0000127 "Dependence Analysis", true, true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000128INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000129INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000130INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Chandler Carruth49c22192016-05-12 22:19:39 +0000131INITIALIZE_PASS_END(DependenceAnalysisWrapperPass, "da", "Dependence Analysis",
132 true, true)
Sebastian Pop59b61b92012-10-11 07:32:34 +0000133
Chandler Carruth49c22192016-05-12 22:19:39 +0000134char DependenceAnalysisWrapperPass::ID = 0;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000135
Chandler Carruth49c22192016-05-12 22:19:39 +0000136FunctionPass *llvm::createDependenceAnalysisWrapperPass() {
137 return new DependenceAnalysisWrapperPass();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000138}
139
Chandler Carruth49c22192016-05-12 22:19:39 +0000140bool DependenceAnalysisWrapperPass::runOnFunction(Function &F) {
141 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
142 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
143 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
144 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
Sebastian Pop59b61b92012-10-11 07:32:34 +0000145 return false;
146}
147
Chandler Carruth49c22192016-05-12 22:19:39 +0000148DependenceInfo &DependenceAnalysisWrapperPass::getDI() const { return *info; }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000149
Chandler Carruth49c22192016-05-12 22:19:39 +0000150void DependenceAnalysisWrapperPass::releaseMemory() { info.reset(); }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000151
Chandler Carruth49c22192016-05-12 22:19:39 +0000152void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000153 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000154 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000155 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000156 AU.addRequiredTransitive<LoopInfoWrapperPass>();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000157}
158
159
160// Used to test the dependence analyzer.
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000161// Looks through the function, noting loads and stores.
162// Calls depends() on every possible pair and prints out the result.
Sebastian Pop59b61b92012-10-11 07:32:34 +0000163// Ignores all other instructions.
Chandler Carruth49c22192016-05-12 22:19:39 +0000164static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA) {
165 auto *F = DA->getFunction();
166 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
167 ++SrcI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000168 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000169 for (inst_iterator DstI = SrcI, DstE = inst_end(F);
170 DstI != DstE; ++DstI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000171 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000172 OS << "da analyze - ";
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +0000173 if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000174 D->dump(OS);
175 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
176 if (D->isSplitable(Level)) {
177 OS << "da analyze - split level = " << Level;
Dylan Noblesmithd96ce662014-08-25 00:28:35 +0000178 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
Sebastian Pop59b61b92012-10-11 07:32:34 +0000179 OS << "!\n";
180 }
181 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000182 }
183 else
184 OS << "none!\n";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000185 }
186 }
187 }
188 }
189}
190
Chandler Carruth49c22192016-05-12 22:19:39 +0000191void DependenceAnalysisWrapperPass::print(raw_ostream &OS,
192 const Module *) const {
193 dumpExampleDependence(OS, info.get());
Sebastian Pop59b61b92012-10-11 07:32:34 +0000194}
195
196//===----------------------------------------------------------------------===//
197// Dependence methods
198
199// Returns true if this is an input dependence.
200bool Dependence::isInput() const {
201 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
202}
203
204
205// Returns true if this is an output dependence.
206bool Dependence::isOutput() const {
207 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
208}
209
210
211// Returns true if this is an flow (aka true) dependence.
212bool Dependence::isFlow() const {
213 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
214}
215
216
217// Returns true if this is an anti dependence.
218bool Dependence::isAnti() const {
219 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
220}
221
222
223// Returns true if a particular level is scalar; that is,
224// if no subscript in the source or destination mention the induction
225// variable associated with the loop at this level.
226// Leave this out of line, so it will serve as a virtual method anchor
227bool Dependence::isScalar(unsigned level) const {
228 return false;
229}
230
231
232//===----------------------------------------------------------------------===//
233// FullDependence methods
234
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000235FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
Sebastian Pop59b61b92012-10-11 07:32:34 +0000236 bool PossiblyLoopIndependent,
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000237 unsigned CommonLevels)
238 : Dependence(Source, Destination), Levels(CommonLevels),
239 LoopIndependent(PossiblyLoopIndependent) {
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000240 Consistent = true;
David Blaikie47039dc2015-07-31 21:37:09 +0000241 if (CommonLevels)
242 DV = make_unique<DVEntry[]>(CommonLevels);
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000243}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000244
245// The rest are simple getters that hide the implementation.
246
247// getDirection - Returns the direction associated with a particular level.
248unsigned FullDependence::getDirection(unsigned Level) const {
249 assert(0 < Level && Level <= Levels && "Level out of range");
250 return DV[Level - 1].Direction;
251}
252
253
254// Returns the distance (or NULL) associated with a particular level.
255const SCEV *FullDependence::getDistance(unsigned Level) const {
256 assert(0 < Level && Level <= Levels && "Level out of range");
257 return DV[Level - 1].Distance;
258}
259
260
261// Returns true if a particular level is scalar; that is,
262// if no subscript in the source or destination mention the induction
263// variable associated with the loop at this level.
264bool FullDependence::isScalar(unsigned Level) const {
265 assert(0 < Level && Level <= Levels && "Level out of range");
266 return DV[Level - 1].Scalar;
267}
268
269
270// Returns true if peeling the first iteration from this loop
271// will break this dependence.
272bool FullDependence::isPeelFirst(unsigned Level) const {
273 assert(0 < Level && Level <= Levels && "Level out of range");
274 return DV[Level - 1].PeelFirst;
275}
276
277
278// Returns true if peeling the last iteration from this loop
279// will break this dependence.
280bool FullDependence::isPeelLast(unsigned Level) const {
281 assert(0 < Level && Level <= Levels && "Level out of range");
282 return DV[Level - 1].PeelLast;
283}
284
285
286// Returns true if splitting this loop will break the dependence.
287bool FullDependence::isSplitable(unsigned Level) const {
288 assert(0 < Level && Level <= Levels && "Level out of range");
289 return DV[Level - 1].Splitable;
290}
291
292
293//===----------------------------------------------------------------------===//
Chandler Carruth49c22192016-05-12 22:19:39 +0000294// DependenceInfo::Constraint methods
Sebastian Pop59b61b92012-10-11 07:32:34 +0000295
296// If constraint is a point <X, Y>, returns X.
297// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000298const SCEV *DependenceInfo::Constraint::getX() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000299 assert(Kind == Point && "Kind should be Point");
300 return A;
301}
302
303
304// If constraint is a point <X, Y>, returns Y.
305// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000306const SCEV *DependenceInfo::Constraint::getY() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000307 assert(Kind == Point && "Kind should be Point");
308 return B;
309}
310
311
312// If constraint is a line AX + BY = C, returns A.
313// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000314const SCEV *DependenceInfo::Constraint::getA() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000315 assert((Kind == Line || Kind == Distance) &&
316 "Kind should be Line (or Distance)");
317 return A;
318}
319
320
321// If constraint is a line AX + BY = C, returns B.
322// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000323const SCEV *DependenceInfo::Constraint::getB() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000324 assert((Kind == Line || Kind == Distance) &&
325 "Kind should be Line (or Distance)");
326 return B;
327}
328
329
330// If constraint is a line AX + BY = C, returns C.
331// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000332const SCEV *DependenceInfo::Constraint::getC() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000333 assert((Kind == Line || Kind == Distance) &&
334 "Kind should be Line (or Distance)");
335 return C;
336}
337
338
339// If constraint is a distance, returns D.
340// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000341const SCEV *DependenceInfo::Constraint::getD() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000342 assert(Kind == Distance && "Kind should be Distance");
343 return SE->getNegativeSCEV(C);
344}
345
346
347// Returns the loop associated with this constraint.
Chandler Carruth49c22192016-05-12 22:19:39 +0000348const Loop *DependenceInfo::Constraint::getAssociatedLoop() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000349 assert((Kind == Distance || Kind == Line || Kind == Point) &&
350 "Kind should be Distance, Line, or Point");
351 return AssociatedLoop;
352}
353
Chandler Carruth49c22192016-05-12 22:19:39 +0000354void DependenceInfo::Constraint::setPoint(const SCEV *X, const SCEV *Y,
355 const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000356 Kind = Point;
357 A = X;
358 B = Y;
359 AssociatedLoop = CurLoop;
360}
361
Chandler Carruth49c22192016-05-12 22:19:39 +0000362void DependenceInfo::Constraint::setLine(const SCEV *AA, const SCEV *BB,
363 const SCEV *CC, const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000364 Kind = Line;
365 A = AA;
366 B = BB;
367 C = CC;
368 AssociatedLoop = CurLoop;
369}
370
Chandler Carruth49c22192016-05-12 22:19:39 +0000371void DependenceInfo::Constraint::setDistance(const SCEV *D,
372 const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000373 Kind = Distance;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000374 A = SE->getOne(D->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +0000375 B = SE->getNegativeSCEV(A);
376 C = SE->getNegativeSCEV(D);
377 AssociatedLoop = CurLoop;
378}
379
Chandler Carruth49c22192016-05-12 22:19:39 +0000380void DependenceInfo::Constraint::setEmpty() { Kind = Empty; }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000381
Chandler Carruth49c22192016-05-12 22:19:39 +0000382void DependenceInfo::Constraint::setAny(ScalarEvolution *NewSE) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000383 SE = NewSE;
384 Kind = Any;
385}
386
Aaron Ballman615eb472017-10-15 14:32:27 +0000387#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sebastian Pop59b61b92012-10-11 07:32:34 +0000388// For debugging purposes. Dumps the constraint out to OS.
Matthias Braun8c209aa2017-01-28 02:02:38 +0000389LLVM_DUMP_METHOD void DependenceInfo::Constraint::dump(raw_ostream &OS) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000390 if (isEmpty())
391 OS << " Empty\n";
392 else if (isAny())
393 OS << " Any\n";
394 else if (isPoint())
395 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
396 else if (isDistance())
397 OS << " Distance is " << *getD() <<
398 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
399 else if (isLine())
400 OS << " Line is " << *getA() << "*X + " <<
401 *getB() << "*Y = " << *getC() << "\n";
402 else
403 llvm_unreachable("unknown constraint type in Constraint::dump");
404}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000405#endif
Sebastian Pop59b61b92012-10-11 07:32:34 +0000406
407
408// Updates X with the intersection
409// of the Constraints X and Y. Returns true if X has changed.
410// Corresponds to Figure 4 from the paper
411//
412// Practical Dependence Testing
413// Goff, Kennedy, Tseng
414// PLDI 1991
Chandler Carruth49c22192016-05-12 22:19:39 +0000415bool DependenceInfo::intersectConstraints(Constraint *X, const Constraint *Y) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000416 ++DeltaApplications;
417 DEBUG(dbgs() << "\tintersect constraints\n");
418 DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
419 DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
420 assert(!Y->isPoint() && "Y must not be a Point");
421 if (X->isAny()) {
422 if (Y->isAny())
423 return false;
424 *X = *Y;
425 return true;
426 }
427 if (X->isEmpty())
428 return false;
429 if (Y->isEmpty()) {
430 X->setEmpty();
431 return true;
432 }
433
434 if (X->isDistance() && Y->isDistance()) {
435 DEBUG(dbgs() << "\t intersect 2 distances\n");
436 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
437 return false;
438 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
439 X->setEmpty();
440 ++DeltaSuccesses;
441 return true;
442 }
443 // Hmmm, interesting situation.
444 // I guess if either is constant, keep it and ignore the other.
445 if (isa<SCEVConstant>(Y->getD())) {
446 *X = *Y;
447 return true;
448 }
449 return false;
450 }
451
452 // At this point, the pseudo-code in Figure 4 of the paper
453 // checks if (X->isPoint() && Y->isPoint()).
454 // This case can't occur in our implementation,
455 // since a Point can only arise as the result of intersecting
456 // two Line constraints, and the right-hand value, Y, is never
457 // the result of an intersection.
458 assert(!(X->isPoint() && Y->isPoint()) &&
459 "We shouldn't ever see X->isPoint() && Y->isPoint()");
460
461 if (X->isLine() && Y->isLine()) {
462 DEBUG(dbgs() << "\t intersect 2 lines\n");
463 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
464 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
465 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
466 // slopes are equal, so lines are parallel
467 DEBUG(dbgs() << "\t\tsame slope\n");
468 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
469 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
470 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
471 return false;
472 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
473 X->setEmpty();
474 ++DeltaSuccesses;
475 return true;
476 }
477 return false;
478 }
479 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
480 // slopes differ, so lines intersect
481 DEBUG(dbgs() << "\t\tdifferent slopes\n");
482 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
483 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
484 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
485 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
486 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
487 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
488 const SCEVConstant *C1A2_C2A1 =
489 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
490 const SCEVConstant *C1B2_C2B1 =
491 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
492 const SCEVConstant *A1B2_A2B1 =
493 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
494 const SCEVConstant *A2B1_A1B2 =
495 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
496 if (!C1B2_C2B1 || !C1A2_C2A1 ||
497 !A1B2_A2B1 || !A2B1_A1B2)
498 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000499 APInt Xtop = C1B2_C2B1->getAPInt();
500 APInt Xbot = A1B2_A2B1->getAPInt();
501 APInt Ytop = C1A2_C2A1->getAPInt();
502 APInt Ybot = A2B1_A1B2->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000503 DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
504 DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
505 DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
506 DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
507 APInt Xq = Xtop; // these need to be initialized, even
508 APInt Xr = Xtop; // though they're just going to be overwritten
509 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
510 APInt Yq = Ytop;
Jakub Staszak340c7802013-08-06 16:40:40 +0000511 APInt Yr = Ytop;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000512 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
513 if (Xr != 0 || Yr != 0) {
514 X->setEmpty();
515 ++DeltaSuccesses;
516 return true;
517 }
518 DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
519 if (Xq.slt(0) || Yq.slt(0)) {
520 X->setEmpty();
521 ++DeltaSuccesses;
522 return true;
523 }
524 if (const SCEVConstant *CUB =
525 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000526 const APInt &UpperBound = CUB->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000527 DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
528 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
529 X->setEmpty();
530 ++DeltaSuccesses;
531 return true;
532 }
533 }
534 X->setPoint(SE->getConstant(Xq),
535 SE->getConstant(Yq),
536 X->getAssociatedLoop());
537 ++DeltaSuccesses;
538 return true;
539 }
540 return false;
541 }
542
543 // if (X->isLine() && Y->isPoint()) This case can't occur.
544 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
545
546 if (X->isPoint() && Y->isLine()) {
547 DEBUG(dbgs() << "\t intersect Point and Line\n");
548 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
549 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
550 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
551 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
552 return false;
553 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
554 X->setEmpty();
555 ++DeltaSuccesses;
556 return true;
557 }
558 return false;
559 }
560
561 llvm_unreachable("shouldn't reach the end of Constraint intersection");
562 return false;
563}
564
565
566//===----------------------------------------------------------------------===//
Chandler Carruth49c22192016-05-12 22:19:39 +0000567// DependenceInfo methods
Sebastian Pop59b61b92012-10-11 07:32:34 +0000568
569// For debugging purposes. Dumps a dependence to OS.
570void Dependence::dump(raw_ostream &OS) const {
571 bool Splitable = false;
572 if (isConfused())
573 OS << "confused";
574 else {
575 if (isConsistent())
576 OS << "consistent ";
577 if (isFlow())
578 OS << "flow";
579 else if (isOutput())
580 OS << "output";
581 else if (isAnti())
582 OS << "anti";
583 else if (isInput())
584 OS << "input";
585 unsigned Levels = getLevels();
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000586 OS << " [";
587 for (unsigned II = 1; II <= Levels; ++II) {
588 if (isSplitable(II))
589 Splitable = true;
590 if (isPeelFirst(II))
591 OS << 'p';
592 const SCEV *Distance = getDistance(II);
593 if (Distance)
594 OS << *Distance;
595 else if (isScalar(II))
596 OS << "S";
597 else {
598 unsigned Direction = getDirection(II);
599 if (Direction == DVEntry::ALL)
600 OS << "*";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000601 else {
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000602 if (Direction & DVEntry::LT)
603 OS << "<";
604 if (Direction & DVEntry::EQ)
605 OS << "=";
606 if (Direction & DVEntry::GT)
607 OS << ">";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000608 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000609 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000610 if (isPeelLast(II))
611 OS << 'p';
612 if (II < Levels)
613 OS << " ";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000614 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000615 if (isLoopIndependent())
616 OS << "|<";
617 OS << "]";
618 if (Splitable)
619 OS << " splitable";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000620 }
621 OS << "!\n";
622}
623
David Green5ef933b2018-04-10 11:37:21 +0000624// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
625// underlaying objects. If LocA and LocB are known to not alias (for any reason:
626// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
627// Otherwise the underlying objects are checked to see if they point to
628// different identifiable objects.
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000629static AliasResult underlyingObjectsAlias(AliasAnalysis *AA,
David Green5ef933b2018-04-10 11:37:21 +0000630 const DataLayout &DL,
631 const MemoryLocation &LocA,
632 const MemoryLocation &LocB) {
633 // Check the original locations (minus size) for noalias, which can happen for
634 // tbaa, incompatible underlying object locations, etc.
635 MemoryLocation LocAS(LocA.Ptr, MemoryLocation::UnknownSize, LocA.AATags);
636 MemoryLocation LocBS(LocB.Ptr, MemoryLocation::UnknownSize, LocB.AATags);
637 if (AA->alias(LocAS, LocBS) == NoAlias)
638 return NoAlias;
639
640 // Check the underlying objects are the same
641 const Value *AObj = GetUnderlyingObject(LocA.Ptr, DL);
642 const Value *BObj = GetUnderlyingObject(LocB.Ptr, DL);
643
644 // If the underlying objects are the same, they must alias
645 if (AObj == BObj)
646 return MustAlias;
647
648 // We may have hit the recursion limit for underlying objects, or have
649 // underlying objects where we don't know they will alias.
650 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
651 return MayAlias;
652
653 // Otherwise we know the objects are different and both identified objects so
654 // must not alias.
655 return NoAlias;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000656}
657
658
659// Returns true if the load or store can be analyzed. Atomic and volatile
660// operations have properties which this analysis does not understand.
661static
662bool isLoadOrStore(const Instruction *I) {
663 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
664 return LI->isUnordered();
665 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
666 return SI->isUnordered();
667 return false;
668}
669
670
Sebastian Pop59b61b92012-10-11 07:32:34 +0000671// Examines the loop nesting of the Src and Dst
672// instructions and establishes their shared loops. Sets the variables
673// CommonLevels, SrcLevels, and MaxLevels.
674// The source and destination instructions needn't be contained in the same
675// loop. The routine establishNestingLevels finds the level of most deeply
676// nested loop that contains them both, CommonLevels. An instruction that's
677// not contained in a loop is at level = 0. MaxLevels is equal to the level
678// of the source plus the level of the destination, minus CommonLevels.
679// This lets us allocate vectors MaxLevels in length, with room for every
680// distinct loop referenced in both the source and destination subscripts.
681// The variable SrcLevels is the nesting depth of the source instruction.
682// It's used to help calculate distinct loops referenced by the destination.
683// Here's the map from loops to levels:
684// 0 - unused
685// 1 - outermost common loop
686// ... - other common loops
687// CommonLevels - innermost common loop
688// ... - loops containing Src but not Dst
689// SrcLevels - innermost loop containing Src but not Dst
690// ... - loops containing Dst but not Src
691// MaxLevels - innermost loops containing Dst but not Src
692// Consider the follow code fragment:
693// for (a = ...) {
694// for (b = ...) {
695// for (c = ...) {
696// for (d = ...) {
697// A[] = ...;
698// }
699// }
700// for (e = ...) {
701// for (f = ...) {
702// for (g = ...) {
703// ... = A[];
704// }
705// }
706// }
707// }
708// }
709// If we're looking at the possibility of a dependence between the store
710// to A (the Src) and the load from A (the Dst), we'll note that they
711// have 2 loops in common, so CommonLevels will equal 2 and the direction
712// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
713// A map from loop names to loop numbers would look like
714// a - 1
715// b - 2 = CommonLevels
716// c - 3
717// d - 4 = SrcLevels
718// e - 5
719// f - 6
720// g - 7 = MaxLevels
Chandler Carruth49c22192016-05-12 22:19:39 +0000721void DependenceInfo::establishNestingLevels(const Instruction *Src,
722 const Instruction *Dst) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000723 const BasicBlock *SrcBlock = Src->getParent();
724 const BasicBlock *DstBlock = Dst->getParent();
725 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
726 unsigned DstLevel = LI->getLoopDepth(DstBlock);
727 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
728 const Loop *DstLoop = LI->getLoopFor(DstBlock);
729 SrcLevels = SrcLevel;
730 MaxLevels = SrcLevel + DstLevel;
731 while (SrcLevel > DstLevel) {
732 SrcLoop = SrcLoop->getParentLoop();
733 SrcLevel--;
734 }
735 while (DstLevel > SrcLevel) {
736 DstLoop = DstLoop->getParentLoop();
737 DstLevel--;
738 }
739 while (SrcLoop != DstLoop) {
740 SrcLoop = SrcLoop->getParentLoop();
741 DstLoop = DstLoop->getParentLoop();
742 SrcLevel--;
743 }
744 CommonLevels = SrcLevel;
745 MaxLevels -= CommonLevels;
746}
747
748
749// Given one of the loops containing the source, return
750// its level index in our numbering scheme.
Chandler Carruth49c22192016-05-12 22:19:39 +0000751unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000752 return SrcLoop->getLoopDepth();
753}
754
755
756// Given one of the loops containing the destination,
757// return its level index in our numbering scheme.
Chandler Carruth49c22192016-05-12 22:19:39 +0000758unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000759 unsigned D = DstLoop->getLoopDepth();
760 if (D > CommonLevels)
761 return D - CommonLevels + SrcLevels;
762 else
763 return D;
764}
765
766
767// Returns true if Expression is loop invariant in LoopNest.
Chandler Carruth49c22192016-05-12 22:19:39 +0000768bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
769 const Loop *LoopNest) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000770 if (!LoopNest)
771 return true;
772 return SE->isLoopInvariant(Expression, LoopNest) &&
773 isLoopInvariant(Expression, LoopNest->getParentLoop());
774}
775
776
777
778// Finds the set of loops from the LoopNest that
779// have a level <= CommonLevels and are referred to by the SCEV Expression.
Chandler Carruth49c22192016-05-12 22:19:39 +0000780void DependenceInfo::collectCommonLoops(const SCEV *Expression,
781 const Loop *LoopNest,
782 SmallBitVector &Loops) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000783 while (LoopNest) {
784 unsigned Level = LoopNest->getLoopDepth();
785 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
786 Loops.set(Level);
787 LoopNest = LoopNest->getParentLoop();
788 }
789}
790
Chandler Carruth49c22192016-05-12 22:19:39 +0000791void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
Jingyue Wua84feb12015-05-29 16:58:08 +0000792
793 unsigned widestWidthSeen = 0;
794 Type *widestType;
795
796 // Go through each pair and find the widest bit to which we need
797 // to extend all of them.
Benjamin Krameraa209152016-06-26 17:27:42 +0000798 for (Subscript *Pair : Pairs) {
799 const SCEV *Src = Pair->Src;
800 const SCEV *Dst = Pair->Dst;
Jingyue Wua84feb12015-05-29 16:58:08 +0000801 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
802 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
803 if (SrcTy == nullptr || DstTy == nullptr) {
804 assert(SrcTy == DstTy && "This function only unify integer types and "
805 "expect Src and Dst share the same type "
806 "otherwise.");
807 continue;
808 }
809 if (SrcTy->getBitWidth() > widestWidthSeen) {
810 widestWidthSeen = SrcTy->getBitWidth();
811 widestType = SrcTy;
812 }
813 if (DstTy->getBitWidth() > widestWidthSeen) {
814 widestWidthSeen = DstTy->getBitWidth();
815 widestType = DstTy;
816 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000817 }
Jingyue Wua84feb12015-05-29 16:58:08 +0000818
819
820 assert(widestWidthSeen > 0);
821
822 // Now extend each pair to the widest seen.
Benjamin Krameraa209152016-06-26 17:27:42 +0000823 for (Subscript *Pair : Pairs) {
824 const SCEV *Src = Pair->Src;
825 const SCEV *Dst = Pair->Dst;
Jingyue Wua84feb12015-05-29 16:58:08 +0000826 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
827 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
828 if (SrcTy == nullptr || DstTy == nullptr) {
829 assert(SrcTy == DstTy && "This function only unify integer types and "
830 "expect Src and Dst share the same type "
831 "otherwise.");
832 continue;
833 }
834 if (SrcTy->getBitWidth() < widestWidthSeen)
835 // Sign-extend Src to widestType
Benjamin Krameraa209152016-06-26 17:27:42 +0000836 Pair->Src = SE->getSignExtendExpr(Src, widestType);
Jingyue Wua84feb12015-05-29 16:58:08 +0000837 if (DstTy->getBitWidth() < widestWidthSeen) {
838 // Sign-extend Dst to widestType
Benjamin Krameraa209152016-06-26 17:27:42 +0000839 Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
Jingyue Wua84feb12015-05-29 16:58:08 +0000840 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000841 }
842}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000843
844// removeMatchingExtensions - Examines a subscript pair.
845// If the source and destination are identically sign (or zero)
846// extended, it strips off the extension in an effect to simplify
847// the actual analysis.
Chandler Carruth49c22192016-05-12 22:19:39 +0000848void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000849 const SCEV *Src = Pair->Src;
850 const SCEV *Dst = Pair->Dst;
851 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
852 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
853 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src);
854 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst);
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000855 const SCEV *SrcCastOp = SrcCast->getOperand();
856 const SCEV *DstCastOp = DstCast->getOperand();
857 if (SrcCastOp->getType() == DstCastOp->getType()) {
858 Pair->Src = SrcCastOp;
859 Pair->Dst = DstCastOp;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000860 }
861 }
862}
863
864
865// Examine the scev and return true iff it's linear.
866// Collect any loops mentioned in the set of "Loops".
Chandler Carruth49c22192016-05-12 22:19:39 +0000867bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
868 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000869 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src);
870 if (!AddRec)
871 return isLoopInvariant(Src, LoopNest);
872 const SCEV *Start = AddRec->getStart();
873 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000874 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
875 if (!isa<SCEVCouldNotCompute>(UB)) {
876 if (SE->getTypeSizeInBits(Start->getType()) <
877 SE->getTypeSizeInBits(UB->getType())) {
878 if (!AddRec->getNoWrapFlags())
879 return false;
880 }
881 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000882 if (!isLoopInvariant(Step, LoopNest))
883 return false;
884 Loops.set(mapSrcLoop(AddRec->getLoop()));
885 return checkSrcSubscript(Start, LoopNest, Loops);
886}
887
888
889
890// Examine the scev and return true iff it's linear.
891// Collect any loops mentioned in the set of "Loops".
Chandler Carruth49c22192016-05-12 22:19:39 +0000892bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
893 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000894 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst);
895 if (!AddRec)
896 return isLoopInvariant(Dst, LoopNest);
897 const SCEV *Start = AddRec->getStart();
898 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000899 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
900 if (!isa<SCEVCouldNotCompute>(UB)) {
901 if (SE->getTypeSizeInBits(Start->getType()) <
902 SE->getTypeSizeInBits(UB->getType())) {
903 if (!AddRec->getNoWrapFlags())
904 return false;
905 }
906 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000907 if (!isLoopInvariant(Step, LoopNest))
908 return false;
909 Loops.set(mapDstLoop(AddRec->getLoop()));
910 return checkDstSubscript(Start, LoopNest, Loops);
911}
912
913
914// Examines the subscript pair (the Src and Dst SCEVs)
915// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
916// Collects the associated loops in a set.
Chandler Carruth49c22192016-05-12 22:19:39 +0000917DependenceInfo::Subscript::ClassificationKind
918DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
919 const SCEV *Dst, const Loop *DstLoopNest,
920 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000921 SmallBitVector SrcLoops(MaxLevels + 1);
922 SmallBitVector DstLoops(MaxLevels + 1);
923 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
924 return Subscript::NonLinear;
925 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
926 return Subscript::NonLinear;
927 Loops = SrcLoops;
928 Loops |= DstLoops;
929 unsigned N = Loops.count();
930 if (N == 0)
931 return Subscript::ZIV;
932 if (N == 1)
933 return Subscript::SIV;
934 if (N == 2 && (SrcLoops.count() == 0 ||
935 DstLoops.count() == 0 ||
936 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
937 return Subscript::RDIV;
938 return Subscript::MIV;
939}
940
941
942// A wrapper around SCEV::isKnownPredicate.
943// Looks for cases where we're interested in comparing for equality.
944// If both X and Y have been identically sign or zero extended,
945// it strips off the (confusing) extensions before invoking
946// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
947// will be similarly updated.
948//
949// If SCEV::isKnownPredicate can't prove the predicate,
950// we try simple subtraction, which seems to help in some cases
951// involving symbolics.
Chandler Carruth49c22192016-05-12 22:19:39 +0000952bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
953 const SCEV *Y) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000954 if (Pred == CmpInst::ICMP_EQ ||
955 Pred == CmpInst::ICMP_NE) {
956 if ((isa<SCEVSignExtendExpr>(X) &&
957 isa<SCEVSignExtendExpr>(Y)) ||
958 (isa<SCEVZeroExtendExpr>(X) &&
959 isa<SCEVZeroExtendExpr>(Y))) {
960 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X);
961 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y);
962 const SCEV *Xop = CX->getOperand();
963 const SCEV *Yop = CY->getOperand();
964 if (Xop->getType() == Yop->getType()) {
965 X = Xop;
966 Y = Yop;
967 }
968 }
969 }
970 if (SE->isKnownPredicate(Pred, X, Y))
971 return true;
972 // If SE->isKnownPredicate can't prove the condition,
973 // we try the brute-force approach of subtracting
974 // and testing the difference.
975 // By testing with SE->isKnownPredicate first, we avoid
976 // the possibility of overflow when the arguments are constants.
977 const SCEV *Delta = SE->getMinusSCEV(X, Y);
978 switch (Pred) {
979 case CmpInst::ICMP_EQ:
980 return Delta->isZero();
981 case CmpInst::ICMP_NE:
982 return SE->isKnownNonZero(Delta);
983 case CmpInst::ICMP_SGE:
984 return SE->isKnownNonNegative(Delta);
985 case CmpInst::ICMP_SLE:
986 return SE->isKnownNonPositive(Delta);
987 case CmpInst::ICMP_SGT:
988 return SE->isKnownPositive(Delta);
989 case CmpInst::ICMP_SLT:
990 return SE->isKnownNegative(Delta);
991 default:
992 llvm_unreachable("unexpected predicate in isKnownPredicate");
993 }
994}
995
996
997// All subscripts are all the same type.
998// Loop bound may be smaller (e.g., a char).
999// Should zero extend loop bound, since it's always >= 0.
James Molloyc0661ae2015-05-15 12:17:22 +00001000// This routine collects upper bound and extends or truncates if needed.
1001// Truncating is safe when subscripts are known not to wrap. Cases without
1002// nowrap flags should have been rejected earlier.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001003// Return null if no bound available.
Chandler Carruth49c22192016-05-12 22:19:39 +00001004const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001005 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1006 const SCEV *UB = SE->getBackedgeTakenCount(L);
James Molloyc0661ae2015-05-15 12:17:22 +00001007 return SE->getTruncateOrZeroExtend(UB, T);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001008 }
Craig Topper9f008862014-04-15 04:59:12 +00001009 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001010}
1011
1012
1013// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1014// If the cast fails, returns NULL.
Chandler Carruth49c22192016-05-12 22:19:39 +00001015const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1016 Type *T) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001017 if (const SCEV *UB = collectUpperBound(L, T))
1018 return dyn_cast<SCEVConstant>(UB);
Craig Topper9f008862014-04-15 04:59:12 +00001019 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001020}
1021
1022
1023// testZIV -
1024// When we have a pair of subscripts of the form [c1] and [c2],
1025// where c1 and c2 are both loop invariant, we attack it using
1026// the ZIV test. Basically, we test by comparing the two values,
1027// but there are actually three possible results:
1028// 1) the values are equal, so there's a dependence
1029// 2) the values are different, so there's no dependence
1030// 3) the values might be equal, so we have to assume a dependence.
1031//
1032// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001033bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1034 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001035 DEBUG(dbgs() << " src = " << *Src << "\n");
1036 DEBUG(dbgs() << " dst = " << *Dst << "\n");
1037 ++ZIVapplications;
1038 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1039 DEBUG(dbgs() << " provably dependent\n");
1040 return false; // provably dependent
1041 }
1042 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1043 DEBUG(dbgs() << " provably independent\n");
1044 ++ZIVindependence;
1045 return true; // provably independent
1046 }
1047 DEBUG(dbgs() << " possibly dependent\n");
1048 Result.Consistent = false;
1049 return false; // possibly dependent
1050}
1051
1052
1053// strongSIVtest -
1054// From the paper, Practical Dependence Testing, Section 4.2.1
1055//
1056// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1057// where i is an induction variable, c1 and c2 are loop invariant,
1058// and a is a constant, we can solve it exactly using the Strong SIV test.
1059//
1060// Can prove independence. Failing that, can compute distance (and direction).
1061// In the presence of symbolic terms, we can sometimes make progress.
1062//
1063// If there's a dependence,
1064//
1065// c1 + a*i = c2 + a*i'
1066//
1067// The dependence distance is
1068//
1069// d = i' - i = (c1 - c2)/a
1070//
1071// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1072// loop's upper bound. If a dependence exists, the dependence direction is
1073// defined as
1074//
1075// { < if d > 0
1076// direction = { = if d = 0
1077// { > if d < 0
1078//
1079// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001080bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1081 const SCEV *DstConst, const Loop *CurLoop,
1082 unsigned Level, FullDependence &Result,
1083 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001084 DEBUG(dbgs() << "\tStrong SIV test\n");
1085 DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1086 DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1087 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1088 DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1089 DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1090 DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
1091 ++StrongSIVapplications;
1092 assert(0 < Level && Level <= CommonLevels && "level out of range");
1093 Level--;
1094
1095 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1096 DEBUG(dbgs() << "\t Delta = " << *Delta);
1097 DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
1098
1099 // check that |Delta| < iteration count
1100 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1101 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1102 DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1103 const SCEV *AbsDelta =
1104 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1105 const SCEV *AbsCoeff =
1106 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1107 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1108 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1109 // Distance greater than trip count - no dependence
1110 ++StrongSIVindependence;
1111 ++StrongSIVsuccesses;
1112 return true;
1113 }
1114 }
1115
1116 // Can we compute distance?
1117 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001118 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1119 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001120 APInt Distance = ConstDelta; // these need to be initialized
1121 APInt Remainder = ConstDelta;
1122 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1123 DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1124 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1125 // Make sure Coeff divides Delta exactly
1126 if (Remainder != 0) {
1127 // Coeff doesn't divide Distance, no dependence
1128 ++StrongSIVindependence;
1129 ++StrongSIVsuccesses;
1130 return true;
1131 }
1132 Result.DV[Level].Distance = SE->getConstant(Distance);
1133 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
1134 if (Distance.sgt(0))
1135 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1136 else if (Distance.slt(0))
1137 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1138 else
1139 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1140 ++StrongSIVsuccesses;
1141 }
1142 else if (Delta->isZero()) {
1143 // since 0/X == 0
1144 Result.DV[Level].Distance = Delta;
1145 NewConstraint.setDistance(Delta, CurLoop);
1146 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1147 ++StrongSIVsuccesses;
1148 }
1149 else {
1150 if (Coeff->isOne()) {
1151 DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1152 Result.DV[Level].Distance = Delta; // since X/1 == X
1153 NewConstraint.setDistance(Delta, CurLoop);
1154 }
1155 else {
1156 Result.Consistent = false;
1157 NewConstraint.setLine(Coeff,
1158 SE->getNegativeSCEV(Coeff),
1159 SE->getNegativeSCEV(Delta), CurLoop);
1160 }
1161
1162 // maybe we can get a useful direction
1163 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1164 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1165 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1166 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1167 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1168 // The double negatives above are confusing.
1169 // It helps to read !SE->isKnownNonZero(Delta)
1170 // as "Delta might be Zero"
1171 unsigned NewDirection = Dependence::DVEntry::NONE;
1172 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1173 (DeltaMaybeNegative && CoeffMaybeNegative))
1174 NewDirection = Dependence::DVEntry::LT;
1175 if (DeltaMaybeZero)
1176 NewDirection |= Dependence::DVEntry::EQ;
1177 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1178 (DeltaMaybePositive && CoeffMaybeNegative))
1179 NewDirection |= Dependence::DVEntry::GT;
1180 if (NewDirection < Result.DV[Level].Direction)
1181 ++StrongSIVsuccesses;
1182 Result.DV[Level].Direction &= NewDirection;
1183 }
1184 return false;
1185}
1186
1187
1188// weakCrossingSIVtest -
1189// From the paper, Practical Dependence Testing, Section 4.2.2
1190//
1191// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1192// where i is an induction variable, c1 and c2 are loop invariant,
1193// and a is a constant, we can solve it exactly using the
1194// Weak-Crossing SIV test.
1195//
1196// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1197// the two lines, where i = i', yielding
1198//
1199// c1 + a*i = c2 - a*i
1200// 2a*i = c2 - c1
1201// i = (c2 - c1)/2a
1202//
1203// If i < 0, there is no dependence.
1204// If i > upperbound, there is no dependence.
1205// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1206// If i = upperbound, there's a dependence with distance = 0.
1207// If i is integral, there's a dependence (all directions).
1208// If the non-integer part = 1/2, there's a dependence (<> directions).
1209// Otherwise, there's no dependence.
1210//
1211// Can prove independence. Failing that,
1212// can sometimes refine the directions.
1213// Can determine iteration for splitting.
1214//
1215// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001216bool DependenceInfo::weakCrossingSIVtest(
1217 const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
1218 const Loop *CurLoop, unsigned Level, FullDependence &Result,
1219 Constraint &NewConstraint, const SCEV *&SplitIter) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001220 DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1221 DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1222 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1223 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1224 ++WeakCrossingSIVapplications;
1225 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1226 Level--;
1227 Result.Consistent = false;
1228 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1229 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1230 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1231 if (Delta->isZero()) {
Sebastian Pope96232612012-10-12 02:04:32 +00001232 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1233 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001234 ++WeakCrossingSIVsuccesses;
1235 if (!Result.DV[Level].Direction) {
1236 ++WeakCrossingSIVindependence;
1237 return true;
1238 }
1239 Result.DV[Level].Distance = Delta; // = 0
1240 return false;
1241 }
1242 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1243 if (!ConstCoeff)
1244 return false;
1245
1246 Result.DV[Level].Splitable = true;
1247 if (SE->isKnownNegative(ConstCoeff)) {
1248 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1249 assert(ConstCoeff &&
1250 "dynamic cast of negative of ConstCoeff should yield constant");
1251 Delta = SE->getNegativeSCEV(Delta);
1252 }
1253 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1254
Chandler Carruth49c22192016-05-12 22:19:39 +00001255 // compute SplitIter for use by DependenceInfo::getSplitIteration()
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001256 SplitIter = SE->getUDivExpr(
1257 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1258 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
Sebastian Pop59b61b92012-10-11 07:32:34 +00001259 DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
1260
1261 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1262 if (!ConstDelta)
1263 return false;
1264
1265 // We're certain that ConstCoeff > 0; therefore,
1266 // if Delta < 0, then no dependence.
1267 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1268 DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1269 if (SE->isKnownNegative(Delta)) {
1270 // No dependence, Delta < 0
1271 ++WeakCrossingSIVindependence;
1272 ++WeakCrossingSIVsuccesses;
1273 return true;
1274 }
1275
1276 // We're certain that Delta > 0 and ConstCoeff > 0.
1277 // Check Delta/(2*ConstCoeff) against upper loop bound
1278 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1279 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1280 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1281 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
1282 ConstantTwo);
1283 DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1284 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1285 // Delta too big, no dependence
1286 ++WeakCrossingSIVindependence;
1287 ++WeakCrossingSIVsuccesses;
1288 return true;
1289 }
1290 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1291 // i = i' = UB
Sebastian Pope96232612012-10-12 02:04:32 +00001292 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1293 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001294 ++WeakCrossingSIVsuccesses;
1295 if (!Result.DV[Level].Direction) {
1296 ++WeakCrossingSIVindependence;
1297 return true;
1298 }
1299 Result.DV[Level].Splitable = false;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001300 Result.DV[Level].Distance = SE->getZero(Delta->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00001301 return false;
1302 }
1303 }
1304
1305 // check that Coeff divides Delta
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001306 APInt APDelta = ConstDelta->getAPInt();
1307 APInt APCoeff = ConstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001308 APInt Distance = APDelta; // these need to be initialzed
1309 APInt Remainder = APDelta;
1310 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1311 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1312 if (Remainder != 0) {
1313 // Coeff doesn't divide Delta, no dependence
1314 ++WeakCrossingSIVindependence;
1315 ++WeakCrossingSIVsuccesses;
1316 return true;
1317 }
1318 DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1319
1320 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1321 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1322 Remainder = Distance.srem(Two);
1323 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1324 if (Remainder != 0) {
1325 // Equal direction isn't possible
Sebastian Pope96232612012-10-12 02:04:32 +00001326 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001327 ++WeakCrossingSIVsuccesses;
1328 }
1329 return false;
1330}
1331
1332
1333// Kirch's algorithm, from
1334//
1335// Optimizing Supercompilers for Supercomputers
1336// Michael Wolfe
1337// MIT Press, 1989
1338//
1339// Program 2.1, page 29.
1340// Computes the GCD of AM and BM.
Mingjie Xing9deac1b2014-01-07 01:54:16 +00001341// Also finds a solution to the equation ax - by = gcd(a, b).
1342// Returns true if dependence disproved; i.e., gcd does not divide Delta.
Benjamin Kramerc321e532016-06-08 19:09:22 +00001343static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1344 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001345 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1346 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1347 APInt G0 = AM.abs();
1348 APInt G1 = BM.abs();
1349 APInt Q = G0; // these need to be initialized
1350 APInt R = G0;
1351 APInt::sdivrem(G0, G1, Q, R);
1352 while (R != 0) {
1353 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1354 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1355 G0 = G1; G1 = R;
1356 APInt::sdivrem(G0, G1, Q, R);
1357 }
1358 G = G1;
1359 DEBUG(dbgs() << "\t GCD = " << G << "\n");
1360 X = AM.slt(0) ? -A1 : A1;
1361 Y = BM.slt(0) ? B1 : -B1;
1362
1363 // make sure gcd divides Delta
1364 R = Delta.srem(G);
1365 if (R != 0)
1366 return true; // gcd doesn't divide Delta, no dependence
1367 Q = Delta.sdiv(G);
1368 X *= Q;
1369 Y *= Q;
1370 return false;
1371}
1372
Benjamin Kramerc321e532016-06-08 19:09:22 +00001373static APInt floorOfQuotient(const APInt &A, const APInt &B) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001374 APInt Q = A; // these need to be initialized
1375 APInt R = A;
1376 APInt::sdivrem(A, B, Q, R);
1377 if (R == 0)
1378 return Q;
1379 if ((A.sgt(0) && B.sgt(0)) ||
1380 (A.slt(0) && B.slt(0)))
1381 return Q;
1382 else
1383 return Q - 1;
1384}
1385
Benjamin Kramerc321e532016-06-08 19:09:22 +00001386static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001387 APInt Q = A; // these need to be initialized
1388 APInt R = A;
1389 APInt::sdivrem(A, B, Q, R);
1390 if (R == 0)
1391 return Q;
1392 if ((A.sgt(0) && B.sgt(0)) ||
1393 (A.slt(0) && B.slt(0)))
1394 return Q + 1;
1395 else
1396 return Q;
1397}
1398
1399
1400static
1401APInt maxAPInt(APInt A, APInt B) {
1402 return A.sgt(B) ? A : B;
1403}
1404
1405
1406static
1407APInt minAPInt(APInt A, APInt B) {
1408 return A.slt(B) ? A : B;
1409}
1410
1411
1412// exactSIVtest -
1413// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1414// where i is an induction variable, c1 and c2 are loop invariant, and a1
1415// and a2 are constant, we can solve it exactly using an algorithm developed
1416// by Banerjee and Wolfe. See Section 2.5.3 in
1417//
1418// Optimizing Supercompilers for Supercomputers
1419// Michael Wolfe
1420// MIT Press, 1989
1421//
1422// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1423// so use them if possible. They're also a bit better with symbolics and,
1424// in the case of the strong SIV test, can compute Distances.
1425//
1426// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001427bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1428 const SCEV *SrcConst, const SCEV *DstConst,
1429 const Loop *CurLoop, unsigned Level,
1430 FullDependence &Result,
1431 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001432 DEBUG(dbgs() << "\tExact SIV test\n");
1433 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1434 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1435 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1436 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1437 ++ExactSIVapplications;
1438 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1439 Level--;
1440 Result.Consistent = false;
1441 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1442 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1443 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff),
1444 Delta, CurLoop);
1445 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1446 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1447 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1448 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1449 return false;
1450
1451 // find gcd
1452 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001453 APInt AM = ConstSrcCoeff->getAPInt();
1454 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001455 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001456 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001457 // gcd doesn't divide Delta, no dependence
1458 ++ExactSIVindependence;
1459 ++ExactSIVsuccesses;
1460 return true;
1461 }
1462
1463 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1464
1465 // since SCEV construction normalizes, LM = 0
1466 APInt UM(Bits, 1, true);
1467 bool UMvalid = false;
1468 // UM is perhaps unavailable, let's check
1469 if (const SCEVConstant *CUB =
1470 collectConstantUpperBound(CurLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001471 UM = CUB->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001472 DEBUG(dbgs() << "\t UM = " << UM << "\n");
1473 UMvalid = true;
1474 }
1475
1476 APInt TU(APInt::getSignedMaxValue(Bits));
1477 APInt TL(APInt::getSignedMinValue(Bits));
1478
1479 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1480 APInt TMUL = BM.sdiv(G);
1481 if (TMUL.sgt(0)) {
1482 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1483 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1484 if (UMvalid) {
1485 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL));
1486 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1487 }
1488 }
1489 else {
1490 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1491 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1492 if (UMvalid) {
1493 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL));
1494 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1495 }
1496 }
1497
1498 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1499 TMUL = AM.sdiv(G);
1500 if (TMUL.sgt(0)) {
1501 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1502 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1503 if (UMvalid) {
1504 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL));
1505 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1506 }
1507 }
1508 else {
1509 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1510 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1511 if (UMvalid) {
1512 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL));
1513 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1514 }
1515 }
1516 if (TL.sgt(TU)) {
1517 ++ExactSIVindependence;
1518 ++ExactSIVsuccesses;
1519 return true;
1520 }
1521
1522 // explore directions
1523 unsigned NewDirection = Dependence::DVEntry::NONE;
1524
1525 // less than
1526 APInt SaveTU(TU); // save these
1527 APInt SaveTL(TL);
1528 DEBUG(dbgs() << "\t exploring LT direction\n");
1529 TMUL = AM - BM;
1530 if (TMUL.sgt(0)) {
1531 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL));
1532 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1533 }
1534 else {
1535 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL));
1536 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1537 }
1538 if (TL.sle(TU)) {
1539 NewDirection |= Dependence::DVEntry::LT;
1540 ++ExactSIVsuccesses;
1541 }
1542
1543 // equal
1544 TU = SaveTU; // restore
1545 TL = SaveTL;
1546 DEBUG(dbgs() << "\t exploring EQ direction\n");
1547 if (TMUL.sgt(0)) {
1548 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL));
1549 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1550 }
1551 else {
1552 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL));
1553 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1554 }
1555 TMUL = BM - AM;
1556 if (TMUL.sgt(0)) {
1557 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL));
1558 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1559 }
1560 else {
1561 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL));
1562 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1563 }
1564 if (TL.sle(TU)) {
1565 NewDirection |= Dependence::DVEntry::EQ;
1566 ++ExactSIVsuccesses;
1567 }
1568
1569 // greater than
1570 TU = SaveTU; // restore
1571 TL = SaveTL;
1572 DEBUG(dbgs() << "\t exploring GT direction\n");
1573 if (TMUL.sgt(0)) {
1574 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL));
1575 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1576 }
1577 else {
1578 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL));
1579 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1580 }
1581 if (TL.sle(TU)) {
1582 NewDirection |= Dependence::DVEntry::GT;
1583 ++ExactSIVsuccesses;
1584 }
1585
1586 // finished
1587 Result.DV[Level].Direction &= NewDirection;
1588 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1589 ++ExactSIVindependence;
1590 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1591}
1592
1593
1594
1595// Return true if the divisor evenly divides the dividend.
1596static
1597bool isRemainderZero(const SCEVConstant *Dividend,
1598 const SCEVConstant *Divisor) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +00001599 const APInt &ConstDividend = Dividend->getAPInt();
1600 const APInt &ConstDivisor = Divisor->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001601 return ConstDividend.srem(ConstDivisor) == 0;
1602}
1603
1604
1605// weakZeroSrcSIVtest -
1606// From the paper, Practical Dependence Testing, Section 4.2.2
1607//
1608// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1609// where i is an induction variable, c1 and c2 are loop invariant,
1610// and a is a constant, we can solve it exactly using the
1611// Weak-Zero SIV test.
1612//
1613// Given
1614//
1615// c1 = c2 + a*i
1616//
1617// we get
1618//
1619// (c1 - c2)/a = i
1620//
1621// If i is not an integer, there's no dependence.
1622// If i < 0 or > UB, there's no dependence.
1623// If i = 0, the direction is <= and peeling the
1624// 1st iteration will break the dependence.
1625// If i = UB, the direction is >= and peeling the
1626// last iteration will break the dependence.
1627// Otherwise, the direction is *.
1628//
1629// Can prove independence. Failing that, we can sometimes refine
1630// the directions. Can sometimes show that first or last
1631// iteration carries all the dependences (so worth peeling).
1632//
1633// (see also weakZeroDstSIVtest)
1634//
1635// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001636bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1637 const SCEV *SrcConst,
1638 const SCEV *DstConst,
1639 const Loop *CurLoop, unsigned Level,
1640 FullDependence &Result,
1641 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001642 // For the WeakSIV test, it's possible the loop isn't common to
1643 // the Src and Dst loops. If it isn't, then there's no need to
1644 // record a direction.
1645 DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1646 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1647 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1648 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1649 ++WeakZeroSIVapplications;
1650 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1651 Level--;
1652 Result.Consistent = false;
1653 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001654 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1655 CurLoop);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001656 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1657 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1658 if (Level < CommonLevels) {
1659 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1660 Result.DV[Level].PeelFirst = true;
1661 ++WeakZeroSIVsuccesses;
1662 }
1663 return false; // dependences caused by first iteration
1664 }
1665 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1666 if (!ConstCoeff)
1667 return false;
1668 const SCEV *AbsCoeff =
1669 SE->isKnownNegative(ConstCoeff) ?
1670 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1671 const SCEV *NewDelta =
1672 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1673
1674 // check that Delta/SrcCoeff < iteration count
1675 // really check NewDelta < count*AbsCoeff
1676 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1677 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1678 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1679 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1680 ++WeakZeroSIVindependence;
1681 ++WeakZeroSIVsuccesses;
1682 return true;
1683 }
1684 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1685 // dependences caused by last iteration
1686 if (Level < CommonLevels) {
1687 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1688 Result.DV[Level].PeelLast = true;
1689 ++WeakZeroSIVsuccesses;
1690 }
1691 return false;
1692 }
1693 }
1694
1695 // check that Delta/SrcCoeff >= 0
1696 // really check that NewDelta >= 0
1697 if (SE->isKnownNegative(NewDelta)) {
1698 // No dependence, newDelta < 0
1699 ++WeakZeroSIVindependence;
1700 ++WeakZeroSIVsuccesses;
1701 return true;
1702 }
1703
1704 // if SrcCoeff doesn't divide Delta, then no dependence
1705 if (isa<SCEVConstant>(Delta) &&
1706 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1707 ++WeakZeroSIVindependence;
1708 ++WeakZeroSIVsuccesses;
1709 return true;
1710 }
1711 return false;
1712}
1713
1714
1715// weakZeroDstSIVtest -
1716// From the paper, Practical Dependence Testing, Section 4.2.2
1717//
1718// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1719// where i is an induction variable, c1 and c2 are loop invariant,
1720// and a is a constant, we can solve it exactly using the
1721// Weak-Zero SIV test.
1722//
1723// Given
1724//
1725// c1 + a*i = c2
1726//
1727// we get
1728//
1729// i = (c2 - c1)/a
1730//
1731// If i is not an integer, there's no dependence.
1732// If i < 0 or > UB, there's no dependence.
1733// If i = 0, the direction is <= and peeling the
1734// 1st iteration will break the dependence.
1735// If i = UB, the direction is >= and peeling the
1736// last iteration will break the dependence.
1737// Otherwise, the direction is *.
1738//
1739// Can prove independence. Failing that, we can sometimes refine
1740// the directions. Can sometimes show that first or last
1741// iteration carries all the dependences (so worth peeling).
1742//
1743// (see also weakZeroSrcSIVtest)
1744//
1745// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001746bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1747 const SCEV *SrcConst,
1748 const SCEV *DstConst,
1749 const Loop *CurLoop, unsigned Level,
1750 FullDependence &Result,
1751 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001752 // For the WeakSIV test, it's possible the loop isn't common to the
1753 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1754 DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1755 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1756 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1757 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1758 ++WeakZeroSIVapplications;
1759 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1760 Level--;
1761 Result.Consistent = false;
1762 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001763 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
1764 CurLoop);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001765 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1766 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1767 if (Level < CommonLevels) {
1768 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1769 Result.DV[Level].PeelFirst = true;
1770 ++WeakZeroSIVsuccesses;
1771 }
1772 return false; // dependences caused by first iteration
1773 }
1774 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1775 if (!ConstCoeff)
1776 return false;
1777 const SCEV *AbsCoeff =
1778 SE->isKnownNegative(ConstCoeff) ?
1779 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1780 const SCEV *NewDelta =
1781 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1782
1783 // check that Delta/SrcCoeff < iteration count
1784 // really check NewDelta < count*AbsCoeff
1785 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1786 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1787 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1788 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1789 ++WeakZeroSIVindependence;
1790 ++WeakZeroSIVsuccesses;
1791 return true;
1792 }
1793 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1794 // dependences caused by last iteration
1795 if (Level < CommonLevels) {
1796 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1797 Result.DV[Level].PeelLast = true;
1798 ++WeakZeroSIVsuccesses;
1799 }
1800 return false;
1801 }
1802 }
1803
1804 // check that Delta/SrcCoeff >= 0
1805 // really check that NewDelta >= 0
1806 if (SE->isKnownNegative(NewDelta)) {
1807 // No dependence, newDelta < 0
1808 ++WeakZeroSIVindependence;
1809 ++WeakZeroSIVsuccesses;
1810 return true;
1811 }
1812
1813 // if SrcCoeff doesn't divide Delta, then no dependence
1814 if (isa<SCEVConstant>(Delta) &&
1815 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1816 ++WeakZeroSIVindependence;
1817 ++WeakZeroSIVsuccesses;
1818 return true;
1819 }
1820 return false;
1821}
1822
1823
1824// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1825// Things of the form [c1 + a*i] and [c2 + b*j],
1826// where i and j are induction variable, c1 and c2 are loop invariant,
1827// and a and b are constants.
1828// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00001829// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001830// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
Chandler Carruth49c22192016-05-12 22:19:39 +00001831bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1832 const SCEV *SrcConst, const SCEV *DstConst,
1833 const Loop *SrcLoop, const Loop *DstLoop,
1834 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001835 DEBUG(dbgs() << "\tExact RDIV test\n");
1836 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1837 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1838 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1839 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1840 ++ExactRDIVapplications;
1841 Result.Consistent = false;
1842 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1843 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1844 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1845 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1846 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1847 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1848 return false;
1849
1850 // find gcd
1851 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001852 APInt AM = ConstSrcCoeff->getAPInt();
1853 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001854 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001855 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001856 // gcd doesn't divide Delta, no dependence
1857 ++ExactRDIVindependence;
1858 return true;
1859 }
1860
1861 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1862
1863 // since SCEV construction seems to normalize, LM = 0
1864 APInt SrcUM(Bits, 1, true);
1865 bool SrcUMvalid = false;
1866 // SrcUM is perhaps unavailable, let's check
1867 if (const SCEVConstant *UpperBound =
1868 collectConstantUpperBound(SrcLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001869 SrcUM = UpperBound->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001870 DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n");
1871 SrcUMvalid = true;
1872 }
1873
1874 APInt DstUM(Bits, 1, true);
1875 bool DstUMvalid = false;
1876 // UM is perhaps unavailable, let's check
1877 if (const SCEVConstant *UpperBound =
1878 collectConstantUpperBound(DstLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001879 DstUM = UpperBound->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001880 DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n");
1881 DstUMvalid = true;
1882 }
1883
1884 APInt TU(APInt::getSignedMaxValue(Bits));
1885 APInt TL(APInt::getSignedMinValue(Bits));
1886
1887 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1888 APInt TMUL = BM.sdiv(G);
1889 if (TMUL.sgt(0)) {
1890 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1891 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1892 if (SrcUMvalid) {
1893 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL));
1894 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1895 }
1896 }
1897 else {
1898 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1899 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1900 if (SrcUMvalid) {
1901 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL));
1902 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1903 }
1904 }
1905
1906 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1907 TMUL = AM.sdiv(G);
1908 if (TMUL.sgt(0)) {
1909 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1910 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1911 if (DstUMvalid) {
1912 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL));
1913 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1914 }
1915 }
1916 else {
1917 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1918 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1919 if (DstUMvalid) {
1920 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL));
1921 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1922 }
1923 }
1924 if (TL.sgt(TU))
1925 ++ExactRDIVindependence;
1926 return TL.sgt(TU);
1927}
1928
1929
1930// symbolicRDIVtest -
1931// In Section 4.5 of the Practical Dependence Testing paper,the authors
1932// introduce a special case of Banerjee's Inequalities (also called the
1933// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
1934// particularly cases with symbolics. Since it's only able to disprove
1935// dependence (not compute distances or directions), we'll use it as a
1936// fall back for the other tests.
1937//
1938// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1939// where i and j are induction variables and c1 and c2 are loop invariants,
1940// we can use the symbolic tests to disprove some dependences, serving as a
1941// backup for the RDIV test. Note that i and j can be the same variable,
1942// letting this test serve as a backup for the various SIV tests.
1943//
1944// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
1945// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
1946// loop bounds for the i and j loops, respectively. So, ...
1947//
1948// c1 + a1*i = c2 + a2*j
1949// a1*i - a2*j = c2 - c1
1950//
1951// To test for a dependence, we compute c2 - c1 and make sure it's in the
1952// range of the maximum and minimum possible values of a1*i - a2*j.
1953// Considering the signs of a1 and a2, we have 4 possible cases:
1954//
1955// 1) If a1 >= 0 and a2 >= 0, then
1956// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
1957// -a2*N2 <= c2 - c1 <= a1*N1
1958//
1959// 2) If a1 >= 0 and a2 <= 0, then
1960// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
1961// 0 <= c2 - c1 <= a1*N1 - a2*N2
1962//
1963// 3) If a1 <= 0 and a2 >= 0, then
1964// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
1965// a1*N1 - a2*N2 <= c2 - c1 <= 0
1966//
1967// 4) If a1 <= 0 and a2 <= 0, then
1968// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
1969// a1*N1 <= c2 - c1 <= -a2*N2
1970//
1971// return true if dependence disproved
Chandler Carruth49c22192016-05-12 22:19:39 +00001972bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
1973 const SCEV *C1, const SCEV *C2,
1974 const Loop *Loop1,
1975 const Loop *Loop2) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001976 ++SymbolicRDIVapplications;
1977 DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
1978 DEBUG(dbgs() << "\t A1 = " << *A1);
1979 DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
1980 DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
1981 DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
1982 DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
1983 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
1984 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
1985 DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
1986 DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
1987 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
1988 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
1989 DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
1990 DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
1991 if (SE->isKnownNonNegative(A1)) {
1992 if (SE->isKnownNonNegative(A2)) {
1993 // A1 >= 0 && A2 >= 0
1994 if (N1) {
1995 // make sure that c2 - c1 <= a1*N1
1996 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
1997 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
1998 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
1999 ++SymbolicRDIVindependence;
2000 return true;
2001 }
2002 }
2003 if (N2) {
2004 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2005 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2006 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2007 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2008 ++SymbolicRDIVindependence;
2009 return true;
2010 }
2011 }
2012 }
2013 else if (SE->isKnownNonPositive(A2)) {
2014 // a1 >= 0 && a2 <= 0
2015 if (N1 && N2) {
2016 // make sure that c2 - c1 <= a1*N1 - a2*N2
2017 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2018 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2019 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2020 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2021 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2022 ++SymbolicRDIVindependence;
2023 return true;
2024 }
2025 }
2026 // make sure that 0 <= c2 - c1
2027 if (SE->isKnownNegative(C2_C1)) {
2028 ++SymbolicRDIVindependence;
2029 return true;
2030 }
2031 }
2032 }
2033 else if (SE->isKnownNonPositive(A1)) {
2034 if (SE->isKnownNonNegative(A2)) {
2035 // a1 <= 0 && a2 >= 0
2036 if (N1 && N2) {
2037 // make sure that a1*N1 - a2*N2 <= c2 - c1
2038 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2039 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2040 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2041 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2042 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2043 ++SymbolicRDIVindependence;
2044 return true;
2045 }
2046 }
2047 // make sure that c2 - c1 <= 0
2048 if (SE->isKnownPositive(C2_C1)) {
2049 ++SymbolicRDIVindependence;
2050 return true;
2051 }
2052 }
2053 else if (SE->isKnownNonPositive(A2)) {
2054 // a1 <= 0 && a2 <= 0
2055 if (N1) {
2056 // make sure that a1*N1 <= c2 - c1
2057 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2058 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2059 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2060 ++SymbolicRDIVindependence;
2061 return true;
2062 }
2063 }
2064 if (N2) {
2065 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2066 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2067 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2068 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2069 ++SymbolicRDIVindependence;
2070 return true;
2071 }
2072 }
2073 }
2074 }
2075 return false;
2076}
2077
2078
2079// testSIV -
2080// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2081// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2082// a2 are constant, we attack it with an SIV test. While they can all be
2083// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2084// they apply; they're cheaper and sometimes more precise.
2085//
2086// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002087bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2088 FullDependence &Result, Constraint &NewConstraint,
2089 const SCEV *&SplitIter) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002090 DEBUG(dbgs() << " src = " << *Src << "\n");
2091 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2092 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2093 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2094 if (SrcAddRec && DstAddRec) {
2095 const SCEV *SrcConst = SrcAddRec->getStart();
2096 const SCEV *DstConst = DstAddRec->getStart();
2097 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2098 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2099 const Loop *CurLoop = SrcAddRec->getLoop();
2100 assert(CurLoop == DstAddRec->getLoop() &&
2101 "both loops in SIV should be same");
2102 Level = mapSrcLoop(CurLoop);
2103 bool disproven;
2104 if (SrcCoeff == DstCoeff)
2105 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2106 Level, Result, NewConstraint);
2107 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2108 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2109 Level, Result, NewConstraint, SplitIter);
2110 else
2111 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2112 Level, Result, NewConstraint);
2113 return disproven ||
2114 gcdMIVtest(Src, Dst, Result) ||
2115 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
2116 }
2117 if (SrcAddRec) {
2118 const SCEV *SrcConst = SrcAddRec->getStart();
2119 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2120 const SCEV *DstConst = Dst;
2121 const Loop *CurLoop = SrcAddRec->getLoop();
2122 Level = mapSrcLoop(CurLoop);
2123 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2124 Level, Result, NewConstraint) ||
2125 gcdMIVtest(Src, Dst, Result);
2126 }
2127 if (DstAddRec) {
2128 const SCEV *DstConst = DstAddRec->getStart();
2129 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2130 const SCEV *SrcConst = Src;
2131 const Loop *CurLoop = DstAddRec->getLoop();
2132 Level = mapDstLoop(CurLoop);
2133 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
2134 CurLoop, Level, Result, NewConstraint) ||
2135 gcdMIVtest(Src, Dst, Result);
2136 }
2137 llvm_unreachable("SIV test expected at least one AddRec");
2138 return false;
2139}
2140
2141
2142// testRDIV -
2143// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2144// where i and j are induction variables, c1 and c2 are loop invariant,
2145// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2146// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2147// It doesn't make sense to talk about distance or direction in this case,
2148// so there's no point in making special versions of the Strong SIV test or
2149// the Weak-crossing SIV test.
2150//
2151// With minor algebra, this test can also be used for things like
2152// [c1 + a1*i + a2*j][c2].
2153//
2154// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002155bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2156 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002157 // we have 3 possible situations here:
2158 // 1) [a*i + b] and [c*j + d]
2159 // 2) [a*i + c*j + b] and [d]
2160 // 3) [b] and [a*i + c*j + d]
2161 // We need to find what we've got and get organized
2162
2163 const SCEV *SrcConst, *DstConst;
2164 const SCEV *SrcCoeff, *DstCoeff;
2165 const Loop *SrcLoop, *DstLoop;
2166
2167 DEBUG(dbgs() << " src = " << *Src << "\n");
2168 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2169 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2170 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2171 if (SrcAddRec && DstAddRec) {
2172 SrcConst = SrcAddRec->getStart();
2173 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2174 SrcLoop = SrcAddRec->getLoop();
2175 DstConst = DstAddRec->getStart();
2176 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2177 DstLoop = DstAddRec->getLoop();
2178 }
2179 else if (SrcAddRec) {
2180 if (const SCEVAddRecExpr *tmpAddRec =
2181 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2182 SrcConst = tmpAddRec->getStart();
2183 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2184 SrcLoop = tmpAddRec->getLoop();
2185 DstConst = Dst;
2186 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2187 DstLoop = SrcAddRec->getLoop();
2188 }
2189 else
2190 llvm_unreachable("RDIV reached by surprising SCEVs");
2191 }
2192 else if (DstAddRec) {
2193 if (const SCEVAddRecExpr *tmpAddRec =
2194 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2195 DstConst = tmpAddRec->getStart();
2196 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2197 DstLoop = tmpAddRec->getLoop();
2198 SrcConst = Src;
2199 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2200 SrcLoop = DstAddRec->getLoop();
2201 }
2202 else
2203 llvm_unreachable("RDIV reached by surprising SCEVs");
2204 }
2205 else
2206 llvm_unreachable("RDIV expected at least one AddRec");
2207 return exactRDIVtest(SrcCoeff, DstCoeff,
2208 SrcConst, DstConst,
2209 SrcLoop, DstLoop,
2210 Result) ||
2211 gcdMIVtest(Src, Dst, Result) ||
2212 symbolicRDIVtest(SrcCoeff, DstCoeff,
2213 SrcConst, DstConst,
2214 SrcLoop, DstLoop);
2215}
2216
2217
2218// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2219// Return true if dependence disproved.
2220// Can sometimes refine direction vectors.
Chandler Carruth49c22192016-05-12 22:19:39 +00002221bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2222 const SmallBitVector &Loops,
2223 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002224 DEBUG(dbgs() << " src = " << *Src << "\n");
2225 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2226 Result.Consistent = false;
2227 return gcdMIVtest(Src, Dst, Result) ||
2228 banerjeeMIVtest(Src, Dst, Loops, Result);
2229}
2230
2231
2232// Given a product, e.g., 10*X*Y, returns the first constant operand,
2233// in this case 10. If there is no constant part, returns NULL.
2234static
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002235const SCEVConstant *getConstantPart(const SCEV *Expr) {
2236 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2237 return Constant;
2238 else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2239 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
Sebastian Pop59b61b92012-10-11 07:32:34 +00002240 return Constant;
Craig Topper9f008862014-04-15 04:59:12 +00002241 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002242}
2243
2244
2245//===----------------------------------------------------------------------===//
2246// gcdMIVtest -
2247// Tests an MIV subscript pair for dependence.
2248// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00002249// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002250// Can sometimes disprove the equal direction for 1 or more loops,
2251// as discussed in Michael Wolfe's book,
2252// High Performance Compilers for Parallel Computing, page 235.
2253//
2254// We spend some effort (code!) to handle cases like
2255// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2256// but M and N are just loop-invariant variables.
2257// This should help us handle linearized subscripts;
2258// also makes this test a useful backup to the various SIV tests.
2259//
2260// It occurs to me that the presence of loop-invariant variables
2261// changes the nature of the test from "greatest common divisor"
Preston Briggs4eb7ee52012-11-29 04:30:52 +00002262// to "a common divisor".
Chandler Carruth49c22192016-05-12 22:19:39 +00002263bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2264 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002265 DEBUG(dbgs() << "starting gcd\n");
2266 ++GCDapplications;
Preston Briggs3ad39492012-11-21 23:50:04 +00002267 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002268 APInt RunningGCD = APInt::getNullValue(BitWidth);
2269
2270 // Examine Src coefficients.
2271 // Compute running GCD and record source constant.
2272 // Because we're looking for the constant at the end of the chain,
2273 // we can't quit the loop just because the GCD == 1.
2274 const SCEV *Coefficients = Src;
2275 while (const SCEVAddRecExpr *AddRec =
2276 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2277 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002278 // If the coefficient is the product of a constant and other stuff,
2279 // we can use the constant in the GCD computation.
2280 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002281 if (!Constant)
2282 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002283 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002284 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2285 Coefficients = AddRec->getStart();
2286 }
2287 const SCEV *SrcConst = Coefficients;
2288
2289 // Examine Dst coefficients.
2290 // Compute running GCD and record destination constant.
2291 // Because we're looking for the constant at the end of the chain,
2292 // we can't quit the loop just because the GCD == 1.
2293 Coefficients = Dst;
2294 while (const SCEVAddRecExpr *AddRec =
2295 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2296 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002297 // If the coefficient is the product of a constant and other stuff,
2298 // we can use the constant in the GCD computation.
2299 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002300 if (!Constant)
2301 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002302 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002303 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2304 Coefficients = AddRec->getStart();
2305 }
2306 const SCEV *DstConst = Coefficients;
2307
2308 APInt ExtraGCD = APInt::getNullValue(BitWidth);
2309 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2310 DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2311 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2312 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2313 // If Delta is a sum of products, we may be able to make further progress.
2314 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
2315 const SCEV *Operand = Sum->getOperand(Op);
2316 if (isa<SCEVConstant>(Operand)) {
2317 assert(!Constant && "Surprised to find multiple constants");
2318 Constant = cast<SCEVConstant>(Operand);
2319 }
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002320 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002321 // Search for constant operand to participate in GCD;
2322 // If none found; return false.
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002323 const SCEVConstant *ConstOp = getConstantPart(Product);
2324 if (!ConstOp)
2325 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002326 APInt ConstOpValue = ConstOp->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002327 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2328 ConstOpValue.abs());
2329 }
2330 else
2331 return false;
2332 }
2333 }
2334 if (!Constant)
2335 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002336 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002337 DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2338 if (ConstDelta == 0)
2339 return false;
2340 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2341 DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2342 APInt Remainder = ConstDelta.srem(RunningGCD);
2343 if (Remainder != 0) {
2344 ++GCDindependence;
2345 return true;
2346 }
2347
2348 // Try to disprove equal directions.
2349 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2350 // the code above can't disprove the dependence because the GCD = 1.
2351 // So we consider what happen if i = i' and what happens if j = j'.
2352 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2353 // which is infeasible, so we can disallow the = direction for the i level.
2354 // Setting j = j' doesn't help matters, so we end up with a direction vector
2355 // of [<>, *]
2356 //
2357 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2358 // we need to remember that the constant part is 5 and the RunningGCD should
2359 // be initialized to ExtraGCD = 30.
2360 DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2361
2362 bool Improved = false;
2363 Coefficients = Src;
2364 while (const SCEVAddRecExpr *AddRec =
2365 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2366 Coefficients = AddRec->getStart();
2367 const Loop *CurLoop = AddRec->getLoop();
2368 RunningGCD = ExtraGCD;
2369 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2370 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2371 const SCEV *Inner = Src;
2372 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2373 AddRec = cast<SCEVAddRecExpr>(Inner);
2374 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2375 if (CurLoop == AddRec->getLoop())
2376 ; // SrcCoeff == Coeff
2377 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002378 // If the coefficient is the product of a constant and other stuff,
2379 // we can use the constant in the GCD computation.
2380 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002381 if (!Constant)
2382 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002383 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002384 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2385 }
2386 Inner = AddRec->getStart();
2387 }
2388 Inner = Dst;
2389 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2390 AddRec = cast<SCEVAddRecExpr>(Inner);
2391 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2392 if (CurLoop == AddRec->getLoop())
2393 DstCoeff = Coeff;
2394 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002395 // If the coefficient is the product of a constant and other stuff,
2396 // we can use the constant in the GCD computation.
2397 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002398 if (!Constant)
2399 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002400 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002401 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2402 }
2403 Inner = AddRec->getStart();
2404 }
2405 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002406 // If the coefficient is the product of a constant and other stuff,
2407 // we can use the constant in the GCD computation.
2408 Constant = getConstantPart(Delta);
2409 if (!Constant)
Sebastian Pop59b61b92012-10-11 07:32:34 +00002410 // The difference of the two coefficients might not be a product
2411 // or constant, in which case we give up on this direction.
2412 continue;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002413 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002414 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2415 DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2416 if (RunningGCD != 0) {
2417 Remainder = ConstDelta.srem(RunningGCD);
2418 DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2419 if (Remainder != 0) {
2420 unsigned Level = mapSrcLoop(CurLoop);
Sebastian Pope96232612012-10-12 02:04:32 +00002421 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002422 Improved = true;
2423 }
2424 }
2425 }
2426 if (Improved)
2427 ++GCDsuccesses;
2428 DEBUG(dbgs() << "all done\n");
2429 return false;
2430}
2431
2432
2433//===----------------------------------------------------------------------===//
2434// banerjeeMIVtest -
2435// Use Banerjee's Inequalities to test an MIV subscript pair.
2436// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2437// Generally follows the discussion in Section 2.5.2 of
2438//
2439// Optimizing Supercompilers for Supercomputers
2440// Michael Wolfe
2441//
2442// The inequalities given on page 25 are simplified in that loops are
2443// normalized so that the lower bound is always 0 and the stride is always 1.
2444// For example, Wolfe gives
2445//
2446// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2447//
2448// where A_k is the coefficient of the kth index in the source subscript,
2449// B_k is the coefficient of the kth index in the destination subscript,
2450// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2451// index, and N_k is the stride of the kth index. Since all loops are normalized
2452// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2453// equation to
2454//
2455// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2456// = (A^-_k - B_k)^- (U_k - 1) - B_k
2457//
2458// Similar simplifications are possible for the other equations.
2459//
2460// When we can't determine the number of iterations for a loop,
2461// we use NULL as an indicator for the worst case, infinity.
2462// When computing the upper bound, NULL denotes +inf;
2463// for the lower bound, NULL denotes -inf.
2464//
2465// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002466bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2467 const SmallBitVector &Loops,
2468 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002469 DEBUG(dbgs() << "starting Banerjee\n");
2470 ++BanerjeeApplications;
2471 DEBUG(dbgs() << " Src = " << *Src << '\n');
2472 const SCEV *A0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002473 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002474 DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2475 const SCEV *B0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002476 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2477 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002478 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2479 DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2480
2481 // Compute bounds for all the * directions.
2482 DEBUG(dbgs() << "\tBounds[*]\n");
2483 for (unsigned K = 1; K <= MaxLevels; ++K) {
2484 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2485 Bound[K].Direction = Dependence::DVEntry::ALL;
2486 Bound[K].DirSet = Dependence::DVEntry::NONE;
2487 findBoundsALL(A, B, Bound, K);
2488#ifndef NDEBUG
2489 DEBUG(dbgs() << "\t " << K << '\t');
2490 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2491 DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2492 else
2493 DEBUG(dbgs() << "-inf\t");
2494 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2495 DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2496 else
2497 DEBUG(dbgs() << "+inf\n");
2498#endif
2499 }
2500
2501 // Test the *, *, *, ... case.
2502 bool Disproved = false;
2503 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2504 // Explore the direction vector hierarchy.
2505 unsigned DepthExpanded = 0;
2506 unsigned NewDeps = exploreDirections(1, A, B, Bound,
2507 Loops, DepthExpanded, Delta);
2508 if (NewDeps > 0) {
2509 bool Improved = false;
2510 for (unsigned K = 1; K <= CommonLevels; ++K) {
2511 if (Loops[K]) {
2512 unsigned Old = Result.DV[K - 1].Direction;
2513 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2514 Improved |= Old != Result.DV[K - 1].Direction;
2515 if (!Result.DV[K - 1].Direction) {
2516 Improved = false;
2517 Disproved = true;
2518 break;
2519 }
2520 }
2521 }
2522 if (Improved)
2523 ++BanerjeeSuccesses;
2524 }
2525 else {
2526 ++BanerjeeIndependence;
2527 Disproved = true;
2528 }
2529 }
2530 else {
2531 ++BanerjeeIndependence;
2532 Disproved = true;
2533 }
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002534 delete [] Bound;
2535 delete [] A;
2536 delete [] B;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002537 return Disproved;
2538}
2539
2540
2541// Hierarchically expands the direction vector
2542// search space, combining the directions of discovered dependences
2543// in the DirSet field of Bound. Returns the number of distinct
2544// dependences discovered. If the dependence is disproved,
2545// it will return 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002546unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2547 CoefficientInfo *B, BoundInfo *Bound,
2548 const SmallBitVector &Loops,
2549 unsigned &DepthExpanded,
2550 const SCEV *Delta) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002551 if (Level > CommonLevels) {
2552 // record result
2553 DEBUG(dbgs() << "\t[");
2554 for (unsigned K = 1; K <= CommonLevels; ++K) {
2555 if (Loops[K]) {
2556 Bound[K].DirSet |= Bound[K].Direction;
2557#ifndef NDEBUG
2558 switch (Bound[K].Direction) {
2559 case Dependence::DVEntry::LT:
2560 DEBUG(dbgs() << " <");
2561 break;
2562 case Dependence::DVEntry::EQ:
2563 DEBUG(dbgs() << " =");
2564 break;
2565 case Dependence::DVEntry::GT:
2566 DEBUG(dbgs() << " >");
2567 break;
2568 case Dependence::DVEntry::ALL:
2569 DEBUG(dbgs() << " *");
2570 break;
2571 default:
2572 llvm_unreachable("unexpected Bound[K].Direction");
2573 }
2574#endif
2575 }
2576 }
2577 DEBUG(dbgs() << " ]\n");
2578 return 1;
2579 }
2580 if (Loops[Level]) {
2581 if (Level > DepthExpanded) {
2582 DepthExpanded = Level;
2583 // compute bounds for <, =, > at current level
2584 findBoundsLT(A, B, Bound, Level);
2585 findBoundsGT(A, B, Bound, Level);
2586 findBoundsEQ(A, B, Bound, Level);
2587#ifndef NDEBUG
2588 DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2589 DEBUG(dbgs() << "\t <\t");
2590 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2591 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT] << '\t');
2592 else
2593 DEBUG(dbgs() << "-inf\t");
2594 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2595 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT] << '\n');
2596 else
2597 DEBUG(dbgs() << "+inf\n");
2598 DEBUG(dbgs() << "\t =\t");
2599 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2600 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ] << '\t');
2601 else
2602 DEBUG(dbgs() << "-inf\t");
2603 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2604 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ] << '\n');
2605 else
2606 DEBUG(dbgs() << "+inf\n");
2607 DEBUG(dbgs() << "\t >\t");
2608 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2609 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT] << '\t');
2610 else
2611 DEBUG(dbgs() << "-inf\t");
2612 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2613 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT] << '\n');
2614 else
2615 DEBUG(dbgs() << "+inf\n");
2616#endif
2617 }
2618
2619 unsigned NewDeps = 0;
2620
2621 // test bounds for <, *, *, ...
2622 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2623 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2624 Loops, DepthExpanded, Delta);
2625
2626 // Test bounds for =, *, *, ...
2627 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2628 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2629 Loops, DepthExpanded, Delta);
2630
2631 // test bounds for >, *, *, ...
2632 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2633 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2634 Loops, DepthExpanded, Delta);
2635
2636 Bound[Level].Direction = Dependence::DVEntry::ALL;
2637 return NewDeps;
2638 }
2639 else
2640 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
2641}
2642
2643
2644// Returns true iff the current bounds are plausible.
Chandler Carruth49c22192016-05-12 22:19:39 +00002645bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2646 BoundInfo *Bound, const SCEV *Delta) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002647 Bound[Level].Direction = DirKind;
2648 if (const SCEV *LowerBound = getLowerBound(Bound))
2649 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2650 return false;
2651 if (const SCEV *UpperBound = getUpperBound(Bound))
2652 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2653 return false;
2654 return true;
2655}
2656
2657
2658// Computes the upper and lower bounds for level K
2659// using the * direction. Records them in Bound.
2660// Wolfe gives the equations
2661//
2662// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2663// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2664//
2665// Since we normalize loops, we can simplify these equations to
2666//
2667// LB^*_k = (A^-_k - B^+_k)U_k
2668// UB^*_k = (A^+_k - B^-_k)U_k
2669//
2670// We must be careful to handle the case where the upper bound is unknown.
2671// Note that the lower bound is always <= 0
2672// and the upper bound is always >= 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002673void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2674 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002675 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
2676 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002677 if (Bound[K].Iterations) {
2678 Bound[K].Lower[Dependence::DVEntry::ALL] =
2679 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
2680 Bound[K].Iterations);
2681 Bound[K].Upper[Dependence::DVEntry::ALL] =
2682 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
2683 Bound[K].Iterations);
2684 }
2685 else {
2686 // If the difference is 0, we won't need to know the number of iterations.
2687 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2688 Bound[K].Lower[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002689 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002690 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2691 Bound[K].Upper[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002692 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002693 }
2694}
2695
2696
2697// Computes the upper and lower bounds for level K
2698// using the = direction. Records them in Bound.
2699// Wolfe gives the equations
2700//
2701// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2702// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2703//
2704// Since we normalize loops, we can simplify these equations to
2705//
2706// LB^=_k = (A_k - B_k)^- U_k
2707// UB^=_k = (A_k - B_k)^+ U_k
2708//
2709// We must be careful to handle the case where the upper bound is unknown.
2710// Note that the lower bound is always <= 0
2711// and the upper bound is always >= 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002712void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2713 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002714 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
2715 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002716 if (Bound[K].Iterations) {
2717 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2718 const SCEV *NegativePart = getNegativePart(Delta);
2719 Bound[K].Lower[Dependence::DVEntry::EQ] =
2720 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2721 const SCEV *PositivePart = getPositivePart(Delta);
2722 Bound[K].Upper[Dependence::DVEntry::EQ] =
2723 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2724 }
2725 else {
2726 // If the positive/negative part of the difference is 0,
2727 // we won't need to know the number of iterations.
2728 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2729 const SCEV *NegativePart = getNegativePart(Delta);
2730 if (NegativePart->isZero())
2731 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2732 const SCEV *PositivePart = getPositivePart(Delta);
2733 if (PositivePart->isZero())
2734 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2735 }
2736}
2737
2738
2739// Computes the upper and lower bounds for level K
2740// using the < direction. Records them in Bound.
2741// Wolfe gives the equations
2742//
2743// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2744// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2745//
2746// Since we normalize loops, we can simplify these equations to
2747//
2748// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2749// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2750//
2751// We must be careful to handle the case where the upper bound is unknown.
Chandler Carruth49c22192016-05-12 22:19:39 +00002752void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
2753 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002754 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
2755 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002756 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002757 const SCEV *Iter_1 = SE->getMinusSCEV(
2758 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002759 const SCEV *NegPart =
2760 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2761 Bound[K].Lower[Dependence::DVEntry::LT] =
2762 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2763 const SCEV *PosPart =
2764 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2765 Bound[K].Upper[Dependence::DVEntry::LT] =
2766 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2767 }
2768 else {
2769 // If the positive/negative part of the difference is 0,
2770 // we won't need to know the number of iterations.
2771 const SCEV *NegPart =
2772 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2773 if (NegPart->isZero())
2774 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2775 const SCEV *PosPart =
2776 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2777 if (PosPart->isZero())
2778 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2779 }
2780}
2781
2782
2783// Computes the upper and lower bounds for level K
2784// using the > direction. Records them in Bound.
2785// Wolfe gives the equations
2786//
2787// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2788// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2789//
2790// Since we normalize loops, we can simplify these equations to
2791//
2792// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2793// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2794//
2795// We must be careful to handle the case where the upper bound is unknown.
Chandler Carruth49c22192016-05-12 22:19:39 +00002796void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
2797 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002798 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
2799 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002800 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002801 const SCEV *Iter_1 = SE->getMinusSCEV(
2802 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002803 const SCEV *NegPart =
2804 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2805 Bound[K].Lower[Dependence::DVEntry::GT] =
2806 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2807 const SCEV *PosPart =
2808 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2809 Bound[K].Upper[Dependence::DVEntry::GT] =
2810 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2811 }
2812 else {
2813 // If the positive/negative part of the difference is 0,
2814 // we won't need to know the number of iterations.
2815 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2816 if (NegPart->isZero())
2817 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2818 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2819 if (PosPart->isZero())
2820 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2821 }
2822}
2823
2824
2825// X^+ = max(X, 0)
Chandler Carruth49c22192016-05-12 22:19:39 +00002826const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002827 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002828}
2829
2830
2831// X^- = min(X, 0)
Chandler Carruth49c22192016-05-12 22:19:39 +00002832const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002833 return SE->getSMinExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002834}
2835
2836
2837// Walks through the subscript,
2838// collecting each coefficient, the associated loop bounds,
2839// and recording its positive and negative parts for later use.
Chandler Carruth49c22192016-05-12 22:19:39 +00002840DependenceInfo::CoefficientInfo *
2841DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
2842 const SCEV *&Constant) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002843 const SCEV *Zero = SE->getZero(Subscript->getType());
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002844 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002845 for (unsigned K = 1; K <= MaxLevels; ++K) {
2846 CI[K].Coeff = Zero;
2847 CI[K].PosPart = Zero;
2848 CI[K].NegPart = Zero;
Craig Topper9f008862014-04-15 04:59:12 +00002849 CI[K].Iterations = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002850 }
2851 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2852 const Loop *L = AddRec->getLoop();
2853 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2854 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2855 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2856 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2857 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2858 Subscript = AddRec->getStart();
2859 }
2860 Constant = Subscript;
2861#ifndef NDEBUG
2862 DEBUG(dbgs() << "\tCoefficient Info\n");
2863 for (unsigned K = 1; K <= MaxLevels; ++K) {
2864 DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
2865 DEBUG(dbgs() << "\tPos Part = ");
2866 DEBUG(dbgs() << *CI[K].PosPart);
2867 DEBUG(dbgs() << "\tNeg Part = ");
2868 DEBUG(dbgs() << *CI[K].NegPart);
2869 DEBUG(dbgs() << "\tUpper Bound = ");
2870 if (CI[K].Iterations)
2871 DEBUG(dbgs() << *CI[K].Iterations);
2872 else
2873 DEBUG(dbgs() << "+inf");
2874 DEBUG(dbgs() << '\n');
2875 }
2876 DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
2877#endif
2878 return CI;
2879}
2880
2881
2882// Looks through all the bounds info and
2883// computes the lower bound given the current direction settings
2884// at each level. If the lower bound for any level is -inf,
2885// the result is -inf.
Chandler Carruth49c22192016-05-12 22:19:39 +00002886const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002887 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2888 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2889 if (Bound[K].Lower[Bound[K].Direction])
2890 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2891 else
Craig Topper9f008862014-04-15 04:59:12 +00002892 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002893 }
2894 return Sum;
2895}
2896
2897
2898// Looks through all the bounds info and
2899// computes the upper bound given the current direction settings
2900// at each level. If the upper bound at any level is +inf,
2901// the result is +inf.
Chandler Carruth49c22192016-05-12 22:19:39 +00002902const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002903 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2904 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2905 if (Bound[K].Upper[Bound[K].Direction])
2906 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2907 else
Craig Topper9f008862014-04-15 04:59:12 +00002908 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002909 }
2910 return Sum;
2911}
2912
2913
2914//===----------------------------------------------------------------------===//
2915// Constraint manipulation for Delta test.
2916
2917// Given a linear SCEV,
2918// return the coefficient (the step)
2919// corresponding to the specified loop.
2920// If there isn't one, return 0.
Jingyue Wua84feb12015-05-29 16:58:08 +00002921// For example, given a*i + b*j + c*k, finding the coefficient
Sebastian Pop59b61b92012-10-11 07:32:34 +00002922// corresponding to the j loop would yield b.
Chandler Carruth49c22192016-05-12 22:19:39 +00002923const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
2924 const Loop *TargetLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002925 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2926 if (!AddRec)
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002927 return SE->getZero(Expr->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002928 if (AddRec->getLoop() == TargetLoop)
2929 return AddRec->getStepRecurrence(*SE);
2930 return findCoefficient(AddRec->getStart(), TargetLoop);
2931}
2932
2933
2934// Given a linear SCEV,
2935// return the SCEV given by zeroing out the coefficient
2936// corresponding to the specified loop.
2937// For example, given a*i + b*j + c*k, zeroing the coefficient
2938// corresponding to the j loop would yield a*i + c*k.
Chandler Carruth49c22192016-05-12 22:19:39 +00002939const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
2940 const Loop *TargetLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002941 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2942 if (!AddRec)
2943 return Expr; // ignore
2944 if (AddRec->getLoop() == TargetLoop)
2945 return AddRec->getStart();
2946 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
2947 AddRec->getStepRecurrence(*SE),
2948 AddRec->getLoop(),
2949 AddRec->getNoWrapFlags());
2950}
2951
2952
2953// Given a linear SCEV Expr,
2954// return the SCEV given by adding some Value to the
2955// coefficient corresponding to the specified TargetLoop.
2956// For example, given a*i + b*j + c*k, adding 1 to the coefficient
2957// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
Chandler Carruth49c22192016-05-12 22:19:39 +00002958const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
2959 const Loop *TargetLoop,
2960 const SCEV *Value) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002961 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2962 if (!AddRec) // create a new addRec
2963 return SE->getAddRecExpr(Expr,
2964 Value,
2965 TargetLoop,
2966 SCEV::FlagAnyWrap); // Worst case, with no info.
2967 if (AddRec->getLoop() == TargetLoop) {
2968 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
2969 if (Sum->isZero())
2970 return AddRec->getStart();
2971 return SE->getAddRecExpr(AddRec->getStart(),
2972 Sum,
2973 AddRec->getLoop(),
2974 AddRec->getNoWrapFlags());
2975 }
Preston Briggs6c286b62013-06-28 18:44:48 +00002976 if (SE->isLoopInvariant(AddRec, TargetLoop))
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +00002977 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
2978 return SE->getAddRecExpr(
2979 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
2980 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
2981 AddRec->getNoWrapFlags());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002982}
2983
2984
2985// Review the constraints, looking for opportunities
2986// to simplify a subscript pair (Src and Dst).
2987// Return true if some simplification occurs.
2988// If the simplification isn't exact (that is, if it is conservative
2989// in terms of dependence), set consistent to false.
2990// Corresponds to Figure 5 from the paper
2991//
2992// Practical Dependence Testing
2993// Goff, Kennedy, Tseng
2994// PLDI 1991
Chandler Carruth49c22192016-05-12 22:19:39 +00002995bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
2996 SmallBitVector &Loops,
2997 SmallVectorImpl<Constraint> &Constraints,
2998 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002999 bool Result = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003000 for (unsigned LI : Loops.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003001 DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
3002 DEBUG(Constraints[LI].dump(dbgs()));
3003 if (Constraints[LI].isDistance())
3004 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3005 else if (Constraints[LI].isLine())
3006 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3007 else if (Constraints[LI].isPoint())
3008 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3009 }
3010 return Result;
3011}
3012
3013
3014// Attempt to propagate a distance
3015// constraint into a subscript pair (Src and Dst).
3016// Return true if some simplification occurs.
3017// If the simplification isn't exact (that is, if it is conservative
3018// in terms of dependence), set consistent to false.
Chandler Carruth49c22192016-05-12 22:19:39 +00003019bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
3020 Constraint &CurConstraint,
3021 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003022 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3023 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3024 const SCEV *A_K = findCoefficient(Src, CurLoop);
3025 if (A_K->isZero())
3026 return false;
3027 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3028 Src = SE->getMinusSCEV(Src, DA_K);
3029 Src = zeroCoefficient(Src, CurLoop);
3030 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3031 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3032 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
3033 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3034 if (!findCoefficient(Dst, CurLoop)->isZero())
3035 Consistent = false;
3036 return true;
3037}
3038
3039
3040// Attempt to propagate a line
3041// constraint into a subscript pair (Src and Dst).
3042// Return true if some simplification occurs.
3043// If the simplification isn't exact (that is, if it is conservative
3044// in terms of dependence), set consistent to false.
Chandler Carruth49c22192016-05-12 22:19:39 +00003045bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
3046 Constraint &CurConstraint,
3047 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003048 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3049 const SCEV *A = CurConstraint.getA();
3050 const SCEV *B = CurConstraint.getB();
3051 const SCEV *C = CurConstraint.getC();
3052 DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C << "\n");
3053 DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3054 DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
3055 if (A->isZero()) {
3056 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3057 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3058 if (!Bconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003059 APInt Beta = Bconst->getAPInt();
3060 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003061 APInt CdivB = Charlie.sdiv(Beta);
3062 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3063 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3064 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3065 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3066 Dst = zeroCoefficient(Dst, CurLoop);
3067 if (!findCoefficient(Src, CurLoop)->isZero())
3068 Consistent = false;
3069 }
3070 else if (B->isZero()) {
3071 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3072 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3073 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003074 APInt Alpha = Aconst->getAPInt();
3075 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003076 APInt CdivA = Charlie.sdiv(Alpha);
3077 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3078 const SCEV *A_K = findCoefficient(Src, CurLoop);
3079 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3080 Src = zeroCoefficient(Src, CurLoop);
3081 if (!findCoefficient(Dst, CurLoop)->isZero())
3082 Consistent = false;
3083 }
3084 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3085 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3086 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3087 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003088 APInt Alpha = Aconst->getAPInt();
3089 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003090 APInt CdivA = Charlie.sdiv(Alpha);
3091 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3092 const SCEV *A_K = findCoefficient(Src, CurLoop);
3093 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3094 Src = zeroCoefficient(Src, CurLoop);
3095 Dst = addToCoefficient(Dst, CurLoop, A_K);
3096 if (!findCoefficient(Dst, CurLoop)->isZero())
3097 Consistent = false;
3098 }
3099 else {
3100 // paper is incorrect here, or perhaps just misleading
3101 const SCEV *A_K = findCoefficient(Src, CurLoop);
3102 Src = SE->getMulExpr(Src, A);
3103 Dst = SE->getMulExpr(Dst, A);
3104 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3105 Src = zeroCoefficient(Src, CurLoop);
3106 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3107 if (!findCoefficient(Dst, CurLoop)->isZero())
3108 Consistent = false;
3109 }
3110 DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3111 DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
3112 return true;
3113}
3114
3115
3116// Attempt to propagate a point
3117// constraint into a subscript pair (Src and Dst).
3118// Return true if some simplification occurs.
Chandler Carruth49c22192016-05-12 22:19:39 +00003119bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
3120 Constraint &CurConstraint) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003121 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3122 const SCEV *A_K = findCoefficient(Src, CurLoop);
3123 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3124 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3125 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3126 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3127 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3128 Src = zeroCoefficient(Src, CurLoop);
3129 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3130 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3131 Dst = zeroCoefficient(Dst, CurLoop);
3132 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3133 return true;
3134}
3135
3136
3137// Update direction vector entry based on the current constraint.
Chandler Carruth49c22192016-05-12 22:19:39 +00003138void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
3139 const Constraint &CurConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003140 DEBUG(dbgs() << "\tUpdate direction, constraint =");
3141 DEBUG(CurConstraint.dump(dbgs()));
3142 if (CurConstraint.isAny())
3143 ; // use defaults
3144 else if (CurConstraint.isDistance()) {
3145 // this one is consistent, the others aren't
3146 Level.Scalar = false;
3147 Level.Distance = CurConstraint.getD();
3148 unsigned NewDirection = Dependence::DVEntry::NONE;
3149 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3150 NewDirection = Dependence::DVEntry::EQ;
3151 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3152 NewDirection |= Dependence::DVEntry::LT;
3153 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3154 NewDirection |= Dependence::DVEntry::GT;
3155 Level.Direction &= NewDirection;
3156 }
3157 else if (CurConstraint.isLine()) {
3158 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003159 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003160 // direction should be accurate
3161 }
3162 else if (CurConstraint.isPoint()) {
3163 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003164 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003165 unsigned NewDirection = Dependence::DVEntry::NONE;
3166 if (!isKnownPredicate(CmpInst::ICMP_NE,
3167 CurConstraint.getY(),
3168 CurConstraint.getX()))
3169 // if X may be = Y
3170 NewDirection |= Dependence::DVEntry::EQ;
3171 if (!isKnownPredicate(CmpInst::ICMP_SLE,
3172 CurConstraint.getY(),
3173 CurConstraint.getX()))
3174 // if Y may be > X
3175 NewDirection |= Dependence::DVEntry::LT;
3176 if (!isKnownPredicate(CmpInst::ICMP_SGE,
3177 CurConstraint.getY(),
3178 CurConstraint.getX()))
3179 // if Y may be < X
3180 NewDirection |= Dependence::DVEntry::GT;
3181 Level.Direction &= NewDirection;
3182 }
3183 else
3184 llvm_unreachable("constraint has unexpected kind");
3185}
3186
Sebastian Popc62c6792013-11-12 22:47:20 +00003187/// Check if we can delinearize the subscripts. If the SCEVs representing the
3188/// source and destination array references are recurrences on a nested loop,
Alp Tokercb402912014-01-24 17:20:08 +00003189/// this function flattens the nested recurrences into separate recurrences
Sebastian Popc62c6792013-11-12 22:47:20 +00003190/// for each loop level.
Chandler Carruth49c22192016-05-12 22:19:39 +00003191bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3192 SmallVectorImpl<Subscript> &Pair) {
Renato Golin038ede22018-03-09 21:05:58 +00003193 assert(isLoadOrStore(Src) && "instruction is not load or store");
3194 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3195 Value *SrcPtr = getLoadStorePointerOperand(Src);
3196 Value *DstPtr = getLoadStorePointerOperand(Dst);
Hal Finkel0ef2b102015-08-19 02:56:36 +00003197
3198 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3199 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3200
3201 // Below code mimics the code in Delinearization.cpp
3202 const SCEV *SrcAccessFn =
3203 SE->getSCEVAtScope(SrcPtr, SrcLoop);
3204 const SCEV *DstAccessFn =
3205 SE->getSCEVAtScope(DstPtr, DstLoop);
3206
Sebastian Pop28e6b972014-05-27 22:41:51 +00003207 const SCEVUnknown *SrcBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003208 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003209 const SCEVUnknown *DstBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003210 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003211
3212 if (!SrcBase || !DstBase || SrcBase != DstBase)
3213 return false;
3214
Hal Finkel0ef2b102015-08-19 02:56:36 +00003215 const SCEV *ElementSize = SE->getElementSize(Src);
3216 if (ElementSize != SE->getElementSize(Dst))
3217 return false;
3218
3219 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3220 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
Sebastian Pop28e6b972014-05-27 22:41:51 +00003221
Sebastian Popc62c6792013-11-12 22:47:20 +00003222 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3223 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3224 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3225 return false;
3226
Sebastian Pop448712b2014-05-07 18:01:20 +00003227 // First step: collect parametric terms in both array references.
3228 SmallVector<const SCEV *, 4> Terms;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003229 SE->collectParametricTerms(SrcAR, Terms);
3230 SE->collectParametricTerms(DstAR, Terms);
Sebastian Popc62c6792013-11-12 22:47:20 +00003231
Sebastian Pop448712b2014-05-07 18:01:20 +00003232 // Second step: find subscript sizes.
3233 SmallVector<const SCEV *, 4> Sizes;
Sebastian Popa6e58602014-05-27 22:41:45 +00003234 SE->findArrayDimensions(Terms, Sizes, ElementSize);
Sebastian Pop448712b2014-05-07 18:01:20 +00003235
3236 // Third step: compute the access functions for each subscript.
3237 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003238 SE->computeAccessFunctions(SrcAR, SrcSubscripts, Sizes);
3239 SE->computeAccessFunctions(DstAR, DstSubscripts, Sizes);
Sebastian Pop448712b2014-05-07 18:01:20 +00003240
Sebastian Pop5133d2e2014-02-21 18:15:07 +00003241 // Fail when there is only a subscript: that's a linearized access function.
Sebastian Pop448712b2014-05-07 18:01:20 +00003242 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3243 SrcSubscripts.size() != DstSubscripts.size())
Sebastian Popc62c6792013-11-12 22:47:20 +00003244 return false;
3245
Sebastian Pop448712b2014-05-07 18:01:20 +00003246 int size = SrcSubscripts.size();
Sebastian Pop29026d32014-02-21 18:15:11 +00003247
Sebastian Pop448712b2014-05-07 18:01:20 +00003248 DEBUG({
3249 dbgs() << "\nSrcSubscripts: ";
3250 for (int i = 0; i < size; i++)
3251 dbgs() << *SrcSubscripts[i];
3252 dbgs() << "\nDstSubscripts: ";
3253 for (int i = 0; i < size; i++)
3254 dbgs() << *DstSubscripts[i];
3255 });
Sebastian Popc62c6792013-11-12 22:47:20 +00003256
Sebastian Pop7ee14722013-11-13 22:37:58 +00003257 // The delinearization transforms a single-subscript MIV dependence test into
3258 // a multi-subscript SIV dependence test that is easier to compute. So we
3259 // resize Pair to contain as many pairs of subscripts as the delinearization
3260 // has found, and then initialize the pairs following the delinearization.
Sebastian Popc62c6792013-11-12 22:47:20 +00003261 Pair.resize(size);
3262 for (int i = 0; i < size; ++i) {
3263 Pair[i].Src = SrcSubscripts[i];
3264 Pair[i].Dst = DstSubscripts[i];
Jingyue Wu0fa125a2014-11-16 16:52:44 +00003265 unifySubscriptType(&Pair[i]);
Sebastian Pop7ee14722013-11-13 22:37:58 +00003266
3267 // FIXME: we should record the bounds SrcSizes[i] and DstSizes[i] that the
3268 // delinearization has found, and add these constraints to the dependence
3269 // check to avoid memory accesses overflow from one dimension into another.
3270 // This is related to the problem of determining the existence of data
3271 // dependences in array accesses using a different number of subscripts: in
3272 // C one can access an array A[100][100]; as A[0][9999], *A[9999], etc.
Sebastian Popc62c6792013-11-12 22:47:20 +00003273 }
3274
3275 return true;
3276}
Sebastian Pop59b61b92012-10-11 07:32:34 +00003277
3278//===----------------------------------------------------------------------===//
3279
3280#ifndef NDEBUG
3281// For debugging purposes, dump a small bit vector to dbgs().
3282static void dumpSmallBitVector(SmallBitVector &BV) {
3283 dbgs() << "{";
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003284 for (unsigned VI : BV.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003285 dbgs() << VI;
3286 if (BV.find_next(VI) >= 0)
3287 dbgs() << ' ';
3288 }
3289 dbgs() << "}\n";
3290}
3291#endif
3292
Sebastian Pop59b61b92012-10-11 07:32:34 +00003293// depends -
3294// Returns NULL if there is no dependence.
3295// Otherwise, return a Dependence with as many details as possible.
3296// Corresponds to Section 3.1 in the paper
3297//
3298// Practical Dependence Testing
3299// Goff, Kennedy, Tseng
3300// PLDI 1991
3301//
Preston Briggs3ad39492012-11-21 23:50:04 +00003302// Care is required to keep the routine below, getSplitIteration(),
3303// up to date with respect to this routine.
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003304std::unique_ptr<Dependence>
Chandler Carruth49c22192016-05-12 22:19:39 +00003305DependenceInfo::depends(Instruction *Src, Instruction *Dst,
3306 bool PossiblyLoopIndependent) {
Preston Briggs1084fa22012-11-27 06:41:46 +00003307 if (Src == Dst)
3308 PossiblyLoopIndependent = false;
3309
Sebastian Pop59b61b92012-10-11 07:32:34 +00003310 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) ||
3311 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory()))
3312 // if both instructions don't reference memory, there's no dependence
Craig Topper9f008862014-04-15 04:59:12 +00003313 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003314
Preston Briggs3ad39492012-11-21 23:50:04 +00003315 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003316 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
Preston Briggs3ad39492012-11-21 23:50:04 +00003317 DEBUG(dbgs() << "can only handle simple loads and stores\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003318 return make_unique<Dependence>(Src, Dst);
Preston Briggs3ad39492012-11-21 23:50:04 +00003319 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003320
Renato Golin038ede22018-03-09 21:05:58 +00003321 assert(isLoadOrStore(Src) && "instruction is not load or store");
3322 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3323 Value *SrcPtr = getLoadStorePointerOperand(Src);
3324 Value *DstPtr = getLoadStorePointerOperand(Dst);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003325
David Green5ef933b2018-04-10 11:37:21 +00003326 switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
3327 MemoryLocation::get(Dst),
3328 MemoryLocation::get(Src))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003329 case MayAlias:
3330 case PartialAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003331 // cannot analyse objects if we don't understand their aliasing.
Preston Briggs3ad39492012-11-21 23:50:04 +00003332 DEBUG(dbgs() << "can't analyze may or partial alias\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003333 return make_unique<Dependence>(Src, Dst);
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003334 case NoAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003335 // If the objects noalias, they are distinct, accesses are independent.
Preston Briggs3ad39492012-11-21 23:50:04 +00003336 DEBUG(dbgs() << "no alias\n");
Craig Topper9f008862014-04-15 04:59:12 +00003337 return nullptr;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003338 case MustAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003339 break; // The underlying objects alias; test accesses for dependence.
3340 }
3341
Sebastian Pop59b61b92012-10-11 07:32:34 +00003342 // establish loop nesting levels
3343 establishNestingLevels(Src, Dst);
3344 DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3345 DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3346
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003347 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003348 ++TotalArrayPairs;
3349
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003350 unsigned Pairs = 1;
3351 SmallVector<Subscript, 2> Pair(Pairs);
3352 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3353 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3354 DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3355 DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3356 Pair[0].Src = SrcSCEV;
3357 Pair[0].Dst = DstSCEV;
Preston Briggs3ad39492012-11-21 23:50:04 +00003358
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003359 if (Delinearize) {
Hal Finkel0ef2b102015-08-19 02:56:36 +00003360 if (tryDelinearize(Src, Dst, Pair)) {
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003361 DEBUG(dbgs() << " delinearized\n");
Hal Finkel0ef2b102015-08-19 02:56:36 +00003362 Pairs = Pair.size();
3363 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003364 }
3365
Preston Briggs3ad39492012-11-21 23:50:04 +00003366 for (unsigned P = 0; P < Pairs; ++P) {
3367 Pair[P].Loops.resize(MaxLevels + 1);
3368 Pair[P].GroupLoops.resize(MaxLevels + 1);
3369 Pair[P].Group.resize(Pairs);
3370 removeMatchingExtensions(&Pair[P]);
3371 Pair[P].Classification =
3372 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3373 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3374 Pair[P].Loops);
3375 Pair[P].GroupLoops = Pair[P].Loops;
3376 Pair[P].Group.set(P);
3377 DEBUG(dbgs() << " subscript " << P << "\n");
3378 DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3379 DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3380 DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003381 DEBUG(dbgs() << "\tloops = ");
Preston Briggs3ad39492012-11-21 23:50:04 +00003382 DEBUG(dumpSmallBitVector(Pair[P].Loops));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003383 }
3384
3385 SmallBitVector Separable(Pairs);
3386 SmallBitVector Coupled(Pairs);
3387
3388 // Partition subscripts into separable and minimally-coupled groups
3389 // Algorithm in paper is algorithmically better;
3390 // this may be faster in practice. Check someday.
3391 //
3392 // Here's an example of how it works. Consider this code:
3393 //
3394 // for (i = ...) {
3395 // for (j = ...) {
3396 // for (k = ...) {
3397 // for (l = ...) {
3398 // for (m = ...) {
3399 // A[i][j][k][m] = ...;
3400 // ... = A[0][j][l][i + j];
3401 // }
3402 // }
3403 // }
3404 // }
3405 // }
3406 //
3407 // There are 4 subscripts here:
3408 // 0 [i] and [0]
3409 // 1 [j] and [j]
3410 // 2 [k] and [l]
3411 // 3 [m] and [i + j]
3412 //
3413 // We've already classified each subscript pair as ZIV, SIV, etc.,
3414 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3415 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3416 // and set Pair[P].Group = {P}.
3417 //
3418 // Src Dst Classification Loops GroupLoops Group
3419 // 0 [i] [0] SIV {1} {1} {0}
3420 // 1 [j] [j] SIV {2} {2} {1}
3421 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3422 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3423 //
3424 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3425 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3426 //
3427 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3428 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3429 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3430 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3431 // to either Separable or Coupled).
3432 //
3433 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3434 // Next, 1 and 3. The intersectionof their GroupLoops = {2}, not empty,
3435 // so Pair[3].Group = {0, 1, 3} and Done = false.
3436 //
3437 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3438 // Since Done remains true, we add 2 to the set of Separable pairs.
3439 //
3440 // Finally, we consider 3. There's nothing to compare it with,
3441 // so Done remains true and we add it to the Coupled set.
3442 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3443 //
3444 // In the end, we've got 1 separable subscript and 1 coupled group.
3445 for (unsigned SI = 0; SI < Pairs; ++SI) {
3446 if (Pair[SI].Classification == Subscript::NonLinear) {
3447 // ignore these, but collect loops for later
3448 ++NonlinearSubscriptPairs;
3449 collectCommonLoops(Pair[SI].Src,
3450 LI->getLoopFor(Src->getParent()),
3451 Pair[SI].Loops);
3452 collectCommonLoops(Pair[SI].Dst,
3453 LI->getLoopFor(Dst->getParent()),
3454 Pair[SI].Loops);
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003455 Result.Consistent = false;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003456 } else if (Pair[SI].Classification == Subscript::ZIV) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003457 // always separable
3458 Separable.set(SI);
3459 }
3460 else {
3461 // SIV, RDIV, or MIV, so check for coupled group
3462 bool Done = true;
3463 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3464 SmallBitVector Intersection = Pair[SI].GroupLoops;
3465 Intersection &= Pair[SJ].GroupLoops;
3466 if (Intersection.any()) {
3467 // accumulate set of all the loops in group
3468 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3469 // accumulate set of all subscripts in group
3470 Pair[SJ].Group |= Pair[SI].Group;
3471 Done = false;
3472 }
3473 }
3474 if (Done) {
3475 if (Pair[SI].Group.count() == 1) {
3476 Separable.set(SI);
3477 ++SeparableSubscriptPairs;
3478 }
3479 else {
3480 Coupled.set(SI);
3481 ++CoupledSubscriptPairs;
3482 }
3483 }
3484 }
3485 }
3486
3487 DEBUG(dbgs() << " Separable = ");
3488 DEBUG(dumpSmallBitVector(Separable));
3489 DEBUG(dbgs() << " Coupled = ");
3490 DEBUG(dumpSmallBitVector(Coupled));
3491
3492 Constraint NewConstraint;
3493 NewConstraint.setAny(SE);
3494
3495 // test separable subscripts
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003496 for (unsigned SI : Separable.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003497 DEBUG(dbgs() << "testing subscript " << SI);
3498 switch (Pair[SI].Classification) {
3499 case Subscript::ZIV:
3500 DEBUG(dbgs() << ", ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003501 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003502 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003503 break;
3504 case Subscript::SIV: {
3505 DEBUG(dbgs() << ", SIV\n");
3506 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003507 const SCEV *SplitIter = nullptr;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003508 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
3509 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003510 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003511 break;
3512 }
3513 case Subscript::RDIV:
3514 DEBUG(dbgs() << ", RDIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003515 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003516 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003517 break;
3518 case Subscript::MIV:
3519 DEBUG(dbgs() << ", MIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003520 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003521 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003522 break;
3523 default:
3524 llvm_unreachable("subscript has unexpected classification");
3525 }
3526 }
3527
3528 if (Coupled.count()) {
3529 // test coupled subscript groups
3530 DEBUG(dbgs() << "starting on coupled subscripts\n");
3531 DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
3532 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3533 for (unsigned II = 0; II <= MaxLevels; ++II)
3534 Constraints[II].setAny(SE);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003535 for (unsigned SI : Coupled.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003536 DEBUG(dbgs() << "testing subscript group " << SI << " { ");
3537 SmallBitVector Group(Pair[SI].Group);
3538 SmallBitVector Sivs(Pairs);
3539 SmallBitVector Mivs(Pairs);
3540 SmallBitVector ConstrainedLevels(MaxLevels + 1);
Jingyue Wua84feb12015-05-29 16:58:08 +00003541 SmallVector<Subscript *, 4> PairsInGroup;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003542 for (unsigned SJ : Group.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003543 DEBUG(dbgs() << SJ << " ");
3544 if (Pair[SJ].Classification == Subscript::SIV)
3545 Sivs.set(SJ);
3546 else
3547 Mivs.set(SJ);
Jingyue Wua84feb12015-05-29 16:58:08 +00003548 PairsInGroup.push_back(&Pair[SJ]);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003549 }
Jingyue Wua84feb12015-05-29 16:58:08 +00003550 unifySubscriptType(PairsInGroup);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003551 DEBUG(dbgs() << "}\n");
3552 while (Sivs.any()) {
3553 bool Changed = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003554 for (unsigned SJ : Sivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003555 DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
3556 // SJ is an SIV subscript that's part of the current coupled group
3557 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003558 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003559 DEBUG(dbgs() << "SIV\n");
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003560 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
3561 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003562 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003563 ConstrainedLevels.set(Level);
3564 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3565 if (Constraints[Level].isEmpty()) {
3566 ++DeltaIndependence;
Craig Topper9f008862014-04-15 04:59:12 +00003567 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003568 }
3569 Changed = true;
3570 }
3571 Sivs.reset(SJ);
3572 }
3573 if (Changed) {
3574 // propagate, possibly creating new SIVs and ZIVs
3575 DEBUG(dbgs() << " propagating\n");
3576 DEBUG(dbgs() << "\tMivs = ");
3577 DEBUG(dumpSmallBitVector(Mivs));
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003578 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003579 // SJ is an MIV subscript that's part of the current coupled group
3580 DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
3581 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003582 Constraints, Result.Consistent)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003583 DEBUG(dbgs() << "\t Changed\n");
3584 ++DeltaPropagations;
3585 Pair[SJ].Classification =
3586 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3587 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3588 Pair[SJ].Loops);
3589 switch (Pair[SJ].Classification) {
3590 case Subscript::ZIV:
3591 DEBUG(dbgs() << "ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003592 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003593 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003594 Mivs.reset(SJ);
3595 break;
3596 case Subscript::SIV:
3597 Sivs.set(SJ);
3598 Mivs.reset(SJ);
3599 break;
3600 case Subscript::RDIV:
3601 case Subscript::MIV:
3602 break;
3603 default:
3604 llvm_unreachable("bad subscript classification");
3605 }
3606 }
3607 }
3608 }
3609 }
3610
3611 // test & propagate remaining RDIVs
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003612 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003613 if (Pair[SJ].Classification == Subscript::RDIV) {
3614 DEBUG(dbgs() << "RDIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003615 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003616 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003617 // I don't yet understand how to propagate RDIV results
3618 Mivs.reset(SJ);
3619 }
3620 }
3621
3622 // test remaining MIVs
3623 // This code is temporary.
3624 // Better to somehow test all remaining subscripts simultaneously.
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003625 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003626 if (Pair[SJ].Classification == Subscript::MIV) {
3627 DEBUG(dbgs() << "MIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003628 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003629 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003630 }
3631 else
3632 llvm_unreachable("expected only MIV subscripts at this point");
3633 }
3634
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003635 // update Result.DV from constraint vector
Sebastian Pop59b61b92012-10-11 07:32:34 +00003636 DEBUG(dbgs() << " updating\n");
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003637 for (unsigned SJ : ConstrainedLevels.set_bits()) {
3638 if (SJ > CommonLevels)
Karthik Bhat8d7f7ed2015-03-10 14:32:02 +00003639 break;
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003640 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3641 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
Craig Topper9f008862014-04-15 04:59:12 +00003642 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003643 }
3644 }
3645 }
3646
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003647 // Make sure the Scalar flags are set correctly.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003648 SmallBitVector CompleteLoops(MaxLevels + 1);
3649 for (unsigned SI = 0; SI < Pairs; ++SI)
3650 CompleteLoops |= Pair[SI].Loops;
3651 for (unsigned II = 1; II <= CommonLevels; ++II)
3652 if (CompleteLoops[II])
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003653 Result.DV[II - 1].Scalar = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003654
Sebastian Pop59b61b92012-10-11 07:32:34 +00003655 if (PossiblyLoopIndependent) {
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003656 // Make sure the LoopIndependent flag is set correctly.
3657 // All directions must include equal, otherwise no
3658 // loop-independent dependence is possible.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003659 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003660 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3661 Result.LoopIndependent = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003662 break;
3663 }
3664 }
3665 }
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003666 else {
3667 // On the other hand, if all directions are equal and there's no
3668 // loop-independent dependence possible, then no dependence exists.
3669 bool AllEqual = true;
3670 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003671 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003672 AllEqual = false;
3673 break;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003674 }
3675 }
3676 if (AllEqual)
Craig Topper9f008862014-04-15 04:59:12 +00003677 return nullptr;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003678 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003679
David Blaikie47039dc2015-07-31 21:37:09 +00003680 return make_unique<FullDependence>(std::move(Result));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003681}
3682
3683
3684
3685//===----------------------------------------------------------------------===//
3686// getSplitIteration -
3687// Rather than spend rarely-used space recording the splitting iteration
3688// during the Weak-Crossing SIV test, we re-compute it on demand.
3689// The re-computation is basically a repeat of the entire dependence test,
3690// though simplified since we know that the dependence exists.
3691// It's tedious, since we must go through all propagations, etc.
3692//
Preston Briggs3ad39492012-11-21 23:50:04 +00003693// Care is required to keep this code up to date with respect to the routine
3694// above, depends().
Sebastian Pop59b61b92012-10-11 07:32:34 +00003695//
3696// Generally, the dependence analyzer will be used to build
3697// a dependence graph for a function (basically a map from instructions
3698// to dependences). Looking for cycles in the graph shows us loops
3699// that cannot be trivially vectorized/parallelized.
3700//
3701// We can try to improve the situation by examining all the dependences
3702// that make up the cycle, looking for ones we can break.
3703// Sometimes, peeling the first or last iteration of a loop will break
3704// dependences, and we've got flags for those possibilities.
3705// Sometimes, splitting a loop at some other iteration will do the trick,
3706// and we've got a flag for that case. Rather than waste the space to
3707// record the exact iteration (since we rarely know), we provide
3708// a method that calculates the iteration. It's a drag that it must work
3709// from scratch, but wonderful in that it's possible.
3710//
3711// Here's an example:
3712//
3713// for (i = 0; i < 10; i++)
3714// A[i] = ...
3715// ... = A[11 - i]
3716//
3717// There's a loop-carried flow dependence from the store to the load,
3718// found by the weak-crossing SIV test. The dependence will have a flag,
3719// indicating that the dependence can be broken by splitting the loop.
3720// Calling getSplitIteration will return 5.
3721// Splitting the loop breaks the dependence, like so:
3722//
3723// for (i = 0; i <= 5; i++)
3724// A[i] = ...
3725// ... = A[11 - i]
3726// for (i = 6; i < 10; i++)
3727// A[i] = ...
3728// ... = A[11 - i]
3729//
3730// breaks the dependence and allows us to vectorize/parallelize
3731// both loops.
Chandler Carruth49c22192016-05-12 22:19:39 +00003732const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
3733 unsigned SplitLevel) {
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003734 assert(Dep.isSplitable(SplitLevel) &&
Sebastian Pop59b61b92012-10-11 07:32:34 +00003735 "Dep should be splitable at SplitLevel");
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003736 Instruction *Src = Dep.getSrc();
3737 Instruction *Dst = Dep.getDst();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003738 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
3739 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
3740 assert(isLoadOrStore(Src));
3741 assert(isLoadOrStore(Dst));
Renato Golin038ede22018-03-09 21:05:58 +00003742 Value *SrcPtr = getLoadStorePointerOperand(Src);
3743 Value *DstPtr = getLoadStorePointerOperand(Dst);
David Green5ef933b2018-04-10 11:37:21 +00003744 assert(underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
3745 MemoryLocation::get(Dst),
3746 MemoryLocation::get(Src)) == MustAlias);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003747
3748 // establish loop nesting levels
3749 establishNestingLevels(Src, Dst);
3750
3751 FullDependence Result(Src, Dst, false, CommonLevels);
3752
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003753 unsigned Pairs = 1;
3754 SmallVector<Subscript, 2> Pair(Pairs);
3755 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3756 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3757 Pair[0].Src = SrcSCEV;
3758 Pair[0].Dst = DstSCEV;
Preston Briggs3ad39492012-11-21 23:50:04 +00003759
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003760 if (Delinearize) {
Hal Finkel0ef2b102015-08-19 02:56:36 +00003761 if (tryDelinearize(Src, Dst, Pair)) {
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003762 DEBUG(dbgs() << " delinearized\n");
Hal Finkel0ef2b102015-08-19 02:56:36 +00003763 Pairs = Pair.size();
3764 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003765 }
3766
Preston Briggs3ad39492012-11-21 23:50:04 +00003767 for (unsigned P = 0; P < Pairs; ++P) {
3768 Pair[P].Loops.resize(MaxLevels + 1);
3769 Pair[P].GroupLoops.resize(MaxLevels + 1);
3770 Pair[P].Group.resize(Pairs);
3771 removeMatchingExtensions(&Pair[P]);
3772 Pair[P].Classification =
3773 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3774 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3775 Pair[P].Loops);
3776 Pair[P].GroupLoops = Pair[P].Loops;
3777 Pair[P].Group.set(P);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003778 }
3779
3780 SmallBitVector Separable(Pairs);
3781 SmallBitVector Coupled(Pairs);
3782
3783 // partition subscripts into separable and minimally-coupled groups
3784 for (unsigned SI = 0; SI < Pairs; ++SI) {
3785 if (Pair[SI].Classification == Subscript::NonLinear) {
3786 // ignore these, but collect loops for later
3787 collectCommonLoops(Pair[SI].Src,
3788 LI->getLoopFor(Src->getParent()),
3789 Pair[SI].Loops);
3790 collectCommonLoops(Pair[SI].Dst,
3791 LI->getLoopFor(Dst->getParent()),
3792 Pair[SI].Loops);
3793 Result.Consistent = false;
3794 }
3795 else if (Pair[SI].Classification == Subscript::ZIV)
3796 Separable.set(SI);
3797 else {
3798 // SIV, RDIV, or MIV, so check for coupled group
3799 bool Done = true;
3800 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3801 SmallBitVector Intersection = Pair[SI].GroupLoops;
3802 Intersection &= Pair[SJ].GroupLoops;
3803 if (Intersection.any()) {
3804 // accumulate set of all the loops in group
3805 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3806 // accumulate set of all subscripts in group
3807 Pair[SJ].Group |= Pair[SI].Group;
3808 Done = false;
3809 }
3810 }
3811 if (Done) {
3812 if (Pair[SI].Group.count() == 1)
3813 Separable.set(SI);
3814 else
3815 Coupled.set(SI);
3816 }
3817 }
3818 }
3819
3820 Constraint NewConstraint;
3821 NewConstraint.setAny(SE);
3822
3823 // test separable subscripts
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003824 for (unsigned SI : Separable.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003825 switch (Pair[SI].Classification) {
3826 case Subscript::SIV: {
3827 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003828 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003829 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
3830 Result, NewConstraint, SplitIter);
3831 if (Level == SplitLevel) {
Craig Topper9f008862014-04-15 04:59:12 +00003832 assert(SplitIter != nullptr);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003833 return SplitIter;
3834 }
3835 break;
3836 }
3837 case Subscript::ZIV:
3838 case Subscript::RDIV:
3839 case Subscript::MIV:
3840 break;
3841 default:
3842 llvm_unreachable("subscript has unexpected classification");
3843 }
3844 }
3845
3846 if (Coupled.count()) {
3847 // test coupled subscript groups
3848 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3849 for (unsigned II = 0; II <= MaxLevels; ++II)
3850 Constraints[II].setAny(SE);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003851 for (unsigned SI : Coupled.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003852 SmallBitVector Group(Pair[SI].Group);
3853 SmallBitVector Sivs(Pairs);
3854 SmallBitVector Mivs(Pairs);
3855 SmallBitVector ConstrainedLevels(MaxLevels + 1);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003856 for (unsigned SJ : Group.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003857 if (Pair[SJ].Classification == Subscript::SIV)
3858 Sivs.set(SJ);
3859 else
3860 Mivs.set(SJ);
3861 }
3862 while (Sivs.any()) {
3863 bool Changed = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003864 for (unsigned SJ : Sivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003865 // SJ is an SIV subscript that's part of the current coupled group
3866 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003867 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003868 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
3869 Result, NewConstraint, SplitIter);
3870 if (Level == SplitLevel && SplitIter)
3871 return SplitIter;
3872 ConstrainedLevels.set(Level);
3873 if (intersectConstraints(&Constraints[Level], &NewConstraint))
3874 Changed = true;
3875 Sivs.reset(SJ);
3876 }
3877 if (Changed) {
3878 // propagate, possibly creating new SIVs and ZIVs
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003879 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003880 // SJ is an MIV subscript that's part of the current coupled group
3881 if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
3882 Pair[SJ].Loops, Constraints, Result.Consistent)) {
3883 Pair[SJ].Classification =
3884 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3885 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3886 Pair[SJ].Loops);
3887 switch (Pair[SJ].Classification) {
3888 case Subscript::ZIV:
3889 Mivs.reset(SJ);
3890 break;
3891 case Subscript::SIV:
3892 Sivs.set(SJ);
3893 Mivs.reset(SJ);
3894 break;
3895 case Subscript::RDIV:
3896 case Subscript::MIV:
3897 break;
3898 default:
3899 llvm_unreachable("bad subscript classification");
3900 }
3901 }
3902 }
3903 }
3904 }
3905 }
3906 }
3907 llvm_unreachable("somehow reached end of routine");
Craig Topper9f008862014-04-15 04:59:12 +00003908 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003909}