[llvm] r288642 - TableGen: Use more StringInit instead of StringRef

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 4 22:00:37 PST 2016


Author: matze
Date: Mon Dec  5 00:00:36 2016
New Revision: 288642

URL: http://llvm.org/viewvc/llvm-project?rev=288642&view=rev
Log:
TableGen: Use more StringInit instead of StringRef

This forces the code to call StringInit::get on the string early and
avoids storing duplicates in std::string and sometimes allows pointer
comparisons instead of string comparisons.

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

Modified: llvm/trunk/include/llvm/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TableGen/Record.h?rev=288642&r1=288641&r2=288642&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h (original)
+++ llvm/trunk/include/llvm/TableGen/Record.h Mon Dec  5 00:00:36 2016
@@ -38,10 +38,11 @@
 namespace llvm {
 
 class ListRecTy;
-struct MultiClass;
 class Record;
-class RecordVal;
 class RecordKeeper;
+class RecordVal;
+class StringInit;
+struct MultiClass;
 
 //===----------------------------------------------------------------------===//
 //  Type Classes
@@ -360,7 +361,7 @@ public:
   /// Implementors of this method should return the type of the named field if
   /// they are of record type.
   ///
-  virtual RecTy *getFieldType(StringRef FieldName) const {
+  virtual RecTy *getFieldType(StringInit *FieldName) const {
     return nullptr;
   }
 
@@ -369,7 +370,7 @@ public:
   /// this method should return non-null, otherwise it returns null.
   ///
   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
-                             StringRef FieldName) const {
+                             StringInit *FieldName) const {
     return nullptr;
   }
 
@@ -431,7 +432,7 @@ public:
   /// Implementors of this method should return the type of the named field if
   /// they are of record type.
   ///
-  RecTy *getFieldType(StringRef FieldName) const override;
+  RecTy *getFieldType(StringInit *FieldName) const override;
 
   /// This method is used to implement
   /// VarListElementInit::resolveReferences.  If the list element is resolvable
@@ -969,9 +970,9 @@ public:
   Init *resolveListElementReference(Record &R, const RecordVal *RV,
                                     unsigned Elt) const override;
 
-  RecTy *getFieldType(StringRef FieldName) const override;
+  RecTy *getFieldType(StringInit *FieldName) const override;
   Init *getFieldInit(Record &R, const RecordVal *RV,
-                     StringRef FieldName) const override;
+                     StringInit *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.
@@ -1087,9 +1088,9 @@ public:
 
   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
 
-  RecTy *getFieldType(StringRef FieldName) const override;
+  RecTy *getFieldType(StringInit *FieldName) const override;
   Init *getFieldInit(Record &R, const RecordVal *RV,
-                     StringRef FieldName) const override;
+                     StringInit *FieldName) const override;
 
   std::string getAsString() const override;
 
@@ -1110,9 +1111,9 @@ public:
 ///
 class FieldInit : public TypedInit {
   Init *Rec;                // Record we are referring to
-  std::string FieldName;    // Field we are accessing
+  StringInit *FieldName;    // Field we are accessing
 
-  FieldInit(Init *R, StringRef FN)
+  FieldInit(Init *R, StringInit *FN)
       : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
     assert(getType() && "FieldInit with non-record type!");
   }
@@ -1125,7 +1126,7 @@ public:
     return I->getKind() == IK_FieldInit;
   }
 
-  static FieldInit *get(Init *R, StringRef FN);
+  static FieldInit *get(Init *R, StringInit *FN);
 
   Init *getBit(unsigned Bit) const override;
 
@@ -1135,7 +1136,7 @@ public:
   Init *resolveReferences(Record &R, const RecordVal *RV) const override;
 
   std::string getAsString() const override {
-    return Rec->getAsString() + "." + FieldName;
+    return Rec->getAsString() + "." + FieldName->getValue().str();
   }
 };
 
@@ -1712,11 +1713,6 @@ raw_ostream &operator<<(raw_ostream &OS,
 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
                   Init *Name, StringRef Scoper);
 
-/// Return an Init with a qualifier prefix referring
-/// to CurRec's name.
-Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                  StringRef Name, StringRef Scoper);
-
 } // end namespace llvm
 
 #endif // LLVM_TABLEGEN_RECORD_H

Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=288642&r1=288641&r2=288642&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Mon Dec  5 00:00:36 2016
@@ -678,9 +678,7 @@ Init *UnOpInit::Fold(Record *CurRec, Mul
       if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
         return StringInit::get(LHSi->getAsString());
     } else {
-      if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) {
-        StringRef Name = LHSs->getValue();
-
+      if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
         // From TGParser::ParseIDValue
         if (CurRec) {
           if (const RecordVal *RV = CurRec->getValue(Name)) {
@@ -718,11 +716,11 @@ Init *UnOpInit::Fold(Record *CurRec, Mul
           }
         }
         assert(CurRec && "NULL pointer");
-        if (Record *D = (CurRec->getRecords()).getDef(Name))
+        if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
           return DefInit::get(D);
 
         PrintFatalError(CurRec->getLoc(),
-                        "Undefined reference:'" + Name + "'\n");
+                        "Undefined reference:'" + Name->getValue() + "'\n");
       }
 
       if (isa<IntRecTy>(getType())) {
@@ -1175,7 +1173,7 @@ std::string TernOpInit::getAsString() co
          RHS->getAsString() + ")";
 }
 
