Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++2a %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s |
Richard Smith | 1fa07eb | 2019-06-22 21:30:43 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -std=c++2a %s -emit-llvm -o - -triple x86_64-linux-gnu -O2 -disable-llvm-passes | FileCheck %s --check-prefix=CHECK-OPT |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 3 | |
| 4 | struct A { ~A(); int n; char c[3]; }; |
| 5 | struct B { [[no_unique_address]] A a; char k; }; |
| 6 | // CHECK-DAG: @b = global { i32, [3 x i8], i8 } { i32 1, [3 x i8] c"\02\03\04", i8 5 } |
| 7 | B b = {1, 2, 3, 4, 5}; |
| 8 | |
| 9 | struct C : A {}; |
| 10 | struct D : C {}; |
| 11 | struct E { int e; [[no_unique_address]] D d; char k; }; |
| 12 | // CHECK-DAG: @e = global { i32, i32, [3 x i8], i8 } { i32 1, i32 2, [3 x i8] c"\03\04\05", i8 6 } |
| 13 | E e = {1, 2, 3, 4, 5, 6}; |
| 14 | |
| 15 | struct Empty1 {}; |
| 16 | struct Empty2 {}; |
| 17 | struct Empty3 {}; |
| 18 | struct HasEmpty { |
| 19 | [[no_unique_address]] Empty1 e1; |
| 20 | int a; |
| 21 | [[no_unique_address]] Empty2 e2; |
| 22 | int b; |
| 23 | [[no_unique_address]] Empty3 e3; |
| 24 | }; |
| 25 | // CHECK-DAG: @he = global %{{[^ ]*}} { i32 1, i32 2 } |
| 26 | HasEmpty he = {{}, 1, {}, 2, {}}; |
| 27 | |
| 28 | struct HasEmptyDuplicates { |
| 29 | [[no_unique_address]] Empty1 e1; // +0 |
| 30 | int a; |
| 31 | [[no_unique_address]] Empty1 e2; // +4 |
| 32 | int b; |
| 33 | [[no_unique_address]] Empty1 e3; // +8 |
| 34 | }; |
| 35 | // CHECK-DAG: @off1 = global i64 0 |
| 36 | Empty1 HasEmptyDuplicates::*off1 = &HasEmptyDuplicates::e1; |
| 37 | // CHECK-DAG: @off2 = global i64 4 |
| 38 | Empty1 HasEmptyDuplicates::*off2 = &HasEmptyDuplicates::e2; |
| 39 | // CHECK-DAG: @off3 = global i64 8 |
| 40 | Empty1 HasEmptyDuplicates::*off3 = &HasEmptyDuplicates::e3; |
| 41 | |
| 42 | // CHECK-DAG: @hed = global %{{[^ ]*}} { i32 1, i32 2, [4 x i8] undef } |
| 43 | HasEmptyDuplicates hed = {{}, 1, {}, 2, {}}; |
| 44 | |
| 45 | struct __attribute__((packed, aligned(2))) PackedAndPadded { |
| 46 | ~PackedAndPadded(); |
| 47 | char c; |
| 48 | int n; |
| 49 | }; |
| 50 | struct WithPackedAndPadded { |
| 51 | [[no_unique_address]] PackedAndPadded pap; |
| 52 | char d; |
| 53 | }; |
| 54 | // CHECK-DAG: @wpap = global <{ i8, i32, i8 }> <{ i8 1, i32 2, i8 3 }> |
| 55 | WithPackedAndPadded wpap = {1, 2, 3}; |
| 56 | |
| 57 | struct FieldOverlap { |
| 58 | [[no_unique_address]] Empty1 e1, e2, e3, e4; |
| 59 | int n; |
| 60 | }; |
| 61 | static_assert(sizeof(FieldOverlap) == 4); |
| 62 | // CHECK-DAG: @fo = global %{{[^ ]*}} { i32 1234 } |
| 63 | FieldOverlap fo = {{}, {}, {}, {}, 1234}; |
| 64 | |
| 65 | // CHECK-DAG: @e1 = constant %[[E1:[^ ]*]]* bitcast (%[[FO:[^ ]*]]* @fo to %[[E1]]*) |
| 66 | Empty1 &e1 = fo.e1; |
| 67 | // CHECK-DAG: @e2 = constant %[[E1]]* bitcast (i8* getelementptr (i8, i8* bitcast (%[[FO]]* @fo to i8*), i64 1) to %[[E1]]*) |
| 68 | Empty1 &e2 = fo.e2; |
| 69 | |
| 70 | // CHECK-LABEL: accessE1 |
| 71 | // CHECK: %[[RET:.*]] = bitcast %[[FO]]* %{{.*}} to %[[E1]]* |
| 72 | // CHECK: ret %[[E1]]* %[[RET]] |
| 73 | Empty1 &accessE1(FieldOverlap &fo) { return fo.e1; } |
| 74 | |
| 75 | // CHECK-LABEL: accessE2 |
| 76 | // CHECK: %[[AS_I8:.*]] = bitcast %[[FO]]* %{{.*}} to i8* |
| 77 | // CHECK: %[[ADJUSTED:.*]] = getelementptr inbounds i8, i8* %[[AS_I8]], i64 1 |
| 78 | // CHECK: %[[RET:.*]] = bitcast i8* %[[ADJUSTED]] to %[[E1]]* |
| 79 | // CHECK: ret %[[E1]]* %[[RET]] |
| 80 | Empty1 &accessE2(FieldOverlap &fo) { return fo.e2; } |
Richard Smith | 1fa07eb | 2019-06-22 21:30:43 +0000 | [diff] [blame] | 81 | |
| 82 | struct LaterDeclaredFieldHasLowerOffset { |
| 83 | int a; |
| 84 | int b; |
| 85 | [[no_unique_address]] Empty1 e; |
| 86 | }; |
| 87 | // CHECK-OPT-LABEL: @_Z41loadWhereLaterDeclaredFieldHasLowerOffset |
| 88 | int loadWhereLaterDeclaredFieldHasLowerOffset(LaterDeclaredFieldHasLowerOffset &a) { |
| 89 | // CHECK-OPT: getelementptr |
| 90 | // CHECK-OPT: load {{.*}}, !tbaa ![[TBAA_AB:[0-9]*]] |
| 91 | return a.b; |
| 92 | } |
| 93 | // Note, never emit TBAA for zero-size fields. |
| 94 | // CHECK-OPT: ![[TBAA_AB]] = !{![[TBAA_A:[0-9]*]], ![[TBAA_INT:[0-9]*]], i64 4} |
| 95 | // CHECK-OPT: ![[TBAA_A]] = !{!"_ZTS32LaterDeclaredFieldHasLowerOffset", ![[TBAA_INT]], i64 0, ![[TBAA_INT]], i64 4} |