blob: a1e64c0935e785d816557950ac47f936079a01a4 [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
20#include "polly/ScopInfo.h"
21
22#include "polly/TempScopInfo.h"
23#include "polly/LinkAllPasses.h"
24#include "polly/Support/GICHelper.h"
25#include "polly/Support/ScopHelper.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000026#include "polly/Support/SCEVValidator.h"
Tobias Grosser75805372011-04-29 06:27:02 +000027
28#include "llvm/Analysis/LoopInfo.h"
29#include "llvm/Analysis/ScalarEvolutionExpressions.h"
30#include "llvm/Analysis/RegionIterator.h"
31#include "llvm/Assembly/Writer.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/SetVector.h"
34#include "llvm/Support/CommandLine.h"
35
36#define DEBUG_TYPE "polly-scops"
37#include "llvm/Support/Debug.h"
38
39#include "isl/constraint.h"
40#include "isl/set.h"
41#include "isl/map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000042#include "isl/aff.h"
43#include "isl/printer.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000044#include "isl/local_space.h"
Tobias Grosser75805372011-04-29 06:27:02 +000045#include <sstream>
46#include <string>
47#include <vector>
48
49using namespace llvm;
50using namespace polly;
51
52STATISTIC(ScopFound, "Number of valid Scops");
53STATISTIC(RichScopFound, "Number of Scops containing a loop");
54
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000055/// Convert an int into a string.
56static std::string convertInt(int number)
57{
58 if (number == 0)
59 return "0";
60 std::string temp = "";
61 std::string returnvalue = "";
62 while (number > 0)
63 {
64 temp += number % 10 + 48;
65 number /= 10;
66 }
67 for (unsigned i = 0; i < temp.length(); i++)
68 returnvalue+=temp[temp.length() - i - 1];
69 return returnvalue;
Tobias Grosser75805372011-04-29 06:27:02 +000070}
71
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000072/// Translate a SCEVExpression into an isl_pw_aff object.
73struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff*> {
74private:
75 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000076 int NbLoopSpaces;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000077 const Scop *scop;
78
79 /// baseAdress is set if we analyze a memory access. It holds the base address
80 /// of this memory access.
81 const Value *baseAddress;
82
83public:
Tobias Grosser60b54f12011-11-08 15:41:28 +000084 static isl_pw_aff *getPwAff(ScopStmt *stmt, const SCEV *scev,
Tobias Grosser9b13d3d2011-10-06 22:32:58 +000085 const Value *baseAddress = 0) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000086 Scop *S = stmt->getParent();
87 const Region *Reg = &S->getRegion();
88
89 if (baseAddress) {
90 Value *Base;
91 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE(), &Base));
92 } else {
93 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE()));
94 }
95
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000096 SCEVAffinator Affinator(stmt, baseAddress);
97 return Affinator.visit(scev);
98 }
99
100 isl_pw_aff *visit(const SCEV *scev) {
Tobias Grosser76c2e322011-11-07 12:58:59 +0000101 // In case the scev is a valid parameter, we do not further analyze this
102 // expression, but create a new parameter in the isl_pw_aff. This allows us
103 // to treat subexpressions that we cannot translate into an piecewise affine
104 // expression, as constant parameters of the piecewise affine expression.
105 if (isl_id *Id = scop->getIdForParam(scev)) {
106 isl_space *Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
107 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000108
Tobias Grosser76c2e322011-11-07 12:58:59 +0000109 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
110 isl_aff *Affine = isl_aff_zero_on_domain(
111 isl_local_space_from_space(Space));
112 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000113
Tobias Grosser76c2e322011-11-07 12:58:59 +0000114 return isl_pw_aff_alloc(Domain, Affine);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000115 }
116
117 return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev);
118 }
119
120 SCEVAffinator(const ScopStmt *stmt, const Value *baseAddress) :
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000121 ctx(stmt->getIslCtx()),
Tobias Grosserf5338802011-10-06 00:03:35 +0000122 NbLoopSpaces(stmt->getNumIterators()),
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000123 scop(stmt->getParent()),
124 baseAddress(baseAddress) {};
125
126 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) {
127 ConstantInt *Value = Constant->getValue();
128 isl_int v;
129 isl_int_init(v);
130
131 // LLVM does not define if an integer value is interpreted as a signed or
132 // unsigned value. Hence, without further information, it is unknown how
133 // this value needs to be converted to GMP. At the moment, we only support
134 // signed operations. So we just interpret it as signed. Later, there are
135 // two options:
136 //
137 // 1. We always interpret any value as signed and convert the values on
138 // demand.
139 // 2. We pass down the signedness of the calculation and use it to interpret
140 // this constant correctly.
141 MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true);
142
Tobias Grosserf5338802011-10-06 00:03:35 +0000143 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
144 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
145 isl_aff *Affine = isl_aff_zero_on_domain(ls);
146 isl_set *Domain = isl_set_universe(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000147
148 Affine = isl_aff_add_constant(Affine, v);
149 isl_int_clear(v);
150
151 return isl_pw_aff_alloc(Domain, Affine);
152 }
153
154 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr* Expr) {
155 assert(0 && "Not yet supported");
156 }
157
158 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) {
159 assert(0 && "Not yet supported");
160 }
161
162 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr* Expr) {
163 // Assuming the value is signed, a sign extension is basically a noop.
164 // TODO: Reconsider this as soon as we support unsigned values.
165 return visit(Expr->getOperand());
166 }
167
168 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr* Expr) {
169 isl_pw_aff *Sum = visit(Expr->getOperand(0));
170
171 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
172 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
173 Sum = isl_pw_aff_add(Sum, NextSummand);
174 }
175
176 // TODO: Check for NSW and NUW.
177
178 return Sum;
179 }
180
181 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr* Expr) {
182 isl_pw_aff *Product = visit(Expr->getOperand(0));
183
184 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
185 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
186
187 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
188 isl_pw_aff_free(Product);
189 isl_pw_aff_free(NextOperand);
190 return NULL;
191 }
192
193 Product = isl_pw_aff_mul(Product, NextOperand);
194 }
195
196 // TODO: Check for NSW and NUW.
197 return Product;
198 }
199
200 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr* Expr) {
201 assert(0 && "Not yet supported");
202 }
203
204 int getLoopDepth(const Loop *L) {
205 Loop *outerLoop =
206 scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L));
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000207 assert(outerLoop && "Scop does not contain this loop");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000208 return L->getLoopDepth() - outerLoop->getLoopDepth();
209 }
210
211 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr* Expr) {
212 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000213 assert(scop->getRegion().contains(Expr->getLoop())
214 && "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000215
216 isl_pw_aff *Start = visit(Expr->getStart());
217 isl_pw_aff *Step = visit(Expr->getOperand(1));
Tobias Grosserf5338802011-10-06 00:03:35 +0000218 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
219 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000220
221 int loopDimension = getLoopDepth(Expr->getLoop());
222
Tobias Grosserf5338802011-10-06 00:03:35 +0000223 isl_aff *LAff = isl_aff_set_coefficient_si(
224 isl_aff_zero_on_domain (LocalSpace), isl_dim_in, loopDimension, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000225 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
226
227 // TODO: Do we need to check for NSW and NUW?
228 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
229 }
230
231 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr* Expr) {
232 isl_pw_aff *Max = visit(Expr->getOperand(0));
233
234 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
235 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
236 Max = isl_pw_aff_max(Max, NextOperand);
237 }
238
239 return Max;
240 }
241
242 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr* Expr) {
243 assert(0 && "Not yet supported");
244 }
245
246 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown* Expr) {
247 Value *Value = Expr->getValue();
248
Tobias Grosserf5338802011-10-06 00:03:35 +0000249 isl_space *Space;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000250
251 /// If baseAddress is set, we ignore its Value object in the scev and do not
252 /// add it to the isl_pw_aff. This is because it is regarded as defining the
253 /// name of an array, in contrast to its array subscript.
254 if (baseAddress != Value) {
255 isl_id *ID = isl_id_alloc(ctx, Value->getNameStr().c_str(), Value);
Tobias Grosserf5338802011-10-06 00:03:35 +0000256 Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
257 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000258 } else {
Tobias Grosserf5338802011-10-06 00:03:35 +0000259 Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000260 }
261
Tobias Grosserf5338802011-10-06 00:03:35 +0000262 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
263 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000264
265 if (baseAddress != Value)
266 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
267
268 return isl_pw_aff_alloc(Domain, Affine);
269 }
270};
271
Tobias Grosser75805372011-04-29 06:27:02 +0000272//===----------------------------------------------------------------------===//
273
274MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000275 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000276 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000277}
278
279static void replace(std::string& str, const std::string& find,
280 const std::string& replace) {
281 size_t pos = 0;
282 while((pos = str.find(find, pos)) != std::string::npos)
283 {
284 str.replace(pos, find.length(), replace);
285 pos += replace.length();
286 }
287}
288
289static void makeIslCompatible(std::string& str) {
Tobias Grossereec4d56e2011-08-20 11:11:14 +0000290 str.erase(0, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000291 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000292 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000293}
294
295void MemoryAccess::setBaseName() {
296 raw_string_ostream OS(BaseName);
297 WriteAsOperand(OS, getBaseAddr(), false);
298 BaseName = OS.str();
299
Tobias Grosser75805372011-04-29 06:27:02 +0000300 makeIslCompatible(BaseName);
301 BaseName = "MemRef_" + BaseName;
302}
303
Tobias Grosser5d453812011-10-06 00:04:11 +0000304isl_map *MemoryAccess::getAccessRelation() const {
305 return isl_map_copy(AccessRelation);
306}
307
308std::string MemoryAccess::getAccessRelationStr() const {
309 return stringFromIslObj(AccessRelation);
310}
311
312isl_map *MemoryAccess::getNewAccessRelation() const {
313 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000314}
315
316isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000317 isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0,
Tobias Grosserf5338802011-10-06 00:03:35 +0000318 Statement->getNumIterators(), 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000319 setBaseName();
320
Tobias Grosserf5338802011-10-06 00:03:35 +0000321 Space = isl_space_set_tuple_name(Space, isl_dim_out, getBaseName().c_str());
322 Space = isl_space_set_tuple_name(Space, isl_dim_in, Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000323
Tobias Grosserf5338802011-10-06 00:03:35 +0000324 return isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000325}
326
327MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000328 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000329 BaseAddr = AffFunc.getBaseAddr();
330 Type = AffFunc.isRead() ? Read : Write;
331 statement = Statement;
332
333 setBaseName();
334
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000335 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, AffFunc.OriginalSCEV,
336 AffFunc.getBaseAddr());
Tobias Grosser75805372011-04-29 06:27:02 +0000337
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000338 // Devide the access function by the size of the elements in the array.
339 //
340 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
341 // as something like A[i * elementsize]. This hides the fact that two
342 // subsequent values of 'i' index two values that are stored next to each
343 // other in memory. By this devision we make this characteristic obvious
344 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000345 isl_int v;
346 isl_int_init(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000347 isl_int_set_si(v, AffFunc.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000348 Affine = isl_pw_aff_scale_down(Affine, v);
349 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000350
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000351 AccessRelation = isl_map_from_pw_aff(Affine);
352 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
353 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000354 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
355 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000356}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000357
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000358void MemoryAccess::realignParams() {
359 isl_space *ParamSpace = statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000360 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000361}
362
363MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000364 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000365 BaseAddr = BaseAddress;
366 Type = Read;
367 statement = Statement;
368
369 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
370 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000371 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
372 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000373}
374
375void MemoryAccess::print(raw_ostream &OS) const {
376 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000377 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000378}
379
380void MemoryAccess::dump() const {
381 print(errs());
382}
383
384// Create a map in the size of the provided set domain, that maps from the
385// one element of the provided set domain to another element of the provided
386// set domain.
387// The mapping is limited to all points that are equal in all but the last
388// dimension and for which the last dimension of the input is strict smaller
389// than the last dimension of the output.
390//
391// getEqualAndLarger(set[i0, i1, ..., iX]):
392//
393// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
394// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
395//
Tobias Grosserf5338802011-10-06 00:03:35 +0000396static isl_map *getEqualAndLarger(isl_space *setDomain) {
397 isl_space *mapDomain = isl_space_map_from_set(setDomain);
Tobias Grosser23b36662011-10-17 08:32:36 +0000398 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(mapDomain));
Tobias Grosserf5338802011-10-06 00:03:35 +0000399 isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000400
401 // Set all but the last dimension to be equal for the input and output
402 //
403 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
404 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
405 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
406 isl_int v;
407 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000408 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000409
410 isl_int_set_si(v, 1);
411 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
412 isl_int_set_si(v, -1);
413 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
414
415 bmap = isl_basic_map_add_constraint(bmap, c);
416
417 isl_int_clear(v);
418 }
419
420 // Set the last dimension of the input to be strict smaller than the
421 // last dimension of the output.
422 //
423 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
424 //
425 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
426 isl_int v;
427 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000428 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000429 isl_int_set_si(v, -1);
430 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
431 isl_int_set_si(v, 1);
432 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
433 isl_int_set_si(v, -1);
434 isl_constraint_set_constant(c, v);
435 isl_int_clear(v);
436
437 bmap = isl_basic_map_add_constraint(bmap, c);
438
Tobias Grosser23b36662011-10-17 08:32:36 +0000439 isl_local_space_free(MapLocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000440 return isl_map_from_basic_map(bmap);
441}
442
443isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000444 isl_map *accessRelation = getAccessRelation();
Tobias Grosser75805372011-04-29 06:27:02 +0000445 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
Tobias Grossercf3942d2011-10-06 00:04:05 +0000446 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000447
448 scattering = isl_map_reverse(scattering);
449 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
450 scattering = isl_map_project_out(scattering, isl_dim_in,
451 isl_set_n_dim(scatteringDomain),
452 difference);
453
454 // Remove all names of the scattering dimensions, as the names may be lost
455 // anyways during the project. This leads to consistent results.
456 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
457 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
458
Tobias Grosserf5338802011-10-06 00:03:35 +0000459 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000460 nextScatt = isl_map_lexmin(nextScatt);
461
462 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
463
464 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
465 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
466 nextScatt = isl_map_apply_domain(nextScatt, scattering);
467 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
468
469 return isl_map_deltas(nextScatt);
470}
471
472bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
473 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000474 isl_space *StrideSpace = isl_set_get_space(stride);
475 isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace);
476 isl_constraint *c = isl_equality_alloc(StrideLS);
Tobias Grosser75805372011-04-29 06:27:02 +0000477
478 isl_int v;
479 isl_int_init(v);
480 isl_int_set_si(v, 1);
481 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
482 isl_int_set_si(v, 0);
483 isl_constraint_set_constant(c, v);
484 isl_int_clear(v);
485
Tobias Grosserf5338802011-10-06 00:03:35 +0000486 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000487
488 bset = isl_basic_set_add_constraint(bset, c);
489 isl_set *strideZero = isl_set_from_basic_set(bset);
490
Tobias Grosserb76f38532011-08-20 11:11:25 +0000491 bool isStrideZero = isl_set_is_equal(stride, strideZero);
492
493 isl_set_free(strideZero);
494 isl_set_free(stride);
495
496 return isStrideZero;
Tobias Grosser75805372011-04-29 06:27:02 +0000497}
498
499bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
500 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000501 isl_space *StrideSpace = isl_set_get_space(stride);
502 isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace);
503 isl_constraint *c = isl_equality_alloc(StrideLSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000504
505 isl_int v;
506 isl_int_init(v);
507 isl_int_set_si(v, 1);
508 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
509 isl_int_set_si(v, -1);
510 isl_constraint_set_constant(c, v);
511 isl_int_clear(v);
512
Tobias Grosserf5338802011-10-06 00:03:35 +0000513 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000514
515 bset = isl_basic_set_add_constraint(bset, c);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000516 isl_set *strideOne = isl_set_from_basic_set(bset);
Tobias Grosser75805372011-04-29 06:27:02 +0000517
Tobias Grosserb76f38532011-08-20 11:11:25 +0000518 bool isStrideOne = isl_set_is_equal(stride, strideOne);
519
520 isl_set_free(strideOne);
521 isl_set_free(stride);
522
523 return isStrideOne;
Tobias Grosser75805372011-04-29 06:27:02 +0000524}
525
Tobias Grosser5d453812011-10-06 00:04:11 +0000526void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000527 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000528 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000529}
Tobias Grosser75805372011-04-29 06:27:02 +0000530
531//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000532
533isl_map *ScopStmt::getScattering() const {
534 return isl_map_copy(Scattering);
535}
536
537void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000538 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000539 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000540}
541
Tobias Grosser75805372011-04-29 06:27:02 +0000542void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
543 unsigned NumberOfIterators = getNumIterators();
Tobias Grosserf5338802011-10-06 00:03:35 +0000544 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000545 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators,
Tobias Grosserf5338802011-10-06 00:03:35 +0000546 ScatSpace);
547 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
548 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
549 isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space));
550 isl_basic_map *bmap = isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000551 isl_int v;
552 isl_int_init(v);
553
554 // Loop dimensions.
555 for (unsigned i = 0; i < NumberOfIterators; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000556 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000557 isl_int_set_si(v, 1);
558 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
559 isl_int_set_si(v, -1);
560 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
561
562 bmap = isl_basic_map_add_constraint(bmap, c);
563 }
564
565 // Constant dimensions
566 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000567 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000568 isl_int_set_si(v, -1);
569 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
570 isl_int_set_si(v, Scatter[i]);
571 isl_constraint_set_constant(c, v);
572
573 bmap = isl_basic_map_add_constraint(bmap, c);
574 }
575
576 // Fill scattering dimensions.
Tobias Grosserf5338802011-10-06 00:03:35 +0000577 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) {
578 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000579 isl_int_set_si(v, 1);
580 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
581 isl_int_set_si(v, 0);
582 isl_constraint_set_constant(c, v);
583
584 bmap = isl_basic_map_add_constraint(bmap, c);
585 }
586
587 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000588 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000589 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser0ad4caa2011-10-08 00:35:17 +0000590 isl_local_space_free(LSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000591}
592
593void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
594 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
595
596 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
597 E = AccFuncs->end(); I != E; ++I) {
598 MemAccs.push_back(new MemoryAccess(I->first, this));
599 InstructionToAccess[I->second] = MemAccs.back();
600 }
601}
602
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000603void ScopStmt::realignParams() {
604 for (memacc_iterator MI = memacc_begin(), ME = memacc_end(); MI != ME; ++MI)
605 (*MI)->realignParams();
606
607 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
608 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
609}
610
Tobias Grosser65b00582011-11-08 15:41:19 +0000611__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000612 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS()->OriginalSCEV, 0);
613 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS()->OriginalSCEV, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000614
Tobias Grosserd2795d02011-08-18 07:51:40 +0000615 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000616 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000617 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000618 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000619 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000620 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000621 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000622 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000623 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000624 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000625 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000626 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000627 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000628 case ICmpInst::ICMP_ULT:
629 case ICmpInst::ICMP_UGT:
630 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000631 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000632 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000633 default:
634 llvm_unreachable("Non integer predicate not supported");
635 }
Tobias Grosser75805372011-04-29 06:27:02 +0000636}
637
Tobias Grossere19661e2011-10-07 08:46:57 +0000638__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000639 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000640 isl_space *Space;
641 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000642
Tobias Grossere19661e2011-10-07 08:46:57 +0000643 Space = isl_set_get_space(Domain);
644 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000645
Tobias Grosser75805372011-04-29 06:27:02 +0000646 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000647 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
648 isl_pw_aff *IV = isl_pw_aff_from_aff(
649 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000650
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000651 // 0 <= IV.
652 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
653 Domain = isl_set_intersect(Domain, LowerBound);
654
655 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000656 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000657 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000658 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
659 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000660 Domain = isl_set_intersect(Domain, UpperBoundSet);
661 }
662
Tobias Grosserf5338802011-10-06 00:03:35 +0000663 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000664 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000665}
666
Tobias Grossere19661e2011-10-07 08:46:57 +0000667__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
668 TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000669 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000670 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
671 *CurrentRegion = &CurRegion;
672 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000673
Tobias Grosser75805372011-04-29 06:27:02 +0000674 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000675 if (BranchingBB != CurrentRegion->getEntry()) {
676 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
677 for (BBCond::const_iterator CI = Condition->begin(),
678 CE = Condition->end(); CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000679 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000680 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000681 }
682 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000683 BranchingBB = CurrentRegion->getEntry();
684 CurrentRegion = CurrentRegion->getParent();
685 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000686
Tobias Grossere19661e2011-10-07 08:46:57 +0000687 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000688}
689
Tobias Grossere19661e2011-10-07 08:46:57 +0000690__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000691 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000692 isl_space *Space;
693 isl_set *Domain;
694
695 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
696
697 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000698 Domain = addLoopBoundsToDomain(Domain, tempScop);
699 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
700 Domain = isl_set_set_tuple_name(Domain, getBaseName());
701
702 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000703}
704
705ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
706 const Region &CurRegion, BasicBlock &bb,
707 SmallVectorImpl<Loop*> &NestLoops,
708 SmallVectorImpl<unsigned> &Scatter)
709 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
710 // Setup the induction variables.
711 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
712 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
713 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000714 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000715 }
716
717 raw_string_ostream OS(BaseName);
718 WriteAsOperand(OS, &bb, false);
719 BaseName = OS.str();
720
Tobias Grosser75805372011-04-29 06:27:02 +0000721 makeIslCompatible(BaseName);
722 BaseName = "Stmt_" + BaseName;
723
Tobias Grossere19661e2011-10-07 08:46:57 +0000724 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000725 buildScattering(Scatter);
726 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000727}
728
729ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
730 : Parent(parent), BB(NULL), IVS(0) {
731
732 BaseName = "FinalRead";
733
734 // Build iteration domain.
735 std::string IterationDomainString = "{[i0] : i0 = 0}";
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000736 Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str());
Tobias Grosser75805372011-04-29 06:27:02 +0000737 Domain = isl_set_set_tuple_name(Domain, getBaseName());
738
739 // Build scattering.
Tobias Grosserf5338802011-10-06 00:03:35 +0000740 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000741 isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace);
Tobias Grosserf5338802011-10-06 00:03:35 +0000742 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
743 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
744 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000745 isl_int v;
746 isl_int_init(v);
747
Tobias Grosserf5338802011-10-06 00:03:35 +0000748 isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000749 isl_int_set_si(v, -1);
750 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
751
752 // TODO: This is incorrect. We should not use a very large number to ensure
753 // that this statement is executed last.
754 isl_int_set_si(v, 200000000);
755 isl_constraint_set_constant(c, v);
756
757 bmap = isl_basic_map_add_constraint(bmap, c);
758 isl_int_clear(v);
759 Scattering = isl_map_from_basic_map(bmap);
760
761 // Build memory accesses, use SetVector to keep the order of memory accesses
762 // and prevent the same memory access inserted more than once.
763 SetVector<const Value*> BaseAddressSet;
764
765 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
766 ++SI) {
767 ScopStmt *Stmt = *SI;
768
769 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
770 E = Stmt->memacc_end(); I != E; ++I)
771 BaseAddressSet.insert((*I)->getBaseAddr());
772 }
773
774 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
775 BE = BaseAddressSet.end(); BI != BE; ++BI)
776 MemAccs.push_back(new MemoryAccess(*BI, this));
Tobias Grosser75805372011-04-29 06:27:02 +0000777}
778
779std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000780 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000781}
782
783std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000784 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000785}
786
787unsigned ScopStmt::getNumParams() const {
788 return Parent.getNumParams();
789}
790
791unsigned ScopStmt::getNumIterators() const {
792 // The final read has one dimension with one element.
793 if (!BB)
794 return 1;
795
796 return IVS.size();
797}
798
799unsigned ScopStmt::getNumScattering() const {
800 return isl_map_dim(Scattering, isl_dim_out);
801}
802
803const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
804
805const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
806 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000807 return IVS[Dimension].first;
808}
809
810const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
811 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000812}
813
814const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
815 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000816 PHINode *PN =
817 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000818 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
819}
820
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000821isl_ctx *ScopStmt::getIslCtx() const {
822 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000823}
824
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000825isl_set *ScopStmt::getDomain() const {
826 return isl_set_copy(Domain);
827}
828
Tobias Grosser75805372011-04-29 06:27:02 +0000829ScopStmt::~ScopStmt() {
830 while (!MemAccs.empty()) {
831 delete MemAccs.back();
832 MemAccs.pop_back();
833 }
834
835 isl_set_free(Domain);
836 isl_map_free(Scattering);
837}
838
839void ScopStmt::print(raw_ostream &OS) const {
840 OS << "\t" << getBaseName() << "\n";
841
842 OS.indent(12) << "Domain :=\n";
843
844 if (Domain) {
845 OS.indent(16) << getDomainStr() << ";\n";
846 } else
847 OS.indent(16) << "n/a\n";
848
849 OS.indent(12) << "Scattering :=\n";
850
851 if (Domain) {
852 OS.indent(16) << getScatteringStr() << ";\n";
853 } else
854 OS.indent(16) << "n/a\n";
855
856 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
857 I != E; ++I)
858 (*I)->print(OS);
859}
860
861void ScopStmt::dump() const { print(dbgs()); }
862
863//===----------------------------------------------------------------------===//
864/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000865
866void Scop::addParams(std::vector<const SCEV*> NewParameters) {
867 for (std::vector<const SCEV*>::iterator PI = NewParameters.begin(),
868 PE = NewParameters.end(); PI != PE; ++PI) {
869 const SCEV *Parameter = *PI;
870
871 if (ParameterIds.find(Parameter) != ParameterIds.end())
872 continue;
873
874 int dimension = Parameters.size();
875
876 Parameters.push_back(Parameter);
877 ParameterIds[Parameter] = dimension;
878 }
879}
880
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000881__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
882 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000883
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000884 if (IdIter == ParameterIds.end())
885 return NULL;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000886
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000887 std::string ParameterName = "p" + convertInt(IdIter->second);
888 return isl_id_alloc(getIslCtx(), ParameterName.c_str(), (void *) Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000889}
Tobias Grosser75805372011-04-29 06:27:02 +0000890
Tobias Grosser6be480c2011-11-08 15:41:13 +0000891void Scop::buildContext() {
892 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grosserf5338802011-10-06 00:03:35 +0000893 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000894}
895
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000896void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000897 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000898 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000899
900 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
901 PI != PE; ++PI) {
902 const SCEV *Parameter = PI->first;
903 isl_id *id = getIdForParam(Parameter);
904 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
905 }
906
907 // Align the parameters of all data structures to the model.
908 Context = isl_set_align_params(Context, Space);
909
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000910 for (iterator I = begin(), E = end(); I != E; ++I)
911 (*I)->realignParams();
912}
913
Tobias Grosser0e27e242011-10-06 00:03:48 +0000914Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
915 isl_ctx *Context)
916 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
917 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000918 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000919 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000920
921 SmallVector<Loop*, 8> NestLoops;
922 SmallVector<unsigned, 8> Scatter;
923
924 Scatter.assign(MaxLoopDepth + 1, 0);
925
926 // Build the iteration domain, access functions and scattering functions
927 // traversing the region tree.
928 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
929 Stmts.push_back(new ScopStmt(*this, Scatter));
930
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000931 realignParams();
932
Tobias Grosser75805372011-04-29 06:27:02 +0000933 assert(NestLoops.empty() && "NestLoops not empty at top level!");
934}
935
936Scop::~Scop() {
937 isl_set_free(Context);
938
939 // Free the statements;
940 for (iterator I = begin(), E = end(); I != E; ++I)
941 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000942}
943
944std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000945 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000946}
947
948std::string Scop::getNameStr() const {
949 std::string ExitName, EntryName;
950 raw_string_ostream ExitStr(ExitName);
951 raw_string_ostream EntryStr(EntryName);
952
953 WriteAsOperand(EntryStr, R.getEntry(), false);
954 EntryStr.str();
955
956 if (R.getExit()) {
957 WriteAsOperand(ExitStr, R.getExit(), false);
958 ExitStr.str();
959 } else
960 ExitName = "FunctionExit";
961
962 return EntryName + "---" + ExitName;
963}
964
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000965__isl_give isl_set *Scop::getContext() const {
966 return isl_set_copy(Context);
967}
Tobias Grosser37487052011-10-06 00:03:42 +0000968__isl_give isl_space *Scop::getParamSpace() const {
969 return isl_set_get_space(this->Context);
970}
971
Tobias Grosser75805372011-04-29 06:27:02 +0000972void Scop::printContext(raw_ostream &OS) const {
973 OS << "Context:\n";
974
975 if (!Context) {
976 OS.indent(4) << "n/a\n\n";
977 return;
978 }
979
980 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000981
982 for (ParamVecType::const_iterator PI = Parameters.begin(),
983 PE = Parameters.end(); PI != PE; ++PI) {
984 const SCEV *Parameter = *PI;
985 int Dim = ParameterIds.find(Parameter)->second;
986
987 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
988 }
Tobias Grosser75805372011-04-29 06:27:02 +0000989}
990
991void Scop::printStatements(raw_ostream &OS) const {
992 OS << "Statements {\n";
993
994 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
995 OS.indent(4) << (**SI);
996
997 OS.indent(4) << "}\n";
998}
999
1000
1001void Scop::print(raw_ostream &OS) const {
1002 printContext(OS.indent(4));
1003 printStatements(OS.indent(4));
1004}
1005
1006void Scop::dump() const { print(dbgs()); }
1007
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001008isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001009
1010ScalarEvolution *Scop::getSE() const { return SE; }
1011
1012bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1013 if (tempScop.getAccessFunctions(BB))
1014 return false;
1015
1016 return true;
1017}
1018
1019void Scop::buildScop(TempScop &tempScop,
1020 const Region &CurRegion,
1021 SmallVectorImpl<Loop*> &NestLoops,
1022 SmallVectorImpl<unsigned> &Scatter,
1023 LoopInfo &LI) {
1024 Loop *L = castToLoop(CurRegion, LI);
1025
1026 if (L)
1027 NestLoops.push_back(L);
1028
1029 unsigned loopDepth = NestLoops.size();
1030 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1031
1032 for (Region::const_element_iterator I = CurRegion.element_begin(),
1033 E = CurRegion.element_end(); I != E; ++I)
1034 if (I->isSubRegion())
1035 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1036 else {
1037 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1038
1039 if (isTrivialBB(BB, tempScop))
1040 continue;
1041
1042 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
1043 Scatter));
1044
1045 // Increasing the Scattering function is OK for the moment, because
1046 // we are using a depth first iterator and the program is well structured.
1047 ++Scatter[loopDepth];
1048 }
1049
1050 if (!L)
1051 return;
1052
1053 // Exiting a loop region.
1054 Scatter[loopDepth] = 0;
1055 NestLoops.pop_back();
1056 ++Scatter[loopDepth-1];
1057}
1058
1059//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001060ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1061 ctx = isl_ctx_alloc();
1062}
1063
1064ScopInfo::~ScopInfo() {
1065 clear();
1066 isl_ctx_free(ctx);
1067}
1068
1069
Tobias Grosser75805372011-04-29 06:27:02 +00001070
1071void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1072 AU.addRequired<LoopInfo>();
1073 AU.addRequired<RegionInfo>();
1074 AU.addRequired<ScalarEvolution>();
1075 AU.addRequired<TempScopInfo>();
1076 AU.setPreservesAll();
1077}
1078
1079bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1080 LoopInfo &LI = getAnalysis<LoopInfo>();
1081 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1082
1083 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1084
1085 // This region is no Scop.
1086 if (!tempScop) {
1087 scop = 0;
1088 return false;
1089 }
1090
1091 // Statistics.
1092 ++ScopFound;
1093 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1094
Tobias Grosserb76f38532011-08-20 11:11:25 +00001095 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001096
1097 return false;
1098}
1099
1100char ScopInfo::ID = 0;
1101
Tobias Grosser73600b82011-10-08 00:30:40 +00001102INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1103 "Polly - Create polyhedral description of Scops", false,
1104 false)
1105INITIALIZE_PASS_DEPENDENCY(LoopInfo)
1106INITIALIZE_PASS_DEPENDENCY(RegionInfo)
1107INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1108INITIALIZE_PASS_DEPENDENCY(TempScopInfo)
1109INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1110 "Polly - Create polyhedral description of Scops", false,
1111 false)
Tobias Grosser75805372011-04-29 06:27:02 +00001112
1113Pass *polly::createScopInfoPass() {
1114 return new ScopInfo();
1115}