Remove SelectorTable/SelectorInfo, simply store all selectors in the central IdentifierTable.

Rationale:

We currently have a separate table to unique ObjC selectors. Since I don't need all the instance data in IdentifierInfo, I thought this would save space (and make more sense conceptually).

It turns out the cost of having duplicate entries for unary selectors (i.e. names without colons) outweighs the cost difference between the IdentifierInfo & SelectorInfo structures. Here is the data:

Two tables:

*** Selector/Identifier Stats:
# Selectors/Identifiers: 51635 
Bytes allocated:         1999824

One table:

*** Identifier Table Stats:
# Identifiers:   49500
Bytes allocated: 1990316




git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42139 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp
index df0398c..6420f0f 100644
--- a/AST/ASTContext.cpp
+++ b/AST/ASTContext.cpp
@@ -104,9 +104,6 @@
     NumFunctionP*sizeof(FunctionTypeProto)+
     NumFunctionNP*sizeof(FunctionTypeNoProto)+
     NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
-  
-  if (Selectors)
-    Selectors->PrintStats();
 }
 
 
@@ -809,11 +806,3 @@
   
   return getTagDeclType(CFConstantStringTypeDecl);
 }
-
-SelectorInfo &ASTContext::getSelectorInfo(const char *NameStart, 
-                                          const char *NameEnd) {
-  if (!Selectors) // create the table lazily
-    Selectors = new SelectorTable();
-  return Selectors->get(NameStart, NameEnd);
-}
-
diff --git a/AST/Expr.cpp b/AST/Expr.cpp
index 08773e8..e5a1282 100644
--- a/AST/Expr.cpp
+++ b/AST/Expr.cpp
@@ -858,7 +858,7 @@
 
 // constructor for unary messages.
 ObjCMessageExpr::ObjCMessageExpr(
-  IdentifierInfo *clsName, SelectorInfo &methName, QualType retType, 
+  IdentifierInfo *clsName, IdentifierInfo &methName, QualType retType, 
   SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(methName) {
   ClassName = clsName;
@@ -867,7 +867,7 @@
 }
 
 ObjCMessageExpr::ObjCMessageExpr(
-  Expr *fn, SelectorInfo &methName, QualType retType, 
+  Expr *fn, IdentifierInfo &methName, QualType retType, 
   SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(methName), ClassName(0) {
   SubExprs = new Expr*[1];
@@ -878,7 +878,7 @@
 
 // constructor for keyword messages.
 ObjCMessageExpr::ObjCMessageExpr(
-  Expr *fn, SelectorInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs, 
+  Expr *fn, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs, 
   QualType retType, SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(0) {
   SubExprs = new Expr*[numargs+1];
@@ -890,7 +890,7 @@
 }
 
 ObjCMessageExpr::ObjCMessageExpr(
-  IdentifierInfo *clsName, SelectorInfo &selInfo, ObjcKeywordMessage *keys, 
+  IdentifierInfo *clsName, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, 
   unsigned numargs, QualType retType, SourceLocation LBrac, SourceLocation RBrac)
   : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(clsName) {
   SubExprs = new Expr*[numargs+1];
diff --git a/Lex/IdentifierTable.cpp b/Lex/IdentifierTable.cpp
index c1c00d2..d3faeb5 100644
--- a/Lex/IdentifierTable.cpp
+++ b/Lex/IdentifierTable.cpp
@@ -209,34 +209,3 @@
   // Compute statistics about the memory allocated for identifiers.
   HashTable.getAllocator().PrintStats();
 }
-
-/// PrintStats - Print statistics about how well the identifier table is doing
-/// at hashing identifiers.
-void SelectorTable::PrintStats() const {
-  unsigned NumBuckets = HashTable.getNumBuckets();
-  unsigned NumIdentifiers = HashTable.getNumItems();
-  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
-  unsigned AverageIdentifierSize = 0;
-  unsigned MaxIdentifierLength = 0;
-  
-  // TODO: Figure out maximum times an identifier had to probe for -stats.
-  for (llvm::StringMap<SelectorInfo, llvm::BumpPtrAllocator>::const_iterator
-       I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
-    unsigned IdLen = I->getKeyLength();
-    AverageIdentifierSize += IdLen;
-    if (MaxIdentifierLength < IdLen)
-      MaxIdentifierLength = IdLen;
-  }
-  
-  fprintf(stderr, "\n*** Selector Table Stats:\n");
-  fprintf(stderr, "# Selectors:   %d\n", NumIdentifiers);
-  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
-  fprintf(stderr, "Hash density (#selectors per bucket): %f\n",
-          NumIdentifiers/(double)NumBuckets);
-  fprintf(stderr, "Ave selector length: %f\n",
-          (AverageIdentifierSize/(double)NumIdentifiers));
-  fprintf(stderr, "Max selector length: %d\n", MaxIdentifierLength);
-  
-  // Compute statistics about the memory allocated for identifiers.
-  HashTable.getAllocator().PrintStats();
-}
diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp
index 3742702..42024b1 100644
--- a/Sema/SemaDecl.cpp
+++ b/Sema/SemaDecl.cpp
@@ -1308,8 +1308,8 @@
     methodName += ":";
   }
   methodName[len] = '\0';
-  SelectorInfo &SelName = Context.getSelectorInfo(&methodName[0], 
-                                                  &methodName[0]+len);
+  IdentifierInfo &SelName = Context.Idents.get(&methodName[0], 
+                                               &methodName[0]+len);
   llvm::SmallVector<ParmVarDecl*, 16> Params;
 
   for (unsigned i = 0; i < NumKeywords; i++) {
@@ -1340,8 +1340,8 @@
                       IdentifierInfo *SelectorName, AttributeList *AttrList,
 		      tok::ObjCKeywordKind MethodDeclKind) {
   const char *methodName = SelectorName->getName();
-  SelectorInfo &SelName = Context.getSelectorInfo(methodName, 
-                                                  methodName+strlen(methodName));
+  IdentifierInfo &SelName = Context.Idents.get(methodName, 
+                                              methodName+strlen(methodName));
   QualType resultDeclType = QualType::getFromOpaquePtr(ReturnType);
   ObjcMethodDecl* ObjcMethod = new ObjcMethodDecl(MethodLoc, 
 			             SelName, resultDeclType, 0, -1,
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index 886b3dd..11c3ecc 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -1857,7 +1857,7 @@
   return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
 }
 
-static SelectorInfo &DeriveSelector(ObjcKeywordMessage *Keywords, 
+static IdentifierInfo &DeriveSelector(ObjcKeywordMessage *Keywords, 
                                     unsigned NumKeywords,
                                     ASTContext &Context) {
   // Derive the selector name from the keyword declarations.
@@ -1875,7 +1875,7 @@
     methodName += ":";
   }
   methodName[len] = '\0';
-  return Context.getSelectorInfo(&methodName[0], &methodName[0]+len);
+  return Context.Idents.get(&methodName[0], &methodName[0]+len);
 }
 
 // This actions handles keyword message to classes.
@@ -1884,7 +1884,7 @@
   ObjcKeywordMessage *Keywords, unsigned NumKeywords,
   SourceLocation lbrac, SourceLocation rbrac)
 {
-  SelectorInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
+  IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
   assert(receivingClassName && "missing receiver class name");
 
   return new ObjCMessageExpr(receivingClassName, SelName, Keywords, NumKeywords, 
@@ -1895,7 +1895,7 @@
 Sema::ExprResult Sema::ActOnKeywordMessage(
   ExprTy *receiver, ObjcKeywordMessage *Keywords, unsigned NumKeywords,
   SourceLocation lbrac, SourceLocation rbrac) {
-  SelectorInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
+  IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
   assert(receiver && "missing receiver expression");
   
   Expr *RExpr = static_cast<Expr *>(receiver);
@@ -1910,7 +1910,7 @@
   assert(receivingClassName && "missing receiver class name");
   
   // FIXME: this should be passed in...
-  SelectorInfo &SName = Context.getSelectorInfo(
+  IdentifierInfo &SName = Context.Idents.get(
            selName->getName(), selName->getName()+strlen(selName->getName()));
   return new ObjCMessageExpr(receivingClassName, SName,
                              Context.IntTy/*FIXME*/, lbrac, rbrac);
@@ -1924,7 +1924,7 @@
   
   Expr *RExpr = static_cast<Expr *>(receiver);
   // FIXME: this should be passed in...
-  SelectorInfo &SName = Context.getSelectorInfo(
+  IdentifierInfo &SName = Context.Idents.get(
            selName->getName(), selName->getName()+strlen(selName->getName()));
   return new ObjCMessageExpr(RExpr, SName,
                              Context.IntTy/*FIXME*/, lbrac, rbrac);
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index 5d753aa..76a51fc 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -24,7 +24,6 @@
 
 namespace clang {
   class TargetInfo;
-  class SelectorTable;
   
 /// ASTContext - This class holds long-lived AST nodes (such as types and
 /// decls) that can be referred to throughout the semantic analysis of a file.
@@ -39,7 +38,6 @@
   llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
   llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
   RecordDecl *CFConstantStringTypeDecl;
-  SelectorTable *Selectors;
 public:
   SourceManager &SourceMgr;
   TargetInfo &Target;
@@ -57,8 +55,7 @@
   QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
   
   ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents) : 
-    CFConstantStringTypeDecl(0), Selectors(0), 
-    SourceMgr(SM), Target(t), Idents(idents) {
+    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), Idents(idents) {
     InitBuiltinTypes();
     BuiltinInfo.InitializeBuiltins(idents, Target);
   }    
@@ -179,14 +176,6 @@
   /// 'typeSize' is a real floating point or complex type.
   QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 
                                              QualType typeDomain) const;
-
-  //===--------------------------------------------------------------------===//
-  //                            Objective-C
-  //===--------------------------------------------------------------------===//
-  
-  /// getSelectorInfo - Return a uniqued character string for the selector.
-  SelectorInfo &getSelectorInfo(const char *NameStart, const char *NameEnd);
-
 private:
   ASTContext(const ASTContext&); // DO NOT IMPLEMENT
   void operator=(const ASTContext&); // DO NOT IMPLEMENT
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index aea0dc8..67f5ebd 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -26,7 +26,6 @@
 class AttributeList;
 class ObjcIvarDecl;
 class ObjcMethodDecl;
-class SelectorInfo;
 
 
 /// Decl - This represents one declaration (or definition), e.g. a variable, 
@@ -618,7 +617,7 @@
   enum ImplementationControl { None, Required, Optional };
 private:
   // A unigue name for this method.
-  SelectorInfo &Selector;
+  IdentifierInfo &Selector;
   
   // Type of this method.
   QualType MethodDeclType;
@@ -636,7 +635,7 @@
   ImplementationControl DeclImplementation : 2;
 
 public:
-  ObjcMethodDecl(SourceLocation L, SelectorInfo &SelId, QualType T,
+  ObjcMethodDecl(SourceLocation L, IdentifierInfo &SelId, QualType T,
 		 ParmVarDecl **paramInfo = 0, int numParams=-1,
 		 AttributeList *M = 0, bool isInstance = true, 
 		 Decl *PrevDecl = 0)
@@ -644,7 +643,7 @@
       ParamInfo(paramInfo), NumMethodParams(numParams),
       MethodAttrs(M), IsInstance(isInstance) {}
 
-  ObjcMethodDecl(Kind DK, SourceLocation L, SelectorInfo &SelId, QualType T,
+  ObjcMethodDecl(Kind DK, SourceLocation L, IdentifierInfo &SelId, QualType T,
 		 ParmVarDecl **paramInfo = 0, int numParams=-1,
 		 AttributeList *M = 0, bool isInstance = true, 
 		 Decl *PrevDecl = 0)
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h
index 84d9a79..24eecf5 100644
--- a/include/clang/AST/Expr.h
+++ b/include/clang/AST/Expr.h
@@ -1068,7 +1068,7 @@
   unsigned NumArgs;
   
   // A unigue name for this message.
-  SelectorInfo &Selector;
+  IdentifierInfo &Selector;
   
   IdentifierInfo **KeyIdents;
   
@@ -1078,17 +1078,17 @@
 public:
   // constructor for unary messages. 
   // FIXME: clsName should be typed to ObjCInterfaceType
-  ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo &selInfo,
+  ObjCMessageExpr(IdentifierInfo *clsName, IdentifierInfo &selInfo,
                   QualType retType, SourceLocation LBrac, SourceLocation RBrac);
-  ObjCMessageExpr(Expr *receiver, SelectorInfo &selInfo,
+  ObjCMessageExpr(Expr *receiver, IdentifierInfo &selInfo,
                   QualType retType, SourceLocation LBrac, SourceLocation RBrac);
                   
   // constructor for keyword messages.
   // FIXME: clsName should be typed to ObjCInterfaceType
-  ObjCMessageExpr(IdentifierInfo *clsName, SelectorInfo &selInfo,
+  ObjCMessageExpr(IdentifierInfo *clsName, IdentifierInfo &selInfo,
                   ObjcKeywordMessage *keys, unsigned numargs, QualType retType, 
                   SourceLocation LBrac, SourceLocation RBrac);
-  ObjCMessageExpr(Expr *receiver, SelectorInfo &selInfo,
+  ObjCMessageExpr(Expr *receiver, IdentifierInfo &selInfo,
                   ObjcKeywordMessage *keys, unsigned numargs, QualType retType, 
                   SourceLocation LBrac, SourceLocation RBrac);
   ~ObjCMessageExpr() {
diff --git a/include/clang/Lex/IdentifierTable.h b/include/clang/Lex/IdentifierTable.h
index 9a6a93e..fd2cb6a 100644
--- a/include/clang/Lex/IdentifierTable.h
+++ b/include/clang/Lex/IdentifierTable.h
@@ -166,54 +166,6 @@
   void AddKeywords(const LangOptions &LangOpts);
 };
 
-/// SelectorInfo - One of these records is kept for each selector. Selectors
-/// are created as a by-product of parsing a method declaration/definition, 
-/// message expression, or @selector expression.
-class SelectorInfo {
-  void *ObjcMethodDecl;  // FIXME: add setter/getter.
-  
-  SelectorInfo(const SelectorInfo&);  // NONCOPYABLE.
-public:
-  SelectorInfo() : ObjcMethodDecl(0) {}
-
-  /// getName - Return the actual string for this selector.  The returned 
-  /// string is properly null terminated.
-  ///
-  const char *getName() const {
-    // String data is stored immediately after the IdentifierInfo object.
-    return (const char*)(this+1);
-  }
-};
-
-/// SelectorTable - This table implements an efficient mapping from strings to
-/// SelectorInfo nodes.  
-class SelectorTable {
-  // Shark shows that using MallocAllocator is *much* slower than using this
-  // BumpPtrAllocator!
-  typedef llvm::StringMap<SelectorInfo, llvm::BumpPtrAllocator> HashTableTy;
-  HashTableTy HashTable;
-public:
-  SelectorTable() : HashTable(4096) { }
-  
-  /// get - Return the selector info for the specified name.
-  ///
-  SelectorInfo &get(const char *NameStart, const char *NameEnd) {
-    return HashTable.GetOrCreateValue(NameStart, NameEnd).getValue();
-  }
-  SelectorInfo &get(const char *Name) {
-    return get(Name, Name+strlen(Name));
-  }
-  typedef HashTableTy::const_iterator iterator;
-  typedef HashTableTy::const_iterator const_iterator;
-  
-  iterator begin() const { return HashTable.begin(); }
-  iterator end() const   { return HashTable.end(); }
-  
-  /// PrintStats - Print some statistics to stderr that indicate how well the
-  /// hashing is doing.
-  void PrintStats() const;
-};
-
 }  // end namespace clang
 
 #endif