[llvm] [StructurizeCFG] Introduce struct PredInfo. NFC. (PR #115457)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 02:22:45 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Jay Foad (jayfoad)

<details>
<summary>Changes</summary>

This just provides a neater encapsulation of the info about the
predicate for an edge, rather than ValueWeightPair aka std::pair.


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


1 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/StructurizeCFG.cpp (+25-32) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
index 2a0f0423bbc8f6..67f9add90c8d26 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -120,9 +120,12 @@ class CondBranchWeights {
   }
 };
 
-using ValueWeightPair = std::pair<Value *, MaybeCondBranchWeights>;
+struct PredInfo {
+  Value *Pred;
+  MaybeCondBranchWeights Weights;
+};
 
-using BBPredicates = DenseMap<BasicBlock *, ValueWeightPair>;
+using BBPredicates = DenseMap<BasicBlock *, PredInfo>;
 using PredMap = DenseMap<BasicBlock *, BBPredicates>;
 using BB2BBMap = DenseMap<BasicBlock *, BasicBlock *>;
 
@@ -308,7 +311,7 @@ class StructurizeCFG {
 
   void analyzeLoops(RegionNode *N);
 
-  ValueWeightPair buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
+  PredInfo buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
 
   void gatherPredicates(RegionNode *N);
 
@@ -490,8 +493,8 @@ void StructurizeCFG::analyzeLoops(RegionNode *N) {
 }
 
 /// Build the condition for one edge
-ValueWeightPair StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
-                                               bool Invert) {
+PredInfo StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
+                                        bool Invert) {
   Value *Cond = Invert ? BoolFalse : BoolTrue;
   MaybeCondBranchWeights Weights;
 
@@ -624,24 +627,19 @@ void StructurizeCFG::insertConditions(bool Loops) {
     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;
-
+    PredInfo ParentInfo{nullptr, std::nullopt};
+    for (auto [BB, PI] : Preds) {
       if (BB == Parent) {
-        ParentValue = Pred;
-        ParentWeights = Weight;
+        ParentInfo = PI;
         break;
       }
-      PhiInserter.AddAvailableValue(BB, Pred);
+      PhiInserter.AddAvailableValue(BB, PI.Pred);
       Dominator.addAndRememberBlock(BB);
     }
 
-    if (ParentValue) {
-      Term->setCondition(ParentValue);
-      CondBranchWeights::setMetadata(*Term, ParentWeights);
+    if (ParentInfo.Pred) {
+      Term->setCondition(ParentInfo.Pred);
+      CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
     } else {
       if (!Dominator.resultIsRememberedBlock())
         PhiInserter.AddAvailableValue(Dominator.result(), Default);
@@ -656,15 +654,14 @@ void StructurizeCFG::simplifyConditions() {
   SmallVector<Instruction *> InstToErase;
   for (auto &I : concat<PredMap::value_type>(Predicates, LoopPreds)) {
     auto &Preds = I.second;
-    for (auto &J : Preds) {
-      Value *Cond = J.second.first;
+    for (auto [BB, PI] : Preds) {
       Instruction *Inverted;
-      if (match(Cond, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
-          !Cond->use_empty()) {
+      if (match(PI.Pred, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
+          !PI.Pred->use_empty()) {
         if (auto *InvertedCmp = dyn_cast<CmpInst>(Inverted)) {
           InvertedCmp->setPredicate(InvertedCmp->getInversePredicate());
-          Cond->replaceAllUsesWith(InvertedCmp);
-          InstToErase.push_back(cast<Instruction>(Cond));
+          PI.Pred->replaceAllUsesWith(InvertedCmp);
+          InstToErase.push_back(cast<Instruction>(PI.Pred));
         }
       }
     }
@@ -1046,10 +1043,9 @@ void StructurizeCFG::setPrevNode(BasicBlock *BB) {
 /// Does BB dominate all the predicates of Node?
 bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
   BBPredicates &Preds = Predicates[Node->getEntry()];
-  return llvm::all_of(Preds,
-                      [&](std::pair<BasicBlock *, ValueWeightPair> Pred) {
-                        return DT->dominates(BB, Pred.first);
-                      });
+  return llvm::all_of(Preds, [&](std::pair<BasicBlock *, PredInfo> Pred) {
+    return DT->dominates(BB, Pred.first);
+  });
 }
 
 /// Can we predict that this node will always be called?
@@ -1061,11 +1057,8 @@ bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
   if (!PrevNode)
     return true;
 
-  for (std::pair<BasicBlock *, ValueWeightPair> Pred : Preds) {
-    BasicBlock *BB = Pred.first;
-    Value *V = Pred.second.first;
-
-    if (V != BoolTrue)
+  for (auto [BB, PI] : Preds) {
+    if (PI.Pred != BoolTrue)
       return false;
 
     if (!Dominated && DT->dominates(BB, PrevNode->getEntry()))

``````````

</details>


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


More information about the llvm-commits mailing list