From patchwork Thu Apr 27 11:59:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1774430 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=PwDT4+GL; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q6Z760vLdz23v8 for ; Thu, 27 Apr 2023 22:00:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C1843856948 for ; Thu, 27 Apr 2023 12:00:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C1843856948 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682596815; bh=cQUeT+XE24RuHALTajk/p9GEsPpfK0p6xH9R5+PUzRc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=PwDT4+GLJGhbul4zfY0nafHOcSW04ETsjacjTuKkHc7h7LyHaPOrA0krj5VOXGTET YdGgAuW+GSD1r5ZqppKqheba3OdqXYzFgL3Jm84sYUsAu59KX+lIIVwnMJD/M04Y4i FaXCwc2AZneCdCrTpeQM00gkNJAlrzUduzGf9zpk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 16AAA3858D33 for ; Thu, 27 Apr 2023 11:59:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 16AAA3858D33 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-674-JOOVIdgBOk2NsLqgFTaFEw-1; Thu, 27 Apr 2023 07:59:52 -0400 X-MC-Unique: JOOVIdgBOk2NsLqgFTaFEw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 08D0B3828887 for ; Thu, 27 Apr 2023 11:59:52 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.195.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A609440C201F; Thu, 27 Apr 2023 11:59:51 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 33RBxnkY480761 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 27 Apr 2023 13:59:49 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 33RBxnG6480760; Thu, 27 Apr 2023 13:59:49 +0200 To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] Normalize addresses in IPA before calling range_op_handler [PR109639] Date: Thu, 27 Apr 2023 13:59:48 +0200 Message-Id: <20230427115948.480747-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The old legacy code would allow building ranges containing symbolics, even though the entire ranger ecosystem does not handle them. These were normalized into non-zero ranges by helper functions in VRP (range_fold_*_expr) before calling the ranger. The only users of these functions should have been legacy VRP, which is no more. However, a handful of users crept into IPA, even though these functions shouldn't never been called outside of VRP or vr-values. The issue here is that IPA is building a range of [&foo, &foo] and expecting range_fold_binary to normalize it to non-zero. Fixed by adding a helper function before calling the range_op handler. I think these covers the problematic ranges. If not, I'll come up with something more generalized that does not involve polluting irange::set with the normalization code. After all, this only involves a handful of IPA places. I've also added an assert in irange::set() making it easier to detect any possible fallout without having to drill deep into the setter. gcc/ChangeLog: PR tree-optimization/109639 * ipa-cp.cc (ipa_value_range_from_jfunc): Normalize range. (propagate_vr_across_jump_function): Same. * ipa-fnsummary.cc (evaluate_conditions_for_known_args): Same. * ipa-prop.h (ipa_range_set_and_normalize): New. * value-range.cc (irange::set): Assert min and max are INTEGER_CST. --- gcc/ipa-cp.cc | 8 ++++++-- gcc/ipa-fnsummary.cc | 10 +++++----- gcc/ipa-prop.h | 14 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr109639.c | 20 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr109643.c | 18 ++++++++++++++++++ gcc/value-range.cc | 3 +++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109639.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109643.c diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc index 9ec86d77992..a5b45a8e6b9 100644 --- a/gcc/ipa-cp.cc +++ b/gcc/ipa-cp.cc @@ -1963,9 +1963,11 @@ ipa_value_range_from_jfunc (ipa_node_params *info, cgraph_edge *cs, { value_range op_res, res; tree op = ipa_get_jf_pass_through_operand (jfunc); - value_range op_vr (op, op); + value_range op_vr; range_op_handler handler (operation, vr_type); + ipa_range_set_and_normalize (op_vr, op); + if (!handler || !op_res.supports_type_p (vr_type) || !handler.fold_range (op_res, vr_type, srcvr, op_vr)) @@ -2757,10 +2759,12 @@ propagate_vr_across_jump_function (cgraph_edge *cs, ipa_jump_func *jfunc, else if (!ipa_edge_within_scc (cs)) { tree op = ipa_get_jf_pass_through_operand (jfunc); - value_range op_vr (op, op); + value_range op_vr; value_range op_res,res; range_op_handler handler (operation, operand_type); + ipa_range_set_and_normalize (op_vr, op); + if (!handler || !op_res.supports_type_p (operand_type) || !handler.fold_range (op_res, operand_type, diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc index 48093a8b623..b328bb8ce14 100644 --- a/gcc/ipa-fnsummary.cc +++ b/gcc/ipa-fnsummary.cc @@ -500,9 +500,11 @@ evaluate_conditions_for_known_args (struct cgraph_node *node, } else if (!op->val[1]) { - value_range op0 (op->val[0], op->val[0]); + value_range op0; range_op_handler handler (op->code, op->type); + ipa_range_set_and_normalize (op0, op->val[0]); + if (!handler || !res.supports_type_p (op->type) || !handler.fold_range (res, op->type, @@ -518,12 +520,10 @@ evaluate_conditions_for_known_args (struct cgraph_node *node, { value_range res; value_range val_vr; - if (TREE_CODE (c->val) == INTEGER_CST) - val_vr.set (c->val, c->val); - else - val_vr.set_varying (TREE_TYPE (c->val)); range_op_handler handler (c->code, boolean_type_node); + ipa_range_set_and_normalize (val_vr, c->val); + if (!handler || !res.supports_type_p (boolean_type_node) || !handler.fold_range (res, boolean_type_node, vr, val_vr)) diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h index 7eb5c8f44ea..93785a6a8e6 100644 --- a/gcc/ipa-prop.h +++ b/gcc/ipa-prop.h @@ -1201,4 +1201,18 @@ tree build_ref_for_offset (location_t, tree, poly_int64, bool, tree, /* In ipa-cp.cc */ void ipa_cp_cc_finalize (void); +/* Set R to the range of [VAL, VAL] while normalizing addresses to + non-zero. */ + +inline void +ipa_range_set_and_normalize (irange &r, tree val) +{ + if (TREE_CODE (val) == INTEGER_CST) + r.set (val, val); + else if (TREE_CODE (val) == ADDR_EXPR) + r.set_nonzero (TREE_TYPE (val)); + else + r.set_varying (TREE_TYPE (val)); +} + #endif /* IPA_PROP_H */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109639.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109639.c new file mode 100644 index 00000000000..897e62cfad6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109639.c @@ -0,0 +1,20 @@ +// { dg-do compile } +// { dg-options "-O2" } + +extern int k[]; +int m; +int* j(); +void f(int *howto) { + short __trans_tmp_1; + long offset = howto - k; + __trans_tmp_1 = offset; + for (;;) { + if (howto == 0) + return; + if (__trans_tmp_1) { + howto = j(); + m = *howto; + } + f(howto); + } +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109643.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109643.c new file mode 100644 index 00000000000..2bd6f25e9ef --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109643.c @@ -0,0 +1,18 @@ +// { dg-do compile } +// { dg-options "-O2" } + +int g_variant_type_info_basic_table[1]; +int g_variant_type_info_check__g_boolean_var_, g_variant_type_info_get_index; +int *g_variant_type_info_get_info; +int g_assertion_message_expr(); +void g_variant_type_info_check(int *info) { + int index = info - g_variant_type_info_basic_table; + if (index) + g_variant_type_info_check__g_boolean_var_ = 1; + g_assertion_message_expr(); +} +void g_variant_type_info_get() { + g_variant_type_info_get_info = + g_variant_type_info_basic_table + g_variant_type_info_get_index; + g_variant_type_info_check(g_variant_type_info_get_info); +} diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 1a3013be6fd..c11c3f58d2c 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -1074,6 +1074,9 @@ irange::set (tree min, tree max, value_range_kind kind) return; } + gcc_checking_assert (TREE_CODE (min) == INTEGER_CST); + gcc_checking_assert (TREE_CODE (max) == INTEGER_CST); + if (TREE_OVERFLOW_P (min)) min = drop_tree_overflow (min); if (TREE_OVERFLOW_P (max))