Clang Project

clang_source_code/test/CodeGenCXX/cxx11-initializer-aggregate.cpp
1// RUN: %clang_cc1 -std=c++11 -S -emit-llvm -o - %s -triple x86_64-linux-gnu | FileCheck %s
2
3struct A { int a, b; int f(); };
4
5namespace NonAggregateCopyInAggregateInit { // PR32044
6  struct A { constexpr A(int n) : x(n), y() {} int x, y; } extern a;
7  // CHECK-DAG: @_ZN31NonAggregateCopyInAggregateInit1bE = global %{{.*}} { %[[A:.*]]* @_ZN31NonAggregateCopyInAggregateInit1aE }
8  struct B { A &p; } b{{a}};
9  // CHECK-DAG: @_ZGRN31NonAggregateCopyInAggregateInit1cE_ = internal global %[[A]] { i32 1, i32 0 }
10  // CHECK-DAG: @_ZN31NonAggregateCopyInAggregateInit1cE = global %{{.*}} { %{{.*}}* @_ZGRN31NonAggregateCopyInAggregateInit1cE_ }
11  struct C { A &&p; } c{{1}};
12}
13
14namespace NearlyZeroInit {
15  // CHECK-DAG: @_ZN14NearlyZeroInit1aE = global {{.*}} <{ i32 1, i32 2, i32 3, [120 x i32] zeroinitializer }>
16  int a[123] = {1, 2, 3};
17  // CHECK-DAG: @_ZN14NearlyZeroInit1bE = global {{.*}} { i32 1, <{ i32, [2147483647 x i32] }> <{ i32 2, [2147483647 x i32] zeroinitializer }> }
18  struct B { int n; int arr[1024 * 1024 * 1024 * 2u]; } b = {1, {2}};
19}
20
21namespace PR37560 {
22  union U {
23    char x;
24    int a;
25  };
26  // FIXME: [dcl.init]p2, the padding bits of the union object should be
27  // initialized to 0, not undef, which would allow us to collapse the tail
28  // of these arrays to zeroinitializer.
29  // CHECK-DAG: @_ZN7PR375601cE = global <{ { i8, [3 x i8] } }> <{ { i8, [3 x i8] } { i8 0, [3 x i8] undef } }>
30  U c[1] = {};
31  // CHECK-DAG: @_ZN7PR375601dE = global {{.*}} <{ { i8, [3 x i8] } { i8 97, [3 x i8] undef }, %"{{[^"]*}}" { i32 123 }, { i8, [3 x i8] } { i8 98, [3 x i8] undef }, { i8, [3 x i8] } { i8 0, [3 x i8] undef },
32  U d[16] = {'a', {.a = 123}, 'b'};
33  // CHECK-DAG: @_ZN7PR375601eE = global {{.*}} <{ %"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, { i8, [3 x i8] } { i8 0, [3 x i8] undef },
34  U e[16] = {{.a = 123}, {.a = 456}};
35
36  union V {
37    int a;
38    char x;
39  };
40  // CHECK-DAG: @_ZN7PR375601fE = global [1 x %"{{[^"]*}}"] zeroinitializer
41  V f[1] = {};
42  // CHECK-DAG: @_ZN7PR375601gE = global {{.*}} <{ { i8, [3 x i8] } { i8 97, [3 x i8] undef }, %"{{[^"]*}}" { i32 123 }, { i8, [3 x i8] } { i8 98, [3 x i8] undef }, [13 x %"{{[^"]*}}"] zeroinitializer }>
43  V g[16] = {{.x = 'a'}, {.a = 123}, {.x = 'b'}};
44  // CHECK-DAG: @_ZN7PR375601hE = global {{.*}} <{ %"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, [14 x %"{{[^"]*}}"] zeroinitializer }>
45  V h[16] = {{.a = 123}, {.a = 456}};
46  // CHECK-DAG: @_ZN7PR375601iE = global [4 x %"{{[^"]*}}"] [%"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, %"{{[^"]*}}" zeroinitializer, %"{{[^"]*}}" zeroinitializer]
47  V i[4] = {{.a = 123}, {.a = 456}};
48}
49
50// CHECK-LABEL: define {{.*}}@_Z3fn1i(
51int fn1(int x) {
52  // CHECK: %[[INITLIST:.*]] = alloca %struct.A
53  // CHECK: %[[A:.*]] = getelementptr inbounds %struct.A, %struct.A* %[[INITLIST]], i32 0, i32 0
54  // CHECK: store i32 %{{.*}}, i32* %[[A]], align 4
55  // CHECK: %[[B:.*]] = getelementptr inbounds %struct.A, %struct.A* %[[INITLIST]], i32 0, i32 1
56  // CHECK: store i32 5, i32* %[[B]], align 4
57  // CHECK: call i32 @_ZN1A1fEv(%struct.A* %[[INITLIST]])
58  return A{x, 5}.f();
59}
60
61struct B { int &r; int &f() { return r; } };
62
63// CHECK-LABEL: define {{.*}}@_Z3fn2Ri(
64int &fn2(int &v) {
65  // CHECK: %[[INITLIST2:.*]] = alloca %struct.B, align 8
66  // CHECK: %[[R:.*]] = getelementptr inbounds %struct.B, %struct.B* %[[INITLIST2:.*]], i32 0, i32 0
67  // CHECK: store i32* %{{.*}}, i32** %[[R]], align 8
68  // CHECK: call dereferenceable({{[0-9]+}}) i32* @_ZN1B1fEv(%struct.B* %[[INITLIST2:.*]])
69  return B{v}.f();
70}
71
72// CHECK-LABEL: define {{.*}}@__cxx_global_var_init(
73//
74// CHECK: call {{.*}}@_ZN14NonTrivialInit1AC1Ev(
75// CHECK: getelementptr inbounds {{.*}}, i64 1
76// CHECK: br i1
77//
78// CHECK: getelementptr inbounds {{.*}}, i64 1
79// CHECK: icmp eq {{.*}}, i64 30
80// CHECK: br i1
81//
82// CHECK: call i32 @__cxa_atexit(
83namespace NonTrivialInit {
84  struct A { A(); A(const A&) = delete; ~A(); };
85  struct B { A a[20]; };
86  // NB, this must be large enough to be worth memsetting for this test to be
87  // meaningful.
88  B b[30] = {};
89}
90
91namespace ZeroInit {
92  enum { Zero, One };
93  constexpr int zero() { return 0; }
94  constexpr int *null() { return nullptr; }
95  struct Filler {
96    int x;
97    Filler();
98  };
99  struct S1 {
100    int x;
101  };
102
103  // These declarations, if implemented elementwise, require huge
104  // amout of memory and compiler time.
105  unsigned char data_1[1024 * 1024 * 1024 * 2u] = { 0 };
106  unsigned char data_2[1024 * 1024 * 1024 * 2u] = { Zero };
107  unsigned char data_3[1024][1024][1024] = {{{0}}};
108  unsigned char data_4[1024 * 1024 * 1024 * 2u] = { zero() };
109  int *data_5[1024 * 1024 * 512] = { nullptr };
110  int *data_6[1024 * 1024 * 512] = { null() };
111  struct S1 data_7[1024 * 1024 * 512] = {{0}};
112  char data_8[1000 * 1000 * 1000] = {};
113  int (&&data_9)[1000 * 1000 * 1000] = {0};
114  unsigned char data_10[1024 * 1024 * 1024 * 2u] = { 1 };
115  unsigned char data_11[1024 * 1024 * 1024 * 2u] = { One };
116  unsigned char data_12[1024][1024][1024] = {{{1}}};
117
118  // This variable must be initialized elementwise.
119  Filler data_e1[1024] = {};
120  // CHECK: getelementptr inbounds {{.*}} @_ZN8ZeroInit7data_e1E
121}
122