GCC correctly costs `mulh` instruction for RISC-V, improving code generation.
GCC now properly costs the RISC-V `mulh` instruction, leading to better code generation by favoring multiplication for division-by-constant.
This commit fixes an issue where the RISC-V mulh instruction was incorrectly costed, causing GCC to generate suboptimal code. The previous miscalculation made multiplication-by-reciprocal for division-by-constant operations appear too expensive, discouraging its use. By correctly recognizing the mulh RTL and using appropriate integer multiply costs, GCC can now make more efficient code generation decisions, leading to performance improvements.
In Details
The change affects the riscv_rtx_costs function in config/riscv/riscv.cc within the GCC backend for RISC-V. Previously, the mulh (multiply high) instruction's cost was severely miscalculated due to recursion into the RTL recognizer and incorrect handling of TI mode operands. This patch correctly recognizes the mulh RTL pattern and applies the precise integer multiply cost from the tuning structure, which already accounts for COSTS_N_INSNS. This addresses PR target/125387 and improves the selection of multiplication-by-reciprocal strategies for constant division.
For Context
When a compiler like GCC translates your code, it has to decide between different ways to perform the same operation. For example, dividing by a constant number (like x / 7) can sometimes be faster if the compiler converts it into a multiplication by a reciprocal (like x * (1/7)). To make the best choice, the compiler uses a 'cost model' to estimate how fast each operation will be on the target processor. This commit fixes a long-standing issue in the GCC compiler for RISC-V processors where the cost of a specific instruction, mulh (multiply high), was wildly miscalculated. This incorrect cost made the compiler think that using multiplication for division-by-constant was always too slow. By correcting the cost of mulh, the compiler can now make more intelligent decisions, leading to faster and more efficient code for RISC-V systems, especially for mathematical operations.