24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Metadata.h"
28 #include "llvm/IR/Type.h"
29 using namespace clang;
30 using namespace CodeGen;
35 :
Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
36 MDHelper(VMContext), Root(nullptr), Char(nullptr) {
42 llvm::MDNode *CodeGenTBAA::getRoot() {
48 Root = MDHelper.createTBAARoot(
"Simple C/C++ TBAA");
55 llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef
Name,
56 llvm::MDNode *Parent) {
57 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
60 llvm::MDNode *CodeGenTBAA::getChar() {
66 Char = createTBAAScalarType(
"omnipotent char", getRoot());
73 if (
const TagType *TTy = dyn_cast<TagType>(QTy))
74 return TTy->getDecl()->hasAttr<MayAliasAttr>();
77 if (
const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
78 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
90 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
100 if (llvm::MDNode *N = MetadataCache[Ty])
104 if (
const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
105 switch (BTy->getKind()) {
111 case BuiltinType::Char_U:
112 case BuiltinType::Char_S:
113 case BuiltinType::UChar:
114 case BuiltinType::SChar:
118 case BuiltinType::UShort:
120 case BuiltinType::UInt:
122 case BuiltinType::ULong:
124 case BuiltinType::ULongLong:
126 case BuiltinType::UInt128:
133 return MetadataCache[Ty] =
134 createTBAAScalarType(BTy->getName(Features), getChar());
141 if (Ty->isPointerType())
142 return MetadataCache[Ty] = createTBAAScalarType(
"any pointer",
147 if (
const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
152 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
153 return MetadataCache[Ty] = getChar();
156 llvm::raw_svector_ostream Out(OutName);
158 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
162 return MetadataCache[Ty] = getChar();
166 return createTBAAScalarType(
"vtable pointer", getRoot());
170 CodeGenTBAA::CollectFields(uint64_t BaseOffset,
184 if (
Decl->bases_begin() !=
Decl->bases_end())
191 e = RD->
field_end(); i != e; ++i, ++idx) {
192 uint64_t
Offset = BaseOffset +
195 if (!CollectFields(Offset, FieldQTy, Fields,
203 uint64_t Offset = BaseOffset;
205 llvm::MDNode *TBAAInfo = MayAlias ? getChar() :
getTBAAInfo(QTy);
207 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
215 if (llvm::MDNode *N = StructMetadataCache[Ty])
220 return MDHelper.createTBAAStructNode(Fields);
223 return StructMetadataCache[Ty] =
nullptr;
245 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
255 e = RD->
field_end(); i != e; ++i, ++idx) {
257 llvm::MDNode *FieldNode;
263 return StructTypeMetadataCache[Ty] =
nullptr;
264 Fields.push_back(std::make_pair(
269 if (Features.CPlusPlus) {
271 llvm::raw_svector_ostream Out(OutName);
277 return StructTypeMetadataCache[Ty] =
278 MDHelper.createTBAAStructTypeNode(OutName, Fields);
281 return StructMetadataCache[Ty] =
nullptr;
291 if (!CodeGenOpts.StructPathTBAA)
296 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
299 llvm::MDNode *BNode =
nullptr;
303 return StructTagMetadataCache[PathTag] =
304 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
306 return StructTagMetadataCache[PathTag] =
307 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
314 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
317 return ScalarTagMetadataCache[AccessNode] =
318 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Defines the clang::ASTContext interface.
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
A (possibly-)qualified type.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Decl - This represents one declaration (or definition), e.g.
bool hasFlexibleArrayMember() const
The base class of the type hierarchy.
field_iterator field_begin() const
RecordDecl - Represents a struct/union/class.
CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, const CodeGenOptions &CGO, const LangOptions &Features, MangleContext &MContext)
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::MDNode * getTBAAInfoForVTablePtr()
getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a dereference of a vtable pointer...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
llvm::MDNode * getTBAAStructTypeInfo(QualType QType)
Get the MDNode in the type DAG for given struct type QType.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
field_iterator field_end() const
static bool isTBAAPathStruct(QualType QTy)
Check if the given type can be handled by path-aware TBAA.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
llvm::MDNode * getTBAAScalarTagInfo(llvm::MDNode *AccessNode)
Get the scalar tag MDNode for a given scalar type.
static bool TypeHasMayAlias(QualType QTy)
RecordDecl * getDefinition() const
getDefinition - Returns the RecordDecl that actually defines this struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums...
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
llvm::MDNode * getTBAAStructInfo(QualType QTy)
getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of the given type...
llvm::MDNode * getTBAAStructTagInfo(QualType BaseQType, llvm::MDNode *AccessNode, uint64_t Offset)
Get the tag MDNode for a given base type, the actual scalar access MDNode and offset into the base ty...
virtual void mangleTypeName(QualType T, raw_ostream &)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing...
llvm::MDNode * getTBAAInfo(QualType QTy)
getTBAAInfo - Get the TBAA MDNode to be used for a dereference of the given type. ...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
const T * getAs() const
Member-template getAs<specific type>'.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Represents a C++ struct/union/class.
This class is used for builtin types like 'int'.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.