Index: lib/CodeGen/CGAtomic.cpp =================================================================== --- lib/CodeGen/CGAtomic.cpp +++ lib/CodeGen/CGAtomic.cpp @@ -1425,8 +1425,7 @@ // Other decoration. if (IsVolatile) Load->setVolatile(true); - if (LVal.getTBAAInfo()) - CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAInfo()); + CGF.CGM.DecorateInstructionWithTBAA(Load, LVal.getTBAAInfo().AccessType); return Load; } @@ -1942,8 +1941,7 @@ // Other decoration. if (IsVolatile) store->setVolatile(true); - if (dest.getTBAAInfo()) - CGM.DecorateInstructionWithTBAA(store, dest.getTBAAInfo()); + CGM.DecorateInstructionWithTBAA(store, dest.getTBAAInfo().AccessType); return; } Index: lib/CodeGen/CGExpr.cpp =================================================================== --- lib/CodeGen/CGExpr.cpp +++ lib/CodeGen/CGExpr.cpp @@ -1361,10 +1361,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, SourceLocation Loc) { return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), - lvalue.getType(), Loc, lvalue.getBaseInfo(), - lvalue.getTBAAInfo(), - lvalue.getTBAABaseType(), lvalue.getTBAAOffset(), - lvalue.isNontemporal()); + lvalue.getType(), Loc, lvalue.getTBAAInfo(), + lvalue.getBaseInfo(), lvalue.isNontemporal()); } static bool hasBooleanRepresentation(QualType Ty) { @@ -1470,14 +1468,9 @@ return true; } -llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, - QualType Ty, - SourceLocation Loc, - LValueBaseInfo BaseInfo, - llvm::MDNode *TBAAInfo, - QualType TBAABaseType, - uint64_t TBAAOffset, - bool isNontemporal) { +llvm::Value *CodeGenFunction::EmitLoadOfScalar( + Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, + TBAAAccessInfo TBAAInfo, LValueBaseInfo BaseInfo, bool isNontemporal) { if (!CGM.getCodeGenOpts().PreserveVec3Type) { // For better performance, handle vector loads differently. if (Ty->isVectorType()) { @@ -1516,14 +1509,12 @@ Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1))); Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); } - if (TBAAInfo) { - bool MayAlias = BaseInfo.getMayAlias(); - llvm::MDNode *TBAA = MayAlias - ? CGM.getTBAAInfo(getContext().CharTy) - : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, TBAAOffset); - if (TBAA) - CGM.DecorateInstructionWithTBAA(Load, TBAA, MayAlias); - } + + bool MayAlias = BaseInfo.getMayAlias(); + llvm::MDNode *TBAA = MayAlias ? CGM.getTBAAInfo(getContext().CharTy) : + CGM.getTBAAStructTagInfo(TBAAInfo); + CGM.DecorateInstructionWithTBAA(Load, TBAA, + /* ConvertTypeToTag= */ MayAlias); if (EmitScalarRangeCheck(Load, Ty, Loc)) { // In order to prevent the optimizer from throwing away the check, don't @@ -1562,12 +1553,9 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, + TBAAAccessInfo TBAAInfo, LValueBaseInfo BaseInfo, - llvm::MDNode *TBAAInfo, - bool isInit, QualType TBAABaseType, - uint64_t TBAAOffset, - bool isNontemporal) { - + bool isInit, bool isNontemporal) { if (!CGM.getCodeGenOpts().PreserveVec3Type) { // Handle vectors differently to get better performance. if (Ty->isVectorType()) { @@ -1607,22 +1595,19 @@ llvm::ConstantAsMetadata::get(Builder.getInt32(1))); Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); } - if (TBAAInfo) { - bool MayAlias = BaseInfo.getMayAlias(); - llvm::MDNode *TBAA = MayAlias - ? CGM.getTBAAInfo(getContext().CharTy) - : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, TBAAOffset); - if (TBAA) - CGM.DecorateInstructionWithTBAA(Store, TBAA, MayAlias); - } + + bool MayAlias = BaseInfo.getMayAlias(); + llvm::MDNode *TBAA = MayAlias ? CGM.getTBAAInfo(getContext().CharTy) : + CGM.getTBAAStructTagInfo(TBAAInfo); + CGM.DecorateInstructionWithTBAA(Store, TBAA, + /* ConvertTypeToTag= */ MayAlias); } void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit) { EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), - lvalue.getType(), lvalue.getBaseInfo(), - lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(), - lvalue.getTBAAOffset(), lvalue.isNontemporal()); + lvalue.getType(), lvalue.getTBAAInfo(), + lvalue.getBaseInfo(), isInit, lvalue.isNontemporal()); } /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this @@ -3720,13 +3705,9 @@ // Loading the reference will disable path-aware TBAA. TBAAPath = false; if (CGM.shouldUseTBAA()) { - llvm::MDNode *tbaa; - if (mayAlias) - tbaa = CGM.getTBAAInfo(getContext().CharTy); - else - tbaa = CGM.getTBAAInfo(type); - if (tbaa) - CGM.DecorateInstructionWithTBAA(load, tbaa); + llvm::MDNode *tbaa = mayAlias ? CGM.getTBAAInfo(getContext().CharTy) : + CGM.getTBAAInfo(type); + CGM.DecorateInstructionWithTBAA(load, tbaa); } mayAlias = false; @@ -3762,10 +3743,13 @@ getContext().getASTRecordLayout(field->getParent()); // Set the base type to be the base type of the base LValue and // update offset to be relative to the base type. - LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType()); - LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() + + TBAAAccessInfo TBAAInfo( + mayAlias ? getContext().CharTy : base.getTBAAInfo().BaseType, + LV.getTBAAInfo().AccessType, + mayAlias ? 0 : base.getTBAAInfo().Offset + Layout.getFieldOffset(field->getFieldIndex()) / getContext().getCharWidth()); + LV.setTBAAInfo(TBAAInfo); } // __weak attribute on a field is ignored. @@ -3775,8 +3759,10 @@ // Fields of may_alias structs act like 'char' for TBAA purposes. // FIXME: this should get propagated down through anonymous structs // and unions. - if (mayAlias && LV.getTBAAInfo()) - LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy)); + if (mayAlias && LV.getTBAAInfo().AccessType) + LV.setTBAAInfo(TBAAAccessInfo(QualType(), + CGM.getTBAAInfo(getContext().CharTy), + /* Offset= */ 0)); return LV; } Index: lib/CodeGen/CGValue.h =================================================================== --- lib/CodeGen/CGValue.h +++ lib/CodeGen/CGValue.h @@ -20,6 +20,7 @@ #include "llvm/IR/Value.h" #include "llvm/IR/Type.h" #include "Address.h" +#include "CodeGenTBAA.h" namespace llvm { class Constant; @@ -227,18 +228,12 @@ Expr *BaseIvarExp; - /// Used by struct-path-aware TBAA. - QualType TBAABaseType; - /// Offset relative to the base type. - uint64_t TBAAOffset; - - /// TBAAInfo - TBAA information to attach to dereferences of this LValue. - llvm::MDNode *TBAAInfo; + /// TBAAInfo - TBAA information associated with this LValue. + TBAAAccessInfo TBAAInfo; private: - void Initialize(QualType Type, Qualifiers Quals, - CharUnits Alignment, LValueBaseInfo BaseInfo, - llvm::MDNode *TBAAInfo = nullptr) { + void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment, + LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) { assert((!Alignment.isZero() || Type->isIncompleteType()) && "initializing l-value with zero alignment!"); this->Type = Type; @@ -256,8 +251,6 @@ this->BaseIvarExp = nullptr; // Initialize fields for TBAA. - this->TBAABaseType = Type; - this->TBAAOffset = 0; this->TBAAInfo = TBAAInfo; } @@ -314,18 +307,12 @@ bool isVolatile() const { return Quals.hasVolatile(); } - + Expr *getBaseIvarExp() const { return BaseIvarExp; } void setBaseIvarExp(Expr *V) { BaseIvarExp = V; } - QualType getTBAABaseType() const { return TBAABaseType; } - void setTBAABaseType(QualType T) { TBAABaseType = T; } - - uint64_t getTBAAOffset() const { return TBAAOffset; } - void setTBAAOffset(uint64_t O) { TBAAOffset = O; } - - llvm::MDNode *getTBAAInfo() const { return TBAAInfo; } - void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; } + TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; } + void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; } const Qualifiers &getQuals() const { return Quals; } Qualifiers &getQuals() { return Quals; } @@ -384,9 +371,8 @@ llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; } static LValue MakeAddr(Address address, QualType type, - ASTContext &Context, - LValueBaseInfo BaseInfo, - llvm::MDNode *TBAAInfo = nullptr) { + ASTContext &Context, LValueBaseInfo BaseInfo, + TBAAAccessInfo TBAAInfo) { Qualifiers qs = type.getQualifiers(); qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); @@ -405,7 +391,7 @@ R.V = vecAddress.getPointer(); R.VectorIdx = Idx; R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(), - BaseInfo); + BaseInfo, TBAAAccessInfo(type)); return R; } @@ -416,7 +402,7 @@ R.V = vecAddress.getPointer(); R.VectorElts = Elts; R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(), - BaseInfo); + BaseInfo, TBAAAccessInfo(type)); return R; } @@ -434,7 +420,8 @@ R.LVType = BitField; R.V = Addr.getPointer(); R.BitFieldInfo = &Info; - R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo); + R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo, + TBAAAccessInfo(type)); return R; } @@ -443,7 +430,8 @@ R.LVType = GlobalReg; R.V = Reg.getPointer(); R.Initialize(type, type.getQualifiers(), Reg.getAlignment(), - LValueBaseInfo(AlignmentSource::Decl, false)); + LValueBaseInfo(AlignmentSource::Decl, false), + TBAAAccessInfo(type)); return R; } Index: lib/CodeGen/CodeGenFunction.h =================================================================== --- lib/CodeGen/CodeGenFunction.h +++ lib/CodeGen/CodeGenFunction.h @@ -1900,14 +1900,14 @@ LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type)) { return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, - CGM.getTBAAInfo(T)); + TBAAAccessInfo(T, CGM.getTBAAInfo(T))); } LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type)) { return LValue::MakeAddr(Address(V, Alignment), T, getContext(), - BaseInfo, CGM.getTBAAInfo(T)); + BaseInfo, TBAAAccessInfo(T, CGM.getTBAAInfo(T))); } LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T); @@ -3033,14 +3033,19 @@ /// EmitLoadOfScalar - Load a scalar value from an address, taking /// care to appropriately convert from the memory representation to /// the LLVM value representation. - llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, - SourceLocation Loc, - LValueBaseInfo BaseInfo = - LValueBaseInfo(AlignmentSource::Type), - llvm::MDNode *TBAAInfo = nullptr, - QualType TBAABaseTy = QualType(), - uint64_t TBAAOffset = 0, - bool isNontemporal = false); + llvm::Value *EmitLoadOfScalar( + Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, + TBAAAccessInfo TBAAInfo, + LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type), + bool isNontemporal = false); + + llvm::Value *EmitLoadOfScalar( + Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, + LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type), + bool isNontemporal = false) { + return EmitLoadOfScalar(Addr, Volatile, Ty, Loc, TBAAAccessInfo(Ty), + BaseInfo, isNontemporal); + } /// EmitLoadOfScalar - Load a scalar value from an address, taking /// care to appropriately convert from the memory representation to @@ -3051,13 +3056,19 @@ /// EmitStoreOfScalar - Store a scalar value to an address, taking /// care to appropriately convert from the memory representation to /// the LLVM value representation. - void EmitStoreOfScalar(llvm::Value *Value, Address Addr, - bool Volatile, QualType Ty, - LValueBaseInfo BaseInfo = - LValueBaseInfo(AlignmentSource::Type), - llvm::MDNode *TBAAInfo = nullptr, bool isInit = false, - QualType TBAABaseTy = QualType(), - uint64_t TBAAOffset = 0, bool isNontemporal = false); + void EmitStoreOfScalar( + llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, + TBAAAccessInfo TBAAInfo, + LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type), + bool isInit = false, bool isNontemporal = false); + + void EmitStoreOfScalar( + llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, + LValueBaseInfo BaseInfo = LValueBaseInfo(AlignmentSource::Type), + bool isInit = false, bool isNontemporal = false) { + EmitStoreOfScalar(Value, Addr, Volatile, Ty, TBAAAccessInfo(Ty), + BaseInfo, isInit, isNontemporal); + } /// EmitStoreOfScalar - Store a scalar value to an address, taking /// care to appropriately convert from the memory representation to Index: lib/CodeGen/CodeGenFunction.cpp =================================================================== --- lib/CodeGen/CodeGenFunction.cpp +++ lib/CodeGen/CodeGenFunction.cpp @@ -170,8 +170,8 @@ LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { LValueBaseInfo BaseInfo; CharUnits Alignment = getNaturalTypeAlignment(T, &BaseInfo); - return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo, - CGM.getTBAAInfo(T)); + return LValue::MakeAddr(Address(V, Alignment), T, getContext(), + BaseInfo, TBAAAccessInfo(T, CGM.getTBAAInfo(T))); } /// Given a value of type T* that may not be to a complete object, Index: lib/CodeGen/CodeGenModule.h =================================================================== --- lib/CodeGen/CodeGenModule.h +++ lib/CodeGen/CodeGenModule.h @@ -15,6 +15,7 @@ #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H #include "CGVTables.h" +#include "CodeGenTBAA.h" #include "CodeGenTypeCache.h" #include "CodeGenTypes.h" #include "SanitizerMetadata.h" @@ -653,8 +654,7 @@ llvm::MDNode *getTBAAInfoForVTablePtr(); llvm::MDNode *getTBAAStructInfo(QualType QTy); /// Return the path-aware tag for given base type, access node and offset. - llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN, - uint64_t O); + llvm::MDNode *getTBAAStructTagInfo(TBAAAccessInfo Info); bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); @@ -666,7 +666,7 @@ /// is different from the type: base type, access type and offset. /// When ConvertTypeToTag is true, we create a tag based on the scalar type. void DecorateInstructionWithTBAA(llvm::Instruction *Inst, - llvm::MDNode *TBAAInfo, + llvm::MDNode *AccessType, bool ConvertTypeToTag = true); /// Adds !invariant.barrier !tag to instruction Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -23,7 +23,6 @@ #include "CGOpenMPRuntimeNVPTX.h" #include "CodeGenFunction.h" #include "CodeGenPGO.h" -#include "CodeGenTBAA.h" #include "ConstantEmitter.h" #include "CoverageMappingGen.h" #include "TargetInfo.h" @@ -592,12 +591,10 @@ return TBAA->getTBAAStructInfo(QTy); } -llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy, - llvm::MDNode *AccessN, - uint64_t O) { +llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(TBAAAccessInfo Info) { if (!TBAA) return nullptr; - return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O); + return TBAA->getTBAAStructTagInfo(Info); } /// Decorate the instruction with a TBAA tag. For both scalar TBAA @@ -605,13 +602,16 @@ /// 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, + llvm::MDNode *AccessType, bool ConvertTypeToTag) { + if(!AccessType) + return; + if (ConvertTypeToTag && TBAA) Inst->setMetadata(llvm::LLVMContext::MD_tbaa, - TBAA->getTBAAScalarTagInfo(TBAAInfo)); + TBAA->getTBAAScalarTagInfo(AccessType)); else - Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); + Inst->setMetadata(llvm::LLVMContext::MD_tbaa, AccessType); } void CodeGenModule::DecorateInstructionWithInvariantGroup( Index: lib/CodeGen/CodeGenTBAA.h =================================================================== --- lib/CodeGen/CodeGenTBAA.h +++ lib/CodeGen/CodeGenTBAA.h @@ -40,6 +40,21 @@ uint64_t Offset; }; +struct TBAAAccessInfo { + TBAAAccessInfo(QualType BaseType, llvm::MDNode *AccessType = nullptr, + uint64_t Offset = 0) + : BaseType(BaseType), AccessType(AccessType), Offset(Offset) + {} + + TBAAAccessInfo() + : TBAAAccessInfo(/* BaseType= */ QualType()) + {} + + QualType BaseType; + llvm::MDNode *AccessType; + uint64_t Offset; +}; + /// CodeGenTBAA - This class organizes the cross-module state that is used /// while lowering AST types to LLVM types. class CodeGenTBAA { @@ -111,8 +126,7 @@ llvm::MDNode *getTBAAStructTypeInfo(QualType QType); /// Get the tag MDNode for a given base type, the actual scalar access MDNode /// and offset into the base type. - llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType, - llvm::MDNode *AccessNode, uint64_t Offset); + llvm::MDNode *getTBAAStructTagInfo(TBAAAccessInfo Info); /// Get the scalar tag MDNode for a given scalar type. llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode); Index: lib/CodeGen/CodeGenTBAA.cpp =================================================================== --- lib/CodeGen/CodeGenTBAA.cpp +++ lib/CodeGen/CodeGenTBAA.cpp @@ -88,8 +88,7 @@ return false; } -llvm::MDNode * -CodeGenTBAA::getTBAAInfo(QualType QTy) { +llvm::MDNode *CodeGenTBAA::getTBAAInfo(QualType QTy) { // At -O0 or relaxed aliasing, TBAA is not emitted for regular types. if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) return nullptr; @@ -134,8 +133,8 @@ // treating wchar_t, char16_t, and char32_t as distinct from their // "underlying types". default: - return MetadataCache[Ty] = - createTBAAScalarType(BTy->getName(Features), getChar()); + return MetadataCache[Ty] = createTBAAScalarType(BTy->getName(Features), + getChar()); } } @@ -149,8 +148,7 @@ // TODO: Implement C++'s type "similarity" and consider dis-"similar" // pointers distinct. if (Ty->isPointerType()) - return MetadataCache[Ty] = createTBAAScalarType("any pointer", - getChar()); + return MetadataCache[Ty] = createTBAAScalarType("any pointer", getChar()); // Enum types are distinct types. In C++ they have "underlying types", // however they aren't related for TBAA. @@ -292,29 +290,28 @@ } /// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA. -llvm::MDNode * -CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode, - uint64_t Offset) { - if (!AccessNode) +llvm::MDNode *CodeGenTBAA::getTBAAStructTagInfo(TBAAAccessInfo Info) { + if (!Info.AccessType) return nullptr; if (!CodeGenOpts.StructPathTBAA) - return getTBAAScalarTagInfo(AccessNode); + return getTBAAScalarTagInfo(Info.AccessType); - const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr(); - TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset); + const Type *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(BaseQTy)) - BNode = getTBAAStructTypeInfo(BaseQTy); + if (isTBAAPathStruct(Info.BaseType)) + BNode = getTBAAStructTypeInfo(Info.BaseType); if (!BNode) return StructTagMetadataCache[PathTag] = - MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); + MDHelper.createTBAAStructTagNode(Info.AccessType, Info.AccessType, + /* Offset= */ 0); return StructTagMetadataCache[PathTag] = - MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset); + MDHelper.createTBAAStructTagNode(BNode, Info.AccessType, Info.Offset); } llvm::MDNode *