@@ -2765,86 +2765,20 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
2765
2765
return EverChanged;
2766
2766
}
2767
2767
2768
- static bool SpeculativelyExecuteThenElseCode (BranchInst *BI,
2769
- const TargetTransformInfo &TTI,
2770
- DomTreeUpdater *DTU,
2771
- const DataLayout &DL) {
2772
- assert (BI->isConditional () && !isa<ConstantInt>(BI->getCondition ()) &&
2773
- BI->getSuccessor (0 ) != BI->getSuccessor (1 ) &&
2774
- " Only for truly conditional branches." );
2775
-
2776
- // Which ones of our successors end up with an unconditional branch?
2777
- SmallVector<BasicBlock *, 2 > UncondSuccessors;
2778
- SmallVector<BasicBlock *, 2 > OtherSuccessors;
2779
- for (BasicBlock *Succ : successors (BI)) {
2780
- auto *SuccBI = dyn_cast<BranchInst>(Succ->getTerminator ());
2781
- if (SuccBI && SuccBI->isUnconditional ())
2782
- UncondSuccessors.emplace_back (Succ);
2783
- else
2784
- OtherSuccessors.emplace_back (Succ);
2785
- }
2786
- assert (UncondSuccessors.size () + OtherSuccessors.size () == 2 &&
2787
- " Can not have more than two successors!" );
2788
-
2789
- // If none do, then we can't do anything.
2790
- if (UncondSuccessors.empty ())
2791
- return false ;
2792
-
2793
- // We want to hoist code from the unconditional block[s] and eliminate them,
2794
- // but if they have their address taken, then we essentially can't do this.
2795
- for (BasicBlock *UncondSucc : UncondSuccessors)
2796
- if (UncondSucc->hasAddressTaken ())
2797
- return false ;
2798
-
2799
- // All unconditional successors must have a single (and the same) predecessor.
2800
- // FIXME: lift this restriction.
2801
- for (BasicBlock *UncondSucc : UncondSuccessors)
2802
- if (!UncondSucc->getSinglePredecessor ())
2803
- return false ;
2804
-
2805
- // Now, what is the merge point?
2806
- BasicBlock *MergeBB = nullptr ;
2807
- // If there was only a single unconditional successor,
2808
- // then the other successor *must* be the merge point.
2809
- if (UncondSuccessors.size () == 1 )
2810
- MergeBB = OtherSuccessors.front ();
2811
-
2812
- // All unconditional successors must have the same successor themselves.
2813
- for (BasicBlock *UncondSucc : UncondSuccessors) {
2814
- auto *SuccBI = cast<BranchInst>(UncondSucc->getTerminator ());
2815
- assert (SuccBI->isUnconditional () && " Should be an unconditional branch." );
2816
- BasicBlock *SuccOfSucc = SuccBI->getSuccessor (0 );
2817
- if (!MergeBB) // First unconditional successor, record it's successor.
2818
- MergeBB = SuccOfSucc;
2819
- else if (SuccOfSucc != MergeBB) // Do all succs have the same successor?
2820
- return false ;
2821
- }
2822
-
2823
- assert (MergeBB && " Should have found the merge point." );
2824
- assert (all_of (UncondSuccessors,
2825
- [MergeBB](BasicBlock *UncondSucc) {
2826
- return is_contained (predecessors (MergeBB), UncondSucc);
2827
- }) &&
2828
- " All unconditional successors must be predecessors of merge block." );
2829
- assert ((UncondSuccessors.size () != 1 ||
2830
- is_contained (predecessors (MergeBB), BI->getParent ())) &&
2831
- " If there is only a single unconditional successor, then the dispatch "
2832
- " block must also be merge block's predecessor." );
2833
-
2834
- auto *PN = dyn_cast<PHINode>(MergeBB->begin ());
2835
- if (!PN || PN->getNumIncomingValues () != 2 )
2836
- return false ;
2837
-
2768
+ // / Given a BB that starts with the specified two-entry PHI node,
2769
+ // / see if we can eliminate it.
2770
+ static bool FoldTwoEntryPHINode (PHINode *PN, const TargetTransformInfo &TTI,
2771
+ DomTreeUpdater *DTU, const DataLayout &DL) {
2838
2772
// Ok, this is a two entry PHI node. Check to see if this is a simple "if
2839
2773
// statement", which has a very simple dominance structure. Basically, we
2840
2774
// are trying to find the condition that is being branched on, which
2841
2775
// subsequently causes this merge to happen. We really want control
2842
2776
// dependence information for this check, but simplifycfg can't keep it up
2843
2777
// to date, and this catches most of the cases we care about anyway.
2844
- MergeBB = PN->getParent ();
2778
+ BasicBlock *BB = PN->getParent ();
2845
2779
2846
2780
BasicBlock *IfTrue, *IfFalse;
2847
- BranchInst *DomBI = GetIfCondition (MergeBB , IfTrue, IfFalse);
2781
+ BranchInst *DomBI = GetIfCondition (BB , IfTrue, IfFalse);
2848
2782
if (!DomBI)
2849
2783
return false ;
2850
2784
Value *IfCond = DomBI->getCondition ();
@@ -2876,7 +2810,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2876
2810
BranchProbability BIFalseProb = BITrueProb.getCompl ();
2877
2811
if (IfBlocks.size () == 1 ) {
2878
2812
BranchProbability BIBBProb =
2879
- DomBI->getSuccessor (0 ) == MergeBB ? BITrueProb : BIFalseProb;
2813
+ DomBI->getSuccessor (0 ) == BB ? BITrueProb : BIFalseProb;
2880
2814
if (BIBBProb >= Likely)
2881
2815
return false ;
2882
2816
} else {
@@ -2889,7 +2823,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2889
2823
// Don't try to fold an unreachable block. For example, the phi node itself
2890
2824
// can't be the candidate if-condition for a select that we want to form.
2891
2825
if (auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
2892
- if (IfCondPhiInst->getParent () == MergeBB )
2826
+ if (IfCondPhiInst->getParent () == BB )
2893
2827
return false ;
2894
2828
2895
2829
// Okay, we found that we can merge this two-entry phi node into a select.
@@ -2898,8 +2832,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2898
2832
// doesn't support cmov's). Only do this transformation if there are two or
2899
2833
// fewer PHI nodes in this block.
2900
2834
unsigned NumPhis = 0 ;
2901
- for (BasicBlock::iterator I = MergeBB->begin (); isa<PHINode>(I);
2902
- ++NumPhis, ++I)
2835
+ for (BasicBlock::iterator I = BB->begin (); isa<PHINode>(I); ++NumPhis, ++I)
2903
2836
if (NumPhis > 2 )
2904
2837
return false ;
2905
2838
@@ -2912,7 +2845,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2912
2845
TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
2913
2846
2914
2847
bool Changed = false ;
2915
- for (BasicBlock::iterator II = MergeBB ->begin (); isa<PHINode>(II);) {
2848
+ for (BasicBlock::iterator II = BB ->begin (); isa<PHINode>(II);) {
2916
2849
PHINode *PN = cast<PHINode>(II++);
2917
2850
if (Value *V = SimplifyInstruction (PN, {DL, PN})) {
2918
2851
PN->replaceAllUsesWith (V);
@@ -2921,16 +2854,16 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2921
2854
continue ;
2922
2855
}
2923
2856
2924
- if (!dominatesMergePoint (PN->getIncomingValue (0 ), MergeBB , AggressiveInsts,
2857
+ if (!dominatesMergePoint (PN->getIncomingValue (0 ), BB , AggressiveInsts,
2925
2858
Cost, Budget, TTI) ||
2926
- !dominatesMergePoint (PN->getIncomingValue (1 ), MergeBB , AggressiveInsts,
2859
+ !dominatesMergePoint (PN->getIncomingValue (1 ), BB , AggressiveInsts,
2927
2860
Cost, Budget, TTI))
2928
2861
return Changed;
2929
2862
}
2930
2863
2931
2864
// If we folded the first phi, PN dangles at this point. Refresh it. If
2932
2865
// we ran out of PHIs then we simplified them all.
2933
- PN = dyn_cast<PHINode>(MergeBB ->begin ());
2866
+ PN = dyn_cast<PHINode>(BB ->begin ());
2934
2867
if (!PN)
2935
2868
return true ;
2936
2869
@@ -2994,7 +2927,7 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
2994
2927
IRBuilder<NoFolder> Builder (DomBI);
2995
2928
// Propagate fast-math-flags from phi nodes to replacement selects.
2996
2929
IRBuilder<>::FastMathFlagGuard FMFGuard (Builder);
2997
- while (PHINode *PN = dyn_cast<PHINode>(MergeBB ->begin ())) {
2930
+ while (PHINode *PN = dyn_cast<PHINode>(BB ->begin ())) {
2998
2931
if (isa<FPMathOperator>(PN))
2999
2932
Builder.setFastMathFlags (PN->getFastMathFlags ());
3000
2933
@@ -3011,11 +2944,11 @@ static bool SpeculativelyExecuteThenElseCode(BranchInst *BI,
3011
2944
// At this point, all IfBlocks are empty, so our if statement
3012
2945
// has been flattened. Change DomBlock to jump directly to our new block to
3013
2946
// avoid other simplifycfg's kicking in on the diamond.
3014
- Builder.CreateBr (MergeBB );
2947
+ Builder.CreateBr (BB );
3015
2948
3016
2949
SmallVector<DominatorTree::UpdateType, 3 > Updates;
3017
2950
if (DTU) {
3018
- Updates.push_back ({DominatorTree::Insert, DomBlock, MergeBB });
2951
+ Updates.push_back ({DominatorTree::Insert, DomBlock, BB });
3019
2952
for (auto *Successor : successors (DomBlock))
3020
2953
Updates.push_back ({DominatorTree::Delete, DomBlock, Successor});
3021
2954
}
@@ -6588,11 +6521,6 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
6588
6521
return requestResimplify ();
6589
6522
}
6590
6523
6591
- if (Options.FoldTwoEntryPHINode ) {
6592
- if (SpeculativelyExecuteThenElseCode (BI, TTI, DTU, DL))
6593
- return true ;
6594
- }
6595
-
6596
6524
// If this is a branch on a phi node in the current block, thread control
6597
6525
// through this block if any PHI node entries are constants.
6598
6526
if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition ()))
@@ -6808,6 +6736,15 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
6808
6736
6809
6737
IRBuilder<> Builder (BB);
6810
6738
6739
+ if (Options.FoldTwoEntryPHINode ) {
6740
+ // If there is a trivial two-entry PHI node in this basic block, and we can
6741
+ // eliminate it, do so now.
6742
+ if (auto *PN = dyn_cast<PHINode>(BB->begin ()))
6743
+ if (PN->getNumIncomingValues () == 2 )
6744
+ if (FoldTwoEntryPHINode (PN, TTI, DTU, DL))
6745
+ return true ;
6746
+ }
6747
+
6811
6748
Instruction *Terminator = BB->getTerminator ();
6812
6749
Builder.SetInsertPoint (Terminator);
6813
6750
switch (Terminator->getOpcode ()) {
0 commit comments