Skip to content

Commit ab49dce

Browse files
committed
[DebugInfo][InstrRef][NFC] Use unique_ptr instead of raw pointers
InstrRefBasedLDV allocates some big tables of ValueIDNum, to store live-in and live-out block values in, that then get passed around as pointers everywhere. This patch wraps the allocation in a std::unique_ptr, names some types based on unique_ptr, and passes references to those around instead. There's no functional change, but it makes it clearer to the reader that references to these tables are borrowed rather than owned, and we get some extra validity assertions too. Differential Revision: https://reviews.llvm.org/D118774
1 parent 75db179 commit ab49dce

File tree

3 files changed

+613
-634
lines changed

3 files changed

+613
-634
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class TransferTracker {
266266
/// object fields to track variable locations as we step through the block.
267267
/// FIXME: could just examine mloctracker instead of passing in \p mlocs?
268268
void
269-
loadInlocs(MachineBasicBlock &MBB, ValueIDNum *MLocs,
269+
loadInlocs(MachineBasicBlock &MBB, ValueTable &MLocs,
270270
const SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs,
271271
unsigned NumLocs) {
272272
ActiveMLocs.clear();
@@ -1022,8 +1022,8 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
10221022
}
10231023

10241024
bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
1025-
ValueIDNum **MLiveOuts,
1026-
ValueIDNum **MLiveIns) {
1025+
const ValueTable *MLiveOuts,
1026+
const ValueTable *MLiveIns) {
10271027
if (!MI.isDebugRef())
10281028
return false;
10291029

@@ -1782,8 +1782,8 @@ void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
17821782
AllSeenFragments.insert(ThisFragment);
17831783
}
17841784

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

18341834
// Step through each instruction in this block.
18351835
for (auto &MI : MBB) {
1836-
process(MI);
1836+
// Pass in an empty unique_ptr for the value tables when accumulating the
1837+
// machine transfer function.
1838+
process(MI, nullptr, nullptr);
1839+
18371840
// Also accumulate fragment map.
18381841
if (MI.isDebugValue() || MI.isDebugRef())
18391842
accumulateFragmentMap(MI);
@@ -1922,7 +1925,7 @@ void InstrRefBasedLDV::produceMLocTransferFunction(
19221925

19231926
bool InstrRefBasedLDV::mlocJoin(
19241927
MachineBasicBlock &MBB, SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1925-
ValueIDNum **OutLocs, ValueIDNum *InLocs) {
1928+
FuncValueTable &OutLocs, ValueTable &InLocs) {
19261929
LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
19271930
bool Changed = false;
19281931

@@ -2023,7 +2026,7 @@ void InstrRefBasedLDV::findStackIndexInterference(
20232026

20242027
void InstrRefBasedLDV::placeMLocPHIs(
20252028
MachineFunction &MF, SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
2026-
ValueIDNum **MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
2029+
FuncValueTable &MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
20272030
SmallVector<unsigned, 4> StackUnits;
20282031
findStackIndexInterference(StackUnits);
20292032

@@ -2152,7 +2155,7 @@ void InstrRefBasedLDV::placeMLocPHIs(
21522155
}
21532156

21542157
void InstrRefBasedLDV::buildMLocValueMap(
2155-
MachineFunction &MF, ValueIDNum **MInLocs, ValueIDNum **MOutLocs,
2158+
MachineFunction &MF, FuncValueTable &MInLocs, FuncValueTable &MOutLocs,
21562159
SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
21572160
std::priority_queue<unsigned int, std::vector<unsigned int>,
21582161
std::greater<unsigned int>>
@@ -2294,7 +2297,7 @@ void InstrRefBasedLDV::BlockPHIPlacement(
22942297

22952298
Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc(
22962299
const MachineBasicBlock &MBB, const DebugVariable &Var,
2297-
const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs,
2300+
const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs,
22982301
const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
22992302
// Collect a set of locations from predecessor where its live-out value can
23002303
// be found.
@@ -2562,7 +2565,7 @@ void InstrRefBasedLDV::getBlocksForScope(
25622565
void InstrRefBasedLDV::buildVLocValueMap(
25632566
const DILocation *DILoc, const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
25642567
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output,
2565-
ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
2568+
FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
25662569
SmallVectorImpl<VLocTracker> &AllTheVLocs) {
25672570
// This method is much like buildMLocValueMap: but focuses on a single
25682571
// LexicalScope at a time. Pick out a set of blocks and variables that are
@@ -2947,7 +2950,7 @@ void InstrRefBasedLDV::makeDepthFirstEjectionMap(
29472950
bool InstrRefBasedLDV::depthFirstVLocAndEmit(
29482951
unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
29492952
const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToAssignBlocks,
2950-
LiveInsT &Output, ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
2953+
LiveInsT &Output, FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
29512954
SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
29522955
DenseMap<DebugVariable, unsigned> &AllVarsNumbering,
29532956
const TargetPassConfig &TPC) {
@@ -2956,15 +2959,8 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
29562959
VTracker = nullptr;
29572960

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

29692965
// Build map from block number to the last scope that uses the block.
29702966
SmallVector<unsigned, 16> EjectionMap;
@@ -2988,17 +2984,14 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
29882984
CurBB = BBNum;
29892985
CurInst = 1;
29902986
for (auto &MI : MBB) {
2991-
process(MI, MOutLocs, MInLocs);
2987+
process(MI, MOutLocs.get(), MInLocs.get());
29922988
TTracker->checkInstForNewValues(CurInst, MI.getIterator());
29932989
++CurInst;
29942990
}
29952991

29962992
// Free machine-location tables for this block.
2997-
delete[] MInLocs[BBNum];
2998-
delete[] MOutLocs[BBNum];
2999-
// Make ourselves brittle to use-after-free errors.
3000-
MInLocs[BBNum] = nullptr;
3001-
MOutLocs[BBNum] = nullptr;
2993+
MInLocs[BBNum].reset();
2994+
MOutLocs[BBNum].reset();
30022995
// We don't need live-in variable values for this block either.
30032996
Output[BBNum].clear();
30042997
AllTheVLocs[BBNum].clear();
@@ -3066,16 +3059,6 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
30663059
if (MOutLocs[MBB->getNumber()])
30673060
EjectBlock(*MBB);
30683061

3069-
// Finally, there might have been gaps in the block numbering, from dead
3070-
// blocks being deleted or folded. In those scenarios, we might allocate a
3071-
// block-table that's never ejected, meaning we have to free it at the end.
3072-
for (unsigned int I = 0; I < MaxNumBlocks; ++I) {
3073-
if (MInLocs[I]) {
3074-
delete[] MInLocs[I];
3075-
delete[] MOutLocs[I];
3076-
}
3077-
}
3078-
30793062
return emitTransfers(AllVarsNumbering);
30803063
}
30813064

@@ -3173,13 +3156,13 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
31733156
// Allocate and initialize two array-of-arrays for the live-in and live-out
31743157
// machine values. The outer dimension is the block number; while the inner
31753158
// dimension is a LocIdx from MLocTracker.
3176-
ValueIDNum **MOutLocs = new ValueIDNum *[MaxNumBlocks];
3177-
ValueIDNum **MInLocs = new ValueIDNum *[MaxNumBlocks];
3159+
FuncValueTable MOutLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
3160+
FuncValueTable MInLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
31783161
unsigned NumLocs = MTracker->getNumLocs();
31793162
for (int i = 0; i < MaxNumBlocks; ++i) {
31803163
// These all auto-initialize to ValueIDNum::EmptyValue
3181-
MOutLocs[i] = new ValueIDNum[NumLocs];
3182-
MInLocs[i] = new ValueIDNum[NumLocs];
3164+
MOutLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
3165+
MInLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
31833166
}
31843167

31853168
// Solve the machine value dataflow problem using the MLocTransfer function,
@@ -3216,7 +3199,7 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32163199
MTracker->loadFromArray(MInLocs[CurBB], CurBB);
32173200
CurInst = 1;
32183201
for (auto &MI : MBB) {
3219-
process(MI, MOutLocs, MInLocs);
3202+
process(MI, MOutLocs.get(), MInLocs.get());
32203203
++CurInst;
32213204
}
32223205
MTracker->reset();
@@ -3271,12 +3254,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32713254
<< " has " << MaxNumBlocks << " basic blocks and "
32723255
<< VarAssignCount
32733256
<< " variable assignments, exceeding limits.\n");
3274-
3275-
// Perform memory cleanup that emitLocations would do otherwise.
3276-
for (int Idx = 0; Idx < MaxNumBlocks; ++Idx) {
3277-
delete[] MOutLocs[Idx];
3278-
delete[] MInLocs[Idx];
3279-
}
32803257
} else {
32813258
// Optionally, solve the variable value problem and emit to blocks by using
32823259
// a lexical-scope-depth search. It should be functionally identical to
@@ -3286,10 +3263,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32863263
SavedLiveIns, MOutLocs, MInLocs, vlocs, MF, AllVarsNumbering, *TPC);
32873264
}
32883265

3289-
// Elements of these arrays will be deleted by emitLocations.
3290-
delete[] MOutLocs;
3291-
delete[] MInLocs;
3292-
32933266
delete MTracker;
32943267
delete TTracker;
32953268
MTracker = nullptr;
@@ -3406,9 +3379,10 @@ class LDVSSAUpdater {
34063379
/// Machine location where any PHI must occur.
34073380
LocIdx Loc;
34083381
/// Table of live-in machine value numbers for blocks / locations.
3409-
ValueIDNum **MLiveIns;
3382+
const ValueTable *MLiveIns;
34103383

3411-
LDVSSAUpdater(LocIdx L, ValueIDNum **MLiveIns) : Loc(L), MLiveIns(MLiveIns) {}
3384+
LDVSSAUpdater(LocIdx L, const ValueTable *MLiveIns)
3385+
: Loc(L), MLiveIns(MLiveIns) {}
34123386

34133387
void reset() {
34143388
for (auto &Block : BlockMap)
@@ -3565,11 +3539,13 @@ template <> class SSAUpdaterTraits<LDVSSAUpdater> {
35653539

35663540
} // end namespace llvm
35673541

3568-
Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF,
3569-
ValueIDNum **MLiveOuts,
3570-
ValueIDNum **MLiveIns,
3571-
MachineInstr &Here,
3572-
uint64_t InstrNum) {
3542+
Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
3543+
MachineFunction &MF, const ValueTable *MLiveOuts,
3544+
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
3545+
assert(MLiveOuts && MLiveIns &&
3546+
"Tried to resolve DBG_PHI before location "
3547+
"tables allocated?");
3548+
35733549
// This function will be called twice per DBG_INSTR_REF, and might end up
35743550
// computing lots of SSA information: memoize it.
35753551
auto SeenDbgPHIIt = SeenDbgPHIs.find(&Here);
@@ -3583,8 +3559,8 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF,
35833559
}
35843560

35853561
Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
3586-
MachineFunction &MF, ValueIDNum **MLiveOuts, ValueIDNum **MLiveIns,
3587-
MachineInstr &Here, uint64_t InstrNum) {
3562+
MachineFunction &MF, const ValueTable *MLiveOuts,
3563+
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
35883564
// Pick out records of DBG_PHI instructions that have been observed. If there
35893565
// are none, then we cannot compute a value number.
35903566
auto RangePair = std::equal_range(DebugPHINumToValue.begin(),
@@ -3691,7 +3667,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
36913667
return None;
36923668

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

36963672
auto VVal = ValidatedValues.find(PHIIt.first);
36973673
if (VVal == ValidatedValues.end()) {

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class ValueIDNum {
171171
static ValueIDNum TombstoneValue;
172172
};
173173

174+
/// Type for a table of values in a block.
175+
using ValueTable = std::unique_ptr<ValueIDNum[]>;
176+
177+
/// Type for a table-of-table-of-values, i.e., the collection of either
178+
/// live-in or live-out values for each block in the function.
179+
using FuncValueTable = std::unique_ptr<ValueTable[]>;
180+
174181
/// Thin wrapper around an integer -- designed to give more type safety to
175182
/// spill location numbers.
176183
class SpillLocationNo {
@@ -507,7 +514,7 @@ class MLocTracker {
507514

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

917924
/// Observe a single instruction while stepping through a block.
918-
void process(MachineInstr &MI, ValueIDNum **MLiveOuts = nullptr,
919-
ValueIDNum **MLiveIns = nullptr);
925+
void process(MachineInstr &MI, const ValueTable *MLiveOuts,
926+
const ValueTable *MLiveIns);
920927

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

925932
/// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers.
926933
/// \returns true if MI was recognized and processed.
927-
bool transferDebugInstrRef(MachineInstr &MI, ValueIDNum **MLiveOuts,
928-
ValueIDNum **MLiveIns);
934+
bool transferDebugInstrRef(MachineInstr &MI, const ValueTable *MLiveOuts,
935+
const ValueTable *MLiveIns);
929936

930937
/// Stores value-information about where this PHI occurred, and what
931938
/// instruction number is associated with it.
@@ -957,13 +964,13 @@ class InstrRefBasedLDV : public LDVImpl {
957964
/// \p InstrNum Debug instruction number defined by DBG_PHI instructions.
958965
/// \returns The machine value number at position Here, or None.
959966
Optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF,
960-
ValueIDNum **MLiveOuts,
961-
ValueIDNum **MLiveIns, MachineInstr &Here,
962-
uint64_t InstrNum);
967+
const ValueTable *MLiveOuts,
968+
const ValueTable *MLiveIns,
969+
MachineInstr &Here, uint64_t InstrNum);
963970

964971
Optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF,
965-
ValueIDNum **MLiveOuts,
966-
ValueIDNum **MLiveIns,
972+
const ValueTable *MLiveOuts,
973+
const ValueTable *MLiveIns,
967974
MachineInstr &Here,
968975
uint64_t InstrNum);
969976

@@ -981,8 +988,8 @@ class InstrRefBasedLDV : public LDVImpl {
981988
/// live-out arrays to the (initialized to zero) multidimensional arrays in
982989
/// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block
983990
/// number, the inner by LocIdx.
984-
void buildMLocValueMap(MachineFunction &MF, ValueIDNum **MInLocs,
985-
ValueIDNum **MOutLocs,
991+
void buildMLocValueMap(MachineFunction &MF, FuncValueTable &MInLocs,
992+
FuncValueTable &MOutLocs,
986993
SmallVectorImpl<MLocTransferMap> &MLocTransfer);
987994

988995
/// Examine the stack indexes (i.e. offsets within the stack) to find the
@@ -993,7 +1000,7 @@ class InstrRefBasedLDV : public LDVImpl {
9931000
/// the IDF of each register.
9941001
void placeMLocPHIs(MachineFunction &MF,
9951002
SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
996-
ValueIDNum **MInLocs,
1003+
FuncValueTable &MInLocs,
9971004
SmallVectorImpl<MLocTransferMap> &MLocTransfer);
9981005

9991006
/// Propagate variable values to blocks in the common case where there's
@@ -1024,7 +1031,7 @@ class InstrRefBasedLDV : public LDVImpl {
10241031
/// is true, revisiting this block is necessary.
10251032
bool mlocJoin(MachineBasicBlock &MBB,
10261033
SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1027-
ValueIDNum **OutLocs, ValueIDNum *InLocs);
1034+
FuncValueTable &OutLocs, ValueTable &InLocs);
10281035

10291036
/// Produce a set of blocks that are in the current lexical scope. This means
10301037
/// those blocks that contain instructions "in" the scope, blocks where
@@ -1052,11 +1059,11 @@ class InstrRefBasedLDV : public LDVImpl {
10521059
/// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks
10531060
/// locations through.
10541061
void buildVLocValueMap(const DILocation *DILoc,
1055-
const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
1056-
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
1057-
LiveInsT &Output, ValueIDNum **MOutLocs,
1058-
ValueIDNum **MInLocs,
1059-
SmallVectorImpl<VLocTracker> &AllTheVLocs);
1062+
const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
1063+
SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
1064+
LiveInsT &Output, FuncValueTable &MOutLocs,
1065+
FuncValueTable &MInLocs,
1066+
SmallVectorImpl<VLocTracker> &AllTheVLocs);
10601067

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

10801087
/// Take collections of DBG_VALUE instructions stored in TTracker, and
@@ -1104,7 +1111,7 @@ class InstrRefBasedLDV : public LDVImpl {
11041111
bool depthFirstVLocAndEmit(
11051112
unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
11061113
const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToBlocks,
1107-
LiveInsT &Output, ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
1114+
LiveInsT &Output, FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
11081115
SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
11091116
DenseMap<DebugVariable, unsigned> &AllVarsNumbering,
11101117
const TargetPassConfig &TPC);

0 commit comments

Comments
 (0)