diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 6cdd18fe9041c..a1bc89e7d8096 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3521,7 +3521,7 @@ static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI, /// Determine if the two branches share a common destination and deduce a glue /// that joins the branches' conditions to arrive at the common destination if /// that would be profitable. -static std::optional> +static std::optional> shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI, const TargetTransformInfo *TTI) { assert(BI && PBI && BI->isConditional() && PBI->isConditional() && @@ -3544,19 +3544,19 @@ shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI, if (PBI->getSuccessor(0) == BI->getSuccessor(0)) { // Speculate the 2nd condition unless the 1st is probably true. if (PBITrueProb.isUnknown() || PBITrueProb < Likely) - return {{Instruction::Or, false}}; + return {{BI->getSuccessor(0), Instruction::Or, false}}; } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) { // Speculate the 2nd condition unless the 1st is probably false. if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely) - return {{Instruction::And, false}}; + return {{BI->getSuccessor(1), Instruction::And, false}}; } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) { // Speculate the 2nd condition unless the 1st is probably true. if (PBITrueProb.isUnknown() || PBITrueProb < Likely) - return {{Instruction::And, true}}; + return {{BI->getSuccessor(1), Instruction::And, true}}; } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) { // Speculate the 2nd condition unless the 1st is probably false. if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely) - return {{Instruction::Or, true}}; + return {{BI->getSuccessor(0), Instruction::Or, true}}; } return std::nullopt; } @@ -3569,9 +3569,10 @@ static bool performBranchToCommonDestFolding(BranchInst *BI, BranchInst *PBI, BasicBlock *PredBlock = PBI->getParent(); // Determine if the two branches share a common destination. + BasicBlock *CommonSucc; Instruction::BinaryOps Opc; bool InvertPredCond; - std::tie(Opc, InvertPredCond) = + std::tie(CommonSucc, Opc, InvertPredCond) = *shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI); LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB); @@ -3730,10 +3731,11 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU, continue; // Determine if the two branches share a common destination. + BasicBlock *CommonSucc; Instruction::BinaryOps Opc; bool InvertPredCond; if (auto Recipe = shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI)) - std::tie(Opc, InvertPredCond) = *Recipe; + std::tie(CommonSucc, Opc, InvertPredCond) = *Recipe; else continue;