diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp index 2a0f0423bbc8f..67f9add90c8d2 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; +struct PredInfo { + Value *Pred; + MaybeCondBranchWeights Weights; +}; -using BBPredicates = DenseMap; +using BBPredicates = DenseMap; using PredMap = DenseMap; using BB2BBMap = DenseMap; @@ -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 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 InstToErase; for (auto &I : concat(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(Inverted)) { InvertedCmp->setPredicate(InvertedCmp->getInversePredicate()); - Cond->replaceAllUsesWith(InvertedCmp); - InstToErase.push_back(cast(Cond)); + PI.Pred->replaceAllUsesWith(InvertedCmp); + InstToErase.push_back(cast(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 Pred) { - return DT->dominates(BB, Pred.first); - }); + return llvm::all_of(Preds, [&](std::pair 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 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()))