Skip to content

Commit 0365f1a

Browse files
committed
[Statepoints 2/4] Statepoint infrastructure for garbage collection: MI & x86-64 Backend
This is the second patch in a small series. This patch contains the MachineInstruction and x86-64 backend pieces required to lower Statepoints. It does not include the code to actually generate the STATEPOINT machine instruction and as a result, the entire patch is currently dead code. I will be submitting the SelectionDAG parts within the next 24-48 hours. Since those pieces are by far the most complicated, I wanted to minimize the size of that patch. That patch will include the tests which exercise the functionality in this patch. The entire series can be seen as one combined whole in http://reviews.llvm.org/D5683. The STATEPOINT psuedo node is generated after all gc values are explicitly spilled to stack slots. The purpose of this node is to wrap an actual call instruction while recording the spill locations of the meta arguments used for garbage collection and other purposes. The STATEPOINT is modeled as modifing all of those locations to prevent backend optimizations from forwarding the value from before the STATEPOINT to after the STATEPOINT. (Doing so would break relocation semantics for collectors which wish to relocate roots.) The implementation of STATEPOINT is closely modeled on PATCHPOINT. Eventually, much of the code in this patch will be removed. The long term plan is to merge the functionality provided by statepoints and patchpoints. Merging their implementations in the backend is likely to be a good starting point. Reviewed by: atrick, ributzka llvm-svn: 223085
1 parent e206820 commit 0365f1a

14 files changed

+263
-5
lines changed

llvm/include/llvm/CodeGen/StackMaps.h

+49-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,52 @@ class PatchPointOpers {
8181
unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
8282
};
8383

