blob: 1473fc184f84a3c226c6db15ccc2b8af6a70cd23 [file] [log] [blame]
Andrew Trick3d74dea2013-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 Trick3d74dea2013-10-31 22:11:56 +000010#include "llvm/CodeGen/StackMaps.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000011#include "llvm/CodeGen/AsmPrinter.h"
Stephen Hines36b56882014-04-23 16:57:46 -070012#include "llvm/CodeGen/MachineFrameInfo.h"
13#include "llvm/CodeGen/MachineFunction.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000014#include "llvm/CodeGen/MachineInstr.h"
Stephen Hines36b56882014-04-23 16:57:46 -070015#include "llvm/IR/DataLayout.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
Lang Hamesde753f42013-11-08 22:30:52 +000018#include "llvm/MC/MCObjectFileInfo.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000019#include "llvm/MC/MCSectionMachO.h"
20#include "llvm/MC/MCStreamer.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070021#include "llvm/Support/CommandLine.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000024#include "llvm/Target/TargetMachine.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/Target/TargetOpcodes.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000026#include "llvm/Target/TargetRegisterInfo.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000027#include <iterator>
28
29using namespace llvm;
30
Stephen Hinesdce4a402014-05-29 02:49:00 -070031#define DEBUG_TYPE "stackmaps"
32
33static cl::opt<int> StackMapVersion("stackmap-version", cl::init(1),
34 cl::desc("Specify the stackmap encoding version (default = 1)"));
35
36const char *StackMaps::WSMP = "Stack Maps: ";
37
Stephen Hines36b56882014-04-23 16:57:46 -070038PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
39 : MI(MI),
40 HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
41 !MI->getOperand(0).isImplicit()),
42 IsAnyReg(MI->getOperand(getMetaIdx(CCPos)).getImm() == CallingConv::AnyReg)
43{
Bill Wendling72ef53a2013-11-19 06:36:46 +000044#ifndef NDEBUG
Bill Wendling72ef53a2013-11-19 06:36:46 +000045 unsigned CheckStartIdx = 0, e = MI->getNumOperands();
46 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
47 MI->getOperand(CheckStartIdx).isDef() &&
48 !MI->getOperand(CheckStartIdx).isImplicit())
49 ++CheckStartIdx;
50
51 assert(getMetaIdx() == CheckStartIdx &&
Stephen Hines36b56882014-04-23 16:57:46 -070052 "Unexpected additional definition in Patchpoint intrinsic.");
Bill Wendling72ef53a2013-11-19 06:36:46 +000053#endif
54}
55
56unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
57 if (!StartIdx)
58 StartIdx = getVarIdx();
59
60 // Find the next scratch register (implicit def and early clobber)
61 unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
62 while (ScratchIdx < e &&
63 !(MI->getOperand(ScratchIdx).isReg() &&
64 MI->getOperand(ScratchIdx).isDef() &&
65 MI->getOperand(ScratchIdx).isImplicit() &&
66 MI->getOperand(ScratchIdx).isEarlyClobber()))
67 ++ScratchIdx;
68
69 assert(ScratchIdx != e && "No scratch register available");
70 return ScratchIdx;
71}
72
Stephen Hinesdce4a402014-05-29 02:49:00 -070073StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
74 if (StackMapVersion != 1)
75 llvm_unreachable("Unsupported stackmap version!");
76}
77
Stephen Hines36b56882014-04-23 16:57:46 -070078MachineInstr::const_mop_iterator
79StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
80 MachineInstr::const_mop_iterator MOE,
81 LocationVec &Locs, LiveOutVec &LiveOuts) const {
82 if (MOI->isImm()) {
83 switch (MOI->getImm()) {
84 default: llvm_unreachable("Unrecognized operand type.");
85 case StackMaps::DirectMemRefOp: {
86 unsigned Size = AP.TM.getDataLayout()->getPointerSizeInBits();
87 assert((Size % 8) == 0 && "Need pointer size in bytes.");
88 Size /= 8;
89 unsigned Reg = (++MOI)->getReg();
90 int64_t Imm = (++MOI)->getImm();
91 Locs.push_back(Location(StackMaps::Location::Direct, Size, Reg, Imm));
92 break;
93 }
94 case StackMaps::IndirectMemRefOp: {
95 int64_t Size = (++MOI)->getImm();
96 assert(Size > 0 && "Need a valid size for indirect memory locations.");
97 unsigned Reg = (++MOI)->getReg();
98 int64_t Imm = (++MOI)->getImm();
99 Locs.push_back(Location(StackMaps::Location::Indirect, Size, Reg, Imm));
100 break;
101 }
102 case StackMaps::ConstantOp: {
103 ++MOI;
104 assert(MOI->isImm() && "Expected constant operand.");
105 int64_t Imm = MOI->getImm();
106 Locs.push_back(Location(Location::Constant, sizeof(int64_t), 0, Imm));
107 break;
108 }
109 }
110 return ++MOI;
111 }
112
113 // The physical register number will ultimately be encoded as a DWARF regno.
114 // The stack map also records the size of a spill slot that can hold the
115 // register content. (The runtime can track the actual size of the data type
116 // if it needs to.)
117 if (MOI->isReg()) {
118 // Skip implicit registers (this includes our scratch registers)
119 if (MOI->isImplicit())
120 return ++MOI;
121
122 assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
123 "Virtreg operands should have been rewritten before now.");
124 const TargetRegisterClass *RC =
125 AP.TM.getRegisterInfo()->getMinimalPhysRegClass(MOI->getReg());
126 assert(!MOI->getSubReg() && "Physical subreg still around.");
127 Locs.push_back(
128 Location(Location::Register, RC->getSize(), MOI->getReg(), 0));
129 return ++MOI;
130 }
131
132 if (MOI->isRegLiveOut())
133 LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
134
135 return ++MOI;
136}
137
138/// Go up the super-register chain until we hit a valid dwarf register number.
139static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
140 int RegNo = TRI->getDwarfRegNum(Reg, false);
141 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNo < 0; ++SR)
142 RegNo = TRI->getDwarfRegNum(*SR, false);
143
144 assert(RegNo >= 0 && "Invalid Dwarf register number.");
145 return (unsigned) RegNo;
146}
147
148/// Create a live-out register record for the given register Reg.
149StackMaps::LiveOutReg
150StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
151 unsigned RegNo = getDwarfRegNum(Reg, TRI);
152 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
153 return LiveOutReg(Reg, RegNo, Size);
154}
155
156/// Parse the register live-out mask and return a vector of live-out registers
157/// that need to be recorded in the stackmap.
158StackMaps::LiveOutVec
159StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
160 assert(Mask && "No register mask specified");
161 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
162 LiveOutVec LiveOuts;
163
164 // Create a LiveOutReg for each bit that is set in the register mask.
165 for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
166 if ((Mask[Reg / 32] >> Reg % 32) & 1)
167 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
168
169 // We don't need to keep track of a register if its super-register is already
170 // in the list. Merge entries that refer to the same dwarf register and use
171 // the maximum size that needs to be spilled.
172 std::sort(LiveOuts.begin(), LiveOuts.end());
173 for (LiveOutVec::iterator I = LiveOuts.begin(), E = LiveOuts.end();
174 I != E; ++I) {
175 for (LiveOutVec::iterator II = std::next(I); II != E; ++II) {
176 if (I->RegNo != II->RegNo) {
177 // Skip all the now invalid entries.
178 I = --II;
179 break;
180 }
181 I->Size = std::max(I->Size, II->Size);
182 if (TRI->isSuperRegister(I->Reg, II->Reg))
183 I->Reg = II->Reg;
184 II->MarkInvalid();
185 }
186 }
187 LiveOuts.erase(std::remove_if(LiveOuts.begin(), LiveOuts.end(),
188 LiveOutReg::IsInvalid), LiveOuts.end());
189 return LiveOuts;
190}
191
192void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
Bill Wendling72ef53a2013-11-19 06:36:46 +0000193 MachineInstr::const_mop_iterator MOI,
194 MachineInstr::const_mop_iterator MOE,
195 bool recordResult) {
Andrew Trick3d74dea2013-10-31 22:11:56 +0000196
197 MCContext &OutContext = AP.OutStreamer.getContext();
198 MCSymbol *MILabel = OutContext.CreateTempSymbol();
199 AP.OutStreamer.EmitLabel(MILabel);
200
Stephen Hines36b56882014-04-23 16:57:46 -0700201 LocationVec Locations;
202 LiveOutVec LiveOuts;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000203
Juergen Ributzka623d2e62013-11-08 23:28:16 +0000204 if (recordResult) {
Stephen Hines36b56882014-04-23 16:57:46 -0700205 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
206 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()),
207 Locations, LiveOuts);
Juergen Ributzka623d2e62013-11-08 23:28:16 +0000208 }
209
Stephen Hines36b56882014-04-23 16:57:46 -0700210 // Parse operands.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000211 while (MOI != MOE) {
Stephen Hines36b56882014-04-23 16:57:46 -0700212 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000213 }
214
Stephen Hines36b56882014-04-23 16:57:46 -0700215 // Move large constants into the constant pool.
216 for (LocationVec::iterator I = Locations.begin(), E = Locations.end();
217 I != E; ++I) {
218 // Constants are encoded as sign-extended integers.
219 // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
220 if (I->LocType == Location::Constant &&
221 ((I->Offset + (int64_t(1)<<31)) >> 32) != 0) {
222 I->LocType = Location::ConstantIndex;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700223 auto Result = ConstPool.insert(std::make_pair(I->Offset, I->Offset));
224 I->Offset = Result.first - ConstPool.begin();
Stephen Hines36b56882014-04-23 16:57:46 -0700225 }
226 }
227
228 // Create an expression to calculate the offset of the callsite from function
229 // entry.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000230 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
231 MCSymbolRefExpr::Create(MILabel, OutContext),
232 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
233 OutContext);
234
Stephen Hines36b56882014-04-23 16:57:46 -0700235 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, Locations, LiveOuts));
Andrew Trick3d74dea2013-10-31 22:11:56 +0000236
Stephen Hines36b56882014-04-23 16:57:46 -0700237 // Record the stack size of the current function.
238 const MachineFrameInfo *MFI = AP.MF->getFrameInfo();
239 FnStackSize[AP.CurrentFnSym] =
240 MFI->hasVarSizedObjects() ? UINT64_MAX : MFI->getStackSize();
Bill Wendling72ef53a2013-11-19 06:36:46 +0000241}
242
243void StackMaps::recordStackMap(const MachineInstr &MI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700244 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
Bill Wendling72ef53a2013-11-19 06:36:46 +0000245
246 int64_t ID = MI.getOperand(0).getImm();
Stephen Hines36b56882014-04-23 16:57:46 -0700247 recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2),
248 MI.operands_end());
Bill Wendling72ef53a2013-11-19 06:36:46 +0000249}
250
251void StackMaps::recordPatchPoint(const MachineInstr &MI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700252 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
Bill Wendling72ef53a2013-11-19 06:36:46 +0000253
254 PatchPointOpers opers(&MI);
255 int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm();
Stephen Hines36b56882014-04-23 16:57:46 -0700256
Bill Wendling72ef53a2013-11-19 06:36:46 +0000257 MachineInstr::const_mop_iterator MOI =
Stephen Hines36b56882014-04-23 16:57:46 -0700258 std::next(MI.operands_begin(), opers.getStackMapStartIdx());
259 recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
Bill Wendling72ef53a2013-11-19 06:36:46 +0000260 opers.isAnyReg() && opers.hasDef());
261
262#ifndef NDEBUG
263 // verify anyregcc
264 LocationVec &Locations = CSInfos.back().Locations;
265 if (opers.isAnyReg()) {
266 unsigned NArgs = opers.getMetaOper(PatchPointOpers::NArgPos).getImm();
267 for (unsigned i = 0, e = (opers.hasDef() ? NArgs+1 : NArgs); i != e; ++i)
268 assert(Locations[i].LocType == Location::Register &&
269 "anyreg arg must be in reg.");
270 }
271#endif
272}
273
Stephen Hinesdce4a402014-05-29 02:49:00 -0700274/// Emit the stackmap header.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000275///
Stephen Hines36b56882014-04-23 16:57:46 -0700276/// Header {
277/// uint8 : Stack Map Version (currently 1)
278/// uint8 : Reserved (expected to be 0)
279/// uint16 : Reserved (expected to be 0)
280/// }
281/// uint32 : NumFunctions
Andrew Trick3d74dea2013-10-31 22:11:56 +0000282/// uint32 : NumConstants
Andrew Trick3d74dea2013-10-31 22:11:56 +0000283/// uint32 : NumRecords
Stephen Hinesdce4a402014-05-29 02:49:00 -0700284void StackMaps::emitStackmapHeader(MCStreamer &OS) {
285 // Header.
286 OS.EmitIntValue(StackMapVersion, 1); // Version.
287 OS.EmitIntValue(0, 1); // Reserved.
288 OS.EmitIntValue(0, 2); // Reserved.
289
290 // Num functions.
291 DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
292 OS.EmitIntValue(FnStackSize.size(), 4);
293 // Num constants.
294 DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
295 OS.EmitIntValue(ConstPool.size(), 4);
296 // Num callsites.
297 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
298 OS.EmitIntValue(CSInfos.size(), 4);
299}
300
301/// Emit the function frame record for each function.
302///
Stephen Hines36b56882014-04-23 16:57:46 -0700303/// StkSizeRecord[NumFunctions] {
304/// uint64 : Function Address
305/// uint64 : Stack Size
306/// }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700307void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
308 // Function Frame records.
309 DEBUG(dbgs() << WSMP << "functions:\n");
310 for (auto const &FR : FnStackSize) {
311 DEBUG(dbgs() << WSMP << "function addr: " << FR.first
312 << " frame size: " << FR.second);
313 OS.EmitSymbolValue(FR.first, 8);
314 OS.EmitIntValue(FR.second, 8);
315 }
316}
317
318/// Emit the constant pool.
319///
Stephen Hines36b56882014-04-23 16:57:46 -0700320/// int64 : Constants[NumConstants]
Stephen Hinesdce4a402014-05-29 02:49:00 -0700321void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
322 // Constant pool entries.
323 DEBUG(dbgs() << WSMP << "constants:\n");
324 for (auto ConstEntry : ConstPool) {
325 DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
326 OS.EmitIntValue(ConstEntry.second, 8);
327 }
328}
329
330/// Emit the callsite info for each callsite.
331///
Andrew Trick3d74dea2013-10-31 22:11:56 +0000332/// StkMapRecord[NumRecords] {
Stephen Hines36b56882014-04-23 16:57:46 -0700333/// uint64 : PatchPoint ID
Andrew Trick3d74dea2013-10-31 22:11:56 +0000334/// uint32 : Instruction Offset
335/// uint16 : Reserved (record flags)
336/// uint16 : NumLocations
337/// Location[NumLocations] {
338/// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
Andrew Trickbb756ca2013-11-17 01:36:23 +0000339/// uint8 : Size in Bytes
Andrew Trick3d74dea2013-10-31 22:11:56 +0000340/// uint16 : Dwarf RegNum
341/// int32 : Offset
342/// }
Stephen Hines36b56882014-04-23 16:57:46 -0700343/// uint16 : Padding
344/// uint16 : NumLiveOuts
345/// LiveOuts[NumLiveOuts] {
346/// uint16 : Dwarf RegNum
347/// uint8 : Reserved
348/// uint8 : Size in Bytes
349/// }
350/// uint32 : Padding (only if required to align to 8 byte)
Andrew Trick3d74dea2013-10-31 22:11:56 +0000351/// }
352///
353/// Location Encoding, Type, Value:
354/// 0x1, Register, Reg (value in register)
355/// 0x2, Direct, Reg + Offset (frame index)
356/// 0x3, Indirect, [Reg + Offset] (spilled value)
357/// 0x4, Constant, Offset (small constant)
358/// 0x5, ConstIndex, Constants[Offset] (large constant)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700359void StackMaps::emitCallsiteEntries(MCStreamer &OS,
360 const TargetRegisterInfo *TRI) {
Stephen Hines36b56882014-04-23 16:57:46 -0700361 // Callsite entries.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700362 DEBUG(dbgs() << WSMP << "callsites:\n");
363 for (const auto &CSI : CSInfos) {
364 const LocationVec &CSLocs = CSI.Locations;
365 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000366
Stephen Hinesdce4a402014-05-29 02:49:00 -0700367 DEBUG(dbgs() << WSMP << "callsite " << CSI.ID << "\n");
Andrew Trick3d74dea2013-10-31 22:11:56 +0000368
369 // Verify stack map entry. It's better to communicate a problem to the
370 // runtime than crash in case of in-process compilation. Currently, we do
371 // simple overflow checks, but we may eventually communicate other
372 // compilation errors this way.
Stephen Hines36b56882014-04-23 16:57:46 -0700373 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700374 OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
375 OS.EmitValue(CSI.CSOffsetExpr, 4);
376 OS.EmitIntValue(0, 2); // Reserved.
377 OS.EmitIntValue(0, 2); // 0 locations.
378 OS.EmitIntValue(0, 2); // padding.
379 OS.EmitIntValue(0, 2); // 0 live-out registers.
380 OS.EmitIntValue(0, 4); // padding.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000381 continue;
382 }
383
Stephen Hinesdce4a402014-05-29 02:49:00 -0700384 OS.EmitIntValue(CSI.ID, 8);
385 OS.EmitValue(CSI.CSOffsetExpr, 4);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000386
387 // Reserved for flags.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700388 OS.EmitIntValue(0, 2);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000389
390 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
391
Stephen Hinesdce4a402014-05-29 02:49:00 -0700392 OS.EmitIntValue(CSLocs.size(), 2);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000393
Stephen Hinesdce4a402014-05-29 02:49:00 -0700394 unsigned OperIdx = 0;
395 for (const auto &Loc : CSLocs) {
Stephen Hines36b56882014-04-23 16:57:46 -0700396 unsigned RegNo = 0;
397 int Offset = Loc.Offset;
398 if(Loc.Reg) {
399 RegNo = getDwarfRegNum(Loc.Reg, TRI);
400
401 // If this is a register location, put the subregister byte offset in
402 // the location offset.
403 if (Loc.LocType == Location::Register) {
404 assert(!Loc.Offset && "Register location should have zero offset");
405 unsigned LLVMRegNo = TRI->getLLVMRegNum(RegNo, false);
406 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNo, Loc.Reg);
407 if (SubRegIdx)
408 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
409 }
410 }
411 else {
412 assert(Loc.LocType != Location::Register &&
413 "Missing location register");
414 }
415
Stephen Hinesdce4a402014-05-29 02:49:00 -0700416 DEBUG(dbgs() << WSMP << " Loc " << OperIdx << ": ";
417 switch (Loc.LocType) {
418 case Location::Unprocessed:
419 dbgs() << "<Unprocessed operand>";
420 break;
421 case Location::Register:
422 dbgs() << "Register " << TRI->getName(Loc.Reg);
423 break;
424 case Location::Direct:
425 dbgs() << "Direct " << TRI->getName(Loc.Reg);
426 if (Loc.Offset)
427 dbgs() << " + " << Loc.Offset;
428 break;
429 case Location::Indirect:
430 dbgs() << "Indirect " << TRI->getName(Loc.Reg)
431 << " + " << Loc.Offset;
432 break;
433 case Location::Constant:
434 dbgs() << "Constant " << Loc.Offset;
435 break;
436 case Location::ConstantIndex:
437 dbgs() << "Constant Index " << Loc.Offset;
438 break;
439 }
440 dbgs() << " [encoding: .byte " << Loc.LocType
441 << ", .byte " << Loc.Size
442 << ", .short " << RegNo
443 << ", .int " << Offset << "]\n";
444 );
Andrew Trick3d74dea2013-10-31 22:11:56 +0000445
Stephen Hinesdce4a402014-05-29 02:49:00 -0700446 OS.EmitIntValue(Loc.LocType, 1);
447 OS.EmitIntValue(Loc.Size, 1);
448 OS.EmitIntValue(RegNo, 2);
449 OS.EmitIntValue(Offset, 4);
450 OperIdx++;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000451 }
Stephen Hines36b56882014-04-23 16:57:46 -0700452
453 DEBUG(dbgs() << WSMP << " has " << LiveOuts.size()
Stephen Hinesdce4a402014-05-29 02:49:00 -0700454 << " live-out registers\n");
Stephen Hines36b56882014-04-23 16:57:46 -0700455
456 // Num live-out registers and padding to align to 4 byte.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700457 OS.EmitIntValue(0, 2);
458 OS.EmitIntValue(LiveOuts.size(), 2);
Stephen Hines36b56882014-04-23 16:57:46 -0700459
Stephen Hinesdce4a402014-05-29 02:49:00 -0700460 OperIdx = 0;
461 for (const auto &LO : LiveOuts) {
462 DEBUG(dbgs() << WSMP << " LO " << OperIdx << ": "
463 << TRI->getName(LO.Reg)
464 << " [encoding: .short " << LO.RegNo
465 << ", .byte 0, .byte " << LO.Size << "]\n");
466 OS.EmitIntValue(LO.RegNo, 2);
467 OS.EmitIntValue(0, 1);
468 OS.EmitIntValue(LO.Size, 1);
Stephen Hines36b56882014-04-23 16:57:46 -0700469 }
470 // Emit alignment to 8 byte.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700471 OS.EmitValueToAlignment(8);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000472 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700473}
Andrew Trick3d74dea2013-10-31 22:11:56 +0000474
Stephen Hinesdce4a402014-05-29 02:49:00 -0700475/// Serialize the stackmap data.
476void StackMaps::serializeToStackMapSection() {
477 (void) WSMP;
478 // Bail out if there's no stack map data.
479 assert((!CSInfos.empty() || (CSInfos.empty() && ConstPool.empty())) &&
480 "Expected empty constant pool too!");
481 assert((!CSInfos.empty() || (CSInfos.empty() && FnStackSize.empty())) &&
482 "Expected empty function record too!");
483 if (CSInfos.empty())
484 return;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000485
Stephen Hinesdce4a402014-05-29 02:49:00 -0700486 MCContext &OutContext = AP.OutStreamer.getContext();
487 MCStreamer &OS = AP.OutStreamer;
488 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
489
490 // Create the section.
491 const MCSection *StackMapSection =
492 OutContext.getObjectFileInfo()->getStackMapSection();
493 OS.SwitchSection(StackMapSection);
494
495 // Emit a dummy symbol to force section inclusion.
496 OS.EmitLabel(OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
497
498 // Serialize data.
499 DEBUG(dbgs() << "********** Stack Map Output **********\n");
500 emitStackmapHeader(OS);
501 emitFunctionFrameRecords(OS);
502 emitConstantPoolEntries(OS);
503 emitCallsiteEntries(OS, TRI);
504 OS.AddBlankLine();
505
506 // Clean up.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000507 CSInfos.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700508 ConstPool.clear();
Andrew Trick3d74dea2013-10-31 22:11:56 +0000509}