[llvm] r288610 - TableGen: Use StringRef instead of const std::string& for parameters

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 3 21:48:04 PST 2016


Author: matze
Date: Sat Dec  3 23:48:03 2016
New Revision: 288610

URL: http://llvm.org/viewvc/llvm-project?rev=288610&view=rev
Log:
TableGen: Use StringRef instead of const std::string& for parameters

This avoid an extra construction of a std::string (and a heap
allocation) when the caller only has a StringRef but no std::string at
hand.

Modified:
    llvm/trunk/include/llvm/TableGen/Record.h
    llvm/trunk/lib/TableGen/Record.cpp
    llvm/trunk/lib/TableGen/TGParser.cpp
    llvm/trunk/lib/TableGen/TGParser.h

Modified: llvm/trunk/include/llvm/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TableGen/Record.h?rev=288610&r1=288609&r2=288610&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h (original)
+++ llvm/trunk/include/llvm/TableGen/Record.h Sat Dec  3 23:48:03 2016
@@ -360,7 +360,7 @@ public:
   /// Implementors of this method should return the type of the named field if
   /// they are of record type.
   ///
-  virtual RecTy *getFieldType(const std::string &FieldName) const {
+  virtual RecTy *getFieldType(StringRef FieldName) const {
     return nullptr;
   }
 
@@ -369,7 +369,7 @@ public:
   /// this method should return non-null, otherwise it returns null.
   ///
   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
-                             const std::string &FieldName) const {
+                             StringRef FieldName) const {
     return nullptr;
   }
 
@@ -436,7 +436,7 @@ public:
   /// Implementors of this method should return the type of the named field if
   /// they are of record type.
   ///
-  RecTy *getFieldType(const std::string &FieldName) const override;
+  RecTy *getFieldType(StringRef FieldName) const override;
 
   /// This method is used to implement
   /// VarListElementInit::resolveReferences.  If the list element is resolvable
@@ -961,7 +961,7 @@ public:
     return I->getKind() == IK_VarInit;
   }
 
-  static VarInit *get(const std::string &VN, RecTy *T);
+  static VarInit *get(StringRef VN, RecTy *T);
   static VarInit *get(Init *VN, RecTy *T);
 
   const std::string &getName() const;
@@ -974,9 +974,9 @@ public:
   Init *resolveListElementReference(Record &R, const RecordVal *RV,
                                     unsigned Elt) const override;
 
-  RecTy *getFieldType(const std::string &FieldName) const override;
+  RecTy *getFieldType(StringRef FieldName) const override;
   Init *getFieldInit(Record &R, const RecordVal *RV,
-                     const std::string &FieldName) const override;
+                     StringRef FieldName) const override;
 
   /// This method is used by classes that refer to other
   /// variables which may not be defined at the time they expression is formed.
@@ -1092,9 +1092,9 @@ public:
 
   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
 
-  RecTy *getFieldType(const std::string &FieldName) const override;
+  RecTy *getFieldType(StringRef FieldName) const override;
   Init *getFieldInit(Record &R, const RecordVal *RV,
-                     const std::string &FieldName) const override;
+                     StringRef FieldName) const override;
 
   std::string getAsString() const override;
 
@@ -1117,7 +1117,7 @@ class FieldInit : public TypedInit {
   Init *Rec;                // Record we are referring to
   std::string FieldName;    // Field we are accessing
 
-  FieldInit(Init *R, const std::string &FN)
+  FieldInit(Init *R, StringRef FN)
       : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
     assert(getType() && "FieldInit with non-record type!");
   }
@@ -1130,7 +1130,7 @@ public:
     return I->getKind() == IK_FieldInit;
   }
 
-  static FieldInit *get(Init *R, const std::string &FN);
+  static FieldInit *get(Init *R, StringRef FN);
 
   Init *getBit(unsigned Bit) const override;
 
@@ -1154,8 +1154,7 @@ class DagInit : public TypedInit, public
   std::vector<Init*> Args;
   std::vector<std::string> ArgNames;
 
-  DagInit(Init *V, const std::string &VN,
-          ArrayRef<Init *> ArgRange,
+  DagInit(Init *V, StringRef VN, ArrayRef<Init *> ArgRange,
           ArrayRef<std::string> NameRange)
       : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
           Args(ArgRange.begin(), ArgRange.end()),
@@ -1169,10 +1168,9 @@ public:
     return I->getKind() == IK_DagInit;
   }
 
-  static DagInit *get(Init *V, const std::string &VN,
-                      ArrayRef<Init *> ArgRange,
+  static DagInit *get(Init *V, StringRef VN, ArrayRef<Init *> ArgRange,
                       ArrayRef<std::string> NameRange);
-  static DagInit *get(Init *V, const std::string &VN,
+  static DagInit *get(Init *V, StringRef VN,
                       const std::vector<std::pair<Init*, std::string>> &args);
 
   void Profile(FoldingSetNodeID &ID) const;
@@ -1233,7 +1231,7 @@ class RecordVal {
 
 public:
   RecordVal(Init *N, RecTy *T, bool P);
-  RecordVal(const std::string &N, RecTy *T, bool P);
+  RecordVal(StringRef N, RecTy *T, bool P);
 
   const std::string &getName() const;
   const Init *getNameInit() const { return NameAndPrefix.getPointer(); }
@@ -1309,8 +1307,8 @@ public:
     init();
   }
 
-  explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
-                  RecordKeeper &records, bool Anonymous = false)
+  explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
+                  bool Anonymous = false)
     : Record(StringInit::get(N), locs, records, Anonymous) {}
 
   // When copy-constructing a Record, we must still guarantee a globally unique
