]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Allow const0_rtx operand in max/min
authorLin Sinan <sinan.lin@linux.alibaba.com>
Sun, 5 Mar 2023 08:09:50 +0000 (16:09 +0800)
committerKito Cheng <kito.cheng@sifive.com>
Sun, 5 Mar 2023 08:59:16 +0000 (16:59 +0800)
Optimize cases that use max[u]/min[u] against a zero constant.

E.g., the case int f(int x) { return x >= 0 ? x : 0; }
the current asm output in rv64gc_zba_zbb

 li rtmp,0
 max a0,a0,rtmp

could be optimized into

 max a0,a0,zero

gcc/ChangeLog:
* config/riscv/bitmanip.md: allow 0 constant in max/min
pattern.

gcc/testsuite/ChangeLog:
* gcc.target/riscv/zbb-min-max-03.c: New test.

gcc/config/riscv/bitmanip.md
gcc/testsuite/gcc.target/riscv/zbb-min-max-03.c [new file with mode: 0644]

index 58a86bd929f1305715800b5e43d6b66679a64758..cfdb9eb4689b6902779791a91ff38180cdee5185 100644 (file)
 (define_insn "<bitmanip_optab><mode>3"
   [(set (match_operand:X 0 "register_operand" "=r")
         (bitmanip_minmax:X (match_operand:X 1 "register_operand" "r")
-                          (match_operand:X 2 "register_operand" "r")))]
+                          (match_operand:X 2 "reg_or_0_operand" "rJ")))]
   "TARGET_ZBB"
-  "<bitmanip_insn>\t%0,%1,%2"
+  "<bitmanip_insn>\t%0,%1,%z2"
   [(set_attr "type" "bitmanip")])
 
 ;; Optimize the common case of a SImode min/max against a constant
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-min-max-03.c b/gcc/testsuite/gcc.target/riscv/zbb-min-max-03.c
new file mode 100644 (file)
index 0000000..c7de100
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zba_zbb -mabi=lp64d" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" } } */
+
+int f(int x) {
+ return x >= 0 ? x : 0;
+}
+
+/* { dg-final { scan-assembler-times "max\t" 1 } } */
+/* { dg-final { scan-assembler-not "li\t" } } */