From patchwork Wed Jul 27 09:33:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 205 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp282878pxt; Wed, 27 Jul 2022 02:34:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vbEHeLZ7pzz3OIgNZrYPkdfgEchVp73lpReFeTEtfO8hmFKo04yioyJoCKw4pNJcyF4Z91 X-Received: by 2002:a05:6402:294c:b0:43a:91a9:a691 with SMTP id ed12-20020a056402294c00b0043a91a9a691mr22356547edb.182.1658914486377; Wed, 27 Jul 2022 02:34:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658914486; cv=none; d=google.com; s=arc-20160816; b=Nsn+Zcb1mtlO+qQQ/JPMdzp39kJQXjM8qhGcq5RrNOjOIIPtIlp0zZWE73Z2YC+//1 DS/vyzvIKtY2CvPcEs1lrpheo+DsNXbulZSORF7/IJgEsKCVa8oO75cavtqm8nzYyPmp ICdpHOohKz/k6HZOYa1WHRhDprAhC14nrtDTgdOgE/jzKfwVF8YrOVLh49J9v+v3xt6y upc+ZAPXzK61mWMxQXmwsUI9qBgx9mphvj6Xwu3yc3u+Qn7XbrhXFCTHLOFNwsfj1AMq GOJH0H8rVOacz5pQ8QfTJX/8hlGHbJTnkkmXyqFvcQCo3aHIR23VRMA8wwr36XTfzO0Q aLsA== 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-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=f3/Vi/b3EZMbBfavmSg2f2K6hGMt9PaAvBXQBlqGhfE=; b=txUyRTpQsx7vxMiusNIjRq9t3YCFQVveGTJzowjkwloe86gskt+9wSDidMrb6xs8cV BQbjIGerxaabD1iynXg06bHOiASA4T+Fl7cRl2RRTZ1hoU6hxwLUsBmm//zmQw0/06Ut TkEluo72t9E1wLKOVl4NqWHaXAHrfwRptJA8nCpe4B/JLpsTWCAmiH3OXbcAPvipJ0+e 5cXvfayUB7Adx80ZiUtLCztjoAcUC/HUgDlNVCwvk90T1Pm/Yks3Lg4hVc/Bdcrm3YdM s0FfqYnZeWPS/XDJR+MxJvJ3tmXIWzOr2DayBQ06IFcqPooSrJ6gMUBY72Mx3LqUsaFw Xe/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IYgMsMLX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a25-20020a1709063a5900b0072a605a57e9si3314466ejf.329.2022.07.27.02.34.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Jul 2022 02:34:46 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IYgMsMLX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 204153856DDC for ; Wed, 27 Jul 2022 09:34:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 204153856DDC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658914458; bh=f3/Vi/b3EZMbBfavmSg2f2K6hGMt9PaAvBXQBlqGhfE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IYgMsMLXqnzoj2M2JQYmZoJlsJMEMPNEwISRGamGN+Zlf9MDwX5M/4XyUoeLn/aQE ppC5mwycXZwqlCt8+13TbdW85ycaZLkufcduqH9cqYRqPaFbYMaK/Bbm1fWNQ8LLqA pACDdd4qgnkLaLw98A2brEizF6Y81mEMKH7RZtT8= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id A35593857375 for ; Wed, 27 Jul 2022 09:33:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A35593857375 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-475-uu2abVdTNDabSd1grrgHUw-1; Wed, 27 Jul 2022 05:33:32 -0400 X-MC-Unique: uu2abVdTNDabSd1grrgHUw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 96A1585A585; Wed, 27 Jul 2022 09:33:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 56F03492CA2; Wed, 27 Jul 2022 09:33:32 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 26R9XTkg2208225 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 27 Jul 2022 11:33:30 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 26R9XTdd2208224; Wed, 27 Jul 2022 11:33:29 +0200 Date: Wed, 27 Jul 2022 11:33:29 +0200 To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Outline the overlapping case of string _M_replace into a separate function [PR105329] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1739497916727957493?= X-GMAIL-MSGID: =?utf-8?q?1739497916727957493?= Hi! The following patch is partially a workaround for bogus warnings when the compiler isn't able to fold _M_disjunct call into constant false, but also an optimization attempt - assuming _M_disjunct (__s) is rare, the patch should shrink code size for the common case and use library or for non-standard instantiations an out of line function to handle the rare case. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-07-27 Jakub Jelinek PR tree-optimization/105329 * acinclude.m4 (libtool_VERSION): Change to 6:31:0. * config/abi/pre/gnu.ver (GLIBCXX_3.4.21): Don't export std::basic_string methods with name length of 15. (GLIBCXX_3.4.31): Export std::basic_string::_M_replace_cold. * testsuite/util/testsuite_abi.cc (check_version): Handle GLIBCXX_3.4.31. * include/bits/basic_string.h (std::basic_string::_M_replace_cold): Declare. * include/bits/basic_string.tcc (std::basic_string::_M_replace_cold): Define and export even for C++20. (std::basic_string::_M_replace): Use __builtin_expect, outline the overlapping case to _M_replace_cold. * configure: Regenerated. Jakub --- libstdc++-v3/acinclude.m4.jj 2022-06-27 11:18:03.102052967 +0200 +++ libstdc++-v3/acinclude.m4 2022-07-26 13:15:51.187572177 +0200 @@ -3821,7 +3821,7 @@ changequote([,])dnl fi # For libtool versioning info, format is CURRENT:REVISION:AGE -libtool_VERSION=6:30:0 +libtool_VERSION=6:31:0 # Everything parsed; figure out what files and settings to use. case $enable_symvers in --- libstdc++-v3/config/abi/pre/gnu.ver.jj 2022-03-17 09:22:50.135144471 +0100 +++ libstdc++-v3/config/abi/pre/gnu.ver 2022-07-26 13:12:13.098402944 +0200 @@ -1736,7 +1736,7 @@ GLIBCXX_3.4.21 { _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE12_M*; _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE13*; _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE14_M_replace_aux*; - _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE1[568-9]*; + _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE1[68-9]*; _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE2at*; _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE3end*; _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE4back*; @@ -2444,6 +2444,10 @@ GLIBCXX_3.4.30 { } GLIBCXX_3.4.29; +GLIBCXX_3.4.31 { + _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE15_M_replace_cold*; +} GLIBCXX_3.4.30; + # Symbols in the support library (libsupc++) have their own tag. CXXABI_1.3 { --- libstdc++-v3/testsuite/util/testsuite_abi.cc.jj 2022-03-17 09:22:50.135144471 +0100 +++ libstdc++-v3/testsuite/util/testsuite_abi.cc 2022-07-26 13:16:44.042886118 +0200 @@ -211,6 +211,7 @@ check_version(symbol& test, bool added) known_versions.push_back("GLIBCXX_3.4.28"); known_versions.push_back("GLIBCXX_3.4.29"); known_versions.push_back("GLIBCXX_3.4.30"); + known_versions.push_back("GLIBCXX_3.4.31"); known_versions.push_back("GLIBCXX_LDBL_3.4.29"); known_versions.push_back("GLIBCXX_IEEE128_3.4.29"); known_versions.push_back("GLIBCXX_IEEE128_3.4.30"); @@ -247,7 +248,7 @@ check_version(symbol& test, bool added) test.version_status = symbol::incompatible; // Check that added symbols are added in the latest pre-release version. - bool latestp = (test.version_name == "GLIBCXX_3.4.30" + bool latestp = (test.version_name == "GLIBCXX_3.4.31" // XXX remove next line when baselines have been regenerated. || test.version_name == "GLIBCXX_IEEE128_3.4.30" || test.version_name == "CXXABI_1.3.13" --- libstdc++-v3/include/bits/basic_string.h.jj 2022-06-17 11:08:11.971491626 +0200 +++ libstdc++-v3/include/bits/basic_string.h 2022-07-26 12:42:38.008440082 +0200 @@ -2504,6 +2504,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, _CharT __c); + __attribute__((__noinline__, __noclone__, __cold__)) void + _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s, + const size_type __len2, const size_type __how_much); + _GLIBCXX20_CONSTEXPR basic_string& _M_replace(size_type __pos, size_type __len1, const _CharT* __s, --- libstdc++-v3/include/bits/basic_string.tcc.jj 2022-06-15 10:43:46.796946222 +0200 +++ libstdc++-v3/include/bits/basic_string.tcc 2022-07-26 13:43:35.293012051 +0200 @@ -471,6 +471,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template + __attribute__((__noinline__, __noclone__, __cold__)) void + basic_string<_CharT, _Traits, _Alloc>:: + _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s, + const size_type __len2, const size_type __how_much) + { + // Work in-place. + if (__len2 && __len2 <= __len1) + this->_S_move(__p, __s, __len2); + if (__how_much && __len1 != __len2) + this->_S_move(__p + __len2, __p + __len1, __how_much); + if (__len2 > __len1) + { + if (__s + __len2 <= __p + __len1) + this->_S_move(__p, __s, __len2); + else if (__s >= __p + __len1) + { + // Hint to middle end that __p and __s overlap + // (PR 98465). + const size_type __poff = (__s - __p) + (__len2 - __len1); + this->_S_copy(__p, __p + __poff, __len2); + } + else + { + const size_type __nleft = (__p + __len1) - __s; + this->_S_move(__p, __s, __nleft); + this->_S_copy(__p + __nleft, __p + __len2, __len2 - __nleft); + } + } + } + + template _GLIBCXX20_CONSTEXPR basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: @@ -500,7 +531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else #endif - if (_M_disjunct(__s)) + if (__builtin_expect(_M_disjunct(__s), true)) { if (__how_much && __len1 != __len2) this->_S_move(__p + __len2, __p + __len1, __how_much); @@ -508,32 +539,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION this->_S_copy(__p, __s, __len2); } else - { - // Work in-place. - if (__len2 && __len2 <= __len1) - this->_S_move(__p, __s, __len2); - if (__how_much && __len1 != __len2) - this->_S_move(__p + __len2, __p + __len1, __how_much); - if (__len2 > __len1) - { - if (__s + __len2 <= __p + __len1) - this->_S_move(__p, __s, __len2); - else if (__s >= __p + __len1) - { - // Hint to middle end that __p and __s overlap - // (PR 98465). - const size_type __poff = (__s - __p) + (__len2 - __len1); - this->_S_copy(__p, __p + __poff, __len2); - } - else - { - const size_type __nleft = (__p + __len1) - __s; - this->_S_move(__p, __s, __nleft); - this->_S_copy(__p + __nleft, __p + __len2, - __len2 - __nleft); - } - } - } + _M_replace_cold(__p, __len1, __s, __len2, __how_much); } else this->_M_mutate(__pos, __len1, __s, __len2); @@ -1000,6 +1006,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // to ensure the definition in libstdc++.so is unique (PR 86138). extern template basic_string::size_type basic_string::_Rep::_S_empty_rep_storage[]; +# elif _GLIBCXX_EXTERN_TEMPLATE > 0 + // Export _M_replace_cold even for C++20. + extern template void + basic_string::_M_replace_cold(char *, size_type, const char*, + const size_type, const size_type); # endif extern template @@ -1021,6 +1032,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION # elif ! _GLIBCXX_USE_CXX11_ABI extern template basic_string::size_type basic_string::_Rep::_S_empty_rep_storage[]; +# elif _GLIBCXX_EXTERN_TEMPLATE > 0 + // Export _M_replace_cold even for C++20. + extern template void + basic_string::_M_replace_cold(wchar_t*, size_type, const wchar_t*, + const size_type, const size_type); # endif extern template --- libstdc++-v3/configure.jj 2022-07-26 10:32:24.395262574 +0200 +++ libstdc++-v3/configure 2022-07-26 13:16:09.830330197 +0200 @@ -69048,7 +69048,7 @@ $as_echo "$as_me: WARNING: === Symbol ve fi # For libtool versioning info, format is CURRENT:REVISION:AGE -libtool_VERSION=6:30:0 +libtool_VERSION=6:31:0 # Everything parsed; figure out what files and settings to use. case $enable_symvers in