Skip to content

Commit cacab8a

Browse files
authored
[TableGen][Decoder] Simplify parseFixedLenOperands (NFCI) (#156181)
Use information from CGIOperandList instead of re-parsing operand dags from scratch.
1 parent 912ce26 commit cacab8a

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,19 +1937,6 @@ static void addOneOperandFields(const Record *EncodingDef, const BitsInit &Bits,
19371937
}
19381938

19391939
void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
1940-
const Record &Def = *Inst->TheDef;
1941-
1942-
// Gather the outputs/inputs of the instruction, so we can find their
1943-
// positions in the encoding. This assumes for now that they appear in the
1944-
// MCInst in the order that they're listed.
1945-
std::vector<std::pair<const Init *, StringRef>> InOutOperands;
1946-
const DagInit *Out = Def.getValueAsDag("OutOperandList");
1947-
const DagInit *In = Def.getValueAsDag("InOperandList");
1948-
for (const auto &[Idx, Arg] : enumerate(Out->getArgs()))
1949-
InOutOperands.emplace_back(Arg, Out->getArgNameStr(Idx));
1950-
for (const auto &[Idx, Arg] : enumerate(In->getArgs()))
1951-
InOutOperands.emplace_back(Arg, In->getArgNameStr(Idx));
1952-
19531940
// Search for tied operands, so that we can correctly instantiate
19541941
// operands that are not explicitly represented in the encoding.
19551942
std::map<StringRef, StringRef> TiedNames;
@@ -1972,48 +1959,28 @@ void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
19721959
}
19731960

19741961
// For each operand, see if we can figure out where it is encoded.
1975-
for (const auto &Op : InOutOperands) {
1976-
const Init *OpInit = Op.first;
1977-
StringRef OpName = Op.second;
1978-
1979-
// We're ready to find the instruction encoding locations for this
1980-
// operand.
1981-
1982-
// First, find the operand type ("OpInit"), and sub-op names
1983-
// ("SubArgDag") if present.
1984-
const DagInit *SubArgDag = dyn_cast<DagInit>(OpInit);
1985-
if (SubArgDag)
1986-
OpInit = SubArgDag->getOperator();
1987-
const Record *OpTypeRec = cast<DefInit>(OpInit)->getDef();
1988-
// Lookup the sub-operands from the operand type record (note that only
1989-
// Operand subclasses have MIOperandInfo, see CodeGenInstruction.cpp).
1990-
const DagInit *SubOps = OpTypeRec->isSubClassOf("Operand")
1991-
? OpTypeRec->getValueAsDag("MIOperandInfo")
1992-
: nullptr;
1993-
1962+
for (const CGIOperandList::OperandInfo &Op : Inst->Operands) {
19941963
// Lookup the decoder method and construct a new OperandInfo to hold our
19951964
// result.
1996-
OperandInfo OpInfo = getOpInfo(OpTypeRec);
1965+
OperandInfo OpInfo = getOpInfo(Op.Rec);
19971966

19981967
// If we have named sub-operands...
1999-
if (SubArgDag) {
1968+
if (Op.MIOperandInfo && !Op.SubOpNames[0].empty()) {
20001969
// Then there should not be a custom decoder specified on the top-level
20011970
// type.
20021971
if (!OpInfo.Decoder.empty()) {
20031972
PrintError(EncodingDef,
2004-
"DecoderEmitter: operand \"" + OpName + "\" has type \"" +
2005-
OpInit->getAsString() +
1973+
"DecoderEmitter: operand \"" + Op.Name + "\" has type \"" +
1974+
Op.Rec->getName() +
20061975
"\" with a custom DecoderMethod, but also named "
20071976
"sub-operands.");
20081977
continue;
20091978
}
20101979

20111980
// Decode each of the sub-ops separately.
2012-
assert(SubOps && SubArgDag->getNumArgs() == SubOps->getNumArgs());
2013-
for (const auto &[I, Arg] : enumerate(SubOps->getArgs())) {
2014-
StringRef SubOpName = SubArgDag->getArgNameStr(I);
2015-
OperandInfo SubOpInfo = getOpInfo(cast<DefInit>(Arg)->getDef());
2016-
1981+
for (auto [SubOpName, SubOp] :
1982+
zip_equal(Op.SubOpNames, Op.MIOperandInfo->getArgs())) {
1983+
OperandInfo SubOpInfo = getOpInfo(cast<DefInit>(SubOp)->getDef());
20171984
addOneOperandFields(EncodingDef, Bits, TiedNames, SubOpName, SubOpInfo);
20181985
Operands.push_back(std::move(SubOpInfo));
20191986
}
@@ -2022,25 +1989,26 @@ void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
20221989

20231990
// Otherwise, if we have an operand with sub-operands, but they aren't
20241991
// named...
2025-
if (SubOps && OpInfo.Decoder.empty()) {
1992+
if (Op.MIOperandInfo && OpInfo.Decoder.empty()) {
20261993
// If it's a single sub-operand, and no custom decoder, use the decoder
20271994
// from the one sub-operand.
2028-
if (SubOps->getNumArgs() == 1)
2029-
OpInfo = getOpInfo(cast<DefInit>(SubOps->getArg(0))->getDef());
1995+
if (Op.MIOperandInfo->getNumArgs() == 1)
1996+
OpInfo =
1997+
getOpInfo(cast<DefInit>(Op.MIOperandInfo->getArg(0))->getDef());
20301998

20311999
// If we have multiple sub-ops, there'd better have a custom
20322000
// decoder. (Otherwise we don't know how to populate them properly...)
2033-
if (SubOps->getNumArgs() > 1) {
2001+
if (Op.MIOperandInfo->getNumArgs() > 1) {
20342002
PrintError(EncodingDef,
2035-
"DecoderEmitter: operand \"" + OpName +
2003+
"DecoderEmitter: operand \"" + Op.Name +
20362004
"\" uses MIOperandInfo with multiple ops, but doesn't "
20372005
"have a custom decoder!");
20382006
debugDumpRecord(*EncodingDef);
20392007
continue;
20402008
}
20412009
}
20422010

2043-
addOneOperandFields(EncodingDef, Bits, TiedNames, OpName, OpInfo);
2011+
addOneOperandFields(EncodingDef, Bits, TiedNames, Op.Name, OpInfo);
20442012
// FIXME: it should be an error not to find a definition for a given
20452013
// operand, rather than just failing to add it to the resulting
20462014
// instruction! (This is a longstanding bug, which will be addressed in an

0 commit comments

Comments
 (0)