Message ID | 3158023-8523-n61r-460-r7n98r84q57@fhfr.qr |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 81FD93857C48 for <patchwork@sourceware.org>; Thu, 20 Jan 2022 13:59:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81FD93857C48 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642687174; bh=7dh9SM/xPQsKYsMqiHvnF7b4/a2KSjVDO77c66cZ+0g=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sIRzc8OROGxpiD6yajY8+OqWXbuR7tbqmgah87tfk1GizAdOt+/D2BaTYFPz1R66Z E2WjPZVi6Lau5qFQb7px42XUbjAuMxq2BSLrKjgdgi1PVl8bjojTdvBlu0V7JSMuay uiec1XwvcNY8fMDTAMOQ9pbSEt7uesTYAoGggCvI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id AAD02385781D for <gcc-patches@gcc.gnu.org>; Thu, 20 Jan 2022 13:58:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AAD02385781D 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-out2.suse.de (Postfix) with ESMTPS id 50F801F3AA for <gcc-patches@gcc.gnu.org>; Thu, 20 Jan 2022 13:58:22 +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 397BE13BCC for <gcc-patches@gcc.gnu.org>; Thu, 20 Jan 2022 13:58:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id aZblDH5q6WErDAAAMHmgww (envelope-from <rguenther@suse.de>) for <gcc-patches@gcc.gnu.org>; Thu, 20 Jan 2022 13:58:22 +0000 Date: Thu, 20 Jan 2022 14:58:21 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] middle-end/100786 - constant folding from incompatible alias Message-ID: <3158023-8523-n61r-460-r7n98r84q57@fhfr.qr> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Richard Biener via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Richard Biener <rguenther@suse.de> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
middle-end/100786 - constant folding from incompatible alias
|
|
Commit Message
Richard Biener
Jan. 20, 2022, 1:58 p.m. UTC
The following avoids us ICEing doing constant folding from variables with aliases of different types. The formerly used fold_convert wasn't entirely correct even for the cases it handled and using a VIEW_CONVERT_EXPR avoids the ICE. Reading from a larger alias will cause unfolded constants to appear but appearantly we handle that just "fine". b.0_1 = VIEW_CONVERT_EXPR<double>(1); There is no obvious spot and way to just disable the constant folding so I've not attempted to do that. Boostrapped on x86_64-unknown-linux-gnu, testing in progress. 2022-01-20 Richard Biener <rguenther@suse.de> PR middle-end/100786 * gimple-fold.cc (fold_stmt_1): Use a VIEW_CONVERT_EXPR on type mismatches. * gcc.dg/torture/pr100786.c: New testcase. --- gcc/gimple-fold.cc | 2 +- gcc/testsuite/gcc.dg/torture/pr100786.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr100786.c
Comments
On Thu, Jan 20, 2022 at 02:58:21PM +0100, Richard Biener via Gcc-patches wrote: > The following avoids us ICEing doing constant folding from variables > with aliases of different types. The formerly used fold_convert > wasn't entirely correct even for the cases it handled and using > a VIEW_CONVERT_EXPR avoids the ICE. Reading from a larger alias > will cause unfolded constants to appear but appearantly we handle > that just "fine". > > b.0_1 = VIEW_CONVERT_EXPR<double>(1); If they have the same sizes, why not, but doesn't int have different size from double and isn't VCE defined only for same sizes? Jakub
On Thu, 20 Jan 2022, Jakub Jelinek wrote: > On Thu, Jan 20, 2022 at 02:58:21PM +0100, Richard Biener via Gcc-patches wrote: > > The following avoids us ICEing doing constant folding from variables > > with aliases of different types. The formerly used fold_convert > > wasn't entirely correct even for the cases it handled and using > > a VIEW_CONVERT_EXPR avoids the ICE. Reading from a larger alias > > will cause unfolded constants to appear but appearantly we handle > > that just "fine". > > > > b.0_1 = VIEW_CONVERT_EXPR<double>(1); > > If they have the same sizes, why not, but doesn't int have > different size from double and isn't VCE defined only for same sizes? Well yes, it's undefined. IL wise we only constrain us for SSA operands, not constants. But the whole testcase is undefined, and previously we'd happily accept a int -1 as a -1 long by sign-extending it. I'm going to test an alternative patch tackling get_symbol_constant_value which is only used from CCP (which looks suffering from the same issue) and folding. Richard.
On Thu, 20 Jan 2022, Richard Biener wrote: > On Thu, 20 Jan 2022, Jakub Jelinek wrote: > > > On Thu, Jan 20, 2022 at 02:58:21PM +0100, Richard Biener via Gcc-patches wrote: > > > The following avoids us ICEing doing constant folding from variables > > > with aliases of different types. The formerly used fold_convert > > > wasn't entirely correct even for the cases it handled and using > > > a VIEW_CONVERT_EXPR avoids the ICE. Reading from a larger alias > > > will cause unfolded constants to appear but appearantly we handle > > > that just "fine". > > > > > > b.0_1 = VIEW_CONVERT_EXPR<double>(1); > > > > If they have the same sizes, why not, but doesn't int have > > different size from double and isn't VCE defined only for same sizes? > > Well yes, it's undefined. IL wise we only constrain us for SSA > operands, not constants. But the whole testcase is undefined, > and previously we'd happily accept a int -1 as a -1 long by > sign-extending it. > > I'm going to test an alternative patch tackling get_symbol_constant_value > which is only used from CCP (which looks suffering from the same issue) > and folding. Like the following. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. From cc3f3c7428253c2326a00699f08bd89467b941f5 Mon Sep 17 00:00:00 2001 From: Richard Biener <rguenther@suse.de> Date: Thu, 20 Jan 2022 14:25:51 +0100 Subject: [PATCH] middle-end/100786 - constant folding from incompatible alias To: gcc-patches@gcc.gnu.org The following avoids us ICEing doing constant folding from variables with aliases of different types. The issue appears both in folding and CCP and FRE can do more fancy stuff to still constant fold cases where the load is smaller than the initializer so defer it to there. 2022-01-20 Richard Biener <rguenther@suse.de> PR middle-end/100786 * gimple-fold.cc (get_symbol_constant_value): Only return values of compatible type to the symbol. * gcc.dg/torture/pr100786.c: New testcase. --- gcc/gimple-fold.cc | 4 +++- gcc/testsuite/gcc.dg/torture/pr100786.c | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr100786.c diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 08d3cc214ff..3639aa20626 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -291,7 +291,9 @@ get_symbol_constant_value (tree sym) if (val) { val = canonicalize_constructor_val (unshare_expr (val), sym); - if (val && is_gimple_min_invariant (val)) + if (val + && is_gimple_min_invariant (val) + && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val))) return val; else return NULL_TREE; diff --git a/gcc/testsuite/gcc.dg/torture/pr100786.c b/gcc/testsuite/gcc.dg/torture/pr100786.c new file mode 100644 index 00000000000..42f4e485593 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr100786.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ + +const double a = 0; +extern int b __attribute__((alias("a"))); +void inc() { b++; } + +const int a2 = 0; +extern double b2 __attribute__((alias("a2"))); +void inc2() { b2+=1; }
diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 08d3cc214ff..a4f1fdabc18 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -6264,7 +6264,7 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree)) if (new_rhs && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_rhs))) - new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); + new_rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), new_rhs); if (new_rhs && (!inplace || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) diff --git a/gcc/testsuite/gcc.dg/torture/pr100786.c b/gcc/testsuite/gcc.dg/torture/pr100786.c new file mode 100644 index 00000000000..42f4e485593 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr100786.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ + +const double a = 0; +extern int b __attribute__((alias("a"))); +void inc() { b++; } + +const int a2 = 0; +extern double b2 __attribute__((alias("a2"))); +void inc2() { b2+=1; }