Skip to content

Commit 107af4a

Browse files
authored
[StructurizeCFG] Introduce struct PredInfo. NFC. (#115457)
This just provides a neater encapsulation of the info about the predicate for an edge, rather than ValueWeightPair aka std::pair.
1 parent d5677b6 commit 107af4a

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

llvm/lib/Transforms/Scalar/StructurizeCFG.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@ class CondBranchWeights {
120120
}
121121
};
122122

123-
using ValueWeightPair = std::pair<Value *, MaybeCondBranchWeights>;
123+
struct PredInfo {
124+
Value *Pred;
125+
MaybeCondBranchWeights Weights;
126+
};
124127

125-
using BBPredicates = DenseMap<BasicBlock *, ValueWeightPair>;
128+
using BBPredicates = DenseMap<BasicBlock *, PredInfo>;
126129
using PredMap = DenseMap<BasicBlock *, BBPredicates>;
127130
using BB2BBMap = DenseMap<BasicBlock *, BasicBlock *>;
128131

@@ -308,7 +311,7 @@ class StructurizeCFG {
308311

309312
void analyzeLoops(RegionNode *N);
310313

311-
ValueWeightPair buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
314+
PredInfo buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
312315

313316
void gatherPredicates(RegionNode *N);
314317

@@ -490,8 +493,8 @@ void StructurizeCFG::analyzeLoops(RegionNode *N) {
490493
}
491494

492495
/// Build the condition for one edge
493-
ValueWeightPair StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
494-
bool Invert) {
496+
PredInfo StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
497+
bool Invert) {
495498
Value *Cond = Invert ? BoolFalse : BoolTrue;
496499
MaybeCondBranchWeights Weights;
497500

@@ -624,24 +627,19 @@ void StructurizeCFG::insertConditions(bool Loops) {
624627
NearestCommonDominator Dominator(DT);
625628
Dominator.addBlock(Parent);
626629

627-
Value *ParentValue = nullptr;
628-
MaybeCondBranchWeights ParentWeights = std::nullopt;
629-
for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
630-
BasicBlock *BB = BBAndPred.first;
631-
auto [Pred, Weight] = BBAndPred.second;
632-
630+
PredInfo ParentInfo{nullptr, std::nullopt};
631+
for (auto [BB, PI] : Preds) {
633632
if (BB == Parent) {
634-
ParentValue = Pred;
635-
ParentWeights = Weight;
633+
ParentInfo = PI;
636634
break;
637635
}
638-
PhiInserter.AddAvailableValue(BB, Pred);
636+
PhiInserter.AddAvailableValue(BB, PI.Pred);
639637
Dominator.addAndRememberBlock(BB);
640638
}
641639

642-
if (ParentValue) {
643-
Term->setCondition(ParentValue);
644-
CondBranchWeights::setMetadata(*Term, ParentWeights);
640+
if (ParentInfo.Pred) {
641+
Term->setCondition(ParentInfo.Pred);
642+
CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
645643
} else {
646644
if (!Dominator.resultIsRememberedBlock())
647645
PhiInserter.AddAvailableValue(Dominator.result(), Default);
@@ -656,15 +654,14 @@ void StructurizeCFG::simplifyConditions() {
656654
SmallVector<Instruction *> InstToErase;
657655
for (auto &I : concat<PredMap::value_type>(Predicates, LoopPreds)) {
658656
auto &Preds = I.second;
659-
for (auto &J : Preds) {
660-
Value *Cond = J.second.first;
657+
for (auto [BB, PI] : Preds) {
661658
Instruction *Inverted;
662-
if (match(Cond, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
663-
!Cond->use_empty()) {
659+
if (match(PI.Pred, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
660+
!PI.Pred->use_empty()) {
664661
if (auto *InvertedCmp = dyn_cast<CmpInst>(Inverted)) {
665662
InvertedCmp->setPredicate(InvertedCmp->getInversePredicate());
666-
Cond->replaceAllUsesWith(InvertedCmp);
667-
InstToErase.push_back(cast<Instruction>(Cond));
663+
PI.Pred->replaceAllUsesWith(InvertedCmp);
664+
InstToErase.push_back(cast<Instruction>(PI.Pred));
668665
}
669666
}
670667
}
@@ -1046,10 +1043,9 @@ void StructurizeCFG::setPrevNode(BasicBlock *BB) {
10461043
/// Does BB dominate all the predicates of Node?
10471044
bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
10481045
BBPredicates &Preds = Predicates[Node->getEntry()];
1049-
return llvm::all_of(Preds,
1050-
[&](std::pair<BasicBlock *, ValueWeightPair> Pred) {
1051-
return DT->dominates(BB, Pred.first);
1052-
});
1046+
return llvm::all_of(Preds, [&](std::pair<BasicBlock *, PredInfo> Pred) {
1047+
return DT->dominates(BB, Pred.first);
1048+
});
10531049
}
10541050

10551051
/// Can we predict that this node will always be called?
@@ -1061,11 +1057,8 @@ bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
10611057
if (!PrevNode)
10621058
return true;
10631059

1064-
for (std::pair<BasicBlock *, ValueWeightPair> Pred : Preds) {
1065-
BasicBlock *BB = Pred.first;
1066-
Value *V = Pred.second.first;
1067-
1068-
if (V != BoolTrue)
1060+
for (auto [BB, PI] : Preds) {
1061+
if (PI.Pred != BoolTrue)
10691062
return false;
10701063

10711064
if (!Dominated && DT->dominates(BB, PrevNode->getEntry()))

0 commit comments

Comments
 (0)