blob: 0ddd75294db7059ec1be030b464afd92665b4404 [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
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000079public:
Tobias Grossere5e171e2011-11-10 12:45:03 +000080 static isl_pw_aff *getPwAff(ScopStmt *stmt, const SCEV *scev) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000081 Scop *S = stmt->getParent();
82 const Region *Reg = &S->getRegion();
83
Tobias Grossere5e171e2011-11-10 12:45:03 +000084 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE()));
Tobias Grosser60b54f12011-11-08 15:41:28 +000085
Tobias Grossere5e171e2011-11-10 12:45:03 +000086 SCEVAffinator Affinator(stmt);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000087 return Affinator.visit(scev);
88 }
89
90 isl_pw_aff *visit(const SCEV *scev) {
Tobias Grosser76c2e322011-11-07 12:58:59 +000091 // In case the scev is a valid parameter, we do not further analyze this
92 // expression, but create a new parameter in the isl_pw_aff. This allows us
93 // to treat subexpressions that we cannot translate into an piecewise affine
94 // expression, as constant parameters of the piecewise affine expression.
95 if (isl_id *Id = scop->getIdForParam(scev)) {
96 isl_space *Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
97 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000098
Tobias Grosser76c2e322011-11-07 12:58:59 +000099 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
100 isl_aff *Affine = isl_aff_zero_on_domain(
101 isl_local_space_from_space(Space));
102 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000103
Tobias Grosser76c2e322011-11-07 12:58:59 +0000104 return isl_pw_aff_alloc(Domain, Affine);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000105 }
106
107 return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev);
108 }
109
Tobias Grossere5e171e2011-11-10 12:45:03 +0000110 SCEVAffinator(const ScopStmt *stmt) :
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000111 ctx(stmt->getIslCtx()),
Tobias Grosserf5338802011-10-06 00:03:35 +0000112 NbLoopSpaces(stmt->getNumIterators()),
Tobias Grossere5e171e2011-11-10 12:45:03 +0000113 scop(stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000114
115 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) {
116 ConstantInt *Value = Constant->getValue();
117 isl_int v;
118 isl_int_init(v);
119
120 // LLVM does not define if an integer value is interpreted as a signed or
121 // unsigned value. Hence, without further information, it is unknown how
122 // this value needs to be converted to GMP. At the moment, we only support
123 // signed operations. So we just interpret it as signed. Later, there are
124 // two options:
125 //
126 // 1. We always interpret any value as signed and convert the values on
127 // demand.
128 // 2. We pass down the signedness of the calculation and use it to interpret
129 // this constant correctly.
130 MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true);
131
Tobias Grosserf5338802011-10-06 00:03:35 +0000132 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
133 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
134 isl_aff *Affine = isl_aff_zero_on_domain(ls);
135 isl_set *Domain = isl_set_universe(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000136
137 Affine = isl_aff_add_constant(Affine, v);
138 isl_int_clear(v);
139
140 return isl_pw_aff_alloc(Domain, Affine);
141 }
142
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000143 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000144 assert(0 && "Not yet supported");
145 }
146
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000147 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000148 assert(0 && "Not yet supported");
149 }
150
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000151 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000152 // Assuming the value is signed, a sign extension is basically a noop.
153 // TODO: Reconsider this as soon as we support unsigned values.
154 return visit(Expr->getOperand());
155 }
156
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000157 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000158 isl_pw_aff *Sum = visit(Expr->getOperand(0));
159
160 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
161 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
162 Sum = isl_pw_aff_add(Sum, NextSummand);
163 }
164
165 // TODO: Check for NSW and NUW.
166
167 return Sum;
168 }
169
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000170 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000171 isl_pw_aff *Product = visit(Expr->getOperand(0));
172
173 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
174 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
175
176 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
177 isl_pw_aff_free(Product);
178 isl_pw_aff_free(NextOperand);
179 return NULL;
180 }
181
182 Product = isl_pw_aff_mul(Product, NextOperand);
183 }
184
185 // TODO: Check for NSW and NUW.
186 return Product;
187 }
188
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000189 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000190 assert(0 && "Not yet supported");
191 }
192
193 int getLoopDepth(const Loop *L) {
194 Loop *outerLoop =
195 scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L));
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000196 assert(outerLoop && "Scop does not contain this loop");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000197 return L->getLoopDepth() - outerLoop->getLoopDepth();
198 }
199
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000200 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000201 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000202 assert(scop->getRegion().contains(Expr->getLoop())
203 && "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000204
205 isl_pw_aff *Start = visit(Expr->getStart());
206 isl_pw_aff *Step = visit(Expr->getOperand(1));
Tobias Grosserf5338802011-10-06 00:03:35 +0000207 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
208 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000209
210 int loopDimension = getLoopDepth(Expr->getLoop());
211
Tobias Grosserf5338802011-10-06 00:03:35 +0000212 isl_aff *LAff = isl_aff_set_coefficient_si(
213 isl_aff_zero_on_domain (LocalSpace), isl_dim_in, loopDimension, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000214 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
215
216 // TODO: Do we need to check for NSW and NUW?
217 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
218 }
219
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000220 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000221 isl_pw_aff *Max = visit(Expr->getOperand(0));
222
223 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
224 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
225 Max = isl_pw_aff_max(Max, NextOperand);
226 }
227
228 return Max;
229 }
230
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000231 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000232 assert(0 && "Not yet supported");
233 }
234
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000235 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000236 Value *Value = Expr->getValue();
237
Tobias Grosserf5338802011-10-06 00:03:35 +0000238 isl_space *Space;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000239
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000240 std::string ValueName = Value->getName();
241 isl_id *ID = isl_id_alloc(ctx, ValueName.c_str(), Value);
Tobias Grossere5e171e2011-11-10 12:45:03 +0000242 Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
243 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000244
Tobias Grosserf5338802011-10-06 00:03:35 +0000245 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
246 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000247
Tobias Grossere5e171e2011-11-10 12:45:03 +0000248 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000249
250 return isl_pw_aff_alloc(Domain, Affine);
251 }
252};
253
Tobias Grosser75805372011-04-29 06:27:02 +0000254//===----------------------------------------------------------------------===//
255
256MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000257 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000258 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000259}
260
261static void replace(std::string& str, const std::string& find,
262 const std::string& replace) {
263 size_t pos = 0;
264 while((pos = str.find(find, pos)) != std::string::npos)
265 {
266 str.replace(pos, find.length(), replace);
267 pos += replace.length();
268 }
269}
270
271static void makeIslCompatible(std::string& str) {
Tobias Grossereec4d56e2011-08-20 11:11:14 +0000272 str.erase(0, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000273 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000274 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000275}
276
277void MemoryAccess::setBaseName() {
278 raw_string_ostream OS(BaseName);
279 WriteAsOperand(OS, getBaseAddr(), false);
280 BaseName = OS.str();
281
Tobias Grosser75805372011-04-29 06:27:02 +0000282 makeIslCompatible(BaseName);
283 BaseName = "MemRef_" + BaseName;
284}
285
Tobias Grosser5d453812011-10-06 00:04:11 +0000286isl_map *MemoryAccess::getAccessRelation() const {
287 return isl_map_copy(AccessRelation);
288}
289
290std::string MemoryAccess::getAccessRelationStr() const {
291 return stringFromIslObj(AccessRelation);
292}
293
294isl_map *MemoryAccess::getNewAccessRelation() const {
295 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000296}
297
298isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000299 isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0,
Tobias Grosserf5338802011-10-06 00:03:35 +0000300 Statement->getNumIterators(), 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000301 setBaseName();
302
Tobias Grosserf5338802011-10-06 00:03:35 +0000303 Space = isl_space_set_tuple_name(Space, isl_dim_out, getBaseName().c_str());
304 Space = isl_space_set_tuple_name(Space, isl_dim_in, Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000305
Tobias Grosserf5338802011-10-06 00:03:35 +0000306 return isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000307}
308
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000309MemoryAccess::MemoryAccess(const IRAccess &Access, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000310 newAccessRelation = NULL;
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000311 Type = Access.isRead() ? Read : Write;
Tobias Grosser75805372011-04-29 06:27:02 +0000312 statement = Statement;
313
Tobias Grosser9759f852011-11-10 12:44:55 +0000314 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, Access.getOffset());
315 BaseAddr = Access.getBase();
Tobias Grosser5683df42011-11-09 22:34:34 +0000316
317 setBaseName();
Tobias Grosser75805372011-04-29 06:27:02 +0000318
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000319 // Devide the access function by the size of the elements in the array.
320 //
321 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
322 // as something like A[i * elementsize]. This hides the fact that two
323 // subsequent values of 'i' index two values that are stored next to each
324 // other in memory. By this devision we make this characteristic obvious
325 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000326 isl_int v;
327 isl_int_init(v);
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000328 isl_int_set_si(v, Access.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000329 Affine = isl_pw_aff_scale_down(Affine, v);
330 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000331
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000332 AccessRelation = isl_map_from_pw_aff(Affine);
333 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
334 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000335 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
336 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000337}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000338
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000339void MemoryAccess::realignParams() {
340 isl_space *ParamSpace = statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000341 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000342}
343
344MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000345 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000346 BaseAddr = BaseAddress;
347 Type = Read;
348 statement = Statement;
349
350 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
351 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000352 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
353 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000354}
355
356void MemoryAccess::print(raw_ostream &OS) const {
357 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000358 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000359}
360
361void MemoryAccess::dump() const {
362 print(errs());
363}
364
365// Create a map in the size of the provided set domain, that maps from the
366// one element of the provided set domain to another element of the provided
367// set domain.
368// The mapping is limited to all points that are equal in all but the last
369// dimension and for which the last dimension of the input is strict smaller
370// than the last dimension of the output.
371//
372// getEqualAndLarger(set[i0, i1, ..., iX]):
373//
374// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
375// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
376//
Tobias Grosserf5338802011-10-06 00:03:35 +0000377static isl_map *getEqualAndLarger(isl_space *setDomain) {
378 isl_space *mapDomain = isl_space_map_from_set(setDomain);
Tobias Grosser23b36662011-10-17 08:32:36 +0000379 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(mapDomain));
Tobias Grosserf5338802011-10-06 00:03:35 +0000380 isl_local_space *MapLocalSpace = isl_local_space_from_space(mapDomain);
Tobias Grosser75805372011-04-29 06:27:02 +0000381
382 // Set all but the last dimension to be equal for the input and output
383 //
384 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
385 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
386 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
387 isl_int v;
388 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000389 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000390
391 isl_int_set_si(v, 1);
392 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
393 isl_int_set_si(v, -1);
394 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
395
396 bmap = isl_basic_map_add_constraint(bmap, c);
397
398 isl_int_clear(v);
399 }
400
401 // Set the last dimension of the input to be strict smaller than the
402 // last dimension of the output.
403 //
404 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
405 //
406 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
407 isl_int v;
408 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000409 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000410 isl_int_set_si(v, -1);
411 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
412 isl_int_set_si(v, 1);
413 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
414 isl_int_set_si(v, -1);
415 isl_constraint_set_constant(c, v);
416 isl_int_clear(v);
417
418 bmap = isl_basic_map_add_constraint(bmap, c);
419
Tobias Grosser23b36662011-10-17 08:32:36 +0000420 isl_local_space_free(MapLocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000421 return isl_map_from_basic_map(bmap);
422}
423
424isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000425 isl_map *accessRelation = getAccessRelation();
Tobias Grosser75805372011-04-29 06:27:02 +0000426 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
Tobias Grossercf3942d2011-10-06 00:04:05 +0000427 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000428
429 scattering = isl_map_reverse(scattering);
430 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
431 scattering = isl_map_project_out(scattering, isl_dim_in,
432 isl_set_n_dim(scatteringDomain),
433 difference);
434
435 // Remove all names of the scattering dimensions, as the names may be lost
436 // anyways during the project. This leads to consistent results.
437 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
438 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
439
Tobias Grosserf5338802011-10-06 00:03:35 +0000440 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000441 nextScatt = isl_map_lexmin(nextScatt);
442
443 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
444
445 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
446 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
447 nextScatt = isl_map_apply_domain(nextScatt, scattering);
448 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
449
450 return isl_map_deltas(nextScatt);
451}
452
453bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
454 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000455 isl_space *StrideSpace = isl_set_get_space(stride);
456 isl_local_space *StrideLS = isl_local_space_from_space(StrideSpace);
457 isl_constraint *c = isl_equality_alloc(StrideLS);
Tobias Grosser75805372011-04-29 06:27:02 +0000458
459 isl_int v;
460 isl_int_init(v);
461 isl_int_set_si(v, 1);
462 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
463 isl_int_set_si(v, 0);
464 isl_constraint_set_constant(c, v);
465 isl_int_clear(v);
466
Tobias Grosserf5338802011-10-06 00:03:35 +0000467 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000468
469 bset = isl_basic_set_add_constraint(bset, c);
470 isl_set *strideZero = isl_set_from_basic_set(bset);
471
Tobias Grosserb76f38532011-08-20 11:11:25 +0000472 bool isStrideZero = isl_set_is_equal(stride, strideZero);
473
474 isl_set_free(strideZero);
475 isl_set_free(stride);
476
477 return isStrideZero;
Tobias Grosser75805372011-04-29 06:27:02 +0000478}
479
480bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
481 isl_set *stride = getStride(domainSubset);
Tobias Grosserf5338802011-10-06 00:03:35 +0000482 isl_space *StrideSpace = isl_set_get_space(stride);
483 isl_local_space *StrideLSpace = isl_local_space_from_space(StrideSpace);
484 isl_constraint *c = isl_equality_alloc(StrideLSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000485
486 isl_int v;
487 isl_int_init(v);
488 isl_int_set_si(v, 1);
489 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
490 isl_int_set_si(v, -1);
491 isl_constraint_set_constant(c, v);
492 isl_int_clear(v);
493
Tobias Grosserf5338802011-10-06 00:03:35 +0000494 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_space(stride));
Tobias Grosser75805372011-04-29 06:27:02 +0000495
496 bset = isl_basic_set_add_constraint(bset, c);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000497 isl_set *strideOne = isl_set_from_basic_set(bset);
Tobias Grosser75805372011-04-29 06:27:02 +0000498
Tobias Grosserb76f38532011-08-20 11:11:25 +0000499 bool isStrideOne = isl_set_is_equal(stride, strideOne);
500
501 isl_set_free(strideOne);
502 isl_set_free(stride);
503
504 return isStrideOne;
Tobias Grosser75805372011-04-29 06:27:02 +0000505}
506
Tobias Grosser5d453812011-10-06 00:04:11 +0000507void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000508 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000509 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000510}
Tobias Grosser75805372011-04-29 06:27:02 +0000511
512//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000513
514isl_map *ScopStmt::getScattering() const {
515 return isl_map_copy(Scattering);
516}
517
518void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000519 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000520 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000521}
522
Tobias Grosser75805372011-04-29 06:27:02 +0000523void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
524 unsigned NumberOfIterators = getNumIterators();
Tobias Grosserf5338802011-10-06 00:03:35 +0000525 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000526 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NumberOfIterators,
Tobias Grosserf5338802011-10-06 00:03:35 +0000527 ScatSpace);
528 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
529 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
530 isl_local_space *LSpace = isl_local_space_from_space(isl_space_copy(Space));
531 isl_basic_map *bmap = isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000532 isl_int v;
533 isl_int_init(v);
534
535 // Loop dimensions.
536 for (unsigned i = 0; i < NumberOfIterators; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000537 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000538 isl_int_set_si(v, 1);
539 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
540 isl_int_set_si(v, -1);
541 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
542
543 bmap = isl_basic_map_add_constraint(bmap, c);
544 }
545
546 // Constant dimensions
547 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000548 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000549 isl_int_set_si(v, -1);
550 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
551 isl_int_set_si(v, Scatter[i]);
552 isl_constraint_set_constant(c, v);
553
554 bmap = isl_basic_map_add_constraint(bmap, c);
555 }
556
557 // Fill scattering dimensions.
Tobias Grosserf5338802011-10-06 00:03:35 +0000558 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatSpace ; ++i) {
559 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000560 isl_int_set_si(v, 1);
561 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
562 isl_int_set_si(v, 0);
563 isl_constraint_set_constant(c, v);
564
565 bmap = isl_basic_map_add_constraint(bmap, c);
566 }
567
568 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000569 Scattering = isl_map_from_basic_map(bmap);
Tobias Grosser37487052011-10-06 00:03:42 +0000570 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser0ad4caa2011-10-08 00:35:17 +0000571 isl_local_space_free(LSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000572}
573
574void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
575 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
576
577 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
578 E = AccFuncs->end(); I != E; ++I) {
579 MemAccs.push_back(new MemoryAccess(I->first, this));
580 InstructionToAccess[I->second] = MemAccs.back();
581 }
582}
583
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000584void ScopStmt::realignParams() {
585 for (memacc_iterator MI = memacc_begin(), ME = memacc_end(); MI != ME; ++MI)
586 (*MI)->realignParams();
587
588 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
589 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
590}
591
Tobias Grosser65b00582011-11-08 15:41:19 +0000592__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000593 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
594 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000595
Tobias Grosserd2795d02011-08-18 07:51:40 +0000596 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000597 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000598 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000599 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000600 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000601 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000602 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000603 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000604 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000605 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000606 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000607 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000608 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000609 case ICmpInst::ICMP_ULT:
610 case ICmpInst::ICMP_UGT:
611 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000612 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000613 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000614 default:
615 llvm_unreachable("Non integer predicate not supported");
616 }
Tobias Grosser75805372011-04-29 06:27:02 +0000617}
618
Tobias Grossere19661e2011-10-07 08:46:57 +0000619__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000620 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000621 isl_space *Space;
622 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000623
Tobias Grossere19661e2011-10-07 08:46:57 +0000624 Space = isl_set_get_space(Domain);
625 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000626
Tobias Grosser75805372011-04-29 06:27:02 +0000627 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000628 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
629 isl_pw_aff *IV = isl_pw_aff_from_aff(
630 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000631
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000632 // 0 <= IV.
633 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
634 Domain = isl_set_intersect(Domain, LowerBound);
635
636 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000637 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000638 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000639 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
640 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000641 Domain = isl_set_intersect(Domain, UpperBoundSet);
642 }
643
Tobias Grosserf5338802011-10-06 00:03:35 +0000644 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000645 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000646}
647
Tobias Grossere19661e2011-10-07 08:46:57 +0000648__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
649 TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000650 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000651 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
652 *CurrentRegion = &CurRegion;
653 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000654
Tobias Grosser75805372011-04-29 06:27:02 +0000655 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000656 if (BranchingBB != CurrentRegion->getEntry()) {
657 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
658 for (BBCond::const_iterator CI = Condition->begin(),
659 CE = Condition->end(); CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000660 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000661 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000662 }
663 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000664 BranchingBB = CurrentRegion->getEntry();
665 CurrentRegion = CurrentRegion->getParent();
666 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000667
Tobias Grossere19661e2011-10-07 08:46:57 +0000668 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000669}
670
Tobias Grossere19661e2011-10-07 08:46:57 +0000671__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000672 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000673 isl_space *Space;
674 isl_set *Domain;
675
676 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
677
678 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000679 Domain = addLoopBoundsToDomain(Domain, tempScop);
680 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
681 Domain = isl_set_set_tuple_name(Domain, getBaseName());
682
683 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000684}
685
686ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
687 const Region &CurRegion, BasicBlock &bb,
688 SmallVectorImpl<Loop*> &NestLoops,
689 SmallVectorImpl<unsigned> &Scatter)
690 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
691 // Setup the induction variables.
692 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
693 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
694 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000695 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000696 }
697
698 raw_string_ostream OS(BaseName);
699 WriteAsOperand(OS, &bb, false);
700 BaseName = OS.str();
701
Tobias Grosser75805372011-04-29 06:27:02 +0000702 makeIslCompatible(BaseName);
703 BaseName = "Stmt_" + BaseName;
704
Tobias Grossere19661e2011-10-07 08:46:57 +0000705 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000706 buildScattering(Scatter);
707 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000708}
709
710ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
711 : Parent(parent), BB(NULL), IVS(0) {
712
713 BaseName = "FinalRead";
714
715 // Build iteration domain.
716 std::string IterationDomainString = "{[i0] : i0 = 0}";
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000717 Domain = isl_set_read_from_str(getIslCtx(), IterationDomainString.c_str());
Tobias Grosser75805372011-04-29 06:27:02 +0000718 Domain = isl_set_set_tuple_name(Domain, getBaseName());
719
720 // Build scattering.
Tobias Grosserf5338802011-10-06 00:03:35 +0000721 unsigned ScatSpace = Parent.getMaxLoopDepth() * 2 + 1;
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000722 isl_space *Space = isl_space_alloc(getIslCtx(), 0, 1, ScatSpace);
Tobias Grosserf5338802011-10-06 00:03:35 +0000723 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
724 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
725 isl_basic_map *bmap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000726 isl_int v;
727 isl_int_init(v);
728
Tobias Grosserf5338802011-10-06 00:03:35 +0000729 isl_constraint *c = isl_equality_alloc(isl_local_space_from_space(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000730 isl_int_set_si(v, -1);
731 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
732
733 // TODO: This is incorrect. We should not use a very large number to ensure
734 // that this statement is executed last.
735 isl_int_set_si(v, 200000000);
736 isl_constraint_set_constant(c, v);
737
738 bmap = isl_basic_map_add_constraint(bmap, c);
739 isl_int_clear(v);
740 Scattering = isl_map_from_basic_map(bmap);
741
742 // Build memory accesses, use SetVector to keep the order of memory accesses
743 // and prevent the same memory access inserted more than once.
744 SetVector<const Value*> BaseAddressSet;
745
746 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
747 ++SI) {
748 ScopStmt *Stmt = *SI;
749
750 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
751 E = Stmt->memacc_end(); I != E; ++I)
752 BaseAddressSet.insert((*I)->getBaseAddr());
753 }
754
755 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
756 BE = BaseAddressSet.end(); BI != BE; ++BI)
757 MemAccs.push_back(new MemoryAccess(*BI, this));
Tobias Grosser75805372011-04-29 06:27:02 +0000758}
759
760std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000761 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000762}
763
764std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000765 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000766}
767
768unsigned ScopStmt::getNumParams() const {
769 return Parent.getNumParams();
770}
771
772unsigned ScopStmt::getNumIterators() const {
773 // The final read has one dimension with one element.
774 if (!BB)
775 return 1;
776
777 return IVS.size();
778}
779
780unsigned ScopStmt::getNumScattering() const {
781 return isl_map_dim(Scattering, isl_dim_out);
782}
783
784const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
785
786const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
787 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000788 return IVS[Dimension].first;
789}
790
791const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
792 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000793}
794
795const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
796 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000797 PHINode *PN =
798 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000799 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
800}
801
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000802isl_ctx *ScopStmt::getIslCtx() const {
803 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000804}
805
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000806isl_set *ScopStmt::getDomain() const {
807 return isl_set_copy(Domain);
808}
809
Tobias Grosser75805372011-04-29 06:27:02 +0000810ScopStmt::~ScopStmt() {
811 while (!MemAccs.empty()) {
812 delete MemAccs.back();
813 MemAccs.pop_back();
814 }
815
816 isl_set_free(Domain);
817 isl_map_free(Scattering);
818}
819
820void ScopStmt::print(raw_ostream &OS) const {
821 OS << "\t" << getBaseName() << "\n";
822
823 OS.indent(12) << "Domain :=\n";
824
825 if (Domain) {
826 OS.indent(16) << getDomainStr() << ";\n";
827 } else
828 OS.indent(16) << "n/a\n";
829
830 OS.indent(12) << "Scattering :=\n";
831
832 if (Domain) {
833 OS.indent(16) << getScatteringStr() << ";\n";
834 } else
835 OS.indent(16) << "n/a\n";
836
837 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
838 I != E; ++I)
839 (*I)->print(OS);
840}
841
842void ScopStmt::dump() const { print(dbgs()); }
843
844//===----------------------------------------------------------------------===//
845/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000846
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000847void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +0000848 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
849 isl_set_free(Context);
850 Context = NewContext;
851}
852
Tobias Grosser60b54f12011-11-08 15:41:28 +0000853void Scop::addParams(std::vector<const SCEV*> NewParameters) {
854 for (std::vector<const SCEV*>::iterator PI = NewParameters.begin(),
855 PE = NewParameters.end(); PI != PE; ++PI) {
856 const SCEV *Parameter = *PI;
857
858 if (ParameterIds.find(Parameter) != ParameterIds.end())
859 continue;
860
861 int dimension = Parameters.size();
862
863 Parameters.push_back(Parameter);
864 ParameterIds[Parameter] = dimension;
865 }
866}
867
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000868__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
869 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000870
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000871 if (IdIter == ParameterIds.end())
872 return NULL;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000873
Tobias Grosser8f99c162011-11-15 11:38:55 +0000874 std::string ParameterName;
875
876 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
877 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000878 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +0000879 }
880
881 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
882 ParameterName = "p_" + convertInt(IdIter->second);
883
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000884 return isl_id_alloc(getIslCtx(), ParameterName.c_str(), (void *) Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000885}
Tobias Grosser75805372011-04-29 06:27:02 +0000886
Tobias Grosser6be480c2011-11-08 15:41:13 +0000887void Scop::buildContext() {
888 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grosserf5338802011-10-06 00:03:35 +0000889 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000890}
891
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000892void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000893 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000894 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000895
896 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
897 PI != PE; ++PI) {
898 const SCEV *Parameter = PI->first;
899 isl_id *id = getIdForParam(Parameter);
900 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
901 }
902
903 // Align the parameters of all data structures to the model.
904 Context = isl_set_align_params(Context, Space);
905
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000906 for (iterator I = begin(), E = end(); I != E; ++I)
907 (*I)->realignParams();
908}
909
Tobias Grosser0e27e242011-10-06 00:03:48 +0000910Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
911 isl_ctx *Context)
912 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
913 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000914 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000915 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000916
917 SmallVector<Loop*, 8> NestLoops;
918 SmallVector<unsigned, 8> Scatter;
919
920 Scatter.assign(MaxLoopDepth + 1, 0);
921
922 // Build the iteration domain, access functions and scattering functions
923 // traversing the region tree.
924 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
925 Stmts.push_back(new ScopStmt(*this, Scatter));
926
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000927 realignParams();
928
Tobias Grosser75805372011-04-29 06:27:02 +0000929 assert(NestLoops.empty() && "NestLoops not empty at top level!");
930}
931
932Scop::~Scop() {
933 isl_set_free(Context);
934
935 // Free the statements;
936 for (iterator I = begin(), E = end(); I != E; ++I)
937 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000938}
939
940std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000941 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000942}
943
944std::string Scop::getNameStr() const {
945 std::string ExitName, EntryName;
946 raw_string_ostream ExitStr(ExitName);
947 raw_string_ostream EntryStr(EntryName);
948
949 WriteAsOperand(EntryStr, R.getEntry(), false);
950 EntryStr.str();
951
952 if (R.getExit()) {
953 WriteAsOperand(ExitStr, R.getExit(), false);
954 ExitStr.str();
955 } else
956 ExitName = "FunctionExit";
957
958 return EntryName + "---" + ExitName;
959}
960
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000961__isl_give isl_set *Scop::getContext() const {
962 return isl_set_copy(Context);
963}
Tobias Grosser37487052011-10-06 00:03:42 +0000964__isl_give isl_space *Scop::getParamSpace() const {
965 return isl_set_get_space(this->Context);
966}
967
Tobias Grosser75805372011-04-29 06:27:02 +0000968void Scop::printContext(raw_ostream &OS) const {
969 OS << "Context:\n";
970
971 if (!Context) {
972 OS.indent(4) << "n/a\n\n";
973 return;
974 }
975
976 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000977
978 for (ParamVecType::const_iterator PI = Parameters.begin(),
979 PE = Parameters.end(); PI != PE; ++PI) {
980 const SCEV *Parameter = *PI;
981 int Dim = ParameterIds.find(Parameter)->second;
982
983 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
984 }
Tobias Grosser75805372011-04-29 06:27:02 +0000985}
986
987void Scop::printStatements(raw_ostream &OS) const {
988 OS << "Statements {\n";
989
990 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
991 OS.indent(4) << (**SI);
992
993 OS.indent(4) << "}\n";
994}
995
996
997void Scop::print(raw_ostream &OS) const {
998 printContext(OS.indent(4));
999 printStatements(OS.indent(4));
1000}
1001
1002void Scop::dump() const { print(dbgs()); }
1003
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001004isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001005
1006ScalarEvolution *Scop::getSE() const { return SE; }
1007
1008bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1009 if (tempScop.getAccessFunctions(BB))
1010 return false;
1011
1012 return true;
1013}
1014
1015void Scop::buildScop(TempScop &tempScop,
1016 const Region &CurRegion,
1017 SmallVectorImpl<Loop*> &NestLoops,
1018 SmallVectorImpl<unsigned> &Scatter,
1019 LoopInfo &LI) {
1020 Loop *L = castToLoop(CurRegion, LI);
1021
1022 if (L)
1023 NestLoops.push_back(L);
1024
1025 unsigned loopDepth = NestLoops.size();
1026 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1027
1028 for (Region::const_element_iterator I = CurRegion.element_begin(),
1029 E = CurRegion.element_end(); I != E; ++I)
1030 if (I->isSubRegion())
1031 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1032 else {
1033 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1034
1035 if (isTrivialBB(BB, tempScop))
1036 continue;
1037
1038 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
1039 Scatter));
1040
1041 // Increasing the Scattering function is OK for the moment, because
1042 // we are using a depth first iterator and the program is well structured.
1043 ++Scatter[loopDepth];
1044 }
1045
1046 if (!L)
1047 return;
1048
1049 // Exiting a loop region.
1050 Scatter[loopDepth] = 0;
1051 NestLoops.pop_back();
1052 ++Scatter[loopDepth-1];
1053}
1054
1055//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001056ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1057 ctx = isl_ctx_alloc();
1058}
1059
1060ScopInfo::~ScopInfo() {
1061 clear();
1062 isl_ctx_free(ctx);
1063}
1064
1065
Tobias Grosser75805372011-04-29 06:27:02 +00001066
1067void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1068 AU.addRequired<LoopInfo>();
1069 AU.addRequired<RegionInfo>();
1070 AU.addRequired<ScalarEvolution>();
1071 AU.addRequired<TempScopInfo>();
1072 AU.setPreservesAll();
1073}
1074
1075bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1076 LoopInfo &LI = getAnalysis<LoopInfo>();
1077 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1078
1079 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1080
1081 // This region is no Scop.
1082 if (!tempScop) {
1083 scop = 0;
1084 return false;
1085 }
1086
1087 // Statistics.
1088 ++ScopFound;
1089 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1090
Tobias Grosserb76f38532011-08-20 11:11:25 +00001091 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001092
1093 return false;
1094}
1095
1096char ScopInfo::ID = 0;
1097
Tobias Grosser73600b82011-10-08 00:30:40 +00001098INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1099 "Polly - Create polyhedral description of Scops", false,
1100 false)
1101INITIALIZE_PASS_DEPENDENCY(LoopInfo)
1102INITIALIZE_PASS_DEPENDENCY(RegionInfo)
1103INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1104INITIALIZE_PASS_DEPENDENCY(TempScopInfo)
1105INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1106 "Polly - Create polyhedral description of Scops", false,
1107 false)
Tobias Grosser75805372011-04-29 06:27:02 +00001108
1109Pass *polly::createScopInfoPass() {
1110 return new ScopInfo();
1111}