[PATCH] D127857: [NFC][TableGen] Optimize DecoderEmmitter

Senran Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 15 07:05:00 PDT 2022


zsrkmyn created this revision.
Herald added a project: All.
zsrkmyn requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

1. Replace std::string with StringRef;
2. Hoist a map query out of loop & avoid unnecessary element construction.


https://reviews.llvm.org/D127857

Files:
  llvm/utils/TableGen/DecoderEmitter.cpp


Index: llvm/utils/TableGen/DecoderEmitter.cpp
===================================================================
--- llvm/utils/TableGen/DecoderEmitter.cpp
+++ llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1957,24 +1957,22 @@
 
   // Search for tied operands, so that we can correctly instantiate
   // operands that are not explicitly represented in the encoding.
-  std::map<std::string, std::string> TiedNames;
+  std::map<StringRef, StringRef> TiedNames;
   for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
     int tiedTo = CGI.Operands[i].getTiedRegister();
     if (tiedTo != -1) {
       std::pair<unsigned, unsigned> SO =
         CGI.Operands.getSubOperandNumber(tiedTo);
-      TiedNames[std::string(InOutOperands[i].second)] =
-          std::string(InOutOperands[SO.first].second);
-      TiedNames[std::string(InOutOperands[SO.first].second)] =
-          std::string(InOutOperands[i].second);
+      TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
+      TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
     }
   }
 
   if (IsVarLenInst) {
     parseVarLenInstOperand(EncodingDef, InsnOperands, CGI);
   } else {
-    std::map<std::string, std::vector<OperandInfo>> NumberedInsnOperands;
-    std::set<std::string> NumberedInsnOperandsNoTie;
+    std::map<StringRef, std::vector<OperandInfo>> NumberedInsnOperands;
+    std::set<StringRef> NumberedInsnOperandsNoTie;
     if (Target.getInstructionSet()->getValueAsBit(
             "decodePositionallyEncodedOperands")) {
       const std::vector<RecordVal> &Vals = Def.getValues();
@@ -2120,17 +2118,14 @@
 
     // For each operand, see if we can figure out where it is encoded.
     for (const auto &Op : InOutOperands) {
-      if (!NumberedInsnOperands[std::string(Op.second)].empty()) {
-        llvm::append_range(InsnOperands,
-                           NumberedInsnOperands[std::string(Op.second)]);
+      if (!NumberedInsnOperands[Op.second].empty()) {
+        llvm::append_range(InsnOperands, NumberedInsnOperands[Op.second]);
         continue;
       }
-      if (!NumberedInsnOperands[TiedNames[std::string(Op.second)]].empty()) {
-        if (!NumberedInsnOperandsNoTie.count(
-                TiedNames[std::string(Op.second)])) {
+      if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
+        if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
           // Figure out to which (sub)operand we're tied.
-          unsigned i =
-              CGI.Operands.getOperandNamed(TiedNames[std::string(Op.second)]);
+          unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
           int tiedTo = CGI.Operands[i].getTiedRegister();
           if (tiedTo == -1) {
             i = CGI.Operands.getOperandNamed(Op.second);
@@ -2142,8 +2137,7 @@
                 CGI.Operands.getSubOperandNumber(tiedTo);
 
             InsnOperands.push_back(
-                NumberedInsnOperands[TiedNames[std::string(Op.second)]]
-                                    [SO.second]);
+                NumberedInsnOperands[TiedNames[Op.second]][SO.second]);
           }
         }
         continue;
@@ -2169,6 +2163,8 @@
       unsigned Width = 0;
       unsigned Offset = 0;
 
+      auto It = TiedNames.find(Op.second);
+      StringRef TiedName = It == TiedNames.end() ? It->second : "";
       for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
         VarInit *Var = nullptr;
         VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
@@ -2187,8 +2183,7 @@
           continue;
         }
 
-        if ((Var->getName() != Op.second &&
-             Var->getName() != TiedNames[std::string(Op.second)])) {
+        if ((Var->getName() != Op.second && Var->getName() != TiedName)) {
           if (Base != ~0U) {
             OpInfo.addField(Base, Width, Offset);
             Base = ~0U;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127857.437143.patch
Type: text/x-patch
Size: 3847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220615/1e1cefd5/attachment.bin>


More information about the llvm-commits mailing list