]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH v3] RISC-V: Add split pattern to generate SFB instructions. [PR113095]
authorMonk Chiang <monk.chiang@sifive.com>
Wed, 24 Jan 2024 17:19:28 +0000 (10:19 -0700)
committerKito Cheng <kito.cheng@sifive.com>
Tue, 30 Apr 2024 09:13:06 +0000 (17:13 +0800)
Since the match.pd transforms (zero_one == 0) ? y : z <op> y,
into ((typeof(y))zero_one * z) <op> y. Add splitters to recongize
this expression to generate SFB instructions.

gcc/ChangeLog:
PR target/113095
* config/riscv/riscv.md: New splitters to rewrite single bit
sign extension as the condition to SFB instructions.

gcc/testsuite/ChangeLog:
* gcc.target/riscv/sfb.c: New test.
* gcc.target/riscv/pr113095.c: New test.

(cherry picked from commit fb54b9772816968032518d4008be5090e0d95109)

gcc/config/riscv/riscv.md
gcc/testsuite/gcc.target/riscv/pr113095.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/sfb.c [new file with mode: 0644]

index bc384d9aedf12a755c95197c8f07d60987580257..9a121c13d3b57c6f0d5e1e6e0df8390c9b96a65d 100644 (file)
    (set_attr "type" "sfb_alu")
    (set_attr "mode" "<GPR:MODE>")])
 
+;; Combine creates this form ((typeof(y))zero_one * z) <op> y
+;; for SiFive short forward branches.
+
+(define_split
+  [(set (match_operand:X 0 "register_operand")
+       (and:X (sign_extract:X (match_operand:X 1 "register_operand")
+                              (const_int 1)
+                              (match_operand 2 "immediate_operand"))
+              (match_operand:X 3 "register_operand")))
+   (clobber (match_operand:X 4 "register_operand"))]
+  "TARGET_SFB_ALU"
+  [(set (match_dup 4) (zero_extract:X (match_dup 1) (const_int 1) (match_dup 2)))
+   (set (match_dup 0) (if_then_else:X (ne (match_dup 4) (const_int 0))
+                                     (match_dup 3)
+                                     (const_int 0)))])
+
+(define_split
+  [(set (match_operand:X 0 "register_operand")
+       (and:X (sign_extract:X (match_operand:X 1 "register_operand")
+                              (const_int 1)
+                              (match_operand 2 "immediate_operand"))
+              (match_operand:X 3 "register_operand")))
+   (clobber (match_operand:X 4 "register_operand"))]
+  "TARGET_SFB_ALU && (UINTVAL (operands[2]) < 11)"
+  [(set (match_dup 4) (and:X (match_dup 1) (match_dup 2)))
+   (set (match_dup 0) (if_then_else:X (ne (match_dup 4) (const_int 0))
+                                     (match_dup 3)
+                                     (const_int 0)))]
+{
+  operands[2] = GEN_INT (1 << UINTVAL(operands[2]));
+})
+
 ;; Used to implement built-in functions.
 (define_expand "condjump"
   [(set (pc)
diff --git a/gcc/testsuite/gcc.target/riscv/pr113095.c b/gcc/testsuite/gcc.target/riscv/pr113095.c
new file mode 100644 (file)
index 0000000..04321b5
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=rv32gc -mabi=ilp32d -mtune=sifive-7-series" { target { rv32 } } } */
+/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -mtune=sifive-7-series" {target { rv64 } } } */
+
+extern void abort (void);
+extern void exit (int);
+
+unsigned short __attribute__ ((noinline, noclone))
+foo (unsigned short x) {
+  if (x == 1)
+    x ^= 0x4002;
+
+  return x;
+}
+
+int main () {
+  if (foo(1) != 0x4003)
+    abort ();
+
+  exit(0);
+}
diff --git a/gcc/testsuite/gcc.target/riscv/sfb.c b/gcc/testsuite/gcc.target/riscv/sfb.c
new file mode 100644 (file)
index 0000000..22f1640
--- /dev/null
@@ -0,0 +1,24 @@
+//* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc -mabi=ilp32d -mtune=sifive-7-series" } */
+
+int f1(unsigned int x, unsigned int y, unsigned int z)
+{
+  return ((x & 1) == 0) ? y : z ^ y;
+}
+
+int f2(unsigned int x, unsigned int y, unsigned int z)
+{
+  return ((x & 1) != 0) ? z ^ y : y;
+}
+
+int f3(unsigned int x, unsigned int y, unsigned int z)
+{
+  return ((x & 1) == 0) ? y : z | y;
+}
+
+int f4(unsigned int x, unsigned int y, unsigned int z)
+{
+  return ((x & 1) != 0) ? z | y : y;
+}
+/* { dg-final { scan-assembler-times "bne" 4 } } */
+/* { dg-final { scan-assembler-times "movcc" 4 } } */