From 383890bad473c1df16a4397aa669b801712b13d8 Mon Sep 17 00:00:00 2001 From: "Ivan A. Kosarev" Date: Fri, 6 Oct 2017 08:17:48 +0000 Subject: Refine generation of TBAA information in clang This patch is an attempt to clarify and simplify generation and propagation of TBAA information. The idea is to pack all values that describe a memory access, namely, base type, access type and offset, into a single structure. This is supposed to make further changes, such as adding support for unions and array members, easier to prepare and review. DecorateInstructionWithTBAA() is no more responsible for converting types to tags. These implicit conversions not only complicate reading the code, but also suggest assigning scalar access tags while we generally prefer full-size struct-path tags. TBAAPathTag is replaced with TBAAAccessInfo; the latter is now the type of the keys of the cache map that translates access descriptors to metadata nodes. Fixed a bug with writing to a wrong map in getTBAABaseTypeMetadata() (former getTBAAStructTypeInfo()). We now check for valid base access types every time we dereference a field. The original code only checks the top-level base type. See isValidBaseType() / isTBAAPathStruct() calls. Some entities have been renamed to sound more adequate and less confusing/misleading in presence of path-aware TBAA information. Now we do not lookup twice for the same cache entry in getAccessTagInfo(). Refined relevant comments and descriptions. Differential Revision: https://reviews.llvm.org/D37826 llvm-svn: 315048 --- clang/lib/CodeGen/CodeGenTBAA.cpp | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenTBAA.cpp') diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp index 3f0f942ea99..49a49c1f5d7 100644 --- a/clang/lib/CodeGen/CodeGenTBAA.cpp +++ b/clang/lib/CodeGen/CodeGenTBAA.cpp @@ -234,8 +234,6 @@ CodeGenTBAA::getTBAAStructInfo(QualType QTy) { /// Check if the given type is a valid base type to be used in access tags. static bool isValidBaseType(QualType QTy) { - if (QTy == QualType()) - return false; if (const RecordType *TTy = QTy->getAs()) { const RecordDecl *RD = TTy->getDecl()->getDefinition(); if (RD->hasFlexibleArrayMember()) @@ -249,10 +247,11 @@ static bool isValidBaseType(QualType QTy) { } llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { - const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); - assert(isValidBaseType(QTy)); + if (!isValidBaseType(QTy)) + return nullptr; - if (llvm::MDNode *N = StructTypeMetadataCache[Ty]) + const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); + if (llvm::MDNode *N = BaseTypeMetadataCache[Ty]) return N; if (const RecordType *TTy = QTy->getAs()) { @@ -267,7 +266,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { llvm::MDNode *FieldNode = isValidBaseType(FieldQTy) ? getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy); if (!FieldNode) - return StructTypeMetadataCache[Ty] = nullptr; + return BaseTypeMetadataCache[Ty] = nullptr; Fields.push_back(std::make_pair( FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth())); } @@ -281,11 +280,11 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { OutName = RD->getName(); } // Create the struct type node with a vector of pairs (offset, type). - return StructTypeMetadataCache[Ty] = + return BaseTypeMetadataCache[Ty] = MDHelper.createTBAAStructTypeNode(OutName, Fields); } - return StructMetadataCache[Ty] = nullptr; + return BaseTypeMetadataCache[Ty] = nullptr; } llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { @@ -295,23 +294,16 @@ llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { if (!CodeGenOpts.StructPathTBAA) Info = TBAAAccessInfo(Info.AccessType); - const Type *BTy = nullptr; - if (Info.BaseType != QualType()) - BTy = Context.getCanonicalType(Info.BaseType).getTypePtr(); - TBAAPathTag PathTag = TBAAPathTag(BTy, Info.AccessType, Info.Offset); - if (llvm::MDNode *N = StructTagMetadataCache[PathTag]) + llvm::MDNode *&N = AccessTagMetadataCache[Info]; + if (N) return N; - llvm::MDNode *BNode = nullptr; - if (isValidBaseType(Info.BaseType)) - BNode = getBaseTypeInfo(Info.BaseType); - if (!BNode) - return StructTagMetadataCache[PathTag] = - MDHelper.createTBAAStructTagNode(Info.AccessType, Info.AccessType, - /* Offset= */ 0); - - return StructTagMetadataCache[PathTag] = - MDHelper.createTBAAStructTagNode(BNode, Info.AccessType, Info.Offset); + 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); } TBAAAccessInfo CodeGenTBAA::getMayAliasAccessInfo() { -- cgit v1.2.3