2010-10-14 23:06:10 +00:00
|
|
|
//===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-10-21 18:49:12 +00:00
|
|
|
// This is the code that manages TBAA information and defines the TBAA policy
|
|
|
|
// for the optimizer to use. Relevant standards text includes:
|
2010-10-15 20:54:41 +00:00
|
|
|
//
|
|
|
|
// C99 6.5p7
|
|
|
|
// C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
|
2010-10-14 23:06:10 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenTBAA.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-01 15:09:41 +00:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-01-13 18:57:25 +00:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-12-01 15:09:41 +00:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2016-04-08 16:52:00 +00:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-04-04 21:53:22 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2013-01-02 11:45:17 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2017-11-27 09:39:29 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-01-02 11:45:17 +00:00
|
|
|
#include "llvm/IR/Type.h"
|
2010-10-14 23:06:10 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2017-11-27 09:39:29 +00:00
|
|
|
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
|
2012-04-24 06:57:01 +00:00
|
|
|
const CodeGenOptions &CGO,
|
2010-10-15 20:23:12 +00:00
|
|
|
const LangOptions &Features, MangleContext &MContext)
|
2017-11-27 09:39:29 +00:00
|
|
|
: Context(Ctx), Module(M), CodeGenOpts(CGO),
|
|
|
|
Features(Features), MContext(MContext), MDHelper(M.getContext()),
|
|
|
|
Root(nullptr), Char(nullptr)
|
|
|
|
{}
|
2010-10-14 23:06:10 +00:00
|
|
|
|
2015-10-20 13:23:58 +00:00
|
|
|
CodeGenTBAA::~CodeGenTBAA() {
|
|
|
|
}
|
2010-10-14 23:06:10 +00:00
|
|
|
|
2010-10-25 21:48:30 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getRoot() {
|
|
|
|
// Define the root of the tree. This identifies the tree, so that
|
|
|
|
// if our LLVM IR is linked with LLVM IR from a different front-end
|
|
|
|
// (or a different version of this front-end), their TBAA trees will
|
|
|
|
// remain distinct, and the optimizer will treat them conservatively.
|
2016-02-11 19:19:18 +00:00
|
|
|
if (!Root) {
|
|
|
|
if (Features.CPlusPlus)
|
|
|
|
Root = MDHelper.createTBAARoot("Simple C++ TBAA");
|
|
|
|
else
|
|
|
|
Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
|
|
|
|
}
|
2010-10-25 21:48:30 +00:00
|
|
|
|
|
|
|
return Root;
|
|
|
|
}
|
|
|
|
|
2017-11-27 09:39:29 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
|
|
|
|
llvm::MDNode *Parent,
|
|
|
|
uint64_t Size) {
|
|
|
|
(void)Size; // TODO: Support generation of size-aware type nodes.
|
2013-10-08 00:08:49 +00:00
|
|
|
return MDHelper.createTBAAScalarTypeNode(Name, Parent);
|
2013-04-11 23:02:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-25 21:48:30 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getChar() {
|
|
|
|
// Define the root of the tree for user-accessible memory. C and C++
|
|
|
|
// give special powers to char and certain similar types. However,
|
|
|
|
// these special powers only cover user-accessible memory, and doesn't
|
|
|
|
// include things like vtables.
|
|
|
|
if (!Char)
|
2017-11-27 09:39:29 +00:00
|
|
|
Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1);
|
2010-10-25 21:48:30 +00:00
|
|
|
|
|
|
|
return Char;
|
|
|
|
}
|
|
|
|
|
[CodeGen] Add initial support for union members in TBAA
The basic idea behind this patch is that since in strict aliasing
mode all accesses to union members require their outermost
enclosing union objects to be specified explicitly, then for a
couple given accesses to union members of the form
p->a.b.c...
q->x.y.z...
it is known they can only alias if both p and q point to the same
union type and offset ranges of members a.b.c... and x.y.z...
overlap. Note that the actual types of the members do not matter.
Specifically, in this patch we do the following:
* Make unions to be valid TBAA base access types. This enables
generation of TBAA type descriptors for unions.
* Encode union types as structures with a single member of a
special "union member" type. Currently we do not encode
information about sizes of types, but conceptually such union
members are considered to be of the size of the whole union.
* Encode accesses to direct and indirect union members, including
member arrays, as accesses to these special members. All
accesses to members of a union thus get the same offset, which
is the offset of the union they are part of. This means the
existing LLVM TBAA machinery is able to handle such accesses
with no changes.
While this is already an improvement comparing to the current
situation, that is, representing all union accesses as may-alias
ones, there are further changes planned to complete the support
for unions. One of them is storing information about access sizes
so we can distinct accesses to non-overlapping union members,
including accesses to different elements of member arrays.
Another change is encoding type sizes in order to make it
possible to compute offsets within constant-indexed array
elements. These enhancements will be addressed with separate
patches.
Differential Revision: https://reviews.llvm.org/D39455
llvm-svn: 319413
2017-11-30 09:26:39 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getUnionMemberType(uint64_t Size) {
|
|
|
|
return createScalarTypeNode("union member", getChar(), Size);
|
|
|
|
}
|
|
|
|
|
2010-12-13 23:51:08 +00:00
|
|
|
static bool TypeHasMayAlias(QualType QTy) {
|
|
|
|
// Tagged types have declarations, and therefore may have attributes.
|
|
|
|
if (const TagType *TTy = dyn_cast<TagType>(QTy))
|
|
|
|
return TTy->getDecl()->hasAttr<MayAliasAttr>();
|
|
|
|
|
|
|
|
// Typedef types have declarations, and therefore may have attributes.
|
|
|
|
if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
|
|
|
|
if (TTy->getDecl()->hasAttr<MayAliasAttr>())
|
|
|
|
return true;
|
|
|
|
// Also, their underlying types may have relevant attributes.
|
|
|
|
return TypeHasMayAlias(TTy->desugar());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:05:34 +00:00
|
|
|
/// Check if the given type is a valid base type to be used in access tags.
|
|
|
|
static bool isValidBaseType(QualType QTy) {
|
|
|
|
if (QTy->isReferenceType())
|
|
|
|
return false;
|
|
|
|
if (const RecordType *TTy = QTy->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *RD = TTy->getDecl()->getDefinition();
|
|
|
|
// Incomplete types are not valid base access types.
|
|
|
|
if (!RD)
|
|
|
|
return false;
|
|
|
|
if (RD->hasFlexibleArrayMember())
|
|
|
|
return false;
|
[CodeGen] Add initial support for union members in TBAA
The basic idea behind this patch is that since in strict aliasing
mode all accesses to union members require their outermost
enclosing union objects to be specified explicitly, then for a
couple given accesses to union members of the form
p->a.b.c...
q->x.y.z...
it is known they can only alias if both p and q point to the same
union type and offset ranges of members a.b.c... and x.y.z...
overlap. Note that the actual types of the members do not matter.
Specifically, in this patch we do the following:
* Make unions to be valid TBAA base access types. This enables
generation of TBAA type descriptors for unions.
* Encode union types as structures with a single member of a
special "union member" type. Currently we do not encode
information about sizes of types, but conceptually such union
members are considered to be of the size of the whole union.
* Encode accesses to direct and indirect union members, including
member arrays, as accesses to these special members. All
accesses to members of a union thus get the same offset, which
is the offset of the union they are part of. This means the
existing LLVM TBAA machinery is able to handle such accesses
with no changes.
While this is already an improvement comparing to the current
situation, that is, representing all union accesses as may-alias
ones, there are further changes planned to complete the support
for unions. One of them is storing information about access sizes
so we can distinct accesses to non-overlapping union members,
including accesses to different elements of member arrays.
Another change is encoding type sizes in order to make it
possible to compute offsets within constant-indexed array
elements. These enhancements will be addressed with separate
patches.
Differential Revision: https://reviews.llvm.org/D39455
llvm-svn: 319413
2017-11-30 09:26:39 +00:00
|
|
|
// For now, we do not allow interface classes to be base access types.
|
|
|
|
if (RD->isStruct() || RD->isClass() || RD->isUnion())
|
2017-10-31 11:05:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:18:06 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
|
2017-11-27 09:39:29 +00:00
|
|
|
uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
|
|
|
|
|
2010-10-21 18:49:12 +00:00
|
|
|
// Handle builtin types.
|
2010-10-14 23:39:00 +00:00
|
|
|
if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
|
2010-10-14 23:06:10 +00:00
|
|
|
switch (BTy->getKind()) {
|
2010-10-15 17:52:03 +00:00
|
|
|
// Character types are special and can alias anything.
|
|
|
|
// In C++, this technically only includes "char" and "unsigned char",
|
|
|
|
// and not "signed char". In C, it includes all three. For now,
|
2010-10-15 20:24:10 +00:00
|
|
|
// the risk of exploiting this detail in C++ seems likely to outweigh
|
2010-10-15 17:52:03 +00:00
|
|
|
// the benefit.
|
2010-10-14 23:06:10 +00:00
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::SChar:
|
2010-10-25 21:48:30 +00:00
|
|
|
return getChar();
|
2010-10-14 23:39:00 +00:00
|
|
|
|
|
|
|
// Unsigned types can alias their corresponding signed types.
|
|
|
|
case BuiltinType::UShort:
|
2017-10-02 09:54:47 +00:00
|
|
|
return getTypeInfo(Context.ShortTy);
|
2010-10-14 23:39:00 +00:00
|
|
|
case BuiltinType::UInt:
|
2017-10-02 09:54:47 +00:00
|
|
|
return getTypeInfo(Context.IntTy);
|
2010-10-14 23:39:00 +00:00
|
|
|
case BuiltinType::ULong:
|
2017-10-02 09:54:47 +00:00
|
|
|
return getTypeInfo(Context.LongTy);
|
2010-10-14 23:39:00 +00:00
|
|
|
case BuiltinType::ULongLong:
|
2017-10-02 09:54:47 +00:00
|
|
|
return getTypeInfo(Context.LongLongTy);
|
2010-10-14 23:39:00 +00:00
|
|
|
case BuiltinType::UInt128:
|
2017-10-02 09:54:47 +00:00
|
|
|
return getTypeInfo(Context.Int128Ty);
|
2010-10-14 23:39:00 +00:00
|
|
|
|
2010-10-15 20:24:53 +00:00
|
|
|
// Treat all other builtin types as distinct types. This includes
|
|
|
|
// treating wchar_t, char16_t, and char32_t as distinct from their
|
|
|
|
// "underlying types".
|
2010-10-14 23:06:10 +00:00
|
|
|
default:
|
2017-11-27 09:39:29 +00:00
|
|
|
return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
|
2010-10-14 23:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 23:33:58 +00:00
|
|
|
// C++1z [basic.lval]p10: "If a program attempts to access the stored value of
|
|
|
|
// an object through a glvalue of other than one of the following types the
|
|
|
|
// behavior is undefined: [...] a char, unsigned char, or std::byte type."
|
|
|
|
if (Ty->isStdByteType())
|
2017-11-21 11:18:06 +00:00
|
|
|
return getChar();
|
2017-07-25 23:33:58 +00:00
|
|
|
|
2017-09-26 14:22:48 +00:00
|
|
|
// Handle pointers and references.
|
2010-10-15 20:26:20 +00:00
|
|
|
// TODO: Implement C++'s type "similarity" and consider dis-"similar"
|
|
|
|
// pointers distinct.
|
2017-09-26 14:22:48 +00:00
|
|
|
if (Ty->isPointerType() || Ty->isReferenceType())
|
2017-11-27 09:39:29 +00:00
|
|
|
return createScalarTypeNode("any pointer", getChar(), Size);
|
2010-10-15 00:01:39 +00:00
|
|
|
|
2010-10-15 20:23:12 +00:00
|
|
|
// Enum types are distinct types. In C++ they have "underlying types",
|
|
|
|
// however they aren't related for TBAA.
|
|
|
|
if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
|
|
|
|
// In C++ mode, types have linkage, so we can rely on the ODR and
|
|
|
|
// on their mangled names, if they're external.
|
|
|
|
// TODO: Is there a way to get a program-wide unique name for a
|
|
|
|
// decl with local linkage or no linkage?
|
2013-07-05 20:27:40 +00:00
|
|
|
if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
|
2017-11-21 11:18:06 +00:00
|
|
|
return getChar();
|
2010-10-15 20:23:12 +00:00
|
|
|
|
2012-02-05 02:13:05 +00:00
|
|
|
SmallString<256> OutName;
|
2011-02-11 02:52:17 +00:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2013-11-19 23:23:00 +00:00
|
|
|
MContext.mangleTypeName(QualType(ETy, 0), Out);
|
2017-11-27 09:39:29 +00:00
|
|
|
return createScalarTypeNode(OutName, getChar(), Size);
|
2010-10-15 20:23:12 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 23:39:00 +00:00
|
|
|
// For now, handle any other kind of type conservatively.
|
2017-11-21 11:18:06 +00:00
|
|
|
return getChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
|
|
|
|
// At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
|
|
|
|
if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If the type has the may_alias attribute (even on a typedef), it is
|
|
|
|
// effectively in the general char alias class.
|
|
|
|
if (TypeHasMayAlias(QTy))
|
|
|
|
return getChar();
|
|
|
|
|
|
|
|
// We need this function to not fall back to returning the "omnipotent char"
|
|
|
|
// type node for aggregate and union types. Otherwise, any dereference of an
|
|
|
|
// aggregate will result into the may-alias access descriptor, meaning all
|
|
|
|
// subsequent accesses to direct and indirect members of that aggregate will
|
|
|
|
// be considered may-alias too.
|
|
|
|
// TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
|
|
|
|
if (isValidBaseType(QTy))
|
|
|
|
return getBaseTypeInfo(QTy);
|
|
|
|
|
|
|
|
const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
|
|
|
|
if (llvm::MDNode *N = MetadataCache[Ty])
|
|
|
|
return N;
|
|
|
|
|
|
|
|
// Note that the following helper call is allowed to add new nodes to the
|
|
|
|
// cache, which invalidates all its previously obtained iterators. So we
|
|
|
|
// first generate the node for the type and then add that node to the cache.
|
|
|
|
llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
|
|
|
|
return MetadataCache[Ty] = TypeNode;
|
2010-10-14 23:06:10 +00:00
|
|
|
}
|
2012-03-26 17:03:51 +00:00
|
|
|
|
2017-11-27 09:39:29 +00:00
|
|
|
TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
|
|
|
|
llvm::DataLayout DL(&Module);
|
|
|
|
unsigned Size = DL.getPointerTypeSize(VTablePtrType);
|
|
|
|
return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
|
|
|
|
Size);
|
2012-03-26 17:03:51 +00:00
|
|
|
}
|
2012-09-28 21:58:29 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
CodeGenTBAA::CollectFields(uint64_t BaseOffset,
|
|
|
|
QualType QTy,
|
|
|
|
SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
|
|
|
|
Fields,
|
|
|
|
bool MayAlias) {
|
|
|
|
/* Things not handled yet include: C++ base classes, bitfields, */
|
|
|
|
|
|
|
|
if (const RecordType *TTy = QTy->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *RD = TTy->getDecl()->getDefinition();
|
|
|
|
if (RD->hasFlexibleArrayMember())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: Handle C++ base classes.
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, treat whatever it is as a field. */
|
|
|
|
uint64_t Offset = BaseOffset;
|
|
|
|
uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
|
2017-10-05 11:08:17 +00:00
|
|
|
llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
|
2017-11-27 09:39:29 +00:00
|
|
|
llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
|
2013-04-22 19:50:07 +00:00
|
|
|
Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
|
2012-09-28 21:58:29 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
// For now, handle any other kind of type conservatively.
|
2014-05-21 05:09:00 +00:00
|
|
|
return StructMetadataCache[Ty] = nullptr;
|
2012-09-28 21:58:29 +00:00
|
|
|
}
|
2013-04-04 21:53:22 +00:00
|
|
|
|
2017-11-21 11:18:06 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
|
|
|
|
if (auto *TTy = dyn_cast<RecordType>(Ty)) {
|
2013-04-04 21:53:22 +00:00
|
|
|
const RecordDecl *RD = TTy->getDecl()->getDefinition();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2017-11-27 09:39:29 +00:00
|
|
|
SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
|
[CodeGen] Add initial support for union members in TBAA
The basic idea behind this patch is that since in strict aliasing
mode all accesses to union members require their outermost
enclosing union objects to be specified explicitly, then for a
couple given accesses to union members of the form
p->a.b.c...
q->x.y.z...
it is known they can only alias if both p and q point to the same
union type and offset ranges of members a.b.c... and x.y.z...
overlap. Note that the actual types of the members do not matter.
Specifically, in this patch we do the following:
* Make unions to be valid TBAA base access types. This enables
generation of TBAA type descriptors for unions.
* Encode union types as structures with a single member of a
special "union member" type. Currently we do not encode
information about sizes of types, but conceptually such union
members are considered to be of the size of the whole union.
* Encode accesses to direct and indirect union members, including
member arrays, as accesses to these special members. All
accesses to members of a union thus get the same offset, which
is the offset of the union they are part of. This means the
existing LLVM TBAA machinery is able to handle such accesses
with no changes.
While this is already an improvement comparing to the current
situation, that is, representing all union accesses as may-alias
ones, there are further changes planned to complete the support
for unions. One of them is storing information about access sizes
so we can distinct accesses to non-overlapping union members,
including accesses to different elements of member arrays.
Another change is encoding type sizes in order to make it
possible to compute offsets within constant-indexed array
elements. These enhancements will be addressed with separate
patches.
Differential Revision: https://reviews.llvm.org/D39455
llvm-svn: 319413
2017-11-30 09:26:39 +00:00
|
|
|
if (RD->isUnion()) {
|
|
|
|
// Unions are represented as structures with a single member that has a
|
|
|
|
// special type and occupies the whole object.
|
|
|
|
uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
|
|
|
|
llvm::MDNode *TypeNode = getUnionMemberType(Size);
|
|
|
|
Fields.push_back(llvm::MDBuilder::TBAAStructField(/* Offset= */ 0, Size,
|
2017-11-27 09:39:29 +00:00
|
|
|
TypeNode));
|
[CodeGen] Add initial support for union members in TBAA
The basic idea behind this patch is that since in strict aliasing
mode all accesses to union members require their outermost
enclosing union objects to be specified explicitly, then for a
couple given accesses to union members of the form
p->a.b.c...
q->x.y.z...
it is known they can only alias if both p and q point to the same
union type and offset ranges of members a.b.c... and x.y.z...
overlap. Note that the actual types of the members do not matter.
Specifically, in this patch we do the following:
* Make unions to be valid TBAA base access types. This enables
generation of TBAA type descriptors for unions.
* Encode union types as structures with a single member of a
special "union member" type. Currently we do not encode
information about sizes of types, but conceptually such union
members are considered to be of the size of the whole union.
* Encode accesses to direct and indirect union members, including
member arrays, as accesses to these special members. All
accesses to members of a union thus get the same offset, which
is the offset of the union they are part of. This means the
existing LLVM TBAA machinery is able to handle such accesses
with no changes.
While this is already an improvement comparing to the current
situation, that is, representing all union accesses as may-alias
ones, there are further changes planned to complete the support
for unions. One of them is storing information about access sizes
so we can distinct accesses to non-overlapping union members,
including accesses to different elements of member arrays.
Another change is encoding type sizes in order to make it
possible to compute offsets within constant-indexed array
elements. These enhancements will be addressed with separate
patches.
Differential Revision: https://reviews.llvm.org/D39455
llvm-svn: 319413
2017-11-30 09:26:39 +00:00
|
|
|
} else {
|
|
|
|
for (FieldDecl *Field : RD->fields()) {
|
|
|
|
QualType FieldQTy = Field->getType();
|
|
|
|
llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
|
|
|
|
getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
|
|
|
|
if (!TypeNode)
|
|
|
|
return 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));
|
|
|
|
}
|
2013-04-04 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<256> OutName;
|
2013-08-21 20:58:45 +00:00
|
|
|
if (Features.CPlusPlus) {
|
2013-11-19 23:23:00 +00:00
|
|
|
// Don't use the mangler for C code.
|
2013-08-21 20:58:45 +00:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2013-11-19 23:23:00 +00:00
|
|
|
MContext.mangleTypeName(QualType(Ty, 0), Out);
|
2013-08-21 20:58:45 +00:00
|
|
|
} else {
|
|
|
|
OutName = RD->getName();
|
|
|
|
}
|
2017-11-27 09:39:29 +00:00
|
|
|
|
|
|
|
// TODO: Support size-aware type nodes and create one here for the
|
|
|
|
// given aggregate type.
|
|
|
|
|
2013-04-04 21:53:22 +00:00
|
|
|
// Create the struct type node with a vector of pairs (offset, type).
|
2017-11-27 09:39:29 +00:00
|
|
|
SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
|
|
|
|
for (const auto &Field : Fields)
|
|
|
|
OffsetsAndTypes.push_back(std::make_pair(Field.TBAA, Field.Offset));
|
|
|
|
return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
|
2013-04-04 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 11:18:06 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// Note that the following helper call is allowed to add new nodes to the
|
|
|
|
// cache, which invalidates all its previously obtained iterators. So we
|
|
|
|
// first generate the node for the type and then add that node to the cache.
|
|
|
|
llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
|
|
|
|
return BaseTypeMetadataCache[Ty] = TypeNode;
|
2013-04-04 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 11:08:17 +00:00
|
|
|
llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
|
2017-11-27 09:39:29 +00:00
|
|
|
assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
|
|
|
|
|
2017-10-31 11:05:34 +00:00
|
|
|
if (Info.isMayAlias())
|
2017-11-27 09:39:29 +00:00
|
|
|
Info = TBAAAccessInfo(getChar(), Info.Size);
|
[CodeGen] Add initial support for union members in TBAA
The basic idea behind this patch is that since in strict aliasing
mode all accesses to union members require their outermost
enclosing union objects to be specified explicitly, then for a
couple given accesses to union members of the form
p->a.b.c...
q->x.y.z...
it is known they can only alias if both p and q point to the same
union type and offset ranges of members a.b.c... and x.y.z...
overlap. Note that the actual types of the members do not matter.
Specifically, in this patch we do the following:
* Make unions to be valid TBAA base access types. This enables
generation of TBAA type descriptors for unions.
* Encode union types as structures with a single member of a
special "union member" type. Currently we do not encode
information about sizes of types, but conceptually such union
members are considered to be of the size of the whole union.
* Encode accesses to direct and indirect union members, including
member arrays, as accesses to these special members. All
accesses to members of a union thus get the same offset, which
is the offset of the union they are part of. This means the
existing LLVM TBAA machinery is able to handle such accesses
with no changes.
While this is already an improvement comparing to the current
situation, that is, representing all union accesses as may-alias
ones, there are further changes planned to complete the support
for unions. One of them is storing information about access sizes
so we can distinct accesses to non-overlapping union members,
including accesses to different elements of member arrays.
Another change is encoding type sizes in order to make it
possible to compute offsets within constant-indexed array
elements. These enhancements will be addressed with separate
patches.
Differential Revision: https://reviews.llvm.org/D39455
llvm-svn: 319413
2017-11-30 09:26:39 +00:00
|
|
|
else if (Info.isUnionMember())
|
|
|
|
Info.AccessType = getUnionMemberType(Info.Size);
|
2017-10-31 11:05:34 +00:00
|
|
|
|
2017-10-03 10:52:39 +00:00
|
|
|
if (!Info.AccessType)
|
2014-05-21 05:09:00 +00:00
|
|
|
return nullptr;
|
2013-10-08 00:08:49 +00:00
|
|
|
|
2013-04-04 21:53:22 +00:00
|
|
|
if (!CodeGenOpts.StructPathTBAA)
|
2017-11-27 09:39:29 +00:00
|
|
|
Info = TBAAAccessInfo(Info.AccessType, Info.Size);
|
2013-04-04 21:53:22 +00:00
|
|
|
|
2017-10-06 08:17:48 +00:00
|
|
|
llvm::MDNode *&N = AccessTagMetadataCache[Info];
|
|
|
|
if (N)
|
2013-04-04 21:53:22 +00:00
|
|
|
return N;
|
|
|
|
|
2017-10-06 08:17:48 +00:00
|
|
|
if (!Info.BaseType) {
|
|
|
|
Info.BaseType = Info.AccessType;
|
|
|
|
assert(!Info.Offset && "Nonzero offset for an access with no base type!");
|
|
|
|
}
|
|
|
|
return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
|
|
|
|
Info.Offset);
|
2017-10-05 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 09:12:13 +00:00
|
|
|
TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
|
|
|
|
TBAAAccessInfo TargetInfo) {
|
2017-10-31 11:05:34 +00:00
|
|
|
if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
|
|
|
|
return TBAAAccessInfo::getMayAliasInfo();
|
2017-10-17 09:12:13 +00:00
|
|
|
return TargetInfo;
|
|
|
|
}
|
2017-10-31 11:05:34 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// TODO: Implement the rest of the logic here. For example, two accesses
|
|
|
|
// with same final access types result in an access to an object of that final
|
|
|
|
// access type regardless of their base types.
|
|
|
|
return TBAAAccessInfo::getMayAliasInfo();
|
|
|
|
}
|