@@ -7731,19 +7731,24 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
77317731 // label. The other is those powers of 2 that don't appear in the case
77327732 // statement. We don't know the distribution of the values coming in, so
77337733 // the safest is to split 50-50 the original probability to `default`.
7734- uint64_t OrigDenominator = sum_of ( map_range (
7735- Weights, []( const auto &V) { return static_cast <uint64_t >(V); } ));
7734+ uint64_t OrigDenominator =
7735+ sum_of ( map_range ( Weights, StaticCastTo <uint64_t >));
77367736 SmallVector<uint64_t > NewWeights (2 );
77377737 NewWeights[1 ] = Weights[0 ] / 2 ;
77387738 NewWeights[0 ] = OrigDenominator - NewWeights[1 ];
77397739 setFittedBranchWeights (*BI, NewWeights, /* IsExpected=*/ false );
7740-
7741- // For the original switch, we reduce the weight of the default by the
7742- // amount by which the previous branch contributes to getting to default,
7743- // and then make sure the remaining weights have the same relative ratio
7744- // wrt eachother.
7740+ // The probability of executing the default block stays constant. It was
7741+ // p_d = Weights[0] / OrigDenominator
7742+ // we rewrite as W/D
7743+ // We want to find the probability of the default branch of the switch
7744+ // statement. Let's call it X. We have W/D = W/2D + X * (1-W/2D)
7745+ // i.e. the original probability is the probability we go to the default
7746+ // branch from the BI branch, or we take the default branch on the SI.
7747+ // Meaning X = W / (2D - W), or (W/2) / (D - W/2)
7748+ // This matches using W/2 for the default branch probability numerator and
7749+ // D-W/2 as the denominator.
7750+ Weights[0 ] = NewWeights[1 ];
77457751 uint64_t CasesDenominator = OrigDenominator - Weights[0 ];
7746- Weights[0 ] /= 2 ;
77477752 for (auto &W : drop_begin (Weights))
77487753 W = NewWeights[0 ] * static_cast <double >(W) / CasesDenominator;
77497754
0 commit comments