From patchwork Thu Oct 26 18:01:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1855903 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=itgC/6gh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SGYX16Gqlz23jV for ; Fri, 27 Oct 2023 05:01:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B99B9385DC33 for ; Thu, 26 Oct 2023 18:01:35 +0000 (GMT) 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 7B606385B528 for ; Thu, 26 Oct 2023 18:01:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B606385B528 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7B606385B528 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698343284; cv=none; b=wMzNEoZ0tQ9KUi07oRe5npdHt/d4AomrjuMEbDWE+Aya4lxLOOf8ashW4E+0NvB3SNgoXknrWbuWQztw511WrViSFpKJODT/RA/7ffhr7+uS/IC9v91yz0d5ttBZDme/UuWw87WTwYsBcQlid/1VRiwQKS9eFyRILwD2fQNANNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698343284; c=relaxed/simple; bh=LaKeUX4FynR6r9W3OpNT6HR2IIGtHkaASr+CGq0XnvY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Gi3IWivjaVeOS6yy4evdT4VQbi6MaYHp76TMEOjCcdpk7eNjNvjBNDkEaG+SASCifrMyXgCtMBRVmZrIElyusMA4dTCUhnMWnwk0FePdFx3grQQ2i2oMkePojfG9KbeSuNnIumC+rCZhVctDGNypQZEOFWpXhOjTAB5PU9EjJHI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698343282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TLeOSeqoxA+FoN6c3osVrbnQwOzv+v+XRkJmhIfa8zw=; b=itgC/6ghI1fNV0zTWSqXHGP/BvbCjZLXdtXcvXyF06sGS0JV3FI38m9yJ0LFfaSSKtHGU8 j/hjTU7aLN2b+SrZyuI/HVSfLYsikmqWVF4iWPum2G7iF4lxT30Yf9dmrPj1SPT88LfR8W PFhb/Ep7CTgUd5G2QCef7owYQOzXY2k= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-zsldgb2SNha8kQdQ2tJveQ-1; Thu, 26 Oct 2023 14:01:20 -0400 X-MC-Unique: zsldgb2SNha8kQdQ2tJveQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7789405320fso164578685a.0 for ; Thu, 26 Oct 2023 11:01:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698343279; x=1698948079; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TLeOSeqoxA+FoN6c3osVrbnQwOzv+v+XRkJmhIfa8zw=; b=n9gbE4QrlQ5+bns1crqPJ0bktZW0flufmcfqWuDLXnTSoUTUJ6ZW+1r6sfgXb9t2L+ bHWlb0elP3WZJXS+SZC8DrUHnOvGAvo27qDg5LGuC9AyJh0J6g0xX2wtpi836Zlxio2X UxYZ3KwoUkrfFKbifmWYVKvCiiXsPAge4nXpNJINlyNLgqw3zTGQRYUgfkIR7CwqqSmR FDoxhVXtnybRph4ur2UefVibqNalx4yZBWBYxnGXUMG1MOaAP1T8tVGYFB8n4p/mFqEe r92OpYVEs4dmxhwyPQHz6F5vWcs9cfr5/NuNI8BuLcPrsOxn2X8GJQ61fY1MeXdZHs2X Gq8A== X-Gm-Message-State: AOJu0Yz/a+N3gB3TQYFTuvD6vCiC8LqMMc1xaaLDwzBRZV8/cMc7N1HJ V54fYdQ8iJH48pqyC7I8eJvdrGuO1t2X2glrDtVeOSvSpBPj/u50D2vy9UviVP44j7weT8faMy0 WYM+ezqpSZfykwkh0J+krRsa82HBUz3qAxsW21hj9C+sYcpB77xJbKaITzrW/UIBgLtuQHdf9Ao Y= X-Received: by 2002:a05:620a:410a:b0:774:3147:4251 with SMTP id j10-20020a05620a410a00b0077431474251mr71096qko.6.1698343279347; Thu, 26 Oct 2023 11:01:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHsPtAL+nXVF85FEvk4HwINoMqCFerrHJxVp/atO3Ly16RbKSQwoDND4hn/wIOg9V+J9/AqmQ== X-Received: by 2002:a05:620a:410a:b0:774:3147:4251 with SMTP id j10-20020a05620a410a00b0077431474251mr71031qko.6.1698343278637; Thu, 26 Oct 2023 11:01:18 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id v7-20020a05620a090700b0076f124abe4dsm5168218qkv.77.2023.10.26.11.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 11:01:18 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: simplify build_new_1 when in a template context Date: Thu, 26 Oct 2023 14:01:15 -0400 Message-ID: <20231026180115.1740432-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.482.g2e8e77cbac MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- Since when in a template context we end up just discarding the result of build_new_1, we don't have to bother with much of the code generation it performs. This patch makes the function exit early, returning a dummy non-erroneous result, once we've done pretty much all ahead of time checks that we could have. In passing avoid building up 'outer_nelts_check' in a template context too. gcc/cp/ChangeLog: * init.cc (build_new_1): Don't build 'outer_nelts_check' when in a template context. Exit early returning void_node when in a template context. Simplify the remainder of the function accordingly. --- gcc/cp/init.cc | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 6444f0a8518..fab1126ca65 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3281,7 +3281,7 @@ build_new_1 (vec **placement, tree type, tree nelts, return error_mark_node; } } - else + else if (!processing_template_decl) { /* When a runtime check is necessary because the array size isn't constant, keep only the top-most seven bits (starting @@ -3467,10 +3467,15 @@ build_new_1 (vec **placement, tree type, tree nelts, } } + if (processing_template_decl) + /* We've done pretty much all we can to check this non-dependent new-expr + ahead of time. Any further work is unnecessary since (build_new just + discards the result) and/or unsuitable inside a template context. */ + return void_node; + /* If we found a simple case of PLACEMENT_EXPR above, then copy it into a temporary variable. */ - if (!processing_template_decl - && TREE_CODE (alloc_call) == CALL_EXPR + if (TREE_CODE (alloc_call) == CALL_EXPR && call_expr_nargs (alloc_call) == 2 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))) @@ -3609,25 +3614,7 @@ build_new_1 (vec **placement, tree type, tree nelts, explicit_value_init_p = true; } - if (processing_template_decl) - { - /* Avoid an ICE when converting to a base in build_simple_base_path. - We'll throw this all away anyway, and build_new will create - a NEW_EXPR. */ - tree t = fold_convert (build_pointer_type (elt_type), data_addr); - /* build_value_init doesn't work in templates, and we don't need - the initializer anyway since we're going to throw it away and - rebuild it at instantiation time, so just build up a single - constructor call to get any appropriate diagnostics. */ - init_expr = cp_build_fold_indirect_ref (t); - if (type_build_ctor_call (elt_type)) - init_expr = build_special_member_call (init_expr, - complete_ctor_identifier, - init, elt_type, - LOOKUP_NORMAL, - complain); - } - else if (array_p) + if (array_p) { tree vecinit = NULL_TREE; const size_t len = vec_safe_length (*init); @@ -3721,8 +3708,7 @@ build_new_1 (vec **placement, tree type, tree nelts, object being initialized, replace them now and don't try to preevaluate. */ bool had_placeholder = false; - if (!processing_template_decl - && TREE_CODE (init_expr) == INIT_EXPR) + if (TREE_CODE (init_expr) == INIT_EXPR) TREE_OPERAND (init_expr, 1) = replace_placeholders (TREE_OPERAND (init_expr, 1), TREE_OPERAND (init_expr, 0), @@ -3760,7 +3746,7 @@ build_new_1 (vec **placement, tree type, tree nelts, alloc_fn, complain)); - if (cleanup && init_expr && !processing_template_decl) + if (cleanup && init_expr) /* Ack! First we allocate the memory. Then we set our sentry variable to true, and expand a cleanup that deletes the memory if sentry is true. Then we run the constructor, and