From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7FAB33857C45 for ; Fri, 10 Dec 2021 10:41:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7FAB33857C45 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-523-thLX_EymOHCYVKch06ByHQ-1; Fri, 10 Dec 2021 05:41:08 -0500 X-MC-Unique: thLX_EymOHCYVKch06ByHQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3EE05102CB2A; Fri, 10 Dec 2021 10:41:07 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.188]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5486C45D61; Fri, 10 Dec 2021 10:41:06 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1BAAf3W73102877 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 10 Dec 2021 11:41:03 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1BAAf2G33102876; Fri, 10 Dec 2021 11:41:02 +0100 Date: Fri, 10 Dec 2021 11:41:02 +0100 From: Jakub Jelinek To: Jason Merrill , Jan Hubicka Cc: Jonathan Wakely , gcc-patches@gcc.gnu.org Subject: [PATCH] symtab: Fold &a == &b to 0 if folding_initializer [PR94716] Message-ID: <20211210104102.GM2646553@tucnak> Reply-To: Jakub Jelinek References: <20211208103528.GE2646553@tucnak> <20211209201523.GK2646553@tucnak> <53b915f2-8ac5-d6a8-9051-3518b3380ea2@redhat.com> MIME-Version: 1.0 In-Reply-To: <53b915f2-8ac5-d6a8-9051-3518b3380ea2@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Dec 2021 10:41:14 -0000 Hi! On Thu, Dec 09, 2021 at 06:09:12PM -0500, Jason Merrill wrote: > For the more general comparison of decls like your a != b example above I > think clang is in the right; in manifestly constant-evaluated context > (folding_initializer) we should return that they are unequal and prevent a > later alias declaration, like we do for comparison to 0 in > maybe_nonzero_address. It's possible that this gives a wrong answer based > on something in another translation unit, but that's unlikely, and taking > that chance seems better than rejecting code that needs a constant answer. I agree. This is an incremental patch to do that (so far untested), Honza, what do you think about it? 2021-12-10 Jakub Jelinek PR c++/94716 gcc/ * symtab.c: Include fold-const.h. (symtab_node::equal_address_to): If folding_initializer is true, handle it like memory_accessed. Simplify. gcc/testsuite/ * gcc.dg/init-compare-1.c: New test. * g++.dg/cpp0x/constexpr-compare1.C: New test. * g++.dg/cpp1y/constexpr-94716.C: New test. * g++.dg/cpp1z/constexpr-compare1.C: New test. --- gcc/symtab.c.jj 2021-12-09 20:41:30.085566768 +0100 +++ gcc/symtab.c 2021-12-10 11:14:56.072577457 +0100 @@ -38,6 +38,7 @@ along with GCC; see the file COPYING3. #include "stringpool.h" #include "attribs.h" #include "builtins.h" +#include "fold-const.h" static const char *ipa_ref_use_name[] = {"read","write","addr","alias"}; @@ -2276,10 +2277,12 @@ symtab_node::equal_address_to (symtab_no return 0; } + if (rs1 == rs2) + return -1; + /* If the FE tells us at least one of the decls will never be aliased nor overlapping with other vars in some other way, return 0. */ if (VAR_P (decl) - && rs1 != rs2 && (lookup_attribute ("non overlapping", DECL_ATTRIBUTES (decl)) || lookup_attribute ("non overlapping", DECL_ATTRIBUTES (s2->decl)))) return 0; @@ -2288,9 +2291,14 @@ symtab_node::equal_address_to (symtab_no are different unless they are declared as alias of one to another while the code folding comparisons doesn't. We probably should be consistent and use this fact here, too, but for - the moment return false only when we are called from the alias oracle. */ + the moment return false only when we are called from the alias oracle. + Return 0 in C constant initializers and C++ manifestly constant + expressions, the likelyhood that different vars will be aliases is + small and returning -1 lets us reject too many initializers. */ + if (memory_accessed || folding_initializer) + return 0; - return memory_accessed && rs1 != rs2 ? 0 : -1; + return -1; } /* Worker for call_for_symbol_and_aliases. */ --- gcc/testsuite/gcc.dg/init-compare-1.c.jj 2021-12-10 11:31:45.839084036 +0100 +++ gcc/testsuite/gcc.dg/init-compare-1.c 2021-12-10 11:32:08.551757661 +0100 @@ -0,0 +1,5 @@ +/* { dg-do compile } */ + +extern int a, b; +int c = &a == &a; +int d = &a != &b; --- gcc/testsuite/g++.dg/cpp0x/constexpr-compare1.C.jj 2021-12-10 11:25:58.579074051 +0100 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-compare1.C 2021-12-10 11:31:38.100195242 +0100 @@ -0,0 +1,7 @@ +// { dg-do compile { target c++11 } } + +extern int a, b; +static_assert (&a == &a, ""); +static_assert (&a != &b, ""); +constexpr bool c = &a == &a; +constexpr bool d = &a != &b; --- gcc/testsuite/g++.dg/cpp1y/constexpr-94716.C.jj 2021-12-10 11:24:20.028490189 +0100 +++ gcc/testsuite/g++.dg/cpp1y/constexpr-94716.C 2021-12-10 11:35:52.782538861 +0100 @@ -0,0 +1,8 @@ +// PR c++/94716 +// { dg-do compile { target c++14 } } + +template char v = 0; +static_assert (&v<2> == &v<2>, ""); +static_assert (&v<0> != &v<1>, ""); +constexpr bool a = &v<2> == &v<2>; +constexpr bool b = &v<0> != &v<1>; --- gcc/testsuite/g++.dg/cpp1z/constexpr-compare1.C.jj 2021-12-10 11:28:31.642874577 +0100 +++ gcc/testsuite/g++.dg/cpp1z/constexpr-compare1.C 2021-12-10 11:30:59.114755454 +0100 @@ -0,0 +1,8 @@ +// { dg-do compile { target c++17 } } + +inline int a = 0; +inline int b = 0; +static_assert (&a == &a); +static_assert (&a != &b); +constexpr bool c = &a == &a; +constexpr bool d = &a != &b; Jakub