c++: instantiate less for constant folding
authorJason Merrill <jason@redhat.com>
Thu, 14 Oct 2021 02:04:53 +0000 (22:04 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 15 Oct 2021 01:40:11 +0000 (21:40 -0400)
commit1595fe44e11a969d8ae462212886fb0a87b46226
tree597c4fe89c15dd41e714e99532514947bc5add95
parent5bb1e518b4a5b772665a22cc27245ece5bf82750
c++: instantiate less for constant folding

I've been experimenting with a change to make all inline functions
implicitly constexpr; this revealed that we are instantiating too
aggressively for speculative constant evaluation, leading to ordering
difficulties with e.g. is_a_helper<cgraph_node*>::test.  This patch tries to
avoid such instantiation until we actually need the function definition to
determine whether a call is constant, by limiting the initial instantiation
of all used functions to manifestly-constant-evaluated expressions, and
checking whether the function arguments are constant before instantiating
the function.

This change resulted in a change in the diagnostics for a few library tests
due to instantiating the function with the static_assert later (during
constant evaluation) than we did before (during instantiation of the
intermediate function).

gcc/cp/ChangeLog:

* constexpr.c (cxx_bind_parameters_in_call): Replace
new_call parameter with fun.
(cxx_eval_call_expression): Call it before instantiation.
(cxx_eval_outermost_constant_expr): Only instantiate fns
when manifestly_const_eval.
* typeck2.c (check_narrowing): This context is manifestly
constant-evaluated.

libstdc++-v3/ChangeLog:

* testsuite/20_util/integer_comparisons/greater_equal_neg.cc:
* testsuite/20_util/integer_comparisons/greater_neg.cc:
* testsuite/20_util/integer_comparisons/less_equal_neg.cc:
Adjust expected message.
* testsuite/lib/prune.exp: Prune 'in constexpr expansion'.

gcc/testsuite/ChangeLog:

* g++.dg/ext/vla22.C: Don't expect a narrowing error.
* g++.dg/cpp0x/constexpr-inst1.C: New test.
gcc/cp/constexpr.c
gcc/cp/typeck2.c
gcc/testsuite/g++.dg/cpp0x/constexpr-inst1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/vla22.C
libstdc++-v3/testsuite/20_util/integer_comparisons/greater_equal_neg.cc
libstdc++-v3/testsuite/20_util/integer_comparisons/greater_neg.cc
libstdc++-v3/testsuite/20_util/integer_comparisons/less_equal_neg.cc
libstdc++-v3/testsuite/lib/prune.exp