From patchwork Fri Nov 11 13:31:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 18739 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp741483wru; Fri, 11 Nov 2022 05:32:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf4AyhlF6tRALRBP3f5zCm2Yb9at5aCjExl2XU0KL6NKH3d8SEzWTsEVxyyoyZcheQ1rkxk7 X-Received: by 2002:a17:906:6b82:b0:770:e23b:2604 with SMTP id l2-20020a1709066b8200b00770e23b2604mr1775924ejr.390.1668173519854; Fri, 11 Nov 2022 05:31:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668173519; cv=none; d=google.com; s=arc-20160816; b=A86P+1nAhDJBqX4OLK0R7+EwUyg9Fg8VB45Y7aCyAZrc7SWTZJuPm9IP8c4oeFvVLo U1FKJEnjW83FgaA2QnHr02I67R1kvYKWe1xmtEOaxuCyrmvTF2thcEhUox+fQ/sYFHgm wp6oS7K8Nnd7u35RNpifJzh811X3IqbdCLWoc2ZSwLtPnde4Xg8Ala2wMV3XBiRZP4nV U1LeomlgRHCsJacEyt1raCwE/r05hz4Bv9+qk1AEFekFvcSNKYgwLcuHkXIGzwuqH9Ly pbyk1ZgYkKiK206+MJz9yiZChLj81fuPm4aB8JceeiqrQOv5UQqCNx3WA3q/485kxZ5f Y+4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=yJTJ+G+/LUPYvAAdrlniEEOZRBdGs0eiiTuiVHco2T4=; b=FsAFT6oUAzP/x8DHmivJ13MfI8y8T9q9gN0KurAOhB9fOd38rzh6E3rEJd9Z6uDjic P0ViPFVg68qytUhUpUTOEurwXrT53PwuB95Lurm9VotMuPtxsNggMJeBvVNUD8pPP5Tm 2xERNmDAFWeOVGennfkG0M4soea7GMOJzRhgfwHEEQEamWnoG8BOzrJcfuczpOAoW+OL 5ABRir1Rz2l7sSQOdO0EnloM+0G+Nv6TN/LIBHtysbVvWLrkNvl4g+TZuyP6hbXc9bZf 3hg4Eu8IW9t2k6nb2uwsBlCFUHPb1dJMw/8gNIBgoQup22TVS6g0pNrfBO7UGEzTBZdr zPpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ArjEK13I; 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 dr22-20020a170907721600b007ad9adaf33dsi2268916ejc.372.2022.11.11.05.31.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 05:31:59 -0800 (PST) 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=ArjEK13I; 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 B54093858408 for ; Fri, 11 Nov 2022 13:31:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B54093858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668173518; bh=yJTJ+G+/LUPYvAAdrlniEEOZRBdGs0eiiTuiVHco2T4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ArjEK13IKPwnu79xIWYSdFGNqA7FfcMJKOaRc/AwpbMKp1M8lum+sRNVgLPoW4wYh ttiIhHYkgQYZeFvSIo7zQfyELtn1tBr94Ot3BIOHGzE7lFqTN63C+Wio75HER6fbqh MO8JJe4zMcIm02wgSzqoko3Yc3niXxmoypci2Ae8= 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 87D573858D1E for ; Fri, 11 Nov 2022 13:31:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 87D573858D1E 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) 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) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107618 - enhance copy propagation of constants MIME-Version: 1.0 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 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" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749206716311414730?= X-GMAIL-MSGID: =?utf-8?q?1749206716311414730?= 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. */