blob: 8337b9b96ea23b53a600b953bac54edc9d68b81f [file] [log] [blame]
Sebastian Pop59b61b92012-10-11 07:32:34 +00001//===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sebastian Pop59b61b92012-10-11 07:32:34 +00006//
7//===----------------------------------------------------------------------===//
8//
9// DependenceAnalysis is an LLVM pass that analyses dependences between memory
10// accesses. Currently, it is an (incomplete) implementation of the approach
11// described in
12//
13// Practical Dependence Testing
14// Goff, Kennedy, Tseng
15// PLDI 1991
16//
17// There's a single entry point that analyzes the dependence between a pair
18// of memory references in a function, returning either NULL, for no dependence,
19// or a more-or-less detailed description of the dependence between them.
20//
21// Currently, the implementation cannot propagate constraints between
22// coupled RDIV subscripts and lacks a multi-subscript MIV test.
23// Both of these are conservative weaknesses;
24// that is, not a source of correctness problems.
25//
Sebastian Popbf6e1c22018-03-06 21:55:59 +000026// Since Clang linearizes some array subscripts, the dependence
Sebastian Pop7ee14722013-11-13 22:37:58 +000027// analysis is using SCEV->delinearize to recover the representation of multiple
28// subscripts, and thus avoid the more expensive and less precise MIV tests. The
29// delinearization is controlled by the flag -da-delinearize.
Sebastian Pop59b61b92012-10-11 07:32:34 +000030//
31// We should pay some careful attention to the possibility of integer overflow
32// in the implementation of the various tests. This could happen with Add,
33// Subtract, or Multiply, with both APInt's and SCEV's.
34//
35// Some non-linear subscript pairs can be handled by the GCD test
36// (and perhaps other tests).
37// Should explore how often these things occur.
38//
39// Finally, it seems like certain test cases expose weaknesses in the SCEV
40// simplification, especially in the handling of sign and zero extensions.
41// It could be useful to spend time exploring these.
42//
43// Please note that this is work in progress and the interface is subject to
44// change.
45//
46//===----------------------------------------------------------------------===//
47// //
48// In memory of Ken Kennedy, 1945 - 2007 //
49// //
50//===----------------------------------------------------------------------===//
51
Sebastian Pop59b61b92012-10-11 07:32:34 +000052#include "llvm/Analysis/DependenceAnalysis.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000053#include "llvm/ADT/STLExtras.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000054#include "llvm/ADT/Statistic.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000055#include "llvm/Analysis/AliasAnalysis.h"
56#include "llvm/Analysis/LoopInfo.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000057#include "llvm/Analysis/ScalarEvolution.h"
58#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000059#include "llvm/Analysis/ValueTracking.h"
Nico Weber432a3882018-04-30 14:59:11 +000060#include "llvm/Config/llvm-config.h"
Chandler Carruth83948572014-03-04 10:30:26 +000061#include "llvm/IR/InstIterator.h"
Mehdi Aminia28d91d2015-03-10 02:37:25 +000062#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000063#include "llvm/IR/Operator.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000064#include "llvm/Support/CommandLine.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000065#include "llvm/Support/Debug.h"
66#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer71a35122012-10-25 16:15:22 +000067#include "llvm/Support/raw_ostream.h"
Sebastian Pop59b61b92012-10-11 07:32:34 +000068
69using namespace llvm;
70
Chandler Carruthf1221bd2014-04-22 02:48:03 +000071#define DEBUG_TYPE "da"
72
Sebastian Pop59b61b92012-10-11 07:32:34 +000073//===----------------------------------------------------------------------===//
74// statistics
75
76STATISTIC(TotalArrayPairs, "Array pairs tested");
77STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
78STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
79STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
80STATISTIC(ZIVapplications, "ZIV applications");
81STATISTIC(ZIVindependence, "ZIV independence");
82STATISTIC(StrongSIVapplications, "Strong SIV applications");
83STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
84STATISTIC(StrongSIVindependence, "Strong SIV independence");
85STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
86STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
87STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
88STATISTIC(ExactSIVapplications, "Exact SIV applications");
89STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
90STATISTIC(ExactSIVindependence, "Exact SIV independence");
91STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
92STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
93STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
94STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
95STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
96STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
97STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
98STATISTIC(DeltaApplications, "Delta applications");
99STATISTIC(DeltaSuccesses, "Delta successes");
100STATISTIC(DeltaIndependence, "Delta independence");
101STATISTIC(DeltaPropagations, "Delta propagations");
102STATISTIC(GCDapplications, "GCD applications");
103STATISTIC(GCDsuccesses, "GCD successes");
104STATISTIC(GCDindependence, "GCD independence");
105STATISTIC(BanerjeeApplications, "Banerjee applications");
106STATISTIC(BanerjeeIndependence, "Banerjee independence");
107STATISTIC(BanerjeeSuccesses, "Banerjee successes");
108
Sebastian Popc62c6792013-11-12 22:47:20 +0000109static cl::opt<bool>
David Greend143c652018-06-21 11:53:16 +0000110 Delinearize("da-delinearize", cl::init(true), cl::Hidden, cl::ZeroOrMore,
111 cl::desc("Try to delinearize array references."));
Sebastian Popc62c6792013-11-12 22:47:20 +0000112
Sebastian Pop59b61b92012-10-11 07:32:34 +0000113//===----------------------------------------------------------------------===//
114// basics
115
Chandler Carruth49c22192016-05-12 22:19:39 +0000116DependenceAnalysis::Result
117DependenceAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
118 auto &AA = FAM.getResult<AAManager>(F);
119 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
120 auto &LI = FAM.getResult<LoopAnalysis>(F);
121 return DependenceInfo(&F, &AA, &SE, &LI);
122}
123
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000124AnalysisKey DependenceAnalysis::Key;
Chandler Carruth49c22192016-05-12 22:19:39 +0000125
126INITIALIZE_PASS_BEGIN(DependenceAnalysisWrapperPass, "da",
Sebastian Pop59b61b92012-10-11 07:32:34 +0000127 "Dependence Analysis", true, true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000128INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000129INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000130INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Chandler Carruth49c22192016-05-12 22:19:39 +0000131INITIALIZE_PASS_END(DependenceAnalysisWrapperPass, "da", "Dependence Analysis",
132 true, true)
Sebastian Pop59b61b92012-10-11 07:32:34 +0000133
Chandler Carruth49c22192016-05-12 22:19:39 +0000134char DependenceAnalysisWrapperPass::ID = 0;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000135
Chandler Carruth49c22192016-05-12 22:19:39 +0000136FunctionPass *llvm::createDependenceAnalysisWrapperPass() {
137 return new DependenceAnalysisWrapperPass();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000138}
139
Chandler Carruth49c22192016-05-12 22:19:39 +0000140bool DependenceAnalysisWrapperPass::runOnFunction(Function &F) {
141 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
142 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
143 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
144 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
Sebastian Pop59b61b92012-10-11 07:32:34 +0000145 return false;
146}
147
Chandler Carruth49c22192016-05-12 22:19:39 +0000148DependenceInfo &DependenceAnalysisWrapperPass::getDI() const { return *info; }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000149
Chandler Carruth49c22192016-05-12 22:19:39 +0000150void DependenceAnalysisWrapperPass::releaseMemory() { info.reset(); }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000151
Chandler Carruth49c22192016-05-12 22:19:39 +0000152void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000153 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000154 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000155 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000156 AU.addRequiredTransitive<LoopInfoWrapperPass>();
Sebastian Pop59b61b92012-10-11 07:32:34 +0000157}
158
159
160// Used to test the dependence analyzer.
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000161// Looks through the function, noting loads and stores.
162// Calls depends() on every possible pair and prints out the result.
Sebastian Pop59b61b92012-10-11 07:32:34 +0000163// Ignores all other instructions.
Chandler Carruth49c22192016-05-12 22:19:39 +0000164static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA) {
165 auto *F = DA->getFunction();
166 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
167 ++SrcI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000168 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000169 for (inst_iterator DstI = SrcI, DstE = inst_end(F);
170 DstI != DstE; ++DstI) {
Benjamin Kramer3eb15632012-11-13 12:12:02 +0000171 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000172 OS << "da analyze - ";
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +0000173 if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000174 D->dump(OS);
175 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
176 if (D->isSplitable(Level)) {
177 OS << "da analyze - split level = " << Level;
Dylan Noblesmithd96ce662014-08-25 00:28:35 +0000178 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
Sebastian Pop59b61b92012-10-11 07:32:34 +0000179 OS << "!\n";
180 }
181 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000182 }
183 else
184 OS << "none!\n";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000185 }
186 }
187 }
188 }
189}
190
Chandler Carruth49c22192016-05-12 22:19:39 +0000191void DependenceAnalysisWrapperPass::print(raw_ostream &OS,
192 const Module *) const {
193 dumpExampleDependence(OS, info.get());
Sebastian Pop59b61b92012-10-11 07:32:34 +0000194}
195
Philip Pfaffeefb5ad12019-01-08 14:06:58 +0000196PreservedAnalyses
197DependenceAnalysisPrinterPass::run(Function &F, FunctionAnalysisManager &FAM) {
198 OS << "'Dependence Analysis' for function '" << F.getName() << "':\n";
199 dumpExampleDependence(OS, &FAM.getResult<DependenceAnalysis>(F));
200 return PreservedAnalyses::all();
201}
202
Sebastian Pop59b61b92012-10-11 07:32:34 +0000203//===----------------------------------------------------------------------===//
204// Dependence methods
205
206// Returns true if this is an input dependence.
207bool Dependence::isInput() const {
208 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
209}
210
211
212// Returns true if this is an output dependence.
213bool Dependence::isOutput() const {
214 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
215}
216
217
218// Returns true if this is an flow (aka true) dependence.
219bool Dependence::isFlow() const {
220 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
221}
222
223
224// Returns true if this is an anti dependence.
225bool Dependence::isAnti() const {
226 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
227}
228
229
230// Returns true if a particular level is scalar; that is,
231// if no subscript in the source or destination mention the induction
232// variable associated with the loop at this level.
233// Leave this out of line, so it will serve as a virtual method anchor
234bool Dependence::isScalar(unsigned level) const {
235 return false;
236}
237
238
239//===----------------------------------------------------------------------===//
240// FullDependence methods
241
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000242FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
Sebastian Pop59b61b92012-10-11 07:32:34 +0000243 bool PossiblyLoopIndependent,
NAKAMURA Takumi478559a2015-03-05 01:25:19 +0000244 unsigned CommonLevels)
245 : Dependence(Source, Destination), Levels(CommonLevels),
246 LoopIndependent(PossiblyLoopIndependent) {
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000247 Consistent = true;
David Blaikie47039dc2015-07-31 21:37:09 +0000248 if (CommonLevels)
249 DV = make_unique<DVEntry[]>(CommonLevels);
NAKAMURA Takumie110d642015-03-05 01:25:06 +0000250}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000251
252// The rest are simple getters that hide the implementation.
253
254// getDirection - Returns the direction associated with a particular level.
255unsigned FullDependence::getDirection(unsigned Level) const {
256 assert(0 < Level && Level <= Levels && "Level out of range");
257 return DV[Level - 1].Direction;
258}
259
260
261// Returns the distance (or NULL) associated with a particular level.
262const SCEV *FullDependence::getDistance(unsigned Level) const {
263 assert(0 < Level && Level <= Levels && "Level out of range");
264 return DV[Level - 1].Distance;
265}
266
267
268// Returns true if a particular level is scalar; that is,
269// if no subscript in the source or destination mention the induction
270// variable associated with the loop at this level.
271bool FullDependence::isScalar(unsigned Level) const {
272 assert(0 < Level && Level <= Levels && "Level out of range");
273 return DV[Level - 1].Scalar;
274}
275
276
277// Returns true if peeling the first iteration from this loop
278// will break this dependence.
279bool FullDependence::isPeelFirst(unsigned Level) const {
280 assert(0 < Level && Level <= Levels && "Level out of range");
281 return DV[Level - 1].PeelFirst;
282}
283
284
285// Returns true if peeling the last iteration from this loop
286// will break this dependence.
287bool FullDependence::isPeelLast(unsigned Level) const {
288 assert(0 < Level && Level <= Levels && "Level out of range");
289 return DV[Level - 1].PeelLast;
290}
291
292
293// Returns true if splitting this loop will break the dependence.
294bool FullDependence::isSplitable(unsigned Level) const {
295 assert(0 < Level && Level <= Levels && "Level out of range");
296 return DV[Level - 1].Splitable;
297}
298
299
300//===----------------------------------------------------------------------===//
Chandler Carruth49c22192016-05-12 22:19:39 +0000301// DependenceInfo::Constraint methods
Sebastian Pop59b61b92012-10-11 07:32:34 +0000302
303// If constraint is a point <X, Y>, returns X.
304// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000305const SCEV *DependenceInfo::Constraint::getX() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000306 assert(Kind == Point && "Kind should be Point");
307 return A;
308}
309
310
311// If constraint is a point <X, Y>, returns Y.
312// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000313const SCEV *DependenceInfo::Constraint::getY() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000314 assert(Kind == Point && "Kind should be Point");
315 return B;
316}
317
318
319// If constraint is a line AX + BY = C, returns A.
320// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000321const SCEV *DependenceInfo::Constraint::getA() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000322 assert((Kind == Line || Kind == Distance) &&
323 "Kind should be Line (or Distance)");
324 return A;
325}
326
327
328// If constraint is a line AX + BY = C, returns B.
329// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000330const SCEV *DependenceInfo::Constraint::getB() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000331 assert((Kind == Line || Kind == Distance) &&
332 "Kind should be Line (or Distance)");
333 return B;
334}
335
336
337// If constraint is a line AX + BY = C, returns C.
338// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000339const SCEV *DependenceInfo::Constraint::getC() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000340 assert((Kind == Line || Kind == Distance) &&
341 "Kind should be Line (or Distance)");
342 return C;
343}
344
345
346// If constraint is a distance, returns D.
347// Otherwise assert.
Chandler Carruth49c22192016-05-12 22:19:39 +0000348const SCEV *DependenceInfo::Constraint::getD() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000349 assert(Kind == Distance && "Kind should be Distance");
350 return SE->getNegativeSCEV(C);
351}
352
353
354// Returns the loop associated with this constraint.
Chandler Carruth49c22192016-05-12 22:19:39 +0000355const Loop *DependenceInfo::Constraint::getAssociatedLoop() const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000356 assert((Kind == Distance || Kind == Line || Kind == Point) &&
357 "Kind should be Distance, Line, or Point");
358 return AssociatedLoop;
359}
360
Chandler Carruth49c22192016-05-12 22:19:39 +0000361void DependenceInfo::Constraint::setPoint(const SCEV *X, const SCEV *Y,
362 const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000363 Kind = Point;
364 A = X;
365 B = Y;
366 AssociatedLoop = CurLoop;
367}
368
Chandler Carruth49c22192016-05-12 22:19:39 +0000369void DependenceInfo::Constraint::setLine(const SCEV *AA, const SCEV *BB,
370 const SCEV *CC, const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000371 Kind = Line;
372 A = AA;
373 B = BB;
374 C = CC;
375 AssociatedLoop = CurLoop;
376}
377
Chandler Carruth49c22192016-05-12 22:19:39 +0000378void DependenceInfo::Constraint::setDistance(const SCEV *D,
379 const Loop *CurLoop) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000380 Kind = Distance;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000381 A = SE->getOne(D->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +0000382 B = SE->getNegativeSCEV(A);
383 C = SE->getNegativeSCEV(D);
384 AssociatedLoop = CurLoop;
385}
386
Chandler Carruth49c22192016-05-12 22:19:39 +0000387void DependenceInfo::Constraint::setEmpty() { Kind = Empty; }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000388
Chandler Carruth49c22192016-05-12 22:19:39 +0000389void DependenceInfo::Constraint::setAny(ScalarEvolution *NewSE) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000390 SE = NewSE;
391 Kind = Any;
392}
393
Aaron Ballman615eb472017-10-15 14:32:27 +0000394#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sebastian Pop59b61b92012-10-11 07:32:34 +0000395// For debugging purposes. Dumps the constraint out to OS.
Matthias Braun8c209aa2017-01-28 02:02:38 +0000396LLVM_DUMP_METHOD void DependenceInfo::Constraint::dump(raw_ostream &OS) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000397 if (isEmpty())
398 OS << " Empty\n";
399 else if (isAny())
400 OS << " Any\n";
401 else if (isPoint())
402 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
403 else if (isDistance())
404 OS << " Distance is " << *getD() <<
405 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
406 else if (isLine())
407 OS << " Line is " << *getA() << "*X + " <<
408 *getB() << "*Y = " << *getC() << "\n";
409 else
410 llvm_unreachable("unknown constraint type in Constraint::dump");
411}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000412#endif
Sebastian Pop59b61b92012-10-11 07:32:34 +0000413
414
415// Updates X with the intersection
416// of the Constraints X and Y. Returns true if X has changed.
417// Corresponds to Figure 4 from the paper
418//
419// Practical Dependence Testing
420// Goff, Kennedy, Tseng
421// PLDI 1991
Chandler Carruth49c22192016-05-12 22:19:39 +0000422bool DependenceInfo::intersectConstraints(Constraint *X, const Constraint *Y) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000423 ++DeltaApplications;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000424 LLVM_DEBUG(dbgs() << "\tintersect constraints\n");
425 LLVM_DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
426 LLVM_DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
Sebastian Pop59b61b92012-10-11 07:32:34 +0000427 assert(!Y->isPoint() && "Y must not be a Point");
428 if (X->isAny()) {
429 if (Y->isAny())
430 return false;
431 *X = *Y;
432 return true;
433 }
434 if (X->isEmpty())
435 return false;
436 if (Y->isEmpty()) {
437 X->setEmpty();
438 return true;
439 }
440
441 if (X->isDistance() && Y->isDistance()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000442 LLVM_DEBUG(dbgs() << "\t intersect 2 distances\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000443 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
444 return false;
445 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
446 X->setEmpty();
447 ++DeltaSuccesses;
448 return true;
449 }
450 // Hmmm, interesting situation.
451 // I guess if either is constant, keep it and ignore the other.
452 if (isa<SCEVConstant>(Y->getD())) {
453 *X = *Y;
454 return true;
455 }
456 return false;
457 }
458
459 // At this point, the pseudo-code in Figure 4 of the paper
460 // checks if (X->isPoint() && Y->isPoint()).
461 // This case can't occur in our implementation,
462 // since a Point can only arise as the result of intersecting
463 // two Line constraints, and the right-hand value, Y, is never
464 // the result of an intersection.
465 assert(!(X->isPoint() && Y->isPoint()) &&
466 "We shouldn't ever see X->isPoint() && Y->isPoint()");
467
468 if (X->isLine() && Y->isLine()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000469 LLVM_DEBUG(dbgs() << "\t intersect 2 lines\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000470 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
471 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
472 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
473 // slopes are equal, so lines are parallel
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000474 LLVM_DEBUG(dbgs() << "\t\tsame slope\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000475 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
476 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
477 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
478 return false;
479 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
480 X->setEmpty();
481 ++DeltaSuccesses;
482 return true;
483 }
484 return false;
485 }
486 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
487 // slopes differ, so lines intersect
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000488 LLVM_DEBUG(dbgs() << "\t\tdifferent slopes\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000489 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
490 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
491 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
492 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
493 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
494 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
495 const SCEVConstant *C1A2_C2A1 =
496 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
497 const SCEVConstant *C1B2_C2B1 =
498 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
499 const SCEVConstant *A1B2_A2B1 =
500 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
501 const SCEVConstant *A2B1_A1B2 =
502 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
503 if (!C1B2_C2B1 || !C1A2_C2A1 ||
504 !A1B2_A2B1 || !A2B1_A1B2)
505 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000506 APInt Xtop = C1B2_C2B1->getAPInt();
507 APInt Xbot = A1B2_A2B1->getAPInt();
508 APInt Ytop = C1A2_C2A1->getAPInt();
509 APInt Ybot = A2B1_A1B2->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000510 LLVM_DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
511 LLVM_DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
512 LLVM_DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
513 LLVM_DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000514 APInt Xq = Xtop; // these need to be initialized, even
515 APInt Xr = Xtop; // though they're just going to be overwritten
516 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
517 APInt Yq = Ytop;
Jakub Staszak340c7802013-08-06 16:40:40 +0000518 APInt Yr = Ytop;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000519 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
520 if (Xr != 0 || Yr != 0) {
521 X->setEmpty();
522 ++DeltaSuccesses;
523 return true;
524 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000525 LLVM_DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000526 if (Xq.slt(0) || Yq.slt(0)) {
527 X->setEmpty();
528 ++DeltaSuccesses;
529 return true;
530 }
531 if (const SCEVConstant *CUB =
532 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000533 const APInt &UpperBound = CUB->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000534 LLVM_DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000535 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
536 X->setEmpty();
537 ++DeltaSuccesses;
538 return true;
539 }
540 }
541 X->setPoint(SE->getConstant(Xq),
542 SE->getConstant(Yq),
543 X->getAssociatedLoop());
544 ++DeltaSuccesses;
545 return true;
546 }
547 return false;
548 }
549
550 // if (X->isLine() && Y->isPoint()) This case can't occur.
551 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
552
553 if (X->isPoint() && Y->isLine()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000554 LLVM_DEBUG(dbgs() << "\t intersect Point and Line\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +0000555 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
556 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
557 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
558 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
559 return false;
560 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
561 X->setEmpty();
562 ++DeltaSuccesses;
563 return true;
564 }
565 return false;
566 }
567
568 llvm_unreachable("shouldn't reach the end of Constraint intersection");
569 return false;
570}
571
572
573//===----------------------------------------------------------------------===//
Chandler Carruth49c22192016-05-12 22:19:39 +0000574// DependenceInfo methods
Sebastian Pop59b61b92012-10-11 07:32:34 +0000575
576// For debugging purposes. Dumps a dependence to OS.
577void Dependence::dump(raw_ostream &OS) const {
578 bool Splitable = false;
579 if (isConfused())
580 OS << "confused";
581 else {
582 if (isConsistent())
583 OS << "consistent ";
584 if (isFlow())
585 OS << "flow";
586 else if (isOutput())
587 OS << "output";
588 else if (isAnti())
589 OS << "anti";
590 else if (isInput())
591 OS << "input";
592 unsigned Levels = getLevels();
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000593 OS << " [";
594 for (unsigned II = 1; II <= Levels; ++II) {
595 if (isSplitable(II))
596 Splitable = true;
597 if (isPeelFirst(II))
598 OS << 'p';
599 const SCEV *Distance = getDistance(II);
600 if (Distance)
601 OS << *Distance;
602 else if (isScalar(II))
603 OS << "S";
604 else {
605 unsigned Direction = getDirection(II);
606 if (Direction == DVEntry::ALL)
607 OS << "*";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000608 else {
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000609 if (Direction & DVEntry::LT)
610 OS << "<";
611 if (Direction & DVEntry::EQ)
612 OS << "=";
613 if (Direction & DVEntry::GT)
614 OS << ">";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000615 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000616 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000617 if (isPeelLast(II))
618 OS << 'p';
619 if (II < Levels)
620 OS << " ";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000621 }
Preston Briggsfd0b5c82012-11-30 00:44:47 +0000622 if (isLoopIndependent())
623 OS << "|<";
624 OS << "]";
625 if (Splitable)
626 OS << " splitable";
Sebastian Pop59b61b92012-10-11 07:32:34 +0000627 }
628 OS << "!\n";
629}
630
David Green5ef933b2018-04-10 11:37:21 +0000631// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
632// underlaying objects. If LocA and LocB are known to not alias (for any reason:
633// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
634// Otherwise the underlying objects are checked to see if they point to
635// different identifiable objects.
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000636static AliasResult underlyingObjectsAlias(AliasAnalysis *AA,
David Green5ef933b2018-04-10 11:37:21 +0000637 const DataLayout &DL,
638 const MemoryLocation &LocA,
639 const MemoryLocation &LocB) {
640 // Check the original locations (minus size) for noalias, which can happen for
641 // tbaa, incompatible underlying object locations, etc.
George Burgess IV6ef80022018-10-10 21:28:44 +0000642 MemoryLocation LocAS(LocA.Ptr, LocationSize::unknown(), LocA.AATags);
643 MemoryLocation LocBS(LocB.Ptr, LocationSize::unknown(), LocB.AATags);
David Green5ef933b2018-04-10 11:37:21 +0000644 if (AA->alias(LocAS, LocBS) == NoAlias)
645 return NoAlias;
646
647 // Check the underlying objects are the same
648 const Value *AObj = GetUnderlyingObject(LocA.Ptr, DL);
649 const Value *BObj = GetUnderlyingObject(LocB.Ptr, DL);
650
651 // If the underlying objects are the same, they must alias
652 if (AObj == BObj)
653 return MustAlias;
654
655 // We may have hit the recursion limit for underlying objects, or have
656 // underlying objects where we don't know they will alias.
657 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
658 return MayAlias;
659
660 // Otherwise we know the objects are different and both identified objects so
661 // must not alias.
662 return NoAlias;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000663}
664
665
666// Returns true if the load or store can be analyzed. Atomic and volatile
667// operations have properties which this analysis does not understand.
668static
669bool isLoadOrStore(const Instruction *I) {
670 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
671 return LI->isUnordered();
672 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
673 return SI->isUnordered();
674 return false;
675}
676
677
Sebastian Pop59b61b92012-10-11 07:32:34 +0000678// Examines the loop nesting of the Src and Dst
679// instructions and establishes their shared loops. Sets the variables
680// CommonLevels, SrcLevels, and MaxLevels.
681// The source and destination instructions needn't be contained in the same
682// loop. The routine establishNestingLevels finds the level of most deeply
683// nested loop that contains them both, CommonLevels. An instruction that's
684// not contained in a loop is at level = 0. MaxLevels is equal to the level
685// of the source plus the level of the destination, minus CommonLevels.
686// This lets us allocate vectors MaxLevels in length, with room for every
687// distinct loop referenced in both the source and destination subscripts.
688// The variable SrcLevels is the nesting depth of the source instruction.
689// It's used to help calculate distinct loops referenced by the destination.
690// Here's the map from loops to levels:
691// 0 - unused
692// 1 - outermost common loop
693// ... - other common loops
694// CommonLevels - innermost common loop
695// ... - loops containing Src but not Dst
696// SrcLevels - innermost loop containing Src but not Dst
697// ... - loops containing Dst but not Src
698// MaxLevels - innermost loops containing Dst but not Src
699// Consider the follow code fragment:
700// for (a = ...) {
701// for (b = ...) {
702// for (c = ...) {
703// for (d = ...) {
704// A[] = ...;
705// }
706// }
707// for (e = ...) {
708// for (f = ...) {
709// for (g = ...) {
710// ... = A[];
711// }
712// }
713// }
714// }
715// }
716// If we're looking at the possibility of a dependence between the store
717// to A (the Src) and the load from A (the Dst), we'll note that they
718// have 2 loops in common, so CommonLevels will equal 2 and the direction
719// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
720// A map from loop names to loop numbers would look like
721// a - 1
722// b - 2 = CommonLevels
723// c - 3
724// d - 4 = SrcLevels
725// e - 5
726// f - 6
727// g - 7 = MaxLevels
Chandler Carruth49c22192016-05-12 22:19:39 +0000728void DependenceInfo::establishNestingLevels(const Instruction *Src,
729 const Instruction *Dst) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000730 const BasicBlock *SrcBlock = Src->getParent();
731 const BasicBlock *DstBlock = Dst->getParent();
732 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
733 unsigned DstLevel = LI->getLoopDepth(DstBlock);
734 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
735 const Loop *DstLoop = LI->getLoopFor(DstBlock);
736 SrcLevels = SrcLevel;
737 MaxLevels = SrcLevel + DstLevel;
738 while (SrcLevel > DstLevel) {
739 SrcLoop = SrcLoop->getParentLoop();
740 SrcLevel--;
741 }
742 while (DstLevel > SrcLevel) {
743 DstLoop = DstLoop->getParentLoop();
744 DstLevel--;
745 }
746 while (SrcLoop != DstLoop) {
747 SrcLoop = SrcLoop->getParentLoop();
748 DstLoop = DstLoop->getParentLoop();
749 SrcLevel--;
750 }
751 CommonLevels = SrcLevel;
752 MaxLevels -= CommonLevels;
753}
754
755
756// Given one of the loops containing the source, return
757// its level index in our numbering scheme.
Chandler Carruth49c22192016-05-12 22:19:39 +0000758unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000759 return SrcLoop->getLoopDepth();
760}
761
762
763// Given one of the loops containing the destination,
764// return its level index in our numbering scheme.
Chandler Carruth49c22192016-05-12 22:19:39 +0000765unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000766 unsigned D = DstLoop->getLoopDepth();
767 if (D > CommonLevels)
768 return D - CommonLevels + SrcLevels;
769 else
770 return D;
771}
772
773
774// Returns true if Expression is loop invariant in LoopNest.
Chandler Carruth49c22192016-05-12 22:19:39 +0000775bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
776 const Loop *LoopNest) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000777 if (!LoopNest)
778 return true;
779 return SE->isLoopInvariant(Expression, LoopNest) &&
780 isLoopInvariant(Expression, LoopNest->getParentLoop());
781}
782
783
784
785// Finds the set of loops from the LoopNest that
786// have a level <= CommonLevels and are referred to by the SCEV Expression.
Chandler Carruth49c22192016-05-12 22:19:39 +0000787void DependenceInfo::collectCommonLoops(const SCEV *Expression,
788 const Loop *LoopNest,
789 SmallBitVector &Loops) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000790 while (LoopNest) {
791 unsigned Level = LoopNest->getLoopDepth();
792 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
793 Loops.set(Level);
794 LoopNest = LoopNest->getParentLoop();
795 }
796}
797
Chandler Carruth49c22192016-05-12 22:19:39 +0000798void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
Jingyue Wua84feb12015-05-29 16:58:08 +0000799
800 unsigned widestWidthSeen = 0;
801 Type *widestType;
802
803 // Go through each pair and find the widest bit to which we need
804 // to extend all of them.
Benjamin Krameraa209152016-06-26 17:27:42 +0000805 for (Subscript *Pair : Pairs) {
806 const SCEV *Src = Pair->Src;
807 const SCEV *Dst = Pair->Dst;
Jingyue Wua84feb12015-05-29 16:58:08 +0000808 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
809 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
810 if (SrcTy == nullptr || DstTy == nullptr) {
811 assert(SrcTy == DstTy && "This function only unify integer types and "
812 "expect Src and Dst share the same type "
813 "otherwise.");
814 continue;
815 }
816 if (SrcTy->getBitWidth() > widestWidthSeen) {
817 widestWidthSeen = SrcTy->getBitWidth();
818 widestType = SrcTy;
819 }
820 if (DstTy->getBitWidth() > widestWidthSeen) {
821 widestWidthSeen = DstTy->getBitWidth();
822 widestType = DstTy;
823 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000824 }
Jingyue Wua84feb12015-05-29 16:58:08 +0000825
826
827 assert(widestWidthSeen > 0);
828
829 // Now extend each pair to the widest seen.
Benjamin Krameraa209152016-06-26 17:27:42 +0000830 for (Subscript *Pair : Pairs) {
831 const SCEV *Src = Pair->Src;
832 const SCEV *Dst = Pair->Dst;
Jingyue Wua84feb12015-05-29 16:58:08 +0000833 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
834 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
835 if (SrcTy == nullptr || DstTy == nullptr) {
836 assert(SrcTy == DstTy && "This function only unify integer types and "
837 "expect Src and Dst share the same type "
838 "otherwise.");
839 continue;
840 }
841 if (SrcTy->getBitWidth() < widestWidthSeen)
842 // Sign-extend Src to widestType
Benjamin Krameraa209152016-06-26 17:27:42 +0000843 Pair->Src = SE->getSignExtendExpr(Src, widestType);
Jingyue Wua84feb12015-05-29 16:58:08 +0000844 if (DstTy->getBitWidth() < widestWidthSeen) {
845 // Sign-extend Dst to widestType
Benjamin Krameraa209152016-06-26 17:27:42 +0000846 Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
Jingyue Wua84feb12015-05-29 16:58:08 +0000847 }
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000848 }
849}
Sebastian Pop59b61b92012-10-11 07:32:34 +0000850
851// removeMatchingExtensions - Examines a subscript pair.
852// If the source and destination are identically sign (or zero)
853// extended, it strips off the extension in an effect to simplify
854// the actual analysis.
Chandler Carruth49c22192016-05-12 22:19:39 +0000855void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000856 const SCEV *Src = Pair->Src;
857 const SCEV *Dst = Pair->Dst;
858 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
859 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
860 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src);
861 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst);
Jingyue Wu0fa125a2014-11-16 16:52:44 +0000862 const SCEV *SrcCastOp = SrcCast->getOperand();
863 const SCEV *DstCastOp = DstCast->getOperand();
864 if (SrcCastOp->getType() == DstCastOp->getType()) {
865 Pair->Src = SrcCastOp;
866 Pair->Dst = DstCastOp;
Sebastian Pop59b61b92012-10-11 07:32:34 +0000867 }
868 }
869}
870
871
872// Examine the scev and return true iff it's linear.
873// Collect any loops mentioned in the set of "Loops".
Chandler Carruth49c22192016-05-12 22:19:39 +0000874bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
875 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000876 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src);
877 if (!AddRec)
878 return isLoopInvariant(Src, LoopNest);
879 const SCEV *Start = AddRec->getStart();
880 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000881 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
882 if (!isa<SCEVCouldNotCompute>(UB)) {
883 if (SE->getTypeSizeInBits(Start->getType()) <
884 SE->getTypeSizeInBits(UB->getType())) {
885 if (!AddRec->getNoWrapFlags())
886 return false;
887 }
888 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000889 if (!isLoopInvariant(Step, LoopNest))
890 return false;
891 Loops.set(mapSrcLoop(AddRec->getLoop()));
892 return checkSrcSubscript(Start, LoopNest, Loops);
893}
894
895
896
897// Examine the scev and return true iff it's linear.
898// Collect any loops mentioned in the set of "Loops".
Chandler Carruth49c22192016-05-12 22:19:39 +0000899bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
900 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000901 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst);
902 if (!AddRec)
903 return isLoopInvariant(Dst, LoopNest);
904 const SCEV *Start = AddRec->getStart();
905 const SCEV *Step = AddRec->getStepRecurrence(*SE);
James Molloyc0661ae2015-05-15 12:17:22 +0000906 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
907 if (!isa<SCEVCouldNotCompute>(UB)) {
908 if (SE->getTypeSizeInBits(Start->getType()) <
909 SE->getTypeSizeInBits(UB->getType())) {
910 if (!AddRec->getNoWrapFlags())
911 return false;
912 }
913 }
Sebastian Pop59b61b92012-10-11 07:32:34 +0000914 if (!isLoopInvariant(Step, LoopNest))
915 return false;
916 Loops.set(mapDstLoop(AddRec->getLoop()));
917 return checkDstSubscript(Start, LoopNest, Loops);
918}
919
920
921// Examines the subscript pair (the Src and Dst SCEVs)
922// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
923// Collects the associated loops in a set.
Chandler Carruth49c22192016-05-12 22:19:39 +0000924DependenceInfo::Subscript::ClassificationKind
925DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
926 const SCEV *Dst, const Loop *DstLoopNest,
927 SmallBitVector &Loops) {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000928 SmallBitVector SrcLoops(MaxLevels + 1);
929 SmallBitVector DstLoops(MaxLevels + 1);
930 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
931 return Subscript::NonLinear;
932 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
933 return Subscript::NonLinear;
934 Loops = SrcLoops;
935 Loops |= DstLoops;
936 unsigned N = Loops.count();
937 if (N == 0)
938 return Subscript::ZIV;
939 if (N == 1)
940 return Subscript::SIV;
941 if (N == 2 && (SrcLoops.count() == 0 ||
942 DstLoops.count() == 0 ||
943 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
944 return Subscript::RDIV;
945 return Subscript::MIV;
946}
947
948
949// A wrapper around SCEV::isKnownPredicate.
950// Looks for cases where we're interested in comparing for equality.
951// If both X and Y have been identically sign or zero extended,
952// it strips off the (confusing) extensions before invoking
953// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
954// will be similarly updated.
955//
956// If SCEV::isKnownPredicate can't prove the predicate,
957// we try simple subtraction, which seems to help in some cases
958// involving symbolics.
Chandler Carruth49c22192016-05-12 22:19:39 +0000959bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
960 const SCEV *Y) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +0000961 if (Pred == CmpInst::ICMP_EQ ||
962 Pred == CmpInst::ICMP_NE) {
963 if ((isa<SCEVSignExtendExpr>(X) &&
964 isa<SCEVSignExtendExpr>(Y)) ||
965 (isa<SCEVZeroExtendExpr>(X) &&
966 isa<SCEVZeroExtendExpr>(Y))) {
967 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X);
968 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y);
969 const SCEV *Xop = CX->getOperand();
970 const SCEV *Yop = CY->getOperand();
971 if (Xop->getType() == Yop->getType()) {
972 X = Xop;
973 Y = Yop;
974 }
975 }
976 }
977 if (SE->isKnownPredicate(Pred, X, Y))
978 return true;
979 // If SE->isKnownPredicate can't prove the condition,
980 // we try the brute-force approach of subtracting
981 // and testing the difference.
982 // By testing with SE->isKnownPredicate first, we avoid
983 // the possibility of overflow when the arguments are constants.
984 const SCEV *Delta = SE->getMinusSCEV(X, Y);
985 switch (Pred) {
986 case CmpInst::ICMP_EQ:
987 return Delta->isZero();
988 case CmpInst::ICMP_NE:
989 return SE->isKnownNonZero(Delta);
990 case CmpInst::ICMP_SGE:
991 return SE->isKnownNonNegative(Delta);
992 case CmpInst::ICMP_SLE:
993 return SE->isKnownNonPositive(Delta);
994 case CmpInst::ICMP_SGT:
995 return SE->isKnownPositive(Delta);
996 case CmpInst::ICMP_SLT:
997 return SE->isKnownNegative(Delta);
998 default:
999 llvm_unreachable("unexpected predicate in isKnownPredicate");
1000 }
1001}
1002
David Greend143c652018-06-21 11:53:16 +00001003/// Compare to see if S is less than Size, using isKnownNegative(S - max(Size, 1))
1004/// with some extra checking if S is an AddRec and we can prove less-than using
1005/// the loop bounds.
1006bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
1007 // First unify to the same type
1008 auto *SType = dyn_cast<IntegerType>(S->getType());
1009 auto *SizeType = dyn_cast<IntegerType>(Size->getType());
1010 if (!SType || !SizeType)
1011 return false;
1012 Type *MaxType =
1013 (SType->getBitWidth() >= SizeType->getBitWidth()) ? SType : SizeType;
1014 S = SE->getTruncateOrZeroExtend(S, MaxType);
1015 Size = SE->getTruncateOrZeroExtend(Size, MaxType);
1016
1017 // Special check for addrecs using BE taken count
1018 const SCEV *Bound = SE->getMinusSCEV(S, Size);
1019 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Bound)) {
1020 if (AddRec->isAffine()) {
1021 const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
1022 if (!isa<SCEVCouldNotCompute>(BECount)) {
1023 const SCEV *Limit = AddRec->evaluateAtIteration(BECount, *SE);
1024 if (SE->isKnownNegative(Limit))
1025 return true;
1026 }
1027 }
1028 }
1029
1030 // Check using normal isKnownNegative
1031 const SCEV *LimitedBound =
1032 SE->getMinusSCEV(S, SE->getSMaxExpr(Size, SE->getOne(Size->getType())));
1033 return SE->isKnownNegative(LimitedBound);
1034}
Sebastian Pop59b61b92012-10-11 07:32:34 +00001035
David Green86994922018-06-25 15:13:26 +00001036bool DependenceInfo::isKnownNonNegative(const SCEV *S, const Value *Ptr) const {
1037 bool Inbounds = false;
1038 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(Ptr))
1039 Inbounds = SrcGEP->isInBounds();
1040 if (Inbounds) {
1041 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
1042 if (AddRec->isAffine()) {
1043 // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
1044 // If both parts are NonNegative, the end result will be NonNegative
1045 if (SE->isKnownNonNegative(AddRec->getStart()) &&
1046 SE->isKnownNonNegative(AddRec->getOperand(1)))
1047 return true;
1048 }
1049 }
1050 }
1051
1052 return SE->isKnownNonNegative(S);
1053}
1054
Sebastian Pop59b61b92012-10-11 07:32:34 +00001055// All subscripts are all the same type.
1056// Loop bound may be smaller (e.g., a char).
1057// Should zero extend loop bound, since it's always >= 0.
James Molloyc0661ae2015-05-15 12:17:22 +00001058// This routine collects upper bound and extends or truncates if needed.
1059// Truncating is safe when subscripts are known not to wrap. Cases without
1060// nowrap flags should have been rejected earlier.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001061// Return null if no bound available.
Chandler Carruth49c22192016-05-12 22:19:39 +00001062const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001063 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1064 const SCEV *UB = SE->getBackedgeTakenCount(L);
James Molloyc0661ae2015-05-15 12:17:22 +00001065 return SE->getTruncateOrZeroExtend(UB, T);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001066 }
Craig Topper9f008862014-04-15 04:59:12 +00001067 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001068}
1069
1070
1071// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1072// If the cast fails, returns NULL.
Chandler Carruth49c22192016-05-12 22:19:39 +00001073const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1074 Type *T) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001075 if (const SCEV *UB = collectUpperBound(L, T))
1076 return dyn_cast<SCEVConstant>(UB);
Craig Topper9f008862014-04-15 04:59:12 +00001077 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001078}
1079
1080
1081// testZIV -
1082// When we have a pair of subscripts of the form [c1] and [c2],
1083// where c1 and c2 are both loop invariant, we attack it using
1084// the ZIV test. Basically, we test by comparing the two values,
1085// but there are actually three possible results:
1086// 1) the values are equal, so there's a dependence
1087// 2) the values are different, so there's no dependence
1088// 3) the values might be equal, so we have to assume a dependence.
1089//
1090// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001091bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1092 FullDependence &Result) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001093 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1094 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001095 ++ZIVapplications;
1096 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001097 LLVM_DEBUG(dbgs() << " provably dependent\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001098 return false; // provably dependent
1099 }
1100 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001101 LLVM_DEBUG(dbgs() << " provably independent\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001102 ++ZIVindependence;
1103 return true; // provably independent
1104 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001105 LLVM_DEBUG(dbgs() << " possibly dependent\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001106 Result.Consistent = false;
1107 return false; // possibly dependent
1108}
1109
1110
1111// strongSIVtest -
1112// From the paper, Practical Dependence Testing, Section 4.2.1
1113//
1114// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1115// where i is an induction variable, c1 and c2 are loop invariant,
1116// and a is a constant, we can solve it exactly using the Strong SIV test.
1117//
1118// Can prove independence. Failing that, can compute distance (and direction).
1119// In the presence of symbolic terms, we can sometimes make progress.
1120//
1121// If there's a dependence,
1122//
1123// c1 + a*i = c2 + a*i'
1124//
1125// The dependence distance is
1126//
1127// d = i' - i = (c1 - c2)/a
1128//
1129// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1130// loop's upper bound. If a dependence exists, the dependence direction is
1131// defined as
1132//
1133// { < if d > 0
1134// direction = { = if d = 0
1135// { > if d < 0
1136//
1137// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001138bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1139 const SCEV *DstConst, const Loop *CurLoop,
1140 unsigned Level, FullDependence &Result,
1141 Constraint &NewConstraint) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001142 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
1143 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1144 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1145 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1146 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1147 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1148 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001149 ++StrongSIVapplications;
1150 assert(0 < Level && Level <= CommonLevels && "level out of range");
1151 Level--;
1152
1153 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001154 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
1155 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001156
1157 // check that |Delta| < iteration count
1158 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001159 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1160 LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001161 const SCEV *AbsDelta =
1162 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1163 const SCEV *AbsCoeff =
1164 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1165 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1166 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1167 // Distance greater than trip count - no dependence
1168 ++StrongSIVindependence;
1169 ++StrongSIVsuccesses;
1170 return true;
1171 }
1172 }
1173
1174 // Can we compute distance?
1175 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001176 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1177 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001178 APInt Distance = ConstDelta; // these need to be initialized
1179 APInt Remainder = ConstDelta;
1180 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001181 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1182 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001183 // Make sure Coeff divides Delta exactly
1184 if (Remainder != 0) {
1185 // Coeff doesn't divide Distance, no dependence
1186 ++StrongSIVindependence;
1187 ++StrongSIVsuccesses;
1188 return true;
1189 }
1190 Result.DV[Level].Distance = SE->getConstant(Distance);
1191 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
1192 if (Distance.sgt(0))
1193 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1194 else if (Distance.slt(0))
1195 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1196 else
1197 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1198 ++StrongSIVsuccesses;
1199 }
1200 else if (Delta->isZero()) {
1201 // since 0/X == 0
1202 Result.DV[Level].Distance = Delta;
1203 NewConstraint.setDistance(Delta, CurLoop);
1204 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1205 ++StrongSIVsuccesses;
1206 }
1207 else {
1208 if (Coeff->isOne()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001209 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001210 Result.DV[Level].Distance = Delta; // since X/1 == X
1211 NewConstraint.setDistance(Delta, CurLoop);
1212 }
1213 else {
1214 Result.Consistent = false;
1215 NewConstraint.setLine(Coeff,
1216 SE->getNegativeSCEV(Coeff),
1217 SE->getNegativeSCEV(Delta), CurLoop);
1218 }
1219
1220 // maybe we can get a useful direction
1221 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1222 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1223 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1224 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1225 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1226 // The double negatives above are confusing.
1227 // It helps to read !SE->isKnownNonZero(Delta)
1228 // as "Delta might be Zero"
1229 unsigned NewDirection = Dependence::DVEntry::NONE;
1230 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1231 (DeltaMaybeNegative && CoeffMaybeNegative))
1232 NewDirection = Dependence::DVEntry::LT;
1233 if (DeltaMaybeZero)
1234 NewDirection |= Dependence::DVEntry::EQ;
1235 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1236 (DeltaMaybePositive && CoeffMaybeNegative))
1237 NewDirection |= Dependence::DVEntry::GT;
1238 if (NewDirection < Result.DV[Level].Direction)
1239 ++StrongSIVsuccesses;
1240 Result.DV[Level].Direction &= NewDirection;
1241 }
1242 return false;
1243}
1244
1245
1246// weakCrossingSIVtest -
1247// From the paper, Practical Dependence Testing, Section 4.2.2
1248//
1249// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1250// where i is an induction variable, c1 and c2 are loop invariant,
1251// and a is a constant, we can solve it exactly using the
1252// Weak-Crossing SIV test.
1253//
1254// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1255// the two lines, where i = i', yielding
1256//
1257// c1 + a*i = c2 - a*i
1258// 2a*i = c2 - c1
1259// i = (c2 - c1)/2a
1260//
1261// If i < 0, there is no dependence.
1262// If i > upperbound, there is no dependence.
1263// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1264// If i = upperbound, there's a dependence with distance = 0.
1265// If i is integral, there's a dependence (all directions).
1266// If the non-integer part = 1/2, there's a dependence (<> directions).
1267// Otherwise, there's no dependence.
1268//
1269// Can prove independence. Failing that,
1270// can sometimes refine the directions.
1271// Can determine iteration for splitting.
1272//
1273// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001274bool DependenceInfo::weakCrossingSIVtest(
1275 const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
1276 const Loop *CurLoop, unsigned Level, FullDependence &Result,
1277 Constraint &NewConstraint, const SCEV *&SplitIter) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001278 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1279 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1280 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1281 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001282 ++WeakCrossingSIVapplications;
1283 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1284 Level--;
1285 Result.Consistent = false;
1286 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001287 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001288 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1289 if (Delta->isZero()) {
Sebastian Pope96232612012-10-12 02:04:32 +00001290 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1291 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001292 ++WeakCrossingSIVsuccesses;
1293 if (!Result.DV[Level].Direction) {
1294 ++WeakCrossingSIVindependence;
1295 return true;
1296 }
1297 Result.DV[Level].Distance = Delta; // = 0
1298 return false;
1299 }
1300 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1301 if (!ConstCoeff)
1302 return false;
1303
1304 Result.DV[Level].Splitable = true;
1305 if (SE->isKnownNegative(ConstCoeff)) {
1306 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1307 assert(ConstCoeff &&
1308 "dynamic cast of negative of ConstCoeff should yield constant");
1309 Delta = SE->getNegativeSCEV(Delta);
1310 }
1311 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1312
Chandler Carruth49c22192016-05-12 22:19:39 +00001313 // compute SplitIter for use by DependenceInfo::getSplitIteration()
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001314 SplitIter = SE->getUDivExpr(
1315 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1316 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001317 LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001318
1319 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1320 if (!ConstDelta)
1321 return false;
1322
1323 // We're certain that ConstCoeff > 0; therefore,
1324 // if Delta < 0, then no dependence.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001325 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1326 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001327 if (SE->isKnownNegative(Delta)) {
1328 // No dependence, Delta < 0
1329 ++WeakCrossingSIVindependence;
1330 ++WeakCrossingSIVsuccesses;
1331 return true;
1332 }
1333
1334 // We're certain that Delta > 0 and ConstCoeff > 0.
1335 // Check Delta/(2*ConstCoeff) against upper loop bound
1336 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001337 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001338 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1339 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
1340 ConstantTwo);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001341 LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001342 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1343 // Delta too big, no dependence
1344 ++WeakCrossingSIVindependence;
1345 ++WeakCrossingSIVsuccesses;
1346 return true;
1347 }
1348 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1349 // i = i' = UB
Sebastian Pope96232612012-10-12 02:04:32 +00001350 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1351 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001352 ++WeakCrossingSIVsuccesses;
1353 if (!Result.DV[Level].Direction) {
1354 ++WeakCrossingSIVindependence;
1355 return true;
1356 }
1357 Result.DV[Level].Splitable = false;
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001358 Result.DV[Level].Distance = SE->getZero(Delta->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00001359 return false;
1360 }
1361 }
1362
1363 // check that Coeff divides Delta
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001364 APInt APDelta = ConstDelta->getAPInt();
1365 APInt APCoeff = ConstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001366 APInt Distance = APDelta; // these need to be initialzed
1367 APInt Remainder = APDelta;
1368 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001369 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001370 if (Remainder != 0) {
1371 // Coeff doesn't divide Delta, no dependence
1372 ++WeakCrossingSIVindependence;
1373 ++WeakCrossingSIVsuccesses;
1374 return true;
1375 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001376 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001377
1378 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1379 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1380 Remainder = Distance.srem(Two);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001381 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001382 if (Remainder != 0) {
1383 // Equal direction isn't possible
Sebastian Pope96232612012-10-12 02:04:32 +00001384 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00001385 ++WeakCrossingSIVsuccesses;
1386 }
1387 return false;
1388}
1389
1390
1391// Kirch's algorithm, from
1392//
1393// Optimizing Supercompilers for Supercomputers
1394// Michael Wolfe
1395// MIT Press, 1989
1396//
1397// Program 2.1, page 29.
1398// Computes the GCD of AM and BM.
Mingjie Xing9deac1b2014-01-07 01:54:16 +00001399// Also finds a solution to the equation ax - by = gcd(a, b).
1400// Returns true if dependence disproved; i.e., gcd does not divide Delta.
Benjamin Kramerc321e532016-06-08 19:09:22 +00001401static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1402 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001403 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1404 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1405 APInt G0 = AM.abs();
1406 APInt G1 = BM.abs();
1407 APInt Q = G0; // these need to be initialized
1408 APInt R = G0;
1409 APInt::sdivrem(G0, G1, Q, R);
1410 while (R != 0) {
1411 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1412 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1413 G0 = G1; G1 = R;
1414 APInt::sdivrem(G0, G1, Q, R);
1415 }
1416 G = G1;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001417 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001418 X = AM.slt(0) ? -A1 : A1;
1419 Y = BM.slt(0) ? B1 : -B1;
1420
1421 // make sure gcd divides Delta
1422 R = Delta.srem(G);
1423 if (R != 0)
1424 return true; // gcd doesn't divide Delta, no dependence
1425 Q = Delta.sdiv(G);
1426 X *= Q;
1427 Y *= Q;
1428 return false;
1429}
1430
Benjamin Kramerc321e532016-06-08 19:09:22 +00001431static APInt floorOfQuotient(const APInt &A, const APInt &B) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001432 APInt Q = A; // these need to be initialized
1433 APInt R = A;
1434 APInt::sdivrem(A, B, Q, R);
1435 if (R == 0)
1436 return Q;
1437 if ((A.sgt(0) && B.sgt(0)) ||
1438 (A.slt(0) && B.slt(0)))
1439 return Q;
1440 else
1441 return Q - 1;
1442}
1443
Benjamin Kramerc321e532016-06-08 19:09:22 +00001444static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001445 APInt Q = A; // these need to be initialized
1446 APInt R = A;
1447 APInt::sdivrem(A, B, Q, R);
1448 if (R == 0)
1449 return Q;
1450 if ((A.sgt(0) && B.sgt(0)) ||
1451 (A.slt(0) && B.slt(0)))
1452 return Q + 1;
1453 else
1454 return Q;
1455}
1456
1457
1458static
1459APInt maxAPInt(APInt A, APInt B) {
1460 return A.sgt(B) ? A : B;
1461}
1462
1463
1464static
1465APInt minAPInt(APInt A, APInt B) {
1466 return A.slt(B) ? A : B;
1467}
1468
1469
1470// exactSIVtest -
1471// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1472// where i is an induction variable, c1 and c2 are loop invariant, and a1
1473// and a2 are constant, we can solve it exactly using an algorithm developed
1474// by Banerjee and Wolfe. See Section 2.5.3 in
1475//
1476// Optimizing Supercompilers for Supercomputers
1477// Michael Wolfe
1478// MIT Press, 1989
1479//
1480// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1481// so use them if possible. They're also a bit better with symbolics and,
1482// in the case of the strong SIV test, can compute Distances.
1483//
1484// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001485bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1486 const SCEV *SrcConst, const SCEV *DstConst,
1487 const Loop *CurLoop, unsigned Level,
1488 FullDependence &Result,
1489 Constraint &NewConstraint) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001490 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1491 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1492 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1493 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1494 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001495 ++ExactSIVapplications;
1496 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1497 Level--;
1498 Result.Consistent = false;
1499 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001500 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001501 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff),
1502 Delta, CurLoop);
1503 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1504 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1505 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1506 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1507 return false;
1508
1509 // find gcd
1510 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001511 APInt AM = ConstSrcCoeff->getAPInt();
1512 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001513 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001514 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001515 // gcd doesn't divide Delta, no dependence
1516 ++ExactSIVindependence;
1517 ++ExactSIVsuccesses;
1518 return true;
1519 }
1520
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001521 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001522
1523 // since SCEV construction normalizes, LM = 0
1524 APInt UM(Bits, 1, true);
1525 bool UMvalid = false;
1526 // UM is perhaps unavailable, let's check
1527 if (const SCEVConstant *CUB =
1528 collectConstantUpperBound(CurLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001529 UM = CUB->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001530 LLVM_DEBUG(dbgs() << "\t UM = " << UM << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001531 UMvalid = true;
1532 }
1533
1534 APInt TU(APInt::getSignedMaxValue(Bits));
1535 APInt TL(APInt::getSignedMinValue(Bits));
1536
1537 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1538 APInt TMUL = BM.sdiv(G);
1539 if (TMUL.sgt(0)) {
1540 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001541 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001542 if (UMvalid) {
1543 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001544 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001545 }
1546 }
1547 else {
1548 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001549 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001550 if (UMvalid) {
1551 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001552 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001553 }
1554 }
1555
1556 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1557 TMUL = AM.sdiv(G);
1558 if (TMUL.sgt(0)) {
1559 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001560 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001561 if (UMvalid) {
1562 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001563 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001564 }
1565 }
1566 else {
1567 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001568 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001569 if (UMvalid) {
1570 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001571 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001572 }
1573 }
1574 if (TL.sgt(TU)) {
1575 ++ExactSIVindependence;
1576 ++ExactSIVsuccesses;
1577 return true;
1578 }
1579
1580 // explore directions
1581 unsigned NewDirection = Dependence::DVEntry::NONE;
1582
1583 // less than
1584 APInt SaveTU(TU); // save these
1585 APInt SaveTL(TL);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001586 LLVM_DEBUG(dbgs() << "\t exploring LT direction\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001587 TMUL = AM - BM;
1588 if (TMUL.sgt(0)) {
1589 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001590 LLVM_DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001591 }
1592 else {
1593 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001594 LLVM_DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001595 }
1596 if (TL.sle(TU)) {
1597 NewDirection |= Dependence::DVEntry::LT;
1598 ++ExactSIVsuccesses;
1599 }
1600
1601 // equal
1602 TU = SaveTU; // restore
1603 TL = SaveTL;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001604 LLVM_DEBUG(dbgs() << "\t exploring EQ direction\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001605 if (TMUL.sgt(0)) {
1606 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001607 LLVM_DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001608 }
1609 else {
1610 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001611 LLVM_DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001612 }
1613 TMUL = BM - AM;
1614 if (TMUL.sgt(0)) {
1615 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001616 LLVM_DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001617 }
1618 else {
1619 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001620 LLVM_DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001621 }
1622 if (TL.sle(TU)) {
1623 NewDirection |= Dependence::DVEntry::EQ;
1624 ++ExactSIVsuccesses;
1625 }
1626
1627 // greater than
1628 TU = SaveTU; // restore
1629 TL = SaveTL;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001630 LLVM_DEBUG(dbgs() << "\t exploring GT direction\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001631 if (TMUL.sgt(0)) {
1632 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001633 LLVM_DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001634 }
1635 else {
1636 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001637 LLVM_DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001638 }
1639 if (TL.sle(TU)) {
1640 NewDirection |= Dependence::DVEntry::GT;
1641 ++ExactSIVsuccesses;
1642 }
1643
1644 // finished
1645 Result.DV[Level].Direction &= NewDirection;
1646 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1647 ++ExactSIVindependence;
1648 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1649}
1650
1651
1652
1653// Return true if the divisor evenly divides the dividend.
1654static
1655bool isRemainderZero(const SCEVConstant *Dividend,
1656 const SCEVConstant *Divisor) {
Benjamin Kramer46e38f32016-06-08 10:01:20 +00001657 const APInt &ConstDividend = Dividend->getAPInt();
1658 const APInt &ConstDivisor = Divisor->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001659 return ConstDividend.srem(ConstDivisor) == 0;
1660}
1661
1662
1663// weakZeroSrcSIVtest -
1664// From the paper, Practical Dependence Testing, Section 4.2.2
1665//
1666// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1667// where i is an induction variable, c1 and c2 are loop invariant,
1668// and a is a constant, we can solve it exactly using the
1669// Weak-Zero SIV test.
1670//
1671// Given
1672//
1673// c1 = c2 + a*i
1674//
1675// we get
1676//
1677// (c1 - c2)/a = i
1678//
1679// If i is not an integer, there's no dependence.
1680// If i < 0 or > UB, there's no dependence.
David Green2911b3a2018-05-31 14:55:29 +00001681// If i = 0, the direction is >= and peeling the
Sebastian Pop59b61b92012-10-11 07:32:34 +00001682// 1st iteration will break the dependence.
David Green2911b3a2018-05-31 14:55:29 +00001683// If i = UB, the direction is <= and peeling the
Sebastian Pop59b61b92012-10-11 07:32:34 +00001684// last iteration will break the dependence.
1685// Otherwise, the direction is *.
1686//
1687// Can prove independence. Failing that, we can sometimes refine
1688// the directions. Can sometimes show that first or last
1689// iteration carries all the dependences (so worth peeling).
1690//
1691// (see also weakZeroDstSIVtest)
1692//
1693// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001694bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1695 const SCEV *SrcConst,
1696 const SCEV *DstConst,
1697 const Loop *CurLoop, unsigned Level,
1698 FullDependence &Result,
1699 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001700 // For the WeakSIV test, it's possible the loop isn't common to
1701 // the Src and Dst loops. If it isn't, then there's no need to
1702 // record a direction.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001703 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1704 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1705 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1706 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001707 ++WeakZeroSIVapplications;
1708 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1709 Level--;
1710 Result.Consistent = false;
1711 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001712 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1713 CurLoop);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001714 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001715 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1716 if (Level < CommonLevels) {
David Green2911b3a2018-05-31 14:55:29 +00001717 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001718 Result.DV[Level].PeelFirst = true;
1719 ++WeakZeroSIVsuccesses;
1720 }
1721 return false; // dependences caused by first iteration
1722 }
1723 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1724 if (!ConstCoeff)
1725 return false;
1726 const SCEV *AbsCoeff =
1727 SE->isKnownNegative(ConstCoeff) ?
1728 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1729 const SCEV *NewDelta =
1730 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1731
1732 // check that Delta/SrcCoeff < iteration count
1733 // really check NewDelta < count*AbsCoeff
1734 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001735 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001736 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1737 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1738 ++WeakZeroSIVindependence;
1739 ++WeakZeroSIVsuccesses;
1740 return true;
1741 }
1742 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1743 // dependences caused by last iteration
1744 if (Level < CommonLevels) {
David Green2911b3a2018-05-31 14:55:29 +00001745 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
Sebastian Pop59b61b92012-10-11 07:32:34 +00001746 Result.DV[Level].PeelLast = true;
1747 ++WeakZeroSIVsuccesses;
1748 }
1749 return false;
1750 }
1751 }
1752
1753 // check that Delta/SrcCoeff >= 0
1754 // really check that NewDelta >= 0
1755 if (SE->isKnownNegative(NewDelta)) {
1756 // No dependence, newDelta < 0
1757 ++WeakZeroSIVindependence;
1758 ++WeakZeroSIVsuccesses;
1759 return true;
1760 }
1761
1762 // if SrcCoeff doesn't divide Delta, then no dependence
1763 if (isa<SCEVConstant>(Delta) &&
1764 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1765 ++WeakZeroSIVindependence;
1766 ++WeakZeroSIVsuccesses;
1767 return true;
1768 }
1769 return false;
1770}
1771
1772
1773// weakZeroDstSIVtest -
1774// From the paper, Practical Dependence Testing, Section 4.2.2
1775//
1776// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1777// where i is an induction variable, c1 and c2 are loop invariant,
1778// and a is a constant, we can solve it exactly using the
1779// Weak-Zero SIV test.
1780//
1781// Given
1782//
1783// c1 + a*i = c2
1784//
1785// we get
1786//
1787// i = (c2 - c1)/a
1788//
1789// If i is not an integer, there's no dependence.
1790// If i < 0 or > UB, there's no dependence.
1791// If i = 0, the direction is <= and peeling the
1792// 1st iteration will break the dependence.
1793// If i = UB, the direction is >= and peeling the
1794// last iteration will break the dependence.
1795// Otherwise, the direction is *.
1796//
1797// Can prove independence. Failing that, we can sometimes refine
1798// the directions. Can sometimes show that first or last
1799// iteration carries all the dependences (so worth peeling).
1800//
1801// (see also weakZeroSrcSIVtest)
1802//
1803// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00001804bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1805 const SCEV *SrcConst,
1806 const SCEV *DstConst,
1807 const Loop *CurLoop, unsigned Level,
1808 FullDependence &Result,
1809 Constraint &NewConstraint) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001810 // For the WeakSIV test, it's possible the loop isn't common to the
1811 // Src and Dst loops. If it isn't, then there's no need to record a direction.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001812 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1813 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1814 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1815 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001816 ++WeakZeroSIVapplications;
1817 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1818 Level--;
1819 Result.Consistent = false;
1820 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001821 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
1822 CurLoop);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001823 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001824 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1825 if (Level < CommonLevels) {
1826 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1827 Result.DV[Level].PeelFirst = true;
1828 ++WeakZeroSIVsuccesses;
1829 }
1830 return false; // dependences caused by first iteration
1831 }
1832 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1833 if (!ConstCoeff)
1834 return false;
1835 const SCEV *AbsCoeff =
1836 SE->isKnownNegative(ConstCoeff) ?
1837 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1838 const SCEV *NewDelta =
1839 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1840
1841 // check that Delta/SrcCoeff < iteration count
1842 // really check NewDelta < count*AbsCoeff
1843 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001844 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001845 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1846 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1847 ++WeakZeroSIVindependence;
1848 ++WeakZeroSIVsuccesses;
1849 return true;
1850 }
1851 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1852 // dependences caused by last iteration
1853 if (Level < CommonLevels) {
1854 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1855 Result.DV[Level].PeelLast = true;
1856 ++WeakZeroSIVsuccesses;
1857 }
1858 return false;
1859 }
1860 }
1861
1862 // check that Delta/SrcCoeff >= 0
1863 // really check that NewDelta >= 0
1864 if (SE->isKnownNegative(NewDelta)) {
1865 // No dependence, newDelta < 0
1866 ++WeakZeroSIVindependence;
1867 ++WeakZeroSIVsuccesses;
1868 return true;
1869 }
1870
1871 // if SrcCoeff doesn't divide Delta, then no dependence
1872 if (isa<SCEVConstant>(Delta) &&
1873 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1874 ++WeakZeroSIVindependence;
1875 ++WeakZeroSIVsuccesses;
1876 return true;
1877 }
1878 return false;
1879}
1880
1881
1882// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1883// Things of the form [c1 + a*i] and [c2 + b*j],
1884// where i and j are induction variable, c1 and c2 are loop invariant,
1885// and a and b are constants.
1886// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00001887// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00001888// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
Chandler Carruth49c22192016-05-12 22:19:39 +00001889bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1890 const SCEV *SrcConst, const SCEV *DstConst,
1891 const Loop *SrcLoop, const Loop *DstLoop,
1892 FullDependence &Result) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001893 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
1894 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1895 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1896 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1897 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001898 ++ExactRDIVapplications;
1899 Result.Consistent = false;
1900 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001901 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001902 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1903 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1904 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1905 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1906 return false;
1907
1908 // find gcd
1909 APInt G, X, Y;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001910 APInt AM = ConstSrcCoeff->getAPInt();
1911 APInt BM = ConstDstCoeff->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00001912 unsigned Bits = AM.getBitWidth();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001913 if (findGCD(Bits, AM, BM, ConstDelta->getAPInt(), G, X, Y)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00001914 // gcd doesn't divide Delta, no dependence
1915 ++ExactRDIVindependence;
1916 return true;
1917 }
1918
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001919 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001920
1921 // since SCEV construction seems to normalize, LM = 0
1922 APInt SrcUM(Bits, 1, true);
1923 bool SrcUMvalid = false;
1924 // SrcUM is perhaps unavailable, let's check
1925 if (const SCEVConstant *UpperBound =
1926 collectConstantUpperBound(SrcLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001927 SrcUM = UpperBound->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001928 LLVM_DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001929 SrcUMvalid = true;
1930 }
1931
1932 APInt DstUM(Bits, 1, true);
1933 bool DstUMvalid = false;
1934 // UM is perhaps unavailable, let's check
1935 if (const SCEVConstant *UpperBound =
1936 collectConstantUpperBound(DstLoop, Delta->getType())) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001937 DstUM = UpperBound->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001938 LLVM_DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001939 DstUMvalid = true;
1940 }
1941
1942 APInt TU(APInt::getSignedMaxValue(Bits));
1943 APInt TL(APInt::getSignedMinValue(Bits));
1944
1945 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1946 APInt TMUL = BM.sdiv(G);
1947 if (TMUL.sgt(0)) {
1948 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001949 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001950 if (SrcUMvalid) {
1951 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001952 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001953 }
1954 }
1955 else {
1956 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001957 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001958 if (SrcUMvalid) {
1959 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001960 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001961 }
1962 }
1963
1964 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1965 TMUL = AM.sdiv(G);
1966 if (TMUL.sgt(0)) {
1967 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001968 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001969 if (DstUMvalid) {
1970 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001971 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001972 }
1973 }
1974 else {
1975 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001976 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001977 if (DstUMvalid) {
1978 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001979 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00001980 }
1981 }
1982 if (TL.sgt(TU))
1983 ++ExactRDIVindependence;
1984 return TL.sgt(TU);
1985}
1986
1987
1988// symbolicRDIVtest -
1989// In Section 4.5 of the Practical Dependence Testing paper,the authors
1990// introduce a special case of Banerjee's Inequalities (also called the
1991// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
1992// particularly cases with symbolics. Since it's only able to disprove
1993// dependence (not compute distances or directions), we'll use it as a
1994// fall back for the other tests.
1995//
1996// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1997// where i and j are induction variables and c1 and c2 are loop invariants,
1998// we can use the symbolic tests to disprove some dependences, serving as a
1999// backup for the RDIV test. Note that i and j can be the same variable,
2000// letting this test serve as a backup for the various SIV tests.
2001//
2002// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
2003// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2004// loop bounds for the i and j loops, respectively. So, ...
2005//
2006// c1 + a1*i = c2 + a2*j
2007// a1*i - a2*j = c2 - c1
2008//
2009// To test for a dependence, we compute c2 - c1 and make sure it's in the
2010// range of the maximum and minimum possible values of a1*i - a2*j.
2011// Considering the signs of a1 and a2, we have 4 possible cases:
2012//
2013// 1) If a1 >= 0 and a2 >= 0, then
2014// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2015// -a2*N2 <= c2 - c1 <= a1*N1
2016//
2017// 2) If a1 >= 0 and a2 <= 0, then
2018// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2019// 0 <= c2 - c1 <= a1*N1 - a2*N2
2020//
2021// 3) If a1 <= 0 and a2 >= 0, then
2022// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2023// a1*N1 - a2*N2 <= c2 - c1 <= 0
2024//
2025// 4) If a1 <= 0 and a2 <= 0, then
2026// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2027// a1*N1 <= c2 - c1 <= -a2*N2
2028//
2029// return true if dependence disproved
Chandler Carruth49c22192016-05-12 22:19:39 +00002030bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
2031 const SCEV *C1, const SCEV *C2,
2032 const Loop *Loop1,
2033 const Loop *Loop2) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002034 ++SymbolicRDIVapplications;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002035 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
2036 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1);
2037 LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
2038 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
2039 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
2040 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002041 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2042 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002043 LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
2044 LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002045 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2046 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002047 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
2048 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002049 if (SE->isKnownNonNegative(A1)) {
2050 if (SE->isKnownNonNegative(A2)) {
2051 // A1 >= 0 && A2 >= 0
2052 if (N1) {
2053 // make sure that c2 - c1 <= a1*N1
2054 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002055 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002056 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2057 ++SymbolicRDIVindependence;
2058 return true;
2059 }
2060 }
2061 if (N2) {
2062 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2063 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002064 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002065 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2066 ++SymbolicRDIVindependence;
2067 return true;
2068 }
2069 }
2070 }
2071 else if (SE->isKnownNonPositive(A2)) {
2072 // a1 >= 0 && a2 <= 0
2073 if (N1 && N2) {
2074 // make sure that c2 - c1 <= a1*N1 - a2*N2
2075 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2076 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2077 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002078 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002079 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2080 ++SymbolicRDIVindependence;
2081 return true;
2082 }
2083 }
2084 // make sure that 0 <= c2 - c1
2085 if (SE->isKnownNegative(C2_C1)) {
2086 ++SymbolicRDIVindependence;
2087 return true;
2088 }
2089 }
2090 }
2091 else if (SE->isKnownNonPositive(A1)) {
2092 if (SE->isKnownNonNegative(A2)) {
2093 // a1 <= 0 && a2 >= 0
2094 if (N1 && N2) {
2095 // make sure that a1*N1 - a2*N2 <= c2 - c1
2096 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2097 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2098 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002099 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002100 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2101 ++SymbolicRDIVindependence;
2102 return true;
2103 }
2104 }
2105 // make sure that c2 - c1 <= 0
2106 if (SE->isKnownPositive(C2_C1)) {
2107 ++SymbolicRDIVindependence;
2108 return true;
2109 }
2110 }
2111 else if (SE->isKnownNonPositive(A2)) {
2112 // a1 <= 0 && a2 <= 0
2113 if (N1) {
2114 // make sure that a1*N1 <= c2 - c1
2115 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002116 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002117 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2118 ++SymbolicRDIVindependence;
2119 return true;
2120 }
2121 }
2122 if (N2) {
2123 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2124 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002125 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002126 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2127 ++SymbolicRDIVindependence;
2128 return true;
2129 }
2130 }
2131 }
2132 }
2133 return false;
2134}
2135
2136
2137// testSIV -
2138// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2139// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2140// a2 are constant, we attack it with an SIV test. While they can all be
2141// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2142// they apply; they're cheaper and sometimes more precise.
2143//
2144// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002145bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2146 FullDependence &Result, Constraint &NewConstraint,
2147 const SCEV *&SplitIter) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002148 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2149 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002150 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2151 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2152 if (SrcAddRec && DstAddRec) {
2153 const SCEV *SrcConst = SrcAddRec->getStart();
2154 const SCEV *DstConst = DstAddRec->getStart();
2155 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2156 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2157 const Loop *CurLoop = SrcAddRec->getLoop();
2158 assert(CurLoop == DstAddRec->getLoop() &&
2159 "both loops in SIV should be same");
2160 Level = mapSrcLoop(CurLoop);
2161 bool disproven;
2162 if (SrcCoeff == DstCoeff)
2163 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2164 Level, Result, NewConstraint);
2165 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2166 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2167 Level, Result, NewConstraint, SplitIter);
2168 else
2169 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2170 Level, Result, NewConstraint);
2171 return disproven ||
2172 gcdMIVtest(Src, Dst, Result) ||
2173 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
2174 }
2175 if (SrcAddRec) {
2176 const SCEV *SrcConst = SrcAddRec->getStart();
2177 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2178 const SCEV *DstConst = Dst;
2179 const Loop *CurLoop = SrcAddRec->getLoop();
2180 Level = mapSrcLoop(CurLoop);
2181 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2182 Level, Result, NewConstraint) ||
2183 gcdMIVtest(Src, Dst, Result);
2184 }
2185 if (DstAddRec) {
2186 const SCEV *DstConst = DstAddRec->getStart();
2187 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2188 const SCEV *SrcConst = Src;
2189 const Loop *CurLoop = DstAddRec->getLoop();
2190 Level = mapDstLoop(CurLoop);
2191 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
2192 CurLoop, Level, Result, NewConstraint) ||
2193 gcdMIVtest(Src, Dst, Result);
2194 }
2195 llvm_unreachable("SIV test expected at least one AddRec");
2196 return false;
2197}
2198
2199
2200// testRDIV -
2201// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2202// where i and j are induction variables, c1 and c2 are loop invariant,
2203// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2204// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2205// It doesn't make sense to talk about distance or direction in this case,
2206// so there's no point in making special versions of the Strong SIV test or
2207// the Weak-crossing SIV test.
2208//
2209// With minor algebra, this test can also be used for things like
2210// [c1 + a1*i + a2*j][c2].
2211//
2212// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002213bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2214 FullDependence &Result) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002215 // we have 3 possible situations here:
2216 // 1) [a*i + b] and [c*j + d]
2217 // 2) [a*i + c*j + b] and [d]
2218 // 3) [b] and [a*i + c*j + d]
2219 // We need to find what we've got and get organized
2220
2221 const SCEV *SrcConst, *DstConst;
2222 const SCEV *SrcCoeff, *DstCoeff;
2223 const Loop *SrcLoop, *DstLoop;
2224
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002225 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2226 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002227 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2228 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2229 if (SrcAddRec && DstAddRec) {
2230 SrcConst = SrcAddRec->getStart();
2231 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2232 SrcLoop = SrcAddRec->getLoop();
2233 DstConst = DstAddRec->getStart();
2234 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2235 DstLoop = DstAddRec->getLoop();
2236 }
2237 else if (SrcAddRec) {
2238 if (const SCEVAddRecExpr *tmpAddRec =
2239 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2240 SrcConst = tmpAddRec->getStart();
2241 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2242 SrcLoop = tmpAddRec->getLoop();
2243 DstConst = Dst;
2244 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2245 DstLoop = SrcAddRec->getLoop();
2246 }
2247 else
2248 llvm_unreachable("RDIV reached by surprising SCEVs");
2249 }
2250 else if (DstAddRec) {
2251 if (const SCEVAddRecExpr *tmpAddRec =
2252 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2253 DstConst = tmpAddRec->getStart();
2254 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2255 DstLoop = tmpAddRec->getLoop();
2256 SrcConst = Src;
2257 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2258 SrcLoop = DstAddRec->getLoop();
2259 }
2260 else
2261 llvm_unreachable("RDIV reached by surprising SCEVs");
2262 }
2263 else
2264 llvm_unreachable("RDIV expected at least one AddRec");
2265 return exactRDIVtest(SrcCoeff, DstCoeff,
2266 SrcConst, DstConst,
2267 SrcLoop, DstLoop,
2268 Result) ||
2269 gcdMIVtest(Src, Dst, Result) ||
2270 symbolicRDIVtest(SrcCoeff, DstCoeff,
2271 SrcConst, DstConst,
2272 SrcLoop, DstLoop);
2273}
2274
2275
2276// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2277// Return true if dependence disproved.
2278// Can sometimes refine direction vectors.
Chandler Carruth49c22192016-05-12 22:19:39 +00002279bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2280 const SmallBitVector &Loops,
2281 FullDependence &Result) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002282 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2283 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002284 Result.Consistent = false;
2285 return gcdMIVtest(Src, Dst, Result) ||
2286 banerjeeMIVtest(Src, Dst, Loops, Result);
2287}
2288
2289
2290// Given a product, e.g., 10*X*Y, returns the first constant operand,
2291// in this case 10. If there is no constant part, returns NULL.
2292static
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002293const SCEVConstant *getConstantPart(const SCEV *Expr) {
2294 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2295 return Constant;
2296 else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2297 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
Sebastian Pop59b61b92012-10-11 07:32:34 +00002298 return Constant;
Craig Topper9f008862014-04-15 04:59:12 +00002299 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002300}
2301
2302
2303//===----------------------------------------------------------------------===//
2304// gcdMIVtest -
2305// Tests an MIV subscript pair for dependence.
2306// Returns true if any possible dependence is disproved.
Benjamin Kramerc914ab62012-10-31 11:25:32 +00002307// Marks the result as inconsistent.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002308// Can sometimes disprove the equal direction for 1 or more loops,
2309// as discussed in Michael Wolfe's book,
2310// High Performance Compilers for Parallel Computing, page 235.
2311//
2312// We spend some effort (code!) to handle cases like
2313// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2314// but M and N are just loop-invariant variables.
2315// This should help us handle linearized subscripts;
2316// also makes this test a useful backup to the various SIV tests.
2317//
2318// It occurs to me that the presence of loop-invariant variables
2319// changes the nature of the test from "greatest common divisor"
Preston Briggs4eb7ee52012-11-29 04:30:52 +00002320// to "a common divisor".
Chandler Carruth49c22192016-05-12 22:19:39 +00002321bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2322 FullDependence &Result) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002323 LLVM_DEBUG(dbgs() << "starting gcd\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002324 ++GCDapplications;
Preston Briggs3ad39492012-11-21 23:50:04 +00002325 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002326 APInt RunningGCD = APInt::getNullValue(BitWidth);
2327
2328 // Examine Src coefficients.
2329 // Compute running GCD and record source constant.
2330 // Because we're looking for the constant at the end of the chain,
2331 // we can't quit the loop just because the GCD == 1.
2332 const SCEV *Coefficients = Src;
2333 while (const SCEVAddRecExpr *AddRec =
2334 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2335 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002336 // If the coefficient is the product of a constant and other stuff,
2337 // we can use the constant in the GCD computation.
2338 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002339 if (!Constant)
2340 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002341 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002342 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2343 Coefficients = AddRec->getStart();
2344 }
2345 const SCEV *SrcConst = Coefficients;
2346
2347 // Examine Dst coefficients.
2348 // Compute running GCD and record destination constant.
2349 // Because we're looking for the constant at the end of the chain,
2350 // we can't quit the loop just because the GCD == 1.
2351 Coefficients = Dst;
2352 while (const SCEVAddRecExpr *AddRec =
2353 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2354 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002355 // If the coefficient is the product of a constant and other stuff,
2356 // we can use the constant in the GCD computation.
2357 const auto *Constant = getConstantPart(Coeff);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002358 if (!Constant)
2359 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002360 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002361 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2362 Coefficients = AddRec->getStart();
2363 }
2364 const SCEV *DstConst = Coefficients;
2365
2366 APInt ExtraGCD = APInt::getNullValue(BitWidth);
2367 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002368 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002369 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2370 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2371 // If Delta is a sum of products, we may be able to make further progress.
2372 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
2373 const SCEV *Operand = Sum->getOperand(Op);
2374 if (isa<SCEVConstant>(Operand)) {
2375 assert(!Constant && "Surprised to find multiple constants");
2376 Constant = cast<SCEVConstant>(Operand);
2377 }
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002378 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002379 // Search for constant operand to participate in GCD;
2380 // If none found; return false.
Benjamin Kramer24c643b2012-10-31 09:20:38 +00002381 const SCEVConstant *ConstOp = getConstantPart(Product);
2382 if (!ConstOp)
2383 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002384 APInt ConstOpValue = ConstOp->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002385 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2386 ConstOpValue.abs());
2387 }
2388 else
2389 return false;
2390 }
2391 }
2392 if (!Constant)
2393 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002394 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002395 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002396 if (ConstDelta == 0)
2397 return false;
2398 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002399 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002400 APInt Remainder = ConstDelta.srem(RunningGCD);
2401 if (Remainder != 0) {
2402 ++GCDindependence;
2403 return true;
2404 }
2405
2406 // Try to disprove equal directions.
2407 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2408 // the code above can't disprove the dependence because the GCD = 1.
2409 // So we consider what happen if i = i' and what happens if j = j'.
2410 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2411 // which is infeasible, so we can disallow the = direction for the i level.
2412 // Setting j = j' doesn't help matters, so we end up with a direction vector
2413 // of [<>, *]
2414 //
2415 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2416 // we need to remember that the constant part is 5 and the RunningGCD should
2417 // be initialized to ExtraGCD = 30.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002418 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002419
2420 bool Improved = false;
2421 Coefficients = Src;
2422 while (const SCEVAddRecExpr *AddRec =
2423 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2424 Coefficients = AddRec->getStart();
2425 const Loop *CurLoop = AddRec->getLoop();
2426 RunningGCD = ExtraGCD;
2427 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2428 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2429 const SCEV *Inner = Src;
2430 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2431 AddRec = cast<SCEVAddRecExpr>(Inner);
2432 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2433 if (CurLoop == AddRec->getLoop())
2434 ; // SrcCoeff == Coeff
2435 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002436 // If the coefficient is the product of a constant and other stuff,
2437 // we can use the constant in the GCD computation.
2438 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002439 if (!Constant)
2440 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002441 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002442 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2443 }
2444 Inner = AddRec->getStart();
2445 }
2446 Inner = Dst;
2447 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2448 AddRec = cast<SCEVAddRecExpr>(Inner);
2449 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2450 if (CurLoop == AddRec->getLoop())
2451 DstCoeff = Coeff;
2452 else {
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002453 // If the coefficient is the product of a constant and other stuff,
2454 // we can use the constant in the GCD computation.
2455 Constant = getConstantPart(Coeff);
Brendon Cahoon86f783e2016-04-04 18:13:18 +00002456 if (!Constant)
2457 return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002458 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002459 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2460 }
2461 Inner = AddRec->getStart();
2462 }
2463 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
Brendon Cahoonbe2da822016-04-19 16:46:57 +00002464 // If the coefficient is the product of a constant and other stuff,
2465 // we can use the constant in the GCD computation.
2466 Constant = getConstantPart(Delta);
2467 if (!Constant)
Sebastian Pop59b61b92012-10-11 07:32:34 +00002468 // The difference of the two coefficients might not be a product
2469 // or constant, in which case we give up on this direction.
2470 continue;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002471 APInt ConstCoeff = Constant->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00002472 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002473 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002474 if (RunningGCD != 0) {
2475 Remainder = ConstDelta.srem(RunningGCD);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002476 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002477 if (Remainder != 0) {
2478 unsigned Level = mapSrcLoop(CurLoop);
Sebastian Pope96232612012-10-12 02:04:32 +00002479 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002480 Improved = true;
2481 }
2482 }
2483 }
2484 if (Improved)
2485 ++GCDsuccesses;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002486 LLVM_DEBUG(dbgs() << "all done\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002487 return false;
2488}
2489
2490
2491//===----------------------------------------------------------------------===//
2492// banerjeeMIVtest -
2493// Use Banerjee's Inequalities to test an MIV subscript pair.
2494// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2495// Generally follows the discussion in Section 2.5.2 of
2496//
2497// Optimizing Supercompilers for Supercomputers
2498// Michael Wolfe
2499//
2500// The inequalities given on page 25 are simplified in that loops are
2501// normalized so that the lower bound is always 0 and the stride is always 1.
2502// For example, Wolfe gives
2503//
2504// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2505//
2506// where A_k is the coefficient of the kth index in the source subscript,
2507// B_k is the coefficient of the kth index in the destination subscript,
2508// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2509// index, and N_k is the stride of the kth index. Since all loops are normalized
2510// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2511// equation to
2512//
2513// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2514// = (A^-_k - B_k)^- (U_k - 1) - B_k
2515//
2516// Similar simplifications are possible for the other equations.
2517//
2518// When we can't determine the number of iterations for a loop,
2519// we use NULL as an indicator for the worst case, infinity.
2520// When computing the upper bound, NULL denotes +inf;
2521// for the lower bound, NULL denotes -inf.
2522//
2523// Return true if dependence disproved.
Chandler Carruth49c22192016-05-12 22:19:39 +00002524bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2525 const SmallBitVector &Loops,
2526 FullDependence &Result) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002527 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002528 ++BanerjeeApplications;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002529 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002530 const SCEV *A0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002531 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002532 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002533 const SCEV *B0;
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002534 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2535 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002536 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002537 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002538
2539 // Compute bounds for all the * directions.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002540 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002541 for (unsigned K = 1; K <= MaxLevels; ++K) {
2542 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2543 Bound[K].Direction = Dependence::DVEntry::ALL;
2544 Bound[K].DirSet = Dependence::DVEntry::NONE;
2545 findBoundsALL(A, B, Bound, K);
2546#ifndef NDEBUG
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002547 LLVM_DEBUG(dbgs() << "\t " << K << '\t');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002548 if (Bound[K].Lower[Dependence::DVEntry::ALL])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002549 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002550 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002551 LLVM_DEBUG(dbgs() << "-inf\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002552 if (Bound[K].Upper[Dependence::DVEntry::ALL])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002553 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002554 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002555 LLVM_DEBUG(dbgs() << "+inf\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002556#endif
2557 }
2558
2559 // Test the *, *, *, ... case.
2560 bool Disproved = false;
2561 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2562 // Explore the direction vector hierarchy.
2563 unsigned DepthExpanded = 0;
2564 unsigned NewDeps = exploreDirections(1, A, B, Bound,
2565 Loops, DepthExpanded, Delta);
2566 if (NewDeps > 0) {
2567 bool Improved = false;
2568 for (unsigned K = 1; K <= CommonLevels; ++K) {
2569 if (Loops[K]) {
2570 unsigned Old = Result.DV[K - 1].Direction;
2571 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2572 Improved |= Old != Result.DV[K - 1].Direction;
2573 if (!Result.DV[K - 1].Direction) {
2574 Improved = false;
2575 Disproved = true;
2576 break;
2577 }
2578 }
2579 }
2580 if (Improved)
2581 ++BanerjeeSuccesses;
2582 }
2583 else {
2584 ++BanerjeeIndependence;
2585 Disproved = true;
2586 }
2587 }
2588 else {
2589 ++BanerjeeIndependence;
2590 Disproved = true;
2591 }
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002592 delete [] Bound;
2593 delete [] A;
2594 delete [] B;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002595 return Disproved;
2596}
2597
2598
2599// Hierarchically expands the direction vector
2600// search space, combining the directions of discovered dependences
2601// in the DirSet field of Bound. Returns the number of distinct
2602// dependences discovered. If the dependence is disproved,
2603// it will return 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002604unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2605 CoefficientInfo *B, BoundInfo *Bound,
2606 const SmallBitVector &Loops,
2607 unsigned &DepthExpanded,
2608 const SCEV *Delta) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002609 if (Level > CommonLevels) {
2610 // record result
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002611 LLVM_DEBUG(dbgs() << "\t[");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002612 for (unsigned K = 1; K <= CommonLevels; ++K) {
2613 if (Loops[K]) {
2614 Bound[K].DirSet |= Bound[K].Direction;
2615#ifndef NDEBUG
2616 switch (Bound[K].Direction) {
2617 case Dependence::DVEntry::LT:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002618 LLVM_DEBUG(dbgs() << " <");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002619 break;
2620 case Dependence::DVEntry::EQ:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002621 LLVM_DEBUG(dbgs() << " =");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002622 break;
2623 case Dependence::DVEntry::GT:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002624 LLVM_DEBUG(dbgs() << " >");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002625 break;
2626 case Dependence::DVEntry::ALL:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002627 LLVM_DEBUG(dbgs() << " *");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002628 break;
2629 default:
2630 llvm_unreachable("unexpected Bound[K].Direction");
2631 }
2632#endif
2633 }
2634 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002635 LLVM_DEBUG(dbgs() << " ]\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002636 return 1;
2637 }
2638 if (Loops[Level]) {
2639 if (Level > DepthExpanded) {
2640 DepthExpanded = Level;
2641 // compute bounds for <, =, > at current level
2642 findBoundsLT(A, B, Bound, Level);
2643 findBoundsGT(A, B, Bound, Level);
2644 findBoundsEQ(A, B, Bound, Level);
2645#ifndef NDEBUG
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002646 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2647 LLVM_DEBUG(dbgs() << "\t <\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002648 if (Bound[Level].Lower[Dependence::DVEntry::LT])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002649 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
2650 << '\t');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002651 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002652 LLVM_DEBUG(dbgs() << "-inf\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002653 if (Bound[Level].Upper[Dependence::DVEntry::LT])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002654 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
2655 << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002656 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002657 LLVM_DEBUG(dbgs() << "+inf\n");
2658 LLVM_DEBUG(dbgs() << "\t =\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002659 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002660 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
2661 << '\t');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002662 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002663 LLVM_DEBUG(dbgs() << "-inf\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002664 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002665 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
2666 << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002667 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002668 LLVM_DEBUG(dbgs() << "+inf\n");
2669 LLVM_DEBUG(dbgs() << "\t >\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002670 if (Bound[Level].Lower[Dependence::DVEntry::GT])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002671 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
2672 << '\t');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002673 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002674 LLVM_DEBUG(dbgs() << "-inf\t");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002675 if (Bound[Level].Upper[Dependence::DVEntry::GT])
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002676 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
2677 << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002678 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002679 LLVM_DEBUG(dbgs() << "+inf\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002680#endif
2681 }
2682
2683 unsigned NewDeps = 0;
2684
2685 // test bounds for <, *, *, ...
2686 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2687 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2688 Loops, DepthExpanded, Delta);
2689
2690 // Test bounds for =, *, *, ...
2691 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2692 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2693 Loops, DepthExpanded, Delta);
2694
2695 // test bounds for >, *, *, ...
2696 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2697 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2698 Loops, DepthExpanded, Delta);
2699
2700 Bound[Level].Direction = Dependence::DVEntry::ALL;
2701 return NewDeps;
2702 }
2703 else
2704 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
2705}
2706
2707
2708// Returns true iff the current bounds are plausible.
Chandler Carruth49c22192016-05-12 22:19:39 +00002709bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2710 BoundInfo *Bound, const SCEV *Delta) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002711 Bound[Level].Direction = DirKind;
2712 if (const SCEV *LowerBound = getLowerBound(Bound))
2713 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2714 return false;
2715 if (const SCEV *UpperBound = getUpperBound(Bound))
2716 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2717 return false;
2718 return true;
2719}
2720
2721
2722// Computes the upper and lower bounds for level K
2723// using the * direction. Records them in Bound.
2724// Wolfe gives the equations
2725//
2726// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2727// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2728//
2729// Since we normalize loops, we can simplify these equations to
2730//
2731// LB^*_k = (A^-_k - B^+_k)U_k
2732// UB^*_k = (A^+_k - B^-_k)U_k
2733//
2734// We must be careful to handle the case where the upper bound is unknown.
2735// Note that the lower bound is always <= 0
2736// and the upper bound is always >= 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002737void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2738 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002739 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
2740 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002741 if (Bound[K].Iterations) {
2742 Bound[K].Lower[Dependence::DVEntry::ALL] =
2743 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
2744 Bound[K].Iterations);
2745 Bound[K].Upper[Dependence::DVEntry::ALL] =
2746 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
2747 Bound[K].Iterations);
2748 }
2749 else {
2750 // If the difference is 0, we won't need to know the number of iterations.
2751 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2752 Bound[K].Lower[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002753 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002754 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2755 Bound[K].Upper[Dependence::DVEntry::ALL] =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002756 SE->getZero(A[K].Coeff->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002757 }
2758}
2759
2760
2761// Computes the upper and lower bounds for level K
2762// using the = direction. Records them in Bound.
2763// Wolfe gives the equations
2764//
2765// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2766// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2767//
2768// Since we normalize loops, we can simplify these equations to
2769//
2770// LB^=_k = (A_k - B_k)^- U_k
2771// UB^=_k = (A_k - B_k)^+ U_k
2772//
2773// We must be careful to handle the case where the upper bound is unknown.
2774// Note that the lower bound is always <= 0
2775// and the upper bound is always >= 0.
Chandler Carruth49c22192016-05-12 22:19:39 +00002776void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2777 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002778 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
2779 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002780 if (Bound[K].Iterations) {
2781 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2782 const SCEV *NegativePart = getNegativePart(Delta);
2783 Bound[K].Lower[Dependence::DVEntry::EQ] =
2784 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2785 const SCEV *PositivePart = getPositivePart(Delta);
2786 Bound[K].Upper[Dependence::DVEntry::EQ] =
2787 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2788 }
2789 else {
2790 // If the positive/negative part of the difference is 0,
2791 // we won't need to know the number of iterations.
2792 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2793 const SCEV *NegativePart = getNegativePart(Delta);
2794 if (NegativePart->isZero())
2795 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2796 const SCEV *PositivePart = getPositivePart(Delta);
2797 if (PositivePart->isZero())
2798 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2799 }
2800}
2801
2802
2803// Computes the upper and lower bounds for level K
2804// using the < direction. Records them in Bound.
2805// Wolfe gives the equations
2806//
2807// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2808// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2809//
2810// Since we normalize loops, we can simplify these equations to
2811//
2812// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2813// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2814//
2815// We must be careful to handle the case where the upper bound is unknown.
Chandler Carruth49c22192016-05-12 22:19:39 +00002816void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
2817 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002818 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
2819 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002820 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002821 const SCEV *Iter_1 = SE->getMinusSCEV(
2822 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002823 const SCEV *NegPart =
2824 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2825 Bound[K].Lower[Dependence::DVEntry::LT] =
2826 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2827 const SCEV *PosPart =
2828 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2829 Bound[K].Upper[Dependence::DVEntry::LT] =
2830 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2831 }
2832 else {
2833 // If the positive/negative part of the difference is 0,
2834 // we won't need to know the number of iterations.
2835 const SCEV *NegPart =
2836 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2837 if (NegPart->isZero())
2838 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2839 const SCEV *PosPart =
2840 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2841 if (PosPart->isZero())
2842 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2843 }
2844}
2845
2846
2847// Computes the upper and lower bounds for level K
2848// using the > direction. Records them in Bound.
2849// Wolfe gives the equations
2850//
2851// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2852// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2853//
2854// Since we normalize loops, we can simplify these equations to
2855//
2856// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2857// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2858//
2859// We must be careful to handle the case where the upper bound is unknown.
Chandler Carruth49c22192016-05-12 22:19:39 +00002860void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
2861 BoundInfo *Bound, unsigned K) const {
Craig Topper9f008862014-04-15 04:59:12 +00002862 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
2863 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
Sebastian Pop59b61b92012-10-11 07:32:34 +00002864 if (Bound[K].Iterations) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002865 const SCEV *Iter_1 = SE->getMinusSCEV(
2866 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002867 const SCEV *NegPart =
2868 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2869 Bound[K].Lower[Dependence::DVEntry::GT] =
2870 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2871 const SCEV *PosPart =
2872 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2873 Bound[K].Upper[Dependence::DVEntry::GT] =
2874 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2875 }
2876 else {
2877 // If the positive/negative part of the difference is 0,
2878 // we won't need to know the number of iterations.
2879 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2880 if (NegPart->isZero())
2881 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2882 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2883 if (PosPart->isZero())
2884 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2885 }
2886}
2887
2888
2889// X^+ = max(X, 0)
Chandler Carruth49c22192016-05-12 22:19:39 +00002890const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002891 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002892}
2893
2894
2895// X^- = min(X, 0)
Chandler Carruth49c22192016-05-12 22:19:39 +00002896const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002897 return SE->getSMinExpr(X, SE->getZero(X->getType()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00002898}
2899
2900
2901// Walks through the subscript,
2902// collecting each coefficient, the associated loop bounds,
2903// and recording its positive and negative parts for later use.
Chandler Carruth49c22192016-05-12 22:19:39 +00002904DependenceInfo::CoefficientInfo *
2905DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
2906 const SCEV *&Constant) const {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002907 const SCEV *Zero = SE->getZero(Subscript->getType());
Dylan Noblesmith4ffafef2014-08-26 02:03:38 +00002908 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
Sebastian Pop59b61b92012-10-11 07:32:34 +00002909 for (unsigned K = 1; K <= MaxLevels; ++K) {
2910 CI[K].Coeff = Zero;
2911 CI[K].PosPart = Zero;
2912 CI[K].NegPart = Zero;
Craig Topper9f008862014-04-15 04:59:12 +00002913 CI[K].Iterations = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002914 }
2915 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2916 const Loop *L = AddRec->getLoop();
2917 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2918 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2919 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2920 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2921 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2922 Subscript = AddRec->getStart();
2923 }
2924 Constant = Subscript;
2925#ifndef NDEBUG
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002926 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002927 for (unsigned K = 1; K <= MaxLevels; ++K) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002928 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
2929 LLVM_DEBUG(dbgs() << "\tPos Part = ");
2930 LLVM_DEBUG(dbgs() << *CI[K].PosPart);
2931 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
2932 LLVM_DEBUG(dbgs() << *CI[K].NegPart);
2933 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
Sebastian Pop59b61b92012-10-11 07:32:34 +00002934 if (CI[K].Iterations)
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002935 LLVM_DEBUG(dbgs() << *CI[K].Iterations);
Sebastian Pop59b61b92012-10-11 07:32:34 +00002936 else
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002937 LLVM_DEBUG(dbgs() << "+inf");
2938 LLVM_DEBUG(dbgs() << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002939 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002940 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
Sebastian Pop59b61b92012-10-11 07:32:34 +00002941#endif
2942 return CI;
2943}
2944
2945
2946// Looks through all the bounds info and
2947// computes the lower bound given the current direction settings
2948// at each level. If the lower bound for any level is -inf,
2949// the result is -inf.
Chandler Carruth49c22192016-05-12 22:19:39 +00002950const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002951 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2952 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2953 if (Bound[K].Lower[Bound[K].Direction])
2954 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2955 else
Craig Topper9f008862014-04-15 04:59:12 +00002956 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002957 }
2958 return Sum;
2959}
2960
2961
2962// Looks through all the bounds info and
2963// computes the upper bound given the current direction settings
2964// at each level. If the upper bound at any level is +inf,
2965// the result is +inf.
Chandler Carruth49c22192016-05-12 22:19:39 +00002966const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002967 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2968 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2969 if (Bound[K].Upper[Bound[K].Direction])
2970 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2971 else
Craig Topper9f008862014-04-15 04:59:12 +00002972 Sum = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00002973 }
2974 return Sum;
2975}
2976
2977
2978//===----------------------------------------------------------------------===//
2979// Constraint manipulation for Delta test.
2980
2981// Given a linear SCEV,
2982// return the coefficient (the step)
2983// corresponding to the specified loop.
2984// If there isn't one, return 0.
Jingyue Wua84feb12015-05-29 16:58:08 +00002985// For example, given a*i + b*j + c*k, finding the coefficient
Sebastian Pop59b61b92012-10-11 07:32:34 +00002986// corresponding to the j loop would yield b.
Chandler Carruth49c22192016-05-12 22:19:39 +00002987const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
2988 const Loop *TargetLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00002989 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2990 if (!AddRec)
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002991 return SE->getZero(Expr->getType());
Sebastian Pop59b61b92012-10-11 07:32:34 +00002992 if (AddRec->getLoop() == TargetLoop)
2993 return AddRec->getStepRecurrence(*SE);
2994 return findCoefficient(AddRec->getStart(), TargetLoop);
2995}
2996
2997
2998// Given a linear SCEV,
2999// return the SCEV given by zeroing out the coefficient
3000// corresponding to the specified loop.
3001// For example, given a*i + b*j + c*k, zeroing the coefficient
3002// corresponding to the j loop would yield a*i + c*k.
Chandler Carruth49c22192016-05-12 22:19:39 +00003003const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
3004 const Loop *TargetLoop) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003005 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3006 if (!AddRec)
3007 return Expr; // ignore
3008 if (AddRec->getLoop() == TargetLoop)
3009 return AddRec->getStart();
3010 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
3011 AddRec->getStepRecurrence(*SE),
3012 AddRec->getLoop(),
3013 AddRec->getNoWrapFlags());
3014}
3015
3016
3017// Given a linear SCEV Expr,
3018// return the SCEV given by adding some Value to the
3019// coefficient corresponding to the specified TargetLoop.
3020// For example, given a*i + b*j + c*k, adding 1 to the coefficient
3021// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
Chandler Carruth49c22192016-05-12 22:19:39 +00003022const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
3023 const Loop *TargetLoop,
3024 const SCEV *Value) const {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003025 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3026 if (!AddRec) // create a new addRec
3027 return SE->getAddRecExpr(Expr,
3028 Value,
3029 TargetLoop,
3030 SCEV::FlagAnyWrap); // Worst case, with no info.
3031 if (AddRec->getLoop() == TargetLoop) {
3032 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
3033 if (Sum->isZero())
3034 return AddRec->getStart();
3035 return SE->getAddRecExpr(AddRec->getStart(),
3036 Sum,
3037 AddRec->getLoop(),
3038 AddRec->getNoWrapFlags());
3039 }
Preston Briggs6c286b62013-06-28 18:44:48 +00003040 if (SE->isLoopInvariant(AddRec, TargetLoop))
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +00003041 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
3042 return SE->getAddRecExpr(
3043 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
3044 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3045 AddRec->getNoWrapFlags());
Sebastian Pop59b61b92012-10-11 07:32:34 +00003046}
3047
3048
3049// Review the constraints, looking for opportunities
3050// to simplify a subscript pair (Src and Dst).
3051// Return true if some simplification occurs.
3052// If the simplification isn't exact (that is, if it is conservative
3053// in terms of dependence), set consistent to false.
3054// Corresponds to Figure 5 from the paper
3055//
3056// Practical Dependence Testing
3057// Goff, Kennedy, Tseng
3058// PLDI 1991
Chandler Carruth49c22192016-05-12 22:19:39 +00003059bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
3060 SmallBitVector &Loops,
3061 SmallVectorImpl<Constraint> &Constraints,
3062 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003063 bool Result = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003064 for (unsigned LI : Loops.set_bits()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003065 LLVM_DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
3066 LLVM_DEBUG(Constraints[LI].dump(dbgs()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003067 if (Constraints[LI].isDistance())
3068 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3069 else if (Constraints[LI].isLine())
3070 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3071 else if (Constraints[LI].isPoint())
3072 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3073 }
3074 return Result;
3075}
3076
3077
3078// Attempt to propagate a distance
3079// constraint into a subscript pair (Src and Dst).
3080// Return true if some simplification occurs.
3081// If the simplification isn't exact (that is, if it is conservative
3082// in terms of dependence), set consistent to false.
Chandler Carruth49c22192016-05-12 22:19:39 +00003083bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
3084 Constraint &CurConstraint,
3085 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003086 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003087 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003088 const SCEV *A_K = findCoefficient(Src, CurLoop);
3089 if (A_K->isZero())
3090 return false;
3091 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3092 Src = SE->getMinusSCEV(Src, DA_K);
3093 Src = zeroCoefficient(Src, CurLoop);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003094 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3095 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003096 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003097 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003098 if (!findCoefficient(Dst, CurLoop)->isZero())
3099 Consistent = false;
3100 return true;
3101}
3102
3103
3104// Attempt to propagate a line
3105// constraint into a subscript pair (Src and Dst).
3106// Return true if some simplification occurs.
3107// If the simplification isn't exact (that is, if it is conservative
3108// in terms of dependence), set consistent to false.
Chandler Carruth49c22192016-05-12 22:19:39 +00003109bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
3110 Constraint &CurConstraint,
3111 bool &Consistent) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003112 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3113 const SCEV *A = CurConstraint.getA();
3114 const SCEV *B = CurConstraint.getB();
3115 const SCEV *C = CurConstraint.getC();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003116 LLVM_DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C
3117 << "\n");
3118 LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3119 LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003120 if (A->isZero()) {
3121 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3122 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3123 if (!Bconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003124 APInt Beta = Bconst->getAPInt();
3125 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003126 APInt CdivB = Charlie.sdiv(Beta);
3127 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3128 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3129 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3130 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3131 Dst = zeroCoefficient(Dst, CurLoop);
3132 if (!findCoefficient(Src, CurLoop)->isZero())
3133 Consistent = false;
3134 }
3135 else if (B->isZero()) {
3136 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3137 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3138 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003139 APInt Alpha = Aconst->getAPInt();
3140 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003141 APInt CdivA = Charlie.sdiv(Alpha);
3142 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3143 const SCEV *A_K = findCoefficient(Src, CurLoop);
3144 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3145 Src = zeroCoefficient(Src, CurLoop);
3146 if (!findCoefficient(Dst, CurLoop)->isZero())
3147 Consistent = false;
3148 }
3149 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3150 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3151 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3152 if (!Aconst || !Cconst) return false;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003153 APInt Alpha = Aconst->getAPInt();
3154 APInt Charlie = Cconst->getAPInt();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003155 APInt CdivA = Charlie.sdiv(Alpha);
3156 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3157 const SCEV *A_K = findCoefficient(Src, CurLoop);
3158 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3159 Src = zeroCoefficient(Src, CurLoop);
3160 Dst = addToCoefficient(Dst, CurLoop, A_K);
3161 if (!findCoefficient(Dst, CurLoop)->isZero())
3162 Consistent = false;
3163 }
3164 else {
3165 // paper is incorrect here, or perhaps just misleading
3166 const SCEV *A_K = findCoefficient(Src, CurLoop);
3167 Src = SE->getMulExpr(Src, A);
3168 Dst = SE->getMulExpr(Dst, A);
3169 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3170 Src = zeroCoefficient(Src, CurLoop);
3171 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3172 if (!findCoefficient(Dst, CurLoop)->isZero())
3173 Consistent = false;
3174 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003175 LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3176 LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003177 return true;
3178}
3179
3180
3181// Attempt to propagate a point
3182// constraint into a subscript pair (Src and Dst).
3183// Return true if some simplification occurs.
Chandler Carruth49c22192016-05-12 22:19:39 +00003184bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
3185 Constraint &CurConstraint) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003186 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3187 const SCEV *A_K = findCoefficient(Src, CurLoop);
3188 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3189 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3190 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003191 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003192 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3193 Src = zeroCoefficient(Src, CurLoop);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003194 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3195 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003196 Dst = zeroCoefficient(Dst, CurLoop);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003197 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003198 return true;
3199}
3200
3201
3202// Update direction vector entry based on the current constraint.
Chandler Carruth49c22192016-05-12 22:19:39 +00003203void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
3204 const Constraint &CurConstraint) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003205 LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =");
3206 LLVM_DEBUG(CurConstraint.dump(dbgs()));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003207 if (CurConstraint.isAny())
3208 ; // use defaults
3209 else if (CurConstraint.isDistance()) {
3210 // this one is consistent, the others aren't
3211 Level.Scalar = false;
3212 Level.Distance = CurConstraint.getD();
3213 unsigned NewDirection = Dependence::DVEntry::NONE;
3214 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3215 NewDirection = Dependence::DVEntry::EQ;
3216 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3217 NewDirection |= Dependence::DVEntry::LT;
3218 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3219 NewDirection |= Dependence::DVEntry::GT;
3220 Level.Direction &= NewDirection;
3221 }
3222 else if (CurConstraint.isLine()) {
3223 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003224 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003225 // direction should be accurate
3226 }
3227 else if (CurConstraint.isPoint()) {
3228 Level.Scalar = false;
Craig Topper9f008862014-04-15 04:59:12 +00003229 Level.Distance = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003230 unsigned NewDirection = Dependence::DVEntry::NONE;
3231 if (!isKnownPredicate(CmpInst::ICMP_NE,
3232 CurConstraint.getY(),
3233 CurConstraint.getX()))
3234 // if X may be = Y
3235 NewDirection |= Dependence::DVEntry::EQ;
3236 if (!isKnownPredicate(CmpInst::ICMP_SLE,
3237 CurConstraint.getY(),
3238 CurConstraint.getX()))
3239 // if Y may be > X
3240 NewDirection |= Dependence::DVEntry::LT;
3241 if (!isKnownPredicate(CmpInst::ICMP_SGE,
3242 CurConstraint.getY(),
3243 CurConstraint.getX()))
3244 // if Y may be < X
3245 NewDirection |= Dependence::DVEntry::GT;
3246 Level.Direction &= NewDirection;
3247 }
3248 else
3249 llvm_unreachable("constraint has unexpected kind");
3250}
3251
Sebastian Popc62c6792013-11-12 22:47:20 +00003252/// Check if we can delinearize the subscripts. If the SCEVs representing the
3253/// source and destination array references are recurrences on a nested loop,
Alp Tokercb402912014-01-24 17:20:08 +00003254/// this function flattens the nested recurrences into separate recurrences
Sebastian Popc62c6792013-11-12 22:47:20 +00003255/// for each loop level.
Chandler Carruth49c22192016-05-12 22:19:39 +00003256bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3257 SmallVectorImpl<Subscript> &Pair) {
Renato Golin038ede22018-03-09 21:05:58 +00003258 assert(isLoadOrStore(Src) && "instruction is not load or store");
3259 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3260 Value *SrcPtr = getLoadStorePointerOperand(Src);
3261 Value *DstPtr = getLoadStorePointerOperand(Dst);
Hal Finkel0ef2b102015-08-19 02:56:36 +00003262
3263 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3264 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3265
3266 // Below code mimics the code in Delinearization.cpp
3267 const SCEV *SrcAccessFn =
3268 SE->getSCEVAtScope(SrcPtr, SrcLoop);
3269 const SCEV *DstAccessFn =
3270 SE->getSCEVAtScope(DstPtr, DstLoop);
3271
Sebastian Pop28e6b972014-05-27 22:41:51 +00003272 const SCEVUnknown *SrcBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003273 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003274 const SCEVUnknown *DstBase =
Hal Finkel0ef2b102015-08-19 02:56:36 +00003275 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
Sebastian Pop28e6b972014-05-27 22:41:51 +00003276
3277 if (!SrcBase || !DstBase || SrcBase != DstBase)
3278 return false;
3279
Hal Finkel0ef2b102015-08-19 02:56:36 +00003280 const SCEV *ElementSize = SE->getElementSize(Src);
3281 if (ElementSize != SE->getElementSize(Dst))
3282 return false;
3283
3284 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3285 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
Sebastian Pop28e6b972014-05-27 22:41:51 +00003286
Sebastian Popc62c6792013-11-12 22:47:20 +00003287 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3288 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3289 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3290 return false;
3291
Sebastian Pop448712b2014-05-07 18:01:20 +00003292 // First step: collect parametric terms in both array references.
3293 SmallVector<const SCEV *, 4> Terms;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003294 SE->collectParametricTerms(SrcAR, Terms);
3295 SE->collectParametricTerms(DstAR, Terms);
Sebastian Popc62c6792013-11-12 22:47:20 +00003296
Sebastian Pop448712b2014-05-07 18:01:20 +00003297 // Second step: find subscript sizes.
3298 SmallVector<const SCEV *, 4> Sizes;
Sebastian Popa6e58602014-05-27 22:41:45 +00003299 SE->findArrayDimensions(Terms, Sizes, ElementSize);
Sebastian Pop448712b2014-05-07 18:01:20 +00003300
3301 // Third step: compute the access functions for each subscript.
3302 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +00003303 SE->computeAccessFunctions(SrcAR, SrcSubscripts, Sizes);
3304 SE->computeAccessFunctions(DstAR, DstSubscripts, Sizes);
Sebastian Pop448712b2014-05-07 18:01:20 +00003305
Sebastian Pop5133d2e2014-02-21 18:15:07 +00003306 // Fail when there is only a subscript: that's a linearized access function.
Sebastian Pop448712b2014-05-07 18:01:20 +00003307 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3308 SrcSubscripts.size() != DstSubscripts.size())
Sebastian Popc62c6792013-11-12 22:47:20 +00003309 return false;
3310
Sebastian Pop448712b2014-05-07 18:01:20 +00003311 int size = SrcSubscripts.size();
Sebastian Pop29026d32014-02-21 18:15:11 +00003312
David Greend143c652018-06-21 11:53:16 +00003313 // Statically check that the array bounds are in-range. The first subscript we
3314 // don't have a size for and it cannot overflow into another subscript, so is
3315 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3316 // and dst.
3317 // FIXME: It may be better to record these sizes and add them as constraints
3318 // to the dependency checks.
3319 for (int i = 1; i < size; ++i) {
David Green86994922018-06-25 15:13:26 +00003320 if (!isKnownNonNegative(SrcSubscripts[i], SrcPtr))
David Greend143c652018-06-21 11:53:16 +00003321 return false;
3322
3323 if (!isKnownLessThan(SrcSubscripts[i], Sizes[i - 1]))
3324 return false;
3325
David Green86994922018-06-25 15:13:26 +00003326 if (!isKnownNonNegative(DstSubscripts[i], DstPtr))
David Greend143c652018-06-21 11:53:16 +00003327 return false;
3328
3329 if (!isKnownLessThan(DstSubscripts[i], Sizes[i - 1]))
3330 return false;
3331 }
3332
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003333 LLVM_DEBUG({
3334 dbgs() << "\nSrcSubscripts: ";
Sebastian Pop448712b2014-05-07 18:01:20 +00003335 for (int i = 0; i < size; i++)
3336 dbgs() << *SrcSubscripts[i];
3337 dbgs() << "\nDstSubscripts: ";
3338 for (int i = 0; i < size; i++)
3339 dbgs() << *DstSubscripts[i];
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003340 });
Sebastian Popc62c6792013-11-12 22:47:20 +00003341
Sebastian Pop7ee14722013-11-13 22:37:58 +00003342 // The delinearization transforms a single-subscript MIV dependence test into
3343 // a multi-subscript SIV dependence test that is easier to compute. So we
3344 // resize Pair to contain as many pairs of subscripts as the delinearization
3345 // has found, and then initialize the pairs following the delinearization.
Sebastian Popc62c6792013-11-12 22:47:20 +00003346 Pair.resize(size);
3347 for (int i = 0; i < size; ++i) {
3348 Pair[i].Src = SrcSubscripts[i];
3349 Pair[i].Dst = DstSubscripts[i];
Jingyue Wu0fa125a2014-11-16 16:52:44 +00003350 unifySubscriptType(&Pair[i]);
Sebastian Popc62c6792013-11-12 22:47:20 +00003351 }
3352
3353 return true;
3354}
Sebastian Pop59b61b92012-10-11 07:32:34 +00003355
3356//===----------------------------------------------------------------------===//
3357
3358#ifndef NDEBUG
3359// For debugging purposes, dump a small bit vector to dbgs().
3360static void dumpSmallBitVector(SmallBitVector &BV) {
3361 dbgs() << "{";
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003362 for (unsigned VI : BV.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003363 dbgs() << VI;
3364 if (BV.find_next(VI) >= 0)
3365 dbgs() << ' ';
3366 }
3367 dbgs() << "}\n";
3368}
3369#endif
3370
Philip Pfaffe94385852019-02-03 12:25:41 +00003371bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA,
3372 FunctionAnalysisManager::Invalidator &Inv) {
3373 // Check if the analysis itself has been invalidated.
3374 auto PAC = PA.getChecker<DependenceAnalysis>();
3375 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
3376 return true;
3377
3378 // Check transitive dependencies.
3379 return Inv.invalidate<AAManager>(F, PA) ||
3380 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
3381 Inv.invalidate<LoopAnalysis>(F, PA);
3382}
3383
Sebastian Pop59b61b92012-10-11 07:32:34 +00003384// depends -
3385// Returns NULL if there is no dependence.
3386// Otherwise, return a Dependence with as many details as possible.
3387// Corresponds to Section 3.1 in the paper
3388//
3389// Practical Dependence Testing
3390// Goff, Kennedy, Tseng
3391// PLDI 1991
3392//
Preston Briggs3ad39492012-11-21 23:50:04 +00003393// Care is required to keep the routine below, getSplitIteration(),
3394// up to date with respect to this routine.
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003395std::unique_ptr<Dependence>
Chandler Carruth49c22192016-05-12 22:19:39 +00003396DependenceInfo::depends(Instruction *Src, Instruction *Dst,
3397 bool PossiblyLoopIndependent) {
Preston Briggs1084fa22012-11-27 06:41:46 +00003398 if (Src == Dst)
3399 PossiblyLoopIndependent = false;
3400
Sebastian Pop59b61b92012-10-11 07:32:34 +00003401 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) ||
3402 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory()))
3403 // if both instructions don't reference memory, there's no dependence
Craig Topper9f008862014-04-15 04:59:12 +00003404 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003405
Preston Briggs3ad39492012-11-21 23:50:04 +00003406 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003407 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003408 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003409 return make_unique<Dependence>(Src, Dst);
Preston Briggs3ad39492012-11-21 23:50:04 +00003410 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003411
Renato Golin038ede22018-03-09 21:05:58 +00003412 assert(isLoadOrStore(Src) && "instruction is not load or store");
3413 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3414 Value *SrcPtr = getLoadStorePointerOperand(Src);
3415 Value *DstPtr = getLoadStorePointerOperand(Dst);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003416
David Green5ef933b2018-04-10 11:37:21 +00003417 switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
3418 MemoryLocation::get(Dst),
3419 MemoryLocation::get(Src))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003420 case MayAlias:
3421 case PartialAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003422 // cannot analyse objects if we don't understand their aliasing.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003423 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
Dylan Noblesmith2cae60e2014-08-25 00:28:39 +00003424 return make_unique<Dependence>(Src, Dst);
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003425 case NoAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003426 // If the objects noalias, they are distinct, accesses are independent.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003427 LLVM_DEBUG(dbgs() << "no alias\n");
Craig Topper9f008862014-04-15 04:59:12 +00003428 return nullptr;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +00003429 case MustAlias:
Sebastian Pop59b61b92012-10-11 07:32:34 +00003430 break; // The underlying objects alias; test accesses for dependence.
3431 }
3432
Sebastian Pop59b61b92012-10-11 07:32:34 +00003433 // establish loop nesting levels
3434 establishNestingLevels(Src, Dst);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003435 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3436 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003437
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003438 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003439 ++TotalArrayPairs;
3440
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003441 unsigned Pairs = 1;
3442 SmallVector<Subscript, 2> Pair(Pairs);
3443 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3444 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003445 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3446 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003447 Pair[0].Src = SrcSCEV;
3448 Pair[0].Dst = DstSCEV;
Preston Briggs3ad39492012-11-21 23:50:04 +00003449
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003450 if (Delinearize) {
Hal Finkel0ef2b102015-08-19 02:56:36 +00003451 if (tryDelinearize(Src, Dst, Pair)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003452 LLVM_DEBUG(dbgs() << " delinearized\n");
Hal Finkel0ef2b102015-08-19 02:56:36 +00003453 Pairs = Pair.size();
3454 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003455 }
3456
Preston Briggs3ad39492012-11-21 23:50:04 +00003457 for (unsigned P = 0; P < Pairs; ++P) {
3458 Pair[P].Loops.resize(MaxLevels + 1);
3459 Pair[P].GroupLoops.resize(MaxLevels + 1);
3460 Pair[P].Group.resize(Pairs);
3461 removeMatchingExtensions(&Pair[P]);
3462 Pair[P].Classification =
3463 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3464 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3465 Pair[P].Loops);
3466 Pair[P].GroupLoops = Pair[P].Loops;
3467 Pair[P].Group.set(P);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003468 LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
3469 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3470 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3471 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
3472 LLVM_DEBUG(dbgs() << "\tloops = ");
3473 LLVM_DEBUG(dumpSmallBitVector(Pair[P].Loops));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003474 }
3475
3476 SmallBitVector Separable(Pairs);
3477 SmallBitVector Coupled(Pairs);
3478
3479 // Partition subscripts into separable and minimally-coupled groups
3480 // Algorithm in paper is algorithmically better;
3481 // this may be faster in practice. Check someday.
3482 //
3483 // Here's an example of how it works. Consider this code:
3484 //
3485 // for (i = ...) {
3486 // for (j = ...) {
3487 // for (k = ...) {
3488 // for (l = ...) {
3489 // for (m = ...) {
3490 // A[i][j][k][m] = ...;
3491 // ... = A[0][j][l][i + j];
3492 // }
3493 // }
3494 // }
3495 // }
3496 // }
3497 //
3498 // There are 4 subscripts here:
3499 // 0 [i] and [0]
3500 // 1 [j] and [j]
3501 // 2 [k] and [l]
3502 // 3 [m] and [i + j]
3503 //
3504 // We've already classified each subscript pair as ZIV, SIV, etc.,
3505 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3506 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3507 // and set Pair[P].Group = {P}.
3508 //
3509 // Src Dst Classification Loops GroupLoops Group
3510 // 0 [i] [0] SIV {1} {1} {0}
3511 // 1 [j] [j] SIV {2} {2} {1}
3512 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3513 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3514 //
3515 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3516 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3517 //
3518 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3519 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3520 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3521 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3522 // to either Separable or Coupled).
3523 //
3524 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
Hiroshi Inouec437f312019-01-30 05:26:31 +00003525 // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
Sebastian Pop59b61b92012-10-11 07:32:34 +00003526 // so Pair[3].Group = {0, 1, 3} and Done = false.
3527 //
3528 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3529 // Since Done remains true, we add 2 to the set of Separable pairs.
3530 //
3531 // Finally, we consider 3. There's nothing to compare it with,
3532 // so Done remains true and we add it to the Coupled set.
3533 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3534 //
3535 // In the end, we've got 1 separable subscript and 1 coupled group.
3536 for (unsigned SI = 0; SI < Pairs; ++SI) {
3537 if (Pair[SI].Classification == Subscript::NonLinear) {
3538 // ignore these, but collect loops for later
3539 ++NonlinearSubscriptPairs;
3540 collectCommonLoops(Pair[SI].Src,
3541 LI->getLoopFor(Src->getParent()),
3542 Pair[SI].Loops);
3543 collectCommonLoops(Pair[SI].Dst,
3544 LI->getLoopFor(Dst->getParent()),
3545 Pair[SI].Loops);
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003546 Result.Consistent = false;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003547 } else if (Pair[SI].Classification == Subscript::ZIV) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003548 // always separable
3549 Separable.set(SI);
3550 }
3551 else {
3552 // SIV, RDIV, or MIV, so check for coupled group
3553 bool Done = true;
3554 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3555 SmallBitVector Intersection = Pair[SI].GroupLoops;
3556 Intersection &= Pair[SJ].GroupLoops;
3557 if (Intersection.any()) {
3558 // accumulate set of all the loops in group
3559 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3560 // accumulate set of all subscripts in group
3561 Pair[SJ].Group |= Pair[SI].Group;
3562 Done = false;
3563 }
3564 }
3565 if (Done) {
3566 if (Pair[SI].Group.count() == 1) {
3567 Separable.set(SI);
3568 ++SeparableSubscriptPairs;
3569 }
3570 else {
3571 Coupled.set(SI);
3572 ++CoupledSubscriptPairs;
3573 }
3574 }
3575 }
3576 }
3577
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003578 LLVM_DEBUG(dbgs() << " Separable = ");
3579 LLVM_DEBUG(dumpSmallBitVector(Separable));
3580 LLVM_DEBUG(dbgs() << " Coupled = ");
3581 LLVM_DEBUG(dumpSmallBitVector(Coupled));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003582
3583 Constraint NewConstraint;
3584 NewConstraint.setAny(SE);
3585
3586 // test separable subscripts
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003587 for (unsigned SI : Separable.set_bits()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003588 LLVM_DEBUG(dbgs() << "testing subscript " << SI);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003589 switch (Pair[SI].Classification) {
3590 case Subscript::ZIV:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003591 LLVM_DEBUG(dbgs() << ", ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003592 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003593 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003594 break;
3595 case Subscript::SIV: {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003596 LLVM_DEBUG(dbgs() << ", SIV\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003597 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003598 const SCEV *SplitIter = nullptr;
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003599 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
3600 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003601 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003602 break;
3603 }
3604 case Subscript::RDIV:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003605 LLVM_DEBUG(dbgs() << ", RDIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003606 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003607 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003608 break;
3609 case Subscript::MIV:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003610 LLVM_DEBUG(dbgs() << ", MIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003611 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003612 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003613 break;
3614 default:
3615 llvm_unreachable("subscript has unexpected classification");
3616 }
3617 }
3618
3619 if (Coupled.count()) {
3620 // test coupled subscript groups
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003621 LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n");
3622 LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003623 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3624 for (unsigned II = 0; II <= MaxLevels; ++II)
3625 Constraints[II].setAny(SE);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003626 for (unsigned SI : Coupled.set_bits()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003627 LLVM_DEBUG(dbgs() << "testing subscript group " << SI << " { ");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003628 SmallBitVector Group(Pair[SI].Group);
3629 SmallBitVector Sivs(Pairs);
3630 SmallBitVector Mivs(Pairs);
3631 SmallBitVector ConstrainedLevels(MaxLevels + 1);
Jingyue Wua84feb12015-05-29 16:58:08 +00003632 SmallVector<Subscript *, 4> PairsInGroup;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003633 for (unsigned SJ : Group.set_bits()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003634 LLVM_DEBUG(dbgs() << SJ << " ");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003635 if (Pair[SJ].Classification == Subscript::SIV)
3636 Sivs.set(SJ);
3637 else
3638 Mivs.set(SJ);
Jingyue Wua84feb12015-05-29 16:58:08 +00003639 PairsInGroup.push_back(&Pair[SJ]);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003640 }
Jingyue Wua84feb12015-05-29 16:58:08 +00003641 unifySubscriptType(PairsInGroup);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003642 LLVM_DEBUG(dbgs() << "}\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003643 while (Sivs.any()) {
3644 bool Changed = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003645 for (unsigned SJ : Sivs.set_bits()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003646 LLVM_DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003647 // SJ is an SIV subscript that's part of the current coupled group
3648 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003649 const SCEV *SplitIter = nullptr;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003650 LLVM_DEBUG(dbgs() << "SIV\n");
NAKAMURA Takumi478559a2015-03-05 01:25:19 +00003651 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
3652 SplitIter))
Craig Topper9f008862014-04-15 04:59:12 +00003653 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003654 ConstrainedLevels.set(Level);
3655 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3656 if (Constraints[Level].isEmpty()) {
3657 ++DeltaIndependence;
Craig Topper9f008862014-04-15 04:59:12 +00003658 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003659 }
3660 Changed = true;
3661 }
3662 Sivs.reset(SJ);
3663 }
3664 if (Changed) {
3665 // propagate, possibly creating new SIVs and ZIVs
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003666 LLVM_DEBUG(dbgs() << " propagating\n");
3667 LLVM_DEBUG(dbgs() << "\tMivs = ");
3668 LLVM_DEBUG(dumpSmallBitVector(Mivs));
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003669 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003670 // SJ is an MIV subscript that's part of the current coupled group
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003671 LLVM_DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003672 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003673 Constraints, Result.Consistent)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003674 LLVM_DEBUG(dbgs() << "\t Changed\n");
Sebastian Pop59b61b92012-10-11 07:32:34 +00003675 ++DeltaPropagations;
3676 Pair[SJ].Classification =
3677 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3678 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3679 Pair[SJ].Loops);
3680 switch (Pair[SJ].Classification) {
3681 case Subscript::ZIV:
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003682 LLVM_DEBUG(dbgs() << "ZIV\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003683 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003684 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003685 Mivs.reset(SJ);
3686 break;
3687 case Subscript::SIV:
3688 Sivs.set(SJ);
3689 Mivs.reset(SJ);
3690 break;
3691 case Subscript::RDIV:
3692 case Subscript::MIV:
3693 break;
3694 default:
3695 llvm_unreachable("bad subscript classification");
3696 }
3697 }
3698 }
3699 }
3700 }
3701
3702 // test & propagate remaining RDIVs
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003703 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003704 if (Pair[SJ].Classification == Subscript::RDIV) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003705 LLVM_DEBUG(dbgs() << "RDIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003706 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003707 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003708 // I don't yet understand how to propagate RDIV results
3709 Mivs.reset(SJ);
3710 }
3711 }
3712
3713 // test remaining MIVs
3714 // This code is temporary.
3715 // Better to somehow test all remaining subscripts simultaneously.
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003716 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003717 if (Pair[SJ].Classification == Subscript::MIV) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003718 LLVM_DEBUG(dbgs() << "MIV test\n");
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003719 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
Craig Topper9f008862014-04-15 04:59:12 +00003720 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003721 }
3722 else
3723 llvm_unreachable("expected only MIV subscripts at this point");
3724 }
3725
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003726 // update Result.DV from constraint vector
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003727 LLVM_DEBUG(dbgs() << " updating\n");
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003728 for (unsigned SJ : ConstrainedLevels.set_bits()) {
3729 if (SJ > CommonLevels)
Karthik Bhat8d7f7ed2015-03-10 14:32:02 +00003730 break;
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003731 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3732 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
Craig Topper9f008862014-04-15 04:59:12 +00003733 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003734 }
3735 }
3736 }
3737
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003738 // Make sure the Scalar flags are set correctly.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003739 SmallBitVector CompleteLoops(MaxLevels + 1);
3740 for (unsigned SI = 0; SI < Pairs; ++SI)
3741 CompleteLoops |= Pair[SI].Loops;
3742 for (unsigned II = 1; II <= CommonLevels; ++II)
3743 if (CompleteLoops[II])
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003744 Result.DV[II - 1].Scalar = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003745
Sebastian Pop59b61b92012-10-11 07:32:34 +00003746 if (PossiblyLoopIndependent) {
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003747 // Make sure the LoopIndependent flag is set correctly.
3748 // All directions must include equal, otherwise no
3749 // loop-independent dependence is possible.
Sebastian Pop59b61b92012-10-11 07:32:34 +00003750 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003751 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3752 Result.LoopIndependent = false;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003753 break;
3754 }
3755 }
3756 }
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003757 else {
3758 // On the other hand, if all directions are equal and there's no
3759 // loop-independent dependence possible, then no dependence exists.
3760 bool AllEqual = true;
3761 for (unsigned II = 1; II <= CommonLevels; ++II) {
NAKAMURA Takumid8422ce2015-03-05 01:25:12 +00003762 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
Preston Briggs4eb7ee52012-11-29 04:30:52 +00003763 AllEqual = false;
3764 break;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003765 }
3766 }
3767 if (AllEqual)
Craig Topper9f008862014-04-15 04:59:12 +00003768 return nullptr;
Preston Briggs5cb8cfa2012-11-27 19:12:26 +00003769 }
Sebastian Pop59b61b92012-10-11 07:32:34 +00003770
David Blaikie47039dc2015-07-31 21:37:09 +00003771 return make_unique<FullDependence>(std::move(Result));
Sebastian Pop59b61b92012-10-11 07:32:34 +00003772}
3773
3774
3775
3776//===----------------------------------------------------------------------===//
3777// getSplitIteration -
3778// Rather than spend rarely-used space recording the splitting iteration
3779// during the Weak-Crossing SIV test, we re-compute it on demand.
3780// The re-computation is basically a repeat of the entire dependence test,
3781// though simplified since we know that the dependence exists.
3782// It's tedious, since we must go through all propagations, etc.
3783//
Preston Briggs3ad39492012-11-21 23:50:04 +00003784// Care is required to keep this code up to date with respect to the routine
3785// above, depends().
Sebastian Pop59b61b92012-10-11 07:32:34 +00003786//
3787// Generally, the dependence analyzer will be used to build
3788// a dependence graph for a function (basically a map from instructions
3789// to dependences). Looking for cycles in the graph shows us loops
3790// that cannot be trivially vectorized/parallelized.
3791//
3792// We can try to improve the situation by examining all the dependences
3793// that make up the cycle, looking for ones we can break.
3794// Sometimes, peeling the first or last iteration of a loop will break
3795// dependences, and we've got flags for those possibilities.
3796// Sometimes, splitting a loop at some other iteration will do the trick,
3797// and we've got a flag for that case. Rather than waste the space to
3798// record the exact iteration (since we rarely know), we provide
3799// a method that calculates the iteration. It's a drag that it must work
3800// from scratch, but wonderful in that it's possible.
3801//
3802// Here's an example:
3803//
3804// for (i = 0; i < 10; i++)
3805// A[i] = ...
3806// ... = A[11 - i]
3807//
3808// There's a loop-carried flow dependence from the store to the load,
3809// found by the weak-crossing SIV test. The dependence will have a flag,
3810// indicating that the dependence can be broken by splitting the loop.
3811// Calling getSplitIteration will return 5.
3812// Splitting the loop breaks the dependence, like so:
3813//
3814// for (i = 0; i <= 5; i++)
3815// A[i] = ...
3816// ... = A[11 - i]
3817// for (i = 6; i < 10; i++)
3818// A[i] = ...
3819// ... = A[11 - i]
3820//
3821// breaks the dependence and allows us to vectorize/parallelize
3822// both loops.
Chandler Carruth49c22192016-05-12 22:19:39 +00003823const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
3824 unsigned SplitLevel) {
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003825 assert(Dep.isSplitable(SplitLevel) &&
Sebastian Pop59b61b92012-10-11 07:32:34 +00003826 "Dep should be splitable at SplitLevel");
Dylan Noblesmithd96ce662014-08-25 00:28:35 +00003827 Instruction *Src = Dep.getSrc();
3828 Instruction *Dst = Dep.getDst();
Sebastian Pop59b61b92012-10-11 07:32:34 +00003829 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
3830 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
3831 assert(isLoadOrStore(Src));
3832 assert(isLoadOrStore(Dst));
Renato Golin038ede22018-03-09 21:05:58 +00003833 Value *SrcPtr = getLoadStorePointerOperand(Src);
3834 Value *DstPtr = getLoadStorePointerOperand(Dst);
David Green5ef933b2018-04-10 11:37:21 +00003835 assert(underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
3836 MemoryLocation::get(Dst),
3837 MemoryLocation::get(Src)) == MustAlias);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003838
3839 // establish loop nesting levels
3840 establishNestingLevels(Src, Dst);
3841
3842 FullDependence Result(Src, Dst, false, CommonLevels);
3843
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003844 unsigned Pairs = 1;
3845 SmallVector<Subscript, 2> Pair(Pairs);
3846 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3847 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3848 Pair[0].Src = SrcSCEV;
3849 Pair[0].Dst = DstSCEV;
Preston Briggs3ad39492012-11-21 23:50:04 +00003850
Sebastian Popbf6e1c22018-03-06 21:55:59 +00003851 if (Delinearize) {
Hal Finkel0ef2b102015-08-19 02:56:36 +00003852 if (tryDelinearize(Src, Dst, Pair)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003853 LLVM_DEBUG(dbgs() << " delinearized\n");
Hal Finkel0ef2b102015-08-19 02:56:36 +00003854 Pairs = Pair.size();
3855 }
Sebastian Popc62c6792013-11-12 22:47:20 +00003856 }
3857
Preston Briggs3ad39492012-11-21 23:50:04 +00003858 for (unsigned P = 0; P < Pairs; ++P) {
3859 Pair[P].Loops.resize(MaxLevels + 1);
3860 Pair[P].GroupLoops.resize(MaxLevels + 1);
3861 Pair[P].Group.resize(Pairs);
3862 removeMatchingExtensions(&Pair[P]);
3863 Pair[P].Classification =
3864 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3865 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3866 Pair[P].Loops);
3867 Pair[P].GroupLoops = Pair[P].Loops;
3868 Pair[P].Group.set(P);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003869 }
3870
3871 SmallBitVector Separable(Pairs);
3872 SmallBitVector Coupled(Pairs);
3873
3874 // partition subscripts into separable and minimally-coupled groups
3875 for (unsigned SI = 0; SI < Pairs; ++SI) {
3876 if (Pair[SI].Classification == Subscript::NonLinear) {
3877 // ignore these, but collect loops for later
3878 collectCommonLoops(Pair[SI].Src,
3879 LI->getLoopFor(Src->getParent()),
3880 Pair[SI].Loops);
3881 collectCommonLoops(Pair[SI].Dst,
3882 LI->getLoopFor(Dst->getParent()),
3883 Pair[SI].Loops);
3884 Result.Consistent = false;
3885 }
3886 else if (Pair[SI].Classification == Subscript::ZIV)
3887 Separable.set(SI);
3888 else {
3889 // SIV, RDIV, or MIV, so check for coupled group
3890 bool Done = true;
3891 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3892 SmallBitVector Intersection = Pair[SI].GroupLoops;
3893 Intersection &= Pair[SJ].GroupLoops;
3894 if (Intersection.any()) {
3895 // accumulate set of all the loops in group
3896 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3897 // accumulate set of all subscripts in group
3898 Pair[SJ].Group |= Pair[SI].Group;
3899 Done = false;
3900 }
3901 }
3902 if (Done) {
3903 if (Pair[SI].Group.count() == 1)
3904 Separable.set(SI);
3905 else
3906 Coupled.set(SI);
3907 }
3908 }
3909 }
3910
3911 Constraint NewConstraint;
3912 NewConstraint.setAny(SE);
3913
3914 // test separable subscripts
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003915 for (unsigned SI : Separable.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003916 switch (Pair[SI].Classification) {
3917 case Subscript::SIV: {
3918 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003919 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003920 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
3921 Result, NewConstraint, SplitIter);
3922 if (Level == SplitLevel) {
Craig Topper9f008862014-04-15 04:59:12 +00003923 assert(SplitIter != nullptr);
Sebastian Pop59b61b92012-10-11 07:32:34 +00003924 return SplitIter;
3925 }
3926 break;
3927 }
3928 case Subscript::ZIV:
3929 case Subscript::RDIV:
3930 case Subscript::MIV:
3931 break;
3932 default:
3933 llvm_unreachable("subscript has unexpected classification");
3934 }
3935 }
3936
3937 if (Coupled.count()) {
3938 // test coupled subscript groups
3939 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3940 for (unsigned II = 0; II <= MaxLevels; ++II)
3941 Constraints[II].setAny(SE);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003942 for (unsigned SI : Coupled.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003943 SmallBitVector Group(Pair[SI].Group);
3944 SmallBitVector Sivs(Pairs);
3945 SmallBitVector Mivs(Pairs);
3946 SmallBitVector ConstrainedLevels(MaxLevels + 1);
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003947 for (unsigned SJ : Group.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003948 if (Pair[SJ].Classification == Subscript::SIV)
3949 Sivs.set(SJ);
3950 else
3951 Mivs.set(SJ);
3952 }
3953 while (Sivs.any()) {
3954 bool Changed = false;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003955 for (unsigned SJ : Sivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003956 // SJ is an SIV subscript that's part of the current coupled group
3957 unsigned Level;
Craig Topper9f008862014-04-15 04:59:12 +00003958 const SCEV *SplitIter = nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00003959 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
3960 Result, NewConstraint, SplitIter);
3961 if (Level == SplitLevel && SplitIter)
3962 return SplitIter;
3963 ConstrainedLevels.set(Level);
3964 if (intersectConstraints(&Constraints[Level], &NewConstraint))
3965 Changed = true;
3966 Sivs.reset(SJ);
3967 }
3968 if (Changed) {
3969 // propagate, possibly creating new SIVs and ZIVs
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00003970 for (unsigned SJ : Mivs.set_bits()) {
Sebastian Pop59b61b92012-10-11 07:32:34 +00003971 // SJ is an MIV subscript that's part of the current coupled group
3972 if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
3973 Pair[SJ].Loops, Constraints, Result.Consistent)) {
3974 Pair[SJ].Classification =
3975 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3976 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3977 Pair[SJ].Loops);
3978 switch (Pair[SJ].Classification) {
3979 case Subscript::ZIV:
3980 Mivs.reset(SJ);
3981 break;
3982 case Subscript::SIV:
3983 Sivs.set(SJ);
3984 Mivs.reset(SJ);
3985 break;
3986 case Subscript::RDIV:
3987 case Subscript::MIV:
3988 break;
3989 default:
3990 llvm_unreachable("bad subscript classification");
3991 }
3992 }
3993 }
3994 }
3995 }
3996 }
3997 }
3998 llvm_unreachable("somehow reached end of routine");
Craig Topper9f008862014-04-15 04:59:12 +00003999 return nullptr;
Sebastian Pop59b61b92012-10-11 07:32:34 +00004000}