1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c2x -verify -emit-llvm %s -o - | FileCheck %s
2 // expected-no-diagnostics
4 struct S
{ int x
, y
; };
10 // CHECK: @[[CONST_T1:.+]] = private unnamed_addr constant %struct.T { i32 1, %struct.S zeroinitializer }
11 // CHECK: @[[CONST_T2:.+]] = private unnamed_addr constant %struct.T { i32 1, %struct.S { i32 2, i32 0 } }
15 // CHECK: define {{.*}} void @test_struct
17 // CHECK-NEXT: %[[S:.+]] = alloca %struct.S
18 // CHECK-NEXT: call void @llvm.memset.p0.i64({{.*}}%[[S]], i8 0, i64 8, i1 false)
23 // CHECK: define {{.*}} void @test_var
25 // CHECK-NEXT: %[[I:.+]] = alloca i32
26 // CHECK-NEXT: store i32 0, ptr %[[I]]
29 void test_simple_compound_literal() {
31 // CHECK: define {{.*}} void @test_simple_compound_literal
33 // CHECK-NEXT: %[[J:.+]] = alloca i32
34 // CHECK-NEXT: %[[COMPOUND:.+]] = alloca i32
35 // CHECK-NEXT: store i32 0, ptr %[[COMPOUND]]
36 // CHECK-NEXT: %[[MEM:.+]] = load i32, ptr %[[COMPOUND]]
37 // CHECK-NEXT: store i32 %[[MEM]], ptr %[[J]]
40 void test_zero_size_array() {
41 int unknown_size
[] = {};
42 // CHECK: define {{.*}} void @test_zero_size_array
44 // CHECK-NEXT: %[[UNKNOWN:.+]] = alloca [0 x i32]
49 int vla
[num_elts
] = {};
50 // CHECK: define {{.*}} void @test_vla
52 // CHECK-NEXT: %[[NUM_ELTS_PTR:.+]] = alloca i32
53 // CHECK: %[[VLA_EXPR:.+]] = alloca i64
54 // CHECK-NEXT: store i32 12, ptr %[[NUM_ELTS_PTR]]
55 // CHECK-NEXT: %[[NUM_ELTS:.+]] = load i32, ptr %[[NUM_ELTS_PTR]]
56 // CHECK-NEXT: %[[NUM_ELTS_EXT:.+]] = zext i32 %[[NUM_ELTS]] to i64
57 // CHECK: %[[VLA:.+]] = alloca i32, i64 %[[NUM_ELTS_EXT]]
58 // CHECK-NEXT: store i64 %[[NUM_ELTS_EXT]], ptr %[[VLA_EXPR]]
59 // CHECK-NEXT: %[[BYTES_TO_COPY:.+]] = mul nuw i64 %[[NUM_ELTS_EXT]], 4
60 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr {{.*}} %[[VLA]], i8 0, i64 %[[BYTES_TO_COPY]], i1 false)
63 void test_zero_size_vla() {
65 int vla
[num_elts
] = {};
66 // CHECK: define {{.*}} void @test_zero_size_vla
68 // CHECK-NEXT: %[[NUM_ELTS_PTR:.+]] = alloca i32
69 // CHECK: %[[VLA_EXPR:.+]] = alloca i64
70 // CHECK-NEXT: store i32 0, ptr %[[NUM_ELTS_PTR]]
71 // CHECK-NEXT: %[[NUM_ELTS:.+]] = load i32, ptr %[[NUM_ELTS_PTR]]
72 // CHECK-NEXT: %[[NUM_ELTS_EXT:.+]] = zext i32 %[[NUM_ELTS]] to i64
73 // CHECK: %[[VLA:.+]] = alloca i32, i64 %[[NUM_ELTS_EXT]]
74 // CHECK-NEXT: store i64 %[[NUM_ELTS_EXT]], ptr %[[VLA_EXPR]]
75 // CHECK-NEXT: %[[BYTES_TO_COPY:.+]] = mul nuw i64 %[[NUM_ELTS_EXT]], 4
76 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr {{.*}} %[[VLA]], i8 0, i64 %[[BYTES_TO_COPY]], i1 false)
79 void test_compound_literal_vla() {
81 int *compound_literal_vla
= (int[num_elts
]){};
82 // CHECK: define {{.*}} void @test_compound_literal_vla
84 // CHECK-NEXT: %[[NUM_ELTS_PTR:.+]] = alloca i32
85 // CHECK-NEXT: %[[COMP_LIT_VLA:.+]] = alloca ptr
86 // CHECK-NEXT: %[[COMP_LIT:.+]] = alloca i32
87 // CHECK-NEXT: store i32 12, ptr %[[NUM_ELTS_PTR]]
88 // CHECK-NEXT: %[[NUM_ELTS:.+]] = load i32, ptr %[[NUM_ELTS_PTR]]
89 // CHECK-NEXT: %[[NUM_ELTS_EXT:.+]] = zext i32 %[[NUM_ELTS]] to i64
90 // CHECK-NEXT: %[[BYTES_TO_COPY:.+]] = mul nuw i64 %[[NUM_ELTS_EXT]], 4
91 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr {{.*}} %[[COMP_LIT]], i8 0, i64 %[[BYTES_TO_COPY]], i1 false)
92 // CHECK-NEXT: store ptr %[[COMP_LIT]], ptr %[[COMP_LIT_VLA]]
95 void test_nested_structs() {
96 struct T t1
= { 1, {} };
97 struct T t2
= { 1, { 2, {} } };
98 struct T t3
= { (int){}, {} };
99 // CHECK: define {{.*}} void @test_nested_structs
100 // CHECK-NEXT: entry:
101 // CHECK-NEXT: %[[T1:.+]] = alloca %struct.T
102 // CHECK-NEXT: %[[T2:.+]] = alloca %struct.T
103 // CHECK-NEXT: %[[T3:.+]] = alloca %struct.T
104 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr {{.*}} %[[T1]], ptr {{.*}} @[[CONST_T1]], i64 12, i1 false)
105 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr {{.*}} %[[T2]], ptr {{.*}} @[[CONST_T2]], i64 12, i1 false)
106 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr {{.*}} %[[T3]], i8 0, i64 12, i1 false)
109 void test_vla_of_nested_structs(int num_elts
) {
110 struct T t3
[num_elts
] = {};
111 // CHECK: define {{.*}} void @test_vla_of_nested_structs(i32 noundef %[[NUM_ELTS_PARAM:.+]])
112 // CHECK-NEXT: entry:
113 // CHECK-NEXT: %[[NUM_ELTS_PTR:.+]] = alloca i32
114 // CHECK: %[[VLA_EXPR:.+]] = alloca i64
115 // CHECK-NEXT: store i32 %[[NUM_ELTS_PARAM]], ptr %[[NUM_ELTS_PTR]]
116 // CHECK-NEXT: %[[NUM_ELTS_LOCAL:.+]] = load i32, ptr %[[NUM_ELTS_PTR]]
117 // CHECK-NEXT: %[[NUM_ELTS_EXT:.+]] = zext i32 %[[NUM_ELTS_LOCAL]] to i64
118 // CHECK: %[[VLA:.+]] = alloca %struct.T, i64 %[[NUM_ELTS_EXT]]
119 // CHECK-NEXT: store i64 %[[NUM_ELTS_EXT]], ptr %[[VLA_EXPR]]
120 // CHECK-NEXT: %[[COPY_BYTES:.+]] = mul nuw i64 %[[NUM_ELTS_EXT]], 12
121 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr {{.*}} %[[VLA]], i8 0, i64 %[[COPY_BYTES]], i1 false)