GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
ifcvt

ifcvt: Make average_cost independent of arm order [PR125557]

The `ifcvt` optimization's average cost calculation is fixed to be independent of control flow arm order, preventing incorrect conditional move decisions.

This commit addresses a bug in the ifcvt (if-conversion) optimization where the average_cost calculation was sensitive to the order of control flow arms. The previous implementation could lead to different decisions about converting if statements to conditional moves based on whether the ‘then’ or ‘else’ branch appeared first. The fix ensures the calculation is commutative by always starting with the cheaper arm and scaling the difference by the probability of the more expensive arm, improving the robustness and correctness of the optimization.

In Details

The average_cost function in ifcvt.cc is refactored to ensure its output is independent of the order of THEN_COST and ELSE_COST. The original calculation ELSE_COST + P * (THEN_COST - ELSE_COST) was susceptible to rounding differences when P was applied to a negative difference, especially with signed profile probabilities and integer rounding. The new logic calculates max(THEN_COST, ELSE_COST) + P * abs(THEN_COST - ELSE_COST) or equivalent, ensuring that the weighted average rounds consistently regardless of arm order. This resolves PR125557, where opposite profitability decisio…

For Context
ifcvt
A GCC optimization pass that attempts to convert conditional control flow (if-else statements) into conditional move instructions, which can sometimes improve performance on certain architectures.
average_cost
A function within the ifcvt optimization that estimates the expected cost of a conditional branch, considering the probabilities of each branch being taken.
PR125557
Problem Report number 125557, filed in the GCC bug tracking system, related to the ifcvt optimization's handling of average cost calculation.
conditional move
A CPU instruction that conditionally updates a destination register with a source value. Unlike traditional branches, conditional moves do not alter the program counter, which can help avoid pipeline stalls.
CFG
Control Flow Graph. A directed graph representing all execution paths through a function or program. Nodes are basic blocks, and edges represent possible transfers of control.
Filed Under: optimizationifcvtbugfix