From e093109e4a1551de13a1219275d62e9c7ee3146f Mon Sep 17 00:00:00 2001 From: Jeremy Morse Date: Wed, 17 Jul 2024 13:56:44 +0100 Subject: [PATCH] [InstrRef][NFC] Avoid another DenseMap, use a sorted vector (#99051) When resolving value-numbers to specific machine locations in the final stages of LiveDebugValues, we've been producing a DenseMap containing all the value-numbers we're interested in. However we never modify the map keys as they're all pre-known. Thus, this is a suitable collection to switch to a sorted vector that gets searched, rather than a DenseMap that gets probed. The overall operation of LiveDebugValues isn't affected at all. --- .../LiveDebugValues/InstrRefBasedImpl.cpp | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp index bde8cc4a897158..247258a1ff5533 100644 --- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp +++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp @@ -316,6 +316,13 @@ class TransferTracker { bool isBest() const { return getQuality() == LocationQuality::Best; } }; + using ValueLocPair = std::pair; + + static inline bool ValueToLocSort(const ValueLocPair &A, + const ValueLocPair &B) { + return A.first < B.first; + }; + // Returns the LocationQuality for the location L iff the quality of L is // is strictly greater than the provided minimum quality. std::optional @@ -344,7 +351,7 @@ class TransferTracker { /// \p DbgOpStore is the map containing the DbgOpID->DbgOp mapping needed to /// determine the values used by Value. void loadVarInloc(MachineBasicBlock &MBB, DbgOpIDMap &DbgOpStore, - const DenseMap &ValueToLoc, + const SmallVectorImpl &ValueToLoc, DebugVariable Var, DbgValue Value) { SmallVector DbgOps; SmallVector ResolvedDbgOps; @@ -373,9 +380,17 @@ class TransferTracker { continue; } - // If the value has no location, we can't make a variable location. + // Search for the desired ValueIDNum, to examine the best location found + // for it. Use an empty ValueLocPair to search for an entry in ValueToLoc. const ValueIDNum &Num = Op.ID; - auto ValuesPreferredLoc = ValueToLoc.find(Num); + ValueLocPair Probe(Num, LocationAndQuality()); + auto ValuesPreferredLoc = std::lower_bound( + ValueToLoc.begin(), ValueToLoc.end(), Probe, ValueToLocSort); + + // There must be a legitimate entry found for Num. + assert(ValuesPreferredLoc != ValueToLoc.end() && + ValuesPreferredLoc->first == Num); + if (ValuesPreferredLoc->second.isIllegal()) { // If it's a def that occurs in this block, register it as a // use-before-def to be resolved as we step through the block. @@ -439,8 +454,9 @@ class TransferTracker { UseBeforeDefs.clear(); UseBeforeDefVariables.clear(); - // Map of the preferred location for each value. - DenseMap ValueToLoc; + // Mapping of the preferred locations for each value. Collected into this + // vector then sorted for easy searching. + SmallVector ValueToLoc; // Initialized the preferred-location map with illegal locations, to be // filled in later. @@ -448,8 +464,10 @@ class TransferTracker { if (VLoc.second.Kind == DbgValue::Def) for (DbgOpID OpID : VLoc.second.getDbgOpIDs()) if (!OpID.ID.IsConst) - ValueToLoc.insert({DbgOpStore.find(OpID).ID, LocationAndQuality()}); + ValueToLoc.push_back( + {DbgOpStore.find(OpID).ID, LocationAndQuality()}); + llvm::sort(ValueToLoc, ValueToLocSort); ActiveMLocs.reserve(VLocs.size()); ActiveVLocs.reserve(VLocs.size()); @@ -464,8 +482,10 @@ class TransferTracker { VarLocs.push_back(VNum); // Is there a variable that wants a location for this value? If not, skip. - auto VIt = ValueToLoc.find(VNum); - if (VIt == ValueToLoc.end()) + ValueLocPair Probe(VNum, LocationAndQuality()); + auto VIt = std::lower_bound(ValueToLoc.begin(), ValueToLoc.end(), Probe, + ValueToLocSort); + if (VIt == ValueToLoc.end() || VIt->first != VNum) continue; auto &Previous = VIt->second;