84+
/// MI-level Statepoint operands
85+
///
86+
/// Statepoint operands take the form:
87+
/// <num call arguments>, <call target>, [call arguments],
88+
/// <StackMaps::ConstantOp>, <flags>,
89+
/// <StackMaps::ConstantOp>, <num other args>, [other args],
90+
/// [gc values]
91+
class StatepointOpers {
92+
private:
93+
enum {
94+
NCallArgsPos = 0,
95+
CallTargetPos = 1
96+
};
97+
98+
public:
99+
explicit StatepointOpers(const MachineInstr *MI):
100+
MI(MI) { }
101+
102+
/// Get starting index of non call related arguments
103+
/// (statepoint flags, vm state and gc state).
104+
unsigned getVarIdx() const {
105+
return MI->getOperand(NCallArgsPos).getImm() + 2;
106+
}
107+
108+
/// Returns the index of the operand containing the number of non-gc non-call
109+
/// arguments.
110+
unsigned getNumVMSArgsIdx() const {
111+
return getVarIdx() + 3;
112+
}
113+
114+
/// Returns the number of non-gc non-call arguments attached to the
115+
/// statepoint. Note that this is the number of arguments, not the number of
116+
/// operands required to represent those arguments.
117+
unsigned getNumVMSArgs() const {
118+
return MI->getOperand(getNumVMSArgsIdx()).getImm();
119+
}
120+
121+
/// Returns the target of the underlying call.
122+
const MachineOperand &getCallTarget() const {
123+
return MI->getOperand(CallTargetPos);
124+
}
125+
126+
private:
127+
const MachineInstr *MI;
128+
};
129+
84130
class StackMaps {
85131
public:
86132
struct Location {
@@ -132,14 +178,16 @@ class StackMaps {
132178
/// \brief Generate a stackmap record for a patchpoint instruction.
133179
void recordPatchPoint(const MachineInstr &MI);
134180

181+
/// \brief Generate a stackmap record for a statepoint instruction.
182+
void recordStatepoint(const MachineInstr &MI);
183+
135184
/// If there is any stack map data, create a stack map section and serialize
136185
/// the map info into it. This clears the stack map data structures
137186
/// afterwards.
138187
void serializeToStackMapSection();
139188

140189
private:
141190
static const char *WSMP;
142-
143191
typedef SmallVector<Location, 8> LocationVec;
144192
typedef SmallVector<LiveOutReg, 8> LiveOutVec;
145193
typedef MapVector<uint64_t, uint64_t> ConstantPool;

llvm/include/llvm/Target/Target.td

+9
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,15 @@ def PATCHPOINT : Instruction {
845845
let mayLoad = 1;
846846
let usesCustomInserter = 1;
847847
}
848+
def STATEPOINT : Instruction {
849+
let OutOperandList = (outs);
850+
let InOperandList = (ins variable_ops);
851+
let usesCustomInserter = 1;
852+
let mayLoad = 1;
853+
let mayStore = 1;
854+
let hasSideEffects = 1;
855+
let isCall = 1;
856+
}
848857
def LOAD_STACK_GUARD : Instruction {
849858
let OutOperandList = (outs ptr_rc:$dst);
850859
let InOperandList = (ins);

llvm/include/llvm/Target/TargetFrameLowering.h

+10
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ class TargetFrameLowering {
199199
virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
200200
unsigned &FrameReg) const;
201201

202+
/// Same as above, except that the 'base register' will always be RSP, not
203+
/// RBP on x86. This is used exclusively for lowering STATEPOINT nodes.
204+
/// TODO: This should really be a parameterizable choice.
205+
virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
206+
unsigned &FrameReg) const {
207+
// default to calling normal version, we override this on x86 only
208+
llvm_unreachable("unimplemented for non-x86");
209+
return 0;
210+
}
211+
202212
/// processFunctionBeforeCalleeSavedScan - This method is called immediately
203213
/// before PrologEpilogInserter scans the physical registers used to determine
204214
/// what callee saved registers should be spilled. This method is optional.

llvm/include/llvm/Target/TargetOpcodes.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ enum {
110110
/// to prevent the stack guard value or address from being spilled to the
111111
/// stack should override TargetLowering::emitLoadStackGuardNode and
112112
/// additionally expand this pseudo after register allocation.
113-
LOAD_STACK_GUARD = 19
113+
LOAD_STACK_GUARD = 19,
114+
115+
/// Call instruction with associated vm state for deoptimization and list
116+
/// of live pointers for relocation by the garbage collector. It is
117+
/// intended to support garbage collection with fully precise relocating
118+
/// collectors and deoptimizations in either the callee or caller.
119+
STATEPOINT = 20
114120
};
115121
} // end namespace TargetOpcode
116122
} // end namespace llvm

llvm/lib/CodeGen/InlineSpiller.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,8 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
10881088
bool WasCopy = MI->isCopy();
10891089
unsigned ImpReg = 0;
10901090

1091-
bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1091+
bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::STATEPOINT ||
1092+
MI->getOpcode() == TargetOpcode::PATCHPOINT ||
10921093
MI->getOpcode() == TargetOpcode::STACKMAP);
10931094

10941095
// TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied

llvm/lib/CodeGen/LocalStackSlotAllocation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
291291
// Debug value, stackmap and patchpoint instructions can't be out of
292292
// range, so they don't need any updates.
293293
if (MI->isDebugValue() ||
294+
MI->getOpcode() == TargetOpcode::STATEPOINT ||
294295
MI->getOpcode() == TargetOpcode::STACKMAP ||
295296
MI->getOpcode() == TargetOpcode::PATCHPOINT)
296297
continue;

llvm/lib/CodeGen/PrologEpilogInserter.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,26 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
797797
continue;
798798
}
799799

800+
// TODO: This code should be commoned with the code for
801+
// PATCHPOINT. There's no good reason for the difference in
802+
// implementation other than historical accident. The only
803+
// remaining difference is the unconditional use of the stack
804+
// pointer as the base register.
805+
if (MI->getOpcode() == TargetOpcode::STATEPOINT) {
806+
assert((!MI->isDebugValue() || i == 0) &&
807+
"Frame indicies can only appear as the first operand of a "
808+
"DBG_VALUE machine instruction");
809+
unsigned Reg;
810+
MachineOperand &Offset = MI->getOperand(i + 1);
811+
const unsigned refOffset =
812+
TFI->getFrameIndexReferenceFromSP(Fn, MI->getOperand(i).getIndex(),
813+
Reg);
814+
815+
Offset.setImm(Offset.getImm() + refOffset);
816+
MI->getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
817+
continue;
818+
}
819+
800820
// Some instructions (e.g. inline asm instructions) can have
801821
// multiple frame indices and/or cause eliminateFrameIndex
802822
// to insert more than one instruction. We need the register

