From patchwork Tue Mar 28 20:14:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 76253 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2481180vqo; Tue, 28 Mar 2023 13:19:06 -0700 (PDT) X-Google-Smtp-Source: AKy350Z//DKJO8vaQAJhlUVrqB7kIjPoqqjCXEVK8y+KvnGPv6SWtIAIiY0tlSXrfWOApZygimy6 X-Received: by 2002:a17:906:374b:b0:878:66bc:2280 with SMTP id e11-20020a170906374b00b0087866bc2280mr16838006ejc.12.1680034746813; Tue, 28 Mar 2023 13:19:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680034746; cv=none; d=google.com; s=arc-20160816; b=OMmmiS/bytagT+qpv77+B0SF3/qcEIXzEp4zNhRWxcw8pq/VY9INugYy4ak91lkD0V KZR9qo/BTpV82qMdPoIzmosve1/II81QNtMSPBBxdAUtTguEcAlcpOR5H/IueU1p01p0 PEg+0SwlA6OgjBgJKBgzJHBz9TRnIa1YCgOBq8LimA14dYnmS5Gq9BkTGdKfvCWjJHiX EWN85TQHgnbpKC4vcZLMp6EKAKcFnfjUrRFFkcmBYL9fm2oh5HSne27hHhsyZ4ogRV0w 6vZlkSXg1j0ytNdakjoEWDAxA+4gYeotzrtIR0SYqrJEo0Q4x8EpTjUYi+zIRCiWBHlo Tzeg== 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=OWaV9ObmchDCvrkEh8KSoqPba72gxGeW/QNmFxYKzLo=; b=xSnajX8O80tggRMJKgCg5HjBMa2bmuno9cn8ZP7mpwLPbtiqnxRzVC7mwR44bdtvYh 1VCiBJqR+uQk2NLB2rEW+LiIsztbB3OXr3noSlyLeXEiDHvV10JcOIsdMymS8C9TiHAl 0Ha9hcK0gNBZr8kKwKNAoU/cdhffl8+oj3QMQE9tqXEcuu2SN2sFMcjw4YzOHKSIf+/U SljbGRQ9ZS0r9WtVTf/y0rHJOijx5hJTE/kgQGwh2JTmf7o1vUQ1Y6vZkVNDitjQ7XmW xbdXfmxwG700ixN545YWc3ov4Ck2jcsoN3w5oJcmTIGtWgacya8FqLPs7M0sLwNqkbrf DG7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fM8V9zXj; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id fy32-20020a1709069f2000b00934faf70453si21327306ejc.533.2023.03.28.13.19.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 13:19:06 -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=fM8V9zXj; 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 E23A138754AB for ; Tue, 28 Mar 2023 20:17:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E23A138754AB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680034636; bh=OWaV9ObmchDCvrkEh8KSoqPba72gxGeW/QNmFxYKzLo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fM8V9zXjsS8zNgJkgzWdubEapZUTt3T2mNO+ezSHLzWqWVnH4DJ092C4tHkEWzNQp 9UtCztUw/OgvYIwPHv4Il2+teqT7QkFcK0hO+9nUjEyRDOOTOlBcu81QAN0M43MlwJ KHzut3riR5ceEV0SAeNZHcvhOYX9Wnz/dU2oN+/o= 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 AA5233853567 for ; Tue, 28 Mar 2023 20:15:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA5233853567 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-612-XXIsC_jiNZK2JEAU5D889Q-1; Tue, 28 Mar 2023 16:14:57 -0400 X-MC-Unique: XXIsC_jiNZK2JEAU5D889Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A9CA3823A03; Tue, 28 Mar 2023 20:14:56 +0000 (UTC) Received: from localhost (unknown [10.33.36.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9113202701E; Tue, 28 Mar 2023 20:14:55 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299] Date: Tue, 28 Mar 2023 21:14:55 +0100 Message-Id: <20230328201455.1202542-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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=unavailable 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?1761644114473268541?= X-GMAIL-MSGID: =?utf-8?q?1761644114473268541?= Tested powerpc64le-linux. Pushed to trunk. -- >8 -- This avoids a bogus warning about overflowing a buffer, because GCC can't tell that we don't copy into the buffer unless it fits. By adding a __builtin_unreachable() hint we inform the compiler about the invariant that the buffer is only used when it's big enough. This can also improve codegen, by eliminating dead code that GCC couldn't tell was unreachable. libstdc++-v3/ChangeLog: PR libstdc++/109299 * include/bits/basic_string.h (basic_string::_M_is_local()): Add hint for compiler that local strings fit in the local buffer. --- libstdc++-v3/include/bits/basic_string.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 1b8ebca7dad..5d040e2897d 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -271,7 +271,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR bool _M_is_local() const - { return _M_data() == _M_local_data(); } + { + if (_M_data() == _M_local_data()) + { + if (_M_string_length > _S_local_capacity) + __builtin_unreachable(); + return true; + } + return false; + } // Create & Destroy _GLIBCXX20_CONSTEXPR