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.133.124]) by sourceware.org (Postfix) with ESMTPS id DF8743857C58 for ; Tue, 18 Jan 2022 16:25:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DF8743857C58 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-298-KP_1FPBkOtKfoiXiEv9gLQ-1; Tue, 18 Jan 2022 11:25:41 -0500 X-MC-Unique: KP_1FPBkOtKfoiXiEv9gLQ-1 Received: by mail-qt1-f199.google.com with SMTP id f21-20020ac840d5000000b002c93f29abbcso9619348qtm.14 for ; Tue, 18 Jan 2022 08:25:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=HjDJxTbs7sly20n/MXy3ExR5H7+ysVed8YePyywdvwo=; b=Vh5oqnu4s6ZAuFEIeVwSlRt/7aPD5aX8WV2yZkOzEgvE+zT96TfX2iTatFLF6Hyfr4 BNifyP156BwPXJZ0XL8I/hr/h7FLK1/dsfiEWbwoqUXXIWwJHFdW7woKTp8tH/SXszkQ 1e9Euu2ipirh5xBIeiulRO2vDmr2iCpe754wiRzr9QgBPR2LqgxeJcH+bJBtJ7Tv3RzA qAOnOQ9m1bpQV+9Mypl/fc0ygzs4fcDiETugkQXgYukMFR7MhlS8kHi8GFTDACBPMDcc 4kJD7kvPqvXZkassVt8KnWPYIzTxZABhvnhtMMshAf+/6u38GEhW9mu9UY+FLly40tuv l+ng== X-Gm-Message-State: AOAM530cTIApdlYdjAlRvNzDTWSHn1QBWhx1PucIi5SfWCDJX2PDfDRi ltL8HpUgFcFAE+iMB86Gi9gsKRgiAANZxc82kwB9uIr9l6ENPef2+Ent+/l+TfTNjU3vflBOylD rklwXNpF1zOIjJAbzLw== X-Received: by 2002:a05:6214:d47:: with SMTP id 7mr23934038qvr.22.1642523140712; Tue, 18 Jan 2022 08:25:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbiXuLiPqnkaZgRjY/AAlZlWyxJdyyUtt26Hr2OOfM+WhglE3rvfMVIYcGhYEJ+Q3mZNyvGw== X-Received: by 2002:a05:6214:d47:: with SMTP id 7mr23934001qvr.22.1642523140241; Tue, 18 Jan 2022 08:25:40 -0800 (PST) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s9sm11123682qki.99.2022.01.18.08.25.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 18 Jan 2022 08:25:38 -0800 (PST) Message-ID: <841aeb56-57a3-07b3-dbc0-a45ef0cca554@redhat.com> Date: Tue, 18 Jan 2022 11:25:38 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH] c++: Further address_compare fixes [PR89074] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org, Richard Biener References: <20220106092416.GX2646553@tucnak> <494ae254-aff9-8be9-1cee-5ee42b6db593@redhat.com> <20220118101741.GW2646553@tucnak> From: Jason Merrill In-Reply-To: <20220118101741.GW2646553@tucnak> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, NICE_REPLY_A, 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: Tue, 18 Jan 2022 16:25:50 -0000 On 1/18/22 05:17, Jakub Jelinek wrote: > On Thu, Jan 13, 2022 at 04:18:33PM -0500, Jason Merrill wrote: >>> Note, address_compare has some special cases, e.g. it assumes that >>> static vars are never adjacent to automatic vars, which is the case >>> for the usual layout where automatic vars are on the stack and after >>> .rodata/.data sections there is heap: >>> /* Assume that automatic variables can't be adjacent to global >>> variables. */ >>> else if (is_global_var (base0) != is_global_var (base1)) >>> ; >>> Is it ok that during constant evaluation we don't treat those as undefined >>> behavior, or shall that be with !folding_initializer && too? >> >> I guess that's undefined as well. > > Ok, following patch seems to guard that too > >>> Another special case is: >>> if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) >>> || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) >>> || (TREE_CODE (base0) == STRING_CST >>> && TREE_CODE (base1) == STRING_CST >>> && ioff0 >= 0 && ioff1 >= 0 >>> && ioff0 < TREE_STRING_LENGTH (base0) >>> && ioff1 < TREE_STRING_LENGTH (base1) >>> /* This is a too conservative test that the STRING_CSTs >>> will not end up being string-merged. */ >>> && strncmp (TREE_STRING_POINTER (base0) + ioff0, >>> TREE_STRING_POINTER (base1) + ioff1, >>> MIN (TREE_STRING_LENGTH (base0) - ioff0, >>> TREE_STRING_LENGTH (base1) - ioff1)) != 0)) >>> ; >>> else if (!DECL_P (base0) || !DECL_P (base1)) >>> return 2; >>> Here we similarly assume that vars aren't adjacent to string literals >>> or vice versa. Do we need to stick !folding_initializer && to those >>> DECL_P vs. STRING_CST cases? >> >> Seems so. > > and this too > >>> Though, because of the return 2; for >>> non-DECL_P that would mean rejecting comparisons like &var == &"foobar"[3] >>> etc. which ought to be fine, no? So perhaps we need to watch for >>> decls. vs. STRING_CSTs like for DECLs whether the address is at the start >>> or at the end of the string literal or somewhere in between (at least >>> for folding_initializer)? >> >> Agreed. > > and this as well. > Furthermore I've fixed constexpr-compare2.C by assuming if > folding_initializer that addresses of non-aliased (visibly to the compiler) > FUNCTION_DECLs are different and that functions are non-zero sized for the > purpose of var vs. function comparisons. > >>> And yet another chapter but probably unsolvable is comparison of >>> string literal addresses. I think pedantically in C++ >>> &"foo"[0] == &"foo"[0] is undefined behavior, different occurences of >>> the same string literals might still not be merged in some implementations. >> >> I disagree; it's unspecified whether string literals are merged, but I think >> the comparison result is well specified depending on that implementation >> behavior. > > Can you please comment on https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86369#c1 > then? Done. About the rest of the patch, I thought I had seen richi comment on IRC (but can't find the comment now) that these cases where we could give a constant answer but decide not to because of C++ rules should be handled in the front end rather than generic code, which makes sense to me; that is, use folding_initializer only for giving more constant results, not for giving fewer constant results. Maybe add another flag for limiting constant results if we think it's significantly easier to recognize these cases in fold? > Anyway, the following has been successfully bootstrapped/regtested on > x86_64-linux and i686-linux, ok for trunk? > > 2022-01-18 Jakub Jelinek > > PR c++/89074 > PR c++/104033 > * fold-const.c (address_compare): Restrict the decl vs. STRING_CST > or vice versa or STRING_CST vs. STRING_CST or > is_global_var != is_global_var optimizations to !folding_initializer. > Punt for FUNCTION_DECLs with non-zero offsets. If folding_initializer, > assume non-aliased functions have non-zero size and have different > addresses. For folding_initializer, punt on comparisons of start > of some object and end of another one, regardless whether it is a decl > or string literal. Also punt for folding_initializer of > STRING_CST vs. STRING_CST comparisons if the two literals could be > overlapping. > > * g++.dg/cpp1y/constexpr-89074-3.C: New test. > > --- gcc/fold-const.c.jj 2022-01-17 14:19:08.817376382 +0100 > +++ gcc/fold-const.c 2022-01-17 15:50:16.687211071 +0100 > @@ -16608,21 +16608,27 @@ address_compare (tree_code code, tree ty > HOST_WIDE_INT ioff0 = -1, ioff1 = -1; > off0.is_constant (&ioff0); > off1.is_constant (&ioff1); > - if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) > - || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) > - || (TREE_CODE (base0) == STRING_CST > - && TREE_CODE (base1) == STRING_CST > - && ioff0 >= 0 && ioff1 >= 0 > - && ioff0 < TREE_STRING_LENGTH (base0) > - && ioff1 < TREE_STRING_LENGTH (base1) > - /* This is a too conservative test that the STRING_CSTs > - will not end up being string-merged. */ > - && strncmp (TREE_STRING_POINTER (base0) + ioff0, > - TREE_STRING_POINTER (base1) + ioff1, > - MIN (TREE_STRING_LENGTH (base0) - ioff0, > - TREE_STRING_LENGTH (base1) - ioff1)) != 0)) > + if (!folding_initializer > + && ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) > + || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) > + || (TREE_CODE (base0) == STRING_CST > + && TREE_CODE (base1) == STRING_CST > + && ioff0 >= 0 && ioff1 >= 0 > + && ioff0 < TREE_STRING_LENGTH (base0) > + && ioff1 < TREE_STRING_LENGTH (base1) > + /* This is a too conservative test that the STRING_CSTs > + will not end up being string-merged. */ > + && strncmp (TREE_STRING_POINTER (base0) + ioff0, > + TREE_STRING_POINTER (base1) + ioff1, > + MIN (TREE_STRING_LENGTH (base0) - ioff0, > + TREE_STRING_LENGTH (base1) - ioff1)) != 0))) > ; > - else if (!DECL_P (base0) || !DECL_P (base1)) > + /* Punt on non-zero offsets from functions. */ > + else if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0) > + || (TREE_CODE (base1) == FUNCTION_DECL && ioff1)) > + return 2; > + else if ((!DECL_P (base0) && TREE_CODE (base0) != STRING_CST) > + || (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)) > return 2; > /* If this is a pointer comparison, ignore for now even > valid equalities where one pointer is the offset zero > @@ -16631,18 +16637,62 @@ address_compare (tree_code code, tree ty > ; > /* Assume that automatic variables can't be adjacent to global > variables. */ > - else if (is_global_var (base0) != is_global_var (base1)) > + else if (!folding_initializer > + && is_global_var (base0) != is_global_var (base1)) > + ; > + /* For initializers, assume addresses of different functions are > + different. */ > + else if (folding_initializer > + && TREE_CODE (base0) == FUNCTION_DECL > + && TREE_CODE (base1) == FUNCTION_DECL) > ; > else > { > - tree sz0 = DECL_SIZE_UNIT (base0); > - tree sz1 = DECL_SIZE_UNIT (base1); > - /* If sizes are unknown, e.g. VLA or not representable, punt. */ > - if (!tree_fits_poly_int64_p (sz0) || !tree_fits_poly_int64_p (sz1)) > - return 2; > + poly_int64 size0, size1; > + if (TREE_CODE (base0) == STRING_CST) > + { > + if (!folding_initializer > + || ioff0 < 0 > + || ioff0 > TREE_STRING_LENGTH (base0)) > + return 2; > + size0 = TREE_STRING_LENGTH (base0); > + } > + /* For initializers, assume function decls don't overlap and have > + non-empty size. */ > + else if (folding_initializer && TREE_CODE (base0) == FUNCTION_DECL) > + size0 = 1; > + else > + { > + tree sz0 = DECL_SIZE_UNIT (base0); > + /* If sizes are unknown, e.g. VLA or not representable, punt. */ > + if (!tree_fits_poly_int64_p (sz0)) > + return 2; > + > + size0 = tree_to_poly_int64 (sz0); > + } > + > + if (TREE_CODE (base1) == STRING_CST) > + { > + if (!folding_initializer > + || ioff1 < 0 > + || ioff1 > TREE_STRING_LENGTH (base1)) > + return 2; > + size1 = TREE_STRING_LENGTH (base1); > + } > + /* For initializers, assume function decls don't overlap and have > + non-empty size. */ > + else if (folding_initializer && TREE_CODE (base1) == FUNCTION_DECL) > + size1 = 1; > + else > + { > + tree sz1 = DECL_SIZE_UNIT (base1); > + /* If sizes are unknown, e.g. VLA or not representable, punt. */ > + if (!tree_fits_poly_int64_p (sz1)) > + return 2; > + > + size1 = tree_to_poly_int64 (sz1); > + } > > - poly_int64 size0 = tree_to_poly_int64 (sz0); > - poly_int64 size1 = tree_to_poly_int64 (sz1); > /* If one offset is pointing (or could be) to the beginning of one > object and the other is pointing to one past the last byte of the > other object, punt. */ > @@ -16658,6 +16708,27 @@ address_compare (tree_code code, tree ty > && (known_ne (off0, 0) > || (known_ne (size0, 0) && known_ne (size1, 0)))) > equal = 0; > + if (equal == 0 > + && TREE_CODE (base0) == STRING_CST > + && TREE_CODE (base1) == STRING_CST) > + { > + /* If the bytes in the string literals starting at the pointers > + differ, the pointers need to be different. */ > + if (memcmp (TREE_STRING_POINTER (base0) + ioff0, > + TREE_STRING_POINTER (base1) + ioff1, > + MIN (TREE_STRING_LENGTH (base0) - ioff0, > + TREE_STRING_LENGTH (base1) - ioff1)) == 0) > + { > + HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1); > + if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin, > + TREE_STRING_POINTER (base1) + ioff1 - ioffmin, > + ioffmin) == 0) > + /* If even the bytes in the string literal before the > + pointers are the same, the string literals could be > + tail merged. */ > + return 2; > + } > + } > } > return equal; > } > --- gcc/testsuite/g++.dg/cpp1y/constexpr-89074-3.C.jj 2022-01-17 15:22:43.743566175 +0100 > +++ gcc/testsuite/g++.dg/cpp1y/constexpr-89074-3.C 2022-01-17 16:10:19.182230570 +0100 > @@ -0,0 +1,132 @@ > +// PR c++/89074 > +// { dg-do compile { target c++14 } } > + > +int fn1 (void) { return 0; } > +int fn2 (void) { return 1; } > + > +constexpr bool > +f1 () > +{ > + char a[] = { 1, 2, 3, 4 }; > + > + if (&a[1] == "foo") > + return false; > + > + if (&a[1] == &"foo"[4]) > + return false; > + > + if (&"foo"[1] == &a[0]) > + return false; > + > + if (&"foo"[3] == &a[4]) > + return false; > + > + if (&a[0] == "foo") > + return false; > + > + // Pointer to start of one object (var) and end of another one (literal) > + if (&a[0] == &"foo"[4]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool > +f2 () > +{ > + char a[] = { 1, 2, 3, 4 }; > + > + // Pointer to end of one object (var) and start of another one (literal) > + if (&a[4] == "foo") // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +char v[] = { 1, 2, 3, 4 }; > + > +constexpr bool > +f3 () > +{ > + char a[] = { 1, 2, 3, 4 }; > + > + if (&a[1] == &v[1]) > + return false; > + > + if (&a[0] == &v[3]) > + return false; > + > + if (&a[2] == &v[4]) > + return false; > + > + // Pointer to start of one object (automatic var) and end of another one (non-automagic var) > + if (&a[0] == &v[4]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool > +f4 () > +{ > + char a[] = { 1, 2, 3, 4, 5 }; > + > + // Pointer to end of one object (automatic var) and start of another one (non-automagic var) > + if (&a[5] == &v[0]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool > +f5 () > +{ > + if (fn1 != fn1) > + return false; > + > + if (fn1 == fn2) > + return false; > + > + if (&"abcde"[0] == &"edcba"[1]) > + return false; > + > + if (&"abcde"[1] == &"edcba"[6]) > + return false; > + > + // Pointer to start of one object (literal) and end of another one (literal) > + if (&"abcde"[0] == &"edcba"[6]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool > +f6 () > +{ > + // Pointer to start of one object (literal) and end of another one (literal) > + if (&"abcde"[6] == &"edcba"[0]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool > +f7 () > +{ > + if (&"abcde"[3] == &"fabcde"[3]) > + return false; > + > + // These could be suffix merged, with &"abcde"[0] == &"fabcde"[1]. > + if (&"abcde"[3] == &"fabcde"[4]) // { dg-error "is not a constant expression" } > + return false; > + > + return true; > +} > + > +constexpr bool a = f1 (); > +constexpr bool b = f2 (); > +constexpr bool c = f3 (); > +constexpr bool d = f4 (); > +constexpr bool e = f5 (); > +constexpr bool f = f6 (); > +constexpr bool g = f7 (); > > Jakub >