comparison lib/CodeGen/StackMaps.cpp @ 77:54457678186b LLVM3.6

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