Skip to content

Commit 11ce014

Browse files
committed
[DebugInfo][NFC] Update LDV to use generic DBG_VALUE* MI interface
Currently, InstrRefLDV only handles DBG_VALUE instructions, not DBG_VALUE_LIST, and as a result of this it handles these instructions using functions that only work for that type of debug value, i.e. using getOperand(0) to get the debug operand. This patch changes this to use the generic debug value functions, such as getDebugOperand and isDebugOffsetImm, as well as adding an IsVariadic field to the DbgValueProperties class and a few other minor changes to acknowledge DBG_VALUE_LISTs. Note that this patch does not add support for DBG_VALUE_LIST here, but is a precursor to other patches that do add that support. Differential Revision: https://reviews.llvm.org/D128174
1 parent aa59c98 commit 11ce014

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp

+47-27
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ class TransferTracker {
442442
if (!ShouldEmitDebugEntryValues)
443443
return false;
444444

445+
// We don't currently emit entry values for DBG_VALUE_LISTs.
446+
if (Prop.IsVariadic)
447+
return false;
448+
445449
// Is the variable appropriate for entry values (i.e., is a parameter).
446450
if (!isEntryValueVariable(Var, Prop.DIExpr))
447451
return false;
@@ -456,7 +460,8 @@ class TransferTracker {
456460
Register Reg = MTracker->LocIdxToLocID[Num.getLoc()];
457461
MachineOperand MO = MachineOperand::CreateReg(Reg, false);
458462

459-
PendingDbgValues.push_back(emitMOLoc(MO, Var, {NewExpr, Prop.Indirect}));
463+
PendingDbgValues.push_back(
464+
emitMOLoc(MO, Var, {NewExpr, Prop.Indirect, Prop.IsVariadic}));
460465
return true;
461466
}
462467

@@ -466,7 +471,7 @@ class TransferTracker {
466471
MI.getDebugLoc()->getInlinedAt());
467472
DbgValueProperties Properties(MI);
468473

469-
const MachineOperand &MO = MI.getOperand(0);
474+
const MachineOperand &MO = MI.getDebugOperand(0);
470475

471476
// Ignore non-register locations, we don't transfer those.
472477
if (!MO.isReg() || MO.getReg() == 0) {
@@ -861,13 +866,38 @@ MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc,
861866
DebugLoc DL = DILocation::get(Var.getVariable()->getContext(), 0, 0,
862867
Var.getVariable()->getScope(),
863868
const_cast<DILocation *>(Var.getInlinedAt()));
864-
auto MIB = BuildMI(MF, DL, TII.get(TargetOpcode::DBG_VALUE));
869+
870+
const MCInstrDesc &Desc = Properties.IsVariadic
871+
? TII.get(TargetOpcode::DBG_VALUE_LIST)
872+
: TII.get(TargetOpcode::DBG_VALUE);
873+
874+
auto GetRegOp = [](unsigned Reg) -> MachineOperand {
875+
return MachineOperand::CreateReg(
876+
/* Reg */ Reg, /* isDef */ false, /* isImp */ false,
877+
/* isKill */ false, /* isDead */ false,
878+
/* isUndef */ false, /* isEarlyClobber */ false,
879+
/* SubReg */ 0, /* isDebug */ true);
880+
};
881+
882+
SmallVector<MachineOperand> MOs;
883+
884+
auto EmitUndef = [&]() {
885+
MOs.clear();
886+
MOs.assign(Properties.getLocationOpCount(), GetRegOp(0));
887+
return BuildMI(MF, DL, Desc, false, MOs, Var.getVariable(),
888+
Properties.DIExpr);
889+
};
890+
891+
// Only 1 location is currently supported.
892+
if (Properties.IsVariadic && Properties.getLocationOpCount() != 1)
893+
return EmitUndef();
894+
895+
bool Indirect = Properties.Indirect;
865896

866897
const DIExpression *Expr = Properties.DIExpr;
867898
if (!MLoc) {
868899
// No location -> DBG_VALUE $noreg
869-
MIB.addReg(0);
870-
MIB.addReg(0);
900+
return EmitUndef();
871901
} else if (LocIdxToLocID[*MLoc] >= NumRegs) {
872902
unsigned LocID = LocIdxToLocID[*MLoc];
873903
SpillLocationNo SpillID = locIDToSpill(LocID);
@@ -884,7 +914,7 @@ MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc,
884914
if (Offset == 0) {
885915
const SpillLoc &Spill = SpillLocs[SpillID.id()];
886916
unsigned Base = Spill.SpillBase;
887-
MIB.addReg(Base);
917+
MOs.push_back(GetRegOp(Base));
888918

889919
// There are several ways we can dereference things, and several inputs
890920
// to consider:
@@ -923,7 +953,6 @@ MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc,
923953
Expr = TRI.prependOffsetExpression(
924954
Expr, DIExpression::ApplyOffset | DIExpression::DerefAfter,
925955
Spill.SpillOffset);
926-
MIB.addImm(0);
927956
} else if (UseDerefSize) {
928957
// We're loading a value off the stack that's not the same size as the
929958
// variable. Add / subtract stack offset, explicitly deref with a size,
@@ -933,42 +962,33 @@ MachineInstrBuilder MLocTracker::emitLoc(Optional<LocIdx> MLoc,
933962
Expr = DIExpression::prependOpcodes(Expr, Ops, true);
934963
unsigned Flags = DIExpression::StackValue | DIExpression::ApplyOffset;
935964
Expr = TRI.prependOffsetExpression(Expr, Flags, Spill.SpillOffset);
936-
MIB.addReg(0);
937965
} else if (Expr->isComplex()) {
938966
// A variable with no size ambiguity, but with extra elements in it's
939967
// expression. Manually dereference the stack location.
940968
assert(Expr->isComplex());
941969
Expr = TRI.prependOffsetExpression(
942970
Expr, DIExpression::ApplyOffset | DIExpression::DerefAfter,
943971
Spill.SpillOffset);
944-
MIB.addReg(0);
945972
} else {
946973
// A plain value that has been spilt to the stack, with no further
947974
// context. Request a location expression, marking the DBG_VALUE as
948975
// IsIndirect.
949976
Expr = TRI.prependOffsetExpression(Expr, DIExpression::ApplyOffset,
950977
Spill.SpillOffset);
951-
MIB.addImm(0);
978+
Indirect = true;
952979
}
953980
} else {
954981
// This is a stack location with a weird subregister offset: emit an undef
955982
// DBG_VALUE instead.
956-
MIB.addReg(0);
957-
MIB.addReg(0);
983+
return EmitUndef();
958984
}
959985
} else {
960986
// Non-empty, non-stack slot, must be a plain register.
961987
unsigned LocID = LocIdxToLocID[*MLoc];
962-
MIB.addReg(LocID);
963-
if (Properties.Indirect)
964-
MIB.addImm(0);
965-
else
966-
MIB.addReg(0);
988+
MOs.push_back(GetRegOp(LocID));
967989
}
968990

969-
MIB.addMetadata(Var.getVariable());
970-
MIB.addMetadata(Expr);
971-
return MIB;
991+
return BuildMI(MF, DL, Desc, Indirect, MOs, Var.getVariable(), Expr);
972992
}
973993

974994
/// Default construct and initialize the pass.
@@ -1063,7 +1083,7 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
10631083
return true;
10641084
}
10651085