llvm/lib/CodeGen/StackMaps.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ void StackMaps::recordPatchPoint(const MachineInstr &MI) {
286286
}
287287
#endif
288288
}
289+
void StackMaps::recordStatepoint(const MachineInstr &MI) {
290+
assert(MI.getOpcode() == TargetOpcode::STATEPOINT &&
291+
"expected statepoint");
292+
293+
StatepointOpers opers(&MI);
294+
// Record all the deopt and gc operands (they're contiguous and run from the
295+
// initial index to the end of the operand list)
296+
const unsigned StartIdx = opers.getVarIdx();
297+
recordStackMapOpers(MI, 0xABCDEF00,
298+
MI.operands_begin() + StartIdx, MI.operands_end(),
299+
false);
300+
}
289301

290302
/// Emit the stackmap header.
291303
///

llvm/lib/CodeGen/TargetLoweringBase.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,14 @@ TargetLoweringBase::emitPatchPoint(MachineInstr *MI,
992992
// Add a new memory operand for this FI.
993993
const MachineFrameInfo &MFI = *MF.getFrameInfo();
994994
assert(MFI.getObjectOffset(FI) != -1);
995+
996+
unsigned Flags = MachineMemOperand::MOLoad;
997+
if (MI->getOpcode() == TargetOpcode::STATEPOINT) {
998+
Flags |= MachineMemOperand::MOStore;
999+
Flags |= MachineMemOperand::MOVolatile;
1000+
}
9951001
MachineMemOperand *MMO = MF.getMachineMemOperand(
996-
MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
1002+
MachinePointerInfo::getFixedStack(FI), Flags,
9971003
TM.getSubtargetImpl()->getDataLayout()->getPointerSize(),
9981004
MFI.getObjectAlignment(FI));
9991005
MIB->addMemOperand(MF, MMO);

llvm/lib/Target/X86/X86FrameLowering.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,79 @@ int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
11351135
return getFrameIndexOffset(MF, FI);
11361136
}
11371137

