TableGen/TGParser: Prefer SmallVector/ArrayRef over std::vector

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288649 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Matthias Braun 2016-12-05 06:41:54 +00:00
parent 5a87cb27d9
commit a3f0e48f72
2 changed files with 81 additions and 68 deletions

View File

@ -36,7 +36,7 @@ namespace llvm {
struct SubClassReference {
SMRange RefRange;
Record *Rec;
std::vector<Init*> TemplateArgs;
SmallVector<Init*, 4> TemplateArgs;
SubClassReference() : Rec(nullptr) {}
@ -46,7 +46,7 @@ struct SubClassReference {
struct SubMultiClassReference {
SMRange RefRange;
MultiClass *MC;
std::vector<Init*> TemplateArgs;
SmallVector<Init*, 4> TemplateArgs;
SubMultiClassReference() : MC(nullptr) {}
@ -485,7 +485,7 @@ ParseSubClassReference(Record *CurRec, bool isDefm) {
return Result;
}
Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
if (Result.TemplateArgs.empty()) {
Result.Rec = nullptr; // Error parsing value list.
return Result;
@ -530,7 +530,7 @@ ParseSubMultiClassReference(MultiClass *CurMC) {
return Result;
}
Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
if (Result.TemplateArgs.empty()) {
Result.MC = nullptr; // Error parsing value list.
return Result;
@ -551,7 +551,7 @@ ParseSubMultiClassReference(MultiClass *CurMC) {
/// RangePiece ::= INTVAL
/// RangePiece ::= INTVAL '-' INTVAL
/// RangePiece ::= INTVAL INTVAL
bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
if (Lex.getCode() != tgtok::IntVal) {
TokError("expected integer or bitrange");
return true;
@ -595,26 +595,27 @@ bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
///
/// RangeList ::= RangePiece (',' RangePiece)*
///
std::vector<unsigned> TGParser::ParseRangeList() {
std::vector<unsigned> Result;
void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
// Parse the first piece.
if (ParseRangePiece(Result))
return std::vector<unsigned>();
if (ParseRangePiece(Result)) {
Result.clear();
return;
}
while (Lex.getCode() == tgtok::comma) {
Lex.Lex(); // Eat the comma.
// Parse the next range piece.
if (ParseRangePiece(Result))
return std::vector<unsigned>();
if (ParseRangePiece(Result)) {
Result.clear();
return;
}
}
return Result;
}
/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
/// OptionalRangeList ::= '<' RangeList '>'
/// OptionalRangeList ::= /*empty*/
bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
if (Lex.getCode() != tgtok::less)
return false;
@ -622,7 +623,7 @@ bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
Lex.Lex(); // eat the '<'
// Parse the range list.
Ranges = ParseRangeList();
ParseRangeList(Ranges);
if (Ranges.empty()) return true;
if (Lex.getCode() != tgtok::greater) {
@ -636,7 +637,7 @@ bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
/// OptionalBitList ::= '{' RangeList '}'
/// OptionalBitList ::= /*empty*/
bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
if (Lex.getCode() != tgtok::l_brace)
return false;
@ -644,7 +645,7 @@ bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
Lex.Lex(); // eat the '{'
// Parse the range list.
Ranges = ParseRangeList();
ParseRangeList(Ranges);
if (Ranges.empty()) return true;
if (Lex.getCode() != tgtok::r_brace) {
@ -1205,8 +1206,9 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
return nullptr;
}
std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
if (ValueList.empty()) return nullptr;
SubClassReference SCRef;
ParseValueList(SCRef.TemplateArgs, CurRec, Class);
if (SCRef.TemplateArgs.empty()) return nullptr;
if (Lex.getCode() != tgtok::greater) {
TokError("expected '>' at end of value list");
@ -1219,10 +1221,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
Records, /*IsAnonymous=*/true);
Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
SubClassReference SCRef;
SCRef.RefRange = SMRange(NameLoc, EndLoc);
SCRef.Rec = Class;
SCRef.TemplateArgs = ValueList;
// Add info about the subclass to NewRec.
if (AddSubClass(NewRec, SCRef))
return nullptr;
@ -1264,10 +1264,10 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
case tgtok::l_brace: { // Value ::= '{' ValueList '}'
SMLoc BraceLoc = Lex.getLoc();
Lex.Lex(); // eat the '{'
std::vector<Init*> Vals;
SmallVector<Init*, 16> Vals;
if (Lex.getCode() != tgtok::r_brace) {
Vals = ParseValueList(CurRec);
ParseValueList(Vals, CurRec);
if (Vals.empty()) return nullptr;
}
if (Lex.getCode() != tgtok::r_brace) {
@ -1314,7 +1314,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
}
case tgtok::l_square: { // Value ::= '[' ValueList ']'
Lex.Lex(); // eat the '['
std::vector<Init*> Vals;
SmallVector<Init*, 16> Vals;
RecTy *DeducedEltTy = nullptr;
ListRecTy *GivenListTy = nullptr;
@ -1330,8 +1330,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
}
if (Lex.getCode() != tgtok::r_square) {
Vals = ParseValueList(CurRec, nullptr,
GivenListTy ? GivenListTy->getElementType() : nullptr);
ParseValueList(Vals, CurRec, nullptr,
GivenListTy ? GivenListTy->getElementType() : nullptr);
if (Vals.empty()) return nullptr;
}
if (Lex.getCode() != tgtok::r_square) {
@ -1489,7 +1489,8 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
SMLoc CurlyLoc = Lex.getLoc();
Lex.Lex(); // eat the '{'
std::vector<unsigned> Ranges = ParseRangeList();
SmallVector<unsigned, 16> Ranges;
ParseRangeList(Ranges);
if (Ranges.empty()) return nullptr;
// Reverse the bitlist.
@ -1511,7 +1512,8 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
case tgtok::l_square: {
SMLoc SquareLoc = Lex.getLoc();
Lex.Lex(); // eat the '['
std::vector<unsigned> Ranges = ParseRangeList();
SmallVector<unsigned, 16> Ranges;
ParseRangeList(Ranges);
if (Ranges.empty()) return nullptr;
Result = Result->convertInitListSlice(Ranges);
@ -1647,16 +1649,16 @@ void TGParser::ParseDagArgList(
///
/// ValueList ::= Value (',' Value)
///
std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
RecTy *EltTy) {
std::vector<Init*> Result;
void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
Record *ArgsRec, RecTy *EltTy) {
RecTy *ItemType = EltTy;
unsigned int ArgN = 0;
if (ArgsRec && !EltTy) {
ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
if (TArgs.empty()) {
TokError("template argument provided to non-template class");
return std::vector<Init*>();
Result.clear();
return;
}
const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
if (!RV) {
@ -1668,7 +1670,10 @@ std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
++ArgN;
}
Result.push_back(ParseValue(CurRec, ItemType));
if (!Result.back()) return std::vector<Init*>();
if (!Result.back()) {
Result.clear();
return;
}
while (Lex.getCode() == tgtok::comma) {
Lex.Lex(); // Eat the comma
@ -1677,7 +1682,8 @@ std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
if (ArgN >= TArgs.size()) {
TokError("too many template arguments");
return std::vector<Init*>();
Result.clear();
return;
}
const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
assert(RV && "Template argument record not found??");
@ -1685,10 +1691,11 @@ std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
++ArgN;
}
Result.push_back(ParseValue(CurRec, ItemType));
if (!Result.back()) return std::vector<Init*>();
if (!Result.back()) {
Result.clear();
return;
}
}
return Result;
}
/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
@ -1774,7 +1781,7 @@ VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
Lex.Lex(); // Eat the '='
RecTy *IterType = nullptr;
std::vector<unsigned> Ranges;
SmallVector<unsigned, 16> Ranges;
switch (Lex.getCode()) {
default: TokError("Unknown token when expecting a range list"); return nullptr;
@ -1803,7 +1810,7 @@ VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
case tgtok::l_brace: { // '{' RangeList '}'
Lex.Lex(); // eat the '{'
Ranges = ParseRangeList();
ParseRangeList(Ranges);
if (Lex.getCode() != tgtok::r_brace) {
TokError("expected '}' at end of bit range list");
return nullptr;
@ -1887,7 +1894,7 @@ bool TGParser::ParseBodyItem(Record *CurRec) {
std::string FieldName = Lex.getCurStrVal();
Lex.Lex(); // eat the field name.
std::vector<unsigned> BitList;
SmallVector<unsigned, 16> BitList;
if (ParseOptionalBitList(BitList))
return true;
std::reverse(BitList.begin(), BitList.end());
@ -1943,7 +1950,7 @@ bool TGParser::ParseBody(Record *CurRec) {
/// \brief Apply the current let bindings to \a CurRec.
/// \returns true on error, false otherwise.
bool TGParser::ApplyLetStack(Record *CurRec) {
for (std::vector<LetRecord> &LetInfo : LetStack)
for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
for (LetRecord &LR : LetInfo)
if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
return true;
@ -2154,38 +2161,44 @@ bool TGParser::ParseClass() {
/// LetList ::= LetItem (',' LetItem)*
/// LetItem ::= ID OptionalRangeList '=' Value
///
std::vector<LetRecord> TGParser::ParseLetList() {
std::vector<LetRecord> Result;
void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
while (true) {
if (Lex.getCode() != tgtok::Id) {
TokError("expected identifier in let definition");
return std::vector<LetRecord>();
Result.clear();
return;
}
std::string Name = Lex.getCurStrVal();
SMLoc NameLoc = Lex.getLoc();
Lex.Lex(); // Eat the identifier.
// Check for an optional RangeList.
std::vector<unsigned> Bits;
if (ParseOptionalRangeList(Bits))
return std::vector<LetRecord>();
SmallVector<unsigned, 16> Bits;
if (ParseOptionalRangeList(Bits)) {
Result.clear();
return;
}
std::reverse(Bits.begin(), Bits.end());
if (Lex.getCode() != tgtok::equal) {
TokError("expected '=' in let expression");
return std::vector<LetRecord>();
Result.clear();
return;
}
Lex.Lex(); // eat the '='.
Init *Val = ParseValue(nullptr);
if (!Val) return std::vector<LetRecord>();
if (!Val) {
Result.clear();
return;
}
// Now that we have everything, add the record.
Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
Result.emplace_back(std::move(Name), Bits, Val, NameLoc);
if (Lex.getCode() != tgtok::comma)
return Result;
return;
Lex.Lex(); // eat the comma.
}
}
@ -2201,7 +2214,8 @@ bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Lex.Lex();
// Add this entry to the let stack.
std::vector<LetRecord> LetInfo = ParseLetList();
SmallVector<LetRecord, 8> LetInfo;
ParseLetList(LetInfo);
if (LetInfo.empty()) return true;
LetStack.push_back(std::move(LetInfo));
@ -2327,7 +2341,7 @@ Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
Init *&DefmPrefix,
SMRange DefmPrefixRange,
ArrayRef<Init *> TArgs,
std::vector<Init *> &TemplateVals) {
ArrayRef<Init *> TemplateVals) {
// We need to preserve DefProto so it can be reused for later
// instantiations, so create a new Record to inherit from it.
@ -2448,7 +2462,7 @@ Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
ArrayRef<Init *> TArgs,
std::vector<Init *> &TemplateVals,
ArrayRef<Init *> TemplateVals,
bool DeleteArgs) {
// Loop over all of the template arguments, setting them to the specified
// value or leaving them as the default if necessary.
@ -2542,7 +2556,7 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
// template parameters.
MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
assert(MC && "Didn't lookup multiclass correctly?");
std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
// Verify that the correct number of template arguments were specified.
ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();

View File

@ -36,7 +36,7 @@ namespace llvm {
std::vector<unsigned> Bits;
Init *Value;
SMLoc Loc;
LetRecord(StringRef N, const std::vector<unsigned> &B, Init *V, SMLoc L)
LetRecord(StringRef N, ArrayRef<unsigned> B, Init *V, SMLoc L)
: Name(N), Bits(B), Value(V), Loc(L) {
}
};
@ -53,7 +53,7 @@ namespace llvm {
class TGParser {
TGLexer Lex;
std::vector<std::vector<LetRecord> > LetStack;
std::vector<SmallVector<LetRecord, 4>> LetStack;
std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
/// Loops - Keep track of any foreach loops we are within.
@ -140,12 +140,11 @@ private: // Parser methods.
Record *InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
Init *&DefmPrefix, SMRange DefmPrefixRange,
ArrayRef<Init *> TArgs,
std::vector<Init *> &TemplateVals);
ArrayRef<Init *> TemplateVals);
bool ResolveMulticlassDefArgs(MultiClass &MC, Record *DefProto,
SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
ArrayRef<Init *> TArgs,
std::vector<Init *> &TemplateVals,
bool DeleteArgs);
ArrayRef<Init *> TemplateVals, bool DeleteArgs);
bool ResolveMulticlassDef(MultiClass &MC,
Record *CurRec,
Record *DefProto,
@ -154,7 +153,7 @@ private: // Parser methods.
bool ParseDef(MultiClass *CurMultiClass);
bool ParseForeach(MultiClass *CurMultiClass);
bool ParseTopLevelLet(MultiClass *CurMultiClass);
std::vector<LetRecord> ParseLetList();
void ParseLetList(SmallVectorImpl<LetRecord> &Result);
bool ParseObjectBody(Record *CurRec);
bool ParseBody(Record *CurRec);
@ -173,15 +172,15 @@ private: // Parser methods.
IDParseMode Mode = ParseValueMode);
Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
IDParseMode Mode = ParseValueMode);
std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = nullptr,
RecTy *EltTy = nullptr);
void ParseValueList(SmallVectorImpl<llvm::Init*> &Result, Record *CurRec,
Record *ArgsRec = nullptr, RecTy *EltTy = nullptr);
void ParseDagArgList(
SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
Record *CurRec);
bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
std::vector<unsigned> ParseRangeList();
bool ParseRangePiece(std::vector<unsigned> &Ranges);
bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
void ParseRangeList(SmallVectorImpl<unsigned> &Result);
bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges);
RecTy *ParseType();
Init *ParseOperation(Record *CurRec, RecTy *ItemType);
RecTy *ParseOperatorType();