Skip to content

Commit 1b531d5

Browse files
[InstrRef][nfc] Remove usage of unique_ptrs of arrays (#74203)
These are usually difficult to reason about, and they were being used to pass raw pointers around with array semantic (i.e., we were using operator [] on raw pointers). To put it in InstrRef terminology: we were passing a pointer to a ValueTable but using it as if it were a FuncValueTable. These could have easily been SmallVectors, which now allow us to have reference semantics in some places, as well as simpler initialization. In the future, we can use even more pass-by-reference with some extra changes in the code.
1 parent 0d02ecc commit 1b531d5

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
14291429

14301430
std::optional<ValueIDNum> InstrRefBasedLDV::getValueForInstrRef(
14311431
unsigned InstNo, unsigned OpNo, MachineInstr &MI,
1432-
const ValueTable *MLiveOuts, const ValueTable *MLiveIns) {
1432+
const FuncValueTable *MLiveOuts, const FuncValueTable *MLiveIns) {
14331433
// Various optimizations may have happened to the value during codegen,
14341434
// recorded in the value substitution table. Apply any substitutions to
14351435
// the instruction / operand number in this DBG_INSTR_REF, and collect
@@ -1495,7 +1495,8 @@ std::optional<ValueIDNum> InstrRefBasedLDV::getValueForInstrRef(
14951495
} else if (PHIIt != DebugPHINumToValue.end() && PHIIt->InstrNum == InstNo) {
14961496
// It's actually a PHI value. Which value it is might not be obvious, use
14971497
// the resolver helper to find out.
1498-
NewID = resolveDbgPHIs(*MI.getParent()->getParent(), MLiveOuts, MLiveIns,
1498+
assert(MLiveOuts && MLiveIns);
1499+
NewID = resolveDbgPHIs(*MI.getParent()->getParent(), *MLiveOuts, *MLiveIns,
14991500
MI, InstNo);
15001501
}
15011502

@@ -1574,8 +1575,8 @@ std::optional<ValueIDNum> InstrRefBasedLDV::getValueForInstrRef(
15741575
}
15751576

15761577
bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
1577-
const ValueTable *MLiveOuts,
1578-
const ValueTable *MLiveIns) {
1578+
const FuncValueTable *MLiveOuts,
1579+
const FuncValueTable *MLiveIns) {
15791580
if (!MI.isDebugRef())
15801581
return false;
15811582

@@ -2245,8 +2246,9 @@ void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
22452246
AllSeenFragments.insert(ThisFragment);
22462247
}
22472248

2248-
void InstrRefBasedLDV::process(MachineInstr &MI, const ValueTable *MLiveOuts,
2249-
const ValueTable *MLiveIns) {
2249+
void InstrRefBasedLDV::process(MachineInstr &MI,
2250+
const FuncValueTable *MLiveOuts,
2251+
const FuncValueTable *MLiveIns) {
22502252
// Try to interpret an MI as a debug or transfer instruction. Only if it's
22512253
// none of these should we interpret it's register defs as new value
22522254
// definitions.
@@ -3503,7 +3505,10 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
35033505
// Helper lambda for ejecting a block -- if nothing is going to use the block,
35043506
// we can translate the variable location information into DBG_VALUEs and then
35053507
// free all of InstrRefBasedLDV's data structures.
3508+
SmallPtrSet<const MachineBasicBlock *, 8> EjectedBBs;
35063509
auto EjectBlock = [&](MachineBasicBlock &MBB) -> void {
3510+
if (EjectedBBs.insert(&MBB).second == false)
3511+
return;
35073512
unsigned BBNum = MBB.getNumber();
35083513
AllTheVLocs[BBNum].clear();
35093514

@@ -3517,14 +3522,14 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
35173522
CurBB = BBNum;
35183523
CurInst = 1;
35193524
for (auto &MI : MBB) {
3520-
process(MI, MOutLocs.get(), MInLocs.get());
3525+
process(MI, &MOutLocs, &MInLocs);
35213526
TTracker->checkInstForNewValues(CurInst, MI.getIterator());
35223527
++CurInst;
35233528
}
35243529

35253530
// Free machine-location tables for this block.
3526-
MInLocs[BBNum].reset();
3527-
MOutLocs[BBNum].reset();
3531+
MInLocs[BBNum] = ValueTable();
3532+
MOutLocs[BBNum] = ValueTable();
35283533
// We don't need live-in variable values for this block either.
35293534
Output[BBNum].clear();
35303535
AllTheVLocs[BBNum].clear();
@@ -3589,8 +3594,7 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
35893594
// anything for such out-of-scope blocks, but for the sake of being similar
35903595
// to VarLocBasedLDV, eject these too.
35913596
for (auto *MBB : ArtificialBlocks)
3592-
if (MOutLocs[MBB->getNumber()])
3593-
EjectBlock(*MBB);
3597+
EjectBlock(*MBB);
35943598

35953599
return emitTransfers(AllVarsNumbering);
35963600
}
@@ -3688,14 +3692,9 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
36883692
// Allocate and initialize two array-of-arrays for the live-in and live-out
36893693
// machine values. The outer dimension is the block number; while the inner
36903694
// dimension is a LocIdx from MLocTracker.
3691-
FuncValueTable MOutLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
3692-
FuncValueTable MInLocs = std::make_unique<ValueTable[]>(MaxNumBlocks);
36933695
unsigned NumLocs = MTracker->getNumLocs();
3694-
for (int i = 0; i < MaxNumBlocks; ++i) {
3695-
// These all auto-initialize to ValueIDNum::EmptyValue
3696-
MOutLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
3697-
MInLocs[i] = std::make_unique<ValueIDNum[]>(NumLocs);
3698-
}
3696+
FuncValueTable MOutLocs(MaxNumBlocks, ValueTable(NumLocs));
3697+
FuncValueTable MInLocs(MaxNumBlocks, ValueTable(NumLocs));
36993698

37003699
// Solve the machine value dataflow problem using the MLocTransfer function,
37013700
// storing the computed live-ins / live-outs into the array-of-arrays. We use
@@ -3736,7 +3735,7 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
37363735
MTracker->loadFromArray(MInLocs[CurBB], CurBB);
37373736
CurInst = 1;
37383737
for (auto &MI : MBB) {
3739-
process(MI, MOutLocs.get(), MInLocs.get());
3738+
process(MI, &MOutLocs, &MInLocs);
37403739
++CurInst;
37413740
}
37423741
MTracker->reset();
@@ -3917,9 +3916,9 @@ class LDVSSAUpdater {
39173916
/// Machine location where any PHI must occur.
39183917
LocIdx Loc;
39193918
/// Table of live-in machine value numbers for blocks / locations.
3920-
const ValueTable *MLiveIns;
3919+
const FuncValueTable &MLiveIns;
39213920

3922-
LDVSSAUpdater(LocIdx L, const ValueTable *MLiveIns)
3921+
LDVSSAUpdater(LocIdx L, const FuncValueTable &MLiveIns)
39233922
: Loc(L), MLiveIns(MLiveIns) {}
39243923

39253924
void reset() {
@@ -4075,12 +4074,8 @@ template <> class SSAUpdaterTraits<LDVSSAUpdater> {
40754074
} // end namespace llvm
40764075

40774076
std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
4078-
MachineFunction &MF, const ValueTable *MLiveOuts,
4079-
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
4080-
assert(MLiveOuts && MLiveIns &&
4081-
"Tried to resolve DBG_PHI before location "
4082-
"tables allocated?");
4083-
4077+
MachineFunction &MF, const FuncValueTable &MLiveOuts,
4078+
const FuncValueTable &MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
40844079
// This function will be called twice per DBG_INSTR_REF, and might end up
40854080
// computing lots of SSA information: memoize it.
40864081
auto SeenDbgPHIIt = SeenDbgPHIs.find(std::make_pair(&Here, InstrNum));
@@ -4094,8 +4089,8 @@ std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
40944089
}
40954090

40964091
std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
4097-
MachineFunction &MF, const ValueTable *MLiveOuts,
4098-
const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
4092+
MachineFunction &MF, const FuncValueTable &MLiveOuts,
4093+
const FuncValueTable &MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
40994094
// Pick out records of DBG_PHI instructions that have been observed. If there
41004095
// are none, then we cannot compute a value number.
41014096
auto RangePair = std::equal_range(DebugPHINumToValue.begin(),

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ namespace LiveDebugValues {
205205
using namespace llvm;
206206

207207
/// Type for a table of values in a block.
208-
using ValueTable = std::unique_ptr<ValueIDNum[]>;
208+
using ValueTable = SmallVector<ValueIDNum, 0>;
209209

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

214214
/// Thin wrapper around an integer -- designed to give more type safety to
215215
/// spill location numbers.
@@ -1200,21 +1200,21 @@ class InstrRefBasedLDV : public LDVImpl {
12001200
/// exists, otherwise returns std::nullopt.
12011201
std::optional<ValueIDNum> getValueForInstrRef(unsigned InstNo, unsigned OpNo,
12021202
MachineInstr &MI,
1203-
const ValueTable *MLiveOuts,
1204-
const ValueTable *MLiveIns);
1203+
const FuncValueTable *MLiveOuts,
1204+
const FuncValueTable *MLiveIns);
12051205

12061206
/// Observe a single instruction while stepping through a block.
1207-
void process(MachineInstr &MI, const ValueTable *MLiveOuts,
1208-
const ValueTable *MLiveIns);
1207+
void process(MachineInstr &MI, const FuncValueTable *MLiveOuts,
1208+
const FuncValueTable *MLiveIns);
12091209

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

12141214
/// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers.
12151215
/// \returns true if MI was recognized and processed.
1216-
bool transferDebugInstrRef(MachineInstr &MI, const ValueTable *MLiveOuts,
1217-
const ValueTable *MLiveIns);
1216+
bool transferDebugInstrRef(MachineInstr &MI, const FuncValueTable *MLiveOuts,
1217+
const FuncValueTable *MLiveIns);
12181218

12191219
/// Stores value-information about where this PHI occurred, and what
12201220
/// instruction number is associated with it.
@@ -1246,14 +1246,14 @@ class InstrRefBasedLDV : public LDVImpl {
12461246
/// \p InstrNum Debug instruction number defined by DBG_PHI instructions.
12471247
/// \returns The machine value number at position Here, or std::nullopt.
12481248
std::optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF,
1249-
const ValueTable *MLiveOuts,
1250-
const ValueTable *MLiveIns,
1249+
const FuncValueTable &MLiveOuts,
1250+
const FuncValueTable &MLiveIns,
12511251
MachineInstr &Here,
12521252
uint64_t InstrNum);
12531253

12541254
std::optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF,
1255-
const ValueTable *MLiveOuts,
1256-
const ValueTable *MLiveIns,
1255+
const FuncValueTable &MLiveOuts,
1256+
const FuncValueTable &MLiveIns,
12571257
MachineInstr &Here,
12581258
uint64_t InstrNum);
12591259

llvm/unittests/CodeGen/InstrRefLDVTest.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,8 @@ body: |
497497

498498
std::pair<FuncValueTable, FuncValueTable>
499499
allocValueTables(unsigned Blocks, unsigned Locs) {
500-
FuncValueTable MOutLocs = std::make_unique<ValueTable[]>(Blocks);
501-
FuncValueTable MInLocs = std::make_unique<ValueTable[]>(Blocks);
502-
503-
for (unsigned int I = 0; I < Blocks; ++I) {
504-
MOutLocs[I] = std::make_unique<ValueIDNum[]>(Locs);
505-
MInLocs[I] = std::make_unique<ValueIDNum[]>(Locs);
506-
}
507-
508-
return std::make_pair(std::move(MOutLocs), std::move(MInLocs));
500+
return {FuncValueTable(Blocks, ValueTable(Locs)),
501+
FuncValueTable(Blocks, ValueTable(Locs))};
509502
}
510503
};
511504

0 commit comments

Comments
 (0)