blob: b1f18961e95f343017df4823e56ea6ae933fa038 [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//
15// This represantation is shared among several tools in the polyhedral
16// community, which are e.g. Cloog, Pluto, Loopo, Graphite.
17//
18//===----------------------------------------------------------------------===//
19
Sebastian Pop27c10c62013-03-22 22:07:43 +000020#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser75805372011-04-29 06:27:02 +000021#include "polly/LinkAllPasses.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000022#include "polly/ScopInfo.h"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000023#include "polly/Options.h"
Tobias Grosser75805372011-04-29 06:27:02 +000024#include "polly/Support/GICHelper.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000025#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000026#include "polly/Support/ScopHelper.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000027#include "polly/TempScopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000028#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000029#include "llvm/ADT/Statistic.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000030#include "llvm/ADT/StringExtras.h"
Tobias Grosser83628182013-05-07 08:11:54 +000031#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000032#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser83628182013-05-07 08:11:54 +000033#include "llvm/Analysis/RegionIterator.h"
34#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000035#include "llvm/Support/Debug.h"
36
37#include "isl/constraint.h"
38#include "isl/set.h"
39#include "isl/map.h"
Tobias Grosser37eb4222014-02-20 21:43:54 +000040#include "isl/union_map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000041#include "isl/aff.h"
42#include "isl/printer.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000043#include "isl/local_space.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000044#include "isl/options.h"
Tobias Grosseredab1352013-06-21 06:41:31 +000045#include "isl/val.h"
Chandler Carruth95fef942014-04-22 03:30:19 +000046
Tobias Grosser75805372011-04-29 06:27:02 +000047#include <sstream>
48#include <string>
49#include <vector>
50
51using namespace llvm;
52using namespace polly;
53
Chandler Carruth95fef942014-04-22 03:30:19 +000054#define DEBUG_TYPE "polly-scops"
55
Tobias Grosser74394f02013-01-14 22:40:23 +000056STATISTIC(ScopFound, "Number of valid Scops");
57STATISTIC(RichScopFound, "Number of Scops containing a loop");
Tobias Grosser75805372011-04-29 06:27:02 +000058
Johannes Doerfert9e7b17b2014-08-18 00:40:13 +000059// Multiplicative reductions can be disabled separately as these kind of
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000060// operations can overflow easily. Additive reductions and bit operations
61// are in contrast pretty stable.
Tobias Grosser483a90d2014-07-09 10:50:10 +000062static cl::opt<bool> DisableMultiplicativeReductions(
63 "polly-disable-multiplicative-reductions",
64 cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore,
65 cl::init(false), cl::cat(PollyCategory));
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000066
Tobias Grosser0695ee42013-09-17 03:30:31 +000067/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000068struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000069public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000070 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
71 ///
72 /// @param Stmt The location at which the scalar evolution expression
73 /// is evaluated.
74 /// @param Expr The expression that is translated.
75 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
76
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000077private:
Tobias Grosser3cc99742012-06-06 16:33:15 +000078 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000079 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +000080 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000081
Tobias Grosser0695ee42013-09-17 03:30:31 +000082 SCEVAffinator(const ScopStmt *Stmt);
83 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +000084
Tobias Grosser0695ee42013-09-17 03:30:31 +000085 __isl_give isl_pw_aff *visit(const SCEV *Expr);
86 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
87 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
88 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
89 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
90 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
91 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
92 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
93 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
94 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
95 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
96 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Tobias Grosser60b54f12011-11-08 15:41:28 +000097
Tobias Grosser0695ee42013-09-17 03:30:31 +000098 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
99};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000100
Tobias Grosser0695ee42013-09-17 03:30:31 +0000101SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
102 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
103 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000104
Tobias Grosser0695ee42013-09-17 03:30:31 +0000105__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
106 const SCEV *Scev) {
107 Scop *S = Stmt->getParent();
108 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000109
Tobias Grosser0695ee42013-09-17 03:30:31 +0000110 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000111
Tobias Grosser0695ee42013-09-17 03:30:31 +0000112 SCEVAffinator Affinator(Stmt);
113 return Affinator.visit(Scev);
114}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000115
Tobias Grosser0695ee42013-09-17 03:30:31 +0000116__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
117 // In case the scev is a valid parameter, we do not further analyze this
118 // expression, but create a new parameter in the isl_pw_aff. This allows us
119 // to treat subexpressions that we cannot translate into an piecewise affine
120 // expression, as constant parameters of the piecewise affine expression.
121 if (isl_id *Id = S->getIdForParam(Expr)) {
122 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
123 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000124
Tobias Grosser0695ee42013-09-17 03:30:31 +0000125 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
126 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
127 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000128
129 return isl_pw_aff_alloc(Domain, Affine);
130 }
131
Tobias Grosser0695ee42013-09-17 03:30:31 +0000132 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
133}
134
Tobias Grosser0d170132013-10-03 13:09:19 +0000135__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000136 ConstantInt *Value = Expr->getValue();
137 isl_val *v;
138
139 // LLVM does not define if an integer value is interpreted as a signed or
140 // unsigned value. Hence, without further information, it is unknown how
141 // this value needs to be converted to GMP. At the moment, we only support
142 // signed operations. So we just interpret it as signed. Later, there are
143 // two options:
144 //
145 // 1. We always interpret any value as signed and convert the values on
146 // demand.
147 // 2. We pass down the signedness of the calculation and use it to interpret
148 // this constant correctly.
149 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
150
151 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
152 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
153 isl_aff *Affine = isl_aff_zero_on_domain(ls);
154 isl_set *Domain = isl_set_universe(Space);
155
156 Affine = isl_aff_add_constant_val(Affine, v);
157
158 return isl_pw_aff_alloc(Domain, Affine);
159}
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
266__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosserf4daf342014-08-16 09:08:55 +0000267 llvm_unreachable("Unknowns are always parameters");
Tobias Grosser0695ee42013-09-17 03:30:31 +0000268}
269
270int SCEVAffinator::getLoopDepth(const Loop *L) {
271 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
272 assert(outerLoop && "Scop does not contain this loop");
273 return L->getLoopDepth() - outerLoop->getLoopDepth();
274}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000275
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000276const std::string
277MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
278 switch (RT) {
279 case MemoryAccess::RT_NONE:
280 llvm_unreachable("Requested a reduction operator string for a memory "
281 "access which isn't a reduction");
282 case MemoryAccess::RT_ADD:
283 return "+";
284 case MemoryAccess::RT_MUL:
285 return "*";
286 case MemoryAccess::RT_BOR:
287 return "|";
288 case MemoryAccess::RT_BXOR:
289 return "^";
290 case MemoryAccess::RT_BAND:
291 return "&";
292 }
293 llvm_unreachable("Unknown reduction type");
294 return "";
295}
296
Johannes Doerfertf6183392014-07-01 20:52:51 +0000297/// @brief Return the reduction type for a given binary operator
298static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
299 const Instruction *Load) {
300 if (!BinOp)
301 return MemoryAccess::RT_NONE;
302 switch (BinOp->getOpcode()) {
303 case Instruction::FAdd:
304 if (!BinOp->hasUnsafeAlgebra())
305 return MemoryAccess::RT_NONE;
306 // Fall through
307 case Instruction::Add:
308 return MemoryAccess::RT_ADD;
309 case Instruction::Or:
310 return MemoryAccess::RT_BOR;
311 case Instruction::Xor:
312 return MemoryAccess::RT_BXOR;
313 case Instruction::And:
314 return MemoryAccess::RT_BAND;
315 case Instruction::FMul:
316 if (!BinOp->hasUnsafeAlgebra())
317 return MemoryAccess::RT_NONE;
318 // Fall through
319 case Instruction::Mul:
320 if (DisableMultiplicativeReductions)
321 return MemoryAccess::RT_NONE;
322 return MemoryAccess::RT_MUL;
323 default:
324 return MemoryAccess::RT_NONE;
325 }
326}
Tobias Grosser75805372011-04-29 06:27:02 +0000327//===----------------------------------------------------------------------===//
328
329MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000330 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000331 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000332}
333
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000334static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
335 switch (Access.getType()) {
336 case IRAccess::READ:
337 return MemoryAccess::READ;
338 case IRAccess::MUST_WRITE:
339 return MemoryAccess::MUST_WRITE;
340 case IRAccess::MAY_WRITE:
341 return MemoryAccess::MAY_WRITE;
342 }
343 llvm_unreachable("Unknown IRAccess type!");
344}
345
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000346isl_id *MemoryAccess::getArrayId() const {
347 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
348}
349
Tobias Grosser5d453812011-10-06 00:04:11 +0000350isl_map *MemoryAccess::getAccessRelation() const {
351 return isl_map_copy(AccessRelation);
352}
353
354std::string MemoryAccess::getAccessRelationStr() const {
355 return stringFromIslObj(AccessRelation);
356}
357
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000358__isl_give isl_space *MemoryAccess::getAccessRelationSpace() const {
359 return isl_map_get_space(AccessRelation);
360}
361
Tobias Grosser5d453812011-10-06 00:04:11 +0000362isl_map *MemoryAccess::getNewAccessRelation() const {
363 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000364}
365
366isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000367 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000368 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000369
Tobias Grosser084d8f72012-05-29 09:29:44 +0000370 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000371 isl_basic_set_universe(Statement->getDomainSpace()),
372 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000373}
374
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000375// Formalize no out-of-bound access assumption
376//
377// When delinearizing array accesses we optimistically assume that the
378// delinearized accesses do not access out of bound locations (the subscript
379// expression of each array evaluates for each statement instance that is
380// executed to a value that is larger than zero and strictly smaller than the
381// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000382// dimension for which we do not need to assume any upper bound. At this point
383// we formalize this assumption to ensure that at code generation time the
384// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000385//
386// To find the set of constraints necessary to avoid out of bound accesses, we
387// first build the set of data locations that are not within array bounds. We
388// then apply the reverse access relation to obtain the set of iterations that
389// may contain invalid accesses and reduce this set of iterations to the ones
390// that are actually executed by intersecting them with the domain of the
391// statement. If we now project out all loop dimensions, we obtain a set of
392// parameters that may cause statement instances to be executed that may
393// possibly yield out of bound memory accesses. The complement of these
394// constraints is the set of constraints that needs to be assumed to ensure such
395// statement instances are never executed.
396void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
397 isl_space *Space = isl_space_range(getAccessRelationSpace());
398 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000399 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000400 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
401 isl_pw_aff *Var =
402 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
403 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
404
405 isl_set *DimOutside;
406
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000407 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
408 isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000409
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000410 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
411 Statement->getNumIterators());
412 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
413 isl_space_dim(Space, isl_dim_set));
414 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
415 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000416
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000417 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000418
419 Outside = isl_set_union(Outside, DimOutside);
420 }
421
422 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
423 Outside = isl_set_intersect(Outside, Statement->getDomain());
424 Outside = isl_set_params(Outside);
425 Outside = isl_set_complement(Outside);
426 Statement->getParent()->addAssumption(Outside);
427 isl_space_free(Space);
428}
429
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000430MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Tobias Grosserabfbe632013-02-05 12:09:06 +0000431 ScopStmt *Statement)
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000432 : Type(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst),
433 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000434
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000435 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000436 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000437 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000438 isl_id *BaseAddrId = isl_id_alloc(Ctx, getBaseName().c_str(), nullptr);
Tobias Grosser5683df42011-11-09 22:34:34 +0000439
Tobias Grossera1879642011-12-20 10:43:14 +0000440 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000441 // We overapproximate non-affine accesses with a possible access to the
442 // whole array. For read accesses it does not make a difference, if an
443 // access must or may happen. However, for write accesses it is important to
444 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000445 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000446 AccessRelation =
447 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Tobias Grossera1879642011-12-20 10:43:14 +0000448 return;
449 }
450
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000451 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000452 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000453
Tobias Grosser79baa212014-04-10 08:38:02 +0000454 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000455 isl_pw_aff *Affine =
456 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000457
Sebastian Pop422e33f2014-06-03 18:16:31 +0000458 if (Size == 1) {
459 // For the non delinearized arrays, divide the access function of the last
460 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000461 //
462 // A stride one array access in C expressed as A[i] is expressed in
463 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
464 // two subsequent values of 'i' index two values that are stored next to
465 // each other in memory. By this division we make this characteristic
466 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000467 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000468 Affine = isl_pw_aff_scale_down_val(Affine, v);
469 }
470
471 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
472
Tobias Grosser79baa212014-04-10 08:38:02 +0000473 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000474 }
475
Tobias Grosser79baa212014-04-10 08:38:02 +0000476 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000477 AccessRelation = isl_map_set_tuple_id(
478 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000479 AccessRelation =
480 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
481
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000482 assumeNoOutOfBound(Access);
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000483 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000484}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000485
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000486void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000487 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000488 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000489}
490
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000491const std::string MemoryAccess::getReductionOperatorStr() const {
492 return MemoryAccess::getReductionOperatorStr(getReductionType());
493}
494
Johannes Doerfertf6183392014-07-01 20:52:51 +0000495raw_ostream &polly::operator<<(raw_ostream &OS,
496 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000497 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000498 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000499 else
500 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000501 return OS;
502}
503
Tobias Grosser75805372011-04-29 06:27:02 +0000504void MemoryAccess::print(raw_ostream &OS) const {
Tobias Grosser4f967492013-06-23 05:21:18 +0000505 switch (Type) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000506 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000507 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000508 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000509 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000510 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000511 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000512 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000513 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000514 break;
515 }
Johannes Doerfertf6183392014-07-01 20:52:51 +0000516 OS << "[Reduction Type: " << getReductionType() << "]\n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000517 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000518}
519
Tobias Grosser74394f02013-01-14 22:40:23 +0000520void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000521
522// Create a map in the size of the provided set domain, that maps from the
523// one element of the provided set domain to another element of the provided
524// set domain.
525// The mapping is limited to all points that are equal in all but the last
526// dimension and for which the last dimension of the input is strict smaller
527// than the last dimension of the output.
528//
529// getEqualAndLarger(set[i0, i1, ..., iX]):
530//
531// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
532// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
533//
Tobias Grosserf5338802011-10-06 00:03:35 +0000534static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000535 isl_space *Space = isl_space_map_from_set(setDomain);
536 isl_map *Map = isl_map_universe(isl_space_copy(Space));
537 isl_local_space *MapLocalSpace = isl_local_space_from_space(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000538 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000539
540 // Set all but the last dimension to be equal for the input and output
541 //
542 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
543 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000544 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000545 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000546
547 // Set the last dimension of the input to be strict smaller than the
548 // last dimension of the output.
549 //
550 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
551 //
Tobias Grosseredab1352013-06-21 06:41:31 +0000552 isl_val *v;
553 isl_ctx *Ctx = isl_map_get_ctx(Map);
Tobias Grosserf5338802011-10-06 00:03:35 +0000554 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosseredab1352013-06-21 06:41:31 +0000555 v = isl_val_int_from_si(Ctx, -1);
556 c = isl_constraint_set_coefficient_val(c, isl_dim_in, lastDimension, v);
557 v = isl_val_int_from_si(Ctx, 1);
558 c = isl_constraint_set_coefficient_val(c, isl_dim_out, lastDimension, v);
559 v = isl_val_int_from_si(Ctx, -1);
560 c = isl_constraint_set_constant_val(c, v);
Tobias Grosser75805372011-04-29 06:27:02 +0000561
Tobias Grosserc327932c2012-02-01 14:23:36 +0000562 Map = isl_map_add_constraint(Map, c);
Tobias Grosser75805372011-04-29 06:27:02 +0000563
Tobias Grosser23b36662011-10-17 08:32:36 +0000564 isl_local_space_free(MapLocalSpace);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000565 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000566}
567
Sebastian Popa00a0292012-12-18 07:46:06 +0000568isl_set *MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000569 isl_map *S = const_cast<isl_map *>(Schedule);
Sebastian Popa00a0292012-12-18 07:46:06 +0000570 isl_map *AccessRelation = getAccessRelation();
571 isl_space *Space = isl_space_range(isl_map_get_space(S));
572 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000573
Sebastian Popa00a0292012-12-18 07:46:06 +0000574 S = isl_map_reverse(S);
575 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000576
Sebastian Popa00a0292012-12-18 07:46:06 +0000577 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
578 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
579 NextScatt = isl_map_apply_domain(NextScatt, S);
580 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000581
Sebastian Popa00a0292012-12-18 07:46:06 +0000582 isl_set *Deltas = isl_map_deltas(NextScatt);
583 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000584}
585
Sebastian Popa00a0292012-12-18 07:46:06 +0000586bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000587 int StrideWidth) const {
588 isl_set *Stride, *StrideX;
589 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000590
Sebastian Popa00a0292012-12-18 07:46:06 +0000591 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000592 StrideX = isl_set_universe(isl_set_get_space(Stride));
593 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
594 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000595
Tobias Grosser28dd4862012-01-24 16:42:16 +0000596 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000597 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000598
Tobias Grosser28dd4862012-01-24 16:42:16 +0000599 return IsStrideX;
600}
601
Sebastian Popa00a0292012-12-18 07:46:06 +0000602bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
603 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000604}
605
Tobias Grosser79baa212014-04-10 08:38:02 +0000606bool MemoryAccess::isScalar() const {
607 return isl_map_n_out(AccessRelation) == 0;
608}
609
Sebastian Popa00a0292012-12-18 07:46:06 +0000610bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
611 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000612}
613
Tobias Grosser5d453812011-10-06 00:04:11 +0000614void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000615 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000616 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000617}
Tobias Grosser75805372011-04-29 06:27:02 +0000618
619//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000620
Tobias Grosser74394f02013-01-14 22:40:23 +0000621isl_map *ScopStmt::getScattering() const { return isl_map_copy(Scattering); }
Tobias Grossercf3942d2011-10-06 00:04:05 +0000622
Tobias Grosser37eb4222014-02-20 21:43:54 +0000623void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
624 assert(isl_set_is_subset(NewDomain, Domain) &&
625 "New domain is not a subset of old domain!");
626 isl_set_free(Domain);
627 Domain = NewDomain;
628 Scattering = isl_map_intersect_domain(Scattering, isl_set_copy(Domain));
629}
630
Tobias Grossercf3942d2011-10-06 00:04:05 +0000631void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000632 assert(NewScattering && "New scattering is nullptr");
Tobias Grosserb76f38532011-08-20 11:11:25 +0000633 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000634 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000635}
636
Tobias Grosser75805372011-04-29 06:27:02 +0000637void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000638 unsigned NbIterators = getNumIterators();
639 unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1;
640
Tobias Grosser084d8f72012-05-29 09:29:44 +0000641 isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, NbScatteringDims);
Tobias Grosserf5338802011-10-06 00:03:35 +0000642 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000643
Tobias Grosser084d8f72012-05-29 09:29:44 +0000644 Scattering = isl_map_from_domain_and_range(isl_set_universe(getDomainSpace()),
645 isl_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000646
647 // Loop dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000648 for (unsigned i = 0; i < NbIterators; ++i)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000649 Scattering =
650 isl_map_equate(Scattering, isl_dim_out, 2 * i + 1, isl_dim_in, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000651
652 // Constant dimensions
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000653 for (unsigned i = 0; i < NbIterators + 1; ++i)
654 Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000655
656 // Fill scattering dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000657 for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i)
658 Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000659
Tobias Grosser37487052011-10-06 00:03:42 +0000660 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000661}
662
663void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
Tobias Grosser083d3d32014-06-28 08:59:45 +0000664 for (auto &&Access : *tempScop.getAccessFunctions(BB)) {
665 MemAccs.push_back(new MemoryAccess(Access.first, Access.second, this));
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000666
667 // We do not track locations for scalar memory accesses at the moment.
668 //
669 // We do not have a use for this information at the moment. If we need this
670 // at some point, the "instruction -> access" mapping needs to be enhanced
671 // as a single instruction could then possibly perform multiple accesses.
Tobias Grosser083d3d32014-06-28 08:59:45 +0000672 if (!Access.first.isScalar()) {
673 assert(!InstructionToAccess.count(Access.second) &&
Tobias Grosser3fc91542014-02-20 21:43:45 +0000674 "Unexpected 1-to-N mapping on instruction to access map!");
Tobias Grosser083d3d32014-06-28 08:59:45 +0000675 InstructionToAccess[Access.second] = MemAccs.back();
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000676 }
Tobias Grosser75805372011-04-29 06:27:02 +0000677 }
678}
679
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000680void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000681 for (MemoryAccess *MA : *this)
682 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000683
684 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
685 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
686}
687
Tobias Grosser65b00582011-11-08 15:41:19 +0000688__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000689 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
690 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000691
Tobias Grosserd2795d02011-08-18 07:51:40 +0000692 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000693 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000694 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000695 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000696 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000697 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000698 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000699 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000700 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000701 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000702 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000703 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000704 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000705 case ICmpInst::ICMP_ULT:
706 case ICmpInst::ICMP_UGT:
707 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000708 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000709 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000710 default:
711 llvm_unreachable("Non integer predicate not supported");
712 }
Tobias Grosser75805372011-04-29 06:27:02 +0000713}
714
Tobias Grossere19661e2011-10-07 08:46:57 +0000715__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000716 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000717 isl_space *Space;
718 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000719
Tobias Grossere19661e2011-10-07 08:46:57 +0000720 Space = isl_set_get_space(Domain);
721 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000722
Tobias Grosser75805372011-04-29 06:27:02 +0000723 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000724 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000725 isl_pw_aff *IV =
726 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000727
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000728 // 0 <= IV.
729 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
730 Domain = isl_set_intersect(Domain, LowerBound);
731
732 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000733 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000734 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000735 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
736 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000737 Domain = isl_set_intersect(Domain, UpperBoundSet);
738 }
739
Tobias Grosserf5338802011-10-06 00:03:35 +0000740 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000741 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000742}
743
Tobias Grossere602a072013-05-07 07:30:56 +0000744__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
745 TempScop &tempScop,
746 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000747 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000748 *CurrentRegion = &CurRegion;
Tobias Grossere19661e2011-10-07 08:46:57 +0000749 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000750
Tobias Grosser75805372011-04-29 06:27:02 +0000751 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000752 if (BranchingBB != CurrentRegion->getEntry()) {
753 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000754 for (const auto &C : *Condition) {
755 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000756 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000757 }
758 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000759 BranchingBB = CurrentRegion->getEntry();
760 CurrentRegion = CurrentRegion->getParent();
761 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000762
Tobias Grossere19661e2011-10-07 08:46:57 +0000763 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000764}
765
Tobias Grossere602a072013-05-07 07:30:56 +0000766__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
767 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000768 isl_space *Space;
769 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000770 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000771
772 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
773
Tobias Grosser084d8f72012-05-29 09:29:44 +0000774 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
775
Tobias Grossere19661e2011-10-07 08:46:57 +0000776 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000777 Domain = addLoopBoundsToDomain(Domain, tempScop);
778 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000779 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +0000780
781 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000782}
783
Tobias Grosser74394f02013-01-14 22:40:23 +0000784ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Sebastian Pop860e0212013-02-15 21:26:44 +0000785 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest,
Tobias Grosser75805372011-04-29 06:27:02 +0000786 SmallVectorImpl<unsigned> &Scatter)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000787 : Parent(parent), BB(&bb), IVS(Nest.size()), NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000788 // Setup the induction variables.
Sebastian Pop860e0212013-02-15 21:26:44 +0000789 for (unsigned i = 0, e = Nest.size(); i < e; ++i) {
Sebastian Pop27c10c62013-03-22 22:07:43 +0000790 if (!SCEVCodegen) {
791 PHINode *PN = Nest[i]->getCanonicalInductionVariable();
792 assert(PN && "Non canonical IV in Scop!");
Tobias Grosser826b2af2013-03-21 16:14:50 +0000793 IVS[i] = PN;
Sebastian Pop27c10c62013-03-22 22:07:43 +0000794 }
Sebastian Pop860e0212013-02-15 21:26:44 +0000795 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +0000796 }
797
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000798 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +0000799
Tobias Grossere19661e2011-10-07 08:46:57 +0000800 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000801 buildScattering(Scatter);
802 buildAccesses(tempScop, CurRegion);
Johannes Doerferte58a0122014-06-27 20:31:28 +0000803 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000804}
805
Johannes Doerferte58a0122014-06-27 20:31:28 +0000806/// @brief Collect loads which might form a reduction chain with @p StoreMA
807///
808/// Check if the stored value for @p StoreMA is a binary operator with one or
809/// two loads as operands. If the binary operand is commutative & associative,
810/// used only once (by @p StoreMA) and its load operands are also used only
811/// once, we have found a possible reduction chain. It starts at an operand
812/// load and includes the binary operator and @p StoreMA.
813///
814/// Note: We allow only one use to ensure the load and binary operator cannot
815/// escape this block or into any other store except @p StoreMA.
816void ScopStmt::collectCandiateReductionLoads(
817 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
818 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
819 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000820 return;
821
822 // Skip if there is not one binary operator between the load and the store
823 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +0000824 if (!BinOp)
825 return;
826
827 // Skip if the binary operators has multiple uses
828 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000829 return;
830
831 // Skip if the opcode of the binary operator is not commutative/associative
832 if (!BinOp->isCommutative() || !BinOp->isAssociative())
833 return;
834
Johannes Doerfert9890a052014-07-01 00:32:29 +0000835 // Skip if the binary operator is outside the current SCoP
836 if (BinOp->getParent() != Store->getParent())
837 return;
838
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000839 // Skip if it is a multiplicative reduction and we disabled them
840 if (DisableMultiplicativeReductions &&
841 (BinOp->getOpcode() == Instruction::Mul ||
842 BinOp->getOpcode() == Instruction::FMul))
843 return;
844
Johannes Doerferte58a0122014-06-27 20:31:28 +0000845 // Check the binary operator operands for a candidate load
846 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
847 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
848 if (!PossibleLoad0 && !PossibleLoad1)
849 return;
850
851 // A load is only a candidate if it cannot escape (thus has only this use)
852 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000853 if (PossibleLoad0->getParent() == Store->getParent())
854 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000855 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000856 if (PossibleLoad1->getParent() == Store->getParent())
857 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000858}
859
860/// @brief Check for reductions in this ScopStmt
861///
862/// Iterate over all store memory accesses and check for valid binary reduction
863/// like chains. For all candidates we check if they have the same base address
864/// and there are no other accesses which overlap with them. The base address
865/// check rules out impossible reductions candidates early. The overlap check,
866/// together with the "only one user" check in collectCandiateReductionLoads,
867/// guarantees that none of the intermediate results will escape during
868/// execution of the loop nest. We basically check here that no other memory
869/// access can access the same memory as the potential reduction.
870void ScopStmt::checkForReductions() {
871 SmallVector<MemoryAccess *, 2> Loads;
872 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
873
874 // First collect candidate load-store reduction chains by iterating over all
875 // stores and collecting possible reduction loads.
876 for (MemoryAccess *StoreMA : MemAccs) {
877 if (StoreMA->isRead())
878 continue;
879
880 Loads.clear();
881 collectCandiateReductionLoads(StoreMA, Loads);
882 for (MemoryAccess *LoadMA : Loads)
883 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
884 }
885
886 // Then check each possible candidate pair.
887 for (const auto &CandidatePair : Candidates) {
888 bool Valid = true;
889 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
890 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
891
892 // Skip those with obviously unequal base addresses.
893 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
894 isl_map_free(LoadAccs);
895 isl_map_free(StoreAccs);
896 continue;
897 }
898
899 // And check if the remaining for overlap with other memory accesses.
900 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
901 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
902 isl_set *AllAccs = isl_map_range(AllAccsRel);
903
904 for (MemoryAccess *MA : MemAccs) {
905 if (MA == CandidatePair.first || MA == CandidatePair.second)
906 continue;
907
908 isl_map *AccRel =
909 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
910 isl_set *Accs = isl_map_range(AccRel);
911
912 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
913 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
914 Valid = Valid && isl_set_is_empty(OverlapAccs);
915 isl_set_free(OverlapAccs);
916 }
917 }
918
919 isl_set_free(AllAccs);
920 if (!Valid)
921 continue;
922
Johannes Doerfertf6183392014-07-01 20:52:51 +0000923 const LoadInst *Load =
924 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
925 MemoryAccess::ReductionType RT =
926 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
927
Johannes Doerferte58a0122014-06-27 20:31:28 +0000928 // If no overlapping access was found we mark the load and store as
929 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +0000930 CandidatePair.first->markAsReductionLike(RT);
931 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +0000932 }
Tobias Grosser75805372011-04-29 06:27:02 +0000933}
934
Tobias Grosser74394f02013-01-14 22:40:23 +0000935std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +0000936
937std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000938 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000939}
940
Tobias Grosser74394f02013-01-14 22:40:23 +0000941unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000942
943unsigned ScopStmt::getNumIterators() const {
944 // The final read has one dimension with one element.
945 if (!BB)
946 return 1;
947
Sebastian Pop860e0212013-02-15 21:26:44 +0000948 return NestLoops.size();
Tobias Grosser75805372011-04-29 06:27:02 +0000949}
950
951unsigned ScopStmt::getNumScattering() const {
952 return isl_map_dim(Scattering, isl_dim_out);
953}
954
955const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
956
Tobias Grosserabfbe632013-02-05 12:09:06 +0000957const PHINode *
958ScopStmt::getInductionVariableForDimension(unsigned Dimension) const {
Sebastian Popf30d3b22013-02-15 21:26:48 +0000959 return IVS[Dimension];
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000960}
961
962const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +0000963 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +0000964}
965
Tobias Grosser74394f02013-01-14 22:40:23 +0000966isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000967
Tobias Grosser74394f02013-01-14 22:40:23 +0000968isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000969
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000970isl_space *ScopStmt::getDomainSpace() const {
971 return isl_set_get_space(Domain);
972}
973
Tobias Grosser74394f02013-01-14 22:40:23 +0000974isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); }
Tobias Grossercd95b772012-08-30 11:49:38 +0000975
Tobias Grosser75805372011-04-29 06:27:02 +0000976ScopStmt::~ScopStmt() {
977 while (!MemAccs.empty()) {
978 delete MemAccs.back();
979 MemAccs.pop_back();
980 }
981
982 isl_set_free(Domain);
983 isl_map_free(Scattering);
984}
985
986void ScopStmt::print(raw_ostream &OS) const {
987 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000988 OS.indent(12) << "Domain :=\n";
989
990 if (Domain) {
991 OS.indent(16) << getDomainStr() << ";\n";
992 } else
993 OS.indent(16) << "n/a\n";
994
995 OS.indent(12) << "Scattering :=\n";
996
997 if (Domain) {
998 OS.indent(16) << getScatteringStr() << ";\n";
999 } else
1000 OS.indent(16) << "n/a\n";
1001
Tobias Grosser083d3d32014-06-28 08:59:45 +00001002 for (MemoryAccess *Access : MemAccs)
1003 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001004}
1005
1006void ScopStmt::dump() const { print(dbgs()); }
1007
1008//===----------------------------------------------------------------------===//
1009/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001010
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001011void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001012 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1013 isl_set_free(Context);
1014 Context = NewContext;
1015}
1016
Tobias Grosserabfbe632013-02-05 12:09:06 +00001017void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001018 for (const SCEV *Parameter : NewParameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001019 if (ParameterIds.find(Parameter) != ParameterIds.end())
1020 continue;
1021
1022 int dimension = Parameters.size();
1023
1024 Parameters.push_back(Parameter);
1025 ParameterIds[Parameter] = dimension;
1026 }
1027}
1028
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001029__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1030 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001031
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001032 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001033 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001034
Tobias Grosser8f99c162011-11-15 11:38:55 +00001035 std::string ParameterName;
1036
1037 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1038 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001039 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001040 }
1041
1042 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001043 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001044
Tobias Grosser20532b82014-04-11 17:56:49 +00001045 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1046 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001047}
Tobias Grosser75805372011-04-29 06:27:02 +00001048
Tobias Grosser6be480c2011-11-08 15:41:13 +00001049void Scop::buildContext() {
1050 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001051 Context = isl_set_universe(isl_space_copy(Space));
1052 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001053}
1054
Tobias Grosser18daaca2012-05-22 10:47:27 +00001055void Scop::addParameterBounds() {
1056 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
Tobias Grosseredab1352013-06-21 06:41:31 +00001057 isl_val *V;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001058 isl_id *Id;
1059 const SCEV *Scev;
1060 const IntegerType *T;
1061
1062 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserabfbe632013-02-05 12:09:06 +00001063 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001064 T = dyn_cast<IntegerType>(Scev->getType());
1065 isl_id_free(Id);
1066
1067 assert(T && "Not an integer type");
1068 int Width = T->getBitWidth();
1069
Tobias Grosseredab1352013-06-21 06:41:31 +00001070 V = isl_val_int_from_si(IslCtx, Width - 1);
1071 V = isl_val_2exp(V);
1072 V = isl_val_neg(V);
1073 Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001074
Tobias Grosseredab1352013-06-21 06:41:31 +00001075 V = isl_val_int_from_si(IslCtx, Width - 1);
1076 V = isl_val_2exp(V);
1077 V = isl_val_sub_ui(V, 1);
1078 Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001079 }
1080}
1081
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001082void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001083 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001084 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001085
Tobias Grosser083d3d32014-06-28 08:59:45 +00001086 for (const auto &ParamID : ParameterIds) {
1087 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001088 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001089 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001090 }
1091
1092 // Align the parameters of all data structures to the model.
1093 Context = isl_set_align_params(Context, Space);
1094
Tobias Grosser083d3d32014-06-28 08:59:45 +00001095 for (ScopStmt *Stmt : *this)
1096 Stmt->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001097}
1098
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001099void Scop::simplifyAssumedContext() {
1100 // The parameter constraints of the iteration domains give us a set of
1101 // constraints that need to hold for all cases where at least a single
1102 // statement iteration is executed in the whole scop. We now simplify the
1103 // assumed context under the assumption that such constraints hold and at
1104 // least a single statement iteration is executed. For cases where no
1105 // statement instances are executed, the assumptions we have taken about
1106 // the executed code do not matter and can be changed.
1107 //
1108 // WARNING: This only holds if the assumptions we have taken do not reduce
1109 // the set of statement instances that are executed. Otherwise we
1110 // may run into a case where the iteration domains suggest that
1111 // for a certain set of parameter constraints no code is executed,
1112 // but in the original program some computation would have been
1113 // performed. In such a case, modifying the run-time conditions and
1114 // possibly influencing the run-time check may cause certain scops
1115 // to not be executed.
1116 //
1117 // Example:
1118 //
1119 // When delinearizing the following code:
1120 //
1121 // for (long i = 0; i < 100; i++)
1122 // for (long j = 0; j < m; j++)
1123 // A[i+p][j] = 1.0;
1124 //
1125 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
1126 // otherwise we would access out of bound data. Now, knowing that code is
1127 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1128 AssumedContext =
1129 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
1130}
1131
Johannes Doerfertb164c792014-09-18 11:17:17 +00001132/// @brief Add the minimal/maximal access in @p Set to @p User.
1133static int buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
1134 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1135 isl_pw_multi_aff *MinPMA, *MaxPMA;
1136 isl_pw_aff *LastDimAff;
1137 isl_aff *OneAff;
1138 unsigned Pos;
1139
1140 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1141 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1142
1143 // Adjust the last dimension of the maximal access by one as we want to
1144 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1145 // we test during code generation might now point after the end of the
1146 // allocated array but we will never dereference it anyway.
1147 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1148 "Assumed at least one output dimension");
1149 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1150 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1151 OneAff = isl_aff_zero_on_domain(
1152 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1153 OneAff = isl_aff_add_constant_si(OneAff, 1);
1154 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1155 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1156
1157 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1158
1159 isl_set_free(Set);
1160 return 0;
1161}
1162
1163void Scop::buildAliasGroups(AliasAnalysis &AA) {
1164 // To create sound alias checks we perform the following steps:
1165 // o) Use the alias analysis and an alias set tracker to build alias sets
1166 // for all memory accesses inside the SCoP.
1167 // o) For each alias set we then map the aliasing pointers back to the
1168 // memory accesses we know, thus obtain groups of memory accesses which
1169 // might alias.
1170 // o) For each group with more then one base pointer we then compute minimal
1171 // and maximal accesses to each array in this group.
1172 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1173
1174 AliasSetTracker AST(AA);
1175
1176 DenseMap<Value *, MemoryAccess *> PtrToAcc;
1177 for (ScopStmt *Stmt : *this) {
1178 for (MemoryAccess *MA : *Stmt) {
1179 if (MA->isScalar())
1180 continue;
1181 Instruction *Acc = MA->getAccessInstruction();
1182 PtrToAcc[getPointerOperand(*Acc)] = MA;
1183 AST.add(Acc);
1184 }
1185 }
1186
1187 SmallVector<AliasGroupTy, 4> AliasGroups;
1188 for (AliasSet &AS : AST) {
1189 if (AS.isMustAlias())
1190 continue;
1191 AliasGroupTy AG;
1192 for (auto PR : AS)
1193 AG.push_back(PtrToAcc[PR.getValue()]);
1194 assert(AG.size() > 1 &&
1195 "Alias groups should contain at least two accesses");
1196 AliasGroups.push_back(std::move(AG));
1197 }
1198
1199 SmallPtrSet<const Value *, 4> BaseValues;
1200 for (auto I = AliasGroups.begin(); I != AliasGroups.end();) {
1201 BaseValues.clear();
1202 for (MemoryAccess *MA : *I)
1203 BaseValues.insert(MA->getBaseAddr());
1204 if (BaseValues.size() > 1)
1205 I++;
1206 else
1207 I = AliasGroups.erase(I);
1208 }
1209
1210 for (AliasGroupTy &AG : AliasGroups) {
1211 MinMaxVectorTy *MinMaxAccesses = new MinMaxVectorTy();
1212 MinMaxAccesses->reserve(AG.size());
1213
1214 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
1215 for (MemoryAccess *MA : AG)
1216 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1217 Accesses = isl_union_map_intersect_domain(Accesses, getDomains());
1218
1219 isl_union_set *Locations = isl_union_map_range(Accesses);
1220 Locations = isl_union_set_intersect_params(Locations, getAssumedContext());
1221 Locations = isl_union_set_coalesce(Locations);
1222 Locations = isl_union_set_detect_equalities(Locations);
1223 isl_union_set_foreach_set(Locations, buildMinMaxAccess, MinMaxAccesses);
1224 isl_union_set_free(Locations);
1225
1226 MinMaxAliasGroups.push_back(MinMaxAccesses);
1227 }
1228}
1229
Tobias Grosser0e27e242011-10-06 00:03:48 +00001230Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
1231 isl_ctx *Context)
Tobias Grosserabfbe632013-02-05 12:09:06 +00001232 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
1233 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001234 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001235 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001236
Tobias Grosserabfbe632013-02-05 12:09:06 +00001237 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001238 SmallVector<unsigned, 8> Scatter;
1239
1240 Scatter.assign(MaxLoopDepth + 1, 0);
1241
1242 // Build the iteration domain, access functions and scattering functions
1243 // traversing the region tree.
1244 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +00001245
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001246 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001247 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001248 simplifyAssumedContext();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001249
Tobias Grosser75805372011-04-29 06:27:02 +00001250 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1251}
1252
1253Scop::~Scop() {
1254 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001255 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +00001256
1257 // Free the statements;
Tobias Grosser083d3d32014-06-28 08:59:45 +00001258 for (ScopStmt *Stmt : *this)
1259 delete Stmt;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001260
1261 // Free the alias groups
1262 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1263 for (MinMaxAccessTy &MMA : *MinMaxAccesses) {
1264 isl_pw_multi_aff_free(MMA.first);
1265 isl_pw_multi_aff_free(MMA.second);
1266 }
1267 delete MinMaxAccesses;
1268 }
Tobias Grosser75805372011-04-29 06:27:02 +00001269}
1270
Tobias Grosser74394f02013-01-14 22:40:23 +00001271std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001272std::string Scop::getAssumedContextStr() const {
1273 return stringFromIslObj(AssumedContext);
1274}
Tobias Grosser75805372011-04-29 06:27:02 +00001275
1276std::string Scop::getNameStr() const {
1277 std::string ExitName, EntryName;
1278 raw_string_ostream ExitStr(ExitName);
1279 raw_string_ostream EntryStr(EntryName);
1280
Tobias Grosserf240b482014-01-09 10:42:15 +00001281 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001282 EntryStr.str();
1283
1284 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001285 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001286 ExitStr.str();
1287 } else
1288 ExitName = "FunctionExit";
1289
1290 return EntryName + "---" + ExitName;
1291}
1292
Tobias Grosser74394f02013-01-14 22:40:23 +00001293__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001294__isl_give isl_space *Scop::getParamSpace() const {
1295 return isl_set_get_space(this->Context);
1296}
1297
Tobias Grossere86109f2013-10-29 21:05:49 +00001298__isl_give isl_set *Scop::getAssumedContext() const {
1299 return isl_set_copy(AssumedContext);
1300}
1301
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001302void Scop::addAssumption(__isl_take isl_set *Set) {
1303 AssumedContext = isl_set_intersect(AssumedContext, Set);
1304}
1305
Tobias Grosser75805372011-04-29 06:27:02 +00001306void Scop::printContext(raw_ostream &OS) const {
1307 OS << "Context:\n";
1308
1309 if (!Context) {
1310 OS.indent(4) << "n/a\n\n";
1311 return;
1312 }
1313
1314 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001315
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001316 OS.indent(4) << "Assumed Context:\n";
1317 if (!AssumedContext) {
1318 OS.indent(4) << "n/a\n\n";
1319 return;
1320 }
1321
1322 OS.indent(4) << getAssumedContextStr() << "\n";
1323
Tobias Grosser083d3d32014-06-28 08:59:45 +00001324 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001325 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001326 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1327 }
Tobias Grosser75805372011-04-29 06:27:02 +00001328}
1329
Johannes Doerfertb164c792014-09-18 11:17:17 +00001330void Scop::printAliasAssumptions(raw_ostream &OS) const {
1331 OS.indent(4) << "Alias Groups (" << MinMaxAliasGroups.size() << "):\n";
1332 if (MinMaxAliasGroups.empty()) {
1333 OS.indent(8) << "n/a\n";
1334 return;
1335 }
1336 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1337 OS.indent(8) << "[[";
1338 for (MinMaxAccessTy &MinMacAccess : *MinMaxAccesses)
1339 OS << " <" << MinMacAccess.first << ", " << MinMacAccess.second << ">";
1340 OS << " ]]\n";
1341 }
1342}
1343
Tobias Grosser75805372011-04-29 06:27:02 +00001344void Scop::printStatements(raw_ostream &OS) const {
1345 OS << "Statements {\n";
1346
Tobias Grosser083d3d32014-06-28 08:59:45 +00001347 for (ScopStmt *Stmt : *this)
1348 OS.indent(4) << *Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001349
1350 OS.indent(4) << "}\n";
1351}
1352
Tobias Grosser75805372011-04-29 06:27:02 +00001353void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001354 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1355 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001356 OS.indent(4) << "Region: " << getNameStr() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001357 printContext(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001358 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001359 printStatements(OS.indent(4));
1360}
1361
1362void Scop::dump() const { print(dbgs()); }
1363
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001364isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001365
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001366__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001367 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001368
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001369 for (ScopStmt *Stmt : *this)
1370 Domain = isl_union_set_add_set(Domain, Stmt->getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001371
1372 return Domain;
1373}
1374
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001375__isl_give isl_union_map *Scop::getMustWrites() {
1376 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1377
1378 for (ScopStmt *Stmt : *this) {
1379 for (MemoryAccess *MA : *Stmt) {
1380 if (!MA->isMustWrite())
1381 continue;
1382
1383 isl_set *Domain = Stmt->getDomain();
1384 isl_map *AccessDomain = MA->getAccessRelation();
1385 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1386 Write = isl_union_map_add_map(Write, AccessDomain);
1387 }
1388 }
1389 return isl_union_map_coalesce(Write);
1390}
1391
1392__isl_give isl_union_map *Scop::getMayWrites() {
1393 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1394
1395 for (ScopStmt *Stmt : *this) {
1396 for (MemoryAccess *MA : *Stmt) {
1397 if (!MA->isMayWrite())
1398 continue;
1399
1400 isl_set *Domain = Stmt->getDomain();
1401 isl_map *AccessDomain = MA->getAccessRelation();
1402 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1403 Write = isl_union_map_add_map(Write, AccessDomain);
1404 }
1405 }
1406 return isl_union_map_coalesce(Write);
1407}
1408
Tobias Grosser37eb4222014-02-20 21:43:54 +00001409__isl_give isl_union_map *Scop::getWrites() {
1410 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1411
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001412 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001413 for (MemoryAccess *MA : *Stmt) {
1414 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001415 continue;
1416
1417 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001418 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001419 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1420 Write = isl_union_map_add_map(Write, AccessDomain);
1421 }
1422 }
1423 return isl_union_map_coalesce(Write);
1424}
1425
1426__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001427 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001428
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001429 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001430 for (MemoryAccess *MA : *Stmt) {
1431 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001432 continue;
1433
1434 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001435 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001436
1437 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1438 Read = isl_union_map_add_map(Read, AccessDomain);
1439 }
1440 }
1441 return isl_union_map_coalesce(Read);
1442}
1443
1444__isl_give isl_union_map *Scop::getSchedule() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001445 isl_union_map *Schedule = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001446
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001447 for (ScopStmt *Stmt : *this)
Tobias Grosser37eb4222014-02-20 21:43:54 +00001448 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001449
Tobias Grosser37eb4222014-02-20 21:43:54 +00001450 return isl_union_map_coalesce(Schedule);
1451}
1452
1453bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1454 bool Changed = false;
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001455 for (ScopStmt *Stmt : *this) {
Tobias Grosser37eb4222014-02-20 21:43:54 +00001456 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001457 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1458 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1459
1460 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1461 isl_union_set_free(StmtDomain);
1462 isl_union_set_free(NewStmtDomain);
1463 continue;
1464 }
1465
1466 Changed = true;
1467
1468 isl_union_set_free(StmtDomain);
1469 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1470
1471 if (isl_union_set_is_empty(NewStmtDomain)) {
1472 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1473 isl_union_set_free(NewStmtDomain);
1474 } else
1475 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1476 }
1477 isl_union_set_free(Domain);
1478 return Changed;
1479}
1480
Tobias Grosser75805372011-04-29 06:27:02 +00001481ScalarEvolution *Scop::getSE() const { return SE; }
1482
1483bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1484 if (tempScop.getAccessFunctions(BB))
1485 return false;
1486
1487 return true;
1488}
1489
Tobias Grosser74394f02013-01-14 22:40:23 +00001490void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1491 SmallVectorImpl<Loop *> &NestLoops,
1492 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001493 Loop *L = castToLoop(CurRegion, LI);
1494
1495 if (L)
1496 NestLoops.push_back(L);
1497
1498 unsigned loopDepth = NestLoops.size();
1499 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1500
1501 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001502 E = CurRegion.element_end();
1503 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001504 if (I->isSubRegion())
1505 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1506 else {
1507 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1508
1509 if (isTrivialBB(BB, tempScop))
1510 continue;
1511
Tobias Grosserabfbe632013-02-05 12:09:06 +00001512 Stmts.push_back(
1513 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter));
Tobias Grosser75805372011-04-29 06:27:02 +00001514
1515 // Increasing the Scattering function is OK for the moment, because
1516 // we are using a depth first iterator and the program is well structured.
1517 ++Scatter[loopDepth];
1518 }
1519
1520 if (!L)
1521 return;
1522
1523 // Exiting a loop region.
1524 Scatter[loopDepth] = 0;
1525 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001526 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001527}
1528
1529//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001530ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1531 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001532 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001533}
1534
1535ScopInfo::~ScopInfo() {
1536 clear();
1537 isl_ctx_free(ctx);
1538}
1539
Tobias Grosser75805372011-04-29 06:27:02 +00001540void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1541 AU.addRequired<LoopInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001542 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00001543 AU.addRequired<ScalarEvolution>();
1544 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001545 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001546 AU.setPreservesAll();
1547}
1548
1549bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1550 LoopInfo &LI = getAnalysis<LoopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001551 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001552 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1553
1554 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1555
1556 // This region is no Scop.
1557 if (!tempScop) {
1558 scop = 0;
1559 return false;
1560 }
1561
1562 // Statistics.
1563 ++ScopFound;
Tobias Grosser74394f02013-01-14 22:40:23 +00001564 if (tempScop->getMaxLoopDepth() > 0)
1565 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00001566
Tobias Grosserb76f38532011-08-20 11:11:25 +00001567 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001568
Johannes Doerfertb164c792014-09-18 11:17:17 +00001569 if (PollyUseRuntimeAliasChecks)
1570 scop->buildAliasGroups(AA);
1571
Tobias Grosser75805372011-04-29 06:27:02 +00001572 return false;
1573}
1574
1575char ScopInfo::ID = 0;
1576
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001577Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1578
Tobias Grosser73600b82011-10-08 00:30:40 +00001579INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1580 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001581 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00001582INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001583INITIALIZE_PASS_DEPENDENCY(LoopInfo);
Matt Arsenault8ca36812014-07-19 18:40:17 +00001584INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001585INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1586INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001587INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1588 "Polly - Create polyhedral description of Scops", false,
1589 false)