1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #include "CodeGenTBAA.h" |
18 | #include "clang/AST/ASTContext.h" |
19 | #include "clang/AST/Attr.h" |
20 | #include "clang/AST/Mangle.h" |
21 | #include "clang/AST/RecordLayout.h" |
22 | #include "clang/Basic/CodeGenOptions.h" |
23 | #include "llvm/ADT/SmallSet.h" |
24 | #include "llvm/IR/Constants.h" |
25 | #include "llvm/IR/LLVMContext.h" |
26 | #include "llvm/IR/Metadata.h" |
27 | #include "llvm/IR/Module.h" |
28 | #include "llvm/IR/Type.h" |
29 | using namespace clang; |
30 | using namespace CodeGen; |
31 | |
32 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, |
33 | const CodeGenOptions &CGO, |
34 | const LangOptions &Features, MangleContext &MContext) |
35 | : Context(Ctx), Module(M), CodeGenOpts(CGO), |
36 | Features(Features), MContext(MContext), MDHelper(M.getContext()), |
37 | Root(nullptr), Char(nullptr) |
38 | {} |
39 | |
40 | CodeGenTBAA::~CodeGenTBAA() { |
41 | } |
42 | |
43 | llvm::MDNode *CodeGenTBAA::getRoot() { |
44 | |
45 | |
46 | |
47 | |
48 | if (!Root) { |
49 | if (Features.CPlusPlus) |
50 | Root = MDHelper.createTBAARoot("Simple C++ TBAA"); |
51 | else |
52 | Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); |
53 | } |
54 | |
55 | return Root; |
56 | } |
57 | |
58 | llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name, |
59 | llvm::MDNode *Parent, |
60 | uint64_t Size) { |
61 | if (CodeGenOpts.NewStructPathTBAA) { |
62 | llvm::Metadata *Id = MDHelper.createString(Name); |
63 | return MDHelper.createTBAATypeNode(Parent, Size, Id); |
64 | } |
65 | return MDHelper.createTBAAScalarTypeNode(Name, Parent); |
66 | } |
67 | |
68 | llvm::MDNode *CodeGenTBAA::getChar() { |
69 | |
70 | |
71 | |
72 | |
73 | if (!Char) |
74 | Char = createScalarTypeNode("omnipotent char", getRoot(), 1); |
75 | |
76 | return Char; |
77 | } |
78 | |
79 | static bool TypeHasMayAlias(QualType QTy) { |
80 | |
81 | if (const TagType *TTy = dyn_cast<TagType>(QTy)) |
82 | return TTy->getDecl()->hasAttr<MayAliasAttr>(); |
83 | |
84 | |
85 | if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { |
86 | if (TTy->getDecl()->hasAttr<MayAliasAttr>()) |
87 | return true; |
88 | |
89 | return TypeHasMayAlias(TTy->desugar()); |
90 | } |
91 | |
92 | return false; |
93 | } |
94 | |
95 | |
96 | static bool isValidBaseType(QualType QTy) { |
97 | if (QTy->isReferenceType()) |
98 | return false; |
99 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
100 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
101 | |
102 | if (!RD) |
103 | return false; |
104 | if (RD->hasFlexibleArrayMember()) |
105 | return false; |
106 | |
107 | |
108 | if (RD->isStruct() || RD->isClass()) |
109 | return true; |
110 | } |
111 | return false; |
112 | } |
113 | |
114 | llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) { |
115 | uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity(); |
116 | |
117 | |
118 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
119 | switch (BTy->getKind()) { |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | case BuiltinType::Char_U: |
126 | case BuiltinType::Char_S: |
127 | case BuiltinType::UChar: |
128 | case BuiltinType::SChar: |
129 | return getChar(); |
130 | |
131 | |
132 | case BuiltinType::UShort: |
133 | return getTypeInfo(Context.ShortTy); |
134 | case BuiltinType::UInt: |
135 | return getTypeInfo(Context.IntTy); |
136 | case BuiltinType::ULong: |
137 | return getTypeInfo(Context.LongTy); |
138 | case BuiltinType::ULongLong: |
139 | return getTypeInfo(Context.LongLongTy); |
140 | case BuiltinType::UInt128: |
141 | return getTypeInfo(Context.Int128Ty); |
142 | |
143 | |
144 | |
145 | |
146 | default: |
147 | return createScalarTypeNode(BTy->getName(Features), getChar(), Size); |
148 | } |
149 | } |
150 | |
151 | |
152 | |
153 | |
154 | if (Ty->isStdByteType()) |
155 | return getChar(); |
156 | |
157 | |
158 | |
159 | |
160 | if (Ty->isPointerType() || Ty->isReferenceType()) |
161 | return createScalarTypeNode("any pointer", getChar(), Size); |
162 | |
163 | |
164 | if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType()) |
165 | return getTypeInfo(cast<ArrayType>(Ty)->getElementType()); |
166 | |
167 | |
168 | |
169 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
170 | |
171 | |
172 | |
173 | |
174 | if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible()) |
175 | return getChar(); |
176 | |
177 | SmallString<256> OutName; |
178 | llvm::raw_svector_ostream Out(OutName); |
179 | MContext.mangleTypeName(QualType(ETy, 0), Out); |
180 | return createScalarTypeNode(OutName, getChar(), Size); |
181 | } |
182 | |
183 | |
184 | return getChar(); |
185 | } |
186 | |
187 | llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) { |
188 | |
189 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
190 | return nullptr; |
191 | |
192 | |
193 | |
194 | if (TypeHasMayAlias(QTy)) |
195 | return getChar(); |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | if (isValidBaseType(QTy)) |
204 | return getBaseTypeInfo(QTy); |
205 | |
206 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
207 | if (llvm::MDNode *N = MetadataCache[Ty]) |
208 | return N; |
209 | |
210 | |
211 | |
212 | |
213 | llvm::MDNode *TypeNode = getTypeInfoHelper(Ty); |
214 | return MetadataCache[Ty] = TypeNode; |
215 | } |
216 | |
217 | TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) { |
218 | |
219 | |
220 | if (AccessType->isIncompleteType()) |
221 | return TBAAAccessInfo::getIncompleteInfo(); |
222 | |
223 | if (TypeHasMayAlias(AccessType)) |
224 | return TBAAAccessInfo::getMayAliasInfo(); |
225 | |
226 | uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity(); |
227 | return TBAAAccessInfo(getTypeInfo(AccessType), Size); |
228 | } |
229 | |
230 | TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
231 | llvm::DataLayout DL(&Module); |
232 | unsigned Size = DL.getPointerTypeSize(VTablePtrType); |
233 | return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size), |
234 | Size); |
235 | } |
236 | |
237 | bool |
238 | CodeGenTBAA::CollectFields(uint64_t BaseOffset, |
239 | QualType QTy, |
240 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & |
241 | Fields, |
242 | bool MayAlias) { |
243 | |
244 | |
245 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
246 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
247 | if (RD->hasFlexibleArrayMember()) |
248 | return false; |
249 | |
250 | |
251 | if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) |
252 | if (Decl->bases_begin() != Decl->bases_end()) |
253 | return false; |
254 | |
255 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
256 | |
257 | unsigned idx = 0; |
258 | for (RecordDecl::field_iterator i = RD->field_begin(), |
259 | e = RD->field_end(); i != e; ++i, ++idx) { |
260 | uint64_t Offset = BaseOffset + |
261 | Layout.getFieldOffset(idx) / Context.getCharWidth(); |
262 | QualType FieldQTy = i->getType(); |
263 | if (!CollectFields(Offset, FieldQTy, Fields, |
264 | MayAlias || TypeHasMayAlias(FieldQTy))) |
265 | return false; |
266 | } |
267 | return true; |
268 | } |
269 | |
270 | |
271 | uint64_t Offset = BaseOffset; |
272 | uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); |
273 | llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy); |
274 | llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size)); |
275 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); |
276 | return true; |
277 | } |
278 | |
279 | llvm::MDNode * |
280 | CodeGenTBAA::getTBAAStructInfo(QualType QTy) { |
281 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
282 | |
283 | if (llvm::MDNode *N = StructMetadataCache[Ty]) |
284 | return N; |
285 | |
286 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
287 | if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) |
288 | return MDHelper.createTBAAStructNode(Fields); |
289 | |
290 | |
291 | return StructMetadataCache[Ty] = nullptr; |
292 | } |
293 | |
294 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) { |
295 | if (auto *TTy = dyn_cast<RecordType>(Ty)) { |
296 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
297 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
298 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
299 | for (FieldDecl *Field : RD->fields()) { |
300 | QualType FieldQTy = Field->getType(); |
301 | llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ? |
302 | getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy); |
303 | if (!TypeNode) |
304 | return BaseTypeMetadataCache[Ty] = nullptr; |
305 | |
306 | uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex()); |
307 | uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity(); |
308 | uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity(); |
309 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, |
310 | TypeNode)); |
311 | } |
312 | |
313 | SmallString<256> OutName; |
314 | if (Features.CPlusPlus) { |
315 | |
316 | llvm::raw_svector_ostream Out(OutName); |
317 | MContext.mangleTypeName(QualType(Ty, 0), Out); |
318 | } else { |
319 | OutName = RD->getName(); |
320 | } |
321 | |
322 | if (CodeGenOpts.NewStructPathTBAA) { |
323 | llvm::MDNode *Parent = getChar(); |
324 | uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity(); |
325 | llvm::Metadata *Id = MDHelper.createString(OutName); |
326 | return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields); |
327 | } |
328 | |
329 | |
330 | SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes; |
331 | for (const auto &Field : Fields) |
332 | OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset)); |
333 | return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes); |
334 | } |
335 | |
336 | return nullptr; |
337 | } |
338 | |
339 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { |
340 | if (!isValidBaseType(QTy)) |
341 | return nullptr; |
342 | |
343 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
344 | if (llvm::MDNode *N = BaseTypeMetadataCache[Ty]) |
345 | return N; |
346 | |
347 | |
348 | |
349 | |
350 | llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty); |
351 | return BaseTypeMetadataCache[Ty] = TypeNode; |
352 | } |
353 | |
354 | llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { |
355 | (0) . __assert_fail ("!Info.isIncomplete() && \"Access to an object of an incomplete type!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenTBAA.cpp", 355, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Info.isIncomplete() && "Access to an object of an incomplete type!"); |
356 | |
357 | if (Info.isMayAlias()) |
358 | Info = TBAAAccessInfo(getChar(), Info.Size); |
359 | |
360 | if (!Info.AccessType) |
361 | return nullptr; |
362 | |
363 | if (!CodeGenOpts.StructPathTBAA) |
364 | Info = TBAAAccessInfo(Info.AccessType, Info.Size); |
365 | |
366 | llvm::MDNode *&N = AccessTagMetadataCache[Info]; |
367 | if (N) |
368 | return N; |
369 | |
370 | if (!Info.BaseType) { |
371 | Info.BaseType = Info.AccessType; |
372 | (0) . __assert_fail ("!Info.Offset && \"Nonzero offset for an access with no base type!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenTBAA.cpp", 372, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Info.Offset && "Nonzero offset for an access with no base type!"); |
373 | } |
374 | if (CodeGenOpts.NewStructPathTBAA) { |
375 | return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType, |
376 | Info.Offset, Info.Size); |
377 | } |
378 | return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType, |
379 | Info.Offset); |
380 | } |
381 | |
382 | TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
383 | TBAAAccessInfo TargetInfo) { |
384 | if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias()) |
385 | return TBAAAccessInfo::getMayAliasInfo(); |
386 | return TargetInfo; |
387 | } |
388 | |
389 | TBAAAccessInfo |
390 | CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
391 | TBAAAccessInfo InfoB) { |
392 | if (InfoA == InfoB) |
393 | return InfoA; |
394 | |
395 | if (!InfoA || !InfoB) |
396 | return TBAAAccessInfo(); |
397 | |
398 | if (InfoA.isMayAlias() || InfoB.isMayAlias()) |
399 | return TBAAAccessInfo::getMayAliasInfo(); |
400 | |
401 | |
402 | |
403 | |
404 | return TBAAAccessInfo::getMayAliasInfo(); |
405 | } |
406 | |
407 | TBAAAccessInfo |
408 | CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
409 | TBAAAccessInfo SrcInfo) { |
410 | if (DestInfo == SrcInfo) |
411 | return DestInfo; |
412 | |
413 | if (!DestInfo || !SrcInfo) |
414 | return TBAAAccessInfo(); |
415 | |
416 | if (DestInfo.isMayAlias() || SrcInfo.isMayAlias()) |
417 | return TBAAAccessInfo::getMayAliasInfo(); |
418 | |
419 | |
420 | |
421 | |
422 | return TBAAAccessInfo::getMayAliasInfo(); |
423 | } |
424 | |