diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -211,7 +211,7 @@ mutable SmallVector Types; mutable llvm::FoldingSet ExtQualNodes; mutable llvm::FoldingSet ComplexTypes; - mutable llvm::FoldingSet PointerTypes; + mutable llvm::FoldingSet PointerTypes{GeneralTypesLog2InitSize}; mutable llvm::FoldingSet AdjustedTypes; mutable llvm::FoldingSet BlockPointerTypes; mutable llvm::FoldingSet LValueReferenceTypes; @@ -243,9 +243,10 @@ SubstTemplateTypeParmPackTypes; mutable llvm::ContextualFoldingSet TemplateSpecializationTypes; - mutable llvm::FoldingSet ParenTypes; + mutable llvm::FoldingSet ParenTypes{GeneralTypesLog2InitSize}; mutable llvm::FoldingSet UsingTypes; - mutable llvm::FoldingSet ElaboratedTypes; + mutable llvm::FoldingSet ElaboratedTypes{ + GeneralTypesLog2InitSize}; mutable llvm::FoldingSet DependentNameTypes; mutable llvm::ContextualFoldingSet @@ -466,6 +467,10 @@ }; llvm::DenseMap ModuleInitializers; + static constexpr unsigned ConstantArrayTypesLog2InitSize = 8; + static constexpr unsigned GeneralTypesLog2InitSize = 9; + static constexpr unsigned FunctionProtoTypesLog2InitSize = 12; + ASTContext &this_() { return *this; } public: diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -973,7 +973,8 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, IdentifierTable &idents, SelectorTable &sels, Builtin::Context &builtins, TranslationUnitKind TUKind) - : ConstantArrayTypes(this_()), FunctionProtoTypes(this_()), + : ConstantArrayTypes(this_(), ConstantArrayTypesLog2InitSize), + FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize), TemplateSpecializationTypes(this_()), DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()), SubstTemplateTemplateParmPacks(this_()), diff --git a/clang/lib/CodeGen/CodeGenTypes.h b/clang/lib/CodeGen/CodeGenTypes.h --- a/clang/lib/CodeGen/CodeGenTypes.h +++ b/clang/lib/CodeGen/CodeGenTypes.h @@ -76,7 +76,7 @@ llvm::DenseMap RecordDeclTypes; /// Hold memoized CGFunctionInfo results. - llvm::FoldingSet FunctionInfos; + llvm::FoldingSet FunctionInfos{FunctionInfosLog2InitSize}; /// This set keeps track of records that we're currently converting /// to an IR type. For example, when converting: @@ -98,6 +98,7 @@ llvm::SmallSet RecordsWithOpaqueMemberPointers; + static constexpr unsigned FunctionInfosLog2InitSize = 9; /// Helper for ConvertType. llvm::Type *ConvertFunctionTypeInternal(QualType FT); diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h --- a/llvm/include/llvm/ADT/FoldingSet.h +++ b/llvm/include/llvm/ADT/FoldingSet.h @@ -15,6 +15,7 @@ #ifndef LLVM_ADT_FOLDINGSET_H #define LLVM_ADT_FOLDINGSET_H +#include "llvm/ADT/Hashing.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/iterator.h" #include "llvm/Support/Allocator.h" @@ -292,7 +293,9 @@ /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, /// used to lookup the node in the FoldingSetBase. - unsigned ComputeHash() const; + unsigned ComputeHash() const { + return static_cast(hash_combine_range(Data, Data + Size)); + } bool operator==(FoldingSetNodeIDRef) const; @@ -322,13 +325,33 @@ : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {} /// Add* - Add various data types to Bit data. - void AddPointer(const void *Ptr); - void AddInteger(signed I); - void AddInteger(unsigned I); - void AddInteger(long I); - void AddInteger(unsigned long I); - void AddInteger(long long I); - void AddInteger(unsigned long long I); + void AddPointer(const void *Ptr) { + // Note: this adds pointers to the hash using sizes and endianness that + // depend on the host. It doesn't matter, however, because hashing on + // pointer values is inherently unstable. Nothing should depend on the + // ordering of nodes in the folding set. + static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long), + "unexpected pointer size"); + AddInteger(reinterpret_cast(Ptr)); + } + void AddInteger(signed I) { Bits.push_back(I); } + void AddInteger(unsigned I) { Bits.push_back(I); } + void AddInteger(long I) { AddInteger((unsigned long)I); } + void AddInteger(unsigned long I) { + if (sizeof(long) == sizeof(int)) + AddInteger(unsigned(I)); + else if (sizeof(long) == sizeof(long long)) { + AddInteger((unsigned long long)I); + } else { + llvm_unreachable("unexpected sizeof(long)"); + } + } + void AddInteger(long long I) { AddInteger((unsigned long long)I); } + void AddInteger(unsigned long long I) { + AddInteger(unsigned(I)); + AddInteger(unsigned(I >> 32)); + } + void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); } void AddString(StringRef String); void AddNodeID(const FoldingSetNodeID &ID); @@ -342,7 +365,9 @@ /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used /// to lookup the node in the FoldingSetBase. - unsigned ComputeHash() const; + unsigned ComputeHash() const { + return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash(); + } /// operator== - Used to compare two nodes to each other. bool operator==(const FoldingSetNodeID &RHS) const; diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp --- a/llvm/lib/Support/FoldingSet.cpp +++ b/llvm/lib/Support/FoldingSet.cpp @@ -12,7 +12,6 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/FoldingSet.h" -#include "llvm/ADT/Hashing.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/ErrorHandling.h" @@ -25,12 +24,6 @@ //===----------------------------------------------------------------------===// // FoldingSetNodeIDRef Implementation -/// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, -/// used to lookup the node in the FoldingSetBase. -unsigned FoldingSetNodeIDRef::ComputeHash() const { - return static_cast(hash_combine_range(Data, Data+Size)); -} - bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const { if (Size != RHS.Size) return false; return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0; @@ -49,41 +42,6 @@ /// Add* - Add various data types to Bit data. /// -void FoldingSetNodeID::AddPointer(const void *Ptr) { - // Note: this adds pointers to the hash using sizes and endianness that - // depend on the host. It doesn't matter, however, because hashing on - // pointer values is inherently unstable. Nothing should depend on the - // ordering of nodes in the folding set. - static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long), - "unexpected pointer size"); - AddInteger(reinterpret_cast(Ptr)); -} -void FoldingSetNodeID::AddInteger(signed I) { - Bits.push_back(I); -} -void FoldingSetNodeID::AddInteger(unsigned I) { - Bits.push_back(I); -} -void FoldingSetNodeID::AddInteger(long I) { - AddInteger((unsigned long)I); -} -void FoldingSetNodeID::AddInteger(unsigned long I) { - if (sizeof(long) == sizeof(int)) - AddInteger(unsigned(I)); - else if (sizeof(long) == sizeof(long long)) { - AddInteger((unsigned long long)I); - } else { - llvm_unreachable("unexpected sizeof(long)"); - } -} -void FoldingSetNodeID::AddInteger(long long I) { - AddInteger((unsigned long long)I); -} -void FoldingSetNodeID::AddInteger(unsigned long long I) { - AddInteger(unsigned(I)); - AddInteger(unsigned(I >> 32)); -} - void FoldingSetNodeID::AddString(StringRef String) { unsigned Size = String.size(); @@ -145,12 +103,6 @@ Bits.append(ID.Bits.begin(), ID.Bits.end()); } -/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to -/// lookup the node in the FoldingSetBase. -unsigned FoldingSetNodeID::ComputeHash() const { - return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash(); -} - /// operator== - Used to compare two nodes to each other. /// bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const {