public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RISC-V sign extension query
@ 2023-09-18 19:45 Vineet Gupta
  2023-09-18 20:10 ` Andrew Waterman
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Vineet Gupta @ 2023-09-18 19:45 UTC (permalink / raw)
  To: Jeff Law, Andrew Waterman
  Cc: Kito Cheng, Palmer Dabbelt, Ajit Agarwal, GCC Patches,
	Jivan Hakobyan, gnu-toolchain, Joern Rennecke

Hi Jeff, Andrew

I've been looking into redundant sign extension and while there are 
things to be improved in REE, there's something I wanted to confirm 
before heading off into the weeds.

Consider the test below:

int foo(int unused, int n, unsigned y, unsigned delta){
   int s = 0;
   unsigned int x = 0;    // if int, sext elided
   for (;x<n;x +=delta)
     s += x+y;
   return s;
}

-O2 -march=rv64gc_zba_zbb_zbs

foo2:
     sext.w    a6,a1            # 1
     beq    a1,zero,.L4
     li    a5,0
     li    a0,0
.L3:
     addw    a4,a2,a5
     addw    a5,a3,a5
     addw    a0,a4,a0
     bltu    a5,a6,.L3
     ret
.L4:
     li    a0,0
     ret

I believe the SEXT.W is not semantically needed as a1 is supposed to be 
sign extended already at call site as per psABI [1]. I quote

     "When passed in registers or on the stack, integer scalars narrower 
than XLEN bits are widened according to the sign of their type up to 32 
bits, then sign-extended to XLEN bits"

However currently RISC-V backend thinks otherwise: changing @x to int, 
causes the the sign extend to go away. I think both the cases should 
behave the same (and not generate SEXT.w) given the ABI clause above. 
Note that this manifests in initial RTL expand itself 
generating/or-not-generating the sign_extend so if it is unnecessary we 
can avoid late fixups in REE.

So the questions is for you to confirm if my conclusions above are correct.

For the cases which do require sign extends, but not being eliminated 
due to "missing definition(s)" I'm working on adapting Ajit's REE ABI 
interfaces work [2] to work for RISC-V as well.

Thx,
-Vineet

[1] 
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc
[2] https://gcc.gnu.org/pipermail/gcc-patches/2023-September/630713.html

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-09-27 21:11 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-18 19:45 RISC-V sign extension query Vineet Gupta
2023-09-18 20:10 ` Andrew Waterman
2023-09-18 20:18   ` Vineet Gupta
2023-09-18 21:39 ` Jeff Law
2023-09-19  2:41 ` Jeff Law
2023-09-19  3:19   ` PR 111/466 (was Re: RISC-V sign extension query) Vineet Gupta
2023-09-19  3:37   ` RISC-V sign extension query Vineet Gupta
2023-09-19 14:59     ` Jeff Law
2023-09-27  6:29       ` Vineet Gupta
2023-09-27 21:11         ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).