1 //===- ScopHelper.cpp - Some Helper Functions for Scop. ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Small functions that help with Scop and LLVM-IR.
11 //===----------------------------------------------------------------------===//
13 #include "polly/Support/ScopHelper.h"
14 #include "polly/Options.h"
15 #include "polly/ScopInfo.h"
16 #include "polly/Support/SCEVValidator.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 #include "llvm/Transforms/Utils/LoopUtils.h"
23 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
27 using namespace polly
;
29 #define DEBUG_TYPE "polly-scop-helper"
31 static cl::list
<std::string
> DebugFunctions(
33 cl::desc("Allow calls to the specified functions in SCoPs even if their "
34 "side-effects are unknown. This can be used to do debug output in "
35 "Polly-transformed code."),
36 cl::Hidden
, cl::CommaSeparated
, cl::cat(PollyCategory
));
38 // Ensures that there is just one predecessor to the entry node from outside the
40 // The identity of the region entry node is preserved.
41 static void simplifyRegionEntry(Region
*R
, DominatorTree
*DT
, LoopInfo
*LI
,
43 BasicBlock
*EnteringBB
= R
->getEnteringBlock();
44 BasicBlock
*Entry
= R
->getEntry();
52 // Entry <--\ Entry <--\ //
56 // Create single entry edge if the region has multiple entry edges.
58 SmallVector
<BasicBlock
*, 4> Preds
;
59 for (BasicBlock
*P
: predecessors(Entry
))
63 BasicBlock
*NewEntering
=
64 SplitBlockPredecessors(Entry
, Preds
, ".region_entering", DT
, LI
);
67 // The exit block of predecessing regions must be changed to NewEntering
68 for (BasicBlock
*ExitPred
: predecessors(NewEntering
)) {
69 Region
*RegionOfPred
= RI
->getRegionFor(ExitPred
);
70 if (RegionOfPred
->getExit() != Entry
)
73 while (!RegionOfPred
->isTopLevelRegion() &&
74 RegionOfPred
->getExit() == Entry
) {
75 RegionOfPred
->replaceExit(NewEntering
);
76 RegionOfPred
= RegionOfPred
->getParent();
80 // Make all ancestors use EnteringBB as entry; there might be edges to it
81 Region
*AncestorR
= R
->getParent();
82 RI
->setRegionFor(NewEntering
, AncestorR
);
83 while (!AncestorR
->isTopLevelRegion() && AncestorR
->getEntry() == Entry
) {
84 AncestorR
->replaceEntry(NewEntering
);
85 AncestorR
= AncestorR
->getParent();
89 EnteringBB
= NewEntering
;
91 assert(R
->getEnteringBlock() == EnteringBB
);
104 // Ensure that the region has a single block that branches to the exit node.
105 static void simplifyRegionExit(Region
*R
, DominatorTree
*DT
, LoopInfo
*LI
,
107 BasicBlock
*ExitBB
= R
->getExit();
108 BasicBlock
*ExitingBB
= R
->getExitingBlock();
112 // (Region) ______/ //
118 SmallVector
<BasicBlock
*, 4> Preds
;
119 for (BasicBlock
*P
: predecessors(ExitBB
))
123 // Preds[0] Preds[1] otherBB //
128 SplitBlockPredecessors(ExitBB
, Preds
, ".region_exiting", DT
, LI
);
129 // Preds[0] Preds[1] otherBB //
131 // BB.region_exiting / //
136 RI
->setRegionFor(ExitingBB
, R
);
138 // Change the exit of nested regions, but not the region itself,
139 R
->replaceExitRecursive(ExitingBB
);
140 R
->replaceExit(ExitBB
);
142 assert(ExitingBB
== R
->getExitingBlock());
147 // ExitingBB _____/ //
153 void polly::simplifyRegion(Region
*R
, DominatorTree
*DT
, LoopInfo
*LI
,
155 assert(R
&& !R
->isTopLevelRegion());
156 assert(!RI
|| RI
== R
->getRegionInfo());
157 assert((!RI
|| DT
) &&
158 "RegionInfo requires DominatorTree to be updated as well");
160 simplifyRegionEntry(R
, DT
, LI
, RI
);
161 simplifyRegionExit(R
, DT
, LI
, RI
);
162 assert(R
->isSimple());
165 // Split the block into two successive blocks.
167 // Like llvm::SplitBlock, but also preserves RegionInfo
168 static BasicBlock
*splitBlock(BasicBlock
*Old
, Instruction
*SplitPt
,
169 DominatorTree
*DT
, llvm::LoopInfo
*LI
,
171 assert(Old
&& SplitPt
);
179 BasicBlock
*NewBlock
= llvm::SplitBlock(Old
, SplitPt
, DT
, LI
);
182 Region
*R
= RI
->getRegionFor(Old
);
183 RI
->setRegionFor(NewBlock
, R
);
197 void polly::splitEntryBlockForAlloca(BasicBlock
*EntryBlock
, DominatorTree
*DT
,
198 LoopInfo
*LI
, RegionInfo
*RI
) {
199 // Find first non-alloca instruction. Every basic block has a non-alloca
200 // instruction, as every well formed basic block has a terminator.
201 BasicBlock::iterator I
= EntryBlock
->begin();
202 while (isa
<AllocaInst
>(I
))
205 // splitBlock updates DT, LI and RI.
206 splitBlock(EntryBlock
, &*I
, DT
, LI
, RI
);
209 void polly::splitEntryBlockForAlloca(BasicBlock
*EntryBlock
, Pass
*P
) {
210 auto *DTWP
= P
->getAnalysisIfAvailable
<DominatorTreeWrapperPass
>();
211 auto *DT
= DTWP
? &DTWP
->getDomTree() : nullptr;
212 auto *LIWP
= P
->getAnalysisIfAvailable
<LoopInfoWrapperPass
>();
213 auto *LI
= LIWP
? &LIWP
->getLoopInfo() : nullptr;
214 RegionInfoPass
*RIP
= P
->getAnalysisIfAvailable
<RegionInfoPass
>();
215 RegionInfo
*RI
= RIP
? &RIP
->getRegionInfo() : nullptr;
217 // splitBlock updates DT, LI and RI.
218 polly::splitEntryBlockForAlloca(EntryBlock
, DT
, LI
, RI
);
221 void polly::recordAssumption(polly::RecordedAssumptionsTy
*RecordedAssumptions
,
222 polly::AssumptionKind Kind
, isl::set Set
,
223 DebugLoc Loc
, polly::AssumptionSign Sign
,
224 BasicBlock
*BB
, bool RTC
) {
225 assert((Set
.is_params() || BB
) &&
226 "Assumptions without a basic block must be parameter sets");
227 if (RecordedAssumptions
)
228 RecordedAssumptions
->push_back({Kind
, Sign
, Set
, Loc
, BB
, RTC
});
231 /// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
232 /// instruction but just use it, if it is referenced as a SCEVUnknown. We want
233 /// however to generate new code if the instruction is in the analyzed region
234 /// and we generate code outside/in front of that region. Hence, we generate the
235 /// code for the SDiv/SRem operands in front of the analyzed region and then
236 /// create a new SDiv/SRem operation there too.
237 struct ScopExpander final
: SCEVVisitor
<ScopExpander
, const SCEV
*> {
238 friend struct SCEVVisitor
<ScopExpander
, const SCEV
*>;
240 explicit ScopExpander(const Region
&R
, ScalarEvolution
&SE
,
241 const DataLayout
&DL
, const char *Name
, ValueMapT
*VMap
,
243 : Expander(SE
, DL
, Name
, /*PreserveLCSSA=*/false), SE(SE
), Name(Name
),
244 R(R
), VMap(VMap
), RTCBB(RTCBB
) {}
246 Value
*expandCodeFor(const SCEV
*E
, Type
*Ty
, Instruction
*I
) {
247 // If we generate code in the region we will immediately fall back to the
248 // SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
249 // needed replace them by copies computed in the entering block.
252 return Expander
.expandCodeFor(E
, Ty
, I
);
255 const SCEV
*visit(const SCEV
*E
) {
256 // Cache the expansion results for intermediate SCEV expressions. A SCEV
257 // expression can refer to an operand multiple times (e.g. "x*x), so
258 // a naive visitor takes exponential time.
259 if (SCEVCache
.count(E
))
261 const SCEV
*Result
= SCEVVisitor::visit(E
);
262 SCEVCache
[E
] = Result
;
267 SCEVExpander Expander
;
273 DenseMap
<const SCEV
*, const SCEV
*> SCEVCache
;
275 const SCEV
*visitGenericInst(const SCEVUnknown
*E
, Instruction
*Inst
,
277 if (!Inst
|| !R
.contains(Inst
))
280 assert(!Inst
->mayThrow() && !Inst
->mayReadOrWriteMemory() &&
281 !isa
<PHINode
>(Inst
));
283 auto *InstClone
= Inst
->clone();
284 for (auto &Op
: Inst
->operands()) {
285 assert(SE
.isSCEVable(Op
->getType()));
286 auto *OpSCEV
= SE
.getSCEV(Op
);
287 auto *OpClone
= expandCodeFor(OpSCEV
, Op
->getType(), IP
);
288 InstClone
->replaceUsesOfWith(Op
, OpClone
);
291 InstClone
->setName(Name
+ Inst
->getName());
292 InstClone
->insertBefore(IP
);
293 return SE
.getSCEV(InstClone
);
296 const SCEV
*visitUnknown(const SCEVUnknown
*E
) {
298 // If a value mapping was given try if the underlying value is remapped.
299 Value
*NewVal
= VMap
? VMap
->lookup(E
->getValue()) : nullptr;
301 auto *NewE
= SE
.getSCEV(NewVal
);
303 // While the mapped value might be different the SCEV representation might
304 // not be. To this end we will check before we go into recursion here.
309 Instruction
*Inst
= dyn_cast
<Instruction
>(E
->getValue());
311 if (Inst
&& !R
.contains(Inst
))
313 else if (Inst
&& RTCBB
->getParent() == Inst
->getFunction())
314 IP
= RTCBB
->getTerminator();
316 IP
= RTCBB
->getParent()->getEntryBlock().getTerminator();
318 if (!Inst
|| (Inst
->getOpcode() != Instruction::SRem
&&
319 Inst
->getOpcode() != Instruction::SDiv
))
320 return visitGenericInst(E
, Inst
, IP
);
322 const SCEV
*LHSScev
= SE
.getSCEV(Inst
->getOperand(0));
323 const SCEV
*RHSScev
= SE
.getSCEV(Inst
->getOperand(1));
325 if (!SE
.isKnownNonZero(RHSScev
))
326 RHSScev
= SE
.getUMaxExpr(RHSScev
, SE
.getConstant(E
->getType(), 1));
328 Value
*LHS
= expandCodeFor(LHSScev
, E
->getType(), IP
);
329 Value
*RHS
= expandCodeFor(RHSScev
, E
->getType(), IP
);
332 BinaryOperator::Create((Instruction::BinaryOps
)Inst
->getOpcode(), LHS
,
333 RHS
, Inst
->getName() + Name
, IP
->getIterator());
334 return SE
.getSCEV(Inst
);
337 /// The following functions will just traverse the SCEV and rebuild it with
338 /// the new operands returned by the traversal.
341 const SCEV
*visitConstant(const SCEVConstant
*E
) { return E
; }
342 const SCEV
*visitVScale(const SCEVVScale
*E
) { return E
; }
343 const SCEV
*visitPtrToIntExpr(const SCEVPtrToIntExpr
*E
) {
344 return SE
.getPtrToIntExpr(visit(E
->getOperand()), E
->getType());
346 const SCEV
*visitTruncateExpr(const SCEVTruncateExpr
*E
) {
347 return SE
.getTruncateExpr(visit(E
->getOperand()), E
->getType());
349 const SCEV
*visitZeroExtendExpr(const SCEVZeroExtendExpr
*E
) {
350 return SE
.getZeroExtendExpr(visit(E
->getOperand()), E
->getType());
352 const SCEV
*visitSignExtendExpr(const SCEVSignExtendExpr
*E
) {
353 return SE
.getSignExtendExpr(visit(E
->getOperand()), E
->getType());
355 const SCEV
*visitUDivExpr(const SCEVUDivExpr
*E
) {
356 auto *RHSScev
= visit(E
->getRHS());
357 if (!SE
.isKnownNonZero(RHSScev
))
358 RHSScev
= SE
.getUMaxExpr(RHSScev
, SE
.getConstant(E
->getType(), 1));
359 return SE
.getUDivExpr(visit(E
->getLHS()), RHSScev
);
361 const SCEV
*visitAddExpr(const SCEVAddExpr
*E
) {
362 SmallVector
<const SCEV
*, 4> NewOps
;
363 for (const SCEV
*Op
: E
->operands())
364 NewOps
.push_back(visit(Op
));
365 return SE
.getAddExpr(NewOps
);
367 const SCEV
*visitMulExpr(const SCEVMulExpr
*E
) {
368 SmallVector
<const SCEV
*, 4> NewOps
;
369 for (const SCEV
*Op
: E
->operands())
370 NewOps
.push_back(visit(Op
));
371 return SE
.getMulExpr(NewOps
);
373 const SCEV
*visitUMaxExpr(const SCEVUMaxExpr
*E
) {
374 SmallVector
<const SCEV
*, 4> NewOps
;
375 for (const SCEV
*Op
: E
->operands())
376 NewOps
.push_back(visit(Op
));
377 return SE
.getUMaxExpr(NewOps
);
379 const SCEV
*visitSMaxExpr(const SCEVSMaxExpr
*E
) {
380 SmallVector
<const SCEV
*, 4> NewOps
;
381 for (const SCEV
*Op
: E
->operands())
382 NewOps
.push_back(visit(Op
));
383 return SE
.getSMaxExpr(NewOps
);
385 const SCEV
*visitUMinExpr(const SCEVUMinExpr
*E
) {
386 SmallVector
<const SCEV
*, 4> NewOps
;
387 for (const SCEV
*Op
: E
->operands())
388 NewOps
.push_back(visit(Op
));
389 return SE
.getUMinExpr(NewOps
);
391 const SCEV
*visitSMinExpr(const SCEVSMinExpr
*E
) {
392 SmallVector
<const SCEV
*, 4> NewOps
;
393 for (const SCEV
*Op
: E
->operands())
394 NewOps
.push_back(visit(Op
));
395 return SE
.getSMinExpr(NewOps
);
397 const SCEV
*visitSequentialUMinExpr(const SCEVSequentialUMinExpr
*E
) {
398 SmallVector
<const SCEV
*, 4> NewOps
;
399 for (const SCEV
*Op
: E
->operands())
400 NewOps
.push_back(visit(Op
));
401 return SE
.getUMinExpr(NewOps
, /*Sequential=*/true);
403 const SCEV
*visitAddRecExpr(const SCEVAddRecExpr
*E
) {
404 SmallVector
<const SCEV
*, 4> NewOps
;
405 for (const SCEV
*Op
: E
->operands())
406 NewOps
.push_back(visit(Op
));
407 return SE
.getAddRecExpr(NewOps
, E
->getLoop(), E
->getNoWrapFlags());
412 Value
*polly::expandCodeFor(Scop
&S
, ScalarEvolution
&SE
, const DataLayout
&DL
,
413 const char *Name
, const SCEV
*E
, Type
*Ty
,
414 Instruction
*IP
, ValueMapT
*VMap
,
416 ScopExpander
Expander(S
.getRegion(), SE
, DL
, Name
, VMap
, RTCBB
);
417 return Expander
.expandCodeFor(E
, Ty
, IP
);
420 Value
*polly::getConditionFromTerminator(Instruction
*TI
) {
421 if (BranchInst
*BR
= dyn_cast
<BranchInst
>(TI
)) {
422 if (BR
->isUnconditional())
423 return ConstantInt::getTrue(Type::getInt1Ty(TI
->getContext()));
425 return BR
->getCondition();
428 if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(TI
))
429 return SI
->getCondition();
434 Loop
*polly::getLoopSurroundingScop(Scop
&S
, LoopInfo
&LI
) {
435 // Start with the smallest loop containing the entry and expand that
436 // loop until it contains all blocks in the region. If there is a loop
437 // containing all blocks in the region check if it is itself contained
438 // and if so take the parent loop as it will be the smallest containing
439 // the region but not contained by it.
440 Loop
*L
= LI
.getLoopFor(S
.getEntry());
442 bool AllContained
= true;
443 for (auto *BB
: S
.blocks())
444 AllContained
&= L
->contains(BB
);
447 L
= L
->getParentLoop();
450 return L
? (S
.contains(L
) ? L
->getParentLoop() : L
) : nullptr;
453 unsigned polly::getNumBlocksInLoop(Loop
*L
) {
454 unsigned NumBlocks
= L
->getNumBlocks();
455 SmallVector
<BasicBlock
*, 4> ExitBlocks
;
456 L
->getExitBlocks(ExitBlocks
);
458 for (auto ExitBlock
: ExitBlocks
) {
459 if (isa
<UnreachableInst
>(ExitBlock
->getTerminator()))
465 unsigned polly::getNumBlocksInRegionNode(RegionNode
*RN
) {
466 if (!RN
->isSubRegion())
469 Region
*R
= RN
->getNodeAs
<Region
>();
470 return std::distance(R
->block_begin(), R
->block_end());
473 Loop
*polly::getRegionNodeLoop(RegionNode
*RN
, LoopInfo
&LI
) {
474 if (!RN
->isSubRegion()) {
475 BasicBlock
*BB
= RN
->getNodeAs
<BasicBlock
>();
476 Loop
*L
= LI
.getLoopFor(BB
);
478 // Unreachable statements are not considered to belong to a LLVM loop, as
479 // they are not part of an actual loop in the control flow graph.
480 // Nevertheless, we handle certain unreachable statements that are common
481 // when modeling run-time bounds checks as being part of the loop to be
482 // able to model them and to later eliminate the run-time bounds checks.
484 // Specifically, for basic blocks that terminate in an unreachable and
485 // where the immediate predecessor is part of a loop, we assume these
486 // basic blocks belong to the loop the predecessor belongs to. This
487 // allows us to model the following code.
489 // for (i = 0; i < N; i++) {
491 // abort(); <- this abort might be translated to an
496 if (!L
&& isa
<UnreachableInst
>(BB
->getTerminator()) && BB
->getPrevNode())
497 L
= LI
.getLoopFor(BB
->getPrevNode());
501 Region
*NonAffineSubRegion
= RN
->getNodeAs
<Region
>();
502 Loop
*L
= LI
.getLoopFor(NonAffineSubRegion
->getEntry());
503 while (L
&& NonAffineSubRegion
->contains(L
))
504 L
= L
->getParentLoop();
508 static bool hasVariantIndex(GetElementPtrInst
*Gep
, Loop
*L
, Region
&R
,
509 ScalarEvolution
&SE
) {
510 for (const Use
&Val
: llvm::drop_begin(Gep
->operands(), 1)) {
511 const SCEV
*PtrSCEV
= SE
.getSCEVAtScope(Val
, L
);
512 Loop
*OuterLoop
= R
.outermostLoopInRegion(L
);
513 if (!SE
.isLoopInvariant(PtrSCEV
, OuterLoop
))
519 bool polly::isHoistableLoad(LoadInst
*LInst
, Region
&R
, LoopInfo
&LI
,
520 ScalarEvolution
&SE
, const DominatorTree
&DT
,
521 const InvariantLoadsSetTy
&KnownInvariantLoads
) {
522 Loop
*L
= LI
.getLoopFor(LInst
->getParent());
523 auto *Ptr
= LInst
->getPointerOperand();
525 // A LoadInst is hoistable if the address it is loading from is also
526 // invariant; in this case: another invariant load (whether that address
527 // is also not written to has to be checked separately)
528 // TODO: This only checks for a LoadInst->GetElementPtrInst->LoadInst
529 // pattern generated by the Chapel frontend, but generally this applies
530 // for any chain of instruction that does not also depend on any
531 // induction variable
532 if (auto *GepInst
= dyn_cast
<GetElementPtrInst
>(Ptr
)) {
533 if (!hasVariantIndex(GepInst
, L
, R
, SE
)) {
534 if (auto *DecidingLoad
=
535 dyn_cast
<LoadInst
>(GepInst
->getPointerOperand())) {
536 if (KnownInvariantLoads
.count(DecidingLoad
))
542 const SCEV
*PtrSCEV
= SE
.getSCEVAtScope(Ptr
, L
);
543 while (L
&& R
.contains(L
)) {
544 if (!SE
.isLoopInvariant(PtrSCEV
, L
))
546 L
= L
->getParentLoop();
549 for (auto *User
: Ptr
->users()) {
550 auto *UserI
= dyn_cast
<Instruction
>(User
);
551 if (!UserI
|| !R
.contains(UserI
))
553 if (!UserI
->mayWriteToMemory())
556 auto &BB
= *UserI
->getParent();
557 if (DT
.dominates(&BB
, LInst
->getParent()))
560 bool DominatesAllPredecessors
= true;
561 if (R
.isTopLevelRegion()) {
562 for (BasicBlock
&I
: *R
.getEntry()->getParent())
563 if (isa
<ReturnInst
>(I
.getTerminator()) && !DT
.dominates(&BB
, &I
))
564 DominatesAllPredecessors
= false;
566 for (auto Pred
: predecessors(R
.getExit()))
567 if (R
.contains(Pred
) && !DT
.dominates(&BB
, Pred
))
568 DominatesAllPredecessors
= false;
571 if (!DominatesAllPredecessors
)
580 bool polly::isIgnoredIntrinsic(const Value
*V
) {
581 if (auto *IT
= dyn_cast
<IntrinsicInst
>(V
)) {
582 switch (IT
->getIntrinsicID()) {
583 // Lifetime markers are supported/ignored.
584 case llvm::Intrinsic::lifetime_start
:
585 case llvm::Intrinsic::lifetime_end
:
586 // Invariant markers are supported/ignored.
587 case llvm::Intrinsic::invariant_start
:
588 case llvm::Intrinsic::invariant_end
:
589 // Some misc annotations are supported/ignored.
590 case llvm::Intrinsic::var_annotation
:
591 case llvm::Intrinsic::ptr_annotation
:
592 case llvm::Intrinsic::annotation
:
593 case llvm::Intrinsic::donothing
:
594 case llvm::Intrinsic::assume
:
595 // Some debug info intrinsics are supported/ignored.
596 case llvm::Intrinsic::dbg_value
:
597 case llvm::Intrinsic::dbg_declare
:
606 bool polly::canSynthesize(const Value
*V
, const Scop
&S
, ScalarEvolution
*SE
,
608 if (!V
|| !SE
->isSCEVable(V
->getType()))
611 const InvariantLoadsSetTy
&ILS
= S
.getRequiredInvariantLoads();
612 if (const SCEV
*Scev
= SE
->getSCEVAtScope(const_cast<Value
*>(V
), Scope
))
613 if (!isa
<SCEVCouldNotCompute
>(Scev
))
614 if (!hasScalarDepsInsideRegion(Scev
, &S
.getRegion(), Scope
, false, ILS
))
620 llvm::BasicBlock
*polly::getUseBlock(const llvm::Use
&U
) {
621 Instruction
*UI
= dyn_cast
<Instruction
>(U
.getUser());
625 if (PHINode
*PHI
= dyn_cast
<PHINode
>(UI
))
626 return PHI
->getIncomingBlock(U
);
628 return UI
->getParent();
631 llvm::Loop
*polly::getFirstNonBoxedLoopFor(llvm::Loop
*L
, llvm::LoopInfo
&LI
,
632 const BoxedLoopsSetTy
&BoxedLoops
) {
633 while (BoxedLoops
.count(L
))
634 L
= L
->getParentLoop();
638 llvm::Loop
*polly::getFirstNonBoxedLoopFor(llvm::BasicBlock
*BB
,
640 const BoxedLoopsSetTy
&BoxedLoops
) {
641 Loop
*L
= LI
.getLoopFor(BB
);
642 return getFirstNonBoxedLoopFor(L
, LI
, BoxedLoops
);
645 bool polly::isDebugCall(Instruction
*Inst
) {
646 auto *CI
= dyn_cast
<CallInst
>(Inst
);
650 Function
*CF
= CI
->getCalledFunction();
654 return std::find(DebugFunctions
.begin(), DebugFunctions
.end(),
655 CF
->getName()) != DebugFunctions
.end();
658 static bool hasDebugCall(BasicBlock
*BB
) {
659 for (Instruction
&Inst
: *BB
) {
660 if (isDebugCall(&Inst
))
666 bool polly::hasDebugCall(ScopStmt
*Stmt
) {
667 // Quick skip if no debug functions have been defined.
668 if (DebugFunctions
.empty())
674 for (Instruction
*Inst
: Stmt
->getInstructions())
675 if (isDebugCall(Inst
))
678 if (Stmt
->isRegionStmt()) {
679 for (BasicBlock
*RBB
: Stmt
->getRegion()->blocks())
680 if (RBB
!= Stmt
->getEntryBlock() && ::hasDebugCall(RBB
))
687 /// Find a property in a LoopID.
688 static MDNode
*findNamedMetadataNode(MDNode
*LoopMD
, StringRef Name
) {
691 for (const MDOperand
&X
: drop_begin(LoopMD
->operands(), 1)) {
692 auto *OpNode
= dyn_cast
<MDNode
>(X
.get());
696 auto *OpName
= dyn_cast
<MDString
>(OpNode
->getOperand(0));
699 if (OpName
->getString() == Name
)
705 static std::optional
<const MDOperand
*> findNamedMetadataArg(MDNode
*LoopID
,
707 MDNode
*MD
= findNamedMetadataNode(LoopID
, Name
);
710 switch (MD
->getNumOperands()) {
714 return &MD
->getOperand(1);
716 llvm_unreachable("loop metadata has 0 or 1 operand");
720 std::optional
<Metadata
*> polly::findMetadataOperand(MDNode
*LoopMD
,
722 MDNode
*MD
= findNamedMetadataNode(LoopMD
, Name
);
725 switch (MD
->getNumOperands()) {
729 return MD
->getOperand(1).get();
731 llvm_unreachable("loop metadata must have 0 or 1 operands");
735 static std::optional
<bool> getOptionalBoolLoopAttribute(MDNode
*LoopID
,
737 MDNode
*MD
= findNamedMetadataNode(LoopID
, Name
);
740 switch (MD
->getNumOperands()) {
744 if (ConstantInt
*IntMD
=
745 mdconst::extract_or_null
<ConstantInt
>(MD
->getOperand(1).get()))
746 return IntMD
->getZExtValue();
749 llvm_unreachable("unexpected number of options");
752 bool polly::getBooleanLoopAttribute(MDNode
*LoopID
, StringRef Name
) {
753 return getOptionalBoolLoopAttribute(LoopID
, Name
).value_or(false);
756 std::optional
<int> polly::getOptionalIntLoopAttribute(MDNode
*LoopID
,
758 const MDOperand
*AttrMD
=
759 findNamedMetadataArg(LoopID
, Name
).value_or(nullptr);
763 ConstantInt
*IntMD
= mdconst::extract_or_null
<ConstantInt
>(AttrMD
->get());
767 return IntMD
->getSExtValue();
770 bool polly::hasDisableAllTransformsHint(Loop
*L
) {
771 return llvm::hasDisableAllTransformsHint(L
);
774 bool polly::hasDisableAllTransformsHint(llvm::MDNode
*LoopID
) {
775 return getBooleanLoopAttribute(LoopID
, "llvm.loop.disable_nonforced");
778 isl::id
polly::getIslLoopAttr(isl::ctx Ctx
, BandAttr
*Attr
) {
779 assert(Attr
&& "Must be a valid BandAttr");
781 // The name "Loop" signals that this id contains a pointer to a BandAttr.
782 // The ScheduleOptimizer also uses the string "Inter iteration alias-free" in
783 // markers, but it's user pointer is an llvm::Value.
784 isl::id Result
= isl::id::alloc(Ctx
, "Loop with Metadata", Attr
);
785 Result
= isl::manage(isl_id_set_free_user(Result
.release(), [](void *Ptr
) {
786 BandAttr
*Attr
= reinterpret_cast<BandAttr
*>(Ptr
);
792 isl::id
polly::createIslLoopAttr(isl::ctx Ctx
, Loop
*L
) {
796 // A loop without metadata does not need to be annotated.
797 MDNode
*LoopID
= L
->getLoopID();
801 BandAttr
*Attr
= new BandAttr();
802 Attr
->OriginalLoop
= L
;
803 Attr
->Metadata
= L
->getLoopID();
805 return getIslLoopAttr(Ctx
, Attr
);
808 bool polly::isLoopAttr(const isl::id
&Id
) {
812 return Id
.get_name() == "Loop with Metadata";
815 BandAttr
*polly::getLoopAttr(const isl::id
&Id
) {
819 return reinterpret_cast<BandAttr
*>(Id
.get_user());