From patchwork Tue Feb 21 19:10:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1745846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=mnQBkJ5E; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PLpmG6WHkz240n for ; Wed, 22 Feb 2023 06:11:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C0845384F490 for ; Tue, 21 Feb 2023 19:11:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0845384F490 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677006664; bh=CpHo4AwwQ/8Ksn+wPlmkS52d1fbQGJpeVK8juq97+/g=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=mnQBkJ5EYqlgL36ntd0/U4r3Krih7vBsCWo7ZaPr2X0dnXZGzHrPprrXVCdD3d+oI zHQxShUE9plpQ0LjMY/37FnXTvKZ9Cy4bWyXYINfXtXNLVrfvgGftxsnfVsxhWWThv zM888sMPeR3t9TG15tMNRAcrKrW5UPAHAi2joWmo= 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 79A13385B523 for ; Tue, 21 Feb 2023 19:10:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 79A13385B523 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-237-SIWIzmP_O-ijEMwc5c85bA-1; Tue, 21 Feb 2023 14:10:42 -0500 X-MC-Unique: SIWIzmP_O-ijEMwc5c85bA-1 Received: by mail-qt1-f200.google.com with SMTP id fu21-20020a05622a5d9500b003bf9ece0541so1766574qtb.16 for ; Tue, 21 Feb 2023 11:10:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=CpHo4AwwQ/8Ksn+wPlmkS52d1fbQGJpeVK8juq97+/g=; b=JH3TeM2TwGnE4UTGiyWhwwRmAm01BGY8fhVRfWDQ7wbBYS9kYN3/CkxZM4h9gBuI5a FbLidpcpOSP3W4lJeRdI+Y4M0eWipR3WsAE0b0js42Q6fR0y8daBDWdoGV7ME0WpmeMK nJe2qorCJBD4QIZeEP+4oSQQN7GZj4oHf1W821iuMx5mE1iHwv1PvX3jLZL8y3L2sDac fDDg2Z02Dcw613JliqA/1tLl+PbDEopvWzyvNkDPBqUeTS+JDP0b8cNRAGD2mopwguT4 zYTz23mo4JY2HoTdyK9xkLPE3xQqdekgnfXgwJ0pd6QDqWJfnqlce0mGgT3ff5sr5d7l DbKg== X-Gm-Message-State: AO0yUKVqoBSsDVY1iIx+5KqcGIOL1e9T0NgtnhrSf2f5yKjec8fN/pGo 8SqbinLp5cpGHkNADcOl/BW9B0FeyQJV1lILC/A7OlcLXjwiRUNXgcoH8Lc1HAlucHVQqteJ2H4 YAIW0aX9XaS45IoGB1bKe6xl36dLduaGsoB1I+diEnIIS6Ix0ccSrrU117NB1/xkrSZXdUkRb X-Received: by 2002:a05:622a:4ca:b0:3b8:2a6c:d1e4 with SMTP id q10-20020a05622a04ca00b003b82a6cd1e4mr9199654qtx.23.1677006640124; Tue, 21 Feb 2023 11:10:40 -0800 (PST) X-Google-Smtp-Source: AK7set9N2ti673JnuMX1Zz5ImzjtHlfW0Ld7d+d+rtfrZoF2fLYCB1E06C7HlgXDoifPuUprIdhEbg== X-Received: by 2002:a05:622a:4ca:b0:3b8:2a6c:d1e4 with SMTP id q10-20020a05622a04ca00b003b82a6cd1e4mr9199601qtx.23.1677006639678; Tue, 21 Feb 2023 11:10:39 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id bn9-20020a05622a1dc900b003b9bb59543fsm1524319qtb.61.2023.02.21.11.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 11:10:39 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: more mce_false folding from cp_fully_fold_init [PR108243] Date: Tue, 21 Feb 2023 14:10:36 -0500 Message-Id: <20230221191036.1140927-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.2.501.gd9d677b2d8 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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=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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" We should also fold the overall initializer passed to cp_fully_fold_init with mce_false, which enables folding of the copy-initialization of 'a1' in the below testcase (the initializer here is an AGGR_INIT_EXPR). Unfortunately this doesn't help with direct- or default-initialization because we don't call cp_fully_fold_init in that case, and even if we did the initializer in that case is expressed as a bare CALL_EXPR instead of an AGGR_INIT_EXPR, which cp_fully_fold_init can't really fold. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/108243 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fully_fold): Add an internal overload that additionally takes and propagate an mce_value parameter, and define the existing public overload in terms of it. (cp_fully_fold_init): Pass mce_false to cp_fully_fold. gcc/testsuite/ChangeLog: * g++.dg/opt/is_constant_evaluated3.C: New test. --- gcc/cp/cp-gimplify.cc | 14 +++++++---- .../g++.dg/opt/is_constant_evaluated3.C | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 32fe53521cc..5d5c6efb856 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -2447,8 +2447,8 @@ cp_fold_rvalue (tree x) /* Perform folding on expression X. */ -tree -cp_fully_fold (tree x) +static tree +cp_fully_fold (tree x, mce_value manifestly_const_eval) { if (processing_template_decl) return x; @@ -2456,7 +2456,7 @@ cp_fully_fold (tree x) have to call both. */ if (cxx_dialect >= cxx11) { - x = maybe_constant_value (x); + x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval); /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into a TARGET_EXPR; undo that here. */ if (TREE_CODE (x) == TARGET_EXPR) @@ -2469,6 +2469,12 @@ cp_fully_fold (tree x) return cp_fold_rvalue (x); } +tree +cp_fully_fold (tree x) +{ + return cp_fully_fold (x, mce_unknown); +} + /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform in some cases. */ @@ -2477,7 +2483,7 @@ cp_fully_fold_init (tree x) { if (processing_template_decl) return x; - x = cp_fully_fold (x); + x = cp_fully_fold (x, mce_false); cp_fold_data data (ff_mce_false); cp_walk_tree (&x, cp_fold_r, &data, NULL); return x; diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C new file mode 100644 index 00000000000..0a1e46e5638 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C @@ -0,0 +1,23 @@ +// PR c++/108243 +// { dg-do compile { target c++11 } } +// { dg-additional-options "-O -fdump-tree-original" } + +struct A { + constexpr A(int n) : n(n), m(__builtin_is_constant_evaluated()) { } + constexpr A() : A(42) { } + int n, m; +}; + +int main() { + A a1 = {42}; + A a2{42}; + A a3(42); + A a4; + A a5{}; +} + +// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" } } +// { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } +// { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } +// { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } +// { dg-final { scan-tree-dump "a5 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }