From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2017) id 8598E386075C; Thu, 25 Jan 2024 15:54:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8598E386075C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706198054; bh=PyAooKhg1dqwNhuGpStCHvNgvJO5HHn5ouOc4DAAmd8=; h=From:To:Subject:Date:From; b=QcbzgdzT6Ckb5GhR4KdsEQTiNlRsIsBb56JVe3pFBBrpK4sSZCYduvpIe84CASG6s mreNIGjfwFPicKq+NUoHbB2chsTYig7NGjvG0k3HFMky2eDRAAYH9qJQl7wEHHvyj0 WGYaA05iBdsno++tLgGnkBXDghzmVczrRPKEZI34= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Robin Dapp To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8430] fold-const: Handle AND, IOR, XOR with stepped vectors [PR112971]. X-Act-Checkin: gcc X-Git-Author: Robin Dapp X-Git-Refname: refs/heads/master X-Git-Oldrev: 90880e117aa70a5ecd9b7df4381410c2ea0dcfdb X-Git-Newrev: 660e17f00658b68115282e6de38243e3c6cc1ee2 Message-Id: <20240125155414.8598E386075C@sourceware.org> Date: Thu, 25 Jan 2024 15:54:14 +0000 (GMT) List-Id: https://gcc.gnu.org/g:660e17f00658b68115282e6de38243e3c6cc1ee2 commit r14-8430-g660e17f00658b68115282e6de38243e3c6cc1ee2 Author: Robin Dapp Date: Mon Jan 15 16:23:30 2024 +0100 fold-const: Handle AND, IOR, XOR with stepped vectors [PR112971]. Found in PR112971 this patch adds folding support for bitwise operations of const duplicate zero/one vectors with stepped vectors. On riscv we have the situation that a folding would perpetually continue without simplifying because e.g. {0, 0, 0, ...} & {7, 6, 5, ...} would not be folded to {0, 0, 0, ...}. gcc/ChangeLog: PR middle-end/112971 * fold-const.cc (simplify_const_binop): New function for binop simplification of two constant vectors when element-wise handling is not necessary. (const_binop): Call new function. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/pr112971.c: New test. Diff: --- gcc/fold-const.cc | 31 ++++++++++++++++++++++ .../gcc.target/riscv/rvv/autovec/pr112971.c | 18 +++++++++++++ 2 files changed, 49 insertions(+) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 1fd957288d4a..80e211e18c0e 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -1343,6 +1343,29 @@ distributes_over_addition_p (tree_code op, int opno) } } +/* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP + is the other operand. Try to use the value of OP to simplify the + operation in one step, without having to process individual elements. */ +static tree +simplify_const_binop (tree_code code, tree op, tree other_op, + int index ATTRIBUTE_UNUSED) +{ + /* AND, IOR as well as XOR with a zerop can be simplified directly. */ + if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST) + { + if (integer_zerop (other_op)) + { + if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR) + return op; + else if (code == BIT_AND_EXPR) + return other_op; + } + } + + return NULL_TREE; +} + + /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new constant. We assume ARG1 and ARG2 have the same data type, or at least are the same kind of constant and the same machine mode. Return zero if @@ -1646,6 +1669,14 @@ const_binop (enum tree_code code, tree arg1, tree arg2) return build_complex (type, real, imag); } + tree simplified; + if ((simplified = simplify_const_binop (code, arg1, arg2, 0))) + return simplified; + + if (commutative_tree_code (code) + && (simplified = simplify_const_binop (code, arg2, arg1, 1))) + return simplified; + if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112971.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112971.c new file mode 100644 index 000000000000..816ebd3c4939 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112971.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gcv_zvl256b -mabi=lp64d -O3 -fno-vect-cost-model" } */ + +int a; +short b[9]; +char c, d; +void e() { + d = 0; + for (;; d++) { + if (b[d]) + break; + a = 8; + for (; a >= 0; a--) { + char *f = &c; + *f &= d == (a & d); + } + } +}