From patchwork Tue Dec 6 21:36:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61613 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4833382B3D4 for ; Tue, 6 Dec 2022 21:37:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4833382B3D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670362671; bh=ZQFV1ThtLvXbPvNv4use3XRujjFtUW5s/t90bjQuI7M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yCdfdtbm3GD/w82AoQXsDc1MFare1Q0ts8cLWvOaf8KK22UZw5OpJgEHg1fe/3W0G zzQhPVl0Ozum0wXS2JIRis7M7I1LwRHWx2LDiz+xTHXbN1sFYz1NyYhxsZ/2l99YYK ZZFRlmXQoeVE+zHwVaD5+TAc/4D0E+hForcpwctg= 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 E7717382D3FC for ; Tue, 6 Dec 2022 21:37:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E7717382D3FC 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-65-XOZPCgiqOd2eLZLeABSDhA-1; Tue, 06 Dec 2022 16:36:57 -0500 X-MC-Unique: XOZPCgiqOd2eLZLeABSDhA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 56934101A528; Tue, 6 Dec 2022 21:36:57 +0000 (UTC) Received: from localhost (unknown [10.33.36.203]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21332FD48; Tue, 6 Dec 2022 21:36:57 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add hint to compiler about vector invariants [PR106434] Date: Tue, 6 Dec 2022 21:36:54 +0000 Message-Id: <20221206213654.239004-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux. Pushed to trunk. -- >8 -- The PR shows a bogus warning where jump threading generates code for the undefined case that the insertion point is a value-initialized iterator but _M_finish and _M_end_of_storage are unequal (so at least one must be non-null). Using __builtin_unreachable() removes the bogus warning. Also add an assertion to diagnose undefined misuses of a null iterator here, so we don't just silently optimize that undefined code to something unsafe. libstdc++-v3/ChangeLog: PR c++/106434 * include/bits/vector.tcc (insert(const_iterator, const T&)): Add assertion and optimization hint that the iterator for the insertion point must be non-null. --- libstdc++-v3/include/bits/vector.tcc | 40 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 27ef1a4ee7f..8ae79ffc7af 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -139,26 +139,32 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { const size_type __n = __position - begin(); if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) - if (__position == end()) - { - _GLIBCXX_ASAN_ANNOTATE_GROW(1); - _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, - __x); - ++this->_M_impl._M_finish; - _GLIBCXX_ASAN_ANNOTATE_GREW(1); - } - else - { + { + __glibcxx_assert(__position != const_iterator()); + if (!(__position != const_iterator())) + __builtin_unreachable(); // PR 106434 + + if (__position == end()) + { + _GLIBCXX_ASAN_ANNOTATE_GROW(1); + _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, + __x); + ++this->_M_impl._M_finish; + _GLIBCXX_ASAN_ANNOTATE_GREW(1); + } + else + { #if __cplusplus >= 201103L - const auto __pos = begin() + (__position - cbegin()); - // __x could be an existing element of this vector, so make a - // copy of it before _M_insert_aux moves elements around. - _Temporary_value __x_copy(this, __x); - _M_insert_aux(__pos, std::move(__x_copy._M_val())); + const auto __pos = begin() + (__position - cbegin()); + // __x could be an existing element of this vector, so make a + // copy of it before _M_insert_aux moves elements around. + _Temporary_value __x_copy(this, __x); + _M_insert_aux(__pos, std::move(__x_copy._M_val())); #else - _M_insert_aux(__position, __x); + _M_insert_aux(__position, __x); #endif - } + } + } else #if __cplusplus >= 201103L _M_realloc_insert(begin() + (__position - cbegin()), __x);