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