[AST] Remove unnecessary indirections in DeclarationNameTable
authorBruno Ricci <riccibrun@gmail.com>
Mon, 6 Aug 2018 16:47:31 +0000 (16:47 +0000)
committerBruno Ricci <riccibrun@gmail.com>
Mon, 6 Aug 2018 16:47:31 +0000 (16:47 +0000)
DeclarationNameTable currently hold 3 "void *" to
FoldingSet<CXXSpecialName>, FoldingSet<CXXLiteralOperatorIdName>
and FoldingSet<CXXDeductionGuideNameExtra>.

CXXSpecialName, CXXLiteralOperatorIdName and
CXXDeductionGuideNameExtra are private classes holding extra
information about a "special" declaration name and are in
AST/DeclarationName.cpp. The original intent seems to have
been to keep these classes private and only expose
DeclarationNameExtra and DeclarationName (the code dates from
2008 and has not been significantly changed since).

However this make the code less straightforward than necessary
because of the need to have "void *" in DeclarationNameTable
(with 1 of 3 comments wrong) and to manually allocate/deallocate
the FoldingSets.

Moreover removing the extra indirections reduce the run-time of
an fsyntax-only on all of Boost by 2.3% which is not totally
unexpected given how frequently this data structure is used
(especially for C++).

A concern raised by erichkeane during the review was that
including Type.h would increase the compile time unreasonably.
However test builds (both clean and incremental) showed that
this patch did not result in any compile time increase.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D50261

llvm-svn: 339030

clang/include/clang/AST/DeclarationName.h
clang/lib/AST/DeclarationName.cpp

index 856f3ab5720e3b59c4e5a69d1c0be8cacb0030bc..766017b84df09d51662e5020b968c69bc7c18a5f 100644 (file)
 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
 #define LLVM_CLANG_AST_DECLARATIONNAME_H
 
+#include "clang/AST/Type.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/PartialDiagnostic.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/FoldingSet.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/type_traits.h"
 #include <cassert>
@@ -39,9 +41,7 @@ class IdentifierInfo;
 class MultiKeywordSelector;
 enum OverloadedOperatorKind : int;
 struct PrintingPolicy;
-class QualType;
 class TemplateDecl;
-class Type;
 class TypeSourceInfo;
 class UsingDirectiveDecl;
 
@@ -343,33 +343,113 @@ inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
   return DeclarationName::compare(LHS, RHS) >= 0;
 }
 
+/// CXXSpecialName - Records the type associated with one of the
+/// "special" kinds of declaration names in C++, e.g., constructors,
+/// destructors, and conversion functions.
+class CXXSpecialName : public DeclarationNameExtra,
+                       public llvm::FoldingSetNode {
+public:
+  /// Type - The type associated with this declaration name.
+  QualType Type;
+
+  /// FETokenInfo - Extra information associated with this declaration
+  /// name that can be used by the front end. All bits are really needed
+  /// so it is not possible to stash something in the low order bits.
+  void *FETokenInfo;
+
+  void Profile(llvm::FoldingSetNodeID &ID) {
+    ID.AddInteger(ExtraKindOrNumArgs);
+    ID.AddPointer(Type.getAsOpaquePtr());
+  }
+};
+
+/// Contains extra information for the name of a C++ deduction guide.
+class CXXDeductionGuideNameExtra : public DeclarationNameExtra,
+                                   public llvm::FoldingSetNode {
+public:
+  /// The template named by the deduction guide.
+  TemplateDecl *Template;
+
+  /// FETokenInfo - Extra information associated with this operator
+  /// name that can be used by the front end. All bits are really needed
+  /// so it is not possible to stash something in the low order bits.
+  void *FETokenInfo;
+
+  void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
+};
+
+/// CXXOperatorIdName - Contains extra information for the name of an
+/// overloaded operator in C++, such as "operator+.
+class CXXOperatorIdName : public DeclarationNameExtra {
+public:
+  /// FETokenInfo - Extra information associated with this operator
+  /// name that can be used by the front end. All bits are really needed
+  /// so it is not possible to stash something in the low order bits.
+  void *FETokenInfo;
+};
+
+/// CXXLiteralOperatorName - Contains the actual identifier that makes up the
+/// name.
+///
+/// This identifier is stored here rather than directly in DeclarationName so as
+/// to allow Objective-C selectors, which are about a million times more common,
+/// to consume minimal memory.
+class CXXLiteralOperatorIdName : public DeclarationNameExtra,
+                                 public llvm::FoldingSetNode {
+public:
+  IdentifierInfo *ID;
+
+  /// FETokenInfo - Extra information associated with this operator
+  /// name that can be used by the front end. All bits are really needed
+  /// so it is not possible to stash something in the low order bits.
+  void *FETokenInfo;
+
+  void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
+};
+
 /// DeclarationNameTable - Used to store and retrieve DeclarationName
 /// instances for the various kinds of declaration names, e.g., normal
 /// identifiers, C++ constructor names, etc. This class contains
 /// uniqued versions of each of the C++ special names, which can be
