0015-RISC-V-zicond-Fix-opt2-pattern.patch 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. From b62c6d08720ed3c4dafef314816e09d8769b373a Mon Sep 17 00:00:00 2001
  2. From: Vineet Gupta <vineetg@rivosinc.com>
  3. Date: Tue, 5 Sep 2023 07:55:07 -0700
  4. Subject: [PATCH 15/30] RISC-V: zicond: Fix opt2 pattern
  5. Fixes: 1d5bc3285e8a ("[committed][RISC-V] Fix 20010221-1.c with zicond")
  6. This was tripping up gcc.c-torture/execute/pr60003.c at -O1 since in
  7. failing case, pattern semantics were not matching with asm czero.nez
  8. We start with the following src code snippet:
  9. if (a == 0)
  10. return 0;
  11. else
  12. return x;
  13. }
  14. which is equivalent to: "x = (a != 0) ? x : a" where x is NOT 0.
  15. ^^^^^^^^^^^^^^^^
  16. and matches define_insn "*czero.nez.<GPR:mode><X:mode>.opt2"
  17. | (insn 41 20 38 3 (set (reg/v:DI 136 [ x ])
  18. | (if_then_else:DI (ne (reg/v:DI 134 [ a ])
  19. | (const_int 0 [0]))
  20. | (reg/v:DI 136 [ x ])
  21. | (reg/v:DI 134 [ a ]))) {*czero.nez.didi.opt2}
  22. The corresponding asm pattern generates
  23. czero.nez x, x, a ; %0, %2, %1
  24. which implies
  25. "x = (a != 0) ? 0 : a"
  26. clearly not what the pattern wants to do.
  27. Essentially "(a != 0) ? x : a" cannot be expressed with CZERO.nez if X
  28. is not guaranteed to be 0.
  29. However this can be fixed with a small tweak
  30. "x = (a != 0) ? x : a"
  31. is same as
  32. "x = (a == 0) ? a : x"
  33. and since middle operand is 0 when a == 0, it is equivalent to
  34. "x = (a == 0) ? 0 : x"
  35. which can be expressed with CZERO.eqz
  36. before fix after fix
  37. ----------------- -----------------
  38. li a5,1 li a5,1
  39. ld a4,8(sp) ld a4,8(sp)
  40. czero.nez a0,a4,a5 czero.eqz a0,a4,a5
  41. The issue only happens at -O1 as at higher optimization levels, the
  42. whole conditional move gets optimized away.
  43. This fixes 4 testsuite failues in a zicond build:
  44. FAIL: gcc.c-torture/execute/pr60003.c -O1 execution test
  45. FAIL: gcc.dg/setjmp-3.c execution test
  46. FAIL: gcc.dg/torture/stackalign/setjmp-3.c -O1 execution test
  47. FAIL: gcc.dg/torture/stackalign/setjmp-3.c -O1 -fpic execution test
  48. gcc/ChangeLog:
  49. * config/riscv/zicond.md: Fix op2 pattern.
  50. Signed-off-by: Vineet Gupta <vineetg@rivosinc.com>
  51. ---
  52. gcc/config/riscv/zicond.md | 2 +-
  53. 1 file changed, 1 insertion(+), 1 deletion(-)
  54. diff --git a/gcc/config/riscv/zicond.md b/gcc/config/riscv/zicond.md
  55. index 4619220ef8a..1721e1011ea 100644
  56. --- a/gcc/config/riscv/zicond.md
  57. +++ b/gcc/config/riscv/zicond.md
  58. @@ -60,7 +60,7 @@
  59. (match_operand:GPR 2 "register_operand" "r")
  60. (match_operand:GPR 3 "register_operand" "1")))]
  61. "TARGET_ZICOND && rtx_equal_p (operands[1], operands[3])"
  62. - "czero.nez\t%0,%2,%1"
  63. + "czero.eqz\t%0,%2,%1"
  64. )
  65. ;; Combine creates this form in some cases (particularly the coremark
  66. --
  67. 2.25.1