]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/53792 - [C++11] improving compiler-time constexpr evaluation
authormsebor <msebor@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 15 Mar 2016 03:05:17 +0000 (03:05 +0000)
committermsebor <msebor@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 15 Mar 2016 03:05:17 +0000 (03:05 +0000)
gcc/testsuite/ChangeLog:
2016-03-14  Martin Sebor  <msebor@redhat.com>

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

gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/constexpr-inline-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-inline.C [new file with mode: 0644]

index d9015bb2ed29a304a554f8ca9a5348601f10f286..e9b0a9aeed02e1c27ea5e5a805eb992c30c0cefb 100644 (file)
@@ -1,3 +1,9 @@
+2016-03-14  Martin Sebor  <msebor@redhat.com>
+
+       PR c++/53792
+       * g++.dg/cpp0x/constexpr-inline.C: New test.
+       * g++.dg/cpp0x/constexpr-inline-1.C: Same.
+
 2016-03-14  David Edelsohn  <dje.gcc@gmail.com>
 
        * 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 (file)
index 0000000..fed6990
--- /dev/null
@@ -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 <class T>
+void sink (T);
+  
+constexpr unsigned foo ()
+{
+  unsigned  i = 1;
+  while ((i << 1) > i)
+    i = i << 1;
+
+  return i;
+}
+
+template <unsigned N>
+struct S { };
+
+void bar ()
+{
+  sink (foo ());
+  sink (S<foo ()>());
+}
+
+// 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 (file)
index 0000000..d04257c
--- /dev/null
@@ -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" } }