From: msebor Date: Tue, 15 Mar 2016 03:05:17 +0000 (+0000) Subject: PR c++/53792 - [C++11] improving compiler-time constexpr evaluation X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7bad2faf72b5290c1f6b545952f529222759e006;p=thirdparty%2Fgcc.git PR c++/53792 - [C++11] improving compiler-time constexpr evaluation gcc/testsuite/ChangeLog: 2016-03-14 Martin Sebor PR c++/53792 * g++.dg/cpp0x/constexpr-inline.C: New test. * g++.dg/cpp0x/constexpr-inline-1.C: Same. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@234208 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d9015bb2ed29..e9b0a9aeed02 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2016-03-14 Martin Sebor + + PR c++/53792 + * g++.dg/cpp0x/constexpr-inline.C: New test. + * g++.dg/cpp0x/constexpr-inline-1.C: Same. + 2016-03-14 David Edelsohn * gcc.dg/torture/pr70083.c: Prune non-standard ABI. diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-inline-1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-inline-1.C new file mode 100644 index 000000000000..fed699049124 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-inline-1.C @@ -0,0 +1,29 @@ +// PR c++/53792 - [C++11] improving compiler-time constexpr evaluation +// Test case from comment #8. +// { dg-do compile { target c++14 } } +// { dg-additional-options "-O1 -fdump-tree-optimized" } + +template +void sink (T); + +constexpr unsigned foo () +{ + unsigned i = 1; + while ((i << 1) > i) + i = i << 1; + + return i; +} + +template +struct S { }; + +void bar () +{ + sink (foo ()); + sink (S()); +} + +// Verify that the call to the foo() constexpr function is inlined +// regardless of whether or not it's invoked in a constant expression. +// { dg-final { scan-tree-dump-not "= *foo *\\\(" "optimized" } } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-inline.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-inline.C new file mode 100644 index 000000000000..d04257c8c33f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-inline.C @@ -0,0 +1,40 @@ +// PR c++/53792 - [C++11] improving compiler-time constexpr evaluation +// { dg-do compile { target c++11 } } +// { dg-additional-options "-O1 -fdump-tree-optimized" } + +struct entry +{ + char const* label; + int value; +}; + +constexpr bool same (char const *x, char const *y) +{ + return !*x && !*y ? true : /* default */ (*x == *y && same (x + 1, y + 1)); +} + +constexpr int +keyToValue (char const *label, entry const *entries) +{ + return !entries->label ? entries->value + : same (entries->label, label) ? entries->value + : /* default */ keyToValue (label, entries + 1); +} + +constexpr entry foo[] = {{"Foo", 0}, {"Bar", 1}, {"FooBar", 2}, {0, -1}}; + +int bar () +{ + int result = keyToValue ("Foo", foo); + return result; +} + +int baz () +{ + constexpr int result = keyToValue ("Foo", foo); + return result; +} + +// Verify that the call to the keyToValue() constexpr function is inlined +// regardless of whether or not it's invoked in a constexpr expression. +// { dg-final { scan-tree-dump-not "keyToValue" "optimized" } }