@@ -2706,26 +2706,6 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
2706
2706
if (CE->canTrap ())
2707
2707
return false ;
2708
2708
2709
- // If BI is reached from the true path of PBI and PBI's condition implies
2710
- // BI's condition, we know the direction of the BI branch.
2711
- if ((PBI->getSuccessor (0 ) == BI->getParent () ||
2712
- PBI->getSuccessor (1 ) == BI->getParent ()) &&
2713
- PBI->getSuccessor (0 ) != PBI->getSuccessor (1 ) &&
2714
- BB->getSinglePredecessor ()) {
2715
- bool FalseDest = PBI->getSuccessor (1 ) == BI->getParent ();
2716
- Optional<bool > Implication = isImpliedCondition (
2717
- PBI->getCondition (), BI->getCondition (), DL, FalseDest);
2718
- if (Implication) {
2719
- // Turn this into a branch on constant.
2720
- auto *OldCond = BI->getCondition ();
2721
- ConstantInt *CI = *Implication ? ConstantInt::getTrue (BB->getContext ())
2722
- : ConstantInt::getFalse (BB->getContext ());
2723
- BI->setCondition (CI);
2724
- RecursivelyDeleteTriviallyDeadInstructions (OldCond);
2725
- return true ; // Nuke the branch on constant.
2726
- }
2727
- }
2728
-
2729
2709
// If both branches are conditional and both contain stores to the same
2730
2710
// address, remove the stores from the conditionals and create a conditional
2731
2711
// merged store at the end.
@@ -5149,6 +5129,30 @@ bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
5149
5129
if (SimplifyBranchOnICmpChain (BI, Builder, DL))
5150
5130
return true ;
5151
5131
5132
+ // If this basic block has a single dominating predecessor block and the
5133
+ // dominating block's condition implies BI's condition, we know the direction
5134
+ // of the BI branch.
5135
+ if (BasicBlock *Dom = BB->getSinglePredecessor ()) {
5136
+ auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator ());
5137
+ if (PBI && PBI->isConditional () &&
5138
+ PBI->getSuccessor (0 ) != PBI->getSuccessor (1 ) &&
5139
+ (PBI->getSuccessor (0 ) == BB || PBI->getSuccessor (1 ) == BB)) {
5140
+ bool CondIsFalse = PBI->getSuccessor (1 ) == BB;
5141
+ Optional<bool > Implication = isImpliedCondition (
5142
+ PBI->getCondition (), BI->getCondition (), DL, CondIsFalse);
5143
+ if (Implication) {
5144
+ // Turn this into a branch on constant.
5145
+ auto *OldCond = BI->getCondition ();
5146
+ ConstantInt *CI = *Implication
5147
+ ? ConstantInt::getTrue (BB->getContext ())
5148
+ : ConstantInt::getFalse (BB->getContext ());
5149
+ BI->setCondition (CI);
5150
+ RecursivelyDeleteTriviallyDeadInstructions (OldCond);
5151
+ return SimplifyCFG (BB, TTI, BonusInstThreshold, AC) | true ;
5152
+ }
5153
+ }
5154
+ }
5155
+
5152
5156
// If this basic block is ONLY a compare and a branch, and if a predecessor
5153
5157
// branches to us and one of our successors, fold the comparison into the
5154
5158
// predecessor and use logical operations to pick the right destination.
0 commit comments