blob: 92a495089addbb69ce96987787747c0eb67838e5 [file] [log] [blame]
Johannes Doerfert58a7c752015-09-28 09:48:53 +00001//===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===//
Tobias Grosser75805372011-04-29 06:27:02 +00002//
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"
Tobias Grosserf4c24b22015-04-05 13:11:54 +000026#include "llvm/ADT/MapVector.h"
Tobias Grosserc2bb0cb2015-09-25 09:49:19 +000027#include "llvm/ADT/PostOrderIterator.h"
28#include "llvm/ADT/STLExtras.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000029#include "llvm/ADT/SetVector.h"
Tobias Grosser83628182013-05-07 08:11:54 +000030#include "llvm/ADT/Statistic.h"
Hongbin Zheng86a37742012-04-25 08:01:38 +000031#include "llvm/ADT/StringExtras.h"
Johannes Doerfertb164c792014-09-18 11:17:17 +000032#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000033#include "llvm/Analysis/LoopInfo.h"
Tobias Grosserc2bb0cb2015-09-25 09:49:19 +000034#include "llvm/Analysis/LoopIterator.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//===----------------------------------------------------------------------===//
Michael Kruse7bf39442015-09-10 12:46:52 +000095
Michael Kruse046dde42015-08-10 13:01:57 +000096// Create a sequence of two schedules. Either argument may be null and is
97// interpreted as the empty schedule. Can also return null if both schedules are
98// empty.
99static __isl_give isl_schedule *
100combineInSequence(__isl_take isl_schedule *Prev,
101 __isl_take isl_schedule *Succ) {
102 if (!Prev)
103 return Succ;
104 if (!Succ)
105 return Prev;
106
107 return isl_schedule_sequence(Prev, Succ);
108}
109
Johannes Doerferte7044942015-02-24 11:58:30 +0000110static __isl_give isl_set *addRangeBoundsToSet(__isl_take isl_set *S,
111 const ConstantRange &Range,
112 int dim,
113 enum isl_dim_type type) {
114 isl_val *V;
115 isl_ctx *ctx = isl_set_get_ctx(S);
116
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000117 bool useLowerUpperBound = Range.isSignWrappedSet() && !Range.isFullSet();
118 const auto LB = useLowerUpperBound ? Range.getLower() : Range.getSignedMin();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000119 V = isl_valFromAPInt(ctx, LB, true);
Johannes Doerferte7044942015-02-24 11:58:30 +0000120 isl_set *SLB = isl_set_lower_bound_val(isl_set_copy(S), type, dim, V);
121
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000122 const auto UB = useLowerUpperBound ? Range.getUpper() : Range.getSignedMax();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000123 V = isl_valFromAPInt(ctx, UB, true);
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000124 if (useLowerUpperBound)
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000125 V = isl_val_sub_ui(V, 1);
Johannes Doerferte7044942015-02-24 11:58:30 +0000126 isl_set *SUB = isl_set_upper_bound_val(S, type, dim, V);
127
Johannes Doerfert8f8af432015-04-26 20:07:21 +0000128 if (useLowerUpperBound)
Johannes Doerferte7044942015-02-24 11:58:30 +0000129 return isl_set_union(SLB, SUB);
130 else
131 return isl_set_intersect(SLB, SUB);
132}
133
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000134static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) {
135 LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr);
136 if (!BasePtrLI)
137 return nullptr;
138
139 if (!S->getRegion().contains(BasePtrLI))
140 return nullptr;
141
142 ScalarEvolution &SE = *S->getSE();
143
144 auto *OriginBaseSCEV =
145 SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand()));
146 if (!OriginBaseSCEV)
147 return nullptr;
148
149 auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV);
150 if (!OriginBaseSCEVUnknown)
151 return nullptr;
152
153 return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue());
154}
155
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000156ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx,
Tobias Grosser99c70dd2015-09-26 08:55:54 +0000157 ArrayRef<const SCEV *> Sizes, bool IsPHI, Scop *S)
158 : BasePtr(BasePtr), ElementType(ElementType), IsPHI(IsPHI), S(*S) {
Tobias Grosser92245222015-07-28 14:53:44 +0000159 std::string BasePtrName =
160 getIslCompatibleName("MemRef_", BasePtr, IsPHI ? "__phi" : "");
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000161 Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this);
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000162
Tobias Grosser99c70dd2015-09-26 08:55:54 +0000163 updateSizes(Sizes);
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000164 BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr);
165 if (BasePtrOriginSAI)
166 const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this);
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000167}
168
Tobias Grosser99c70dd2015-09-26 08:55:54 +0000169__isl_give isl_space *ScopArrayInfo::getSpace() const {
170 auto Space =
171 isl_space_set_alloc(isl_id_get_ctx(Id), 0, getNumberOfDimensions());
172 Space = isl_space_set_tuple_id(Space, isl_dim_set, isl_id_copy(Id));
173 return Space;
174}
175
176void ScopArrayInfo::updateSizes(ArrayRef<const SCEV *> NewSizes) {
177#ifndef NDEBUG
178 int SharedDims = std::min(NewSizes.size(), DimensionSizes.size());
179 int ExtraDimsNew = NewSizes.size() - SharedDims;
180 int ExtraDimsOld = DimensionSizes.size() - SharedDims;
181 for (int i = 0; i < SharedDims; i++) {
182 assert(NewSizes[i + ExtraDimsNew] == DimensionSizes[i + ExtraDimsOld] &&
183 "Array update with non-matching dimension sizes");
184 }
185#endif
186
187 DimensionSizes.clear();
188 DimensionSizes.insert(DimensionSizes.begin(), NewSizes.begin(),
189 NewSizes.end());
190 for (isl_pw_aff *Size : DimensionSizesPw)
191 isl_pw_aff_free(Size);
192 DimensionSizesPw.clear();
193 for (const SCEV *Expr : DimensionSizes) {
194 isl_pw_aff *Size = S.getPwAff(Expr);
195 DimensionSizesPw.push_back(Size);
196 }
197}
198
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000199ScopArrayInfo::~ScopArrayInfo() {
200 isl_id_free(Id);
201 for (isl_pw_aff *Size : DimensionSizesPw)
202 isl_pw_aff_free(Size);
203}
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000204
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000205std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); }
206
207int ScopArrayInfo::getElemSizeInBytes() const {
208 return ElementType->getPrimitiveSizeInBits() / 8;
209}
210
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000211isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); }
212
213void ScopArrayInfo::dump() const { print(errs()); }
214
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000215void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const {
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000216 OS.indent(8) << *getElementType() << " " << getName() << "[*]";
Tobias Grosserd46fd5e2015-08-12 15:27:16 +0000217 for (unsigned u = 0; u < getNumberOfDimensions(); u++) {
218 OS << "[";
219
220 if (SizeAsPwAff)
221 OS << " " << DimensionSizesPw[u] << " ";
222 else
223 OS << *DimensionSizes[u];
224
225 OS << "]";
226 }
227
Johannes Doerfert4eed5be2015-08-20 18:04:22 +0000228 if (BasePtrOriginSAI)
229 OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]";
230
Tobias Grosser49ad36c2015-05-20 08:05:31 +0000231 OS << " // Element size " << getElemSizeInBytes() << "\n";
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000232}
233
234const ScopArrayInfo *
235ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) {
236 isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out);
237 assert(Id && "Output dimension didn't have an ID");
238 return getFromId(Id);
239}
240
241const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) {
242 void *User = isl_id_get_user(Id);
243 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
244 isl_id_free(Id);
245 return SAI;
246}
247
Tobias Grosser99c70dd2015-09-26 08:55:54 +0000248void MemoryAccess::updateDimensionality() {
249 auto ArraySpace = getScopArrayInfo()->getSpace();
250 auto AccessSpace = isl_space_range(isl_map_get_space(AccessRelation));
251
252 auto DimsArray = isl_space_dim(ArraySpace, isl_dim_set);
253 auto DimsAccess = isl_space_dim(AccessSpace, isl_dim_set);
254 auto DimsMissing = DimsArray - DimsAccess;
255
256 auto Map = isl_map_from_domain_and_range(isl_set_universe(AccessSpace),
257 isl_set_universe(ArraySpace));
258
259 for (unsigned i = 0; i < DimsMissing; i++)
260 Map = isl_map_fix_si(Map, isl_dim_out, i, 0);
261
262 for (unsigned i = DimsMissing; i < DimsArray; i++)
263 Map = isl_map_equate(Map, isl_dim_in, i - DimsMissing, isl_dim_out, i);
264
265 AccessRelation = isl_map_apply_range(AccessRelation, Map);
266}
267
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000268const std::string
269MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) {
270 switch (RT) {
271 case MemoryAccess::RT_NONE:
272 llvm_unreachable("Requested a reduction operator string for a memory "
273 "access which isn't a reduction");
274 case MemoryAccess::RT_ADD:
275 return "+";
276 case MemoryAccess::RT_MUL:
277 return "*";
278 case MemoryAccess::RT_BOR:
279 return "|";
280 case MemoryAccess::RT_BXOR:
281 return "^";
282 case MemoryAccess::RT_BAND:
283 return "&";
284 }
285 llvm_unreachable("Unknown reduction type");
286 return "";
287}
288
Johannes Doerfertf6183392014-07-01 20:52:51 +0000289/// @brief Return the reduction type for a given binary operator
290static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp,
291 const Instruction *Load) {
292 if (!BinOp)
293 return MemoryAccess::RT_NONE;
294 switch (BinOp->getOpcode()) {
295 case Instruction::FAdd:
296 if (!BinOp->hasUnsafeAlgebra())
297 return MemoryAccess::RT_NONE;
298 // Fall through
299 case Instruction::Add:
300 return MemoryAccess::RT_ADD;
301 case Instruction::Or:
302 return MemoryAccess::RT_BOR;
303 case Instruction::Xor:
304 return MemoryAccess::RT_BXOR;
305 case Instruction::And:
306 return MemoryAccess::RT_BAND;
307 case Instruction::FMul:
308 if (!BinOp->hasUnsafeAlgebra())
309 return MemoryAccess::RT_NONE;
310 // Fall through
311 case Instruction::Mul:
312 if (DisableMultiplicativeReductions)
313 return MemoryAccess::RT_NONE;
314 return MemoryAccess::RT_MUL;
315 default:
316 return MemoryAccess::RT_NONE;
317 }
318}
Tobias Grosser5fd8c092015-09-17 17:28:15 +0000319
Tobias Grosser5fd8c092015-09-17 17:28:15 +0000320/// @brief Derive the individual index expressions from a GEP instruction
321///
322/// This function optimistically assumes the GEP references into a fixed size
323/// array. If this is actually true, this function returns a list of array
324/// subscript expressions as SCEV as well as a list of integers describing
325/// the size of the individual array dimensions. Both lists have either equal
326/// length of the size list is one element shorter in case there is no known
327/// size available for the outermost array dimension.
328///
329/// @param GEP The GetElementPtr instruction to analyze.
330///
331/// @return A tuple with the subscript expressions and the dimension sizes.
332static std::tuple<std::vector<const SCEV *>, std::vector<int>>
333getIndexExpressionsFromGEP(GetElementPtrInst *GEP, ScalarEvolution &SE) {
334 std::vector<const SCEV *> Subscripts;
335 std::vector<int> Sizes;
336
337 Type *Ty = GEP->getPointerOperandType();
338
339 bool DroppedFirstDim = false;
340
Michael Kruse26ed65e2015-09-24 17:32:49 +0000341 for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
Tobias Grosser5fd8c092015-09-17 17:28:15 +0000342
343 const SCEV *Expr = SE.getSCEV(GEP->getOperand(i));
344
345 if (i == 1) {
346 if (auto PtrTy = dyn_cast<PointerType>(Ty)) {
347 Ty = PtrTy->getElementType();
348 } else if (auto ArrayTy = dyn_cast<ArrayType>(Ty)) {
349 Ty = ArrayTy->getElementType();
350 } else {
351 Subscripts.clear();
352 Sizes.clear();
353 break;
354 }
355 if (auto Const = dyn_cast<SCEVConstant>(Expr))
356 if (Const->getValue()->isZero()) {
357 DroppedFirstDim = true;
358 continue;
359 }
360 Subscripts.push_back(Expr);
361 continue;
362 }
363
364 auto ArrayTy = dyn_cast<ArrayType>(Ty);
365 if (!ArrayTy) {
366 Subscripts.clear();
367 Sizes.clear();
368 break;
369 }
370
371 Subscripts.push_back(Expr);
372 if (!(DroppedFirstDim && i == 2))
373 Sizes.push_back(ArrayTy->getNumElements());
374
375 Ty = ArrayTy->getElementType();
376 }
377
378 return std::make_tuple(Subscripts, Sizes);
379}
380
Tobias Grosser75805372011-04-29 06:27:02 +0000381MemoryAccess::~MemoryAccess() {
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000382 isl_id_free(Id);
Tobias Grosser54a86e62011-08-18 06:31:46 +0000383 isl_map_free(AccessRelation);
Tobias Grosser166c4222015-09-05 07:46:40 +0000384 isl_map_free(NewAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000385}
386
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000387const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const {
388 isl_id *ArrayId = getArrayId();
389 void *User = isl_id_get_user(ArrayId);
390 const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User);
391 isl_id_free(ArrayId);
392 return SAI;
393}
394
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000395__isl_give isl_id *MemoryAccess::getArrayId() const {
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000396 return isl_map_get_tuple_id(AccessRelation, isl_dim_out);
397}
398
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000399__isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation(
400 __isl_take isl_union_map *USchedule) const {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000401 isl_map *Schedule, *ScheduledAccRel;
402 isl_union_set *UDomain;
403
404 UDomain = isl_union_set_from_set(getStatement()->getDomain());
405 USchedule = isl_union_map_intersect_domain(USchedule, UDomain);
406 Schedule = isl_map_from_union_map(USchedule);
407 ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule);
408 return isl_pw_multi_aff_from_map(ScheduledAccRel);
409}
410
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000411__isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000412 return isl_map_copy(AccessRelation);
413}
414
Johannes Doerferta99130f2014-10-13 12:58:03 +0000415std::string MemoryAccess::getOriginalAccessRelationStr() const {
Tobias Grosser5d453812011-10-06 00:04:11 +0000416 return stringFromIslObj(AccessRelation);
417}
418
Johannes Doerferta99130f2014-10-13 12:58:03 +0000419__isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000420 return isl_map_get_space(AccessRelation);
421}
422
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000423__isl_give isl_map *MemoryAccess::getNewAccessRelation() const {
Tobias Grosser166c4222015-09-05 07:46:40 +0000424 return isl_map_copy(NewAccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000425}
426
Tobias Grosser6f730082015-09-05 07:46:47 +0000427std::string MemoryAccess::getNewAccessRelationStr() const {
428 return stringFromIslObj(NewAccessRelation);
429}
430
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000431__isl_give isl_basic_map *
432MemoryAccess::createBasicAccessMap(ScopStmt *Statement) {
Tobias Grosser084d8f72012-05-29 09:29:44 +0000433 isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1);
Tobias Grossered295662012-09-11 13:50:21 +0000434 Space = isl_space_align_params(Space, Statement->getDomainSpace());
Tobias Grosser75805372011-04-29 06:27:02 +0000435
Tobias Grosser084d8f72012-05-29 09:29:44 +0000436 return isl_basic_map_from_domain_and_range(
Tobias Grosserabfbe632013-02-05 12:09:06 +0000437 isl_basic_set_universe(Statement->getDomainSpace()),
438 isl_basic_set_universe(Space));
Tobias Grosser75805372011-04-29 06:27:02 +0000439}
440
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000441// Formalize no out-of-bound access assumption
442//
443// When delinearizing array accesses we optimistically assume that the
444// delinearized accesses do not access out of bound locations (the subscript
445// expression of each array evaluates for each statement instance that is
446// executed to a value that is larger than zero and strictly smaller than the
447// size of the corresponding dimension). The only exception is the outermost
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000448// dimension for which we do not need to assume any upper bound. At this point
449// we formalize this assumption to ensure that at code generation time the
450// relevant run-time checks can be generated.
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000451//
452// To find the set of constraints necessary to avoid out of bound accesses, we
453// first build the set of data locations that are not within array bounds. We
454// then apply the reverse access relation to obtain the set of iterations that
455// may contain invalid accesses and reduce this set of iterations to the ones
456// that are actually executed by intersecting them with the domain of the
457// statement. If we now project out all loop dimensions, we obtain a set of
458// parameters that may cause statement instances to be executed that may
459// possibly yield out of bound memory accesses. The complement of these
460// constraints is the set of constraints that needs to be assumed to ensure such
461// statement instances are never executed.
Michael Krusee2bccbb2015-09-18 19:59:43 +0000462void MemoryAccess::assumeNoOutOfBound() {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000463 isl_space *Space = isl_space_range(getOriginalAccessRelationSpace());
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000464 isl_set *Outside = isl_set_empty(isl_space_copy(Space));
Michael Krusee2bccbb2015-09-18 19:59:43 +0000465 for (int i = 1, Size = Subscripts.size(); i < Size; ++i) {
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000466 isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space));
467 isl_pw_aff *Var =
468 isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i);
469 isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS);
470
471 isl_set *DimOutside;
472
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000473 DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero);
Michael Krusee2bccbb2015-09-18 19:59:43 +0000474 isl_pw_aff *SizeE = Statement->getPwAff(Sizes[i - 1]);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000475
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000476 SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0,
477 Statement->getNumIterators());
478 SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in,
479 isl_space_dim(Space, isl_dim_set));
480 SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in,
481 isl_space_get_tuple_id(Space, isl_dim_set));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000482
Tobias Grosserf57d63f2014-08-03 21:07:30 +0000483 DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var));
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000484
485 Outside = isl_set_union(Outside, DimOutside);
486 }
487
488 Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation()));
489 Outside = isl_set_intersect(Outside, Statement->getDomain());
490 Outside = isl_set_params(Outside);
Tobias Grosserf54bb772015-06-26 12:09:28 +0000491
492 // Remove divs to avoid the construction of overly complicated assumptions.
493 // Doing so increases the set of parameter combinations that are assumed to
494 // not appear. This is always save, but may make the resulting run-time check
495 // bail out more often than strictly necessary.
496 Outside = isl_set_remove_divs(Outside);
Tobias Grosser5e6813d2014-07-02 17:47:48 +0000497 Outside = isl_set_complement(Outside);
498 Statement->getParent()->addAssumption(Outside);
499 isl_space_free(Space);
500}
501
Johannes Doerferte7044942015-02-24 11:58:30 +0000502void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) {
503 ScalarEvolution *SE = Statement->getParent()->getSE();
504
505 Value *Ptr = getPointerOperand(*getAccessInstruction());
506 if (!Ptr || !SE->isSCEVable(Ptr->getType()))
507 return;
508
509 auto *PtrSCEV = SE->getSCEV(Ptr);
510 if (isa<SCEVCouldNotCompute>(PtrSCEV))
511 return;
512
513 auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV);
514 if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV))
515 PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV);
516
517 const ConstantRange &Range = SE->getSignedRange(PtrSCEV);
518 if (Range.isFullSet())
519 return;
520
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000521 bool isWrapping = Range.isSignWrappedSet();
Johannes Doerferte7044942015-02-24 11:58:30 +0000522 unsigned BW = Range.getBitWidth();
Johannes Doerferte4bd53b2015-03-08 19:49:50 +0000523 const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin();
524 const auto UB = isWrapping ? Range.getUpper() : Range.getSignedMax();
525
526 auto Min = LB.sdiv(APInt(BW, ElementSize));
527 auto Max = (UB - APInt(BW, 1)).sdiv(APInt(BW, ElementSize));
Johannes Doerferte7044942015-02-24 11:58:30 +0000528
529 isl_set *AccessRange = isl_map_range(isl_map_copy(AccessRelation));
530 AccessRange =
531 addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, isl_dim_set);
532 AccessRelation = isl_map_intersect_range(AccessRelation, AccessRange);
533}
534
Michael Krusee2bccbb2015-09-18 19:59:43 +0000535__isl_give isl_map *MemoryAccess::foldAccess(__isl_take isl_map *AccessRelation,
Tobias Grosser619190d2015-03-30 17:22:28 +0000536 ScopStmt *Statement) {
Michael Krusee2bccbb2015-09-18 19:59:43 +0000537 int Size = Subscripts.size();
Tobias Grosser619190d2015-03-30 17:22:28 +0000538
539 for (int i = Size - 2; i >= 0; --i) {
540 isl_space *Space;
541 isl_map *MapOne, *MapTwo;
Michael Krusee2bccbb2015-09-18 19:59:43 +0000542 isl_pw_aff *DimSize = Statement->getPwAff(Sizes[i]);
Tobias Grosser619190d2015-03-30 17:22:28 +0000543
544 isl_space *SpaceSize = isl_pw_aff_get_space(DimSize);
545 isl_pw_aff_free(DimSize);
546 isl_id *ParamId = isl_space_get_dim_id(SpaceSize, isl_dim_param, 0);
547
548 Space = isl_map_get_space(AccessRelation);
549 Space = isl_space_map_from_set(isl_space_range(Space));
550 Space = isl_space_align_params(Space, SpaceSize);
551
552 int ParamLocation = isl_space_find_dim_by_id(Space, isl_dim_param, ParamId);
553 isl_id_free(ParamId);
554
555 MapOne = isl_map_universe(isl_space_copy(Space));
556 for (int j = 0; j < Size; ++j)
557 MapOne = isl_map_equate(MapOne, isl_dim_in, j, isl_dim_out, j);
558 MapOne = isl_map_lower_bound_si(MapOne, isl_dim_in, i + 1, 0);
559
560 MapTwo = isl_map_universe(isl_space_copy(Space));
561 for (int j = 0; j < Size; ++j)
562 if (j < i || j > i + 1)
563 MapTwo = isl_map_equate(MapTwo, isl_dim_in, j, isl_dim_out, j);
564
565 isl_local_space *LS = isl_local_space_from_space(Space);
566 isl_constraint *C;
567 C = isl_equality_alloc(isl_local_space_copy(LS));
568 C = isl_constraint_set_constant_si(C, -1);
569 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, 1);
570 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, -1);
571 MapTwo = isl_map_add_constraint(MapTwo, C);
572 C = isl_equality_alloc(LS);
573 C = isl_constraint_set_coefficient_si(C, isl_dim_in, i + 1, 1);
574 C = isl_constraint_set_coefficient_si(C, isl_dim_out, i + 1, -1);
575 C = isl_constraint_set_coefficient_si(C, isl_dim_param, ParamLocation, 1);
576 MapTwo = isl_map_add_constraint(MapTwo, C);
577 MapTwo = isl_map_upper_bound_si(MapTwo, isl_dim_in, i + 1, -1);
578
579 MapOne = isl_map_union(MapOne, MapTwo);
580 AccessRelation = isl_map_apply_range(AccessRelation, MapOne);
581 }
582 return AccessRelation;
583}
584
Michael Krusee2bccbb2015-09-18 19:59:43 +0000585void MemoryAccess::buildAccessRelation(const ScopArrayInfo *SAI) {
586 assert(!AccessRelation && "AccessReltation already built");
Tobias Grosser75805372011-04-29 06:27:02 +0000587
Michael Krusee2bccbb2015-09-18 19:59:43 +0000588 isl_ctx *Ctx = isl_id_get_ctx(Id);
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000589 isl_id *BaseAddrId = SAI->getBasePtrId();
Tobias Grosser5683df42011-11-09 22:34:34 +0000590
Michael Krusee2bccbb2015-09-18 19:59:43 +0000591 if (!isAffine()) {
Tobias Grosser4f967492013-06-23 05:21:18 +0000592 // We overapproximate non-affine accesses with a possible access to the
593 // whole array. For read accesses it does not make a difference, if an
594 // access must or may happen. However, for write accesses it is important to
595 // differentiate between writes that must happen and writes that may happen.
Tobias Grosser04d6ae62013-06-23 06:04:54 +0000596 AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000597 AccessRelation =
598 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
Johannes Doerferte7044942015-02-24 11:58:30 +0000599
Michael Krusee2bccbb2015-09-18 19:59:43 +0000600 computeBoundsOnAccessRelation(getElemSizeInBytes());
Tobias Grossera1879642011-12-20 10:43:14 +0000601 return;
602 }
603
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000604 isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0);
Tobias Grosser79baa212014-04-10 08:38:02 +0000605 AccessRelation = isl_map_universe(Space);
Tobias Grossera1879642011-12-20 10:43:14 +0000606
Michael Krusee2bccbb2015-09-18 19:59:43 +0000607 for (int i = 0, Size = Subscripts.size(); i < Size; ++i) {
608 isl_pw_aff *Affine = Statement->getPwAff(Subscripts[i]);
Tobias Grosser75805372011-04-29 06:27:02 +0000609
Sebastian Pop422e33f2014-06-03 18:16:31 +0000610 if (Size == 1) {
611 // For the non delinearized arrays, divide the access function of the last
612 // subscript by the size of the elements in the array.
Sebastian Pop18016682014-04-08 21:20:44 +0000613 //
614 // A stride one array access in C expressed as A[i] is expressed in
615 // LLVM-IR as something like A[i * elementsize]. This hides the fact that
616 // two subsequent values of 'i' index two values that are stored next to
617 // each other in memory. By this division we make this characteristic
618 // obvious again.
Michael Krusee2bccbb2015-09-18 19:59:43 +0000619 isl_val *v = isl_val_int_from_si(Ctx, getElemSizeInBytes());
Sebastian Pop18016682014-04-08 21:20:44 +0000620 Affine = isl_pw_aff_scale_down_val(Affine, v);
621 }
622
623 isl_map *SubscriptMap = isl_map_from_pw_aff(Affine);
624
Tobias Grosser79baa212014-04-10 08:38:02 +0000625 AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap);
Sebastian Pop18016682014-04-08 21:20:44 +0000626 }
627
Michael Krusee2bccbb2015-09-18 19:59:43 +0000628 if (Sizes.size() > 1 && !isa<SCEVConstant>(Sizes[0]))
629 AccessRelation = foldAccess(AccessRelation, Statement);
Tobias Grosser619190d2015-03-30 17:22:28 +0000630
Tobias Grosser79baa212014-04-10 08:38:02 +0000631 Space = Statement->getDomainSpace();
Tobias Grosserabfbe632013-02-05 12:09:06 +0000632 AccessRelation = isl_map_set_tuple_id(
633 AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set));
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000634 AccessRelation =
635 isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId);
636
Michael Krusee2bccbb2015-09-18 19:59:43 +0000637 assumeNoOutOfBound();
Tobias Grosseraa660a92015-03-30 00:07:50 +0000638 AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain());
Johannes Doerfert5d83f092014-07-29 08:37:55 +0000639 isl_space_free(Space);
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000640}
Tobias Grosser30b8a092011-08-18 07:51:37 +0000641
Michael Krusecac948e2015-10-02 13:53:07 +0000642MemoryAccess::MemoryAccess(ScopStmt *Stmt, Instruction *AccessInst,
643 __isl_take isl_id *Id, AccessType Type,
644 Value *BaseAddress, unsigned ElemBytes, bool Affine,
Michael Krusee2bccbb2015-09-18 19:59:43 +0000645 ArrayRef<const SCEV *> Subscripts,
646 ArrayRef<const SCEV *> Sizes, Value *AccessValue,
Michael Kruse8d0b7342015-09-25 21:21:00 +0000647 AccessOrigin Origin, StringRef BaseName)
Michael Krusecac948e2015-10-02 13:53:07 +0000648 : Id(Id), Origin(Origin), AccType(Type), RedType(RT_NONE), Statement(Stmt),
649 BaseAddr(BaseAddress), BaseName(BaseName), ElemBytes(ElemBytes),
650 Sizes(Sizes.begin(), Sizes.end()), AccessInstruction(AccessInst),
651 AccessValue(AccessValue), IsAffine(Affine),
Michael Krusee2bccbb2015-09-18 19:59:43 +0000652 Subscripts(Subscripts.begin(), Subscripts.end()), AccessRelation(nullptr),
653 NewAccessRelation(nullptr) {}
654
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000655void MemoryAccess::realignParams() {
Tobias Grosser6defb5b2014-04-10 08:37:44 +0000656 isl_space *ParamSpace = Statement->getParent()->getParamSpace();
Tobias Grosser37487052011-10-06 00:03:42 +0000657 AccessRelation = isl_map_align_params(AccessRelation, ParamSpace);
Tobias Grosser75805372011-04-29 06:27:02 +0000658}
659
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000660const std::string MemoryAccess::getReductionOperatorStr() const {
661 return MemoryAccess::getReductionOperatorStr(getReductionType());
662}
663
Tobias Grosser6f48e0f2015-05-15 09:58:32 +0000664__isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); }
665
Johannes Doerfertf6183392014-07-01 20:52:51 +0000666raw_ostream &polly::operator<<(raw_ostream &OS,
667 MemoryAccess::ReductionType RT) {
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000668 if (RT == MemoryAccess::RT_NONE)
Johannes Doerfertf6183392014-07-01 20:52:51 +0000669 OS << "NONE";
Johannes Doerfert32868bf2014-08-01 08:13:25 +0000670 else
671 OS << MemoryAccess::getReductionOperatorStr(RT);
Johannes Doerfertf6183392014-07-01 20:52:51 +0000672 return OS;
673}
674
Tobias Grosser75805372011-04-29 06:27:02 +0000675void MemoryAccess::print(raw_ostream &OS) const {
Johannes Doerfert4c7ce472014-10-08 10:11:33 +0000676 switch (AccType) {
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000677 case READ:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000678 OS.indent(12) << "ReadAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000679 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000680 case MUST_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000681 OS.indent(12) << "MustWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000682 break;
Tobias Grosserb58f6a42013-07-13 20:41:24 +0000683 case MAY_WRITE:
Johannes Doerfert6780bc32014-06-26 18:47:03 +0000684 OS.indent(12) << "MayWriteAccess :=\t";
Tobias Grosser4f967492013-06-23 05:21:18 +0000685 break;
686 }
Johannes Doerfert0ff23ec2015-02-06 20:13:15 +0000687 OS << "[Reduction Type: " << getReductionType() << "] ";
Michael Kruse8d0b7342015-09-25 21:21:00 +0000688 OS << "[Scalar: " << isImplicit() << "]\n";
Johannes Doerferta99130f2014-10-13 12:58:03 +0000689 OS.indent(16) << getOriginalAccessRelationStr() << ";\n";
Tobias Grosser6f730082015-09-05 07:46:47 +0000690 if (hasNewAccessRelation())
691 OS.indent(11) << "new: " << getNewAccessRelationStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +0000692}
693
Tobias Grosser74394f02013-01-14 22:40:23 +0000694void MemoryAccess::dump() const { print(errs()); }
Tobias Grosser75805372011-04-29 06:27:02 +0000695
696// Create a map in the size of the provided set domain, that maps from the
697// one element of the provided set domain to another element of the provided
698// set domain.
699// The mapping is limited to all points that are equal in all but the last
700// dimension and for which the last dimension of the input is strict smaller
701// than the last dimension of the output.
702//
703// getEqualAndLarger(set[i0, i1, ..., iX]):
704//
705// set[i0, i1, ..., iX] -> set[o0, o1, ..., oX]
706// : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX
707//
Tobias Grosserf5338802011-10-06 00:03:35 +0000708static isl_map *getEqualAndLarger(isl_space *setDomain) {
Tobias Grosserc327932c2012-02-01 14:23:36 +0000709 isl_space *Space = isl_space_map_from_set(setDomain);
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000710 isl_map *Map = isl_map_universe(Space);
Sebastian Pop40408762013-10-04 17:14:53 +0000711 unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1;
Tobias Grosser75805372011-04-29 06:27:02 +0000712
713 // Set all but the last dimension to be equal for the input and output
714 //
715 // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX]
716 // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1)
Sebastian Pop40408762013-10-04 17:14:53 +0000717 for (unsigned i = 0; i < lastDimension; ++i)
Tobias Grosserc327932c2012-02-01 14:23:36 +0000718 Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i);
Tobias Grosser75805372011-04-29 06:27:02 +0000719
720 // Set the last dimension of the input to be strict smaller than the
721 // last dimension of the output.
722 //
723 // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX
Tobias Grosser1b6ea572015-05-21 19:02:44 +0000724 Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out,
725 lastDimension);
Tobias Grosserc327932c2012-02-01 14:23:36 +0000726 return Map;
Tobias Grosser75805372011-04-29 06:27:02 +0000727}
728
Tobias Grosser4f663aa2015-03-30 11:52:59 +0000729__isl_give isl_set *
730MemoryAccess::getStride(__isl_take const isl_map *Schedule) const {
Tobias Grosserabfbe632013-02-05 12:09:06 +0000731 isl_map *S = const_cast<isl_map *>(Schedule);
Johannes Doerferta99130f2014-10-13 12:58:03 +0000732 isl_map *AccessRelation = getAccessRelation();
Sebastian Popa00a0292012-12-18 07:46:06 +0000733 isl_space *Space = isl_space_range(isl_map_get_space(S));
734 isl_map *NextScatt = getEqualAndLarger(Space);
Tobias Grosser75805372011-04-29 06:27:02 +0000735
Sebastian Popa00a0292012-12-18 07:46:06 +0000736 S = isl_map_reverse(S);
737 NextScatt = isl_map_lexmin(NextScatt);
Tobias Grosser75805372011-04-29 06:27:02 +0000738
Sebastian Popa00a0292012-12-18 07:46:06 +0000739 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S));
740 NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation));
741 NextScatt = isl_map_apply_domain(NextScatt, S);
742 NextScatt = isl_map_apply_domain(NextScatt, AccessRelation);
Tobias Grosser75805372011-04-29 06:27:02 +0000743
Sebastian Popa00a0292012-12-18 07:46:06 +0000744 isl_set *Deltas = isl_map_deltas(NextScatt);
745 return Deltas;
Tobias Grosser75805372011-04-29 06:27:02 +0000746}
747
Sebastian Popa00a0292012-12-18 07:46:06 +0000748bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule,
Tobias Grosser28dd4862012-01-24 16:42:16 +0000749 int StrideWidth) const {
750 isl_set *Stride, *StrideX;
751 bool IsStrideX;
Tobias Grosser75805372011-04-29 06:27:02 +0000752
Sebastian Popa00a0292012-12-18 07:46:06 +0000753 Stride = getStride(Schedule);
Tobias Grosser28dd4862012-01-24 16:42:16 +0000754 StrideX = isl_set_universe(isl_set_get_space(Stride));
Tobias Grosser01c8f5f2015-08-24 22:20:46 +0000755 for (unsigned i = 0; i < isl_set_dim(StrideX, isl_dim_set) - 1; i++)
756 StrideX = isl_set_fix_si(StrideX, isl_dim_set, i, 0);
757 StrideX = isl_set_fix_si(StrideX, isl_dim_set,
758 isl_set_dim(StrideX, isl_dim_set) - 1, StrideWidth);
Roman Gareevf2bd72e2015-08-18 16:12:05 +0000759 IsStrideX = isl_set_is_subset(Stride, StrideX);
Tobias Grosser75805372011-04-29 06:27:02 +0000760
Tobias Grosser28dd4862012-01-24 16:42:16 +0000761 isl_set_free(StrideX);
Tobias Grosserdea98232012-01-17 20:34:27 +0000762 isl_set_free(Stride);
Tobias Grosserb76f38532011-08-20 11:11:25 +0000763
Tobias Grosser28dd4862012-01-24 16:42:16 +0000764 return IsStrideX;
765}
766
Sebastian Popa00a0292012-12-18 07:46:06 +0000767bool MemoryAccess::isStrideZero(const isl_map *Schedule) const {
768 return isStrideX(Schedule, 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000769}
770
Sebastian Popa00a0292012-12-18 07:46:06 +0000771bool MemoryAccess::isStrideOne(const isl_map *Schedule) const {
772 return isStrideX(Schedule, 1);
Tobias Grosser75805372011-04-29 06:27:02 +0000773}
774
Tobias Grosser166c4222015-09-05 07:46:40 +0000775void MemoryAccess::setNewAccessRelation(isl_map *NewAccess) {
776 isl_map_free(NewAccessRelation);
777 NewAccessRelation = NewAccess;
Raghesh Aloor3cb66282011-07-12 17:14:03 +0000778}
Tobias Grosser75805372011-04-29 06:27:02 +0000779
780//===----------------------------------------------------------------------===//
Tobias Grossercf3942d2011-10-06 00:04:05 +0000781
Tobias Grosser808cd692015-07-14 09:33:13 +0000782isl_map *ScopStmt::getSchedule() const {
783 isl_set *Domain = getDomain();
784 if (isl_set_is_empty(Domain)) {
785 isl_set_free(Domain);
786 return isl_map_from_aff(
787 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
788 }
789 auto *Schedule = getParent()->getSchedule();
790 Schedule = isl_union_map_intersect_domain(
791 Schedule, isl_union_set_from_set(isl_set_copy(Domain)));
792 if (isl_union_map_is_empty(Schedule)) {
793 isl_set_free(Domain);
794 isl_union_map_free(Schedule);
795 return isl_map_from_aff(
796 isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace())));
797 }
798 auto *M = isl_map_from_union_map(Schedule);
799 M = isl_map_coalesce(M);
800 M = isl_map_gist_domain(M, Domain);
801 M = isl_map_coalesce(M);
802 return M;
803}
Tobias Grossercf3942d2011-10-06 00:04:05 +0000804
Johannes Doerfert574182d2015-08-12 10:19:50 +0000805__isl_give isl_pw_aff *ScopStmt::getPwAff(const SCEV *E) {
Johannes Doerfertcef616f2015-09-15 22:49:04 +0000806 return getParent()->getPwAff(E, isBlockStmt() ? getBasicBlock()
807 : getRegion()->getEntry());
Johannes Doerfert574182d2015-08-12 10:19:50 +0000808}
809
Tobias Grosser37eb4222014-02-20 21:43:54 +0000810void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) {
811 assert(isl_set_is_subset(NewDomain, Domain) &&
812 "New domain is not a subset of old domain!");
813 isl_set_free(Domain);
814 Domain = NewDomain;
Tobias Grosser75805372011-04-29 06:27:02 +0000815}
816
Michael Krusecac948e2015-10-02 13:53:07 +0000817void ScopStmt::buildAccessRelations() {
818 for (MemoryAccess *Access : MemAccs) {
819 Type *ElementType = Access->getAccessValue()->getType();
Johannes Doerfert1a28a892014-10-05 11:32:18 +0000820
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000821 const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo(
Michael Krusecac948e2015-10-02 13:53:07 +0000822 Access->getBaseAddr(), ElementType, Access->Sizes, Access->isPHI());
Johannes Doerfert80ef1102014-11-07 08:31:31 +0000823
Michael Krusecac948e2015-10-02 13:53:07 +0000824 Access->buildAccessRelation(SAI);
Tobias Grosser75805372011-04-29 06:27:02 +0000825 }
826}
827
Michael Krusecac948e2015-10-02 13:53:07 +0000828void ScopStmt::addAccess(MemoryAccess *Access) {
829 Instruction *AccessInst = Access->getAccessInstruction();
830
831 MemoryAccessList *&MAL = InstructionToAccess[AccessInst];
832 if (!MAL)
833 MAL = new MemoryAccessList();
834 MAL->emplace_front(Access);
835 MemAccs.push_back(MAL->front());
836}
837
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000838void ScopStmt::realignParams() {
Johannes Doerfertf6752892014-06-13 18:01:45 +0000839 for (MemoryAccess *MA : *this)
840 MA->realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000841
842 Domain = isl_set_align_params(Domain, Parent.getParamSpace());
Tobias Grosser8cae72f2011-11-08 15:41:08 +0000843}
844
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +0000845/// @brief Add @p BSet to the set @p User if @p BSet is bounded.
846static isl_stat collectBoundedParts(__isl_take isl_basic_set *BSet,
847 void *User) {
848 isl_set **BoundedParts = static_cast<isl_set **>(User);
849 if (isl_basic_set_is_bounded(BSet))
850 *BoundedParts = isl_set_union(*BoundedParts, isl_set_from_basic_set(BSet));
851 else
852 isl_basic_set_free(BSet);
853 return isl_stat_ok;
854}
855
856/// @brief Return the bounded parts of @p S.
857static __isl_give isl_set *collectBoundedParts(__isl_take isl_set *S) {
858 isl_set *BoundedParts = isl_set_empty(isl_set_get_space(S));
859 isl_set_foreach_basic_set(S, collectBoundedParts, &BoundedParts);
860 isl_set_free(S);
861 return BoundedParts;
862}
863
864/// @brief Compute the (un)bounded parts of @p S wrt. to dimension @p Dim.
865///
866/// @returns A separation of @p S into first an unbounded then a bounded subset,
867/// both with regards to the dimension @p Dim.
868static std::pair<__isl_give isl_set *, __isl_give isl_set *>
869partitionSetParts(__isl_take isl_set *S, unsigned Dim) {
870
871 for (unsigned u = 0, e = isl_set_n_dim(S); u < e; u++)
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +0000872 S = isl_set_lower_bound_si(S, isl_dim_set, u, 0);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +0000873
874 unsigned NumDimsS = isl_set_n_dim(S);
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +0000875 isl_set *OnlyDimS = isl_set_copy(S);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +0000876
877 // Remove dimensions that are greater than Dim as they are not interesting.
878 assert(NumDimsS >= Dim + 1);
879 OnlyDimS =
880 isl_set_project_out(OnlyDimS, isl_dim_set, Dim + 1, NumDimsS - Dim - 1);
881
882 // Create artificial parametric upper bounds for dimensions smaller than Dim
883 // as we are not interested in them.
884 OnlyDimS = isl_set_insert_dims(OnlyDimS, isl_dim_param, 0, Dim);
885 for (unsigned u = 0; u < Dim; u++) {
886 isl_constraint *C = isl_inequality_alloc(
887 isl_local_space_from_space(isl_set_get_space(OnlyDimS)));
888 C = isl_constraint_set_coefficient_si(C, isl_dim_param, u, 1);
889 C = isl_constraint_set_coefficient_si(C, isl_dim_set, u, -1);
890 OnlyDimS = isl_set_add_constraint(OnlyDimS, C);
891 }
892
893 // Collect all bounded parts of OnlyDimS.
894 isl_set *BoundedParts = collectBoundedParts(OnlyDimS);
895
896 // Create the dimensions greater than Dim again.
897 BoundedParts = isl_set_insert_dims(BoundedParts, isl_dim_set, Dim + 1,
898 NumDimsS - Dim - 1);
899
900 // Remove the artificial upper bound parameters again.
901 BoundedParts = isl_set_remove_dims(BoundedParts, isl_dim_param, 0, Dim);
902
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +0000903 isl_set *UnboundedParts = isl_set_subtract(S, isl_set_copy(BoundedParts));
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +0000904 return std::make_pair(UnboundedParts, BoundedParts);
905}
906
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000907/// @brief Set the dimension Ids from @p From in @p To.
908static __isl_give isl_set *setDimensionIds(__isl_keep isl_set *From,
909 __isl_take isl_set *To) {
910 for (unsigned u = 0, e = isl_set_n_dim(From); u < e; u++) {
911 isl_id *DimId = isl_set_get_dim_id(From, isl_dim_set, u);
912 To = isl_set_set_dim_id(To, isl_dim_set, u, DimId);
913 }
914 return To;
915}
916
917/// @brief Create the conditions under which @p L @p Pred @p R is true.
Johannes Doerfert96425c22015-08-30 21:13:53 +0000918static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred,
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000919 __isl_take isl_pw_aff *L,
920 __isl_take isl_pw_aff *R) {
Johannes Doerfert96425c22015-08-30 21:13:53 +0000921 switch (Pred) {
922 case ICmpInst::ICMP_EQ:
923 return isl_pw_aff_eq_set(L, R);
924 case ICmpInst::ICMP_NE:
925 return isl_pw_aff_ne_set(L, R);
926 case ICmpInst::ICMP_SLT:
927 return isl_pw_aff_lt_set(L, R);
928 case ICmpInst::ICMP_SLE:
929 return isl_pw_aff_le_set(L, R);
930 case ICmpInst::ICMP_SGT:
931 return isl_pw_aff_gt_set(L, R);
932 case ICmpInst::ICMP_SGE:
933 return isl_pw_aff_ge_set(L, R);
934 case ICmpInst::ICMP_ULT:
935 return isl_pw_aff_lt_set(L, R);
936 case ICmpInst::ICMP_UGT:
937 return isl_pw_aff_gt_set(L, R);
938 case ICmpInst::ICMP_ULE:
939 return isl_pw_aff_le_set(L, R);
940 case ICmpInst::ICMP_UGE:
941 return isl_pw_aff_ge_set(L, R);
942 default:
943 llvm_unreachable("Non integer predicate not supported");
944 }
945}
946
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000947/// @brief Create the conditions under which @p L @p Pred @p R is true.
948///
949/// Helper function that will make sure the dimensions of the result have the
950/// same isl_id's as the @p Domain.
951static __isl_give isl_set *buildConditionSet(ICmpInst::Predicate Pred,
952 __isl_take isl_pw_aff *L,
953 __isl_take isl_pw_aff *R,
954 __isl_keep isl_set *Domain) {
955 isl_set *ConsequenceCondSet = buildConditionSet(Pred, L, R);
956 return setDimensionIds(Domain, ConsequenceCondSet);
957}
958
959/// @brief Build the conditions sets for the switch @p SI in the @p Domain.
Johannes Doerfert96425c22015-08-30 21:13:53 +0000960///
961/// This will fill @p ConditionSets with the conditions under which control
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000962/// will be moved from @p SI to its successors. Hence, @p ConditionSets will
963/// have as many elements as @p SI has successors.
Johannes Doerfert96425c22015-08-30 21:13:53 +0000964static void
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000965buildConditionSets(Scop &S, SwitchInst *SI, Loop *L, __isl_keep isl_set *Domain,
Johannes Doerfert96425c22015-08-30 21:13:53 +0000966 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
967
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000968 Value *Condition = getConditionFromTerminator(SI);
969 assert(Condition && "No condition for switch");
970
971 ScalarEvolution &SE = *S.getSE();
972 BasicBlock *BB = SI->getParent();
973 isl_pw_aff *LHS, *RHS;
974 LHS = S.getPwAff(SE.getSCEVAtScope(Condition, L), BB);
975
976 unsigned NumSuccessors = SI->getNumSuccessors();
977 ConditionSets.resize(NumSuccessors);
978 for (auto &Case : SI->cases()) {
979 unsigned Idx = Case.getSuccessorIndex();
980 ConstantInt *CaseValue = Case.getCaseValue();
981
982 RHS = S.getPwAff(SE.getSCEV(CaseValue), BB);
983 isl_set *CaseConditionSet =
984 buildConditionSet(ICmpInst::ICMP_EQ, isl_pw_aff_copy(LHS), RHS, Domain);
985 ConditionSets[Idx] = isl_set_coalesce(
986 isl_set_intersect(CaseConditionSet, isl_set_copy(Domain)));
987 }
988
989 assert(ConditionSets[0] == nullptr && "Default condition set was set");
990 isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]);
991 for (unsigned u = 2; u < NumSuccessors; u++)
992 ConditionSetUnion =
993 isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u]));
994 ConditionSets[0] = setDimensionIds(
995 Domain, isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion));
996
997 S.markAsOptimized();
998 isl_pw_aff_free(LHS);
999}
1000
Johannes Doerfert9b1f9c82015-10-11 13:21:03 +00001001/// @brief Build the conditions sets for the branch condition @p Condition in
1002/// the @p Domain.
1003///
1004/// This will fill @p ConditionSets with the conditions under which control
1005/// will be moved from @p TI to its successors. Hence, @p ConditionSets will
1006/// have as many elements as @p TI has successors.
1007static void
1008buildConditionSets(Scop &S, Value *Condition, TerminatorInst *TI, Loop *L,
1009 __isl_keep isl_set *Domain,
1010 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
1011
1012 isl_set *ConsequenceCondSet = nullptr;
1013 if (auto *CCond = dyn_cast<ConstantInt>(Condition)) {
1014 if (CCond->isZero())
1015 ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
1016 else
1017 ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain));
1018 } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) {
1019 auto Opcode = BinOp->getOpcode();
1020 assert(Opcode == Instruction::And || Opcode == Instruction::Or);
1021
1022 buildConditionSets(S, BinOp->getOperand(0), TI, L, Domain, ConditionSets);
1023 buildConditionSets(S, BinOp->getOperand(1), TI, L, Domain, ConditionSets);
1024
1025 isl_set_free(ConditionSets.pop_back_val());
1026 isl_set *ConsCondPart0 = ConditionSets.pop_back_val();
1027 isl_set_free(ConditionSets.pop_back_val());
1028 isl_set *ConsCondPart1 = ConditionSets.pop_back_val();
1029
1030 if (Opcode == Instruction::And)
1031 ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1);
1032 else
1033 ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1);
1034 } else {
1035 auto *ICond = dyn_cast<ICmpInst>(Condition);
1036 assert(ICond &&
1037 "Condition of exiting branch was neither constant nor ICmp!");
1038
1039 ScalarEvolution &SE = *S.getSE();
1040 BasicBlock *BB = TI->getParent();
1041 isl_pw_aff *LHS, *RHS;
1042 LHS = S.getPwAff(SE.getSCEVAtScope(ICond->getOperand(0), L), BB);
1043 RHS = S.getPwAff(SE.getSCEVAtScope(ICond->getOperand(1), L), BB);
1044 ConsequenceCondSet =
1045 buildConditionSet(ICond->getPredicate(), LHS, RHS, Domain);
1046 }
1047
1048 assert(ConsequenceCondSet);
1049 isl_set *AlternativeCondSet =
1050 isl_set_complement(isl_set_copy(ConsequenceCondSet));
1051
1052 ConditionSets.push_back(isl_set_coalesce(
1053 isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain))));
1054 ConditionSets.push_back(isl_set_coalesce(
1055 isl_set_intersect(AlternativeCondSet, isl_set_copy(Domain))));
1056}
1057
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001058/// @brief Build the conditions sets for the terminator @p TI in the @p Domain.
1059///
1060/// This will fill @p ConditionSets with the conditions under which control
1061/// will be moved from @p TI to its successors. Hence, @p ConditionSets will
1062/// have as many elements as @p TI has successors.
1063static void
1064buildConditionSets(Scop &S, TerminatorInst *TI, Loop *L,
1065 __isl_keep isl_set *Domain,
1066 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
1067
1068 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
1069 return buildConditionSets(S, SI, L, Domain, ConditionSets);
1070
1071 assert(isa<BranchInst>(TI) && "Terminator was neither branch nor switch.");
1072
1073 if (TI->getNumSuccessors() == 1) {
Johannes Doerfert96425c22015-08-30 21:13:53 +00001074 ConditionSets.push_back(isl_set_copy(Domain));
1075 return;
1076 }
1077
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001078 Value *Condition = getConditionFromTerminator(TI);
1079 assert(Condition && "No condition for Terminator");
Johannes Doerfert96425c22015-08-30 21:13:53 +00001080
Johannes Doerfert9b1f9c82015-10-11 13:21:03 +00001081 return buildConditionSets(S, Condition, TI, L, Domain, ConditionSets);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001082}
1083
Johannes Doerfert32ae76e2015-09-10 13:12:02 +00001084void ScopStmt::buildDomain() {
Tobias Grosser084d8f72012-05-29 09:29:44 +00001085 isl_id *Id;
Tobias Grossere19661e2011-10-07 08:46:57 +00001086
Tobias Grosser084d8f72012-05-29 09:29:44 +00001087 Id = isl_id_alloc(getIslCtx(), getBaseName(), this);
1088
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00001089 Domain = getParent()->getDomainConditions(this);
Tobias Grosser084d8f72012-05-29 09:29:44 +00001090 Domain = isl_set_set_tuple_id(Domain, Id);
Tobias Grosser75805372011-04-29 06:27:02 +00001091}
1092
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001093void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) {
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001094 isl_ctx *Ctx = Parent.getIslCtx();
1095 isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace());
1096 Type *Ty = GEP->getPointerOperandType();
1097 ScalarEvolution &SE = *Parent.getSE();
Johannes Doerfert09e36972015-10-07 20:17:36 +00001098 ScopDetection &SD = Parent.getSD();
1099
1100 // The set of loads that are required to be invariant.
1101 auto &ScopRIL = *SD.getRequiredInvariantLoads(&Parent.getRegion());
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001102
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001103 std::vector<const SCEV *> Subscripts;
1104 std::vector<int> Sizes;
1105
Tobias Grosser5fd8c092015-09-17 17:28:15 +00001106 std::tie(Subscripts, Sizes) = getIndexExpressionsFromGEP(GEP, SE);
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001107
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001108 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001109 Ty = PtrTy->getElementType();
1110 }
1111
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001112 int IndexOffset = Subscripts.size() - Sizes.size();
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001113
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001114 assert(IndexOffset <= 1 && "Unexpected large index offset");
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001115
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001116 for (size_t i = 0; i < Sizes.size(); i++) {
1117 auto Expr = Subscripts[i + IndexOffset];
1118 auto Size = Sizes[i];
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001119
Johannes Doerfert09e36972015-10-07 20:17:36 +00001120 InvariantLoadsSetTy AccessILS;
1121 if (!isAffineExpr(&Parent.getRegion(), Expr, SE, nullptr, &AccessILS))
1122 continue;
1123
1124 bool NonAffine = false;
1125 for (LoadInst *LInst : AccessILS)
1126 if (!ScopRIL.count(LInst))
1127 NonAffine = true;
1128
1129 if (NonAffine)
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001130 continue;
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001131
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001132 isl_pw_aff *AccessOffset = getPwAff(Expr);
1133 AccessOffset =
1134 isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId());
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001135
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001136 isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain(
1137 isl_local_space_copy(LSpace), isl_val_int_from_si(Ctx, Size)));
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001138
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001139 isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize);
1140 OutOfBound = isl_set_intersect(getDomain(), OutOfBound);
1141 OutOfBound = isl_set_params(OutOfBound);
1142 isl_set *InBound = isl_set_complement(OutOfBound);
1143 isl_set *Executed = isl_set_params(getDomain());
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001144
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001145 // A => B == !A or B
1146 isl_set *InBoundIfExecuted =
1147 isl_set_union(isl_set_complement(Executed), InBound);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001148
Tobias Grosserfaf8f6f2015-09-17 15:47:52 +00001149 Parent.addAssumption(InBoundIfExecuted);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001150 }
1151
1152 isl_local_space_free(LSpace);
1153}
1154
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001155void ScopStmt::deriveAssumptions(BasicBlock *Block) {
1156 for (Instruction &Inst : *Block)
Tobias Grosser7b50bee2014-11-25 10:51:12 +00001157 if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst))
1158 deriveAssumptionsFromGEP(GEP);
1159}
1160
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001161void ScopStmt::collectSurroundingLoops() {
1162 for (unsigned u = 0, e = isl_set_n_dim(Domain); u < e; u++) {
1163 isl_id *DimId = isl_set_get_dim_id(Domain, isl_dim_set, u);
1164 NestLoops.push_back(static_cast<Loop *>(isl_id_get_user(DimId)));
1165 isl_id_free(DimId);
1166 }
1167}
1168
Michael Kruse9d080092015-09-11 21:41:48 +00001169ScopStmt::ScopStmt(Scop &parent, Region &R)
Michael Krusecac948e2015-10-02 13:53:07 +00001170 : Parent(parent), Domain(nullptr), BB(nullptr), R(&R), Build(nullptr) {
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001171
Tobias Grosser16c44032015-07-09 07:31:45 +00001172 BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), "");
Johannes Doerfertff9d1982015-02-24 12:00:50 +00001173}
1174
Michael Kruse9d080092015-09-11 21:41:48 +00001175ScopStmt::ScopStmt(Scop &parent, BasicBlock &bb)
Michael Krusecac948e2015-10-02 13:53:07 +00001176 : Parent(parent), Domain(nullptr), BB(&bb), R(nullptr), Build(nullptr) {
Tobias Grosser75805372011-04-29 06:27:02 +00001177
Johannes Doerfert79fc23f2014-07-24 23:48:02 +00001178 BaseName = getIslCompatibleName("Stmt_", &bb, "");
Michael Krusecac948e2015-10-02 13:53:07 +00001179}
1180
1181void ScopStmt::init() {
1182 assert(!Domain && "init must be called only once");
Tobias Grosser75805372011-04-29 06:27:02 +00001183
Johannes Doerfert32ae76e2015-09-10 13:12:02 +00001184 buildDomain();
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001185 collectSurroundingLoops();
Michael Krusecac948e2015-10-02 13:53:07 +00001186 buildAccessRelations();
1187
1188 if (BB) {
1189 deriveAssumptions(BB);
1190 } else {
1191 for (BasicBlock *Block : R->blocks()) {
1192 deriveAssumptions(Block);
1193 }
1194 }
1195
Tobias Grosserd83b8a82015-08-20 19:08:11 +00001196 if (DetectReductions)
1197 checkForReductions();
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001198}
1199
Johannes Doerferte58a0122014-06-27 20:31:28 +00001200/// @brief Collect loads which might form a reduction chain with @p StoreMA
1201///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001202/// Check if the stored value for @p StoreMA is a binary operator with one or
1203/// two loads as operands. If the binary operand is commutative & associative,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001204/// used only once (by @p StoreMA) and its load operands are also used only
1205/// once, we have found a possible reduction chain. It starts at an operand
1206/// load and includes the binary operator and @p StoreMA.
1207///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001208/// Note: We allow only one use to ensure the load and binary operator cannot
Johannes Doerferte58a0122014-06-27 20:31:28 +00001209/// escape this block or into any other store except @p StoreMA.
1210void ScopStmt::collectCandiateReductionLoads(
1211 MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) {
1212 auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction());
1213 if (!Store)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001214 return;
1215
1216 // Skip if there is not one binary operator between the load and the store
1217 auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand());
Johannes Doerferte58a0122014-06-27 20:31:28 +00001218 if (!BinOp)
1219 return;
1220
1221 // Skip if the binary operators has multiple uses
1222 if (BinOp->getNumUses() != 1)
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001223 return;
1224
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001225 // Skip if the opcode of the binary operator is not commutative/associative
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001226 if (!BinOp->isCommutative() || !BinOp->isAssociative())
1227 return;
1228
Johannes Doerfert9890a052014-07-01 00:32:29 +00001229 // Skip if the binary operator is outside the current SCoP
1230 if (BinOp->getParent() != Store->getParent())
1231 return;
1232
Johannes Doerfert0ee1f212014-06-17 17:31:36 +00001233 // Skip if it is a multiplicative reduction and we disabled them
1234 if (DisableMultiplicativeReductions &&
1235 (BinOp->getOpcode() == Instruction::Mul ||
1236 BinOp->getOpcode() == Instruction::FMul))
1237 return;
1238
Johannes Doerferte58a0122014-06-27 20:31:28 +00001239 // Check the binary operator operands for a candidate load
1240 auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0));
1241 auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1));
1242 if (!PossibleLoad0 && !PossibleLoad1)
1243 return;
1244
1245 // A load is only a candidate if it cannot escape (thus has only this use)
1246 if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001247 if (PossibleLoad0->getParent() == Store->getParent())
1248 Loads.push_back(lookupAccessFor(PossibleLoad0));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001249 if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1)
Johannes Doerfert9890a052014-07-01 00:32:29 +00001250 if (PossibleLoad1->getParent() == Store->getParent())
1251 Loads.push_back(lookupAccessFor(PossibleLoad1));
Johannes Doerferte58a0122014-06-27 20:31:28 +00001252}
1253
1254/// @brief Check for reductions in this ScopStmt
1255///
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001256/// Iterate over all store memory accesses and check for valid binary reduction
1257/// like chains. For all candidates we check if they have the same base address
1258/// and there are no other accesses which overlap with them. The base address
1259/// check rules out impossible reductions candidates early. The overlap check,
1260/// together with the "only one user" check in collectCandiateReductionLoads,
Johannes Doerferte58a0122014-06-27 20:31:28 +00001261/// guarantees that none of the intermediate results will escape during
1262/// execution of the loop nest. We basically check here that no other memory
1263/// access can access the same memory as the potential reduction.
1264void ScopStmt::checkForReductions() {
1265 SmallVector<MemoryAccess *, 2> Loads;
1266 SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates;
1267
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001268 // First collect candidate load-store reduction chains by iterating over all
Johannes Doerferte58a0122014-06-27 20:31:28 +00001269 // stores and collecting possible reduction loads.
1270 for (MemoryAccess *StoreMA : MemAccs) {
1271 if (StoreMA->isRead())
1272 continue;
1273
1274 Loads.clear();
1275 collectCandiateReductionLoads(StoreMA, Loads);
1276 for (MemoryAccess *LoadMA : Loads)
1277 Candidates.push_back(std::make_pair(LoadMA, StoreMA));
1278 }
1279
1280 // Then check each possible candidate pair.
1281 for (const auto &CandidatePair : Candidates) {
1282 bool Valid = true;
1283 isl_map *LoadAccs = CandidatePair.first->getAccessRelation();
1284 isl_map *StoreAccs = CandidatePair.second->getAccessRelation();
1285
1286 // Skip those with obviously unequal base addresses.
1287 if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) {
1288 isl_map_free(LoadAccs);
1289 isl_map_free(StoreAccs);
1290 continue;
1291 }
1292
1293 // And check if the remaining for overlap with other memory accesses.
1294 isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs);
1295 AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain());
1296 isl_set *AllAccs = isl_map_range(AllAccsRel);
1297
1298 for (MemoryAccess *MA : MemAccs) {
1299 if (MA == CandidatePair.first || MA == CandidatePair.second)
1300 continue;
1301
1302 isl_map *AccRel =
1303 isl_map_intersect_domain(MA->getAccessRelation(), getDomain());
1304 isl_set *Accs = isl_map_range(AccRel);
1305
1306 if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) {
1307 isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs));
1308 Valid = Valid && isl_set_is_empty(OverlapAccs);
1309 isl_set_free(OverlapAccs);
1310 }
1311 }
1312
1313 isl_set_free(AllAccs);
1314 if (!Valid)
1315 continue;
1316
Johannes Doerfertf6183392014-07-01 20:52:51 +00001317 const LoadInst *Load =
1318 dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction());
1319 MemoryAccess::ReductionType RT =
1320 getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load);
1321
Johannes Doerferte58a0122014-06-27 20:31:28 +00001322 // If no overlapping access was found we mark the load and store as
1323 // reduction like.
Johannes Doerfertf6183392014-07-01 20:52:51 +00001324 CandidatePair.first->markAsReductionLike(RT);
1325 CandidatePair.second->markAsReductionLike(RT);
Johannes Doerferte58a0122014-06-27 20:31:28 +00001326 }
Tobias Grosser75805372011-04-29 06:27:02 +00001327}
1328
Tobias Grosser74394f02013-01-14 22:40:23 +00001329std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); }
Tobias Grosser75805372011-04-29 06:27:02 +00001330
Tobias Grosser54839312015-04-21 11:37:25 +00001331std::string ScopStmt::getScheduleStr() const {
Tobias Grosser808cd692015-07-14 09:33:13 +00001332 auto *S = getSchedule();
1333 auto Str = stringFromIslObj(S);
1334 isl_map_free(S);
1335 return Str;
Tobias Grosser75805372011-04-29 06:27:02 +00001336}
1337
Tobias Grosser74394f02013-01-14 22:40:23 +00001338unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001339
Tobias Grosserf567e1a2015-02-19 22:16:12 +00001340unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001341
Tobias Grosser75805372011-04-29 06:27:02 +00001342const char *ScopStmt::getBaseName() const { return BaseName.c_str(); }
1343
Hongbin Zheng27f3afb2011-04-30 03:26:51 +00001344const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const {
Sebastian Pop860e0212013-02-15 21:26:44 +00001345 return NestLoops[Dimension];
Tobias Grosser75805372011-04-29 06:27:02 +00001346}
1347
Tobias Grosser74394f02013-01-14 22:40:23 +00001348isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); }
Tobias Grosser75805372011-04-29 06:27:02 +00001349
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001350__isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); }
Tobias Grosserd5a7bfc2011-05-06 19:52:19 +00001351
Tobias Grosser6e6c7e02015-03-30 12:22:39 +00001352__isl_give isl_space *ScopStmt::getDomainSpace() const {
Tobias Grosser78d8a3d2012-01-17 20:34:23 +00001353 return isl_set_get_space(Domain);
1354}
1355
Tobias Grosser4f663aa2015-03-30 11:52:59 +00001356__isl_give isl_id *ScopStmt::getDomainId() const {
1357 return isl_set_get_tuple_id(Domain);
1358}
Tobias Grossercd95b772012-08-30 11:49:38 +00001359
Tobias Grosser75805372011-04-29 06:27:02 +00001360ScopStmt::~ScopStmt() {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001361 DeleteContainerSeconds(InstructionToAccess);
Tobias Grosser75805372011-04-29 06:27:02 +00001362 isl_set_free(Domain);
Tobias Grosser75805372011-04-29 06:27:02 +00001363}
1364
1365void ScopStmt::print(raw_ostream &OS) const {
1366 OS << "\t" << getBaseName() << "\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001367 OS.indent(12) << "Domain :=\n";
1368
1369 if (Domain) {
1370 OS.indent(16) << getDomainStr() << ";\n";
1371 } else
1372 OS.indent(16) << "n/a\n";
1373
Tobias Grosser54839312015-04-21 11:37:25 +00001374 OS.indent(12) << "Schedule :=\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001375
1376 if (Domain) {
Tobias Grosser54839312015-04-21 11:37:25 +00001377 OS.indent(16) << getScheduleStr() << ";\n";
Tobias Grosser75805372011-04-29 06:27:02 +00001378 } else
1379 OS.indent(16) << "n/a\n";
1380
Tobias Grosser083d3d32014-06-28 08:59:45 +00001381 for (MemoryAccess *Access : MemAccs)
1382 Access->print(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00001383}
1384
1385void ScopStmt::dump() const { print(dbgs()); }
1386
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00001387void ScopStmt::hoistMemoryAccesses(MemoryAccessList &InvMAs,
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001388 InvariantAccessesTy &InvariantEquivClasses) {
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00001389
1390 // Remove all memory accesses in @p InvMAs from this statement together
1391 // with all scalar accesses that were caused by them. The tricky iteration
1392 // order uses is needed because the MemAccs is a vector and the order in
1393 // which the accesses of each memory access list (MAL) are stored in this
1394 // vector is reversed.
1395 for (MemoryAccess *MA : InvMAs) {
1396 auto &MAL = *lookupAccessesFor(MA->getAccessInstruction());
1397 MAL.reverse();
1398
1399 auto MALIt = MAL.begin();
1400 auto MALEnd = MAL.end();
1401 auto MemAccsIt = MemAccs.begin();
1402 while (MALIt != MALEnd) {
1403 while (*MemAccsIt != *MALIt)
1404 MemAccsIt++;
1405
1406 MALIt++;
1407 MemAccs.erase(MemAccsIt);
1408 }
1409
1410 InstructionToAccess.erase(MA->getAccessInstruction());
1411 delete &MAL;
1412 }
1413
1414 // Get the context under which this statement, hence the memory accesses, are
1415 // executed.
1416 isl_set *DomainCtx = isl_set_params(getDomain());
1417 DomainCtx = isl_set_remove_redundancies(DomainCtx);
1418 DomainCtx = isl_set_detect_equalities(DomainCtx);
1419 DomainCtx = isl_set_coalesce(DomainCtx);
1420
Johannes Doerfertf7e29672015-10-08 11:05:57 +00001421 Scop &S = *getParent();
1422 ScalarEvolution &SE = *S.getSE();
1423
1424 // Project out all parameters that relate to loads in this statement that
1425 // we will hoist. Otherwise we would have cyclic dependences on the
1426 // constraints under which the hoisted loads are executed and we could not
1427 // determine an order in which to preload them. This happens because not only
1428 // lower bounds are part of the domain but also upper bounds.
1429 for (MemoryAccess *MA : InvMAs) {
1430 Instruction *AccInst = MA->getAccessInstruction();
1431 if (SE.isSCEVable(AccInst->getType())) {
1432 isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst));
1433 if (ParamId) {
1434 int Dim = isl_set_find_dim_by_id(DomainCtx, isl_dim_param, ParamId);
1435 DomainCtx = isl_set_eliminate(DomainCtx, isl_dim_param, Dim, 1);
1436 }
1437 isl_id_free(ParamId);
1438 }
1439 }
1440
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001441 for (MemoryAccess *MA : InvMAs) {
1442
1443 // Check for another invariant access that accesses the same location as
1444 // MA and if found consolidate them. Otherwise create a new equivalence
1445 // class at the end of InvariantEquivClasses.
1446 LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction());
1447 const SCEV *PointerSCEV = SE.getSCEV(LInst->getPointerOperand());
1448 bool Consolidated = false;
1449
1450 for (auto &IAClass : InvariantEquivClasses) {
1451 const SCEV *ClassPointerSCEV = IAClass.first;
1452 if (PointerSCEV != ClassPointerSCEV)
1453 continue;
1454
1455 Consolidated = true;
1456
1457 // We created empty equivalence classes for required invariant loads
1458 // in the beginning and might encounter one of them here. If so, this
1459 // MA will be the first in that equivalence class.
1460 auto &ClassList = IAClass.second;
1461 if (ClassList.empty()) {
1462 ClassList.push_front(std::make_pair(MA, isl_set_copy(DomainCtx)));
1463 break;
1464 }
1465
1466 // If the equivalence class for MA is not empty we unify the execution
1467 // context and add MA to the list of accesses that are in this class.
1468 isl_set *IAClassDomainCtx = IAClass.second.front().second;
1469 IAClassDomainCtx =
1470 isl_set_union(IAClassDomainCtx, isl_set_copy(DomainCtx));
1471 ClassList.push_front(std::make_pair(MA, IAClassDomainCtx));
1472 break;
1473 }
1474
1475 if (Consolidated)
1476 continue;
1477
1478 // If we did not consolidate MA, thus did not find an equivalence class
1479 // that for it, we create a new one.
1480 InvariantAccessTy IA = std::make_pair(MA, isl_set_copy(DomainCtx));
1481 InvariantEquivClasses.emplace_back(InvariantEquivClassTy(
1482 std::make_pair(PointerSCEV, InvariantAccessListTy({IA}))));
1483 }
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00001484
1485 isl_set_free(DomainCtx);
1486}
1487
Tobias Grosser75805372011-04-29 06:27:02 +00001488//===----------------------------------------------------------------------===//
1489/// Scop class implement
Tobias Grosser60b54f12011-11-08 15:41:28 +00001490
Tobias Grosser7ffe4e82011-11-17 12:56:10 +00001491void Scop::setContext(__isl_take isl_set *NewContext) {
Tobias Grosserff9b54d2011-11-15 11:38:44 +00001492 NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context));
1493 isl_set_free(Context);
1494 Context = NewContext;
1495}
1496
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001497const SCEV *Scop::getRepresentingInvariantLoadSCEV(const SCEV *S) const {
1498 const SCEVUnknown *SU = dyn_cast_or_null<SCEVUnknown>(S);
1499 if (!SU)
1500 return S;
1501
1502 LoadInst *LInst = dyn_cast<LoadInst>(SU->getValue());
1503 if (!LInst)
1504 return S;
1505
1506 // Try to find an equivalence class for the load, if found return
1507 // the SCEV for the representing element, otherwise return S.
1508 const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand());
1509 for (const InvariantEquivClassTy &IAClass : InvariantEquivClasses) {
1510 const SCEV *ClassPointerSCEV = IAClass.first;
1511 if (ClassPointerSCEV == PointerSCEV)
1512 return ClassPointerSCEV;
1513 }
1514
1515 return S;
1516}
1517
Tobias Grosserabfbe632013-02-05 12:09:06 +00001518void Scop::addParams(std::vector<const SCEV *> NewParameters) {
Tobias Grosser083d3d32014-06-28 08:59:45 +00001519 for (const SCEV *Parameter : NewParameters) {
Johannes Doerfertbe409962015-03-29 20:45:09 +00001520 Parameter = extractConstantFactor(Parameter, *SE).second;
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001521
1522 // Normalize the SCEV to get the representing element for an invariant load.
1523 Parameter = getRepresentingInvariantLoadSCEV(Parameter);
1524
Tobias Grosser60b54f12011-11-08 15:41:28 +00001525 if (ParameterIds.find(Parameter) != ParameterIds.end())
1526 continue;
1527
1528 int dimension = Parameters.size();
1529
1530 Parameters.push_back(Parameter);
1531 ParameterIds[Parameter] = dimension;
1532 }
1533}
1534
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001535__isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const {
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001536 // Normalize the SCEV to get the representing element for an invariant load.
1537 Parameter = getRepresentingInvariantLoadSCEV(Parameter);
1538
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001539 ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter);
Tobias Grosser76c2e322011-11-07 12:58:59 +00001540
Tobias Grosser9a38ab82011-11-08 15:41:03 +00001541 if (IdIter == ParameterIds.end())
Tobias Grosser5a56cbf2014-04-16 07:33:47 +00001542 return nullptr;
Tobias Grosser76c2e322011-11-07 12:58:59 +00001543
Tobias Grosser8f99c162011-11-15 11:38:55 +00001544 std::string ParameterName;
1545
1546 if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) {
1547 Value *Val = ValueParameter->getValue();
Tobias Grosser29ee0b12011-11-17 14:52:36 +00001548 ParameterName = Val->getName();
Tobias Grosser8f99c162011-11-15 11:38:55 +00001549 }
1550
1551 if (ParameterName == "" || ParameterName.substr(0, 2) == "p_")
Hongbin Zheng86a37742012-04-25 08:01:38 +00001552 ParameterName = "p_" + utostr_32(IdIter->second);
Tobias Grosser8f99c162011-11-15 11:38:55 +00001553
Tobias Grosser20532b82014-04-11 17:56:49 +00001554 return isl_id_alloc(getIslCtx(), ParameterName.c_str(),
1555 const_cast<void *>((const void *)Parameter));
Tobias Grosser76c2e322011-11-07 12:58:59 +00001556}
Tobias Grosser75805372011-04-29 06:27:02 +00001557
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00001558isl_set *Scop::addNonEmptyDomainConstraints(isl_set *C) const {
1559 isl_set *DomainContext = isl_union_set_params(getDomains());
1560 return isl_set_intersect_params(C, DomainContext);
1561}
1562
Johannes Doerfert883f8c12015-09-15 22:52:53 +00001563void Scop::buildBoundaryContext() {
1564 BoundaryContext = Affinator.getWrappingContext();
1565 BoundaryContext = isl_set_complement(BoundaryContext);
1566 BoundaryContext = isl_set_gist_params(BoundaryContext, getContext());
1567}
1568
Tobias Grosser8a9c2352015-08-16 10:19:29 +00001569void Scop::addUserContext() {
1570 if (UserContextStr.empty())
1571 return;
1572
1573 isl_set *UserContext = isl_set_read_from_str(IslCtx, UserContextStr.c_str());
1574 isl_space *Space = getParamSpace();
1575 if (isl_space_dim(Space, isl_dim_param) !=
1576 isl_set_dim(UserContext, isl_dim_param)) {
1577 auto SpaceStr = isl_space_to_str(Space);
1578 errs() << "Error: the context provided in -polly-context has not the same "
1579 << "number of dimensions than the computed context. Due to this "
1580 << "mismatch, the -polly-context option is ignored. Please provide "
1581 << "the context in the parameter space: " << SpaceStr << ".\n";
1582 free(SpaceStr);
1583 isl_set_free(UserContext);
1584 isl_space_free(Space);
1585 return;
1586 }
1587
1588 for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
1589 auto NameContext = isl_set_get_dim_name(Context, isl_dim_param, i);
1590 auto NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i);
1591
1592 if (strcmp(NameContext, NameUserContext) != 0) {
1593 auto SpaceStr = isl_space_to_str(Space);
1594 errs() << "Error: the name of dimension " << i
1595 << " provided in -polly-context "
1596 << "is '" << NameUserContext << "', but the name in the computed "
1597 << "context is '" << NameContext
1598 << "'. Due to this name mismatch, "
1599 << "the -polly-context option is ignored. Please provide "
1600 << "the context in the parameter space: " << SpaceStr << ".\n";
1601 free(SpaceStr);
1602 isl_set_free(UserContext);
1603 isl_space_free(Space);
1604 return;
1605 }
1606
1607 UserContext =
1608 isl_set_set_dim_id(UserContext, isl_dim_param, i,
1609 isl_space_get_dim_id(Space, isl_dim_param, i));
1610 }
1611
1612 Context = isl_set_intersect(Context, UserContext);
1613 isl_space_free(Space);
1614}
1615
Johannes Doerfert697fdf82015-10-09 17:12:26 +00001616void Scop::buildInvariantEquivalenceClasses() {
1617 const InvariantLoadsSetTy &RIL = *SD.getRequiredInvariantLoads(&getRegion());
1618 SmallPtrSet<const SCEV *, 4> ClassPointerSet;
1619 for (LoadInst *LInst : RIL) {
1620 const SCEV *PointerSCEV = SE->getSCEV(LInst->getPointerOperand());
1621
1622 // Skip the load if we already have a equivalence class for the pointer.
1623 if (!ClassPointerSet.insert(PointerSCEV).second)
1624 continue;
1625
1626 InvariantEquivClasses.emplace_back(InvariantEquivClassTy(
1627 std::make_pair(PointerSCEV, InvariantAccessListTy())));
1628 }
1629}
1630
Tobias Grosser6be480c2011-11-08 15:41:13 +00001631void Scop::buildContext() {
1632 isl_space *Space = isl_space_params_alloc(IslCtx, 0);
Tobias Grossere86109f2013-10-29 21:05:49 +00001633 Context = isl_set_universe(isl_space_copy(Space));
1634 AssumedContext = isl_set_universe(Space);
Tobias Grosser0e27e242011-10-06 00:03:48 +00001635}
1636
Tobias Grosser18daaca2012-05-22 10:47:27 +00001637void Scop::addParameterBounds() {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001638 for (const auto &ParamID : ParameterIds) {
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001639 int dim = ParamID.second;
Tobias Grosser18daaca2012-05-22 10:47:27 +00001640
Johannes Doerfert4f8ac3d2015-02-23 16:15:51 +00001641 ConstantRange SRange = SE->getSignedRange(ParamID.first);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001642
Johannes Doerferte7044942015-02-24 11:58:30 +00001643 Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param);
Tobias Grosser18daaca2012-05-22 10:47:27 +00001644 }
1645}
1646
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001647void Scop::realignParams() {
Tobias Grosser6be480c2011-11-08 15:41:13 +00001648 // Add all parameters into a common model.
Tobias Grosser60b54f12011-11-08 15:41:28 +00001649 isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size());
Tobias Grosser6be480c2011-11-08 15:41:13 +00001650
Tobias Grosser083d3d32014-06-28 08:59:45 +00001651 for (const auto &ParamID : ParameterIds) {
1652 const SCEV *Parameter = ParamID.first;
Tobias Grosser6be480c2011-11-08 15:41:13 +00001653 isl_id *id = getIdForParam(Parameter);
Tobias Grosser083d3d32014-06-28 08:59:45 +00001654 Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id);
Tobias Grosser6be480c2011-11-08 15:41:13 +00001655 }
1656
1657 // Align the parameters of all data structures to the model.
1658 Context = isl_set_align_params(Context, Space);
1659
Tobias Grosser7c3bad52015-05-27 05:16:57 +00001660 for (ScopStmt &Stmt : *this)
1661 Stmt.realignParams();
Tobias Grosser8cae72f2011-11-08 15:41:08 +00001662}
1663
Johannes Doerfert883f8c12015-09-15 22:52:53 +00001664static __isl_give isl_set *
1665simplifyAssumptionContext(__isl_take isl_set *AssumptionContext,
1666 const Scop &S) {
1667 isl_set *DomainParameters = isl_union_set_params(S.getDomains());
1668 AssumptionContext = isl_set_gist_params(AssumptionContext, DomainParameters);
1669 AssumptionContext = isl_set_gist_params(AssumptionContext, S.getContext());
1670 return AssumptionContext;
1671}
1672
1673void Scop::simplifyContexts() {
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001674 // The parameter constraints of the iteration domains give us a set of
1675 // constraints that need to hold for all cases where at least a single
1676 // statement iteration is executed in the whole scop. We now simplify the
1677 // assumed context under the assumption that such constraints hold and at
1678 // least a single statement iteration is executed. For cases where no
1679 // statement instances are executed, the assumptions we have taken about
1680 // the executed code do not matter and can be changed.
1681 //
1682 // WARNING: This only holds if the assumptions we have taken do not reduce
1683 // the set of statement instances that are executed. Otherwise we
1684 // may run into a case where the iteration domains suggest that
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001685 // for a certain set of parameter constraints no code is executed,
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001686 // but in the original program some computation would have been
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001687 // performed. In such a case, modifying the run-time conditions and
1688 // possibly influencing the run-time check may cause certain scops
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001689 // to not be executed.
1690 //
1691 // Example:
1692 //
1693 // When delinearizing the following code:
1694 //
1695 // for (long i = 0; i < 100; i++)
1696 // for (long j = 0; j < m; j++)
1697 // A[i+p][j] = 1.0;
1698 //
1699 // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00001700 // otherwise we would access out of bound data. Now, knowing that code is
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001701 // only executed for the case m >= 0, it is sufficient to assume p >= 0.
Johannes Doerfert883f8c12015-09-15 22:52:53 +00001702 AssumedContext = simplifyAssumptionContext(AssumedContext, *this);
1703 BoundaryContext = simplifyAssumptionContext(BoundaryContext, *this);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00001704}
1705
Johannes Doerfertb164c792014-09-18 11:17:17 +00001706/// @brief Add the minimal/maximal access in @p Set to @p User.
Tobias Grosserb2f39922015-05-28 13:32:11 +00001707static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00001708 Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User;
1709 isl_pw_multi_aff *MinPMA, *MaxPMA;
1710 isl_pw_aff *LastDimAff;
1711 isl_aff *OneAff;
1712 unsigned Pos;
1713
Johannes Doerfert9143d672014-09-27 11:02:39 +00001714 // Restrict the number of parameters involved in the access as the lexmin/
1715 // lexmax computation will take too long if this number is high.
1716 //
1717 // Experiments with a simple test case using an i7 4800MQ:
1718 //
1719 // #Parameters involved | Time (in sec)
1720 // 6 | 0.01
1721 // 7 | 0.04
1722 // 8 | 0.12
1723 // 9 | 0.40
1724 // 10 | 1.54
1725 // 11 | 6.78
1726 // 12 | 30.38
1727 //
1728 if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) {
1729 unsigned InvolvedParams = 0;
1730 for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++)
1731 if (isl_set_involves_dims(Set, isl_dim_param, u, 1))
1732 InvolvedParams++;
1733
1734 if (InvolvedParams > RunTimeChecksMaxParameters) {
1735 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001736 return isl_stat_error;
Johannes Doerfert9143d672014-09-27 11:02:39 +00001737 }
1738 }
1739
Johannes Doerfertb6755bb2015-02-14 12:00:06 +00001740 Set = isl_set_remove_divs(Set);
1741
Johannes Doerfertb164c792014-09-18 11:17:17 +00001742 MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set));
1743 MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set));
1744
Johannes Doerfert219b20e2014-10-07 14:37:59 +00001745 MinPMA = isl_pw_multi_aff_coalesce(MinPMA);
1746 MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA);
1747
Johannes Doerfertb164c792014-09-18 11:17:17 +00001748 // Adjust the last dimension of the maximal access by one as we want to
1749 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
1750 // we test during code generation might now point after the end of the
1751 // allocated array but we will never dereference it anyway.
1752 assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) &&
1753 "Assumed at least one output dimension");
1754 Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1;
1755 LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos);
1756 OneAff = isl_aff_zero_on_domain(
1757 isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff)));
1758 OneAff = isl_aff_add_constant_si(OneAff, 1);
1759 LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff));
1760 MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff);
1761
1762 MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA));
1763
1764 isl_set_free(Set);
Tobias Grosserb2f39922015-05-28 13:32:11 +00001765 return isl_stat_ok;
Johannes Doerfertb164c792014-09-18 11:17:17 +00001766}
1767
Johannes Doerferteeab05a2014-10-01 12:42:37 +00001768static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) {
1769 isl_set *Domain = MA->getStatement()->getDomain();
1770 Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain));
1771 return isl_set_reset_tuple_id(Domain);
1772}
1773
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001774/// @brief Wrapper function to calculate minimal/maximal accesses to each array.
1775static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses,
Tobias Grosserbb853c22015-07-25 12:31:03 +00001776 __isl_take isl_union_set *Domains,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001777 Scop::MinMaxVectorTy &MinMaxAccesses) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001778
1779 Accesses = isl_union_map_intersect_domain(Accesses, Domains);
1780 isl_union_set *Locations = isl_union_map_range(Accesses);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001781 Locations = isl_union_set_coalesce(Locations);
1782 Locations = isl_union_set_detect_equalities(Locations);
1783 bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess,
Johannes Doerfert210b09a2015-07-26 13:14:38 +00001784 &MinMaxAccesses));
Johannes Doerfert338b42c2015-07-23 17:04:54 +00001785 isl_union_set_free(Locations);
1786 return Valid;
1787}
1788
Johannes Doerfert96425c22015-08-30 21:13:53 +00001789/// @brief Helper to treat non-affine regions and basic blocks the same.
1790///
1791///{
1792
1793/// @brief Return the block that is the representing block for @p RN.
1794static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) {
1795 return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry()
1796 : RN->getNodeAs<BasicBlock>();
1797}
1798
1799/// @brief Return the @p idx'th block that is executed after @p RN.
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001800static inline BasicBlock *
1801getRegionNodeSuccessor(RegionNode *RN, TerminatorInst *TI, unsigned idx) {
Johannes Doerfert96425c22015-08-30 21:13:53 +00001802 if (RN->isSubRegion()) {
1803 assert(idx == 0);
1804 return RN->getNodeAs<Region>()->getExit();
1805 }
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001806 return TI->getSuccessor(idx);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001807}
1808
1809/// @brief Return the smallest loop surrounding @p RN.
1810static inline Loop *getRegionNodeLoop(RegionNode *RN, LoopInfo &LI) {
1811 if (!RN->isSubRegion())
1812 return LI.getLoopFor(RN->getNodeAs<BasicBlock>());
1813
1814 Region *NonAffineSubRegion = RN->getNodeAs<Region>();
1815 Loop *L = LI.getLoopFor(NonAffineSubRegion->getEntry());
1816 while (L && NonAffineSubRegion->contains(L))
1817 L = L->getParentLoop();
1818 return L;
1819}
1820
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001821static inline unsigned getNumBlocksInRegionNode(RegionNode *RN) {
1822 if (!RN->isSubRegion())
1823 return 1;
1824
1825 unsigned NumBlocks = 0;
1826 Region *R = RN->getNodeAs<Region>();
1827 for (auto BB : R->blocks()) {
1828 (void)BB;
1829 NumBlocks++;
1830 }
1831 return NumBlocks;
1832}
1833
Johannes Doerfert08d90a32015-10-07 20:32:43 +00001834static bool containsErrorBlock(RegionNode *RN, const Region &R, LoopInfo &LI,
1835 const DominatorTree &DT) {
Johannes Doerfertf5673802015-10-01 23:48:18 +00001836 if (!RN->isSubRegion())
Johannes Doerfert08d90a32015-10-07 20:32:43 +00001837 return isErrorBlock(*RN->getNodeAs<BasicBlock>(), R, LI, DT);
Johannes Doerfertf5673802015-10-01 23:48:18 +00001838 for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks())
Johannes Doerfert08d90a32015-10-07 20:32:43 +00001839 if (isErrorBlock(*BB, R, LI, DT))
Johannes Doerfertf5673802015-10-01 23:48:18 +00001840 return true;
1841 return false;
1842}
1843
Johannes Doerfert96425c22015-08-30 21:13:53 +00001844///}
1845
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001846static inline __isl_give isl_set *addDomainDimId(__isl_take isl_set *Domain,
1847 unsigned Dim, Loop *L) {
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00001848 Domain = isl_set_lower_bound_si(Domain, isl_dim_set, Dim, -1);
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001849 isl_id *DimId =
1850 isl_id_alloc(isl_set_get_ctx(Domain), nullptr, static_cast<void *>(L));
1851 return isl_set_set_dim_id(Domain, isl_dim_set, Dim, DimId);
1852}
1853
Johannes Doerfert96425c22015-08-30 21:13:53 +00001854isl_set *Scop::getDomainConditions(ScopStmt *Stmt) {
1855 BasicBlock *BB = Stmt->isBlockStmt() ? Stmt->getBasicBlock()
1856 : Stmt->getRegion()->getEntry();
Johannes Doerfertcef616f2015-09-15 22:49:04 +00001857 return getDomainConditions(BB);
1858}
1859
1860isl_set *Scop::getDomainConditions(BasicBlock *BB) {
1861 assert(DomainMap.count(BB) && "Requested BB did not have a domain");
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001862 return isl_set_copy(DomainMap[BB]);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001863}
1864
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00001865void Scop::buildDomains(Region *R) {
Johannes Doerfert96425c22015-08-30 21:13:53 +00001866
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001867 auto *EntryBB = R->getEntry();
1868 int LD = getRelativeLoopDepth(LI.getLoopFor(EntryBB));
1869 auto *S = isl_set_universe(isl_space_set_alloc(getIslCtx(), 0, LD + 1));
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001870
1871 Loop *L = LI.getLoopFor(EntryBB);
1872 while (LD-- >= 0) {
1873 S = addDomainDimId(S, LD + 1, L);
1874 L = L->getParentLoop();
1875 }
1876
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001877 DomainMap[EntryBB] = S;
Johannes Doerfert96425c22015-08-30 21:13:53 +00001878
Johannes Doerfert40fa56f2015-09-14 11:15:07 +00001879 if (SD.isNonAffineSubRegion(R, R))
1880 return;
1881
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00001882 buildDomainsWithBranchConstraints(R);
1883 propagateDomainConstraints(R);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001884}
1885
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00001886void Scop::buildDomainsWithBranchConstraints(Region *R) {
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001887 RegionInfo &RI = *R->getRegionInfo();
Johannes Doerfert96425c22015-08-30 21:13:53 +00001888
1889 // To create the domain for each block in R we iterate over all blocks and
1890 // subregions in R and propagate the conditions under which the current region
1891 // element is executed. To this end we iterate in reverse post order over R as
1892 // it ensures that we first visit all predecessors of a region node (either a
1893 // basic block or a subregion) before we visit the region node itself.
1894 // Initially, only the domain for the SCoP region entry block is set and from
1895 // there we propagate the current domain to all successors, however we add the
1896 // condition that the successor is actually executed next.
1897 // As we are only interested in non-loop carried constraints here we can
1898 // simply skip loop back edges.
1899
1900 ReversePostOrderTraversal<Region *> RTraversal(R);
1901 for (auto *RN : RTraversal) {
1902
1903 // Recurse for affine subregions but go on for basic blocks and non-affine
1904 // subregions.
1905 if (RN->isSubRegion()) {
1906 Region *SubRegion = RN->getNodeAs<Region>();
1907 if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) {
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00001908 buildDomainsWithBranchConstraints(SubRegion);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001909 continue;
1910 }
1911 }
1912
Johannes Doerfertf5673802015-10-01 23:48:18 +00001913 // Error blocks are assumed not to be executed. Therefor they are not
1914 // checked properly in the ScopDetection. Any attempt to generate control
1915 // conditions from them might result in a crash. However, this is only true
1916 // for the first step of the domain generation (this function) where we
1917 // push the control conditions of a block to the successors. In the second
1918 // step (propagateDomainConstraints) we only receive domain constraints from
1919 // the predecessors and can therefor look at the domain of a error block.
1920 // That allows us to generate the assumptions needed for them not to be
1921 // executed at runtime.
Johannes Doerfert08d90a32015-10-07 20:32:43 +00001922 if (containsErrorBlock(RN, getRegion(), LI, DT))
Johannes Doerfertf5673802015-10-01 23:48:18 +00001923 continue;
1924
Johannes Doerfert96425c22015-08-30 21:13:53 +00001925 BasicBlock *BB = getRegionNodeBasicBlock(RN);
Johannes Doerfert90db75e2015-09-10 17:51:27 +00001926 TerminatorInst *TI = BB->getTerminator();
1927
Johannes Doerfertf5673802015-10-01 23:48:18 +00001928 isl_set *Domain = DomainMap.lookup(BB);
1929 if (!Domain) {
1930 DEBUG(dbgs() << "\tSkip: " << BB->getName()
1931 << ", it is only reachable from error blocks.\n");
Johannes Doerfert90db75e2015-09-10 17:51:27 +00001932 continue;
1933 }
1934
Johannes Doerfert96425c22015-08-30 21:13:53 +00001935 DEBUG(dbgs() << "\tVisit: " << BB->getName() << " : " << Domain << "\n");
Johannes Doerfert96425c22015-08-30 21:13:53 +00001936
1937 Loop *BBLoop = getRegionNodeLoop(RN, LI);
1938 int BBLoopDepth = getRelativeLoopDepth(BBLoop);
1939
1940 // Build the condition sets for the successor nodes of the current region
1941 // node. If it is a non-affine subregion we will always execute the single
1942 // exit node, hence the single entry node domain is the condition set. For
1943 // basic blocks we use the helper function buildConditionSets.
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001944 SmallVector<isl_set *, 8> ConditionSets;
Johannes Doerfert96425c22015-08-30 21:13:53 +00001945 if (RN->isSubRegion())
1946 ConditionSets.push_back(isl_set_copy(Domain));
1947 else
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001948 buildConditionSets(*this, TI, BBLoop, Domain, ConditionSets);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001949
1950 // Now iterate over the successors and set their initial domain based on
1951 // their condition set. We skip back edges here and have to be careful when
1952 // we leave a loop not to keep constraints over a dimension that doesn't
1953 // exist anymore.
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001954 assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size());
Johannes Doerfert96425c22015-08-30 21:13:53 +00001955 for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) {
Johannes Doerfert96425c22015-08-30 21:13:53 +00001956 isl_set *CondSet = ConditionSets[u];
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001957 BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u);
Johannes Doerfert96425c22015-08-30 21:13:53 +00001958
1959 // Skip back edges.
1960 if (DT.dominates(SuccBB, BB)) {
1961 isl_set_free(CondSet);
1962 continue;
1963 }
1964
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001965 // Do not adjust the number of dimensions if we enter a boxed loop or are
1966 // in a non-affine subregion or if the surrounding loop stays the same.
Johannes Doerfert96425c22015-08-30 21:13:53 +00001967 Loop *SuccBBLoop = LI.getLoopFor(SuccBB);
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001968 Region *SuccRegion = RI.getRegionFor(SuccBB);
Johannes Doerfert634909c2015-10-04 14:57:41 +00001969 if (SD.isNonAffineSubRegion(SuccRegion, &getRegion()))
1970 while (SuccBBLoop && SuccRegion->contains(SuccBBLoop))
1971 SuccBBLoop = SuccBBLoop->getParentLoop();
1972
1973 if (BBLoop != SuccBBLoop) {
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001974
1975 // Check if the edge to SuccBB is a loop entry or exit edge. If so
1976 // adjust the dimensionality accordingly. Lastly, if we leave a loop
1977 // and enter a new one we need to drop the old constraints.
1978 int SuccBBLoopDepth = getRelativeLoopDepth(SuccBBLoop);
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00001979 unsigned LoopDepthDiff = std::abs(BBLoopDepth - SuccBBLoopDepth);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001980 if (BBLoopDepth > SuccBBLoopDepth) {
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00001981 CondSet = isl_set_project_out(CondSet, isl_dim_set,
1982 isl_set_n_dim(CondSet) - LoopDepthDiff,
1983 LoopDepthDiff);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001984 } else if (SuccBBLoopDepth > BBLoopDepth) {
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00001985 assert(LoopDepthDiff == 1);
Johannes Doerfertf08bd002015-08-31 13:56:32 +00001986 CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1);
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001987 CondSet = addDomainDimId(CondSet, SuccBBLoopDepth, SuccBBLoop);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001988 } else if (BBLoopDepth >= 0) {
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00001989 assert(LoopDepthDiff <= 1);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001990 CondSet = isl_set_project_out(CondSet, isl_dim_set, BBLoopDepth, 1);
1991 CondSet = isl_set_add_dims(CondSet, isl_dim_set, 1);
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00001992 CondSet = addDomainDimId(CondSet, SuccBBLoopDepth, SuccBBLoop);
Tobias Grosser2df884f2015-09-01 18:17:41 +00001993 }
Johannes Doerfert96425c22015-08-30 21:13:53 +00001994 }
1995
1996 // Set the domain for the successor or merge it with an existing domain in
1997 // case there are multiple paths (without loop back edges) to the
1998 // successor block.
1999 isl_set *&SuccDomain = DomainMap[SuccBB];
2000 if (!SuccDomain)
2001 SuccDomain = CondSet;
2002 else
2003 SuccDomain = isl_set_union(SuccDomain, CondSet);
2004
2005 SuccDomain = isl_set_coalesce(SuccDomain);
Johannes Doerfert634909c2015-10-04 14:57:41 +00002006 DEBUG(dbgs() << "\tSet SuccBB: " << SuccBB->getName() << " : "
2007 << SuccDomain << "\n");
Johannes Doerfert96425c22015-08-30 21:13:53 +00002008 }
2009 }
2010}
2011
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002012/// @brief Return the domain for @p BB wrt @p DomainMap.
2013///
2014/// This helper function will lookup @p BB in @p DomainMap but also handle the
2015/// case where @p BB is contained in a non-affine subregion using the region
2016/// tree obtained by @p RI.
2017static __isl_give isl_set *
2018getDomainForBlock(BasicBlock *BB, DenseMap<BasicBlock *, isl_set *> &DomainMap,
2019 RegionInfo &RI) {
2020 auto DIt = DomainMap.find(BB);
2021 if (DIt != DomainMap.end())
2022 return isl_set_copy(DIt->getSecond());
2023
2024 Region *R = RI.getRegionFor(BB);
2025 while (R->getEntry() == BB)
2026 R = R->getParent();
2027 return getDomainForBlock(R->getEntry(), DomainMap, RI);
2028}
2029
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002030void Scop::propagateDomainConstraints(Region *R) {
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002031 // Iterate over the region R and propagate the domain constrains from the
2032 // predecessors to the current node. In contrast to the
2033 // buildDomainsWithBranchConstraints function, this one will pull the domain
2034 // information from the predecessors instead of pushing it to the successors.
2035 // Additionally, we assume the domains to be already present in the domain
2036 // map here. However, we iterate again in reverse post order so we know all
2037 // predecessors have been visited before a block or non-affine subregion is
2038 // visited.
2039
2040 // The set of boxed loops (loops in non-affine subregions) for this SCoP.
2041 auto &BoxedLoops = *SD.getBoxedLoops(&getRegion());
2042
2043 ReversePostOrderTraversal<Region *> RTraversal(R);
2044 for (auto *RN : RTraversal) {
2045
2046 // Recurse for affine subregions but go on for basic blocks and non-affine
2047 // subregions.
2048 if (RN->isSubRegion()) {
2049 Region *SubRegion = RN->getNodeAs<Region>();
2050 if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) {
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002051 propagateDomainConstraints(SubRegion);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002052 continue;
2053 }
2054 }
2055
Johannes Doerfertf5673802015-10-01 23:48:18 +00002056 // Get the domain for the current block and check if it was initialized or
2057 // not. The only way it was not is if this block is only reachable via error
2058 // blocks, thus will not be executed under the assumptions we make. Such
2059 // blocks have to be skipped as their predecessors might not have domains
2060 // either. It would not benefit us to compute the domain anyway, only the
2061 // domains of the error blocks that are reachable from non-error blocks
2062 // are needed to generate assumptions.
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002063 BasicBlock *BB = getRegionNodeBasicBlock(RN);
Johannes Doerfertf5673802015-10-01 23:48:18 +00002064 isl_set *&Domain = DomainMap[BB];
2065 if (!Domain) {
2066 DEBUG(dbgs() << "\tSkip: " << BB->getName()
2067 << ", it is only reachable from error blocks.\n");
2068 DomainMap.erase(BB);
2069 continue;
2070 }
2071 DEBUG(dbgs() << "\tVisit: " << BB->getName() << " : " << Domain << "\n");
2072
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002073 Loop *BBLoop = getRegionNodeLoop(RN, LI);
2074 int BBLoopDepth = getRelativeLoopDepth(BBLoop);
2075
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002076 isl_set *PredDom = isl_set_empty(isl_set_get_space(Domain));
2077 for (auto *PredBB : predecessors(BB)) {
2078
2079 // Skip backedges
2080 if (DT.dominates(BB, PredBB))
2081 continue;
2082
2083 isl_set *PredBBDom = nullptr;
2084
2085 // Handle the SCoP entry block with its outside predecessors.
2086 if (!getRegion().contains(PredBB))
2087 PredBBDom = isl_set_universe(isl_set_get_space(PredDom));
2088
2089 if (!PredBBDom) {
2090 // Determine the loop depth of the predecessor and adjust its domain to
2091 // the domain of the current block. This can mean we have to:
2092 // o) Drop a dimension if this block is the exit of a loop, not the
2093 // header of a new loop and the predecessor was part of the loop.
2094 // o) Add an unconstrainted new dimension if this block is the header
2095 // of a loop and the predecessor is not part of it.
2096 // o) Drop the information about the innermost loop dimension when the
2097 // predecessor and the current block are surrounded by different
2098 // loops in the same depth.
2099 PredBBDom = getDomainForBlock(PredBB, DomainMap, *R->getRegionInfo());
2100 Loop *PredBBLoop = LI.getLoopFor(PredBB);
2101 while (BoxedLoops.count(PredBBLoop))
2102 PredBBLoop = PredBBLoop->getParentLoop();
2103
2104 int PredBBLoopDepth = getRelativeLoopDepth(PredBBLoop);
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00002105 unsigned LoopDepthDiff = std::abs(BBLoopDepth - PredBBLoopDepth);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002106 if (BBLoopDepth < PredBBLoopDepth)
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00002107 PredBBDom = isl_set_project_out(
2108 PredBBDom, isl_dim_set, isl_set_n_dim(PredBBDom) - LoopDepthDiff,
2109 LoopDepthDiff);
2110 else if (PredBBLoopDepth < BBLoopDepth) {
2111 assert(LoopDepthDiff == 1);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002112 PredBBDom = isl_set_add_dims(PredBBDom, isl_dim_set, 1);
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00002113 } else if (BBLoop != PredBBLoop && BBLoopDepth >= 0) {
2114 assert(LoopDepthDiff <= 1);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002115 PredBBDom = isl_set_drop_constraints_involving_dims(
2116 PredBBDom, isl_dim_set, BBLoopDepth, 1);
Johannes Doerfertf4fa9872015-09-10 15:53:59 +00002117 }
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002118 }
2119
2120 PredDom = isl_set_union(PredDom, PredBBDom);
2121 }
2122
2123 // Under the union of all predecessor conditions we can reach this block.
Johannes Doerfertb20f1512015-09-15 22:11:49 +00002124 Domain = isl_set_coalesce(isl_set_intersect(Domain, PredDom));
Johannes Doerfert90db75e2015-09-10 17:51:27 +00002125
Johannes Doerfertf32f5f22015-09-28 01:30:37 +00002126 if (BBLoop && BBLoop->getHeader() == BB && getRegion().contains(BBLoop))
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002127 addLoopBoundsToHeaderDomain(BBLoop);
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002128
Johannes Doerfert90db75e2015-09-10 17:51:27 +00002129 // Add assumptions for error blocks.
Johannes Doerfert08d90a32015-10-07 20:32:43 +00002130 if (containsErrorBlock(RN, getRegion(), LI, DT)) {
Johannes Doerfert90db75e2015-09-10 17:51:27 +00002131 IsOptimized = true;
2132 isl_set *DomPar = isl_set_params(isl_set_copy(Domain));
2133 addAssumption(isl_set_complement(DomPar));
2134 }
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002135 }
2136}
2137
2138/// @brief Create a map from SetSpace -> SetSpace where the dimensions @p Dim
2139/// is incremented by one and all other dimensions are equal, e.g.,
2140/// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3]
2141/// if @p Dim is 2 and @p SetSpace has 4 dimensions.
2142static __isl_give isl_map *
2143createNextIterationMap(__isl_take isl_space *SetSpace, unsigned Dim) {
2144 auto *MapSpace = isl_space_map_from_set(SetSpace);
2145 auto *NextIterationMap = isl_map_universe(isl_space_copy(MapSpace));
2146 for (unsigned u = 0; u < isl_map_n_in(NextIterationMap); u++)
2147 if (u != Dim)
2148 NextIterationMap =
2149 isl_map_equate(NextIterationMap, isl_dim_in, u, isl_dim_out, u);
2150 auto *C = isl_constraint_alloc_equality(isl_local_space_from_space(MapSpace));
2151 C = isl_constraint_set_constant_si(C, 1);
2152 C = isl_constraint_set_coefficient_si(C, isl_dim_in, Dim, 1);
2153 C = isl_constraint_set_coefficient_si(C, isl_dim_out, Dim, -1);
2154 NextIterationMap = isl_map_add_constraint(NextIterationMap, C);
2155 return NextIterationMap;
2156}
2157
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002158void Scop::addLoopBoundsToHeaderDomain(Loop *L) {
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002159 int LoopDepth = getRelativeLoopDepth(L);
2160 assert(LoopDepth >= 0 && "Loop in region should have at least depth one");
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002161
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002162 BasicBlock *HeaderBB = L->getHeader();
2163 assert(DomainMap.count(HeaderBB));
2164 isl_set *&HeaderBBDom = DomainMap[HeaderBB];
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002165
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002166 isl_map *NextIterationMap =
2167 createNextIterationMap(isl_set_get_space(HeaderBBDom), LoopDepth);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002168
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002169 isl_set *UnionBackedgeCondition =
2170 isl_set_empty(isl_set_get_space(HeaderBBDom));
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002171
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002172 SmallVector<llvm::BasicBlock *, 4> LatchBlocks;
2173 L->getLoopLatches(LatchBlocks);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002174
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002175 for (BasicBlock *LatchBB : LatchBlocks) {
Johannes Doerfertf5673802015-10-01 23:48:18 +00002176
2177 // If the latch is only reachable via error statements we skip it.
2178 isl_set *LatchBBDom = DomainMap.lookup(LatchBB);
2179 if (!LatchBBDom)
2180 continue;
2181
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002182 isl_set *BackedgeCondition = nullptr;
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002183
Johannes Doerfert9a132f32015-09-28 09:33:22 +00002184 TerminatorInst *TI = LatchBB->getTerminator();
2185 BranchInst *BI = dyn_cast<BranchInst>(TI);
2186 if (BI && BI->isUnconditional())
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002187 BackedgeCondition = isl_set_copy(LatchBBDom);
2188 else {
Johannes Doerfert9a132f32015-09-28 09:33:22 +00002189 SmallVector<isl_set *, 8> ConditionSets;
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002190 int idx = BI->getSuccessor(0) != HeaderBB;
Johannes Doerfert9a132f32015-09-28 09:33:22 +00002191 buildConditionSets(*this, TI, L, LatchBBDom, ConditionSets);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002192
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002193 // Free the non back edge condition set as we do not need it.
2194 isl_set_free(ConditionSets[1 - idx]);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002195
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002196 BackedgeCondition = ConditionSets[idx];
Johannes Doerfert06c57b52015-09-20 15:00:20 +00002197 }
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002198
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002199 int LatchLoopDepth = getRelativeLoopDepth(LI.getLoopFor(LatchBB));
2200 assert(LatchLoopDepth >= LoopDepth);
2201 BackedgeCondition =
2202 isl_set_project_out(BackedgeCondition, isl_dim_set, LoopDepth + 1,
2203 LatchLoopDepth - LoopDepth);
2204 UnionBackedgeCondition =
2205 isl_set_union(UnionBackedgeCondition, BackedgeCondition);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002206 }
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002207
2208 isl_map *ForwardMap = isl_map_lex_le(isl_set_get_space(HeaderBBDom));
2209 for (int i = 0; i < LoopDepth; i++)
2210 ForwardMap = isl_map_equate(ForwardMap, isl_dim_in, i, isl_dim_out, i);
2211
2212 isl_set *UnionBackedgeConditionComplement =
2213 isl_set_complement(UnionBackedgeCondition);
2214 UnionBackedgeConditionComplement = isl_set_lower_bound_si(
2215 UnionBackedgeConditionComplement, isl_dim_set, LoopDepth, 0);
2216 UnionBackedgeConditionComplement =
2217 isl_set_apply(UnionBackedgeConditionComplement, ForwardMap);
2218 HeaderBBDom = isl_set_subtract(HeaderBBDom, UnionBackedgeConditionComplement);
2219 HeaderBBDom = isl_set_apply(HeaderBBDom, NextIterationMap);
2220
2221 auto Parts = partitionSetParts(HeaderBBDom, LoopDepth);
2222 HeaderBBDom = Parts.second;
2223
Johannes Doerfert6a72a2a2015-09-20 16:59:23 +00002224 // Check if there is a <nsw> tagged AddRec for this loop and if so do not add
2225 // the bounded assumptions to the context as they are already implied by the
2226 // <nsw> tag.
2227 if (Affinator.hasNSWAddRecForLoop(L)) {
2228 isl_set_free(Parts.first);
2229 return;
2230 }
2231
Johannes Doerfertf2cc86e2015-09-20 16:15:32 +00002232 isl_set *UnboundedCtx = isl_set_params(Parts.first);
2233 isl_set *BoundedCtx = isl_set_complement(UnboundedCtx);
Johannes Doerfert707a4062015-09-20 16:38:19 +00002234 addAssumption(BoundedCtx);
Johannes Doerfert5b9ff8b2015-09-10 13:00:06 +00002235}
2236
Johannes Doerfert120de4b2015-08-20 18:30:08 +00002237void Scop::buildAliasChecks(AliasAnalysis &AA) {
2238 if (!PollyUseRuntimeAliasChecks)
2239 return;
2240
2241 if (buildAliasGroups(AA))
2242 return;
2243
2244 // If a problem occurs while building the alias groups we need to delete
2245 // this SCoP and pretend it wasn't valid in the first place. To this end
2246 // we make the assumed context infeasible.
2247 addAssumption(isl_set_empty(getParamSpace()));
2248
2249 DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr()
2250 << " could not be created as the number of parameters involved "
2251 "is too high. The SCoP will be "
2252 "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust "
2253 "the maximal number of parameters but be advised that the "
2254 "compile time might increase exponentially.\n\n");
2255}
2256
Johannes Doerfert9143d672014-09-27 11:02:39 +00002257bool Scop::buildAliasGroups(AliasAnalysis &AA) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00002258 // To create sound alias checks we perform the following steps:
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002259 // o) Use the alias analysis and an alias set tracker to build alias sets
Johannes Doerfertb164c792014-09-18 11:17:17 +00002260 // for all memory accesses inside the SCoP.
2261 // o) For each alias set we then map the aliasing pointers back to the
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002262 // memory accesses we know, thus obtain groups of memory accesses which
Johannes Doerfertb164c792014-09-18 11:17:17 +00002263 // might alias.
Johannes Doerferteeab05a2014-10-01 12:42:37 +00002264 // o) We divide each group based on the domains of the minimal/maximal
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002265 // accesses. That means two minimal/maximal accesses are only in a group
Johannes Doerferteeab05a2014-10-01 12:42:37 +00002266 // if their access domains intersect, otherwise they are in different
2267 // ones.
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002268 // o) We partition each group into read only and non read only accesses.
Johannes Doerfert6cad9c42015-02-24 16:00:29 +00002269 // o) For each group with more than one base pointer we then compute minimal
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002270 // and maximal accesses to each array of a group in read only and non
2271 // read only partitions separately.
Johannes Doerfertb164c792014-09-18 11:17:17 +00002272 using AliasGroupTy = SmallVector<MemoryAccess *, 4>;
2273
2274 AliasSetTracker AST(AA);
2275
2276 DenseMap<Value *, MemoryAccess *> PtrToAcc;
Johannes Doerfert13771732014-10-01 12:40:46 +00002277 DenseSet<Value *> HasWriteAccess;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002278 for (ScopStmt &Stmt : *this) {
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00002279
2280 // Skip statements with an empty domain as they will never be executed.
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002281 isl_set *StmtDomain = Stmt.getDomain();
Johannes Doerfertf1ee2622014-10-06 17:43:00 +00002282 bool StmtDomainEmpty = isl_set_is_empty(StmtDomain);
2283 isl_set_free(StmtDomain);
2284 if (StmtDomainEmpty)
2285 continue;
2286
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002287 for (MemoryAccess *MA : Stmt) {
Michael Kruse8d0b7342015-09-25 21:21:00 +00002288 if (MA->isImplicit())
Johannes Doerfertb164c792014-09-18 11:17:17 +00002289 continue;
Johannes Doerfert13771732014-10-01 12:40:46 +00002290 if (!MA->isRead())
2291 HasWriteAccess.insert(MA->getBaseAddr());
Johannes Doerfertb164c792014-09-18 11:17:17 +00002292 Instruction *Acc = MA->getAccessInstruction();
2293 PtrToAcc[getPointerOperand(*Acc)] = MA;
2294 AST.add(Acc);
2295 }
2296 }
2297
2298 SmallVector<AliasGroupTy, 4> AliasGroups;
2299 for (AliasSet &AS : AST) {
Johannes Doerfert74f68692014-10-08 02:23:48 +00002300 if (AS.isMustAlias() || AS.isForwardingAliasSet())
Johannes Doerfertb164c792014-09-18 11:17:17 +00002301 continue;
2302 AliasGroupTy AG;
2303 for (auto PR : AS)
2304 AG.push_back(PtrToAcc[PR.getValue()]);
2305 assert(AG.size() > 1 &&
2306 "Alias groups should contain at least two accesses");
2307 AliasGroups.push_back(std::move(AG));
2308 }
2309
Johannes Doerferteeab05a2014-10-01 12:42:37 +00002310 // Split the alias groups based on their domain.
2311 for (unsigned u = 0; u < AliasGroups.size(); u++) {
2312 AliasGroupTy NewAG;
2313 AliasGroupTy &AG = AliasGroups[u];
2314 AliasGroupTy::iterator AGI = AG.begin();
2315 isl_set *AGDomain = getAccessDomain(*AGI);
2316 while (AGI != AG.end()) {
2317 MemoryAccess *MA = *AGI;
2318 isl_set *MADomain = getAccessDomain(MA);
2319 if (isl_set_is_disjoint(AGDomain, MADomain)) {
2320 NewAG.push_back(MA);
2321 AGI = AG.erase(AGI);
2322 isl_set_free(MADomain);
2323 } else {
2324 AGDomain = isl_set_union(AGDomain, MADomain);
2325 AGI++;
2326 }
2327 }
2328 if (NewAG.size() > 1)
2329 AliasGroups.push_back(std::move(NewAG));
2330 isl_set_free(AGDomain);
2331 }
2332
Tobias Grosserf4c24b22015-04-05 13:11:54 +00002333 MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs;
Johannes Doerfert13771732014-10-01 12:40:46 +00002334 SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues;
2335 for (AliasGroupTy &AG : AliasGroups) {
2336 NonReadOnlyBaseValues.clear();
2337 ReadOnlyPairs.clear();
2338
Johannes Doerferteeab05a2014-10-01 12:42:37 +00002339 if (AG.size() < 2) {
2340 AG.clear();
2341 continue;
2342 }
2343
Johannes Doerfert13771732014-10-01 12:40:46 +00002344 for (auto II = AG.begin(); II != AG.end();) {
2345 Value *BaseAddr = (*II)->getBaseAddr();
2346 if (HasWriteAccess.count(BaseAddr)) {
2347 NonReadOnlyBaseValues.insert(BaseAddr);
2348 II++;
2349 } else {
2350 ReadOnlyPairs[BaseAddr].insert(*II);
2351 II = AG.erase(II);
2352 }
2353 }
2354
2355 // If we don't have read only pointers check if there are at least two
2356 // non read only pointers, otherwise clear the alias group.
Tobias Grosserbb853c22015-07-25 12:31:03 +00002357 if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002358 AG.clear();
Johannes Doerfert13771732014-10-01 12:40:46 +00002359 continue;
2360 }
2361
2362 // If we don't have non read only pointers clear the alias group.
2363 if (NonReadOnlyBaseValues.empty()) {
2364 AG.clear();
2365 continue;
2366 }
2367
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002368 // Calculate minimal and maximal accesses for non read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002369 MinMaxAliasGroups.emplace_back();
2370 MinMaxVectorPairTy &pair = MinMaxAliasGroups.back();
2371 MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first;
2372 MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second;
2373 MinMaxAccessesNonReadOnly.reserve(AG.size());
Johannes Doerfertb164c792014-09-18 11:17:17 +00002374
2375 isl_union_map *Accesses = isl_union_map_empty(getParamSpace());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002376
2377 // AG contains only non read only accesses.
Johannes Doerfertb164c792014-09-18 11:17:17 +00002378 for (MemoryAccess *MA : AG)
2379 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
Johannes Doerfertb164c792014-09-18 11:17:17 +00002380
Tobias Grosserdaaed0e2015-08-20 21:29:26 +00002381 bool Valid = calculateMinMaxAccess(Accesses, getDomains(),
2382 MinMaxAccessesNonReadOnly);
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002383
2384 // Bail out if the number of values we need to compare is too large.
2385 // This is important as the number of comparisions grows quadratically with
2386 // the number of values we need to compare.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002387 if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() >
2388 RunTimeChecksMaxArraysPerGroup))
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002389 return false;
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002390
2391 // Calculate minimal and maximal accesses for read only accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002392 MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size());
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002393 Accesses = isl_union_map_empty(getParamSpace());
2394
2395 for (const auto &ReadOnlyPair : ReadOnlyPairs)
2396 for (MemoryAccess *MA : ReadOnlyPair.second)
2397 Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation());
2398
Tobias Grosserdaaed0e2015-08-20 21:29:26 +00002399 Valid =
2400 calculateMinMaxAccess(Accesses, getDomains(), MinMaxAccessesReadOnly);
Johannes Doerfert9143d672014-09-27 11:02:39 +00002401
2402 if (!Valid)
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00002403 return false;
Johannes Doerfertb164c792014-09-18 11:17:17 +00002404 }
Johannes Doerfert9143d672014-09-27 11:02:39 +00002405
Tobias Grosser50d4e2e2015-03-28 14:50:32 +00002406 return true;
Johannes Doerfertb164c792014-09-18 11:17:17 +00002407}
2408
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00002409static Loop *getLoopSurroundingRegion(Region &R, LoopInfo &LI) {
2410 Loop *L = LI.getLoopFor(R.getEntry());
2411 return L ? (R.contains(L) ? L->getParentLoop() : L) : nullptr;
2412}
2413
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00002414static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI,
2415 ScopDetection &SD) {
2416
2417 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R);
2418
Johannes Doerferte3da05a2014-11-01 00:12:13 +00002419 unsigned MinLD = INT_MAX, MaxLD = 0;
2420 for (BasicBlock *BB : R.blocks()) {
2421 if (Loop *L = LI.getLoopFor(BB)) {
David Peixottodc0a11c2015-01-13 18:31:55 +00002422 if (!R.contains(L))
2423 continue;
Johannes Doerfertf8206cf2015-04-12 22:58:40 +00002424 if (BoxedLoops && BoxedLoops->count(L))
2425 continue;
Johannes Doerferte3da05a2014-11-01 00:12:13 +00002426 unsigned LD = L->getLoopDepth();
2427 MinLD = std::min(MinLD, LD);
2428 MaxLD = std::max(MaxLD, LD);
2429 }
2430 }
2431
2432 // Handle the case that there is no loop in the SCoP first.
2433 if (MaxLD == 0)
2434 return 1;
2435
2436 assert(MinLD >= 1 && "Minimal loop depth should be at least one");
2437 assert(MaxLD >= MinLD &&
2438 "Maximal loop depth was smaller than mininaml loop depth?");
2439 return MaxLD - MinLD + 1;
2440}
2441
Johannes Doerfert478a7de2015-10-02 13:09:31 +00002442Scop::Scop(Region &R, AccFuncMapType &AccFuncMap, ScopDetection &SD,
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002443 ScalarEvolution &ScalarEvolution, DominatorTree &DT, LoopInfo &LI,
Johannes Doerfert96425c22015-08-30 21:13:53 +00002444 isl_ctx *Context, unsigned MaxLoopDepth)
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002445 : LI(LI), DT(DT), SE(&ScalarEvolution), SD(SD), R(R),
2446 AccFuncMap(AccFuncMap), IsOptimized(false),
2447 HasSingleExitEdge(R.getExitingBlock()), MaxLoopDepth(MaxLoopDepth),
2448 IslCtx(Context), Context(nullptr), Affinator(this),
2449 AssumedContext(nullptr), BoundaryContext(nullptr), Schedule(nullptr) {}
Johannes Doerfertff9d1982015-02-24 12:00:50 +00002450
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002451void Scop::init(AliasAnalysis &AA) {
Tobias Grosser6be480c2011-11-08 15:41:13 +00002452 buildContext();
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002453 buildInvariantEquivalenceClasses();
2454
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002455 buildDomains(&R);
Johannes Doerfert96425c22015-08-30 21:13:53 +00002456
Michael Krusecac948e2015-10-02 13:53:07 +00002457 // Remove empty and ignored statements.
Michael Kruseafe06702015-10-02 16:33:27 +00002458 // Exit early in case there are no executable statements left in this scop.
Michael Krusecac948e2015-10-02 13:53:07 +00002459 simplifySCoP(true);
Michael Kruseafe06702015-10-02 16:33:27 +00002460 if (Stmts.empty())
2461 return;
Tobias Grosser75805372011-04-29 06:27:02 +00002462
Michael Krusecac948e2015-10-02 13:53:07 +00002463 // The ScopStmts now have enough information to initialize themselves.
2464 for (ScopStmt &Stmt : Stmts)
2465 Stmt.init();
2466
2467 DenseMap<Loop *, std::pair<isl_schedule *, unsigned>> LoopSchedules;
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00002468 Loop *L = getLoopSurroundingRegion(R, LI);
2469 LoopSchedules[L];
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00002470 buildSchedule(&R, LoopSchedules);
Tobias Grosser99c70dd2015-09-26 08:55:54 +00002471 updateAccessDimensionality();
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00002472 Schedule = LoopSchedules[L].first;
Tobias Grosser75805372011-04-29 06:27:02 +00002473
Tobias Grosser8cae72f2011-11-08 15:41:08 +00002474 realignParams();
Tobias Grosser18daaca2012-05-22 10:47:27 +00002475 addParameterBounds();
Tobias Grosser8a9c2352015-08-16 10:19:29 +00002476 addUserContext();
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002477 buildBoundaryContext();
2478 simplifyContexts();
Johannes Doerfert120de4b2015-08-20 18:30:08 +00002479 buildAliasChecks(AA);
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002480
2481 hoistInvariantLoads();
Michael Krusecac948e2015-10-02 13:53:07 +00002482 simplifySCoP(false);
Tobias Grosser75805372011-04-29 06:27:02 +00002483}
2484
2485Scop::~Scop() {
2486 isl_set_free(Context);
Tobias Grossere86109f2013-10-29 21:05:49 +00002487 isl_set_free(AssumedContext);
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002488 isl_set_free(BoundaryContext);
Tobias Grosser808cd692015-07-14 09:33:13 +00002489 isl_schedule_free(Schedule);
Tobias Grosser75805372011-04-29 06:27:02 +00002490
Johannes Doerfert96425c22015-08-30 21:13:53 +00002491 for (auto It : DomainMap)
2492 isl_set_free(It.second);
2493
Johannes Doerfertb164c792014-09-18 11:17:17 +00002494 // Free the alias groups
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002495 for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002496 for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) {
Johannes Doerfertb164c792014-09-18 11:17:17 +00002497 isl_pw_multi_aff_free(MMA.first);
2498 isl_pw_multi_aff_free(MMA.second);
2499 }
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002500 for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002501 isl_pw_multi_aff_free(MMA.first);
2502 isl_pw_multi_aff_free(MMA.second);
2503 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002504 }
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002505
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002506 for (const auto &IAClass : InvariantEquivClasses)
2507 if (!IAClass.second.empty())
2508 isl_set_free(IAClass.second.front().second);
Tobias Grosser75805372011-04-29 06:27:02 +00002509}
2510
Tobias Grosser99c70dd2015-09-26 08:55:54 +00002511void Scop::updateAccessDimensionality() {
2512 for (auto &Stmt : *this)
2513 for (auto &Access : Stmt)
2514 Access->updateDimensionality();
2515}
2516
Michael Krusecac948e2015-10-02 13:53:07 +00002517void Scop::simplifySCoP(bool RemoveIgnoredStmts) {
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002518 for (auto StmtIt = Stmts.begin(), StmtEnd = Stmts.end(); StmtIt != StmtEnd;) {
2519 ScopStmt &Stmt = *StmtIt;
Michael Krusecac948e2015-10-02 13:53:07 +00002520 RegionNode *RN = Stmt.isRegionStmt()
2521 ? Stmt.getRegion()->getNode()
2522 : getRegion().getBBNode(Stmt.getBasicBlock());
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002523
Johannes Doerfertf17a78e2015-10-04 15:00:05 +00002524 if (StmtIt->isEmpty() ||
2525 isl_set_is_empty(DomainMap[getRegionNodeBasicBlock(RN)]) ||
2526 (RemoveIgnoredStmts && isIgnored(RN))) {
2527
Michael Krusecac948e2015-10-02 13:53:07 +00002528 // Remove the statement because it is unnecessary.
2529 if (Stmt.isRegionStmt())
2530 for (BasicBlock *BB : Stmt.getRegion()->blocks())
2531 StmtMap.erase(BB);
2532 else
2533 StmtMap.erase(Stmt.getBasicBlock());
2534
2535 StmtIt = Stmts.erase(StmtIt);
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002536 continue;
2537 }
2538
Michael Krusecac948e2015-10-02 13:53:07 +00002539 StmtIt++;
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002540 }
2541}
2542
2543void Scop::hoistInvariantLoads() {
2544 isl_union_map *Writes = getWrites();
2545 for (ScopStmt &Stmt : *this) {
2546
2547 // TODO: Loads that are not loop carried, hence are in a statement with
2548 // zero iterators, are by construction invariant, though we
Johannes Doerfert09e36972015-10-07 20:17:36 +00002549 // currently "hoist" them anyway. This is necessary because we allow
2550 // them to be treated as parameters (e.g., in conditions) and our code
2551 // generation would otherwise use the old value.
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002552
Johannes Doerfert8930f482015-10-02 14:51:00 +00002553 BasicBlock *BB = Stmt.isBlockStmt() ? Stmt.getBasicBlock()
2554 : Stmt.getRegion()->getEntry();
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002555 isl_set *Domain = Stmt.getDomain();
2556 MemoryAccessList InvMAs;
2557
2558 for (MemoryAccess *MA : Stmt) {
2559 if (MA->isImplicit() || MA->isWrite() || !MA->isAffine())
2560 continue;
2561
Johannes Doerfert8930f482015-10-02 14:51:00 +00002562 // Skip accesses in non-affine subregions as they might not be executed
2563 // under the same condition as the entry of the non-affine subregion.
2564 if (BB != MA->getAccessInstruction()->getParent())
2565 continue;
2566
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002567 isl_map *AccessRelation = MA->getAccessRelation();
2568 if (isl_map_involves_dims(AccessRelation, isl_dim_in, 0,
2569 Stmt.getNumIterators())) {
2570 isl_map_free(AccessRelation);
2571 continue;
2572 }
2573
2574 AccessRelation =
2575 isl_map_intersect_domain(AccessRelation, isl_set_copy(Domain));
2576 isl_set *AccessRange = isl_map_range(AccessRelation);
2577
2578 isl_union_map *Written = isl_union_map_intersect_range(
2579 isl_union_map_copy(Writes), isl_union_set_from_set(AccessRange));
2580 bool IsWritten = !isl_union_map_is_empty(Written);
2581 isl_union_map_free(Written);
2582
2583 if (IsWritten)
2584 continue;
2585
2586 InvMAs.push_front(MA);
2587 }
2588
2589 // We inserted invariant accesses always in the front but need them to be
2590 // sorted in a "natural order". The statements are already sorted in reverse
2591 // post order and that suffices for the accesses too. The reason we require
2592 // an order in the first place is the dependences between invariant loads
2593 // that can be caused by indirect loads.
2594 InvMAs.reverse();
2595
2596 // Transfer the memory access from the statement to the SCoP.
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002597 Stmt.hoistMemoryAccesses(InvMAs, InvariantEquivClasses);
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002598
2599 isl_set_free(Domain);
2600 }
2601 isl_union_map_free(Writes);
2602
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002603 if (!InvariantEquivClasses.empty())
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002604 IsOptimized = true;
Johannes Doerfert09e36972015-10-07 20:17:36 +00002605
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002606 auto &ScopRIL = *SD.getRequiredInvariantLoads(&getRegion());
Johannes Doerfert09e36972015-10-07 20:17:36 +00002607 // Check required invariant loads that were tagged during SCoP detection.
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002608 for (LoadInst *LI : ScopRIL) {
Johannes Doerfert09e36972015-10-07 20:17:36 +00002609 assert(LI && getRegion().contains(LI));
2610 ScopStmt *Stmt = getStmtForBasicBlock(LI->getParent());
2611 if (Stmt && Stmt->lookupAccessesFor(LI) != nullptr) {
2612 DEBUG(dbgs() << "\n\nWARNING: Load (" << *LI
2613 << ") is required to be invariant but was not marked as "
2614 "such. SCoP for "
2615 << getRegion() << " will be dropped\n\n");
2616 addAssumption(isl_set_empty(getParamSpace()));
2617 return;
2618 }
2619 }
2620
2621 // We want invariant accesses to be sorted in a "natural order" because there
2622 // might be dependences between invariant loads. These can be caused by
2623 // indirect loads but also because an invariant load is only conditionally
2624 // executed and the condition is dependent on another invariant load. As we
2625 // want to do code generation in a straight forward way, e.g., preload the
2626 // accesses in the list one after another, we sort them such that the
2627 // preloaded values needed in the conditions will always be in front. Before
2628 // we already ordered the accesses such that indirect loads can be resolved,
2629 // thus we use a stable sort here.
2630
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002631 auto compareInvariantAccesses = [this](
2632 const InvariantEquivClassTy &IAClass0,
2633 const InvariantEquivClassTy &IAClass1) {
2634 const InvariantAccessTy &IA0 = IAClass0.second.front();
2635 const InvariantAccessTy &IA1 = IAClass1.second.front();
2636
Johannes Doerfert09e36972015-10-07 20:17:36 +00002637 Instruction *AI0 = IA0.first->getAccessInstruction();
2638 Instruction *AI1 = IA1.first->getAccessInstruction();
2639
2640 const SCEV *S0 =
2641 SE->isSCEVable(AI0->getType()) ? SE->getSCEV(AI0) : nullptr;
2642 const SCEV *S1 =
2643 SE->isSCEVable(AI1->getType()) ? SE->getSCEV(AI1) : nullptr;
2644
2645 isl_id *Id0 = getIdForParam(S0);
2646 isl_id *Id1 = getIdForParam(S1);
2647
2648 if (Id0 && !Id1) {
2649 isl_id_free(Id0);
2650 isl_id_free(Id1);
2651 return true;
2652 }
2653
2654 if (!Id0) {
2655 isl_id_free(Id0);
2656 isl_id_free(Id1);
2657 return false;
2658 }
2659
2660 assert(Id0 && Id1);
2661
2662 isl_set *Dom0 = IA0.second;
2663 isl_set *Dom1 = IA1.second;
2664
2665 int Dim0 = isl_set_find_dim_by_id(Dom0, isl_dim_param, Id0);
Johannes Doerfert09e36972015-10-07 20:17:36 +00002666
Johannes Doerfert09e36972015-10-07 20:17:36 +00002667 bool Involves1Id0 = isl_set_involves_dims(Dom1, isl_dim_param, Dim0, 1);
David Blaikie91e113d2015-10-09 18:22:18 +00002668 assert(!Involves1Id0 ||
2669 !isl_set_involves_dims(
2670 Dom0, isl_dim_param,
2671 isl_set_find_dim_by_id(Dom0, isl_dim_param, Id1), 1));
Johannes Doerfert09e36972015-10-07 20:17:36 +00002672
2673 isl_id_free(Id0);
2674 isl_id_free(Id1);
2675
2676 return Involves1Id0;
2677 };
2678
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002679 std::stable_sort(InvariantEquivClasses.begin(), InvariantEquivClasses.end(),
Johannes Doerfert09e36972015-10-07 20:17:36 +00002680 compareInvariantAccesses);
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002681}
2682
Johannes Doerfert80ef1102014-11-07 08:31:31 +00002683const ScopArrayInfo *
2684Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType,
Michael Kruse28468772015-09-14 15:45:33 +00002685 ArrayRef<const SCEV *> Sizes, bool IsPHI) {
Tobias Grosser92245222015-07-28 14:53:44 +00002686 auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)];
Tobias Grosser99c70dd2015-09-26 08:55:54 +00002687 if (!SAI) {
Tobias Grosserd46fd5e2015-08-12 15:27:16 +00002688 SAI.reset(new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI,
2689 this));
Tobias Grosser99c70dd2015-09-26 08:55:54 +00002690 } else {
2691 if (Sizes.size() > SAI->getNumberOfDimensions())
2692 SAI->updateSizes(Sizes);
2693 }
Tobias Grosserab671442015-05-23 05:58:27 +00002694 return SAI.get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00002695}
2696
Tobias Grosser92245222015-07-28 14:53:44 +00002697const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) {
2698 auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get();
Johannes Doerfert1a28a892014-10-05 11:32:18 +00002699 assert(SAI && "No ScopArrayInfo available for this base pointer");
2700 return SAI;
2701}
2702
Tobias Grosser74394f02013-01-14 22:40:23 +00002703std::string Scop::getContextStr() const { return stringFromIslObj(Context); }
Tobias Grosser5e6813d2014-07-02 17:47:48 +00002704std::string Scop::getAssumedContextStr() const {
2705 return stringFromIslObj(AssumedContext);
2706}
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002707std::string Scop::getBoundaryContextStr() const {
2708 return stringFromIslObj(BoundaryContext);
2709}
Tobias Grosser75805372011-04-29 06:27:02 +00002710
2711std::string Scop::getNameStr() const {
2712 std::string ExitName, EntryName;
2713 raw_string_ostream ExitStr(ExitName);
2714 raw_string_ostream EntryStr(EntryName);
2715
Tobias Grosserf240b482014-01-09 10:42:15 +00002716 R.getEntry()->printAsOperand(EntryStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00002717 EntryStr.str();
2718
2719 if (R.getExit()) {
Tobias Grosserf240b482014-01-09 10:42:15 +00002720 R.getExit()->printAsOperand(ExitStr, false);
Tobias Grosser75805372011-04-29 06:27:02 +00002721 ExitStr.str();
2722 } else
2723 ExitName = "FunctionExit";
2724
2725 return EntryName + "---" + ExitName;
2726}
2727
Tobias Grosser74394f02013-01-14 22:40:23 +00002728__isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); }
Tobias Grosser37487052011-10-06 00:03:42 +00002729__isl_give isl_space *Scop::getParamSpace() const {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002730 return isl_set_get_space(Context);
Tobias Grosser37487052011-10-06 00:03:42 +00002731}
2732
Tobias Grossere86109f2013-10-29 21:05:49 +00002733__isl_give isl_set *Scop::getAssumedContext() const {
2734 return isl_set_copy(AssumedContext);
2735}
2736
Johannes Doerfert43788c52015-08-20 05:58:56 +00002737__isl_give isl_set *Scop::getRuntimeCheckContext() const {
2738 isl_set *RuntimeCheckContext = getAssumedContext();
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002739 RuntimeCheckContext =
2740 isl_set_intersect(RuntimeCheckContext, getBoundaryContext());
2741 RuntimeCheckContext = simplifyAssumptionContext(RuntimeCheckContext, *this);
Johannes Doerfert43788c52015-08-20 05:58:56 +00002742 return RuntimeCheckContext;
2743}
2744
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00002745bool Scop::hasFeasibleRuntimeContext() const {
Johannes Doerfert43788c52015-08-20 05:58:56 +00002746 isl_set *RuntimeCheckContext = getRuntimeCheckContext();
Johannes Doerfert5d5b3062015-08-20 18:06:30 +00002747 RuntimeCheckContext = addNonEmptyDomainConstraints(RuntimeCheckContext);
Johannes Doerfert43788c52015-08-20 05:58:56 +00002748 bool IsFeasible = !isl_set_is_empty(RuntimeCheckContext);
2749 isl_set_free(RuntimeCheckContext);
2750 return IsFeasible;
2751}
2752
Tobias Grosser5e6813d2014-07-02 17:47:48 +00002753void Scop::addAssumption(__isl_take isl_set *Set) {
2754 AssumedContext = isl_set_intersect(AssumedContext, Set);
Tobias Grosser7b50bee2014-11-25 10:51:12 +00002755 AssumedContext = isl_set_coalesce(AssumedContext);
Tobias Grosser5e6813d2014-07-02 17:47:48 +00002756}
2757
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002758__isl_give isl_set *Scop::getBoundaryContext() const {
2759 return isl_set_copy(BoundaryContext);
2760}
2761
Tobias Grosser75805372011-04-29 06:27:02 +00002762void Scop::printContext(raw_ostream &OS) const {
2763 OS << "Context:\n";
2764
2765 if (!Context) {
2766 OS.indent(4) << "n/a\n\n";
2767 return;
2768 }
2769
2770 OS.indent(4) << getContextStr() << "\n";
Tobias Grosser60b54f12011-11-08 15:41:28 +00002771
Tobias Grosser5e6813d2014-07-02 17:47:48 +00002772 OS.indent(4) << "Assumed Context:\n";
2773 if (!AssumedContext) {
2774 OS.indent(4) << "n/a\n\n";
2775 return;
2776 }
2777
2778 OS.indent(4) << getAssumedContextStr() << "\n";
2779
Johannes Doerfert883f8c12015-09-15 22:52:53 +00002780 OS.indent(4) << "Boundary Context:\n";
2781 if (!BoundaryContext) {
2782 OS.indent(4) << "n/a\n\n";
2783 return;
2784 }
2785
2786 OS.indent(4) << getBoundaryContextStr() << "\n";
2787
Tobias Grosser083d3d32014-06-28 08:59:45 +00002788 for (const SCEV *Parameter : Parameters) {
Tobias Grosser60b54f12011-11-08 15:41:28 +00002789 int Dim = ParameterIds.find(Parameter)->second;
Tobias Grosser60b54f12011-11-08 15:41:28 +00002790 OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n";
2791 }
Tobias Grosser75805372011-04-29 06:27:02 +00002792}
2793
Johannes Doerfertb164c792014-09-18 11:17:17 +00002794void Scop::printAliasAssumptions(raw_ostream &OS) const {
Tobias Grosserbb853c22015-07-25 12:31:03 +00002795 int noOfGroups = 0;
2796 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002797 if (Pair.second.size() == 0)
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002798 noOfGroups += 1;
2799 else
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002800 noOfGroups += Pair.second.size();
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002801 }
2802
Tobias Grosserbb853c22015-07-25 12:31:03 +00002803 OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n";
Johannes Doerfertb164c792014-09-18 11:17:17 +00002804 if (MinMaxAliasGroups.empty()) {
2805 OS.indent(8) << "n/a\n";
2806 return;
2807 }
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002808
Tobias Grosserbb853c22015-07-25 12:31:03 +00002809 for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002810
2811 // If the group has no read only accesses print the write accesses.
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002812 if (Pair.second.empty()) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002813 OS.indent(8) << "[[";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002814 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00002815 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
2816 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002817 }
2818 OS << " ]]\n";
2819 }
2820
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002821 for (const MinMaxAccessTy &MMAReadOnly : Pair.second) {
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002822 OS.indent(8) << "[[";
Tobias Grosserbb853c22015-07-25 12:31:03 +00002823 OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">";
Johannes Doerfert210b09a2015-07-26 13:14:38 +00002824 for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) {
Tobias Grosserbb853c22015-07-25 12:31:03 +00002825 OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second
2826 << ">";
Johannes Doerfert338b42c2015-07-23 17:04:54 +00002827 }
2828 OS << " ]]\n";
2829 }
Johannes Doerfertb164c792014-09-18 11:17:17 +00002830 }
2831}
2832
Tobias Grosser75805372011-04-29 06:27:02 +00002833void Scop::printStatements(raw_ostream &OS) const {
2834 OS << "Statements {\n";
2835
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002836 for (const ScopStmt &Stmt : *this)
2837 OS.indent(4) << Stmt;
Tobias Grosser75805372011-04-29 06:27:02 +00002838
2839 OS.indent(4) << "}\n";
2840}
2841
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002842void Scop::printArrayInfo(raw_ostream &OS) const {
2843 OS << "Arrays {\n";
2844
Tobias Grosserab671442015-05-23 05:58:27 +00002845 for (auto &Array : arrays())
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002846 Array.second->print(OS);
2847
2848 OS.indent(4) << "}\n";
Tobias Grosserd46fd5e2015-08-12 15:27:16 +00002849
2850 OS.indent(4) << "Arrays (Bounds as pw_affs) {\n";
2851
2852 for (auto &Array : arrays())
2853 Array.second->print(OS, /* SizeAsPwAff */ true);
2854
2855 OS.indent(4) << "}\n";
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002856}
2857
Tobias Grosser75805372011-04-29 06:27:02 +00002858void Scop::print(raw_ostream &OS) const {
Tobias Grosser4eb7ddb2014-03-18 18:51:11 +00002859 OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName()
2860 << "\n";
Tobias Grosser483fdd42014-03-18 18:05:38 +00002861 OS.indent(4) << "Region: " << getNameStr() << "\n";
David Peixottodc0a11c2015-01-13 18:31:55 +00002862 OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n";
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002863 OS.indent(4) << "Invariant Accesses: {\n";
Johannes Doerfert697fdf82015-10-09 17:12:26 +00002864 for (const auto &IAClass : InvariantEquivClasses) {
2865 if (IAClass.second.empty()) {
2866 OS.indent(12) << "Class Pointer: " << IAClass.first << "\n";
2867 } else {
2868 IAClass.second.front().first->print(OS);
2869 OS.indent(12) << "Execution Context: " << IAClass.second.front().second
2870 << "\n";
2871 }
Johannes Doerfertc1db67e2015-09-29 23:47:21 +00002872 }
2873 OS.indent(4) << "}\n";
Tobias Grosser75805372011-04-29 06:27:02 +00002874 printContext(OS.indent(4));
Tobias Grosser49ad36c2015-05-20 08:05:31 +00002875 printArrayInfo(OS.indent(4));
Johannes Doerfertb164c792014-09-18 11:17:17 +00002876 printAliasAssumptions(OS);
Tobias Grosser75805372011-04-29 06:27:02 +00002877 printStatements(OS.indent(4));
2878}
2879
2880void Scop::dump() const { print(dbgs()); }
2881
Tobias Grosser9a38ab82011-11-08 15:41:03 +00002882isl_ctx *Scop::getIslCtx() const { return IslCtx; }
Tobias Grosser75805372011-04-29 06:27:02 +00002883
Johannes Doerfertcef616f2015-09-15 22:49:04 +00002884__isl_give isl_pw_aff *Scop::getPwAff(const SCEV *E, BasicBlock *BB) {
2885 return Affinator.getPwAff(E, BB);
Johannes Doerfert574182d2015-08-12 10:19:50 +00002886}
2887
Tobias Grosser808cd692015-07-14 09:33:13 +00002888__isl_give isl_union_set *Scop::getDomains() const {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002889 isl_union_set *Domain = isl_union_set_empty(getParamSpace());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00002890
Tobias Grosser808cd692015-07-14 09:33:13 +00002891 for (const ScopStmt &Stmt : *this)
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002892 Domain = isl_union_set_add_set(Domain, Stmt.getDomain());
Tobias Grosser5f9a7622012-02-14 14:02:40 +00002893
2894 return Domain;
2895}
2896
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002897__isl_give isl_union_map *Scop::getMustWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002898 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002899
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002900 for (ScopStmt &Stmt : *this) {
2901 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002902 if (!MA->isMustWrite())
2903 continue;
2904
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002905 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002906 isl_map *AccessDomain = MA->getAccessRelation();
2907 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2908 Write = isl_union_map_add_map(Write, AccessDomain);
2909 }
2910 }
2911 return isl_union_map_coalesce(Write);
2912}
2913
2914__isl_give isl_union_map *Scop::getMayWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002915 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002916
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002917 for (ScopStmt &Stmt : *this) {
2918 for (MemoryAccess *MA : Stmt) {
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002919 if (!MA->isMayWrite())
2920 continue;
2921
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002922 isl_set *Domain = Stmt.getDomain();
Tobias Grosser780ce0f2014-07-11 07:12:10 +00002923 isl_map *AccessDomain = MA->getAccessRelation();
2924 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2925 Write = isl_union_map_add_map(Write, AccessDomain);
2926 }
2927 }
2928 return isl_union_map_coalesce(Write);
2929}
2930
Tobias Grosser37eb4222014-02-20 21:43:54 +00002931__isl_give isl_union_map *Scop::getWrites() {
Tobias Grossereeb9f3c2015-05-26 21:37:31 +00002932 isl_union_map *Write = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002933
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002934 for (ScopStmt &Stmt : *this) {
2935 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00002936 if (!MA->isWrite())
Tobias Grosser37eb4222014-02-20 21:43:54 +00002937 continue;
2938
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002939 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00002940 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00002941 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2942 Write = isl_union_map_add_map(Write, AccessDomain);
2943 }
2944 }
2945 return isl_union_map_coalesce(Write);
2946}
2947
2948__isl_give isl_union_map *Scop::getReads() {
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002949 isl_union_map *Read = isl_union_map_empty(getParamSpace());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002950
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002951 for (ScopStmt &Stmt : *this) {
2952 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertf6752892014-06-13 18:01:45 +00002953 if (!MA->isRead())
Tobias Grosser37eb4222014-02-20 21:43:54 +00002954 continue;
2955
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002956 isl_set *Domain = Stmt.getDomain();
Johannes Doerfertf6752892014-06-13 18:01:45 +00002957 isl_map *AccessDomain = MA->getAccessRelation();
Tobias Grosser37eb4222014-02-20 21:43:54 +00002958
2959 AccessDomain = isl_map_intersect_domain(AccessDomain, Domain);
2960 Read = isl_union_map_add_map(Read, AccessDomain);
2961 }
2962 }
2963 return isl_union_map_coalesce(Read);
2964}
2965
Tobias Grosser808cd692015-07-14 09:33:13 +00002966__isl_give isl_union_map *Scop::getSchedule() const {
2967 auto Tree = getScheduleTree();
2968 auto S = isl_schedule_get_map(Tree);
2969 isl_schedule_free(Tree);
2970 return S;
2971}
Tobias Grosser37eb4222014-02-20 21:43:54 +00002972
Tobias Grosser808cd692015-07-14 09:33:13 +00002973__isl_give isl_schedule *Scop::getScheduleTree() const {
2974 return isl_schedule_intersect_domain(isl_schedule_copy(Schedule),
2975 getDomains());
2976}
Tobias Grosserbc4ef902014-06-28 08:59:38 +00002977
Tobias Grosser808cd692015-07-14 09:33:13 +00002978void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) {
2979 auto *S = isl_schedule_from_domain(getDomains());
2980 S = isl_schedule_insert_partial_schedule(
2981 S, isl_multi_union_pw_aff_from_union_map(NewSchedule));
2982 isl_schedule_free(Schedule);
2983 Schedule = S;
2984}
2985
2986void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) {
2987 isl_schedule_free(Schedule);
2988 Schedule = NewSchedule;
Tobias Grosser37eb4222014-02-20 21:43:54 +00002989}
2990
2991bool Scop::restrictDomains(__isl_take isl_union_set *Domain) {
2992 bool Changed = false;
Tobias Grosser7c3bad52015-05-27 05:16:57 +00002993 for (ScopStmt &Stmt : *this) {
2994 isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain());
Tobias Grosser37eb4222014-02-20 21:43:54 +00002995 isl_union_set *NewStmtDomain = isl_union_set_intersect(
2996 isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain));
2997
2998 if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) {
2999 isl_union_set_free(StmtDomain);
3000 isl_union_set_free(NewStmtDomain);
3001 continue;
3002 }
3003
3004 Changed = true;
3005
3006 isl_union_set_free(StmtDomain);
3007 NewStmtDomain = isl_union_set_coalesce(NewStmtDomain);
3008
3009 if (isl_union_set_is_empty(NewStmtDomain)) {
Tobias Grosser7c3bad52015-05-27 05:16:57 +00003010 Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace()));
Tobias Grosser37eb4222014-02-20 21:43:54 +00003011 isl_union_set_free(NewStmtDomain);
3012 } else
Tobias Grosser7c3bad52015-05-27 05:16:57 +00003013 Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain));
Tobias Grosser37eb4222014-02-20 21:43:54 +00003014 }
3015 isl_union_set_free(Domain);
3016 return Changed;
3017}
3018
Tobias Grosser75805372011-04-29 06:27:02 +00003019ScalarEvolution *Scop::getSE() const { return SE; }
3020
Johannes Doerfertf5673802015-10-01 23:48:18 +00003021bool Scop::isIgnored(RegionNode *RN) {
3022 BasicBlock *BB = getRegionNodeBasicBlock(RN);
Tobias Grosser75805372011-04-29 06:27:02 +00003023
Johannes Doerfertf5673802015-10-01 23:48:18 +00003024 // Check if there are accesses contained.
3025 bool ContainsAccesses = false;
3026 if (!RN->isSubRegion())
3027 ContainsAccesses = getAccessFunctions(BB);
3028 else
3029 for (BasicBlock *RBB : RN->getNodeAs<Region>()->blocks())
3030 ContainsAccesses |= (getAccessFunctions(RBB) != nullptr);
3031 if (!ContainsAccesses)
3032 return true;
3033
3034 // Check for reachability via non-error blocks.
3035 if (!DomainMap.count(BB))
3036 return true;
3037
3038 // Check if error blocks are contained.
Johannes Doerfert08d90a32015-10-07 20:32:43 +00003039 if (containsErrorBlock(RN, getRegion(), LI, DT))
Johannes Doerfertf5673802015-10-01 23:48:18 +00003040 return true;
3041
3042 return false;
Tobias Grosser75805372011-04-29 06:27:02 +00003043}
3044
Tobias Grosser808cd692015-07-14 09:33:13 +00003045struct MapToDimensionDataTy {
3046 int N;
3047 isl_union_pw_multi_aff *Res;
3048};
Johannes Doerfertff9d1982015-02-24 12:00:50 +00003049
Tobias Grosser808cd692015-07-14 09:33:13 +00003050// @brief Create a function that maps the elements of 'Set' to its N-th
3051// dimension.
3052//
3053// The result is added to 'User->Res'.
3054//
3055// @param Set The input set.
3056// @param N The dimension to map to.
3057//
3058// @returns Zero if no error occurred, non-zero otherwise.
3059static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) {
3060 struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User;
3061 int Dim;
3062 isl_space *Space;
3063 isl_pw_multi_aff *PMA;
3064
3065 Dim = isl_set_dim(Set, isl_dim_set);
3066 Space = isl_set_get_space(Set);
3067 PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N,
3068 Dim - Data->N);
3069 if (Data->N > 1)
3070 PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1);
3071 Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA);
3072
3073 isl_set_free(Set);
3074
3075 return isl_stat_ok;
Johannes Doerfertff9d1982015-02-24 12:00:50 +00003076}
3077
Tobias Grosser808cd692015-07-14 09:33:13 +00003078// @brief Create a function that maps the elements of Domain to their Nth
3079// dimension.
3080//
3081// @param Domain The set of elements to map.
3082// @param N The dimension to map to.
3083static __isl_give isl_multi_union_pw_aff *
3084mapToDimension(__isl_take isl_union_set *Domain, int N) {
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003085 if (N <= 0 || isl_union_set_is_empty(Domain)) {
3086 isl_union_set_free(Domain);
3087 return nullptr;
3088 }
3089
Tobias Grosser808cd692015-07-14 09:33:13 +00003090 struct MapToDimensionDataTy Data;
3091 isl_space *Space;
3092
3093 Space = isl_union_set_get_space(Domain);
3094 Data.N = N;
3095 Data.Res = isl_union_pw_multi_aff_empty(Space);
3096 if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0)
3097 Data.Res = isl_union_pw_multi_aff_free(Data.Res);
3098
3099 isl_union_set_free(Domain);
3100 return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res);
3101}
3102
Michael Kruse9d080092015-09-11 21:41:48 +00003103ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R) {
Tobias Grosser808cd692015-07-14 09:33:13 +00003104 ScopStmt *Stmt;
3105 if (BB) {
Michael Kruse9d080092015-09-11 21:41:48 +00003106 Stmts.emplace_back(*this, *BB);
Tobias Grosser808cd692015-07-14 09:33:13 +00003107 Stmt = &Stmts.back();
3108 StmtMap[BB] = Stmt;
3109 } else {
3110 assert(R && "Either basic block or a region expected.");
Michael Kruse9d080092015-09-11 21:41:48 +00003111 Stmts.emplace_back(*this, *R);
Tobias Grosser808cd692015-07-14 09:33:13 +00003112 Stmt = &Stmts.back();
3113 for (BasicBlock *BB : R->blocks())
3114 StmtMap[BB] = Stmt;
3115 }
3116 return Stmt;
3117}
3118
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003119void Scop::buildSchedule(
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00003120 Region *R,
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003121 DenseMap<Loop *, std::pair<isl_schedule *, unsigned>> &LoopSchedules) {
Michael Kruse046dde42015-08-10 13:01:57 +00003122
Johannes Doerfert40fa56f2015-09-14 11:15:07 +00003123 if (SD.isNonAffineSubRegion(R, &getRegion())) {
Johannes Doerfertc6987c12015-09-26 13:41:43 +00003124 Loop *L = getLoopSurroundingRegion(*R, LI);
3125 auto &LSchedulePair = LoopSchedules[L];
Michael Krusecac948e2015-10-02 13:53:07 +00003126 ScopStmt *Stmt = getStmtForBasicBlock(R->getEntry());
Michael Kruseafe06702015-10-02 16:33:27 +00003127 isl_set *Domain = Stmt->getDomain();
Michael Krusecac948e2015-10-02 13:53:07 +00003128 auto *UDomain = isl_union_set_from_set(Domain);
3129 auto *StmtSchedule = isl_schedule_from_domain(UDomain);
Johannes Doerfert40fa56f2015-09-14 11:15:07 +00003130 LSchedulePair.first = StmtSchedule;
3131 return;
3132 }
3133
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003134 ReversePostOrderTraversal<Region *> RTraversal(R);
3135 for (auto *RN : RTraversal) {
Michael Kruse046dde42015-08-10 13:01:57 +00003136
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003137 if (RN->isSubRegion()) {
3138 Region *SubRegion = RN->getNodeAs<Region>();
3139 if (!SD.isNonAffineSubRegion(SubRegion, &getRegion())) {
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00003140 buildSchedule(SubRegion, LoopSchedules);
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003141 continue;
3142 }
Tobias Grosser75805372011-04-29 06:27:02 +00003143 }
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003144
3145 Loop *L = getRegionNodeLoop(RN, LI);
3146 int LD = getRelativeLoopDepth(L);
3147 auto &LSchedulePair = LoopSchedules[L];
3148 LSchedulePair.second += getNumBlocksInRegionNode(RN);
3149
Michael Krusecac948e2015-10-02 13:53:07 +00003150 BasicBlock *BB = getRegionNodeBasicBlock(RN);
3151 ScopStmt *Stmt = getStmtForBasicBlock(BB);
3152 if (Stmt) {
Johannes Doerfertb68cffb2015-09-10 15:27:46 +00003153 auto *UDomain = isl_union_set_from_set(Stmt->getDomain());
3154 auto *StmtSchedule = isl_schedule_from_domain(UDomain);
3155 LSchedulePair.first =
3156 combineInSequence(LSchedulePair.first, StmtSchedule);
3157 }
3158
3159 unsigned NumVisited = LSchedulePair.second;
3160 while (L && NumVisited == L->getNumBlocks()) {
3161 auto *LDomain = isl_schedule_get_domain(LSchedulePair.first);
3162 if (auto *MUPA = mapToDimension(LDomain, LD + 1))
3163 LSchedulePair.first =
3164 isl_schedule_insert_partial_schedule(LSchedulePair.first, MUPA);
3165
3166 auto *PL = L->getParentLoop();
3167 assert(LoopSchedules.count(PL));
3168 auto &PSchedulePair = LoopSchedules[PL];
3169 PSchedulePair.first =
3170 combineInSequence(PSchedulePair.first, LSchedulePair.first);
3171 PSchedulePair.second += NumVisited;
3172
3173 L = PL;
3174 NumVisited = PSchedulePair.second;
3175 }
Tobias Grosser808cd692015-07-14 09:33:13 +00003176 }
Tobias Grosser75805372011-04-29 06:27:02 +00003177}
3178
Johannes Doerfert7c494212014-10-31 23:13:39 +00003179ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const {
Tobias Grosser57411e32015-05-27 06:51:34 +00003180 auto StmtMapIt = StmtMap.find(BB);
Johannes Doerfert7c494212014-10-31 23:13:39 +00003181 if (StmtMapIt == StmtMap.end())
3182 return nullptr;
3183 return StmtMapIt->second;
3184}
3185
Johannes Doerfert96425c22015-08-30 21:13:53 +00003186int Scop::getRelativeLoopDepth(const Loop *L) const {
3187 Loop *OuterLoop =
3188 L ? R.outermostLoopInRegion(const_cast<Loop *>(L)) : nullptr;
3189 if (!OuterLoop)
3190 return -1;
Johannes Doerfertd020b772015-08-27 06:53:52 +00003191 return L->getLoopDepth() - OuterLoop->getLoopDepth();
3192}
3193
Michael Krused868b5d2015-09-10 15:25:24 +00003194void ScopInfo::buildPHIAccesses(PHINode *PHI, Region &R,
Michael Krused868b5d2015-09-10 15:25:24 +00003195 Region *NonAffineSubRegion, bool IsExitBlock) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003196
3197 // PHI nodes that are in the exit block of the region, hence if IsExitBlock is
3198 // true, are not modeled as ordinary PHI nodes as they are not part of the
3199 // region. However, we model the operands in the predecessor blocks that are
3200 // part of the region as regular scalar accesses.
3201
3202 // If we can synthesize a PHI we can skip it, however only if it is in
3203 // the region. If it is not it can only be in the exit block of the region.
3204 // In this case we model the operands but not the PHI itself.
3205 if (!IsExitBlock && canSynthesize(PHI, LI, SE, &R))
3206 return;
3207
3208 // PHI nodes are modeled as if they had been demoted prior to the SCoP
3209 // detection. Hence, the PHI is a load of a new memory location in which the
3210 // incoming value was written at the end of the incoming basic block.
3211 bool OnlyNonAffineSubRegionOperands = true;
3212 for (unsigned u = 0; u < PHI->getNumIncomingValues(); u++) {
3213 Value *Op = PHI->getIncomingValue(u);
3214 BasicBlock *OpBB = PHI->getIncomingBlock(u);
3215
3216 // Do not build scalar dependences inside a non-affine subregion.
3217 if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB))
3218 continue;
3219
3220 OnlyNonAffineSubRegionOperands = false;
3221
3222 if (!R.contains(OpBB))
3223 continue;
3224
3225 Instruction *OpI = dyn_cast<Instruction>(Op);
3226 if (OpI) {
3227 BasicBlock *OpIBB = OpI->getParent();
3228 // As we pretend there is a use (or more precise a write) of OpI in OpBB
3229 // we have to insert a scalar dependence from the definition of OpI to
3230 // OpBB if the definition is not in OpBB.
Michael Kruse668af712015-10-15 14:45:48 +00003231 if (scop->getStmtForBasicBlock(OpIBB) !=
3232 scop->getStmtForBasicBlock(OpBB)) {
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003233 addScalarReadAccess(OpI, PHI, OpBB);
3234 addScalarWriteAccess(OpI);
Michael Kruse7bf39442015-09-10 12:46:52 +00003235 }
Tobias Grosserda95a4a2015-09-24 20:59:59 +00003236 } else if (ModelReadOnlyScalars && !isa<Constant>(Op)) {
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003237 addScalarReadAccess(Op, PHI, OpBB);
Michael Kruse7bf39442015-09-10 12:46:52 +00003238 }
3239
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003240 addPHIWriteAccess(PHI, OpBB, Op, IsExitBlock);
Michael Kruse7bf39442015-09-10 12:46:52 +00003241 }
3242
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003243 if (!OnlyNonAffineSubRegionOperands && !IsExitBlock) {
3244 addPHIReadAccess(PHI);
Michael Kruse7bf39442015-09-10 12:46:52 +00003245 }
3246}
3247
Michael Krused868b5d2015-09-10 15:25:24 +00003248bool ScopInfo::buildScalarDependences(Instruction *Inst, Region *R,
3249 Region *NonAffineSubRegion) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003250 bool canSynthesizeInst = canSynthesize(Inst, LI, SE, R);
3251 if (isIgnoredIntrinsic(Inst))
3252 return false;
3253
3254 bool AnyCrossStmtUse = false;
3255 BasicBlock *ParentBB = Inst->getParent();
3256
3257 for (User *U : Inst->users()) {
3258 Instruction *UI = dyn_cast<Instruction>(U);
3259
3260 // Ignore the strange user
3261 if (UI == 0)
3262 continue;
3263
3264 BasicBlock *UseParent = UI->getParent();
3265
3266 // Ignore the users in the same BB (statement)
3267 if (UseParent == ParentBB)
3268 continue;
3269
3270 // Do not build scalar dependences inside a non-affine subregion.
3271 if (NonAffineSubRegion && NonAffineSubRegion->contains(UseParent))
3272 continue;
3273
Michael Kruse01cb3792015-10-17 21:07:08 +00003274 // Check for PHI nodes in the region exit and skip them, if they will be
Tobias Grosser05d7fa72015-10-17 21:46:28 +00003275 // modeled as PHI nodes.
Michael Kruse01cb3792015-10-17 21:07:08 +00003276 //
3277 // PHI nodes in the region exit that have more than two incoming edges need
Tobias Grosser05d7fa72015-10-17 21:46:28 +00003278 // to be modeled as PHI-Nodes to correctly model the fact that depending on
3279 // the control flow a different value will be assigned to the PHI node. In
3280 // case this is the case, there is no need to create an additional normal
3281 // scalar dependence. Hence, bail out before we register an "out-of-region"
3282 // use for this definition.
Michael Kruse01cb3792015-10-17 21:07:08 +00003283 if (isa<PHINode>(UI) && UI->getParent() == R->getExit() &&
3284 !R->getExitingBlock())
3285 continue;
3286
Michael Kruse7bf39442015-09-10 12:46:52 +00003287 // Check whether or not the use is in the SCoP.
Michael Kruse225f0d12015-10-17 21:36:00 +00003288 // If there is single exiting block, the single incoming value exit for node
3289 // PHIs are handled like any escaping SCALAR. Otherwise, as if the PHI
3290 // belongs to the the scop region.
3291 bool IsExitNodePHI = isa<PHINode>(UI) && UI->getParent() == R->getExit();
3292 if (!R->contains(UseParent) && (R->getExitingBlock() || !IsExitNodePHI)) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003293 AnyCrossStmtUse = true;
3294 continue;
3295 }
3296
3297 // If the instruction can be synthesized and the user is in the region
3298 // we do not need to add scalar dependences.
3299 if (canSynthesizeInst)
3300 continue;
3301
3302 // No need to translate these scalar dependences into polyhedral form,
3303 // because synthesizable scalars can be generated by the code generator.
3304 if (canSynthesize(UI, LI, SE, R))
3305 continue;
3306
3307 // Skip PHI nodes in the region as they handle their operands on their own.
3308 if (isa<PHINode>(UI))
3309 continue;
3310
3311 // Now U is used in another statement.
3312 AnyCrossStmtUse = true;
3313
3314 // Do not build a read access that is not in the current SCoP
Michael Krusee2bccbb2015-09-18 19:59:43 +00003315 // Use the def instruction as base address of the MemoryAccess, so that it
3316 // will become the name of the scalar access in the polyhedral form.
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003317 addScalarReadAccess(Inst, UI);
Michael Kruse7bf39442015-09-10 12:46:52 +00003318 }
3319
Tobias Grosserda95a4a2015-09-24 20:59:59 +00003320 if (ModelReadOnlyScalars && !isa<PHINode>(Inst)) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003321 for (Value *Op : Inst->operands()) {
3322 if (canSynthesize(Op, LI, SE, R))
3323 continue;
3324
3325 if (Instruction *OpInst = dyn_cast<Instruction>(Op))
3326 if (R->contains(OpInst))
3327 continue;
3328
3329 if (isa<Constant>(Op))
3330 continue;
3331
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003332 addScalarReadAccess(Op, Inst);
Michael Kruse7bf39442015-09-10 12:46:52 +00003333 }
3334 }
3335
3336 return AnyCrossStmtUse;
3337}
3338
3339extern MapInsnToMemAcc InsnToMemAcc;
3340
Michael Krusee2bccbb2015-09-18 19:59:43 +00003341void ScopInfo::buildMemoryAccess(
3342 Instruction *Inst, Loop *L, Region *R,
Johannes Doerfert09e36972015-10-07 20:17:36 +00003343 const ScopDetection::BoxedLoopsSetTy *BoxedLoops,
3344 const InvariantLoadsSetTy &ScopRIL) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003345 unsigned Size;
3346 Type *SizeType;
3347 Value *Val;
Michael Krusee2bccbb2015-09-18 19:59:43 +00003348 enum MemoryAccess::AccessType Type;
Michael Kruse7bf39442015-09-10 12:46:52 +00003349
3350 if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
3351 SizeType = Load->getType();
3352 Size = TD->getTypeStoreSize(SizeType);
Michael Krusee2bccbb2015-09-18 19:59:43 +00003353 Type = MemoryAccess::READ;
Michael Kruse7bf39442015-09-10 12:46:52 +00003354 Val = Load;
3355 } else {
3356 StoreInst *Store = cast<StoreInst>(Inst);
3357 SizeType = Store->getValueOperand()->getType();
3358 Size = TD->getTypeStoreSize(SizeType);
Michael Krusee2bccbb2015-09-18 19:59:43 +00003359 Type = MemoryAccess::MUST_WRITE;
Michael Kruse7bf39442015-09-10 12:46:52 +00003360 Val = Store->getValueOperand();
3361 }
3362
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003363 auto Address = getPointerOperand(*Inst);
3364
3365 const SCEV *AccessFunction = SE->getSCEVAtScope(Address, L);
Michael Kruse7bf39442015-09-10 12:46:52 +00003366 const SCEVUnknown *BasePointer =
3367 dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
3368
3369 assert(BasePointer && "Could not find base pointer");
3370 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
3371
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003372 if (isa<GetElementPtrInst>(Address) || isa<BitCastInst>(Address)) {
3373 auto NewAddress = Address;
3374 if (auto *BitCast = dyn_cast<BitCastInst>(Address)) {
3375 auto Src = BitCast->getOperand(0);
3376 auto SrcTy = Src->getType();
3377 auto DstTy = BitCast->getType();
3378 if (SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits())
3379 NewAddress = Src;
3380 }
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003381
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003382 if (auto *GEP = dyn_cast<GetElementPtrInst>(NewAddress)) {
3383 std::vector<const SCEV *> Subscripts;
3384 std::vector<int> Sizes;
3385 std::tie(Subscripts, Sizes) = getIndexExpressionsFromGEP(GEP, *SE);
3386 auto BasePtr = GEP->getOperand(0);
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003387
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003388 std::vector<const SCEV *> SizesSCEV;
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003389
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003390 bool AllAffineSubcripts = true;
Johannes Doerfert09e36972015-10-07 20:17:36 +00003391 for (auto Subscript : Subscripts) {
3392 InvariantLoadsSetTy AccessILS;
3393 AllAffineSubcripts =
3394 isAffineExpr(R, Subscript, *SE, nullptr, &AccessILS);
3395
3396 for (LoadInst *LInst : AccessILS)
3397 if (!ScopRIL.count(LInst))
3398 AllAffineSubcripts = false;
3399
3400 if (!AllAffineSubcripts)
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003401 break;
Johannes Doerfert09e36972015-10-07 20:17:36 +00003402 }
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003403
3404 if (AllAffineSubcripts && Sizes.size() > 0) {
3405 for (auto V : Sizes)
3406 SizesSCEV.push_back(SE->getSCEV(ConstantInt::get(
3407 IntegerType::getInt64Ty(BasePtr->getContext()), V)));
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003408 SizesSCEV.push_back(SE->getSCEV(ConstantInt::get(
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003409 IntegerType::getInt64Ty(BasePtr->getContext()), Size)));
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003410
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003411 addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, true,
3412 Subscripts, SizesSCEV, Val);
Tobias Grosserb1c39422015-09-21 16:19:25 +00003413 return;
Tobias Grosser6f36d9a2015-09-17 20:16:21 +00003414 }
Tobias Grosser5fd8c092015-09-17 17:28:15 +00003415 }
3416 }
3417
Michael Kruse7bf39442015-09-10 12:46:52 +00003418 auto AccItr = InsnToMemAcc.find(Inst);
Michael Krusee2bccbb2015-09-18 19:59:43 +00003419 if (PollyDelinearize && AccItr != InsnToMemAcc.end()) {
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003420 addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, true,
3421 AccItr->second.DelinearizedSubscripts,
3422 AccItr->second.Shape->DelinearizedSizes, Val);
Michael Krusee2bccbb2015-09-18 19:59:43 +00003423 return;
3424 }
Michael Kruse7bf39442015-09-10 12:46:52 +00003425
3426 // Check if the access depends on a loop contained in a non-affine subregion.
3427 bool isVariantInNonAffineLoop = false;
3428 if (BoxedLoops) {
3429 SetVector<const Loop *> Loops;
3430 findLoops(AccessFunction, Loops);
3431 for (const Loop *L : Loops)
3432 if (BoxedLoops->count(L))
3433 isVariantInNonAffineLoop = true;
3434 }
3435
Johannes Doerfert09e36972015-10-07 20:17:36 +00003436 InvariantLoadsSetTy AccessILS;
3437 bool IsAffine =
3438 !isVariantInNonAffineLoop &&
3439 isAffineExpr(R, AccessFunction, *SE, BasePointer->getValue(), &AccessILS);
3440
3441 for (LoadInst *LInst : AccessILS)
3442 if (!ScopRIL.count(LInst))
3443 IsAffine = false;
Michael Kruse7bf39442015-09-10 12:46:52 +00003444
Michael Krusecaac2b62015-09-26 15:51:44 +00003445 // FIXME: Size of the number of bytes of an array element, not the number of
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003446 // elements as probably intended here.
Tobias Grossera43b6e92015-09-27 17:54:50 +00003447 const SCEV *SizeSCEV =
3448 SE->getConstant(TD->getIntPtrType(Inst->getContext()), Size);
Michael Kruse7bf39442015-09-10 12:46:52 +00003449
Michael Krusee2bccbb2015-09-18 19:59:43 +00003450 if (!IsAffine && Type == MemoryAccess::MUST_WRITE)
3451 Type = MemoryAccess::MAY_WRITE;
Michael Kruse7bf39442015-09-10 12:46:52 +00003452
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003453 addExplicitAccess(Inst, Type, BasePointer->getValue(), Size, IsAffine,
3454 ArrayRef<const SCEV *>(AccessFunction),
3455 ArrayRef<const SCEV *>(SizeSCEV), Val);
Michael Kruse7bf39442015-09-10 12:46:52 +00003456}
3457
Michael Krused868b5d2015-09-10 15:25:24 +00003458void ScopInfo::buildAccessFunctions(Region &R, Region &SR) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003459
3460 if (SD->isNonAffineSubRegion(&SR, &R)) {
3461 for (BasicBlock *BB : SR.blocks())
3462 buildAccessFunctions(R, *BB, &SR);
3463 return;
3464 }
3465
3466 for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
3467 if (I->isSubRegion())
3468 buildAccessFunctions(R, *I->getNodeAs<Region>());
3469 else
3470 buildAccessFunctions(R, *I->getNodeAs<BasicBlock>());
3471}
3472
Michael Krusecac948e2015-10-02 13:53:07 +00003473void ScopInfo::buildStmts(Region &SR) {
3474 Region *R = getRegion();
3475
3476 if (SD->isNonAffineSubRegion(&SR, R)) {
3477 scop->addScopStmt(nullptr, &SR);
3478 return;
3479 }
3480
3481 for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
3482 if (I->isSubRegion())
3483 buildStmts(*I->getNodeAs<Region>());
3484 else
3485 scop->addScopStmt(I->getNodeAs<BasicBlock>(), nullptr);
3486}
3487
Michael Krused868b5d2015-09-10 15:25:24 +00003488void ScopInfo::buildAccessFunctions(Region &R, BasicBlock &BB,
3489 Region *NonAffineSubRegion,
3490 bool IsExitBlock) {
Michael Kruse7bf39442015-09-10 12:46:52 +00003491 Loop *L = LI->getLoopFor(&BB);
3492
3493 // The set of loops contained in non-affine subregions that are part of R.
3494 const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD->getBoxedLoops(&R);
3495
Johannes Doerfert09e36972015-10-07 20:17:36 +00003496 // The set of loads that are required to be invariant.
3497 auto &ScopRIL = *SD->getRequiredInvariantLoads(&R);
3498
Michael Kruse7bf39442015-09-10 12:46:52 +00003499 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) {
3500 Instruction *Inst = I;
3501
3502 PHINode *PHI = dyn_cast<PHINode>(Inst);
3503 if (PHI)
Michael Krusee2bccbb2015-09-18 19:59:43 +00003504 buildPHIAccesses(PHI, R, NonAffineSubRegion, IsExitBlock);
Michael Kruse7bf39442015-09-10 12:46:52 +00003505
3506 // For the exit block we stop modeling after the last PHI node.
3507 if (!PHI && IsExitBlock)
3508 break;
3509
Johannes Doerfert09e36972015-10-07 20:17:36 +00003510 // TODO: At this point we only know that elements of ScopRIL have to be
3511 // invariant and will be hoisted for the SCoP to be processed. Though,
3512 // there might be other invariant accesses that will be hoisted and
3513 // that would allow to make a non-affine access affine.
Michael Kruse7bf39442015-09-10 12:46:52 +00003514 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
Johannes Doerfert09e36972015-10-07 20:17:36 +00003515 buildMemoryAccess(Inst, L, &R, BoxedLoops, ScopRIL);
Michael Kruse7bf39442015-09-10 12:46:52 +00003516
3517 if (isIgnoredIntrinsic(Inst))
3518 continue;
3519
Johannes Doerfert09e36972015-10-07 20:17:36 +00003520 // Do not build scalar dependences for required invariant loads as we will
3521 // hoist them later on anyway or drop the SCoP if we cannot.
3522 if (ScopRIL.count(dyn_cast<LoadInst>(Inst)))
3523 continue;
3524
Michael Kruse7bf39442015-09-10 12:46:52 +00003525 if (buildScalarDependences(Inst, &R, NonAffineSubRegion)) {
Michael Krusee2bccbb2015-09-18 19:59:43 +00003526 if (!isa<StoreInst>(Inst))
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003527 addScalarWriteAccess(Inst);
Michael Kruse7bf39442015-09-10 12:46:52 +00003528 }
3529 }
Michael Krusee2bccbb2015-09-18 19:59:43 +00003530}
Michael Kruse7bf39442015-09-10 12:46:52 +00003531
Michael Kruse2d0ece92015-09-24 11:41:21 +00003532void ScopInfo::addMemoryAccess(BasicBlock *BB, Instruction *Inst,
3533 MemoryAccess::AccessType Type,
3534 Value *BaseAddress, unsigned ElemBytes,
3535 bool Affine, Value *AccessValue,
3536 ArrayRef<const SCEV *> Subscripts,
Michael Kruse8d0b7342015-09-25 21:21:00 +00003537 ArrayRef<const SCEV *> Sizes,
3538 MemoryAccess::AccessOrigin Origin) {
Michael Krusecac948e2015-10-02 13:53:07 +00003539 ScopStmt *Stmt = scop->getStmtForBasicBlock(BB);
3540
3541 // Do not create a memory access for anything not in the SCoP. It would be
3542 // ignored anyway.
3543 if (!Stmt)
3544 return;
3545
Michael Krusee2bccbb2015-09-18 19:59:43 +00003546 AccFuncSetType &AccList = AccFuncMap[BB];
3547 size_t Identifier = AccList.size();
Michael Kruse7bf39442015-09-10 12:46:52 +00003548
Michael Krusee2bccbb2015-09-18 19:59:43 +00003549 Value *BaseAddr = BaseAddress;
3550 std::string BaseName = getIslCompatibleName("MemRef_", BaseAddr, "");
3551
3552 std::string IdName = "__polly_array_ref_" + std::to_string(Identifier);
3553 isl_id *Id = isl_id_alloc(ctx, IdName.c_str(), nullptr);
3554
Michael Krusecac948e2015-10-02 13:53:07 +00003555 bool isApproximated =
3556 Stmt->isRegionStmt() && (Stmt->getRegion()->getEntry() != BB);
3557 if (isApproximated && Type == MemoryAccess::MUST_WRITE)
3558 Type = MemoryAccess::MAY_WRITE;
3559
3560 AccList.emplace_back(Stmt, Inst, Id, Type, BaseAddress, ElemBytes, Affine,
Michael Kruse8d0b7342015-09-25 21:21:00 +00003561 Subscripts, Sizes, AccessValue, Origin, BaseName);
Michael Krusecac948e2015-10-02 13:53:07 +00003562 Stmt->addAccess(&AccList.back());
Michael Kruse7bf39442015-09-10 12:46:52 +00003563}
3564
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003565void ScopInfo::addExplicitAccess(
3566 Instruction *MemAccInst, MemoryAccess::AccessType Type, Value *BaseAddress,
3567 unsigned ElemBytes, bool IsAffine, ArrayRef<const SCEV *> Subscripts,
3568 ArrayRef<const SCEV *> Sizes, Value *AccessValue) {
3569 assert(isa<LoadInst>(MemAccInst) || isa<StoreInst>(MemAccInst));
3570 assert(isa<LoadInst>(MemAccInst) == (Type == MemoryAccess::READ));
3571 addMemoryAccess(MemAccInst->getParent(), MemAccInst, Type, BaseAddress,
Michael Kruse8d0b7342015-09-25 21:21:00 +00003572 ElemBytes, IsAffine, AccessValue, Subscripts, Sizes,
3573 MemoryAccess::EXPLICIT);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003574}
3575void ScopInfo::addScalarWriteAccess(Instruction *Value) {
3576 addMemoryAccess(Value->getParent(), Value, MemoryAccess::MUST_WRITE, Value, 1,
3577 true, Value, ArrayRef<const SCEV *>(),
Michael Kruse8d0b7342015-09-25 21:21:00 +00003578 ArrayRef<const SCEV *>(), MemoryAccess::SCALAR);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003579}
3580void ScopInfo::addScalarReadAccess(Value *Value, Instruction *User) {
3581 assert(!isa<PHINode>(User));
3582 addMemoryAccess(User->getParent(), User, MemoryAccess::READ, Value, 1, true,
3583 Value, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
Michael Kruse8d0b7342015-09-25 21:21:00 +00003584 MemoryAccess::SCALAR);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003585}
3586void ScopInfo::addScalarReadAccess(Value *Value, PHINode *User,
3587 BasicBlock *UserBB) {
3588 addMemoryAccess(UserBB, User, MemoryAccess::READ, Value, 1, true, Value,
Michael Kruse8d0b7342015-09-25 21:21:00 +00003589 ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
3590 MemoryAccess::SCALAR);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003591}
3592void ScopInfo::addPHIWriteAccess(PHINode *PHI, BasicBlock *IncomingBlock,
3593 Value *IncomingValue, bool IsExitBlock) {
3594 addMemoryAccess(IncomingBlock, IncomingBlock->getTerminator(),
3595 MemoryAccess::MUST_WRITE, PHI, 1, true, IncomingValue,
3596 ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
Michael Kruse8d0b7342015-09-25 21:21:00 +00003597 IsExitBlock ? MemoryAccess::SCALAR : MemoryAccess::PHI);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003598}
3599void ScopInfo::addPHIReadAccess(PHINode *PHI) {
3600 addMemoryAccess(PHI->getParent(), PHI, MemoryAccess::READ, PHI, 1, true, PHI,
Michael Kruse8d0b7342015-09-25 21:21:00 +00003601 ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
3602 MemoryAccess::PHI);
Michael Kruse33d6c0b2015-09-25 18:53:27 +00003603}
3604
Michael Kruse76e924d2015-09-30 09:16:07 +00003605void ScopInfo::buildScop(Region &R, DominatorTree &DT) {
Michael Kruse9d080092015-09-11 21:41:48 +00003606 unsigned MaxLoopDepth = getMaxLoopDepthInRegion(R, *LI, *SD);
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00003607 scop = new Scop(R, AccFuncMap, *SD, *SE, DT, *LI, ctx, MaxLoopDepth);
Michael Kruse7bf39442015-09-10 12:46:52 +00003608
Michael Krusecac948e2015-10-02 13:53:07 +00003609 buildStmts(R);
Michael Kruse7bf39442015-09-10 12:46:52 +00003610 buildAccessFunctions(R, R);
3611
3612 // In case the region does not have an exiting block we will later (during
3613 // code generation) split the exit block. This will move potential PHI nodes
3614 // from the current exit block into the new region exiting block. Hence, PHI
3615 // nodes that are at this point not part of the region will be.
3616 // To handle these PHI nodes later we will now model their operands as scalar
3617 // accesses. Note that we do not model anything in the exit block if we have
3618 // an exiting block in the region, as there will not be any splitting later.
3619 if (!R.getExitingBlock())
3620 buildAccessFunctions(R, *R.getExit(), nullptr, /* IsExitBlock */ true);
3621
Johannes Doerfertd8dd8632015-10-07 20:31:36 +00003622 scop->init(*AA);
Michael Kruse7bf39442015-09-10 12:46:52 +00003623}
3624
Michael Krused868b5d2015-09-10 15:25:24 +00003625void ScopInfo::print(raw_ostream &OS, const Module *) const {
Michael Kruse9d080092015-09-11 21:41:48 +00003626 if (!scop) {
Michael Krused868b5d2015-09-10 15:25:24 +00003627 OS << "Invalid Scop!\n";
Michael Kruse9d080092015-09-11 21:41:48 +00003628 return;
3629 }
3630
Michael Kruse9d080092015-09-11 21:41:48 +00003631 scop->print(OS);
Michael Kruse7bf39442015-09-10 12:46:52 +00003632}
3633
Michael Krused868b5d2015-09-10 15:25:24 +00003634void ScopInfo::clear() {
Michael Kruse7bf39442015-09-10 12:46:52 +00003635 AccFuncMap.clear();
Michael Krused868b5d2015-09-10 15:25:24 +00003636 if (scop) {
3637 delete scop;
3638 scop = 0;
3639 }
Michael Kruse7bf39442015-09-10 12:46:52 +00003640}
3641
3642//===----------------------------------------------------------------------===//
Michael Kruse9d080092015-09-11 21:41:48 +00003643ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
Tobias Grosserb76f38532011-08-20 11:11:25 +00003644 ctx = isl_ctx_alloc();
Tobias Grosser4a8e3562011-12-07 07:42:51 +00003645 isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);
Tobias Grosserb76f38532011-08-20 11:11:25 +00003646}
3647
3648ScopInfo::~ScopInfo() {
3649 clear();
3650 isl_ctx_free(ctx);
3651}
3652
Tobias Grosser75805372011-04-29 06:27:02 +00003653void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Michael Krused868b5d2015-09-10 15:25:24 +00003654 AU.addRequiredID(IndependentBlocksID);
Chandler Carruthf5579872015-01-17 14:16:56 +00003655 AU.addRequired<LoopInfoWrapperPass>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00003656 AU.addRequired<RegionInfoPass>();
Johannes Doerfert96425c22015-08-30 21:13:53 +00003657 AU.addRequired<DominatorTreeWrapperPass>();
Michael Krused868b5d2015-09-10 15:25:24 +00003658 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
3659 AU.addRequiredTransitive<ScopDetection>();
Chandler Carruth66ef16b2015-09-09 22:13:56 +00003660 AU.addRequired<AAResultsWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00003661 AU.setPreservesAll();
3662}
3663
3664bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) {
Michael Krused868b5d2015-09-10 15:25:24 +00003665 SD = &getAnalysis<ScopDetection>();
Tobias Grosser75805372011-04-29 06:27:02 +00003666
Michael Krused868b5d2015-09-10 15:25:24 +00003667 if (!SD->isMaxRegionInScop(*R))
3668 return false;
3669
3670 Function *F = R->getEntry()->getParent();
3671 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
3672 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
3673 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
3674 TD = &F->getParent()->getDataLayout();
3675 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Michael Krused868b5d2015-09-10 15:25:24 +00003676
Michael Kruse76e924d2015-09-30 09:16:07 +00003677 buildScop(*R, DT);
Tobias Grosser75805372011-04-29 06:27:02 +00003678
Tobias Grosserd6a50b32015-05-30 06:26:21 +00003679 DEBUG(scop->print(dbgs()));
3680
Michael Kruseafe06702015-10-02 16:33:27 +00003681 if (scop->isEmpty() || !scop->hasFeasibleRuntimeContext()) {
Johannes Doerfert43788c52015-08-20 05:58:56 +00003682 delete scop;
3683 scop = nullptr;
3684 return false;
3685 }
3686
Johannes Doerfert120de4b2015-08-20 18:30:08 +00003687 // Statistics.
3688 ++ScopFound;
3689 if (scop->getMaxLoopDepth() > 0)
3690 ++RichScopFound;
Tobias Grosser75805372011-04-29 06:27:02 +00003691 return false;
3692}
3693
3694char ScopInfo::ID = 0;
3695
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00003696Pass *polly::createScopInfoPass() { return new ScopInfo(); }
3697
Tobias Grosser73600b82011-10-08 00:30:40 +00003698INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops",
3699 "Polly - Create polyhedral description of Scops", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00003700 false);
Chandler Carruth66ef16b2015-09-09 22:13:56 +00003701INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +00003702INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00003703INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosserc5bcf242015-08-17 10:57:08 +00003704INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
Johannes Doerfertff9d1982015-02-24 12:00:50 +00003705INITIALIZE_PASS_DEPENDENCY(ScopDetection);
Johannes Doerfert96425c22015-08-30 21:13:53 +00003706INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser73600b82011-10-08 00:30:40 +00003707INITIALIZE_PASS_END(ScopInfo, "polly-scops",
3708 "Polly - Create polyhedral description of Scops", false,
3709 false)