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