From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 87D573858D1E for ; Fri, 11 Nov 2022 13:31:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 87D573858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 97444225DF for ; Fri, 11 Nov 2022 13:31:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1668173472; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=yJTJ+G+/LUPYvAAdrlniEEOZRBdGs0eiiTuiVHco2T4=; b=Nyt2vfnDheVrkq/YIfGsvvRMRdLu/jt5kAgBaqaWZfUzYgljks0AK039keivmfavshM6Zx qNjzbmHO06wnN8WvHnABD8eYiKprkuPZXnTmRD1TByQ+roZ7wSLC2pBijnfFl9zXgVH6x+ V7UX1JRw/uFfuXM6GiHsOIM8wWrdvtc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1668173472; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=yJTJ+G+/LUPYvAAdrlniEEOZRBdGs0eiiTuiVHco2T4=; b=kJ5KR3FS5lK7RC2tBsC3vY59aXV23Gai5xp9yFZ09+GttnbSTU8GpaavD6p0OhsK/UBako zQTPKBb+c8usBJBg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 85A4A13273 for ; Fri, 11 Nov 2022 13:31:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 5BykH6BObmNGbgAAMHmgww (envelope-from ) for ; Fri, 11 Nov 2022 13:31:12 +0000 Date: Fri, 11 Nov 2022 14:31:12 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107618 - enhance copy propagation of constants MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20221111133112.85A4A13273@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,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 List-Id: The following enhances copy propagation of constants to also see through simple operations like conversions but also operations with otherwise constant operands. That's required to fulfill the promise /* Copy propagation also copy-propagates constants, this is necessary to forward object-size and builtin folding results properly. */ NEXT_PASS (pass_copy_prop); and avoid false diagnostics as shown in the testcase. We're using gimple_fold_stmt_to_constant_1 with not following SSA edges and accordingly adjust what stmts we simulate during SSA propagation. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/107618 * tree-sssa-copy.cc (stmt_may_generate_copy): Simulate all assignments with a single SSA use. (copy_prop_visit_assignment): Use gimple_fold_stmt_to_constant_1 to perform simple constant folding. (copy_prop::visit_stmt): Visit all assignments. * gcc.dg/pr107618.c: New testcase. --- gcc/testsuite/gcc.dg/pr107618.c | 10 +++++++ gcc/tree-ssa-copy.cc | 49 +++++++++++++++++---------------- 2 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr107618.c diff --git a/gcc/testsuite/gcc.dg/pr107618.c b/gcc/testsuite/gcc.dg/pr107618.c new file mode 100644 index 00000000000..9e73cc1f1a1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr107618.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-Og" } */ + +void a(void) __attribute__((__warning__(""))); +int main(void) +{ + unsigned long b = __builtin_object_size(0, 0); + if (__builtin_expect(b < 1, 0)) + a(); /* { dg-bogus "warning" } */ +} diff --git a/gcc/tree-ssa-copy.cc b/gcc/tree-ssa-copy.cc index 782ceb500cc..811161c223e 100644 --- a/gcc/tree-ssa-copy.cc +++ b/gcc/tree-ssa-copy.cc @@ -33,6 +33,7 @@ along with GCC; see the file COPYING3. If not see #include "cfgloop.h" #include "tree-scalar-evolution.h" #include "tree-ssa-loop-niter.h" +#include "gimple-fold.h" /* This file implements the copy propagation pass and provides a @@ -99,12 +100,16 @@ stmt_may_generate_copy (gimple *stmt) if (gimple_vuse (stmt)) return false; + /* If the assignment is from a constant it generates a useful copy. */ + if (gimple_assign_single_p (stmt) + && is_gimple_min_invariant (gimple_assign_rhs1 (stmt))) + return true; + /* Otherwise, the only statements that generate useful copies are - assignments whose RHS is just an SSA name that doesn't flow - through abnormal edges. */ - return ((gimple_assign_rhs_code (stmt) == SSA_NAME - && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))) - || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))); + assignments whose single SSA use doesn't flow through abnormal + edges. */ + tree rhs = single_ssa_tree_operand (stmt, SSA_OP_USE); + return (rhs && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs)); } @@ -197,26 +202,24 @@ dump_copy_of (FILE *file, tree var) static enum ssa_prop_result copy_prop_visit_assignment (gimple *stmt, tree *result_p) { - tree lhs, rhs; - - lhs = gimple_assign_lhs (stmt); - rhs = valueize_val (gimple_assign_rhs1 (stmt)); - - if (TREE_CODE (lhs) == SSA_NAME) + tree lhs = gimple_assign_lhs (stmt); + tree rhs = gimple_fold_stmt_to_constant_1 (stmt, valueize_val); + if (rhs + && (TREE_CODE (rhs) == SSA_NAME + || is_gimple_min_invariant (rhs))) { - /* Straight copy between two SSA names. First, make sure that + /* Straight copy between two SSA names or a constant. Make sure that we can propagate the RHS into uses of LHS. */ if (!may_propagate_copy (lhs, rhs)) - return SSA_PROP_VARYING; - - *result_p = lhs; - if (set_copy_of_val (*result_p, rhs)) - return SSA_PROP_INTERESTING; - else - return SSA_PROP_NOT_INTERESTING; + rhs = lhs; } + else + rhs = lhs; - return SSA_PROP_VARYING; + *result_p = lhs; + if (set_copy_of_val (*result_p, rhs)) + return SSA_PROP_INTERESTING; + return rhs != lhs ? SSA_PROP_NOT_INTERESTING : SSA_PROP_VARYING; } @@ -282,10 +285,8 @@ copy_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *result_p) fprintf (dump_file, "\n"); } - if (gimple_assign_single_p (stmt) - && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME - && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME - || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))) + if (is_gimple_assign (stmt) + && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME) { /* If the statement is a copy assignment, evaluate its RHS to see if the lattice value of its output has changed. */ -- 2.35.3