@@ -1335,8 +1333,8 @@ public:
     return getNameInit()->getAsUnquotedString();
   }
 
-  void setName(Init *Name);               // Also updates RecordKeeper.
-  void setName(const std::string &Name);  // Also updates RecordKeeper.
+  void setName(Init *Name);      // Also updates RecordKeeper.
+  void setName(StringRef Name);  // Also updates RecordKeeper.
 
   ArrayRef<SMLoc> getLoc() const { return Locs; }
 
@@ -1559,7 +1557,7 @@ struct MultiClass {
 
   void dump() const;
 
-  MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
+  MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
     Rec(Name, Loc, Records) {}
 };
 
@@ -1571,12 +1569,12 @@ public:
   const RecordMap &getClasses() const { return Classes; }
   const RecordMap &getDefs() const { return Defs; }
 
-  Record *getClass(const std::string &Name) const {
+  Record *getClass(StringRef Name) const {
     auto I = Classes.find(Name);
     return I == Classes.end() ? nullptr : I->second.get();
   }
 
-  Record *getDef(const std::string &Name) const {
+  Record *getDef(StringRef Name) const {
     auto I = Defs.find(Name);
     return I == Defs.end() ? nullptr : I->second.get();
   }
@@ -1601,8 +1599,7 @@ public:
   /// This method returns all concrete definitions
   /// that derive from the specified class name.  A class with the specified
   /// name must exist.
-  std::vector<Record *>
-  getAllDerivedDefinitions(const std::string &ClassName) const;
+  std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
 
   void dump() const;
 };
@@ -1718,12 +1715,12 @@ raw_ostream &operator<<(raw_ostream &OS,
 /// Return an Init with a qualifier prefix referring
 /// to CurRec's name.
 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                  Init *Name, const std::string &Scoper);
+                  Init *Name, StringRef Scoper);
 
 /// Return an Init with a qualifier prefix referring
 /// to CurRec's name.
 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                  const std::string &Name, const std::string &Scoper);
