Skip to content

Commit b1f0869

Browse files
jmorseyuxuanchen1997
authored andcommitted
[InstrRef][NFC] Avoid another DenseMap, use a sorted vector (#99051)
Summary: 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. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251012
1 parent f9608ed commit b1f0869

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ class TransferTracker {
316316
bool isBest() const { return getQuality() == LocationQuality::Best; }
317317
};
318318

319+
using ValueLocPair = std::pair<ValueIDNum, LocationAndQuality>;
320+
321+
static inline bool ValueToLocSort(const ValueLocPair &A,
322+
const ValueLocPair &B) {
323+
return A.first < B.first;
324+
};
325+
319326
// Returns the LocationQuality for the location L iff the quality of L is
320327
// is strictly greater than the provided minimum quality.
321328
std::optional<LocationQuality>
@@ -344,7 +351,7 @@ class TransferTracker {
344351
/// \p DbgOpStore is the map containing the DbgOpID->DbgOp mapping needed to
345352
/// determine the values used by Value.
346353
void loadVarInloc(MachineBasicBlock &MBB, DbgOpIDMap &DbgOpStore,
347-
const DenseMap<ValueIDNum, LocationAndQuality> &ValueToLoc,
354+
const SmallVectorImpl<ValueLocPair> &ValueToLoc,
348355
DebugVariable Var, DbgValue Value) {
349356
SmallVector<DbgOp> DbgOps;
350357
SmallVector<ResolvedDbgOp> ResolvedDbgOps;
@@ -373,9 +380,17 @@ class TransferTracker {
373380
continue;
374381
}
375382

376-
// If the value has no location, we can't make a variable location.
383+
// Search for the desired ValueIDNum, to examine the best location found
384+
// for it. Use an empty ValueLocPair to search for an entry in ValueToLoc.
377385
const ValueIDNum &Num = Op.ID;
378-
auto ValuesPreferredLoc = ValueToLoc.find(Num);
386+
ValueLocPair Probe(Num, LocationAndQuality());
387+
auto ValuesPreferredLoc = std::lower_bound(
388+
ValueToLoc.begin(), ValueToLoc.end(), Probe, ValueToLocSort);
389+
390+
// There must be a legitimate entry found for Num.
391+
assert(ValuesPreferredLoc != ValueToLoc.end() &&
392+
ValuesPreferredLoc->first == Num);
393+
379394
if (ValuesPreferredLoc->second.isIllegal()) {
380395
// If it's a def that occurs in this block, register it as a
381396
// use-before-def to be resolved as we step through the block.
@@ -439,17 +454,20 @@ class TransferTracker {
439454
UseBeforeDefs.clear();
440455
UseBeforeDefVariables.clear();
441456

442-
// Map of the preferred location for each value.
443-
DenseMap<ValueIDNum, LocationAndQuality> ValueToLoc;
457+
// Mapping of the preferred locations for each value. Collected into this
458+
// vector then sorted for easy searching.
459+
SmallVector<ValueLocPair, 16> ValueToLoc;
444460

445461
// Initialized the preferred-location map with illegal locations, to be
446462
// filled in later.
447463
for (const auto &VLoc : VLocs)
448464
if (VLoc.second.Kind == DbgValue::Def)
449465
for (DbgOpID OpID : VLoc.second.getDbgOpIDs())
450466
if (!OpID.ID.IsConst)
451-
ValueToLoc.insert({DbgOpStore.find(OpID).ID, LocationAndQuality()});
467+
ValueToLoc.push_back(
468+
{DbgOpStore.find(OpID).ID, LocationAndQuality()});
452469

470+
llvm::sort(ValueToLoc, ValueToLocSort);
453471
ActiveMLocs.reserve(VLocs.size());
454472
ActiveVLocs.reserve(VLocs.size());
455473

@@ -464,8 +482,10 @@ class TransferTracker {
464482
VarLocs.push_back(VNum);
465483

466484
// Is there a variable that wants a location for this value? If not, skip.
467-
auto VIt = ValueToLoc.find(VNum);
468-
if (VIt == ValueToLoc.end())
485+
ValueLocPair Probe(VNum, LocationAndQuality());
486+
auto VIt = std::lower_bound(ValueToLoc.begin(), ValueToLoc.end(), Probe,
487+
ValueToLocSort);
488+
if (VIt == ValueToLoc.end() || VIt->first != VNum)
469489
continue;
470490

471491
auto &Previous = VIt->second;

0 commit comments

Comments
 (0)