[llvm] [TableGen][DecoderEmitter] Inline a couple of small functions (NFC) (PR #155100)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 23 06:41:43 PDT 2025


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/155100

So that `emitTableEntries()` calls itself directly rather than through other functions. This should make the code a little easier to follow.

>From 8d1f5d1e1b2de8c209c71da75b18038eec20e67c Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Sat, 23 Aug 2025 08:53:23 +0300
Subject: [PATCH] [TableGen][DecoderEmitter] Inline a couple of small functions
 (NFC)

So that `emitTableEntries()` calls itself directly rather than through
other functions. This should make the code a little easier to follow.
---
 llvm/utils/TableGen/DecoderEmitter.cpp | 64 ++++++++++----------------
 1 file changed, 24 insertions(+), 40 deletions(-)

diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 84a9d35436bef..47020d716f4c0 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -490,6 +490,7 @@ class FilterChooser {
   /// If the selected filter matches multiple encodings, and there is
   /// *exactly one* encoding in which all bits are known in the filtered range,
   /// then this is the ID of that encoding.
+  /// Also used when there is only one encoding.
   std::optional<unsigned> SingletonEncodingID;
 
   /// If the selected filter matches multiple encodings, and there is
@@ -591,13 +592,6 @@ class FilterChooser {
   void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
                                unsigned EncodingID) const;
 
-  // Emits code to decode the singleton, and then to decode the rest.
-  void emitSingletonTableEntry(DecoderTableInfo &TableInfo) const;
-
-  // Emit table entries to decode instructions given a segment or segments of
-  // bits.
-  void emitTableEntry(DecoderTableInfo &TableInfo) const;
-
   void emitBinaryParser(raw_ostream &OS, indent Indent,
                         const OperandInfo &OpInfo) const;
 
@@ -624,8 +618,7 @@ class FilterChooser {
   void doFilter();
 
 public:
-  // emitTableEntries - Emit state machine entries to decode our share of
-  // instructions.
+  /// Emits state machine entries to decode our share of instructions.
   void emitTableEntries(DecoderTableInfo &TableInfo) const;
 
   void dump() const;
@@ -702,7 +695,25 @@ void FilterChooser::applyFilter(const Filter &F) {
 
 // Emit table entries to decode instructions given a segment or segments
 // of bits.
-void FilterChooser::emitTableEntry(DecoderTableInfo &TableInfo) const {
+void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
+  if (SingletonEncodingID) {
+    // There is only one encoding in which all bits in the filtered range
+    // are fully defined, but we still need to check the remaining bits
+    // to see if the encoding matches. We also need to check predicates etc.
+    // If there are other encodings that could match if this one doesn't,
+    // enter a scope so that they have a chance.
+    if (VariableFC) {
+      TableInfo.pushScope();
+      emitSingletonTableEntry(TableInfo, *SingletonEncodingID);
+      TableInfo.popScope();
+      VariableFC->emitTableEntries(TableInfo);
+    } else {
+      emitSingletonTableEntry(TableInfo, *SingletonEncodingID);
+    }
+    return;
+  }
+
+  // Fall back to the general case.
   assert(isUInt<8>(NumBits) && "NumBits overflowed uint8 table entry!");
   TableInfo.Table.push_back(MCD::OPC_ExtractField);
 
@@ -1426,17 +1437,6 @@ void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
   }
 }
 
-// Emits table entries to decode the singleton, and then to decode the rest.
-void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo) const {
-  // complex singletons need predicate checks from the first singleton
-  // to refer forward to the variable filterchooser that follows.
-  TableInfo.pushScope();
-  emitSingletonTableEntry(TableInfo, *SingletonEncodingID);
-  TableInfo.popScope();
-
-  VariableFC->emitTableEntries(TableInfo);
-}
-
 // reportRegion is a helper function for filterProcessor to mark a region as
 // eligible for use as a filter region.
 void FilterChooser::reportRegion(std::vector<std::unique_ptr<Filter>> &Filters,
@@ -1695,8 +1695,10 @@ void FilterChooser::doFilter() {
   assert(!EncodingIDs.empty() && "FilterChooser created with no instructions");
 
   // No filter needed.
-  if (EncodingIDs.size() < 2)
+  if (EncodingIDs.size() == 1) {
+    SingletonEncodingID = EncodingIDs.front();
     return;
+  }
 
   std::unique_ptr<Filter> BestFilter = findBestFilter();
   if (BestFilter) {
@@ -1727,24 +1729,6 @@ void FilterChooser::dump() const {
   }
 }
 
-// emitTableEntries - Emit state machine entries to decode our share of
-// instructions.
-void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
-  if (EncodingIDs.size() == 1) {
-    // There is only one instruction in the set, which is great!
-    // Call emitSingletonDecoder() to see whether there are any remaining
-    // encodings bits.
-    emitSingletonTableEntry(TableInfo, EncodingIDs[0]);
-    return;
-  }
-
-  // Use the best filter to do the decoding!
-  if (SingletonEncodingID)
-    emitSingletonTableEntry(TableInfo);
-  else
-    emitTableEntry(TableInfo);
-}
-
 static std::string findOperandDecoderMethod(const Record *Record) {
   std::string Decoder;
 



More information about the llvm-commits mailing list