GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
tree-optimization

Revise (T *)(ptr - ptr_cst) pattern

Canonicalizes pointer arithmetic patterns for better optimization.

This commit refines a GCC pattern for pointer arithmetic involving type casting. The pattern (T *)(ptr - ptr_cst) is now directly canonicalized to (T *)ptr p+ cst. This change simplifies the GENERIC representation, allowing subsequent optimizations to more effectively recognize and process pointer arithmetic. The pattern was originally introduced to avoid issues with build_fold_addr_expr_with_type, and this revision ensures more robust simplification.

In Details

The match.pd file is being updated to modify the canonicalization of the pattern (T *)(ptr - ptr_cst). Previously, this pattern might have been avoided or handled indirectly. The revision applies the GENERIC simplification directly to the pattern, transforming it into (T *)ptr p+ cst. This change aims to ensure consistent and predictable representation of pointer arithmetic involving constant offsets, facilitating further optimizations.

For Context
Pointer Arithmetic
Performing arithmetic operations (like addition or subtraction) on memory addresses stored in pointers. The result is a new memory address.
Canonicalization
The process of transforming an expression or data structure into a standard or canonical form, simplifying comparisons and further processing.
GENERIC
GCC's first intermediate representation (IR), which is language-independent and tree-based. It represents the structure of the code before lower-level optimizations.
Tree Optimization
A phase of the compiler that operates on GCC's intermediate representations (like GENERIC or GIMPLE) to improve code performance or reduce its size.
build_fold_addr_expr_with_type
An internal GCC function likely used to construct expressions representing memory addresses with associated types.
Filed Under: optimizationpointer arithmetictree-optimization