blob: 29b099c9cba31b1e20cfda325747177fff114f32 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Create a polyhedral description for a static control flow region.
11//
12// The pass creates a polyhedral description of the Scops detected by the Scop
13// detection derived from their LLVM-IR code.
14//
Tobias Grossera5605d32014-10-29 19:58:28 +000015// This representation is shared among several tools in the polyhedral
Tobias Grosser75805372011-04-29 06:27:02 +000016// community, which are e.g. Cloog, Pluto, Loopo, Graphite.
17//
18//===----------------------------------------------------------------------===//
19
Tobias Grosser75805372011-04-29 06:27:02 +000020#include "polly/LinkAllPasses.h"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000021#include "polly/Options.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000022#include "polly/ScopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000023#include "polly/Support/GICHelper.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000024#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000025#include "polly/Support/ScopHelper.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000026#include "polly/TempScopInfo.h"
Tobias Grosserf4c24b22015-04-05 13:11:54 +000027#include "llvm/ADT/MapVector.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000028#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000029#include "llvm/ADT/Statistic.h"
Johannes Doerfertecff11d2015-05-22 23:43:58 +000030#include "llvm/ADT/STLExtras.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000031#include "llvm/ADT/StringExtras.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000032#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000033#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser83628182013-05-07 08:11:54 +000034#include "llvm/Analysis/RegionIterator.h"
35#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000036#include "llvm/Support/Debug.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000037#include "isl/aff.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000038#include "isl/constraint.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000039#include "isl/local_space.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000040#include "isl/map.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000041#include "isl/options.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000042#include "isl/printer.h"
Tobias Grosser808cd692015-07-14 09:33:13 +000043#include "isl/schedule.h"
44#include "isl/schedule_node.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000045#include "isl/set.h"
46#include "isl/union_map.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000047#include "isl/union_set.h"
Tobias Grosseredab1352013-06-21 06:41:31 +000048#include "isl/val.h"
Tobias Grosser75805372011-04-29 06:27:02 +000049#include <sstream>
50#include <string>
51#include <vector>
52
53using namespace llvm;
54using namespace polly;
55
Chandler Carruth95fef942014-04-22 03:30:19 +000056#define DEBUG_TYPE "polly-scops"
57
Tobias Grosser74394f02013-01-14 22:40:23 +000058STATISTIC(ScopFound, "Number of valid Scops");
59STATISTIC(RichScopFound, "Number of Scops containing a loop");
Tobias Grosser75805372011-04-29 06:27:02 +000060
Johannes Doerfert9e7b17b2014-08-18 00:40:13 +000061// Multiplicative reductions can be disabled separately as these kind of
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000062// operations can overflow easily. Additive reductions and bit operations
63// are in contrast pretty stable.
Tobias Grosser483a90d2014-07-09 10:50:10 +000064static cl::opt<bool> DisableMultiplicativeReductions(
65 "polly-disable-multiplicative-reductions",
66 cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore,
67 cl::init(false), cl::cat(PollyCategory));
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000068
Johannes Doerfert9143d672014-09-27 11:02:39 +000069static cl::opt<unsigned> RunTimeChecksMaxParameters(
70 "polly-rtc-max-parameters",
71 cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden,
72 cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory));
73
Tobias Grosser71500722015-03-28 15:11:14 +000074static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup(
75 "polly-rtc-max-arrays-per-group",
76 cl::desc("The maximal number of arrays to compare in each alias group."),
77 cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory));
78
Michael Kruse046dde42015-08-10 13:01:57 +000079// Create a sequence of two schedules. Either argument may be null and is
80// interpreted as the empty schedule. Can also return null if both schedules are
81// empty.
82static __isl_give isl_schedule *
83combineInSequence(__isl_take isl_schedule *Prev,
84 __isl_take isl_schedule *Succ) {
85 if (!Prev)
86 return Succ;
87 if (!Succ)
88 return Prev;
89
90 return isl_schedule_sequence(Prev, Succ);
91}
92
Tobias Grosser0695ee42013-09-17 03:30:31 +000093/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000094struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000095public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000096 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
97 ///
98 /// @param Stmt The location at which the scalar evolution expression
99 /// is evaluated.
100 /// @param Expr The expression that is translated.
101 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
102
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000103private:
Tobias Grosser3cc99742012-06-06 16:33:15 +0000104 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000105 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +0000106 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000107
Tobias Grosser0695ee42013-09-17 03:30:31 +0000108 SCEVAffinator(const ScopStmt *Stmt);
109 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000110
Tobias Grosser0695ee42013-09-17 03:30:31 +0000111 __isl_give isl_pw_aff *visit(const SCEV *Expr);
112 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
113 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
114 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
115 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
116 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
117 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
118 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
119 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
120 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
121 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
122 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000123 __isl_give isl_pw_aff *visitSDivInstruction(Instruction *SDiv);
Tobias Grosser50165ff2015-06-24 04:13:29 +0000124 __isl_give isl_pw_aff *visitSRemInstruction(Instruction *SDiv);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000125
Tobias Grosser0695ee42013-09-17 03:30:31 +0000126 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
127};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000128
Tobias Grosser0695ee42013-09-17 03:30:31 +0000129SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
130 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
131 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000132
Tobias Grosser0695ee42013-09-17 03:30:31 +0000133__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
134 const SCEV *Scev) {
135 Scop *S = Stmt->getParent();
136 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000137
Tobias Grosser0695ee42013-09-17 03:30:31 +0000138 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000139
Tobias Grosser0695ee42013-09-17 03:30:31 +0000140 SCEVAffinator Affinator(Stmt);
141 return Affinator.visit(Scev);
142}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000143
Tobias Grosser0695ee42013-09-17 03:30:31 +0000144__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
145 // In case the scev is a valid parameter, we do not further analyze this
146 // expression, but create a new parameter in the isl_pw_aff. This allows us
147 // to treat subexpressions that we cannot translate into an piecewise affine
148 // expression, as constant parameters of the piecewise affine expression.
149 if (isl_id *Id = S->getIdForParam(Expr)) {
150 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
151 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000152
Tobias Grosser0695ee42013-09-17 03:30:31 +0000153 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
154 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
155 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000156
157 return isl_pw_aff_alloc(Domain, Affine);
158 }
159
Tobias Grosser0695ee42013-09-17 03:30:31 +0000160 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
161}
162
Tobias Grosser0d170132013-10-03 13:09:19 +0000163__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000164 ConstantInt *Value = Expr->getValue();
165 isl_val *v;
166
167 // LLVM does not define if an integer value is interpreted as a signed or
168 // unsigned value. Hence, without further information, it is unknown how
169 // this value needs to be converted to GMP. At the moment, we only support
170 // signed operations. So we just interpret it as signed. Later, there are
171 // two options:
172 //
173 // 1. We always interpret any value as signed and convert the values on
174 // demand.
175 // 2. We pass down the signedness of the calculation and use it to interpret
176 // this constant correctly.
177 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
178
179 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
Johannes Doerfert9c147372014-11-19 15:36:59 +0000180 isl_local_space *ls = isl_local_space_from_space(Space);
181 return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000182}
183
184__isl_give isl_pw_aff *
185SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
186 llvm_unreachable("SCEVTruncateExpr not yet supported");
187}
188
189__isl_give isl_pw_aff *
190SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
191 llvm_unreachable("SCEVZeroExtendExpr not yet supported");
192}
193
194__isl_give isl_pw_aff *
195SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
196 // Assuming the value is signed, a sign extension is basically a noop.
197 // TODO: Reconsider this as soon as we support unsigned values.
198 return visit(Expr->getOperand());
199}
200
201__isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
202 isl_pw_aff *Sum = visit(Expr->getOperand(0));
203
204 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
205 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
206 Sum = isl_pw_aff_add(Sum, NextSummand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000207 }
208
Tobias Grosser0695ee42013-09-17 03:30:31 +0000209 // TODO: Check for NSW and NUW.
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000210
Tobias Grosser0695ee42013-09-17 03:30:31 +0000211 return Sum;
212}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000213
Tobias Grosser0695ee42013-09-17 03:30:31 +0000214__isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
Johannes Doerfertbe409962015-03-29 20:45:09 +0000215 // Divide Expr into a constant part and the rest. Then visit both and multiply
216 // the result to obtain the representation for Expr. While the second part of
217 // ConstantAndLeftOverPair might still be a SCEVMulExpr we will not get to
218 // this point again. The reason is that if it is a multiplication it consists
219 // only of parameters and we will stop in the visit(const SCEV *) function and
220 // return the isl_pw_aff for that parameter.
221 auto ConstantAndLeftOverPair = extractConstantFactor(Expr, *S->getSE());
222 return isl_pw_aff_mul(visit(ConstantAndLeftOverPair.first),
223 visit(ConstantAndLeftOverPair.second));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000224}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000225
Tobias Grosser0695ee42013-09-17 03:30:31 +0000226__isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
227 llvm_unreachable("SCEVUDivExpr not yet supported");
228}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000229
Tobias Grosser0695ee42013-09-17 03:30:31 +0000230__isl_give isl_pw_aff *
231SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
232 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000233
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000234 auto Flags = Expr->getNoWrapFlags();
235
Tobias Grosser0695ee42013-09-17 03:30:31 +0000236 // Directly generate isl_pw_aff for Expr if 'start' is zero.
237 if (Expr->getStart()->isZero()) {
238 assert(S->getRegion().contains(Expr->getLoop()) &&
239 "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000240
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000241 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser0695ee42013-09-17 03:30:31 +0000242 isl_pw_aff *Step = visit(Expr->getOperand(1));
243 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
244 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000245
Tobias Grosser0695ee42013-09-17 03:30:31 +0000246 int loopDimension = getLoopDepth(Expr->getLoop());
247
248 isl_aff *LAff = isl_aff_set_coefficient_si(
249 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
250 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
251
252 // TODO: Do we need to check for NSW and NUW?
253 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000254 }
255
Tobias Grosser0695ee42013-09-17 03:30:31 +0000256 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
257 // if 'start' is not zero.
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000258 // TODO: Using the original SCEV no-wrap flags is not always safe, however
259 // as our code generation is reordering the expression anyway it doesn't
260 // really matter.
Tobias Grosser0695ee42013-09-17 03:30:31 +0000261 ScalarEvolution &SE = *S->getSE();
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000262 const SCEV *ZeroStartExpr =
263 SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
264 Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000265
Tobias Grosser0695ee42013-09-17 03:30:31 +0000266 isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
267 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000268
Tobias Grosser0695ee42013-09-17 03:30:31 +0000269 return isl_pw_aff_add(ZeroStartResult, Start);
270}
271
272__isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
273 isl_pw_aff *Max = visit(Expr->getOperand(0));
274
275 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
276 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
277 Max = isl_pw_aff_max(Max, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000278 }
279
Tobias Grosser0695ee42013-09-17 03:30:31 +0000280 return Max;
281}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000282
Tobias Grosser0695ee42013-09-17 03:30:31 +0000283__isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
284 llvm_unreachable("SCEVUMaxExpr not yet supported");
285}
286
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000287__isl_give isl_pw_aff *SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
288 assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
289 auto *SE = S->getSE();
290
291 auto *Divisor = SDiv->getOperand(1);
292 auto *DivisorSCEV = SE->getSCEV(Divisor);
293 auto *DivisorPWA = visit(DivisorSCEV);
294 assert(isa<ConstantInt>(Divisor) &&
295 "SDiv is no parameter but has a non-constant RHS.");
296
297 auto *Dividend = SDiv->getOperand(0);
298 auto *DividendSCEV = SE->getSCEV(Dividend);
299 auto *DividendPWA = visit(DividendSCEV);
300 return isl_pw_aff_tdiv_q(DividendPWA, DivisorPWA);
301}
302
Tobias Grosser50165ff2015-06-24 04:13:29 +0000303__isl_give isl_pw_aff *SCEVAffinator::visitSRemInstruction(Instruction *SRem) {
304 assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!");
305 auto *SE = S->getSE();
306
307 auto *Divisor = dyn_cast<ConstantInt>(SRem->getOperand(1));
308 assert(Divisor && "SRem is no parameter but has a non-constant RHS.");
309 auto *DivisorVal = isl_valFromAPInt(Ctx, Divisor->getValue(),
310 /* isSigned */ true);
311
312 auto *Dividend = SRem->getOperand(0);
313 auto *DividendSCEV = SE->getSCEV(Dividend);
314 auto *DividendPWA = visit(DividendSCEV);
315
316 return isl_pw_aff_mod_val(DividendPWA, isl_val_abs(DivisorVal));
317}
318
Tobias Grosser0695ee42013-09-17 03:30:31 +0000319__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000320 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
321 switch (I->getOpcode()) {
322 case Instruction::SDiv:
323 return visitSDivInstruction(I);
Tobias Grosser50165ff2015-06-24 04:13:29 +0000324 case Instruction::SRem:
325 return visitSRemInstruction(I);
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000326 default:
327 break; // Fall through.
328 }
329 }
330
331 llvm_unreachable(
332 "Unknowns SCEV was neither parameter nor a valid instruction.");
Tobias Grosser0695ee42013-09-17 03:30:31 +0000333}
334
335int SCEVAffinator::getLoopDepth(const Loop *L) {
336 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
337 assert(outerLoop && "Scop does not contain this loop");
338 return L->getLoopDepth() - outerLoop->getLoopDepth();
339}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000340
Johannes Doerferte7044942015-02-24 11:58:30 +0000341/// @brief Add the bounds of @p Range to the set @p S for dimension @p dim.
342static __isl_give isl_set *addRangeBoundsToSet(__isl_take isl_set *S,
343 const ConstantRange &Range,
344 int dim,
345 enum isl_dim_type type) {
346 isl_val *V;
347 isl_ctx *ctx = isl_set_get_ctx(S);
348
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000349 bool useLowerUpperBound = Range.isSignWrappedSet() && !Range.isFullSet();
350 const auto LB = useLowerUpperBound ? Range.getLower() : Range.getSignedMin();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000351 V = isl_valFromAPInt(ctx, LB, true);
Johannes Doerferte7044942015-02-24 11:58:30 +0000352 isl_set *SLB = isl_set_lower_bound_val(isl_set_copy(S), type, dim, V);
353
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000354 const auto UB = useLowerUpperBound ? Range.getUpper() : Range.getSignedMax();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000355 V = isl_valFromAPInt(ctx, UB, true);
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000356 if (useLowerUpperBound)
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000357 V = isl_val_sub_ui(V, 1);
Johannes Doerferte7044942015-02-24 11:58:30 +0000358 isl_set *SUB = isl_set_upper_bound_val(S, type, dim, V);
359
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000360 if (useLowerUpperBound)
Johannes Doerferte7044942015-02-24 11:58:30 +0000361 return isl_set_union(SLB, SUB);
362 else
363 return isl_set_intersect(SLB, SUB);
364}
365
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000366ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx,
Tobias Grosser92245222015-07-28 14:53:44 +0000367 const SmallVector<const SCEV *, 4> &DimensionSizes,
368 bool IsPHI)
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000369 : BasePtr(BasePtr), ElementType(ElementType),
Tobias Grosser92245222015-07-28 14:53:44 +0000370 DimensionSizes(DimensionSizes), IsPHI(IsPHI) {
371 std::string BasePtrName =
372 getIslCompatibleName("MemRef_", BasePtr, IsPHI ? "__phi" : "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000373 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
374}
375
376ScopArrayInfo::~ScopArrayInfo() { isl_id_free(Id); }
377
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000378std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); }
379
380int ScopArrayInfo::getElemSizeInBytes() const {
381 return ElementType->getPrimitiveSizeInBits() / 8;
382}
383
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000384isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
385
386void ScopArrayInfo::dump() const { print(errs()); }
387
388void ScopArrayInfo::print(raw_ostream &OS) const {
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000389 OS.indent(8) << *getElementType() << " " << getName() << "[*]";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000390 for (unsigned u = 0; u < getNumberOfDimensions(); u++)
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000391 OS << "[" << *DimensionSizes[u] << "]";
392 OS << " // Element size " << getElemSizeInBytes() << "\n";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000393}
394
395const ScopArrayInfo *
396ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
397 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
398 assert(Id && "Output dimension didn't have an ID");
399 return getFromId(Id);
400}
401
402const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
403 void *User = isl_id_get_user(Id);
404 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
405 isl_id_free(Id);
406 return SAI;
407}
408
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000409const std::string
410MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
411 switch (RT) {
412 case MemoryAccess::RT_NONE:
413 llvm_unreachable("Requested a reduction operator string for a memory "
414 "access which isn't a reduction");
415 case MemoryAccess::RT_ADD:
416 return "+";
417 case MemoryAccess::RT_MUL:
418 return "*";
419 case MemoryAccess::RT_BOR:
420 return "|";
421 case MemoryAccess::RT_BXOR:
422 return "^";
423 case MemoryAccess::RT_BAND:
424 return "&";
425 }
426 llvm_unreachable("Unknown reduction type");
427 return "";
428}
429
Johannes Doerfertf6183392014-07-01 20:52:51 +0000430/// @brief Return the reduction type for a given binary operator
431static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
432 const Instruction *Load) {
433 if (!BinOp)
434 return MemoryAccess::RT_NONE;
435 switch (BinOp->getOpcode()) {
436 case Instruction::FAdd:
437 if (!BinOp->hasUnsafeAlgebra())
438 return MemoryAccess::RT_NONE;
439 // Fall through
440 case Instruction::Add:
441 return MemoryAccess::RT_ADD;
442 case Instruction::Or:
443 return MemoryAccess::RT_BOR;
444 case Instruction::Xor:
445 return MemoryAccess::RT_BXOR;
446 case Instruction::And:
447 return MemoryAccess::RT_BAND;
448 case Instruction::FMul:
449 if (!BinOp->hasUnsafeAlgebra())
450 return MemoryAccess::RT_NONE;
451 // Fall through
452 case Instruction::Mul:
453 if (DisableMultiplicativeReductions)
454 return MemoryAccess::RT_NONE;
455 return MemoryAccess::RT_MUL;
456 default:
457 return MemoryAccess::RT_NONE;
458 }
459}
Tobias Grosser75805372011-04-29 06:27:02 +0000460//===----------------------------------------------------------------------===//
461
462MemoryAccess::~MemoryAccess() {
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000463 isl_id_free(Id);
Tobias Grosser54a86e62011-08-18 06:31:46 +0000464 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000465 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000466}
467
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000468static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
469 switch (Access.getType()) {
470 case IRAccess::READ:
471 return MemoryAccess::READ;
472 case IRAccess::MUST_WRITE:
473 return MemoryAccess::MUST_WRITE;
474 case IRAccess::MAY_WRITE:
475 return MemoryAccess::MAY_WRITE;
476 }
477 llvm_unreachable("Unknown IRAccess type!");
478}
479
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000480const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
481 isl_id *ArrayId = getArrayId();
482 void *User = isl_id_get_user(ArrayId);
483 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
484 isl_id_free(ArrayId);
485 return SAI;
486}
487
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000488__isl_give isl_id *MemoryAccess::getArrayId() const {
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000489 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
490}
491
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000492__isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation(
493 __isl_take isl_union_map *USchedule) const {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000494 isl_map *Schedule, *ScheduledAccRel;
495 isl_union_set *UDomain;
496
497 UDomain = isl_union_set_from_set(getStatement()->getDomain());
498 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
499 Schedule = isl_map_from_union_map(USchedule);
500 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
501 return isl_pw_multi_aff_from_map(ScheduledAccRel);
502}
503
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000504__isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000505 return isl_map_copy(AccessRelation);
506}
507
Johannes Doerferta99130f2014-10-13 12:58:03 +0000508std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000509 return stringFromIslObj(AccessRelation);
510}
511
Johannes Doerferta99130f2014-10-13 12:58:03 +0000512__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000513 return isl_map_get_space(AccessRelation);
514}
515
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000516__isl_give isl_map *MemoryAccess::getNewAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000517 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000518}
519
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000520__isl_give isl_basic_map *
521MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000522 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000523 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000524
Tobias Grosser084d8f72012-05-29 09:29:44 +0000525 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000526 isl_basic_set_universe(Statement->getDomainSpace()),
527 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000528}
529
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000530// Formalize no out-of-bound access assumption
531//
532// When delinearizing array accesses we optimistically assume that the
533// delinearized accesses do not access out of bound locations (the subscript
534// expression of each array evaluates for each statement instance that is
535// executed to a value that is larger than zero and strictly smaller than the
536// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000537// dimension for which we do not need to assume any upper bound. At this point
538// we formalize this assumption to ensure that at code generation time the
539// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000540//
541// To find the set of constraints necessary to avoid out of bound accesses, we
542// first build the set of data locations that are not within array bounds. We
543// then apply the reverse access relation to obtain the set of iterations that
544// may contain invalid accesses and reduce this set of iterations to the ones
545// that are actually executed by intersecting them with the domain of the
546// statement. If we now project out all loop dimensions, we obtain a set of
547// parameters that may cause statement instances to be executed that may
548// possibly yield out of bound memory accesses. The complement of these
549// constraints is the set of constraints that needs to be assumed to ensure such
550// statement instances are never executed.
551void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000552 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000553 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000554 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000555 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
556 isl_pw_aff *Var =
557 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
558 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
559
560 isl_set *DimOutside;
561
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000562 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
563 isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000564
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000565 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
566 Statement->getNumIterators());
567 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
568 isl_space_dim(Space, isl_dim_set));
569 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
570 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000571
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000572 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000573
574 Outside = isl_set_union(Outside, DimOutside);
575 }
576
577 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
578 Outside = isl_set_intersect(Outside, Statement->getDomain());
579 Outside = isl_set_params(Outside);
Tobias Grosserf54bb772015-06-26 12:09:28 +0000580
581 // Remove divs to avoid the construction of overly complicated assumptions.
582 // Doing so increases the set of parameter combinations that are assumed to
583 // not appear. This is always save, but may make the resulting run-time check
584 // bail out more often than strictly necessary.
585 Outside = isl_set_remove_divs(Outside);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000586 Outside = isl_set_complement(Outside);
587 Statement->getParent()->addAssumption(Outside);
588 isl_space_free(Space);
589}
590
Johannes Doerferte7044942015-02-24 11:58:30 +0000591void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) {
592 ScalarEvolution *SE = Statement->getParent()->getSE();
593
594 Value *Ptr = getPointerOperand(*getAccessInstruction());
595 if (!Ptr || !SE->isSCEVable(Ptr->getType()))
596 return;
597
598 auto *PtrSCEV = SE->getSCEV(Ptr);
599 if (isa<SCEVCouldNotCompute>(PtrSCEV))
600 return;
601
602 auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV);
603 if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV))
604 PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV);
605
606 const ConstantRange &Range = SE->getSignedRange(PtrSCEV);
607 if (Range.isFullSet())
608 return;
609
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000610 bool isWrapping = Range.isSignWrappedSet();
Johannes Doerferte7044942015-02-24 11:58:30 +0000611 unsigned BW = Range.getBitWidth();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000612 const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin();
613 const auto UB = isWrapping ? Range.getUpper() : Range.getSignedMax();
614
615 auto Min = LB.sdiv(APInt(BW, ElementSize));
616 auto Max = (UB - APInt(BW, 1)).sdiv(APInt(BW, ElementSize));
Johannes Doerferte7044942015-02-24 11:58:30 +0000617
618 isl_set *AccessRange = isl_map_range(isl_map_copy(AccessRelation));
619 AccessRange =
620 addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, isl_dim_set);
621 AccessRelation = isl_map_intersect_range(AccessRelation, AccessRange);
622}
623
Tobias Grosser619190d2015-03-30 17:22:28 +0000624__isl_give isl_map *MemoryAccess::foldAccess(const IRAccess &Access,
625 __isl_take isl_map *AccessRelation,
626 ScopStmt *Statement) {
627 int Size = Access.Subscripts.size();
628
629 for (int i = Size - 2; i >= 0; --i) {
630 isl_space *Space;
631 isl_map *MapOne, *MapTwo;
632 isl_pw_aff *DimSize = SCEVAffinator::getPwAff(Statement, Access.Sizes[i]);
633
634 isl_space *SpaceSize = isl_pw_aff_get_space(DimSize);
635 isl_pw_aff_free(DimSize);
636 isl_id *ParamId = isl_space_get_dim_id(SpaceSize, isl_dim_param, 0);
637
638 Space = isl_map_get_space(AccessRelation);
639 Space = isl_space_map_from_set(isl_space_range(Space));
640 Space = isl_space_align_params(Space, SpaceSize);
641
642 int ParamLocation = isl_space_find_dim_by_id(Space, isl_dim_param, ParamId);
643 isl_id_free(ParamId);
644
645 MapOne = isl_map_universe(isl_space_copy(Space));
646 for (int j = 0; j < Size; ++j)
647 MapOne = isl_map_equate(MapOne, isl_dim_in, j, isl_dim_out, j);
648 MapOne = isl_map_lower_bound_si(MapOne, isl_dim_in, i + 1, 0);
649
650 MapTwo = isl_map_universe(isl_space_copy(Space));
651 for (int j = 0; j < Size; ++j)
652 if (j < i || j > i + 1)
653 MapTwo = isl_map_equate(MapTwo, isl_dim_in, j, isl_dim_out, j);
654
655 isl_local_space *LS = isl_local_space_from_space(Space);
656 isl_constraint *C;
657 C = isl_equality_alloc(isl_local_space_copy(LS));
658 C = isl_constraint_set_constant_si(C, -1);
659 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, 1);
660 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, -1);
661 MapTwo = isl_map_add_constraint(MapTwo, C);
662 C = isl_equality_alloc(LS);
663 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i + 1, 1);
664 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i + 1, -1);
665 C = isl_constraint_set_coefficient_si(C, isl_dim_param, ParamLocation, 1);
666 MapTwo = isl_map_add_constraint(MapTwo, C);
667 MapTwo = isl_map_upper_bound_si(MapTwo, isl_dim_in, i + 1, -1);
668
669 MapOne = isl_map_union(MapOne, MapTwo);
670 AccessRelation = isl_map_apply_range(AccessRelation, MapOne);
671 }
672 return AccessRelation;
673}
674
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000675MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000676 ScopStmt *Statement, const ScopArrayInfo *SAI,
677 int Identifier)
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000678 : AccType(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst),
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000679 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000680
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000681 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000682 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000683 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000684
685 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000686
Tobias Grosserac3a95f2015-08-03 17:53:21 +0000687 auto IdName = "__polly_array_ref_" + std::to_string(Identifier);
Tobias Grossere29d31c2015-05-15 12:24:09 +0000688 Id = isl_id_alloc(Ctx, IdName.c_str(), nullptr);
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000689
Tobias Grossera1879642011-12-20 10:43:14 +0000690 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000691 // We overapproximate non-affine accesses with a possible access to the
692 // whole array. For read accesses it does not make a difference, if an
693 // access must or may happen. However, for write accesses it is important to
694 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000695 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000696 AccessRelation =
697 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Johannes Doerferte7044942015-02-24 11:58:30 +0000698
699 computeBoundsOnAccessRelation(Access.getElemSizeInBytes());
Tobias Grossera1879642011-12-20 10:43:14 +0000700 return;
701 }
702
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000703 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000704 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000705
Tobias Grosser79baa212014-04-10 08:38:02 +0000706 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000707 isl_pw_aff *Affine =
708 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000709
Sebastian Pop422e33f2014-06-03 18:16:31 +0000710 if (Size == 1) {
711 // For the non delinearized arrays, divide the access function of the last
712 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000713 //
714 // A stride one array access in C expressed as A[i] is expressed in
715 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
716 // two subsequent values of 'i' index two values that are stored next to
717 // each other in memory. By this division we make this characteristic
718 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000719 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000720 Affine = isl_pw_aff_scale_down_val(Affine, v);
721 }
722
723 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
724
Tobias Grosser79baa212014-04-10 08:38:02 +0000725 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000726 }
727
Tobias Grosser619190d2015-03-30 17:22:28 +0000728 AccessRelation = foldAccess(Access, AccessRelation, Statement);
729
Tobias Grosser79baa212014-04-10 08:38:02 +0000730 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000731 AccessRelation = isl_map_set_tuple_id(
732 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000733 AccessRelation =
734 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
735
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000736 assumeNoOutOfBound(Access);
Tobias Grosseraa660a92015-03-30 00:07:50 +0000737 AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain());
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000738 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000739}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000740
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000741void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000742 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000743 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000744}
745
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000746const std::string MemoryAccess::getReductionOperatorStr() const {
747 return MemoryAccess::getReductionOperatorStr(getReductionType());
748}
749
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000750__isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); }
751
Johannes Doerfertf6183392014-07-01 20:52:51 +0000752raw_ostream &polly::operator<<(raw_ostream &OS,
753 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000754 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000755 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000756 else
757 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000758 return OS;
759}
760
Tobias Grosser75805372011-04-29 06:27:02 +0000761void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000762 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000763 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000764 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000765 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000766 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000767 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000768 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000769 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000770 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000771 break;
772 }
Johannes Doerfert0ff23ec2015-02-06 20:13:15 +0000773 OS << "[Reduction Type: " << getReductionType() << "] ";
774 OS << "[Scalar: " << isScalar() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000775 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000776}
777
Tobias Grosser74394f02013-01-14 22:40:23 +0000778void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000779
780// Create a map in the size of the provided set domain, that maps from the
781// one element of the provided set domain to another element of the provided
782// set domain.
783// The mapping is limited to all points that are equal in all but the last
784// dimension and for which the last dimension of the input is strict smaller
785// than the last dimension of the output.
786//
787// getEqualAndLarger(set[i0, i1, ..., iX]):
788//
789// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
790// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
791//
Tobias Grosserf5338802011-10-06 00:03:35 +0000792static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000793 isl_space *Space = isl_space_map_from_set(setDomain);
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000794 isl_map *Map = isl_map_universe(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000795 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000796
797 // Set all but the last dimension to be equal for the input and output
798 //
799 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
800 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000801 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000802 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000803
804 // Set the last dimension of the input to be strict smaller than the
805 // last dimension of the output.
806 //
807 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000808 Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out,
809 lastDimension);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000810 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000811}
812
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000813__isl_give isl_set *
814MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000815 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000816 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000817 isl_space *Space = isl_space_range(isl_map_get_space(S));
818 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000819
Sebastian Popa00a0292012-12-18 07:46:06 +0000820 S = isl_map_reverse(S);
821 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000822
Sebastian Popa00a0292012-12-18 07:46:06 +0000823 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
824 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
825 NextScatt = isl_map_apply_domain(NextScatt, S);
826 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000827
Sebastian Popa00a0292012-12-18 07:46:06 +0000828 isl_set *Deltas = isl_map_deltas(NextScatt);
829 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000830}
831
Sebastian Popa00a0292012-12-18 07:46:06 +0000832bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000833 int StrideWidth) const {
834 isl_set *Stride, *StrideX;
835 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000836
Sebastian Popa00a0292012-12-18 07:46:06 +0000837 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000838 StrideX = isl_set_universe(isl_set_get_space(Stride));
839 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
840 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000841
Tobias Grosser28dd4862012-01-24 16:42:16 +0000842 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000843 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000844
Tobias Grosser28dd4862012-01-24 16:42:16 +0000845 return IsStrideX;
846}
847
Sebastian Popa00a0292012-12-18 07:46:06 +0000848bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
849 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000850}
851
Tobias Grosser79baa212014-04-10 08:38:02 +0000852bool MemoryAccess::isScalar() const {
853 return isl_map_n_out(AccessRelation) == 0;
854}
855
Sebastian Popa00a0292012-12-18 07:46:06 +0000856bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
857 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000858}
859
Tobias Grosser5d453812011-10-06 00:04:11 +0000860void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000861 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000862 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000863}
Tobias Grosser75805372011-04-29 06:27:02 +0000864
865//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000866
Tobias Grosser808cd692015-07-14 09:33:13 +0000867isl_map *ScopStmt::getSchedule() const {
868 isl_set *Domain = getDomain();
869 if (isl_set_is_empty(Domain)) {
870 isl_set_free(Domain);
871 return isl_map_from_aff(
872 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
873 }
874 auto *Schedule = getParent()->getSchedule();
875 Schedule = isl_union_map_intersect_domain(
876 Schedule, isl_union_set_from_set(isl_set_copy(Domain)));
877 if (isl_union_map_is_empty(Schedule)) {
878 isl_set_free(Domain);
879 isl_union_map_free(Schedule);
880 return isl_map_from_aff(
881 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
882 }
883 auto *M = isl_map_from_union_map(Schedule);
884 M = isl_map_coalesce(M);
885 M = isl_map_gist_domain(M, Domain);
886 M = isl_map_coalesce(M);
887 return M;
888}
Tobias Grossercf3942d2011-10-06 00:04:05 +0000889
Tobias Grosser37eb4222014-02-20 21:43:54 +0000890void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
891 assert(isl_set_is_subset(NewDomain, Domain) &&
892 "New domain is not a subset of old domain!");
893 isl_set_free(Domain);
894 Domain = NewDomain;
Tobias Grosser75805372011-04-29 06:27:02 +0000895}
896
Tobias Grosser62139132015-08-02 16:17:41 +0000897// @brief Get the data-type of the elements accessed
898static Type *getAccessType(IRAccess &Access, Instruction *AccessInst) {
899 if (Access.isPHI())
900 return Access.getBase()->getType();
901
902 if (StoreInst *Store = dyn_cast<StoreInst>(AccessInst))
903 return Store->getValueOperand()->getType();
904 if (BranchInst *Branch = dyn_cast<BranchInst>(AccessInst))
905 return Branch->getCondition()->getType();
906 return AccessInst->getType();
907}
908
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000909void ScopStmt::buildAccesses(TempScop &tempScop, BasicBlock *Block,
910 bool isApproximated) {
911 AccFuncSetType *AFS = tempScop.getAccessFunctions(Block);
912 if (!AFS)
913 return;
914
915 for (auto &AccessPair : *AFS) {
916 IRAccess &Access = AccessPair.first;
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000917 Instruction *AccessInst = AccessPair.second;
Tobias Grosser62139132015-08-02 16:17:41 +0000918 Type *ElementType = getAccessType(Access, AccessInst);
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000919
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000920 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
Tobias Grosser92245222015-07-28 14:53:44 +0000921 Access.getBase(), ElementType, Access.Sizes, Access.isPHI());
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000922
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000923 if (isApproximated && Access.isWrite())
924 Access.setMayWrite();
925
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000926 MemoryAccessList *&MAL = InstructionToAccess[AccessInst];
927 if (!MAL)
928 MAL = new MemoryAccessList();
929 MAL->emplace_front(Access, AccessInst, this, SAI, MemAccs.size());
930 MemAccs.push_back(&MAL->front());
Tobias Grosser75805372011-04-29 06:27:02 +0000931 }
932}
933
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000934void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000935 for (MemoryAccess *MA : *this)
936 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000937
938 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000939}
940
Tobias Grosser65b00582011-11-08 15:41:19 +0000941__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000942 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
943 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000944
Tobias Grosserd2795d02011-08-18 07:51:40 +0000945 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000946 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000947 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000948 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000949 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000950 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000951 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000952 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000953 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000954 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000955 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000956 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000957 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000958 case ICmpInst::ICMP_ULT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000959 return isl_pw_aff_lt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000960 case ICmpInst::ICMP_UGT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000961 return isl_pw_aff_gt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000962 case ICmpInst::ICMP_ULE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000963 return isl_pw_aff_le_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000964 case ICmpInst::ICMP_UGE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000965 return isl_pw_aff_ge_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000966 default:
967 llvm_unreachable("Non integer predicate not supported");
968 }
Tobias Grosser75805372011-04-29 06:27:02 +0000969}
970
Tobias Grossere19661e2011-10-07 08:46:57 +0000971__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000972 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000973 isl_space *Space;
974 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000975
Tobias Grossere19661e2011-10-07 08:46:57 +0000976 Space = isl_set_get_space(Domain);
977 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000978
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000979 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000980 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000981 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000982 isl_pw_aff *IV =
983 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000984
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000985 // 0 <= IV.
986 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
987 Domain = isl_set_intersect(Domain, LowerBound);
988
989 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000990 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000991 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000992 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
993 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000994 Domain = isl_set_intersect(Domain, UpperBoundSet);
995 }
996
Tobias Grosserf5338802011-10-06 00:03:35 +0000997 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000998 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000999}
1000
Tobias Grossere602a072013-05-07 07:30:56 +00001001__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
1002 TempScop &tempScop,
1003 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +00001004 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +00001005 *CurrentRegion = &CurRegion;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001006 const BasicBlock *BranchingBB = BB ? BB : R->getEntry();
Tobias Grosser75805372011-04-29 06:27:02 +00001007
Tobias Grosser75805372011-04-29 06:27:02 +00001008 do {
Tobias Grossere19661e2011-10-07 08:46:57 +00001009 if (BranchingBB != CurrentRegion->getEntry()) {
1010 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +00001011 for (const auto &C : *Condition) {
1012 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +00001013 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +00001014 }
1015 }
Tobias Grossere19661e2011-10-07 08:46:57 +00001016 BranchingBB = CurrentRegion->getEntry();
1017 CurrentRegion = CurrentRegion->getParent();
1018 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +00001019
Tobias Grossere19661e2011-10-07 08:46:57 +00001020 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +00001021}
1022
Tobias Grossere602a072013-05-07 07:30:56 +00001023__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
1024 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +00001025 isl_space *Space;
1026 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +00001027 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +00001028
1029 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
1030
Tobias Grosser084d8f72012-05-29 09:29:44 +00001031 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
1032
Tobias Grossere19661e2011-10-07 08:46:57 +00001033 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +00001034 Domain = addLoopBoundsToDomain(Domain, tempScop);
1035 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +00001036 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +00001037
1038 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +00001039}
1040
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001041void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
1042 int Dimension = 0;
1043 isl_ctx *Ctx = Parent.getIslCtx();
1044 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
1045 Type *Ty = GEP->getPointerOperandType();
1046 ScalarEvolution &SE = *Parent.getSE();
1047
1048 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
1049 Dimension = 1;
1050 Ty = PtrTy->getElementType();
1051 }
1052
1053 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
1054 unsigned int Operand = 1 + Dimension;
1055
1056 if (GEP->getNumOperands() <= Operand)
1057 break;
1058
1059 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
1060
1061 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
1062 isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr);
1063 AccessOffset =
1064 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
1065
1066 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
1067 isl_local_space_copy(LSpace),
1068 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
1069
1070 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
1071 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
1072 OutOfBound = isl_set_params(OutOfBound);
1073 isl_set *InBound = isl_set_complement(OutOfBound);
1074 isl_set *Executed = isl_set_params(getDomain());
1075
1076 // A => B == !A or B
1077 isl_set *InBoundIfExecuted =
1078 isl_set_union(isl_set_complement(Executed), InBound);
1079
1080 Parent.addAssumption(InBoundIfExecuted);
1081 }
1082
1083 Dimension += 1;
1084 Ty = ArrayTy->getElementType();
1085 }
1086
1087 isl_local_space_free(LSpace);
1088}
1089
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001090void ScopStmt::deriveAssumptions(BasicBlock *Block) {
1091 for (Instruction &Inst : *Block)
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001092 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
1093 deriveAssumptionsFromGEP(GEP);
1094}
1095
Tobias Grosser74394f02013-01-14 22:40:23 +00001096ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001097 Region &R, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001098 : Parent(parent), BB(nullptr), R(&R), Build(nullptr),
1099 NestLoops(Nest.size()) {
1100 // Setup the induction variables.
1101 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
1102 NestLoops[i] = Nest[i];
1103
Tobias Grosser16c44032015-07-09 07:31:45 +00001104 BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), "");
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001105
1106 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001107
1108 BasicBlock *EntryBB = R.getEntry();
1109 for (BasicBlock *Block : R.blocks()) {
1110 buildAccesses(tempScop, Block, Block != EntryBB);
1111 deriveAssumptions(Block);
1112 }
1113 checkForReductions();
1114}
1115
1116ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001117 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001118 : Parent(parent), BB(&bb), R(nullptr), Build(nullptr),
1119 NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +00001120 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +00001121 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +00001122 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +00001123
Johannes Doerfert79fc23f2014-07-24 23:48:02 +00001124 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +00001125
Tobias Grossere19661e2011-10-07 08:46:57 +00001126 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001127 buildAccesses(tempScop, BB);
1128 deriveAssumptions(BB);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001129 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001130}
1131
Johannes Doerferte58a0122014-06-27 20:31:28 +00001132/// @brief Collect loads which might form a reduction chain with @p StoreMA
1133///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001134/// Check if the stored value for @p StoreMA is a binary operator with one or
1135/// two loads as operands. If the binary operand is commutative & associative,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001136/// used only once (by @p StoreMA) and its load operands are also used only
1137/// once, we have found a possible reduction chain. It starts at an operand
1138/// load and includes the binary operator and @p StoreMA.
1139///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001140/// Note: We allow only one use to ensure the load and binary operator cannot
Johannes Doerferte58a0122014-06-27 20:31:28 +00001141/// escape this block or into any other store except @p StoreMA.
1142void ScopStmt::collectCandiateReductionLoads(
1143 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
1144 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
1145 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001146 return;
1147
1148 // Skip if there is not one binary operator between the load and the store
1149 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +00001150 if (!BinOp)
1151 return;
1152
1153 // Skip if the binary operators has multiple uses
1154 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001155 return;
1156
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001157 // Skip if the opcode of the binary operator is not commutative/associative
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001158 if (!BinOp->isCommutative() || !BinOp->isAssociative())
1159 return;
1160
Johannes Doerfert9890a052014-07-01 00:32:29 +00001161 // Skip if the binary operator is outside the current SCoP
1162 if (BinOp->getParent() != Store->getParent())
1163 return;
1164
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001165 // Skip if it is a multiplicative reduction and we disabled them
1166 if (DisableMultiplicativeReductions &&
1167 (BinOp->getOpcode() == Instruction::Mul ||
1168 BinOp->getOpcode() == Instruction::FMul))
1169 return;
1170
Johannes Doerferte58a0122014-06-27 20:31:28 +00001171 // Check the binary operator operands for a candidate load
1172 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
1173 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
1174 if (!PossibleLoad0 && !PossibleLoad1)
1175 return;
1176
1177 // A load is only a candidate if it cannot escape (thus has only this use)
1178 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001179 if (PossibleLoad0->getParent() == Store->getParent())
1180 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001181 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001182 if (PossibleLoad1->getParent() == Store->getParent())
1183 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001184}
1185
1186/// @brief Check for reductions in this ScopStmt
1187///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001188/// Iterate over all store memory accesses and check for valid binary reduction
1189/// like chains. For all candidates we check if they have the same base address
1190/// and there are no other accesses which overlap with them. The base address
1191/// check rules out impossible reductions candidates early. The overlap check,
1192/// together with the "only one user" check in collectCandiateReductionLoads,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001193/// guarantees that none of the intermediate results will escape during
1194/// execution of the loop nest. We basically check here that no other memory
1195/// access can access the same memory as the potential reduction.
1196void ScopStmt::checkForReductions() {
1197 SmallVector<MemoryAccess *, 2> Loads;
1198 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1199
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001200 // First collect candidate load-store reduction chains by iterating over all
Johannes Doerferte58a0122014-06-27 20:31:28 +00001201 // stores and collecting possible reduction loads.
1202 for (MemoryAccess *StoreMA : MemAccs) {
1203 if (StoreMA->isRead())
1204 continue;
1205
1206 Loads.clear();
1207 collectCandiateReductionLoads(StoreMA, Loads);
1208 for (MemoryAccess *LoadMA : Loads)
1209 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1210 }
1211
1212 // Then check each possible candidate pair.
1213 for (const auto &CandidatePair : Candidates) {
1214 bool Valid = true;
1215 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1216 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1217
1218 // Skip those with obviously unequal base addresses.
1219 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1220 isl_map_free(LoadAccs);
1221 isl_map_free(StoreAccs);
1222 continue;
1223 }
1224
1225 // And check if the remaining for overlap with other memory accesses.
1226 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1227 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1228 isl_set *AllAccs = isl_map_range(AllAccsRel);
1229
1230 for (MemoryAccess *MA : MemAccs) {
1231 if (MA == CandidatePair.first || MA == CandidatePair.second)
1232 continue;
1233
1234 isl_map *AccRel =
1235 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1236 isl_set *Accs = isl_map_range(AccRel);
1237
1238 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1239 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1240 Valid = Valid && isl_set_is_empty(OverlapAccs);
1241 isl_set_free(OverlapAccs);
1242 }
1243 }
1244
1245 isl_set_free(AllAccs);
1246 if (!Valid)
1247 continue;
1248
Johannes Doerfertf6183392014-07-01 20:52:51 +00001249 const LoadInst *Load =
1250 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1251 MemoryAccess::ReductionType RT =
1252 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1253
Johannes Doerferte58a0122014-06-27 20:31:28 +00001254 // If no overlapping access was found we mark the load and store as
1255 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001256 CandidatePair.first->markAsReductionLike(RT);
1257 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001258 }
Tobias Grosser75805372011-04-29 06:27:02 +00001259}
1260
Tobias Grosser74394f02013-01-14 22:40:23 +00001261std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001262
Tobias Grosser54839312015-04-21 11:37:25 +00001263std::string ScopStmt::getScheduleStr() const {
Tobias Grosser808cd692015-07-14 09:33:13 +00001264 auto *S = getSchedule();
1265 auto Str = stringFromIslObj(S);
1266 isl_map_free(S);
1267 return Str;
Tobias Grosser75805372011-04-29 06:27:02 +00001268}
1269
Tobias Grosser74394f02013-01-14 22:40:23 +00001270unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001271
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001272unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001273
Tobias Grosser75805372011-04-29 06:27:02 +00001274const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1275
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001276const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001277 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001278}
1279
Tobias Grosser74394f02013-01-14 22:40:23 +00001280isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001281
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001282__isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001283
Tobias Grosser6e6c7e02015-03-30 12:22:39 +00001284__isl_give isl_space *ScopStmt::getDomainSpace() const {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001285 return isl_set_get_space(Domain);
1286}
1287
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001288__isl_give isl_id *ScopStmt::getDomainId() const {
1289 return isl_set_get_tuple_id(Domain);
1290}
Tobias Grossercd95b772012-08-30 11:49:38 +00001291
Tobias Grosser75805372011-04-29 06:27:02 +00001292ScopStmt::~ScopStmt() {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001293 DeleteContainerSeconds(InstructionToAccess);
Tobias Grosser75805372011-04-29 06:27:02 +00001294 isl_set_free(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +00001295}
1296
1297void ScopStmt::print(raw_ostream &OS) const {
1298 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001299 OS.indent(12) << "Domain :=\n";
1300
1301 if (Domain) {
1302 OS.indent(16) << getDomainStr() << ";\n";
1303 } else
1304 OS.indent(16) << "n/a\n";
1305
Tobias Grosser54839312015-04-21 11:37:25 +00001306 OS.indent(12) << "Schedule :=\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001307
1308 if (Domain) {
Tobias Grosser54839312015-04-21 11:37:25 +00001309 OS.indent(16) << getScheduleStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001310 } else
1311 OS.indent(16) << "n/a\n";
1312
Tobias Grosser083d3d32014-06-28 08:59:45 +00001313 for (MemoryAccess *Access : MemAccs)
1314 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001315}
1316
1317void ScopStmt::dump() const { print(dbgs()); }
1318
1319//===----------------------------------------------------------------------===//
1320/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001321
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001322void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001323 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1324 isl_set_free(Context);
1325 Context = NewContext;
1326}
1327
Tobias Grosserabfbe632013-02-05 12:09:06 +00001328void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001329 for (const SCEV *Parameter : NewParameters) {
Johannes Doerfertbe409962015-03-29 20:45:09 +00001330 Parameter = extractConstantFactor(Parameter, *SE).second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001331 if (ParameterIds.find(Parameter) != ParameterIds.end())
1332 continue;
1333
1334 int dimension = Parameters.size();
1335
1336 Parameters.push_back(Parameter);
1337 ParameterIds[Parameter] = dimension;
1338 }
1339}
1340
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001341__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1342 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001343
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001344 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001345 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001346
Tobias Grosser8f99c162011-11-15 11:38:55 +00001347 std::string ParameterName;
1348
1349 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1350 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001351 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001352 }
1353
1354 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001355 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001356
Tobias Grosser20532b82014-04-11 17:56:49 +00001357 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1358 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001359}
Tobias Grosser75805372011-04-29 06:27:02 +00001360
Tobias Grosser6be480c2011-11-08 15:41:13 +00001361void Scop::buildContext() {
1362 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001363 Context = isl_set_universe(isl_space_copy(Space));
1364 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001365}
1366
Tobias Grosser18daaca2012-05-22 10:47:27 +00001367void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001368 for (const auto &ParamID : ParameterIds) {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001369 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001370
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001371 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001372
Johannes Doerferte7044942015-02-24 11:58:30 +00001373 Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001374 }
1375}
1376
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001377void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001378 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001379 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001380
Tobias Grosser083d3d32014-06-28 08:59:45 +00001381 for (const auto &ParamID : ParameterIds) {
1382 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001383 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001384 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001385 }
1386
1387 // Align the parameters of all data structures to the model.
1388 Context = isl_set_align_params(Context, Space);
1389
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001390 for (ScopStmt &Stmt : *this)
1391 Stmt.realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001392}
1393
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001394void Scop::simplifyAssumedContext() {
1395 // The parameter constraints of the iteration domains give us a set of
1396 // constraints that need to hold for all cases where at least a single
1397 // statement iteration is executed in the whole scop. We now simplify the
1398 // assumed context under the assumption that such constraints hold and at
1399 // least a single statement iteration is executed. For cases where no
1400 // statement instances are executed, the assumptions we have taken about
1401 // the executed code do not matter and can be changed.
1402 //
1403 // WARNING: This only holds if the assumptions we have taken do not reduce
1404 // the set of statement instances that are executed. Otherwise we
1405 // may run into a case where the iteration domains suggest that
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001406 // for a certain set of parameter constraints no code is executed,
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001407 // but in the original program some computation would have been
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001408 // performed. In such a case, modifying the run-time conditions and
1409 // possibly influencing the run-time check may cause certain scops
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001410 // to not be executed.
1411 //
1412 // Example:
1413 //
1414 // When delinearizing the following code:
1415 //
1416 // for (long i = 0; i < 100; i++)
1417 // for (long j = 0; j < m; j++)
1418 // A[i+p][j] = 1.0;
1419 //
1420 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001421 // otherwise we would access out of bound data. Now, knowing that code is
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001422 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1423 AssumedContext =
1424 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001425 AssumedContext = isl_set_gist_params(AssumedContext, getContext());
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001426}
1427
Johannes Doerfertb164c792014-09-18 11:17:17 +00001428/// @brief Add the minimal/maximal access in @p Set to @p User.
Tobias Grosserb2f39922015-05-28 13:32:11 +00001429static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001430 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1431 isl_pw_multi_aff *MinPMA, *MaxPMA;
1432 isl_pw_aff *LastDimAff;
1433 isl_aff *OneAff;
1434 unsigned Pos;
1435
Johannes Doerfert9143d672014-09-27 11:02:39 +00001436 // Restrict the number of parameters involved in the access as the lexmin/
1437 // lexmax computation will take too long if this number is high.
1438 //
1439 // Experiments with a simple test case using an i7 4800MQ:
1440 //
1441 // #Parameters involved | Time (in sec)
1442 // 6 | 0.01
1443 // 7 | 0.04
1444 // 8 | 0.12
1445 // 9 | 0.40
1446 // 10 | 1.54
1447 // 11 | 6.78
1448 // 12 | 30.38
1449 //
1450 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1451 unsigned InvolvedParams = 0;
1452 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1453 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1454 InvolvedParams++;
1455
1456 if (InvolvedParams > RunTimeChecksMaxParameters) {
1457 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001458 return isl_stat_error;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001459 }
1460 }
1461
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001462 Set = isl_set_remove_divs(Set);
1463
Johannes Doerfertb164c792014-09-18 11:17:17 +00001464 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1465 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1466
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001467 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1468 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1469
Johannes Doerfertb164c792014-09-18 11:17:17 +00001470 // Adjust the last dimension of the maximal access by one as we want to
1471 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1472 // we test during code generation might now point after the end of the
1473 // allocated array but we will never dereference it anyway.
1474 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1475 "Assumed at least one output dimension");
1476 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1477 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1478 OneAff = isl_aff_zero_on_domain(
1479 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1480 OneAff = isl_aff_add_constant_si(OneAff, 1);
1481 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1482 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1483
1484 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1485
1486 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001487 return isl_stat_ok;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001488}
1489
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001490static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1491 isl_set *Domain = MA->getStatement()->getDomain();
1492 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1493 return isl_set_reset_tuple_id(Domain);
1494}
1495
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001496/// @brief Wrapper function to calculate minimal/maximal accesses to each array.
1497static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses,
Tobias Grosserbb853c22015-07-25 12:31:03 +00001498 __isl_take isl_union_set *Domains,
1499 __isl_take isl_set *AssumedContext,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001500 Scop::MinMaxVectorTy &MinMaxAccesses) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001501
1502 Accesses = isl_union_map_intersect_domain(Accesses, Domains);
1503 isl_union_set *Locations = isl_union_map_range(Accesses);
1504 Locations = isl_union_set_intersect_params(Locations, AssumedContext);
1505 Locations = isl_union_set_coalesce(Locations);
1506 Locations = isl_union_set_detect_equalities(Locations);
1507 bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001508 &MinMaxAccesses));
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001509 isl_union_set_free(Locations);
1510 return Valid;
1511}
1512
Johannes Doerfert9143d672014-09-27 11:02:39 +00001513bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001514 // To create sound alias checks we perform the following steps:
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001515 // o) Use the alias analysis and an alias set tracker to build alias sets
Johannes Doerfertb164c792014-09-18 11:17:17 +00001516 // for all memory accesses inside the SCoP.
1517 // o) For each alias set we then map the aliasing pointers back to the
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001518 // memory accesses we know, thus obtain groups of memory accesses which
Johannes Doerfertb164c792014-09-18 11:17:17 +00001519 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001520 // o) We divide each group based on the domains of the minimal/maximal
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001521 // accesses. That means two minimal/maximal accesses are only in a group
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001522 // if their access domains intersect, otherwise they are in different
1523 // ones.
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001524 // o) We partition each group into read only and non read only accesses.
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001525 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001526 // and maximal accesses to each array of a group in read only and non
1527 // read only partitions separately.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001528 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1529
1530 AliasSetTracker AST(AA);
1531
1532 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001533 DenseSet<Value *> HasWriteAccess;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001534 for (ScopStmt &Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001535
1536 // Skip statements with an empty domain as they will never be executed.
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001537 isl_set *StmtDomain = Stmt.getDomain();
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001538 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1539 isl_set_free(StmtDomain);
1540 if (StmtDomainEmpty)
1541 continue;
1542
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001543 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001544 if (MA->isScalar())
1545 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001546 if (!MA->isRead())
1547 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001548 Instruction *Acc = MA->getAccessInstruction();
1549 PtrToAcc[getPointerOperand(*Acc)] = MA;
1550 AST.add(Acc);
1551 }
1552 }
1553
1554 SmallVector<AliasGroupTy, 4> AliasGroups;
1555 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001556 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001557 continue;
1558 AliasGroupTy AG;
1559 for (auto PR : AS)
1560 AG.push_back(PtrToAcc[PR.getValue()]);
1561 assert(AG.size() > 1 &&
1562 "Alias groups should contain at least two accesses");
1563 AliasGroups.push_back(std::move(AG));
1564 }
1565
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001566 // Split the alias groups based on their domain.
1567 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1568 AliasGroupTy NewAG;
1569 AliasGroupTy &AG = AliasGroups[u];
1570 AliasGroupTy::iterator AGI = AG.begin();
1571 isl_set *AGDomain = getAccessDomain(*AGI);
1572 while (AGI != AG.end()) {
1573 MemoryAccess *MA = *AGI;
1574 isl_set *MADomain = getAccessDomain(MA);
1575 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1576 NewAG.push_back(MA);
1577 AGI = AG.erase(AGI);
1578 isl_set_free(MADomain);
1579 } else {
1580 AGDomain = isl_set_union(AGDomain, MADomain);
1581 AGI++;
1582 }
1583 }
1584 if (NewAG.size() > 1)
1585 AliasGroups.push_back(std::move(NewAG));
1586 isl_set_free(AGDomain);
1587 }
1588
Tobias Grosserf4c24b22015-04-05 13:11:54 +00001589 MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
Johannes Doerfert13771732014-10-01 12:40:46 +00001590 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1591 for (AliasGroupTy &AG : AliasGroups) {
1592 NonReadOnlyBaseValues.clear();
1593 ReadOnlyPairs.clear();
1594
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001595 if (AG.size() < 2) {
1596 AG.clear();
1597 continue;
1598 }
1599
Johannes Doerfert13771732014-10-01 12:40:46 +00001600 for (auto II = AG.begin(); II != AG.end();) {
1601 Value *BaseAddr = (*II)->getBaseAddr();
1602 if (HasWriteAccess.count(BaseAddr)) {
1603 NonReadOnlyBaseValues.insert(BaseAddr);
1604 II++;
1605 } else {
1606 ReadOnlyPairs[BaseAddr].insert(*II);
1607 II = AG.erase(II);
1608 }
1609 }
1610
1611 // If we don't have read only pointers check if there are at least two
1612 // non read only pointers, otherwise clear the alias group.
Tobias Grosserbb853c22015-07-25 12:31:03 +00001613 if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001614 AG.clear();
Johannes Doerfert13771732014-10-01 12:40:46 +00001615 continue;
1616 }
1617
1618 // If we don't have non read only pointers clear the alias group.
1619 if (NonReadOnlyBaseValues.empty()) {
1620 AG.clear();
1621 continue;
1622 }
1623
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001624 // Calculate minimal and maximal accesses for non read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001625 MinMaxAliasGroups.emplace_back();
1626 MinMaxVectorPairTy &pair = MinMaxAliasGroups.back();
1627 MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first;
1628 MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second;
1629 MinMaxAccessesNonReadOnly.reserve(AG.size());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001630
1631 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001632
1633 // AG contains only non read only accesses.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001634 for (MemoryAccess *MA : AG)
1635 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001636
Tobias Grosserbb853c22015-07-25 12:31:03 +00001637 bool Valid = calculateMinMaxAccess(
1638 Accesses, getDomains(), getAssumedContext(), MinMaxAccessesNonReadOnly);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001639
1640 // Bail out if the number of values we need to compare is too large.
1641 // This is important as the number of comparisions grows quadratically with
1642 // the number of values we need to compare.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001643 if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() >
1644 RunTimeChecksMaxArraysPerGroup))
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001645 return false;
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001646
1647 // Calculate minimal and maximal accesses for read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001648 MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001649 Accesses = isl_union_map_empty(getParamSpace());
1650
1651 for (const auto &ReadOnlyPair : ReadOnlyPairs)
1652 for (MemoryAccess *MA : ReadOnlyPair.second)
1653 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1654
Tobias Grosserbb853c22015-07-25 12:31:03 +00001655 Valid = calculateMinMaxAccess(Accesses, getDomains(), getAssumedContext(),
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001656 MinMaxAccessesReadOnly);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001657
1658 if (!Valid)
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001659 return false;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001660 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001661
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001662 return true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001663}
1664
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001665static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI,
1666 ScopDetection &SD) {
1667
1668 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R);
1669
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001670 unsigned MinLD = INT_MAX, MaxLD = 0;
1671 for (BasicBlock *BB : R.blocks()) {
1672 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00001673 if (!R.contains(L))
1674 continue;
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001675 if (BoxedLoops && BoxedLoops->count(L))
1676 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001677 unsigned LD = L->getLoopDepth();
1678 MinLD = std::min(MinLD, LD);
1679 MaxLD = std::max(MaxLD, LD);
1680 }
1681 }
1682
1683 // Handle the case that there is no loop in the SCoP first.
1684 if (MaxLD == 0)
1685 return 1;
1686
1687 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1688 assert(MaxLD >= MinLD &&
1689 "Maximal loop depth was smaller than mininaml loop depth?");
1690 return MaxLD - MinLD + 1;
1691}
1692
Michael Kruse471a5e32015-07-30 19:27:04 +00001693Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, isl_ctx *Context,
1694 unsigned MaxLoopDepth)
1695 : SE(&ScalarEvolution), R(R), IsOptimized(false),
1696 MaxLoopDepth(MaxLoopDepth), IslCtx(Context) {}
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001697
Michael Kruse471a5e32015-07-30 19:27:04 +00001698void Scop::initFromTempScop(TempScop &TempScop, LoopInfo &LI,
1699 ScopDetection &SD) {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001700 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001701
Tobias Grosserabfbe632013-02-05 12:09:06 +00001702 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001703
Tobias Grosser54839312015-04-21 11:37:25 +00001704 // Build the iteration domain, access functions and schedule functions
Tobias Grosser75805372011-04-29 06:27:02 +00001705 // traversing the region tree.
Michael Kruse471a5e32015-07-30 19:27:04 +00001706 Schedule = buildScop(TempScop, getRegion(), NestLoops, LI, SD);
Tobias Grosser808cd692015-07-14 09:33:13 +00001707 if (!Schedule)
1708 Schedule = isl_schedule_empty(getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +00001709
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001710 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001711 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001712 simplifyAssumedContext();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001713
Tobias Grosser75805372011-04-29 06:27:02 +00001714 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1715}
1716
Michael Kruse471a5e32015-07-30 19:27:04 +00001717Scop *Scop::createFromTempScop(TempScop &TempScop, LoopInfo &LI,
1718 ScalarEvolution &SE, ScopDetection &SD,
1719 isl_ctx *ctx) {
1720 auto &R = TempScop.getMaxRegion();
1721 auto MaxLoopDepth = getMaxLoopDepthInRegion(R, LI, SD);
1722 auto S = new Scop(R, SE, ctx, MaxLoopDepth);
1723 S->initFromTempScop(TempScop, LI, SD);
1724 return S;
1725}
1726
Tobias Grosser75805372011-04-29 06:27:02 +00001727Scop::~Scop() {
1728 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001729 isl_set_free(AssumedContext);
Tobias Grosser808cd692015-07-14 09:33:13 +00001730 isl_schedule_free(Schedule);
Tobias Grosser75805372011-04-29 06:27:02 +00001731
Johannes Doerfertb164c792014-09-18 11:17:17 +00001732 // Free the alias groups
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001733 for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001734 for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001735 isl_pw_multi_aff_free(MMA.first);
1736 isl_pw_multi_aff_free(MMA.second);
1737 }
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001738 for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001739 isl_pw_multi_aff_free(MMA.first);
1740 isl_pw_multi_aff_free(MMA.second);
1741 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001742 }
Tobias Grosser75805372011-04-29 06:27:02 +00001743}
1744
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001745const ScopArrayInfo *
1746Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
Tobias Grosser92245222015-07-28 14:53:44 +00001747 const SmallVector<const SCEV *, 4> &Sizes,
1748 bool IsPHI) {
1749 auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001750 if (!SAI)
Tobias Grosser92245222015-07-28 14:53:44 +00001751 SAI.reset(
1752 new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI));
Tobias Grosserab671442015-05-23 05:58:27 +00001753 return SAI.get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001754}
1755
Tobias Grosser92245222015-07-28 14:53:44 +00001756const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) {
1757 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001758 assert(SAI && "No ScopArrayInfo available for this base pointer");
1759 return SAI;
1760}
1761
Tobias Grosser74394f02013-01-14 22:40:23 +00001762std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001763std::string Scop::getAssumedContextStr() const {
1764 return stringFromIslObj(AssumedContext);
1765}
Tobias Grosser75805372011-04-29 06:27:02 +00001766
1767std::string Scop::getNameStr() const {
1768 std::string ExitName, EntryName;
1769 raw_string_ostream ExitStr(ExitName);
1770 raw_string_ostream EntryStr(EntryName);
1771
Tobias Grosserf240b482014-01-09 10:42:15 +00001772 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001773 EntryStr.str();
1774
1775 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001776 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001777 ExitStr.str();
1778 } else
1779 ExitName = "FunctionExit";
1780
1781 return EntryName + "---" + ExitName;
1782}
1783
Tobias Grosser74394f02013-01-14 22:40:23 +00001784__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001785__isl_give isl_space *Scop::getParamSpace() const {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001786 return isl_set_get_space(Context);
Tobias Grosser37487052011-10-06 00:03:42 +00001787}
1788
Tobias Grossere86109f2013-10-29 21:05:49 +00001789__isl_give isl_set *Scop::getAssumedContext() const {
1790 return isl_set_copy(AssumedContext);
1791}
1792
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001793void Scop::addAssumption(__isl_take isl_set *Set) {
1794 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001795 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001796}
1797
Tobias Grosser75805372011-04-29 06:27:02 +00001798void Scop::printContext(raw_ostream &OS) const {
1799 OS << "Context:\n";
1800
1801 if (!Context) {
1802 OS.indent(4) << "n/a\n\n";
1803 return;
1804 }
1805
1806 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001807
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001808 OS.indent(4) << "Assumed Context:\n";
1809 if (!AssumedContext) {
1810 OS.indent(4) << "n/a\n\n";
1811 return;
1812 }
1813
1814 OS.indent(4) << getAssumedContextStr() << "\n";
1815
Tobias Grosser083d3d32014-06-28 08:59:45 +00001816 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001817 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001818 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1819 }
Tobias Grosser75805372011-04-29 06:27:02 +00001820}
1821
Johannes Doerfertb164c792014-09-18 11:17:17 +00001822void Scop::printAliasAssumptions(raw_ostream &OS) const {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001823 int noOfGroups = 0;
1824 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001825 if (Pair.second.size() == 0)
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001826 noOfGroups += 1;
1827 else
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001828 noOfGroups += Pair.second.size();
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001829 }
1830
Tobias Grosserbb853c22015-07-25 12:31:03 +00001831 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n";
Johannes Doerfertb164c792014-09-18 11:17:17 +00001832 if (MinMaxAliasGroups.empty()) {
1833 OS.indent(8) << "n/a\n";
1834 return;
1835 }
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001836
Tobias Grosserbb853c22015-07-25 12:31:03 +00001837 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001838
1839 // If the group has no read only accesses print the write accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001840 if (Pair.second.empty()) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001841 OS.indent(8) << "[[";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001842 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001843 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1844 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001845 }
1846 OS << " ]]\n";
1847 }
1848
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001849 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001850 OS.indent(8) << "[[";
Tobias Grosserbb853c22015-07-25 12:31:03 +00001851 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001852 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001853 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1854 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001855 }
1856 OS << " ]]\n";
1857 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001858 }
1859}
1860
Tobias Grosser75805372011-04-29 06:27:02 +00001861void Scop::printStatements(raw_ostream &OS) const {
1862 OS << "Statements {\n";
1863
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001864 for (const ScopStmt &Stmt : *this)
1865 OS.indent(4) << Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001866
1867 OS.indent(4) << "}\n";
1868}
1869
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001870void Scop::printArrayInfo(raw_ostream &OS) const {
1871 OS << "Arrays {\n";
1872
Tobias Grosserab671442015-05-23 05:58:27 +00001873 for (auto &Array : arrays())
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001874 Array.second->print(OS);
1875
1876 OS.indent(4) << "}\n";
1877}
1878
Tobias Grosser75805372011-04-29 06:27:02 +00001879void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001880 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1881 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001882 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00001883 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001884 printContext(OS.indent(4));
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001885 printArrayInfo(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001886 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001887 printStatements(OS.indent(4));
1888}
1889
1890void Scop::dump() const { print(dbgs()); }
1891
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001892isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001893
Tobias Grosser808cd692015-07-14 09:33:13 +00001894__isl_give isl_union_set *Scop::getDomains() const {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001895 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001896
Tobias Grosser808cd692015-07-14 09:33:13 +00001897 for (const ScopStmt &Stmt : *this)
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001898 Domain = isl_union_set_add_set(Domain, Stmt.getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001899
1900 return Domain;
1901}
1902
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001903__isl_give isl_union_map *Scop::getMustWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001904 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001905
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001906 for (ScopStmt &Stmt : *this) {
1907 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001908 if (!MA->isMustWrite())
1909 continue;
1910
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001911 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001912 isl_map *AccessDomain = MA->getAccessRelation();
1913 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1914 Write = isl_union_map_add_map(Write, AccessDomain);
1915 }
1916 }
1917 return isl_union_map_coalesce(Write);
1918}
1919
1920__isl_give isl_union_map *Scop::getMayWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001921 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001922
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001923 for (ScopStmt &Stmt : *this) {
1924 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001925 if (!MA->isMayWrite())
1926 continue;
1927
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001928 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001929 isl_map *AccessDomain = MA->getAccessRelation();
1930 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1931 Write = isl_union_map_add_map(Write, AccessDomain);
1932 }
1933 }
1934 return isl_union_map_coalesce(Write);
1935}
1936
Tobias Grosser37eb4222014-02-20 21:43:54 +00001937__isl_give isl_union_map *Scop::getWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001938 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001939
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001940 for (ScopStmt &Stmt : *this) {
1941 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001942 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001943 continue;
1944
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001945 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001946 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001947 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1948 Write = isl_union_map_add_map(Write, AccessDomain);
1949 }
1950 }
1951 return isl_union_map_coalesce(Write);
1952}
1953
1954__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001955 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001956
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001957 for (ScopStmt &Stmt : *this) {
1958 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001959 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001960 continue;
1961
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001962 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001963 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001964
1965 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1966 Read = isl_union_map_add_map(Read, AccessDomain);
1967 }
1968 }
1969 return isl_union_map_coalesce(Read);
1970}
1971
Tobias Grosser808cd692015-07-14 09:33:13 +00001972__isl_give isl_union_map *Scop::getSchedule() const {
1973 auto Tree = getScheduleTree();
1974 auto S = isl_schedule_get_map(Tree);
1975 isl_schedule_free(Tree);
1976 return S;
1977}
Tobias Grosser37eb4222014-02-20 21:43:54 +00001978
Tobias Grosser808cd692015-07-14 09:33:13 +00001979__isl_give isl_schedule *Scop::getScheduleTree() const {
1980 return isl_schedule_intersect_domain(isl_schedule_copy(Schedule),
1981 getDomains());
1982}
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001983
Tobias Grosser808cd692015-07-14 09:33:13 +00001984void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) {
1985 auto *S = isl_schedule_from_domain(getDomains());
1986 S = isl_schedule_insert_partial_schedule(
1987 S, isl_multi_union_pw_aff_from_union_map(NewSchedule));
1988 isl_schedule_free(Schedule);
1989 Schedule = S;
1990}
1991
1992void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) {
1993 isl_schedule_free(Schedule);
1994 Schedule = NewSchedule;
Tobias Grosser37eb4222014-02-20 21:43:54 +00001995}
1996
1997bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1998 bool Changed = false;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001999 for (ScopStmt &Stmt : *this) {
2000 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002001 isl_union_set *NewStmtDomain = isl_union_set_intersect(
2002 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
2003
2004 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
2005 isl_union_set_free(StmtDomain);
2006 isl_union_set_free(NewStmtDomain);
2007 continue;
2008 }
2009
2010 Changed = true;
2011
2012 isl_union_set_free(StmtDomain);
2013 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
2014
2015 if (isl_union_set_is_empty(NewStmtDomain)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002016 Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002017 isl_union_set_free(NewStmtDomain);
2018 } else
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002019 Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002020 }
2021 isl_union_set_free(Domain);
2022 return Changed;
2023}
2024
Tobias Grosser75805372011-04-29 06:27:02 +00002025ScalarEvolution *Scop::getSE() const { return SE; }
2026
2027bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
2028 if (tempScop.getAccessFunctions(BB))
2029 return false;
2030
2031 return true;
2032}
2033
Tobias Grosser808cd692015-07-14 09:33:13 +00002034struct MapToDimensionDataTy {
2035 int N;
2036 isl_union_pw_multi_aff *Res;
2037};
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002038
Tobias Grosser808cd692015-07-14 09:33:13 +00002039// @brief Create a function that maps the elements of 'Set' to its N-th
2040// dimension.
2041//
2042// The result is added to 'User->Res'.
2043//
2044// @param Set The input set.
2045// @param N The dimension to map to.
2046//
2047// @returns Zero if no error occurred, non-zero otherwise.
2048static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) {
2049 struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User;
2050 int Dim;
2051 isl_space *Space;
2052 isl_pw_multi_aff *PMA;
2053
2054 Dim = isl_set_dim(Set, isl_dim_set);
2055 Space = isl_set_get_space(Set);
2056 PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N,
2057 Dim - Data->N);
2058 if (Data->N > 1)
2059 PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1);
2060 Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA);
2061
2062 isl_set_free(Set);
2063
2064 return isl_stat_ok;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002065}
2066
Tobias Grosser808cd692015-07-14 09:33:13 +00002067// @brief Create a function that maps the elements of Domain to their Nth
2068// dimension.
2069//
2070// @param Domain The set of elements to map.
2071// @param N The dimension to map to.
2072static __isl_give isl_multi_union_pw_aff *
2073mapToDimension(__isl_take isl_union_set *Domain, int N) {
2074 struct MapToDimensionDataTy Data;
2075 isl_space *Space;
2076
2077 Space = isl_union_set_get_space(Domain);
2078 Data.N = N;
2079 Data.Res = isl_union_pw_multi_aff_empty(Space);
2080 if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0)
2081 Data.Res = isl_union_pw_multi_aff_free(Data.Res);
2082
2083 isl_union_set_free(Domain);
2084 return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res);
2085}
2086
2087ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R, TempScop &tempScop,
2088 const Region &CurRegion,
2089 SmallVectorImpl<Loop *> &NestLoops) {
2090 ScopStmt *Stmt;
2091 if (BB) {
2092 Stmts.emplace_back(*this, tempScop, CurRegion, *BB, NestLoops);
2093 Stmt = &Stmts.back();
2094 StmtMap[BB] = Stmt;
2095 } else {
2096 assert(R && "Either basic block or a region expected.");
2097 Stmts.emplace_back(*this, tempScop, CurRegion, *R, NestLoops);
2098 Stmt = &Stmts.back();
2099 for (BasicBlock *BB : R->blocks())
2100 StmtMap[BB] = Stmt;
2101 }
2102 return Stmt;
2103}
2104
Michael Kruse046dde42015-08-10 13:01:57 +00002105__isl_give isl_schedule *
2106Scop::buildBBScopStmt(BasicBlock *BB, TempScop &tempScop,
2107 const Region &CurRegion,
2108 SmallVectorImpl<Loop *> &NestLoops) {
2109 if (isTrivialBB(BB, tempScop))
2110 return nullptr;
2111
2112 auto *Stmt = addScopStmt(BB, nullptr, tempScop, CurRegion, NestLoops);
2113 auto *Domain = Stmt->getDomain();
2114 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2115}
2116
Tobias Grosser808cd692015-07-14 09:33:13 +00002117__isl_give isl_schedule *Scop::buildScop(TempScop &tempScop,
2118 const Region &CurRegion,
2119 SmallVectorImpl<Loop *> &NestLoops,
2120 LoopInfo &LI, ScopDetection &SD) {
2121 if (SD.isNonAffineSubRegion(&CurRegion, &getRegion())) {
2122 auto *Stmt = addScopStmt(nullptr, const_cast<Region *>(&CurRegion),
2123 tempScop, CurRegion, NestLoops);
2124 auto *Domain = Stmt->getDomain();
2125 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2126 }
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002127
Tobias Grosser75805372011-04-29 06:27:02 +00002128 Loop *L = castToLoop(CurRegion, LI);
2129
2130 if (L)
2131 NestLoops.push_back(L);
2132
2133 unsigned loopDepth = NestLoops.size();
Tobias Grosser808cd692015-07-14 09:33:13 +00002134 isl_schedule *Schedule = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002135
2136 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00002137 E = CurRegion.element_end();
Tobias Grosser808cd692015-07-14 09:33:13 +00002138 I != E; ++I) {
2139 isl_schedule *StmtSchedule = nullptr;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002140 if (I->isSubRegion()) {
Tobias Grosser808cd692015-07-14 09:33:13 +00002141 StmtSchedule =
2142 buildScop(tempScop, *I->getNodeAs<Region>(), NestLoops, LI, SD);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002143 } else {
Michael Kruse046dde42015-08-10 13:01:57 +00002144 StmtSchedule = buildBBScopStmt(I->getNodeAs<BasicBlock>(), tempScop,
2145 CurRegion, NestLoops);
Tobias Grosser75805372011-04-29 06:27:02 +00002146 }
Michael Kruse046dde42015-08-10 13:01:57 +00002147 Schedule = combineInSequence(Schedule, StmtSchedule);
Tobias Grosser808cd692015-07-14 09:33:13 +00002148 }
Tobias Grosser75805372011-04-29 06:27:02 +00002149
Tobias Grosser808cd692015-07-14 09:33:13 +00002150 if (!L)
2151 return Schedule;
2152
2153 auto *Domain = isl_schedule_get_domain(Schedule);
2154 if (!isl_union_set_is_empty(Domain)) {
2155 auto *MUPA = mapToDimension(isl_union_set_copy(Domain), loopDepth);
2156 Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA);
2157 }
2158 isl_union_set_free(Domain);
2159
Tobias Grosser75805372011-04-29 06:27:02 +00002160 NestLoops.pop_back();
Tobias Grosser808cd692015-07-14 09:33:13 +00002161 return Schedule;
Tobias Grosser75805372011-04-29 06:27:02 +00002162}
2163
Johannes Doerfert7c494212014-10-31 23:13:39 +00002164ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
Tobias Grosser57411e32015-05-27 06:51:34 +00002165 auto StmtMapIt = StmtMap.find(BB);
Johannes Doerfert7c494212014-10-31 23:13:39 +00002166 if (StmtMapIt == StmtMap.end())
2167 return nullptr;
2168 return StmtMapIt->second;
2169}
2170
Tobias Grosser75805372011-04-29 06:27:02 +00002171//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00002172ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
2173 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00002174 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00002175}
2176
2177ScopInfo::~ScopInfo() {
2178 clear();
2179 isl_ctx_free(ctx);
2180}
2181
Tobias Grosser75805372011-04-29 06:27:02 +00002182void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00002183 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00002184 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00002185 AU.addRequired<ScalarEvolution>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002186 AU.addRequired<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002187 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002188 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00002189 AU.setPreservesAll();
2190}
2191
2192bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Chandler Carruthf5579872015-01-17 14:16:56 +00002193 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002194 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002195 ScopDetection &SD = getAnalysis<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002196 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
2197
2198 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
2199
2200 // This region is no Scop.
2201 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00002202 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002203 return false;
2204 }
2205
Michael Kruse471a5e32015-07-30 19:27:04 +00002206 scop = Scop::createFromTempScop(*tempScop, LI, SE, SD, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00002207
Tobias Grosserd6a50b32015-05-30 06:26:21 +00002208 DEBUG(scop->print(dbgs()));
2209
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002210 if (!PollyUseRuntimeAliasChecks) {
2211 // Statistics.
2212 ++ScopFound;
2213 if (scop->getMaxLoopDepth() > 0)
2214 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002215 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002216 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002217
Johannes Doerfert9143d672014-09-27 11:02:39 +00002218 // If a problem occurs while building the alias groups we need to delete
2219 // this SCoP and pretend it wasn't valid in the first place.
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002220 if (scop->buildAliasGroups(AA)) {
2221 // Statistics.
2222 ++ScopFound;
2223 if (scop->getMaxLoopDepth() > 0)
2224 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002225 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002226 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00002227
2228 DEBUG(dbgs()
2229 << "\n\nNOTE: Run time checks for " << scop->getNameStr()
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002230 << " could not be created as the number of parameters involved is too "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002231 "high. The SCoP will be "
2232 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the "
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002233 "maximal number of parameters but be advised that the compile time "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002234 "might increase exponentially.\n\n");
2235
2236 delete scop;
2237 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002238 return false;
2239}
2240
2241char ScopInfo::ID = 0;
2242
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002243Pass *polly::createScopInfoPass() { return new ScopInfo(); }
2244
Tobias Grosser73600b82011-10-08 00:30:40 +00002245INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
2246 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002247 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00002248INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Chandler Carruthf5579872015-01-17 14:16:56 +00002249INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00002250INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002251INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002252INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002253INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00002254INITIALIZE_PASS_END(ScopInfo, "polly-scops",
2255 "Polly - Create polyhedral description of Scops", false,
2256 false)