[llvm] 9314186 - [TableGen] Reimplement union_modes for InfoByHwMode to be avoid a secondary set.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 8 12:57:09 PDT 2023


Author: Craig Topper
Date: 2023-04-08T12:46:20-07:00
New Revision: 931418667063743330f2c4195fd43ea64b0f8a12

URL: https://github.com/llvm/llvm-project/commit/931418667063743330f2c4195fd43ea64b0f8a12
DIFF: https://github.com/llvm/llvm-project/commit/931418667063743330f2c4195fd43ea64b0f8a12.diff

LOG: [TableGen] Reimplement union_modes for InfoByHwMode to be avoid a secondary set.

Previously we collected both modes in a set and iterated over that set.

Instead, iterate over the two maps in parallel and detect the differences
as we go.

Added: 
    

Modified: 
    llvm/utils/TableGen/InfoByHwMode.h

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/InfoByHwMode.h b/llvm/utils/TableGen/InfoByHwMode.h
index 6cfd6e8bb493..cb20795c03a6 100644
--- a/llvm/utils/TableGen/InfoByHwMode.h
+++ b/llvm/utils/TableGen/InfoByHwMode.h
@@ -44,18 +44,44 @@ template <typename InfoT>
 void union_modes(const InfoByHwMode<InfoT> &A,
                  const InfoByHwMode<InfoT> &B,
                  SmallVectorImpl<unsigned> &Modes) {
-  SmallSet<unsigned, 4> U;
-  for (const auto &P : A)
-    U.insert(P.first);
-  for (const auto &P : B)
-    U.insert(P.first);
-  // Make sure that the default mode is last on the list.
+  auto AI = A.begin();
+  auto BI = B.begin();
+
+  // Skip default mode, but remember if we had one.
   bool HasDefault = false;
-  for (unsigned M : U)
-    if (M != DefaultMode)
-      Modes.push_back(M);
-    else
-      HasDefault = true;
+  if (AI != A.end() && AI->first == DefaultMode) {
+    HasDefault = true;
+    ++AI;
+  }
+  if (BI != A.end() && BI->first == DefaultMode) {
+    HasDefault = true;
+    ++BI;
+  }
+
+  while (AI != A.end()) {
+    // If we're done with B, finish A.
+    if (BI == B.end()) {
+      for (; AI != A.end(); ++AI)
+        Modes.push_back(AI->first);
+      break;
+    }
+
+    if (BI->first < AI->first) {
+      Modes.push_back(BI->first);
+      ++BI;
+    } else {
+      Modes.push_back(AI->first);
+      if (AI->first == BI->first)
+        ++BI;
+      ++AI;
+    }
+  }
+
+  // Finish B.
+  for (; BI != B.end(); ++BI)
+    Modes.push_back(BI->first);
+
+  // Make sure that the default mode is last on the list.
   if (HasDefault)
     Modes.push_back(DefaultMode);
 }


        


More information about the llvm-commits mailing list