[llvm] d1fbdf5 - [llvm-tblgen] NFC: Small code refactor in DecoderEmitter.

James Y Knight via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 2 10:59:42 PDT 2022


Author: James Y Knight
Date: 2022-11-02T13:59:27-04:00
New Revision: d1fbdf5bf79219549bc1fde255186d02f646a46f

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

LOG: [llvm-tblgen] NFC: Small code refactor in DecoderEmitter.

Extracts part of populateInstruction into a separate
addOneOperandFields function.

Added: 
    

Modified: 
    llvm/utils/TableGen/DecoderEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 05d81bae0a9d3..f46cb4c77f6dd 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1906,6 +1906,69 @@ void parseVarLenInstOperand(const Record &Def,
   }
 }
 
+static void addOneOperandFields(const Record &EncodingDef, const BitsInit &Bits,
+                                std::map<std::string, std::string> &TiedNames,
+                                StringRef OpName, OperandInfo &OpInfo) {
+  // Some bits of the operand may be required to be 1 depending on the
+  // instruction's encoding. Collect those bits.
+  if (const RecordVal *EncodedValue = EncodingDef.getValue(OpName))
+    if (const BitsInit *OpBits = dyn_cast<BitsInit>(EncodedValue->getValue()))
+      for (unsigned I = 0; I < OpBits->getNumBits(); ++I)
+        if (const BitInit *OpBit = dyn_cast<BitInit>(OpBits->getBit(I)))
+          if (OpBit->getValue())
+            OpInfo.InitValue |= 1ULL << I;
+
+  unsigned Base = ~0U;
+  unsigned Width = 0;
+  unsigned Offset = 0;
+
+  for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
+    VarInit *Var = nullptr;
+    VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
+    if (BI)
+      Var = dyn_cast<VarInit>(BI->getBitVar());
+    else
+      Var = dyn_cast<VarInit>(Bits.getBit(bi));
+
+    if (!Var) {
+      if (Base != ~0U) {
+        OpInfo.addField(Base, Width, Offset);
+        Base = ~0U;
+        Width = 0;
+        Offset = 0;
+      }
+      continue;
+    }
+
+    if ((Var->getName() != OpName &&
+         Var->getName() != TiedNames[std::string(OpName)])) {
+      if (Base != ~0U) {
+        OpInfo.addField(Base, Width, Offset);
+        Base = ~0U;
+        Width = 0;
+        Offset = 0;
+      }
+      continue;
+    }
+
+    if (Base == ~0U) {
+      Base = bi;
+      Width = 1;
+      Offset = BI ? BI->getBitNum() : 0;
+    } else if (BI && BI->getBitNum() != Offset + Width) {
+      OpInfo.addField(Base, Width, Offset);
+      Base = bi;
+      Width = 1;
+      Offset = BI->getBitNum();
+    } else {
+      ++Width;
+    }
+  }
+
+  if (Base != ~0U)
+    OpInfo.addField(Base, Width, Offset);
+}
+
 static unsigned
 populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
                     const CodeGenInstruction &CGI, unsigned Opc,
@@ -2119,21 +2182,24 @@ populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
 
     // For each operand, see if we can figure out where it is encoded.
     for (const auto &Op : InOutOperands) {
+      Init *OpInit = Op.first;
+      StringRef OpName = Op.second;
+
       if (SupportPositionalDecoding) {
-        if (!NumberedInsnOperands[std::string(Op.second)].empty()) {
+        if (!NumberedInsnOperands[std::string(OpName)].empty()) {
           llvm::append_range(InsnOperands,
-                             NumberedInsnOperands[std::string(Op.second)]);
+                             NumberedInsnOperands[std::string(OpName)]);
           continue;
         }
-        if (!NumberedInsnOperands[TiedNames[std::string(Op.second)]].empty()) {
+        if (!NumberedInsnOperands[TiedNames[std::string(OpName)]].empty()) {
           if (!NumberedInsnOperandsNoTie.count(
-                  TiedNames[std::string(Op.second)])) {
+                  TiedNames[std::string(OpName)])) {
             // Figure out to which (sub)operand we're tied.
             unsigned i =
-                CGI.Operands.getOperandNamed(TiedNames[std::string(Op.second)]);
+                CGI.Operands.getOperandNamed(TiedNames[std::string(OpName)]);
             int tiedTo = CGI.Operands[i].getTiedRegister();
             if (tiedTo == -1) {
-              i = CGI.Operands.getOperandNamed(Op.second);
+              i = CGI.Operands.getOperandNamed(OpName);
               tiedTo = CGI.Operands[i].getTiedRegister();
             }
 
@@ -2142,7 +2208,7 @@ populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
                   CGI.Operands.getSubOperandNumber(tiedTo);
 
               InsnOperands.push_back(
-                  NumberedInsnOperands[TiedNames[std::string(Op.second)]]
+                  NumberedInsnOperands[TiedNames[std::string(OpName)]]
                                       [SO.second]);
             }
           }
@@ -2154,76 +2220,15 @@ populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
       // to interpret it.  As a first step, require the target to provide
       // callbacks for decoding register classes.
 
-      Init *OpInit = Op.first;
       if (DagInit *Dag = dyn_cast<DagInit>(OpInit))
         OpInit = Dag->getOperator();
       OperandInfo OpInfo = getOpInfo(cast<DefInit>(OpInit)->getDef());
 
-      // Some bits of the operand may be required to be 1 depending on the
-      // instruction's encoding. Collect those bits.
-      if (const RecordVal *EncodedValue = EncodingDef.getValue(Op.second))
-        if (const BitsInit *OpBits =
-                dyn_cast<BitsInit>(EncodedValue->getValue()))
-          for (unsigned I = 0; I < OpBits->getNumBits(); ++I)
-            if (const BitInit *OpBit = dyn_cast<BitInit>(OpBits->getBit(I)))
-              if (OpBit->getValue())
-                OpInfo.InitValue |= 1ULL << I;
-
-      unsigned Base = ~0U;
-      unsigned Width = 0;
-      unsigned Offset = 0;
-
-      for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
-        VarInit *Var = nullptr;
-        VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
-        if (BI)
-          Var = dyn_cast<VarInit>(BI->getBitVar());
-        else
-          Var = dyn_cast<VarInit>(Bits.getBit(bi));
-
-        if (!Var) {
-          if (Base != ~0U) {
-            OpInfo.addField(Base, Width, Offset);
-            Base = ~0U;
-            Width = 0;
-            Offset = 0;
-          }
-          continue;
-        }
-
-        if ((Var->getName() != Op.second &&
-             Var->getName() != TiedNames[std::string(Op.second)])) {
-          if (Base != ~0U) {
-            OpInfo.addField(Base, Width, Offset);
-            Base = ~0U;
-            Width = 0;
-            Offset = 0;
-          }
-          continue;
-        }
-
-        if (Base == ~0U) {
-          Base = bi;
-          Width = 1;
-          Offset = BI ? BI->getBitNum() : 0;
-        } else if (BI && BI->getBitNum() != Offset + Width) {
-          OpInfo.addField(Base, Width, Offset);
-          Base = bi;
-          Width = 1;
-          Offset = BI->getBitNum();
-        } else {
-          ++Width;
-        }
-      }
-
-      if (Base != ~0U)
-        OpInfo.addField(Base, Width, Offset);
-
+      addOneOperandFields(EncodingDef, Bits, TiedNames, OpName, OpInfo);
       if (OpInfo.numFields() > 0)
         InsnOperands.push_back(OpInfo);
     }
   }
-
   Operands[Opc] = InsnOperands;
 
 #if 0


        


More information about the llvm-commits mailing list