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);
159 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
163 return MetadataCache[Ty] = getChar();
167 return createTBAAScalarType(
"vtable pointer", getRoot());
171 CodeGenTBAA::CollectFields(uint64_t BaseOffset,
185 if (
Decl->bases_begin() !=
Decl->bases_end())
192 e = RD->
field_end(); i != e; ++i, ++idx) {
193 uint64_t
Offset = BaseOffset +
196 if (!CollectFields(Offset, FieldQTy, Fields,
204 uint64_t Offset = BaseOffset;
206 llvm::MDNode *TBAAInfo = MayAlias ? getChar() :
getTBAAInfo(QTy);
208 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
216 if (llvm::MDNode *N = StructMetadataCache[Ty])
221 return MDHelper.createTBAAStructNode(Fields);
224 return StructMetadataCache[Ty] =
nullptr;
246 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
256 e = RD->
field_end(); i != e; ++i, ++idx) {
258 llvm::MDNode *FieldNode;
264 return StructTypeMetadataCache[Ty] =
nullptr;
265 Fields.push_back(std::make_pair(
270 if (Features.CPlusPlus) {
272 llvm::raw_svector_ostream Out(OutName);
279 return StructTypeMetadataCache[Ty] =
280 MDHelper.createTBAAStructTypeNode(OutName, Fields);
283 return StructMetadataCache[Ty] =
nullptr;
293 if (!CodeGenOpts.StructPathTBAA)
298 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
301 llvm::MDNode *BNode =
nullptr;
305 return StructTagMetadataCache[PathTag] =
306 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
308 return StructTagMetadataCache[PathTag] =
309 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
316 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
319 return ScalarTagMetadataCache[AccessNode] =
320 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Defines the clang::ASTContext interface.
StringRef getName() const
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
bool hasFlexibleArrayMember() const
field_iterator field_begin() const
CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, const CodeGenOptions &CGO, const LangOptions &Features, MangleContext &MContext)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::MDNode * getTBAAInfoForVTablePtr()
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
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.
llvm::MDNode * getTBAAScalarTagInfo(llvm::MDNode *AccessNode)
Get the scalar tag MDNode for a given scalar type.
static bool TypeHasMayAlias(QualType QTy)
RecordDecl * getDefinition() const
llvm::MDNode * getTBAAStructInfo(QualType QTy)
llvm::MDNode * getTBAAStructTagInfo(QualType BaseQType, llvm::MDNode *AccessNode, uint64_t Offset)
Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
virtual void mangleTypeName(QualType T, raw_ostream &)=0
llvm::MDNode * getTBAAInfo(QualType QTy)
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Represents a C++ struct/union/class.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.