blob: e7e4c68ab94b44b29f8f0d2e9444aef2d75d0655 [file] [log] [blame]
Andrew Trick153ebe62013-10-31 22:11:56 +00001//===---------------------------- 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 Trick153ebe62013-10-31 22:11:56 +000010#include "llvm/CodeGen/StackMaps.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000011#include "llvm/CodeGen/AsmPrinter.h"
Juergen Ributzkafb4d6482014-01-30 18:58:27 +000012#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000013#include "llvm/CodeGen/MachineFunction.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000014#include "llvm/CodeGen/MachineInstr.h"
Lang Hames39609992013-11-29 03:07:54 +000015#include "llvm/IR/DataLayout.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
Lang Hames8a065702013-11-08 22:30:52 +000018#include "llvm/MC/MCObjectFileInfo.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000019#include "llvm/MC/MCSectionMachO.h"
20#include "llvm/MC/MCStreamer.h"
Juergen Ributzka673a7622014-05-01 22:21:30 +000021#include "llvm/Support/CommandLine.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000022#include "llvm/Target/TargetMachine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/Target/TargetOpcodes.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000024#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000025#include "llvm/Target/TargetSubtargetInfo.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000026#include <iterator>
27
28using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "stackmaps"
31
Juergen Ributzkad25407e2015-07-08 22:42:09 +000032static cl::opt<int> StackMapVersion(
33 "stackmap-version", cl::init(1),
34 cl::desc("Specify the stackmap encoding version (default = 1)"));
Juergen Ributzka673a7622014-05-01 22:21:30 +000035
Juergen Ributzka37fc0a82014-05-01 22:39:26 +000036const char *StackMaps::WSMP = "Stack Maps: ";
37
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000038PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
Juergen Ributzkad25407e2015-07-08 22:42:09 +000039 : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
40 !MI->getOperand(0).isImplicit()),
41 IsAnyReg(MI->getOperand(getMetaIdx(CCPos)).getImm() ==
42 CallingConv::AnyReg) {
Andrew Trickd4e3dc62013-11-19 03:29:56 +000043#ifndef NDEBUG
Andrew Trickd4e3dc62013-11-19 03:29:56 +000044 unsigned CheckStartIdx = 0, e = MI->getNumOperands();
45 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
46 MI->getOperand(CheckStartIdx).isDef() &&
47 !MI->getOperand(CheckStartIdx).isImplicit())
48 ++CheckStartIdx;
49
50 assert(getMetaIdx() == CheckStartIdx &&
Alp Tokercb402912014-01-24 17:20:08 +000051 "Unexpected additional definition in Patchpoint intrinsic.");
Andrew Trickd4e3dc62013-11-19 03:29:56 +000052#endif
53}
54
55unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
56 if (!StartIdx)
57 StartIdx = getVarIdx();
58
59 // Find the next scratch register (implicit def and early clobber)
60 unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
61 while (ScratchIdx < e &&
62 !(MI->getOperand(ScratchIdx).isReg() &&
63 MI->getOperand(ScratchIdx).isDef() &&
64 MI->getOperand(ScratchIdx).isImplicit() &&
65 MI->getOperand(ScratchIdx).isEarlyClobber()))
66 ++ScratchIdx;
67
68 assert(ScratchIdx != e && "No scratch register available");
69 return ScratchIdx;
70}
71
Juergen Ributzka673a7622014-05-01 22:21:30 +000072StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
73 if (StackMapVersion != 1)
74 llvm_unreachable("Unsupported stackmap version!");
75}
76
Eric Christophercef8e712015-03-20 16:03:42 +000077/// Go up the super-register chain until we hit a valid dwarf register number.
78static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
79 int RegNo = TRI->getDwarfRegNum(Reg, false);
80 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNo < 0; ++SR)
81 RegNo = TRI->getDwarfRegNum(*SR, false);
82
83 assert(RegNo >= 0 && "Invalid Dwarf register number.");
Juergen Ributzkad25407e2015-07-08 22:42:09 +000084 return (unsigned)RegNo;
Eric Christophercef8e712015-03-20 16:03:42 +000085}
86
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000087MachineInstr::const_mop_iterator
Lang Hames39609992013-11-29 03:07:54 +000088StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
Juergen Ributzkad25407e2015-07-08 22:42:09 +000089 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
90 LiveOutVec &LiveOuts) const {
Eric Christophercef8e712015-03-20 16:03:42 +000091 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000092 if (MOI->isImm()) {
93 switch (MOI->getImm()) {
Juergen Ributzkad25407e2015-07-08 22:42:09 +000094 default:
95 llvm_unreachable("Unrecognized operand type.");
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000096 case StackMaps::DirectMemRefOp: {
Eric Christopher8b770652015-01-26 19:03:15 +000097 unsigned Size = AP.TM.getDataLayout()->getPointerSizeInBits();
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000098 assert((Size % 8) == 0 && "Need pointer size in bytes.");
99 Size /= 8;
100 unsigned Reg = (++MOI)->getReg();
101 int64_t Imm = (++MOI)->getImm();
Juergen Ributzkae4685a12015-07-09 17:11:08 +0000102 Locs.emplace_back(StackMaps::Location::Direct, Size,
103 getDwarfRegNum(Reg, TRI), Imm);
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000104 break;
Lang Hames39609992013-11-29 03:07:54 +0000105 }
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000106 case StackMaps::IndirectMemRefOp: {
107 int64_t Size = (++MOI)->getImm();
108 assert(Size > 0 && "Need a valid size for indirect memory locations.");
109 unsigned Reg = (++MOI)->getReg();
110 int64_t Imm = (++MOI)->getImm();
Juergen Ributzkae4685a12015-07-09 17:11:08 +0000111 Locs.emplace_back(StackMaps::Location::Indirect, Size,
112 getDwarfRegNum(Reg, TRI), Imm);
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000113 break;
114 }
115 case StackMaps::ConstantOp: {
116 ++MOI;
117 assert(MOI->isImm() && "Expected constant operand.");
118 int64_t Imm = MOI->getImm();
Juergen Ributzkae4685a12015-07-09 17:11:08 +0000119 Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000120 break;
121 }
122 }
123 return ++MOI;
Lang Hames39609992013-11-29 03:07:54 +0000124 }
125
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000126 // The physical register number will ultimately be encoded as a DWARF regno.
127 // The stack map also records the size of a spill slot that can hold the
128 // register content. (The runtime can track the actual size of the data type
129 // if it needs to.)
130 if (MOI->isReg()) {
131 // Skip implicit registers (this includes our scratch registers)
132 if (MOI->isImplicit())
133 return ++MOI;
Juergen Ributzkae8294752013-12-14 06:53:06 +0000134
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000135 assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
136 "Virtreg operands should have been rewritten before now.");
Eric Christophercef8e712015-03-20 16:03:42 +0000137 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000138 assert(!MOI->getSubReg() && "Physical subreg still around.");
Eric Christophercef8e712015-03-20 16:03:42 +0000139
140 unsigned Offset = 0;
141 unsigned RegNo = getDwarfRegNum(MOI->getReg(), TRI);
142 unsigned LLVMRegNo = TRI->getLLVMRegNum(RegNo, false);
143 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNo, MOI->getReg());
144 if (SubRegIdx)
145 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
146
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000147 Locs.push_back(Location(Location::Register, RC->getSize(), RegNo, Offset));
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000148 return ++MOI;
149 }
150
151 if (MOI->isRegLiveOut())
152 LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
153
154 return ++MOI;
Lang Hames39609992013-11-29 03:07:54 +0000155}
156
Eric Christophercef8e712015-03-20 16:03:42 +0000157void StackMaps::print(raw_ostream &OS) {
158 const TargetRegisterInfo *TRI =
159 AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
160 OS << WSMP << "callsites:\n";
161 for (const auto &CSI : CSInfos) {
162 const LocationVec &CSLocs = CSI.Locations;
163 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Juergen Ributzkae8294752013-12-14 06:53:06 +0000164
Eric Christophercef8e712015-03-20 16:03:42 +0000165 OS << WSMP << "callsite " << CSI.ID << "\n";
166 OS << WSMP << " has " << CSLocs.size() << " locations\n";
167
168 unsigned OperIdx = 0;
169 for (const auto &Loc : CSLocs) {
170 OS << WSMP << " Loc " << OperIdx << ": ";
171 switch (Loc.LocType) {
172 case Location::Unprocessed:
173 OS << "<Unprocessed operand>";
174 break;
175 case Location::Register:
176 OS << "Register ";
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000177 if (TRI)
178 OS << TRI->getName(Loc.Reg);
179 else
180 OS << Loc.Reg;
Eric Christophercef8e712015-03-20 16:03:42 +0000181 break;
182 case Location::Direct:
183 OS << "Direct ";
184 if (TRI)
185 OS << TRI->getName(Loc.Reg);
186 else
187 OS << Loc.Reg;
188 if (Loc.Offset)
189 OS << " + " << Loc.Offset;
190 break;
191 case Location::Indirect:
192 OS << "Indirect ";
193 if (TRI)
194 OS << TRI->getName(Loc.Reg);
195 else
196 OS << Loc.Reg;
197 OS << "+" << Loc.Offset;
198 break;
199 case Location::Constant:
200 OS << "Constant " << Loc.Offset;
201 break;
202 case Location::ConstantIndex:
203 OS << "Constant Index " << Loc.Offset;
204 break;
205 }
206 OS << " [encoding: .byte " << Loc.LocType << ", .byte " << Loc.Size
207 << ", .short " << Loc.Reg << ", .int " << Loc.Offset << "]\n";
208 OperIdx++;
209 }
210
211 OS << WSMP << " has " << LiveOuts.size() << " live-out registers\n";
212
213 OperIdx = 0;
214 for (const auto &LO : LiveOuts) {
215 OS << WSMP << " LO " << OperIdx << ": ";
216 if (TRI)
217 OS << TRI->getName(LO.Reg);
218 else
219 OS << LO.Reg;
220 OS << " [encoding: .short " << LO.RegNo << ", .byte 0, .byte "
221 << LO.Size << "]\n";
222 OperIdx++;
223 }
224 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000225}
226
227/// Create a live-out register record for the given register Reg.
228StackMaps::LiveOutReg
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000229StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
230 unsigned RegNo = getDwarfRegNum(Reg, TRI);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000231 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
232 return LiveOutReg(Reg, RegNo, Size);
233}
234
235/// Parse the register live-out mask and return a vector of live-out registers
236/// that need to be recorded in the stackmap.
237StackMaps::LiveOutVec
238StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
239 assert(Mask && "No register mask specified");
Eric Christopherd83003e2015-03-19 23:27:42 +0000240 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000241 LiveOutVec LiveOuts;
242
243 // Create a LiveOutReg for each bit that is set in the register mask.
244 for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
245 if ((Mask[Reg / 32] >> Reg % 32) & 1)
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000246 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
Juergen Ributzkae8294752013-12-14 06:53:06 +0000247
248 // We don't need to keep track of a register if its super-register is already
249 // in the list. Merge entries that refer to the same dwarf register and use
250 // the maximum size that needs to be spilled.
251 std::sort(LiveOuts.begin(), LiveOuts.end());
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000252 for (LiveOutVec::iterator I = LiveOuts.begin(), E = LiveOuts.end(); I != E;
253 ++I) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000254 for (LiveOutVec::iterator II = std::next(I); II != E; ++II) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000255 if (I->RegNo != II->RegNo) {
256 // Skip all the now invalid entries.
257 I = --II;
258 break;
259 }
260 I->Size = std::max(I->Size, II->Size);
261 if (TRI->isSuperRegister(I->Reg, II->Reg))
262 I->Reg = II->Reg;
263 II->MarkInvalid();
264 }
265 }
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000266 LiveOuts.erase(
267 std::remove_if(LiveOuts.begin(), LiveOuts.end(), LiveOutReg::IsInvalid),
268 LiveOuts.end());
Juergen Ributzkae8294752013-12-14 06:53:06 +0000269 return LiveOuts;
270}
271
Andrew Tricke8cba372013-12-13 18:37:10 +0000272void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000273 MachineInstr::const_mop_iterator MOI,
274 MachineInstr::const_mop_iterator MOE,
275 bool recordResult) {
Andrew Trick153ebe62013-10-31 22:11:56 +0000276
Lang Hames9ff69c82015-04-24 19:11:51 +0000277 MCContext &OutContext = AP.OutStreamer->getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +0000278 MCSymbol *MILabel = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000279 AP.OutStreamer->EmitLabel(MILabel);
Andrew Trick153ebe62013-10-31 22:11:56 +0000280
Juergen Ributzkae8294752013-12-14 06:53:06 +0000281 LocationVec Locations;
282 LiveOutVec LiveOuts;
Andrew Trick153ebe62013-10-31 22:11:56 +0000283
Juergen Ributzka9969d3e2013-11-08 23:28:16 +0000284 if (recordResult) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000285 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000286 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
287 LiveOuts);
Juergen Ributzka9969d3e2013-11-08 23:28:16 +0000288 }
289
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000290 // Parse operands.
Andrew Trick153ebe62013-10-31 22:11:56 +0000291 while (MOI != MOE) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000292 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
293 }
Andrew Trick153ebe62013-10-31 22:11:56 +0000294
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000295 // Move large constants into the constant pool.
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000296 for (LocationVec::iterator I = Locations.begin(), E = Locations.end(); I != E;
297 ++I) {
Andrew Trick32e1be72014-01-09 00:22:31 +0000298 // Constants are encoded as sign-extended integers.
299 // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
Sanjoy Das429c9ca2014-11-04 00:06:57 +0000300 if (I->LocType == Location::Constant && !isInt<32>(I->Offset)) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000301 I->LocType = Location::ConstantIndex;
Sanjoy Dase8399652014-11-04 00:59:21 +0000302 // ConstPool is intentionally a MapVector of 'uint64_t's (as
303 // opposed to 'int64_t's). We should never be in a situation
304 // where we have to insert either the tombstone or the empty
305 // keys into a map, and for a DenseMap<uint64_t, T> these are
306 // (uint64_t)0 and (uint64_t)-1. They can be and are
307 // represented using 32 bit integers.
308
309 assert((uint64_t)I->Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
310 (uint64_t)I->Offset != DenseMapInfo<uint64_t>::getTombstoneKey() &&
311 "empty and tombstone keys should fit in 32 bits!");
Juergen Ributzkaf01e8092014-05-01 22:21:24 +0000312 auto Result = ConstPool.insert(std::make_pair(I->Offset, I->Offset));
313 I->Offset = Result.first - ConstPool.begin();
Andrew Trick153ebe62013-10-31 22:11:56 +0000314 }
Andrew Trick153ebe62013-10-31 22:11:56 +0000315 }
316
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000317 // Create an expression to calculate the offset of the callsite from function
318 // entry.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000319 const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000320 MCSymbolRefExpr::create(MILabel, OutContext),
321 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
Andrew Trick153ebe62013-10-31 22:11:56 +0000322
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000323 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
324 std::move(LiveOuts));
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000325
326 // Record the stack size of the current function.
327 const MachineFrameInfo *MFI = AP.MF->getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000328 const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000329 const bool DynamicFrameSize =
330 MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000331 FnStackSize[AP.CurrentFnSym] =
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000332 DynamicFrameSize ? UINT64_MAX : MFI->getStackSize();
Andrew Trick153ebe62013-10-31 22:11:56 +0000333}
334
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000335void StackMaps::recordStackMap(const MachineInstr &MI) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000336 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000337
338 int64_t ID = MI.getOperand(0).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000339 recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2),
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000340 MI.operands_end());
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000341}
342
343void StackMaps::recordPatchPoint(const MachineInstr &MI) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000344 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000345
346 PatchPointOpers opers(&MI);
347 int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm();
Andrew Tricke8cba372013-12-13 18:37:10 +0000348
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000349 MachineInstr::const_mop_iterator MOI =
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000350 std::next(MI.operands_begin(), opers.getStackMapStartIdx());
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000351 recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000352 opers.isAnyReg() && opers.hasDef());
353
354#ifndef NDEBUG
355 // verify anyregcc
356 LocationVec &Locations = CSInfos.back().Locations;
357 if (opers.isAnyReg()) {
358 unsigned NArgs = opers.getMetaOper(PatchPointOpers::NArgPos).getImm();
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000359 for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000360 assert(Locations[i].LocType == Location::Register &&
361 "anyreg arg must be in reg.");
362 }
363#endif
364}
Philip Reames0365f1a2014-12-01 22:52:56 +0000365void StackMaps::recordStatepoint(const MachineInstr &MI) {
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000366 assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
Philip Reames0365f1a2014-12-01 22:52:56 +0000367
368 StatepointOpers opers(&MI);
369 // Record all the deopt and gc operands (they're contiguous and run from the
370 // initial index to the end of the operand list)
371 const unsigned StartIdx = opers.getVarIdx();
Sanjoy Dasa1d39ba2015-05-12 23:52:24 +0000372 recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
373 MI.operands_end(), false);
Philip Reames0365f1a2014-12-01 22:52:56 +0000374}
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000375
Juergen Ributzka63401952014-05-01 22:21:27 +0000376/// Emit the stackmap header.
Andrew Trick153ebe62013-10-31 22:11:56 +0000377///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000378/// Header {
379/// uint8 : Stack Map Version (currently 1)
380/// uint8 : Reserved (expected to be 0)
381/// uint16 : Reserved (expected to be 0)
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000382/// }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000383/// uint32 : NumFunctions
Andrew Trick153ebe62013-10-31 22:11:56 +0000384/// uint32 : NumConstants
Andrew Trick153ebe62013-10-31 22:11:56 +0000385/// uint32 : NumRecords
Juergen Ributzka63401952014-05-01 22:21:27 +0000386void StackMaps::emitStackmapHeader(MCStreamer &OS) {
387 // Header.
Juergen Ributzka673a7622014-05-01 22:21:30 +0000388 OS.EmitIntValue(StackMapVersion, 1); // Version.
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000389 OS.EmitIntValue(0, 1); // Reserved.
390 OS.EmitIntValue(0, 2); // Reserved.
Juergen Ributzka63401952014-05-01 22:21:27 +0000391
392 // Num functions.
393 DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
394 OS.EmitIntValue(FnStackSize.size(), 4);
395 // Num constants.
396 DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
397 OS.EmitIntValue(ConstPool.size(), 4);
398 // Num callsites.
399 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
400 OS.EmitIntValue(CSInfos.size(), 4);
401}
402
403/// Emit the function frame record for each function.
404///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000405/// StkSizeRecord[NumFunctions] {
406/// uint64 : Function Address
407/// uint64 : Stack Size
408/// }
Juergen Ributzka63401952014-05-01 22:21:27 +0000409void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
410 // Function Frame records.
411 DEBUG(dbgs() << WSMP << "functions:\n");
412 for (auto const &FR : FnStackSize) {
413 DEBUG(dbgs() << WSMP << "function addr: " << FR.first
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000414 << " frame size: " << FR.second);
Juergen Ributzka63401952014-05-01 22:21:27 +0000415 OS.EmitSymbolValue(FR.first, 8);
416 OS.EmitIntValue(FR.second, 8);
417 }
418}
419
420/// Emit the constant pool.
421///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000422/// int64 : Constants[NumConstants]
Juergen Ributzka63401952014-05-01 22:21:27 +0000423void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
424 // Constant pool entries.
425 DEBUG(dbgs() << WSMP << "constants:\n");
426 for (auto ConstEntry : ConstPool) {
427 DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
428 OS.EmitIntValue(ConstEntry.second, 8);
429 }
430}
431
432/// Emit the callsite info for each callsite.
433///
Andrew Trick153ebe62013-10-31 22:11:56 +0000434/// StkMapRecord[NumRecords] {
Andrew Tricke8cba372013-12-13 18:37:10 +0000435/// uint64 : PatchPoint ID
Andrew Trick153ebe62013-10-31 22:11:56 +0000436/// uint32 : Instruction Offset
437/// uint16 : Reserved (record flags)
438/// uint16 : NumLocations
439/// Location[NumLocations] {
440/// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
Andrew Trick10d5be42013-11-17 01:36:23 +0000441/// uint8 : Size in Bytes
Andrew Trick153ebe62013-10-31 22:11:56 +0000442/// uint16 : Dwarf RegNum
443/// int32 : Offset
444/// }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000445/// uint16 : Padding
Juergen Ributzkae8294752013-12-14 06:53:06 +0000446/// uint16 : NumLiveOuts
Juergen Ributzkae1179922014-03-31 22:14:04 +0000447/// LiveOuts[NumLiveOuts] {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000448/// uint16 : Dwarf RegNum
449/// uint8 : Reserved
450/// uint8 : Size in Bytes
Juergen Ributzkae1179922014-03-31 22:14:04 +0000451/// }
452/// uint32 : Padding (only if required to align to 8 byte)
Andrew Trick153ebe62013-10-31 22:11:56 +0000453/// }
454///
455/// Location Encoding, Type, Value:
456/// 0x1, Register, Reg (value in register)
457/// 0x2, Direct, Reg + Offset (frame index)
458/// 0x3, Indirect, [Reg + Offset] (spilled value)
459/// 0x4, Constant, Offset (small constant)
460/// 0x5, ConstIndex, Constants[Offset] (large constant)
Eric Christopher594fa96a2015-03-20 21:05:18 +0000461void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
Eric Christophercef8e712015-03-20 16:03:42 +0000462 DEBUG(print(dbgs()));
Juergen Ributzkae1179922014-03-31 22:14:04 +0000463 // Callsite entries.
Juergen Ributzka63401952014-05-01 22:21:27 +0000464 for (const auto &CSI : CSInfos) {
465 const LocationVec &CSLocs = CSI.Locations;
466 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Andrew Trick153ebe62013-10-31 22:11:56 +0000467
Andrew Trick153ebe62013-10-31 22:11:56 +0000468 // Verify stack map entry. It's better to communicate a problem to the
469 // runtime than crash in case of in-process compilation. Currently, we do
470 // simple overflow checks, but we may eventually communicate other
471 // compilation errors this way.
Juergen Ributzkae8294752013-12-14 06:53:06 +0000472 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
Juergen Ributzka63401952014-05-01 22:21:27 +0000473 OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
474 OS.EmitValue(CSI.CSOffsetExpr, 4);
475 OS.EmitIntValue(0, 2); // Reserved.
476 OS.EmitIntValue(0, 2); // 0 locations.
477 OS.EmitIntValue(0, 2); // padding.
478 OS.EmitIntValue(0, 2); // 0 live-out registers.
479 OS.EmitIntValue(0, 4); // padding.
Andrew Trick153ebe62013-10-31 22:11:56 +0000480 continue;
481 }
482
Juergen Ributzka63401952014-05-01 22:21:27 +0000483 OS.EmitIntValue(CSI.ID, 8);
484 OS.EmitValue(CSI.CSOffsetExpr, 4);
Andrew Trick153ebe62013-10-31 22:11:56 +0000485
486 // Reserved for flags.
Juergen Ributzka63401952014-05-01 22:21:27 +0000487 OS.EmitIntValue(0, 2);
Juergen Ributzka63401952014-05-01 22:21:27 +0000488 OS.EmitIntValue(CSLocs.size(), 2);
Andrew Trick153ebe62013-10-31 22:11:56 +0000489
Juergen Ributzka63401952014-05-01 22:21:27 +0000490 for (const auto &Loc : CSLocs) {
Juergen Ributzka63401952014-05-01 22:21:27 +0000491 OS.EmitIntValue(Loc.LocType, 1);
492 OS.EmitIntValue(Loc.Size, 1);
Eric Christophercef8e712015-03-20 16:03:42 +0000493 OS.EmitIntValue(Loc.Reg, 2);
494 OS.EmitIntValue(Loc.Offset, 4);
Andrew Trick153ebe62013-10-31 22:11:56 +0000495 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000496
Juergen Ributzkae1179922014-03-31 22:14:04 +0000497 // Num live-out registers and padding to align to 4 byte.
Juergen Ributzka63401952014-05-01 22:21:27 +0000498 OS.EmitIntValue(0, 2);
499 OS.EmitIntValue(LiveOuts.size(), 2);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000500
Juergen Ributzka63401952014-05-01 22:21:27 +0000501 for (const auto &LO : LiveOuts) {
Juergen Ributzka63401952014-05-01 22:21:27 +0000502 OS.EmitIntValue(LO.RegNo, 2);
503 OS.EmitIntValue(0, 1);
504 OS.EmitIntValue(LO.Size, 1);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000505 }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000506 // Emit alignment to 8 byte.
Juergen Ributzka63401952014-05-01 22:21:27 +0000507 OS.EmitValueToAlignment(8);
Andrew Trick153ebe62013-10-31 22:11:56 +0000508 }
Juergen Ributzka63401952014-05-01 22:21:27 +0000509}
Andrew Trick153ebe62013-10-31 22:11:56 +0000510
Juergen Ributzka63401952014-05-01 22:21:27 +0000511/// Serialize the stackmap data.
512void StackMaps::serializeToStackMapSection() {
Juergen Ributzkad25407e2015-07-08 22:42:09 +0000513 (void)WSMP;
Juergen Ributzka63401952014-05-01 22:21:27 +0000514 // Bail out if there's no stack map data.
515 assert((!CSInfos.empty() || (CSInfos.empty() && ConstPool.empty())) &&
516 "Expected empty constant pool too!");
517 assert((!CSInfos.empty() || (CSInfos.empty() && FnStackSize.empty())) &&
518 "Expected empty function record too!");
519 if (CSInfos.empty())
520 return;
Andrew Trick153ebe62013-10-31 22:11:56 +0000521
Lang Hames9ff69c82015-04-24 19:11:51 +0000522 MCContext &OutContext = AP.OutStreamer->getContext();
523 MCStreamer &OS = *AP.OutStreamer;
Juergen Ributzka63401952014-05-01 22:21:27 +0000524
525 // Create the section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000526 MCSection *StackMapSection =
527 OutContext.getObjectFileInfo()->getStackMapSection();
Juergen Ributzka63401952014-05-01 22:21:27 +0000528 OS.SwitchSection(StackMapSection);
529
530 // Emit a dummy symbol to force section inclusion.
Jim Grosbach6f482002015-05-18 18:43:14 +0000531 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
Juergen Ributzka63401952014-05-01 22:21:27 +0000532
533 // Serialize data.
534 DEBUG(dbgs() << "********** Stack Map Output **********\n");
535 emitStackmapHeader(OS);
536 emitFunctionFrameRecords(OS);
537 emitConstantPoolEntries(OS);
Eric Christopher594fa96a2015-03-20 21:05:18 +0000538 emitCallsiteEntries(OS);
Juergen Ributzka63401952014-05-01 22:21:27 +0000539 OS.AddBlankLine();
540
541 // Clean up.
Andrew Trick153ebe62013-10-31 22:11:56 +0000542 CSInfos.clear();
Juergen Ributzkaf01e8092014-05-01 22:21:24 +0000543 ConstPool.clear();
Andrew Trick153ebe62013-10-31 22:11:56 +0000544}