blob: 0676fa2421e8006dae1fde55392ab07d695db78e [file] [log] [blame]
Eugene Zelenko096e40d2017-02-22 22:32:51 +00001//===- StackMaps.cpp ------------------------------------------------------===//
Andrew Trick3d74dea2013-10-31 22:11:56 +00002//
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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/CodeGen/StackMaps.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000011#include "llvm/ADT/DenseMapInfo.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/Twine.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000014#include "llvm/CodeGen/AsmPrinter.h"
Juergen Ributzka014fdcd2014-01-30 18:58:27 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruth1decd562014-03-04 10:07:28 +000016#include "llvm/CodeGen/MachineFunction.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000017#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000018#include "llvm/CodeGen/MachineOperand.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetOpcodes.h"
20#include "llvm/CodeGen/TargetRegisterInfo.h"
21#include "llvm/CodeGen/TargetSubtargetInfo.h"
Lang Hames1cbca512013-11-29 03:07:54 +000022#include "llvm/IR/DataLayout.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000023#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
Lang Hamesde753f42013-11-08 22:30:52 +000025#include "llvm/MC/MCObjectFileInfo.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000026#include "llvm/MC/MCRegisterInfo.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000027#include "llvm/MC/MCStreamer.h"
Juergen Ributzkaa7057942014-05-01 22:21:30 +000028#include "llvm/Support/CommandLine.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/Support/raw_ostream.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000033#include <algorithm>
34#include <cassert>
35#include <cstdint>
Andrew Trick3d74dea2013-10-31 22:11:56 +000036#include <iterator>
Eugene Zelenko096e40d2017-02-22 22:32:51 +000037#include <utility>
Andrew Trick3d74dea2013-10-31 22:11:56 +000038
39using namespace llvm;
40
Chandler Carruth8677f2f2014-04-22 02:02:50 +000041#define DEBUG_TYPE "stackmaps"
42
Juergen Ributzka9a396812015-07-08 22:42:09 +000043static cl::opt<int> StackMapVersion(
Zachary Turner9a4e15c2017-12-01 00:53:10 +000044 "stackmap-version", cl::init(3), cl::Hidden,
Sanjoy Dasedb3c902017-04-28 04:48:42 +000045 cl::desc("Specify the stackmap encoding version (default = 3)"));
Juergen Ributzkaa7057942014-05-01 22:21:30 +000046
Juergen Ributzka30e46552014-05-01 22:39:26 +000047const char *StackMaps::WSMP = "Stack Maps: ";
48
Philip Reames87510032016-08-23 23:33:29 +000049StackMapOpers::StackMapOpers(const MachineInstr *MI)
50 : MI(MI) {
Philip Reames87aa10b2016-08-23 21:21:43 +000051 assert(getVarIdx() <= MI->getNumOperands() &&
52 "invalid stackmap definition");
53}
54
Juergen Ributzkabfee0192013-12-14 23:06:19 +000055PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
Juergen Ributzka9a396812015-07-08 22:42:09 +000056 : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
Philip Reames4ecbb912016-08-23 23:58:08 +000057 !MI->getOperand(0).isImplicit()) {
Andrew Trick8ddf9882013-11-19 03:29:56 +000058#ifndef NDEBUG
Andrew Trick8ddf9882013-11-19 03:29:56 +000059 unsigned CheckStartIdx = 0, e = MI->getNumOperands();
60 while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
61 MI->getOperand(CheckStartIdx).isDef() &&
62 !MI->getOperand(CheckStartIdx).isImplicit())
63 ++CheckStartIdx;
64
65 assert(getMetaIdx() == CheckStartIdx &&
Alp Tokerae43cab62014-01-24 17:20:08 +000066 "Unexpected additional definition in Patchpoint intrinsic.");
Andrew Trick8ddf9882013-11-19 03:29:56 +000067#endif
68}
69
70unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
71 if (!StartIdx)
72 StartIdx = getVarIdx();
73
74 // Find the next scratch register (implicit def and early clobber)
75 unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
76 while (ScratchIdx < e &&
77 !(MI->getOperand(ScratchIdx).isReg() &&
78 MI->getOperand(ScratchIdx).isDef() &&
79 MI->getOperand(ScratchIdx).isImplicit() &&
80 MI->getOperand(ScratchIdx).isEarlyClobber()))
81 ++ScratchIdx;
82
83 assert(ScratchIdx != e && "No scratch register available");
84 return ScratchIdx;
85}
86
Juergen Ributzkaa7057942014-05-01 22:21:30 +000087StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
Sanjoy Dasedb3c902017-04-28 04:48:42 +000088 if (StackMapVersion != 3)
Juergen Ributzkaa7057942014-05-01 22:21:30 +000089 llvm_unreachable("Unsupported stackmap version!");
90}
91
Eric Christopheraa6604c2015-03-20 16:03:42 +000092/// Go up the super-register chain until we hit a valid dwarf register number.
93static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +000094 int RegNum = TRI->getDwarfRegNum(Reg, false);
95 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR)
96 RegNum = TRI->getDwarfRegNum(*SR, false);
Eric Christopheraa6604c2015-03-20 16:03:42 +000097
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +000098 assert(RegNum >= 0 && "Invalid Dwarf register number.");
99 return (unsigned)RegNum;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000100}
101
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000102MachineInstr::const_mop_iterator
Lang Hames1cbca512013-11-29 03:07:54 +0000103StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
Juergen Ributzka9a396812015-07-08 22:42:09 +0000104 MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
105 LiveOutVec &LiveOuts) const {
Eric Christopheraa6604c2015-03-20 16:03:42 +0000106 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000107 if (MOI->isImm()) {
108 switch (MOI->getImm()) {
Juergen Ributzka9a396812015-07-08 22:42:09 +0000109 default:
110 llvm_unreachable("Unrecognized operand type.");
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000111 case StackMaps::DirectMemRefOp: {
Mehdi Amini9c5961b2015-07-16 06:11:10 +0000112 auto &DL = AP.MF->getDataLayout();
113
114 unsigned Size = DL.getPointerSizeInBits();
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000115 assert((Size % 8) == 0 && "Need pointer size in bytes.");
116 Size /= 8;
117 unsigned Reg = (++MOI)->getReg();
118 int64_t Imm = (++MOI)->getImm();
Juergen Ributzka9ba87b82015-07-09 17:11:08 +0000119 Locs.emplace_back(StackMaps::Location::Direct, Size,
120 getDwarfRegNum(Reg, TRI), Imm);
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000121 break;
Lang Hames1cbca512013-11-29 03:07:54 +0000122 }
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000123 case StackMaps::IndirectMemRefOp: {
124 int64_t Size = (++MOI)->getImm();
125 assert(Size > 0 && "Need a valid size for indirect memory locations.");
126 unsigned Reg = (++MOI)->getReg();
127 int64_t Imm = (++MOI)->getImm();
Juergen Ributzka9ba87b82015-07-09 17:11:08 +0000128 Locs.emplace_back(StackMaps::Location::Indirect, Size,
129 getDwarfRegNum(Reg, TRI), Imm);
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000130 break;
131 }
132 case StackMaps::ConstantOp: {
133 ++MOI;
134 assert(MOI->isImm() && "Expected constant operand.");
135 int64_t Imm = MOI->getImm();
Juergen Ributzka9ba87b82015-07-09 17:11:08 +0000136 Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000137 break;
138 }
139 }
140 return ++MOI;
Lang Hames1cbca512013-11-29 03:07:54 +0000141 }
142
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000143 // The physical register number will ultimately be encoded as a DWARF regno.
144 // The stack map also records the size of a spill slot that can hold the
145 // register content. (The runtime can track the actual size of the data type
146 // if it needs to.)
147 if (MOI->isReg()) {
148 // Skip implicit registers (this includes our scratch registers)
149 if (MOI->isImplicit())
150 return ++MOI;
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000151
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000152 assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
153 "Virtreg operands should have been rewritten before now.");
Eric Christopheraa6604c2015-03-20 16:03:42 +0000154 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000155 assert(!MOI->getSubReg() && "Physical subreg still around.");
Eric Christopheraa6604c2015-03-20 16:03:42 +0000156
157 unsigned Offset = 0;
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000158 unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
159 unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false);
160 unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
Eric Christopheraa6604c2015-03-20 16:03:42 +0000161 if (SubRegIdx)
162 Offset = TRI->getSubRegIdxOffset(SubRegIdx);
163
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000164 Locs.emplace_back(Location::Register, TRI->getSpillSize(*RC),
165 DwarfRegNum, Offset);
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000166 return ++MOI;
167 }
168
169 if (MOI->isRegLiveOut())
170 LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
171
172 return ++MOI;
Lang Hames1cbca512013-11-29 03:07:54 +0000173}
174
Eric Christopheraa6604c2015-03-20 16:03:42 +0000175void StackMaps::print(raw_ostream &OS) {
176 const TargetRegisterInfo *TRI =
177 AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
178 OS << WSMP << "callsites:\n";
179 for (const auto &CSI : CSInfos) {
180 const LocationVec &CSLocs = CSI.Locations;
181 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000182
Eric Christopheraa6604c2015-03-20 16:03:42 +0000183 OS << WSMP << "callsite " << CSI.ID << "\n";
184 OS << WSMP << " has " << CSLocs.size() << " locations\n";
185
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000186 unsigned Idx = 0;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000187 for (const auto &Loc : CSLocs) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000188 OS << WSMP << "\t\tLoc " << Idx << ": ";
189 switch (Loc.Type) {
Eric Christopheraa6604c2015-03-20 16:03:42 +0000190 case Location::Unprocessed:
191 OS << "<Unprocessed operand>";
192 break;
193 case Location::Register:
194 OS << "Register ";
Juergen Ributzka9a396812015-07-08 22:42:09 +0000195 if (TRI)
Francis Visoiu Mistrihe6b89912017-11-30 16:12:24 +0000196 OS << printReg(Loc.Reg, TRI);
Juergen Ributzka9a396812015-07-08 22:42:09 +0000197 else
198 OS << Loc.Reg;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000199 break;
200 case Location::Direct:
201 OS << "Direct ";
202 if (TRI)
Francis Visoiu Mistrihe6b89912017-11-30 16:12:24 +0000203 OS << printReg(Loc.Reg, TRI);
Eric Christopheraa6604c2015-03-20 16:03:42 +0000204 else
205 OS << Loc.Reg;
206 if (Loc.Offset)
207 OS << " + " << Loc.Offset;
208 break;
209 case Location::Indirect:
210 OS << "Indirect ";
211 if (TRI)
Francis Visoiu Mistrihe6b89912017-11-30 16:12:24 +0000212 OS << printReg(Loc.Reg, TRI);
Eric Christopheraa6604c2015-03-20 16:03:42 +0000213 else
214 OS << Loc.Reg;
215 OS << "+" << Loc.Offset;
216 break;
217 case Location::Constant:
218 OS << "Constant " << Loc.Offset;
219 break;
220 case Location::ConstantIndex:
221 OS << "Constant Index " << Loc.Offset;
222 break;
223 }
Sanjoy Dasedb3c902017-04-28 04:48:42 +0000224 OS << "\t[encoding: .byte " << Loc.Type << ", .byte 0"
225 << ", .short " << Loc.Size << ", .short " << Loc.Reg << ", .short 0"
226 << ", .int " << Loc.Offset << "]\n";
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000227 Idx++;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000228 }
229
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000230 OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
Eric Christopheraa6604c2015-03-20 16:03:42 +0000231
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000232 Idx = 0;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000233 for (const auto &LO : LiveOuts) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000234 OS << WSMP << "\t\tLO " << Idx << ": ";
Eric Christopheraa6604c2015-03-20 16:03:42 +0000235 if (TRI)
Francis Visoiu Mistrihe6b89912017-11-30 16:12:24 +0000236 OS << printReg(LO.Reg, TRI);
Eric Christopheraa6604c2015-03-20 16:03:42 +0000237 else
238 OS << LO.Reg;
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000239 OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
Eric Christopheraa6604c2015-03-20 16:03:42 +0000240 << LO.Size << "]\n";
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000241 Idx++;
Eric Christopheraa6604c2015-03-20 16:03:42 +0000242 }
243 }
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000244}
245
246/// Create a live-out register record for the given register Reg.
247StackMaps::LiveOutReg
Juergen Ributzka1a66b632014-02-10 23:30:26 +0000248StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000249 unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000250 unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg));
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000251 return LiveOutReg(Reg, DwarfRegNum, Size);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000252}
253
254/// Parse the register live-out mask and return a vector of live-out registers
255/// that need to be recorded in the stackmap.
256StackMaps::LiveOutVec
257StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
258 assert(Mask && "No register mask specified");
Eric Christopher8d5dd672015-03-19 23:27:42 +0000259 const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000260 LiveOutVec LiveOuts;
261
262 // Create a LiveOutReg for each bit that is set in the register mask.
263 for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
264 if ((Mask[Reg / 32] >> Reg % 32) & 1)
Juergen Ributzka1a66b632014-02-10 23:30:26 +0000265 LiveOuts.push_back(createLiveOutReg(Reg, TRI));
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000266
267 // We don't need to keep track of a register if its super-register is already
268 // in the list. Merge entries that refer to the same dwarf register and use
269 // the maximum size that needs to be spilled.
Juergen Ributzkac0149202015-07-09 17:11:15 +0000270
Fangrui Song3b35e172018-09-27 02:13:45 +0000271 llvm::sort(LiveOuts, [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
272 // Only sort by the dwarf register number.
273 return LHS.DwarfRegNum < RHS.DwarfRegNum;
274 });
Juergen Ributzkac0149202015-07-09 17:11:15 +0000275
276 for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
277 for (auto II = std::next(I); II != E; ++II) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000278 if (I->DwarfRegNum != II->DwarfRegNum) {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000279 // Skip all the now invalid entries.
280 I = --II;
281 break;
282 }
283 I->Size = std::max(I->Size, II->Size);
284 if (TRI->isSuperRegister(I->Reg, II->Reg))
285 I->Reg = II->Reg;
Juergen Ributzkac0149202015-07-09 17:11:15 +0000286 II->Reg = 0; // mark for deletion.
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000287 }
288 }
Juergen Ributzkac0149202015-07-09 17:11:15 +0000289
Juergen Ributzka9a396812015-07-08 22:42:09 +0000290 LiveOuts.erase(
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000291 llvm::remove_if(LiveOuts,
292 [](const LiveOutReg &LO) { return LO.Reg == 0; }),
Juergen Ributzka9a396812015-07-08 22:42:09 +0000293 LiveOuts.end());
Juergen Ributzkac0149202015-07-09 17:11:15 +0000294
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000295 return LiveOuts;
296}
297
Andrew Trickcd8314d2013-12-13 18:37:10 +0000298void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
Andrew Trick8ddf9882013-11-19 03:29:56 +0000299 MachineInstr::const_mop_iterator MOI,
300 MachineInstr::const_mop_iterator MOE,
301 bool recordResult) {
Lang Hames579cebf2015-04-24 19:11:51 +0000302 MCContext &OutContext = AP.OutStreamer->getContext();
Jim Grosbach19696da2015-05-18 18:43:14 +0000303 MCSymbol *MILabel = OutContext.createTempSymbol();
Lang Hames579cebf2015-04-24 19:11:51 +0000304 AP.OutStreamer->EmitLabel(MILabel);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000305
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000306 LocationVec Locations;
307 LiveOutVec LiveOuts;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000308
Juergen Ributzka623d2e62013-11-08 23:28:16 +0000309 if (recordResult) {
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000310 assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
Juergen Ributzka9a396812015-07-08 22:42:09 +0000311 parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
312 LiveOuts);
Juergen Ributzka623d2e62013-11-08 23:28:16 +0000313 }
314
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000315 // Parse operands.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000316 while (MOI != MOE) {
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000317 MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
318 }
Andrew Trick3d74dea2013-10-31 22:11:56 +0000319
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000320 // Move large constants into the constant pool.
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000321 for (auto &Loc : Locations) {
Andrew Trickb3ea6d72014-01-09 00:22:31 +0000322 // Constants are encoded as sign-extended integers.
323 // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000324 if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
325 Loc.Type = Location::ConstantIndex;
Sanjoy Das67bcf742014-11-04 00:59:21 +0000326 // ConstPool is intentionally a MapVector of 'uint64_t's (as
327 // opposed to 'int64_t's). We should never be in a situation
328 // where we have to insert either the tombstone or the empty
329 // keys into a map, and for a DenseMap<uint64_t, T> these are
330 // (uint64_t)0 and (uint64_t)-1. They can be and are
331 // represented using 32 bit integers.
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000332 assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
333 (uint64_t)Loc.Offset !=
334 DenseMapInfo<uint64_t>::getTombstoneKey() &&
Sanjoy Das67bcf742014-11-04 00:59:21 +0000335 "empty and tombstone keys should fit in 32 bits!");
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000336 auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
337 Loc.Offset = Result.first - ConstPool.begin();
Andrew Trick3d74dea2013-10-31 22:11:56 +0000338 }
Andrew Trick3d74dea2013-10-31 22:11:56 +0000339 }
340
Juergen Ributzka014fdcd2014-01-30 18:58:27 +0000341 // Create an expression to calculate the offset of the callsite from function
342 // entry.
Jim Grosbach586c0042015-05-30 01:25:56 +0000343 const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
Juergen Ributzka9a396812015-07-08 22:42:09 +0000344 MCSymbolRefExpr::create(MILabel, OutContext),
345 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000346
Benjamin Kramerdbc6d9b2014-10-04 16:55:56 +0000347 CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
348 std::move(LiveOuts));
Juergen Ributzka014fdcd2014-01-30 18:58:27 +0000349
Sanjoy Das9becdee2016-09-14 20:22:03 +0000350 // Record the stack size of the current function and update callsite count.
Matthias Braunf79c57a2016-07-28 18:40:00 +0000351 const MachineFrameInfo &MFI = AP.MF->getFrameInfo();
Eric Christopher60355182014-08-05 02:39:49 +0000352 const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000353 bool HasDynamicFrameSize =
Matthias Braunf79c57a2016-07-28 18:40:00 +0000354 MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
Sanjoy Das9becdee2016-09-14 20:22:03 +0000355 uint64_t FrameSize = HasDynamicFrameSize ? UINT64_MAX : MFI.getStackSize();
356
357 auto CurrentIt = FnInfos.find(AP.CurrentFnSym);
358 if (CurrentIt != FnInfos.end())
359 CurrentIt->second.RecordCount++;
360 else
361 FnInfos.insert(std::make_pair(AP.CurrentFnSym, FunctionInfo(FrameSize)));
Andrew Trick3d74dea2013-10-31 22:11:56 +0000362}
363
Andrew Trick8ddf9882013-11-19 03:29:56 +0000364void StackMaps::recordStackMap(const MachineInstr &MI) {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000365 assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
Andrew Trick8ddf9882013-11-19 03:29:56 +0000366
Philip Reames87aa10b2016-08-23 21:21:43 +0000367 StackMapOpers opers(&MI);
368 const int64_t ID = MI.getOperand(PatchPointOpers::IDPos).getImm();
369 recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), opers.getVarIdx()),
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000370 MI.operands_end());
Andrew Trick8ddf9882013-11-19 03:29:56 +0000371}
372
373void StackMaps::recordPatchPoint(const MachineInstr &MI) {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000374 assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
Andrew Trick8ddf9882013-11-19 03:29:56 +0000375
376 PatchPointOpers opers(&MI);
Philip Reames87510032016-08-23 23:33:29 +0000377 const int64_t ID = opers.getID();
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000378 auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
Juergen Ributzkabfee0192013-12-14 23:06:19 +0000379 recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
Andrew Trick8ddf9882013-11-19 03:29:56 +0000380 opers.isAnyReg() && opers.hasDef());
381
382#ifndef NDEBUG
383 // verify anyregcc
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000384 auto &Locations = CSInfos.back().Locations;
Andrew Trick8ddf9882013-11-19 03:29:56 +0000385 if (opers.isAnyReg()) {
Philip Reames87510032016-08-23 23:33:29 +0000386 unsigned NArgs = opers.getNumCallArgs();
Juergen Ributzka9a396812015-07-08 22:42:09 +0000387 for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000388 assert(Locations[i].Type == Location::Register &&
Andrew Trick8ddf9882013-11-19 03:29:56 +0000389 "anyreg arg must be in reg.");
390 }
391#endif
392}
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000393
Philip Reames78cc6fc2014-12-01 22:52:56 +0000394void StackMaps::recordStatepoint(const MachineInstr &MI) {
Juergen Ributzka9a396812015-07-08 22:42:09 +0000395 assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
Philip Reames78cc6fc2014-12-01 22:52:56 +0000396
397 StatepointOpers opers(&MI);
398 // Record all the deopt and gc operands (they're contiguous and run from the
399 // initial index to the end of the operand list)
400 const unsigned StartIdx = opers.getVarIdx();
Sanjoy Dasead2d1f2015-05-12 23:52:24 +0000401 recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
402 MI.operands_end(), false);
Philip Reames78cc6fc2014-12-01 22:52:56 +0000403}
Andrew Trick8ddf9882013-11-19 03:29:56 +0000404
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000405/// Emit the stackmap header.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000406///
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000407/// Header {
Sanjoy Das9becdee2016-09-14 20:22:03 +0000408/// uint8 : Stack Map Version (currently 2)
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000409/// uint8 : Reserved (expected to be 0)
410/// uint16 : Reserved (expected to be 0)
Juergen Ributzka014fdcd2014-01-30 18:58:27 +0000411/// }
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000412/// uint32 : NumFunctions
Andrew Trick3d74dea2013-10-31 22:11:56 +0000413/// uint32 : NumConstants
Andrew Trick3d74dea2013-10-31 22:11:56 +0000414/// uint32 : NumRecords
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000415void StackMaps::emitStackmapHeader(MCStreamer &OS) {
416 // Header.
Juergen Ributzkaa7057942014-05-01 22:21:30 +0000417 OS.EmitIntValue(StackMapVersion, 1); // Version.
Juergen Ributzka9a396812015-07-08 22:42:09 +0000418 OS.EmitIntValue(0, 1); // Reserved.
419 OS.EmitIntValue(0, 2); // Reserved.
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000420
421 // Num functions.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000422 LLVM_DEBUG(dbgs() << WSMP << "#functions = " << FnInfos.size() << '\n');
Sanjoy Das9becdee2016-09-14 20:22:03 +0000423 OS.EmitIntValue(FnInfos.size(), 4);
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000424 // Num constants.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000425 LLVM_DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000426 OS.EmitIntValue(ConstPool.size(), 4);
427 // Num callsites.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000428 LLVM_DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000429 OS.EmitIntValue(CSInfos.size(), 4);
430}
431
432/// Emit the function frame record for each function.
433///
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000434/// StkSizeRecord[NumFunctions] {
435/// uint64 : Function Address
436/// uint64 : Stack Size
Sanjoy Das9becdee2016-09-14 20:22:03 +0000437/// uint64 : Record Count
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000438/// }
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000439void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
440 // Function Frame records.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000441 LLVM_DEBUG(dbgs() << WSMP << "functions:\n");
Sanjoy Das9becdee2016-09-14 20:22:03 +0000442 for (auto const &FR : FnInfos) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000443 LLVM_DEBUG(dbgs() << WSMP << "function addr: " << FR.first
444 << " frame size: " << FR.second.StackSize
445 << " callsite count: " << FR.second.RecordCount << '\n');
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000446 OS.EmitSymbolValue(FR.first, 8);
Sanjoy Das9becdee2016-09-14 20:22:03 +0000447 OS.EmitIntValue(FR.second.StackSize, 8);
448 OS.EmitIntValue(FR.second.RecordCount, 8);
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000449 }
450}
451
452/// Emit the constant pool.
453///
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000454/// int64 : Constants[NumConstants]
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000455void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
456 // Constant pool entries.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000457 LLVM_DEBUG(dbgs() << WSMP << "constants:\n");
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000458 for (const auto &ConstEntry : ConstPool) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000459 LLVM_DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000460 OS.EmitIntValue(ConstEntry.second, 8);
461 }
462}
463
464/// Emit the callsite info for each callsite.
465///
Andrew Trick3d74dea2013-10-31 22:11:56 +0000466/// StkMapRecord[NumRecords] {
Andrew Trickcd8314d2013-12-13 18:37:10 +0000467/// uint64 : PatchPoint ID
Andrew Trick3d74dea2013-10-31 22:11:56 +0000468/// uint32 : Instruction Offset
469/// uint16 : Reserved (record flags)
470/// uint16 : NumLocations
471/// Location[NumLocations] {
472/// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
Andrew Trickbb756ca2013-11-17 01:36:23 +0000473/// uint8 : Size in Bytes
Andrew Trick3d74dea2013-10-31 22:11:56 +0000474/// uint16 : Dwarf RegNum
475/// int32 : Offset
476/// }
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000477/// uint16 : Padding
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000478/// uint16 : NumLiveOuts
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000479/// LiveOuts[NumLiveOuts] {
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000480/// uint16 : Dwarf RegNum
481/// uint8 : Reserved
482/// uint8 : Size in Bytes
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000483/// }
484/// uint32 : Padding (only if required to align to 8 byte)
Andrew Trick3d74dea2013-10-31 22:11:56 +0000485/// }
486///
487/// Location Encoding, Type, Value:
488/// 0x1, Register, Reg (value in register)
489/// 0x2, Direct, Reg + Offset (frame index)
490/// 0x3, Indirect, [Reg + Offset] (spilled value)
491/// 0x4, Constant, Offset (small constant)
492/// 0x5, ConstIndex, Constants[Offset] (large constant)
Eric Christopheraaca69b2015-03-20 21:05:18 +0000493void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000494 LLVM_DEBUG(print(dbgs()));
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000495 // Callsite entries.
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000496 for (const auto &CSI : CSInfos) {
497 const LocationVec &CSLocs = CSI.Locations;
498 const LiveOutVec &LiveOuts = CSI.LiveOuts;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000499
Andrew Trick3d74dea2013-10-31 22:11:56 +0000500 // Verify stack map entry. It's better to communicate a problem to the
501 // runtime than crash in case of in-process compilation. Currently, we do
502 // simple overflow checks, but we may eventually communicate other
503 // compilation errors this way.
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000504 if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000505 OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
506 OS.EmitValue(CSI.CSOffsetExpr, 4);
507 OS.EmitIntValue(0, 2); // Reserved.
508 OS.EmitIntValue(0, 2); // 0 locations.
509 OS.EmitIntValue(0, 2); // padding.
510 OS.EmitIntValue(0, 2); // 0 live-out registers.
511 OS.EmitIntValue(0, 4); // padding.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000512 continue;
513 }
514
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000515 OS.EmitIntValue(CSI.ID, 8);
516 OS.EmitValue(CSI.CSOffsetExpr, 4);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000517
518 // Reserved for flags.
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000519 OS.EmitIntValue(0, 2);
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000520 OS.EmitIntValue(CSLocs.size(), 2);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000521
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000522 for (const auto &Loc : CSLocs) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000523 OS.EmitIntValue(Loc.Type, 1);
Sanjoy Dasedb3c902017-04-28 04:48:42 +0000524 OS.EmitIntValue(0, 1); // Reserved
525 OS.EmitIntValue(Loc.Size, 2);
Eric Christopheraa6604c2015-03-20 16:03:42 +0000526 OS.EmitIntValue(Loc.Reg, 2);
Sanjoy Dasedb3c902017-04-28 04:48:42 +0000527 OS.EmitIntValue(0, 2); // Reserved
Eric Christopheraa6604c2015-03-20 16:03:42 +0000528 OS.EmitIntValue(Loc.Offset, 4);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000529 }
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000530
Sanjoy Dasedb3c902017-04-28 04:48:42 +0000531 // Emit alignment to 8 byte.
532 OS.EmitValueToAlignment(8);
533
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000534 // Num live-out registers and padding to align to 4 byte.
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000535 OS.EmitIntValue(0, 2);
536 OS.EmitIntValue(LiveOuts.size(), 2);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000537
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000538 for (const auto &LO : LiveOuts) {
Juergen Ributzkafd5ef9d2015-07-09 17:11:11 +0000539 OS.EmitIntValue(LO.DwarfRegNum, 2);
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000540 OS.EmitIntValue(0, 1);
541 OS.EmitIntValue(LO.Size, 1);
Juergen Ributzkaaaecc0f2013-12-14 06:53:06 +0000542 }
Juergen Ributzkad8c95772014-03-31 22:14:04 +0000543 // Emit alignment to 8 byte.
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000544 OS.EmitValueToAlignment(8);
Andrew Trick3d74dea2013-10-31 22:11:56 +0000545 }
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000546}
Andrew Trick3d74dea2013-10-31 22:11:56 +0000547
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000548/// Serialize the stackmap data.
549void StackMaps::serializeToStackMapSection() {
Juergen Ributzka9a396812015-07-08 22:42:09 +0000550 (void)WSMP;
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000551 // Bail out if there's no stack map data.
Hans Wennborg683020d2016-04-11 20:35:01 +0000552 assert((!CSInfos.empty() || ConstPool.empty()) &&
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000553 "Expected empty constant pool too!");
Sanjoy Das9becdee2016-09-14 20:22:03 +0000554 assert((!CSInfos.empty() || FnInfos.empty()) &&
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000555 "Expected empty function record too!");
556 if (CSInfos.empty())
557 return;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000558
Lang Hames579cebf2015-04-24 19:11:51 +0000559 MCContext &OutContext = AP.OutStreamer->getContext();
560 MCStreamer &OS = *AP.OutStreamer;
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000561
562 // Create the section.
Rafael Espindola75219642015-05-21 19:20:38 +0000563 MCSection *StackMapSection =
564 OutContext.getObjectFileInfo()->getStackMapSection();
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000565 OS.SwitchSection(StackMapSection);
566
567 // Emit a dummy symbol to force section inclusion.
Jim Grosbach19696da2015-05-18 18:43:14 +0000568 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000569
570 // Serialize data.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000571 LLVM_DEBUG(dbgs() << "********** Stack Map Output **********\n");
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000572 emitStackmapHeader(OS);
573 emitFunctionFrameRecords(OS);
574 emitConstantPoolEntries(OS);
Eric Christopheraaca69b2015-03-20 21:05:18 +0000575 emitCallsiteEntries(OS);
Juergen Ributzkaa6775522014-05-01 22:21:27 +0000576 OS.AddBlankLine();
577
578 // Clean up.
Andrew Trick3d74dea2013-10-31 22:11:56 +0000579 CSInfos.clear();
Juergen Ributzka23fc1722014-05-01 22:21:24 +0000580 ConstPool.clear();
Andrew Trick3d74dea2013-10-31 22:11:56 +0000581}