Skip to content

Commit 994ddbb

Browse files
committed
[StructurizeCFG] Refactor insertConditions. NFC.
This just makes it more obvious that having Parent as the single predecessor is a special case, instead of checking for it in the middle of a loop that finds the nearest common dominator of multiple predecessors.
1 parent d74127e commit 994ddbb

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

llvm/lib/Transforms/Scalar/StructurizeCFG.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -615,34 +615,29 @@ void StructurizeCFG::insertConditions(bool Loops) {
615615
BasicBlock *SuccTrue = Term->getSuccessor(0);
616616
BasicBlock *SuccFalse = Term->getSuccessor(1);
617617

618-
PhiInserter.Initialize(Boolean, "");
619-
PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
620-
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
621-
622618
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
623619

624-
NearestCommonDominator Dominator(DT);
625-
Dominator.addBlock(Parent);
620+
if (Preds.size() == 1 && Preds.begin()->first == Parent) {
621+
auto [Pred, Weight] = Preds.begin()->second;
622+
Term->setCondition(Pred);
623+
CondBranchWeights::setMetadata(*Term, Weight);
624+
} else {
625+
PhiInserter.Initialize(Boolean, "");
626+
PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
627+
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
628+
629+
NearestCommonDominator Dominator(DT);
630+
Dominator.addBlock(Parent);
626631

627-
Value *ParentValue = nullptr;
628-
MaybeCondBranchWeights ParentWeights = std::nullopt;
629-
for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
630-
BasicBlock *BB = BBAndPred.first;
631-
auto [Pred, Weight] = BBAndPred.second;
632+
for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
633+
BasicBlock *BB = BBAndPred.first;
634+
auto [Pred, Weight] = BBAndPred.second;
632635

633-
if (BB == Parent) {
634-
ParentValue = Pred;
635-
ParentWeights = Weight;
636-
break;
636+
assert(BB != Parent);
637+
PhiInserter.AddAvailableValue(BB, Pred);
638+
Dominator.addAndRememberBlock(BB);
637639
}
638-
PhiInserter.AddAvailableValue(BB, Pred);
639-
Dominator.addAndRememberBlock(BB);
640-
}
641640

642-
if (ParentValue) {
643-
Term->setCondition(ParentValue);
644-
CondBranchWeights::setMetadata(*Term, ParentWeights);
645-
} else {
646641
if (!Dominator.resultIsRememberedBlock())
647642
PhiInserter.AddAvailableValue(Dominator.result(), Default);
648643

0 commit comments

Comments
 (0)