1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm %s -o - | FileCheck %s
2 
3 // Capture the type and name so matching later is cleaner.
4 struct CompoundTy { int a; };
5 // CHECK: @MyCLH = constant [[MY_CLH:[^,]+]]
6 const struct CompoundTy *const MyCLH = &(struct CompoundTy){3};
7 
8 int* a = &(int){1};
9 struct s {int a, b, c;} * b = &(struct s) {1, 2, 3};
10 _Complex double * x = &(_Complex double){1.0f};
11 typedef int v4i32 __attribute((vector_size(16)));
12 v4i32 *y = &(v4i32){1,2,3,4};
13 
14 // Check generated code for GNU constant array init from compound literal,
15 // for a global variable.
16 // CHECK: @compound_array = global [8 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8]
17 int compound_array[] = __extension__(__builtin_choose_expr(0, 0, _Generic(1, int: (int[]){1, 2, 3, 4, 5, 6, 7, 8})));
18 
xxx()19 void xxx() {
20 int* a = &(int){1};
21 struct s {int a, b, c;} * b = &(struct s) {1, 2, 3};
22 _Complex double * x = &(_Complex double){1.0f};
23 }
24 
25 // CHECK-LABEL: define void @f()
f()26 void f() {
27   typedef struct S { int x,y; } S;
28   // CHECK: [[S:%[a-zA-Z0-9.]+]] = alloca [[STRUCT:%[a-zA-Z0-9.]+]],
29   struct S s;
30   // CHECK-NEXT: [[COMPOUNDLIT:%[a-zA-Z0-9.]+]] = alloca [[STRUCT]]
31   // CHECK-NEXT: [[CX:%[a-zA-Z0-9.]+]] = getelementptr inbounds [[STRUCT]], [[STRUCT]]* [[COMPOUNDLIT]], i32 0, i32 0
32   // CHECK-NEXT: [[SY:%[a-zA-Z0-9.]+]] = getelementptr inbounds [[STRUCT]], [[STRUCT]]* [[S]], i32 0, i32 1
33   // CHECK-NEXT: [[TMP:%[a-zA-Z0-9.]+]] = load i32, i32* [[SY]]
34   // CHECK-NEXT: store i32 [[TMP]], i32* [[CX]]
35   // CHECK-NEXT: [[CY:%[a-zA-Z0-9.]+]] = getelementptr inbounds [[STRUCT]], [[STRUCT]]* [[COMPOUNDLIT]], i32 0, i32 1
36   // CHECK-NEXT: [[SX:%[a-zA-Z0-9.]+]] = getelementptr inbounds [[STRUCT]], [[STRUCT]]* [[S]], i32 0, i32 0
37   // CHECK-NEXT: [[TMP:%[a-zA-Z0-9.]+]] = load i32, i32* [[SX]]
38   // CHECK-NEXT: store i32 [[TMP]], i32* [[CY]]
39   // CHECK-NEXT: [[SI8:%[a-zA-Z0-9.]+]] = bitcast [[STRUCT]]* [[S]] to i8*
40   // CHECK-NEXT: [[COMPOUNDLITI8:%[a-zA-Z0-9.]+]] = bitcast [[STRUCT]]* [[COMPOUNDLIT]] to i8*
41   // CHECK-NEXT: call void @llvm.memcpy{{.*}}(i8* align {{[0-9]+}} [[SI8]], i8* align {{[0-9]+}} [[COMPOUNDLITI8]]
42   s = (S){s.y,s.x};
43   // CHECK-NEXT: ret void
44 }
45 
46 // CHECK-LABEL: define i48 @g(
47 struct G { short x, y, z; };
g(int x,int y,int z)48 struct G g(int x, int y, int z) {
49   // CHECK:      [[RESULT:%.*]] = alloca [[G:%.*]], align 2
50   // CHECK-NEXT: [[X:%.*]] = alloca i32, align 4
51   // CHECK-NEXT: [[Y:%.*]] = alloca i32, align 4
52   // CHECK-NEXT: [[Z:%.*]] = alloca i32, align 4
53   // CHECK-NEXT: [[COERCE_TEMP:%.*]] = alloca i48
54   // CHECK-NEXT: store i32
55   // CHECK-NEXT: store i32
56   // CHECK-NEXT: store i32
57 
58   // Evaluate the compound literal directly in the result value slot.
59   // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[G]], [[G]]* [[RESULT]], i32 0, i32 0
60   // CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[X]], align 4
61   // CHECK-NEXT: [[T2:%.*]] = trunc i32 [[T1]] to i16
62   // CHECK-NEXT: store i16 [[T2]], i16* [[T0]], align 2
63   // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[G]], [[G]]* [[RESULT]], i32 0, i32 1
64   // CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[Y]], align 4
65   // CHECK-NEXT: [[T2:%.*]] = trunc i32 [[T1]] to i16
66   // CHECK-NEXT: store i16 [[T2]], i16* [[T0]], align 2
67   // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[G]], [[G]]* [[RESULT]], i32 0, i32 2
68   // CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[Z]], align 4
69   // CHECK-NEXT: [[T2:%.*]] = trunc i32 [[T1]] to i16
70   // CHECK-NEXT: store i16 [[T2]], i16* [[T0]], align 2
71   return (struct G) { x, y, z };
72 
73   // CHECK-NEXT: [[T0:%.*]] = bitcast i48* [[COERCE_TEMP]] to i8*
74   // CHECK-NEXT: [[T1:%.*]] = bitcast [[G]]* [[RESULT]] to i8*
75   // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align {{[0-9]+}} [[T0]], i8* align {{[0-9]+}} [[T1]], i64 6
76   // CHECK-NEXT: [[T0:%.*]] = load i48, i48* [[COERCE_TEMP]]
77   // CHECK-NEXT: ret i48 [[T0]]
78 }
79 
80 // We had a bug where we'd emit a new GlobalVariable for each time we used a
81 // const pointer to a variable initialized by a compound literal.
82 // CHECK-LABEL: define i32 @compareMyCLH() #0
compareMyCLH()83 int compareMyCLH() {
84   // CHECK: store i8* bitcast ([[MY_CLH]] to i8*)
85   const void *a = MyCLH;
86   // CHECK: store i8* bitcast ([[MY_CLH]] to i8*)
87   const void *b = MyCLH;
88   return a == b;
89 }
90 
91 // Check generated code for GNU constant array init from compound literal,
92 // for a local variable.
93 // CHECK-LABEL: define i32 @compound_array_fn()
94 // CHECK: [[COMPOUND_ARRAY:%.*]] = alloca [8 x i32]
95 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64({{.*}}, i64 32, i1 false)
compound_array_fn()96 int compound_array_fn() {
97   int compound_array[] = (int[]){1,2,3,4,5,6,7,8};
98   return compound_array[0];
99 }
100