1066-
const MachineOperand &MO = MI.getOperand(0);
1086+
const MachineOperand &MO = MI.getDebugOperand(0);
10671087

10681088
// MLocTracker needs to know that this register is read, even if it's only
10691089
// read by a debug inst.
@@ -1081,9 +1101,8 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
10811101
VTracker->defVar(MI, Properties, MTracker->readReg(MO.getReg()));
10821102
else
10831103
VTracker->defVar(MI, Properties, None);
1084-
} else if (MI.getOperand(0).isImm() || MI.getOperand(0).isFPImm() ||
1085-
MI.getOperand(0).isCImm()) {
1086-
VTracker->defVar(MI, MI.getOperand(0));
1104+
} else if (MO.isImm() || MO.isFPImm() || MO.isCImm()) {
1105+
VTracker->defVar(MI, MO);
10871106
}
10881107
}
10891108

@@ -1267,7 +1286,7 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
12671286
// it. The rest of this LiveDebugValues implementation acts exactly the same
12681287
// for DBG_INSTR_REFs as DBG_VALUEs (just, the former can refer to values that
12691288
// aren't immediately available).
1270-
DbgValueProperties Properties(Expr, false);
1289+
DbgValueProperties Properties(Expr, false, false);
12711290
if (VTracker)
12721291
VTracker->defVar(MI, Properties, NewID);
12731292

