blob: 4cc1e80b73fef23c322df0a6fc7a9019aba93b2f [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) {
Tobias Grosser083d3d32014-06-28 08:59:45 +0000559 for (auto &&Access : *tempScop.getAccessFunctions(BB)) {
560 MemAccs.push_back(new MemoryAccess(Access.first, Access.second, this));
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000561
562 // We do not track locations for scalar memory accesses at the moment.
563 //
564 // We do not have a use for this information at the moment. If we need this
565 // at some point, the "instruction -> access" mapping needs to be enhanced
566 // as a single instruction could then possibly perform multiple accesses.
Tobias Grosser083d3d32014-06-28 08:59:45 +0000567 if (!Access.first.isScalar()) {
568 assert(!InstructionToAccess.count(Access.second) &&
Tobias Grosser3fc91542014-02-20 21:43:45 +0000569 "Unexpected 1-to-N mapping on instruction to access map!");
Tobias Grosser083d3d32014-06-28 08:59:45 +0000570 InstructionToAccess[Access.second] = MemAccs.back();
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000571 }
Tobias Grosser75805372011-04-29 06:27:02 +0000572 }
573}
574
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000575void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000576 for (MemoryAccess *MA : *this)
577 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000578
579 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
580 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
581}
582
Tobias Grosser65b00582011-11-08 15:41:19 +0000583__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000584 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
585 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000586
Tobias Grosserd2795d02011-08-18 07:51:40 +0000587 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000588 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000589 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000590 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000591 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000592 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000593 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000594 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000595 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000596 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000597 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000598 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000599 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000600 case ICmpInst::ICMP_ULT:
601 case ICmpInst::ICMP_UGT:
602 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000603 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000604 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000605 default:
606 llvm_unreachable("Non integer predicate not supported");
607 }
Tobias Grosser75805372011-04-29 06:27:02 +0000608}
609
Tobias Grossere19661e2011-10-07 08:46:57 +0000610__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000611 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000612 isl_space *Space;
613 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000614
Tobias Grossere19661e2011-10-07 08:46:57 +0000615 Space = isl_set_get_space(Domain);
616 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000617
Tobias Grosser75805372011-04-29 06:27:02 +0000618 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000619 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000620 isl_pw_aff *IV =
621 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000622
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000623 // 0 <= IV.
624 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
625 Domain = isl_set_intersect(Domain, LowerBound);
626
627 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000628 const Loop *L = getLoopForDimension(i);
Tobias Grosser1179afa2011-11-02 21:37:51 +0000629 const SCEV *LatchExecutions = tempScop.getLoopBound(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000630 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
631 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000632 Domain = isl_set_intersect(Domain, UpperBoundSet);
633 }
634
Tobias Grosserf5338802011-10-06 00:03:35 +0000635 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000636 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000637}
638
Tobias Grossere602a072013-05-07 07:30:56 +0000639__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
640 TempScop &tempScop,
641 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000642 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000643 *CurrentRegion = &CurRegion;
Tobias Grossere19661e2011-10-07 08:46:57 +0000644 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000645
Tobias Grosser75805372011-04-29 06:27:02 +0000646 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000647 if (BranchingBB != CurrentRegion->getEntry()) {
648 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000649 for (const auto &C : *Condition) {
650 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000651 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000652 }
653 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000654 BranchingBB = CurrentRegion->getEntry();
655 CurrentRegion = CurrentRegion->getParent();
656 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000657
Tobias Grossere19661e2011-10-07 08:46:57 +0000658 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000659}
660
Tobias Grossere602a072013-05-07 07:30:56 +0000661__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
662 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000663 isl_space *Space;
664 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000665 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000666
667 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
668
Tobias Grosser084d8f72012-05-29 09:29:44 +0000669 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
670
Tobias Grossere19661e2011-10-07 08:46:57 +0000671 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000672 Domain = addLoopBoundsToDomain(Domain, tempScop);
673 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000674 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +0000675
676 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000677}
678
Tobias Grosser74394f02013-01-14 22:40:23 +0000679ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Sebastian Pop860e0212013-02-15 21:26:44 +0000680 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest,
Tobias Grosser75805372011-04-29 06:27:02 +0000681 SmallVectorImpl<unsigned> &Scatter)
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000682 : Parent(parent), BB(&bb), IVS(Nest.size()), NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000683 // Setup the induction variables.
Sebastian Pop860e0212013-02-15 21:26:44 +0000684 for (unsigned i = 0, e = Nest.size(); i < e; ++i) {
Sebastian Pop27c10c62013-03-22 22:07:43 +0000685 if (!SCEVCodegen) {
686 PHINode *PN = Nest[i]->getCanonicalInductionVariable();
687 assert(PN && "Non canonical IV in Scop!");
Tobias Grosser826b2af2013-03-21 16:14:50 +0000688 IVS[i] = PN;
Sebastian Pop27c10c62013-03-22 22:07:43 +0000689 }
Sebastian Pop860e0212013-02-15 21:26:44 +0000690 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +0000691 }
692
693 raw_string_ostream OS(BaseName);
Tobias Grosserf240b482014-01-09 10:42:15 +0000694 bb.printAsOperand(OS, false);
Tobias Grosser75805372011-04-29 06:27:02 +0000695 BaseName = OS.str();
696
Tobias Grosser75805372011-04-29 06:27:02 +0000697 makeIslCompatible(BaseName);
698 BaseName = "Stmt_" + BaseName;
699
Tobias Grossere19661e2011-10-07 08:46:57 +0000700 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000701 buildScattering(Scatter);
702 buildAccesses(tempScop, CurRegion);
Johannes Doerferte58a0122014-06-27 20:31:28 +0000703 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000704}
705
Johannes Doerferte58a0122014-06-27 20:31:28 +0000706/// @brief Collect loads which might form a reduction chain with @p StoreMA
707///
708/// Check if the stored value for @p StoreMA is a binary operator with one or
709/// two loads as operands. If the binary operand is commutative & associative,
710/// used only once (by @p StoreMA) and its load operands are also used only
711/// once, we have found a possible reduction chain. It starts at an operand
712/// load and includes the binary operator and @p StoreMA.
713///
714/// Note: We allow only one use to ensure the load and binary operator cannot
715/// escape this block or into any other store except @p StoreMA.
716void ScopStmt::collectCandiateReductionLoads(
717 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
718 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
719 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000720 return;
721
722 // Skip if there is not one binary operator between the load and the store
723 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +0000724 if (!BinOp)
725 return;
726
727 // Skip if the binary operators has multiple uses
728 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000729 return;
730
731 // Skip if the opcode of the binary operator is not commutative/associative
732 if (!BinOp->isCommutative() || !BinOp->isAssociative())
733 return;
734
Johannes Doerfert9890a052014-07-01 00:32:29 +0000735 // Skip if the binary operator is outside the current SCoP
736 if (BinOp->getParent() != Store->getParent())
737 return;
738
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000739 // 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 Doerferte58a0122014-06-27 20:31:28 +0000745 // Check the binary operator operands for a candidate load
746 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
747 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
748 if (!PossibleLoad0 && !PossibleLoad1)
749 return;
750
751 // A load is only a candidate if it cannot escape (thus has only this use)
752 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000753 if (PossibleLoad0->getParent() == Store->getParent())
754 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000755 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000756 if (PossibleLoad1->getParent() == Store->getParent())
757 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000758}
759
760/// @brief Check for reductions in this ScopStmt
761///
762/// Iterate over all store memory accesses and check for valid binary reduction
763/// like chains. For all candidates we check if they have the same base address
764/// and there are no other accesses which overlap with them. The base address
765/// check rules out impossible reductions candidates early. The overlap check,
766/// together with the "only one user" check in collectCandiateReductionLoads,
767/// guarantees that none of the intermediate results will escape during
768/// execution of the loop nest. We basically check here that no other memory
769/// access can access the same memory as the potential reduction.
770void ScopStmt::checkForReductions() {
771 SmallVector<MemoryAccess *, 2> Loads;
772 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
773
774 // First collect candidate load-store reduction chains by iterating over all
775 // stores and collecting possible reduction loads.
776 for (MemoryAccess *StoreMA : MemAccs) {
777 if (StoreMA->isRead())
778 continue;
779
780 Loads.clear();
781 collectCandiateReductionLoads(StoreMA, Loads);
782 for (MemoryAccess *LoadMA : Loads)
783 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
784 }
785
786 // Then check each possible candidate pair.
787 for (const auto &CandidatePair : Candidates) {
788 bool Valid = true;
789 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
790 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
791
792 // Skip those with obviously unequal base addresses.
793 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
794 isl_map_free(LoadAccs);
795 isl_map_free(StoreAccs);
796 continue;
797 }
798
799 // And check if the remaining for overlap with other memory accesses.
800 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
801 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
802 isl_set *AllAccs = isl_map_range(AllAccsRel);
803
804 for (MemoryAccess *MA : MemAccs) {
805 if (MA == CandidatePair.first || MA == CandidatePair.second)
806 continue;
807
808 isl_map *AccRel =
809 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
810 isl_set *Accs = isl_map_range(AccRel);
811
812 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
813 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
814 Valid = Valid && isl_set_is_empty(OverlapAccs);
815 isl_set_free(OverlapAccs);
816 }
817 }
818
819 isl_set_free(AllAccs);
820 if (!Valid)
821 continue;
822
823 // If no overlapping access was found we mark the load and store as
824 // reduction like.
825 CandidatePair.first->markReductionLike();
826 CandidatePair.second->markReductionLike();
827 }
Tobias Grosser75805372011-04-29 06:27:02 +0000828}
829
Tobias Grosser74394f02013-01-14 22:40:23 +0000830std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +0000831
832std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +0000833 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +0000834}
835
Tobias Grosser74394f02013-01-14 22:40:23 +0000836unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000837
838unsigned ScopStmt::getNumIterators() const {
839 // The final read has one dimension with one element.
840 if (!BB)
841 return 1;
842
Sebastian Pop860e0212013-02-15 21:26:44 +0000843 return NestLoops.size();
Tobias Grosser75805372011-04-29 06:27:02 +0000844}
845
846unsigned ScopStmt::getNumScattering() const {
847 return isl_map_dim(Scattering, isl_dim_out);
848}
849
850const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
851
Tobias Grosserabfbe632013-02-05 12:09:06 +0000852const PHINode *
853ScopStmt::getInductionVariableForDimension(unsigned Dimension) const {
Sebastian Popf30d3b22013-02-15 21:26:48 +0000854 return IVS[Dimension];
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000855}
856
857const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +0000858 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +0000859}
860
Tobias Grosser74394f02013-01-14 22:40:23 +0000861isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +0000862
Tobias Grosser74394f02013-01-14 22:40:23 +0000863isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +0000864
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000865isl_space *ScopStmt::getDomainSpace() const {
866 return isl_set_get_space(Domain);
867}
868
Tobias Grosser74394f02013-01-14 22:40:23 +0000869isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); }
Tobias Grossercd95b772012-08-30 11:49:38 +0000870
Tobias Grosser75805372011-04-29 06:27:02 +0000871ScopStmt::~ScopStmt() {
872 while (!MemAccs.empty()) {
873 delete MemAccs.back();
874 MemAccs.pop_back();
875 }
876
877 isl_set_free(Domain);
878 isl_map_free(Scattering);
879}
880
881void ScopStmt::print(raw_ostream &OS) const {
882 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000883 OS.indent(12) << "Domain :=\n";
884
885 if (Domain) {
886 OS.indent(16) << getDomainStr() << ";\n";
887 } else
888 OS.indent(16) << "n/a\n";
889
890 OS.indent(12) << "Scattering :=\n";
891
892 if (Domain) {
893 OS.indent(16) << getScatteringStr() << ";\n";
894 } else
895 OS.indent(16) << "n/a\n";
896
Tobias Grosser083d3d32014-06-28 08:59:45 +0000897 for (MemoryAccess *Access : MemAccs)
898 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +0000899}
900
901void ScopStmt::dump() const { print(dbgs()); }
902
903//===----------------------------------------------------------------------===//
904/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000905
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000906void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +0000907 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
908 isl_set_free(Context);
909 Context = NewContext;
910}
911
Tobias Grosserabfbe632013-02-05 12:09:06 +0000912void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +0000913 for (const SCEV *Parameter : NewParameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000914 if (ParameterIds.find(Parameter) != ParameterIds.end())
915 continue;
916
917 int dimension = Parameters.size();
918
919 Parameters.push_back(Parameter);
920 ParameterIds[Parameter] = dimension;
921 }
922}
923
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000924__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
925 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000926
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000927 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000928 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000929
Tobias Grosser8f99c162011-11-15 11:38:55 +0000930 std::string ParameterName;
931
932 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
933 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000934 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +0000935 }
936
937 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +0000938 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +0000939
Tobias Grosser20532b82014-04-11 17:56:49 +0000940 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
941 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +0000942}
Tobias Grosser75805372011-04-29 06:27:02 +0000943
Tobias Grosser6be480c2011-11-08 15:41:13 +0000944void Scop::buildContext() {
945 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +0000946 Context = isl_set_universe(isl_space_copy(Space));
947 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000948}
949
Tobias Grosser18daaca2012-05-22 10:47:27 +0000950void Scop::addParameterBounds() {
951 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
Tobias Grosseredab1352013-06-21 06:41:31 +0000952 isl_val *V;
Tobias Grosser18daaca2012-05-22 10:47:27 +0000953 isl_id *Id;
954 const SCEV *Scev;
955 const IntegerType *T;
956
957 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserabfbe632013-02-05 12:09:06 +0000958 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000959 T = dyn_cast<IntegerType>(Scev->getType());
960 isl_id_free(Id);
961
962 assert(T && "Not an integer type");
963 int Width = T->getBitWidth();
964
Tobias Grosseredab1352013-06-21 06:41:31 +0000965 V = isl_val_int_from_si(IslCtx, Width - 1);
966 V = isl_val_2exp(V);
967 V = isl_val_neg(V);
968 Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000969
Tobias Grosseredab1352013-06-21 06:41:31 +0000970 V = isl_val_int_from_si(IslCtx, Width - 1);
971 V = isl_val_2exp(V);
972 V = isl_val_sub_ui(V, 1);
973 Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000974 }
975}
976
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000977void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000978 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000979 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000980
Tobias Grosser083d3d32014-06-28 08:59:45 +0000981 for (const auto &ParamID : ParameterIds) {
982 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000983 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +0000984 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +0000985 }
986
987 // Align the parameters of all data structures to the model.
988 Context = isl_set_align_params(Context, Space);
989
Tobias Grosser083d3d32014-06-28 08:59:45 +0000990 for (ScopStmt *Stmt : *this)
991 Stmt->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000992}
993
Tobias Grosser0e27e242011-10-06 00:03:48 +0000994Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
995 isl_ctx *Context)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000996 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
997 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000998 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +0000999 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001000
Tobias Grosserabfbe632013-02-05 12:09:06 +00001001 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001002 SmallVector<unsigned, 8> Scatter;
1003
1004 Scatter.assign(MaxLoopDepth + 1, 0);
1005
1006 // Build the iteration domain, access functions and scattering functions
1007 // traversing the region tree.
1008 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +00001009
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001010 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001011 addParameterBounds();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001012
Tobias Grosser75805372011-04-29 06:27:02 +00001013 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1014}
1015
1016Scop::~Scop() {
1017 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001018 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +00001019
1020 // Free the statements;
Tobias Grosser083d3d32014-06-28 08:59:45 +00001021 for (ScopStmt *Stmt : *this)
1022 delete Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001023}
1024
Tobias Grosser74394f02013-01-14 22:40:23 +00001025std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser75805372011-04-29 06:27:02 +00001026
1027std::string Scop::getNameStr() const {
1028 std::string ExitName, EntryName;
1029 raw_string_ostream ExitStr(ExitName);
1030 raw_string_ostream EntryStr(EntryName);
1031
Tobias Grosserf240b482014-01-09 10:42:15 +00001032 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001033 EntryStr.str();
1034
1035 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001036 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001037 ExitStr.str();
1038 } else
1039 ExitName = "FunctionExit";
1040
1041 return EntryName + "---" + ExitName;
1042}
1043
Tobias Grosser74394f02013-01-14 22:40:23 +00001044__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001045__isl_give isl_space *Scop::getParamSpace() const {
1046 return isl_set_get_space(this->Context);
1047}
1048
Tobias Grossere86109f2013-10-29 21:05:49 +00001049__isl_give isl_set *Scop::getAssumedContext() const {
1050 return isl_set_copy(AssumedContext);
1051}
1052
Tobias Grosser75805372011-04-29 06:27:02 +00001053void Scop::printContext(raw_ostream &OS) const {
1054 OS << "Context:\n";
1055
1056 if (!Context) {
1057 OS.indent(4) << "n/a\n\n";
1058 return;
1059 }
1060
1061 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001062
Tobias Grosser083d3d32014-06-28 08:59:45 +00001063 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001064 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001065 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1066 }
Tobias Grosser75805372011-04-29 06:27:02 +00001067}
1068
1069void Scop::printStatements(raw_ostream &OS) const {
1070 OS << "Statements {\n";
1071
Tobias Grosser083d3d32014-06-28 08:59:45 +00001072 for (ScopStmt *Stmt : *this)
1073 OS.indent(4) << *Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001074
1075 OS.indent(4) << "}\n";
1076}
1077
Tobias Grosser75805372011-04-29 06:27:02 +00001078void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001079 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1080 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001081 OS.indent(4) << "Region: " << getNameStr() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001082 printContext(OS.indent(4));
1083 printStatements(OS.indent(4));
1084}
1085
1086void Scop::dump() const { print(dbgs()); }
1087
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001088isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001089
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001090__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001091 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001092
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001093 for (ScopStmt *Stmt : *this)
1094 Domain = isl_union_set_add_set(Domain, Stmt->getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001095
1096 return Domain;
1097}
1098
Tobias Grosser37eb4222014-02-20 21:43:54 +00001099__isl_give isl_union_map *Scop::getWrites() {
1100 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1101
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001102 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001103 for (MemoryAccess *MA : *Stmt) {
1104 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001105 continue;
1106
1107 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001108 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001109 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1110 Write = isl_union_map_add_map(Write, AccessDomain);
1111 }
1112 }
1113 return isl_union_map_coalesce(Write);
1114}
1115
1116__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001117 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001118
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001119 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001120 for (MemoryAccess *MA : *Stmt) {
1121 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001122 continue;
1123
1124 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001125 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001126
1127 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1128 Read = isl_union_map_add_map(Read, AccessDomain);
1129 }
1130 }
1131 return isl_union_map_coalesce(Read);
1132}
1133
1134__isl_give isl_union_map *Scop::getSchedule() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001135 isl_union_map *Schedule = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001136
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001137 for (ScopStmt *Stmt : *this)
Tobias Grosser37eb4222014-02-20 21:43:54 +00001138 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001139
Tobias Grosser37eb4222014-02-20 21:43:54 +00001140 return isl_union_map_coalesce(Schedule);
1141}
1142
1143bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1144 bool Changed = false;
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001145 for (ScopStmt *Stmt : *this) {
Tobias Grosser37eb4222014-02-20 21:43:54 +00001146 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001147 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1148 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1149
1150 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1151 isl_union_set_free(StmtDomain);
1152 isl_union_set_free(NewStmtDomain);
1153 continue;
1154 }
1155
1156 Changed = true;
1157
1158 isl_union_set_free(StmtDomain);
1159 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1160
1161 if (isl_union_set_is_empty(NewStmtDomain)) {
1162 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1163 isl_union_set_free(NewStmtDomain);
1164 } else
1165 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1166 }
1167 isl_union_set_free(Domain);
1168 return Changed;
1169}
1170
Tobias Grosser75805372011-04-29 06:27:02 +00001171ScalarEvolution *Scop::getSE() const { return SE; }
1172
1173bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1174 if (tempScop.getAccessFunctions(BB))
1175 return false;
1176
1177 return true;
1178}
1179
Tobias Grosser74394f02013-01-14 22:40:23 +00001180void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1181 SmallVectorImpl<Loop *> &NestLoops,
1182 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001183 Loop *L = castToLoop(CurRegion, LI);
1184
1185 if (L)
1186 NestLoops.push_back(L);
1187
1188 unsigned loopDepth = NestLoops.size();
1189 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1190
1191 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001192 E = CurRegion.element_end();
1193 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001194 if (I->isSubRegion())
1195 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1196 else {
1197 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1198
1199 if (isTrivialBB(BB, tempScop))
1200 continue;
1201
Tobias Grosserabfbe632013-02-05 12:09:06 +00001202 Stmts.push_back(
1203 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter));
Tobias Grosser75805372011-04-29 06:27:02 +00001204
1205 // Increasing the Scattering function is OK for the moment, because
1206 // we are using a depth first iterator and the program is well structured.
1207 ++Scatter[loopDepth];
1208 }
1209
1210 if (!L)
1211 return;
1212
1213 // Exiting a loop region.
1214 Scatter[loopDepth] = 0;
1215 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001216 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001217}
1218
1219//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001220ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1221 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001222 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001223}
1224
1225ScopInfo::~ScopInfo() {
1226 clear();
1227 isl_ctx_free(ctx);
1228}
1229
Tobias Grosser75805372011-04-29 06:27:02 +00001230void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1231 AU.addRequired<LoopInfo>();
1232 AU.addRequired<RegionInfo>();
1233 AU.addRequired<ScalarEvolution>();
1234 AU.addRequired<TempScopInfo>();
1235 AU.setPreservesAll();
1236}
1237
1238bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1239 LoopInfo &LI = getAnalysis<LoopInfo>();
1240 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1241
1242 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1243
1244 // This region is no Scop.
1245 if (!tempScop) {
1246 scop = 0;
1247 return false;
1248 }
1249
1250 // Statistics.
1251 ++ScopFound;
Tobias Grosser74394f02013-01-14 22:40:23 +00001252 if (tempScop->getMaxLoopDepth() > 0)
1253 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00001254
Tobias Grosserb76f38532011-08-20 11:11:25 +00001255 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001256
1257 return false;
1258}
1259
1260char ScopInfo::ID = 0;
1261
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001262Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1263
Tobias Grosser73600b82011-10-08 00:30:40 +00001264INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1265 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001266 false);
1267INITIALIZE_PASS_DEPENDENCY(LoopInfo);
1268INITIALIZE_PASS_DEPENDENCY(RegionInfo);
1269INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1270INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001271INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1272 "Polly - Create polyhedral description of Scops", false,
1273 false)