Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for llvm/llvm-project#54130 #112

Open
wants to merge 1 commit into
base: release/14.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 37 additions & 61 deletions llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class TransferTracker {
/// 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, ValueIDNum *MLocs,
loadInlocs(MachineBasicBlock &MBB, ValueTable &MLocs,
const SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs,
unsigned NumLocs) {
ActiveMLocs.clear();
Expand Down Expand Up @@ -1022,8 +1022,8 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
}

bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns) {
const ValueTable *MLiveOuts,
const ValueTable *MLiveIns) {
if (!MI.isDebugRef())
return false;

Expand Down Expand Up @@ -1755,8 +1755,8 @@ void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
AllSeenFragments.insert(ThisFragment);
}

void InstrRefBasedLDV::process(MachineInstr &MI, ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns) {
void InstrRefBasedLDV::process(MachineInstr &MI, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns) {
// Try to interpret an MI as a debug or transfer instruction. Only if it's
// none of these should we interpret it's register defs as new value
// definitions.
Expand Down Expand Up @@ -1806,7 +1806,10 @@ void InstrRefBasedLDV::produceMLocTransferFunction(

// Step through each instruction in this block.
for (auto &MI : MBB) {
process(MI);
// Pass in an empty unique_ptr for the value tables when accumulating the
// machine transfer function.
process(MI, nullptr, nullptr);

// Also accumulate fragment map.
if (MI.isDebugValue() || MI.isDebugRef())
accumulateFragmentMap(MI);
Expand Down Expand Up @@ -1895,7 +1898,7 @@ void InstrRefBasedLDV::produceMLocTransferFunction(

bool InstrRefBasedLDV::mlocJoin(
MachineBasicBlock &MBB, SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
ValueIDNum **OutLocs, ValueIDNum *InLocs) {
FuncValueTable &OutLocs, ValueTable &InLocs) {
LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
bool Changed = false;

Expand Down Expand Up @@ -1996,7 +1999,7 @@ void InstrRefBasedLDV::findStackIndexInterference(

void InstrRefBasedLDV::placeMLocPHIs(
MachineFunction &MF, SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
ValueIDNum **MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
FuncValueTable &MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
SmallVector<unsigned, 4> StackUnits;
findStackIndexInterference(StackUnits);

Expand Down Expand Up @@ -2125,7 +2128,7 @@ void InstrRefBasedLDV::placeMLocPHIs(
}

void InstrRefBasedLDV::buildMLocValueMap(
MachineFunction &MF, ValueIDNum **MInLocs, ValueIDNum **MOutLocs,
MachineFunction &MF, FuncValueTable &MInLocs, FuncValueTable &MOutLocs,
SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
std::priority_queue<unsigned int, std::vector<unsigned int>,
std::greater<unsigned int>>
Expand Down Expand Up @@ -2267,7 +2270,7 @@ void InstrRefBasedLDV::BlockPHIPlacement(

Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
const MachineBasicBlock &MBB, const DebugVariable &Var,
const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs,
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.
Expand Down Expand Up @@ -2535,7 +2538,7 @@ void InstrRefBasedLDV::getBlocksForScope(
void InstrRefBasedLDV::buildVLocValueMap(
const DILocation *DILoc, const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output,
ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
SmallVectorImpl<VLocTracker> &AllTheVLocs) {
// This method is much like buildMLocValueMap: but focuses on a single
// LexicalScope at a time. Pick out a set of blocks and variables that are
Expand Down Expand Up @@ -2920,7 +2923,7 @@ void InstrRefBasedLDV::makeDepthFirstEjectionMap(
bool InstrRefBasedLDV::depthFirstVLocAndEmit(
unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToAssignBlocks,
LiveInsT &Output, ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
LiveInsT &Output, FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
DenseMap<DebugVariable, unsigned> &AllVarsNumbering,
const TargetPassConfig &TPC) {
Expand All @@ -2929,15 +2932,8 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
VTracker = nullptr;

// No scopes? No variable locations.
if (!LS.getCurrentFunctionScope()) {
// FIXME: this is a sticking plaster to prevent a memory leak, these
// pointers will be automagically freed by being unique pointers, shortly.
for (unsigned int I = 0; I < MaxNumBlocks; ++I) {
delete[] MInLocs[I];
delete[] MOutLocs[I];
}
if (!LS.getCurrentFunctionScope())
return false;
}

// Build map from block number to the last scope that uses the block.
SmallVector<unsigned, 16> EjectionMap;
Expand All @@ -2961,17 +2957,14 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
CurBB = BBNum;
CurInst = 1;
for (auto &MI : MBB) {
process(MI, MOutLocs, MInLocs);
process(MI, MOutLocs.get(), MInLocs.get());
TTracker->checkInstForNewValues(CurInst, MI.getIterator());
++CurInst;
}

// Free machine-location tables for this block.
delete[] MInLocs[BBNum];
delete[] MOutLocs[BBNum];
// Make ourselves brittle to use-after-free errors.
MInLocs[BBNum] = nullptr;
MOutLocs[BBNum] = nullptr;
MInLocs[BBNum].reset();
MOutLocs[BBNum].reset();
// We don't need live-in variable values for this block either.
Output[BBNum].clear();
AllTheVLocs[BBNum].clear();
Expand Down Expand Up @@ -3039,16 +3032,6 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
if (MOutLocs[MBB->getNumber()])
EjectBlock(*MBB);

// Finally, there might have been gaps in the block numbering, from dead
// blocks being deleted or folded. In those scenarios, we might allocate a
// block-table that's never ejected, meaning we have to free it at the end.
for (unsigned int I = 0; I < MaxNumBlocks; ++I) {
if (MInLocs[I]) {
delete[] MInLocs[I];
delete[] MOutLocs[I];
}
}

return emitTransfers(AllVarsNumbering);
}

Expand Down Expand Up @@ -3146,13 +3129,13 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
// Allocate and initialize two array-of-arrays for the live-in and live-out
// machine values. The outer dimension is the block number; while the inner
// dimension is a LocIdx from MLocTracker.
ValueIDNum **MOutLocs = new ValueIDNum *[MaxNumBlocks];
ValueIDNum **MInLocs = new ValueIDNum *[MaxNumBlocks];
FuncValueTable MOutLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
FuncValueTable MInLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
unsigned NumLocs = MTracker->getNumLocs();
for (int i = 0; i < MaxNumBlocks; ++i) {
// These all auto-initialize to ValueIDNum::EmptyValue
MOutLocs[i] = new ValueIDNum[NumLocs];
MInLocs[i] = new ValueIDNum[NumLocs];
MOutLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
MInLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
}

// Solve the machine value dataflow problem using the MLocTransfer function,
Expand Down Expand Up @@ -3186,7 +3169,7 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
MTracker->loadFromArray(MInLocs[CurBB], CurBB);
CurInst = 1;
for (auto &MI : MBB) {
process(MI, MOutLocs, MInLocs);
process(MI, MOutLocs.get(), MInLocs.get());
++CurInst;
}
MTracker->reset();
Expand Down Expand Up @@ -3241,12 +3224,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
<< " has " << MaxNumBlocks << " basic blocks and "
<< VarAssignCount
<< " variable assignments, exceeding limits.\n");

// Perform memory cleanup that emitLocations would do otherwise.
for (int Idx = 0; Idx < MaxNumBlocks; ++Idx) {
delete[] MOutLocs[Idx];
delete[] MInLocs[Idx];
}
} else {
// Optionally, solve the variable value problem and emit to blocks by using
// a lexical-scope-depth search. It should be functionally identical to
Expand All @@ -3256,10 +3233,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
SavedLiveIns, MOutLocs, MInLocs, vlocs, MF, AllVarsNumbering, *TPC);
}

// Elements of these arrays will be deleted by emitLocations.
delete[] MOutLocs;
delete[] MInLocs;

delete MTracker;
delete TTracker;
MTracker = nullptr;
Expand Down Expand Up @@ -3376,9 +3349,10 @@ class LDVSSAUpdater {
/// Machine location where any PHI must occur.
LocIdx Loc;
/// Table of live-in machine value numbers for blocks / locations.
ValueIDNum **MLiveIns;
const ValueTable *MLiveIns;

LDVSSAUpdater(LocIdx L, ValueIDNum **MLiveIns) : Loc(L), MLiveIns(MLiveIns) {}
LDVSSAUpdater(LocIdx L, const ValueTable *MLiveIns)
: Loc(L), MLiveIns(MLiveIns) {}

void reset() {
for (auto &Block : BlockMap)
Expand Down Expand Up @@ -3535,11 +3509,13 @@ template <> class SSAUpdaterTraits<LDVSSAUpdater> {

} // end namespace llvm

Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF,
ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns,
MachineInstr &Here,
uint64_t InstrNum) {
Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
MachineFunction &MF, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
assert(MLiveOuts && MLiveIns &&
"Tried to resolve DBG_PHI before location "
"tables allocated?");

// 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);
Expand All @@ -3553,8 +3529,8 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF,
}

Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
MachineFunction &MF, ValueIDNum **MLiveOuts, ValueIDNum **MLiveIns,
MachineInstr &Here, uint64_t InstrNum) {
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
// are none, then we cannot compute a value number.
auto RangePair = std::equal_range(DebugPHINumToValue.begin(),
Expand Down Expand Up @@ -3654,7 +3630,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
return None;

ValueIDNum ValueToCheck;
ValueIDNum *BlockLiveOuts = MLiveOuts[PHIIt.first->BB.getNumber()];
const ValueTable &BlockLiveOuts = MLiveOuts[PHIIt.first->BB.getNumber()];

auto VVal = ValidatedValues.find(PHIIt.first);
if (VVal == ValidatedValues.end()) {
Expand Down
49 changes: 28 additions & 21 deletions llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ class ValueIDNum {
static ValueIDNum TombstoneValue;
};

/// Type for a table of values in a block.
using ValueTable = std::unique_ptr<ValueIDNum[]>;

/// Type for a table-of-table-of-values, i.e., the collection of either
/// live-in or live-out values for each block in the function.
using FuncValueTable = std::unique_ptr<ValueTable[]>;

/// Thin wrapper around an integer -- designed to give more type safety to
/// spill location numbers.
class SpillLocationNo {
Expand Down Expand Up @@ -507,7 +514,7 @@ class MLocTracker {

/// Load values for each location from array of ValueIDNums. Take current
/// bbnum just in case we read a value from a hitherto untouched register.
void loadFromArray(ValueIDNum *Locs, unsigned NewCurBB) {
void loadFromArray(ValueTable &Locs, unsigned NewCurBB) {
CurBB = NewCurBB;
// Iterate over all tracked locations, and load each locations live-in
// value into our local index.
Expand Down Expand Up @@ -909,17 +916,17 @@ class InstrRefBasedLDV : public LDVImpl {
extractSpillBaseRegAndOffset(const MachineInstr &MI);

/// Observe a single instruction while stepping through a block.
void process(MachineInstr &MI, ValueIDNum **MLiveOuts = nullptr,
ValueIDNum **MLiveIns = nullptr);
void process(MachineInstr &MI, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns);

/// Examines whether \p MI is a DBG_VALUE and notifies trackers.
/// \returns true if MI was recognized and processed.
bool transferDebugValue(const MachineInstr &MI);

/// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers.
/// \returns true if MI was recognized and processed.
bool transferDebugInstrRef(MachineInstr &MI, ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns);
bool transferDebugInstrRef(MachineInstr &MI, const ValueTable *MLiveOuts,
const ValueTable *MLiveIns);

/// Stores value-information about where this PHI occurred, and what
/// instruction number is associated with it.
Expand Down Expand Up @@ -951,13 +958,13 @@ class InstrRefBasedLDV : public LDVImpl {
/// \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,
ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns, MachineInstr &Here,
uint64_t InstrNum);
const ValueTable *MLiveOuts,
const ValueTable *MLiveIns,
MachineInstr &Here, uint64_t InstrNum);

Optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF,
ValueIDNum **MLiveOuts,
ValueIDNum **MLiveIns,
const ValueTable *MLiveOuts,
const ValueTable *MLiveIns,
MachineInstr &Here,
uint64_t InstrNum);

Expand All @@ -975,8 +982,8 @@ class InstrRefBasedLDV : public LDVImpl {
/// live-out arrays to the (initialized to zero) multidimensional arrays in
/// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block
/// number, the inner by LocIdx.
void buildMLocValueMap(MachineFunction &MF, ValueIDNum **MInLocs,
ValueIDNum **MOutLocs,
void buildMLocValueMap(MachineFunction &MF, FuncValueTable &MInLocs,
FuncValueTable &MOutLocs,
SmallVectorImpl<MLocTransferMap> &MLocTransfer);

/// Examine the stack indexes (i.e. offsets within the stack) to find the
Expand All @@ -987,7 +994,7 @@ class InstrRefBasedLDV : public LDVImpl {
/// the IDF of each register.
void placeMLocPHIs(MachineFunction &MF,
SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
ValueIDNum **MInLocs,
FuncValueTable &MInLocs,
SmallVectorImpl<MLocTransferMap> &MLocTransfer);

/// Propagate variable values to blocks in the common case where there's
Expand Down Expand Up @@ -1018,7 +1025,7 @@ class InstrRefBasedLDV : public LDVImpl {
/// is true, revisiting this block is necessary.
bool mlocJoin(MachineBasicBlock &MBB,
SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
ValueIDNum **OutLocs, ValueIDNum *InLocs);
FuncValueTable &OutLocs, ValueTable &InLocs);

/// Produce a set of blocks that are in the current lexical scope. This means
/// those blocks that contain instructions "in" the scope, blocks where
Expand Down Expand Up @@ -1046,11 +1053,11 @@ class InstrRefBasedLDV : public LDVImpl {
/// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks
/// locations through.
void buildVLocValueMap(const DILocation *DILoc,
const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
LiveInsT &Output, ValueIDNum **MOutLocs,
ValueIDNum **MInLocs,
SmallVectorImpl<VLocTracker> &AllTheVLocs);
const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
LiveInsT &Output, FuncValueTable &MOutLocs,
FuncValueTable &MInLocs,
SmallVectorImpl<VLocTracker> &AllTheVLocs);

/// Attempt to eliminate un-necessary PHIs on entry to a block. Examines the
/// live-in values coming from predecessors live-outs, and replaces any PHIs
Expand All @@ -1068,7 +1075,7 @@ class InstrRefBasedLDV : public LDVImpl {
/// \returns Value ID of a machine PHI if an appropriate one is available.
Optional<ValueIDNum>
pickVPHILoc(const MachineBasicBlock &MBB, const DebugVariable &Var,
const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs,
const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs,
const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders);

/// Take collections of DBG_VALUE instructions stored in TTracker, and
Expand Down Expand Up @@ -1098,7 +1105,7 @@ class InstrRefBasedLDV : public LDVImpl {
bool depthFirstVLocAndEmit(
unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToBlocks,
LiveInsT &Output, ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
LiveInsT &Output, FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
DenseMap<DebugVariable, unsigned> &AllVarsNumbering,
const TargetPassConfig &TPC);
Expand Down
Loading