Skip to content

Commit 6965f83

Browse files
committed
[DebugInfo] Make describeLoadedValue() reg aware
Summary: Currently the describeLoadedValue() hook is assumed to describe the value of the instruction's first explicit define. The hook will not be called for instructions with more than one explicit define. This commit adds a register parameter to the describeLoadedValue() hook, and invokes the hook for all registers in the worklist. This will allow us to for example describe instructions which produce more than two parameters' values; e.g. Hexagon's various combine instructions. This also fixes situations in our downstream target where we may pass smaller parameters in the high part of a register. If such a parameter's value is produced by a larger copy instruction, we can't describe the call site value using the super-register, and we instead need to know which sub-register that should be used. This also allows us to handle cases like this: $ebx = [...] $rdi = MOVSX64rr32 $ebx $esi = MOV32rr $edi CALL64pcrel32 @call The hook will first be invoked for the MOV32rr instruction, which will say that @call's second parameter (passed in $esi) is described by $edi. As $edi is not preserved it will be added to the worklist. When we get to the MOVSX64rr32 instruction, we need to describe two values; the sign-extended value of $ebx -> $rdi for the first parameter, and $ebx -> $edi for the second parameter, which is now possible. This commit modifies the dbgcall-site-lea-interpretation.mir test case. In the test case, the values of some 32-bit parameters were produced with LEA64r. Perhaps we can in general cases handle such by emitting expressions that AND out the lower 32-bits, but I have not been able to land in a case where a LEA64r is used for a 32-bit parameter instead of LEA64_32 from C code. I have not found a case where it would be useful to describe parameters using implicit defines, so in this patch the hook is still only invoked for explicit defines of forwarding registers. Reviewers: djtodoro, NikolaPrica, aprantl, vsk Reviewed By: djtodoro, vsk Subscribers: ormris, hiraditya, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D70431
1 parent d0fb7a4 commit 6965f83

13 files changed

+770
-71
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ struct DestSourcePair {
7272
: Destination(&Dest), Source(&Src) {}
7373
};
7474

75+
/// Used to describe a register and immediate addition.
76+
struct RegImmPair {
77+
Register Reg;
78+
int64_t Imm;
79+
80+
RegImmPair(Register Reg, int64_t Imm) : Reg(Reg), Imm(Imm) {}
81+
};
82+
7583
//---------------------------------------------------------------------------
7684
///
7785
/// TargetInstrInfo - Interface to description of machine instruction set
@@ -950,11 +958,11 @@ class TargetInstrInfo : public MCInstrInfo {
950958
}
951959

952960
/// If the specific machine instruction is an instruction that adds an
953-
/// immediate value to its source operand and stores it in destination,
954-
/// return destination and source registers as machine operands along with
955-
/// \c Offset which has been added.
956-
virtual Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
957-
int64_t &Offset) const {
961+
/// immediate value and a physical register, and stores the result in
962+
/// the given physical register \c Reg, return a pair of the source
963+
/// register and the offset which has been added.
964+
virtual Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
965+
Register Reg) const {
958966
return None;
959967
}
960968

@@ -1789,9 +1797,10 @@ class TargetInstrInfo : public MCInstrInfo {
17891797
}
17901798

17911799
/// Produce the expression describing the \p MI loading a value into
1792-
/// the parameter's forwarding register.
1793-
virtual Optional<ParamLoadedValue>
1794-
describeLoadedValue(const MachineInstr &MI) const;
1800+
/// the physical register \p Reg. This hook should only be used with
1801+
/// \p MIs belonging to VReg-less functions.
1802+
virtual Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
1803+
Register Reg) const;
17951804

