[llvm] [NFC][MC][DecoderEmitter] Simplify loop to find the best filter (PR #156237)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 31 05:07:14 PDT 2025


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/156237

>From 98eabb8b0b7cea41b44358c42adcad6316114843 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sat, 30 Aug 2025 16:33:48 -0700
Subject: [PATCH 1/2] [NFC][MC][DecoderEmitter] Simplify loop to find the best
 filter

---
 llvm/utils/TableGen/DecoderEmitter.cpp | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 98569abfbb1a4..871e06a07a7b3 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1473,26 +1473,21 @@ FilterChooser::findBestFilter(ArrayRef<bitAttr_t> BitAttrs, bool AllowMixed,
 
   // We have finished with the filter processings.  Now it's time to choose
   // the best performing filter.
-  unsigned BestIndex = 0;
-  bool AllUseless = true;
+  std::unique_ptr<Filter> *BestFilter = nullptr;
   unsigned BestScore = 0;
 
-  for (const auto &[Idx, Filter] : enumerate(Filters)) {
+  for (std::unique_ptr<Filter> &Filter : Filters) {
     unsigned Usefulness = Filter->usefulness();
-
-    if (Usefulness)
-      AllUseless = false;
-
     if (Usefulness > BestScore) {
-      BestIndex = Idx;
+      BestFilter = &Filter;
       BestScore = Usefulness;
     }
   }
 
-  if (AllUseless)
+  if (BestFilter == nullptr)
     return nullptr;
 
-  return std::move(Filters[BestIndex]);
+  return std::move(*BestFilter);
 }
 
 std::unique_ptr<Filter> FilterChooser::findBestFilter() const {

>From 9d6e3d02f8f6e936cceae7eace8639d570cdcfc3 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sun, 31 Aug 2025 05:04:50 -0700
Subject: [PATCH 2/2] use max_element

---
 llvm/utils/TableGen/DecoderEmitter.cpp | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 871e06a07a7b3..671cef6fae390 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1473,21 +1473,13 @@ FilterChooser::findBestFilter(ArrayRef<bitAttr_t> BitAttrs, bool AllowMixed,
 
   // We have finished with the filter processings.  Now it's time to choose
   // the best performing filter.
-  std::unique_ptr<Filter> *BestFilter = nullptr;
-  unsigned BestScore = 0;
-
-  for (std::unique_ptr<Filter> &Filter : Filters) {
-    unsigned Usefulness = Filter->usefulness();
-    if (Usefulness > BestScore) {
-      BestFilter = &Filter;
-      BestScore = Usefulness;
-    }
-  }
-
-  if (BestFilter == nullptr)
+  auto MaxIt = llvm::max_element(Filters, [](const std::unique_ptr<Filter> &A,
+                                             const std::unique_ptr<Filter> &B) {
+    return A->usefulness() < B->usefulness();
+  });
+  if (MaxIt == Filters.end() || (*MaxIt)->usefulness() == 0)
     return nullptr;
-
-  return std::move(*BestFilter);
+  return std::move(*MaxIt);
 }
 
 std::unique_ptr<Filter> FilterChooser::findBestFilter() const {



More information about the llvm-commits mailing list