From 2b8419a22d0fdb9b204ef0a0262a6ca22137b9a5 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Wed, 31 May 2017 19:01:11 +0000 Subject: [PATCH] [TableGen] Make Record::getValueAsString and getValueAsListOfStrings return StringRefs instead of std::string Internally both these methods just return the result of getValue on either a StringInit or a CodeInit object. In both cases this returns a StringRef pointing to a string allocated in the BumpPtrAllocator so its not going anywhere. So we can just pass that StringRef along. This is a fairly naive patch that targets just the build failures caused by this change. There's additional work that can be done to avoid creating std::string at call sites that still think getValueAsString returns a std::string. I'll try to clean those up in future patches. Differential Revision: https://reviews.llvm.org/D33710 llvm-svn: 304325 --- llvm/include/llvm/TableGen/Record.h | 4 +-- llvm/lib/TableGen/Record.cpp | 6 ++-- llvm/utils/TableGen/AsmMatcherEmitter.cpp | 8 ++++-- llvm/utils/TableGen/AsmWriterEmitter.cpp | 2 +- llvm/utils/TableGen/CodeEmitterGen.cpp | 6 ++-- llvm/utils/TableGen/CodeGenDAGPatterns.cpp | 4 ++- llvm/utils/TableGen/GlobalISelEmitter.cpp | 2 +- llvm/utils/TableGen/OptParserEmitter.cpp | 32 ++++++++++++---------- 8 files changed, 36 insertions(+), 28 deletions(-) diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index b81726e26eed..3e07e7072d64 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -1491,7 +1491,7 @@ public: /// its value as a string, throwing an exception if the field does not exist /// or if the value is not a string. /// - std::string getValueAsString(StringRef FieldName) const; + StringRef getValueAsString(StringRef FieldName) const; /// This method looks up the specified field and returns /// its value as a BitsInit, throwing an exception if the field does not exist @@ -1521,7 +1521,7 @@ public: /// returns its value as a vector of strings, throwing an exception if the /// field does not exist or if the value is not the right type. /// - std::vector getValueAsListOfStrings(StringRef FieldName) const; + std::vector getValueAsListOfStrings(StringRef FieldName) const; /// This method looks up the specified field and returns its /// value as a Record, throwing an exception if the field does not exist or if diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 06129c590950..b7432d5314cb 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -1708,7 +1708,7 @@ Init *Record::getValueInit(StringRef FieldName) const { return R->getValue(); } -std::string Record::getValueAsString(StringRef FieldName) const { +StringRef Record::getValueAsString(StringRef FieldName) const { const RecordVal *R = getValue(FieldName); if (!R || !R->getValue()) PrintFatalError(getLoc(), "Record `" + getName() + @@ -1787,10 +1787,10 @@ Record::getValueAsListOfInts(StringRef FieldName) const { return Ints; } -std::vector +std::vector Record::getValueAsListOfStrings(StringRef FieldName) const { ListInit *List = getValueAsListInit(FieldName); - std::vector Strings; + std::vector Strings; for (Init *I : List->getValues()) { if (StringInit *SI = dyn_cast(I)) Strings.push_back(SI->getValue()); diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp index 264175ae9677..3814ce8db6a2 100644 --- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp +++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp @@ -2486,14 +2486,18 @@ static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info, if (!MatchCode.empty()) MatchCode += "else "; MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n"; - MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n"; + MatchCode += " Mnemonic = \""; + MatchCode += R->getValueAsString("ToMnemonic"); + MatchCode += "\";\n"; } if (AliasWithNoPredicate != -1) { Record *R = ToVec[AliasWithNoPredicate]; if (!MatchCode.empty()) MatchCode += "else\n "; - MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n"; + MatchCode += "Mnemonic = \""; + MatchCode += R->getValueAsString("ToMnemonic"); + MatchCode += "\";\n"; } MatchCode += "return;"; diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp index 40b7857ab994..4b06e9e247f9 100644 --- a/llvm/utils/TableGen/AsmWriterEmitter.cpp +++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp @@ -523,7 +523,7 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName, // If the register has an alternate name for this index, use it. // Otherwise, leave it empty as an error flag. if (Idx < e) { - std::vector AltNames = + std::vector AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames"); if (AltNames.size() <= Idx) PrintFatalError(Reg.TheDef->getLoc(), diff --git a/llvm/utils/TableGen/CodeEmitterGen.cpp b/llvm/utils/TableGen/CodeEmitterGen.cpp index 565235d82143..26b007250eb5 100644 --- a/llvm/utils/TableGen/CodeEmitterGen.cpp +++ b/llvm/utils/TableGen/CodeEmitterGen.cpp @@ -278,11 +278,11 @@ void CodeEmitterGen::run(raw_ostream &o) { if (R->getValueAsString("Namespace") == "TargetOpcode" || R->getValueAsBit("isPseudo")) continue; - const std::string &InstName = R->getValueAsString("Namespace") + "::" - + R->getName().str(); + std::string InstName = + (R->getValueAsString("Namespace") + "::" + R->getName()).str(); std::string Case = getInstructionCase(R, Target); - CaseMap[Case].push_back(InstName); + CaseMap[Case].push_back(std::move(InstName)); } // Emit initial function code diff --git a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp index ef2cb4208eae..241fabf86314 100644 --- a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp +++ b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp @@ -893,7 +893,9 @@ std::string PatternToMatch::getPredicateCheck() const { for (Record *Pred : PredicateRecs) { if (!PredicateCheck.empty()) PredicateCheck += " && "; - PredicateCheck += "(" + Pred->getValueAsString("CondString") + ")"; + PredicateCheck += "("; + PredicateCheck += Pred->getValueAsString("CondString"); + PredicateCheck += ")"; } return PredicateCheck.str(); diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp index e0303b7b1ab4..88ded1f25ffb 100644 --- a/llvm/utils/TableGen/GlobalISelEmitter.cpp +++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp @@ -118,7 +118,7 @@ static std::string explainPredicates(const TreePatternNode *N) { std::string explainOperator(Record *Operator) { if (Operator->isSubClassOf("SDNode")) - return " (" + Operator->getValueAsString("Opcode") + ")"; + return (" (" + Operator->getValueAsString("Opcode") + ")").str(); if (Operator->isSubClassOf("Intrinsic")) return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp index c1b5e6510325..04e6537f3d15 100644 --- a/llvm/utils/TableGen/OptParserEmitter.cpp +++ b/llvm/utils/TableGen/OptParserEmitter.cpp @@ -21,6 +21,8 @@ using namespace llvm; // Ordering on Info. The logic should match with the consumer-side function in // llvm/Option/OptTable.h. +// FIXME: Mmake this take StringRefs instead of null terminated strings to +// simplify callers. static int StrCmpOptionName(const char *A, const char *B) { const char *X = A, *Y = B; char a = tolower(*A), b = tolower(*B); @@ -53,22 +55,22 @@ static int CompareOptionRecords(Record *const *Av, Record *const *Bv) { // Compare options by name, unless they are sentinels. if (!ASent) - if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(), - B->getValueAsString("Name").c_str())) + if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(), + B->getValueAsString("Name").str().c_str())) return Cmp; if (!ASent) { - std::vector APrefixes = A->getValueAsListOfStrings("Prefixes"); - std::vector BPrefixes = B->getValueAsListOfStrings("Prefixes"); + std::vector APrefixes = A->getValueAsListOfStrings("Prefixes"); + std::vector BPrefixes = B->getValueAsListOfStrings("Prefixes"); - for (std::vector::const_iterator APre = APrefixes.begin(), - AEPre = APrefixes.end(), - BPre = BPrefixes.begin(), - BEPre = BPrefixes.end(); - APre != AEPre && - BPre != BEPre; - ++APre, ++BPre) { - if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str())) + for (std::vector::const_iterator APre = APrefixes.begin(), + AEPre = APrefixes.end(), + BPre = BPrefixes.begin(), + BEPre = BPrefixes.end(); + APre != AEPre && + BPre != BEPre; + ++APre, ++BPre) { + if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str())) return Cmp; } } @@ -122,7 +124,7 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { unsigned CurPrefix = 0; for (unsigned i = 0, e = Opts.size(); i != e; ++i) { const Record &R = *Opts[i]; - std::vector prf = R.getValueAsListOfStrings("Prefixes"); + std::vector prf = R.getValueAsListOfStrings("Prefixes"); PrefixKeyT prfkey(prf.begin(), prf.end()); unsigned NewPrefix = CurPrefix + 1; if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") + @@ -207,7 +209,7 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { OS << "OPTION("; // The option prefix; - std::vector prf = R.getValueAsListOfStrings("Prefixes"); + std::vector prf = R.getValueAsListOfStrings("Prefixes"); OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", "; // The option string. @@ -240,7 +242,7 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { // would become "foo\0bar\0". Note that the compiler adds an implicit // terminating \0 at the end. OS << ", "; - std::vector AliasArgs = R.getValueAsListOfStrings("AliasArgs"); + std::vector AliasArgs = R.getValueAsListOfStrings("AliasArgs"); if (AliasArgs.size() == 0) { OS << "nullptr"; } else {