Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 4a37681

Browse files
committed
Record Wasm specific frame pointer.
1 parent c144c6d commit 4a37681

10 files changed

+85
-6
lines changed

include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ struct RegClassWeight {
211211
unsigned WeightLimit;
212212
};
213213

214+
struct FrameBaseLocation {
215+
enum LocationKind {
216+
Register,
217+
CFA,
218+
TargetIndex
219+
} Kind;
220+
struct TargetIndexInfo {
221+
unsigned Index;
222+
signed Offset;
223+
};
224+
union {
225+
unsigned Reg;
226+
TargetIndexInfo TI;
227+
};
228+
};
229+
214230
/// TargetRegisterInfo base class - We assume that the target defines a static
215231
/// array of TargetRegisterDesc objects that represent all of the machine
216232
/// registers that the target has. As such, we simply have to track a pointer
@@ -988,6 +1004,13 @@ class TargetRegisterInfo : public MCRegisterInfo {
9881004
/// for values allocated in the current stack frame.
9891005
virtual unsigned getFrameRegister(const MachineFunction &MF) const = 0;
9901006

1007+
virtual FrameBaseLocation getFrameBaseLocation(const MachineFunction &MF) const {
1008+
FrameBaseLocation Loc;
1009+
Loc.Kind = FrameBaseLocation::Register;
1010+
Loc.Reg = getFrameRegister(MF);
1011+
return Loc;
1012+
}
1013+
9911014
/// Mark a register and all its aliases as reserved in the given set.
9921015
void markSuperRegs(BitVector &RegisterSet, unsigned Reg) const;
9931016

lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,24 @@ DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
340340

341341
// Only include DW_AT_frame_base in full debug info
342342
if (!includeMinimalInlineScopes()) {
343-
if (Asm->MF->getTarget().getTargetTriple().isNVPTX()) {
343+
const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
344+
auto FBL = RI->getFrameBaseLocation(*Asm->MF);
345+
if (FBL.Kind == FrameBaseLocation::CFA) {
344346
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
345347
addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa);
346348
addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
349+
} else if (FBL.Kind == FrameBaseLocation::TargetIndex) {
350+
if (FBL.TI.Offset >= 0) {
351+
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
352+
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
353+
DIExpressionCursor Cursor({});
354+
DwarfExpr.addTargetIndexLocation(FBL.TI.Index, FBL.TI.Offset);
355+
DwarfExpr.addExpression(std::move(Cursor));
356+
addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize());
357+
}
347358
} else {
348-
const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
349-
MachineLocation Location(RI->getFrameRegister(*Asm->MF));
359+
assert(FBL.Kind == FrameBaseLocation::Register);
360+
MachineLocation Location(FBL.Reg);
350361
if (RI->isPhysicalRegister(Location.getReg()))
351362
addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
352363
}
@@ -1023,7 +1034,7 @@ void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
10231034

10241035
/// Add an address attribute to a die based on the location provided.
10251036
void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
1026-
const MachineLocation &Location) {
1037+
const MachineLocation &Location) {
10271038
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
10281039
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
10291040
if (Location.isIndirect())

lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,11 @@ static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
10601060
MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
10611061
return DebugLocEntry::Value(Expr, MLoc);
10621062
}
1063+
if (MI->getOperand(0).isTargetIndex()) {
1064+
auto Op = MI->getOperand(0);
1065+
return DebugLocEntry::Value(
1066+
Expr, TargetIndexLocation(Op.getIndex(), Op.getOffset()));
1067+
}
10631068
if (MI->getOperand(0).isImm())
10641069
return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
10651070
if (MI->getOperand(0).isFPImm())
@@ -1899,6 +1904,9 @@ static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
18991904
} else if (Value.isConstantFP()) {
19001905
APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
19011906
DwarfExpr.addUnsignedConstant(RawBytes);
1907+
} else if (Value.isTargetIndexLocation()) {
1908+
TargetIndexLocation Loc = Value.getTargetIndexLocation();
1909+
DwarfExpr.addTargetIndexLocation(Loc.Index, Loc.Offset);
19021910
}
19031911
DwarfExpr.addExpression(std::move(ExprCursor));
19041912
}

