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"
65#define DEBUG_TYPE "polly-scops"
68STATISTIC(RichScopFound,
"Number of Scops containing a loop");
70 "Number of SCoPs with statically infeasible context.");
81 "polly-analyze-read-only-scalars",
82 cl::desc(
"Model read-only scalar values in the scop description"),
88 cl::desc(
"Bound the scop analysis by a maximal amount of "
89 "computational steps (0 means no bound)"),
93 "polly-allow-dereference-of-all-function-parameters",
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"
104 cl::desc(
"Do not take inbounds assumptions at all"),
108 "polly-rtc-max-arrays-per-group",
109 cl::desc(
"The maximal number of arrays to compare in each alias group."),
113 "polly-rtc-max-array-disjuncts",
114 cl::desc(
"The maximal number of disjunts allowed in memory accesses to "
119 "polly-rtc-max-parameters",
120 cl::desc(
"The maximal number of parameters allowed in RTCs."), cl::Hidden,
124 "polly-unprofitable-scalar-accs",
125 cl::desc(
"Count statements with scalar accesses as not optimizable"),
129 "polly-context", cl::value_desc(
"isl parameter set"),
130 cl::desc(
"Provide additional constraints on the context parameters"),
134 cl::desc(
"Detect and exploit reductions"),
135 cl::Hidden, cl::init(
true),
142 "polly-disable-multiplicative-reductions",
143 cl::desc(
"Disable multiplicative reductions"), cl::Hidden,
149 "polly-stmt-granularity",
151 "Algorithm to use for splitting basic blocks into multiple statements"),
153 "One statement per basic block"),
155 "Scalar independence heuristic"),
157 "Store-level granularity")),
166 return RN->isSubRegion() ? RN->getNodeAs<Region>()->getEntry()
167 : RN->getNodeAs<BasicBlock>();
171static inline BasicBlock *
173 if (RN->isSubRegion()) {
175 return RN->getNodeAs<Region>()->getExit();
177 return TI->getSuccessor(idx);
182 if (!RN->isSubRegion())
183 return SD->
isErrorBlock(*RN->getNodeAs<BasicBlock>(), R);
184 for (BasicBlock *BB : RN->getNodeAs<Region>()->blocks())
208 C =
C.set_constant_si(1);
212 return NextIterationMap;
219 if (BSet.is_bounded())
237 assert(NumDimsS >= Dim + 1);
244 for (
unsigned u = 0; u < Dim; u++) {
262 isl::set UnboundedParts =
S.subtract(BoundedParts);
263 return std::make_pair(UnboundedParts, BoundedParts);
270 case ICmpInst::ICMP_EQ:
272 case ICmpInst::ICMP_NE:
274 case ICmpInst::ICMP_SLT:
276 case ICmpInst::ICMP_SLE:
278 case ICmpInst::ICMP_SGT:
280 case ICmpInst::ICMP_SGE:
282 case ICmpInst::ICMP_ULT:
284 case ICmpInst::ICMP_UGT:
286 case ICmpInst::ICMP_ULE:
288 case ICmpInst::ICMP_UGE:
291 llvm_unreachable(
"Non integer predicate not supported");
301 int OldDepth =
scop->getRelativeLoopDepth(OldL);
302 int NewDepth =
scop->getRelativeLoopDepth(NewL);
304 if (OldDepth == -1 && NewDepth == -1)
314 if (OldDepth == NewDepth) {
315 assert(OldL->getParentLoop() == NewL->getParentLoop());
318 }
else if (OldDepth < NewDepth) {
319 assert(OldDepth + 1 == NewDepth);
320 auto &R =
scop->getRegion();
322 assert(NewL->getParentLoop() == OldL ||
323 ((!OldL || !R.contains(OldL)) && R.contains(NewL)));
326 assert(OldDepth > NewDepth);
327 unsigned Diff = OldDepth - NewDepth;
348 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
349 const SCEV *E,
bool NonNegative) {
351 InvalidDomainMap[BB] = InvalidDomainMap[BB].unite(PWAC.second);
352 return PWAC.first.release();
365 const SCEV *SCEV_TestVal,
const SCEV *SCEV_UpperBound,
366 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
367 bool IsStrictUpperBound) {
373 getPwAff(BB, InvalidDomainMap, SCEV_UpperBound,
true);
382 if (IsStrictUpperBound)
390 return ConsequenceCondSet;
395 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
396 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
398 assert(Condition &&
"No condition for switch");
401 LHS =
getPwAff(BB, InvalidDomainMap,
SE.getSCEVAtScope(Condition, L));
403 unsigned NumSuccessors = SI->getNumSuccessors();
404 ConditionSets.resize(NumSuccessors);
405 for (
auto &Case : SI->cases()) {
406 unsigned Idx = Case.getSuccessorIndex();
407 ConstantInt *CaseValue = Case.getCaseValue();
409 RHS =
getPwAff(BB, InvalidDomainMap,
SE.getSCEV(CaseValue));
418 assert(ConditionSets[0] ==
nullptr &&
"Default condition set was set");
420 for (
unsigned u = 2; u < NumSuccessors; u++)
431 BasicBlock *BB,
Value *Condition, Instruction *TI, Loop *L,
433 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
434 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
435 isl_set *ConsequenceCondSet =
nullptr;
437 if (
auto Load = dyn_cast<LoadInst>(Condition)) {
438 const SCEV *LHSSCEV =
SE.getSCEVAtScope(Load, L);
439 const SCEV *RHSSCEV =
SE.getZero(LHSSCEV->getType());
446 }
else if (
auto *
PHI = dyn_cast<PHINode>(Condition)) {
447 auto *Unique = dyn_cast<ConstantInt>(
450 "A PHINode condition should only be accepted by ScopDetection if "
451 "getUniqueNonErrorValue returns non-NULL");
453 if (Unique->isZero())
457 }
else if (
auto *CCond = dyn_cast<ConstantInt>(Condition)) {
462 }
else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) {
463 auto Opcode = BinOp->getOpcode();
464 assert(Opcode == Instruction::And || Opcode == Instruction::Or);
467 InvalidDomainMap, ConditionSets) &&
469 InvalidDomainMap, ConditionSets);
471 while (!ConditionSets.empty())
477 isl_set *ConsCondPart0 = ConditionSets.pop_back_val();
479 isl_set *ConsCondPart1 = ConditionSets.pop_back_val();
481 if (Opcode == Instruction::And)
484 ConsequenceCondSet =
isl_set_union(ConsCondPart0, ConsCondPart1);
486 auto *ICond = dyn_cast<ICmpInst>(Condition);
488 "Condition of exiting branch was neither constant nor ICmp!");
490 Region &R =
scop->getRegion();
496 bool NonNeg = ICond->isUnsigned();
497 const SCEV *LeftOperand =
SE.getSCEVAtScope(ICond->getOperand(0), L),
498 *RightOperand =
SE.getSCEVAtScope(ICond->getOperand(1), L);
503 switch (ICond->getPredicate()) {
504 case ICmpInst::ICMP_ULT:
507 RightOperand, InvalidDomainMap,
true);
509 case ICmpInst::ICMP_ULE:
512 RightOperand, InvalidDomainMap,
false);
514 case ICmpInst::ICMP_UGT:
517 LeftOperand, InvalidDomainMap,
true);
519 case ICmpInst::ICMP_UGE:
522 LeftOperand, InvalidDomainMap,
false);
525 LHS =
getPwAff(BB, InvalidDomainMap, LeftOperand, NonNeg);
526 RHS =
getPwAff(BB, InvalidDomainMap, RightOperand, NonNeg);
538 assert(ConsequenceCondSet);
542 isl_set *AlternativeCondSet =
nullptr;
555 TI ? TI->getParent() :
nullptr );
561 ConditionSets.push_back(ConsequenceCondSet);
569 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
570 SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
571 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
575 assert(isa<BranchInst>(TI) &&
"Terminator was neither branch nor switch.");
577 if (TI->getNumSuccessors() == 1) {
583 assert(Condition &&
"No condition for Terminator");
590 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
600 ReversePostOrderTraversal<Region *> RTraversal(R);
601 for (
auto *RN : RTraversal) {
604 if (RN->isSubRegion()) {
605 Region *SubRegion = RN->getNodeAs<Region>();
606 if (!
scop->isNonAffineSubRegion(SubRegion)) {
623 if (BBLoop && BBLoop->getHeader() == BB &&
scop->contains(BBLoop))
632 BasicBlock *BB, Loop *BBLoop,
633 SmallPtrSetImpl<BasicBlock *> &FinishedExitBlocks,
634 DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
637 auto *RI =
scop->getRegion().getRegionInfo();
638 auto *BBReg = RI ? RI->getRegionFor(BB) :
nullptr;
639 auto *ExitBB = BBReg ? BBReg->getExit() :
nullptr;
640 if (!BBReg || BBReg->getEntry() != BB || !
scop->contains(ExitBB))
646 while (L &&
scop->contains(L)) {
647 SmallVector<BasicBlock *, 4> LatchBBs;
648 BBLoop->getLoopLatches(LatchBBs);
649 for (
auto *LatchBB : LatchBBs)
650 if (BB != LatchBB && BBReg->contains(LatchBB))
652 L = L->getParentLoop();
656 assert(!
Domain.is_null() &&
"Cannot propagate a nullptr");
663 isl::set &ExitDomain =
scop->getOrInitEmptyDomain(ExitBB);
668 !ExitDomain.
is_null() ? AdjustedDomain.
unite(ExitDomain) : AdjustedDomain;
671 InvalidDomainMap[ExitBB] = ExitDomain.
empty(ExitDomain.
get_space());
673 FinishedExitBlocks.insert(ExitBB);
679 if (
scop->getRegion().getEntry() == BB)
683 auto &RI = *
scop->getRegion().getRegionInfo();
693 SmallPtrSet<Region *, 8> PropagatedRegions;
695 for (
auto *PredBB : predecessors(BB)) {
697 if (
DT.dominates(BB, PredBB))
701 auto PredBBInRegion = [PredBB](Region *PR) {
return PR->contains(PredBB); };
702 if (llvm::any_of(PropagatedRegions, PredBBInRegion)) {
710 auto *PredR = RI.getRegionFor(PredBB);
711 while (PredR->getExit() != BB && !PredR->contains(BB))
712 PredR = PredR->getParent();
716 if (PredR->getExit() == BB) {
717 PredBB = PredR->getEntry();
718 PropagatedRegions.insert(PredR);
725 PredDom = PredDom.
unite(PredBBDom);
732 Loop *L, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
733 int LoopDepth =
scop->getRelativeLoopDepth(L);
734 assert(LoopDepth >= 0 &&
"Loop in region should have at least depth one");
736 BasicBlock *HeaderBB = L->getHeader();
738 isl::set &HeaderBBDom =
scop->getOrInitEmptyDomain(HeaderBB);
745 SmallVector<BasicBlock *, 4> LatchBlocks;
746 L->getLoopLatches(LatchBlocks);
748 for (BasicBlock *LatchBB : LatchBlocks) {
750 if (!
scop->isDomainDefined(LatchBB))
753 isl::set LatchBBDom =
scop->getDomainConditions(LatchBB);
757 Instruction *TI = LatchBB->getTerminator();
758 BranchInst *BI = dyn_cast<BranchInst>(TI);
759 assert(BI &&
"Only branch instructions allowed in loop latches");
761 if (BI->isUnconditional())
762 BackedgeCondition = LatchBBDom;
764 SmallVector<isl_set *, 8> ConditionSets;
765 int idx = BI->getSuccessor(0) != HeaderBB;
767 InvalidDomainMap, ConditionSets))
773 BackedgeCondition =
isl::manage(ConditionSets[idx]);
776 int LatchLoopDepth =
scop->getRelativeLoopDepth(
LI.getLoopFor(LatchBB));
777 assert(LatchLoopDepth >= LoopDepth);
780 UnionBackedgeCondition = UnionBackedgeCondition.
unite(BackedgeCondition);
784 for (
int i = 0; i < LoopDepth; i++)
787 isl::set UnionBackedgeConditionComplement =
789 UnionBackedgeConditionComplement =
792 UnionBackedgeConditionComplement =
793 UnionBackedgeConditionComplement.
apply(ForwardMap);
794 HeaderBBDom = HeaderBBDom.
subtract(UnionBackedgeConditionComplement);
795 HeaderBBDom = HeaderBBDom.
apply(NextIterationMap);
798 HeaderBBDom = Parts.second;
803 bool RequiresRTC = !
scop->hasNSWAddRecForLoop(L);
808 nullptr, RequiresRTC);
813 DenseMap<std::pair<const SCEV *, Type *>, LoadInst *> EquivClasses;
816 for (LoadInst *LInst : RIL) {
817 const SCEV *PointerSCEV =
SE.getSCEV(LInst->getPointerOperand());
819 Type *Ty = LInst->getType();
820 LoadInst *&ClassRep = EquivClasses[std::make_pair(PointerSCEV, Ty)];
822 scop->addInvariantLoadMapping(LInst, ClassRep);
827 scop->addInvariantEquivClass(
833 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
834 bool IsOnlyNonAffineRegion =
scop->isNonAffineSubRegion(R);
835 auto *EntryBB = R->getEntry();
836 auto *L = IsOnlyNonAffineRegion ? nullptr :
LI.getLoopFor(EntryBB);
837 int LD =
scop->getRelativeLoopDepth(L);
845 if (IsOnlyNonAffineRegion)
873 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
885 SmallPtrSet<BasicBlock *, 8> FinishedExitBlocks;
886 ReversePostOrderTraversal<Region *> RTraversal(R);
887 for (
auto *RN : RTraversal) {
890 if (RN->isSubRegion()) {
891 Region *SubRegion = RN->getNodeAs<Region>();
892 if (!
scop->isNonAffineSubRegion(SubRegion)) {
900 scop->notifyErrorBlock();
904 Instruction *TI = BB->getTerminator();
906 if (isa<UnreachableInst>(TI))
909 if (!
scop->isDomainDefined(BB))
926 auto IsFinishedRegionExit = [&FinishedExitBlocks](BasicBlock *SuccBB) {
927 return FinishedExitBlocks.count(SuccBB);
929 if (std::all_of(succ_begin(BB), succ_end(BB), IsFinishedRegionExit))
936 SmallVector<isl_set *, 8> ConditionSets;
937 if (RN->isSubRegion())
938 ConditionSets.push_back(
Domain.copy());
947 assert(RN->isSubRegion() || TI->getNumSuccessors() == ConditionSets.size());
948 for (
unsigned u = 0, e = ConditionSets.size(); u < e; u++) {
953 if (!
scop->contains(SuccBB))
958 if (FinishedExitBlocks.count(SuccBB))
962 if (
DT.dominates(SuccBB, BB))
973 isl::set &SuccDomain =
scop->getOrInitEmptyDomain(SuccBB);
980 SuccDomain = CondSet;
991 while (++u < ConditionSets.size())
1001 Region *R, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
1002 ReversePostOrderTraversal<Region *> RTraversal(R);
1003 for (
auto *RN : RTraversal) {
1007 if (RN->isSubRegion()) {
1008 Region *SubRegion = RN->getNodeAs<Region>();
1009 if (!
scop->isNonAffineSubRegion(SubRegion)) {
1018 assert(!
Domain.is_null() &&
"Cannot propagate a nullptr");
1020 isl::set InvalidDomain = InvalidDomainMap[BB];
1022 bool IsInvalidBlock = ContainsErrorBlock ||
Domain.is_subset(InvalidDomain);
1024 if (!IsInvalidBlock) {
1035 InvalidDomainMap[BB] = InvalidDomain;
1040 auto *TI = BB->getTerminator();
1041 unsigned NumSuccs = RN->isSubRegion() ? 1 : TI->getNumSuccessors();
1042 for (
unsigned u = 0; u < NumSuccs; u++) {
1046 if (!
scop->contains(SuccBB))
1050 if (
DT.dominates(SuccBB, BB))
1056 auto AdjustedInvalidDomain =
1059 isl::set SuccInvalidDomain = InvalidDomainMap[SuccBB];
1060 SuccInvalidDomain = SuccInvalidDomain.
unite(AdjustedInvalidDomain);
1061 SuccInvalidDomain = SuccInvalidDomain.
coalesce();
1063 InvalidDomainMap[SuccBB] = SuccInvalidDomain;
1071 InvalidDomainMap.erase(BB);
1072 scop->invalidate(
COMPLEXITY, TI->getDebugLoc(), TI->getParent());
1076 InvalidDomainMap[BB] = InvalidDomain;
1083 Region *NonAffineSubRegion,
1093 auto *Scope =
LI.getLoopFor(
PHI->getParent());
1100 bool OnlyNonAffineSubRegionOperands =
true;
1101 for (
unsigned u = 0; u <
PHI->getNumIncomingValues(); u++) {
1102 Value *Op =
PHI->getIncomingValue(u);
1103 BasicBlock *OpBB =
PHI->getIncomingBlock(u);
1108 if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB)) {
1109 auto *OpInst = dyn_cast<Instruction>(Op);
1110 if (!OpInst || !NonAffineSubRegion->contains(OpInst))
1115 OnlyNonAffineSubRegionOperands =
false;
1119 if (!OnlyNonAffineSubRegionOperands && !IsExitBlock) {
1125 Instruction *Inst) {
1126 assert(!isa<PHINode>(Inst));
1129 for (Use &Op : Inst->operands())
1173 Result = Result.add_pw_multi_aff(PMA);
1183 assert(LoopStack.size() == 1 && LoopStack.back().L == L);
1184 scop->setScheduleTree(LoopStack[0].Schedule);
1214 ReversePostOrderTraversal<Region *> RTraversal(R);
1215 std::deque<RegionNode *> WorkList(RTraversal.begin(), RTraversal.end());
1216 std::deque<RegionNode *> DelayList;
1217 bool LastRNWaiting =
false;
1226 while (!WorkList.empty() || !DelayList.empty()) {
1229 if ((LastRNWaiting && !WorkList.empty()) || DelayList.empty()) {
1230 RN = WorkList.front();
1231 WorkList.pop_front();
1232 LastRNWaiting =
false;
1234 RN = DelayList.front();
1235 DelayList.pop_front();
1239 if (!
scop->contains(L))
1242 Loop *LastLoop = LoopStack.back().L;
1243 if (LastLoop != L) {
1244 if (LastLoop && !LastLoop->contains(L)) {
1245 LastRNWaiting =
true;
1246 DelayList.push_back(RN);
1249 LoopStack.push_back({L, {}, 0});
1256 if (RN->isSubRegion()) {
1257 auto *LocalRegion = RN->getNodeAs<Region>();
1258 if (!
scop->isNonAffineSubRegion(LocalRegion)) {
1264 assert(LoopStack.rbegin() != LoopStack.rend());
1265 auto LoopData = LoopStack.rbegin();
1268 for (
auto *Stmt :
scop->getStmtListFor(RN)) {
1283 size_t Dimension = LoopStack.size();
1284 while (LoopData->L &&
1287 auto NumBlocksProcessed = LoopData->NumBlocksProcessed;
1289 assert(std::next(LoopData) != LoopStack.rend());
1290 Loop *L = LoopData->L;
1306 scop->markDisableHeuristics();
1318 LoopData->NumBlocksProcessed += NumBlocksProcessed;
1321 LoopStack.erase(LoopStack.begin() + Dimension, LoopStack.end());
1328 if (
scop->isEscaping(Inst))
1336 scop->addAssumption(AS.Kind, AS.Set, AS.Loc, AS.Sign,
1337 nullptr , AS.RequiresRTC);
1342 isl_set *Dom =
scop->getDomainConditions(AS.BB).release();
1367 AssumptionCache &AC, DenseMap<BasicBlock *, isl::set> &InvalidDomainMap) {
1369 auto *CI = dyn_cast_or_null<CallInst>(
Assumption);
1370 if (!CI || CI->arg_size() != 1)
1373 bool InScop =
scop->contains(CI);
1374 if (!InScop && !
scop->isDominatedBy(
DT, CI->getParent()))
1377 auto *L =
LI.getLoopFor(CI->getParent());
1378 auto *Val = CI->getArgOperand(0);
1380 auto &R =
scop->getRegion();
1383 OptimizationRemarkAnalysis(
DEBUG_TYPE,
"IgnoreUserAssumption", CI)
1384 <<
"Non-affine user assumption ignored.");
1390 for (
auto *Param : DetectedParams) {
1392 Param =
scop->getRepresentingInvariantLoadSCEV(Param);
1393 if (
scop->isParam(Param))
1395 NewParams.insert(Param);
1398 SmallVector<isl_set *, 2> ConditionSets;
1399 auto *TI = InScop ? CI->getParent()->getTerminator() :
nullptr;
1400 BasicBlock *BB = InScop ? CI->getParent() : R.getEntry();
1403 assert(Dom &&
"Cannot propagate a nullptr.");
1411 isl_set *AssumptionCtx =
nullptr;
1421 if (!NewParams.empty()) {
1427 if (!NewParams.count(Param))
1434 ORE.emit(OptimizationRemarkAnalysis(
DEBUG_TYPE,
"UserAssumption", CI)
1435 <<
"Use user assumption: "
1436 << stringFromIslObj(AssumptionCtx,
"null"));
1439 scop->setContext(newContext);
1449 Type *ElementType = Val->getType();
1451 const SCEV *AccessFunction =
1452 SE.getSCEVAtScope(Address,
LI.getLoopFor(Inst->getParent()));
1453 const SCEVUnknown *BasePointer =
1454 dyn_cast<SCEVUnknown>(
SE.getPointerBase(AccessFunction));
1458 if (
auto *BitCast = dyn_cast<BitCastInst>(Address))
1459 Address = BitCast->getOperand(0);
1461 auto *GEP = dyn_cast<GetElementPtrInst>(Address);
1462 if (!GEP ||
DL.getTypeAllocSize(GEP->getResultElementType()) !=
1463 DL.getTypeAllocSize(ElementType))
1466 SmallVector<const SCEV *, 4> Subscripts;
1467 SmallVector<int, 4> Sizes;
1468 getIndexExpressionsFromGEP(
SE, GEP, Subscripts, Sizes);
1469 auto *BasePtr = GEP->getOperand(0);
1471 if (
auto *BasePtrCast = dyn_cast<BitCastInst>(BasePtr))
1472 BasePtr = BasePtrCast->getOperand(0);
1476 if (BasePtr != BasePointer->getValue())
1479 std::vector<const SCEV *> SizesSCEV;
1484 for (
auto *Subscript : Subscripts) {
1490 for (LoadInst *LInst : AccessILS)
1491 if (!ScopRIL.count(LInst))
1498 SizesSCEV.push_back(
nullptr);
1500 for (
auto V : Sizes)
1501 SizesSCEV.push_back(
SE.getSCEV(
1502 ConstantInt::get(IntegerType::getInt64Ty(BasePtr->getContext()), V)));
1504 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1505 true, Subscripts, SizesSCEV, Val);
1519 Type *ElementType = Val->getType();
1520 unsigned ElementSize =
DL.getTypeAllocSize(ElementType);
1524 const SCEV *AccessFunction =
1525 SE.getSCEVAtScope(Address,
LI.getLoopFor(Inst->getParent()));
1526 const SCEVUnknown *BasePointer =
1527 dyn_cast<SCEVUnknown>(
SE.getPointerBase(AccessFunction));
1529 assert(BasePointer &&
"Could not find base pointer");
1531 auto &InsnToMemAcc =
scop->getInsnToMemAccMap();
1532 auto AccItr = InsnToMemAcc.find(Inst);
1533 if (AccItr == InsnToMemAcc.end())
1536 std::vector<const SCEV *> Sizes = {
nullptr};
1538 Sizes.insert(Sizes.end(), AccItr->second.Shape->DelinearizedSizes.begin(),
1539 AccItr->second.Shape->DelinearizedSizes.end());
1544 if (Sizes.size() == 1)
1553 auto DelinearizedSize =
1554 cast<SCEVConstant>(Sizes.back())->getAPInt().getSExtValue();
1556 if (ElementSize != DelinearizedSize)
1559 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1560 true, AccItr->second.DelinearizedSubscripts, Sizes, Val);
1565 auto *MemIntr = dyn_cast_or_null<MemIntrinsic>(Inst);
1567 if (MemIntr ==
nullptr)
1570 auto *L =
LI.getLoopFor(Inst->getParent());
1571 const SCEV *LengthVal =
SE.getSCEVAtScope(MemIntr->getLength(), L);
1580 LengthVal,
SE, &AccessILS);
1581 for (LoadInst *LInst : AccessILS)
1582 if (!ScopRIL.count(LInst))
1583 LengthIsAffine =
false;
1584 if (!LengthIsAffine)
1585 LengthVal =
nullptr;
1587 auto *DestPtrVal = MemIntr->getDest();
1590 const SCEV *DestAccFunc =
SE.getSCEVAtScope(DestPtrVal, L);
1597 if (DestAccFunc->isZero())
1600 if (
auto *U = dyn_cast<SCEVUnknown>(DestAccFunc)) {
1601 if (isa<ConstantPointerNull>(U->getValue()))
1605 auto *DestPtrSCEV = dyn_cast<SCEVUnknown>(
SE.getPointerBase(DestAccFunc));
1607 DestAccFunc =
SE.getMinusSCEV(DestAccFunc, DestPtrSCEV);
1609 IntegerType::getInt8Ty(DestPtrVal->getContext()),
1610 LengthIsAffine, {DestAccFunc, LengthVal}, {nullptr},
1613 auto *MemTrans = dyn_cast<MemTransferInst>(MemIntr);
1617 auto *SrcPtrVal = MemTrans->getSource();
1620 const SCEV *SrcAccFunc =
SE.getSCEVAtScope(SrcPtrVal, L);
1624 if (SrcAccFunc->isZero())
1627 auto *SrcPtrSCEV = dyn_cast<SCEVUnknown>(
SE.getPointerBase(SrcAccFunc));
1629 SrcAccFunc =
SE.getMinusSCEV(SrcAccFunc, SrcPtrSCEV);
1631 IntegerType::getInt8Ty(SrcPtrVal->getContext()),
1632 LengthIsAffine, {SrcAccFunc, LengthVal}, {nullptr},
1639 auto *CI = dyn_cast_or_null<CallInst>(Inst);
1647 const SCEV *AF =
SE.getConstant(IntegerType::getInt64Ty(CI->getContext()), 0);
1648 auto *CalledFunction = CI->getCalledFunction();
1649 MemoryEffects ME =
AA.getMemoryEffects(CalledFunction);
1650 if (ME.doesNotAccessMemory())
1653 if (ME.onlyAccessesArgPointees()) {
1654 ModRefInfo ArgMR = ME.getModRef(IRMemLocation::ArgMem);
1657 Loop *L =
LI.getLoopFor(Inst->getParent());
1658 for (
const auto &Arg : CI->args()) {
1659 if (!Arg->getType()->isPointerTy())
1662 const SCEV *ArgSCEV =
SE.getSCEVAtScope(Arg, L);
1663 if (ArgSCEV->isZero())
1666 if (
auto *U = dyn_cast<SCEVUnknown>(ArgSCEV)) {
1667 if (isa<ConstantPointerNull>(U->getValue()))
1671 auto *ArgBasePtr = cast<SCEVUnknown>(
SE.getPointerBase(ArgSCEV));
1673 ArgBasePtr->getType(),
false, {AF}, {nullptr}, CI);
1678 if (ME.onlyReadsMemory()) {
1692 Type *ElementType = Val->getType();
1696 const SCEV *AccessFunction =
1697 SE.getSCEVAtScope(Address,
LI.getLoopFor(Inst->getParent()));
1698 const SCEVUnknown *BasePointer =
1699 dyn_cast<SCEVUnknown>(
SE.getPointerBase(AccessFunction));
1701 assert(BasePointer &&
"Could not find base pointer");
1702 AccessFunction =
SE.getMinusSCEV(AccessFunction, BasePointer);
1705 bool isVariantInNonAffineLoop =
false;
1706 SetVector<const Loop *> Loops;
1708 for (
const Loop *L : Loops)
1710 isVariantInNonAffineLoop =
true;
1717 bool IsAffine = !isVariantInNonAffineLoop &&
1719 AccessFunction,
SE, &AccessILS);
1722 for (LoadInst *LInst : AccessILS)
1723 if (!ScopRIL.count(LInst))
1729 addArrayAccess(Stmt, Inst, AccType, BasePointer->getValue(), ElementType,
1730 IsAffine, {AccessFunction}, {nullptr}, Val);
1751 "At least one of the buildAccess functions must handled this access, or "
1752 "ScopDetection should have rejected this SCoP");
1756 for (
auto &Stmt : *
scop) {
1757 if (Stmt.isBlockStmt()) {
1762 Region *R = Stmt.getRegion();
1763 for (BasicBlock *BB : R->blocks())
1771 for (BasicBlock *BB :
scop->getRegion().blocks()) {
1772 for (Instruction &Inst : *BB)
1791 bool IsMain,
bool IsLast =
false) {
1798 else if (Count < 26)
1799 Suffix +=
'a' + Count;
1801 Suffix += std::to_string(Count);
1816 Loop *SurroundingLoop =
LI.getLoopFor(BB);
1819 long BBIdx =
scop->getNextStmtIdx();
1820 std::vector<Instruction *> Instructions;
1821 for (Instruction &Inst : *BB) {
1823 Instructions.push_back(&Inst);
1824 if (Inst.getMetadata(
"polly_split_after") ||
1825 (SplitOnStore && isa<StoreInst>(Inst))) {
1826 std::string Name =
makeStmtName(BB, BBIdx, Count, Count == 0);
1827 scop->addScopStmt(BB, Name, SurroundingLoop, Instructions);
1829 Instructions.clear();
1833 std::string Name =
makeStmtName(BB, BBIdx, Count, Count == 0);
1834 scop->addScopStmt(BB, Name, SurroundingLoop, Instructions);
1843 return Inst->mayHaveSideEffects() || Inst->mayReadOrWriteMemory();
1849 ArrayRef<Instruction *> ModeledInsts) {
1850 for (Instruction *Inst : ModeledInsts) {
1851 if (isa<PHINode>(Inst))
1854 for (Use &Op : Inst->operands()) {
1855 Instruction *OpInst = dyn_cast<Instruction>(Op.get());
1860 if (!UnionFind.contains(OpInst))
1863 UnionFind.unionSets(Inst, OpInst);
1875 ArrayRef<Instruction *> ModeledInsts) {
1876 SetVector<Instruction *> SeenLeaders;
1877 for (Instruction *Inst : ModeledInsts) {
1881 Instruction *Leader = UnionFind.getLeaderValue(Inst);
1886 bool Inserted = SeenLeaders.insert(Leader);
1894 for (Instruction *Prev : reverse(SeenLeaders)) {
1902 UnionFind.unionSets(Prev, Leader);
1926 ArrayRef<Instruction *> ModeledInsts) {
1927 for (Instruction *Inst : ModeledInsts) {
1928 PHINode *
PHI = dyn_cast<PHINode>(Inst);
1932 int Idx =
PHI->getBasicBlockIndex(
PHI->getParent());
1936 Instruction *IncomingVal =
1937 dyn_cast<Instruction>(
PHI->getIncomingValue(Idx));
1941 UnionFind.unionSets(
PHI, IncomingVal);
1946 Loop *L =
LI.getLoopFor(BB);
1950 SmallVector<Instruction *, 32> ModeledInsts;
1951 EquivalenceClasses<Instruction *> UnionFind;
1952 Instruction *MainInst =
nullptr, *MainLeader =
nullptr;
1953 for (Instruction &Inst : *BB) {
1956 ModeledInsts.push_back(&Inst);
1957 UnionFind.insert(&Inst);
1964 if (!MainInst && (isa<StoreInst>(Inst) ||
1965 (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst))))
1975 MapVector<Instruction *, std::vector<Instruction *>> LeaderToInstList;
1980 for (Instruction *Inst : ModeledInsts) {
1984 auto LeaderIt = UnionFind.findLeader(Inst);
1985 if (LeaderIt == UnionFind.member_end())
1989 (void)LeaderToInstList[*LeaderIt];
1994 for (Instruction *Inst : ModeledInsts) {
1995 auto LeaderIt = UnionFind.findLeader(Inst);
1996 if (LeaderIt == UnionFind.member_end())
1999 if (Inst == MainInst)
2000 MainLeader = *LeaderIt;
2001 std::vector<Instruction *> &InstList = LeaderToInstList[*LeaderIt];
2002 InstList.push_back(Inst);
2007 long BBIdx =
scop->getNextStmtIdx();
2008 for (
auto &Instructions : LeaderToInstList) {
2009 std::vector<Instruction *> &InstList = Instructions.second;
2012 bool IsMain = (MainInst ? MainLeader == Instructions.first : Count == 0);
2014 std::string Name =
makeStmtName(BB, BBIdx, Count, IsMain);
2015 scop->addScopStmt(BB, Name, L, std::move(InstList));
2025 std::string EpilogueName =
makeStmtName(BB, BBIdx, Count, Count == 0,
true);
2026 scop->addScopStmt(BB, EpilogueName, L, {});
2030 if (
scop->isNonAffineSubRegion(&SR)) {
2031 std::vector<Instruction *> Instructions;
2032 Loop *SurroundingLoop =
2034 for (Instruction &Inst : *SR.getEntry())
2036 Instructions.push_back(&Inst);
2037 long RIdx =
scop->getNextStmtIdx();
2039 scop->addScopStmt(&SR, Name, SurroundingLoop, Instructions);
2043 for (
auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
2044 if (I->isSubRegion())
2047 BasicBlock *BB = I->getNodeAs<BasicBlock>();
2063 Region *NonAffineSubRegion) {
2066 "The exit BB is the only one that cannot be represented by a statement");
2071 if (
SD.isErrorBlock(BB,
scop->getRegion()))
2074 auto BuildAccessesForInst = [
this, Stmt,
2075 NonAffineSubRegion](Instruction *Inst) {
2076 PHINode *
PHI = dyn_cast<PHINode>(Inst);
2081 assert(Stmt &&
"Cannot build access function in non-existing statement");
2097 BuildAccessesForInst(Inst);
2099 BuildAccessesForInst(BB.getTerminator());
2101 for (Instruction &Inst : BB) {
2106 if (isa<LoadInst>(Inst) && RIL.count(cast<LoadInst>(&Inst)))
2109 BuildAccessesForInst(&Inst);
2116 Value *BaseAddress, Type *ElementType,
bool Affine,
Value *AccessValue,
2117 ArrayRef<const SCEV *> Subscripts, ArrayRef<const SCEV *> Sizes,
2119 bool isKnownMustAccess =
false;
2123 isKnownMustAccess =
true;
2131 if (Inst &&
DT.dominates(Inst->getParent(), Stmt->
getRegion()->getExit()))
2132 isKnownMustAccess =
true;
2139 isKnownMustAccess =
true;
2144 auto *Access =
new MemoryAccess(Stmt, Inst, AccType, BaseAddress, ElementType,
2145 Affine, Subscripts, Sizes, AccessValue,
Kind);
2147 scop->addAccessFunction(Access);
2154 Value *BaseAddress, Type *ElementType,
2156 ArrayRef<const SCEV *> Subscripts,
2157 ArrayRef<const SCEV *> Sizes,
2158 Value *AccessValue) {
2165static bool isDivisible(
const SCEV *Expr,
unsigned Size, ScalarEvolution &SE) {
2171 if (
auto *MulExpr = dyn_cast<SCEVMulExpr>(Expr)) {
2172 for (
const SCEV *FactorExpr : MulExpr->operands())
2180 if (
auto *NAryExpr = dyn_cast<SCEVNAryExpr>(Expr)) {
2181 for (
const SCEV *OpExpr : NAryExpr->operands())
2187 const SCEV *SizeSCEV = SE.getConstant(Expr->getType(), Size);
2188 const SCEV *UDivSCEV = SE.getUDivExpr(Expr, SizeSCEV);
2189 const SCEV *MulSCEV = SE.getMulExpr(UDivSCEV, SizeSCEV);
2190 return MulSCEV == Expr;
2197 if (
Array->getNumberOfDimensions() <= 1)
2209 std::vector<int> Int;
2211 for (
unsigned i = 0; i < Dims; i++) {
2218 if (i == Dims - 1) {
2231 if (ValAPInt.isSignedIntN(32))
2232 ValInt = ValAPInt.getSExtValue();
2236 Int.push_back(ValInt);
2253 Int.push_back(ValInt);
2258 if (!Elements.
is_subset(MappedElements))
2261 bool CanFold =
true;
2265 unsigned NumDims =
Array->getNumberOfDimensions();
2266 for (
unsigned i = 1; i < NumDims - 1; i++)
2267 if (Int[0] != Int[i] && Int[i])
2273 for (
auto &Access :
scop->access_functions())
2274 if (Access->getScopArrayInfo() ==
Array)
2275 Access->setAccessRelation(
2276 Access->getAccessRelation().apply_range(Transform));
2278 std::vector<const SCEV *> Sizes;
2279 for (
unsigned i = 0; i < NumDims; i++) {
2280 auto Size =
Array->getDimensionSize(i);
2282 if (i == NumDims - 1)
2283 Size =
SE.getMulExpr(Size,
SE.getConstant(Size->getType(), Int[0]));
2284 Sizes.push_back(Size);
2287 Array->updateSizes(Sizes,
false );
2303 if (!Access->isArrayKind())
2308 if (
Array->getNumberOfDimensions() != 1)
2310 unsigned DivisibleSize =
Array->getElemSizeInBytes();
2311 const SCEV *Subscript = Access->getSubscript(0);
2314 auto *Ty = IntegerType::get(
SE.getContext(), DivisibleSize * 8);
2315 Array->updateElementType(Ty);
2318 for (
auto &Stmt : *
scop)
2319 for (
auto &Access : Stmt)
2320 Access->updateDimensionality();
2324 for (
auto &Stmt : *
scop)
2325 for (
auto &Access : Stmt)
2326 Access->foldAccessRelation();
2332 for (
auto &Stmt : *
scop)
2333 for (
auto &Access : Stmt) {
2334 isl::set Outside = Access->assumeNoOutOfBound();
2335 const auto &Loc = Access->getAccessInstruction()
2336 ? Access->getAccessInstruction()->getDebugLoc()
2354 Stmt =
scop->getLastStmtFor(Inst->getParent());
2365 true, Inst, ArrayRef<const SCEV *>(),
2381 switch (VUse.getKind()) {
2404 true, V, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2415 BasicBlock *IncomingBlock,
2416 Value *IncomingValue,
bool IsExitBlock) {
2421 scop->getOrCreateScopArrayInfo(
PHI,
PHI->getType(), {},
2438 assert(Acc->getAccessInstruction() ==
PHI);
2439 Acc->addIncoming(IncomingBlock, IncomingValue);
2445 PHI, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2453 PHI, ArrayRef<const SCEV *>(), ArrayRef<const SCEV *>(),
2460 Stmt.
Domain =
scop->getDomainConditions(&Stmt);
2468 Loop *L =
LI.getLoopFor(BB);
2471 L = L->getParentLoop();
2473 SmallVector<llvm::Loop *, 8> Loops;
2477 L = L->getParentLoop();
2488 switch (BinOp->getOpcode()) {
2489 case Instruction::FAdd:
2490 if (!BinOp->isFast())
2493 case Instruction::Add:
2495 case Instruction::Or:
2497 case Instruction::Xor:
2499 case Instruction::And:
2501 case Instruction::FMul:
2502 if (!BinOp->isFast())
2505 case Instruction::Mul:
2529 SmallVector<MemoryAccess *, 8> &MemAccs) {
2530 bool HasIntersectingAccs =
false;
2534 if (MA == LoadMA || MA == StoreMA)
2536 auto AccRel = MA->getAccessRelation().intersect_domain(
Domain);
2537 auto Accs = AccRel.range();
2538 auto AccsNoParams = Accs.project_out_all_params();
2540 bool CompatibleSpace = AllAccsNoParams.has_equal_space(AccsNoParams);
2542 if (CompatibleSpace) {
2543 auto OverlapAccs = Accs.intersect(AllAccs);
2544 bool DoesIntersect = !OverlapAccs.is_empty();
2545 HasIntersectingAccs |= DoesIntersect;
2548 return HasIntersectingAccs;
2554 SmallVector<MemoryAccess *, 8> &MemAccs) {
2559 POLLY_DEBUG(dbgs() <<
" == The accessed space below is "
2560 << (Valid ?
"" :
"not ") <<
"equal!\n");
2575 POLLY_DEBUG(dbgs() <<
" == The accessed memory is " << (Valid ?
"" :
"not ")
2576 <<
"overlapping!\n");
2585 POLLY_DEBUG(dbgs() <<
" == The accessed memory is " << (Valid ?
"not " :
"")
2586 <<
"accessed by other instructions!\n");
2600 using StatePairTy = std::pair<unsigned, MemoryAccess::ReductionType>;
2601 using FlowInSetTy = MapVector<const LoadInst *, StatePairTy>;
2602 using StateTy = MapVector<const Instruction *, FlowInSetTy>;
2614 SmallPtrSet<const Instruction *, 8> InvalidLoads;
2615 SmallVector<BasicBlock *, 8> ScopBlocks;
2618 ScopBlocks.push_back(BB);
2620 for (BasicBlock *Block : Stmt.
getRegion()->blocks())
2621 ScopBlocks.push_back(Block);
2623 for (BasicBlock *Block : ScopBlocks) {
2624 for (Instruction &Inst : *Block) {
2625 if ((Stmt.
getParent())->getStmtFor(&Inst) != &Stmt)
2627 bool UsedOutsideStmt = any_of(Inst.users(), [&Stmt](User *U) {
2628 return (Stmt.getParent())->getStmtFor(cast<Instruction>(U)) != &Stmt;
2631 if (
auto *Load = dyn_cast<LoadInst>(&Inst)) {
2633 if (
auto *Ptr = dyn_cast<Instruction>(Load->getPointerOperand())) {
2634 const auto &It = State.find(Ptr);
2635 if (It != State.end())
2636 InvalidLoads.insert_range(llvm::make_first_range(It->second));
2640 if (UsedOutsideStmt)
2641 InvalidLoads.insert(Load);
2650 if (
auto *Store = dyn_cast<StoreInst>(&Inst)) {
2652 if (
const Instruction *Ptr =
2653 dyn_cast<Instruction>(Store->getPointerOperand())) {
2654 const auto &It = State.find(Ptr);
2655 if (It != State.end())
2656 InvalidLoads.insert_range(llvm::make_first_range(It->second));
2660 if (
auto *ValueInst = dyn_cast<Instruction>(Store->getValueOperand()))
2661 State.insert(std::make_pair(Store, State[ValueInst]));
2667 auto *BinOp = dyn_cast<BinaryOperator>(&Inst);
2669 POLLY_DEBUG(dbgs() <<
"CurInst: " << Inst <<
" RT: " << CurRedType
2674 FlowInSetTy &InstInFlowSet = State[&Inst];
2675 for (Use &Op : Inst.operands()) {
2676 auto *OpInst = dyn_cast<Instruction>(Op);
2680 POLLY_DEBUG(dbgs().indent(4) <<
"Op Inst: " << *OpInst <<
"\n");
2681 const StateTy::iterator &OpInFlowSetIt = State.find(OpInst);
2682 if (OpInFlowSetIt == State.end())
2687 FlowInSetTy &OpInFlowSet = OpInFlowSetIt->second;
2688 for (
auto &OpInFlowPair : OpInFlowSet) {
2689 unsigned OpFlowIn = OpInFlowPair.second.first;
2690 unsigned InstFlowIn = InstInFlowSet[OpInFlowPair.first].first;
2694 InstInFlowSet[OpInFlowPair.first].second;
2701 POLLY_DEBUG(dbgs().indent(8) <<
"OpRedType: " << OpRedType <<
"\n");
2702 POLLY_DEBUG(dbgs().indent(8) <<
"NewRedType: " << NewRedType <<
"\n");
2703 InstInFlowSet[OpInFlowPair.first] =
2704 std::make_pair(OpFlowIn + InstFlowIn, NewRedType);
2710 if (UsedOutsideStmt)
2711 InvalidLoads.insert_range(llvm::make_first_range(InstInFlowSet));
2720 using MemAccPair = std::pair<MemoryAccess *, MemoryAccess *>;
2721 DenseMap<MemAccPair, MemoryAccess::ReductionType> ValidCandidates;
2731 assert(!St->isVolatile());
2734 for (
auto &MaInFlowSetElem : MaInFlowSet) {
2736 assert(ReadMA &&
"Couldn't find memory access for incoming load!");
2739 <<
"'\n\tflows into\n'"
2741 << MaInFlowSetElem.second.first <<
" times & RT: "
2742 << MaInFlowSetElem.second.second <<
"\n");
2745 unsigned NumAllowableInFlow = 1;
2748 bool Valid = (MaInFlowSetElem.second.first == NumAllowableInFlow);
2760 ValidCandidates[std::make_pair(ReadMA, WriteMA)] = RT;
2768 for (
auto &CandidatePair : ValidCandidates) {
2773 dbgs() <<
" Load :: "
2774 << *((CandidatePair.first.first)->getAccessInstruction())
2776 << *((CandidatePair.first.second)->getAccessInstruction())
2777 <<
"\n are marked as reduction like\n");
2779 CandidatePair.first.first->markAsReductionLike(RT);
2780 CandidatePair.first.second->markAsReductionLike(RT);
2785 auto &RIL =
scop->getRequiredInvariantLoads();
2786 for (LoadInst *
LI : RIL) {
2791 if (Stmt.getArrayAccessOrNULLFor(
LI)) {
2809 InvariantAccesses.push_back({Access, NHCtx});
2813 for (
auto InvMA : InvariantAccesses)
2814 Stmt.removeMemoryAccess(InvMA.MA);
2832 unsigned NumTotalDims = 0;
2847 if (
auto *BasePtrMA =
scop->lookupBasePtrAccess(MA)) {
2852 if (
auto *BasePtrInst = dyn_cast<Instruction>(BaseAddr))
2853 if (!isa<LoadInst>(BasePtrInst))
2854 return scop->contains(BasePtrInst);
2868 std::string SpaceStr = stringFromIslObj(Space,
"null");
2869 errs() <<
"Error: the context provided in -polly-context has not the same "
2870 <<
"number of dimensions than the computed context. Due to this "
2871 <<
"mismatch, the -polly-context option is ignored. Please provide "
2872 <<
"the context in the parameter space: " << SpaceStr <<
".\n";
2877 std::string NameContext =
2881 if (NameContext != NameUserContext) {
2882 std::string SpaceStr = stringFromIslObj(Space,
"null");
2883 errs() <<
"Error: the name of dimension " << i
2884 <<
" provided in -polly-context "
2885 <<
"is '" << NameUserContext <<
"', but the name in the computed "
2886 <<
"context is '" << NameContext
2887 <<
"'. Due to this name mismatch, "
2888 <<
"the -polly-context option is ignored. Please provide "
2889 <<
"the context in the parameter space: " << SpaceStr <<
".\n";
2896 isl::set newContext =
scop->getContext().intersect(UserContext);
2897 scop->setContext(newContext);
2937 auto &
DL =
scop->getFunction().getDataLayout();
2938 if (isSafeToLoadUnconditionally(
LI->getPointerOperand(),
LI->getType(),
2939 LI->getAlign(),
DL,
nullptr)) {
2941 }
else if (BB !=
LI->getParent()) {
2946 SafeToLoad = AccessRelation.
range();
2954 bool IsWritten = !WrittenCtx.
is_empty();
2971 for (
const llvm::Argument &Arg : F.args())
2972 if (&Arg == maybeParam)
2979 bool StmtInvalidCtxIsEmpty,
2980 bool MAInvalidCtxIsEmpty,
2981 bool NonHoistableCtxIsEmpty) {
2983 const DataLayout &
DL = LInst->getDataLayout();
2990 if (!isDereferenceableAndAlignedPointer(
2991 LInst->getPointerOperand(), LInst->getType(), LInst->getAlign(),
DL))
2997 if (!NonHoistableCtxIsEmpty)
3002 if (StmtInvalidCtxIsEmpty && MAInvalidCtxIsEmpty)
3008 for (
const SCEV *Subscript : MA->
subscripts())
3009 if (!isa<SCEVConstant>(Subscript))
3020 bool StmtInvalidCtxIsEmpty = StmtInvalidCtx.
is_empty();
3025 DomainCtx = DomainCtx.
subtract(StmtInvalidCtx);
3028 auto *AccInst = InvMAs.front().MA->getAccessInstruction();
3029 scop->invalidate(
COMPLEXITY, AccInst->getDebugLoc(), AccInst->getParent());
3038 for (
auto &InvMA : InvMAs) {
3039 auto *MA = InvMA.MA;
3040 Instruction *AccInst = MA->getAccessInstruction();
3041 if (
SE.isSCEVable(AccInst->getType())) {
3042 SetVector<Value *> Values;
3043 for (
const SCEV *Parameter :
scop->parameters()) {
3046 if (!Values.count(AccInst))
3059 for (
auto &InvMA : InvMAs) {
3060 auto *MA = InvMA.MA;
3061 isl::set NHCtx = InvMA.NonHoistableCtx;
3066 LoadInst *LInst = cast<LoadInst>(MA->getAccessInstruction());
3067 Type *Ty = LInst->getType();
3068 const SCEV *PointerSCEV =
SE.getSCEV(LInst->getPointerOperand());
3070 isl::set MAInvalidCtx = MA->getInvalidContext();
3071 bool NonHoistableCtxIsEmpty = NHCtx.
is_empty();
3072 bool MAInvalidCtxIsEmpty = MAInvalidCtx.
is_empty();
3077 NonHoistableCtxIsEmpty)) {
3085 bool Consolidated =
false;
3086 for (
auto &IAClass :
scop->invariantEquivClasses()) {
3087 if (PointerSCEV != IAClass.IdentifyingPointer || Ty != IAClass.AccessType)
3095 auto &MAs = IAClass.InvariantAccesses;
3097 auto *LastMA = MAs.front();
3099 isl::set AR = MA->getAccessRelation().range();
3100 isl::set LastAR = LastMA->getAccessRelation().range();
3110 Consolidated =
true;
3113 isl::set IAClassDomainCtx = IAClass.ExecutionContext;
3114 if (!IAClassDomainCtx.
is_null())
3115 IAClassDomainCtx = IAClassDomainCtx.
unite(MACtx).
coalesce();
3117 IAClassDomainCtx = MACtx;
3118 IAClass.ExecutionContext = IAClassDomainCtx;
3129 scop->addInvariantEquivClass(
3144 return CanonicalArray;
3152 for (
MemoryAccess *Access2 : EqClass2.InvariantAccesses)
3164 if (Access->getLatestScopArrayInfo() != Old)
3168 isl::map Map = Access->getAccessRelation();
3170 Access->setAccessRelation(Map);
3181 if (!CanonicalBasePtrSAI)
3187 if (!BasePtrSAI || BasePtrSAI == CanonicalBasePtrSAI ||
3221 for (
const SCEV *Size : Access->
Sizes) {
3227 ElementType, Access->
Sizes, Ty);
3232 for (
const SCEV *Subscript : Access->
subscripts()) {
3233 if (!Access->
isAffine() || !Subscript)
3239 scop->addAccessData(Access);
3276 unsigned InvolvedParams = 0;
3300 assert(MaxOutputSize >= 1 &&
"Assumed at least one output dimension");
3302 Pos = MaxOutputSize - 1;
3303 LastDimAff = MaxPMA.
at(Pos);
3306 LastDimAff = LastDimAff.
add(OneAff);
3312 MinMaxAccesses.push_back(std::make_pair(MinPMA, MaxPMA));
3320 MinMaxAccesses.reserve(AliasGroup.size());
3326 Accesses = Accesses.
unite(MA->getAccessRelation());
3331 bool LimitReached =
false;
3338 return !LimitReached;
3345 return Domain.reset_tuple_id();
3355 if (
scop->getAliasGroups().size())
3365 POLLY_DEBUG(dbgs() <<
"\n\nNOTE: Run time checks for " <<
scop->getNameStr()
3366 <<
" could not be created. This SCoP has been dismissed.");
3370std::tuple<ScopBuilder::AliasGroupVectorTy, DenseSet<const ScopArrayInfo *>>
3372 BatchAAResults BAA(
AA);
3373 AliasSetTracker AST(BAA);
3375 DenseMap<Value *, MemoryAccess *> PtrToAcc;
3376 DenseSet<const ScopArrayInfo *> HasWriteAccess;
3379 isl::set StmtDomain = Stmt.getDomain();
3380 bool StmtDomainEmpty = StmtDomain.
is_empty();
3383 if (StmtDomainEmpty)
3387 if (MA->isScalarKind())
3390 HasWriteAccess.insert(MA->getScopArrayInfo());
3392 if (MA->isRead() && isa<MemTransferInst>(Acc))
3393 PtrToAcc[cast<MemTransferInst>(Acc)->getRawSource()] = MA;
3401 for (AliasSet &AS : AST) {
3402 if (AS.isMustAlias() || AS.isForwardingAliasSet())
3405 for (
const Value *Ptr : AS.getPointers())
3406 AG.push_back(PtrToAcc[
const_cast<Value *
>(Ptr)]);
3409 AliasGroups.push_back(std::move(AG));
3412 return std::make_tuple(AliasGroups, HasWriteAccess);
3422 DenseSet<const ScopArrayInfo *> HasWriteAccess;
3429 if (!
scop->hasFeasibleRuntimeContext())
3448 AliasGroupTy &AliasGroup, DenseSet<const ScopArrayInfo *> HasWriteAccess) {
3451 SmallPtrSet<const ScopArrayInfo *, 4> ReadWriteArrays;
3452 SmallPtrSet<const ScopArrayInfo *, 4> ReadOnlyArrays;
3454 if (AliasGroup.size() < 2)
3458 ORE.emit(OptimizationRemarkAnalysis(
DEBUG_TYPE,
"PossibleAlias",
3459 Access->getAccessInstruction())
3460 <<
"Possibly aliasing pointer, use restrict keyword.");
3462 if (HasWriteAccess.count(
Array)) {
3463 ReadWriteArrays.insert(
Array);
3464 ReadWriteAccesses.push_back(Access);
3466 ReadOnlyArrays.insert(
Array);
3467 ReadOnlyAccesses.push_back(Access);
3473 if (ReadOnlyAccesses.empty() && ReadWriteArrays.size() <= 1)
3477 if (ReadWriteArrays.empty())
3483 if (!MA->isAffine()) {
3484 scop->invalidate(
ALIASING, MA->getAccessInstruction()->getDebugLoc(),
3485 MA->getAccessInstruction()->getParent());
3494 scop->addRequiredInvariantLoad(
3495 cast<LoadInst>(BasePtrMA->getAccessInstruction()));
3513 if (MinMaxAccessesReadWrite.size() + ReadOnlyArrays.size() >
3519 scop->addAliasGroup(MinMaxAccessesReadWrite, MinMaxAccessesReadOnly);
3527 for (
unsigned u = 0; u < AliasGroups.size(); u++) {
3530 AliasGroupTy::iterator AGI = AG.begin();
3532 while (AGI != AG.end()) {
3536 NewAG.push_back(MA);
3537 AGI = AG.erase(AGI);
3539 AGDomain = AGDomain.
unite(MADomain);
3543 if (NewAG.size() > 1)
3544 AliasGroups.push_back(std::move(NewAG));
3552 assert(PhysUse.getKind() == VirtUse.getKind());
3571 for (
auto *BB :
S->getRegion().blocks()) {
3572 for (
auto &Inst : *BB) {
3573 auto *Stmt =
S->getStmtFor(&Inst);
3581 if (Inst.isTerminator() && Stmt->isBlockStmt())
3585 for (
auto &Op : Inst.operands())
3589 if (isa<StoreInst>(Inst))
3604 if (
S->hasSingleExitEdge())
3608 if (!
S->getRegion().isTopLevelRegion()) {
3609 for (
auto &Inst : *
S->getRegion().getExit()) {
3610 if (!isa<PHINode>(Inst))
3613 for (
auto &Op : Inst.operands())
3630 for (BasicBlock *BB :
scop->getRegion().blocks()) {
3631 if (
SD.isErrorBlock(*BB,
scop->getRegion()))
3634 for (Instruction &Inst : *BB) {
3635 LoadInst *Load = dyn_cast<LoadInst>(&Inst);
3639 if (!RIL.count(Load))
3646 ArrayRef<ScopStmt *> List =
scop->getStmtListFor(BB);
3661 if (!R.isTopLevelRegion() && !
scop->hasSingleExitEdge()) {
3662 for (Instruction &Inst : *R.getExit()) {
3663 PHINode *
PHI = dyn_cast<PHINode>(&Inst);
3672 const SCEV *AF =
SE.getConstant(IntegerType::getInt64Ty(
SE.getContext()), 0);
3674 ScopStmt *GlobalReadStmt = GlobalReadPair.first;
3675 Instruction *GlobalRead = GlobalReadPair.second;
3678 BP, BP->getType(),
false, {AF}, {nullptr}, GlobalRead);
3684 DenseMap<BasicBlock *, isl::set> InvalidDomainMap;
3688 dbgs() <<
"Bailing-out because buildDomains encountered problems\n");
3704 scop->removeStmtNotInDomainMap();
3705 scop->simplifySCoP(
false);
3706 if (
scop->isEmpty()) {
3707 POLLY_DEBUG(dbgs() <<
"Bailing-out because SCoP is empty\n");
3723 if (!
scop->hasFeasibleRuntimeContext()) {
3725 dbgs() <<
"Bailing-out because of unfeasible context (early)\n");
3734 dbgs() <<
"Bailing-out because SCoP is not considered profitable\n");
3742 scop->realignParams();
3750 scop->simplifyContexts();
3752 POLLY_DEBUG(dbgs() <<
"Bailing-out because could not build alias checks\n");
3759 scop->simplifySCoP(
true);
3763 if (!
scop->hasFeasibleRuntimeContext()) {
3764 POLLY_DEBUG(dbgs() <<
"Bailing-out because of unfeasible context (late)\n");
3774 const DataLayout &
DL, DominatorTree &
DT, LoopInfo &
LI,
3776 OptimizationRemarkEmitter &
ORE)
3782 std::string Msg =
"SCoP begins here.";
3783 ORE.emit(OptimizationRemarkAnalysis(
DEBUG_TYPE,
"ScopEntry", Beg, P.first)
3790 if (!
scop->hasFeasibleRuntimeContext()) {
3792 Msg =
"SCoP ends here but was dismissed.";
3793 POLLY_DEBUG(dbgs() <<
"SCoP detected but dismissed\n");
3797 Msg =
"SCoP ends here.";
3799 if (
scop->getMaxLoopDepth() > 0)
3803 if (R->isTopLevelRegion())
3804 ORE.emit(OptimizationRemarkAnalysis(
DEBUG_TYPE,
"ScopEnd", End, P.first)
3807 ORE.emit(OptimizationRemarkAnalysis(
DEBUG_TYPE,
"ScopEnd", End, P.second)
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))
llvm::cl::OptionCategory PollyCategory
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))
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)
__isl_give isl_pw_aff * isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
__isl_give isl_space * isl_pw_aff_get_domain_space(__isl_keep isl_pw_aff *pwaff)
__isl_export __isl_give isl_set * isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2)
__isl_export __isl_give isl_set * isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2)
__isl_give isl_pw_aff * isl_pw_aff_copy(__isl_keep isl_pw_aff *pwaff)
isl::val get_denominator_val() const
isl::aff add_constant_si(int v) const
isl::aff get_div(int pos) const
boolean is_equal(const isl::basic_set &bset2) const
class size dim(isl::dim type) const
isl::basic_set fix_si(isl::dim type, unsigned int pos, int value) 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)
isl::map add_constraint(isl::constraint constraint) const
isl::map equate(isl::dim type1, int pos1, isl::dim type2, int pos2) const
static isl::map universe(isl::space space)
class size domain_tuple_dim() const
isl::map set_tuple_id(isl::dim type, isl::id id) const
isl::map unite(isl::map map2) const
isl::space get_space() const
boolean has_equal_space(const isl::map &map2) const
isl::map intersect_domain(isl::set set) const
static isl::map lex_le(isl::space set_space)
__isl_give isl_map * copy() const &
boolean involves_dims(isl::dim type, unsigned int first, unsigned int n) const
isl::set lt_set(isl::pw_aff pwaff2) const
isl::set le_set(isl::pw_aff pwaff2) const
isl::space get_domain_space() const
isl::set eq_set(isl::pw_aff pwaff2) const
isl::set ne_set(isl::pw_aff pwaff2) const
isl::set ge_set(isl::pw_aff pwaff2) const
isl::multi_pw_aff add(const isl::multi_pw_aff &multi2) const
isl::set gt_set(isl::pw_aff pwaff2) const
isl::pw_aff at(int pos) const
class size dim(isl::dim type) const
isl::multi_pw_aff set_pw_aff(int pos, const isl::pw_aff &el) const
isl::pw_multi_aff coalesce() const
static isl::pw_multi_aff project_out_map(isl::space space, isl::dim type, unsigned int first, unsigned int n)
isl::schedule_node insert_mark(isl::id mark) const
isl::schedule_node child(int pos) const
isl::schedule get_schedule() const
isl::schedule insert_partial_schedule(isl::multi_union_pw_aff partial) const
isl::schedule_node get_root() const
static isl::schedule from_domain(isl::union_set domain)
isl::union_set get_domain() const
isl::schedule sequence(isl::schedule schedule2) const
isl::set project_out(isl::dim type, unsigned int first, unsigned int n) const
isl::set intersect(isl::set set2) const
isl::set subtract(isl::set set2) const
boolean involves_dims(isl::dim type, unsigned int first, unsigned int n) const
isl::set set_dim_id(isl::dim type, unsigned int pos, isl::id id) const
isl::set insert_dims(isl::dim type, unsigned int pos, unsigned int n) const
int find_dim_by_id(isl::dim type, const isl::id &id) const
static isl::set universe(isl::space space)
class size n_basic_set() const
__isl_give isl_set * copy() const &
isl::set complement() const
isl::set gist_params(isl::set context) const
isl::pw_multi_aff lexmax_pw_multi_aff() const
boolean is_subset(const isl::set &set2) const
isl::set remove_dims(isl::dim type, unsigned int first, unsigned int n) const
isl::pw_multi_aff lexmin_pw_multi_aff() const
isl::set detect_equalities() const
std::string get_dim_name(isl::dim type, unsigned int pos) const
isl::set set_tuple_id(isl::id id) const
isl::set coalesce() const
static isl::set empty(isl::space space)
class size tuple_dim() const
isl::set add_constraint(isl::constraint constraint) const
isl::space get_space() const
isl::set apply(isl::map map) const
__isl_give isl_set * release()
isl::set lower_bound_si(isl::dim type, unsigned int pos, int value) const
__isl_keep isl_set * get() const
class size dim(isl::dim type) const
isl::set add_dims(isl::dim type, unsigned int n) const
isl::set eliminate(isl::dim type, unsigned int first, unsigned int n) const
boolean is_disjoint(const isl::set &set2) const
isl::set unite(isl::set set2) const
isl::basic_set_list get_basic_set_list() const
boolean is_equal(const isl::set &set2) const
isl::basic_set simple_hull() const
isl::set remove_divs() const
isl::basic_set affine_hull() const
isl::set project_out_all_params() const
class size dim(isl::dim type) const
isl::id get_dim_id(isl::dim type, unsigned int pos) const
isl::space map_from_set() const
isl::space align_params(isl::space space2) const
isl::union_set range() const
isl::union_map unite(isl::union_map umap2) const
isl::union_map intersect_range(isl::space space) const
static isl::union_map empty(isl::ctx ctx)
isl::union_map intersect_domain(isl::space space) const
static isl::union_pw_multi_aff empty(isl::space space)
boolean contains(const isl::space &space) const
isl::set_list get_set_list() const
isl::set extract_set(isl::space space) const
isl::space get_space() const
Scoped limit of ISL operations.
Utility proxy to wrap the common members of LoadInst and StoreInst.
llvm::Value * getValueOperand() const
static MemAccInst dyn_cast(llvm::Value &V)
llvm::Value * getPointerOperand() const
Represent memory accesses in statements.
void addIncoming(BasicBlock *IncomingBlock, Value *IncomingValue)
Add a new incoming block/value pairs for this PHI/ExitPHI access.
void dump() const
Print the MemoryAccess to stderr.
SmallVector< const SCEV *, 4 > Sizes
Size of each dimension of the accessed array.
AccessType
The access type of a memory access.
ReductionType
Reduction access type.
@ RT_BOTTOM
Pseudo type for the data flow analysis.
@ RT_NONE
Indicate no reduction at all.
bool isValueKind() const
Old name of isOriginalValueKind().
bool isPHIKind() const
Old name of isOriginalPHIKind.
bool isWrite() const
Is this a write memory access?
Instruction * getAccessInstruction() const
Return the access instruction of this memory access.
iterator_range< SubscriptsTy::const_iterator > subscripts() const
Return an iterator range containing the subscripts.
bool isExitPHIKind() const
Old name of isOriginalExitPHIKind().
bool isRead() const
Is this a read memory access?
void buildAccessRelation(const ScopArrayInfo *SAI)
Assemble the access relation from all available information.
bool isScalarKind() const
Old name of isOriginalScalarKind.
Type * getElementType() const
Return the element type of the accessed array wrt. this access.
const ScopArrayInfo * getScopArrayInfo() const
Legacy name of getOriginalScopArrayInfo().
Value * getOriginalBaseAddr() const
Get the original base address of this access (e.g.
ScopStmt * getStatement() const
Get the statement that contains this memory access.
bool isAffine() const
Is the memory access affine?
isl::map getAccessRelation() const
Old name of getLatestAccessRelation().
bool isMemoryIntrinsic() const
Is this a memory intrinsic access (memcpy, memset, memmove)?
A class to store information about arrays in the SCoP.
bool isCompatibleWith(const ScopArrayInfo *Array) const
Verify that Array is compatible to this ScopArrayInfo.
isl::id getBasePtrId() const
Return the isl id for the base pointer.
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.
ScalarEvolution & SE
The ScalarEvolution to help building Scop.
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.
bool buildConditionSets(BasicBlock *BB, Instruction *TI, Loop *L, __isl_keep isl_set *Domain, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, SmallVectorImpl< __isl_give isl_set * > &ConditionSets)
Build the conditions sets for the terminator TI in the Domain.
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.
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.
SmallVector< AliasGroupTy, 4 > AliasGroupVectorTy
A vector of alias groups.
AAResults & AA
The AAResults to build AliasSetTracker.
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.
__isl_give isl_set * buildUnsignedConditionSets(BasicBlock *BB, Value *Condition, __isl_keep isl_set *Domain, const SCEV *SCEV_TestVal, const SCEV *SCEV_UpperBound, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, bool IsStrictUpperBound)
Build condition sets for unsigned ICmpInst(s).
const DataLayout & DL
Target data for element size computing.
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 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.
bool shouldModelInst(Instruction *Inst, Loop *L)
Should an instruction be modeled in a ScopStmt.
std::unique_ptr< Scop > scop
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.
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.
OptimizationRemarkEmitter & ORE
An optimization diagnostic interface to add optimization remarks.
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_give isl_pw_aff * getPwAff(BasicBlock *BB, DenseMap< BasicBlock *, isl::set > &InvalidDomainMap, const SCEV *E, bool NonNegative=false)
Compute the isl representation for the SCEV E in this BB.
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.
MemoryAccess & getArrayAccessFor(const Instruction *Inst) const
Return the only array access for Inst.
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.
const std::vector< Instruction * > & getInstructions() const
bool isBlockStmt() const
Return true if this statement represents a single basic block.
isl::set getInvalidContext() const
Get the invalid context for this statement.
SmallVector< Loop *, 4 > NestLoops
Region * getRegion() const
Get the region represented by this ScopStmt (if any).
bool represents(BasicBlock *BB) const
Return whether this statement represents BB.
BasicBlock * getBasicBlock() const
Get the BasicBlock represented by this ScopStmt (if any).
MemoryAccessVec MemAccs
The memory accesses of this statement.
const char * getBaseName() const
bool contains(const Loop *L) const
Return whether L is boxed within this statement.
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.
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,...
Loop * getSurroundingLoop() const
Return the closest innermost loop that contains this statement, but is not contained in it.
MemoryAccess * lookupPHIWriteOf(PHINode *PHI) const
Return the PHI write MemoryAccess for the incoming values from any basic block in this ScopStmt,...
MemoryAccess * lookupValueReadOf(Value *Inst) const
Return the MemoryAccess that reloads a value, or nullptr if not existing, respectively not yet added.
SmallVector< MinMaxAccessTy, 4 > MinMaxVectorTy
Vector of minimal/maximal accesses to different arrays.
static void incrementNumberOfAliasingAssumptions(unsigned Step)
Increment actual number of aliasing assumptions taken.
const Region & getRegion() const
Get the maximum region of this static control part.
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)
__isl_null isl_id * isl_id_free(__isl_take isl_id *id)
void * isl_id_get_user(__isl_keep isl_id *id)
__isl_give isl_local_space * isl_local_space_from_space(__isl_take isl_space *space)
aff manage_copy(__isl_keep isl_aff *ptr)
boolean manage(isl_bool val)
std::forward_list< MemoryAccess * > MemoryAccessList
Ordered list type to hold accesses.
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
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.
llvm::Value * getConditionFromTerminator(llvm::Instruction *TI)
Return the condition for the terminator TI.
SmallVector< InvariantAccess, 8 > InvariantAccessesTy
Ordered container type to hold invariant accesses.
llvm::SetVector< llvm::AssertingVH< llvm::LoadInst > > InvariantLoadsSetTy
Type for a set of invariant loads.
llvm::SetVector< const llvm::SCEV * > ParameterSetTy
Set type for parameters.
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.
MemoryKind
The different memory kinds used in Polly.
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
@ Value
MemoryKind::Value: Models an llvm::Value.
@ PHI
MemoryKind::PHI: Models PHI nodes within the SCoP.
@ ExitPHI
MemoryKind::ExitPHI: Models PHI nodes in the SCoP's exit block.
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.
void simplify(isl::set &Set)
Simplify a set inplace.
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.
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
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.
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)
__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)
__isl_export __isl_give isl_set * isl_set_union(__isl_take isl_set *set1, __isl_take isl_set *set2)
isl_size isl_set_n_param(__isl_keep isl_set *set)
__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)
__isl_give isl_set * isl_set_copy(__isl_keep isl_set *set)
__isl_give isl_set * isl_set_project_out(__isl_take isl_set *set, enum isl_dim_type type, unsigned first, unsigned n)
__isl_export isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
__isl_export __isl_give isl_set * isl_set_intersect(__isl_take isl_set *set1, __isl_take isl_set *set2)
__isl_give isl_id * isl_set_get_dim_id(__isl_keep isl_set *set, enum isl_dim_type type, unsigned pos)
__isl_export __isl_give isl_set * isl_set_empty(__isl_take isl_space *space)
__isl_export __isl_give isl_set * isl_set_params(__isl_take isl_set *set)
__isl_give isl_space * isl_space_set_alloc(isl_ctx *ctx, unsigned nparam, unsigned dim)
Helper struct to remember assumptions.
Type for equivalent invariant accesses and their domain context.
MemoryAccessList InvariantAccesses
Memory accesses now treated invariant.
static TupleKindPtr Domain("Domain")