Simplify `x+umax(x,1)` to `umax(x*2, 1)`.
New optimization pattern for `x+umax(x,1)` simplifies code for `std::vector::push_back`.
This commit introduces a new optimization pattern that transforms x + umax(x, 1) into umax(x * 2, 1), provided that x * 2 is known not to overflow. This simplification is particularly useful for the std::vector::push_back implementation, as it can trade a post-umax addition for a pre-umax shift, potentially improving code generation. This pattern is seen as a special case of a more general x + minmax(a, b) optimization.
In Details
This commit adds a specific pattern x + umax(x, 1) -> umax(x * 2, 1) to match.pd. The transformation is guarded by an overflow_free_p check for x * 2. This pattern is a specialization of the previously merged x + minmax(a, b) simplification and is relevant for optimizing code generated for C++ standard library containers like std::vector::push_back, where it can improve instruction selection by replacing an addition with a shift.
- pattern matcher
- A component of the compiler that applies predefined transformation rules (patterns) to the intermediate representation of the code to optimize it or generate machine-specific instructions.
- umax
- Represents the maximum of two values. In GCC,
MAX_EXPRis used in the intermediate representation. - overflow_free_p
- A predicate function used in GCC to determine if an arithmetic operation is guaranteed not to overflow for a given type and operands.
- std::vector::push_back
- A C++ standard library function that adds an element to the end of a vector. Its implementation often involves capacity checks and resizing, which can lead to arithmetic operations that this optimization targets.