tree-optimization: Recognize widened unsigned multiply overflow checks
GCC now recognizes and optimizes widened unsigned multiply overflow checks, canonicalizing them to a specific overflow detection pattern.
GCC’s tree optimization pass can now recognize and optimize overflow checks involving widened unsigned multiplications. The compiler canonicalizes patterns where a multiplication result is shifted and compared against the maximum value of the narrow type into a widened comparison form. This form is then further simplified to IFN_MUL_OVERFLOW when the target architecture supports direct unsigned multiply-overflow instructions, enabling more efficient code generation.
In Details
This change enhances match.pd to recognize overflow checks for unsigned widened multiplications. It first canonicalizes shifted high-half forms to a widened comparison against the narrow type's maximum (e.g., (a*b) >> N == MAX_T). Subsequently, this comparison is folded to IFN_MUL_OVERFLOW if the target has native support, optimizing checks for conditions like (unsigned short)(a*b) > 65535.
- tree-optimization
- GCC's middle-end optimization framework that operates on an intermediate representation called GENERIC/GIMPLE, which resembles an abstract syntax tree.
- widened multiplication
- A multiplication operation where the operands are promoted to a wider type before multiplication. For example, multiplying two 16-bit integers into a 32-bit result.
- overflow check
- A check to determine if the result of an arithmetic operation exceeds the representable range of the data type.
- canonicalize
- To transform an expression or pattern into a standard, unique form, making it easier for optimizations to recognize and manipulate.
- IFN_MUL_OVERFLOW
- An internal GCC pattern representing an unsigned multiplication overflow check, used by the optimizer.