From patchwork Fri Sep 20 22:18:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 97781 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 588913858432 for ; Fri, 20 Sep 2024 22:18:57 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 382F93858C33 for ; Fri, 20 Sep 2024 22:18:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 382F93858C33 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 382F93858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726870704; cv=none; b=thnUkH7v+OAEkuig8G2vJt2jVs6COUrRV/bA5TJx8JyDQhLlvsFwXuYLvZovEaXeLKnZ1QAWbj9aHY858EmHYbmcJdWhg94Fsd+An3O/VtyU3uqGOdbNops6R3Mq46+G6f65FtuDnClfSo/wuBFCH1cgItk6jp8QqhkCmFvLzSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726870704; c=relaxed/simple; bh=z/obTTz4RzJXvevLpAkW1PLOYssgrjDawr25aLjJ/0A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oxS1wWXplKeOpvEU7z0aTRQbSDuNM3didUA5NBvqXUJclH5SuBCC6wENAuhPxwSKvfeiTbjPLI/DXAkuOkuKndrc+V/VELUQwpr/rNvG9OmZbuxdpOPZ5E/jFdyyGUBCkTt6cSHSFItxGOBE3cAawLCt6knSxU/Oiuw7o6bPdNk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726870702; 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=XWEEF8vV+E5oHBibw96PJ9O72hE1NV0/cxe4Rr1t4wc=; b=TpHScKqNJsc4+vJifIojWE3piVGAPzW/qbAeouvTrhIC/Pz1Wx9K2wn99PMbikoHy4A0Av JqcRRwfLX3qwUPBNJRKCzD4dKqW7qje9x8ZqKQYUayOaqU64qRgAe/2o7olorUos2xtiud 4mw51QAZeCS9yqdLbaAh6IP0XDyRTEI= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-176-Y_JtpKixNlKEc589bR65xw-1; Fri, 20 Sep 2024 18:18:19 -0400 X-MC-Unique: Y_JtpKixNlKEc589bR65xw-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 08BD2192DE26 for ; Fri, 20 Sep 2024 22:18:19 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.166]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4D4241956060; Fri, 20 Sep 2024 22:18:18 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Cc: Patrick Palka Subject: [PATCH] c++: compile time evaluation of prvalues [PR116416] Date: Fri, 20 Sep 2024 18:18:15 -0400 Message-ID: <20240920221815.694554-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_H3, 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~patchwork=sourceware.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This PR reports a missed optimization. When we have: Str str{"Test"}; callback(str); as in the test, we're able to evaluate the Str::Str() call at compile time. But when we have: callback(Str{"Test"}); we are not. With this patch (in fact, it's Patrick's patch with a little tweak), we turn callback (TARGET_EXPR >> (const char *) "Test" >>>>) into callback (TARGET_EXPR ) I explored the idea of calling maybe_constant_value for the whole TARGET_EXPR in cp_fold. That has three problems: - we can't always elide a TARGET_EXPR, so we'd have to make sure the result is also a TARGET_EXPR; - the resulting TARGET_EXPR must have the same flags, otherwise Bad Things happen; - getting a new slot is also problematic. I've seen a test where we had "TARGET_EXPR, D.2680", and folding the whole TARGET_EXPR would get us "TARGET_EXPR", but since we don't see the outer D.2680, we can't replace it with D.2681, and things break. With this patch, two tree-ssa tests regressed: pr78687.C and pr90883.C. FAIL: g++.dg/tree-ssa/pr90883.C scan-tree-dump dse1 "Deleted redundant store: .*.a = {}" is easy. Previously, we would call C::C, so .gimple has: D.2590 = {}; C::C (&D.2590); D.2597 = D.2590; return D.2597; Then .einline inlines the C::C call: D.2590 = {}; D.2590.a = {}; // #1 D.2590.b = 0; // #2 D.2597 = D.2590; D.2590 ={v} {CLOBBER(eos)}; return D.2597; then #2 is removed in .fre1, and #1 is removed in .dse1. So the test passes. But with the patch, .gimple won't have that C::C call, so the IL is of course going to look different. Unfortunately, pr78687.C is much more complicated and I can't explain precisely what happens there. But it seems like a good idea to have a way to avoid this optimization. So I've added the "noinline" check. PR c++/116416 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold_r) : Try to fold TARGET_EXPR_INITIAL and replace it with the folded result if it's TREE_CONSTANT. gcc/testsuite/ChangeLog: * g++.dg/analyzer/pr97116.C: Adjust dg-message. * g++.dg/cpp2a/consteval-prop2.C: Adjust dg-bogus. * g++.dg/tree-ssa/pr78687.C: Add __attribute__((noinline)). * g++.dg/tree-ssa/pr90883.C: Likewise. * g++.dg/cpp1y/constexpr-prvalue1.C: New test. Co-authored-by: Patrick Palka --- gcc/cp/cp-gimplify.cc | 14 +++++++++ gcc/testsuite/g++.dg/analyzer/pr97116.C | 2 +- .../g++.dg/cpp1y/constexpr-prvalue1.C | 29 +++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C | 2 +- gcc/testsuite/g++.dg/tree-ssa/pr78687.C | 5 +++- gcc/testsuite/g++.dg/tree-ssa/pr90883.C | 1 + 6 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-prvalue1.C base-commit: 9227a64495d5594613604573b72422e8e3722fc5 diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 003e68f1ea7..41d6333f650 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1473,6 +1473,20 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_) that case, strip it in favor of this one. */ if (tree &init = TARGET_EXPR_INITIAL (stmt)) { + tree fn; + if ((data->flags & ff_genericize) + /* Give the user an option to opt out. */ + && !((fn = current_function_decl) + && lookup_attribute ("noinline", + DECL_ATTRIBUTES (fn)))) + { + tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt)); + if (folded != init && TREE_CONSTANT (folded)) + { + init = folded; + break; + } + } cp_walk_tree (&init, cp_fold_r, data, NULL); cp_walk_tree (&TARGET_EXPR_CLEANUP (stmt), cp_fold_r, data, NULL); *walk_subtrees = 0; diff --git a/gcc/testsuite/g++.dg/analyzer/pr97116.C b/gcc/testsuite/g++.dg/analyzer/pr97116.C index d8e08a73172..1c404c2ceb2 100644 --- a/gcc/testsuite/g++.dg/analyzer/pr97116.C +++ b/gcc/testsuite/g++.dg/analyzer/pr97116.C @@ -16,7 +16,7 @@ struct foo void test_1 (void) { foo *p = new(NULL) foo (42); // { dg-warning "non-null expected" "warning" } - // { dg-message "argument 'this' \\(\[^\n\]*\\) NULL where non-null expected" "final event" { target *-*-* } .-1 } + // { dg-message "argument 'this'( \\(\[^\n\]*\\))? NULL where non-null expected" "final event" { target *-*-* } .-1 } } int test_2 (void) diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-prvalue1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-prvalue1.C new file mode 100644 index 00000000000..45e3a382056 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-prvalue1.C @@ -0,0 +1,29 @@ +// PR c++/116416 +// { dg-do compile { target c++14 } } + +struct Str { + constexpr Str() {} + constexpr Str(const char *instr) { + str = instr; length = 0; + for (auto index = 0; instr[index]; ++index) { + ++length; + } + } + const char *str = nullptr; + int length = 0; +}; +extern void callback(Str str); +void +func1() +{ + callback(Str{"Test"}); +} +void +func2() +{ + Str str{"Test"}; + callback(str); +} + +// Check that we don't call Str::Str(char const*) +// { dg-final { scan-assembler-not "_ZN3StrC1EPKc" } } diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C b/gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C index 30129a4a266..64c06fb0b7d 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-prop2.C @@ -68,7 +68,7 @@ test (int i) { int r = g (42) + g(i); int t = k(42) - + k(i); // { dg-bogus "call to|constant" "" { xfail *-*-* } } + + k(i); // { dg-bogus "call to|constant" } return r + t; } diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr78687.C b/gcc/testsuite/g++.dg/tree-ssa/pr78687.C index 698458f0e9a..7f5306bf841 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/pr78687.C +++ b/gcc/testsuite/g++.dg/tree-ssa/pr78687.C @@ -446,7 +446,10 @@ struct qual_option int quals_; }; -inline ref_proxy make_object_1() +// Compile-time evaluation of TARGET_EXPR's initials in cp_fold_r/TARGET_EXPR +// breaks this test. The attribute prevents that early evaluation. +__attribute__((noinline)) +ref_proxy make_object_1() { return ref_proxy(option_2()); } diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr90883.C b/gcc/testsuite/g++.dg/tree-ssa/pr90883.C index 37df17d0b16..f66124945b6 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/pr90883.C +++ b/gcc/testsuite/g++.dg/tree-ssa/pr90883.C @@ -7,6 +7,7 @@ int b{}; }; + __attribute__((noinline)) C slow() { return {};