[llvm] 72e1441 - [TableGen] Slightly improve the efficiency of InfoByHwMode::get.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 8 19:17:35 PDT 2023
Author: Craig Topper
Date: 2023-04-08T18:54:56-07:00
New Revision: 72e1441c7a3067a303bd0909f9928af8d5bf1769
URL: https://github.com/llvm/llvm-project/commit/72e1441c7a3067a303bd0909f9928af8d5bf1769
DIFF: https://github.com/llvm/llvm-project/commit/72e1441c7a3067a303bd0909f9928af8d5bf1769.diff
LOG: [TableGen] Slightly improve the efficiency of InfoByHwMode::get.
This avoids some double map lookups. Also use Map.begin() to find
the default mode instead of going through find.
Added:
Modified:
llvm/utils/TableGen/InfoByHwMode.cpp
llvm/utils/TableGen/InfoByHwMode.h
Removed:
################################################################################
diff --git a/llvm/utils/TableGen/InfoByHwMode.cpp b/llvm/utils/TableGen/InfoByHwMode.cpp
index 5140c5a0d20f..4e9136e936af 100644
--- a/llvm/utils/TableGen/InfoByHwMode.cpp
+++ b/llvm/utils/TableGen/InfoByHwMode.cpp
@@ -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;
diff --git a/llvm/utils/TableGen/InfoByHwMode.h b/llvm/utils/TableGen/InfoByHwMode.h
index cb20795c03a6..dc760292578f 100644
--- a/llvm/utils/TableGen/InfoByHwMode.h
+++ b/llvm/utils/TableGen/InfoByHwMode.h
@@ -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;
}
More information about the llvm-commits
mailing list