[X86][mem-fold][NFC] Refine code
authorShengchen Kan <shengchen.kan@intel.com>
Wed, 5 Apr 2023 09:42:12 +0000 (17:42 +0800)
committerShengchen Kan <shengchen.kan@intel.com>
Wed, 5 Apr 2023 09:54:58 +0000 (17:54 +0800)
1. Use `unsigned` for `KeyOp` and `DstOp` b/c `Opcode` is of type `unsigned`.
2. Align the comparator used in X86FoldTablesEmitter.cpp with the one in
   CodeGenTarget::ComputeInstrsByEnum.

llvm/lib/Target/X86/X86InstrFoldTables.h
llvm/utils/TableGen/X86FoldTablesEmitter.cpp

index 384369b..e1458e3 100644 (file)
@@ -21,8 +21,8 @@ namespace llvm {
 // This struct is used for both the folding and unfold tables. They KeyOp
 // is used to determine the sorting order.
 struct X86MemoryFoldTableEntry {
-  uint16_t KeyOp;
-  uint16_t DstOp;
+  unsigned KeyOp;
+  unsigned DstOp;
   uint16_t Flags;
 
   bool operator<(const X86MemoryFoldTableEntry &RHS) const {
index ea9e06b..b576e36 100644 (file)
@@ -109,22 +109,23 @@ class X86FoldTablesEmitter {
 
   };
 
-  struct CodeGenInstructionComparator {
-    // Comparator function
+  // NOTE: We check the fold tables are sorted in X86InstrFoldTables.cpp by the enum of the
+  //       instruction, which is computed in CodeGenTarget::ComputeInstrsByEnum. So we should
+  //       use the same comparator here.
+  // FIXME: Could we share the code with CodeGenTarget::ComputeInstrsByEnum?
+  struct CompareInstrsByEnum {
     bool operator()(const CodeGenInstruction *LHS,
                     const CodeGenInstruction *RHS) const {
       assert(LHS && RHS && "LHS and RHS shouldn't be nullptr");
-      bool LHSpseudo = LHS->TheDef->getValueAsBit("isPseudo");
-      bool RHSpseudo = RHS->TheDef->getValueAsBit("isPseudo");
-      if (LHSpseudo != RHSpseudo)
-        return LHSpseudo;
-
-      return LHS->TheDef->getName() < RHS->TheDef->getName();
+      const auto &D1 = *LHS->TheDef;
+      const auto &D2 = *RHS->TheDef;
+      return std::make_tuple(!D1.getValueAsBit("isPseudo"), D1.getName()) <
+             std::make_tuple(!D2.getValueAsBit("isPseudo"), D2.getName());
     }
   };
 
   typedef std::map<const CodeGenInstruction *, X86FoldTableEntry,
-                   CodeGenInstructionComparator>
+                   CompareInstrsByEnum>
       FoldTable;
   // std::vector for each folding table.
   // Table2Addr - Holds instructions which their memory form performs load+store