blob: 1521b3bf147762993aece70e20d2eed19f80717e [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 Doerferte58a0122014-06-27 20:31:28 +0000709 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000710}
711
Johannes Doerferte58a0122014-06-27 20:31:28 +0000712/// @brief Collect loads which might form a reduction chain with @p StoreMA
713///
714/// Check if the stored value for @p StoreMA is a binary operator with one or
715/// two loads as operands. If the binary operand is commutative & associative,
716/// used only once (by @p StoreMA) and its load operands are also used only
717/// once, we have found a possible reduction chain. It starts at an operand
718/// load and includes the binary operator and @p StoreMA.
719///
720/// Note: We allow only one use to ensure the load and binary operator cannot
721/// escape this block or into any other store except @p StoreMA.
722void ScopStmt::collectCandiateReductionLoads(
723 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
724 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
725 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000726 return;
727
728 // Skip if there is not one binary operator between the load and the store
729 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +0000730 if (!BinOp)
731 return;
732
733 // Skip if the binary operators has multiple uses
734 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000735 return;
736
737 // Skip if the opcode of the binary operator is not commutative/associative
738 if (!BinOp->isCommutative() || !BinOp->isAssociative())
739 return;
740
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000741 // Skip if it is a multiplicative reduction and we disabled them
742 if (DisableMultiplicativeReductions &&
743 (BinOp->getOpcode() == Instruction::Mul ||
744 BinOp->getOpcode() == Instruction::FMul))
745 return;
746
Johannes Doerferte58a0122014-06-27 20:31:28 +0000747 // Check the binary operator operands for a candidate load
748 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
749 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
750 if (!PossibleLoad0 && !PossibleLoad1)
751 return;
752
753 // A load is only a candidate if it cannot escape (thus has only this use)
754 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
755 Loads.push_back(lookupAccessFor(PossibleLoad0));
756 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
757 Loads.push_back(lookupAccessFor(PossibleLoad1));
758}
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
897 for (MemoryAccessVec::const_iterator I = MemAccs.begin(), E = MemAccs.end();
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000898 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +0000899 (*I)->print(OS);
900}
901
902void ScopStmt::dump() const { print(dbgs()); }
903
904//===----------------------------------------------------------------------===//
905/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +0000906
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000907void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +0000908 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
909 isl_set_free(Context);
910 Context = NewContext;
911}
912
Tobias Grosserabfbe632013-02-05 12:09:06 +0000913void Scop::addParams(std::vector<const SCEV *> NewParameters) {
914 for (std::vector<const SCEV *>::iterator PI = NewParameters.begin(),
915 PE = NewParameters.end();
916 PI != PE; ++PI) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000917 const SCEV *Parameter = *PI;
918
919 if (ParameterIds.find(Parameter) != ParameterIds.end())
920 continue;
921
922 int dimension = Parameters.size();
923
924 Parameters.push_back(Parameter);
925 ParameterIds[Parameter] = dimension;
926 }
927}
928
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000929__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
930 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +0000931
Tobias Grosser9a38ab82011-11-08 15:41:03 +0000932 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000933 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +0000934
Tobias Grosser8f99c162011-11-15 11:38:55 +0000935 std::string ParameterName;
936
937 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
938 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000939 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +0000940 }
941
942 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +0000943 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +0000944
Tobias Grosser20532b82014-04-11 17:56:49 +0000945 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
946 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +0000947}
Tobias Grosser75805372011-04-29 06:27:02 +0000948
Tobias Grosser6be480c2011-11-08 15:41:13 +0000949void Scop::buildContext() {
950 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +0000951 Context = isl_set_universe(isl_space_copy(Space));
952 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +0000953}
954
Tobias Grosser18daaca2012-05-22 10:47:27 +0000955void Scop::addParameterBounds() {
956 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
Tobias Grosseredab1352013-06-21 06:41:31 +0000957 isl_val *V;
Tobias Grosser18daaca2012-05-22 10:47:27 +0000958 isl_id *Id;
959 const SCEV *Scev;
960 const IntegerType *T;
961
962 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserabfbe632013-02-05 12:09:06 +0000963 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000964 T = dyn_cast<IntegerType>(Scev->getType());
965 isl_id_free(Id);
966
967 assert(T && "Not an integer type");
968 int Width = T->getBitWidth();
969
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_neg(V);
973 Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000974
Tobias Grosseredab1352013-06-21 06:41:31 +0000975 V = isl_val_int_from_si(IslCtx, Width - 1);
976 V = isl_val_2exp(V);
977 V = isl_val_sub_ui(V, 1);
978 Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +0000979 }
980}
981
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000982void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +0000983 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +0000984 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +0000985
986 for (ParamIdType::iterator PI = ParameterIds.begin(), PE = ParameterIds.end();
987 PI != PE; ++PI) {
988 const SCEV *Parameter = PI->first;
989 isl_id *id = getIdForParam(Parameter);
990 Space = isl_space_set_dim_id(Space, isl_dim_param, PI->second, id);
991 }
992
993 // Align the parameters of all data structures to the model.
994 Context = isl_set_align_params(Context, Space);
995
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000996 for (iterator I = begin(), E = end(); I != E; ++I)
997 (*I)->realignParams();
998}
999
Tobias Grosser0e27e242011-10-06 00:03:48 +00001000Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
1001 isl_ctx *Context)
Tobias Grosserabfbe632013-02-05 12:09:06 +00001002 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
1003 MaxLoopDepth(tempScop.getMaxLoopDepth()) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001004 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001005 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001006
Tobias Grosserabfbe632013-02-05 12:09:06 +00001007 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001008 SmallVector<unsigned, 8> Scatter;
1009
1010 Scatter.assign(MaxLoopDepth + 1, 0);
1011
1012 // Build the iteration domain, access functions and scattering functions
1013 // traversing the region tree.
1014 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +00001015
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001016 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001017 addParameterBounds();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001018
Tobias Grosser75805372011-04-29 06:27:02 +00001019 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1020}
1021
1022Scop::~Scop() {
1023 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001024 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +00001025
1026 // Free the statements;
1027 for (iterator I = begin(), E = end(); I != E; ++I)
1028 delete *I;
Tobias Grosser75805372011-04-29 06:27:02 +00001029}
1030
Tobias Grosser74394f02013-01-14 22:40:23 +00001031std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser75805372011-04-29 06:27:02 +00001032
1033std::string Scop::getNameStr() const {
1034 std::string ExitName, EntryName;
1035 raw_string_ostream ExitStr(ExitName);
1036 raw_string_ostream EntryStr(EntryName);
1037
Tobias Grosserf240b482014-01-09 10:42:15 +00001038 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001039 EntryStr.str();
1040
1041 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001042 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001043 ExitStr.str();
1044 } else
1045 ExitName = "FunctionExit";
1046
1047 return EntryName + "---" + ExitName;
1048}
1049
Tobias Grosser74394f02013-01-14 22:40:23 +00001050__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001051__isl_give isl_space *Scop::getParamSpace() const {
1052 return isl_set_get_space(this->Context);
1053}
1054
Tobias Grossere86109f2013-10-29 21:05:49 +00001055__isl_give isl_set *Scop::getAssumedContext() const {
1056 return isl_set_copy(AssumedContext);
1057}
1058
Tobias Grosser75805372011-04-29 06:27:02 +00001059void Scop::printContext(raw_ostream &OS) const {
1060 OS << "Context:\n";
1061
1062 if (!Context) {
1063 OS.indent(4) << "n/a\n\n";
1064 return;
1065 }
1066
1067 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001068
1069 for (ParamVecType::const_iterator PI = Parameters.begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001070 PE = Parameters.end();
1071 PI != PE; ++PI) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001072 const SCEV *Parameter = *PI;
1073 int Dim = ParameterIds.find(Parameter)->second;
1074
1075 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1076 }
Tobias Grosser75805372011-04-29 06:27:02 +00001077}
1078
1079void Scop::printStatements(raw_ostream &OS) const {
1080 OS << "Statements {\n";
1081
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001082 for (const_iterator SI = begin(), SE = end(); SI != SE; ++SI)
Tobias Grosser75805372011-04-29 06:27:02 +00001083 OS.indent(4) << (**SI);
1084
1085 OS.indent(4) << "}\n";
1086}
1087
Tobias Grosser75805372011-04-29 06:27:02 +00001088void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001089 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1090 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001091 OS.indent(4) << "Region: " << getNameStr() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001092 printContext(OS.indent(4));
1093 printStatements(OS.indent(4));
1094}
1095
1096void Scop::dump() const { print(dbgs()); }
1097
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001098isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001099
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001100__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001101 isl_union_set *Domain = nullptr;
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001102
1103 for (Scop::iterator SI = begin(), SE = end(); SI != SE; ++SI)
Tobias Grosser3cbe5cf2012-03-08 15:21:51 +00001104 if (!Domain)
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001105 Domain = isl_union_set_from_set((*SI)->getDomain());
1106 else
1107 Domain = isl_union_set_union(Domain,
Tobias Grosserabfbe632013-02-05 12:09:06 +00001108 isl_union_set_from_set((*SI)->getDomain()));
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001109
1110 return Domain;
1111}
1112
Tobias Grosser37eb4222014-02-20 21:43:54 +00001113__isl_give isl_union_map *Scop::getWrites() {
1114 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1115
1116 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1117 ScopStmt *Stmt = *SI;
1118
Johannes Doerfertf6752892014-06-13 18:01:45 +00001119 for (MemoryAccess *MA : *Stmt) {
1120 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001121 continue;
1122
1123 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001124 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001125
1126 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1127 Write = isl_union_map_add_map(Write, AccessDomain);
1128 }
1129 }
1130 return isl_union_map_coalesce(Write);
1131}
1132
1133__isl_give isl_union_map *Scop::getReads() {
1134 isl_union_map *Read = isl_union_map_empty(this->getParamSpace());
1135
1136 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1137 ScopStmt *Stmt = *SI;
1138
Johannes Doerfertf6752892014-06-13 18:01:45 +00001139 for (MemoryAccess *MA : *Stmt) {
1140 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001141 continue;
1142
1143 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001144 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001145
1146 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1147 Read = isl_union_map_add_map(Read, AccessDomain);
1148 }
1149 }
1150 return isl_union_map_coalesce(Read);
1151}
1152
1153__isl_give isl_union_map *Scop::getSchedule() {
1154 isl_union_map *Schedule = isl_union_map_empty(this->getParamSpace());
1155
1156 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1157 ScopStmt *Stmt = *SI;
1158 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
1159 }
1160 return isl_union_map_coalesce(Schedule);
1161}
1162
1163bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1164 bool Changed = false;
1165 for (Scop::iterator SI = this->begin(), SE = this->end(); SI != SE; ++SI) {
1166 ScopStmt *Stmt = *SI;
1167 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
1168
1169 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1170 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1171
1172 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1173 isl_union_set_free(StmtDomain);
1174 isl_union_set_free(NewStmtDomain);
1175 continue;
1176 }
1177
1178 Changed = true;
1179
1180 isl_union_set_free(StmtDomain);
1181 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1182
1183 if (isl_union_set_is_empty(NewStmtDomain)) {
1184 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1185 isl_union_set_free(NewStmtDomain);
1186 } else
1187 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1188 }
1189 isl_union_set_free(Domain);
1190 return Changed;
1191}
1192
Tobias Grosser75805372011-04-29 06:27:02 +00001193ScalarEvolution *Scop::getSE() const { return SE; }
1194
1195bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1196 if (tempScop.getAccessFunctions(BB))
1197 return false;
1198
1199 return true;
1200}
1201
Tobias Grosser74394f02013-01-14 22:40:23 +00001202void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1203 SmallVectorImpl<Loop *> &NestLoops,
1204 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001205 Loop *L = castToLoop(CurRegion, LI);
1206
1207 if (L)
1208 NestLoops.push_back(L);
1209
1210 unsigned loopDepth = NestLoops.size();
1211 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1212
1213 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001214 E = CurRegion.element_end();
1215 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001216 if (I->isSubRegion())
1217 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1218 else {
1219 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1220
1221 if (isTrivialBB(BB, tempScop))
1222 continue;
1223
Tobias Grosserabfbe632013-02-05 12:09:06 +00001224 Stmts.push_back(
1225 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter));
Tobias Grosser75805372011-04-29 06:27:02 +00001226
1227 // Increasing the Scattering function is OK for the moment, because
1228 // we are using a depth first iterator and the program is well structured.
1229 ++Scatter[loopDepth];
1230 }
1231
1232 if (!L)
1233 return;
1234
1235 // Exiting a loop region.
1236 Scatter[loopDepth] = 0;
1237 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001238 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001239}
1240
1241//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001242ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1243 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001244 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001245}
1246
1247ScopInfo::~ScopInfo() {
1248 clear();
1249 isl_ctx_free(ctx);
1250}
1251
Tobias Grosser75805372011-04-29 06:27:02 +00001252void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1253 AU.addRequired<LoopInfo>();
1254 AU.addRequired<RegionInfo>();
1255 AU.addRequired<ScalarEvolution>();
1256 AU.addRequired<TempScopInfo>();
1257 AU.setPreservesAll();
1258}
1259
1260bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1261 LoopInfo &LI = getAnalysis<LoopInfo>();
1262 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1263
1264 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1265
1266 // This region is no Scop.
1267 if (!tempScop) {
1268 scop = 0;
1269 return false;
1270 }
1271
1272 // Statistics.
1273 ++ScopFound;
Tobias Grosser74394f02013-01-14 22:40:23 +00001274 if (tempScop->getMaxLoopDepth() > 0)
1275 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00001276
Tobias Grosserb76f38532011-08-20 11:11:25 +00001277 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001278
1279 return false;
1280}
1281
1282char ScopInfo::ID = 0;
1283
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001284Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1285
Tobias Grosser73600b82011-10-08 00:30:40 +00001286INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1287 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001288 false);
1289INITIALIZE_PASS_DEPENDENCY(LoopInfo);
1290INITIALIZE_PASS_DEPENDENCY(RegionInfo);
1291INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1292INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001293INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1294 "Polly - Create polyhedral description of Scops", false,
1295 false)