17961805
private:
17971806
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,6 @@ static void collectCallSiteParameters(const MachineInstr *CallMI,
595595
Implicit.push_back(FwdReg);
596596
else
597597
Explicit.push_back(FwdReg);
598-
break;
599598
}
600599
}
601600
}
@@ -640,39 +639,33 @@ static void collectCallSiteParameters(const MachineInstr *CallMI,
640639
for (auto Reg : concat<unsigned>(ExplicitFwdRegDefs, ImplicitFwdRegDefs))
641640
ForwardedRegWorklist.erase(Reg);
642641

643-
// The describeLoadedValue() hook currently does not have any information
644-
// about which register it should describe in case of multiple defines, so
645-
// for now we only handle instructions where a forwarded register is (at
646-
// least partially) defined by the instruction's single explicit define.
647-
if (I->getNumExplicitDefs() != 1 || ExplicitFwdRegDefs.empty())
648-
continue;
649-
unsigned ParamFwdReg = ExplicitFwdRegDefs[0];
650-
651-
if (auto ParamValue = TII->describeLoadedValue(*I)) {
652-
if (ParamValue->first.isImm()) {
653-
int64_t Val = ParamValue->first.getImm();
654-
DbgValueLoc DbgLocVal(ParamValue->second, Val);
655-
finishCallSiteParam(DbgLocVal, ParamFwdReg);
656-
} else if (ParamValue->first.isReg()) {
657-
Register RegLoc = ParamValue->first.getReg();
658-
// TODO: For now, there is no use of describing the value loaded into the
659-
// register that is also the source registers (e.g. $r0 = add $r0, x).
660-
if (ParamFwdReg == RegLoc)
661-
continue;
662-
663-
unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
664-
Register FP = TRI->getFrameRegister(*MF);
665-
bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP);
666-
if (TRI->isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP) {
667-
DbgValueLoc DbgLocVal(ParamValue->second,
668-
MachineLocation(RegLoc,
669-
/*IsIndirect=*/IsSPorFP));
642+
for (auto ParamFwdReg : ExplicitFwdRegDefs) {
643+
if (auto ParamValue = TII->describeLoadedValue(*I, ParamFwdReg)) {
644+
if (ParamValue->first.isImm()) {
645+
int64_t Val = ParamValue->first.getImm();
646+
DbgValueLoc DbgLocVal(ParamValue->second, Val);
670647
finishCallSiteParam(DbgLocVal, ParamFwdReg);
671-
// TODO: Add support for entry value plus an expression.
672-
} else if (ShouldTryEmitEntryVals &&
673-
ParamValue->second->getNumElements() == 0) {
674-
ForwardedRegWorklist.insert(RegLoc);
675-
RegsForEntryValues[RegLoc] = ParamFwdReg;
648+
} else if (ParamValue->first.isReg()) {
649+
Register RegLoc = ParamValue->first.getReg();
650+
// TODO: For now, there is no use of describing the value loaded into the
651+
// register that is also the source registers (e.g. $r0 = add $r0, x).
652+
if (ParamFwdReg == RegLoc)
653+
continue;
654+
655+
unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
656+
Register FP = TRI->getFrameRegister(*MF);
657+
bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP);
658+
if (TRI->isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP) {
659+
DbgValueLoc DbgLocVal(ParamValue->second,
660+
MachineLocation(RegLoc,
661+
/*IsIndirect=*/IsSPorFP));
662+
finishCallSiteParam(DbgLocVal, ParamFwdReg);
663+
// TODO: Add support for entry value plus an expression.
664+
} else if (ShouldTryEmitEntryVals &&
665+
ParamValue->second->getNumElements() == 0) {
666+
ForwardedRegWorklist.insert(RegLoc);
667+
RegsForEntryValues[RegLoc] = ParamFwdReg;
668+
}
676669
}
677670
}
678671
}

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,16 +1121,35 @@ bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
11211121
}
11221122

11231123
Optional<ParamLoadedValue>
1124-
TargetInstrInfo::describeLoadedValue(const MachineInstr &MI) const {
1124+
TargetInstrInfo::describeLoadedValue(const MachineInstr &MI,
1125+
Register Reg) const {
11251126
const MachineFunction *MF = MI.getMF();
1127+
const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
11261128
DIExpression *Expr = DIExpression::get(MF->getFunction().getContext(), {});
11271129
int64_t Offset;
11281130

1131+
// To simplify the sub-register handling, verify that we only need to
1132+
// consider physical registers.
1133+
assert(MF->getProperties().hasProperty(
1134+
MachineFunctionProperties::Property::NoVRegs));
1135+
11291136
if (auto DestSrc = isCopyInstr(MI)) {
1130-
return ParamLoadedValue(*DestSrc->Source, Expr);
1131-
} else if (auto DestSrc = isAddImmediate(MI, Offset)) {
1137+
Register DestReg = DestSrc->Destination->getReg();
1138+
1139+
if (Reg == DestReg)
1140+
return ParamLoadedValue(*DestSrc->Source, Expr);
1141+
1142+
// Cases where super- or sub-registers needs to be described should
1143+
// be handled by the target's hook implementation.
1144+
assert(!TRI->isSuperOrSubRegisterEq(Reg, DestReg) &&
1145+
"TargetInstrInfo::describeLoadedValue can't describe super- or "
1146+
"sub-regs for copy instructions");
1147+
return None;
1148+
} else if (auto RegImm = isAddImmediate(MI, Reg)) {
1149+
Register SrcReg = RegImm->Reg;
1150+
Offset = RegImm->Imm;
11321151
Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, Offset);
1133-
return ParamLoadedValue(*DestSrc->Source, Expr);
1152+
return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
11341153
} else if (MI.hasOneMemOperand()) {
11351154
// Only describe memory which provably does not escape the function. As
11361155
// described in llvm.org/PR43343, escaped memory may be clobbered by the
@@ -1145,11 +1164,15 @@ TargetInstrInfo::describeLoadedValue(const MachineInstr &MI) const {
11451164
if (!PSV || PSV->mayAlias(&MFI))
11461165
return None;
11471166

1148-
const auto &TRI = MF->getSubtarget().getRegisterInfo();
11491167
const MachineOperand *BaseOp;
11501168
if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI))
11511169
return None;
11521170

1171+
assert(MI.getNumExplicitDefs() == 1 &&
1172+
"Can currently only handle mem instructions with a single define");
1173+
1174+
// TODO: In what way do we need to take Reg into consideration here?
1175+
11531176
SmallVector<uint64_t, 8> Ops;
11541177
DIExpression::appendOffset(Ops, Offset);
11551178
Ops.push_back(dwarf::DW_OP_deref_size);

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/CodeGen/StackMaps.h"
3131
#include "llvm/CodeGen/TargetRegisterInfo.h"
3232
#include "llvm/CodeGen/TargetSubtargetInfo.h"
33+
#include "llvm/IR/DebugInfoMetadata.h"
3334
#include "llvm/IR/DebugLoc.h"
3435
#include "llvm/IR/GlobalValue.h"
3536
#include "llvm/MC/MCAsmInfo.h"
@@ -6447,10 +6448,16 @@ AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
64476448
return None;
64486449
}
64496450

6450-
Optional<DestSourcePair>
6451-
AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
6452-
int64_t &Offset) const {
6451+
Optional<RegImmPair> AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
6452+
Register Reg) const {
64536453
int Sign = 1;
6454+
int64_t Offset = 0;
6455+
6456+
// TODO: Handle cases where Reg is a super- or sub-register of the
6457+
// destination register.
6458+
if (Reg != MI.getOperand(0).getReg())
6459+
return None;
6460+
64546461
switch (MI.getOpcode()) {
64556462
default:
64566463
return None;
@@ -6474,22 +6481,73 @@ AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
64746481
Offset = Offset << Shift;
64756482
}
64766483
}
6477-
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
6484+
return RegImmPair{MI.getOperand(1).getReg(), Offset};
6485+
}
6486+
6487+
/// If the given ORR instruction is a copy, and \p DescribedReg overlaps with
6488+
/// the destination register then, if possible, describe the value in terms of
6489+
/// the source register.
6490+
static Optional<ParamLoadedValue>
6491+
describeORRLoadedValue(const MachineInstr &MI, Register DescribedReg,
6492+
const TargetInstrInfo *TII,
6493+
const TargetRegisterInfo *TRI) {
6494+
auto DestSrc = TII->isCopyInstr(MI);
6495+
if (!DestSrc)
6496+
return None;
6497+
6498+
Register DestReg = DestSrc->Destination->getReg();
6499+
Register SrcReg = DestSrc->Source->getReg();
6500+
6501+
auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
6502+
6503+
// If the described register is the destination, just return the source.
6504+
if (DestReg == DescribedReg)
6505+
return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
6506+
6507+
// ORRWrs zero-extends to 64-bits, so we need to consider such cases.
6508+
if (MI.getOpcode() == AArch64::ORRWrs &&
6509+
TRI->isSuperRegister(DestReg, DescribedReg))
6510+
return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
6511+
6512+
// We may need to describe the lower part of a ORRXrs move.
6513+
if (MI.getOpcode() == AArch64::ORRXrs &&
6514+
TRI->isSubRegister(DestReg, DescribedReg)) {
6515+
Register SrcSubReg = TRI->getSubReg(SrcReg, AArch64::sub_32);
6516+
return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr);
6517+
}
6518+
6519+
assert(!TRI->isSuperOrSubRegisterEq(DestReg, DescribedReg) &&
6520+
"Unhandled ORR[XW]rs copy case");
6521+
6522+
return None;
64786523
}
64796524

