#include "CodeGenTBAA.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Mangle.h"
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/CodeGenOptions.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
using namespace clang;
using namespace CodeGen;
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
const CodeGenOptions &CGO,
const LangOptions &Features, MangleContext &MContext)
: Context(Ctx), Module(M), CodeGenOpts(CGO),
Features(Features), MContext(MContext), MDHelper(M.getContext()),
Root(nullptr), Char(nullptr)
{}
CodeGenTBAA::~CodeGenTBAA() {
}
llvm::MDNode *CodeGenTBAA::getRoot() {
if (!Root) {
if (Features.CPlusPlus)
Root = MDHelper.createTBAARoot("Simple C++ TBAA");
else
Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
}
return Root;
}
llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
llvm::MDNode *Parent,
uint64_t Size) {
if (CodeGenOpts.NewStructPathTBAA) {
llvm::Metadata *Id = MDHelper.createString(Name);
return MDHelper.createTBAATypeNode(Parent, Size, Id);
}
return MDHelper.createTBAAScalarTypeNode(Name, Parent);
}
llvm::MDNode *CodeGenTBAA::getChar() {
if (!Char)
Char = createScalarTypeNode("omnipotent char", getRoot(), 1);
return Char;
}
static bool TypeHasMayAlias(QualType QTy) {
if (auto *TD = QTy->getAsTagDecl())
if (TD->hasAttr<MayAliasAttr>())
return true;
while (auto *TT = QTy->getAs<TypedefType>()) {
if (TT->getDecl()->hasAttr<MayAliasAttr>())
return true;
QTy = TT->desugar();
}
return false;
}
static bool isValidBaseType(QualType QTy) {
if (QTy->isReferenceType())
return false;
if (const RecordType *TTy = QTy->getAs<RecordType>()) {
const RecordDecl *RD = TTy->getDecl()->getDefinition();
if (!RD)
return false;
if (RD->hasFlexibleArrayMember())
return false;
if (RD->isStruct() || RD->isClass())
return true;
}
return false;
}
llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
switch (BTy->getKind()) {
case BuiltinType::Char_U:
case BuiltinType::Char_S:
case BuiltinType::UChar:
case BuiltinType::SChar:
return getChar();
case BuiltinType::UShort:
return getTypeInfo(Context.ShortTy);
case BuiltinType::UInt:
return getTypeInfo(Context.IntTy);
case BuiltinType::ULong:
return getTypeInfo(Context.LongTy);
case BuiltinType::ULongLong:
return getTypeInfo(Context.LongLongTy);
case BuiltinType::UInt128:
return getTypeInfo(Context.Int128Ty);
case BuiltinType::UShortFract:
return getTypeInfo(Context.ShortFractTy);
case BuiltinType::UFract:
return getTypeInfo(Context.FractTy);
case BuiltinType::ULongFract:
return getTypeInfo(Context.LongFractTy);
case BuiltinType::SatUShortFract:
return getTypeInfo(Context.SatShortFractTy);
case BuiltinType::SatUFract:
return getTypeInfo(Context.SatFractTy);
case BuiltinType::SatULongFract:
return getTypeInfo(Context.SatLongFractTy);
case BuiltinType::UShortAccum:
return getTypeInfo(Context.ShortAccumTy);
case BuiltinType::UAccum:
return getTypeInfo(Context.AccumTy);
case BuiltinType::ULongAccum:
return getTypeInfo(Context.LongAccumTy);
case BuiltinType::SatUShortAccum:
return getTypeInfo(Context.SatShortAccumTy);
case BuiltinType::SatUAccum:
return getTypeInfo(Context.SatAccumTy);
case BuiltinType::SatULongAccum:
return getTypeInfo(Context.SatLongAccumTy);
default:
return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
}
}
if (Ty->isStdByteType())
return getChar();
if (Ty->isPointerType() || Ty->isReferenceType())
return createScalarTypeNode("any pointer", getChar(), Size);
if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
return getChar();
SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
MContext.mangleTypeName(QualType(ETy, 0), Out);
return createScalarTypeNode(OutName, getChar(), Size);
}
if (const auto *EIT = dyn_cast<BitIntType>(Ty)) {
SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
Out << "_BitInt(" << EIT->getNumBits() << ')';
return createScalarTypeNode(OutName, getChar(), Size);
}
return getChar();
}
llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
return nullptr;
if (TypeHasMayAlias(QTy))
return getChar();
if (isValidBaseType(QTy))
return getBaseTypeInfo(QTy);
const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
if (llvm::MDNode *N = MetadataCache[Ty])
return N;
llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
return MetadataCache[Ty] = TypeNode;
}
TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
if (AccessType->isIncompleteType())
return TBAAAccessInfo::getIncompleteInfo();
if (TypeHasMayAlias(AccessType))
return TBAAAccessInfo::getMayAliasInfo();
uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
return TBAAAccessInfo(getTypeInfo(AccessType), Size);
}
TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
llvm::DataLayout DL(&Module);
unsigned Size = DL.getPointerTypeSize(VTablePtrType);
return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
Size);
}
bool
CodeGenTBAA::CollectFields(uint64_t BaseOffset,
QualType QTy,
SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
Fields,
bool MayAlias) {
if (const RecordType *TTy = QTy->getAs<RecordType>()) {
const RecordDecl *RD = TTy->getDecl()->getDefinition();
if (RD->hasFlexibleArrayMember())
return false;
if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
if (Decl->bases_begin() != Decl->bases_end())
return false;
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
unsigned idx = 0;
for (RecordDecl::field_iterator i = RD->field_begin(),
e = RD->field_end(); i != e; ++i, ++idx) {
if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
continue;
uint64_t Offset = BaseOffset +
Layout.getFieldOffset(idx) / Context.getCharWidth();
QualType FieldQTy = i->getType();
if (!CollectFields(Offset, FieldQTy, Fields,
MayAlias || TypeHasMayAlias(FieldQTy)))
return false;
}
return true;
}
uint64_t Offset = BaseOffset;
uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
return true;
}
llvm::MDNode *
CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
if (llvm::MDNode *N = StructMetadataCache[Ty])
return N;
SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
return MDHelper.createTBAAStructNode(Fields);
return StructMetadataCache[Ty] = nullptr;
}
llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
if (auto *TTy = dyn_cast<RecordType>(Ty)) {
const RecordDecl *RD = TTy->getDecl()->getDefinition();
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
using TBAAStructField = llvm::MDBuilder::TBAAStructField;
SmallVector<TBAAStructField, 4> Fields;
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0)
return BaseTypeMetadataCache[Ty] = nullptr;
for (const CXXBaseSpecifier &B : CXXRD->bases()) {
if (B.isVirtual())
continue;
QualType BaseQTy = B.getType();
const CXXRecordDecl *BaseRD = BaseQTy->getAsCXXRecordDecl();
if (BaseRD->isEmpty())
continue;
llvm::MDNode *TypeNode = isValidBaseType(BaseQTy)
? getBaseTypeInfo(BaseQTy)
: getTypeInfo(BaseQTy);
if (!TypeNode)
return BaseTypeMetadataCache[Ty] = nullptr;
uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
uint64_t Size =
Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
Fields.push_back(
llvm::MDBuilder::TBAAStructField(Offset, Size, TypeNode));
}
llvm::sort(Fields,
[](const TBAAStructField &A, const TBAAStructField &B) {
return A.Offset < B.Offset;
});
}
for (FieldDecl *Field : RD->fields()) {
if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
continue;
QualType FieldQTy = Field->getType();
llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
if (!TypeNode)
return BaseTypeMetadataCache[Ty] = nullptr;
uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
TypeNode));
}
SmallString<256> OutName;
if (Features.CPlusPlus) {
llvm::raw_svector_ostream Out(OutName);
MContext.mangleTypeName(QualType(Ty, 0), Out);
} else {
OutName = RD->getName();
}
if (CodeGenOpts.NewStructPathTBAA) {
llvm::MDNode *Parent = getChar();
uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
llvm::Metadata *Id = MDHelper.createString(OutName);
return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
}
SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
for (const auto &Field : Fields)
OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
}
return nullptr;
}
llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
if (!isValidBaseType(QTy))
return nullptr;
const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
return N;
llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
return BaseTypeMetadataCache[Ty] = TypeNode;
}
llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
if (Info.isMayAlias())
Info = TBAAAccessInfo(getChar(), Info.Size);
if (!Info.AccessType)
return nullptr;
if (!CodeGenOpts.StructPathTBAA)
Info = TBAAAccessInfo(Info.AccessType, Info.Size);
llvm::MDNode *&N = AccessTagMetadataCache[Info];
if (N)
return N;
if (!Info.BaseType) {
Info.BaseType = Info.AccessType;
assert(!Info.Offset && "Nonzero offset for an access with no base type!");
}
if (CodeGenOpts.NewStructPathTBAA) {
return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
Info.Offset, Info.Size);
}
return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
Info.Offset);
}
TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
TBAAAccessInfo TargetInfo) {
if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
return TBAAAccessInfo::getMayAliasInfo();
return TargetInfo;
}
TBAAAccessInfo
CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
TBAAAccessInfo InfoB) {
if (InfoA == InfoB)
return InfoA;
if (!InfoA || !InfoB)
return TBAAAccessInfo();
if (InfoA.isMayAlias() || InfoB.isMayAlias())
return TBAAAccessInfo::getMayAliasInfo();
return TBAAAccessInfo::getMayAliasInfo();
}
TBAAAccessInfo
CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
TBAAAccessInfo SrcInfo) {
if (DestInfo == SrcInfo)
return DestInfo;
if (!DestInfo || !SrcInfo)
return TBAAAccessInfo();
if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
return TBAAAccessInfo::getMayAliasInfo();
return TBAAAccessInfo::getMayAliasInfo();
}