LLVM 23.0.0git
StackMaps.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/Twine.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCStreamer.h"
27#include "llvm/Support/Debug.h"
31#include <algorithm>
32#include <cassert>
33#include <cstdint>
34#include <iterator>
35#include <utility>
36
37using namespace llvm;
38
39#define DEBUG_TYPE "stackmaps"
40
42 "stackmap-version", cl::init(3), cl::Hidden,
43 cl::desc("Specify the stackmap encoding version (default = 3)"));
44
45const char *StackMaps::WSMP = "Stack Maps: ";
46
47static uint64_t getConstMetaVal(const MachineInstr &MI, unsigned Idx) {
48 assert(MI.getOperand(Idx).isImm() &&
49 MI.getOperand(Idx).getImm() == StackMaps::ConstantOp);
50 const auto &MO = MI.getOperand(Idx + 1);
51 assert(MO.isImm());
52 return MO.getImm();
53}
54
56 : MI(MI) {
57 assert(getVarIdx() <= MI->getNumOperands() &&
58 "invalid stackmap definition");
59}
60
62 : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
63 !MI->getOperand(0).isImplicit()) {
64#ifndef NDEBUG
65 unsigned CheckStartIdx = 0, e = MI->getNumOperands();
66 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
67 MI->getOperand(CheckStartIdx).isDef() &&
68 !MI->getOperand(CheckStartIdx).isImplicit())
69 ++CheckStartIdx;
70
71 assert(getMetaIdx() == CheckStartIdx &&
72 "Unexpected additional definition in Patchpoint intrinsic.");
73#endif
74}
75
76unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
77 if (!StartIdx)
78 StartIdx = getVarIdx();
79
80 // Find the next scratch register (implicit def and early clobber)
81 unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
82 while (ScratchIdx < e &&
83 !(MI->getOperand(ScratchIdx).isReg() &&
84 MI->getOperand(ScratchIdx).isDef() &&
85 MI->getOperand(ScratchIdx).isImplicit() &&
86 MI->getOperand(ScratchIdx).isEarlyClobber()))
87 ++ScratchIdx;
88
89 assert(ScratchIdx != e && "No scratch register available");
90 return ScratchIdx;
91}
92
94 // Take index of num of allocas and skip all allocas records.
95 unsigned CurIdx = getNumAllocaIdx();
96 unsigned NumAllocas = getConstMetaVal(*MI, CurIdx - 1);
97 CurIdx++;
98 while (NumAllocas--)
99 CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
100 return CurIdx + 1; // skip <StackMaps::ConstantOp>
101}
102
104 // Take index of num of gc ptrs and skip all gc ptr records.
105 unsigned CurIdx = getNumGCPtrIdx();
106 unsigned NumGCPtrs = getConstMetaVal(*MI, CurIdx - 1);
107 CurIdx++;
108 while (NumGCPtrs--)
109 CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
110 return CurIdx + 1; // skip <StackMaps::ConstantOp>
111}
112
114 // Take index of num of deopt args and skip all deopt records.
115 unsigned CurIdx = getNumDeoptArgsIdx();
116 unsigned NumDeoptArgs = getConstMetaVal(*MI, CurIdx - 1);
117 CurIdx++;
118 while (NumDeoptArgs--) {
119 CurIdx = StackMaps::getNextMetaArgIdx(MI, CurIdx);
120 }
121 return CurIdx + 1; // skip <StackMaps::ConstantOp>
122}
123
125 unsigned NumGCPtrsIdx = getNumGCPtrIdx();
126 unsigned NumGCPtrs = getConstMetaVal(*MI, NumGCPtrsIdx - 1);
127 if (NumGCPtrs == 0)
128 return -1;
129 ++NumGCPtrsIdx; // skip <num gc ptrs>
130 assert(NumGCPtrsIdx < MI->getNumOperands());
131 return (int)NumGCPtrsIdx;
132}
133
135 SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap) {
136 unsigned CurIdx = getNumGcMapEntriesIdx();
137 unsigned GCMapSize = getConstMetaVal(*MI, CurIdx - 1);
138 CurIdx++;
139 for (unsigned N = 0; N < GCMapSize; ++N) {
140 unsigned B = MI->getOperand(CurIdx++).getImm();
141 unsigned D = MI->getOperand(CurIdx++).getImm();
142 GCMap.push_back(std::make_pair(B, D));
143 }
144
145 return GCMapSize;
146}
147
149 unsigned FoldableAreaStart = getVarIdx();
150 for (const MachineOperand &MO : MI->uses()) {
151 if (MO.getOperandNo() >= FoldableAreaStart)
152 break;
153 if (MO.isReg() && MO.getReg() == Reg)
154 return false;
155 }
156 return true;
157}
158
160 if (MI->getOpcode() != TargetOpcode::STATEPOINT)
161 return false;
162 return StatepointOpers(MI).isFoldableReg(Reg);
163}
164
166 if (StackMapVersion != 3)
167 llvm_unreachable("Unsupported stackmap version!");
168}
169
170unsigned StackMaps::getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx) {
171 assert(CurIdx < MI->getNumOperands() && "Bad meta arg index");
172 const auto &MO = MI->getOperand(CurIdx);
173 if (MO.isImm()) {
174 switch (MO.getImm()) {
175 default:
176 llvm_unreachable("Unrecognized operand type.");
177 case StackMaps::DirectMemRefOp:
178 CurIdx += 2;
179 break;
180 case StackMaps::IndirectMemRefOp:
181 CurIdx += 3;
182 break;
183 case StackMaps::ConstantOp:
184 ++CurIdx;
185 break;
186 }
187 }
188 ++CurIdx;
189 assert(CurIdx < MI->getNumOperands() && "points past operand list");
190 return CurIdx;
191}
192
193/// Go up the super-register chain until we hit a valid dwarf register number.
195 int RegNum;
196 for (MCPhysReg SR : TRI->superregs_inclusive(Reg)) {
197 RegNum = TRI->getDwarfRegNum(SR, false);
198 if (RegNum >= 0)
199 break;
200 }
201
202 assert(RegNum >= 0 && isUInt<16>(RegNum) && "Invalid Dwarf register number.");
203 return (unsigned)RegNum;
204}
205
207StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
208 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
209 LiveOutVec &LiveOuts) {
210 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
211 if (MOI->isImm()) {
212 switch (MOI->getImm()) {
213 default:
214 llvm_unreachable("Unrecognized operand type.");
215 case StackMaps::DirectMemRefOp: {
216 auto &DL = AP.MF->getDataLayout();
217
218 unsigned Size = DL.getPointerSizeInBits();
219 assert((Size % 8) == 0 && "Need pointer size in bytes.");
220 Size /= 8;
221 Register Reg = (++MOI)->getReg();
222 int64_t Imm = (++MOI)->getImm();
223 Locs.emplace_back(StackMaps::Location::Direct, Size,
224 getDwarfRegNum(Reg, TRI), Imm);
225 break;
226 }
227 case StackMaps::IndirectMemRefOp: {
228 int64_t Size = (++MOI)->getImm();
229 assert(Size > 0 && "Need a valid size for indirect memory locations.");
230 Register Reg = (++MOI)->getReg();
231 int64_t Imm = (++MOI)->getImm();
232 Locs.emplace_back(StackMaps::Location::Indirect, Size,
233 getDwarfRegNum(Reg, TRI), Imm);
234 break;
235 }
236 case StackMaps::ConstantOp: {
237 ++MOI;
238 assert(MOI->isImm() && "Expected constant operand.");
239 int64_t Imm = MOI->getImm();
240 if (isInt<32>(Imm)) {
241 Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
242 } else {
243 // ConstPool is intentionally a MapVector of 'uint64_t's (as
244 // opposed to 'int64_t's). We should never be in a situation
245 // where we have to insert the empty key into a map, and for a
246 // DenseMap<uint64_t, T> this is (uint64_t)-1. It can be and is
247 // represented using 32 bit integers.
248 assert((uint64_t)Imm != DenseMapInfo<uint64_t>::getEmptyKey() &&
249 "empty key should fit in 32 bits!");
250 auto Result = ConstPool.insert(std::make_pair(Imm, Imm));
251 Locs.emplace_back(Location::ConstantIndex, sizeof(int64_t), 0,
252 Result.first - ConstPool.begin());
253 }
254 break;
255 }
256 }
257 return ++MOI;
258 }
259
260 // The physical register number will ultimately be encoded as a DWARF regno.
261 // The stack map also records the size of a spill slot that can hold the
262 // register content. (The runtime can track the actual size of the data type
263 // if it needs to.)
264 if (MOI->isReg()) {
265 // Skip implicit registers (this includes our scratch registers)
266 if (MOI->isImplicit())
267 return ++MOI;
268
269 assert(MOI->getReg().isPhysical() &&
270 "Virtreg operands should have been rewritten before now.");
271 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
272 assert(!MOI->getSubReg() && "Physical subreg still around.");
273
274 unsigned Offset = 0;
275 unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
276 MCRegister LLVMRegNum = *TRI->getLLVMRegNum(DwarfRegNum, false);
277 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
278 if (SubRegIdx)
279 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
280
281 Locs.emplace_back(Location::Register, TRI->getSpillSize(*RC),
282 DwarfRegNum, Offset);
283 return ++MOI;
284 }
285
286 if (MOI->isRegLiveOut())
287 LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
288
289 return ++MOI;
290}
291
292void StackMaps::print(raw_ostream &OS) {
293 const TargetRegisterInfo *TRI =
294 AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
295 OS << WSMP << "callsites:\n";
296 for (const auto &CSI : CSInfos) {
297 const LocationVec &CSLocs = CSI.Locations;
298 const LiveOutVec &LiveOuts = CSI.LiveOuts;
299
300 OS << WSMP << "callsite " << CSI.ID << "\n";
301 OS << WSMP << " has " << CSLocs.size() << " locations\n";
302
303 unsigned Idx = 0;
304 for (const auto &Loc : CSLocs) {
305 OS << WSMP << "\t\tLoc " << Idx << ": ";
306 switch (Loc.Type) {
308 OS << "<Unprocessed operand>";
309 break;
311 OS << "Register ";
312 if (TRI)
313 OS << printReg(Loc.Reg, TRI);
314 else
315 OS << Loc.Reg;
316 break;
317 case Location::Direct:
318 OS << "Direct ";
319 if (TRI)
320 OS << printReg(Loc.Reg, TRI);
321 else
322 OS << Loc.Reg;
323 if (Loc.Offset)
324 OS << " + " << Loc.Offset;
325 break;
327 OS << "Indirect ";
328 if (TRI)
329 OS << printReg(Loc.Reg, TRI);
330 else
331 OS << Loc.Reg;
332 OS << "+" << Loc.Offset;
333 break;
335 OS << "Constant " << Loc.Offset;
336 break;
338 OS << "Constant Index " << Loc.Offset;
339 break;
340 }
341 OS << "\t[encoding: .byte " << Loc.Type << ", .byte 0"
342 << ", .short " << Loc.Size << ", .short " << Loc.Reg << ", .short 0"
343 << ", .int " << Loc.Offset << "]\n";
344 Idx++;
345 }
346
347 OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
348
349 Idx = 0;
350 for (const auto &LO : LiveOuts) {
351 OS << WSMP << "\t\tLO " << Idx << ": ";
352 if (TRI)
353 OS << printReg(LO.Reg, TRI);
354 else
355 OS << LO.Reg;
356 OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
357 << LO.Size << "]\n";
358 Idx++;
359 }
360 }
361}
362
363/// Create a live-out register record for the given register Reg.
365StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
366 unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
367 unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg));
368 return LiveOutReg(Reg, DwarfRegNum, Size);
369}
370
371/// Parse the register live-out mask and return a vector of live-out registers
372/// that need to be recorded in the stackmap.
374StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
375 assert(Mask && "No register mask specified");
376 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
377 LiveOutVec LiveOuts;
378
379 // Create a LiveOutReg for each bit that is set in the register mask.
380 for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
381 if ((Mask[Reg / 32] >> (Reg % 32)) & 1)
382 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
383
384 // We don't need to keep track of a register if its super-register is already
385 // in the list. Merge entries that refer to the same dwarf register and use
386 // the maximum size that needs to be spilled.
387
388 llvm::sort(LiveOuts, [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
389 // Only sort by the dwarf register number.
390 return LHS.DwarfRegNum < RHS.DwarfRegNum;
391 });
392
393 for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
394 for (auto *II = std::next(I); II != E; ++II) {
395 if (I->DwarfRegNum != II->DwarfRegNum) {
396 // Skip all the now invalid entries.
397 I = --II;
398 break;
399 }
400 I->Size = std::max(I->Size, II->Size);
401 if (I->Reg && TRI->isSuperRegister(I->Reg, II->Reg))
402 I->Reg = II->Reg;
403 II->Reg = 0; // mark for deletion.
404 }
405 }
406
407 llvm::erase_if(LiveOuts, [](const LiveOutReg &LO) { return LO.Reg == 0; });
408
409 return LiveOuts;
410}
411
412// See statepoint MI format description in StatepointOpers' class comment
413// in include/llvm/CodeGen/StackMaps.h
414void StackMaps::parseStatepointOpers(const MachineInstr &MI,
417 LocationVec &Locations,
418 LiveOutVec &LiveOuts) {
419 LLVM_DEBUG(dbgs() << "record statepoint : " << MI << "\n");
420 StatepointOpers SO(&MI);
421 MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // CC
422 MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // Flags
423 MOI = parseOperand(MOI, MOE, Locations, LiveOuts); // Num Deopts
424
425 // Record Deopt Args.
426 unsigned NumDeoptArgs = Locations.back().Offset;
427 assert(Locations.back().Type == Location::Constant);
428 assert(NumDeoptArgs == SO.getNumDeoptArgs());
429
430 while (NumDeoptArgs--)
431 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
432
433 // Record gc base/derived pairs
434 assert(MOI->isImm() && MOI->getImm() == StackMaps::ConstantOp);
435 ++MOI;
436 assert(MOI->isImm());
437 unsigned NumGCPointers = MOI->getImm();
438 ++MOI;
439 if (NumGCPointers) {
440 // Map logical index of GC ptr to MI operand index.
441 SmallVector<unsigned, 8> GCPtrIndices;
442 unsigned GCPtrIdx = (unsigned)SO.getFirstGCPtrIdx();
443 assert((int)GCPtrIdx != -1);
444 assert(MOI - MI.operands_begin() == GCPtrIdx + 0LL);
445 while (NumGCPointers--) {
446 GCPtrIndices.push_back(GCPtrIdx);
447 GCPtrIdx = StackMaps::getNextMetaArgIdx(&MI, GCPtrIdx);
448 }
449
451 unsigned NumGCPairs = SO.getGCPointerMap(GCPairs);
452 (void)NumGCPairs;
453 LLVM_DEBUG(dbgs() << "NumGCPairs = " << NumGCPairs << "\n");
454
455 auto MOB = MI.operands_begin();
456 for (auto &P : GCPairs) {
457 assert(P.first < GCPtrIndices.size() && "base pointer index not found");
458 assert(P.second < GCPtrIndices.size() &&
459 "derived pointer index not found");
460 unsigned BaseIdx = GCPtrIndices[P.first];
461 unsigned DerivedIdx = GCPtrIndices[P.second];
462 LLVM_DEBUG(dbgs() << "Base : " << BaseIdx << " Derived : " << DerivedIdx
463 << "\n");
464 (void)parseOperand(MOB + BaseIdx, MOE, Locations, LiveOuts);
465 (void)parseOperand(MOB + DerivedIdx, MOE, Locations, LiveOuts);
466 }
467
468 MOI = MOB + GCPtrIdx;
469 }
470
471 // Record gc allocas
472 assert(MOI < MOE);
473 assert(MOI->isImm() && MOI->getImm() == StackMaps::ConstantOp);
474 ++MOI;
475 unsigned NumAllocas = MOI->getImm();
476 ++MOI;
477 while (NumAllocas--) {
478 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
479 assert(MOI < MOE);
480 }
481}
482
483void StackMaps::recordStackMapOpers(const MCSymbol &MILabel,
484 const MachineInstr &MI, uint64_t ID,
487 bool recordResult) {
488 MCContext &OutContext = AP.OutStreamer->getContext();
489
491 LiveOutVec LiveOuts;
492
493 if (recordResult) {
494 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
495 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
496 LiveOuts);
497 }
498
499 // Parse operands.
500 if (MI.getOpcode() == TargetOpcode::STATEPOINT)
501 parseStatepointOpers(MI, MOI, MOE, Locations, LiveOuts);
502 else
503 while (MOI != MOE)
504 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
505
506 // Create an expression to calculate the offset of the callsite from function
507 // entry.
508 const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
509 MCSymbolRefExpr::create(&MILabel, OutContext),
510 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
511
512 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
513 std::move(LiveOuts));
514
515 // Record the stack size of the current function and update callsite count.
516 const MachineFrameInfo &MFI = AP.MF->getFrameInfo();
517 const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
518 bool HasDynamicFrameSize =
519 MFI.hasVarSizedObjects() || RegInfo->hasStackRealignment(*(AP.MF));
520 uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize();
521
522 auto [CurrentIt, Inserted] = FnInfos.try_emplace(AP.CurrentFnSym, FrameSize);
523 if (!Inserted)
524 CurrentIt->second.RecordCount++;
525}
526
528 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
529
530 StackMapOpers opers(&MI);
531 const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm();
532 recordStackMapOpers(L, MI, ID, std::next(MI.operands_begin(),
533 opers.getVarIdx()),
534 MI.operands_end());
535}
536
538 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
539
540 PatchPointOpers opers(&MI);
541 const int64_t ID = opers.getID();
542 auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
543 recordStackMapOpers(L, MI, ID, MOI, MI.operands_end(),
544 opers.isAnyReg() && opers.hasDef());
545
546#ifndef NDEBUG
547 // verify anyregcc
548 auto &Locations = CSInfos.back().Locations;
549 if (opers.isAnyReg()) {
550 unsigned NArgs = opers.getNumCallArgs();
551 for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
552 assert(Locations[i].Type == Location::Register &&
553 "anyreg arg must be in reg.");
554 }
555#endif
556}
557
559 assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
560
561 StatepointOpers opers(&MI);
562 const unsigned StartIdx = opers.getVarIdx();
563 recordStackMapOpers(L, MI, opers.getID(), MI.operands_begin() + StartIdx,
564 MI.operands_end(), false);
565}
566
567/// Emit the stackmap header.
568///
569/// Header {
570/// uint8 : Stack Map Version (currently 3)
571/// uint8 : Reserved (expected to be 0)
572/// uint16 : Reserved (expected to be 0)
573/// }
574/// uint32 : NumFunctions
575/// uint32 : NumConstants
576/// uint32 : NumRecords
577void StackMaps::emitStackmapHeader(MCStreamer &OS) {
578 // Header.
579 OS.emitIntValue(StackMapVersion, 1); // Version.
580 OS.emitIntValue(0, 1); // Reserved.
581 OS.emitInt16(0); // Reserved.
582
583 // Num functions.
584 LLVM_DEBUG(dbgs() << WSMP << "#functions = " << FnInfos.size() << '\n');
585 OS.emitInt32(FnInfos.size());
586 // Num constants.
587 LLVM_DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
588 OS.emitInt32(ConstPool.size());
589 // Num callsites.
590 LLVM_DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
591 OS.emitInt32(CSInfos.size());
592}
593
594/// Emit the function frame record for each function.
595///
596/// StkSizeRecord[NumFunctions] {
597/// uint64 : Function Address
598/// uint64 : Stack Size
599/// uint64 : Record Count
600/// }
601void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
602 // Function Frame records.
603 LLVM_DEBUG(dbgs() << WSMP << "functions:\n");
604 for (auto const &FR : FnInfos) {
605 LLVM_DEBUG(dbgs() << WSMP << "function addr: " << FR.first
606 << " frame size: " << FR.second.StackSize
607 << " callsite count: " << FR.second.RecordCount << '\n');
608 OS.emitSymbolValue(FR.first, 8);
609 OS.emitIntValue(FR.second.StackSize, 8);
610 OS.emitIntValue(FR.second.RecordCount, 8);
611 }
612}
613
614/// Emit the constant pool.
615///
616/// int64 : Constants[NumConstants]
617void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
618 // Constant pool entries.
619 LLVM_DEBUG(dbgs() << WSMP << "constants:\n");
620 for (const auto &ConstEntry : ConstPool) {
621 LLVM_DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
622 OS.emitIntValue(ConstEntry.second, 8);
623 }
624}
625
626/// Emit the callsite info for each callsite.
627///
628/// StkMapRecord[NumRecords] {
629/// uint64 : PatchPoint ID
630/// uint32 : Instruction Offset
631/// uint16 : Reserved (record flags)
632/// uint16 : NumLocations
633/// Location[NumLocations] {
634/// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
635/// uint8 : Size in Bytes
636/// uint16 : Dwarf RegNum
637/// int32 : Offset
638/// }
639/// uint16 : Padding
640/// uint16 : NumLiveOuts
641/// LiveOuts[NumLiveOuts] {
642/// uint16 : Dwarf RegNum
643/// uint8 : Reserved
644/// uint8 : Size in Bytes
645/// }
646/// uint32 : Padding (only if required to align to 8 byte)
647/// }
648///
649/// Location Encoding, Type, Value:
650/// 0x1, Register, Reg (value in register)
651/// 0x2, Direct, Reg + Offset (frame index)
652/// 0x3, Indirect, [Reg + Offset] (spilled value)
653/// 0x4, Constant, Offset (small constant)
654/// 0x5, ConstIndex, Constants[Offset] (large constant)
655void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
656 LLVM_DEBUG(print(dbgs()));
657 // Callsite entries.
658 for (const auto &CSI : CSInfos) {
659 const LocationVec &CSLocs = CSI.Locations;
660 const LiveOutVec &LiveOuts = CSI.LiveOuts;
661
662 // Verify stack map entry. It's better to communicate a problem to the
663 // runtime than crash in case of in-process compilation. Currently, we do
664 // simple overflow checks, but we may eventually communicate other
665 // compilation errors this way.
666 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
667 OS.emitIntValue(UINT64_MAX, 8); // Invalid ID.
668 OS.emitValue(CSI.CSOffsetExpr, 4);
669 OS.emitInt16(0); // Reserved.
670 OS.emitInt16(0); // 0 locations.
671 OS.emitInt16(0); // padding.
672 OS.emitInt16(0); // 0 live-out registers.
673 OS.emitInt32(0); // padding.
674 continue;
675 }
676
677 OS.emitIntValue(CSI.ID, 8);
678 OS.emitValue(CSI.CSOffsetExpr, 4);
679
680 // Reserved for flags.
681 OS.emitInt16(0);
682 OS.emitInt16(CSLocs.size());
683
684 for (const auto &Loc : CSLocs) {
685 OS.emitIntValue(Loc.Type, 1);
686 OS.emitIntValue(0, 1); // Reserved
687 OS.emitInt16(Loc.Size);
688 OS.emitInt16(Loc.Reg);
689 OS.emitInt16(0); // Reserved
690 OS.emitInt32(Loc.Offset);
691 }
692
693 // Emit alignment to 8 byte.
695
696 // Num live-out registers and padding to align to 4 byte.
697 OS.emitInt16(0);
698 OS.emitInt16(LiveOuts.size());
699
700 for (const auto &LO : LiveOuts) {
701 OS.emitInt16(LO.DwarfRegNum);
702 OS.emitIntValue(0, 1);
703 OS.emitIntValue(LO.Size, 1);
704 }
705 // Emit alignment to 8 byte.
707 }
708}
709
710/// Serialize the stackmap data.
712 (void)WSMP;
713 // Bail out if there's no stack map data.
714 assert((!CSInfos.empty() || ConstPool.empty()) &&
715 "Expected empty constant pool too!");
716 assert((!CSInfos.empty() || FnInfos.empty()) &&
717 "Expected empty function record too!");
718 if (CSInfos.empty())
719 return;
720
721 MCContext &OutContext = AP.OutStreamer->getContext();
722 MCStreamer &OS = *AP.OutStreamer;
723
724 // Create the section.
725 MCSection *StackMapSection =
727 OS.switchSection(StackMapSection);
728
729 // Emit a dummy symbol to force section inclusion.
730 OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
731
732 // Serialize data.
733 LLVM_DEBUG(dbgs() << "********** Stack Map Output **********\n");
734 emitStackmapHeader(OS);
735 emitFunctionFrameRecords(OS);
736 emitConstantPoolEntries(OS);
737 emitCallsiteEntries(OS);
738 OS.addBlankLine();
739
740 // Clean up.
741 CSInfos.clear();
742 ConstPool.clear();
743}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines DenseMapInfo traits for DenseMap.
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
uint64_t IntrinsicInst * II
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t getConstMetaVal(const MachineInstr &MI, unsigned Idx)
Definition StackMaps.cpp:47
static cl::opt< int > StackMapVersion("stackmap-version", cl::init(3), cl::Hidden, cl::desc("Specify the stackmap encoding version (default = 3)"))
static unsigned getDwarfRegNum(MCRegister Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:428
Context object for machine code objects.
Definition MCContext.h:83
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:413
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
MCSection * getStackMapSection() const
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual void addBlankLine()
Emit a blank line to a .s file to pretty it up.
Definition MCStreamer.h:415
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
void emitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
void emitInt16(uint64_t Value)
Definition MCStreamer.h:756
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
Definition MCStreamer.h:757
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Representation of each machine instruction.
const MachineOperand * const_mop_iterator
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const uint32_t * getRegLiveOut() const
getRegLiveOut - Returns a bit mask of live-out registers.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isRegLiveOut() const
isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
Register getReg() const
getReg - Returns the register number.
size_type size() const
Definition MapVector.h:58
MI-level patchpoint operands.
Definition StackMaps.h:77
uint32_t getNumCallArgs() const
Return the number of call arguments.
Definition StackMaps.h:122
LLVM_ABI PatchPointOpers(const MachineInstr *MI)
Definition StackMaps.cpp:61
LLVM_ABI unsigned getNextScratchIdx(unsigned StartIdx=0) const
Get the next scratch register operand index.
Definition StackMaps.cpp:76
uint64_t getID() const
Return the ID for the given patchpoint.
Definition StackMaps.h:102
bool isAnyReg() const
Definition StackMaps.h:98
unsigned getStackMapStartIdx() const
Get the index at which stack map locations will be recorded.
Definition StackMaps.h:134
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition StackMaps.h:128
bool hasDef() const
Definition StackMaps.h:99
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
MI-level stackmap operands.
Definition StackMaps.h:36
LLVM_ABI StackMapOpers(const MachineInstr *MI)
Definition StackMaps.cpp:55
unsigned getVarIdx() const
Get the operand index of the variable list of non-argument operands.
Definition StackMaps.h:57
static LLVM_ABI unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx)
Get index of next meta operand.
LLVM_ABI StackMaps(AsmPrinter &AP)
LLVM_ABI void serializeToStackMapSection()
If there is any stack map data, create a stack map section and serialize the map info into it.
SmallVector< LiveOutReg, 8 > LiveOutVec
Definition StackMaps.h:310
SmallVector< Location, 8 > LocationVec
Definition StackMaps.h:309
LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
MI-level Statepoint operands.
Definition StackMaps.h:159
StatepointOpers(const MachineInstr *MI)
Definition StackMaps.h:174
LLVM_ABI unsigned getGCPointerMap(SmallVectorImpl< std::pair< unsigned, unsigned > > &GCMap)
Get vector of base/derived pairs from statepoint.
LLVM_ABI unsigned getNumAllocaIdx()
Get index of number of gc allocas.
LLVM_ABI unsigned getNumGcMapEntriesIdx()
Get index of number of gc map entries.
Definition StackMaps.cpp:93
LLVM_ABI int getFirstGCPtrIdx()
Get index of first GC pointer operand of -1 if there are none.
unsigned getNumDeoptArgsIdx() const
Get index of Number Deopt Arguments operand.
Definition StackMaps.h:200
uint64_t getID() const
Return the ID for the given statepoint.
Definition StackMaps.h:205
LLVM_ABI bool isFoldableReg(Register Reg) const
Return true if Reg is used only in operands which can be folded to stack usage.
unsigned getVarIdx() const
Get starting index of non call related arguments (calling convention, statepoint flags,...
Definition StackMaps.h:189
LLVM_ABI unsigned getNumGCPtrIdx()
Get index of number of GC pointers.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1635
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2191
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
#define N