]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[to-be-committed] [RISC-V] Improve (1 << N) | C for rv64
authorJeff Law <jlaw@ventanamicro.com>
Sun, 16 Jun 2024 14:36:27 +0000 (08:36 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Sun, 16 Jun 2024 14:37:56 +0000 (08:37 -0600)
Another improvement for generating Zbs instructions.

In this case we're looking at stuff like (1 << N) | C where N varies and C is a
single bit constant.

In this pattern the (1 << N) happens in SImode, but is zero extended out to
DImode before the bit manipulation.  The fact that we're modifying a DImode
object in the logical op is important as it means we don't have to worry about
whether or not the resulting value is sign extended from SI to DI.

This has run through Ventana's CI system.  I'll wait for it to roll through
pre-commit CI before moving forward.

gcc/
* config/riscv/bitmanip.md ((1 << N) | C): New splitter for IOR/XOR
of a single bit an a DImode object.

gcc/testsuite/

* gcc.target/riscv/zbs-zext.c: New test.

gcc/config/riscv/bitmanip.md
gcc/testsuite/gcc.target/riscv/zbs-zext.c [new file with mode: 0644]

index 4ee413c143e30aec6fe277e46bb4c147776f3ed4..0d35fb786e11eade6d10845c9901534bbf859076 100644 (file)
   "bseti\t%0,%1,%S2"
   [(set_attr "type" "bitmanip")])
 
+;; We can easily handle zero extensions
+(define_split
+  [(set (match_operand:DI 0 "register_operand")
+    (any_or:DI (zero_extend:DI
+                (ashift:SI (const_int 1)
+                           (match_operand:QI 1 "register_operand")))
+              (match_operand:DI 2 "single_bit_mask_operand")))
+   (clobber (match_operand:DI 3 "register_operand"))]
+  "TARGET_64BIT && TARGET_ZBS"
+  [(set (match_dup 3)
+        (match_dup 2))
+   (set (match_dup 0)
+     (any_or:DI (ashift:DI (const_int 1) (match_dup 1))
+               (match_dup 3)))])
+
 (define_insn "*bclr<mode>"
   [(set (match_operand:X 0 "register_operand" "=r")
        (and:X (rotate:X (const_int -2)
diff --git a/gcc/testsuite/gcc.target/riscv/zbs-zext.c b/gcc/testsuite/gcc.target/riscv/zbs-zext.c
new file mode 100644 (file)
index 0000000..5773b15
--- /dev/null
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbs -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-O1" } } */
+typedef unsigned long uint64_t;
+typedef unsigned int uint32_t;
+
+uint64_t bset (const uint32_t i)
+{
+  uint64_t checks = 8;
+  checks |= 1U << i;
+  return checks;
+}
+
+uint64_t binv (const uint32_t i)
+{
+  uint64_t checks = 8;
+  checks ^= 1U << i;
+  return checks;
+}
+
+uint64_t bclr (const uint32_t i)
+{
+  uint64_t checks = 10;
+  checks &= ~(1U << i);
+  return checks;
+}
+
+/* { dg-final { scan-assembler-times "bset\t" 1 } } */
+/* { dg-final { scan-assembler-times "binv\t" 1 } } */
+/* { dg-final { scan-assembler-times "bclr\t" 1 } } */
+/* { dg-final { scan-assembler-not "sllw\t"} } */