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