41 #define DEBUG_TYPE "stackmaps"
45 cl::desc(
"Specify the stackmap encoding version (default = 3)"));
47 const char *StackMaps::WSMP =
"Stack Maps: ";
52 "invalid stackmap definition");
56 : MI(MI), HasDef(MI->getOperand(0).
isReg() && MI->getOperand(0).isDef() &&
57 !MI->getOperand(0).isImplicit()) {
60 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).
isReg() &&
65 assert(getMetaIdx() == CheckStartIdx &&
66 "Unexpected additional definition in Patchpoint intrinsic.");
76 while (ScratchIdx < e &&
83 assert(ScratchIdx != e &&
"No scratch register available");
98 assert(RegNum >= 0 &&
"Invalid Dwarf register number.");
99 return (
unsigned)RegNum;
105 LiveOutVec &LiveOuts)
const {
111 case StackMaps::DirectMemRefOp: {
115 assert((Size % 8) == 0 &&
"Need pointer size in bytes.");
118 int64_t Imm = (++MOI)->getImm();
123 case StackMaps::IndirectMemRefOp: {
124 int64_t Size = (++MOI)->getImm();
125 assert(Size > 0 &&
"Need a valid size for indirect memory locations.");
126 unsigned Reg = (++MOI)->
getReg();
127 int64_t Imm = (++MOI)->getImm();
132 case StackMaps::ConstantOp: {
134 assert(MOI->
isImm() &&
"Expected constant operand.");
135 int64_t Imm = MOI->
getImm();
153 "Virtreg operands should have been rewritten before now.");
159 unsigned LLVMRegNum = TRI->
getLLVMRegNum(DwarfRegNum,
false);
178 OS << WSMP <<
"callsites:\n";
179 for (
const auto &CSI : CSInfos) {
180 const LocationVec &CSLocs = CSI.Locations;
181 const LiveOutVec &LiveOuts = CSI.LiveOuts;
183 OS << WSMP <<
"callsite " << CSI.ID <<
"\n";
184 OS << WSMP <<
" has " << CSLocs.size() <<
" locations\n";
187 for (
const auto &Loc : CSLocs) {
188 OS << WSMP <<
"\t\tLoc " << Idx <<
": ";
191 OS <<
"<Unprocessed operand>";
207 OS <<
" + " << Loc.Offset;
215 OS <<
"+" << Loc.Offset;
218 OS <<
"Constant " << Loc.Offset;
221 OS <<
"Constant Index " << Loc.Offset;
224 OS <<
"\t[encoding: .byte " << Loc.Type <<
", .byte 0"
225 <<
", .short " << Loc.Size <<
", .short " << Loc.Reg <<
", .short 0"
226 <<
", .int " << Loc.Offset <<
"]\n";
230 OS << WSMP <<
"\thas " << LiveOuts.size() <<
" live-out registers\n";
233 for (
const auto &
LO : LiveOuts) {
234 OS << WSMP <<
"\t\tLO " << Idx <<
": ";
239 OS <<
"\t[encoding: .short " <<
LO.DwarfRegNum <<
", .byte 0, .byte "
251 return LiveOutReg(Reg, DwarfRegNum, Size);
257 StackMaps::parseRegisterLiveOutMask(
const uint32_t *
Mask)
const {
258 assert(Mask &&
"No register mask specified");
263 for (
unsigned Reg = 0, NumRegs = TRI->
getNumRegs(); Reg != NumRegs; ++
Reg)
264 if ((Mask[Reg / 32] >> Reg % 32) & 1)
265 LiveOuts.
push_back(createLiveOutReg(Reg, TRI));
271 std::sort(LiveOuts.begin(), LiveOuts.end(),
272 [](
const LiveOutReg &LHS,
const LiveOutReg &RHS) {
274 return LHS.DwarfRegNum < RHS.DwarfRegNum;
277 for (
auto I = LiveOuts.begin(),
E = LiveOuts.end();
I !=
E; ++
I) {
278 for (
auto II = std::next(
I); II !=
E; ++II) {
279 if (
I->DwarfRegNum != II->DwarfRegNum) {
293 [](
const LiveOutReg &
LO) {
return LO.Reg == 0; }),
307 LocationVec Locations;
318 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
322 for (
auto &Loc : Locations) {
334 (uint64_t)Loc.Offset !=
336 "empty and tombstone keys should fit in 32 bits!");
337 auto Result = ConstPool.
insert(std::make_pair(Loc.Offset, Loc.Offset));
338 Loc.Offset = Result.first - ConstPool.
begin();
348 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
349 std::move(LiveOuts));
354 bool HasDynamicFrameSize =
356 uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.
getStackSize();
359 if (CurrentIt != FnInfos.
end())
360 CurrentIt->second.RecordCount++;
366 assert(MI.
getOpcode() == TargetOpcode::STACKMAP &&
"expected stackmap");
370 recordStackMapOpers(MI, ID, std::next(MI.
operands_begin(), opers.getVarIdx()),
375 assert(MI.
getOpcode() == TargetOpcode::PATCHPOINT &&
"expected patchpoint");
378 const int64_t ID = opers.getID();
379 auto MOI = std::next(MI.
operands_begin(), opers.getStackMapStartIdx());
381 opers.isAnyReg() && opers.hasDef());
385 auto &Locations = CSInfos.back().Locations;
386 if (opers.isAnyReg()) {
387 unsigned NArgs = opers.getNumCallArgs();
388 for (
unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
390 "anyreg arg must be in reg.");
396 assert(MI.
getOpcode() == TargetOpcode::STATEPOINT &&
"expected statepoint");
401 const unsigned StartIdx = opers.getVarIdx();
402 recordStackMapOpers(MI, opers.getID(), MI.
operands_begin() + StartIdx,
416 void StackMaps::emitStackmapHeader(
MCStreamer &OS) {
423 DEBUG(
dbgs() << WSMP <<
"#functions = " << FnInfos.
size() <<
'\n');
426 DEBUG(
dbgs() << WSMP <<
"#constants = " << ConstPool.
size() <<
'\n');
429 DEBUG(
dbgs() << WSMP <<
"#callsites = " << CSInfos.size() <<
'\n');
440 void StackMaps::emitFunctionFrameRecords(
MCStreamer &OS) {
443 for (
auto const &FR : FnInfos) {
444 DEBUG(
dbgs() << WSMP <<
"function addr: " << FR.first
445 <<
" frame size: " << FR.second.StackSize
446 <<
" callsite count: " << FR.second.RecordCount <<
'\n');
456 void StackMaps::emitConstantPoolEntries(
MCStreamer &OS) {
459 for (
const auto &ConstEntry : ConstPool) {
460 DEBUG(
dbgs() << WSMP << ConstEntry.second <<
'\n');
494 void StackMaps::emitCallsiteEntries(
MCStreamer &OS) {
497 for (
const auto &CSI : CSInfos) {
498 const LocationVec &CSLocs = CSI.Locations;
499 const LiveOutVec &LiveOuts = CSI.LiveOuts;
505 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
523 for (
const auto &Loc : CSLocs) {
539 for (
const auto &
LO : LiveOuts) {
553 assert((!CSInfos.empty() || ConstPool.empty()) &&
554 "Expected empty constant pool too!");
555 assert((!CSInfos.empty() || FnInfos.empty()) &&
556 "Expected empty function record too!");
572 DEBUG(
dbgs() <<
"********** Stack Map Output **********\n");
573 emitStackmapHeader(OS);
574 emitFunctionFrameRecords(OS);
575 emitConstantPoolEntries(OS);
576 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...
void push_back(const T &Elt)
mop_iterator operands_end()
MCSection * getStackMapSection() const
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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)
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.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
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.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
const MachineFunction * MF
The current machine function.
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
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.
MCSuperRegIterator enumerates all super-registers of Reg.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Base class for the full range of assembler expressions which are needed for parsing.
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Context object for machine code objects.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
PatchPointOpers(const MachineInstr *MI)
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
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())
void recordStatepoint(const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isEarlyClobber() const
initializer< Ty > init(const Ty &Val)
Streaming machine code generation interface.
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
MCSymbol * CurrentFnSym
The symbol for the current function.
The instances of the Type class are immutable: once they are created, they are never changed...
static cl::opt< int > StackMapVersion("stackmap-version", cl::init(3), cl::desc("Specify the stackmap encoding version (default = 3)"))
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
const MachineOperand & getOperand(unsigned i) const
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.
MI-level patchpoint operands.
unsigned getSubReg() const
int getLLVMRegNum(unsigned RegNum, bool isEH) const
Map a dwarf register back to a target register.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void recordPatchPoint(const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
StackMapOpers(const MachineInstr *MI)
constexpr bool isInt< 32 >(int64_t x)
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...
bool isRegLiveOut() const
isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void recordStackMap(const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
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...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
bool isSuperRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a super-register of RegA.
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
const uint32_t * getRegLiveOut() const
getRegLiveOut - Returns a bit mask of live-out registers.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
MI-level stackmap operands.
StackMaps(AsmPrinter &AP)
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
MI-level Statepoint operands.
unsigned getSubRegIdxOffset(unsigned Idx) const
Get the offset of the bit range covered by a sub-register index.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
mop_iterator operands_begin()
const MCObjectFileInfo * getObjectFileInfo() const
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...
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
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 ...