59 #define DEBUG_TYPE "simple-loop-unswitch" 63 STATISTIC(NumBranches,
"Number of branches unswitched");
64 STATISTIC(NumSwitches,
"Number of switches unswitched");
65 STATISTIC(NumGuards,
"Number of guards turned into branches for unswitching");
66 STATISTIC(NumTrivial,
"Number of unswitches that are trivial");
68 NumCostMultiplierSkipped,
69 "Number of unswitch candidates that had their cost multiplier skipped");
73 cl::desc(
"Forcibly enables non-trivial loop unswitching rather than " 74 "following the configuration passed into the pass."));
78 cl::desc(
"The cost threshold for unswitching a loop."));
82 cl::desc(
"Enable unswitch cost multiplier that prohibits exponential " 83 "explosion in nontrivial unswitch."));
86 cl::desc(
"Toplevel siblings divisor for cost multiplier."));
89 cl::desc(
"Number of unswitch candidates that are ignored when calculating " 93 cl::desc(
"If enabled, simple loop unswitching will also consider " 94 "llvm.experimental.guard intrinsics as unswitch candidates."));
107 "Only need to walk the graph if root itself is not invariant.");
119 if (isa<Constant>(OpV))
124 Invariants.push_back(OpV);
134 if (Visited.
insert(OpI).second)
137 }
while (!Worklist.
empty());
144 assert(!isa<Constant>(Invariant) &&
"Why are we unswitching on a constant?");
147 for (
auto UI = Invariant->
use_begin(), UE = Invariant->
use_end(); UI != UE;) {
154 U->set(&Replacement);
185 for (
Value *Invariant :
188 Cond = IRB.
CreateOr(Cond, Invariant);
192 IRB.
CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc,
193 Direction ? &NormalSucc : &UnswitchedSucc);
210 for (
auto i : seq<int>(0, PN.getNumOperands())) {
211 assert(PN.getIncomingBlock(i) == &OldExitingBB &&
212 "Found incoming block different from unique predecessor!");
213 PN.setIncomingBlock(i, &OldPH);
230 assert(&ExitBB != &UnswitchedBB &&
231 "Must have different loop exit and unswitched blocks!");
235 PN.getName() +
".split", InsertPt);
246 for (
int i = PN.getNumIncomingValues() - 1; i >= 0; --i) {
247 if (PN.getIncomingBlock(i) != &OldExitingBB)
250 Value *Incoming = PN.getIncomingValue(i);
253 PN.removeIncomingValue(i);
255 NewPN->addIncoming(Incoming, &OldPH);
261 NewPN->addIncoming(&PN, &ExitBB);
279 Loop *NewParentL =
nullptr;
280 for (
auto *ExitBB : Exits)
282 if (!NewParentL || NewParentL->
contains(ExitL))
285 if (NewParentL == OldParentL)
291 "Can only hoist this loop up the nest!");
296 "Parent loop of this loop should contain this loop's preheader!");
311 for (
Loop *OldContainingL = OldParentL; OldContainingL != NewParentL;
315 return BB == &Preheader || L.
contains(BB);
318 OldContainingL->getBlocksSet().erase(&Preheader);
320 OldContainingL->getBlocksSet().erase(BB);
325 formLCSSA(*OldContainingL, DT, &LI,
nullptr);
357 LLVM_DEBUG(
dbgs() <<
" Trying to unswitch branch: " << BI <<
"\n");
364 bool FullUnswitch =
false;
370 if (
auto *CondInst = dyn_cast<Instruction>(BI.
getCondition()))
372 if (Invariants.
empty())
378 bool ExitDirection =
true;
379 int LoopExitSuccIdx = 0;
382 ExitDirection =
false;
388 auto *ContinueBB = BI.
getSuccessor(1 - LoopExitSuccIdx);
409 dbgs() <<
" unswitching trivial invariant conditions for: " << BI
411 for (
Value *Invariant : Invariants) {
412 dbgs() <<
" " << *Invariant <<
" == true";
413 if (Invariant != Invariants.back())
443 if (FullUnswitch && LoopExitBB->getUniquePredecessor()) {
445 "A branch's parent isn't a predecessor!");
446 UnswitchedBB = LoopExitBB;
449 SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI, MSSAU);
468 ParentBB->getInstList().push_back(BI.
clone());
482 "Must have an `or` of `i1`s for the condition!");
486 "Must have an `and` of `i1`s for the condition!");
488 *UnswitchedBB, *NewPH);
506 ParentBB->getTerminator()->eraseFromParent();
518 if (UnswitchedBB == LoopExitBB)
522 *ParentBB, *OldPH, FullUnswitch);
533 for (
Value *Invariant : Invariants)
576 LLVM_DEBUG(
dbgs() <<
" Trying to unswitch switch: " << SI <<
"\n");
586 for (
auto Case : SI.
cases()) {
587 auto *SuccBB = Case.getCaseSuccessor();
590 ExitCaseIndices.
push_back(Case.getCaseIndex());
597 else if (ExitCaseIndices.
empty())
615 if (!ExitL || ExitL->
contains(OuterL))
629 if (!ExitL || ExitL->
contains(OuterL))
632 ExitCases.
push_back({CaseI->getCaseValue(), CaseI->getCaseSuccessor()});
650 return Case.getCaseSuccessor() ==
653 CommonSuccBB = SI.
case_begin()->getCaseSuccessor();
654 if (!DefaultExitBB) {
661 CommonSuccBB =
nullptr;
686 UnswitchedExitBBs.
insert(DefaultExitBB);
694 DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB;
699 for (
auto &CasePair :
reverse(ExitCases)) {
707 if (UnswitchedExitBBs.insert(ExitBB).second)
714 BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
723 CasePair.second = SplitExitBB;
728 for (
auto CasePair :
reverse(ExitCases)) {
732 NewSI->addCase(CaseVal, UnswitchedBB);
738 NewSI->setDefaultDest(DefaultExitBB);
742 for (
auto Case : SI.
cases())
743 NewSI->addCase(Case.getCaseValue(), NewPH);
755 bool SkippedFirst = DefaultExitBB ==
nullptr;
756 for (
auto Case : SI.
cases()) {
757 assert(Case.getCaseSuccessor() == CommonSuccBB &&
758 "Non-common successor!");
770 }
else if (DefaultExitBB) {
772 "If we had no cases we'd have a common successor!");
777 auto LastCaseI = std::prev(SI.
case_end());
787 for (
auto *UnswitchedExitBB : UnswitchedExitBBs) {
791 for (
auto SplitUnswitchedPair : SplitExitBBMap) {
792 auto *UnswitchedBB = SplitUnswitchedPair.second;
831 bool Changed =
false;
846 Visited.
insert(CurrentBB);
857 if (
auto *
SI = dyn_cast<SwitchInst>(CurrentTerm)) {
861 if (isa<Constant>(
SI->getCondition()))
876 if (!BI || BI->isConditional())
879 CurrentBB = BI->getSuccessor(0);
891 if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
905 if (BI->isConditional())
909 CurrentBB = BI->getSuccessor(0);
914 }
while (L.
contains(CurrentBB) && Visited.
insert(CurrentBB).second);
970 auto It = DominatingSucc.
find(BB);
971 return It != DominatingSucc.
end() && It->second != UnswitchedSuccBB;
975 auto *ClonedPH = CloneBlock(LoopPH);
978 for (
auto *LoopBB : L.
blocks())
979 if (!SkipBlock(LoopBB))
985 for (
auto *ExitBB : ExitBlocks) {
986 if (SkipBlock(ExitBB))
994 auto *MergeBB =
SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI, MSSAU);
1000 ExitBB->setName(
Twine(MergeBB->getName()) +
".split");
1003 auto *ClonedExitBB = CloneBlock(ExitBB);
1004 assert(ClonedExitBB->getTerminator()->getNumSuccessors() == 1 &&
1005 "Exit block should have been split to have one successor!");
1006 assert(ClonedExitBB->getTerminator()->getSuccessor(0) == MergeBB &&
1007 "Cloned exit block has the wrong successor!");
1013 std::prev(ClonedExitBB->end())))) {
1020 (isa<PHINode>(I) || isa<LandingPadInst>(I) || isa<CatchPadInst>(I)) &&
1021 "Bad instruction in exit block!");
1023 assert(VMap.
lookup(&I) == &ClonedI &&
"Mismatch in the value map!");
1027 &*MergeBB->getFirstInsertionPt());
1029 MergePN->addIncoming(&I, ExitBB);
1030 MergePN->addIncoming(&ClonedI, ClonedExitBB);
1039 for (
auto *ClonedBB : NewBlocks)
1043 if (
auto *II = dyn_cast<IntrinsicInst>(&
I))
1050 for (
auto *LoopBB : L.
blocks())
1051 if (SkipBlock(LoopBB))
1053 if (
auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.
lookup(SuccBB)))
1054 for (
PHINode &PN : ClonedSuccBB->phis())
1055 PN.removeIncomingValue(LoopBB,
false);
1059 auto *ClonedParentBB = cast<BasicBlock>(VMap.
lookup(ParentBB));
1061 if (SuccBB == UnswitchedSuccBB)
1064 auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.
lookup(SuccBB));
1068 ClonedSuccBB->removePredecessor(ClonedParentBB,
1074 auto *ClonedSuccBB = cast<BasicBlock>(VMap.
lookup(UnswitchedSuccBB));
1075 ClonedParentBB->getTerminator()->eraseFromParent();
1081 for (
PHINode &PN : ClonedSuccBB->phis()) {
1085 for (
int i = PN.getNumOperands() - 1; i >= 0; --i) {
1086 if (PN.getIncomingBlock(i) != ClonedParentBB)
1092 PN.removeIncomingValue(i,
false);
1098 for (
auto *ClonedBB : NewBlocks) {
1100 if (SuccSet.
insert(SuccBB).second)
1101 DTUpdates.
push_back({DominatorTree::Insert, ClonedBB, SuccBB});
1116 auto AddClonedBlocksToLoop = [&](
Loop &OrigL,
Loop &ClonedL) {
1117 assert(ClonedL.getBlocks().empty() &&
"Must start with an empty loop!");
1119 for (
auto *BB : OrigL.
blocks()) {
1120 auto *ClonedBB = cast<BasicBlock>(VMap.
lookup(BB));
1121 ClonedL.addBlockEntry(ClonedBB);
1134 AddClonedBlocksToLoop(OrigRootL, *ClonedRootL);
1136 if (OrigRootL.
empty())
1146 LoopsToClone.
push_back({ClonedRootL, ChildL});
1148 Loop *ClonedParentL, *L;
1149 std::tie(ClonedParentL, L) = LoopsToClone.
pop_back_val();
1152 AddClonedBlocksToLoop(*L, *ClonedL);
1154 LoopsToClone.
push_back({ClonedL, ChildL});
1155 }
while (!LoopsToClone.
empty());
1176 Loop *ClonedL =
nullptr;
1181 auto *ClonedPH = cast<BasicBlock>(VMap.
lookup(OrigPH));
1182 auto *ClonedHeader = cast<BasicBlock>(VMap.
lookup(OrigHeader));
1188 Loop *ParentL =
nullptr;
1192 for (
auto *ExitBB : ExitBlocks)
1193 if (
auto *ClonedExitBB = cast_or_null<BasicBlock>(VMap.
lookup(ExitBB)))
1195 ExitLoopMap[ClonedExitBB] = ExitL;
1196 ClonedExitsInLoops.
push_back(ClonedExitBB);
1197 if (!ParentL || (ParentL != ExitL && ParentL->contains(ExitL)))
1202 "The computed parent loop should always contain (or be) the parent of " 1203 "the original loop.");
1210 for (
auto *BB : OrigL.
blocks())
1211 if (
auto *ClonedBB = cast_or_null<BasicBlock>(VMap.
lookup(BB)))
1212 ClonedLoopBlocks.
insert(ClonedBB);
1223 if (Pred == ClonedPH)
1228 assert(ClonedLoopBlocks.count(Pred) &&
"Found a predecessor of the loop " 1229 "header other than the preheader " 1230 "that is not part of the loop!");
1235 if (BlocksInClonedLoop.
insert(Pred).second && Pred != ClonedHeader)
1242 if (!BlocksInClonedLoop.
empty()) {
1243 BlocksInClonedLoop.
insert(ClonedHeader);
1245 while (!Worklist.
empty()) {
1248 "Didn't put block into the loop set!");
1256 if (ClonedLoopBlocks.count(Pred) &&
1257 BlocksInClonedLoop.
insert(Pred).second)
1264 ParentL->addChildLoop(ClonedL);
1276 for (
auto *BB : OrigL.
blocks()) {
1277 auto *ClonedBB = cast_or_null<BasicBlock>(VMap.
lookup(BB));
1278 if (!ClonedBB || !BlocksInClonedLoop.
count(ClonedBB))
1291 PL->addBlockEntry(ClonedBB);
1298 for (
Loop *ChildL : OrigL) {
1299 auto *ClonedChildHeader =
1300 cast_or_null<BasicBlock>(VMap.
lookup(ChildL->getHeader()));
1301 if (!ClonedChildHeader || !BlocksInClonedLoop.
count(ClonedChildHeader))
1307 for (
auto *ChildLoopBB : ChildL->blocks())
1309 cast<BasicBlock>(VMap.
lookup(ChildLoopBB))) &&
1310 "Child cloned loop has a header within the cloned outer " 1311 "loop but not all of its blocks!");
1326 if (BlocksInClonedLoop.
empty())
1327 UnloopedBlockSet.
insert(ClonedPH);
1328 for (
auto *ClonedBB : ClonedLoopBlocks)
1329 if (!BlocksInClonedLoop.
count(ClonedBB))
1330 UnloopedBlockSet.
insert(ClonedBB);
1336 auto OrderedClonedExitsInLoops = ClonedExitsInLoops;
1344 while (!UnloopedBlockSet.
empty() && !OrderedClonedExitsInLoops.empty()) {
1345 assert(Worklist.
empty() &&
"Didn't clear worklist!");
1347 BasicBlock *ExitBB = OrderedClonedExitsInLoops.pop_back_val();
1362 if (!UnloopedBlockSet.
erase(PredBB)) {
1364 (BlocksInClonedLoop.
count(PredBB) || ExitLoopMap.
count(PredBB)) &&
1365 "Predecessor not mapped to a loop!");
1372 bool Inserted = ExitLoopMap.
insert({PredBB, ExitL}).
second;
1374 assert(Inserted &&
"Should only visit an unlooped block once!");
1379 }
while (!Worklist.
empty());
1388 for (
auto *BB : llvm::concat<BasicBlock *const>(
1389 makeArrayRef(ClonedPH), ClonedLoopBlocks, ClonedExitsInLoops))
1391 OuterL->addBasicBlockToLoop(BB, LI);
1394 for (
auto &BBAndL : ExitLoopMap) {
1395 auto *BB = BBAndL.first;
1396 auto *OuterL = BBAndL.second;
1398 "Failed to put all blocks into outer loops!");
1405 for (
Loop *ChildL : OrigL) {
1406 auto *ClonedChildHeader =
1407 cast_or_null<BasicBlock>(VMap.
lookup(ChildL->getHeader()));
1408 if (!ClonedChildHeader || BlocksInClonedLoop.
count(ClonedChildHeader))
1412 for (
auto *ChildLoopBB : ChildL->blocks())
1414 "Cloned a child loop header but not all of that loops blocks!");
1418 *ChildL, ExitLoopMap.lookup(ClonedChildHeader), VMap, LI));
1424 ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
1428 for (
BasicBlock *BB : llvm::concat<BasicBlock *const>(L.
blocks(), ExitBlocks))
1429 for (
auto &VMap : VMaps)
1430 if (
BasicBlock *ClonedBB = cast_or_null<BasicBlock>(VMap->lookup(BB)))
1433 SuccBB->removePredecessor(ClonedBB);
1446 BB->dropAllReferences();
1449 BB->eraseFromParent();
1465 while (!DeathCandidates.empty()) {
1466 auto *BB = DeathCandidates.pop_back_val();
1469 SuccBB->removePredecessor(BB);
1470 DeathCandidates.push_back(SuccBB);
1487 for (
auto *BB : DeadBlockSet)
1488 ParentL->getBlocksSet().erase(BB);
1490 [&](
BasicBlock *BB) {
return DeadBlockSet.count(BB); });
1496 if (!DeadBlockSet.
count(ChildL->getHeader()))
1501 return DeadBlockSet.
count(ChildBB);
1503 "If the child loop header is dead all blocks in the child loop must " 1504 "be dead as well!");
1512 for (
auto *BB : DeadBlockSet) {
1514 assert(!DT.
getNode(BB) &&
"Should already have cleared domtree!");
1521 for (
auto *BB : DeadBlockSet)
1555 assert(L.
contains(Pred) &&
"Found a predecessor of the loop header other " 1556 "than the preheader that is not part of the " 1562 if (LoopBlockSet.
insert(Pred).second && Pred != Header)
1567 if (LoopBlockSet.
empty())
1568 return LoopBlockSet;
1571 while (!Worklist.
empty()) {
1573 assert(LoopBlockSet.
count(BB) &&
"Didn't put block into the loop set!");
1586 "Should not reach a loop *outside* this loop!");
1589 auto *InnerPH = InnerL->getLoopPreheader();
1590 assert(L.
contains(InnerPH) &&
"Cannot contain an inner loop block " 1591 "but not contain the inner loop " 1593 if (!LoopBlockSet.
insert(InnerPH).second)
1603 for (
auto *InnerBB : InnerL->blocks()) {
1604 if (InnerBB == BB) {
1606 "Block should already be in the set!");
1610 LoopBlockSet.
insert(InnerBB);
1626 assert(LoopBlockSet.
count(Header) &&
"Cannot fail to add the header!");
1630 return LoopBlockSet;
1654 Loop *ParentL =
nullptr;
1658 for (
auto *ExitBB : ExitBlocks)
1662 if (!ParentL || (ParentL != ExitL && ParentL->
contains(ExitL)))
1674 if (!LoopBlockSet.empty() && L.
getParentLoop() != ParentL) {
1678 IL->getBlocksSet().erase(PH);
1679 for (
auto *BB : L.
blocks())
1680 IL->getBlocksSet().erase(BB);
1697 LoopBlockSet.empty()
1699 : std::stable_partition(
1700 Blocks.begin(), Blocks.end(),
1701 [&](
BasicBlock *BB) {
return LoopBlockSet.count(BB); });
1705 if (LoopBlockSet.empty())
1706 UnloopedBlocks.
insert(PH);
1709 for (
auto *BB :
make_range(BlocksSplitI, Blocks.end()))
1711 Blocks.erase(BlocksSplitI, Blocks.end());
1715 std::stable_sort(ExitsInLoops.
begin(), ExitsInLoops.
end(),
1724 auto RemoveUnloopedBlocksFromLoop =
1726 for (
auto *BB : UnloopedBlocks)
1729 return UnloopedBlocks.count(BB);
1734 while (!UnloopedBlocks.empty() && !ExitsInLoops.
empty()) {
1735 assert(Worklist.
empty() &&
"Didn't clear worklist!");
1736 assert(NewExitLoopBlocks.empty() &&
"Didn't clear loop set!");
1741 assert(ExitL.
contains(&L) &&
"Exit loop must contain the inner loop!");
1747 for (; PrevExitL != &ExitL; PrevExitL = PrevExitL->
getParentLoop())
1748 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
1762 if (!UnloopedBlocks.erase(PredBB)) {
1763 assert((NewExitLoopBlocks.count(PredBB) ||
1765 "Predecessor not in a nested loop (or already visited)!");
1772 bool Inserted = NewExitLoopBlocks.insert(PredBB).second;
1774 assert(Inserted &&
"Should only visit an unlooped block once!");
1779 }
while (!Worklist.
empty());
1784 for (
auto *BB : NewExitLoopBlocks)
1791 NewExitLoopBlocks.clear();
1797 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
1798 for (
auto *BB : UnloopedBlocks)
1807 auto SubLoopsSplitI =
1808 LoopBlockSet.empty()
1810 : std::stable_partition(
1811 SubLoops.begin(), SubLoops.end(), [&](
Loop *SubL) {
1812 return LoopBlockSet.count(SubL->getHeader());
1814 for (
auto *HoistedL :
make_range(SubLoopsSplitI, SubLoops.end())) {
1816 HoistedL->setParentLoop(
nullptr);
1826 if (
auto *NewParentL = LI.
getLoopFor(HoistedL->getLoopPreheader()))
1827 NewParentL->addChildLoop(HoistedL);
1831 SubLoops.erase(SubLoopsSplitI, SubLoops.end());
1834 if (Blocks.empty()) {
1835 assert(SubLoops.empty() &&
1836 "Failed to remove all subloops from the original loop!");
1851 template <
typename CallableT>
1869 "Cannot visit a node twice when walking a tree!");
1872 }
while (!DomWorklist.
empty());
1887 "Can only unswitch switches and conditional branch!");
1888 bool FullUnswitch = SI || BI->
getCondition() == Invariants[0];
1891 "Cannot have other invariants with full unswitching!");
1894 "Partial unswitching requires an instruction as the condition!");
1905 bool Direction =
true;
1907 if (!FullUnswitch) {
1911 "Only `or` and `and` instructions can combine invariants being " 1919 BI ? BI->
getSuccessor(1 - ClonedSucc) : SI->getDefaultDest();
1924 for (
auto Case : SI->cases())
1925 if (Case.getCaseSuccessor() != RetainedSuccBB)
1926 UnswitchedSuccBBs.
insert(Case.getCaseSuccessor());
1928 assert(!UnswitchedSuccBBs.
count(RetainedSuccBB) &&
1929 "Should not unswitch the same successor we are retaining!");
1947 Loop *OuterExitL = &L;
1948 for (
auto *ExitBB : ExitBlocks) {
1950 if (!NewOuterExitL) {
1952 OuterExitL =
nullptr;
1955 if (NewOuterExitL != OuterExitL && NewOuterExitL->
contains(OuterExitL))
1956 OuterExitL = NewOuterExitL;
1975 for (
auto *SuccBB : llvm::concat<BasicBlock *const>(
1977 if (SuccBB->getUniquePredecessor() ||
1979 return PredBB == ParentBB || DT.
dominates(SuccBB, PredBB);
1982 DominatingSucc[BB] = SuccBB;
2001 for (
auto *SuccBB : UnswitchedSuccBBs) {
2004 L, LoopPH, SplitBB, ExitBlocks, ParentBB, SuccBB, RetainedSuccBB,
2005 DominatingSucc, *VMaps.
back(), DTUpdates, AC, DT, LI, MSSAU);
2015 SplitBB->getInstList().
splice(SplitBB->end(), ParentBB->getInstList(), TI);
2019 ParentBB->getInstList().push_back(NewTI);
2028 assert(SI &&
"Must either be a branch or switch!");
2031 assert(SI->getDefaultDest() == RetainedSuccBB &&
2032 "Not retaining default successor!");
2033 SI->setDefaultDest(LoopPH);
2034 for (
auto &Case : SI->cases())
2035 if (Case.getCaseSuccessor() == RetainedSuccBB)
2036 Case.setSuccessor(LoopPH);
2038 Case.setSuccessor(ClonedPHs.
find(Case.getCaseSuccessor())->
second);
2044 DTUpdates.push_back(
2059 for (
auto &VMap : VMaps)
2074 assert(UnswitchedSuccBBs.size() == 1 &&
2075 "Only one possible unswitched block for a branch!");
2077 UnswitchedSuccBB->removePredecessor(ParentBB,
2089 "Not retaining default successor!");
2090 for (
auto &Case : NewSI->
cases())
2091 Case.getCaseSuccessor()->removePredecessor(
2103 ParentBB->getTerminator()->eraseFromParent();
2109 assert(BI &&
"Only branches have partial unswitching.");
2110 assert(UnswitchedSuccBBs.size() == 1 &&
2111 "Only one possible unswitched block for a branch!");
2116 *ClonedPH, *LoopPH);
2122 if (!FullUnswitch && MSSAU) {
2140 for (std::unique_ptr<ValueToValueMapTy> &VMap : VMaps)
2169 assert(UnswitchedSuccBBs.size() == 1 &&
2170 "Only one possible unswitched block for a branch!");
2182 bool ReplaceUnswitched = FullUnswitch || (Invariants.
size() == 1);
2190 for (
Value *Invariant : Invariants)
2191 for (
auto UI = Invariant->use_begin(), UE = Invariant->use_end();
2202 U->set(ContinueReplacement);
2203 else if (ReplaceUnswitched &&
2205 U->set(UnswitchedReplacement);
2221 auto UpdateLoop = [&](
Loop &UpdateL) {
2223 UpdateL.verifyLoop();
2224 for (
Loop *ChildL : UpdateL) {
2225 ChildL->verifyLoop();
2226 assert(ChildL->isRecursivelyLCSSAForm(DT, LI) &&
2227 "Perturbed a child loop's LCSSA form!");
2247 for (
Loop *UpdatedL :
2248 llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops)) {
2249 UpdateLoop(*UpdatedL);
2250 if (!UpdatedL->getParentLoop())
2251 OuterExitL =
nullptr;
2256 OuterExitL =
nullptr;
2261 if (OuterExitL != &L)
2262 for (
Loop *OuterL = ParentL; OuterL != OuterExitL;
2264 UpdateLoop(*OuterL);
2276 for (
Loop *UpdatedL : llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops))
2277 if (UpdatedL->getParentLoop() == ParentL)
2279 UnswitchCB(IsStillLoop, SibLoops);
2303 if (BBCostIt == BBCostMap.
end())
2307 auto DTCostIt = DTCostMap.
find(&N);
2308 if (DTCostIt != DTCostMap.
end())
2309 return DTCostIt->second;
2313 int Cost = std::accumulate(
2319 assert(Inserted &&
"Should not insert a node while visiting children!");
2359 if (Successors.
insert(Succ).second)
2360 DTUpdates.
push_back({DominatorTree::Delete, CheckBB, Succ});
2369 BasicBlock *GuardedBlock = CheckBI->getSuccessor(0);
2370 GuardedBlock->
setName(
"guarded");
2371 CheckBI->getSuccessor(1)->setName(
"deopt");
2372 BasicBlock *DeoptBlock = CheckBI->getSuccessor(1);
2375 ExitBlocks.
push_back(CheckBI->getSuccessor(1));
2385 DTUpdates.
push_back({DominatorTree::Insert, CheckBB, Succ});
2389 for (
auto *Succ : Successors)
2424 UnswitchCandidates) {
2437 NumCostMultiplierSkipped++;
2442 int SiblingsCount = (ParentL ? ParentL->getSubLoopsVector().size()
2443 : std::distance(LI.
begin(), LI.
end()));
2446 int UnswitchedClones = 0;
2447 for (
auto Candidate : UnswitchCandidates) {
2450 bool SkipExitingSuccessors = DT.
dominates(CondBlock, Latch);
2452 if (!SkipExitingSuccessors)
2458 return !SkipExitingSuccessors || L.
contains(SuccBB);
2460 UnswitchedClones +=
Log2_32(NonExitingSuccessors);
2468 unsigned ClonesPower =
2472 int SiblingsMultiplier =
2483 CostMultiplier = std::min(SiblingsMultiplier * (1 << ClonesPower),
2487 <<
" (siblings " << SiblingsMultiplier <<
" * clones " 2488 << (1 << ClonesPower) <<
")" 2489 <<
" for unswitch candidate: " << TI <<
"\n");
2490 return CostMultiplier;
2504 bool CollectGuards =
false;
2508 if (GuardDecl && !GuardDecl->use_empty())
2509 CollectGuards =
true;
2512 for (
auto *BB : L.
blocks()) {
2519 auto *Cond = cast<IntrinsicInst>(&
I)->getArgOperand(0);
2522 UnswitchCandidates.
push_back({&I, {Cond}});
2525 if (
auto *
SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
2528 if (!isa<Constant>(
SI->getCondition()) &&
2535 if (!BI || !BI->isConditional() || isa<Constant>(BI->getCondition()) ||
2536 BI->getSuccessor(0) == BI->getSuccessor(1))
2540 UnswitchCandidates.
push_back({BI, {BI->getCondition()}});
2544 Instruction &CondI = *cast<Instruction>(BI->getCondition());
2545 if (CondI.
getOpcode() != Instruction::And &&
2551 if (Invariants.
empty())
2554 UnswitchCandidates.
push_back({BI, std::move(Invariants)});
2558 if (UnswitchCandidates.empty())
2569 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
2579 for (
auto *ExitBB : ExitBlocks)
2580 if (isa<CleanupPadInst>(ExitBB->getFirstNonPHI())) {
2581 dbgs() <<
"Cannot unswitch because of cleanuppad in exit block\n";
2586 dbgs() <<
"Considering " << UnswitchCandidates.size()
2587 <<
" non-trivial loop invariant conditions for unswitching.\n");
2604 for (
auto *BB : L.
blocks()) {
2606 for (
auto &
I : *BB) {
2607 if (EphValues.count(&
I))
2610 if (
I.getType()->isTokenTy() &&
I.isUsedOutsideOfBlock(BB))
2613 if (CS.isConvergent() || CS.cannotDuplicate())
2618 assert(Cost >= 0 &&
"Must not have negative costs!");
2620 assert(LoopCost >= 0 &&
"Must not have negative loop costs!");
2621 BBCostMap[BB] = Cost;
2642 auto ComputeUnswitchedCost = [&](
Instruction &TI,
bool FullUnswitch) {
2646 int Cost = LoopCost;
2649 if (!Visited.
insert(SuccBB).second)
2656 if (!FullUnswitch) {
2657 auto &BI = cast<BranchInst>(TI);
2658 if (cast<Instruction>(BI.getCondition())->
getOpcode() ==
2660 if (SuccBB == BI.getSuccessor(1))
2665 "Only `and` and `or` conditions can result in a partial " 2667 if (SuccBB == BI.getSuccessor(0))
2676 if (SuccBB->getUniquePredecessor() ||
2678 return PredBB == &BB || DT.
dominates(SuccBB, PredBB);
2682 "Non-duplicated cost should never exceed total loop cost!");
2691 int SuccessorsCount =
isGuard(&TI) ? 2 : Visited.
size();
2692 assert(SuccessorsCount > 1 &&
2693 "Cannot unswitch a condition without multiple distinct successors!");
2694 return Cost * (SuccessorsCount - 1);
2697 int BestUnswitchCost;
2699 for (
auto &TerminatorAndInvariants : UnswitchCandidates) {
2703 int CandidateCost = ComputeUnswitchedCost(
2704 TI, !BI || (Invariants.
size() == 1 &&
2709 int CostMultiplier =
2713 "cost multiplier needs to be in the range of 1..UnswitchThreshold");
2714 CandidateCost *= CostMultiplier;
2716 <<
" (multiplier: " << CostMultiplier <<
")" 2717 <<
" for unswitch candidate: " << TI <<
"\n");
2720 <<
" for unswitch candidate: " << TI <<
"\n");
2723 if (!BestUnswitchTI || CandidateCost < BestUnswitchCost) {
2724 BestUnswitchTI = &TI;
2725 BestUnswitchCost = CandidateCost;
2726 BestUnswitchInvariants = Invariants;
2732 << BestUnswitchCost <<
"\n");
2739 ExitBlocks, DT, LI, MSSAU);
2742 << BestUnswitchCost <<
") terminator: " << *BestUnswitchTI
2745 ExitBlocks, DT, LI, AC, UnswitchCB, SE, MSSAU);
2776 "Loops must be in LCSSA form before unswitching.");
2777 bool Changed =
false;
2787 UnswitchCB(
true, {});
2823 std::string LoopName = L.
getName();
2825 auto UnswitchCB = [&L, &U, &LoopName](
bool CurrentLoopValid,
2828 if (!NewLoops.empty())
2829 U.addSiblingLoops(NewLoops);
2833 if (CurrentLoopValid)
2834 U.revisitCurrentLoop();
2836 U.markLoopAsDeleted(L, LoopName);
2860 class SimpleLoopUnswitchLegacyPass :
public LoopPass {
2866 explicit SimpleLoopUnswitchLegacyPass(
bool NonTrivial =
false)
2867 :
LoopPass(ID), NonTrivial(NonTrivial) {
2896 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2897 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2898 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
2899 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
2903 MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
2907 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
2908 auto *SE = SEWP ? &SEWP->getSE() :
nullptr;
2910 auto UnswitchCB = [&L, &LPM](
bool CurrentLoopValid,
2913 for (
auto *NewL : NewLoops)
2919 if (CurrentLoopValid)
2928 bool Changed =
unswitchLoop(*L, DT, LI, AC, TTI, NonTrivial, UnswitchCB, SE,
2947 "Simple unswitch loops",
false,
false)
2958 return new SimpleLoopUnswitchLegacyPass(NonTrivial);
Pass interface - Implemented by all 'passes'.
const T & front() const
front - Get the first element.
static 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)...
static cl::opt< int > UnswitchThreshold("unswitch-threshold", cl::init(50), cl::Hidden, cl::desc("The cost threshold for unswitching a loop."))
void destroy(LoopT *L)
Destroy a loop that has been removed from the LoopInfo nest.
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
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."))
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
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)
Build the cloned blocks for an unswitched copy of the given loop.
This routine provides some synthesis utilities to produce sequences of values.
static ConstantInt * getFalse(LLVMContext &Context)
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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."))
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...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
std::vector< BlockT * > & getBlocksVector()
Return a direct, mutable handle to the blocks vector so that we can mutate it efficiently with techni...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
bool VerifyMemorySSA
Enables verification of MemorySSA.
This class represents lattice values for constants.
void swapSuccessors()
Swap the successors of this branch instruction.
size_type size() const
Determine the number of elements in the SetVector.
void updateExitBlocksForClonedLoop(ArrayRef< BasicBlock *> ExitBlocks, const ValueToValueMapTy &VMap, DominatorTree &DT)
Update phi nodes in exit block successors following cloning.
void removeDuplicatePhiEdgesBetween(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To to have a single incoming edge from From, following a CFG change that repl...
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
void applyInsertUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT)
Apply CFG insert updates, analogous with the DT edge updates.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
unsigned getLoopDepth() const
Return the nesting level of this loop.
void push_back(const T &Elt)
void reserveBlocks(unsigned size)
interface to do reserve() for Blocks
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
The main scalar evolution driver.
void removeBlocks(const SmallPtrSetImpl< BasicBlock *> &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
An immutable pass that tracks lazily created AssumptionCache objects.
Value * getCondition() const
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
An efficient, type-erasing, non-owning reference to a callable.
A cache of @llvm.assume calls within a function.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
LLVMContext & getContext() const
All values hold a context through their type.
auto count_if(R &&Range, UnaryPredicate P) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
std::vector< LoopT * > & getSubLoopsVector()
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
void setArgOperand(unsigned i, Value *v)
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
static int computeDomSubtreeCost(DomTreeNode &N, const SmallDenseMap< BasicBlock *, int, 4 > &BBCostMap, SmallDenseMap< DomTreeNode *, int, 4 > &DTCostMap)
Recursively compute the cost of a dominator subtree based on the per-block cost map provided...
Value * getCondition() const
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...
This defines the Use class.
static bool areLoopExitPHIsLoopInvariant(Loop &L, BasicBlock &ExitingBB, BasicBlock &ExitBB)
Check that all the LCSSA PHI nodes in the loop exit block have trivial incoming values along this edg...
void reserve(size_type N)
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
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...
iterator begin()
Instruction iterator methods.
static void deleteDeadBlocksFromLoop(Loop &L, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU)
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the edge connecting specified block.
Value * getArgOperand(unsigned i) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void removeEdge(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To following an edge deletion between From and To.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void initializeSimpleLoopUnswitchLegacyPassPass(PassRegistry &)
Legacy analysis pass which computes MemorySSA.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static constexpr UpdateKind Delete
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
static void unswitchNontrivialInvariants(Loop &L, Instruction &TI, ArrayRef< Value *> Invariants, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
SmallPtrSetImpl< const BlockT * > & getBlocksSet()
Return a direct, mutable handle to the blocks set so that we can mutate it efficiently.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void deleteSimpleAnalysisLoop(Loop *L)
Invoke deleteAnalysisLoop hook for all passes that implement simple analysis interface.
A Use represents the edge between a Value definition and its users.
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Encapsulates MemorySSA, including all data associated with memory accesses.
void applyUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT)
Apply CFG updates, analogous with the DT edge updates.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
void setName(const Twine &Name)
Change the name of the value.
BlockT * getHeader() const
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Type * getType() const
All values are typed, get the type of this value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
This header provides classes for managing per-loop analyses.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
void takeName(Value *V)
Transfer the name from V to this value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void insertEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge insertion and update the tree.
static SmallPtrSet< const BasicBlock *, 16 > recomputeLoopBlockSet(Loop &L, LoopInfo &LI)
Recompute the set of blocks in a loop after unswitching.
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
iterator find(const_arg_type_t< KeyT > Val)
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
initializer< Ty > init(const Ty &Val)
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
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.
A set of analyses that are preserved following a run of a transformation pass.
static constexpr UpdateKind Insert
static TinyPtrVector< Value * > collectHomogenousInstGraphLoopInvariants(Loop &L, Instruction &Root, LoopInfo &LI)
Collect all of the loop invariant input values transitively used by the homogeneous instruction graph...
LLVM Basic Block Representation.
void push_back(EltTy NewVal)
Conditional or Unconditional Branch instruction.
size_t size() const
size - Get the array size.
This is an important base class in LLVM.
LLVM_NODISCARD bool empty() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", "Simple unswitch loops", false, false) INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass
const Instruction & front() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
BasicBlock * getDefaultDest() const
void moveAllAfterSpliceBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was spliced into From and To.
Represent the analysis usage information of a pass.
void splice(iterator where, iplist_impl &L2)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
static Loop * cloneLoopNest(Loop &OrigRootL, Loop *RootParentL, const ValueToValueMapTy &VMap, LoopInfo &LI)
Recursively clone the specified loop and all of its children.
const T * getPointer() const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
bool pred_empty(const BasicBlock *BB)
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...
CaseIt removeCase(CaseIt I)
This method removes the specified case and its successor from the switch instruction.
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...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
static BranchInst * turnGuardIntoBranch(IntrinsicInst *GI, Loop &L, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU)
Turns a llvm.experimental.guard intrinsic into implicit control flow branch, making the following rep...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static void buildPartialUnswitchConditionalBranch(BasicBlock &BB, ArrayRef< Value *> Invariants, bool Direction, BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc)
Insert code to test a set of loop invariant values, and conditionally branch on them.
void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
TargetTransformInfo & TTI
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
void sort(IteratorTy Start, IteratorTy End)
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
const InstListType & getInstList() const
Return the underlying instruction list container.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
A SetVector that performs no allocations if smaller than a certain size.
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.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
void verifyMemorySSA() const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
static void replaceLoopInvariantUses(Loop &L, Value *Invariant, Constant &Replacement)
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=nullptr)
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."))
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB, BasicBlock &OldExitingBB, BasicBlock &OldPH)
Rewrite the PHI nodes in an unswitched loop exit basic block.
LoopT * removeLoop(iterator I)
This removes the specified top-level loop from this loop info object.
LoopT * AllocateLoop(ArgsTy &&... Args)
LLVM_NODISCARD T pop_back_val()
simple loop Simple unswitch loops
Pass * createSimpleLoopUnswitchLegacyPass(bool NonTrivial=false)
Create the legacy pass object for the simple loop unswitcher.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
bool isConditional() const
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
void markLoopAsDeleted(Loop &L)
static void deleteDeadClonedBlocks(Loop &L, ArrayRef< BasicBlock *> ExitBlocks, ArrayRef< std::unique_ptr< ValueToValueMapTy >> VMaps, DominatorTree &DT, MemorySSAUpdater *MSSAU)
static ConstantInt * getTrue(LLVMContext &Context)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
bool isGuard(const User *U)
Returns true iff U has semantics of a guard.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, DebugInfoFinder *DIFinder=nullptr)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM...
static void hoistLoopToNewParent(Loop &L, BasicBlock &Preheader, DominatorTree &DT, LoopInfo &LI)
Hoist the current loop up to the innermost loop containing a remaining exit.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
If this flag is set, the remapper ignores missing function-local entries (Argument, Instruction, BasicBlock) that are not in the value map.
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
static bool unswitchBestCondition(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, TargetTransformInfo &TTI, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
LoopT * getParentLoop() const
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
void registerAssumption(CallInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
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...
void emplace_back(ArgTypes &&... Args)
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
static cl::opt< int > UnswitchSiblingsToplevelDiv("unswitch-siblings-toplevel-div", cl::init(2), cl::Hidden, cl::desc("Toplevel siblings divisor for cost multiplier."))
LLVM_NODISCARD bool empty() const
StringRef getName() const
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
iterator_range< value_op_iterator > operand_values()
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
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.
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...
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
static bool unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, TargetTransformInfo &TTI, bool NonTrivial, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
Unswitch control flow predicated on loop invariant conditions.
void setDefaultDest(BasicBlock *DefaultCase)
succ_range successors(Instruction *I)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
The legacy pass manager's analysis pass to compute loop information.
void getUniqueExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all unique successor blocks of this loop.
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
static int calculateUnswitchCostMultiplier(Instruction &TI, Loop &L, LoopInfo &LI, DominatorTree &DT, ArrayRef< std::pair< Instruction *, TinyPtrVector< Value *>>> UnswitchCandidates)
Cost multiplier is a way to limit potentially exponential behavior of loop-unswitch.
A container for analyses that lazily runs them and caches their results.
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."))
Legacy analysis pass which computes a DominatorTree.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
iterator_range< block_iterator > blocks() const
static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef< BasicBlock *> ExitBlocks, LoopInfo &LI, SmallVectorImpl< Loop *> &HoistedLoops)
Rebuild a loop after unswitching removes some subset of blocks and edges.
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
void dropAllReferences()
Cause all subinstructions to "let go" of all the references that said subinstructions are maintaining...
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
void visitDomSubTree(DominatorTree &DT, BasicBlock *BB, CallableT Callable)
Helper to visit a dominator subtree, invoking a callable on each node.
static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU)
Unswitch a trivial branch if the condition is loop invariant.
void forgetTopmostLoop(const Loop *L)
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.