xref: /llvm-project/llvm/lib/CodeGen/StackMaps.cpp (revision c08d48fc2d7cced7b86043854c235394e87c4506)
1 //===- StackMaps.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/CodeGen/StackMaps.h"
10 #include "llvm/ADT/DenseMapInfo.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/CodeGen/AsmPrinter.h"
14 #include "llvm/CodeGen/MachineFrameInfo.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineOperand.h"
18 #include "llvm/CodeGen/TargetOpcodes.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCObjectFileInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstdint>
35 #include <iterator>
36 #include <utility>
37 
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "stackmaps"
41 
42 static cl::opt<int> StackMapVersion(
43     "stackmap-version", cl::init(3), cl::Hidden,
44     cl::desc("Specify the stackmap encoding version (default = 3)"));
45 
46 const char *StackMaps::WSMP = "Stack Maps: ";
47 
48 static uint64_t getConstMetaVal(const MachineInstr &MI, unsigned Idx) {
49   assert(MI.getOperand(Idx).isImm() &&
50          MI.getOperand(Idx).getImm() == StackMaps::ConstantOp);
51   const auto &MO = MI.getOperand(Idx + 1);
52   assert(MO.isImm());
53   return MO.getImm();
54 }
55 
56 StackMapOpers::StackMapOpers(const MachineInstr *MI)
57   : MI(MI) {
58   assert(getVarIdx() <= MI->getNumOperands() &&
59          "invalid stackmap definition");
60 }
61 
62 PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
63     : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
64                      !MI->getOperand(0).isImplicit()) {
65 #ifndef NDEBUG
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())
70     ++CheckStartIdx;
71 
72   assert(getMetaIdx() == CheckStartIdx &&
73          "Unexpected additional definition in Patchpoint intrinsic.");
74 #endif
75 }
76 
77 unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
78   if (!StartIdx)
79     StartIdx = getVarIdx();
80 
81   // Find the next scratch register (implicit def and early clobber)
82   unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
83   while (ScratchIdx < e &&
84          !(MI->getOperand(ScratchIdx).isReg() &&
85            MI->getOperand(ScratchIdx).isDef() &&
86            MI->getOperand(ScratchIdx).isImplicit() &&
87            MI->getOperand(ScratchIdx).isEarlyClobber()))
88     ++ScratchIdx;
89 
90   assert(ScratchIdx != e && "No scratch register available");
91   return ScratchIdx;
92 }
93 
94 int StatepointOpers::getFirstGCPtrIdx() {
95   unsigned NumDeoptsIdx = getNumDeoptArgsIdx();
96   unsigned NumDeoptArgs = MI->getOperand(NumDeoptsIdx).getImm();
97 
98   unsigned CurIdx = NumDeoptsIdx + 1;
99   while (NumDeoptArgs--) {
100     CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
101   }
102   ++CurIdx; // <StackMaps::ConstantOp>
103   unsigned NumGCPtrs = MI->getOperand(CurIdx).getImm();
104   if (NumGCPtrs == 0)
105     return -1;
106   ++CurIdx; // <num gc ptrs>
107   assert(CurIdx < MI->getNumOperands() && "Index points past operand list");
108   return (int)CurIdx;
109 }
110 
111 unsigned StatepointOpers::getGCPointerMap(
112     SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap) {
113   int FirstGCIdx = getFirstGCPtrIdx();
114   if (FirstGCIdx == -1)
115     return 0;
116   unsigned NumGCPtr = getConstMetaVal(*MI, (unsigned)FirstGCIdx - 2);
117   unsigned CurIdx = (unsigned)FirstGCIdx;
118   while (NumGCPtr--)
119     CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
120 
121   unsigned NumAllocas = getConstMetaVal(*MI, CurIdx);
122   CurIdx += 2;
123   while (NumAllocas--)
124     CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
125 
126   assert(CurIdx < MI->getNumOperands());
127   unsigned GCMapSize = getConstMetaVal(*MI, CurIdx);
128   CurIdx += 2;
129   for (unsigned N = 0; N < GCMapSize; ++N) {
130     unsigned B = MI->getOperand(CurIdx++).getImm();
131     unsigned D = MI->getOperand(CurIdx++).getImm();
132     GCMap.push_back(std::make_pair(B, D));
133   }
134 
135   return GCMapSize;
136 }
137 
138 StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
139   if (StackMapVersion != 3)
140     llvm_unreachable("Unsupported stackmap version!");
141 }
142 
143 unsigned StackMaps::getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx) {
144   assert(CurIdx < MI->getNumOperands() && "Bad meta arg index");
145   const auto &MO = MI->getOperand(CurIdx);
146   if (MO.isImm()) {
147     switch (MO.getImm()) {
148     default:
149       llvm_unreachable("Unrecognized operand type.");
150     case StackMaps::DirectMemRefOp:
151       CurIdx += 2;
152       break;
153     case StackMaps::IndirectMemRefOp:
154       CurIdx += 3;
155       break;
156     case StackMaps::ConstantOp:
157       ++CurIdx;
158       break;
159     }
160   }
161   ++CurIdx;
162   assert(CurIdx < MI->getNumOperands() && "points past operand list");
163   return CurIdx;
164 }
165 
166 /// Go up the super-register chain until we hit a valid dwarf register number.
167 static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
168   int RegNum = TRI->getDwarfRegNum(Reg, false);
169   for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR)
170     RegNum = TRI->getDwarfRegNum(*SR, false);
171 
172   assert(RegNum >= 0 && "Invalid Dwarf register number.");
173   return (unsigned)RegNum;
174 }
175 
176 MachineInstr::const_mop_iterator
177 StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
178                         MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
179                         LiveOutVec &LiveOuts) const {
180   const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
181   if (MOI->isImm()) {
182     switch (MOI->getImm()) {
183     default:
184       llvm_unreachable("Unrecognized operand type.");
185     case StackMaps::DirectMemRefOp: {
186       auto &DL = AP.MF->getDataLayout();
187 
188       unsigned Size = DL.getPointerSizeInBits();
189       assert((Size % 8) == 0 && "Need pointer size in bytes.");
190       Size /= 8;
191       Register Reg = (++MOI)->getReg();
192       int64_t Imm = (++MOI)->getImm();
193       Locs.emplace_back(StackMaps::Location::Direct, Size,
194                         getDwarfRegNum(Reg, TRI), Imm);
195       break;
196     }
197     case StackMaps::IndirectMemRefOp: {
198       int64_t Size = (++MOI)->getImm();
199       assert(Size > 0 && "Need a valid size for indirect memory locations.");
200       Register Reg = (++MOI)->getReg();
201       int64_t Imm = (++MOI)->getImm();
202       Locs.emplace_back(StackMaps::Location::Indirect, Size,
203                         getDwarfRegNum(Reg, TRI), Imm);
204       break;
205     }
206     case StackMaps::ConstantOp: {
207       ++MOI;
208       assert(MOI->isImm() && "Expected constant operand.");
209       int64_t Imm = MOI->getImm();
210       Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
211       break;
212     }
213     }
214     return ++MOI;
215   }
216 
217   // The physical register number will ultimately be encoded as a DWARF regno.
218   // The stack map also records the size of a spill slot that can hold the
219   // register content. (The runtime can track the actual size of the data type
220   // if it needs to.)
221   if (MOI->isReg()) {
222     // Skip implicit registers (this includes our scratch registers)
223     if (MOI->isImplicit())
224       return ++MOI;
225 
226     assert(Register::isPhysicalRegister(MOI->getReg()) &&
227            "Virtreg operands should have been rewritten before now.");
228     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
229     assert(!MOI->getSubReg() && "Physical subreg still around.");
230 
231     unsigned Offset = 0;
232     unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
233     unsigned LLVMRegNum = *TRI->getLLVMRegNum(DwarfRegNum, false);
234     unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
235     if (SubRegIdx)
236       Offset = TRI->getSubRegIdxOffset(SubRegIdx);
237 
238     Locs.emplace_back(Location::Register, TRI->getSpillSize(*RC),
239                       DwarfRegNum, Offset);
240     return ++MOI;
241   }
242 
243   if (MOI->isRegLiveOut())
244     LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
245 
246   return ++MOI;
247 }
248 
249 void StackMaps::print(raw_ostream &OS) {
250   const TargetRegisterInfo *TRI =
251       AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
252   OS << WSMP << "callsites:\n";
253   for (const auto &CSI : CSInfos) {
254     const LocationVec &CSLocs = CSI.Locations;
255     const LiveOutVec &LiveOuts = CSI.LiveOuts;
256 
257     OS << WSMP << "callsite " << CSI.ID << "\n";
258     OS << WSMP << "  has " << CSLocs.size() << " locations\n";
259 
260     unsigned Idx = 0;
261     for (const auto &Loc : CSLocs) {
262       OS << WSMP << "\t\tLoc " << Idx << ": ";
263       switch (Loc.Type) {
264       case Location::Unprocessed:
265         OS << "<Unprocessed operand>";
266         break;
267       case Location::Register:
268         OS << "Register ";
269         if (TRI)
270           OS << printReg(Loc.Reg, TRI);
271         else
272           OS << Loc.Reg;
273         break;
274       case Location::Direct:
275         OS << "Direct ";
276         if (TRI)
277           OS << printReg(Loc.Reg, TRI);
278         else
279           OS << Loc.Reg;
280         if (Loc.Offset)
281           OS << " + " << Loc.Offset;
282         break;
283       case Location::Indirect:
284         OS << "Indirect ";
285         if (TRI)
286           OS << printReg(Loc.Reg, TRI);
287         else
288           OS << Loc.Reg;
289         OS << "+" << Loc.Offset;
290         break;
291       case Location::Constant:
292         OS << "Constant " << Loc.Offset;
293         break;
294       case Location::ConstantIndex:
295         OS << "Constant Index " << Loc.Offset;
296         break;
297       }
298       OS << "\t[encoding: .byte " << Loc.Type << ", .byte 0"
299          << ", .short " << Loc.Size << ", .short " << Loc.Reg << ", .short 0"
300          << ", .int " << Loc.Offset << "]\n";
301       Idx++;
302     }
303 
304     OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
305 
306     Idx = 0;
307     for (const auto &LO : LiveOuts) {
308       OS << WSMP << "\t\tLO " << Idx << ": ";
309       if (TRI)
310         OS << printReg(LO.Reg, TRI);
311       else
312         OS << LO.Reg;
313       OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
314          << LO.Size << "]\n";
315       Idx++;
316     }
317   }
318 }
319 
320 /// Create a live-out register record for the given register Reg.
321 StackMaps::LiveOutReg
322 StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
323   unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
324   unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg));
325   return LiveOutReg(Reg, DwarfRegNum, Size);
326 }
327 
328 /// Parse the register live-out mask and return a vector of live-out registers
329 /// that need to be recorded in the stackmap.
330 StackMaps::LiveOutVec
331 StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
332   assert(Mask && "No register mask specified");
333   const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
334   LiveOutVec LiveOuts;
335 
336   // Create a LiveOutReg for each bit that is set in the register mask.
337   for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
338     if ((Mask[Reg / 32] >> (Reg % 32)) & 1)
339       LiveOuts.push_back(createLiveOutReg(Reg, TRI));
340 
341   // We don't need to keep track of a register if its super-register is already
342   // in the list. Merge entries that refer to the same dwarf register and use
343   // the maximum size that needs to be spilled.
344 
345   llvm::sort(LiveOuts, [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
346     // Only sort by the dwarf register number.
347     return LHS.DwarfRegNum < RHS.DwarfRegNum;
348   });
349 
350   for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
351     for (auto II = std::next(I); II != E; ++II) {
352       if (I->DwarfRegNum != II->DwarfRegNum) {
353         // Skip all the now invalid entries.
354         I = --II;
355         break;
356       }
357       I->Size = std::max(I->Size, II->Size);
358       if (TRI->isSuperRegister(I->Reg, II->Reg))
359         I->Reg = II->Reg;
360       II->Reg = 0; // mark for deletion.
361     }
362   }
363 
364   LiveOuts.erase(
365       llvm::remove_if(LiveOuts,
366                       [](const LiveOutReg &LO) { return LO.Reg == 0; }),
367       LiveOuts.end());
368 
369   return LiveOuts;
370 }
371 
372 // See statepoint MI format description in StatepointOpers' class comment
373 // in include/llvm/CodeGen/StackMaps.h
374 void StackMaps::parseStatepointOpers(const MachineInstr &MI,
375                                      MachineInstr::const_mop_iterator MOI,
376                                      MachineInstr::const_mop_iterator MOE,
377                                      LocationVec &Locations,
378                                      LiveOutVec &LiveOuts) {
379   LLVM_DEBUG(dbgs() << "record statepoint : " << MI << "\n");
380   StatepointOpers SO(&MI);
381   MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // CC
382   MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // Flags
383   MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // Num Deopts
384 
385   // Record Deopt Args.
386   unsigned NumDeoptArgs = Locations.back().Offset;
387   assert(Locations.back().Type = Location::Constant);
388   assert(NumDeoptArgs == SO.getNumDeoptArgs());
389 
390   while (NumDeoptArgs--)
391     MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
392 
393   // Record gc base/derived pairs
394   assert(MOI->isImm() && MOI->getImm() == StackMaps::ConstantOp);
395   ++MOI;
396   assert(MOI->isImm());
397   unsigned NumGCPointers = MOI->getImm();
398   ++MOI;
399   if (NumGCPointers) {
400     // Map logical index of GC ptr to MI operand index.
401     SmallVector<unsigned, 8> GCPtrIndices;
402     unsigned GCPtrIdx = (unsigned)SO.getFirstGCPtrIdx();
403     assert((int)GCPtrIdx != -1);
404     assert(MOI - MI.operands_begin() == GCPtrIdx);
405     while (NumGCPointers--) {
406       GCPtrIndices.push_back(GCPtrIdx);
407       GCPtrIdx = StackMaps::getNextMetaArgIdx(&MI, GCPtrIdx);
408     }
409 
410     SmallVector<std::pair<unsigned, unsigned>, 8> GCPairs;
411     unsigned NumGCPairs = SO.getGCPointerMap(GCPairs);
412     LLVM_DEBUG(dbgs() << "NumGCPairs = " << NumGCPairs << "\n");
413 
414     auto MOB = MI.operands_begin();
415     for (auto &P : GCPairs) {
416       assert(P.first < GCPtrIndices.size() && "base pointer index not found");
417       assert(P.second < GCPtrIndices.size() &&
418              "derived pointer index not found");
419       unsigned BaseIdx = GCPtrIndices[P.first];
420       unsigned DerivedIdx = GCPtrIndices[P.second];
421       LLVM_DEBUG(dbgs() << "Base : " << BaseIdx << " Derived : " << DerivedIdx
422                         << "\n");
423       (void)parseOperand(MOB + BaseIdx, MOE, Locations, LiveOuts);
424       (void)parseOperand(MOB + DerivedIdx, MOE, Locations, LiveOuts);
425     }
426 
427     MOI = MOB + GCPtrIdx;
428   }
429 
430   // Record gc allocas
431   assert(MOI < MOE);
432   assert(MOI->isImm() && MOI->getImm() == StackMaps::ConstantOp);
433   ++MOI;
434   unsigned NumAllocas = MOI->getImm();
435   ++MOI;
436   while (NumAllocas--) {
437     MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
438     assert(MOI < MOE);
439   }
440 }
441 
442 void StackMaps::recordStackMapOpers(const MCSymbol &MILabel,
443                                     const MachineInstr &MI, uint64_t ID,
444                                     MachineInstr::const_mop_iterator MOI,
445                                     MachineInstr::const_mop_iterator MOE,
446                                     bool recordResult) {
447   MCContext &OutContext = AP.OutStreamer->getContext();
448 
449   LocationVec Locations;
450   LiveOutVec LiveOuts;
451 
452   if (recordResult) {
453     assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
454     parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
455                  LiveOuts);
456   }
457 
458   // Parse operands.
459   if (MI.getOpcode() == TargetOpcode::STATEPOINT)
460     parseStatepointOpers(MI, MOI, MOE, Locations, LiveOuts);
461   else
462     while (MOI != MOE)
463       MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
464 
465   // Move large constants into the constant pool.
466   for (auto &Loc : Locations) {
467     // Constants are encoded as sign-extended integers.
468     // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
469     if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
470       Loc.Type = Location::ConstantIndex;
471       // ConstPool is intentionally a MapVector of 'uint64_t's (as
472       // opposed to 'int64_t's).  We should never be in a situation
473       // where we have to insert either the tombstone or the empty
474       // keys into a map, and for a DenseMap<uint64_t, T> these are
475       // (uint64_t)0 and (uint64_t)-1.  They can be and are
476       // represented using 32 bit integers.
477       assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
478              (uint64_t)Loc.Offset !=
479                  DenseMapInfo<uint64_t>::getTombstoneKey() &&
480              "empty and tombstone keys should fit in 32 bits!");
481       auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
482       Loc.Offset = Result.first - ConstPool.begin();
483     }
484   }
485 
486   // Create an expression to calculate the offset of the callsite from function
487   // entry.
488   const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
489       MCSymbolRefExpr::create(&MILabel, OutContext),
490       MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
491 
492   CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
493                        std::move(LiveOuts));
494 
495   // Record the stack size of the current function and update callsite count.
496   const MachineFrameInfo &MFI = AP.MF->getFrameInfo();
497   const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
498   bool HasDynamicFrameSize =
499       MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
500   uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize();
501 
502   auto CurrentIt = FnInfos.find(AP.CurrentFnSym);
503   if (CurrentIt != FnInfos.end())
504     CurrentIt->second.RecordCount++;
505   else
506     FnInfos.insert(std::make_pair(AP.CurrentFnSym, FunctionInfo(FrameSize)));
507 }
508 
509 void StackMaps::recordStackMap(const MCSymbol &L, const MachineInstr &MI) {
510   assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
511 
512   StackMapOpers opers(&MI);
513   const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm();
514   recordStackMapOpers(L, MI, ID, std::next(MI.operands_begin(),
515                                            opers.getVarIdx()),
516                       MI.operands_end());
517 }
518 
519 void StackMaps::recordPatchPoint(const MCSymbol &L, const MachineInstr &MI) {
520   assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
521 
522   PatchPointOpers opers(&MI);
523   const int64_t ID = opers.getID();
524   auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
525   recordStackMapOpers(L, MI, ID, MOI, MI.operands_end(),
526                       opers.isAnyReg() && opers.hasDef());
527 
528 #ifndef NDEBUG
529   // verify anyregcc
530   auto &Locations = CSInfos.back().Locations;
531   if (opers.isAnyReg()) {
532     unsigned NArgs = opers.getNumCallArgs();
533     for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
534       assert(Locations[i].Type == Location::Register &&
535              "anyreg arg must be in reg.");
536   }
537 #endif
538 }
539 
540 void StackMaps::recordStatepoint(const MCSymbol &L, const MachineInstr &MI) {
541   assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
542 
543   StatepointOpers opers(&MI);
544   const unsigned StartIdx = opers.getVarIdx();
545   recordStackMapOpers(L, MI, opers.getID(), MI.operands_begin() + StartIdx,
546                       MI.operands_end(), false);
547 }
548 
549 /// Emit the stackmap header.
550 ///
551 /// Header {
552 ///   uint8  : Stack Map Version (currently 3)
553 ///   uint8  : Reserved (expected to be 0)
554 ///   uint16 : Reserved (expected to be 0)
555 /// }
556 /// uint32 : NumFunctions
557 /// uint32 : NumConstants
558 /// uint32 : NumRecords
559 void StackMaps::emitStackmapHeader(MCStreamer &OS) {
560   // Header.
561   OS.emitIntValue(StackMapVersion, 1); // Version.
562   OS.emitIntValue(0, 1);               // Reserved.
563   OS.emitInt16(0);                     // Reserved.
564 
565   // Num functions.
566   LLVM_DEBUG(dbgs() << WSMP << "#functions = " << FnInfos.size() << '\n');
567   OS.emitInt32(FnInfos.size());
568   // Num constants.
569   LLVM_DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
570   OS.emitInt32(ConstPool.size());
571   // Num callsites.
572   LLVM_DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
573   OS.emitInt32(CSInfos.size());
574 }
575 
576 /// Emit the function frame record for each function.
577 ///
578 /// StkSizeRecord[NumFunctions] {
579 ///   uint64 : Function Address
580 ///   uint64 : Stack Size
581 ///   uint64 : Record Count
582 /// }
583 void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
584   // Function Frame records.
585   LLVM_DEBUG(dbgs() << WSMP << "functions:\n");
586   for (auto const &FR : FnInfos) {
587     LLVM_DEBUG(dbgs() << WSMP << "function addr: " << FR.first
588                       << " frame size: " << FR.second.StackSize
589                       << " callsite count: " << FR.second.RecordCount << '\n');
590     OS.emitSymbolValue(FR.first, 8);
591     OS.emitIntValue(FR.second.StackSize, 8);
592     OS.emitIntValue(FR.second.RecordCount, 8);
593   }
594 }
595 
596 /// Emit the constant pool.
597 ///
598 /// int64  : Constants[NumConstants]
599 void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
600   // Constant pool entries.
601   LLVM_DEBUG(dbgs() << WSMP << "constants:\n");
602   for (const auto &ConstEntry : ConstPool) {
603     LLVM_DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
604     OS.emitIntValue(ConstEntry.second, 8);
605   }
606 }
607 
608 /// Emit the callsite info for each callsite.
609 ///
610 /// StkMapRecord[NumRecords] {
611 ///   uint64 : PatchPoint ID
612 ///   uint32 : Instruction Offset
613 ///   uint16 : Reserved (record flags)
614 ///   uint16 : NumLocations
615 ///   Location[NumLocations] {
616 ///     uint8  : Register | Direct | Indirect | Constant | ConstantIndex
617 ///     uint8  : Size in Bytes
618 ///     uint16 : Dwarf RegNum
619 ///     int32  : Offset
620 ///   }
621 ///   uint16 : Padding
622 ///   uint16 : NumLiveOuts
623 ///   LiveOuts[NumLiveOuts] {
624 ///     uint16 : Dwarf RegNum
625 ///     uint8  : Reserved
626 ///     uint8  : Size in Bytes
627 ///   }
628 ///   uint32 : Padding (only if required to align to 8 byte)
629 /// }
630 ///
631 /// Location Encoding, Type, Value:
632 ///   0x1, Register, Reg                 (value in register)
633 ///   0x2, Direct, Reg + Offset          (frame index)
634 ///   0x3, Indirect, [Reg + Offset]      (spilled value)
635 ///   0x4, Constant, Offset              (small constant)
636 ///   0x5, ConstIndex, Constants[Offset] (large constant)
637 void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
638   LLVM_DEBUG(print(dbgs()));
639   // Callsite entries.
640   for (const auto &CSI : CSInfos) {
641     const LocationVec &CSLocs = CSI.Locations;
642     const LiveOutVec &LiveOuts = CSI.LiveOuts;
643 
644     // Verify stack map entry. It's better to communicate a problem to the
645     // runtime than crash in case of in-process compilation. Currently, we do
646     // simple overflow checks, but we may eventually communicate other
647     // compilation errors this way.
648     if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
649       OS.emitIntValue(UINT64_MAX, 8); // Invalid ID.
650       OS.emitValue(CSI.CSOffsetExpr, 4);
651       OS.emitInt16(0); // Reserved.
652       OS.emitInt16(0); // 0 locations.
653       OS.emitInt16(0); // padding.
654       OS.emitInt16(0); // 0 live-out registers.
655       OS.emitInt32(0); // padding.
656       continue;
657     }
658 
659     OS.emitIntValue(CSI.ID, 8);
660     OS.emitValue(CSI.CSOffsetExpr, 4);
661 
662     // Reserved for flags.
663     OS.emitInt16(0);
664     OS.emitInt16(CSLocs.size());
665 
666     for (const auto &Loc : CSLocs) {
667       OS.emitIntValue(Loc.Type, 1);
668       OS.emitIntValue(0, 1);  // Reserved
669       OS.emitInt16(Loc.Size);
670       OS.emitInt16(Loc.Reg);
671       OS.emitInt16(0); // Reserved
672       OS.emitInt32(Loc.Offset);
673     }
674 
675     // Emit alignment to 8 byte.
676     OS.emitValueToAlignment(8);
677 
678     // Num live-out registers and padding to align to 4 byte.
679     OS.emitInt16(0);
680     OS.emitInt16(LiveOuts.size());
681 
682     for (const auto &LO : LiveOuts) {
683       OS.emitInt16(LO.DwarfRegNum);
684       OS.emitIntValue(0, 1);
685       OS.emitIntValue(LO.Size, 1);
686     }
687     // Emit alignment to 8 byte.
688     OS.emitValueToAlignment(8);
689   }
690 }
691 
692 /// Serialize the stackmap data.
693 void StackMaps::serializeToStackMapSection() {
694   (void)WSMP;
695   // Bail out if there's no stack map data.
696   assert((!CSInfos.empty() || ConstPool.empty()) &&
697          "Expected empty constant pool too!");
698   assert((!CSInfos.empty() || FnInfos.empty()) &&
699          "Expected empty function record too!");
700   if (CSInfos.empty())
701     return;
702 
703   MCContext &OutContext = AP.OutStreamer->getContext();
704   MCStreamer &OS = *AP.OutStreamer;
705 
706   // Create the section.
707   MCSection *StackMapSection =
708       OutContext.getObjectFileInfo()->getStackMapSection();
709   OS.SwitchSection(StackMapSection);
710 
711   // Emit a dummy symbol to force section inclusion.
712   OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
713 
714   // Serialize data.
715   LLVM_DEBUG(dbgs() << "********** Stack Map Output **********\n");
716   emitStackmapHeader(OS);
717   emitFunctionFrameRecords(OS);
718   emitConstantPoolEntries(OS);
719   emitCallsiteEntries(OS);
720   OS.AddBlankLine();
721 
722   // Clean up.
723   CSInfos.clear();
724   ConstPool.clear();
725 }
726