52#define DEBUG_TYPE "loop-peel"
55STATISTIC(NumPeeledEnd,
"Number of loops peeled from end");
59 cl::desc(
"Set the unroll peeling count, for testing purposes"));
63 cl::desc(
"Allows loops to be peeled when the dynamic "
64 "trip count is known to be low."));
69 cl::desc(
"Allows loop nests to be peeled."));
73 cl::desc(
"Max average trip count which will cause loop peeling."));
77 cl::desc(
"Force a peel count regardless of profiling information."));
82 "Disable advance peeling. Issues for convergent targets (D134803)."));
86 cl::desc(
"Enable peeling to convert Phi nodes into IVs"));
93 if (!L->isLoopSimplifyForm())
99 L->getUniqueNonLatchExitBlocks(Exits);
193 PhiAnalyzer(
const Loop &L,
unsigned MaxIterations,
bool PeelForIV);
197 std::optional<unsigned> calculateIterationsToPeel();
200 enum class PeelCounterType {
205 using PeelCounterValue = std::pair<unsigned, PeelCounterType>;
206 using PeelCounter = std::optional<PeelCounterValue>;
207 const PeelCounter
Unknown = std::nullopt;
210 PeelCounter addOne(PeelCounter PC)
const {
213 auto [Val, Ty] = *PC;
214 return (Val + 1 <= MaxIterations) ? PeelCounter({Val + 1, Ty}) : Unknown;
218 PeelCounter makeZero(PeelCounterType Ty)
const {
219 return PeelCounter({0, Ty});
224 PeelCounter calculate(
const Value &);
228 PeelCounter mergeTwoCounters(
const Instruction &CmpOrBinaryOp,
229 const PeelCounterValue &LHS,
230 const PeelCounterValue &RHS)
const;
234 bool isInductionPHI(
const PHINode *Phi)
const;
237 const unsigned MaxIterations;
238 const bool PeelForIV;
244PhiAnalyzer::PhiAnalyzer(
const Loop &L,
unsigned MaxIterations,
bool PeelForIV)
245 :
L(
L), MaxIterations(MaxIterations), PeelForIV(PeelForIV) {
247 assert(MaxIterations > 0 &&
"no peeling is allowed?");
254bool PhiAnalyzer::isInductionPHI(
const PHINode *Phi)
const {
257 if (Latch ==
nullptr)
260 Value *Cur =
Phi->getIncomingValueForBlock(Latch);
262 bool VisitBinOp =
false;
272 if (!Visited.
insert(Cur).second)
275 auto *
I = dyn_cast<Instruction>(Cur);
276 if (!
I || !
L.contains(
I))
279 if (
auto *Cast = dyn_cast<CastInst>(
I)) {
280 Cur = Cast->getOperand(0);
281 }
else if (
auto *BinOp = dyn_cast<BinaryOperator>(
I)) {
282 if (BinOp->getOpcode() != Instruction::Add &&
283 BinOp->getOpcode() != Instruction::Sub)
285 if (!isa<ConstantInt>(BinOp->getOperand(1)))
289 Cur = BinOp->getOperand(0);
305PhiAnalyzer::PeelCounter
306PhiAnalyzer::mergeTwoCounters(
const Instruction &CmpOrBinaryOp,
307 const PeelCounterValue &LHS,
308 const PeelCounterValue &RHS)
const {
309 auto &[LVal, LTy] =
LHS;
310 auto &[RVal, RTy] =
RHS;
311 unsigned NewVal = std::max(LVal, RVal);
313 if (LTy == PeelCounterType::Induction || RTy == PeelCounterType::Induction) {
314 if (
const auto *BinOp = dyn_cast<BinaryOperator>(&CmpOrBinaryOp)) {
315 if (BinOp->getOpcode() == Instruction::Add ||
316 BinOp->getOpcode() == Instruction::Sub)
317 return PeelCounter({NewVal, PeelCounterType::Induction});
321 return PeelCounter({NewVal, PeelCounterType::Invariant});
338PhiAnalyzer::PeelCounter PhiAnalyzer::calculate(
const Value &V) {
343 IterationsToInvarianceOrInduction.try_emplace(&V,
Unknown);
347 if (
L.isLoopInvariant(&V))
349 return (IterationsToInvarianceOrInduction[&V] =
350 makeZero(PeelCounterType::Invariant));
351 if (
const PHINode *Phi = dyn_cast<PHINode>(&V)) {
352 if (
Phi->getParent() !=
L.getHeader()) {
355 "unexpected value saved");
360 if (PeelForIV && isInductionPHI(Phi))
361 return (IterationsToInvarianceOrInduction[&V] =
362 makeZero(PeelCounterType::Induction));
365 Value *Input =
Phi->getIncomingValueForBlock(
L.getLoopLatch());
366 PeelCounter Iterations = calculate(*Input);
367 assert(IterationsToInvarianceOrInduction[Input] == Iterations &&
368 "unexpected value saved");
369 return (IterationsToInvarianceOrInduction[Phi] = addOne(Iterations));
372 if (isa<CmpInst>(
I) ||
I->isBinaryOp()) {
374 PeelCounter
LHS = calculate(*
I->getOperand(0));
377 PeelCounter
RHS = calculate(*
I->getOperand(1));
380 return (IterationsToInvarianceOrInduction[
I] =
381 mergeTwoCounters(*
I, *LHS, *RHS));
385 return (IterationsToInvarianceOrInduction[
I] =
386 calculate(*
I->getOperand(0)));
392 "unexpected value saved");
396std::optional<unsigned> PhiAnalyzer::calculateIterationsToPeel() {
397 unsigned Iterations = 0;
398 for (
auto &
PHI :
L.getHeader()->phis()) {
399 PeelCounter ToInvarianceOrInduction = calculate(
PHI);
400 if (ToInvarianceOrInduction !=
Unknown) {
401 unsigned Val = ToInvarianceOrInduction->first;
402 assert(Val <= MaxIterations &&
"bad result in phi analysis");
403 Iterations = std::max(Iterations, Val);
404 if (Iterations == MaxIterations)
408 assert((Iterations <= MaxIterations) &&
"bad result in phi analysis");
409 return Iterations ? std::optional<unsigned>(Iterations) :
std::nullopt;
423 if (L.getExitingBlock())
429 L.getUniqueNonLatchExitBlocks(Exits);
446 if (
I.mayWriteToMemory())
455 if (
auto *LI = dyn_cast<LoadInst>(&
I)) {
456 Value *
Ptr = LI->getPointerOperand();
457 if (DT.
dominates(BB, Latch) && L.isLoopInvariant(
Ptr) &&
464 L.getExitingBlocks(ExitingBlocks);
474 if (isa<SCEVCouldNotCompute>(BTC))
488 return Latch && Latch == L.getExitingBlock() &&
511 SCEVExpander Expander(SE, L.getHeader()->getDataLayout(),
"loop-peel");
514 L.getLoopPredecessor()->getTerminator()))
524 return SE.
isKnownPredicate(ICmpInst::getInversePredicate(Pred), ValAtLastIter,
540static std::pair<unsigned, unsigned>
543 assert(L.isLoopSimplifyForm() &&
"Loop needs to be in loop simplify form");
544 unsigned DesiredPeelCount = 0;
545 unsigned DesiredPeelCountLast = 0;
549 if (
const SCEVConstant *SC = dyn_cast<SCEVConstant>(BE))
551 std::min((
unsigned)SC->getAPInt().getLimitedValue() - 1, MaxPeelCount);
556 auto PeelWhilePredicateIsKnown =
557 [&](
unsigned &PeelCount,
const SCEV *&IterVal,
const SCEV *BoundSCEV,
559 while (PeelCount < MaxPeelCount &&
568 const unsigned MaxDepth = 4;
569 std::function<void(
Value *,
unsigned)> ComputePeelCount =
570 [&](
Value *Condition,
unsigned Depth) ->
void {
574 Value *LeftVal, *RightVal;
577 ComputePeelCount(LeftVal,
Depth + 1);
578 ComputePeelCount(RightVal,
Depth + 1);
596 if (!isa<SCEVAddRecExpr>(LeftSCEV)) {
597 if (isa<SCEVAddRecExpr>(RightSCEV)) {
599 Pred = ICmpInst::getSwappedPredicate(Pred);
616 unsigned NewPeelCount = DesiredPeelCount;
625 Pred = ICmpInst::getInversePredicate(Pred);
628 if (!PeelWhilePredicateIsKnown(NewPeelCount, IterVal, RightSCEV, Step,
631 DesiredPeelCountLast = 1;
644 if (NewPeelCount >= MaxPeelCount)
649 DesiredPeelCount = std::max(DesiredPeelCount, NewPeelCount);
650 DesiredPeelCountLast = std::max(DesiredPeelCountLast, NewPeelCount);
654 if (!
MinMax->getType()->isIntegerTy())
657 const SCEV *BoundSCEV, *IterSCEV;
658 if (L.isLoopInvariant(
LHS)) {
661 }
else if (L.isLoopInvariant(
RHS)) {
666 const auto *AddRec = dyn_cast<SCEVAddRecExpr>(IterSCEV);
668 if (!AddRec || !AddRec->isAffine() || AddRec->getLoop() != &L)
670 const SCEV *Step = AddRec->getStepRecurrence(SE);
671 bool IsSigned =
MinMax->isSigned();
676 Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
678 Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
682 if (!(IsSigned ? AddRec->hasNoSignedWrap() : AddRec->hasNoUnsignedWrap()))
684 unsigned NewPeelCount = DesiredPeelCount;
685 const SCEV *IterVal = AddRec->evaluateAtIteration(
686 SE.
getConstant(AddRec->getType(), NewPeelCount), SE);
687 if (!PeelWhilePredicateIsKnown(NewPeelCount, IterVal, BoundSCEV, Step,
690 DesiredPeelCountLast = 1;
693 DesiredPeelCount = NewPeelCount;
699 ComputePeelCount(SI->getCondition(), 0);
701 ComputePeelCountMinMax(
MinMax);
704 auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
705 if (!BI || BI->isUnconditional())
709 if (L.getLoopLatch() == BB)
712 ComputePeelCount(BI->getCondition(), 0);
715 return {DesiredPeelCount, DesiredPeelCountLast};
728 if (!LatchBR || LatchBR->
getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
733 "At least one edge out of the latch must go to the header");
736 L->getUniqueNonLatchExitBlocks(ExitBlocks);
749 assert(LoopSize > 0 &&
"Zero loop size is not allowed!");
768 <<
" iterations.\n");
779 if (2 * LoopSize > Threshold)
782 unsigned AlreadyPeeled = 0;
784 AlreadyPeeled = *Peeled;
791 MaxPeelCount = std::min(MaxPeelCount, Threshold / LoopSize - 1);
795 unsigned DesiredPeelCount = TargetPeelCount;
802 if (MaxPeelCount > DesiredPeelCount) {
805 .calculateIterationsToPeel();
807 DesiredPeelCount = std::max(DesiredPeelCount, *NumPeels);
810 const auto &[CountToEliminateCmps, CountToEliminateCmpsLast] =
812 DesiredPeelCount = std::max(DesiredPeelCount, CountToEliminateCmps);
814 if (DesiredPeelCount == 0)
817 if (DesiredPeelCount > 0) {
818 DesiredPeelCount = std::min(DesiredPeelCount, MaxPeelCount);
820 assert(DesiredPeelCount > 0 &&
"Wrong loop size estimation?");
823 <<
" iteration(s) to turn"
824 <<
" some Phis into invariants or inductions.\n");
832 if (CountToEliminateCmpsLast > 0) {
833 unsigned DesiredPeelCountLast =
834 std::min(CountToEliminateCmpsLast, MaxPeelCount);
836 assert(DesiredPeelCountLast > 0 &&
"Wrong loop size estimation?");
839 <<
" iteration(s) to turn"
840 <<
" some Phis into invariants.\n");
861 if (L->getHeader()->getParent()->hasProfileData()) {
865 if (!EstimatedTripCount)
869 << *EstimatedTripCount <<
"\n");
871 if (*EstimatedTripCount + AlreadyPeeled <= MaxPeelCount) {
872 unsigned PeelCount = *EstimatedTripCount;
873 LLVM_DEBUG(
dbgs() <<
"Peeling first " << PeelCount <<
" iterations.\n");
877 LLVM_DEBUG(
dbgs() <<
"Already peel count: " << AlreadyPeeled <<
"\n");
882 << (Threshold / LoopSize - 1) <<
"\n");
916 ? std::max(
Info.Weights[
Idx] - SubWeight, SubWeight)
924 L->getExitingBlocks(ExitingBlocks);
925 for (
BasicBlock *ExitingBlock : ExitingBlocks) {
936 if (L->contains(Succ))
937 FallThroughWeights += Weight;
939 ExitWeights += Weight;
943 if (FallThroughWeights == 0)
948 if (!L->contains(Succ)) {
956 double W = (double)Weight / (
double)FallThroughWeights;
960 WeightInfos.
insert({Term, {std::move(Weights), std::move(SubWeights)}});
976 Loop *L,
unsigned IterNumber,
bool PeelLast,
BasicBlock *InsertTop,
985 BasicBlock *PreHeader = L->getLoopPreheader();
990 Loop *ParentLoop = L->getParentLoop();
1022 std::string Ext = (
Twine(
"Peel") +
Twine(IterNumber)).str();
1024 Header->getContext(), Ext);
1029 for (
Loop *ChildLoop : *L) {
1030 cloneLoop(ChildLoop, ParentLoop, VMap, LI,
nullptr);
1041 BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
1045 assert(IterNumber == 0 &&
"Only peeling a single iteration implemented.");
1046 auto *LatchTerm = cast<BranchInst>(NewLatch->
getTerminator());
1047 LatchTerm->setSuccessor(0, InsertBot);
1048 LatchTerm->setSuccessor(1, InsertBot);
1050 auto *LatchTerm = cast<Instruction>(NewLatch->
getTerminator());
1054 for (
unsigned idx = 0, e = LatchTerm->getNumSuccessors(); idx < e; ++idx) {
1055 if (LatchTerm->getSuccessor(idx) == Header) {
1056 LatchTerm->setSuccessor(idx, InsertBot);
1075 PHINode *NewPHI = cast<PHINode>(VMap[&*
I]);
1079 PN->
addIncoming(cast<PHINode>(&*
I)->getIncomingValueForBlock(PreHeader),
1082 PN->
addIncoming(cast<PHINode>(&*
I)->getIncomingValueForBlock(Latch),
1092 PHINode *NewPHI = cast<PHINode>(VMap[&*
I]);
1093 if (IterNumber == 0) {
1097 Instruction *LatchInst = dyn_cast<Instruction>(LatchVal);
1098 if (LatchInst && L->contains(LatchInst))
1099 VMap[&*
I] = LVMap[LatchInst];
1101 VMap[&*
I] = LatchVal;
1111 for (
auto Edge : ExitEdges)
1113 Value *LatchVal =
PHI.getIncomingValueForBlock(
Edge.first);
1114 Instruction *LatchInst = dyn_cast<Instruction>(LatchVal);
1115 if (LatchInst && L->contains(LatchInst))
1116 LatchVal = VMap[LatchVal];
1117 PHI.addIncoming(LatchVal, cast<BasicBlock>(VMap[
Edge.first]));
1123 for (
auto KV : VMap)
1124 LVMap[KV.first] = KV.second;
1130 std::optional<bool> UserAllowPeeling,
1131 std::optional<bool> UserAllowProfileBasedPeeling,
1132 bool UnrollingSpecficValues) {
1146 if (UnrollingSpecficValues) {
1156 if (UserAllowPeeling)
1158 if (UserAllowProfileBasedPeeling)
1176 assert(PeelCount > 0 &&
"Attempt to peel out zero iterations?");
1177 assert(
canPeel(L) &&
"Attempt to peel a loop which is not peelable?");
1179 "when peeling the last iteration, the loop must be supported and can "
1180 "only peel a single iteration");
1186 BasicBlock *PreHeader = L->getLoopPreheader();
1189 L->getExitEdges(ExitEdges);
1196 for (
auto *BB : L->blocks()) {
1197 auto *BBDomNode = DT.
getNode(BB);
1199 for (
auto *ChildDomNode : BBDomNode->children()) {
1200 auto *ChildBB = ChildDomNode->getBlock();
1201 if (!L->contains(ChildBB))
1209 for (
auto *ChildBB : ChildrenToUpdate)
1210 NonLoopBlocksIDom[ChildBB] = NewIDom;
1247 ExitValues[&
P] =
P.getIncomingValueForBlock(Latch);
1251 InsertTop =
SplitEdge(Latch, Exit, &DT, LI);
1254 InsertTop->
setName(Exit->getName() +
".peel.begin");
1255 InsertBot->
setName(Exit->getName() +
".peel.next");
1256 NewPreHeader =
nullptr;
1262 NewPreHeader =
SplitEdge(PreHeader, Header, &DT, LI);
1270 B.CreateICmpNE(BTCValue, ConstantInt::get(BTCValue->
getType(), 0));
1271 B.CreateCondBr(
Cond, NewPreHeader, InsertTop);
1323 InsertTop =
SplitEdge(PreHeader, Header, &DT, LI);
1327 InsertTop->
setName(Header->getName() +
".peel.begin");
1328 InsertBot->
setName(Header->getName() +
".peel.next");
1333 cast<Instruction>(cast<BasicBlock>(Latch)->getTerminator());
1347 for (
unsigned Iter = 0; Iter < PeelCount; ++Iter) {
1351 NewPreHeader ? PreHeader :
nullptr, ExitEdges, NewBlocks,
1352 LoopBlocks, VMap, LVMap, &DT, LI,
1353 LoopLocalNoAliasDeclScopes, *SE);
1368 cast<ICmpInst>(L->getLoopLatch()->getTerminator()->getOperand(0));
1371 1,
B.CreateSub(Cmp->getOperand(1),
1372 ConstantInt::get(Cmp->getOperand(1)->getType(), 1)));
1375 for (
auto BBIDom : NonLoopBlocksIDom)
1377 cast<BasicBlock>(LVMap[BBIDom.second]));
1381#ifdef EXPENSIVE_CHECKS
1382 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
1385 for (
auto &[Term,
Info] : Weights) {
1386 auto *TermCopy = cast<Instruction>(VMap[Term]);
1392 auto *LatchTermCopy = cast<Instruction>(VMap[LatchTerm]);
1393 LatchTermCopy->setMetadata(LLVMContext::MD_loop,
nullptr);
1395 InsertTop = InsertBot;
1397 InsertBot->
setName(Header->getName() +
".peel.next");
1399 F->splice(InsertTop->
getIterator(),
F, NewBlocks[0]->getIterator(),
1406 for (
const auto &[
P, E] : ExitValues) {
1408 if (ExitInst && L->contains(ExitInst))
1409 P->replaceAllUsesWith(&*VMap[ExitInst]);
1411 P->replaceAllUsesWith(E);
1412 P->eraseFromParent();
1420 Value *NewVal =
PHI->getIncomingValueForBlock(Latch);
1421 Instruction *LatchInst = dyn_cast<Instruction>(NewVal);
1422 if (LatchInst && L->contains(LatchInst))
1423 NewVal = LVMap[LatchInst];
1425 PHI->setIncomingValueForBlock(NewPreHeader, NewVal);
1429 for (
const auto &[Term,
Info] : Weights) {
1434 unsigned AlreadyPeeled = 0;
1436 AlreadyPeeled = *Peeled;
1439 if (
Loop *ParentLoop = L->getParentLoop())
1446#ifdef EXPENSIVE_CHECKS
1448 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
1452 simplifyLoop(L, &DT, LI, SE, AC,
nullptr, PreserveLCSSA);
1455 NumPeeledEnd += PeelLast;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
static void updateBranchWeights(Instruction *Term, WeightInfo &Info)
Update the branch weights of an exiting block of a peeled-off loop iteration.
static cl::opt< bool > DisableAdvancedPeeling("disable-advanced-peeling", cl::init(false), cl::Hidden, cl::desc("Disable advance peeling. Issues for convergent targets (D134803)."))
static cl::opt< unsigned > UnrollPeelMaxCount("unroll-peel-max-count", cl::init(7), cl::Hidden, cl::desc("Max average trip count which will cause loop peeling."))
static bool shouldPeelLastIteration(Loop &L, CmpPredicate Pred, const SCEVAddRecExpr *LeftAR, const SCEV *RightSCEV, ScalarEvolution &SE, const TargetTransformInfo &TTI)
Returns true if the last iteration can be peeled off and the condition (Pred LeftAR,...
static cl::opt< bool > UnrollAllowPeeling("unroll-allow-peeling", cl::init(true), cl::Hidden, cl::desc("Allows loops to be peeled when the dynamic " "trip count is known to be low."))
static cl::opt< unsigned > UnrollForcePeelCount("unroll-force-peel-count", cl::init(0), cl::Hidden, cl::desc("Force a peel count regardless of profiling information."))
static bool violatesLegacyMultiExitLoopCheck(Loop *L)
This "heuristic" exactly matches implicit behavior which used to exist inside getLoopEstimatedTripCou...
static const char * PeeledCountMetaData
static std::pair< unsigned, unsigned > countToEliminateCompares(Loop &L, unsigned MaxPeelCount, ScalarEvolution &SE, const TargetTransformInfo &TTI)
static cl::opt< bool > UnrollAllowLoopNestsPeeling("unroll-allow-loop-nests-peeling", cl::init(false), cl::Hidden, cl::desc("Allows loop nests to be peeled."))
static cl::opt< unsigned > UnrollPeelCount("unroll-peel-count", cl::Hidden, cl::desc("Set the unroll peeling count, for testing purposes"))
static unsigned peelToTurnInvariantLoadsDerefencebale(Loop &L, DominatorTree &DT, AssumptionCache *AC)
static cl::opt< bool > EnablePeelingForIV("enable-peeling-for-iv", cl::init(false), cl::Hidden, cl::desc("Enable peeling to convert Phi nodes into IVs"))
static void cloneLoopBlocks(Loop *L, unsigned IterNumber, bool PeelLast, BasicBlock *InsertTop, BasicBlock *InsertBot, BasicBlock *OrigPreHeader, SmallVectorImpl< std::pair< BasicBlock *, BasicBlock * > > &ExitEdges, SmallVectorImpl< BasicBlock * > &NewBlocks, LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap, ValueToValueMapTy &LVMap, DominatorTree *DT, LoopInfo *LI, ArrayRef< MDNode * > LoopLocalNoAliasDeclScopes, ScalarEvolution &SE)
Clones the body of the loop L, putting it between InsertTop and InsertBot.
static void initBranchWeights(DenseMap< Instruction *, WeightInfo > &WeightInfos, Loop *L)
Initialize the weights for all exiting blocks.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
std::pair< BasicBlock *, BasicBlock * > Edge
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Conditional or Unconditional Branch instruction.
unsigned getNumSuccessors() const
BasicBlock * getSuccessor(unsigned i) const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
A parsed version of the target data layout string in and methods for querying it.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
DomTreeNodeBase * getIDom() const
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
bool isEquality() const
Return true if this predicate is either EQ or NE.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Store the result of a depth first search within basic blocks contained by a single loop.
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
std::vector< BasicBlock * >::const_reverse_iterator RPOIterator
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
RPOIterator endRPO() const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
This class represents min/max intrinsics.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const Loop * getLoop() const
This class represents a constant integer value.
This class uses information about analyze scalars to rewrite expressions in canonical form.
bool isHighCostExpansion(ArrayRef< const SCEV * > Exprs, Loop *L, unsigned Budget, const TargetTransformInfo *TTI, const Instruction *At)
Return true for expressions that can't be evaluated at runtime within given Budget.
LLVM_ABI Value * expandCodeFor(const SCEV *SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
bool hasNoSelfWrap() const
This class represents an analyzed expression in the program.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
static LLVM_ABI LoopGuards collect(const Loop *L, ScalarEvolution &SE)
Collect rewrite map for loop guards for loop L, together with flags indicating if NUW and NSW can be ...
The main scalar evolution driver.
const SCEV * getConstantMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
LLVM_ABI std::optional< MonotonicPredicateType > getMonotonicPredicateType(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred)
If, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or decreasing,...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI std::optional< bool > evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Check whether the condition described by Pred, LHS, and RHS is true or false.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
This class represents the LLVM 'select' instruction.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
bool isIntegerTy() const
True if this is an instance of IntegerType.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
int getNumOccurrences() const
self_iterator getIterator()
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
SCEVAffineAddRec_match< Op0_t, Op1_t, class_match< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
bool match(const SCEV *S, const Pattern &P)
class_match< const SCEV > m_SCEV()
initializer< Ty > init(const Ty &Val)
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
LLVM_ABI std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Returns a loop's estimated trip count based on branch weight metadata.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB)
Check if we can prove that all paths starting from this block converge to a block that either has a @...
LLVM_ABI BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, bool MapAtoms=true)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
auto successors(const MachineBasicBlock *BB)
bool canPeel(const Loop *L)
bool canPeelLastIteration(const Loop &L, ScalarEvolution &SE)
Returns true if the last iteration of L can be peeled off.
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
TargetTransformInfo::PeelingPreferences gatherPeelingPreferences(Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, std::optional< bool > UserAllowPeeling, std::optional< bool > UserAllowProfileBasedPeeling, bool UnrollingSpecficValues=false)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void computePeelCount(Loop *L, unsigned LoopSize, TargetTransformInfo::PeelingPreferences &PP, unsigned TripCount, DominatorTree &DT, ScalarEvolution &SE, const TargetTransformInfo &TTI, AssumptionCache *AC=nullptr, unsigned Threshold=UINT_MAX)
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
LLVM_ABI std::optional< int > getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name)
Find named metadata for a loop with an integer value.
LLVM_ABI void cloneAndAdaptNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, ArrayRef< BasicBlock * > NewBlocks, LLVMContext &Context, StringRef Ext)
Clone the specified noalias decl scopes.
LLVM_ABI void remapInstructionsInBlocks(ArrayRef< BasicBlock * > Blocks, ValueToValueMapTy &VMap)
Remaps instructions in Blocks using the mapping in VMap.
LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
LLVM_ABI void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
LLVM_ABI BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
LLVM_ABI bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
bool peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI, ScalarEvolution *SE, DominatorTree &DT, AssumptionCache *AC, bool PreserveLCSSA, ValueToValueMapTy &VMap)
VMap is the value-map that maps instructions from the original loop to instructions in the last peele...
LLVM_ABI Loop * cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
Recursively clone the specified loop and all of its children, mapping the blocks with the specified m...
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
SmallVector< uint32_t > Weights
const SmallVector< uint32_t > SubWeights