0024-RISC-V-Avoid-extraneous-EQ-or-NE-operation-in-cond-m.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. From d51ae2f0f825fddae4cc6a08a469f33aa72ce558 Mon Sep 17 00:00:00 2001
  2. From: "Maciej W. Rozycki" <macro@embecosm.com>
  3. Date: Wed, 22 Nov 2023 01:18:26 +0000
  4. Subject: [PATCH 24/30] RISC-V: Avoid extraneous EQ or NE operation in
  5. cond-move expansion
  6. In the non-zero case there is no need for the conditional value used by
  7. Ventana and Zicond integer conditional operations to be specifically 1.
  8. Regardless we canonicalize it by producing an extraneous conditional-set
  9. operation, such as with the sequence below:
  10. (insn 22 6 23 2 (set (reg:DI 141)
  11. (minus:DI (reg/v:DI 135 [ w ])
  12. (reg/v:DI 136 [ x ]))) 11 {subdi3}
  13. (nil))
  14. (insn 23 22 24 2 (set (reg:DI 140)
  15. (ne:DI (reg:DI 141)
  16. (const_int 0 [0]))) 307 {*sne_zero_didi}
  17. (nil))
  18. (insn 24 23 25 2 (set (reg:DI 143)
  19. (if_then_else:DI (eq:DI (reg:DI 140)
  20. (const_int 0 [0]))
  21. (const_int 0 [0])
  22. (reg:DI 13 a3 [ z ]))) 27913 {*czero.eqz.didi}
  23. (nil))
  24. (insn 25 24 26 2 (set (reg:DI 142)
  25. (if_then_else:DI (ne:DI (reg:DI 140)
  26. (const_int 0 [0]))
  27. (const_int 0 [0])
  28. (reg/v:DI 137 [ y ]))) 27914 {*czero.nez.didi}
  29. (nil))
  30. (insn 26 25 18 2 (set (reg/v:DI 138 [ z ])
  31. (ior:DI (reg:DI 142)
  32. (reg:DI 143))) 105 {iordi3}
  33. (nil))
  34. where insn 23 can well be removed without changing the semantics of the
  35. sequence. This is actually fixed up later on by combine and the insn
  36. does not make it to output meaning no SNEZ (or SEQZ in the reverse case)
  37. appears in the assembly produced, however it counts towards the cost of
  38. the sequence calculated by if-conversion, raising the trigger level for
  39. the branchless sequence to be chosen. Arguably to emit this extraneous
  40. operation it can be also considered rather sloppy of our backend's.
  41. Remove the check for operand 1 being constant 0 in the Ventana/Zicond
  42. case for equality comparisons then, observing that `riscv_zero_if_equal'
  43. called via `riscv_emit_int_compare' will canonicalize the comparison if
  44. required, removing the extraneous insn from output:
  45. (insn 22 6 23 2 (set (reg:DI 142)
  46. (minus:DI (reg/v:DI 135 [ w ])
  47. (reg/v:DI 136 [ x ]))) 11 {subdi3}
  48. (nil))
  49. (insn 23 22 24 2 (set (reg:DI 141)
  50. (if_then_else:DI (eq:DI (reg:DI 142)
  51. (const_int 0 [0]))
  52. (const_int 0 [0])
  53. (reg:DI 13 a3 [ z ]))) 27913 {*czero.eqz.didi}
  54. (nil))
  55. (insn 24 23 25 2 (set (reg:DI 140)
  56. (if_then_else:DI (ne:DI (reg:DI 142)
  57. (const_int 0 [0]))
  58. (const_int 0 [0])
  59. (reg/v:DI 137 [ y ]))) 27914 {*czero.nez.didi}
  60. (nil))
  61. (insn 25 24 18 2 (set (reg/v:DI 138 [ z ])
  62. (ior:DI (reg:DI 140)
  63. (reg:DI 141))) 105 {iordi3}
  64. (nil))
  65. while keeping actual assembly produced the same.
  66. Adjust branch costs across the test cases affected accordingly.
  67. gcc/
  68. * config/riscv/riscv.cc (riscv_expand_conditional_move): Remove
  69. the check for operand 1 being constant 0 in the Ventana/Zicond
  70. case for equality comparisons.
  71. gcc/testsuite/
  72. * gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_imm.c:
  73. Lower `-mbranch-cost=' setting.
  74. * gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_reg.c:
  75. Likewise.
  76. * gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_reg_reg.c:
  77. Likewise.
  78. * gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_imm.c:
  79. Likewise.
  80. * gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_reg.c:
  81. Likewise.
  82. * gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_reg_reg.c:
  83. Likewise.
  84. ---
  85. gcc/config/riscv/riscv.cc | 6 +++---
  86. .../zicond-primitiveSemantics_compare_imm_return_imm_imm.c | 4 ++--
  87. .../zicond-primitiveSemantics_compare_imm_return_imm_reg.c | 4 ++--
  88. .../zicond-primitiveSemantics_compare_imm_return_reg_reg.c | 4 ++--
  89. .../zicond-primitiveSemantics_compare_reg_return_imm_imm.c | 4 ++--
  90. .../zicond-primitiveSemantics_compare_reg_return_imm_reg.c | 4 ++--
  91. .../zicond-primitiveSemantics_compare_reg_return_reg_reg.c | 4 ++--
  92. 7 files changed, 15 insertions(+), 15 deletions(-)
  93. diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
  94. index cb2408b375d..4784fa051e1 100644
  95. --- a/gcc/config/riscv/riscv.cc
  96. +++ b/gcc/config/riscv/riscv.cc
  97. @@ -3580,9 +3580,9 @@ riscv_expand_conditional_move (rtx dest, rtx op, rtx cons, rtx alt)
  98. return false;
  99. /* Canonicalize the comparison. It must be an equality comparison
  100. - against 0. If it isn't, then emit an SCC instruction so that
  101. - we can then use an equality comparison against zero. */
  102. - if (!equality_operator (op, VOIDmode) || op1 != CONST0_RTX (mode))
  103. + of integer operands. If it isn't, then emit an SCC instruction
  104. + so that we can then use an equality comparison against zero. */
  105. + if (!equality_operator (op, VOIDmode) || !INTEGRAL_MODE_P (mode0))
  106. {
  107. enum rtx_code new_code = NE;
  108. bool *invert_ptr = 0;
  109. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_imm.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_imm.c
  110. index d6d3f013e9b..4015220283e 100644
  111. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_imm.c
  112. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_imm.c
  113. @@ -1,6 +1,6 @@
  114. /* { dg-do compile } */
  115. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  116. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  117. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=3" { target { rv64 } } } */
  118. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=3" { target { rv32 } } } */
  119. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  120. long primitiveSemantics_compare_imm_return_imm_imm_00(long a, long b) {
  121. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_reg.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_reg.c
  122. index 4c14e892bac..dd3665c432e 100644
  123. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_reg.c
  124. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_imm_reg.c
  125. @@ -1,6 +1,6 @@
  126. /* { dg-do compile } */
  127. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  128. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  129. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=3" { target { rv64 } } } */
  130. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=3" { target { rv32 } } } */
  131. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  132. long primitiveSemantics_compare_imm_return_imm_reg_00(long a, long b) {
  133. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_reg_reg.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_reg_reg.c
  134. index e95318301bf..19ca4a44f04 100644
  135. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_reg_reg.c
  136. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_imm_return_reg_reg.c
  137. @@ -1,6 +1,6 @@
  138. /* { dg-do compile } */
  139. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=5" { target { rv64 } } } */
  140. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=5" { target { rv32 } } } */
  141. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  142. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  143. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  144. long primitiveSemantics_compare_imm_return_reg_reg_00(long a, long b, long c) {
  145. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_imm.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_imm.c
  146. index cafdf79537d..e670d1d8fdf 100644
  147. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_imm.c
  148. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_imm.c
  149. @@ -1,6 +1,6 @@
  150. /* { dg-do compile } */
  151. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  152. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  153. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=3" { target { rv64 } } } */
  154. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=3" { target { rv32 } } } */
  155. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  156. long primitiveSemantics_compare_reg_return_imm_imm_00(long a, long b, long c) {
  157. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_reg.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_reg.c
  158. index dda9a58c388..e25487cc4bd 100644
  159. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_reg.c
  160. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_imm_reg.c
  161. @@ -1,6 +1,6 @@
  162. /* { dg-do compile } */
  163. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  164. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  165. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=3" { target { rv64 } } } */
  166. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=3" { target { rv32 } } } */
  167. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  168. long primitiveSemantics_compare_reg_return_imm_reg_00(long a, long b, long c) {
  169. diff --git a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_reg_reg.c b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_reg_reg.c
  170. index 0c2db3ca59d..765c0e0ade7 100644
  171. --- a/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_reg_reg.c
  172. +++ b/gcc/testsuite/gcc.target/riscv/zicond-primitiveSemantics_compare_reg_return_reg_reg.c
  173. @@ -1,6 +1,6 @@
  174. /* { dg-do compile } */
  175. -/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=5" { target { rv64 } } } */
  176. -/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=5" { target { rv32 } } } */
  177. +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
  178. +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
  179. /* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
  180. long primitiveSemantics_compare_reg_return_reg_reg_00(long a, long b, long c,
  181. --
  182. 2.25.1