blob: f3104a992ed4e977d62abb86d3869abfe6215832 [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"
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000021#include "polly/Options.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000022#include "polly/ScopInfo.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"
Michael Kruse7bf39442015-09-10 12:46:52 +000026#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosserf4c24b22015-04-05 13:11:54 +000027#include "llvm/ADT/MapVector.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000028#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000029#include "llvm/ADT/Statistic.h"
Johannes Doerfertecff11d2015-05-22 23:43:58 +000030#include "llvm/ADT/STLExtras.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000031#include "llvm/ADT/StringExtras.h"
Johannes Doerfert96425c22015-08-30 21:13:53 +000032#include "llvm/ADT/PostOrderIterator.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000033#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000034#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser83628182013-05-07 08:11:54 +000035#include "llvm/Analysis/RegionIterator.h"
36#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser75805372011-04-29 06:27:02 +000037#include "llvm/Support/Debug.h"
Tobias Grosser33ba62ad2011-08-18 06:31:50 +000038#include "isl/aff.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000039#include "isl/constraint.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000040#include "isl/local_space.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000041#include "isl/map.h"
Tobias Grosser4a8e3562011-12-07 07:42:51 +000042#include "isl/options.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000043#include "isl/printer.h"
Tobias Grosser808cd692015-07-14 09:33:13 +000044#include "isl/schedule.h"
45#include "isl/schedule_node.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000046#include "isl/set.h"
47#include "isl/union_map.h"
Tobias Grossercd524dc2015-05-09 09:36:38 +000048#include "isl/union_set.h"
Tobias Grosseredab1352013-06-21 06:41:31 +000049#include "isl/val.h"
Tobias Grosser75805372011-04-29 06:27:02 +000050#include <sstream>
51#include <string>
52#include <vector>
53
54using namespace llvm;
55using namespace polly;
56
Chandler Carruth95fef942014-04-22 03:30:19 +000057#define DEBUG_TYPE "polly-scops"
58
Tobias Grosser74394f02013-01-14 22:40:23 +000059STATISTIC(ScopFound, "Number of valid Scops");
60STATISTIC(RichScopFound, "Number of Scops containing a loop");
Tobias Grosser75805372011-04-29 06:27:02 +000061
Michael Kruse7bf39442015-09-10 12:46:52 +000062static cl::opt<bool> ModelReadOnlyScalars(
63 "polly-analyze-read-only-scalars",
64 cl::desc("Model read-only scalar values in the scop description"),
65 cl::Hidden, cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory));
66
Johannes Doerfert9e7b17b2014-08-18 00:40:13 +000067// Multiplicative reductions can be disabled separately as these kind of
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000068// operations can overflow easily. Additive reductions and bit operations
69// are in contrast pretty stable.
Tobias Grosser483a90d2014-07-09 10:50:10 +000070static cl::opt<bool> DisableMultiplicativeReductions(
71 "polly-disable-multiplicative-reductions",
72 cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore,
73 cl::init(false), cl::cat(PollyCategory));
Johannes Doerfert0ee1f212014-06-17 17:31:36 +000074
Johannes Doerfert9143d672014-09-27 11:02:39 +000075static cl::opt<unsigned> RunTimeChecksMaxParameters(
76 "polly-rtc-max-parameters",
77 cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden,
78 cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory));
79
Tobias Grosser71500722015-03-28 15:11:14 +000080static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup(
81 "polly-rtc-max-arrays-per-group",
82 cl::desc("The maximal number of arrays to compare in each alias group."),
83 cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory));
Tobias Grosser8a9c2352015-08-16 10:19:29 +000084static cl::opt<std::string> UserContextStr(
85 "polly-context", cl::value_desc("isl parameter set"),
86 cl::desc("Provide additional constraints on the context parameters"),
87 cl::init(""), cl::cat(PollyCategory));
Tobias Grosser71500722015-03-28 15:11:14 +000088
Tobias Grosserd83b8a82015-08-20 19:08:11 +000089static cl::opt<bool> DetectReductions("polly-detect-reductions",
90 cl::desc("Detect and exploit reductions"),
91 cl::Hidden, cl::ZeroOrMore,
92 cl::init(true), cl::cat(PollyCategory));
93
Michael Kruse7bf39442015-09-10 12:46:52 +000094//===----------------------------------------------------------------------===//
95/// Helper Classes
96
97void Comparison::print(raw_ostream &OS) const {
98 // Not yet implemented.
99}
100
Michael Kruse046dde42015-08-10 13:01:57 +0000101// Create a sequence of two schedules. Either argument may be null and is
102// interpreted as the empty schedule. Can also return null if both schedules are
103// empty.
104static __isl_give isl_schedule *
105combineInSequence(__isl_take isl_schedule *Prev,
106 __isl_take isl_schedule *Succ) {
107 if (!Prev)
108 return Succ;
109 if (!Succ)
110 return Prev;
111
112 return isl_schedule_sequence(Prev, Succ);
113}
114
Johannes Doerferte7044942015-02-24 11:58:30 +0000115static __isl_give isl_set *addRangeBoundsToSet(__isl_take isl_set *S,
116 const ConstantRange &Range,
117 int dim,
118 enum isl_dim_type type) {
119 isl_val *V;
120 isl_ctx *ctx = isl_set_get_ctx(S);
121
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000122 bool useLowerUpperBound = Range.isSignWrappedSet() && !Range.isFullSet();
123 const auto LB = useLowerUpperBound ? Range.getLower() : Range.getSignedMin();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000124 V = isl_valFromAPInt(ctx, LB, true);
Johannes Doerferte7044942015-02-24 11:58:30 +0000125 isl_set *SLB = isl_set_lower_bound_val(isl_set_copy(S), type, dim, V);
126
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000127 const auto UB = useLowerUpperBound ? Range.getUpper() : Range.getSignedMax();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000128 V = isl_valFromAPInt(ctx, UB, true);
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000129 if (useLowerUpperBound)
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000130 V = isl_val_sub_ui(V, 1);
Johannes Doerferte7044942015-02-24 11:58:30 +0000131 isl_set *SUB = isl_set_upper_bound_val(S, type, dim, V);
132
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000133 if (useLowerUpperBound)
Johannes Doerferte7044942015-02-24 11:58:30 +0000134 return isl_set_union(SLB, SUB);
135 else
136 return isl_set_intersect(SLB, SUB);
137}
138
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000139static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) {
140 LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr);
141 if (!BasePtrLI)
142 return nullptr;
143
144 if (!S->getRegion().contains(BasePtrLI))
145 return nullptr;
146
147 ScalarEvolution &SE = *S->getSE();
148
149 auto *OriginBaseSCEV =
150 SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand()));
151 if (!OriginBaseSCEV)
152 return nullptr;
153
154 auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV);
155 if (!OriginBaseSCEVUnknown)
156 return nullptr;
157
158 return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue());
159}
160
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000161ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx,
Tobias Grosser92245222015-07-28 14:53:44 +0000162 const SmallVector<const SCEV *, 4> &DimensionSizes,
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000163 bool IsPHI, Scop *S)
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000164 : BasePtr(BasePtr), ElementType(ElementType),
Tobias Grosser92245222015-07-28 14:53:44 +0000165 DimensionSizes(DimensionSizes), IsPHI(IsPHI) {
166 std::string BasePtrName =
167 getIslCompatibleName("MemRef_", BasePtr, IsPHI ? "__phi" : "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000168 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000169 for (const SCEV *Expr : DimensionSizes) {
170 isl_pw_aff *Size = S->getPwAff(Expr);
171 DimensionSizesPw.push_back(Size);
172 }
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000173
174 BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr);
175 if (BasePtrOriginSAI)
176 const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this);
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000177}
178
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000179ScopArrayInfo::~ScopArrayInfo() {
180 isl_id_free(Id);
181 for (isl_pw_aff *Size : DimensionSizesPw)
182 isl_pw_aff_free(Size);
183}
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000184
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000185std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); }
186
187int ScopArrayInfo::getElemSizeInBytes() const {
188 return ElementType->getPrimitiveSizeInBits() / 8;
189}
190
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000191isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
192
193void ScopArrayInfo::dump() const { print(errs()); }
194
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000195void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const {
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000196 OS.indent(8) << *getElementType() << " " << getName() << "[*]";
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000197 for (unsigned u = 0; u < getNumberOfDimensions(); u++) {
198 OS << "[";
199
200 if (SizeAsPwAff)
201 OS << " " << DimensionSizesPw[u] << " ";
202 else
203 OS << *DimensionSizes[u];
204
205 OS << "]";
206 }
207
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000208 if (BasePtrOriginSAI)
209 OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]";
210
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000211 OS << " // Element size " << getElemSizeInBytes() << "\n";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000212}
213
214const ScopArrayInfo *
215ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
216 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
217 assert(Id && "Output dimension didn't have an ID");
218 return getFromId(Id);
219}
220
221const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
222 void *User = isl_id_get_user(Id);
223 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
224 isl_id_free(Id);
225 return SAI;
226}
227
Michael Kruse7bf39442015-09-10 12:46:52 +0000228void IRAccess::print(raw_ostream &OS) const {
229 if (isRead())
230 OS << "Read ";
231 else {
232 if (isMayWrite())
233 OS << "May";
234 OS << "Write ";
235 }
236 OS << BaseAddress->getName() << '[' << *Offset << "]\n";
237}
238
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000239const std::string
240MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
241 switch (RT) {
242 case MemoryAccess::RT_NONE:
243 llvm_unreachable("Requested a reduction operator string for a memory "
244 "access which isn't a reduction");
245 case MemoryAccess::RT_ADD:
246 return "+";
247 case MemoryAccess::RT_MUL:
248 return "*";
249 case MemoryAccess::RT_BOR:
250 return "|";
251 case MemoryAccess::RT_BXOR:
252 return "^";
253 case MemoryAccess::RT_BAND:
254 return "&";
255 }
256 llvm_unreachable("Unknown reduction type");
257 return "";
258}
259
Johannes Doerfertf6183392014-07-01 20:52:51 +0000260/// @brief Return the reduction type for a given binary operator
261static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
262 const Instruction *Load) {
263 if (!BinOp)
264 return MemoryAccess::RT_NONE;
265 switch (BinOp->getOpcode()) {
266 case Instruction::FAdd:
267 if (!BinOp->hasUnsafeAlgebra())
268 return MemoryAccess::RT_NONE;
269 // Fall through
270 case Instruction::Add:
271 return MemoryAccess::RT_ADD;
272 case Instruction::Or:
273 return MemoryAccess::RT_BOR;
274 case Instruction::Xor:
275 return MemoryAccess::RT_BXOR;
276 case Instruction::And:
277 return MemoryAccess::RT_BAND;
278 case Instruction::FMul:
279 if (!BinOp->hasUnsafeAlgebra())
280 return MemoryAccess::RT_NONE;
281 // Fall through
282 case Instruction::Mul:
283 if (DisableMultiplicativeReductions)
284 return MemoryAccess::RT_NONE;
285 return MemoryAccess::RT_MUL;
286 default:
287 return MemoryAccess::RT_NONE;
288 }
289}
Tobias Grosser75805372011-04-29 06:27:02 +0000290//===----------------------------------------------------------------------===//
291
292MemoryAccess::~MemoryAccess() {
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000293 isl_id_free(Id);
Tobias Grosser54a86e62011-08-18 06:31:46 +0000294 isl_map_free(AccessRelation);
Tobias Grosser166c4222015-09-05 07:46:40 +0000295 isl_map_free(NewAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000296}
297
Johannes Doerfert8f7124c2014-09-12 11:00:49 +0000298static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) {
299 switch (Access.getType()) {
300 case IRAccess::READ:
301 return MemoryAccess::READ;
302 case IRAccess::MUST_WRITE:
303 return MemoryAccess::MUST_WRITE;
304 case IRAccess::MAY_WRITE:
305 return MemoryAccess::MAY_WRITE;
306 }
307 llvm_unreachable("Unknown IRAccess type!");
308}
309
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000310const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
311 isl_id *ArrayId = getArrayId();
312 void *User = isl_id_get_user(ArrayId);
313 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
314 isl_id_free(ArrayId);
315 return SAI;
316}
317
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000318__isl_give isl_id *MemoryAccess::getArrayId() const {
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000319 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
320}
321
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000322__isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation(
323 __isl_take isl_union_map *USchedule) const {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000324 isl_map *Schedule, *ScheduledAccRel;
325 isl_union_set *UDomain;
326
327 UDomain = isl_union_set_from_set(getStatement()->getDomain());
328 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
329 Schedule = isl_map_from_union_map(USchedule);
330 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
331 return isl_pw_multi_aff_from_map(ScheduledAccRel);
332}
333
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000334__isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000335 return isl_map_copy(AccessRelation);
336}
337
Johannes Doerferta99130f2014-10-13 12:58:03 +0000338std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000339 return stringFromIslObj(AccessRelation);
340}
341
Johannes Doerferta99130f2014-10-13 12:58:03 +0000342__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000343 return isl_map_get_space(AccessRelation);
344}
345
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000346__isl_give isl_map *MemoryAccess::getNewAccessRelation() const {
Tobias Grosser166c4222015-09-05 07:46:40 +0000347 return isl_map_copy(NewAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000348}
349
Tobias Grosser6f730082015-09-05 07:46:47 +0000350std::string MemoryAccess::getNewAccessRelationStr() const {
351 return stringFromIslObj(NewAccessRelation);
352}
353
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000354__isl_give isl_basic_map *
355MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000356 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000357 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000358
Tobias Grosser084d8f72012-05-29 09:29:44 +0000359 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000360 isl_basic_set_universe(Statement->getDomainSpace()),
361 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000362}
363
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000364// Formalize no out-of-bound access assumption
365//
366// When delinearizing array accesses we optimistically assume that the
367// delinearized accesses do not access out of bound locations (the subscript
368// expression of each array evaluates for each statement instance that is
369// executed to a value that is larger than zero and strictly smaller than the
370// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000371// dimension for which we do not need to assume any upper bound. At this point
372// we formalize this assumption to ensure that at code generation time the
373// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000374//
375// To find the set of constraints necessary to avoid out of bound accesses, we
376// first build the set of data locations that are not within array bounds. We
377// then apply the reverse access relation to obtain the set of iterations that
378// may contain invalid accesses and reduce this set of iterations to the ones
379// that are actually executed by intersecting them with the domain of the
380// statement. If we now project out all loop dimensions, we obtain a set of
381// parameters that may cause statement instances to be executed that may
382// possibly yield out of bound memory accesses. The complement of these
383// constraints is the set of constraints that needs to be assumed to ensure such
384// statement instances are never executed.
385void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000386 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000387 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000388 for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000389 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
390 isl_pw_aff *Var =
391 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
392 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
393
394 isl_set *DimOutside;
395
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000396 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
Johannes Doerfert574182d2015-08-12 10:19:50 +0000397 isl_pw_aff *SizeE = Statement->getPwAff(Access.Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000398
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000399 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
400 Statement->getNumIterators());
401 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
402 isl_space_dim(Space, isl_dim_set));
403 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
404 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000405
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000406 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000407
408 Outside = isl_set_union(Outside, DimOutside);
409 }
410
411 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
412 Outside = isl_set_intersect(Outside, Statement->getDomain());
413 Outside = isl_set_params(Outside);
Tobias Grosserf54bb772015-06-26 12:09:28 +0000414
415 // Remove divs to avoid the construction of overly complicated assumptions.
416 // Doing so increases the set of parameter combinations that are assumed to
417 // not appear. This is always save, but may make the resulting run-time check
418 // bail out more often than strictly necessary.
419 Outside = isl_set_remove_divs(Outside);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000420 Outside = isl_set_complement(Outside);
421 Statement->getParent()->addAssumption(Outside);
422 isl_space_free(Space);
423}
424
Johannes Doerferte7044942015-02-24 11:58:30 +0000425void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) {
426 ScalarEvolution *SE = Statement->getParent()->getSE();
427
428 Value *Ptr = getPointerOperand(*getAccessInstruction());
429 if (!Ptr || !SE->isSCEVable(Ptr->getType()))
430 return;
431
432 auto *PtrSCEV = SE->getSCEV(Ptr);
433 if (isa<SCEVCouldNotCompute>(PtrSCEV))
434 return;
435
436 auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV);
437 if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV))
438 PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV);
439
440 const ConstantRange &Range = SE->getSignedRange(PtrSCEV);
441 if (Range.isFullSet())
442 return;
443
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000444 bool isWrapping = Range.isSignWrappedSet();
Johannes Doerferte7044942015-02-24 11:58:30 +0000445 unsigned BW = Range.getBitWidth();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000446 const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin();
447 const auto UB = isWrapping ? Range.getUpper() : Range.getSignedMax();
448
449 auto Min = LB.sdiv(APInt(BW, ElementSize));
450 auto Max = (UB - APInt(BW, 1)).sdiv(APInt(BW, ElementSize));
Johannes Doerferte7044942015-02-24 11:58:30 +0000451
452 isl_set *AccessRange = isl_map_range(isl_map_copy(AccessRelation));
453 AccessRange =
454 addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, isl_dim_set);
455 AccessRelation = isl_map_intersect_range(AccessRelation, AccessRange);
456}
457
Tobias Grosser619190d2015-03-30 17:22:28 +0000458__isl_give isl_map *MemoryAccess::foldAccess(const IRAccess &Access,
459 __isl_take isl_map *AccessRelation,
460 ScopStmt *Statement) {
461 int Size = Access.Subscripts.size();
462
463 for (int i = Size - 2; i >= 0; --i) {
464 isl_space *Space;
465 isl_map *MapOne, *MapTwo;
Johannes Doerfert574182d2015-08-12 10:19:50 +0000466 isl_pw_aff *DimSize = Statement->getPwAff(Access.Sizes[i]);
Tobias Grosser619190d2015-03-30 17:22:28 +0000467
468 isl_space *SpaceSize = isl_pw_aff_get_space(DimSize);
469 isl_pw_aff_free(DimSize);
470 isl_id *ParamId = isl_space_get_dim_id(SpaceSize, isl_dim_param, 0);
471
472 Space = isl_map_get_space(AccessRelation);
473 Space = isl_space_map_from_set(isl_space_range(Space));
474 Space = isl_space_align_params(Space, SpaceSize);
475
476 int ParamLocation = isl_space_find_dim_by_id(Space, isl_dim_param, ParamId);
477 isl_id_free(ParamId);
478
479 MapOne = isl_map_universe(isl_space_copy(Space));
480 for (int j = 0; j < Size; ++j)
481 MapOne = isl_map_equate(MapOne, isl_dim_in, j, isl_dim_out, j);
482 MapOne = isl_map_lower_bound_si(MapOne, isl_dim_in, i + 1, 0);
483
484 MapTwo = isl_map_universe(isl_space_copy(Space));
485 for (int j = 0; j < Size; ++j)
486 if (j < i || j > i + 1)
487 MapTwo = isl_map_equate(MapTwo, isl_dim_in, j, isl_dim_out, j);
488
489 isl_local_space *LS = isl_local_space_from_space(Space);
490 isl_constraint *C;
491 C = isl_equality_alloc(isl_local_space_copy(LS));
492 C = isl_constraint_set_constant_si(C, -1);
493 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, 1);
494 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, -1);
495 MapTwo = isl_map_add_constraint(MapTwo, C);
496 C = isl_equality_alloc(LS);
497 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i + 1, 1);
498 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i + 1, -1);
499 C = isl_constraint_set_coefficient_si(C, isl_dim_param, ParamLocation, 1);
500 MapTwo = isl_map_add_constraint(MapTwo, C);
501 MapTwo = isl_map_upper_bound_si(MapTwo, isl_dim_in, i + 1, -1);
502
503 MapOne = isl_map_union(MapOne, MapTwo);
504 AccessRelation = isl_map_apply_range(AccessRelation, MapOne);
505 }
506 return AccessRelation;
507}
508
Johannes Doerfert13c8cf22014-08-10 08:09:38 +0000509MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst,
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000510 ScopStmt *Statement, const ScopArrayInfo *SAI,
511 int Identifier)
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000512 : AccType(getMemoryAccessType(Access)), Statement(Statement),
513 AccessInstruction(AccInst), AccessValue(Access.getAccessValue()),
Tobias Grosser166c4222015-09-05 07:46:40 +0000514 NewAccessRelation(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +0000515
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000516 isl_ctx *Ctx = Statement->getIslCtx();
Tobias Grosser9759f852011-11-10 12:44:55 +0000517 BaseAddr = Access.getBase();
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000518 BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000519
520 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000521
Tobias Grosserac3a95f2015-08-03 17:53:21 +0000522 auto IdName = "__polly_array_ref_" + std::to_string(Identifier);
Tobias Grossere29d31c2015-05-15 12:24:09 +0000523 Id = isl_id_alloc(Ctx, IdName.c_str(), nullptr);
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000524
Tobias Grossera1879642011-12-20 10:43:14 +0000525 if (!Access.isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000526 // We overapproximate non-affine accesses with a possible access to the
527 // whole array. For read accesses it does not make a difference, if an
528 // access must or may happen. However, for write accesses it is important to
529 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000530 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000531 AccessRelation =
532 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Johannes Doerferte7044942015-02-24 11:58:30 +0000533
534 computeBoundsOnAccessRelation(Access.getElemSizeInBytes());
Tobias Grossera1879642011-12-20 10:43:14 +0000535 return;
536 }
537
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000538 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000539 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000540
Tobias Grosser79baa212014-04-10 08:38:02 +0000541 for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) {
Johannes Doerfert574182d2015-08-12 10:19:50 +0000542 isl_pw_aff *Affine = Statement->getPwAff(Access.Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000543
Sebastian Pop422e33f2014-06-03 18:16:31 +0000544 if (Size == 1) {
545 // For the non delinearized arrays, divide the access function of the last
546 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000547 //
548 // A stride one array access in C expressed as A[i] is expressed in
549 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
550 // two subsequent values of 'i' index two values that are stored next to
551 // each other in memory. By this division we make this characteristic
552 // obvious again.
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000553 isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000554 Affine = isl_pw_aff_scale_down_val(Affine, v);
555 }
556
557 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
558
Tobias Grosser79baa212014-04-10 08:38:02 +0000559 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000560 }
561
Tobias Grosser619190d2015-03-30 17:22:28 +0000562 AccessRelation = foldAccess(Access, AccessRelation, Statement);
563
Tobias Grosser79baa212014-04-10 08:38:02 +0000564 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000565 AccessRelation = isl_map_set_tuple_id(
566 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000567 AccessRelation =
568 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
569
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000570 assumeNoOutOfBound(Access);
Tobias Grosseraa660a92015-03-30 00:07:50 +0000571 AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain());
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000572 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000573}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000574
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000575void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000576 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000577 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000578}
579
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000580const std::string MemoryAccess::getReductionOperatorStr() const {
581 return MemoryAccess::getReductionOperatorStr(getReductionType());
582}
583
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000584__isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); }
585
Johannes Doerfertf6183392014-07-01 20:52:51 +0000586raw_ostream &polly::operator<<(raw_ostream &OS,
587 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000588 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000589 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000590 else
591 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000592 return OS;
593}
594
Tobias Grosser75805372011-04-29 06:27:02 +0000595void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000596 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000597 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000598 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000599 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000600 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000601 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000602 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000603 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000604 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000605 break;
606 }
Johannes Doerfert0ff23ec2015-02-06 20:13:15 +0000607 OS << "[Reduction Type: " << getReductionType() << "] ";
608 OS << "[Scalar: " << isScalar() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000609 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser6f730082015-09-05 07:46:47 +0000610 if (hasNewAccessRelation())
611 OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000612}
613
Tobias Grosser74394f02013-01-14 22:40:23 +0000614void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000615
616// Create a map in the size of the provided set domain, that maps from the
617// one element of the provided set domain to another element of the provided
618// set domain.
619// The mapping is limited to all points that are equal in all but the last
620// dimension and for which the last dimension of the input is strict smaller
621// than the last dimension of the output.
622//
623// getEqualAndLarger(set[i0, i1, ..., iX]):
624//
625// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
626// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
627//
Tobias Grosserf5338802011-10-06 00:03:35 +0000628static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000629 isl_space *Space = isl_space_map_from_set(setDomain);
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000630 isl_map *Map = isl_map_universe(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000631 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000632
633 // Set all but the last dimension to be equal for the input and output
634 //
635 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
636 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000637 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000638 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000639
640 // Set the last dimension of the input to be strict smaller than the
641 // last dimension of the output.
642 //
643 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000644 Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out,
645 lastDimension);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000646 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000647}
648
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000649__isl_give isl_set *
650MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000651 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000652 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000653 isl_space *Space = isl_space_range(isl_map_get_space(S));
654 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000655
Sebastian Popa00a0292012-12-18 07:46:06 +0000656 S = isl_map_reverse(S);
657 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000658
Sebastian Popa00a0292012-12-18 07:46:06 +0000659 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
660 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
661 NextScatt = isl_map_apply_domain(NextScatt, S);
662 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000663
Sebastian Popa00a0292012-12-18 07:46:06 +0000664 isl_set *Deltas = isl_map_deltas(NextScatt);
665 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000666}
667
Sebastian Popa00a0292012-12-18 07:46:06 +0000668bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000669 int StrideWidth) const {
670 isl_set *Stride, *StrideX;
671 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000672
Sebastian Popa00a0292012-12-18 07:46:06 +0000673 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000674 StrideX = isl_set_universe(isl_set_get_space(Stride));
Tobias Grosser01c8f5f2015-08-24 22:20:46 +0000675 for (unsigned i = 0; i < isl_set_dim(StrideX, isl_dim_set) - 1; i++)
676 StrideX = isl_set_fix_si(StrideX, isl_dim_set, i, 0);
677 StrideX = isl_set_fix_si(StrideX, isl_dim_set,
678 isl_set_dim(StrideX, isl_dim_set) - 1, StrideWidth);
Roman Gareevf2bd72e2015-08-18 16:12:05 +0000679 IsStrideX = isl_set_is_subset(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000680
Tobias Grosser28dd4862012-01-24 16:42:16 +0000681 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000682 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000683
Tobias Grosser28dd4862012-01-24 16:42:16 +0000684 return IsStrideX;
685}
686
Sebastian Popa00a0292012-12-18 07:46:06 +0000687bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
688 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000689}
690
Tobias Grosser79baa212014-04-10 08:38:02 +0000691bool MemoryAccess::isScalar() const {
692 return isl_map_n_out(AccessRelation) == 0;
693}
694
Sebastian Popa00a0292012-12-18 07:46:06 +0000695bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
696 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000697}
698
Tobias Grosser166c4222015-09-05 07:46:40 +0000699void MemoryAccess::setNewAccessRelation(isl_map *NewAccess) {
700 isl_map_free(NewAccessRelation);
701 NewAccessRelation = NewAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000702}
Tobias Grosser75805372011-04-29 06:27:02 +0000703
704//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000705
Tobias Grosser808cd692015-07-14 09:33:13 +0000706isl_map *ScopStmt::getSchedule() const {
707 isl_set *Domain = getDomain();
708 if (isl_set_is_empty(Domain)) {
709 isl_set_free(Domain);
710 return isl_map_from_aff(
711 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
712 }
713 auto *Schedule = getParent()->getSchedule();
714 Schedule = isl_union_map_intersect_domain(
715 Schedule, isl_union_set_from_set(isl_set_copy(Domain)));
716 if (isl_union_map_is_empty(Schedule)) {
717 isl_set_free(Domain);
718 isl_union_map_free(Schedule);
719 return isl_map_from_aff(
720 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
721 }
722 auto *M = isl_map_from_union_map(Schedule);
723 M = isl_map_coalesce(M);
724 M = isl_map_gist_domain(M, Domain);
725 M = isl_map_coalesce(M);
726 return M;
727}
Tobias Grossercf3942d2011-10-06 00:04:05 +0000728
Johannes Doerfert574182d2015-08-12 10:19:50 +0000729__isl_give isl_pw_aff *ScopStmt::getPwAff(const SCEV *E) {
Johannes Doerfertb409fdc2015-08-28 09:24:35 +0000730 return getParent()->getPwAff(E, Domain);
Johannes Doerfert574182d2015-08-12 10:19:50 +0000731}
732
Tobias Grosser37eb4222014-02-20 21:43:54 +0000733void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
734 assert(isl_set_is_subset(NewDomain, Domain) &&
735 "New domain is not a subset of old domain!");
736 isl_set_free(Domain);
737 Domain = NewDomain;
Tobias Grosser75805372011-04-29 06:27:02 +0000738}
739
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000740void ScopStmt::buildAccesses(TempScop &tempScop, BasicBlock *Block,
741 bool isApproximated) {
742 AccFuncSetType *AFS = tempScop.getAccessFunctions(Block);
743 if (!AFS)
744 return;
745
746 for (auto &AccessPair : *AFS) {
747 IRAccess &Access = AccessPair.first;
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000748 Instruction *AccessInst = AccessPair.second;
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000749 Type *ElementType = Access.getAccessValue()->getType();
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000750
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000751 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
Tobias Grosser92245222015-07-28 14:53:44 +0000752 Access.getBase(), ElementType, Access.Sizes, Access.isPHI());
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000753
Johannes Doerfertff9d1982015-02-24 12:00:50 +0000754 if (isApproximated && Access.isWrite())
755 Access.setMayWrite();
756
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000757 MemoryAccessList *&MAL = InstructionToAccess[AccessInst];
758 if (!MAL)
759 MAL = new MemoryAccessList();
760 MAL->emplace_front(Access, AccessInst, this, SAI, MemAccs.size());
761 MemAccs.push_back(&MAL->front());
Tobias Grosser75805372011-04-29 06:27:02 +0000762 }
763}
764
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000765void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000766 for (MemoryAccess *MA : *this)
767 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000768
769 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000770}
771
Johannes Doerfert96425c22015-08-30 21:13:53 +0000772static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred,
773 isl_pw_aff *L, isl_pw_aff *R) {
774 switch (Pred) {
775 case ICmpInst::ICMP_EQ:
776 return isl_pw_aff_eq_set(L, R);
777 case ICmpInst::ICMP_NE:
778 return isl_pw_aff_ne_set(L, R);
779 case ICmpInst::ICMP_SLT:
780 return isl_pw_aff_lt_set(L, R);
781 case ICmpInst::ICMP_SLE:
782 return isl_pw_aff_le_set(L, R);
783 case ICmpInst::ICMP_SGT:
784 return isl_pw_aff_gt_set(L, R);
785 case ICmpInst::ICMP_SGE:
786 return isl_pw_aff_ge_set(L, R);
787 case ICmpInst::ICMP_ULT:
788 return isl_pw_aff_lt_set(L, R);
789 case ICmpInst::ICMP_UGT:
790 return isl_pw_aff_gt_set(L, R);
791 case ICmpInst::ICMP_ULE:
792 return isl_pw_aff_le_set(L, R);
793 case ICmpInst::ICMP_UGE:
794 return isl_pw_aff_ge_set(L, R);
795 default:
796 llvm_unreachable("Non integer predicate not supported");
797 }
798}
799
800/// @brief Build the conditions sets for the branch @p BI in the @p Domain.
801///
802/// This will fill @p ConditionSets with the conditions under which control
803/// will be moved from @p BI to its successors. Hence, @p ConditionSets will
804/// have as many elements as @p BI has successors.
805static void
806buildConditionSets(Scop &S, BranchInst *BI, Loop *L, __isl_keep isl_set *Domain,
807 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
808
809 if (BI->isUnconditional()) {
810 ConditionSets.push_back(isl_set_copy(Domain));
811 return;
812 }
813
814 Value *Condition = BI->getCondition();
815
816 isl_set *ConsequenceCondSet = nullptr;
817 if (auto *CCond = dyn_cast<ConstantInt>(Condition)) {
818 if (CCond->isZero())
819 ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
820 else
821 ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain));
822 } else {
823 auto *ICond = dyn_cast<ICmpInst>(Condition);
824 assert(ICond &&
825 "Condition of exiting branch was neither constant nor ICmp!");
826
827 ScalarEvolution &SE = *S.getSE();
828 isl_pw_aff *LHS, *RHS;
829 LHS = S.getPwAff(SE.getSCEVAtScope(ICond->getOperand(0), L), Domain);
830 RHS = S.getPwAff(SE.getSCEVAtScope(ICond->getOperand(1), L), Domain);
831 ConsequenceCondSet = buildConditionSet(ICond->getPredicate(), LHS, RHS);
832 }
833
834 assert(ConsequenceCondSet);
835 isl_set *AlternativeCondSet =
836 isl_set_complement(isl_set_copy(ConsequenceCondSet));
837
838 ConditionSets.push_back(isl_set_coalesce(
839 isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))));
840 ConditionSets.push_back(isl_set_coalesce(
841 isl_set_intersect(AlternativeCondSet, isl_set_copy(Domain))));
842}
843
Johannes Doerfertd020b772015-08-27 06:53:52 +0000844void ScopStmt::addLoopTripCountToDomain(const Loop *L) {
845
Johannes Doerfert96425c22015-08-30 21:13:53 +0000846 int RelativeLoopDimension = getParent()->getRelativeLoopDepth(L);
847 assert(RelativeLoopDimension >= 0 &&
848 "Expected relative loop depth of L to be non-negative");
849 unsigned loopDimension = RelativeLoopDimension;
850
Johannes Doerfertd020b772015-08-27 06:53:52 +0000851 ScalarEvolution *SE = getParent()->getSE();
852 isl_space *DomSpace = isl_set_get_space(Domain);
853
854 isl_space *MapSpace = isl_space_map_from_set(isl_space_copy(DomSpace));
855 isl_multi_aff *LoopMAff = isl_multi_aff_identity(MapSpace);
856 isl_aff *LoopAff = isl_multi_aff_get_aff(LoopMAff, loopDimension);
857 LoopAff = isl_aff_add_constant_si(LoopAff, 1);
858 LoopMAff = isl_multi_aff_set_aff(LoopMAff, loopDimension, LoopAff);
859 isl_map *TranslationMap = isl_map_from_multi_aff(LoopMAff);
860
861 BasicBlock *ExitingBB = L->getExitingBlock();
862 assert(ExitingBB && "Loop has more than one exiting block");
863
864 BranchInst *Term = dyn_cast<BranchInst>(ExitingBB->getTerminator());
865 assert(Term && Term->isConditional() && "Terminator is not conditional");
866
867 const SCEV *LHS = nullptr;
868 const SCEV *RHS = nullptr;
869 Value *Cond = Term->getCondition();
870 CmpInst::Predicate Pred = CmpInst::Predicate::BAD_ICMP_PREDICATE;
871
872 ICmpInst *CondICmpInst = dyn_cast<ICmpInst>(Cond);
873 ConstantInt *CondConstant = dyn_cast<ConstantInt>(Cond);
874 if (CondICmpInst) {
875 LHS = SE->getSCEVAtScope(CondICmpInst->getOperand(0), L);
876 RHS = SE->getSCEVAtScope(CondICmpInst->getOperand(1), L);
877 Pred = CondICmpInst->getPredicate();
878 } else if (CondConstant) {
879 LHS = SE->getConstant(CondConstant);
880 RHS = SE->getConstant(ConstantInt::getTrue(SE->getContext()));
881 Pred = CmpInst::Predicate::ICMP_EQ;
882 } else {
883 llvm_unreachable("Condition is neither a ConstantInt nor a ICmpInst");
884 }
885
886 if (!L->contains(Term->getSuccessor(0)))
887 Pred = ICmpInst::getInversePredicate(Pred);
888 Comparison Comp(LHS, RHS, Pred);
889
Johannes Doerfert96425c22015-08-30 21:13:53 +0000890 isl_pw_aff *LPWA = getPwAff(Comp.getLHS());
891 isl_pw_aff *RPWA = getPwAff(Comp.getRHS());
892
893 isl_set *CondSet = buildConditionSet(Comp.getPred(), LPWA, RPWA);
Johannes Doerfertd020b772015-08-27 06:53:52 +0000894 isl_map *ForwardMap = isl_map_lex_le(isl_space_copy(DomSpace));
895 for (unsigned i = 0; i < isl_set_n_dim(Domain); i++)
896 if (i != loopDimension)
897 ForwardMap = isl_map_equate(ForwardMap, isl_dim_in, i, isl_dim_out, i);
898
899 ForwardMap = isl_map_apply_range(ForwardMap, isl_map_copy(TranslationMap));
900 isl_set *CondDom = isl_set_subtract(isl_set_copy(Domain), CondSet);
901 isl_set *ForwardCond = isl_set_apply(CondDom, isl_map_copy(ForwardMap));
902 isl_set *ForwardDomain = isl_set_apply(isl_set_copy(Domain), ForwardMap);
903 ForwardCond = isl_set_gist(ForwardCond, ForwardDomain);
904 Domain = isl_set_subtract(Domain, ForwardCond);
905
906 isl_map_free(TranslationMap);
907 isl_space_free(DomSpace);
908}
909
Johannes Doerfert45545ff2015-08-16 14:36:01 +0000910void ScopStmt::addLoopBoundsToDomain(TempScop &tempScop) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000911 isl_space *Space;
912 isl_local_space *LocalSpace;
Tobias Grosser75805372011-04-29 06:27:02 +0000913
Tobias Grossere19661e2011-10-07 08:46:57 +0000914 Space = isl_set_get_space(Domain);
915 LocalSpace = isl_local_space_from_space(Space);
Tobias Grosserf5338802011-10-06 00:03:35 +0000916
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000917 ScalarEvolution *SE = getParent()->getSE();
Tobias Grosser75805372011-04-29 06:27:02 +0000918 for (int i = 0, e = getNumIterators(); i != e; ++i) {
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000919 isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace));
Tobias Grosserabfbe632013-02-05 12:09:06 +0000920 isl_pw_aff *IV =
921 isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1));
Tobias Grosser75805372011-04-29 06:27:02 +0000922
Tobias Grosser9b13d3d2011-10-06 22:32:58 +0000923 // 0 <= IV.
924 isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV));
925 Domain = isl_set_intersect(Domain, LowerBound);
926
927 // IV <= LatchExecutions.
Hongbin Zheng27f3afb2011-04-30 03:26:51 +0000928 const Loop *L = getLoopForDimension(i);
Johannes Doerfert5ad8a6a2014-11-01 01:14:56 +0000929 const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L);
Johannes Doerfertd020b772015-08-27 06:53:52 +0000930 if (!isa<SCEVCouldNotCompute>(LatchExecutions)) {
931 isl_pw_aff *UpperBound = getPwAff(LatchExecutions);
932 isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound);
933 Domain = isl_set_intersect(Domain, UpperBoundSet);
934 } else {
Johannes Doerfert5f912d32015-08-31 19:58:24 +0000935 // If SCEV cannot provide a loop trip count, we compute it with ISL. If
936 // the domain remains unbounded, make the assumed context infeasible
937 // as code generation currently does not expect unbounded loops.
Johannes Doerfertd020b772015-08-27 06:53:52 +0000938 addLoopTripCountToDomain(L);
939 isl_pw_aff_free(IV);
Johannes Doerfert5f912d32015-08-31 19:58:24 +0000940 if (!isl_set_dim_has_upper_bound(Domain, isl_dim_set, i))
941 Parent.addAssumption(isl_set_empty(Parent.getParamSpace()));
Johannes Doerfertd020b772015-08-27 06:53:52 +0000942 }
Tobias Grosser75805372011-04-29 06:27:02 +0000943 }
944
Tobias Grosserf5338802011-10-06 00:03:35 +0000945 isl_local_space_free(LocalSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000946}
947
Johannes Doerfert45545ff2015-08-16 14:36:01 +0000948void ScopStmt::buildDomain(TempScop &tempScop, const Region &CurRegion) {
Tobias Grossere19661e2011-10-07 08:46:57 +0000949 isl_space *Space;
Tobias Grosser084d8f72012-05-29 09:29:44 +0000950 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +0000951
952 Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators());
953
Tobias Grosser084d8f72012-05-29 09:29:44 +0000954 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
955
Tobias Grossere19661e2011-10-07 08:46:57 +0000956 Domain = isl_set_universe(Space);
Johannes Doerfert45545ff2015-08-16 14:36:01 +0000957 addLoopBoundsToDomain(tempScop);
Johannes Doerfert96425c22015-08-30 21:13:53 +0000958 Domain = isl_set_intersect(Domain, getParent()->getDomainConditions(this));
959 Domain = isl_set_coalesce(Domain);
Tobias Grosser084d8f72012-05-29 09:29:44 +0000960 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grosser75805372011-04-29 06:27:02 +0000961}
962
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000963void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
964 int Dimension = 0;
965 isl_ctx *Ctx = Parent.getIslCtx();
966 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
967 Type *Ty = GEP->getPointerOperandType();
968 ScalarEvolution &SE = *Parent.getSE();
969
970 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
971 Dimension = 1;
972 Ty = PtrTy->getElementType();
973 }
974
975 while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
976 unsigned int Operand = 1 + Dimension;
977
978 if (GEP->getNumOperands() <= Operand)
979 break;
980
981 const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand));
982
983 if (isAffineExpr(&Parent.getRegion(), Expr, SE)) {
Johannes Doerfert574182d2015-08-12 10:19:50 +0000984 isl_pw_aff *AccessOffset = getPwAff(Expr);
Tobias Grosser7b50bee2014-11-25 10:51:12 +0000985 AccessOffset =
986 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
987
988 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
989 isl_local_space_copy(LSpace),
990 isl_val_int_from_si(Ctx, ArrayTy->getNumElements())));
991
992 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
993 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
994 OutOfBound = isl_set_params(OutOfBound);
995 isl_set *InBound = isl_set_complement(OutOfBound);
996 isl_set *Executed = isl_set_params(getDomain());
997
998 // A => B == !A or B
999 isl_set *InBoundIfExecuted =
1000 isl_set_union(isl_set_complement(Executed), InBound);
1001
1002 Parent.addAssumption(InBoundIfExecuted);
1003 }
1004
1005 Dimension += 1;
1006 Ty = ArrayTy->getElementType();
1007 }
1008
1009 isl_local_space_free(LSpace);
1010}
1011
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001012void ScopStmt::deriveAssumptions(BasicBlock *Block) {
1013 for (Instruction &Inst : *Block)
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001014 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
1015 deriveAssumptionsFromGEP(GEP);
1016}
1017
Tobias Grosser74394f02013-01-14 22:40:23 +00001018ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001019 Region &R, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001020 : Parent(parent), BB(nullptr), R(&R), Build(nullptr),
1021 NestLoops(Nest.size()) {
1022 // Setup the induction variables.
1023 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
1024 NestLoops[i] = Nest[i];
1025
Tobias Grosser16c44032015-07-09 07:31:45 +00001026 BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), "");
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001027
Johannes Doerfert45545ff2015-08-16 14:36:01 +00001028 buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001029
1030 BasicBlock *EntryBB = R.getEntry();
1031 for (BasicBlock *Block : R.blocks()) {
1032 buildAccesses(tempScop, Block, Block != EntryBB);
1033 deriveAssumptions(Block);
1034 }
Tobias Grosserd83b8a82015-08-20 19:08:11 +00001035 if (DetectReductions)
1036 checkForReductions();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001037}
1038
1039ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion,
Tobias Grosser808cd692015-07-14 09:33:13 +00001040 BasicBlock &bb, SmallVectorImpl<Loop *> &Nest)
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001041 : Parent(parent), BB(&bb), R(nullptr), Build(nullptr),
1042 NestLoops(Nest.size()) {
Tobias Grosser75805372011-04-29 06:27:02 +00001043 // Setup the induction variables.
Tobias Grosser683b8e42014-11-30 14:33:31 +00001044 for (unsigned i = 0, e = Nest.size(); i < e; ++i)
Sebastian Pop860e0212013-02-15 21:26:44 +00001045 NestLoops[i] = Nest[i];
Tobias Grosser75805372011-04-29 06:27:02 +00001046
Johannes Doerfert79fc23f2014-07-24 23:48:02 +00001047 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Tobias Grosser75805372011-04-29 06:27:02 +00001048
Johannes Doerfert45545ff2015-08-16 14:36:01 +00001049 buildDomain(tempScop, CurRegion);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001050 buildAccesses(tempScop, BB);
1051 deriveAssumptions(BB);
Tobias Grosserd83b8a82015-08-20 19:08:11 +00001052 if (DetectReductions)
1053 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001054}
1055
Johannes Doerferte58a0122014-06-27 20:31:28 +00001056/// @brief Collect loads which might form a reduction chain with @p StoreMA
1057///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001058/// Check if the stored value for @p StoreMA is a binary operator with one or
1059/// two loads as operands. If the binary operand is commutative & associative,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001060/// used only once (by @p StoreMA) and its load operands are also used only
1061/// once, we have found a possible reduction chain. It starts at an operand
1062/// load and includes the binary operator and @p StoreMA.
1063///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001064/// Note: We allow only one use to ensure the load and binary operator cannot
Johannes Doerferte58a0122014-06-27 20:31:28 +00001065/// escape this block or into any other store except @p StoreMA.
1066void ScopStmt::collectCandiateReductionLoads(
1067 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
1068 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
1069 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001070 return;
1071
1072 // Skip if there is not one binary operator between the load and the store
1073 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +00001074 if (!BinOp)
1075 return;
1076
1077 // Skip if the binary operators has multiple uses
1078 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001079 return;
1080
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001081 // Skip if the opcode of the binary operator is not commutative/associative
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001082 if (!BinOp->isCommutative() || !BinOp->isAssociative())
1083 return;
1084
Johannes Doerfert9890a052014-07-01 00:32:29 +00001085 // Skip if the binary operator is outside the current SCoP
1086 if (BinOp->getParent() != Store->getParent())
1087 return;
1088
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001089 // Skip if it is a multiplicative reduction and we disabled them
1090 if (DisableMultiplicativeReductions &&
1091 (BinOp->getOpcode() == Instruction::Mul ||
1092 BinOp->getOpcode() == Instruction::FMul))
1093 return;
1094
Johannes Doerferte58a0122014-06-27 20:31:28 +00001095 // Check the binary operator operands for a candidate load
1096 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
1097 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
1098 if (!PossibleLoad0 && !PossibleLoad1)
1099 return;
1100
1101 // A load is only a candidate if it cannot escape (thus has only this use)
1102 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001103 if (PossibleLoad0->getParent() == Store->getParent())
1104 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001105 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001106 if (PossibleLoad1->getParent() == Store->getParent())
1107 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001108}
1109
1110/// @brief Check for reductions in this ScopStmt
1111///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001112/// Iterate over all store memory accesses and check for valid binary reduction
1113/// like chains. For all candidates we check if they have the same base address
1114/// and there are no other accesses which overlap with them. The base address
1115/// check rules out impossible reductions candidates early. The overlap check,
1116/// together with the "only one user" check in collectCandiateReductionLoads,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001117/// guarantees that none of the intermediate results will escape during
1118/// execution of the loop nest. We basically check here that no other memory
1119/// access can access the same memory as the potential reduction.
1120void ScopStmt::checkForReductions() {
1121 SmallVector<MemoryAccess *, 2> Loads;
1122 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1123
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001124 // First collect candidate load-store reduction chains by iterating over all
Johannes Doerferte58a0122014-06-27 20:31:28 +00001125 // stores and collecting possible reduction loads.
1126 for (MemoryAccess *StoreMA : MemAccs) {
1127 if (StoreMA->isRead())
1128 continue;
1129
1130 Loads.clear();
1131 collectCandiateReductionLoads(StoreMA, Loads);
1132 for (MemoryAccess *LoadMA : Loads)
1133 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1134 }
1135
1136 // Then check each possible candidate pair.
1137 for (const auto &CandidatePair : Candidates) {
1138 bool Valid = true;
1139 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1140 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1141
1142 // Skip those with obviously unequal base addresses.
1143 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1144 isl_map_free(LoadAccs);
1145 isl_map_free(StoreAccs);
1146 continue;
1147 }
1148
1149 // And check if the remaining for overlap with other memory accesses.
1150 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1151 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1152 isl_set *AllAccs = isl_map_range(AllAccsRel);
1153
1154 for (MemoryAccess *MA : MemAccs) {
1155 if (MA == CandidatePair.first || MA == CandidatePair.second)
1156 continue;
1157
1158 isl_map *AccRel =
1159 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1160 isl_set *Accs = isl_map_range(AccRel);
1161
1162 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1163 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1164 Valid = Valid && isl_set_is_empty(OverlapAccs);
1165 isl_set_free(OverlapAccs);
1166 }
1167 }
1168
1169 isl_set_free(AllAccs);
1170 if (!Valid)
1171 continue;
1172
Johannes Doerfertf6183392014-07-01 20:52:51 +00001173 const LoadInst *Load =
1174 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1175 MemoryAccess::ReductionType RT =
1176 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1177
Johannes Doerferte58a0122014-06-27 20:31:28 +00001178 // If no overlapping access was found we mark the load and store as
1179 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001180 CandidatePair.first->markAsReductionLike(RT);
1181 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001182 }
Tobias Grosser75805372011-04-29 06:27:02 +00001183}
1184
Tobias Grosser74394f02013-01-14 22:40:23 +00001185std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001186
Tobias Grosser54839312015-04-21 11:37:25 +00001187std::string ScopStmt::getScheduleStr() const {
Tobias Grosser808cd692015-07-14 09:33:13 +00001188 auto *S = getSchedule();
1189 auto Str = stringFromIslObj(S);
1190 isl_map_free(S);
1191 return Str;
Tobias Grosser75805372011-04-29 06:27:02 +00001192}
1193
Tobias Grosser74394f02013-01-14 22:40:23 +00001194unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001195
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001196unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001197
Tobias Grosser75805372011-04-29 06:27:02 +00001198const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1199
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001200const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001201 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001202}
1203
Tobias Grosser74394f02013-01-14 22:40:23 +00001204isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001205
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001206__isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001207
Tobias Grosser6e6c7e02015-03-30 12:22:39 +00001208__isl_give isl_space *ScopStmt::getDomainSpace() const {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001209 return isl_set_get_space(Domain);
1210}
1211
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001212__isl_give isl_id *ScopStmt::getDomainId() const {
1213 return isl_set_get_tuple_id(Domain);
1214}
Tobias Grossercd95b772012-08-30 11:49:38 +00001215
Tobias Grosser75805372011-04-29 06:27:02 +00001216ScopStmt::~ScopStmt() {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001217 DeleteContainerSeconds(InstructionToAccess);
Tobias Grosser75805372011-04-29 06:27:02 +00001218 isl_set_free(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +00001219}
1220
1221void ScopStmt::print(raw_ostream &OS) const {
1222 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001223 OS.indent(12) << "Domain :=\n";
1224
1225 if (Domain) {
1226 OS.indent(16) << getDomainStr() << ";\n";
1227 } else
1228 OS.indent(16) << "n/a\n";
1229
Tobias Grosser54839312015-04-21 11:37:25 +00001230 OS.indent(12) << "Schedule :=\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001231
1232 if (Domain) {
Tobias Grosser54839312015-04-21 11:37:25 +00001233 OS.indent(16) << getScheduleStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001234 } else
1235 OS.indent(16) << "n/a\n";
1236
Tobias Grosser083d3d32014-06-28 08:59:45 +00001237 for (MemoryAccess *Access : MemAccs)
1238 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001239}
1240
1241void ScopStmt::dump() const { print(dbgs()); }
1242
1243//===----------------------------------------------------------------------===//
1244/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001245
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001246void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001247 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1248 isl_set_free(Context);
1249 Context = NewContext;
1250}
1251
Tobias Grosserabfbe632013-02-05 12:09:06 +00001252void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001253 for (const SCEV *Parameter : NewParameters) {
Johannes Doerfertbe409962015-03-29 20:45:09 +00001254 Parameter = extractConstantFactor(Parameter, *SE).second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001255 if (ParameterIds.find(Parameter) != ParameterIds.end())
1256 continue;
1257
1258 int dimension = Parameters.size();
1259
1260 Parameters.push_back(Parameter);
1261 ParameterIds[Parameter] = dimension;
1262 }
1263}
1264
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001265__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
1266 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001267
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001268 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001269 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001270
Tobias Grosser8f99c162011-11-15 11:38:55 +00001271 std::string ParameterName;
1272
1273 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1274 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001275 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001276 }
1277
1278 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001279 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001280
Tobias Grosser20532b82014-04-11 17:56:49 +00001281 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1282 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001283}
Tobias Grosser75805372011-04-29 06:27:02 +00001284
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00001285isl_set *Scop::addNonEmptyDomainConstraints(isl_set *C) const {
1286 isl_set *DomainContext = isl_union_set_params(getDomains());
1287 return isl_set_intersect_params(C, DomainContext);
1288}
1289
Tobias Grosser8a9c2352015-08-16 10:19:29 +00001290void Scop::addUserContext() {
1291 if (UserContextStr.empty())
1292 return;
1293
1294 isl_set *UserContext = isl_set_read_from_str(IslCtx, UserContextStr.c_str());
1295 isl_space *Space = getParamSpace();
1296 if (isl_space_dim(Space, isl_dim_param) !=
1297 isl_set_dim(UserContext, isl_dim_param)) {
1298 auto SpaceStr = isl_space_to_str(Space);
1299 errs() << "Error: the context provided in -polly-context has not the same "
1300 << "number of dimensions than the computed context. Due to this "
1301 << "mismatch, the -polly-context option is ignored. Please provide "
1302 << "the context in the parameter space: " << SpaceStr << ".\n";
1303 free(SpaceStr);
1304 isl_set_free(UserContext);
1305 isl_space_free(Space);
1306 return;
1307 }
1308
1309 for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
1310 auto NameContext = isl_set_get_dim_name(Context, isl_dim_param, i);
1311 auto NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i);
1312
1313 if (strcmp(NameContext, NameUserContext) != 0) {
1314 auto SpaceStr = isl_space_to_str(Space);
1315 errs() << "Error: the name of dimension " << i
1316 << " provided in -polly-context "
1317 << "is '" << NameUserContext << "', but the name in the computed "
1318 << "context is '" << NameContext
1319 << "'. Due to this name mismatch, "
1320 << "the -polly-context option is ignored. Please provide "
1321 << "the context in the parameter space: " << SpaceStr << ".\n";
1322 free(SpaceStr);
1323 isl_set_free(UserContext);
1324 isl_space_free(Space);
1325 return;
1326 }
1327
1328 UserContext =
1329 isl_set_set_dim_id(UserContext, isl_dim_param, i,
1330 isl_space_get_dim_id(Space, isl_dim_param, i));
1331 }
1332
1333 Context = isl_set_intersect(Context, UserContext);
1334 isl_space_free(Space);
1335}
1336
Tobias Grosser6be480c2011-11-08 15:41:13 +00001337void Scop::buildContext() {
1338 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001339 Context = isl_set_universe(isl_space_copy(Space));
1340 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001341}
1342
Tobias Grosser18daaca2012-05-22 10:47:27 +00001343void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001344 for (const auto &ParamID : ParameterIds) {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001345 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001346
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001347 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001348
Johannes Doerferte7044942015-02-24 11:58:30 +00001349 Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001350 }
1351}
1352
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001353void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001354 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001355 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001356
Tobias Grosser083d3d32014-06-28 08:59:45 +00001357 for (const auto &ParamID : ParameterIds) {
1358 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001359 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001360 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001361 }
1362
1363 // Align the parameters of all data structures to the model.
1364 Context = isl_set_align_params(Context, Space);
1365
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001366 for (ScopStmt &Stmt : *this)
1367 Stmt.realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001368}
1369
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001370void Scop::simplifyAssumedContext() {
1371 // The parameter constraints of the iteration domains give us a set of
1372 // constraints that need to hold for all cases where at least a single
1373 // statement iteration is executed in the whole scop. We now simplify the
1374 // assumed context under the assumption that such constraints hold and at
1375 // least a single statement iteration is executed. For cases where no
1376 // statement instances are executed, the assumptions we have taken about
1377 // the executed code do not matter and can be changed.
1378 //
1379 // WARNING: This only holds if the assumptions we have taken do not reduce
1380 // the set of statement instances that are executed. Otherwise we
1381 // may run into a case where the iteration domains suggest that
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001382 // for a certain set of parameter constraints no code is executed,
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001383 // but in the original program some computation would have been
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001384 // performed. In such a case, modifying the run-time conditions and
1385 // possibly influencing the run-time check may cause certain scops
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001386 // to not be executed.
1387 //
1388 // Example:
1389 //
1390 // When delinearizing the following code:
1391 //
1392 // for (long i = 0; i < 100; i++)
1393 // for (long j = 0; j < m; j++)
1394 // A[i+p][j] = 1.0;
1395 //
1396 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001397 // otherwise we would access out of bound data. Now, knowing that code is
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001398 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
1399 AssumedContext =
1400 isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains()));
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001401 AssumedContext = isl_set_gist_params(AssumedContext, getContext());
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001402}
1403
Johannes Doerfertb164c792014-09-18 11:17:17 +00001404/// @brief Add the minimal/maximal access in @p Set to @p User.
Tobias Grosserb2f39922015-05-28 13:32:11 +00001405static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001406 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1407 isl_pw_multi_aff *MinPMA, *MaxPMA;
1408 isl_pw_aff *LastDimAff;
1409 isl_aff *OneAff;
1410 unsigned Pos;
1411
Johannes Doerfert9143d672014-09-27 11:02:39 +00001412 // Restrict the number of parameters involved in the access as the lexmin/
1413 // lexmax computation will take too long if this number is high.
1414 //
1415 // Experiments with a simple test case using an i7 4800MQ:
1416 //
1417 // #Parameters involved | Time (in sec)
1418 // 6 | 0.01
1419 // 7 | 0.04
1420 // 8 | 0.12
1421 // 9 | 0.40
1422 // 10 | 1.54
1423 // 11 | 6.78
1424 // 12 | 30.38
1425 //
1426 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1427 unsigned InvolvedParams = 0;
1428 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1429 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1430 InvolvedParams++;
1431
1432 if (InvolvedParams > RunTimeChecksMaxParameters) {
1433 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001434 return isl_stat_error;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001435 }
1436 }
1437
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001438 Set = isl_set_remove_divs(Set);
1439
Johannes Doerfertb164c792014-09-18 11:17:17 +00001440 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1441 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1442
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001443 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1444 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1445
Johannes Doerfertb164c792014-09-18 11:17:17 +00001446 // Adjust the last dimension of the maximal access by one as we want to
1447 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1448 // we test during code generation might now point after the end of the
1449 // allocated array but we will never dereference it anyway.
1450 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1451 "Assumed at least one output dimension");
1452 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1453 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1454 OneAff = isl_aff_zero_on_domain(
1455 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1456 OneAff = isl_aff_add_constant_si(OneAff, 1);
1457 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1458 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1459
1460 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1461
1462 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001463 return isl_stat_ok;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001464}
1465
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001466static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1467 isl_set *Domain = MA->getStatement()->getDomain();
1468 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1469 return isl_set_reset_tuple_id(Domain);
1470}
1471
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001472/// @brief Wrapper function to calculate minimal/maximal accesses to each array.
1473static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses,
Tobias Grosserbb853c22015-07-25 12:31:03 +00001474 __isl_take isl_union_set *Domains,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001475 Scop::MinMaxVectorTy &MinMaxAccesses) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001476
1477 Accesses = isl_union_map_intersect_domain(Accesses, Domains);
1478 isl_union_set *Locations = isl_union_map_range(Accesses);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001479 Locations = isl_union_set_coalesce(Locations);
1480 Locations = isl_union_set_detect_equalities(Locations);
1481 bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001482 &MinMaxAccesses));
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001483 isl_union_set_free(Locations);
1484 return Valid;
1485}
1486
Johannes Doerfert96425c22015-08-30 21:13:53 +00001487/// @brief Helper to treat non-affine regions and basic blocks the same.
1488///
1489///{
1490
1491/// @brief Return the block that is the representing block for @p RN.
1492static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) {
1493 return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry()
1494 : RN->getNodeAs<BasicBlock>();
1495}
1496
1497/// @brief Return the @p idx'th block that is executed after @p RN.
1498static inline BasicBlock *getRegionNodeSuccessor(RegionNode *RN, BranchInst *BI,
1499 unsigned idx) {
1500 if (RN->isSubRegion()) {
1501 assert(idx == 0);
1502 return RN->getNodeAs<Region>()->getExit();
1503 }
1504 return BI->getSuccessor(idx);
1505}
1506
1507/// @brief Return the smallest loop surrounding @p RN.
1508static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) {
1509 if (!RN->isSubRegion())
1510 return LI.getLoopFor(RN->getNodeAs<BasicBlock>());
1511
1512 Region *NonAffineSubRegion = RN->getNodeAs<Region>();
1513 Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry());
1514 while (L && NonAffineSubRegion->contains(L))
1515 L = L->getParentLoop();
1516 return L;
1517}
1518
1519///}
1520
1521isl_set *Scop::getDomainConditions(ScopStmt *Stmt) {
1522 BasicBlock *BB = Stmt->isBlockStmt() ? Stmt->getBasicBlock()
1523 : Stmt->getRegion()->getEntry();
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001524 return isl_set_copy(DomainMap[BB]);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001525}
1526
1527void Scop::buildDomains(Region *R, LoopInfo &LI, ScopDetection &SD,
1528 DominatorTree &DT) {
1529
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001530 auto *EntryBB = R->getEntry();
1531 int LD = getRelativeLoopDepth(LI.getLoopFor(EntryBB));
1532 auto *S = isl_set_universe(isl_space_set_alloc(getIslCtx(), 0, LD + 1));
1533 DomainMap[EntryBB] = S;
Johannes Doerfert96425c22015-08-30 21:13:53 +00001534
1535 buildDomainsWithBranchConstraints(R, LI, SD, DT);
1536}
1537
1538void Scop::buildDomainsWithBranchConstraints(Region *R, LoopInfo &LI,
1539 ScopDetection &SD,
1540 DominatorTree &DT) {
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001541 RegionInfo &RI = *R->getRegionInfo();
Johannes Doerfert96425c22015-08-30 21:13:53 +00001542
1543 // To create the domain for each block in R we iterate over all blocks and
1544 // subregions in R and propagate the conditions under which the current region
1545 // element is executed. To this end we iterate in reverse post order over R as
1546 // it ensures that we first visit all predecessors of a region node (either a
1547 // basic block or a subregion) before we visit the region node itself.
1548 // Initially, only the domain for the SCoP region entry block is set and from
1549 // there we propagate the current domain to all successors, however we add the
1550 // condition that the successor is actually executed next.
1551 // As we are only interested in non-loop carried constraints here we can
1552 // simply skip loop back edges.
1553
1554 ReversePostOrderTraversal<Region *> RTraversal(R);
1555 for (auto *RN : RTraversal) {
1556
1557 // Recurse for affine subregions but go on for basic blocks and non-affine
1558 // subregions.
1559 if (RN->isSubRegion()) {
1560 Region *SubRegion = RN->getNodeAs<Region>();
1561 if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) {
1562 buildDomainsWithBranchConstraints(SubRegion, LI, SD, DT);
1563 continue;
1564 }
1565 }
1566
1567 BasicBlock *BB = getRegionNodeBasicBlock(RN);
1568 isl_set *Domain = DomainMap[BB];
1569 DEBUG(dbgs() << "\tVisit: " << BB->getName() << " : " << Domain << "\n");
1570 assert(Domain && "Due to reverse post order traversal of the region all "
1571 "predecessor of the current region node should have been "
1572 "visited and a domain for this region node should have "
1573 "been set.");
1574
1575 Loop *BBLoop = getRegionNodeLoop(RN, LI);
1576 int BBLoopDepth = getRelativeLoopDepth(BBLoop);
1577
1578 // Build the condition sets for the successor nodes of the current region
1579 // node. If it is a non-affine subregion we will always execute the single
1580 // exit node, hence the single entry node domain is the condition set. For
1581 // basic blocks we use the helper function buildConditionSets.
1582 SmallVector<isl_set *, 2> ConditionSets;
1583 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1584 if (RN->isSubRegion())
1585 ConditionSets.push_back(isl_set_copy(Domain));
1586 else
1587 buildConditionSets(*this, BI, BBLoop, Domain, ConditionSets);
1588
1589 // Now iterate over the successors and set their initial domain based on
1590 // their condition set. We skip back edges here and have to be careful when
1591 // we leave a loop not to keep constraints over a dimension that doesn't
1592 // exist anymore.
1593 for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) {
1594 BasicBlock *SuccBB = getRegionNodeSuccessor(RN, BI, u);
1595 isl_set *CondSet = ConditionSets[u];
1596
1597 // Skip back edges.
1598 if (DT.dominates(SuccBB, BB)) {
1599 isl_set_free(CondSet);
1600 continue;
1601 }
1602
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001603 // Do not adjust the number of dimensions if we enter a boxed loop or are
1604 // in a non-affine subregion or if the surrounding loop stays the same.
Johannes Doerfert96425c22015-08-30 21:13:53 +00001605 Loop *SuccBBLoop = LI.getLoopFor(SuccBB);
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001606 Region *SuccRegion = RI.getRegionFor(SuccBB);
1607 if (BBLoop != SuccBBLoop && !RN->isSubRegion() &&
1608 !(SD.isNonAffineSubRegion(SuccRegion, &getRegion()) &&
1609 SuccRegion->contains(SuccBBLoop))) {
1610
1611 // Check if the edge to SuccBB is a loop entry or exit edge. If so
1612 // adjust the dimensionality accordingly. Lastly, if we leave a loop
1613 // and enter a new one we need to drop the old constraints.
1614 int SuccBBLoopDepth = getRelativeLoopDepth(SuccBBLoop);
1615 assert(std::abs(BBLoopDepth - SuccBBLoopDepth) <= 1);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001616 if (BBLoopDepth > SuccBBLoopDepth) {
1617 CondSet = isl_set_project_out(CondSet, isl_dim_set, BBLoopDepth, 1);
1618 } else if (SuccBBLoopDepth > BBLoopDepth) {
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001619 CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001620 } else if (BBLoopDepth >= 0) {
1621 CondSet = isl_set_project_out(CondSet, isl_dim_set, BBLoopDepth, 1);
1622 CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1);
1623 }
Johannes Doerfert96425c22015-08-30 21:13:53 +00001624 }
1625
1626 // Set the domain for the successor or merge it with an existing domain in
1627 // case there are multiple paths (without loop back edges) to the
1628 // successor block.
1629 isl_set *&SuccDomain = DomainMap[SuccBB];
1630 if (!SuccDomain)
1631 SuccDomain = CondSet;
1632 else
1633 SuccDomain = isl_set_union(SuccDomain, CondSet);
1634
1635 SuccDomain = isl_set_coalesce(SuccDomain);
1636 DEBUG(dbgs() << "\tSet SuccBB: " << SuccBB->getName() << " : " << Domain
1637 << "\n");
1638 }
1639 }
1640}
1641
Johannes Doerfert120de4b2015-08-20 18:30:08 +00001642void Scop::buildAliasChecks(AliasAnalysis &AA) {
1643 if (!PollyUseRuntimeAliasChecks)
1644 return;
1645
1646 if (buildAliasGroups(AA))
1647 return;
1648
1649 // If a problem occurs while building the alias groups we need to delete
1650 // this SCoP and pretend it wasn't valid in the first place. To this end
1651 // we make the assumed context infeasible.
1652 addAssumption(isl_set_empty(getParamSpace()));
1653
1654 DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr()
1655 << " could not be created as the number of parameters involved "
1656 "is too high. The SCoP will be "
1657 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust "
1658 "the maximal number of parameters but be advised that the "
1659 "compile time might increase exponentially.\n\n");
1660}
1661
Johannes Doerfert9143d672014-09-27 11:02:39 +00001662bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001663 // To create sound alias checks we perform the following steps:
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001664 // o) Use the alias analysis and an alias set tracker to build alias sets
Johannes Doerfertb164c792014-09-18 11:17:17 +00001665 // for all memory accesses inside the SCoP.
1666 // o) For each alias set we then map the aliasing pointers back to the
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001667 // memory accesses we know, thus obtain groups of memory accesses which
Johannes Doerfertb164c792014-09-18 11:17:17 +00001668 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001669 // o) We divide each group based on the domains of the minimal/maximal
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001670 // accesses. That means two minimal/maximal accesses are only in a group
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001671 // if their access domains intersect, otherwise they are in different
1672 // ones.
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001673 // o) We partition each group into read only and non read only accesses.
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001674 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001675 // and maximal accesses to each array of a group in read only and non
1676 // read only partitions separately.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001677 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
1678
1679 AliasSetTracker AST(AA);
1680
1681 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00001682 DenseSet<Value *> HasWriteAccess;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001683 for (ScopStmt &Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001684
1685 // Skip statements with an empty domain as they will never be executed.
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001686 isl_set *StmtDomain = Stmt.getDomain();
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00001687 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
1688 isl_set_free(StmtDomain);
1689 if (StmtDomainEmpty)
1690 continue;
1691
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001692 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001693 if (MA->isScalar())
1694 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00001695 if (!MA->isRead())
1696 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001697 Instruction *Acc = MA->getAccessInstruction();
1698 PtrToAcc[getPointerOperand(*Acc)] = MA;
1699 AST.add(Acc);
1700 }
1701 }
1702
1703 SmallVector<AliasGroupTy, 4> AliasGroups;
1704 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00001705 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00001706 continue;
1707 AliasGroupTy AG;
1708 for (auto PR : AS)
1709 AG.push_back(PtrToAcc[PR.getValue()]);
1710 assert(AG.size() > 1 &&
1711 "Alias groups should contain at least two accesses");
1712 AliasGroups.push_back(std::move(AG));
1713 }
1714
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001715 // Split the alias groups based on their domain.
1716 for (unsigned u = 0; u < AliasGroups.size(); u++) {
1717 AliasGroupTy NewAG;
1718 AliasGroupTy &AG = AliasGroups[u];
1719 AliasGroupTy::iterator AGI = AG.begin();
1720 isl_set *AGDomain = getAccessDomain(*AGI);
1721 while (AGI != AG.end()) {
1722 MemoryAccess *MA = *AGI;
1723 isl_set *MADomain = getAccessDomain(MA);
1724 if (isl_set_is_disjoint(AGDomain, MADomain)) {
1725 NewAG.push_back(MA);
1726 AGI = AG.erase(AGI);
1727 isl_set_free(MADomain);
1728 } else {
1729 AGDomain = isl_set_union(AGDomain, MADomain);
1730 AGI++;
1731 }
1732 }
1733 if (NewAG.size() > 1)
1734 AliasGroups.push_back(std::move(NewAG));
1735 isl_set_free(AGDomain);
1736 }
1737
Tobias Grosserf4c24b22015-04-05 13:11:54 +00001738 MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
Johannes Doerfert13771732014-10-01 12:40:46 +00001739 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
1740 for (AliasGroupTy &AG : AliasGroups) {
1741 NonReadOnlyBaseValues.clear();
1742 ReadOnlyPairs.clear();
1743
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001744 if (AG.size() < 2) {
1745 AG.clear();
1746 continue;
1747 }
1748
Johannes Doerfert13771732014-10-01 12:40:46 +00001749 for (auto II = AG.begin(); II != AG.end();) {
1750 Value *BaseAddr = (*II)->getBaseAddr();
1751 if (HasWriteAccess.count(BaseAddr)) {
1752 NonReadOnlyBaseValues.insert(BaseAddr);
1753 II++;
1754 } else {
1755 ReadOnlyPairs[BaseAddr].insert(*II);
1756 II = AG.erase(II);
1757 }
1758 }
1759
1760 // If we don't have read only pointers check if there are at least two
1761 // non read only pointers, otherwise clear the alias group.
Tobias Grosserbb853c22015-07-25 12:31:03 +00001762 if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001763 AG.clear();
Johannes Doerfert13771732014-10-01 12:40:46 +00001764 continue;
1765 }
1766
1767 // If we don't have non read only pointers clear the alias group.
1768 if (NonReadOnlyBaseValues.empty()) {
1769 AG.clear();
1770 continue;
1771 }
1772
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001773 // Calculate minimal and maximal accesses for non read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001774 MinMaxAliasGroups.emplace_back();
1775 MinMaxVectorPairTy &pair = MinMaxAliasGroups.back();
1776 MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first;
1777 MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second;
1778 MinMaxAccessesNonReadOnly.reserve(AG.size());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001779
1780 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001781
1782 // AG contains only non read only accesses.
Johannes Doerfertb164c792014-09-18 11:17:17 +00001783 for (MemoryAccess *MA : AG)
1784 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
Johannes Doerfertb164c792014-09-18 11:17:17 +00001785
Tobias Grosserdaaed0e2015-08-20 21:29:26 +00001786 bool Valid = calculateMinMaxAccess(Accesses, getDomains(),
1787 MinMaxAccessesNonReadOnly);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001788
1789 // Bail out if the number of values we need to compare is too large.
1790 // This is important as the number of comparisions grows quadratically with
1791 // the number of values we need to compare.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001792 if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() >
1793 RunTimeChecksMaxArraysPerGroup))
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001794 return false;
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001795
1796 // Calculate minimal and maximal accesses for read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001797 MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001798 Accesses = isl_union_map_empty(getParamSpace());
1799
1800 for (const auto &ReadOnlyPair : ReadOnlyPairs)
1801 for (MemoryAccess *MA : ReadOnlyPair.second)
1802 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
1803
Tobias Grosserdaaed0e2015-08-20 21:29:26 +00001804 Valid =
1805 calculateMinMaxAccess(Accesses, getDomains(), MinMaxAccessesReadOnly);
Johannes Doerfert9143d672014-09-27 11:02:39 +00001806
1807 if (!Valid)
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001808 return false;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001809 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00001810
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00001811 return true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001812}
1813
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001814static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI,
1815 ScopDetection &SD) {
1816
1817 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R);
1818
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001819 unsigned MinLD = INT_MAX, MaxLD = 0;
1820 for (BasicBlock *BB : R.blocks()) {
1821 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00001822 if (!R.contains(L))
1823 continue;
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00001824 if (BoxedLoops && BoxedLoops->count(L))
1825 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00001826 unsigned LD = L->getLoopDepth();
1827 MinLD = std::min(MinLD, LD);
1828 MaxLD = std::max(MaxLD, LD);
1829 }
1830 }
1831
1832 // Handle the case that there is no loop in the SCoP first.
1833 if (MaxLD == 0)
1834 return 1;
1835
1836 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
1837 assert(MaxLD >= MinLD &&
1838 "Maximal loop depth was smaller than mininaml loop depth?");
1839 return MaxLD - MinLD + 1;
1840}
1841
Johannes Doerfert96425c22015-08-30 21:13:53 +00001842Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, DominatorTree &DT,
1843 isl_ctx *Context, unsigned MaxLoopDepth)
1844 : DT(DT), SE(&ScalarEvolution), R(R), IsOptimized(false),
Johannes Doerfert717b8662015-09-08 21:44:27 +00001845 HasSingleExitEdge(R.getExitingBlock()), MaxLoopDepth(MaxLoopDepth),
1846 IslCtx(Context), Affinator(this) {}
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001847
Tobias Grosser40985012015-08-20 19:08:05 +00001848void Scop::initFromTempScop(TempScop &TempScop, LoopInfo &LI, ScopDetection &SD,
1849 AliasAnalysis &AA) {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001850 buildContext();
Tobias Grosser75805372011-04-29 06:27:02 +00001851
Johannes Doerfert96425c22015-08-30 21:13:53 +00001852 buildDomains(&R, LI, SD, DT);
1853
Tobias Grosserabfbe632013-02-05 12:09:06 +00001854 SmallVector<Loop *, 8> NestLoops;
Tobias Grosser75805372011-04-29 06:27:02 +00001855
Tobias Grosser54839312015-04-21 11:37:25 +00001856 // Build the iteration domain, access functions and schedule functions
Tobias Grosser75805372011-04-29 06:27:02 +00001857 // traversing the region tree.
Michael Kruse471a5e32015-07-30 19:27:04 +00001858 Schedule = buildScop(TempScop, getRegion(), NestLoops, LI, SD);
Tobias Grosser808cd692015-07-14 09:33:13 +00001859 if (!Schedule)
1860 Schedule = isl_schedule_empty(getParamSpace());
Tobias Grosser75805372011-04-29 06:27:02 +00001861
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001862 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00001863 addParameterBounds();
Tobias Grosser8a9c2352015-08-16 10:19:29 +00001864 addUserContext();
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001865 simplifyAssumedContext();
Johannes Doerfert120de4b2015-08-20 18:30:08 +00001866 buildAliasChecks(AA);
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001867
Tobias Grosser75805372011-04-29 06:27:02 +00001868 assert(NestLoops.empty() && "NestLoops not empty at top level!");
1869}
1870
Michael Kruse471a5e32015-07-30 19:27:04 +00001871Scop *Scop::createFromTempScop(TempScop &TempScop, LoopInfo &LI,
1872 ScalarEvolution &SE, ScopDetection &SD,
Johannes Doerfert96425c22015-08-30 21:13:53 +00001873 AliasAnalysis &AA, DominatorTree &DT,
1874 isl_ctx *ctx) {
Michael Kruse471a5e32015-07-30 19:27:04 +00001875 auto &R = TempScop.getMaxRegion();
1876 auto MaxLoopDepth = getMaxLoopDepthInRegion(R, LI, SD);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001877 auto S = new Scop(R, SE, DT, ctx, MaxLoopDepth);
Johannes Doerfert120de4b2015-08-20 18:30:08 +00001878 S->initFromTempScop(TempScop, LI, SD, AA);
1879
Michael Kruse471a5e32015-07-30 19:27:04 +00001880 return S;
1881}
1882
Tobias Grosser75805372011-04-29 06:27:02 +00001883Scop::~Scop() {
1884 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00001885 isl_set_free(AssumedContext);
Tobias Grosser808cd692015-07-14 09:33:13 +00001886 isl_schedule_free(Schedule);
Tobias Grosser75805372011-04-29 06:27:02 +00001887
Johannes Doerfert96425c22015-08-30 21:13:53 +00001888 for (auto It : DomainMap)
1889 isl_set_free(It.second);
1890
Johannes Doerfertb164c792014-09-18 11:17:17 +00001891 // Free the alias groups
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001892 for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001893 for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001894 isl_pw_multi_aff_free(MMA.first);
1895 isl_pw_multi_aff_free(MMA.second);
1896 }
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001897 for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001898 isl_pw_multi_aff_free(MMA.first);
1899 isl_pw_multi_aff_free(MMA.second);
1900 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00001901 }
Tobias Grosser75805372011-04-29 06:27:02 +00001902}
1903
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001904const ScopArrayInfo *
1905Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
Tobias Grosser92245222015-07-28 14:53:44 +00001906 const SmallVector<const SCEV *, 4> &Sizes,
1907 bool IsPHI) {
1908 auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)];
Johannes Doerfert80ef1102014-11-07 08:31:31 +00001909 if (!SAI)
Tobias Grosserd46fd5e2015-08-12 15:27:16 +00001910 SAI.reset(new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI,
1911 this));
Tobias Grosserab671442015-05-23 05:58:27 +00001912 return SAI.get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001913}
1914
Tobias Grosser92245222015-07-28 14:53:44 +00001915const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) {
1916 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00001917 assert(SAI && "No ScopArrayInfo available for this base pointer");
1918 return SAI;
1919}
1920
Tobias Grosser74394f02013-01-14 22:40:23 +00001921std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001922std::string Scop::getAssumedContextStr() const {
1923 return stringFromIslObj(AssumedContext);
1924}
Tobias Grosser75805372011-04-29 06:27:02 +00001925
1926std::string Scop::getNameStr() const {
1927 std::string ExitName, EntryName;
1928 raw_string_ostream ExitStr(ExitName);
1929 raw_string_ostream EntryStr(EntryName);
1930
Tobias Grosserf240b482014-01-09 10:42:15 +00001931 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001932 EntryStr.str();
1933
1934 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00001935 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00001936 ExitStr.str();
1937 } else
1938 ExitName = "FunctionExit";
1939
1940 return EntryName + "---" + ExitName;
1941}
1942
Tobias Grosser74394f02013-01-14 22:40:23 +00001943__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00001944__isl_give isl_space *Scop::getParamSpace() const {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00001945 return isl_set_get_space(Context);
Tobias Grosser37487052011-10-06 00:03:42 +00001946}
1947
Tobias Grossere86109f2013-10-29 21:05:49 +00001948__isl_give isl_set *Scop::getAssumedContext() const {
1949 return isl_set_copy(AssumedContext);
1950}
1951
Johannes Doerfert43788c52015-08-20 05:58:56 +00001952__isl_give isl_set *Scop::getRuntimeCheckContext() const {
1953 isl_set *RuntimeCheckContext = getAssumedContext();
1954 return RuntimeCheckContext;
1955}
1956
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00001957bool Scop::hasFeasibleRuntimeContext() const {
Johannes Doerfert43788c52015-08-20 05:58:56 +00001958 isl_set *RuntimeCheckContext = getRuntimeCheckContext();
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00001959 RuntimeCheckContext = addNonEmptyDomainConstraints(RuntimeCheckContext);
Johannes Doerfert43788c52015-08-20 05:58:56 +00001960 bool IsFeasible = !isl_set_is_empty(RuntimeCheckContext);
1961 isl_set_free(RuntimeCheckContext);
1962 return IsFeasible;
1963}
1964
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001965void Scop::addAssumption(__isl_take isl_set *Set) {
1966 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001967 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001968}
1969
Tobias Grosser75805372011-04-29 06:27:02 +00001970void Scop::printContext(raw_ostream &OS) const {
1971 OS << "Context:\n";
1972
1973 if (!Context) {
1974 OS.indent(4) << "n/a\n\n";
1975 return;
1976 }
1977
1978 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00001979
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001980 OS.indent(4) << "Assumed Context:\n";
1981 if (!AssumedContext) {
1982 OS.indent(4) << "n/a\n\n";
1983 return;
1984 }
1985
1986 OS.indent(4) << getAssumedContextStr() << "\n";
1987
Tobias Grosser083d3d32014-06-28 08:59:45 +00001988 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00001989 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00001990 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
1991 }
Tobias Grosser75805372011-04-29 06:27:02 +00001992}
1993
Johannes Doerfertb164c792014-09-18 11:17:17 +00001994void Scop::printAliasAssumptions(raw_ostream &OS) const {
Tobias Grosserbb853c22015-07-25 12:31:03 +00001995 int noOfGroups = 0;
1996 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001997 if (Pair.second.size() == 0)
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001998 noOfGroups += 1;
1999 else
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002000 noOfGroups += Pair.second.size();
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002001 }
2002
Tobias Grosserbb853c22015-07-25 12:31:03 +00002003 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n";
Johannes Doerfertb164c792014-09-18 11:17:17 +00002004 if (MinMaxAliasGroups.empty()) {
2005 OS.indent(8) << "n/a\n";
2006 return;
2007 }
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002008
Tobias Grosserbb853c22015-07-25 12:31:03 +00002009 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002010
2011 // If the group has no read only accesses print the write accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002012 if (Pair.second.empty()) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002013 OS.indent(8) << "[[";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002014 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00002015 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
2016 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002017 }
2018 OS << " ]]\n";
2019 }
2020
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002021 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002022 OS.indent(8) << "[[";
Tobias Grosserbb853c22015-07-25 12:31:03 +00002023 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002024 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00002025 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
2026 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002027 }
2028 OS << " ]]\n";
2029 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002030 }
2031}
2032
Tobias Grosser75805372011-04-29 06:27:02 +00002033void Scop::printStatements(raw_ostream &OS) const {
2034 OS << "Statements {\n";
2035
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002036 for (const ScopStmt &Stmt : *this)
2037 OS.indent(4) << Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00002038
2039 OS.indent(4) << "}\n";
2040}
2041
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002042void Scop::printArrayInfo(raw_ostream &OS) const {
2043 OS << "Arrays {\n";
2044
Tobias Grosserab671442015-05-23 05:58:27 +00002045 for (auto &Array : arrays())
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002046 Array.second->print(OS);
2047
2048 OS.indent(4) << "}\n";
Tobias Grosserd46fd5e2015-08-12 15:27:16 +00002049
2050 OS.indent(4) << "Arrays (Bounds as pw_affs) {\n";
2051
2052 for (auto &Array : arrays())
2053 Array.second->print(OS, /* SizeAsPwAff */ true);
2054
2055 OS.indent(4) << "}\n";
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002056}
2057
Tobias Grosser75805372011-04-29 06:27:02 +00002058void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00002059 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
2060 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00002061 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00002062 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00002063 printContext(OS.indent(4));
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002064 printArrayInfo(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00002065 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00002066 printStatements(OS.indent(4));
2067}
2068
2069void Scop::dump() const { print(dbgs()); }
2070
Tobias Grosser9a38ab82011-11-08 15:41:03 +00002071isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00002072
Johannes Doerfertb409fdc2015-08-28 09:24:35 +00002073__isl_give isl_pw_aff *Scop::getPwAff(const SCEV *E, isl_set *Domain) {
2074 return Affinator.getPwAff(E, Domain);
Johannes Doerfert574182d2015-08-12 10:19:50 +00002075}
2076
Tobias Grosser808cd692015-07-14 09:33:13 +00002077__isl_give isl_union_set *Scop::getDomains() const {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002078 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00002079
Tobias Grosser808cd692015-07-14 09:33:13 +00002080 for (const ScopStmt &Stmt : *this)
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002081 Domain = isl_union_set_add_set(Domain, Stmt.getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00002082
2083 return Domain;
2084}
2085
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002086__isl_give isl_union_map *Scop::getMustWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002087 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002088
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002089 for (ScopStmt &Stmt : *this) {
2090 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002091 if (!MA->isMustWrite())
2092 continue;
2093
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002094 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002095 isl_map *AccessDomain = MA->getAccessRelation();
2096 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2097 Write = isl_union_map_add_map(Write, AccessDomain);
2098 }
2099 }
2100 return isl_union_map_coalesce(Write);
2101}
2102
2103__isl_give isl_union_map *Scop::getMayWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002104 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002105
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002106 for (ScopStmt &Stmt : *this) {
2107 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002108 if (!MA->isMayWrite())
2109 continue;
2110
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002111 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002112 isl_map *AccessDomain = MA->getAccessRelation();
2113 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2114 Write = isl_union_map_add_map(Write, AccessDomain);
2115 }
2116 }
2117 return isl_union_map_coalesce(Write);
2118}
2119
Tobias Grosser37eb4222014-02-20 21:43:54 +00002120__isl_give isl_union_map *Scop::getWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002121 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002122
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002123 for (ScopStmt &Stmt : *this) {
2124 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00002125 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00002126 continue;
2127
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002128 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00002129 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00002130 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2131 Write = isl_union_map_add_map(Write, AccessDomain);
2132 }
2133 }
2134 return isl_union_map_coalesce(Write);
2135}
2136
2137__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002138 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002139
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002140 for (ScopStmt &Stmt : *this) {
2141 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00002142 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00002143 continue;
2144
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002145 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00002146 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00002147
2148 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2149 Read = isl_union_map_add_map(Read, AccessDomain);
2150 }
2151 }
2152 return isl_union_map_coalesce(Read);
2153}
2154
Tobias Grosser808cd692015-07-14 09:33:13 +00002155__isl_give isl_union_map *Scop::getSchedule() const {
2156 auto Tree = getScheduleTree();
2157 auto S = isl_schedule_get_map(Tree);
2158 isl_schedule_free(Tree);
2159 return S;
2160}
Tobias Grosser37eb4222014-02-20 21:43:54 +00002161
Tobias Grosser808cd692015-07-14 09:33:13 +00002162__isl_give isl_schedule *Scop::getScheduleTree() const {
2163 return isl_schedule_intersect_domain(isl_schedule_copy(Schedule),
2164 getDomains());
2165}
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002166
Tobias Grosser808cd692015-07-14 09:33:13 +00002167void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) {
2168 auto *S = isl_schedule_from_domain(getDomains());
2169 S = isl_schedule_insert_partial_schedule(
2170 S, isl_multi_union_pw_aff_from_union_map(NewSchedule));
2171 isl_schedule_free(Schedule);
2172 Schedule = S;
2173}
2174
2175void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) {
2176 isl_schedule_free(Schedule);
2177 Schedule = NewSchedule;
Tobias Grosser37eb4222014-02-20 21:43:54 +00002178}
2179
2180bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
2181 bool Changed = false;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002182 for (ScopStmt &Stmt : *this) {
2183 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002184 isl_union_set *NewStmtDomain = isl_union_set_intersect(
2185 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
2186
2187 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
2188 isl_union_set_free(StmtDomain);
2189 isl_union_set_free(NewStmtDomain);
2190 continue;
2191 }
2192
2193 Changed = true;
2194
2195 isl_union_set_free(StmtDomain);
2196 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
2197
2198 if (isl_union_set_is_empty(NewStmtDomain)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002199 Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002200 isl_union_set_free(NewStmtDomain);
2201 } else
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002202 Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain));
Tobias Grosser37eb4222014-02-20 21:43:54 +00002203 }
2204 isl_union_set_free(Domain);
2205 return Changed;
2206}
2207
Tobias Grosser75805372011-04-29 06:27:02 +00002208ScalarEvolution *Scop::getSE() const { return SE; }
2209
2210bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) {
2211 if (tempScop.getAccessFunctions(BB))
2212 return false;
2213
2214 return true;
2215}
2216
Tobias Grosser808cd692015-07-14 09:33:13 +00002217struct MapToDimensionDataTy {
2218 int N;
2219 isl_union_pw_multi_aff *Res;
2220};
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002221
Tobias Grosser808cd692015-07-14 09:33:13 +00002222// @brief Create a function that maps the elements of 'Set' to its N-th
2223// dimension.
2224//
2225// The result is added to 'User->Res'.
2226//
2227// @param Set The input set.
2228// @param N The dimension to map to.
2229//
2230// @returns Zero if no error occurred, non-zero otherwise.
2231static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) {
2232 struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User;
2233 int Dim;
2234 isl_space *Space;
2235 isl_pw_multi_aff *PMA;
2236
2237 Dim = isl_set_dim(Set, isl_dim_set);
2238 Space = isl_set_get_space(Set);
2239 PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N,
2240 Dim - Data->N);
2241 if (Data->N > 1)
2242 PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1);
2243 Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA);
2244
2245 isl_set_free(Set);
2246
2247 return isl_stat_ok;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002248}
2249
Tobias Grosser808cd692015-07-14 09:33:13 +00002250// @brief Create a function that maps the elements of Domain to their Nth
2251// dimension.
2252//
2253// @param Domain The set of elements to map.
2254// @param N The dimension to map to.
2255static __isl_give isl_multi_union_pw_aff *
2256mapToDimension(__isl_take isl_union_set *Domain, int N) {
2257 struct MapToDimensionDataTy Data;
2258 isl_space *Space;
2259
2260 Space = isl_union_set_get_space(Domain);
2261 Data.N = N;
2262 Data.Res = isl_union_pw_multi_aff_empty(Space);
2263 if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0)
2264 Data.Res = isl_union_pw_multi_aff_free(Data.Res);
2265
2266 isl_union_set_free(Domain);
2267 return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res);
2268}
2269
2270ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R, TempScop &tempScop,
2271 const Region &CurRegion,
2272 SmallVectorImpl<Loop *> &NestLoops) {
2273 ScopStmt *Stmt;
2274 if (BB) {
2275 Stmts.emplace_back(*this, tempScop, CurRegion, *BB, NestLoops);
2276 Stmt = &Stmts.back();
2277 StmtMap[BB] = Stmt;
2278 } else {
2279 assert(R && "Either basic block or a region expected.");
2280 Stmts.emplace_back(*this, tempScop, CurRegion, *R, NestLoops);
2281 Stmt = &Stmts.back();
2282 for (BasicBlock *BB : R->blocks())
2283 StmtMap[BB] = Stmt;
2284 }
2285 return Stmt;
2286}
2287
Michael Kruse046dde42015-08-10 13:01:57 +00002288__isl_give isl_schedule *
2289Scop::buildBBScopStmt(BasicBlock *BB, TempScop &tempScop,
2290 const Region &CurRegion,
2291 SmallVectorImpl<Loop *> &NestLoops) {
2292 if (isTrivialBB(BB, tempScop))
2293 return nullptr;
2294
2295 auto *Stmt = addScopStmt(BB, nullptr, tempScop, CurRegion, NestLoops);
2296 auto *Domain = Stmt->getDomain();
2297 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2298}
2299
Tobias Grosser808cd692015-07-14 09:33:13 +00002300__isl_give isl_schedule *Scop::buildScop(TempScop &tempScop,
2301 const Region &CurRegion,
2302 SmallVectorImpl<Loop *> &NestLoops,
2303 LoopInfo &LI, ScopDetection &SD) {
2304 if (SD.isNonAffineSubRegion(&CurRegion, &getRegion())) {
2305 auto *Stmt = addScopStmt(nullptr, const_cast<Region *>(&CurRegion),
2306 tempScop, CurRegion, NestLoops);
2307 auto *Domain = Stmt->getDomain();
2308 return isl_schedule_from_domain(isl_union_set_from_set(Domain));
2309 }
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002310
Tobias Grosser75805372011-04-29 06:27:02 +00002311 Loop *L = castToLoop(CurRegion, LI);
2312
2313 if (L)
2314 NestLoops.push_back(L);
2315
2316 unsigned loopDepth = NestLoops.size();
Tobias Grosser808cd692015-07-14 09:33:13 +00002317 isl_schedule *Schedule = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002318
2319 for (Region::const_element_iterator I = CurRegion.element_begin(),
Tobias Grosserabfbe632013-02-05 12:09:06 +00002320 E = CurRegion.element_end();
Tobias Grosser808cd692015-07-14 09:33:13 +00002321 I != E; ++I) {
2322 isl_schedule *StmtSchedule = nullptr;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002323 if (I->isSubRegion()) {
Tobias Grosser808cd692015-07-14 09:33:13 +00002324 StmtSchedule =
2325 buildScop(tempScop, *I->getNodeAs<Region>(), NestLoops, LI, SD);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002326 } else {
Michael Kruse046dde42015-08-10 13:01:57 +00002327 StmtSchedule = buildBBScopStmt(I->getNodeAs<BasicBlock>(), tempScop,
2328 CurRegion, NestLoops);
Tobias Grosser75805372011-04-29 06:27:02 +00002329 }
Michael Kruse046dde42015-08-10 13:01:57 +00002330 Schedule = combineInSequence(Schedule, StmtSchedule);
Tobias Grosser808cd692015-07-14 09:33:13 +00002331 }
Tobias Grosser75805372011-04-29 06:27:02 +00002332
Tobias Grosser808cd692015-07-14 09:33:13 +00002333 if (!L)
2334 return Schedule;
2335
2336 auto *Domain = isl_schedule_get_domain(Schedule);
2337 if (!isl_union_set_is_empty(Domain)) {
2338 auto *MUPA = mapToDimension(isl_union_set_copy(Domain), loopDepth);
2339 Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA);
2340 }
2341 isl_union_set_free(Domain);
2342
Tobias Grosser75805372011-04-29 06:27:02 +00002343 NestLoops.pop_back();
Tobias Grosser808cd692015-07-14 09:33:13 +00002344 return Schedule;
Tobias Grosser75805372011-04-29 06:27:02 +00002345}
2346
Johannes Doerfert7c494212014-10-31 23:13:39 +00002347ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
Tobias Grosser57411e32015-05-27 06:51:34 +00002348 auto StmtMapIt = StmtMap.find(BB);
Johannes Doerfert7c494212014-10-31 23:13:39 +00002349 if (StmtMapIt == StmtMap.end())
2350 return nullptr;
2351 return StmtMapIt->second;
2352}
2353
Michael Kruse7bf39442015-09-10 12:46:52 +00002354//===----------------------------------------------------------------------===//
2355// TempScop implementation
2356TempScop::~TempScop() {}
2357
2358void TempScop::print(raw_ostream &OS, ScalarEvolution *SE, LoopInfo *LI) const {
2359 OS << "Scop: " << R.getNameStr() << "\n";
2360
2361 printDetail(OS, SE, LI, &R, 0);
2362}
2363
2364void TempScop::printDetail(raw_ostream &OS, ScalarEvolution *SE, LoopInfo *LI,
2365 const Region *CurR, unsigned ind) const {
2366 // FIXME: Print other details rather than memory accesses.
2367 for (const auto &CurBlock : CurR->blocks()) {
2368 AccFuncMapType::const_iterator AccSetIt = AccFuncMap.find(CurBlock);
2369
2370 // Ignore trivial blocks that do not contain any memory access.
2371 if (AccSetIt == AccFuncMap.end())
2372 continue;
2373
2374 OS.indent(ind) << "BB: " << CurBlock->getName() << '\n';
2375 typedef AccFuncSetType::const_iterator access_iterator;
2376 const AccFuncSetType &AccFuncs = AccSetIt->second;
2377
2378 for (access_iterator AI = AccFuncs.begin(), AE = AccFuncs.end(); AI != AE;
2379 ++AI)
2380 AI->first.print(OS.indent(ind + 2));
2381 }
2382}
2383
Johannes Doerfert96425c22015-08-30 21:13:53 +00002384int Scop::getRelativeLoopDepth(const Loop *L) const {
2385 Loop *OuterLoop =
2386 L ? R.outermostLoopInRegion(const_cast<Loop *>(L)) : nullptr;
2387 if (!OuterLoop)
2388 return -1;
Johannes Doerfertd020b772015-08-27 06:53:52 +00002389 return L->getLoopDepth() - OuterLoop->getLoopDepth();
2390}
2391
Michael Kruse7bf39442015-09-10 12:46:52 +00002392void TempScopInfo::buildPHIAccesses(PHINode *PHI, Region &R,
2393 AccFuncSetType &Functions,
2394 Region *NonAffineSubRegion,
2395 bool IsExitBlock) {
2396
2397 // PHI nodes that are in the exit block of the region, hence if IsExitBlock is
2398 // true, are not modeled as ordinary PHI nodes as they are not part of the
2399 // region. However, we model the operands in the predecessor blocks that are
2400 // part of the region as regular scalar accesses.
2401
2402 // If we can synthesize a PHI we can skip it, however only if it is in
2403 // the region. If it is not it can only be in the exit block of the region.
2404 // In this case we model the operands but not the PHI itself.
2405 if (!IsExitBlock && canSynthesize(PHI, LI, SE, &R))
2406 return;
2407
2408 // PHI nodes are modeled as if they had been demoted prior to the SCoP
2409 // detection. Hence, the PHI is a load of a new memory location in which the
2410 // incoming value was written at the end of the incoming basic block.
2411 bool OnlyNonAffineSubRegionOperands = true;
2412 for (unsigned u = 0; u < PHI->getNumIncomingValues(); u++) {
2413 Value *Op = PHI->getIncomingValue(u);
2414 BasicBlock *OpBB = PHI->getIncomingBlock(u);
2415
2416 // Do not build scalar dependences inside a non-affine subregion.
2417 if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB))
2418 continue;
2419
2420 OnlyNonAffineSubRegionOperands = false;
2421
2422 if (!R.contains(OpBB))
2423 continue;
2424
2425 Instruction *OpI = dyn_cast<Instruction>(Op);
2426 if (OpI) {
2427 BasicBlock *OpIBB = OpI->getParent();
2428 // As we pretend there is a use (or more precise a write) of OpI in OpBB
2429 // we have to insert a scalar dependence from the definition of OpI to
2430 // OpBB if the definition is not in OpBB.
2431 if (OpIBB != OpBB) {
2432 IRAccess ScalarRead(IRAccess::READ, OpI, ZeroOffset, 1, true, OpI);
2433 AccFuncMap[OpBB].push_back(std::make_pair(ScalarRead, PHI));
2434 IRAccess ScalarWrite(IRAccess::MUST_WRITE, OpI, ZeroOffset, 1, true,
2435 OpI);
2436 AccFuncMap[OpIBB].push_back(std::make_pair(ScalarWrite, OpI));
2437 }
2438 }
2439
2440 // Always use the terminator of the incoming basic block as the access
2441 // instruction.
2442 OpI = OpBB->getTerminator();
2443
2444 IRAccess ScalarAccess(IRAccess::MUST_WRITE, PHI, ZeroOffset, 1, true, Op,
2445 /* IsPHI */ !IsExitBlock);
2446 AccFuncMap[OpBB].push_back(std::make_pair(ScalarAccess, OpI));
2447 }
2448
2449 if (!OnlyNonAffineSubRegionOperands) {
2450 IRAccess ScalarAccess(IRAccess::READ, PHI, ZeroOffset, 1, true, PHI,
2451 /* IsPHI */ !IsExitBlock);
2452 Functions.push_back(std::make_pair(ScalarAccess, PHI));
2453 }
2454}
2455
2456bool TempScopInfo::buildScalarDependences(Instruction *Inst, Region *R,
2457 Region *NonAffineSubRegion) {
2458 bool canSynthesizeInst = canSynthesize(Inst, LI, SE, R);
2459 if (isIgnoredIntrinsic(Inst))
2460 return false;
2461
2462 bool AnyCrossStmtUse = false;
2463 BasicBlock *ParentBB = Inst->getParent();
2464
2465 for (User *U : Inst->users()) {
2466 Instruction *UI = dyn_cast<Instruction>(U);
2467
2468 // Ignore the strange user
2469 if (UI == 0)
2470 continue;
2471
2472 BasicBlock *UseParent = UI->getParent();
2473
2474 // Ignore the users in the same BB (statement)
2475 if (UseParent == ParentBB)
2476 continue;
2477
2478 // Do not build scalar dependences inside a non-affine subregion.
2479 if (NonAffineSubRegion && NonAffineSubRegion->contains(UseParent))
2480 continue;
2481
2482 // Check whether or not the use is in the SCoP.
2483 if (!R->contains(UseParent)) {
2484 AnyCrossStmtUse = true;
2485 continue;
2486 }
2487
2488 // If the instruction can be synthesized and the user is in the region
2489 // we do not need to add scalar dependences.
2490 if (canSynthesizeInst)
2491 continue;
2492
2493 // No need to translate these scalar dependences into polyhedral form,
2494 // because synthesizable scalars can be generated by the code generator.
2495 if (canSynthesize(UI, LI, SE, R))
2496 continue;
2497
2498 // Skip PHI nodes in the region as they handle their operands on their own.
2499 if (isa<PHINode>(UI))
2500 continue;
2501
2502 // Now U is used in another statement.
2503 AnyCrossStmtUse = true;
2504
2505 // Do not build a read access that is not in the current SCoP
2506 // Use the def instruction as base address of the IRAccess, so that it will
2507 // become the name of the scalar access in the polyhedral form.
2508 IRAccess ScalarAccess(IRAccess::READ, Inst, ZeroOffset, 1, true, Inst);
2509 AccFuncMap[UseParent].push_back(std::make_pair(ScalarAccess, UI));
2510 }
2511
2512 if (ModelReadOnlyScalars) {
2513 for (Value *Op : Inst->operands()) {
2514 if (canSynthesize(Op, LI, SE, R))
2515 continue;
2516
2517 if (Instruction *OpInst = dyn_cast<Instruction>(Op))
2518 if (R->contains(OpInst))
2519 continue;
2520
2521 if (isa<Constant>(Op))
2522 continue;
2523
2524 IRAccess ScalarAccess(IRAccess::READ, Op, ZeroOffset, 1, true, Op);
2525 AccFuncMap[Inst->getParent()].push_back(
2526 std::make_pair(ScalarAccess, Inst));
2527 }
2528 }
2529
2530 return AnyCrossStmtUse;
2531}
2532
2533extern MapInsnToMemAcc InsnToMemAcc;
2534
2535IRAccess
2536TempScopInfo::buildIRAccess(Instruction *Inst, Loop *L, Region *R,
2537 const ScopDetection::BoxedLoopsSetTy *BoxedLoops) {
2538 unsigned Size;
2539 Type *SizeType;
2540 Value *Val;
2541 enum IRAccess::TypeKind Type;
2542
2543 if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
2544 SizeType = Load->getType();
2545 Size = TD->getTypeStoreSize(SizeType);
2546 Type = IRAccess::READ;
2547 Val = Load;
2548 } else {
2549 StoreInst *Store = cast<StoreInst>(Inst);
2550 SizeType = Store->getValueOperand()->getType();
2551 Size = TD->getTypeStoreSize(SizeType);
2552 Type = IRAccess::MUST_WRITE;
2553 Val = Store->getValueOperand();
2554 }
2555
2556 const SCEV *AccessFunction = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
2557 const SCEVUnknown *BasePointer =
2558 dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
2559
2560 assert(BasePointer && "Could not find base pointer");
2561 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
2562
2563 auto AccItr = InsnToMemAcc.find(Inst);
2564 if (PollyDelinearize && AccItr != InsnToMemAcc.end())
2565 return IRAccess(Type, BasePointer->getValue(), AccessFunction, Size, true,
2566 AccItr->second.DelinearizedSubscripts,
2567 AccItr->second.Shape->DelinearizedSizes, Val);
2568
2569 // Check if the access depends on a loop contained in a non-affine subregion.
2570 bool isVariantInNonAffineLoop = false;
2571 if (BoxedLoops) {
2572 SetVector<const Loop *> Loops;
2573 findLoops(AccessFunction, Loops);
2574 for (const Loop *L : Loops)
2575 if (BoxedLoops->count(L))
2576 isVariantInNonAffineLoop = true;
2577 }
2578
2579 bool IsAffine = !isVariantInNonAffineLoop &&
2580 isAffineExpr(R, AccessFunction, *SE, BasePointer->getValue());
2581
2582 SmallVector<const SCEV *, 4> Subscripts, Sizes;
2583 Subscripts.push_back(AccessFunction);
2584 Sizes.push_back(SE->getConstant(ZeroOffset->getType(), Size));
2585
2586 if (!IsAffine && Type == IRAccess::MUST_WRITE)
2587 Type = IRAccess::MAY_WRITE;
2588
2589 return IRAccess(Type, BasePointer->getValue(), AccessFunction, Size, IsAffine,
2590 Subscripts, Sizes, Val);
2591}
2592
2593void TempScopInfo::buildAccessFunctions(Region &R, Region &SR) {
2594
2595 if (SD->isNonAffineSubRegion(&SR, &R)) {
2596 for (BasicBlock *BB : SR.blocks())
2597 buildAccessFunctions(R, *BB, &SR);
2598 return;
2599 }
2600
2601 for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
2602 if (I->isSubRegion())
2603 buildAccessFunctions(R, *I->getNodeAs<Region>());
2604 else
2605 buildAccessFunctions(R, *I->getNodeAs<BasicBlock>());
2606}
2607
2608void TempScopInfo::buildAccessFunctions(Region &R, BasicBlock &BB,
2609 Region *NonAffineSubRegion,
2610 bool IsExitBlock) {
2611 AccFuncSetType Functions;
2612 Loop *L = LI->getLoopFor(&BB);
2613
2614 // The set of loops contained in non-affine subregions that are part of R.
2615 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD->getBoxedLoops(&R);
2616
2617 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) {
2618 Instruction *Inst = I;
2619
2620 PHINode *PHI = dyn_cast<PHINode>(Inst);
2621 if (PHI)
2622 buildPHIAccesses(PHI, R, Functions, NonAffineSubRegion, IsExitBlock);
2623
2624 // For the exit block we stop modeling after the last PHI node.
2625 if (!PHI && IsExitBlock)
2626 break;
2627
2628 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
2629 Functions.push_back(
2630 std::make_pair(buildIRAccess(Inst, L, &R, BoxedLoops), Inst));
2631
2632 if (isIgnoredIntrinsic(Inst))
2633 continue;
2634
2635 if (buildScalarDependences(Inst, &R, NonAffineSubRegion)) {
2636 // If the Instruction is used outside the statement, we need to build the
2637 // write access.
2638 if (!isa<StoreInst>(Inst)) {
2639 IRAccess ScalarAccess(IRAccess::MUST_WRITE, Inst, ZeroOffset, 1, true,
2640 Inst);
2641 Functions.push_back(std::make_pair(ScalarAccess, Inst));
2642 }
2643 }
2644 }
2645
2646 if (Functions.empty())
2647 return;
2648
2649 AccFuncSetType &Accs = AccFuncMap[&BB];
2650 Accs.insert(Accs.end(), Functions.begin(), Functions.end());
2651}
2652
2653TempScop *TempScopInfo::buildTempScop(Region &R) {
2654 TempScop *TScop = new TempScop(R, AccFuncMap);
2655
2656 buildAccessFunctions(R, R);
2657
2658 // In case the region does not have an exiting block we will later (during
2659 // code generation) split the exit block. This will move potential PHI nodes
2660 // from the current exit block into the new region exiting block. Hence, PHI
2661 // nodes that are at this point not part of the region will be.
2662 // To handle these PHI nodes later we will now model their operands as scalar
2663 // accesses. Note that we do not model anything in the exit block if we have
2664 // an exiting block in the region, as there will not be any splitting later.
2665 if (!R.getExitingBlock())
2666 buildAccessFunctions(R, *R.getExit(), nullptr, /* IsExitBlock */ true);
2667
2668 return TScop;
2669}
2670
2671TempScop *TempScopInfo::getTempScop() const { return TempScopOfRegion; }
2672
2673void TempScopInfo::print(raw_ostream &OS, const Module *) const {
2674 if (TempScopOfRegion)
2675 TempScopOfRegion->print(OS, SE, LI);
2676}
2677
2678bool TempScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
2679 SD = &getAnalysis<ScopDetection>();
2680
2681 if (!SD->isMaxRegionInScop(*R))
2682 return false;
2683
2684 Function *F = R->getEntry()->getParent();
2685 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2686 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2687 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2688 TD = &F->getParent()->getDataLayout();
2689 ZeroOffset = SE->getConstant(TD->getIntPtrType(F->getContext()), 0);
2690
2691 assert(!TempScopOfRegion && "Build the TempScop only once");
2692 TempScopOfRegion = buildTempScop(*R);
2693
2694 return false;
2695}
2696
2697void TempScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
2698 AU.addRequiredTransitive<LoopInfoWrapperPass>();
2699 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
2700 AU.addRequiredTransitive<ScopDetection>();
2701 AU.addRequiredID(IndependentBlocksID);
2702 AU.addRequired<AAResultsWrapperPass>();
2703 AU.setPreservesAll();
2704}
2705
2706TempScopInfo::~TempScopInfo() { clear(); }
2707
2708void TempScopInfo::clear() {
2709 AccFuncMap.clear();
2710 if (TempScopOfRegion)
2711 delete TempScopOfRegion;
2712 TempScopOfRegion = nullptr;
2713}
2714
2715//===----------------------------------------------------------------------===//
2716// TempScop information extraction pass implement
2717char TempScopInfo::ID = 0;
2718
2719Pass *polly::createTempScopInfoPass() { return new TempScopInfo(); }
2720
2721INITIALIZE_PASS_BEGIN(TempScopInfo, "polly-analyze-ir",
2722 "Polly - Analyse the LLVM-IR in the detected regions",
2723 false, false);
2724INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass);
2725INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
2726INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
2727INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
2728INITIALIZE_PASS_END(TempScopInfo, "polly-analyze-ir",
2729 "Polly - Analyse the LLVM-IR in the detected regions",
2730 false, false)
2731
Tobias Grosser75805372011-04-29 06:27:02 +00002732//===----------------------------------------------------------------------===//
Tobias Grosserb76f38532011-08-20 11:11:25 +00002733ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
2734 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00002735 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00002736}
2737
2738ScopInfo::~ScopInfo() {
2739 clear();
2740 isl_ctx_free(ctx);
2741}
2742
Tobias Grosser75805372011-04-29 06:27:02 +00002743void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00002744 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00002745 AU.addRequired<RegionInfoPass>();
Johannes Doerfert96425c22015-08-30 21:13:53 +00002746 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +00002747 AU.addRequired<ScalarEvolutionWrapperPass>();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002748 AU.addRequired<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00002749 AU.addRequired<TempScopInfo>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +00002750 AU.addRequired<AAResultsWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00002751 AU.setPreservesAll();
2752}
2753
2754bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Chandler Carruthf5579872015-01-17 14:16:56 +00002755 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth66ef16b2015-09-09 22:13:56 +00002756 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002757 ScopDetection &SD = getAnalysis<ScopDetection>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +00002758 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Johannes Doerfert96425c22015-08-30 21:13:53 +00002759 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser75805372011-04-29 06:27:02 +00002760
Michael Kruse82a1c7d2015-08-14 20:10:27 +00002761 TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop();
Tobias Grosser75805372011-04-29 06:27:02 +00002762
2763 // This region is no Scop.
2764 if (!tempScop) {
Tobias Grosserc98a8fc2014-11-14 11:12:31 +00002765 scop = nullptr;
Tobias Grosser75805372011-04-29 06:27:02 +00002766 return false;
2767 }
2768
Johannes Doerfert96425c22015-08-30 21:13:53 +00002769 scop = Scop::createFromTempScop(*tempScop, LI, SE, SD, AA, DT, ctx);
Tobias Grosser75805372011-04-29 06:27:02 +00002770
Tobias Grosserd6a50b32015-05-30 06:26:21 +00002771 DEBUG(scop->print(dbgs()));
2772
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00002773 if (!scop->hasFeasibleRuntimeContext()) {
Johannes Doerfert43788c52015-08-20 05:58:56 +00002774 delete scop;
2775 scop = nullptr;
2776 return false;
2777 }
2778
Johannes Doerfert120de4b2015-08-20 18:30:08 +00002779 // Statistics.
2780 ++ScopFound;
2781 if (scop->getMaxLoopDepth() > 0)
2782 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00002783 return false;
2784}
2785
2786char ScopInfo::ID = 0;
2787
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002788Pass *polly::createScopInfoPass() { return new ScopInfo(); }
2789
Tobias Grosser73600b82011-10-08 00:30:40 +00002790INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
2791 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002792 false);
Chandler Carruth66ef16b2015-09-09 22:13:56 +00002793INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +00002794INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00002795INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosserc5bcf242015-08-17 10:57:08 +00002796INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002797INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00002798INITIALIZE_PASS_DEPENDENCY(TempScopInfo);
Johannes Doerfert96425c22015-08-30 21:13:53 +00002799INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser73600b82011-10-08 00:30:40 +00002800INITIALIZE_PASS_END(ScopInfo, "polly-scops",
2801 "Polly - Create polyhedral description of Scops", false,
2802 false)