llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Krystian Stasiowski (sdkrystian) <details> <summary>Changes</summary> `TemplateTypeParmType` currently stores the depth, index, and whether a template type parameter is a pack in a union of `CanonicalTTPTInfo` and `TemplateTypeParmDecl*`, and only the canonical type stores the position information. These bits can be stored for all `TemplateTypeParmTypes` in `TypeBits` to avoid unnecessary indirection when accessing the position information. --- Full diff: https://github.com/llvm/llvm-project/pull/102481.diff 2 Files Affected: - (modified) clang/include/clang/AST/Type.h (+34-38) - (modified) clang/lib/AST/ASTContext.cpp (+3-3) ``````````diff diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index 0c886526c61cef..03832cf56dd06e 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2134,6 +2134,23 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase { unsigned hasTypeDifferentFromDecl : 1; }; + class TemplateTypeParmTypeBitfields { + friend class TemplateTypeParmType; + + LLVM_PREFERRED_TYPE(TypeBitfields) + unsigned : NumTypeBits; + + /// The depth of the template parameter. + unsigned Depth : 15; + + /// Whether this is a template parameter pack. + LLVM_PREFERRED_TYPE(bool) + unsigned ParameterPack : 1; + + /// The index of the template parameter. + unsigned Index : 16; + }; + class SubstTemplateTypeParmTypeBitfields { friend class SubstTemplateTypeParmType; @@ -2257,6 +2274,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase { TypeWithKeywordBitfields TypeWithKeywordBits; ElaboratedTypeBitfields ElaboratedTypeBits; VectorTypeBitfields VectorTypeBits; + TemplateTypeParmTypeBitfields TemplateTypeParmTypeBits; SubstTemplateTypeParmTypeBitfields SubstTemplateTypeParmTypeBits; SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; @@ -6131,52 +6149,30 @@ class BTFTagAttributedType : public Type, public llvm::FoldingSetNode { class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { friend class ASTContext; // ASTContext creates these - // Helper data collector for canonical types. - struct CanonicalTTPTInfo { - unsigned Depth : 15; - unsigned ParameterPack : 1; - unsigned Index : 16; - }; - - union { - // Info for the canonical type. - CanonicalTTPTInfo CanTTPTInfo; - - // Info for the non-canonical type. - TemplateTypeParmDecl *TTPDecl; - }; + // The associated TemplateTypeParmDecl for the non-canonical type. + TemplateTypeParmDecl *TTPDecl; - /// Build a non-canonical type. - TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) + TemplateTypeParmType(unsigned D, unsigned I, bool PP, + TemplateTypeParmDecl *TTPDecl, QualType Canon) : Type(TemplateTypeParm, Canon, TypeDependence::DependentInstantiation | - (Canon->getDependence() & TypeDependence::UnexpandedPack)), - TTPDecl(TTPDecl) {} - - /// Build the canonical type. - TemplateTypeParmType(unsigned D, unsigned I, bool PP) - : Type(TemplateTypeParm, QualType(this, 0), - TypeDependence::DependentInstantiation | - (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { - CanTTPTInfo.Depth = D; - CanTTPTInfo.Index = I; - CanTTPTInfo.ParameterPack = PP; - } - - const CanonicalTTPTInfo& getCanTTPTInfo() const { - QualType Can = getCanonicalTypeInternal(); - return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; + (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)), + TTPDecl(TTPDecl) { + assert(!TTPDecl == Canon.isNull()); + TemplateTypeParmTypeBits.Depth = D; + TemplateTypeParmTypeBits.Index = I; + TemplateTypeParmTypeBits.ParameterPack = PP; } public: - unsigned getDepth() const { return getCanTTPTInfo().Depth; } - unsigned getIndex() const { return getCanTTPTInfo().Index; } - bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } - - TemplateTypeParmDecl *getDecl() const { - return isCanonicalUnqualified() ? nullptr : TTPDecl; + unsigned getDepth() const { return TemplateTypeParmTypeBits.Depth; } + unsigned getIndex() const { return TemplateTypeParmTypeBits.Index; } + bool isParameterPack() const { + return TemplateTypeParmTypeBits.ParameterPack; } + TemplateTypeParmDecl *getDecl() const { return TTPDecl; } + IdentifierInfo *getIdentifier() const; bool isSugared() const { return false; } diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 995d01734eea0d..c55d28b506887e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -5297,15 +5297,15 @@ QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, if (TTPDecl) { QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); TypeParm = new (*this, alignof(TemplateTypeParmType)) - TemplateTypeParmType(TTPDecl, Canon); + TemplateTypeParmType(Depth, Index, ParameterPack, TTPDecl, Canon); TemplateTypeParmType *TypeCheck = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); assert(!TypeCheck && "Template type parameter canonical type broken"); (void)TypeCheck; } else - TypeParm = new (*this, alignof(TemplateTypeParmType)) - TemplateTypeParmType(Depth, Index, ParameterPack); + TypeParm = new (*this, alignof(TemplateTypeParmType)) TemplateTypeParmType( + Depth, Index, ParameterPack, /*TTPDecl=*/nullptr, /*Canon=*/QualType()); Types.push_back(TypeParm); TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); `````````` </details> https://github.com/llvm/llvm-project/pull/102481 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits