Skip to content

Commit 7bf6313

Browse files
authored
Revert "[StructurizeCFG] Refactor insertConditions. NFC. (llvm#115476)" (llvm#136370) (llvm#3176)
2 parents 025424f + ee87619 commit 7bf6313

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

llvm/lib/Transforms/Scalar/StructurizeCFG.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,25 +614,28 @@ void StructurizeCFG::insertConditions(bool Loops) {
614614
BasicBlock *SuccTrue = Term->getSuccessor(0);
615615
BasicBlock *SuccFalse = Term->getSuccessor(1);
616616

617-
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
617+
PhiInserter.Initialize(Boolean, "");
618+
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
618619

619-
if (Preds.size() == 1 && Preds.begin()->first == Parent) {
620-
auto &PI = Preds.begin()->second;
621-
Term->setCondition(PI.Pred);
622-
CondBranchWeights::setMetadata(*Term, PI.Weights);
623-
} else {
624-
PhiInserter.Initialize(Boolean, "");
625-
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
620+
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
626621

627-
NearestCommonDominator Dominator(DT);
628-
Dominator.addBlock(Parent);
622+
NearestCommonDominator Dominator(DT);
623+
Dominator.addBlock(Parent);
629624

630-
for (auto [BB, PI] : Preds) {
631-
assert(BB != Parent);
632-
PhiInserter.AddAvailableValue(BB, PI.Pred);
633-
Dominator.addAndRememberBlock(BB);
625+
PredInfo ParentInfo{nullptr, std::nullopt};
626+
for (auto [BB, PI] : Preds) {
627+
if (BB == Parent) {
628+
ParentInfo = PI;
629+
break;
634630
}
631+
PhiInserter.AddAvailableValue(BB, PI.Pred);
632+
Dominator.addAndRememberBlock(BB);
633+
}
635634

635+
if (ParentInfo.Pred) {
636+
Term->setCondition(ParentInfo.Pred);
637+
CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
638+
} else {
636639
if (!Dominator.resultIsRememberedBlock())
637640
PhiInserter.AddAvailableValue(Dominator.result(), Default);
638641

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=structurizecfg %s -o - | FileCheck %s
3+
4+
; Issue tracking: https://github.com/llvm/llvm-project/issues/126534.
5+
6+
define void @foo() {
7+
; CHECK-LABEL: define void @foo() {
8+
; CHECK-NEXT: [[ENTRY:.*:]]
9+
; CHECK-NEXT: br label %[[COND_FALSE:.*]]
10+
; CHECK: [[COND_TRUE:.*]]:
11+
; CHECK-NEXT: br label %[[COND_END:.*]]
12+
; CHECK: [[COND_FALSE]]:
13+
; CHECK-NEXT: br i1 false, label %[[COND_TRUE]], label %[[COND_END]]
14+
; CHECK: [[COND_END]]:
15+
; CHECK-NEXT: ret void
16+
;
17+
entry:
18+
br i1 false, label %cond.true, label %cond.false
19+
20+
cond.true: ; preds = %entry
21+
br label %cond.end
22+
23+
cond.false: ; preds = %entry
24+
br label %cond.end
25+
26+
cond.end: ; preds = %cond.false, %cond.true
27+
ret void
28+
}

0 commit comments

Comments
 (0)