Skip to content

Commit

Permalink
[NFC][SimplifyCFG] shouldFoldCondBranchesToCommonDestination(): rep…
Browse files Browse the repository at this point in the history
…ort the common succ
  • Loading branch information
LebedevRI committed Dec 4, 2022
1 parent 0ca43d4 commit 92bf8f3
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Expand Up @@ -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<std::pair<Instruction::BinaryOps, bool>>
static std::optional<std::tuple<BasicBlock *, Instruction::BinaryOps, bool>>
shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI,
const TargetTransformInfo *TTI) {
assert(BI && PBI && BI->isConditional() && PBI->isConditional() &&
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 92bf8f3

Please sign in to comment.