From patchwork Fri Mar 31 22:45:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 78025 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp887156vqo; Fri, 31 Mar 2023 15:47:02 -0700 (PDT) X-Google-Smtp-Source: AKy350bOzYVrXM+hdf7lrNHZ38W/Vh7D2P9HggmW9LIiPNkl+wJ9EKEswrU3imhHi8WBQe+nyIci X-Received: by 2002:aa7:d713:0:b0:501:d52d:7f88 with SMTP id t19-20020aa7d713000000b00501d52d7f88mr27359888edq.10.1680302822782; Fri, 31 Mar 2023 15:47:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680302822; cv=none; d=google.com; s=arc-20160816; b=cqkfLU1DOD/SfR6lbPQgKRwdIwqd+tUy28rDKLuxDKeb2recIXma1k72vHOw0wh55j gmk0g5nLgMOTTKscnl/8Zpr6b9VeRS2nOG0+j5tzqDslu8IrOdD52vVcP385QP2IvUib OZw+EVSwOHZDygW+wXGqSNU5LJ0cu36tMQFvVY5We+PddWMtvHxptMBiAD3JLTf47WQp iBXLdo16XG78e8pXhAvMXsKFGidsZAoZ8Yc1XQpoW4xb6vxWxAIFjFdvmDBGagMXIvyi SpbJfNWRauG5d3hMWlnOzwdpkw0to6f7G9kvTFXkZHADG7DBiuPBEf0hdvgDIWE6/6A+ Iksg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=O44jINuGjq9ThhYoxS2vDgvVzLgq+xQHEm2OWxgCW3E=; b=G12ZdlEWc2NCSB2F1xhtqjlt/maiWNCUOTaVwSMnIQsfzvblz4x4Y+sNjFO+UF5ARy i/leB/jx8mVhWRu0cvxWHkptyZda3BqLol/SLAd5PujhmHtPvWSmL9eipsbA1bo1PtEw vVYf7CgaAuzbavK14Yo5zO/rTwCqonb+ks/Z3qg1bh5Tcn1XNTPVjMw3z80xnQbbKtAM xtt5MCszAFHWrlc1WvQRSmP2/H1i29Xe4r/bEdaWgimCmlmV2+UBMUVpvaCD/xiTyVB7 kzwxZDiQJDmmB8USr289rQlD7sxsWXxBZWWPU4B4ORw3tL8czdElU5/BiVwYBTLIwSv9 f3YQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=d0wsiYFS; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dy17-20020a05640231f100b004ab1c20c759si2827730edb.543.2023.03.31.15.47.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 15:47:02 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=d0wsiYFS; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D899F3853D13 for ; Fri, 31 Mar 2023 22:46:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D899F3853D13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680302801; bh=O44jINuGjq9ThhYoxS2vDgvVzLgq+xQHEm2OWxgCW3E=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=d0wsiYFSxkCRcKYm93kNiegP+MKFIUv+b8CTSjCqWp8sJ2utDpNfTW8kXahVCDGZp aT4an5AKGuSvaPoIRX7VDbymGLz2k3Greo3m9PR+bHB+V5i71Rvyt4hQ1W+1TNvmsJ oIxP2hTimHcPuG82RG54NUMXTWDzLUZ4e3VtyznQ= 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 A22F33858C50 for ; Fri, 31 Mar 2023 22:45:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A22F33858C50 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-192-YMkEYQo4OwmTXgNg9zbwkg-1; Fri, 31 Mar 2023 18:45:42 -0400 X-MC-Unique: YMkEYQo4OwmTXgNg9zbwkg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C71785C064; Fri, 31 Mar 2023 22:45:42 +0000 (UTC) Received: from localhost (unknown [10.33.36.223]) by smtp.corp.redhat.com (Postfix) with ESMTP id 62F4D492C3E; Fri, 31 Mar 2023 22:45:42 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Teach optimizer that empty COW strings are empty [PR107087] Date: Fri, 31 Mar 2023 23:45:41 +0100 Message-Id: <20230331224541.102599-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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 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: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761925212356043258?= X-GMAIL-MSGID: =?utf-8?q?1761925212356043258?= Tested powerpc64le-linux. Pushed to trunk. -- >8 -- The compiler doesn't know about the invariant that the _S_empty_rep() object is immutable and so _M_length and _M_refcount are always zero. This means that we get warnings about writing possibly-non-zero length strings into buffers that can't hold them. If we teach the compiler that the empty rep is always zero length, it knows it can be copied into any buffer. For Stage 1 we might want to also consider adding this to capacity(): if (_S_empty_rep()._M_capacity != 0) __builtin_unreachable(); And this to _Rep::_M_is_leaked() and _Rep::_M_is_shared(): if (_S_empty_rep()._M_refcount != 0) __builtin_unreachable(); libstdc++-v3/ChangeLog: PR tree-optimization/107087 * include/bits/cow_string.h (basic_string::size()): Add optimizer hint that _S_empty_rep()._M_length is always zero. (basic_string::length()): Call size(). --- libstdc++-v3/include/bits/cow_string.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 1ee84e60678..b6024365d4f 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -907,17 +907,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION public: // Capacity: + /// Returns the number of characters in the string, not including any /// null-termination. size_type size() const _GLIBCXX_NOEXCEPT - { return _M_rep()->_M_length; } + { +#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__ + if (_S_empty_rep()._M_length != 0) + __builtin_unreachable(); +#endif + return _M_rep()->_M_length; + } /// Returns the number of characters in the string, not including any /// null-termination. size_type length() const _GLIBCXX_NOEXCEPT - { return _M_rep()->_M_length; } + { return size(); } /// Returns the size() of the largest possible %string. size_type