@@ -1307,7 +1326,8 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
13071326
// later instruction in this block, this is a block-local use-before-def.
13081327
if (!FoundLoc && NewID && NewID->getBlock() == CurBB &&
13091328
NewID->getInst() > CurInst)
1310-
TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false}, *NewID);
1329+
TTracker->addUseBeforeDef(V, {MI.getDebugExpression(), false, false},
1330+
*NewID);
13111331

13121332
// Produce a DBG_VALUE representing what this DBG_INSTR_REF meant.
13131333
// This DBG_VALUE is potentially a $noreg / undefined location, if
@@ -2683,7 +2703,7 @@ void InstrRefBasedLDV::buildVLocValueMap(
26832703

26842704
// Initialize all values to start as NoVals. This signifies "it's live
26852705
// through, but we don't know what it is".
2686-
DbgValueProperties EmptyProperties(EmptyExpr, false);
2706+
DbgValueProperties EmptyProperties(EmptyExpr, false, false);
26872707
for (unsigned int I = 0; I < NumBlocks; ++I) {
26882708
DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal);
26892709
LiveIns.push_back(EmptyDbgValue);

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,35 @@ class SpillLocationNo {
199199
/// the value, and Boolean of whether or not it's indirect.
200200
class DbgValueProperties {
201201
public:
202-
DbgValueProperties(const DIExpression *DIExpr, bool Indirect)
203-
: DIExpr(DIExpr), Indirect(Indirect) {}
202+
DbgValueProperties(const DIExpression *DIExpr, bool Indirect, bool IsVariadic)
203+
: DIExpr(DIExpr), Indirect(Indirect), IsVariadic(IsVariadic) {}
204204

205205
/// Extract properties from an existing DBG_VALUE instruction.
206206
DbgValueProperties(const MachineInstr &MI) {
207207
assert(MI.isDebugValue());
208+
assert(MI.getDebugExpression()->getNumLocationOperands() == 0 ||
209+
MI.isDebugValueList() || MI.isUndefDebugValue());
210+
IsVariadic = MI.isDebugValueList();
208211
DIExpr = MI.getDebugExpression();
209-
Indirect = MI.getOperand(1).isImm();
212+
Indirect = MI.isDebugOffsetImm();
210213
}
211214

212215
bool operator==(const DbgValueProperties &Other) const {
213-
return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect);
216+
return std::tie(DIExpr, Indirect, IsVariadic) ==
217+
std::tie(Other.DIExpr, Other.Indirect, Other.IsVariadic);
214218
}
215219

216220
bool operator!=(const DbgValueProperties &Other) const {
217221
return !(*this == Other);
218222
}
219223

224+
unsigned getLocationOpCount() const {
225+
return IsVariadic ? DIExpr->getNumLocationOperands() : 1;
226+
}
227+
220228
const DIExpression *DIExpr;
221229
bool Indirect;
230+
bool IsVariadic;
222231
};
223232

224233
/// Class recording the (high level) _value_ of a variable. Identifies either
@@ -704,7 +713,7 @@ class VLocTracker {
704713

705714
public:
706715
VLocTracker(const OverlapMap &O, const DIExpression *EmptyExpr)
707-
: OverlappingFragments(O), EmptyProperties(EmptyExpr, false) {}
716+
: OverlappingFragments(O), EmptyProperties(EmptyExpr, false, false) {}
708717

709718
void defVar(const MachineInstr &MI, const DbgValueProperties &Properties,
710719
Optional<ValueIDNum> ID) {

llvm/unittests/CodeGen/InstrRefLDVTest.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ TEST_F(InstrRefLDVTest, pickVPHILocDiamond) {
18001800
ValueIDNum RspPHIInBlk3(RetBlk, 0, RspLoc);
18011801

18021802
DebugVariable Var(FuncVariable, None, nullptr);
1803-
DbgValueProperties EmptyProps(EmptyExpr, false);
1803+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
18041804
SmallVector<DbgValue, 32> VLiveOuts;
18051805
VLiveOuts.resize(4, DbgValue(EmptyProps, DbgValue::Undef));
18061806
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -1888,13 +1888,13 @@ TEST_F(InstrRefLDVTest, pickVPHILocDiamond) {
18881888
// different indirectness or DIExpression.
18891889
DIExpression *NewExpr =
18901890
DIExpression::prepend(EmptyExpr, DIExpression::ApplyOffset, 4);
1891-
DbgValueProperties PropsWithExpr(NewExpr, false);
1891+
DbgValueProperties PropsWithExpr(NewExpr, false, false);
18921892
VLiveOuts[1] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
18931893
VLiveOuts[2] = DbgValue(LiveInRsp, PropsWithExpr, DbgValue::Def);
18941894
Result = pickVPHILoc(*MBB3, Var, VLiveOutIdx, MOutLocs, Preds);
18951895
EXPECT_FALSE(Result);
18961896

1897-
DbgValueProperties PropsWithIndirect(EmptyExpr, true);
1897+
DbgValueProperties PropsWithIndirect(EmptyExpr, true, false);
18981898
VLiveOuts[1] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
18991899
VLiveOuts[2] = DbgValue(LiveInRsp, PropsWithIndirect, DbgValue::Def);
19001900
Result = pickVPHILoc(*MBB3, Var, VLiveOutIdx, MOutLocs, Preds);
@@ -1929,7 +1929,7 @@ TEST_F(InstrRefLDVTest, pickVPHILocLoops) {
19291929
ValueIDNum RaxPHIInBlk1(LoopBlk, 0, RaxLoc);
19301930

19311931
DebugVariable Var(FuncVariable, None, nullptr);
1932-
DbgValueProperties EmptyProps(EmptyExpr, false);
1932+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
19331933
SmallVector<DbgValue, 32> VLiveOuts;
19341934
VLiveOuts.resize(3, DbgValue(EmptyProps, DbgValue::Undef));
19351935
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -2031,7 +2031,7 @@ TEST_F(InstrRefLDVTest, pickVPHILocBadlyNestedLoops) {
20312031
ValueIDNum RbxPHIInBlk1(Loop1Blk, 0, RbxLoc);
20322032

20332033
DebugVariable Var(FuncVariable, None, nullptr);
2034-
DbgValueProperties EmptyProps(EmptyExpr, false);
2034+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
20352035
SmallVector<DbgValue, 32> VLiveOuts;
20362036
VLiveOuts.resize(5, DbgValue(EmptyProps, DbgValue::Undef));
20372037
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -2160,7 +2160,7 @@ TEST_F(InstrRefLDVTest, vlocJoinDiamond) {
21602160
ValueIDNum RspPHIInBlkRetBlk(RetBlk, 0, RspLoc);
21612161

21622162
DebugVariable Var(FuncVariable, None, nullptr);
2163-
DbgValueProperties EmptyProps(EmptyExpr, false);
2163+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
21642164
SmallVector<DbgValue, 32> VLiveOuts;
21652165
VLiveOuts.resize(4, DbgValue(EmptyProps, DbgValue::Undef));
21662166
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -2272,7 +2272,7 @@ TEST_F(InstrRefLDVTest, vlocJoinDiamond) {
22722272
EXPECT_EQ(JoinedLoc.BlockNo, 0);
22732273

22742274
// We shouldn't eliminate PHIs when properties disagree.
2275-
DbgValueProperties PropsWithIndirect(EmptyExpr, true);
2275+
DbgValueProperties PropsWithIndirect(EmptyExpr, true, false);
22762276
VLiveOuts[1] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
22772277
VLiveOuts[2] = DbgValue(LiveInRsp, PropsWithIndirect, DbgValue::Def);
22782278
JoinedLoc = DbgValue(3, EmptyProps, DbgValue::VPHI);
@@ -2299,7 +2299,7 @@ TEST_F(InstrRefLDVTest, vlocJoinDiamond) {
22992299
// not be eliminated.
23002300
DIExpression *NewExpr =
23012301
DIExpression::prepend(EmptyExpr, DIExpression::ApplyOffset, 4);
2302-
DbgValueProperties PropsWithExpr(NewExpr, false);
2302+
DbgValueProperties PropsWithExpr(NewExpr, false, false);
23032303
VLiveOuts[1] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
23042304
VLiveOuts[2] = DbgValue(LiveInRsp, PropsWithExpr, DbgValue::Def);
23052305
JoinedLoc = DbgValue(3, EmptyProps, DbgValue::VPHI);
@@ -2328,7 +2328,7 @@ TEST_F(InstrRefLDVTest, vlocJoinLoops) {
23282328
ValueIDNum RspPHIInBlk1(LoopBlk, 0, RspLoc);
23292329

23302330
DebugVariable Var(FuncVariable, None, nullptr);
2331-
DbgValueProperties EmptyProps(EmptyExpr, false);
2331+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
23322332
SmallVector<DbgValue, 32> VLiveOuts;
23332333
VLiveOuts.resize(3, DbgValue(EmptyProps, DbgValue::Undef));
23342334
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -2384,7 +2384,7 @@ TEST_F(InstrRefLDVTest, vlocJoinLoops) {
23842384
// properties.
23852385
DIExpression *NewExpr =
23862386
DIExpression::prepend(EmptyExpr, DIExpression::ApplyOffset, 4);
2387-
DbgValueProperties PropsWithExpr(NewExpr, false);
2387+
DbgValueProperties PropsWithExpr(NewExpr, false, false);
23882388
VLiveOuts[0] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
23892389
VLiveOuts[1] = DbgValue(1, PropsWithExpr, DbgValue::VPHI);
23902390
JoinedLoc = DbgValue(1, EmptyProps, DbgValue::VPHI);
@@ -2431,7 +2431,7 @@ TEST_F(InstrRefLDVTest, vlocJoinBadlyNestedLoops) {
24312431
ValueIDNum LiveInRbx(EntryBlk, 0, RbxLoc);
24322432

24332433
DebugVariable Var(FuncVariable, None, nullptr);
2434-
DbgValueProperties EmptyProps(EmptyExpr, false);
2434+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
24352435
SmallVector<DbgValue, 32> VLiveOuts;
24362436
VLiveOuts.resize(5, DbgValue(EmptyProps, DbgValue::Undef));
24372437
InstrRefBasedLDV::LiveIdxT VLiveOutIdx;
@@ -2477,7 +2477,7 @@ TEST_F(InstrRefLDVTest, vlocJoinBadlyNestedLoops) {
24772477
EXPECT_EQ(JoinedLoc.ID, LiveInRsp);
24782478

24792479
// They shouldn't merge if one of their properties is different.
2480-
DbgValueProperties PropsWithIndirect(EmptyExpr, true);
2480+
DbgValueProperties PropsWithIndirect(EmptyExpr, true, false);
24812481
VLiveOuts[0] = DbgValue(LiveInRsp, EmptyProps, DbgValue::Def);
24822482
VLiveOuts[1] = DbgValue(1, EmptyProps, DbgValue::VPHI);
24832483
VLiveOuts[2] = DbgValue(1, PropsWithIndirect, DbgValue::VPHI);
@@ -2516,7 +2516,7 @@ TEST_F(InstrRefLDVTest, VLocSingleBlock) {
25162516
MInLocs[0][0] = MOutLocs[0][0] = LiveInRsp;
25172517

25182518
DebugVariable Var(FuncVariable, None, nullptr);
2519-
DbgValueProperties EmptyProps(EmptyExpr, false);
2519+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
25202520

25212521
SmallSet<DebugVariable, 4> AllVars;
25222522
AllVars.insert(Var);
@@ -2576,7 +2576,7 @@ TEST_F(InstrRefLDVTest, VLocDiamondBlocks) {
25762576
initValueArray(MOutLocs, 4, 2);
25772577

25782578
DebugVariable Var(FuncVariable, None, nullptr);
2579-
DbgValueProperties EmptyProps(EmptyExpr, false);
2579+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
25802580

25812581
SmallSet<DebugVariable, 4> AllVars;
25822582
AllVars.insert(Var);
@@ -2793,7 +2793,7 @@ TEST_F(InstrRefLDVTest, VLocSimpleLoop) {
27932793
initValueArray(MOutLocs, 3, 2);
27942794

27952795
DebugVariable Var(FuncVariable, None, nullptr);
2796-
DbgValueProperties EmptyProps(EmptyExpr, false);
2796+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
27972797

27982798
SmallSet<DebugVariable, 4> AllVars;
27992799
AllVars.insert(Var);
@@ -3046,7 +3046,7 @@ TEST_F(InstrRefLDVTest, VLocNestedLoop) {
30463046
initValueArray(MOutLocs, 5, 2);
30473047

30483048
DebugVariable Var(FuncVariable, None, nullptr);
3049-
DbgValueProperties EmptyProps(EmptyExpr, false);
3049+
DbgValueProperties EmptyProps(EmptyExpr, false, false);
30503050

30513051
SmallSet<DebugVariable, 4> AllVars;
30523052
AllVars.insert(Var);

0 commit comments

Comments
 (0)