+                  StringRef Name, StringRef Scoper);
 
 } // end namespace llvm
 

Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=288610&r1=288609&r2=288610&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Sat Dec  3 23:48:03 2016
@@ -43,7 +43,7 @@ namespace llvm {
 
 class TableGenStringKey {
 public:
-  TableGenStringKey(const std::string &str) : data(str) {}
+  TableGenStringKey(StringRef str) : data(str) {}
   TableGenStringKey(const char *str) : data(str) {}
 
   const std::string &str() const { return data; }
@@ -1155,7 +1155,7 @@ std::string TernOpInit::getAsString() co
          RHS->getAsString() + ")";
 }
 
-RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
+RecTy *TypedInit::getFieldType(StringRef FieldName) const {
   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
     if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
       return Field->getType();
@@ -1277,7 +1277,7 @@ TypedInit::convertInitListSlice(const st
 }
 
 
-VarInit *VarInit::get(const std::string &VN, RecTy *T) {
+VarInit *VarInit::get(StringRef VN, RecTy *T) {
   Init *Value = StringInit::get(VN);
   return VarInit::get(Value, T);
 }
@@ -1327,7 +1327,7 @@ Init *VarInit::resolveListElementReferen
   return nullptr;
 }
 
-RecTy *VarInit::getFieldType(const std::string &FieldName) const {
+RecTy *VarInit::getFieldType(StringRef FieldName) const {
   if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
       return RV->getType();
@@ -1335,7 +1335,7 @@ RecTy *VarInit::getFieldType(const std::
 }
 
 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
-                            const std::string &FieldName) const {
+                            StringRef FieldName) const {
   if (isa<RecordRecTy>(getType()))
     if (const RecordVal *Val = R.getValue(VarName)) {
       if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
@@ -1442,14 +1442,14 @@ Init *DefInit::convertInitializerTo(RecT
   return nullptr;
 }
 
-RecTy *DefInit::getFieldType(const std::string &FieldName) const {
+RecTy *DefInit::getFieldType(StringRef FieldName) const {
   if (const RecordVal *RV = Def->getValue(FieldName))
     return RV->getType();
   return nullptr;
 }
 
 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
-                            const std::string &FieldName) const {
+                            StringRef FieldName) const {
   return Def->getValue(FieldName)->getValue();
 }
 
@@ -1457,7 +1457,7 @@ std::string DefInit::getAsString() const
   return Def->getName();
 }
 
-FieldInit *FieldInit::get(Init *R, const std::string &FN) {
+FieldInit *FieldInit::get(Init *R, StringRef FN) {
   typedef std::pair<Init *, TableGenStringKey> Key;
   static DenseMap<Key, std::unique_ptr<FieldInit>> ThePool;
 
@@ -1503,7 +1503,7 @@ Init *FieldInit::resolveReferences(Recor
   return const_cast<FieldInit *>(this);
 }
 
-static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN,
+static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringRef VN,
                            ArrayRef<Init *> ArgRange,
                            ArrayRef<std::string> NameRange) {
   ID.AddPointer(V);
@@ -1520,8 +1520,7 @@ static void ProfileDagInit(FoldingSetNod
 }
 
 DagInit *
-DagInit::get(Init *V, const std::string &VN,
-             ArrayRef<Init *> ArgRange,
+DagInit::get(Init *V, StringRef VN, ArrayRef<Init *> ArgRange,
              ArrayRef<std::string> NameRange) {
   static FoldingSet<DagInit> ThePool;
   static std::vector<std::unique_ptr<DagInit>> TheActualPool;
@@ -1540,7 +1539,7 @@ DagInit::get(Init *V, const std::string
 }
 
 DagInit *
-DagInit::get(Init *V, const std::string &VN,
+DagInit::get(Init *V, StringRef VN,
              const std::vector<std::pair<Init*, std::string> > &args) {
   std::vector<Init *> Args;
   std::vector<std::string> Names;
@@ -1602,7 +1601,7 @@ RecordVal::RecordVal(Init *N, RecTy *T,
   assert(Value && "Cannot create unset value for current type!");
 }
 
-RecordVal::RecordVal(const std::string &N, RecTy *T, bool P)
+RecordVal::RecordVal(StringRef N, RecTy *T, bool P)
   : NameAndPrefix(StringInit::get(N), P), Ty(T) {
   Value = UnsetInit::get()->convertInitializerTo(Ty);
   assert(Value && "Cannot create unset value for current type!");
@@ -1668,7 +1667,7 @@ void Record::setName(Init *NewName) {
   // this.  See TGParser::ParseDef and TGParser::ParseDefm.
 }
 
-void Record::setName(const std::string &Name) {
+void Record::setName(StringRef Name) {
   setName(StringInit::get(Name));
 }
 
@@ -1909,7 +1908,7 @@ raw_ostream &llvm::operator<<(raw_ostrea
 }
 
 std::vector<Record *>
-RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
+RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
   Record *Class = getClass(ClassName);
   if (!Class)
     PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
@@ -1923,7 +1922,7 @@ RecordKeeper::getAllDerivedDefinitions(c
 }
 
 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                        Init *Name, const std::string &Scoper) {
+                        Init *Name, StringRef Scoper) {
   RecTy *Type = cast<TypedInit>(Name)->getType();
 
   BinOpInit *NewName =
@@ -1950,7 +1949,6 @@ Init *llvm::QualifyName(Record &CurRec,
 }
 
 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                        const std::string &Name,
-                        const std::string &Scoper) {
+                        StringRef Name, StringRef Scoper) {
   return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper);
 }

Modified: llvm/trunk/lib/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?rev=288610&r1=288609&r2=288610&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.cpp (original)
+++ llvm/trunk/lib/TableGen/TGParser.cpp Sat Dec  3 23:48:03 2016
@@ -715,8 +715,7 @@ RecTy *TGParser::ParseType() {
 
 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
 /// has already been read.
-Init *TGParser::ParseIDValue(Record *CurRec,
-                             const std::string &Name, SMLoc NameLoc,
+Init *TGParser::ParseIDValue(Record *CurRec, StringRef Name, SMLoc NameLoc,
                              IDParseMode Mode) {
   if (CurRec) {
     if (const RecordVal *RV = CurRec->getValue(Name))

Modified: llvm/trunk/lib/TableGen/TGParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.h?rev=288610&r1=288609&r2=288610&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.h (original)
+++ llvm/trunk/lib/TableGen/TGParser.h Sat Dec  3 23:48:03 2016
@@ -36,8 +36,7 @@ namespace llvm {
     std::vector<unsigned> Bits;
     Init *Value;
     SMLoc Loc;
-    LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
-              SMLoc L)
+    LetRecord(StringRef N, const std::vector<unsigned> &B, Init *V, SMLoc L)
       : Name(N), Bits(B), Value(V), Loc(L) {
     }
   };
@@ -107,7 +106,7 @@ private:  // Semantic analysis methods.
   bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
                 ArrayRef<unsigned> BitList, Init *V,
                 bool AllowSelfAssignment = false);
-  bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
+  bool SetValue(Record *TheRec, SMLoc Loc, StringRef ValName,
                 ArrayRef<unsigned> BitList, Init *V,
                 bool AllowSelfAssignment = false) {
     return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V,
@@ -168,7 +167,7 @@ private:  // Parser methods.
   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
 
-  Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
+  Init *ParseIDValue(Record *CurRec, StringRef Name, SMLoc NameLoc,
                      IDParseMode Mode = ParseValueMode);
   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
                          IDParseMode Mode = ParseValueMode);




More information about the llvm-commits mailing list