40#define DEBUG_TYPE "stackmaps"
44 cl::desc(
"Specify the stackmap encoding version (default = 3)"));
46const char *StackMaps::WSMP =
"Stack Maps: ";
50 MI.getOperand(
Idx).getImm() == StackMaps::ConstantOp);
51 const auto &MO =
MI.getOperand(
Idx + 1);
59 "invalid stackmap definition");
63 :
MI(
MI), HasDef(
MI->getOperand(0).
isReg() &&
MI->getOperand(0).isDef() &&
64 !
MI->getOperand(0).isImplicit()) {
66 unsigned CheckStartIdx = 0, e =
MI->getNumOperands();
67 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).
isReg() &&
68 MI->getOperand(CheckStartIdx).isDef() &&
69 !
MI->getOperand(CheckStartIdx).isImplicit())
72 assert(getMetaIdx() == CheckStartIdx &&
73 "Unexpected additional definition in Patchpoint intrinsic.");
83 while (ScratchIdx < e &&
90 assert(ScratchIdx != e &&
"No scratch register available");
119 while (NumDeoptArgs--) {
131 assert(NumGCPtrsIdx < MI->getNumOperands());
132 return (
int)NumGCPtrsIdx;
140 for (
unsigned N = 0;
N < GCMapSize; ++
N) {
143 GCMap.push_back(std::make_pair(
B,
D));
150 unsigned FoldableAreaStart =
getVarIdx();
152 if (MO.getOperandNo() >= FoldableAreaStart)
154 if (MO.isReg() && MO.getReg() == Reg)
161 if (
MI->getOpcode() != TargetOpcode::STATEPOINT)
172 assert(CurIdx < MI->getNumOperands() &&
"Bad meta arg index");
173 const auto &MO =
MI->getOperand(CurIdx);
175 switch (MO.getImm()) {
178 case StackMaps::DirectMemRefOp:
181 case StackMaps::IndirectMemRefOp:
184 case StackMaps::ConstantOp:
190 assert(CurIdx < MI->getNumOperands() &&
"points past operand list");
198 RegNum =
TRI->getDwarfRegNum(SR,
false);
203 assert(RegNum >= 0 && isUInt<16>(RegNum) &&
"Invalid Dwarf register number.");
204 return (
unsigned)RegNum;
210 LiveOutVec &LiveOuts) {
216 case StackMaps::DirectMemRefOp: {
219 unsigned Size =
DL.getPointerSizeInBits();
220 assert((
Size % 8) == 0 &&
"Need pointer size in bytes.");
223 int64_t
Imm = (++MOI)->getImm();
228 case StackMaps::IndirectMemRefOp: {
229 int64_t
Size = (++MOI)->getImm();
230 assert(
Size > 0 &&
"Need a valid size for indirect memory locations.");
232 int64_t
Imm = (++MOI)->getImm();
237 case StackMaps::ConstantOp: {
239 assert(MOI->
isImm() &&
"Expected constant operand.");
241 if (isInt<32>(Imm)) {
252 "empty and tombstone keys should fit in 32 bits!");
253 auto Result = ConstPool.
insert(std::make_pair(Imm, Imm));
279 "Virtreg operands should have been rewritten before now.");
285 unsigned LLVMRegNum = *
TRI->getLLVMRegNum(DwarfRegNum,
false);
286 unsigned SubRegIdx =
TRI->getSubRegIndex(LLVMRegNum, MOI->
getReg());
288 Offset =
TRI->getSubRegIdxOffset(SubRegIdx);
304 OS << WSMP <<
"callsites:\n";
305 for (
const auto &CSI : CSInfos) {
309 OS << WSMP <<
"callsite " << CSI.ID <<
"\n";
310 OS << WSMP <<
" has " << CSLocs.size() <<
" locations\n";
313 for (
const auto &Loc : CSLocs) {
314 OS << WSMP <<
"\t\tLoc " <<
Idx <<
": ";
317 OS <<
"<Unprocessed operand>";
333 OS <<
" + " << Loc.Offset;
341 OS <<
"+" << Loc.Offset;
344 OS <<
"Constant " << Loc.Offset;
347 OS <<
"Constant Index " << Loc.Offset;
350 OS <<
"\t[encoding: .byte " << Loc.Type <<
", .byte 0"
351 <<
", .short " << Loc.Size <<
", .short " << Loc.Reg <<
", .short 0"
352 <<
", .int " << Loc.Offset <<
"]\n";
356 OS << WSMP <<
"\thas " << LiveOuts.size() <<
" live-out registers\n";
359 for (
const auto &LO : LiveOuts) {
360 OS << WSMP <<
"\t\tLO " <<
Idx <<
": ";
365 OS <<
"\t[encoding: .short " <<
LO.DwarfRegNum <<
", .byte 0, .byte "
376 unsigned Size =
TRI->getSpillSize(*
TRI->getMinimalPhysRegClass(Reg));
377 return LiveOutReg(Reg, DwarfRegNum,
Size);
383StackMaps::parseRegisterLiveOutMask(
const uint32_t *Mask)
const {
384 assert(Mask &&
"No register mask specified");
389 for (
unsigned Reg = 0, NumRegs =
TRI->getNumRegs(); Reg != NumRegs; ++Reg)
390 if ((Mask[Reg / 32] >> (Reg % 32)) & 1)
397 llvm::sort(LiveOuts, [](
const LiveOutReg &LHS,
const LiveOutReg &RHS) {
399 return LHS.DwarfRegNum <
RHS.DwarfRegNum;
402 for (
auto I = LiveOuts.begin(), E = LiveOuts.end();
I != E; ++
I) {
403 for (
auto *
II = std::next(
I);
II != E; ++
II) {
404 if (
I->DwarfRegNum !=
II->DwarfRegNum) {
409 I->Size = std::max(
I->Size,
II->Size);
410 if (
I->Reg &&
TRI->isSuperRegister(
I->Reg,
II->Reg))
426 LocationVec &Locations,
427 LiveOutVec &LiveOuts) {
430 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
431 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
432 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
435 unsigned NumDeoptArgs =
Locations.back().Offset;
437 assert(NumDeoptArgs == SO.getNumDeoptArgs());
439 while (NumDeoptArgs--)
440 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
446 unsigned NumGCPointers = MOI->
getImm();
451 unsigned GCPtrIdx = (
unsigned)SO.getFirstGCPtrIdx();
452 assert((
int)GCPtrIdx != -1);
453 assert(MOI -
MI.operands_begin() == GCPtrIdx + 0LL);
454 while (NumGCPointers--) {
460 unsigned NumGCPairs = SO.getGCPointerMap(GCPairs);
464 auto MOB =
MI.operands_begin();
465 for (
auto &
P : GCPairs) {
466 assert(
P.first < GCPtrIndices.
size() &&
"base pointer index not found");
468 "derived pointer index not found");
469 unsigned BaseIdx = GCPtrIndices[
P.first];
470 unsigned DerivedIdx = GCPtrIndices[
P.second];
471 LLVM_DEBUG(
dbgs() <<
"Base : " << BaseIdx <<
" Derived : " << DerivedIdx
473 (void)parseOperand(MOB + BaseIdx, MOE, Locations, LiveOuts);
474 (void)parseOperand(MOB + DerivedIdx, MOE, Locations, LiveOuts);
477 MOI = MOB + GCPtrIdx;
484 unsigned NumAllocas = MOI->
getImm();
486 while (NumAllocas--) {
487 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
492void StackMaps::recordStackMapOpers(
const MCSymbol &MILabel,
504 parseOperand(
MI.operands_begin(), std::next(
MI.operands_begin()), Locations,
509 if (
MI.getOpcode() == TargetOpcode::STATEPOINT)
510 parseStatepointOpers(
MI, MOI, MOE, Locations, LiveOuts);
513 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
521 CSInfos.emplace_back(CSOffsetExpr,
ID, std::move(Locations),
522 std::move(LiveOuts));
527 bool HasDynamicFrameSize =
532 if (CurrentIt != FnInfos.
end())
533 CurrentIt->second.RecordCount++;
539 assert(
MI.getOpcode() == TargetOpcode::STACKMAP &&
"expected stackmap");
543 recordStackMapOpers(L,
MI,
ID, std::next(
MI.operands_begin(),
549 assert(
MI.getOpcode() == TargetOpcode::PATCHPOINT &&
"expected patchpoint");
552 const int64_t
ID = opers.
getID();
554 recordStackMapOpers(L,
MI,
ID, MOI,
MI.operands_end(),
559 auto &Locations = CSInfos.back().Locations;
562 for (
unsigned i = 0, e = (opers.
hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
564 "anyreg arg must be in reg.");
570 assert(
MI.getOpcode() == TargetOpcode::STATEPOINT &&
"expected statepoint");
573 const unsigned StartIdx = opers.
getVarIdx();
574 recordStackMapOpers(L,
MI, opers.
getID(),
MI.operands_begin() + StartIdx,
575 MI.operands_end(),
false);
591 OS.emitIntValue(0, 1);
596 OS.emitInt32(FnInfos.
size());
599 OS.emitInt32(ConstPool.
size());
601 LLVM_DEBUG(
dbgs() << WSMP <<
"#callsites = " << CSInfos.size() <<
'\n');
602 OS.emitInt32(CSInfos.size());
612void StackMaps::emitFunctionFrameRecords(
MCStreamer &
OS) {
615 for (
auto const &FR : FnInfos) {
617 <<
" frame size: " << FR.second.StackSize
618 <<
" callsite count: " << FR.second.RecordCount <<
'\n');
619 OS.emitSymbolValue(FR.first, 8);
620 OS.emitIntValue(FR.second.StackSize, 8);
621 OS.emitIntValue(FR.second.RecordCount, 8);
628void StackMaps::emitConstantPoolEntries(
MCStreamer &
OS) {
631 for (
const auto &ConstEntry : ConstPool) {
633 OS.emitIntValue(ConstEntry.second, 8);
669 for (
const auto &CSI : CSInfos) {
677 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
679 OS.emitValue(CSI.CSOffsetExpr, 4);
688 OS.emitIntValue(CSI.ID, 8);
689 OS.emitValue(CSI.CSOffsetExpr, 4);
693 OS.emitInt16(CSLocs.size());
695 for (
const auto &Loc : CSLocs) {
696 OS.emitIntValue(Loc.Type, 1);
697 OS.emitIntValue(0, 1);
698 OS.emitInt16(Loc.Size);
699 OS.emitInt16(Loc.Reg);
701 OS.emitInt32(Loc.Offset);
705 OS.emitValueToAlignment(
Align(8));
709 OS.emitInt16(LiveOuts.size());
711 for (
const auto &LO : LiveOuts) {
712 OS.emitInt16(
LO.DwarfRegNum);
713 OS.emitIntValue(0, 1);
714 OS.emitIntValue(
LO.Size, 1);
717 OS.emitValueToAlignment(
Align(8));
725 assert((!CSInfos.empty() || ConstPool.empty()) &&
726 "Expected empty constant pool too!");
727 assert((!CSInfos.empty() || FnInfos.empty()) &&
728 "Expected empty function record too!");
738 OS.switchSection(StackMapSection);
745 emitStackmapHeader(
OS);
746 emitFunctionFrameRecords(
OS);
747 emitConstantPoolEntries(
OS);
748 emitCallsiteEntries(
OS);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines DenseMapInfo traits for DenseMap.
unsigned const TargetRegisterInfo * TRI
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static uint64_t getConstMetaVal(const MachineInstr &MI, unsigned Idx)
static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
static cl::opt< int > StackMapVersion("stackmap-version", cl::init(3), cl::Hidden, cl::desc("Specify the stackmap encoding version (default = 3)"))
This class is intended to be used as a driving class for all asm writers.
MachineFunction * MF
The current machine function.
MCSymbol * CurrentFnSym
The symbol for the current function.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Context object for machine code objects.
const MCObjectFileInfo * getObjectFileInfo() const
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Base class for the full range of assembler expressions which are needed for parsing.
MCSection * getStackMapSection() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Streaming machine code generation interface.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Representation of each machine instruction.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
unsigned getNumOperands() const
Retuns the total number of operands.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const uint32_t * getRegLiveOut() const
getRegLiveOut - Returns a bit mask of live-out registers.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isRegLiveOut() const
isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
iterator find(const KeyT &Key)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
MI-level patchpoint operands.
uint32_t getNumCallArgs() const
Return the number of call arguments.
PatchPointOpers(const MachineInstr *MI)
unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
uint64_t getID() const
Return the ID for the given patchpoint.
unsigned getStackMapStartIdx() const
Get the index at which stack map locations will be recorded.
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Wrapper class representing virtual and physical registers.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
MI-level stackmap operands.
StackMapOpers(const MachineInstr *MI)
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
static unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx)
Get index of next meta operand.
StackMaps(AsmPrinter &AP)
void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it.
SmallVector< LiveOutReg, 8 > LiveOutVec
SmallVector< Location, 8 > LocationVec
void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
MI-level Statepoint operands.
unsigned getGCPointerMap(SmallVectorImpl< std::pair< unsigned, unsigned > > &GCMap)
Get vector of base/derived pairs from statepoint.
unsigned getNumAllocaIdx()
Get index of number of gc allocas.
unsigned getNumGcMapEntriesIdx()
Get index of number of gc map entries.
int getFirstGCPtrIdx()
Get index of first GC pointer operand of -1 if there are none.
unsigned getNumDeoptArgsIdx() const
Get index of Number Deopt Arguments operand.
uint64_t getID() const
Return the ID for the given statepoint.
bool isFoldableReg(Register Reg) const
Return true if Reg is used only in operands which can be folded to stack usage.
unsigned getVarIdx() const
Get starting index of non call related arguments (calling convention, statepoint flags,...
unsigned getNumGCPtrIdx()
Get index of number of GC pointers.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Reg
All possible values of the reg field in the ModR/M byte.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
This struct is a compact representation of a valid (non-zero power of two) alignment.
An information struct used to provide DenseMap with the various necessary components for a given valu...