xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/temp-order.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // Output file should have no calls to error() with folding.
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-unknown-unknown -mllvm -inline-threshold=1024 -O3 -emit-llvm -o %t %s
3*f4a2713aSLionel Sambuc // RUN: FileCheck %s < %t
4*f4a2713aSLionel Sambuc 
pow(unsigned Base,unsigned Power)5*f4a2713aSLionel Sambuc static unsigned pow(unsigned Base, unsigned Power) {
6*f4a2713aSLionel Sambuc   unsigned Val = 1;
7*f4a2713aSLionel Sambuc   while (Power--)
8*f4a2713aSLionel Sambuc     Val *= Base;
9*f4a2713aSLionel Sambuc   return Val;
10*f4a2713aSLionel Sambuc }
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc struct TempTracker {
13*f4a2713aSLionel Sambuc   unsigned Product, Index;
14*f4a2713aSLionel Sambuc 
TempTrackerTempTracker15*f4a2713aSLionel Sambuc   TempTracker() : Product(1), Index(0) {}
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc };
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc // FIXME: This can be used to check elision as well, if P = 0 hacks are removed.
20*f4a2713aSLionel Sambuc struct A {
21*f4a2713aSLionel Sambuc   TempTracker &TT;
22*f4a2713aSLionel Sambuc   mutable unsigned P;
23*f4a2713aSLionel Sambuc   bool Truth;
24*f4a2713aSLionel Sambuc 
AA25*f4a2713aSLionel Sambuc   A(TempTracker &_TT, unsigned _P, bool _Truth = true)
26*f4a2713aSLionel Sambuc     : TT(_TT), P(_P), Truth(_Truth) {}
AA27*f4a2713aSLionel Sambuc   A(const A &RHS) : TT(RHS.TT), P(RHS.P), Truth(RHS.Truth) { RHS.P = 0; }
~AA28*f4a2713aSLionel Sambuc   ~A() {
29*f4a2713aSLionel Sambuc     if (P)
30*f4a2713aSLionel Sambuc       TT.Product *= pow(P, ++TT.Index);
31*f4a2713aSLionel Sambuc   }
32*f4a2713aSLionel Sambuc 
operator =A33*f4a2713aSLionel Sambuc   A &operator=(const A &RHS) {
34*f4a2713aSLionel Sambuc     TT = RHS.TT;
35*f4a2713aSLionel Sambuc     P = RHS.P;
36*f4a2713aSLionel Sambuc     Truth = RHS.Truth;
37*f4a2713aSLionel Sambuc     RHS.P = 0;
38*f4a2713aSLionel Sambuc     return *this;
39*f4a2713aSLionel Sambuc   }
40*f4a2713aSLionel Sambuc 
operator boolA41*f4a2713aSLionel Sambuc   operator bool () { return Truth; }
42*f4a2713aSLionel Sambuc };
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc // 3, 7, 2
f0(bool val=false)45*f4a2713aSLionel Sambuc static unsigned f0(bool val = false) {
46*f4a2713aSLionel Sambuc   TempTracker tt;
47*f4a2713aSLionel Sambuc   {
48*f4a2713aSLionel Sambuc     A a(tt, 2);
49*f4a2713aSLionel Sambuc     if ((A(tt, 3), val))
50*f4a2713aSLionel Sambuc       A b(tt, 5);
51*f4a2713aSLionel Sambuc     A c(tt, 7);
52*f4a2713aSLionel Sambuc   }
53*f4a2713aSLionel Sambuc   return tt.Product;
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc // 3, 5, 7, 2
f1(bool val=true)57*f4a2713aSLionel Sambuc static unsigned f1(bool val = true) {
58*f4a2713aSLionel Sambuc   TempTracker tt;
59*f4a2713aSLionel Sambuc   {
60*f4a2713aSLionel Sambuc     A a(tt, 2);
61*f4a2713aSLionel Sambuc     if ((A(tt, 3), val))
62*f4a2713aSLionel Sambuc       A b(tt, 5);
63*f4a2713aSLionel Sambuc     A c(tt, 7);
64*f4a2713aSLionel Sambuc   }
65*f4a2713aSLionel Sambuc   return tt.Product;
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc // 5, 3, 7, 2
f2()69*f4a2713aSLionel Sambuc static unsigned f2() {
70*f4a2713aSLionel Sambuc   TempTracker tt;
71*f4a2713aSLionel Sambuc   {
72*f4a2713aSLionel Sambuc     A a(tt, 2);
73*f4a2713aSLionel Sambuc     if (A b = A(tt, 3))
74*f4a2713aSLionel Sambuc       A c(tt, 5);
75*f4a2713aSLionel Sambuc     A d(tt, 7);
76*f4a2713aSLionel Sambuc   }
77*f4a2713aSLionel Sambuc   return tt.Product;
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc // 7, 3, 11, 2
f3()81*f4a2713aSLionel Sambuc static unsigned f3() {
82*f4a2713aSLionel Sambuc   TempTracker tt;
83*f4a2713aSLionel Sambuc   {
84*f4a2713aSLionel Sambuc     A a(tt, 2);
85*f4a2713aSLionel Sambuc     if (A b = A(tt, 3, false))
86*f4a2713aSLionel Sambuc       A c(tt, 5);
87*f4a2713aSLionel Sambuc     else
88*f4a2713aSLionel Sambuc       A c(tt, 7);
89*f4a2713aSLionel Sambuc     A d(tt, 11);
90*f4a2713aSLionel Sambuc   }
91*f4a2713aSLionel Sambuc   return tt.Product;
92*f4a2713aSLionel Sambuc }
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc // 3, 7, 2
f4()95*f4a2713aSLionel Sambuc static unsigned f4() {
96*f4a2713aSLionel Sambuc   TempTracker tt;
97*f4a2713aSLionel Sambuc   {
98*f4a2713aSLionel Sambuc     A a(tt, 2);
99*f4a2713aSLionel Sambuc     while (A b = A(tt, 3, false))
100*f4a2713aSLionel Sambuc       A c(tt, 5);
101*f4a2713aSLionel Sambuc     A c(tt, 7);
102*f4a2713aSLionel Sambuc   }
103*f4a2713aSLionel Sambuc   return tt.Product;
104*f4a2713aSLionel Sambuc }
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc // 5, 3, 7, 2
f5()107*f4a2713aSLionel Sambuc static unsigned f5() {
108*f4a2713aSLionel Sambuc   TempTracker tt;
109*f4a2713aSLionel Sambuc   {
110*f4a2713aSLionel Sambuc     A a(tt, 2);
111*f4a2713aSLionel Sambuc     while (A b = A(tt, 3, true)) {
112*f4a2713aSLionel Sambuc       A c(tt, 5);
113*f4a2713aSLionel Sambuc       break;
114*f4a2713aSLionel Sambuc     }
115*f4a2713aSLionel Sambuc     A c(tt, 7);
116*f4a2713aSLionel Sambuc   }
117*f4a2713aSLionel Sambuc   return tt.Product;
118*f4a2713aSLionel Sambuc }
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc // 3, 7, 11, 5, 13, 2
f6()121*f4a2713aSLionel Sambuc static unsigned f6() {
122*f4a2713aSLionel Sambuc   TempTracker tt;
123*f4a2713aSLionel Sambuc   {
124*f4a2713aSLionel Sambuc     A a(tt, 2);
125*f4a2713aSLionel Sambuc     for (A b = (A(tt, 3), A(tt, 5)), c = (A(tt, 7), A(tt, 11));;)
126*f4a2713aSLionel Sambuc       break;
127*f4a2713aSLionel Sambuc     A c(tt, 13);
128*f4a2713aSLionel Sambuc   }
129*f4a2713aSLionel Sambuc   return tt.Product;
130*f4a2713aSLionel Sambuc }
131*f4a2713aSLionel Sambuc 
132*f4a2713aSLionel Sambuc // 5, 2
f7()133*f4a2713aSLionel Sambuc static unsigned f7() {
134*f4a2713aSLionel Sambuc   TempTracker tt;
135*f4a2713aSLionel Sambuc   {
136*f4a2713aSLionel Sambuc     (void)((A(tt, 2, false) && A(tt, 3, false)) || A(tt, 5, false));
137*f4a2713aSLionel Sambuc   }
138*f4a2713aSLionel Sambuc   return tt.Product;
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc // 5, 2
f8()142*f4a2713aSLionel Sambuc static unsigned f8() {
143*f4a2713aSLionel Sambuc   TempTracker tt;
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc   {
146*f4a2713aSLionel Sambuc     (void)((A(tt, 2) || A(tt, 3)) && A(tt, 5));
147*f4a2713aSLionel Sambuc   }
148*f4a2713aSLionel Sambuc   return tt.Product;
149*f4a2713aSLionel Sambuc }
150*f4a2713aSLionel Sambuc 
151*f4a2713aSLionel Sambuc extern "C" void error();
152*f4a2713aSLionel Sambuc extern "C" void print(const char *Name, unsigned N);
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc #define ORDER2(a, b) (pow(a, 1) * pow(b, 2))
155*f4a2713aSLionel Sambuc #define ORDER3(a, b, c) (ORDER2(a, b) * pow(c, 3))
156*f4a2713aSLionel Sambuc #define ORDER4(a, b, c, d) (ORDER3(a, b, c) * pow(d, 4))
157*f4a2713aSLionel Sambuc #define ORDER5(a, b, c, d, e) (ORDER4(a, b, c, d) * pow(e, 5))
158*f4a2713aSLionel Sambuc #define ORDER6(a, b, c, d, e, f) (ORDER5(a, b, c, d, e) * pow(f, 6))
test()159*f4a2713aSLionel Sambuc void test() {
160*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 1176)
161*f4a2713aSLionel Sambuc   print("f0", f0());
162*f4a2713aSLionel Sambuc   if (f0() != ORDER3(3, 7, 2))
163*f4a2713aSLionel Sambuc     error();
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 411600)
166*f4a2713aSLionel Sambuc   print("f1", f1());
167*f4a2713aSLionel Sambuc   if (f1() != ORDER4(3, 5, 7, 2))
168*f4a2713aSLionel Sambuc     error();
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 246960)
171*f4a2713aSLionel Sambuc   print("f2", f2());
172*f4a2713aSLionel Sambuc   if (f2() != ORDER4(5, 3, 7, 2))
173*f4a2713aSLionel Sambuc     error();
174*f4a2713aSLionel Sambuc 
175*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 1341648)
176*f4a2713aSLionel Sambuc   print("f3", f3());
177*f4a2713aSLionel Sambuc   if (f3() != ORDER4(7, 3, 11, 2))
178*f4a2713aSLionel Sambuc     error();
179*f4a2713aSLionel Sambuc 
180*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 1176)
181*f4a2713aSLionel Sambuc   print("f4", f4());
182*f4a2713aSLionel Sambuc   if (f4() != ORDER3(3, 7, 2))
183*f4a2713aSLionel Sambuc     error();
184*f4a2713aSLionel Sambuc 
185*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 246960)
186*f4a2713aSLionel Sambuc   print("f5", f5());
187*f4a2713aSLionel Sambuc   if (f5() != ORDER4(5, 3, 7, 2))
188*f4a2713aSLionel Sambuc     error();
189*f4a2713aSLionel Sambuc 
190*f4a2713aSLionel Sambuc // CHECK: call void @print(i8* {{.*}}, i32 1251552576)
191*f4a2713aSLionel Sambuc   print("f6", f6());
192*f4a2713aSLionel Sambuc   if (f6() != ORDER6(3, 7, 11, 5, 13, 2))
193*f4a2713aSLionel Sambuc     error();
194*f4a2713aSLionel Sambuc 
195*f4a2713aSLionel Sambuc //  CHECK: call void @print(i8* {{.*}}, i32 20)
196*f4a2713aSLionel Sambuc   print("f7", f7());
197*f4a2713aSLionel Sambuc   if (f7() != ORDER2(5, 2))
198*f4a2713aSLionel Sambuc     error();
199*f4a2713aSLionel Sambuc 
200*f4a2713aSLionel Sambuc //  CHECK: call void @print(i8* {{.*}}, i32 20)
201*f4a2713aSLionel Sambuc   print("f8", f8());
202*f4a2713aSLionel Sambuc   if (f8() != ORDER2(5, 2))
203*f4a2713aSLionel Sambuc     error();
204*f4a2713aSLionel Sambuc }
205*f4a2713aSLionel Sambuc 
206*f4a2713aSLionel Sambuc 
207*f4a2713aSLionel Sambuc 
208*f4a2713aSLionel Sambuc #ifdef HARNESS
209*f4a2713aSLionel Sambuc 
210*f4a2713aSLionel Sambuc #include <cstdlib>
211*f4a2713aSLionel Sambuc #include <cstdio>
212*f4a2713aSLionel Sambuc 
error()213*f4a2713aSLionel Sambuc extern "C" void error() {
214*f4a2713aSLionel Sambuc   abort();
215*f4a2713aSLionel Sambuc }
216*f4a2713aSLionel Sambuc 
print(const char * name,unsigned N)217*f4a2713aSLionel Sambuc extern "C" void print(const char *name, unsigned N) {
218*f4a2713aSLionel Sambuc   printf("%s: %d\n", name, N);
219*f4a2713aSLionel Sambuc }
220*f4a2713aSLionel Sambuc 
main()221*f4a2713aSLionel Sambuc int main() {
222*f4a2713aSLionel Sambuc   test();
223*f4a2713aSLionel Sambuc   return 0;
224*f4a2713aSLionel Sambuc }
225*f4a2713aSLionel Sambuc 
226*f4a2713aSLionel Sambuc #endif
227