public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Vineet Gupta <vineetg@rivosinc.com>, gcc-patches@gcc.gnu.org
Cc: kito.cheng@gmail.com, Palmer Dabbelt <palmer@rivosinc.com>,
	gnu-toolchain@rivosinc.com
Subject: Re: [PATCH v2] RISC-V: zicond: Fix opt2 pattern
Date: Mon, 4 Sep 2023 17:16:12 -0600	[thread overview]
Message-ID: <629c9a26-a12e-46f8-b8aa-1d1f125cf116@gmail.com> (raw)
In-Reply-To: <20230901195311.761131-1-vineetg@rivosinc.com>



On 9/1/23 13:53, Vineet Gupta wrote:
> This was tripping up gcc.c-torture/execute/pr60003.c at -O1 since in
> failing case, pattern's asm czero.nez gets both rs2 and rs1 as non zero.
> 
> We start with the following src code snippet:
> 
>        if (a == 0)
> 	return 0;
>        else
> 	return x;
>      }
> 
> which is equivalent to:  "x = (a != 0) ? x : a" where x is NOT 0.
>                                                  ^^^^^^^^^^^^^^^^
> 
> and matches define_insn "*czero.nez.<GPR:mode><X:mode>.opt2"
> 
> | (insn 41 20 38 3 (set (reg/v:DI 136 [ x ])
> |        (if_then_else:DI (ne (reg/v:DI 134 [ a ])
> |                (const_int 0 [0]))
> |            (reg/v:DI 136 [ x ])
> |            (reg/v:DI 134 [ a ]))) {*czero.nez.didi.opt2}
> 
> The corresponding asm pattern generates
>      czero.nez x, x, a   ; %0, %2, %1
> 
> which implies
>      "x = (a != 0) ? 0 : a"
> 
> clearly not what the pattern wants to do.
> 
> Essentially "(a != 0) ? x : a" cannot be expressed with CZERO.nez if X
> is not guaranteed to be 0.
> 
> However this can be fixed with a small tweak
> 
> "x = (a != 0) ? x : a"
> 
>     is same as
> 
> "x = (a == 0) ? a : x" since middle operand is 0 when a == 0.
> 
> which can be expressed with CZERO.eqz
> 
> before fix			after fix
> -----------------		-----------------
> li        a5,1		        li        a5,1
> ld        a4,8(sp)	        ld        a4,8(sp)   # a4 is runtime non zero
> czero.nez a0,a4,a5 # a0=0 NOK 	czero.eqz a0,a4,a5   # a0=a4!=0 OK
> 
> The issue only happens at -O1 as at higher optimization levels, the
> whole conditional move gets optimized away.
> 
> This fixes 4 testsuite failues in a zicond build:
> 
> FAIL: gcc.c-torture/execute/pr60003.c   -O1  execution test
> FAIL: gcc.dg/setjmp-3.c execution test
> FAIL: gcc.dg/torture/stackalign/setjmp-3.c   -O1  execution test
> FAIL: gcc.dg/torture/stackalign/setjmp-3.c   -O1 -fpic execution test
> 
> gcc/ChangeLog:
> 	* config/riscv/zicond.md: Fix op2 pattern.
> 
> Fixes: 1d5bc3285e8a ("[committed][RISC-V] Fix 20010221-1.c with zicond")
> Signed-off-by: Vineet Gupta <vineetg@rivosinc.com>
OK.

Still not sure why I didn't trip over it in my own testing (execute.exp 
runs pr60003.c), but regardless, glad to have it fixed.

jeff

  reply	other threads:[~2023-09-04 23:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-01 19:53 Vineet Gupta
2023-09-04 23:16 ` Jeff Law [this message]
2023-09-05 15:20   ` [Committed] " Vineet Gupta
2023-09-05  2:19 ` [PATCH v2] " Tsukasa OI
2023-09-05  5:33   ` Jeff Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=629c9a26-a12e-46f8-b8aa-1d1f125cf116@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gnu-toolchain@rivosinc.com \
    --cc=kito.cheng@gmail.com \
    --cc=palmer@rivosinc.com \
    --cc=vineetg@rivosinc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).