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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 948523857813 for ; Mon, 23 Aug 2021 13:48:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 948523857813 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-413-CDtOPk48NmuO-G7vyDJU-Q-1; Mon, 23 Aug 2021 09:48:11 -0400 X-MC-Unique: CDtOPk48NmuO-G7vyDJU-Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 891BF87D550; Mon, 23 Aug 2021 13:48:10 +0000 (UTC) Received: from localhost (unknown [10.33.36.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 350E25E26A; Mon, 23 Aug 2021 13:48:10 +0000 (UTC) Date: Mon, 23 Aug 2021 14:48:09 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use __builtin_expect in __dynamic_cast Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="cb/DzOeOf18LRUb8" Content-Disposition: inline X-Spam-Status: No, score=-14.2 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_LOW, RCVD_IN_MSPIKE_H2, 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Aug 2021 13:48:23 -0000 --cb/DzOeOf18LRUb8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The null pointer check is never needed for correct code, only to gracefully handle undefined cases. Add __builtin_expect to be sure that we don't pessimize the valid uses. libstdc++-v3/ChangeLog: * libsupc++/dyncast.cc (__dynamic_cast): Add __builtin_expect to precondition check. Tested powerpc64le-linux. Committed to trunk. --cb/DzOeOf18LRUb8 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit da6ce351069bda74d814b723934008d75bd4e8b0 Author: Jonathan Wakely Date: Mon Aug 23 13:12:24 2021 libstdc++: Use __builtin_expect in __dynamic_cast The null pointer check is never needed for correct code, only to gracefully handle undefined cases. Add __builtin_expect to be sure that we don't pessimize the valid uses. libstdc++-v3/ChangeLog: * libsupc++/dyncast.cc (__dynamic_cast): Add __builtin_expect to precondition check. diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index f8f707ee4d4..a1138d04f6d 100644 --- a/libstdc++-v3/libsupc++/dyncast.cc +++ b/libstdc++-v3/libsupc++/dyncast.cc @@ -47,9 +47,9 @@ __dynamic_cast (const void *src_ptr, // object started from const __class_type_info *dst_type, // desired target type ptrdiff_t src2dst) // how src and dst are related { - if (!src_ptr) - /* Handle precondition violations gracefully. */ - return NULL; + if (__builtin_expect(!src_ptr, 0)) + return NULL; // Handle precondition violations gracefully. + const void *vtable = *static_cast (src_ptr); const vtable_prefix *prefix = (adjust_pointer @@ -70,7 +70,7 @@ __dynamic_cast (const void *src_ptr, // object started from (whole_vtable, -ptrdiff_t (offsetof (vtable_prefix, origin)))); if (whole_prefix->whole_type != whole_type) return NULL; - + whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public, dst_type, whole_ptr, src_type, src_ptr, result); if (!result.dst_ptr) --cb/DzOeOf18LRUb8--