From patchwork Fri Mar 5 14:27:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1447850 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DsVRv3PNdz9sW1 for ; Sat, 6 Mar 2021 01:27:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E1573951C10; Fri, 5 Mar 2021 14:27:05 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 779743858025 for ; Fri, 5 Mar 2021 14:27:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 779743858025 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mjambor@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 6110BAD21 for ; Fri, 5 Mar 2021 14:27:01 +0000 (UTC) From: Martin Jambor To: GCC Patches Subject: [PATCH] ipa: Check that scalar types that IPA-CP comes up with are sane (PR99122) User-Agent: Notmuch/0.31.3 (https://notmuchmail.org) Emacs/27.1 (x86_64-suse-linux-gnu) Date: Fri, 05 Mar 2021 15:27:01 +0100 Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-3039.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Jan Hubicka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, this patch fixes the last bit of PR 99122 where various bits of IPA infrastructure are presented with a program with type mismatches that make it have undefined behavior, and when inlining or performing IPA-CP, and encountering such mismatch, we basically try to VIEW_CONVERT_EXPR whatever the caller has into whatever the callee has or simply use an empty constructor if that cannot be done. This however does not work when the callee has VLA parameters because we ICE in the process. Richi has already disabled inlining for such cases, this patch avoids the issue in IPA-CP. It adds checks that whatever constant the propagation arrived at is actually compatible or fold_convertible to the callees formal parameer type. Unlike in the past, we now have types of all parameters of functions that we have analyzed, even with LTO, and so can do it. This should prevent only bogus propagations. I have looked at the effect of the patch on WPA of Firefox and did not have any. I have bootstrapped and LTO bootstrapped and tested the patch on x86_64-linux. OK for trunk? And perhaps later for GCC 10 too? Thanks gcc/ChangeLog: 2021-02-26 Martin Jambor PR ipa/99122 * ipa-cp.c (initialize_node_lattices): Mark as bottom all parameters with unknown type. (ipacp_value_safe_for_type): New function. (propagate_vals_across_arith_jfunc): Verify that the constant type can be used for a type of the formal parameter. (propagate_vals_across_ancestor): Likewise. (propagate_scalar_across_jump_function): Likewise. Pass the type also to propagate_vals_across_ancestor. gcc/testsuite/ChangeLog: 2021-02-26 Martin Jambor PR ipa/99122 * gcc.dg/pr99122-3.c: Remove -fno-ipa-cp from options. --- gcc/ipa-cp.c | 37 ++++++++++++++++++++++++++------ gcc/testsuite/gcc.dg/pr99122-3.c | 2 +- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c index 167913cb927..6041f75d824 100644 --- a/gcc/ipa-cp.c +++ b/gcc/ipa-cp.c @@ -1276,6 +1276,7 @@ initialize_node_lattices (struct cgraph_node *node) { ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); if (disable + || !ipa_get_type (info, i) || (pre_modified && (surviving_params.length () <= (unsigned) i || !surviving_params[i]))) { @@ -1304,6 +1305,21 @@ initialize_node_lattices (struct cgraph_node *node) } } +/* Return true if VALUE can be safely IPA-CP propagated to a parameter of type + PARAM_TYPE. */ + +static bool +ipacp_value_safe_for_type (tree param_type, tree value) +{ + tree val_type = TREE_TYPE (value); + if (param_type == val_type + || useless_type_conversion_p (param_type, val_type) + || fold_convertible_p (param_type, value)) + return true; + else + return false; +} + /* Return true iff X and Y should be considered equal values by IPA-CP. */ static bool @@ -2072,7 +2088,8 @@ propagate_vals_across_arith_jfunc (cgraph_edge *cs, { tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2, src_val, res_type); - if (!cstval) + if (!cstval + || !ipacp_value_safe_for_type (res_type, cstval)) break; ret |= dest_lat->add_value (cstval, cs, src_val, src_idx, @@ -2096,7 +2113,8 @@ propagate_vals_across_arith_jfunc (cgraph_edge *cs, tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2, src_val, res_type); - if (cstval) + if (cstval + && ipacp_value_safe_for_type (res_type, cstval)) ret |= dest_lat->add_value (cstval, cs, src_val, src_idx, src_offset); else @@ -2132,7 +2150,8 @@ static bool propagate_vals_across_ancestor (struct cgraph_edge *cs, struct ipa_jump_func *jfunc, ipcp_lattice *src_lat, - ipcp_lattice *dest_lat, int src_idx) + ipcp_lattice *dest_lat, int src_idx, + tree param_type) { ipcp_value *src_val; bool ret = false; @@ -2144,7 +2163,7 @@ propagate_vals_across_ancestor (struct cgraph_edge *cs, { tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value); - if (t) + if (t && ipacp_value_safe_for_type (param_type, t)) ret |= dest_lat->add_value (t, cs, src_val, src_idx); else ret |= dest_lat->set_contains_variable (); @@ -2169,7 +2188,10 @@ propagate_scalar_across_jump_function (struct cgraph_edge *cs, if (jfunc->type == IPA_JF_CONST) { tree val = ipa_get_jf_constant (jfunc); - return dest_lat->add_value (val, cs, NULL, 0); + if (ipacp_value_safe_for_type (param_type, val)) + return dest_lat->add_value (val, cs, NULL, 0); + else + return dest_lat->set_contains_variable (); } else if (jfunc->type == IPA_JF_PASS_THROUGH || jfunc->type == IPA_JF_ANCESTOR) @@ -2196,10 +2218,11 @@ propagate_scalar_across_jump_function (struct cgraph_edge *cs, if (jfunc->type == IPA_JF_PASS_THROUGH) ret = propagate_vals_across_pass_through (cs, jfunc, src_lat, - dest_lat, src_idx, param_type); + dest_lat, src_idx, + param_type); else ret = propagate_vals_across_ancestor (cs, jfunc, src_lat, dest_lat, - src_idx); + src_idx, param_type); if (src_lat->contains_variable) ret |= dest_lat->set_contains_variable (); diff --git a/gcc/testsuite/gcc.dg/pr99122-3.c b/gcc/testsuite/gcc.dg/pr99122-3.c index 6aa5b2912ca..6a908ad86c0 100644 --- a/gcc/testsuite/gcc.dg/pr99122-3.c +++ b/gcc/testsuite/gcc.dg/pr99122-3.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -g -fno-ipa-cp -w" } */ +/* { dg-options "-O2 -g -w" } */ static int foo ();