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