bpf: use xBPF signed div, mod insns when available
authorDavid Faust <david.faust@oracle.com>
Tue, 22 Sep 2020 18:31:35 +0000 (20:31 +0200)
committerJose E. Marchesi <jose.marchesi@oracle.com>
Tue, 22 Sep 2020 18:31:35 +0000 (20:31 +0200)
The 'mod' and 'div' operators in eBPF are unsigned, with no signed
counterpart. xBPF adds two new ALU operations, sdiv and smod, for
signed division and modulus, respectively. Update bpf.md with
'define_insn' blocks for signed div and mod to use them when targetting
xBPF, and add new tests to ensure they are used appropriately.

2020-09-17  David Faust  <david.faust@oracle.com>

gcc/
* config/bpf/bpf.md: Add defines for signed div and mod operators.

gcc/testsuite/
* gcc.target/bpf/diag-sdiv.c: New test.
* gcc.target/bpf/diag-smod.c: New test.
* gcc.target/bpf/xbpf-sdiv-1.c: New test.
* gcc.target/bpf/xbpf-smod-1.c: New test.

gcc/config/bpf/bpf.md
gcc/testsuite/gcc.target/bpf/diag-sdiv.c [new file with mode: 0644]
gcc/testsuite/gcc.target/bpf/diag-smod.c [new file with mode: 0644]
gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c [new file with mode: 0644]

index 769d8ea..8e7cf50 100644 (file)
   "div<msuffix>\t%0,%2"
   [(set_attr "type" "<mtype>")])
 
+;; However, xBPF does provide a signed division operator, sdiv.
+
+(define_insn "div<AM:mode>3"
+  [(set (match_operand:AM 0 "register_operand" "=r,r")
+        (div:AM (match_operand:AM 1 "register_operand" " 0,0")
+                (match_operand:AM 2 "reg_or_imm_operand" "r,I")))]
+  "TARGET_XBPF"
+  "sdiv<msuffix>\t%0,%2"
+  [(set_attr "type" "<mtype>")])
+
 ;;; Modulus
 
 ;; Note that eBPF doesn't provide instructions for signed integer
   "mod<msuffix>\t%0,%2"
   [(set_attr "type" "<mtype>")])
 
+;; Again, xBPF provides a signed version, smod.
+
+(define_insn "mod<AM:mode>3"
+  [(set (match_operand:AM 0 "register_operand" "=r,r")
+        (mod:AM (match_operand:AM 1 "register_operand" " 0,0")
+                (match_operand:AM 2 "reg_or_imm_operand" "r,I")))]
+  "TARGET_XBPF"
+  "smod<msuffix>\t%0,%2"
+  [(set_attr "type" "<mtype>")])
+
 ;;; Logical AND
 (define_insn "and<AM:mode>3"
   [(set (match_operand:AM 0 "register_operand" "=r,r")
diff --git a/gcc/testsuite/gcc.target/bpf/diag-sdiv.c b/gcc/testsuite/gcc.target/bpf/diag-sdiv.c
new file mode 100644 (file)
index 0000000..db0c494
--- /dev/null
@@ -0,0 +1,12 @@
+/* Verify signed division does not produce 'sdiv' insn in eBPF.  */
+/* { dg-do compile } */
+/* { dg-options "-O0" } */
+
+void
+foo ()
+{
+  signed int x = 5;
+  signed int y = 2;
+  signed int z = x / y;
+}
+/* { dg-final { scan-assembler-not "sdiv(32)?\t%r" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/diag-smod.c b/gcc/testsuite/gcc.target/bpf/diag-smod.c
new file mode 100644 (file)
index 0000000..20234ee
--- /dev/null
@@ -0,0 +1,12 @@
+/* Verify signed modulo does not produce 'smod' insn in eBPF.  */
+/* { dg-do compile } */
+/* { dg-options "-O0" } */
+
+void
+foo ()
+{
+  signed int x = 5;
+  signed int y = 2;
+  signed int z = x % y;
+}
+/* { dg-final { scan-assembler-not "smod(32)?\t%r" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c b/gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c
new file mode 100644 (file)
index 0000000..f6c5c9e
--- /dev/null
@@ -0,0 +1,14 @@
+/* Verify that sdiv instruction is used for xBPF. */
+/* { dg-do compile } */
+/* { dg-options "-O0 -mxbpf" } */
+
+void
+foo ()
+{
+  signed int x = 5;
+  signed int y = 2;
+  signed int z = x / y;
+  signed int w = x / 3;
+}
+
+/* { dg-final { scan-assembler "sdiv(32)?\t%r" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c b/gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c
new file mode 100644 (file)
index 0000000..b3e5816
--- /dev/null
@@ -0,0 +1,14 @@
+/* Verify that smod instruction is used for xBPF. */
+/* { dg-do compile } */
+/* { dg-options "-O0 -mxbpf" } */
+
+void
+foo ()
+{
+  signed int x = 5;
+  signed int y = 2;
+  signed int z = x % y;
+  signed int w = x % 3;
+}
+
+/* { dg-final { scan-assembler "smod(32)?\t%r" } } */