blob: 5d898fc0d65b6d98f437c8bb47a48b77295a250e [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"
26
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/Analysis/RegionIterator.h"
30#include "llvm/Assembly/Writer.h"
31#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/SetVector.h"
33#include "llvm/Support/CommandLine.h"
34
35#define DEBUG_TYPE "polly-scops"
36#include "llvm/Support/Debug.h"
37
38#include "isl/constraint.h"
39#include "isl/set.h"
40#include "isl/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 Grosser75805372011-04-29 06:27:02 +000044#include <sstream>
45#include <string>
46#include <vector>
47
48using namespace llvm;
49using namespace polly;
50
51STATISTIC(ScopFound, "Number of valid Scops");
52STATISTIC(RichScopFound, "Number of Scops containing a loop");
53
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000054/// Convert an int into a string.
55static std::string convertInt(int number)
56{
57 if (number == 0)
58 return "0";
59 std::string temp = "";
60 std::string returnvalue = "";
61 while (number > 0)
62 {
63 temp += number % 10 + 48;
64 number /= 10;
65 }
66 for (unsigned i = 0; i < temp.length(); i++)
67 returnvalue+=temp[temp.length() - i - 1];
68 return returnvalue;
Tobias Grosser75805372011-04-29 06:27:02 +000069}
70
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000071/// Translate a SCEVExpression into an isl_pw_aff object.
72struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff*> {
73private:
74 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000075 int NbLoopSpaces;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000076 const Scop *scop;
77
78 /// baseAdress is set if we analyze a memory access. It holds the base address
79 /// of this memory access.
80 const Value *baseAddress;
81
82public:
83 static isl_pw_aff *getPwAff(const ScopStmt *stmt, const SCEV *scev,
Tobias Grosser9b13d3d2011-10-06 22:32:58 +000084 const Value *baseAddress = 0) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000085 SCEVAffinator Affinator(stmt, baseAddress);
86 return Affinator.visit(scev);
87 }
88
89 isl_pw_aff *visit(const SCEV *scev) {
90 // In case the scev is contained in our list of parameters, we do not
91 // further analyze this expression, but create a new parameter in the
92 // isl_pw_aff. This allows us to treat subexpressions that we cannot
93 // translate into an piecewise affine expression, as constant parameters of
94 // the piecewise affine expression.
95 int i = 0;
96 for (Scop::const_param_iterator PI = scop->param_begin(),
97 PE = scop->param_end(); PI != PE; ++PI) {
98 if (*PI == scev) {
99 isl_id *ID = isl_id_alloc(ctx, ("p" + convertInt(i)).c_str(),
100 (void *) scev);
Tobias Grosserf5338802011-10-06 00:03:35 +0000101 isl_space *Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
102 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000103
Tobias Grosserf5338802011-10-06 00:03:35 +0000104 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
105 isl_aff *Affine = isl_aff_zero_on_domain(
106 isl_local_space_from_space(Space));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000107 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
108
109 return isl_pw_aff_alloc(Domain, Affine);
110 }
111 i++;
112 }
113
114 return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev);
115 }
116
117 SCEVAffinator(const ScopStmt *stmt, const Value *baseAddress) :
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000118 ctx(stmt->getIslCtx()),
Tobias Grosserf5338802011-10-06 00:03:35 +0000119 NbLoopSpaces(stmt->getNumIterators()),
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000120 scop(stmt->getParent()),
121 baseAddress(baseAddress) {};
122
123 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) {
124 ConstantInt *Value = Constant->getValue();
125 isl_int v;
126 isl_int_init(v);
127
128 // LLVM does not define if an integer value is interpreted as a signed or
129 // unsigned value. Hence, without further information, it is unknown how
130 // this value needs to be converted to GMP. At the moment, we only support
131 // signed operations. So we just interpret it as signed. Later, there are
132 // two options:
133 //
134 // 1. We always interpret any value as signed and convert the values on
135 // demand.
136 // 2. We pass down the signedness of the calculation and use it to interpret
137 // this constant correctly.
138 MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true);
139
Tobias Grosserf5338802011-10-06 00:03:35 +0000140 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
141 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
142 isl_aff *Affine = isl_aff_zero_on_domain(ls);
143 isl_set *Domain = isl_set_universe(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000144
145 Affine = isl_aff_add_constant(Affine, v);
146 isl_int_clear(v);
147
148 return isl_pw_aff_alloc(Domain, Affine);
149 }
150
151 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr* Expr) {
152 assert(0 && "Not yet supported");
153 }
154
155 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) {
156 assert(0 && "Not yet supported");
157 }
158
159 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr* Expr) {
160 // Assuming the value is signed, a sign extension is basically a noop.
161 // TODO: Reconsider this as soon as we support unsigned values.
162 return visit(Expr->getOperand());
163 }
164
165 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr* Expr) {
166 isl_pw_aff *Sum = visit(Expr->getOperand(0));
167
168 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
169 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
170 Sum = isl_pw_aff_add(Sum, NextSummand);
171 }
172
173 // TODO: Check for NSW and NUW.
174
175 return Sum;
176 }
177
178 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr* Expr) {
179 isl_pw_aff *Product = visit(Expr->getOperand(0));
180
181 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
182 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
183
184 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
185 isl_pw_aff_free(Product);
186 isl_pw_aff_free(NextOperand);
187 return NULL;
188 }
189
190 Product = isl_pw_aff_mul(Product, NextOperand);
191 }
192
193 // TODO: Check for NSW and NUW.
194 return Product;
195 }
196
197 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr* Expr) {
198 assert(0 && "Not yet supported");
199 }
200
201 int getLoopDepth(const Loop *L) {
202 Loop *outerLoop =
203 scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L));
204 return L->getLoopDepth() - outerLoop->getLoopDepth();
205 }
206
207 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr* Expr) {
208 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
209
210 isl_pw_aff *Start = visit(Expr->getStart());
211 isl_pw_aff *Step = visit(Expr->getOperand(1));
Tobias Grosserf5338802011-10-06 00:03:35 +0000212 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
213 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000214
215 int loopDimension = getLoopDepth(Expr->getLoop());
216
Tobias Grosserf5338802011-10-06 00:03:35 +0000217 isl_aff *LAff = isl_aff_set_coefficient_si(
218 isl_aff_zero_on_domain (LocalSpace), isl_dim_in, loopDimension, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000219 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
220
221 // TODO: Do we need to check for NSW and NUW?
222 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
223 }
224
225 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr* Expr) {
226 isl_pw_aff *Max = visit(Expr->getOperand(0));
227
228 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
229 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
230 Max = isl_pw_aff_max(Max, NextOperand);
231 }
232
233 return Max;
234 }
235
236 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr* Expr) {
237 assert(0 && "Not yet supported");
238 }
239
240 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown* Expr) {
241 Value *Value = Expr->getValue();
242
Tobias Grosserf5338802011-10-06 00:03:35 +0000243 isl_space *Space;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000244
245 /// If baseAddress is set, we ignore its Value object in the scev and do not
246 /// add it to the isl_pw_aff. This is because it is regarded as defining the
247 /// name of an array, in contrast to its array subscript.
248 if (baseAddress != Value) {
249 isl_id *ID = isl_id_alloc(ctx, Value->getNameStr().c_str(), Value);
Tobias Grosserf5338802011-10-06 00:03:35 +0000250 Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
251 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000252 } else {
Tobias Grosserf5338802011-10-06 00:03:35 +0000253 Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000254 }
255
Tobias Grosserf5338802011-10-06 00:03:35 +0000256 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
257 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000258
259 if (baseAddress != Value)
260 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
261
262 return isl_pw_aff_alloc(Domain, Affine);
263 }
264};
265
Tobias Grosser75805372011-04-29 06:27:02 +0000266//===----------------------------------------------------------------------===//
267
268MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000269 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000270 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000271}
272
273static void replace(std::string& str, const std::string& find,
274 const std::string& replace) {
275 size_t pos = 0;
276 while((pos = str.find(find, pos)) != std::string::npos)
277 {
278 str.replace(pos, find.length(), replace);
279 pos += replace.length();
280 }
281}
282
283static void makeIslCompatible(std::string& str) {
Tobias Grossereec4d56e2011-08-20 11:11:14 +0000284 str.erase(0, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000285 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000286 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000287}
288
289void MemoryAccess::setBaseName() {
290 raw_string_ostream OS(BaseName);
291 WriteAsOperand(OS, getBaseAddr(), false);
292 BaseName = OS.str();
293
Tobias Grosser75805372011-04-29 06:27:02 +0000294 makeIslCompatible(BaseName);
295 BaseName = "MemRef_" + BaseName;
296}
297
Tobias Grosser5d453812011-10-06 00:04:11 +0000298isl_map *MemoryAccess::getAccessRelation() const {
299 return isl_map_copy(AccessRelation);
300}
301
302std::string MemoryAccess::getAccessRelationStr() const {
303 return stringFromIslObj(AccessRelation);
304}
305
306isl_map *MemoryAccess::getNewAccessRelation() const {
307 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000308}
309
310isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000311 isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0,
Tobias Grosserf5338802011-10-06 00:03:35 +0000312 Statement->getNumIterators(), 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000313 setBaseName();
314
Tobias Grosserf5338802011-10-06 00:03:35 +0000315 Space = isl_space_set_tuple_name(Space, isl_dim_out, getBaseName().c_str());
316 Space = isl_space_set_tuple_name(Space, isl_dim_in, Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000317
Tobias Grosserf5338802011-10-06 00:03:35 +0000318 return isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000319}
320
321MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000322 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000323 BaseAddr = AffFunc.getBaseAddr();
324 Type = AffFunc.isRead() ? Read : Write;
325 statement = Statement;
326
327 setBaseName();
328
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000329 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, AffFunc.OriginalSCEV,
330 AffFunc.getBaseAddr());
Tobias Grosser75805372011-04-29 06:27:02 +0000331
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000332 // Devide the access function by the size of the elements in the array.
333 //
334 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
335 // as something like A[i * elementsize]. This hides the fact that two
336 // subsequent values of 'i' index two values that are stored next to each
337 // other in memory. By this devision we make this characteristic obvious
338 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000339 isl_int v;
340 isl_int_init(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000341 isl_int_set_si(v, AffFunc.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000342 Affine = isl_pw_aff_scale_down(Affine, v);
343 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000344
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000345 AccessRelation = isl_map_from_pw_aff(Affine);
346 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
347 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000348 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
349 getBaseName().c_str());
Tobias Grosser30b8a092011-08-18 07:51:37 +0000350
Tobias Grosser37487052011-10-06 00:03:42 +0000351 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
352 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000353}
354
355MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000356 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000357 BaseAddr = BaseAddress;
358 Type = Read;
359 statement = Statement;
360
361 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
362 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000363 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
364 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000365}
366
367void MemoryAccess::print(raw_ostream &OS) const {
368 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000369 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000370}
371
372void MemoryAccess::dump() const {
373 print(errs());
374}
375
376// Create a map in the size of the provided set domain, that maps from the
377// one element of the provided set domain to another element of the provided
378// set domain.
379// The mapping is limited to all points that are equal in all but the last
380// dimension and for which the last dimension of the input is strict smaller
381// than the last dimension of the output.
382//
383// getEqualAndLarger(set[i0, i1, ..., iX]):
384//
385// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
386// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
387//
Tobias Grosserf5338802011-10-06 00:03:35 +0000388static isl_map *getEqualAndLarger(isl_space *setDomain) {
389 isl_space *mapDomain = isl_space_map_from_set(setDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000390 isl_basic_map *bmap = isl_basic_map_universe(mapDomain);
Tobias Grosserf5338802011-10-06 00:03:35 +0000391 isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000392
393 // Set all but the last dimension to be equal for the input and output
394 //
395 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
396 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
397 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
398 isl_int v;
399 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000400 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000401
402 isl_int_set_si(v, 1);
403 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
404 isl_int_set_si(v, -1);
405 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
406
407 bmap = isl_basic_map_add_constraint(bmap, c);
408
409 isl_int_clear(v);
410 }
411
412 // Set the last dimension of the input to be strict smaller than the
413 // last dimension of the output.
414 //
415 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
416 //
417 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
418 isl_int v;
419 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000420 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000421 isl_int_set_si(v, -1);
422 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
423 isl_int_set_si(v, 1);
424 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
425 isl_int_set_si(v, -1);
426 isl_constraint_set_constant(c, v);
427 isl_int_clear(v);
428
429 bmap = isl_basic_map_add_constraint(bmap, c);
430
431 return isl_map_from_basic_map(bmap);
432}
433
434isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000435 isl_map *accessRelation = getAccessRelation();
Tobias Grosser75805372011-04-29 06:27:02 +0000436 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
Tobias Grossercf3942d2011-10-06 00:04:05 +0000437 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000438
439 scattering = isl_map_reverse(scattering);
440 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
441 scattering = isl_map_project_out(scattering, isl_dim_in,
442 isl_set_n_dim(scatteringDomain),
443 difference);
444
445 // Remove all names of the scattering dimensions, as the names may be lost
446 // anyways during the project. This leads to consistent results.
447 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
448 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
449
Tobias Grosserf5338802011-10-06 00:03:35 +0000450 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000451 nextScatt = isl_map_lexmin(nextScatt);
452
453 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
454
455 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
456 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
457 nextScatt = isl_map_apply_domain(nextScatt, scattering);
458 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
459
460 return isl_map_deltas(nextScatt);
461}
462
463bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
464 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000465 isl_space *StrideSpace = isl_set_get_space(stride);
466 isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace);
467 isl_constraint *c = isl_equality_alloc(StrideLS);
Tobias Grosser75805372011-04-29 06:27:02 +0000468
469 isl_int v;
470 isl_int_init(v);
471 isl_int_set_si(v, 1);
472 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
473 isl_int_set_si(v, 0);
474 isl_constraint_set_constant(c, v);
475 isl_int_clear(v);
476
Tobias Grosserf5338802011-10-06 00:03:35 +0000477 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000478
479 bset = isl_basic_set_add_constraint(bset, c);
480 isl_set *strideZero = isl_set_from_basic_set(bset);
481
Tobias Grosserb76f38532011-08-20 11:11:25 +0000482 bool isStrideZero = isl_set_is_equal(stride, strideZero);
483
484 isl_set_free(strideZero);
485 isl_set_free(stride);
486
487 return isStrideZero;
Tobias Grosser75805372011-04-29 06:27:02 +0000488}
489
490bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
491 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000492 isl_space *StrideSpace = isl_set_get_space(stride);
493 isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace);
494 isl_constraint *c = isl_equality_alloc(StrideLSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000495
496 isl_int v;
497 isl_int_init(v);
498 isl_int_set_si(v, 1);
499 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
500 isl_int_set_si(v, -1);
501 isl_constraint_set_constant(c, v);
502 isl_int_clear(v);
503
Tobias Grosserf5338802011-10-06 00:03:35 +0000504 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000505
506 bset = isl_basic_set_add_constraint(bset, c);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000507 isl_set *strideOne = isl_set_from_basic_set(bset);
Tobias Grosser75805372011-04-29 06:27:02 +0000508
Tobias Grosserb76f38532011-08-20 11:11:25 +0000509 bool isStrideOne = isl_set_is_equal(stride, strideOne);
510
511 isl_set_free(strideOne);
512 isl_set_free(stride);
513
514 return isStrideOne;
Tobias Grosser75805372011-04-29 06:27:02 +0000515}
516
Tobias Grosser5d453812011-10-06 00:04:11 +0000517void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000518 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000519 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000520}
Tobias Grosser75805372011-04-29 06:27:02 +0000521
522//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000523
524isl_map *ScopStmt::getScattering() const {
525 return isl_map_copy(Scattering);
526}
527
528void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000529 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000530 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000531}
532
Tobias Grosser75805372011-04-29 06:27:02 +0000533void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
534 unsigned NumberOfIterators = getNumIterators();
Tobias Grosserf5338802011-10-06 00:03:35 +0000535 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000536 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators,
Tobias Grosserf5338802011-10-06 00:03:35 +0000537 ScatSpace);
538 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
539 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
540 isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space));
541 isl_basic_map *bmap = isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000542 isl_int v;
543 isl_int_init(v);
544
545 // Loop dimensions.
546 for (unsigned i = 0; i < NumberOfIterators; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000547 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000548 isl_int_set_si(v, 1);
549 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
550 isl_int_set_si(v, -1);
551 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
552
553 bmap = isl_basic_map_add_constraint(bmap, c);
554 }
555
556 // Constant dimensions
557 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000558 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000559 isl_int_set_si(v, -1);
560 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
561 isl_int_set_si(v, Scatter[i]);
562 isl_constraint_set_constant(c, v);
563
564 bmap = isl_basic_map_add_constraint(bmap, c);
565 }
566
567 // Fill scattering dimensions.
Tobias Grosserf5338802011-10-06 00:03:35 +0000568 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) {
569 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000570 isl_int_set_si(v, 1);
571 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
572 isl_int_set_si(v, 0);
573 isl_constraint_set_constant(c, v);
574
575 bmap = isl_basic_map_add_constraint(bmap, c);
576 }
577
578 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000579 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000580 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000581}
582
583void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
584 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
585
586 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
587 E = AccFuncs->end(); I != E; ++I) {
588 MemAccs.push_back(new MemoryAccess(I->first, this));
589 InstructionToAccess[I->second] = MemAccs.back();
590 }
591}
592
Tobias Grosserf5338802011-10-06 00:03:35 +0000593isl_set *ScopStmt::toConditionSet(const Comparison &Comp,
594 isl_space *space) const {
Tobias Grosserd2795d02011-08-18 07:51:40 +0000595 isl_pw_aff *LHS = SCEVAffinator::getPwAff(this, Comp.getLHS()->OriginalSCEV,
596 0);
597 isl_pw_aff *RHS = SCEVAffinator::getPwAff(this, Comp.getRHS()->OriginalSCEV,
598 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000599
Tobias Grosserd2795d02011-08-18 07:51:40 +0000600 isl_set *set;
Tobias Grosser75805372011-04-29 06:27:02 +0000601
Tobias Grosserd2795d02011-08-18 07:51:40 +0000602 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000603 case ICmpInst::ICMP_EQ:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000604 set = isl_pw_aff_eq_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000605 break;
606 case ICmpInst::ICMP_NE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000607 set = isl_pw_aff_ne_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000608 break;
609 case ICmpInst::ICMP_SLT:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000610 set = isl_pw_aff_lt_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000611 break;
612 case ICmpInst::ICMP_SLE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000613 set = isl_pw_aff_le_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000614 break;
Tobias Grosserd2795d02011-08-18 07:51:40 +0000615 case ICmpInst::ICMP_SGT:
616 set = isl_pw_aff_gt_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000617 break;
618 case ICmpInst::ICMP_SGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000619 set = isl_pw_aff_ge_set(LHS, RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000620 break;
Tobias Grosserd2795d02011-08-18 07:51:40 +0000621 case ICmpInst::ICMP_ULT:
622 case ICmpInst::ICMP_UGT:
623 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000624 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000625 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000626 default:
627 llvm_unreachable("Non integer predicate not supported");
628 }
629
Tobias Grosserf5338802011-10-06 00:03:35 +0000630 set = isl_set_set_tuple_name(set, isl_space_get_tuple_name(space, isl_dim_set));
Tobias Grosserd2795d02011-08-18 07:51:40 +0000631
632 return set;
Tobias Grosser75805372011-04-29 06:27:02 +0000633}
634
Tobias Grosser75805372011-04-29 06:27:02 +0000635void ScopStmt::buildIterationDomainFromLoops(TempScop &tempScop) {
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000636 isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
Tobias Grosser75805372011-04-29 06:27:02 +0000637
Tobias Grosserf5338802011-10-06 00:03:35 +0000638 Domain = isl_set_universe(isl_space_copy(Space));
Tobias Grosser37487052011-10-06 00:03:42 +0000639 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000640 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000641
Tobias Grosser75805372011-04-29 06:27:02 +0000642 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000643 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
644 isl_pw_aff *IV = isl_pw_aff_from_aff(
645 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000646
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000647 // 0 <= IV.
648 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
649 Domain = isl_set_intersect(Domain, LowerBound);
650
651 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000652 const Loop *L = getLoopForDimension(i);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000653 const SCEV *LatchExecutions = tempScop.getLoopBound(L).OriginalSCEV;
654 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
655 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000656 Domain = isl_set_intersect(Domain, UpperBoundSet);
657 }
658
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000659 Domain = isl_set_set_tuple_name(Domain, getBaseName());
Tobias Grosserf5338802011-10-06 00:03:35 +0000660 isl_local_space_free(LocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000661}
662
663void ScopStmt::addConditionsToDomain(TempScop &tempScop,
664 const Region &CurRegion) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000665 isl_space *Space = isl_set_get_space(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000666 const Region *TopR = tempScop.getMaxRegion().getParent(),
667 *CurR = &CurRegion;
668 const BasicBlock *CurEntry = BB;
669
670 // Build BB condition constrains, by traveling up the region tree.
671 do {
672 assert(CurR && "We exceed the top region?");
673 // Skip when multiple regions share the same entry.
674 if (CurEntry != CurR->getEntry()) {
675 if (const BBCond *Cnd = tempScop.getBBCond(CurEntry))
676 for (BBCond::const_iterator I = Cnd->begin(), E = Cnd->end();
677 I != E; ++I) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000678 isl_set *c = toConditionSet(*I, Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000679 Domain = isl_set_intersect(Domain, c);
680 }
681 }
682 CurEntry = CurR->getEntry();
683 CurR = CurR->getParent();
684 } while (TopR != CurR);
685
Tobias Grosserf5338802011-10-06 00:03:35 +0000686 isl_space_free(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000687}
688
689void ScopStmt::buildIterationDomain(TempScop &tempScop, const Region &CurRegion)
690{
691 buildIterationDomainFromLoops(tempScop);
692 addConditionsToDomain(tempScop, CurRegion);
693}
694
695ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
696 const Region &CurRegion, BasicBlock &bb,
697 SmallVectorImpl<Loop*> &NestLoops,
698 SmallVectorImpl<unsigned> &Scatter)
699 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
700 // Setup the induction variables.
701 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
702 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
703 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000704 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000705 }
706
707 raw_string_ostream OS(BaseName);
708 WriteAsOperand(OS, &bb, false);
709 BaseName = OS.str();
710
Tobias Grosser75805372011-04-29 06:27:02 +0000711 makeIslCompatible(BaseName);
712 BaseName = "Stmt_" + BaseName;
713
714 buildIterationDomain(tempScop, CurRegion);
715 buildScattering(Scatter);
716 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000717}
718
719ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
720 : Parent(parent), BB(NULL), IVS(0) {
721
722 BaseName = "FinalRead";
723
724 // Build iteration domain.
725 std::string IterationDomainString = "{[i0] : i0 = 0}";
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000726 Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str());
Tobias Grosser75805372011-04-29 06:27:02 +0000727 Domain = isl_set_set_tuple_name(Domain, getBaseName());
Tobias Grosser37487052011-10-06 00:03:42 +0000728 Domain = isl_set_align_params(Domain, parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000729
730 // Build scattering.
Tobias Grosserf5338802011-10-06 00:03:35 +0000731 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000732 isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace);
Tobias Grosserf5338802011-10-06 00:03:35 +0000733 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
734 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
735 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000736 isl_int v;
737 isl_int_init(v);
738
Tobias Grosserf5338802011-10-06 00:03:35 +0000739 isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000740 isl_int_set_si(v, -1);
741 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
742
743 // TODO: This is incorrect. We should not use a very large number to ensure
744 // that this statement is executed last.
745 isl_int_set_si(v, 200000000);
746 isl_constraint_set_constant(c, v);
747
748 bmap = isl_basic_map_add_constraint(bmap, c);
749 isl_int_clear(v);
750 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000751 Scattering = isl_map_align_params(Scattering, parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000752
753 // Build memory accesses, use SetVector to keep the order of memory accesses
754 // and prevent the same memory access inserted more than once.
755 SetVector<const Value*> BaseAddressSet;
756
757 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
758 ++SI) {
759 ScopStmt *Stmt = *SI;
760
761 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
762 E = Stmt->memacc_end(); I != E; ++I)
763 BaseAddressSet.insert((*I)->getBaseAddr());
764 }
765
766 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
767 BE = BaseAddressSet.end(); BI != BE; ++BI)
768 MemAccs.push_back(new MemoryAccess(*BI, this));
Tobias Grosser75805372011-04-29 06:27:02 +0000769}
770
771std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000772 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000773}
774
775std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000776 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000777}
778
779unsigned ScopStmt::getNumParams() const {
780 return Parent.getNumParams();
781}
782
783unsigned ScopStmt::getNumIterators() const {
784 // The final read has one dimension with one element.
785 if (!BB)
786 return 1;
787
788 return IVS.size();
789}
790
791unsigned ScopStmt::getNumScattering() const {
792 return isl_map_dim(Scattering, isl_dim_out);
793}
794
795const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
796
797const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
798 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000799 return IVS[Dimension].first;
800}
801
802const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
803 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000804}
805
806const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
807 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000808 PHINode *PN =
809 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000810 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
811}
812
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000813isl_ctx *ScopStmt::getIslCtx() const {
814 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000815}
816
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000817isl_set *ScopStmt::getDomain() const {
818 return isl_set_copy(Domain);
819}
820
Tobias Grosser75805372011-04-29 06:27:02 +0000821ScopStmt::~ScopStmt() {
822 while (!MemAccs.empty()) {
823 delete MemAccs.back();
824 MemAccs.pop_back();
825 }
826
827 isl_set_free(Domain);
828 isl_map_free(Scattering);
829}
830
831void ScopStmt::print(raw_ostream &OS) const {
832 OS << "\t" << getBaseName() << "\n";
833
834 OS.indent(12) << "Domain :=\n";
835
836 if (Domain) {
837 OS.indent(16) << getDomainStr() << ";\n";
838 } else
839 OS.indent(16) << "n/a\n";
840
841 OS.indent(12) << "Scattering :=\n";
842
843 if (Domain) {
844 OS.indent(16) << getScatteringStr() << ";\n";
845 } else
846 OS.indent(16) << "n/a\n";
847
848 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
849 I != E; ++I)
850 (*I)->print(OS);
851}
852
853void ScopStmt::dump() const { print(dbgs()); }
854
855//===----------------------------------------------------------------------===//
856/// Scop class implement
Tobias Grosser75805372011-04-29 06:27:02 +0000857
Tobias Grosser0e27e242011-10-06 00:03:48 +0000858void Scop::buildContext(isl_ctx *IslCtx, ParamSetType *ParamSet) {
859 isl_space *Space = isl_space_params_alloc(IslCtx, ParamSet->size());
Tobias Grosser75805372011-04-29 06:27:02 +0000860
Tobias Grosser30b8a092011-08-18 07:51:37 +0000861 int i = 0;
Tobias Grosser0e27e242011-10-06 00:03:48 +0000862 for (ParamSetType::iterator PI = ParamSet->begin(), PE = ParamSet->end();
Tobias Grosser30b8a092011-08-18 07:51:37 +0000863 PI != PE; ++PI) {
Tobias Grosser0e27e242011-10-06 00:03:48 +0000864 const SCEV *Parameter = *PI;
865 Parameters.push_back(Parameter);
866 std::string ParameterName = "p" + convertInt(i);
867 isl_id *id = isl_id_alloc(IslCtx, ParameterName.c_str(),
868 (void *) Parameter);
Tobias Grosserf5338802011-10-06 00:03:35 +0000869 Space = isl_space_set_dim_id(Space, isl_dim_param, i, id);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000870 i++;
871 }
872
Tobias Grosser75805372011-04-29 06:27:02 +0000873 // TODO: Insert relations between parameters.
874 // TODO: Insert constraints on parameters.
Tobias Grosserf5338802011-10-06 00:03:35 +0000875 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000876}
877
878Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
879 isl_ctx *Context)
880 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
881 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
882 buildContext(Context, &tempScop.getParamSet());
Tobias Grosser75805372011-04-29 06:27:02 +0000883
884 SmallVector<Loop*, 8> NestLoops;
885 SmallVector<unsigned, 8> Scatter;
886
887 Scatter.assign(MaxLoopDepth + 1, 0);
888
889 // Build the iteration domain, access functions and scattering functions
890 // traversing the region tree.
891 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
892 Stmts.push_back(new ScopStmt(*this, Scatter));
893
Tobias Grosser75805372011-04-29 06:27:02 +0000894 assert(NestLoops.empty() && "NestLoops not empty at top level!");
895}
896
897Scop::~Scop() {
898 isl_set_free(Context);
899
900 // Free the statements;
901 for (iterator I = begin(), E = end(); I != E; ++I)
902 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000903}
904
905std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000906 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000907}
908
909std::string Scop::getNameStr() const {
910 std::string ExitName, EntryName;
911 raw_string_ostream ExitStr(ExitName);
912 raw_string_ostream EntryStr(EntryName);
913
914 WriteAsOperand(EntryStr, R.getEntry(), false);
915 EntryStr.str();
916
917 if (R.getExit()) {
918 WriteAsOperand(ExitStr, R.getExit(), false);
919 ExitStr.str();
920 } else
921 ExitName = "FunctionExit";
922
923 return EntryName + "---" + ExitName;
924}
925
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000926__isl_give isl_set *Scop::getContext() const {
927 return isl_set_copy(Context);
928}
Tobias Grosser37487052011-10-06 00:03:42 +0000929__isl_give isl_space *Scop::getParamSpace() const {
930 return isl_set_get_space(this->Context);
931}
932
Tobias Grosser75805372011-04-29 06:27:02 +0000933void Scop::printContext(raw_ostream &OS) const {
934 OS << "Context:\n";
935
936 if (!Context) {
937 OS.indent(4) << "n/a\n\n";
938 return;
939 }
940
941 OS.indent(4) << getContextStr() << "\n";
942}
943
944void Scop::printStatements(raw_ostream &OS) const {
945 OS << "Statements {\n";
946
947 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
948 OS.indent(4) << (**SI);
949
950 OS.indent(4) << "}\n";
951}
952
953
954void Scop::print(raw_ostream &OS) const {
955 printContext(OS.indent(4));
956 printStatements(OS.indent(4));
957}
958
959void Scop::dump() const { print(dbgs()); }
960
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000961isl_ctx *Scop::getIslCtx() const { return isl_set_get_ctx(Context); }
Tobias Grosser75805372011-04-29 06:27:02 +0000962
963ScalarEvolution *Scop::getSE() const { return SE; }
964
965bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
966 if (tempScop.getAccessFunctions(BB))
967 return false;
968
969 return true;
970}
971
972void Scop::buildScop(TempScop &tempScop,
973 const Region &CurRegion,
974 SmallVectorImpl<Loop*> &NestLoops,
975 SmallVectorImpl<unsigned> &Scatter,
976 LoopInfo &LI) {
977 Loop *L = castToLoop(CurRegion, LI);
978
979 if (L)
980 NestLoops.push_back(L);
981
982 unsigned loopDepth = NestLoops.size();
983 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
984
985 for (Region::const_element_iterator I = CurRegion.element_begin(),
986 E = CurRegion.element_end(); I != E; ++I)
987 if (I->isSubRegion())
988 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
989 else {
990 BasicBlock *BB = I->getNodeAs<BasicBlock>();
991
992 if (isTrivialBB(BB, tempScop))
993 continue;
994
995 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
996 Scatter));
997
998 // Increasing the Scattering function is OK for the moment, because
999 // we are using a depth first iterator and the program is well structured.
1000 ++Scatter[loopDepth];
1001 }
1002
1003 if (!L)
1004 return;
1005
1006 // Exiting a loop region.
1007 Scatter[loopDepth] = 0;
1008 NestLoops.pop_back();
1009 ++Scatter[loopDepth-1];
1010}
1011
1012//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001013ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1014 ctx = isl_ctx_alloc();
1015}
1016
1017ScopInfo::~ScopInfo() {
1018 clear();
1019 isl_ctx_free(ctx);
1020}
1021
1022
Tobias Grosser75805372011-04-29 06:27:02 +00001023
1024void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1025 AU.addRequired<LoopInfo>();
1026 AU.addRequired<RegionInfo>();
1027 AU.addRequired<ScalarEvolution>();
1028 AU.addRequired<TempScopInfo>();
1029 AU.setPreservesAll();
1030}
1031
1032bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1033 LoopInfo &LI = getAnalysis<LoopInfo>();
1034 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1035
1036 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1037
1038 // This region is no Scop.
1039 if (!tempScop) {
1040 scop = 0;
1041 return false;
1042 }
1043
1044 // Statistics.
1045 ++ScopFound;
1046 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1047
Tobias Grosserb76f38532011-08-20 11:11:25 +00001048 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001049
1050 return false;
1051}
1052
1053char ScopInfo::ID = 0;
1054
1055
1056static RegisterPass<ScopInfo>
1057X("polly-scops", "Polly - Create polyhedral description of Scops");
1058
1059Pass *polly::createScopInfoPass() {
1060 return new ScopInfo();
1061}