From 6a441839a6a75f3844116a30703cf14027325b0d Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 5 Dec 2016 06:00:36 +0000 Subject: [PATCH] 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. llvm-svn: 288642 --- llvm/include/llvm/TableGen/Record.h | 32 ++++++++++++++------------------ llvm/lib/TableGen/Record.cpp | 27 ++++++++++----------------- llvm/lib/TableGen/TGParser.cpp | 25 +++++++++++++++---------- 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index bd7fcff..1cc6c22 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -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 &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, const RecordKeeper &RK); 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 diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 8c2f5a1..f84a917 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -678,9 +678,7 @@ Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { if (IntInit *LHSi = dyn_cast(LHS)) return StringInit::get(LHSi->getAsString()); } else { - if (StringInit *LHSs = dyn_cast(LHS)) { - StringRef Name = LHSs->getValue(); - + if (StringInit *Name = dyn_cast(LHS)) { // From TGParser::ParseIDValue if (CurRec) { if (const RecordVal *RV = CurRec->getValue(Name)) { @@ -718,11 +716,11 @@ Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { } } 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(getType())) { @@ -1175,7 +1173,7 @@ std::string TernOpInit::getAsString() const { RHS->getAsString() + ")"; } -RecTy *TypedInit::getFieldType(StringRef FieldName) const { +RecTy *TypedInit::getFieldType(StringInit *FieldName) const { if (RecordRecTy *RecordType = dyn_cast(getType())) if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName)) return Field->getType(); @@ -1348,7 +1346,7 @@ Init *VarInit::resolveListElementReference(Record &R, return nullptr; } -RecTy *VarInit::getFieldType(StringRef FieldName) const { +RecTy *VarInit::getFieldType(StringInit *FieldName) const { if (RecordRecTy *RTy = dyn_cast(getType())) if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) return RV->getType(); @@ -1356,7 +1354,7 @@ RecTy *VarInit::getFieldType(StringRef FieldName) const { } Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, - StringRef FieldName) const { + StringInit *FieldName) const { if (isa(getType())) if (const RecordVal *Val = R.getValue(VarName)) { if (RV != Val && (RV || isa(Val->getValue()))) @@ -1464,14 +1462,14 @@ Init *DefInit::convertInitializerTo(RecTy *Ty) const { 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 Key; +FieldInit *FieldInit::get(Init *R, StringInit *FN) { + typedef std::pair Key; static DenseMap ThePool; Key TheKey(std::make_pair(R, FN)); @@ -1965,8 +1963,3 @@ Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 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); -} diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index 3d0e487..5b8d730 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -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 *CurRec, StringRef Name, SMLoc NameLoc, 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 *CurRec, StringRef Name, SMLoc NameLoc, // If this is in a foreach loop, make sure it's not a loop iterator for (const auto &L : Loops) { VarInit *IterVar = dyn_cast(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 *CurRec, StringRef Name, SMLoc NameLoc, 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 *CurRec, RecTy *ItemType, IDParseMode Mode) { 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(); -- 2.7.4