17a7e6055SDimitry 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 10db17bf38SDimitry Andric #include "llvm/CodeGen/StackMaps.h" 117a7e6055SDimitry Andric #include "llvm/ADT/DenseMapInfo.h" 127a7e6055SDimitry Andric #include "llvm/ADT/STLExtras.h" 137a7e6055SDimitry Andric #include "llvm/ADT/Twine.h" 14f785676fSDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 1591bc56edSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 1691bc56edSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 17f785676fSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 187a7e6055SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 192cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 202cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 212cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 2291bc56edSDimitry Andric #include "llvm/IR/DataLayout.h" 23f785676fSDimitry Andric #include "llvm/MC/MCContext.h" 24f785676fSDimitry Andric #include "llvm/MC/MCExpr.h" 25f785676fSDimitry Andric #include "llvm/MC/MCObjectFileInfo.h" 267a7e6055SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 27f785676fSDimitry Andric #include "llvm/MC/MCStreamer.h" 2891bc56edSDimitry Andric #include "llvm/Support/CommandLine.h" 297a7e6055SDimitry Andric #include "llvm/Support/Debug.h" 307a7e6055SDimitry Andric #include "llvm/Support/ErrorHandling.h" 317a7e6055SDimitry Andric #include "llvm/Support/MathExtras.h" 327a7e6055SDimitry Andric #include "llvm/Support/raw_ostream.h" 337a7e6055SDimitry Andric #include <algorithm> 347a7e6055SDimitry Andric #include <cassert> 357a7e6055SDimitry Andric #include <cstdint> 36f785676fSDimitry Andric #include <iterator> 377a7e6055SDimitry Andric #include <utility> 38f785676fSDimitry Andric 39f785676fSDimitry Andric using namespace llvm; 40f785676fSDimitry Andric 4191bc56edSDimitry Andric #define DEBUG_TYPE "stackmaps" 4291bc56edSDimitry Andric 43875ed548SDimitry Andric static cl::opt<int> StackMapVersion( 442cab237bSDimitry Andric "stackmap-version", cl::init(3), cl::Hidden, 45f37b6182SDimitry Andric cl::desc("Specify the stackmap encoding version (default = 3)")); 4691bc56edSDimitry Andric 4791bc56edSDimitry Andric const char *StackMaps::WSMP = "Stack Maps: "; 4891bc56edSDimitry Andric 49d88c1a5aSDimitry Andric StackMapOpers::StackMapOpers(const MachineInstr *MI) 50d88c1a5aSDimitry Andric : MI(MI) { 51d88c1a5aSDimitry Andric assert(getVarIdx() <= MI->getNumOperands() && 52d88c1a5aSDimitry Andric "invalid stackmap definition"); 53d88c1a5aSDimitry Andric } 54d88c1a5aSDimitry Andric 5591bc56edSDimitry Andric PatchPointOpers::PatchPointOpers(const MachineInstr *MI) 56875ed548SDimitry Andric : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() && 57d88c1a5aSDimitry Andric !MI->getOperand(0).isImplicit()) { 5891bc56edSDimitry Andric #ifndef NDEBUG 59f785676fSDimitry Andric unsigned CheckStartIdx = 0, e = MI->getNumOperands(); 60f785676fSDimitry Andric while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() && 61f785676fSDimitry Andric MI->getOperand(CheckStartIdx).isDef() && 62f785676fSDimitry Andric !MI->getOperand(CheckStartIdx).isImplicit()) 63f785676fSDimitry Andric ++CheckStartIdx; 64f785676fSDimitry Andric 65f785676fSDimitry Andric assert(getMetaIdx() == CheckStartIdx && 6691bc56edSDimitry Andric "Unexpected additional definition in Patchpoint intrinsic."); 67f785676fSDimitry Andric #endif 68f785676fSDimitry Andric } 69f785676fSDimitry Andric 70f785676fSDimitry Andric unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const { 71f785676fSDimitry Andric if (!StartIdx) 72f785676fSDimitry Andric StartIdx = getVarIdx(); 73f785676fSDimitry Andric 74f785676fSDimitry Andric // Find the next scratch register (implicit def and early clobber) 75f785676fSDimitry Andric unsigned ScratchIdx = StartIdx, e = MI->getNumOperands(); 76f785676fSDimitry Andric while (ScratchIdx < e && 77f785676fSDimitry Andric !(MI->getOperand(ScratchIdx).isReg() && 78f785676fSDimitry Andric MI->getOperand(ScratchIdx).isDef() && 79f785676fSDimitry Andric MI->getOperand(ScratchIdx).isImplicit() && 80f785676fSDimitry Andric MI->getOperand(ScratchIdx).isEarlyClobber())) 81f785676fSDimitry Andric ++ScratchIdx; 82f785676fSDimitry Andric 83f785676fSDimitry Andric assert(ScratchIdx != e && "No scratch register available"); 84f785676fSDimitry Andric return ScratchIdx; 85f785676fSDimitry Andric } 86f785676fSDimitry Andric 8791bc56edSDimitry Andric StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) { 88f37b6182SDimitry Andric if (StackMapVersion != 3) 8991bc56edSDimitry Andric llvm_unreachable("Unsupported stackmap version!"); 9091bc56edSDimitry Andric } 9191bc56edSDimitry Andric 92ff0cc061SDimitry Andric /// Go up the super-register chain until we hit a valid dwarf register number. 93ff0cc061SDimitry Andric static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) { 94875ed548SDimitry Andric int RegNum = TRI->getDwarfRegNum(Reg, false); 95875ed548SDimitry Andric for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR) 96875ed548SDimitry Andric RegNum = TRI->getDwarfRegNum(*SR, false); 97ff0cc061SDimitry Andric 98875ed548SDimitry Andric assert(RegNum >= 0 && "Invalid Dwarf register number."); 99875ed548SDimitry Andric return (unsigned)RegNum; 100ff0cc061SDimitry Andric } 101ff0cc061SDimitry Andric 10291bc56edSDimitry Andric MachineInstr::const_mop_iterator 10391bc56edSDimitry Andric StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI, 104875ed548SDimitry Andric MachineInstr::const_mop_iterator MOE, LocationVec &Locs, 105875ed548SDimitry Andric LiveOutVec &LiveOuts) const { 106ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo(); 10791bc56edSDimitry Andric if (MOI->isImm()) { 10891bc56edSDimitry Andric switch (MOI->getImm()) { 109875ed548SDimitry Andric default: 110875ed548SDimitry Andric llvm_unreachable("Unrecognized operand type."); 11191bc56edSDimitry Andric case StackMaps::DirectMemRefOp: { 1127d523365SDimitry Andric auto &DL = AP.MF->getDataLayout(); 1137d523365SDimitry Andric 1147d523365SDimitry Andric unsigned Size = DL.getPointerSizeInBits(); 11591bc56edSDimitry Andric assert((Size % 8) == 0 && "Need pointer size in bytes."); 11691bc56edSDimitry Andric Size /= 8; 11791bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg(); 11891bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm(); 119875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Direct, Size, 120875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm); 12191bc56edSDimitry Andric break; 12291bc56edSDimitry Andric } 12391bc56edSDimitry Andric case StackMaps::IndirectMemRefOp: { 12491bc56edSDimitry Andric int64_t Size = (++MOI)->getImm(); 12591bc56edSDimitry Andric assert(Size > 0 && "Need a valid size for indirect memory locations."); 12691bc56edSDimitry Andric unsigned Reg = (++MOI)->getReg(); 12791bc56edSDimitry Andric int64_t Imm = (++MOI)->getImm(); 128875ed548SDimitry Andric Locs.emplace_back(StackMaps::Location::Indirect, Size, 129875ed548SDimitry Andric getDwarfRegNum(Reg, TRI), Imm); 13091bc56edSDimitry Andric break; 13191bc56edSDimitry Andric } 13291bc56edSDimitry Andric case StackMaps::ConstantOp: { 13391bc56edSDimitry Andric ++MOI; 13491bc56edSDimitry Andric assert(MOI->isImm() && "Expected constant operand."); 13591bc56edSDimitry Andric int64_t Imm = MOI->getImm(); 136875ed548SDimitry Andric Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm); 13791bc56edSDimitry Andric break; 13891bc56edSDimitry Andric } 13991bc56edSDimitry Andric } 14091bc56edSDimitry Andric return ++MOI; 14191bc56edSDimitry Andric } 14291bc56edSDimitry Andric 14391bc56edSDimitry Andric // The physical register number will ultimately be encoded as a DWARF regno. 14491bc56edSDimitry Andric // The stack map also records the size of a spill slot that can hold the 14591bc56edSDimitry Andric // register content. (The runtime can track the actual size of the data type 14691bc56edSDimitry Andric // if it needs to.) 14791bc56edSDimitry Andric if (MOI->isReg()) { 14891bc56edSDimitry Andric // Skip implicit registers (this includes our scratch registers) 14991bc56edSDimitry Andric if (MOI->isImplicit()) 15091bc56edSDimitry Andric return ++MOI; 15191bc56edSDimitry Andric 15291bc56edSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) && 15391bc56edSDimitry Andric "Virtreg operands should have been rewritten before now."); 154ff0cc061SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg()); 15591bc56edSDimitry Andric assert(!MOI->getSubReg() && "Physical subreg still around."); 156ff0cc061SDimitry Andric 157ff0cc061SDimitry Andric unsigned Offset = 0; 158875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI); 159875ed548SDimitry Andric unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false); 160875ed548SDimitry Andric unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg()); 161ff0cc061SDimitry Andric if (SubRegIdx) 162ff0cc061SDimitry Andric Offset = TRI->getSubRegIdxOffset(SubRegIdx); 163ff0cc061SDimitry Andric 16451690af2SDimitry Andric Locs.emplace_back(Location::Register, TRI->getSpillSize(*RC), 16551690af2SDimitry Andric DwarfRegNum, Offset); 16691bc56edSDimitry Andric return ++MOI; 16791bc56edSDimitry Andric } 16891bc56edSDimitry Andric 16991bc56edSDimitry Andric if (MOI->isRegLiveOut()) 17091bc56edSDimitry Andric LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut()); 17191bc56edSDimitry Andric 17291bc56edSDimitry Andric return ++MOI; 17391bc56edSDimitry Andric } 17491bc56edSDimitry Andric 175ff0cc061SDimitry Andric void StackMaps::print(raw_ostream &OS) { 176ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = 177ff0cc061SDimitry Andric AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr; 178ff0cc061SDimitry Andric OS << WSMP << "callsites:\n"; 179ff0cc061SDimitry Andric for (const auto &CSI : CSInfos) { 180ff0cc061SDimitry Andric const LocationVec &CSLocs = CSI.Locations; 181ff0cc061SDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts; 18291bc56edSDimitry Andric 183ff0cc061SDimitry Andric OS << WSMP << "callsite " << CSI.ID << "\n"; 184ff0cc061SDimitry Andric OS << WSMP << " has " << CSLocs.size() << " locations\n"; 185ff0cc061SDimitry Andric 186875ed548SDimitry Andric unsigned Idx = 0; 187ff0cc061SDimitry Andric for (const auto &Loc : CSLocs) { 188875ed548SDimitry Andric OS << WSMP << "\t\tLoc " << Idx << ": "; 189875ed548SDimitry Andric switch (Loc.Type) { 190ff0cc061SDimitry Andric case Location::Unprocessed: 191ff0cc061SDimitry Andric OS << "<Unprocessed operand>"; 192ff0cc061SDimitry Andric break; 193ff0cc061SDimitry Andric case Location::Register: 194ff0cc061SDimitry Andric OS << "Register "; 195ff0cc061SDimitry Andric if (TRI) 1962cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI); 197ff0cc061SDimitry Andric else 198ff0cc061SDimitry Andric OS << Loc.Reg; 199ff0cc061SDimitry Andric break; 200ff0cc061SDimitry Andric case Location::Direct: 201ff0cc061SDimitry Andric OS << "Direct "; 202ff0cc061SDimitry Andric if (TRI) 2032cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI); 204ff0cc061SDimitry Andric else 205ff0cc061SDimitry Andric OS << Loc.Reg; 206ff0cc061SDimitry Andric if (Loc.Offset) 207ff0cc061SDimitry Andric OS << " + " << Loc.Offset; 208ff0cc061SDimitry Andric break; 209ff0cc061SDimitry Andric case Location::Indirect: 210ff0cc061SDimitry Andric OS << "Indirect "; 211ff0cc061SDimitry Andric if (TRI) 2122cab237bSDimitry Andric OS << printReg(Loc.Reg, TRI); 213ff0cc061SDimitry Andric else 214ff0cc061SDimitry Andric OS << Loc.Reg; 215ff0cc061SDimitry Andric OS << "+" << Loc.Offset; 216ff0cc061SDimitry Andric break; 217ff0cc061SDimitry Andric case Location::Constant: 218ff0cc061SDimitry Andric OS << "Constant " << Loc.Offset; 219ff0cc061SDimitry Andric break; 220ff0cc061SDimitry Andric case Location::ConstantIndex: 221ff0cc061SDimitry Andric OS << "Constant Index " << Loc.Offset; 222ff0cc061SDimitry Andric break; 223ff0cc061SDimitry Andric } 224f37b6182SDimitry Andric OS << "\t[encoding: .byte " << Loc.Type << ", .byte 0" 225f37b6182SDimitry Andric << ", .short " << Loc.Size << ", .short " << Loc.Reg << ", .short 0" 226f37b6182SDimitry Andric << ", .int " << Loc.Offset << "]\n"; 227875ed548SDimitry Andric Idx++; 228ff0cc061SDimitry Andric } 229ff0cc061SDimitry Andric 230875ed548SDimitry Andric OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n"; 231ff0cc061SDimitry Andric 232875ed548SDimitry Andric Idx = 0; 233ff0cc061SDimitry Andric for (const auto &LO : LiveOuts) { 234875ed548SDimitry Andric OS << WSMP << "\t\tLO " << Idx << ": "; 235ff0cc061SDimitry Andric if (TRI) 2362cab237bSDimitry Andric OS << printReg(LO.Reg, TRI); 237ff0cc061SDimitry Andric else 238ff0cc061SDimitry Andric OS << LO.Reg; 239875ed548SDimitry Andric OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte " 240ff0cc061SDimitry Andric << LO.Size << "]\n"; 241875ed548SDimitry Andric Idx++; 242ff0cc061SDimitry Andric } 243ff0cc061SDimitry Andric } 24491bc56edSDimitry Andric } 24591bc56edSDimitry Andric 24691bc56edSDimitry Andric /// Create a live-out register record for the given register Reg. 24791bc56edSDimitry Andric StackMaps::LiveOutReg 24891bc56edSDimitry Andric StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const { 249875ed548SDimitry Andric unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI); 25051690af2SDimitry Andric unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg)); 251875ed548SDimitry Andric return LiveOutReg(Reg, DwarfRegNum, Size); 25291bc56edSDimitry Andric } 25391bc56edSDimitry Andric 25491bc56edSDimitry Andric /// Parse the register live-out mask and return a vector of live-out registers 25591bc56edSDimitry Andric /// that need to be recorded in the stackmap. 25691bc56edSDimitry Andric StackMaps::LiveOutVec 25791bc56edSDimitry Andric StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const { 25891bc56edSDimitry Andric assert(Mask && "No register mask specified"); 259ff0cc061SDimitry Andric const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo(); 26091bc56edSDimitry Andric LiveOutVec LiveOuts; 26191bc56edSDimitry Andric 26291bc56edSDimitry Andric // Create a LiveOutReg for each bit that is set in the register mask. 26391bc56edSDimitry Andric for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) 26491bc56edSDimitry Andric if ((Mask[Reg / 32] >> Reg % 32) & 1) 26591bc56edSDimitry Andric LiveOuts.push_back(createLiveOutReg(Reg, TRI)); 26691bc56edSDimitry Andric 26791bc56edSDimitry Andric // We don't need to keep track of a register if its super-register is already 26891bc56edSDimitry Andric // in the list. Merge entries that refer to the same dwarf register and use 26991bc56edSDimitry Andric // the maximum size that needs to be spilled. 270875ed548SDimitry Andric 271875ed548SDimitry Andric std::sort(LiveOuts.begin(), LiveOuts.end(), 272875ed548SDimitry Andric [](const LiveOutReg &LHS, const LiveOutReg &RHS) { 273875ed548SDimitry Andric // Only sort by the dwarf register number. 274875ed548SDimitry Andric return LHS.DwarfRegNum < RHS.DwarfRegNum; 275875ed548SDimitry Andric }); 276875ed548SDimitry Andric 277875ed548SDimitry Andric for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) { 278875ed548SDimitry Andric for (auto II = std::next(I); II != E; ++II) { 279875ed548SDimitry Andric if (I->DwarfRegNum != II->DwarfRegNum) { 28091bc56edSDimitry Andric // Skip all the now invalid entries. 28191bc56edSDimitry Andric I = --II; 28291bc56edSDimitry Andric break; 28391bc56edSDimitry Andric } 28491bc56edSDimitry Andric I->Size = std::max(I->Size, II->Size); 28591bc56edSDimitry Andric if (TRI->isSuperRegister(I->Reg, II->Reg)) 28691bc56edSDimitry Andric I->Reg = II->Reg; 287875ed548SDimitry Andric II->Reg = 0; // mark for deletion. 28891bc56edSDimitry Andric } 28991bc56edSDimitry Andric } 290875ed548SDimitry Andric 291875ed548SDimitry Andric LiveOuts.erase( 2927a7e6055SDimitry Andric llvm::remove_if(LiveOuts, 2937a7e6055SDimitry Andric [](const LiveOutReg &LO) { return LO.Reg == 0; }), 294875ed548SDimitry Andric LiveOuts.end()); 295875ed548SDimitry Andric 29691bc56edSDimitry Andric return LiveOuts; 29791bc56edSDimitry Andric } 29891bc56edSDimitry Andric 29991bc56edSDimitry Andric void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID, 300f785676fSDimitry Andric MachineInstr::const_mop_iterator MOI, 301f785676fSDimitry Andric MachineInstr::const_mop_iterator MOE, 302f785676fSDimitry Andric bool recordResult) { 303ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext(); 304ff0cc061SDimitry Andric MCSymbol *MILabel = OutContext.createTempSymbol(); 305ff0cc061SDimitry Andric AP.OutStreamer->EmitLabel(MILabel); 306f785676fSDimitry Andric 30791bc56edSDimitry Andric LocationVec Locations; 30891bc56edSDimitry Andric LiveOutVec LiveOuts; 309f785676fSDimitry Andric 310f785676fSDimitry Andric if (recordResult) { 31191bc56edSDimitry Andric assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value."); 312875ed548SDimitry Andric parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations, 313875ed548SDimitry Andric LiveOuts); 314f785676fSDimitry Andric } 315f785676fSDimitry Andric 31691bc56edSDimitry Andric // Parse operands. 317f785676fSDimitry Andric while (MOI != MOE) { 31891bc56edSDimitry Andric MOI = parseOperand(MOI, MOE, Locations, LiveOuts); 31991bc56edSDimitry Andric } 320f785676fSDimitry Andric 321f785676fSDimitry Andric // Move large constants into the constant pool. 322875ed548SDimitry Andric for (auto &Loc : Locations) { 32391bc56edSDimitry Andric // Constants are encoded as sign-extended integers. 32491bc56edSDimitry Andric // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool. 325875ed548SDimitry Andric if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) { 326875ed548SDimitry Andric Loc.Type = Location::ConstantIndex; 32739d628a0SDimitry Andric // ConstPool is intentionally a MapVector of 'uint64_t's (as 32839d628a0SDimitry Andric // opposed to 'int64_t's). We should never be in a situation 32939d628a0SDimitry Andric // where we have to insert either the tombstone or the empty 33039d628a0SDimitry Andric // keys into a map, and for a DenseMap<uint64_t, T> these are 33139d628a0SDimitry Andric // (uint64_t)0 and (uint64_t)-1. They can be and are 33239d628a0SDimitry Andric // represented using 32 bit integers. 333875ed548SDimitry Andric assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() && 334875ed548SDimitry Andric (uint64_t)Loc.Offset != 335875ed548SDimitry Andric DenseMapInfo<uint64_t>::getTombstoneKey() && 33639d628a0SDimitry Andric "empty and tombstone keys should fit in 32 bits!"); 337875ed548SDimitry Andric auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset)); 338875ed548SDimitry Andric Loc.Offset = Result.first - ConstPool.begin(); 33991bc56edSDimitry Andric } 340f785676fSDimitry Andric } 341f785676fSDimitry Andric 34291bc56edSDimitry Andric // Create an expression to calculate the offset of the callsite from function 34391bc56edSDimitry Andric // entry. 34497bc6c73SDimitry Andric const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub( 34597bc6c73SDimitry Andric MCSymbolRefExpr::create(MILabel, OutContext), 346875ed548SDimitry Andric MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext); 347f785676fSDimitry Andric 34839d628a0SDimitry Andric CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations), 34939d628a0SDimitry Andric std::move(LiveOuts)); 350f785676fSDimitry Andric 351d88c1a5aSDimitry Andric // Record the stack size of the current function and update callsite count. 352d88c1a5aSDimitry Andric const MachineFrameInfo &MFI = AP.MF->getFrameInfo(); 35339d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo(); 354875ed548SDimitry Andric bool HasDynamicFrameSize = 355d88c1a5aSDimitry Andric MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF)); 356d88c1a5aSDimitry Andric uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize(); 357d88c1a5aSDimitry Andric 358d88c1a5aSDimitry Andric auto CurrentIt = FnInfos.find(AP.CurrentFnSym); 359d88c1a5aSDimitry Andric if (CurrentIt != FnInfos.end()) 360d88c1a5aSDimitry Andric CurrentIt->second.RecordCount++; 361d88c1a5aSDimitry Andric else 362d88c1a5aSDimitry Andric FnInfos.insert(std::make_pair(AP.CurrentFnSym, FunctionInfo(FrameSize))); 363f785676fSDimitry Andric } 364f785676fSDimitry Andric 365f785676fSDimitry Andric void StackMaps::recordStackMap(const MachineInstr &MI) { 36691bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap"); 367f785676fSDimitry Andric 368d88c1a5aSDimitry Andric StackMapOpers opers(&MI); 369d88c1a5aSDimitry Andric const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm(); 370d88c1a5aSDimitry Andric recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), opers.getVarIdx()), 37191bc56edSDimitry Andric MI.operands_end()); 372f785676fSDimitry Andric } 373f785676fSDimitry Andric 374f785676fSDimitry Andric void StackMaps::recordPatchPoint(const MachineInstr &MI) { 37591bc56edSDimitry Andric assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint"); 376f785676fSDimitry Andric 377f785676fSDimitry Andric PatchPointOpers opers(&MI); 378d88c1a5aSDimitry Andric const int64_t ID = opers.getID(); 379875ed548SDimitry Andric auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx()); 38091bc56edSDimitry Andric recordStackMapOpers(MI, ID, MOI, MI.operands_end(), 381f785676fSDimitry Andric opers.isAnyReg() && opers.hasDef()); 382f785676fSDimitry Andric 383f785676fSDimitry Andric #ifndef NDEBUG 384f785676fSDimitry Andric // verify anyregcc 385875ed548SDimitry Andric auto &Locations = CSInfos.back().Locations; 386f785676fSDimitry Andric if (opers.isAnyReg()) { 387d88c1a5aSDimitry Andric unsigned NArgs = opers.getNumCallArgs(); 388f785676fSDimitry Andric for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i) 389875ed548SDimitry Andric assert(Locations[i].Type == Location::Register && 390f785676fSDimitry Andric "anyreg arg must be in reg."); 391f785676fSDimitry Andric } 392f785676fSDimitry Andric #endif 393f785676fSDimitry Andric } 3947a7e6055SDimitry Andric 39539d628a0SDimitry Andric void StackMaps::recordStatepoint(const MachineInstr &MI) { 396875ed548SDimitry Andric assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint"); 39739d628a0SDimitry Andric 39839d628a0SDimitry Andric StatepointOpers opers(&MI); 39939d628a0SDimitry Andric // Record all the deopt and gc operands (they're contiguous and run from the 40039d628a0SDimitry Andric // initial index to the end of the operand list) 40139d628a0SDimitry Andric const unsigned StartIdx = opers.getVarIdx(); 402ff0cc061SDimitry Andric recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx, 403ff0cc061SDimitry Andric MI.operands_end(), false); 40439d628a0SDimitry Andric } 405f785676fSDimitry Andric 40691bc56edSDimitry Andric /// Emit the stackmap header. 407f785676fSDimitry Andric /// 40891bc56edSDimitry Andric /// Header { 409d88c1a5aSDimitry Andric /// uint8 : Stack Map Version (currently 2) 41091bc56edSDimitry Andric /// uint8 : Reserved (expected to be 0) 41191bc56edSDimitry Andric /// uint16 : Reserved (expected to be 0) 41291bc56edSDimitry Andric /// } 41391bc56edSDimitry Andric /// uint32 : NumFunctions 414f785676fSDimitry Andric /// uint32 : NumConstants 415f785676fSDimitry Andric /// uint32 : NumRecords 41691bc56edSDimitry Andric void StackMaps::emitStackmapHeader(MCStreamer &OS) { 41791bc56edSDimitry Andric // Header. 41891bc56edSDimitry Andric OS.EmitIntValue(StackMapVersion, 1); // Version. 41991bc56edSDimitry Andric OS.EmitIntValue(0, 1); // Reserved. 42091bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved. 42191bc56edSDimitry Andric 42291bc56edSDimitry Andric // Num functions. 423d88c1a5aSDimitry Andric DEBUG(dbgs() << WSMP << "#functions = " << FnInfos.size() << '\n'); 424d88c1a5aSDimitry Andric OS.EmitIntValue(FnInfos.size(), 4); 42591bc56edSDimitry Andric // Num constants. 42691bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n'); 42791bc56edSDimitry Andric OS.EmitIntValue(ConstPool.size(), 4); 42891bc56edSDimitry Andric // Num callsites. 42991bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n'); 43091bc56edSDimitry Andric OS.EmitIntValue(CSInfos.size(), 4); 43191bc56edSDimitry Andric } 43291bc56edSDimitry Andric 43391bc56edSDimitry Andric /// Emit the function frame record for each function. 43491bc56edSDimitry Andric /// 43591bc56edSDimitry Andric /// StkSizeRecord[NumFunctions] { 43691bc56edSDimitry Andric /// uint64 : Function Address 43791bc56edSDimitry Andric /// uint64 : Stack Size 438d88c1a5aSDimitry Andric /// uint64 : Record Count 43991bc56edSDimitry Andric /// } 44091bc56edSDimitry Andric void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) { 44191bc56edSDimitry Andric // Function Frame records. 44291bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "functions:\n"); 443d88c1a5aSDimitry Andric for (auto const &FR : FnInfos) { 44491bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "function addr: " << FR.first 445d88c1a5aSDimitry Andric << " frame size: " << FR.second.StackSize 446d88c1a5aSDimitry Andric << " callsite count: " << FR.second.RecordCount << '\n'); 44791bc56edSDimitry Andric OS.EmitSymbolValue(FR.first, 8); 448d88c1a5aSDimitry Andric OS.EmitIntValue(FR.second.StackSize, 8); 449d88c1a5aSDimitry Andric OS.EmitIntValue(FR.second.RecordCount, 8); 45091bc56edSDimitry Andric } 45191bc56edSDimitry Andric } 45291bc56edSDimitry Andric 45391bc56edSDimitry Andric /// Emit the constant pool. 45491bc56edSDimitry Andric /// 45591bc56edSDimitry Andric /// int64 : Constants[NumConstants] 45691bc56edSDimitry Andric void StackMaps::emitConstantPoolEntries(MCStreamer &OS) { 45791bc56edSDimitry Andric // Constant pool entries. 45891bc56edSDimitry Andric DEBUG(dbgs() << WSMP << "constants:\n"); 459875ed548SDimitry Andric for (const auto &ConstEntry : ConstPool) { 46091bc56edSDimitry Andric DEBUG(dbgs() << WSMP << ConstEntry.second << '\n'); 46191bc56edSDimitry Andric OS.EmitIntValue(ConstEntry.second, 8); 46291bc56edSDimitry Andric } 46391bc56edSDimitry Andric } 46491bc56edSDimitry Andric 46591bc56edSDimitry Andric /// Emit the callsite info for each callsite. 46691bc56edSDimitry Andric /// 467f785676fSDimitry Andric /// StkMapRecord[NumRecords] { 46891bc56edSDimitry Andric /// uint64 : PatchPoint ID 469f785676fSDimitry Andric /// uint32 : Instruction Offset 470f785676fSDimitry Andric /// uint16 : Reserved (record flags) 471f785676fSDimitry Andric /// uint16 : NumLocations 472f785676fSDimitry Andric /// Location[NumLocations] { 473f785676fSDimitry Andric /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex 474f785676fSDimitry Andric /// uint8 : Size in Bytes 475f785676fSDimitry Andric /// uint16 : Dwarf RegNum 476f785676fSDimitry Andric /// int32 : Offset 477f785676fSDimitry Andric /// } 47891bc56edSDimitry Andric /// uint16 : Padding 47991bc56edSDimitry Andric /// uint16 : NumLiveOuts 48091bc56edSDimitry Andric /// LiveOuts[NumLiveOuts] { 48191bc56edSDimitry Andric /// uint16 : Dwarf RegNum 48291bc56edSDimitry Andric /// uint8 : Reserved 48391bc56edSDimitry Andric /// uint8 : Size in Bytes 48491bc56edSDimitry Andric /// } 48591bc56edSDimitry Andric /// uint32 : Padding (only if required to align to 8 byte) 486f785676fSDimitry Andric /// } 487f785676fSDimitry Andric /// 488f785676fSDimitry Andric /// Location Encoding, Type, Value: 489f785676fSDimitry Andric /// 0x1, Register, Reg (value in register) 490f785676fSDimitry Andric /// 0x2, Direct, Reg + Offset (frame index) 491f785676fSDimitry Andric /// 0x3, Indirect, [Reg + Offset] (spilled value) 492f785676fSDimitry Andric /// 0x4, Constant, Offset (small constant) 493f785676fSDimitry Andric /// 0x5, ConstIndex, Constants[Offset] (large constant) 494ff0cc061SDimitry Andric void StackMaps::emitCallsiteEntries(MCStreamer &OS) { 495ff0cc061SDimitry Andric DEBUG(print(dbgs())); 49691bc56edSDimitry Andric // Callsite entries. 49791bc56edSDimitry Andric for (const auto &CSI : CSInfos) { 49891bc56edSDimitry Andric const LocationVec &CSLocs = CSI.Locations; 49991bc56edSDimitry Andric const LiveOutVec &LiveOuts = CSI.LiveOuts; 500f785676fSDimitry Andric 501f785676fSDimitry Andric // Verify stack map entry. It's better to communicate a problem to the 502f785676fSDimitry Andric // runtime than crash in case of in-process compilation. Currently, we do 503f785676fSDimitry Andric // simple overflow checks, but we may eventually communicate other 504f785676fSDimitry Andric // compilation errors this way. 50591bc56edSDimitry Andric if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) { 50691bc56edSDimitry Andric OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID. 50791bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4); 50891bc56edSDimitry Andric OS.EmitIntValue(0, 2); // Reserved. 50991bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 locations. 51091bc56edSDimitry Andric OS.EmitIntValue(0, 2); // padding. 51191bc56edSDimitry Andric OS.EmitIntValue(0, 2); // 0 live-out registers. 51291bc56edSDimitry Andric OS.EmitIntValue(0, 4); // padding. 513f785676fSDimitry Andric continue; 514f785676fSDimitry Andric } 515f785676fSDimitry Andric 51691bc56edSDimitry Andric OS.EmitIntValue(CSI.ID, 8); 51791bc56edSDimitry Andric OS.EmitValue(CSI.CSOffsetExpr, 4); 518f785676fSDimitry Andric 519f785676fSDimitry Andric // Reserved for flags. 52091bc56edSDimitry Andric OS.EmitIntValue(0, 2); 52191bc56edSDimitry Andric OS.EmitIntValue(CSLocs.size(), 2); 522f785676fSDimitry Andric 52391bc56edSDimitry Andric for (const auto &Loc : CSLocs) { 524875ed548SDimitry Andric OS.EmitIntValue(Loc.Type, 1); 525f37b6182SDimitry Andric OS.EmitIntValue(0, 1); // Reserved 526f37b6182SDimitry Andric OS.EmitIntValue(Loc.Size, 2); 527ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Reg, 2); 528f37b6182SDimitry Andric OS.EmitIntValue(0, 2); // Reserved 529ff0cc061SDimitry Andric OS.EmitIntValue(Loc.Offset, 4); 530f785676fSDimitry Andric } 53191bc56edSDimitry Andric 532f37b6182SDimitry Andric // Emit alignment to 8 byte. 533f37b6182SDimitry Andric OS.EmitValueToAlignment(8); 534f37b6182SDimitry Andric 53591bc56edSDimitry Andric // Num live-out registers and padding to align to 4 byte. 53691bc56edSDimitry Andric OS.EmitIntValue(0, 2); 53791bc56edSDimitry Andric OS.EmitIntValue(LiveOuts.size(), 2); 53891bc56edSDimitry Andric 53991bc56edSDimitry Andric for (const auto &LO : LiveOuts) { 540875ed548SDimitry Andric OS.EmitIntValue(LO.DwarfRegNum, 2); 54191bc56edSDimitry Andric OS.EmitIntValue(0, 1); 54291bc56edSDimitry Andric OS.EmitIntValue(LO.Size, 1); 543f785676fSDimitry Andric } 54491bc56edSDimitry Andric // Emit alignment to 8 byte. 54591bc56edSDimitry Andric OS.EmitValueToAlignment(8); 546f785676fSDimitry Andric } 547f785676fSDimitry Andric } 548f785676fSDimitry Andric 54991bc56edSDimitry Andric /// Serialize the stackmap data. 55091bc56edSDimitry Andric void StackMaps::serializeToStackMapSection() { 55191bc56edSDimitry Andric (void)WSMP; 55291bc56edSDimitry Andric // Bail out if there's no stack map data. 5533ca95b02SDimitry Andric assert((!CSInfos.empty() || ConstPool.empty()) && 55491bc56edSDimitry Andric "Expected empty constant pool too!"); 555d88c1a5aSDimitry Andric assert((!CSInfos.empty() || FnInfos.empty()) && 55691bc56edSDimitry Andric "Expected empty function record too!"); 55791bc56edSDimitry Andric if (CSInfos.empty()) 55891bc56edSDimitry Andric return; 559f785676fSDimitry Andric 560ff0cc061SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext(); 561ff0cc061SDimitry Andric MCStreamer &OS = *AP.OutStreamer; 56291bc56edSDimitry Andric 56391bc56edSDimitry Andric // Create the section. 564ff0cc061SDimitry Andric MCSection *StackMapSection = 56591bc56edSDimitry Andric OutContext.getObjectFileInfo()->getStackMapSection(); 56691bc56edSDimitry Andric OS.SwitchSection(StackMapSection); 56791bc56edSDimitry Andric 56891bc56edSDimitry Andric // Emit a dummy symbol to force section inclusion. 569ff0cc061SDimitry Andric OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps"))); 57091bc56edSDimitry Andric 57191bc56edSDimitry Andric // Serialize data. 57291bc56edSDimitry Andric DEBUG(dbgs() << "********** Stack Map Output **********\n"); 57391bc56edSDimitry Andric emitStackmapHeader(OS); 57491bc56edSDimitry Andric emitFunctionFrameRecords(OS); 57591bc56edSDimitry Andric emitConstantPoolEntries(OS); 576ff0cc061SDimitry Andric emitCallsiteEntries(OS); 57791bc56edSDimitry Andric OS.AddBlankLine(); 57891bc56edSDimitry Andric 57991bc56edSDimitry Andric // Clean up. 580f785676fSDimitry Andric CSInfos.clear(); 58191bc56edSDimitry Andric ConstPool.clear(); 582f785676fSDimitry Andric } 583