@@ -120,9 +120,12 @@ class CondBranchWeights {
120
120
}
121
121
};
122
122
123
- using ValueWeightPair = std::pair<Value *, MaybeCondBranchWeights>;
123
+ struct PredInfo {
124
+ Value *Pred;
125
+ MaybeCondBranchWeights Weights;
126
+ };
124
127
125
- using BBPredicates = DenseMap<BasicBlock *, ValueWeightPair >;
128
+ using BBPredicates = DenseMap<BasicBlock *, PredInfo >;
126
129
using PredMap = DenseMap<BasicBlock *, BBPredicates>;
127
130
using BB2BBMap = DenseMap<BasicBlock *, BasicBlock *>;
128
131
@@ -308,7 +311,7 @@ class StructurizeCFG {
308
311
309
312
void analyzeLoops (RegionNode *N);
310
313
311
- ValueWeightPair buildCondition (BranchInst *Term, unsigned Idx, bool Invert);
314
+ PredInfo buildCondition (BranchInst *Term, unsigned Idx, bool Invert);
312
315
313
316
void gatherPredicates (RegionNode *N);
314
317
@@ -490,8 +493,8 @@ void StructurizeCFG::analyzeLoops(RegionNode *N) {
490
493
}
491
494
492
495
// / 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) {
495
498
Value *Cond = Invert ? BoolFalse : BoolTrue;
496
499
MaybeCondBranchWeights Weights;
497
500
@@ -624,24 +627,19 @@ void StructurizeCFG::insertConditions(bool Loops) {
624
627
NearestCommonDominator Dominator (DT);
625
628
Dominator.addBlock (Parent);
626
629
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) {
633
632
if (BB == Parent) {
634
- ParentValue = Pred;
635
- ParentWeights = Weight;
633
+ ParentInfo = PI;
636
634
break ;
637
635
}
638
- PhiInserter.AddAvailableValue (BB, Pred);
636
+ PhiInserter.AddAvailableValue (BB, PI. Pred );
639
637
Dominator.addAndRememberBlock (BB);
640
638
}
641
639
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 );
645
643
} else {
646
644
if (!Dominator.resultIsRememberedBlock ())
647
645
PhiInserter.AddAvailableValue (Dominator.result (), Default);
@@ -656,15 +654,14 @@ void StructurizeCFG::simplifyConditions() {
656
654
SmallVector<Instruction *> InstToErase;
657
655
for (auto &I : concat<PredMap::value_type>(Predicates, LoopPreds)) {
658
656
auto &Preds = I.second ;
659
- for (auto &J : Preds) {
660
- Value *Cond = J.second .first ;
657
+ for (auto [BB, PI] : Preds) {
661
658
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 ()) {
664
661
if (auto *InvertedCmp = dyn_cast<CmpInst>(Inverted)) {
665
662
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 ));
668
665
}
669
666
}
670
667
}
@@ -1046,10 +1043,9 @@ void StructurizeCFG::setPrevNode(BasicBlock *BB) {
1046
1043
// / Does BB dominate all the predicates of Node?
1047
1044
bool StructurizeCFG::dominatesPredicates (BasicBlock *BB, RegionNode *Node) {
1048
1045
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
+ });
1053
1049
}
1054
1050
1055
1051
// / Can we predict that this node will always be called?
@@ -1061,11 +1057,8 @@ bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
1061
1057
if (!PrevNode)
1062
1058
return true ;
1063
1059
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)
1069
1062
return false ;
1070
1063
1071
1064
if (!Dominated && DT->dominates (BB, PrevNode->getEntry ()))
0 commit comments