1f785676fSDimitry Andric //===---------------------------- StackMaps.cpp ---------------------------===// 2f785676fSDimitry Andric // 3f785676fSDimitry Andric // The LLVM Compiler Infrastructure 4f785676fSDimitry Andric // 5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source 6f785676fSDimitry Andric // License. See LICENSE.TXT for details. 7f785676fSDimitry Andric // 8f785676fSDimitry Andric //===----------------------------------------------------------------------===// 9f785676fSDimitry Andric 10f785676fSDimitry Andric #include "llvm/CodeGen/StackMaps.h" 11f785676fSDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 1291bc56edSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 1391bc56edSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 14f785676fSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 1591bc56edSDimitry Andric #include "llvm/IR/DataLayout.h" 16f785676fSDimitry Andric #include "llvm/MC/MCContext.h" 17f785676fSDimitry Andric #include "llvm/MC/MCExpr.h" 18f785676fSDimitry Andric #include "llvm/MC/MCObjectFileInfo.h" 19f785676fSDimitry Andric #include "llvm/MC/MCSectionMachO.h" 20f785676fSDimitry Andric #include "llvm/MC/MCStreamer.h" 2191bc56edSDimitry Andric #include "llvm/Support/CommandLine.h" 22f785676fSDimitry Andric #include "llvm/Target/TargetMachine.h" 2391bc56edSDimitry Andric #include "llvm/Target/TargetOpcodes.h" 24f785676fSDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 2539d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h" 26f785676fSDimitry Andric #include <iterator> 27f785676fSDimitry Andric 28f785676fSDimitry Andric using namespace llvm; 29f785676fSDimitry Andric 3091bc56edSDimitry Andric #define DEBUG_TYPE "stackmaps" 3191bc56edSDimitry Andric 32875ed548SDimitry Andric static cl::opt<int> StackMapVersion( 33875ed548SDimitry Andric "stackmap-version", cl::init(1), 3491bc56edSDimitry Andric cl::desc("Specify the stackmap encoding version (default = 1)")); 3591bc56edSDimitry Andric 3691bc56edSDimitry Andric const char *StackMaps::WSMP = "Stack Maps: "; 3791bc56edSDimitry Andric 3891bc56edSDimitry Andric PatchPointOpers::PatchPointOpers(const MachineInstr *MI) 39875ed548SDimitry Andric : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() && 40f785676fSDimitry Andric !MI->getOperand(0).isImplicit()), 41875ed548SDimitry Andric IsAnyReg(MI->getOperand(getMetaIdx(CCPos)).getImm() == 42875ed548SDimitry Andric CallingConv::AnyReg) { 4391bc56edSDimitry Andric #ifndef NDEBUG 44f785676fSDimitry Andric unsigned CheckStartIdx = 0, e = MI->getNumOperands(); 45f785676fSDimitry Andric while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() && 46f785676fSDimitry Andric MI->getOperand(CheckStartIdx).isDef() && 47f785676fSDimitry Andric !MI->getOperand(CheckStartIdx).isImplicit()) 48f785676fSDimitry Andric ++CheckStartIdx; 49f785676fSDimitry Andric 50f785676fSDimitry Andric assert(getMetaIdx() == CheckStartIdx && 5191bc56edSDimitry Andric "Unexpected additional definition in Patchpoint intrinsic."); 52f785676fSDimitry Andric #endif 53f785676fSDimitry Andric } 54f785676fSDimitry Andric 55f785676fSDimitry Andric unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const { 56f785676fSDimitry Andric if (!StartIdx) 57f785676fSDimitry Andric StartIdx = getVarIdx(); 58f785676fSDimitry Andric 59f785676fSDimitry Andric // Find the next scratch register (implicit def and early clobber) 60f785676fSDimitry Andric unsigned ScratchIdx = StartIdx, e = MI->getNumOperands(); 61f785676fSDimitry Andric while (ScratchIdx < e && 62f785676fSDimitry Andric !(MI->getOperand(ScratchIdx).isReg() && 63f785676fSDimitry Andric MI->getOperand(ScratchIdx).isDef() && 64f785676fSDimitry Andric MI->getOperand(ScratchIdx).isImplicit() && 65f785676fSDimitry Andric MI->getOperand(ScratchIdx).isEarlyClobber())) 66f785676fSDimitry Andric ++ScratchIdx; 67f785676fSDimitry Andric 68f785676fSDimitry Andric assert(ScratchIdx != e && "No scratch register available"); 69f785676fSDimitry Andric return ScratchIdx; 70f785676fSDimitry Andric } 71f785676fSDimitry Andric 7291bc56edSDimitry Andric StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) { 7391bc56edSDimitry Andric if (StackMapVersion != 1) 7491bc56edSDimitry Andric llvm_unreachable("Unsupported stackmap version!"); 7591bc56edSDimitry Andric } 7691bc56edSDimitry Andric 77ff0cc061SDimitry Andric /// Go up the super-register chain until we hit a valid dwarf register number. 78ff0cc061SDimitry Andric static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) { 79875ed548SDimitry Andric int RegNum = TRI->getDwarfRegNum(Reg, false); 80875ed548SDimitry Andric for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR) 81875ed548SDimitry Andric RegNum = TRI->getDwarfRegNum(*SR, false); 82ff0cc061SDimitry Andric 83875ed548SDimitry Andric assert(RegNum >= 0 && "Invalid Dwarf register number."); 84875ed548SDimitry Andric return (unsigned)RegNum; 85ff0cc061SDimitry Andric } 86ff0cc061SDimitry Andric 8791bc56edSDimitry Andric MachineInstr::const_mop_iterator 8891bc56edSDimitry Andric StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI, 89875ed548SDimitry Andric MachineInstr::const_mop_iterator MOE, LocationVec &Locs, 90875ed548SDimitry Andric LiveOutVec &LiveOuts) const { 91ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo(); 9291bc56edSDimitry Andric if (MOI->isImm()) { 9391bc56edSDimitry Andric switch (MOI->getImm()) { 94875ed548SDimitry Andric default: 95875ed548SDimitry Andric llvm_unreachable("Unrecognized operand type."); 9691bc56edSDimitry Andric case StackMaps::DirectMemRefOp: { 977d523365SDimitry Andric auto &DL = AP.MF->getDataLayout(); 987d523365SDimitry Andric 997d523365SDimitry Andric unsigned Size = DL.getPointerSizeInBits(); 10091bc56edSDimitry Andric assert((Size % 8) == 0 && "Need pointer size in bytes."); 10191bc56edSDimitry Andric Size /= 8; 10291bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg(); 10391bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm(); 104875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Direct, Size, 105875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm); 10691bc56edSDimitry Andric break; 10791bc56edSDimitry Andric } 10891bc56edSDimitry Andric case StackMaps::IndirectMemRefOp: { 10991bc56edSDimitry Andric int64_t Size = (++MOI)->getImm(); 11091bc56edSDimitry Andric assert(Size > 0 && "Need a valid size for indirect memory locations."); 11191bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg(); 11291bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm(); 113875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Indirect, Size, 114875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm); 11591bc56edSDimitry Andric break; 11691bc56edSDimitry Andric } 11791bc56edSDimitry Andric case StackMaps::ConstantOp: { 11891bc56edSDimitry Andric ++MOI; 11991bc56edSDimitry Andric assert(MOI->isImm() && "Expected constant operand."); 12091bc56edSDimitry Andric int64_t Imm = MOI->getImm(); 121875ed548SDimitry Andric Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm); 12291bc56edSDimitry Andric break; 12391bc56edSDimitry Andric } 12491bc56edSDimitry Andric } 12591bc56edSDimitry Andric return ++MOI; 12691bc56edSDimitry Andric } 12791bc56edSDimitry Andric 12891bc56edSDimitry Andric // The physical register number will ultimately be encoded as a DWARF regno. 12991bc56edSDimitry Andric // The stack map also records the size of a spill slot that can hold the 13091bc56edSDimitry Andric // register content. (The runtime can track the actual size of the data type 13191bc56edSDimitry Andric // if it needs to.) 13291bc56edSDimitry Andric if (MOI->isReg()) { 13391bc56edSDimitry Andric // Skip implicit registers (this includes our scratch registers) 13491bc56edSDimitry Andric if (MOI->isImplicit()) 13591bc56edSDimitry Andric return ++MOI; 13691bc56edSDimitry Andric 13791bc56edSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) && 13891bc56edSDimitry Andric "Virtreg operands should have been rewritten before now."); 139ff0cc061SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg()); 14091bc56edSDimitry Andric assert(!MOI->getSubReg() && "Physical subreg still around."); 141ff0cc061SDimitry Andric 142ff0cc061SDimitry Andric unsigned Offset = 0; 143875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI); 144875ed548SDimitry Andric unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false); 145875ed548SDimitry Andric unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg()); 146ff0cc061SDimitry Andric if (SubRegIdx) 147ff0cc061SDimitry Andric Offset = TRI->getSubRegIdxOffset(SubRegIdx); 148ff0cc061SDimitry Andric 149875ed548SDimitry Andric Locs.emplace_back(Location::Register, RC->getSize(), DwarfRegNum, Offset); 15091bc56edSDimitry Andric return ++MOI; 15191bc56edSDimitry Andric } 15291bc56edSDimitry Andric 15391bc56edSDimitry Andric if (MOI->isRegLiveOut()) 15491bc56edSDimitry Andric LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut()); 15591bc56edSDimitry Andric 15691bc56edSDimitry Andric return ++MOI; 15791bc56edSDimitry Andric } 15891bc56edSDimitry Andric 159ff0cc061SDimitry Andric void StackMaps::print(raw_ostream &OS) { 160ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = 161ff0cc061SDimitry Andric AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr; 162ff0cc061SDimitry Andric OS << WSMP << "callsites:\n"; 163ff0cc061SDimitry Andric for (const auto &CSI : CSInfos) { 164ff0cc061SDimitry Andric const LocationVec &CSLocs = CSI.Locations; 165ff0cc061SDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts; 16691bc56edSDimitry Andric 167ff0cc061SDimitry Andric OS << WSMP << "callsite " << CSI.ID << "\n"; 168ff0cc061SDimitry Andric OS << WSMP << " has " << CSLocs.size() << " locations\n"; 169ff0cc061SDimitry Andric 170875ed548SDimitry Andric unsigned Idx = 0; 171ff0cc061SDimitry Andric for (const auto &Loc : CSLocs) { 172875ed548SDimitry Andric OS << WSMP << "\t\tLoc " << Idx << ": "; 173875ed548SDimitry Andric switch (Loc.Type) { 174ff0cc061SDimitry Andric case Location::Unprocessed: 175ff0cc061SDimitry Andric OS << "<Unprocessed operand>"; 176ff0cc061SDimitry Andric break; 177ff0cc061SDimitry Andric case Location::Register: 178ff0cc061SDimitry Andric OS << "Register "; 179ff0cc061SDimitry Andric if (TRI) 180ff0cc061SDimitry Andric OS << TRI->getName(Loc.Reg); 181ff0cc061SDimitry Andric else 182ff0cc061SDimitry Andric OS << Loc.Reg; 183ff0cc061SDimitry Andric break; 184ff0cc061SDimitry Andric case Location::Direct: 185ff0cc061SDimitry Andric OS << "Direct "; 186ff0cc061SDimitry Andric if (TRI) 187ff0cc061SDimitry Andric OS << TRI->getName(Loc.Reg); 188ff0cc061SDimitry Andric else 189ff0cc061SDimitry Andric OS << Loc.Reg; 190ff0cc061SDimitry Andric if (Loc.Offset) 191ff0cc061SDimitry Andric OS << " + " << Loc.Offset; 192ff0cc061SDimitry Andric break; 193ff0cc061SDimitry Andric case Location::Indirect: 194ff0cc061SDimitry Andric OS << "Indirect "; 195ff0cc061SDimitry Andric if (TRI) 196ff0cc061SDimitry Andric OS << TRI->getName(Loc.Reg); 197ff0cc061SDimitry Andric else 198ff0cc061SDimitry Andric OS << Loc.Reg; 199ff0cc061SDimitry Andric OS << "+" << Loc.Offset; 200ff0cc061SDimitry Andric break; 201ff0cc061SDimitry Andric case Location::Constant: 202ff0cc061SDimitry Andric OS << "Constant " << Loc.Offset; 203ff0cc061SDimitry Andric break; 204ff0cc061SDimitry Andric case Location::ConstantIndex: 205ff0cc061SDimitry Andric OS << "Constant Index " << Loc.Offset; 206ff0cc061SDimitry Andric break; 207ff0cc061SDimitry Andric } 208875ed548SDimitry Andric OS << "\t[encoding: .byte " << Loc.Type << ", .byte " << Loc.Size 209ff0cc061SDimitry Andric << ", .short " << Loc.Reg << ", .int " << Loc.Offset << "]\n"; 210875ed548SDimitry Andric Idx++; 211ff0cc061SDimitry Andric } 212ff0cc061SDimitry Andric 213875ed548SDimitry Andric OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n"; 214ff0cc061SDimitry Andric 215875ed548SDimitry Andric Idx = 0; 216ff0cc061SDimitry Andric for (const auto &LO : LiveOuts) { 217875ed548SDimitry Andric OS << WSMP << "\t\tLO " << Idx << ": "; 218ff0cc061SDimitry Andric if (TRI) 219ff0cc061SDimitry Andric OS << TRI->getName(LO.Reg); 220ff0cc061SDimitry Andric else 221ff0cc061SDimitry Andric OS << LO.Reg; 222875ed548SDimitry Andric OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte " 223ff0cc061SDimitry Andric << LO.Size << "]\n"; 224875ed548SDimitry Andric Idx++; 225ff0cc061SDimitry Andric } 226ff0cc061SDimitry Andric } 22791bc56edSDimitry Andric } 22891bc56edSDimitry Andric 22991bc56edSDimitry Andric /// Create a live-out register record for the given register Reg. 23091bc56edSDimitry Andric StackMaps::LiveOutReg 23191bc56edSDimitry Andric StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const { 232875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI); 23391bc56edSDimitry Andric unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize(); 234875ed548SDimitry Andric return LiveOutReg(Reg, DwarfRegNum, Size); 23591bc56edSDimitry Andric } 23691bc56edSDimitry Andric 23791bc56edSDimitry Andric /// Parse the register live-out mask and return a vector of live-out registers 23891bc56edSDimitry Andric /// that need to be recorded in the stackmap. 23991bc56edSDimitry Andric StackMaps::LiveOutVec 24091bc56edSDimitry Andric StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const { 24191bc56edSDimitry Andric assert(Mask && "No register mask specified"); 242ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo(); 24391bc56edSDimitry Andric LiveOutVec LiveOuts; 24491bc56edSDimitry Andric 24591bc56edSDimitry Andric // Create a LiveOutReg for each bit that is set in the register mask. 24691bc56edSDimitry Andric for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) 24791bc56edSDimitry Andric if ((Mask[Reg / 32] >> Reg % 32) & 1) 24891bc56edSDimitry Andric LiveOuts.push_back(createLiveOutReg(Reg, TRI)); 24991bc56edSDimitry Andric 25091bc56edSDimitry Andric // We don't need to keep track of a register if its super-register is already 25191bc56edSDimitry Andric // in the list. Merge entries that refer to the same dwarf register and use 25291bc56edSDimitry Andric // the maximum size that needs to be spilled. 253875ed548SDimitry Andric 254875ed548SDimitry Andric std::sort(LiveOuts.begin(), LiveOuts.end(), 255875ed548SDimitry Andric [](const LiveOutReg &LHS, const LiveOutReg &RHS) { 256875ed548SDimitry Andric // Only sort by the dwarf register number. 257875ed548SDimitry Andric return LHS.DwarfRegNum < RHS.DwarfRegNum; 258875ed548SDimitry Andric }); 259875ed548SDimitry Andric 260875ed548SDimitry Andric for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) { 261875ed548SDimitry Andric for (auto II = std::next(I); II != E; ++II) { 262875ed548SDimitry Andric if (I->DwarfRegNum != II->DwarfRegNum) { 26391bc56edSDimitry Andric // Skip all the now invalid entries. 26491bc56edSDimitry Andric I = --II; 26591bc56edSDimitry Andric break; 26691bc56edSDimitry Andric } 26791bc56edSDimitry Andric I->Size = std::max(I->Size, II->Size); 26891bc56edSDimitry Andric if (TRI->isSuperRegister(I->Reg, II->Reg)) 26991bc56edSDimitry Andric I->Reg = II->Reg; 270875ed548SDimitry Andric II->Reg = 0; // mark for deletion. 27191bc56edSDimitry Andric } 27291bc56edSDimitry Andric } 273875ed548SDimitry Andric 274875ed548SDimitry Andric LiveOuts.erase( 275875ed548SDimitry Andric std::remove_if(LiveOuts.begin(), LiveOuts.end(), 276875ed548SDimitry Andric [](const LiveOutReg &LO) { return LO.Reg == 0; }), 277875ed548SDimitry Andric LiveOuts.end()); 278875ed548SDimitry Andric 27991bc56edSDimitry Andric return LiveOuts; 28091bc56edSDimitry Andric } 28191bc56edSDimitry Andric 28291bc56edSDimitry Andric void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID, 283f785676fSDimitry Andric MachineInstr::const_mop_iterator MOI, 284f785676fSDimitry Andric MachineInstr::const_mop_iterator MOE, 285f785676fSDimitry Andric bool recordResult) { 286f785676fSDimitry Andric 287ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext(); 288ff0cc061SDimitry Andric MCSymbol *MILabel = OutContext.createTempSymbol(); 289ff0cc061SDimitry Andric AP.OutStreamer->EmitLabel(MILabel); 290f785676fSDimitry Andric 29191bc56edSDimitry Andric LocationVec Locations; 29291bc56edSDimitry Andric LiveOutVec LiveOuts; 293f785676fSDimitry Andric 294f785676fSDimitry Andric if (recordResult) { 29591bc56edSDimitry Andric assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value."); 296875ed548SDimitry Andric parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations, 297875ed548SDimitry Andric LiveOuts); 298f785676fSDimitry Andric } 299f785676fSDimitry Andric 30091bc56edSDimitry Andric // Parse operands. 301f785676fSDimitry Andric while (MOI != MOE) { 30291bc56edSDimitry Andric MOI = parseOperand(MOI, MOE, Locations, LiveOuts); 30391bc56edSDimitry Andric } 304f785676fSDimitry Andric 305f785676fSDimitry Andric // Move large constants into the constant pool. 306875ed548SDimitry Andric for (auto &Loc : Locations) { 30791bc56edSDimitry Andric // Constants are encoded as sign-extended integers. 30891bc56edSDimitry Andric // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool. 309875ed548SDimitry Andric if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) { 310875ed548SDimitry Andric Loc.Type = Location::ConstantIndex; 31139d628a0SDimitry Andric // ConstPool is intentionally a MapVector of 'uint64_t's (as 31239d628a0SDimitry Andric // opposed to 'int64_t's). We should never be in a situation 31339d628a0SDimitry Andric // where we have to insert either the tombstone or the empty 31439d628a0SDimitry Andric // keys into a map, and for a DenseMap<uint64_t, T> these are 31539d628a0SDimitry Andric // (uint64_t)0 and (uint64_t)-1. They can be and are 31639d628a0SDimitry Andric // represented using 32 bit integers. 317875ed548SDimitry Andric assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() && 318875ed548SDimitry Andric (uint64_t)Loc.Offset != 319875ed548SDimitry Andric DenseMapInfo<uint64_t>::getTombstoneKey() && 32039d628a0SDimitry Andric "empty and tombstone keys should fit in 32 bits!"); 321875ed548SDimitry Andric auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset)); 322875ed548SDimitry Andric Loc.Offset = Result.first - ConstPool.begin(); 32391bc56edSDimitry Andric } 324f785676fSDimitry Andric } 325f785676fSDimitry Andric 32691bc56edSDimitry Andric // Create an expression to calculate the offset of the callsite from function 32791bc56edSDimitry Andric // entry. 32897bc6c73SDimitry Andric const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub( 32997bc6c73SDimitry Andric MCSymbolRefExpr::create(MILabel, OutContext), 330875ed548SDimitry Andric MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext); 331f785676fSDimitry Andric 33239d628a0SDimitry Andric CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations), 33339d628a0SDimitry Andric std::move(LiveOuts)); 334f785676fSDimitry Andric 33591bc56edSDimitry Andric // Record the stack size of the current function. 33691bc56edSDimitry Andric const MachineFrameInfo *MFI = AP.MF->getFrameInfo(); 33739d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo(); 338875ed548SDimitry Andric bool HasDynamicFrameSize = 339875ed548SDimitry Andric MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF)); 34091bc56edSDimitry Andric FnStackSize[AP.CurrentFnSym] = 341875ed548SDimitry Andric HasDynamicFrameSize ? UINT64_MAX : MFI->getStackSize(); 342f785676fSDimitry Andric } 343f785676fSDimitry Andric 344f785676fSDimitry Andric void StackMaps::recordStackMap(const MachineInstr &MI) { 34591bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap"); 346f785676fSDimitry Andric 347f785676fSDimitry Andric int64_t ID = MI.getOperand(0).getImm(); 34891bc56edSDimitry Andric recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2), 34991bc56edSDimitry Andric MI.operands_end()); 350f785676fSDimitry Andric } 351f785676fSDimitry Andric 352f785676fSDimitry Andric void StackMaps::recordPatchPoint(const MachineInstr &MI) { 35391bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint"); 354f785676fSDimitry Andric 355f785676fSDimitry Andric PatchPointOpers opers(&MI); 356f785676fSDimitry Andric int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm(); 35791bc56edSDimitry Andric 358875ed548SDimitry Andric auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx()); 35991bc56edSDimitry Andric recordStackMapOpers(MI, ID, MOI, MI.operands_end(), 360f785676fSDimitry Andric opers.isAnyReg() && opers.hasDef()); 361f785676fSDimitry Andric 362f785676fSDimitry Andric #ifndef NDEBUG 363f785676fSDimitry Andric // verify anyregcc 364875ed548SDimitry Andric auto &Locations = CSInfos.back().Locations; 365f785676fSDimitry Andric if (opers.isAnyReg()) { 366f785676fSDimitry Andric unsigned NArgs = opers.getMetaOper(PatchPointOpers::NArgPos).getImm(); 367f785676fSDimitry Andric for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i) 368875ed548SDimitry Andric assert(Locations[i].Type == Location::Register && 369f785676fSDimitry Andric "anyreg arg must be in reg."); 370f785676fSDimitry Andric } 371f785676fSDimitry Andric #endif 372f785676fSDimitry Andric } 37339d628a0SDimitry Andric void StackMaps::recordStatepoint(const MachineInstr &MI) { 374875ed548SDimitry Andric assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint"); 37539d628a0SDimitry Andric 37639d628a0SDimitry Andric StatepointOpers opers(&MI); 37739d628a0SDimitry Andric // Record all the deopt and gc operands (they're contiguous and run from the 37839d628a0SDimitry Andric // initial index to the end of the operand list) 37939d628a0SDimitry Andric const unsigned StartIdx = opers.getVarIdx(); 380ff0cc061SDimitry Andric recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx, 381ff0cc061SDimitry Andric MI.operands_end(), false); 38239d628a0SDimitry Andric } 383f785676fSDimitry Andric 38491bc56edSDimitry Andric /// Emit the stackmap header. 385f785676fSDimitry Andric /// 38691bc56edSDimitry Andric /// Header { 38791bc56edSDimitry Andric /// uint8 : Stack Map Version (currently 1) 38891bc56edSDimitry Andric /// uint8 : Reserved (expected to be 0) 38991bc56edSDimitry Andric /// uint16 : Reserved (expected to be 0) 39091bc56edSDimitry Andric /// } 39191bc56edSDimitry Andric /// uint32 : NumFunctions 392f785676fSDimitry Andric /// uint32 : NumConstants 393f785676fSDimitry Andric /// uint32 : NumRecords 39491bc56edSDimitry Andric void StackMaps::emitStackmapHeader(MCStreamer &OS) { 39591bc56edSDimitry Andric // Header. 39691bc56edSDimitry Andric OS.EmitIntValue(StackMapVersion, 1); // Version. 39791bc56edSDimitry Andric OS.EmitIntValue(0, 1); // Reserved. 39891bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved. 39991bc56edSDimitry Andric 40091bc56edSDimitry Andric // Num functions. 40191bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n'); 40291bc56edSDimitry Andric OS.EmitIntValue(FnStackSize.size(), 4); 40391bc56edSDimitry Andric // Num constants. 40491bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n'); 40591bc56edSDimitry Andric OS.EmitIntValue(ConstPool.size(), 4); 40691bc56edSDimitry Andric // Num callsites. 40791bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n'); 40891bc56edSDimitry Andric OS.EmitIntValue(CSInfos.size(), 4); 40991bc56edSDimitry Andric } 41091bc56edSDimitry Andric 41191bc56edSDimitry Andric /// Emit the function frame record for each function. 41291bc56edSDimitry Andric /// 41391bc56edSDimitry Andric /// StkSizeRecord[NumFunctions] { 41491bc56edSDimitry Andric /// uint64 : Function Address 41591bc56edSDimitry Andric /// uint64 : Stack Size 41691bc56edSDimitry Andric /// } 41791bc56edSDimitry Andric void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) { 41891bc56edSDimitry Andric // Function Frame records. 41991bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "functions:\n"); 42091bc56edSDimitry Andric for (auto const &FR : FnStackSize) { 42191bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "function addr: " << FR.first 42291bc56edSDimitry Andric << " frame size: " << FR.second); 42391bc56edSDimitry Andric OS.EmitSymbolValue(FR.first, 8); 42491bc56edSDimitry Andric OS.EmitIntValue(FR.second, 8); 42591bc56edSDimitry Andric } 42691bc56edSDimitry Andric } 42791bc56edSDimitry Andric 42891bc56edSDimitry Andric /// Emit the constant pool. 42991bc56edSDimitry Andric /// 43091bc56edSDimitry Andric /// int64 : Constants[NumConstants] 43191bc56edSDimitry Andric void StackMaps::emitConstantPoolEntries(MCStreamer &OS) { 43291bc56edSDimitry Andric // Constant pool entries. 43391bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "constants:\n"); 434875ed548SDimitry Andric for (const auto &ConstEntry : ConstPool) { 43591bc56edSDimitry Andric DEBUG(dbgs() << WSMP << ConstEntry.second << '\n'); 43691bc56edSDimitry Andric OS.EmitIntValue(ConstEntry.second, 8); 43791bc56edSDimitry Andric } 43891bc56edSDimitry Andric } 43991bc56edSDimitry Andric 44091bc56edSDimitry Andric /// Emit the callsite info for each callsite. 44191bc56edSDimitry Andric /// 442f785676fSDimitry Andric /// StkMapRecord[NumRecords] { 44391bc56edSDimitry Andric /// uint64 : PatchPoint ID 444f785676fSDimitry Andric /// uint32 : Instruction Offset 445f785676fSDimitry Andric /// uint16 : Reserved (record flags) 446f785676fSDimitry Andric /// uint16 : NumLocations 447f785676fSDimitry Andric /// Location[NumLocations] { 448f785676fSDimitry Andric /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex 449f785676fSDimitry Andric /// uint8 : Size in Bytes 450f785676fSDimitry Andric /// uint16 : Dwarf RegNum 451f785676fSDimitry Andric /// int32 : Offset 452f785676fSDimitry Andric /// } 45391bc56edSDimitry Andric /// uint16 : Padding 45491bc56edSDimitry Andric /// uint16 : NumLiveOuts 45591bc56edSDimitry Andric /// LiveOuts[NumLiveOuts] { 45691bc56edSDimitry Andric /// uint16 : Dwarf RegNum 45791bc56edSDimitry Andric /// uint8 : Reserved 45891bc56edSDimitry Andric /// uint8 : Size in Bytes 45991bc56edSDimitry Andric /// } 46091bc56edSDimitry Andric /// uint32 : Padding (only if required to align to 8 byte) 461f785676fSDimitry Andric /// } 462f785676fSDimitry Andric /// 463f785676fSDimitry Andric /// Location Encoding, Type, Value: 464f785676fSDimitry Andric /// 0x1, Register, Reg (value in register) 465f785676fSDimitry Andric /// 0x2, Direct, Reg + Offset (frame index) 466f785676fSDimitry Andric /// 0x3, Indirect, [Reg + Offset] (spilled value) 467f785676fSDimitry Andric /// 0x4, Constant, Offset (small constant) 468f785676fSDimitry Andric /// 0x5, ConstIndex, Constants[Offset] (large constant) 469ff0cc061SDimitry Andric void StackMaps::emitCallsiteEntries(MCStreamer &OS) { 470ff0cc061SDimitry Andric DEBUG(print(dbgs())); 47191bc56edSDimitry Andric // Callsite entries. 47291bc56edSDimitry Andric for (const auto &CSI : CSInfos) { 47391bc56edSDimitry Andric const LocationVec &CSLocs = CSI.Locations; 47491bc56edSDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts; 475f785676fSDimitry Andric 476f785676fSDimitry Andric // Verify stack map entry. It's better to communicate a problem to the 477f785676fSDimitry Andric // runtime than crash in case of in-process compilation. Currently, we do 478f785676fSDimitry Andric // simple overflow checks, but we may eventually communicate other 479f785676fSDimitry Andric // compilation errors this way. 48091bc56edSDimitry Andric if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) { 48191bc56edSDimitry Andric OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID. 48291bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4); 48391bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved. 48491bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 locations. 48591bc56edSDimitry Andric OS.EmitIntValue(0, 2); // padding. 48691bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 live-out registers. 48791bc56edSDimitry Andric OS.EmitIntValue(0, 4); // padding. 488f785676fSDimitry Andric continue; 489f785676fSDimitry Andric } 490f785676fSDimitry Andric 49191bc56edSDimitry Andric OS.EmitIntValue(CSI.ID, 8); 49291bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4); 493f785676fSDimitry Andric 494f785676fSDimitry Andric // Reserved for flags. 49591bc56edSDimitry Andric OS.EmitIntValue(0, 2); 49691bc56edSDimitry Andric OS.EmitIntValue(CSLocs.size(), 2); 497f785676fSDimitry Andric 49891bc56edSDimitry Andric for (const auto &Loc : CSLocs) { 499875ed548SDimitry Andric OS.EmitIntValue(Loc.Type, 1); 50091bc56edSDimitry Andric OS.EmitIntValue(Loc.Size, 1); 501ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Reg, 2); 502ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Offset, 4); 503f785676fSDimitry Andric } 50491bc56edSDimitry Andric 50591bc56edSDimitry Andric // Num live-out registers and padding to align to 4 byte. 50691bc56edSDimitry Andric OS.EmitIntValue(0, 2); 50791bc56edSDimitry Andric OS.EmitIntValue(LiveOuts.size(), 2); 50891bc56edSDimitry Andric 50991bc56edSDimitry Andric for (const auto &LO : LiveOuts) { 510875ed548SDimitry Andric OS.EmitIntValue(LO.DwarfRegNum, 2); 51191bc56edSDimitry Andric OS.EmitIntValue(0, 1); 51291bc56edSDimitry Andric OS.EmitIntValue(LO.Size, 1); 513f785676fSDimitry Andric } 51491bc56edSDimitry Andric // Emit alignment to 8 byte. 51591bc56edSDimitry Andric OS.EmitValueToAlignment(8); 516f785676fSDimitry Andric } 517f785676fSDimitry Andric } 518f785676fSDimitry Andric 51991bc56edSDimitry Andric /// Serialize the stackmap data. 52091bc56edSDimitry Andric void StackMaps::serializeToStackMapSection() { 52191bc56edSDimitry Andric (void)WSMP; 52291bc56edSDimitry Andric // Bail out if there's no stack map data. 5233ca95b02SDimitry Andric assert((!CSInfos.empty() || ConstPool.empty()) && 52491bc56edSDimitry Andric "Expected empty constant pool too!"); 5253ca95b02SDimitry Andric assert((!CSInfos.empty() || FnStackSize.empty()) && 52691bc56edSDimitry Andric "Expected empty function record too!"); 52791bc56edSDimitry Andric if (CSInfos.empty()) 52891bc56edSDimitry Andric return; 529f785676fSDimitry Andric 530ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext(); 531ff0cc061SDimitry Andric MCStreamer &OS = *AP.OutStreamer; 53291bc56edSDimitry Andric 53391bc56edSDimitry Andric // Create the section. 534ff0cc061SDimitry Andric MCSection *StackMapSection = 53591bc56edSDimitry Andric OutContext.getObjectFileInfo()->getStackMapSection(); 53691bc56edSDimitry Andric OS.SwitchSection(StackMapSection); 53791bc56edSDimitry Andric 53891bc56edSDimitry Andric // Emit a dummy symbol to force section inclusion. 539ff0cc061SDimitry Andric OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps"))); 54091bc56edSDimitry Andric 54191bc56edSDimitry Andric // Serialize data. 54291bc56edSDimitry Andric DEBUG(dbgs() << "********** Stack Map Output **********\n"); 54391bc56edSDimitry Andric emitStackmapHeader(OS); 54491bc56edSDimitry Andric emitFunctionFrameRecords(OS); 54591bc56edSDimitry Andric emitConstantPoolEntries(OS); 546ff0cc061SDimitry Andric emitCallsiteEntries(OS); 54791bc56edSDimitry Andric OS.AddBlankLine(); 54891bc56edSDimitry Andric 54991bc56edSDimitry Andric // Clean up. 550f785676fSDimitry Andric CSInfos.clear(); 55191bc56edSDimitry Andric ConstPool.clear(); 552f785676fSDimitry Andric } 553