69#define DEBUG_TYPE "simple-loop-unswitch"
74STATISTIC(NumBranches,
"Number of branches unswitched");
75STATISTIC(NumSwitches,
"Number of switches unswitched");
76STATISTIC(NumSelects,
"Number of selects turned into branches for unswitching");
77STATISTIC(NumGuards,
"Number of guards turned into branches for unswitching");
78STATISTIC(NumTrivial,
"Number of unswitches that are trivial");
80 NumCostMultiplierSkipped,
81 "Number of unswitch candidates that had their cost multiplier skipped");
83 "Number of invariant conditions injected and unswitched");
88 cl::desc(
"Forcibly enables non-trivial loop unswitching rather than "
89 "following the configuration passed into the pass."));
93 cl::desc(
"The cost threshold for unswitching a loop."));
97 cl::desc(
"Enable unswitch cost multiplier that prohibits exponential "
98 "explosion in nontrivial unswitch."));
101 cl::desc(
"Toplevel siblings divisor for cost multiplier."));
104 cl::desc(
"Outer loop size divisor for cost multiplier."));
107 cl::desc(
"Number of unswitch candidates that are ignored when calculating "
108 "cost multiplier."));
111 cl::desc(
"If enabled, simple loop unswitching will also consider "
112 "llvm.experimental.guard intrinsics as unswitch candidates."));
114 "simple-loop-unswitch-drop-non-trivial-implicit-null-checks",
116 cl::desc(
"If enabled, drop make.implicit metadata in unswitched implicit "
117 "null checks to save time analyzing if we can keep it."));
120 cl::desc(
"Max number of memory uses to explore during "
121 "partial unswitching analysis"),
125 cl::desc(
"If enabled, the freeze instruction will be added to condition "
126 "of loop unswitch to prevent miscompilation."));
129 "simple-loop-unswitch-inject-invariant-conditions",
cl::Hidden,
130 cl::desc(
"Whether we should inject new invariants and unswitch them to "
131 "eliminate some existing (non-invariant) conditions."),
135 "simple-loop-unswitch-inject-invariant-condition-hotness-threshold",
137 cl::desc(
"Only try to inject loop invariant conditions and "
138 "unswitch on them to eliminate branches that are "
139 "not-taken 1/<this option> times or less."),
155 : Term(Term), Invariant(Invariant), InLoopSucc(InLoopSucc) {}
158struct InjectedInvariant {
159 ICmpInst::Predicate Pred;
164 InjectedInvariant(ICmpInst::Predicate Pred,
Value *LHS,
Value *RHS,
165 BasicBlock *InLoopSucc)
166 : Pred(Pred), LHS(LHS), RHS(RHS), InLoopSucc(InLoopSucc) {}
169struct NonTrivialUnswitchCandidate {
171 TinyPtrVector<Value *> Invariants;
172 std::optional<InstructionCost> Cost;
173 std::optional<InjectedInvariant> PendingInjection;
174 NonTrivialUnswitchCandidate(
176 std::optional<InstructionCost> Cost = std::nullopt,
177 std::optional<InjectedInvariant> PendingInjection = std::nullopt)
178 : TI(TI), Invariants(Invariants), Cost(Cost),
179 PendingInjection(PendingInjection) {};
181 bool hasPendingInjection()
const {
return PendingInjection.has_value(); }
205 assert(!L.isLoopInvariant(&Root) &&
206 "Only need to walk the graph if root itself is not invariant.");
219 for (
Value *OpV :
I.operand_values()) {
225 if (L.isLoopInvariant(OpV)) {
236 if (Visited.
insert(OpI).second)
240 }
while (!Worklist.
empty());
255 if (UserI && L.contains(UserI))
273 if (!L.isLoopInvariant(PN->getIncomingValueForBlock(&ExitingBB)))
309 if (HasBranchWeights &&
310 static_cast<double>(BranchWeights[
Direction ? 0 : 1]) /
311 static_cast<double>(
sum_of(BranchWeights)) >
313 HasBranchWeights =
false;
319 for (
Value *Inv : Invariants) {
329 Direction ? &NormalSucc : &UnswitchedSucc,
330 HasBranchWeights ? ComputeProfFrom.getMetadata(LLVMContext::MD_prof)
332 if (!HasBranchWeights)
342 for (
auto *Val :
reverse(ToDuplicate)) {
360 auto *DefiningAccess = MemUse->getDefiningAccess();
362 while (L.contains(DefiningAccess->getBlock())) {
367 MemPhi->getIncomingValueForBlock(L.getLoopPreheader());
385 ? OriginalBranch.getMetadata(LLVMContext::MD_prof)
389 Direction ? &NormalSucc : &UnswitchedSucc, ProfData);
408 for (
auto i :
seq<int>(0, PN.getNumOperands())) {
409 assert(PN.getIncomingBlock(i) == &OldExitingBB &&
410 "Found incoming block different from unique predecessor!");
411 PN.setIncomingBlock(i, &OldPH);
428 assert(&ExitBB != &UnswitchedBB &&
429 "Must have different loop exit and unswitched blocks!");
433 PN.getName() +
".split");
434 NewPN->insertBefore(InsertPt);
445 for (
int i = PN.getNumIncomingValues() - 1; i >= 0; --i) {
446 if (PN.getIncomingBlock(i) != &OldExitingBB)
449 Value *Incoming = PN.getIncomingValue(i);
452 PN.removeIncomingValue(i);
454 NewPN->addIncoming(Incoming, &OldPH);
459 PN.replaceAllUsesWith(NewPN);
460 NewPN->addIncoming(&PN, &ExitBB);
473 Loop *OldParentL = L.getParentLoop();
478 L.getExitBlocks(Exits);
479 Loop *NewParentL =
nullptr;
480 for (
auto *ExitBB : Exits)
482 if (!NewParentL || NewParentL->
contains(ExitL))
485 if (NewParentL == OldParentL)
491 "Can only hoist this loop up the nest!");
496 "Parent loop of this loop should contain this loop's preheader!");
511 for (
Loop *OldContainingL = OldParentL; OldContainingL != NewParentL;
515 return BB == &Preheader || L.contains(BB);
518 OldContainingL->getBlocksSet().erase(&Preheader);
520 OldContainingL->getBlocksSet().erase(BB);
543 Loop *Current = TopMost;
572 LLVM_DEBUG(
dbgs() <<
" Trying to unswitch branch: " << BI <<
"\n");
579 bool FullUnswitch =
false;
582 if (L.isLoopInvariant(
Cond)) {
588 if (Invariants.
empty()) {
595 bool ExitDirection =
true;
596 int LoopExitSuccIdx = 0;
598 if (L.contains(LoopExitBB)) {
599 ExitDirection =
false;
602 if (L.contains(LoopExitBB)) {
607 auto *ContinueBB = BI.
getSuccessor(1 - LoopExitSuccIdx);
608 auto *ParentBB = BI.getParent();
610 LLVM_DEBUG(
dbgs() <<
" Loop exit PHI's aren't loop-invariant!\n");
623 "non-full unswitch!\n");
629 dbgs() <<
" unswitching trivial invariant conditions for: " << BI
631 for (
Value *Invariant : Invariants) {
632 dbgs() <<
" " << *Invariant <<
" == true";
633 if (Invariant != Invariants.back())
665 if (FullUnswitch && LoopExitBB->getUniquePredecessor()) {
666 assert(LoopExitBB->getUniquePredecessor() == BI.getParent() &&
667 "A branch's parent isn't a predecessor!");
668 UnswitchedBB = LoopExitBB;
671 SplitBlock(LoopExitBB, LoopExitBB->begin(), &DT, &LI, MSSAU,
"");
685 BI.moveBefore(*OldPH, OldPH->
end());
690 BI.clone()->insertInto(ParentBB, ParentBB->end());
704 "Must have an `or` of `i1`s or `select i1 X, true, Y`s for the "
708 "Must have an `and` of `i1`s or `select i1 X, Y, false`s for the"
711 *OldPH, Invariants, ExitDirection, *UnswitchedBB, *NewPH,
734 Term->eraseFromParent();
744 if (UnswitchedBB == LoopExitBB)
748 *ParentBB, *OldPH, FullUnswitch);
759 for (
Value *Invariant : Invariants)
806 Value *LoopCond =
SI.getCondition();
809 if (!L.isLoopInvariant(LoopCond))
812 auto *ParentBB =
SI.getParent();
819 auto IsTriviallyUnswitchableExitBlock = [&](
BasicBlock &BBToCheck) {
821 if (L.contains(&BBToCheck))
830 auto *TI = BBToCheck.getTerminator();
832 return !isUnreachable || &*BBToCheck.getFirstNonPHIOrDbg() != TI;
836 for (
auto Case :
SI.cases())
837 if (IsTriviallyUnswitchableExitBlock(*Case.getCaseSuccessor()))
838 ExitCaseIndices.
push_back(Case.getCaseIndex());
842 if (IsTriviallyUnswitchableExitBlock(*
SI.getDefaultDest())) {
843 DefaultExitBB =
SI.getDefaultDest();
844 }
else if (ExitCaseIndices.
empty())
859 if (!ExitL || ExitL->
contains(OuterL))
862 for (
unsigned Index : ExitCaseIndices) {
863 auto CaseI =
SI.case_begin() + Index;
866 if (!ExitL || ExitL->
contains(OuterL))
880 SI.setDefaultDest(
nullptr);
888 ExitCases.reserve(ExitCaseIndices.
size());
892 for (
unsigned Index :
reverse(ExitCaseIndices)) {
893 auto CaseI =
SI.case_begin() + Index;
896 ExitCases.emplace_back(CaseI->getCaseValue(), CaseI->getCaseSuccessor(), W);
904 if (
SI.getNumCases() > 0 &&
906 return Case.getCaseSuccessor() == SI.case_begin()->getCaseSuccessor();
908 CommonSuccBB =
SI.case_begin()->getCaseSuccessor();
909 if (!DefaultExitBB) {
913 if (
SI.getNumCases() == 0)
914 CommonSuccBB =
SI.getDefaultDest();
915 else if (
SI.getDefaultDest() != CommonSuccBB)
916 CommonSuccBB =
nullptr;
945 UnswitchedExitBBs.
insert(DefaultExitBB);
953 DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB;
958 for (
auto &ExitCase :
reverse(ExitCases)) {
966 if (UnswitchedExitBBs.
insert(ExitBB).second)
973 BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
982 std::get<1>(ExitCase) = SplitExitBB;
987 for (
auto &ExitCase :
reverse(ExitCases)) {
989 BasicBlock *UnswitchedBB = std::get<1>(ExitCase);
991 NewSIW.
addCase(CaseVal, UnswitchedBB, std::get<2>(ExitCase));
1002 for (
const auto &Case :
SI.cases())
1005 }
else if (DefaultCaseWeight) {
1008 for (
const auto &Case :
SI.cases()) {
1011 "case weight must be defined as default case weight is defined");
1026 bool SkippedFirst = DefaultExitBB ==
nullptr;
1027 for (
auto Case :
SI.cases()) {
1029 "Non-common successor!");
1031 if (!SkippedFirst) {
1032 SkippedFirst =
true;
1042 }
else if (DefaultExitBB) {
1044 "If we had no cases we'd have a common successor!");
1049 auto LastCaseI = std::prev(
SI.case_end());
1051 SI.setDefaultDest(LastCaseI->getCaseSuccessor());
1062 for (
auto *UnswitchedExitBB : UnswitchedExitBBs) {
1066 for (
auto SplitUnswitchedPair : SplitExitBBMap) {
1067 DTUpdates.
push_back({DT.
Delete, ParentBB, SplitUnswitchedPair.first});
1079 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
1124 Visited.
insert(CurrentBB);
1131 if (!
isa<MemoryPhi>(*Defs->begin()) || (++Defs->begin() != Defs->end()))
1161 CurrentBB = BI->getSuccessor();
1195 }
while (L.contains(CurrentBB) && Visited.
insert(CurrentBB).second);
1233 NewBlocks.
reserve(L.getNumBlocks() + ExitBlocks.
size());
1244 VMap[OldBB] = NewBB;
1252 auto It = DominatingSucc.
find(BB);
1253 return It != DominatingSucc.
end() && It->second != UnswitchedSuccBB;
1257 auto *ClonedPH = CloneBlock(LoopPH);
1260 for (
auto *LoopBB : L.blocks())
1261 if (!SkipBlock(LoopBB))
1267 for (
auto *ExitBB : ExitBlocks) {
1268 if (SkipBlock(ExitBB))
1276 auto *MergeBB =
SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU);
1281 MergeBB->takeName(ExitBB);
1282 ExitBB->setName(
Twine(MergeBB->getName()) +
".split");
1285 auto *ClonedExitBB = CloneBlock(ExitBB);
1286 assert(ClonedExitBB->getTerminator()->getNumSuccessors() == 1 &&
1287 "Exit block should have been split to have one successor!");
1288 assert(ClonedExitBB->getTerminator()->getSuccessor(0) == MergeBB &&
1289 "Cloned exit block has the wrong successor!");
1295 std::prev(ClonedExitBB->end())))) {
1303 "Bad instruction in exit block!");
1305 assert(VMap.
lookup(&
I) == &ClonedI &&
"Mismatch in the value map!");
1316 MergePN->insertBefore(InsertPt);
1317 MergePN->setDebugLoc(InsertPt->getDebugLoc());
1318 I.replaceAllUsesWith(MergePN);
1319 MergePN->addIncoming(&
I, ExitBB);
1320 MergePN->addIncoming(&ClonedI, ClonedExitBB);
1329 Module *M = ClonedPH->getParent()->getParent();
1330 for (
auto *ClonedBB : NewBlocks)
1342 for (
auto *LoopBB : L.blocks())
1343 if (SkipBlock(LoopBB))
1346 for (
PHINode &PN : ClonedSuccBB->phis())
1347 PN.removeIncomingValue(LoopBB,
false);
1353 if (SuccBB == UnswitchedSuccBB)
1360 ClonedSuccBB->removePredecessor(ClonedParentBB,
1367 Instruction *ClonedTerminator = ClonedParentBB->getTerminator();
1370 Value *ClonedConditionToErase =
nullptr;
1372 ClonedConditionToErase = BI->getCondition();
1374 ClonedConditionToErase =
SI->getCondition();
1380 if (ClonedConditionToErase)
1387 for (
PHINode &PN : ClonedSuccBB->phis()) {
1391 for (
int i = PN.getNumOperands() - 1; i >= 0; --i) {
1392 if (PN.getIncomingBlock(i) != ClonedParentBB)
1398 PN.removeIncomingValue(i,
false);
1404 for (
auto *ClonedBB : NewBlocks) {
1406 if (SuccSet.
insert(SuccBB).second)
1422 auto AddClonedBlocksToLoop = [&](
Loop &OrigL,
Loop &ClonedL) {
1423 assert(ClonedL.getBlocks().empty() &&
"Must start with an empty loop!");
1425 for (
auto *BB : OrigL.
blocks()) {
1427 ClonedL.addBlockEntry(ClonedBB);
1440 AddClonedBlocksToLoop(OrigRootL, *ClonedRootL);
1452 LoopsToClone.
push_back({ClonedRootL, ChildL});
1454 Loop *ClonedParentL, *L;
1455 std::tie(ClonedParentL, L) = LoopsToClone.
pop_back_val();
1458 AddClonedBlocksToLoop(*L, *ClonedL);
1460 LoopsToClone.
push_back({ClonedL, ChildL});
1461 }
while (!LoopsToClone.
empty());
1482 Loop *ClonedL =
nullptr;
1494 Loop *ParentL =
nullptr;
1498 for (
auto *ExitBB : ExitBlocks)
1501 ExitLoopMap[ClonedExitBB] = ExitL;
1502 ClonedExitsInLoops.
push_back(ClonedExitBB);
1503 if (!ParentL || (ParentL != ExitL && ParentL->
contains(ExitL)))
1508 "The computed parent loop should always contain (or be) the parent of "
1509 "the original loop.");
1516 for (
auto *BB : OrigL.
blocks())
1518 ClonedLoopBlocks.
insert(ClonedBB);
1529 if (Pred == ClonedPH)
1534 assert(ClonedLoopBlocks.
count(Pred) &&
"Found a predecessor of the loop "
1535 "header other than the preheader "
1536 "that is not part of the loop!");
1541 if (BlocksInClonedLoop.
insert(Pred).second && Pred != ClonedHeader)
1548 if (!BlocksInClonedLoop.
empty()) {
1549 BlocksInClonedLoop.
insert(ClonedHeader);
1551 while (!Worklist.
empty()) {
1554 "Didn't put block into the loop set!");
1562 if (ClonedLoopBlocks.
count(Pred) &&
1563 BlocksInClonedLoop.
insert(Pred).second)
1582 for (
auto *BB : OrigL.
blocks()) {
1584 if (!ClonedBB || !BlocksInClonedLoop.
count(ClonedBB))
1596 for (
Loop *PL = ClonedL; PL; PL = PL->getParentLoop())
1597 PL->addBlockEntry(ClonedBB);
1604 for (
Loop *ChildL : OrigL) {
1605 auto *ClonedChildHeader =
1607 if (!ClonedChildHeader || !BlocksInClonedLoop.
count(ClonedChildHeader))
1613 for (
auto *ChildLoopBB : ChildL->blocks())
1616 "Child cloned loop has a header within the cloned outer "
1617 "loop but not all of its blocks!");
1632 if (BlocksInClonedLoop.
empty())
1633 UnloopedBlockSet.
insert(ClonedPH);
1634 for (
auto *ClonedBB : ClonedLoopBlocks)
1635 if (!BlocksInClonedLoop.
count(ClonedBB))
1636 UnloopedBlockSet.
insert(ClonedBB);
1642 auto OrderedClonedExitsInLoops = ClonedExitsInLoops;
1644 return ExitLoopMap.
lookup(
LHS)->getLoopDepth() <
1645 ExitLoopMap.
lookup(
RHS)->getLoopDepth();
1650 while (!UnloopedBlockSet.
empty() && !OrderedClonedExitsInLoops.empty()) {
1651 assert(Worklist.
empty() &&
"Didn't clear worklist!");
1653 BasicBlock *ExitBB = OrderedClonedExitsInLoops.pop_back_val();
1668 if (!UnloopedBlockSet.
erase(PredBB)) {
1670 (BlocksInClonedLoop.
count(PredBB) || ExitLoopMap.
count(PredBB)) &&
1671 "Predecessor not mapped to a loop!");
1678 bool Inserted = ExitLoopMap.
insert({PredBB, ExitL}).second;
1680 assert(Inserted &&
"Should only visit an unlooped block once!");
1685 }
while (!Worklist.
empty());
1695 ArrayRef(ClonedPH), ClonedLoopBlocks, ClonedExitsInLoops))
1697 OuterL->addBasicBlockToLoop(BB, LI);
1700 for (
auto &BBAndL : ExitLoopMap) {
1701 auto *BB = BBAndL.first;
1702 auto *OuterL = BBAndL.second;
1704 "Failed to put all blocks into outer loops!");
1711 for (
Loop *ChildL : OrigL) {
1712 auto *ClonedChildHeader =
1714 if (!ClonedChildHeader || BlocksInClonedLoop.
count(ClonedChildHeader))
1718 for (
auto *ChildLoopBB : ChildL->blocks())
1720 "Cloned a child loop header but not all of that loops blocks!");
1724 *ChildL, ExitLoopMap.
lookup(ClonedChildHeader), VMap, LI));
1730 ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
1735 for (
const auto &VMap : VMaps)
1739 SuccBB->removePredecessor(ClonedBB);
1752 BB->dropAllReferences();
1755 BB->eraseFromParent();
1772 DeathCandidates.
append(L.blocks().begin(), L.blocks().end());
1773 while (!DeathCandidates.
empty()) {
1777 SuccBB->removePredecessor(BB);
1794 for (
Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop()) {
1795 for (
auto *BB : DeadBlockSet)
1796 ParentL->getBlocksSet().erase(BB);
1798 [&](
BasicBlock *BB) { return DeadBlockSet.count(BB); });
1804 if (!DeadBlockSet.count(ChildL->getHeader()))
1807 assert(llvm::all_of(ChildL->blocks(),
1808 [&](BasicBlock *ChildBB) {
1809 return DeadBlockSet.count(ChildBB);
1811 "If the child loop header is dead all blocks in the child loop must "
1812 "be dead as well!");
1823 for (
auto *BB : DeadBlockSet) {
1825 assert(!DT.getNode(BB) &&
"Should already have cleared domtree!");
1826 LI.changeLoopFor(BB,
nullptr);
1832 BB->dropAllReferences();
1837 for (
auto *BB : DeadBlockSet)
1838 BB->eraseFromParent();
1856 auto *PH = L.getLoopPreheader();
1857 auto *Header = L.getHeader();
1871 assert(L.contains(Pred) &&
"Found a predecessor of the loop header other "
1872 "than the preheader that is not part of the "
1878 if (LoopBlockSet.
insert(Pred).second && Pred != Header)
1883 if (LoopBlockSet.
empty())
1884 return LoopBlockSet;
1887 while (!Worklist.
empty()) {
1889 assert(LoopBlockSet.
count(BB) &&
"Didn't put block into the loop set!");
1901 assert(L.contains(InnerL) &&
1902 "Should not reach a loop *outside* this loop!");
1905 auto *InnerPH = InnerL->getLoopPreheader();
1906 assert(L.contains(InnerPH) &&
"Cannot contain an inner loop block "
1907 "but not contain the inner loop "
1909 if (!LoopBlockSet.
insert(InnerPH).second)
1919 for (
auto *InnerBB : InnerL->blocks()) {
1920 if (InnerBB == BB) {
1922 "Block should already be in the set!");
1926 LoopBlockSet.
insert(InnerBB);
1938 if (L.contains(Pred) && LoopBlockSet.
insert(Pred).second)
1942 assert(LoopBlockSet.
count(Header) &&
"Cannot fail to add the header!");
1946 return LoopBlockSet;
1967 auto *PH = L.getLoopPreheader();
1971 Loop *ParentL =
nullptr;
1975 for (
auto *ExitBB : ExitBlocks)
1979 if (!ParentL || (ParentL != ExitL && ParentL->
contains(ExitL)))
1991 if (!LoopBlockSet.empty() && L.getParentLoop() != ParentL) {
1993 for (
Loop *IL = L.getParentLoop(); IL != ParentL;
1995 IL->getBlocksSet().erase(PH);
1996 for (
auto *BB : L.blocks())
1997 IL->getBlocksSet().erase(BB);
1999 return BB == PH || L.contains(BB);
2004 L.getParentLoop()->removeChildLoop(&L);
2012 auto &Blocks = L.getBlocksVector();
2014 LoopBlockSet.empty()
2016 : std::stable_partition(
2017 Blocks.begin(), Blocks.end(),
2018 [&](
BasicBlock *BB) { return LoopBlockSet.count(BB); });
2022 if (LoopBlockSet.empty())
2023 UnloopedBlocks.
insert(PH);
2026 for (
auto *BB :
make_range(BlocksSplitI, Blocks.end()))
2027 L.getBlocksSet().erase(BB);
2028 Blocks.erase(BlocksSplitI, Blocks.end());
2038 Loop *PrevExitL = L.getParentLoop();
2040 auto RemoveUnloopedBlocksFromLoop =
2042 for (
auto *BB : UnloopedBlocks)
2043 L.getBlocksSet().erase(BB);
2045 return UnloopedBlocks.count(BB);
2050 while (!UnloopedBlocks.
empty() && !ExitsInLoops.
empty()) {
2051 assert(Worklist.
empty() &&
"Didn't clear worklist!");
2052 assert(NewExitLoopBlocks.empty() &&
"Didn't clear loop set!");
2057 assert(ExitL.
contains(&L) &&
"Exit loop must contain the inner loop!");
2063 for (; PrevExitL != &ExitL; PrevExitL = PrevExitL->
getParentLoop())
2064 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
2078 if (!UnloopedBlocks.
erase(PredBB)) {
2079 assert((NewExitLoopBlocks.count(PredBB) ||
2081 "Predecessor not in a nested loop (or already visited)!");
2088 bool Inserted = NewExitLoopBlocks.insert(PredBB).second;
2090 assert(Inserted &&
"Should only visit an unlooped block once!");
2095 }
while (!Worklist.
empty());
2100 for (
auto *BB : NewExitLoopBlocks)
2102 if (BBL == &L || !L.contains(BBL))
2107 NewExitLoopBlocks.clear();
2113 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
2114 for (
auto *BB : UnloopedBlocks)
2116 if (BBL == &L || !L.contains(BBL))
2122 auto &SubLoops = L.getSubLoopsVector();
2123 auto SubLoopsSplitI =
2124 LoopBlockSet.empty()
2126 : std::stable_partition(
2127 SubLoops.begin(), SubLoops.end(), [&](
Loop *SubL) {
2128 return LoopBlockSet.count(SubL->getHeader());
2130 for (
auto *HoistedL :
make_range(SubLoopsSplitI, SubLoops.end())) {
2132 HoistedL->setParentLoop(
nullptr);
2142 if (
auto *NewParentL = LI.
getLoopFor(HoistedL->getLoopPreheader()))
2143 NewParentL->addChildLoop(HoistedL);
2147 SubLoops.erase(SubLoopsSplitI, SubLoops.end());
2150 if (Blocks.empty()) {
2151 assert(SubLoops.empty() &&
2152 "Failed to remove all subloops from the original loop!");
2153 if (
Loop *ParentL = L.getParentLoop())
2171template <
typename CallableT>
2183 if (!Callable(
N->getBlock()))
2189 "Cannot visit a node twice when walking a tree!");
2192 }
while (!DomWorklist.
empty());
2196 bool CurrentLoopValid,
bool PartiallyInvariant,
2199 if (!NewLoops.
empty())
2200 U.addSiblingLoops(NewLoops);
2204 if (CurrentLoopValid) {
2205 if (PartiallyInvariant) {
2208 L.addStringLoopAttribute(
"llvm.loop.unswitch.partial.disable",
2209 {
"llvm.loop.unswitch.partial"});
2210 }
else if (InjectedCondition) {
2212 L.addStringLoopAttribute(
"llvm.loop.unswitch.injection.disable",
2213 {
"llvm.loop.unswitch.injection"});
2215 U.revisitCurrentLoop();
2217 U.markLoopAsDeleted(L, LoopName);
2224 LPMUpdater &LoopUpdater,
bool InsertFreeze,
bool InjectedCondition) {
2231 std::string LoopName(L.getName());
2236 assert((
SI || BI) &&
"Can only unswitch switches and conditional branch!");
2240 !PartiallyInvariant);
2243 "Cannot have other invariants with full unswitching!");
2246 "Partial unswitching requires an instruction as the condition!");
2259 if (!FullUnswitch) {
2263 PartiallyInvariant) &&
2264 "Only `or`, `and`, an `select`, partially invariant instructions "
2265 "can combine invariants being unswitched.");
2281 for (
auto Case :
SI->cases())
2282 if (Case.getCaseSuccessor() != RetainedSuccBB)
2283 UnswitchedSuccBBs.
insert(Case.getCaseSuccessor());
2285 assert(!UnswitchedSuccBBs.
count(RetainedSuccBB) &&
2286 "Should not unswitch the same successor we are retaining!");
2295 Loop *ParentL = L.getParentLoop();
2304 Loop *OuterExitL = &L;
2306 L.getUniqueExitBlocks(ExitBlocks);
2307 for (
auto *ExitBB : ExitBlocks) {
2311 if (!NewOuterExitL) {
2313 OuterExitL =
nullptr;
2316 if (NewOuterExitL != OuterExitL && NewOuterExitL->
contains(OuterExitL))
2317 OuterExitL = NewOuterExitL;
2339 if (SuccBB->getUniquePredecessor() ||
2341 return PredBB == ParentBB || DT.
dominates(SuccBB, PredBB);
2344 DominatingSucc[BB] = SuccBB;
2363 for (
auto *SuccBB : UnswitchedSuccBBs) {
2366 L, LoopPH, SplitBB, ExitBlocks, ParentBB, SuccBB, RetainedSuccBB,
2367 DominatingSucc, *VMaps.
back(), DTUpdates, AC, DT, LI, MSSAU, SE);
2372 if (TI.
getMetadata(LLVMContext::MD_make_implicit)) {
2376 TI.
setMetadata(LLVMContext::MD_make_implicit,
nullptr);
2383 TI.
setMetadata(LLVMContext::MD_make_implicit,
nullptr);
2394 NewTI->
insertInto(ParentBB, ParentBB->end());
2417 assert(
SI &&
"Must either be a branch or switch!");
2420 assert(
SI->getDefaultDest() == RetainedSuccBB &&
2421 "Not retaining default successor!");
2422 SI->setDefaultDest(LoopPH);
2423 for (
const auto &Case :
SI->cases())
2424 if (Case.getCaseSuccessor() == RetainedSuccBB)
2425 Case.setSuccessor(LoopPH);
2427 Case.setSuccessor(ClonedPHs.
find(Case.getCaseSuccessor())->second);
2431 SI->getCondition()->getName() +
".fr",
2432 SI->getIterator()));
2453 for (
auto &VMap : VMaps)
2469 "Only one possible unswitched block for a branch!");
2483 "Not retaining default successor!");
2484 for (
const auto &Case : NewSI->
cases())
2485 Case.getCaseSuccessor()->removePredecessor(
2504 assert(BI &&
"Only branches have partial unswitching.");
2506 "Only one possible unswitched block for a branch!");
2510 if (PartiallyInvariant)
2512 *SplitBB, Invariants,
Direction, *ClonedPH, *LoopPH, L, MSSAU, *BI);
2515 *SplitBB, Invariants,
Direction, *ClonedPH, *LoopPH,
2525 for (
auto &VMap : VMaps)
2545 for (std::unique_ptr<ValueToValueMapTy> &VMap : VMaps)
2563#ifdef EXPENSIVE_CHECKS
2569 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
2572 if (BI && !PartiallyInvariant) {
2578 "Only one possible unswitched block for a branch!");
2590 bool ReplaceUnswitched =
2591 FullUnswitch || (Invariants.
size() == 1) || PartiallyInvariant;
2599 for (
Value *Invariant : Invariants) {
2601 "Should not be replacing constant values!");
2611 U.set(ContinueReplacement);
2612 else if (ReplaceUnswitched &&
2614 U.set(UnswitchedReplacement);
2631 auto UpdateLoop = [&](
Loop &UpdateL) {
2633 UpdateL.verifyLoop();
2634 for (
Loop *ChildL : UpdateL) {
2635 ChildL->verifyLoop();
2636 assert(ChildL->isRecursivelyLCSSAForm(DT, LI) &&
2637 "Perturbed a child loop's LCSSA form!");
2657 for (
Loop *UpdatedL :
2659 UpdateLoop(*UpdatedL);
2660 if (UpdatedL->isOutermost())
2661 OuterExitL =
nullptr;
2665 if (L.isOutermost())
2666 OuterExitL =
nullptr;
2671 if (OuterExitL != &L)
2672 for (
Loop *OuterL = ParentL; OuterL != OuterExitL;
2674 UpdateLoop(*OuterL);
2676#ifdef EXPENSIVE_CHECKS
2687 if (UpdatedL->getParentLoop() == ParentL)
2689 postUnswitch(L, LoopUpdater, LoopName, IsStillLoop, PartiallyInvariant,
2690 InjectedCondition, SibLoops);
2713 auto BBCostIt = BBCostMap.
find(
N.getBlock());
2714 if (BBCostIt == BBCostMap.
end())
2718 auto DTCostIt = DTCostMap.
find(&
N);
2719 if (DTCostIt != DTCostMap.
end())
2720 return DTCostIt->second;
2725 N.begin(),
N.end(), BBCostIt->second,
2727 return Sum + computeDomSubtreeCost(*ChildN, BBCostMap, DTCostMap);
2729 bool Inserted = DTCostMap.
insert({&
N, Cost}).second;
2731 assert(Inserted &&
"Should not insert a node while visiting children!");
2766 SI->getMetadata(LLVMContext::MD_prof), &DTU, &LI);
2768 BasicBlock *ThenBB = CondBr->getSuccessor(0),
2769 *TailBB = CondBr->getSuccessor(1);
2775 Phi->addIncoming(
SI->getTrueValue(), ThenBB);
2776 Phi->addIncoming(
SI->getFalseValue(), HeadBB);
2777 Phi->setDebugLoc(
SI->getDebugLoc());
2778 SI->replaceAllUsesWith(Phi);
2779 SI->eraseFromParent();
2833 GuardedBlock->
setName(
"guarded");
2884 return L.contains(SuccBB);
2886 NumCostMultiplierSkipped++;
2897 auto *ParentL = L.getParentLoop();
2898 int ParentLoopSizeMultiplier = 1;
2900 ParentLoopSizeMultiplier =
2904 (ParentL ? ParentL->getSubLoopsVector().
size() :
llvm::size(LI));
2908 int UnswitchedClones = 0;
2909 for (
const auto &Candidate : UnswitchCandidates) {
2912 bool SkipExitingSuccessors = DT.
dominates(CondBlock, Latch);
2918 if (!SkipExitingSuccessors)
2922 int NonExitingSuccessors =
2924 [SkipExitingSuccessors, &L](
const BasicBlock *SuccBB) {
2925 return !SkipExitingSuccessors || L.contains(SuccBB);
2927 UnswitchedClones +=
Log2_32(NonExitingSuccessors);
2935 unsigned ClonesPower =
2939 int SiblingsMultiplier =
2940 std::max((ParentL ? SiblingsCount
2951 CostMultiplier = std::min(SiblingsMultiplier * (1 << ClonesPower),
2955 <<
" (siblings " << SiblingsMultiplier <<
" * parent size "
2956 << ParentLoopSizeMultiplier <<
" * clones "
2957 << (1 << ClonesPower) <<
")"
2958 <<
" for unswitch candidate: " << TI <<
"\n");
2959 return CostMultiplier;
2967 assert(UnswitchCandidates.
empty() &&
"Should be!");
2973 if (L.isLoopInvariant(
Cond)) {
2981 if (!Invariants.
empty())
2982 UnswitchCandidates.
push_back({
I, std::move(Invariants)});
2987 bool CollectGuards =
false;
2990 L.getHeader()->getParent()->getParent(), Intrinsic::experimental_guard);
2991 if (GuardDecl && !GuardDecl->use_empty())
2992 CollectGuards =
true;
2995 for (
auto *BB : L.blocks()) {
2999 for (
auto &
I : *BB) {
3001 auto *
Cond =
SI->getCondition();
3003 if (
Cond->getType()->isIntegerTy(1) && !
SI->getType()->isIntegerTy(1))
3004 AddUnswitchCandidatesForInst(
SI,
Cond);
3005 }
else if (CollectGuards &&
isGuard(&
I)) {
3018 L.isLoopInvariant(
SI->getCondition()) && !BB->getUniqueSuccessor())
3024 if (!BI || BI->getSuccessor(0) == BI->getSuccessor(1))
3027 AddUnswitchCandidatesForInst(BI, BI->getCondition());
3031 !
any_of(UnswitchCandidates, [&L](
auto &TerminatorAndInvariants) {
3032 return TerminatorAndInvariants.TI == L.getHeader()->getTerminator();
3037 dbgs() <<
"simple-loop-unswitch: Found partially invariant condition "
3038 << *Info->InstToDuplicate[0] <<
"\n");
3039 PartialIVInfo = *Info;
3040 PartialIVCondBranch = L.getHeader()->getTerminator();
3044 {L.getHeader()->getTerminator(), std::move(ValsToDuplicate)});
3047 return !UnswitchCandidates.
empty();
3062 if (!L.contains(IfTrue)) {
3068 if (L.isLoopInvariant(
LHS)) {
3076 RHS = ConstantInt::get(
3088 if (L.isLoopInvariant(
LHS) || !L.isLoopInvariant(
RHS))
3094 if (!L.contains(IfTrue) || L.contains(IfFalse))
3098 if (L.getHeader() == IfTrue)
3115 assert(Weights.
size() == 2 &&
"Unexpected profile data!");
3117 auto Num = Weights[Idx];
3118 auto Denom = Weights[0] + Weights[1];
3120 if (Denom == 0 || Num > Denom)
3123 if (LikelyTaken > ActualTaken)
3146static NonTrivialUnswitchCandidate
3150 assert(Candidate.hasPendingInjection() &&
"Nothing to inject!");
3151 BasicBlock *Preheader = L.getLoopPreheader();
3152 assert(Preheader &&
"Loop is not in simplified form?");
3154 "Unswitching branch of inner loop!");
3156 auto Pred = Candidate.PendingInjection->Pred;
3157 auto *
LHS = Candidate.PendingInjection->LHS;
3158 auto *
RHS = Candidate.PendingInjection->RHS;
3159 auto *InLoopSucc = Candidate.PendingInjection->InLoopSucc;
3162 auto *OutOfLoopSucc = InLoopSucc == TI->getSuccessor(0) ? TI->getSuccessor(1)
3163 : TI->getSuccessor(0);
3165 assert(L.contains(InLoopSucc) &&
"Not supported yet!");
3166 assert(!L.contains(OutOfLoopSucc) &&
"Not supported yet!");
3167 auto &Ctx = BB->getContext();
3171 if (
LHS->getType() !=
RHS->getType()) {
3172 if (
LHS->getType()->getIntegerBitWidth() <
3173 RHS->getType()->getIntegerBitWidth())
3174 LHS = Builder.CreateZExt(
LHS,
RHS->getType(),
LHS->getName() +
".wide");
3176 RHS = Builder.CreateZExt(
RHS,
LHS->getType(),
RHS->getName() +
".wide");
3180 auto *InjectedCond =
3185 BB->getParent(), InLoopSucc);
3186 Builder.SetInsertPoint(TI);
3188 Builder.CreateCondBr(InjectedCond, InLoopSucc, CheckBlock);
3192 Builder.SetInsertPoint(CheckBlock);
3193 Builder.CreateCondBr(
3194 TI->getCondition(), TI->getSuccessor(0), TI->getSuccessor(1),
3197 TI->eraseFromParent();
3200 for (
auto &
I : *InLoopSucc) {
3204 auto *Inc = PN->getIncomingValueForBlock(BB);
3205 PN->addIncoming(Inc, CheckBlock);
3207 OutOfLoopSucc->replacePhiUsesWith(BB, CheckBlock);
3219 L.addBasicBlockToLoop(CheckBlock, LI);
3231 LLVM_DEBUG(
dbgs() <<
"Injected a new loop-invariant branch " << *InvariantBr
3232 <<
" and considering it for unswitching.");
3233 ++NumInvariantConditionsInjected;
3234 return NonTrivialUnswitchCandidate(InvariantBr, { InjectedCond },
3256 if (Compares.
size() < 2)
3264 InjectedInvariant ToInject(NonStrictPred,
LHS,
RHS, InLoopSucc);
3265 NonTrivialUnswitchCandidate Candidate(Prev->Term, { LHS, RHS },
3266 std::nullopt, std::move(ToInject));
3267 UnswitchCandidates.
push_back(std::move(Candidate));
3297 auto *Latch = L.getLoopLatch();
3301 assert(L.getLoopPreheader() &&
"Must have a preheader!");
3306 for (
auto *DTN = DT.
getNode(Latch); L.contains(DTN->getBlock());
3307 DTN = DTN->getIDom()) {
3310 BasicBlock *IfTrue =
nullptr, *IfFalse =
nullptr;
3311 auto *BB = DTN->getBlock();
3315 auto *Term = BB->getTerminator();
3319 if (!
LHS->getType()->isIntegerTy())
3331 LHS = Zext->getOperand(0);
3332 CandidatesULT[
LHS].push_back(
Desc);
3336 for (
auto &It : CandidatesULT)
3343 if (!L.isSafeToClone())
3345 for (
auto *BB : L.blocks())
3346 for (
auto &
I : *BB) {
3347 if (
I.getType()->isTokenTy() &&
I.isUsedOutsideOfBlock(BB))
3350 assert(!CB->cannotDuplicate() &&
"Checked by L.isSafeToClone().");
3351 if (CB->isConvergent())
3368 L.getUniqueExitBlocks(ExitBlocks);
3373 for (
auto *ExitBB : ExitBlocks) {
3374 auto It = ExitBB->getFirstNonPHIIt();
3376 LLVM_DEBUG(
dbgs() <<
"Cannot unswitch because of cleanuppad/catchswitch "
3404 L.getHeader()->getParent()->hasMinSize()
3408 for (
auto *BB : L.blocks()) {
3410 for (
auto &
I : *BB) {
3415 assert(Cost >= 0 &&
"Must not have negative costs!");
3417 assert(LoopCost >= 0 &&
"Must not have negative loop costs!");
3418 BBCostMap[BB] = Cost;
3451 if (!Visited.
insert(SuccBB).second)
3459 if (!FullUnswitch) {
3463 if (SuccBB == BI.getSuccessor(1))
3466 if (SuccBB == BI.getSuccessor(0))
3469 SuccBB == BI.getSuccessor(0)) ||
3471 SuccBB == BI.getSuccessor(1)))
3479 if (SuccBB->getUniquePredecessor() ||
3481 return PredBB == &BB || DT.
dominates(SuccBB, PredBB);
3484 assert(Cost <= LoopCost &&
3485 "Non-duplicated cost should never exceed total loop cost!");
3494 int SuccessorsCount =
isGuard(&TI) ? 2 : Visited.
size();
3495 assert(SuccessorsCount > 1 &&
3496 "Cannot unswitch a condition without multiple distinct successors!");
3497 return (LoopCost - Cost) * (SuccessorsCount - 1);
3500 std::optional<NonTrivialUnswitchCandidate> Best;
3501 for (
auto &Candidate : UnswitchCandidates) {
3506 !BI || Candidate.hasPendingInjection() ||
3507 (Invariants.
size() == 1 &&
3509 InstructionCost CandidateCost = ComputeUnswitchedCost(TI, FullUnswitch);
3513 int CostMultiplier =
3517 "cost multiplier needs to be in the range of 1..UnswitchThreshold");
3518 CandidateCost *= CostMultiplier;
3520 <<
" (multiplier: " << CostMultiplier <<
")"
3521 <<
" for unswitch candidate: " << TI <<
"\n");
3524 <<
" for unswitch candidate: " << TI <<
"\n");
3527 if (!Best || CandidateCost < Best->Cost) {
3529 Best->Cost = CandidateCost;
3532 assert(Best &&
"Must be!");
3559 Cond, &AC, L.getLoopPreheader()->getTerminator(), &DT);
3573 PartialIVCondBranch, L, LI,
AA, MSSAU);
3576 PartialIVCondBranch, L, DT, LI,
AA,
3579 if (UnswitchCandidates.
empty())
3583 dbgs() <<
"Considering " << UnswitchCandidates.
size()
3584 <<
" non-trivial loop invariant conditions for unswitching.\n");
3587 UnswitchCandidates, L, DT, LI, AC,
TTI, PartialIVInfo);
3589 assert(Best.TI &&
"Failed to find loop unswitch candidate");
3590 assert(Best.Cost &&
"Failed to compute cost");
3593 LLVM_DEBUG(
dbgs() <<
"Cannot unswitch, lowest cost found: " << *Best.Cost
3598 bool InjectedCondition =
false;
3599 if (Best.hasPendingInjection()) {
3601 InjectedCondition =
true;
3603 assert(!Best.hasPendingInjection() &&
3604 "All injections should have been done by now!");
3606 if (Best.TI != PartialIVCondBranch)
3616 SI->getCondition(), &AC, L.getLoopPreheader()->getTerminator(), &DT);
3626 LLVM_DEBUG(
dbgs() <<
" Unswitching non-trivial (cost = " << Best.Cost
3627 <<
") terminator: " << *Best.TI <<
"\n");
3629 LI, AC, SE, MSSAU, LoopUpdater, InsertFreeze,
3660 assert(L.isRecursivelyLCSSAForm(DT, LI) &&
3661 "Loops must be in LCSSA form before unswitching.");
3664 if (!L.isLoopSimplifyForm())
3677 const Function *
F = L.getHeader()->getParent();
3690 bool ContinueWithNonTrivial =
3692 if (!ContinueWithNonTrivial)
3696 if (
F->hasOptSize())
3721 Function &
F = *L.getHeader()->getParent();
3723 LLVM_DEBUG(
dbgs() <<
"Unswitching loop in " <<
F.getName() <<
": " << L
3726 std::optional<MemorySSAUpdater> MSSAU;
3733 &AR.
SE, MSSAU ? &*MSSAU :
nullptr, U))
3739#ifdef EXPENSIVE_CHECKS
3754 OS, MapClassName2PassName);
3757 OS << (NonTrivial ?
"" :
"no-") <<
"nontrivial;";
3758 OS << (Trivial ?
"" :
"no-") <<
"trivial";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
This file defines the DenseMap class.
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
static Value * getCondition(Instruction *I)
Module.h This file contains the declarations for the Module class.
This defines the Use class.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
This header provides classes for managing per-loop analyses.
Loop::LoopBounds::Direction Direction
This header provides classes for managing a pipeline of passes over loops in LLVM IR.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
uint64_t IntrinsicInst * II
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
Provides some synthesis utilities to produce sequences of values.
This file implements a set that has insertion order iteration characteristics.
static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB, BasicBlock &OldExitingBB, BasicBlock &OldPH)
Rewrite the PHI nodes in an unswitched loop exit basic block.
static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
This routine scans the loop to find a branch or switch which occurs before any side effects occur.
static SmallPtrSet< const BasicBlock *, 16 > recomputeLoopBlockSet(Loop &L, LoopInfo &LI)
Recompute the set of blocks in a loop after unswitching.
static int CalculateUnswitchCostMultiplier(const Instruction &TI, const Loop &L, const LoopInfo &LI, const DominatorTree &DT, ArrayRef< NonTrivialUnswitchCandidate > UnswitchCandidates)
Cost multiplier is a way to limit potentially exponential behavior of loop-unswitch.
static TinyPtrVector< Value * > collectHomogenousInstGraphLoopInvariants(const Loop &L, Instruction &Root, const LoopInfo &LI)
Collect all of the loop invariant input values transitively used by the homogeneous instruction graph...
static void deleteDeadClonedBlocks(Loop &L, ArrayRef< BasicBlock * > ExitBlocks, ArrayRef< std::unique_ptr< ValueToValueMapTy > > VMaps, DominatorTree &DT, MemorySSAUpdater *MSSAU)
void visitDomSubTree(DominatorTree &DT, BasicBlock *BB, CallableT Callable)
Helper to visit a dominator subtree, invoking a callable on each node.
static bool isSafeForNoNTrivialUnswitching(Loop &L, LoopInfo &LI)
void postUnswitch(Loop &L, LPMUpdater &U, StringRef LoopName, bool CurrentLoopValid, bool PartiallyInvariant, bool InjectedCondition, ArrayRef< Loop * > NewLoops)
static bool shouldTryInjectInvariantCondition(const ICmpInst::Predicate Pred, const Value *LHS, const Value *RHS, const BasicBlock *IfTrue, const BasicBlock *IfFalse, const Loop &L)
Returns true, if predicate described by ( Pred, LHS, RHS ) succeeding into blocks ( IfTrue,...
static NonTrivialUnswitchCandidate findBestNonTrivialUnswitchCandidate(ArrayRef< NonTrivialUnswitchCandidate > UnswitchCandidates, const Loop &L, const DominatorTree &DT, const LoopInfo &LI, AssumptionCache &AC, const TargetTransformInfo &TTI, const IVConditionInfo &PartialIVInfo)
static void buildPartialInvariantUnswitchConditionalBranch(BasicBlock &BB, ArrayRef< Value * > ToDuplicate, bool Direction, BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, Loop &L, MemorySSAUpdater *MSSAU, const CondBrInst &OriginalBranch)
Copy a set of loop invariant values, and conditionally branch on them.
static Value * skipTrivialSelect(Value *Cond)
static Loop * getTopMostExitingLoop(const BasicBlock *ExitBB, const LoopInfo &LI)
static bool collectUnswitchCandidatesWithInjections(SmallVectorImpl< NonTrivialUnswitchCandidate > &UnswitchCandidates, IVConditionInfo &PartialIVInfo, Instruction *&PartialIVCondBranch, Loop &L, const DominatorTree &DT, const LoopInfo &LI, AAResults &AA, const MemorySSAUpdater *MSSAU)
Collect unswitch candidates by invariant conditions that are not immediately present in the loop.
static void replaceLoopInvariantUses(const Loop &L, Value *Invariant, Constant &Replacement)
static CondBrInst * turnGuardIntoBranch(IntrinsicInst *GI, Loop &L, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU)
Turns a llvm.experimental.guard intrinsic into implicit control flow branch, making the following rep...
static bool collectUnswitchCandidates(SmallVectorImpl< NonTrivialUnswitchCandidate > &UnswitchCandidates, IVConditionInfo &PartialIVInfo, Instruction *&PartialIVCondBranch, const Loop &L, const LoopInfo &LI, AAResults &AA, const MemorySSAUpdater *MSSAU)
static InstructionCost computeDomSubtreeCost(DomTreeNode &N, const SmallDenseMap< BasicBlock *, InstructionCost, 4 > &BBCostMap, SmallDenseMap< DomTreeNode *, InstructionCost, 4 > &DTCostMap)
Recursively compute the cost of a dominator subtree based on the per-block cost map provided.
static bool shouldInsertFreeze(Loop &L, Instruction &TI, DominatorTree &DT, AssumptionCache &AC)
bool shouldTryInjectBasingOnMetadata(const CondBrInst *BI, const BasicBlock *TakenSucc)
Returns true, if metadata on BI allows us to optimize branching into TakenSucc via injection of invar...
static void canonicalizeForInvariantConditionInjection(CmpPredicate &Pred, Value *&LHS, Value *&RHS, BasicBlock *&IfTrue, BasicBlock *&IfFalse, const Loop &L)
Tries to canonicalize condition described by:
static bool areLoopExitPHIsLoopInvariant(const Loop &L, const BasicBlock &ExitingBB, const BasicBlock &ExitBB)
Check that all the LCSSA PHI nodes in the loop exit block have trivial incoming values along this edg...
static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock &ExitBB, BasicBlock &UnswitchedBB, BasicBlock &OldExitingBB, BasicBlock &OldPH, bool FullUnswitch)
Rewrite the PHI nodes in the loop exit basic block and the split off unswitched block.
static bool insertCandidatesWithPendingInjections(SmallVectorImpl< NonTrivialUnswitchCandidate > &UnswitchCandidates, Loop &L, ICmpInst::Predicate Pred, ArrayRef< CompareDesc > Compares, const DominatorTree &DT)
Given chain of loop branch conditions looking like: br (Variant < Invariant1) br (Variant < Invariant...
static NonTrivialUnswitchCandidate injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate, Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, MemorySSAUpdater *MSSAU)
Materialize pending invariant condition of the given candidate into IR.
static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
Unswitch a trivial switch if the condition is loop invariant.
static void unswitchNontrivialInvariants(Loop &L, Instruction &TI, ArrayRef< Value * > Invariants, IVConditionInfo &PartialIVInfo, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, LPMUpdater &LoopUpdater, bool InsertFreeze, bool InjectedCondition)
static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef< BasicBlock * > ExitBlocks, LoopInfo &LI, SmallVectorImpl< Loop * > &HoistedLoops, ScalarEvolution *SE)
Rebuild a loop after unswitching removes some subset of blocks and edges.
static CondBrInst * turnSelectIntoBranch(SelectInst *SI, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, AssumptionCache *AC)
Turns a select instruction into implicit control flow branch, making the following replacement:
static bool unswitchBestCondition(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, AAResults &AA, TargetTransformInfo &TTI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, LPMUpdater &LoopUpdater)
static bool unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, AAResults &AA, TargetTransformInfo &TTI, bool Trivial, bool NonTrivial, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, LPMUpdater &LoopUpdater)
Unswitch control flow predicated on loop invariant conditions.
static bool unswitchTrivialBranch(Loop &L, CondBrInst &BI, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
Unswitch a trivial branch if the condition is loop invariant.
static BasicBlock * buildClonedLoopBlocks(Loop &L, BasicBlock *LoopPH, BasicBlock *SplitBB, ArrayRef< BasicBlock * > ExitBlocks, BasicBlock *ParentBB, BasicBlock *UnswitchedSuccBB, BasicBlock *ContinueSuccBB, const SmallDenseMap< BasicBlock *, BasicBlock *, 16 > &DominatingSucc, ValueToValueMapTy &VMap, SmallVectorImpl< DominatorTree::UpdateType > &DTUpdates, AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, ScalarEvolution *SE)
Build the cloned blocks for an unswitched copy of the given loop.
static void deleteDeadBlocksFromLoop(Loop &L, SmallVectorImpl< BasicBlock * > &ExitBlocks, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, ScalarEvolution *SE, LPMUpdater &LoopUpdater)
static void buildPartialUnswitchConditionalBranch(BasicBlock &BB, ArrayRef< Value * > Invariants, bool Direction, BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, bool InsertFreeze, const Instruction *I, AssumptionCache *AC, const DominatorTree &DT, const CondBrInst &ComputeProfFrom)
Copy a set of loop invariant values Invariants and insert them at the end of BB and conditionally bra...
static Loop * cloneLoopNest(Loop &OrigRootL, Loop *RootParentL, const ValueToValueMapTy &VMap, LoopInfo &LI)
Recursively clone the specified loop and all of its children.
static void hoistLoopToNewParent(Loop &L, BasicBlock &Preheader, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU, ScalarEvolution *SE)
Hoist the current loop up to the innermost loop containing a remaining exit.
static void buildClonedLoops(Loop &OrigL, ArrayRef< BasicBlock * > ExitBlocks, const ValueToValueMapTy &VMap, LoopInfo &LI, SmallVectorImpl< Loop * > &NonChildClonedLoops)
Build the cloned loops of an original loop from unswitching.
This file defines the SmallPtrSet class.
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)
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
const Instruction * getTerminatorOrNull() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
InstListType::iterator iterator
Instruction iterators...
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static LLVM_ABI CmpInst * Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
static LLVM_ABI bool isStrictPredicate(Predicate predicate)
This is a static version that you can use without an instruction available.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Conditional Branch instruction.
LLVM_ABI void swapSuccessors()
Swap the successors of this branch instruction.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
void setCondition(Value *V)
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
This is the shared class of boolean and integer constants.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
This is an important base class in LLVM.
LLVM_ABI bool isOneValue() const
Returns true if the value is one.
static DebugLoc getCompilerGenerated()
static DebugLoc getDropped()
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
void insertEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge insertion and update the tree.
static constexpr UpdateKind Delete
static constexpr UpdateKind Insert
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
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 bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
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.
This class represents a freeze function that returns random concrete value if an operand is either a ...
This implementation of LoopSafetyInfo use ImplicitControlFlowTracking to give precise answers on "may...
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop) const override
Returns true if the instruction in a loop is guaranteed to execute at least once (under the assumptio...
void computeLoopSafetyInfo(const Loop *CurLoop) override
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
CondBrInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Value * CreateFreeze(Value *V, const Twine &Name="")
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void dropLocation()
Drop the instruction's debug location.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
bool isTerminator() const
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
A wrapper class for inspecting calls to intrinsic functions.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
void markLoopAsDeleted(Loop &L, llvm::StringRef Name)
Loop passes should use this method to indicate they have deleted a loop from the nest.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
BlockT * getHeader() const
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void reserveBlocks(unsigned size)
interface to do reserve() for Blocks
iterator_range< block_iterator > blocks() const
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop.
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
LoopT * AllocateLoop(ArgsTy &&...Args)
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
void changeLoopFor(const BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
Represents a single loop in the control flow graph.
StringRef getName() const
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
An analysis that produces MemorySSA for a function.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
LLVM_ABI void removeEdge(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To following an edge deletion between From and To.
LLVM_ABI void updateForClonedLoop(const LoopBlocksRPO &LoopBlocks, ArrayRef< BasicBlock * > ExitBlocks, const ValueToValueMapTy &VM, bool IgnoreIncomingWithNoClones=false)
Update MemorySSA after a loop was cloned, given the blocks in RPO order, the exit blocks and a 1:1 ma...
LLVM_ABI void removeDuplicatePhiEdgesBetween(const BasicBlock *From, const BasicBlock *To)
Update the MemoryPhi in To to have a single incoming edge from From, following a CFG change that repl...
LLVM_ABI void removeBlocks(const SmallSetVector< BasicBlock *, 8 > &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
LLVM_ABI void moveAllAfterSpliceBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was spliced into From and To.
LLVM_ABI MemoryAccess * createMemoryAccessInBB(Instruction *I, MemoryAccess *Definition, const BasicBlock *BB, MemorySSA::InsertionPlace Point, bool CreationMustSucceed=true)
Create a MemoryAccess in MemorySSA at a specified point in a block.
LLVM_ABI void applyInsertUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT)
Apply CFG insert updates, analogous with the DT edge updates.
LLVM_ABI void applyUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT, bool UpdateDTFirst=false)
Apply CFG updates, analogous with the DT edge updates.
LLVM_ABI void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
LLVM_ABI void updateExitBlocksForClonedLoop(ArrayRef< BasicBlock * > ExitBlocks, const ValueToValueMapTy &VMap, DominatorTree &DT)
Update phi nodes in exit block successors following cloning.
Encapsulates MemorySSA, including all data associated with memory accesses.
DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
A Module instance is used to store all the information related to an LLVM module.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
The main scalar evolution driver.
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
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 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...
This class represents the LLVM 'select' instruction.
size_type size() const
Determine the number of elements in the SetVector.
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
iterator begin()
Get an iterator to the beginning of the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
LLVM_ABI void setSuccessorWeight(unsigned idx, CaseWeightOpt W)
LLVM_ABI Instruction::InstListType::iterator eraseFromParent()
Delegate the call to the underlying SwitchInst::eraseFromParent() and mark this object to not touch t...
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W)
Delegate the call to the underlying SwitchInst::addCase() and set the specified branch weight for the...
LLVM_ABI CaseWeightOpt getSuccessorWeight(unsigned idx)
std::optional< uint32_t > CaseWeightOpt
LLVM_ABI SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I)
Delegate the call to the underlying SwitchInst::removeCase() and remove correspondent branch weight.
unsigned getSuccessorIndex() const
Returns successor index for current case successor.
BasicBlockT * getCaseSuccessor() const
Resolves successor for current case.
ConstantIntT * getCaseValue() const
Resolves case value for current case.
BasicBlock * getDefaultDest() const
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
void setDefaultDest(BasicBlock *DefaultCase)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
void push_back(EltTy NewVal)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
A Use represents the edge between a Value definition and its users.
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
LLVM Value Representation.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVMContext & getContext() const
All values hold a context through their type.
iterator_range< use_iterator > uses()
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
@ BasicBlock
Various leaf nodes.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
LogicalOp_match< LHS, RHS, Instruction::And > m_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R either in the form of L & R or L ?
bool match(Val *V, const Pattern &P)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
auto m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
LogicalOp_match< LHS, RHS, Instruction::Or > m_LogicalOr(const LHS &L, const RHS &R)
Matches L || R either in the form of L | R or L ?
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
initializer< Ty > init(const Ty &Val)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
FunctionAddr VTableAddr Value
void stable_sort(R &&Range)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
cl::opt< bool > ProfcheckDisableMetadataFixes
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
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...
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
static cl::opt< int > UnswitchThreshold("unswitch-threshold", cl::init(50), cl::Hidden, cl::desc("The cost threshold for unswitching a loop."))
auto successors(const MachineBasicBlock *BB)
static cl::opt< bool > EnableNonTrivialUnswitch("enable-nontrivial-unswitch", cl::init(false), cl::Hidden, cl::desc("Forcibly enables non-trivial loop unswitching rather than " "following the configuration passed into the pass."))
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto cast_or_null(const Y &Val)
LLVM_ABI MDNode * findOptionMDForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for a loop.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
DomTreeNodeBase< BasicBlock > DomTreeNode
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
static cl::opt< bool > EnableUnswitchCostMultiplier("enable-unswitch-cost-multiplier", cl::init(true), cl::Hidden, cl::desc("Enable unswitch cost multiplier that prohibits exponential " "explosion in nontrivial unswitch."))
auto dyn_cast_or_null(const Y &Val)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
auto reverse(ContainerTy &&C)
static cl::opt< bool > DropNonTrivialImplicitNullChecks("simple-loop-unswitch-drop-non-trivial-implicit-null-checks", cl::init(false), cl::Hidden, cl::desc("If enabled, drop make.implicit metadata in unswitched implicit " "null checks to save time analyzing if we can keep it."))
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI)
Return true if the control flow in RPOTraversal is irreducible.
static cl::opt< unsigned > InjectInvariantConditionHotnesThreshold("simple-loop-unswitch-inject-invariant-condition-hotness-threshold", cl::Hidden, cl::desc("Only try to inject loop invariant conditions and " "unswitch on them to eliminate branches that are " "not-taken 1/<this option> times or less."), cl::init(16))
static cl::opt< int > UnswitchSiblingsToplevelDiv("unswitch-siblings-toplevel-div", cl::init(2), cl::Hidden, cl::desc("Toplevel siblings divisor for cost multiplier."))
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
void sort(IteratorTy Start, IteratorTy End)
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static cl::opt< bool > InjectInvariantConditions("simple-loop-unswitch-inject-invariant-conditions", cl::Hidden, cl::desc("Whether we should inject new invariants and unswitch them to " "eliminate some existing (non-invariant) conditions."), cl::init(true))
LLVM_ABI bool VerifyLoopInfo
Enable verification of loop info.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
FunctionAddr VTableAddr Next
LLVM_ABI bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
auto sum_of(R &&Range, E Init=E{0})
Returns the sum of all values in Range with Init initial value.
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
static cl::opt< int > UnswitchNumInitialUnscaledCandidates("unswitch-num-initial-unscaled-candidates", cl::init(8), cl::Hidden, cl::desc("Number of unswitch candidates that are ignored when calculating " "cost multiplier."))
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
static cl::opt< bool > EstimateProfile("simple-loop-unswitch-estimate-profile", cl::Hidden, cl::init(true))
static cl::opt< unsigned > MSSAThreshold("simple-loop-unswitch-memoryssa-threshold", cl::desc("Max number of memory uses to explore during " "partial unswitching analysis"), cl::init(100), cl::Hidden)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
auto predecessors(const MachineBasicBlock *BB)
bool pred_empty(const BasicBlock *BB)
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
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...
static cl::opt< bool > FreezeLoopUnswitchCond("freeze-loop-unswitch-cond", cl::init(true), cl::Hidden, cl::desc("If enabled, the freeze instruction will be added to condition " "of loop unswitch to prevent miscompilation."))
LLVM_ABI std::optional< IVConditionInfo > hasPartialIVCondition(const Loop &L, unsigned MSSAThreshold, const MemorySSA &MSSA, AAResults &AA)
Check if the loop header has a conditional branch that is not loop-invariant, because it involves loa...
LLVM_ABI bool formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
static cl::opt< bool > UnswitchGuards("simple-loop-unswitch-guards", cl::init(true), cl::Hidden, cl::desc("If enabled, simple loop unswitching will also consider " "llvm.experimental.guard intrinsics as unswitch candidates."))
LLVM_ABI void mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap)
Mark a cloned instruction as a new instance so that its source loc can be updated when remapped.
static cl::opt< int > UnswitchParentBlocksDiv("unswitch-parent-blocks-div", cl::init(8), cl::Hidden, cl::desc("Outer loop size divisor for cost multiplier."))
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A special type used by analysis passes to provide an address that identifies that particular analysis...
static LLVM_ABI void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop).
Struct to hold information about a partially invariant condition.
SmallVector< Instruction * > InstToDuplicate
Instructions that need to be duplicated and checked for the unswitching condition.
Constant * KnownValue
Constant to indicate for which value the condition is invariant.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
TargetTransformInfo & TTI
A CRTP mix-in to automatically provide informational APIs needed for passes.