[llvm] [NFC][TableGen] Minor code cleanup in SearchableTableEmitter (PR #147856)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 9 22:43:41 PDT 2025


================
@@ -232,67 +235,74 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
 /// key of \p Index.
 bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
                                        const SearchIndex &Index) {
-  for (const auto &Field : Index.Fields) {
-    const Init *LHSI = LHS->getValueInit(Field.Name);
-    const Init *RHSI = RHS->getValueInit(Field.Name);
+  // Compare two values and return:
+  // true if LHS < RHS
+  // false if  LHS > RHS
+  // std::nullopt if LHS == RHS
+  auto CmpLTValue = [](const auto &LHS,
+                       const auto &RHS) -> std::optional<bool> {
+    if (LHS < RHS)
+      return true;
+    if (LHS > RHS)
+      return false;
+    return std::nullopt;
+  };
 
+  // Compare two fields and returns:
+  // true if LHS < RHS
+  // false if  LHS > RHS
+  // std::nullopt if LHS == RHS
+  auto CmpLTField = [this, &Index, CmpLTValue](
+                        const Init *LHSI, const Init *RHSI,
+                        const GenericField &Field) -> std::optional<bool> {
     if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
       int64_t LHSi = getAsInt(LHSI);
       int64_t RHSi = getAsInt(RHSI);
-      if (LHSi < RHSi)
-        return true;
-      if (LHSi > RHSi)
-        return false;
-    } else if (Field.IsIntrinsic) {
+      return CmpLTValue(LHSi, RHSi);
+    }
+
+    if (Field.IsIntrinsic) {
       const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
       const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
-      if (std::tie(LHSi.TargetPrefix, LHSi.Name) <
-          std::tie(RHSi.TargetPrefix, RHSi.Name))
-        return true;
-      if (std::tie(LHSi.TargetPrefix, LHSi.Name) >
-          std::tie(RHSi.TargetPrefix, RHSi.Name))
-        return false;
-    } else if (Field.IsInstruction) {
+      return CmpLTValue(std::tie(LHSi.TargetPrefix, LHSi.Name),
+                        std::tie(RHSi.TargetPrefix, RHSi.Name));
+    }
+
+    if (Field.IsInstruction) {
       // This does not correctly compare the predefined instructions!
       const Record *LHSr = cast<DefInit>(LHSI)->getDef();
       const Record *RHSr = cast<DefInit>(RHSI)->getDef();
 
-      bool LHSpseudo = LHSr->getValueAsBit("isPseudo");
-      bool RHSpseudo = RHSr->getValueAsBit("isPseudo");
-      if (LHSpseudo && !RHSpseudo)
-        return true;
-      if (!LHSpseudo && RHSpseudo)
-        return false;
+      // Order pseudo instructions before non-pseudo ones.
+      bool LHSNotPseudo = !LHSr->getValueAsBit("isPseudo");
+      bool RHSNotPseudo = !RHSr->getValueAsBit("isPseudo");
+      return CmpLTValue(std::tuple(LHSNotPseudo, LHSr->getName()),
+                        std::tuple(RHSNotPseudo, RHSr->getName()));
+    }
 
-      int comp = LHSr->getName().compare(RHSr->getName());
-      if (comp < 0)
-        return true;
-      if (comp > 0)
-        return false;
-    } else if (Field.Enum) {
-      auto LHSr = cast<DefInit>(LHSI)->getDef();
-      auto RHSr = cast<DefInit>(RHSI)->getDef();
+    if (Field.Enum) {
+      const Record *LHSr = cast<DefInit>(LHSI)->getDef();
+      const Record *RHSr = cast<DefInit>(RHSI)->getDef();
       int64_t LHSv = Field.Enum->EntryMap[LHSr]->second;
       int64_t RHSv = Field.Enum->EntryMap[RHSr]->second;
-      if (LHSv < RHSv)
-        return true;
-      if (LHSv > RHSv)
-        return false;
-    } else {
-      std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI);
-      std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI);
+      return CmpLTValue(LHSv, RHSv);
+    }
 
-      if (isa<StringRecTy>(Field.RecType)) {
-        LHSs = StringRef(LHSs).upper();
-        RHSs = StringRef(RHSs).upper();
-      }
+    std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI);
+    std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI);
 
-      int comp = LHSs.compare(RHSs);
-      if (comp < 0)
-        return true;
-      if (comp > 0)
-        return false;
+    if (isa<StringRecTy>(Field.RecType)) {
+      LHSs = StringRef(LHSs).upper();
+      RHSs = StringRef(RHSs).upper();
     }
+    return CmpLTValue(LHSs, RHSs);
+  };
+
+  for (const GenericField &Field : Index.Fields) {
+    const Init *LHSI = LHS->getValueInit(Field.Name);
+    const Init *RHSI = RHS->getValueInit(Field.Name);
+    if (std::optional<bool> Cmp = CmpLTField(LHSI, RHSI, Field))
----------------
topperc wrote:

Why not use -1, 0, 1 so we can use `compare` directly for the StringRefs?

https://github.com/llvm/llvm-project/pull/147856


More information about the llvm-commits mailing list