40 #define DEBUG_TYPE "stackmaps" 44 cl::desc(
"Specify the stackmap encoding version (default = 3)"));
46 const char *StackMaps::WSMP =
"Stack Maps: ";
51 "invalid stackmap definition");
55 : MI(MI), HasDef(MI->getOperand(0).
isReg() && MI->getOperand(0).isDef() &&
56 !MI->getOperand(0).isImplicit()) {
59 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).
isReg() &&
64 assert(getMetaIdx() == CheckStartIdx &&
65 "Unexpected additional definition in Patchpoint intrinsic.");
75 while (ScratchIdx < e &&
82 assert(ScratchIdx != e &&
"No scratch register available");
97 assert(RegNum >= 0 &&
"Invalid Dwarf register number.");
98 return (
unsigned)RegNum;
110 case StackMaps::DirectMemRefOp: {
113 unsigned Size = DL.getPointerSizeInBits();
114 assert((Size % 8) == 0 &&
"Need pointer size in bytes.");
117 int64_t Imm = (++MOI)->getImm();
122 case StackMaps::IndirectMemRefOp: {
123 int64_t
Size = (++MOI)->getImm();
124 assert(Size > 0 &&
"Need a valid size for indirect memory locations.");
126 int64_t Imm = (++MOI)->getImm();
131 case StackMaps::ConstantOp: {
133 assert(MOI->
isImm() &&
"Expected constant operand.");
134 int64_t Imm = MOI->
getImm();
152 "Virtreg operands should have been rewritten before now.");
158 unsigned LLVMRegNum = *TRI->
getLLVMRegNum(DwarfRegNum,
false);
177 OS << WSMP <<
"callsites:\n";
178 for (
const auto &CSI : CSInfos) {
182 OS << WSMP <<
"callsite " << CSI.ID <<
"\n";
183 OS << WSMP <<
" has " << CSLocs.size() <<
" locations\n";
186 for (
const auto &Loc : CSLocs) {
187 OS << WSMP <<
"\t\tLoc " << Idx <<
": ";
190 OS <<
"<Unprocessed operand>";
206 OS <<
" + " << Loc.Offset;
214 OS <<
"+" << Loc.Offset;
217 OS <<
"Constant " << Loc.Offset;
220 OS <<
"Constant Index " << Loc.Offset;
223 OS <<
"\t[encoding: .byte " << Loc.Type <<
", .byte 0" 224 <<
", .short " << Loc.Size <<
", .short " << Loc.Reg <<
", .short 0" 225 <<
", .int " << Loc.Offset <<
"]\n";
229 OS << WSMP <<
"\thas " << LiveOuts.size() <<
" live-out registers\n";
232 for (
const auto &
LO : LiveOuts) {
233 OS << WSMP <<
"\t\tLO " << Idx <<
": ";
238 OS <<
"\t[encoding: .short " <<
LO.DwarfRegNum <<
", .byte 0, .byte " 250 return LiveOutReg(Reg, DwarfRegNum, Size);
256 StackMaps::parseRegisterLiveOutMask(
const uint32_t *
Mask)
const {
257 assert(Mask &&
"No register mask specified");
262 for (
unsigned Reg = 0, NumRegs = TRI->
getNumRegs(); Reg != NumRegs; ++
Reg)
263 if ((Mask[Reg / 32] >> (Reg % 32)) & 1)
264 LiveOuts.
push_back(createLiveOutReg(Reg, TRI));
270 llvm::sort(LiveOuts, [](
const LiveOutReg &LHS,
const LiveOutReg &RHS) {
272 return LHS.DwarfRegNum < RHS.DwarfRegNum;
275 for (
auto I = LiveOuts.begin(),
E = LiveOuts.end();
I !=
E; ++
I) {
276 for (
auto II = std::next(
I); II !=
E; ++II) {
277 if (
I->DwarfRegNum != II->DwarfRegNum) {
291 [](
const LiveOutReg &
LO) {
return LO.Reg == 0; }),
297 void StackMaps::recordStackMapOpers(
const MCSymbol &MILabel,
315 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
319 for (
auto &Loc : Locations) {
331 (uint64_t)Loc.Offset !=
333 "empty and tombstone keys should fit in 32 bits!");
334 auto Result = ConstPool.
insert(std::make_pair(Loc.Offset, Loc.Offset));
335 Loc.Offset = Result.first - ConstPool.
begin();
345 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
346 std::move(LiveOuts));
351 bool HasDynamicFrameSize =
356 if (CurrentIt != FnInfos.
end())
357 CurrentIt->second.RecordCount++;
363 assert(MI.
getOpcode() == TargetOpcode::STACKMAP &&
"expected stackmap");
373 assert(MI.
getOpcode() == TargetOpcode::PATCHPOINT &&
"expected patchpoint");
376 const int64_t ID = opers.getID();
377 auto MOI = std::next(MI.
operands_begin(), opers.getStackMapStartIdx());
379 opers.isAnyReg() && opers.hasDef());
383 auto &Locations = CSInfos.back().Locations;
384 if (opers.isAnyReg()) {
385 unsigned NArgs = opers.getNumCallArgs();
386 for (
unsigned i = 0,
e = (opers.hasDef() ? NArgs + 1 : NArgs); i !=
e; ++i)
388 "anyreg arg must be in reg.");
394 assert(MI.
getOpcode() == TargetOpcode::STATEPOINT &&
"expected statepoint");
399 const unsigned StartIdx = opers.getVarIdx();
400 recordStackMapOpers(L, MI, opers.getID(), MI.
operands_begin() + StartIdx,
414 void StackMaps::emitStackmapHeader(
MCStreamer &OS) {
427 LLVM_DEBUG(
dbgs() << WSMP <<
"#callsites = " << CSInfos.size() <<
'\n');
438 void StackMaps::emitFunctionFrameRecords(
MCStreamer &OS) {
441 for (
auto const &FR : FnInfos) {
443 <<
" frame size: " << FR.second.StackSize
444 <<
" callsite count: " << FR.second.RecordCount <<
'\n');
454 void StackMaps::emitConstantPoolEntries(
MCStreamer &OS) {
457 for (
const auto &ConstEntry : ConstPool) {
492 void StackMaps::emitCallsiteEntries(
MCStreamer &OS) {
495 for (
const auto &CSI : CSInfos) {
503 if (CSLocs.
size() > UINT16_MAX || LiveOuts.
size() > UINT16_MAX) {
521 for (
const auto &Loc : CSLocs) {
537 for (
const auto &
LO : LiveOuts) {
552 "Expected empty constant pool too!");
554 "Expected empty function record too!");
571 emitStackmapHeader(OS);
572 emitFunctionFrameRecords(OS);
573 emitConstantPoolEntries(OS);
574 emitCallsiteEntries(OS);
static bool isReg(const MCInst &MI, unsigned OpNo)
Instances of this class represent a uniqued identifier for a section in the current translation unit...
mop_iterator operands_end()
void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
This class represents lattice values for constants.
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
MCSection * getStackMapSection() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
virtual void AddBlankLine()
AddBlankLine - Emit a blank line to a .s file to pretty it up.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool isRegLiveOut() const
isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
void push_back(const T &Elt)
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
unsigned getSubReg() const
unsigned const TargetRegisterInfo * TRI
MachineFunction * MF
The current machine function.
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.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
bool isEarlyClobber() const
MCSuperRegIterator enumerates all super-registers of Reg.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
const uint32_t * getRegLiveOut() const
getRegLiveOut - Returns a bit mask of live-out registers.
unsigned getNumOperands() const
Retuns the total number of operands.
Base class for the full range of assembler expressions which are needed for parsing.
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 ...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Context object for machine code objects.
int getDwarfRegNum(MCRegister RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
PatchPointOpers(const MachineInstr *MI)
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
SmallVector< LiveOutReg, 8 > LiveOutVec
iterator find(const KeyT &Key)
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Optional< unsigned > getLLVMRegNum(unsigned RegNum, bool isEH) const
Map a dwarf register back to a target register.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
initializer< Ty > init(const Ty &Val)
Streaming machine code generation interface.
MCSymbol * CurrentFnSym
The symbol for the current function.
The instances of the Type class are immutable: once they are created, they are never changed...
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
This class is intended to be used as a driving class for all asm writers.
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
static cl::opt< int > StackMapVersion("stackmap-version", cl::init(3), cl::Hidden, cl::desc("Specify the stackmap encoding version (default = 3)"))
const MCObjectFileInfo * getObjectFileInfo() const
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
bool isSuperRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a super-register of RegA.
MI-level patchpoint operands.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
unsigned getSubRegIdxOffset(unsigned Idx) const
Get the offset of the bit range covered by a sub-register index.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it...
void sort(IteratorTy Start, IteratorTy End)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
StackMapOpers(const MachineInstr *MI)
constexpr bool isInt< 32 >(int64_t x)
Align max(MaybeAlign Lhs, Align Rhs)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
Representation of each machine instruction.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
MI-level stackmap operands.
StackMaps(AsmPrinter &AP)
MI-level Statepoint operands.
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
SmallVector< Location, 8 > LocationVec
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
mop_iterator operands_begin()
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
This class implements an extremely fast bulk output stream that can only output to a stream...
MCSymbol * CurrentFnSymForSize
The symbol used to represent the start of the current function for the purpose of calculating its siz...
void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
Wrapper class representing virtual and physical registers.