[llvm] [StructurizeCFG] Refactor insertConditions. NFC. (PR #115476)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 05:05:26 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Jay Foad (jayfoad)

<details>
<summary>Changes</summary>

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
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);
 

``````````

</details>


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


More information about the llvm-commits mailing list