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 class StructA
10 {
11 public:
12 uint16_t f16;
13 uint32_t f32;
14 uint16_t f16_2;
15 uint32_t f32_2;
16 };
17 class StructB
18 {
19 public:
20 uint16_t f16;
21 StructA a;
22 uint32_t f32;
23 };
24 class StructC
25 {
26 public:
27 uint16_t f16;
28 StructB b;
29 uint32_t f32;
30 };
31 class StructD
32 {
33 public:
34 uint16_t f16;
35 StructB b;
36 uint32_t f32;
37 uint8_t f8;
38 };
39
40 class StructS
41 {
42 public:
43 uint16_t f16;
44 uint32_t f32;
45 };
46 class StructS2 : public StructS
47 {
48 public:
49 uint16_t f16_2;
50 uint32_t f32_2;
51 };
52
g(uint32_t * s,StructA * A,uint64_t count)53 uint32_t g(uint32_t *s, StructA *A, uint64_t count) {
54 // CHECK-LABEL: define i32 @_Z1g
55 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
56 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
57 // PATH-LABEL: define i32 @_Z1g
58 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
59 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32:!.*]]
60 *s = 1;
61 A->f32 = 4;
62 return *s;
63 }
64
g2(uint32_t * s,StructA * A,uint64_t count)65 uint32_t g2(uint32_t *s, StructA *A, uint64_t count) {
66 // CHECK-LABEL: define i32 @_Z2g2
67 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
68 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16:!.*]]
69 // PATH-LABEL: define i32 @_Z2g2
70 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
71 // PATH: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_A_f16:!.*]]
72 *s = 1;
73 A->f16 = 4;
74 return *s;
75 }
76
g3(StructA * A,StructB * B,uint64_t count)77 uint32_t g3(StructA *A, StructB *B, uint64_t count) {
78 // CHECK-LABEL: define i32 @_Z2g3
79 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
80 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
81 // PATH-LABEL: define i32 @_Z2g3
82 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
83 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32:!.*]]
84 A->f32 = 1;
85 B->a.f32 = 4;
86 return A->f32;
87 }
88
g4(StructA * A,StructB * B,uint64_t count)89 uint32_t g4(StructA *A, StructB *B, uint64_t count) {
90 // CHECK-LABEL: define i32 @_Z2g4
91 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
92 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16]]
93 // PATH-LABEL: define i32 @_Z2g4
94 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
95 // PATH: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_B_a_f16:!.*]]
96 A->f32 = 1;
97 B->a.f16 = 4;
98 return A->f32;
99 }
100
g5(StructA * A,StructB * B,uint64_t count)101 uint32_t g5(StructA *A, StructB *B, uint64_t count) {
102 // CHECK-LABEL: define i32 @_Z2g5
103 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
104 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
105 // PATH-LABEL: define i32 @_Z2g5
106 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
107 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_f32:!.*]]
108 A->f32 = 1;
109 B->f32 = 4;
110 return A->f32;
111 }
112
g6(StructA * A,StructB * B,uint64_t count)113 uint32_t g6(StructA *A, StructB *B, uint64_t count) {
114 // CHECK-LABEL: define i32 @_Z2g6
115 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
116 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
117 // PATH-LABEL: define i32 @_Z2g6
118 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
119 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32_2:!.*]]
120 A->f32 = 1;
121 B->a.f32_2 = 4;
122 return A->f32;
123 }
124
g7(StructA * A,StructS * S,uint64_t count)125 uint32_t g7(StructA *A, StructS *S, uint64_t count) {
126 // CHECK-LABEL: define i32 @_Z2g7
127 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
128 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
129 // PATH-LABEL: define i32 @_Z2g7
130 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
131 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32:!.*]]
132 A->f32 = 1;
133 S->f32 = 4;
134 return A->f32;
135 }
136
g8(StructA * A,StructS * S,uint64_t count)137 uint32_t g8(StructA *A, StructS *S, uint64_t count) {
138 // CHECK-LABEL: define i32 @_Z2g8
139 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
140 // CHECK: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_i16]]
141 // PATH-LABEL: define i32 @_Z2g8
142 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
143 // PATH: store i16 4, i16* %{{.*}}, align 4, !tbaa [[TAG_S_f16:!.*]]
144 A->f32 = 1;
145 S->f16 = 4;
146 return A->f32;
147 }
148
g9(StructS * S,StructS2 * S2,uint64_t count)149 uint32_t g9(StructS *S, StructS2 *S2, uint64_t count) {
150 // CHECK-LABEL: define i32 @_Z2g9
151 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
152 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
153 // PATH-LABEL: define i32 @_Z2g9
154 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
155 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32:!.*]]
156 S->f32 = 1;
157 S2->f32 = 4;
158 return S->f32;
159 }
160
g10(StructS * S,StructS2 * S2,uint64_t count)161 uint32_t g10(StructS *S, StructS2 *S2, uint64_t count) {
162 // CHECK-LABEL: define i32 @_Z3g10
163 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
164 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
165 // PATH-LABEL: define i32 @_Z3g10
166 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
167 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S2_f32_2:!.*]]
168 S->f32 = 1;
169 S2->f32_2 = 4;
170 return S->f32;
171 }
172
g11(StructC * C,StructD * D,uint64_t count)173 uint32_t g11(StructC *C, StructD *D, uint64_t count) {
174 // CHECK-LABEL: define i32 @_Z3g11
175 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
176 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
177 // PATH-LABEL: define i32 @_Z3g11
178 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_C_b_a_f32:!.*]]
179 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_D_b_a_f32:!.*]]
180 C->b.a.f32 = 1;
181 D->b.a.f32 = 4;
182 return C->b.a.f32;
183 }
184
g12(StructC * C,StructD * D,uint64_t count)185 uint32_t g12(StructC *C, StructD *D, uint64_t count) {
186 // CHECK-LABEL: define i32 @_Z3g12
187 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
188 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
189 // TODO: differentiate the two accesses.
190 // PATH-LABEL: define i32 @_Z3g12
191 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
192 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
193 StructB *b1 = &(C->b);
194 StructB *b2 = &(D->b);
195 // b1, b2 have different context.
196 b1->a.f32 = 1;
197 b2->a.f32 = 4;
198 return b1->a.f32;
199 }
200
201 // CHECK: [[TYPE_char:!.*]] = !{!"omnipotent char", [[TAG_cxx_tbaa:!.*]],
202 // CHECK: [[TAG_cxx_tbaa]] = !{!"Simple C++ TBAA"}
203 // CHECK: [[TAG_i32]] = !{[[TYPE_i32:!.*]], [[TYPE_i32]], i64 0}
204 // CHECK: [[TYPE_i32]] = !{!"int", [[TYPE_char]],
205 // CHECK: [[TAG_i16]] = !{[[TYPE_i16:!.*]], [[TYPE_i16]], i64 0}
206 // CHECK: [[TYPE_i16]] = !{!"short", [[TYPE_char]],
207
208 // PATH: [[TYPE_CHAR:!.*]] = !{!"omnipotent char", !
209 // PATH: [[TAG_i32]] = !{[[TYPE_INT:!.*]], [[TYPE_INT]], i64 0}
210 // PATH: [[TYPE_INT]] = !{!"int", [[TYPE_CHAR]]
211 // PATH: [[TAG_A_f32]] = !{[[TYPE_A:!.*]], [[TYPE_INT]], i64 4}
212 // PATH: [[TYPE_A]] = !{!"_ZTS7StructA", [[TYPE_SHORT:!.*]], i64 0, [[TYPE_INT]], i64 4, [[TYPE_SHORT]], i64 8, [[TYPE_INT]], i64 12}
213 // PATH: [[TYPE_SHORT:!.*]] = !{!"short", [[TYPE_CHAR]]
214 // PATH: [[TAG_A_f16]] = !{[[TYPE_A]], [[TYPE_SHORT]], i64 0}
215 // PATH: [[TAG_B_a_f32]] = !{[[TYPE_B:!.*]], [[TYPE_INT]], i64 8}
216 // PATH: [[TYPE_B]] = !{!"_ZTS7StructB", [[TYPE_SHORT]], i64 0, [[TYPE_A]], i64 4, [[TYPE_INT]], i64 20}
217 // PATH: [[TAG_B_a_f16]] = !{[[TYPE_B]], [[TYPE_SHORT]], i64 4}
218 // PATH: [[TAG_B_f32]] = !{[[TYPE_B]], [[TYPE_INT]], i64 20}
219 // PATH: [[TAG_B_a_f32_2]] = !{[[TYPE_B]], [[TYPE_INT]], i64 16}
220 // PATH: [[TAG_S_f32]] = !{[[TYPE_S:!.*]], [[TYPE_INT]], i64 4}
221 // PATH: [[TYPE_S]] = !{!"_ZTS7StructS", [[TYPE_SHORT]], i64 0, [[TYPE_INT]], i64 4}
222 // PATH: [[TAG_S_f16]] = !{[[TYPE_S]], [[TYPE_SHORT]], i64 0}
223 // PATH: [[TAG_S2_f32_2]] = !{[[TYPE_S2:!.*]], [[TYPE_INT]], i64 12}
224 // PATH: [[TYPE_S2]] = !{!"_ZTS8StructS2", [[TYPE_SHORT]], i64 8, [[TYPE_INT]], i64 12}
225 // PATH: [[TAG_C_b_a_f32]] = !{[[TYPE_C:!.*]], [[TYPE_INT]], i64 12}
226 // PATH: [[TYPE_C]] = !{!"_ZTS7StructC", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28}
227 // PATH: [[TAG_D_b_a_f32]] = !{[[TYPE_D:!.*]], [[TYPE_INT]], i64 12}
228 // PATH: [[TYPE_D]] = !{!"_ZTS7StructD", [[TYPE_SHORT]], i64 0, [[TYPE_B]], i64 4, [[TYPE_INT]], i64 28, [[TYPE_CHAR]], i64 32}
229