1 //===---------------------------- StackMaps.cpp ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/StackMaps.h"
11 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/CodeGen/MachineFrameInfo.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetOpcodes.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetSubtargetInfo.h"
30 #define DEBUG_TYPE "stackmaps"
32 static cl::opt<int> StackMapVersion(
33 "stackmap-version", cl::init(1),
34 cl::desc("Specify the stackmap encoding version (default = 1)"));
36 const char *StackMaps::WSMP = "Stack Maps: ";
38 PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
39 : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
40 !MI->getOperand(0).isImplicit()),
41 IsAnyReg(MI->getOperand(getMetaIdx(CCPos)).getImm() ==
42 CallingConv::AnyReg) {
44 unsigned CheckStartIdx = 0, e = MI->getNumOperands();
45 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
46 MI->getOperand(CheckStartIdx).isDef() &&
47 !MI->getOperand(CheckStartIdx).isImplicit())
50 assert(getMetaIdx() == CheckStartIdx &&
51 "Unexpected additional definition in Patchpoint intrinsic.");
55 unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
57 StartIdx = getVarIdx();
59 // Find the next scratch register (implicit def and early clobber)
60 unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
61 while (ScratchIdx < e &&
62 !(MI->getOperand(ScratchIdx).isReg() &&
63 MI->getOperand(ScratchIdx).isDef() &&
64 MI->getOperand(ScratchIdx).isImplicit() &&
65 MI->getOperand(ScratchIdx).isEarlyClobber()))
68 assert(ScratchIdx != e && "No scratch register available");
72 StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
73 if (StackMapVersion != 1)
74 llvm_unreachable("Unsupported stackmap version!");
77 /// Go up the super-register chain until we hit a valid dwarf register number.
78 static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
79 int RegNo = TRI->getDwarfRegNum(Reg, false);
80 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNo < 0; ++SR)
81 RegNo = TRI->getDwarfRegNum(*SR, false);
83 assert(RegNo >= 0 && "Invalid Dwarf register number.");
84 return (unsigned)RegNo;
87 MachineInstr::const_mop_iterator
88 StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
89 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
90 LiveOutVec &LiveOuts) const {
91 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
93 switch (MOI->getImm()) {
95 llvm_unreachable("Unrecognized operand type.");
96 case StackMaps::DirectMemRefOp: {
97 unsigned Size = AP.TM.getDataLayout()->getPointerSizeInBits();
98 assert((Size % 8) == 0 && "Need pointer size in bytes.");
100 unsigned Reg = (++MOI)->getReg();
101 int64_t Imm = (++MOI)->getImm();
102 Locs.push_back(Location(StackMaps::Location::Direct, Size,
103 getDwarfRegNum(Reg, TRI), Imm));
106 case StackMaps::IndirectMemRefOp: {
107 int64_t Size = (++MOI)->getImm();
108 assert(Size > 0 && "Need a valid size for indirect memory locations.");
109 unsigned Reg = (++MOI)->getReg();
110 int64_t Imm = (++MOI)->getImm();
111 Locs.push_back(Location(StackMaps::Location::Indirect, Size,
112 getDwarfRegNum(Reg, TRI), Imm));
115 case StackMaps::ConstantOp: {
117 assert(MOI->isImm() && "Expected constant operand.");
118 int64_t Imm = MOI->getImm();
119 Locs.push_back(Location(Location::Constant, sizeof(int64_t), 0, Imm));
126 // The physical register number will ultimately be encoded as a DWARF regno.
127 // The stack map also records the size of a spill slot that can hold the
128 // register content. (The runtime can track the actual size of the data type
131 // Skip implicit registers (this includes our scratch registers)
132 if (MOI->isImplicit())
135 assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
136 "Virtreg operands should have been rewritten before now.");
137 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
138 assert(!MOI->getSubReg() && "Physical subreg still around.");
141 unsigned RegNo = getDwarfRegNum(MOI->getReg(), TRI);
142 unsigned LLVMRegNo = TRI->getLLVMRegNum(RegNo, false);
143 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNo, MOI->getReg());
145 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
147 Locs.push_back(Location(Location::Register, RC->getSize(), RegNo, Offset));
151 if (MOI->isRegLiveOut())
152 LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
157 void StackMaps::print(raw_ostream &OS) {
158 const TargetRegisterInfo *TRI =
159 AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
160 OS << WSMP << "callsites:\n";
161 for (const auto &CSI : CSInfos) {
162 const LocationVec &CSLocs = CSI.Locations;
163 const LiveOutVec &LiveOuts = CSI.LiveOuts;
165 OS << WSMP << "callsite " << CSI.ID << "\n";
166 OS << WSMP << " has " << CSLocs.size() << " locations\n";
168 unsigned OperIdx = 0;
169 for (const auto &Loc : CSLocs) {
170 OS << WSMP << " Loc " << OperIdx << ": ";
171 switch (Loc.LocType) {
172 case Location::Unprocessed:
173 OS << "<Unprocessed operand>";
175 case Location::Register:
178 OS << TRI->getName(Loc.Reg);
182 case Location::Direct:
185 OS << TRI->getName(Loc.Reg);
189 OS << " + " << Loc.Offset;
191 case Location::Indirect:
194 OS << TRI->getName(Loc.Reg);
197 OS << "+" << Loc.Offset;
199 case Location::Constant:
200 OS << "Constant " << Loc.Offset;
202 case Location::ConstantIndex:
203 OS << "Constant Index " << Loc.Offset;
206 OS << " [encoding: .byte " << Loc.LocType << ", .byte " << Loc.Size
207 << ", .short " << Loc.Reg << ", .int " << Loc.Offset << "]\n";
211 OS << WSMP << " has " << LiveOuts.size() << " live-out registers\n";
214 for (const auto &LO : LiveOuts) {
215 OS << WSMP << " LO " << OperIdx << ": ";
217 OS << TRI->getName(LO.Reg);
220 OS << " [encoding: .short " << LO.RegNo << ", .byte 0, .byte "
227 /// Create a live-out register record for the given register Reg.
228 StackMaps::LiveOutReg
229 StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
230 unsigned RegNo = getDwarfRegNum(Reg, TRI);
231 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
232 return LiveOutReg(Reg, RegNo, Size);
235 /// Parse the register live-out mask and return a vector of live-out registers
236 /// that need to be recorded in the stackmap.
237 StackMaps::LiveOutVec
238 StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
239 assert(Mask && "No register mask specified");
240 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
243 // Create a LiveOutReg for each bit that is set in the register mask.
244 for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
245 if ((Mask[Reg / 32] >> Reg % 32) & 1)
246 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
248 // We don't need to keep track of a register if its super-register is already
249 // in the list. Merge entries that refer to the same dwarf register and use
250 // the maximum size that needs to be spilled.
251 std::sort(LiveOuts.begin(), LiveOuts.end());
252 for (LiveOutVec::iterator I = LiveOuts.begin(), E = LiveOuts.end(); I != E;
254 for (LiveOutVec::iterator II = std::next(I); II != E; ++II) {
255 if (I->RegNo != II->RegNo) {
256 // Skip all the now invalid entries.
260 I->Size = std::max(I->Size, II->Size);
261 if (TRI->isSuperRegister(I->Reg, II->Reg))
267 std::remove_if(LiveOuts.begin(), LiveOuts.end(), LiveOutReg::IsInvalid),
272 void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
273 MachineInstr::const_mop_iterator MOI,
274 MachineInstr::const_mop_iterator MOE,
277 MCContext &OutContext = AP.OutStreamer->getContext();
278 MCSymbol *MILabel = OutContext.createTempSymbol();
279 AP.OutStreamer->EmitLabel(MILabel);
281 LocationVec Locations;
285 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
286 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
292 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
295 // Move large constants into the constant pool.
296 for (LocationVec::iterator I = Locations.begin(), E = Locations.end(); I != E;
298 // Constants are encoded as sign-extended integers.
299 // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
300 if (I->LocType == Location::Constant && !isInt<32>(I->Offset)) {
301 I->LocType = Location::ConstantIndex;
302 // ConstPool is intentionally a MapVector of 'uint64_t's (as
303 // opposed to 'int64_t's). We should never be in a situation
304 // where we have to insert either the tombstone or the empty
305 // keys into a map, and for a DenseMap<uint64_t, T> these are
306 // (uint64_t)0 and (uint64_t)-1. They can be and are
307 // represented using 32 bit integers.
309 assert((uint64_t)I->Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
310 (uint64_t)I->Offset != DenseMapInfo<uint64_t>::getTombstoneKey() &&
311 "empty and tombstone keys should fit in 32 bits!");
312 auto Result = ConstPool.insert(std::make_pair(I->Offset, I->Offset));
313 I->Offset = Result.first - ConstPool.begin();
317 // Create an expression to calculate the offset of the callsite from function
319 const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
320 MCSymbolRefExpr::create(MILabel, OutContext),
321 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
323 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
324 std::move(LiveOuts));
326 // Record the stack size of the current function.
327 const MachineFrameInfo *MFI = AP.MF->getFrameInfo();
328 const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
329 const bool DynamicFrameSize =
330 MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
331 FnStackSize[AP.CurrentFnSym] =
332 DynamicFrameSize ? UINT64_MAX : MFI->getStackSize();
335 void StackMaps::recordStackMap(const MachineInstr &MI) {
336 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
338 int64_t ID = MI.getOperand(0).getImm();
339 recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2),
343 void StackMaps::recordPatchPoint(const MachineInstr &MI) {
344 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
346 PatchPointOpers opers(&MI);
347 int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm();
349 MachineInstr::const_mop_iterator MOI =
350 std::next(MI.operands_begin(), opers.getStackMapStartIdx());
351 recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
352 opers.isAnyReg() && opers.hasDef());
356 LocationVec &Locations = CSInfos.back().Locations;
357 if (opers.isAnyReg()) {
358 unsigned NArgs = opers.getMetaOper(PatchPointOpers::NArgPos).getImm();
359 for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
360 assert(Locations[i].LocType == Location::Register &&
361 "anyreg arg must be in reg.");
365 void StackMaps::recordStatepoint(const MachineInstr &MI) {
366 assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
368 StatepointOpers opers(&MI);
369 // Record all the deopt and gc operands (they're contiguous and run from the
370 // initial index to the end of the operand list)
371 const unsigned StartIdx = opers.getVarIdx();
372 recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
373 MI.operands_end(), false);
376 /// Emit the stackmap header.
379 /// uint8 : Stack Map Version (currently 1)
380 /// uint8 : Reserved (expected to be 0)
381 /// uint16 : Reserved (expected to be 0)
383 /// uint32 : NumFunctions
384 /// uint32 : NumConstants
385 /// uint32 : NumRecords
386 void StackMaps::emitStackmapHeader(MCStreamer &OS) {
388 OS.EmitIntValue(StackMapVersion, 1); // Version.
389 OS.EmitIntValue(0, 1); // Reserved.
390 OS.EmitIntValue(0, 2); // Reserved.
393 DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
394 OS.EmitIntValue(FnStackSize.size(), 4);
396 DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
397 OS.EmitIntValue(ConstPool.size(), 4);
399 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
400 OS.EmitIntValue(CSInfos.size(), 4);
403 /// Emit the function frame record for each function.
405 /// StkSizeRecord[NumFunctions] {
406 /// uint64 : Function Address
407 /// uint64 : Stack Size
409 void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
410 // Function Frame records.
411 DEBUG(dbgs() << WSMP << "functions:\n");
412 for (auto const &FR : FnStackSize) {
413 DEBUG(dbgs() << WSMP << "function addr: " << FR.first
414 << " frame size: " << FR.second);
415 OS.EmitSymbolValue(FR.first, 8);
416 OS.EmitIntValue(FR.second, 8);
420 /// Emit the constant pool.
422 /// int64 : Constants[NumConstants]
423 void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
424 // Constant pool entries.
425 DEBUG(dbgs() << WSMP << "constants:\n");
426 for (auto ConstEntry : ConstPool) {
427 DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
428 OS.EmitIntValue(ConstEntry.second, 8);
432 /// Emit the callsite info for each callsite.
434 /// StkMapRecord[NumRecords] {
435 /// uint64 : PatchPoint ID
436 /// uint32 : Instruction Offset
437 /// uint16 : Reserved (record flags)
438 /// uint16 : NumLocations
439 /// Location[NumLocations] {
440 /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
441 /// uint8 : Size in Bytes
442 /// uint16 : Dwarf RegNum
446 /// uint16 : NumLiveOuts
447 /// LiveOuts[NumLiveOuts] {
448 /// uint16 : Dwarf RegNum
450 /// uint8 : Size in Bytes
452 /// uint32 : Padding (only if required to align to 8 byte)
455 /// Location Encoding, Type, Value:
456 /// 0x1, Register, Reg (value in register)
457 /// 0x2, Direct, Reg + Offset (frame index)
458 /// 0x3, Indirect, [Reg + Offset] (spilled value)
459 /// 0x4, Constant, Offset (small constant)
460 /// 0x5, ConstIndex, Constants[Offset] (large constant)
461 void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
462 DEBUG(print(dbgs()));
464 for (const auto &CSI : CSInfos) {
465 const LocationVec &CSLocs = CSI.Locations;
466 const LiveOutVec &LiveOuts = CSI.LiveOuts;
468 // Verify stack map entry. It's better to communicate a problem to the
469 // runtime than crash in case of in-process compilation. Currently, we do
470 // simple overflow checks, but we may eventually communicate other
471 // compilation errors this way.
472 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
473 OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
474 OS.EmitValue(CSI.CSOffsetExpr, 4);
475 OS.EmitIntValue(0, 2); // Reserved.
476 OS.EmitIntValue(0, 2); // 0 locations.
477 OS.EmitIntValue(0, 2); // padding.
478 OS.EmitIntValue(0, 2); // 0 live-out registers.
479 OS.EmitIntValue(0, 4); // padding.
483 OS.EmitIntValue(CSI.ID, 8);
484 OS.EmitValue(CSI.CSOffsetExpr, 4);
486 // Reserved for flags.
487 OS.EmitIntValue(0, 2);
488 OS.EmitIntValue(CSLocs.size(), 2);
490 for (const auto &Loc : CSLocs) {
491 OS.EmitIntValue(Loc.LocType, 1);
492 OS.EmitIntValue(Loc.Size, 1);
493 OS.EmitIntValue(Loc.Reg, 2);
494 OS.EmitIntValue(Loc.Offset, 4);
497 // Num live-out registers and padding to align to 4 byte.
498 OS.EmitIntValue(0, 2);
499 OS.EmitIntValue(LiveOuts.size(), 2);
501 for (const auto &LO : LiveOuts) {
502 OS.EmitIntValue(LO.RegNo, 2);
503 OS.EmitIntValue(0, 1);
504 OS.EmitIntValue(LO.Size, 1);
506 // Emit alignment to 8 byte.
507 OS.EmitValueToAlignment(8);
511 /// Serialize the stackmap data.
512 void StackMaps::serializeToStackMapSection() {
514 // Bail out if there's no stack map data.
515 assert((!CSInfos.empty() || (CSInfos.empty() && ConstPool.empty())) &&
516 "Expected empty constant pool too!");
517 assert((!CSInfos.empty() || (CSInfos.empty() && FnStackSize.empty())) &&
518 "Expected empty function record too!");
522 MCContext &OutContext = AP.OutStreamer->getContext();
523 MCStreamer &OS = *AP.OutStreamer;
525 // Create the section.
526 MCSection *StackMapSection =
527 OutContext.getObjectFileInfo()->getStackMapSection();
528 OS.SwitchSection(StackMapSection);
530 // Emit a dummy symbol to force section inclusion.
531 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
534 DEBUG(dbgs() << "********** Stack Map Output **********\n");
535 emitStackmapHeader(OS);
536 emitFunctionFrameRecords(OS);
537 emitConstantPoolEntries(OS);
538 emitCallsiteEntries(OS);