From patchwork Mon Jul 17 21:18:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 121609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1350392vqt; Mon, 17 Jul 2023 14:19:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlFY/P8Nm+bobQvN59J3P4xB//wEWYVBal3zdixhelBgtr1Ij7FohJtnS197qh6BAEqx6iLx X-Received: by 2002:a17:906:224e:b0:978:ac42:5ef5 with SMTP id 14-20020a170906224e00b00978ac425ef5mr13031512ejr.65.1689628794024; Mon, 17 Jul 2023 14:19:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689628794; cv=none; d=google.com; s=arc-20160816; b=xoANlEsTfoJeXskV4/A7Ia/ZEAxmV1u8eKpvuqQqBLTbeVi0w/mTlbNC+qVf7mtFNe JJvy2S0wfFaV/IKMGXWT41nxKvVhprV/9Vy9BFwsETavu/E0tyWH98ETvwptccqTd7/J 5vyuysbLv3RnjRc0X9/7qJNqVneBpfqfI/6W4ZJ949VWZoUfx2nnwEW5uvFWrZ4SX27e k3ZnSl4kh1I9t2HUpJWXB4Ldvd0Y3A2IWyE2/A6A4QTB8h8pr/Q/c4Wf4v5zFEG0szfy OB2N4ZV9ueQESsrQ6pGMtv89Fq3LSfnrMnXgz96LkQRlZA8jvwBaJwz0DmRRwf/rvmYp 94qg== 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=OQOPRz9bj9Pu2IvxUWx++ZRB43cWvu7Q1NtRTN5VG3A=; fh=FHIbdYAvW7UcerCc2kkGnU8sznsIVp6Ma09wdfxeBu4=; b=ryIZ8ZyIYOFXFGl8Ah08eLNwfh5cmTpIxb9Dnhpv/BI6GNY36jKMT6bNwZhJFO6x1N nCfOtBU8Tvn/tKiTSW4QdDDP3zk0BQ/q94jHFrVb/S4fdqkj53k/wQE+PbDXoBF9m8Rw AobYOTAG61QaQFeVcuIQ45Mo0MDczcoHj+VfcKPH+CNwbEpxKw+o2yuIyAJ5ZL9VlbTk uK/HHTkhA9wK6nP3egJ8TUuYju8T3WvmEA70HjYN4fv5FPnHxdOf3/XUlFsy6PQBFjoo ljNS3rHCwOhBxt/k2i2OPYU6926XiUiIlN5bquuvah1pZgQYsIgQ7vQtXiwQz2xaz66Y oIZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Iu9aNYBX; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id x10-20020a1709064a8a00b00988a6421830si137292eju.979.2023.07.17.14.19.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 14:19:54 -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=Iu9aNYBX; 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 C339E385AFB2 for ; Mon, 17 Jul 2023 21:19:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C339E385AFB2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689628755; bh=OQOPRz9bj9Pu2IvxUWx++ZRB43cWvu7Q1NtRTN5VG3A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Iu9aNYBXEcWK5viS5j1rLkwbo3kXG9yecRAknAZ0qYHcEZ475CxcVaSRQzjsUbcrW DvdXjJMpd5DkS5YA8yjU/uj5jvaIsEdrmgClcjGITjYukJ1+cOQUdJVfeftnwikoqO FmZtcLH8c0U5PdhX6sPnVJYFTr6PQCMHiExY+9DM= 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 B79E73858017 for ; Mon, 17 Jul 2023 21:18:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B79E73858017 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-341-I96EXAc7MWWqCdE8Fgpnfw-1; Mon, 17 Jul 2023 17:18:12 -0400 X-MC-Unique: I96EXAc7MWWqCdE8Fgpnfw-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-403c1294d95so53607651cf.0 for ; Mon, 17 Jul 2023 14:18:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689628692; x=1692220692; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OQOPRz9bj9Pu2IvxUWx++ZRB43cWvu7Q1NtRTN5VG3A=; b=YQAuDmQtvWzLebcK2ZFmDgSbmr9MsUmTAVohJYdm+vhEfEhMSk8waP9J7CAg8PxAWc pgG+JV8MANe66Bu/G72BG+Dn8e35Dj0lxj8p8Cb6NvlFNOvZe4N6YVzl4THCfKG8Svx5 o6XF45Z9jhXBtmxnIFW05HVmdxBEcVK08shWPhd1Z01Pv3bwTm0+ygg0AGCzaTUsnYcQ s3EbnfyZ3hMvOjJExA7tfm2low6gO+5t6tAWhDLZCLYiajOPgaMqKbOQHxD/nswcQyS9 Av6NS6SyJ8jaP2i++Zq7Ld4mtbK9laYayx92FkLL5Ka41PUJBD0+1oRZe+eouwyRlQ3G OPkw== X-Gm-Message-State: ABy/qLZkD4vZlOKm41iQVdmNNQU5dbCWSDxhSwpi19vIFRZW258WJ+tg RTgdcpXBZf8B5KG0EcEKPcAQJoFpIr/MOx7IW4EpWzEGrnfBFQo2F4ZtU0YPSaGi0z555Vbd1c4 402MDviD/XRh1tp9I/tF7Zd9t7o8wuo4ouzpQS59ps50h3uNneNjh9P4IA12NBfJDbX9bqvCH6g == X-Received: by 2002:ac8:5ac3:0:b0:403:a7c0:c597 with SMTP id d3-20020ac85ac3000000b00403a7c0c597mr15798825qtd.19.1689628692036; Mon, 17 Jul 2023 14:18:12 -0700 (PDT) X-Received: by 2002:ac8:5ac3:0:b0:403:a7c0:c597 with SMTP id d3-20020ac85ac3000000b00403a7c0c597mr15798808qtd.19.1689628691658; Mon, 17 Jul 2023 14:18:11 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id b16-20020ac84f10000000b00403b44bc230sm131011qte.95.2023.07.17.14.18.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 14:18:10 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: only cache constexpr calls that are constant exprs Date: Mon, 17 Jul 2023 17:18:08 -0400 Message-Id: <20230717211808.946183-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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, T_SCC_BODY_TEXT_LINE 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771704202577379139 X-GMAIL-MSGID: 1771704202577379139 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- In reviewing Nathaniel's patch for PR70331, it occurred to me that instead of looking for various specific problematic things in the result of a constexpr call to decide whether to cache it, we should use reduced_constant_expression_p. The change to that function is to avoid crashing on uninitialized objects of non-class type. In a trial version of this patch I checked to see what cases this stopped caching; some were instances of partially-initialized return values, which seem fine to not cache. Some were returning pointers to expiring local variables, which we definitely want not to cache. And one was bit-cast3.C, which will be handled in a follow-up patch. gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_call_expression): Only cache reduced_constant_expression_p results. (reduced_constant_expression_p): Handle CONSTRUCTOR of scalar type. (cxx_eval_constant_expression): Fold vectors here. (cxx_eval_bare_aggregate): Not here. --- gcc/cp/constexpr.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) base-commit: caabf0973a4e9a26421c94d540e3e20051e93e77 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index c6f323ebf43..9d85c3be5cc 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3033,7 +3033,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, } else { - bool cacheable = true; + bool cacheable = !!entry; if (result && result != error_mark_node) /* OK */; else if (!DECL_SAVED_TREE (fun)) @@ -3185,7 +3185,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, for the constexpr evaluation and should not be cached. It is fine if the call allocates something and deallocates it too. */ - if (entry + if (cacheable && (save_heap_alloc_count != ctx->global->heap_vars.length () || (save_heap_dealloc_count != ctx->global->heap_dealloc_count))) @@ -3204,10 +3204,6 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, cacheable = false; break; } - /* Also don't cache a call that returns a deallocated pointer. */ - if (cacheable && (cp_walk_tree_without_duplicates - (&result, find_heap_var_refs, NULL))) - cacheable = false; } /* Rewrite all occurrences of the function's RESULT_DECL with the @@ -3217,6 +3213,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, && !is_empty_class (TREE_TYPE (res))) if (replace_decl (&result, res, ctx->object)) cacheable = false; + + /* Only cache a permitted result of a constant expression. */ + if (cacheable && !reduced_constant_expression_p (result)) + cacheable = false; } else /* Couldn't get a function copy to evaluate. */ @@ -3268,8 +3268,9 @@ reduced_constant_expression_p (tree t) case CONSTRUCTOR: /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ tree field; - if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) - /* An initialized vector would have a VECTOR_CST. */ + if (!AGGREGATE_TYPE_P (TREE_TYPE (t))) + /* A constant vector would be folded to VECTOR_CST. + A CONSTRUCTOR of scalar type means uninitialized. */ return false; if (CONSTRUCTOR_NO_CLEARING (t)) {