Skip to content

[StructurizeCFG] Refactor insertConditions. NFC. #115476

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 3 commits into from
Nov 26, 2024

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Nov 8, 2024

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Jay Foad (jayfoad)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/115476.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/StructurizeCFG.cpp (+17-22)
diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
index 2a0f0423bbc8f6..1cc1bb6a4e04f2 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -615,34 +615,29 @@ void StructurizeCFG::insertConditions(bool Loops) {
     BasicBlock *SuccTrue = Term->getSuccessor(0);
     BasicBlock *SuccFalse = Term->getSuccessor(1);
 
-    PhiInserter.Initialize(Boolean, "");
-    PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
-    PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
-
     BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
 
-    NearestCommonDominator Dominator(DT);
-    Dominator.addBlock(Parent);
+    if (Preds.size() == 1 && Preds.begin()->first == Parent) {
+      auto [Pred, Weight] = Preds.begin()->second;
+      Term->setCondition(Pred);
+      CondBranchWeights::setMetadata(*Term, Weight);
+    } else {
+      PhiInserter.Initialize(Boolean, "");
+      PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
+      PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
+
+      NearestCommonDominator Dominator(DT);
+      Dominator.addBlock(Parent);
 
-    Value *ParentValue = nullptr;
-    MaybeCondBranchWeights ParentWeights = std::nullopt;
-    for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
-      BasicBlock *BB = BBAndPred.first;
-      auto [Pred, Weight] = BBAndPred.second;
+      for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
+        BasicBlock *BB = BBAndPred.first;
+        auto [Pred, Weight] = BBAndPred.second;
 
-      if (BB == Parent) {
-        ParentValue = Pred;
-        ParentWeights = Weight;
-        break;
+        assert(BB != Parent);
+        PhiInserter.AddAvailableValue(BB, Pred);
+        Dominator.addAndRememberBlock(BB);
       }
-      PhiInserter.AddAvailableValue(BB, Pred);
-      Dominator.addAndRememberBlock(BB);
-    }
 
-    if (ParentValue) {
-      Term->setCondition(ParentValue);
-      CondBranchWeights::setMetadata(*Term, ParentWeights);
-    } else {
       if (!Dominator.resultIsRememberedBlock())
         PhiInserter.AddAvailableValue(Dominator.result(), Default);
 

Copy link
Contributor

@ruiling ruiling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code is indeed confusing if the block has multiple predecessors. I think this is good to go?

@jayfoad jayfoad merged commit 231e63d into llvm:main Nov 26, 2024
6 of 8 checks passed
@jayfoad jayfoad deleted the structurizer-refactor-insertconditions branch November 26, 2024 09:40
shiltian added a commit that referenced this pull request Apr 18, 2025
This reverts commit `231e63d8162a1c78a973c6e546bea39d04fefd67` because it was
intended to be NFC, but it actually introduces a semantic change. The behavior
is not equivalent to the original code.

Specifically, the change attempts to make the special case of `Parent` being the
only predecessor more explicit by isolating it, and then asserts in the `else`
branch that `BB` can't be `Parent`. However, it's still possible for
`Preds.size() > 1`, in which case `BB` can be `Parent`, causing the assertion to
trigger.

Eventually, this introduced a regression (though the related test case wasn't in
the repo at the time). To fix it properly, we'd have to reintroduce logic in the
`else` block to handle the case where `BB` is `Parent`, which counters the
intent of the original refactor. Because of that, I think it's cleaner and more
reliable to simply revert the commit.

Fixes #126534.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants