Polly 23.0.0git
ScopBuilder.cpp
Go to the documentation of this file.
1//===- ScopBuilder.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Create a polyhedral description for a static control flow region.
10//
11// The pass creates a polyhedral description of the Scops detected by the SCoP
12// detection derived from their LLVM-IR code.
13//
14//===----------------------------------------------------------------------===//
15
16#include "polly/ScopBuilder.h"
17#include "polly/Options.h"
18#include "polly/ScopDetection.h"
19#include "polly/ScopInfo.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/EquivalenceClasses.h"
27#include "llvm/ADT/PostOrderIterator.h"
28#include "llvm/ADT/Sequence.h"
29#include "llvm/ADT/SmallSet.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Analysis/AliasAnalysis.h"
32#include "llvm/Analysis/AssumptionCache.h"
33#include "llvm/Analysis/Delinearization.h"
34#include "llvm/Analysis/Loads.h"
35#include "llvm/Analysis/LoopInfo.h"
36#include "llvm/Analysis/OptimizationRemarkEmitter.h"
37#include "llvm/Analysis/RegionInfo.h"
38#include "llvm/Analysis/RegionIterator.h"
39#include "llvm/Analysis/ScalarEvolution.h"
40#include "llvm/Analysis/ScalarEvolutionExpressions.h"
41#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/DataLayout.h"
43#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/DerivedTypes.h"
45#include "llvm/IR/Dominators.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/InstrTypes.h"
48#include "llvm/IR/Instruction.h"
49#include "llvm/IR/Instructions.h"
50#include "llvm/IR/Type.h"
51#include "llvm/IR/Use.h"
52#include "llvm/IR/Value.h"
53#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/Compiler.h"
55#include "llvm/Support/Debug.h"
56#include "llvm/Support/ErrorHandling.h"
57#include "llvm/Support/raw_ostream.h"
58#include <cassert>
59#include <deque>
60
61using namespace llvm;
62using namespace polly;
63
65#define DEBUG_TYPE "polly-scops"
66
67STATISTIC(ScopFound, "Number of valid Scops");
68STATISTIC(RichScopFound, "Number of Scops containing a loop");
69STATISTIC(InfeasibleScops,
70 "Number of SCoPs with statically infeasible context.");
71
73
74// The maximal number of dimensions we allow during invariant load construction.
75// More complex access ranges will result in very high compile time and are also
76// unlikely to result in good code. This value is very high and should only
77// trigger for corner cases (e.g., the "dct_luma" function in h264, SPEC2006).
78static unsigned const MaxDimensionsInAccessRange = 9;
79
80static cl::opt<bool, true> XModelReadOnlyScalars(
81 "polly-analyze-read-only-scalars",
82 cl::desc("Model read-only scalar values in the scop description"),
83 cl::location(ModelReadOnlyScalars), cl::Hidden, cl::init(true),
84 cl::cat(PollyCategory));
85
86static cl::opt<int>
87 OptComputeOut("polly-analysis-computeout",
88 cl::desc("Bound the scop analysis by a maximal amount of "
89 "computational steps (0 means no bound)"),
90 cl::Hidden, cl::init(800000), cl::cat(PollyCategory));
91
93 "polly-allow-dereference-of-all-function-parameters",
94 cl::desc(
95 "Treat all parameters to functions that are pointers as dereferencible."
96 " This is useful for invariant load hoisting, since we can generate"
97 " less runtime checks. This is only valid if all pointers to functions"
98 " are always initialized, so that Polly can choose to hoist"
99 " their loads. "),
100 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
101
102static cl::opt<bool>
103 PollyIgnoreInbounds("polly-ignore-inbounds",
104 cl::desc("Do not take inbounds assumptions at all"),
105 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
106
107static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup(
108 "polly-rtc-max-arrays-per-group",
109 cl::desc("The maximal number of arrays to compare in each alias group."),
110 cl::Hidden, cl::init(20), cl::cat(PollyCategory));
111
112static cl::opt<unsigned> RunTimeChecksMaxAccessDisjuncts(
113 "polly-rtc-max-array-disjuncts",
114 cl::desc("The maximal number of disjunts allowed in memory accesses to "
115 "to build RTCs."),
116 cl::Hidden, cl::init(8), cl::cat(PollyCategory));
117
118static cl::opt<unsigned> RunTimeChecksMaxParameters(
119 "polly-rtc-max-parameters",
120 cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden,
121 cl::init(8), cl::cat(PollyCategory));
122
123static cl::opt<bool> UnprofitableScalarAccs(
124 "polly-unprofitable-scalar-accs",
125 cl::desc("Count statements with scalar accesses as not optimizable"),
126 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
127
128static cl::opt<std::string> UserContextStr(
129 "polly-context", cl::value_desc("isl parameter set"),
130 cl::desc("Provide additional constraints on the context parameters"),
131 cl::init(""), cl::cat(PollyCategory));
132
133static cl::opt<bool> DetectReductions("polly-detect-reductions",
134 cl::desc("Detect and exploit reductions"),
135 cl::Hidden, cl::init(true),
136 cl::cat(PollyCategory));
137
138// Multiplicative reductions can be disabled separately as these kind of
139// operations can overflow easily. Additive reductions and bit operations
140// are in contrast pretty stable.
142 "polly-disable-multiplicative-reductions",
143 cl::desc("Disable multiplicative reductions"), cl::Hidden,
144 cl::cat(PollyCategory));
145
147
148static cl::opt<GranularityChoice> StmtGranularity(
149 "polly-stmt-granularity",
150 cl::desc(
151 "Algorithm to use for splitting basic blocks into multiple statements"),
152 cl::values(clEnumValN(GranularityChoice::BasicBlocks, "bb",
153 "One statement per basic block"),
154 clEnumValN(GranularityChoice::ScalarIndependence, "scalar-indep",
155 "Scalar independence heuristic"),
156 clEnumValN(GranularityChoice::Stores, "store",
157 "Store-level granularity")),
159
160/// Helper to treat non-affine regions and basic blocks the same.
161///
162///{
163
164/// Return the block that is the representing block for @p RN.
165static inline BasicBlock *getRegionNodeBasicBlock(RegionNode *RN) {
166 return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry()
167 : RN->getNodeAs<BasicBlock>();
168}
169
170/// Return the @p idx'th block that is executed after @p RN.
171static inline BasicBlock *
172getRegionNodeSuccessor(RegionNode *RN, Instruction *TI, unsigned idx) {
173 if (RN->isSubRegion()) {
174 assert(idx == 0);
175 return RN->getNodeAs<Region>()->getExit();
176 }
177 return TI->getSuccessor(idx);
178}
179
180static bool containsErrorBlock(RegionNode *RN, const Region &R,
181 ScopDetection *SD) {
182 if (!RN->isSubRegion())
183 return SD->isErrorBlock(*RN->getNodeAs<BasicBlock>(), R);
184 for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks())
185 if (SD->isErrorBlock(*BB, R))
186 return true;
187 return false;
188}
189
190///}
191
192/// Create a map to map from a given iteration to a subsequent iteration.
193///
194/// This map maps from SetSpace -> SetSpace where the dimensions @p Dim
195/// is incremented by one and all other dimensions are equal, e.g.,
196/// [i0, i1, i2, i3] -> [i0, i1, i2 + 1, i3]
197///
198/// if @p Dim is 2 and @p SetSpace has 4 dimensions.
199static isl::map createNextIterationMap(isl::space SetSpace, unsigned Dim) {
200 isl::space MapSpace = SetSpace.map_from_set();
201 isl::map NextIterationMap = isl::map::universe(MapSpace);
202 for (unsigned u : rangeIslSize(0, NextIterationMap.domain_tuple_dim()))
203 if (u != Dim)
204 NextIterationMap =
205 NextIterationMap.equate(isl::dim::in, u, isl::dim::out, u);
208 C = C.set_constant_si(1);
209 C = C.set_coefficient_si(isl::dim::in, Dim, 1);
210 C = C.set_coefficient_si(isl::dim::out, Dim, -1);
211 NextIterationMap = NextIterationMap.add_constraint(C);
212 return NextIterationMap;
213}
214
215/// Add @p BSet to set @p BoundedParts if @p BSet is bounded.
217 isl::set BoundedParts = isl::set::empty(S.get_space());
218 for (isl::basic_set BSet : S.get_basic_set_list())
219 if (BSet.is_bounded())
220 BoundedParts = BoundedParts.unite(isl::set(BSet));
221 return BoundedParts;
222}
223
224/// Compute the (un)bounded parts of @p S wrt. to dimension @p Dim.
225///
226/// @returns A separation of @p S into first an unbounded then a bounded subset,
227/// both with regards to the dimension @p Dim.
228static std::pair<isl::set, isl::set> partitionSetParts(isl::set S,
229 unsigned Dim) {
230 for (unsigned u : rangeIslSize(0, S.tuple_dim()))
231 S = S.lower_bound_si(isl::dim::set, u, 0);
232
233 unsigned NumDimsS = unsignedFromIslSize(S.tuple_dim());
234 isl::set OnlyDimS = S;
235
236 // Remove dimensions that are greater than Dim as they are not interesting.
237 assert(NumDimsS >= Dim + 1);
238 OnlyDimS = OnlyDimS.project_out(isl::dim::set, Dim + 1, NumDimsS - Dim - 1);
239
240 // Create artificial parametric upper bounds for dimensions smaller than Dim
241 // as we are not interested in them.
242 OnlyDimS = OnlyDimS.insert_dims(isl::dim::param, 0, Dim);
243
244 for (unsigned u = 0; u < Dim; u++) {
246 isl::local_space(OnlyDimS.get_space()));
247 C = C.set_coefficient_si(isl::dim::param, u, 1);
248 C = C.set_coefficient_si(isl::dim::set, u, -1);
249 OnlyDimS = OnlyDimS.add_constraint(C);
250 }
251
252 // Collect all bounded parts of OnlyDimS.
253 isl::set BoundedParts = collectBoundedParts(OnlyDimS);
254
255 // Create the dimensions greater than Dim again.
256 BoundedParts =
257 BoundedParts.insert_dims(isl::dim::set, Dim + 1, NumDimsS - Dim - 1);
258
259 // Remove the artificial upper bound parameters again.
260 BoundedParts = BoundedParts.remove_dims(isl::dim::param, 0, Dim);
261
262 isl::set UnboundedParts = S.subtract(BoundedParts);
263 return std::make_pair(UnboundedParts, BoundedParts);
264}
265
266/// Create the conditions under which @p L @p Pred @p R is true.
267static isl::set buildConditionSet(ICmpInst::Predicate Pred, isl::pw_aff L,
268 isl::pw_aff R) {
269 switch (Pred) {
270 case ICmpInst::ICMP_EQ:
271 return L.eq_set(R);
272 case ICmpInst::ICMP_NE:
273 return L.ne_set(R);
274 case ICmpInst::ICMP_SLT:
275 return L.lt_set(R);
276 case ICmpInst::ICMP_SLE:
277 return L.le_set(R);
278 case ICmpInst::ICMP_SGT:
279 return L.gt_set(R);
280 case ICmpInst::ICMP_SGE:
281 return L.ge_set(R);
282 case ICmpInst::ICMP_ULT:
283 return L.lt_set(R);
284 case ICmpInst::ICMP_UGT:
285 return L.gt_set(R);
286 case ICmpInst::ICMP_ULE:
287 return L.le_set(R);
288 case ICmpInst::ICMP_UGE:
289 return L.ge_set(R);
290 default:
291 llvm_unreachable("Non integer predicate not supported");
292 }
293}
294
296 Loop *NewL) {
297 // If the loops are the same there is nothing to do.
298 if (NewL == OldL)
299 return Dom;
300
301 int OldDepth = scop->getRelativeLoopDepth(OldL);
302 int NewDepth = scop->getRelativeLoopDepth(NewL);
303 // If both loops are non-affine loops there is nothing to do.
304 if (OldDepth == -1 && NewDepth == -1)
305 return Dom;
306
307 // Distinguish three cases:
308 // 1) The depth is the same but the loops are not.
309 // => One loop was left one was entered.
310 // 2) The depth increased from OldL to NewL.
311 // => One loop was entered, none was left.
312 // 3) The depth decreased from OldL to NewL.
313 // => Loops were left were difference of the depths defines how many.
314 if (OldDepth == NewDepth) {
315 assert(OldL->getParentLoop() == NewL->getParentLoop());
316 Dom = Dom.project_out(isl::dim::set, NewDepth, 1);
317 Dom = Dom.add_dims(isl::dim::set, 1);
318 } else if (OldDepth < NewDepth) {
319 assert(OldDepth + 1 == NewDepth);
320 auto &R = scop->getRegion();
321 (void)R;
322 assert(NewL->getParentLoop() == OldL ||
323 ((!OldL || !R.contains(OldL)) && R.contains(NewL)));
324 Dom = Dom.add_dims(isl::dim::set, 1);
325 } else {
326 assert(OldDepth > NewDepth);
327 unsigned Diff = OldDepth - NewDepth;
328 unsigned NumDim = unsignedFromIslSize(Dom.tuple_dim());
329 assert(NumDim >= Diff);
330 Dom = Dom.project_out(isl::dim::set, NumDim - Diff, Diff);
331 }
332
333 return Dom;
334}
335
338 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
339 const SCEV *E, bool NonNegative, bool IsInsideDomain) {
340 PWACtx PWAC =
341 scop->getPwAff(E, BB, NonNegative, &RecordedAssumptions, IsInsideDomain);
342 InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(PWAC.second);
343 return std::move(PWAC.first);
344}
345
346/// Build condition sets for unsigned ICmpInst(s).
347/// Special handling is required for unsigned operands to ensure that if
348/// MSB (aka the Sign bit) is set for an operands in an unsigned ICmpInst
349/// it should wrap around.
350///
351/// @param IsStrictUpperBound holds information on the predicate relation
352/// between TestVal and UpperBound, i.e,
353/// TestVal < UpperBound OR TestVal <= UpperBound
355 BasicBlock *BB, Value *Condition, const isl::set &Domain,
356 const SCEV *SCEV_TestVal, const SCEV *SCEV_UpperBound,
357 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap, bool IsStrictUpperBound,
358 bool IsInsideDomain) {
359 // Do not take NonNeg assumption on TestVal
360 // as it might have MSB (Sign bit) set.
361 isl::pw_aff TestVal = getPwAff(BB, InvalidDomainMap, SCEV_TestVal,
362 /*NonNegative=*/false, IsInsideDomain);
363 // Take NonNeg assumption on UpperBound.
364 isl::pw_aff UpperBound = getPwAff(BB, InvalidDomainMap, SCEV_UpperBound,
365 /*NonNegative=*/true, IsInsideDomain);
366
367 // 0 <= TestVal
368 isl::set First =
369 isl::pw_aff(isl::local_space(TestVal.domain_space())).le_set(TestVal);
370
371 isl::set Second;
372 if (IsStrictUpperBound)
373 // TestVal < UpperBound
374 Second = TestVal.lt_set(std::move(UpperBound));
375 else
376 // TestVal <= UpperBound
377 Second = TestVal.le_set(std::move(UpperBound));
378
379 isl::set ConsequenceCondSet = First.intersect(std::move(Second));
380 return ConsequenceCondSet;
381}
382
384 BasicBlock *BB, SwitchInst *SI, Loop *L, __isl_keep isl_set *Domain,
385 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
386 SmallVectorImpl<__isl_give isl_set *> &ConditionSets, bool IsInsideDomain) {
387 Value *Condition = SI->getCondition();
388
389 isl_pw_aff *LHS, *RHS;
390 LHS = getPwAff(BB, InvalidDomainMap, SE.getSCEVAtScope(Condition, L),
391 /*NonNegative=*/false, IsInsideDomain)
392 .release();
393
394 unsigned NumSuccessors = SI->getNumSuccessors();
395 ConditionSets.resize(NumSuccessors);
396 for (auto &Case : SI->cases()) {
397 unsigned Idx = Case.getSuccessorIndex();
398 ConstantInt *CaseValue = Case.getCaseValue();
399
400 RHS = getPwAff(BB, InvalidDomainMap, SE.getSCEV(CaseValue),
401 /*NonNegative=*/false, IsInsideDomain)
402 .release();
403 isl_set *CaseConditionSet =
404 buildConditionSet(ICmpInst::ICMP_EQ, isl::manage_copy(LHS),
405 isl::manage(RHS))
406 .release();
407 ConditionSets[Idx] = isl_set_coalesce(
408 isl_set_intersect(CaseConditionSet, isl_set_copy(Domain)));
409 }
410
411 assert(ConditionSets[0] == nullptr && "Default condition set was set");
412 isl_set *ConditionSetUnion = isl_set_copy(ConditionSets[1]);
413 for (unsigned u = 2; u < NumSuccessors; u++)
414 ConditionSetUnion =
415 isl_set_union(ConditionSetUnion, isl_set_copy(ConditionSets[u]));
416 ConditionSets[0] = isl_set_subtract(isl_set_copy(Domain), ConditionSetUnion);
417
418 isl_pw_aff_free(LHS);
419
420 return true;
421}
422
424 BasicBlock *BB, Value *Condition, Instruction *TI, Loop *L,
426 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
427 SmallVectorImpl<__isl_give isl_set *> &ConditionSets, bool IsInsideDomain) {
428 isl_set *ConsequenceCondSet = nullptr;
429
430 if (auto Load = dyn_cast<LoadInst>(Condition)) {
431 const SCEV *LHSSCEV = SE.getSCEVAtScope(Load, L);
432 const SCEV *RHSSCEV = SE.getZero(LHSSCEV->getType());
433 bool NonNeg = false;
434 isl_pw_aff *LHS =
435 getPwAff(BB, InvalidDomainMap, LHSSCEV, NonNeg, IsInsideDomain)
436 .release();
437 isl_pw_aff *RHS =
438 getPwAff(BB, InvalidDomainMap, RHSSCEV, NonNeg, IsInsideDomain)
439 .release();
440 ConsequenceCondSet = buildConditionSet(ICmpInst::ICMP_SLE, isl::manage(LHS),
441 isl::manage(RHS))
442 .release();
443 } else if (auto *PHI = dyn_cast<PHINode>(Condition)) {
444 auto *Unique = dyn_cast<ConstantInt>(
445 getUniqueNonErrorValue(PHI, &scop->getRegion(), &SD));
446 assert(Unique &&
447 "A PHINode condition should only be accepted by ScopDetection if "
448 "getUniqueNonErrorValue returns non-NULL");
449
450 if (Unique->isZero())
451 ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
452 else
453 ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain));
454 } else if (auto *CCond = dyn_cast<ConstantInt>(Condition)) {
455 if (CCond->isZero())
456 ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
457 else
458 ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain));
459 } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) {
460 auto Opcode = BinOp->getOpcode();
461 assert(Opcode == Instruction::And || Opcode == Instruction::Or);
462
463 bool Valid =
464 buildConditionSets(BB, BinOp->getOperand(0), TI, L, Domain,
465 InvalidDomainMap, ConditionSets, IsInsideDomain) &&
466 buildConditionSets(BB, BinOp->getOperand(1), TI, L, Domain,
467 InvalidDomainMap, ConditionSets, IsInsideDomain);
468 if (!Valid) {
469 while (!ConditionSets.empty())
470 isl_set_free(ConditionSets.pop_back_val());
471 return false;
472 }
473
474 isl_set_free(ConditionSets.pop_back_val());
475 isl_set *ConsCondPart0 = ConditionSets.pop_back_val();
476 isl_set_free(ConditionSets.pop_back_val());
477 isl_set *ConsCondPart1 = ConditionSets.pop_back_val();
478
479 if (Opcode == Instruction::And)
480 ConsequenceCondSet = isl_set_intersect(ConsCondPart0, ConsCondPart1);
481 else
482 ConsequenceCondSet = isl_set_union(ConsCondPart0, ConsCondPart1);
483 } else {
484 auto *ICond = dyn_cast<ICmpInst>(Condition);
485 assert(ICond &&
486 "Condition of exiting branch was neither constant nor ICmp!");
487
488 Region &R = scop->getRegion();
489
490 isl_pw_aff *LHS, *RHS;
491 // For unsigned comparisons we assumed the signed bit of neither operand
492 // to be set. The comparison is equal to a signed comparison under this
493 // assumption.
494 bool NonNeg = ICond->isUnsigned();
495 const SCEV *LeftOperand = SE.getSCEVAtScope(ICond->getOperand(0), L),
496 *RightOperand = SE.getSCEVAtScope(ICond->getOperand(1), L);
497
498 LeftOperand = tryForwardThroughPHI(LeftOperand, R, SE, &SD);
499 RightOperand = tryForwardThroughPHI(RightOperand, R, SE, &SD);
500
501 switch (ICond->getPredicate()) {
502 case ICmpInst::ICMP_ULT:
503 ConsequenceCondSet = buildUnsignedConditionSets(
504 BB, Condition, isl::manage_copy(Domain),
505 LeftOperand, RightOperand, InvalidDomainMap,
506 /*IsStrictUpperBound=*/true, IsInsideDomain)
507 .release();
508 break;
509 case ICmpInst::ICMP_ULE:
510 ConsequenceCondSet = buildUnsignedConditionSets(
511 BB, Condition, isl::manage_copy(Domain),
512 LeftOperand, RightOperand, InvalidDomainMap,
513 /*IsStrictUpperBound=*/false, IsInsideDomain)
514 .release();
515 break;
516 case ICmpInst::ICMP_UGT:
517 ConsequenceCondSet = buildUnsignedConditionSets(
518 BB, Condition, isl::manage_copy(Domain),
519 RightOperand, LeftOperand, InvalidDomainMap,
520 /*IsStrictUpperBound=*/true, IsInsideDomain)
521 .release();
522 break;
523 case ICmpInst::ICMP_UGE:
524 ConsequenceCondSet = buildUnsignedConditionSets(
525 BB, Condition, isl::manage_copy(Domain),
526 RightOperand, LeftOperand, InvalidDomainMap,
527 /*IsStrictUpperBound=*/false, IsInsideDomain)
528 .release();
529 break;
530 default:
531 LHS = getPwAff(BB, InvalidDomainMap, LeftOperand, NonNeg, IsInsideDomain)
532 .release();
533 RHS = getPwAff(BB, InvalidDomainMap, RightOperand, NonNeg, IsInsideDomain)
534 .release();
535 ConsequenceCondSet = buildConditionSet(ICond->getPredicate(),
536 isl::manage(LHS), isl::manage(RHS))
537 .release();
538 break;
539 }
540 }
541
542 // If no terminator was given we are only looking for parameter constraints
543 // under which @p Condition is true/false.
544 if (!TI)
545 ConsequenceCondSet = isl_set_params(ConsequenceCondSet);
546 assert(ConsequenceCondSet);
547 ConsequenceCondSet = isl_set_coalesce(
548 isl_set_intersect(ConsequenceCondSet, isl_set_copy(Domain)));
549
550 isl_set *AlternativeCondSet = nullptr;
551 bool TooComplex =
552 isl_set_n_basic_set(ConsequenceCondSet) >= (int)MaxDisjunctsInDomain;
553
554 if (!TooComplex) {
555 AlternativeCondSet = isl_set_subtract(isl_set_copy(Domain),
556 isl_set_copy(ConsequenceCondSet));
557 TooComplex =
558 isl_set_n_basic_set(AlternativeCondSet) >= (int)MaxDisjunctsInDomain;
559 }
560
561 if (TooComplex) {
562 scop->invalidate(COMPLEXITY, TI ? TI->getDebugLoc() : DebugLoc(),
563 TI ? TI->getParent() : nullptr /* BasicBlock */);
564 isl_set_free(AlternativeCondSet);
565 isl_set_free(ConsequenceCondSet);
566 return false;
567 }
568
569 ConditionSets.push_back(ConsequenceCondSet);
570 ConditionSets.push_back(isl_set_coalesce(AlternativeCondSet));
571
572 return true;
573}
574
576 BasicBlock *BB, Instruction *TI, Loop *L, __isl_keep isl_set *Domain,
577 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
578 SmallVectorImpl<__isl_give isl_set *> &ConditionSets, bool IsInsideDomain) {
579 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
580 return buildConditionSets(BB, SI, L, Domain, InvalidDomainMap,
581 ConditionSets, IsInsideDomain);
582
583 if (isa<UncondBrInst>(TI)) {
584 ConditionSets.push_back(isl_set_copy(Domain));
585 return true;
586 }
587
588 Value *Condition = cast<CondBrInst>(TI)->getCondition();
589 return buildConditionSets(BB, Condition, TI, L, Domain, InvalidDomainMap,
590 ConditionSets, IsInsideDomain);
591}
592
594 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
595 // Iterate over the region R and propagate the domain constrains from the
596 // predecessors to the current node. In contrast to the
597 // buildDomainsWithBranchConstraints function, this one will pull the domain
598 // information from the predecessors instead of pushing it to the successors.
599 // Additionally, we assume the domains to be already present in the domain
600 // map here. However, we iterate again in reverse post order so we know all
601 // predecessors have been visited before a block or non-affine subregion is
602 // visited.
603
604 ReversePostOrderTraversal<Region *> RTraversal(R);
605 for (auto *RN : RTraversal) {
606 // Recurse for affine subregions but go on for basic blocks and non-affine
607 // subregions.
608 if (RN->isSubRegion()) {
609 Region *SubRegion = RN->getNodeAs<Region>();
610 if (!scop->isNonAffineSubRegion(SubRegion)) {
611 if (!propagateDomainConstraints(SubRegion, InvalidDomainMap))
612 return false;
613 continue;
614 }
615 }
616
617 BasicBlock *BB = getRegionNodeBasicBlock(RN);
618 isl::set &Domain = scop->getOrInitEmptyDomain(BB);
619 assert(!Domain.is_null());
620
621 // Under the union of all predecessor conditions we can reach this block.
623 Domain = Domain.intersect(PredDom).coalesce();
624 Domain = Domain.align_params(scop->getParamSpace());
625
626 Loop *BBLoop = getRegionNodeLoop(RN, LI);
627 if (BBLoop && BBLoop->getHeader() == BB && scop->contains(BBLoop))
628 if (!addLoopBoundsToHeaderDomain(BBLoop, InvalidDomainMap))
629 return false;
630 }
631
632 return true;
633}
634
636 BasicBlock *BB, Loop *BBLoop,
637 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks,
638 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
639 // Check if the block @p BB is the entry of a region. If so we propagate it's
640 // domain to the exit block of the region. Otherwise we are done.
641 auto *RI = scop->getRegion().getRegionInfo();
642 auto *BBReg = RI ? RI->getRegionFor(BB) : nullptr;
643 auto *ExitBB = BBReg ? BBReg->getExit() : nullptr;
644 if (!BBReg || BBReg->getEntry() != BB || !ExitBB || !scop->contains(ExitBB))
645 return;
646
647 // Do not propagate the domain if there is a loop backedge inside the region
648 // that would prevent the exit block from being executed.
649 auto *L = BBLoop;
650 while (L && scop->contains(L)) {
651 SmallVector<BasicBlock *, 4> LatchBBs;
652 BBLoop->getLoopLatches(LatchBBs);
653 for (auto *LatchBB : LatchBBs)
654 if (BB != LatchBB && BBReg->contains(LatchBB))
655 return;
656 L = L->getParentLoop();
657 }
658
659 isl::set Domain = scop->getOrInitEmptyDomain(BB);
660 assert(!Domain.is_null() && "Cannot propagate a nullptr");
661
662 Loop *ExitBBLoop = getFirstNonBoxedLoopFor(ExitBB, LI, scop->getBoxedLoops());
663
664 // Since the dimensions of @p BB and @p ExitBB might be different we have to
665 // adjust the domain before we can propagate it.
666 isl::set AdjustedDomain = adjustDomainDimensions(Domain, BBLoop, ExitBBLoop);
667 isl::set &ExitDomain = scop->getOrInitEmptyDomain(ExitBB);
668
669 // If the exit domain is not yet created we set it otherwise we "add" the
670 // current domain.
671 ExitDomain =
672 !ExitDomain.is_null() ? AdjustedDomain.unite(ExitDomain) : AdjustedDomain;
673
674 // Initialize the invalid domain.
675 InvalidDomainMap[ExitBB] = ExitDomain.empty(ExitDomain.get_space());
676
677 FinishedExitBlocks.insert(ExitBB);
678}
679
682 // If @p BB is the ScopEntry we are done
683 if (scop->getRegion().getEntry() == BB)
684 return isl::set::universe(Domain.get_space());
685
686 // The region info of this function.
687 auto &RI = *scop->getRegion().getRegionInfo();
688
689 Loop *BBLoop = getFirstNonBoxedLoopFor(BB, LI, scop->getBoxedLoops());
690
691 // A domain to collect all predecessor domains, thus all conditions under
692 // which the block is executed. To this end we start with the empty domain.
693 isl::set PredDom = isl::set::empty(Domain.get_space());
694
695 // Set of regions of which the entry block domain has been propagated to BB.
696 // all predecessors inside any of the regions can be skipped.
697 SmallPtrSet<Region *, 8> PropagatedRegions;
698
699 for (auto *PredBB : predecessors(BB)) {
700 // Skip backedges.
701 if (DT.dominates(BB, PredBB))
702 continue;
703
704 // If the predecessor is in a region we used for propagation we can skip it.
705 auto PredBBInRegion = [PredBB](Region *PR) { return PR->contains(PredBB); };
706 if (llvm::any_of(PropagatedRegions, PredBBInRegion)) {
707 continue;
708 }
709
710 // Check if there is a valid region we can use for propagation, thus look
711 // for a region that contains the predecessor and has @p BB as exit block.
712 // FIXME: This was an side-effect-free (and possibly infinite) loop when
713 // committed and seems not to be needed.
714 auto *PredR = RI.getRegionFor(PredBB);
715 while (PredR->getExit() != BB && !PredR->contains(BB))
716 PredR = PredR->getParent();
717
718 // If a valid region for propagation was found use the entry of that region
719 // for propagation, otherwise the PredBB directly.
720 if (PredR->getExit() == BB) {
721 PredBB = PredR->getEntry();
722 PropagatedRegions.insert(PredR);
723 }
724
725 isl::set PredBBDom = scop->getDomainConditions(PredBB);
726 Loop *PredBBLoop =
727 getFirstNonBoxedLoopFor(PredBB, LI, scop->getBoxedLoops());
728 PredBBDom = adjustDomainDimensions(PredBBDom, PredBBLoop, BBLoop);
729 PredDom = PredDom.unite(PredBBDom);
730 }
731
732 return PredDom;
733}
734
736 Loop *L, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
737 int LoopDepth = scop->getRelativeLoopDepth(L);
738 assert(LoopDepth >= 0 && "Loop in region should have at least depth one");
739
740 BasicBlock *HeaderBB = L->getHeader();
741 assert(scop->isDomainDefined(HeaderBB));
742 isl::set &HeaderBBDom = scop->getOrInitEmptyDomain(HeaderBB);
743
744 isl::map NextIterationMap =
745 createNextIterationMap(HeaderBBDom.get_space(), LoopDepth);
746
747 isl::set UnionBackedgeCondition = HeaderBBDom.empty(HeaderBBDom.get_space());
748
749 SmallVector<BasicBlock *, 4> LatchBlocks;
750 L->getLoopLatches(LatchBlocks);
751
752 for (BasicBlock *LatchBB : LatchBlocks) {
753 // If the latch is only reachable via error statements we skip it.
754 if (!scop->isDomainDefined(LatchBB))
755 continue;
756
757 isl::set LatchBBDom = scop->getDomainConditions(LatchBB);
758
759 isl::set BackedgeCondition;
760
761 Instruction *TI = LatchBB->getTerminator();
762 if (isa<UncondBrInst>(TI))
763 BackedgeCondition = LatchBBDom;
764 else if (auto *BI = dyn_cast<CondBrInst>(TI)) {
765 SmallVector<isl_set *, 8> ConditionSets;
766 int idx = BI->getSuccessor(0) != HeaderBB;
767 if (!buildConditionSets(LatchBB, TI, L, LatchBBDom.get(),
768 InvalidDomainMap, ConditionSets,
769 /*IsInsideDomain=*/false))
770 return false;
771
772 // Free the non back edge condition set as we do not need it.
773 isl_set_free(ConditionSets[1 - idx]);
774
775 BackedgeCondition = isl::manage(ConditionSets[idx]);
776 } else
777 llvm_unreachable("Only branch instructions allowed in loop latches");
778
779 int LatchLoopDepth = scop->getRelativeLoopDepth(LI.getLoopFor(LatchBB));
780 assert(LatchLoopDepth >= LoopDepth);
781 BackedgeCondition = BackedgeCondition.project_out(
782 isl::dim::set, LoopDepth + 1, LatchLoopDepth - LoopDepth);
783 UnionBackedgeCondition = UnionBackedgeCondition.unite(BackedgeCondition);
784 }
785
786 isl::map ForwardMap = ForwardMap.lex_le(HeaderBBDom.get_space());
787 for (int i = 0; i < LoopDepth; i++)
788 ForwardMap = ForwardMap.equate(isl::dim::in, i, isl::dim::out, i);
789
790 isl::set UnionBackedgeConditionComplement =
791 UnionBackedgeCondition.complement();
792 UnionBackedgeConditionComplement =
793 UnionBackedgeConditionComplement.lower_bound_si(isl::dim::set, LoopDepth,
794 0);
795 UnionBackedgeConditionComplement =
796 UnionBackedgeConditionComplement.apply(ForwardMap);
797 HeaderBBDom = HeaderBBDom.subtract(UnionBackedgeConditionComplement);
798 HeaderBBDom = HeaderBBDom.apply(NextIterationMap);
799
800 auto Parts = partitionSetParts(HeaderBBDom, LoopDepth);
801 HeaderBBDom = Parts.second;
802
803 // Check if there is a <nsw> tagged AddRec for this loop and if so do not
804 // require a runtime check. The assumption is already implied by the <nsw>
805 // tag.
806 bool RequiresRTC = !scop->hasNSWAddRecForLoop(L);
807
808 isl::set UnboundedCtx = Parts.first.params();
810 HeaderBB->getTerminator()->getDebugLoc(), AS_RESTRICTION,
811 nullptr, RequiresRTC);
812 return true;
813}
814
816 DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses;
817
818 const InvariantLoadsSetTy &RIL = scop->getRequiredInvariantLoads();
819 for (LoadInst *LInst : RIL) {
820 const SCEV *PointerSCEV = SE.getSCEV(LInst->getPointerOperand());
821
822 Type *Ty = LInst->getType();
823 LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)];
824 if (ClassRep) {
825 scop->addInvariantLoadMapping(LInst, ClassRep);
826 continue;
827 }
828
829 ClassRep = LInst;
830 scop->addInvariantEquivClass(
831 InvariantEquivClassTy{PointerSCEV, MemoryAccessList(), {}, Ty});
832 }
833}
834
836 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
837 bool IsOnlyNonAffineRegion = scop->isNonAffineSubRegion(R);
838 auto *EntryBB = R->getEntry();
839 auto *L = IsOnlyNonAffineRegion ? nullptr : LI.getLoopFor(EntryBB);
840 int LD = scop->getRelativeLoopDepth(L);
841 auto *S =
842 isl_set_universe(isl_space_set_alloc(scop->getIslCtx().get(), 0, LD + 1));
843
844 InvalidDomainMap[EntryBB] = isl::manage(isl_set_empty(isl_set_get_space(S)));
846 scop->setDomain(EntryBB, Domain);
847
848 if (IsOnlyNonAffineRegion)
849 return !containsErrorBlock(R->getNode(), *R, &SD);
850
851 if (!buildDomainsWithBranchConstraints(R, InvalidDomainMap))
852 return false;
853
854 if (!propagateDomainConstraints(R, InvalidDomainMap))
855 return false;
856
857 // Error blocks and blocks dominated by them have been assumed to never be
858 // executed. Representing them in the Scop does not add any value. In fact,
859 // it is likely to cause issues during construction of the ScopStmts. The
860 // contents of error blocks have not been verified to be expressible and
861 // will cause problems when building up a ScopStmt for them.
862 // Furthermore, basic blocks dominated by error blocks may reference
863 // instructions in the error block which, if the error block is not modeled,
864 // can themselves not be constructed properly. To this end we will replace
865 // the domains of error blocks and those only reachable via error blocks
866 // with an empty set. Additionally, we will record for each block under which
867 // parameter combination it would be reached via an error block in its
868 // InvalidDomain. This information is needed during load hoisting.
869 if (!propagateInvalidStmtDomains(R, InvalidDomainMap))
870 return false;
871
872 return true;
873}
874
876 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
877 // To create the domain for each block in R we iterate over all blocks and
878 // subregions in R and propagate the conditions under which the current region
879 // element is executed. To this end we iterate in reverse post order over R as
880 // it ensures that we first visit all predecessors of a region node (either a
881 // basic block or a subregion) before we visit the region node itself.
882 // Initially, only the domain for the SCoP region entry block is set and from
883 // there we propagate the current domain to all successors, however we add the
884 // condition that the successor is actually executed next.
885 // As we are only interested in non-loop carried constraints here we can
886 // simply skip loop back edges.
887
888 SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks;
889 ReversePostOrderTraversal<Region *> RTraversal(R);
890 for (auto *RN : RTraversal) {
891 // Recurse for affine subregions but go on for basic blocks and non-affine
892 // subregions.
893 if (RN->isSubRegion()) {
894 Region *SubRegion = RN->getNodeAs<Region>();
895 if (!scop->isNonAffineSubRegion(SubRegion)) {
896 if (!buildDomainsWithBranchConstraints(SubRegion, InvalidDomainMap))
897 return false;
898 continue;
899 }
900 }
901
902 if (containsErrorBlock(RN, scop->getRegion(), &SD))
903 scop->notifyErrorBlock();
904 ;
905
906 BasicBlock *BB = getRegionNodeBasicBlock(RN);
907 Instruction *TI = BB->getTerminator();
908
909 if (isa<UnreachableInst>(TI))
910 continue;
911
912 if (!scop->isDomainDefined(BB))
913 continue;
914 isl::set Domain = scop->getDomainConditions(BB);
915
916 scop->updateMaxLoopDepth(unsignedFromIslSize(Domain.tuple_dim()));
917
918 auto *BBLoop = getRegionNodeLoop(RN, LI);
919 // Propagate the domain from BB directly to blocks that have a superset
920 // domain, at the moment only region exit nodes of regions that start in BB.
921 propagateDomainConstraintsToRegionExit(BB, BBLoop, FinishedExitBlocks,
922 InvalidDomainMap);
923
924 // If all successors of BB have been set a domain through the propagation
925 // above we do not need to build condition sets but can just skip this
926 // block. However, it is important to note that this is a local property
927 // with regards to the region @p R. To this end FinishedExitBlocks is a
928 // local variable.
929 auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) {
930 return FinishedExitBlocks.count(SuccBB);
931 };
932 if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit))
933 continue;
934
935 // Build the condition sets for the successor nodes of the current region
936 // node. If it is a non-affine subregion we will always execute the single
937 // exit node, hence the single entry node domain is the condition set. For
938 // basic blocks we use the helper function buildConditionSets.
939 SmallVector<isl_set *, 8> ConditionSets;
940 if (RN->isSubRegion())
941 ConditionSets.push_back(Domain.copy());
942 else if (!buildConditionSets(BB, TI, BBLoop, Domain.get(), InvalidDomainMap,
943 ConditionSets, /*IsInsideDomain=*/false))
944 return false;
945
946 // Now iterate over the successors and set their initial domain based on
947 // their condition set. We skip back edges here and have to be careful when
948 // we leave a loop not to keep constraints over a dimension that doesn't
949 // exist anymore.
950 assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size());
951 for (unsigned u = 0, e = ConditionSets.size(); u < e; u++) {
952 isl::set CondSet = isl::manage(ConditionSets[u]);
953 BasicBlock *SuccBB = getRegionNodeSuccessor(RN, TI, u);
954
955 // Skip blocks outside the region.
956 if (!scop->contains(SuccBB))
957 continue;
958
959 // If we propagate the domain of some block to "SuccBB" we do not have to
960 // adjust the domain.
961 if (FinishedExitBlocks.count(SuccBB))
962 continue;
963
964 // Skip back edges.
965 if (DT.dominates(SuccBB, BB))
966 continue;
967
968 Loop *SuccBBLoop =
969 getFirstNonBoxedLoopFor(SuccBB, LI, scop->getBoxedLoops());
970
971 CondSet = adjustDomainDimensions(CondSet, BBLoop, SuccBBLoop);
972
973 // Set the domain for the successor or merge it with an existing domain in
974 // case there are multiple paths (without loop back edges) to the
975 // successor block.
976 isl::set &SuccDomain = scop->getOrInitEmptyDomain(SuccBB);
977
978 if (!SuccDomain.is_null()) {
979 SuccDomain = SuccDomain.unite(CondSet).coalesce();
980 } else {
981 // Initialize the invalid domain.
982 InvalidDomainMap[SuccBB] = CondSet.empty(CondSet.get_space());
983 SuccDomain = CondSet;
984 }
985
986 SuccDomain = SuccDomain.detect_equalities();
987
988 // Check if the maximal number of domain disjunctions was reached.
989 // In case this happens we will clean up and bail.
991 continue;
992
993 scop->invalidate(COMPLEXITY, DebugLoc());
994 while (++u < ConditionSets.size())
995 isl_set_free(ConditionSets[u]);
996 return false;
997 }
998 }
999
1000 return true;
1001}
1002
1004 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
1005 ReversePostOrderTraversal<Region *> RTraversal(R);
1006 for (auto *RN : RTraversal) {
1007
1008 // Recurse for affine subregions but go on for basic blocks and non-affine
1009 // subregions.
1010 if (RN->isSubRegion()) {
1011 Region *SubRegion = RN->getNodeAs<Region>();
1012 if (!scop->isNonAffineSubRegion(SubRegion)) {
1013 propagateInvalidStmtDomains(SubRegion, InvalidDomainMap);
1014 continue;
1015 }
1016 }
1017
1018 bool ContainsErrorBlock = containsErrorBlock(RN, scop->getRegion(), &SD);
1019 BasicBlock *BB = getRegionNodeBasicBlock(RN);
1020 isl::set &Domain = scop->getOrInitEmptyDomain(BB);
1021 assert(!Domain.is_null() && "Cannot propagate a nullptr");
1022
1023 isl::set InvalidDomain = InvalidDomainMap[BB];
1024
1025 bool IsInvalidBlock = ContainsErrorBlock || Domain.is_subset(InvalidDomain);
1026
1027 if (!IsInvalidBlock) {
1028 InvalidDomain = InvalidDomain.intersect(Domain);
1029 } else {
1030 InvalidDomain = Domain;
1031 isl::set DomPar = Domain.params();
1033 BB->getTerminator()->getDebugLoc(), AS_RESTRICTION);
1034 Domain = isl::set::empty(Domain.get_space());
1035 }
1036
1037 if (InvalidDomain.is_empty()) {
1038 InvalidDomainMap[BB] = InvalidDomain;
1039 continue;
1040 }
1041
1042 auto *BBLoop = getRegionNodeLoop(RN, LI);
1043 auto *TI = BB->getTerminator();
1044 unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors();
1045 for (unsigned u = 0; u < NumSuccs; u++) {
1046 auto *SuccBB = getRegionNodeSuccessor(RN, TI, u);
1047
1048 // Skip successors outside the SCoP.
1049 if (!scop->contains(SuccBB))
1050 continue;
1051
1052 // Skip backedges.
1053 if (DT.dominates(SuccBB, BB))
1054 continue;
1055
1056 Loop *SuccBBLoop =
1057 getFirstNonBoxedLoopFor(SuccBB, LI, scop->getBoxedLoops());
1058
1059 auto AdjustedInvalidDomain =
1060 adjustDomainDimensions(InvalidDomain, BBLoop, SuccBBLoop);
1061
1062 isl::set SuccInvalidDomain = InvalidDomainMap[SuccBB];
1063 SuccInvalidDomain = SuccInvalidDomain.unite(AdjustedInvalidDomain);
1064 SuccInvalidDomain = SuccInvalidDomain.coalesce();
1065
1066 InvalidDomainMap[SuccBB] = SuccInvalidDomain;
1067
1068 // Check if the maximal number of domain disjunctions was reached.
1069 // In case this happens we will bail.
1070 if (unsignedFromIslSize(SuccInvalidDomain.n_basic_set()) <
1072 continue;
1073
1074 InvalidDomainMap.erase(BB);
1075 scop->invalidate(COMPLEXITY, TI->getDebugLoc(), TI->getParent());
1076 return false;
1077 }
1078
1079 InvalidDomainMap[BB] = InvalidDomain;
1080 }
1081
1082 return true;
1083}
1084
1086 Region *NonAffineSubRegion,
1087 bool IsExitBlock) {
1088 // PHI nodes that are in the exit block of the region, hence if IsExitBlock is
1089 // true, are not modeled as ordinary PHI nodes as they are not part of the
1090 // region. However, we model the operands in the predecessor blocks that are
1091 // part of the region as regular scalar accesses.
1092
1093 // If we can synthesize a PHI we can skip it, however only if it is in
1094 // the region. If it is not it can only be in the exit block of the region.
1095 // In this case we model the operands but not the PHI itself.
1096 auto *Scope = LI.getLoopFor(PHI->getParent());
1097 if (!IsExitBlock && canSynthesize(PHI, *scop, &SE, Scope))
1098 return;
1099
1100 // PHI nodes are modeled as if they had been demoted prior to the SCoP
1101 // detection. Hence, the PHI is a load of a new memory location in which the
1102 // incoming value was written at the end of the incoming basic block.
1103 bool OnlyNonAffineSubRegionOperands = true;
1104 for (unsigned u = 0; u < PHI->getNumIncomingValues(); u++) {
1105 Value *Op = PHI->getIncomingValue(u);
1106 BasicBlock *OpBB = PHI->getIncomingBlock(u);
1107 ScopStmt *OpStmt = scop->getIncomingStmtFor(PHI->getOperandUse(u));
1108
1109 // Do not build PHI dependences inside a non-affine subregion, but make
1110 // sure that the necessary scalar values are still made available.
1111 if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB)) {
1112 auto *OpInst = dyn_cast<Instruction>(Op);
1113 if (!OpInst || !NonAffineSubRegion->contains(OpInst))
1114 ensureValueRead(Op, OpStmt);
1115 continue;
1116 }
1117
1118 OnlyNonAffineSubRegionOperands = false;
1119 ensurePHIWrite(PHI, OpStmt, OpBB, Op, IsExitBlock);
1120 }
1121
1122 if (!OnlyNonAffineSubRegionOperands && !IsExitBlock) {
1123 addPHIReadAccess(PHIStmt, PHI);
1124 }
1125}
1126
1128 Instruction *Inst) {
1129 assert(!isa<PHINode>(Inst));
1130
1131 // Pull-in required operands.
1132 for (Use &Op : Inst->operands())
1133 ensureValueRead(Op.get(), UserStmt);
1134}
1135
1136// Create a sequence of two schedules. Either argument may be null and is
1137// interpreted as the empty schedule. Can also return null if both schedules are
1138// empty.
1140 if (Prev.is_null())
1141 return Succ;
1142 if (Succ.is_null())
1143 return Prev;
1144
1145 return Prev.sequence(Succ);
1146}
1147
1148// Create an isl_multi_union_aff that defines an identity mapping from the
1149// elements of USet to their N-th dimension.
1150//
1151// # Example:
1152//
1153// Domain: { A[i,j]; B[i,j,k] }
1154// N: 1
1155//
1156// Resulting Mapping: { {A[i,j] -> [(j)]; B[i,j,k] -> [(j)] }
1157//
1158// @param USet A union set describing the elements for which to generate a
1159// mapping.
1160// @param N The dimension to map to.
1161// @returns A mapping from USet to its N-th dimension.
1163 assert(!USet.is_null());
1164 assert(!USet.is_empty());
1165
1166 auto Result = isl::union_pw_multi_aff::empty(USet.get_space());
1167
1168 for (isl::set S : USet.get_set_list()) {
1169 unsigned Dim = unsignedFromIslSize(S.tuple_dim());
1170 assert(Dim >= N);
1171 auto PMA = isl::pw_multi_aff::project_out_map(S.get_space(), isl::dim::set,
1172 N, Dim - N);
1173 if (N > 1)
1174 PMA = PMA.drop_dims(isl::dim::out, 0, N - 1);
1175
1176 Result = Result.add_pw_multi_aff(PMA);
1177 }
1178
1180}
1181
1183 Loop *L = getLoopSurroundingScop(*scop, LI);
1184 LoopStackTy LoopStack({LoopStackElementTy(L, {}, 0)});
1185 buildSchedule(scop->getRegion().getNode(), LoopStack);
1186 assert(LoopStack.size() == 1 && LoopStack.back().L == L);
1187 scop->setScheduleTree(LoopStack[0].Schedule);
1188}
1189
1190/// To generate a schedule for the elements in a Region we traverse the Region
1191/// in reverse-post-order and add the contained RegionNodes in traversal order
1192/// to the schedule of the loop that is currently at the top of the LoopStack.
1193/// For loop-free codes, this results in a correct sequential ordering.
1194///
1195/// Example:
1196/// bb1(0)
1197/// / \.
1198/// bb2(1) bb3(2)
1199/// \ / \.
1200/// bb4(3) bb5(4)
1201/// \ /
1202/// bb6(5)
1203///
1204/// Including loops requires additional processing. Whenever a loop header is
1205/// encountered, the corresponding loop is added to the @p LoopStack. Starting
1206/// from an empty schedule, we first process all RegionNodes that are within
1207/// this loop and complete the sequential schedule at this loop-level before
1208/// processing about any other nodes. To implement this
1209/// loop-nodes-first-processing, the reverse post-order traversal is
1210/// insufficient. Hence, we additionally check if the traversal yields
1211/// sub-regions or blocks that are outside the last loop on the @p LoopStack.
1212/// These region-nodes are then queue and only traverse after the all nodes
1213/// within the current loop have been processed.
1214void ScopBuilder::buildSchedule(Region *R, LoopStackTy &LoopStack) {
1215 Loop *OuterScopLoop = getLoopSurroundingScop(*scop, LI);
1216
1217 ReversePostOrderTraversal<Region *> RTraversal(R);
1218 std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end());
1219 std::deque<RegionNode *> DelayList;
1220 bool LastRNWaiting = false;
1221
1222 // Iterate over the region @p R in reverse post-order but queue
1223 // sub-regions/blocks iff they are not part of the last encountered but not
1224 // completely traversed loop. The variable LastRNWaiting is a flag to indicate
1225 // that we queued the last sub-region/block from the reverse post-order
1226 // iterator. If it is set we have to explore the next sub-region/block from
1227 // the iterator (if any) to guarantee progress. If it is not set we first try
1228 // the next queued sub-region/blocks.
1229 while (!WorkList.empty() || !DelayList.empty()) {
1230 RegionNode *RN;
1231
1232 if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) {
1233 RN = WorkList.front();
1234 WorkList.pop_front();
1235 LastRNWaiting = false;
1236 } else {
1237 RN = DelayList.front();
1238 DelayList.pop_front();
1239 }
1240
1241 Loop *L = getRegionNodeLoop(RN, LI);
1242 if (!scop->contains(L))
1243 L = OuterScopLoop;
1244
1245 Loop *LastLoop = LoopStack.back().L;
1246 if (LastLoop != L) {
1247 if (LastLoop && !LastLoop->contains(L)) {
1248 LastRNWaiting = true;
1249 DelayList.push_back(RN);
1250 continue;
1251 }
1252 LoopStack.push_back({L, {}, 0});
1253 }
1254 buildSchedule(RN, LoopStack);
1255 }
1256}
1257
1258void ScopBuilder::buildSchedule(RegionNode *RN, LoopStackTy &LoopStack) {
1259 if (RN->isSubRegion()) {
1260 auto *LocalRegion = RN->getNodeAs<Region>();
1261 if (!scop->isNonAffineSubRegion(LocalRegion)) {
1262 buildSchedule(LocalRegion, LoopStack);
1263 return;
1264 }
1265 }
1266
1267 assert(LoopStack.rbegin() != LoopStack.rend());
1268 auto LoopData = LoopStack.rbegin();
1269 LoopData->NumBlocksProcessed += getNumBlocksInRegionNode(RN);
1270
1271 for (auto *Stmt : scop->getStmtListFor(RN)) {
1272 isl::union_set UDomain{Stmt->getDomain()};
1273 auto StmtSchedule = isl::schedule::from_domain(UDomain);
1274 LoopData->Schedule = combineInSequence(LoopData->Schedule, StmtSchedule);
1275 }
1276
1277 // Check if we just processed the last node in this loop. If we did, finalize
1278 // the loop by:
1279 //
1280 // - adding new schedule dimensions
1281 // - folding the resulting schedule into the parent loop schedule
1282 // - dropping the loop schedule from the LoopStack.
1283 //
1284 // Then continue to check surrounding loops, which might also have been
1285 // completed by this node.
1286 size_t Dimension = LoopStack.size();
1287 while (LoopData->L &&
1288 LoopData->NumBlocksProcessed == getNumBlocksInLoop(LoopData->L)) {
1289 isl::schedule Schedule = LoopData->Schedule;
1290 auto NumBlocksProcessed = LoopData->NumBlocksProcessed;
1291
1292 assert(std::next(LoopData) != LoopStack.rend());
1293 Loop *L = LoopData->L;
1294 ++LoopData;
1295 --Dimension;
1296
1297 if (!Schedule.is_null()) {
1298 isl::union_set Domain = Schedule.get_domain();
1300 Schedule = Schedule.insert_partial_schedule(MUPA);
1301
1303 /// If any of the loops has a disable_nonforced heuristic, mark the
1304 /// entire SCoP as such. The ISL rescheduler can only reschedule the
1305 /// SCoP in its entirety.
1306 /// TODO: ScopDetection could avoid including such loops or warp them as
1307 /// boxed loop. It still needs to pass-through loop with user-defined
1308 /// metadata.
1309 scop->markDisableHeuristics();
1310 }
1311
1312 // It is easier to insert the marks here that do it retroactively.
1313 isl::id IslLoopId = createIslLoopAttr(scop->getIslCtx(), L);
1314 if (!IslLoopId.is_null())
1315 Schedule =
1316 Schedule.get_root().child(0).insert_mark(IslLoopId).get_schedule();
1317
1318 LoopData->Schedule = combineInSequence(LoopData->Schedule, Schedule);
1319 }
1320
1321 LoopData->NumBlocksProcessed += NumBlocksProcessed;
1322 }
1323 // Now pop all loops processed up there from the LoopStack
1324 LoopStack.erase(LoopStack.begin() + Dimension, LoopStack.end());
1325}
1326
1328 // Check for uses of this instruction outside the scop. Because we do not
1329 // iterate over such instructions and therefore did not "ensure" the existence
1330 // of a write, we must determine such use here.
1331 if (scop->isEscaping(Inst))
1332 ensureValueWrite(Inst);
1333}
1334
1336 for (auto &AS : llvm::reverse(RecordedAssumptions)) {
1337 isl::set S = AS.Set;
1338 AssumptionSign Sign = AS.Sign;
1339
1340 // Assumptions/restructions apply only when the code containing it is
1341 // actually executed
1342 if (AS.BB && !AS.Set.is_params()) {
1343 // If the domain was deleted the assumptions are void.
1344 isl::set Dom = scop->getDomainConditions(AS.BB);
1345 if (Dom.is_null())
1346 continue;
1347
1348 // If a basic block was given use its domain to simplify the assumption.
1349 // In case of restrictions we know they only have to hold on the domain,
1350 // thus we can intersect them with the domain of the block. However, for
1351 // assumptions the domain has to imply them, thus:
1352 // _ _____
1353 // Dom => S <==> A v B <==> A - B
1354 //
1355 // To avoid the complement we will register A - B as a restriction not an
1356 // assumption.
1357 if (Sign == AS_RESTRICTION) {
1358 S = std::move(S).intersect(std::move(Dom));
1359 } else {
1360 S = std::move(Dom).subtract(std::move(S));
1361 Sign = AS_RESTRICTION;
1362 }
1363 }
1364
1365 isl::set PSet = S.params();
1366#ifndef NDEBUG
1367 // .params() is an overapproximation; if an AS_ASSUMPTION says
1368 //
1369 // [p] -> { [i] : p == 1 and i == 1 }
1370 //
1371 // the params space will be
1372 //
1373 // [p] -> { [] : }
1374 //
1375 // (because there is at least one element with p == 1 in the set);
1376 // if RequiresRTC is true, we will not include a check for p at all. The
1377 // code above adds the domain constraints which don't need (and should not)
1378 // to checked, but the actual assumption/restructions should have access to
1379 // the parameters only.
1380 if (AS.RequiresRTC && Sign == AS_RESTRICTION) {
1381 // Overapproximation is OK in this case: failing more RTC checks than
1382 // strictly necessary (Underapproximation of RTC-checked AS_ASSUMPTIONs
1383 // would be as well)
1384 } else if (!AS.RequiresRTC && Sign == AS_ASSUMPTION) {
1385 // Overapproximation of defined behavior is OK: Only too optimistic
1386 // assumptions could lead to invalid transformations; the universe set
1387 // would be equivalent to "no assumptions" (Underapproximation of
1388 // undefined behaviour would be as well)
1389 } else {
1390 isl::set ReconstructedSet =
1391 S.get_space().universe_set().intersect_params(PSet);
1392 assert(ReconstructedSet.is_subset(S) &&
1393 "Must not overapproximate assumptions/restructions");
1394 }
1395#endif
1396
1397 scop->addAssumption(AS.Kind, std::move(PSet), AS.Loc, Sign, AS.BB,
1398 AS.RequiresRTC);
1399 }
1400}
1401
1403 AssumptionCache &AC, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
1404 for (auto &Assumption : AC.assumptions()) {
1405 auto *CI = dyn_cast_or_null<CallInst>(Assumption);
1406 if (!CI || CI->arg_size() != 1)
1407 continue;
1408
1409 bool InScop = scop->contains(CI);
1410 if (!InScop && !scop->isDominatedBy(DT, CI->getParent()))
1411 continue;
1412
1413 auto *L = LI.getLoopFor(CI->getParent());
1414 auto *Val = CI->getArgOperand(0);
1415 ParameterSetTy DetectedParams;
1416 auto &R = scop->getRegion();
1417 if (!isAffineConstraint(Val, &R, L, SE, DetectedParams)) {
1418 ORE.emit(
1419 OptimizationRemarkAnalysis(DEBUG_TYPE, "IgnoreUserAssumption", CI)
1420 << "Non-affine user assumption ignored.");
1421 continue;
1422 }
1423
1424 // Collect all newly introduced parameters.
1425 ParameterSetTy NewParams;
1426 for (auto *Param : DetectedParams) {
1427 Param = extractConstantFactor(Param, SE).second;
1428 Param = scop->getRepresentingInvariantLoadSCEV(Param);
1429 if (scop->isParam(Param))
1430 continue;
1431 NewParams.insert(Param);
1432 }
1433
1434 size_t NumAssumptions = RecordedAssumptions.size();
1435 SmallVector<isl_set *, 2> ConditionSets;
1436 auto *TI = InScop ? CI->getParent()->getTerminator() : nullptr;
1437 BasicBlock *BB = InScop ? CI->getParent() : R.getEntry();
1438 auto *Dom = InScop ? isl_set_copy(scop->getDomainConditions(BB).get())
1439 : isl_set_copy(scop->getContext().get());
1440 assert(Dom && "Cannot propagate a nullptr.");
1441 bool Valid = buildConditionSets(BB, Val, TI, L, Dom, InvalidDomainMap,
1442 ConditionSets);
1443 isl_set_free(Dom);
1444
1445 if (!Valid)
1446 continue;
1447
1448 isl_set *AssumptionCtx = nullptr;
1449 if (InScop) {
1450 AssumptionCtx = isl_set_complement(isl_set_params(ConditionSets[1]));
1451 isl_set_free(ConditionSets[0]);
1452 } else {
1453 AssumptionCtx = isl_set_complement(ConditionSets[1]);
1454 AssumptionCtx = isl_set_intersect(AssumptionCtx, ConditionSets[0]);
1455 }
1456
1457 // Project out newly introduced parameters as they are not otherwise useful.
1458 if (!NewParams.empty()) {
1459 for (isl_size u = 0; u < isl_set_n_param(AssumptionCtx); u++) {
1460 auto *Id = isl_set_get_dim_id(AssumptionCtx, isl_dim_param, u);
1461 auto *Param = static_cast<const SCEV *>(isl_id_get_user(Id));
1462 isl_id_free(Id);
1463
1464 if (!NewParams.count(Param))
1465 continue;
1466
1467 AssumptionCtx =
1468 isl_set_project_out(AssumptionCtx, isl_dim_param, u--, 1);
1469 }
1470 }
1471 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "UserAssumption", CI)
1472 << "Use user assumption: "
1473 << stringFromIslObj(AssumptionCtx, "null"));
1474
1475 // scop->setContext is used to gist AssumedContext and InvalidContext. Both
1476 // add RTCs, so using setContext would remove the RTC that would ensure the
1477 // correctness of AssumptionCtx. Using DefinedBehaviorContext which does not
1478 // gist the other contexts.
1479 // TODO: Use recordAssumption() for adding context/assumptions
1480 if (NumAssumptions == RecordedAssumptions.size()) {
1481 isl::set newContext =
1482 scop->getContext().intersect(isl::manage(AssumptionCtx));
1483 scop->setContext(newContext);
1484 } else {
1485 scop->intersectDefinedBehavior(isl::manage(AssumptionCtx), AS_ASSUMPTION);
1486 }
1487 }
1488}
1489
1491 // Memory builtins are not considered in this function.
1492 if (!Inst.isLoad() && !Inst.isStore())
1493 return false;
1494
1495 Value *Val = Inst.getValueOperand();
1496 Type *ElementType = Val->getType();
1497 Value *Address = Inst.getPointerOperand();
1498 const SCEV *AccessFunction =
1499 SE.getSCEVAtScope(Address, LI.getLoopFor(Inst->getParent()));
1500 const SCEVUnknown *BasePointer =
1501 dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFunction));
1502 enum MemoryAccess::AccessType AccType =
1503 isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
1504
1505 if (auto *BitCast = dyn_cast<BitCastInst>(Address))
1506 Address = BitCast->getOperand(0);
1507
1508 auto *GEP = dyn_cast<GetElementPtrInst>(Address);
1509 if (!GEP || DL.getTypeAllocSize(GEP->getResultElementType()) !=
1510 DL.getTypeAllocSize(ElementType))
1511 return false;
1512
1513 SmallVector<const SCEV *, 4> Subscripts;
1514 SmallVector<const SCEV *, 4> Sizes;
1515 getIndexExpressionsFromGEP(SE, GEP, Subscripts, Sizes);
1516 auto *BasePtr = GEP->getOperand(0);
1517
1518 if (auto *BasePtrCast = dyn_cast<BitCastInst>(BasePtr))
1519 BasePtr = BasePtrCast->getOperand(0);
1520
1521 // Check for identical base pointers to ensure that we do not miss index
1522 // offsets that have been added before this GEP is applied.
1523 if (BasePtr != BasePointer->getValue())
1524 return false;
1525
1526 const InvariantLoadsSetTy &ScopRIL = scop->getRequiredInvariantLoads();
1527
1528 Loop *SurroundingLoop = Stmt->getSurroundingLoop();
1529 for (auto *Subscript : Subscripts) {
1530 InvariantLoadsSetTy AccessILS;
1531 if (!isAffineExpr(&scop->getRegion(), SurroundingLoop, Subscript, SE,
1532 &AccessILS))
1533 return false;
1534
1535 for (LoadInst *LInst : AccessILS)
1536 if (!ScopRIL.count(LInst))
1537 return false;
1538 }
1539
1540 if (Sizes.empty())
1541 return false;
1542
1543 std::vector<const SCEV *> SizesSCEV;
1544 SizesSCEV.push_back(nullptr);
1545 SizesSCEV.insert(SizesSCEV.end(), Sizes.begin(), Sizes.end());
1546
1547 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1548 true, Subscripts, SizesSCEV, Val);
1549 return true;
1550}
1551
1553 // Memory builtins are not considered by this function.
1554 if (!Inst.isLoad() && !Inst.isStore())
1555 return false;
1556
1557 if (!PollyDelinearize)
1558 return false;
1559
1560 Value *Address = Inst.getPointerOperand();
1561 Value *Val = Inst.getValueOperand();
1562 Type *ElementType = Val->getType();
1563 unsigned ElementSize = DL.getTypeAllocSize(ElementType);
1564 enum MemoryAccess::AccessType AccType =
1565 isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
1566
1567 const SCEV *AccessFunction =
1568 SE.getSCEVAtScope(Address, LI.getLoopFor(Inst->getParent()));
1569 const SCEVUnknown *BasePointer =
1570 dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFunction));
1571
1572 assert(BasePointer && "Could not find base pointer");
1573
1574 auto &InsnToMemAcc = scop->getInsnToMemAccMap();
1575 auto AccItr = InsnToMemAcc.find(Inst);
1576 if (AccItr == InsnToMemAcc.end())
1577 return false;
1578
1579 std::vector<const SCEV *> Sizes = {nullptr};
1580
1581 Sizes.insert(Sizes.end(), AccItr->second.Shape->DelinearizedSizes.begin(),
1582 AccItr->second.Shape->DelinearizedSizes.end());
1583
1584 // In case only the element size is contained in the 'Sizes' array, the
1585 // access does not access a real multi-dimensional array. Hence, we allow
1586 // the normal single-dimensional access construction to handle this.
1587 if (Sizes.size() == 1)
1588 return false;
1589
1590 // Remove the element size. This information is already provided by the
1591 // ElementSize parameter. In case the element size of this access and the
1592 // element size used for delinearization differs the delinearization is
1593 // incorrect. Hence, we invalidate the scop.
1594 //
1595 // TODO: Handle delinearization with differing element sizes.
1596 auto DelinearizedSize =
1597 cast<SCEVConstant>(Sizes.back())->getAPInt().getSExtValue();
1598 Sizes.pop_back();
1599 if (ElementSize != DelinearizedSize)
1600 scop->invalidate(DELINEARIZATION, Inst->getDebugLoc(), Inst->getParent());
1601
1602 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1603 true, AccItr->second.DelinearizedSubscripts, Sizes, Val);
1604 return true;
1605}
1606
1608 auto *MemIntr = dyn_cast_or_null<MemIntrinsic>(Inst);
1609
1610 if (MemIntr == nullptr)
1611 return false;
1612
1613 auto *L = LI.getLoopFor(Inst->getParent());
1614 const SCEV *LengthVal = SE.getSCEVAtScope(MemIntr->getLength(), L);
1615 assert(LengthVal);
1616
1617 // Check if the length val is actually affine or if we overapproximate it
1618 InvariantLoadsSetTy AccessILS;
1619 const InvariantLoadsSetTy &ScopRIL = scop->getRequiredInvariantLoads();
1620
1621 Loop *SurroundingLoop = Stmt->getSurroundingLoop();
1622 bool LengthIsAffine = isAffineExpr(&scop->getRegion(), SurroundingLoop,
1623 LengthVal, SE, &AccessILS);
1624 for (LoadInst *LInst : AccessILS)
1625 if (!ScopRIL.count(LInst))
1626 LengthIsAffine = false;
1627 if (!LengthIsAffine)
1628 LengthVal = nullptr;
1629
1630 auto *DestPtrVal = MemIntr->getDest();
1631 assert(DestPtrVal);
1632
1633 const SCEV *DestAccFunc = SE.getSCEVAtScope(DestPtrVal, L);
1634 assert(DestAccFunc);
1635 // Ignore accesses to "NULL".
1636 // TODO: We could use this to optimize the region further, e.g., intersect
1637 // the context with
1638 // isl_set_complement(isl_set_params(getDomain()))
1639 // as we know it would be undefined to execute this instruction anyway.
1640 if (DestAccFunc->isZero())
1641 return true;
1642
1643 if (auto *U = dyn_cast<SCEVUnknown>(DestAccFunc)) {
1644 if (isa<ConstantPointerNull>(U->getValue()))
1645 return true;
1646 }
1647
1648 auto *DestPtrSCEV = dyn_cast<SCEVUnknown>(SE.getPointerBase(DestAccFunc));
1649 assert(DestPtrSCEV);
1650 DestAccFunc = SE.getMinusSCEV(DestAccFunc, DestPtrSCEV);
1651 addArrayAccess(Stmt, Inst, MemoryAccess::MUST_WRITE, DestPtrSCEV->getValue(),
1652 IntegerType::getInt8Ty(DestPtrVal->getContext()),
1653 LengthIsAffine, {DestAccFunc, LengthVal}, {nullptr},
1654 Inst.getValueOperand());
1655
1656 auto *MemTrans = dyn_cast<MemTransferInst>(MemIntr);
1657 if (!MemTrans)
1658 return true;
1659
1660 auto *SrcPtrVal = MemTrans->getSource();
1661 assert(SrcPtrVal);
1662
1663 const SCEV *SrcAccFunc = SE.getSCEVAtScope(SrcPtrVal, L);
1664 assert(SrcAccFunc);
1665 // Ignore accesses to "NULL".
1666 // TODO: See above TODO
1667 if (SrcAccFunc->isZero())
1668 return true;
1669
1670 auto *SrcPtrSCEV = dyn_cast<SCEVUnknown>(SE.getPointerBase(SrcAccFunc));
1671 assert(SrcPtrSCEV);
1672 SrcAccFunc = SE.getMinusSCEV(SrcAccFunc, SrcPtrSCEV);
1673 addArrayAccess(Stmt, Inst, MemoryAccess::READ, SrcPtrSCEV->getValue(),
1674 IntegerType::getInt8Ty(SrcPtrVal->getContext()),
1675 LengthIsAffine, {SrcAccFunc, LengthVal}, {nullptr},
1676 Inst.getValueOperand());
1677
1678 return true;
1679}
1680
1682 auto *CI = dyn_cast_or_null<CallInst>(Inst);
1683
1684 if (CI == nullptr)
1685 return false;
1686
1687 if (CI->doesNotAccessMemory() || isIgnoredIntrinsic(CI) || isDebugCall(CI))
1688 return true;
1689
1690 const SCEV *AF = SE.getConstant(IntegerType::getInt64Ty(CI->getContext()), 0);
1691 auto *CalledFunction = CI->getCalledFunction();
1692 MemoryEffects ME = AA.getMemoryEffects(CalledFunction);
1693 if (ME.doesNotAccessMemory())
1694 return true;
1695
1696 if (ME.onlyAccessesArgPointees()) {
1697 ModRefInfo ArgMR = ME.getModRef(IRMemLocation::ArgMem);
1698 auto AccType =
1699 !isModSet(ArgMR) ? MemoryAccess::READ : MemoryAccess::MAY_WRITE;
1700 Loop *L = LI.getLoopFor(Inst->getParent());
1701 for (const auto &Arg : CI->args()) {
1702 if (!Arg->getType()->isPointerTy())
1703 continue;
1704
1705 const SCEV *ArgSCEV = SE.getSCEVAtScope(Arg, L);
1706 if (ArgSCEV->isZero())
1707 continue;
1708
1709 if (auto *U = dyn_cast<SCEVUnknown>(ArgSCEV)) {
1710 if (isa<ConstantPointerNull>(U->getValue()))
1711 return true;
1712 }
1713
1714 auto *ArgBasePtr = cast<SCEVUnknown>(SE.getPointerBase(ArgSCEV));
1715 addArrayAccess(Stmt, Inst, AccType, ArgBasePtr->getValue(),
1716 ArgBasePtr->getType(), false, {AF}, {nullptr}, CI);
1717 }
1718 return true;
1719 }
1720
1721 if (ME.onlyReadsMemory()) {
1722 GlobalReads.emplace_back(Stmt, CI);
1723 return true;
1724 }
1725 return false;
1726}
1727
1729 // Memory builtins are not considered by this function.
1730 if (!Inst.isLoad() && !Inst.isStore())
1731 return false;
1732
1733 Value *Address = Inst.getPointerOperand();
1734 Value *Val = Inst.getValueOperand();
1735 Type *ElementType = Val->getType();
1736 enum MemoryAccess::AccessType AccType =
1737 isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
1738
1739 const SCEV *AccessFunction =
1740 SE.getSCEVAtScope(Address, LI.getLoopFor(Inst->getParent()));
1741 const SCEVUnknown *BasePointer =
1742 dyn_cast<SCEVUnknown>(SE.getPointerBase(AccessFunction));
1743
1744 assert(BasePointer && "Could not find base pointer");
1745 AccessFunction = SE.getMinusSCEV(AccessFunction, BasePointer);
1746
1747 // Check if the access depends on a loop contained in a non-affine subregion.
1748 bool isVariantInNonAffineLoop = false;
1749 SetVector<const Loop *> Loops;
1750 findLoops(AccessFunction, Loops);
1751 for (const Loop *L : Loops)
1752 if (Stmt->contains(L)) {
1753 isVariantInNonAffineLoop = true;
1754 break;
1755 }
1756
1757 InvariantLoadsSetTy AccessILS;
1758
1759 Loop *SurroundingLoop = Stmt->getSurroundingLoop();
1760 bool IsAffine = !isVariantInNonAffineLoop &&
1761 isAffineExpr(&scop->getRegion(), SurroundingLoop,
1762 AccessFunction, SE, &AccessILS);
1763
1764 const InvariantLoadsSetTy &ScopRIL = scop->getRequiredInvariantLoads();
1765 for (LoadInst *LInst : AccessILS)
1766 if (!ScopRIL.count(LInst))
1767 IsAffine = false;
1768
1769 if (!IsAffine && AccType == MemoryAccess::MUST_WRITE)
1770 AccType = MemoryAccess::MAY_WRITE;
1771
1772 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1773 IsAffine, {AccessFunction}, {nullptr}, Val);
1774 return true;
1775}
1776
1778 if (buildAccessMemIntrinsic(Inst, Stmt))
1779 return;
1780
1781 if (buildAccessCallInst(Inst, Stmt))
1782 return;
1783
1784 if (buildAccessMultiDimFixed(Inst, Stmt))
1785 return;
1786
1787 if (buildAccessMultiDimParam(Inst, Stmt))
1788 return;
1789
1790 if (buildAccessSingleDim(Inst, Stmt))
1791 return;
1792
1793 llvm_unreachable(
1794 "At least one of the buildAccess functions must handled this access, or "
1795 "ScopDetection should have rejected this SCoP");
1796}
1797
1799 for (auto &Stmt : *scop) {
1800 if (Stmt.isBlockStmt()) {
1801 buildAccessFunctions(&Stmt, *Stmt.getBasicBlock());
1802 continue;
1803 }
1804
1805 Region *R = Stmt.getRegion();
1806 for (BasicBlock *BB : R->blocks())
1807 buildAccessFunctions(&Stmt, *BB, R);
1808 }
1809
1810 // Build write accesses for values that are used after the SCoP.
1811 // The instructions defining them might be synthesizable and therefore not
1812 // contained in any statement, hence we iterate over the original instructions
1813 // to identify all escaping values.
1814 for (BasicBlock *BB : scop->getRegion().blocks()) {
1815 for (Instruction &Inst : *BB)
1817 }
1818}
1819
1820bool ScopBuilder::shouldModelInst(Instruction *Inst, Loop *L) {
1821 return !Inst->isTerminator() && !isIgnoredIntrinsic(Inst) &&
1822 !canSynthesize(Inst, *scop, &SE, L);
1823}
1824
1825/// Generate a name for a statement.
1826///
1827/// @param BB The basic block the statement will represent.
1828/// @param BBIdx The index of the @p BB relative to other BBs/regions.
1829/// @param Count The index of the created statement in @p BB.
1830/// @param IsMain Whether this is the main of all statement for @p BB. If true,
1831/// no suffix will be added.
1832/// @param IsLast Uses a special indicator for the last statement of a BB.
1833static std::string makeStmtName(BasicBlock *BB, long BBIdx, int Count,
1834 bool IsMain, bool IsLast = false) {
1835 std::string Suffix;
1836 if (!IsMain) {
1838 Suffix = '_';
1839 if (IsLast)
1840 Suffix += "last";
1841 else if (Count < 26)
1842 Suffix += 'a' + Count;
1843 else
1844 Suffix += std::to_string(Count);
1845 }
1846 return getIslCompatibleName("Stmt", BB, BBIdx, Suffix, UseInstructionNames);
1847}
1848
1849/// Generate a name for a statement that represents a non-affine subregion.
1850///
1851/// @param R The region the statement will represent.
1852/// @param RIdx The index of the @p R relative to other BBs/regions.
1853static std::string makeStmtName(Region *R, long RIdx) {
1854 return getIslCompatibleName("Stmt", R->getNameStr(), RIdx, "",
1856}
1857
1858void ScopBuilder::buildSequentialBlockStmts(BasicBlock *BB, bool SplitOnStore) {
1859 Loop *SurroundingLoop = LI.getLoopFor(BB);
1860
1861 int Count = 0;
1862 long BBIdx = scop->getNextStmtIdx();
1863 std::vector<Instruction *> Instructions;
1864 for (Instruction &Inst : *BB) {
1865 if (shouldModelInst(&Inst, SurroundingLoop))
1866 Instructions.push_back(&Inst);
1867 if (Inst.getMetadata("polly_split_after") ||
1868 (SplitOnStore && isa<StoreInst>(Inst))) {
1869 std::string Name = makeStmtName(BB, BBIdx, Count, Count == 0);
1870 scop->addScopStmt(BB, Name, SurroundingLoop, Instructions);
1871 Count++;
1872 Instructions.clear();
1873 }
1874 }
1875
1876 std::string Name = makeStmtName(BB, BBIdx, Count, Count == 0);
1877 scop->addScopStmt(BB, Name, SurroundingLoop, Instructions);
1878}
1879
1880/// Is @p Inst an ordered instruction?
1881///
1882/// An unordered instruction is an instruction, such that a sequence of
1883/// unordered instructions can be permuted without changing semantics. Any
1884/// instruction for which this is not always the case is ordered.
1885static bool isOrderedInstruction(Instruction *Inst) {
1886 return Inst->mayHaveSideEffects() || Inst->mayReadOrWriteMemory();
1887}
1888
1889/// Join instructions to the same statement if one uses the scalar result of the
1890/// other.
1891static void joinOperandTree(EquivalenceClasses<Instruction *> &UnionFind,
1892 ArrayRef<Instruction *> ModeledInsts) {
1893 for (Instruction *Inst : ModeledInsts) {
1894 if (isa<PHINode>(Inst))
1895 continue;
1896
1897 for (Use &Op : Inst->operands()) {
1898 Instruction *OpInst = dyn_cast<Instruction>(Op.get());
1899 if (!OpInst)
1900 continue;
1901
1902 // Check if OpInst is in the BB and is a modeled instruction.
1903 if (!UnionFind.contains(OpInst))
1904 continue;
1905
1906 UnionFind.unionSets(Inst, OpInst);
1907 }
1908 }
1909}
1910
1911/// Ensure that the order of ordered instructions does not change.
1912///
1913/// If we encounter an ordered instruction enclosed in instructions belonging to
1914/// a different statement (which might as well contain ordered instructions, but
1915/// this is not tested here), join them.
1916static void
1917joinOrderedInstructions(EquivalenceClasses<Instruction *> &UnionFind,
1918 ArrayRef<Instruction *> ModeledInsts) {
1919 SetVector<Instruction *> SeenLeaders;
1920 for (Instruction *Inst : ModeledInsts) {
1921 if (!isOrderedInstruction(Inst))
1922 continue;
1923
1924 Instruction *Leader = UnionFind.getLeaderValue(Inst);
1925 // Since previous iterations might have merged sets, some items in
1926 // SeenLeaders are not leaders anymore. However, The new leader of
1927 // previously merged instructions must be one of the former leaders of
1928 // these merged instructions.
1929 bool Inserted = SeenLeaders.insert(Leader);
1930 if (Inserted)
1931 continue;
1932
1933 // Merge statements to close holes. Say, we have already seen statements A
1934 // and B, in this order. Then we see an instruction of A again and we would
1935 // see the pattern "A B A". This function joins all statements until the
1936 // only seen occurrence of A.
1937 for (Instruction *Prev : reverse(SeenLeaders)) {
1938 // We are backtracking from the last element until we see Inst's leader
1939 // in SeenLeaders and merge all into one set. Although leaders of
1940 // instructions change during the execution of this loop, it's irrelevant
1941 // as we are just searching for the element that we already confirmed is
1942 // in the list.
1943 if (Prev == Leader)
1944 break;
1945 UnionFind.unionSets(Prev, Leader);
1946 }
1947 }
1948}
1949
1950/// If the BasicBlock has an edge from itself, ensure that the PHI WRITEs for
1951/// the incoming values from this block are executed after the PHI READ.
1952///
1953/// Otherwise it could overwrite the incoming value from before the BB with the
1954/// value for the next execution. This can happen if the PHI WRITE is added to
1955/// the statement with the instruction that defines the incoming value (instead
1956/// of the last statement of the same BB). To ensure that the PHI READ and WRITE
1957/// are in order, we put both into the statement. PHI WRITEs are always executed
1958/// after PHI READs when they are in the same statement.
1959///
1960/// TODO: This is an overpessimization. We only have to ensure that the PHI
1961/// WRITE is not put into a statement containing the PHI itself. That could also
1962/// be done by
1963/// - having all (strongly connected) PHIs in a single statement,
1964/// - unite only the PHIs in the operand tree of the PHI WRITE (because it only
1965/// has a chance of being lifted before a PHI by being in a statement with a
1966/// PHI that comes before in the basic block), or
1967/// - when uniting statements, ensure that no (relevant) PHIs are overtaken.
1968static void joinOrderedPHIs(EquivalenceClasses<Instruction *> &UnionFind,
1969 ArrayRef<Instruction *> ModeledInsts) {
1970 for (Instruction *Inst : ModeledInsts) {
1971 PHINode *PHI = dyn_cast<PHINode>(Inst);
1972 if (!PHI)
1973 continue;
1974
1975 int Idx = PHI->getBasicBlockIndex(PHI->getParent());
1976 if (Idx < 0)
1977 continue;
1978
1979 Instruction *IncomingVal =
1980 dyn_cast<Instruction>(PHI->getIncomingValue(Idx));
1981 if (!IncomingVal)
1982 continue;
1983
1984 UnionFind.unionSets(PHI, IncomingVal);
1985 }
1986}
1987
1989 Loop *L = LI.getLoopFor(BB);
1990
1991 // Extracting out modeled instructions saves us from checking
1992 // shouldModelInst() repeatedly.
1993 SmallVector<Instruction *, 32> ModeledInsts;
1994 EquivalenceClasses<Instruction *> UnionFind;
1995 Instruction *MainInst = nullptr, *MainLeader = nullptr;
1996 for (Instruction &Inst : *BB) {
1997 if (!shouldModelInst(&Inst, L))
1998 continue;
1999 ModeledInsts.push_back(&Inst);
2000 UnionFind.insert(&Inst);
2001
2002 // When a BB is split into multiple statements, the main statement is the
2003 // one containing the 'main' instruction. We select the first instruction
2004 // that is unlikely to be removed (because it has side-effects) as the main
2005 // one. It is used to ensure that at least one statement from the bb has the
2006 // same name as with -polly-stmt-granularity=bb.
2007 if (!MainInst && (isa<StoreInst>(Inst) ||
2008 (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst))))
2009 MainInst = &Inst;
2010 }
2011
2012 joinOperandTree(UnionFind, ModeledInsts);
2013 joinOrderedInstructions(UnionFind, ModeledInsts);
2014 joinOrderedPHIs(UnionFind, ModeledInsts);
2015
2016 // The list of instructions for statement (statement represented by the leader
2017 // instruction).
2018 MapVector<Instruction *, std::vector<Instruction *>> LeaderToInstList;
2019
2020 // The order of statements must be preserved w.r.t. their ordered
2021 // instructions. Without this explicit scan, we would also use non-ordered
2022 // instructions (whose order is arbitrary) to determine statement order.
2023 for (Instruction *Inst : ModeledInsts) {
2024 if (!isOrderedInstruction(Inst))
2025 continue;
2026
2027 auto LeaderIt = UnionFind.findLeader(Inst);
2028 if (LeaderIt == UnionFind.member_end())
2029 continue;
2030
2031 // Insert element for the leader instruction.
2032 (void)LeaderToInstList[*LeaderIt];
2033 }
2034
2035 // Collect the instructions of all leaders. UnionFind's member iterator
2036 // unfortunately are not in any specific order.
2037 for (Instruction *Inst : ModeledInsts) {
2038 auto LeaderIt = UnionFind.findLeader(Inst);
2039 if (LeaderIt == UnionFind.member_end())
2040 continue;
2041
2042 if (Inst == MainInst)
2043 MainLeader = *LeaderIt;
2044 std::vector<Instruction *> &InstList = LeaderToInstList[*LeaderIt];
2045 InstList.push_back(Inst);
2046 }
2047
2048 // Finally build the statements.
2049 int Count = 0;
2050 long BBIdx = scop->getNextStmtIdx();
2051 for (auto &Instructions : LeaderToInstList) {
2052 std::vector<Instruction *> &InstList = Instructions.second;
2053
2054 // If there is no main instruction, make the first statement the main.
2055 bool IsMain = (MainInst ? MainLeader == Instructions.first : Count == 0);
2056
2057 std::string Name = makeStmtName(BB, BBIdx, Count, IsMain);
2058 scop->addScopStmt(BB, Name, L, std::move(InstList));
2059 Count += 1;
2060 }
2061
2062 // Unconditionally add an epilogue (last statement). It contains no
2063 // instructions, but holds the PHI write accesses for successor basic blocks,
2064 // if the incoming value is not defined in another statement if the same BB.
2065 // The epilogue becomes the main statement only if there is no other
2066 // statement that could become main.
2067 // The epilogue will be removed if no PHIWrite is added to it.
2068 std::string EpilogueName = makeStmtName(BB, BBIdx, Count, Count == 0, true);
2069 scop->addScopStmt(BB, EpilogueName, L, {});
2070}
2071
2072void ScopBuilder::buildStmts(Region &SR) {
2073 if (scop->isNonAffineSubRegion(&SR)) {
2074 std::vector<Instruction *> Instructions;
2075 Loop *SurroundingLoop =
2076 getFirstNonBoxedLoopFor(SR.getEntry(), LI, scop->getBoxedLoops());
2077 for (Instruction &Inst : *SR.getEntry())
2078 if (shouldModelInst(&Inst, SurroundingLoop))
2079 Instructions.push_back(&Inst);
2080 long RIdx = scop->getNextStmtIdx();
2081 std::string Name = makeStmtName(&SR, RIdx);
2082 scop->addScopStmt(&SR, Name, SurroundingLoop, Instructions);
2083 return;
2084 }
2085
2086 for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
2087 if (I->isSubRegion())
2088 buildStmts(*I->getNodeAs<Region>());
2089 else {
2090 BasicBlock *BB = I->getNodeAs<BasicBlock>();
2091 switch (StmtGranularity) {
2094 break;
2097 break;
2099 buildSequentialBlockStmts(BB, true);
2100 break;
2101 }
2102 }
2103}
2104
2106 Region *NonAffineSubRegion) {
2107 assert(
2108 Stmt &&
2109 "The exit BB is the only one that cannot be represented by a statement");
2110 assert(Stmt->represents(&BB));
2111
2112 // We do not build access functions for error blocks, as they may contain
2113 // instructions we can not model.
2114 if (SD.isErrorBlock(BB, scop->getRegion()))
2115 return;
2116
2117 auto BuildAccessesForInst = [this, Stmt,
2118 NonAffineSubRegion](Instruction *Inst) {
2119 PHINode *PHI = dyn_cast<PHINode>(Inst);
2120 if (PHI)
2121 buildPHIAccesses(Stmt, PHI, NonAffineSubRegion, false);
2122
2123 if (auto MemInst = MemAccInst::dyn_cast(*Inst)) {
2124 assert(Stmt && "Cannot build access function in non-existing statement");
2125 buildMemoryAccess(MemInst, Stmt);
2126 }
2127
2128 // PHI nodes have already been modeled above and terminators that are
2129 // not part of a non-affine subregion are fully modeled and regenerated
2130 // from the polyhedral domains. Hence, they do not need to be modeled as
2131 // explicit data dependences.
2132 if (!PHI)
2133 buildScalarDependences(Stmt, Inst);
2134 };
2135
2136 const InvariantLoadsSetTy &RIL = scop->getRequiredInvariantLoads();
2137 bool IsEntryBlock = (Stmt->getEntryBlock() == &BB);
2138 if (IsEntryBlock) {
2139 for (Instruction *Inst : Stmt->getInstructions())
2140 BuildAccessesForInst(Inst);
2141 if (Stmt->isRegionStmt())
2142 BuildAccessesForInst(BB.getTerminator());
2143 } else {
2144 for (Instruction &Inst : BB) {
2145 if (isIgnoredIntrinsic(&Inst))
2146 continue;
2147
2148 // Invariant loads already have been processed.
2149 if (isa<LoadInst>(Inst) && RIL.count(cast<LoadInst>(&Inst)))
2150 continue;
2151
2152 BuildAccessesForInst(&Inst);
2153 }
2154 }
2155}
2156
2158 ScopStmt *Stmt, Instruction *Inst, MemoryAccess::AccessType AccType,
2159 Value *BaseAddress, Type *ElementType, bool Affine, Value *AccessValue,
2160 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
2161 MemoryKind Kind) {
2162 bool isKnownMustAccess = false;
2163
2164 // Accesses in single-basic block statements are always executed.
2165 if (Stmt->isBlockStmt())
2166 isKnownMustAccess = true;
2167
2168 if (Stmt->isRegionStmt()) {
2169 // Accesses that dominate the exit block of a non-affine region are always
2170 // executed. In non-affine regions there may exist MemoryKind::Values that
2171 // do not dominate the exit. MemoryKind::Values will always dominate the
2172 // exit and MemoryKind::PHIs only if there is at most one PHI_WRITE in the
2173 // non-affine region.
2174 if (Inst && DT.dominates(Inst->getParent(), Stmt->getRegion()->getExit()))
2175 isKnownMustAccess = true;
2176 }
2177
2178 // Non-affine PHI writes do not "happen" at a particular instruction, but
2179 // after exiting the statement. Therefore they are guaranteed to execute and
2180 // overwrite the old value.
2182 isKnownMustAccess = true;
2183
2184 if (!isKnownMustAccess && AccType == MemoryAccess::MUST_WRITE)
2185 AccType = MemoryAccess::MAY_WRITE;
2186
2187 auto *Access = new MemoryAccess(Stmt, Inst, AccType, BaseAddress, ElementType,
2188 Affine, Subscripts, Sizes, AccessValue, Kind);
2189
2190 scop->addAccessFunction(Access);
2191 Stmt->addAccess(Access);
2192 return Access;
2193}
2194
2197 Value *BaseAddress, Type *ElementType,
2198 bool IsAffine,
2199 ArrayRef<const SCEV *> Subscripts,
2200 ArrayRef<const SCEV *> Sizes,
2201 Value *AccessValue) {
2202 ArrayBasePointers.insert(BaseAddress);
2203 addMemoryAccess(Stmt, MemAccInst, AccType, BaseAddress, ElementType, IsAffine,
2204 AccessValue, Subscripts, Sizes, MemoryKind::Array);
2205}
2206
2207/// Check if @p Expr is divisible by @p Size.
2208static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE) {
2209 assert(Size != 0);
2210 if (Size == 1)
2211 return true;
2212
2213 // Only one factor needs to be divisible.
2214 if (auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) {
2215 for (const SCEV *FactorExpr : MulExpr->operands())
2216 if (isDivisible(FactorExpr, Size, SE))
2217 return true;
2218 return false;
2219 }
2220
2221 // For other n-ary expressions (Add, AddRec, Max,...) all operands need
2222 // to be divisible.
2223 if (auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) {
2224 for (const SCEV *OpExpr : NAryExpr->operands())
2225 if (!isDivisible(OpExpr, Size, SE))
2226 return false;
2227 return true;
2228 }
2229
2230 const SCEV *SizeSCEV = SE.getConstant(Expr->getType(), Size);
2231 const SCEV *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV);
2232 const SCEV *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV);
2233 return MulSCEV == Expr;
2234}
2235
2237 isl::union_set Accessed = scop->getAccesses().range();
2238
2239 for (auto Array : scop->arrays()) {
2240 if (Array->getNumberOfDimensions() <= 1)
2241 continue;
2242
2243 isl::space Space = Array->getSpace();
2244 Space = Space.align_params(Accessed.get_space());
2245
2246 if (!Accessed.contains(Space))
2247 continue;
2248
2249 isl::set Elements = Accessed.extract_set(Space);
2250 isl::map Transform = isl::map::universe(Array->getSpace().map_from_set());
2251
2252 std::vector<int> Int;
2253 unsigned Dims = unsignedFromIslSize(Elements.tuple_dim());
2254 for (unsigned i = 0; i < Dims; i++) {
2255 isl::set DimOnly = isl::set(Elements).project_out(isl::dim::set, 0, i);
2256 DimOnly = DimOnly.project_out(isl::dim::set, 1, Dims - i - 1);
2257 DimOnly = DimOnly.lower_bound_si(isl::dim::set, 0, 0);
2258
2259 isl::basic_set DimHull = DimOnly.affine_hull();
2260
2261 if (i == Dims - 1) {
2262 Int.push_back(1);
2263 Transform = Transform.equate(isl::dim::in, i, isl::dim::out, i);
2264 continue;
2265 }
2266
2267 if (unsignedFromIslSize(DimHull.dim(isl::dim::div)) == 1) {
2268 isl::aff Diff = DimHull.get_div(0);
2269 isl::val Val = Diff.get_denominator_val();
2270
2271 int ValInt = 1;
2272 if (Val.is_int()) {
2273 auto ValAPInt = APIntFromVal(Val);
2274 if (ValAPInt.isSignedIntN(32))
2275 ValInt = ValAPInt.getSExtValue();
2276 } else {
2277 }
2278
2279 Int.push_back(ValInt);
2281 isl::local_space(Transform.get_space()));
2282 C = C.set_coefficient_si(isl::dim::out, i, ValInt);
2283 C = C.set_coefficient_si(isl::dim::in, i, -1);
2284 Transform = Transform.add_constraint(C);
2285 continue;
2286 }
2287
2288 isl::basic_set ZeroSet = isl::basic_set(DimHull);
2289 ZeroSet = ZeroSet.fix_si(isl::dim::set, 0, 0);
2290
2291 int ValInt = 1;
2292 if (ZeroSet.is_equal(DimHull)) {
2293 ValInt = 0;
2294 }
2295
2296 Int.push_back(ValInt);
2297 Transform = Transform.equate(isl::dim::in, i, isl::dim::out, i);
2298 }
2299
2300 isl::set MappedElements = isl::map(Transform).domain();
2301 if (!Elements.is_subset(MappedElements))
2302 continue;
2303
2304 bool CanFold = true;
2305 if (Int[0] <= 1)
2306 CanFold = false;
2307
2308 unsigned NumDims = Array->getNumberOfDimensions();
2309 for (unsigned i = 1; i < NumDims - 1; i++)
2310 if (Int[0] != Int[i] && Int[i])
2311 CanFold = false;
2312
2313 if (!CanFold)
2314 continue;
2315
2316 for (auto &Access : scop->access_functions())
2317 if (Access->getScopArrayInfo() == Array)
2318 Access->setAccessRelation(
2319 Access->getAccessRelation().apply_range(Transform));
2320
2321 std::vector<const SCEV *> Sizes;
2322 for (unsigned i = 0; i < NumDims; i++) {
2323 auto Size = Array->getDimensionSize(i);
2324
2325 if (i == NumDims - 1)
2326 Size = SE.getMulExpr(Size, SE.getConstant(Size->getType(), Int[0]));
2327 Sizes.push_back(Size);
2328 }
2329
2330 Array->updateSizes(Sizes, false /* CheckConsistency */);
2331 }
2332}
2333
2340
2342 // Check all array accesses for each base pointer and find a (virtual) element
2343 // size for the base pointer that divides all access functions.
2344 for (ScopStmt &Stmt : *scop)
2345 for (MemoryAccess *Access : Stmt) {
2346 if (!Access->isArrayKind())
2347 continue;
2349 const_cast<ScopArrayInfo *>(Access->getScopArrayInfo());
2350
2351 if (Array->getNumberOfDimensions() != 1)
2352 continue;
2353 unsigned DivisibleSize = Array->getElemSizeInBytes();
2354 const SCEV *Subscript = Access->getSubscript(0);
2355 while (!isDivisible(Subscript, DivisibleSize, SE))
2356 DivisibleSize /= 2;
2357 auto *Ty = IntegerType::get(SE.getContext(), DivisibleSize * 8);
2358 Array->updateElementType(Ty);
2359 }
2360
2361 for (auto &Stmt : *scop)
2362 for (auto &Access : Stmt)
2363 Access->updateDimensionality();
2364}
2365
2367 for (auto &Stmt : *scop)
2368 for (auto &Access : Stmt)
2369 Access->foldAccessRelation();
2370}
2371
2374 return;
2375 for (auto &Stmt : *scop)
2376 for (auto &Access : Stmt) {
2377 isl::set Outside = Access->assumeNoOutOfBound();
2378 const auto &Loc = Access->getAccessInstruction()
2379 ? Access->getAccessInstruction()->getDebugLoc()
2380 : DebugLoc();
2383 }
2384}
2385
2386void ScopBuilder::ensureValueWrite(Instruction *Inst) {
2387 // Find the statement that defines the value of Inst. That statement has to
2388 // write the value to make it available to those statements that read it.
2389 ScopStmt *Stmt = scop->getStmtFor(Inst);
2390
2391 // It is possible that the value is synthesizable within a loop (such that it
2392 // is not part of any statement), but not after the loop (where you need the
2393 // number of loop round-trips to synthesize it). In LCSSA-form a PHI node will
2394 // avoid this. In case the IR has no such PHI, use the last statement (where
2395 // the value is synthesizable) to write the value.
2396 if (!Stmt)
2397 Stmt = scop->getLastStmtFor(Inst->getParent());
2398
2399 // Inst not defined within this SCoP.
2400 if (!Stmt)
2401 return;
2402
2403 // Do not process further if the instruction is already written.
2404 if (Stmt->lookupValueWriteOf(Inst))
2405 return;
2406
2407 addMemoryAccess(Stmt, Inst, MemoryAccess::MUST_WRITE, Inst, Inst->getType(),
2408 true, Inst, ArrayRef<const SCEV *>(),
2409 ArrayRef<const SCEV *>(), MemoryKind::Value);
2410}
2411
2413 // TODO: Make ScopStmt::ensureValueRead(Value*) offer the same functionality
2414 // to be able to replace this one. Currently, there is a split responsibility.
2415 // In a first step, the MemoryAccess is created, but without the
2416 // AccessRelation. In the second step by ScopStmt::buildAccessRelations(), the
2417 // AccessRelation is created. At least for scalar accesses, there is no new
2418 // information available at ScopStmt::buildAccessRelations(), so we could
2419 // create the AccessRelation right away. This is what
2420 // ScopStmt::ensureValueRead(Value*) does.
2421
2422 auto *Scope = UserStmt->getSurroundingLoop();
2423 auto VUse = VirtualUse::create(scop.get(), UserStmt, Scope, V, false);
2424 switch (VUse.getKind()) {
2426 case VirtualUse::Block:
2429 case VirtualUse::Intra:
2430 // Uses of these kinds do not need a MemoryAccess.
2431 break;
2432
2434 // Add MemoryAccess for invariant values only if requested.
2436 break;
2437
2438 [[fallthrough]];
2439 case VirtualUse::Inter:
2440
2441 // Do not create another MemoryAccess for reloading the value if one already
2442 // exists.
2443 if (UserStmt->lookupValueReadOf(V))
2444 break;
2445
2446 addMemoryAccess(UserStmt, nullptr, MemoryAccess::READ, V, V->getType(),
2447 true, V, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2449
2450 // Inter-statement uses need to write the value in their defining statement.
2451 if (VUse.isInter())
2452 ensureValueWrite(cast<Instruction>(V));
2453 break;
2454 }
2455}
2456
2457void ScopBuilder::ensurePHIWrite(PHINode *PHI, ScopStmt *IncomingStmt,
2458 BasicBlock *IncomingBlock,
2459 Value *IncomingValue, bool IsExitBlock) {
2460 // As the incoming block might turn out to be an error statement ensure we
2461 // will create an exit PHI SAI object. It is needed during code generation
2462 // and would be created later anyway.
2463 if (IsExitBlock)
2464 scop->getOrCreateScopArrayInfo(PHI, PHI->getType(), {},
2466
2467 // This is possible if PHI is in the SCoP's entry block. The incoming blocks
2468 // from outside the SCoP's region have no statement representation.
2469 if (!IncomingStmt)
2470 return;
2471
2472 // Take care for the incoming value being available in the incoming block.
2473 // This must be done before the check for multiple PHI writes because multiple
2474 // exiting edges from subregion each can be the effective written value of the
2475 // subregion. As such, all of them must be made available in the subregion
2476 // statement.
2477 ensureValueRead(IncomingValue, IncomingStmt);
2478
2479 // Do not add more than one MemoryAccess per PHINode and ScopStmt.
2480 if (MemoryAccess *Acc = IncomingStmt->lookupPHIWriteOf(PHI)) {
2481 assert(Acc->getAccessInstruction() == PHI);
2482 Acc->addIncoming(IncomingBlock, IncomingValue);
2483 return;
2484 }
2485
2487 IncomingStmt, PHI, MemoryAccess::MUST_WRITE, PHI, PHI->getType(), true,
2488 PHI, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2489 IsExitBlock ? MemoryKind::ExitPHI : MemoryKind::PHI);
2490 assert(Acc);
2491 Acc->addIncoming(IncomingBlock, IncomingValue);
2492}
2493
2495 addMemoryAccess(PHIStmt, PHI, MemoryAccess::READ, PHI, PHI->getType(), true,
2496 PHI, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2498}
2499
2501 isl::id Id = isl::id::alloc(scop->getIslCtx(), Stmt.getBaseName(), &Stmt);
2502
2503 Stmt.Domain = scop->getDomainConditions(&Stmt);
2504 Stmt.Domain = Stmt.Domain.set_tuple_id(Id);
2505}
2506
2508 isl::set Domain = Stmt.getDomain();
2509 BasicBlock *BB = Stmt.getEntryBlock();
2510
2511 Loop *L = LI.getLoopFor(BB);
2512
2513 while (L && Stmt.isRegionStmt() && Stmt.getRegion()->contains(L))
2514 L = L->getParentLoop();
2515
2516 SmallVector<llvm::Loop *, 8> Loops;
2517
2518 while (L && Stmt.getParent()->getRegion().contains(L)) {
2519 Loops.push_back(L);
2520 L = L->getParentLoop();
2521 }
2522
2523 Stmt.NestLoops.insert(Stmt.NestLoops.begin(), Loops.rbegin(), Loops.rend());
2524}
2525
2526/// Return the reduction type for a given binary operator.
2528getReductionType(const BinaryOperator *BinOp) {
2529 if (!BinOp)
2530 return MemoryAccess::RT_NONE;
2531 switch (BinOp->getOpcode()) {
2532 case Instruction::FAdd:
2533 if (!BinOp->isFast())
2534 return MemoryAccess::RT_NONE;
2535 [[fallthrough]];
2536 case Instruction::Add:
2537 return MemoryAccess::RT_ADD;
2538 case Instruction::Or:
2539 return MemoryAccess::RT_BOR;
2540 case Instruction::Xor:
2541 return MemoryAccess::RT_BXOR;
2542 case Instruction::And:
2543 return MemoryAccess::RT_BAND;
2544 case Instruction::FMul:
2545 if (!BinOp->isFast())
2546 return MemoryAccess::RT_NONE;
2547 [[fallthrough]];
2548 case Instruction::Mul:
2550 return MemoryAccess::RT_NONE;
2551 return MemoryAccess::RT_MUL;
2552 default:
2553 return MemoryAccess::RT_NONE;
2554 }
2555}
2556
2557/// @brief Combine two reduction types
2561 if (RT0 == MemoryAccess::RT_BOTTOM)
2562 return RT1;
2563 if (RT0 == RT1)
2564 return RT1;
2565 return MemoryAccess::RT_NONE;
2566}
2567
2568/// True if @p AllAccs intersects with @p MemAccs except @p LoadMA and @p
2569/// StoreMA
2571 MemoryAccess *StoreMA, isl::set Domain,
2572 SmallVector<MemoryAccess *, 8> &MemAccs) {
2573 bool HasIntersectingAccs = false;
2574 auto AllAccsNoParams = AllAccs.project_out_all_params();
2575
2576 for (MemoryAccess *MA : MemAccs) {
2577 if (MA == LoadMA || MA == StoreMA)
2578 continue;
2579 auto AccRel = MA->getAccessRelation().intersect_domain(Domain);
2580 auto Accs = AccRel.range();
2581 auto AccsNoParams = Accs.project_out_all_params();
2582
2583 bool CompatibleSpace = AllAccsNoParams.has_equal_space(AccsNoParams);
2584
2585 if (CompatibleSpace) {
2586 auto OverlapAccs = Accs.intersect(AllAccs);
2587 bool DoesIntersect = !OverlapAccs.is_empty();
2588 HasIntersectingAccs |= DoesIntersect;
2589 }
2590 }
2591 return HasIntersectingAccs;
2592}
2593
2594/// Test if the accesses of @p LoadMA and @p StoreMA can form a reduction
2597 SmallVector<MemoryAccess *, 8> &MemAccs) {
2598 // First check if the base value is the same.
2599 isl::map LoadAccs = LoadMA->getAccessRelation();
2600 isl::map StoreAccs = StoreMA->getAccessRelation();
2601 bool Valid = LoadAccs.has_equal_space(StoreAccs);
2602 POLLY_DEBUG(dbgs() << " == The accessed space below is "
2603 << (Valid ? "" : "not ") << "equal!\n");
2604 POLLY_DEBUG(LoadMA->dump(); StoreMA->dump());
2605
2606 if (Valid) {
2607 // Then check if they actually access the same memory.
2608 isl::map R = isl::manage(LoadAccs.copy())
2609 .intersect_domain(isl::manage(Domain.copy()));
2610 isl::map W = isl::manage(StoreAccs.copy())
2611 .intersect_domain(isl::manage(Domain.copy()));
2612 isl::set RS = R.range();
2613 isl::set WS = W.range();
2614
2615 isl::set InterAccs =
2616 isl::manage(RS.copy()).intersect(isl::manage(WS.copy()));
2617 Valid = !InterAccs.is_empty();
2618 POLLY_DEBUG(dbgs() << " == The accessed memory is " << (Valid ? "" : "not ")
2619 << "overlapping!\n");
2620 }
2621
2622 if (Valid) {
2623 // Finally, check if they are no other instructions accessing this memory
2624 isl::map AllAccsRel = LoadAccs.unite(StoreAccs);
2625 AllAccsRel = AllAccsRel.intersect_domain(Domain);
2626 isl::set AllAccs = AllAccsRel.range();
2627 Valid = !hasIntersectingAccesses(AllAccs, LoadMA, StoreMA, Domain, MemAccs);
2628 POLLY_DEBUG(dbgs() << " == The accessed memory is " << (Valid ? "not " : "")
2629 << "accessed by other instructions!\n");
2630 }
2631
2632 return Valid;
2633}
2634
2636 // Perform a data flow analysis on the current scop statement to propagate the
2637 // uses of loaded values. Then check and mark the memory accesses which are
2638 // part of reduction like chains.
2639 // During the data flow analysis we use the State variable to keep track of
2640 // the used "load-instructions" for each instruction in the scop statement.
2641 // This includes the LLVM-IR of the load and the "number of uses" (or the
2642 // number of paths in the operand tree which end in this load).
2643 using StatePairTy = std::pair<unsigned, MemoryAccess::ReductionType>;
2644 using FlowInSetTy = MapVector<const LoadInst *, StatePairTy>;
2645 using StateTy = MapVector<const Instruction *, FlowInSetTy>;
2646 StateTy State;
2647
2648 // Invalid loads are loads which have uses we can't track properly in the
2649 // state map. This includes loads which:
2650 // o do not form a reduction when they flow into a memory location:
2651 // (e.g., A[i] = B[i] * 3 and A[i] = A[i] * A[i] + A[i])
2652 // o are used by a non binary operator or one which is not commutative
2653 // and associative (e.g., A[i] = A[i] % 3)
2654 // o might change the control flow (e.g., if (A[i]))
2655 // o are used in indirect memory accesses (e.g., A[B[i]])
2656 // o are used outside the current scop statement
2657 SmallPtrSet<const Instruction *, 8> InvalidLoads;
2658 SmallVector<BasicBlock *, 8> ScopBlocks;
2659 BasicBlock *BB = Stmt.getBasicBlock();
2660 if (BB)
2661 ScopBlocks.push_back(BB);
2662 else
2663 for (BasicBlock *Block : Stmt.getRegion()->blocks())
2664 ScopBlocks.push_back(Block);
2665 // Run the data flow analysis for all values in the scop statement
2666 for (BasicBlock *Block : ScopBlocks) {
2667 for (Instruction &Inst : *Block) {
2668 if ((Stmt.getParent())->getStmtFor(&Inst) != &Stmt)
2669 continue;
2670 bool UsedOutsideStmt = any_of(Inst.users(), [&Stmt](User *U) {
2671 return (Stmt.getParent())->getStmtFor(cast<Instruction>(U)) != &Stmt;
2672 });
2673 // Treat loads and stores special
2674 if (auto *Load = dyn_cast<LoadInst>(&Inst)) {
2675 // Invalidate all loads used which feed into the address of this load.
2676 if (auto *Ptr = dyn_cast<Instruction>(Load->getPointerOperand())) {
2677 const auto &It = State.find(Ptr);
2678 if (It != State.end())
2679 InvalidLoads.insert_range(llvm::make_first_range(It->second));
2680 }
2681
2682 // If this load is used outside this stmt, invalidate it.
2683 if (UsedOutsideStmt)
2684 InvalidLoads.insert(Load);
2685
2686 // And indicate that this load uses itself once but without specifying
2687 // any reduction operator.
2688 State[Load].insert(
2689 std::make_pair(Load, std::make_pair(1, MemoryAccess::RT_BOTTOM)));
2690 continue;
2691 }
2692
2693 if (auto *Store = dyn_cast<StoreInst>(&Inst)) {
2694 // Invalidate all loads which feed into the address of this store.
2695 if (const Instruction *Ptr =
2696 dyn_cast<Instruction>(Store->getPointerOperand())) {
2697 const auto &It = State.find(Ptr);
2698 if (It != State.end())
2699 InvalidLoads.insert_range(llvm::make_first_range(It->second));
2700 }
2701
2702 // Propagate the uses of the value operand to the store
2703 if (auto *ValueInst = dyn_cast<Instruction>(Store->getValueOperand()))
2704 State.insert(std::make_pair(Store, State[ValueInst]));
2705 continue;
2706 }
2707
2708 // Non load and store instructions are either binary operators or they
2709 // will invalidate all used loads.
2710 auto *BinOp = dyn_cast<BinaryOperator>(&Inst);
2712 POLLY_DEBUG(dbgs() << "CurInst: " << Inst << " RT: " << CurRedType
2713 << "\n");
2714
2715 // Iterate over all operands and propagate their input loads to
2716 // instruction.
2717 FlowInSetTy &InstInFlowSet = State[&Inst];
2718 for (Use &Op : Inst.operands()) {
2719 auto *OpInst = dyn_cast<Instruction>(Op);
2720 if (!OpInst)
2721 continue;
2722
2723 POLLY_DEBUG(dbgs().indent(4) << "Op Inst: " << *OpInst << "\n");
2724 const StateTy::iterator &OpInFlowSetIt = State.find(OpInst);
2725 if (OpInFlowSetIt == State.end())
2726 continue;
2727
2728 // Iterate over all the input loads of the operand and combine them
2729 // with the input loads of current instruction.
2730 FlowInSetTy &OpInFlowSet = OpInFlowSetIt->second;
2731 for (auto &OpInFlowPair : OpInFlowSet) {
2732 unsigned OpFlowIn = OpInFlowPair.second.first;
2733 unsigned InstFlowIn = InstInFlowSet[OpInFlowPair.first].first;
2734
2735 MemoryAccess::ReductionType OpRedType = OpInFlowPair.second.second;
2736 MemoryAccess::ReductionType InstRedType =
2737 InstInFlowSet[OpInFlowPair.first].second;
2738
2739 MemoryAccess::ReductionType NewRedType =
2740 combineReductionType(OpRedType, CurRedType);
2741 if (InstFlowIn)
2742 NewRedType = combineReductionType(NewRedType, InstRedType);
2743
2744 POLLY_DEBUG(dbgs().indent(8) << "OpRedType: " << OpRedType << "\n");
2745 POLLY_DEBUG(dbgs().indent(8) << "NewRedType: " << NewRedType << "\n");
2746 InstInFlowSet[OpInFlowPair.first] =
2747 std::make_pair(OpFlowIn + InstFlowIn, NewRedType);
2748 }
2749 }
2750
2751 // If this operation is used outside the stmt, invalidate all the loads
2752 // which feed into it.
2753 if (UsedOutsideStmt)
2754 InvalidLoads.insert_range(llvm::make_first_range(InstInFlowSet));
2755 }
2756 }
2757
2758 // All used loads are propagated through the whole basic block; now try to
2759 // find valid reduction-like candidate pairs. These load-store pairs fulfill
2760 // all reduction like properties with regards to only this load-store chain.
2761 // We later have to check if the loaded value was invalidated by an
2762 // instruction not in that chain.
2763 using MemAccPair = std::pair<MemoryAccess *, MemoryAccess *>;
2764 DenseMap<MemAccPair, MemoryAccess::ReductionType> ValidCandidates;
2765
2766 // Iterate over all write memory accesses and check the loads flowing into
2767 // it for reduction candidate pairs.
2768 for (MemoryAccess *WriteMA : Stmt.MemAccs) {
2769 if (WriteMA->isRead())
2770 continue;
2771 StoreInst *St = dyn_cast<StoreInst>(WriteMA->getAccessInstruction());
2772 if (!St)
2773 continue;
2774 assert(!St->isVolatile());
2775
2776 FlowInSetTy &MaInFlowSet = State[WriteMA->getAccessInstruction()];
2777 for (auto &MaInFlowSetElem : MaInFlowSet) {
2778 MemoryAccess *ReadMA = &Stmt.getArrayAccessFor(MaInFlowSetElem.first);
2779 assert(ReadMA && "Couldn't find memory access for incoming load!");
2780
2781 POLLY_DEBUG(dbgs() << "'" << *ReadMA->getAccessInstruction()
2782 << "'\n\tflows into\n'"
2783 << *WriteMA->getAccessInstruction() << "'\n\t #"
2784 << MaInFlowSetElem.second.first << " times & RT: "
2785 << MaInFlowSetElem.second.second << "\n");
2786
2787 MemoryAccess::ReductionType RT = MaInFlowSetElem.second.second;
2788 unsigned NumAllowableInFlow = 1;
2789
2790 // We allow the load to flow in exactly once for binary reductions
2791 bool Valid = (MaInFlowSetElem.second.first == NumAllowableInFlow);
2792
2793 // Check if we saw a valid chain of binary operators.
2794 Valid = Valid && RT != MemoryAccess::RT_BOTTOM;
2795 Valid = Valid && RT != MemoryAccess::RT_NONE;
2796
2797 // Then check if the memory accesses allow a reduction.
2798 Valid = Valid && checkCandidatePairAccesses(
2799 ReadMA, WriteMA, Stmt.getDomain(), Stmt.MemAccs);
2800
2801 // Finally, mark the pair as a candidate or the load as a invalid one.
2802 if (Valid)
2803 ValidCandidates[std::make_pair(ReadMA, WriteMA)] = RT;
2804 else
2805 InvalidLoads.insert(ReadMA->getAccessInstruction());
2806 }
2807 }
2808
2809 // In the last step mark the memory accesses of candidate pairs as reduction
2810 // like if the load wasn't marked invalid in the previous step.
2811 for (auto &CandidatePair : ValidCandidates) {
2812 MemoryAccess *LoadMA = CandidatePair.first.first;
2813 if (InvalidLoads.count(LoadMA->getAccessInstruction()))
2814 continue;
2816 dbgs() << " Load :: "
2817 << *((CandidatePair.first.first)->getAccessInstruction())
2818 << "\n Store :: "
2819 << *((CandidatePair.first.second)->getAccessInstruction())
2820 << "\n are marked as reduction like\n");
2821 MemoryAccess::ReductionType RT = CandidatePair.second;
2822 CandidatePair.first.first->markAsReductionLike(RT);
2823 CandidatePair.first.second->markAsReductionLike(RT);
2824 }
2825}
2826
2828 auto &RIL = scop->getRequiredInvariantLoads();
2829 for (LoadInst *LI : RIL) {
2830 assert(LI && scop->contains(LI));
2831 // If there exists a statement in the scop which has a memory access for
2832 // @p LI, then mark this scop as infeasible for optimization.
2833 for (ScopStmt &Stmt : *scop)
2834 if (Stmt.getArrayAccessOrNULLFor(LI)) {
2835 scop->invalidate(INVARIANTLOAD, LI->getDebugLoc(), LI->getParent());
2836 return;
2837 }
2838 }
2839}
2840
2843 return;
2844
2845 isl::union_map Writes = scop->getWrites();
2846 for (ScopStmt &Stmt : *scop) {
2847 InvariantAccessesTy InvariantAccesses;
2848
2849 for (MemoryAccess *Access : Stmt) {
2850 isl::set NHCtx = getNonHoistableCtx(Access, Writes);
2851 if (!NHCtx.is_null())
2852 InvariantAccesses.push_back({Access, NHCtx});
2853 }
2854
2855 // Transfer the memory access from the statement to the SCoP.
2856 for (auto InvMA : InvariantAccesses)
2857 Stmt.removeMemoryAccess(InvMA.MA);
2858 addInvariantLoads(Stmt, InvariantAccesses);
2859 }
2860}
2861
2862/// Check if an access range is too complex.
2863///
2864/// An access range is too complex, if it contains either many disjuncts or
2865/// very complex expressions. As a simple heuristic, we assume if a set to
2866/// be too complex if the sum of existentially quantified dimensions and
2867/// set dimensions is larger than a threshold. This reliably detects both
2868/// sets with many disjuncts as well as sets with many divisions as they
2869/// arise in h264.
2870///
2871/// @param AccessRange The range to check for complexity.
2872///
2873/// @returns True if the access range is too complex.
2874static bool isAccessRangeTooComplex(isl::set AccessRange) {
2875 unsigned NumTotalDims = 0;
2876
2877 for (isl::basic_set BSet : AccessRange.get_basic_set_list()) {
2878 NumTotalDims += unsignedFromIslSize(BSet.dim(isl::dim::div));
2879 NumTotalDims += unsignedFromIslSize(BSet.dim(isl::dim::set));
2880 }
2881
2882 if (NumTotalDims > MaxDimensionsInAccessRange)
2883 return true;
2884
2885 return false;
2886}
2887
2889 isl::union_map Writes) {
2890 if (auto *BasePtrMA = scop->lookupBasePtrAccess(MA)) {
2891 return getNonHoistableCtx(BasePtrMA, Writes).is_null();
2892 }
2893
2894 Value *BaseAddr = MA->getOriginalBaseAddr();
2895 if (auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr))
2896 if (!isa<LoadInst>(BasePtrInst))
2897 return scop->contains(BasePtrInst);
2898
2899 return false;
2900}
2901
2903 if (UserContextStr.empty())
2904 return;
2905
2906 isl::set UserContext = isl::set(scop->getIslCtx(), UserContextStr.c_str());
2907 isl::space Space = scop->getParamSpace();
2908 isl::size SpaceParams = Space.dim(isl::dim::param);
2909 if (unsignedFromIslSize(SpaceParams) !=
2910 unsignedFromIslSize(UserContext.dim(isl::dim::param))) {
2911 std::string SpaceStr = stringFromIslObj(Space, "null");
2912 errs() << "Error: the context provided in -polly-context has not the same "
2913 << "number of dimensions than the computed context. Due to this "
2914 << "mismatch, the -polly-context option is ignored. Please provide "
2915 << "the context in the parameter space: " << SpaceStr << ".\n";
2916 return;
2917 }
2918
2919 for (auto i : rangeIslSize(0, SpaceParams)) {
2920 std::string NameContext =
2921 scop->getContext().get_dim_name(isl::dim::param, i);
2922 std::string NameUserContext = UserContext.get_dim_name(isl::dim::param, i);
2923
2924 if (NameContext != NameUserContext) {
2925 std::string SpaceStr = stringFromIslObj(Space, "null");
2926 errs() << "Error: the name of dimension " << i
2927 << " provided in -polly-context "
2928 << "is '" << NameUserContext << "', but the name in the computed "
2929 << "context is '" << NameContext
2930 << "'. Due to this name mismatch, "
2931 << "the -polly-context option is ignored. Please provide "
2932 << "the context in the parameter space: " << SpaceStr << ".\n";
2933 return;
2934 }
2935
2936 UserContext = UserContext.set_dim_id(isl::dim::param, i,
2937 Space.get_dim_id(isl::dim::param, i));
2938 }
2939 isl::set newContext = scop->getContext().intersect(UserContext);
2940 scop->setContext(newContext);
2941}
2942
2944 isl::union_map Writes) {
2945 // TODO: Loads that are not loop carried, hence are in a statement with
2946 // zero iterators, are by construction invariant, though we
2947 // currently "hoist" them anyway. This is necessary because we allow
2948 // them to be treated as parameters (e.g., in conditions) and our code
2949 // generation would otherwise use the old value.
2950
2951 auto &Stmt = *Access->getStatement();
2952 BasicBlock *BB = Stmt.getEntryBlock();
2953
2954 if (Access->isScalarKind() || Access->isWrite() || !Access->isAffine() ||
2955 Access->isMemoryIntrinsic())
2956 return {};
2957
2958 // Skip accesses that have an invariant base pointer which is defined but
2959 // not loaded inside the SCoP. This can happened e.g., if a readnone call
2960 // returns a pointer that is used as a base address. However, as we want
2961 // to hoist indirect pointers, we allow the base pointer to be defined in
2962 // the region if it is also a memory access. Each ScopArrayInfo object
2963 // that has a base pointer origin has a base pointer that is loaded and
2964 // that it is invariant, thus it will be hoisted too. However, if there is
2965 // no base pointer origin we check that the base pointer is defined
2966 // outside the region.
2967 auto *LI = cast<LoadInst>(Access->getAccessInstruction());
2968 if (hasNonHoistableBasePtrInScop(Access, Writes))
2969 return {};
2970
2971 isl::map AccessRelation = Access->getAccessRelation();
2972 assert(!AccessRelation.is_empty());
2973
2974 if (AccessRelation.involves_dims(isl::dim::in, 0, Stmt.getNumIterators()))
2975 return {};
2976
2977 AccessRelation = AccessRelation.intersect_domain(Stmt.getDomain());
2978 isl::set SafeToLoad;
2979
2980 auto &DL = scop->getFunction().getDataLayout();
2981 if (isSafeToLoadUnconditionally(LI->getPointerOperand(), LI->getType(),
2982 LI->getAlign(), DL, nullptr)) {
2983 SafeToLoad = isl::set::universe(AccessRelation.get_space().range());
2984 } else if (BB != LI->getParent()) {
2985 // Skip accesses in non-affine subregions as they might not be executed
2986 // under the same condition as the entry of the non-affine subregion.
2987 return {};
2988 } else {
2989 SafeToLoad = AccessRelation.range();
2990 }
2991
2992 if (isAccessRangeTooComplex(AccessRelation.range()))
2993 return {};
2994
2995 isl::union_map Written = Writes.intersect_range(SafeToLoad);
2996 isl::set WrittenCtx = Written.params();
2997 bool IsWritten = !WrittenCtx.is_empty();
2998
2999 if (!IsWritten)
3000 return WrittenCtx;
3001
3002 WrittenCtx = WrittenCtx.remove_divs();
3003 bool TooComplex =
3005 if (TooComplex || !isRequiredInvariantLoad(LI))
3006 return {};
3007
3008 scop->addAssumption(INVARIANTLOAD, WrittenCtx, LI->getDebugLoc(),
3009 AS_RESTRICTION, LI->getParent());
3010 return WrittenCtx;
3011}
3012
3013static bool isAParameter(llvm::Value *maybeParam, const Function &F) {
3014 for (const llvm::Argument &Arg : F.args())
3015 if (&Arg == maybeParam)
3016 return true;
3017
3018 return false;
3019}
3020
3022 bool StmtInvalidCtxIsEmpty,
3023 bool MAInvalidCtxIsEmpty,
3024 bool NonHoistableCtxIsEmpty) {
3025 LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction());
3026 const DataLayout &DL = LInst->getDataLayout();
3028 isAParameter(LInst->getPointerOperand(), scop->getFunction()))
3029 return true;
3030
3031 // TODO: We can provide more information for better but more expensive
3032 // results.
3033 if (!isDereferenceableAndAlignedPointer(
3034 LInst->getPointerOperand(), LInst->getType(), LInst->getAlign(), DL))
3035 return false;
3036
3037 // If the location might be overwritten we do not hoist it unconditionally.
3038 //
3039 // TODO: This is probably too conservative.
3040 if (!NonHoistableCtxIsEmpty)
3041 return false;
3042
3043 // If a dereferenceable load is in a statement that is modeled precisely we
3044 // can hoist it.
3045 if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty)
3046 return true;
3047
3048 // Even if the statement is not modeled precisely we can hoist the load if it
3049 // does not involve any parameters that might have been specialized by the
3050 // statement domain.
3051 for (const SCEV *Subscript : MA->subscripts())
3052 if (!isa<SCEVConstant>(Subscript))
3053 return false;
3054 return true;
3055}
3056
3058 InvariantAccessesTy &InvMAs) {
3059 if (InvMAs.empty())
3060 return;
3061
3062 isl::set StmtInvalidCtx = Stmt.getInvalidContext();
3063 bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.is_empty();
3064
3065 // Get the context under which the statement is executed but remove the error
3066 // context under which this statement is reached.
3067 isl::set DomainCtx = Stmt.getDomain().params();
3068 DomainCtx = DomainCtx.subtract(StmtInvalidCtx);
3069
3071 auto *AccInst = InvMAs.front().MA->getAccessInstruction();
3072 scop->invalidate(COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent());
3073 return;
3074 }
3075
3076 // Project out all parameters that relate to loads in the statement. Otherwise
3077 // we could have cyclic dependences on the constraints under which the
3078 // hoisted loads are executed and we could not determine an order in which to
3079 // pre-load them. This happens because not only lower bounds are part of the
3080 // domain but also upper bounds.
3081 for (auto &InvMA : InvMAs) {
3082 auto *MA = InvMA.MA;
3083 Instruction *AccInst = MA->getAccessInstruction();
3084 if (SE.isSCEVable(AccInst->getType())) {
3085 SetVector<Value *> Values;
3086 for (const SCEV *Parameter : scop->parameters()) {
3087 Values.clear();
3088 findValues(Parameter, SE, Values);
3089 if (!Values.count(AccInst))
3090 continue;
3091
3092 isl::id ParamId = scop->getIdForParam(Parameter);
3093 if (!ParamId.is_null()) {
3094 int Dim = DomainCtx.find_dim_by_id(isl::dim::param, ParamId);
3095 if (Dim >= 0)
3096 DomainCtx = DomainCtx.eliminate(isl::dim::param, Dim, 1);
3097 }
3098 }
3099 }
3100 }
3101
3102 for (auto &InvMA : InvMAs) {
3103 auto *MA = InvMA.MA;
3104 isl::set NHCtx = InvMA.NonHoistableCtx;
3105
3106 // Check for another invariant access that accesses the same location as
3107 // MA and if found consolidate them. Otherwise create a new equivalence
3108 // class at the end of InvariantEquivClasses.
3109 LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction());
3110 Type *Ty = LInst->getType();
3111 const SCEV *PointerSCEV = SE.getSCEV(LInst->getPointerOperand());
3112
3113 isl::set MAInvalidCtx = MA->getInvalidContext();
3114 bool NonHoistableCtxIsEmpty = NHCtx.is_empty();
3115 bool MAInvalidCtxIsEmpty = MAInvalidCtx.is_empty();
3116
3117 isl::set MACtx;
3118 // Check if we know that this pointer can be speculatively accessed.
3119 if (canAlwaysBeHoisted(MA, StmtInvalidCtxIsEmpty, MAInvalidCtxIsEmpty,
3120 NonHoistableCtxIsEmpty)) {
3121 MACtx = isl::set::universe(DomainCtx.get_space());
3122 } else {
3123 MACtx = DomainCtx;
3124 MACtx = MACtx.subtract(MAInvalidCtx.unite(NHCtx));
3125 MACtx = MACtx.gist_params(scop->getContext());
3126 }
3127
3128 bool Consolidated = false;
3129 for (auto &IAClass : scop->invariantEquivClasses()) {
3130 if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType)
3131 continue;
3132
3133 // If the pointer and the type is equal check if the access function wrt.
3134 // to the domain is equal too. It can happen that the domain fixes
3135 // parameter values and these can be different for distinct part of the
3136 // SCoP. If this happens we cannot consolidate the loads but need to
3137 // create a new invariant load equivalence class.
3138 auto &MAs = IAClass.InvariantAccesses;
3139 if (!MAs.empty()) {
3140 auto *LastMA = MAs.front();
3141
3142 isl::set AR = MA->getAccessRelation().range();
3143 isl::set LastAR = LastMA->getAccessRelation().range();
3144 bool SameAR = AR.is_equal(LastAR);
3145
3146 if (!SameAR)
3147 continue;
3148 }
3149
3150 // Add MA to the list of accesses that are in this class.
3151 MAs.push_front(MA);
3152
3153 Consolidated = true;
3154
3155 // Unify the execution context of the class and this statement.
3156 isl::set IAClassDomainCtx = IAClass.ExecutionContext;
3157 if (!IAClassDomainCtx.is_null())
3158 IAClassDomainCtx = IAClassDomainCtx.unite(MACtx).coalesce();
3159 else
3160 IAClassDomainCtx = MACtx;
3161 IAClass.ExecutionContext = IAClassDomainCtx;
3162 break;
3163 }
3164
3165 if (Consolidated)
3166 continue;
3167
3168 MACtx = MACtx.coalesce();
3169
3170 // If we did not consolidate MA, thus did not find an equivalence class
3171 // for it, we create a new one.
3172 scop->addInvariantEquivClass(
3173 InvariantEquivClassTy{PointerSCEV, MemoryAccessList{MA}, MACtx, Ty});
3174 }
3175}
3176
3177/// Find the canonical scop array info object for a set of invariant load
3178/// hoisted loads. The canonical array is the one that corresponds to the
3179/// first load in the list of accesses which is used as base pointer of a
3180/// scop array.
3182 MemoryAccessList &Accesses) {
3183 for (MemoryAccess *Access : Accesses) {
3184 const ScopArrayInfo *CanonicalArray = S.getScopArrayInfoOrNull(
3185 Access->getAccessInstruction(), MemoryKind::Array);
3186 if (CanonicalArray)
3187 return CanonicalArray;
3188 }
3189 return nullptr;
3190}
3191
3192/// Check if @p Array severs as base array in an invariant load.
3194 for (InvariantEquivClassTy &EqClass2 : S.getInvariantAccesses())
3195 for (MemoryAccess *Access2 : EqClass2.InvariantAccesses)
3196 if (Access2->getScopArrayInfo() == Array)
3197 return true;
3198 return false;
3199}
3200
3201/// Replace the base pointer arrays in all memory accesses referencing @p Old,
3202/// with a reference to @p New.
3203static void replaceBasePtrArrays(Scop &S, const ScopArrayInfo *Old,
3204 const ScopArrayInfo *New) {
3205 for (ScopStmt &Stmt : S)
3206 for (MemoryAccess *Access : Stmt) {
3207 if (Access->getLatestScopArrayInfo() != Old)
3208 continue;
3209
3210 isl::id Id = New->getBasePtrId();
3211 isl::map Map = Access->getAccessRelation();
3212 Map = Map.set_tuple_id(isl::dim::out, Id);
3213 Access->setAccessRelation(Map);
3214 }
3215}
3216
3218 for (InvariantEquivClassTy &EqClass : scop->InvariantEquivClasses) {
3219 MemoryAccessList &BasePtrAccesses = EqClass.InvariantAccesses;
3220
3221 const ScopArrayInfo *CanonicalBasePtrSAI =
3222 findCanonicalArray(*scop, BasePtrAccesses);
3223
3224 if (!CanonicalBasePtrSAI)
3225 continue;
3226
3227 for (MemoryAccess *BasePtrAccess : BasePtrAccesses) {
3228 const ScopArrayInfo *BasePtrSAI = scop->getScopArrayInfoOrNull(
3229 BasePtrAccess->getAccessInstruction(), MemoryKind::Array);
3230 if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI ||
3231 !BasePtrSAI->isCompatibleWith(CanonicalBasePtrSAI))
3232 continue;
3233
3234 // we currently do not canonicalize arrays where some accesses are
3235 // hoisted as invariant loads. If we would, we need to update the access
3236 // function of the invariant loads as well. However, as this is not a
3237 // very common situation, we leave this for now to avoid further
3238 // complexity increases.
3239 if (isUsedForIndirectHoistedLoad(*scop, BasePtrSAI))
3240 continue;
3241
3242 replaceBasePtrArrays(*scop, BasePtrSAI, CanonicalBasePtrSAI);
3243 }
3244 }
3245}
3246
3248 for (MemoryAccess *Access : Stmt.MemAccs) {
3249 Type *ElementType = Access->getElementType();
3250
3251 MemoryKind Ty;
3252 if (Access->isPHIKind())
3253 Ty = MemoryKind::PHI;
3254 else if (Access->isExitPHIKind())
3256 else if (Access->isValueKind())
3257 Ty = MemoryKind::Value;
3258 else
3259 Ty = MemoryKind::Array;
3260
3261 // Create isl::pw_aff for SCEVs which describe sizes. Collect all
3262 // assumptions which are taken. isl::pw_aff objects are cached internally
3263 // and they are used later by scop.
3264 for (const SCEV *Size : Access->Sizes) {
3265 if (!Size)
3266 continue;
3267 scop->getPwAff(Size, nullptr, false, &RecordedAssumptions);
3268 }
3269 auto *SAI = scop->getOrCreateScopArrayInfo(Access->getOriginalBaseAddr(),
3270 ElementType, Access->Sizes, Ty);
3271
3272 // Create isl::pw_aff for SCEVs which describe subscripts. Collect all
3273 // assumptions which are taken. isl::pw_aff objects are cached internally
3274 // and they are used later by scop.
3275 for (const SCEV *Subscript : Access->subscripts()) {
3276 if (!Access->isAffine() || !Subscript)
3277 continue;
3278 scop->getPwAff(Subscript, Stmt.getEntryBlock(), false,
3280 }
3281 Access->buildAccessRelation(SAI);
3282 scop->addAccessData(Access);
3283 }
3284}
3285
3286/// Add the minimal/maximal access in @p Set to @p User.
3287///
3288/// @return True if more accesses should be added, false if we reached the
3289/// maximal number of run-time checks to be generated.
3291 Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S) {
3292 isl::pw_multi_aff MinPMA, MaxPMA;
3293 isl::pw_aff LastDimAff;
3294 isl::aff OneAff;
3295 unsigned Pos;
3296
3297 Set = Set.remove_divs();
3298 polly::simplify(Set);
3299
3300 if (Set.is_null())
3301 return false;
3302
3304 Set = Set.simple_hull();
3305
3306 // Restrict the number of parameters involved in the access as the lexmin/
3307 // lexmax computation will take too long if this number is high.
3308 //
3309 // Experiments with a simple test case using an i7 4800MQ:
3310 //
3311 // #Parameters involved | Time (in sec)
3312 // 6 | 0.01
3313 // 7 | 0.04
3314 // 8 | 0.12
3315 // 9 | 0.40
3316 // 10 | 1.54
3317 // 11 | 6.78
3318 // 12 | 30.38
3319 //
3320 if (isl_set_n_param(Set.get()) >
3321 static_cast<isl_size>(RunTimeChecksMaxParameters)) {
3322 unsigned InvolvedParams = 0;
3323 for (unsigned u = 0, e = isl_set_n_param(Set.get()); u < e; u++)
3324 if (Set.involves_dims(isl::dim::param, u, 1))
3325 InvolvedParams++;
3326
3327 if (InvolvedParams > RunTimeChecksMaxParameters)
3328 return false;
3329 }
3330
3331 MinPMA = Set.lexmin_pw_multi_aff();
3332 MaxPMA = Set.lexmax_pw_multi_aff();
3333
3334 MinPMA = MinPMA.coalesce();
3335 MaxPMA = MaxPMA.coalesce();
3336
3337 if (MaxPMA.is_null())
3338 return false;
3339
3340 unsigned MaxOutputSize = unsignedFromIslSize(MaxPMA.dim(isl::dim::out));
3341
3342 // Adjust the last dimension of the maximal access by one as we want to
3343 // enclose the accessed memory region by MinPMA and MaxPMA. The pointer
3344 // we test during code generation might now point after the end of the
3345 // allocated array but we will never dereference it anyway.
3346 assert(MaxOutputSize >= 1 && "Assumed at least one output dimension");
3347
3348 Pos = MaxOutputSize - 1;
3349 LastDimAff = MaxPMA.at(Pos);
3350 OneAff = isl::aff(isl::local_space(LastDimAff.get_domain_space()));
3351 OneAff = OneAff.add_constant_si(1);
3352 LastDimAff = LastDimAff.add(OneAff);
3353 MaxPMA = MaxPMA.set_pw_aff(Pos, LastDimAff);
3354
3355 if (MinPMA.is_null() || MaxPMA.is_null())
3356 return false;
3357
3358 MinMaxAccesses.push_back(std::make_pair(MinPMA, MaxPMA));
3359
3360 return true;
3361}
3362
3363/// Wrapper function to calculate minimal/maximal accesses to each array.
3365 Scop::MinMaxVectorTy &MinMaxAccesses) {
3366 MinMaxAccesses.reserve(AliasGroup.size());
3367
3368 isl::union_set Domains = scop->getDomains();
3369 isl::union_map Accesses = isl::union_map::empty(scop->getIslCtx());
3370
3371 for (MemoryAccess *MA : AliasGroup)
3372 Accesses = Accesses.unite(MA->getAccessRelation());
3373
3374 Accesses = Accesses.intersect_domain(Domains);
3375 isl::union_set Locations = Accesses.range();
3376
3377 bool LimitReached = false;
3378 for (isl::set Set : Locations.get_set_list()) {
3379 LimitReached |= !buildMinMaxAccess(Set, MinMaxAccesses, *scop);
3380 if (LimitReached)
3381 break;
3382 }
3383
3384 return !LimitReached;
3385}
3386
3389 Domain = Domain.project_out(isl::dim::set, 0,
3390 unsignedFromIslSize(Domain.tuple_dim()));
3391 return Domain.reset_tuple_id();
3392}
3393
3396 return true;
3397
3398 if (buildAliasGroups()) {
3399 // Aliasing assumptions do not go through addAssumption but we still want to
3400 // collect statistics so we do it here explicitly.
3401 if (scop->getAliasGroups().size())
3403 return true;
3404 }
3405
3406 // If a problem occurs while building the alias groups we need to delete
3407 // this SCoP and pretend it wasn't valid in the first place. To this end
3408 // we make the assumed context infeasible.
3409 scop->invalidate(ALIASING, DebugLoc());
3410
3411 POLLY_DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << scop->getNameStr()
3412 << " could not be created. This SCoP has been dismissed.");
3413 return false;
3414}
3415
3416std::tuple<ScopBuilder::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>>
3418 BatchAAResults BAA(AA);
3419 AliasSetTracker AST(BAA);
3420
3421 DenseMap<Value *, MemoryAccess *> PtrToAcc;
3422 DenseSet<const ScopArrayInfo *> HasWriteAccess;
3423 for (ScopStmt &Stmt : *scop) {
3424
3425 isl::set StmtDomain = Stmt.getDomain();
3426 bool StmtDomainEmpty = StmtDomain.is_empty();
3427
3428 // Statements with an empty domain will never be executed.
3429 if (StmtDomainEmpty)
3430 continue;
3431
3432 for (MemoryAccess *MA : Stmt) {
3433 if (MA->isScalarKind())
3434 continue;
3435 if (!MA->isRead())
3436 HasWriteAccess.insert(MA->getScopArrayInfo());
3437 MemAccInst Acc(MA->getAccessInstruction());
3438 if (MA->isRead() && isa<MemTransferInst>(Acc))
3439 PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA;
3440 else
3441 PtrToAcc[Acc.getPointerOperand()] = MA;
3442 AST.add(Acc);
3443 }
3444 }
3445
3446 AliasGroupVectorTy AliasGroups;
3447 for (AliasSet &AS : AST) {
3448 if (AS.isMustAlias() || AS.isForwardingAliasSet())
3449 continue;
3450 AliasGroupTy AG;
3451 for (const Value *Ptr : AS.getPointers())
3452 AG.push_back(PtrToAcc[const_cast<Value *>(Ptr)]);
3453 if (AG.size() < 2)
3454 continue;
3455 AliasGroups.push_back(std::move(AG));
3456 }
3457
3458 return std::make_tuple(AliasGroups, HasWriteAccess);
3459}
3460
3462 // To create sound alias checks we perform the following steps:
3463 // o) We partition each group into read only and non read only accesses.
3464 // o) For each group with more than one base pointer we then compute minimal
3465 // and maximal accesses to each array of a group in read only and non
3466 // read only partitions separately.
3467 AliasGroupVectorTy AliasGroups;
3468 DenseSet<const ScopArrayInfo *> HasWriteAccess;
3469
3470 std::tie(AliasGroups, HasWriteAccess) = buildAliasGroupsForAccesses();
3471
3472 splitAliasGroupsByDomain(AliasGroups);
3473
3474 for (AliasGroupTy &AG : AliasGroups) {
3475 if (!scop->hasFeasibleRuntimeContext())
3476 return false;
3477
3478 {
3479 IslMaxOperationsGuard MaxOpGuard(scop->getIslCtx().get(), OptComputeOut);
3480 bool Valid = buildAliasGroup(AG, HasWriteAccess);
3481 if (!Valid)
3482 return false;
3483 }
3484 if (isl_ctx_last_error(scop->getIslCtx().get()) == isl_error_quota) {
3485 scop->invalidate(COMPLEXITY, DebugLoc());
3486 return false;
3487 }
3488 }
3489
3490 return true;
3491}
3492
3494 AliasGroupTy &AliasGroup, DenseSet<const ScopArrayInfo *> HasWriteAccess) {
3495 AliasGroupTy ReadOnlyAccesses;
3496 AliasGroupTy ReadWriteAccesses;
3497 SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays;
3498 SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays;
3499
3500 if (AliasGroup.size() < 2)
3501 return true;
3502
3503 for (MemoryAccess *Access : AliasGroup) {
3504 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "PossibleAlias",
3505 Access->getAccessInstruction())
3506 << "Possibly aliasing pointer, use restrict keyword.");
3507 const ScopArrayInfo *Array = Access->getScopArrayInfo();
3508 if (HasWriteAccess.count(Array)) {
3509 ReadWriteArrays.insert(Array);
3510 ReadWriteAccesses.push_back(Access);
3511 } else {
3512 ReadOnlyArrays.insert(Array);
3513 ReadOnlyAccesses.push_back(Access);
3514 }
3515 }
3516
3517 // If there are no read-only pointers, and less than two read-write pointers,
3518 // no alias check is needed.
3519 if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1)
3520 return true;
3521
3522 // If there is no read-write pointer, no alias check is needed.
3523 if (ReadWriteArrays.empty())
3524 return true;
3525
3526 // For non-affine accesses, no alias check can be generated as we cannot
3527 // compute a sufficiently tight lower and upper bound: bail out.
3528 for (MemoryAccess *MA : AliasGroup) {
3529 if (!MA->isAffine()) {
3530 scop->invalidate(ALIASING, MA->getAccessInstruction()->getDebugLoc(),
3531 MA->getAccessInstruction()->getParent());
3532 return false;
3533 }
3534 }
3535
3536 // Ensure that for all memory accesses for which we generate alias checks,
3537 // their base pointers are available.
3538 for (MemoryAccess *MA : AliasGroup) {
3539 if (MemoryAccess *BasePtrMA = scop->lookupBasePtrAccess(MA))
3540 scop->addRequiredInvariantLoad(
3541 cast<LoadInst>(BasePtrMA->getAccessInstruction()));
3542 }
3543
3544 // scop->getAliasGroups().emplace_back();
3545 // Scop::MinMaxVectorPairTy &pair = scop->getAliasGroups().back();
3546 Scop::MinMaxVectorTy MinMaxAccessesReadWrite;
3547 Scop::MinMaxVectorTy MinMaxAccessesReadOnly;
3548
3549 bool Valid;
3550
3551 Valid = calculateMinMaxAccess(ReadWriteAccesses, MinMaxAccessesReadWrite);
3552
3553 if (!Valid)
3554 return false;
3555
3556 // Bail out if the number of values we need to compare is too large.
3557 // This is important as the number of comparisons grows quadratically with
3558 // the number of values we need to compare.
3559 if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() >
3561 return false;
3562
3563 Valid = calculateMinMaxAccess(ReadOnlyAccesses, MinMaxAccessesReadOnly);
3564
3565 scop->addAliasGroup(MinMaxAccessesReadWrite, MinMaxAccessesReadOnly);
3566 if (!Valid)
3567 return false;
3568
3569 return true;
3570}
3571
3573 for (unsigned u = 0; u < AliasGroups.size(); u++) {
3574 AliasGroupTy NewAG;
3575 AliasGroupTy &AG = AliasGroups[u];
3576 AliasGroupTy::iterator AGI = AG.begin();
3577 isl::set AGDomain = getAccessDomain(*AGI);
3578 while (AGI != AG.end()) {
3579 MemoryAccess *MA = *AGI;
3580 isl::set MADomain = getAccessDomain(MA);
3581 if (AGDomain.is_disjoint(MADomain)) {
3582 NewAG.push_back(MA);
3583 AGI = AG.erase(AGI);
3584 } else {
3585 AGDomain = AGDomain.unite(MADomain);
3586 AGI++;
3587 }
3588 }
3589 if (NewAG.size() > 1)
3590 AliasGroups.push_back(std::move(NewAG));
3591 }
3592}
3593
3594#ifndef NDEBUG
3595static void verifyUse(Scop *S, Use &Op, LoopInfo &LI) {
3596 auto PhysUse = VirtualUse::create(S, Op, &LI, false);
3597 auto VirtUse = VirtualUse::create(S, Op, &LI, true);
3598 assert(PhysUse.getKind() == VirtUse.getKind());
3599}
3600
3601/// Check the consistency of every statement's MemoryAccesses.
3602///
3603/// The check is carried out by expecting the "physical" kind of use (derived
3604/// from the BasicBlocks instructions resides in) to be same as the "virtual"
3605/// kind of use (derived from a statement's MemoryAccess).
3606///
3607/// The "physical" uses are taken by ensureValueRead to determine whether to
3608/// create MemoryAccesses. When done, the kind of scalar access should be the
3609/// same no matter which way it was derived.
3610///
3611/// The MemoryAccesses might be changed by later SCoP-modifying passes and hence
3612/// can intentionally influence on the kind of uses (not corresponding to the
3613/// "physical" anymore, hence called "virtual"). The CodeGenerator therefore has
3614/// to pick up the virtual uses. But here in the code generator, this has not
3615/// happened yet, such that virtual and physical uses are equivalent.
3616static void verifyUses(Scop *S, LoopInfo &LI, DominatorTree &DT) {
3617 for (auto *BB : S->getRegion().blocks()) {
3618 for (auto &Inst : *BB) {
3619 auto *Stmt = S->getStmtFor(&Inst);
3620 if (!Stmt)
3621 continue;
3622
3623 if (isIgnoredIntrinsic(&Inst))
3624 continue;
3625
3626 // Branch conditions are encoded in the statement domains.
3627 if (Inst.isTerminator() && Stmt->isBlockStmt())
3628 continue;
3629
3630 // Verify all uses.
3631 for (auto &Op : Inst.operands())
3632 verifyUse(S, Op, LI);
3633
3634 // Stores do not produce values used by other statements.
3635 if (isa<StoreInst>(Inst))
3636 continue;
3637
3638 // For every value defined in the block, also check that a use of that
3639 // value in the same statement would not be an inter-statement use. It can
3640 // still be synthesizable or load-hoisted, but these kind of instructions
3641 // are not directly copied in code-generation.
3642 auto VirtDef =
3643 VirtualUse::create(S, Stmt, Stmt->getSurroundingLoop(), &Inst, true);
3644 assert(VirtDef.getKind() == VirtualUse::Synthesizable ||
3645 VirtDef.getKind() == VirtualUse::Intra ||
3646 VirtDef.getKind() == VirtualUse::Hoisted);
3647 }
3648 }
3649
3650 if (S->hasSingleExitEdge())
3651 return;
3652
3653 // PHINodes in the SCoP region's exit block are also uses to be checked.
3654 if (!S->getRegion().isTopLevelRegion()) {
3655 for (auto &Inst : *S->getRegion().getExit()) {
3656 if (!isa<PHINode>(Inst))
3657 break;
3658
3659 for (auto &Op : Inst.operands())
3660 verifyUse(S, Op, LI);
3661 }
3662 }
3663}
3664#endif
3665
3666void ScopBuilder::buildScop(Region &R, AssumptionCache &AC) {
3667 scop = Scop::makeScop(R, SE, LI, DT, *SD.getDetectionContext(&R), ORE,
3668 SD.getNextID());
3669
3670 buildStmts(R);
3671
3672 // Create all invariant load instructions first. These are categorized as
3673 // 'synthesizable', therefore are not part of any ScopStmt but need to be
3674 // created somewhere.
3675 const InvariantLoadsSetTy &RIL = scop->getRequiredInvariantLoads();
3676 for (BasicBlock *BB : scop->getRegion().blocks()) {
3677 if (SD.isErrorBlock(*BB, scop->getRegion()))
3678 continue;
3679
3680 for (Instruction &Inst : *BB) {
3681 LoadInst *Load = dyn_cast<LoadInst>(&Inst);
3682 if (!Load)
3683 continue;
3684
3685 if (!RIL.count(Load))
3686 continue;
3687
3688 // Invariant loads require a MemoryAccess to be created in some statement.
3689 // It is not important to which statement the MemoryAccess is added
3690 // because it will later be removed from the ScopStmt again. We chose the
3691 // first statement of the basic block the LoadInst is in.
3692 ArrayRef<ScopStmt *> List = scop->getStmtListFor(BB);
3693 assert(!List.empty());
3694 ScopStmt *RILStmt = List.front();
3695 buildMemoryAccess(Load, RILStmt);
3696 }
3697 }
3699
3700 // In case the region does not have an exiting block we will later (during
3701 // code generation) split the exit block. This will move potential PHI nodes
3702 // from the current exit block into the new region exiting block. Hence, PHI
3703 // nodes that are at this point not part of the region will be.
3704 // To handle these PHI nodes later we will now model their operands as scalar
3705 // accesses. Note that we do not model anything in the exit block if we have
3706 // an exiting block in the region, as there will not be any splitting later.
3707 if (!R.isTopLevelRegion() && !scop->hasSingleExitEdge()) {
3708 for (Instruction &Inst : *R.getExit()) {
3709 PHINode *PHI = dyn_cast<PHINode>(&Inst);
3710 if (!PHI)
3711 break;
3712
3713 buildPHIAccesses(nullptr, PHI, nullptr, true);
3714 }
3715 }
3716
3717 // Create memory accesses for global reads since all arrays are now known.
3718 const SCEV *AF = SE.getConstant(IntegerType::getInt64Ty(SE.getContext()), 0);
3719 for (auto GlobalReadPair : GlobalReads) {
3720 ScopStmt *GlobalReadStmt = GlobalReadPair.first;
3721 Instruction *GlobalRead = GlobalReadPair.second;
3722 for (auto *BP : ArrayBasePointers)
3723 addArrayAccess(GlobalReadStmt, MemAccInst(GlobalRead), MemoryAccess::READ,
3724 BP, BP->getType(), false, {AF}, {nullptr}, GlobalRead);
3725 }
3726
3728
3729 /// A map from basic blocks to their invalid domains.
3730 DenseMap<BasicBlock *, isl::set> InvalidDomainMap;
3731
3732 if (!buildDomains(&R, InvalidDomainMap)) {
3734 dbgs() << "Bailing-out because buildDomains encountered problems\n");
3735 return;
3736 }
3737
3738 addUserAssumptions(AC, InvalidDomainMap);
3739
3740 // Initialize the invalid domain.
3741 for (ScopStmt &Stmt : scop->Stmts)
3742 if (Stmt.isBlockStmt())
3743 Stmt.setInvalidDomain(InvalidDomainMap[Stmt.getEntryBlock()]);
3744 else
3745 Stmt.setInvalidDomain(InvalidDomainMap[getRegionNodeBasicBlock(
3746 Stmt.getRegion()->getNode())]);
3747
3748 // Remove empty statements.
3749 // Exit early in case there are no executable statements left in this scop.
3750 scop->removeStmtNotInDomainMap();
3751 scop->simplifySCoP(false);
3752 if (scop->isEmpty()) {
3753 POLLY_DEBUG(dbgs() << "Bailing-out because SCoP is empty\n");
3754 return;
3755 }
3756
3757 // The ScopStmts now have enough information to initialize themselves.
3758 for (ScopStmt &Stmt : *scop) {
3760
3761 buildDomain(Stmt);
3763
3764 if (DetectReductions)
3765 checkForReductions(Stmt);
3766 }
3767
3768 // Check early for a feasible runtime context.
3769 if (!scop->hasFeasibleRuntimeContext()) {
3771 dbgs() << "Bailing-out because of unfeasible context (early)\n");
3772 return;
3773 }
3774
3775 // Check early for profitability. Afterwards it cannot change anymore,
3776 // only the runtime context could become infeasible.
3777 if (!scop->isProfitable(UnprofitableScalarAccs)) {
3778 scop->invalidate(PROFITABLE, DebugLoc());
3780 dbgs() << "Bailing-out because SCoP is not considered profitable\n");
3781 return;
3782 }
3783
3784 buildSchedule();
3785
3787
3788 scop->realignParams();
3790
3791 // After the context was fully constructed, thus all our knowledge about
3792 // the parameters is in there, we add all recorded assumptions to the
3793 // assumed/invalid context.
3795
3796 scop->simplifyContexts();
3797 if (!buildAliasChecks()) {
3798 POLLY_DEBUG(dbgs() << "Bailing-out because could not build alias checks\n");
3799 return;
3800 }
3801
3805 scop->simplifySCoP(true);
3806
3807 // Check late for a feasible runtime context because profitability did not
3808 // change.
3809 if (!scop->hasFeasibleRuntimeContext()) {
3810 POLLY_DEBUG(dbgs() << "Bailing-out because of unfeasible context (late)\n");
3811 return;
3812 }
3813
3814#ifndef NDEBUG
3815 verifyUses(scop.get(), LI, DT);
3816#endif
3817}
3818
3819ScopBuilder::ScopBuilder(Region *R, AssumptionCache &AC, AAResults &AA,
3820 const DataLayout &DL, DominatorTree &DT, LoopInfo &LI,
3821 ScopDetection &SD, ScalarEvolution &SE,
3822 OptimizationRemarkEmitter &ORE)
3823 : AA(AA), DL(DL), DT(DT), LI(LI), SD(SD), SE(SE), ORE(ORE) {
3824 DebugLoc Beg, End;
3825 auto P = getBBPairForRegion(R);
3826 getDebugLocations(P, Beg, End);
3827
3828 std::string Msg = "SCoP begins here.";
3829 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "ScopEntry", Beg, P.first)
3830 << Msg);
3831
3832 buildScop(*R, AC);
3833
3834 POLLY_DEBUG(dbgs() << *scop);
3835
3836 if (!scop->hasFeasibleRuntimeContext()) {
3837 InfeasibleScops++;
3838 Msg = "SCoP ends here but was dismissed.";
3839 POLLY_DEBUG(dbgs() << "SCoP detected but dismissed\n");
3840 RecordedAssumptions.clear();
3841 scop.reset();
3842 } else {
3843 Msg = "SCoP ends here.";
3844 ++ScopFound;
3845 if (scop->getMaxLoopDepth() > 0)
3846 ++RichScopFound;
3847 }
3848
3849 if (R->isTopLevelRegion())
3850 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "ScopEnd", End, P.first)
3851 << Msg);
3852 else
3853 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "ScopEnd", End, P.second)
3854 << Msg);
3855}
static cl::opt< int > OptComputeOut("polly-dependences-computeout", cl::desc("Bound the dependence analysis by a maximal amount of " "computational steps (0 means no bound)"), cl::Hidden, cl::init(500000), cl::cat(PollyCategory))
#define DEBUG_TYPE
unsigned unsignedFromIslSize(const isl::size &Size)
Check that Size is valid (only on debug builds) and cast it to unsigned.
Definition ISLTools.h:40
llvm::cl::OptionCategory PollyCategory
#define POLLY_DEBUG(X)
Definition PollyDebug.h:23
static cl::opt< int > OptComputeOut("polly-analysis-computeout", cl::desc("Bound the scop analysis by a maximal amount of " "computational steps (0 means no bound)"), cl::Hidden, cl::init(800000), cl::cat(PollyCategory))
static cl::opt< bool > DisableMultiplicativeReductions("polly-disable-multiplicative-reductions", cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::cat(PollyCategory))
static void replaceBasePtrArrays(Scop &S, const ScopArrayInfo *Old, const ScopArrayInfo *New)
Replace the base pointer arrays in all memory accesses referencing Old, with a reference to New.
static std::pair< isl::set, isl::set > partitionSetParts(isl::set S, unsigned Dim)
Compute the (un)bounded parts of S wrt.
static isl::map createNextIterationMap(isl::space SetSpace, unsigned Dim)
}
static isl::set buildConditionSet(ICmpInst::Predicate Pred, isl::pw_aff L, isl::pw_aff R)
Create the conditions under which L Pred R is true.
static const ScopArrayInfo * findCanonicalArray(Scop &S, MemoryAccessList &Accesses)
Find the canonical scop array info object for a set of invariant load hoisted loads.
static isl::set collectBoundedParts(isl::set S)
Add BSet to set BoundedParts if BSet is bounded.
static void joinOrderedPHIs(EquivalenceClasses< Instruction * > &UnionFind, ArrayRef< Instruction * > ModeledInsts)
If the BasicBlock has an edge from itself, ensure that the PHI WRITEs for the incoming values from th...
static cl::opt< std::string > UserContextStr("polly-context", cl::value_desc("isl parameter set"), cl::desc("Provide additional constraints on the context parameters"), cl::init(""), cl::cat(PollyCategory))
static bool isDivisible(const SCEV *Expr, unsigned Size, ScalarEvolution &SE)
Check if Expr is divisible by Size.
static BasicBlock * getRegionNodeSuccessor(RegionNode *RN, Instruction *TI, unsigned idx)
Return the idx'th block that is executed after RN.
static cl::opt< bool > PollyAllowDereferenceOfAllFunctionParams("polly-allow-dereference-of-all-function-parameters", cl::desc("Treat all parameters to functions that are pointers as dereferencible." " This is useful for invariant load hoisting, since we can generate" " less runtime checks. This is only valid if all pointers to functions" " are always initialized, so that Polly can choose to hoist" " their loads. "), cl::Hidden, cl::init(false), cl::cat(PollyCategory))
static isl::set getAccessDomain(MemoryAccess *MA)
static cl::opt< unsigned > RunTimeChecksMaxArraysPerGroup("polly-rtc-max-arrays-per-group", cl::desc("The maximal number of arrays to compare in each alias group."), cl::Hidden, cl::init(20), cl::cat(PollyCategory))
static bool isAccessRangeTooComplex(isl::set AccessRange)
Check if an access range is too complex.
static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp)
Return the reduction type for a given binary operator.
static bool isUsedForIndirectHoistedLoad(Scop &S, const ScopArrayInfo *Array)
Check if Array severs as base array in an invariant load.
static cl::opt< bool, true > XModelReadOnlyScalars("polly-analyze-read-only-scalars", cl::desc("Model read-only scalar values in the scop description"), cl::location(ModelReadOnlyScalars), cl::Hidden, cl::init(true), cl::cat(PollyCategory))
static bool isAParameter(llvm::Value *maybeParam, const Function &F)
static isl::schedule combineInSequence(isl::schedule Prev, isl::schedule Succ)
static void joinOrderedInstructions(EquivalenceClasses< Instruction * > &UnionFind, ArrayRef< Instruction * > ModeledInsts)
Ensure that the order of ordered instructions does not change.
static cl::opt< unsigned > RunTimeChecksMaxAccessDisjuncts("polly-rtc-max-array-disjuncts", cl::desc("The maximal number of disjunts allowed in memory accesses to " "to build RTCs."), cl::Hidden, cl::init(8), cl::cat(PollyCategory))
GranularityChoice
static void joinOperandTree(EquivalenceClasses< Instruction * > &UnionFind, ArrayRef< Instruction * > ModeledInsts)
Join instructions to the same statement if one uses the scalar result of the other.
bool hasIntersectingAccesses(isl::set AllAccs, MemoryAccess *LoadMA, MemoryAccess *StoreMA, isl::set Domain, SmallVector< MemoryAccess *, 8 > &MemAccs)
True if AllAccs intersects with MemAccs except LoadMA and StoreMA.
static cl::opt< bool > DetectReductions("polly-detect-reductions", cl::desc("Detect and exploit reductions"), cl::Hidden, cl::init(true), cl::cat(PollyCategory))
static std::string makeStmtName(BasicBlock *BB, long BBIdx, int Count, bool IsMain, bool IsLast=false)
Generate a name for a statement.
static BasicBlock * getRegionNodeBasicBlock(RegionNode *RN)
Helper to treat non-affine regions and basic blocks the same.
static cl::opt< GranularityChoice > StmtGranularity("polly-stmt-granularity", cl::desc("Algorithm to use for splitting basic blocks into multiple statements"), cl::values(clEnumValN(GranularityChoice::BasicBlocks, "bb", "One statement per basic block"), clEnumValN(GranularityChoice::ScalarIndependence, "scalar-indep", "Scalar independence heuristic"), clEnumValN(GranularityChoice::Stores, "store", "Store-level granularity")), cl::init(GranularityChoice::ScalarIndependence), cl::cat(PollyCategory))
static bool containsErrorBlock(RegionNode *RN, const Region &R, ScopDetection *SD)
static void verifyUse(Scop *S, Use &Op, LoopInfo &LI)
STATISTIC(ScopFound, "Number of valid Scops")
static unsigned const MaxDimensionsInAccessRange
static bool buildMinMaxAccess(isl::set Set, Scop::MinMaxVectorTy &MinMaxAccesses, Scop &S)
Add the minimal/maximal access in Set to User.
static isl::multi_union_pw_aff mapToDimension(isl::union_set USet, unsigned N)
static void verifyUses(Scop *S, LoopInfo &LI, DominatorTree &DT)
Check the consistency of every statement's MemoryAccesses.
static MemoryAccess::ReductionType combineReductionType(MemoryAccess::ReductionType RT0, MemoryAccess::ReductionType RT1)
Combine two reduction types.
static bool isOrderedInstruction(Instruction *Inst)
Is Inst an ordered instruction?
static cl::opt< unsigned > RunTimeChecksMaxParameters("polly-rtc-max-parameters", cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, cl::init(8), cl::cat(PollyCategory))
static cl::opt< bool > UnprofitableScalarAccs("polly-unprofitable-scalar-accs", cl::desc("Count statements with scalar accesses as not optimizable"), cl::Hidden, cl::init(false), cl::cat(PollyCategory))
bool checkCandidatePairAccesses(MemoryAccess *LoadMA, MemoryAccess *StoreMA, isl::set Domain, SmallVector< MemoryAccess *, 8 > &MemAccs)
Test if the accesses of LoadMA and StoreMA can form a reduction.
static cl::opt< bool > PollyIgnoreInbounds("polly-ignore-inbounds", cl::desc("Do not take inbounds assumptions at all"), cl::Hidden, cl::init(false), cl::cat(PollyCategory))
__isl_null isl_pw_aff * isl_pw_aff_free(__isl_take isl_pw_aff *pwaff)
boolean is_equal(const isl::checked::basic_set &bset2) const
bool is_null() const
class size domain_tuple_dim() const
isl::checked::set range() const
isl::checked::space get_space() const
isl::checked::map intersect_domain(isl::checked::set set) const
isl::checked::set domain() const
boolean is_empty() const
isl::checked::map unite(isl::checked::map map2) const
__isl_give isl_map * copy() const &
__isl_give isl_pw_aff * release()
isl::checked::set gt_set(isl::checked::pw_aff pwaff2) const
isl::checked::set le_set(isl::checked::pw_aff pwaff2) const
isl::checked::multi_pw_aff add(const isl::checked::multi_pw_aff &multi2) const
isl::checked::set eq_set(isl::checked::pw_aff pwaff2) const
isl::checked::set lt_set(isl::checked::pw_aff pwaff2) const
isl::checked::set ne_set(isl::checked::pw_aff pwaff2) const
isl::checked::set ge_set(isl::checked::pw_aff pwaff2) const
isl::checked::pw_aff at(int pos) const
isl::checked::pw_multi_aff coalesce() const
isl::checked::schedule_node child(int pos) const
isl::checked::schedule get_schedule() const
isl::checked::schedule_node insert_mark(isl::checked::id mark) const
isl::checked::schedule_node get_root() const
isl::checked::union_set get_domain() const
boolean is_disjoint(const isl::checked::set &set2) const
class size n_basic_set() const
__isl_give isl_set * copy() const &
isl::checked::set complement() const
isl::checked::set intersect(isl::checked::set set2) const
isl::checked::pw_multi_aff lexmax_pw_multi_aff() const
isl::checked::set gist_params(isl::checked::set context) const
isl::checked::set unite(isl::checked::set set2) const
isl::checked::pw_multi_aff lexmin_pw_multi_aff() const
isl::checked::set detect_equalities() const
boolean is_subset(const isl::checked::set &set2) const
isl::checked::set coalesce() const
bool is_null() const
class size tuple_dim() const
boolean is_equal(const isl::checked::set &set2) const
isl::checked::space get_space() const
boolean is_empty() const
isl::checked::set apply(isl::checked::map map) const
__isl_give isl_set * release()
__isl_keep isl_set * get() const
isl::checked::set subtract(isl::checked::set set2) const
static isl::checked::set empty(isl::checked::space space)
isl::checked::basic_set affine_hull() const
isl::checked::set params() const
isl::checked::set project_out_all_params() const
isl::checked::space map_from_set() const
isl::checked::space range() const
isl::checked::union_set range() const
isl::checked::union_map unite(isl::checked::union_map umap2) const
isl::checked::union_map intersect_domain(isl::checked::space space) const
isl::checked::union_map intersect_range(isl::checked::space space) const
isl::checked::set params() const
isl::checked::set_list get_set_list() const
isl::checked::set extract_set(isl::checked::space space) const
isl::checked::space get_space() const
boolean is_empty() const
boolean is_int() const
static isl::constraint alloc_inequality(isl::local_space ls)
static isl::constraint alloc_equality(isl::local_space ls)
static isl::id alloc(isl::ctx ctx, const std::string &name, void *user)
static isl::map universe(isl::space space)
static isl::pw_multi_aff project_out_map(isl::space space, isl::dim type, unsigned int first, unsigned int n)
static isl::schedule from_domain(isl::union_set domain)
static isl::set empty(isl::space space)
static isl::set universe(isl::space space)
static isl::union_map empty(isl::ctx ctx)
static isl::union_pw_multi_aff empty(isl::ctx ctx)
Scoped limit of ISL operations.
Definition GICHelper.h:424
Utility proxy to wrap the common members of LoadInst and StoreInst.
Definition ScopHelper.h:141
llvm::Value * getValueOperand() const
Definition ScopHelper.h:238
bool isLoad() const
Definition ScopHelper.h:311
static MemAccInst dyn_cast(llvm::Value &V)
Definition ScopHelper.h:179
bool isStore() const
Definition ScopHelper.h:312
llvm::Value * getPointerOperand() const
Definition ScopHelper.h:249
Represent memory accesses in statements.
Definition ScopInfo.h:426
void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue)
Add a new incoming block/value pairs for this PHI/ExitPHI access.
Definition ScopInfo.h:731
void dump() const
Print the MemoryAccess to stderr.
Definition ScopInfo.cpp:952
SmallVector< const SCEV *, 4 > Sizes
Size of each dimension of the accessed array.
Definition ScopInfo.h:543
AccessType
The access type of a memory access.
Definition ScopInfo.h:452
ReductionType
Reduction access type.
Definition ScopInfo.h:461
@ RT_BOTTOM
Pseudo type for the data flow analysis.
Definition ScopInfo.h:469
@ RT_BOR
Bitwise Or.
Definition ScopInfo.h:465
@ RT_BAND
Bitwise And.
Definition ScopInfo.h:467
@ RT_ADD
Addition.
Definition ScopInfo.h:463
@ RT_BXOR
Bitwise XOr.
Definition ScopInfo.h:466
@ RT_NONE
Indicate no reduction at all.
Definition ScopInfo.h:462
@ RT_MUL
Multiplication.
Definition ScopInfo.h:464
bool isValueKind() const
Old name of isOriginalValueKind().
Definition ScopInfo.h:981
bool isPHIKind() const
Old name of isOriginalPHIKind.
Definition ScopInfo.h:993
bool isWrite() const
Is this a write memory access?
Definition ScopInfo.h:764
Instruction * getAccessInstruction() const
Return the access instruction of this memory access.
Definition ScopInfo.h:880
iterator_range< SubscriptsTy::const_iterator > subscripts() const
Return an iterator range containing the subscripts.
Definition ScopInfo.h:883
bool isExitPHIKind() const
Old name of isOriginalExitPHIKind().
Definition ScopInfo.h:1009
bool isRead() const
Is this a read memory access?
Definition ScopInfo.h:755
void buildAccessRelation(const ScopArrayInfo *SAI)
Assemble the access relation from all available information.
Definition ScopInfo.cpp:814
bool isScalarKind() const
Old name of isOriginalScalarKind.
Definition ScopInfo.h:968
Type * getElementType() const
Return the element type of the accessed array wrt. this access.
Definition ScopInfo.h:859
const ScopArrayInfo * getScopArrayInfo() const
Legacy name of getOriginalScopArrayInfo().
Definition ScopInfo.h:848
Value * getOriginalBaseAddr() const
Get the original base address of this access (e.g.
Definition ScopInfo.h:828
ScopStmt * getStatement() const
Get the statement that contains this memory access.
Definition ScopInfo.h:1026
bool isAffine() const
Is the memory access affine?
Definition ScopInfo.h:1080
isl::map getAccessRelation() const
Old name of getLatestAccessRelation().
Definition ScopInfo.h:790
bool isMemoryIntrinsic() const
Is this a memory intrinsic access (memcpy, memset, memmove)?
Definition ScopInfo.h:767
A class to store information about arrays in the SCoP.
Definition ScopInfo.h:214
bool isCompatibleWith(const ScopArrayInfo *Array) const
Verify that Array is compatible to this ScopArrayInfo.
Definition ScopInfo.cpp:268
isl::id getBasePtrId() const
Return the isl id for the base pointer.
Definition ScopInfo.cpp:339
void buildDomain(ScopStmt &Stmt)
Build the domain of Stmt.
void propagateDomainConstraintsToRegionExit(BasicBlock *BB, Loop *BBLoop, SmallPtrSetImpl< BasicBlock * > &FinishedExitBlocks, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Propagate domains that are known due to graph properties.
bool isRequiredInvariantLoad(LoadInst *LI) const
Return true if and only if LI is a required invariant load.
bool propagateInvalidStmtDomains(Region *R, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Propagate invalid domains of statements through R.
void ensurePHIWrite(PHINode *PHI, ScopStmt *IncomintStmt, BasicBlock *IncomingBlock, Value *IncomingValue, bool IsExitBlock)
Create a write MemoryAccess for the incoming block of a phi node.
void addInvariantLoads(ScopStmt &Stmt, InvariantAccessesTy &InvMAs)
Add invariant loads listed in InvMAs with the domain of Stmt.
void canonicalizeDynamicBasePtrs()
Canonicalize arrays with base pointers from the same equivalence class.
bool calculateMinMaxAccess(AliasGroupTy AliasGroup, Scop::MinMaxVectorTy &MinMaxAccesses)
Wrapper function to calculate minimal/maximal accesses to each array.
void verifyInvariantLoads()
Verify that all required invariant loads have been hoisted.
void addUserContext()
Add user provided parameter constraints to context (command line).
void ensureValueRead(Value *V, ScopStmt *UserStmt)
Ensure an llvm::Value is available in the BB's statement, creating a MemoryAccess for reloading it if...
struct LoopStackElement { Loop *L; isl::schedule Schedule; unsigned NumBlocksProcessed; LoopStackElement(Loop *L, isl::schedule S, unsigned NumBlocksProcessed) :L(L), Schedule(S), NumBlocksProcessed(NumBlocksProcessed) {} } LoopStackElementTy
A loop stack element to keep track of per-loop information during schedule construction.
void buildPHIAccesses(ScopStmt *PHIStmt, PHINode *PHI, Region *NonAffineSubRegion, bool IsExitBlock=false)
Create MemoryAccesses for the given PHI node in the given region.
void buildSchedule()
Construct the schedule of this SCoP.
SmallVector< std::pair< ScopStmt *, Instruction * >, 16 > GlobalReads
Set of instructions that might read any memory location.
Definition ScopBuilder.h:57
ScalarEvolution & SE
The ScalarEvolution to help building Scop.
Definition ScopBuilder.h:51
void foldAccessRelations()
Fold memory accesses to handle parametric offset.
std::tuple< AliasGroupVectorTy, DenseSet< const ScopArrayInfo * > > buildAliasGroupsForAccesses()
Build alias groups for all memory accesses in the Scop.
bool propagateDomainConstraints(Region *R, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Propagate the domain constraints through the region R.
void addPHIReadAccess(ScopStmt *PHIStmt, PHINode *PHI)
Create a MemoryAccess for reading the value of a phi.
bool buildAccessCallInst(MemAccInst Inst, ScopStmt *Stmt)
Try to build a MemoryAccess for a call instruction.
void buildScalarDependences(ScopStmt *UserStmt, Instruction *Inst)
Analyze and extract the cross-BB scalar dependences (or, dataflow dependencies) of an instruction.
void foldSizeConstantsToRight()
Fold size constants to the right.
SmallSetVector< Value *, 16 > ArrayBasePointers
Set of all accessed array base pointers.
Definition ScopBuilder.h:60
SmallVector< LoopStackElementTy, 4 > LoopStackTy
The loop stack used for schedule construction.
MemoryAccess * addMemoryAccess(ScopStmt *Stmt, Instruction *Inst, MemoryAccess::AccessType AccType, Value *BaseAddress, Type *ElemType, bool Affine, Value *AccessValue, ArrayRef< const SCEV * > Subscripts, ArrayRef< const SCEV * > Sizes, MemoryKind Kind)
Create a new MemoryAccess object and add it to AccFuncMap.
void hoistInvariantLoads()
Hoist invariant memory loads and check for required ones.
isl::pw_aff getPwAff(BasicBlock *BB, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, const SCEV *E, bool NonNegative=false, bool IsInsideDomain=true)
Compute the isl representation for the SCEV E in this BB.
SmallVector< AliasGroupTy, 4 > AliasGroupVectorTy
A vector of alias groups.
AAResults & AA
The AAResults to build AliasSetTracker.
Definition ScopBuilder.h:36
bool buildAccessMultiDimFixed(MemAccInst Inst, ScopStmt *Stmt)
Try to build a multi-dimensional fixed sized MemoryAccess from the Load/Store instruction.
DominatorTree & DT
DominatorTree to reason about guaranteed execution.
Definition ScopBuilder.h:42
const DataLayout & DL
Target data for element size computing.
Definition ScopBuilder.h:39
bool buildAccessMemIntrinsic(MemAccInst Inst, ScopStmt *Stmt)
Try to build a MemoryAccess for a memory intrinsic.
void assumeNoOutOfBounds()
Assume that all memory accesses are within bounds.
isl::set getNonHoistableCtx(MemoryAccess *Access, isl::union_map Writes)
Return the context under which the access cannot be hoisted.
void buildInvariantEquivalenceClasses()
Create equivalence classes for required invariant accesses.
bool buildConditionSets(BasicBlock *BB, Instruction *TI, Loop *L, __isl_keep isl_set *Domain, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, SmallVectorImpl< __isl_give isl_set * > &ConditionSets, bool IsInsideDomain=true)
Build the conditions sets for the terminator TI in the Domain.
bool buildAliasGroups()
Build all alias groups for this SCoP.
void addArrayAccess(ScopStmt *Stmt, MemAccInst MemAccInst, MemoryAccess::AccessType AccType, Value *BaseAddress, Type *ElemType, bool IsAffine, ArrayRef< const SCEV * > Subscripts, ArrayRef< const SCEV * > Sizes, Value *AccessValue)
Create a MemoryAccess that represents either a LoadInst or StoreInst.
isl::set adjustDomainDimensions(isl::set Dom, Loop *OldL, Loop *NewL)
Adjust the dimensions of Dom that was constructed for OldL to be compatible to domains constructed fo...
bool buildAccessMultiDimParam(MemAccInst Inst, ScopStmt *Stmt)
Try to build a multi-dimensional parametric sized MemoryAccess.
void buildEscapingDependences(Instruction *Inst)
Build the escaping dependences for Inst.
void buildEqivClassBlockStmts(BasicBlock *BB)
Create one or more ScopStmts for BB using equivalence classes.
void splitAliasGroupsByDomain(AliasGroupVectorTy &AliasGroups)
Split alias groups by iteration domains.
bool buildAliasGroup(AliasGroupTy &AliasGroup, DenseSet< const ScopArrayInfo * > HasWriteAccess)
Build a given alias group and its access data.
void addUserAssumptions(AssumptionCache &AC, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Add user provided parameter constraints to context (source code).
void checkForReductions(ScopStmt &Stmt)
Check for reductions in Stmt.
bool buildDomains(Region *R, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Compute the domain for each basic block in R.
void buildSequentialBlockStmts(BasicBlock *BB, bool SplitOnStore=false)
Create one or more ScopStmts for BB.
ScopDetection & SD
Valid Regions for Scop.
Definition ScopBuilder.h:48
isl::set buildUnsignedConditionSets(BasicBlock *BB, Value *Condition, const isl::set &Domain, const SCEV *SCEV_TestVal, const SCEV *SCEV_UpperBound, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, bool IsStrictUpperBound, bool IsInsideDomain=true)
Build condition sets for unsigned ICmpInst(s).
bool shouldModelInst(Instruction *Inst, Loop *L)
Should an instruction be modeled in a ScopStmt.
std::unique_ptr< Scop > scop
Definition ScopBuilder.h:63
void buildMemoryAccess(MemAccInst Inst, ScopStmt *Stmt)
Build an instance of MemoryAccess from the Load/Store instruction.
bool buildAliasChecks()
Build the alias checks for this SCoP.
void updateAccessDimensionality()
Update access dimensionalities.
void addRecordedAssumptions()
Add all recorded assumptions to the assumed context.
void buildAccessRelations(ScopStmt &Stmt)
Build the access relation of all memory accesses of Stmt.
RecordedAssumptionsTy RecordedAssumptions
Collection to hold taken assumptions.
Definition ScopBuilder.h:75
bool hasNonHoistableBasePtrInScop(MemoryAccess *MA, isl::union_map Writes)
Check if the base ptr of MA is in the SCoP but not hoistable.
bool addLoopBoundsToHeaderDomain(Loop *L, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Add loop carried constraints to the header block of the loop L.
bool buildDomainsWithBranchConstraints(Region *R, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap)
Compute the branching constraints for each basic block in R.
void buildAccessFunctions()
Build the access functions for the subregion SR.
bool canAlwaysBeHoisted(MemoryAccess *MA, bool StmtInvalidCtxIsEmpty, bool MAInvalidCtxIsEmpty, bool NonHoistableCtxIsEmpty)
Check if MA can always be hoisted without execution context.
bool buildAccessSingleDim(MemAccInst Inst, ScopStmt *Stmt)
Build a single-dimensional parametric sized MemoryAccess from the Load/Store instruction.
void collectSurroundingLoops(ScopStmt &Stmt)
Fill NestLoops with loops surrounding Stmt.
void finalizeAccesses()
Finalize all access relations.
void buildScop(Region &R, AssumptionCache &AC)
LoopInfo & LI
LoopInfo for information about loops.
Definition ScopBuilder.h:45
OptimizationRemarkEmitter & ORE
An optimization diagnostic interface to add optimization remarks.
Definition ScopBuilder.h:54
void buildStmts(Region &SR)
Create ScopStmt for all BBs and non-affine subregions of SR.
void ensureValueWrite(Instruction *Inst)
Create a MemoryAccess for writing an llvm::Instruction.
SmallVector< MemoryAccess *, 4 > AliasGroupTy
A vector of memory accesses that belong to an alias group.
isl::set getPredecessorDomainConstraints(BasicBlock *BB, isl::set Domain)
Compute the union of predecessor domains for BB.
ScopBuilder(Region *R, AssumptionCache &AC, AAResults &AA, const DataLayout &DL, DominatorTree &DT, LoopInfo &LI, ScopDetection &SD, ScalarEvolution &SE, OptimizationRemarkEmitter &ORE)
Pass to detect the maximal static control parts (Scops) of a function.
bool isErrorBlock(llvm::BasicBlock &BB, const llvm::Region &R)
Check if the block is a error block.
Statement of the Scop.
Definition ScopInfo.h:1135
MemoryAccess & getArrayAccessFor(const Instruction *Inst) const
Return the only array access for Inst.
Definition ScopInfo.h:1429
Scop * getParent()
Definition ScopInfo.h:1523
BasicBlock * getEntryBlock() const
Return a BasicBlock from this statement.
isl::set Domain
The iteration domain describes the set of iterations for which this statement is executed.
Definition ScopInfo.h:1202
const std::vector< Instruction * > & getInstructions() const
Definition ScopInfo.h:1526
bool isBlockStmt() const
Return true if this statement represents a single basic block.
Definition ScopInfo.h:1316
isl::set getInvalidContext() const
Get the invalid context for this statement.
Definition ScopInfo.h:1304
SmallVector< Loop *, 4 > NestLoops
Definition ScopInfo.h:1253
Region * getRegion() const
Get the region represented by this ScopStmt (if any).
Definition ScopInfo.h:1325
bool represents(BasicBlock *BB) const
Return whether this statement represents BB.
Definition ScopInfo.h:1346
BasicBlock * getBasicBlock() const
Get the BasicBlock represented by this ScopStmt (if any).
Definition ScopInfo.h:1313
MemoryAccessVec MemAccs
The memory accesses of this statement.
Definition ScopInfo.h:1207
const char * getBaseName() const
bool contains(const Loop *L) const
Return whether L is boxed within this statement.
Definition ScopInfo.h:1337
void addAccess(MemoryAccess *Access, bool Prepend=false)
Add Access to this statement's list of accesses.
bool isRegionStmt() const
Return true if this statement represents a whole region.
Definition ScopInfo.h:1328
void setInvalidDomain(isl::set ID)
Set the invalid context for this statement to ID.
isl::set getDomain() const
Get the iteration domain of this ScopStmt.
MemoryAccess * lookupValueWriteOf(Instruction *Inst) const
Return the MemoryAccess that writes the value of an instruction defined in this statement,...
Definition ScopInfo.h:1439
Loop * getSurroundingLoop() const
Return the closest innermost loop that contains this statement, but is not contained in it.
Definition ScopInfo.h:1376
MemoryAccess * lookupPHIWriteOf(PHINode *PHI) const
Return the PHI write MemoryAccess for the incoming values from any basic block in this ScopStmt,...
Definition ScopInfo.h:1460
MemoryAccess * lookupValueReadOf(Value *Inst) const
Return the MemoryAccess that reloads a value, or nullptr if not existing, respectively not yet added.
Definition ScopInfo.h:1447
Static Control Part.
Definition ScopInfo.h:1625
SmallVector< MinMaxAccessTy, 4 > MinMaxVectorTy
Vector of minimal/maximal accesses to different arrays.
Definition ScopInfo.h:1631
static void incrementNumberOfAliasingAssumptions(unsigned Step)
Increment actual number of aliasing assumptions taken.
static std::unique_ptr< Scop > makeScop(Region &R, ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT, ScopDetection::DetectionContext &DC, OptimizationRemarkEmitter &ORE, int ID)
Factory pattern for creating a new (empty) SCoP.
const Region & getRegion() const
Get the maximum region of this static control part.
Definition ScopInfo.h:2086
static VirtualUse create(Scop *S, const Use &U, LoopInfo *LI, bool Virtual)
Get a VirtualUse for an llvm::Use.
enum isl_error isl_ctx_last_error(isl_ctx *ctx)
Definition isl_ctx.c:333
@ isl_error_quota
Definition ctx.h:82
#define __isl_keep
Definition ctx.h:26
int isl_size
Definition ctx.h:98
__isl_null isl_id * isl_id_free(__isl_take isl_id *id)
Definition isl_id.c:207
void * isl_id_get_user(__isl_keep isl_id *id)
Definition isl_id.c:36
#define S(TYPE, NAME)
#define isl_set
#define C(FN,...)
Definition isl_test2.cc:266
#define assert(exp)
boolean manage(isl_bool val)
Definition cpp-checked.h:98
aff manage_copy(__isl_keep isl_aff *ptr)
std::forward_list< MemoryAccess * > MemoryAccessList
Ordered list type to hold accesses.
Definition ScopInfo.h:1086
std::pair< isl::pw_aff, isl::set > PWACtx
The result type of the SCEVAffinator.
llvm::Loop * getRegionNodeLoop(llvm::RegionNode *RN, llvm::LoopInfo &LI)
Return the smallest loop surrounding RN.
bool isAffineConstraint(llvm::Value *V, const llvm::Region *R, llvm::Loop *Scope, llvm::ScalarEvolution &SE, ParameterSetTy &Params, bool OrExpr=false)
Check if V describes an affine constraint in R.
unsigned const MaxDisjunctsInDomain
Definition ScopInfo.cpp:115
std::string getIslCompatibleName(const std::string &Prefix, const llvm::Value *Val, long Number, const std::string &Suffix, bool UseInstructionNames)
Combine Prefix, Val (or Number) and Suffix to an isl-compatible name.
void findValues(const llvm::SCEV *Expr, llvm::ScalarEvolution &SE, llvm::SetVector< llvm::Value * > &Values)
Find the values referenced by SCEVUnknowns in a given SCEV expression.
void findLoops(const llvm::SCEV *Expr, llvm::SetVector< const llvm::Loop * > &Loops)
Find the loops referenced from a SCEV expression.
SmallVector< InvariantAccess, 8 > InvariantAccessesTy
Ordered container type to hold invariant accesses.
Definition ScopInfo.h:1098
llvm::SetVector< llvm::AssertingVH< llvm::LoadInst > > InvariantLoadsSetTy
Type for a set of invariant loads.
Definition ScopHelper.h:110
llvm::SetVector< const llvm::SCEV * > ParameterSetTy
Set type for parameters.
Definition ScopHelper.h:113
bool isAffineExpr(const llvm::Region *R, llvm::Loop *Scope, const llvm::SCEV *Expression, llvm::ScalarEvolution &SE, InvariantLoadsSetTy *ILS=nullptr)
unsigned getNumBlocksInRegionNode(llvm::RegionNode *RN)
Get the number of blocks in RN.
llvm::Loop * getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI, const BoxedLoopsSetTy &BoxedLoops)
void getDebugLocations(const BBPair &P, DebugLoc &Begin, DebugLoc &End)
Set the begin and end source location for the region limited by P.
AssumptionSign
Enum to distinguish between assumptions and restrictions.
Definition ScopHelper.h:58
@ AS_RESTRICTION
Definition ScopHelper.h:58
@ AS_ASSUMPTION
Definition ScopHelper.h:58
MemoryKind
The different memory kinds used in Polly.
Definition ScopInfo.h:95
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
Definition ScopInfo.h:110
@ Value
MemoryKind::Value: Models an llvm::Value.
Definition ScopInfo.h:149
@ PHI
MemoryKind::PHI: Models PHI nodes within the SCoP.
Definition ScopInfo.h:186
@ ExitPHI
MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block.
Definition ScopInfo.h:196
bool hasDisableAllTransformsHint(llvm::Loop *L)
Does the loop's LoopID contain a 'llvm.loop.disable_heuristics' property?
const llvm::SCEV * tryForwardThroughPHI(const llvm::SCEV *Expr, llvm::Region &R, llvm::ScalarEvolution &SE, ScopDetection *SD)
Try to look through PHI nodes, where some incoming edges come from error blocks.
bool isDebugCall(llvm::Instruction *Inst)
Is the given instruction a call to a debug function?
llvm::iota_range< unsigned > rangeIslSize(unsigned Begin, isl::size End)
Check that End is valid and return an iterator from Begin to End.
Definition ISLTools.cpp:597
void simplify(isl::set &Set)
Simplify a set inplace.
Definition ISLTools.cpp:289
BBPair getBBPairForRegion(const Region *R)
Return the region delimiters (entry & exit block) of R.
llvm::Loop * getLoopSurroundingScop(Scop &S, llvm::LoopInfo &LI)
Get the smallest loop that contains S but is not in S.
bool UseInstructionNames
Definition ScopInfo.cpp:153
void recordAssumption(RecordedAssumptionsTy *RecordedAssumptions, AssumptionKind Kind, isl::set Set, llvm::DebugLoc Loc, AssumptionSign Sign, llvm::BasicBlock *BB=nullptr, bool RTC=true)
Record an assumption for later addition to the assumed context.
std::pair< const llvm::SCEVConstant *, const llvm::SCEV * > extractConstantFactor(const llvm::SCEV *M, llvm::ScalarEvolution &SE)
Extract the constant factors from the multiplication M.
bool ModelReadOnlyScalars
Command line switch whether to model read-only accesses.
isl::id createIslLoopAttr(isl::ctx Ctx, llvm::Loop *L)
Create an isl::id that identifies an original loop.
bool PollyUseRuntimeAliasChecks
bool PollyDelinearize
@ INFINITELOOP
Definition ScopHelper.h:52
@ ERRORBLOCK
Definition ScopHelper.h:50
@ INVARIANTLOAD
Definition ScopHelper.h:53
@ COMPLEXITY
Definition ScopHelper.h:51
@ ALIASING
Definition ScopHelper.h:45
@ PROFITABLE
Definition ScopHelper.h:49
@ INBOUNDS
Definition ScopHelper.h:46
@ DELINEARIZATION
Definition ScopHelper.h:54
llvm::Value * getUniqueNonErrorValue(llvm::PHINode *PHI, llvm::Region *R, ScopDetection *SD)
Return a unique non-error block incoming value for PHI if available.
bool PollyInvariantLoadHoisting
bool isIgnoredIntrinsic(const llvm::Value *V)
Return true iff V is an intrinsic that we ignore during code generation.
bool canSynthesize(const llvm::Value *V, const Scop &S, llvm::ScalarEvolution *SE, llvm::Loop *Scope)
Check whether a value an be synthesized by the code generator.
llvm::APInt APIntFromVal(__isl_take isl_val *Val)
Translate isl_val to llvm::APInt.
Definition GICHelper.cpp:51
unsigned getNumBlocksInLoop(llvm::Loop *L)
Get the number of blocks in L.
__isl_export __isl_give isl_set * isl_set_universe(__isl_take isl_space *space)
Definition isl_map.c:6985
__isl_export __isl_give isl_set * isl_set_coalesce(__isl_take isl_set *set)
__isl_export __isl_give isl_set * isl_set_subtract(__isl_take isl_set *set1, __isl_take isl_set *set2)
__isl_export __isl_give isl_space * isl_set_get_space(__isl_keep isl_set *set)
Definition isl_map.c:604
__isl_export __isl_give isl_set * isl_set_union(__isl_take isl_set *set1, __isl_take isl_set *set2)
Definition isl_map.c:8929
isl_size isl_set_n_param(__isl_keep isl_set *set)
Definition isl_map.c:228
__isl_export __isl_give isl_set * isl_set_complement(__isl_take isl_set *set)
__isl_null isl_set * isl_set_free(__isl_take isl_set *set)
Definition isl_map.c:4055
__isl_give isl_set * isl_set_copy(__isl_keep isl_set *set)
Definition isl_map.c:1470
__isl_give isl_set * isl_set_project_out(__isl_take isl_set *set, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:5241
__isl_export isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
Definition isl_map.c:11927
__isl_export __isl_give isl_set * isl_set_intersect(__isl_take isl_set *set1, __isl_take isl_set *set2)
Definition isl_map.c:4521
__isl_give isl_id * isl_set_get_dim_id(__isl_keep isl_set *set, enum isl_dim_type type, unsigned pos)
Definition isl_map.c:1004
__isl_export __isl_give isl_set * isl_set_empty(__isl_take isl_space *space)
Definition isl_map.c:6962
__isl_export __isl_give isl_set * isl_set_params(__isl_take isl_set *set)
Definition isl_map.c:6567
__isl_give isl_space * isl_space_set_alloc(isl_ctx *ctx, unsigned nparam, unsigned dim)
Definition isl_space.c:188
@ isl_dim_param
Definition space_type.h:15
Helper struct to remember assumptions.
Definition ScopHelper.h:61
Type for equivalent invariant accesses and their domain context.
Definition ScopInfo.h:1101
MemoryAccessList InvariantAccesses
Memory accesses now treated invariant.
Definition ScopInfo.h:1110
static TupleKindPtr Domain("Domain")