riscv-gnu-toolchain icon indicating copy to clipboard operation
riscv-gnu-toolchain copied to clipboard

2025.02 Compiler Issue: Unnecessary Shift Instructions for int16_t Load

Open bigmagic123 opened this issue 3 months ago • 0 comments

The compiler generates redundant shift operations when loading a volatile int16_t value. This inefficiency persists even under standard optimization levels (-O1, -O2, -Os).

#include <stdint.h>  
volatile int16_t x;  
int get() {  
    return x;  // Should generate a simple `LH` + sign-extension (if needed)  
}

asm:

get:  
        lui     a5, %hi(x)          # Load upper address of 'x'  
        lhu     a0, %lo(x)(a5)      # Load unsigned halfword (LH would suffice)  
        slli    a0, a0, 16          # Unnecessary left shift  
        srai    a0, a0, 16          # Unnecessary arithmetic right shift  
        ret  
x:  
        .zero   2                   # Variable storage

Upstream Reference: GCC Bug #119709

bigmagic123 avatar Aug 18 '25 08:08 bigmagic123