blob: 7c5ce1d2d53ee1c8d8f84dc6ae828da218db436a [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"
Hongbin Zheng86a37742012-04-25 08:01:38 +000034#include "llvm/ADT/StringExtras.h"
Tobias Grosser75805372011-04-29 06:27:02 +000035#include "llvm/Support/CommandLine.h"
36
37#define DEBUG_TYPE "polly-scops"
38#include "llvm/Support/Debug.h"
39
40#include "isl/constraint.h"
41#include "isl/set.h"
42#include "isl/map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000043#include "isl/aff.h"
44#include "isl/printer.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000045#include "isl/local_space.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000046#include "isl/options.h"
Tobias Grosser75805372011-04-29 06:27:02 +000047#include <sstream>
48#include <string>
49#include <vector>
50
51using namespace llvm;
52using namespace polly;
53
54STATISTIC(ScopFound, "Number of valid Scops");
55STATISTIC(RichScopFound, "Number of Scops containing a loop");
56
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000057/// Translate a SCEVExpression into an isl_pw_aff object.
58struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff*> {
59private:
60 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000061 int NbLoopSpaces;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000062 const Scop *scop;
63
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000064public:
Tobias Grossere5e171e2011-11-10 12:45:03 +000065 static isl_pw_aff *getPwAff(ScopStmt *stmt, const SCEV *scev) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000066 Scop *S = stmt->getParent();
67 const Region *Reg = &S->getRegion();
68
Tobias Grossere5e171e2011-11-10 12:45:03 +000069 S->addParams(getParamsInAffineExpr(Reg, scev, *S->getSE()));
Tobias Grosser60b54f12011-11-08 15:41:28 +000070
Tobias Grossere5e171e2011-11-10 12:45:03 +000071 SCEVAffinator Affinator(stmt);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000072 return Affinator.visit(scev);
73 }
74
75 isl_pw_aff *visit(const SCEV *scev) {
Tobias Grosser76c2e322011-11-07 12:58:59 +000076 // In case the scev is a valid parameter, we do not further analyze this
77 // expression, but create a new parameter in the isl_pw_aff. This allows us
78 // to treat subexpressions that we cannot translate into an piecewise affine
79 // expression, as constant parameters of the piecewise affine expression.
80 if (isl_id *Id = scop->getIdForParam(scev)) {
81 isl_space *Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
82 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000083
Tobias Grosser76c2e322011-11-07 12:58:59 +000084 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
85 isl_aff *Affine = isl_aff_zero_on_domain(
86 isl_local_space_from_space(Space));
87 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000088
Tobias Grosser76c2e322011-11-07 12:58:59 +000089 return isl_pw_aff_alloc(Domain, Affine);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000090 }
91
92 return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev);
93 }
94
Tobias Grossere5e171e2011-11-10 12:45:03 +000095 SCEVAffinator(const ScopStmt *stmt) :
Tobias Grosser3c69fab2011-10-06 00:03:54 +000096 ctx(stmt->getIslCtx()),
Tobias Grosserf5338802011-10-06 00:03:35 +000097 NbLoopSpaces(stmt->getNumIterators()),
Tobias Grossere5e171e2011-11-10 12:45:03 +000098 scop(stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000099
100 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) {
101 ConstantInt *Value = Constant->getValue();
102 isl_int v;
103 isl_int_init(v);
104
105 // LLVM does not define if an integer value is interpreted as a signed or
106 // unsigned value. Hence, without further information, it is unknown how
107 // this value needs to be converted to GMP. At the moment, we only support
108 // signed operations. So we just interpret it as signed. Later, there are
109 // two options:
110 //
111 // 1. We always interpret any value as signed and convert the values on
112 // demand.
113 // 2. We pass down the signedness of the calculation and use it to interpret
114 // this constant correctly.
115 MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true);
116
Tobias Grosserf5338802011-10-06 00:03:35 +0000117 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
118 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
119 isl_aff *Affine = isl_aff_zero_on_domain(ls);
120 isl_set *Domain = isl_set_universe(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000121
122 Affine = isl_aff_add_constant(Affine, v);
123 isl_int_clear(v);
124
125 return isl_pw_aff_alloc(Domain, Affine);
126 }
127
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000128 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000129 assert(0 && "Not yet supported");
130 }
131
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000132 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000133 assert(0 && "Not yet supported");
134 }
135
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000136 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000137 // Assuming the value is signed, a sign extension is basically a noop.
138 // TODO: Reconsider this as soon as we support unsigned values.
139 return visit(Expr->getOperand());
140 }
141
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000142 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000143 isl_pw_aff *Sum = visit(Expr->getOperand(0));
144
145 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
146 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
147 Sum = isl_pw_aff_add(Sum, NextSummand);
148 }
149
150 // TODO: Check for NSW and NUW.
151
152 return Sum;
153 }
154
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000155 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000156 isl_pw_aff *Product = visit(Expr->getOperand(0));
157
158 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
159 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
160
161 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
162 isl_pw_aff_free(Product);
163 isl_pw_aff_free(NextOperand);
164 return NULL;
165 }
166
167 Product = isl_pw_aff_mul(Product, NextOperand);
168 }
169
170 // TODO: Check for NSW and NUW.
171 return Product;
172 }
173
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000174 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000175 assert(0 && "Not yet supported");
176 }
177
178 int getLoopDepth(const Loop *L) {
179 Loop *outerLoop =
180 scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L));
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000181 assert(outerLoop && "Scop does not contain this loop");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000182 return L->getLoopDepth() - outerLoop->getLoopDepth();
183 }
184
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000185 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000186 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser7b0ee0e2011-11-04 10:08:03 +0000187 assert(scop->getRegion().contains(Expr->getLoop())
188 && "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000189
190 isl_pw_aff *Start = visit(Expr->getStart());
191 isl_pw_aff *Step = visit(Expr->getOperand(1));
Tobias Grosserf5338802011-10-06 00:03:35 +0000192 isl_space *Space = isl_space_set_alloc(ctx, 0, NbLoopSpaces);
193 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000194
195 int loopDimension = getLoopDepth(Expr->getLoop());
196
Tobias Grosserf5338802011-10-06 00:03:35 +0000197 isl_aff *LAff = isl_aff_set_coefficient_si(
198 isl_aff_zero_on_domain (LocalSpace), isl_dim_in, loopDimension, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000199 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
200
201 // TODO: Do we need to check for NSW and NUW?
202 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
203 }
204
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000205 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000206 isl_pw_aff *Max = visit(Expr->getOperand(0));
207
208 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
209 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
210 Max = isl_pw_aff_max(Max, NextOperand);
211 }
212
213 return Max;
214 }
215
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000216 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000217 assert(0 && "Not yet supported");
218 }
219
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000220 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000221 Value *Value = Expr->getValue();
222
Tobias Grosserf5338802011-10-06 00:03:35 +0000223 isl_space *Space;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000224
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000225 std::string ValueName = Value->getName();
226 isl_id *ID = isl_id_alloc(ctx, ValueName.c_str(), Value);
Tobias Grossere5e171e2011-11-10 12:45:03 +0000227 Space = isl_space_set_alloc(ctx, 1, NbLoopSpaces);
228 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, ID);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000229
Tobias Grosserf5338802011-10-06 00:03:35 +0000230 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
231 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000232
Tobias Grossere5e171e2011-11-10 12:45:03 +0000233 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000234
235 return isl_pw_aff_alloc(Domain, Affine);
236 }
237};
238
Tobias Grosser75805372011-04-29 06:27:02 +0000239//===----------------------------------------------------------------------===//
240
241MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000242 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000243 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000244}
245
246static void replace(std::string& str, const std::string& find,
247 const std::string& replace) {
248 size_t pos = 0;
249 while((pos = str.find(find, pos)) != std::string::npos)
250 {
251 str.replace(pos, find.length(), replace);
252 pos += replace.length();
253 }
254}
255
256static void makeIslCompatible(std::string& str) {
Tobias Grossereec4d56e2011-08-20 11:11:14 +0000257 str.erase(0, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000258 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000259 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000260}
261
262void MemoryAccess::setBaseName() {
263 raw_string_ostream OS(BaseName);
264 WriteAsOperand(OS, getBaseAddr(), false);
265 BaseName = OS.str();
266
Tobias Grosser75805372011-04-29 06:27:02 +0000267 makeIslCompatible(BaseName);
268 BaseName = "MemRef_" + BaseName;
269}
270
Tobias Grosser5d453812011-10-06 00:04:11 +0000271isl_map *MemoryAccess::getAccessRelation() const {
272 return isl_map_copy(AccessRelation);
273}
274
275std::string MemoryAccess::getAccessRelationStr() const {
276 return stringFromIslObj(AccessRelation);
277}
278
279isl_map *MemoryAccess::getNewAccessRelation() const {
280 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000281}
282
283isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000284 isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0,
Tobias Grosserf5338802011-10-06 00:03:35 +0000285 Statement->getNumIterators(), 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000286 setBaseName();
287
Tobias Grosserf5338802011-10-06 00:03:35 +0000288 Space = isl_space_set_tuple_name(Space, isl_dim_out, getBaseName().c_str());
289 Space = isl_space_set_tuple_name(Space, isl_dim_in, Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000290
Tobias Grosserf5338802011-10-06 00:03:35 +0000291 return isl_basic_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000292}
293
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000294MemoryAccess::MemoryAccess(const IRAccess &Access, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000295 newAccessRelation = NULL;
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000296 Type = Access.isRead() ? Read : Write;
Tobias Grosser75805372011-04-29 06:27:02 +0000297 statement = Statement;
298
Tobias Grosser9759f852011-11-10 12:44:55 +0000299 BaseAddr = Access.getBase();
Tobias Grosser5683df42011-11-09 22:34:34 +0000300
Tobias Grossera1879642011-12-20 10:43:14 +0000301 if (!Access.isAffine()) {
302 Type = (Type == Read) ? Read : MayWrite;
303 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
304 return;
305 }
306
307 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, Access.getOffset());
308
Tobias Grosser5683df42011-11-09 22:34:34 +0000309 setBaseName();
Tobias Grosser75805372011-04-29 06:27:02 +0000310
Tobias Grosserf9fbbdf2012-04-09 19:46:05 +0000311 // Divide the access function by the size of the elements in the array.
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000312 //
313 // A stride one array access in C expressed as A[i] is expressed in LLVM-IR
314 // as something like A[i * elementsize]. This hides the fact that two
315 // subsequent values of 'i' index two values that are stored next to each
Tobias Grosserf9fbbdf2012-04-09 19:46:05 +0000316 // other in memory. By this division we make this characteristic obvious
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000317 // again.
Tobias Grosser75805372011-04-29 06:27:02 +0000318 isl_int v;
319 isl_int_init(v);
Tobias Grossere4e2f7b2011-11-09 22:35:09 +0000320 isl_int_set_si(v, Access.getElemSizeInBytes());
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000321 Affine = isl_pw_aff_scale_down(Affine, v);
322 isl_int_clear(v);
Tobias Grosser75805372011-04-29 06:27:02 +0000323
Tobias Grosser7d4cee42011-08-19 23:34:28 +0000324 AccessRelation = isl_map_from_pw_aff(Affine);
325 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_in,
326 Statement->getBaseName());
Tobias Grosser75805372011-04-29 06:27:02 +0000327 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
328 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000329}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000330
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000331void MemoryAccess::realignParams() {
332 isl_space *ParamSpace = statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000333 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000334}
335
336MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000337 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000338 BaseAddr = BaseAddress;
339 Type = Read;
340 statement = Statement;
341
342 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
343 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000344 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
345 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000346}
347
348void MemoryAccess::print(raw_ostream &OS) const {
349 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000350 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000351}
352
353void MemoryAccess::dump() const {
354 print(errs());
355}
356
357// Create a map in the size of the provided set domain, that maps from the
358// one element of the provided set domain to another element of the provided
359// set domain.
360// The mapping is limited to all points that are equal in all but the last
361// dimension and for which the last dimension of the input is strict smaller
362// than the last dimension of the output.
363//
364// getEqualAndLarger(set[i0, i1, ..., iX]):
365//
366// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
367// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
368//
Tobias Grosserf5338802011-10-06 00:03:35 +0000369static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000370 isl_space *Space = isl_space_map_from_set(setDomain);
371 isl_map *Map = isl_map_universe(isl_space_copy(Space));
372 isl_local_space *MapLocalSpace = isl_local_space_from_space(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000373
374 // Set all but the last dimension to be equal for the input and output
375 //
376 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
377 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000378 for (unsigned i = 0; i < isl_map_dim(Map, isl_dim_in) - 1; ++i)
379 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000380
381 // Set the last dimension of the input to be strict smaller than the
382 // last dimension of the output.
383 //
384 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
385 //
Tobias Grosserc327932c2012-02-01 14:23:36 +0000386 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000387 isl_int v;
388 isl_int_init(v);
Tobias Grosserf5338802011-10-06 00:03:35 +0000389 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosser75805372011-04-29 06:27:02 +0000390 isl_int_set_si(v, -1);
391 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
392 isl_int_set_si(v, 1);
393 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
394 isl_int_set_si(v, -1);
395 isl_constraint_set_constant(c, v);
396 isl_int_clear(v);
397
Tobias Grosserc327932c2012-02-01 14:23:36 +0000398 Map = isl_map_add_constraint(Map, c);
Tobias Grosser75805372011-04-29 06:27:02 +0000399
Tobias Grosser23b36662011-10-17 08:32:36 +0000400 isl_local_space_free(MapLocalSpace);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000401 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000402}
403
Tobias Grosser28dd4862012-01-24 16:42:16 +0000404isl_set *MemoryAccess::getStride(__isl_take const isl_set *domainSubset) const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000405 isl_map *accessRelation = getAccessRelation();
Tobias Grosser28dd4862012-01-24 16:42:16 +0000406 isl_set *scatteringDomain = const_cast<isl_set*>(domainSubset);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000407 isl_map *scattering = getStatement()->getScattering();
Tobias Grosser75805372011-04-29 06:27:02 +0000408
409 scattering = isl_map_reverse(scattering);
410 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
411 scattering = isl_map_project_out(scattering, isl_dim_in,
412 isl_set_n_dim(scatteringDomain),
413 difference);
414
415 // Remove all names of the scattering dimensions, as the names may be lost
416 // anyways during the project. This leads to consistent results.
417 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
418 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
419
Tobias Grosserf5338802011-10-06 00:03:35 +0000420 isl_map *nextScatt = getEqualAndLarger(isl_set_get_space(scatteringDomain));
Tobias Grosser75805372011-04-29 06:27:02 +0000421 nextScatt = isl_map_lexmin(nextScatt);
422
423 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
424
425 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
426 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
427 nextScatt = isl_map_apply_domain(nextScatt, scattering);
428 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
429
430 return isl_map_deltas(nextScatt);
431}
432
Tobias Grosser28dd4862012-01-24 16:42:16 +0000433bool MemoryAccess::isStrideX(__isl_take const isl_set *DomainSubset,
434 int StrideWidth) const {
435 isl_set *Stride, *StrideX;
436 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000437
Tobias Grosser030b0d72012-01-17 20:39:11 +0000438 Stride = getStride(DomainSubset);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000439 StrideX = isl_set_universe(isl_set_get_space(Stride));
440 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
441 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000442
Tobias Grosser28dd4862012-01-24 16:42:16 +0000443 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000444 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000445
Tobias Grosser28dd4862012-01-24 16:42:16 +0000446 return IsStrideX;
447}
448
449bool MemoryAccess::isStrideZero(const isl_set *DomainSubset) const {
450 return isStrideX(DomainSubset, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000451}
452
Tobias Grosserdea98232012-01-17 20:34:27 +0000453bool MemoryAccess::isStrideOne(const isl_set *DomainSubset) const {
Tobias Grosser28dd4862012-01-24 16:42:16 +0000454 return isStrideX(DomainSubset, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000455}
456
Tobias Grosser5d453812011-10-06 00:04:11 +0000457void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000458 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000459 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000460}
Tobias Grosser75805372011-04-29 06:27:02 +0000461
462//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000463
464isl_map *ScopStmt::getScattering() const {
465 return isl_map_copy(Scattering);
466}
467
468void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000469 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000470 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000471}
472
Tobias Grosser75805372011-04-29 06:27:02 +0000473void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000474 unsigned NbIterators = getNumIterators();
475 unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1;
476
477 isl_space *Space = isl_space_alloc(getIslCtx(), 0, NbIterators,
478 NbScatteringDims);
Tobias Grosserf5338802011-10-06 00:03:35 +0000479 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
480 Space = isl_space_set_tuple_name(Space, isl_dim_in, getBaseName());
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000481
482 Scattering = isl_map_universe(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000483
484 // Loop dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000485 for (unsigned i = 0; i < NbIterators; ++i)
486 Scattering = isl_map_equate(Scattering, isl_dim_out, 2 * i + 1,
487 isl_dim_in, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000488
489 // Constant dimensions
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000490 for (unsigned i = 0; i < NbIterators + 1; ++i)
491 Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000492
493 // Fill scattering dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000494 for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i)
495 Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000496
Tobias Grosser37487052011-10-06 00:03:42 +0000497 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000498}
499
500void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
501 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
502
503 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
504 E = AccFuncs->end(); I != E; ++I) {
505 MemAccs.push_back(new MemoryAccess(I->first, this));
506 InstructionToAccess[I->second] = MemAccs.back();
507 }
508}
509
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000510void ScopStmt::realignParams() {
511 for (memacc_iterator MI = memacc_begin(), ME = memacc_end(); MI != ME; ++MI)
512 (*MI)->realignParams();
513
514 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
515 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
516}
517
Tobias Grosser65b00582011-11-08 15:41:19 +0000518__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000519 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
520 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000521
Tobias Grosserd2795d02011-08-18 07:51:40 +0000522 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000523 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000524 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000525 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000526 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000527 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000528 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000529 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000530 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000531 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000532 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000533 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000534 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000535 case ICmpInst::ICMP_ULT:
536 case ICmpInst::ICMP_UGT:
537 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000538 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000539 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000540 default:
541 llvm_unreachable("Non integer predicate not supported");
542 }
Tobias Grosser75805372011-04-29 06:27:02 +0000543}
544
Tobias Grossere19661e2011-10-07 08:46:57 +0000545__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000546 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000547 isl_space *Space;
548 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000549
Tobias Grossere19661e2011-10-07 08:46:57 +0000550 Space = isl_set_get_space(Domain);
551 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000552
Tobias Grosser75805372011-04-29 06:27:02 +0000553 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000554 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
555 isl_pw_aff *IV = isl_pw_aff_from_aff(
556 isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000557
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000558 // 0 <= IV.
559 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
560 Domain = isl_set_intersect(Domain, LowerBound);
561
562 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000563 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000564 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000565 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
566 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000567 Domain = isl_set_intersect(Domain, UpperBoundSet);
568 }
569
Tobias Grosserf5338802011-10-06 00:03:35 +0000570 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000571 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000572}
573
Tobias Grossere19661e2011-10-07 08:46:57 +0000574__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
575 TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000576 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000577 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
578 *CurrentRegion = &CurRegion;
579 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000580
Tobias Grosser75805372011-04-29 06:27:02 +0000581 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000582 if (BranchingBB != CurrentRegion->getEntry()) {
583 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
584 for (BBCond::const_iterator CI = Condition->begin(),
585 CE = Condition->end(); CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000586 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000587 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000588 }
589 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000590 BranchingBB = CurrentRegion->getEntry();
591 CurrentRegion = CurrentRegion->getParent();
592 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000593
Tobias Grossere19661e2011-10-07 08:46:57 +0000594 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000595}
596
Tobias Grossere19661e2011-10-07 08:46:57 +0000597__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
Tobias Grosser65b00582011-11-08 15:41:19 +0000598 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000599 isl_space *Space;
600 isl_set *Domain;
601
602 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
603
604 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000605 Domain = addLoopBoundsToDomain(Domain, tempScop);
606 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
607 Domain = isl_set_set_tuple_name(Domain, getBaseName());
608
609 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000610}
611
612ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
613 const Region &CurRegion, BasicBlock &bb,
614 SmallVectorImpl<Loop*> &NestLoops,
615 SmallVectorImpl<unsigned> &Scatter)
616 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
617 // Setup the induction variables.
618 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
619 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
620 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000621 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000622 }
623
624 raw_string_ostream OS(BaseName);
625 WriteAsOperand(OS, &bb, false);
626 BaseName = OS.str();
627
Tobias Grosser75805372011-04-29 06:27:02 +0000628 makeIslCompatible(BaseName);
629 BaseName = "Stmt_" + BaseName;
630
Tobias Grossere19661e2011-10-07 08:46:57 +0000631 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000632 buildScattering(Scatter);
633 buildAccesses(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000634}
635
Tobias Grosser75805372011-04-29 06:27:02 +0000636std::string ScopStmt::getDomainStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000637 return stringFromIslObj(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000638}
639
640std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000641 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000642}
643
644unsigned ScopStmt::getNumParams() const {
645 return Parent.getNumParams();
646}
647
648unsigned ScopStmt::getNumIterators() const {
649 // The final read has one dimension with one element.
650 if (!BB)
651 return 1;
652
653 return IVS.size();
654}
655
656unsigned ScopStmt::getNumScattering() const {
657 return isl_map_dim(Scattering, isl_dim_out);
658}
659
660const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
661
662const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
663 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000664 return IVS[Dimension].first;
665}
666
667const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
668 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000669}
670
671const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
672 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000673 PHINode *PN =
674 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000675 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
676}
677
Tobias Grosser3c69fab2011-10-06 00:03:54 +0000678isl_ctx *ScopStmt::getIslCtx() const {
679 return Parent.getIslCtx();
Tobias Grosser75805372011-04-29 06:27:02 +0000680}
681
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000682isl_set *ScopStmt::getDomain() const {
683 return isl_set_copy(Domain);
684}
685
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000686isl_space *ScopStmt::getDomainSpace() const {
687 return isl_set_get_space(Domain);
688}
689
Tobias Grosser75805372011-04-29 06:27:02 +0000690ScopStmt::~ScopStmt() {
691 while (!MemAccs.empty()) {
692 delete MemAccs.back();
693 MemAccs.pop_back();
694 }
695
696 isl_set_free(Domain);
697 isl_map_free(Scattering);
698}
699
700void ScopStmt::print(raw_ostream &OS) const {
701 OS << "\t" << getBaseName() << "\n";
702
703 OS.indent(12) << "Domain :=\n";
704
705 if (Domain) {
706 OS.indent(16) << getDomainStr() << ";\n";
707 } else
708 OS.indent(16) << "n/a\n";
709
710 OS.indent(12) << "Scattering :=\n";
711
712 if (Domain) {
713 OS.indent(16) << getScatteringStr() << ";\n";
714 } else
715 OS.indent(16) << "n/a\n";
716
717 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
718 I != E; ++I)
719 (*I)->print(OS);
720}
721
722void ScopStmt::dump() const { print(dbgs()); }
723
724//===----------------------------------------------------------------------===//
725/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000726
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000727void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +0000728 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
729 isl_set_free(Context);
730 Context = NewContext;
731}
732
Tobias Grosser60b54f12011-11-08 15:41:28 +0000733void Scop::addParams(std::vector<const SCEV*> NewParameters) {
734 for (std::vector<const SCEV*>::iterator PI = NewParameters.begin(),
735 PE = NewParameters.end(); PI != PE; ++PI) {
736 const SCEV *Parameter = *PI;
737
738 if (ParameterIds.find(Parameter) != ParameterIds.end())
739 continue;
740
741 int dimension = Parameters.size();
742
743 Parameters.push_back(Parameter);
744 ParameterIds[Parameter] = dimension;
745 }
746}
747
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000748__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
749 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000750
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000751 if (IdIter == ParameterIds.end())
752 return NULL;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000753
Tobias Grosser8f99c162011-11-15 11:38:55 +0000754 std::string ParameterName;
755
756 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
757 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000758 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +0000759 }
760
761 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +0000762 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +0000763
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000764 return isl_id_alloc(getIslCtx(), ParameterName.c_str(), (void *) Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000765}
Tobias Grosser75805372011-04-29 06:27:02 +0000766
Tobias Grosser6be480c2011-11-08 15:41:13 +0000767void Scop::buildContext() {
768 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grosserf5338802011-10-06 00:03:35 +0000769 Context = isl_set_universe (Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000770}
771
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000772void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000773 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000774 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000775
776 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
777 PI != PE; ++PI) {
778 const SCEV *Parameter = PI->first;
779 isl_id *id = getIdForParam(Parameter);
780 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
781 }
782
783 // Align the parameters of all data structures to the model.
784 Context = isl_set_align_params(Context, Space);
785
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000786 for (iterator I = begin(), E = end(); I != E; ++I)
787 (*I)->realignParams();
788}
789
Tobias Grosser0e27e242011-10-06 00:03:48 +0000790Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
791 isl_ctx *Context)
792 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
793 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000794 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000795 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000796
797 SmallVector<Loop*, 8> NestLoops;
798 SmallVector<unsigned, 8> Scatter;
799
800 Scatter.assign(MaxLoopDepth + 1, 0);
801
802 // Build the iteration domain, access functions and scattering functions
803 // traversing the region tree.
804 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +0000805
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000806 realignParams();
807
Tobias Grosser75805372011-04-29 06:27:02 +0000808 assert(NestLoops.empty() && "NestLoops not empty at top level!");
809}
810
811Scop::~Scop() {
812 isl_set_free(Context);
813
814 // Free the statements;
815 for (iterator I = begin(), E = end(); I != E; ++I)
816 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000817}
818
819std::string Scop::getContextStr() const {
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000820 return stringFromIslObj(Context);
Tobias Grosser75805372011-04-29 06:27:02 +0000821}
822
823std::string Scop::getNameStr() const {
824 std::string ExitName, EntryName;
825 raw_string_ostream ExitStr(ExitName);
826 raw_string_ostream EntryStr(EntryName);
827
828 WriteAsOperand(EntryStr, R.getEntry(), false);
829 EntryStr.str();
830
831 if (R.getExit()) {
832 WriteAsOperand(ExitStr, R.getExit(), false);
833 ExitStr.str();
834 } else
835 ExitName = "FunctionExit";
836
837 return EntryName + "---" + ExitName;
838}
839
Tobias Grosser4da8d9f2011-10-06 00:03:59 +0000840__isl_give isl_set *Scop::getContext() const {
841 return isl_set_copy(Context);
842}
Tobias Grosser37487052011-10-06 00:03:42 +0000843__isl_give isl_space *Scop::getParamSpace() const {
844 return isl_set_get_space(this->Context);
845}
846
Tobias Grosser75805372011-04-29 06:27:02 +0000847void Scop::printContext(raw_ostream &OS) const {
848 OS << "Context:\n";
849
850 if (!Context) {
851 OS.indent(4) << "n/a\n\n";
852 return;
853 }
854
855 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000856
857 for (ParamVecType::const_iterator PI = Parameters.begin(),
858 PE = Parameters.end(); PI != PE; ++PI) {
859 const SCEV *Parameter = *PI;
860 int Dim = ParameterIds.find(Parameter)->second;
861
862 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
863 }
Tobias Grosser75805372011-04-29 06:27:02 +0000864}
865
866void Scop::printStatements(raw_ostream &OS) const {
867 OS << "Statements {\n";
868
869 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
870 OS.indent(4) << (**SI);
871
872 OS.indent(4) << "}\n";
873}
874
875
876void Scop::print(raw_ostream &OS) const {
877 printContext(OS.indent(4));
878 printStatements(OS.indent(4));
879}
880
881void Scop::dump() const { print(dbgs()); }
882
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000883isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +0000884
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000885__isl_give isl_union_set *Scop::getDomains() {
886 isl_union_set *Domain = NULL;
887
888 for (Scop::iterator SI = begin(), SE = end(); SI != SE; ++SI)
Tobias Grosser3cbe5cf2012-03-08 15:21:51 +0000889 if (!Domain)
Tobias Grosser5f9a7622012-02-14 14:02:40 +0000890 Domain = isl_union_set_from_set((*SI)->getDomain());
891 else
892 Domain = isl_union_set_union(Domain,
893 isl_union_set_from_set((*SI)->getDomain()));
894
895 return Domain;
896}
897
Tobias Grosser75805372011-04-29 06:27:02 +0000898ScalarEvolution *Scop::getSE() const { return SE; }
899
900bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
901 if (tempScop.getAccessFunctions(BB))
902 return false;
903
904 return true;
905}
906
907void Scop::buildScop(TempScop &tempScop,
908 const Region &CurRegion,
909 SmallVectorImpl<Loop*> &NestLoops,
910 SmallVectorImpl<unsigned> &Scatter,
911 LoopInfo &LI) {
912 Loop *L = castToLoop(CurRegion, LI);
913
914 if (L)
915 NestLoops.push_back(L);
916
917 unsigned loopDepth = NestLoops.size();
918 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
919
920 for (Region::const_element_iterator I = CurRegion.element_begin(),
921 E = CurRegion.element_end(); I != E; ++I)
922 if (I->isSubRegion())
923 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
924 else {
925 BasicBlock *BB = I->getNodeAs<BasicBlock>();
926
927 if (isTrivialBB(BB, tempScop))
928 continue;
929
930 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
931 Scatter));
932
933 // Increasing the Scattering function is OK for the moment, because
934 // we are using a depth first iterator and the program is well structured.
935 ++Scatter[loopDepth];
936 }
937
938 if (!L)
939 return;
940
941 // Exiting a loop region.
942 Scatter[loopDepth] = 0;
943 NestLoops.pop_back();
944 ++Scatter[loopDepth-1];
945}
946
947//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +0000948ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
949 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +0000950 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000951}
952
953ScopInfo::~ScopInfo() {
954 clear();
955 isl_ctx_free(ctx);
956}
957
958
Tobias Grosser75805372011-04-29 06:27:02 +0000959
960void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
961 AU.addRequired<LoopInfo>();
962 AU.addRequired<RegionInfo>();
963 AU.addRequired<ScalarEvolution>();
964 AU.addRequired<TempScopInfo>();
965 AU.setPreservesAll();
966}
967
968bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
969 LoopInfo &LI = getAnalysis<LoopInfo>();
970 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
971
972 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
973
974 // This region is no Scop.
975 if (!tempScop) {
976 scop = 0;
977 return false;
978 }
979
980 // Statistics.
981 ++ScopFound;
982 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
983
Tobias Grosserb76f38532011-08-20 11:11:25 +0000984 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +0000985
986 return false;
987}
988
989char ScopInfo::ID = 0;
990
Tobias Grosser73600b82011-10-08 00:30:40 +0000991INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
992 "Polly - Create polyhedral description of Scops", false,
993 false)
994INITIALIZE_PASS_DEPENDENCY(LoopInfo)
995INITIALIZE_PASS_DEPENDENCY(RegionInfo)
996INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
997INITIALIZE_PASS_DEPENDENCY(TempScopInfo)
998INITIALIZE_PASS_END(ScopInfo, "polly-scops",
999 "Polly - Create polyhedral description of Scops", false,
1000 false)
Tobias Grosser75805372011-04-29 06:27:02 +00001001
1002Pass *polly::createScopInfoPass() {
1003 return new ScopInfo();
1004}