StackMaps.cpp 18 KB

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