StackMaps.cpp 20 KB

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