Skip to content

Commit

Permalink
[InstrRef][NFC] Avoid another DenseMap, use a sorted vector (llvm#99051)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmorse authored Jul 17, 2024
1 parent b05ccaf commit e093109
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ class TransferTracker {
bool isBest() const { return getQuality() == LocationQuality::Best; }
};

using ValueLocPair = std::pair<ValueIDNum, LocationAndQuality>;

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<LocationQuality>
Expand Down Expand Up @@ -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<ValueIDNum, LocationAndQuality> &ValueToLoc,
const SmallVectorImpl<ValueLocPair> &ValueToLoc,
DebugVariable Var, DbgValue Value) {
SmallVector<DbgOp> DbgOps;
SmallVector<ResolvedDbgOp> ResolvedDbgOps;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -439,17 +454,20 @@ class TransferTracker {
UseBeforeDefs.clear();
UseBeforeDefVariables.clear();

// Map of the preferred location for each value.
DenseMap<ValueIDNum, LocationAndQuality> ValueToLoc;
// Mapping of the preferred locations for each value. Collected into this
// vector then sorted for easy searching.
SmallVector<ValueLocPair, 16> ValueToLoc;

// Initialized the preferred-location map with illegal locations, to be
// filled in later.
for (const auto &VLoc : VLocs)
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());

Expand All @@ -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;
Expand Down

0 comments on commit e093109

Please sign in to comment.