GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
middle-end Performance Win

Fold averages with equal operands

Optimizes average calculations by folding identical operands into a direct result.

The GCC middle-end now performs a constant folding optimization for average calculations where both operands are identical. Expressions like IFN_AVG_FLOOR(x, x) and IFN_AVG_CEIL(x, x) are now correctly simplified to just x, reducing redundant computations.

In Details

In match.pd, GCC now folds IFN_AVG_FLOOR(x, x) and IFN_AVG_CEIL(x, x) to x. This pattern matching optimization simplifies expressions involving the average of two identical values, directly addressing PR middle-end/122715.

For Context
Constant folding
An optimization technique where expressions with constant values are evaluated at compile time rather than runtime. This reduces the amount of computation the program needs to perform when it executes.
Middle-end
The intermediate stage in a compiler's processing pipeline, typically operating on an Intermediate Representation (IR) after initial parsing and before target-specific code generation.
IFN_AVG_FLOOR
An internal GCC intermediate representation operation that computes the floor of the average of two operands.
IFN_AVG_CEIL
An internal GCC intermediate representation operation that computes the ceiling of the average of two operands.
match.pd
A file within GCC that defines patterns for instruction matching and optimization, used by the compiler's middle-end.
Filed Under: optimizationmiddle-endconstant folding