Skip to content

[StructurizeCFG] Introduce struct PredInfo. NFC. #115457

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
Nov 8, 2024

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Nov 8, 2024

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

This just provides a neater encapsulation of the info about the
predicate for an edge, rather than ValueWeightPair aka std::pair.
@jayfoad jayfoad requested review from arsenm and jmmartinez and removed request for arsenm November 8, 2024 10:22
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Jay Foad (jayfoad)

Changes

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

@jayfoad jayfoad requested a review from ruiling November 8, 2024 10:22
Copy link
Contributor

@jmmartinez jmmartinez left a comment

Choose a reason for hiding this comment

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

Thanks ! I've should have done this in the first place instead of using using ValueWeightPair = std::pair<Value *, MaybeCondBranchWeights>;.

@jayfoad jayfoad merged commit 107af4a into llvm:main Nov 8, 2024
10 checks passed
@jayfoad jayfoad deleted the structurize-predinfo branch November 8, 2024 14:26
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
This just provides a neater encapsulation of the info about the
predicate for an edge, rather than ValueWeightPair aka std::pair.
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.

3 participants