[llvm] [TableGen][NFC] Factor early-out range check. (PR #123645)

Jason Eckhardt via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 20 09:00:31 PST 2025


https://github.com/nvjle created https://github.com/llvm/llvm-project/pull/123645

Combine the EarlyOut and IsContiguous range check.
Also avoid "comparison is always false" warnings in emitted code when the lower-bound check is against 0.

>From d919d4d28a2d2343abf68d14ea908d514986ebbf Mon Sep 17 00:00:00 2001
From: Jason Eckhardt <jeckhardt at nvidia.com>
Date: Mon, 20 Jan 2025 10:35:06 -0600
Subject: [PATCH] [TableGen][NFC] Factor early-out range check.

Combine the EarlyOut and IsContiguous range check.
Also avoid "comparison is always false" warnings in emitted code when
the lower-bound check is against 0.
---
 .../utils/TableGen/SearchableTableEmitter.cpp | 41 ++++++++-----------
 1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index 38b6f2b3951375..5788e6eb56a683 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -392,37 +392,32 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
     }
   }
 
-  if (IsContiguous) {
+  if (Index.EarlyOut || IsContiguous) {
     const GenericField &Field = Index.Fields[0];
     std::string FirstRepr = primaryRepresentation(
         Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
     std::string LastRepr = primaryRepresentation(
         Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
-    OS << "  if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
-    OS << "      (" << Field.Name << " > " << LastRepr << "))\n";
-    OS << "    return nullptr;\n";
-    OS << "  auto Table = ArrayRef(" << IndexName << ");\n";
-    OS << "  size_t Idx = " << Index.Fields[0].Name << " - " << FirstRepr
-       << ";\n";
-    OS << "  return ";
-    if (IsPrimary)
-      OS << "&Table[Idx]";
+    if (getNumericKey(Index, IndexRows[0]) == 0)
+      OS << "  if (";
     else
-      OS << "&" << Table.Name << "[Table[Idx]._index]";
-    OS << ";\n";
-    OS << "}\n";
-    return;
-  }
-
-  if (Index.EarlyOut) {
-    const GenericField &Field = Index.Fields[0];
-    std::string FirstRepr = primaryRepresentation(
-        Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
-    std::string LastRepr = primaryRepresentation(
-        Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
-    OS << "  if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
+      OS << "  if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
     OS << "      (" << Field.Name << " > " << LastRepr << "))\n";
     OS << "    return nullptr;\n\n";
+
+    if (IsContiguous) {
+      OS << "  auto Table = ArrayRef(" << IndexName << ");\n";
+      OS << "  size_t Idx = " << Index.Fields[0].Name << " - " << FirstRepr
+         << ";\n";
+      OS << "  return ";
+      if (IsPrimary)
+        OS << "&Table[Idx]";
+      else
+        OS << "&" << Table.Name << "[Table[Idx]._index]";
+      OS << ";\n";
+      OS << "}\n";
+      return;
+    }
   }
 
   OS << "  struct KeyType {\n";



More information about the llvm-commits mailing list