lib/Target/NVPTX/NVPTXRegisterInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class NVPTXRegisterInfo : public NVPTXGenRegisterInfo {
4545

4646
unsigned getFrameRegister(const MachineFunction &MF) const override;
4747

48+
FrameBaseLocation getFrameBaseLocation(const MachineFunction &MF) const override {
49+
FrameBaseLocation Loc;
50+
Loc.Kind = FrameBaseLocation::CFA;
51+
return Loc;
52+
}
53+
4854
ManagedStringPool *getStrPool() const {
4955
return const_cast<ManagedStringPool *>(&ManagedStrPool);
5056
}

lib/Target/WebAssembly/WebAssembly.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ namespace WebAssembly {
8686
enum TargetIndex { TI_LOCAL_START, TI_GLOBAL_START, TI_OPERAND_STACK_START };
8787
} // end namespace WebAssembly
8888

89+
namespace WebAssembly {
90+
enum TargetIndex { TI_LOCAL_START, TI_GLOBAL_START, TI_OPERAND_STACK_START };
91+
} // end namespace WebAssembly
92+
8993
} // end namespace llvm
9094

9195
#endif

lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
430430
Changed = true;
431431
}
432432

433+
{
434+
auto RL = Reg2Local.find(MFI.SPVReg);
435+
if (RL != Reg2Local.end()) {
436+
MFI.SPLocal = RL->second;
437+
}
438+
}
439+
433440
#ifndef NDEBUG
434441
// Assert that all registers have been stackified at this point.
435442
for (const MachineBasicBlock &MBB : MF) {

lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
5353
unsigned BasePtrVreg = -1U;
5454

5555
public:
56-
explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
56+
explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF), SPVReg(WebAssembly::NoRegister) {}
5757
~WebAssemblyFunctionInfo() override;
5858

5959
void addParam(MVT VT) { Params.push_back(VT); }
@@ -118,6 +118,9 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
118118
assert(Reg & INT32_MIN);
119119
return Reg & INT32_MAX;
120120
}
121+
122+
unsigned SPVReg;
123+
unsigned SPLocal;
121124
};
122125

123126
void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,

lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ WebAssemblyRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
138138
return Regs[TFI->hasFP(MF)][TT.isArch64Bit()];
139139
}
140140

141+
FrameBaseLocation
142+
WebAssemblyRegisterInfo::getFrameBaseLocation(const MachineFunction &MF) const {
143+
const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
144+
FrameBaseLocation Loc;
145+
Loc.Kind = FrameBaseLocation::TargetIndex;
146+
signed Local = MFI.SPVReg != WebAssembly::NoRegister ? MFI.SPLocal : -1;
147+
Loc.TI = {0, Local};
148+
return Loc;
149+
}
150+
141151
const TargetRegisterClass *
142152
WebAssemblyRegisterInfo::getPointerRegClass(const MachineFunction &MF,
143153
unsigned Kind) const {

lib/Target/WebAssembly/WebAssemblyRegisterInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class WebAssemblyRegisterInfo final : public WebAssemblyGenRegisterInfo {
4242
// Debug information queries.
4343
unsigned getFrameRegister(const MachineFunction &MF) const override;
4444

45+
FrameBaseLocation getFrameBaseLocation(const MachineFunction &MF) const override;
46+
4547
const TargetRegisterClass *
4648
getPointerRegClass(const MachineFunction &MF,
4749
unsigned Kind = 0) const override;

lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
8989
for (auto I = MRI.reg_begin(PReg), E = MRI.reg_end(); I != E;) {
9090
MachineOperand &MO = *I++;
9191
if (!MO.isImplicit()) {
92-
if (VReg == WebAssembly::NoRegister)
92+
if (VReg == WebAssembly::NoRegister) {
9393
VReg = MRI.createVirtualRegister(RC);
94+
if (PReg == WebAssembly::SP32) {
95+
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
96+
MFI.SPVReg = VReg;
97+
}
98+
}
9499
MO.setReg(VReg);
95100
if (MO.getParent()->isDebugValue())
96101
MO.setIsDebug();

0 commit comments

Comments
 (0)