blob: f1d1160c9528921eb47310f1900827c5ee79694a [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/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000024#include "llvm/Target/TargetMachine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/Target/TargetOpcodes.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000026#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000027#include "llvm/Target/TargetSubtargetInfo.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000028#include <iterator>
29
30using namespace llvm;
31
Chandler Carruth1b9dde02014-04-22 02:02:50 +000032#define DEBUG_TYPE "stackmaps"
33
Juergen Ributzka673a7622014-05-01 22:21:30 +000034static cl::opt<int> StackMapVersion("stackmap-version", cl::init(1),
35 cl::desc("Specify the stackmap encoding version (default = 1)"));
36
Juergen Ributzka37fc0a82014-05-01 22:39:26 +000037const char *StackMaps::WSMP = "Stack Maps: ";
38
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000039PatchPointOpers::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{
Andrew Trickd4e3dc62013-11-19 03:29:56 +000045#ifndef NDEBUG
Andrew Trickd4e3dc62013-11-19 03:29:56 +000046 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 &&
Alp Tokercb402912014-01-24 17:20:08 +000053 "Unexpected additional definition in Patchpoint intrinsic.");
Andrew Trickd4e3dc62013-11-19 03:29:56 +000054#endif
55}
56
57unsigned 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
Juergen Ributzka673a7622014-05-01 22:21:30 +000074StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
75 if (StackMapVersion != 1)
76 llvm_unreachable("Unsupported stackmap version!");
77}
78
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000079MachineInstr::const_mop_iterator
Lang Hames39609992013-11-29 03:07:54 +000080StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000081 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: {
Eric Christopherd9134482014-08-04 21:25:23 +000087 unsigned Size =
88 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSizeInBits();
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000089 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;
Lang Hames39609992013-11-29 03:07:54 +000095 }
Juergen Ributzkac26b68a2013-12-14 23:06:19 +000096 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;
Lang Hames39609992013-11-29 03:07:54 +0000113 }
114
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000115 // 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;
Juergen Ributzkae8294752013-12-14 06:53:06 +0000123
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000124 assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
125 "Virtreg operands should have been rewritten before now.");
126 const TargetRegisterClass *RC =
Eric Christopherd9134482014-08-04 21:25:23 +0000127 AP.TM.getSubtargetImpl()->getRegisterInfo()->getMinimalPhysRegClass(
128 MOI->getReg());
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000129 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;
Lang Hames39609992013-11-29 03:07:54 +0000139}
140
Juergen Ributzkae8294752013-12-14 06:53:06 +0000141/// Go up the super-register chain until we hit a valid dwarf register number.
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000142static 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)
Juergen Ributzkae8294752013-12-14 06:53:06 +0000145 RegNo = TRI->getDwarfRegNum(*SR, false);
146
147 assert(RegNo >= 0 && "Invalid Dwarf register number.");
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000148 return (unsigned) RegNo;
Juergen Ributzkae8294752013-12-14 06:53:06 +0000149}
150
151/// Create a live-out register record for the given register Reg.
152StackMaps::LiveOutReg
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000153StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
154 unsigned RegNo = getDwarfRegNum(Reg, TRI);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000155 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.
161StackMaps::LiveOutVec
162StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
163 assert(Mask && "No register mask specified");
Eric Christopherd9134482014-08-04 21:25:23 +0000164 const TargetRegisterInfo *TRI = AP.TM.getSubtargetImpl()->getRegisterInfo();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000165 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)
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000170 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
Juergen Ributzkae8294752013-12-14 06:53:06 +0000171
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) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000178 for (LiveOutVec::iterator II = std::next(I); II != E; ++II) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000179 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
Andrew Tricke8cba372013-12-13 18:37:10 +0000195void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000196 MachineInstr::const_mop_iterator MOI,
197 MachineInstr::const_mop_iterator MOE,
198 bool recordResult) {
Andrew Trick153ebe62013-10-31 22:11:56 +0000199
200 MCContext &OutContext = AP.OutStreamer.getContext();
201 MCSymbol *MILabel = OutContext.CreateTempSymbol();
202 AP.OutStreamer.EmitLabel(MILabel);
203
Juergen Ributzkae8294752013-12-14 06:53:06 +0000204 LocationVec Locations;
205 LiveOutVec LiveOuts;
Andrew Trick153ebe62013-10-31 22:11:56 +0000206
Juergen Ributzka9969d3e2013-11-08 23:28:16 +0000207 if (recordResult) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000208 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000209 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()),
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000210 Locations, LiveOuts);
Juergen Ributzka9969d3e2013-11-08 23:28:16 +0000211 }
212
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000213 // Parse operands.
Andrew Trick153ebe62013-10-31 22:11:56 +0000214 while (MOI != MOE) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000215 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
216 }
Andrew Trick153ebe62013-10-31 22:11:56 +0000217
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000218 // Move large constants into the constant pool.
219 for (LocationVec::iterator I = Locations.begin(), E = Locations.end();
220 I != E; ++I) {
Andrew Trick32e1be72014-01-09 00:22:31 +0000221 // Constants are encoded as sign-extended integers.
222 // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
Sanjoy Das429c9ca2014-11-04 00:06:57 +0000223 if (I->LocType == Location::Constant && !isInt<32>(I->Offset)) {
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000224 I->LocType = Location::ConstantIndex;
Sanjoy Dase8399652014-11-04 00:59:21 +0000225 // 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!");
Juergen Ributzkaf01e8092014-05-01 22:21:24 +0000235 auto Result = ConstPool.insert(std::make_pair(I->Offset, I->Offset));
236 I->Offset = Result.first - ConstPool.begin();
Andrew Trick153ebe62013-10-31 22:11:56 +0000237 }
Andrew Trick153ebe62013-10-31 22:11:56 +0000238 }
239
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000240 // Create an expression to calculate the offset of the callsite from function
241 // entry.
Andrew Trick153ebe62013-10-31 22:11:56 +0000242 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
243 MCSymbolRefExpr::Create(MILabel, OutContext),
Hal Finkelc4ee2c52015-01-13 17:48:07 +0000244 MCSymbolRefExpr::Create(AP.CurrentFnSymForSize, OutContext),
Andrew Trick153ebe62013-10-31 22:11:56 +0000245 OutContext);
246
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000247 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
248 std::move(LiveOuts));
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000249
250 // Record the stack size of the current function.
251 const MachineFrameInfo *MFI = AP.MF->getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000252 const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
Philip Reames87c2b602014-08-01 18:26:27 +0000253 const bool DynamicFrameSize = MFI->hasVarSizedObjects() ||
254 RegInfo->needsStackRealignment(*(AP.MF));
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000255 FnStackSize[AP.CurrentFnSym] =
Philip Reames87c2b602014-08-01 18:26:27 +0000256 DynamicFrameSize ? UINT64_MAX : MFI->getStackSize();
Andrew Trick153ebe62013-10-31 22:11:56 +0000257}
258
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000259void StackMaps::recordStackMap(const MachineInstr &MI) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000260 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000261
262 int64_t ID = MI.getOperand(0).getImm();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000263 recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2),
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000264 MI.operands_end());
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000265}
266
267void StackMaps::recordPatchPoint(const MachineInstr &MI) {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000268 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000269
270 PatchPointOpers opers(&MI);
271 int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm();
Andrew Tricke8cba372013-12-13 18:37:10 +0000272
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000273 MachineInstr::const_mop_iterator MOI =
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000274 std::next(MI.operands_begin(), opers.getStackMapStartIdx());
Juergen Ributzkac26b68a2013-12-14 23:06:19 +0000275 recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000276 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}
Philip Reames0365f1a2014-12-01 22:52:56 +0000289void StackMaps::recordStatepoint(const MachineInstr &MI) {
290 assert(MI.getOpcode() == TargetOpcode::STATEPOINT &&
291 "expected statepoint");
292
293 StatepointOpers opers(&MI);
294 // Record all the deopt and gc operands (they're contiguous and run from the
295 // initial index to the end of the operand list)
296 const unsigned StartIdx = opers.getVarIdx();
297 recordStackMapOpers(MI, 0xABCDEF00,
298 MI.operands_begin() + StartIdx, MI.operands_end(),
299 false);
300}
Andrew Trickd4e3dc62013-11-19 03:29:56 +0000301
Juergen Ributzka63401952014-05-01 22:21:27 +0000302/// Emit the stackmap header.
Andrew Trick153ebe62013-10-31 22:11:56 +0000303///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000304/// Header {
305/// uint8 : Stack Map Version (currently 1)
306/// uint8 : Reserved (expected to be 0)
307/// uint16 : Reserved (expected to be 0)
Juergen Ributzkafb4d6482014-01-30 18:58:27 +0000308/// }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000309/// uint32 : NumFunctions
Andrew Trick153ebe62013-10-31 22:11:56 +0000310/// uint32 : NumConstants
Andrew Trick153ebe62013-10-31 22:11:56 +0000311/// uint32 : NumRecords
Juergen Ributzka63401952014-05-01 22:21:27 +0000312void StackMaps::emitStackmapHeader(MCStreamer &OS) {
313 // Header.
Juergen Ributzka673a7622014-05-01 22:21:30 +0000314 OS.EmitIntValue(StackMapVersion, 1); // Version.
Juergen Ributzka63401952014-05-01 22:21:27 +0000315 OS.EmitIntValue(0, 1); // Reserved.
316 OS.EmitIntValue(0, 2); // Reserved.
317
318 // Num functions.
319 DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
320 OS.EmitIntValue(FnStackSize.size(), 4);
321 // Num constants.
322 DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
323 OS.EmitIntValue(ConstPool.size(), 4);
324 // Num callsites.
325 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
326 OS.EmitIntValue(CSInfos.size(), 4);
327}
328
329/// Emit the function frame record for each function.
330///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000331/// StkSizeRecord[NumFunctions] {
332/// uint64 : Function Address
333/// uint64 : Stack Size
334/// }
Juergen Ributzka63401952014-05-01 22:21:27 +0000335void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
336 // Function Frame records.
337 DEBUG(dbgs() << WSMP << "functions:\n");
338 for (auto const &FR : FnStackSize) {
339 DEBUG(dbgs() << WSMP << "function addr: " << FR.first
340 << " frame size: " << FR.second);
341 OS.EmitSymbolValue(FR.first, 8);
342 OS.EmitIntValue(FR.second, 8);
343 }
344}
345
346/// Emit the constant pool.
347///
Juergen Ributzkae1179922014-03-31 22:14:04 +0000348/// int64 : Constants[NumConstants]
Juergen Ributzka63401952014-05-01 22:21:27 +0000349void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
350 // Constant pool entries.
351 DEBUG(dbgs() << WSMP << "constants:\n");
352 for (auto ConstEntry : ConstPool) {
353 DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
354 OS.EmitIntValue(ConstEntry.second, 8);
355 }
356}
357
358/// Emit the callsite info for each callsite.
359///
Andrew Trick153ebe62013-10-31 22:11:56 +0000360/// StkMapRecord[NumRecords] {
Andrew Tricke8cba372013-12-13 18:37:10 +0000361/// uint64 : PatchPoint ID
Andrew Trick153ebe62013-10-31 22:11:56 +0000362/// uint32 : Instruction Offset
363/// uint16 : Reserved (record flags)
364/// uint16 : NumLocations
365/// Location[NumLocations] {
366/// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
Andrew Trick10d5be42013-11-17 01:36:23 +0000367/// uint8 : Size in Bytes
Andrew Trick153ebe62013-10-31 22:11:56 +0000368/// uint16 : Dwarf RegNum
369/// int32 : Offset
370/// }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000371/// uint16 : Padding
Juergen Ributzkae8294752013-12-14 06:53:06 +0000372/// uint16 : NumLiveOuts
Juergen Ributzkae1179922014-03-31 22:14:04 +0000373/// LiveOuts[NumLiveOuts] {
Juergen Ributzkae8294752013-12-14 06:53:06 +0000374/// uint16 : Dwarf RegNum
375/// uint8 : Reserved
376/// uint8 : Size in Bytes
Juergen Ributzkae1179922014-03-31 22:14:04 +0000377/// }
378/// uint32 : Padding (only if required to align to 8 byte)
Andrew Trick153ebe62013-10-31 22:11:56 +0000379/// }
380///
381/// Location Encoding, Type, Value:
382/// 0x1, Register, Reg (value in register)
383/// 0x2, Direct, Reg + Offset (frame index)
384/// 0x3, Indirect, [Reg + Offset] (spilled value)
385/// 0x4, Constant, Offset (small constant)
386/// 0x5, ConstIndex, Constants[Offset] (large constant)
Juergen Ributzka63401952014-05-01 22:21:27 +0000387void StackMaps::emitCallsiteEntries(MCStreamer &OS,
388 const TargetRegisterInfo *TRI) {
Juergen Ributzkae1179922014-03-31 22:14:04 +0000389 // Callsite entries.
Juergen Ributzka63401952014-05-01 22:21:27 +0000390 DEBUG(dbgs() << WSMP << "callsites:\n");
391 for (const auto &CSI : CSInfos) {
392 const LocationVec &CSLocs = CSI.Locations;
393 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Andrew Trick153ebe62013-10-31 22:11:56 +0000394
Juergen Ributzka63401952014-05-01 22:21:27 +0000395 DEBUG(dbgs() << WSMP << "callsite " << CSI.ID << "\n");
Andrew Trick153ebe62013-10-31 22:11:56 +0000396
397 // Verify stack map entry. It's better to communicate a problem to the
398 // runtime than crash in case of in-process compilation. Currently, we do
399 // simple overflow checks, but we may eventually communicate other
400 // compilation errors this way.
Juergen Ributzkae8294752013-12-14 06:53:06 +0000401 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
Juergen Ributzka63401952014-05-01 22:21:27 +0000402 OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
403 OS.EmitValue(CSI.CSOffsetExpr, 4);
404 OS.EmitIntValue(0, 2); // Reserved.
405 OS.EmitIntValue(0, 2); // 0 locations.
406 OS.EmitIntValue(0, 2); // padding.
407 OS.EmitIntValue(0, 2); // 0 live-out registers.
408 OS.EmitIntValue(0, 4); // padding.
Andrew Trick153ebe62013-10-31 22:11:56 +0000409 continue;
410 }
411
Juergen Ributzka63401952014-05-01 22:21:27 +0000412 OS.EmitIntValue(CSI.ID, 8);
413 OS.EmitValue(CSI.CSOffsetExpr, 4);
Andrew Trick153ebe62013-10-31 22:11:56 +0000414
415 // Reserved for flags.
Juergen Ributzka63401952014-05-01 22:21:27 +0000416 OS.EmitIntValue(0, 2);
Andrew Trick153ebe62013-10-31 22:11:56 +0000417
418 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
419
Juergen Ributzka63401952014-05-01 22:21:27 +0000420 OS.EmitIntValue(CSLocs.size(), 2);
Andrew Trick153ebe62013-10-31 22:11:56 +0000421
Juergen Ributzka63401952014-05-01 22:21:27 +0000422 unsigned OperIdx = 0;
423 for (const auto &Loc : CSLocs) {
Lang Hamesfde8e4b2013-11-27 20:10:16 +0000424 unsigned RegNo = 0;
425 int Offset = Loc.Offset;
426 if(Loc.Reg) {
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000427 RegNo = getDwarfRegNum(Loc.Reg, TRI);
428
Lang Hamesfde8e4b2013-11-27 20:10:16 +0000429 // If this is a register location, put the subregister byte offset in
430 // the location offset.
431 if (Loc.LocType == Location::Register) {
432 assert(!Loc.Offset && "Register location should have zero offset");
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000433 unsigned LLVMRegNo = TRI->getLLVMRegNum(RegNo, false);
434 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNo, Loc.Reg);
Lang Hamesfde8e4b2013-11-27 20:10:16 +0000435 if (SubRegIdx)
Juergen Ributzka73a7fcc2014-02-10 23:30:26 +0000436 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
Lang Hamesfde8e4b2013-11-27 20:10:16 +0000437 }
438 }
439 else {
440 assert(Loc.LocType != Location::Register &&
441 "Missing location register");
442 }
443
Juergen Ributzka63401952014-05-01 22:21:27 +0000444 DEBUG(dbgs() << WSMP << " Loc " << OperIdx << ": ";
445 switch (Loc.LocType) {
446 case Location::Unprocessed:
447 dbgs() << "<Unprocessed operand>";
448 break;
449 case Location::Register:
450 dbgs() << "Register " << TRI->getName(Loc.Reg);
451 break;
452 case Location::Direct:
453 dbgs() << "Direct " << TRI->getName(Loc.Reg);
454 if (Loc.Offset)
455 dbgs() << " + " << Loc.Offset;
456 break;
457 case Location::Indirect:
458 dbgs() << "Indirect " << TRI->getName(Loc.Reg)
459 << " + " << Loc.Offset;
460 break;
461 case Location::Constant:
462 dbgs() << "Constant " << Loc.Offset;
463 break;
464 case Location::ConstantIndex:
465 dbgs() << "Constant Index " << Loc.Offset;
466 break;
467 }
468 dbgs() << " [encoding: .byte " << Loc.LocType
469 << ", .byte " << Loc.Size
470 << ", .short " << RegNo
471 << ", .int " << Offset << "]\n";
472 );
Andrew Trick153ebe62013-10-31 22:11:56 +0000473
Juergen Ributzka63401952014-05-01 22:21:27 +0000474 OS.EmitIntValue(Loc.LocType, 1);
475 OS.EmitIntValue(Loc.Size, 1);
476 OS.EmitIntValue(RegNo, 2);
477 OS.EmitIntValue(Offset, 4);
478 OperIdx++;
Andrew Trick153ebe62013-10-31 22:11:56 +0000479 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000480
481 DEBUG(dbgs() << WSMP << " has " << LiveOuts.size()
Juergen Ributzka63401952014-05-01 22:21:27 +0000482 << " live-out registers\n");
Juergen Ributzkae8294752013-12-14 06:53:06 +0000483
Juergen Ributzkae1179922014-03-31 22:14:04 +0000484 // Num live-out registers and padding to align to 4 byte.
Juergen Ributzka63401952014-05-01 22:21:27 +0000485 OS.EmitIntValue(0, 2);
486 OS.EmitIntValue(LiveOuts.size(), 2);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000487
Juergen Ributzka63401952014-05-01 22:21:27 +0000488 OperIdx = 0;
489 for (const auto &LO : LiveOuts) {
490 DEBUG(dbgs() << WSMP << " LO " << OperIdx << ": "
491 << TRI->getName(LO.Reg)
492 << " [encoding: .short " << LO.RegNo
493 << ", .byte 0, .byte " << LO.Size << "]\n");
494 OS.EmitIntValue(LO.RegNo, 2);
495 OS.EmitIntValue(0, 1);
496 OS.EmitIntValue(LO.Size, 1);
Juergen Ributzkae8294752013-12-14 06:53:06 +0000497 }
Juergen Ributzkae1179922014-03-31 22:14:04 +0000498 // Emit alignment to 8 byte.
Juergen Ributzka63401952014-05-01 22:21:27 +0000499 OS.EmitValueToAlignment(8);
Andrew Trick153ebe62013-10-31 22:11:56 +0000500 }
Juergen Ributzka63401952014-05-01 22:21:27 +0000501}
Andrew Trick153ebe62013-10-31 22:11:56 +0000502
Juergen Ributzka63401952014-05-01 22:21:27 +0000503/// Serialize the stackmap data.
504void StackMaps::serializeToStackMapSection() {
Juergen Ributzka37fc0a82014-05-01 22:39:26 +0000505 (void) WSMP;
Juergen Ributzka63401952014-05-01 22:21:27 +0000506 // Bail out if there's no stack map data.
507 assert((!CSInfos.empty() || (CSInfos.empty() && ConstPool.empty())) &&
508 "Expected empty constant pool too!");
509 assert((!CSInfos.empty() || (CSInfos.empty() && FnStackSize.empty())) &&
510 "Expected empty function record too!");
511 if (CSInfos.empty())
512 return;
Andrew Trick153ebe62013-10-31 22:11:56 +0000513
Juergen Ributzka63401952014-05-01 22:21:27 +0000514 MCContext &OutContext = AP.OutStreamer.getContext();
515 MCStreamer &OS = AP.OutStreamer;
Eric Christopherd9134482014-08-04 21:25:23 +0000516 const TargetRegisterInfo *TRI = AP.TM.getSubtargetImpl()->getRegisterInfo();
Juergen Ributzka63401952014-05-01 22:21:27 +0000517
518 // Create the section.
519 const MCSection *StackMapSection =
520 OutContext.getObjectFileInfo()->getStackMapSection();
521 OS.SwitchSection(StackMapSection);
522
523 // Emit a dummy symbol to force section inclusion.
524 OS.EmitLabel(OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
525
526 // Serialize data.
527 DEBUG(dbgs() << "********** Stack Map Output **********\n");
528 emitStackmapHeader(OS);
529 emitFunctionFrameRecords(OS);
530 emitConstantPoolEntries(OS);
531 emitCallsiteEntries(OS, TRI);
532 OS.AddBlankLine();
533
534 // Clean up.
Andrew Trick153ebe62013-10-31 22:11:56 +0000535 CSInfos.clear();
Juergen Ributzkaf01e8092014-05-01 22:21:24 +0000536 ConstPool.clear();
Andrew Trick153ebe62013-10-31 22:11:56 +0000537}