https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126022

            Bug ID: 126022
           Summary: For RISC-V, prefer li+slli rather than lui+add for
                    some constants
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: law at gcc dot gnu.org
  Target Milestone: ---

There are a variety of methods to synthesize constants.  Those methods have
primarily been focused on minimizing the instruction count (as a rough proxy
for performance).  However, there are cases where we can produce the same
instruction count, but generate sequences that potentially compress better.

Let's consider this case for -O2 -march=rv64gcbv_zicond

unsigned long fubar(unsigned long x) { return 3072UL; }

The naive code generation for this would look like:

        li      a0,4096
        addi    a0,a0,-1024


However the "addi" instruction is a 4 byte instruction.

Instead we could generate:


        li      a0, 3
        slli    a0, a0, 10

That has the advantage that it compresses better (the slli compresses to 2
bytes).  Of course lui+addi is the poster child for fusion, so on designs which
support fusion, lui+addi may still be the better choice.  But certainly for
-Os, and on designs without fusion support the li+slli sequence is better.

I suspect we just need some special casing in the constant synthesis code.  We
can query fusion status in there as well so we can likely tune this however we
want.

Reply via email to