[TableGen] Slightly improve the efficiency of InfoByHwMode::get.
authorCraig Topper <craig.topper@sifive.com>
Sun, 9 Apr 2023 01:54:56 +0000 (18:54 -0700)
committerCraig Topper <craig.topper@sifive.com>
Sun, 9 Apr 2023 01:54:56 +0000 (18:54 -0700)
This avoids some double map lookups. Also use Map.begin() to find
the default mode instead of going through find.

llvm/utils/TableGen/InfoByHwMode.cpp
llvm/utils/TableGen/InfoByHwMode.h

index 5140c5a..4e9136e 100644 (file)
@@ -65,8 +65,8 @@ MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) {
     return F->second;
   // If Mode is not in the map, look up the default mode. If it exists,
   // make a copy of it for Mode and return it.
-  auto D = Map.find(DefaultMode);
-  if (D != Map.end())
+  auto D = Map.begin();
+  if (D != Map.end() && D->first == DefaultMode)
     return Map.insert(std::make_pair(Mode, D->second)).first->second;
   // If default mode is not present either, use provided Type.
   return Map.insert(std::make_pair(Mode, Type)).first->second;
index cb20795..dc76029 100644 (file)
@@ -110,20 +110,27 @@ struct InfoByHwMode {
   LLVM_ATTRIBUTE_ALWAYS_INLINE
   bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); }
   LLVM_ATTRIBUTE_ALWAYS_INLINE
-  bool hasDefault() const { return hasMode(DefaultMode); }
+  bool hasDefault() const {
+    return !Map.empty() && Map.begin()->first == DefaultMode;
+  }
 
   InfoT &get(unsigned Mode) {
-    if (!hasMode(Mode)) {
-      assert(hasMode(DefaultMode));
-      Map.insert({Mode, Map.at(DefaultMode)});
-    }
-    return Map.at(Mode);
+    auto F = Map.find(Mode);
+    if (F != Map.end())
+      return F->second;
+
+    // Copy and insert the default mode which should be first.
+    assert(hasDefault());
+    auto P = Map.insert({Mode, Map.begin()->second});
+    return P.first->second;
   }
   const InfoT &get(unsigned Mode) const {
     auto F = Map.find(Mode);
-    if (Mode != DefaultMode && F == Map.end())
-      F = Map.find(DefaultMode);
-    assert(F != Map.end());
+    if (F != Map.end())
+      return F->second;
+    // Get the default mode which should be first.
+    F = Map.begin();
+    assert(F != Map.end() && F->first == DefaultMode);
     return F->second;
   }