aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/LiveDebugValues
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/CodeGen/LiveDebugValues')
-rw-r--r--llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp1274
-rw-r--r--llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h492
-rw-r--r--llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp14
-rw-r--r--llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp207
4 files changed, 1412 insertions, 575 deletions
diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
index 191596dbf53e..ba417322d4f6 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
@@ -127,6 +127,7 @@
#include "InstrRefBasedImpl.h"
#include "LiveDebugValues.h"
+#include <optional>
using namespace llvm;
using namespace LiveDebugValues;
@@ -155,6 +156,8 @@ static cl::opt<unsigned>
cl::desc("livedebugvalues-stack-ws-limit"),
cl::init(250));
+DbgOpID DbgOpID::UndefID = DbgOpID(0xffffffff);
+
/// Tracker for converting machine value locations and variable values into
/// variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs
/// specifying block live-in locations and transfers within blocks.
@@ -191,9 +194,25 @@ public:
SmallVector<MachineInstr *, 4> Insts; /// Vector of DBG_VALUEs to insert.
};
- struct LocAndProperties {
- LocIdx Loc;
+ /// Stores the resolved operands (machine locations and constants) and
+ /// qualifying meta-information needed to construct a concrete DBG_VALUE-like
+ /// instruction.
+ struct ResolvedDbgValue {
+ SmallVector<ResolvedDbgOp> Ops;
DbgValueProperties Properties;
+
+ ResolvedDbgValue(SmallVectorImpl<ResolvedDbgOp> &Ops,
+ DbgValueProperties Properties)
+ : Ops(Ops.begin(), Ops.end()), Properties(Properties) {}
+
+ /// Returns all the LocIdx values used in this struct, in the order in which
+ /// they appear as operands in the debug value; may contain duplicates.
+ auto loc_indices() const {
+ return map_range(
+ make_filter_range(
+ Ops, [](const ResolvedDbgOp &Op) { return !Op.IsConst; }),
+ [](const ResolvedDbgOp &Op) { return Op.Loc; });
+ }
};
/// Collection of transfers (DBG_VALUEs) to be inserted.
@@ -213,7 +232,7 @@ public:
/// Map from DebugVariable to it's current location and qualifying meta
/// information. To be used in conjunction with ActiveMLocs to construct
/// enough information for the DBG_VALUEs for a particular LocIdx.
- DenseMap<DebugVariable, LocAndProperties> ActiveVLocs;
+ DenseMap<DebugVariable, ResolvedDbgValue> ActiveVLocs;
/// Temporary cache of DBG_VALUEs to be entered into the Transfers collection.
SmallVector<MachineInstr *, 4> PendingDbgValues;
@@ -223,11 +242,15 @@ public:
/// defined in this block.
struct UseBeforeDef {
/// Value of this variable, def'd in block.
- ValueIDNum ID;
+ SmallVector<DbgOp> Values;
/// Identity of this variable.
DebugVariable Var;
/// Additional variable properties.
DbgValueProperties Properties;
+ UseBeforeDef(ArrayRef<DbgOp> Values, const DebugVariable &Var,
+ const DbgValueProperties &Properties)
+ : Values(Values.begin(), Values.end()), Var(Var),
+ Properties(Properties) {}
};
/// Map from instruction index (within the block) to the set of UseBeforeDefs
@@ -252,6 +275,153 @@ public:
ShouldEmitDebugEntryValues = TM.Options.ShouldEmitDebugEntryValues();
}
+ bool isCalleeSaved(LocIdx L) const {
+ unsigned Reg = MTracker->LocIdxToLocID[L];
+ if (Reg >= MTracker->NumRegs)
+ return false;
+ for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI)
+ if (CalleeSavedRegs.test(*RAI))
+ return true;
+ return false;
+ };
+
+ // An estimate of the expected lifespan of values at a machine location, with
+ // a greater value corresponding to a longer expected lifespan, i.e. spill
+ // slots generally live longer than callee-saved registers which generally
+ // live longer than non-callee-saved registers. The minimum value of 0
+ // corresponds to an illegal location that cannot have a "lifespan" at all.
+ enum class LocationQuality : unsigned char {
+ Illegal = 0,
+ Register,
+ CalleeSavedRegister,
+ SpillSlot,
+ Best = SpillSlot
+ };
+
+ class LocationAndQuality {
+ unsigned Location : 24;
+ unsigned Quality : 8;
+
+ public:
+ LocationAndQuality() : Location(0), Quality(0) {}
+ LocationAndQuality(LocIdx L, LocationQuality Q)
+ : Location(L.asU64()), Quality(static_cast<unsigned>(Q)) {}
+ LocIdx getLoc() const {
+ if (!Quality)
+ return LocIdx::MakeIllegalLoc();
+ return LocIdx(Location);
+ }
+ LocationQuality getQuality() const { return LocationQuality(Quality); }
+ bool isIllegal() const { return !Quality; }
+ bool isBest() const { return getQuality() == LocationQuality::Best; }
+ };
+
+ // Returns the LocationQuality for the location L iff the quality of L is
+ // is strictly greater than the provided minimum quality.
+ std::optional<LocationQuality>
+ getLocQualityIfBetter(LocIdx L, LocationQuality Min) const {
+ if (L.isIllegal())
+ return std::nullopt;
+ if (Min >= LocationQuality::SpillSlot)
+ return std::nullopt;
+ if (MTracker->isSpill(L))
+ return LocationQuality::SpillSlot;
+ if (Min >= LocationQuality::CalleeSavedRegister)
+ return std::nullopt;
+ if (isCalleeSaved(L))
+ return LocationQuality::CalleeSavedRegister;
+ if (Min >= LocationQuality::Register)
+ return std::nullopt;
+ return LocationQuality::Register;
+ }
+
+ /// For a variable \p Var with the live-in value \p Value, attempts to resolve
+ /// the DbgValue to a concrete DBG_VALUE, emitting that value and loading the
+ /// tracking information to track Var throughout the block.
+ /// \p ValueToLoc is a map containing the best known location for every
+ /// ValueIDNum that Value may use.
+ /// \p MBB is the basic block that we are loading the live-in value for.
+ /// \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,
+ DebugVariable Var, DbgValue Value) {
+ SmallVector<DbgOp> DbgOps;
+ SmallVector<ResolvedDbgOp> ResolvedDbgOps;
+ bool IsValueValid = true;
+ unsigned LastUseBeforeDef = 0;
+
+ // If every value used by the incoming DbgValue is available at block
+ // entry, ResolvedDbgOps will contain the machine locations/constants for
+ // those values and will be used to emit a debug location.
+ // If one or more values are not yet available, but will all be defined in
+ // this block, then LastUseBeforeDef will track the instruction index in
+ // this BB at which the last of those values is defined, DbgOps will
+ // contain the values that we will emit when we reach that instruction.
+ // If one or more values are undef or not available throughout this block,
+ // and we can't recover as an entry value, we set IsValueValid=false and
+ // skip this variable.
+ for (DbgOpID ID : Value.getDbgOpIDs()) {
+ DbgOp Op = DbgOpStore.find(ID);
+ DbgOps.push_back(Op);
+ if (ID.isUndef()) {
+ IsValueValid = false;
+ break;
+ }
+ if (ID.isConst()) {
+ ResolvedDbgOps.push_back(Op.MO);
+ continue;
+ }
+
+ // If the value has no location, we can't make a variable location.
+ const ValueIDNum &Num = Op.ID;
+ auto ValuesPreferredLoc = ValueToLoc.find(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.
+ // Continue processing values so that we add any other UseBeforeDef
+ // entries needed for later.
+ if (Num.getBlock() == (unsigned)MBB.getNumber() && !Num.isPHI()) {
+ LastUseBeforeDef = std::max(LastUseBeforeDef,
+ static_cast<unsigned>(Num.getInst()));
+ continue;
+ }
+ recoverAsEntryValue(Var, Value.Properties, Num);
+ IsValueValid = false;
+ break;
+ }
+
+ // Defer modifying ActiveVLocs until after we've confirmed we have a
+ // live range.
+ LocIdx M = ValuesPreferredLoc->second.getLoc();
+ ResolvedDbgOps.push_back(M);
+ }
+
+ // If we cannot produce a valid value for the LiveIn value within this
+ // block, skip this variable.
+ if (!IsValueValid)
+ return;
+
+ // Add UseBeforeDef entry for the last value to be defined in this block.
+ if (LastUseBeforeDef) {
+ addUseBeforeDef(Var, Value.Properties, DbgOps,
+ LastUseBeforeDef);
+ return;
+ }
+
+ // The LiveIn value is available at block entry, begin tracking and record
+ // the transfer.
+ for (const ResolvedDbgOp &Op : ResolvedDbgOps)
+ if (!Op.IsConst)
+ ActiveMLocs[Op.Loc].insert(Var);
+ auto NewValue = ResolvedDbgValue{ResolvedDbgOps, Value.Properties};
+ auto Result = ActiveVLocs.insert(std::make_pair(Var, NewValue));
+ if (!Result.second)
+ Result.first->second = NewValue;
+ PendingDbgValues.push_back(
+ MTracker->emitLoc(ResolvedDbgOps, Var, Value.Properties));
+ }
+
/// Load object with live-in variable values. \p mlocs contains the live-in
/// values in each machine location, while \p vlocs the live-in variable
/// values. This method picks variable locations for the live-in variables,
@@ -259,7 +429,7 @@ public:
/// object fields to track variable locations as we step through the block.
/// FIXME: could just examine mloctracker instead of passing in \p mlocs?
void
- loadInlocs(MachineBasicBlock &MBB, ValueTable &MLocs,
+ loadInlocs(MachineBasicBlock &MBB, ValueTable &MLocs, DbgOpIDMap &DbgOpStore,
const SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs,
unsigned NumLocs) {
ActiveMLocs.clear();
@@ -269,24 +439,16 @@ public:
UseBeforeDefs.clear();
UseBeforeDefVariables.clear();
- auto isCalleeSaved = [&](LocIdx L) {
- unsigned Reg = MTracker->LocIdxToLocID[L];
- if (Reg >= MTracker->NumRegs)
- return false;
- for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI)
- if (CalleeSavedRegs.test(*RAI))
- return true;
- return false;
- };
-
// Map of the preferred location for each value.
- DenseMap<ValueIDNum, LocIdx> ValueToLoc;
+ DenseMap<ValueIDNum, LocationAndQuality> 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)
- ValueToLoc.insert({VLoc.second.ID, LocIdx::MakeIllegalLoc()});
+ for (DbgOpID OpID : VLoc.second.getDbgOpIDs())
+ if (!OpID.ID.IsConst)
+ ValueToLoc.insert({DbgOpStore.find(OpID).ID, LocationAndQuality()});
ActiveMLocs.reserve(VLocs.size());
ActiveVLocs.reserve(VLocs.size());
@@ -297,6 +459,8 @@ public:
for (auto Location : MTracker->locations()) {
LocIdx Idx = Location.Idx;
ValueIDNum &VNum = MLocs[Idx.asU64()];
+ if (VNum == ValueIDNum::EmptyValue)
+ continue;
VarLocs.push_back(VNum);
// Is there a variable that wants a location for this value? If not, skip.
@@ -304,47 +468,18 @@ public:
if (VIt == ValueToLoc.end())
continue;
- LocIdx CurLoc = VIt->second;
- // In order of preference, pick:
- // * Callee saved registers,
- // * Other registers,
- // * Spill slots.
- if (CurLoc.isIllegal() || MTracker->isSpill(CurLoc) ||
- (!isCalleeSaved(CurLoc) && isCalleeSaved(Idx.asU64()))) {
- // Insert, or overwrite if insertion failed.
- VIt->second = Idx;
- }
+ auto &Previous = VIt->second;
+ // If this is the first location with that value, pick it. Otherwise,
+ // consider whether it's a "longer term" location.
+ std::optional<LocationQuality> ReplacementQuality =
+ getLocQualityIfBetter(Idx, Previous.getQuality());
+ if (ReplacementQuality)
+ Previous = LocationAndQuality(Idx, *ReplacementQuality);
}
// Now map variables to their picked LocIdxes.
for (const auto &Var : VLocs) {
- if (Var.second.Kind == DbgValue::Const) {
- PendingDbgValues.push_back(
- emitMOLoc(*Var.second.MO, Var.first, Var.second.Properties));
- continue;
- }
-
- // If the value has no location, we can't make a variable location.
- const ValueIDNum &Num = Var.second.ID;
- auto ValuesPreferredLoc = ValueToLoc.find(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.
- if (Num.getBlock() == (unsigned)MBB.getNumber() && !Num.isPHI())
- addUseBeforeDef(Var.first, Var.second.Properties, Num);
- else
- recoverAsEntryValue(Var.first, Var.second.Properties, Num);
- continue;
- }
-
- LocIdx M = ValuesPreferredLoc->second;
- auto NewValue = LocAndProperties{M, Var.second.Properties};
- auto Result = ActiveVLocs.insert(std::make_pair(Var.first, NewValue));
- if (!Result.second)
- Result.first->second = NewValue;
- ActiveMLocs[M].insert(Var.first);
- PendingDbgValues.push_back(
- MTracker->emitLoc(M, Var.first, Var.second.Properties));
+ loadVarInloc(MBB, DbgOpStore, ValueToLoc, Var.first, Var.second);
}
flushDbgValues(MBB.begin(), &MBB);
}
@@ -352,9 +487,9 @@ public:
/// Record that \p Var has value \p ID, a value that becomes available
/// later in the function.
void addUseBeforeDef(const DebugVariable &Var,
- const DbgValueProperties &Properties, ValueIDNum ID) {
- UseBeforeDef UBD = {ID, Var, Properties};
- UseBeforeDefs[ID.getInst()].push_back(UBD);
+ const DbgValueProperties &Properties,
+ const SmallVectorImpl<DbgOp> &DbgOps, unsigned Inst) {
+ UseBeforeDefs[Inst].emplace_back(DbgOps, Var, Properties);
UseBeforeDefVariables.insert(Var);
}
@@ -367,22 +502,77 @@ public:
if (MIt == UseBeforeDefs.end())
return;
+ // Map of values to the locations that store them for every value used by
+ // the variables that may have become available.
+ SmallDenseMap<ValueIDNum, LocationAndQuality> ValueToLoc;
+
+ // Populate ValueToLoc with illegal default mappings for every value used by
+ // any UseBeforeDef variables for this instruction.
for (auto &Use : MIt->second) {
- LocIdx L = Use.ID.getLoc();
+ if (!UseBeforeDefVariables.count(Use.Var))
+ continue;
+
+ for (DbgOp &Op : Use.Values) {
+ assert(!Op.isUndef() && "UseBeforeDef erroneously created for a "
+ "DbgValue with undef values.");
+ if (Op.IsConst)
+ continue;
+
+ ValueToLoc.insert({Op.ID, LocationAndQuality()});
+ }
+ }
- // If something goes very wrong, we might end up labelling a COPY
- // instruction or similar with an instruction number, where it doesn't
- // actually define a new value, instead it moves a value. In case this
- // happens, discard.
- if (MTracker->readMLoc(L) != Use.ID)
+ // Exit early if we have no DbgValues to produce.
+ if (ValueToLoc.empty())
+ return;
+
+ // Determine the best location for each desired value.
+ for (auto Location : MTracker->locations()) {
+ LocIdx Idx = Location.Idx;
+ ValueIDNum &LocValueID = Location.Value;
+
+ // Is there a variable that wants a location for this value? If not, skip.
+ auto VIt = ValueToLoc.find(LocValueID);
+ if (VIt == ValueToLoc.end())
continue;
- // If a different debug instruction defined the variable value / location
- // since the start of the block, don't materialize this use-before-def.
+ auto &Previous = VIt->second;
+ // If this is the first location with that value, pick it. Otherwise,
+ // consider whether it's a "longer term" location.
+ std::optional<LocationQuality> ReplacementQuality =
+ getLocQualityIfBetter(Idx, Previous.getQuality());
+ if (ReplacementQuality)
+ Previous = LocationAndQuality(Idx, *ReplacementQuality);
+ }
+
+ // Using the map of values to locations, produce a final set of values for
+ // this variable.
+ for (auto &Use : MIt->second) {
if (!UseBeforeDefVariables.count(Use.Var))
continue;
- PendingDbgValues.push_back(MTracker->emitLoc(L, Use.Var, Use.Properties));
+ SmallVector<ResolvedDbgOp> DbgOps;
+
+ for (DbgOp &Op : Use.Values) {
+ if (Op.IsConst) {
+ DbgOps.push_back(Op.MO);
+ continue;
+ }
+ LocIdx NewLoc = ValueToLoc.find(Op.ID)->second.getLoc();
+ if (NewLoc.isIllegal())
+ break;
+ DbgOps.push_back(NewLoc);
+ }
+
+ // If at least one value used by this debug value is no longer available,
+ // i.e. one of the values was killed before we finished defining all of
+ // the values used by this variable, discard.
+ if (DbgOps.size() != Use.Values.size())
+ continue;
+
+ // Otherwise, we're good to go.
+ PendingDbgValues.push_back(
+ MTracker->emitLoc(DbgOps, Use.Var, Use.Properties));
}
flushDbgValues(pos, nullptr);
}
@@ -440,8 +630,21 @@ public:
if (!ShouldEmitDebugEntryValues)
return false;
+ const DIExpression *DIExpr = Prop.DIExpr;
+
+ // We don't currently emit entry values for DBG_VALUE_LISTs.
+ if (Prop.IsVariadic) {
+ // If this debug value can be converted to be non-variadic, then do so;
+ // otherwise give up.
+ auto NonVariadicExpression =
+ DIExpression::convertToNonVariadicExpression(DIExpr);
+ if (!NonVariadicExpression)
+ return false;
+ DIExpr = *NonVariadicExpression;
+ }
+
// Is the variable appropriate for entry values (i.e., is a parameter).
- if (!isEntryValueVariable(Var, Prop.DIExpr))
+ if (!isEntryValueVariable(Var, DIExpr))
return false;
// Is the value assigned to this variable still the entry value?
@@ -450,11 +653,12 @@ public:
// Emit a variable location using an entry value expression.
DIExpression *NewExpr =
- DIExpression::prepend(Prop.DIExpr, DIExpression::EntryValue);
+ DIExpression::prepend(DIExpr, DIExpression::EntryValue);
Register Reg = MTracker->LocIdxToLocID[Num.getLoc()];
MachineOperand MO = MachineOperand::CreateReg(Reg, false);
- PendingDbgValues.push_back(emitMOLoc(MO, Var, {NewExpr, Prop.Indirect}));
+ PendingDbgValues.push_back(
+ emitMOLoc(MO, Var, {NewExpr, Prop.Indirect, false}));
return true;
}
@@ -464,62 +668,100 @@ public:
MI.getDebugLoc()->getInlinedAt());
DbgValueProperties Properties(MI);
- const MachineOperand &MO = MI.getOperand(0);
-
// Ignore non-register locations, we don't transfer those.
- if (!MO.isReg() || MO.getReg() == 0) {
+ if (MI.isUndefDebugValue() ||
+ all_of(MI.debug_operands(),
+ [](const MachineOperand &MO) { return !MO.isReg(); })) {
auto It = ActiveVLocs.find(Var);
if (It != ActiveVLocs.end()) {
- ActiveMLocs[It->second.Loc].erase(Var);
+ for (LocIdx Loc : It->second.loc_indices())
+ ActiveMLocs[Loc].erase(Var);
ActiveVLocs.erase(It);
- }
+ }
// Any use-before-defs no longer apply.
UseBeforeDefVariables.erase(Var);
return;
}
- Register Reg = MO.getReg();
- LocIdx NewLoc = MTracker->getRegMLoc(Reg);
- redefVar(MI, Properties, NewLoc);
+ SmallVector<ResolvedDbgOp> NewLocs;
+ for (const MachineOperand &MO : MI.debug_operands()) {
+ if (MO.isReg()) {
+ // Any undef regs have already been filtered out above.
+ Register Reg = MO.getReg();
+ LocIdx NewLoc = MTracker->getRegMLoc(Reg);
+ NewLocs.push_back(NewLoc);
+ } else {
+ NewLocs.push_back(MO);
+ }
+ }
+
+ redefVar(MI, Properties, NewLocs);
}
/// Handle a change in variable location within a block. Terminate the
/// variables current location, and record the value it now refers to, so
/// that we can detect location transfers later on.
void redefVar(const MachineInstr &MI, const DbgValueProperties &Properties,
- Optional<LocIdx> OptNewLoc) {
+ SmallVectorImpl<ResolvedDbgOp> &NewLocs) {
DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
MI.getDebugLoc()->getInlinedAt());
// Any use-before-defs no longer apply.
UseBeforeDefVariables.erase(Var);
- // Erase any previous location,
+ // Erase any previous location.
auto It = ActiveVLocs.find(Var);
- if (It != ActiveVLocs.end())
- ActiveMLocs[It->second.Loc].erase(Var);
+ if (It != ActiveVLocs.end()) {
+ for (LocIdx Loc : It->second.loc_indices())
+ ActiveMLocs[Loc].erase(Var);
+ }
// If there _is_ no new location, all we had to do was erase.
- if (!OptNewLoc)
+ if (NewLocs.empty()) {
+ if (It != ActiveVLocs.end())
+ ActiveVLocs.erase(It);
return;
- LocIdx NewLoc = *OptNewLoc;
-
- // Check whether our local copy of values-by-location in #VarLocs is out of
- // date. Wipe old tracking data for the location if it's been clobbered in
- // the meantime.
- if (MTracker->readMLoc(NewLoc) != VarLocs[NewLoc.asU64()]) {
- for (const auto &P : ActiveMLocs[NewLoc]) {
- ActiveVLocs.erase(P);
+ }
+
+ SmallVector<std::pair<LocIdx, DebugVariable>> LostMLocs;
+ for (ResolvedDbgOp &Op : NewLocs) {
+ if (Op.IsConst)
+ continue;
+
+ LocIdx NewLoc = Op.Loc;
+
+ // Check whether our local copy of values-by-location in #VarLocs is out
+ // of date. Wipe old tracking data for the location if it's been clobbered
+ // in the meantime.
+ if (MTracker->readMLoc(NewLoc) != VarLocs[NewLoc.asU64()]) {
+ for (const auto &P : ActiveMLocs[NewLoc]) {
+ auto LostVLocIt = ActiveVLocs.find(P);
+ if (LostVLocIt != ActiveVLocs.end()) {
+ for (LocIdx Loc : LostVLocIt->second.loc_indices()) {
+ // Every active variable mapping for NewLoc will be cleared, no
+ // need to track individual variables.
+ if (Loc == NewLoc)
+ continue;
+ LostMLocs.emplace_back(Loc, P);
+ }
+ }
+ ActiveVLocs.erase(P);
+ }
+ for (const auto &LostMLoc : LostMLocs)
+ ActiveMLocs[LostMLoc.first].erase(LostMLoc.second);
+ LostMLocs.clear();
+ It = ActiveVLocs.find(Var);
+ ActiveMLocs[NewLoc.asU64()].clear();
+ VarLocs[NewLoc.asU64()] = MTracker->readMLoc(NewLoc);
}
- ActiveMLocs[NewLoc.asU64()].clear();
- VarLocs[NewLoc.asU64()] = MTracker->readMLoc(NewLoc);
+
+ ActiveMLocs[NewLoc].insert(Var);
}
- ActiveMLocs[NewLoc].insert(Var);
if (It == ActiveVLocs.end()) {
ActiveVLocs.insert(
- std::make_pair(Var, LocAndProperties{NewLoc, Properties}));
+ std::make_pair(Var, ResolvedDbgValue(NewLocs, Properties)));
} else {
- It->second.Loc = NewLoc;
+ It->second.Ops.assign(NewLocs);
It->second.Properties = Properties;
}
}
@@ -551,7 +793,7 @@ public:
// Examine the remaining variable locations: if we can find the same value
// again, we can recover the location.
- Optional<LocIdx> NewLoc = None;
+ std::optional<LocIdx> NewLoc;
for (auto Loc : MTracker->locations())
if (Loc.Value == OldValue)
NewLoc = Loc.Idx;
@@ -570,28 +812,54 @@ public:
// Examine all the variables based on this location.
DenseSet<DebugVariable> NewMLocs;
+ // If no new location has been found, every variable that depends on this
+ // MLoc is dead, so end their existing MLoc->Var mappings as well.
+ SmallVector<std::pair<LocIdx, DebugVariable>> LostMLocs;
for (const auto &Var : ActiveMLocIt->second) {
auto ActiveVLocIt = ActiveVLocs.find(Var);
// Re-state the variable location: if there's no replacement then NewLoc
- // is None and a $noreg DBG_VALUE will be created. Otherwise, a DBG_VALUE
- // identifying the alternative location will be emitted.
+ // is std::nullopt and a $noreg DBG_VALUE will be created. Otherwise, a
+ // DBG_VALUE identifying the alternative location will be emitted.
const DbgValueProperties &Properties = ActiveVLocIt->second.Properties;
- PendingDbgValues.push_back(MTracker->emitLoc(NewLoc, Var, Properties));
+
+ // Produce the new list of debug ops - an empty list if no new location
+ // was found, or the existing list with the substitution MLoc -> NewLoc
+ // otherwise.
+ SmallVector<ResolvedDbgOp> DbgOps;
+ if (NewLoc) {
+ ResolvedDbgOp OldOp(MLoc);
+ ResolvedDbgOp NewOp(*NewLoc);
+ // Insert illegal ops to overwrite afterwards.
+ DbgOps.insert(DbgOps.begin(), ActiveVLocIt->second.Ops.size(),
+ ResolvedDbgOp(LocIdx::MakeIllegalLoc()));
+ replace_copy(ActiveVLocIt->second.Ops, DbgOps.begin(), OldOp, NewOp);
+ }
+
+ PendingDbgValues.push_back(MTracker->emitLoc(DbgOps, Var, Properties));
// Update machine locations <=> variable locations maps. Defer updating
- // ActiveMLocs to avoid invalidaing the ActiveMLocIt iterator.
+ // ActiveMLocs to avoid invalidating the ActiveMLocIt iterator.
if (!NewLoc) {
+ for (LocIdx Loc : ActiveVLocIt->second.loc_indices()) {
+ if (Loc != MLoc)
+ LostMLocs.emplace_back(Loc, Var);
+ }
ActiveVLocs.erase(ActiveVLocIt);
} else {
- ActiveVLocIt->second.Loc = *NewLoc;
+ ActiveVLocIt->second.Ops = DbgOps;
NewMLocs.insert(Var);
}
}
- // Commit any deferred ActiveMLoc changes.
- if (!NewMLocs.empty())
- for (auto &Var : NewMLocs)
- ActiveMLocs[*NewLoc].insert(Var);
+ // Remove variables from ActiveMLocs if they no longer use any other MLocs
+ // due to being killed by this clobber.
+ for (auto &LocVarIt : LostMLocs) {
+ auto LostMLocIt = ActiveMLocs.find(LocVarIt.first);
+ assert(LostMLocIt != ActiveMLocs.end() &&
+ "Variable was using this MLoc, but ActiveMLocs[MLoc] has no "
+ "entries?");
+ LostMLocIt->second.erase(LocVarIt.second);
+ }
// We lazily track what locations have which values; if we've found a new
// location for the clobbered value, remember it.
@@ -600,9 +868,11 @@ public:
flushDbgValues(Pos, nullptr);
- // Re-find ActiveMLocIt, iterator could have been invalidated.
- ActiveMLocIt = ActiveMLocs.find(MLoc);
+ // Commit ActiveMLoc changes.
ActiveMLocIt->second.clear();
+ if (!NewMLocs.empty())
+ for (auto &Var : NewMLocs)
+ ActiveMLocs[*NewLoc].insert(Var);
}
/// Transfer variables based on \p Src to be based on \p Dst. This handles
@@ -619,17 +889,22 @@ public:
// Move set of active variables from one location to another.
auto MovingVars = ActiveMLocs[Src];
- ActiveMLocs[Dst] = MovingVars;
+ ActiveMLocs[Dst].insert(MovingVars.begin(), MovingVars.end());
VarLocs[Dst.asU64()] = VarLocs[Src.asU64()];
// For each variable based on Src; create a location at Dst.
+ ResolvedDbgOp SrcOp(Src);
+ ResolvedDbgOp DstOp(Dst);
for (const auto &Var : MovingVars) {
auto ActiveVLocIt = ActiveVLocs.find(Var);
assert(ActiveVLocIt != ActiveVLocs.end());
- ActiveVLocIt->second.Loc = Dst;
- MachineInstr *MI =
- MTracker->emitLoc(Dst, Var, ActiveVLocIt->second.Properties);
+ // Update all instances of Src in the variable's tracked values to Dst.
+ std::replace(ActiveVLocIt->second.Ops.begin(),
+ ActiveVLocIt->second.Ops.end(), SrcOp, DstOp);
+
+ MachineInstr *MI = MTracker->emitLoc(ActiveVLocIt->second.Ops, Var,
+ ActiveVLocIt->second.Properties);
PendingDbgValues.push_back(MI);
}
ActiveMLocs[Src].clear();
@@ -667,17 +942,43 @@ ValueIDNum ValueIDNum::EmptyValue = {UINT_MAX, UINT_MAX, UINT_MAX};
ValueIDNum ValueIDNum::TombstoneValue = {UINT_MAX, UINT_MAX, UINT_MAX - 1};
#ifndef NDEBUG
-void DbgValue::dump(const MLocTracker *MTrack) const {
- if (Kind == Const) {
- MO->dump();
- } else if (Kind == NoVal) {
- dbgs() << "NoVal(" << BlockNo << ")";
- } else if (Kind == VPHI) {
- dbgs() << "VPHI(" << BlockNo << "," << MTrack->IDAsString(ID) << ")";
+void ResolvedDbgOp::dump(const MLocTracker *MTrack) const {
+ if (IsConst) {
+ dbgs() << MO;
} else {
- assert(Kind == Def);
+ dbgs() << MTrack->LocIdxToName(Loc);
+ }
+}
+void DbgOp::dump(const MLocTracker *MTrack) const {
+ if (IsConst) {
+ dbgs() << MO;
+ } else if (!isUndef()) {
dbgs() << MTrack->IDAsString(ID);
}
+}
+void DbgOpID::dump(const MLocTracker *MTrack, const DbgOpIDMap *OpStore) const {
+ if (!OpStore) {
+ dbgs() << "ID(" << asU32() << ")";
+ } else {
+ OpStore->find(*this).dump(MTrack);
+ }
+}
+void DbgValue::dump(const MLocTracker *MTrack,
+ const DbgOpIDMap *OpStore) const {
+ if (Kind == NoVal) {
+ dbgs() << "NoVal(" << BlockNo << ")";
+ } else if (Kind == VPHI || Kind == Def) {
+ if (Kind == VPHI)
+ dbgs() << "VPHI(" << BlockNo << ",";
+ else
+ dbgs() << "Def(";
+ for (unsigned Idx = 0; Idx < getDbgOpIDs().size(); ++Idx) {
+ getDbgOpID(Idx).dump(MTrack, OpStore);
+ if (Idx != 0)
+ dbgs() << ",";
+ }
+ dbgs() << ")";
+ }
if (Properties.Indirect)
dbgs() << " indir";
if (Properties.DIExpr)
@@ -789,14 +1090,14 @@ void MLocTracker::writeRegMask(const MachineOperand *MO, unsigned CurBB,
Masks.push_back(std::make_pair(MO, InstID));
}
-Optional<SpillLocationNo> MLocTracker::getOrTrackSpillLoc(SpillLoc L) {
+std::optional<SpillLocationNo> MLocTracker::getOrTrackSpillLoc(SpillLoc L) {
SpillLocationNo SpillID(SpillLocs.idFor(L));
if (SpillID.id() == 0) {
// If there is no location, and we have reached the limit of how many stack
// slots to track, then don't track this one.
if (SpillLocs.size() >= StackWorkingSetLimit)
- return None;
+ return std::nullopt;
// Spill location is untracked: create record for this one, and all
// subregister slots too.
@@ -853,120 +1154,157 @@ LLVM_DUMP_METHOD void MLocTracker::dump_mloc_map() {
}
#endif
-MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc,
- const DebugVariable &Var,
- const DbgValueProperties &Properties) {
+MachineInstrBuilder
+MLocTracker::emitLoc(const SmallVectorImpl<ResolvedDbgOp> &DbgOps,
+ const DebugVariable &Var,
+ const DbgValueProperties &Properties) {
DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0,
Var.getVariable()->getScope(),
const_cast<DILocation *>(Var.getInlinedAt()));
- auto MIB = BuildMI(MF, DL, TII.get(TargetOpcode::DBG_VALUE));
+
+ const MCInstrDesc &Desc = Properties.IsVariadic
+ ? TII.get(TargetOpcode::DBG_VALUE_LIST)
+ : TII.get(TargetOpcode::DBG_VALUE);
+
+#ifdef EXPENSIVE_CHECKS
+ assert(all_of(DbgOps,
+ [](const ResolvedDbgOp &Op) {
+ return Op.IsConst || !Op.Loc.isIllegal();
+ }) &&
+ "Did not expect illegal ops in DbgOps.");
+ assert((DbgOps.size() == 0 ||
+ DbgOps.size() == Properties.getLocationOpCount()) &&
+ "Expected to have either one DbgOp per MI LocationOp, or none.");
+#endif
+
+ auto GetRegOp = [](unsigned Reg) -> MachineOperand {
+ return MachineOperand::CreateReg(
+ /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
+ /* isKill */ false, /* isDead */ false,
+ /* isUndef */ false, /* isEarlyClobber */ false,
+ /* SubReg */ 0, /* isDebug */ true);
+ };
+
+ SmallVector<MachineOperand> MOs;
+
+ auto EmitUndef = [&]() {
+ MOs.clear();
+ MOs.assign(Properties.getLocationOpCount(), GetRegOp(0));
+ return BuildMI(MF, DL, Desc, false, MOs, Var.getVariable(),
+ Properties.DIExpr);
+ };
+
+ // Don't bother passing any real operands to BuildMI if any of them would be
+ // $noreg.
+ if (DbgOps.empty())
+ return EmitUndef();
+
+ bool Indirect = Properties.Indirect;
const DIExpression *Expr = Properties.DIExpr;
- if (!MLoc) {
- // No location -> DBG_VALUE $noreg
- MIB.addReg(0);
- MIB.addReg(0);
- } else if (LocIdxToLocID[*MLoc] >= NumRegs) {
- unsigned LocID = LocIdxToLocID[*MLoc];
- SpillLocationNo SpillID = locIDToSpill(LocID);
- StackSlotPos StackIdx = locIDToSpillIdx(LocID);
- unsigned short Offset = StackIdx.second;
-
- // TODO: support variables that are located in spill slots, with non-zero
- // offsets from the start of the spill slot. It would require some more
- // complex DIExpression calculations. This doesn't seem to be produced by
- // LLVM right now, so don't try and support it.
- // Accept no-subregister slots and subregisters where the offset is zero.
- // The consumer should already have type information to work out how large
- // the variable is.
- if (Offset == 0) {
- const SpillLoc &Spill = SpillLocs[SpillID.id()];
- unsigned Base = Spill.SpillBase;
- MIB.addReg(Base);
-
- // There are several ways we can dereference things, and several inputs
- // to consider:
- // * NRVO variables will appear with IsIndirect set, but should have
- // nothing else in their DIExpressions,
- // * Variables with DW_OP_stack_value in their expr already need an
- // explicit dereference of the stack location,
- // * Values that don't match the variable size need DW_OP_deref_size,
- // * Everything else can just become a simple location expression.
-
- // We need to use deref_size whenever there's a mismatch between the
- // size of value and the size of variable portion being read.
- // Additionally, we should use it whenever dealing with stack_value
- // fragments, to avoid the consumer having to determine the deref size
- // from DW_OP_piece.
- bool UseDerefSize = false;
- unsigned ValueSizeInBits = getLocSizeInBits(*MLoc);
- unsigned DerefSizeInBytes = ValueSizeInBits / 8;
- if (auto Fragment = Var.getFragment()) {
- unsigned VariableSizeInBits = Fragment->SizeInBits;
- if (VariableSizeInBits != ValueSizeInBits || Expr->isComplex())
- UseDerefSize = true;
- } else if (auto Size = Var.getVariable()->getSizeInBits()) {
- if (*Size != ValueSizeInBits) {
- UseDerefSize = true;
+
+ assert(DbgOps.size() == Properties.getLocationOpCount());
+
+ // If all locations are valid, accumulate them into our list of
+ // MachineOperands. For any spilled locations, either update the indirectness
+ // register or apply the appropriate transformations in the DIExpression.
+ for (size_t Idx = 0; Idx < Properties.getLocationOpCount(); ++Idx) {
+ const ResolvedDbgOp &Op = DbgOps[Idx];
+
+ if (Op.IsConst) {
+ MOs.push_back(Op.MO);
+ continue;
+ }
+
+ LocIdx MLoc = Op.Loc;
+ unsigned LocID = LocIdxToLocID[MLoc];
+ if (LocID >= NumRegs) {
+ SpillLocationNo SpillID = locIDToSpill(LocID);
+ StackSlotPos StackIdx = locIDToSpillIdx(LocID);
+ unsigned short Offset = StackIdx.second;
+
+ // TODO: support variables that are located in spill slots, with non-zero
+ // offsets from the start of the spill slot. It would require some more
+ // complex DIExpression calculations. This doesn't seem to be produced by
+ // LLVM right now, so don't try and support it.
+ // Accept no-subregister slots and subregisters where the offset is zero.
+ // The consumer should already have type information to work out how large
+ // the variable is.
+ if (Offset == 0) {
+ const SpillLoc &Spill = SpillLocs[SpillID.id()];
+ unsigned Base = Spill.SpillBase;
+
+ // There are several ways we can dereference things, and several inputs
+ // to consider:
+ // * NRVO variables will appear with IsIndirect set, but should have
+ // nothing else in their DIExpressions,
+ // * Variables with DW_OP_stack_value in their expr already need an
+ // explicit dereference of the stack location,
+ // * Values that don't match the variable size need DW_OP_deref_size,
+ // * Everything else can just become a simple location expression.
+
+ // We need to use deref_size whenever there's a mismatch between the
+ // size of value and the size of variable portion being read.
+ // Additionally, we should use it whenever dealing with stack_value
+ // fragments, to avoid the consumer having to determine the deref size
+ // from DW_OP_piece.
+ bool UseDerefSize = false;
+ unsigned ValueSizeInBits = getLocSizeInBits(MLoc);
+ unsigned DerefSizeInBytes = ValueSizeInBits / 8;
+ if (auto Fragment = Var.getFragment()) {
+ unsigned VariableSizeInBits = Fragment->SizeInBits;
+ if (VariableSizeInBits != ValueSizeInBits || Expr->isComplex())
+ UseDerefSize = true;
+ } else if (auto Size = Var.getVariable()->getSizeInBits()) {
+ if (*Size != ValueSizeInBits) {
+ UseDerefSize = true;
+ }
}
- }
- if (Properties.Indirect) {
- // This is something like an NRVO variable, where the pointer has been
- // spilt to the stack, or a dbg.addr pointing at a coroutine frame
- // field. It should end up being a memory location, with the pointer
- // to the variable loaded off the stack with a deref. It can't be a
- // DW_OP_stack_value expression.
- assert(!Expr->isImplicit());
- Expr = TRI.prependOffsetExpression(
- Expr, DIExpression::ApplyOffset | DIExpression::DerefAfter,
- Spill.SpillOffset);
- MIB.addImm(0);
- } else if (UseDerefSize) {
- // We're loading a value off the stack that's not the same size as the
- // variable. Add / subtract stack offset, explicitly deref with a size,
- // and add DW_OP_stack_value if not already present.
- SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size,
- DerefSizeInBytes};
- Expr = DIExpression::prependOpcodes(Expr, Ops, true);
- unsigned Flags = DIExpression::StackValue | DIExpression::ApplyOffset;
- Expr = TRI.prependOffsetExpression(Expr, Flags, Spill.SpillOffset);
- MIB.addReg(0);
- } else if (Expr->isComplex()) {
- // A variable with no size ambiguity, but with extra elements in it's
- // expression. Manually dereference the stack location.
- assert(Expr->isComplex());
- Expr = TRI.prependOffsetExpression(
- Expr, DIExpression::ApplyOffset | DIExpression::DerefAfter,
- Spill.SpillOffset);
- MIB.addReg(0);
+ SmallVector<uint64_t, 5> OffsetOps;
+ TRI.getOffsetOpcodes(Spill.SpillOffset, OffsetOps);
+ bool StackValue = false;
+
+ if (Properties.Indirect) {
+ // This is something like an NRVO variable, where the pointer has been
+ // spilt to the stack. It should end up being a memory location, with
+ // the pointer to the variable loaded off the stack with a deref:
+ assert(!Expr->isImplicit());
+ OffsetOps.push_back(dwarf::DW_OP_deref);
+ } else if (UseDerefSize && Expr->isSingleLocationExpression()) {
+ // TODO: Figure out how to handle deref size issues for variadic
+ // values.
+ // We're loading a value off the stack that's not the same size as the
+ // variable. Add / subtract stack offset, explicitly deref with a
+ // size, and add DW_OP_stack_value if not already present.
+ OffsetOps.push_back(dwarf::DW_OP_deref_size);
+ OffsetOps.push_back(DerefSizeInBytes);
+ StackValue = true;
+ } else if (Expr->isComplex() || Properties.IsVariadic) {
+ // A variable with no size ambiguity, but with extra elements in it's
+ // expression. Manually dereference the stack location.
+ OffsetOps.push_back(dwarf::DW_OP_deref);
+ } else {
+ // A plain value that has been spilt to the stack, with no further
+ // context. Request a location expression, marking the DBG_VALUE as
+ // IsIndirect.
+ Indirect = true;
+ }
+
+ Expr = DIExpression::appendOpsToArg(Expr, OffsetOps, Idx, StackValue);
+ MOs.push_back(GetRegOp(Base));
} else {
- // A plain value that has been spilt to the stack, with no further
- // context. Request a location expression, marking the DBG_VALUE as
- // IsIndirect.
- Expr = TRI.prependOffsetExpression(Expr, DIExpression::ApplyOffset,
- Spill.SpillOffset);
- MIB.addImm(0);
+ // This is a stack location with a weird subregister offset: emit an
+ // undef DBG_VALUE instead.
+ return EmitUndef();
}
} else {
- // This is a stack location with a weird subregister offset: emit an undef
- // DBG_VALUE instead.
- MIB.addReg(0);
- MIB.addReg(0);
+ // Non-empty, non-stack slot, must be a plain register.
+ MOs.push_back(GetRegOp(LocID));
}
- } else {
- // Non-empty, non-stack slot, must be a plain register.
- unsigned LocID = LocIdxToLocID[*MLoc];
- MIB.addReg(LocID);
- if (Properties.Indirect)
- MIB.addImm(0);
- else
- MIB.addReg(0);
}
- MIB.addMetadata(Var.getVariable());
- MIB.addMetadata(Expr);
- return MIB;
+ return BuildMI(MF, DL, Desc, Indirect, MOs, Var.getVariable(), Expr);
}
/// Default construct and initialize the pass.
@@ -974,7 +1312,10 @@ InstrRefBasedLDV::InstrRefBasedLDV() = default;
bool InstrRefBasedLDV::isCalleeSaved(LocIdx L) const {
unsigned Reg = MTracker->LocIdxToLocID[L];
- for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
+ return isCalleeSavedReg(Reg);
+}
+bool InstrRefBasedLDV::isCalleeSavedReg(Register R) const {
+ for (MCRegAliasIterator RAI(R, TRI, true); RAI.isValid(); ++RAI)
if (CalleeSavedRegs.test(*RAI))
return true;
return false;
@@ -989,7 +1330,7 @@ bool InstrRefBasedLDV::isCalleeSaved(LocIdx L) const {
// void InstrRefBasedLDV::printVarLocInMBB(..)
#endif
-Optional<SpillLocationNo>
+std::optional<SpillLocationNo>
InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
assert(MI.hasOneMemOperand() &&
"Spill instruction does not have exactly one memory operand?");
@@ -1004,11 +1345,11 @@ InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
return MTracker->getOrTrackSpillLoc({Reg, Offset});
}
-Optional<LocIdx>
+std::optional<LocIdx>
InstrRefBasedLDV::findLocationForMemOperand(const MachineInstr &MI) {
- Optional<SpillLocationNo> SpillLoc = extractSpillBaseRegAndOffset(MI);
+ std::optional<SpillLocationNo> SpillLoc = extractSpillBaseRegAndOffset(MI);
if (!SpillLoc)
- return None;
+ return std::nullopt;
// Where in the stack slot is this value defined -- i.e., what size of value
// is this? An important question, because it could be loaded into a register
@@ -1022,7 +1363,7 @@ InstrRefBasedLDV::findLocationForMemOperand(const MachineInstr &MI) {
if (IdxIt == MTracker->StackSlotIdxes.end())
// That index is not tracked. This is suprising, and unlikely to ever
// occur, but the safe action is to indicate the variable is optimised out.
- return None;
+ return std::nullopt;
unsigned SpillID = MTracker->getSpillIDWithIdx(*SpillLoc, IdxIt->second);
return MTracker->getSpillMLoc(SpillID);
@@ -1050,39 +1391,33 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
if (Scope == nullptr)
return true; // handled it; by doing nothing
- // For now, ignore DBG_VALUE_LISTs when extending ranges. Allow it to
- // contribute to locations in this block, but don't propagate further.
- // Interpret it like a DBG_VALUE $noreg.
- if (MI.isDebugValueList()) {
- if (VTracker)
- VTracker->defVar(MI, Properties, None);
- if (TTracker)
- TTracker->redefVar(MI, Properties, None);
- return true;
- }
-
- const MachineOperand &MO = MI.getOperand(0);
-
// MLocTracker needs to know that this register is read, even if it's only
// read by a debug inst.
- if (MO.isReg() && MO.getReg() != 0)
- (void)MTracker->readReg(MO.getReg());
+ for (const MachineOperand &MO : MI.debug_operands())
+ if (MO.isReg() && MO.getReg() != 0)
+ (void)MTracker->readReg(MO.getReg());
// If we're preparing for the second analysis (variables), the machine value
// locations are already solved, and we report this DBG_VALUE and the value
// it refers to to VLocTracker.
if (VTracker) {
- if (MO.isReg()) {
- // Feed defVar the new variable location, or if this is a
- // DBG_VALUE $noreg, feed defVar None.
- if (MO.getReg())
- VTracker->defVar(MI, Properties, MTracker->readReg(MO.getReg()));
- else
- VTracker->defVar(MI, Properties, None);
- } else if (MI.getOperand(0).isImm() || MI.getOperand(0).isFPImm() ||
- MI.getOperand(0).isCImm()) {
- VTracker->defVar(MI, MI.getOperand(0));
+ SmallVector<DbgOpID> DebugOps;
+ // Feed defVar the new variable location, or if this is a DBG_VALUE $noreg,
+ // feed defVar None.
+ if (!MI.isUndefDebugValue()) {
+ for (const MachineOperand &MO : MI.debug_operands()) {
+ // There should be no undef registers here, as we've screened for undef
+ // debug values.
+ if (MO.isReg()) {
+ DebugOps.push_back(DbgOpStore.insert(MTracker->readReg(MO.getReg())));
+ } else if (MO.isImm() || MO.isFPImm() || MO.isCImm()) {
+ DebugOps.push_back(DbgOpStore.insert(MO));
+ } else {
+ llvm_unreachable("Unexpected debug operand type.");
+ }
+ }
}
+ VTracker->defVar(MI, Properties, DebugOps);
}
// If performing final tracking of transfers, report this variable definition
@@ -1092,39 +1427,14 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
return true;
}
-bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
- const ValueTable *MLiveOuts,
- const ValueTable *MLiveIns) {
- if (!MI.isDebugRef())
- return false;
-
- // Only handle this instruction when we are building the variable value
- // transfer function.
- if (!VTracker && !TTracker)
- return false;
-
- unsigned InstNo = MI.getOperand(0).getImm();
- unsigned OpNo = MI.getOperand(1).getImm();
-
- const DILocalVariable *Var = MI.getDebugVariable();
- const DIExpression *Expr = MI.getDebugExpression();
- const DILocation *DebugLoc = MI.getDebugLoc();
- const DILocation *InlinedAt = DebugLoc->getInlinedAt();
- assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
- "Expected inlined-at fields to agree");
-
- DebugVariable V(Var, Expr, InlinedAt);
-
- auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get());
- if (Scope == nullptr)
- return true; // Handled by doing nothing. This variable is never in scope.
-
- const MachineFunction &MF = *MI.getParent()->getParent();
-
+std::optional<ValueIDNum> InstrRefBasedLDV::getValueForInstrRef(
+ unsigned InstNo, unsigned OpNo, MachineInstr &MI,
+ const ValueTable *MLiveOuts, const ValueTable *MLiveIns) {
// Various optimizations may have happened to the value during codegen,
// recorded in the value substitution table. Apply any substitutions to
// the instruction / operand number in this DBG_INSTR_REF, and collect
// any subregister extractions performed during optimization.
+ const MachineFunction &MF = *MI.getParent()->getParent();
// Create dummy substitution with Src set, for lookup.
auto SoughtSub =
@@ -1143,13 +1453,12 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
// Default machine value number is <None> -- if no instruction defines
// the corresponding value, it must have been optimized out.
- Optional<ValueIDNum> NewID = None;
+ std::optional<ValueIDNum> NewID;
// Try to lookup the instruction number, and find the machine value number
// that it defines. It could be an instruction, or a PHI.
auto InstrIt = DebugInstrNumToInstr.find(InstNo);
- auto PHIIt = std::lower_bound(DebugPHINumToValue.begin(),
- DebugPHINumToValue.end(), InstNo);
+ auto PHIIt = llvm::lower_bound(DebugPHINumToValue, InstNo);
if (InstrIt != DebugInstrNumToInstr.end()) {
const MachineInstr &TargetInstr = *InstrIt->second.first;
uint64_t BlockNo = TargetInstr.getParent()->getNumber();
@@ -1158,7 +1467,7 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
// a register def was folded into a stack store.
if (OpNo == MachineFunction::DebugOperandMemNumber &&
TargetInstr.hasOneMemOperand()) {
- Optional<LocIdx> L = findLocationForMemOperand(TargetInstr);
+ std::optional<LocIdx> L = findLocationForMemOperand(TargetInstr);
if (L)
NewID = ValueIDNum(BlockNo, InstrIt->second.second, *L);
} else if (OpNo != MachineFunction::DebugOperandMemNumber) {
@@ -1247,7 +1556,7 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
// If we didn't find anything: there's no way to express our value.
if (!NewReg) {
- NewID = None;
+ NewID = std::nullopt;
} else {
// Re-state the value as being defined within the subregister
// that we found.
@@ -1257,61 +1566,162 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
}
} else {
// If we can't handle subregisters, unset the new value.
- NewID = None;
+ NewID = std::nullopt;
}
}
- // We, we have a value number or None. Tell the variable value tracker about
- // it. The rest of this LiveDebugValues implementation acts exactly the same
- // for DBG_INSTR_REFs as DBG_VALUEs (just, the former can refer to values that
- // aren't immediately available).
- DbgValueProperties Properties(Expr, false);
+ return NewID;
+}
+
+bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
+ const ValueTable *MLiveOuts,
+ const ValueTable *MLiveIns) {
+ if (!MI.isDebugRef())
+ return false;
+
+ // Only handle this instruction when we are building the variable value
+ // transfer function.
+ if (!VTracker && !TTracker)
+ return false;
+
+ const DILocalVariable *Var = MI.getDebugVariable();
+ const DIExpression *Expr = MI.getDebugExpression();
+ const DILocation *DebugLoc = MI.getDebugLoc();
+ const DILocation *InlinedAt = DebugLoc->getInlinedAt();
+ assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
+ "Expected inlined-at fields to agree");
+
+ DebugVariable V(Var, Expr, InlinedAt);
+
+ auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get());
+ if (Scope == nullptr)
+ return true; // Handled by doing nothing. This variable is never in scope.
+
+ SmallVector<DbgOpID> DbgOpIDs;
+ for (const MachineOperand &MO : MI.debug_operands()) {
+ if (!MO.isDbgInstrRef()) {
+ assert(!MO.isReg() && "DBG_INSTR_REF should not contain registers");
+ DbgOpID ConstOpID = DbgOpStore.insert(DbgOp(MO));
+ DbgOpIDs.push_back(ConstOpID);
+ continue;
+ }
+
+ unsigned InstNo = MO.getInstrRefInstrIndex();
+ unsigned OpNo = MO.getInstrRefOpIndex();
+
+ // Default machine value number is <None> -- if no instruction defines
+ // the corresponding value, it must have been optimized out.
+ std::optional<ValueIDNum> NewID =
+ getValueForInstrRef(InstNo, OpNo, MI, MLiveOuts, MLiveIns);
+ // We have a value number or std::nullopt. If the latter, then kill the
+ // entire debug value.
+ if (NewID) {
+ DbgOpIDs.push_back(DbgOpStore.insert(*NewID));
+ } else {
+ DbgOpIDs.clear();
+ break;
+ }
+ }
+
+ // We have a DbgOpID for every value or for none. Tell the variable value
+ // tracker about it. The rest of this LiveDebugValues implementation acts
+ // exactly the same for DBG_INSTR_REFs as DBG_VALUEs (just, the former can
+ // refer to values that aren't immediately available).
+ DbgValueProperties Properties(Expr, false, true);
if (VTracker)
- VTracker->defVar(MI, Properties, NewID);
+ VTracker->defVar(MI, Properties, DbgOpIDs);
// If we're on the final pass through the function, decompose this INSTR_REF
// into a plain DBG_VALUE.
if (!TTracker)
return true;
+ // Fetch the concrete DbgOps now, as we will need them later.
+ SmallVector<DbgOp> DbgOps;
+ for (DbgOpID OpID : DbgOpIDs) {
+ DbgOps.push_back(DbgOpStore.find(OpID));
+ }
+
// Pick a location for the machine value number, if such a location exists.
// (This information could be stored in TransferTracker to make it faster).
- Optional<LocIdx> FoundLoc = None;
+ SmallDenseMap<ValueIDNum, TransferTracker::LocationAndQuality> FoundLocs;
+ SmallVector<ValueIDNum> ValuesToFind;
+ // Initialized the preferred-location map with illegal locations, to be
+ // filled in later.
+ for (const DbgOp &Op : DbgOps) {
+ if (!Op.IsConst)
+ if (FoundLocs.insert({Op.ID, TransferTracker::LocationAndQuality()})
+ .second)
+ ValuesToFind.push_back(Op.ID);
+ }
+
for (auto Location : MTracker->locations()) {
LocIdx CurL = Location.Idx;
ValueIDNum ID = MTracker->readMLoc(CurL);
- if (NewID && ID == NewID) {
- // If this is the first location with that value, pick it. Otherwise,
- // consider whether it's a "longer term" location.
- if (!FoundLoc) {
- FoundLoc = CurL;
- continue;
+ auto ValueToFindIt = find(ValuesToFind, ID);
+ if (ValueToFindIt == ValuesToFind.end())
+ continue;
+ auto &Previous = FoundLocs.find(ID)->second;
+ // If this is the first location with that value, pick it. Otherwise,
+ // consider whether it's a "longer term" location.
+ std::optional<TransferTracker::LocationQuality> ReplacementQuality =
+ TTracker->getLocQualityIfBetter(CurL, Previous.getQuality());
+ if (ReplacementQuality) {
+ Previous = TransferTracker::LocationAndQuality(CurL, *ReplacementQuality);
+ if (Previous.isBest()) {
+ ValuesToFind.erase(ValueToFindIt);
+ if (ValuesToFind.empty())
+ break;
}
-
- if (MTracker->isSpill(CurL))
- FoundLoc = CurL; // Spills are a longer term location.
- else if (!MTracker->isSpill(*FoundLoc) &&
- !MTracker->isSpill(CurL) &&
- !isCalleeSaved(*FoundLoc) &&
- isCalleeSaved(CurL))
- FoundLoc = CurL; // Callee saved regs are longer term than normal.
}
}
+ SmallVector<ResolvedDbgOp> NewLocs;
+ for (const DbgOp &DbgOp : DbgOps) {
+ if (DbgOp.IsConst) {
+ NewLocs.push_back(DbgOp.MO);
+ continue;
+ }
+ LocIdx FoundLoc = FoundLocs.find(DbgOp.ID)->second.getLoc();
+ if (FoundLoc.isIllegal()) {
+ NewLocs.clear();
+ break;
+ }
+ NewLocs.push_back(FoundLoc);
+ }
// Tell transfer tracker that the variable value has changed.
- TTracker->redefVar(MI, Properties, FoundLoc);
-
- // If there was a value with no location; but the value is defined in a
- // later instruction in this block, this is a block-local use-before-def.
- if (!FoundLoc && NewID && NewID->getBlock() == CurBB &&
- NewID->getInst() > CurInst)
- TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false}, *NewID);
+ TTracker->redefVar(MI, Properties, NewLocs);
+
+ // If there were values with no location, but all such values are defined in
+ // later instructions in this block, this is a block-local use-before-def.
+ if (!DbgOps.empty() && NewLocs.empty()) {
+ bool IsValidUseBeforeDef = true;
+ uint64_t LastUseBeforeDef = 0;
+ for (auto ValueLoc : FoundLocs) {
+ ValueIDNum NewID = ValueLoc.first;
+ LocIdx FoundLoc = ValueLoc.second.getLoc();
+ if (!FoundLoc.isIllegal())
+ continue;
+ // If we have an value with no location that is not defined in this block,
+ // then it has no location in this block, leaving this value undefined.
+ if (NewID.getBlock() != CurBB || NewID.getInst() <= CurInst) {
+ IsValidUseBeforeDef = false;
+ break;
+ }
+ LastUseBeforeDef = std::max(LastUseBeforeDef, NewID.getInst());
+ }
+ if (IsValidUseBeforeDef) {
+ TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false, true},
+ DbgOps, LastUseBeforeDef);
+ }
+ }
// Produce a DBG_VALUE representing what this DBG_INSTR_REF meant.
// This DBG_VALUE is potentially a $noreg / undefined location, if
- // FoundLoc is None.
+ // FoundLoc is illegal.
// (XXX -- could morph the DBG_INSTR_REF in the future).
- MachineInstr *DbgMI = MTracker->emitLoc(FoundLoc, V, Properties);
+ MachineInstr *DbgMI = MTracker->emitLoc(NewLocs, V, Properties);
+
TTracker->PendingDbgValues.push_back(DbgMI);
TTracker->flushDbgValues(MI.getIterator(), nullptr);
return true;
@@ -1335,7 +1745,8 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
// a DBG_PHI. This can happen if DBG_PHIs are malformed, or refer to a
// dead stack slot, for example.
// Record a DebugPHIRecord with an empty value + location.
- DebugPHINumToValue.push_back({InstrNum, MI.getParent(), None, None});
+ DebugPHINumToValue.push_back(
+ {InstrNum, MI.getParent(), std::nullopt, std::nullopt});
return true;
};
@@ -1364,7 +1775,7 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
Register Base;
StackOffset Offs = TFI->getFrameIndexReference(*MI.getMF(), FI, Base);
SpillLoc SL = {Base, Offs};
- Optional<SpillLocationNo> SpillNo = MTracker->getOrTrackSpillLoc(SL);
+ std::optional<SpillLocationNo> SpillNo = MTracker->getOrTrackSpillLoc(SL);
// We might be able to find a value, but have chosen not to, to avoid
// tracking too much stack information.
@@ -1437,8 +1848,7 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
SmallVector<const MachineOperand *, 4> RegMaskPtrs;
for (const MachineOperand &MO : MI.operands()) {
// Determine whether the operand is a register def.
- if (MO.isReg() && MO.isDef() && MO.getReg() &&
- Register::isPhysicalRegister(MO.getReg()) &&
+ if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical() &&
!IgnoreSPAlias(MO.getReg())) {
// Remove ranges of all aliased registers.
for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
@@ -1459,7 +1869,8 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
// If this instruction writes to a spill slot, def that slot.
if (hasFoldedStackStore(MI)) {
- if (Optional<SpillLocationNo> SpillNo = extractSpillBaseRegAndOffset(MI)) {
+ if (std::optional<SpillLocationNo> SpillNo =
+ extractSpillBaseRegAndOffset(MI)) {
for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) {
unsigned SpillID = MTracker->getSpillIDWithIdx(*SpillNo, I);
LocIdx L = MTracker->getSpillMLoc(SpillID);
@@ -1501,7 +1912,8 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
// Tell TTracker about any folded stack store.
if (hasFoldedStackStore(MI)) {
- if (Optional<SpillLocationNo> SpillNo = extractSpillBaseRegAndOffset(MI)) {
+ if (std::optional<SpillLocationNo> SpillNo =
+ extractSpillBaseRegAndOffset(MI)) {
for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) {
unsigned SpillID = MTracker->getSpillIDWithIdx(*SpillNo, I);
LocIdx L = MTracker->getSpillMLoc(SpillID);
@@ -1542,22 +1954,22 @@ void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) {
}
}
-Optional<SpillLocationNo>
+std::optional<SpillLocationNo>
InstrRefBasedLDV::isSpillInstruction(const MachineInstr &MI,
MachineFunction *MF) {
// TODO: Handle multiple stores folded into one.
if (!MI.hasOneMemOperand())
- return None;
+ return std::nullopt;
// Reject any memory operand that's aliased -- we can't guarantee its value.
auto MMOI = MI.memoperands_begin();
const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
if (PVal->isAliased(MFI))
- return None;
+ return std::nullopt;
if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII))
- return None; // This is not a spill instruction, since no valid size was
- // returned from either function.
+ return std::nullopt; // This is not a spill instruction, since no valid size
+ // was returned from either function.
return extractSpillBaseRegAndOffset(MI);
}
@@ -1572,11 +1984,11 @@ bool InstrRefBasedLDV::isLocationSpill(const MachineInstr &MI,
return Reg != 0;
}
-Optional<SpillLocationNo>
+std::optional<SpillLocationNo>
InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI,
MachineFunction *MF, unsigned &Reg) {
if (!MI.hasOneMemOperand())
- return None;
+ return std::nullopt;
// FIXME: Handle folded restore instructions with more than one memory
// operand.
@@ -1584,7 +1996,7 @@ InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI,
Reg = MI.getOperand(0).getReg();
return extractSpillBaseRegAndOffset(MI);
}
- return None;
+ return std::nullopt;
}
bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
@@ -1616,12 +2028,12 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
// First, if there are any DBG_VALUEs pointing at a spill slot that is
// written to, terminate that variable location. The value in memory
// will have changed. DbgEntityHistoryCalculator doesn't try to detect this.
- if (Optional<SpillLocationNo> Loc = isSpillInstruction(MI, MF)) {
+ if (std::optional<SpillLocationNo> Loc = isSpillInstruction(MI, MF)) {
// Un-set this location and clobber, so that earlier locations don't
// continue past this store.
for (unsigned SlotIdx = 0; SlotIdx < MTracker->NumSlotIdxes; ++SlotIdx) {
unsigned SpillID = MTracker->getSpillIDWithIdx(*Loc, SlotIdx);
- Optional<LocIdx> MLoc = MTracker->getSpillMLoc(SpillID);
+ std::optional<LocIdx> MLoc = MTracker->getSpillMLoc(SpillID);
if (!MLoc)
continue;
@@ -1667,7 +2079,7 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
unsigned SpillID = MTracker->getLocID(Loc, {Size, 0});
DoTransfer(Reg, SpillID);
} else {
- Optional<SpillLocationNo> Loc = isRestoreInstruction(MI, MF, Reg);
+ std::optional<SpillLocationNo> Loc = isRestoreInstruction(MI, MF, Reg);
if (!Loc)
return false;
@@ -1711,13 +2123,6 @@ bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
const MachineOperand *DestRegOp = DestSrc->Destination;
const MachineOperand *SrcRegOp = DestSrc->Source;
- auto isCalleeSavedReg = [&](unsigned Reg) {
- for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
- if (CalleeSavedRegs.test(*RAI))
- return true;
- return false;
- };
-
Register SrcReg = SrcRegOp->getReg();
Register DestReg = DestRegOp->getReg();
@@ -1791,7 +2196,7 @@ bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
/// \param MI A previously unprocessed debug instruction to analyze for
/// fragment usage.
void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
- assert(MI.isDebugValue() || MI.isDebugRef());
+ assert(MI.isDebugValueLike());
DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(),
MI.getDebugLoc()->getInlinedAt());
FragmentInfo ThisFragment = MIVar.getFragmentOrDefault();
@@ -1896,7 +2301,7 @@ void InstrRefBasedLDV::produceMLocTransferFunction(
process(MI, nullptr, nullptr);
// Also accumulate fragment map.
- if (MI.isDebugValue() || MI.isDebugRef())
+ if (MI.isDebugValueLike())
accumulateFragmentMap(MI);
// Create a map from the instruction number (if present) to the
@@ -1931,7 +2336,7 @@ void InstrRefBasedLDV::produceMLocTransferFunction(
Result.first->second = P;
}
- // Accumulate any bitmask operands into the clobberred reg mask for this
+ // Accumulate any bitmask operands into the clobbered reg mask for this
// block.
for (auto &P : MTracker->Masks) {
BlockMasks[CurBB].clearBitsNotInMask(P.first->getRegMask(), BVWords);
@@ -2353,33 +2758,104 @@ void InstrRefBasedLDV::BlockPHIPlacement(
IDF.calculate(PHIBlocks);
}
-Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
- const MachineBasicBlock &MBB, const DebugVariable &Var,
+bool InstrRefBasedLDV::pickVPHILoc(
+ SmallVectorImpl<DbgOpID> &OutValues, const MachineBasicBlock &MBB,
const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs,
const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
- // Collect a set of locations from predecessor where its live-out value can
- // be found.
- SmallVector<SmallVector<LocIdx, 4>, 8> Locs;
- SmallVector<const DbgValueProperties *, 4> Properties;
- unsigned NumLocs = MTracker->getNumLocs();
// No predecessors means no PHIs.
if (BlockOrders.empty())
- return None;
+ return false;
- for (const auto *p : BlockOrders) {
- unsigned ThisBBNum = p->getNumber();
+ // All the location operands that do not already agree need to be joined,
+ // track the indices of each such location operand here.
+ SmallDenseSet<unsigned> LocOpsToJoin;
+
+ auto FirstValueIt = LiveOuts.find(BlockOrders[0]);
+ if (FirstValueIt == LiveOuts.end())
+ return false;
+ const DbgValue &FirstValue = *FirstValueIt->second;
+
+ for (const auto p : BlockOrders) {
auto OutValIt = LiveOuts.find(p);
if (OutValIt == LiveOuts.end())
// If we have a predecessor not in scope, we'll never find a PHI position.
- return None;
+ return false;
const DbgValue &OutVal = *OutValIt->second;
- if (OutVal.Kind == DbgValue::Const || OutVal.Kind == DbgValue::NoVal)
- // Consts and no-values cannot have locations we can join on.
- return None;
+ // No-values cannot have locations we can join on.
+ if (OutVal.Kind == DbgValue::NoVal)
+ return false;
+
+ // For unjoined VPHIs where we don't know the location, we definitely
+ // can't find a join loc unless the VPHI is a backedge.
+ if (OutVal.isUnjoinedPHI() && OutVal.BlockNo != MBB.getNumber())
+ return false;
+
+ if (!FirstValue.Properties.isJoinable(OutVal.Properties))
+ return false;
+
+ for (unsigned Idx = 0; Idx < FirstValue.getLocationOpCount(); ++Idx) {
+ // An unjoined PHI has no defined locations, and so a shared location must
+ // be found for every operand.
+ if (OutVal.isUnjoinedPHI()) {
+ LocOpsToJoin.insert(Idx);
+ continue;
+ }
+ DbgOpID FirstValOp = FirstValue.getDbgOpID(Idx);
+ DbgOpID OutValOp = OutVal.getDbgOpID(Idx);
+ if (FirstValOp != OutValOp) {
+ // We can never join constant ops - the ops must either both be equal
+ // constant ops or non-const ops.
+ if (FirstValOp.isConst() || OutValOp.isConst())
+ return false;
+ else
+ LocOpsToJoin.insert(Idx);
+ }
+ }
+ }
+
+ SmallVector<DbgOpID> NewDbgOps;
- Properties.push_back(&OutVal.Properties);
+ for (unsigned Idx = 0; Idx < FirstValue.getLocationOpCount(); ++Idx) {
+ // If this op doesn't need to be joined because the values agree, use that
+ // already-agreed value.
+ if (!LocOpsToJoin.contains(Idx)) {
+ NewDbgOps.push_back(FirstValue.getDbgOpID(Idx));
+ continue;
+ }
+
+ std::optional<ValueIDNum> JoinedOpLoc =
+ pickOperandPHILoc(Idx, MBB, LiveOuts, MOutLocs, BlockOrders);
+
+ if (!JoinedOpLoc)
+ return false;
+
+ NewDbgOps.push_back(DbgOpStore.insert(*JoinedOpLoc));
+ }
+
+ OutValues.append(NewDbgOps);
+ return true;
+}
+
+std::optional<ValueIDNum> InstrRefBasedLDV::pickOperandPHILoc(
+ unsigned DbgOpIdx, const MachineBasicBlock &MBB, const LiveIdxT &LiveOuts,
+ FuncValueTable &MOutLocs,
+ const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
+
+ // Collect a set of locations from predecessor where its live-out value can
+ // be found.
+ SmallVector<SmallVector<LocIdx, 4>, 8> Locs;
+ unsigned NumLocs = MTracker->getNumLocs();
+
+ for (const auto p : BlockOrders) {
+ unsigned ThisBBNum = p->getNumber();
+ auto OutValIt = LiveOuts.find(p);
+ assert(OutValIt != LiveOuts.end());
+ const DbgValue &OutVal = *OutValIt->second;
+ DbgOpID OutValOpID = OutVal.getDbgOpID(DbgOpIdx);
+ DbgOp OutValOp = DbgOpStore.find(OutValOpID);
+ assert(!OutValOp.IsConst);
// Create new empty vector of locations.
Locs.resize(Locs.size() + 1);
@@ -2388,8 +2864,8 @@ Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
// present. Do the same for VPHIs where we know the VPHI value.
if (OutVal.Kind == DbgValue::Def ||
(OutVal.Kind == DbgValue::VPHI && OutVal.BlockNo != MBB.getNumber() &&
- OutVal.ID != ValueIDNum::EmptyValue)) {
- ValueIDNum ValToLookFor = OutVal.ID;
+ !OutValOp.isUndef())) {
+ ValueIDNum ValToLookFor = OutValOp.ID;
// Search the live-outs of the predecessor for the specified value.
for (unsigned int I = 0; I < NumLocs; ++I) {
if (MOutLocs[ThisBBNum][I] == ValToLookFor)
@@ -2397,11 +2873,6 @@ Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
}
} else {
assert(OutVal.Kind == DbgValue::VPHI);
- // For VPHIs where we don't know the location, we definitely can't find
- // a join loc.
- if (OutVal.BlockNo != MBB.getNumber())
- return None;
-
// Otherwise: this is a VPHI on a backedge feeding back into itself, i.e.
// a value that's live-through the whole loop. (It has to be a backedge,
// because a block can't dominate itself). We can accept as a PHI location
@@ -2415,17 +2886,9 @@ Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
}
}
}
-
// We should have found locations for all predecessors, or returned.
assert(Locs.size() == BlockOrders.size());
- // Check that all properties are the same. We can't pick a location if they're
- // not.
- const DbgValueProperties *Properties0 = Properties[0];
- for (const auto *Prop : Properties)
- if (*Prop != *Properties0)
- return None;
-
// Starting with the first set of locations, take the intersection with
// subsequent sets.
SmallVector<LocIdx, 4> CandidateLocs = Locs[0];
@@ -2437,7 +2900,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
CandidateLocs = NewCandidates;
}
if (CandidateLocs.empty())
- return None;
+ return std::nullopt;
// We now have a set of LocIdxes that contain the right output value in
// each of the predecessors. Pick the lowest; if there's a register loc,
@@ -2516,12 +2979,12 @@ bool InstrRefBasedLDV::vlocJoin(
// Scan for variable values that can never be resolved: if they have
// different DIExpressions, different indirectness, or are mixed constants /
// non-constants.
- for (auto &V : Values) {
- if (V.second->Properties != FirstVal.Properties)
+ for (const auto &V : Values) {
+ if (!V.second->Properties.isJoinable(FirstVal.Properties))
return false;
if (V.second->Kind == DbgValue::NoVal)
return false;
- if (V.second->Kind == DbgValue::Const && FirstVal.Kind != DbgValue::Const)
+ if (!V.second->hasJoinableLocOps(FirstVal))
return false;
}
@@ -2531,6 +2994,12 @@ bool InstrRefBasedLDV::vlocJoin(
if (*V.second == FirstVal)
continue; // No disagreement.
+ // If both values are not equal but have equal non-empty IDs then they refer
+ // to the same value from different sources (e.g. one is VPHI and the other
+ // is Def), which does not cause disagreement.
+ if (V.second->hasIdenticalValidLocOps(FirstVal))
+ continue;
+
// Eliminate if a backedge feeds a VPHI back into itself.
if (V.second->Kind == DbgValue::VPHI &&
V.second->BlockNo == MBB.getNumber() &&
@@ -2675,7 +3144,7 @@ void InstrRefBasedLDV::buildVLocValueMap(
// Initialize all values to start as NoVals. This signifies "it's live
// through, but we don't know what it is".
- DbgValueProperties EmptyProperties(EmptyExpr, false);
+ DbgValueProperties EmptyProperties(EmptyExpr, false, false);
for (unsigned int I = 0; I < NumBlocks; ++I) {
DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal);
LiveIns.push_back(EmptyDbgValue);
@@ -2775,12 +3244,13 @@ void InstrRefBasedLDV::buildVLocValueMap(
// eliminated and transitions from VPHI-with-location to
// live-through-value. As a result, the selected location of any VPHI
// might change, so we need to re-compute it on each iteration.
- Optional<ValueIDNum> ValueNum =
- pickVPHILoc(*MBB, Var, LiveOutIdx, MOutLocs, Preds);
+ SmallVector<DbgOpID> JoinedOps;
- if (ValueNum) {
- InLocsChanged |= LiveIn->ID != *ValueNum;
- LiveIn->ID = *ValueNum;
+ if (pickVPHILoc(JoinedOps, *MBB, LiveOutIdx, MOutLocs, Preds)) {
+ bool NewLocPicked = !equal(LiveIn->getDbgOpIDs(), JoinedOps);
+ InLocsChanged |= NewLocPicked;
+ if (NewLocPicked)
+ LiveIn->setDbgOpIDs(JoinedOps);
}
}
@@ -2850,8 +3320,7 @@ void InstrRefBasedLDV::buildVLocValueMap(
DbgValue *BlockLiveIn = LiveInIdx[MBB];
if (BlockLiveIn->Kind == DbgValue::NoVal)
continue;
- if (BlockLiveIn->Kind == DbgValue::VPHI &&
- BlockLiveIn->ID == ValueIDNum::EmptyValue)
+ if (BlockLiveIn->isUnjoinedPHI())
continue;
if (BlockLiveIn->Kind == DbgValue::VPHI)
BlockLiveIn->Kind = DbgValue::Def;
@@ -2933,12 +3402,17 @@ void InstrRefBasedLDV::initialSetup(MachineFunction &MF) {
// Compute mappings of block <=> RPO order.
ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
unsigned int RPONumber = 0;
- for (MachineBasicBlock *MBB : RPOT) {
+ auto processMBB = [&](MachineBasicBlock *MBB) {
OrderToBB[RPONumber] = MBB;
BBToOrder[MBB] = RPONumber;
BBNumToRPO[MBB->getNumber()] = RPONumber;
++RPONumber;
- }
+ };
+ for (MachineBasicBlock *MBB : RPOT)
+ processMBB(MBB);
+ for (MachineBasicBlock &MBB : MF)
+ if (BBToOrder.find(&MBB) == BBToOrder.end())
+ processMBB(&MBB);
// Order value substitutions by their "source" operand pair, for quick lookup.
llvm::sort(MF.DebugValueSubstitutions);
@@ -3037,7 +3511,8 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
// instructions, installing transfers.
MTracker->reset();
MTracker->loadFromArray(MInLocs[BBNum], BBNum);
- TTracker->loadInlocs(MBB, MInLocs[BBNum], Output[BBNum], NumLocs);
+ TTracker->loadInlocs(MBB, MInLocs[BBNum], DbgOpStore, Output[BBNum],
+ NumLocs);
CurBB = BBNum;
CurInst = 1;
@@ -3335,6 +3810,7 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
OverlapFragments.clear();
SeenFragments.clear();
SeenDbgPHIs.clear();
+ DbgOpStore.clear();
return Changed;
}
@@ -3596,7 +4072,7 @@ public:
} // end namespace llvm
-Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
+std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
MachineFunction &MF, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
assert(MLiveOuts && MLiveIns &&
@@ -3605,17 +4081,17 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
// This function will be called twice per DBG_INSTR_REF, and might end up
// computing lots of SSA information: memoize it.
- auto SeenDbgPHIIt = SeenDbgPHIs.find(&Here);
+ auto SeenDbgPHIIt = SeenDbgPHIs.find(std::make_pair(&Here, InstrNum));
if (SeenDbgPHIIt != SeenDbgPHIs.end())
return SeenDbgPHIIt->second;
- Optional<ValueIDNum> Result =
+ std::optional<ValueIDNum> Result =
resolveDbgPHIsImpl(MF, MLiveOuts, MLiveIns, Here, InstrNum);
- SeenDbgPHIs.insert({&Here, Result});
+ SeenDbgPHIs.insert({std::make_pair(&Here, InstrNum), Result});
return Result;
}
-Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
+std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
MachineFunction &MF, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
// Pick out records of DBG_PHI instructions that have been observed. If there
@@ -3627,7 +4103,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
// No DBG_PHI means there can be no location.
if (LowerIt == UpperIt)
- return None;
+ return std::nullopt;
// If any DBG_PHIs referred to a location we didn't understand, don't try to
// compute a value. There might be scenarios where we could recover a value
@@ -3636,7 +4112,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
auto DBGPHIRange = make_range(LowerIt, UpperIt);
for (const DebugPHIRecord &DBG_PHI : DBGPHIRange)
if (!DBG_PHI.ValueRead)
- return None;
+ return std::nullopt;
// If there's only one DBG_PHI, then that is our value number.
if (std::distance(LowerIt, UpperIt) == 1)
@@ -3720,7 +4196,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
for (auto &PHIIt : PHI->IncomingValues) {
// Any undef input means DBG_PHIs didn't dominate the use point.
if (Updater.UndefMap.find(&PHIIt.first->BB) != Updater.UndefMap.end())
- return None;
+ return std::nullopt;
ValueIDNum ValueToCheck;
const ValueTable &BlockLiveOuts = MLiveOuts[PHIIt.first->BB.getNumber()];
@@ -3739,7 +4215,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
}
if (BlockLiveOuts[Loc.asU64()] != ValueToCheck)
- return None;
+ return std::nullopt;
}
// Record this value as validated.
diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h
index 70aae47c8bdc..2fdc37c6dda2 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h
+++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h
@@ -19,6 +19,7 @@
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
+#include <optional>
#include "LiveDebugValues.h"
@@ -30,6 +31,7 @@ class InstrRefLDVTest;
namespace LiveDebugValues {
class MLocTracker;
+class DbgOpIDMap;
using namespace llvm;
@@ -168,6 +170,40 @@ public:
static ValueIDNum TombstoneValue;
};
+} // End namespace LiveDebugValues
+
+namespace llvm {
+using namespace LiveDebugValues;
+
+template <> struct DenseMapInfo<LocIdx> {
+ static inline LocIdx getEmptyKey() { return LocIdx::MakeIllegalLoc(); }
+ static inline LocIdx getTombstoneKey() { return LocIdx::MakeTombstoneLoc(); }
+
+ static unsigned getHashValue(const LocIdx &Loc) { return Loc.asU64(); }
+
+ static bool isEqual(const LocIdx &A, const LocIdx &B) { return A == B; }
+};
+
+template <> struct DenseMapInfo<ValueIDNum> {
+ static inline ValueIDNum getEmptyKey() { return ValueIDNum::EmptyValue; }
+ static inline ValueIDNum getTombstoneKey() {
+ return ValueIDNum::TombstoneValue;
+ }
+
+ static unsigned getHashValue(const ValueIDNum &Val) {
+ return hash_value(Val.asU64());
+ }
+
+ static bool isEqual(const ValueIDNum &A, const ValueIDNum &B) {
+ return A == B;
+ }
+};
+
+} // end namespace llvm
+
+namespace LiveDebugValues {
+using namespace llvm;
+
/// Type for a table of values in a block.
using ValueTable = std::unique_ptr<ValueIDNum[]>;
@@ -199,41 +235,219 @@ public:
/// the value, and Boolean of whether or not it's indirect.
class DbgValueProperties {
public:
- DbgValueProperties(const DIExpression *DIExpr, bool Indirect)
- : DIExpr(DIExpr), Indirect(Indirect) {}
+ DbgValueProperties(const DIExpression *DIExpr, bool Indirect, bool IsVariadic)
+ : DIExpr(DIExpr), Indirect(Indirect), IsVariadic(IsVariadic) {}
/// Extract properties from an existing DBG_VALUE instruction.
DbgValueProperties(const MachineInstr &MI) {
assert(MI.isDebugValue());
+ assert(MI.getDebugExpression()->getNumLocationOperands() == 0 ||
+ MI.isDebugValueList() || MI.isUndefDebugValue());
+ IsVariadic = MI.isDebugValueList();
DIExpr = MI.getDebugExpression();
- Indirect = MI.getOperand(1).isImm();
+ Indirect = MI.isDebugOffsetImm();
+ }
+
+ bool isJoinable(const DbgValueProperties &Other) const {
+ return DIExpression::isEqualExpression(DIExpr, Indirect, Other.DIExpr,
+ Other.Indirect);
}
bool operator==(const DbgValueProperties &Other) const {
- return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect);
+ return std::tie(DIExpr, Indirect, IsVariadic) ==
+ std::tie(Other.DIExpr, Other.Indirect, Other.IsVariadic);
}
bool operator!=(const DbgValueProperties &Other) const {
return !(*this == Other);
}
+ unsigned getLocationOpCount() const {
+ return IsVariadic ? DIExpr->getNumLocationOperands() : 1;
+ }
+
const DIExpression *DIExpr;
bool Indirect;
+ bool IsVariadic;
+};
+
+/// TODO: Might pack better if we changed this to a Struct of Arrays, since
+/// MachineOperand is width 32, making this struct width 33. We could also
+/// potentially avoid storing the whole MachineOperand (sizeof=32), instead
+/// choosing to store just the contents portion (sizeof=8) and a Kind enum,
+/// since we already know it is some type of immediate value.
+/// Stores a single debug operand, which can either be a MachineOperand for
+/// directly storing immediate values, or a ValueIDNum representing some value
+/// computed at some point in the program. IsConst is used as a discriminator.
+struct DbgOp {
+ union {
+ ValueIDNum ID;
+ MachineOperand MO;
+ };
+ bool IsConst;
+
+ DbgOp() : ID(ValueIDNum::EmptyValue), IsConst(false) {}
+ DbgOp(ValueIDNum ID) : ID(ID), IsConst(false) {}
+ DbgOp(MachineOperand MO) : MO(MO), IsConst(true) {}
+
+ bool isUndef() const { return !IsConst && ID == ValueIDNum::EmptyValue; }
+
+#ifndef NDEBUG
+ void dump(const MLocTracker *MTrack) const;
+#endif
+};
+
+/// A DbgOp whose ID (if any) has resolved to an actual location, LocIdx. Used
+/// when working with concrete debug values, i.e. when joining MLocs and VLocs
+/// in the TransferTracker or emitting DBG_VALUE/DBG_VALUE_LIST instructions in
+/// the MLocTracker.
+struct ResolvedDbgOp {
+ union {
+ LocIdx Loc;
+ MachineOperand MO;
+ };
+ bool IsConst;
+
+ ResolvedDbgOp(LocIdx Loc) : Loc(Loc), IsConst(false) {}
+ ResolvedDbgOp(MachineOperand MO) : MO(MO), IsConst(true) {}
+
+ bool operator==(const ResolvedDbgOp &Other) const {
+ if (IsConst != Other.IsConst)
+ return false;
+ if (IsConst)
+ return MO.isIdenticalTo(Other.MO);
+ return Loc == Other.Loc;
+ }
+
+#ifndef NDEBUG
+ void dump(const MLocTracker *MTrack) const;
+#endif
};
-/// Class recording the (high level) _value_ of a variable. Identifies either
-/// the value of the variable as a ValueIDNum, or a constant MachineOperand.
+/// An ID used in the DbgOpIDMap (below) to lookup a stored DbgOp. This is used
+/// in place of actual DbgOps inside of a DbgValue to reduce its size, as
+/// DbgValue is very frequently used and passed around, and the actual DbgOp is
+/// over 8x larger than this class, due to storing a MachineOperand. This ID
+/// should be equal for all equal DbgOps, and also encodes whether the mapped
+/// DbgOp is a constant, meaning that for simple equality or const-ness checks
+/// it is not necessary to lookup this ID.
+struct DbgOpID {
+ struct IsConstIndexPair {
+ uint32_t IsConst : 1;
+ uint32_t Index : 31;
+ };
+
+ union {
+ struct IsConstIndexPair ID;
+ uint32_t RawID;
+ };
+
+ DbgOpID() : RawID(UndefID.RawID) {
+ static_assert(sizeof(DbgOpID) == 4, "DbgOpID should fit within 4 bytes.");
+ }
+ DbgOpID(uint32_t RawID) : RawID(RawID) {}
+ DbgOpID(bool IsConst, uint32_t Index) : ID({IsConst, Index}) {}
+
+ static DbgOpID UndefID;
+
+ bool operator==(const DbgOpID &Other) const { return RawID == Other.RawID; }
+ bool operator!=(const DbgOpID &Other) const { return !(*this == Other); }
+
+ uint32_t asU32() const { return RawID; }
+
+ bool isUndef() const { return *this == UndefID; }
+ bool isConst() const { return ID.IsConst && !isUndef(); }
+ uint32_t getIndex() const { return ID.Index; }
+
+#ifndef NDEBUG
+ void dump(const MLocTracker *MTrack, const DbgOpIDMap *OpStore) const;
+#endif
+};
+
+/// Class storing the complete set of values that are observed by DbgValues
+/// within the current function. Allows 2-way lookup, with `find` returning the
+/// Op for a given ID and `insert` returning the ID for a given Op (creating one
+/// if none exists).
+class DbgOpIDMap {
+
+ SmallVector<ValueIDNum, 0> ValueOps;
+ SmallVector<MachineOperand, 0> ConstOps;
+
+ DenseMap<ValueIDNum, DbgOpID> ValueOpToID;
+ DenseMap<MachineOperand, DbgOpID> ConstOpToID;
+
+public:
+ /// If \p Op does not already exist in this map, it is inserted and the
+ /// corresponding DbgOpID is returned. If Op already exists in this map, then
+ /// no change is made and the existing ID for Op is returned.
+ /// Calling this with the undef DbgOp will always return DbgOpID::UndefID.
+ DbgOpID insert(DbgOp Op) {
+ if (Op.isUndef())
+ return DbgOpID::UndefID;
+ if (Op.IsConst)
+ return insertConstOp(Op.MO);
+ return insertValueOp(Op.ID);
+ }
+ /// Returns the DbgOp associated with \p ID. Should only be used for IDs
+ /// returned from calling `insert` from this map or DbgOpID::UndefID.
+ DbgOp find(DbgOpID ID) const {
+ if (ID == DbgOpID::UndefID)
+ return DbgOp();
+ if (ID.isConst())
+ return DbgOp(ConstOps[ID.getIndex()]);
+ return DbgOp(ValueOps[ID.getIndex()]);
+ }
+
+ void clear() {
+ ValueOps.clear();
+ ConstOps.clear();
+ ValueOpToID.clear();
+ ConstOpToID.clear();
+ }
+
+private:
+ DbgOpID insertConstOp(MachineOperand &MO) {
+ auto ExistingIt = ConstOpToID.find(MO);
+ if (ExistingIt != ConstOpToID.end())
+ return ExistingIt->second;
+ DbgOpID ID(true, ConstOps.size());
+ ConstOpToID.insert(std::make_pair(MO, ID));
+ ConstOps.push_back(MO);
+ return ID;
+ }
+ DbgOpID insertValueOp(ValueIDNum VID) {
+ auto ExistingIt = ValueOpToID.find(VID);
+ if (ExistingIt != ValueOpToID.end())
+ return ExistingIt->second;
+ DbgOpID ID(false, ValueOps.size());
+ ValueOpToID.insert(std::make_pair(VID, ID));
+ ValueOps.push_back(VID);
+ return ID;
+ }
+};
+
+// We set the maximum number of operands that we will handle to keep DbgValue
+// within a reasonable size (64 bytes), as we store and pass a lot of them
+// around.
+#define MAX_DBG_OPS 8
+
+/// Class recording the (high level) _value_ of a variable. Identifies the value
+/// of the variable as a list of ValueIDNums and constant MachineOperands, or as
+/// an empty list for undef debug values or VPHI values which we have not found
+/// valid locations for.
/// This class also stores meta-information about how the value is qualified.
/// Used to reason about variable values when performing the second
/// (DebugVariable specific) dataflow analysis.
class DbgValue {
+private:
+ /// If Kind is Def or VPHI, the set of IDs corresponding to the DbgOps that
+ /// are used. VPHIs set every ID to EmptyID when we have not found a valid
+ /// machine-value for every operand, and sets them to the corresponding
+ /// machine-values when we have found all of them.
+ DbgOpID DbgOps[MAX_DBG_OPS];
+ unsigned OpCount;
+
public:
- /// If Kind is Def, the value number that this value is based on. VPHIs set
- /// this field to EmptyValue if there is no machine-value for this VPHI, or
- /// the corresponding machine-value if there is one.
- ValueIDNum ID;
- /// If Kind is Const, the MachineOperand defining this value.
- Optional<MachineOperand> MO;
/// For a NoVal or VPHI DbgValue, which block it was generated in.
int BlockNo;
@@ -242,8 +456,8 @@ public:
typedef enum {
Undef, // Represents a DBG_VALUE $noreg in the transfer function only.
- Def, // This value is defined by an inst, or is a PHI value.
- Const, // A constant value contained in the MachineOperand field.
+ Def, // This value is defined by some combination of constants,
+ // instructions, or PHI values.
VPHI, // Incoming values to BlockNo differ, those values must be joined by
// a PHI in this block.
NoVal, // Empty DbgValue indicating an unknown value. Used as initializer,
@@ -252,52 +466,113 @@ public:
/// Discriminator for whether this is a constant or an in-program value.
KindT Kind;
- DbgValue(const ValueIDNum &Val, const DbgValueProperties &Prop, KindT Kind)
- : ID(Val), MO(None), BlockNo(0), Properties(Prop), Kind(Kind) {
- assert(Kind == Def);
+ DbgValue(ArrayRef<DbgOpID> DbgOps, const DbgValueProperties &Prop)
+ : OpCount(DbgOps.size()), BlockNo(0), Properties(Prop), Kind(Def) {
+ static_assert(sizeof(DbgValue) <= 64,
+ "DbgValue should fit within 64 bytes.");
+ assert(DbgOps.size() == Prop.getLocationOpCount());
+ if (DbgOps.size() > MAX_DBG_OPS ||
+ any_of(DbgOps, [](DbgOpID ID) { return ID.isUndef(); })) {
+ Kind = Undef;
+ OpCount = 0;
+#define DEBUG_TYPE "LiveDebugValues"
+ if (DbgOps.size() > MAX_DBG_OPS) {
+ LLVM_DEBUG(dbgs() << "Found DbgValue with more than maximum allowed "
+ "operands.\n");
+ }
+#undef DEBUG_TYPE
+ } else {
+ for (unsigned Idx = 0; Idx < DbgOps.size(); ++Idx)
+ this->DbgOps[Idx] = DbgOps[Idx];
+ }
}
DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind)
- : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(BlockNo),
- Properties(Prop), Kind(Kind) {
+ : OpCount(0), BlockNo(BlockNo), Properties(Prop), Kind(Kind) {
assert(Kind == NoVal || Kind == VPHI);
}
- DbgValue(const MachineOperand &MO, const DbgValueProperties &Prop, KindT Kind)
- : ID(ValueIDNum::EmptyValue), MO(MO), BlockNo(0), Properties(Prop),
- Kind(Kind) {
- assert(Kind == Const);
- }
-
DbgValue(const DbgValueProperties &Prop, KindT Kind)
- : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(0), Properties(Prop),
- Kind(Kind) {
+ : OpCount(0), BlockNo(0), Properties(Prop), Kind(Kind) {
assert(Kind == Undef &&
"Empty DbgValue constructor must pass in Undef kind");
}
#ifndef NDEBUG
- void dump(const MLocTracker *MTrack) const;
+ void dump(const MLocTracker *MTrack = nullptr,
+ const DbgOpIDMap *OpStore = nullptr) const;
#endif
bool operator==(const DbgValue &Other) const {
if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties))
return false;
- else if (Kind == Def && ID != Other.ID)
+ else if (Kind == Def && !equal(getDbgOpIDs(), Other.getDbgOpIDs()))
return false;
else if (Kind == NoVal && BlockNo != Other.BlockNo)
return false;
- else if (Kind == Const)
- return MO->isIdenticalTo(*Other.MO);
else if (Kind == VPHI && BlockNo != Other.BlockNo)
return false;
- else if (Kind == VPHI && ID != Other.ID)
+ else if (Kind == VPHI && !equal(getDbgOpIDs(), Other.getDbgOpIDs()))
return false;
return true;
}
bool operator!=(const DbgValue &Other) const { return !(*this == Other); }
+
+ // Returns an array of all the machine values used to calculate this variable
+ // value, or an empty list for an Undef or unjoined VPHI.
+ ArrayRef<DbgOpID> getDbgOpIDs() const { return {DbgOps, OpCount}; }
+
+ // Returns either DbgOps[Index] if this DbgValue has Debug Operands, or
+ // the ID for ValueIDNum::EmptyValue otherwise (i.e. if this is an Undef,
+ // NoVal, or an unjoined VPHI).
+ DbgOpID getDbgOpID(unsigned Index) const {
+ if (!OpCount)
+ return DbgOpID::UndefID;
+ assert(Index < OpCount);
+ return DbgOps[Index];
+ }
+ // Replaces this DbgValue's existing DbgOpIDs (if any) with the contents of
+ // \p NewIDs. The number of DbgOpIDs passed must be equal to the number of
+ // arguments expected by this DbgValue's properties (the return value of
+ // `getLocationOpCount()`).
+ void setDbgOpIDs(ArrayRef<DbgOpID> NewIDs) {
+ // We can go from no ops to some ops, but not from some ops to no ops.
+ assert(NewIDs.size() == getLocationOpCount() &&
+ "Incorrect number of Debug Operands for this DbgValue.");
+ OpCount = NewIDs.size();
+ for (unsigned Idx = 0; Idx < NewIDs.size(); ++Idx)
+ DbgOps[Idx] = NewIDs[Idx];
+ }
+
+ // The number of debug operands expected by this DbgValue's expression.
+ // getDbgOpIDs() should return an array of this length, unless this is an
+ // Undef or an unjoined VPHI.
+ unsigned getLocationOpCount() const {
+ return Properties.getLocationOpCount();
+ }
+
+ // Returns true if this or Other are unjoined PHIs, which do not have defined
+ // Loc Ops, or if the `n`th Loc Op for this has a different constness to the
+ // `n`th Loc Op for Other.
+ bool hasJoinableLocOps(const DbgValue &Other) const {
+ if (isUnjoinedPHI() || Other.isUnjoinedPHI())
+ return true;
+ for (unsigned Idx = 0; Idx < getLocationOpCount(); ++Idx) {
+ if (getDbgOpID(Idx).isConst() != Other.getDbgOpID(Idx).isConst())
+ return false;
+ }
+ return true;
+ }
+
+ bool isUnjoinedPHI() const { return Kind == VPHI && OpCount == 0; }
+
+ bool hasIdenticalValidLocOps(const DbgValue &Other) const {
+ if (!OpCount)
+ return false;
+ return equal(getDbgOpIDs(), Other.getDbgOpIDs());
+ }
};
class LocIdxToIndexFunctor {
@@ -620,9 +895,9 @@ public:
void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID);
/// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked.
- /// Returns None when in scenarios where a spill slot could be tracked, but
- /// we would likely run into resource limitations.
- Optional<SpillLocationNo> getOrTrackSpillLoc(SpillLoc L);
+ /// Returns std::nullopt when in scenarios where a spill slot could be
+ /// tracked, but we would likely run into resource limitations.
+ std::optional<SpillLocationNo> getOrTrackSpillLoc(SpillLoc L);
// Get LocIdx of a spill ID.
LocIdx getSpillMLoc(unsigned SpillID) {
@@ -667,10 +942,11 @@ public:
LLVM_DUMP_METHOD void dump_mloc_map();
#endif
- /// Create a DBG_VALUE based on machine location \p MLoc. Qualify it with the
+ /// Create a DBG_VALUE based on debug operands \p DbgOps. Qualify it with the
/// information in \pProperties, for variable Var. Don't insert it anywhere,
/// just return the builder for it.
- MachineInstrBuilder emitLoc(Optional<LocIdx> MLoc, const DebugVariable &Var,
+ MachineInstrBuilder emitLoc(const SmallVectorImpl<ResolvedDbgOp> &DbgOps,
+ const DebugVariable &Var,
const DbgValueProperties &Properties);
};
@@ -704,32 +980,16 @@ public:
public:
VLocTracker(const OverlapMap &O, const DIExpression *EmptyExpr)
- : OverlappingFragments(O), EmptyProperties(EmptyExpr, false) {}
+ : OverlappingFragments(O), EmptyProperties(EmptyExpr, false, false) {}
void defVar(const MachineInstr &MI, const DbgValueProperties &Properties,
- Optional<ValueIDNum> ID) {
- assert(MI.isDebugValue() || MI.isDebugRef());
+ const SmallVectorImpl<DbgOpID> &DebugOps) {
+ assert(MI.isDebugValueLike());
DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
MI.getDebugLoc()->getInlinedAt());
- DbgValue Rec = (ID) ? DbgValue(*ID, Properties, DbgValue::Def)
- : DbgValue(Properties, DbgValue::Undef);
-
- // Attempt insertion; overwrite if it's already mapped.
- auto Result = Vars.insert(std::make_pair(Var, Rec));
- if (!Result.second)
- Result.first->second = Rec;
- Scopes[Var] = MI.getDebugLoc().get();
-
- considerOverlaps(Var, MI.getDebugLoc().get());
- }
-
- void defVar(const MachineInstr &MI, const MachineOperand &MO) {
- // Only DBG_VALUEs can define constant-valued variables.
- assert(MI.isDebugValue());
- DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
- MI.getDebugLoc()->getInlinedAt());
- DbgValueProperties Properties(MI);
- DbgValue Rec = DbgValue(MO, Properties, DbgValue::Const);
+ DbgValue Rec = (DebugOps.size() > 0)
+ ? DbgValue(DebugOps, Properties)
+ : DbgValue(Properties, DbgValue::Undef);
// Attempt insertion; overwrite if it's already mapped.
auto Result = Vars.insert(std::make_pair(Var, Rec));
@@ -751,9 +1011,9 @@ public:
// The "empty" fragment is stored as DebugVariable::DefaultFragment, so
// that it overlaps with everything, however its cannonical representation
// in a DebugVariable is as "None".
- Optional<DIExpression::FragmentInfo> OptFragmentInfo = FragmentInfo;
+ std::optional<DIExpression::FragmentInfo> OptFragmentInfo = FragmentInfo;
if (DebugVariable::isDefaultFragment(FragmentInfo))
- OptFragmentInfo = None;
+ OptFragmentInfo = std::nullopt;
DebugVariable Overlapped(Var.getVariable(), OptFragmentInfo,
Var.getInlinedAt());
@@ -779,7 +1039,7 @@ public:
friend class ::InstrRefLDVTest;
using FragmentInfo = DIExpression::FragmentInfo;
- using OptFragmentInfo = Optional<DIExpression::FragmentInfo>;
+ using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>;
// Helper while building OverlapMap, a map of all fragments seen for a given
// DILocalVariable.
@@ -872,12 +1132,12 @@ private:
uint64_t InstrNum;
/// Block where DBG_PHI occurred.
MachineBasicBlock *MBB;
- /// The value number read by the DBG_PHI -- or None if it didn't refer to
- /// a value.
- Optional<ValueIDNum> ValueRead;
- /// Register/Stack location the DBG_PHI reads -- or None if it referred to
- /// something unexpected.
- Optional<LocIdx> ReadLoc;
+ /// The value number read by the DBG_PHI -- or std::nullopt if it didn't
+ /// refer to a value.
+ std::optional<ValueIDNum> ValueRead;
+ /// Register/Stack location the DBG_PHI reads -- or std::nullopt if it
+ /// referred to something unexpected.
+ std::optional<LocIdx> ReadLoc;
operator unsigned() const { return InstrNum; }
};
@@ -896,7 +1156,10 @@ private:
/// DBG_INSTR_REFs that call resolveDbgPHIs. These variable references solve
/// a mini SSA problem caused by DBG_PHIs being cloned, this collection caches
/// the result.
- DenseMap<MachineInstr *, Optional<ValueIDNum>> SeenDbgPHIs;
+ DenseMap<std::pair<MachineInstr *, unsigned>, std::optional<ValueIDNum>>
+ SeenDbgPHIs;
+
+ DbgOpIDMap DbgOpStore;
/// True if we need to examine call instructions for stack clobbers. We
/// normally assume that they don't clobber SP, but stack probes on Windows
@@ -909,8 +1172,8 @@ private:
StringRef StackProbeSymbolName;
/// Tests whether this instruction is a spill to a stack slot.
- Optional<SpillLocationNo> isSpillInstruction(const MachineInstr &MI,
- MachineFunction *MF);
+ std::optional<SpillLocationNo> isSpillInstruction(const MachineInstr &MI,
+ MachineFunction *MF);
/// Decide if @MI is a spill instruction and return true if it is. We use 2
/// criteria to make this decision:
@@ -923,14 +1186,23 @@ private:
/// If a given instruction is identified as a spill, return the spill slot
/// and set \p Reg to the spilled register.
- Optional<SpillLocationNo> isRestoreInstruction(const MachineInstr &MI,
- MachineFunction *MF, unsigned &Reg);
+ std::optional<SpillLocationNo> isRestoreInstruction(const MachineInstr &MI,
+ MachineFunction *MF,
+ unsigned &Reg);
/// Given a spill instruction, extract the spill slot information, ensure it's
/// tracked, and return the spill number.
- Optional<SpillLocationNo>
+ std::optional<SpillLocationNo>
extractSpillBaseRegAndOffset(const MachineInstr &MI);
+ /// For an instruction reference given by \p InstNo and \p OpNo in instruction
+ /// \p MI returns the Value pointed to by that instruction reference if any
+ /// exists, otherwise returns None.
+ std::optional<ValueIDNum> getValueForInstrRef(unsigned InstNo, unsigned OpNo,
+ MachineInstr &MI,
+ const ValueTable *MLiveOuts,
+ const ValueTable *MLiveIns);
+
/// Observe a single instruction while stepping through a block.
void process(MachineInstr &MI, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns);
@@ -972,17 +1244,18 @@ private:
/// forming another mini-ssa problem to solve.
/// \p Here the position of a DBG_INSTR_REF seeking a machine value number
/// \p InstrNum Debug instruction number defined by DBG_PHI instructions.
- /// \returns The machine value number at position Here, or None.
- Optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF,
- const ValueTable *MLiveOuts,
- const ValueTable *MLiveIns,
- MachineInstr &Here, uint64_t InstrNum);
-
- Optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF,
- const ValueTable *MLiveOuts,
- const ValueTable *MLiveIns,
- MachineInstr &Here,
- uint64_t InstrNum);
+ /// \returns The machine value number at position Here, or std::nullopt.
+ std::optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF,
+ const ValueTable *MLiveOuts,
+ const ValueTable *MLiveIns,
+ MachineInstr &Here,
+ uint64_t InstrNum);
+
+ std::optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF,
+ const ValueTable *MLiveOuts,
+ const ValueTable *MLiveIns,
+ MachineInstr &Here,
+ uint64_t InstrNum);
/// Step through the function, recording register definitions and movements
/// in an MLocTracker. Convert the observations into a per-block transfer
@@ -1086,14 +1359,21 @@ private:
SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore,
DbgValue &LiveIn);
- /// For the given block and live-outs feeding into it, try to find a
- /// machine location where all the variable values join together.
- /// \returns Value ID of a machine PHI if an appropriate one is available.
- Optional<ValueIDNum>
- pickVPHILoc(const MachineBasicBlock &MBB, const DebugVariable &Var,
+ /// For the given block and live-outs feeding into it, try to find
+ /// machine locations for each debug operand where all the values feeding
+ /// into that operand join together.
+ /// \returns true if a joined location was found for every value that needed
+ /// to be joined.
+ bool
+ pickVPHILoc(SmallVectorImpl<DbgOpID> &OutValues, const MachineBasicBlock &MBB,
const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs,
const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders);
+ std::optional<ValueIDNum> pickOperandPHILoc(
+ unsigned DbgOpIdx, const MachineBasicBlock &MBB, const LiveIdxT &LiveOuts,
+ FuncValueTable &MOutLocs,
+ const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders);
+
/// Take collections of DBG_VALUE instructions stored in TTracker, and
/// install them into their output blocks. Preserves a stable order of
/// DBG_VALUEs produced (which would otherwise cause nondeterminism) through
@@ -1138,6 +1418,7 @@ public:
void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const;
bool isCalleeSaved(LocIdx L) const;
+ bool isCalleeSavedReg(Register R) const;
bool hasFoldedStackStore(const MachineInstr &MI) {
// Instruction must have a memory operand that's a stack slot, and isn't
@@ -1152,38 +1433,9 @@ public:
&& !MemOperand->getPseudoValue()->isAliased(MFI);
}
- Optional<LocIdx> findLocationForMemOperand(const MachineInstr &MI);
+ std::optional<LocIdx> findLocationForMemOperand(const MachineInstr &MI);
};
} // namespace LiveDebugValues
-namespace llvm {
-using namespace LiveDebugValues;
-
-template <> struct DenseMapInfo<LocIdx> {
- static inline LocIdx getEmptyKey() { return LocIdx::MakeIllegalLoc(); }
- static inline LocIdx getTombstoneKey() { return LocIdx::MakeTombstoneLoc(); }
-
- static unsigned getHashValue(const LocIdx &Loc) { return Loc.asU64(); }
-
- static bool isEqual(const LocIdx &A, const LocIdx &B) { return A == B; }
-};
-
-template <> struct DenseMapInfo<ValueIDNum> {
- static inline ValueIDNum getEmptyKey() { return ValueIDNum::EmptyValue; }
- static inline ValueIDNum getTombstoneKey() {
- return ValueIDNum::TombstoneValue;
- }
-
- static unsigned getHashValue(const ValueIDNum &Val) {
- return hash_value(Val.asU64());
- }
-
- static bool isEqual(const ValueIDNum &A, const ValueIDNum &B) {
- return A == B;
- }
-};
-
-} // end namespace llvm
-
#endif /* LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H */
diff --git a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
index 141008ac2296..9dba9a88f703 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
@@ -18,6 +18,7 @@
#include "llvm/Pass.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Target/TargetMachine.h"
/// \file LiveDebugValues.cpp
///
@@ -72,11 +73,6 @@ public:
/// Calculate the liveness information for the given machine function.
bool runOnMachineFunction(MachineFunction &MF) override;
- MachineFunctionProperties getRequiredProperties() const override {
- return MachineFunctionProperties().set(
- MachineFunctionProperties::Property::NoVRegs);
- }
-
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
@@ -106,6 +102,14 @@ LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
}
bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
+ // Except for Wasm, all targets should be only using physical register at this
+ // point. Wasm only use virtual registers throught its pipeline, but its
+ // virtual registers don't participate in this LiveDebugValues analysis; only
+ // its target indices do.
+ assert(MF.getTarget().getTargetTriple().isWasm() ||
+ MF.getProperties().hasProperty(
+ MachineFunctionProperties::Property::NoVRegs));
+
bool InstrRefBased = MF.useDebugInstrRef();
// Allow the user to force selection of InstrRef LDV.
InstrRefBased |= ForceInstrRefLDV;
diff --git a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
index 32e07eb77efe..b78757b855f4 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
@@ -10,12 +10,13 @@
///
/// LiveDebugValues is an optimistic "available expressions" dataflow
/// algorithm. The set of expressions is the set of machine locations
-/// (registers, spill slots, constants) that a variable fragment might be
-/// located, qualified by a DIExpression and indirect-ness flag, while each
-/// variable is identified by a DebugVariable object. The availability of an
-/// expression begins when a DBG_VALUE instruction specifies the location of a
-/// DebugVariable, and continues until that location is clobbered or
-/// re-specified by a different DBG_VALUE for the same DebugVariable.
+/// (registers, spill slots, constants, and target indices) that a variable
+/// fragment might be located, qualified by a DIExpression and indirect-ness
+/// flag, while each variable is identified by a DebugVariable object. The
+/// availability of an expression begins when a DBG_VALUE instruction specifies
+/// the location of a DebugVariable, and continues until that location is
+/// clobbered or re-specified by a different DBG_VALUE for the same
+/// DebugVariable.
///
/// The output of LiveDebugValues is additional DBG_VALUE instructions,
/// placed to extend variable locations as far they're available. This file
@@ -148,6 +149,7 @@
#include <cstdint>
#include <functional>
#include <map>
+#include <optional>
#include <queue>
#include <tuple>
#include <utility>
@@ -229,6 +231,14 @@ struct LocIndex {
static constexpr u32_location_t kEntryValueBackupLocation =
kFirstInvalidRegLocation + 1;
+ /// A special location reserved for VarLocs with locations of kind
+ /// WasmLocKind.
+ /// TODO Placing all Wasm target index locations in this single kWasmLocation
+ /// may cause slowdown in compilation time in very large functions. Consider
+ /// giving a each target index/offset pair its own u32_location_t if this
+ /// becomes a problem.
+ static constexpr u32_location_t kWasmLocation = kFirstInvalidRegLocation + 2;
+
LocIndex(u32_location_t Location, u32_index_t Index)
: Location(Location), Index(Index) {}
@@ -237,8 +247,7 @@ struct LocIndex {
}
template<typename IntT> static LocIndex fromRawInteger(IntT ID) {
- static_assert(std::is_unsigned<IntT>::value &&
- sizeof(ID) == sizeof(uint64_t),
+ static_assert(std::is_unsigned_v<IntT> && sizeof(ID) == sizeof(uint64_t),
"Cannot convert raw integer to LocIndex");
return {static_cast<u32_location_t>(ID >> 32),
static_cast<u32_index_t>(ID)};
@@ -282,7 +291,7 @@ private:
enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore };
using FragmentInfo = DIExpression::FragmentInfo;
- using OptFragmentInfo = Optional<DIExpression::FragmentInfo>;
+ using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>;
/// A pair of debug variable and value location.
struct VarLoc {
@@ -299,6 +308,21 @@ private:
}
};
+ // Target indices used for wasm-specific locations.
+ struct WasmLoc {
+ // One of TargetIndex values defined in WebAssembly.h. We deal with
+ // local-related TargetIndex in this analysis (TI_LOCAL and
+ // TI_LOCAL_INDIRECT). Stack operands (TI_OPERAND_STACK) will be handled
+ // separately WebAssemblyDebugFixup pass, and we don't associate debug
+ // info with values in global operands (TI_GLOBAL_RELOC) at the moment.
+ int Index;
+ int64_t Offset;
+ bool operator==(const WasmLoc &Other) const {
+ return Index == Other.Index && Offset == Other.Offset;
+ }
+ bool operator!=(const WasmLoc &Other) const { return !(*this == Other); }
+ };
+
/// Identity of the variable at this location.
const DebugVariable Var;
@@ -313,7 +337,8 @@ private:
InvalidKind = 0,
RegisterKind,
SpillLocKind,
- ImmediateKind
+ ImmediateKind,
+ WasmLocKind
};
enum class EntryValueLocKind {
@@ -332,6 +357,7 @@ private:
int64_t Immediate;
const ConstantFP *FPImm;
const ConstantInt *CImm;
+ WasmLoc WasmLocation;
MachineLocValue() : Hash(0) {}
};
@@ -348,6 +374,8 @@ private:
switch (Kind) {
case MachineLocKind::SpillLocKind:
return Value.SpillLocation == Other.Value.SpillLocation;
+ case MachineLocKind::WasmLocKind:
+ return Value.WasmLocation == Other.Value.WasmLocation;
case MachineLocKind::RegisterKind:
case MachineLocKind::ImmediateKind:
return Value.Hash == Other.Value.Hash;
@@ -366,6 +394,11 @@ private:
Other.Kind, Other.Value.SpillLocation.SpillBase,
Other.Value.SpillLocation.SpillOffset.getFixed(),
Other.Value.SpillLocation.SpillOffset.getScalable());
+ case MachineLocKind::WasmLocKind:
+ return std::make_tuple(Kind, Value.WasmLocation.Index,
+ Value.WasmLocation.Offset) <
+ std::make_tuple(Other.Kind, Other.Value.WasmLocation.Index,
+ Other.Value.WasmLocation.Offset);
case MachineLocKind::RegisterKind:
case MachineLocKind::ImmediateKind:
return std::tie(Kind, Value.Hash) <
@@ -386,7 +419,7 @@ private:
/// emitting a debug value.
SmallVector<unsigned, 8> OrigLocMap;
- VarLoc(const MachineInstr &MI, LexicalScopes &LS)
+ VarLoc(const MachineInstr &MI)
: Var(MI.getDebugVariable(), MI.getDebugExpression(),
MI.getDebugLoc()->getInlinedAt()),
Expr(MI.getDebugExpression()), MI(MI) {
@@ -429,6 +462,9 @@ private:
} else if (Op.isCImm()) {
Kind = MachineLocKind::ImmediateKind;
Loc.CImm = Op.getCImm();
+ } else if (Op.isTargetIndex()) {
+ Kind = MachineLocKind::WasmLocKind;
+ Loc.WasmLocation = {Op.getIndex(), Op.getOffset()};
} else
llvm_unreachable("Invalid Op kind for MachineLoc.");
return {Kind, Loc};
@@ -436,9 +472,9 @@ private:
/// Take the variable and machine-location in DBG_VALUE MI, and build an
/// entry location using the given expression.
- static VarLoc CreateEntryLoc(const MachineInstr &MI, LexicalScopes &LS,
+ static VarLoc CreateEntryLoc(const MachineInstr &MI,
const DIExpression *EntryExpr, Register Reg) {
- VarLoc VL(MI, LS);
+ VarLoc VL(MI);
assert(VL.Locs.size() == 1 &&
VL.Locs[0].Kind == MachineLocKind::RegisterKind);
VL.EVKind = EntryValueLocKind::EntryValueKind;
@@ -452,9 +488,8 @@ private:
/// location will turn into the normal location if the backup is valid at
/// the time of the primary location clobbering.
static VarLoc CreateEntryBackupLoc(const MachineInstr &MI,
- LexicalScopes &LS,
const DIExpression *EntryExpr) {
- VarLoc VL(MI, LS);
+ VarLoc VL(MI);
assert(VL.Locs.size() == 1 &&
VL.Locs[0].Kind == MachineLocKind::RegisterKind);
VL.EVKind = EntryValueLocKind::EntryValueBackupKind;
@@ -466,10 +501,9 @@ private:
/// function entry), and build a copy of an entry value backup location by
/// setting the register location to NewReg.
static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI,
- LexicalScopes &LS,
const DIExpression *EntryExpr,
Register NewReg) {
- VarLoc VL(MI, LS);
+ VarLoc VL(MI);
assert(VL.Locs.size() == 1 &&
VL.Locs[0].Kind == MachineLocKind::RegisterKind);
VL.EVKind = EntryValueLocKind::EntryValueCopyBackupKind;
@@ -564,6 +598,10 @@ private:
MOs.push_back(Orig);
break;
}
+ case MachineLocKind::WasmLocKind: {
+ MOs.push_back(Orig);
+ break;
+ }
case MachineLocKind::InvalidKind:
llvm_unreachable("Tried to produce DBG_VALUE for invalid VarLoc");
}
@@ -649,6 +687,21 @@ private:
llvm_unreachable("Could not find given SpillLoc in Locs");
}
+ bool containsWasmLocs() const {
+ return any_of(Locs, [](VarLoc::MachineLoc ML) {
+ return ML.Kind == VarLoc::MachineLocKind::WasmLocKind;
+ });
+ }
+
+ /// If this variable is described in whole or part by \p WasmLocation,
+ /// return true.
+ bool usesWasmLoc(WasmLoc WasmLocation) const {
+ MachineLoc WasmML;
+ WasmML.Kind = MachineLocKind::WasmLocKind;
+ WasmML.Value.WasmLocation = WasmLocation;
+ return is_contained(Locs, WasmML);
+ }
+
/// Determine whether the lexical scope of this value's debug location
/// dominates MBB.
bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const {
@@ -656,8 +709,9 @@ private:
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
- // TRI can be null.
- void dump(const TargetRegisterInfo *TRI, raw_ostream &Out = dbgs()) const {
+ // TRI and TII can be null.
+ void dump(const TargetRegisterInfo *TRI, const TargetInstrInfo *TII,
+ raw_ostream &Out = dbgs()) const {
Out << "VarLoc(";
for (const MachineLoc &MLoc : Locs) {
if (Locs.begin() != &MLoc)
@@ -676,6 +730,22 @@ private:
case MachineLocKind::ImmediateKind:
Out << MLoc.Value.Immediate;
break;
+ case MachineLocKind::WasmLocKind: {
+ if (TII) {
+ auto Indices = TII->getSerializableTargetIndices();
+ auto Found =
+ find_if(Indices, [&](const std::pair<int, const char *> &I) {
+ return I.first == MLoc.Value.WasmLocation.Index;
+ });
+ assert(Found != Indices.end());
+ Out << Found->second;
+ if (MLoc.Value.WasmLocation.Offset > 0)
+ Out << " + " << MLoc.Value.WasmLocation.Offset;
+ } else {
+ Out << "WasmLoc";
+ }
+ break;
+ }
case MachineLocKind::InvalidKind:
llvm_unreachable("Invalid VarLoc in dump method");
}
@@ -743,10 +813,10 @@ private:
return RegNo < LocIndex::kFirstInvalidRegLocation;
}) &&
"Physreg out of range?");
- if (VL.containsSpillLocs()) {
- LocIndex::u32_location_t Loc = LocIndex::kSpillLocation;
- Locations.push_back(Loc);
- }
+ if (VL.containsSpillLocs())
+ Locations.push_back(LocIndex::kSpillLocation);
+ if (VL.containsWasmLocs())
+ Locations.push_back(LocIndex::kWasmLocation);
} else if (VL.EVKind != VarLoc::EntryValueLocKind::EntryValueKind) {
LocIndex::u32_location_t Loc = LocIndex::kEntryValueBackupLocation;
Locations.push_back(Loc);
@@ -859,7 +929,7 @@ private:
/// Insert a set of ranges.
void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map);
- llvm::Optional<LocIndices> getEntryValueBackup(DebugVariable Var);
+ std::optional<LocIndices> getEntryValueBackup(DebugVariable Var);
/// Empty the set.
void clear() {
@@ -899,6 +969,12 @@ private:
return LocIndex::indexRangeForLocation(
getVarLocs(), LocIndex::kEntryValueBackupLocation);
}
+
+ /// Get all set IDs for VarLocs with MLs of kind WasmLocKind.
+ auto getWasmVarLocs() const {
+ return LocIndex::indexRangeForLocation(getVarLocs(),
+ LocIndex::kWasmLocation);
+ }
};
/// Collect all VarLoc IDs from \p CollectFrom for VarLocs with MLs of kind
@@ -946,9 +1022,9 @@ private:
/// If a given instruction is identified as a spill, return the spill location
/// and set \p Reg to the spilled register.
- Optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI,
- MachineFunction *MF,
- Register &Reg);
+ std::optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI,
+ MachineFunction *MF,
+ Register &Reg);
/// Given a spill instruction, extract the register and offset used to
/// address the spill location in a target independent way.
VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI);
@@ -985,6 +1061,8 @@ private:
VarLocMap &VarLocIDs,
InstToEntryLocMap &EntryValTransfers,
RegDefToInstMap &RegSetInstrs);
+ void transferWasmDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
+ VarLocMap &VarLocIDs);
bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges,
VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
@@ -1110,13 +1188,13 @@ void VarLocBasedLDV::OpenRangesSet::insert(LocIndices VarLocIDs,
/// Return the Loc ID of an entry value backup location, if it exists for the
/// variable.
-llvm::Optional<LocIndices>
+std::optional<LocIndices>
VarLocBasedLDV::OpenRangesSet::getEntryValueBackup(DebugVariable Var) {
auto It = EntryValuesBackupVars.find(Var);
if (It != EntryValuesBackupVars.end())
return It->second;
- return llvm::None;
+ return std::nullopt;
}
void VarLocBasedLDV::collectIDsForRegs(VarLocsInRange &Collected,
@@ -1203,7 +1281,7 @@ void VarLocBasedLDV::printVarLocInMBB(const MachineFunction &MF,
for (const VarLoc &VL : VarLocs) {
Out << " Var: " << VL.Var.getVariable()->getName();
Out << " MI: ";
- VL.dump(TRI, Out);
+ VL.dump(TRI, TII, Out);
}
}
Out << "\n";
@@ -1341,10 +1419,10 @@ void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI,
if (all_of(MI.debug_operands(), [](const MachineOperand &MO) {
return (MO.isReg() && MO.getReg()) || MO.isImm() || MO.isFPImm() ||
- MO.isCImm();
+ MO.isCImm() || MO.isTargetIndex();
})) {
// Use normal VarLoc constructor for registers and immediates.
- VarLoc VL(MI, LS);
+ VarLoc VL(MI);
// End all previous ranges of VL.Var.
OpenRanges.erase(VL);
@@ -1357,7 +1435,7 @@ void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI,
// This must be an undefined location. If it has an open range, erase it.
assert(MI.isUndefDebugValue() &&
"Unexpected non-undef DBG_VALUE encountered");
- VarLoc VL(MI, LS);
+ VarLoc VL(MI);
OpenRanges.erase(VL);
}
}
@@ -1398,7 +1476,7 @@ void VarLocBasedLDV::emitEntryValues(MachineInstr &MI,
continue;
auto DebugVar = VL.Var;
- Optional<LocIndices> EntryValBackupIDs =
+ std::optional<LocIndices> EntryValBackupIDs =
OpenRanges.getEntryValueBackup(DebugVar);
// If the parameter has the entry value backup, it means we should
@@ -1407,7 +1485,7 @@ void VarLocBasedLDV::emitEntryValues(MachineInstr &MI,
continue;
const VarLoc &EntryVL = VarLocIDs[EntryValBackupIDs->back()];
- VarLoc EntryLoc = VarLoc::CreateEntryLoc(EntryVL.MI, LS, EntryVL.Expr,
+ VarLoc EntryLoc = VarLoc::CreateEntryLoc(EntryVL.MI, EntryVL.Expr,
EntryVL.Locs[0].Value.RegNo);
LocIndices EntryValueIDs = VarLocIDs.insert(EntryLoc);
assert(EntryValueIDs.size() == 1 &&
@@ -1454,7 +1532,7 @@ void VarLocBasedLDV::insertTransferDebugPair(
ProcessVarLoc(VL);
LLVM_DEBUG({
dbgs() << "Creating VarLoc for register copy:";
- VL.dump(TRI);
+ VL.dump(TRI, TII);
});
return;
}
@@ -1467,7 +1545,7 @@ void VarLocBasedLDV::insertTransferDebugPair(
ProcessVarLoc(VL);
LLVM_DEBUG({
dbgs() << "Creating VarLoc for spill:";
- VL.dump(TRI);
+ VL.dump(TRI, TII);
});
return;
}
@@ -1480,7 +1558,7 @@ void VarLocBasedLDV::insertTransferDebugPair(
ProcessVarLoc(VL);
LLVM_DEBUG({
dbgs() << "Creating VarLoc for restore:";
- VL.dump(TRI);
+ VL.dump(TRI, TII);
});
return;
}
@@ -1509,8 +1587,7 @@ void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI,
SmallVector<const uint32_t *, 4> RegMasks;
for (const MachineOperand &MO : MI.operands()) {
// Determine whether the operand is a register def.
- if (MO.isReg() && MO.isDef() && MO.getReg() &&
- Register::isPhysicalRegister(MO.getReg()) &&
+ if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical() &&
!(MI.isCall() && MO.getReg() == SP)) {
// Remove ranges of all aliased registers.
for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
@@ -1565,6 +1642,30 @@ void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI,
}
}
+void VarLocBasedLDV::transferWasmDef(MachineInstr &MI,
+ OpenRangesSet &OpenRanges,
+ VarLocMap &VarLocIDs) {
+ // If this is not a Wasm local.set or local.tee, which sets local values,
+ // return.
+ int Index;
+ int64_t Offset;
+ if (!TII->isExplicitTargetIndexDef(MI, Index, Offset))
+ return;
+
+ // Find the target indices killed by MI, and delete those variable locations
+ // from the open range.
+ VarLocsInRange KillSet;
+ VarLoc::WasmLoc Loc{Index, Offset};
+ for (uint64_t ID : OpenRanges.getWasmVarLocs()) {
+ LocIndex Idx = LocIndex::fromRawInteger(ID);
+ const VarLoc &VL = VarLocIDs[Idx];
+ assert(VL.containsWasmLocs() && "Broken VarLocSet?");
+ if (VL.usesWasmLoc(Loc))
+ KillSet.insert(ID);
+ }
+ OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kWasmLocation);
+}
+
bool VarLocBasedLDV::isSpillInstruction(const MachineInstr &MI,
MachineFunction *MF) {
// TODO: Handle multiple stores folded into one.
@@ -1618,11 +1719,11 @@ bool VarLocBasedLDV::isLocationSpill(const MachineInstr &MI,
return false;
}
-Optional<VarLocBasedLDV::VarLoc::SpillLoc>
+std::optional<VarLocBasedLDV::VarLoc::SpillLoc>
VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI,
- MachineFunction *MF, Register &Reg) {
+ MachineFunction *MF, Register &Reg) {
if (!MI.hasOneMemOperand())
- return None;
+ return std::nullopt;
// FIXME: Handle folded restore instructions with more than one memory
// operand.
@@ -1630,7 +1731,7 @@ VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI,
Reg = MI.getOperand(0).getReg();
return extractSpillBaseRegAndOffset(MI);
}
- return None;
+ return std::nullopt;
}
/// A spilled register may indicate that we have to end the current range of
@@ -1647,7 +1748,7 @@ void VarLocBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI,
MachineFunction *MF = MI.getMF();
TransferKind TKind;
Register Reg;
- Optional<VarLoc::SpillLoc> Loc;
+ std::optional<VarLoc::SpillLoc> Loc;
LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump(););
@@ -1777,7 +1878,7 @@ void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI,
if (VL.isEntryValueBackupReg(SrcReg)) {
LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump(););
VarLoc EntryValLocCopyBackup =
- VarLoc::CreateEntryCopyBackupLoc(VL.MI, LS, VL.Expr, DestReg);
+ VarLoc::CreateEntryCopyBackupLoc(VL.MI, VL.Expr, DestReg);
// Stop tracking the original entry value.
OpenRanges.erase(VL);
@@ -1818,7 +1919,7 @@ bool VarLocBasedLDV::transferTerminator(MachineBasicBlock *CurMBB,
for (VarLoc &VL : VarLocs) {
// Copy OpenRanges to OutLocs, if not already present.
dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": ";
- VL.dump(TRI);
+ VL.dump(TRI, TII);
}
});
VarLocSet &VLS = getVarLocsInMBB(CurMBB, OutLocs);
@@ -1903,6 +2004,7 @@ void VarLocBasedLDV::process(MachineInstr &MI, OpenRangesSet &OpenRanges,
RegSetInstrs);
transferRegisterDef(MI, OpenRanges, VarLocIDs, EntryValTransfers,
RegSetInstrs);
+ transferWasmDef(MI, OpenRanges, VarLocIDs);
transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers);
transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers);
}
@@ -2058,10 +2160,13 @@ bool VarLocBasedLDV::isEntryValueCandidate(
/// Collect all register defines (including aliases) for the given instruction.
static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs,
const TargetRegisterInfo *TRI) {
- for (const MachineOperand &MO : MI.operands())
- if (MO.isReg() && MO.isDef() && MO.getReg())
+ for (const MachineOperand &MO : MI.operands()) {
+ if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical()) {
+ Regs.insert(MO.getReg());
for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
Regs.insert(*AI);
+ }
+ }
}
/// This routine records the entry values of function parameters. The values
@@ -2090,7 +2195,7 @@ void VarLocBasedLDV::recordEntryValue(const MachineInstr &MI,
// valid. It is valid until a parameter is not changed.
DIExpression *NewExpr =
DIExpression::prepend(MI.getDebugExpression(), DIExpression::EntryValue);
- VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, LS, NewExpr);
+ VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, NewExpr);
LocIndices EntryValLocIDs = VarLocIDs.insert(EntryValLocAsBackup);
OpenRanges.insert(EntryValLocIDs, EntryValLocAsBackup);
}
@@ -2102,7 +2207,7 @@ bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF,
TargetPassConfig *TPC, unsigned InputBBLimit,
unsigned InputDbgValLimit) {
(void)DomTree;
- LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n");
+ LLVM_DEBUG(dbgs() << "\nDebug Range Extension: " << MF.getName() << "\n");
if (!MF.getFunction().getSubprogram())
// VarLocBaseLDV will already have removed all DBG_VALUEs.