tailc: Punt on some return type mismatches in tail call optimization
Compiler's tail call optimization conservatively avoids miscompilations due to return type mismatches involving _BitInt.
The tail call optimization (tailc) in GCC was incorrectly optimizing some function calls where the callee’s return type and the caller’s expected return type mismatched, particularly with _BitInt types on certain targets. This commit makes the optimization more conservative by punting (disabling the optimization) in cases of _BitInt to _BitInt conversions with different sizes, or when precision differs for narrowing casts, unless the target specifically handles padding bits in a way that preserves ABI compatibility. This prevents miscompilations by avoiding assumptions about type conversions at function boundaries.
In Details
The tailc optimization pass, specifically within useless_type_conversion_p, needs to conservatively check for ABI compatibility across function calls. This commit refines that check for _BitInt types. It now punts on BITINT_TYPE_P != BITINT_TYPE_P conversions and when TYPE_SIZE differs for same TYPE_MODE. For narrowing casts, it only proceeds on bitint_ext_undef targets where padding bits are unspecified and can be safely ignored if they don't increase. For widening casts, it punts unless the target is bitint_ext_full and the narrower type's bits are already extended, ensuring…
- tail call optimization (tailc)
- A compiler optimization that transforms a function call, which is the last operation in a function (a tail call), into a jump. This avoids growing the call stack, preventing stack overflow for deep recursion and improving performance.
- _BitInt
- A C language extension that allows for integer types of arbitrary bit widths, rather than being limited to fixed sizes like 8, 16, 32, or 64 bits.
- ABI
- Application Binary Interface. Defines how compiled code should interact at the binary level, including function call conventions, data type representations, and memory layout. Ensuring ABI compatibility is crucial for interoperability between different compiled components.
- padding bits
- Extra bits in a data type that are not used to store the actual value but are present due to alignment requirements or the way hardware/compilers handle data types. The behavior of padding bits can be target-specific and may affect ABI compatibility.
- GIMPLE
- GCC's internal representation of code between the parsing and optimization phases. It's a three-address form that simplifies many optimizations.