Skip to content

Commit b482560

Browse files
committed
[NFC][SimplifyCFG] FoldBranchToCommonDest(): extract check for destination sharing into a helper function
As a follow-up, i'll extract the actual transform into a function, and this helper will be called from both places, so this avoids code duplication.
1 parent 7b89efb commit b482560

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,6 +2766,27 @@ static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
27662766
}
27672767
}
27682768

2769+
// Determine if the two branches share a common destination,
2770+
// and deduce a glue that we need to use to join branch's conditions
2771+
// to arrive at the common destination.
2772+
static Optional<std::pair<Instruction::BinaryOps, bool>>
2773+
CheckIfCondBranchesShareCommonDestination(BranchInst *BI, BranchInst *PBI) {
2774+
assert(BI && PBI && BI->isConditional() && PBI->isConditional() &&
2775+
"Both blocks must end with a conditional branches.");
2776+
assert(is_contained(predecessors(BI->getParent()), PBI->getParent()) &&
2777+
"PredBB must be a predecessor of BB.");
2778+
2779+
if (PBI->getSuccessor(0) == BI->getSuccessor(0))
2780+
return {{Instruction::Or, false}};
2781+
else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
2782+
return {{Instruction::And, false}};
2783+
else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
2784+
return {{Instruction::And, true}};
2785+
else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
2786+
return {{Instruction::Or, true}};
2787+
return None;
2788+
}
2789+
27692790
/// If this basic block is simple enough, and if a predecessor branches to us
27702791
/// and one of our successors, fold the block into the predecessor and use
27712792
/// logical operations to pick the right destination.
@@ -2868,22 +2889,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
28682889
continue;
28692890

28702891
// Determine if the two branches share a common destination.
2871-
Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
2872-
bool InvertPredCond = false;
2873-
2874-
if (PBI->getSuccessor(0) == TrueDest) {
2875-
Opc = Instruction::Or;
2876-
} else if (PBI->getSuccessor(1) == FalseDest) {
2877-
Opc = Instruction::And;
2878-
} else if (PBI->getSuccessor(0) == FalseDest) {
2879-
Opc = Instruction::And;
2880-
InvertPredCond = true;
2881-
} else if (PBI->getSuccessor(1) == TrueDest) {
2882-
Opc = Instruction::Or;
2883-
InvertPredCond = true;
2884-
} else {
2892+
Instruction::BinaryOps Opc;
2893+
bool InvertPredCond;
2894+
if (auto Recepie = CheckIfCondBranchesShareCommonDestination(BI, PBI))
2895+
std::tie(Opc, InvertPredCond) = *Recepie;
2896+
else
28852897
continue;
2886-
}
28872898

28882899
// Check the cost of inserting the necessary logic before performing the
28892900
// transformation.

0 commit comments

Comments
 (0)