Skip to content

Revert "[StructurizeCFG] Refactor insertConditions. NFC. (#115476)" #136370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,28 @@ void StructurizeCFG::insertConditions(bool Loops) {
BasicBlock *SuccTrue = Term->getSuccessor(0);
BasicBlock *SuccFalse = Term->getSuccessor(1);

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

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

NearestCommonDominator Dominator(DT);
Dominator.addBlock(Parent);
NearestCommonDominator Dominator(DT);
Dominator.addBlock(Parent);

for (auto [BB, PI] : Preds) {
assert(BB != Parent);
PhiInserter.AddAvailableValue(BB, PI.Pred);
Dominator.addAndRememberBlock(BB);
PredInfo ParentInfo{nullptr, std::nullopt};
for (auto [BB, PI] : Preds) {
if (BB == Parent) {
ParentInfo = PI;
break;
}
PhiInserter.AddAvailableValue(BB, PI.Pred);
Dominator.addAndRememberBlock(BB);
}

if (ParentInfo.Pred) {
Term->setCondition(ParentInfo.Pred);
CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
} else {
if (!Dominator.resultIsRememberedBlock())
PhiInserter.AddAvailableValue(Dominator.result(), Default);

Expand Down
16 changes: 12 additions & 4 deletions llvm/test/Transforms/StructurizeCFG/simple-structurizecfg-crash.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
; RUN: opt -S -passes=structurizecfg %s -o -
; REQUIRES: asserts
; XFAIL: *
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=structurizecfg %s -o - | FileCheck %s

; Issue tracking: https://github.com/llvm/llvm-project/issues/126534.
; FIXME: This test is expected to crash. Generate checklines after the crash is fixed.

define void @foo() {
; CHECK-LABEL: define void @foo() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[COND_FALSE:.*]]
; CHECK: [[COND_TRUE:.*]]:
; CHECK-NEXT: br label %[[COND_END:.*]]
; CHECK: [[COND_FALSE]]:
; CHECK-NEXT: br i1 false, label %[[COND_TRUE]], label %[[COND_END]]
; CHECK: [[COND_END]]:
; CHECK-NEXT: ret void
;
entry:
br i1 false, label %cond.true, label %cond.false

Expand Down
Loading