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 if (Features.CPlusPlus)
49 Root = MDHelper.createTBAARoot(
"Simple C++ TBAA");
51 Root = MDHelper.createTBAARoot(
"Simple C/C++ TBAA");
59 llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef
Name,
60 llvm::MDNode *Parent) {
61 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
64 llvm::MDNode *CodeGenTBAA::getChar() {
70 Char = createTBAAScalarType(
"omnipotent char", getRoot());
77 if (
const TagType *TTy = dyn_cast<TagType>(QTy))
78 return TTy->getDecl()->hasAttr<MayAliasAttr>();
81 if (
const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
82 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
94 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
104 if (llvm::MDNode *N = MetadataCache[Ty])
108 if (
const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
109 switch (BTy->getKind()) {
115 case BuiltinType::Char_U:
116 case BuiltinType::Char_S:
117 case BuiltinType::UChar:
118 case BuiltinType::SChar:
122 case BuiltinType::UShort:
124 case BuiltinType::UInt:
126 case BuiltinType::ULong:
128 case BuiltinType::ULongLong:
130 case BuiltinType::UInt128:
137 return MetadataCache[Ty] =
138 createTBAAScalarType(BTy->getName(Features), getChar());
145 if (Ty->isPointerType())
146 return MetadataCache[Ty] = createTBAAScalarType(
"any pointer",
151 if (
const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
156 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
157 return MetadataCache[Ty] = getChar();
160 llvm::raw_svector_ostream Out(OutName);
162 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
166 return MetadataCache[Ty] = getChar();
170 return createTBAAScalarType(
"vtable pointer", getRoot());
174 CodeGenTBAA::CollectFields(uint64_t BaseOffset,
188 if (
Decl->bases_begin() !=
Decl->bases_end())
195 e = RD->
field_end(); i != e; ++i, ++idx) {
196 uint64_t
Offset = BaseOffset +
199 if (!CollectFields(Offset, FieldQTy, Fields,
207 uint64_t Offset = BaseOffset;
209 llvm::MDNode *TBAAInfo = MayAlias ? getChar() :
getTBAAInfo(QTy);
211 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
219 if (llvm::MDNode *N = StructMetadataCache[Ty])
224 return MDHelper.createTBAAStructNode(Fields);
227 return StructMetadataCache[Ty] =
nullptr;
249 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
259 e = RD->
field_end(); i != e; ++i, ++idx) {
261 llvm::MDNode *FieldNode;
267 return StructTypeMetadataCache[Ty] =
nullptr;
268 Fields.push_back(std::make_pair(
273 if (Features.CPlusPlus) {
275 llvm::raw_svector_ostream Out(OutName);
281 return StructTypeMetadataCache[Ty] =
282 MDHelper.createTBAAStructTypeNode(OutName, Fields);
285 return StructMetadataCache[Ty] =
nullptr;
295 if (!CodeGenOpts.StructPathTBAA)
300 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
303 llvm::MDNode *BNode =
nullptr;
307 return StructTagMetadataCache[PathTag] =
308 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
310 return StructTagMetadataCache[PathTag] =
311 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
318 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
321 return ScalarTagMetadataCache[AccessNode] =
322 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.
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.