blob: fce5f8b819725dab22b88545341d8c5c2a27d7e3 [file] [log] [blame]
Richard Smith78b239e2019-06-20 20:44:45 +00001// RUN: %clang_cc1 -std=c++2a %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
2
3struct A { ~A(); int n; char c[3]; };
4struct B { [[no_unique_address]] A a; char k; };
5// CHECK-DAG: @b = global { i32, [3 x i8], i8 } { i32 1, [3 x i8] c"\02\03\04", i8 5 }
6B b = {1, 2, 3, 4, 5};
7
8struct C : A {};
9struct D : C {};
10struct E { int e; [[no_unique_address]] D d; char k; };
11// CHECK-DAG: @e = global { i32, i32, [3 x i8], i8 } { i32 1, i32 2, [3 x i8] c"\03\04\05", i8 6 }
12E e = {1, 2, 3, 4, 5, 6};
13
14struct Empty1 {};
15struct Empty2 {};
16struct Empty3 {};
17struct HasEmpty {
18 [[no_unique_address]] Empty1 e1;
19 int a;
20 [[no_unique_address]] Empty2 e2;
21 int b;
22 [[no_unique_address]] Empty3 e3;
23};
24// CHECK-DAG: @he = global %{{[^ ]*}} { i32 1, i32 2 }
25HasEmpty he = {{}, 1, {}, 2, {}};
26
27struct HasEmptyDuplicates {
28 [[no_unique_address]] Empty1 e1; // +0
29 int a;
30 [[no_unique_address]] Empty1 e2; // +4
31 int b;
32 [[no_unique_address]] Empty1 e3; // +8
33};
34// CHECK-DAG: @off1 = global i64 0
35Empty1 HasEmptyDuplicates::*off1 = &HasEmptyDuplicates::e1;
36// CHECK-DAG: @off2 = global i64 4
37Empty1 HasEmptyDuplicates::*off2 = &HasEmptyDuplicates::e2;
38// CHECK-DAG: @off3 = global i64 8
39Empty1 HasEmptyDuplicates::*off3 = &HasEmptyDuplicates::e3;
40
41// CHECK-DAG: @hed = global %{{[^ ]*}} { i32 1, i32 2, [4 x i8] undef }
42HasEmptyDuplicates hed = {{}, 1, {}, 2, {}};
43
44struct __attribute__((packed, aligned(2))) PackedAndPadded {
45 ~PackedAndPadded();
46 char c;
47 int n;
48};
49struct WithPackedAndPadded {
50 [[no_unique_address]] PackedAndPadded pap;
51 char d;
52};
53// CHECK-DAG: @wpap = global <{ i8, i32, i8 }> <{ i8 1, i32 2, i8 3 }>
54WithPackedAndPadded wpap = {1, 2, 3};
55
56struct FieldOverlap {
57 [[no_unique_address]] Empty1 e1, e2, e3, e4;
58 int n;
59};
60static_assert(sizeof(FieldOverlap) == 4);
61// CHECK-DAG: @fo = global %{{[^ ]*}} { i32 1234 }
62FieldOverlap fo = {{}, {}, {}, {}, 1234};
63
64// CHECK-DAG: @e1 = constant %[[E1:[^ ]*]]* bitcast (%[[FO:[^ ]*]]* @fo to %[[E1]]*)
65Empty1 &e1 = fo.e1;
66// CHECK-DAG: @e2 = constant %[[E1]]* bitcast (i8* getelementptr (i8, i8* bitcast (%[[FO]]* @fo to i8*), i64 1) to %[[E1]]*)
67Empty1 &e2 = fo.e2;
68
69// CHECK-LABEL: accessE1
70// CHECK: %[[RET:.*]] = bitcast %[[FO]]* %{{.*}} to %[[E1]]*
71// CHECK: ret %[[E1]]* %[[RET]]
72Empty1 &accessE1(FieldOverlap &fo) { return fo.e1; }
73
74// CHECK-LABEL: accessE2
75// CHECK: %[[AS_I8:.*]] = bitcast %[[FO]]* %{{.*}} to i8*
76// CHECK: %[[ADJUSTED:.*]] = getelementptr inbounds i8, i8* %[[AS_I8]], i64 1
77// CHECK: %[[RET:.*]] = bitcast i8* %[[ADJUSTED]] to %[[E1]]*
78// CHECK: ret %[[E1]]* %[[RET]]
79Empty1 &accessE2(FieldOverlap &fo) { return fo.e2; }