https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119709
Bug ID: 119709
Summary: RISC-V: Why volatile int16_t variables generate extra
shift instructions in compiler output
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: bigmagicreadsun at gmail dot com
Target Milestone: ---
When compiling the following code with RISC-V GCC:
<C>
#include <stdint.h>
volatile int16_t x;
int get() {
return x;
}
The generated assembly is:
<ASM>
get:
lui a5,%hi(x)
lhu a0,%lo(x)(a5)
slli a0,a0,16
srai a0,a0,16
ret
x:
.zero 2
(Full example: Godbolt link: https://godbolt.org/z/Y93T4c7M7)
Why does the compiler generate redundant shift operations (slli + srai) instead
of directly using lh?
When I change x to volatile uint16_t x, GCC correctly generates an lhu
instruction without shifts. Why does this behavior occur?