From patchwork Fri Oct 18 11:18:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 1999069 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XVMgb0fwjz1xth for ; Fri, 18 Oct 2024 22:20:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 43A993857C4F for ; Fri, 18 Oct 2024 11:20:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id DD2023858C3A for ; Fri, 18 Oct 2024 11:19:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD2023858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DD2023858C3A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729250355; cv=none; b=NC7cA87Y0AitJvBfVML2Coj6eJBGUodUb56ZT7CfGnKjXXZfWr6KYKQoaF+2CRjUGKoZty4wsspVjM6rSvl5lRBg9G2Rt9Q0qhKVVlzmPHcvSzk21DWo9RrFsneqzV0Q2hB3JcBFQ994nynUtFeMS70LW7VuNI0Fv7lsXyyCQj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729250355; c=relaxed/simple; bh=9wxFBSTr0aCQYLVG+D8CQRdc/yC93kB2ADgHKlrPsnw=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=GOsqwOy22QM97YNIeqXozVn5Kilms1Ii/57+F7zIwAFfpMdN2G/FZBqxt3IAfcadCynHBxf2rCZ9XTQZgUq/DbnXA7a4DQSNorPl26gICAZ10RJlIg33BLBC55QjKyE7jyC1UnTdWuqVAOlXtuXzsGt7+lOIkqiZThddqRc1r5g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 510F5FEC; Fri, 18 Oct 2024 04:19:36 -0700 (PDT) Received: from e121540-lin.manchester.arm.com (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1AD433F58B; Fri, 18 Oct 2024 04:19:05 -0700 (PDT) From: Richard Sandiford To: rguenther@suse.de, gcc-patches@gcc.gnu.org Cc: Richard Sandiford Subject: [PATCH 4/9] Simplify (X /[ex] C1) * (C1 * C2) -> X * C2 Date: Fri, 18 Oct 2024 12:18:01 +0100 Message-Id: <20241018111806.4026759-5-richard.sandiford@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241018111806.4026759-1-richard.sandiford@arm.com> References: <20241018111806.4026759-1-richard.sandiford@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-18.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ * match.pd: Simplify (X /[ex] C1) * (C1 * C2) -> X * C2. gcc/testsuite/ * gcc.dg/tree-ssa/mulexactdiv-1.c: New test. * gcc.dg/tree-ssa/mulexactdiv-2.c: Likewise. * gcc.dg/tree-ssa/mulexactdiv-3.c: Likewise. * gcc.dg/tree-ssa/mulexactdiv-4.c: Likewise. * gcc.target/aarch64/sve/cnt_fold_1.c: Likewise. * gcc.target/aarch64/sve/cnt_fold_2.c: Likewise. --- gcc/match.pd | 8 ++ gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-1.c | 23 ++++ gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-2.c | 19 +++ gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-3.c | 21 ++++ gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-4.c | 14 +++ .../gcc.target/aarch64/sve/cnt_fold_1.c | 110 ++++++++++++++++++ .../gcc.target/aarch64/sve/cnt_fold_2.c | 55 +++++++++ 7 files changed, 250 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-4.c create mode 100644 gcc/testsuite/gcc.target/aarch64/sve/cnt_fold_1.c create mode 100644 gcc/testsuite/gcc.target/aarch64/sve/cnt_fold_2.c diff --git a/gcc/match.pd b/gcc/match.pd index 1b1d38cf105..6677bc06d80 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see zerop initializer_each_zero_or_onep CONSTANT_CLASS_P + poly_int_tree_p tree_expr_nonnegative_p tree_expr_nonzero_p integer_valued_real_p @@ -5467,6 +5468,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) (convert @0)) +/* (X /[ex] C1) * (C1 * C2) -> X * C2. */ +(simplify + (mult (convert? (exact_div @0 INTEGER_CST@1)) poly_int_tree_p@2) + (with { poly_widest_int factor; } + (if (multiple_p (wi::to_poly_widest (@2), wi::to_widest (@1), &factor)) + (mult (convert @0) { wide_int_to_tree (type, factor); })))) + /* Simplify (A / B) * B + (A % B) -> A. */ (for div (trunc_div ceil_div floor_div round_div) mod (trunc_mod ceil_mod floor_mod round_mod) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-1.c b/gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-1.c new file mode 100644 index 00000000000..fa853eb7dff --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-1.c @@ -0,0 +1,23 @@ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ + +#define TEST_CMP(FN, DIV, MUL) \ + int \ + FN (int x) \ + { \ + if (x & 7) \ + __builtin_unreachable (); \ + x /= DIV; \ + return x * MUL; \ + } + +TEST_CMP (f1, 2, 6) +TEST_CMP (f2, 2, 10) +TEST_CMP (f3, 4, 80) +TEST_CMP (f4, 8, 200) + +/* { dg-final { scan-tree-dump-not {<[a-z]*_div_expr, } "optimized" } } */ +/* { dg-final { scan-tree-dump-not {> 1) & -2) +TEST_CMP (f2, int, 4, unsigned long, -8) +TEST_CMP (f3, int, 8, unsigned int, -24) +TEST_CMP (f4, long, 2, int, (~0U >> 1) & -2) +TEST_CMP (f5, long, 4, unsigned int, 100) +TEST_CMP (f6, long, 8, unsigned long, 200) + +/* { dg-final { scan-tree-dump-not {<[a-z]*_div_expr, } "optimized" } } */ +/* { dg-final { scan-tree-dump-not { + +/* +** f1: +** cntd x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f1 (int x) +{ + if (x & 1) + __builtin_unreachable (); + x /= 2; + return x * svcntw(); +} + +/* +** f2: +** cntd x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f2 (int x) +{ + if (x & 3) + __builtin_unreachable (); + x /= 4; + return x * svcnth(); +} + +/* +** f3: +** cntd x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f3 (int x) +{ + if (x & 7) + __builtin_unreachable (); + x /= 8; + return x * svcntb(); +} + +/* +** f4: +** cntw x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f4 (int x) +{ + if (x & 1) + __builtin_unreachable (); + x /= 2; + return x * svcnth(); +} + +/* +** f5: +** cntw x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f5 (int x) +{ + if (x & 3) + __builtin_unreachable (); + x /= 4; + return x * svcntb(); +} + +/* +** f6: +** cnth x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f6 (int x) +{ + if (x & 1) + __builtin_unreachable (); + x /= 2; + return x * svcntb(); +} + +/* +** f7: +** cntb x([0-9]+) +** mul w0, (w0, w\1|w\1, w0) +** ret +*/ +int +f7 (int x) +{ + if (x & 15) + __builtin_unreachable (); + x /= 16; + return x * svcntb() * 16; +} diff --git a/gcc/testsuite/gcc.target/aarch64/sve/cnt_fold_2.c b/gcc/testsuite/gcc.target/aarch64/sve/cnt_fold_2.c new file mode 100644 index 00000000000..7412b7b964e --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/cnt_fold_2.c @@ -0,0 +1,55 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include + +int +f1 (int x) +{ + x /= 2; + return x * svcntw(); +} + +int +f2 (int x) +{ + x /= 4; + return x * svcnth(); +} + +int +f3 (int x) +{ + x /= 8; + return x * svcntb(); +} + +int +f4 (int x) +{ + x /= 2; + return x * svcnth(); +} + +int +f5 (int x) +{ + x /= 4; + return x * svcntb(); +} + +int +f6 (int x) +{ + x /= 2; + return x * svcntb(); +} + +int +f7 (int x) +{ + x /= 16; + return x * svcntb() * 16; +} + +/* { dg-final { scan-assembler-times {\tasr\t} 7 } } */