blob: 311e60ada4f74f6b3d7767409b8383879c071408 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Create a polyhedral description for a static control flow region.
11//
12// The pass creates a polyhedral description of the Scops detected by the Scop
13// detection derived from their LLVM-IR code.
14//
15// This represantation is shared among several tools in the polyhedral
16// community, which are e.g. Cloog, Pluto, Loopo, Graphite.
17//
18//===----------------------------------------------------------------------===//
19
20#include "polly/ScopInfo.h"
21
22#include "polly/TempScopInfo.h"
23#include "polly/LinkAllPasses.h"
24#include "polly/Support/GICHelper.h"
25#include "polly/Support/ScopHelper.h"
26
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/Analysis/RegionIterator.h"
30#include "llvm/Assembly/Writer.h"
31#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/SetVector.h"
33#include "llvm/Support/CommandLine.h"
34
35#define DEBUG_TYPE "polly-scops"
36#include "llvm/Support/Debug.h"
37
38#include "isl/constraint.h"
39#include "isl/set.h"
40#include "isl/map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000041#include "isl/aff.h"
42#include "isl/printer.h"
Tobias Grosser75805372011-04-29 06:27:02 +000043#include <sstream>
44#include <string>
45#include <vector>
46
47using namespace llvm;
48using namespace polly;
49
50STATISTIC(ScopFound, "Number of valid Scops");
51STATISTIC(RichScopFound, "Number of Scops containing a loop");
52
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000053/// Convert an int into a string.
54static std::string convertInt(int number)
55{
56 if (number == 0)
57 return "0";
58 std::string temp = "";
59 std::string returnvalue = "";
60 while (number > 0)
61 {
62 temp += number % 10 + 48;
63 number /= 10;
64 }
65 for (unsigned i = 0; i < temp.length(); i++)
66 returnvalue+=temp[temp.length() - i - 1];
67 return returnvalue;
Tobias Grosser75805372011-04-29 06:27:02 +000068}
69
Tobias Grosser30b8a092011-08-18 07:51:37 +000070static isl_set *set_remove_dim_ids(isl_set *set) {
71 isl_ctx *ctx = isl_set_get_ctx(set);
72 int numParams = isl_set_n_param(set);
73 isl_printer *p = isl_printer_to_str(ctx);
74 isl_set *new_set;
75 char *str;
76 const char *name = isl_set_get_tuple_name(set);
77
78 p = isl_printer_set_output_format (p, ISL_FORMAT_EXT_POLYLIB);
79 p = isl_printer_print_set(p, set);
80
81 str = isl_printer_get_str (p);
82 new_set = isl_set_read_from_str (ctx, str, numParams);
83 new_set = isl_set_set_tuple_name(new_set, name);
84 free (str);
85 isl_set_free (set);
86 isl_printer_free (p);
87 return new_set;
88}
89
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000090static isl_map *map_remove_dim_ids(isl_map *map) {
91 isl_ctx *ctx = isl_map_get_ctx(map);
92 int numParams = isl_map_n_param(map);
93 isl_printer *p = isl_printer_to_str(ctx);
94 char *str;
Tobias Grosser30b8a092011-08-18 07:51:37 +000095 isl_map *new_map;
96 const char *name_in = isl_map_get_tuple_name(map, isl_dim_in);
97 const char *name_out = isl_map_get_tuple_name(map, isl_dim_out);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000098
99 p = isl_printer_set_output_format (p, ISL_FORMAT_EXT_POLYLIB);
100 p = isl_printer_print_map(p, map);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000101
102 str = isl_printer_get_str (p);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000103 new_map = isl_map_read_from_str (ctx, str, numParams);
104 new_map = isl_map_set_tuple_name(new_map, isl_dim_in, name_in);
105 new_map = isl_map_set_tuple_name(new_map, isl_dim_out, name_out);
106 isl_map_free (map);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000107 free (str);
108 isl_printer_free (p);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000109 return new_map;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000110}
111
112/// Translate a SCEVExpression into an isl_pw_aff object.
113struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff*> {
114private:
115 isl_ctx *ctx;
116 int NbLoopDims;
117 const Scop *scop;
118
119 /// baseAdress is set if we analyze a memory access. It holds the base address
120 /// of this memory access.
121 const Value *baseAddress;
122
123public:
124 static isl_pw_aff *getPwAff(const ScopStmt *stmt, const SCEV *scev,
125 const Value *baseAddress) {
126 SCEVAffinator Affinator(stmt, baseAddress);
127 return Affinator.visit(scev);
128 }
129
130 isl_pw_aff *visit(const SCEV *scev) {
131 // In case the scev is contained in our list of parameters, we do not
132 // further analyze this expression, but create a new parameter in the
133 // isl_pw_aff. This allows us to treat subexpressions that we cannot
134 // translate into an piecewise affine expression, as constant parameters of
135 // the piecewise affine expression.
136 int i = 0;
137 for (Scop::const_param_iterator PI = scop->param_begin(),
138 PE = scop->param_end(); PI != PE; ++PI) {
139 if (*PI == scev) {
140 isl_id *ID = isl_id_alloc(ctx, ("p" + convertInt(i)).c_str(),
141 (void *) scev);
142 isl_dim *Dim = isl_dim_set_alloc(ctx, 1, NbLoopDims);
143 Dim = isl_dim_set_dim_id(Dim, isl_dim_param, 0, ID);
144
145 isl_set *Domain = isl_set_universe(isl_dim_copy(Dim));
146 isl_aff *Affine = isl_aff_zero(isl_local_space_from_dim(Dim));
147 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
148
149 return isl_pw_aff_alloc(Domain, Affine);
150 }
151 i++;
152 }
153
154 return SCEVVisitor<SCEVAffinator, isl_pw_aff*>::visit(scev);
155 }
156
157 SCEVAffinator(const ScopStmt *stmt, const Value *baseAddress) :
158 ctx(stmt->getParent()->getCtx()),
159 NbLoopDims(stmt->getNumIterators()),
160 scop(stmt->getParent()),
161 baseAddress(baseAddress) {};
162
163 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Constant) {
164 ConstantInt *Value = Constant->getValue();
165 isl_int v;
166 isl_int_init(v);
167
168 // LLVM does not define if an integer value is interpreted as a signed or
169 // unsigned value. Hence, without further information, it is unknown how
170 // this value needs to be converted to GMP. At the moment, we only support
171 // signed operations. So we just interpret it as signed. Later, there are
172 // two options:
173 //
174 // 1. We always interpret any value as signed and convert the values on
175 // demand.
176 // 2. We pass down the signedness of the calculation and use it to interpret
177 // this constant correctly.
178 MPZ_from_APInt(v, Value->getValue(), /* isSigned */ true);
179
180 isl_dim *dim = isl_dim_set_alloc(ctx, 0, NbLoopDims);
181 isl_local_space *ls = isl_local_space_from_dim(isl_dim_copy(dim));
182 isl_aff *Affine = isl_aff_zero(ls);
183 isl_set *Domain = isl_set_universe(dim);
184
185 Affine = isl_aff_add_constant(Affine, v);
186 isl_int_clear(v);
187
188 return isl_pw_aff_alloc(Domain, Affine);
189 }
190
191 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr* Expr) {
192 assert(0 && "Not yet supported");
193 }
194
195 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) {
196 assert(0 && "Not yet supported");
197 }
198
199 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr* Expr) {
200 // Assuming the value is signed, a sign extension is basically a noop.
201 // TODO: Reconsider this as soon as we support unsigned values.
202 return visit(Expr->getOperand());
203 }
204
205 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr* Expr) {
206 isl_pw_aff *Sum = visit(Expr->getOperand(0));
207
208 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
209 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
210 Sum = isl_pw_aff_add(Sum, NextSummand);
211 }
212
213 // TODO: Check for NSW and NUW.
214
215 return Sum;
216 }
217
218 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr* Expr) {
219 isl_pw_aff *Product = visit(Expr->getOperand(0));
220
221 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
222 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
223
224 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
225 isl_pw_aff_free(Product);
226 isl_pw_aff_free(NextOperand);
227 return NULL;
228 }
229
230 Product = isl_pw_aff_mul(Product, NextOperand);
231 }
232
233 // TODO: Check for NSW and NUW.
234 return Product;
235 }
236
237 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr* Expr) {
238 assert(0 && "Not yet supported");
239 }
240
241 int getLoopDepth(const Loop *L) {
242 Loop *outerLoop =
243 scop->getRegion().outermostLoopInRegion(const_cast<Loop*>(L));
244 return L->getLoopDepth() - outerLoop->getLoopDepth();
245 }
246
247 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr* Expr) {
248 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
249
250 isl_pw_aff *Start = visit(Expr->getStart());
251 isl_pw_aff *Step = visit(Expr->getOperand(1));
252 isl_dim *Dim = isl_dim_set_alloc (ctx, 0, NbLoopDims);
253 isl_local_space *LocalSpace = isl_local_space_from_dim (Dim);
254
255 int loopDimension = getLoopDepth(Expr->getLoop());
256
257 isl_aff *LAff = isl_aff_set_coefficient_si (isl_aff_zero (LocalSpace),
258 isl_dim_set, loopDimension, 1);
259 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
260
261 // TODO: Do we need to check for NSW and NUW?
262 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
263 }
264
265 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr* Expr) {
266 isl_pw_aff *Max = visit(Expr->getOperand(0));
267
268 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
269 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
270 Max = isl_pw_aff_max(Max, NextOperand);
271 }
272
273 return Max;
274 }
275
276 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr* Expr) {
277 assert(0 && "Not yet supported");
278 }
279
280 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown* Expr) {
281 Value *Value = Expr->getValue();
282
283 isl_dim *Dim;
284
285 /// If baseAddress is set, we ignore its Value object in the scev and do not
286 /// add it to the isl_pw_aff. This is because it is regarded as defining the
287 /// name of an array, in contrast to its array subscript.
288 if (baseAddress != Value) {
289 isl_id *ID = isl_id_alloc(ctx, Value->getNameStr().c_str(), Value);
290 Dim = isl_dim_set_alloc(ctx, 1, NbLoopDims);
291 Dim = isl_dim_set_dim_id(Dim, isl_dim_param, 0, ID);
292 } else {
293 Dim = isl_dim_set_alloc(ctx, 0, NbLoopDims);
294 }
295
296 isl_set *Domain = isl_set_universe(isl_dim_copy(Dim));
297 isl_aff *Affine = isl_aff_zero(isl_local_space_from_dim(Dim));
298
299 if (baseAddress != Value)
300 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
301
302 return isl_pw_aff_alloc(Domain, Affine);
303 }
304};
305
Tobias Grosser75805372011-04-29 06:27:02 +0000306static isl_map *getValueOf(const SCEVAffFunc &AffFunc,
307 const ScopStmt *Statement, isl_dim *dim) {
Tobias Grosser75805372011-04-29 06:27:02 +0000308 assert((AffFunc.getType() == SCEVAffFunc::Eq
309 || AffFunc.getType() == SCEVAffFunc::ReadMem
310 || AffFunc.getType() == SCEVAffFunc::WriteMem)
311 && "AffFunc is not an equality");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000312 isl_pw_aff *Affine = SCEVAffinator::getPwAff(Statement, AffFunc.OriginalSCEV,
313 AffFunc.getBaseAddr());
314 isl_map *Map = isl_map_from_pw_aff(Affine);
315 isl_dim *CtxDim = isl_set_get_dim(Statement->getParent()->getContext());
Tobias Grosser75805372011-04-29 06:27:02 +0000316
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000317 isl_map_align_params(Map, CtxDim);
318 const char *dimname = isl_dim_get_tuple_name(dim, isl_dim_set);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000319 Map = isl_map_set_tuple_name(Map, isl_dim_in, dimname);
320 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000321}
322//===----------------------------------------------------------------------===//
323
324MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000325 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000326 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000327}
328
329static void replace(std::string& str, const std::string& find,
330 const std::string& replace) {
331 size_t pos = 0;
332 while((pos = str.find(find, pos)) != std::string::npos)
333 {
334 str.replace(pos, find.length(), replace);
335 pos += replace.length();
336 }
337}
338
339static void makeIslCompatible(std::string& str) {
340 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000341 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000342}
343
344void MemoryAccess::setBaseName() {
345 raw_string_ostream OS(BaseName);
346 WriteAsOperand(OS, getBaseAddr(), false);
347 BaseName = OS.str();
348
349 // Remove the % in the name. This is not supported by isl.
350 BaseName.erase(0,1);
351 makeIslCompatible(BaseName);
352 BaseName = "MemRef_" + BaseName;
353}
354
355std::string MemoryAccess::getAccessFunctionStr() const {
356 return stringFromIslObj(getAccessFunction());
357}
358
359isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
360 isl_dim *dim = isl_dim_alloc(Statement->getIslContext(),
361 Statement->getNumParams(),
362 Statement->getNumIterators(), 1);
363 setBaseName();
364
365 dim = isl_dim_set_tuple_name(dim, isl_dim_out, getBaseName().c_str());
366 dim = isl_dim_set_tuple_name(dim, isl_dim_in, Statement->getBaseName());
367
368 return isl_basic_map_universe(dim);
369}
370
371MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000372 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000373 BaseAddr = AffFunc.getBaseAddr();
374 Type = AffFunc.isRead() ? Read : Write;
375 statement = Statement;
376
377 setBaseName();
378
379 isl_dim *dim = isl_dim_set_alloc(Statement->getIslContext(),
380 Statement->getNumParams(),
381 Statement->getNumIterators());
382 dim = isl_dim_set_tuple_name(dim, isl_dim_set, Statement->getBaseName());
383
384 AccessRelation = getValueOf(AffFunc, Statement, dim);
385
386 // Devide the access function by the size of the elements in the function.
387 isl_dim *dim2 = isl_dim_alloc(Statement->getIslContext(),
Tobias Grosser30b8a092011-08-18 07:51:37 +0000388 0, 1, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000389 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim2));
390 isl_constraint *c = isl_equality_alloc(dim2);
391 isl_int v;
392 isl_int_init(v);
393 isl_int_set_si(v, -1);
394 isl_constraint_set_coefficient(c, isl_dim_in, 0, v);
395 isl_int_set_si(v, AffFunc.getElemSizeInBytes());
396 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
397
398 bmap = isl_basic_map_add_constraint(bmap, c);
399 isl_map* dataSizeMap = isl_map_from_basic_map(bmap);
400
Tobias Grosser30b8a092011-08-18 07:51:37 +0000401 isl_dim *Model = isl_set_get_dim(Statement->getParent()->getContext());
402 dataSizeMap = isl_map_align_params(dataSizeMap, Model);
403
Tobias Grosser75805372011-04-29 06:27:02 +0000404 AccessRelation = isl_map_apply_range(AccessRelation, dataSizeMap);
405
406 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
407 getBaseName().c_str());
Tobias Grosser30b8a092011-08-18 07:51:37 +0000408
409 // FIXME: Temporarily remove dimension ids.
410 AccessRelation = map_remove_dim_ids(AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000411}
412
413MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000414 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000415 BaseAddr = BaseAddress;
416 Type = Read;
417 statement = Statement;
418
419 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
420 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
421}
422
423void MemoryAccess::print(raw_ostream &OS) const {
424 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
425 OS.indent(16) << getAccessFunctionStr() << ";\n";
426}
427
428void MemoryAccess::dump() const {
429 print(errs());
430}
431
432// Create a map in the size of the provided set domain, that maps from the
433// one element of the provided set domain to another element of the provided
434// set domain.
435// The mapping is limited to all points that are equal in all but the last
436// dimension and for which the last dimension of the input is strict smaller
437// than the last dimension of the output.
438//
439// getEqualAndLarger(set[i0, i1, ..., iX]):
440//
441// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
442// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
443//
444static isl_map *getEqualAndLarger(isl_dim *setDomain) {
445 isl_dim *mapDomain = isl_dim_map_from_set(setDomain);
446 isl_basic_map *bmap = isl_basic_map_universe(mapDomain);
447
448 // Set all but the last dimension to be equal for the input and output
449 //
450 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
451 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
452 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
453 isl_int v;
454 isl_int_init(v);
455 isl_constraint *c = isl_equality_alloc(isl_basic_map_get_dim(bmap));
456
457 isl_int_set_si(v, 1);
458 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
459 isl_int_set_si(v, -1);
460 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
461
462 bmap = isl_basic_map_add_constraint(bmap, c);
463
464 isl_int_clear(v);
465 }
466
467 // Set the last dimension of the input to be strict smaller than the
468 // last dimension of the output.
469 //
470 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
471 //
472 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
473 isl_int v;
474 isl_int_init(v);
475 isl_constraint *c = isl_inequality_alloc(isl_basic_map_get_dim(bmap));
476 isl_int_set_si(v, -1);
477 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
478 isl_int_set_si(v, 1);
479 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
480 isl_int_set_si(v, -1);
481 isl_constraint_set_constant(c, v);
482 isl_int_clear(v);
483
484 bmap = isl_basic_map_add_constraint(bmap, c);
485
486 return isl_map_from_basic_map(bmap);
487}
488
489isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
490 isl_map *accessRelation = isl_map_copy(getAccessFunction());
491 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
492 isl_map *scattering = isl_map_copy(getStatement()->getScattering());
493
494 scattering = isl_map_reverse(scattering);
495 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
496 scattering = isl_map_project_out(scattering, isl_dim_in,
497 isl_set_n_dim(scatteringDomain),
498 difference);
499
500 // Remove all names of the scattering dimensions, as the names may be lost
501 // anyways during the project. This leads to consistent results.
502 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
503 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
504
505 isl_map *nextScatt = getEqualAndLarger(isl_set_get_dim(scatteringDomain));
506 nextScatt = isl_map_lexmin(nextScatt);
507
508 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
509
510 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
511 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
512 nextScatt = isl_map_apply_domain(nextScatt, scattering);
513 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
514
515 return isl_map_deltas(nextScatt);
516}
517
518bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
519 isl_set *stride = getStride(domainSubset);
520 isl_constraint *c = isl_equality_alloc(isl_set_get_dim(stride));
521
522 isl_int v;
523 isl_int_init(v);
524 isl_int_set_si(v, 1);
525 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
526 isl_int_set_si(v, 0);
527 isl_constraint_set_constant(c, v);
528 isl_int_clear(v);
529
530 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_dim(stride));
531
532 bset = isl_basic_set_add_constraint(bset, c);
533 isl_set *strideZero = isl_set_from_basic_set(bset);
534
535 return isl_set_is_equal(stride, strideZero);
536}
537
538bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
539 isl_set *stride = getStride(domainSubset);
540 isl_constraint *c = isl_equality_alloc(isl_set_get_dim(stride));
541
542 isl_int v;
543 isl_int_init(v);
544 isl_int_set_si(v, 1);
545 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
546 isl_int_set_si(v, -1);
547 isl_constraint_set_constant(c, v);
548 isl_int_clear(v);
549
550 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_dim(stride));
551
552 bset = isl_basic_set_add_constraint(bset, c);
553 isl_set *strideZero = isl_set_from_basic_set(bset);
554
555 return isl_set_is_equal(stride, strideZero);
556}
557
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000558void MemoryAccess::setNewAccessFunction(isl_map *newAccess) {
559 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000560}
Tobias Grosser75805372011-04-29 06:27:02 +0000561
562//===----------------------------------------------------------------------===//
563void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
564 unsigned NumberOfIterators = getNumIterators();
565 unsigned ScatDim = Parent.getMaxLoopDepth() * 2 + 1;
566 isl_dim *dim = isl_dim_alloc(Parent.getCtx(), Parent.getNumParams(),
567 NumberOfIterators, ScatDim);
568 dim = isl_dim_set_tuple_name(dim, isl_dim_out, "scattering");
569 dim = isl_dim_set_tuple_name(dim, isl_dim_in, getBaseName());
570 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim));
571 isl_int v;
572 isl_int_init(v);
573
574 // Loop dimensions.
575 for (unsigned i = 0; i < NumberOfIterators; ++i) {
576 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
577 isl_int_set_si(v, 1);
578 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
579 isl_int_set_si(v, -1);
580 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
581
582 bmap = isl_basic_map_add_constraint(bmap, c);
583 }
584
585 // Constant dimensions
586 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
587 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
588 isl_int_set_si(v, -1);
589 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
590 isl_int_set_si(v, Scatter[i]);
591 isl_constraint_set_constant(c, v);
592
593 bmap = isl_basic_map_add_constraint(bmap, c);
594 }
595
596 // Fill scattering dimensions.
597 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatDim ; ++i) {
598 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
599 isl_int_set_si(v, 1);
600 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
601 isl_int_set_si(v, 0);
602 isl_constraint_set_constant(c, v);
603
604 bmap = isl_basic_map_add_constraint(bmap, c);
605 }
606
607 isl_int_clear(v);
608 isl_dim_free(dim);
609 Scattering = isl_map_from_basic_map(bmap);
610}
611
612void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
613 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
614
615 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
616 E = AccFuncs->end(); I != E; ++I) {
617 MemAccs.push_back(new MemoryAccess(I->first, this));
618 InstructionToAccess[I->second] = MemAccs.back();
619 }
620}
621
Tobias Grosser30b8a092011-08-18 07:51:37 +0000622static isl_map *MapValueToLHS(isl_dim *Model) {
Tobias Grosser75805372011-04-29 06:27:02 +0000623 std::string MapString;
624 isl_map *Map;
625
626 MapString = "{[i0] -> [i0, o1]}";
Tobias Grosser30b8a092011-08-18 07:51:37 +0000627 Map = isl_map_read_from_str(isl_dim_get_ctx(Model), MapString.c_str(), -1);
628 return isl_map_align_params(Map, Model);
Tobias Grosser75805372011-04-29 06:27:02 +0000629}
630
Tobias Grosser30b8a092011-08-18 07:51:37 +0000631static isl_map *MapValueToRHS(isl_dim *Model) {
Tobias Grosser75805372011-04-29 06:27:02 +0000632 std::string MapString;
633 isl_map *Map;
634
635 MapString = "{[i0] -> [o0, i0]}";
Tobias Grosser30b8a092011-08-18 07:51:37 +0000636 Map = isl_map_read_from_str(isl_dim_get_ctx(Model), MapString.c_str(), -1);
637 return isl_map_align_params(Map, Model);
Tobias Grosser75805372011-04-29 06:27:02 +0000638}
639
Tobias Grosser30b8a092011-08-18 07:51:37 +0000640static isl_set *getComparison(isl_dim *Model, const ICmpInst::Predicate Pred) {
Tobias Grosser75805372011-04-29 06:27:02 +0000641 std::string SetString;
642
643 switch (Pred) {
644 case ICmpInst::ICMP_EQ:
645 SetString = "{[i0, i1] : i0 = i1}";
646 break;
647 case ICmpInst::ICMP_NE:
648 SetString = "{[i0, i1] : i0 + 1 <= i1; [i0, i1] : i0 - 1 >= i1}";
649 break;
650 case ICmpInst::ICMP_SLT:
651 SetString = "{[i0, i1] : i0 + 1 <= i1}";
652 break;
653 case ICmpInst::ICMP_ULT:
654 SetString = "{[i0, i1] : i0 + 1 <= i1}";
655 break;
656 case ICmpInst::ICMP_SGT:
657 SetString = "{[i0, i1] : i0 >= i1 + 1}";
658 break;
659 case ICmpInst::ICMP_UGT:
660 SetString = "{[i0, i1] : i0 >= i1 + 1}";
661 break;
662 case ICmpInst::ICMP_SLE:
663 SetString = "{[i0, i1] : i0 <= i1}";
664 break;
665 case ICmpInst::ICMP_ULE:
666 SetString = "{[i0, i1] : i0 <= i1}";
667 break;
668 case ICmpInst::ICMP_SGE:
669 SetString = "{[i0, i1] : i0 >= i1}";
670 break;
671 case ICmpInst::ICMP_UGE:
672 SetString = "{[i0, i1] : i0 >= i1}";
673 break;
674 default:
675 llvm_unreachable("Non integer predicate not supported");
676 }
677
Tobias Grosser30b8a092011-08-18 07:51:37 +0000678 isl_set *Set = isl_set_read_from_str(isl_dim_get_ctx(Model),
679 SetString.c_str(), -1);
680 return isl_set_align_params(Set, Model);
Tobias Grosser75805372011-04-29 06:27:02 +0000681}
682
683static isl_set *compareValues(isl_map *LeftValue, isl_map *RightValue,
684 const ICmpInst::Predicate Predicate) {
Tobias Grosser30b8a092011-08-18 07:51:37 +0000685 isl_dim *Model = isl_map_get_dim(LeftValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000686
Tobias Grosser30b8a092011-08-18 07:51:37 +0000687 isl_map *MapToLHS = MapValueToLHS(isl_dim_copy(Model));
688 isl_map *MapToRHS = MapValueToRHS(isl_dim_copy(Model));
Tobias Grosser75805372011-04-29 06:27:02 +0000689
690 isl_map *LeftValueAtLHS = isl_map_apply_range(LeftValue, MapToLHS);
691 isl_map *RightValueAtRHS = isl_map_apply_range(RightValue, MapToRHS);
692
693 isl_map *BothValues = isl_map_intersect(LeftValueAtLHS, RightValueAtRHS);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000694 isl_set *Comparison = getComparison(isl_dim_copy(Model), Predicate);
Tobias Grosser75805372011-04-29 06:27:02 +0000695
696 isl_map *ComparedValues = isl_map_intersect_range(BothValues, Comparison);
697 return isl_map_domain(ComparedValues);
698}
699
700isl_set *ScopStmt::toConditionSet(const Comparison &Comp, isl_dim *dim) const {
701 isl_map *LHSValue = getValueOf(*Comp.getLHS(), this, dim);
702 isl_map *RHSValue = getValueOf(*Comp.getRHS(), this, dim);
703
704 return compareValues(LHSValue, RHSValue, Comp.getPred());
705}
706
707isl_set *ScopStmt::toUpperLoopBound(const SCEVAffFunc &UpperBound, isl_dim *dim,
708 unsigned BoundedDimension) const {
709 // Set output dimension to bounded dimension.
Tobias Grosser30b8a092011-08-18 07:51:37 +0000710 isl_dim *RHSDim = isl_dim_alloc(Parent.getCtx(), 0, getNumIterators(), 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000711 RHSDim = isl_dim_set_tuple_name(RHSDim, isl_dim_in, getBaseName());
712 isl_constraint *c = isl_equality_alloc(isl_dim_copy(RHSDim));
713 isl_int v;
714 isl_int_init(v);
715 isl_int_set_si(v, 1);
716 isl_constraint_set_coefficient(c, isl_dim_in, BoundedDimension, v);
717 isl_int_set_si(v, -1);
718 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
719 isl_int_clear(v);
720 isl_basic_map *bmap = isl_basic_map_universe(RHSDim);
721 bmap = isl_basic_map_add_constraint(bmap, c);
722
723 isl_map *LHSValue = isl_map_from_basic_map(bmap);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000724 isl_dim *Model = isl_set_get_dim(getParent()->getContext());
725 LHSValue = isl_map_align_params(LHSValue, Model);
Tobias Grosser75805372011-04-29 06:27:02 +0000726
727 isl_map *RHSValue = getValueOf(UpperBound, this, dim);
728
729 return compareValues(LHSValue, RHSValue, ICmpInst::ICMP_SLE);
730}
731
732void ScopStmt::buildIterationDomainFromLoops(TempScop &tempScop) {
Tobias Grosser30b8a092011-08-18 07:51:37 +0000733 isl_dim *dim = isl_dim_set_alloc(Parent.getCtx(), 0,
Tobias Grosser75805372011-04-29 06:27:02 +0000734 getNumIterators());
735 dim = isl_dim_set_tuple_name(dim, isl_dim_set, getBaseName());
736
737 Domain = isl_set_universe(isl_dim_copy(dim));
Tobias Grosser30b8a092011-08-18 07:51:37 +0000738 Domain = isl_set_align_params(Domain, isl_set_get_dim(Parent.getContext()));
Tobias Grosser75805372011-04-29 06:27:02 +0000739
740 isl_int v;
741 isl_int_init(v);
742
743 for (int i = 0, e = getNumIterators(); i != e; ++i) {
744 // Lower bound: IV >= 0.
745 isl_basic_set *bset = isl_basic_set_universe(isl_dim_copy(dim));
746 isl_constraint *c = isl_inequality_alloc(isl_dim_copy(dim));
747 isl_int_set_si(v, 1);
748 isl_constraint_set_coefficient(c, isl_dim_set, i, v);
749 bset = isl_basic_set_add_constraint(bset, c);
750 Domain = isl_set_intersect(Domain, isl_set_from_basic_set(bset));
751
752 // Upper bound: IV <= NumberOfIterations.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000753 const Loop *L = getLoopForDimension(i);
Tobias Grosser75805372011-04-29 06:27:02 +0000754 const SCEVAffFunc &UpperBound = tempScop.getLoopBound(L);
755 isl_set *UpperBoundSet = toUpperLoopBound(UpperBound, isl_dim_copy(dim), i);
756 Domain = isl_set_intersect(Domain, UpperBoundSet);
757 }
758
759 isl_int_clear(v);
760}
761
762void ScopStmt::addConditionsToDomain(TempScop &tempScop,
763 const Region &CurRegion) {
764 isl_dim *dim = isl_set_get_dim(Domain);
765 const Region *TopR = tempScop.getMaxRegion().getParent(),
766 *CurR = &CurRegion;
767 const BasicBlock *CurEntry = BB;
768
769 // Build BB condition constrains, by traveling up the region tree.
770 do {
771 assert(CurR && "We exceed the top region?");
772 // Skip when multiple regions share the same entry.
773 if (CurEntry != CurR->getEntry()) {
774 if (const BBCond *Cnd = tempScop.getBBCond(CurEntry))
775 for (BBCond::const_iterator I = Cnd->begin(), E = Cnd->end();
776 I != E; ++I) {
777 isl_set *c = toConditionSet(*I, dim);
778 Domain = isl_set_intersect(Domain, c);
779 }
780 }
781 CurEntry = CurR->getEntry();
782 CurR = CurR->getParent();
783 } while (TopR != CurR);
784
785 isl_dim_free(dim);
786}
787
788void ScopStmt::buildIterationDomain(TempScop &tempScop, const Region &CurRegion)
789{
790 buildIterationDomainFromLoops(tempScop);
791 addConditionsToDomain(tempScop, CurRegion);
792}
793
794ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
795 const Region &CurRegion, BasicBlock &bb,
796 SmallVectorImpl<Loop*> &NestLoops,
797 SmallVectorImpl<unsigned> &Scatter)
798 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
799 // Setup the induction variables.
800 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
801 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
802 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000803 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000804 }
805
806 raw_string_ostream OS(BaseName);
807 WriteAsOperand(OS, &bb, false);
808 BaseName = OS.str();
809
810 // Remove the % in the name. This is not supported by isl.
811 BaseName.erase(0, 1);
812 makeIslCompatible(BaseName);
813 BaseName = "Stmt_" + BaseName;
814
815 buildIterationDomain(tempScop, CurRegion);
816 buildScattering(Scatter);
817 buildAccesses(tempScop, CurRegion);
818
819 IsReduction = tempScop.is_Reduction(*BB);
Tobias Grosser30b8a092011-08-18 07:51:37 +0000820
821 // FIXME: Temporarily remove dimension ids.
822 Scattering = map_remove_dim_ids(Scattering);
823 Domain = set_remove_dim_ids(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +0000824}
825
826ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
827 : Parent(parent), BB(NULL), IVS(0) {
828
829 BaseName = "FinalRead";
830
831 // Build iteration domain.
832 std::string IterationDomainString = "{[i0] : i0 = 0}";
833 Domain = isl_set_read_from_str(Parent.getCtx(), IterationDomainString.c_str(),
834 -1);
835 Domain = isl_set_add_dims(Domain, isl_dim_param, Parent.getNumParams());
836 Domain = isl_set_set_tuple_name(Domain, getBaseName());
837
838 // Build scattering.
839 unsigned ScatDim = Parent.getMaxLoopDepth() * 2 + 1;
840 isl_dim *dim = isl_dim_alloc(Parent.getCtx(), Parent.getNumParams(), 1,
841 ScatDim);
842 dim = isl_dim_set_tuple_name(dim, isl_dim_out, "scattering");
843 dim = isl_dim_set_tuple_name(dim, isl_dim_in, getBaseName());
844 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim));
845 isl_int v;
846 isl_int_init(v);
847
848 isl_constraint *c = isl_equality_alloc(dim);
849 isl_int_set_si(v, -1);
850 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
851
852 // TODO: This is incorrect. We should not use a very large number to ensure
853 // that this statement is executed last.
854 isl_int_set_si(v, 200000000);
855 isl_constraint_set_constant(c, v);
856
857 bmap = isl_basic_map_add_constraint(bmap, c);
858 isl_int_clear(v);
859 Scattering = isl_map_from_basic_map(bmap);
860
861 // Build memory accesses, use SetVector to keep the order of memory accesses
862 // and prevent the same memory access inserted more than once.
863 SetVector<const Value*> BaseAddressSet;
864
865 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
866 ++SI) {
867 ScopStmt *Stmt = *SI;
868
869 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
870 E = Stmt->memacc_end(); I != E; ++I)
871 BaseAddressSet.insert((*I)->getBaseAddr());
872 }
873
874 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
875 BE = BaseAddressSet.end(); BI != BE; ++BI)
876 MemAccs.push_back(new MemoryAccess(*BI, this));
877
878 IsReduction = false;
879}
880
881std::string ScopStmt::getDomainStr() const {
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000882 isl_set *domain = getDomain();
883 std::string string = stringFromIslObj(domain);
884 isl_set_free(domain);
885 return string;
Tobias Grosser75805372011-04-29 06:27:02 +0000886}
887
888std::string ScopStmt::getScatteringStr() const {
889 return stringFromIslObj(getScattering());
890}
891
892unsigned ScopStmt::getNumParams() const {
893 return Parent.getNumParams();
894}
895
896unsigned ScopStmt::getNumIterators() const {
897 // The final read has one dimension with one element.
898 if (!BB)
899 return 1;
900
901 return IVS.size();
902}
903
904unsigned ScopStmt::getNumScattering() const {
905 return isl_map_dim(Scattering, isl_dim_out);
906}
907
908const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
909
910const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
911 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000912 return IVS[Dimension].first;
913}
914
915const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
916 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000917}
918
919const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
920 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000921 PHINode *PN =
922 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000923 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
924}
925
926isl_ctx *ScopStmt::getIslContext() {
927 return Parent.getCtx();
928}
929
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000930isl_set *ScopStmt::getDomain() const {
931 return isl_set_copy(Domain);
932}
933
Tobias Grosser75805372011-04-29 06:27:02 +0000934ScopStmt::~ScopStmt() {
935 while (!MemAccs.empty()) {
936 delete MemAccs.back();
937 MemAccs.pop_back();
938 }
939
940 isl_set_free(Domain);
941 isl_map_free(Scattering);
942}
943
944void ScopStmt::print(raw_ostream &OS) const {
945 OS << "\t" << getBaseName() << "\n";
946
947 OS.indent(12) << "Domain :=\n";
948
949 if (Domain) {
950 OS.indent(16) << getDomainStr() << ";\n";
951 } else
952 OS.indent(16) << "n/a\n";
953
954 OS.indent(12) << "Scattering :=\n";
955
956 if (Domain) {
957 OS.indent(16) << getScatteringStr() << ";\n";
958 } else
959 OS.indent(16) << "n/a\n";
960
961 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
962 I != E; ++I)
963 (*I)->print(OS);
964}
965
966void ScopStmt::dump() const { print(dbgs()); }
967
968//===----------------------------------------------------------------------===//
969/// Scop class implement
970Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution)
971 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
972 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
973 isl_ctx *ctx = isl_ctx_alloc();
974
975 ParamSetType &Params = tempScop.getParamSet();
976 Parameters.insert(Parameters.begin(), Params.begin(), Params.end());
977
978 isl_dim *dim = isl_dim_set_alloc(ctx, getNumParams(), 0);
979
Tobias Grosser30b8a092011-08-18 07:51:37 +0000980 int i = 0;
981 for (ParamSetType::iterator PI = Params.begin(), PE = Params.end();
982 PI != PE; ++PI) {
983 const SCEV *scev = *PI;
984 isl_id *id = isl_id_alloc(ctx,
985 ("p" + convertInt(i)).c_str(),
986 (void *) scev);
987 dim = isl_dim_set_dim_id(dim, isl_dim_param, i, id);
988 i++;
989 }
990
Tobias Grosser75805372011-04-29 06:27:02 +0000991 // TODO: Insert relations between parameters.
992 // TODO: Insert constraints on parameters.
993 Context = isl_set_universe (dim);
994
995 SmallVector<Loop*, 8> NestLoops;
996 SmallVector<unsigned, 8> Scatter;
997
998 Scatter.assign(MaxLoopDepth + 1, 0);
999
1000 // Build the iteration domain, access functions and scattering functions
1001 // traversing the region tree.
1002 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
1003 Stmts.push_back(new ScopStmt(*this, Scatter));
1004
Tobias Grosser30b8a092011-08-18 07:51:37 +00001005 // FIXME: Temporarily remove dimension ids
1006 Context = set_remove_dim_ids(Context);
1007
Tobias Grosser75805372011-04-29 06:27:02 +00001008 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1009}
1010
1011Scop::~Scop() {
1012 isl_set_free(Context);
1013
1014 // Free the statements;
1015 for (iterator I = begin(), E = end(); I != E; ++I)
1016 delete *I;
1017
1018 // Do we need a singleton to manage this?
1019 //isl_ctx_free(ctx);
1020}
1021
1022std::string Scop::getContextStr() const {
1023 return stringFromIslObj(getContext());
1024}
1025
1026std::string Scop::getNameStr() const {
1027 std::string ExitName, EntryName;
1028 raw_string_ostream ExitStr(ExitName);
1029 raw_string_ostream EntryStr(EntryName);
1030
1031 WriteAsOperand(EntryStr, R.getEntry(), false);
1032 EntryStr.str();
1033
1034 if (R.getExit()) {
1035 WriteAsOperand(ExitStr, R.getExit(), false);
1036 ExitStr.str();
1037 } else
1038 ExitName = "FunctionExit";
1039
1040 return EntryName + "---" + ExitName;
1041}
1042
1043void Scop::printContext(raw_ostream &OS) const {
1044 OS << "Context:\n";
1045
1046 if (!Context) {
1047 OS.indent(4) << "n/a\n\n";
1048 return;
1049 }
1050
1051 OS.indent(4) << getContextStr() << "\n";
1052}
1053
1054void Scop::printStatements(raw_ostream &OS) const {
1055 OS << "Statements {\n";
1056
1057 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
1058 OS.indent(4) << (**SI);
1059
1060 OS.indent(4) << "}\n";
1061}
1062
1063
1064void Scop::print(raw_ostream &OS) const {
1065 printContext(OS.indent(4));
1066 printStatements(OS.indent(4));
1067}
1068
1069void Scop::dump() const { print(dbgs()); }
1070
1071isl_ctx *Scop::getCtx() const { return isl_set_get_ctx(Context); }
1072
1073ScalarEvolution *Scop::getSE() const { return SE; }
1074
1075bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1076 if (tempScop.getAccessFunctions(BB))
1077 return false;
1078
1079 return true;
1080}
1081
1082void Scop::buildScop(TempScop &tempScop,
1083 const Region &CurRegion,
1084 SmallVectorImpl<Loop*> &NestLoops,
1085 SmallVectorImpl<unsigned> &Scatter,
1086 LoopInfo &LI) {
1087 Loop *L = castToLoop(CurRegion, LI);
1088
1089 if (L)
1090 NestLoops.push_back(L);
1091
1092 unsigned loopDepth = NestLoops.size();
1093 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1094
1095 for (Region::const_element_iterator I = CurRegion.element_begin(),
1096 E = CurRegion.element_end(); I != E; ++I)
1097 if (I->isSubRegion())
1098 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1099 else {
1100 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1101
1102 if (isTrivialBB(BB, tempScop))
1103 continue;
1104
1105 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
1106 Scatter));
1107
1108 // Increasing the Scattering function is OK for the moment, because
1109 // we are using a depth first iterator and the program is well structured.
1110 ++Scatter[loopDepth];
1111 }
1112
1113 if (!L)
1114 return;
1115
1116 // Exiting a loop region.
1117 Scatter[loopDepth] = 0;
1118 NestLoops.pop_back();
1119 ++Scatter[loopDepth-1];
1120}
1121
1122//===----------------------------------------------------------------------===//
1123
1124void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1125 AU.addRequired<LoopInfo>();
1126 AU.addRequired<RegionInfo>();
1127 AU.addRequired<ScalarEvolution>();
1128 AU.addRequired<TempScopInfo>();
1129 AU.setPreservesAll();
1130}
1131
1132bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1133 LoopInfo &LI = getAnalysis<LoopInfo>();
1134 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1135
1136 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1137
1138 // This region is no Scop.
1139 if (!tempScop) {
1140 scop = 0;
1141 return false;
1142 }
1143
1144 // Statistics.
1145 ++ScopFound;
1146 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
1147
1148 scop = new Scop(*tempScop, LI, SE);
1149
1150 return false;
1151}
1152
1153char ScopInfo::ID = 0;
1154
1155
1156static RegisterPass<ScopInfo>
1157X("polly-scops", "Polly - Create polyhedral description of Scops");
1158
1159Pass *polly::createScopInfoPass() {
1160 return new ScopInfo();
1161}