From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from userp2120.oracle.com (userp2120.oracle.com [156.151.31.85]) by sourceware.org (Postfix) with ESMTPS id 5FBFE398BC1B for ; Thu, 17 Sep 2020 17:15:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5FBFE398BC1B Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 08HHEQD7034655 for ; Thu, 17 Sep 2020 17:15:40 GMT Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2120.oracle.com with ESMTP id 33j91dvagb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 17 Sep 2020 17:15:40 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 08HHA5LR020194 for ; Thu, 17 Sep 2020 17:15:39 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserp3030.oracle.com with ESMTP id 33khpnchh5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 17 Sep 2020 17:15:39 +0000 Received: from abhmp0014.oracle.com (abhmp0014.oracle.com [141.146.116.20]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 08HHFbdA011545 for ; Thu, 17 Sep 2020 17:15:38 GMT Received: from sequence.us.oracle.com (/10.159.157.136) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 17 Sep 2020 17:15:36 +0000 From: David Faust To: gcc-patches@gcc.gnu.org Subject: [PATCH] bpf: use xBPF signed div, mod insns when available Date: Thu, 17 Sep 2020 10:15:30 -0700 Message-Id: <20200917171530.37619-1-david.faust@oracle.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9747 signatures=668679 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=1 mlxlogscore=999 phishscore=0 mlxscore=0 adultscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2009170128 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9747 signatures=668679 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 impostorscore=0 priorityscore=1501 malwarescore=0 suspectscore=1 mlxlogscore=999 clxscore=1015 adultscore=0 lowpriorityscore=0 spamscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2009170128 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Sep 2020 17:15:42 -0000 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 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 | 20 ++++++++++++++++++++ gcc/testsuite/gcc.target/bpf/diag-sdiv.c | 12 ++++++++++++ gcc/testsuite/gcc.target/bpf/diag-smod.c | 12 ++++++++++++ gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c | 14 ++++++++++++++ gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c | 14 ++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 gcc/testsuite/gcc.target/bpf/diag-sdiv.c create mode 100644 gcc/testsuite/gcc.target/bpf/diag-smod.c create mode 100644 gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md index 41bb4fcd9a7..ac87e3d9c53 100644 --- a/gcc/config/bpf/bpf.md +++ b/gcc/config/bpf/bpf.md @@ -160,6 +160,16 @@ (define_insn "udiv3" "div\t%0,%2" [(set_attr "type" "")]) +;; However, xBPF does provide a signed division operator, sdiv. + +(define_insn "div3" + [(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\t%0,%2" + [(set_attr "type" "")]) + ;;; Modulus ;; Note that eBPF doesn't provide instructions for signed integer @@ -173,6 +183,16 @@ (define_insn "umod3" "mod\t%0,%2" [(set_attr "type" "")]) +;; Again, xBPF provides a signed version, smod. + +(define_insn "mod3" + [(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\t%0,%2" + [(set_attr "type" "")]) + ;;; Logical AND (define_insn "and3" [(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 index 00000000000..db0c494a789 --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/diag-sdiv.c @@ -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 index 00000000000..20234ee39cc --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/diag-smod.c @@ -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 index 00000000000..f6c5c9e9f1c --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/xbpf-sdiv-1.c @@ -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 index 00000000000..b3e5816b5cf --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/xbpf-smod-1.c @@ -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" } } */ -- 2.26.2