blob: bcb462d8e4ba2c56d37a861938201c47c7a066c8 [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 Grosser5683df42011-11-09 22:34:34 +000085 Value **BaseAddress = NULL) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000086 Scop *S = stmt->getParent();
87 const Region *Reg = &S->getRegion();
88
Tobias Grosser5683df42011-11-09 22:34:34 +000089 if (BaseAddress) {
90 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE(), BaseAddress));
Tobias Grosser60b54f12011-11-08 15:41:28 +000091 } else {
92 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE()));
93 }
94
Tobias Grosser5683df42011-11-09 22:34:34 +000095 Value *Base = BaseAddress ? *BaseAddress : NULL;
96 SCEVAffinator Affinator(stmt, Base);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000097 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
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000327MemoryAccess::MemoryAccess(const IRAccess &Access, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000328 newAccessRelation = NULL;
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000329 Type = Access.isRead() ? Read : Write;
Tobias Grosser75805372011-04-29 06:27:02 +0000330 statement = Statement;
331
Tobias Grosser9759f852011-11-10 12:44:55 +0000332 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, Access.getOffset());
333 BaseAddr = Access.getBase();
Tobias Grosser5683df42011-11-09 22:34:34 +0000334
335 setBaseName();
Tobias Grosser75805372011-04-29 06:27:02 +0000336
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000337 // Devide the access function by the size of the elements in the array.
338 //
339 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
340 // as something like A[i * elementsize]. This hides the fact that two
341 // subsequent values of 'i' index two values that are stored next to each
342 // other in memory. By this devision we make this characteristic obvious
343 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000344 isl_int v;
345 isl_int_init(v);
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000346 isl_int_set_si(v, Access.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000347 Affine = isl_pw_aff_scale_down(Affine, v);
348 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000349
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000350 AccessRelation = isl_map_from_pw_aff(Affine);
351 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
352 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000353 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
354 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000355}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000356
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000357void MemoryAccess::realignParams() {
358 isl_space *ParamSpace = statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000359 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000360}
361
362MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000363 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000364 BaseAddr = BaseAddress;
365 Type = Read;
366 statement = Statement;
367
368 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
369 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000370 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
371 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000372}
373
374void MemoryAccess::print(raw_ostream &OS) const {
375 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000376 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000377}
378
379void MemoryAccess::dump() const {
380 print(errs());
381}
382
383// Create a map in the size of the provided set domain, that maps from the
384// one element of the provided set domain to another element of the provided
385// set domain.
386// The mapping is limited to all points that are equal in all but the last
387// dimension and for which the last dimension of the input is strict smaller
388// than the last dimension of the output.
389//
390// getEqualAndLarger(set[i0, i1, ..., iX]):
391//
392// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
393// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
394//
Tobias Grosserf5338802011-10-06 00:03:35 +0000395static isl_map *getEqualAndLarger(isl_space *setDomain) {
396 isl_space *mapDomain = isl_space_map_from_set(setDomain);
Tobias Grosser23b36662011-10-17 08:32:36 +0000397 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(mapDomain));
Tobias Grosserf5338802011-10-06 00:03:35 +0000398 isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000399
400 // Set all but the last dimension to be equal for the input and output
401 //
402 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
403 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
404 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
405 isl_int v;
406 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000407 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000408
409 isl_int_set_si(v, 1);
410 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
411 isl_int_set_si(v, -1);
412 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
413
414 bmap = isl_basic_map_add_constraint(bmap, c);
415
416 isl_int_clear(v);
417 }
418
419 // Set the last dimension of the input to be strict smaller than the
420 // last dimension of the output.
421 //
422 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
423 //
424 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
425 isl_int v;
426 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000427 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000428 isl_int_set_si(v, -1);
429 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
430 isl_int_set_si(v, 1);
431 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
432 isl_int_set_si(v, -1);
433 isl_constraint_set_constant(c, v);
434 isl_int_clear(v);
435
436 bmap = isl_basic_map_add_constraint(bmap, c);
437
Tobias Grosser23b36662011-10-17 08:32:36 +0000438 isl_local_space_free(MapLocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000439 return isl_map_from_basic_map(bmap);
440}
441
442isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000443 isl_map *accessRelation = getAccessRelation();
Tobias Grosser75805372011-04-29 06:27:02 +0000444 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
Tobias Grossercf3942d2011-10-06 00:04:05 +0000445 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000446
447 scattering = isl_map_reverse(scattering);
448 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
449 scattering = isl_map_project_out(scattering, isl_dim_in,
450 isl_set_n_dim(scatteringDomain),
451 difference);
452
453 // Remove all names of the scattering dimensions, as the names may be lost
454 // anyways during the project. This leads to consistent results.
455 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
456 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
457
Tobias Grosserf5338802011-10-06 00:03:35 +0000458 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000459 nextScatt = isl_map_lexmin(nextScatt);
460
461 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
462
463 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
464 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
465 nextScatt = isl_map_apply_domain(nextScatt, scattering);
466 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
467
468 return isl_map_deltas(nextScatt);
469}
470
471bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
472 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000473 isl_space *StrideSpace = isl_set_get_space(stride);
474 isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace);
475 isl_constraint *c = isl_equality_alloc(StrideLS);
Tobias Grosser75805372011-04-29 06:27:02 +0000476
477 isl_int v;
478 isl_int_init(v);
479 isl_int_set_si(v, 1);
480 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
481 isl_int_set_si(v, 0);
482 isl_constraint_set_constant(c, v);
483 isl_int_clear(v);
484
Tobias Grosserf5338802011-10-06 00:03:35 +0000485 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000486
487 bset = isl_basic_set_add_constraint(bset, c);
488 isl_set *strideZero = isl_set_from_basic_set(bset);
489
Tobias Grosserb76f38532011-08-20 11:11:25 +0000490 bool isStrideZero = isl_set_is_equal(stride, strideZero);
491
492 isl_set_free(strideZero);
493 isl_set_free(stride);
494
495 return isStrideZero;
Tobias Grosser75805372011-04-29 06:27:02 +0000496}
497
498bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
499 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000500 isl_space *StrideSpace = isl_set_get_space(stride);
501 isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace);
502 isl_constraint *c = isl_equality_alloc(StrideLSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000503
504 isl_int v;
505 isl_int_init(v);
506 isl_int_set_si(v, 1);
507 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
508 isl_int_set_si(v, -1);
509 isl_constraint_set_constant(c, v);
510 isl_int_clear(v);
511
Tobias Grosserf5338802011-10-06 00:03:35 +0000512 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000513
514 bset = isl_basic_set_add_constraint(bset, c);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000515 isl_set *strideOne = isl_set_from_basic_set(bset);
Tobias Grosser75805372011-04-29 06:27:02 +0000516
Tobias Grosserb76f38532011-08-20 11:11:25 +0000517 bool isStrideOne = isl_set_is_equal(stride, strideOne);
518
519 isl_set_free(strideOne);
520 isl_set_free(stride);
521
522 return isStrideOne;
Tobias Grosser75805372011-04-29 06:27:02 +0000523}
524
Tobias Grosser5d453812011-10-06 00:04:11 +0000525void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000526 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000527 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000528}
Tobias Grosser75805372011-04-29 06:27:02 +0000529
530//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000531
532isl_map *ScopStmt::getScattering() const {
533 return isl_map_copy(Scattering);
534}
535
536void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000537 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000538 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000539}
540
Tobias Grosser75805372011-04-29 06:27:02 +0000541void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
542 unsigned NumberOfIterators = getNumIterators();
Tobias Grosserf5338802011-10-06 00:03:35 +0000543 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000544 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators,
Tobias Grosserf5338802011-10-06 00:03:35 +0000545 ScatSpace);
546 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
547 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
548 isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space));
549 isl_basic_map *bmap = isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000550 isl_int v;
551 isl_int_init(v);
552
553 // Loop dimensions.
554 for (unsigned i = 0; i < NumberOfIterators; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000555 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000556 isl_int_set_si(v, 1);
557 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
558 isl_int_set_si(v, -1);
559 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
560
561 bmap = isl_basic_map_add_constraint(bmap, c);
562 }
563
564 // Constant dimensions
565 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000566 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000567 isl_int_set_si(v, -1);
568 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
569 isl_int_set_si(v, Scatter[i]);
570 isl_constraint_set_constant(c, v);
571
572 bmap = isl_basic_map_add_constraint(bmap, c);
573 }
574
575 // Fill scattering dimensions.
Tobias Grosserf5338802011-10-06 00:03:35 +0000576 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) {
577 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000578 isl_int_set_si(v, 1);
579 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
580 isl_int_set_si(v, 0);
581 isl_constraint_set_constant(c, v);
582
583 bmap = isl_basic_map_add_constraint(bmap, c);
584 }
585
586 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000587 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000588 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser0ad4caa2011-10-08 00:35:17 +0000589 isl_local_space_free(LSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000590}
591
592void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
593 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
594
595 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
596 E = AccFuncs->end(); I != E; ++I) {
597 MemAccs.push_back(new MemoryAccess(I->first, this));
598 InstructionToAccess[I->second] = MemAccs.back();
599 }
600}
601
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000602void ScopStmt::realignParams() {
603 for (memacc_iterator MI = memacc_begin(), ME = memacc_end(); MI != ME; ++MI)
604 (*MI)->realignParams();
605
606 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
607 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
608}
609
Tobias Grosser65b00582011-11-08 15:41:19 +0000610__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000611 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
612 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000613
Tobias Grosserd2795d02011-08-18 07:51:40 +0000614 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000615 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000616 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000617 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000618 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000619 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000620 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000621 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000622 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000623 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000624 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000625 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000626 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000627 case ICmpInst::ICMP_ULT:
628 case ICmpInst::ICMP_UGT:
629 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000630 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000631 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000632 default:
633 llvm_unreachable("Non integer predicate not supported");
634 }
Tobias Grosser75805372011-04-29 06:27:02 +0000635}
636
Tobias Grossere19661e2011-10-07 08:46:57 +0000637__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000638 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000639 isl_space *Space;
640 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000641
Tobias Grossere19661e2011-10-07 08:46:57 +0000642 Space = isl_set_get_space(Domain);
643 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000644
Tobias Grosser75805372011-04-29 06:27:02 +0000645 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000646 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
647 isl_pw_aff *IV = isl_pw_aff_from_aff(
648 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000649
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000650 // 0 <= IV.
651 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
652 Domain = isl_set_intersect(Domain, LowerBound);
653
654 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000655 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000656 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000657 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
658 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000659 Domain = isl_set_intersect(Domain, UpperBoundSet);
660 }
661
Tobias Grosserf5338802011-10-06 00:03:35 +0000662 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000663 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000664}
665
Tobias Grossere19661e2011-10-07 08:46:57 +0000666__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
667 TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000668 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000669 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
670 *CurrentRegion = &CurRegion;
671 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000672
Tobias Grosser75805372011-04-29 06:27:02 +0000673 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000674 if (BranchingBB != CurrentRegion->getEntry()) {
675 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
676 for (BBCond::const_iterator CI = Condition->begin(),
677 CE = Condition->end(); CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000678 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000679 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000680 }
681 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000682 BranchingBB = CurrentRegion->getEntry();
683 CurrentRegion = CurrentRegion->getParent();
684 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000685
Tobias Grossere19661e2011-10-07 08:46:57 +0000686 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000687}
688
Tobias Grossere19661e2011-10-07 08:46:57 +0000689__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000690 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000691 isl_space *Space;
692 isl_set *Domain;
693
694 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
695
696 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000697 Domain = addLoopBoundsToDomain(Domain, tempScop);
698 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
699 Domain = isl_set_set_tuple_name(Domain, getBaseName());
700
701 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000702}
703
704ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
705 const Region &CurRegion, BasicBlock &bb,
706 SmallVectorImpl<Loop*> &NestLoops,
707 SmallVectorImpl<unsigned> &Scatter)
708 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
709 // Setup the induction variables.
710 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
711 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
712 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000713 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000714 }
715
716 raw_string_ostream OS(BaseName);
717 WriteAsOperand(OS, &bb, false);
718 BaseName = OS.str();
719
Tobias Grosser75805372011-04-29 06:27:02 +0000720 makeIslCompatible(BaseName);
721 BaseName = "Stmt_" + BaseName;
722
Tobias Grossere19661e2011-10-07 08:46:57 +0000723 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000724 buildScattering(Scatter);
725 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000726}
727
728ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
729 : Parent(parent), BB(NULL), IVS(0) {
730
731 BaseName = "FinalRead";
732
733 // Build iteration domain.
734 std::string IterationDomainString = "{[i0] : i0 = 0}";
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000735 Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str());
Tobias Grosser75805372011-04-29 06:27:02 +0000736 Domain = isl_set_set_tuple_name(Domain, getBaseName());
737
738 // Build scattering.
Tobias Grosserf5338802011-10-06 00:03:35 +0000739 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000740 isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace);
Tobias Grosserf5338802011-10-06 00:03:35 +0000741 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
742 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
743 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000744 isl_int v;
745 isl_int_init(v);
746
Tobias Grosserf5338802011-10-06 00:03:35 +0000747 isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000748 isl_int_set_si(v, -1);
749 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
750
751 // TODO: This is incorrect. We should not use a very large number to ensure
752 // that this statement is executed last.
753 isl_int_set_si(v, 200000000);
754 isl_constraint_set_constant(c, v);
755
756 bmap = isl_basic_map_add_constraint(bmap, c);
757 isl_int_clear(v);
758 Scattering = isl_map_from_basic_map(bmap);
759
760 // Build memory accesses, use SetVector to keep the order of memory accesses
761 // and prevent the same memory access inserted more than once.
762 SetVector<const Value*> BaseAddressSet;
763
764 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
765 ++SI) {
766 ScopStmt *Stmt = *SI;
767
768 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
769 E = Stmt->memacc_end(); I != E; ++I)
770 BaseAddressSet.insert((*I)->getBaseAddr());
771 }
772
773 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
774 BE = BaseAddressSet.end(); BI != BE; ++BI)
775 MemAccs.push_back(new MemoryAccess(*BI, this));
Tobias Grosser75805372011-04-29 06:27:02 +0000776}
777
778std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000779 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000780}
781
782std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000783 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000784}
785
786unsigned ScopStmt::getNumParams() const {
787 return Parent.getNumParams();
788}
789
790unsigned ScopStmt::getNumIterators() const {
791 // The final read has one dimension with one element.
792 if (!BB)
793 return 1;
794
795 return IVS.size();
796}
797
798unsigned ScopStmt::getNumScattering() const {
799 return isl_map_dim(Scattering, isl_dim_out);
800}
801
802const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
803
804const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
805 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000806 return IVS[Dimension].first;
807}
808
809const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
810 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000811}
812
813const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
814 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000815 PHINode *PN =
816 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000817 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
818}
819
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000820isl_ctx *ScopStmt::getIslCtx() const {
821 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000822}
823
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000824isl_set *ScopStmt::getDomain() const {
825 return isl_set_copy(Domain);
826}
827
Tobias Grosser75805372011-04-29 06:27:02 +0000828ScopStmt::~ScopStmt() {
829 while (!MemAccs.empty()) {
830 delete MemAccs.back();
831 MemAccs.pop_back();
832 }
833
834 isl_set_free(Domain);
835 isl_map_free(Scattering);
836}
837
838void ScopStmt::print(raw_ostream &OS) const {
839 OS << "\t" << getBaseName() << "\n";
840
841 OS.indent(12) << "Domain :=\n";
842
843 if (Domain) {
844 OS.indent(16) << getDomainStr() << ";\n";
845 } else
846 OS.indent(16) << "n/a\n";
847
848 OS.indent(12) << "Scattering :=\n";
849
850 if (Domain) {
851 OS.indent(16) << getScatteringStr() << ";\n";
852 } else
853 OS.indent(16) << "n/a\n";
854
855 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
856 I != E; ++I)
857 (*I)->print(OS);
858}
859
860void ScopStmt::dump() const { print(dbgs()); }
861
862//===----------------------------------------------------------------------===//
863/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000864
865void Scop::addParams(std::vector<const SCEV*> NewParameters) {
866 for (std::vector<const SCEV*>::iterator PI = NewParameters.begin(),
867 PE = NewParameters.end(); PI != PE; ++PI) {
868 const SCEV *Parameter = *PI;
869
870 if (ParameterIds.find(Parameter) != ParameterIds.end())
871 continue;
872
873 int dimension = Parameters.size();
874
875 Parameters.push_back(Parameter);
876 ParameterIds[Parameter] = dimension;
877 }
878}
879
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000880__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
881 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000882
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000883 if (IdIter == ParameterIds.end())
884 return NULL;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000885
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000886 std::string ParameterName = "p" + convertInt(IdIter->second);
887 return isl_id_alloc(getIslCtx(), ParameterName.c_str(), (void *) Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000888}
Tobias Grosser75805372011-04-29 06:27:02 +0000889
Tobias Grosser6be480c2011-11-08 15:41:13 +0000890void Scop::buildContext() {
891 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grosserf5338802011-10-06 00:03:35 +0000892 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000893}
894
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000895void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000896 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000897 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000898
899 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
900 PI != PE; ++PI) {
901 const SCEV *Parameter = PI->first;
902 isl_id *id = getIdForParam(Parameter);
903 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
904 }
905
906 // Align the parameters of all data structures to the model.
907 Context = isl_set_align_params(Context, Space);
908
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000909 for (iterator I = begin(), E = end(); I != E; ++I)
910 (*I)->realignParams();
911}
912
Tobias Grosser0e27e242011-10-06 00:03:48 +0000913Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
914 isl_ctx *Context)
915 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
916 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000917 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000918 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000919
920 SmallVector<Loop*, 8> NestLoops;
921 SmallVector<unsigned, 8> Scatter;
922
923 Scatter.assign(MaxLoopDepth + 1, 0);
924
925 // Build the iteration domain, access functions and scattering functions
926 // traversing the region tree.
927 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
928 Stmts.push_back(new ScopStmt(*this, Scatter));
929
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000930 realignParams();
931
Tobias Grosser75805372011-04-29 06:27:02 +0000932 assert(NestLoops.empty() && "NestLoops not empty at top level!");
933}
934
935Scop::~Scop() {
936 isl_set_free(Context);
937
938 // Free the statements;
939 for (iterator I = begin(), E = end(); I != E; ++I)
940 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000941}
942
943std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000944 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000945}
946
947std::string Scop::getNameStr() const {
948 std::string ExitName, EntryName;
949 raw_string_ostream ExitStr(ExitName);
950 raw_string_ostream EntryStr(EntryName);
951
952 WriteAsOperand(EntryStr, R.getEntry(), false);
953 EntryStr.str();
954
955 if (R.getExit()) {
956 WriteAsOperand(ExitStr, R.getExit(), false);
957 ExitStr.str();
958 } else
959 ExitName = "FunctionExit";
960
961 return EntryName + "---" + ExitName;
962}
963
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000964__isl_give isl_set *Scop::getContext() const {
965 return isl_set_copy(Context);
966}
Tobias Grosser37487052011-10-06 00:03:42 +0000967__isl_give isl_space *Scop::getParamSpace() const {
968 return isl_set_get_space(this->Context);
969}
970
Tobias Grosser75805372011-04-29 06:27:02 +0000971void Scop::printContext(raw_ostream &OS) const {
972 OS << "Context:\n";
973
974 if (!Context) {
975 OS.indent(4) << "n/a\n\n";
976 return;
977 }
978
979 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000980
981 for (ParamVecType::const_iterator PI = Parameters.begin(),
982 PE = Parameters.end(); PI != PE; ++PI) {
983 const SCEV *Parameter = *PI;
984 int Dim = ParameterIds.find(Parameter)->second;
985
986 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
987 }
Tobias Grosser75805372011-04-29 06:27:02 +0000988}
989
990void Scop::printStatements(raw_ostream &OS) const {
991 OS << "Statements {\n";
992
993 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
994 OS.indent(4) << (**SI);
995
996 OS.indent(4) << "}\n";
997}
998
999
1000void Scop::print(raw_ostream &OS) const {
1001 printContext(OS.indent(4));
1002 printStatements(OS.indent(4));
1003}
1004
1005void Scop::dump() const { print(dbgs()); }
1006
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001007isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001008
1009ScalarEvolution *Scop::getSE() const { return SE; }
1010
1011bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1012 if (tempScop.getAccessFunctions(BB))
1013 return false;
1014
1015 return true;
1016}
1017
1018void Scop::buildScop(TempScop &tempScop,
1019 const Region &CurRegion,
1020 SmallVectorImpl<Loop*> &NestLoops,
1021 SmallVectorImpl<unsigned> &Scatter,
1022 LoopInfo &LI) {
1023 Loop *L = castToLoop(CurRegion, LI);
1024
1025 if (L)
1026 NestLoops.push_back(L);
1027
1028 unsigned loopDepth = NestLoops.size();
1029 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1030
1031 for (Region::const_element_iterator I = CurRegion.element_begin(),
1032 E = CurRegion.element_end(); I != E; ++I)
1033 if (I->isSubRegion())
1034 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1035 else {
1036 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1037
1038 if (isTrivialBB(BB, tempScop))
1039 continue;
1040
1041 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
1042 Scatter));
1043
1044 // Increasing the Scattering function is OK for the moment, because
1045 // we are using a depth first iterator and the program is well structured.
1046 ++Scatter[loopDepth];
1047 }
1048
1049 if (!L)
1050 return;
1051
1052 // Exiting a loop region.
1053 Scatter[loopDepth] = 0;
1054 NestLoops.pop_back();
1055 ++Scatter[loopDepth-1];
1056}
1057
1058//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001059ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1060 ctx = isl_ctx_alloc();
1061}
1062
1063ScopInfo::~ScopInfo() {
1064 clear();
1065 isl_ctx_free(ctx);
1066}
1067
1068
Tobias Grosser75805372011-04-29 06:27:02 +00001069
1070void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1071 AU.addRequired<LoopInfo>();
1072 AU.addRequired<RegionInfo>();
1073 AU.addRequired<ScalarEvolution>();
1074 AU.addRequired<TempScopInfo>();
1075 AU.setPreservesAll();
1076}
1077
1078bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1079 LoopInfo &LI = getAnalysis<LoopInfo>();
1080 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1081
1082 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1083
1084 // This region is no Scop.
1085 if (!tempScop) {
1086 scop = 0;
1087 return false;
1088 }
1089
1090 // Statistics.
1091 ++ScopFound;
1092 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1093
Tobias Grosserb76f38532011-08-20 11:11:25 +00001094 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001095
1096 return false;
1097}
1098
1099char ScopInfo::ID = 0;
1100
Tobias Grosser73600b82011-10-08 00:30:40 +00001101INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1102 "Polly - Create polyhedral description of Scops", false,
1103 false)
1104INITIALIZE_PASS_DEPENDENCY(LoopInfo)
1105INITIALIZE_PASS_DEPENDENCY(RegionInfo)
1106INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1107INITIALIZE_PASS_DEPENDENCY(TempScopInfo)
1108INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1109 "Polly - Create polyhedral description of Scops", false,
1110 false)
Tobias Grosser75805372011-04-29 06:27:02 +00001111
1112Pass *polly::createScopInfoPass() {
1113 return new ScopInfo();
1114}