1138+
// Simplified from getFrameIndexOffset keeping only StackPointer cases
1139+
int X86FrameLowering::getFrameIndexOffsetFromSP(const MachineFunction &MF, int FI) const {
1140+
const X86RegisterInfo *RegInfo =
1141+
static_cast<const X86RegisterInfo*>(MF.getSubtarget().getRegisterInfo());
1142+
const MachineFrameInfo *MFI = MF.getFrameInfo();
1143+
const uint64_t StackSize = MFI->getStackSize(); //not including dynamic realign
1144+
1145+
{
1146+
#ifndef NDEBUG
1147+
// Note: LLVM arranges the stack as:
1148+
// Args > Saved RetPC (<--FP) > CSRs > dynamic alignment (<--BP)
1149+
// > "Stack Slots" (<--SP)
1150+
// We can always address StackSlots from RSP. We can usually (unless
1151+
// needsStackRealignment) address CSRs from RSP, but sometimes need to
1152+
// address them from RBP. FixedObjects can be placed anywhere in the stack
1153+
// frame depending on their specific requirements (i.e. we can actually
1154+
// refer to arguments to the function which are stored in the *callers*
1155+
// frame). As a result, THE RESULT OF THIS CALL IS MEANINGLESS FOR CSRs
1156+
// AND FixedObjects IFF needsStackRealignment or hasVarSizedObject.
1157+
1158+
assert(!RegInfo->hasBasePointer(MF) && "we don't handle this case");
1159+
1160+
// We don't handle tail calls, and shouldn't be seeing them
1161+
// either.
1162+
int TailCallReturnAddrDelta =
1163+
MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta();
1164+
assert(!(TailCallReturnAddrDelta < 0) && "we don't handle this case!");
1165+
#endif
1166+
}
1167+
1168+
// This is how the math works out:
1169+
//
1170+
// %rsp grows (i.e. gets lower) left to right. Each box below is
1171+
// one word (eight bytes). Obj0 is the stack slot we're trying to
1172+
// get to.
1173+
//
1174+
// ----------------------------------
1175+
// | BP | Obj0 | Obj1 | ... | ObjN |
1176+
// ----------------------------------
1177+
// ^ ^ ^ ^
1178+
// A B C E
1179+
//
1180+
// A is the incoming stack pointer.
1181+
// (B - A) is the local area offset (-8 for x86-64) [1]
1182+
// (C - A) is the Offset returned by MFI->getObjectOffset for Obj0 [2]
1183+
//
1184+
// |(E - B)| is the StackSize (absolute value, positive). For a
1185+
// stack that grown down, this works out to be (B - E). [3]
1186+
//
1187+
// E is also the value of %rsp after stack has been set up, and we
1188+
// want (C - E) -- the value we can add to %rsp to get to Obj0. Now
1189+
// (C - E) == (C - A) - (B - A) + (B - E)
1190+
// { Using [1], [2] and [3] above }
1191+
// == getObjectOffset - LocalAreaOffset + StackSize
1192+
//
1193+
1194+
// Get the Offset from the StackPointer
1195+
int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea();
1196+
1197+
return Offset + StackSize;
1198+
}
1199+
// Simplified from getFrameIndexReference keeping only StackPointer cases
1200+
int X86FrameLowering::getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
1201+
unsigned &FrameReg) const {
1202+
const X86RegisterInfo *RegInfo =
1203+
static_cast<const X86RegisterInfo*>(MF.getSubtarget().getRegisterInfo());
1204+
1205+
assert(!RegInfo->hasBasePointer(MF) && "we don't handle this case");
1206+
1207+
FrameReg = RegInfo->getStackRegister();
1208+
return getFrameIndexOffsetFromSP(MF, FI);
1209+
}
1210+
11381211
bool X86FrameLowering::assignCalleeSavedSpillSlots(
11391212
MachineFunction &MF, const TargetRegisterInfo *TRI,
11401213
std::vector<CalleeSavedInfo> &CSI) const {

llvm/lib/Target/X86/X86FrameLowering.h

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class X86FrameLowering : public TargetFrameLowering {
6969
int getFrameIndexReference(const MachineFunction &MF, int FI,
7070
unsigned &FrameReg) const override;
7171

72+
int getFrameIndexOffsetFromSP(const MachineFunction &MF, int FI) const;
73+
int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
74+
unsigned &FrameReg) const override;
75+
7276
void eliminateCallFramePseudoInstr(MachineFunction &MF,
7377
MachineBasicBlock &MBB,
7478
MachineBasicBlock::iterator MI) const override;

llvm/lib/Target/X86/X86ISelLowering.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -21346,6 +21346,11 @@ X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2134621346
case X86::EH_SjLj_LongJmp64:
2134721347
return emitEHSjLjLongJmp(MI, BB);
2134821348

21349+
case TargetOpcode::STATEPOINT:
21350+
// As an implementation detail, STATEPOINT shares the STACKMAP format at
21351+
// this point in the process. We diverge later.
21352+
return emitPatchPoint(MI, BB);
21353+
2134921354
case TargetOpcode::STACKMAP:
2135021355
case TargetOpcode::PATCHPOINT:
2135121356
return emitPatchPoint(MI, BB);

llvm/lib/Target/X86/X86MCInstLower.cpp

+63-1
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,66 @@ static void EmitNops(MCStreamer &OS, unsigned NumBytes, bool Is64Bit, const MCSu
808808
} // while (NumBytes)
809809
}
810810

