17a7e6055SDimitry Andric //===- StackMaps.cpp ------------------------------------------------------===//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric // The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric
10db17bf38SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
117a7e6055SDimitry Andric #include "llvm/ADT/DenseMapInfo.h"
127a7e6055SDimitry Andric #include "llvm/ADT/STLExtras.h"
137a7e6055SDimitry Andric #include "llvm/ADT/Twine.h"
14f785676fSDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
1591bc56edSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
1691bc56edSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
17f785676fSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
187a7e6055SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
192cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
202cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
212cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
2291bc56edSDimitry Andric #include "llvm/IR/DataLayout.h"
23f785676fSDimitry Andric #include "llvm/MC/MCContext.h"
24f785676fSDimitry Andric #include "llvm/MC/MCExpr.h"
25f785676fSDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
267a7e6055SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
27f785676fSDimitry Andric #include "llvm/MC/MCStreamer.h"
2891bc56edSDimitry Andric #include "llvm/Support/CommandLine.h"
297a7e6055SDimitry Andric #include "llvm/Support/Debug.h"
307a7e6055SDimitry Andric #include "llvm/Support/ErrorHandling.h"
317a7e6055SDimitry Andric #include "llvm/Support/MathExtras.h"
327a7e6055SDimitry Andric #include "llvm/Support/raw_ostream.h"
337a7e6055SDimitry Andric #include <algorithm>
347a7e6055SDimitry Andric #include <cassert>
357a7e6055SDimitry Andric #include <cstdint>
36f785676fSDimitry Andric #include <iterator>
377a7e6055SDimitry Andric #include <utility>
38f785676fSDimitry Andric
39f785676fSDimitry Andric using namespace llvm;
40f785676fSDimitry Andric
4191bc56edSDimitry Andric #define DEBUG_TYPE "stackmaps"
4291bc56edSDimitry Andric
43875ed548SDimitry Andric static cl::opt<int> StackMapVersion(
442cab237bSDimitry Andric "stackmap-version", cl::init(3), cl::Hidden,
45f37b6182SDimitry Andric cl::desc("Specify the stackmap encoding version (default = 3)"));
4691bc56edSDimitry Andric
4791bc56edSDimitry Andric const char *StackMaps::WSMP = "Stack Maps: ";
4891bc56edSDimitry Andric
StackMapOpers(const MachineInstr * MI)49d88c1a5aSDimitry Andric StackMapOpers::StackMapOpers(const MachineInstr *MI)
50d88c1a5aSDimitry Andric : MI(MI) {
51d88c1a5aSDimitry Andric assert(getVarIdx() <= MI->getNumOperands() &&
52d88c1a5aSDimitry Andric "invalid stackmap definition");
53d88c1a5aSDimitry Andric }
54d88c1a5aSDimitry Andric
PatchPointOpers(const MachineInstr * MI)5591bc56edSDimitry Andric PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
56875ed548SDimitry Andric : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
57d88c1a5aSDimitry Andric !MI->getOperand(0).isImplicit()) {
5891bc56edSDimitry Andric #ifndef NDEBUG
59f785676fSDimitry Andric unsigned CheckStartIdx = 0, e = MI->getNumOperands();
60f785676fSDimitry Andric while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
61f785676fSDimitry Andric MI->getOperand(CheckStartIdx).isDef() &&
62f785676fSDimitry Andric !MI->getOperand(CheckStartIdx).isImplicit())
63f785676fSDimitry Andric ++CheckStartIdx;
64f785676fSDimitry Andric
65f785676fSDimitry Andric assert(getMetaIdx() == CheckStartIdx &&
6691bc56edSDimitry Andric "Unexpected additional definition in Patchpoint intrinsic.");
67f785676fSDimitry Andric #endif
68f785676fSDimitry Andric }
69f785676fSDimitry Andric
getNextScratchIdx(unsigned StartIdx) const70f785676fSDimitry Andric unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
71f785676fSDimitry Andric if (!StartIdx)
72f785676fSDimitry Andric StartIdx = getVarIdx();
73f785676fSDimitry Andric
74f785676fSDimitry Andric // Find the next scratch register (implicit def and early clobber)
75f785676fSDimitry Andric unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
76f785676fSDimitry Andric while (ScratchIdx < e &&
77f785676fSDimitry Andric !(MI->getOperand(ScratchIdx).isReg() &&
78f785676fSDimitry Andric MI->getOperand(ScratchIdx).isDef() &&
79f785676fSDimitry Andric MI->getOperand(ScratchIdx).isImplicit() &&
80f785676fSDimitry Andric MI->getOperand(ScratchIdx).isEarlyClobber()))
81f785676fSDimitry Andric ++ScratchIdx;
82f785676fSDimitry Andric
83f785676fSDimitry Andric assert(ScratchIdx != e && "No scratch register available");
84f785676fSDimitry Andric return ScratchIdx;
85f785676fSDimitry Andric }
86f785676fSDimitry Andric
StackMaps(AsmPrinter & AP)8791bc56edSDimitry Andric StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
88f37b6182SDimitry Andric if (StackMapVersion != 3)
8991bc56edSDimitry Andric llvm_unreachable("Unsupported stackmap version!");
9091bc56edSDimitry Andric }
9191bc56edSDimitry Andric
92ff0cc061SDimitry Andric /// Go up the super-register chain until we hit a valid dwarf register number.
getDwarfRegNum(unsigned Reg,const TargetRegisterInfo * TRI)93ff0cc061SDimitry Andric static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
94875ed548SDimitry Andric int RegNum = TRI->getDwarfRegNum(Reg, false);
95875ed548SDimitry Andric for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR)
96875ed548SDimitry Andric RegNum = TRI->getDwarfRegNum(*SR, false);
97ff0cc061SDimitry Andric
98875ed548SDimitry Andric assert(RegNum >= 0 && "Invalid Dwarf register number.");
99875ed548SDimitry Andric return (unsigned)RegNum;
100ff0cc061SDimitry Andric }
101ff0cc061SDimitry Andric
10291bc56edSDimitry Andric MachineInstr::const_mop_iterator
parseOperand(MachineInstr::const_mop_iterator MOI,MachineInstr::const_mop_iterator MOE,LocationVec & Locs,LiveOutVec & LiveOuts) const10391bc56edSDimitry Andric StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
104875ed548SDimitry Andric MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
105875ed548SDimitry Andric LiveOutVec &LiveOuts) const {
106ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
10791bc56edSDimitry Andric if (MOI->isImm()) {
10891bc56edSDimitry Andric switch (MOI->getImm()) {
109875ed548SDimitry Andric default:
110875ed548SDimitry Andric llvm_unreachable("Unrecognized operand type.");
11191bc56edSDimitry Andric case StackMaps::DirectMemRefOp: {
1127d523365SDimitry Andric auto &DL = AP.MF->getDataLayout();
1137d523365SDimitry Andric
1147d523365SDimitry Andric unsigned Size = DL.getPointerSizeInBits();
11591bc56edSDimitry Andric assert((Size % 8) == 0 && "Need pointer size in bytes.");
11691bc56edSDimitry Andric Size /= 8;
11791bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg();
11891bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm();
119875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Direct, Size,
120875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm);
12191bc56edSDimitry Andric break;
12291bc56edSDimitry Andric }
12391bc56edSDimitry Andric case StackMaps::IndirectMemRefOp: {
12491bc56edSDimitry Andric int64_t Size = (++MOI)->getImm();
12591bc56edSDimitry Andric assert(Size > 0 && "Need a valid size for indirect memory locations.");
12691bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg();
12791bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm();
128875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Indirect, Size,
129875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm);
13091bc56edSDimitry Andric break;
13191bc56edSDimitry Andric }
13291bc56edSDimitry Andric case StackMaps::ConstantOp: {
13391bc56edSDimitry Andric ++MOI;
13491bc56edSDimitry Andric assert(MOI->isImm() && "Expected constant operand.");
13591bc56edSDimitry Andric int64_t Imm = MOI->getImm();
136875ed548SDimitry Andric Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
13791bc56edSDimitry Andric break;
13891bc56edSDimitry Andric }
13991bc56edSDimitry Andric }
14091bc56edSDimitry Andric return ++MOI;
14191bc56edSDimitry Andric }
14291bc56edSDimitry Andric
14391bc56edSDimitry Andric // The physical register number will ultimately be encoded as a DWARF regno.
14491bc56edSDimitry Andric // The stack map also records the size of a spill slot that can hold the
14591bc56edSDimitry Andric // register content. (The runtime can track the actual size of the data type
14691bc56edSDimitry Andric // if it needs to.)
14791bc56edSDimitry Andric if (MOI->isReg()) {
14891bc56edSDimitry Andric // Skip implicit registers (this includes our scratch registers)
14991bc56edSDimitry Andric if (MOI->isImplicit())
15091bc56edSDimitry Andric return ++MOI;
15191bc56edSDimitry Andric
15291bc56edSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
15391bc56edSDimitry Andric "Virtreg operands should have been rewritten before now.");
154ff0cc061SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
15591bc56edSDimitry Andric assert(!MOI->getSubReg() && "Physical subreg still around.");
156ff0cc061SDimitry Andric
157ff0cc061SDimitry Andric unsigned Offset = 0;
158875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
159875ed548SDimitry Andric unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false);
160875ed548SDimitry Andric unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
161ff0cc061SDimitry Andric if (SubRegIdx)
162ff0cc061SDimitry Andric Offset = TRI->getSubRegIdxOffset(SubRegIdx);
163ff0cc061SDimitry Andric
16451690af2SDimitry Andric Locs.emplace_back(Location::Register, TRI->getSpillSize(*RC),
16551690af2SDimitry Andric DwarfRegNum, Offset);
16691bc56edSDimitry Andric return ++MOI;
16791bc56edSDimitry Andric }
16891bc56edSDimitry Andric
16991bc56edSDimitry Andric if (MOI->isRegLiveOut())
17091bc56edSDimitry Andric LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
17191bc56edSDimitry Andric
17291bc56edSDimitry Andric return ++MOI;
17391bc56edSDimitry Andric }
17491bc56edSDimitry Andric
print(raw_ostream & OS)175ff0cc061SDimitry Andric void StackMaps::print(raw_ostream &OS) {
176ff0cc061SDimitry Andric const TargetRegisterInfo *TRI =
177ff0cc061SDimitry Andric AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
178ff0cc061SDimitry Andric OS << WSMP << "callsites:\n";
179ff0cc061SDimitry Andric for (const auto &CSI : CSInfos) {
180ff0cc061SDimitry Andric const LocationVec &CSLocs = CSI.Locations;
181ff0cc061SDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts;
18291bc56edSDimitry Andric
183ff0cc061SDimitry Andric OS << WSMP << "callsite " << CSI.ID << "\n";
184ff0cc061SDimitry Andric OS << WSMP << " has " << CSLocs.size() << " locations\n";
185ff0cc061SDimitry Andric
186875ed548SDimitry Andric unsigned Idx = 0;
187ff0cc061SDimitry Andric for (const auto &Loc : CSLocs) {
188875ed548SDimitry Andric OS << WSMP << "\t\tLoc " << Idx << ": ";
189875ed548SDimitry Andric switch (Loc.Type) {
190ff0cc061SDimitry Andric case Location::Unprocessed:
191ff0cc061SDimitry Andric OS << "<Unprocessed operand>";
192ff0cc061SDimitry Andric break;
193ff0cc061SDimitry Andric case Location::Register:
194ff0cc061SDimitry Andric OS << "Register ";
195ff0cc061SDimitry Andric if (TRI)
1962cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI);
197ff0cc061SDimitry Andric else
198ff0cc061SDimitry Andric OS << Loc.Reg;
199ff0cc061SDimitry Andric break;
200ff0cc061SDimitry Andric case Location::Direct:
201ff0cc061SDimitry Andric OS << "Direct ";
202ff0cc061SDimitry Andric if (TRI)
2032cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI);
204ff0cc061SDimitry Andric else
205ff0cc061SDimitry Andric OS << Loc.Reg;
206ff0cc061SDimitry Andric if (Loc.Offset)
207ff0cc061SDimitry Andric OS << " + " << Loc.Offset;
208ff0cc061SDimitry Andric break;
209ff0cc061SDimitry Andric case Location::Indirect:
210ff0cc061SDimitry Andric OS << "Indirect ";
211ff0cc061SDimitry Andric if (TRI)
2122cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI);
213ff0cc061SDimitry Andric else
214ff0cc061SDimitry Andric OS << Loc.Reg;
215ff0cc061SDimitry Andric OS << "+" << Loc.Offset;
216ff0cc061SDimitry Andric break;
217ff0cc061SDimitry Andric case Location::Constant:
218ff0cc061SDimitry Andric OS << "Constant " << Loc.Offset;
219ff0cc061SDimitry Andric break;
220ff0cc061SDimitry Andric case Location::ConstantIndex:
221ff0cc061SDimitry Andric OS << "Constant Index " << Loc.Offset;
222ff0cc061SDimitry Andric break;
223ff0cc061SDimitry Andric }
224f37b6182SDimitry Andric OS << "\t[encoding: .byte " << Loc.Type << ", .byte 0"
225f37b6182SDimitry Andric << ", .short " << Loc.Size << ", .short " << Loc.Reg << ", .short 0"
226f37b6182SDimitry Andric << ", .int " << Loc.Offset << "]\n";
227875ed548SDimitry Andric Idx++;
228ff0cc061SDimitry Andric }
229ff0cc061SDimitry Andric
230875ed548SDimitry Andric OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
231ff0cc061SDimitry Andric
232875ed548SDimitry Andric Idx = 0;
233ff0cc061SDimitry Andric for (const auto &LO : LiveOuts) {
234875ed548SDimitry Andric OS << WSMP << "\t\tLO " << Idx << ": ";
235ff0cc061SDimitry Andric if (TRI)
2362cab237bSDimitry Andric OS << printReg(LO.Reg, TRI);
237ff0cc061SDimitry Andric else
238ff0cc061SDimitry Andric OS << LO.Reg;
239875ed548SDimitry Andric OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
240ff0cc061SDimitry Andric << LO.Size << "]\n";
241875ed548SDimitry Andric Idx++;
242ff0cc061SDimitry Andric }
243ff0cc061SDimitry Andric }
24491bc56edSDimitry Andric }
24591bc56edSDimitry Andric
24691bc56edSDimitry Andric /// Create a live-out register record for the given register Reg.
24791bc56edSDimitry Andric StackMaps::LiveOutReg
createLiveOutReg(unsigned Reg,const TargetRegisterInfo * TRI) const24891bc56edSDimitry Andric StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
249875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
25051690af2SDimitry Andric unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg));
251875ed548SDimitry Andric return LiveOutReg(Reg, DwarfRegNum, Size);
25291bc56edSDimitry Andric }
25391bc56edSDimitry Andric
25491bc56edSDimitry Andric /// Parse the register live-out mask and return a vector of live-out registers
25591bc56edSDimitry Andric /// that need to be recorded in the stackmap.
25691bc56edSDimitry Andric StackMaps::LiveOutVec
parseRegisterLiveOutMask(const uint32_t * Mask) const25791bc56edSDimitry Andric StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
25891bc56edSDimitry Andric assert(Mask && "No register mask specified");
259ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
26091bc56edSDimitry Andric LiveOutVec LiveOuts;
26191bc56edSDimitry Andric
26291bc56edSDimitry Andric // Create a LiveOutReg for each bit that is set in the register mask.
26391bc56edSDimitry Andric for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
26491bc56edSDimitry Andric if ((Mask[Reg / 32] >> Reg % 32) & 1)
26591bc56edSDimitry Andric LiveOuts.push_back(createLiveOutReg(Reg, TRI));
26691bc56edSDimitry Andric
26791bc56edSDimitry Andric // We don't need to keep track of a register if its super-register is already
26891bc56edSDimitry Andric // in the list. Merge entries that refer to the same dwarf register and use
26991bc56edSDimitry Andric // the maximum size that needs to be spilled.
270875ed548SDimitry Andric
271*b5893f02SDimitry Andric llvm::sort(LiveOuts, [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
272875ed548SDimitry Andric // Only sort by the dwarf register number.
273875ed548SDimitry Andric return LHS.DwarfRegNum < RHS.DwarfRegNum;
274875ed548SDimitry Andric });
275875ed548SDimitry Andric
276875ed548SDimitry Andric for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
277875ed548SDimitry Andric for (auto II = std::next(I); II != E; ++II) {
278875ed548SDimitry Andric if (I->DwarfRegNum != II->DwarfRegNum) {
27991bc56edSDimitry Andric // Skip all the now invalid entries.
28091bc56edSDimitry Andric I = --II;
28191bc56edSDimitry Andric break;
28291bc56edSDimitry Andric }
28391bc56edSDimitry Andric I->Size = std::max(I->Size, II->Size);
28491bc56edSDimitry Andric if (TRI->isSuperRegister(I->Reg, II->Reg))
28591bc56edSDimitry Andric I->Reg = II->Reg;
286875ed548SDimitry Andric II->Reg = 0; // mark for deletion.
28791bc56edSDimitry Andric }
28891bc56edSDimitry Andric }
289875ed548SDimitry Andric
290875ed548SDimitry Andric LiveOuts.erase(
2917a7e6055SDimitry Andric llvm::remove_if(LiveOuts,
2927a7e6055SDimitry Andric [](const LiveOutReg &LO) { return LO.Reg == 0; }),
293875ed548SDimitry Andric LiveOuts.end());
294875ed548SDimitry Andric
29591bc56edSDimitry Andric return LiveOuts;
29691bc56edSDimitry Andric }
29791bc56edSDimitry Andric
recordStackMapOpers(const MachineInstr & MI,uint64_t ID,MachineInstr::const_mop_iterator MOI,MachineInstr::const_mop_iterator MOE,bool recordResult)29891bc56edSDimitry Andric void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
299f785676fSDimitry Andric MachineInstr::const_mop_iterator MOI,
300f785676fSDimitry Andric MachineInstr::const_mop_iterator MOE,
301f785676fSDimitry Andric bool recordResult) {
302ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext();
303ff0cc061SDimitry Andric MCSymbol *MILabel = OutContext.createTempSymbol();
304ff0cc061SDimitry Andric AP.OutStreamer->EmitLabel(MILabel);
305f785676fSDimitry Andric
30691bc56edSDimitry Andric LocationVec Locations;
30791bc56edSDimitry Andric LiveOutVec LiveOuts;
308f785676fSDimitry Andric
309f785676fSDimitry Andric if (recordResult) {
31091bc56edSDimitry Andric assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
311875ed548SDimitry Andric parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
312875ed548SDimitry Andric LiveOuts);
313f785676fSDimitry Andric }
314f785676fSDimitry Andric
31591bc56edSDimitry Andric // Parse operands.
316f785676fSDimitry Andric while (MOI != MOE) {
31791bc56edSDimitry Andric MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
31891bc56edSDimitry Andric }
319f785676fSDimitry Andric
320f785676fSDimitry Andric // Move large constants into the constant pool.
321875ed548SDimitry Andric for (auto &Loc : Locations) {
32291bc56edSDimitry Andric // Constants are encoded as sign-extended integers.
32391bc56edSDimitry Andric // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
324875ed548SDimitry Andric if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
325875ed548SDimitry Andric Loc.Type = Location::ConstantIndex;
32639d628a0SDimitry Andric // ConstPool is intentionally a MapVector of 'uint64_t's (as
32739d628a0SDimitry Andric // opposed to 'int64_t's). We should never be in a situation
32839d628a0SDimitry Andric // where we have to insert either the tombstone or the empty
32939d628a0SDimitry Andric // keys into a map, and for a DenseMap<uint64_t, T> these are
33039d628a0SDimitry Andric // (uint64_t)0 and (uint64_t)-1. They can be and are
33139d628a0SDimitry Andric // represented using 32 bit integers.
332875ed548SDimitry Andric assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
333875ed548SDimitry Andric (uint64_t)Loc.Offset !=
334875ed548SDimitry Andric DenseMapInfo<uint64_t>::getTombstoneKey() &&
33539d628a0SDimitry Andric "empty and tombstone keys should fit in 32 bits!");
336875ed548SDimitry Andric auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
337875ed548SDimitry Andric Loc.Offset = Result.first - ConstPool.begin();
33891bc56edSDimitry Andric }
339f785676fSDimitry Andric }
340f785676fSDimitry Andric
34191bc56edSDimitry Andric // Create an expression to calculate the offset of the callsite from function
34291bc56edSDimitry Andric // entry.
34397bc6c73SDimitry Andric const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
34497bc6c73SDimitry Andric MCSymbolRefExpr::create(MILabel, OutContext),
345875ed548SDimitry Andric MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
346f785676fSDimitry Andric
34739d628a0SDimitry Andric CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
34839d628a0SDimitry Andric std::move(LiveOuts));
349f785676fSDimitry Andric
350d88c1a5aSDimitry Andric // Record the stack size of the current function and update callsite count.
351d88c1a5aSDimitry Andric const MachineFrameInfo &MFI = AP.MF->getFrameInfo();
35239d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
353875ed548SDimitry Andric bool HasDynamicFrameSize =
354d88c1a5aSDimitry Andric MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
355d88c1a5aSDimitry Andric uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize();
356d88c1a5aSDimitry Andric
357d88c1a5aSDimitry Andric auto CurrentIt = FnInfos.find(AP.CurrentFnSym);
358d88c1a5aSDimitry Andric if (CurrentIt != FnInfos.end())
359d88c1a5aSDimitry Andric CurrentIt->second.RecordCount++;
360d88c1a5aSDimitry Andric else
361d88c1a5aSDimitry Andric FnInfos.insert(std::make_pair(AP.CurrentFnSym, FunctionInfo(FrameSize)));
362f785676fSDimitry Andric }
363f785676fSDimitry Andric
recordStackMap(const MachineInstr & MI)364f785676fSDimitry Andric void StackMaps::recordStackMap(const MachineInstr &MI) {
36591bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
366f785676fSDimitry Andric
367d88c1a5aSDimitry Andric StackMapOpers opers(&MI);
368d88c1a5aSDimitry Andric const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm();
369d88c1a5aSDimitry Andric recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), opers.getVarIdx()),
37091bc56edSDimitry Andric MI.operands_end());
371f785676fSDimitry Andric }
372f785676fSDimitry Andric
recordPatchPoint(const MachineInstr & MI)373f785676fSDimitry Andric void StackMaps::recordPatchPoint(const MachineInstr &MI) {
37491bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
375f785676fSDimitry Andric
376f785676fSDimitry Andric PatchPointOpers opers(&MI);
377d88c1a5aSDimitry Andric const int64_t ID = opers.getID();
378875ed548SDimitry Andric auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
37991bc56edSDimitry Andric recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
380f785676fSDimitry Andric opers.isAnyReg() && opers.hasDef());
381f785676fSDimitry Andric
382f785676fSDimitry Andric #ifndef NDEBUG
383f785676fSDimitry Andric // verify anyregcc
384875ed548SDimitry Andric auto &Locations = CSInfos.back().Locations;
385f785676fSDimitry Andric if (opers.isAnyReg()) {
386d88c1a5aSDimitry Andric unsigned NArgs = opers.getNumCallArgs();
387f785676fSDimitry Andric for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
388875ed548SDimitry Andric assert(Locations[i].Type == Location::Register &&
389f785676fSDimitry Andric "anyreg arg must be in reg.");
390f785676fSDimitry Andric }
391f785676fSDimitry Andric #endif
392f785676fSDimitry Andric }
3937a7e6055SDimitry Andric
recordStatepoint(const MachineInstr & MI)39439d628a0SDimitry Andric void StackMaps::recordStatepoint(const MachineInstr &MI) {
395875ed548SDimitry Andric assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
39639d628a0SDimitry Andric
39739d628a0SDimitry Andric StatepointOpers opers(&MI);
39839d628a0SDimitry Andric // Record all the deopt and gc operands (they're contiguous and run from the
39939d628a0SDimitry Andric // initial index to the end of the operand list)
40039d628a0SDimitry Andric const unsigned StartIdx = opers.getVarIdx();
401ff0cc061SDimitry Andric recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
402ff0cc061SDimitry Andric MI.operands_end(), false);
40339d628a0SDimitry Andric }
404f785676fSDimitry Andric
40591bc56edSDimitry Andric /// Emit the stackmap header.
406f785676fSDimitry Andric ///
40791bc56edSDimitry Andric /// Header {
408d88c1a5aSDimitry Andric /// uint8 : Stack Map Version (currently 2)
40991bc56edSDimitry Andric /// uint8 : Reserved (expected to be 0)
41091bc56edSDimitry Andric /// uint16 : Reserved (expected to be 0)
41191bc56edSDimitry Andric /// }
41291bc56edSDimitry Andric /// uint32 : NumFunctions
413f785676fSDimitry Andric /// uint32 : NumConstants
414f785676fSDimitry Andric /// uint32 : NumRecords
emitStackmapHeader(MCStreamer & OS)41591bc56edSDimitry Andric void StackMaps::emitStackmapHeader(MCStreamer &OS) {
41691bc56edSDimitry Andric // Header.
41791bc56edSDimitry Andric OS.EmitIntValue(StackMapVersion, 1); // Version.
41891bc56edSDimitry Andric OS.EmitIntValue(0, 1); // Reserved.
41991bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved.
42091bc56edSDimitry Andric
42191bc56edSDimitry Andric // Num functions.
4224ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "#functions = " << FnInfos.size() << '\n');
423d88c1a5aSDimitry Andric OS.EmitIntValue(FnInfos.size(), 4);
42491bc56edSDimitry Andric // Num constants.
4254ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
42691bc56edSDimitry Andric OS.EmitIntValue(ConstPool.size(), 4);
42791bc56edSDimitry Andric // Num callsites.
4284ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
42991bc56edSDimitry Andric OS.EmitIntValue(CSInfos.size(), 4);
43091bc56edSDimitry Andric }
43191bc56edSDimitry Andric
43291bc56edSDimitry Andric /// Emit the function frame record for each function.
43391bc56edSDimitry Andric ///
43491bc56edSDimitry Andric /// StkSizeRecord[NumFunctions] {
43591bc56edSDimitry Andric /// uint64 : Function Address
43691bc56edSDimitry Andric /// uint64 : Stack Size
437d88c1a5aSDimitry Andric /// uint64 : Record Count
43891bc56edSDimitry Andric /// }
emitFunctionFrameRecords(MCStreamer & OS)43991bc56edSDimitry Andric void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
44091bc56edSDimitry Andric // Function Frame records.
4414ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "functions:\n");
442d88c1a5aSDimitry Andric for (auto const &FR : FnInfos) {
4434ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "function addr: " << FR.first
444d88c1a5aSDimitry Andric << " frame size: " << FR.second.StackSize
445d88c1a5aSDimitry Andric << " callsite count: " << FR.second.RecordCount << '\n');
44691bc56edSDimitry Andric OS.EmitSymbolValue(FR.first, 8);
447d88c1a5aSDimitry Andric OS.EmitIntValue(FR.second.StackSize, 8);
448d88c1a5aSDimitry Andric OS.EmitIntValue(FR.second.RecordCount, 8);
44991bc56edSDimitry Andric }
45091bc56edSDimitry Andric }
45191bc56edSDimitry Andric
45291bc56edSDimitry Andric /// Emit the constant pool.
45391bc56edSDimitry Andric ///
45491bc56edSDimitry Andric /// int64 : Constants[NumConstants]
emitConstantPoolEntries(MCStreamer & OS)45591bc56edSDimitry Andric void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
45691bc56edSDimitry Andric // Constant pool entries.
4574ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << "constants:\n");
458875ed548SDimitry Andric for (const auto &ConstEntry : ConstPool) {
4594ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
46091bc56edSDimitry Andric OS.EmitIntValue(ConstEntry.second, 8);
46191bc56edSDimitry Andric }
46291bc56edSDimitry Andric }
46391bc56edSDimitry Andric
46491bc56edSDimitry Andric /// Emit the callsite info for each callsite.
46591bc56edSDimitry Andric ///
466f785676fSDimitry Andric /// StkMapRecord[NumRecords] {
46791bc56edSDimitry Andric /// uint64 : PatchPoint ID
468f785676fSDimitry Andric /// uint32 : Instruction Offset
469f785676fSDimitry Andric /// uint16 : Reserved (record flags)
470f785676fSDimitry Andric /// uint16 : NumLocations
471f785676fSDimitry Andric /// Location[NumLocations] {
472f785676fSDimitry Andric /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
473f785676fSDimitry Andric /// uint8 : Size in Bytes
474f785676fSDimitry Andric /// uint16 : Dwarf RegNum
475f785676fSDimitry Andric /// int32 : Offset
476f785676fSDimitry Andric /// }
47791bc56edSDimitry Andric /// uint16 : Padding
47891bc56edSDimitry Andric /// uint16 : NumLiveOuts
47991bc56edSDimitry Andric /// LiveOuts[NumLiveOuts] {
48091bc56edSDimitry Andric /// uint16 : Dwarf RegNum
48191bc56edSDimitry Andric /// uint8 : Reserved
48291bc56edSDimitry Andric /// uint8 : Size in Bytes
48391bc56edSDimitry Andric /// }
48491bc56edSDimitry Andric /// uint32 : Padding (only if required to align to 8 byte)
485f785676fSDimitry Andric /// }
486f785676fSDimitry Andric ///
487f785676fSDimitry Andric /// Location Encoding, Type, Value:
488f785676fSDimitry Andric /// 0x1, Register, Reg (value in register)
489f785676fSDimitry Andric /// 0x2, Direct, Reg + Offset (frame index)
490f785676fSDimitry Andric /// 0x3, Indirect, [Reg + Offset] (spilled value)
491f785676fSDimitry Andric /// 0x4, Constant, Offset (small constant)
492f785676fSDimitry Andric /// 0x5, ConstIndex, Constants[Offset] (large constant)
emitCallsiteEntries(MCStreamer & OS)493ff0cc061SDimitry Andric void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
4944ba319b5SDimitry Andric LLVM_DEBUG(print(dbgs()));
49591bc56edSDimitry Andric // Callsite entries.
49691bc56edSDimitry Andric for (const auto &CSI : CSInfos) {
49791bc56edSDimitry Andric const LocationVec &CSLocs = CSI.Locations;
49891bc56edSDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts;
499f785676fSDimitry Andric
500f785676fSDimitry Andric // Verify stack map entry. It's better to communicate a problem to the
501f785676fSDimitry Andric // runtime than crash in case of in-process compilation. Currently, we do
502f785676fSDimitry Andric // simple overflow checks, but we may eventually communicate other
503f785676fSDimitry Andric // compilation errors this way.
50491bc56edSDimitry Andric if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
50591bc56edSDimitry Andric OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
50691bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4);
50791bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved.
50891bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 locations.
50991bc56edSDimitry Andric OS.EmitIntValue(0, 2); // padding.
51091bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 live-out registers.
51191bc56edSDimitry Andric OS.EmitIntValue(0, 4); // padding.
512f785676fSDimitry Andric continue;
513f785676fSDimitry Andric }
514f785676fSDimitry Andric
51591bc56edSDimitry Andric OS.EmitIntValue(CSI.ID, 8);
51691bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4);
517f785676fSDimitry Andric
518f785676fSDimitry Andric // Reserved for flags.
51991bc56edSDimitry Andric OS.EmitIntValue(0, 2);
52091bc56edSDimitry Andric OS.EmitIntValue(CSLocs.size(), 2);
521f785676fSDimitry Andric
52291bc56edSDimitry Andric for (const auto &Loc : CSLocs) {
523875ed548SDimitry Andric OS.EmitIntValue(Loc.Type, 1);
524f37b6182SDimitry Andric OS.EmitIntValue(0, 1); // Reserved
525f37b6182SDimitry Andric OS.EmitIntValue(Loc.Size, 2);
526ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Reg, 2);
527f37b6182SDimitry Andric OS.EmitIntValue(0, 2); // Reserved
528ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Offset, 4);
529f785676fSDimitry Andric }
53091bc56edSDimitry Andric
531f37b6182SDimitry Andric // Emit alignment to 8 byte.
532f37b6182SDimitry Andric OS.EmitValueToAlignment(8);
533f37b6182SDimitry Andric
53491bc56edSDimitry Andric // Num live-out registers and padding to align to 4 byte.
53591bc56edSDimitry Andric OS.EmitIntValue(0, 2);
53691bc56edSDimitry Andric OS.EmitIntValue(LiveOuts.size(), 2);
53791bc56edSDimitry Andric
53891bc56edSDimitry Andric for (const auto &LO : LiveOuts) {
539875ed548SDimitry Andric OS.EmitIntValue(LO.DwarfRegNum, 2);
54091bc56edSDimitry Andric OS.EmitIntValue(0, 1);
54191bc56edSDimitry Andric OS.EmitIntValue(LO.Size, 1);
542f785676fSDimitry Andric }
54391bc56edSDimitry Andric // Emit alignment to 8 byte.
54491bc56edSDimitry Andric OS.EmitValueToAlignment(8);
545f785676fSDimitry Andric }
546f785676fSDimitry Andric }
547f785676fSDimitry Andric
54891bc56edSDimitry Andric /// Serialize the stackmap data.
serializeToStackMapSection()54991bc56edSDimitry Andric void StackMaps::serializeToStackMapSection() {
55091bc56edSDimitry Andric (void)WSMP;
55191bc56edSDimitry Andric // Bail out if there's no stack map data.
5523ca95b02SDimitry Andric assert((!CSInfos.empty() || ConstPool.empty()) &&
55391bc56edSDimitry Andric "Expected empty constant pool too!");
554d88c1a5aSDimitry Andric assert((!CSInfos.empty() || FnInfos.empty()) &&
55591bc56edSDimitry Andric "Expected empty function record too!");
55691bc56edSDimitry Andric if (CSInfos.empty())
55791bc56edSDimitry Andric return;
558f785676fSDimitry Andric
559ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext();
560ff0cc061SDimitry Andric MCStreamer &OS = *AP.OutStreamer;
56191bc56edSDimitry Andric
56291bc56edSDimitry Andric // Create the section.
563ff0cc061SDimitry Andric MCSection *StackMapSection =
56491bc56edSDimitry Andric OutContext.getObjectFileInfo()->getStackMapSection();
56591bc56edSDimitry Andric OS.SwitchSection(StackMapSection);
56691bc56edSDimitry Andric
56791bc56edSDimitry Andric // Emit a dummy symbol to force section inclusion.
568ff0cc061SDimitry Andric OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
56991bc56edSDimitry Andric
57091bc56edSDimitry Andric // Serialize data.
5714ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Stack Map Output **********\n");
57291bc56edSDimitry Andric emitStackmapHeader(OS);
57391bc56edSDimitry Andric emitFunctionFrameRecords(OS);
57491bc56edSDimitry Andric emitConstantPoolEntries(OS);
575ff0cc061SDimitry Andric emitCallsiteEntries(OS);
57691bc56edSDimitry Andric OS.AddBlankLine();
57791bc56edSDimitry Andric
57891bc56edSDimitry Andric // Clean up.
579f785676fSDimitry Andric CSInfos.clear();
58091bc56edSDimitry Andric ConstPool.clear();
581f785676fSDimitry Andric }
582