blob: 54a56803ff47090b7b6214789673f76920f62455 [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 Pop7ee14722013-11-13 22:37:58 +000027// The implementation depends on the GEP instruction to differentiate
28// subscripts. Since Clang linearizes some array subscripts, the dependence
29// analysis is using SCEV->delinearize to recover the representation of multiple
30// subscripts, and thus avoid the more expensive and less precise MIV tests. The
31// delinearization is controlled by the flag -da-delinearize.
Sebastian Pop59b61b92012-10-11 07:32:34 +000032//
33// We should pay some careful attention to the possibility of integer overflow
34// in the implementation of the various tests. This could happen with Add,
35// Subtract, or Multiply, with both APInt's and SCEV's.
36//
37// Some non-linear subscript pairs can be handled by the GCD test
38// (and perhaps other tests).
39// Should explore how often these things occur.
40//
41// Finally, it seems like certain test cases expose weaknesses in the SCEV
42// simplification, especially in the handling of sign and zero extensions.
43// It could be useful to spend time exploring these.
44//
45// Please note that this is work in progress and the interface is subject to
46// change.
47//
48//===----------------------------------------------------------------------===//
49// //
50// In memory of Ken Kennedy, 1945 - 2007 //
51// //
52//===----------------------------------------------------------------------===//
53
Sebastian Pop59b61b92012-10-11 07:32:34 +000054#include "llvm/Analysis/DependenceAnalysis.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000055#include "llvm/ADT/STLExtras.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000056#include "llvm/ADT/Statistic.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000057#include "llvm/Analysis/AliasAnalysis.h"
58#include "llvm/Analysis/LoopInfo.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000059#include "llvm/Analysis/ScalarEvolution.h"
60#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000061#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth83948572014-03-04 10:30:26 +000062#include "llvm/IR/InstIterator.h"
Mehdi Aminia28d91d2015-03-10 02:37:25 +000063#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000064#include "llvm/IR/Operator.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000065#include "llvm/Support/CommandLine.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000066#include "llvm/Support/Debug.h"
67#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000068#include "llvm/Support/raw_ostream.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000069
70using namespace llvm;
71
Chandler Carruthf1221bd2014-04-22 02:48:03 +000072#define DEBUG_TYPE "da"
73
Sebastian Pop59b61b92012-10-11 07:32:34 +000074//===----------------------------------------------------------------------===//
75// statistics
76
77STATISTIC(TotalArrayPairs, "Array pairs tested");
78STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
79STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
80STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
81STATISTIC(ZIVapplications, "ZIV applications");
82STATISTIC(ZIVindependence, "ZIV independence");
83STATISTIC(StrongSIVapplications, "Strong SIV applications");
84STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
85STATISTIC(StrongSIVindependence, "Strong SIV independence");
86STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
87STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
88STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
89STATISTIC(ExactSIVapplications, "Exact SIV applications");
90STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
91STATISTIC(ExactSIVindependence, "Exact SIV independence");
92STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
93STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
94STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
95STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
96STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
97STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
98STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
99STATISTIC(DeltaApplications, "Delta applications");
100STATISTIC(DeltaSuccesses, "Delta successes");
101STATISTIC(DeltaIndependence, "Delta independence");
102STATISTIC(DeltaPropagations, "Delta propagations");
103STATISTIC(GCDapplications, "GCD applications");
104STATISTIC(GCDsuccesses, "GCD successes");
105STATISTIC(GCDindependence, "GCD independence");
106STATISTIC(BanerjeeApplications, "Banerjee applications");
107STATISTIC(BanerjeeIndependence, "Banerjee independence");
108STATISTIC(BanerjeeSuccesses, "Banerjee successes");
109
Sebastian Popc62c6792013-11-12 22:47:20 +0000110static cl::opt<bool>
111Delinearize("da-delinearize", cl::init(false), cl::Hidden, cl::ZeroOrMore,
112 cl::desc("Try to delinearize array references."));
113
Sebastian Pop59b61b92012-10-11 07:32:34 +0000114//===----------------------------------------------------------------------===//
115// basics
116
117INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da",
118 "Dependence Analysis", true, true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000119INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000120INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Sebastian Pop59b61b92012-10-11 07:32:34 +0000122INITIALIZE_PASS_END(DependenceAnalysis, "da",
123 "Dependence Analysis", true, true)
124
125char DependenceAnalysis::ID = 0;
126
127
128FunctionPass *llvm::createDependenceAnalysisPass() {
129 return new DependenceAnalysis();
130}
131
132
133bool DependenceAnalysis::runOnFunction(Function &F) {
134 this->F = &F;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000135 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000136 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000137 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000138 return false;
139}
140
141
142void DependenceAnalysis::releaseMemory() {
143}
144
145
146void DependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
147 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000148 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000149 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000150 AU.addRequiredTransitive<LoopInfoWrapperPass>();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000151}
152
153
154// Used to test the dependence analyzer.
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000155// Looks through the function, noting loads and stores.
156// Calls depends() on every possible pair and prints out the result.
Sebastian Pop59b61b92012-10-11 07:32:34 +0000157// Ignores all other instructions.
158static
159void dumpExampleDependence(raw_ostream &OS, Function *F,
160 DependenceAnalysis *DA) {
161 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F);
162 SrcI != SrcE; ++SrcI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000163 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000164 for (inst_iterator DstI = SrcI, DstE = inst_end(F);
165 DstI != DstE; ++DstI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000166 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000167 OS << "da analyze - ";
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +0000168 if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000169 D->dump(OS);
170 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
171 if (D->isSplitable(Level)) {
172 OS << "da analyze - split level = " << Level;
Dylan Noblesmithd96ce662014-08-25 00:28:35 +0000173 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
Sebastian Pop59b61b92012-10-11 07:32:34 +0000174 OS << "!\n";
175 }
176 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000177 }
178 else
179 OS << "none!\n";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000180 }
181 }
182 }
183 }
184}
185
186
187void DependenceAnalysis::print(raw_ostream &OS, const Module*) const {
188 dumpExampleDependence(OS, F, const_cast<DependenceAnalysis *>(this));
189}
190
191//===----------------------------------------------------------------------===//
192// Dependence methods
193
194// Returns true if this is an input dependence.
195bool Dependence::isInput() const {
196 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
197}
198
199
200// Returns true if this is an output dependence.
201bool Dependence::isOutput() const {
202 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
203}
204
205
206// Returns true if this is an flow (aka true) dependence.
207bool Dependence::isFlow() const {
208 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
209}
210
211
212// Returns true if this is an anti dependence.
213bool Dependence::isAnti() const {
214 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
215}
216
217
218// Returns true if a particular level is scalar; that is,
219// if no subscript in the source or destination mention the induction
220// variable associated with the loop at this level.
221// Leave this out of line, so it will serve as a virtual method anchor
222bool Dependence::isScalar(unsigned level) const {
223 return false;
224}
225
226
227//===----------------------------------------------------------------------===//
228// FullDependence methods
229
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000230FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
Sebastian Pop59b61b92012-10-11 07:32:34 +0000231 bool PossiblyLoopIndependent,
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000232 unsigned CommonLevels)
233 : Dependence(Source, Destination), Levels(CommonLevels),
234 LoopIndependent(PossiblyLoopIndependent) {
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000235 Consistent = true;
David Blaikie47039dc2015-07-31 21:37:09 +0000236 if (CommonLevels)
237 DV = make_unique<DVEntry[]>(CommonLevels);
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000238}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000239
240// The rest are simple getters that hide the implementation.
241
242// getDirection - Returns the direction associated with a particular level.
243unsigned FullDependence::getDirection(unsigned Level) const {
244 assert(0 < Level && Level <= Levels && "Level out of range");
245 return DV[Level - 1].Direction;
246}
247
248
249// Returns the distance (or NULL) associated with a particular level.
250const SCEV *FullDependence::getDistance(unsigned Level) const {
251 assert(0 < Level && Level <= Levels && "Level out of range");
252 return DV[Level - 1].Distance;
253}
254
255
256// Returns true if a particular level is scalar; that is,
257// if no subscript in the source or destination mention the induction
258// variable associated with the loop at this level.
259bool FullDependence::isScalar(unsigned Level) const {
260 assert(0 < Level && Level <= Levels && "Level out of range");
261 return DV[Level - 1].Scalar;
262}
263
264
265// Returns true if peeling the first iteration from this loop
266// will break this dependence.
267bool FullDependence::isPeelFirst(unsigned Level) const {
268 assert(0 < Level && Level <= Levels && "Level out of range");
269 return DV[Level - 1].PeelFirst;
270}
271
272
273// Returns true if peeling the last iteration from this loop
274// will break this dependence.
275bool FullDependence::isPeelLast(unsigned Level) const {
276 assert(0 < Level && Level <= Levels && "Level out of range");
277 return DV[Level - 1].PeelLast;
278}
279
280
281// Returns true if splitting this loop will break the dependence.
282bool FullDependence::isSplitable(unsigned Level) const {
283 assert(0 < Level && Level <= Levels && "Level out of range");
284 return DV[Level - 1].Splitable;
285}
286
287
288//===----------------------------------------------------------------------===//
289// DependenceAnalysis::Constraint methods
290
291// If constraint is a point <X, Y>, returns X.
292// Otherwise assert.
293const SCEV *DependenceAnalysis::Constraint::getX() const {
294 assert(Kind == Point && "Kind should be Point");
295 return A;
296}
297
298
299// If constraint is a point <X, Y>, returns Y.
300// Otherwise assert.
301const SCEV *DependenceAnalysis::Constraint::getY() const {
302 assert(Kind == Point && "Kind should be Point");
303 return B;
304}
305
306
307// If constraint is a line AX + BY = C, returns A.
308// Otherwise assert.
309const SCEV *DependenceAnalysis::Constraint::getA() const {
310 assert((Kind == Line || Kind == Distance) &&
311 "Kind should be Line (or Distance)");
312 return A;
313}
314
315
316// If constraint is a line AX + BY = C, returns B.
317// Otherwise assert.
318const SCEV *DependenceAnalysis::Constraint::getB() const {
319 assert((Kind == Line || Kind == Distance) &&
320 "Kind should be Line (or Distance)");
321 return B;
322}
323
324
325// If constraint is a line AX + BY = C, returns C.
326// Otherwise assert.
327const SCEV *DependenceAnalysis::Constraint::getC() const {
328 assert((Kind == Line || Kind == Distance) &&
329 "Kind should be Line (or Distance)");
330 return C;
331}
332
333
334// If constraint is a distance, returns D.
335// Otherwise assert.
336const SCEV *DependenceAnalysis::Constraint::getD() const {
337 assert(Kind == Distance && "Kind should be Distance");
338 return SE->getNegativeSCEV(C);
339}
340
341
342// Returns the loop associated with this constraint.
343const Loop *DependenceAnalysis::Constraint::getAssociatedLoop() const {
344 assert((Kind == Distance || Kind == Line || Kind == Point) &&
345 "Kind should be Distance, Line, or Point");
346 return AssociatedLoop;
347}
348
349
350void DependenceAnalysis::Constraint::setPoint(const SCEV *X,
351 const SCEV *Y,
352 const Loop *CurLoop) {
353 Kind = Point;
354 A = X;
355 B = Y;
356 AssociatedLoop = CurLoop;
357}
358
359
360void DependenceAnalysis::Constraint::setLine(const SCEV *AA,
361 const SCEV *BB,
362 const SCEV *CC,
363 const Loop *CurLoop) {
364 Kind = Line;
365 A = AA;
366 B = BB;
367 C = CC;
368 AssociatedLoop = CurLoop;
369}
370
371
372void DependenceAnalysis::Constraint::setDistance(const SCEV *D,
373 const Loop *CurLoop) {
374 Kind = Distance;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000375 A = SE->getOne(D->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +0000376 B = SE->getNegativeSCEV(A);
377 C = SE->getNegativeSCEV(D);
378 AssociatedLoop = CurLoop;
379}
380
381
382void DependenceAnalysis::Constraint::setEmpty() {
383 Kind = Empty;
384}
385
386
387void DependenceAnalysis::Constraint::setAny(ScalarEvolution *NewSE) {
388 SE = NewSE;
389 Kind = Any;
390}
391
392
393// For debugging purposes. Dumps the constraint out to OS.
394void DependenceAnalysis::Constraint::dump(raw_ostream &OS) const {
395 if (isEmpty())
396 OS << " Empty\n";
397 else if (isAny())
398 OS << " Any\n";
399 else if (isPoint())
400 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
401 else if (isDistance())
402 OS << " Distance is " << *getD() <<
403 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
404 else if (isLine())
405 OS << " Line is " << *getA() << "*X + " <<
406 *getB() << "*Y = " << *getC() << "\n";
407 else
408 llvm_unreachable("unknown constraint type in Constraint::dump");
409}
410
411
412// Updates X with the intersection
413// of the Constraints X and Y. Returns true if X has changed.
414// Corresponds to Figure 4 from the paper
415//
416// Practical Dependence Testing
417// Goff, Kennedy, Tseng
418// PLDI 1991
419bool DependenceAnalysis::intersectConstraints(Constraint *X,
420 const Constraint *Y) {
421 ++DeltaApplications;
422 DEBUG(dbgs() << "\tintersect constraints\n");
423 DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
424 DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
425 assert(!Y->isPoint() && "Y must not be a Point");
426 if (X->isAny()) {
427 if (Y->isAny())
428 return false;
429 *X = *Y;
430 return true;
431 }
432 if (X->isEmpty())
433 return false;
434 if (Y->isEmpty()) {
435 X->setEmpty();
436 return true;
437 }
438
439 if (X->isDistance() && Y->isDistance()) {
440 DEBUG(dbgs() << "\t intersect 2 distances\n");
441 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
442 return false;
443 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
444 X->setEmpty();
445 ++DeltaSuccesses;
446 return true;
447 }
448 // Hmmm, interesting situation.
449 // I guess if either is constant, keep it and ignore the other.
450 if (isa<SCEVConstant>(Y->getD())) {
451 *X = *Y;
452 return true;
453 }
454 return false;
455 }
456
457 // At this point, the pseudo-code in Figure 4 of the paper
458 // checks if (X->isPoint() && Y->isPoint()).
459 // This case can't occur in our implementation,
460 // since a Point can only arise as the result of intersecting
461 // two Line constraints, and the right-hand value, Y, is never
462 // the result of an intersection.
463 assert(!(X->isPoint() && Y->isPoint()) &&
464 "We shouldn't ever see X->isPoint() && Y->isPoint()");
465
466 if (X->isLine() && Y->isLine()) {
467 DEBUG(dbgs() << "\t intersect 2 lines\n");
468 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
469 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
470 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
471 // slopes are equal, so lines are parallel
472 DEBUG(dbgs() << "\t\tsame slope\n");
473 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
474 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
475 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
476 return false;
477 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
478 X->setEmpty();
479 ++DeltaSuccesses;
480 return true;
481 }
482 return false;
483 }
484 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
485 // slopes differ, so lines intersect
486 DEBUG(dbgs() << "\t\tdifferent slopes\n");
487 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
488 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
489 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
490 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
491 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
492 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
493 const SCEVConstant *C1A2_C2A1 =
494 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
495 const SCEVConstant *C1B2_C2B1 =
496 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
497 const SCEVConstant *A1B2_A2B1 =
498 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
499 const SCEVConstant *A2B1_A1B2 =
500 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
501 if (!C1B2_C2B1 || !C1A2_C2A1 ||
502 !A1B2_A2B1 || !A2B1_A1B2)
503 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000504 APInt Xtop = C1B2_C2B1->getAPInt();
505 APInt Xbot = A1B2_A2B1->getAPInt();
506 APInt Ytop = C1A2_C2A1->getAPInt();
507 APInt Ybot = A2B1_A1B2->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000508 DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
509 DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
510 DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
511 DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
512 APInt Xq = Xtop; // these need to be initialized, even
513 APInt Xr = Xtop; // though they're just going to be overwritten
514 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
515 APInt Yq = Ytop;
Jakub Staszak340c7802013-08-06 16:40:40 +0000516 APInt Yr = Ytop;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000517 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
518 if (Xr != 0 || Yr != 0) {
519 X->setEmpty();
520 ++DeltaSuccesses;
521 return true;
522 }
523 DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
524 if (Xq.slt(0) || Yq.slt(0)) {
525 X->setEmpty();
526 ++DeltaSuccesses;
527 return true;
528 }
529 if (const SCEVConstant *CUB =
530 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000531 APInt UpperBound = CUB->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000532 DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
533 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
534 X->setEmpty();
535 ++DeltaSuccesses;
536 return true;
537 }
538 }
539 X->setPoint(SE->getConstant(Xq),
540 SE->getConstant(Yq),
541 X->getAssociatedLoop());
542 ++DeltaSuccesses;
543 return true;
544 }
545 return false;
546 }
547
548 // if (X->isLine() && Y->isPoint()) This case can't occur.
549 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
550
551 if (X->isPoint() && Y->isLine()) {
552 DEBUG(dbgs() << "\t intersect Point and Line\n");
553 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
554 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
555 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
556 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
557 return false;
558 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
559 X->setEmpty();
560 ++DeltaSuccesses;
561 return true;
562 }
563 return false;
564 }
565
566 llvm_unreachable("shouldn't reach the end of Constraint intersection");
567 return false;
568}
569
570
571//===----------------------------------------------------------------------===//
572// DependenceAnalysis methods
573
574// For debugging purposes. Dumps a dependence to OS.
575void Dependence::dump(raw_ostream &OS) const {
576 bool Splitable = false;
577 if (isConfused())
578 OS << "confused";
579 else {
580 if (isConsistent())
581 OS << "consistent ";
582 if (isFlow())
583 OS << "flow";
584 else if (isOutput())
585 OS << "output";
586 else if (isAnti())
587 OS << "anti";
588 else if (isInput())
589 OS << "input";
590 unsigned Levels = getLevels();
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000591 OS << " [";
592 for (unsigned II = 1; II <= Levels; ++II) {
593 if (isSplitable(II))
594 Splitable = true;
595 if (isPeelFirst(II))
596 OS << 'p';
597 const SCEV *Distance = getDistance(II);
598 if (Distance)
599 OS << *Distance;
600 else if (isScalar(II))
601 OS << "S";
602 else {
603 unsigned Direction = getDirection(II);
604 if (Direction == DVEntry::ALL)
605 OS << "*";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000606 else {
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000607 if (Direction & DVEntry::LT)
608 OS << "<";
609 if (Direction & DVEntry::EQ)
610 OS << "=";
611 if (Direction & DVEntry::GT)
612 OS << ">";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000613 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000614 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000615 if (isPeelLast(II))
616 OS << 'p';
617 if (II < Levels)
618 OS << " ";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000619 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000620 if (isLoopIndependent())
621 OS << "|<";
622 OS << "]";
623 if (Splitable)
624 OS << " splitable";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000625 }
626 OS << "!\n";
627}
628
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000629static AliasResult underlyingObjectsAlias(AliasAnalysis *AA,
630 const DataLayout &DL, const Value *A,
631 const Value *B) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000632 const Value *AObj = GetUnderlyingObject(A, DL);
633 const Value *BObj = GetUnderlyingObject(B, DL);
Chandler Carruth50fee932015-08-06 02:05:46 +0000634 return AA->alias(AObj, DL.getTypeStoreSize(AObj->getType()),
635 BObj, DL.getTypeStoreSize(BObj->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +0000636}
637
638
639// Returns true if the load or store can be analyzed. Atomic and volatile
640// operations have properties which this analysis does not understand.
641static
642bool isLoadOrStore(const Instruction *I) {
643 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
644 return LI->isUnordered();
645 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
646 return SI->isUnordered();
647 return false;
648}
649
650
651static
Sebastian Pop87ce43c2012-11-20 22:28:04 +0000652Value *getPointerOperand(Instruction *I) {
653 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Sebastian Pop59b61b92012-10-11 07:32:34 +0000654 return LI->getPointerOperand();
Sebastian Pop87ce43c2012-11-20 22:28:04 +0000655 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Sebastian Pop59b61b92012-10-11 07:32:34 +0000656 return SI->getPointerOperand();
657 llvm_unreachable("Value is not load or store instruction");
Craig Topper9f008862014-04-15 04:59:12 +0000658 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000659}
660
661
662// Examines the loop nesting of the Src and Dst
663// instructions and establishes their shared loops. Sets the variables
664// CommonLevels, SrcLevels, and MaxLevels.
665// The source and destination instructions needn't be contained in the same
666// loop. The routine establishNestingLevels finds the level of most deeply
667// nested loop that contains them both, CommonLevels. An instruction that's
668// not contained in a loop is at level = 0. MaxLevels is equal to the level
669// of the source plus the level of the destination, minus CommonLevels.
670// This lets us allocate vectors MaxLevels in length, with room for every
671// distinct loop referenced in both the source and destination subscripts.
672// The variable SrcLevels is the nesting depth of the source instruction.
673// It's used to help calculate distinct loops referenced by the destination.
674// Here's the map from loops to levels:
675// 0 - unused
676// 1 - outermost common loop
677// ... - other common loops
678// CommonLevels - innermost common loop
679// ... - loops containing Src but not Dst
680// SrcLevels - innermost loop containing Src but not Dst
681// ... - loops containing Dst but not Src
682// MaxLevels - innermost loops containing Dst but not Src
683// Consider the follow code fragment:
684// for (a = ...) {
685// for (b = ...) {
686// for (c = ...) {
687// for (d = ...) {
688// A[] = ...;
689// }
690// }
691// for (e = ...) {
692// for (f = ...) {
693// for (g = ...) {
694// ... = A[];
695// }
696// }
697// }
698// }
699// }
700// If we're looking at the possibility of a dependence between the store
701// to A (the Src) and the load from A (the Dst), we'll note that they
702// have 2 loops in common, so CommonLevels will equal 2 and the direction
703// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
704// A map from loop names to loop numbers would look like
705// a - 1
706// b - 2 = CommonLevels
707// c - 3
708// d - 4 = SrcLevels
709// e - 5
710// f - 6
711// g - 7 = MaxLevels
712void DependenceAnalysis::establishNestingLevels(const Instruction *Src,
713 const Instruction *Dst) {
714 const BasicBlock *SrcBlock = Src->getParent();
715 const BasicBlock *DstBlock = Dst->getParent();
716 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
717 unsigned DstLevel = LI->getLoopDepth(DstBlock);
718 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
719 const Loop *DstLoop = LI->getLoopFor(DstBlock);
720 SrcLevels = SrcLevel;
721 MaxLevels = SrcLevel + DstLevel;
722 while (SrcLevel > DstLevel) {
723 SrcLoop = SrcLoop->getParentLoop();
724 SrcLevel--;
725 }
726 while (DstLevel > SrcLevel) {
727 DstLoop = DstLoop->getParentLoop();
728 DstLevel--;
729 }
730 while (SrcLoop != DstLoop) {
731 SrcLoop = SrcLoop->getParentLoop();
732 DstLoop = DstLoop->getParentLoop();
733 SrcLevel--;
734 }
735 CommonLevels = SrcLevel;
736 MaxLevels -= CommonLevels;
737}
738
739
740// Given one of the loops containing the source, return
741// its level index in our numbering scheme.
742unsigned DependenceAnalysis::mapSrcLoop(const Loop *SrcLoop) const {
743 return SrcLoop->getLoopDepth();
744}
745
746
747// Given one of the loops containing the destination,
748// return its level index in our numbering scheme.
749unsigned DependenceAnalysis::mapDstLoop(const Loop *DstLoop) const {
750 unsigned D = DstLoop->getLoopDepth();
751 if (D > CommonLevels)
752 return D - CommonLevels + SrcLevels;
753 else
754 return D;
755}
756
757
758// Returns true if Expression is loop invariant in LoopNest.
759bool DependenceAnalysis::isLoopInvariant(const SCEV *Expression,
760 const Loop *LoopNest) const {
761 if (!LoopNest)
762 return true;
763 return SE->isLoopInvariant(Expression, LoopNest) &&
764 isLoopInvariant(Expression, LoopNest->getParentLoop());
765}
766
767
768
769// Finds the set of loops from the LoopNest that
770// have a level <= CommonLevels and are referred to by the SCEV Expression.
771void DependenceAnalysis::collectCommonLoops(const SCEV *Expression,
772 const Loop *LoopNest,
773 SmallBitVector &Loops) const {
774 while (LoopNest) {
775 unsigned Level = LoopNest->getLoopDepth();
776 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
777 Loops.set(Level);
778 LoopNest = LoopNest->getParentLoop();
779 }
780}
781
Jingyue Wua84feb12015-05-29 16:58:08 +0000782void DependenceAnalysis::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
783
784 unsigned widestWidthSeen = 0;
785 Type *widestType;
786
787 // Go through each pair and find the widest bit to which we need
788 // to extend all of them.
789 for (unsigned i = 0; i < Pairs.size(); i++) {
790 const SCEV *Src = Pairs[i]->Src;
791 const SCEV *Dst = Pairs[i]->Dst;
792 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
793 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
794 if (SrcTy == nullptr || DstTy == nullptr) {
795 assert(SrcTy == DstTy && "This function only unify integer types and "
796 "expect Src and Dst share the same type "
797 "otherwise.");
798 continue;
799 }
800 if (SrcTy->getBitWidth() > widestWidthSeen) {
801 widestWidthSeen = SrcTy->getBitWidth();
802 widestType = SrcTy;
803 }
804 if (DstTy->getBitWidth() > widestWidthSeen) {
805 widestWidthSeen = DstTy->getBitWidth();
806 widestType = DstTy;
807 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000808 }
Jingyue Wua84feb12015-05-29 16:58:08 +0000809
810
811 assert(widestWidthSeen > 0);
812
813 // Now extend each pair to the widest seen.
814 for (unsigned i = 0; i < Pairs.size(); i++) {
815 const SCEV *Src = Pairs[i]->Src;
816 const SCEV *Dst = Pairs[i]->Dst;
817 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
818 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
819 if (SrcTy == nullptr || DstTy == nullptr) {
820 assert(SrcTy == DstTy && "This function only unify integer types and "
821 "expect Src and Dst share the same type "
822 "otherwise.");
823 continue;
824 }
825 if (SrcTy->getBitWidth() < widestWidthSeen)
826 // Sign-extend Src to widestType
827 Pairs[i]->Src = SE->getSignExtendExpr(Src, widestType);
828 if (DstTy->getBitWidth() < widestWidthSeen) {
829 // Sign-extend Dst to widestType
830 Pairs[i]->Dst = SE->getSignExtendExpr(Dst, widestType);
831 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000832 }
833}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000834
835// removeMatchingExtensions - Examines a subscript pair.
836// If the source and destination are identically sign (or zero)
837// extended, it strips off the extension in an effect to simplify
838// the actual analysis.
839void DependenceAnalysis::removeMatchingExtensions(Subscript *Pair) {
840 const SCEV *Src = Pair->Src;
841 const SCEV *Dst = Pair->Dst;
842 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
843 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
844 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src);
845 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst);
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000846 const SCEV *SrcCastOp = SrcCast->getOperand();
847 const SCEV *DstCastOp = DstCast->getOperand();
848 if (SrcCastOp->getType() == DstCastOp->getType()) {
849 Pair->Src = SrcCastOp;
850 Pair->Dst = DstCastOp;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000851 }
852 }
853}
854
855
856// Examine the scev and return true iff it's linear.
857// Collect any loops mentioned in the set of "Loops".
858bool DependenceAnalysis::checkSrcSubscript(const SCEV *Src,
859 const Loop *LoopNest,
860 SmallBitVector &Loops) {
861 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src);
862 if (!AddRec)
863 return isLoopInvariant(Src, LoopNest);
864 const SCEV *Start = AddRec->getStart();
865 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000866 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
867 if (!isa<SCEVCouldNotCompute>(UB)) {
868 if (SE->getTypeSizeInBits(Start->getType()) <
869 SE->getTypeSizeInBits(UB->getType())) {
870 if (!AddRec->getNoWrapFlags())
871 return false;
872 }
873 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000874 if (!isLoopInvariant(Step, LoopNest))
875 return false;
876 Loops.set(mapSrcLoop(AddRec->getLoop()));
877 return checkSrcSubscript(Start, LoopNest, Loops);
878}
879
880
881
882// Examine the scev and return true iff it's linear.
883// Collect any loops mentioned in the set of "Loops".
884bool DependenceAnalysis::checkDstSubscript(const SCEV *Dst,
885 const Loop *LoopNest,
886 SmallBitVector &Loops) {
887 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst);
888 if (!AddRec)
889 return isLoopInvariant(Dst, LoopNest);
890 const SCEV *Start = AddRec->getStart();
891 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000892 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
893 if (!isa<SCEVCouldNotCompute>(UB)) {
894 if (SE->getTypeSizeInBits(Start->getType()) <
895 SE->getTypeSizeInBits(UB->getType())) {
896 if (!AddRec->getNoWrapFlags())
897 return false;
898 }
899 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000900 if (!isLoopInvariant(Step, LoopNest))
901 return false;
902 Loops.set(mapDstLoop(AddRec->getLoop()));
903 return checkDstSubscript(Start, LoopNest, Loops);
904}
905
906
907// Examines the subscript pair (the Src and Dst SCEVs)
908// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
909// Collects the associated loops in a set.
910DependenceAnalysis::Subscript::ClassificationKind
911DependenceAnalysis::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
912 const SCEV *Dst, const Loop *DstLoopNest,
913 SmallBitVector &Loops) {
914 SmallBitVector SrcLoops(MaxLevels + 1);
915 SmallBitVector DstLoops(MaxLevels + 1);
916 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
917 return Subscript::NonLinear;
918 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
919 return Subscript::NonLinear;
920 Loops = SrcLoops;
921 Loops |= DstLoops;
922 unsigned N = Loops.count();
923 if (N == 0)
924 return Subscript::ZIV;
925 if (N == 1)
926 return Subscript::SIV;
927 if (N == 2 && (SrcLoops.count() == 0 ||
928 DstLoops.count() == 0 ||
929 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
930 return Subscript::RDIV;
931 return Subscript::MIV;
932}
933
934
935// A wrapper around SCEV::isKnownPredicate.
936// Looks for cases where we're interested in comparing for equality.
937// If both X and Y have been identically sign or zero extended,
938// it strips off the (confusing) extensions before invoking
939// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
940// will be similarly updated.
941//
942// If SCEV::isKnownPredicate can't prove the predicate,
943// we try simple subtraction, which seems to help in some cases
944// involving symbolics.
945bool DependenceAnalysis::isKnownPredicate(ICmpInst::Predicate Pred,
946 const SCEV *X,
947 const SCEV *Y) const {
948 if (Pred == CmpInst::ICMP_EQ ||
949 Pred == CmpInst::ICMP_NE) {
950 if ((isa<SCEVSignExtendExpr>(X) &&
951 isa<SCEVSignExtendExpr>(Y)) ||
952 (isa<SCEVZeroExtendExpr>(X) &&
953 isa<SCEVZeroExtendExpr>(Y))) {
954 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X);
955 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y);
956 const SCEV *Xop = CX->getOperand();
957 const SCEV *Yop = CY->getOperand();
958 if (Xop->getType() == Yop->getType()) {
959 X = Xop;
960 Y = Yop;
961 }
962 }
963 }
964 if (SE->isKnownPredicate(Pred, X, Y))
965 return true;
966 // If SE->isKnownPredicate can't prove the condition,
967 // we try the brute-force approach of subtracting
968 // and testing the difference.
969 // By testing with SE->isKnownPredicate first, we avoid
970 // the possibility of overflow when the arguments are constants.
971 const SCEV *Delta = SE->getMinusSCEV(X, Y);
972 switch (Pred) {
973 case CmpInst::ICMP_EQ:
974 return Delta->isZero();
975 case CmpInst::ICMP_NE:
976 return SE->isKnownNonZero(Delta);
977 case CmpInst::ICMP_SGE:
978 return SE->isKnownNonNegative(Delta);
979 case CmpInst::ICMP_SLE:
980 return SE->isKnownNonPositive(Delta);
981 case CmpInst::ICMP_SGT:
982 return SE->isKnownPositive(Delta);
983 case CmpInst::ICMP_SLT:
984 return SE->isKnownNegative(Delta);
985 default:
986 llvm_unreachable("unexpected predicate in isKnownPredicate");
987 }
988}
989
990
991// All subscripts are all the same type.
992// Loop bound may be smaller (e.g., a char).
993// Should zero extend loop bound, since it's always >= 0.
James Molloyc0661ae2015-05-15 12:17:22 +0000994// This routine collects upper bound and extends or truncates if needed.
995// Truncating is safe when subscripts are known not to wrap. Cases without
996// nowrap flags should have been rejected earlier.
Sebastian Pop59b61b92012-10-11 07:32:34 +0000997// Return null if no bound available.
998const SCEV *DependenceAnalysis::collectUpperBound(const Loop *L,
999 Type *T) const {
1000 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1001 const SCEV *UB = SE->getBackedgeTakenCount(L);
James Molloyc0661ae2015-05-15 12:17:22 +00001002 return SE->getTruncateOrZeroExtend(UB, T);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001003 }
Craig Topper9f008862014-04-15 04:59:12 +00001004 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001005}
1006
1007
1008// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1009// If the cast fails, returns NULL.
1010const SCEVConstant *DependenceAnalysis::collectConstantUpperBound(const Loop *L,
1011 Type *T
1012 ) const {
1013 if (const SCEV *UB = collectUpperBound(L, T))
1014 return dyn_cast<SCEVConstant>(UB);
Craig Topper9f008862014-04-15 04:59:12 +00001015 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001016}
1017
1018
1019// testZIV -
1020// When we have a pair of subscripts of the form [c1] and [c2],
1021// where c1 and c2 are both loop invariant, we attack it using
1022// the ZIV test. Basically, we test by comparing the two values,
1023// but there are actually three possible results:
1024// 1) the values are equal, so there's a dependence
1025// 2) the values are different, so there's no dependence
1026// 3) the values might be equal, so we have to assume a dependence.
1027//
1028// Return true if dependence disproved.
1029bool DependenceAnalysis::testZIV(const SCEV *Src,
1030 const SCEV *Dst,
1031 FullDependence &Result) const {
1032 DEBUG(dbgs() << " src = " << *Src << "\n");
1033 DEBUG(dbgs() << " dst = " << *Dst << "\n");
1034 ++ZIVapplications;
1035 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1036 DEBUG(dbgs() << " provably dependent\n");
1037 return false; // provably dependent
1038 }
1039 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1040 DEBUG(dbgs() << " provably independent\n");
1041 ++ZIVindependence;
1042 return true; // provably independent
1043 }
1044 DEBUG(dbgs() << " possibly dependent\n");
1045 Result.Consistent = false;
1046 return false; // possibly dependent
1047}
1048
1049
1050// strongSIVtest -
1051// From the paper, Practical Dependence Testing, Section 4.2.1
1052//
1053// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1054// where i is an induction variable, c1 and c2 are loop invariant,
1055// and a is a constant, we can solve it exactly using the Strong SIV test.
1056//
1057// Can prove independence. Failing that, can compute distance (and direction).
1058// In the presence of symbolic terms, we can sometimes make progress.
1059//
1060// If there's a dependence,
1061//
1062// c1 + a*i = c2 + a*i'
1063//
1064// The dependence distance is
1065//
1066// d = i' - i = (c1 - c2)/a
1067//
1068// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1069// loop's upper bound. If a dependence exists, the dependence direction is
1070// defined as
1071//
1072// { < if d > 0
1073// direction = { = if d = 0
1074// { > if d < 0
1075//
1076// Return true if dependence disproved.
1077bool DependenceAnalysis::strongSIVtest(const SCEV *Coeff,
1078 const SCEV *SrcConst,
1079 const SCEV *DstConst,
1080 const Loop *CurLoop,
1081 unsigned Level,
1082 FullDependence &Result,
1083 Constraint &NewConstraint) const {
1084 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.
1216bool DependenceAnalysis::weakCrossingSIVtest(const SCEV *Coeff,
1217 const SCEV *SrcConst,
1218 const SCEV *DstConst,
1219 const Loop *CurLoop,
1220 unsigned Level,
1221 FullDependence &Result,
1222 Constraint &NewConstraint,
1223 const SCEV *&SplitIter) const {
1224 DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1225 DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1226 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1227 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1228 ++WeakCrossingSIVapplications;
1229 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1230 Level--;
1231 Result.Consistent = false;
1232 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1233 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1234 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1235 if (Delta->isZero()) {
Sebastian Pope96232612012-10-12 02:04:32 +00001236 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1237 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001238 ++WeakCrossingSIVsuccesses;
1239 if (!Result.DV[Level].Direction) {
1240 ++WeakCrossingSIVindependence;
1241 return true;
1242 }
1243 Result.DV[Level].Distance = Delta; // = 0
1244 return false;
1245 }
1246 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1247 if (!ConstCoeff)
1248 return false;
1249
1250 Result.DV[Level].Splitable = true;
1251 if (SE->isKnownNegative(ConstCoeff)) {
1252 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1253 assert(ConstCoeff &&
1254 "dynamic cast of negative of ConstCoeff should yield constant");
1255 Delta = SE->getNegativeSCEV(Delta);
1256 }
1257 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1258
1259 // compute SplitIter for use by DependenceAnalysis::getSplitIteration()
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001260 SplitIter = SE->getUDivExpr(
1261 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1262 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
Sebastian Pop59b61b92012-10-11 07:32:34 +00001263 DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
1264
1265 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1266 if (!ConstDelta)
1267 return false;
1268
1269 // We're certain that ConstCoeff > 0; therefore,
1270 // if Delta < 0, then no dependence.
1271 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1272 DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1273 if (SE->isKnownNegative(Delta)) {
1274 // No dependence, Delta < 0
1275 ++WeakCrossingSIVindependence;
1276 ++WeakCrossingSIVsuccesses;
1277 return true;
1278 }
1279
1280 // We're certain that Delta > 0 and ConstCoeff > 0.
1281 // Check Delta/(2*ConstCoeff) against upper loop bound
1282 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1283 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1284 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1285 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
1286 ConstantTwo);
1287 DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1288 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1289 // Delta too big, no dependence
1290 ++WeakCrossingSIVindependence;
1291 ++WeakCrossingSIVsuccesses;
1292 return true;
1293 }
1294 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1295 // i = i' = UB
Sebastian Pope96232612012-10-12 02:04:32 +00001296 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1297 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001298 ++WeakCrossingSIVsuccesses;
1299 if (!Result.DV[Level].Direction) {
1300 ++WeakCrossingSIVindependence;
1301 return true;
1302 }
1303 Result.DV[Level].Splitable = false;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001304 Result.DV[Level].Distance = SE->getZero(Delta->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00001305 return false;
1306 }
1307 }
1308
1309 // check that Coeff divides Delta
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001310 APInt APDelta = ConstDelta->getAPInt();
1311 APInt APCoeff = ConstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001312 APInt Distance = APDelta; // these need to be initialzed
1313 APInt Remainder = APDelta;
1314 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1315 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1316 if (Remainder != 0) {
1317 // Coeff doesn't divide Delta, no dependence
1318 ++WeakCrossingSIVindependence;
1319 ++WeakCrossingSIVsuccesses;
1320 return true;
1321 }
1322 DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1323
1324 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1325 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1326 Remainder = Distance.srem(Two);
1327 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1328 if (Remainder != 0) {
1329 // Equal direction isn't possible
Sebastian Pope96232612012-10-12 02:04:32 +00001330 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001331 ++WeakCrossingSIVsuccesses;
1332 }
1333 return false;
1334}
1335
1336
1337// Kirch's algorithm, from
1338//
1339// Optimizing Supercompilers for Supercomputers
1340// Michael Wolfe
1341// MIT Press, 1989
1342//
1343// Program 2.1, page 29.
1344// Computes the GCD of AM and BM.
Mingjie Xing9deac1b2014-01-07 01:54:16 +00001345// Also finds a solution to the equation ax - by = gcd(a, b).
1346// Returns true if dependence disproved; i.e., gcd does not divide Delta.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001347static
1348bool findGCD(unsigned Bits, APInt AM, APInt BM, APInt Delta,
1349 APInt &G, APInt &X, APInt &Y) {
1350 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1351 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1352 APInt G0 = AM.abs();
1353 APInt G1 = BM.abs();
1354 APInt Q = G0; // these need to be initialized
1355 APInt R = G0;
1356 APInt::sdivrem(G0, G1, Q, R);
1357 while (R != 0) {
1358 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1359 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1360 G0 = G1; G1 = R;
1361 APInt::sdivrem(G0, G1, Q, R);
1362 }
1363 G = G1;
1364 DEBUG(dbgs() << "\t GCD = " << G << "\n");
1365 X = AM.slt(0) ? -A1 : A1;
1366 Y = BM.slt(0) ? B1 : -B1;
1367
1368 // make sure gcd divides Delta
1369 R = Delta.srem(G);
1370 if (R != 0)
1371 return true; // gcd doesn't divide Delta, no dependence
1372 Q = Delta.sdiv(G);
1373 X *= Q;
1374 Y *= Q;
1375 return false;
1376}
1377
1378
1379static
1380APInt floorOfQuotient(APInt A, APInt B) {
1381 APInt Q = A; // these need to be initialized
1382 APInt R = A;
1383 APInt::sdivrem(A, B, Q, R);
1384 if (R == 0)
1385 return Q;
1386 if ((A.sgt(0) && B.sgt(0)) ||
1387 (A.slt(0) && B.slt(0)))
1388 return Q;
1389 else
1390 return Q - 1;
1391}
1392
1393
1394static
1395APInt ceilingOfQuotient(APInt A, APInt B) {
1396 APInt Q = A; // these need to be initialized
1397 APInt R = A;
1398 APInt::sdivrem(A, B, Q, R);
1399 if (R == 0)
1400 return Q;
1401 if ((A.sgt(0) && B.sgt(0)) ||
1402 (A.slt(0) && B.slt(0)))
1403 return Q + 1;
1404 else
1405 return Q;
1406}
1407
1408
1409static
1410APInt maxAPInt(APInt A, APInt B) {
1411 return A.sgt(B) ? A : B;
1412}
1413
1414
1415static
1416APInt minAPInt(APInt A, APInt B) {
1417 return A.slt(B) ? A : B;
1418}
1419
1420
1421// exactSIVtest -
1422// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1423// where i is an induction variable, c1 and c2 are loop invariant, and a1
1424// and a2 are constant, we can solve it exactly using an algorithm developed
1425// by Banerjee and Wolfe. See Section 2.5.3 in
1426//
1427// Optimizing Supercompilers for Supercomputers
1428// Michael Wolfe
1429// MIT Press, 1989
1430//
1431// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1432// so use them if possible. They're also a bit better with symbolics and,
1433// in the case of the strong SIV test, can compute Distances.
1434//
1435// Return true if dependence disproved.
1436bool DependenceAnalysis::exactSIVtest(const SCEV *SrcCoeff,
1437 const SCEV *DstCoeff,
1438 const SCEV *SrcConst,
1439 const SCEV *DstConst,
1440 const Loop *CurLoop,
1441 unsigned Level,
1442 FullDependence &Result,
1443 Constraint &NewConstraint) const {
1444 DEBUG(dbgs() << "\tExact SIV test\n");
1445 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1446 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1447 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1448 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1449 ++ExactSIVapplications;
1450 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1451 Level--;
1452 Result.Consistent = false;
1453 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1454 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1455 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff),
1456 Delta, CurLoop);
1457 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1458 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1459 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1460 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1461 return false;
1462
1463 // find gcd
1464 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001465 APInt AM = ConstSrcCoeff->getAPInt();
1466 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001467 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001468 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001469 // gcd doesn't divide Delta, no dependence
1470 ++ExactSIVindependence;
1471 ++ExactSIVsuccesses;
1472 return true;
1473 }
1474
1475 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1476
1477 // since SCEV construction normalizes, LM = 0
1478 APInt UM(Bits, 1, true);
1479 bool UMvalid = false;
1480 // UM is perhaps unavailable, let's check
1481 if (const SCEVConstant *CUB =
1482 collectConstantUpperBound(CurLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001483 UM = CUB->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001484 DEBUG(dbgs() << "\t UM = " << UM << "\n");
1485 UMvalid = true;
1486 }
1487
1488 APInt TU(APInt::getSignedMaxValue(Bits));
1489 APInt TL(APInt::getSignedMinValue(Bits));
1490
1491 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1492 APInt TMUL = BM.sdiv(G);
1493 if (TMUL.sgt(0)) {
1494 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1495 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1496 if (UMvalid) {
1497 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL));
1498 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1499 }
1500 }
1501 else {
1502 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1503 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1504 if (UMvalid) {
1505 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL));
1506 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1507 }
1508 }
1509
1510 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1511 TMUL = AM.sdiv(G);
1512 if (TMUL.sgt(0)) {
1513 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1514 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1515 if (UMvalid) {
1516 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL));
1517 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1518 }
1519 }
1520 else {
1521 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1522 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1523 if (UMvalid) {
1524 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL));
1525 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1526 }
1527 }
1528 if (TL.sgt(TU)) {
1529 ++ExactSIVindependence;
1530 ++ExactSIVsuccesses;
1531 return true;
1532 }
1533
1534 // explore directions
1535 unsigned NewDirection = Dependence::DVEntry::NONE;
1536
1537 // less than
1538 APInt SaveTU(TU); // save these
1539 APInt SaveTL(TL);
1540 DEBUG(dbgs() << "\t exploring LT direction\n");
1541 TMUL = AM - BM;
1542 if (TMUL.sgt(0)) {
1543 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL));
1544 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1545 }
1546 else {
1547 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL));
1548 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1549 }
1550 if (TL.sle(TU)) {
1551 NewDirection |= Dependence::DVEntry::LT;
1552 ++ExactSIVsuccesses;
1553 }
1554
1555 // equal
1556 TU = SaveTU; // restore
1557 TL = SaveTL;
1558 DEBUG(dbgs() << "\t exploring EQ direction\n");
1559 if (TMUL.sgt(0)) {
1560 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL));
1561 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1562 }
1563 else {
1564 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL));
1565 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1566 }
1567 TMUL = BM - AM;
1568 if (TMUL.sgt(0)) {
1569 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL));
1570 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1571 }
1572 else {
1573 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL));
1574 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1575 }
1576 if (TL.sle(TU)) {
1577 NewDirection |= Dependence::DVEntry::EQ;
1578 ++ExactSIVsuccesses;
1579 }
1580
1581 // greater than
1582 TU = SaveTU; // restore
1583 TL = SaveTL;
1584 DEBUG(dbgs() << "\t exploring GT direction\n");
1585 if (TMUL.sgt(0)) {
1586 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL));
1587 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1588 }
1589 else {
1590 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL));
1591 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1592 }
1593 if (TL.sle(TU)) {
1594 NewDirection |= Dependence::DVEntry::GT;
1595 ++ExactSIVsuccesses;
1596 }
1597
1598 // finished
1599 Result.DV[Level].Direction &= NewDirection;
1600 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1601 ++ExactSIVindependence;
1602 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1603}
1604
1605
1606
1607// Return true if the divisor evenly divides the dividend.
1608static
1609bool isRemainderZero(const SCEVConstant *Dividend,
1610 const SCEVConstant *Divisor) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001611 APInt ConstDividend = Dividend->getAPInt();
1612 APInt ConstDivisor = Divisor->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001613 return ConstDividend.srem(ConstDivisor) == 0;
1614}
1615
1616
1617// weakZeroSrcSIVtest -
1618// From the paper, Practical Dependence Testing, Section 4.2.2
1619//
1620// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1621// where i is an induction variable, c1 and c2 are loop invariant,
1622// and a is a constant, we can solve it exactly using the
1623// Weak-Zero SIV test.
1624//
1625// Given
1626//
1627// c1 = c2 + a*i
1628//
1629// we get
1630//
1631// (c1 - c2)/a = i
1632//
1633// If i is not an integer, there's no dependence.
1634// If i < 0 or > UB, there's no dependence.
1635// If i = 0, the direction is <= and peeling the
1636// 1st iteration will break the dependence.
1637// If i = UB, the direction is >= and peeling the
1638// last iteration will break the dependence.
1639// Otherwise, the direction is *.
1640//
1641// Can prove independence. Failing that, we can sometimes refine
1642// the directions. Can sometimes show that first or last
1643// iteration carries all the dependences (so worth peeling).
1644//
1645// (see also weakZeroDstSIVtest)
1646//
1647// Return true if dependence disproved.
1648bool DependenceAnalysis::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1649 const SCEV *SrcConst,
1650 const SCEV *DstConst,
1651 const Loop *CurLoop,
1652 unsigned Level,
1653 FullDependence &Result,
1654 Constraint &NewConstraint) const {
1655 // For the WeakSIV test, it's possible the loop isn't common to
1656 // the Src and Dst loops. If it isn't, then there's no need to
1657 // record a direction.
1658 DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1659 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1660 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1661 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1662 ++WeakZeroSIVapplications;
1663 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1664 Level--;
1665 Result.Consistent = false;
1666 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001667 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1668 CurLoop);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001669 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1670 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1671 if (Level < CommonLevels) {
1672 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1673 Result.DV[Level].PeelFirst = true;
1674 ++WeakZeroSIVsuccesses;
1675 }
1676 return false; // dependences caused by first iteration
1677 }
1678 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1679 if (!ConstCoeff)
1680 return false;
1681 const SCEV *AbsCoeff =
1682 SE->isKnownNegative(ConstCoeff) ?
1683 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1684 const SCEV *NewDelta =
1685 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1686
1687 // check that Delta/SrcCoeff < iteration count
1688 // really check NewDelta < count*AbsCoeff
1689 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1690 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1691 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1692 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1693 ++WeakZeroSIVindependence;
1694 ++WeakZeroSIVsuccesses;
1695 return true;
1696 }
1697 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1698 // dependences caused by last iteration
1699 if (Level < CommonLevels) {
1700 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1701 Result.DV[Level].PeelLast = true;
1702 ++WeakZeroSIVsuccesses;
1703 }
1704 return false;
1705 }
1706 }
1707
1708 // check that Delta/SrcCoeff >= 0
1709 // really check that NewDelta >= 0
1710 if (SE->isKnownNegative(NewDelta)) {
1711 // No dependence, newDelta < 0
1712 ++WeakZeroSIVindependence;
1713 ++WeakZeroSIVsuccesses;
1714 return true;
1715 }
1716
1717 // if SrcCoeff doesn't divide Delta, then no dependence
1718 if (isa<SCEVConstant>(Delta) &&
1719 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1720 ++WeakZeroSIVindependence;
1721 ++WeakZeroSIVsuccesses;
1722 return true;
1723 }
1724 return false;
1725}
1726
1727
1728// weakZeroDstSIVtest -
1729// From the paper, Practical Dependence Testing, Section 4.2.2
1730//
1731// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1732// where i is an induction variable, c1 and c2 are loop invariant,
1733// and a is a constant, we can solve it exactly using the
1734// Weak-Zero SIV test.
1735//
1736// Given
1737//
1738// c1 + a*i = c2
1739//
1740// we get
1741//
1742// i = (c2 - c1)/a
1743//
1744// If i is not an integer, there's no dependence.
1745// If i < 0 or > UB, there's no dependence.
1746// If i = 0, the direction is <= and peeling the
1747// 1st iteration will break the dependence.
1748// If i = UB, the direction is >= and peeling the
1749// last iteration will break the dependence.
1750// Otherwise, the direction is *.
1751//
1752// Can prove independence. Failing that, we can sometimes refine
1753// the directions. Can sometimes show that first or last
1754// iteration carries all the dependences (so worth peeling).
1755//
1756// (see also weakZeroSrcSIVtest)
1757//
1758// Return true if dependence disproved.
1759bool DependenceAnalysis::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1760 const SCEV *SrcConst,
1761 const SCEV *DstConst,
1762 const Loop *CurLoop,
1763 unsigned Level,
1764 FullDependence &Result,
1765 Constraint &NewConstraint) const {
1766 // For the WeakSIV test, it's possible the loop isn't common to the
1767 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1768 DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1769 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1770 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1771 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1772 ++WeakZeroSIVapplications;
1773 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1774 Level--;
1775 Result.Consistent = false;
1776 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001777 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
1778 CurLoop);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001779 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1780 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1781 if (Level < CommonLevels) {
1782 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1783 Result.DV[Level].PeelFirst = true;
1784 ++WeakZeroSIVsuccesses;
1785 }
1786 return false; // dependences caused by first iteration
1787 }
1788 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1789 if (!ConstCoeff)
1790 return false;
1791 const SCEV *AbsCoeff =
1792 SE->isKnownNegative(ConstCoeff) ?
1793 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1794 const SCEV *NewDelta =
1795 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1796
1797 // check that Delta/SrcCoeff < iteration count
1798 // really check NewDelta < count*AbsCoeff
1799 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1800 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1801 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1802 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1803 ++WeakZeroSIVindependence;
1804 ++WeakZeroSIVsuccesses;
1805 return true;
1806 }
1807 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1808 // dependences caused by last iteration
1809 if (Level < CommonLevels) {
1810 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1811 Result.DV[Level].PeelLast = true;
1812 ++WeakZeroSIVsuccesses;
1813 }
1814 return false;
1815 }
1816 }
1817
1818 // check that Delta/SrcCoeff >= 0
1819 // really check that NewDelta >= 0
1820 if (SE->isKnownNegative(NewDelta)) {
1821 // No dependence, newDelta < 0
1822 ++WeakZeroSIVindependence;
1823 ++WeakZeroSIVsuccesses;
1824 return true;
1825 }
1826
1827 // if SrcCoeff doesn't divide Delta, then no dependence
1828 if (isa<SCEVConstant>(Delta) &&
1829 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1830 ++WeakZeroSIVindependence;
1831 ++WeakZeroSIVsuccesses;
1832 return true;
1833 }
1834 return false;
1835}
1836
1837
1838// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1839// Things of the form [c1 + a*i] and [c2 + b*j],
1840// where i and j are induction variable, c1 and c2 are loop invariant,
1841// and a and b are constants.
1842// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00001843// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001844// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1845bool DependenceAnalysis::exactRDIVtest(const SCEV *SrcCoeff,
1846 const SCEV *DstCoeff,
1847 const SCEV *SrcConst,
1848 const SCEV *DstConst,
1849 const Loop *SrcLoop,
1850 const Loop *DstLoop,
1851 FullDependence &Result) const {
1852 DEBUG(dbgs() << "\tExact RDIV test\n");
1853 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1854 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1855 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1856 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1857 ++ExactRDIVapplications;
1858 Result.Consistent = false;
1859 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1860 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1861 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1862 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1863 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1864 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1865 return false;
1866
1867 // find gcd
1868 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001869 APInt AM = ConstSrcCoeff->getAPInt();
1870 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001871 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001872 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001873 // gcd doesn't divide Delta, no dependence
1874 ++ExactRDIVindependence;
1875 return true;
1876 }
1877
1878 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1879
1880 // since SCEV construction seems to normalize, LM = 0
1881 APInt SrcUM(Bits, 1, true);
1882 bool SrcUMvalid = false;
1883 // SrcUM is perhaps unavailable, let's check
1884 if (const SCEVConstant *UpperBound =
1885 collectConstantUpperBound(SrcLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001886 SrcUM = UpperBound->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001887 DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n");
1888 SrcUMvalid = true;
1889 }
1890
1891 APInt DstUM(Bits, 1, true);
1892 bool DstUMvalid = false;
1893 // UM is perhaps unavailable, let's check
1894 if (const SCEVConstant *UpperBound =
1895 collectConstantUpperBound(DstLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001896 DstUM = UpperBound->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001897 DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n");
1898 DstUMvalid = true;
1899 }
1900
1901 APInt TU(APInt::getSignedMaxValue(Bits));
1902 APInt TL(APInt::getSignedMinValue(Bits));
1903
1904 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1905 APInt TMUL = BM.sdiv(G);
1906 if (TMUL.sgt(0)) {
1907 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1908 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1909 if (SrcUMvalid) {
1910 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL));
1911 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1912 }
1913 }
1914 else {
1915 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1916 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1917 if (SrcUMvalid) {
1918 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL));
1919 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1920 }
1921 }
1922
1923 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1924 TMUL = AM.sdiv(G);
1925 if (TMUL.sgt(0)) {
1926 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1927 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1928 if (DstUMvalid) {
1929 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL));
1930 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1931 }
1932 }
1933 else {
1934 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1935 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1936 if (DstUMvalid) {
1937 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL));
1938 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1939 }
1940 }
1941 if (TL.sgt(TU))
1942 ++ExactRDIVindependence;
1943 return TL.sgt(TU);
1944}
1945
1946
1947// symbolicRDIVtest -
1948// In Section 4.5 of the Practical Dependence Testing paper,the authors
1949// introduce a special case of Banerjee's Inequalities (also called the
1950// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
1951// particularly cases with symbolics. Since it's only able to disprove
1952// dependence (not compute distances or directions), we'll use it as a
1953// fall back for the other tests.
1954//
1955// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1956// where i and j are induction variables and c1 and c2 are loop invariants,
1957// we can use the symbolic tests to disprove some dependences, serving as a
1958// backup for the RDIV test. Note that i and j can be the same variable,
1959// letting this test serve as a backup for the various SIV tests.
1960//
1961// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
1962// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
1963// loop bounds for the i and j loops, respectively. So, ...
1964//
1965// c1 + a1*i = c2 + a2*j
1966// a1*i - a2*j = c2 - c1
1967//
1968// To test for a dependence, we compute c2 - c1 and make sure it's in the
1969// range of the maximum and minimum possible values of a1*i - a2*j.
1970// Considering the signs of a1 and a2, we have 4 possible cases:
1971//
1972// 1) If a1 >= 0 and a2 >= 0, then
1973// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
1974// -a2*N2 <= c2 - c1 <= a1*N1
1975//
1976// 2) If a1 >= 0 and a2 <= 0, then
1977// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
1978// 0 <= c2 - c1 <= a1*N1 - a2*N2
1979//
1980// 3) If a1 <= 0 and a2 >= 0, then
1981// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
1982// a1*N1 - a2*N2 <= c2 - c1 <= 0
1983//
1984// 4) If a1 <= 0 and a2 <= 0, then
1985// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
1986// a1*N1 <= c2 - c1 <= -a2*N2
1987//
1988// return true if dependence disproved
1989bool DependenceAnalysis::symbolicRDIVtest(const SCEV *A1,
1990 const SCEV *A2,
1991 const SCEV *C1,
1992 const SCEV *C2,
1993 const Loop *Loop1,
1994 const Loop *Loop2) const {
1995 ++SymbolicRDIVapplications;
1996 DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
1997 DEBUG(dbgs() << "\t A1 = " << *A1);
1998 DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
1999 DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
2000 DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
2001 DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
2002 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2003 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
2004 DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
2005 DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
2006 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2007 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
2008 DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
2009 DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
2010 if (SE->isKnownNonNegative(A1)) {
2011 if (SE->isKnownNonNegative(A2)) {
2012 // A1 >= 0 && A2 >= 0
2013 if (N1) {
2014 // make sure that c2 - c1 <= a1*N1
2015 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2016 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2017 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2018 ++SymbolicRDIVindependence;
2019 return true;
2020 }
2021 }
2022 if (N2) {
2023 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2024 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2025 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2026 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2027 ++SymbolicRDIVindependence;
2028 return true;
2029 }
2030 }
2031 }
2032 else if (SE->isKnownNonPositive(A2)) {
2033 // a1 >= 0 && a2 <= 0
2034 if (N1 && N2) {
2035 // make sure that c2 - c1 <= a1*N1 - a2*N2
2036 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2037 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2038 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2039 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2040 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2041 ++SymbolicRDIVindependence;
2042 return true;
2043 }
2044 }
2045 // make sure that 0 <= c2 - c1
2046 if (SE->isKnownNegative(C2_C1)) {
2047 ++SymbolicRDIVindependence;
2048 return true;
2049 }
2050 }
2051 }
2052 else if (SE->isKnownNonPositive(A1)) {
2053 if (SE->isKnownNonNegative(A2)) {
2054 // a1 <= 0 && a2 >= 0
2055 if (N1 && N2) {
2056 // make sure that a1*N1 - a2*N2 <= c2 - c1
2057 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2058 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2059 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2060 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2061 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2062 ++SymbolicRDIVindependence;
2063 return true;
2064 }
2065 }
2066 // make sure that c2 - c1 <= 0
2067 if (SE->isKnownPositive(C2_C1)) {
2068 ++SymbolicRDIVindependence;
2069 return true;
2070 }
2071 }
2072 else if (SE->isKnownNonPositive(A2)) {
2073 // a1 <= 0 && a2 <= 0
2074 if (N1) {
2075 // make sure that a1*N1 <= c2 - c1
2076 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2077 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2078 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2079 ++SymbolicRDIVindependence;
2080 return true;
2081 }
2082 }
2083 if (N2) {
2084 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2085 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2086 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2087 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2088 ++SymbolicRDIVindependence;
2089 return true;
2090 }
2091 }
2092 }
2093 }
2094 return false;
2095}
2096
2097
2098// testSIV -
2099// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2100// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2101// a2 are constant, we attack it with an SIV test. While they can all be
2102// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2103// they apply; they're cheaper and sometimes more precise.
2104//
2105// Return true if dependence disproved.
2106bool DependenceAnalysis::testSIV(const SCEV *Src,
2107 const SCEV *Dst,
2108 unsigned &Level,
2109 FullDependence &Result,
2110 Constraint &NewConstraint,
2111 const SCEV *&SplitIter) const {
2112 DEBUG(dbgs() << " src = " << *Src << "\n");
2113 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2114 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2115 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2116 if (SrcAddRec && DstAddRec) {
2117 const SCEV *SrcConst = SrcAddRec->getStart();
2118 const SCEV *DstConst = DstAddRec->getStart();
2119 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2120 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2121 const Loop *CurLoop = SrcAddRec->getLoop();
2122 assert(CurLoop == DstAddRec->getLoop() &&
2123 "both loops in SIV should be same");
2124 Level = mapSrcLoop(CurLoop);
2125 bool disproven;
2126 if (SrcCoeff == DstCoeff)
2127 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2128 Level, Result, NewConstraint);
2129 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2130 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2131 Level, Result, NewConstraint, SplitIter);
2132 else
2133 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2134 Level, Result, NewConstraint);
2135 return disproven ||
2136 gcdMIVtest(Src, Dst, Result) ||
2137 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
2138 }
2139 if (SrcAddRec) {
2140 const SCEV *SrcConst = SrcAddRec->getStart();
2141 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2142 const SCEV *DstConst = Dst;
2143 const Loop *CurLoop = SrcAddRec->getLoop();
2144 Level = mapSrcLoop(CurLoop);
2145 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2146 Level, Result, NewConstraint) ||
2147 gcdMIVtest(Src, Dst, Result);
2148 }
2149 if (DstAddRec) {
2150 const SCEV *DstConst = DstAddRec->getStart();
2151 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2152 const SCEV *SrcConst = Src;
2153 const Loop *CurLoop = DstAddRec->getLoop();
2154 Level = mapDstLoop(CurLoop);
2155 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
2156 CurLoop, Level, Result, NewConstraint) ||
2157 gcdMIVtest(Src, Dst, Result);
2158 }
2159 llvm_unreachable("SIV test expected at least one AddRec");
2160 return false;
2161}
2162
2163
2164// testRDIV -
2165// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2166// where i and j are induction variables, c1 and c2 are loop invariant,
2167// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2168// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2169// It doesn't make sense to talk about distance or direction in this case,
2170// so there's no point in making special versions of the Strong SIV test or
2171// the Weak-crossing SIV test.
2172//
2173// With minor algebra, this test can also be used for things like
2174// [c1 + a1*i + a2*j][c2].
2175//
2176// Return true if dependence disproved.
2177bool DependenceAnalysis::testRDIV(const SCEV *Src,
2178 const SCEV *Dst,
2179 FullDependence &Result) const {
2180 // we have 3 possible situations here:
2181 // 1) [a*i + b] and [c*j + d]
2182 // 2) [a*i + c*j + b] and [d]
2183 // 3) [b] and [a*i + c*j + d]
2184 // We need to find what we've got and get organized
2185
2186 const SCEV *SrcConst, *DstConst;
2187 const SCEV *SrcCoeff, *DstCoeff;
2188 const Loop *SrcLoop, *DstLoop;
2189
2190 DEBUG(dbgs() << " src = " << *Src << "\n");
2191 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2192 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2193 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2194 if (SrcAddRec && DstAddRec) {
2195 SrcConst = SrcAddRec->getStart();
2196 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2197 SrcLoop = SrcAddRec->getLoop();
2198 DstConst = DstAddRec->getStart();
2199 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2200 DstLoop = DstAddRec->getLoop();
2201 }
2202 else if (SrcAddRec) {
2203 if (const SCEVAddRecExpr *tmpAddRec =
2204 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2205 SrcConst = tmpAddRec->getStart();
2206 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2207 SrcLoop = tmpAddRec->getLoop();
2208 DstConst = Dst;
2209 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2210 DstLoop = SrcAddRec->getLoop();
2211 }
2212 else
2213 llvm_unreachable("RDIV reached by surprising SCEVs");
2214 }
2215 else if (DstAddRec) {
2216 if (const SCEVAddRecExpr *tmpAddRec =
2217 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2218 DstConst = tmpAddRec->getStart();
2219 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2220 DstLoop = tmpAddRec->getLoop();
2221 SrcConst = Src;
2222 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2223 SrcLoop = DstAddRec->getLoop();
2224 }
2225 else
2226 llvm_unreachable("RDIV reached by surprising SCEVs");
2227 }
2228 else
2229 llvm_unreachable("RDIV expected at least one AddRec");
2230 return exactRDIVtest(SrcCoeff, DstCoeff,
2231 SrcConst, DstConst,
2232 SrcLoop, DstLoop,
2233 Result) ||
2234 gcdMIVtest(Src, Dst, Result) ||
2235 symbolicRDIVtest(SrcCoeff, DstCoeff,
2236 SrcConst, DstConst,
2237 SrcLoop, DstLoop);
2238}
2239
2240
2241// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2242// Return true if dependence disproved.
2243// Can sometimes refine direction vectors.
2244bool DependenceAnalysis::testMIV(const SCEV *Src,
2245 const SCEV *Dst,
2246 const SmallBitVector &Loops,
2247 FullDependence &Result) const {
2248 DEBUG(dbgs() << " src = " << *Src << "\n");
2249 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2250 Result.Consistent = false;
2251 return gcdMIVtest(Src, Dst, Result) ||
2252 banerjeeMIVtest(Src, Dst, Loops, Result);
2253}
2254
2255
2256// Given a product, e.g., 10*X*Y, returns the first constant operand,
2257// in this case 10. If there is no constant part, returns NULL.
2258static
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002259const SCEVConstant *getConstantPart(const SCEV *Expr) {
2260 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2261 return Constant;
2262 else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2263 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
Sebastian Pop59b61b92012-10-11 07:32:34 +00002264 return Constant;
Craig Topper9f008862014-04-15 04:59:12 +00002265 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002266}
2267
2268
2269//===----------------------------------------------------------------------===//
2270// gcdMIVtest -
2271// Tests an MIV subscript pair for dependence.
2272// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00002273// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002274// Can sometimes disprove the equal direction for 1 or more loops,
2275// as discussed in Michael Wolfe's book,
2276// High Performance Compilers for Parallel Computing, page 235.
2277//
2278// We spend some effort (code!) to handle cases like
2279// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2280// but M and N are just loop-invariant variables.
2281// This should help us handle linearized subscripts;
2282// also makes this test a useful backup to the various SIV tests.
2283//
2284// It occurs to me that the presence of loop-invariant variables
2285// changes the nature of the test from "greatest common divisor"
Preston Briggs4eb7ee52012-11-29 04:30:52 +00002286// to "a common divisor".
Sebastian Pop59b61b92012-10-11 07:32:34 +00002287bool DependenceAnalysis::gcdMIVtest(const SCEV *Src,
2288 const SCEV *Dst,
2289 FullDependence &Result) const {
2290 DEBUG(dbgs() << "starting gcd\n");
2291 ++GCDapplications;
Preston Briggs3ad39492012-11-21 23:50:04 +00002292 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002293 APInt RunningGCD = APInt::getNullValue(BitWidth);
2294
2295 // Examine Src coefficients.
2296 // Compute running GCD and record source constant.
2297 // Because we're looking for the constant at the end of the chain,
2298 // we can't quit the loop just because the GCD == 1.
2299 const SCEV *Coefficients = Src;
2300 while (const SCEVAddRecExpr *AddRec =
2301 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2302 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002303 // If the coefficient is the product of a constant and other stuff,
2304 // we can use the constant in the GCD computation.
2305 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002306 if (!Constant)
2307 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002308 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002309 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2310 Coefficients = AddRec->getStart();
2311 }
2312 const SCEV *SrcConst = Coefficients;
2313
2314 // Examine Dst coefficients.
2315 // Compute running GCD and record destination constant.
2316 // Because we're looking for the constant at the end of the chain,
2317 // we can't quit the loop just because the GCD == 1.
2318 Coefficients = Dst;
2319 while (const SCEVAddRecExpr *AddRec =
2320 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2321 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002322 // If the coefficient is the product of a constant and other stuff,
2323 // we can use the constant in the GCD computation.
2324 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002325 if (!Constant)
2326 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002327 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002328 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2329 Coefficients = AddRec->getStart();
2330 }
2331 const SCEV *DstConst = Coefficients;
2332
2333 APInt ExtraGCD = APInt::getNullValue(BitWidth);
2334 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2335 DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2336 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2337 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2338 // If Delta is a sum of products, we may be able to make further progress.
2339 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
2340 const SCEV *Operand = Sum->getOperand(Op);
2341 if (isa<SCEVConstant>(Operand)) {
2342 assert(!Constant && "Surprised to find multiple constants");
2343 Constant = cast<SCEVConstant>(Operand);
2344 }
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002345 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002346 // Search for constant operand to participate in GCD;
2347 // If none found; return false.
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002348 const SCEVConstant *ConstOp = getConstantPart(Product);
2349 if (!ConstOp)
2350 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002351 APInt ConstOpValue = ConstOp->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002352 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2353 ConstOpValue.abs());
2354 }
2355 else
2356 return false;
2357 }
2358 }
2359 if (!Constant)
2360 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002361 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002362 DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2363 if (ConstDelta == 0)
2364 return false;
2365 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2366 DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2367 APInt Remainder = ConstDelta.srem(RunningGCD);
2368 if (Remainder != 0) {
2369 ++GCDindependence;
2370 return true;
2371 }
2372
2373 // Try to disprove equal directions.
2374 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2375 // the code above can't disprove the dependence because the GCD = 1.
2376 // So we consider what happen if i = i' and what happens if j = j'.
2377 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2378 // which is infeasible, so we can disallow the = direction for the i level.
2379 // Setting j = j' doesn't help matters, so we end up with a direction vector
2380 // of [<>, *]
2381 //
2382 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2383 // we need to remember that the constant part is 5 and the RunningGCD should
2384 // be initialized to ExtraGCD = 30.
2385 DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2386
2387 bool Improved = false;
2388 Coefficients = Src;
2389 while (const SCEVAddRecExpr *AddRec =
2390 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2391 Coefficients = AddRec->getStart();
2392 const Loop *CurLoop = AddRec->getLoop();
2393 RunningGCD = ExtraGCD;
2394 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2395 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2396 const SCEV *Inner = Src;
2397 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2398 AddRec = cast<SCEVAddRecExpr>(Inner);
2399 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2400 if (CurLoop == AddRec->getLoop())
2401 ; // SrcCoeff == Coeff
2402 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002403 // If the coefficient is the product of a constant and other stuff,
2404 // we can use the constant in the GCD computation.
2405 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002406 if (!Constant)
2407 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002408 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002409 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2410 }
2411 Inner = AddRec->getStart();
2412 }
2413 Inner = Dst;
2414 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2415 AddRec = cast<SCEVAddRecExpr>(Inner);
2416 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2417 if (CurLoop == AddRec->getLoop())
2418 DstCoeff = Coeff;
2419 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002420 // If the coefficient is the product of a constant and other stuff,
2421 // we can use the constant in the GCD computation.
2422 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002423 if (!Constant)
2424 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002425 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002426 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2427 }
2428 Inner = AddRec->getStart();
2429 }
2430 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002431 // If the coefficient is the product of a constant and other stuff,
2432 // we can use the constant in the GCD computation.
2433 Constant = getConstantPart(Delta);
2434 if (!Constant)
Sebastian Pop59b61b92012-10-11 07:32:34 +00002435 // The difference of the two coefficients might not be a product
2436 // or constant, in which case we give up on this direction.
2437 continue;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002438 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002439 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2440 DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2441 if (RunningGCD != 0) {
2442 Remainder = ConstDelta.srem(RunningGCD);
2443 DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2444 if (Remainder != 0) {
2445 unsigned Level = mapSrcLoop(CurLoop);
Sebastian Pope96232612012-10-12 02:04:32 +00002446 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002447 Improved = true;
2448 }
2449 }
2450 }
2451 if (Improved)
2452 ++GCDsuccesses;
2453 DEBUG(dbgs() << "all done\n");
2454 return false;
2455}
2456
2457
2458//===----------------------------------------------------------------------===//
2459// banerjeeMIVtest -
2460// Use Banerjee's Inequalities to test an MIV subscript pair.
2461// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2462// Generally follows the discussion in Section 2.5.2 of
2463//
2464// Optimizing Supercompilers for Supercomputers
2465// Michael Wolfe
2466//
2467// The inequalities given on page 25 are simplified in that loops are
2468// normalized so that the lower bound is always 0 and the stride is always 1.
2469// For example, Wolfe gives
2470//
2471// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2472//
2473// where A_k is the coefficient of the kth index in the source subscript,
2474// B_k is the coefficient of the kth index in the destination subscript,
2475// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2476// index, and N_k is the stride of the kth index. Since all loops are normalized
2477// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2478// equation to
2479//
2480// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2481// = (A^-_k - B_k)^- (U_k - 1) - B_k
2482//
2483// Similar simplifications are possible for the other equations.
2484//
2485// When we can't determine the number of iterations for a loop,
2486// we use NULL as an indicator for the worst case, infinity.
2487// When computing the upper bound, NULL denotes +inf;
2488// for the lower bound, NULL denotes -inf.
2489//
2490// Return true if dependence disproved.
2491bool DependenceAnalysis::banerjeeMIVtest(const SCEV *Src,
2492 const SCEV *Dst,
2493 const SmallBitVector &Loops,
2494 FullDependence &Result) const {
2495 DEBUG(dbgs() << "starting Banerjee\n");
2496 ++BanerjeeApplications;
2497 DEBUG(dbgs() << " Src = " << *Src << '\n');
2498 const SCEV *A0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002499 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002500 DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2501 const SCEV *B0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002502 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2503 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002504 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2505 DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2506
2507 // Compute bounds for all the * directions.
2508 DEBUG(dbgs() << "\tBounds[*]\n");
2509 for (unsigned K = 1; K <= MaxLevels; ++K) {
2510 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2511 Bound[K].Direction = Dependence::DVEntry::ALL;
2512 Bound[K].DirSet = Dependence::DVEntry::NONE;
2513 findBoundsALL(A, B, Bound, K);
2514#ifndef NDEBUG
2515 DEBUG(dbgs() << "\t " << K << '\t');
2516 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2517 DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2518 else
2519 DEBUG(dbgs() << "-inf\t");
2520 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2521 DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2522 else
2523 DEBUG(dbgs() << "+inf\n");
2524#endif
2525 }
2526
2527 // Test the *, *, *, ... case.
2528 bool Disproved = false;
2529 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2530 // Explore the direction vector hierarchy.
2531 unsigned DepthExpanded = 0;
2532 unsigned NewDeps = exploreDirections(1, A, B, Bound,
2533 Loops, DepthExpanded, Delta);
2534 if (NewDeps > 0) {
2535 bool Improved = false;
2536 for (unsigned K = 1; K <= CommonLevels; ++K) {
2537 if (Loops[K]) {
2538 unsigned Old = Result.DV[K - 1].Direction;
2539 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2540 Improved |= Old != Result.DV[K - 1].Direction;
2541 if (!Result.DV[K - 1].Direction) {
2542 Improved = false;
2543 Disproved = true;
2544 break;
2545 }
2546 }
2547 }
2548 if (Improved)
2549 ++BanerjeeSuccesses;
2550 }
2551 else {
2552 ++BanerjeeIndependence;
2553 Disproved = true;
2554 }
2555 }
2556 else {
2557 ++BanerjeeIndependence;
2558 Disproved = true;
2559 }
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002560 delete [] Bound;
2561 delete [] A;
2562 delete [] B;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002563 return Disproved;
2564}
2565
2566
2567// Hierarchically expands the direction vector
2568// search space, combining the directions of discovered dependences
2569// in the DirSet field of Bound. Returns the number of distinct
2570// dependences discovered. If the dependence is disproved,
2571// it will return 0.
2572unsigned DependenceAnalysis::exploreDirections(unsigned Level,
2573 CoefficientInfo *A,
2574 CoefficientInfo *B,
2575 BoundInfo *Bound,
2576 const SmallBitVector &Loops,
2577 unsigned &DepthExpanded,
2578 const SCEV *Delta) const {
2579 if (Level > CommonLevels) {
2580 // record result
2581 DEBUG(dbgs() << "\t[");
2582 for (unsigned K = 1; K <= CommonLevels; ++K) {
2583 if (Loops[K]) {
2584 Bound[K].DirSet |= Bound[K].Direction;
2585#ifndef NDEBUG
2586 switch (Bound[K].Direction) {
2587 case Dependence::DVEntry::LT:
2588 DEBUG(dbgs() << " <");
2589 break;
2590 case Dependence::DVEntry::EQ:
2591 DEBUG(dbgs() << " =");
2592 break;
2593 case Dependence::DVEntry::GT:
2594 DEBUG(dbgs() << " >");
2595 break;
2596 case Dependence::DVEntry::ALL:
2597 DEBUG(dbgs() << " *");
2598 break;
2599 default:
2600 llvm_unreachable("unexpected Bound[K].Direction");
2601 }
2602#endif
2603 }
2604 }
2605 DEBUG(dbgs() << " ]\n");
2606 return 1;
2607 }
2608 if (Loops[Level]) {
2609 if (Level > DepthExpanded) {
2610 DepthExpanded = Level;
2611 // compute bounds for <, =, > at current level
2612 findBoundsLT(A, B, Bound, Level);
2613 findBoundsGT(A, B, Bound, Level);
2614 findBoundsEQ(A, B, Bound, Level);
2615#ifndef NDEBUG
2616 DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2617 DEBUG(dbgs() << "\t <\t");
2618 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2619 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT] << '\t');
2620 else
2621 DEBUG(dbgs() << "-inf\t");
2622 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2623 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT] << '\n');
2624 else
2625 DEBUG(dbgs() << "+inf\n");
2626 DEBUG(dbgs() << "\t =\t");
2627 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2628 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ] << '\t');
2629 else
2630 DEBUG(dbgs() << "-inf\t");
2631 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2632 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ] << '\n');
2633 else
2634 DEBUG(dbgs() << "+inf\n");
2635 DEBUG(dbgs() << "\t >\t");
2636 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2637 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT] << '\t');
2638 else
2639 DEBUG(dbgs() << "-inf\t");
2640 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2641 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT] << '\n');
2642 else
2643 DEBUG(dbgs() << "+inf\n");
2644#endif
2645 }
2646
2647 unsigned NewDeps = 0;
2648
2649 // test bounds for <, *, *, ...
2650 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2651 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2652 Loops, DepthExpanded, Delta);
2653
2654 // Test bounds for =, *, *, ...
2655 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2656 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2657 Loops, DepthExpanded, Delta);
2658
2659 // test bounds for >, *, *, ...
2660 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2661 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2662 Loops, DepthExpanded, Delta);
2663
2664 Bound[Level].Direction = Dependence::DVEntry::ALL;
2665 return NewDeps;
2666 }
2667 else
2668 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
2669}
2670
2671
2672// Returns true iff the current bounds are plausible.
2673bool DependenceAnalysis::testBounds(unsigned char DirKind,
2674 unsigned Level,
2675 BoundInfo *Bound,
2676 const SCEV *Delta) const {
2677 Bound[Level].Direction = DirKind;
2678 if (const SCEV *LowerBound = getLowerBound(Bound))
2679 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2680 return false;
2681 if (const SCEV *UpperBound = getUpperBound(Bound))
2682 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2683 return false;
2684 return true;
2685}
2686
2687
2688// Computes the upper and lower bounds for level K
2689// using the * direction. Records them in Bound.
2690// Wolfe gives the equations
2691//
2692// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2693// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2694//
2695// Since we normalize loops, we can simplify these equations to
2696//
2697// LB^*_k = (A^-_k - B^+_k)U_k
2698// UB^*_k = (A^+_k - B^-_k)U_k
2699//
2700// We must be careful to handle the case where the upper bound is unknown.
2701// Note that the lower bound is always <= 0
2702// and the upper bound is always >= 0.
2703void DependenceAnalysis::findBoundsALL(CoefficientInfo *A,
2704 CoefficientInfo *B,
2705 BoundInfo *Bound,
2706 unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002707 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
2708 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002709 if (Bound[K].Iterations) {
2710 Bound[K].Lower[Dependence::DVEntry::ALL] =
2711 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
2712 Bound[K].Iterations);
2713 Bound[K].Upper[Dependence::DVEntry::ALL] =
2714 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
2715 Bound[K].Iterations);
2716 }
2717 else {
2718 // If the difference is 0, we won't need to know the number of iterations.
2719 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2720 Bound[K].Lower[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002721 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002722 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2723 Bound[K].Upper[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002724 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002725 }
2726}
2727
2728
2729// Computes the upper and lower bounds for level K
2730// using the = direction. Records them in Bound.
2731// Wolfe gives the equations
2732//
2733// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2734// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2735//
2736// Since we normalize loops, we can simplify these equations to
2737//
2738// LB^=_k = (A_k - B_k)^- U_k
2739// UB^=_k = (A_k - B_k)^+ U_k
2740//
2741// We must be careful to handle the case where the upper bound is unknown.
2742// Note that the lower bound is always <= 0
2743// and the upper bound is always >= 0.
2744void DependenceAnalysis::findBoundsEQ(CoefficientInfo *A,
2745 CoefficientInfo *B,
2746 BoundInfo *Bound,
2747 unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002748 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
2749 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002750 if (Bound[K].Iterations) {
2751 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2752 const SCEV *NegativePart = getNegativePart(Delta);
2753 Bound[K].Lower[Dependence::DVEntry::EQ] =
2754 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2755 const SCEV *PositivePart = getPositivePart(Delta);
2756 Bound[K].Upper[Dependence::DVEntry::EQ] =
2757 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2758 }
2759 else {
2760 // If the positive/negative part of the difference is 0,
2761 // we won't need to know the number of iterations.
2762 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2763 const SCEV *NegativePart = getNegativePart(Delta);
2764 if (NegativePart->isZero())
2765 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2766 const SCEV *PositivePart = getPositivePart(Delta);
2767 if (PositivePart->isZero())
2768 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2769 }
2770}
2771
2772
2773// Computes the upper and lower bounds for level K
2774// using the < direction. Records them in Bound.
2775// Wolfe gives the equations
2776//
2777// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2778// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2779//
2780// Since we normalize loops, we can simplify these equations to
2781//
2782// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2783// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2784//
2785// We must be careful to handle the case where the upper bound is unknown.
2786void DependenceAnalysis::findBoundsLT(CoefficientInfo *A,
2787 CoefficientInfo *B,
2788 BoundInfo *Bound,
2789 unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002790 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
2791 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002792 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002793 const SCEV *Iter_1 = SE->getMinusSCEV(
2794 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002795 const SCEV *NegPart =
2796 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2797 Bound[K].Lower[Dependence::DVEntry::LT] =
2798 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2799 const SCEV *PosPart =
2800 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2801 Bound[K].Upper[Dependence::DVEntry::LT] =
2802 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2803 }
2804 else {
2805 // If the positive/negative part of the difference is 0,
2806 // we won't need to know the number of iterations.
2807 const SCEV *NegPart =
2808 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2809 if (NegPart->isZero())
2810 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2811 const SCEV *PosPart =
2812 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2813 if (PosPart->isZero())
2814 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2815 }
2816}
2817
2818
2819// Computes the upper and lower bounds for level K
2820// using the > direction. Records them in Bound.
2821// Wolfe gives the equations
2822//
2823// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2824// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2825//
2826// Since we normalize loops, we can simplify these equations to
2827//
2828// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2829// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2830//
2831// We must be careful to handle the case where the upper bound is unknown.
2832void DependenceAnalysis::findBoundsGT(CoefficientInfo *A,
2833 CoefficientInfo *B,
2834 BoundInfo *Bound,
2835 unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002836 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
2837 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002838 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002839 const SCEV *Iter_1 = SE->getMinusSCEV(
2840 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002841 const SCEV *NegPart =
2842 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2843 Bound[K].Lower[Dependence::DVEntry::GT] =
2844 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2845 const SCEV *PosPart =
2846 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2847 Bound[K].Upper[Dependence::DVEntry::GT] =
2848 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2849 }
2850 else {
2851 // If the positive/negative part of the difference is 0,
2852 // we won't need to know the number of iterations.
2853 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2854 if (NegPart->isZero())
2855 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2856 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2857 if (PosPart->isZero())
2858 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2859 }
2860}
2861
2862
2863// X^+ = max(X, 0)
2864const SCEV *DependenceAnalysis::getPositivePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002865 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002866}
2867
2868
2869// X^- = min(X, 0)
2870const SCEV *DependenceAnalysis::getNegativePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002871 return SE->getSMinExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002872}
2873
2874
2875// Walks through the subscript,
2876// collecting each coefficient, the associated loop bounds,
2877// and recording its positive and negative parts for later use.
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002878DependenceAnalysis::CoefficientInfo *
Sebastian Pop59b61b92012-10-11 07:32:34 +00002879DependenceAnalysis::collectCoeffInfo(const SCEV *Subscript,
2880 bool SrcFlag,
2881 const SCEV *&Constant) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002882 const SCEV *Zero = SE->getZero(Subscript->getType());
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002883 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002884 for (unsigned K = 1; K <= MaxLevels; ++K) {
2885 CI[K].Coeff = Zero;
2886 CI[K].PosPart = Zero;
2887 CI[K].NegPart = Zero;
Craig Topper9f008862014-04-15 04:59:12 +00002888 CI[K].Iterations = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002889 }
2890 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2891 const Loop *L = AddRec->getLoop();
2892 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2893 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2894 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2895 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2896 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2897 Subscript = AddRec->getStart();
2898 }
2899 Constant = Subscript;
2900#ifndef NDEBUG
2901 DEBUG(dbgs() << "\tCoefficient Info\n");
2902 for (unsigned K = 1; K <= MaxLevels; ++K) {
2903 DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
2904 DEBUG(dbgs() << "\tPos Part = ");
2905 DEBUG(dbgs() << *CI[K].PosPart);
2906 DEBUG(dbgs() << "\tNeg Part = ");
2907 DEBUG(dbgs() << *CI[K].NegPart);
2908 DEBUG(dbgs() << "\tUpper Bound = ");
2909 if (CI[K].Iterations)
2910 DEBUG(dbgs() << *CI[K].Iterations);
2911 else
2912 DEBUG(dbgs() << "+inf");
2913 DEBUG(dbgs() << '\n');
2914 }
2915 DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
2916#endif
2917 return CI;
2918}
2919
2920
2921// Looks through all the bounds info and
2922// computes the lower bound given the current direction settings
2923// at each level. If the lower bound for any level is -inf,
2924// the result is -inf.
2925const SCEV *DependenceAnalysis::getLowerBound(BoundInfo *Bound) const {
2926 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2927 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2928 if (Bound[K].Lower[Bound[K].Direction])
2929 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2930 else
Craig Topper9f008862014-04-15 04:59:12 +00002931 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002932 }
2933 return Sum;
2934}
2935
2936
2937// Looks through all the bounds info and
2938// computes the upper bound given the current direction settings
2939// at each level. If the upper bound at any level is +inf,
2940// the result is +inf.
2941const SCEV *DependenceAnalysis::getUpperBound(BoundInfo *Bound) const {
2942 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2943 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2944 if (Bound[K].Upper[Bound[K].Direction])
2945 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2946 else
Craig Topper9f008862014-04-15 04:59:12 +00002947 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002948 }
2949 return Sum;
2950}
2951
2952
2953//===----------------------------------------------------------------------===//
2954// Constraint manipulation for Delta test.
2955
2956// Given a linear SCEV,
2957// return the coefficient (the step)
2958// corresponding to the specified loop.
2959// If there isn't one, return 0.
Jingyue Wua84feb12015-05-29 16:58:08 +00002960// For example, given a*i + b*j + c*k, finding the coefficient
Sebastian Pop59b61b92012-10-11 07:32:34 +00002961// corresponding to the j loop would yield b.
2962const SCEV *DependenceAnalysis::findCoefficient(const SCEV *Expr,
2963 const Loop *TargetLoop) const {
2964 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2965 if (!AddRec)
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002966 return SE->getZero(Expr->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002967 if (AddRec->getLoop() == TargetLoop)
2968 return AddRec->getStepRecurrence(*SE);
2969 return findCoefficient(AddRec->getStart(), TargetLoop);
2970}
2971
2972
2973// Given a linear SCEV,
2974// return the SCEV given by zeroing out the coefficient
2975// corresponding to the specified loop.
2976// For example, given a*i + b*j + c*k, zeroing the coefficient
2977// corresponding to the j loop would yield a*i + c*k.
2978const SCEV *DependenceAnalysis::zeroCoefficient(const SCEV *Expr,
2979 const Loop *TargetLoop) const {
2980 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2981 if (!AddRec)
2982 return Expr; // ignore
2983 if (AddRec->getLoop() == TargetLoop)
2984 return AddRec->getStart();
2985 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
2986 AddRec->getStepRecurrence(*SE),
2987 AddRec->getLoop(),
2988 AddRec->getNoWrapFlags());
2989}
2990
2991
2992// Given a linear SCEV Expr,
2993// return the SCEV given by adding some Value to the
2994// coefficient corresponding to the specified TargetLoop.
2995// For example, given a*i + b*j + c*k, adding 1 to the coefficient
2996// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
2997const SCEV *DependenceAnalysis::addToCoefficient(const SCEV *Expr,
2998 const Loop *TargetLoop,
2999 const SCEV *Value) const {
3000 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3001 if (!AddRec) // create a new addRec
3002 return SE->getAddRecExpr(Expr,
3003 Value,
3004 TargetLoop,
3005 SCEV::FlagAnyWrap); // Worst case, with no info.
3006 if (AddRec->getLoop() == TargetLoop) {
3007 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
3008 if (Sum->isZero())
3009 return AddRec->getStart();
3010 return SE->getAddRecExpr(AddRec->getStart(),
3011 Sum,
3012 AddRec->getLoop(),
3013 AddRec->getNoWrapFlags());
3014 }
Preston Briggs6c286b62013-06-28 18:44:48 +00003015 if (SE->isLoopInvariant(AddRec, TargetLoop))
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +00003016 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
3017 return SE->getAddRecExpr(
3018 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
3019 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3020 AddRec->getNoWrapFlags());
Sebastian Pop59b61b92012-10-11 07:32:34 +00003021}
3022
3023
3024// Review the constraints, looking for opportunities
3025// to simplify a subscript pair (Src and Dst).
3026// Return true if some simplification occurs.
3027// If the simplification isn't exact (that is, if it is conservative
3028// in terms of dependence), set consistent to false.
3029// Corresponds to Figure 5 from the paper
3030//
3031// Practical Dependence Testing
3032// Goff, Kennedy, Tseng
3033// PLDI 1991
3034bool DependenceAnalysis::propagate(const SCEV *&Src,
3035 const SCEV *&Dst,
3036 SmallBitVector &Loops,
Craig Topperb94011f2013-07-14 04:42:23 +00003037 SmallVectorImpl<Constraint> &Constraints,
Sebastian Pop59b61b92012-10-11 07:32:34 +00003038 bool &Consistent) {
3039 bool Result = false;
3040 for (int LI = Loops.find_first(); LI >= 0; LI = Loops.find_next(LI)) {
3041 DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
3042 DEBUG(Constraints[LI].dump(dbgs()));
3043 if (Constraints[LI].isDistance())
3044 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3045 else if (Constraints[LI].isLine())
3046 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3047 else if (Constraints[LI].isPoint())
3048 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3049 }
3050 return Result;
3051}
3052
3053
3054// Attempt to propagate a distance
3055// constraint into a subscript pair (Src and Dst).
3056// Return true if some simplification occurs.
3057// If the simplification isn't exact (that is, if it is conservative
3058// in terms of dependence), set consistent to false.
3059bool DependenceAnalysis::propagateDistance(const SCEV *&Src,
3060 const SCEV *&Dst,
3061 Constraint &CurConstraint,
3062 bool &Consistent) {
3063 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3064 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3065 const SCEV *A_K = findCoefficient(Src, CurLoop);
3066 if (A_K->isZero())
3067 return false;
3068 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3069 Src = SE->getMinusSCEV(Src, DA_K);
3070 Src = zeroCoefficient(Src, CurLoop);
3071 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3072 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3073 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
3074 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3075 if (!findCoefficient(Dst, CurLoop)->isZero())
3076 Consistent = false;
3077 return true;
3078}
3079
3080
3081// Attempt to propagate a line
3082// constraint into a subscript pair (Src and Dst).
3083// Return true if some simplification occurs.
3084// If the simplification isn't exact (that is, if it is conservative
3085// in terms of dependence), set consistent to false.
3086bool DependenceAnalysis::propagateLine(const SCEV *&Src,
3087 const SCEV *&Dst,
3088 Constraint &CurConstraint,
3089 bool &Consistent) {
3090 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3091 const SCEV *A = CurConstraint.getA();
3092 const SCEV *B = CurConstraint.getB();
3093 const SCEV *C = CurConstraint.getC();
3094 DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C << "\n");
3095 DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3096 DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
3097 if (A->isZero()) {
3098 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3099 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3100 if (!Bconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003101 APInt Beta = Bconst->getAPInt();
3102 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003103 APInt CdivB = Charlie.sdiv(Beta);
3104 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3105 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3106 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3107 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3108 Dst = zeroCoefficient(Dst, CurLoop);
3109 if (!findCoefficient(Src, CurLoop)->isZero())
3110 Consistent = false;
3111 }
3112 else if (B->isZero()) {
3113 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3114 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3115 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003116 APInt Alpha = Aconst->getAPInt();
3117 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003118 APInt CdivA = Charlie.sdiv(Alpha);
3119 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3120 const SCEV *A_K = findCoefficient(Src, CurLoop);
3121 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3122 Src = zeroCoefficient(Src, CurLoop);
3123 if (!findCoefficient(Dst, CurLoop)->isZero())
3124 Consistent = false;
3125 }
3126 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3127 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3128 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3129 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003130 APInt Alpha = Aconst->getAPInt();
3131 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003132 APInt CdivA = Charlie.sdiv(Alpha);
3133 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3134 const SCEV *A_K = findCoefficient(Src, CurLoop);
3135 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3136 Src = zeroCoefficient(Src, CurLoop);
3137 Dst = addToCoefficient(Dst, CurLoop, A_K);
3138 if (!findCoefficient(Dst, CurLoop)->isZero())
3139 Consistent = false;
3140 }
3141 else {
3142 // paper is incorrect here, or perhaps just misleading
3143 const SCEV *A_K = findCoefficient(Src, CurLoop);
3144 Src = SE->getMulExpr(Src, A);
3145 Dst = SE->getMulExpr(Dst, A);
3146 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3147 Src = zeroCoefficient(Src, CurLoop);
3148 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3149 if (!findCoefficient(Dst, CurLoop)->isZero())
3150 Consistent = false;
3151 }
3152 DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3153 DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
3154 return true;
3155}
3156
3157
3158// Attempt to propagate a point
3159// constraint into a subscript pair (Src and Dst).
3160// Return true if some simplification occurs.
3161bool DependenceAnalysis::propagatePoint(const SCEV *&Src,
3162 const SCEV *&Dst,
3163 Constraint &CurConstraint) {
3164 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3165 const SCEV *A_K = findCoefficient(Src, CurLoop);
3166 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3167 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3168 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3169 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3170 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3171 Src = zeroCoefficient(Src, CurLoop);
3172 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3173 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3174 Dst = zeroCoefficient(Dst, CurLoop);
3175 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3176 return true;
3177}
3178
3179
3180// Update direction vector entry based on the current constraint.
3181void DependenceAnalysis::updateDirection(Dependence::DVEntry &Level,
3182 const Constraint &CurConstraint
3183 ) const {
3184 DEBUG(dbgs() << "\tUpdate direction, constraint =");
3185 DEBUG(CurConstraint.dump(dbgs()));
3186 if (CurConstraint.isAny())
3187 ; // use defaults
3188 else if (CurConstraint.isDistance()) {
3189 // this one is consistent, the others aren't
3190 Level.Scalar = false;
3191 Level.Distance = CurConstraint.getD();
3192 unsigned NewDirection = Dependence::DVEntry::NONE;
3193 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3194 NewDirection = Dependence::DVEntry::EQ;
3195 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3196 NewDirection |= Dependence::DVEntry::LT;
3197 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3198 NewDirection |= Dependence::DVEntry::GT;
3199 Level.Direction &= NewDirection;
3200 }
3201 else if (CurConstraint.isLine()) {
3202 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003203 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003204 // direction should be accurate
3205 }
3206 else if (CurConstraint.isPoint()) {
3207 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003208 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003209 unsigned NewDirection = Dependence::DVEntry::NONE;
3210 if (!isKnownPredicate(CmpInst::ICMP_NE,
3211 CurConstraint.getY(),
3212 CurConstraint.getX()))
3213 // if X may be = Y
3214 NewDirection |= Dependence::DVEntry::EQ;
3215 if (!isKnownPredicate(CmpInst::ICMP_SLE,
3216 CurConstraint.getY(),
3217 CurConstraint.getX()))
3218 // if Y may be > X
3219 NewDirection |= Dependence::DVEntry::LT;
3220 if (!isKnownPredicate(CmpInst::ICMP_SGE,
3221 CurConstraint.getY(),
3222 CurConstraint.getX()))
3223 // if Y may be < X
3224 NewDirection |= Dependence::DVEntry::GT;
3225 Level.Direction &= NewDirection;
3226 }
3227 else
3228 llvm_unreachable("constraint has unexpected kind");
3229}
3230
Sebastian Popc62c6792013-11-12 22:47:20 +00003231/// Check if we can delinearize the subscripts. If the SCEVs representing the
3232/// source and destination array references are recurrences on a nested loop,
Alp Tokercb402912014-01-24 17:20:08 +00003233/// this function flattens the nested recurrences into separate recurrences
Sebastian Popc62c6792013-11-12 22:47:20 +00003234/// for each loop level.
Hal Finkel0ef2b102015-08-19 02:56:36 +00003235bool DependenceAnalysis::tryDelinearize(Instruction *Src,
3236 Instruction *Dst,
3237 SmallVectorImpl<Subscript> &Pair)
3238{
3239 Value *SrcPtr = getPointerOperand(Src);
3240 Value *DstPtr = getPointerOperand(Dst);
3241
3242 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3243 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3244
3245 // Below code mimics the code in Delinearization.cpp
3246 const SCEV *SrcAccessFn =
3247 SE->getSCEVAtScope(SrcPtr, SrcLoop);
3248 const SCEV *DstAccessFn =
3249 SE->getSCEVAtScope(DstPtr, DstLoop);
3250
Sebastian Pop28e6b972014-05-27 22:41:51 +00003251 const SCEVUnknown *SrcBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003252 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003253 const SCEVUnknown *DstBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003254 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003255
3256 if (!SrcBase || !DstBase || SrcBase != DstBase)
3257 return false;
3258
Hal Finkel0ef2b102015-08-19 02:56:36 +00003259 const SCEV *ElementSize = SE->getElementSize(Src);
3260 if (ElementSize != SE->getElementSize(Dst))
3261 return false;
3262
3263 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3264 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
Sebastian Pop28e6b972014-05-27 22:41:51 +00003265
Sebastian Popc62c6792013-11-12 22:47:20 +00003266 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3267 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3268 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3269 return false;
3270
Sebastian Pop448712b2014-05-07 18:01:20 +00003271 // First step: collect parametric terms in both array references.
3272 SmallVector<const SCEV *, 4> Terms;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003273 SE->collectParametricTerms(SrcAR, Terms);
3274 SE->collectParametricTerms(DstAR, Terms);
Sebastian Popc62c6792013-11-12 22:47:20 +00003275
Sebastian Pop448712b2014-05-07 18:01:20 +00003276 // Second step: find subscript sizes.
3277 SmallVector<const SCEV *, 4> Sizes;
Sebastian Popa6e58602014-05-27 22:41:45 +00003278 SE->findArrayDimensions(Terms, Sizes, ElementSize);
Sebastian Pop448712b2014-05-07 18:01:20 +00003279
3280 // Third step: compute the access functions for each subscript.
3281 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003282 SE->computeAccessFunctions(SrcAR, SrcSubscripts, Sizes);
3283 SE->computeAccessFunctions(DstAR, DstSubscripts, Sizes);
Sebastian Pop448712b2014-05-07 18:01:20 +00003284
Sebastian Pop5133d2e2014-02-21 18:15:07 +00003285 // Fail when there is only a subscript: that's a linearized access function.
Sebastian Pop448712b2014-05-07 18:01:20 +00003286 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3287 SrcSubscripts.size() != DstSubscripts.size())
Sebastian Popc62c6792013-11-12 22:47:20 +00003288 return false;
3289
Sebastian Pop448712b2014-05-07 18:01:20 +00003290 int size = SrcSubscripts.size();
Sebastian Pop29026d32014-02-21 18:15:11 +00003291
Sebastian Pop448712b2014-05-07 18:01:20 +00003292 DEBUG({
3293 dbgs() << "\nSrcSubscripts: ";
3294 for (int i = 0; i < size; i++)
3295 dbgs() << *SrcSubscripts[i];
3296 dbgs() << "\nDstSubscripts: ";
3297 for (int i = 0; i < size; i++)
3298 dbgs() << *DstSubscripts[i];
3299 });
Sebastian Popc62c6792013-11-12 22:47:20 +00003300
Sebastian Pop7ee14722013-11-13 22:37:58 +00003301 // The delinearization transforms a single-subscript MIV dependence test into
3302 // a multi-subscript SIV dependence test that is easier to compute. So we
3303 // resize Pair to contain as many pairs of subscripts as the delinearization
3304 // has found, and then initialize the pairs following the delinearization.
Sebastian Popc62c6792013-11-12 22:47:20 +00003305 Pair.resize(size);
3306 for (int i = 0; i < size; ++i) {
3307 Pair[i].Src = SrcSubscripts[i];
3308 Pair[i].Dst = DstSubscripts[i];
Jingyue Wu0fa125a2014-11-16 16:52:44 +00003309 unifySubscriptType(&Pair[i]);
Sebastian Pop7ee14722013-11-13 22:37:58 +00003310
3311 // FIXME: we should record the bounds SrcSizes[i] and DstSizes[i] that the
3312 // delinearization has found, and add these constraints to the dependence
3313 // check to avoid memory accesses overflow from one dimension into another.
3314 // This is related to the problem of determining the existence of data
3315 // dependences in array accesses using a different number of subscripts: in
3316 // C one can access an array A[100][100]; as A[0][9999], *A[9999], etc.
Sebastian Popc62c6792013-11-12 22:47:20 +00003317 }
3318
3319 return true;
3320}
Sebastian Pop59b61b92012-10-11 07:32:34 +00003321
3322//===----------------------------------------------------------------------===//
3323
3324#ifndef NDEBUG
3325// For debugging purposes, dump a small bit vector to dbgs().
3326static void dumpSmallBitVector(SmallBitVector &BV) {
3327 dbgs() << "{";
3328 for (int VI = BV.find_first(); VI >= 0; VI = BV.find_next(VI)) {
3329 dbgs() << VI;
3330 if (BV.find_next(VI) >= 0)
3331 dbgs() << ' ';
3332 }
3333 dbgs() << "}\n";
3334}
3335#endif
3336
Sebastian Pop59b61b92012-10-11 07:32:34 +00003337// depends -
3338// Returns NULL if there is no dependence.
3339// Otherwise, return a Dependence with as many details as possible.
3340// Corresponds to Section 3.1 in the paper
3341//
3342// Practical Dependence Testing
3343// Goff, Kennedy, Tseng
3344// PLDI 1991
3345//
Preston Briggs3ad39492012-11-21 23:50:04 +00003346// Care is required to keep the routine below, getSplitIteration(),
3347// up to date with respect to this routine.
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003348std::unique_ptr<Dependence>
3349DependenceAnalysis::depends(Instruction *Src, Instruction *Dst,
3350 bool PossiblyLoopIndependent) {
Preston Briggs1084fa22012-11-27 06:41:46 +00003351 if (Src == Dst)
3352 PossiblyLoopIndependent = false;
3353
Sebastian Pop59b61b92012-10-11 07:32:34 +00003354 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) ||
3355 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory()))
3356 // if both instructions don't reference memory, there's no dependence
Craig Topper9f008862014-04-15 04:59:12 +00003357 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003358
Preston Briggs3ad39492012-11-21 23:50:04 +00003359 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003360 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
Preston Briggs3ad39492012-11-21 23:50:04 +00003361 DEBUG(dbgs() << "can only handle simple loads and stores\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003362 return make_unique<Dependence>(Src, Dst);
Preston Briggs3ad39492012-11-21 23:50:04 +00003363 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003364
Sebastian Pop87ce43c2012-11-20 22:28:04 +00003365 Value *SrcPtr = getPointerOperand(Src);
3366 Value *DstPtr = getPointerOperand(Dst);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003367
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003368 switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(), DstPtr,
3369 SrcPtr)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003370 case MayAlias:
3371 case PartialAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003372 // cannot analyse objects if we don't understand their aliasing.
Preston Briggs3ad39492012-11-21 23:50:04 +00003373 DEBUG(dbgs() << "can't analyze may or partial alias\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003374 return make_unique<Dependence>(Src, Dst);
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003375 case NoAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003376 // If the objects noalias, they are distinct, accesses are independent.
Preston Briggs3ad39492012-11-21 23:50:04 +00003377 DEBUG(dbgs() << "no alias\n");
Craig Topper9f008862014-04-15 04:59:12 +00003378 return nullptr;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003379 case MustAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003380 break; // The underlying objects alias; test accesses for dependence.
3381 }
3382
Sebastian Pop59b61b92012-10-11 07:32:34 +00003383 // establish loop nesting levels
3384 establishNestingLevels(Src, Dst);
3385 DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3386 DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3387
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003388 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003389 ++TotalArrayPairs;
3390
Preston Briggs3ad39492012-11-21 23:50:04 +00003391 // See if there are GEPs we can use.
3392 bool UsefulGEP = false;
3393 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr);
3394 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr);
3395 if (SrcGEP && DstGEP &&
3396 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) {
3397 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand());
3398 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand());
3399 DEBUG(dbgs() << " SrcPtrSCEV = " << *SrcPtrSCEV << "\n");
3400 DEBUG(dbgs() << " DstPtrSCEV = " << *DstPtrSCEV << "\n");
3401
Karthik Bhat8d0099b2015-03-10 13:31:03 +00003402 UsefulGEP = isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) &&
3403 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())) &&
3404 (SrcGEP->getNumOperands() == DstGEP->getNumOperands());
Sebastian Pop59b61b92012-10-11 07:32:34 +00003405 }
Preston Briggs3ad39492012-11-21 23:50:04 +00003406 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1;
3407 SmallVector<Subscript, 4> Pair(Pairs);
3408 if (UsefulGEP) {
3409 DEBUG(dbgs() << " using GEPs\n");
3410 unsigned P = 0;
3411 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(),
3412 SrcEnd = SrcGEP->idx_end(),
3413 DstIdx = DstGEP->idx_begin();
3414 SrcIdx != SrcEnd;
3415 ++SrcIdx, ++DstIdx, ++P) {
3416 Pair[P].Src = SE->getSCEV(*SrcIdx);
3417 Pair[P].Dst = SE->getSCEV(*DstIdx);
Jingyue Wu0fa125a2014-11-16 16:52:44 +00003418 unifySubscriptType(&Pair[P]);
Preston Briggs3ad39492012-11-21 23:50:04 +00003419 }
3420 }
3421 else {
3422 DEBUG(dbgs() << " ignoring GEPs\n");
3423 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3424 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3425 DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3426 DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3427 Pair[0].Src = SrcSCEV;
3428 Pair[0].Dst = DstSCEV;
3429 }
3430
Hal Finkel0ef2b102015-08-19 02:56:36 +00003431 if (Delinearize && CommonLevels > 1) {
3432 if (tryDelinearize(Src, Dst, Pair)) {
3433 DEBUG(dbgs() << " delinerized GEP\n");
3434 Pairs = Pair.size();
3435 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003436 }
3437
Preston Briggs3ad39492012-11-21 23:50:04 +00003438 for (unsigned P = 0; P < Pairs; ++P) {
3439 Pair[P].Loops.resize(MaxLevels + 1);
3440 Pair[P].GroupLoops.resize(MaxLevels + 1);
3441 Pair[P].Group.resize(Pairs);
3442 removeMatchingExtensions(&Pair[P]);
3443 Pair[P].Classification =
3444 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3445 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3446 Pair[P].Loops);
3447 Pair[P].GroupLoops = Pair[P].Loops;
3448 Pair[P].Group.set(P);
3449 DEBUG(dbgs() << " subscript " << P << "\n");
3450 DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3451 DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3452 DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003453 DEBUG(dbgs() << "\tloops = ");
Preston Briggs3ad39492012-11-21 23:50:04 +00003454 DEBUG(dumpSmallBitVector(Pair[P].Loops));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003455 }
3456
3457 SmallBitVector Separable(Pairs);
3458 SmallBitVector Coupled(Pairs);
3459
3460 // Partition subscripts into separable and minimally-coupled groups
3461 // Algorithm in paper is algorithmically better;
3462 // this may be faster in practice. Check someday.
3463 //
3464 // Here's an example of how it works. Consider this code:
3465 //
3466 // for (i = ...) {
3467 // for (j = ...) {
3468 // for (k = ...) {
3469 // for (l = ...) {
3470 // for (m = ...) {
3471 // A[i][j][k][m] = ...;
3472 // ... = A[0][j][l][i + j];
3473 // }
3474 // }
3475 // }
3476 // }
3477 // }
3478 //
3479 // There are 4 subscripts here:
3480 // 0 [i] and [0]
3481 // 1 [j] and [j]
3482 // 2 [k] and [l]
3483 // 3 [m] and [i + j]
3484 //
3485 // We've already classified each subscript pair as ZIV, SIV, etc.,
3486 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3487 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3488 // and set Pair[P].Group = {P}.
3489 //
3490 // Src Dst Classification Loops GroupLoops Group
3491 // 0 [i] [0] SIV {1} {1} {0}
3492 // 1 [j] [j] SIV {2} {2} {1}
3493 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3494 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3495 //
3496 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3497 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3498 //
3499 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3500 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3501 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3502 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3503 // to either Separable or Coupled).
3504 //
3505 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3506 // Next, 1 and 3. The intersectionof their GroupLoops = {2}, not empty,
3507 // so Pair[3].Group = {0, 1, 3} and Done = false.
3508 //
3509 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3510 // Since Done remains true, we add 2 to the set of Separable pairs.
3511 //
3512 // Finally, we consider 3. There's nothing to compare it with,
3513 // so Done remains true and we add it to the Coupled set.
3514 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3515 //
3516 // In the end, we've got 1 separable subscript and 1 coupled group.
3517 for (unsigned SI = 0; SI < Pairs; ++SI) {
3518 if (Pair[SI].Classification == Subscript::NonLinear) {
3519 // ignore these, but collect loops for later
3520 ++NonlinearSubscriptPairs;
3521 collectCommonLoops(Pair[SI].Src,
3522 LI->getLoopFor(Src->getParent()),
3523 Pair[SI].Loops);
3524 collectCommonLoops(Pair[SI].Dst,
3525 LI->getLoopFor(Dst->getParent()),
3526 Pair[SI].Loops);
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003527 Result.Consistent = false;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003528 } else if (Pair[SI].Classification == Subscript::ZIV) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003529 // always separable
3530 Separable.set(SI);
3531 }
3532 else {
3533 // SIV, RDIV, or MIV, so check for coupled group
3534 bool Done = true;
3535 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3536 SmallBitVector Intersection = Pair[SI].GroupLoops;
3537 Intersection &= Pair[SJ].GroupLoops;
3538 if (Intersection.any()) {
3539 // accumulate set of all the loops in group
3540 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3541 // accumulate set of all subscripts in group
3542 Pair[SJ].Group |= Pair[SI].Group;
3543 Done = false;
3544 }
3545 }
3546 if (Done) {
3547 if (Pair[SI].Group.count() == 1) {
3548 Separable.set(SI);
3549 ++SeparableSubscriptPairs;
3550 }
3551 else {
3552 Coupled.set(SI);
3553 ++CoupledSubscriptPairs;
3554 }
3555 }
3556 }
3557 }
3558
3559 DEBUG(dbgs() << " Separable = ");
3560 DEBUG(dumpSmallBitVector(Separable));
3561 DEBUG(dbgs() << " Coupled = ");
3562 DEBUG(dumpSmallBitVector(Coupled));
3563
3564 Constraint NewConstraint;
3565 NewConstraint.setAny(SE);
3566
3567 // test separable subscripts
3568 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) {
3569 DEBUG(dbgs() << "testing subscript " << SI);
3570 switch (Pair[SI].Classification) {
3571 case Subscript::ZIV:
3572 DEBUG(dbgs() << ", ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003573 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003574 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003575 break;
3576 case Subscript::SIV: {
3577 DEBUG(dbgs() << ", SIV\n");
3578 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003579 const SCEV *SplitIter = nullptr;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003580 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
3581 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003582 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003583 break;
3584 }
3585 case Subscript::RDIV:
3586 DEBUG(dbgs() << ", RDIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003587 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003588 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003589 break;
3590 case Subscript::MIV:
3591 DEBUG(dbgs() << ", MIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003592 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003593 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003594 break;
3595 default:
3596 llvm_unreachable("subscript has unexpected classification");
3597 }
3598 }
3599
3600 if (Coupled.count()) {
3601 // test coupled subscript groups
3602 DEBUG(dbgs() << "starting on coupled subscripts\n");
3603 DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
3604 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3605 for (unsigned II = 0; II <= MaxLevels; ++II)
3606 Constraints[II].setAny(SE);
3607 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) {
3608 DEBUG(dbgs() << "testing subscript group " << SI << " { ");
3609 SmallBitVector Group(Pair[SI].Group);
3610 SmallBitVector Sivs(Pairs);
3611 SmallBitVector Mivs(Pairs);
3612 SmallBitVector ConstrainedLevels(MaxLevels + 1);
Jingyue Wua84feb12015-05-29 16:58:08 +00003613 SmallVector<Subscript *, 4> PairsInGroup;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003614 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) {
3615 DEBUG(dbgs() << SJ << " ");
3616 if (Pair[SJ].Classification == Subscript::SIV)
3617 Sivs.set(SJ);
3618 else
3619 Mivs.set(SJ);
Jingyue Wua84feb12015-05-29 16:58:08 +00003620 PairsInGroup.push_back(&Pair[SJ]);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003621 }
Jingyue Wua84feb12015-05-29 16:58:08 +00003622 unifySubscriptType(PairsInGroup);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003623 DEBUG(dbgs() << "}\n");
3624 while (Sivs.any()) {
3625 bool Changed = false;
3626 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) {
3627 DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
3628 // SJ is an SIV subscript that's part of the current coupled group
3629 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003630 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003631 DEBUG(dbgs() << "SIV\n");
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003632 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
3633 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003634 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003635 ConstrainedLevels.set(Level);
3636 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3637 if (Constraints[Level].isEmpty()) {
3638 ++DeltaIndependence;
Craig Topper9f008862014-04-15 04:59:12 +00003639 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003640 }
3641 Changed = true;
3642 }
3643 Sivs.reset(SJ);
3644 }
3645 if (Changed) {
3646 // propagate, possibly creating new SIVs and ZIVs
3647 DEBUG(dbgs() << " propagating\n");
3648 DEBUG(dbgs() << "\tMivs = ");
3649 DEBUG(dumpSmallBitVector(Mivs));
3650 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3651 // SJ is an MIV subscript that's part of the current coupled group
3652 DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
3653 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003654 Constraints, Result.Consistent)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003655 DEBUG(dbgs() << "\t Changed\n");
3656 ++DeltaPropagations;
3657 Pair[SJ].Classification =
3658 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3659 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3660 Pair[SJ].Loops);
3661 switch (Pair[SJ].Classification) {
3662 case Subscript::ZIV:
3663 DEBUG(dbgs() << "ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003664 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003665 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003666 Mivs.reset(SJ);
3667 break;
3668 case Subscript::SIV:
3669 Sivs.set(SJ);
3670 Mivs.reset(SJ);
3671 break;
3672 case Subscript::RDIV:
3673 case Subscript::MIV:
3674 break;
3675 default:
3676 llvm_unreachable("bad subscript classification");
3677 }
3678 }
3679 }
3680 }
3681 }
3682
3683 // test & propagate remaining RDIVs
3684 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3685 if (Pair[SJ].Classification == Subscript::RDIV) {
3686 DEBUG(dbgs() << "RDIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003687 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003688 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003689 // I don't yet understand how to propagate RDIV results
3690 Mivs.reset(SJ);
3691 }
3692 }
3693
3694 // test remaining MIVs
3695 // This code is temporary.
3696 // Better to somehow test all remaining subscripts simultaneously.
3697 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3698 if (Pair[SJ].Classification == Subscript::MIV) {
3699 DEBUG(dbgs() << "MIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003700 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003701 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003702 }
3703 else
3704 llvm_unreachable("expected only MIV subscripts at this point");
3705 }
3706
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003707 // update Result.DV from constraint vector
Sebastian Pop59b61b92012-10-11 07:32:34 +00003708 DEBUG(dbgs() << " updating\n");
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003709 for (int SJ = ConstrainedLevels.find_first(); SJ >= 0;
3710 SJ = ConstrainedLevels.find_next(SJ)) {
Karthik Bhat8d7f7ed2015-03-10 14:32:02 +00003711 if (SJ > (int)CommonLevels)
3712 break;
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003713 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3714 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
Craig Topper9f008862014-04-15 04:59:12 +00003715 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003716 }
3717 }
3718 }
3719
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003720 // Make sure the Scalar flags are set correctly.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003721 SmallBitVector CompleteLoops(MaxLevels + 1);
3722 for (unsigned SI = 0; SI < Pairs; ++SI)
3723 CompleteLoops |= Pair[SI].Loops;
3724 for (unsigned II = 1; II <= CommonLevels; ++II)
3725 if (CompleteLoops[II])
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003726 Result.DV[II - 1].Scalar = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003727
Sebastian Pop59b61b92012-10-11 07:32:34 +00003728 if (PossiblyLoopIndependent) {
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003729 // Make sure the LoopIndependent flag is set correctly.
3730 // All directions must include equal, otherwise no
3731 // loop-independent dependence is possible.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003732 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003733 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3734 Result.LoopIndependent = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003735 break;
3736 }
3737 }
3738 }
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003739 else {
3740 // On the other hand, if all directions are equal and there's no
3741 // loop-independent dependence possible, then no dependence exists.
3742 bool AllEqual = true;
3743 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003744 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003745 AllEqual = false;
3746 break;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003747 }
3748 }
3749 if (AllEqual)
Craig Topper9f008862014-04-15 04:59:12 +00003750 return nullptr;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003751 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003752
David Blaikie47039dc2015-07-31 21:37:09 +00003753 return make_unique<FullDependence>(std::move(Result));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003754}
3755
3756
3757
3758//===----------------------------------------------------------------------===//
3759// getSplitIteration -
3760// Rather than spend rarely-used space recording the splitting iteration
3761// during the Weak-Crossing SIV test, we re-compute it on demand.
3762// The re-computation is basically a repeat of the entire dependence test,
3763// though simplified since we know that the dependence exists.
3764// It's tedious, since we must go through all propagations, etc.
3765//
Preston Briggs3ad39492012-11-21 23:50:04 +00003766// Care is required to keep this code up to date with respect to the routine
3767// above, depends().
Sebastian Pop59b61b92012-10-11 07:32:34 +00003768//
3769// Generally, the dependence analyzer will be used to build
3770// a dependence graph for a function (basically a map from instructions
3771// to dependences). Looking for cycles in the graph shows us loops
3772// that cannot be trivially vectorized/parallelized.
3773//
3774// We can try to improve the situation by examining all the dependences
3775// that make up the cycle, looking for ones we can break.
3776// Sometimes, peeling the first or last iteration of a loop will break
3777// dependences, and we've got flags for those possibilities.
3778// Sometimes, splitting a loop at some other iteration will do the trick,
3779// and we've got a flag for that case. Rather than waste the space to
3780// record the exact iteration (since we rarely know), we provide
3781// a method that calculates the iteration. It's a drag that it must work
3782// from scratch, but wonderful in that it's possible.
3783//
3784// Here's an example:
3785//
3786// for (i = 0; i < 10; i++)
3787// A[i] = ...
3788// ... = A[11 - i]
3789//
3790// There's a loop-carried flow dependence from the store to the load,
3791// found by the weak-crossing SIV test. The dependence will have a flag,
3792// indicating that the dependence can be broken by splitting the loop.
3793// Calling getSplitIteration will return 5.
3794// Splitting the loop breaks the dependence, like so:
3795//
3796// for (i = 0; i <= 5; i++)
3797// A[i] = ...
3798// ... = A[11 - i]
3799// for (i = 6; i < 10; i++)
3800// A[i] = ...
3801// ... = A[11 - i]
3802//
3803// breaks the dependence and allows us to vectorize/parallelize
3804// both loops.
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003805const SCEV *DependenceAnalysis::getSplitIteration(const Dependence &Dep,
Sebastian Pop59b61b92012-10-11 07:32:34 +00003806 unsigned SplitLevel) {
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003807 assert(Dep.isSplitable(SplitLevel) &&
Sebastian Pop59b61b92012-10-11 07:32:34 +00003808 "Dep should be splitable at SplitLevel");
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003809 Instruction *Src = Dep.getSrc();
3810 Instruction *Dst = Dep.getDst();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003811 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
3812 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
3813 assert(isLoadOrStore(Src));
3814 assert(isLoadOrStore(Dst));
Preston Briggs3ad39492012-11-21 23:50:04 +00003815 Value *SrcPtr = getPointerOperand(Src);
3816 Value *DstPtr = getPointerOperand(Dst);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003817 assert(underlyingObjectsAlias(AA, F->getParent()->getDataLayout(), DstPtr,
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003818 SrcPtr) == MustAlias);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003819
3820 // establish loop nesting levels
3821 establishNestingLevels(Src, Dst);
3822
3823 FullDependence Result(Src, Dst, false, CommonLevels);
3824
Preston Briggs3ad39492012-11-21 23:50:04 +00003825 // See if there are GEPs we can use.
3826 bool UsefulGEP = false;
3827 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr);
3828 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr);
3829 if (SrcGEP && DstGEP &&
3830 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) {
3831 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand());
3832 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand());
Karthik Bhat8d0099b2015-03-10 13:31:03 +00003833 UsefulGEP = isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) &&
3834 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())) &&
3835 (SrcGEP->getNumOperands() == DstGEP->getNumOperands());
Sebastian Pop59b61b92012-10-11 07:32:34 +00003836 }
Preston Briggs3ad39492012-11-21 23:50:04 +00003837 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1;
3838 SmallVector<Subscript, 4> Pair(Pairs);
3839 if (UsefulGEP) {
3840 unsigned P = 0;
3841 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(),
3842 SrcEnd = SrcGEP->idx_end(),
3843 DstIdx = DstGEP->idx_begin();
3844 SrcIdx != SrcEnd;
3845 ++SrcIdx, ++DstIdx, ++P) {
3846 Pair[P].Src = SE->getSCEV(*SrcIdx);
3847 Pair[P].Dst = SE->getSCEV(*DstIdx);
3848 }
3849 }
3850 else {
3851 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3852 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3853 Pair[0].Src = SrcSCEV;
3854 Pair[0].Dst = DstSCEV;
3855 }
3856
Hal Finkel0ef2b102015-08-19 02:56:36 +00003857 if (Delinearize && CommonLevels > 1) {
3858 if (tryDelinearize(Src, Dst, Pair)) {
3859 DEBUG(dbgs() << " delinerized GEP\n");
3860 Pairs = Pair.size();
3861 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003862 }
3863
Preston Briggs3ad39492012-11-21 23:50:04 +00003864 for (unsigned P = 0; P < Pairs; ++P) {
3865 Pair[P].Loops.resize(MaxLevels + 1);
3866 Pair[P].GroupLoops.resize(MaxLevels + 1);
3867 Pair[P].Group.resize(Pairs);
3868 removeMatchingExtensions(&Pair[P]);
3869 Pair[P].Classification =
3870 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3871 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3872 Pair[P].Loops);
3873 Pair[P].GroupLoops = Pair[P].Loops;
3874 Pair[P].Group.set(P);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003875 }
3876
3877 SmallBitVector Separable(Pairs);
3878 SmallBitVector Coupled(Pairs);
3879
3880 // partition subscripts into separable and minimally-coupled groups
3881 for (unsigned SI = 0; SI < Pairs; ++SI) {
3882 if (Pair[SI].Classification == Subscript::NonLinear) {
3883 // ignore these, but collect loops for later
3884 collectCommonLoops(Pair[SI].Src,
3885 LI->getLoopFor(Src->getParent()),
3886 Pair[SI].Loops);
3887 collectCommonLoops(Pair[SI].Dst,
3888 LI->getLoopFor(Dst->getParent()),
3889 Pair[SI].Loops);
3890 Result.Consistent = false;
3891 }
3892 else if (Pair[SI].Classification == Subscript::ZIV)
3893 Separable.set(SI);
3894 else {
3895 // SIV, RDIV, or MIV, so check for coupled group
3896 bool Done = true;
3897 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3898 SmallBitVector Intersection = Pair[SI].GroupLoops;
3899 Intersection &= Pair[SJ].GroupLoops;
3900 if (Intersection.any()) {
3901 // accumulate set of all the loops in group
3902 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3903 // accumulate set of all subscripts in group
3904 Pair[SJ].Group |= Pair[SI].Group;
3905 Done = false;
3906 }
3907 }
3908 if (Done) {
3909 if (Pair[SI].Group.count() == 1)
3910 Separable.set(SI);
3911 else
3912 Coupled.set(SI);
3913 }
3914 }
3915 }
3916
3917 Constraint NewConstraint;
3918 NewConstraint.setAny(SE);
3919
3920 // test separable subscripts
3921 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) {
3922 switch (Pair[SI].Classification) {
3923 case Subscript::SIV: {
3924 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003925 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003926 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
3927 Result, NewConstraint, SplitIter);
3928 if (Level == SplitLevel) {
Craig Topper9f008862014-04-15 04:59:12 +00003929 assert(SplitIter != nullptr);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003930 return SplitIter;
3931 }
3932 break;
3933 }
3934 case Subscript::ZIV:
3935 case Subscript::RDIV:
3936 case Subscript::MIV:
3937 break;
3938 default:
3939 llvm_unreachable("subscript has unexpected classification");
3940 }
3941 }
3942
3943 if (Coupled.count()) {
3944 // test coupled subscript groups
3945 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3946 for (unsigned II = 0; II <= MaxLevels; ++II)
3947 Constraints[II].setAny(SE);
3948 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) {
3949 SmallBitVector Group(Pair[SI].Group);
3950 SmallBitVector Sivs(Pairs);
3951 SmallBitVector Mivs(Pairs);
3952 SmallBitVector ConstrainedLevels(MaxLevels + 1);
3953 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) {
3954 if (Pair[SJ].Classification == Subscript::SIV)
3955 Sivs.set(SJ);
3956 else
3957 Mivs.set(SJ);
3958 }
3959 while (Sivs.any()) {
3960 bool Changed = false;
3961 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) {
3962 // SJ is an SIV subscript that's part of the current coupled group
3963 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003964 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003965 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
3966 Result, NewConstraint, SplitIter);
3967 if (Level == SplitLevel && SplitIter)
3968 return SplitIter;
3969 ConstrainedLevels.set(Level);
3970 if (intersectConstraints(&Constraints[Level], &NewConstraint))
3971 Changed = true;
3972 Sivs.reset(SJ);
3973 }
3974 if (Changed) {
3975 // propagate, possibly creating new SIVs and ZIVs
3976 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3977 // SJ is an MIV subscript that's part of the current coupled group
3978 if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
3979 Pair[SJ].Loops, Constraints, Result.Consistent)) {
3980 Pair[SJ].Classification =
3981 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3982 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3983 Pair[SJ].Loops);
3984 switch (Pair[SJ].Classification) {
3985 case Subscript::ZIV:
3986 Mivs.reset(SJ);
3987 break;
3988 case Subscript::SIV:
3989 Sivs.set(SJ);
3990 Mivs.reset(SJ);
3991 break;
3992 case Subscript::RDIV:
3993 case Subscript::MIV:
3994 break;
3995 default:
3996 llvm_unreachable("bad subscript classification");
3997 }
3998 }
3999 }
4000 }
4001 }
4002 }
4003 }
4004 llvm_unreachable("somehow reached end of routine");
Craig Topper9f008862014-04-15 04:59:12 +00004005 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00004006}