6454cdb50356732e253bc953b947e59f68005334
[gcc.git] / gcc / testsuite / g++.dg / opt / temp1.C
1 // PR c++/16405
2 // { dg-options "-O2" }
3 // { dg-do run }
4
5 // There should be exactly one temporary generated for the code in "f"
6 // below when optimizing -- for the result of "b + c". We have no
7 // easy way of checking that directly, so we count the number of calls
8 // to "memcpy", which is used on (some?) targets to copy temporaries.
9 // If there is more than two calls (one for coping "*this" to "t", and
10 // one for copying the temporary to "a"), then there are too many
11 // temporaries.
12
13 int i;
14
15 extern "C"
16 void *memcpy (void *dest, const void *src, __SIZE_TYPE__ n)
17 {
18 ++i;
19 }
20
21 struct T {
22 int a[128];
23 T &operator+=(T const &v) __attribute__((noinline));
24 T operator+(T const &v) const { T t = *this; t += v; return t; }
25 };
26
27 T &T::operator+=(T const &v) {
28 return *this;
29 }
30
31 T a, b, c;
32
33 void f() { a = b + c; }
34
35 int main () {
36 i = 0;
37 f();
38 if (i > 2)
39 return 1;
40 }