[llvm] cacab8a - [TableGen][Decoder] Simplify parseFixedLenOperands (NFCI) (#156181)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 30 07:17:34 PDT 2025


Author: Sergei Barannikov
Date: 2025-08-30T14:17:30Z
New Revision: cacab8a86fc2c6107cf7e9edb746f9634a6aa97a

URL: https://github.com/llvm/llvm-project/commit/cacab8a86fc2c6107cf7e9edb746f9634a6aa97a
DIFF: https://github.com/llvm/llvm-project/commit/cacab8a86fc2c6107cf7e9edb746f9634a6aa97a.diff

LOG: [TableGen][Decoder] Simplify parseFixedLenOperands (NFCI) (#156181)

Use information from CGIOperandList instead of re-parsing operand dags
from scratch.

Added: 
    

Modified: 
    llvm/utils/TableGen/DecoderEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index f1db0368d0988..98569abfbb1a4 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1937,19 +1937,6 @@ static void addOneOperandFields(const Record *EncodingDef, const BitsInit &Bits,
 }
 
 void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
-  const Record &Def = *Inst->TheDef;
-
-  // Gather the outputs/inputs of the instruction, so we can find their
-  // positions in the encoding.  This assumes for now that they appear in the
-  // MCInst in the order that they're listed.
-  std::vector<std::pair<const Init *, StringRef>> InOutOperands;
-  const DagInit *Out = Def.getValueAsDag("OutOperandList");
-  const DagInit *In = Def.getValueAsDag("InOperandList");
-  for (const auto &[Idx, Arg] : enumerate(Out->getArgs()))
-    InOutOperands.emplace_back(Arg, Out->getArgNameStr(Idx));
-  for (const auto &[Idx, Arg] : enumerate(In->getArgs()))
-    InOutOperands.emplace_back(Arg, In->getArgNameStr(Idx));
-
   // Search for tied operands, so that we can correctly instantiate
   // operands that are not explicitly represented in the encoding.
   std::map<StringRef, StringRef> TiedNames;
@@ -1972,48 +1959,28 @@ void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
   }
 
   // For each operand, see if we can figure out where it is encoded.
-  for (const auto &Op : InOutOperands) {
-    const Init *OpInit = Op.first;
-    StringRef OpName = Op.second;
-
-    // We're ready to find the instruction encoding locations for this
-    // operand.
-
-    // First, find the operand type ("OpInit"), and sub-op names
-    // ("SubArgDag") if present.
-    const DagInit *SubArgDag = dyn_cast<DagInit>(OpInit);
-    if (SubArgDag)
-      OpInit = SubArgDag->getOperator();
-    const Record *OpTypeRec = cast<DefInit>(OpInit)->getDef();
-    // Lookup the sub-operands from the operand type record (note that only
-    // Operand subclasses have MIOperandInfo, see CodeGenInstruction.cpp).
-    const DagInit *SubOps = OpTypeRec->isSubClassOf("Operand")
-                                ? OpTypeRec->getValueAsDag("MIOperandInfo")
-                                : nullptr;
-
+  for (const CGIOperandList::OperandInfo &Op : Inst->Operands) {
     // Lookup the decoder method and construct a new OperandInfo to hold our
     // result.
-    OperandInfo OpInfo = getOpInfo(OpTypeRec);
+    OperandInfo OpInfo = getOpInfo(Op.Rec);
 
     // If we have named sub-operands...
-    if (SubArgDag) {
+    if (Op.MIOperandInfo && !Op.SubOpNames[0].empty()) {
       // Then there should not be a custom decoder specified on the top-level
       // type.
       if (!OpInfo.Decoder.empty()) {
         PrintError(EncodingDef,
-                   "DecoderEmitter: operand \"" + OpName + "\" has type \"" +
-                       OpInit->getAsString() +
+                   "DecoderEmitter: operand \"" + Op.Name + "\" has type \"" +
+                       Op.Rec->getName() +
                        "\" with a custom DecoderMethod, but also named "
                        "sub-operands.");
         continue;
       }
 
       // Decode each of the sub-ops separately.
-      assert(SubOps && SubArgDag->getNumArgs() == SubOps->getNumArgs());
-      for (const auto &[I, Arg] : enumerate(SubOps->getArgs())) {
-        StringRef SubOpName = SubArgDag->getArgNameStr(I);
-        OperandInfo SubOpInfo = getOpInfo(cast<DefInit>(Arg)->getDef());
-
+      for (auto [SubOpName, SubOp] :
+           zip_equal(Op.SubOpNames, Op.MIOperandInfo->getArgs())) {
+        OperandInfo SubOpInfo = getOpInfo(cast<DefInit>(SubOp)->getDef());
         addOneOperandFields(EncodingDef, Bits, TiedNames, SubOpName, SubOpInfo);
         Operands.push_back(std::move(SubOpInfo));
       }
@@ -2022,17 +1989,18 @@ void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
 
     // Otherwise, if we have an operand with sub-operands, but they aren't
     // named...
-    if (SubOps && OpInfo.Decoder.empty()) {
+    if (Op.MIOperandInfo && OpInfo.Decoder.empty()) {
       // If it's a single sub-operand, and no custom decoder, use the decoder
       // from the one sub-operand.
-      if (SubOps->getNumArgs() == 1)
-        OpInfo = getOpInfo(cast<DefInit>(SubOps->getArg(0))->getDef());
+      if (Op.MIOperandInfo->getNumArgs() == 1)
+        OpInfo =
+            getOpInfo(cast<DefInit>(Op.MIOperandInfo->getArg(0))->getDef());
 
       // If we have multiple sub-ops, there'd better have a custom
       // decoder. (Otherwise we don't know how to populate them properly...)
-      if (SubOps->getNumArgs() > 1) {
+      if (Op.MIOperandInfo->getNumArgs() > 1) {
         PrintError(EncodingDef,
-                   "DecoderEmitter: operand \"" + OpName +
+                   "DecoderEmitter: operand \"" + Op.Name +
                        "\" uses MIOperandInfo with multiple ops, but doesn't "
                        "have a custom decoder!");
         debugDumpRecord(*EncodingDef);
@@ -2040,7 +2008,7 @@ void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {
       }
     }
 
-    addOneOperandFields(EncodingDef, Bits, TiedNames, OpName, OpInfo);
+    addOneOperandFields(EncodingDef, Bits, TiedNames, Op.Name, OpInfo);
     // FIXME: it should be an error not to find a definition for a given
     // operand, rather than just failing to add it to the resulting
     // instruction! (This is a longstanding bug, which will be addressed in an


        


More information about the llvm-commits mailing list