blob: cd3df56764a46ae265bed7ad195b56a46af8abd7 [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"
41#include <sstream>
42#include <string>
43#include <vector>
44
45using namespace llvm;
46using namespace polly;
47
48STATISTIC(ScopFound, "Number of valid Scops");
49STATISTIC(RichScopFound, "Number of Scops containing a loop");
50
51
52//===----------------------------------------------------------------------===//
53static void setCoefficient(const SCEV *Coeff, mpz_t v, bool negative,
54 bool isSigned = true) {
55 if (Coeff) {
56 const SCEVConstant *C = dyn_cast<SCEVConstant>(Coeff);
57 const APInt &CI = C->getValue()->getValue();
58 MPZ_from_APInt(v, negative ? (-CI) : CI, isSigned);
59 } else
60 isl_int_set_si(v, 0);
61}
62
63static isl_map *getValueOf(const SCEVAffFunc &AffFunc,
64 const ScopStmt *Statement, isl_dim *dim) {
65
66 const SmallVectorImpl<const SCEV*> &Params =
67 Statement->getParent()->getParams();
68 unsigned num_in = Statement->getNumIterators(), num_param = Params.size();
69
70 const char *dimname = isl_dim_get_tuple_name(dim, isl_dim_set);
71 dim = isl_dim_alloc(isl_dim_get_ctx(dim), num_param,
72 isl_dim_size(dim, isl_dim_set), 1);
73 dim = isl_dim_set_tuple_name(dim, isl_dim_in, dimname);
74
75 assert((AffFunc.getType() == SCEVAffFunc::Eq
76 || AffFunc.getType() == SCEVAffFunc::ReadMem
77 || AffFunc.getType() == SCEVAffFunc::WriteMem)
78 && "AffFunc is not an equality");
79
80 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
81
82 isl_int v;
83 isl_int_init(v);
84
85 // Set single output dimension.
86 isl_int_set_si(v, -1);
87 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
88
89 // Set the coefficient for induction variables.
90 for (unsigned i = 0, e = num_in; i != e; ++i) {
91 setCoefficient(AffFunc.getCoeff(Statement->getSCEVForDimension(i)), v,
92 false, AffFunc.isSigned());
93 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
94 }
95
96 // Set the coefficient of parameters
97 for (unsigned i = 0, e = num_param; i != e; ++i) {
98 setCoefficient(AffFunc.getCoeff(Params[i]), v, false, AffFunc.isSigned());
99 isl_constraint_set_coefficient(c, isl_dim_param, i, v);
100 }
101
102 // Set the constant.
103 setCoefficient(AffFunc.getTransComp(), v, false, AffFunc.isSigned());
104 isl_constraint_set_constant(c, v);
105 isl_int_clear(v);
106
107 isl_basic_map *BasicMap = isl_basic_map_universe(isl_dim_copy(dim));
108 BasicMap = isl_basic_map_add_constraint(BasicMap, c);
109 return isl_map_from_basic_map(BasicMap);
110}
111//===----------------------------------------------------------------------===//
112
113MemoryAccess::~MemoryAccess() {
114 isl_map_free(getAccessFunction());
115}
116
117static void replace(std::string& str, const std::string& find,
118 const std::string& replace) {
119 size_t pos = 0;
120 while((pos = str.find(find, pos)) != std::string::npos)
121 {
122 str.replace(pos, find.length(), replace);
123 pos += replace.length();
124 }
125}
126
127static void makeIslCompatible(std::string& str) {
128 replace(str, ".", "_");
129}
130
131void MemoryAccess::setBaseName() {
132 raw_string_ostream OS(BaseName);
133 WriteAsOperand(OS, getBaseAddr(), false);
134 BaseName = OS.str();
135
136 // Remove the % in the name. This is not supported by isl.
137 BaseName.erase(0,1);
138 makeIslCompatible(BaseName);
139 BaseName = "MemRef_" + BaseName;
140}
141
142std::string MemoryAccess::getAccessFunctionStr() const {
143 return stringFromIslObj(getAccessFunction());
144}
145
146isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
147 isl_dim *dim = isl_dim_alloc(Statement->getIslContext(),
148 Statement->getNumParams(),
149 Statement->getNumIterators(), 1);
150 setBaseName();
151
152 dim = isl_dim_set_tuple_name(dim, isl_dim_out, getBaseName().c_str());
153 dim = isl_dim_set_tuple_name(dim, isl_dim_in, Statement->getBaseName());
154
155 return isl_basic_map_universe(dim);
156}
157
158MemoryAccess::MemoryAccess(const SCEVAffFunc &AffFunc, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000159 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000160 BaseAddr = AffFunc.getBaseAddr();
161 Type = AffFunc.isRead() ? Read : Write;
162 statement = Statement;
163
164 setBaseName();
165
166 isl_dim *dim = isl_dim_set_alloc(Statement->getIslContext(),
167 Statement->getNumParams(),
168 Statement->getNumIterators());
169 dim = isl_dim_set_tuple_name(dim, isl_dim_set, Statement->getBaseName());
170
171 AccessRelation = getValueOf(AffFunc, Statement, dim);
172
173 // Devide the access function by the size of the elements in the function.
174 isl_dim *dim2 = isl_dim_alloc(Statement->getIslContext(),
175 Statement->getNumParams(), 1, 1);
176 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim2));
177 isl_constraint *c = isl_equality_alloc(dim2);
178 isl_int v;
179 isl_int_init(v);
180 isl_int_set_si(v, -1);
181 isl_constraint_set_coefficient(c, isl_dim_in, 0, v);
182 isl_int_set_si(v, AffFunc.getElemSizeInBytes());
183 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
184
185 bmap = isl_basic_map_add_constraint(bmap, c);
186 isl_map* dataSizeMap = isl_map_from_basic_map(bmap);
187
188 AccessRelation = isl_map_apply_range(AccessRelation, dataSizeMap);
189
190 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
191 getBaseName().c_str());
192}
193
194MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement) {
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000195 newAccessRelation = NULL;
Tobias Grosser75805372011-04-29 06:27:02 +0000196 BaseAddr = BaseAddress;
197 Type = Read;
198 statement = Statement;
199
200 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
201 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
202}
203
204void MemoryAccess::print(raw_ostream &OS) const {
205 OS.indent(12) << (isRead() ? "Read" : "Write") << "Access := \n";
206 OS.indent(16) << getAccessFunctionStr() << ";\n";
207}
208
209void MemoryAccess::dump() const {
210 print(errs());
211}
212
213// Create a map in the size of the provided set domain, that maps from the
214// one element of the provided set domain to another element of the provided
215// set domain.
216// The mapping is limited to all points that are equal in all but the last
217// dimension and for which the last dimension of the input is strict smaller
218// than the last dimension of the output.
219//
220// getEqualAndLarger(set[i0, i1, ..., iX]):
221//
222// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
223// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
224//
225static isl_map *getEqualAndLarger(isl_dim *setDomain) {
226 isl_dim *mapDomain = isl_dim_map_from_set(setDomain);
227 isl_basic_map *bmap = isl_basic_map_universe(mapDomain);
228
229 // Set all but the last dimension to be equal for the input and output
230 //
231 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
232 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
233 for (unsigned i = 0; i < isl_basic_map_n_in(bmap) - 1; ++i) {
234 isl_int v;
235 isl_int_init(v);
236 isl_constraint *c = isl_equality_alloc(isl_basic_map_get_dim(bmap));
237
238 isl_int_set_si(v, 1);
239 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
240 isl_int_set_si(v, -1);
241 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
242
243 bmap = isl_basic_map_add_constraint(bmap, c);
244
245 isl_int_clear(v);
246 }
247
248 // Set the last dimension of the input to be strict smaller than the
249 // last dimension of the output.
250 //
251 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
252 //
253 unsigned lastDimension = isl_basic_map_n_in(bmap) - 1;
254 isl_int v;
255 isl_int_init(v);
256 isl_constraint *c = isl_inequality_alloc(isl_basic_map_get_dim(bmap));
257 isl_int_set_si(v, -1);
258 isl_constraint_set_coefficient(c, isl_dim_in, lastDimension, v);
259 isl_int_set_si(v, 1);
260 isl_constraint_set_coefficient(c, isl_dim_out, lastDimension, v);
261 isl_int_set_si(v, -1);
262 isl_constraint_set_constant(c, v);
263 isl_int_clear(v);
264
265 bmap = isl_basic_map_add_constraint(bmap, c);
266
267 return isl_map_from_basic_map(bmap);
268}
269
270isl_set *MemoryAccess::getStride(const isl_set *domainSubset) const {
271 isl_map *accessRelation = isl_map_copy(getAccessFunction());
272 isl_set *scatteringDomain = isl_set_copy(const_cast<isl_set*>(domainSubset));
273 isl_map *scattering = isl_map_copy(getStatement()->getScattering());
274
275 scattering = isl_map_reverse(scattering);
276 int difference = isl_map_n_in(scattering) - isl_set_n_dim(scatteringDomain);
277 scattering = isl_map_project_out(scattering, isl_dim_in,
278 isl_set_n_dim(scatteringDomain),
279 difference);
280
281 // Remove all names of the scattering dimensions, as the names may be lost
282 // anyways during the project. This leads to consistent results.
283 scattering = isl_map_set_tuple_name(scattering, isl_dim_in, "");
284 scatteringDomain = isl_set_set_tuple_name(scatteringDomain, "");
285
286 isl_map *nextScatt = getEqualAndLarger(isl_set_get_dim(scatteringDomain));
287 nextScatt = isl_map_lexmin(nextScatt);
288
289 scattering = isl_map_intersect_domain(scattering, scatteringDomain);
290
291 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(scattering));
292 nextScatt = isl_map_apply_range(nextScatt, isl_map_copy(accessRelation));
293 nextScatt = isl_map_apply_domain(nextScatt, scattering);
294 nextScatt = isl_map_apply_domain(nextScatt, accessRelation);
295
296 return isl_map_deltas(nextScatt);
297}
298
299bool MemoryAccess::isStrideZero(const isl_set *domainSubset) const {
300 isl_set *stride = getStride(domainSubset);
301 isl_constraint *c = isl_equality_alloc(isl_set_get_dim(stride));
302
303 isl_int v;
304 isl_int_init(v);
305 isl_int_set_si(v, 1);
306 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
307 isl_int_set_si(v, 0);
308 isl_constraint_set_constant(c, v);
309 isl_int_clear(v);
310
311 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_dim(stride));
312
313 bset = isl_basic_set_add_constraint(bset, c);
314 isl_set *strideZero = isl_set_from_basic_set(bset);
315
316 return isl_set_is_equal(stride, strideZero);
317}
318
319bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
320 isl_set *stride = getStride(domainSubset);
321 isl_constraint *c = isl_equality_alloc(isl_set_get_dim(stride));
322
323 isl_int v;
324 isl_int_init(v);
325 isl_int_set_si(v, 1);
326 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
327 isl_int_set_si(v, -1);
328 isl_constraint_set_constant(c, v);
329 isl_int_clear(v);
330
331 isl_basic_set *bset = isl_basic_set_universe(isl_set_get_dim(stride));
332
333 bset = isl_basic_set_add_constraint(bset, c);
334 isl_set *strideZero = isl_set_from_basic_set(bset);
335
336 return isl_set_is_equal(stride, strideZero);
337}
338
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000339void MemoryAccess::setNewAccessFunction(isl_map *newAccessRelation) {
340 newAccessRelation = newAccessRelation;
341}
Tobias Grosser75805372011-04-29 06:27:02 +0000342
343//===----------------------------------------------------------------------===//
344void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
345 unsigned NumberOfIterators = getNumIterators();
346 unsigned ScatDim = Parent.getMaxLoopDepth() * 2 + 1;
347 isl_dim *dim = isl_dim_alloc(Parent.getCtx(), Parent.getNumParams(),
348 NumberOfIterators, ScatDim);
349 dim = isl_dim_set_tuple_name(dim, isl_dim_out, "scattering");
350 dim = isl_dim_set_tuple_name(dim, isl_dim_in, getBaseName());
351 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim));
352 isl_int v;
353 isl_int_init(v);
354
355 // Loop dimensions.
356 for (unsigned i = 0; i < NumberOfIterators; ++i) {
357 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
358 isl_int_set_si(v, 1);
359 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i + 1, v);
360 isl_int_set_si(v, -1);
361 isl_constraint_set_coefficient(c, isl_dim_in, i, v);
362
363 bmap = isl_basic_map_add_constraint(bmap, c);
364 }
365
366 // Constant dimensions
367 for (unsigned i = 0; i < NumberOfIterators + 1; ++i) {
368 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
369 isl_int_set_si(v, -1);
370 isl_constraint_set_coefficient(c, isl_dim_out, 2 * i, v);
371 isl_int_set_si(v, Scatter[i]);
372 isl_constraint_set_constant(c, v);
373
374 bmap = isl_basic_map_add_constraint(bmap, c);
375 }
376
377 // Fill scattering dimensions.
378 for (unsigned i = 2 * NumberOfIterators + 1; i < ScatDim ; ++i) {
379 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
380 isl_int_set_si(v, 1);
381 isl_constraint_set_coefficient(c, isl_dim_out, i, v);
382 isl_int_set_si(v, 0);
383 isl_constraint_set_constant(c, v);
384
385 bmap = isl_basic_map_add_constraint(bmap, c);
386 }
387
388 isl_int_clear(v);
389 isl_dim_free(dim);
390 Scattering = isl_map_from_basic_map(bmap);
391}
392
393void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
394 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
395
396 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
397 E = AccFuncs->end(); I != E; ++I) {
398 MemAccs.push_back(new MemoryAccess(I->first, this));
399 InstructionToAccess[I->second] = MemAccs.back();
400 }
401}
402
403static isl_map *MapValueToLHS(isl_ctx *Context, unsigned ParameterNumber) {
404 std::string MapString;
405 isl_map *Map;
406
407 MapString = "{[i0] -> [i0, o1]}";
408 Map = isl_map_read_from_str(Context, MapString.c_str(), -1);
409 return isl_map_add_dims(Map, isl_dim_param, ParameterNumber);
410}
411
412static isl_map *MapValueToRHS(isl_ctx *Context, unsigned ParameterNumber) {
413 std::string MapString;
414 isl_map *Map;
415
416 MapString = "{[i0] -> [o0, i0]}";
417 Map = isl_map_read_from_str(Context, MapString.c_str(), -1);
418 return isl_map_add_dims(Map, isl_dim_param, ParameterNumber);
419}
420
421static isl_set *getComparison(isl_ctx *Context, const ICmpInst::Predicate Pred,
422 unsigned ParameterNumber) {
423 std::string SetString;
424
425 switch (Pred) {
426 case ICmpInst::ICMP_EQ:
427 SetString = "{[i0, i1] : i0 = i1}";
428 break;
429 case ICmpInst::ICMP_NE:
430 SetString = "{[i0, i1] : i0 + 1 <= i1; [i0, i1] : i0 - 1 >= i1}";
431 break;
432 case ICmpInst::ICMP_SLT:
433 SetString = "{[i0, i1] : i0 + 1 <= i1}";
434 break;
435 case ICmpInst::ICMP_ULT:
436 SetString = "{[i0, i1] : i0 + 1 <= i1}";
437 break;
438 case ICmpInst::ICMP_SGT:
439 SetString = "{[i0, i1] : i0 >= i1 + 1}";
440 break;
441 case ICmpInst::ICMP_UGT:
442 SetString = "{[i0, i1] : i0 >= i1 + 1}";
443 break;
444 case ICmpInst::ICMP_SLE:
445 SetString = "{[i0, i1] : i0 <= i1}";
446 break;
447 case ICmpInst::ICMP_ULE:
448 SetString = "{[i0, i1] : i0 <= i1}";
449 break;
450 case ICmpInst::ICMP_SGE:
451 SetString = "{[i0, i1] : i0 >= i1}";
452 break;
453 case ICmpInst::ICMP_UGE:
454 SetString = "{[i0, i1] : i0 >= i1}";
455 break;
456 default:
457 llvm_unreachable("Non integer predicate not supported");
458 }
459
460 isl_set *Set = isl_set_read_from_str(Context, SetString.c_str(), -1);
461 return isl_set_add_dims(Set, isl_dim_param, ParameterNumber);
462}
463
464static isl_set *compareValues(isl_map *LeftValue, isl_map *RightValue,
465 const ICmpInst::Predicate Predicate) {
466 isl_ctx *Context = isl_map_get_ctx(LeftValue);
467 unsigned NumberOfParameters = isl_map_n_param(LeftValue);
468
469 isl_map *MapToLHS = MapValueToLHS(Context, NumberOfParameters);
470 isl_map *MapToRHS = MapValueToRHS(Context, NumberOfParameters);
471
472 isl_map *LeftValueAtLHS = isl_map_apply_range(LeftValue, MapToLHS);
473 isl_map *RightValueAtRHS = isl_map_apply_range(RightValue, MapToRHS);
474
475 isl_map *BothValues = isl_map_intersect(LeftValueAtLHS, RightValueAtRHS);
476 isl_set *Comparison = getComparison(Context, Predicate, NumberOfParameters);
477
478 isl_map *ComparedValues = isl_map_intersect_range(BothValues, Comparison);
479 return isl_map_domain(ComparedValues);
480}
481
482isl_set *ScopStmt::toConditionSet(const Comparison &Comp, isl_dim *dim) const {
483 isl_map *LHSValue = getValueOf(*Comp.getLHS(), this, dim);
484 isl_map *RHSValue = getValueOf(*Comp.getRHS(), this, dim);
485
486 return compareValues(LHSValue, RHSValue, Comp.getPred());
487}
488
489isl_set *ScopStmt::toUpperLoopBound(const SCEVAffFunc &UpperBound, isl_dim *dim,
490 unsigned BoundedDimension) const {
491 // Set output dimension to bounded dimension.
492 isl_dim *RHSDim = isl_dim_alloc(Parent.getCtx(), getNumParams(),
493 getNumIterators(), 1);
494 RHSDim = isl_dim_set_tuple_name(RHSDim, isl_dim_in, getBaseName());
495 isl_constraint *c = isl_equality_alloc(isl_dim_copy(RHSDim));
496 isl_int v;
497 isl_int_init(v);
498 isl_int_set_si(v, 1);
499 isl_constraint_set_coefficient(c, isl_dim_in, BoundedDimension, v);
500 isl_int_set_si(v, -1);
501 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
502 isl_int_clear(v);
503 isl_basic_map *bmap = isl_basic_map_universe(RHSDim);
504 bmap = isl_basic_map_add_constraint(bmap, c);
505
506 isl_map *LHSValue = isl_map_from_basic_map(bmap);
507
508 isl_map *RHSValue = getValueOf(UpperBound, this, dim);
509
510 return compareValues(LHSValue, RHSValue, ICmpInst::ICMP_SLE);
511}
512
513void ScopStmt::buildIterationDomainFromLoops(TempScop &tempScop) {
514 isl_dim *dim = isl_dim_set_alloc(Parent.getCtx(), getNumParams(),
515 getNumIterators());
516 dim = isl_dim_set_tuple_name(dim, isl_dim_set, getBaseName());
517
518 Domain = isl_set_universe(isl_dim_copy(dim));
519
520 isl_int v;
521 isl_int_init(v);
522
523 for (int i = 0, e = getNumIterators(); i != e; ++i) {
524 // Lower bound: IV >= 0.
525 isl_basic_set *bset = isl_basic_set_universe(isl_dim_copy(dim));
526 isl_constraint *c = isl_inequality_alloc(isl_dim_copy(dim));
527 isl_int_set_si(v, 1);
528 isl_constraint_set_coefficient(c, isl_dim_set, i, v);
529 bset = isl_basic_set_add_constraint(bset, c);
530 Domain = isl_set_intersect(Domain, isl_set_from_basic_set(bset));
531
532 // Upper bound: IV <= NumberOfIterations.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000533 const Loop *L = getLoopForDimension(i);
Tobias Grosser75805372011-04-29 06:27:02 +0000534 const SCEVAffFunc &UpperBound = tempScop.getLoopBound(L);
535 isl_set *UpperBoundSet = toUpperLoopBound(UpperBound, isl_dim_copy(dim), i);
536 Domain = isl_set_intersect(Domain, UpperBoundSet);
537 }
538
539 isl_int_clear(v);
540}
541
542void ScopStmt::addConditionsToDomain(TempScop &tempScop,
543 const Region &CurRegion) {
544 isl_dim *dim = isl_set_get_dim(Domain);
545 const Region *TopR = tempScop.getMaxRegion().getParent(),
546 *CurR = &CurRegion;
547 const BasicBlock *CurEntry = BB;
548
549 // Build BB condition constrains, by traveling up the region tree.
550 do {
551 assert(CurR && "We exceed the top region?");
552 // Skip when multiple regions share the same entry.
553 if (CurEntry != CurR->getEntry()) {
554 if (const BBCond *Cnd = tempScop.getBBCond(CurEntry))
555 for (BBCond::const_iterator I = Cnd->begin(), E = Cnd->end();
556 I != E; ++I) {
557 isl_set *c = toConditionSet(*I, dim);
558 Domain = isl_set_intersect(Domain, c);
559 }
560 }
561 CurEntry = CurR->getEntry();
562 CurR = CurR->getParent();
563 } while (TopR != CurR);
564
565 isl_dim_free(dim);
566}
567
568void ScopStmt::buildIterationDomain(TempScop &tempScop, const Region &CurRegion)
569{
570 buildIterationDomainFromLoops(tempScop);
571 addConditionsToDomain(tempScop, CurRegion);
572}
573
574ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop,
575 const Region &CurRegion, BasicBlock &bb,
576 SmallVectorImpl<Loop*> &NestLoops,
577 SmallVectorImpl<unsigned> &Scatter)
578 : Parent(parent), BB(&bb), IVS(NestLoops.size()) {
579 // Setup the induction variables.
580 for (unsigned i = 0, e = NestLoops.size(); i < e; ++i) {
581 PHINode *PN = NestLoops[i]->getCanonicalInductionVariable();
582 assert(PN && "Non canonical IV in Scop!");
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000583 IVS[i] = std::make_pair(PN, NestLoops[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000584 }
585
586 raw_string_ostream OS(BaseName);
587 WriteAsOperand(OS, &bb, false);
588 BaseName = OS.str();
589
590 // Remove the % in the name. This is not supported by isl.
591 BaseName.erase(0, 1);
592 makeIslCompatible(BaseName);
593 BaseName = "Stmt_" + BaseName;
594
595 buildIterationDomain(tempScop, CurRegion);
596 buildScattering(Scatter);
597 buildAccesses(tempScop, CurRegion);
598
599 IsReduction = tempScop.is_Reduction(*BB);
600}
601
602ScopStmt::ScopStmt(Scop &parent, SmallVectorImpl<unsigned> &Scatter)
603 : Parent(parent), BB(NULL), IVS(0) {
604
605 BaseName = "FinalRead";
606
607 // Build iteration domain.
608 std::string IterationDomainString = "{[i0] : i0 = 0}";
609 Domain = isl_set_read_from_str(Parent.getCtx(), IterationDomainString.c_str(),
610 -1);
611 Domain = isl_set_add_dims(Domain, isl_dim_param, Parent.getNumParams());
612 Domain = isl_set_set_tuple_name(Domain, getBaseName());
613
614 // Build scattering.
615 unsigned ScatDim = Parent.getMaxLoopDepth() * 2 + 1;
616 isl_dim *dim = isl_dim_alloc(Parent.getCtx(), Parent.getNumParams(), 1,
617 ScatDim);
618 dim = isl_dim_set_tuple_name(dim, isl_dim_out, "scattering");
619 dim = isl_dim_set_tuple_name(dim, isl_dim_in, getBaseName());
620 isl_basic_map *bmap = isl_basic_map_universe(isl_dim_copy(dim));
621 isl_int v;
622 isl_int_init(v);
623
624 isl_constraint *c = isl_equality_alloc(dim);
625 isl_int_set_si(v, -1);
626 isl_constraint_set_coefficient(c, isl_dim_out, 0, v);
627
628 // TODO: This is incorrect. We should not use a very large number to ensure
629 // that this statement is executed last.
630 isl_int_set_si(v, 200000000);
631 isl_constraint_set_constant(c, v);
632
633 bmap = isl_basic_map_add_constraint(bmap, c);
634 isl_int_clear(v);
635 Scattering = isl_map_from_basic_map(bmap);
636
637 // Build memory accesses, use SetVector to keep the order of memory accesses
638 // and prevent the same memory access inserted more than once.
639 SetVector<const Value*> BaseAddressSet;
640
641 for (Scop::const_iterator SI = Parent.begin(), SE = Parent.end(); SI != SE;
642 ++SI) {
643 ScopStmt *Stmt = *SI;
644
645 for (MemoryAccessVec::const_iterator I = Stmt->memacc_begin(),
646 E = Stmt->memacc_end(); I != E; ++I)
647 BaseAddressSet.insert((*I)->getBaseAddr());
648 }
649
650 for (SetVector<const Value*>::iterator BI = BaseAddressSet.begin(),
651 BE = BaseAddressSet.end(); BI != BE; ++BI)
652 MemAccs.push_back(new MemoryAccess(*BI, this));
653
654 IsReduction = false;
655}
656
657std::string ScopStmt::getDomainStr() const {
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000658 isl_set *domain = getDomain();
659 std::string string = stringFromIslObj(domain);
660 isl_set_free(domain);
661 return string;
Tobias Grosser75805372011-04-29 06:27:02 +0000662}
663
664std::string ScopStmt::getScatteringStr() const {
665 return stringFromIslObj(getScattering());
666}
667
668unsigned ScopStmt::getNumParams() const {
669 return Parent.getNumParams();
670}
671
672unsigned ScopStmt::getNumIterators() const {
673 // The final read has one dimension with one element.
674 if (!BB)
675 return 1;
676
677 return IVS.size();
678}
679
680unsigned ScopStmt::getNumScattering() const {
681 return isl_map_dim(Scattering, isl_dim_out);
682}
683
684const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
685
686const PHINode *ScopStmt::getInductionVariableForDimension(unsigned Dimension)
687 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000688 return IVS[Dimension].first;
689}
690
691const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
692 return IVS[Dimension].second;
Tobias Grosser75805372011-04-29 06:27:02 +0000693}
694
695const SCEVAddRecExpr *ScopStmt::getSCEVForDimension(unsigned Dimension)
696 const {
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000697 PHINode *PN =
698 const_cast<PHINode*>(getInductionVariableForDimension(Dimension));
Tobias Grosser75805372011-04-29 06:27:02 +0000699 return cast<SCEVAddRecExpr>(getParent()->getSE()->getSCEV(PN));
700}
701
702isl_ctx *ScopStmt::getIslContext() {
703 return Parent.getCtx();
704}
705
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000706isl_set *ScopStmt::getDomain() const {
707 return isl_set_copy(Domain);
708}
709
Tobias Grosser75805372011-04-29 06:27:02 +0000710ScopStmt::~ScopStmt() {
711 while (!MemAccs.empty()) {
712 delete MemAccs.back();
713 MemAccs.pop_back();
714 }
715
716 isl_set_free(Domain);
717 isl_map_free(Scattering);
718}
719
720void ScopStmt::print(raw_ostream &OS) const {
721 OS << "\t" << getBaseName() << "\n";
722
723 OS.indent(12) << "Domain :=\n";
724
725 if (Domain) {
726 OS.indent(16) << getDomainStr() << ";\n";
727 } else
728 OS.indent(16) << "n/a\n";
729
730 OS.indent(12) << "Scattering :=\n";
731
732 if (Domain) {
733 OS.indent(16) << getScatteringStr() << ";\n";
734 } else
735 OS.indent(16) << "n/a\n";
736
737 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
738 I != E; ++I)
739 (*I)->print(OS);
740}
741
742void ScopStmt::dump() const { print(dbgs()); }
743
744//===----------------------------------------------------------------------===//
745/// Scop class implement
746Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution)
747 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
748 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
749 isl_ctx *ctx = isl_ctx_alloc();
750
751 ParamSetType &Params = tempScop.getParamSet();
752 Parameters.insert(Parameters.begin(), Params.begin(), Params.end());
753
754 isl_dim *dim = isl_dim_set_alloc(ctx, getNumParams(), 0);
755
756 // TODO: Insert relations between parameters.
757 // TODO: Insert constraints on parameters.
758 Context = isl_set_universe (dim);
759
760 SmallVector<Loop*, 8> NestLoops;
761 SmallVector<unsigned, 8> Scatter;
762
763 Scatter.assign(MaxLoopDepth + 1, 0);
764
765 // Build the iteration domain, access functions and scattering functions
766 // traversing the region tree.
767 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
768 Stmts.push_back(new ScopStmt(*this, Scatter));
769
770 assert(NestLoops.empty() && "NestLoops not empty at top level!");
771}
772
773Scop::~Scop() {
774 isl_set_free(Context);
775
776 // Free the statements;
777 for (iterator I = begin(), E = end(); I != E; ++I)
778 delete *I;
779
780 // Do we need a singleton to manage this?
781 //isl_ctx_free(ctx);
782}
783
784std::string Scop::getContextStr() const {
785 return stringFromIslObj(getContext());
786}
787
788std::string Scop::getNameStr() const {
789 std::string ExitName, EntryName;
790 raw_string_ostream ExitStr(ExitName);
791 raw_string_ostream EntryStr(EntryName);
792
793 WriteAsOperand(EntryStr, R.getEntry(), false);
794 EntryStr.str();
795
796 if (R.getExit()) {
797 WriteAsOperand(ExitStr, R.getExit(), false);
798 ExitStr.str();
799 } else
800 ExitName = "FunctionExit";
801
802 return EntryName + "---" + ExitName;
803}
804
805void Scop::printContext(raw_ostream &OS) const {
806 OS << "Context:\n";
807
808 if (!Context) {
809 OS.indent(4) << "n/a\n\n";
810 return;
811 }
812
813 OS.indent(4) << getContextStr() << "\n";
814}
815
816void Scop::printStatements(raw_ostream &OS) const {
817 OS << "Statements {\n";
818
819 for (const_iterator SI = begin(), SE = end();SI != SE; ++SI)
820 OS.indent(4) << (**SI);
821
822 OS.indent(4) << "}\n";
823}
824
825
826void Scop::print(raw_ostream &OS) const {
827 printContext(OS.indent(4));
828 printStatements(OS.indent(4));
829}
830
831void Scop::dump() const { print(dbgs()); }
832
833isl_ctx *Scop::getCtx() const { return isl_set_get_ctx(Context); }
834
835ScalarEvolution *Scop::getSE() const { return SE; }
836
837bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
838 if (tempScop.getAccessFunctions(BB))
839 return false;
840
841 return true;
842}
843
844void Scop::buildScop(TempScop &tempScop,
845 const Region &CurRegion,
846 SmallVectorImpl<Loop*> &NestLoops,
847 SmallVectorImpl<unsigned> &Scatter,
848 LoopInfo &LI) {
849 Loop *L = castToLoop(CurRegion, LI);
850
851 if (L)
852 NestLoops.push_back(L);
853
854 unsigned loopDepth = NestLoops.size();
855 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
856
857 for (Region::const_element_iterator I = CurRegion.element_begin(),
858 E = CurRegion.element_end(); I != E; ++I)
859 if (I->isSubRegion())
860 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
861 else {
862 BasicBlock *BB = I->getNodeAs<BasicBlock>();
863
864 if (isTrivialBB(BB, tempScop))
865 continue;
866
867 Stmts.push_back(new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops,
868 Scatter));
869
870 // Increasing the Scattering function is OK for the moment, because
871 // we are using a depth first iterator and the program is well structured.
872 ++Scatter[loopDepth];
873 }
874
875 if (!L)
876 return;
877
878 // Exiting a loop region.
879 Scatter[loopDepth] = 0;
880 NestLoops.pop_back();
881 ++Scatter[loopDepth-1];
882}
883
884//===----------------------------------------------------------------------===//
885
886void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
887 AU.addRequired<LoopInfo>();
888 AU.addRequired<RegionInfo>();
889 AU.addRequired<ScalarEvolution>();
890 AU.addRequired<TempScopInfo>();
891 AU.setPreservesAll();
892}
893
894bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
895 LoopInfo &LI = getAnalysis<LoopInfo>();
896 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
897
898 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
899
900 // This region is no Scop.
901 if (!tempScop) {
902 scop = 0;
903 return false;
904 }
905
906 // Statistics.
907 ++ScopFound;
908 if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
909
910 scop = new Scop(*tempScop, LI, SE);
911
912 return false;
913}
914
915char ScopInfo::ID = 0;
916
917
918static RegisterPass<ScopInfo>
919X("polly-scops", "Polly - Create polyhedral description of Scops");
920
921Pass *polly::createScopInfoPass() {
922 return new ScopInfo();
923}