@@ -2766,6 +2766,27 @@ static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
2766
2766
}
2767
2767
}
2768
2768
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
+
2769
2790
// / If this basic block is simple enough, and if a predecessor branches to us
2770
2791
// / and one of our successors, fold the block into the predecessor and use
2771
2792
// / logical operations to pick the right destination.
@@ -2868,22 +2889,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2868
2889
continue ;
2869
2890
2870
2891
// 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
2885
2897
continue ;
2886
- }
2887
2898
2888
2899
// Check the cost of inserting the necessary logic before performing the
2889
2900
// transformation.
0 commit comments