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