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