LCOV - code coverage report
Current view: top level - lib/CodeGen - StackMaps.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 169 233 72.5 %
Date: 2018-10-20 13:21:21 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13