From patchwork Thu Apr 20 12:03:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 85882 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp278912vqo; Thu, 20 Apr 2023 05:03:52 -0700 (PDT) X-Google-Smtp-Source: AKy350bgEPfYvws3MLQdeGGu8HuaiHsF8K9f9TLGC+CJNhS0aJuOQBqpi0oeeUV+MtjMNQs7ommy X-Received: by 2002:a05:6402:38f:b0:508:3b23:d84c with SMTP id o15-20020a056402038f00b005083b23d84cmr1647343edv.1.1681992232637; Thu, 20 Apr 2023 05:03:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681992232; cv=none; d=google.com; s=arc-20160816; b=XkhBo7isBCglMGoFFD+bn/tU7ugaaxlhG5NqjC5qOWrKgrtFes2ZReaflopyEGV7lt rI/iY9ampN5HEJpZPOsEufpq9fSGaZ2+A+nsRh2mgYberVVOs50DwVBE3OEAfbR1sefL OQFES+KlSyUk2guEAjYPTdiUvjKVDamGFNiAaNf7nWM9+eQbzRFENtxKZtR0FKuw9HjI MhtJCwucvGLTRDSrFyknsWIk33RyIf3re3cCMlaXXxZRytavEXakfpv+kL4CuewdYUyF CBmUOHR6czf6YfDkOx+FvAbzO0N/P2Ykd5gzzDZnFxP5DIfcIcOAVgOd7Q36y3CVFUfJ /FvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=Ow1aQ9hhUTvxXQgEzMBOCPX36yqhhZQFPLUGXY2rEyM=; b=q9FVec6kHRcEsnPxt2W8Y56ZjAtlFLvBSoMk5tcdW2WKO0v+ukafVop5AXnME5aGzW rKBlaj/mGzLR0K24AIlpiaoTRsXpnCYK+0oCKe+dzUZms6MtsqF9AmDy3hVBHE7UwFy1 +zW1UH314wqflVaGwFRd/NL7msBG00xm6jfP8r+xfxdw+1SVLMmB2NJTurjCzf5Yq3xb XA7/N5BYkbJ64RMolBKyZiyZvJ+N1J6YrG7e9dx+ddgnOKvpxwIgrem4OzNzenV6twli RVykU4PkukzE+OP0THH5IpyHCq99sNqw2h19AyrVOTnltPurmQUY9s8qG3tuWstoTkT5 HP+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=I4UfkVms; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id k7-20020a056402048700b0050489e43fd7si1548837edv.29.2023.04.20.05.03.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 05:03:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=I4UfkVms; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 918CE3857716 for ; Thu, 20 Apr 2023 12:03:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 918CE3857716 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681992231; bh=Ow1aQ9hhUTvxXQgEzMBOCPX36yqhhZQFPLUGXY2rEyM=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=I4UfkVms7k0AyNfzG7/VVTMo3yM4fnqreN89DjHRxSocJH+3+k8NzIK2tPS9Nkv+v G8DuMfI2hs29x9wDOzTDz+TP53/erUzOE50KaTMVaudU5v4nooSjreuaDFF0PhkbFJ jKxrGGZbSz7gfd+/IFfIUqVuCD1jIFsTs+clwLB8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 2F6653858D37 for ; Thu, 20 Apr 2023 12:03:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F6653858D37 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 57190219E8; Thu, 20 Apr 2023 12:03:06 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 10CE62C141; Thu, 20 Apr 2023 12:03:06 +0000 (UTC) Date: Thu, 20 Apr 2023 12:03:05 +0000 (UTC) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek , amacleod@redhat.com Subject: [PATCH] tree-optimization/109564 - avoid equivalences from PHIs in most cases User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230420120351.918CE3857716@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763696687371363123?= X-GMAIL-MSGID: =?utf-8?q?1763696687371363123?= The following avoids registering two-way equivalences from PHIs when UNDEFINED arguments are involved. Bootstrapped and tested on x86_64-unknown-linux-gnu. As noted this causes missed optimizations for the cases where we have unreachable edges rather than UNDEFINED ranges. OK for trunk / branch? Do we want to try tackle the unreachable edges case separately? Thanks, Richard. PR tree-optimization/109564 * gimple-range-fold.cc (fold_using_range::range_of_phi): Track whether we've seen any UNDEFINED argument and avoid registering equivalences in that case. * gcc.dg/torture/pr109564-1.c: New testcase. * gcc.dg/torture/pr109564-2.c: Likewise. * gcc.dg/tree-ssa/evrp-ignore.c: Likewise. * gcc.dg/tree-ssa/vrp06.c: Likewise. --- gcc/gimple-range-fold.cc | 27 +++----- gcc/testsuite/gcc.dg/torture/pr109564-1.c | 74 +++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr109564-2.c | 33 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/evrp-ignore.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/vrp06.c | 2 +- 5 files changed, 119 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr109564-1.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr109564-2.c diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc index 429734f954a..d6b79600cea 100644 --- a/gcc/gimple-range-fold.cc +++ b/gcc/gimple-range-fold.cc @@ -743,6 +743,7 @@ fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src) // Track if all executable arguments are the same. tree single_arg = NULL_TREE; bool seen_arg = false; + bool seen_undefined = false; // Start with an empty range, unioning in each argument's range. r.set_undefined (); @@ -781,6 +782,8 @@ fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src) else if (single_arg != arg) single_arg = NULL_TREE; } + else + seen_undefined = true; // Once the value reaches varying, stop looking. if (r.varying_p () && single_arg == NULL_TREE) @@ -798,23 +801,13 @@ fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src) // Symbolic arguments can be equivalences. if (gimple_range_ssa_p (single_arg)) { - // Only allow the equivalence if the PHI definition does not - // dominate any incoming edge for SINGLE_ARG. - // See PR 108139 and 109462. - basic_block bb = gimple_bb (phi); - if (!dom_info_available_p (CDI_DOMINATORS)) - single_arg = NULL; - else - for (x = 0; x < gimple_phi_num_args (phi); x++) - if (gimple_phi_arg_def (phi, x) == single_arg - && dominated_by_p (CDI_DOMINATORS, - gimple_phi_arg_edge (phi, x)->src, - bb)) - { - single_arg = NULL; - break; - } - if (single_arg) + // Only allow the equivalence if there isn't any UNDEFINED + // argument we ignored. Such equivalences are one way + // PHIDEF == name, but name == PHIDEF might not hold. + // See PR 108139, 109462 and 109564. + // ??? This misses cases with not executable edges such + // as gcc.dg/tree-ssa/vrp06.c + if (!seen_undefined) src.register_relation (phi, VREL_EQ, phi_def, single_arg); } else if (src.get_operand (arg_range, single_arg) diff --git a/gcc/testsuite/gcc.dg/torture/pr109564-1.c b/gcc/testsuite/gcc.dg/torture/pr109564-1.c new file mode 100644 index 00000000000..e7c855f1edf --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr109564-1.c @@ -0,0 +1,74 @@ +/* { dg-do run } */ + +struct libkeccak_spec { + long int bitrate; +}; + +struct libkeccak_generalised_spec { + long int bitrate; + long int state_size; + long int word_size; +}; + +int __attribute__((noipa)) +libkeccak_degeneralise_spec(struct libkeccak_generalised_spec *restrict spec, + struct libkeccak_spec *restrict output_spec) +{ + long int state_size, word_size, bitrate, output; + const int have_state_size = spec->state_size != (-65536L); + const int have_word_size = spec->word_size != (-65536L); + const int have_bitrate = spec->bitrate != (-65536L); + + if (have_state_size) + { + state_size = spec->state_size; + if (state_size <= 0) + return 1; + if (state_size > 1600) + return 2; + } + + if (have_word_size) + { + word_size = spec->word_size; + if (word_size <= 0) + return 4; + if (word_size > 64) + return 5; + if (have_state_size && state_size != word_size * 25) + return 6; + else if (!have_state_size) { + spec->state_size = 1; + state_size = word_size * 25; + } + } + + if (have_bitrate) + bitrate = spec->bitrate; + + if (!have_bitrate) + { + state_size = (have_state_size ? state_size : (1600L)); + output = ((state_size << 5) / 100L + 7L) & ~0x07L; + bitrate = output << 1; + } + + output_spec->bitrate = bitrate; + + return 0; +} + +int main () +{ + struct libkeccak_generalised_spec gspec; + struct libkeccak_spec spec; + spec.bitrate = -1; + gspec.bitrate = -65536; + gspec.state_size = -65536; + gspec.word_size = -65536; + if (libkeccak_degeneralise_spec(&gspec, &spec)) + __builtin_abort (); + if (spec.bitrate != 1024) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr109564-2.c b/gcc/testsuite/gcc.dg/torture/pr109564-2.c new file mode 100644 index 00000000000..eeab437c0b3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr109564-2.c @@ -0,0 +1,33 @@ +/* { dg-do run } */ + +struct libkeccak_generalised_spec { + int state_size; + int word_size; +} main_gspec; + +long gvar; + +int libkeccak_degeneralise_spec(struct libkeccak_generalised_spec *spec) +{ + int state_size; + int have_state_size = spec->state_size != -1; + int have_word_size = spec->word_size; + + if (have_state_size) + state_size = spec->state_size; + if (have_word_size) + gvar = 12345; + if (have_state_size && state_size != spec->word_size) + return 1; + if (spec) + gvar++; + return 0; +} + +int main() +{ + main_gspec.state_size = -1; + if (libkeccak_degeneralise_spec(&main_gspec)) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp-ignore.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp-ignore.c index 9bfaed6a50a..ee93e5ad9f6 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/evrp-ignore.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp-ignore.c @@ -25,4 +25,4 @@ void foo (int x, int y, int z) kill(); } -/* { dg-final { scan-tree-dump-not "kill" "evrp" } } */ +/* { dg-final { scan-tree-dump-not "kill" "evrp" { xfail *-*-* } } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp06.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp06.c index 898477e42fb..8f5f86021c8 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp06.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp06.c @@ -30,4 +30,4 @@ foo (int i, int j, int a) /* { dg-final { scan-tree-dump-times "Folding predicate \[i|j\]_\[0-9\]+.*0 to 0" 1 "vrp1" } } */ /* { dg-final { scan-tree-dump-times "Folding predicate \[i|j\]_\[0-9\]+.*0 to 1" 1 "vrp1" } } */ -/* { dg-final { scan-tree-dump-times "Folding predicate i_\[0-9]+.*j_\[0-9\]+.* to 0" 1 "vrp1" } } */ +/* { dg-final { scan-tree-dump-times "Folding predicate i_\[0-9]+.*j_\[0-9\]+.* to 0" 1 "vrp1" { xfail *-*-* } } } */