-/// retrieved using its member functions (e.g.,
-/// getCXXConstructorName).
+/// retrieved using its member functions (e.g., getCXXConstructorName).
 class DeclarationNameTable {
+  /// Used to allocate elements in the FoldingSets and
+  /// in the array of CXXOperatorIdName below.
   const ASTContext &Ctx;
 
-  // Actually a FoldingSet<CXXSpecialName> *
-  void *CXXSpecialNamesImpl;
-
-  // Operator names
+  /// Manage the uniqued CXXSpecialName, which contain extra information
+  /// for the "special" kinds of declaration names in C++ such as constructors,
+  /// destructors and conversion functions. getCXXConstructorName,
+  /// getCXXDestructorName, getCXXConversionFunctionName, and getCXXSpecialName
+  /// can be used to obtain a DeclarationName from the corresponding type.
+  llvm::FoldingSet<CXXSpecialName> CXXSpecialNames;
+
+  /// Manage the uniqued CXXOperatorIdName, which contain extra information
+  /// for the name of overloaded C++ operators. getCXXOperatorName
+  /// can be used to obtain a DeclarationName from the operator kind.
+  /// This points to the first element of an array of NUM_OVERLOADED_OPERATORS
+  /// CXXOperatorIdName which is constructed by DeclarationNameTable.
   CXXOperatorIdName *CXXOperatorNames;
 
-  // Actually a CXXOperatorIdName*
-  void *CXXLiteralOperatorNames;
+  /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
+  /// information for the name of C++ literal operators.
+  /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
+  /// from the corresponding IdentifierInfo.
+  llvm::FoldingSet<CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
 
-  // FoldingSet<CXXDeductionGuideNameExtra> *
-  void *CXXDeductionGuideNames;
+  /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
+  /// extra information for the name of a C++ deduction guide.
+  /// getCXXDeductionGuideName can be used to obtain a DeclarationName
+  /// from the corresponding template declaration.
+  llvm::FoldingSet<CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
 
 public:
   DeclarationNameTable(const ASTContext &C);
   DeclarationNameTable(const DeclarationNameTable &) = delete;
   DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
-
-  ~DeclarationNameTable();
+  DeclarationNameTable(DeclarationNameTable &&) = delete;
+  DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
+  ~DeclarationNameTable() = default;
 
   /// getIdentifier - Create a declaration name that is a simple
   /// identifier.
index 9866d92f61eb44e3dbd02f92d5dd5c24985c3f7e..843434107056980ff2f4fe06c6c8137f5354d98b 100644 (file)
 
 using namespace clang;
 
-namespace clang {
-
-/// CXXSpecialName - Records the type associated with one of the
-/// "special" kinds of declaration names in C++, e.g., constructors,
-/// destructors, and conversion functions.
-class CXXSpecialName
-  : public DeclarationNameExtra, public llvm::FoldingSetNode {
-public:
-  /// Type - The type associated with this declaration name.
-  QualType Type;
-
-  /// FETokenInfo - Extra information associated with this declaration
-  /// name that can be used by the front end.
-  void *FETokenInfo;
-
-  void Profile(llvm::FoldingSetNodeID &ID) {
-    ID.AddInteger(ExtraKindOrNumArgs);
-    ID.AddPointer(Type.getAsOpaquePtr());
-  }
-};
-
-/// Contains extra information for the name of a C++ deduction guide.
-class CXXDeductionGuideNameExtra : public DeclarationNameExtra,
-                                   public llvm::FoldingSetNode {
-public:
-  /// The template named by the deduction guide.
-  TemplateDecl *Template;
-
-  /// FETokenInfo - Extra information associated with this operator
-  /// name that can be used by the front end.
-  void *FETokenInfo;
-
-  void Profile(llvm::FoldingSetNodeID &ID) {
-    ID.AddPointer(Template);
-  }
-};
-
-/// CXXOperatorIdName - Contains extra information for the name of an
-/// overloaded operator in C++, such as "operator+.
-class CXXOperatorIdName : public DeclarationNameExtra {
-public:
-  /// FETokenInfo - Extra information associated with this operator
-  /// name that can be used by the front end.
-  void *FETokenInfo;
-};
-
-/// CXXLiteralOperatorName - Contains the actual identifier that makes up the
-/// name.
-///
-/// This identifier is stored here rather than directly in DeclarationName so as
-/// to allow Objective-C selectors, which are about a million times more common,
-/// to consume minimal memory.
-class CXXLiteralOperatorIdName
-  : public DeclarationNameExtra, public llvm::FoldingSetNode {
-public:
-  IdentifierInfo *ID;
-
-  /// FETokenInfo - Extra information associated with this operator
-  /// name that can be used by the front end.
-  void *FETokenInfo;
-
-  void Profile(llvm::FoldingSetNodeID &FSID) {
-    FSID.AddPointer(ID);
-  }
-};
-
-} // namespace clang
-
 static int compareInt(unsigned A, unsigned B) {
   return (A < B ? -1 : (A > B ? 1 : 0));
 }
@@ -436,10 +368,6 @@ LLVM_DUMP_METHOD void DeclarationName::dump() const {
 }
 
 DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
-  CXXSpecialNamesImpl = new llvm::FoldingSet<CXXSpecialName>;
-  CXXLiteralOperatorNames = new llvm::FoldingSet<CXXLiteralOperatorIdName>;
-  CXXDeductionGuideNames = new llvm::FoldingSet<CXXDeductionGuideNameExtra>;
-
   // Initialize the overloaded operator names.
   CXXOperatorNames = new (Ctx) CXXOperatorIdName[NUM_OVERLOADED_OPERATORS];
   for (unsigned Op = 0; Op < NUM_OVERLOADED_OPERATORS; ++Op) {
@@ -449,21 +377,6 @@ DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
   }
 }
 
