Avoid repeated undistribution with negative pow exponents
Fixes GCC's re-associative pass to prevent infinite loops when handling negative exponents in power functions.
This commit resolves an issue where GCC’s re-associative optimization pass could enter a loop, generating thousands of multiplications when encountering expressions with both positive and negative power terms. The problem occurred because stmt_is_power_of_op incorrectly treated explicit factors as part of negative pow calls, leading to repeated removal of terms without actually reducing the pow exponent. The fix synchronizes stmt_is_power_of_op with acceptable_pow_call, ensuring that only terms correctly handled by acceptable_pow_call are considered for elimination, thus breaking the loop and fixing incorrect code generation.
In Details
The tree-ssa-reassoc.cc pass exhibited incorrect behavior with expressions like (x * y) * __builtin_powi (x, 8000) + x * z * x * pow (x, -8000.0) under -Ofast. The stmt_is_power_of_op function incorrectly identified explicit factors of x as belonging to the pow(x, -8000.0) term. This led to zero_one_operation repeatedly decrementing the negative exponent without consuming the explicit x factors, causing an infinite loop of multiplication generation. This change synchronizes stmt_is_power_of_op to only consider operands that acceptable_pow_call deems valid for linearization…
- re-associative optimization
- A compiler optimization pass that rearranges the order of associative operations (like addition and multiplication) to potentially improve performance or enable other optimizations. It operates on the compiler's intermediate representation.
- undistribute
- An optimization technique where a common factor is extracted from terms in an expression, often to enable further simplification or code generation.
- stmt_is_power_of_op
- A function within GCC's re-associative optimization pass that checks if a statement (like a multiplication) involves a power operation, determining if it can be part of an undistribution or re-association transformation.
- acceptable_pow_call
- A function within GCC's re-associative optimization pass that determines if a call to a power function (like
powor__builtin_powi) is suitable for certain optimizations, typically based on the exponent's value and type.