64806525
Optional<ParamLoadedValue>
6481-
AArch64InstrInfo::describeLoadedValue(const MachineInstr &MI) const {
6526+
AArch64InstrInfo::describeLoadedValue(const MachineInstr &MI,
6527+
Register Reg) const {
6528+
const MachineFunction *MF = MI.getMF();
6529+
const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
64826530
switch (MI.getOpcode()) {
64836531
case AArch64::MOVZWi:
6484-
case AArch64::MOVZXi:
6532+
case AArch64::MOVZXi: {
6533+
// MOVZWi may be used for producing zero-extended 32-bit immediates in
6534+
// 64-bit parameters, so we need to consider super-registers.
6535+
if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
6536+
return None;
6537+
64856538
if (!MI.getOperand(1).isImm())
64866539
return None;
64876540
int64_t Immediate = MI.getOperand(1).getImm();
64886541
int Shift = MI.getOperand(2).getImm();
64896542
return ParamLoadedValue(MachineOperand::CreateImm(Immediate << Shift),
64906543
nullptr);
64916544
}
6492-
return TargetInstrInfo::describeLoadedValue(MI);
6545+
case AArch64::ORRWrs:
6546+
case AArch64::ORRXrs:
6547+
return describeORRLoadedValue(MI, Reg, this, TRI);
6548+
}
6549+
6550+
return TargetInstrInfo::describeLoadedValue(MI, Reg);
64936551
}
64946552

64956553
#define GET_INSTRINFO_HELPERS

llvm/lib/Target/AArch64/AArch64InstrInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
265265
/// on Windows.
266266
static bool isSEHInstruction(const MachineInstr &MI);
267267

268-
Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
269-
int64_t &Offset) const override;
268+
Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
269+
Register Reg) const override;
270270

