xref: /llvm-project/clang/test/CodeGen/tbaa.cpp (revision 39db5e1ed87363a9ffea81e53520b542201b3262)
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -no-struct-path-tbaa -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefixes=PATH,OLD-PATH
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -new-struct-path-tbaa -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefixes=PATH,NEW-PATH
4 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O0 -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefix=NO-TBAA
5 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -relaxed-aliasing -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefix=NO-TBAA
6 // Test TBAA metadata generated by front-end.
7 //
8 // NO-TBAA-NOT: !tbaa
9 
10 typedef unsigned char uint8_t;
11 typedef unsigned short uint16_t;
12 typedef unsigned int uint32_t;
13 typedef unsigned long long uint64_t;
14 typedef struct
15 {
16    uint16_t f16;
17    uint32_t f32;
18    uint16_t f16_2;
19    uint32_t f32_2;
20 } StructA;
21 typedef struct
22 {
23    uint16_t f16;
24    StructA a;
25    uint32_t f32;
26 } StructB;
27 typedef struct
28 {
29    uint16_t f16;
30    StructB b;
31    uint32_t f32;
32 } StructC;
33 typedef struct
34 {
35    uint16_t f16;
36    StructB b;
37    uint32_t f32;
38    uint8_t f8;
39 } StructD;
40 
41 typedef struct
42 {
43    uint16_t f16;
44    uint32_t f32;
45 } StructS;
46 typedef struct
47 {
48    uint16_t f16;
49    uint32_t f32;
50 } StructS2;
51 
g(uint32_t * s,StructA * A,uint64_t count)52 uint32_t g(uint32_t *s, StructA *A, uint64_t count) {
53 // CHECK-LABEL: define{{.*}} i32 @_Z1g
54 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
55 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
56 // PATH-LABEL: define{{.*}} i32 @_Z1g
57 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
58 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32:!.*]]
59   *s = 1;
60   A->f32 = 4;
61   return *s;
62 }
63 
g2(uint32_t * s,StructA * A,uint64_t count)64 uint32_t g2(uint32_t *s, StructA *A, uint64_t count) {
65 // CHECK-LABEL: define{{.*}} i32 @_Z2g2
66 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
67 // CHECK: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i16:!.*]]
68 // PATH-LABEL: define{{.*}} i32 @_Z2g2
69 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
70 // PATH: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f16:!.*]]
71   *s = 1;
72   A->f16 = 4;
73   return *s;
74 }
75 
g3(StructA * A,StructB * B,uint64_t count)76 uint32_t g3(StructA *A, StructB *B, uint64_t count) {
77 // CHECK-LABEL: define{{.*}} i32 @_Z2g3
78 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
79 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
80 // PATH-LABEL: define{{.*}} i32 @_Z2g3
81 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
82 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_B_a_f32:!.*]]
83   A->f32 = 1;
84   B->a.f32 = 4;
85   return A->f32;
86 }
87 
g4(StructA * A,StructB * B,uint64_t count)88 uint32_t g4(StructA *A, StructB *B, uint64_t count) {
89 // CHECK-LABEL: define{{.*}} i32 @_Z2g4
90 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
91 // CHECK: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i16]]
92 // PATH-LABEL: define{{.*}} i32 @_Z2g4
93 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
94 // PATH: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_B_a_f16:!.*]]
95   A->f32 = 1;
96   B->a.f16 = 4;
97   return A->f32;
98 }
99 
g5(StructA * A,StructB * B,uint64_t count)100 uint32_t g5(StructA *A, StructB *B, uint64_t count) {
101 // CHECK-LABEL: define{{.*}} i32 @_Z2g5
102 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
103 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
104 // PATH-LABEL: define{{.*}} i32 @_Z2g5
105 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
106 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_B_f32:!.*]]
107   A->f32 = 1;
108   B->f32 = 4;
109   return A->f32;
110 }
111 
g6(StructA * A,StructB * B,uint64_t count)112 uint32_t g6(StructA *A, StructB *B, uint64_t count) {
113 // CHECK-LABEL: define{{.*}} i32 @_Z2g6
114 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
115 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
116 // PATH-LABEL: define{{.*}} i32 @_Z2g6
117 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
118 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_B_a_f32_2:!.*]]
119   A->f32 = 1;
120   B->a.f32_2 = 4;
121   return A->f32;
122 }
123 
g7(StructA * A,StructS * S,uint64_t count)124 uint32_t g7(StructA *A, StructS *S, uint64_t count) {
125 // CHECK-LABEL: define{{.*}} i32 @_Z2g7
126 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
127 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
128 // PATH-LABEL: define{{.*}} i32 @_Z2g7
129 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
130 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f32:!.*]]
131   A->f32 = 1;
132   S->f32 = 4;
133   return A->f32;
134 }
135 
g8(StructA * A,StructS * S,uint64_t count)136 uint32_t g8(StructA *A, StructS *S, uint64_t count) {
137 // CHECK-LABEL: define{{.*}} i32 @_Z2g8
138 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
139 // CHECK: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i16]]
140 // PATH-LABEL: define{{.*}} i32 @_Z2g8
141 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
142 // PATH: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f16:!.*]]
143   A->f32 = 1;
144   S->f16 = 4;
145   return A->f32;
146 }
147 
g9(StructS * S,StructS2 * S2,uint64_t count)148 uint32_t g9(StructS *S, StructS2 *S2, uint64_t count) {
149 // CHECK-LABEL: define{{.*}} i32 @_Z2g9
150 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
151 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
152 // PATH-LABEL: define{{.*}} i32 @_Z2g9
153 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
154 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_S2_f32:!.*]]
155   S->f32 = 1;
156   S2->f32 = 4;
157   return S->f32;
158 }
159 
g10(StructS * S,StructS2 * S2,uint64_t count)160 uint32_t g10(StructS *S, StructS2 *S2, uint64_t count) {
161 // CHECK-LABEL: define{{.*}} i32 @_Z3g10
162 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
163 // CHECK: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i16]]
164 // PATH-LABEL: define{{.*}} i32 @_Z3g10
165 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
166 // PATH: store i16 4, ptr %{{.*}}, align 4, !tbaa [[TAG_S2_f16:!.*]]
167   S->f32 = 1;
168   S2->f16 = 4;
169   return S->f32;
170 }
171 
g11(StructC * C,StructD * D,uint64_t count)172 uint32_t g11(StructC *C, StructD *D, uint64_t count) {
173 // CHECK-LABEL: define{{.*}} i32 @_Z3g11
174 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
175 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
176 // PATH-LABEL: define{{.*}} i32 @_Z3g11
177 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_C_b_a_f32:!.*]]
178 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_D_b_a_f32:!.*]]
179   C->b.a.f32 = 1;
180   D->b.a.f32 = 4;
181   return C->b.a.f32;
182 }
183 
g12(StructC * C,StructD * D,uint64_t count)184 uint32_t g12(StructC *C, StructD *D, uint64_t count) {
185 // CHECK-LABEL: define{{.*}} i32 @_Z3g12
186 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
187 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
188 // TODO: differentiate the two accesses.
189 // PATH-LABEL: define{{.*}} i32 @_Z3g12
190 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
191 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
192   StructB *b1 = &(C->b);
193   StructB *b2 = &(D->b);
194   // b1, b2 have different context.
195   b1->a.f32 = 1;
196   b2->a.f32 = 4;
197   return b1->a.f32;
198 }
199 
200 // Make sure that zero-length bitfield works.
201 #define ATTR __attribute__ ((ms_struct))
202 struct five {
203   char a;
204   int :0;        /* ignored; prior field is not a bitfield. */
205   char b;
206   char c;
207 } ATTR;
g13(struct five * a,struct five * b)208 char g13(struct five *a, struct five *b) {
209   return a->b;
210 // CHECK-LABEL: define{{.*}} signext i8 @_Z3g13
211 // CHECK: load i8, ptr %{{.*}}, align 1, !tbaa [[TAG_char:!.*]]
212 // PATH-LABEL: define{{.*}} signext i8 @_Z3g13
213 // PATH: load i8, ptr %{{.*}}, align 1, !tbaa [[TAG_five_b:!.*]]
214 }
215 
216 struct six {
217   char a;
218   int :0;
219   char b;
220   char c;
221 };
g14(struct six * a,struct six * b)222 char g14(struct six *a, struct six *b) {
223 // CHECK-LABEL: define{{.*}} signext i8 @_Z3g14
224 // CHECK: load i8, ptr %{{.*}}, align 1, !tbaa [[TAG_char]]
225 // PATH-LABEL: define{{.*}} signext i8 @_Z3g14
226 // PATH: load i8, ptr %{{.*}}, align 1, !tbaa [[TAG_six_b:!.*]]
227   return a->b;
228 }
229 
230 // Types that differ only by name may alias.
231 typedef StructS StructS3;
g15(StructS * S,StructS3 * S3,uint64_t count)232 uint32_t g15(StructS *S, StructS3 *S3, uint64_t count) {
233 // CHECK-LABEL: define{{.*}} i32 @_Z3g15
234 // CHECK: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
235 // CHECK: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_i32]]
236 // PATH-LABEL: define{{.*}} i32 @_Z3g15
237 // PATH: store i32 1, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
238 // PATH: store i32 4, ptr %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
239   S->f32 = 1;
240   S3->f32 = 4;
241   return S->f32;
242 }
243 
244 // CHECK: [[TYPE_char:!.*]] = !{!"omnipotent char", [[TAG_cxx_tbaa:!.*]],
245 // CHECK: [[TAG_cxx_tbaa]] = !{!"Simple C++ TBAA"}
246 // CHECK: [[TAG_i32]] = !{[[TYPE_i32:!.*]], [[TYPE_i32]], i64 0}
247 // CHECK: [[TYPE_i32]] = !{!"int", [[TYPE_char]],
248 // CHECK: [[TAG_i16]] = !{[[TYPE_i16:!.*]], [[TYPE_i16]], i64 0}
249 // CHECK: [[TYPE_i16]] = !{!"short", [[TYPE_char]],
250 // CHECK: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0}
251 
252 // OLD-PATH: [[TYPE_CHAR:!.*]] = !{!"omnipotent char", !
253 // OLD-PATH: [[TAG_i32]] = !{[[TYPE_INT:!.*]], [[TYPE_INT]], i64 0}
254 // OLD-PATH: [[TYPE_INT]] = !{!"int", [[TYPE_CHAR]]
255 // OLD-PATH: [[TAG_A_f32]] = !{[[TYPE_A:!.*]], [[TYPE_INT]], i64 4}
256 // OLD-PATH: [[TYPE_A]] = !{!"_ZTS7StructA", [[TYPE_SHORT:!.*]], i64 0, [[TYPE_INT]], i64 4, [[TYPE_SHORT]], i64 8, [[TYPE_INT]], i64 12}
257 // OLD-PATH: [[TYPE_SHORT:!.*]] = !{!"short", [[TYPE_CHAR]]
258 // OLD-PATH: [[TAG_A_f16]] = !{[[TYPE_A]], [[TYPE_SHORT]], i64 0}
259 // OLD-PATH: [[TAG_B_a_f32]] = !{[[TYPE_B:!.*]], [[TYPE_INT]], i64 8}
260 // OLD-PATH: [[TYPE_B]] = !{!"_ZTS7StructB", [[TYPE_SHORT]], i64 0, [[TYPE_A]], i64 4, [[TYPE_INT]], i64 20}
261 // OLD-PATH: [[TAG_B_a_f16]] = !{[[TYPE_B]], [[TYPE_SHORT]], i64 4}
262 // OLD-PATH: [[TAG_B_f32]] = !{[[TYPE_B]], [[TYPE_INT]], i64 20}
263 // OLD-PATH: [[TAG_B_a_f32_2]] = !{[[TYPE_B]], [[TYPE_INT]], i64 16}
264 // OLD-PATH: [[TAG_S_f32]] = !{[[TYPE_S:!.*]], [[TYPE_INT]], i64 4}
265 // OLD-PATH: [[TYPE_S]] = !{!"_ZTS7StructS", [[TYPE_SHORT]], i64 0, [[TYPE_INT]], i64 4}
266 // OLD-PATH: [[TAG_S_f16]] = !{[[TYPE_S]], [[TYPE_SHORT]], i64 0}
267 // OLD-PATH: [[TAG_S2_f32]] = !{[[TYPE_S2:!.*]], [[TYPE_INT]], i64 4}
268 // OLD-PATH: [[TYPE_S2]] = !{!"_ZTS8StructS2", [[TYPE_SHORT]], i64 0, [[TYPE_INT]], i64 4}
269 // OLD-PATH: [[TAG_S2_f16]] = !{[[TYPE_S2]], [[TYPE_SHORT]], i64 0}
270 // OLD-PATH: [[TAG_C_b_a_f32]] = !{[[TYPE_C:!.*]], [[TYPE_INT]], i64 12}
271 // OLD-PATH: [[TYPE_C]] = !{!"_ZTS7StructC", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28}
272 // OLD-PATH: [[TAG_D_b_a_f32]] = !{[[TYPE_D:!.*]], [[TYPE_INT]], i64 12}
273 // OLD-PATH: [[TYPE_D]] = !{!"_ZTS7StructD", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28, [[TYPE_CHAR]], i64 32}
274 // OLD-PATH: [[TAG_five_b]] = !{[[TYPE_five:!.*]], [[TYPE_CHAR]], i64 1}
275 // OLD-PATH: [[TYPE_five]] = !{!"_ZTS4five", [[TYPE_CHAR]], i64 0, [[TYPE_CHAR]], i64 1, [[TYPE_CHAR]], i64 2}
276 // OLD-PATH: [[TAG_six_b]] = !{[[TYPE_six:!.*]], [[TYPE_CHAR]], i64 4}
277 // OLD-PATH: [[TYPE_six]] = !{!"_ZTS3six", [[TYPE_CHAR]], i64 0, [[TYPE_CHAR]], i64 4, [[TYPE_CHAR]], i64 5}
278 
279 // NEW-PATH-DAG: [[ROOT:!.*]] = !{!"Simple C++ TBAA"}
280 // NEW-PATH-DAG: [[TYPE_char:!.*]] = !{[[ROOT]], i64 1, !"omnipotent char"}
281 // NEW-PATH-DAG: [[TYPE_short:!.*]] = !{[[TYPE_char]], i64 2, !"short"}
282 // NEW-PATH-DAG: [[TYPE_int:!.*]] = !{[[TYPE_char]], i64 4, !"int"}
283 // NEW-PATH-DAG: [[TAG_i32:!.*]] = !{[[TYPE_int]], [[TYPE_int]], i64 0, i64 4}
284 // NEW-PATH-DAG: [[TYPE_A:!.*]] = !{[[TYPE_char]], i64 16, !"_ZTS7StructA", [[TYPE_short]], i64 0, i64 2, [[TYPE_int]], i64 4, i64 4, [[TYPE_short]], i64 8, i64 2, [[TYPE_int]], i64 12, i64 4}
285 // NEW-PATH-DAG: [[TAG_A_f16]] = !{[[TYPE_A]], [[TYPE_short]], i64 0, i64 2}
286 // NEW-PATH-DAG: [[TAG_A_f32]] = !{[[TYPE_A]], [[TYPE_int]], i64 4, i64 4}
287 // NEW-PATH-DAG: [[TYPE_B:!.*]] = !{[[TYPE_char]], i64 24, !"_ZTS7StructB", [[TYPE_short]], i64 0, i64 2, [[TYPE_A]], i64 4, i64 16, [[TYPE_int]], i64 20, i64 4}
288 // NEW-PATH-DAG: [[TAG_B_a_f16]] = !{[[TYPE_B]], [[TYPE_short]], i64 4, i64 2}
289 // NEW-PATH-DAG: [[TAG_B_a_f32]] = !{[[TYPE_B]], [[TYPE_int]], i64 8, i64 4}
290 // NEW-PATH-DAG: [[TAG_B_f32]] = !{[[TYPE_B]], [[TYPE_int]], i64 20, i64 4}
291 // NEW-PATH-DAG: [[TAG_B_a_f32_2]] = !{[[TYPE_B]], [[TYPE_int]], i64 16, i64 4}
292 // NEW-PATH-DAG: [[TYPE_S:!.*]] = !{[[TYPE_char]], i64 8, !"_ZTS7StructS", [[TYPE_short]], i64 0, i64 2, [[TYPE_int]], i64 4, i64 4}
293 // NEW-PATH-DAG: [[TAG_S_f16]] = !{[[TYPE_S]], [[TYPE_short]], i64 0, i64 2}
294 // NEW-PATH-DAG: [[TAG_S_f32]] = !{[[TYPE_S]], [[TYPE_int]], i64 4, i64 4}
295 // NEW-PATH-DAG: [[TYPE_S2:!.*]] = !{[[TYPE_char]], i64 8, !"_ZTS8StructS2", [[TYPE_short]], i64 0, i64 2, [[TYPE_int]], i64 4, i64 4}
296 // NEW-PATH-DAG: [[TAG_S2_f16]] = !{[[TYPE_S2]], [[TYPE_short]], i64 0, i64 2}
297 // NEW-PATH-DAG: [[TAG_S2_f32]] = !{[[TYPE_S2]], [[TYPE_int]], i64 4, i64 4}
298 // NEW-PATH-DAG: [[TYPE_C:!.*]] = !{[[TYPE_char]], i64 32, !"_ZTS7StructC", [[TYPE_short]], i64 0, i64 2, [[TYPE_B]], i64 4, i64 24, [[TYPE_int]], i64 28, i64 4}
299 // NEW-PATH-DAG: [[TAG_C_b_a_f32]] = !{[[TYPE_C]], [[TYPE_int]], i64 12, i64 4}
300 // NEW-PATH-DAG: [[TYPE_D:!.*]] = !{[[TYPE_char]], i64 36, !"_ZTS7StructD", [[TYPE_short]], i64 0, i64 2, [[TYPE_B]], i64 4, i64 24, [[TYPE_int]], i64 28, i64 4, [[TYPE_char]], i64 32, i64 1}
301 // NEW-PATH-DAG: [[TAG_D_b_a_f32]] = !{[[TYPE_D]], [[TYPE_int]], i64 12, i64 4}
302 // NEW-PATH-DAG: [[TYPE_five:!.*]] = !{[[TYPE_char]], i64 3, !"_ZTS4five", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, i64 1, [[TYPE_char]], i64 2, i64 1}
303 // NEW-PATH-DAG: [[TAG_five_b]] = !{[[TYPE_five]], [[TYPE_char]], i64 1, i64 1}
304 // NEW-PATH-DAG: [[TYPE_six:!.*]] = !{[[TYPE_char]], i64 6, !"_ZTS3six", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 4, i64 1, [[TYPE_char]], i64 5, i64 1}
305 // NEW-PATH-DAG: [[TAG_six_b]] = !{[[TYPE_six]], [[TYPE_char]], i64 4, i64 1}
306