blob: 11c1dbb7f3f8628643f196af2f3227d3aa25bf7c [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
327MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000328 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000329 Type = AffFunc.isRead() ? Read : Write;
330 statement = Statement;
331
Tobias Grosser5683df42011-11-09 22:34:34 +0000332 Value *TmpBaseAddress = NULL;
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000333 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, AffFunc.OriginalSCEV,
Tobias Grosser5683df42011-11-09 22:34:34 +0000334 &TmpBaseAddress);
335 BaseAddr = TmpBaseAddress;
336
337 setBaseName();
Tobias Grosser75805372011-04-29 06:27:02 +0000338
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000339 // Devide the access function by the size of the elements in the array.
340 //
341 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
342 // as something like A[i * elementsize]. This hides the fact that two
343 // subsequent values of 'i' index two values that are stored next to each
344 // other in memory. By this devision we make this characteristic obvious
345 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000346 isl_int v;
347 isl_int_init(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000348 isl_int_set_si(v, AffFunc.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000349 Affine = isl_pw_aff_scale_down(Affine, v);
350 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000351
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000352 AccessRelation = isl_map_from_pw_aff(Affine);
353 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
354 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000355 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
356 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000357}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000358
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000359void MemoryAccess::realignParams() {
360 isl_space *ParamSpace = statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000361 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000362}
363
364MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000365 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000366 BaseAddr = BaseAddress;
367 Type = Read;
368 statement = Statement;
369
370 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
371 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000372 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
373 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000374}
375
376void MemoryAccess::print(raw_ostream &OS) const {
377 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000378 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000379}
380
381void MemoryAccess::dump() const {
382 print(errs());
383}
384
385// Create a map in the size of the provided set domain, that maps from the
386// one element of the provided set domain to another element of the provided
387// set domain.
388// The mapping is limited to all points that are equal in all but the last
389// dimension and for which the last dimension of the input is strict smaller
390// than the last dimension of the output.
391//
392// getEqualAndLarger(set[i0, i1, ..., iX]):
393//
394// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
395// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
396//
Tobias Grosserf5338802011-10-06 00:03:35 +0000397static isl_map *getEqualAndLarger(isl_space *setDomain) {
398 isl_space *mapDomain = isl_space_map_from_set(setDomain);
Tobias Grosser23b36662011-10-17 08:32:36 +0000399 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(mapDomain));
Tobias Grosserf5338802011-10-06 00:03:35 +0000400 isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000401
402 // Set all but the last dimension to be equal for the input and output
403 //
404 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
405 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
406 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
407 isl_int v;
408 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000409 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000410
411 isl_int_set_si(v, 1);
412 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
413 isl_int_set_si(v, -1);
414 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
415
416 bmap = isl_basic_map_add_constraint(bmap, c);
417
418 isl_int_clear(v);
419 }
420
421 // Set the last dimension of the input to be strict smaller than the
422 // last dimension of the output.
423 //
424 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
425 //
426 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
427 isl_int v;
428 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000429 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000430 isl_int_set_si(v, -1);
431 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
432 isl_int_set_si(v, 1);
433 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
434 isl_int_set_si(v, -1);
435 isl_constraint_set_constant(c, v);
436 isl_int_clear(v);
437
438 bmap = isl_basic_map_add_constraint(bmap, c);
439
Tobias Grosser23b36662011-10-17 08:32:36 +0000440 isl_local_space_free(MapLocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000441 return isl_map_from_basic_map(bmap);
442}
443
444isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000445 isl_map *accessRelation = getAccessRelation();
Tobias Grosser75805372011-04-29 06:27:02 +0000446 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
Tobias Grossercf3942d2011-10-06 00:04:05 +0000447 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000448
449 scattering = isl_map_reverse(scattering);
450 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
451 scattering = isl_map_project_out(scattering, isl_dim_in,
452 isl_set_n_dim(scatteringDomain),
453 difference);
454
455 // Remove all names of the scattering dimensions, as the names may be lost
456 // anyways during the project. This leads to consistent results.
457 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
458 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
459
Tobias Grosserf5338802011-10-06 00:03:35 +0000460 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000461 nextScatt = isl_map_lexmin(nextScatt);
462
463 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
464
465 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
466 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
467 nextScatt = isl_map_apply_domain(nextScatt, scattering);
468 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
469
470 return isl_map_deltas(nextScatt);
471}
472
473bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
474 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000475 isl_space *StrideSpace = isl_set_get_space(stride);
476 isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace);
477 isl_constraint *c = isl_equality_alloc(StrideLS);
Tobias Grosser75805372011-04-29 06:27:02 +0000478
479 isl_int v;
480 isl_int_init(v);
481 isl_int_set_si(v, 1);
482 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
483 isl_int_set_si(v, 0);
484 isl_constraint_set_constant(c, v);
485 isl_int_clear(v);
486
Tobias Grosserf5338802011-10-06 00:03:35 +0000487 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000488
489 bset = isl_basic_set_add_constraint(bset, c);
490 isl_set *strideZero = isl_set_from_basic_set(bset);
491
Tobias Grosserb76f38532011-08-20 11:11:25 +0000492 bool isStrideZero = isl_set_is_equal(stride, strideZero);
493
494 isl_set_free(strideZero);
495 isl_set_free(stride);
496
497 return isStrideZero;
Tobias Grosser75805372011-04-29 06:27:02 +0000498}
499
500bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
501 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000502 isl_space *StrideSpace = isl_set_get_space(stride);
503 isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace);
504 isl_constraint *c = isl_equality_alloc(StrideLSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000505
506 isl_int v;
507 isl_int_init(v);
508 isl_int_set_si(v, 1);
509 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
510 isl_int_set_si(v, -1);
511 isl_constraint_set_constant(c, v);
512 isl_int_clear(v);
513
Tobias Grosserf5338802011-10-06 00:03:35 +0000514 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000515
516 bset = isl_basic_set_add_constraint(bset, c);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000517 isl_set *strideOne = isl_set_from_basic_set(bset);
Tobias Grosser75805372011-04-29 06:27:02 +0000518
Tobias Grosserb76f38532011-08-20 11:11:25 +0000519 bool isStrideOne = isl_set_is_equal(stride, strideOne);
520
521 isl_set_free(strideOne);
522 isl_set_free(stride);
523
524 return isStrideOne;
Tobias Grosser75805372011-04-29 06:27:02 +0000525}
526
Tobias Grosser5d453812011-10-06 00:04:11 +0000527void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000528 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000529 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000530}
Tobias Grosser75805372011-04-29 06:27:02 +0000531
532//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000533
534isl_map *ScopStmt::getScattering() const {
535 return isl_map_copy(Scattering);
536}
537
538void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000539 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000540 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000541}
542
Tobias Grosser75805372011-04-29 06:27:02 +0000543void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
544 unsigned NumberOfIterators = getNumIterators();
Tobias Grosserf5338802011-10-06 00:03:35 +0000545 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000546 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators,
Tobias Grosserf5338802011-10-06 00:03:35 +0000547 ScatSpace);
548 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
549 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
550 isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space));
551 isl_basic_map *bmap = isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000552 isl_int v;
553 isl_int_init(v);
554
555 // Loop dimensions.
556 for (unsigned i = 0; i < NumberOfIterators; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000557 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000558 isl_int_set_si(v, 1);
559 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
560 isl_int_set_si(v, -1);
561 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
562
563 bmap = isl_basic_map_add_constraint(bmap, c);
564 }
565
566 // Constant dimensions
567 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000568 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000569 isl_int_set_si(v, -1);
570 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
571 isl_int_set_si(v, Scatter[i]);
572 isl_constraint_set_constant(c, v);
573
574 bmap = isl_basic_map_add_constraint(bmap, c);
575 }
576
577 // Fill scattering dimensions.
Tobias Grosserf5338802011-10-06 00:03:35 +0000578 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) {
579 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000580 isl_int_set_si(v, 1);
581 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
582 isl_int_set_si(v, 0);
583 isl_constraint_set_constant(c, v);
584
585 bmap = isl_basic_map_add_constraint(bmap, c);
586 }
587
588 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000589 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000590 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser0ad4caa2011-10-08 00:35:17 +0000591 isl_local_space_free(LSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000592}
593
594void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
595 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
596
597 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
598 E = AccFuncs->end(); I != E; ++I) {
599 MemAccs.push_back(new MemoryAccess(I->first, this));
600 InstructionToAccess[I->second] = MemAccs.back();
601 }
602}
603
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000604void ScopStmt::realignParams() {
605 for (memacc_iterator MI = memacc_begin(), ME = memacc_end(); MI != ME; ++MI)
606 (*MI)->realignParams();
607
608 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
609 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
610}
611
Tobias Grosser65b00582011-11-08 15:41:19 +0000612__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000613 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
614 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000615
Tobias Grosserd2795d02011-08-18 07:51:40 +0000616 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000617 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000618 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000619 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000620 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000621 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000622 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000623 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000624 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000625 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000626 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000627 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000628 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000629 case ICmpInst::ICMP_ULT:
630 case ICmpInst::ICMP_UGT:
631 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000632 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000633 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000634 default:
635 llvm_unreachable("Non integer predicate not supported");
636 }
Tobias Grosser75805372011-04-29 06:27:02 +0000637}
638
Tobias Grossere19661e2011-10-07 08:46:57 +0000639__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000640 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000641 isl_space *Space;
642 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000643
Tobias Grossere19661e2011-10-07 08:46:57 +0000644 Space = isl_set_get_space(Domain);
645 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000646
Tobias Grosser75805372011-04-29 06:27:02 +0000647 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000648 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
649 isl_pw_aff *IV = isl_pw_aff_from_aff(
650 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000651
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000652 // 0 <= IV.
653 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
654 Domain = isl_set_intersect(Domain, LowerBound);
655
656 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000657 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000658 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000659 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
660 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000661 Domain = isl_set_intersect(Domain, UpperBoundSet);
662 }
663
Tobias Grosserf5338802011-10-06 00:03:35 +0000664 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000665 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000666}
667
Tobias Grossere19661e2011-10-07 08:46:57 +0000668__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
669 TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000670 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000671 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
672 *CurrentRegion = &CurRegion;
673 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000674
Tobias Grosser75805372011-04-29 06:27:02 +0000675 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000676 if (BranchingBB != CurrentRegion->getEntry()) {
677 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
678 for (BBCond::const_iterator CI = Condition->begin(),
679 CE = Condition->end(); CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000680 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000681 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000682 }
683 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000684 BranchingBB = CurrentRegion->getEntry();
685 CurrentRegion = CurrentRegion->getParent();
686 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000687
Tobias Grossere19661e2011-10-07 08:46:57 +0000688 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000689}
690
Tobias Grossere19661e2011-10-07 08:46:57 +0000691__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000692 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000693 isl_space *Space;
694 isl_set *Domain;
695
696 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
697
698 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000699 Domain = addLoopBoundsToDomain(Domain, tempScop);
700 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
701 Domain = isl_set_set_tuple_name(Domain, getBaseName());
702
703 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000704}
705
706ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
707 const Region &CurRegion, BasicBlock &bb,
708 SmallVectorImpl<Loop*> &NestLoops,
709 SmallVectorImpl<unsigned> &Scatter)
710 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
711 // Setup the induction variables.
712 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
713 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
714 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000715 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000716 }
717
718 raw_string_ostream OS(BaseName);
719 WriteAsOperand(OS, &bb, false);
720 BaseName = OS.str();
721
Tobias Grosser75805372011-04-29 06:27:02 +0000722 makeIslCompatible(BaseName);
723 BaseName = "Stmt_" + BaseName;
724
Tobias Grossere19661e2011-10-07 08:46:57 +0000725 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000726 buildScattering(Scatter);
727 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000728}
729
730ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
731 : Parent(parent), BB(NULL), IVS(0) {
732
733 BaseName = "FinalRead";
734
735 // Build iteration domain.
736 std::string IterationDomainString = "{[i0] : i0 = 0}";
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000737 Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str());
Tobias Grosser75805372011-04-29 06:27:02 +0000738 Domain = isl_set_set_tuple_name(Domain, getBaseName());
739
740 // Build scattering.
Tobias Grosserf5338802011-10-06 00:03:35 +0000741 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000742 isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace);
Tobias Grosserf5338802011-10-06 00:03:35 +0000743 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
744 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
745 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000746 isl_int v;
747 isl_int_init(v);
748
Tobias Grosserf5338802011-10-06 00:03:35 +0000749 isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000750 isl_int_set_si(v, -1);
751 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
752
753 // TODO: This is incorrect. We should not use a very large number to ensure
754 // that this statement is executed last.
755 isl_int_set_si(v, 200000000);
756 isl_constraint_set_constant(c, v);
757
758 bmap = isl_basic_map_add_constraint(bmap, c);
759 isl_int_clear(v);
760 Scattering = isl_map_from_basic_map(bmap);
761
762 // Build memory accesses, use SetVector to keep the order of memory accesses
763 // and prevent the same memory access inserted more than once.
764 SetVector<const Value*> BaseAddressSet;
765
766 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
767 ++SI) {
768 ScopStmt *Stmt = *SI;
769
770 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
771 E = Stmt->memacc_end(); I != E; ++I)
772 BaseAddressSet.insert((*I)->getBaseAddr());
773 }
774
775 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
776 BE = BaseAddressSet.end(); BI != BE; ++BI)
777 MemAccs.push_back(new MemoryAccess(*BI, this));
Tobias Grosser75805372011-04-29 06:27:02 +0000778}
779
780std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000781 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000782}
783
784std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000785 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000786}
787
788unsigned ScopStmt::getNumParams() const {
789 return Parent.getNumParams();
790}
791
792unsigned ScopStmt::getNumIterators() const {
793 // The final read has one dimension with one element.
794 if (!BB)
795 return 1;
796
797 return IVS.size();
798}
799
800unsigned ScopStmt::getNumScattering() const {
801 return isl_map_dim(Scattering, isl_dim_out);
802}
803
804const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
805
806const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
807 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000808 return IVS[Dimension].first;
809}
810
811const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
812 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000813}
814
815const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
816 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000817 PHINode *PN =
818 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000819 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
820}
821
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000822isl_ctx *ScopStmt::getIslCtx() const {
823 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000824}
825
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000826isl_set *ScopStmt::getDomain() const {
827 return isl_set_copy(Domain);
828}
829
Tobias Grosser75805372011-04-29 06:27:02 +0000830ScopStmt::~ScopStmt() {
831 while (!MemAccs.empty()) {
832 delete MemAccs.back();
833 MemAccs.pop_back();
834 }
835
836 isl_set_free(Domain);
837 isl_map_free(Scattering);
838}
839
840void ScopStmt::print(raw_ostream &OS) const {
841 OS << "\t" << getBaseName() << "\n";
842
843 OS.indent(12) << "Domain :=\n";
844
845 if (Domain) {
846 OS.indent(16) << getDomainStr() << ";\n";
847 } else
848 OS.indent(16) << "n/a\n";
849
850 OS.indent(12) << "Scattering :=\n";
851
852 if (Domain) {
853 OS.indent(16) << getScatteringStr() << ";\n";
854 } else
855 OS.indent(16) << "n/a\n";
856
857 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
858 I != E; ++I)
859 (*I)->print(OS);
860}
861
862void ScopStmt::dump() const { print(dbgs()); }
863
864//===----------------------------------------------------------------------===//
865/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000866
867void Scop::addParams(std::vector<const SCEV*> NewParameters) {
868 for (std::vector<const SCEV*>::iterator PI = NewParameters.begin(),
869 PE = NewParameters.end(); PI != PE; ++PI) {
870 const SCEV *Parameter = *PI;
871
872 if (ParameterIds.find(Parameter) != ParameterIds.end())
873 continue;
874
875 int dimension = Parameters.size();
876
877 Parameters.push_back(Parameter);
878 ParameterIds[Parameter] = dimension;
879 }
880}
881
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000882__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
883 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000884
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000885 if (IdIter == ParameterIds.end())
886 return NULL;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000887
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000888 std::string ParameterName = "p" + convertInt(IdIter->second);
889 return isl_id_alloc(getIslCtx(), ParameterName.c_str(), (void *) Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000890}
Tobias Grosser75805372011-04-29 06:27:02 +0000891
Tobias Grosser6be480c2011-11-08 15:41:13 +0000892void Scop::buildContext() {
893 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grosserf5338802011-10-06 00:03:35 +0000894 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000895}
896
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000897void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000898 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000899 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000900
901 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
902 PI != PE; ++PI) {
903 const SCEV *Parameter = PI->first;
904 isl_id *id = getIdForParam(Parameter);
905 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
906 }
907
908 // Align the parameters of all data structures to the model.
909 Context = isl_set_align_params(Context, Space);
910
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000911 for (iterator I = begin(), E = end(); I != E; ++I)
912 (*I)->realignParams();
913}
914
Tobias Grosser0e27e242011-10-06 00:03:48 +0000915Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
916 isl_ctx *Context)
917 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
918 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000919 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000920 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000921
922 SmallVector<Loop*, 8> NestLoops;
923 SmallVector<unsigned, 8> Scatter;
924
925 Scatter.assign(MaxLoopDepth + 1, 0);
926
927 // Build the iteration domain, access functions and scattering functions
928 // traversing the region tree.
929 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
930 Stmts.push_back(new ScopStmt(*this, Scatter));
931
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000932 realignParams();
933
Tobias Grosser75805372011-04-29 06:27:02 +0000934 assert(NestLoops.empty() && "NestLoops not empty at top level!");
935}
936
937Scop::~Scop() {
938 isl_set_free(Context);
939
940 // Free the statements;
941 for (iterator I = begin(), E = end(); I != E; ++I)
942 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000943}
944
945std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000946 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000947}
948
949std::string Scop::getNameStr() const {
950 std::string ExitName, EntryName;
951 raw_string_ostream ExitStr(ExitName);
952 raw_string_ostream EntryStr(EntryName);
953
954 WriteAsOperand(EntryStr, R.getEntry(), false);
955 EntryStr.str();
956
957 if (R.getExit()) {
958 WriteAsOperand(ExitStr, R.getExit(), false);
959 ExitStr.str();
960 } else
961 ExitName = "FunctionExit";
962
963 return EntryName + "---" + ExitName;
964}
965
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000966__isl_give isl_set *Scop::getContext() const {
967 return isl_set_copy(Context);
968}
Tobias Grosser37487052011-10-06 00:03:42 +0000969__isl_give isl_space *Scop::getParamSpace() const {
970 return isl_set_get_space(this->Context);
971}
972
Tobias Grosser75805372011-04-29 06:27:02 +0000973void Scop::printContext(raw_ostream &OS) const {
974 OS << "Context:\n";
975
976 if (!Context) {
977 OS.indent(4) << "n/a\n\n";
978 return;
979 }
980
981 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000982
983 for (ParamVecType::const_iterator PI = Parameters.begin(),
984 PE = Parameters.end(); PI != PE; ++PI) {
985 const SCEV *Parameter = *PI;
986 int Dim = ParameterIds.find(Parameter)->second;
987
988 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
989 }
Tobias Grosser75805372011-04-29 06:27:02 +0000990}
991
992void Scop::printStatements(raw_ostream &OS) const {
993 OS << "Statements {\n";
994
995 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
996 OS.indent(4) << (**SI);
997
998 OS.indent(4) << "}\n";
999}
1000
1001
1002void Scop::print(raw_ostream &OS) const {
1003 printContext(OS.indent(4));
1004 printStatements(OS.indent(4));
1005}
1006
1007void Scop::dump() const { print(dbgs()); }
1008
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001009isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001010
1011ScalarEvolution *Scop::getSE() const { return SE; }
1012
1013bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1014 if (tempScop.getAccessFunctions(BB))
1015 return false;
1016
1017 return true;
1018}
1019
1020void Scop::buildScop(TempScop &tempScop,
1021 const Region &CurRegion,
1022 SmallVectorImpl<Loop*> &NestLoops,
1023 SmallVectorImpl<unsigned> &Scatter,
1024 LoopInfo &LI) {
1025 Loop *L = castToLoop(CurRegion, LI);
1026
1027 if (L)
1028 NestLoops.push_back(L);
1029
1030 unsigned loopDepth = NestLoops.size();
1031 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1032
1033 for (Region::const_element_iterator I = CurRegion.element_begin(),
1034 E = CurRegion.element_end(); I != E; ++I)
1035 if (I->isSubRegion())
1036 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1037 else {
1038 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1039
1040 if (isTrivialBB(BB, tempScop))
1041 continue;
1042
1043 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
1044 Scatter));
1045
1046 // Increasing the Scattering function is OK for the moment, because
1047 // we are using a depth first iterator and the program is well structured.
1048 ++Scatter[loopDepth];
1049 }
1050
1051 if (!L)
1052 return;
1053
1054 // Exiting a loop region.
1055 Scatter[loopDepth] = 0;
1056 NestLoops.pop_back();
1057 ++Scatter[loopDepth-1];
1058}
1059
1060//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001061ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1062 ctx = isl_ctx_alloc();
1063}
1064
1065ScopInfo::~ScopInfo() {
1066 clear();
1067 isl_ctx_free(ctx);
1068}
1069
1070
Tobias Grosser75805372011-04-29 06:27:02 +00001071
1072void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1073 AU.addRequired<LoopInfo>();
1074 AU.addRequired<RegionInfo>();
1075 AU.addRequired<ScalarEvolution>();
1076 AU.addRequired<TempScopInfo>();
1077 AU.setPreservesAll();
1078}
1079
1080bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1081 LoopInfo &LI = getAnalysis<LoopInfo>();
1082 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1083
1084 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1085
1086 // This region is no Scop.
1087 if (!tempScop) {
1088 scop = 0;
1089 return false;
1090 }
1091
1092 // Statistics.
1093 ++ScopFound;
1094 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1095
Tobias Grosserb76f38532011-08-20 11:11:25 +00001096 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001097
1098 return false;
1099}
1100
1101char ScopInfo::ID = 0;
1102
Tobias Grosser73600b82011-10-08 00:30:40 +00001103INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1104 "Polly - Create polyhedral description of Scops", false,
1105 false)
1106INITIALIZE_PASS_DEPENDENCY(LoopInfo)
1107INITIALIZE_PASS_DEPENDENCY(RegionInfo)
1108INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1109INITIALIZE_PASS_DEPENDENCY(TempScopInfo)
1110INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1111 "Polly - Create polyhedral description of Scops", false,
1112 false)
Tobias Grosser75805372011-04-29 06:27:02 +00001113
1114Pass *polly::createScopInfoPass() {
1115 return new ScopInfo();
1116}