-DeclarationNameTable::~DeclarationNameTable() {
-  auto *SpecialNames =
-      static_cast<llvm::FoldingSet<CXXSpecialName> *>(CXXSpecialNamesImpl);
-  auto *LiteralNames =
-      static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName> *>(
-          CXXLiteralOperatorNames);
-  auto *DeductionGuideNames =
-      static_cast<llvm::FoldingSet<CXXDeductionGuideNameExtra> *>(
-          CXXDeductionGuideNames);
-
-  delete SpecialNames;
-  delete LiteralNames;
-  delete DeductionGuideNames;
-}
-
 DeclarationName DeclarationNameTable::getCXXConstructorName(CanQualType Ty) {
   return getCXXSpecialName(DeclarationName::CXXConstructorName,
                            Ty.getUnqualifiedType());
@@ -478,15 +391,11 @@ DeclarationName
 DeclarationNameTable::getCXXDeductionGuideName(TemplateDecl *Template) {
   Template = cast<TemplateDecl>(Template->getCanonicalDecl());
 
-  auto *DeductionGuideNames =
-      static_cast<llvm::FoldingSet<CXXDeductionGuideNameExtra> *>(
-          CXXDeductionGuideNames);
-
   llvm::FoldingSetNodeID ID;
   ID.AddPointer(Template);
 
   void *InsertPos = nullptr;
-  if (auto *Name = DeductionGuideNames->FindNodeOrInsertPos(ID, InsertPos))
+  if (auto *Name = CXXDeductionGuideNames.FindNodeOrInsertPos(ID, InsertPos))
     return DeclarationName(Name);
 
   auto *Name = new (Ctx) CXXDeductionGuideNameExtra;
@@ -494,7 +403,7 @@ DeclarationNameTable::getCXXDeductionGuideName(TemplateDecl *Template) {
   Name->Template = Template;
   Name->FETokenInfo = nullptr;
 
-  DeductionGuideNames->InsertNode(Name, InsertPos);
+  CXXDeductionGuideNames.InsertNode(Name, InsertPos);
   return DeclarationName(Name);
 }
 
@@ -509,8 +418,6 @@ DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
   assert(Kind >= DeclarationName::CXXConstructorName &&
          Kind <= DeclarationName::CXXConversionFunctionName &&
          "Kind must be a C++ special name kind");
-  llvm::FoldingSet<CXXSpecialName> *SpecialNames
-    = static_cast<llvm::FoldingSet<CXXSpecialName>*>(CXXSpecialNamesImpl);
 
   DeclarationNameExtra::ExtraKind EKind;
   switch (Kind) {
@@ -535,7 +442,7 @@ DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
   ID.AddPointer(Ty.getAsOpaquePtr());
 
   void *InsertPos = nullptr;
-  if (CXXSpecialName *Name = SpecialNames->FindNodeOrInsertPos(ID, InsertPos))
+  if (CXXSpecialName *Name = CXXSpecialNames.FindNodeOrInsertPos(ID, InsertPos))
     return DeclarationName(Name);
 
   CXXSpecialName *SpecialName = new (Ctx) CXXSpecialName;
@@ -543,7 +450,7 @@ DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
   SpecialName->Type = Ty;
   SpecialName->FETokenInfo = nullptr;
 
-  SpecialNames->InsertNode(SpecialName, InsertPos);
+  CXXSpecialNames.InsertNode(SpecialName, InsertPos);
   return DeclarationName(SpecialName);
 }
 
@@ -554,16 +461,12 @@ DeclarationNameTable::getCXXOperatorName(OverloadedOperatorKind Op) {
 
 DeclarationName
 DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
-  llvm::FoldingSet<CXXLiteralOperatorIdName> *LiteralNames
-    = static_cast<llvm::FoldingSet<CXXLiteralOperatorIdName>*>
-                                                      (CXXLiteralOperatorNames);
-
   llvm::FoldingSetNodeID ID;
   ID.AddPointer(II);
 
   void *InsertPos = nullptr;
   if (CXXLiteralOperatorIdName *Name =
-                               LiteralNames->FindNodeOrInsertPos(ID, InsertPos))
+          CXXLiteralOperatorNames.FindNodeOrInsertPos(ID, InsertPos))
     return DeclarationName (Name);
 
   CXXLiteralOperatorIdName *LiteralName = new (Ctx) CXXLiteralOperatorIdName;
@@ -571,7 +474,7 @@ DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
   LiteralName->ID = II;
   LiteralName->FETokenInfo = nullptr;
 
-  LiteralNames->InsertNode(LiteralName, InsertPos);
+  CXXLiteralOperatorNames.InsertNode(LiteralName, InsertPos);
   return DeclarationName(LiteralName);
 }