Index: lib/CodeGen/CGAtomic.cpp =================================================================== --- lib/CodeGen/CGAtomic.cpp +++ lib/CodeGen/CGAtomic.cpp @@ -1425,8 +1425,8 @@ // Other decoration. if (IsVolatile) Load->setVolatile(true); - if (LVal.getTBAAAccessType()) - CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAAccessType()); + TBAAAccessInfo TBAAInfo(LVal.getTBAAAccessType()); + CGF.CGM.DecorateInstructionWithTBAA(Load, TBAAInfo); return Load; } @@ -1942,8 +1942,8 @@ // Other decoration. if (IsVolatile) store->setVolatile(true); - if (dest.getTBAAAccessType()) - CGM.DecorateInstructionWithTBAA(store, dest.getTBAAAccessType()); + TBAAAccessInfo TBAAInfo(dest.getTBAAAccessType()); + CGM.DecorateInstructionWithTBAA(store, TBAAInfo); return; } Index: lib/CodeGen/CGClass.cpp =================================================================== --- lib/CodeGen/CGClass.cpp +++ lib/CodeGen/CGClass.cpp @@ -2383,7 +2383,7 @@ VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); - CGM.DecorateInstructionWithTBAA(Store, CGM.getTBAAInfoForVTablePtr()); + CGM.DecorateInstructionWithTBAA(Store, CGM.getTBAAVTablePtrAccessInfo()); if (CGM.getCodeGenOpts().OptimizationLevel > 0 && CGM.getCodeGenOpts().StrictVTablePointers) CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass); @@ -2477,7 +2477,7 @@ const CXXRecordDecl *RD) { Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy); llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); - CGM.DecorateInstructionWithTBAA(VTable, CGM.getTBAAInfoForVTablePtr()); + CGM.DecorateInstructionWithTBAA(VTable, CGM.getTBAAVTablePtrAccessInfo()); if (CGM.getCodeGenOpts().OptimizationLevel > 0 && CGM.getCodeGenOpts().StrictVTablePointers) Index: lib/CodeGen/CGExpr.cpp =================================================================== --- lib/CodeGen/CGExpr.cpp +++ lib/CodeGen/CGExpr.cpp @@ -1526,12 +1526,9 @@ Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); } if (TBAAInfo.AccessType) { - bool MayAlias = BaseInfo.getMayAlias(); - llvm::MDNode *TBAA = MayAlias - ? CGM.getTBAAMayAliasTypeInfo() - : CGM.getTBAAStructTagInfo(TBAAInfo); - if (TBAA) - CGM.DecorateInstructionWithTBAA(Load, TBAA, MayAlias); + if (BaseInfo.getMayAlias()) + TBAAInfo = CGM.getTBAAMayAliasAccessInfo(); + CGM.DecorateInstructionWithTBAA(Load, TBAAInfo); } if (EmitScalarRangeCheck(Load, Ty, Loc)) { @@ -1614,12 +1611,9 @@ Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); } if (TBAAInfo.AccessType) { - bool MayAlias = BaseInfo.getMayAlias(); - llvm::MDNode *TBAA = MayAlias - ? CGM.getTBAAMayAliasTypeInfo() - : CGM.getTBAAStructTagInfo(TBAAInfo); - if (TBAA) - CGM.DecorateInstructionWithTBAA(Store, TBAA, MayAlias); + if (BaseInfo.getMayAlias()) + TBAAInfo = CGM.getTBAAMayAliasAccessInfo(); + CGM.DecorateInstructionWithTBAA(Store, TBAAInfo); } } @@ -3727,12 +3721,9 @@ // Loading the reference will disable path-aware TBAA. TBAAPath = false; - if (CGM.shouldUseTBAA()) { - llvm::MDNode *tbaa = mayAlias ? CGM.getTBAAMayAliasTypeInfo() : - CGM.getTBAATypeInfo(type); - if (tbaa) - CGM.DecorateInstructionWithTBAA(load, tbaa); - } + TBAAAccessInfo TBAAInfo = mayAlias ? CGM.getTBAAMayAliasAccessInfo() : + CGM.getTBAAAccessInfo(type); + CGM.DecorateInstructionWithTBAA(load, TBAAInfo); mayAlias = false; type = refType->getPointeeType(); @@ -3769,7 +3760,7 @@ // update offset to be relative to the base type. unsigned CharWidth = getContext().getCharWidth(); TBAAAccessInfo TBAAInfo = mayAlias ? - TBAAAccessInfo(CGM.getTBAAMayAliasTypeInfo()) : + CGM.getTBAAMayAliasAccessInfo() : TBAAAccessInfo(base.getTBAAInfo().BaseType, CGM.getTBAATypeInfo(type), base.getTBAAInfo().Offset + Layout.getFieldOffset( field->getFieldIndex()) / CharWidth); @@ -3784,7 +3775,7 @@ // FIXME: this should get propagated down through anonymous structs // and unions. if (mayAlias && LV.getTBAAAccessType()) - LV.setTBAAAccessType(CGM.getTBAAMayAliasTypeInfo()); + LV.setTBAAInfo(CGM.getTBAAMayAliasAccessInfo()); return LV; } Index: lib/CodeGen/CodeGenModule.h =================================================================== --- lib/CodeGen/CodeGenModule.h +++ lib/CodeGen/CodeGenModule.h @@ -656,28 +656,31 @@ /// the given type. llvm::MDNode *getTBAATypeInfo(QualType QTy); - llvm::MDNode *getTBAAInfoForVTablePtr(); + /// getTBAAAccessInfo - Get TBAA information that describes an accesses to + /// an object of the given type. + TBAAAccessInfo getTBAAAccessInfo(QualType AccessType); + + /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an + /// access to a virtual table pointer. + TBAAAccessInfo getTBAAVTablePtrAccessInfo(); + llvm::MDNode *getTBAAStructInfo(QualType QTy); - /// Get path-aware TBAA tag for a given memory access. - llvm::MDNode *getTBAAStructTagInfo(TBAAAccessInfo Info); + /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access. + llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info); - /// getTBAAMayAliasTypeInfo - Get TBAA information that represents + /// getTBAAMayAliasAccessInfo - Get TBAA information that represents /// may-alias accesses. - llvm::MDNode *getTBAAMayAliasTypeInfo(); + TBAAAccessInfo getTBAAMayAliasAccessInfo(); bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); bool isPaddedAtomicType(QualType type); bool isPaddedAtomicType(const AtomicType *type); - /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag - /// is the same as the type. For struct-path aware TBAA, the tag - /// is different from the type: base type, access type and offset. - /// When ConvertTypeToTag is true, we create a tag based on the scalar type. + /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag. void DecorateInstructionWithTBAA(llvm::Instruction *Inst, - llvm::MDNode *TBAAInfo, - bool ConvertTypeToTag = true); + TBAAAccessInfo TBAAInfo); /// Adds !invariant.barrier !tag to instruction void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -578,10 +578,14 @@ return TBAA->getTypeInfo(QTy); } -llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() { +TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) { + return TBAAAccessInfo(getTBAATypeInfo(AccessType)); +} + +TBAAAccessInfo CodeGenModule::getTBAAVTablePtrAccessInfo() { if (!TBAA) - return nullptr; - return TBAA->getTBAAInfoForVTablePtr(); + return TBAAAccessInfo(); + return TBAA->getVTablePtrAccessInfo(); } llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { @@ -590,30 +594,22 @@ return TBAA->getTBAAStructInfo(QTy); } -llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(TBAAAccessInfo Info) { +llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) { if (!TBAA) return nullptr; - return TBAA->getTBAAStructTagInfo(Info); + return TBAA->getAccessTagInfo(Info); } -llvm::MDNode *CodeGenModule::getTBAAMayAliasTypeInfo() { +TBAAAccessInfo CodeGenModule::getTBAAMayAliasAccessInfo() { if (!TBAA) - return nullptr; - return TBAA->getMayAliasTypeInfo(); + return TBAAAccessInfo(); + return TBAA->getMayAliasAccessInfo(); } -/// Decorate the instruction with a TBAA tag. For both scalar TBAA -/// and struct-path aware TBAA, the tag has the same format: -/// base type, access type and offset. -/// When ConvertTypeToTag is true, we create a tag based on the scalar type. void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, - llvm::MDNode *TBAAInfo, - bool ConvertTypeToTag) { - if (ConvertTypeToTag && TBAA) - Inst->setMetadata(llvm::LLVMContext::MD_tbaa, - TBAA->getTBAAScalarTagInfo(TBAAInfo)); - else - Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); + TBAAAccessInfo TBAAInfo) { + if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo)) + Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag); } void CodeGenModule::DecorateInstructionWithInvariantGroup( Index: lib/CodeGen/CodeGenTBAA.h =================================================================== --- lib/CodeGen/CodeGenTBAA.h +++ lib/CodeGen/CodeGenTBAA.h @@ -86,8 +86,6 @@ llvm::DenseMap StructTypeMetadataCache; /// This maps TBAAPathTags to a tag node. llvm::DenseMap StructTagMetadataCache; - /// This maps a scalar type to a scalar tag node. - llvm::DenseMap ScalarTagMetadataCache; /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing /// them for struct assignments. @@ -127,26 +125,23 @@ /// given type. llvm::MDNode *getTypeInfo(QualType QTy); - /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a - /// dereference of a vtable pointer. - llvm::MDNode *getTBAAInfoForVTablePtr(); + /// getVTablePtrAccessInfo - Get the TBAA information that describes an + /// access to a virtual table pointer. + TBAAAccessInfo getVTablePtrAccessInfo(); /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of /// the given type. llvm::MDNode *getTBAAStructInfo(QualType QTy); - /// Get the MDNode in the type DAG for given struct type QType. - llvm::MDNode *getTBAAStructTypeInfo(QualType QType); + /// getBaseTypeInfo - Get metadata node for a given base access type. + llvm::MDNode *getBaseTypeInfo(QualType QType); - /// Get path-aware TBAA tag for a given memory access. - llvm::MDNode *getTBAAStructTagInfo(TBAAAccessInfo Info); + /// getAccessTagInfo - Get TBAA tag for a given memory access. + llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info); - /// Get the scalar tag MDNode for a given scalar type. - llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode); - - /// getMayAliasTypeInfo - Get TBAA information that represents may-alias + /// getMayAliasAccessInfo - Get TBAA information that represents may-alias /// accesses. - llvm::MDNode *getMayAliasTypeInfo(); + TBAAAccessInfo getMayAliasAccessInfo(); }; } // end namespace CodeGen Index: lib/CodeGen/CodeGenTBAA.cpp =================================================================== --- lib/CodeGen/CodeGenTBAA.cpp +++ lib/CodeGen/CodeGenTBAA.cpp @@ -171,8 +171,8 @@ return MetadataCache[Ty] = getChar(); } -llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { - return createTBAAScalarType("vtable pointer", getRoot()); +TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo() { + return TBAAAccessInfo(createTBAAScalarType("vtable pointer", getRoot())); } bool @@ -211,8 +211,8 @@ /* Otherwise, treat whatever it is as a field. */ uint64_t Offset = BaseOffset; uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); - llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTypeInfo(QTy); - llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo); + llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy); + llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType)); Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); return true; } @@ -232,8 +232,10 @@ return StructMetadataCache[Ty] = nullptr; } -/// Check if the given type can be handled by path-aware TBAA. -static bool isTBAAPathStruct(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()) @@ -246,10 +248,9 @@ return false; } -llvm::MDNode * -CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) { +llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); - assert(isTBAAPathStruct(QTy)); + assert(isValidBaseType(QTy)); if (llvm::MDNode *N = StructTypeMetadataCache[Ty]) return N; @@ -263,11 +264,8 @@ for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); i != e; ++i, ++idx) { QualType FieldQTy = i->getType(); - llvm::MDNode *FieldNode; - if (isTBAAPathStruct(FieldQTy)) - FieldNode = getTBAAStructTypeInfo(FieldQTy); - else - FieldNode = getTypeInfo(FieldQTy); + llvm::MDNode *FieldNode = isValidBaseType(FieldQTy) ? + getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy); if (!FieldNode) return StructTypeMetadataCache[Ty] = nullptr; Fields.push_back(std::make_pair( @@ -290,21 +288,23 @@ return StructMetadataCache[Ty] = nullptr; } -llvm::MDNode *CodeGenTBAA::getTBAAStructTagInfo(TBAAAccessInfo Info) { +llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { if (!Info.AccessType) return nullptr; if (!CodeGenOpts.StructPathTBAA) - return getTBAAScalarTagInfo(Info.AccessType); + Info = TBAAAccessInfo(Info.AccessType); - const Type *BTy = Context.getCanonicalType(Info.BaseType).getTypePtr(); + 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]) return N; llvm::MDNode *BNode = nullptr; - if (isTBAAPathStruct(Info.BaseType)) - BNode = getTBAAStructTypeInfo(Info.BaseType); + if (isValidBaseType(Info.BaseType)) + BNode = getBaseTypeInfo(Info.BaseType); if (!BNode) return StructTagMetadataCache[PathTag] = MDHelper.createTBAAStructTagNode(Info.AccessType, Info.AccessType, @@ -314,17 +314,6 @@ MDHelper.createTBAAStructTagNode(BNode, Info.AccessType, Info.Offset); } -llvm::MDNode * -CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) { - if (!AccessNode) - return nullptr; - if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode]) - return N; - - return ScalarTagMetadataCache[AccessNode] = - MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); -} - -llvm::MDNode *CodeGenTBAA::getMayAliasTypeInfo() { - return getChar(); +TBAAAccessInfo CodeGenTBAA::getMayAliasAccessInfo() { + return TBAAAccessInfo(getChar()); }