xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/exprs.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // PR1895
4*f4a2713aSLionel Sambuc // sizeof function
5*f4a2713aSLionel Sambuc int zxcv(void);
6*f4a2713aSLionel Sambuc int x=sizeof(zxcv);
7*f4a2713aSLionel Sambuc int y=__alignof__(zxcv);
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc 
test(int * i)10*f4a2713aSLionel Sambuc void *test(int *i) {
11*f4a2713aSLionel Sambuc  short a = 1;
12*f4a2713aSLionel Sambuc  i += a;
13*f4a2713aSLionel Sambuc  i + a;
14*f4a2713aSLionel Sambuc  a + i;
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc _Bool test2b;
test2()18*f4a2713aSLionel Sambuc int test2() { if (test2b); return 0; }
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc // PR1921
test3()21*f4a2713aSLionel Sambuc int test3() {
22*f4a2713aSLionel Sambuc   const unsigned char *bp;
23*f4a2713aSLionel Sambuc   bp -= (short)1;
24*f4a2713aSLionel Sambuc }
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc // PR2080 - sizeof void
27*f4a2713aSLionel Sambuc int t1 = sizeof(void);
28*f4a2713aSLionel Sambuc int t2 = __alignof__(void);
test4()29*f4a2713aSLionel Sambuc void test4() {
30*f4a2713aSLionel Sambuc   t1 = sizeof(void);
31*f4a2713aSLionel Sambuc   t2 = __alignof__(void);
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc   t1 = sizeof(test4());
34*f4a2713aSLionel Sambuc   t2 = __alignof__(test4());
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc // 'const float' promotes to double in varargs.
test5(const float x,float float_number)38*f4a2713aSLionel Sambuc int test5(const float x, float float_number) {
39*f4a2713aSLionel Sambuc   return __builtin_isless(x, float_number);
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc // this one shouldn't fold
ola()43*f4a2713aSLionel Sambuc int ola() {
44*f4a2713aSLionel Sambuc   int a=2;
45*f4a2713aSLionel Sambuc   if ((0, (int)a) & 2) { return 1; }
46*f4a2713aSLionel Sambuc   return 2;
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc // this one shouldn't fold as well
eMaisUma()50*f4a2713aSLionel Sambuc void eMaisUma() {
51*f4a2713aSLionel Sambuc   double t[1];
52*f4a2713aSLionel Sambuc   if (*t)
53*f4a2713aSLionel Sambuc     return;
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc // rdar://6520707
f0(void (* fp)(void),void (* fp2)(void))57*f4a2713aSLionel Sambuc void f0(void (*fp)(void), void (*fp2)(void)) {
58*f4a2713aSLionel Sambuc   int x = fp - fp2;
59*f4a2713aSLionel Sambuc }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc // noop casts as lvalues.
62*f4a2713aSLionel Sambuc struct X {
63*f4a2713aSLionel Sambuc   int Y;
64*f4a2713aSLionel Sambuc };
65*f4a2713aSLionel Sambuc struct X foo();
bar()66*f4a2713aSLionel Sambuc int bar() {
67*f4a2713aSLionel Sambuc   return ((struct X)foo()).Y + 1;
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc // PR3809: INC/DEC of function pointers.
71*f4a2713aSLionel Sambuc void f2(void);
f1(void)72*f4a2713aSLionel Sambuc unsigned f1(void) {
73*f4a2713aSLionel Sambuc   void (*fp)(void) = f2;
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc   ++fp;
76*f4a2713aSLionel Sambuc   fp++;
77*f4a2713aSLionel Sambuc   --fp;
78*f4a2713aSLionel Sambuc   fp--;
79*f4a2713aSLionel Sambuc   return (unsigned) fp;
80*f4a2713aSLionel Sambuc }
81*f4a2713aSLionel Sambuc 
82*f4a2713aSLionel Sambuc union f3_x {int x; float y;};
f3()83*f4a2713aSLionel Sambuc int f3() {return ((union f3_x)2).x;}
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc union f4_y {int x; _Complex float y;};
f4()86*f4a2713aSLionel Sambuc _Complex float f4() {return ((union f4_y)(_Complex float)2.0).y;}
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc struct f5_a { int a; } f5_a;
89*f4a2713aSLionel Sambuc union f5_z {int x; struct f5_a y;};
f5()90*f4a2713aSLionel Sambuc struct f5_a f5() {return ((union f5_z)f5_a).y;}
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc // ?: in "lvalue"
93*f4a2713aSLionel Sambuc struct s6 { int f0; };
f6(int a0,struct s6 a1,struct s6 a2)94*f4a2713aSLionel Sambuc int f6(int a0, struct s6 a1, struct s6 a2) {
95*f4a2713aSLionel Sambuc   return (a0 ? a1 : a2).f0;
96*f4a2713aSLionel Sambuc }
97*f4a2713aSLionel Sambuc 
98*f4a2713aSLionel Sambuc // PR4026
f7()99*f4a2713aSLionel Sambuc void f7() {
100*f4a2713aSLionel Sambuc   __func__;
101*f4a2713aSLionel Sambuc }
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc // PR4067
f8()104*f4a2713aSLionel Sambuc int f8() {
105*f4a2713aSLionel Sambuc   return ({ foo(); }).Y;
106*f4a2713aSLionel Sambuc }
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc // rdar://6880558
109*f4a2713aSLionel Sambuc struct S;
110*f4a2713aSLionel Sambuc struct C {
111*f4a2713aSLionel Sambuc   int i;
112*f4a2713aSLionel Sambuc   struct S *tab[];
113*f4a2713aSLionel Sambuc };
114*f4a2713aSLionel Sambuc struct S { struct C c; };
f9(struct S * x)115*f4a2713aSLionel Sambuc void f9(struct S *x) {
116*f4a2713aSLionel Sambuc   foo(((void)1, x->c).tab[0]);
117*f4a2713aSLionel Sambuc }
118*f4a2713aSLionel Sambuc 
f10()119*f4a2713aSLionel Sambuc void f10() {
120*f4a2713aSLionel Sambuc   __builtin_sin(0);
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc // rdar://7530813
124*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @f11
f11(long X)125*f4a2713aSLionel Sambuc int f11(long X) {
126*f4a2713aSLionel Sambuc   int A[100];
127*f4a2713aSLionel Sambuc   return A[X];
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc // CHECK: [[Xaddr:%[^ ]+]] = alloca i64, align 8
130*f4a2713aSLionel Sambuc // CHECK: load {{.*}}* [[Xaddr]]
131*f4a2713aSLionel Sambuc // CHECK-NEXT: getelementptr inbounds [100 x i32]* %A, i32 0,
132*f4a2713aSLionel Sambuc // CHECK-NEXT: load i32*
133*f4a2713aSLionel Sambuc }
134*f4a2713aSLionel Sambuc 
f12()135*f4a2713aSLionel Sambuc int f12() {
136*f4a2713aSLionel Sambuc   // PR3150
137*f4a2713aSLionel Sambuc   // CHECK-LABEL: define i32 @f12
138*f4a2713aSLionel Sambuc   // CHECK: ret i32 1
139*f4a2713aSLionel Sambuc   return 1||1;
140*f4a2713aSLionel Sambuc }
141*f4a2713aSLionel Sambuc 
142*f4a2713aSLionel Sambuc // Make sure negate of fp uses -0.0 for proper -0 handling.
f13(double X)143*f4a2713aSLionel Sambuc double f13(double X) {
144*f4a2713aSLionel Sambuc   // CHECK-LABEL: define double @f13
145*f4a2713aSLionel Sambuc   // CHECK: fsub double -0.0
146*f4a2713aSLionel Sambuc   return -X;
147*f4a2713aSLionel Sambuc }
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc // Check operations on incomplete types.
f14(struct s14 * a)150*f4a2713aSLionel Sambuc void f14(struct s14 *a) {
151*f4a2713aSLionel Sambuc   (void) &*a;
152*f4a2713aSLionel Sambuc }
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @f15
f15()155*f4a2713aSLionel Sambuc void f15() {
156*f4a2713aSLionel Sambuc   extern void f15_start(void);
157*f4a2713aSLionel Sambuc   f15_start();
158*f4a2713aSLionel Sambuc   // CHECK: call void @f15_start()
159*f4a2713aSLionel Sambuc 
160*f4a2713aSLionel Sambuc   extern void *f15_v(void);
161*f4a2713aSLionel Sambuc   extern const void *f15_cv(void);
162*f4a2713aSLionel Sambuc   extern volatile void *f15_vv(void);
163*f4a2713aSLionel Sambuc   *f15_v(); *f15_v(), *f15_v(); f15_v() ? *f15_v() : *f15_v();
164*f4a2713aSLionel Sambuc   *f15_cv(); *f15_cv(), *f15_cv(); f15_cv() ? *f15_cv() : *f15_cv();
165*f4a2713aSLionel Sambuc   *f15_vv(); *f15_vv(), *f15_vv(); f15_vv() ? *f15_vv() : *f15_vv();
166*f4a2713aSLionel Sambuc   // CHECK-NOT: load
167*f4a2713aSLionel Sambuc   // CHECK: ret void
168*f4a2713aSLionel Sambuc }
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc // PR8967: this was crashing
171*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @f16()
f16()172*f4a2713aSLionel Sambuc void f16() {
173*f4a2713aSLionel Sambuc   __extension__({ goto lbl; });
174*f4a2713aSLionel Sambuc  lbl:
175*f4a2713aSLionel Sambuc   ;
176*f4a2713aSLionel Sambuc }
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc // PR13704: negative increment in i128 is not preserved.
179*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @f17()
f17()180*f4a2713aSLionel Sambuc void f17() {
181*f4a2713aSLionel Sambuc   extern void extfunc(__int128);
182*f4a2713aSLionel Sambuc   __int128 x = 2;
183*f4a2713aSLionel Sambuc   x--;
184*f4a2713aSLionel Sambuc   extfunc(x);
185*f4a2713aSLionel Sambuc // CHECK: add nsw i128 %{{.}}, -1
186*f4a2713aSLionel Sambuc }
187