blob: 1b85804909fb5bfae1216135ddebc0a47991b4d5 [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//
Tobias Grossera5605d32014-10-29 19:58:28 +000015// This representation is shared among several tools in the polyhedral
Tobias Grosser75805372011-04-29 06:27:02 +000016// community, which are e.g. Cloog, Pluto, Loopo, Graphite.
17//
18//===----------------------------------------------------------------------===//
19
Tobias Grosser75805372011-04-29 06:27:02 +000020#include "polly/LinkAllPasses.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000021#include "polly/ScopInfo.h"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000022#include "polly/Options.h"
Tobias Grosser75805372011-04-29 06:27:02 +000023#include "polly/Support/GICHelper.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000024#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000025#include "polly/Support/ScopHelper.h"
Sebastian Pop27c10c62013-03-22 22:07:43 +000026#include "polly/TempScopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000027#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000028#include "llvm/ADT/Statistic.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000029#include "llvm/ADT/StringExtras.h"
Tobias Grosser83628182013-05-07 08:11:54 +000030#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000031#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser83628182013-05-07 08:11:54 +000032#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 Doerfert9e7b17b2014-08-18 00:40:13 +000058// Multiplicative reductions can be disabled separately as these kind of
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000059// operations can overflow easily. Additive reductions and bit operations
60// are in contrast pretty stable.
Tobias Grosser483a90d2014-07-09 10:50:10 +000061static cl::opt<bool> DisableMultiplicativeReductions(
62 "polly-disable-multiplicative-reductions",
63 cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore,
64 cl::init(false), cl::cat(PollyCategory));
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000065
Johannes Doerfert9143d672014-09-27 11:02:39 +000066static cl::opt<unsigned> RunTimeChecksMaxParameters(
67 "polly-rtc-max-parameters",
68 cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden,
69 cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory));
70
Tobias Grosser0695ee42013-09-17 03:30:31 +000071/// Translate a 'const SCEV *' expression in an isl_pw_aff.
Tobias Grosserabfbe632013-02-05 12:09:06 +000072struct SCEVAffinator : public SCEVVisitor<SCEVAffinator, isl_pw_aff *> {
Tobias Grosser0695ee42013-09-17 03:30:31 +000073public:
Tobias Grosser0695ee42013-09-17 03:30:31 +000074 /// @brief Translate a 'const SCEV *' to an isl_pw_aff.
75 ///
76 /// @param Stmt The location at which the scalar evolution expression
77 /// is evaluated.
78 /// @param Expr The expression that is translated.
79 static __isl_give isl_pw_aff *getPwAff(ScopStmt *Stmt, const SCEV *Expr);
80
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000081private:
Tobias Grosser3cc99742012-06-06 16:33:15 +000082 isl_ctx *Ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +000083 int NbLoopSpaces;
Tobias Grosser3cc99742012-06-06 16:33:15 +000084 const Scop *S;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000085
Tobias Grosser0695ee42013-09-17 03:30:31 +000086 SCEVAffinator(const ScopStmt *Stmt);
87 int getLoopDepth(const Loop *L);
Tobias Grosser60b54f12011-11-08 15:41:28 +000088
Tobias Grosser0695ee42013-09-17 03:30:31 +000089 __isl_give isl_pw_aff *visit(const SCEV *Expr);
90 __isl_give isl_pw_aff *visitConstant(const SCEVConstant *Expr);
91 __isl_give isl_pw_aff *visitTruncateExpr(const SCEVTruncateExpr *Expr);
92 __isl_give isl_pw_aff *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr);
93 __isl_give isl_pw_aff *visitSignExtendExpr(const SCEVSignExtendExpr *Expr);
94 __isl_give isl_pw_aff *visitAddExpr(const SCEVAddExpr *Expr);
95 __isl_give isl_pw_aff *visitMulExpr(const SCEVMulExpr *Expr);
96 __isl_give isl_pw_aff *visitUDivExpr(const SCEVUDivExpr *Expr);
97 __isl_give isl_pw_aff *visitAddRecExpr(const SCEVAddRecExpr *Expr);
98 __isl_give isl_pw_aff *visitSMaxExpr(const SCEVSMaxExpr *Expr);
99 __isl_give isl_pw_aff *visitUMaxExpr(const SCEVUMaxExpr *Expr);
100 __isl_give isl_pw_aff *visitUnknown(const SCEVUnknown *Expr);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000101
Tobias Grosser0695ee42013-09-17 03:30:31 +0000102 friend struct SCEVVisitor<SCEVAffinator, isl_pw_aff *>;
103};
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000104
Tobias Grosser0695ee42013-09-17 03:30:31 +0000105SCEVAffinator::SCEVAffinator(const ScopStmt *Stmt)
106 : Ctx(Stmt->getIslCtx()), NbLoopSpaces(Stmt->getNumIterators()),
107 S(Stmt->getParent()) {}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000108
Tobias Grosser0695ee42013-09-17 03:30:31 +0000109__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt,
110 const SCEV *Scev) {
111 Scop *S = Stmt->getParent();
112 const Region *Reg = &S->getRegion();
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000113
Tobias Grosser0695ee42013-09-17 03:30:31 +0000114 S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE()));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000115
Tobias Grosser0695ee42013-09-17 03:30:31 +0000116 SCEVAffinator Affinator(Stmt);
117 return Affinator.visit(Scev);
118}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000119
Tobias Grosser0695ee42013-09-17 03:30:31 +0000120__isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
121 // In case the scev is a valid parameter, we do not further analyze this
122 // expression, but create a new parameter in the isl_pw_aff. This allows us
123 // to treat subexpressions that we cannot translate into an piecewise affine
124 // expression, as constant parameters of the piecewise affine expression.
125 if (isl_id *Id = S->getIdForParam(Expr)) {
126 isl_space *Space = isl_space_set_alloc(Ctx, 1, NbLoopSpaces);
127 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000128
Tobias Grosser0695ee42013-09-17 03:30:31 +0000129 isl_set *Domain = isl_set_universe(isl_space_copy(Space));
130 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
131 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000132
133 return isl_pw_aff_alloc(Domain, Affine);
134 }
135
Tobias Grosser0695ee42013-09-17 03:30:31 +0000136 return SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
137}
138
Tobias Grosser0d170132013-10-03 13:09:19 +0000139__isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
Tobias Grosser0695ee42013-09-17 03:30:31 +0000140 ConstantInt *Value = Expr->getValue();
141 isl_val *v;
142
143 // LLVM does not define if an integer value is interpreted as a signed or
144 // unsigned value. Hence, without further information, it is unknown how
145 // this value needs to be converted to GMP. At the moment, we only support
146 // signed operations. So we just interpret it as signed. Later, there are
147 // two options:
148 //
149 // 1. We always interpret any value as signed and convert the values on
150 // demand.
151 // 2. We pass down the signedness of the calculation and use it to interpret
152 // this constant correctly.
153 v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
154
155 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
Johannes Doerfert9c147372014-11-19 15:36:59 +0000156 isl_local_space *ls = isl_local_space_from_space(Space);
157 return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v));
Tobias Grosser0695ee42013-09-17 03:30:31 +0000158}
159
160__isl_give isl_pw_aff *
161SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
162 llvm_unreachable("SCEVTruncateExpr not yet supported");
163}
164
165__isl_give isl_pw_aff *
166SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
167 llvm_unreachable("SCEVZeroExtendExpr not yet supported");
168}
169
170__isl_give isl_pw_aff *
171SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
172 // Assuming the value is signed, a sign extension is basically a noop.
173 // TODO: Reconsider this as soon as we support unsigned values.
174 return visit(Expr->getOperand());
175}
176
177__isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
178 isl_pw_aff *Sum = visit(Expr->getOperand(0));
179
180 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
181 isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
182 Sum = isl_pw_aff_add(Sum, NextSummand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000183 }
184
Tobias Grosser0695ee42013-09-17 03:30:31 +0000185 // TODO: Check for NSW and NUW.
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000186
Tobias Grosser0695ee42013-09-17 03:30:31 +0000187 return Sum;
188}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000189
Tobias Grosser0695ee42013-09-17 03:30:31 +0000190__isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
191 isl_pw_aff *Product = visit(Expr->getOperand(0));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000192
Tobias Grosser0695ee42013-09-17 03:30:31 +0000193 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
194 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
195
196 if (!isl_pw_aff_is_cst(Product) && !isl_pw_aff_is_cst(NextOperand)) {
197 isl_pw_aff_free(Product);
198 isl_pw_aff_free(NextOperand);
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000199 return nullptr;
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000200 }
201
Tobias Grosser0695ee42013-09-17 03:30:31 +0000202 Product = isl_pw_aff_mul(Product, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000203 }
204
Tobias Grosser0695ee42013-09-17 03:30:31 +0000205 // TODO: Check for NSW and NUW.
206 return Product;
207}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000208
Tobias Grosser0695ee42013-09-17 03:30:31 +0000209__isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
210 llvm_unreachable("SCEVUDivExpr not yet supported");
211}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000212
Tobias Grosser0695ee42013-09-17 03:30:31 +0000213__isl_give isl_pw_aff *
214SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
215 assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000216
Tobias Grosser0695ee42013-09-17 03:30:31 +0000217 // Directly generate isl_pw_aff for Expr if 'start' is zero.
218 if (Expr->getStart()->isZero()) {
219 assert(S->getRegion().contains(Expr->getLoop()) &&
220 "Scop does not contain the loop referenced in this AddRec");
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000221
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000222 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser0695ee42013-09-17 03:30:31 +0000223 isl_pw_aff *Step = visit(Expr->getOperand(1));
224 isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
225 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000226
Tobias Grosser0695ee42013-09-17 03:30:31 +0000227 int loopDimension = getLoopDepth(Expr->getLoop());
228
229 isl_aff *LAff = isl_aff_set_coefficient_si(
230 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
231 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
232
233 // TODO: Do we need to check for NSW and NUW?
234 return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff));
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000235 }
236
Tobias Grosser0695ee42013-09-17 03:30:31 +0000237 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
238 // if 'start' is not zero.
239 ScalarEvolution &SE = *S->getSE();
240 const SCEV *ZeroStartExpr = SE.getAddRecExpr(
241 SE.getConstant(Expr->getStart()->getType(), 0),
242 Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000243
Tobias Grosser0695ee42013-09-17 03:30:31 +0000244 isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
245 isl_pw_aff *Start = visit(Expr->getStart());
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000246
Tobias Grosser0695ee42013-09-17 03:30:31 +0000247 return isl_pw_aff_add(ZeroStartResult, Start);
248}
249
250__isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
251 isl_pw_aff *Max = visit(Expr->getOperand(0));
252
253 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
254 isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
255 Max = isl_pw_aff_max(Max, NextOperand);
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000256 }
257
Tobias Grosser0695ee42013-09-17 03:30:31 +0000258 return Max;
259}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000260
Tobias Grosser0695ee42013-09-17 03:30:31 +0000261__isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
262 llvm_unreachable("SCEVUMaxExpr not yet supported");
263}
264
265__isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosserf4daf342014-08-16 09:08:55 +0000266 llvm_unreachable("Unknowns are always parameters");
Tobias Grosser0695ee42013-09-17 03:30:31 +0000267}
268
269int SCEVAffinator::getLoopDepth(const Loop *L) {
270 Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L));
271 assert(outerLoop && "Scop does not contain this loop");
272 return L->getLoopDepth() - outerLoop->getLoopDepth();
273}
Tobias Grosser33ba62ad2011-08-18 06:31:50 +0000274
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000275ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *AccessType, isl_ctx *Ctx,
276 const SmallVector<const SCEV *, 4> &DimensionSizes)
277 : BasePtr(BasePtr), AccessType(AccessType), DimensionSizes(DimensionSizes) {
278 const std::string BasePtrName = getIslCompatibleName("MemRef_", BasePtr, "");
279 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
280}
281
282ScopArrayInfo::~ScopArrayInfo() { isl_id_free(Id); }
283
284isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
285
286void ScopArrayInfo::dump() const { print(errs()); }
287
288void ScopArrayInfo::print(raw_ostream &OS) const {
289 OS << "ScopArrayInfo:\n";
290 OS << " Base: " << *getBasePtr() << "\n";
291 OS << " Type: " << *getType() << "\n";
292 OS << " Dimension Sizes:\n";
293 for (unsigned u = 0; u < getNumberOfDimensions(); u++)
294 OS << " " << u << ") " << *DimensionSizes[u] << "\n";
295 OS << "\n";
296}
297
298const ScopArrayInfo *
299ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
300 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
301 assert(Id && "Output dimension didn't have an ID");
302 return getFromId(Id);
303}
304
305const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
306 void *User = isl_id_get_user(Id);
307 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
308 isl_id_free(Id);
309 return SAI;
310}
311
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000312const std::string
313MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
314 switch (RT) {
315 case MemoryAccess::RT_NONE:
316 llvm_unreachable("Requested a reduction operator string for a memory "
317 "access which isn't a reduction");
318 case MemoryAccess::RT_ADD:
319 return "+";
320 case MemoryAccess::RT_MUL:
321 return "*";
322 case MemoryAccess::RT_BOR:
323 return "|";
324 case MemoryAccess::RT_BXOR:
325 return "^";
326 case MemoryAccess::RT_BAND:
327 return "&";
328 }
329 llvm_unreachable("Unknown reduction type");
330 return "";
331}
332
Johannes Doerfertf6183392014-07-01 20:52:51 +0000333/// @brief Return the reduction type for a given binary operator
334static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
335 const Instruction *Load) {
336 if (!BinOp)
337 return MemoryAccess::RT_NONE;
338 switch (BinOp->getOpcode()) {
339 case Instruction::FAdd:
340 if (!BinOp->hasUnsafeAlgebra())
341 return MemoryAccess::RT_NONE;
342 // Fall through
343 case Instruction::Add:
344 return MemoryAccess::RT_ADD;
345 case Instruction::Or:
346 return MemoryAccess::RT_BOR;
347 case Instruction::Xor:
348 return MemoryAccess::RT_BXOR;
349 case Instruction::And:
350 return MemoryAccess::RT_BAND;
351 case Instruction::FMul:
352 if (!BinOp->hasUnsafeAlgebra())
353 return MemoryAccess::RT_NONE;
354 // Fall through
355 case Instruction::Mul:
356 if (DisableMultiplicativeReductions)
357 return MemoryAccess::RT_NONE;
358 return MemoryAccess::RT_MUL;
359 default:
360 return MemoryAccess::RT_NONE;
361 }
362}
Tobias Grosser75805372011-04-29 06:27:02 +0000363//===----------------------------------------------------------------------===//
364
365MemoryAccess::~MemoryAccess() {
Tobias Grosser54a86e62011-08-18 06:31:46 +0000366 isl_map_free(AccessRelation);
Raghesh Aloor129e8672011-08-15 02:33:39 +0000367 isl_map_free(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000368}
369
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000370static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
371 switch (Access.getType()) {
372 case IRAccess::READ:
373 return MemoryAccess::READ;
374 case IRAccess::MUST_WRITE:
375 return MemoryAccess::MUST_WRITE;
376 case IRAccess::MAY_WRITE:
377 return MemoryAccess::MAY_WRITE;
378 }
379 llvm_unreachable("Unknown IRAccess type!");
380}
381
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000382const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
383 isl_id *ArrayId = getArrayId();
384 void *User = isl_id_get_user(ArrayId);
385 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
386 isl_id_free(ArrayId);
387 return SAI;
388}
389
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000390isl_id *MemoryAccess::getArrayId() const {
391 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
392}
393
Johannes Doerferta99130f2014-10-13 12:58:03 +0000394isl_pw_multi_aff *
395MemoryAccess::applyScheduleToAccessRelation(isl_union_map *USchedule) const {
396 isl_map *Schedule, *ScheduledAccRel;
397 isl_union_set *UDomain;
398
399 UDomain = isl_union_set_from_set(getStatement()->getDomain());
400 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
401 Schedule = isl_map_from_union_map(USchedule);
402 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
403 return isl_pw_multi_aff_from_map(ScheduledAccRel);
404}
405
406isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000407 return isl_map_copy(AccessRelation);
408}
409
Johannes Doerferta99130f2014-10-13 12:58:03 +0000410std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000411 return stringFromIslObj(AccessRelation);
412}
413
Johannes Doerferta99130f2014-10-13 12:58:03 +0000414__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000415 return isl_map_get_space(AccessRelation);
416}
417
Tobias Grosser5d453812011-10-06 00:04:11 +0000418isl_map *MemoryAccess::getNewAccessRelation() const {
419 return isl_map_copy(newAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000420}
421
422isl_basic_map *MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000423 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000424 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000425
Tobias Grosser084d8f72012-05-29 09:29:44 +0000426 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000427 isl_basic_set_universe(Statement->getDomainSpace()),
428 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000429}
430
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000431// Formalize no out-of-bound access assumption
432//
433// When delinearizing array accesses we optimistically assume that the
434// delinearized accesses do not access out of bound locations (the subscript
435// expression of each array evaluates for each statement instance that is
436// executed to a value that is larger than zero and strictly smaller than the
437// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000438// dimension for which we do not need to assume any upper bound. At this point
439// we formalize this assumption to ensure that at code generation time the
440// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000441//
442// To find the set of constraints necessary to avoid out of bound accesses, we
443// first build the set of data locations that are not within array bounds. We
444// then apply the reverse access relation to obtain the set of iterations that
445// may contain invalid accesses and reduce this set of iterations to the ones
446// that are actually executed by intersecting them with the domain of the
447// statement. If we now project out all loop dimensions, we obtain a set of
448// parameters that may cause statement instances to be executed that may
449// possibly yield out of bound memory accesses. The complement of these
450// constraints is the set of constraints that needs to be assumed to ensure such
451// statement instances are never executed.
452void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000453 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000454 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000455 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000456 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
457 isl_pw_aff *Var =
458 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
459 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
460
461 isl_set *DimOutside;
462
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000463 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
464 isl_pw_aff *SizeE = SCEVAffinator::getPwAff(Statement, Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000465
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000466 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
467 Statement->getNumIterators());
468 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
469 isl_space_dim(Space, isl_dim_set));
470 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
471 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000472
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000473 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000474
475 Outside = isl_set_union(Outside, DimOutside);
476 }
477
478 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
479 Outside = isl_set_intersect(Outside, Statement->getDomain());
480 Outside = isl_set_params(Outside);
481 Outside = isl_set_complement(Outside);
482 Statement->getParent()->addAssumption(Outside);
483 isl_space_free(Space);
484}
485
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000486MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000487 ScopStmt *Statement, const ScopArrayInfo *SAI)
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000488 : AccType(getMemoryAccessType(Access)), Statement(Statement), Inst(AccInst),
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000489 newAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000490
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000491 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000492 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000493 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000494
495 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000496
Tobias Grossera1879642011-12-20 10:43:14 +0000497 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000498 // We overapproximate non-affine accesses with a possible access to the
499 // whole array. For read accesses it does not make a difference, if an
500 // access must or may happen. However, for write accesses it is important to
501 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000502 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000503 AccessRelation =
504 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Tobias Grossera1879642011-12-20 10:43:14 +0000505 return;
506 }
507
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000508 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000509 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000510
Tobias Grosser79baa212014-04-10 08:38:02 +0000511 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Sebastian Pop18016682014-04-08 21:20:44 +0000512 isl_pw_aff *Affine =
513 SCEVAffinator::getPwAff(Statement, Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000514
Sebastian Pop422e33f2014-06-03 18:16:31 +0000515 if (Size == 1) {
516 // For the non delinearized arrays, divide the access function of the last
517 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000518 //
519 // A stride one array access in C expressed as A[i] is expressed in
520 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
521 // two subsequent values of 'i' index two values that are stored next to
522 // each other in memory. By this division we make this characteristic
523 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000524 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000525 Affine = isl_pw_aff_scale_down_val(Affine, v);
526 }
527
528 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
529
Tobias Grosser79baa212014-04-10 08:38:02 +0000530 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000531 }
532
Tobias Grosser79baa212014-04-10 08:38:02 +0000533 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000534 AccessRelation = isl_map_set_tuple_id(
535 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000536 AccessRelation =
537 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
538
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000539 assumeNoOutOfBound(Access);
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000540 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000541}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000542
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000543void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000544 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000545 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000546}
547
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000548const std::string MemoryAccess::getReductionOperatorStr() const {
549 return MemoryAccess::getReductionOperatorStr(getReductionType());
550}
551
Johannes Doerfertf6183392014-07-01 20:52:51 +0000552raw_ostream &polly::operator<<(raw_ostream &OS,
553 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000554 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000555 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000556 else
557 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000558 return OS;
559}
560
Tobias Grosser75805372011-04-29 06:27:02 +0000561void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000562 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000563 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000564 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000565 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000566 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000567 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000568 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000569 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000570 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000571 break;
572 }
Johannes Doerfertf6183392014-07-01 20:52:51 +0000573 OS << "[Reduction Type: " << getReductionType() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000574 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000575}
576
Tobias Grosser74394f02013-01-14 22:40:23 +0000577void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000578
579// Create a map in the size of the provided set domain, that maps from the
580// one element of the provided set domain to another element of the provided
581// set domain.
582// The mapping is limited to all points that are equal in all but the last
583// dimension and for which the last dimension of the input is strict smaller
584// than the last dimension of the output.
585//
586// getEqualAndLarger(set[i0, i1, ..., iX]):
587//
588// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
589// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
590//
Tobias Grosserf5338802011-10-06 00:03:35 +0000591static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000592 isl_space *Space = isl_space_map_from_set(setDomain);
593 isl_map *Map = isl_map_universe(isl_space_copy(Space));
594 isl_local_space *MapLocalSpace = isl_local_space_from_space(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000595 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000596
597 // Set all but the last dimension to be equal for the input and output
598 //
599 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
600 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000601 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000602 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000603
604 // Set the last dimension of the input to be strict smaller than the
605 // last dimension of the output.
606 //
607 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
608 //
Tobias Grosseredab1352013-06-21 06:41:31 +0000609 isl_val *v;
610 isl_ctx *Ctx = isl_map_get_ctx(Map);
Tobias Grosserf5338802011-10-06 00:03:35 +0000611 isl_constraint *c = isl_inequality_alloc(isl_local_space_copy(MapLocalSpace));
Tobias Grosseredab1352013-06-21 06:41:31 +0000612 v = isl_val_int_from_si(Ctx, -1);
613 c = isl_constraint_set_coefficient_val(c, isl_dim_in, lastDimension, v);
614 v = isl_val_int_from_si(Ctx, 1);
615 c = isl_constraint_set_coefficient_val(c, isl_dim_out, lastDimension, v);
616 v = isl_val_int_from_si(Ctx, -1);
617 c = isl_constraint_set_constant_val(c, v);
Tobias Grosser75805372011-04-29 06:27:02 +0000618
Tobias Grosserc327932c2012-02-01 14:23:36 +0000619 Map = isl_map_add_constraint(Map, c);
Tobias Grosser75805372011-04-29 06:27:02 +0000620
Tobias Grosser23b36662011-10-17 08:32:36 +0000621 isl_local_space_free(MapLocalSpace);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000622 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000623}
624
Sebastian Popa00a0292012-12-18 07:46:06 +0000625isl_set *MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000626 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000627 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000628 isl_space *Space = isl_space_range(isl_map_get_space(S));
629 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000630
Sebastian Popa00a0292012-12-18 07:46:06 +0000631 S = isl_map_reverse(S);
632 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000633
Sebastian Popa00a0292012-12-18 07:46:06 +0000634 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
635 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
636 NextScatt = isl_map_apply_domain(NextScatt, S);
637 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000638
Sebastian Popa00a0292012-12-18 07:46:06 +0000639 isl_set *Deltas = isl_map_deltas(NextScatt);
640 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000641}
642
Sebastian Popa00a0292012-12-18 07:46:06 +0000643bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000644 int StrideWidth) const {
645 isl_set *Stride, *StrideX;
646 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000647
Sebastian Popa00a0292012-12-18 07:46:06 +0000648 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000649 StrideX = isl_set_universe(isl_set_get_space(Stride));
650 StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth);
651 IsStrideX = isl_set_is_equal(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000652
Tobias Grosser28dd4862012-01-24 16:42:16 +0000653 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000654 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000655
Tobias Grosser28dd4862012-01-24 16:42:16 +0000656 return IsStrideX;
657}
658
Sebastian Popa00a0292012-12-18 07:46:06 +0000659bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
660 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000661}
662
Tobias Grosser79baa212014-04-10 08:38:02 +0000663bool MemoryAccess::isScalar() const {
664 return isl_map_n_out(AccessRelation) == 0;
665}
666
Sebastian Popa00a0292012-12-18 07:46:06 +0000667bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
668 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000669}
670
Tobias Grosser5d453812011-10-06 00:04:11 +0000671void MemoryAccess::setNewAccessRelation(isl_map *newAccess) {
Tobias Grosserb76f38532011-08-20 11:11:25 +0000672 isl_map_free(newAccessRelation);
Raghesh Aloor7a04f4f2011-08-03 13:47:59 +0000673 newAccessRelation = newAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000674}
Tobias Grosser75805372011-04-29 06:27:02 +0000675
676//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000677
Tobias Grosser74394f02013-01-14 22:40:23 +0000678isl_map *ScopStmt::getScattering() const { return isl_map_copy(Scattering); }
Tobias Grossercf3942d2011-10-06 00:04:05 +0000679
Tobias Grosser37eb4222014-02-20 21:43:54 +0000680void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
681 assert(isl_set_is_subset(NewDomain, Domain) &&
682 "New domain is not a subset of old domain!");
683 isl_set_free(Domain);
684 Domain = NewDomain;
685 Scattering = isl_map_intersect_domain(Scattering, isl_set_copy(Domain));
686}
687
Tobias Grossercf3942d2011-10-06 00:04:05 +0000688void ScopStmt::setScattering(isl_map *NewScattering) {
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000689 assert(NewScattering && "New scattering is nullptr");
Tobias Grosserb76f38532011-08-20 11:11:25 +0000690 isl_map_free(Scattering);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000691 Scattering = NewScattering;
Tobias Grosserb76f38532011-08-20 11:11:25 +0000692}
693
Tobias Grosser75805372011-04-29 06:27:02 +0000694void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000695 unsigned NbIterators = getNumIterators();
696 unsigned NbScatteringDims = Parent.getMaxLoopDepth() * 2 + 1;
697
Tobias Grosser084d8f72012-05-29 09:29:44 +0000698 isl_space *Space = isl_space_set_alloc(getIslCtx(), 0, NbScatteringDims);
Tobias Grosserf5338802011-10-06 00:03:35 +0000699 Space = isl_space_set_tuple_name(Space, isl_dim_out, "scattering");
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000700
Tobias Grosser084d8f72012-05-29 09:29:44 +0000701 Scattering = isl_map_from_domain_and_range(isl_set_universe(getDomainSpace()),
702 isl_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000703
704 // Loop dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000705 for (unsigned i = 0; i < NbIterators; ++i)
Tobias Grosserabfbe632013-02-05 12:09:06 +0000706 Scattering =
707 isl_map_equate(Scattering, isl_dim_out, 2 * i + 1, isl_dim_in, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000708
709 // Constant dimensions
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000710 for (unsigned i = 0; i < NbIterators + 1; ++i)
711 Scattering = isl_map_fix_si(Scattering, isl_dim_out, 2 * i, Scatter[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000712
713 // Fill scattering dimensions.
Tobias Grosser78d8a3d2012-01-17 20:34:23 +0000714 for (unsigned i = 2 * NbIterators + 1; i < NbScatteringDims; ++i)
715 Scattering = isl_map_fix_si(Scattering, isl_dim_out, i, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000716
Tobias Grosser37487052011-10-06 00:03:42 +0000717 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000718}
719
Johannes Doerfert75bd66e2014-10-31 23:16:02 +0000720void ScopStmt::buildAccesses(TempScop &tempScop) {
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000721 for (const auto &AccessPair : *tempScop.getAccessFunctions(BB)) {
722 const IRAccess &Access = AccessPair.first;
723 Instruction *AccessInst = AccessPair.second;
724
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000725 Type *AccessType = getAccessInstType(AccessInst)->getPointerTo();
726 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
727 Access.getBase(), AccessType, Access.Sizes);
728
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000729 MemAccs.push_back(new MemoryAccess(Access, AccessInst, this, SAI));
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000730
731 // We do not track locations for scalar memory accesses at the moment.
732 //
733 // We do not have a use for this information at the moment. If we need this
734 // at some point, the "instruction -> access" mapping needs to be enhanced
735 // as a single instruction could then possibly perform multiple accesses.
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000736 if (!Access.isScalar()) {
737 assert(!InstructionToAccess.count(AccessInst) &&
Tobias Grosser3fc91542014-02-20 21:43:45 +0000738 "Unexpected 1-to-N mapping on instruction to access map!");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000739 InstructionToAccess[AccessInst] = MemAccs.back();
Tobias Grosserd6aafa72014-02-20 21:29:09 +0000740 }
Tobias Grosser75805372011-04-29 06:27:02 +0000741 }
742}
743
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000744void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000745 for (MemoryAccess *MA : *this)
746 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000747
748 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
749 Scattering = isl_map_align_params(Scattering, Parent.getParamSpace());
750}
751
Tobias Grosser65b00582011-11-08 15:41:19 +0000752__isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) {
Tobias Grossera601fbd2011-11-09 22:34:44 +0000753 isl_pw_aff *L = SCEVAffinator::getPwAff(this, Comp.getLHS());
754 isl_pw_aff *R = SCEVAffinator::getPwAff(this, Comp.getRHS());
Tobias Grosser75805372011-04-29 06:27:02 +0000755
Tobias Grosserd2795d02011-08-18 07:51:40 +0000756 switch (Comp.getPred()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000757 case ICmpInst::ICMP_EQ:
Tobias Grosser048c8792011-10-23 20:59:20 +0000758 return isl_pw_aff_eq_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000759 case ICmpInst::ICMP_NE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000760 return isl_pw_aff_ne_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000761 case ICmpInst::ICMP_SLT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000762 return isl_pw_aff_lt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000763 case ICmpInst::ICMP_SLE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000764 return isl_pw_aff_le_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000765 case ICmpInst::ICMP_SGT:
Tobias Grosser048c8792011-10-23 20:59:20 +0000766 return isl_pw_aff_gt_set(L, R);
Tobias Grosser75805372011-04-29 06:27:02 +0000767 case ICmpInst::ICMP_SGE:
Tobias Grosser048c8792011-10-23 20:59:20 +0000768 return isl_pw_aff_ge_set(L, R);
Tobias Grosserd2795d02011-08-18 07:51:40 +0000769 case ICmpInst::ICMP_ULT:
770 case ICmpInst::ICMP_UGT:
771 case ICmpInst::ICMP_ULE:
Tobias Grosser75805372011-04-29 06:27:02 +0000772 case ICmpInst::ICMP_UGE:
Tobias Grosserd2795d02011-08-18 07:51:40 +0000773 llvm_unreachable("Unsigned comparisons not yet supported");
Tobias Grosser75805372011-04-29 06:27:02 +0000774 default:
775 llvm_unreachable("Non integer predicate not supported");
776 }
Tobias Grosser75805372011-04-29 06:27:02 +0000777}
778
Tobias Grossere19661e2011-10-07 08:46:57 +0000779__isl_give isl_set *ScopStmt::addLoopBoundsToDomain(__isl_take isl_set *Domain,
Tobias Grosser60b54f12011-11-08 15:41:28 +0000780 TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000781 isl_space *Space;
782 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000783
Tobias Grossere19661e2011-10-07 08:46:57 +0000784 Space = isl_set_get_space(Domain);
785 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000786
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000787 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000788 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000789 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000790 isl_pw_aff *IV =
791 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000792
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000793 // 0 <= IV.
794 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
795 Domain = isl_set_intersect(Domain, LowerBound);
796
797 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000798 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000799 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000800 isl_pw_aff *UpperBound = SCEVAffinator::getPwAff(this, LatchExecutions);
801 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
Tobias Grosser75805372011-04-29 06:27:02 +0000802 Domain = isl_set_intersect(Domain, UpperBoundSet);
803 }
804
Tobias Grosserf5338802011-10-06 00:03:35 +0000805 isl_local_space_free(LocalSpace);
Tobias Grossere19661e2011-10-07 08:46:57 +0000806 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000807}
808
Tobias Grossere602a072013-05-07 07:30:56 +0000809__isl_give isl_set *ScopStmt::addConditionsToDomain(__isl_take isl_set *Domain,
810 TempScop &tempScop,
811 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000812 const Region *TopRegion = tempScop.getMaxRegion().getParent(),
Tobias Grosserd7e58642013-04-10 06:55:45 +0000813 *CurrentRegion = &CurRegion;
Tobias Grossere19661e2011-10-07 08:46:57 +0000814 const BasicBlock *BranchingBB = BB;
Tobias Grosser75805372011-04-29 06:27:02 +0000815
Tobias Grosser75805372011-04-29 06:27:02 +0000816 do {
Tobias Grossere19661e2011-10-07 08:46:57 +0000817 if (BranchingBB != CurrentRegion->getEntry()) {
818 if (const BBCond *Condition = tempScop.getBBCond(BranchingBB))
Tobias Grosser083d3d32014-06-28 08:59:45 +0000819 for (const auto &C : *Condition) {
820 isl_set *ConditionSet = buildConditionSet(C);
Tobias Grossere19661e2011-10-07 08:46:57 +0000821 Domain = isl_set_intersect(Domain, ConditionSet);
Tobias Grosser75805372011-04-29 06:27:02 +0000822 }
823 }
Tobias Grossere19661e2011-10-07 08:46:57 +0000824 BranchingBB = CurrentRegion->getEntry();
825 CurrentRegion = CurrentRegion->getParent();
826 } while (TopRegion != CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000827
Tobias Grossere19661e2011-10-07 08:46:57 +0000828 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000829}
830
Tobias Grossere602a072013-05-07 07:30:56 +0000831__isl_give isl_set *ScopStmt::buildDomain(TempScop &tempScop,
832 const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000833 isl_space *Space;
834 isl_set *Domain;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000835 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000836
837 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
838
Tobias Grosser084d8f72012-05-29 09:29:44 +0000839 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
840
Tobias Grossere19661e2011-10-07 08:46:57 +0000841 Domain = isl_set_universe(Space);
Tobias Grossere19661e2011-10-07 08:46:57 +0000842 Domain = addLoopBoundsToDomain(Domain, tempScop);
843 Domain = addConditionsToDomain(Domain, tempScop, CurRegion);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000844 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grossere19661e2011-10-07 08:46:57 +0000845
846 return Domain;
Tobias Grosser75805372011-04-29 06:27:02 +0000847}
848
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000849void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
850 int Dimension = 0;
851 isl_ctx *Ctx = Parent.getIslCtx();
852 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
853 Type *Ty = GEP->getPointerOperandType();
854 ScalarEvolution &SE = *Parent.getSE();
855
856 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
857 Dimension = 1;
858 Ty = PtrTy->getElementType();
859 }
860
861 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
862 unsigned int Operand = 1 + Dimension;
863
864 if (GEP->getNumOperands() <= Operand)
865 break;
866
867 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
868
869 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
870 isl_pw_aff *AccessOffset = SCEVAffinator::getPwAff(this, Expr);
871 AccessOffset =
872 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
873
874 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
875 isl_local_space_copy(LSpace),
876 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
877
878 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
879 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
880 OutOfBound = isl_set_params(OutOfBound);
881 isl_set *InBound = isl_set_complement(OutOfBound);
882 isl_set *Executed = isl_set_params(getDomain());
883
884 // A => B == !A or B
885 isl_set *InBoundIfExecuted =
886 isl_set_union(isl_set_complement(Executed), InBound);
887
888 Parent.addAssumption(InBoundIfExecuted);
889 }
890
891 Dimension += 1;
892 Ty = ArrayTy->getElementType();
893 }
894
895 isl_local_space_free(LSpace);
896}
897
898void ScopStmt::deriveAssumptions() {
899 for (Instruction &Inst : *BB)
900 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
901 deriveAssumptionsFromGEP(GEP);
902}
903
Tobias Grosser74394f02013-01-14 22:40:23 +0000904ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Sebastian Pop860e0212013-02-15 21:26:44 +0000905 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest,
Tobias Grosser75805372011-04-29 06:27:02 +0000906 SmallVectorImpl<unsigned> &Scatter)
Tobias Grosser683b8e42014-11-30 14:33:31 +0000907 : Parent(parent), BB(&bb), NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000908 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +0000909 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +0000910 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +0000911
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000912 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +0000913
Tobias Grossere19661e2011-10-07 08:46:57 +0000914 Domain = buildDomain(tempScop, CurRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000915 buildScattering(Scatter);
Johannes Doerfert75bd66e2014-10-31 23:16:02 +0000916 buildAccesses(tempScop);
Johannes Doerferte58a0122014-06-27 20:31:28 +0000917 checkForReductions();
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000918 deriveAssumptions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000919}
920
Johannes Doerferte58a0122014-06-27 20:31:28 +0000921/// @brief Collect loads which might form a reduction chain with @p StoreMA
922///
923/// Check if the stored value for @p StoreMA is a binary operator with one or
924/// two loads as operands. If the binary operand is commutative & associative,
925/// used only once (by @p StoreMA) and its load operands are also used only
926/// once, we have found a possible reduction chain. It starts at an operand
927/// load and includes the binary operator and @p StoreMA.
928///
929/// Note: We allow only one use to ensure the load and binary operator cannot
930/// escape this block or into any other store except @p StoreMA.
931void ScopStmt::collectCandiateReductionLoads(
932 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
933 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
934 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000935 return;
936
937 // Skip if there is not one binary operator between the load and the store
938 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +0000939 if (!BinOp)
940 return;
941
942 // Skip if the binary operators has multiple uses
943 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000944 return;
945
946 // Skip if the opcode of the binary operator is not commutative/associative
947 if (!BinOp->isCommutative() || !BinOp->isAssociative())
948 return;
949
Johannes Doerfert9890a052014-07-01 00:32:29 +0000950 // Skip if the binary operator is outside the current SCoP
951 if (BinOp->getParent() != Store->getParent())
952 return;
953
Johannes Doerfert0ee1f212014-06-17 17:31:36 +0000954 // Skip if it is a multiplicative reduction and we disabled them
955 if (DisableMultiplicativeReductions &&
956 (BinOp->getOpcode() == Instruction::Mul ||
957 BinOp->getOpcode() == Instruction::FMul))
958 return;
959
Johannes Doerferte58a0122014-06-27 20:31:28 +0000960 // Check the binary operator operands for a candidate load
961 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
962 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
963 if (!PossibleLoad0 && !PossibleLoad1)
964 return;
965
966 // A load is only a candidate if it cannot escape (thus has only this use)
967 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000968 if (PossibleLoad0->getParent() == Store->getParent())
969 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000970 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +0000971 if (PossibleLoad1->getParent() == Store->getParent())
972 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +0000973}
974
975/// @brief Check for reductions in this ScopStmt
976///
977/// Iterate over all store memory accesses and check for valid binary reduction
978/// like chains. For all candidates we check if they have the same base address
979/// and there are no other accesses which overlap with them. The base address
980/// check rules out impossible reductions candidates early. The overlap check,
981/// together with the "only one user" check in collectCandiateReductionLoads,
982/// guarantees that none of the intermediate results will escape during
983/// execution of the loop nest. We basically check here that no other memory
984/// access can access the same memory as the potential reduction.
985void ScopStmt::checkForReductions() {
986 SmallVector<MemoryAccess *, 2> Loads;
987 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
988
989 // First collect candidate load-store reduction chains by iterating over all
990 // stores and collecting possible reduction loads.
991 for (MemoryAccess *StoreMA : MemAccs) {
992 if (StoreMA->isRead())
993 continue;
994
995 Loads.clear();
996 collectCandiateReductionLoads(StoreMA, Loads);
997 for (MemoryAccess *LoadMA : Loads)
998 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
999 }
1000
1001 // Then check each possible candidate pair.
1002 for (const auto &CandidatePair : Candidates) {
1003 bool Valid = true;
1004 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1005 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1006
1007 // Skip those with obviously unequal base addresses.
1008 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1009 isl_map_free(LoadAccs);
1010 isl_map_free(StoreAccs);
1011 continue;
1012 }
1013
1014 // And check if the remaining for overlap with other memory accesses.
1015 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1016 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1017 isl_set *AllAccs = isl_map_range(AllAccsRel);
1018
1019 for (MemoryAccess *MA : MemAccs) {
1020 if (MA == CandidatePair.first || MA == CandidatePair.second)
1021 continue;
1022
1023 isl_map *AccRel =
1024 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1025 isl_set *Accs = isl_map_range(AccRel);
1026
1027 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1028 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1029 Valid = Valid && isl_set_is_empty(OverlapAccs);
1030 isl_set_free(OverlapAccs);
1031 }
1032 }
1033
1034 isl_set_free(AllAccs);
1035 if (!Valid)
1036 continue;
1037
Johannes Doerfertf6183392014-07-01 20:52:51 +00001038 const LoadInst *Load =
1039 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1040 MemoryAccess::ReductionType RT =
1041 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1042
Johannes Doerferte58a0122014-06-27 20:31:28 +00001043 // If no overlapping access was found we mark the load and store as
1044 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001045 CandidatePair.first->markAsReductionLike(RT);
1046 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001047 }
Tobias Grosser75805372011-04-29 06:27:02 +00001048}
1049
Tobias Grosser74394f02013-01-14 22:40:23 +00001050std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001051
1052std::string ScopStmt::getScatteringStr() const {
Tobias Grossercf3942d2011-10-06 00:04:05 +00001053 return stringFromIslObj(Scattering);
Tobias Grosser75805372011-04-29 06:27:02 +00001054}
1055
Tobias Grosser74394f02013-01-14 22:40:23 +00001056unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001057
1058unsigned ScopStmt::getNumIterators() const {
1059 // The final read has one dimension with one element.
1060 if (!BB)
1061 return 1;
1062
Sebastian Pop860e0212013-02-15 21:26:44 +00001063 return NestLoops.size();
Tobias Grosser75805372011-04-29 06:27:02 +00001064}
1065
1066unsigned ScopStmt::getNumScattering() const {
1067 return isl_map_dim(Scattering, isl_dim_out);
1068}
1069
1070const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1071
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001072const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001073 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001074}
1075
Tobias Grosser74394f02013-01-14 22:40:23 +00001076isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001077
Tobias Grosser74394f02013-01-14 22:40:23 +00001078isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001079
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001080isl_space *ScopStmt::getDomainSpace() const {
1081 return isl_set_get_space(Domain);
1082}
1083
Tobias Grosser74394f02013-01-14 22:40:23 +00001084isl_id *ScopStmt::getDomainId() const { return isl_set_get_tuple_id(Domain); }
Tobias Grossercd95b772012-08-30 11:49:38 +00001085
Tobias Grosser75805372011-04-29 06:27:02 +00001086ScopStmt::~ScopStmt() {
1087 while (!MemAccs.empty()) {
1088 delete MemAccs.back();
1089 MemAccs.pop_back();
1090 }
1091
1092 isl_set_free(Domain);
1093 isl_map_free(Scattering);
1094}
1095
1096void ScopStmt::print(raw_ostream &OS) const {
1097 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001098 OS.indent(12) << "Domain :=\n";
1099
1100 if (Domain) {
1101 OS.indent(16) << getDomainStr() << ";\n";
1102 } else
1103 OS.indent(16) << "n/a\n";
1104
1105 OS.indent(12) << "Scattering :=\n";
1106
1107 if (Domain) {
1108 OS.indent(16) << getScatteringStr() << ";\n";
1109 } else
1110 OS.indent(16) << "n/a\n";
1111
Tobias Grosser083d3d32014-06-28 08:59:45 +00001112 for (MemoryAccess *Access : MemAccs)
1113 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001114}
1115
1116void ScopStmt::dump() const { print(dbgs()); }
1117
1118//===----------------------------------------------------------------------===//
1119/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001120
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001121void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001122 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1123 isl_set_free(Context);
1124 Context = NewContext;
1125}
1126
Tobias Grosserabfbe632013-02-05 12:09:06 +00001127void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001128 for (const SCEV *Parameter : NewParameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001129 if (ParameterIds.find(Parameter) != ParameterIds.end())
1130 continue;
1131
1132 int dimension = Parameters.size();
1133
1134 Parameters.push_back(Parameter);
1135 ParameterIds[Parameter] = dimension;
1136 }
1137}
1138
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001139__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1140 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001141
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001142 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001143 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001144
Tobias Grosser8f99c162011-11-15 11:38:55 +00001145 std::string ParameterName;
1146
1147 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1148 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001149 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001150 }
1151
1152 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001153 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001154
Tobias Grosser20532b82014-04-11 17:56:49 +00001155 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1156 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001157}
Tobias Grosser75805372011-04-29 06:27:02 +00001158
Tobias Grosser6be480c2011-11-08 15:41:13 +00001159void Scop::buildContext() {
1160 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001161 Context = isl_set_universe(isl_space_copy(Space));
1162 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001163}
1164
Tobias Grosser18daaca2012-05-22 10:47:27 +00001165void Scop::addParameterBounds() {
1166 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
Tobias Grosseredab1352013-06-21 06:41:31 +00001167 isl_val *V;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001168 isl_id *Id;
1169 const SCEV *Scev;
1170 const IntegerType *T;
1171
1172 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserabfbe632013-02-05 12:09:06 +00001173 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001174 T = dyn_cast<IntegerType>(Scev->getType());
1175 isl_id_free(Id);
1176
1177 assert(T && "Not an integer type");
1178 int Width = T->getBitWidth();
1179
Tobias Grosseredab1352013-06-21 06:41:31 +00001180 V = isl_val_int_from_si(IslCtx, Width - 1);
1181 V = isl_val_2exp(V);
1182 V = isl_val_neg(V);
1183 Context = isl_set_lower_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001184
Tobias Grosseredab1352013-06-21 06:41:31 +00001185 V = isl_val_int_from_si(IslCtx, Width - 1);
1186 V = isl_val_2exp(V);
1187 V = isl_val_sub_ui(V, 1);
1188 Context = isl_set_upper_bound_val(Context, isl_dim_param, i, V);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001189 }
1190}
1191
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001192void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001193 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001194 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001195
Tobias Grosser083d3d32014-06-28 08:59:45 +00001196 for (const auto &ParamID : ParameterIds) {
1197 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001198 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001199 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001200 }
1201
1202 // Align the parameters of all data structures to the model.
1203 Context = isl_set_align_params(Context, Space);
1204
Tobias Grosser083d3d32014-06-28 08:59:45 +00001205 for (ScopStmt *Stmt : *this)
1206 Stmt->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001207}
1208
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001209void Scop::simplifyAssumedContext() {
1210 // The parameter constraints of the iteration domains give us a set of
1211 // constraints that need to hold for all cases where at least a single
1212 // statement iteration is executed in the whole scop. We now simplify the
1213 // assumed context under the assumption that such constraints hold and at
1214 // least a single statement iteration is executed. For cases where no
1215 // statement instances are executed, the assumptions we have taken about
1216 // the executed code do not matter and can be changed.
1217 //
1218 // WARNING: This only holds if the assumptions we have taken do not reduce
1219 // the set of statement instances that are executed. Otherwise we
1220 // may run into a case where the iteration domains suggest that
1221 // for a certain set of parameter constraints no code is executed,
1222 // but in the original program some computation would have been
1223 // performed. In such a case, modifying the run-time conditions and
1224 // possibly influencing the run-time check may cause certain scops
1225 // to not be executed.
1226 //
1227 // Example:
1228 //
1229 // When delinearizing the following code:
1230 //
1231 // for (long i = 0; i < 100; i++)
1232 // for (long j = 0; j < m; j++)
1233 // A[i+p][j] = 1.0;
1234 //
1235 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
1236 // otherwise we would access out of bound data. Now, knowing that code is
1237 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1238 AssumedContext =
1239 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
1240}
1241
Johannes Doerfertb164c792014-09-18 11:17:17 +00001242/// @brief Add the minimal/maximal access in @p Set to @p User.
1243static int buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
1244 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1245 isl_pw_multi_aff *MinPMA, *MaxPMA;
1246 isl_pw_aff *LastDimAff;
1247 isl_aff *OneAff;
1248 unsigned Pos;
1249
Johannes Doerfert9143d672014-09-27 11:02:39 +00001250 // Restrict the number of parameters involved in the access as the lexmin/
1251 // lexmax computation will take too long if this number is high.
1252 //
1253 // Experiments with a simple test case using an i7 4800MQ:
1254 //
1255 // #Parameters involved | Time (in sec)
1256 // 6 | 0.01
1257 // 7 | 0.04
1258 // 8 | 0.12
1259 // 9 | 0.40
1260 // 10 | 1.54
1261 // 11 | 6.78
1262 // 12 | 30.38
1263 //
1264 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1265 unsigned InvolvedParams = 0;
1266 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1267 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1268 InvolvedParams++;
1269
1270 if (InvolvedParams > RunTimeChecksMaxParameters) {
1271 isl_set_free(Set);
1272 return -1;
1273 }
1274 }
1275
Johannes Doerfertb164c792014-09-18 11:17:17 +00001276 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1277 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1278
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001279 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1280 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1281
Johannes Doerfertb164c792014-09-18 11:17:17 +00001282 // Adjust the last dimension of the maximal access by one as we want to
1283 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1284 // we test during code generation might now point after the end of the
1285 // allocated array but we will never dereference it anyway.
1286 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1287 "Assumed at least one output dimension");
1288 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1289 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1290 OneAff = isl_aff_zero_on_domain(
1291 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1292 OneAff = isl_aff_add_constant_si(OneAff, 1);
1293 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1294 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1295
1296 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1297
1298 isl_set_free(Set);
1299 return 0;
1300}
1301
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001302static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1303 isl_set *Domain = MA->getStatement()->getDomain();
1304 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1305 return isl_set_reset_tuple_id(Domain);
1306}
1307
Johannes Doerfert9143d672014-09-27 11:02:39 +00001308bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001309 // To create sound alias checks we perform the following steps:
1310 // o) Use the alias analysis and an alias set tracker to build alias sets
1311 // for all memory accesses inside the SCoP.
1312 // o) For each alias set we then map the aliasing pointers back to the
1313 // memory accesses we know, thus obtain groups of memory accesses which
1314 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001315 // o) We divide each group based on the domains of the minimal/maximal
1316 // accesses. That means two minimal/maximal accesses are only in a group
1317 // if their access domains intersect, otherwise they are in different
1318 // ones.
Johannes Doerfert13771732014-10-01 12:40:46 +00001319 // o) We split groups such that they contain at most one read only base
1320 // address.
1321 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfertb164c792014-09-18 11:17:17 +00001322 // and maximal accesses to each array in this group.
1323 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1324
1325 AliasSetTracker AST(AA);
1326
1327 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001328 DenseSet<Value *> HasWriteAccess;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001329 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001330
1331 // Skip statements with an empty domain as they will never be executed.
1332 isl_set *StmtDomain = Stmt->getDomain();
1333 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1334 isl_set_free(StmtDomain);
1335 if (StmtDomainEmpty)
1336 continue;
1337
Johannes Doerfertb164c792014-09-18 11:17:17 +00001338 for (MemoryAccess *MA : *Stmt) {
1339 if (MA->isScalar())
1340 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001341 if (!MA->isRead())
1342 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001343 Instruction *Acc = MA->getAccessInstruction();
1344 PtrToAcc[getPointerOperand(*Acc)] = MA;
1345 AST.add(Acc);
1346 }
1347 }
1348
1349 SmallVector<AliasGroupTy, 4> AliasGroups;
1350 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001351 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001352 continue;
1353 AliasGroupTy AG;
1354 for (auto PR : AS)
1355 AG.push_back(PtrToAcc[PR.getValue()]);
1356 assert(AG.size() > 1 &&
1357 "Alias groups should contain at least two accesses");
1358 AliasGroups.push_back(std::move(AG));
1359 }
1360
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001361 // Split the alias groups based on their domain.
1362 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1363 AliasGroupTy NewAG;
1364 AliasGroupTy &AG = AliasGroups[u];
1365 AliasGroupTy::iterator AGI = AG.begin();
1366 isl_set *AGDomain = getAccessDomain(*AGI);
1367 while (AGI != AG.end()) {
1368 MemoryAccess *MA = *AGI;
1369 isl_set *MADomain = getAccessDomain(MA);
1370 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1371 NewAG.push_back(MA);
1372 AGI = AG.erase(AGI);
1373 isl_set_free(MADomain);
1374 } else {
1375 AGDomain = isl_set_union(AGDomain, MADomain);
1376 AGI++;
1377 }
1378 }
1379 if (NewAG.size() > 1)
1380 AliasGroups.push_back(std::move(NewAG));
1381 isl_set_free(AGDomain);
1382 }
1383
Johannes Doerfert13771732014-10-01 12:40:46 +00001384 DenseMap<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
1385 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1386 for (AliasGroupTy &AG : AliasGroups) {
1387 NonReadOnlyBaseValues.clear();
1388 ReadOnlyPairs.clear();
1389
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001390 if (AG.size() < 2) {
1391 AG.clear();
1392 continue;
1393 }
1394
Johannes Doerfert13771732014-10-01 12:40:46 +00001395 for (auto II = AG.begin(); II != AG.end();) {
1396 Value *BaseAddr = (*II)->getBaseAddr();
1397 if (HasWriteAccess.count(BaseAddr)) {
1398 NonReadOnlyBaseValues.insert(BaseAddr);
1399 II++;
1400 } else {
1401 ReadOnlyPairs[BaseAddr].insert(*II);
1402 II = AG.erase(II);
1403 }
1404 }
1405
1406 // If we don't have read only pointers check if there are at least two
1407 // non read only pointers, otherwise clear the alias group.
1408 if (ReadOnlyPairs.empty()) {
1409 if (NonReadOnlyBaseValues.size() <= 1)
1410 AG.clear();
1411 continue;
1412 }
1413
1414 // If we don't have non read only pointers clear the alias group.
1415 if (NonReadOnlyBaseValues.empty()) {
1416 AG.clear();
1417 continue;
1418 }
1419
1420 // If we have both read only and non read only base pointers we combine
1421 // the non read only ones with exactly one read only one at a time into a
1422 // new alias group and clear the old alias group in the end.
1423 for (const auto &ReadOnlyPair : ReadOnlyPairs) {
1424 AliasGroupTy AGNonReadOnly = AG;
1425 for (MemoryAccess *MA : ReadOnlyPair.second)
1426 AGNonReadOnly.push_back(MA);
1427 AliasGroups.push_back(std::move(AGNonReadOnly));
1428 }
1429 AG.clear();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001430 }
1431
Johannes Doerfert9143d672014-09-27 11:02:39 +00001432 bool Valid = true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001433 for (AliasGroupTy &AG : AliasGroups) {
Johannes Doerfert13771732014-10-01 12:40:46 +00001434 if (AG.empty())
1435 continue;
1436
Johannes Doerfertb164c792014-09-18 11:17:17 +00001437 MinMaxVectorTy *MinMaxAccesses = new MinMaxVectorTy();
1438 MinMaxAccesses->reserve(AG.size());
1439
1440 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
1441 for (MemoryAccess *MA : AG)
1442 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1443 Accesses = isl_union_map_intersect_domain(Accesses, getDomains());
1444
1445 isl_union_set *Locations = isl_union_map_range(Accesses);
1446 Locations = isl_union_set_intersect_params(Locations, getAssumedContext());
1447 Locations = isl_union_set_coalesce(Locations);
1448 Locations = isl_union_set_detect_equalities(Locations);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001449 Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
1450 MinMaxAccesses));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001451 isl_union_set_free(Locations);
Johannes Doerfertb164c792014-09-18 11:17:17 +00001452 MinMaxAliasGroups.push_back(MinMaxAccesses);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001453
1454 if (!Valid)
1455 break;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001456 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001457
1458 return Valid;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001459}
1460
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001461static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI) {
1462 unsigned MinLD = INT_MAX, MaxLD = 0;
1463 for (BasicBlock *BB : R.blocks()) {
1464 if (Loop *L = LI.getLoopFor(BB)) {
1465 unsigned LD = L->getLoopDepth();
1466 MinLD = std::min(MinLD, LD);
1467 MaxLD = std::max(MaxLD, LD);
1468 }
1469 }
1470
1471 // Handle the case that there is no loop in the SCoP first.
1472 if (MaxLD == 0)
1473 return 1;
1474
1475 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1476 assert(MaxLD >= MinLD &&
1477 "Maximal loop depth was smaller than mininaml loop depth?");
1478 return MaxLD - MinLD + 1;
1479}
1480
Tobias Grosser0e27e242011-10-06 00:03:48 +00001481Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
1482 isl_ctx *Context)
Tobias Grosserabfbe632013-02-05 12:09:06 +00001483 : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001484 MaxLoopDepth(getMaxLoopDepthInRegion(tempScop.getMaxRegion(), LI)) {
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001485 IslCtx = Context;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001486 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001487
Tobias Grosserabfbe632013-02-05 12:09:06 +00001488 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001489 SmallVector<unsigned, 8> Scatter;
1490
1491 Scatter.assign(MaxLoopDepth + 1, 0);
1492
1493 // Build the iteration domain, access functions and scattering functions
1494 // traversing the region tree.
1495 buildScop(tempScop, getRegion(), NestLoops, Scatter, LI);
Tobias Grosser75805372011-04-29 06:27:02 +00001496
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001497 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001498 addParameterBounds();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001499 simplifyAssumedContext();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001500
Tobias Grosser75805372011-04-29 06:27:02 +00001501 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1502}
1503
1504Scop::~Scop() {
1505 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001506 isl_set_free(AssumedContext);
Tobias Grosser75805372011-04-29 06:27:02 +00001507
1508 // Free the statements;
Tobias Grosser083d3d32014-06-28 08:59:45 +00001509 for (ScopStmt *Stmt : *this)
1510 delete Stmt;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001511
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001512 // Free the ScopArrayInfo objects.
1513 for (auto &ScopArrayInfoPair : ScopArrayInfoMap)
1514 delete ScopArrayInfoPair.second;
1515
Johannes Doerfertb164c792014-09-18 11:17:17 +00001516 // Free the alias groups
1517 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1518 for (MinMaxAccessTy &MMA : *MinMaxAccesses) {
1519 isl_pw_multi_aff_free(MMA.first);
1520 isl_pw_multi_aff_free(MMA.second);
1521 }
1522 delete MinMaxAccesses;
1523 }
Tobias Grosser75805372011-04-29 06:27:02 +00001524}
1525
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001526const ScopArrayInfo *
1527Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
1528 const SmallVector<const SCEV *, 4> &Sizes) {
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001529 const ScopArrayInfo *&SAI = ScopArrayInfoMap[BasePtr];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001530 if (!SAI)
1531 SAI = new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes);
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001532 return SAI;
1533}
1534
1535const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr) {
1536 const SCEV *PtrSCEV = SE->getSCEV(BasePtr);
1537 const SCEVUnknown *PtrBaseSCEV =
1538 cast<SCEVUnknown>(SE->getPointerBase(PtrSCEV));
1539 const ScopArrayInfo *SAI = ScopArrayInfoMap[PtrBaseSCEV->getValue()];
1540 assert(SAI && "No ScopArrayInfo available for this base pointer");
1541 return SAI;
1542}
1543
Tobias Grosser74394f02013-01-14 22:40:23 +00001544std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001545std::string Scop::getAssumedContextStr() const {
1546 return stringFromIslObj(AssumedContext);
1547}
Tobias Grosser75805372011-04-29 06:27:02 +00001548
1549std::string Scop::getNameStr() const {
1550 std::string ExitName, EntryName;
1551 raw_string_ostream ExitStr(ExitName);
1552 raw_string_ostream EntryStr(EntryName);
1553
Tobias Grosserf240b482014-01-09 10:42:15 +00001554 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001555 EntryStr.str();
1556
1557 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001558 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001559 ExitStr.str();
1560 } else
1561 ExitName = "FunctionExit";
1562
1563 return EntryName + "---" + ExitName;
1564}
1565
Tobias Grosser74394f02013-01-14 22:40:23 +00001566__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001567__isl_give isl_space *Scop::getParamSpace() const {
1568 return isl_set_get_space(this->Context);
1569}
1570
Tobias Grossere86109f2013-10-29 21:05:49 +00001571__isl_give isl_set *Scop::getAssumedContext() const {
1572 return isl_set_copy(AssumedContext);
1573}
1574
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001575void Scop::addAssumption(__isl_take isl_set *Set) {
1576 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001577 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001578}
1579
Tobias Grosser75805372011-04-29 06:27:02 +00001580void Scop::printContext(raw_ostream &OS) const {
1581 OS << "Context:\n";
1582
1583 if (!Context) {
1584 OS.indent(4) << "n/a\n\n";
1585 return;
1586 }
1587
1588 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001589
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001590 OS.indent(4) << "Assumed Context:\n";
1591 if (!AssumedContext) {
1592 OS.indent(4) << "n/a\n\n";
1593 return;
1594 }
1595
1596 OS.indent(4) << getAssumedContextStr() << "\n";
1597
Tobias Grosser083d3d32014-06-28 08:59:45 +00001598 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001599 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001600 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1601 }
Tobias Grosser75805372011-04-29 06:27:02 +00001602}
1603
Johannes Doerfertb164c792014-09-18 11:17:17 +00001604void Scop::printAliasAssumptions(raw_ostream &OS) const {
1605 OS.indent(4) << "Alias Groups (" << MinMaxAliasGroups.size() << "):\n";
1606 if (MinMaxAliasGroups.empty()) {
1607 OS.indent(8) << "n/a\n";
1608 return;
1609 }
1610 for (MinMaxVectorTy *MinMaxAccesses : MinMaxAliasGroups) {
1611 OS.indent(8) << "[[";
1612 for (MinMaxAccessTy &MinMacAccess : *MinMaxAccesses)
1613 OS << " <" << MinMacAccess.first << ", " << MinMacAccess.second << ">";
1614 OS << " ]]\n";
1615 }
1616}
1617
Tobias Grosser75805372011-04-29 06:27:02 +00001618void Scop::printStatements(raw_ostream &OS) const {
1619 OS << "Statements {\n";
1620
Tobias Grosser083d3d32014-06-28 08:59:45 +00001621 for (ScopStmt *Stmt : *this)
1622 OS.indent(4) << *Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00001623
1624 OS.indent(4) << "}\n";
1625}
1626
Tobias Grosser75805372011-04-29 06:27:02 +00001627void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00001628 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
1629 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00001630 OS.indent(4) << "Region: " << getNameStr() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001631 printContext(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00001632 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001633 printStatements(OS.indent(4));
1634}
1635
1636void Scop::dump() const { print(dbgs()); }
1637
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001638isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00001639
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001640__isl_give isl_union_set *Scop::getDomains() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001641 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001642
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001643 for (ScopStmt *Stmt : *this)
1644 Domain = isl_union_set_add_set(Domain, Stmt->getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00001645
1646 return Domain;
1647}
1648
Tobias Grosser780ce0f2014-07-11 07:12:10 +00001649__isl_give isl_union_map *Scop::getMustWrites() {
1650 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1651
1652 for (ScopStmt *Stmt : *this) {
1653 for (MemoryAccess *MA : *Stmt) {
1654 if (!MA->isMustWrite())
1655 continue;
1656
1657 isl_set *Domain = Stmt->getDomain();
1658 isl_map *AccessDomain = MA->getAccessRelation();
1659 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1660 Write = isl_union_map_add_map(Write, AccessDomain);
1661 }
1662 }
1663 return isl_union_map_coalesce(Write);
1664}
1665
1666__isl_give isl_union_map *Scop::getMayWrites() {
1667 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1668
1669 for (ScopStmt *Stmt : *this) {
1670 for (MemoryAccess *MA : *Stmt) {
1671 if (!MA->isMayWrite())
1672 continue;
1673
1674 isl_set *Domain = Stmt->getDomain();
1675 isl_map *AccessDomain = MA->getAccessRelation();
1676 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1677 Write = isl_union_map_add_map(Write, AccessDomain);
1678 }
1679 }
1680 return isl_union_map_coalesce(Write);
1681}
1682
Tobias Grosser37eb4222014-02-20 21:43:54 +00001683__isl_give isl_union_map *Scop::getWrites() {
1684 isl_union_map *Write = isl_union_map_empty(this->getParamSpace());
1685
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001686 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001687 for (MemoryAccess *MA : *Stmt) {
1688 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001689 continue;
1690
1691 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001692 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001693 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1694 Write = isl_union_map_add_map(Write, AccessDomain);
1695 }
1696 }
1697 return isl_union_map_coalesce(Write);
1698}
1699
1700__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001701 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001702
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001703 for (ScopStmt *Stmt : *this) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00001704 for (MemoryAccess *MA : *Stmt) {
1705 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00001706 continue;
1707
1708 isl_set *Domain = Stmt->getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00001709 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00001710
1711 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
1712 Read = isl_union_map_add_map(Read, AccessDomain);
1713 }
1714 }
1715 return isl_union_map_coalesce(Read);
1716}
1717
1718__isl_give isl_union_map *Scop::getSchedule() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001719 isl_union_map *Schedule = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001720
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001721 for (ScopStmt *Stmt : *this)
Tobias Grosser37eb4222014-02-20 21:43:54 +00001722 Schedule = isl_union_map_add_map(Schedule, Stmt->getScattering());
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001723
Tobias Grosser37eb4222014-02-20 21:43:54 +00001724 return isl_union_map_coalesce(Schedule);
1725}
1726
1727bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
1728 bool Changed = false;
Tobias Grosserbc4ef902014-06-28 08:59:38 +00001729 for (ScopStmt *Stmt : *this) {
Tobias Grosser37eb4222014-02-20 21:43:54 +00001730 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt->getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00001731 isl_union_set *NewStmtDomain = isl_union_set_intersect(
1732 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
1733
1734 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
1735 isl_union_set_free(StmtDomain);
1736 isl_union_set_free(NewStmtDomain);
1737 continue;
1738 }
1739
1740 Changed = true;
1741
1742 isl_union_set_free(StmtDomain);
1743 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
1744
1745 if (isl_union_set_is_empty(NewStmtDomain)) {
1746 Stmt->restrictDomain(isl_set_empty(Stmt->getDomainSpace()));
1747 isl_union_set_free(NewStmtDomain);
1748 } else
1749 Stmt->restrictDomain(isl_set_from_union_set(NewStmtDomain));
1750 }
1751 isl_union_set_free(Domain);
1752 return Changed;
1753}
1754
Tobias Grosser75805372011-04-29 06:27:02 +00001755ScalarEvolution *Scop::getSE() const { return SE; }
1756
1757bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
1758 if (tempScop.getAccessFunctions(BB))
1759 return false;
1760
1761 return true;
1762}
1763
Tobias Grosser74394f02013-01-14 22:40:23 +00001764void Scop::buildScop(TempScop &tempScop, const Region &CurRegion,
1765 SmallVectorImpl<Loop *> &NestLoops,
1766 SmallVectorImpl<unsigned> &Scatter, LoopInfo &LI) {
Tobias Grosser75805372011-04-29 06:27:02 +00001767 Loop *L = castToLoop(CurRegion, LI);
1768
1769 if (L)
1770 NestLoops.push_back(L);
1771
1772 unsigned loopDepth = NestLoops.size();
1773 assert(Scatter.size() > loopDepth && "Scatter not big enough!");
1774
1775 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00001776 E = CurRegion.element_end();
1777 I != E; ++I)
Tobias Grosser75805372011-04-29 06:27:02 +00001778 if (I->isSubRegion())
1779 buildScop(tempScop, *(I->getNodeAs<Region>()), NestLoops, Scatter, LI);
1780 else {
1781 BasicBlock *BB = I->getNodeAs<BasicBlock>();
1782
1783 if (isTrivialBB(BB, tempScop))
1784 continue;
1785
Johannes Doerfert7c494212014-10-31 23:13:39 +00001786 ScopStmt *Stmt =
1787 new ScopStmt(*this, tempScop, CurRegion, *BB, NestLoops, Scatter);
1788
1789 // Insert all statements into the statement map and the statement vector.
1790 StmtMap[BB] = Stmt;
1791 Stmts.push_back(Stmt);
Tobias Grosser75805372011-04-29 06:27:02 +00001792
1793 // Increasing the Scattering function is OK for the moment, because
1794 // we are using a depth first iterator and the program is well structured.
1795 ++Scatter[loopDepth];
1796 }
1797
1798 if (!L)
1799 return;
1800
1801 // Exiting a loop region.
1802 Scatter[loopDepth] = 0;
1803 NestLoops.pop_back();
Tobias Grosser74394f02013-01-14 22:40:23 +00001804 ++Scatter[loopDepth - 1];
Tobias Grosser75805372011-04-29 06:27:02 +00001805}
1806
Johannes Doerfert7c494212014-10-31 23:13:39 +00001807ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
1808 const auto &StmtMapIt = StmtMap.find(BB);
1809 if (StmtMapIt == StmtMap.end())
1810 return nullptr;
1811 return StmtMapIt->second;
1812}
1813
Tobias Grosser75805372011-04-29 06:27:02 +00001814//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00001815ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
1816 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00001817 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00001818}
1819
1820ScopInfo::~ScopInfo() {
1821 clear();
1822 isl_ctx_free(ctx);
1823}
1824
Tobias Grosser75805372011-04-29 06:27:02 +00001825void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1826 AU.addRequired<LoopInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001827 AU.addRequired<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00001828 AU.addRequired<ScalarEvolution>();
1829 AU.addRequired<TempScopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001830 AU.addRequired<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001831 AU.setPreservesAll();
1832}
1833
1834bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
1835 LoopInfo &LI = getAnalysis<LoopInfo>();
Johannes Doerfertb164c792014-09-18 11:17:17 +00001836 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Tobias Grosser75805372011-04-29 06:27:02 +00001837 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
1838
1839 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(R);
1840
1841 // This region is no Scop.
1842 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00001843 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00001844 return false;
1845 }
1846
Tobias Grosserb76f38532011-08-20 11:11:25 +00001847 scop = new Scop(*tempScop, LI, SE, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00001848
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001849 if (!PollyUseRuntimeAliasChecks) {
1850 // Statistics.
1851 ++ScopFound;
1852 if (scop->getMaxLoopDepth() > 0)
1853 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001854 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001855 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001856
Johannes Doerfert9143d672014-09-27 11:02:39 +00001857 // If a problem occurs while building the alias groups we need to delete
1858 // this SCoP and pretend it wasn't valid in the first place.
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001859 if (scop->buildAliasGroups(AA)) {
1860 // Statistics.
1861 ++ScopFound;
1862 if (scop->getMaxLoopDepth() > 0)
1863 ++RichScopFound;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001864 return false;
Johannes Doerfert21aa3dc2014-11-01 01:30:11 +00001865 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001866
1867 DEBUG(dbgs()
1868 << "\n\nNOTE: Run time checks for " << scop->getNameStr()
1869 << " could not be created as the number of parameters involved is too "
1870 "high. The SCoP will be "
1871 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust the "
1872 "maximal number of parameters but be advised that the compile time "
1873 "might increase exponentially.\n\n");
1874
1875 delete scop;
1876 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00001877 return false;
1878}
1879
1880char ScopInfo::ID = 0;
1881
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001882Pass *polly::createScopInfoPass() { return new ScopInfo(); }
1883
Tobias Grosser73600b82011-10-08 00:30:40 +00001884INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
1885 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001886 false);
Johannes Doerfertb164c792014-09-18 11:17:17 +00001887INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001888INITIALIZE_PASS_DEPENDENCY(LoopInfo);
Matt Arsenault8ca36812014-07-19 18:40:17 +00001889INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001890INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1891INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Tobias Grosser73600b82011-10-08 00:30:40 +00001892INITIALIZE_PASS_END(ScopInfo, "polly-scops",
1893 "Polly - Create polyhedral description of Scops", false,
1894 false)