[llvm] 231e63d - [StructurizeCFG] Refactor insertConditions. NFC. (#115476)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 01:40:36 PST 2024


Author: Jay Foad
Date: 2024-11-26T09:40:33Z
New Revision: 231e63d8162a1c78a973c6e546bea39d04fefd67

URL: https://github.com/llvm/llvm-project/commit/231e63d8162a1c78a973c6e546bea39d04fefd67
DIFF: https://github.com/llvm/llvm-project/commit/231e63d8162a1c78a973c6e546bea39d04fefd67.diff

LOG: [StructurizeCFG] Refactor insertConditions. NFC. (#115476)

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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/StructurizeCFG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
index 56084bb9c73806..01090b54e5afa3 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -618,28 +618,25 @@ void StructurizeCFG::insertConditions(bool Loops) {
     BasicBlock *SuccTrue = Term->getSuccessor(0);
     BasicBlock *SuccFalse = Term->getSuccessor(1);
 
-    PhiInserter.Initialize(Boolean, "");
-    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 &PI = Preds.begin()->second;
+      Term->setCondition(PI.Pred);
+      CondBranchWeights::setMetadata(*Term, PI.Weights);
+    } else {
+      PhiInserter.Initialize(Boolean, "");
+      PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
 
-    PredInfo ParentInfo{nullptr, std::nullopt};
-    for (auto [BB, PI] : Preds) {
-      if (BB == Parent) {
-        ParentInfo = PI;
-        break;
+      NearestCommonDominator Dominator(DT);
+      Dominator.addBlock(Parent);
+
+      for (auto [BB, PI] : Preds) {
+        assert(BB != Parent);
+        PhiInserter.AddAvailableValue(BB, PI.Pred);
+        Dominator.addAndRememberBlock(BB);
       }
-      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);
 


        


More information about the llvm-commits mailing list