RISC-V: Fix ICE on arrays with negative low bounds in 32-bit mode
Compiler no longer crashes when processing RISC-V arrays with negative lower bounds in 32-bit mode.
An internal compiler error (ICE) occurred in the RISC-V backend when handling arrays with negative lower bounds in 32-bit mode. The issue stemmed from calculating the number of array elements using HOST_WIDE_INT, which did not correctly emulate the wraparound behavior of 32-bit sizetype indices. This commit modifies riscv_flatten_aggregate_field to use the precision of the index type for this calculation, resolving the ICE and ensuring correct behavior.
In Details
The riscv_flatten_aggregate_field function in config/riscv/riscv.cc was asserting n_elts >= 0 after calculating the number of elements for an array type. The calculation 1 + tree_to_uhwi (TYPE_MAX_VALUE (index)) - tree_to_uhwi (TYPE_MIN_VALUE (index)) used HOST_WIDE_INT, which failed to replicate the 32-bit sizetype wraparound when TYPE_MIN_VALUE was negative and TYPE_MAX_VALUE was not. This commit changes the calculation to use the appropriate wide_int precision matching the index type, mirroring the logic in array_type_nelts_top and fixing an ICE triggered by an Ada test…
- ICE
- Internal Compiler Error. A severe error within the compiler itself, typically caused by an unexpected internal state or logic flaw, leading to a crash.
- HOST_WIDE_INT
- A C data type in GCC that is guaranteed to be wide enough to hold any pointer or any value that can be represented by the host's
size_torptrdiff_ttype. - sizetype
- The unsigned integer type used by the compiler to represent sizes and counts, typically
size_t. - wraparound
- In integer arithmetic, when an operation results in a value that exceeds the maximum representable value for the given type, the result 'wraps around' to the smallest representable value. For unsigned types, this is a standard behavior.
- array_type_nelts_top
- A GCC internal function that calculates the number of elements in an array type, often used in higher-level compiler passes.