GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/fold-const

Improve pointer arithmetic optimizations in fold-const and match.pd.

Enhances optimization of pointer differences involving casts and address expressions.

Optimizations for constant pointer differences have been improved by refining the ptr_difference_const logic in match.pd and fold-const.cc. The changes address issues where certain pointer arithmetic expressions, particularly those involving casts and address expressions, were not handled correctly. By adding checks for no-operation conversions and ADDR_EXPR requirements, the compiler can now more reliably fold these expressions, preventing potential regressions and ICEs (Internal Compiler Errors).

In Details

This commit targets the constant folding of pointer difference expressions, specifically addressing regressions in the C++ frontend. The optimization in fold-const.cc that casts pointer arithmetic (T1)(X p+ Y) to ((T1)X p+ Y) when X is a cast was found to be problematic. The fix introduces STRIP_NOPS in split_address_to_core_and_offset and adds ADDR_EXPR requirements to simplification patterns in match.pd. This ensures that ptr_difference_const correctly handles complex scenarios involving casts, SSA_NAMEs, ADDR_EXPRs, and POINTER_PLUS_EXPRs, preventing ICEs and incorrec…

For Context
fold-const
A compiler pass or optimization technique that evaluates constant expressions at compile time rather than runtime. This includes arithmetic on constant values, pointer arithmetic on constant addresses, etc.
match.pd
A file or component within GCC's optimization framework that likely defines patterns for matching and simplifying various expression trees. '.pd' might refer to 'pattern definition'.
POINTER_DIFF_EXPR
An intermediate representation (IR) or tree node in GCC representing the difference between two pointers, which results in an integer value (e.g., number of elements).
ADDR_EXPR
An intermediate representation (IR) or tree node in GCC representing an address of a variable or expression.
SSA_NAME
Static Single Assignment form. In SSA, each variable is assigned exactly once. SSA_NAME represents a named temporary or variable in SSA form, often resulting from computations.
ICE
Internal Compiler Error. An unexpected condition encountered by the compiler during its execution, indicating a bug in the compiler itself.
Filed Under: optimizationcompilerC++