StackMaps.cpp 26 KB

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