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

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 31 06:23:30 PDT 2025


Author: Rahul Joshi
Date: 2025-08-31T06:23:26-07:00
New Revision: 1f2d461e26c4c7fdad922b1b62f2aced58844ce1

URL: https://github.com/llvm/llvm-project/commit/1f2d461e26c4c7fdad922b1b62f2aced58844ce1
DIFF: https://github.com/llvm/llvm-project/commit/1f2d461e26c4c7fdad922b1b62f2aced58844ce1.diff

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

We can just use `max_element` on the array of filters.

Added: 
    

Modified: 
    llvm/utils/TableGen/DecoderEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index ea295b33ce8c9..e4992b9e9e725 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1473,26 +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.
-  unsigned BestIndex = 0;
-  bool AllUseless = true;
-  unsigned BestScore = 0;
-
-  for (const auto &[Idx, Filter] : enumerate(Filters)) {
-    unsigned Usefulness = Filter->usefulness();
-
-    if (Usefulness)
-      AllUseless = false;
-
-    if (Usefulness > BestScore) {
-      BestIndex = Idx;
-      BestScore = Usefulness;
-    }
-  }
-
-  if (AllUseless)
+  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(Filters[BestIndex]);
+  return std::move(*MaxIt);
 }
 
 std::unique_ptr<Filter> FilterChooser::findBestFilter() const {


        


More information about the llvm-commits mailing list