From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 4CCA43858421; Thu, 8 Sep 2022 13:45:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CCA43858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662644754; bh=lo7BhjMJ589mOHb+c9K5KyMVh19yrVH8dSnhVHrBnLM=; h=From:To:Subject:Date:From; b=hRgfRYiiLsfw3bnv8X2kCdbZSFW1ZS2DA/2XzKNVV6bOVFCQpSjxDZI/wxGRk+UOr oWo3LnMxWYO00xRuW2wmEx3nnqNZfyL3bTBgbAXT8woJim77ZLnM8Y5IysCxXG17H8 hmDfN2b4Bh0mlwOwUPm+Rr+Fid+DwHl6zr8PeQ+k= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2540] c++: unnecessary instantiation of constexpr var [PR99130] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 95c7d5899521a9e266c68cbcc92edfd2cde8694e X-Git-Newrev: 4db3cb781c355341fa041e6b5bbbfc495c6a0fdb Message-Id: <20220908134554.4CCA43858421@sourceware.org> Date: Thu, 8 Sep 2022 13:45:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4db3cb781c355341fa041e6b5bbbfc495c6a0fdb commit r13-2540-g4db3cb781c355341fa041e6b5bbbfc495c6a0fdb Author: Patrick Palka Date: Thu Sep 8 09:45:45 2022 -0400 c++: unnecessary instantiation of constexpr var [PR99130] Here the use of 'value' from within an unevaluated context causes us to overeagerly instantiate it, via maybe_instantiate_decl called from mark_used, despite the use occurring in a context that doesn't require a definition. This seems to only affect constexpr variable specializations, though we used to have the same issue for constexpr function specializations until r6-1309-g81371eff9bc7ef made us delay their instantiation until necessary during constexpr evaluation. This patch expands upon the r6-1309 fix to make mark_used avoid unnecessarily instantiating constexpr variable specializations too, by pulling out from maybe_instantiate_decl the condition (decl_maybe_constant_var_p (decl) || (TREE_CODE (decl) == FUNCTION_DECL && DECL_OMP_DECLARE_REDUCTION_P (decl)) || undeduced_auto_decl (decl)) into each of its three callers (including mark_used), removing the problematic first test from mark_used, and simplifying accordingly. The net result is that only mark_used is changed because the other two callers, resolve_address_of_overloaded_function and decl_constant_var_p, already guard the call appropriately. (This relaxation of mark_used seems to be safe because during constexpr evaluation we already take care to instantiate a constexpr variable as necessary via decl_constant_value etc). PR c++/99130 gcc/cp/ChangeLog: * decl2.cc (maybe_instantiate_decl): Adjust function comment. Check VAR_OR_FUNCTION_DECL_P. Pull out the disjunction into ... (mark_used): ... here, removing the decl_maybe_constant_var_p part of it. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-decltype5.C: New test. Diff: --- gcc/cp/decl2.cc | 33 ++++++++---------------- gcc/testsuite/g++.dg/cpp0x/constexpr-decltype5.C | 23 +++++++++++++++++ 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 89ab2545d64..cd188813bee 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -5381,24 +5381,15 @@ possibly_inlined_p (tree decl) return true; } -/* Normally, we can wait until instantiation-time to synthesize DECL. - However, if DECL is a static data member initialized with a constant - or a constexpr function, we need it right now because a reference to - such a data member or a call to such function is not value-dependent. - For a function that uses auto in the return type, we need to instantiate - it to find out its type. For OpenMP user defined reductions, we need - them instantiated for reduction clauses which inline them by hand - directly. */ +/* If DECL is a function or variable template specialization, instantiate + its definition now. */ void maybe_instantiate_decl (tree decl) { - if (DECL_LANG_SPECIFIC (decl) + if (VAR_OR_FUNCTION_DECL_P (decl) + && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) - && (decl_maybe_constant_var_p (decl) - || (TREE_CODE (decl) == FUNCTION_DECL - && DECL_OMP_DECLARE_REDUCTION_P (decl)) - || undeduced_auto_decl (decl)) && !DECL_DECLARED_CONCEPT_P (decl) && !uses_template_parms (DECL_TI_ARGS (decl))) { @@ -5700,15 +5691,13 @@ mark_used (tree decl, tsubst_flags_t complain) return false; } - /* Normally, we can wait until instantiation-time to synthesize DECL. - However, if DECL is a static data member initialized with a constant - or a constexpr function, we need it right now because a reference to - such a data member or a call to such function is not value-dependent. - For a function that uses auto in the return type, we need to instantiate - it to find out its type. For OpenMP user defined reductions, we need - them instantiated for reduction clauses which inline them by hand - directly. */ - maybe_instantiate_decl (decl); + /* If DECL has a deduced return type, we need to instantiate it now to + find out its type. For OpenMP user defined reductions, we need them + instantiated for reduction clauses which inline them by hand directly. */ + if (undeduced_auto_decl (decl) + || (TREE_CODE (decl) == FUNCTION_DECL + && DECL_OMP_DECLARE_REDUCTION_P (decl))) + maybe_instantiate_decl (decl); if (processing_template_decl || in_template_function ()) return true; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-decltype5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-decltype5.C new file mode 100644 index 00000000000..54112262dfc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-decltype5.C @@ -0,0 +1,23 @@ +// PR c++/99130 +// { dg-do compile { target c++11 } } + +template +struct A { + static constexpr int value = T::nonexistent; +}; + +using type = const int; +using type = decltype(A::value); + +#if __cpp_variable_templates +struct B { + template + static constexpr int value = T::nonexistent; +}; + +template +constexpr int value = T::nonexistent; + +using type = decltype(B::value); +using type = decltype(value); +#endif