27#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/SmallString.h"
36 : mlirContext(mlirContext), astContext(astContext), cxxABI(cxxABI),
37 codeGenOpts(codeGenOpts), features(features),
39 astContext.getDiagnostics())) {}
44CIRGenTBAA::createScalarTypeNode(StringRef name, cir::TBAAScalarAttr parent) {
47 return cir::TBAAScalarAttr::get(&mlirContext, name, parent);
50cir::TBAAOmnipotentCharAttr CIRGenTBAA::getChar() {
51 return cir::TBAAOmnipotentCharAttr::get(&mlirContext);
54cir::TBAAScalarAttr CIRGenTBAA::getAnyPtr(
unsigned ptrDepth) {
55 assert(ptrDepth >= 1 &&
"Pointer must have some depth");
60 if (anyPtrs.size() < ptrDepth) {
61 anyPtrs.reserve(ptrDepth);
63 anyPtrs.push_back(createScalarTypeNode(
"any pointer", {}));
64 for (
size_t idx = anyPtrs.size(); idx < ptrDepth; ++idx) {
65 std::string
name = (
"any p" + llvm::Twine(idx + 1) +
" pointer").str();
66 anyPtrs.push_back(createScalarTypeNode(name, anyPtrs[idx - 1]));
70 return anyPtrs[ptrDepth - 1];
75 if (
auto scalar = mlir::dyn_cast<cir::TBAAScalarAttr>(typeDesc))
76 return scalar.getId();
77 assert(mlir::isa<cir::TBAAOmnipotentCharAttr>(typeDesc) &&
78 "expected a scalar type descriptor");
79 return "omnipotent char";
82cir::TBAATypeDescriptorAttr
83CIRGenTBAA::getTypeInfoHelper(
const clang::Type *ty) {
85 if (
const auto *bty = dyn_cast<BuiltinType>(ty)) {
86 switch (bty->getKind()) {
92 case BuiltinType::Char_U:
93 case BuiltinType::Char_S:
94 case BuiltinType::UChar:
95 case BuiltinType::SChar:
110 return createScalarTypeNode(bty->getName(features), {});
123 if (!codeGenOpts.PointerTBAA)
126 unsigned ptrDepth = 0;
137 return getAnyPtr(ptrDepth);
143 SmallString<256> tyName;
152 return getAnyPtr(ptrDepth);
159 if (!rt->getDecl()->getDeclName())
160 return getAnyPtr(ptrDepth);
163 llvm::raw_svector_ostream tyOut(tyName);
164 mangleCtx->mangleCanonicalTypeName(QualType(ty, 0), tyOut);
167 SmallString<256> outName(
"p");
168 outName += std::to_string(ptrDepth);
171 return createScalarTypeNode(outName, getAnyPtr(ptrDepth));
175 if (
const auto *mty = dyn_cast<MatrixType>(ty)) {
177 "only ConstantMatrixType should reach CodeGen");
182 if (
const auto *cty = dyn_cast<ComplexType>(ty))
187 if (
const auto *ety = dyn_cast<EnumType>(ty)) {
188 const EnumDecl *ed = ety->getDecl()->getDefinitionOrSelf();
189 if (!features.CPlusPlus)
197 SmallString<256> outName;
198 llvm::raw_svector_ostream
out(outName);
199 cxxABI.getMangleContext().mangleCanonicalTypeName(QualType(ety, 0),
out);
200 return createScalarTypeNode(outName, {});
203 if (
const auto *eit = dyn_cast<BitIntType>(ty)) {
204 SmallString<256> outName;
205 llvm::raw_svector_ostream
out(outName);
208 out <<
"_BitInt(" << eit->getNumBits() <<
')';
209 return createScalarTypeNode(outName, {});
228 return getValidBaseTypeInfo(qty);
230 const clang::Type *ty = astContext.getCanonicalType(qty).getTypePtr();
237 return metadataCache[ty] = typeDesc;
249 uint64_t size = astContext.getTypeSizeInChars(accessType).getQuantity();
254CIRGenTBAA::getBaseTypeInfoHelper(
const clang::Type *ty) {
255 const auto *rt = dyn_cast<RecordType>(ty);
262 if (
const auto *cxxrd = dyn_cast<CXXRecordDecl>(rd)) {
266 if (base.isVirtual())
274 ? getValidBaseTypeInfo(baseQTy)
280 cir::TBAAMemberAttr::get(&mlirContext, typeDesc, offset));
286 llvm::sort(fields, [](cir::TBAAMemberAttr a, cir::TBAAMemberAttr b) {
287 return a.getOffset() < b.getOffset();
291 for (FieldDecl *field : rd->
fields()) {
292 if (field->isZeroSize(astContext) || field->isUnnamedBitField())
294 QualType fieldQTy = field->getType();
295 cir::TBAATypeDescriptorAttr typeDesc =
297 ? getValidBaseTypeInfo(fieldQTy)
303 uint64_t offset = astContext.toCharUnitsFromBits(bitOffset).getQuantity();
304 fields.push_back(cir::TBAAMemberAttr::get(&mlirContext, typeDesc, offset));
307 SmallString<256> outName;
308 if (features.CPlusPlus) {
310 llvm::raw_svector_ostream
out(outName);
311 cxxABI.getMangleContext().mangleCanonicalTypeName(QualType(ty, 0),
out);
317 return cir::TBAAStructAttr::get(&mlirContext, outName, fields);
320cir::TBAATypeDescriptorAttr CIRGenTBAA::getValidBaseTypeInfo(QualType qty) {
323 const clang::Type *ty = astContext.getCanonicalType(qty).getTypePtr();
327 auto it = baseTypeMetadataCache.find(ty);
328 if (it != baseTypeMetadataCache.end())
333 cir::TBAATypeDescriptorAttr typeDesc = getBaseTypeInfoHelper(ty);
334 [[maybe_unused]]
auto inserted = baseTypeMetadataCache.insert({ty, typeDesc});
335 assert(inserted.second &&
"BaseType metadata was already inserted");
346 assert(!info.isIncomplete() &&
"Access to an object of an incomplete type!");
348 if (info.isMayAlias())
352 if (!info.accessType || mlir::isa<cir::TBAAStructAttr>(info.accessType))
355 if (!codeGenOpts.StructPathTBAA)
358 if (!info.baseType) {
359 info.baseType = info.accessType;
360 assert(!info.offset &&
"Nonzero offset for an access with no base type!");
364 return cir::TBAATagAttr::get(&mlirContext, info.baseType, info.accessType,
381 if (!infoA || !infoB)
Defines the clang::ASTContext interface.
static StringRef getTypeDescriptorName(cir::TBAATypeDescriptorAttr typeDesc)
Returns the name of a scalar type descriptor.
Base class of the TBAA type descriptor attributes.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Implements C++ ABI-specific code generation functions.
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo infoA, TBAAAccessInfo infoB)
Returns the merged TBAA information for the purpose of conditional operator.
cir::TBAATypeDescriptorAttr getTypeInfo(clang::QualType qty)
Returns the type descriptor used to describe accesses to objects of the given type.
cir::TBAATagAttr getAccessTagInfo(TBAAAccessInfo info)
Returns the access tag for the given memory access, or null if the access has no TBAA information.
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo sourceInfo, TBAAAccessInfo targetInfo)
Returns the merged TBAA information for the purpose of type casts.
TBAAAccessInfo getAccessInfo(clang::QualType accessType)
Returns the TBAA information that describes an access to an object of the given type.
CIRGenTBAA(mlir::MLIRContext &mlirContext, clang::ASTContext &astContext, CIRGenCXXABI &cxxABI, const clang::CodeGenOptions &codeGenOpts, const clang::LangOptions &features)
cir::TBAATypeDescriptorAttr getBaseTypeInfo(clang::QualType qty)
Returns the type descriptor that describes the given base access type, or null if the type is not sui...
Represents a base class of a C++ class.
Represents a C++ struct/union/class.
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
QualType getIntegerType() const
Return the integer type this enum decl corresponds to.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
bool isExternallyVisible() const
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Represents a struct/union/class.
field_range fields() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
The base class of the type hierarchy.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isPointerType() const
bool isReferenceType() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
bool isStdByteType() const
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Defines the clang::TargetInfo interface.
bool typeHasMayAlias(QualType QTy)
Returns true if accesses to objects of type QTy are in the general char alias class because of a may_...
QualType getTBAASignedCounterpart(const ASTContext &Ctx, const BuiltinType *BTy)
Returns the signed type whose TBAA type node an access to the unsigned builtin type BTy uses,...
bool isValidTBAABaseType(QualType QTy)
Returns true if QTy is a valid base type to be used in TBAA access tags.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
static bool tbaaNewStructPath()
Describes a memory access in terms of TBAA.
static TBAAAccessInfo getIncompleteInfo()
static TBAAAccessInfo getMayAliasInfo()