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