StackMaps.cpp 20 KB

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