-RecTy *TypedInit::getFieldType(StringRef FieldName) const {
+RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
   if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
     if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
       return Field->getType();
@@ -1348,7 +1346,7 @@ Init *VarInit::resolveListElementReferen
   return nullptr;
 }
 
-RecTy *VarInit::getFieldType(StringRef FieldName) const {
+RecTy *VarInit::getFieldType(StringInit *FieldName) const {
   if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
       return RV->getType();
@@ -1356,7 +1354,7 @@ RecTy *VarInit::getFieldType(StringRef F
 }
 
 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
-                            StringRef FieldName) const {
+                            StringInit *FieldName) const {
   if (isa<RecordRecTy>(getType()))
     if (const RecordVal *Val = R.getValue(VarName)) {
       if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
@@ -1464,14 +1462,14 @@ Init *DefInit::convertInitializerTo(RecT
   return nullptr;
 }
 
-RecTy *DefInit::getFieldType(StringRef FieldName) const {
+RecTy *DefInit::getFieldType(StringInit *FieldName) const {
   if (const RecordVal *RV = Def->getValue(FieldName))
     return RV->getType();
   return nullptr;
 }
 
 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
-                            StringRef FieldName) const {
+                            StringInit *FieldName) const {
   return Def->getValue(FieldName)->getValue();
 }
 
@@ -1479,8 +1477,8 @@ std::string DefInit::getAsString() const
   return Def->getName();
 }
 
-FieldInit *FieldInit::get(Init *R, StringRef FN) {
-  typedef std::pair<Init *, TableGenStringKey> Key;
+FieldInit *FieldInit::get(Init *R, StringInit *FN) {
+  typedef std::pair<Init *, StringInit *> Key;
   static DenseMap<Key, FieldInit*> ThePool;
 
   Key TheKey(std::make_pair(R, FN));
@@ -1965,8 +1963,3 @@ Init *llvm::QualifyName(Record &CurRec,
     NewName = BinOp->Fold(&CurRec, CurMultiClass);
   return NewName;
 }
-
-Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
-                        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=288642&r1=288641&r2=288642&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.cpp (original)
+++ llvm/trunk/lib/TableGen/TGParser.cpp Mon Dec  5 00:00:36 2016
@@ -717,14 +717,16 @@ RecTy *TGParser::ParseType() {
 /// has already been read.
 Init *TGParser::ParseIDValue(Record *CurRec, StringRef Name, SMLoc NameLoc,
                              IDParseMode Mode) {
+  StringInit *NameInit;
   if (CurRec) {
     if (const RecordVal *RV = CurRec->getValue(Name))
       return VarInit::get(Name, RV->getType());
 
-    Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
+    NameInit = StringInit::get(Name);
+    Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, NameInit, ":");
 
     if (CurMultiClass)
-      TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
+      TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, NameInit,
                                     "::");
 
     if (CurRec->isTemplateArg(TemplateArgName)) {
@@ -732,10 +734,11 @@ Init *TGParser::ParseIDValue(Record *Cur
       assert(RV && "Template arg doesn't exist??");
       return VarInit::get(TemplateArgName, RV->getType());
     }
-  }
+  } else
+    NameInit = StringInit::get(Name);
 
   if (CurMultiClass) {
-    Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
+    Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, NameInit,
                                "::");
 
     if (CurMultiClass->Rec.isTemplateArg(MCName)) {
@@ -748,12 +751,12 @@ Init *TGParser::ParseIDValue(Record *Cur
   // If this is in a foreach loop, make sure it's not a loop iterator
   for (const auto &L : Loops) {
     VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
-    if (IterVar && IterVar->getName() == Name)
+    if (IterVar && IterVar->getNameInit() == NameInit)
       return IterVar;
   }
 
   if (Mode == ParseNameMode)
-    return StringInit::get(Name);
+    return NameInit;
 
   if (Record *D = Records.getDef(Name))
     return DefInit::get(D);
@@ -763,7 +766,7 @@ Init *TGParser::ParseIDValue(Record *Cur
     return nullptr;
   }
 
-  return StringInit::get(Name);
+  return NameInit;
 }
 
 /// ParseOperation - Parse an operator.  This returns null on error.
@@ -1525,19 +1528,21 @@ Init *TGParser::ParseValue(Record *CurRe
       Lex.Lex();
       break;
     }
-    case tgtok::period:
+    case tgtok::period: {
       if (Lex.Lex() != tgtok::Id) {  // eat the .
         TokError("expected field identifier after '.'");
         return nullptr;
       }
-      if (!Result->getFieldType(Lex.getCurStrVal())) {
+      StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
+      if (!Result->getFieldType(FieldName)) {
         TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
                  Result->getAsString() + "'");
         return nullptr;
       }
-      Result = FieldInit::get(Result, Lex.getCurStrVal());
+      Result = FieldInit::get(Result, FieldName);
       Lex.Lex();  // eat field name
       break;
+    }
 
     case tgtok::paste:
       SMLoc PasteLoc = Lex.getLoc();




More information about the llvm-commits mailing list