xref: /llvm-project/llvm/lib/CodeGen/StackMaps.cpp (revision e83c4b30ca8f189d5dbd4e0be53110c3a1f7129e)
1 //===---------------------------- StackMaps.cpp ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
26 #include <iterator>
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "stackmaps"
31 
32 static cl::opt<int> StackMapVersion(
33     "stackmap-version", cl::init(1),
34     cl::desc("Specify the stackmap encoding version (default = 1)"));
35 
36 const char *StackMaps::WSMP = "Stack Maps: ";
37 
38 StackMapOpers::StackMapOpers(const MachineInstr *MI)
39   : MI(MI) {
40   assert(getVarIdx() <= MI->getNumOperands() &&
41          "invalid stackmap definition");
42 }
43 
44 PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
45     : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
46                      !MI->getOperand(0).isImplicit()),
47       IsAnyReg(getCallingConv() == CallingConv::AnyReg) {
48 #ifndef NDEBUG
49   unsigned CheckStartIdx = 0, e = MI->getNumOperands();
50   while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
51          MI->getOperand(CheckStartIdx).isDef() &&
52          !MI->getOperand(CheckStartIdx).isImplicit())
53     ++CheckStartIdx;
54 
55   assert(getMetaIdx() == CheckStartIdx &&
56          "Unexpected additional definition in Patchpoint intrinsic.");
57 #endif
58 }
59 
60 unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
61   if (!StartIdx)
62     StartIdx = getVarIdx();
63 
64   // Find the next scratch register (implicit def and early clobber)
65   unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
66   while (ScratchIdx < e &&
67          !(MI->getOperand(ScratchIdx).isReg() &&
68            MI->getOperand(ScratchIdx).isDef() &&
69            MI->getOperand(ScratchIdx).isImplicit() &&
70            MI->getOperand(ScratchIdx).isEarlyClobber()))
71     ++ScratchIdx;
72 
73   assert(ScratchIdx != e && "No scratch register available");
74   return ScratchIdx;
75 }
76 
77 StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
78   if (StackMapVersion != 1)
79     llvm_unreachable("Unsupported stackmap version!");
80 }
81 
82 /// Go up the super-register chain until we hit a valid dwarf register number.
83 static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
84   int RegNum = TRI->getDwarfRegNum(Reg, false);
85   for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR)
86     RegNum = TRI->getDwarfRegNum(*SR, false);
87 
88   assert(RegNum >= 0 && "Invalid Dwarf register number.");
89   return (unsigned)RegNum;
90 }
91 
92 MachineInstr::const_mop_iterator
93 StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
94                         MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
95                         LiveOutVec &LiveOuts) const {
96   const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
97   if (MOI->isImm()) {
98     switch (MOI->getImm()) {
99     default:
100       llvm_unreachable("Unrecognized operand type.");
101     case StackMaps::DirectMemRefOp: {
102       auto &DL = AP.MF->getDataLayout();
103 
104       unsigned Size = DL.getPointerSizeInBits();
105       assert((Size % 8) == 0 && "Need pointer size in bytes.");
106       Size /= 8;
107       unsigned Reg = (++MOI)->getReg();
108       int64_t Imm = (++MOI)->getImm();
109       Locs.emplace_back(StackMaps::Location::Direct, Size,
110                         getDwarfRegNum(Reg, TRI), Imm);
111       break;
112     }
113     case StackMaps::IndirectMemRefOp: {
114       int64_t Size = (++MOI)->getImm();
115       assert(Size > 0 && "Need a valid size for indirect memory locations.");
116       unsigned Reg = (++MOI)->getReg();
117       int64_t Imm = (++MOI)->getImm();
118       Locs.emplace_back(StackMaps::Location::Indirect, Size,
119                         getDwarfRegNum(Reg, TRI), Imm);
120       break;
121     }
122     case StackMaps::ConstantOp: {
123       ++MOI;
124       assert(MOI->isImm() && "Expected constant operand.");
125       int64_t Imm = MOI->getImm();
126       Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
127       break;
128     }
129     }
130     return ++MOI;
131   }
132 
133   // The physical register number will ultimately be encoded as a DWARF regno.
134   // The stack map also records the size of a spill slot that can hold the
135   // register content. (The runtime can track the actual size of the data type
136   // if it needs to.)
137   if (MOI->isReg()) {
138     // Skip implicit registers (this includes our scratch registers)
139     if (MOI->isImplicit())
140       return ++MOI;
141 
142     assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
143            "Virtreg operands should have been rewritten before now.");
144     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
145     assert(!MOI->getSubReg() && "Physical subreg still around.");
146 
147     unsigned Offset = 0;
148     unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
149     unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false);
150     unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
151     if (SubRegIdx)
152       Offset = TRI->getSubRegIdxOffset(SubRegIdx);
153 
154     Locs.emplace_back(Location::Register, RC->getSize(), DwarfRegNum, Offset);
155     return ++MOI;
156   }
157 
158   if (MOI->isRegLiveOut())
159     LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
160 
161   return ++MOI;
162 }
163 
164 void StackMaps::print(raw_ostream &OS) {
165   const TargetRegisterInfo *TRI =
166       AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
167   OS << WSMP << "callsites:\n";
168   for (const auto &CSI : CSInfos) {
169     const LocationVec &CSLocs = CSI.Locations;
170     const LiveOutVec &LiveOuts = CSI.LiveOuts;
171 
172     OS << WSMP << "callsite " << CSI.ID << "\n";
173     OS << WSMP << "  has " << CSLocs.size() << " locations\n";
174 
175     unsigned Idx = 0;
176     for (const auto &Loc : CSLocs) {
177       OS << WSMP << "\t\tLoc " << Idx << ": ";
178       switch (Loc.Type) {
179       case Location::Unprocessed:
180         OS << "<Unprocessed operand>";
181         break;
182       case Location::Register:
183         OS << "Register ";
184         if (TRI)
185           OS << TRI->getName(Loc.Reg);
186         else
187           OS << Loc.Reg;
188         break;
189       case Location::Direct:
190         OS << "Direct ";
191         if (TRI)
192           OS << TRI->getName(Loc.Reg);
193         else
194           OS << Loc.Reg;
195         if (Loc.Offset)
196           OS << " + " << Loc.Offset;
197         break;
198       case Location::Indirect:
199         OS << "Indirect ";
200         if (TRI)
201           OS << TRI->getName(Loc.Reg);
202         else
203           OS << Loc.Reg;
204         OS << "+" << Loc.Offset;
205         break;
206       case Location::Constant:
207         OS << "Constant " << Loc.Offset;
208         break;
209       case Location::ConstantIndex:
210         OS << "Constant Index " << Loc.Offset;
211         break;
212       }
213       OS << "\t[encoding: .byte " << Loc.Type << ", .byte " << Loc.Size
214          << ", .short " << Loc.Reg << ", .int " << Loc.Offset << "]\n";
215       Idx++;
216     }
217 
218     OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
219 
220     Idx = 0;
221     for (const auto &LO : LiveOuts) {
222       OS << WSMP << "\t\tLO " << Idx << ": ";
223       if (TRI)
224         OS << TRI->getName(LO.Reg);
225       else
226         OS << LO.Reg;
227       OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
228          << LO.Size << "]\n";
229       Idx++;
230     }
231   }
232 }
233 
234 /// Create a live-out register record for the given register Reg.
235 StackMaps::LiveOutReg
236 StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
237   unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
238   unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
239   return LiveOutReg(Reg, DwarfRegNum, Size);
240 }
241 
242 /// Parse the register live-out mask and return a vector of live-out registers
243 /// that need to be recorded in the stackmap.
244 StackMaps::LiveOutVec
245 StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
246   assert(Mask && "No register mask specified");
247   const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
248   LiveOutVec LiveOuts;
249 
250   // Create a LiveOutReg for each bit that is set in the register mask.
251   for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
252     if ((Mask[Reg / 32] >> Reg % 32) & 1)
253       LiveOuts.push_back(createLiveOutReg(Reg, TRI));
254 
255   // We don't need to keep track of a register if its super-register is already
256   // in the list. Merge entries that refer to the same dwarf register and use
257   // the maximum size that needs to be spilled.
258 
259   std::sort(LiveOuts.begin(), LiveOuts.end(),
260             [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
261               // Only sort by the dwarf register number.
262               return LHS.DwarfRegNum < RHS.DwarfRegNum;
263             });
264 
265   for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
266     for (auto II = std::next(I); II != E; ++II) {
267       if (I->DwarfRegNum != II->DwarfRegNum) {
268         // Skip all the now invalid entries.
269         I = --II;
270         break;
271       }
272       I->Size = std::max(I->Size, II->Size);
273       if (TRI->isSuperRegister(I->Reg, II->Reg))
274         I->Reg = II->Reg;
275       II->Reg = 0; // mark for deletion.
276     }
277   }
278 
279   LiveOuts.erase(
280       remove_if(LiveOuts, [](const LiveOutReg &LO) { return LO.Reg == 0; }),
281       LiveOuts.end());
282 
283   return LiveOuts;
284 }
285 
286 void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
287                                     MachineInstr::const_mop_iterator MOI,
288                                     MachineInstr::const_mop_iterator MOE,
289                                     bool recordResult) {
290 
291   MCContext &OutContext = AP.OutStreamer->getContext();
292   MCSymbol *MILabel = OutContext.createTempSymbol();
293   AP.OutStreamer->EmitLabel(MILabel);
294 
295   LocationVec Locations;
296   LiveOutVec LiveOuts;
297 
298   if (recordResult) {
299     assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
300     parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
301                  LiveOuts);
302   }
303 
304   // Parse operands.
305   while (MOI != MOE) {
306     MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
307   }
308 
309   // Move large constants into the constant pool.
310   for (auto &Loc : Locations) {
311     // Constants are encoded as sign-extended integers.
312     // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
313     if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
314       Loc.Type = Location::ConstantIndex;
315       // ConstPool is intentionally a MapVector of 'uint64_t's (as
316       // opposed to 'int64_t's).  We should never be in a situation
317       // where we have to insert either the tombstone or the empty
318       // keys into a map, and for a DenseMap<uint64_t, T> these are
319       // (uint64_t)0 and (uint64_t)-1.  They can be and are
320       // represented using 32 bit integers.
321       assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
322              (uint64_t)Loc.Offset !=
323                  DenseMapInfo<uint64_t>::getTombstoneKey() &&
324              "empty and tombstone keys should fit in 32 bits!");
325       auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
326       Loc.Offset = Result.first - ConstPool.begin();
327     }
328   }
329 
330   // Create an expression to calculate the offset of the callsite from function
331   // entry.
332   const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
333       MCSymbolRefExpr::create(MILabel, OutContext),
334       MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
335 
336   CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
337                        std::move(LiveOuts));
338 
339   // Record the stack size of the current function.
340   const MachineFrameInfo &MFI = AP.MF->getFrameInfo();
341   const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
342   bool HasDynamicFrameSize =
343       MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
344   FnStackSize[AP.CurrentFnSym] =
345       HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize();
346 }
347 
348 void StackMaps::recordStackMap(const MachineInstr &MI) {
349   assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
350 
351   StackMapOpers opers(&MI);
352   const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm();
353   recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), opers.getVarIdx()),
354                       MI.operands_end());
355 }
356 
357 void StackMaps::recordPatchPoint(const MachineInstr &MI) {
358   assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
359 
360   PatchPointOpers opers(&MI);
361   const int64_t ID = opers.getID();
362   auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
363   recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
364                       opers.isAnyReg() && opers.hasDef());
365 
366 #ifndef NDEBUG
367   // verify anyregcc
368   auto &Locations = CSInfos.back().Locations;
369   if (opers.isAnyReg()) {
370     unsigned NArgs = opers.getNumCallArgs();
371     for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
372       assert(Locations[i].Type == Location::Register &&
373              "anyreg arg must be in reg.");
374   }
375 #endif
376 }
377 void StackMaps::recordStatepoint(const MachineInstr &MI) {
378   assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
379 
380   StatepointOpers opers(&MI);
381   // Record all the deopt and gc operands (they're contiguous and run from the
382   // initial index to the end of the operand list)
383   const unsigned StartIdx = opers.getVarIdx();
384   recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
385                       MI.operands_end(), false);
386 }
387 
388 /// Emit the stackmap header.
389 ///
390 /// Header {
391 ///   uint8  : Stack Map Version (currently 1)
392 ///   uint8  : Reserved (expected to be 0)
393 ///   uint16 : Reserved (expected to be 0)
394 /// }
395 /// uint32 : NumFunctions
396 /// uint32 : NumConstants
397 /// uint32 : NumRecords
398 void StackMaps::emitStackmapHeader(MCStreamer &OS) {
399   // Header.
400   OS.EmitIntValue(StackMapVersion, 1); // Version.
401   OS.EmitIntValue(0, 1);               // Reserved.
402   OS.EmitIntValue(0, 2);               // Reserved.
403 
404   // Num functions.
405   DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
406   OS.EmitIntValue(FnStackSize.size(), 4);
407   // Num constants.
408   DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
409   OS.EmitIntValue(ConstPool.size(), 4);
410   // Num callsites.
411   DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
412   OS.EmitIntValue(CSInfos.size(), 4);
413 }
414 
415 /// Emit the function frame record for each function.
416 ///
417 /// StkSizeRecord[NumFunctions] {
418 ///   uint64 : Function Address
419 ///   uint64 : Stack Size
420 /// }
421 void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
422   // Function Frame records.
423   DEBUG(dbgs() << WSMP << "functions:\n");
424   for (auto const &FR : FnStackSize) {
425     DEBUG(dbgs() << WSMP << "function addr: " << FR.first
426                  << " frame size: " << FR.second);
427     OS.EmitSymbolValue(FR.first, 8);
428     OS.EmitIntValue(FR.second, 8);
429   }
430 }
431 
432 /// Emit the constant pool.
433 ///
434 /// int64  : Constants[NumConstants]
435 void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
436   // Constant pool entries.
437   DEBUG(dbgs() << WSMP << "constants:\n");
438   for (const auto &ConstEntry : ConstPool) {
439     DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
440     OS.EmitIntValue(ConstEntry.second, 8);
441   }
442 }
443 
444 /// Emit the callsite info for each callsite.
445 ///
446 /// StkMapRecord[NumRecords] {
447 ///   uint64 : PatchPoint ID
448 ///   uint32 : Instruction Offset
449 ///   uint16 : Reserved (record flags)
450 ///   uint16 : NumLocations
451 ///   Location[NumLocations] {
452 ///     uint8  : Register | Direct | Indirect | Constant | ConstantIndex
453 ///     uint8  : Size in Bytes
454 ///     uint16 : Dwarf RegNum
455 ///     int32  : Offset
456 ///   }
457 ///   uint16 : Padding
458 ///   uint16 : NumLiveOuts
459 ///   LiveOuts[NumLiveOuts] {
460 ///     uint16 : Dwarf RegNum
461 ///     uint8  : Reserved
462 ///     uint8  : Size in Bytes
463 ///   }
464 ///   uint32 : Padding (only if required to align to 8 byte)
465 /// }
466 ///
467 /// Location Encoding, Type, Value:
468 ///   0x1, Register, Reg                 (value in register)
469 ///   0x2, Direct, Reg + Offset          (frame index)
470 ///   0x3, Indirect, [Reg + Offset]      (spilled value)
471 ///   0x4, Constant, Offset              (small constant)
472 ///   0x5, ConstIndex, Constants[Offset] (large constant)
473 void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
474   DEBUG(print(dbgs()));
475   // Callsite entries.
476   for (const auto &CSI : CSInfos) {
477     const LocationVec &CSLocs = CSI.Locations;
478     const LiveOutVec &LiveOuts = CSI.LiveOuts;
479 
480     // Verify stack map entry. It's better to communicate a problem to the
481     // runtime than crash in case of in-process compilation. Currently, we do
482     // simple overflow checks, but we may eventually communicate other
483     // compilation errors this way.
484     if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
485       OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
486       OS.EmitValue(CSI.CSOffsetExpr, 4);
487       OS.EmitIntValue(0, 2); // Reserved.
488       OS.EmitIntValue(0, 2); // 0 locations.
489       OS.EmitIntValue(0, 2); // padding.
490       OS.EmitIntValue(0, 2); // 0 live-out registers.
491       OS.EmitIntValue(0, 4); // padding.
492       continue;
493     }
494 
495     OS.EmitIntValue(CSI.ID, 8);
496     OS.EmitValue(CSI.CSOffsetExpr, 4);
497 
498     // Reserved for flags.
499     OS.EmitIntValue(0, 2);
500     OS.EmitIntValue(CSLocs.size(), 2);
501 
502     for (const auto &Loc : CSLocs) {
503       OS.EmitIntValue(Loc.Type, 1);
504       OS.EmitIntValue(Loc.Size, 1);
505       OS.EmitIntValue(Loc.Reg, 2);
506       OS.EmitIntValue(Loc.Offset, 4);
507     }
508 
509     // Num live-out registers and padding to align to 4 byte.
510     OS.EmitIntValue(0, 2);
511     OS.EmitIntValue(LiveOuts.size(), 2);
512 
513     for (const auto &LO : LiveOuts) {
514       OS.EmitIntValue(LO.DwarfRegNum, 2);
515       OS.EmitIntValue(0, 1);
516       OS.EmitIntValue(LO.Size, 1);
517     }
518     // Emit alignment to 8 byte.
519     OS.EmitValueToAlignment(8);
520   }
521 }
522 
523 /// Serialize the stackmap data.
524 void StackMaps::serializeToStackMapSection() {
525   (void)WSMP;
526   // Bail out if there's no stack map data.
527   assert((!CSInfos.empty() || ConstPool.empty()) &&
528          "Expected empty constant pool too!");
529   assert((!CSInfos.empty() || FnStackSize.empty()) &&
530          "Expected empty function record too!");
531   if (CSInfos.empty())
532     return;
533 
534   MCContext &OutContext = AP.OutStreamer->getContext();
535   MCStreamer &OS = *AP.OutStreamer;
536 
537   // Create the section.
538   MCSection *StackMapSection =
539       OutContext.getObjectFileInfo()->getStackMapSection();
540   OS.SwitchSection(StackMapSection);
541 
542   // Emit a dummy symbol to force section inclusion.
543   OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
544 
545   // Serialize data.
546   DEBUG(dbgs() << "********** Stack Map Output **********\n");
547   emitStackmapHeader(OS);
548   emitFunctionFrameRecords(OS);
549   emitConstantPoolEntries(OS);
550   emitCallsiteEntries(OS);
551   OS.AddBlankLine();
552 
553   // Clean up.
554   CSInfos.clear();
555   ConstPool.clear();
556 }
557