blob: 28214685b5950fbe851a4d4456fb7fcd2c70da40 [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
Tobias Grosser0695ee42013-09-17 03:30:31 +000079/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000080struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000081public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000082 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
83 ///
84 /// @param Stmt The location at which the scalar evolution expression
85 /// is evaluated.
86 /// @param Expr The expression that is translated.
87 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
88
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000089private:
Tobias Grosser3cc99742012-06-06 16:33:15 +000090 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000091 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +000092 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000093
Tobias Grosser0695ee42013-09-17 03:30:31 +000094 SCEVAffinator(const ScopStmt *Stmt);
95 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +000096
Tobias Grosser0695ee42013-09-17 03:30:31 +000097 __isl_give isl_pw_aff *visit(const SCEV *Expr);
98 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
99 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
100 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
101 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
102 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
103 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
104 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
105 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
106 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
107 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
108 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000109 __isl_give isl_pw_aff *visitSDivInstruction(Instruction *SDiv);
Tobias Grosser50165ff2015-06-24 04:13:29 +0000110 __isl_give isl_pw_aff *visitSRemInstruction(Instruction *SDiv);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000111
Tobias Grosser0695ee42013-09-17 03:30:31 +0000112 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
113};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000114
Tobias Grosser0695ee42013-09-17 03:30:31 +0000115SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
116 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
117 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000118
Tobias Grosser0695ee42013-09-17 03:30:31 +0000119__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
120 const SCEV *Scev) {
121 Scop *S = Stmt->getParent();
122 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000123
Tobias Grosser0695ee42013-09-17 03:30:31 +0000124 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000125
Tobias Grosser0695ee42013-09-17 03:30:31 +0000126 SCEVAffinator Affinator(Stmt);
127 return Affinator.visit(Scev);
128}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000129
Tobias Grosser0695ee42013-09-17 03:30:31 +0000130__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
131 // In case the scev is a valid parameter, we do not further analyze this
132 // expression, but create a new parameter in the isl_pw_aff. This allows us
133 // to treat subexpressions that we cannot translate into an piecewise affine
134 // expression, as constant parameters of the piecewise affine expression.
135 if (isl_id *Id = S->getIdForParam(Expr)) {
136 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
137 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000138
Tobias Grosser0695ee42013-09-17 03:30:31 +0000139 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
140 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
141 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000142
143 return isl_pw_aff_alloc(Domain, Affine);
144 }
145
Tobias Grosser0695ee42013-09-17 03:30:31 +0000146 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
147}
148
Tobias Grosser0d170132013-10-03 13:09:19 +0000149__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000150 ConstantInt *Value = Expr->getValue();
151 isl_val *v;
152
153 // LLVM does not define if an integer value is interpreted as a signed or
154 // unsigned value. Hence, without further information, it is unknown how
155 // this value needs to be converted to GMP. At the moment, we only support
156 // signed operations. So we just interpret it as signed. Later, there are
157 // two options:
158 //
159 // 1. We always interpret any value as signed and convert the values on
160 // demand.
161 // 2. We pass down the signedness of the calculation and use it to interpret
162 // this constant correctly.
163 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
164
165 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
Johannes Doerfert9c147372014-11-19 15:36:59 +0000166 isl_local_space *ls = isl_local_space_from_space(Space);
167 return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000168}
169
170__isl_give isl_pw_aff *
171SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
172 llvm_unreachable("SCEVTruncateExpr not yet supported");
173}
174
175__isl_give isl_pw_aff *
176SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
177 llvm_unreachable("SCEVZeroExtendExpr not yet supported");
178}
179
180__isl_give isl_pw_aff *
181SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
182 // Assuming the value is signed, a sign extension is basically a noop.
183 // TODO: Reconsider this as soon as we support unsigned values.
184 return visit(Expr->getOperand());
185}
186
187__isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
188 isl_pw_aff *Sum = visit(Expr->getOperand(0));
189
190 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
191 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
192 Sum = isl_pw_aff_add(Sum, NextSummand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000193 }
194
Tobias Grosser0695ee42013-09-17 03:30:31 +0000195 // TODO: Check for NSW and NUW.
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000196
Tobias Grosser0695ee42013-09-17 03:30:31 +0000197 return Sum;
198}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000199
Tobias Grosser0695ee42013-09-17 03:30:31 +0000200__isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
Johannes Doerfertbe409962015-03-29 20:45:09 +0000201 // Divide Expr into a constant part and the rest. Then visit both and multiply
202 // the result to obtain the representation for Expr. While the second part of
203 // ConstantAndLeftOverPair might still be a SCEVMulExpr we will not get to
204 // this point again. The reason is that if it is a multiplication it consists
205 // only of parameters and we will stop in the visit(const SCEV *) function and
206 // return the isl_pw_aff for that parameter.
207 auto ConstantAndLeftOverPair = extractConstantFactor(Expr, *S->getSE());
208 return isl_pw_aff_mul(visit(ConstantAndLeftOverPair.first),
209 visit(ConstantAndLeftOverPair.second));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000210}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000211
Tobias Grosser0695ee42013-09-17 03:30:31 +0000212__isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
213 llvm_unreachable("SCEVUDivExpr not yet supported");
214}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000215
Tobias Grosser0695ee42013-09-17 03:30:31 +0000216__isl_give isl_pw_aff *
217SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
218 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000219
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000220 auto Flags = Expr->getNoWrapFlags();
221
Tobias Grosser0695ee42013-09-17 03:30:31 +0000222 // Directly generate isl_pw_aff for Expr if 'start' is zero.
223 if (Expr->getStart()->isZero()) {
224 assert(S->getRegion().contains(Expr->getLoop()) &&
225 "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000226
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000227 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser0695ee42013-09-17 03:30:31 +0000228 isl_pw_aff *Step = visit(Expr->getOperand(1));
229 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
230 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000231
Tobias Grosser0695ee42013-09-17 03:30:31 +0000232 int loopDimension = getLoopDepth(Expr->getLoop());
233
234 isl_aff *LAff = isl_aff_set_coefficient_si(
235 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
236 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
237
238 // TODO: Do we need to check for NSW and NUW?
239 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000240 }
241
Tobias Grosser0695ee42013-09-17 03:30:31 +0000242 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
243 // if 'start' is not zero.
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000244 // TODO: Using the original SCEV no-wrap flags is not always safe, however
245 // as our code generation is reordering the expression anyway it doesn't
246 // really matter.
Tobias Grosser0695ee42013-09-17 03:30:31 +0000247 ScalarEvolution &SE = *S->getSE();
Johannes Doerfertd5d8f672015-04-26 19:55:21 +0000248 const SCEV *ZeroStartExpr =
249 SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
250 Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000251
Tobias Grosser0695ee42013-09-17 03:30:31 +0000252 isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
253 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000254
Tobias Grosser0695ee42013-09-17 03:30:31 +0000255 return isl_pw_aff_add(ZeroStartResult, Start);
256}
257
258__isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
259 isl_pw_aff *Max = visit(Expr->getOperand(0));
260
261 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
262 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
263 Max = isl_pw_aff_max(Max, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000264 }
265
Tobias Grosser0695ee42013-09-17 03:30:31 +0000266 return Max;
267}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000268
Tobias Grosser0695ee42013-09-17 03:30:31 +0000269__isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
270 llvm_unreachable("SCEVUMaxExpr not yet supported");
271}
272
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000273__isl_give isl_pw_aff *SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
274 assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
275 auto *SE = S->getSE();
276
277 auto *Divisor = SDiv->getOperand(1);
278 auto *DivisorSCEV = SE->getSCEV(Divisor);
279 auto *DivisorPWA = visit(DivisorSCEV);
280 assert(isa<ConstantInt>(Divisor) &&
281 "SDiv is no parameter but has a non-constant RHS.");
282
283 auto *Dividend = SDiv->getOperand(0);
284 auto *DividendSCEV = SE->getSCEV(Dividend);
285 auto *DividendPWA = visit(DividendSCEV);
286 return isl_pw_aff_tdiv_q(DividendPWA, DivisorPWA);
287}
288
Tobias Grosser50165ff2015-06-24 04:13:29 +0000289__isl_give isl_pw_aff *SCEVAffinator::visitSRemInstruction(Instruction *SRem) {
290 assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!");
291 auto *SE = S->getSE();
292
293 auto *Divisor = dyn_cast<ConstantInt>(SRem->getOperand(1));
294 assert(Divisor && "SRem is no parameter but has a non-constant RHS.");
295 auto *DivisorVal = isl_valFromAPInt(Ctx, Divisor->getValue(),
296 /* isSigned */ true);
297
298 auto *Dividend = SRem->getOperand(0);
299 auto *DividendSCEV = SE->getSCEV(Dividend);
300 auto *DividendPWA = visit(DividendSCEV);
301
302 return isl_pw_aff_mod_val(DividendPWA, isl_val_abs(DivisorVal));
303}
304
Tobias Grosser0695ee42013-09-17 03:30:31 +0000305__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000306 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
307 switch (I->getOpcode()) {
308 case Instruction::SDiv:
309 return visitSDivInstruction(I);
Tobias Grosser50165ff2015-06-24 04:13:29 +0000310 case Instruction::SRem:
311 return visitSRemInstruction(I);
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000312 default:
313 break; // Fall through.
314 }
315 }
316
317 llvm_unreachable(
318 "Unknowns SCEV was neither parameter nor a valid instruction.");
Tobias Grosser0695ee42013-09-17 03:30:31 +0000319}
320
321int SCEVAffinator::getLoopDepth(const Loop *L) {
322 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
323 assert(outerLoop && "Scop does not contain this loop");
324 return L->getLoopDepth() - outerLoop->getLoopDepth();
325}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000326
Johannes Doerferte7044942015-02-24 11:58:30 +0000327/// @brief Add the bounds of @p Range to the set @p S for dimension @p dim.
328static __isl_give isl_set *addRangeBoundsToSet(__isl_take isl_set *S,
329 const ConstantRange &Range,
330 int dim,
331 enum isl_dim_type type) {
332 isl_val *V;
333 isl_ctx *ctx = isl_set_get_ctx(S);
334
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000335 bool useLowerUpperBound = Range.isSignWrappedSet() && !Range.isFullSet();
336 const auto LB = useLowerUpperBound ? Range.getLower() : Range.getSignedMin();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000337 V = isl_valFromAPInt(ctx, LB, true);
Johannes Doerferte7044942015-02-24 11:58:30 +0000338 isl_set *SLB = isl_set_lower_bound_val(isl_set_copy(S), type, dim, V);
339
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000340 const auto UB = useLowerUpperBound ? Range.getUpper() : Range.getSignedMax();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000341 V = isl_valFromAPInt(ctx, UB, true);
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000342 if (useLowerUpperBound)
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000343 V = isl_val_sub_ui(V, 1);
Johannes Doerferte7044942015-02-24 11:58:30 +0000344 isl_set *SUB = isl_set_upper_bound_val(S, type, dim, V);
345
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000346 if (useLowerUpperBound)
Johannes Doerferte7044942015-02-24 11:58:30 +0000347 return isl_set_union(SLB, SUB);
348 else
349 return isl_set_intersect(SLB, SUB);
350}
351
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000352ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx,
Tobias Grosser92245222015-07-28 14:53:44 +0000353 const SmallVector<const SCEV *, 4> &DimensionSizes,
354 bool IsPHI)
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000355 : BasePtr(BasePtr), ElementType(ElementType),
Tobias Grosser92245222015-07-28 14:53:44 +0000356 DimensionSizes(DimensionSizes), IsPHI(IsPHI) {
357 std::string BasePtrName =
358 getIslCompatibleName("MemRef_", BasePtr, IsPHI ? "__phi" : "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000359 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
360}
361
362ScopArrayInfo::~ScopArrayInfo() { isl_id_free(Id); }
363
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000364std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); }
365
366int ScopArrayInfo::getElemSizeInBytes() const {
367 return ElementType->getPrimitiveSizeInBits() / 8;
368}
369
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000370isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
371
372void ScopArrayInfo::dump() const { print(errs()); }
373
374void ScopArrayInfo::print(raw_ostream &OS) const {
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000375 OS.indent(8) << *getElementType() << " " << getName() << "[*]";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000376 for (unsigned u = 0; u < getNumberOfDimensions(); u++)
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000377 OS << "[" << *DimensionSizes[u] << "]";
378 OS << " // Element size " << getElemSizeInBytes() << "\n";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000379}
380
381const ScopArrayInfo *
382ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
383 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
384 assert(Id && "Output dimension didn't have an ID");
385 return getFromId(Id);
386}
387
388const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
389 void *User = isl_id_get_user(Id);
390 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
391 isl_id_free(Id);
392 return SAI;
393}
394
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000395const std::string
396MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
397 switch (RT) {
398 case MemoryAccess::RT_NONE:
399 llvm_unreachable("Requested a reduction operator string for a memory "
400 "access which isn't a reduction");
401 case MemoryAccess::RT_ADD:
402 return "+";
403 case MemoryAccess::RT_MUL:
404 return "*";
405 case MemoryAccess::RT_BOR:
406 return "|";
407 case MemoryAccess::RT_BXOR:
408 return "^";
409 case MemoryAccess::RT_BAND:
410 return "&";
411 }
412 llvm_unreachable("Unknown reduction type");
413 return "";
414}
415
Johannes Doerfertf6183392014-07-01 20:52:51 +0000416/// @brief Return the reduction type for a given binary operator
417static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
418 const Instruction *Load) {
419 if (!BinOp)
420 return MemoryAccess::RT_NONE;
421 switch (BinOp->getOpcode()) {
422 case Instruction::FAdd:
423 if (!BinOp->hasUnsafeAlgebra())
424 return MemoryAccess::RT_NONE;
425 // Fall through
426 case Instruction::Add:
427 return MemoryAccess::RT_ADD;
428 case Instruction::Or:
429 return MemoryAccess::RT_BOR;
430 case Instruction::Xor:
431 return MemoryAccess::RT_BXOR;
432 case Instruction::And:
433 return MemoryAccess::RT_BAND;
434 case Instruction::FMul:
435 if (!BinOp->hasUnsafeAlgebra())
436 return MemoryAccess::RT_NONE;
437 // Fall through
438 case Instruction::Mul:
439 if (DisableMultiplicativeReductions)
440 return MemoryAccess::RT_NONE;
441 return MemoryAccess::RT_MUL;
442 default:
443 return MemoryAccess::RT_NONE;
444 }
445}
Tobias Grosser75805372011-04-29 06:27:02 +0000446//===----------------------------------------------------------------------===//
447
448MemoryAccess::~MemoryAccess() {
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000449 isl_id_free(Id);
Tobias Grosser54a86e62011-08-18 06:31:46 +0000450 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000451 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000452}
453
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000454static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
455 switch (Access.getType()) {
456 case IRAccess::READ:
457 return MemoryAccess::READ;
458 case IRAccess::MUST_WRITE:
459 return MemoryAccess::MUST_WRITE;
460 case IRAccess::MAY_WRITE:
461 return MemoryAccess::MAY_WRITE;
462 }
463 llvm_unreachable("Unknown IRAccess type!");
464}
465
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000466const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
467 isl_id *ArrayId = getArrayId();
468 void *User = isl_id_get_user(ArrayId);
469 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
470 isl_id_free(ArrayId);
471 return SAI;
472}
473
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000474__isl_give isl_id *MemoryAccess::getArrayId() const {
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000475 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
476}
477
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000478__isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation(
479 __isl_take isl_union_map *USchedule) const {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000480 isl_map *Schedule, *ScheduledAccRel;
481 isl_union_set *UDomain;
482
483 UDomain = isl_union_set_from_set(getStatement()->getDomain());
484 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
485 Schedule = isl_map_from_union_map(USchedule);
486 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
487 return isl_pw_multi_aff_from_map(ScheduledAccRel);
488}
489
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000490__isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000491 return isl_map_copy(AccessRelation);
492}
493
Johannes Doerferta99130f2014-10-13 12:58:03 +0000494std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000495 return stringFromIslObj(AccessRelation);
496}
497
Johannes Doerferta99130f2014-10-13 12:58:03 +0000498__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000499 return isl_map_get_space(AccessRelation);
500}
501
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000502__isl_give isl_map *MemoryAccess::getNewAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000503 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000504}
505
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000506__isl_give isl_basic_map *
507MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000508 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000509 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000510
Tobias Grosser084d8f72012-05-29 09:29:44 +0000511 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000512 isl_basic_set_universe(Statement->getDomainSpace()),
513 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000514}
515
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000516// Formalize no out-of-bound access assumption
517//
518// When delinearizing array accesses we optimistically assume that the
519// delinearized accesses do not access out of bound locations (the subscript
520// expression of each array evaluates for each statement instance that is
521// executed to a value that is larger than zero and strictly smaller than the
522// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000523// dimension for which we do not need to assume any upper bound. At this point
524// we formalize this assumption to ensure that at code generation time the
525// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000526//
527// To find the set of constraints necessary to avoid out of bound accesses, we
528// first build the set of data locations that are not within array bounds. We
529// then apply the reverse access relation to obtain the set of iterations that
530// may contain invalid accesses and reduce this set of iterations to the ones
531// that are actually executed by intersecting them with the domain of the
532// statement. If we now project out all loop dimensions, we obtain a set of
533// parameters that may cause statement instances to be executed that may
534// possibly yield out of bound memory accesses. The complement of these
535// constraints is the set of constraints that needs to be assumed to ensure such
536// statement instances are never executed.
537void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000538 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000539 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000540 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000541 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
542 isl_pw_aff *Var =
543 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
544 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
545
546 isl_set *DimOutside;
547
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000548 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
549 isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000550
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000551 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
552 Statement->getNumIterators());
553 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
554 isl_space_dim(Space, isl_dim_set));
555 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
556 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000557
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000558 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000559
560 Outside = isl_set_union(Outside, DimOutside);
561 }
562
563 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
564 Outside = isl_set_intersect(Outside, Statement->getDomain());
565 Outside = isl_set_params(Outside);
Tobias Grosserf54bb772015-06-26 12:09:28 +0000566
567 // Remove divs to avoid the construction of overly complicated assumptions.
568 // Doing so increases the set of parameter combinations that are assumed to
569 // not appear. This is always save, but may make the resulting run-time check
570 // bail out more often than strictly necessary.
571 Outside = isl_set_remove_divs(Outside);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000572 Outside = isl_set_complement(Outside);
573 Statement->getParent()->addAssumption(Outside);
574 isl_space_free(Space);
575}
576
Johannes Doerferte7044942015-02-24 11:58:30 +0000577void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) {
578 ScalarEvolution *SE = Statement->getParent()->getSE();
579
580 Value *Ptr = getPointerOperand(*getAccessInstruction());
581 if (!Ptr || !SE->isSCEVable(Ptr->getType()))
582 return;
583
584 auto *PtrSCEV = SE->getSCEV(Ptr);
585 if (isa<SCEVCouldNotCompute>(PtrSCEV))
586 return;
587
588 auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV);
589 if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV))
590 PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV);
591
592 const ConstantRange &Range = SE->getSignedRange(PtrSCEV);
593 if (Range.isFullSet())
594 return;
595
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000596 bool isWrapping = Range.isSignWrappedSet();
Johannes Doerferte7044942015-02-24 11:58:30 +0000597 unsigned BW = Range.getBitWidth();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000598 const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin();
599 const auto UB = isWrapping ? Range.getUpper() : Range.getSignedMax();
600
601 auto Min = LB.sdiv(APInt(BW, ElementSize));
602 auto Max = (UB - APInt(BW, 1)).sdiv(APInt(BW, ElementSize));
Johannes Doerferte7044942015-02-24 11:58:30 +0000603
604 isl_set *AccessRange = isl_map_range(isl_map_copy(AccessRelation));
605 AccessRange =
606 addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, isl_dim_set);
607 AccessRelation = isl_map_intersect_range(AccessRelation, AccessRange);
608}
609
Tobias Grosser619190d2015-03-30 17:22:28 +0000610__isl_give isl_map *MemoryAccess::foldAccess(const IRAccess &Access,
611 __isl_take isl_map *AccessRelation,
612 ScopStmt *Statement) {
613 int Size = Access.Subscripts.size();
614
615 for (int i = Size - 2; i >= 0; --i) {
616 isl_space *Space;
617 isl_map *MapOne, *MapTwo;
618 isl_pw_aff *DimSize = SCEVAffinator::getPwAff(Statement, Access.Sizes[i]);
619
620 isl_space *SpaceSize = isl_pw_aff_get_space(DimSize);
621 isl_pw_aff_free(DimSize);
622 isl_id *ParamId = isl_space_get_dim_id(SpaceSize, isl_dim_param, 0);
623
624 Space = isl_map_get_space(AccessRelation);
625 Space = isl_space_map_from_set(isl_space_range(Space));
626 Space = isl_space_align_params(Space, SpaceSize);
627
628 int ParamLocation = isl_space_find_dim_by_id(Space, isl_dim_param, ParamId);
629 isl_id_free(ParamId);
630
631 MapOne = isl_map_universe(isl_space_copy(Space));
632 for (int j = 0; j < Size; ++j)
633 MapOne = isl_map_equate(MapOne, isl_dim_in, j, isl_dim_out, j);
634 MapOne = isl_map_lower_bound_si(MapOne, isl_dim_in, i + 1, 0);
635
636 MapTwo = isl_map_universe(isl_space_copy(Space));
637 for (int j = 0; j < Size; ++j)
638 if (j < i || j > i + 1)
639 MapTwo = isl_map_equate(MapTwo, isl_dim_in, j, isl_dim_out, j);
640
641 isl_local_space *LS = isl_local_space_from_space(Space);
642 isl_constraint *C;
643 C = isl_equality_alloc(isl_local_space_copy(LS));
644 C = isl_constraint_set_constant_si(C, -1);
645 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, 1);
646 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, -1);
647 MapTwo = isl_map_add_constraint(MapTwo, C);
648 C = isl_equality_alloc(LS);
649 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i + 1, 1);
650 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i + 1, -1);
651 C = isl_constraint_set_coefficient_si(C, isl_dim_param, ParamLocation, 1);
652 MapTwo = isl_map_add_constraint(MapTwo, C);
653 MapTwo = isl_map_upper_bound_si(MapTwo, isl_dim_in, i + 1, -1);
654
655 MapOne = isl_map_union(MapOne, MapTwo);
656 AccessRelation = isl_map_apply_range(AccessRelation, MapOne);
657 }
658 return AccessRelation;
659}
660
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000661MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000662 ScopStmt *Statement, const ScopArrayInfo *SAI,
663 int Identifier)
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000664 : AccType(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst),
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000665 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000666
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000667 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000668 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000669 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000670
671 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000672
Tobias Grossere29d31c2015-05-15 12:24:09 +0000673 auto IdName = "__polly_array_ref_ " + std::to_string(Identifier);
674 Id = isl_id_alloc(Ctx, IdName.c_str(), nullptr);
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000675
Tobias Grossera1879642011-12-20 10:43:14 +0000676 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000677 // We overapproximate non-affine accesses with a possible access to the
678 // whole array. For read accesses it does not make a difference, if an
679 // access must or may happen. However, for write accesses it is important to
680 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000681 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000682 AccessRelation =
683 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Johannes Doerferte7044942015-02-24 11:58:30 +0000684
685 computeBoundsOnAccessRelation(Access.getElemSizeInBytes());
Tobias Grossera1879642011-12-20 10:43:14 +0000686 return;
687 }
688
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000689 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000690 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000691
Tobias Grosser79baa212014-04-10 08:38:02 +0000692 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000693 isl_pw_aff *Affine =
694 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000695
Sebastian Pop422e33f2014-06-03 18:16:31 +0000696 if (Size == 1) {
697 // For the non delinearized arrays, divide the access function of the last
698 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000699 //
700 // A stride one array access in C expressed as A[i] is expressed in
701 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
702 // two subsequent values of 'i' index two values that are stored next to
703 // each other in memory. By this division we make this characteristic
704 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000705 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000706 Affine = isl_pw_aff_scale_down_val(Affine, v);
707 }
708
709 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
710
Tobias Grosser79baa212014-04-10 08:38:02 +0000711 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000712 }
713
Tobias Grosser619190d2015-03-30 17:22:28 +0000714 AccessRelation = foldAccess(Access, AccessRelation, Statement);
715
Tobias Grosser79baa212014-04-10 08:38:02 +0000716 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000717 AccessRelation = isl_map_set_tuple_id(
718 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000719 AccessRelation =
720 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
721
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000722 assumeNoOutOfBound(Access);
Tobias Grosseraa660a92015-03-30 00:07:50 +0000723 AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain());
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000724 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000725}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000726
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000727void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000728 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000729 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000730}
731
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000732const std::string MemoryAccess::getReductionOperatorStr() const {
733 return MemoryAccess::getReductionOperatorStr(getReductionType());
734}
735
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000736__isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); }
737
Johannes Doerfertf6183392014-07-01 20:52:51 +0000738raw_ostream &polly::operator<<(raw_ostream &OS,
739 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000740 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000741 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000742 else
743 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000744 return OS;
745}
746
Tobias Grosser75805372011-04-29 06:27:02 +0000747void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000748 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000749 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000750 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000751 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000752 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000753 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000754 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000755 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000756 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000757 break;
758 }
Johannes Doerfert0ff23ec2015-02-06 20:13:15 +0000759 OS << "[Reduction Type: " << getReductionType() << "] ";
760 OS << "[Scalar: " << isScalar() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000761 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000762}
763
Tobias Grosser74394f02013-01-14 22:40:23 +0000764void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000765
766// Create a map in the size of the provided set domain, that maps from the
767// one element of the provided set domain to another element of the provided
768// set domain.
769// The mapping is limited to all points that are equal in all but the last
770// dimension and for which the last dimension of the input is strict smaller
771// than the last dimension of the output.
772//
773// getEqualAndLarger(set[i0, i1, ..., iX]):
774//
775// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
776// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
777//
Tobias Grosserf5338802011-10-06 00:03:35 +0000778static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000779 isl_space *Space = isl_space_map_from_set(setDomain);
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000780 isl_map *Map = isl_map_universe(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000781 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000782
783 // Set all but the last dimension to be equal for the input and output
784 //
785 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
786 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000787 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000788 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000789
790 // Set the last dimension of the input to be strict smaller than the
791 // last dimension of the output.
792 //
793 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000794 Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out,
795 lastDimension);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000796 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000797}
798
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000799__isl_give isl_set *
800MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000801 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000802 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000803 isl_space *Space = isl_space_range(isl_map_get_space(S));
804 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000805
Sebastian Popa00a0292012-12-18 07:46:06 +0000806 S = isl_map_reverse(S);
807 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000808
Sebastian Popa00a0292012-12-18 07:46:06 +0000809 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
810 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
811 NextScatt = isl_map_apply_domain(NextScatt, S);
812 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000813
Sebastian Popa00a0292012-12-18 07:46:06 +0000814 isl_set *Deltas = isl_map_deltas(NextScatt);
815 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000816}
817
Sebastian Popa00a0292012-12-18 07:46:06 +0000818bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000819 int StrideWidth) const {
820 isl_set *Stride, *StrideX;
821 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000822
Sebastian Popa00a0292012-12-18 07:46:06 +0000823 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000824 StrideX = isl_set_universe(isl_set_get_space(Stride));
825 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
826 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000827
Tobias Grosser28dd4862012-01-24 16:42:16 +0000828 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000829 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000830
Tobias Grosser28dd4862012-01-24 16:42:16 +0000831 return IsStrideX;
832}
833
Sebastian Popa00a0292012-12-18 07:46:06 +0000834bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
835 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000836}
837
Tobias Grosser79baa212014-04-10 08:38:02 +0000838bool MemoryAccess::isScalar() const {
839 return isl_map_n_out(AccessRelation) == 0;
840}
841
Sebastian Popa00a0292012-12-18 07:46:06 +0000842bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
843 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000844}
845
Tobias Grosser5d453812011-10-06 00:04:11 +0000846void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000847 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000848 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000849}
Tobias Grosser75805372011-04-29 06:27:02 +0000850
851//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000852
Tobias Grosser808cd692015-07-14 09:33:13 +0000853isl_map *ScopStmt::getSchedule() const {
854 isl_set *Domain = getDomain();
855 if (isl_set_is_empty(Domain)) {
856 isl_set_free(Domain);
857 return isl_map_from_aff(
858 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
859 }
860 auto *Schedule = getParent()->getSchedule();
861 Schedule = isl_union_map_intersect_domain(
862 Schedule, isl_union_set_from_set(isl_set_copy(Domain)));
863 if (isl_union_map_is_empty(Schedule)) {
864 isl_set_free(Domain);
865 isl_union_map_free(Schedule);
866 return isl_map_from_aff(
867 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
868 }
869 auto *M = isl_map_from_union_map(Schedule);
870 M = isl_map_coalesce(M);
871 M = isl_map_gist_domain(M, Domain);
872 M = isl_map_coalesce(M);
873 return M;
874}
Tobias Grossercf3942d2011-10-06 00:04:05 +0000875
Tobias Grosser37eb4222014-02-20 21:43:54 +0000876void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
877 assert(isl_set_is_subset(NewDomain, Domain) &&
878 "New domain is not a subset of old domain!");
879 isl_set_free(Domain);
880 Domain = NewDomain;
Tobias Grosser75805372011-04-29 06:27:02 +0000881}
882
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000883void ScopStmt::buildAccesses(TempScop &tempScop, BasicBlock *Block,
884 bool isApproximated) {
885 AccFuncSetType *AFS = tempScop.getAccessFunctions(Block);
886 if (!AFS)
887 return;
888
889 for (auto &AccessPair : *AFS) {
890 IRAccess &Access = AccessPair.first;
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000891 Instruction *AccessInst = AccessPair.second;
892
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000893 Type *ElementType = getAccessInstType(AccessInst);
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000894 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
Tobias Grosser92245222015-07-28 14:53:44 +0000895 Access.getBase(), ElementType, Access.Sizes, Access.isPHI());
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000896
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000897 if (isApproximated && Access.isWrite())
898 Access.setMayWrite();
899
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000900 MemoryAccessList *&MAL = InstructionToAccess[AccessInst];
901 if (!MAL)
902 MAL = new MemoryAccessList();
903 MAL->emplace_front(Access, AccessInst, this, SAI, MemAccs.size());
904 MemAccs.push_back(&MAL->front());
Tobias Grosser75805372011-04-29 06:27:02 +0000905 }
906}
907
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000908void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000909 for (MemoryAccess *MA : *this)
910 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000911
912 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000913}
914
Tobias Grosser65b00582011-11-08 15:41:19 +0000915__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000916 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
917 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000918
Tobias Grosserd2795d02011-08-18 07:51:40 +0000919 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000920 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000921 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000922 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000923 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000924 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000925 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000926 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000927 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000928 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000929 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000930 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000931 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000932 case ICmpInst::ICMP_ULT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000933 return isl_pw_aff_lt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000934 case ICmpInst::ICMP_UGT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000935 return isl_pw_aff_gt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000936 case ICmpInst::ICMP_ULE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000937 return isl_pw_aff_le_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000938 case ICmpInst::ICMP_UGE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000939 return isl_pw_aff_ge_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000940 default:
941 llvm_unreachable("Non integer predicate not supported");
942 }
Tobias Grosser75805372011-04-29 06:27:02 +0000943}
944
Tobias Grossere19661e2011-10-07 08:46:57 +0000945__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000946 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000947 isl_space *Space;
948 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000949
Tobias Grossere19661e2011-10-07 08:46:57 +0000950 Space = isl_set_get_space(Domain);
951 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000952
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000953 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000954 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000955 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000956 isl_pw_aff *IV =
957 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000958
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000959 // 0 <= IV.
960 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
961 Domain = isl_set_intersect(Domain, LowerBound);
962
963 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000964 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000965 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000966 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
967 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000968 Domain = isl_set_intersect(Domain, UpperBoundSet);
969 }
970
Tobias Grosserf5338802011-10-06 00:03:35 +0000971 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000972 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000973}
974
Tobias Grossere602a072013-05-07 07:30:56 +0000975__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
976 TempScop &tempScop,
977 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000978 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000979 *CurrentRegion = &CurRegion;
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000980 const BasicBlock *BranchingBB = BB ? BB : R->getEntry();
Tobias Grosser75805372011-04-29 06:27:02 +0000981
Tobias Grosser75805372011-04-29 06:27:02 +0000982 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000983 if (BranchingBB != CurrentRegion->getEntry()) {
984 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000985 for (const auto &C : *Condition) {
986 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000987 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000988 }
989 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000990 BranchingBB = CurrentRegion->getEntry();
991 CurrentRegion = CurrentRegion->getParent();
992 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000993
Tobias Grossere19661e2011-10-07 08:46:57 +0000994 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000995}
996
Tobias Grossere602a072013-05-07 07:30:56 +0000997__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
998 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000999 isl_space *Space;
1000 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +00001001 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +00001002
1003 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
1004
Tobias Grosser084d8f72012-05-29 09:29:44 +00001005 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
1006
Tobias Grossere19661e2011-10-07 08:46:57 +00001007 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +00001008 Domain = addLoopBoundsToDomain(Domain, tempScop);
1009 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +00001010 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +00001011
1012 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +00001013}
1014
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001015void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
1016 int Dimension = 0;
1017 isl_ctx *Ctx = Parent.getIslCtx();
1018 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
1019 Type *Ty = GEP->getPointerOperandType();
1020 ScalarEvolution &SE = *Parent.getSE();
1021
1022 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
1023 Dimension = 1;
1024 Ty = PtrTy->getElementType();
1025 }
1026
1027 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
1028 unsigned int Operand = 1 + Dimension;
1029
1030 if (GEP->getNumOperands() <= Operand)
1031 break;
1032
1033 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
1034
1035 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
1036 isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr);
1037 AccessOffset =
1038 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
1039
1040 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
1041 isl_local_space_copy(LSpace),
1042 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
1043
1044 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
1045 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
1046 OutOfBound = isl_set_params(OutOfBound);
1047 isl_set *InBound = isl_set_complement(OutOfBound);
1048 isl_set *Executed = isl_set_params(getDomain());
1049
1050 // A => B == !A or B
1051 isl_set *InBoundIfExecuted =
1052 isl_set_union(isl_set_complement(Executed), InBound);
1053
1054 Parent.addAssumption(InBoundIfExecuted);
1055 }
1056
1057 Dimension += 1;
1058 Ty = ArrayTy->getElementType();
1059 }
1060
1061 isl_local_space_free(LSpace);
1062}
1063
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001064void ScopStmt::deriveAssumptions(BasicBlock *Block) {
1065 for (Instruction &Inst : *Block)
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001066 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
1067 deriveAssumptionsFromGEP(GEP);
1068}
1069
Tobias Grosser74394f02013-01-14 22:40:23 +00001070ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001071 Region &R, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001072 : Parent(parent), BB(nullptr), R(&R), Build(nullptr),
1073 NestLoops(Nest.size()) {
1074 // Setup the induction variables.
1075 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
1076 NestLoops[i] = Nest[i];
1077
Tobias Grosser16c44032015-07-09 07:31:45 +00001078 BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), "");
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001079
1080 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001081
1082 BasicBlock *EntryBB = R.getEntry();
1083 for (BasicBlock *Block : R.blocks()) {
1084 buildAccesses(tempScop, Block, Block != EntryBB);
1085 deriveAssumptions(Block);
1086 }
1087 checkForReductions();
1088}
1089
1090ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001091 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001092 : Parent(parent), BB(&bb), R(nullptr), Build(nullptr),
1093 NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +00001094 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +00001095 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +00001096 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +00001097
Johannes Doerfert79fc23f2014-07-24 23:48:02 +00001098 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +00001099
Tobias Grossere19661e2011-10-07 08:46:57 +00001100 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001101 buildAccesses(tempScop, BB);
1102 deriveAssumptions(BB);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001103 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001104}
1105
Johannes Doerferte58a0122014-06-27 20:31:28 +00001106/// @brief Collect loads which might form a reduction chain with @p StoreMA
1107///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001108/// Check if the stored value for @p StoreMA is a binary operator with one or
1109/// two loads as operands. If the binary operand is commutative & associative,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001110/// used only once (by @p StoreMA) and its load operands are also used only
1111/// once, we have found a possible reduction chain. It starts at an operand
1112/// load and includes the binary operator and @p StoreMA.
1113///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001114/// Note: We allow only one use to ensure the load and binary operator cannot
Johannes Doerferte58a0122014-06-27 20:31:28 +00001115/// escape this block or into any other store except @p StoreMA.
1116void ScopStmt::collectCandiateReductionLoads(
1117 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
1118 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
1119 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001120 return;
1121
1122 // Skip if there is not one binary operator between the load and the store
1123 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +00001124 if (!BinOp)
1125 return;
1126
1127 // Skip if the binary operators has multiple uses
1128 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001129 return;
1130
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001131 // Skip if the opcode of the binary operator is not commutative/associative
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001132 if (!BinOp->isCommutative() || !BinOp->isAssociative())
1133 return;
1134
Johannes Doerfert9890a052014-07-01 00:32:29 +00001135 // Skip if the binary operator is outside the current SCoP
1136 if (BinOp->getParent() != Store->getParent())
1137 return;
1138
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001139 // Skip if it is a multiplicative reduction and we disabled them
1140 if (DisableMultiplicativeReductions &&
1141 (BinOp->getOpcode() == Instruction::Mul ||
1142 BinOp->getOpcode() == Instruction::FMul))
1143 return;
1144
Johannes Doerferte58a0122014-06-27 20:31:28 +00001145 // Check the binary operator operands for a candidate load
1146 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
1147 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
1148 if (!PossibleLoad0 && !PossibleLoad1)
1149 return;
1150
1151 // A load is only a candidate if it cannot escape (thus has only this use)
1152 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001153 if (PossibleLoad0->getParent() == Store->getParent())
1154 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001155 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001156 if (PossibleLoad1->getParent() == Store->getParent())
1157 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001158}
1159
1160/// @brief Check for reductions in this ScopStmt
1161///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001162/// Iterate over all store memory accesses and check for valid binary reduction
1163/// like chains. For all candidates we check if they have the same base address
1164/// and there are no other accesses which overlap with them. The base address
1165/// check rules out impossible reductions candidates early. The overlap check,
1166/// together with the "only one user" check in collectCandiateReductionLoads,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001167/// guarantees that none of the intermediate results will escape during
1168/// execution of the loop nest. We basically check here that no other memory
1169/// access can access the same memory as the potential reduction.
1170void ScopStmt::checkForReductions() {
1171 SmallVector<MemoryAccess *, 2> Loads;
1172 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1173
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001174 // First collect candidate load-store reduction chains by iterating over all
Johannes Doerferte58a0122014-06-27 20:31:28 +00001175 // stores and collecting possible reduction loads.
1176 for (MemoryAccess *StoreMA : MemAccs) {
1177 if (StoreMA->isRead())
1178 continue;
1179
1180 Loads.clear();
1181 collectCandiateReductionLoads(StoreMA, Loads);
1182 for (MemoryAccess *LoadMA : Loads)
1183 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1184 }
1185
1186 // Then check each possible candidate pair.
1187 for (const auto &CandidatePair : Candidates) {
1188 bool Valid = true;
1189 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1190 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1191
1192 // Skip those with obviously unequal base addresses.
1193 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1194 isl_map_free(LoadAccs);
1195 isl_map_free(StoreAccs);
1196 continue;
1197 }
1198
1199 // And check if the remaining for overlap with other memory accesses.
1200 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1201 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1202 isl_set *AllAccs = isl_map_range(AllAccsRel);
1203
1204 for (MemoryAccess *MA : MemAccs) {
1205 if (MA == CandidatePair.first || MA == CandidatePair.second)
1206 continue;
1207
1208 isl_map *AccRel =
1209 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1210 isl_set *Accs = isl_map_range(AccRel);
1211
1212 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1213 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1214 Valid = Valid && isl_set_is_empty(OverlapAccs);
1215 isl_set_free(OverlapAccs);
1216 }
1217 }
1218
1219 isl_set_free(AllAccs);
1220 if (!Valid)
1221 continue;
1222
Johannes Doerfertf6183392014-07-01 20:52:51 +00001223 const LoadInst *Load =
1224 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1225 MemoryAccess::ReductionType RT =
1226 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1227
Johannes Doerferte58a0122014-06-27 20:31:28 +00001228 // If no overlapping access was found we mark the load and store as
1229 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001230 CandidatePair.first->markAsReductionLike(RT);
1231 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001232 }
Tobias Grosser75805372011-04-29 06:27:02 +00001233}
1234
Tobias Grosser74394f02013-01-14 22:40:23 +00001235std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001236
Tobias Grosser54839312015-04-21 11:37:25 +00001237std::string ScopStmt::getScheduleStr() const {
Tobias Grosser808cd692015-07-14 09:33:13 +00001238 auto *S = getSchedule();
1239 auto Str = stringFromIslObj(S);
1240 isl_map_free(S);
1241 return Str;
Tobias Grosser75805372011-04-29 06:27:02 +00001242}
1243
Tobias Grosser74394f02013-01-14 22:40:23 +00001244unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001245
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001246unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001247
Tobias Grosser75805372011-04-29 06:27:02 +00001248const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1249
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001250const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001251 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001252}
1253
Tobias Grosser74394f02013-01-14 22:40:23 +00001254isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001255
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001256__isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001257
Tobias Grosser6e6c7e02015-03-30 12:22:39 +00001258__isl_give isl_space *ScopStmt::getDomainSpace() const {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001259 return isl_set_get_space(Domain);
1260}
1261
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001262__isl_give isl_id *ScopStmt::getDomainId() const {
1263 return isl_set_get_tuple_id(Domain);
1264}
Tobias Grossercd95b772012-08-30 11:49:38 +00001265
Tobias Grosser75805372011-04-29 06:27:02 +00001266ScopStmt::~ScopStmt() {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001267 DeleteContainerSeconds(InstructionToAccess);
Tobias Grosser75805372011-04-29 06:27:02 +00001268 isl_set_free(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +00001269}
1270
1271void ScopStmt::print(raw_ostream &OS) const {
1272 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001273 OS.indent(12) << "Domain :=\n";
1274
1275 if (Domain) {
1276 OS.indent(16) << getDomainStr() << ";\n";
1277 } else
1278 OS.indent(16) << "n/a\n";
1279
Tobias Grosser54839312015-04-21 11:37:25 +00001280 OS.indent(12) << "Schedule :=\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001281
1282 if (Domain) {
Tobias Grosser54839312015-04-21 11:37:25 +00001283 OS.indent(16) << getScheduleStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001284 } else
1285 OS.indent(16) << "n/a\n";
1286
Tobias Grosser083d3d32014-06-28 08:59:45 +00001287 for (MemoryAccess *Access : MemAccs)
1288 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001289}
1290
1291void ScopStmt::dump() const { print(dbgs()); }
1292
1293//===----------------------------------------------------------------------===//
1294/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001295
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001296void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001297 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1298 isl_set_free(Context);
1299 Context = NewContext;
1300}
1301
Tobias Grosserabfbe632013-02-05 12:09:06 +00001302void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001303 for (const SCEV *Parameter : NewParameters) {
Johannes Doerfertbe409962015-03-29 20:45:09 +00001304 Parameter = extractConstantFactor(Parameter, *SE).second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001305 if (ParameterIds.find(Parameter) != ParameterIds.end())
1306 continue;
1307
1308 int dimension = Parameters.size();
1309
1310 Parameters.push_back(Parameter);
1311 ParameterIds[Parameter] = dimension;
1312 }
1313}
1314
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001315__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1316 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001317
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001318 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001319 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001320
Tobias Grosser8f99c162011-11-15 11:38:55 +00001321 std::string ParameterName;
1322
1323 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1324 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001325 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001326 }
1327
1328 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001329 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001330
Tobias Grosser20532b82014-04-11 17:56:49 +00001331 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1332 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001333}
Tobias Grosser75805372011-04-29 06:27:02 +00001334
Tobias Grosser6be480c2011-11-08 15:41:13 +00001335void Scop::buildContext() {
1336 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001337 Context = isl_set_universe(isl_space_copy(Space));
1338 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001339}
1340
Tobias Grosser18daaca2012-05-22 10:47:27 +00001341void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001342 for (const auto &ParamID : ParameterIds) {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001343 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001344
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001345 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001346
Johannes Doerferte7044942015-02-24 11:58:30 +00001347 Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001348 }
1349}
1350
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001351void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001352 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001353 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001354
Tobias Grosser083d3d32014-06-28 08:59:45 +00001355 for (const auto &ParamID : ParameterIds) {
1356 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001357 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001358 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001359 }
1360
1361 // Align the parameters of all data structures to the model.
1362 Context = isl_set_align_params(Context, Space);
1363
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001364 for (ScopStmt &Stmt : *this)
1365 Stmt.realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001366}
1367
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001368void Scop::simplifyAssumedContext() {
1369 // The parameter constraints of the iteration domains give us a set of
1370 // constraints that need to hold for all cases where at least a single
1371 // statement iteration is executed in the whole scop. We now simplify the
1372 // assumed context under the assumption that such constraints hold and at
1373 // least a single statement iteration is executed. For cases where no
1374 // statement instances are executed, the assumptions we have taken about
1375 // the executed code do not matter and can be changed.
1376 //
1377 // WARNING: This only holds if the assumptions we have taken do not reduce
1378 // the set of statement instances that are executed. Otherwise we
1379 // may run into a case where the iteration domains suggest that
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001380 // for a certain set of parameter constraints no code is executed,
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001381 // but in the original program some computation would have been
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001382 // performed. In such a case, modifying the run-time conditions and
1383 // possibly influencing the run-time check may cause certain scops
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001384 // to not be executed.
1385 //
1386 // Example:
1387 //
1388 // When delinearizing the following code:
1389 //
1390 // for (long i = 0; i < 100; i++)
1391 // for (long j = 0; j < m; j++)
1392 // A[i+p][j] = 1.0;
1393 //
1394 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001395 // otherwise we would access out of bound data. Now, knowing that code is
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001396 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1397 AssumedContext =
1398 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001399 AssumedContext = isl_set_gist_params(AssumedContext, getContext());
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001400}
1401
Johannes Doerfertb164c792014-09-18 11:17:17 +00001402/// @brief Add the minimal/maximal access in @p Set to @p User.
Tobias Grosserb2f39922015-05-28 13:32:11 +00001403static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001404 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1405 isl_pw_multi_aff *MinPMA, *MaxPMA;
1406 isl_pw_aff *LastDimAff;
1407 isl_aff *OneAff;
1408 unsigned Pos;
1409
Johannes Doerfert9143d672014-09-27 11:02:39 +00001410 // Restrict the number of parameters involved in the access as the lexmin/
1411 // lexmax computation will take too long if this number is high.
1412 //
1413 // Experiments with a simple test case using an i7 4800MQ:
1414 //
1415 // #Parameters involved | Time (in sec)
1416 // 6 | 0.01
1417 // 7 | 0.04
1418 // 8 | 0.12
1419 // 9 | 0.40
1420 // 10 | 1.54
1421 // 11 | 6.78
1422 // 12 | 30.38
1423 //
1424 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1425 unsigned InvolvedParams = 0;
1426 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1427 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1428 InvolvedParams++;
1429
1430 if (InvolvedParams > RunTimeChecksMaxParameters) {
1431 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001432 return isl_stat_error;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001433 }
1434 }
1435
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001436 Set = isl_set_remove_divs(Set);
1437
Johannes Doerfertb164c792014-09-18 11:17:17 +00001438 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1439 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1440
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001441 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1442 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1443
Johannes Doerfertb164c792014-09-18 11:17:17 +00001444 // Adjust the last dimension of the maximal access by one as we want to
1445 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1446 // we test during code generation might now point after the end of the
1447 // allocated array but we will never dereference it anyway.
1448 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1449 "Assumed at least one output dimension");
1450 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1451 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1452 OneAff = isl_aff_zero_on_domain(
1453 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1454 OneAff = isl_aff_add_constant_si(OneAff, 1);
1455 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1456 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1457
1458 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1459
1460 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001461 return isl_stat_ok;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001462}
1463
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001464static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1465 isl_set *Domain = MA->getStatement()->getDomain();
1466 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1467 return isl_set_reset_tuple_id(Domain);
1468}
1469
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001470/// @brief Wrapper function to calculate minimal/maximal accesses to each array.
1471static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses,
Tobias Grosserbb853c22015-07-25 12:31:03 +00001472 __isl_take isl_union_set *Domains,
1473 __isl_take isl_set *AssumedContext,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001474 Scop::MinMaxVectorTy &MinMaxAccesses) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001475
1476 Accesses = isl_union_map_intersect_domain(Accesses, Domains);
1477 isl_union_set *Locations = isl_union_map_range(Accesses);
1478 Locations = isl_union_set_intersect_params(Locations, AssumedContext);
1479 Locations = isl_union_set_coalesce(Locations);
1480 Locations = isl_union_set_detect_equalities(Locations);
1481 bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001482 &MinMaxAccesses));
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001483 isl_union_set_free(Locations);
1484 return Valid;
1485}
1486
Johannes Doerfert9143d672014-09-27 11:02:39 +00001487bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001488 // To create sound alias checks we perform the following steps:
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001489 // o) Use the alias analysis and an alias set tracker to build alias sets
Johannes Doerfertb164c792014-09-18 11:17:17 +00001490 // for all memory accesses inside the SCoP.
1491 // o) For each alias set we then map the aliasing pointers back to the
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001492 // memory accesses we know, thus obtain groups of memory accesses which
Johannes Doerfertb164c792014-09-18 11:17:17 +00001493 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001494 // o) We divide each group based on the domains of the minimal/maximal
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001495 // accesses. That means two minimal/maximal accesses are only in a group
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001496 // if their access domains intersect, otherwise they are in different
1497 // ones.
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001498 // o) We partition each group into read only and non read only accesses.
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001499 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001500 // and maximal accesses to each array of a group in read only and non
1501 // read only partitions separately.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001502 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1503
1504 AliasSetTracker AST(AA);
1505
1506 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001507 DenseSet<Value *> HasWriteAccess;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001508 for (ScopStmt &Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001509
1510 // Skip statements with an empty domain as they will never be executed.
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001511 isl_set *StmtDomain = Stmt.getDomain();
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001512 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1513 isl_set_free(StmtDomain);
1514 if (StmtDomainEmpty)
1515 continue;
1516
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001517 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001518 if (MA->isScalar())
1519 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001520 if (!MA->isRead())
1521 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001522 Instruction *Acc = MA->getAccessInstruction();
1523 PtrToAcc[getPointerOperand(*Acc)] = MA;
1524 AST.add(Acc);
1525 }
1526 }
1527
1528 SmallVector<AliasGroupTy, 4> AliasGroups;
1529 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001530 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001531 continue;
1532 AliasGroupTy AG;
1533 for (auto PR : AS)
1534 AG.push_back(PtrToAcc[PR.getValue()]);
1535 assert(AG.size() > 1 &&
1536 "Alias groups should contain at least two accesses");
1537 AliasGroups.push_back(std::move(AG));
1538 }
1539
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001540 // Split the alias groups based on their domain.
1541 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1542 AliasGroupTy NewAG;
1543 AliasGroupTy &AG = AliasGroups[u];
1544 AliasGroupTy::iterator AGI = AG.begin();
1545 isl_set *AGDomain = getAccessDomain(*AGI);
1546 while (AGI != AG.end()) {
1547 MemoryAccess *MA = *AGI;
1548 isl_set *MADomain = getAccessDomain(MA);
1549 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1550 NewAG.push_back(MA);
1551 AGI = AG.erase(AGI);
1552 isl_set_free(MADomain);
1553 } else {
1554 AGDomain = isl_set_union(AGDomain, MADomain);
1555 AGI++;
1556 }
1557 }
1558 if (NewAG.size() > 1)
1559 AliasGroups.push_back(std::move(NewAG));
1560 isl_set_free(AGDomain);
1561 }
1562
Tobias Grosserf4c24b22015-04-05 13:11:54 +00001563 MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
Johannes Doerfert13771732014-10-01 12:40:46 +00001564 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1565 for (AliasGroupTy &AG : AliasGroups) {
1566 NonReadOnlyBaseValues.clear();
1567 ReadOnlyPairs.clear();
1568
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001569 if (AG.size() < 2) {
1570 AG.clear();
1571 continue;
1572 }
1573
Johannes Doerfert13771732014-10-01 12:40:46 +00001574 for (auto II = AG.begin(); II != AG.end();) {
1575 Value *BaseAddr = (*II)->getBaseAddr();
1576 if (HasWriteAccess.count(BaseAddr)) {
1577 NonReadOnlyBaseValues.insert(BaseAddr);
1578 II++;
1579 } else {
1580 ReadOnlyPairs[BaseAddr].insert(*II);
1581 II = AG.erase(II);
1582 }
1583 }
1584
1585 // If we don't have read only pointers check if there are at least two
1586 // non read only pointers, otherwise clear the alias group.
Tobias Grosserbb853c22015-07-25 12:31:03 +00001587 if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001588 AG.clear();
Johannes Doerfert13771732014-10-01 12:40:46 +00001589 continue;
1590 }
1591
1592 // If we don't have non read only pointers clear the alias group.
1593 if (NonReadOnlyBaseValues.empty()) {
1594 AG.clear();
1595 continue;
1596 }
1597
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001598 // Calculate minimal and maximal accesses for non read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001599 MinMaxAliasGroups.emplace_back();
1600 MinMaxVectorPairTy &pair = MinMaxAliasGroups.back();
1601 MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first;
1602 MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second;
1603 MinMaxAccessesNonReadOnly.reserve(AG.size());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001604
1605 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001606
1607 // AG contains only non read only accesses.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001608 for (MemoryAccess *MA : AG)
1609 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001610
Tobias Grosserbb853c22015-07-25 12:31:03 +00001611 bool Valid = calculateMinMaxAccess(
1612 Accesses, getDomains(), getAssumedContext(), MinMaxAccessesNonReadOnly);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001613
1614 // Bail out if the number of values we need to compare is too large.
1615 // This is important as the number of comparisions grows quadratically with
1616 // the number of values we need to compare.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001617 if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() >
1618 RunTimeChecksMaxArraysPerGroup))
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001619 return false;
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001620
1621 // Calculate minimal and maximal accesses for read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001622 MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001623 Accesses = isl_union_map_empty(getParamSpace());
1624
1625 for (const auto &ReadOnlyPair : ReadOnlyPairs)
1626 for (MemoryAccess *MA : ReadOnlyPair.second)
1627 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1628
Tobias Grosserbb853c22015-07-25 12:31:03 +00001629 Valid = calculateMinMaxAccess(Accesses, getDomains(), getAssumedContext(),
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001630 MinMaxAccessesReadOnly);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001631
1632 if (!Valid)
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001633 return false;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001634 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001635
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001636 return true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001637}
1638
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001639static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI,
1640 ScopDetection &SD) {
1641
1642 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R);
1643
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001644 unsigned MinLD = INT_MAX, MaxLD = 0;
1645 for (BasicBlock *BB : R.blocks()) {
1646 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00001647 if (!R.contains(L))
1648 continue;
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001649 if (BoxedLoops && BoxedLoops->count(L))
1650 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001651 unsigned LD = L->getLoopDepth();
1652 MinLD = std::min(MinLD, LD);
1653 MaxLD = std::max(MaxLD, LD);
1654 }
1655 }
1656
1657 // Handle the case that there is no loop in the SCoP first.
1658 if (MaxLD == 0)
1659 return 1;
1660
1661 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1662 assert(MaxLD >= MinLD &&
1663 "Maximal loop depth was smaller than mininaml loop depth?");
1664 return MaxLD - MinLD + 1;
1665}
1666
Tobias Grosser0e27e242011-10-06 00:03:48 +00001667Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001668 ScopDetection &SD, isl_ctx *Context)
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001669 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()), IsOptimized(false),
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001670 MaxLoopDepth(getMaxLoopDepthInRegion(tempScop.getMaxRegion(), LI, SD)) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001671 IslCtx = Context;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001672
Tobias Grosser6be480c2011-11-08 15:41:13 +00001673 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001674
Tobias Grosserabfbe632013-02-05 12:09:06 +00001675 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001676
Tobias Grosser54839312015-04-21 11:37:25 +00001677 // Build the iteration domain, access functions and schedule functions
Tobias Grosser75805372011-04-29 06:27:02 +00001678 // traversing the region tree.
Tobias Grosser808cd692015-07-14 09:33:13 +00001679 Schedule = buildScop(tempScop, getRegion(), NestLoops, LI, SD);
1680 if (!Schedule)
1681 Schedule = isl_schedule_empty(getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +00001682
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001683 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001684 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001685 simplifyAssumedContext();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001686
Tobias Grosser75805372011-04-29 06:27:02 +00001687 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1688}
1689
1690Scop::~Scop() {
1691 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001692 isl_set_free(AssumedContext);
Tobias Grosser808cd692015-07-14 09:33:13 +00001693 isl_schedule_free(Schedule);
Tobias Grosser75805372011-04-29 06:27:02 +00001694
Johannes Doerfertb164c792014-09-18 11:17:17 +00001695 // Free the alias groups
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001696 for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001697 for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001698 isl_pw_multi_aff_free(MMA.first);
1699 isl_pw_multi_aff_free(MMA.second);
1700 }
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001701 for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001702 isl_pw_multi_aff_free(MMA.first);
1703 isl_pw_multi_aff_free(MMA.second);
1704 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001705 }
Tobias Grosser75805372011-04-29 06:27:02 +00001706}
1707
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001708const ScopArrayInfo *
1709Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
Tobias Grosser92245222015-07-28 14:53:44 +00001710 const SmallVector<const SCEV *, 4> &Sizes,
1711 bool IsPHI) {
1712 auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001713 if (!SAI)
Tobias Grosser92245222015-07-28 14:53:44 +00001714 SAI.reset(
1715 new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI));
Tobias Grosserab671442015-05-23 05:58:27 +00001716 return SAI.get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001717}
1718
Tobias Grosser92245222015-07-28 14:53:44 +00001719const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) {
1720 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001721 assert(SAI && "No ScopArrayInfo available for this base pointer");
1722 return SAI;
1723}
1724
Tobias Grosser74394f02013-01-14 22:40:23 +00001725std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001726std::string Scop::getAssumedContextStr() const {
1727 return stringFromIslObj(AssumedContext);
1728}
Tobias Grosser75805372011-04-29 06:27:02 +00001729
1730std::string Scop::getNameStr() const {
1731 std::string ExitName, EntryName;
1732 raw_string_ostream ExitStr(ExitName);
1733 raw_string_ostream EntryStr(EntryName);
1734
Tobias Grosserf240b482014-01-09 10:42:15 +00001735 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001736 EntryStr.str();
1737
1738 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001739 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001740 ExitStr.str();
1741 } else
1742 ExitName = "FunctionExit";
1743
1744 return EntryName + "---" + ExitName;
1745}
1746
Tobias Grosser74394f02013-01-14 22:40:23 +00001747__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001748__isl_give isl_space *Scop::getParamSpace() const {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001749 return isl_set_get_space(Context);
Tobias Grosser37487052011-10-06 00:03:42 +00001750}
1751
Tobias Grossere86109f2013-10-29 21:05:49 +00001752__isl_give isl_set *Scop::getAssumedContext() const {
1753 return isl_set_copy(AssumedContext);
1754}
1755
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001756void Scop::addAssumption(__isl_take isl_set *Set) {
1757 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001758 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001759}
1760
Tobias Grosser75805372011-04-29 06:27:02 +00001761void Scop::printContext(raw_ostream &OS) const {
1762 OS << "Context:\n";
1763
1764 if (!Context) {
1765 OS.indent(4) << "n/a\n\n";
1766 return;
1767 }
1768
1769 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001770
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001771 OS.indent(4) << "Assumed Context:\n";
1772 if (!AssumedContext) {
1773 OS.indent(4) << "n/a\n\n";
1774 return;
1775 }
1776
1777 OS.indent(4) << getAssumedContextStr() << "\n";
1778
Tobias Grosser083d3d32014-06-28 08:59:45 +00001779 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001780 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001781 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1782 }
Tobias Grosser75805372011-04-29 06:27:02 +00001783}
1784
Johannes Doerfertb164c792014-09-18 11:17:17 +00001785void Scop::printAliasAssumptions(raw_ostream &OS) const {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001786 int noOfGroups = 0;
1787 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001788 if (Pair.second.size() == 0)
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001789 noOfGroups += 1;
1790 else
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001791 noOfGroups += Pair.second.size();
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001792 }
1793
Tobias Grosserbb853c22015-07-25 12:31:03 +00001794 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n";
Johannes Doerfertb164c792014-09-18 11:17:17 +00001795 if (MinMaxAliasGroups.empty()) {
1796 OS.indent(8) << "n/a\n";
1797 return;
1798 }
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001799
Tobias Grosserbb853c22015-07-25 12:31:03 +00001800 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001801
1802 // If the group has no read only accesses print the write accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001803 if (Pair.second.empty()) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001804 OS.indent(8) << "[[";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001805 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001806 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1807 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001808 }
1809 OS << " ]]\n";
1810 }
1811
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001812 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001813 OS.indent(8) << "[[";
Tobias Grosserbb853c22015-07-25 12:31:03 +00001814 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001815 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001816 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1817 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001818 }
1819 OS << " ]]\n";
1820 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001821 }
1822}
1823
Tobias Grosser75805372011-04-29 06:27:02 +00001824void Scop::printStatements(raw_ostream &OS) const {
1825 OS << "Statements {\n";
1826
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001827 for (const ScopStmt &Stmt : *this)
1828 OS.indent(4) << Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001829
1830 OS.indent(4) << "}\n";
1831}
1832
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001833void Scop::printArrayInfo(raw_ostream &OS) const {
1834 OS << "Arrays {\n";
1835
Tobias Grosserab671442015-05-23 05:58:27 +00001836 for (auto &Array : arrays())
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001837 Array.second->print(OS);
1838
1839 OS.indent(4) << "}\n";
1840}
1841
Tobias Grosser75805372011-04-29 06:27:02 +00001842void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001843 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1844 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001845 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00001846 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001847 printContext(OS.indent(4));
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001848 printArrayInfo(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001849 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001850 printStatements(OS.indent(4));
1851}
1852
1853void Scop::dump() const { print(dbgs()); }
1854
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001855isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001856
Tobias Grosser808cd692015-07-14 09:33:13 +00001857__isl_give isl_union_set *Scop::getDomains() const {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001858 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001859
Tobias Grosser808cd692015-07-14 09:33:13 +00001860 for (const ScopStmt &Stmt : *this)
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001861 Domain = isl_union_set_add_set(Domain, Stmt.getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001862
1863 return Domain;
1864}
1865
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001866__isl_give isl_union_map *Scop::getMustWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001867 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001868
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001869 for (ScopStmt &Stmt : *this) {
1870 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001871 if (!MA->isMustWrite())
1872 continue;
1873
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001874 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001875 isl_map *AccessDomain = MA->getAccessRelation();
1876 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1877 Write = isl_union_map_add_map(Write, AccessDomain);
1878 }
1879 }
1880 return isl_union_map_coalesce(Write);
1881}
1882
1883__isl_give isl_union_map *Scop::getMayWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001884 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001885
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001886 for (ScopStmt &Stmt : *this) {
1887 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001888 if (!MA->isMayWrite())
1889 continue;
1890
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001891 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001892 isl_map *AccessDomain = MA->getAccessRelation();
1893 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1894 Write = isl_union_map_add_map(Write, AccessDomain);
1895 }
1896 }
1897 return isl_union_map_coalesce(Write);
1898}
1899
Tobias Grosser37eb4222014-02-20 21:43:54 +00001900__isl_give isl_union_map *Scop::getWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001901 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001902
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001903 for (ScopStmt &Stmt : *this) {
1904 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001905 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001906 continue;
1907
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001908 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001909 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001910 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1911 Write = isl_union_map_add_map(Write, AccessDomain);
1912 }
1913 }
1914 return isl_union_map_coalesce(Write);
1915}
1916
1917__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001918 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001919
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001920 for (ScopStmt &Stmt : *this) {
1921 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001922 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001923 continue;
1924
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001925 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001926 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001927
1928 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1929 Read = isl_union_map_add_map(Read, AccessDomain);
1930 }
1931 }
1932 return isl_union_map_coalesce(Read);
1933}
1934
Tobias Grosser808cd692015-07-14 09:33:13 +00001935__isl_give isl_union_map *Scop::getSchedule() const {
1936 auto Tree = getScheduleTree();
1937 auto S = isl_schedule_get_map(Tree);
1938 isl_schedule_free(Tree);
1939 return S;
1940}
Tobias Grosser37eb4222014-02-20 21:43:54 +00001941
Tobias Grosser808cd692015-07-14 09:33:13 +00001942__isl_give isl_schedule *Scop::getScheduleTree() const {
1943 return isl_schedule_intersect_domain(isl_schedule_copy(Schedule),
1944 getDomains());
1945}
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001946
Tobias Grosser808cd692015-07-14 09:33:13 +00001947void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) {
1948 auto *S = isl_schedule_from_domain(getDomains());
1949 S = isl_schedule_insert_partial_schedule(
1950 S, isl_multi_union_pw_aff_from_union_map(NewSchedule));
1951 isl_schedule_free(Schedule);
1952 Schedule = S;
1953}
1954
1955void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) {
1956 isl_schedule_free(Schedule);
1957 Schedule = NewSchedule;
Tobias Grosser37eb4222014-02-20 21:43:54 +00001958}
1959
1960bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1961 bool Changed = false;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001962 for (ScopStmt &Stmt : *this) {
1963 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001964 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1965 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1966
1967 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1968 isl_union_set_free(StmtDomain);
1969 isl_union_set_free(NewStmtDomain);
1970 continue;
1971 }
1972
1973 Changed = true;
1974
1975 isl_union_set_free(StmtDomain);
1976 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1977
1978 if (isl_union_set_is_empty(NewStmtDomain)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001979 Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser37eb4222014-02-20 21:43:54 +00001980 isl_union_set_free(NewStmtDomain);
1981 } else
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001982 Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain));
Tobias Grosser37eb4222014-02-20 21:43:54 +00001983 }
1984 isl_union_set_free(Domain);
1985 return Changed;
1986}
1987
Tobias Grosser75805372011-04-29 06:27:02 +00001988ScalarEvolution *Scop::getSE() const { return SE; }
1989
1990bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1991 if (tempScop.getAccessFunctions(BB))
1992 return false;
1993
1994 return true;
1995}
1996
Tobias Grosser808cd692015-07-14 09:33:13 +00001997struct MapToDimensionDataTy {
1998 int N;
1999 isl_union_pw_multi_aff *Res;
2000};
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002001
Tobias Grosser808cd692015-07-14 09:33:13 +00002002// @brief Create a function that maps the elements of 'Set' to its N-th
2003// dimension.
2004//
2005// The result is added to 'User->Res'.
2006//
2007// @param Set The input set.
2008// @param N The dimension to map to.
2009//
2010// @returns Zero if no error occurred, non-zero otherwise.
2011static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) {
2012 struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User;
2013 int Dim;
2014 isl_space *Space;
2015 isl_pw_multi_aff *PMA;
2016
2017 Dim = isl_set_dim(Set, isl_dim_set);
2018 Space = isl_set_get_space(Set);
2019 PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N,
2020 Dim - Data->N);
2021 if (Data->N > 1)
2022 PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1);
2023 Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA);
2024
2025 isl_set_free(Set);
2026
2027 return isl_stat_ok;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002028}
2029
Tobias Grosser808cd692015-07-14 09:33:13 +00002030// @brief Create a function that maps the elements of Domain to their Nth
2031// dimension.
2032//
2033// @param Domain The set of elements to map.
2034// @param N The dimension to map to.
2035static __isl_give isl_multi_union_pw_aff *
2036mapToDimension(__isl_take isl_union_set *Domain, int N) {
2037 struct MapToDimensionDataTy Data;
2038 isl_space *Space;
2039
2040 Space = isl_union_set_get_space(Domain);
2041 Data.N = N;
2042 Data.Res = isl_union_pw_multi_aff_empty(Space);
2043 if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0)
2044 Data.Res = isl_union_pw_multi_aff_free(Data.Res);
2045
2046 isl_union_set_free(Domain);
2047 return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res);
2048}
2049
2050ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R, TempScop &tempScop,
2051 const Region &CurRegion,
2052 SmallVectorImpl<Loop *> &NestLoops) {
2053 ScopStmt *Stmt;
2054 if (BB) {
2055 Stmts.emplace_back(*this, tempScop, CurRegion, *BB, NestLoops);
2056 Stmt = &Stmts.back();
2057 StmtMap[BB] = Stmt;
2058 } else {
2059 assert(R && "Either basic block or a region expected.");
2060 Stmts.emplace_back(*this, tempScop, CurRegion, *R, NestLoops);
2061 Stmt = &Stmts.back();
2062 for (BasicBlock *BB : R->blocks())
2063 StmtMap[BB] = Stmt;
2064 }
2065 return Stmt;
2066}
2067
2068__isl_give isl_schedule *Scop::buildScop(TempScop &tempScop,
2069 const Region &CurRegion,
2070 SmallVectorImpl<Loop *> &NestLoops,
2071 LoopInfo &LI, ScopDetection &SD) {
2072 if (SD.isNonAffineSubRegion(&CurRegion, &getRegion())) {
2073 auto *Stmt = addScopStmt(nullptr, const_cast<Region *>(&CurRegion),
2074 tempScop, CurRegion, NestLoops);
2075 auto *Domain = Stmt->getDomain();
2076 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2077 }
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002078
Tobias Grosser75805372011-04-29 06:27:02 +00002079 Loop *L = castToLoop(CurRegion, LI);
2080
2081 if (L)
2082 NestLoops.push_back(L);
2083
2084 unsigned loopDepth = NestLoops.size();
Tobias Grosser808cd692015-07-14 09:33:13 +00002085 isl_schedule *Schedule = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002086
2087 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00002088 E = CurRegion.element_end();
Tobias Grosser808cd692015-07-14 09:33:13 +00002089 I != E; ++I) {
2090 isl_schedule *StmtSchedule = nullptr;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002091 if (I->isSubRegion()) {
Tobias Grosser808cd692015-07-14 09:33:13 +00002092 StmtSchedule =
2093 buildScop(tempScop, *I->getNodeAs<Region>(), NestLoops, LI, SD);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002094 } else {
Tobias Grosser75805372011-04-29 06:27:02 +00002095 BasicBlock *BB = I->getNodeAs<BasicBlock>();
2096
Tobias Grosser808cd692015-07-14 09:33:13 +00002097 if (isTrivialBB(BB, tempScop)) {
Tobias Grosser75805372011-04-29 06:27:02 +00002098 continue;
Tobias Grosser808cd692015-07-14 09:33:13 +00002099 } else {
2100 auto *Stmt = addScopStmt(BB, nullptr, tempScop, CurRegion, NestLoops);
2101 auto *Domain = Stmt->getDomain();
2102 StmtSchedule = isl_schedule_from_domain(isl_union_set_from_set(Domain));
2103 }
Tobias Grosser75805372011-04-29 06:27:02 +00002104 }
2105
Tobias Grosser808cd692015-07-14 09:33:13 +00002106 if (!Schedule)
2107 Schedule = StmtSchedule;
2108 else if (StmtSchedule)
2109 Schedule = isl_schedule_sequence(Schedule, StmtSchedule);
2110 }
Tobias Grosser75805372011-04-29 06:27:02 +00002111
Tobias Grosser808cd692015-07-14 09:33:13 +00002112 if (!L)
2113 return Schedule;
2114
2115 auto *Domain = isl_schedule_get_domain(Schedule);
2116 if (!isl_union_set_is_empty(Domain)) {
2117 auto *MUPA = mapToDimension(isl_union_set_copy(Domain), loopDepth);
2118 Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA);
2119 }
2120 isl_union_set_free(Domain);
2121
Tobias Grosser75805372011-04-29 06:27:02 +00002122 NestLoops.pop_back();
Tobias Grosser808cd692015-07-14 09:33:13 +00002123 return Schedule;
Tobias Grosser75805372011-04-29 06:27:02 +00002124}
2125
Johannes Doerfert7c494212014-10-31 23:13:39 +00002126ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
Tobias Grosser57411e32015-05-27 06:51:34 +00002127 auto StmtMapIt = StmtMap.find(BB);
Johannes Doerfert7c494212014-10-31 23:13:39 +00002128 if (StmtMapIt == StmtMap.end())
2129 return nullptr;
2130 return StmtMapIt->second;
2131}
2132
Tobias Grosser75805372011-04-29 06:27:02 +00002133//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00002134ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
2135 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00002136 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00002137}
2138
2139ScopInfo::~ScopInfo() {
2140 clear();
2141 isl_ctx_free(ctx);
2142}
2143
Tobias Grosser75805372011-04-29 06:27:02 +00002144void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00002145 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00002146 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00002147 AU.addRequired<ScalarEvolution>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002148 AU.addRequired<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002149 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002150 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00002151 AU.setPreservesAll();
2152}
2153
2154bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Chandler Carruthf5579872015-01-17 14:16:56 +00002155 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002156 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002157 ScopDetection &SD = getAnalysis<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002158 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
2159
2160 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
2161
2162 // This region is no Scop.
2163 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00002164 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002165 return false;
2166 }
2167
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002168 scop = new Scop(*tempScop, LI, SE, SD, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00002169
Tobias Grosserd6a50b32015-05-30 06:26:21 +00002170 DEBUG(scop->print(dbgs()));
2171
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002172 if (!PollyUseRuntimeAliasChecks) {
2173 // Statistics.
2174 ++ScopFound;
2175 if (scop->getMaxLoopDepth() > 0)
2176 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002177 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002178 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002179
Johannes Doerfert9143d672014-09-27 11:02:39 +00002180 // If a problem occurs while building the alias groups we need to delete
2181 // this SCoP and pretend it wasn't valid in the first place.
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002182 if (scop->buildAliasGroups(AA)) {
2183 // Statistics.
2184 ++ScopFound;
2185 if (scop->getMaxLoopDepth() > 0)
2186 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002187 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002188 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00002189
2190 DEBUG(dbgs()
2191 << "\n\nNOTE: Run time checks for " << scop->getNameStr()
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002192 << " could not be created as the number of parameters involved is too "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002193 "high. The SCoP will be "
2194 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the "
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002195 "maximal number of parameters but be advised that the compile time "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002196 "might increase exponentially.\n\n");
2197
2198 delete scop;
2199 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002200 return false;
2201}
2202
2203char ScopInfo::ID = 0;
2204
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002205Pass *polly::createScopInfoPass() { return new ScopInfo(); }
2206
Tobias Grosser73600b82011-10-08 00:30:40 +00002207INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
2208 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002209 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00002210INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Chandler Carruthf5579872015-01-17 14:16:56 +00002211INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00002212INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002213INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002214INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002215INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00002216INITIALIZE_PASS_END(ScopInfo, "polly-scops",
2217 "Polly - Create polyhedral description of Scops", false,
2218 false)