811+
static void LowerSTATEPOINT(MCStreamer &OS, StackMaps &SM,
812+
const MachineInstr &MI, bool Is64Bit,
813+
const TargetMachine& TM,
814+
const MCSubtargetInfo& STI,
815+
X86MCInstLower &MCInstLowering) {
816+
assert(Is64Bit && "Statepoint currently only supports X86-64");
817+
818+
// We need to record the frame size for stack walking
819+
const MachineFunction* MF = MI.getParent()->getParent();
820+
assert(MF && "can't find machine function?");
821+
822+
//
823+
// Emit call instruction
824+
//
825+
826+
// Lower call target and choose correct opcode
827+
const MachineOperand &call_target = StatepointOpers(&MI).getCallTarget();
828+
MCOperand call_target_mcop;
829+
unsigned call_opcode;
830+
switch (call_target.getType()) {
831+
case MachineOperand::MO_GlobalAddress:
832+
case MachineOperand::MO_ExternalSymbol:
833+
call_target_mcop = MCInstLowering.LowerSymbolOperand(
834+
call_target,
835+
MCInstLowering.GetSymbolFromOperand(call_target));
836+
call_opcode = X86::CALL64pcrel32;
837+
// Currently, we only support relative addressing with statepoints.
838+
// Otherwise, we'll need a scratch register to hold the target
839+
// address. You'll fail asserts during load & relocation if this
840+
// symbol is to far away. (TODO: support non-relative addressing)
841+
break;
842+
case MachineOperand::MO_Immediate:
843+
call_target_mcop = MCOperand::CreateImm(call_target.getImm());
844+
call_opcode = X86::CALL64pcrel32;
845+
// Currently, we only support relative addressing with statepoints.
846+
// Otherwise, we'll need a scratch register to hold the target
847+
// immediate. You'll fail asserts during load & relocation if this
848+
// address is to far away. (TODO: support non-relative addressing)
849+
break;
850+
case MachineOperand::MO_Register:
851+
call_target_mcop = MCOperand::CreateReg(call_target.getReg());
852+
call_opcode = X86::CALL64r;
853+
break;
854+
default:
855+
llvm_unreachable("Unsupported operand type in statepoint call target");
856+
break;
857+
}
858+
859+
// Emit call
860+
MCInst call_inst;
861+
call_inst.setOpcode(call_opcode);
862+
call_inst.addOperand(call_target_mcop);
863+
OS.EmitInstruction(call_inst, STI);
864+
865+
// Record our statepoint node in the same section used by STACKMAP
866+
// and PATCHPOINT
867+
SM.recordStatepoint(MI);
868+
}
869+
870+
811871
// Lower a stackmap of the form:
812872
// <id>, <shadowBytes>, ...
813873
void X86AsmPrinter::LowerSTACKMAP(const MachineInstr &MI) {
@@ -1030,7 +1090,9 @@ void X86AsmPrinter::EmitInstruction(const MachineInstr *MI) {
10301090
.addExpr(DotExpr));
10311091
return;
10321092
}
1033-
1093+
case TargetOpcode::STATEPOINT:
1094+
return LowerSTATEPOINT(OutStreamer, SM, *MI, Subtarget->is64Bit(), TM,
1095+
getSubtargetInfo(), MCInstLowering);
10341096
case TargetOpcode::STACKMAP:
10351097
return LowerSTACKMAP(*MI);
10361098

llvm/utils/TableGen/CodeGenTarget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ void CodeGenTarget::ComputeInstrsByEnum() const {
300300
"IMPLICIT_DEF", "SUBREG_TO_REG", "COPY_TO_REGCLASS", "DBG_VALUE",
301301
"REG_SEQUENCE", "COPY", "BUNDLE", "LIFETIME_START",
302302
"LIFETIME_END", "STACKMAP", "PATCHPOINT", "LOAD_STACK_GUARD",
303+
"STATEPOINT",
303304
nullptr};
304305
const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions();
305306
for (const char *const *p = FixedInstrs; *p; ++p) {

0 commit comments

Comments
 (0)