271-
Optional<ParamLoadedValue>
272-
describeLoadedValue(const MachineInstr &MI) const override;
271+
Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
272+
Register Reg) const override;
273273

274274
#define GET_INSTRINFO_HELPER_DECLS
275275
#include "AArch64GenInstrInfo.inc"

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,11 +5328,16 @@ ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
53285328
return makeArrayRef(TargetFlags);
53295329
}
53305330

5331-
Optional<DestSourcePair>
5332-
ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
5333-
int64_t &Offset) const {
5331+
Optional<RegImmPair> ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
5332+
Register Reg) const {
53345333
int Sign = 1;
53355334
unsigned Opcode = MI.getOpcode();
5335+
int64_t Offset = 0;
5336+
5337+
// TODO: Handle cases where Reg is a super- or sub-register of the
5338+
// destination register.
5339+
if (Reg != MI.getOperand(0).getReg())
5340+
return None;
53365341

53375342
// We describe SUBri or ADDri instructions.
53385343
if (Opcode == ARM::SUBri)
@@ -5348,7 +5353,7 @@ ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
53485353
return None;
53495354

53505355
Offset = MI.getOperand(2).getImm() * Sign;
5351-
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
5356+
return RegImmPair{MI.getOperand(1).getReg(), Offset};
53525357
}
53535358

53545359
bool llvm::registerDefinedBetween(unsigned Reg,

llvm/lib/Target/ARM/ARMBaseInstrInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
455455
return MI.getOperand(3).getReg();
456456
}
457457

458-
Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
459-
int64_t &Offset) const override;
458+
Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
459+
Register Reg) const override;
460460
};
461461

462462
/// Get the operands corresponding to the given \p Pred value. By default, the

0 commit comments

Comments
 (0)