-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This just provides a neater encapsulation of the info about the predicate for an edge, rather than ValueWeightPair aka std::pair.
@llvm/pr-subscribers-llvm-transforms Author: Jay Foad (jayfoad) ChangesThis just provides a neater encapsulation of the info about the Full diff: https://github.com/llvm/llvm-project/pull/115457.diff 1 Files Affected:
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()))
|
There was a problem hiding this 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>;
.
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.