Revert "[llvm] Improve llvm.objectsize computation by computing GEP, alloca and mallo...
[llvm-project.git] / clang / test / CodeGen / tbaa-struct.cpp
blobca076ce5aa27327d91c0265b354e0f0297d6e86c
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - -O1 %s | \
2 // RUN: FileCheck -check-prefixes=CHECK,CHECK-OLD %s
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin -new-struct-path-tbaa \
4 // RUN: -emit-llvm -o - -O1 %s | \
5 // RUN: FileCheck -check-prefixes=CHECK,CHECK-NEW %s
6 //
7 // Check that we generate TBAA metadata for struct copies correctly.
9 struct A {
10 short s;
11 int i;
12 char c;
13 int j;
16 typedef A __attribute__((may_alias)) AA;
18 void copy(A *a1, A *a2) {
19 // CHECK-LABEL: _Z4copyP1AS0_
20 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, i64 16, i1 false)
21 // CHECK-OLD-SAME: !tbaa.struct [[TS:!.*]]
22 // CHECK-NEW-SAME: !tbaa [[TAG_A:![0-9]*]]
23 *a1 = *a2;
26 struct B {
27 char c;
28 A a;
29 int i;
32 void copy2(B *b1, B *b2) {
33 // CHECK-LABEL: _Z5copy2P1BS0_
34 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %{{.*}}, ptr noundef nonnull align 4 dereferenceable(24) %{{.*}}, i64 24, i1 false)
35 // CHECK-OLD-SAME: !tbaa.struct [[TS2:!.*]]
36 // CHECK-NEW-SAME: !tbaa [[TAG_B:![0-9]*]]
37 *b1 = *b2;
40 struct S {
41 _Complex char cc;
42 _Complex int ci;
45 union U {
46 _Complex int ci;
47 S s;
50 void copy3(U *u1, U *u2) {
51 // CHECK-LABEL: _Z5copy3P1US0_
52 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) %{{.*}}, ptr noundef nonnull align 4 dereferenceable(12) %{{.*}}, i64 12, i1 false)
53 // CHECK-OLD-SAME: !tbaa.struct [[TS3:!.*]]
54 // CHECK-NEW-SAME: !tbaa [[TAG_U:![0-9]*]]
55 *u1 = *u2;
58 // Make sure that zero-length bitfield works.
59 struct C {
60 char a;
61 int : 0; // Shall not be ignored; see r185018.
62 char b;
63 char c;
64 } __attribute__((ms_struct));
66 void copy4(C *c1, C *c2) {
67 // CHECK-LABEL: _Z5copy4P1CS0_
68 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(3) {{.*}}, ptr noundef nonnull align 1 dereferenceable(3) {{.*}}, i64 3, i1 false)
69 // CHECK-OLD-SAME: !tbaa.struct [[TS4:!.*]]
70 // CHECK-NEW-SAME: !tbaa [[TAG_C:![0-9]*]]
71 *c1 = *c2;
74 struct D {
75 char a;
76 int : 0;
77 char b;
78 char c;
81 void copy5(D *d1, D *d2) {
82 // CHECK-LABEL: _Z5copy5P1DS0_
83 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(6) {{.*}}, ptr noundef nonnull align 1 dereferenceable(6) {{.*}}, i64 6, i1 false)
84 // CHECK-OLD-SAME: !tbaa.struct [[TS5:!.*]]
85 // CHECK-NEW-SAME: !tbaa [[TAG_D:![0-9]*]]
86 *d1 = *d2;
89 void copy6(AA *a1, A *a2) {
90 // CHECK-LABEL: _Z5copy6P1AS0_
91 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, i64 16, i1 false)
92 // CHECK-OLD-SAME: !tbaa.struct [[TS]]
93 // CHECK-NEW-SAME: !tbaa [[TAG_char:![0-9]*]]
94 *a1 = *a2;
97 void copy7(A *a1, AA *a2) {
98 // CHECK-LABEL: _Z5copy7P1AS0_
99 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, ptr noundef nonnull align 4 dereferenceable(16) %{{.*}}, i64 16, i1 false)
100 // CHECK-OLD-SAME: !tbaa.struct [[TS]]
101 // CHECK-NEW-SAME: !tbaa [[TAG_char]]
102 *a1 = *a2;
105 struct NamedBitfields {
106 signed f0 : 9;
107 unsigned f1 : 2;
108 char f2;
109 double f3;
112 void copy8(NamedBitfields *a1, NamedBitfields *a2) {
113 // CHECK-LABEL: _Z5copy8P14NamedBitfieldsS0_
114 // CHECK: tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, i64 16, i1 false),
115 // CHECK-OLD-SAME: !tbaa.struct [[TS6:!.*]]
116 // CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields:!.+]], !tbaa.struct
117 *a1 = *a2;
120 struct NamedBitfields2 {
121 char a, b, c;
122 signed f0 : 3;
123 unsigned f1 : 4;
124 char f2 : 7;
125 double f3;
126 unsigned f4 : 4;
129 void copy9(NamedBitfields2 *a1, NamedBitfields2 *a2) {
130 // CHECK-LABEL: _Z5copy9P15NamedBitfields2S0_
131 // CHECK: tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %a1, ptr noundef nonnull align 8 dereferenceable(24) %a2, i64 24, i1 false),
132 // CHECK-OLD-SAME: !tbaa.struct [[TS7:!.*]]
133 // CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields2:!.+]], !tbaa.struct
134 *a1 = *a2;
137 // Test with unnamed bitfield at the start and in between named ones..
138 struct NamedBitfields3 {
139 unsigned : 11;
140 signed f0 : 9;
141 char : 2;
142 int f1 : 2;
143 double f2;
146 void copy10(NamedBitfields3 *a1, NamedBitfields3 *a2) {
147 // CHECK-LABEL: _Z6copy10P15NamedBitfields3S0_
148 // CHECK: tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, i64 16, i1 false),
149 // CHECK-OLD-SAME: !tbaa.struct [[TS8:!.*]]
150 // CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields3:!.+]], !tbaa.struct
151 *a1 = *a2;
154 union U2 {
155 double d;
156 float f;
159 struct UnionMember1 {
160 U2 u;
161 int p;
164 void copy11(UnionMember1 *a1, UnionMember1 *a2) {
165 // CHECK-LABEL: _Z6copy11P12UnionMember1S0_
166 // CHECK: tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, i64 16, i1 false),
167 // CHECK-OLD-SAME: !tbaa.struct [[TS9:!.*]]
168 // CHECK-NEW-SAME: !tbaa [[TAG_UnionMember1:!.+]], !tbaa.struct
169 *a1 = *a2;
172 struct UnionMember2 {
173 int p;
174 U2 u;
177 void copy12(UnionMember2 *a1, UnionMember2 *a2) {
178 // CHECK-LABEL: _Z6copy12P12UnionMember2S0_
179 // CHECK: tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, i64 16, i1 false),
180 // CHECK-OLD-SAME: !tbaa.struct [[TS10:!.*]]
181 // CHECK-NEW-SAME: !tbaa [[TAG_UnionMember2:!.+]], !tbaa.struct
183 *a1 = *a2;
186 // CHECK-OLD: [[TS]] = !{i64 0, i64 2, !{{.*}}, i64 4, i64 4, !{{.*}}, i64 8, i64 1, !{{.*}}, i64 12, i64 4, !{{.*}}}
187 // CHECK-OLD: [[CHAR:!.*]] = !{!"omnipotent char", !{{.*}}}
188 // CHECK-OLD: [[TAG_INT:!.*]] = !{[[INT:!.*]], [[INT]], i64 0}
189 // CHECK-OLD: [[INT]] = !{!"int", [[CHAR]]
190 // CHECK-OLD: [[TAG_CHAR:!.*]] = !{[[CHAR]], [[CHAR]], i64 0}
191 // (offset, size) = (0,1) char; (4,2) short; (8,4) int; (12,1) char; (16,4) int; (20,4) int
192 // CHECK-OLD: [[TS2]] = !{i64 0, i64 1, !{{.*}}, i64 4, i64 2, !{{.*}}, i64 8, i64 4, !{{.*}}, i64 12, i64 1, !{{.*}}, i64 16, i64 4, {{.*}}, i64 20, i64 4, {{.*}}}
193 // (offset, size) = (0,8) char; (0,2) char; (4,8) char
194 // CHECK-OLD: [[TS3]] = !{i64 0, i64 12, [[TAG_CHAR]]}
195 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, [[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
196 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, [[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
197 // CHECK-OLD: [[TS6]] = !{i64 0, i64 2, [[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
198 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
199 // CHECK-OLD [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
200 // CHECK-OLD: [[TS7]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, [[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 3, i64 2, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]], i64 16, i64 1, [[TAG_CHAR]]}
201 // CHECK-OLD: [[TS8]] = !{i64 0, i64 4, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]]}
202 // CHECK-OLD: [[TS9]] = !{i64 0, i64 8, [[TAG_CHAR]], i64 8, i64 4, [[TAG_INT]]}
203 // CHECK-OLD: [[TS10]] = !{i64 0, i64 4, [[TAG_INT]], i64 8, i64 8, [[TAG_CHAR]]}
205 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
206 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}
207 // CHECK-NEW-DAG: [[TYPE_short:!.*]] = !{[[TYPE_char]], i64 2, !"short"}
208 // CHECK-NEW-DAG: [[TYPE_int:!.*]] = !{[[TYPE_char]], i64 4, !"int"}
209 // CHECK-NEW-DAG: [[TYPE_A:!.*]] = !{[[TYPE_char]], i64 16, !"_ZTS1A", [[TYPE_short]], i64 0, i64 2, [[TYPE_int]], i64 4, i64 4, [[TYPE_char]], i64 8, i64 1, [[TYPE_int]], i64 12, i64 4}
210 // CHECK-NEW-DAG: [[TAG_A]] = !{[[TYPE_A]], [[TYPE_A]], i64 0, i64 16}
211 // CHECK-NEW-DAG: [[TYPE_B:!.*]] = !{[[TYPE_char]], i64 24, !"_ZTS1B", [[TYPE_char]], i64 0, i64 1, [[TYPE_A]], i64 4, i64 16, [[TYPE_int]], i64 20, i64 4}
212 // CHECK-NEW-DAG: [[TAG_B]] = !{[[TYPE_B]], [[TYPE_B]], i64 0, i64 24}
213 // CHECK-NEW-DAG: [[TAG_U]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 12}
214 // CHECK-NEW-DAG: [[TYPE_C:!.*]] = !{[[TYPE_char]], i64 3, !"_ZTS1C", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, i64 1, [[TYPE_char]], i64 2, i64 1}
215 // CHECK-NEW-DAG: [[TAG_C]] = !{[[TYPE_C]], [[TYPE_C]], i64 0, i64 3}
216 // CHECK-NEW-DAG: [[TYPE_D:!.*]] = !{[[TYPE_char]], i64 6, !"_ZTS1D", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 4, i64 1, [[TYPE_char]], i64 5, i64 1}
217 // CHECK-NEW-DAG: [[TAG_D]] = !{[[TYPE_D]], [[TYPE_D]], i64 0, i64 6}
218 // CHECK-NEW-DAG: [[TAG_NamedBitfields]] = !{[[TYPE_NamedBitfields:!.+]], [[TYPE_NamedBitfields]], i64 0, i64 16}
219 // CHECK-NEW-DAG: [[TYPE_NamedBitfields]] = !{[[TYPE_char]], i64 16, !"_ZTS14NamedBitfields", [[TYPE_int]], i64 0, i64 4, [[TYPE_int]], i64 1, i64 4, [[TYPE_char]], i64 2, i64 1, [[TYPE_double:!.+]], i64 8, i64 8}
220 // CHECK-NEW-DAG: [[TYPE_double]] = !{[[TYPE_char]], i64 8, !"double"}
221 // CHECK-NEW-DAG: [[TAG_NamedBitfields2]] = !{[[TYPE_NamedBitfields2:!.+]], [[TYPE_NamedBitfields2]], i64 0, i64 24}
222 // CHECK-NEW-DAG: [[TYPE_NamedBitfields2]] = !{[[TYPE_char]], i64 24, !"_ZTS15NamedBitfields2", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, i64 1, [[TYPE_char]], i64 2, i64 1, [[TYPE_int]], i64 3, i64 4, [[TYPE_int]], i64 3, i64 4, [[TYPE_char]], i64 4, i64 1, [[TYPE_double]], i64 8, i64 8, [[TYPE_int]], i64 16, i64 4}
223 // CHECK-NEW-DAG: [[TAG_NamedBitfields3]] = !{[[TYPE_NamedBitfields3:!.+]], [[TYPE_NamedBitfields3]], i64 0, i64 16}
224 // CHECK-NEW-DAG: [[TYPE_NamedBitfields3]] = !{[[TYPE_char]], i64 16, !"_ZTS15NamedBitfields3", [[TYPE_int]], i64 1, i64 4, [[TYPE_int]], i64 2, i64 4, [[TYPE_double]], i64 8, i64 8}
225 // CHECK-NEW-DAG: [[TAG_UnionMember1]] = !{[[TYPE_UnionMember1:!.+]], [[TYPE_UnionMember1]], i64 0, i64 16}
226 // CHECK-NEW-DAG: [[TYPE_UnionMember1]] = !{[[TYPE_char]], i64 16, !"_ZTS12UnionMember1", [[TYPE_char]], i64 0, i64 8, [[TYPE_int]], i64 8, i64 4}
227 // CHECK-NEW-DAG: [[TAG_UnionMember2]] = !{[[TYPE_UnionMember2:!.+]], [[TYPE_UnionMember2]], i64 0, i64 16}
228 // CHECK-NEW-DAG: [[TYPE_UnionMember2]] = !{[[TYPE_char]], i64 16, !"_ZTS12UnionMember2", [[TYPE_int]], i64 0, i64 4, [[TYPE_char]], i64 8, i64 8}