blob: cce635e29705f14577232ad24c86e38ff1a6a7f0 [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"
Sebastian Pop27c10c62013-03-22 22:07:43 +000021#include "polly/ScopInfo.h"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000022#include "polly/Options.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 Grosser75805372011-04-29 06:27:02 +000027#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000028#include "llvm/ADT/Statistic.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000029#include "llvm/ADT/StringExtras.h"
Tobias Grosser83628182013-05-07 08:11:54 +000030#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000031#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser83628182013-05-07 08:11:54 +000032#include "llvm/Analysis/RegionIterator.h"
33#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000034#include "llvm/Support/Debug.h"
35
36#include "isl/constraint.h"
37#include "isl/set.h"
38#include "isl/map.h"
Tobias Grosser37eb4222014-02-20 21:43:54 +000039#include "isl/union_map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000040#include "isl/aff.h"
41#include "isl/printer.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000042#include "isl/local_space.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000043#include "isl/options.h"
Tobias Grosseredab1352013-06-21 06:41:31 +000044#include "isl/val.h"
Chandler Carruth95fef942014-04-22 03:30:19 +000045
Tobias Grosser75805372011-04-29 06:27:02 +000046#include <sstream>
47#include <string>
48#include <vector>
49
50using namespace llvm;
51using namespace polly;
52
Chandler Carruth95fef942014-04-22 03:30:19 +000053#define DEBUG_TYPE "polly-scops"
54
Tobias Grosser74394f02013-01-14 22:40:23 +000055STATISTIC(ScopFound, "Number of valid Scops");
56STATISTIC(RichScopFound, "Number of Scops containing a loop");
Tobias Grosser75805372011-04-29 06:27:02 +000057
Johannes Doerfert9e7b17b2014-08-18 00:40:13 +000058// Multiplicative reductions can be disabled separately as these kind of
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000059// operations can overflow easily. Additive reductions and bit operations
60// are in contrast pretty stable.
Tobias Grosser483a90d2014-07-09 10:50:10 +000061static cl::opt<bool> DisableMultiplicativeReductions(
62 "polly-disable-multiplicative-reductions",
63 cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore,
64 cl::init(false), cl::cat(PollyCategory));
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000065
Johannes Doerfert9143d672014-09-27 11:02:39 +000066static cl::opt<unsigned> RunTimeChecksMaxParameters(
67 "polly-rtc-max-parameters",
68 cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden,
69 cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory));
70
Tobias Grosser0695ee42013-09-17 03:30:31 +000071/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000072struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000073public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000074 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
75 ///
76 /// @param Stmt The location at which the scalar evolution expression
77 /// is evaluated.
78 /// @param Expr The expression that is translated.
79 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
80
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000081private:
Tobias Grosser3cc99742012-06-06 16:33:15 +000082 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000083 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +000084 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000085
Tobias Grosser0695ee42013-09-17 03:30:31 +000086 SCEVAffinator(const ScopStmt *Stmt);
87 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +000088
Tobias Grosser0695ee42013-09-17 03:30:31 +000089 __isl_give isl_pw_aff *visit(const SCEV *Expr);
90 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
91 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
92 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
93 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
94 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
95 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
96 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
97 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
98 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
99 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
100 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000101 __isl_give isl_pw_aff *visitSDivInstruction(Instruction *SDiv);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000102
Tobias Grosser0695ee42013-09-17 03:30:31 +0000103 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
104};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000105
Tobias Grosser0695ee42013-09-17 03:30:31 +0000106SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
107 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
108 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000109
Tobias Grosser0695ee42013-09-17 03:30:31 +0000110__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
111 const SCEV *Scev) {
112 Scop *S = Stmt->getParent();
113 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000114
Tobias Grosser0695ee42013-09-17 03:30:31 +0000115 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000116
Tobias Grosser0695ee42013-09-17 03:30:31 +0000117 SCEVAffinator Affinator(Stmt);
118 return Affinator.visit(Scev);
119}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000120
Tobias Grosser0695ee42013-09-17 03:30:31 +0000121__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
122 // In case the scev is a valid parameter, we do not further analyze this
123 // expression, but create a new parameter in the isl_pw_aff. This allows us
124 // to treat subexpressions that we cannot translate into an piecewise affine
125 // expression, as constant parameters of the piecewise affine expression.
126 if (isl_id *Id = S->getIdForParam(Expr)) {
127 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
128 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000129
Tobias Grosser0695ee42013-09-17 03:30:31 +0000130 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
131 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
132 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000133
134 return isl_pw_aff_alloc(Domain, Affine);
135 }
136
Tobias Grosser0695ee42013-09-17 03:30:31 +0000137 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
138}
139
Tobias Grosser0d170132013-10-03 13:09:19 +0000140__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000141 ConstantInt *Value = Expr->getValue();
142 isl_val *v;
143
144 // LLVM does not define if an integer value is interpreted as a signed or
145 // unsigned value. Hence, without further information, it is unknown how
146 // this value needs to be converted to GMP. At the moment, we only support
147 // signed operations. So we just interpret it as signed. Later, there are
148 // two options:
149 //
150 // 1. We always interpret any value as signed and convert the values on
151 // demand.
152 // 2. We pass down the signedness of the calculation and use it to interpret
153 // this constant correctly.
154 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
155
156 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
Johannes Doerfert9c147372014-11-19 15:36:59 +0000157 isl_local_space *ls = isl_local_space_from_space(Space);
158 return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000159}
160
161__isl_give isl_pw_aff *
162SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
163 llvm_unreachable("SCEVTruncateExpr not yet supported");
164}
165
166__isl_give isl_pw_aff *
167SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
168 llvm_unreachable("SCEVZeroExtendExpr not yet supported");
169}
170
171__isl_give isl_pw_aff *
172SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
173 // Assuming the value is signed, a sign extension is basically a noop.
174 // TODO: Reconsider this as soon as we support unsigned values.
175 return visit(Expr->getOperand());
176}
177
178__isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
179 isl_pw_aff *Sum = visit(Expr->getOperand(0));
180
181 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
182 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
183 Sum = isl_pw_aff_add(Sum, NextSummand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000184 }
185
Tobias Grosser0695ee42013-09-17 03:30:31 +0000186 // TODO: Check for NSW and NUW.
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000187
Tobias Grosser0695ee42013-09-17 03:30:31 +0000188 return Sum;
189}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000190
Tobias Grosser0695ee42013-09-17 03:30:31 +0000191__isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
192 isl_pw_aff *Product = visit(Expr->getOperand(0));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000193
Tobias Grosser0695ee42013-09-17 03:30:31 +0000194 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
195 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
196
197 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
198 isl_pw_aff_free(Product);
199 isl_pw_aff_free(NextOperand);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000200 return nullptr;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000201 }
202
Tobias Grosser0695ee42013-09-17 03:30:31 +0000203 Product = isl_pw_aff_mul(Product, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000204 }
205
Tobias Grosser0695ee42013-09-17 03:30:31 +0000206 // TODO: Check for NSW and NUW.
207 return Product;
208}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000209
Tobias Grosser0695ee42013-09-17 03:30:31 +0000210__isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
211 llvm_unreachable("SCEVUDivExpr not yet supported");
212}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000213
Tobias Grosser0695ee42013-09-17 03:30:31 +0000214__isl_give isl_pw_aff *
215SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
216 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000217
Tobias Grosser0695ee42013-09-17 03:30:31 +0000218 // Directly generate isl_pw_aff for Expr if 'start' is zero.
219 if (Expr->getStart()->isZero()) {
220 assert(S->getRegion().contains(Expr->getLoop()) &&
221 "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000222
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000223 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser0695ee42013-09-17 03:30:31 +0000224 isl_pw_aff *Step = visit(Expr->getOperand(1));
225 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
226 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000227
Tobias Grosser0695ee42013-09-17 03:30:31 +0000228 int loopDimension = getLoopDepth(Expr->getLoop());
229
230 isl_aff *LAff = isl_aff_set_coefficient_si(
231 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
232 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
233
234 // TODO: Do we need to check for NSW and NUW?
235 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000236 }
237
Tobias Grosser0695ee42013-09-17 03:30:31 +0000238 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
239 // if 'start' is not zero.
240 ScalarEvolution &SE = *S->getSE();
241 const SCEV *ZeroStartExpr = SE.getAddRecExpr(
242 SE.getConstant(Expr->getStart()->getType(), 0),
243 Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000244
Tobias Grosser0695ee42013-09-17 03:30:31 +0000245 isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
246 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000247
Tobias Grosser0695ee42013-09-17 03:30:31 +0000248 return isl_pw_aff_add(ZeroStartResult, Start);
249}
250
251__isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
252 isl_pw_aff *Max = visit(Expr->getOperand(0));
253
254 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
255 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
256 Max = isl_pw_aff_max(Max, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000257 }
258
Tobias Grosser0695ee42013-09-17 03:30:31 +0000259 return Max;
260}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000261
Tobias Grosser0695ee42013-09-17 03:30:31 +0000262__isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
263 llvm_unreachable("SCEVUMaxExpr not yet supported");
264}
265
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000266__isl_give isl_pw_aff *SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
267 assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
268 auto *SE = S->getSE();
269
270 auto *Divisor = SDiv->getOperand(1);
271 auto *DivisorSCEV = SE->getSCEV(Divisor);
272 auto *DivisorPWA = visit(DivisorSCEV);
273 assert(isa<ConstantInt>(Divisor) &&
274 "SDiv is no parameter but has a non-constant RHS.");
275
276 auto *Dividend = SDiv->getOperand(0);
277 auto *DividendSCEV = SE->getSCEV(Dividend);
278 auto *DividendPWA = visit(DividendSCEV);
279 return isl_pw_aff_tdiv_q(DividendPWA, DivisorPWA);
280}
281
Tobias Grosser0695ee42013-09-17 03:30:31 +0000282__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
Johannes Doerfertb9d18882015-02-11 14:54:50 +0000283 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
284 switch (I->getOpcode()) {
285 case Instruction::SDiv:
286 return visitSDivInstruction(I);
287 default:
288 break; // Fall through.
289 }
290 }
291
292 llvm_unreachable(
293 "Unknowns SCEV was neither parameter nor a valid instruction.");
Tobias Grosser0695ee42013-09-17 03:30:31 +0000294}
295
296int SCEVAffinator::getLoopDepth(const Loop *L) {
297 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
298 assert(outerLoop && "Scop does not contain this loop");
299 return L->getLoopDepth() - outerLoop->getLoopDepth();
300}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000301
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000302ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *AccessType, isl_ctx *Ctx,
303 const SmallVector<const SCEV *, 4> &DimensionSizes)
304 : BasePtr(BasePtr), AccessType(AccessType), DimensionSizes(DimensionSizes) {
305 const std::string BasePtrName = getIslCompatibleName("MemRef_", BasePtr, "");
306 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
307}
308
309ScopArrayInfo::~ScopArrayInfo() { isl_id_free(Id); }
310
311isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
312
313void ScopArrayInfo::dump() const { print(errs()); }
314
315void ScopArrayInfo::print(raw_ostream &OS) const {
316 OS << "ScopArrayInfo:\n";
317 OS << " Base: " << *getBasePtr() << "\n";
318 OS << " Type: " << *getType() << "\n";
319 OS << " Dimension Sizes:\n";
320 for (unsigned u = 0; u < getNumberOfDimensions(); u++)
321 OS << " " << u << ") " << *DimensionSizes[u] << "\n";
322 OS << "\n";
323}
324
325const ScopArrayInfo *
326ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
327 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
328 assert(Id && "Output dimension didn't have an ID");
329 return getFromId(Id);
330}
331
332const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
333 void *User = isl_id_get_user(Id);
334 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
335 isl_id_free(Id);
336 return SAI;
337}
338
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000339const std::string
340MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
341 switch (RT) {
342 case MemoryAccess::RT_NONE:
343 llvm_unreachable("Requested a reduction operator string for a memory "
344 "access which isn't a reduction");
345 case MemoryAccess::RT_ADD:
346 return "+";
347 case MemoryAccess::RT_MUL:
348 return "*";
349 case MemoryAccess::RT_BOR:
350 return "|";
351 case MemoryAccess::RT_BXOR:
352 return "^";
353 case MemoryAccess::RT_BAND:
354 return "&";
355 }
356 llvm_unreachable("Unknown reduction type");
357 return "";
358}
359
Johannes Doerfertf6183392014-07-01 20:52:51 +0000360/// @brief Return the reduction type for a given binary operator
361static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
362 const Instruction *Load) {
363 if (!BinOp)
364 return MemoryAccess::RT_NONE;
365 switch (BinOp->getOpcode()) {
366 case Instruction::FAdd:
367 if (!BinOp->hasUnsafeAlgebra())
368 return MemoryAccess::RT_NONE;
369 // Fall through
370 case Instruction::Add:
371 return MemoryAccess::RT_ADD;
372 case Instruction::Or:
373 return MemoryAccess::RT_BOR;
374 case Instruction::Xor:
375 return MemoryAccess::RT_BXOR;
376 case Instruction::And:
377 return MemoryAccess::RT_BAND;
378 case Instruction::FMul:
379 if (!BinOp->hasUnsafeAlgebra())
380 return MemoryAccess::RT_NONE;
381 // Fall through
382 case Instruction::Mul:
383 if (DisableMultiplicativeReductions)
384 return MemoryAccess::RT_NONE;
385 return MemoryAccess::RT_MUL;
386 default:
387 return MemoryAccess::RT_NONE;
388 }
389}
Tobias Grosser75805372011-04-29 06:27:02 +0000390//===----------------------------------------------------------------------===//
391
392MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000393 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000394 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000395}
396
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000397static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
398 switch (Access.getType()) {
399 case IRAccess::READ:
400 return MemoryAccess::READ;
401 case IRAccess::MUST_WRITE:
402 return MemoryAccess::MUST_WRITE;
403 case IRAccess::MAY_WRITE:
404 return MemoryAccess::MAY_WRITE;
405 }
406 llvm_unreachable("Unknown IRAccess type!");
407}
408
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000409const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
410 isl_id *ArrayId = getArrayId();
411 void *User = isl_id_get_user(ArrayId);
412 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
413 isl_id_free(ArrayId);
414 return SAI;
415}
416
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000417isl_id *MemoryAccess::getArrayId() const {
418 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
419}
420
Johannes Doerferta99130f2014-10-13 12:58:03 +0000421isl_pw_multi_aff *
422MemoryAccess::applyScheduleToAccessRelation(isl_union_map *USchedule) const {
423 isl_map *Schedule, *ScheduledAccRel;
424 isl_union_set *UDomain;
425
426 UDomain = isl_union_set_from_set(getStatement()->getDomain());
427 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
428 Schedule = isl_map_from_union_map(USchedule);
429 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
430 return isl_pw_multi_aff_from_map(ScheduledAccRel);
431}
432
433isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000434 return isl_map_copy(AccessRelation);
435}
436
Johannes Doerferta99130f2014-10-13 12:58:03 +0000437std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000438 return stringFromIslObj(AccessRelation);
439}
440
Johannes Doerferta99130f2014-10-13 12:58:03 +0000441__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000442 return isl_map_get_space(AccessRelation);
443}
444
Tobias Grosser5d453812011-10-06 00:04:11 +0000445isl_map *MemoryAccess::getNewAccessRelation() const {
446 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000447}
448
449isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000450 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000451 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000452
Tobias Grosser084d8f72012-05-29 09:29:44 +0000453 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000454 isl_basic_set_universe(Statement->getDomainSpace()),
455 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000456}
457
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000458// Formalize no out-of-bound access assumption
459//
460// When delinearizing array accesses we optimistically assume that the
461// delinearized accesses do not access out of bound locations (the subscript
462// expression of each array evaluates for each statement instance that is
463// executed to a value that is larger than zero and strictly smaller than the
464// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000465// dimension for which we do not need to assume any upper bound. At this point
466// we formalize this assumption to ensure that at code generation time the
467// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000468//
469// To find the set of constraints necessary to avoid out of bound accesses, we
470// first build the set of data locations that are not within array bounds. We
471// then apply the reverse access relation to obtain the set of iterations that
472// may contain invalid accesses and reduce this set of iterations to the ones
473// that are actually executed by intersecting them with the domain of the
474// statement. If we now project out all loop dimensions, we obtain a set of
475// parameters that may cause statement instances to be executed that may
476// possibly yield out of bound memory accesses. The complement of these
477// constraints is the set of constraints that needs to be assumed to ensure such
478// statement instances are never executed.
479void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000480 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000481 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000482 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000483 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
484 isl_pw_aff *Var =
485 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
486 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
487
488 isl_set *DimOutside;
489
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000490 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
491 isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000492
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000493 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
494 Statement->getNumIterators());
495 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
496 isl_space_dim(Space, isl_dim_set));
497 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
498 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000499
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000500 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000501
502 Outside = isl_set_union(Outside, DimOutside);
503 }
504
505 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
506 Outside = isl_set_intersect(Outside, Statement->getDomain());
507 Outside = isl_set_params(Outside);
508 Outside = isl_set_complement(Outside);
509 Statement->getParent()->addAssumption(Outside);
510 isl_space_free(Space);
511}
512
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000513MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000514 ScopStmt *Statement, const ScopArrayInfo *SAI)
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000515 : AccType(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst),
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000516 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000517
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000518 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000519 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000520 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000521
522 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000523
Tobias Grossera1879642011-12-20 10:43:14 +0000524 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000525 // We overapproximate non-affine accesses with a possible access to the
526 // whole array. For read accesses it does not make a difference, if an
527 // access must or may happen. However, for write accesses it is important to
528 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000529 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000530 AccessRelation =
531 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Tobias Grossera1879642011-12-20 10:43:14 +0000532 return;
533 }
534
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000535 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000536 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000537
Tobias Grosser79baa212014-04-10 08:38:02 +0000538 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000539 isl_pw_aff *Affine =
540 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000541
Sebastian Pop422e33f2014-06-03 18:16:31 +0000542 if (Size == 1) {
543 // For the non delinearized arrays, divide the access function of the last
544 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000545 //
546 // A stride one array access in C expressed as A[i] is expressed in
547 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
548 // two subsequent values of 'i' index two values that are stored next to
549 // each other in memory. By this division we make this characteristic
550 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000551 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000552 Affine = isl_pw_aff_scale_down_val(Affine, v);
553 }
554
555 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
556
Tobias Grosser79baa212014-04-10 08:38:02 +0000557 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000558 }
559
Tobias Grosser79baa212014-04-10 08:38:02 +0000560 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000561 AccessRelation = isl_map_set_tuple_id(
562 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000563 AccessRelation =
564 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
565
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000566 assumeNoOutOfBound(Access);
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000567 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000568}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000569
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000570void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000571 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000572 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000573}
574
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000575const std::string MemoryAccess::getReductionOperatorStr() const {
576 return MemoryAccess::getReductionOperatorStr(getReductionType());
577}
578
Johannes Doerfertf6183392014-07-01 20:52:51 +0000579raw_ostream &polly::operator<<(raw_ostream &OS,
580 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000581 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000582 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000583 else
584 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000585 return OS;
586}
587
Tobias Grosser75805372011-04-29 06:27:02 +0000588void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000589 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000590 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000591 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000592 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000593 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000594 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000595 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000596 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000597 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000598 break;
599 }
Johannes Doerfert0ff23ec2015-02-06 20:13:15 +0000600 OS << "[Reduction Type: " << getReductionType() << "] ";
601 OS << "[Scalar: " << isScalar() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000602 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000603}
604
Tobias Grosser74394f02013-01-14 22:40:23 +0000605void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000606
607// Create a map in the size of the provided set domain, that maps from the
608// one element of the provided set domain to another element of the provided
609// set domain.
610// The mapping is limited to all points that are equal in all but the last
611// dimension and for which the last dimension of the input is strict smaller
612// than the last dimension of the output.
613//
614// getEqualAndLarger(set[i0, i1, ..., iX]):
615//
616// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
617// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
618//
Tobias Grosserf5338802011-10-06 00:03:35 +0000619static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000620 isl_space *Space = isl_space_map_from_set(setDomain);
621 isl_map *Map = isl_map_universe(isl_space_copy(Space));
622 isl_local_space *MapLocalSpace = isl_local_space_from_space(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000623 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000624
625 // Set all but the last dimension to be equal for the input and output
626 //
627 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
628 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000629 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000630 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000631
632 // Set the last dimension of the input to be strict smaller than the
633 // last dimension of the output.
634 //
635 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
636 //
Tobias Grosseredab1352013-06-21 06:41:31 +0000637 isl_val *v;
638 isl_ctx *Ctx = isl_map_get_ctx(Map);
Tobias Grosserf5338802011-10-06 00:03:35 +0000639 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosseredab1352013-06-21 06:41:31 +0000640 v = isl_val_int_from_si(Ctx, -1);
641 c = isl_constraint_set_coefficient_val(c, isl_dim_in, lastDimension, v);
642 v = isl_val_int_from_si(Ctx, 1);
643 c = isl_constraint_set_coefficient_val(c, isl_dim_out, lastDimension, v);
644 v = isl_val_int_from_si(Ctx, -1);
645 c = isl_constraint_set_constant_val(c, v);
Tobias Grosser75805372011-04-29 06:27:02 +0000646
Tobias Grosserc327932c2012-02-01 14:23:36 +0000647 Map = isl_map_add_constraint(Map, c);
Tobias Grosser75805372011-04-29 06:27:02 +0000648
Tobias Grosser23b36662011-10-17 08:32:36 +0000649 isl_local_space_free(MapLocalSpace);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000650 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000651}
652
Sebastian Popa00a0292012-12-18 07:46:06 +0000653isl_set *MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000654 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000655 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000656 isl_space *Space = isl_space_range(isl_map_get_space(S));
657 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000658
Sebastian Popa00a0292012-12-18 07:46:06 +0000659 S = isl_map_reverse(S);
660 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000661
Sebastian Popa00a0292012-12-18 07:46:06 +0000662 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
663 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
664 NextScatt = isl_map_apply_domain(NextScatt, S);
665 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000666
Sebastian Popa00a0292012-12-18 07:46:06 +0000667 isl_set *Deltas = isl_map_deltas(NextScatt);
668 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000669}
670
Sebastian Popa00a0292012-12-18 07:46:06 +0000671bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000672 int StrideWidth) const {
673 isl_set *Stride, *StrideX;
674 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000675
Sebastian Popa00a0292012-12-18 07:46:06 +0000676 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000677 StrideX = isl_set_universe(isl_set_get_space(Stride));
678 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
679 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000680
Tobias Grosser28dd4862012-01-24 16:42:16 +0000681 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000682 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000683
Tobias Grosser28dd4862012-01-24 16:42:16 +0000684 return IsStrideX;
685}
686
Sebastian Popa00a0292012-12-18 07:46:06 +0000687bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
688 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000689}
690
Tobias Grosser79baa212014-04-10 08:38:02 +0000691bool MemoryAccess::isScalar() const {
692 return isl_map_n_out(AccessRelation) == 0;
693}
694
Sebastian Popa00a0292012-12-18 07:46:06 +0000695bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
696 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000697}
698
Tobias Grosser5d453812011-10-06 00:04:11 +0000699void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000700 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000701 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000702}
Tobias Grosser75805372011-04-29 06:27:02 +0000703
704//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000705
Tobias Grosser74394f02013-01-14 22:40:23 +0000706isl_map *ScopStmt::getScattering() const { return isl_map_copy(Scattering); }
Tobias Grossercf3942d2011-10-06 00:04:05 +0000707
Tobias Grosser37eb4222014-02-20 21:43:54 +0000708void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
709 assert(isl_set_is_subset(NewDomain, Domain) &&
710 "New domain is not a subset of old domain!");
711 isl_set_free(Domain);
712 Domain = NewDomain;
713 Scattering = isl_map_intersect_domain(Scattering, isl_set_copy(Domain));
714}
715
Tobias Grossercf3942d2011-10-06 00:04:05 +0000716void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000717 assert(NewScattering && "New scattering is nullptr");
Tobias Grosserb76f38532011-08-20 11:11:25 +0000718 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000719 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000720}
721
Tobias Grosser75805372011-04-29 06:27:02 +0000722void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000723 unsigned NbIterators = getNumIterators();
724 unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1;
725
Tobias Grosser084d8f72012-05-29 09:29:44 +0000726 isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, NbScatteringDims);
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000727
Tobias Grosser084d8f72012-05-29 09:29:44 +0000728 Scattering = isl_map_from_domain_and_range(isl_set_universe(getDomainSpace()),
729 isl_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000730
731 // Loop dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000732 for (unsigned i = 0; i < NbIterators; ++i)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000733 Scattering =
734 isl_map_equate(Scattering, isl_dim_out, 2 * i + 1, isl_dim_in, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000735
736 // Constant dimensions
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000737 for (unsigned i = 0; i < NbIterators + 1; ++i)
738 Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000739
740 // Fill scattering dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000741 for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i)
742 Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000743
Tobias Grosser37487052011-10-06 00:03:42 +0000744 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000745}
746
Johannes Doerfert75bd66e2014-10-31 23:16:02 +0000747void ScopStmt::buildAccesses(TempScop &tempScop) {
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000748 for (const auto &AccessPair : *tempScop.getAccessFunctions(BB)) {
749 const IRAccess &Access = AccessPair.first;
750 Instruction *AccessInst = AccessPair.second;
751
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000752 Type *AccessType = getAccessInstType(AccessInst)->getPointerTo();
753 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
754 Access.getBase(), AccessType, Access.Sizes);
755
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000756 MemAccs.push_back(new MemoryAccess(Access, AccessInst, this, SAI));
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000757
758 // We do not track locations for scalar memory accesses at the moment.
759 //
760 // We do not have a use for this information at the moment. If we need this
761 // at some point, the "instruction -> access" mapping needs to be enhanced
762 // as a single instruction could then possibly perform multiple accesses.
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000763 if (!Access.isScalar()) {
764 assert(!InstructionToAccess.count(AccessInst) &&
Tobias Grosser3fc91542014-02-20 21:43:45 +0000765 "Unexpected 1-to-N mapping on instruction to access map!");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000766 InstructionToAccess[AccessInst] = MemAccs.back();
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000767 }
Tobias Grosser75805372011-04-29 06:27:02 +0000768 }
769}
770
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000771void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000772 for (MemoryAccess *MA : *this)
773 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000774
775 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
776 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
777}
778
Tobias Grosser65b00582011-11-08 15:41:19 +0000779__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000780 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
781 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000782
Tobias Grosserd2795d02011-08-18 07:51:40 +0000783 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000784 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000785 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000786 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000787 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000788 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000789 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000790 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000791 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000792 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000793 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000794 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000795 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000796 case ICmpInst::ICMP_ULT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000797 return isl_pw_aff_lt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000798 case ICmpInst::ICMP_UGT:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000799 return isl_pw_aff_gt_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000800 case ICmpInst::ICMP_ULE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000801 return isl_pw_aff_le_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000802 case ICmpInst::ICMP_UGE:
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000803 return isl_pw_aff_ge_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000804 default:
805 llvm_unreachable("Non integer predicate not supported");
806 }
Tobias Grosser75805372011-04-29 06:27:02 +0000807}
808
Tobias Grossere19661e2011-10-07 08:46:57 +0000809__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000810 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000811 isl_space *Space;
812 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000813
Tobias Grossere19661e2011-10-07 08:46:57 +0000814 Space = isl_set_get_space(Domain);
815 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000816
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000817 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000818 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000819 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000820 isl_pw_aff *IV =
821 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000822
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000823 // 0 <= IV.
824 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
825 Domain = isl_set_intersect(Domain, LowerBound);
826
827 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000828 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000829 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000830 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
831 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000832 Domain = isl_set_intersect(Domain, UpperBoundSet);
833 }
834
Tobias Grosserf5338802011-10-06 00:03:35 +0000835 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000836 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000837}
838
Tobias Grossere602a072013-05-07 07:30:56 +0000839__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
840 TempScop &tempScop,
841 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000842 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000843 *CurrentRegion = &CurRegion;
Tobias Grossere19661e2011-10-07 08:46:57 +0000844 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000845
Tobias Grosser75805372011-04-29 06:27:02 +0000846 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000847 if (BranchingBB != CurrentRegion->getEntry()) {
848 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000849 for (const auto &C : *Condition) {
850 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000851 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000852 }
853 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000854 BranchingBB = CurrentRegion->getEntry();
855 CurrentRegion = CurrentRegion->getParent();
856 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000857
Tobias Grossere19661e2011-10-07 08:46:57 +0000858 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000859}
860
Tobias Grossere602a072013-05-07 07:30:56 +0000861__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
862 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000863 isl_space *Space;
864 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000865 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000866
867 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
868
Tobias Grosser084d8f72012-05-29 09:29:44 +0000869 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
870
Tobias Grossere19661e2011-10-07 08:46:57 +0000871 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000872 Domain = addLoopBoundsToDomain(Domain, tempScop);
873 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000874 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +0000875
876 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000877}
878
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000879void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
880 int Dimension = 0;
881 isl_ctx *Ctx = Parent.getIslCtx();
882 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
883 Type *Ty = GEP->getPointerOperandType();
884 ScalarEvolution &SE = *Parent.getSE();
885
886 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
887 Dimension = 1;
888 Ty = PtrTy->getElementType();
889 }
890
891 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
892 unsigned int Operand = 1 + Dimension;
893
894 if (GEP->getNumOperands() <= Operand)
895 break;
896
897 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
898
899 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
900 isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr);
901 AccessOffset =
902 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
903
904 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
905 isl_local_space_copy(LSpace),
906 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
907
908 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
909 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
910 OutOfBound = isl_set_params(OutOfBound);
911 isl_set *InBound = isl_set_complement(OutOfBound);
912 isl_set *Executed = isl_set_params(getDomain());
913
914 // A => B == !A or B
915 isl_set *InBoundIfExecuted =
916 isl_set_union(isl_set_complement(Executed), InBound);
917
918 Parent.addAssumption(InBoundIfExecuted);
919 }
920
921 Dimension += 1;
922 Ty = ArrayTy->getElementType();
923 }
924
925 isl_local_space_free(LSpace);
926}
927
928void ScopStmt::deriveAssumptions() {
929 for (Instruction &Inst : *BB)
930 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
931 deriveAssumptionsFromGEP(GEP);
932}
933
Tobias Grosser74394f02013-01-14 22:40:23 +0000934ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Sebastian Pop860e0212013-02-15 21:26:44 +0000935 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest,
Tobias Grosser75805372011-04-29 06:27:02 +0000936 SmallVectorImpl<unsigned> &Scatter)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000937 : Parent(parent), BB(&bb), Build(nullptr), NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000938 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +0000939 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +0000940 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +0000941
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000942 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +0000943
Tobias Grossere19661e2011-10-07 08:46:57 +0000944 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000945 buildScattering(Scatter);
Johannes Doerfert75bd66e2014-10-31 23:16:02 +0000946 buildAccesses(tempScop);
Johannes Doerferte58a0122014-06-27 20:31:28 +0000947 checkForReductions();
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000948 deriveAssumptions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000949}
950
Johannes Doerferte58a0122014-06-27 20:31:28 +0000951/// @brief Collect loads which might form a reduction chain with @p StoreMA
952///
953/// Check if the stored value for @p StoreMA is a binary operator with one or
954/// two loads as operands. If the binary operand is commutative & associative,
955/// used only once (by @p StoreMA) and its load operands are also used only
956/// once, we have found a possible reduction chain. It starts at an operand
957/// load and includes the binary operator and @p StoreMA.
958///
959/// Note: We allow only one use to ensure the load and binary operator cannot
960/// escape this block or into any other store except @p StoreMA.
961void ScopStmt::collectCandiateReductionLoads(
962 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
963 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
964 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000965 return;
966
967 // Skip if there is not one binary operator between the load and the store
968 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +0000969 if (!BinOp)
970 return;
971
972 // Skip if the binary operators has multiple uses
973 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000974 return;
975
976 // Skip if the opcode of the binary operator is not commutative/associative
977 if (!BinOp->isCommutative() || !BinOp->isAssociative())
978 return;
979
Johannes Doerfert9890a052014-07-01 00:32:29 +0000980 // Skip if the binary operator is outside the current SCoP
981 if (BinOp->getParent() != Store->getParent())
982 return;
983
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000984 // Skip if it is a multiplicative reduction and we disabled them
985 if (DisableMultiplicativeReductions &&
986 (BinOp->getOpcode() == Instruction::Mul ||
987 BinOp->getOpcode() == Instruction::FMul))
988 return;
989
Johannes Doerferte58a0122014-06-27 20:31:28 +0000990 // Check the binary operator operands for a candidate load
991 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
992 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
993 if (!PossibleLoad0 && !PossibleLoad1)
994 return;
995
996 // A load is only a candidate if it cannot escape (thus has only this use)
997 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000998 if (PossibleLoad0->getParent() == Store->getParent())
999 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001000 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001001 if (PossibleLoad1->getParent() == Store->getParent())
1002 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001003}
1004
1005/// @brief Check for reductions in this ScopStmt
1006///
1007/// Iterate over all store memory accesses and check for valid binary reduction
1008/// like chains. For all candidates we check if they have the same base address
1009/// and there are no other accesses which overlap with them. The base address
1010/// check rules out impossible reductions candidates early. The overlap check,
1011/// together with the "only one user" check in collectCandiateReductionLoads,
1012/// guarantees that none of the intermediate results will escape during
1013/// execution of the loop nest. We basically check here that no other memory
1014/// access can access the same memory as the potential reduction.
1015void ScopStmt::checkForReductions() {
1016 SmallVector<MemoryAccess *, 2> Loads;
1017 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1018
1019 // First collect candidate load-store reduction chains by iterating over all
1020 // stores and collecting possible reduction loads.
1021 for (MemoryAccess *StoreMA : MemAccs) {
1022 if (StoreMA->isRead())
1023 continue;
1024
1025 Loads.clear();
1026 collectCandiateReductionLoads(StoreMA, Loads);
1027 for (MemoryAccess *LoadMA : Loads)
1028 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1029 }
1030
1031 // Then check each possible candidate pair.
1032 for (const auto &CandidatePair : Candidates) {
1033 bool Valid = true;
1034 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1035 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1036
1037 // Skip those with obviously unequal base addresses.
1038 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1039 isl_map_free(LoadAccs);
1040 isl_map_free(StoreAccs);
1041 continue;
1042 }
1043
1044 // And check if the remaining for overlap with other memory accesses.
1045 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1046 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1047 isl_set *AllAccs = isl_map_range(AllAccsRel);
1048
1049 for (MemoryAccess *MA : MemAccs) {
1050 if (MA == CandidatePair.first || MA == CandidatePair.second)
1051 continue;
1052
1053 isl_map *AccRel =
1054 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1055 isl_set *Accs = isl_map_range(AccRel);
1056
1057 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1058 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1059 Valid = Valid && isl_set_is_empty(OverlapAccs);
1060 isl_set_free(OverlapAccs);
1061 }
1062 }
1063
1064 isl_set_free(AllAccs);
1065 if (!Valid)
1066 continue;
1067
Johannes Doerfertf6183392014-07-01 20:52:51 +00001068 const LoadInst *Load =
1069 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1070 MemoryAccess::ReductionType RT =
1071 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1072
Johannes Doerferte58a0122014-06-27 20:31:28 +00001073 // If no overlapping access was found we mark the load and store as
1074 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001075 CandidatePair.first->markAsReductionLike(RT);
1076 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001077 }
Tobias Grosser75805372011-04-29 06:27:02 +00001078}
1079
Tobias Grosser74394f02013-01-14 22:40:23 +00001080std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001081
1082std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +00001083 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +00001084}
1085
Tobias Grosser74394f02013-01-14 22:40:23 +00001086unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001087
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001088unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001089
1090unsigned ScopStmt::getNumScattering() const {
1091 return isl_map_dim(Scattering, isl_dim_out);
1092}
1093
1094const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1095
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001096const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001097 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001098}
1099
Tobias Grosser74394f02013-01-14 22:40:23 +00001100isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001101
Tobias Grosser74394f02013-01-14 22:40:23 +00001102isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001103
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001104isl_space *ScopStmt::getDomainSpace() const {
1105 return isl_set_get_space(Domain);
1106}
1107
Tobias Grosser74394f02013-01-14 22:40:23 +00001108isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); }
Tobias Grossercd95b772012-08-30 11:49:38 +00001109
Tobias Grosser75805372011-04-29 06:27:02 +00001110ScopStmt::~ScopStmt() {
1111 while (!MemAccs.empty()) {
1112 delete MemAccs.back();
1113 MemAccs.pop_back();
1114 }
1115
1116 isl_set_free(Domain);
1117 isl_map_free(Scattering);
1118}
1119
1120void ScopStmt::print(raw_ostream &OS) const {
1121 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001122 OS.indent(12) << "Domain :=\n";
1123
1124 if (Domain) {
1125 OS.indent(16) << getDomainStr() << ";\n";
1126 } else
1127 OS.indent(16) << "n/a\n";
1128
1129 OS.indent(12) << "Scattering :=\n";
1130
1131 if (Domain) {
1132 OS.indent(16) << getScatteringStr() << ";\n";
1133 } else
1134 OS.indent(16) << "n/a\n";
1135
Tobias Grosser083d3d32014-06-28 08:59:45 +00001136 for (MemoryAccess *Access : MemAccs)
1137 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001138}
1139
1140void ScopStmt::dump() const { print(dbgs()); }
1141
1142//===----------------------------------------------------------------------===//
1143/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001144
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001145void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001146 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1147 isl_set_free(Context);
1148 Context = NewContext;
1149}
1150
Tobias Grosserabfbe632013-02-05 12:09:06 +00001151void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001152 for (const SCEV *Parameter : NewParameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001153 if (ParameterIds.find(Parameter) != ParameterIds.end())
1154 continue;
1155
1156 int dimension = Parameters.size();
1157
1158 Parameters.push_back(Parameter);
1159 ParameterIds[Parameter] = dimension;
1160 }
1161}
1162
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001163__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1164 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001165
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001166 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001167 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001168
Tobias Grosser8f99c162011-11-15 11:38:55 +00001169 std::string ParameterName;
1170
1171 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1172 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001173 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001174 }
1175
1176 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001177 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001178
Tobias Grosser20532b82014-04-11 17:56:49 +00001179 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1180 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001181}
Tobias Grosser75805372011-04-29 06:27:02 +00001182
Tobias Grosser6be480c2011-11-08 15:41:13 +00001183void Scop::buildContext() {
1184 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001185 Context = isl_set_universe(isl_space_copy(Space));
1186 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001187}
1188
Tobias Grosser18daaca2012-05-22 10:47:27 +00001189void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001190 for (const auto &ParamID : ParameterIds) {
Tobias Grosseredab1352013-06-21 06:41:31 +00001191 isl_val *V;
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001192 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001193
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001194 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001195
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001196 // TODO: Find a case where the full set is actually helpful.
1197 if (SRange.isFullSet())
Tobias Grosser55bc4c02015-01-08 19:26:53 +00001198 continue;
1199
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001200 V = isl_valFromAPInt(IslCtx, SRange.getLower(), true);
1201 isl_set *ContextLB =
1202 isl_set_lower_bound_val(isl_set_copy(Context), isl_dim_param, dim, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001203
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001204 V = isl_valFromAPInt(IslCtx, SRange.getUpper(), true);
Tobias Grosseredab1352013-06-21 06:41:31 +00001205 V = isl_val_sub_ui(V, 1);
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001206 isl_set *ContextUB =
1207 isl_set_upper_bound_val(Context, isl_dim_param, dim, V);
1208
1209 if (SRange.isSignWrappedSet())
1210 Context = isl_set_union(ContextLB, ContextUB);
1211 else
1212 Context = isl_set_intersect(ContextLB, ContextUB);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001213 }
1214}
1215
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001216void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001217 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001218 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001219
Tobias Grosser083d3d32014-06-28 08:59:45 +00001220 for (const auto &ParamID : ParameterIds) {
1221 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001222 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001223 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001224 }
1225
1226 // Align the parameters of all data structures to the model.
1227 Context = isl_set_align_params(Context, Space);
1228
Tobias Grosser083d3d32014-06-28 08:59:45 +00001229 for (ScopStmt *Stmt : *this)
1230 Stmt->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001231}
1232
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001233void Scop::simplifyAssumedContext() {
1234 // The parameter constraints of the iteration domains give us a set of
1235 // constraints that need to hold for all cases where at least a single
1236 // statement iteration is executed in the whole scop. We now simplify the
1237 // assumed context under the assumption that such constraints hold and at
1238 // least a single statement iteration is executed. For cases where no
1239 // statement instances are executed, the assumptions we have taken about
1240 // the executed code do not matter and can be changed.
1241 //
1242 // WARNING: This only holds if the assumptions we have taken do not reduce
1243 // the set of statement instances that are executed. Otherwise we
1244 // may run into a case where the iteration domains suggest that
1245 // for a certain set of parameter constraints no code is executed,
1246 // but in the original program some computation would have been
1247 // performed. In such a case, modifying the run-time conditions and
1248 // possibly influencing the run-time check may cause certain scops
1249 // to not be executed.
1250 //
1251 // Example:
1252 //
1253 // When delinearizing the following code:
1254 //
1255 // for (long i = 0; i < 100; i++)
1256 // for (long j = 0; j < m; j++)
1257 // A[i+p][j] = 1.0;
1258 //
1259 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
1260 // otherwise we would access out of bound data. Now, knowing that code is
1261 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1262 AssumedContext =
1263 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001264 AssumedContext = isl_set_gist_params(AssumedContext, getContext());
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001265}
1266
Johannes Doerfertb164c792014-09-18 11:17:17 +00001267/// @brief Add the minimal/maximal access in @p Set to @p User.
1268static int buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
1269 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1270 isl_pw_multi_aff *MinPMA, *MaxPMA;
1271 isl_pw_aff *LastDimAff;
1272 isl_aff *OneAff;
1273 unsigned Pos;
1274
Johannes Doerfert9143d672014-09-27 11:02:39 +00001275 // Restrict the number of parameters involved in the access as the lexmin/
1276 // lexmax computation will take too long if this number is high.
1277 //
1278 // Experiments with a simple test case using an i7 4800MQ:
1279 //
1280 // #Parameters involved | Time (in sec)
1281 // 6 | 0.01
1282 // 7 | 0.04
1283 // 8 | 0.12
1284 // 9 | 0.40
1285 // 10 | 1.54
1286 // 11 | 6.78
1287 // 12 | 30.38
1288 //
1289 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1290 unsigned InvolvedParams = 0;
1291 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1292 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1293 InvolvedParams++;
1294
1295 if (InvolvedParams > RunTimeChecksMaxParameters) {
1296 isl_set_free(Set);
1297 return -1;
1298 }
1299 }
1300
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001301 Set = isl_set_remove_divs(Set);
1302
Johannes Doerfertb164c792014-09-18 11:17:17 +00001303 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1304 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1305
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001306 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1307 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1308
Johannes Doerfertb164c792014-09-18 11:17:17 +00001309 // Adjust the last dimension of the maximal access by one as we want to
1310 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1311 // we test during code generation might now point after the end of the
1312 // allocated array but we will never dereference it anyway.
1313 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1314 "Assumed at least one output dimension");
1315 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1316 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1317 OneAff = isl_aff_zero_on_domain(
1318 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1319 OneAff = isl_aff_add_constant_si(OneAff, 1);
1320 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1321 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1322
1323 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1324
1325 isl_set_free(Set);
1326 return 0;
1327}
1328
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001329static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1330 isl_set *Domain = MA->getStatement()->getDomain();
1331 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1332 return isl_set_reset_tuple_id(Domain);
1333}
1334
Johannes Doerfert9143d672014-09-27 11:02:39 +00001335bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001336 // To create sound alias checks we perform the following steps:
1337 // o) Use the alias analysis and an alias set tracker to build alias sets
1338 // for all memory accesses inside the SCoP.
1339 // o) For each alias set we then map the aliasing pointers back to the
1340 // memory accesses we know, thus obtain groups of memory accesses which
1341 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001342 // o) We divide each group based on the domains of the minimal/maximal
1343 // accesses. That means two minimal/maximal accesses are only in a group
1344 // if their access domains intersect, otherwise they are in different
1345 // ones.
Johannes Doerfert13771732014-10-01 12:40:46 +00001346 // o) We split groups such that they contain at most one read only base
1347 // address.
1348 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfertb164c792014-09-18 11:17:17 +00001349 // and maximal accesses to each array in this group.
1350 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1351
1352 AliasSetTracker AST(AA);
1353
1354 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001355 DenseSet<Value *> HasWriteAccess;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001356 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001357
1358 // Skip statements with an empty domain as they will never be executed.
1359 isl_set *StmtDomain = Stmt->getDomain();
1360 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1361 isl_set_free(StmtDomain);
1362 if (StmtDomainEmpty)
1363 continue;
1364
Johannes Doerfertb164c792014-09-18 11:17:17 +00001365 for (MemoryAccess *MA : *Stmt) {
1366 if (MA->isScalar())
1367 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001368 if (!MA->isRead())
1369 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001370 Instruction *Acc = MA->getAccessInstruction();
1371 PtrToAcc[getPointerOperand(*Acc)] = MA;
1372 AST.add(Acc);
1373 }
1374 }
1375
1376 SmallVector<AliasGroupTy, 4> AliasGroups;
1377 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001378 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001379 continue;
1380 AliasGroupTy AG;
1381 for (auto PR : AS)
1382 AG.push_back(PtrToAcc[PR.getValue()]);
1383 assert(AG.size() > 1 &&
1384 "Alias groups should contain at least two accesses");
1385 AliasGroups.push_back(std::move(AG));
1386 }
1387
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001388 // Split the alias groups based on their domain.
1389 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1390 AliasGroupTy NewAG;
1391 AliasGroupTy &AG = AliasGroups[u];
1392 AliasGroupTy::iterator AGI = AG.begin();
1393 isl_set *AGDomain = getAccessDomain(*AGI);
1394 while (AGI != AG.end()) {
1395 MemoryAccess *MA = *AGI;
1396 isl_set *MADomain = getAccessDomain(MA);
1397 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1398 NewAG.push_back(MA);
1399 AGI = AG.erase(AGI);
1400 isl_set_free(MADomain);
1401 } else {
1402 AGDomain = isl_set_union(AGDomain, MADomain);
1403 AGI++;
1404 }
1405 }
1406 if (NewAG.size() > 1)
1407 AliasGroups.push_back(std::move(NewAG));
1408 isl_set_free(AGDomain);
1409 }
1410
Johannes Doerfert13771732014-10-01 12:40:46 +00001411 DenseMap<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
1412 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1413 for (AliasGroupTy &AG : AliasGroups) {
1414 NonReadOnlyBaseValues.clear();
1415 ReadOnlyPairs.clear();
1416
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001417 if (AG.size() < 2) {
1418 AG.clear();
1419 continue;
1420 }
1421
Johannes Doerfert13771732014-10-01 12:40:46 +00001422 for (auto II = AG.begin(); II != AG.end();) {
1423 Value *BaseAddr = (*II)->getBaseAddr();
1424 if (HasWriteAccess.count(BaseAddr)) {
1425 NonReadOnlyBaseValues.insert(BaseAddr);
1426 II++;
1427 } else {
1428 ReadOnlyPairs[BaseAddr].insert(*II);
1429 II = AG.erase(II);
1430 }
1431 }
1432
1433 // If we don't have read only pointers check if there are at least two
1434 // non read only pointers, otherwise clear the alias group.
1435 if (ReadOnlyPairs.empty()) {
1436 if (NonReadOnlyBaseValues.size() <= 1)
1437 AG.clear();
1438 continue;
1439 }
1440
1441 // If we don't have non read only pointers clear the alias group.
1442 if (NonReadOnlyBaseValues.empty()) {
1443 AG.clear();
1444 continue;
1445 }
1446
1447 // If we have both read only and non read only base pointers we combine
1448 // the non read only ones with exactly one read only one at a time into a
1449 // new alias group and clear the old alias group in the end.
1450 for (const auto &ReadOnlyPair : ReadOnlyPairs) {
1451 AliasGroupTy AGNonReadOnly = AG;
1452 for (MemoryAccess *MA : ReadOnlyPair.second)
1453 AGNonReadOnly.push_back(MA);
1454 AliasGroups.push_back(std::move(AGNonReadOnly));
1455 }
1456 AG.clear();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001457 }
1458
Johannes Doerfert9143d672014-09-27 11:02:39 +00001459 bool Valid = true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001460 for (AliasGroupTy &AG : AliasGroups) {
Johannes Doerfert13771732014-10-01 12:40:46 +00001461 if (AG.empty())
1462 continue;
1463
Johannes Doerfertb164c792014-09-18 11:17:17 +00001464 MinMaxVectorTy *MinMaxAccesses = new MinMaxVectorTy();
1465 MinMaxAccesses->reserve(AG.size());
1466
1467 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
1468 for (MemoryAccess *MA : AG)
1469 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1470 Accesses = isl_union_map_intersect_domain(Accesses, getDomains());
1471
1472 isl_union_set *Locations = isl_union_map_range(Accesses);
1473 Locations = isl_union_set_intersect_params(Locations, getAssumedContext());
1474 Locations = isl_union_set_coalesce(Locations);
1475 Locations = isl_union_set_detect_equalities(Locations);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001476 Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
1477 MinMaxAccesses));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001478 isl_union_set_free(Locations);
Johannes Doerfertb164c792014-09-18 11:17:17 +00001479 MinMaxAliasGroups.push_back(MinMaxAccesses);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001480
1481 if (!Valid)
1482 break;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001483 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001484
1485 return Valid;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001486}
1487
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001488static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI) {
1489 unsigned MinLD = INT_MAX, MaxLD = 0;
1490 for (BasicBlock *BB : R.blocks()) {
1491 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00001492 if (!R.contains(L))
1493 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001494 unsigned LD = L->getLoopDepth();
1495 MinLD = std::min(MinLD, LD);
1496 MaxLD = std::max(MaxLD, LD);
1497 }
1498 }
1499
1500 // Handle the case that there is no loop in the SCoP first.
1501 if (MaxLD == 0)
1502 return 1;
1503
1504 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1505 assert(MaxLD >= MinLD &&
1506 "Maximal loop depth was smaller than mininaml loop depth?");
1507 return MaxLD - MinLD + 1;
1508}
1509
Tobias Grosser3f296192015-01-01 23:01:11 +00001510void Scop::dropConstantScheduleDims() {
1511 isl_union_map *FullSchedule = getSchedule();
1512
1513 if (isl_union_map_n_map(FullSchedule) == 0) {
1514 isl_union_map_free(FullSchedule);
1515 return;
1516 }
1517
1518 isl_set *ScheduleSpace =
1519 isl_set_from_union_set(isl_union_map_range(FullSchedule));
1520 isl_map *DropDimMap = isl_set_identity(isl_set_copy(ScheduleSpace));
1521
1522 int NumDimsDropped = 0;
1523 for (unsigned i = 0; i < isl_set_dim(ScheduleSpace, isl_dim_set); i++)
1524 if (i % 2 == 0) {
1525 isl_val *FixedVal =
1526 isl_set_plain_get_val_if_fixed(ScheduleSpace, isl_dim_set, i);
1527 if (isl_val_is_int(FixedVal)) {
1528 DropDimMap =
1529 isl_map_project_out(DropDimMap, isl_dim_out, i - NumDimsDropped, 1);
1530 NumDimsDropped++;
1531 }
1532 isl_val_free(FixedVal);
1533 }
1534
Tobias Grosser3f296192015-01-01 23:01:11 +00001535 for (auto *S : *this) {
1536 isl_map *Schedule = S->getScattering();
1537 Schedule = isl_map_apply_range(Schedule, isl_map_copy(DropDimMap));
1538 S->setScattering(Schedule);
1539 }
1540 isl_set_free(ScheduleSpace);
1541 isl_map_free(DropDimMap);
1542}
1543
Tobias Grosser0e27e242011-10-06 00:03:48 +00001544Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
1545 isl_ctx *Context)
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001546 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()), IsOptimized(false),
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001547 MaxLoopDepth(getMaxLoopDepthInRegion(tempScop.getMaxRegion(), LI)) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001548 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001549 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001550
Tobias Grosserabfbe632013-02-05 12:09:06 +00001551 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001552 SmallVector<unsigned, 8> Scatter;
1553
1554 Scatter.assign(MaxLoopDepth + 1, 0);
1555
1556 // Build the iteration domain, access functions and scattering functions
1557 // traversing the region tree.
1558 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +00001559
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001560 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001561 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001562 simplifyAssumedContext();
Tobias Grosser3f296192015-01-01 23:01:11 +00001563 dropConstantScheduleDims();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001564
Tobias Grosser75805372011-04-29 06:27:02 +00001565 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1566}
1567
1568Scop::~Scop() {
1569 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001570 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +00001571
1572 // Free the statements;
Tobias Grosser083d3d32014-06-28 08:59:45 +00001573 for (ScopStmt *Stmt : *this)
1574 delete Stmt;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001575
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001576 // Free the ScopArrayInfo objects.
1577 for (auto &ScopArrayInfoPair : ScopArrayInfoMap)
1578 delete ScopArrayInfoPair.second;
1579
Johannes Doerfertb164c792014-09-18 11:17:17 +00001580 // Free the alias groups
1581 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1582 for (MinMaxAccessTy &MMA : *MinMaxAccesses) {
1583 isl_pw_multi_aff_free(MMA.first);
1584 isl_pw_multi_aff_free(MMA.second);
1585 }
1586 delete MinMaxAccesses;
1587 }
Tobias Grosser75805372011-04-29 06:27:02 +00001588}
1589
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001590const ScopArrayInfo *
1591Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
1592 const SmallVector<const SCEV *, 4> &Sizes) {
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001593 const ScopArrayInfo *&SAI = ScopArrayInfoMap[BasePtr];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001594 if (!SAI)
1595 SAI = new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes);
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001596 return SAI;
1597}
1598
1599const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr) {
1600 const SCEV *PtrSCEV = SE->getSCEV(BasePtr);
1601 const SCEVUnknown *PtrBaseSCEV =
1602 cast<SCEVUnknown>(SE->getPointerBase(PtrSCEV));
1603 const ScopArrayInfo *SAI = ScopArrayInfoMap[PtrBaseSCEV->getValue()];
1604 assert(SAI && "No ScopArrayInfo available for this base pointer");
1605 return SAI;
1606}
1607
Tobias Grosser74394f02013-01-14 22:40:23 +00001608std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001609std::string Scop::getAssumedContextStr() const {
1610 return stringFromIslObj(AssumedContext);
1611}
Tobias Grosser75805372011-04-29 06:27:02 +00001612
1613std::string Scop::getNameStr() const {
1614 std::string ExitName, EntryName;
1615 raw_string_ostream ExitStr(ExitName);
1616 raw_string_ostream EntryStr(EntryName);
1617
Tobias Grosserf240b482014-01-09 10:42:15 +00001618 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001619 EntryStr.str();
1620
1621 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001622 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001623 ExitStr.str();
1624 } else
1625 ExitName = "FunctionExit";
1626
1627 return EntryName + "---" + ExitName;
1628}
1629
Tobias Grosser74394f02013-01-14 22:40:23 +00001630__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001631__isl_give isl_space *Scop::getParamSpace() const {
1632 return isl_set_get_space(this->Context);
1633}
1634
Tobias Grossere86109f2013-10-29 21:05:49 +00001635__isl_give isl_set *Scop::getAssumedContext() const {
1636 return isl_set_copy(AssumedContext);
1637}
1638
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001639void Scop::addAssumption(__isl_take isl_set *Set) {
1640 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001641 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001642}
1643
Tobias Grosser75805372011-04-29 06:27:02 +00001644void Scop::printContext(raw_ostream &OS) const {
1645 OS << "Context:\n";
1646
1647 if (!Context) {
1648 OS.indent(4) << "n/a\n\n";
1649 return;
1650 }
1651
1652 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001653
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001654 OS.indent(4) << "Assumed Context:\n";
1655 if (!AssumedContext) {
1656 OS.indent(4) << "n/a\n\n";
1657 return;
1658 }
1659
1660 OS.indent(4) << getAssumedContextStr() << "\n";
1661
Tobias Grosser083d3d32014-06-28 08:59:45 +00001662 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001663 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001664 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1665 }
Tobias Grosser75805372011-04-29 06:27:02 +00001666}
1667
Johannes Doerfertb164c792014-09-18 11:17:17 +00001668void Scop::printAliasAssumptions(raw_ostream &OS) const {
1669 OS.indent(4) << "Alias Groups (" << MinMaxAliasGroups.size() << "):\n";
1670 if (MinMaxAliasGroups.empty()) {
1671 OS.indent(8) << "n/a\n";
1672 return;
1673 }
1674 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1675 OS.indent(8) << "[[";
1676 for (MinMaxAccessTy &MinMacAccess : *MinMaxAccesses)
1677 OS << " <" << MinMacAccess.first << ", " << MinMacAccess.second << ">";
1678 OS << " ]]\n";
1679 }
1680}
1681
Tobias Grosser75805372011-04-29 06:27:02 +00001682void Scop::printStatements(raw_ostream &OS) const {
1683 OS << "Statements {\n";
1684
Tobias Grosser083d3d32014-06-28 08:59:45 +00001685 for (ScopStmt *Stmt : *this)
1686 OS.indent(4) << *Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001687
1688 OS.indent(4) << "}\n";
1689}
1690
Tobias Grosser75805372011-04-29 06:27:02 +00001691void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001692 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1693 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001694 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00001695 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001696 printContext(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001697 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001698 printStatements(OS.indent(4));
1699}
1700
1701void Scop::dump() const { print(dbgs()); }
1702
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001703isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001704
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001705__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001706 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001707
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001708 for (ScopStmt *Stmt : *this)
1709 Domain = isl_union_set_add_set(Domain, Stmt->getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001710
1711 return Domain;
1712}
1713
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001714__isl_give isl_union_map *Scop::getMustWrites() {
1715 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1716
1717 for (ScopStmt *Stmt : *this) {
1718 for (MemoryAccess *MA : *Stmt) {
1719 if (!MA->isMustWrite())
1720 continue;
1721
1722 isl_set *Domain = Stmt->getDomain();
1723 isl_map *AccessDomain = MA->getAccessRelation();
1724 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1725 Write = isl_union_map_add_map(Write, AccessDomain);
1726 }
1727 }
1728 return isl_union_map_coalesce(Write);
1729}
1730
1731__isl_give isl_union_map *Scop::getMayWrites() {
1732 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1733
1734 for (ScopStmt *Stmt : *this) {
1735 for (MemoryAccess *MA : *Stmt) {
1736 if (!MA->isMayWrite())
1737 continue;
1738
1739 isl_set *Domain = Stmt->getDomain();
1740 isl_map *AccessDomain = MA->getAccessRelation();
1741 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1742 Write = isl_union_map_add_map(Write, AccessDomain);
1743 }
1744 }
1745 return isl_union_map_coalesce(Write);
1746}
1747
Tobias Grosser37eb4222014-02-20 21:43:54 +00001748__isl_give isl_union_map *Scop::getWrites() {
1749 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1750
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001751 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001752 for (MemoryAccess *MA : *Stmt) {
1753 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001754 continue;
1755
1756 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001757 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001758 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1759 Write = isl_union_map_add_map(Write, AccessDomain);
1760 }
1761 }
1762 return isl_union_map_coalesce(Write);
1763}
1764
1765__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001766 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001767
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001768 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001769 for (MemoryAccess *MA : *Stmt) {
1770 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001771 continue;
1772
1773 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001774 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001775
1776 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1777 Read = isl_union_map_add_map(Read, AccessDomain);
1778 }
1779 }
1780 return isl_union_map_coalesce(Read);
1781}
1782
1783__isl_give isl_union_map *Scop::getSchedule() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001784 isl_union_map *Schedule = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001785
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001786 for (ScopStmt *Stmt : *this)
Tobias Grosser37eb4222014-02-20 21:43:54 +00001787 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001788
Tobias Grosser37eb4222014-02-20 21:43:54 +00001789 return isl_union_map_coalesce(Schedule);
1790}
1791
1792bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1793 bool Changed = false;
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001794 for (ScopStmt *Stmt : *this) {
Tobias Grosser37eb4222014-02-20 21:43:54 +00001795 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001796 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1797 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1798
1799 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1800 isl_union_set_free(StmtDomain);
1801 isl_union_set_free(NewStmtDomain);
1802 continue;
1803 }
1804
1805 Changed = true;
1806
1807 isl_union_set_free(StmtDomain);
1808 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1809
1810 if (isl_union_set_is_empty(NewStmtDomain)) {
1811 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1812 isl_union_set_free(NewStmtDomain);
1813 } else
1814 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1815 }
1816 isl_union_set_free(Domain);
1817 return Changed;
1818}
1819
Tobias Grosser75805372011-04-29 06:27:02 +00001820ScalarEvolution *Scop::getSE() const { return SE; }
1821
1822bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1823 if (tempScop.getAccessFunctions(BB))
1824 return false;
1825
1826 return true;
1827}
1828
Tobias Grosser74394f02013-01-14 22:40:23 +00001829void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1830 SmallVectorImpl<Loop *> &NestLoops,
1831 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001832 Loop *L = castToLoop(CurRegion, LI);
1833
1834 if (L)
1835 NestLoops.push_back(L);
1836
1837 unsigned loopDepth = NestLoops.size();
1838 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1839
1840 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001841 E = CurRegion.element_end();
1842 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001843 if (I->isSubRegion())
1844 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1845 else {
1846 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1847
1848 if (isTrivialBB(BB, tempScop))
1849 continue;
1850
Johannes Doerfert7c494212014-10-31 23:13:39 +00001851 ScopStmt *Stmt =
1852 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter);
1853
1854 // Insert all statements into the statement map and the statement vector.
1855 StmtMap[BB] = Stmt;
1856 Stmts.push_back(Stmt);
Tobias Grosser75805372011-04-29 06:27:02 +00001857
1858 // Increasing the Scattering function is OK for the moment, because
1859 // we are using a depth first iterator and the program is well structured.
1860 ++Scatter[loopDepth];
1861 }
1862
1863 if (!L)
1864 return;
1865
1866 // Exiting a loop region.
1867 Scatter[loopDepth] = 0;
1868 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001869 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001870}
1871
Johannes Doerfert7c494212014-10-31 23:13:39 +00001872ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
1873 const auto &StmtMapIt = StmtMap.find(BB);
1874 if (StmtMapIt == StmtMap.end())
1875 return nullptr;
1876 return StmtMapIt->second;
1877}
1878
Tobias Grosser75805372011-04-29 06:27:02 +00001879//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001880ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1881 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001882 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001883}
1884
1885ScopInfo::~ScopInfo() {
1886 clear();
1887 isl_ctx_free(ctx);
1888}
1889
Tobias Grosser75805372011-04-29 06:27:02 +00001890void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00001891 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001892 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00001893 AU.addRequired<ScalarEvolution>();
1894 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001895 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001896 AU.setPreservesAll();
1897}
1898
1899bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Chandler Carruthf5579872015-01-17 14:16:56 +00001900 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001901 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001902 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1903
1904 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1905
1906 // This region is no Scop.
1907 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00001908 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00001909 return false;
1910 }
1911
Tobias Grosserb76f38532011-08-20 11:11:25 +00001912 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001913
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001914 if (!PollyUseRuntimeAliasChecks) {
1915 // Statistics.
1916 ++ScopFound;
1917 if (scop->getMaxLoopDepth() > 0)
1918 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001919 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001920 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001921
Johannes Doerfert9143d672014-09-27 11:02:39 +00001922 // If a problem occurs while building the alias groups we need to delete
1923 // this SCoP and pretend it wasn't valid in the first place.
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001924 if (scop->buildAliasGroups(AA)) {
1925 // Statistics.
1926 ++ScopFound;
1927 if (scop->getMaxLoopDepth() > 0)
1928 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001929 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001930 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001931
1932 DEBUG(dbgs()
1933 << "\n\nNOTE: Run time checks for " << scop->getNameStr()
1934 << " could not be created as the number of parameters involved is too "
1935 "high. The SCoP will be "
1936 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the "
1937 "maximal number of parameters but be advised that the compile time "
1938 "might increase exponentially.\n\n");
1939
1940 delete scop;
1941 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00001942 return false;
1943}
1944
1945char ScopInfo::ID = 0;
1946
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001947Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1948
Tobias Grosser73600b82011-10-08 00:30:40 +00001949INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1950 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001951 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00001952INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Chandler Carruthf5579872015-01-17 14:16:56 +00001953INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00001954INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001955INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1956INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001957INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1958 "Polly - Create polyhedral description of Scops", false,
1959 false)