@@ -314,46 +314,6 @@ SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
314
314
return !Fail;
315
315
}
316
316
317
- // / Return true if it is safe and profitable to merge these two terminator
318
- // / instructions together, where SI1 is an unconditional branch. PhiNodes will
319
- // / store all PHI nodes in common successors.
320
- static bool
321
- isProfitableToFoldUnconditional (BranchInst *SI1, BranchInst *SI2,
322
- Instruction *Cond,
323
- SmallVectorImpl<PHINode *> &PhiNodes) {
324
- if (SI1 == SI2)
325
- return false ; // Can't merge with self!
326
- assert (SI1->isUnconditional () && SI2->isConditional ());
327
-
328
- // We fold the unconditional branch if we can easily update all PHI nodes in
329
- // common successors:
330
- // 1> We have a constant incoming value for the conditional branch;
331
- // 2> We have "Cond" as the incoming value for the unconditional branch;
332
- // 3> SI2->getCondition() and Cond have same operands.
333
- CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition ());
334
- if (!Ci2)
335
- return false ;
336
- if (!(Cond->getOperand (0 ) == Ci2->getOperand (0 ) &&
337
- Cond->getOperand (1 ) == Ci2->getOperand (1 )) &&
338
- !(Cond->getOperand (0 ) == Ci2->getOperand (1 ) &&
339
- Cond->getOperand (1 ) == Ci2->getOperand (0 )))
340
- return false ;
341
-
342
- BasicBlock *SI1BB = SI1->getParent ();
343
- BasicBlock *SI2BB = SI2->getParent ();
344
- SmallPtrSet<BasicBlock *, 16 > SI1Succs (succ_begin (SI1BB), succ_end (SI1BB));
345
- for (BasicBlock *Succ : successors (SI2BB))
346
- if (SI1Succs.count (Succ))
347
- for (BasicBlock::iterator BBI = Succ->begin (); isa<PHINode>(BBI); ++BBI) {
348
- PHINode *PN = cast<PHINode>(BBI);
349
- if (PN->getIncomingValueForBlock (SI1BB) != Cond ||
350
- !isa<ConstantInt>(PN->getIncomingValueForBlock (SI2BB)))
351
- return false ;
352
- PhiNodes.push_back (PN);
353
- }
354
- return true ;
355
- }
356
-
357
317
// / Update PHI nodes in Succ to indicate that there will now be entries in it
358
318
// / from the 'NewPred' block. The values that will be flowing into the PHI nodes
359
319
// / will be the same as those coming in from ExistPred, an existing predecessor
@@ -2783,23 +2743,6 @@ bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
2783
2743
return true ;
2784
2744
}
2785
2745
2786
- // / Return true if the given instruction is available
2787
- // / in its predecessor block. If yes, the instruction will be removed.
2788
- static bool tryCSEWithPredecessor (Instruction *Inst, BasicBlock *PB) {
2789
- if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2790
- return false ;
2791
- for (Instruction &I : *PB) {
2792
- Instruction *PBI = &I;
2793
- // Check whether Inst and PBI generate the same value.
2794
- if (Inst->isIdenticalTo (PBI)) {
2795
- Inst->replaceAllUsesWith (PBI);
2796
- Inst->eraseFromParent ();
2797
- return true ;
2798
- }
2799
- }
2800
- return false ;
2801
- }
2802
-
2803
2746
// / Return true if either PBI or BI has branch weight available, and store
2804
2747
// / the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
2805
2748
// / not have branch weight, use 1:1 as its weight.
@@ -2830,6 +2773,11 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2830
2773
MemorySSAUpdater *MSSAU,
2831
2774
const TargetTransformInfo *TTI,
2832
2775
unsigned BonusInstThreshold) {
2776
+ // If this block ends with an unconditional branch,
2777
+ // let SpeculativelyExecuteBB() deal with it.
2778
+ if (!BI->isConditional ())
2779
+ return false ;
2780
+
2833
2781
BasicBlock *BB = BI->getParent ();
2834
2782
2835
2783
const unsigned PredCount = pred_size (BB);
@@ -2845,37 +2793,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2845
2793
BB->getParent ()->hasMinSize () ? TargetTransformInfo::TCK_CodeSize
2846
2794
: TargetTransformInfo::TCK_SizeAndLatency;
2847
2795
2848
- Instruction *Cond = nullptr ;
2849
- if (BI->isConditional ())
2850
- Cond = dyn_cast<Instruction>(BI->getCondition ());
2851
- else {
2852
- // For unconditional branch, check for a simple CFG pattern, where
2853
- // BB has a single predecessor and BB's successor is also its predecessor's
2854
- // successor. If such pattern exists, check for CSE between BB and its
2855
- // predecessor.
2856
- if (BasicBlock *PB = BB->getSinglePredecessor ())
2857
- if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator ()))
2858
- if (PBI->isConditional () &&
2859
- (BI->getSuccessor (0 ) == PBI->getSuccessor (0 ) ||
2860
- BI->getSuccessor (0 ) == PBI->getSuccessor (1 ))) {
2861
- for (auto I = BB->instructionsWithoutDebug ().begin (),
2862
- E = BB->instructionsWithoutDebug ().end ();
2863
- I != E;) {
2864
- Instruction *Curr = &*I++;
2865
- if (isa<CmpInst>(Curr)) {
2866
- Cond = Curr;
2867
- break ;
2868
- }
2869
- // Quit if we can't remove this instruction.
2870
- if (!tryCSEWithPredecessor (Curr, PB))
2871
- return Changed;
2872
- Changed = true ;
2873
- }
2874
- }
2875
-
2876
- if (!Cond)
2877
- return Changed;
2878
- }
2796
+ Instruction *Cond = dyn_cast<Instruction>(BI->getCondition ());
2879
2797
2880
2798
if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2881
2799
Cond->getParent () != BB || !Cond->hasOneUse ())
@@ -2934,7 +2852,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2934
2852
2935
2853
// Finally, don't infinitely unroll conditional loops.
2936
2854
BasicBlock *TrueDest = BI->getSuccessor (0 );
2937
- BasicBlock *FalseDest = ( BI->isConditional ()) ? BI-> getSuccessor (1 ) : nullptr ;
2855
+ BasicBlock *FalseDest = BI->getSuccessor (1 );
2938
2856
if (TrueDest == BB || FalseDest == BB)
2939
2857
return Changed;
2940
2858
@@ -2945,39 +2863,30 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2945
2863
// Check that we have two conditional branches. If there is a PHI node in
2946
2864
// the common successor, verify that the same value flows in from both
2947
2865
// blocks.
2948
- SmallVector<PHINode *, 4 > PHIs;
2949
- if (!PBI || PBI->isUnconditional () ||
2950
- (BI->isConditional () && !SafeToMergeTerminators (BI, PBI)) ||
2951
- (!BI->isConditional () &&
2952
- !isProfitableToFoldUnconditional (BI, PBI, Cond, PHIs)))
2866
+ if (!PBI || PBI->isUnconditional () || !SafeToMergeTerminators (BI, PBI))
2953
2867
continue ;
2954
2868
2955
2869
// Determine if the two branches share a common destination.
2956
2870
Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
2957
2871
bool InvertPredCond = false ;
2958
2872
2959
- if (BI->isConditional ()) {
2960
- if (PBI->getSuccessor (0 ) == TrueDest) {
2961
- Opc = Instruction::Or;
2962
- } else if (PBI->getSuccessor (1 ) == FalseDest) {
2963
- Opc = Instruction::And;
2964
- } else if (PBI->getSuccessor (0 ) == FalseDest) {
2965
- Opc = Instruction::And;
2966
- InvertPredCond = true ;
2967
- } else if (PBI->getSuccessor (1 ) == TrueDest) {
2968
- Opc = Instruction::Or;
2969
- InvertPredCond = true ;
2970
- } else {
2971
- continue ;
2972
- }
2873
+ if (PBI->getSuccessor (0 ) == TrueDest) {
2874
+ Opc = Instruction::Or;
2875
+ } else if (PBI->getSuccessor (1 ) == FalseDest) {
2876
+ Opc = Instruction::And;
2877
+ } else if (PBI->getSuccessor (0 ) == FalseDest) {
2878
+ Opc = Instruction::And;
2879
+ InvertPredCond = true ;
2880
+ } else if (PBI->getSuccessor (1 ) == TrueDest) {
2881
+ Opc = Instruction::Or;
2882
+ InvertPredCond = true ;
2973
2883
} else {
2974
- if (PBI->getSuccessor (0 ) != TrueDest && PBI->getSuccessor (1 ) != TrueDest)
2975
- continue ;
2884
+ continue ;
2976
2885
}
2977
2886
2978
2887
// Check the cost of inserting the necessary logic before performing the
2979
2888
// transformation.
2980
- if (TTI && Opc != Instruction::BinaryOpsEnd ) {
2889
+ if (TTI) {
2981
2890
Type *Ty = BI->getCondition ()->getType ();
2982
2891
unsigned Cost = TTI->getArithmeticInstrCost (Opc, Ty, CostKind);
2983
2892
if (InvertPredCond && (!PBI->getCondition ()->hasOneUse () ||
@@ -2991,8 +2900,6 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
2991
2900
LLVM_DEBUG (dbgs () << " FOLDING BRANCH TO COMMON DEST:\n " << *PBI << *BB);
2992
2901
Changed = true ;
2993
2902
2994
- SmallVector<DominatorTree::UpdateType, 3 > Updates;
2995
-
2996
2903
IRBuilder<> Builder (PBI);
2997
2904
// The builder is used to create instructions to eliminate the branch in BB.
2998
2905
// If BB's terminator has !annotation metadata, add it to the new
@@ -3015,16 +2922,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
3015
2922
PBI->swapSuccessors ();
3016
2923
}
3017
2924
3018
- BasicBlock *UniqueSucc =
3019
- BI->isConditional ()
3020
- ? (PBI->getSuccessor (0 ) == BB ? TrueDest : FalseDest)
3021
- : TrueDest;
2925
+ BasicBlock *UniqueSucc = PBI->getSuccessor (0 ) == BB ? TrueDest : FalseDest;
3022
2926
3023
2927
// Before cloning instructions, notify the successor basic block that it
3024
2928
// is about to have a new predecessor. This will update PHI nodes,
3025
2929
// which will allow us to update live-out uses of bonus instructions.
3026
- if (BI->isConditional ())
3027
- AddPredecessorToBlock (UniqueSucc, PredBlock, BB, MSSAU);
2930
+ AddPredecessorToBlock (UniqueSucc, PredBlock, BB, MSSAU);
3028
2931
3029
2932
// If we have bonus instructions, clone them into the predecessor block.
3030
2933
// Note that there may be multiple predecessor blocks, so we cannot move
@@ -3094,120 +2997,64 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
3094
2997
if (User->getParent () != UniqueSucc)
3095
2998
return false ;
3096
2999
// Update the incoming value for the new predecessor.
3097
- return PN->getIncomingBlock (U) ==
3098
- (BI->isConditional () ? PredBlock : BB);
3000
+ return PN->getIncomingBlock (U) == PredBlock;
3099
3001
});
3100
3002
}
3101
3003
3102
3004
// Now that the Cond was cloned into the predecessor basic block,
3103
3005
// or/and the two conditions together.
3104
- if (BI->isConditional ()) {
3105
- Instruction *NewCond = cast<Instruction>(
3106
- Builder.CreateBinOp (Opc, PBI->getCondition (), CondInPred, " or.cond" ));
3107
- PBI->setCondition (NewCond);
3108
-
3109
- uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3110
- bool HasWeights =
3111
- extractPredSuccWeights (PBI, BI, PredTrueWeight, PredFalseWeight,
3112
- SuccTrueWeight, SuccFalseWeight);
3113
- SmallVector<uint64_t , 8 > NewWeights;
3114
-
3115
- if (PBI->getSuccessor (0 ) == BB) {
3116
- if (HasWeights) {
3117
- // PBI: br i1 %x, BB, FalseDest
3118
- // BI: br i1 %y, UniqueSucc, FalseDest
3119
- // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
3120
- NewWeights.push_back (PredTrueWeight * SuccTrueWeight);
3121
- // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
3122
- // TrueWeight for PBI * FalseWeight for BI.
3123
- // We assume that total weights of a BranchInst can fit into 32 bits.
3124
- // Therefore, we will not have overflow using 64-bit arithmetic.
3125
- NewWeights.push_back (PredFalseWeight *
3126
- (SuccFalseWeight + SuccTrueWeight) +
3127
- PredTrueWeight * SuccFalseWeight);
3128
- }
3129
- PBI->setSuccessor (0 , UniqueSucc);
3130
- }
3131
- if (PBI->getSuccessor (1 ) == BB) {
3132
- if (HasWeights) {
3133
- // PBI: br i1 %x, TrueDest, BB
3134
- // BI: br i1 %y, TrueDest, UniqueSucc
3135
- // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
3136
- // FalseWeight for PBI * TrueWeight for BI.
3137
- NewWeights.push_back (PredTrueWeight *
3138
- (SuccFalseWeight + SuccTrueWeight) +
3139
- PredFalseWeight * SuccTrueWeight);
3140
- // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
3141
- NewWeights.push_back (PredFalseWeight * SuccFalseWeight);
3142
- }
3143
- PBI->setSuccessor (1 , UniqueSucc);
3144
- }
3145
- if (NewWeights.size () == 2 ) {
3146
- // Halve the weights if any of them cannot fit in an uint32_t
3147
- FitWeights (NewWeights);
3006
+ Instruction *NewCond = cast<Instruction>(
3007
+ Builder.CreateBinOp (Opc, PBI->getCondition (), CondInPred, " or.cond" ));
3008
+ PBI->setCondition (NewCond);
3148
3009
3149
- SmallVector< uint32_t , 8 > MDWeights (NewWeights. begin (),
3150
- NewWeights. end ());
3151
- setBranchWeights (PBI, MDWeights[ 0 ], MDWeights[ 1 ]);
3152
- } else
3153
- PBI-> setMetadata (LLVMContext::MD_prof, nullptr ) ;
3010
+ uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3011
+ bool HasWeights =
3012
+ extractPredSuccWeights (PBI, BI, PredTrueWeight, PredFalseWeight,
3013
+ SuccTrueWeight, SuccFalseWeight);
3014
+ SmallVector< uint64_t , 8 > NewWeights ;
3154
3015
3155
- Updates.push_back ({DominatorTree::Insert, PredBlock, UniqueSucc});
3156
- Updates.push_back ({DominatorTree::Delete, PredBlock, BB});
3157
- } else {
3158
- // Update PHI nodes in the common successors.
3159
- for (unsigned i = 0 , e = PHIs.size (); i != e; ++i) {
3160
- ConstantInt *PBI_C = cast<ConstantInt>(
3161
- PHIs[i]->getIncomingValueForBlock (PBI->getParent ()));
3162
- assert (PBI_C->getType ()->isIntegerTy (1 ));
3163
- Instruction *MergedCond = nullptr ;
3164
- if (PBI->getSuccessor (0 ) == UniqueSucc) {
3165
- Updates.push_back (
3166
- {DominatorTree::Delete, PredBlock, PBI->getSuccessor (1 )});
3167
- // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
3168
- // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
3169
- // is false: !PBI_Cond and BI_Value
3170
- Instruction *NotCond = cast<Instruction>(
3171
- Builder.CreateNot (PBI->getCondition (), " not.cond" ));
3172
- MergedCond = cast<Instruction>(
3173
- Builder.CreateBinOp (Instruction::And, NotCond, CondInPred,
3174
- " and.cond" ));
3175
- if (PBI_C->isOne ())
3176
- MergedCond = cast<Instruction>(Builder.CreateBinOp (
3177
- Instruction::Or, PBI->getCondition (), MergedCond, " or.cond" ));
3178
- } else {
3179
- assert (PBI->getSuccessor (1 ) == UniqueSucc && " Unexpected branch" );
3180
- Updates.push_back (
3181
- {DominatorTree::Delete, PredBlock, PBI->getSuccessor (0 )});
3182
- // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
3183
- // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
3184
- // is false: PBI_Cond and BI_Value
3185
- MergedCond = cast<Instruction>(Builder.CreateBinOp (
3186
- Instruction::And, PBI->getCondition (), CondInPred, " and.cond" ));
3187
- if (PBI_C->isOne ()) {
3188
- Instruction *NotCond = cast<Instruction>(
3189
- Builder.CreateNot (PBI->getCondition (), " not.cond" ));
3190
- MergedCond = cast<Instruction>(Builder.CreateBinOp (
3191
- Instruction::Or, NotCond, MergedCond, " or.cond" ));
3192
- }
3193
- }
3194
- // Update PHI Node.
3195
- PHIs[i]->setIncomingValueForBlock (PBI->getParent (), MergedCond);
3016
+ if (PBI->getSuccessor (0 ) == BB) {
3017
+ if (HasWeights) {
3018
+ // PBI: br i1 %x, BB, FalseDest
3019
+ // BI: br i1 %y, UniqueSucc, FalseDest
3020
+ // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
3021
+ NewWeights.push_back (PredTrueWeight * SuccTrueWeight);
3022
+ // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
3023
+ // TrueWeight for PBI * FalseWeight for BI.
3024
+ // We assume that total weights of a BranchInst can fit into 32 bits.
3025
+ // Therefore, we will not have overflow using 64-bit arithmetic.
3026
+ NewWeights.push_back (PredFalseWeight *
3027
+ (SuccFalseWeight + SuccTrueWeight) +
3028
+ PredTrueWeight * SuccFalseWeight);
3196
3029
}
3197
-
3198
- // PBI is changed to branch to UniqueSucc below. Remove itself from
3199
- // potential phis from all other successors.
3200
- if (MSSAU)
3201
- MSSAU->changeCondBranchToUnconditionalTo (PBI, UniqueSucc);
3202
-
3203
- // Change PBI from Conditional to Unconditional.
3204
- BranchInst *New_PBI = BranchInst::Create (UniqueSucc, PBI);
3205
- EraseTerminatorAndDCECond (PBI, MSSAU);
3206
- PBI = New_PBI;
3030
+ PBI->setSuccessor (0 , UniqueSucc);
3031
+ }
3032
+ if (PBI->getSuccessor (1 ) == BB) {
3033
+ if (HasWeights) {
3034
+ // PBI: br i1 %x, TrueDest, BB
3035
+ // BI: br i1 %y, TrueDest, UniqueSucc
3036
+ // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
3037
+ // FalseWeight for PBI * TrueWeight for BI.
3038
+ NewWeights.push_back (PredTrueWeight *
3039
+ (SuccFalseWeight + SuccTrueWeight) +
3040
+ PredFalseWeight * SuccTrueWeight);
3041
+ // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
3042
+ NewWeights.push_back (PredFalseWeight * SuccFalseWeight);
3043
+ }
3044
+ PBI->setSuccessor (1 , UniqueSucc);
3207
3045
}
3046
+ if (NewWeights.size () == 2 ) {
3047
+ // Halve the weights if any of them cannot fit in an uint32_t
3048
+ FitWeights (NewWeights);
3049
+
3050
+ SmallVector<uint32_t , 8 > MDWeights (NewWeights.begin (), NewWeights.end ());
3051
+ setBranchWeights (PBI, MDWeights[0 ], MDWeights[1 ]);
3052
+ } else
3053
+ PBI->setMetadata (LLVMContext::MD_prof, nullptr );
3208
3054
3209
3055
if (DTU)
3210
- DTU->applyUpdates (Updates);
3056
+ DTU->applyUpdates ({{DominatorTree::Insert, PredBlock, UniqueSucc},
3057
+ {DominatorTree::Delete, PredBlock, BB}});
3211
3058
3212
3059
// If BI was a loop latch, it may have had associated loop metadata.
3213
3060
// We need to copy it to the new latch, that is, PBI.
0 commit comments