• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-LABEL: define i32 @_Z1g
49 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
50 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
51 // PATH-LABEL: define i32 @_Z1g
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-LABEL: define i32 @_Z2g2
61 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
62 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16:!.*]]
63 // PATH-LABEL: define i32 @_Z2g2
64 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
65 // PATH: store i16 4, i16* %{{.*}}, align 4, !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-LABEL: define i32 @_Z2g3
73 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
74 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
75 // PATH-LABEL: define i32 @_Z2g3
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-LABEL: define i32 @_Z2g4
85 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
86 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16]]
87 // PATH-LABEL: define i32 @_Z2g4
88 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
89 // PATH: store i16 4, i16* %{{.*}}, align 4, !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-LABEL: define i32 @_Z2g5
97 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
98 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
99 // PATH-LABEL: define i32 @_Z2g5
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-LABEL: define i32 @_Z2g6
109 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
110 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
111 // PATH-LABEL: define i32 @_Z2g6
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-LABEL: define i32 @_Z2g7
121 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
122 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
123 // PATH-LABEL: define i32 @_Z2g7
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-LABEL: define i32 @_Z2g8
133 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
134 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16]]
135 // PATH-LABEL: define i32 @_Z2g8
136 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
137 // PATH: store i16 4, i16* %{{.*}}, align 4, !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-LABEL: define i32 @_Z2g9
145 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
146 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
147 // PATH-LABEL: define i32 @_Z2g9
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-LABEL: define i32 @_Z3g10
157 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
158 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16]]
159 // PATH-LABEL: define i32 @_Z3g10
160 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
161 // PATH: store i16 4, i16* %{{.*}}, align 4, !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-LABEL: define i32 @_Z3g11
169 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
170 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
171 // PATH-LABEL: define i32 @_Z3g11
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-LABEL: define i32 @_Z3g12
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-LABEL: define i32 @_Z3g12
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-LABEL: define signext i8 @_Z3g13
206 // CHECK: load i8, i8* %{{.*}}, align 1, !tbaa [[TAG_char:!.*]]
207 // PATH-LABEL: define signext i8 @_Z3g13
208 // PATH: load i8, 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-LABEL: define signext i8 @_Z3g14
219 // CHECK: load i8, i8* %{{.*}}, align 1, !tbaa [[TAG_char]]
220 // PATH-LABEL: define signext i8 @_Z3g14
221 // PATH: load i8, 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-LABEL: define i32 @_Z3g15
229 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
230 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
231 // PATH-LABEL: define i32 @_Z3g15
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:!.*]] = !{!"omnipotent char", [[TAG_cxx_tbaa:!.*]],
240 // CHECK: [[TAG_cxx_tbaa]] = !{!"Simple C/C++ TBAA"}
241 // CHECK: [[TAG_i32]] = !{[[TYPE_i32:!.*]], [[TYPE_i32]], i64 0}
242 // CHECK: [[TYPE_i32]] = !{!"int", [[TYPE_char]],
243 // CHECK: [[TAG_i16]] = !{[[TYPE_i16:!.*]], [[TYPE_i16]], i64 0}
244 // CHECK: [[TYPE_i16]] = !{!"short", [[TYPE_char]],
245 // CHECK: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0}
246 
247 // PATH: [[TYPE_CHAR:!.*]] = !{!"omnipotent char", !
248 // PATH: [[TAG_i32]] = !{[[TYPE_INT:!.*]], [[TYPE_INT]], i64 0}
249 // PATH: [[TYPE_INT]] = !{!"int", [[TYPE_CHAR]]
250 // PATH: [[TAG_A_f32]] = !{[[TYPE_A:!.*]], [[TYPE_INT]], i64 4}
251 // PATH: [[TYPE_A]] = !{!"_ZTS7StructA", [[TYPE_SHORT:!.*]], i64 0, [[TYPE_INT]], i64 4, [[TYPE_SHORT]], i64 8, [[TYPE_INT]], i64 12}
252 // PATH: [[TYPE_SHORT:!.*]] = !{!"short", [[TYPE_CHAR]]
253 // PATH: [[TAG_A_f16]] = !{[[TYPE_A]], [[TYPE_SHORT]], i64 0}
254 // PATH: [[TAG_B_a_f32]] = !{[[TYPE_B:!.*]], [[TYPE_INT]], i64 8}
255 // PATH: [[TYPE_B]] = !{!"_ZTS7StructB", [[TYPE_SHORT]], i64 0, [[TYPE_A]], i64 4, [[TYPE_INT]], i64 20}
256 // PATH: [[TAG_B_a_f16]] = !{[[TYPE_B]], [[TYPE_SHORT]], i64 4}
257 // PATH: [[TAG_B_f32]] = !{[[TYPE_B]], [[TYPE_INT]], i64 20}
258 // PATH: [[TAG_B_a_f32_2]] = !{[[TYPE_B]], [[TYPE_INT]], i64 16}
259 // PATH: [[TAG_S_f32]] = !{[[TYPE_S:!.*]], [[TYPE_INT]], i64 4}
260 // PATH: [[TYPE_S]] = !{!"_ZTS7StructS", [[TYPE_SHORT]], i64 0, [[TYPE_INT]], i64 4}
261 // PATH: [[TAG_S_f16]] = !{[[TYPE_S]], [[TYPE_SHORT]], i64 0}
262 // PATH: [[TAG_S2_f32]] = !{[[TYPE_S2:!.*]], [[TYPE_INT]], i64 4}
263 // PATH: [[TYPE_S2]] = !{!"_ZTS8StructS2", [[TYPE_SHORT]], i64 0, [[TYPE_INT]], i64 4}
264 // PATH: [[TAG_S2_f16]] = !{[[TYPE_S2]], [[TYPE_SHORT]], i64 0}
265 // PATH: [[TAG_C_b_a_f32]] = !{[[TYPE_C:!.*]], [[TYPE_INT]], i64 12}
266 // PATH: [[TYPE_C]] = !{!"_ZTS7StructC", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28}
267 // PATH: [[TAG_D_b_a_f32]] = !{[[TYPE_D:!.*]], [[TYPE_INT]], i64 12}
268 // PATH: [[TYPE_D]] = !{!"_ZTS7StructD", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28, [[TYPE_CHAR]], i64 32}
269 // PATH: [[TAG_five_b]] = !{[[TYPE_five:!.*]], [[TYPE_CHAR]], i64 1}
270 // PATH: [[TYPE_five]] = !{!"_ZTS4five", [[TYPE_CHAR]], i64 0, [[TYPE_INT]], i64 1, [[TYPE_CHAR]], i64 1, [[TYPE_CHAR]], i64 2}
271 // PATH: [[TAG_six_b]] = !{[[TYPE_six:!.*]], [[TYPE_CHAR]], i64 4}
272 // PATH: [[TYPE_six]] = !{!"_ZTS3six", [[TYPE_CHAR]], i64 0, [[TYPE_INT]], i64 4, [[TYPE_CHAR]], i64 4, [[TYPE_CHAR]], i64 5}
273