blob: 8f9c1176f0af5edc9ffbc7b1c9d68826f8dff60e [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 Grosserac3a95f2015-08-03 17:53:21 +0000673 auto IdName = "__polly_array_ref_" + std::to_string(Identifier);
Tobias Grossere29d31c2015-05-15 12:24:09 +0000674 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
Tobias Grosser62139132015-08-02 16:17:41 +0000883// @brief Get the data-type of the elements accessed
884static Type *getAccessType(IRAccess &Access, Instruction *AccessInst) {
885 if (Access.isPHI())
886 return Access.getBase()->getType();
887
888 if (StoreInst *Store = dyn_cast<StoreInst>(AccessInst))
889 return Store->getValueOperand()->getType();
890 if (BranchInst *Branch = dyn_cast<BranchInst>(AccessInst))
891 return Branch->getCondition()->getType();
892 return AccessInst->getType();
893}
894
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000895void ScopStmt::buildAccesses(TempScop &tempScop, BasicBlock *Block,
896 bool isApproximated) {
897 AccFuncSetType *AFS = tempScop.getAccessFunctions(Block);
898 if (!AFS)
899 return;
900
901 for (auto &AccessPair : *AFS) {
902 IRAccess &Access = AccessPair.first;
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000903 Instruction *AccessInst = AccessPair.second;
Tobias Grosser62139132015-08-02 16:17:41 +0000904 Type *ElementType = getAccessType(Access, AccessInst);
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000905
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000906 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
Tobias Grosser92245222015-07-28 14:53:44 +0000907 Access.getBase(), ElementType, Access.Sizes, Access.isPHI());
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000908
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000909 if (isApproximated && Access.isWrite())
910 Access.setMayWrite();
911
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000912 MemoryAccessList *&MAL = InstructionToAccess[AccessInst];
913 if (!MAL)
914 MAL = new MemoryAccessList();
915 MAL->emplace_front(Access, AccessInst, this, SAI, MemAccs.size());
916 MemAccs.push_back(&MAL->front());
Tobias Grosser75805372011-04-29 06:27:02 +0000917 }
918}
919
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000920void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000921 for (MemoryAccess *MA : *this)
922 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000923
924 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000925}
926
Tobias Grosser65b00582011-11-08 15:41:19 +0000927__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000928 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
929 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000930
Tobias Grosserd2795d02011-08-18 07:51:40 +0000931 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000932 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000933 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000934 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000935 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000936 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000937 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000938 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000939 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000940 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000941 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000942 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000943 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000944 case ICmpInst::ICMP_ULT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000945 return isl_pw_aff_lt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000946 case ICmpInst::ICMP_UGT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000947 return isl_pw_aff_gt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000948 case ICmpInst::ICMP_ULE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000949 return isl_pw_aff_le_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000950 case ICmpInst::ICMP_UGE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000951 return isl_pw_aff_ge_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000952 default:
953 llvm_unreachable("Non integer predicate not supported");
954 }
Tobias Grosser75805372011-04-29 06:27:02 +0000955}
956
Tobias Grossere19661e2011-10-07 08:46:57 +0000957__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000958 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000959 isl_space *Space;
960 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000961
Tobias Grossere19661e2011-10-07 08:46:57 +0000962 Space = isl_set_get_space(Domain);
963 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000964
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000965 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000966 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000967 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000968 isl_pw_aff *IV =
969 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000970
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000971 // 0 <= IV.
972 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
973 Domain = isl_set_intersect(Domain, LowerBound);
974
975 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000976 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000977 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000978 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
979 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000980 Domain = isl_set_intersect(Domain, UpperBoundSet);
981 }
982
Tobias Grosserf5338802011-10-06 00:03:35 +0000983 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000984 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000985}
986
Tobias Grossere602a072013-05-07 07:30:56 +0000987__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
988 TempScop &tempScop,
989 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000990 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000991 *CurrentRegion = &CurRegion;
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000992 const BasicBlock *BranchingBB = BB ? BB : R->getEntry();
Tobias Grosser75805372011-04-29 06:27:02 +0000993
Tobias Grosser75805372011-04-29 06:27:02 +0000994 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000995 if (BranchingBB != CurrentRegion->getEntry()) {
996 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000997 for (const auto &C : *Condition) {
998 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000999 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +00001000 }
1001 }
Tobias Grossere19661e2011-10-07 08:46:57 +00001002 BranchingBB = CurrentRegion->getEntry();
1003 CurrentRegion = CurrentRegion->getParent();
1004 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +00001005
Tobias Grossere19661e2011-10-07 08:46:57 +00001006 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +00001007}
1008
Tobias Grossere602a072013-05-07 07:30:56 +00001009__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
1010 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +00001011 isl_space *Space;
1012 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +00001013 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +00001014
1015 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
1016
Tobias Grosser084d8f72012-05-29 09:29:44 +00001017 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
1018
Tobias Grossere19661e2011-10-07 08:46:57 +00001019 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +00001020 Domain = addLoopBoundsToDomain(Domain, tempScop);
1021 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +00001022 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +00001023
1024 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +00001025}
1026
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001027void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
1028 int Dimension = 0;
1029 isl_ctx *Ctx = Parent.getIslCtx();
1030 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
1031 Type *Ty = GEP->getPointerOperandType();
1032 ScalarEvolution &SE = *Parent.getSE();
1033
1034 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
1035 Dimension = 1;
1036 Ty = PtrTy->getElementType();
1037 }
1038
1039 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
1040 unsigned int Operand = 1 + Dimension;
1041
1042 if (GEP->getNumOperands() <= Operand)
1043 break;
1044
1045 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
1046
1047 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
1048 isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr);
1049 AccessOffset =
1050 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
1051
1052 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
1053 isl_local_space_copy(LSpace),
1054 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
1055
1056 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
1057 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
1058 OutOfBound = isl_set_params(OutOfBound);
1059 isl_set *InBound = isl_set_complement(OutOfBound);
1060 isl_set *Executed = isl_set_params(getDomain());
1061
1062 // A => B == !A or B
1063 isl_set *InBoundIfExecuted =
1064 isl_set_union(isl_set_complement(Executed), InBound);
1065
1066 Parent.addAssumption(InBoundIfExecuted);
1067 }
1068
1069 Dimension += 1;
1070 Ty = ArrayTy->getElementType();
1071 }
1072
1073 isl_local_space_free(LSpace);
1074}
1075
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001076void ScopStmt::deriveAssumptions(BasicBlock *Block) {
1077 for (Instruction &Inst : *Block)
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001078 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
1079 deriveAssumptionsFromGEP(GEP);
1080}
1081
Tobias Grosser74394f02013-01-14 22:40:23 +00001082ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001083 Region &R, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001084 : Parent(parent), BB(nullptr), R(&R), Build(nullptr),
1085 NestLoops(Nest.size()) {
1086 // Setup the induction variables.
1087 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
1088 NestLoops[i] = Nest[i];
1089
Tobias Grosser16c44032015-07-09 07:31:45 +00001090 BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), "");
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001091
1092 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001093
1094 BasicBlock *EntryBB = R.getEntry();
1095 for (BasicBlock *Block : R.blocks()) {
1096 buildAccesses(tempScop, Block, Block != EntryBB);
1097 deriveAssumptions(Block);
1098 }
1099 checkForReductions();
1100}
1101
1102ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001103 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001104 : Parent(parent), BB(&bb), R(nullptr), Build(nullptr),
1105 NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +00001106 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +00001107 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +00001108 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +00001109
Johannes Doerfert79fc23f2014-07-24 23:48:02 +00001110 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +00001111
Tobias Grossere19661e2011-10-07 08:46:57 +00001112 Domain = buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001113 buildAccesses(tempScop, BB);
1114 deriveAssumptions(BB);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001115 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001116}
1117
Johannes Doerferte58a0122014-06-27 20:31:28 +00001118/// @brief Collect loads which might form a reduction chain with @p StoreMA
1119///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001120/// Check if the stored value for @p StoreMA is a binary operator with one or
1121/// two loads as operands. If the binary operand is commutative & associative,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001122/// used only once (by @p StoreMA) and its load operands are also used only
1123/// once, we have found a possible reduction chain. It starts at an operand
1124/// load and includes the binary operator and @p StoreMA.
1125///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001126/// Note: We allow only one use to ensure the load and binary operator cannot
Johannes Doerferte58a0122014-06-27 20:31:28 +00001127/// escape this block or into any other store except @p StoreMA.
1128void ScopStmt::collectCandiateReductionLoads(
1129 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
1130 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
1131 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001132 return;
1133
1134 // Skip if there is not one binary operator between the load and the store
1135 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +00001136 if (!BinOp)
1137 return;
1138
1139 // Skip if the binary operators has multiple uses
1140 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001141 return;
1142
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001143 // Skip if the opcode of the binary operator is not commutative/associative
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001144 if (!BinOp->isCommutative() || !BinOp->isAssociative())
1145 return;
1146
Johannes Doerfert9890a052014-07-01 00:32:29 +00001147 // Skip if the binary operator is outside the current SCoP
1148 if (BinOp->getParent() != Store->getParent())
1149 return;
1150
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001151 // Skip if it is a multiplicative reduction and we disabled them
1152 if (DisableMultiplicativeReductions &&
1153 (BinOp->getOpcode() == Instruction::Mul ||
1154 BinOp->getOpcode() == Instruction::FMul))
1155 return;
1156
Johannes Doerferte58a0122014-06-27 20:31:28 +00001157 // Check the binary operator operands for a candidate load
1158 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
1159 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
1160 if (!PossibleLoad0 && !PossibleLoad1)
1161 return;
1162
1163 // A load is only a candidate if it cannot escape (thus has only this use)
1164 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001165 if (PossibleLoad0->getParent() == Store->getParent())
1166 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001167 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001168 if (PossibleLoad1->getParent() == Store->getParent())
1169 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001170}
1171
1172/// @brief Check for reductions in this ScopStmt
1173///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001174/// Iterate over all store memory accesses and check for valid binary reduction
1175/// like chains. For all candidates we check if they have the same base address
1176/// and there are no other accesses which overlap with them. The base address
1177/// check rules out impossible reductions candidates early. The overlap check,
1178/// together with the "only one user" check in collectCandiateReductionLoads,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001179/// guarantees that none of the intermediate results will escape during
1180/// execution of the loop nest. We basically check here that no other memory
1181/// access can access the same memory as the potential reduction.
1182void ScopStmt::checkForReductions() {
1183 SmallVector<MemoryAccess *, 2> Loads;
1184 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1185
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001186 // First collect candidate load-store reduction chains by iterating over all
Johannes Doerferte58a0122014-06-27 20:31:28 +00001187 // stores and collecting possible reduction loads.
1188 for (MemoryAccess *StoreMA : MemAccs) {
1189 if (StoreMA->isRead())
1190 continue;
1191
1192 Loads.clear();
1193 collectCandiateReductionLoads(StoreMA, Loads);
1194 for (MemoryAccess *LoadMA : Loads)
1195 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1196 }
1197
1198 // Then check each possible candidate pair.
1199 for (const auto &CandidatePair : Candidates) {
1200 bool Valid = true;
1201 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1202 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1203
1204 // Skip those with obviously unequal base addresses.
1205 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1206 isl_map_free(LoadAccs);
1207 isl_map_free(StoreAccs);
1208 continue;
1209 }
1210
1211 // And check if the remaining for overlap with other memory accesses.
1212 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1213 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1214 isl_set *AllAccs = isl_map_range(AllAccsRel);
1215
1216 for (MemoryAccess *MA : MemAccs) {
1217 if (MA == CandidatePair.first || MA == CandidatePair.second)
1218 continue;
1219
1220 isl_map *AccRel =
1221 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1222 isl_set *Accs = isl_map_range(AccRel);
1223
1224 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1225 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1226 Valid = Valid && isl_set_is_empty(OverlapAccs);
1227 isl_set_free(OverlapAccs);
1228 }
1229 }
1230
1231 isl_set_free(AllAccs);
1232 if (!Valid)
1233 continue;
1234
Johannes Doerfertf6183392014-07-01 20:52:51 +00001235 const LoadInst *Load =
1236 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1237 MemoryAccess::ReductionType RT =
1238 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1239
Johannes Doerferte58a0122014-06-27 20:31:28 +00001240 // If no overlapping access was found we mark the load and store as
1241 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001242 CandidatePair.first->markAsReductionLike(RT);
1243 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001244 }
Tobias Grosser75805372011-04-29 06:27:02 +00001245}
1246
Tobias Grosser74394f02013-01-14 22:40:23 +00001247std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001248
Tobias Grosser54839312015-04-21 11:37:25 +00001249std::string ScopStmt::getScheduleStr() const {
Tobias Grosser808cd692015-07-14 09:33:13 +00001250 auto *S = getSchedule();
1251 auto Str = stringFromIslObj(S);
1252 isl_map_free(S);
1253 return Str;
Tobias Grosser75805372011-04-29 06:27:02 +00001254}
1255
Tobias Grosser74394f02013-01-14 22:40:23 +00001256unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001257
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001258unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001259
Tobias Grosser75805372011-04-29 06:27:02 +00001260const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1261
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001262const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001263 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001264}
1265
Tobias Grosser74394f02013-01-14 22:40:23 +00001266isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001267
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001268__isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001269
Tobias Grosser6e6c7e02015-03-30 12:22:39 +00001270__isl_give isl_space *ScopStmt::getDomainSpace() const {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001271 return isl_set_get_space(Domain);
1272}
1273
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001274__isl_give isl_id *ScopStmt::getDomainId() const {
1275 return isl_set_get_tuple_id(Domain);
1276}
Tobias Grossercd95b772012-08-30 11:49:38 +00001277
Tobias Grosser75805372011-04-29 06:27:02 +00001278ScopStmt::~ScopStmt() {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001279 DeleteContainerSeconds(InstructionToAccess);
Tobias Grosser75805372011-04-29 06:27:02 +00001280 isl_set_free(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +00001281}
1282
1283void ScopStmt::print(raw_ostream &OS) const {
1284 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001285 OS.indent(12) << "Domain :=\n";
1286
1287 if (Domain) {
1288 OS.indent(16) << getDomainStr() << ";\n";
1289 } else
1290 OS.indent(16) << "n/a\n";
1291
Tobias Grosser54839312015-04-21 11:37:25 +00001292 OS.indent(12) << "Schedule :=\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001293
1294 if (Domain) {
Tobias Grosser54839312015-04-21 11:37:25 +00001295 OS.indent(16) << getScheduleStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001296 } else
1297 OS.indent(16) << "n/a\n";
1298
Tobias Grosser083d3d32014-06-28 08:59:45 +00001299 for (MemoryAccess *Access : MemAccs)
1300 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001301}
1302
1303void ScopStmt::dump() const { print(dbgs()); }
1304
1305//===----------------------------------------------------------------------===//
1306/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001307
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001308void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001309 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1310 isl_set_free(Context);
1311 Context = NewContext;
1312}
1313
Tobias Grosserabfbe632013-02-05 12:09:06 +00001314void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001315 for (const SCEV *Parameter : NewParameters) {
Johannes Doerfertbe409962015-03-29 20:45:09 +00001316 Parameter = extractConstantFactor(Parameter, *SE).second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001317 if (ParameterIds.find(Parameter) != ParameterIds.end())
1318 continue;
1319
1320 int dimension = Parameters.size();
1321
1322 Parameters.push_back(Parameter);
1323 ParameterIds[Parameter] = dimension;
1324 }
1325}
1326
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001327__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1328 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001329
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001330 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001331 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001332
Tobias Grosser8f99c162011-11-15 11:38:55 +00001333 std::string ParameterName;
1334
1335 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1336 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001337 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001338 }
1339
1340 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001341 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001342
Tobias Grosser20532b82014-04-11 17:56:49 +00001343 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1344 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001345}
Tobias Grosser75805372011-04-29 06:27:02 +00001346
Tobias Grosser6be480c2011-11-08 15:41:13 +00001347void Scop::buildContext() {
1348 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001349 Context = isl_set_universe(isl_space_copy(Space));
1350 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001351}
1352
Tobias Grosser18daaca2012-05-22 10:47:27 +00001353void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001354 for (const auto &ParamID : ParameterIds) {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001355 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001356
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001357 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001358
Johannes Doerferte7044942015-02-24 11:58:30 +00001359 Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001360 }
1361}
1362
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001363void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001364 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001365 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001366
Tobias Grosser083d3d32014-06-28 08:59:45 +00001367 for (const auto &ParamID : ParameterIds) {
1368 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001369 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001370 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001371 }
1372
1373 // Align the parameters of all data structures to the model.
1374 Context = isl_set_align_params(Context, Space);
1375
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001376 for (ScopStmt &Stmt : *this)
1377 Stmt.realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001378}
1379
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001380void Scop::simplifyAssumedContext() {
1381 // The parameter constraints of the iteration domains give us a set of
1382 // constraints that need to hold for all cases where at least a single
1383 // statement iteration is executed in the whole scop. We now simplify the
1384 // assumed context under the assumption that such constraints hold and at
1385 // least a single statement iteration is executed. For cases where no
1386 // statement instances are executed, the assumptions we have taken about
1387 // the executed code do not matter and can be changed.
1388 //
1389 // WARNING: This only holds if the assumptions we have taken do not reduce
1390 // the set of statement instances that are executed. Otherwise we
1391 // may run into a case where the iteration domains suggest that
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001392 // for a certain set of parameter constraints no code is executed,
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001393 // but in the original program some computation would have been
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001394 // performed. In such a case, modifying the run-time conditions and
1395 // possibly influencing the run-time check may cause certain scops
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001396 // to not be executed.
1397 //
1398 // Example:
1399 //
1400 // When delinearizing the following code:
1401 //
1402 // for (long i = 0; i < 100; i++)
1403 // for (long j = 0; j < m; j++)
1404 // A[i+p][j] = 1.0;
1405 //
1406 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001407 // otherwise we would access out of bound data. Now, knowing that code is
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001408 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1409 AssumedContext =
1410 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001411 AssumedContext = isl_set_gist_params(AssumedContext, getContext());
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001412}
1413
Johannes Doerfertb164c792014-09-18 11:17:17 +00001414/// @brief Add the minimal/maximal access in @p Set to @p User.
Tobias Grosserb2f39922015-05-28 13:32:11 +00001415static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001416 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1417 isl_pw_multi_aff *MinPMA, *MaxPMA;
1418 isl_pw_aff *LastDimAff;
1419 isl_aff *OneAff;
1420 unsigned Pos;
1421
Johannes Doerfert9143d672014-09-27 11:02:39 +00001422 // Restrict the number of parameters involved in the access as the lexmin/
1423 // lexmax computation will take too long if this number is high.
1424 //
1425 // Experiments with a simple test case using an i7 4800MQ:
1426 //
1427 // #Parameters involved | Time (in sec)
1428 // 6 | 0.01
1429 // 7 | 0.04
1430 // 8 | 0.12
1431 // 9 | 0.40
1432 // 10 | 1.54
1433 // 11 | 6.78
1434 // 12 | 30.38
1435 //
1436 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1437 unsigned InvolvedParams = 0;
1438 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1439 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1440 InvolvedParams++;
1441
1442 if (InvolvedParams > RunTimeChecksMaxParameters) {
1443 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001444 return isl_stat_error;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001445 }
1446 }
1447
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001448 Set = isl_set_remove_divs(Set);
1449
Johannes Doerfertb164c792014-09-18 11:17:17 +00001450 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1451 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1452
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001453 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1454 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1455
Johannes Doerfertb164c792014-09-18 11:17:17 +00001456 // Adjust the last dimension of the maximal access by one as we want to
1457 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1458 // we test during code generation might now point after the end of the
1459 // allocated array but we will never dereference it anyway.
1460 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1461 "Assumed at least one output dimension");
1462 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1463 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1464 OneAff = isl_aff_zero_on_domain(
1465 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1466 OneAff = isl_aff_add_constant_si(OneAff, 1);
1467 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1468 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1469
1470 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1471
1472 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001473 return isl_stat_ok;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001474}
1475
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001476static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1477 isl_set *Domain = MA->getStatement()->getDomain();
1478 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1479 return isl_set_reset_tuple_id(Domain);
1480}
1481
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001482/// @brief Wrapper function to calculate minimal/maximal accesses to each array.
1483static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses,
Tobias Grosserbb853c22015-07-25 12:31:03 +00001484 __isl_take isl_union_set *Domains,
1485 __isl_take isl_set *AssumedContext,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001486 Scop::MinMaxVectorTy &MinMaxAccesses) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001487
1488 Accesses = isl_union_map_intersect_domain(Accesses, Domains);
1489 isl_union_set *Locations = isl_union_map_range(Accesses);
1490 Locations = isl_union_set_intersect_params(Locations, AssumedContext);
1491 Locations = isl_union_set_coalesce(Locations);
1492 Locations = isl_union_set_detect_equalities(Locations);
1493 bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001494 &MinMaxAccesses));
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001495 isl_union_set_free(Locations);
1496 return Valid;
1497}
1498
Johannes Doerfert9143d672014-09-27 11:02:39 +00001499bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001500 // To create sound alias checks we perform the following steps:
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001501 // o) Use the alias analysis and an alias set tracker to build alias sets
Johannes Doerfertb164c792014-09-18 11:17:17 +00001502 // for all memory accesses inside the SCoP.
1503 // o) For each alias set we then map the aliasing pointers back to the
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001504 // memory accesses we know, thus obtain groups of memory accesses which
Johannes Doerfertb164c792014-09-18 11:17:17 +00001505 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001506 // o) We divide each group based on the domains of the minimal/maximal
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001507 // accesses. That means two minimal/maximal accesses are only in a group
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001508 // if their access domains intersect, otherwise they are in different
1509 // ones.
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001510 // o) We partition each group into read only and non read only accesses.
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001511 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001512 // and maximal accesses to each array of a group in read only and non
1513 // read only partitions separately.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001514 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1515
1516 AliasSetTracker AST(AA);
1517
1518 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001519 DenseSet<Value *> HasWriteAccess;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001520 for (ScopStmt &Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001521
1522 // Skip statements with an empty domain as they will never be executed.
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001523 isl_set *StmtDomain = Stmt.getDomain();
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001524 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1525 isl_set_free(StmtDomain);
1526 if (StmtDomainEmpty)
1527 continue;
1528
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001529 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001530 if (MA->isScalar())
1531 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001532 if (!MA->isRead())
1533 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001534 Instruction *Acc = MA->getAccessInstruction();
1535 PtrToAcc[getPointerOperand(*Acc)] = MA;
1536 AST.add(Acc);
1537 }
1538 }
1539
1540 SmallVector<AliasGroupTy, 4> AliasGroups;
1541 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001542 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001543 continue;
1544 AliasGroupTy AG;
1545 for (auto PR : AS)
1546 AG.push_back(PtrToAcc[PR.getValue()]);
1547 assert(AG.size() > 1 &&
1548 "Alias groups should contain at least two accesses");
1549 AliasGroups.push_back(std::move(AG));
1550 }
1551
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001552 // Split the alias groups based on their domain.
1553 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1554 AliasGroupTy NewAG;
1555 AliasGroupTy &AG = AliasGroups[u];
1556 AliasGroupTy::iterator AGI = AG.begin();
1557 isl_set *AGDomain = getAccessDomain(*AGI);
1558 while (AGI != AG.end()) {
1559 MemoryAccess *MA = *AGI;
1560 isl_set *MADomain = getAccessDomain(MA);
1561 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1562 NewAG.push_back(MA);
1563 AGI = AG.erase(AGI);
1564 isl_set_free(MADomain);
1565 } else {
1566 AGDomain = isl_set_union(AGDomain, MADomain);
1567 AGI++;
1568 }
1569 }
1570 if (NewAG.size() > 1)
1571 AliasGroups.push_back(std::move(NewAG));
1572 isl_set_free(AGDomain);
1573 }
1574
Tobias Grosserf4c24b22015-04-05 13:11:54 +00001575 MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
Johannes Doerfert13771732014-10-01 12:40:46 +00001576 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1577 for (AliasGroupTy &AG : AliasGroups) {
1578 NonReadOnlyBaseValues.clear();
1579 ReadOnlyPairs.clear();
1580
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001581 if (AG.size() < 2) {
1582 AG.clear();
1583 continue;
1584 }
1585
Johannes Doerfert13771732014-10-01 12:40:46 +00001586 for (auto II = AG.begin(); II != AG.end();) {
1587 Value *BaseAddr = (*II)->getBaseAddr();
1588 if (HasWriteAccess.count(BaseAddr)) {
1589 NonReadOnlyBaseValues.insert(BaseAddr);
1590 II++;
1591 } else {
1592 ReadOnlyPairs[BaseAddr].insert(*II);
1593 II = AG.erase(II);
1594 }
1595 }
1596
1597 // If we don't have read only pointers check if there are at least two
1598 // non read only pointers, otherwise clear the alias group.
Tobias Grosserbb853c22015-07-25 12:31:03 +00001599 if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001600 AG.clear();
Johannes Doerfert13771732014-10-01 12:40:46 +00001601 continue;
1602 }
1603
1604 // If we don't have non read only pointers clear the alias group.
1605 if (NonReadOnlyBaseValues.empty()) {
1606 AG.clear();
1607 continue;
1608 }
1609
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001610 // Calculate minimal and maximal accesses for non read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001611 MinMaxAliasGroups.emplace_back();
1612 MinMaxVectorPairTy &pair = MinMaxAliasGroups.back();
1613 MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first;
1614 MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second;
1615 MinMaxAccessesNonReadOnly.reserve(AG.size());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001616
1617 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001618
1619 // AG contains only non read only accesses.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001620 for (MemoryAccess *MA : AG)
1621 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001622
Tobias Grosserbb853c22015-07-25 12:31:03 +00001623 bool Valid = calculateMinMaxAccess(
1624 Accesses, getDomains(), getAssumedContext(), MinMaxAccessesNonReadOnly);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001625
1626 // Bail out if the number of values we need to compare is too large.
1627 // This is important as the number of comparisions grows quadratically with
1628 // the number of values we need to compare.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001629 if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() >
1630 RunTimeChecksMaxArraysPerGroup))
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001631 return false;
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001632
1633 // Calculate minimal and maximal accesses for read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001634 MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001635 Accesses = isl_union_map_empty(getParamSpace());
1636
1637 for (const auto &ReadOnlyPair : ReadOnlyPairs)
1638 for (MemoryAccess *MA : ReadOnlyPair.second)
1639 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1640
Tobias Grosserbb853c22015-07-25 12:31:03 +00001641 Valid = calculateMinMaxAccess(Accesses, getDomains(), getAssumedContext(),
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001642 MinMaxAccessesReadOnly);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001643
1644 if (!Valid)
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001645 return false;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001646 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001647
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001648 return true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001649}
1650
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001651static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI,
1652 ScopDetection &SD) {
1653
1654 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R);
1655
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001656 unsigned MinLD = INT_MAX, MaxLD = 0;
1657 for (BasicBlock *BB : R.blocks()) {
1658 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00001659 if (!R.contains(L))
1660 continue;
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001661 if (BoxedLoops && BoxedLoops->count(L))
1662 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001663 unsigned LD = L->getLoopDepth();
1664 MinLD = std::min(MinLD, LD);
1665 MaxLD = std::max(MaxLD, LD);
1666 }
1667 }
1668
1669 // Handle the case that there is no loop in the SCoP first.
1670 if (MaxLD == 0)
1671 return 1;
1672
1673 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1674 assert(MaxLD >= MinLD &&
1675 "Maximal loop depth was smaller than mininaml loop depth?");
1676 return MaxLD - MinLD + 1;
1677}
1678
Michael Kruse471a5e32015-07-30 19:27:04 +00001679Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, isl_ctx *Context,
1680 unsigned MaxLoopDepth)
1681 : SE(&ScalarEvolution), R(R), IsOptimized(false),
1682 MaxLoopDepth(MaxLoopDepth), IslCtx(Context) {}
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001683
Michael Kruse471a5e32015-07-30 19:27:04 +00001684void Scop::initFromTempScop(TempScop &TempScop, LoopInfo &LI,
1685 ScopDetection &SD) {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001686 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001687
Tobias Grosserabfbe632013-02-05 12:09:06 +00001688 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001689
Tobias Grosser54839312015-04-21 11:37:25 +00001690 // Build the iteration domain, access functions and schedule functions
Tobias Grosser75805372011-04-29 06:27:02 +00001691 // traversing the region tree.
Michael Kruse471a5e32015-07-30 19:27:04 +00001692 Schedule = buildScop(TempScop, getRegion(), NestLoops, LI, SD);
Tobias Grosser808cd692015-07-14 09:33:13 +00001693 if (!Schedule)
1694 Schedule = isl_schedule_empty(getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +00001695
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001696 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001697 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001698 simplifyAssumedContext();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001699
Tobias Grosser75805372011-04-29 06:27:02 +00001700 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1701}
1702
Michael Kruse471a5e32015-07-30 19:27:04 +00001703Scop *Scop::createFromTempScop(TempScop &TempScop, LoopInfo &LI,
1704 ScalarEvolution &SE, ScopDetection &SD,
1705 isl_ctx *ctx) {
1706 auto &R = TempScop.getMaxRegion();
1707 auto MaxLoopDepth = getMaxLoopDepthInRegion(R, LI, SD);
1708 auto S = new Scop(R, SE, ctx, MaxLoopDepth);
1709 S->initFromTempScop(TempScop, LI, SD);
1710 return S;
1711}
1712
Tobias Grosser75805372011-04-29 06:27:02 +00001713Scop::~Scop() {
1714 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001715 isl_set_free(AssumedContext);
Tobias Grosser808cd692015-07-14 09:33:13 +00001716 isl_schedule_free(Schedule);
Tobias Grosser75805372011-04-29 06:27:02 +00001717
Johannes Doerfertb164c792014-09-18 11:17:17 +00001718 // Free the alias groups
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001719 for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001720 for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001721 isl_pw_multi_aff_free(MMA.first);
1722 isl_pw_multi_aff_free(MMA.second);
1723 }
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001724 for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001725 isl_pw_multi_aff_free(MMA.first);
1726 isl_pw_multi_aff_free(MMA.second);
1727 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001728 }
Tobias Grosser75805372011-04-29 06:27:02 +00001729}
1730
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001731const ScopArrayInfo *
1732Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
Tobias Grosser92245222015-07-28 14:53:44 +00001733 const SmallVector<const SCEV *, 4> &Sizes,
1734 bool IsPHI) {
1735 auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001736 if (!SAI)
Tobias Grosser92245222015-07-28 14:53:44 +00001737 SAI.reset(
1738 new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI));
Tobias Grosserab671442015-05-23 05:58:27 +00001739 return SAI.get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001740}
1741
Tobias Grosser92245222015-07-28 14:53:44 +00001742const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) {
1743 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001744 assert(SAI && "No ScopArrayInfo available for this base pointer");
1745 return SAI;
1746}
1747
Tobias Grosser74394f02013-01-14 22:40:23 +00001748std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001749std::string Scop::getAssumedContextStr() const {
1750 return stringFromIslObj(AssumedContext);
1751}
Tobias Grosser75805372011-04-29 06:27:02 +00001752
1753std::string Scop::getNameStr() const {
1754 std::string ExitName, EntryName;
1755 raw_string_ostream ExitStr(ExitName);
1756 raw_string_ostream EntryStr(EntryName);
1757
Tobias Grosserf240b482014-01-09 10:42:15 +00001758 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001759 EntryStr.str();
1760
1761 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001762 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001763 ExitStr.str();
1764 } else
1765 ExitName = "FunctionExit";
1766
1767 return EntryName + "---" + ExitName;
1768}
1769
Tobias Grosser74394f02013-01-14 22:40:23 +00001770__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001771__isl_give isl_space *Scop::getParamSpace() const {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001772 return isl_set_get_space(Context);
Tobias Grosser37487052011-10-06 00:03:42 +00001773}
1774
Tobias Grossere86109f2013-10-29 21:05:49 +00001775__isl_give isl_set *Scop::getAssumedContext() const {
1776 return isl_set_copy(AssumedContext);
1777}
1778
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001779void Scop::addAssumption(__isl_take isl_set *Set) {
1780 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001781 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001782}
1783
Tobias Grosser75805372011-04-29 06:27:02 +00001784void Scop::printContext(raw_ostream &OS) const {
1785 OS << "Context:\n";
1786
1787 if (!Context) {
1788 OS.indent(4) << "n/a\n\n";
1789 return;
1790 }
1791
1792 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001793
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001794 OS.indent(4) << "Assumed Context:\n";
1795 if (!AssumedContext) {
1796 OS.indent(4) << "n/a\n\n";
1797 return;
1798 }
1799
1800 OS.indent(4) << getAssumedContextStr() << "\n";
1801
Tobias Grosser083d3d32014-06-28 08:59:45 +00001802 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001803 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001804 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1805 }
Tobias Grosser75805372011-04-29 06:27:02 +00001806}
1807
Johannes Doerfertb164c792014-09-18 11:17:17 +00001808void Scop::printAliasAssumptions(raw_ostream &OS) const {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001809 int noOfGroups = 0;
1810 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001811 if (Pair.second.size() == 0)
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001812 noOfGroups += 1;
1813 else
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001814 noOfGroups += Pair.second.size();
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001815 }
1816
Tobias Grosserbb853c22015-07-25 12:31:03 +00001817 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n";
Johannes Doerfertb164c792014-09-18 11:17:17 +00001818 if (MinMaxAliasGroups.empty()) {
1819 OS.indent(8) << "n/a\n";
1820 return;
1821 }
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001822
Tobias Grosserbb853c22015-07-25 12:31:03 +00001823 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001824
1825 // If the group has no read only accesses print the write accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001826 if (Pair.second.empty()) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001827 OS.indent(8) << "[[";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001828 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001829 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1830 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001831 }
1832 OS << " ]]\n";
1833 }
1834
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001835 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001836 OS.indent(8) << "[[";
Tobias Grosserbb853c22015-07-25 12:31:03 +00001837 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001838 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001839 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
1840 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001841 }
1842 OS << " ]]\n";
1843 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001844 }
1845}
1846
Tobias Grosser75805372011-04-29 06:27:02 +00001847void Scop::printStatements(raw_ostream &OS) const {
1848 OS << "Statements {\n";
1849
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001850 for (const ScopStmt &Stmt : *this)
1851 OS.indent(4) << Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001852
1853 OS.indent(4) << "}\n";
1854}
1855
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001856void Scop::printArrayInfo(raw_ostream &OS) const {
1857 OS << "Arrays {\n";
1858
Tobias Grosserab671442015-05-23 05:58:27 +00001859 for (auto &Array : arrays())
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001860 Array.second->print(OS);
1861
1862 OS.indent(4) << "}\n";
1863}
1864
Tobias Grosser75805372011-04-29 06:27:02 +00001865void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001866 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1867 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001868 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00001869 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001870 printContext(OS.indent(4));
Tobias Grosser49ad36c2015-05-20 08:05:31 +00001871 printArrayInfo(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001872 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001873 printStatements(OS.indent(4));
1874}
1875
1876void Scop::dump() const { print(dbgs()); }
1877
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001878isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001879
Tobias Grosser808cd692015-07-14 09:33:13 +00001880__isl_give isl_union_set *Scop::getDomains() const {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001881 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001882
Tobias Grosser808cd692015-07-14 09:33:13 +00001883 for (const ScopStmt &Stmt : *this)
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001884 Domain = isl_union_set_add_set(Domain, Stmt.getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001885
1886 return Domain;
1887}
1888
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001889__isl_give isl_union_map *Scop::getMustWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001890 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001891
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001892 for (ScopStmt &Stmt : *this) {
1893 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001894 if (!MA->isMustWrite())
1895 continue;
1896
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001897 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001898 isl_map *AccessDomain = MA->getAccessRelation();
1899 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1900 Write = isl_union_map_add_map(Write, AccessDomain);
1901 }
1902 }
1903 return isl_union_map_coalesce(Write);
1904}
1905
1906__isl_give isl_union_map *Scop::getMayWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001907 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001908
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001909 for (ScopStmt &Stmt : *this) {
1910 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001911 if (!MA->isMayWrite())
1912 continue;
1913
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001914 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001915 isl_map *AccessDomain = MA->getAccessRelation();
1916 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1917 Write = isl_union_map_add_map(Write, AccessDomain);
1918 }
1919 }
1920 return isl_union_map_coalesce(Write);
1921}
1922
Tobias Grosser37eb4222014-02-20 21:43:54 +00001923__isl_give isl_union_map *Scop::getWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001924 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001925
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001926 for (ScopStmt &Stmt : *this) {
1927 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001928 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001929 continue;
1930
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001931 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001932 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001933 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1934 Write = isl_union_map_add_map(Write, AccessDomain);
1935 }
1936 }
1937 return isl_union_map_coalesce(Write);
1938}
1939
1940__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001941 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001942
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001943 for (ScopStmt &Stmt : *this) {
1944 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001945 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001946 continue;
1947
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001948 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001949 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001950
1951 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1952 Read = isl_union_map_add_map(Read, AccessDomain);
1953 }
1954 }
1955 return isl_union_map_coalesce(Read);
1956}
1957
Tobias Grosser808cd692015-07-14 09:33:13 +00001958__isl_give isl_union_map *Scop::getSchedule() const {
1959 auto Tree = getScheduleTree();
1960 auto S = isl_schedule_get_map(Tree);
1961 isl_schedule_free(Tree);
1962 return S;
1963}
Tobias Grosser37eb4222014-02-20 21:43:54 +00001964
Tobias Grosser808cd692015-07-14 09:33:13 +00001965__isl_give isl_schedule *Scop::getScheduleTree() const {
1966 return isl_schedule_intersect_domain(isl_schedule_copy(Schedule),
1967 getDomains());
1968}
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001969
Tobias Grosser808cd692015-07-14 09:33:13 +00001970void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) {
1971 auto *S = isl_schedule_from_domain(getDomains());
1972 S = isl_schedule_insert_partial_schedule(
1973 S, isl_multi_union_pw_aff_from_union_map(NewSchedule));
1974 isl_schedule_free(Schedule);
1975 Schedule = S;
1976}
1977
1978void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) {
1979 isl_schedule_free(Schedule);
1980 Schedule = NewSchedule;
Tobias Grosser37eb4222014-02-20 21:43:54 +00001981}
1982
1983bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1984 bool Changed = false;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001985 for (ScopStmt &Stmt : *this) {
1986 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001987 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1988 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1989
1990 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1991 isl_union_set_free(StmtDomain);
1992 isl_union_set_free(NewStmtDomain);
1993 continue;
1994 }
1995
1996 Changed = true;
1997
1998 isl_union_set_free(StmtDomain);
1999 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
2000
2001 if (isl_union_set_is_empty(NewStmtDomain)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002002 Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002003 isl_union_set_free(NewStmtDomain);
2004 } else
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002005 Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002006 }
2007 isl_union_set_free(Domain);
2008 return Changed;
2009}
2010
Tobias Grosser75805372011-04-29 06:27:02 +00002011ScalarEvolution *Scop::getSE() const { return SE; }
2012
2013bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
2014 if (tempScop.getAccessFunctions(BB))
2015 return false;
2016
2017 return true;
2018}
2019
Tobias Grosser808cd692015-07-14 09:33:13 +00002020struct MapToDimensionDataTy {
2021 int N;
2022 isl_union_pw_multi_aff *Res;
2023};
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002024
Tobias Grosser808cd692015-07-14 09:33:13 +00002025// @brief Create a function that maps the elements of 'Set' to its N-th
2026// dimension.
2027//
2028// The result is added to 'User->Res'.
2029//
2030// @param Set The input set.
2031// @param N The dimension to map to.
2032//
2033// @returns Zero if no error occurred, non-zero otherwise.
2034static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) {
2035 struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User;
2036 int Dim;
2037 isl_space *Space;
2038 isl_pw_multi_aff *PMA;
2039
2040 Dim = isl_set_dim(Set, isl_dim_set);
2041 Space = isl_set_get_space(Set);
2042 PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N,
2043 Dim - Data->N);
2044 if (Data->N > 1)
2045 PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1);
2046 Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA);
2047
2048 isl_set_free(Set);
2049
2050 return isl_stat_ok;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002051}
2052
Tobias Grosser808cd692015-07-14 09:33:13 +00002053// @brief Create a function that maps the elements of Domain to their Nth
2054// dimension.
2055//
2056// @param Domain The set of elements to map.
2057// @param N The dimension to map to.
2058static __isl_give isl_multi_union_pw_aff *
2059mapToDimension(__isl_take isl_union_set *Domain, int N) {
2060 struct MapToDimensionDataTy Data;
2061 isl_space *Space;
2062
2063 Space = isl_union_set_get_space(Domain);
2064 Data.N = N;
2065 Data.Res = isl_union_pw_multi_aff_empty(Space);
2066 if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0)
2067 Data.Res = isl_union_pw_multi_aff_free(Data.Res);
2068
2069 isl_union_set_free(Domain);
2070 return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res);
2071}
2072
2073ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R, TempScop &tempScop,
2074 const Region &CurRegion,
2075 SmallVectorImpl<Loop *> &NestLoops) {
2076 ScopStmt *Stmt;
2077 if (BB) {
2078 Stmts.emplace_back(*this, tempScop, CurRegion, *BB, NestLoops);
2079 Stmt = &Stmts.back();
2080 StmtMap[BB] = Stmt;
2081 } else {
2082 assert(R && "Either basic block or a region expected.");
2083 Stmts.emplace_back(*this, tempScop, CurRegion, *R, NestLoops);
2084 Stmt = &Stmts.back();
2085 for (BasicBlock *BB : R->blocks())
2086 StmtMap[BB] = Stmt;
2087 }
2088 return Stmt;
2089}
2090
2091__isl_give isl_schedule *Scop::buildScop(TempScop &tempScop,
2092 const Region &CurRegion,
2093 SmallVectorImpl<Loop *> &NestLoops,
2094 LoopInfo &LI, ScopDetection &SD) {
2095 if (SD.isNonAffineSubRegion(&CurRegion, &getRegion())) {
2096 auto *Stmt = addScopStmt(nullptr, const_cast<Region *>(&CurRegion),
2097 tempScop, CurRegion, NestLoops);
2098 auto *Domain = Stmt->getDomain();
2099 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2100 }
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002101
Tobias Grosser75805372011-04-29 06:27:02 +00002102 Loop *L = castToLoop(CurRegion, LI);
2103
2104 if (L)
2105 NestLoops.push_back(L);
2106
2107 unsigned loopDepth = NestLoops.size();
Tobias Grosser808cd692015-07-14 09:33:13 +00002108 isl_schedule *Schedule = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002109
2110 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00002111 E = CurRegion.element_end();
Tobias Grosser808cd692015-07-14 09:33:13 +00002112 I != E; ++I) {
2113 isl_schedule *StmtSchedule = nullptr;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002114 if (I->isSubRegion()) {
Tobias Grosser808cd692015-07-14 09:33:13 +00002115 StmtSchedule =
2116 buildScop(tempScop, *I->getNodeAs<Region>(), NestLoops, LI, SD);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002117 } else {
Tobias Grosser75805372011-04-29 06:27:02 +00002118 BasicBlock *BB = I->getNodeAs<BasicBlock>();
2119
Tobias Grosser808cd692015-07-14 09:33:13 +00002120 if (isTrivialBB(BB, tempScop)) {
Tobias Grosser75805372011-04-29 06:27:02 +00002121 continue;
Tobias Grosser808cd692015-07-14 09:33:13 +00002122 } else {
2123 auto *Stmt = addScopStmt(BB, nullptr, tempScop, CurRegion, NestLoops);
2124 auto *Domain = Stmt->getDomain();
2125 StmtSchedule = isl_schedule_from_domain(isl_union_set_from_set(Domain));
2126 }
Tobias Grosser75805372011-04-29 06:27:02 +00002127 }
2128
Tobias Grosser808cd692015-07-14 09:33:13 +00002129 if (!Schedule)
2130 Schedule = StmtSchedule;
2131 else if (StmtSchedule)
2132 Schedule = isl_schedule_sequence(Schedule, StmtSchedule);
2133 }
Tobias Grosser75805372011-04-29 06:27:02 +00002134
Tobias Grosser808cd692015-07-14 09:33:13 +00002135 if (!L)
2136 return Schedule;
2137
2138 auto *Domain = isl_schedule_get_domain(Schedule);
2139 if (!isl_union_set_is_empty(Domain)) {
2140 auto *MUPA = mapToDimension(isl_union_set_copy(Domain), loopDepth);
2141 Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA);
2142 }
2143 isl_union_set_free(Domain);
2144
Tobias Grosser75805372011-04-29 06:27:02 +00002145 NestLoops.pop_back();
Tobias Grosser808cd692015-07-14 09:33:13 +00002146 return Schedule;
Tobias Grosser75805372011-04-29 06:27:02 +00002147}
2148
Johannes Doerfert7c494212014-10-31 23:13:39 +00002149ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
Tobias Grosser57411e32015-05-27 06:51:34 +00002150 auto StmtMapIt = StmtMap.find(BB);
Johannes Doerfert7c494212014-10-31 23:13:39 +00002151 if (StmtMapIt == StmtMap.end())
2152 return nullptr;
2153 return StmtMapIt->second;
2154}
2155
Tobias Grosser75805372011-04-29 06:27:02 +00002156//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00002157ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
2158 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00002159 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00002160}
2161
2162ScopInfo::~ScopInfo() {
2163 clear();
2164 isl_ctx_free(ctx);
2165}
2166
Tobias Grosser75805372011-04-29 06:27:02 +00002167void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00002168 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00002169 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00002170 AU.addRequired<ScalarEvolution>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002171 AU.addRequired<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002172 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002173 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00002174 AU.setPreservesAll();
2175}
2176
2177bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Chandler Carruthf5579872015-01-17 14:16:56 +00002178 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfertb164c792014-09-18 11:17:17 +00002179 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002180 ScopDetection &SD = getAnalysis<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002181 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
2182
2183 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
2184
2185 // This region is no Scop.
2186 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00002187 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002188 return false;
2189 }
2190
Michael Kruse471a5e32015-07-30 19:27:04 +00002191 scop = Scop::createFromTempScop(*tempScop, LI, SE, SD, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00002192
Tobias Grosserd6a50b32015-05-30 06:26:21 +00002193 DEBUG(scop->print(dbgs()));
2194
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002195 if (!PollyUseRuntimeAliasChecks) {
2196 // Statistics.
2197 ++ScopFound;
2198 if (scop->getMaxLoopDepth() > 0)
2199 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002200 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002201 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002202
Johannes Doerfert9143d672014-09-27 11:02:39 +00002203 // If a problem occurs while building the alias groups we need to delete
2204 // this SCoP and pretend it wasn't valid in the first place.
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002205 if (scop->buildAliasGroups(AA)) {
2206 // Statistics.
2207 ++ScopFound;
2208 if (scop->getMaxLoopDepth() > 0)
2209 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00002210 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00002211 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00002212
2213 DEBUG(dbgs()
2214 << "\n\nNOTE: Run time checks for " << scop->getNameStr()
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002215 << " could not be created as the number of parameters involved is too "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002216 "high. The SCoP will be "
2217 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the "
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002218 "maximal number of parameters but be advised that the compile time "
Johannes Doerfert9143d672014-09-27 11:02:39 +00002219 "might increase exponentially.\n\n");
2220
2221 delete scop;
2222 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002223 return false;
2224}
2225
2226char ScopInfo::ID = 0;
2227
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002228Pass *polly::createScopInfoPass() { return new ScopInfo(); }
2229
Tobias Grosser73600b82011-10-08 00:30:40 +00002230INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
2231 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002232 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00002233INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Chandler Carruthf5579872015-01-17 14:16:56 +00002234INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00002235INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002236INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002237INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002238INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00002239INITIALIZE_PASS_END(ScopInfo, "polly-scops",
2240 "Polly - Create polyhedral description of Scops", false,
2241 false)