> On 25 Jul 2026, at 04:09, Jeffrey Law <[email protected]> wrote:
> 
> 
> 
> On 7/24/2026 6:27 AM, Kyrylo Tkachov wrote:
>>> On 17 Jul 2026, at 15:25, Kyrylo Tkachov <[email protected]> wrote:
>>> 
>>> From: Kyrylo Tkachov <[email protected]>
>>> 
>>> average_cost computes
>>> 
>>>  ELSE_COST + P * (THEN_COST - ELSE_COST)
>>> 
>>> profile_probability::apply now rounds signed initialized values to the 
>>> nearest
>>> integer, with halfway values away from zero.  That signed rounding can still
>>> give different integer costs when equivalent CFG arms are reversed.  Unknown
>>> probabilities have the same issue for odd cost differences because apply
>>> truncates them toward zero.
>>> 
>>> Write the documented weighted average as
>>> 
>>>  ELSE_COST + P * (THEN_COST - ELSE_COST)
>>> 
>>> when THEN_COST is at least ELSE_COST, and as
>>> 
>>>  THEN_COST + (1 - P) * (ELSE_COST - THEN_COST)
>>> 
>>> otherwise.  Both forms start with the cheaper arm and scale a nonnegative
>>> cost difference by the probability of the costlier arm.  Reversing the CFG
>>> arms therefore keeps the same base, magnitude, probability, and rounded
>>> result.  Cast the costlier arm to gcov_type before subtraction.
>>> 
>>> Add an x86 test with the same costs and probabilities represented using
>>> reversed CFG arms.  The scaled cost difference is an exact halfway value.
>>> The apply-only compiler makes opposite profitability decisions for the two
>>> orientations.  With this change both forms convert to conditional moves.
>>> 
>>> Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux.
>>> Ok for trunk?
>>> Thanks,
>>> Kyrill
>>> 
>>> gcc/ChangeLog:
>>> 
>>> PR tree-optimization/125557
>>> * ifcvt.cc (average_cost): Scale a nonnegative cost difference using
>>> the probability of the more expensive arm.
>>> 
>>> gcc/testsuite/ChangeLog:
>>> 
>>> PR tree-optimization/125557
>>> * gcc.target/i386/ifcvt-average-cost-1.c: New test.
>>> 
>>> Signed-off-by: Kyrylo Tkachov <[email protected]>
> OK.

Thanks for the review Jeff. You seem to have ok’ed this patch twice.
Did you meant to ok patch [1/2] by any chance?
https://gcc.gnu.org/pipermail/gcc-patches/2026-July/724378.html
Kyrill

> jeff

Reply via email to