blob: d2c8fbf876db0bf72de769d9936c3699b064d84f [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
Sebastian Pop27c10c62013-03-22 22:07:43 +000020#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser75805372011-04-29 06:27:02 +000021#include "polly/LinkAllPasses.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000022#include "polly/ScopInfo.h"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000023#include "polly/Options.h"
Tobias Grosser75805372011-04-29 06:27:02 +000024#include "polly/Support/GICHelper.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000025#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000026#include "polly/Support/ScopHelper.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000027#include "polly/TempScopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000028#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000029#include "llvm/ADT/Statistic.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000030#include "llvm/ADT/StringExtras.h"
Tobias Grosser83628182013-05-07 08:11:54 +000031#include "llvm/Analysis/LoopInfo.h"
32#include "llvm/Analysis/RegionIterator.h"
33#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000034#include "llvm/Support/Debug.h"
35
36#include "isl/constraint.h"
37#include "isl/set.h"
38#include "isl/map.h"
Tobias Grosser37eb4222014-02-20 21:43:54 +000039#include "isl/union_map.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000040#include "isl/aff.h"
41#include "isl/printer.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000042#include "isl/local_space.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000043#include "isl/options.h"
Tobias Grosseredab1352013-06-21 06:41:31 +000044#include "isl/val.h"
Chandler Carruth95fef942014-04-22 03:30:19 +000045
Tobias Grosser75805372011-04-29 06:27:02 +000046#include <sstream>
47#include <string>
48#include <vector>
49
50using namespace llvm;
51using namespace polly;
52
Chandler Carruth95fef942014-04-22 03:30:19 +000053#define DEBUG_TYPE "polly-scops"
54
Tobias Grosser74394f02013-01-14 22:40:23 +000055STATISTIC(ScopFound, "Number of valid Scops");
56STATISTIC(RichScopFound, "Number of Scops containing a loop");
Tobias Grosser75805372011-04-29 06:27:02 +000057
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000058// Multiplicative reductions can be disabled seperately as these kind of
59// operations can overflow easily. Additive reductions and bit operations
60// are in contrast pretty stable.
61static cl::opt<bool>
62DisableMultiplicativeReductions("polly-disable-multiplicative-reductions",
63 cl::desc("Disable multiplicative reductions"),
64 cl::Hidden, cl::ZeroOrMore, cl::init(false),
65 cl::cat(PollyCategory));
66
Tobias Grosser0695ee42013-09-17 03:30:31 +000067/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000068struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000069public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000070 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
71 ///
72 /// @param Stmt The location at which the scalar evolution expression
73 /// is evaluated.
74 /// @param Expr The expression that is translated.
75 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
76
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000077private:
Tobias Grosser3cc99742012-06-06 16:33:15 +000078 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000079 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +000080 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000081
Tobias Grosser0695ee42013-09-17 03:30:31 +000082 SCEVAffinator(const ScopStmt *Stmt);
83 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +000084
Tobias Grosser0695ee42013-09-17 03:30:31 +000085 __isl_give isl_pw_aff *visit(const SCEV *Expr);
86 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
87 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
88 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
89 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
90 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
91 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
92 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
93 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
94 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
95 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
96 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Tobias Grosser60b54f12011-11-08 15:41:28 +000097
Tobias Grosser0695ee42013-09-17 03:30:31 +000098 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
99};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000100
Tobias Grosser0695ee42013-09-17 03:30:31 +0000101SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
102 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
103 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000104
Tobias Grosser0695ee42013-09-17 03:30:31 +0000105__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
106 const SCEV *Scev) {
107 Scop *S = Stmt->getParent();
108 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000109
Tobias Grosser0695ee42013-09-17 03:30:31 +0000110 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000111
Tobias Grosser0695ee42013-09-17 03:30:31 +0000112 SCEVAffinator Affinator(Stmt);
113 return Affinator.visit(Scev);
114}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000115
Tobias Grosser0695ee42013-09-17 03:30:31 +0000116__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
117 // In case the scev is a valid parameter, we do not further analyze this
118 // expression, but create a new parameter in the isl_pw_aff. This allows us
119 // to treat subexpressions that we cannot translate into an piecewise affine
120 // expression, as constant parameters of the piecewise affine expression.
121 if (isl_id *Id = S->getIdForParam(Expr)) {
122 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
123 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000124
Tobias Grosser0695ee42013-09-17 03:30:31 +0000125 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
126 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
127 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000128
129 return isl_pw_aff_alloc(Domain, Affine);
130 }
131
Tobias Grosser0695ee42013-09-17 03:30:31 +0000132 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
133}
134
Tobias Grosser0d170132013-10-03 13:09:19 +0000135__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000136 ConstantInt *Value = Expr->getValue();
137 isl_val *v;
138
139 // LLVM does not define if an integer value is interpreted as a signed or
140 // unsigned value. Hence, without further information, it is unknown how
141 // this value needs to be converted to GMP. At the moment, we only support
142 // signed operations. So we just interpret it as signed. Later, there are
143 // two options:
144 //
145 // 1. We always interpret any value as signed and convert the values on
146 // demand.
147 // 2. We pass down the signedness of the calculation and use it to interpret
148 // this constant correctly.
149 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
150
151 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
152 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(Space));
153 isl_aff *Affine = isl_aff_zero_on_domain(ls);
154 isl_set *Domain = isl_set_universe(Space);
155
156 Affine = isl_aff_add_constant_val(Affine, v);
157
158 return isl_pw_aff_alloc(Domain, Affine);
159}
160
161__isl_give isl_pw_aff *
162SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
163 llvm_unreachable("SCEVTruncateExpr not yet supported");
164}
165
166__isl_give isl_pw_aff *
167SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
168 llvm_unreachable("SCEVZeroExtendExpr not yet supported");
169}
170
171__isl_give isl_pw_aff *
172SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
173 // Assuming the value is signed, a sign extension is basically a noop.
174 // TODO: Reconsider this as soon as we support unsigned values.
175 return visit(Expr->getOperand());
176}
177
178__isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
179 isl_pw_aff *Sum = visit(Expr->getOperand(0));
180
181 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
182 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
183 Sum = isl_pw_aff_add(Sum, NextSummand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000184 }
185
Tobias Grosser0695ee42013-09-17 03:30:31 +0000186 // TODO: Check for NSW and NUW.
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000187
Tobias Grosser0695ee42013-09-17 03:30:31 +0000188 return Sum;
189}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000190
Tobias Grosser0695ee42013-09-17 03:30:31 +0000191__isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
192 isl_pw_aff *Product = visit(Expr->getOperand(0));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000193
Tobias Grosser0695ee42013-09-17 03:30:31 +0000194 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
195 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
196
197 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
198 isl_pw_aff_free(Product);
199 isl_pw_aff_free(NextOperand);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000200 return nullptr;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000201 }
202
Tobias Grosser0695ee42013-09-17 03:30:31 +0000203 Product = isl_pw_aff_mul(Product, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000204 }
205
Tobias Grosser0695ee42013-09-17 03:30:31 +0000206 // TODO: Check for NSW and NUW.
207 return Product;
208}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000209
Tobias Grosser0695ee42013-09-17 03:30:31 +0000210__isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
211 llvm_unreachable("SCEVUDivExpr not yet supported");
212}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000213
Tobias Grosser0695ee42013-09-17 03:30:31 +0000214__isl_give isl_pw_aff *
215SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
216 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000217
Tobias Grosser0695ee42013-09-17 03:30:31 +0000218 // Directly generate isl_pw_aff for Expr if 'start' is zero.
219 if (Expr->getStart()->isZero()) {
220 assert(S->getRegion().contains(Expr->getLoop()) &&
221 "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000222
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000223 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser0695ee42013-09-17 03:30:31 +0000224 isl_pw_aff *Step = visit(Expr->getOperand(1));
225 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
226 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000227
Tobias Grosser0695ee42013-09-17 03:30:31 +0000228 int loopDimension = getLoopDepth(Expr->getLoop());
229
230 isl_aff *LAff = isl_aff_set_coefficient_si(
231 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
232 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
233
234 // TODO: Do we need to check for NSW and NUW?
235 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000236 }
237
Tobias Grosser0695ee42013-09-17 03:30:31 +0000238 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
239 // if 'start' is not zero.
240 ScalarEvolution &SE = *S->getSE();
241 const SCEV *ZeroStartExpr = SE.getAddRecExpr(
242 SE.getConstant(Expr->getStart()->getType(), 0),
243 Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000244
Tobias Grosser0695ee42013-09-17 03:30:31 +0000245 isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
246 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000247
Tobias Grosser0695ee42013-09-17 03:30:31 +0000248 return isl_pw_aff_add(ZeroStartResult, Start);
249}
250
251__isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
252 isl_pw_aff *Max = visit(Expr->getOperand(0));
253
254 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
255 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
256 Max = isl_pw_aff_max(Max, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000257 }
258
Tobias Grosser0695ee42013-09-17 03:30:31 +0000259 return Max;
260}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000261
Tobias Grosser0695ee42013-09-17 03:30:31 +0000262__isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
263 llvm_unreachable("SCEVUMaxExpr not yet supported");
264}
265
266__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
267 llvm_unreachable("Unknowns are always parameters");
268}
269
270int SCEVAffinator::getLoopDepth(const Loop *L) {
271 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
272 assert(outerLoop && "Scop does not contain this loop");
273 return L->getLoopDepth() - outerLoop->getLoopDepth();
274}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000275
Tobias Grosser75805372011-04-29 06:27:02 +0000276//===----------------------------------------------------------------------===//
277
278MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000279 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000280 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000281}
282
Tobias Grossere602a072013-05-07 07:30:56 +0000283static void replace(std::string &str, const std::string &find,
284 const std::string &replace) {
Tobias Grosser75805372011-04-29 06:27:02 +0000285 size_t pos = 0;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000286 while ((pos = str.find(find, pos)) != std::string::npos) {
Tobias Grosser75805372011-04-29 06:27:02 +0000287 str.replace(pos, find.length(), replace);
288 pos += replace.length();
289 }
290}
291
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000292static void makeIslCompatible(std::string &str) {
Tobias Grossereec4d56e2011-08-20 11:11:14 +0000293 str.erase(0, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000294 replace(str, ".", "_");
Tobias Grosser3b660f82011-08-03 00:12:11 +0000295 replace(str, "\"", "_");
Tobias Grosser75805372011-04-29 06:27:02 +0000296}
297
298void MemoryAccess::setBaseName() {
299 raw_string_ostream OS(BaseName);
Tobias Grosserf240b482014-01-09 10:42:15 +0000300 getBaseAddr()->printAsOperand(OS, false);
Tobias Grosser75805372011-04-29 06:27:02 +0000301 BaseName = OS.str();
302
Tobias Grosser75805372011-04-29 06:27:02 +0000303 makeIslCompatible(BaseName);
304 BaseName = "MemRef_" + BaseName;
305}
306
Tobias Grosser5d453812011-10-06 00:04:11 +0000307isl_map *MemoryAccess::getAccessRelation() const {
308 return isl_map_copy(AccessRelation);
309}
310
311std::string MemoryAccess::getAccessRelationStr() const {
312 return stringFromIslObj(AccessRelation);
313}
314
315isl_map *MemoryAccess::getNewAccessRelation() const {
316 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000317}
318
319isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000320 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
321 Space = isl_space_set_tuple_name(Space, isl_dim_set, getBaseName().c_str());
Tobias Grossered295662012-09-11 13:50:21 +0000322 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000323
Tobias Grosser084d8f72012-05-29 09:29:44 +0000324 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000325 isl_basic_set_universe(Statement->getDomainSpace()),
326 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000327}
328
Hongbin Zhengcea35f62012-07-06 06:47:03 +0000329MemoryAccess::MemoryAccess(const IRAccess &Access, const Instruction *AccInst,
Tobias Grosserabfbe632013-02-05 12:09:06 +0000330 ScopStmt *Statement)
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000331 : Statement(Statement), Inst(AccInst), newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000332
Tobias Grosser9759f852011-11-10 12:44:55 +0000333 BaseAddr = Access.getBase();
Tobias Grosser084d8f72012-05-29 09:29:44 +0000334 setBaseName();
Tobias Grosser5683df42011-11-09 22:34:34 +0000335
Tobias Grossera1879642011-12-20 10:43:14 +0000336 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000337 // We overapproximate non-affine accesses with a possible access to the
338 // whole array. For read accesses it does not make a difference, if an
339 // access must or may happen. However, for write accesses it is important to
340 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000341 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000342 Type = Access.isRead() ? READ : MAY_WRITE;
Tobias Grossera1879642011-12-20 10:43:14 +0000343 return;
344 }
345
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000346 Type = Access.isRead() ? READ : MUST_WRITE;
Tobias Grosser4f967492013-06-23 05:21:18 +0000347
Tobias Grosser79baa212014-04-10 08:38:02 +0000348 isl_space *Space = isl_space_alloc(Statement->getIslCtx(), 0,
349 Statement->getNumIterators(), 0);
350 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000351
Tobias Grosser79baa212014-04-10 08:38:02 +0000352 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000353 isl_pw_aff *Affine =
354 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000355
Sebastian Pop422e33f2014-06-03 18:16:31 +0000356 if (Size == 1) {
357 // For the non delinearized arrays, divide the access function of the last
358 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000359 //
360 // A stride one array access in C expressed as A[i] is expressed in
361 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
362 // two subsequent values of 'i' index two values that are stored next to
363 // each other in memory. By this division we make this characteristic
364 // obvious again.
365 isl_val *v;
366 v = isl_val_int_from_si(isl_pw_aff_get_ctx(Affine),
367 Access.getElemSizeInBytes());
368 Affine = isl_pw_aff_scale_down_val(Affine, v);
369 }
370
371 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
372
Tobias Grosser79baa212014-04-10 08:38:02 +0000373 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000374 }
375
Tobias Grosser79baa212014-04-10 08:38:02 +0000376 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000377 AccessRelation = isl_map_set_tuple_id(
378 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser084d8f72012-05-29 09:29:44 +0000379 isl_space_free(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000380 AccessRelation = isl_map_set_tuple_name(AccessRelation, isl_dim_out,
381 getBaseName().c_str());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000382}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000383
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000384void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000385 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000386 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000387}
388
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000389MemoryAccess::MemoryAccess(const Value *BaseAddress, ScopStmt *Statement)
390 : Type(READ), BaseAddr(BaseAddress), Statement(Statement),
391 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000392
393 isl_basic_map *BasicAccessMap = createBasicAccessMap(Statement);
394 AccessRelation = isl_map_from_basic_map(BasicAccessMap);
Tobias Grosser37487052011-10-06 00:03:42 +0000395 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
396 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000397}
398
399void MemoryAccess::print(raw_ostream &OS) const {
Tobias Grosser4f967492013-06-23 05:21:18 +0000400 switch (Type) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000401 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000402 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000403 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000404 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000405 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000406 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000407 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000408 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000409 break;
410 }
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000411 OS << "[Reduction like: " << isReductionLike() << "]\n";
Tobias Grosser5d453812011-10-06 00:04:11 +0000412 OS.indent(16) << getAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000413}
414
Tobias Grosser74394f02013-01-14 22:40:23 +0000415void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000416
417// Create a map in the size of the provided set domain, that maps from the
418// one element of the provided set domain to another element of the provided
419// set domain.
420// The mapping is limited to all points that are equal in all but the last
421// dimension and for which the last dimension of the input is strict smaller
422// than the last dimension of the output.
423//
424// getEqualAndLarger(set[i0, i1, ..., iX]):
425//
426// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
427// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
428//
Tobias Grosserf5338802011-10-06 00:03:35 +0000429static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000430 isl_space *Space = isl_space_map_from_set(setDomain);
431 isl_map *Map = isl_map_universe(isl_space_copy(Space));
432 isl_local_space *MapLocalSpace = isl_local_space_from_space(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000433 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000434
435 // Set all but the last dimension to be equal for the input and output
436 //
437 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
438 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000439 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000440 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000441
442 // Set the last dimension of the input to be strict smaller than the
443 // last dimension of the output.
444 //
445 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
446 //
Tobias Grosseredab1352013-06-21 06:41:31 +0000447 isl_val *v;
448 isl_ctx *Ctx = isl_map_get_ctx(Map);
Tobias Grosserf5338802011-10-06 00:03:35 +0000449 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosseredab1352013-06-21 06:41:31 +0000450 v = isl_val_int_from_si(Ctx, -1);
451 c = isl_constraint_set_coefficient_val(c, isl_dim_in, lastDimension, v);
452 v = isl_val_int_from_si(Ctx, 1);
453 c = isl_constraint_set_coefficient_val(c, isl_dim_out, lastDimension, v);
454 v = isl_val_int_from_si(Ctx, -1);
455 c = isl_constraint_set_constant_val(c, v);
Tobias Grosser75805372011-04-29 06:27:02 +0000456
Tobias Grosserc327932c2012-02-01 14:23:36 +0000457 Map = isl_map_add_constraint(Map, c);
Tobias Grosser75805372011-04-29 06:27:02 +0000458
Tobias Grosser23b36662011-10-17 08:32:36 +0000459 isl_local_space_free(MapLocalSpace);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000460 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000461}
462
Sebastian Popa00a0292012-12-18 07:46:06 +0000463isl_set *MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000464 isl_map *S = const_cast<isl_map *>(Schedule);
Sebastian Popa00a0292012-12-18 07:46:06 +0000465 isl_map *AccessRelation = getAccessRelation();
466 isl_space *Space = isl_space_range(isl_map_get_space(S));
467 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000468
Sebastian Popa00a0292012-12-18 07:46:06 +0000469 S = isl_map_reverse(S);
470 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000471
Sebastian Popa00a0292012-12-18 07:46:06 +0000472 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
473 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
474 NextScatt = isl_map_apply_domain(NextScatt, S);
475 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000476
Sebastian Popa00a0292012-12-18 07:46:06 +0000477 isl_set *Deltas = isl_map_deltas(NextScatt);
478 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000479}
480
Sebastian Popa00a0292012-12-18 07:46:06 +0000481bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000482 int StrideWidth) const {
483 isl_set *Stride, *StrideX;
484 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000485
Sebastian Popa00a0292012-12-18 07:46:06 +0000486 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000487 StrideX = isl_set_universe(isl_set_get_space(Stride));
488 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
489 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000490
Tobias Grosser28dd4862012-01-24 16:42:16 +0000491 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000492 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000493
Tobias Grosser28dd4862012-01-24 16:42:16 +0000494 return IsStrideX;
495}
496
Sebastian Popa00a0292012-12-18 07:46:06 +0000497bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
498 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000499}
500
Tobias Grosser79baa212014-04-10 08:38:02 +0000501bool MemoryAccess::isScalar() const {
502 return isl_map_n_out(AccessRelation) == 0;
503}
504
Sebastian Popa00a0292012-12-18 07:46:06 +0000505bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
506 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000507}
508
Tobias Grosser5d453812011-10-06 00:04:11 +0000509void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000510 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000511 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000512}
Tobias Grosser75805372011-04-29 06:27:02 +0000513
514//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000515
Tobias Grosser74394f02013-01-14 22:40:23 +0000516isl_map *ScopStmt::getScattering() const { return isl_map_copy(Scattering); }
Tobias Grossercf3942d2011-10-06 00:04:05 +0000517
Tobias Grosser37eb4222014-02-20 21:43:54 +0000518void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
519 assert(isl_set_is_subset(NewDomain, Domain) &&
520 "New domain is not a subset of old domain!");
521 isl_set_free(Domain);
522 Domain = NewDomain;
523 Scattering = isl_map_intersect_domain(Scattering, isl_set_copy(Domain));
524}
525
Tobias Grossercf3942d2011-10-06 00:04:05 +0000526void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000527 assert(NewScattering && "New scattering is nullptr");
Tobias Grosserb76f38532011-08-20 11:11:25 +0000528 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000529 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000530}
531
Tobias Grosser75805372011-04-29 06:27:02 +0000532void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000533 unsigned NbIterators = getNumIterators();
534 unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1;
535
Tobias Grosser084d8f72012-05-29 09:29:44 +0000536 isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, NbScatteringDims);
Tobias Grosserf5338802011-10-06 00:03:35 +0000537 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000538
Tobias Grosser084d8f72012-05-29 09:29:44 +0000539 Scattering = isl_map_from_domain_and_range(isl_set_universe(getDomainSpace()),
540 isl_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000541
542 // Loop dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000543 for (unsigned i = 0; i < NbIterators; ++i)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000544 Scattering =
545 isl_map_equate(Scattering, isl_dim_out, 2 * i + 1, isl_dim_in, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000546
547 // Constant dimensions
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000548 for (unsigned i = 0; i < NbIterators + 1; ++i)
549 Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000550
551 // Fill scattering dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000552 for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i)
553 Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000554
Tobias Grosser37487052011-10-06 00:03:42 +0000555 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000556}
557
558void ScopStmt::buildAccesses(TempScop &tempScop, const Region &CurRegion) {
559 const AccFuncSetType *AccFuncs = tempScop.getAccessFunctions(BB);
560
561 for (AccFuncSetType::const_iterator I = AccFuncs->begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +0000562 E = AccFuncs->end();
563 I != E; ++I) {
Hongbin Zhengcea35f62012-07-06 06:47:03 +0000564 MemAccs.push_back(new MemoryAccess(I->first, I->second, this));
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000565
566 // We do not track locations for scalar memory accesses at the moment.
567 //
568 // We do not have a use for this information at the moment. If we need this
569 // at some point, the "instruction -> access" mapping needs to be enhanced
570 // as a single instruction could then possibly perform multiple accesses.
571 if (!I->first.isScalar()) {
572 assert(!InstructionToAccess.count(I->second) &&
Tobias Grosser3fc91542014-02-20 21:43:45 +0000573 "Unexpected 1-to-N mapping on instruction to access map!");
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000574 InstructionToAccess[I->second] = MemAccs.back();
575 }
Tobias Grosser75805372011-04-29 06:27:02 +0000576 }
577}
578
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000579void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000580 for (MemoryAccess *MA : *this)
581 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000582
583 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
584 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
585}
586
Tobias Grosser65b00582011-11-08 15:41:19 +0000587__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000588 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
589 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000590
Tobias Grosserd2795d02011-08-18 07:51:40 +0000591 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000592 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000593 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000594 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000595 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000596 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000597 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000598 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000599 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000600 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000601 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000602 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000603 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000604 case ICmpInst::ICMP_ULT:
605 case ICmpInst::ICMP_UGT:
606 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000607 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000608 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000609 default:
610 llvm_unreachable("Non integer predicate not supported");
611 }
Tobias Grosser75805372011-04-29 06:27:02 +0000612}
613
Tobias Grossere19661e2011-10-07 08:46:57 +0000614__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000615 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000616 isl_space *Space;
617 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000618
Tobias Grossere19661e2011-10-07 08:46:57 +0000619 Space = isl_set_get_space(Domain);
620 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000621
Tobias Grosser75805372011-04-29 06:27:02 +0000622 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000623 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000624 isl_pw_aff *IV =
625 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000626
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000627 // 0 <= IV.
628 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
629 Domain = isl_set_intersect(Domain, LowerBound);
630
631 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000632 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000633 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000634 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
635 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000636 Domain = isl_set_intersect(Domain, UpperBoundSet);
637 }
638
Tobias Grosserf5338802011-10-06 00:03:35 +0000639 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000640 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000641}
642
Tobias Grossere602a072013-05-07 07:30:56 +0000643__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
644 TempScop &tempScop,
645 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000646 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000647 *CurrentRegion = &CurRegion;
Tobias Grossere19661e2011-10-07 08:46:57 +0000648 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000649
Tobias Grosser75805372011-04-29 06:27:02 +0000650 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000651 if (BranchingBB != CurrentRegion->getEntry()) {
652 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
653 for (BBCond::const_iterator CI = Condition->begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +0000654 CE = Condition->end();
655 CI != CE; ++CI) {
Tobias Grosser048c8792011-10-23 20:59:20 +0000656 isl_set *ConditionSet = buildConditionSet(*CI);
Tobias Grossere19661e2011-10-07 08:46:57 +0000657 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000658 }
659 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000660 BranchingBB = CurrentRegion->getEntry();
661 CurrentRegion = CurrentRegion->getParent();
662 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000663
Tobias Grossere19661e2011-10-07 08:46:57 +0000664 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000665}
666
Tobias Grossere602a072013-05-07 07:30:56 +0000667__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
668 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000669 isl_space *Space;
670 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000671 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000672
673 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
674
Tobias Grosser084d8f72012-05-29 09:29:44 +0000675 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
676
Tobias Grossere19661e2011-10-07 08:46:57 +0000677 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000678 Domain = addLoopBoundsToDomain(Domain, tempScop);
679 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000680 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +0000681
682 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000683}
684
Tobias Grosser74394f02013-01-14 22:40:23 +0000685ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Sebastian Pop860e0212013-02-15 21:26:44 +0000686 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest,
Tobias Grosser75805372011-04-29 06:27:02 +0000687 SmallVectorImpl<unsigned> &Scatter)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000688 : Parent(parent), BB(&bb), IVS(Nest.size()), NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000689 // Setup the induction variables.
Sebastian Pop860e0212013-02-15 21:26:44 +0000690 for (unsigned i = 0, e = Nest.size(); i < e; ++i) {
Sebastian Pop27c10c62013-03-22 22:07:43 +0000691 if (!SCEVCodegen) {
692 PHINode *PN = Nest[i]->getCanonicalInductionVariable();
693 assert(PN && "Non canonical IV in Scop!");
Tobias Grosser826b2af2013-03-21 16:14:50 +0000694 IVS[i] = PN;
Sebastian Pop27c10c62013-03-22 22:07:43 +0000695 }
Sebastian Pop860e0212013-02-15 21:26:44 +0000696 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +0000697 }
698
699 raw_string_ostream OS(BaseName);
Tobias Grosserf240b482014-01-09 10:42:15 +0000700 bb.printAsOperand(OS, false);
Tobias Grosser75805372011-04-29 06:27:02 +0000701 BaseName = OS.str();
702
Tobias Grosser75805372011-04-29 06:27:02 +0000703 makeIslCompatible(BaseName);
704 BaseName = "Stmt_" + BaseName;
705
Tobias Grossere19661e2011-10-07 08:46:57 +0000706 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000707 buildScattering(Scatter);
708 buildAccesses(tempScop, CurRegion);
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000709 checkForReduction();
710}
711
712void ScopStmt::checkForReduction() {
713 // Skip statements with more than one binary reduction
714 if (MemAccs.size() != 2)
715 return;
716
717 // Skip if there is not exactly one load and one store
718 unsigned LoadIdx = MemAccs[0]->isRead() ? 0 : 1;
719 auto *Load = dyn_cast<LoadInst>(MemAccs[LoadIdx]->getAccessInstruction());
720 auto *Store =
721 dyn_cast<StoreInst>(MemAccs[1 - LoadIdx]->getAccessInstruction());
722 if (!Load || !Store ||
723 Load->getPointerOperand() != Store->getPointerOperand())
724 return;
725
726 // Skip if there is not one binary operator between the load and the store
727 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
728 if (!BinOp || (BinOp->getOperand(0) != Load && BinOp->getOperand(1) != Load))
729 return;
730
731 // Skip if the opcode of the binary operator is not commutative/associative
732 if (!BinOp->isCommutative() || !BinOp->isAssociative())
733 return;
734
735 // Skip if the load has multiple uses
736 if (Load->getNumUses() != 1)
737 return;
738
739 // Skip if it is a multiplicative reduction and we disabled them
740 if (DisableMultiplicativeReductions &&
741 (BinOp->getOpcode() == Instruction::Mul ||
742 BinOp->getOpcode() == Instruction::FMul))
743 return;
744
Johannes Doerfertffa73bd2014-06-20 16:58:12 +0000745 // Valid reduction like access
746 MemAccs[0]->markReductionLike();
747 MemAccs[1]->markReductionLike();
Tobias Grosser75805372011-04-29 06:27:02 +0000748}
749
Tobias Grosser74394f02013-01-14 22:40:23 +0000750std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +0000751
752std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000753 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000754}
755
Tobias Grosser74394f02013-01-14 22:40:23 +0000756unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000757
758unsigned ScopStmt::getNumIterators() const {
759 // The final read has one dimension with one element.
760 if (!BB)
761 return 1;
762
Sebastian Pop860e0212013-02-15 21:26:44 +0000763 return NestLoops.size();
Tobias Grosser75805372011-04-29 06:27:02 +0000764}
765
766unsigned ScopStmt::getNumScattering() const {
767 return isl_map_dim(Scattering, isl_dim_out);
768}
769
770const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
771
Tobias Grosserabfbe632013-02-05 12:09:06 +0000772const PHINode *
773ScopStmt::getInductionVariableForDimension(unsigned Dimension) const {
Sebastian Popf30d3b22013-02-15 21:26:48 +0000774 return IVS[Dimension];
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000775}
776
777const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +0000778 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +0000779}
780
Tobias Grosser74394f02013-01-14 22:40:23 +0000781isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000782
Tobias Grosser74394f02013-01-14 22:40:23 +0000783isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000784
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000785isl_space *ScopStmt::getDomainSpace() const {
786 return isl_set_get_space(Domain);
787}
788
Tobias Grosser74394f02013-01-14 22:40:23 +0000789isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); }
Tobias Grossercd95b772012-08-30 11:49:38 +0000790
Tobias Grosser75805372011-04-29 06:27:02 +0000791ScopStmt::~ScopStmt() {
792 while (!MemAccs.empty()) {
793 delete MemAccs.back();
794 MemAccs.pop_back();
795 }
796
797 isl_set_free(Domain);
798 isl_map_free(Scattering);
799}
800
801void ScopStmt::print(raw_ostream &OS) const {
802 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000803 OS.indent(12) << "Domain :=\n";
804
805 if (Domain) {
806 OS.indent(16) << getDomainStr() << ";\n";
807 } else
808 OS.indent(16) << "n/a\n";
809
810 OS.indent(12) << "Scattering :=\n";
811
812 if (Domain) {
813 OS.indent(16) << getScatteringStr() << ";\n";
814 } else
815 OS.indent(16) << "n/a\n";
816
817 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000818 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +0000819 (*I)->print(OS);
820}
821
822void ScopStmt::dump() const { print(dbgs()); }
823
824//===----------------------------------------------------------------------===//
825/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000826
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000827void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +0000828 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
829 isl_set_free(Context);
830 Context = NewContext;
831}
832
Tobias Grosserabfbe632013-02-05 12:09:06 +0000833void Scop::addParams(std::vector<const SCEV *> NewParameters) {
834 for (std::vector<const SCEV *>::iterator PI = NewParameters.begin(),
835 PE = NewParameters.end();
836 PI != PE; ++PI) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000837 const SCEV *Parameter = *PI;
838
839 if (ParameterIds.find(Parameter) != ParameterIds.end())
840 continue;
841
842 int dimension = Parameters.size();
843
844 Parameters.push_back(Parameter);
845 ParameterIds[Parameter] = dimension;
846 }
847}
848
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000849__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
850 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000851
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000852 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000853 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000854
Tobias Grosser8f99c162011-11-15 11:38:55 +0000855 std::string ParameterName;
856
857 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
858 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000859 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +0000860 }
861
862 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +0000863 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +0000864
Tobias Grosser20532b82014-04-11 17:56:49 +0000865 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
866 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +0000867}
Tobias Grosser75805372011-04-29 06:27:02 +0000868
Tobias Grosser6be480c2011-11-08 15:41:13 +0000869void Scop::buildContext() {
870 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +0000871 Context = isl_set_universe(isl_space_copy(Space));
872 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000873}
874
Tobias Grosser18daaca2012-05-22 10:47:27 +0000875void Scop::addParameterBounds() {
876 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
Tobias Grosseredab1352013-06-21 06:41:31 +0000877 isl_val *V;
Tobias Grosser18daaca2012-05-22 10:47:27 +0000878 isl_id *Id;
879 const SCEV *Scev;
880 const IntegerType *T;
881
882 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserabfbe632013-02-05 12:09:06 +0000883 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000884 T = dyn_cast<IntegerType>(Scev->getType());
885 isl_id_free(Id);
886
887 assert(T && "Not an integer type");
888 int Width = T->getBitWidth();
889
Tobias Grosseredab1352013-06-21 06:41:31 +0000890 V = isl_val_int_from_si(IslCtx, Width - 1);
891 V = isl_val_2exp(V);
892 V = isl_val_neg(V);
893 Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000894
Tobias Grosseredab1352013-06-21 06:41:31 +0000895 V = isl_val_int_from_si(IslCtx, Width - 1);
896 V = isl_val_2exp(V);
897 V = isl_val_sub_ui(V, 1);
898 Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000899 }
900}
901
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000902void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000903 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000904 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000905
906 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
907 PI != PE; ++PI) {
908 const SCEV *Parameter = PI->first;
909 isl_id *id = getIdForParam(Parameter);
910 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
911 }
912
913 // Align the parameters of all data structures to the model.
914 Context = isl_set_align_params(Context, Space);
915
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000916 for (iterator I = begin(), E = end(); I != E; ++I)
917 (*I)->realignParams();
918}
919
Tobias Grosser0e27e242011-10-06 00:03:48 +0000920Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
921 isl_ctx *Context)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000922 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
923 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000924 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000925 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +0000926
Tobias Grosserabfbe632013-02-05 12:09:06 +0000927 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +0000928 SmallVector<unsigned, 8> Scatter;
929
930 Scatter.assign(MaxLoopDepth + 1, 0);
931
932 // Build the iteration domain, access functions and scattering functions
933 // traversing the region tree.
934 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +0000935
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000936 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +0000937 addParameterBounds();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000938
Tobias Grosser75805372011-04-29 06:27:02 +0000939 assert(NestLoops.empty() && "NestLoops not empty at top level!");
940}
941
942Scop::~Scop() {
943 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +0000944 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +0000945
946 // Free the statements;
947 for (iterator I = begin(), E = end(); I != E; ++I)
948 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +0000949}
950
Tobias Grosser74394f02013-01-14 22:40:23 +0000951std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser75805372011-04-29 06:27:02 +0000952
953std::string Scop::getNameStr() const {
954 std::string ExitName, EntryName;
955 raw_string_ostream ExitStr(ExitName);
956 raw_string_ostream EntryStr(EntryName);
957
Tobias Grosserf240b482014-01-09 10:42:15 +0000958 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +0000959 EntryStr.str();
960
961 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +0000962 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +0000963 ExitStr.str();
964 } else
965 ExitName = "FunctionExit";
966
967 return EntryName + "---" + ExitName;
968}
969
Tobias Grosser74394f02013-01-14 22:40:23 +0000970__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +0000971__isl_give isl_space *Scop::getParamSpace() const {
972 return isl_set_get_space(this->Context);
973}
974
Tobias Grossere86109f2013-10-29 21:05:49 +0000975__isl_give isl_set *Scop::getAssumedContext() const {
976 return isl_set_copy(AssumedContext);
977}
978
Tobias Grosser75805372011-04-29 06:27:02 +0000979void Scop::printContext(raw_ostream &OS) const {
980 OS << "Context:\n";
981
982 if (!Context) {
983 OS.indent(4) << "n/a\n\n";
984 return;
985 }
986
987 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +0000988
989 for (ParamVecType::const_iterator PI = Parameters.begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +0000990 PE = Parameters.end();
991 PI != PE; ++PI) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000992 const SCEV *Parameter = *PI;
993 int Dim = ParameterIds.find(Parameter)->second;
994
995 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
996 }
Tobias Grosser75805372011-04-29 06:27:02 +0000997}
998
999void Scop::printStatements(raw_ostream &OS) const {
1000 OS << "Statements {\n";
1001
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001002 for (const_iterator SI = begin(), SE = end(); SI != SE; ++SI)
Tobias Grosser75805372011-04-29 06:27:02 +00001003 OS.indent(4) << (**SI);
1004
1005 OS.indent(4) << "}\n";
1006}
1007
Tobias Grosser75805372011-04-29 06:27:02 +00001008void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001009 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1010 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001011 OS.indent(4) << "Region: " << getNameStr() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001012 printContext(OS.indent(4));
1013 printStatements(OS.indent(4));
1014}
1015
1016void Scop::dump() const { print(dbgs()); }
1017
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001018isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001019
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001020__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001021 isl_union_set *Domain = nullptr;
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001022
1023 for (Scop::iterator SI = begin(), SE = end(); SI != SE; ++SI)
Tobias Grosser3cbe5cf2012-03-08 15:21:51 +00001024 if (!Domain)
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001025 Domain = isl_union_set_from_set((*SI)->getDomain());
1026 else
1027 Domain = isl_union_set_union(Domain,
Tobias Grosserabfbe632013-02-05 12:09:06 +00001028 isl_union_set_from_set((*SI)->getDomain()));
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001029
1030 return Domain;
1031}
1032
Tobias Grosser37eb4222014-02-20 21:43:54 +00001033__isl_give isl_union_map *Scop::getWrites() {
1034 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1035
1036 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1037 ScopStmt *Stmt = *SI;
1038
Johannes Doerfertf6752892014-06-13 18:01:45 +00001039 for (MemoryAccess *MA : *Stmt) {
1040 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001041 continue;
1042
1043 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001044 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001045
1046 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1047 Write = isl_union_map_add_map(Write, AccessDomain);
1048 }
1049 }
1050 return isl_union_map_coalesce(Write);
1051}
1052
1053__isl_give isl_union_map *Scop::getReads() {
1054 isl_union_map *Read = isl_union_map_empty(this->getParamSpace());
1055
1056 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1057 ScopStmt *Stmt = *SI;
1058
Johannes Doerfertf6752892014-06-13 18:01:45 +00001059 for (MemoryAccess *MA : *Stmt) {
1060 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001061 continue;
1062
1063 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001064 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001065
1066 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1067 Read = isl_union_map_add_map(Read, AccessDomain);
1068 }
1069 }
1070 return isl_union_map_coalesce(Read);
1071}
1072
1073__isl_give isl_union_map *Scop::getSchedule() {
1074 isl_union_map *Schedule = isl_union_map_empty(this->getParamSpace());
1075
1076 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1077 ScopStmt *Stmt = *SI;
1078 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
1079 }
1080 return isl_union_map_coalesce(Schedule);
1081}
1082
1083bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1084 bool Changed = false;
1085 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1086 ScopStmt *Stmt = *SI;
1087 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
1088
1089 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1090 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1091
1092 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1093 isl_union_set_free(StmtDomain);
1094 isl_union_set_free(NewStmtDomain);
1095 continue;
1096 }
1097
1098 Changed = true;
1099
1100 isl_union_set_free(StmtDomain);
1101 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1102
1103 if (isl_union_set_is_empty(NewStmtDomain)) {
1104 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1105 isl_union_set_free(NewStmtDomain);
1106 } else
1107 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1108 }
1109 isl_union_set_free(Domain);
1110 return Changed;
1111}
1112
Tobias Grosser75805372011-04-29 06:27:02 +00001113ScalarEvolution *Scop::getSE() const { return SE; }
1114
1115bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1116 if (tempScop.getAccessFunctions(BB))
1117 return false;
1118
1119 return true;
1120}
1121
Tobias Grosser74394f02013-01-14 22:40:23 +00001122void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1123 SmallVectorImpl<Loop *> &NestLoops,
1124 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001125 Loop *L = castToLoop(CurRegion, LI);
1126
1127 if (L)
1128 NestLoops.push_back(L);
1129
1130 unsigned loopDepth = NestLoops.size();
1131 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1132
1133 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001134 E = CurRegion.element_end();
1135 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001136 if (I->isSubRegion())
1137 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1138 else {
1139 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1140
1141 if (isTrivialBB(BB, tempScop))
1142 continue;
1143
Tobias Grosserabfbe632013-02-05 12:09:06 +00001144 Stmts.push_back(
1145 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter));
Tobias Grosser75805372011-04-29 06:27:02 +00001146
1147 // Increasing the Scattering function is OK for the moment, because
1148 // we are using a depth first iterator and the program is well structured.
1149 ++Scatter[loopDepth];
1150 }
1151
1152 if (!L)
1153 return;
1154
1155 // Exiting a loop region.
1156 Scatter[loopDepth] = 0;
1157 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001158 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001159}
1160
1161//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001162ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1163 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001164 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001165}
1166
1167ScopInfo::~ScopInfo() {
1168 clear();
1169 isl_ctx_free(ctx);
1170}
1171
Tobias Grosser75805372011-04-29 06:27:02 +00001172void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1173 AU.addRequired<LoopInfo>();
1174 AU.addRequired<RegionInfo>();
1175 AU.addRequired<ScalarEvolution>();
1176 AU.addRequired<TempScopInfo>();
1177 AU.setPreservesAll();
1178}
1179
1180bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1181 LoopInfo &LI = getAnalysis<LoopInfo>();
1182 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1183
1184 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1185
1186 // This region is no Scop.
1187 if (!tempScop) {
1188 scop = 0;
1189 return false;
1190 }
1191
1192 // Statistics.
1193 ++ScopFound;
Tobias Grosser74394f02013-01-14 22:40:23 +00001194 if (tempScop->getMaxLoopDepth() > 0)
1195 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00001196
Tobias Grosserb76f38532011-08-20 11:11:25 +00001197 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001198
1199 return false;
1200}
1201
1202char ScopInfo::ID = 0;
1203
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001204Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1205
Tobias Grosser73600b82011-10-08 00:30:40 +00001206INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1207 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001208 false);
1209INITIALIZE_PASS_DEPENDENCY(LoopInfo);
1210INITIALIZE_PASS_DEPENDENCY(RegionInfo);
1211INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1212INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001213INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1214 "Polly - Create polyhedral description of Scops", false,
1215 false)