1 //===---------------------------- StackMaps.cpp ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "stackmaps"
12 #include "llvm/CodeGen/StackMaps.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetOpcodes.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
31 void StackMaps::recordStackMap(const MachineInstr &MI, uint32_t ID,
32 MachineInstr::const_mop_iterator MOI,
33 MachineInstr::const_mop_iterator MOE,
36 MCContext &OutContext = AP.OutStreamer.getContext();
37 MCSymbol *MILabel = OutContext.CreateTempSymbol();
38 AP.OutStreamer.EmitLabel(MILabel);
40 LocationVec CallsiteLocs;
43 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
44 OpParser(MI.operands_begin(), llvm::next(MI.operands_begin(), 1));
46 Location &Loc = ParseResult.first;
47 assert(Loc.LocType == Location::Register &&
48 "Stackmap return location must be a register.");
49 CallsiteLocs.push_back(Loc);
53 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
56 Location &Loc = ParseResult.first;
58 // Move large constants into the constant pool.
59 if (Loc.LocType == Location::Constant && (Loc.Offset & ~0xFFFFFFFFULL)) {
60 Loc.LocType = Location::ConstantIndex;
61 Loc.Offset = ConstPool.getConstantIndex(Loc.Offset);
64 CallsiteLocs.push_back(Loc);
65 MOI = ParseResult.second;
68 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
69 MCSymbolRefExpr::Create(MILabel, OutContext),
70 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
73 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, CallsiteLocs));
76 /// serializeToStackMapSection conceptually populates the following fields:
78 /// uint32 : Reserved (header)
79 /// uint32 : NumConstants
80 /// int64 : Constants[NumConstants]
81 /// uint32 : NumRecords
82 /// StkMapRecord[NumRecords] {
83 /// uint32 : PatchPoint ID
84 /// uint32 : Instruction Offset
85 /// uint16 : Reserved (record flags)
86 /// uint16 : NumLocations
87 /// Location[NumLocations] {
88 /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
89 /// uint8 : Reserved (location flags)
90 /// uint16 : Dwarf RegNum
95 /// Location Encoding, Type, Value:
96 /// 0x1, Register, Reg (value in register)
97 /// 0x2, Direct, Reg + Offset (frame index)
98 /// 0x3, Indirect, [Reg + Offset] (spilled value)
99 /// 0x4, Constant, Offset (small constant)
100 /// 0x5, ConstIndex, Constants[Offset] (large constant)
102 void StackMaps::serializeToStackMapSection() {
103 // Bail out if there's no stack map data.
107 MCContext &OutContext = AP.OutStreamer.getContext();
108 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
110 // Create the section.
111 const MCSection *StackMapSection =
112 OutContext.getObjectFileInfo()->getStackMapSection();
113 AP.OutStreamer.SwitchSection(StackMapSection);
115 // Emit a dummy symbol to force section inclusion.
116 AP.OutStreamer.EmitLabel(
117 OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
120 const char *WSMP = "Stack Maps: ";
122 const MCRegisterInfo &MCRI = *OutContext.getRegisterInfo();
124 DEBUG(dbgs() << "********** Stack Map Output **********\n");
127 AP.OutStreamer.EmitIntValue(0, 4);
130 AP.OutStreamer.EmitIntValue(ConstPool.getNumConstants(), 4);
132 // Constant pool entries.
133 for (unsigned i = 0; i < ConstPool.getNumConstants(); ++i)
134 AP.OutStreamer.EmitIntValue(ConstPool.getConstant(i), 8);
136 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << "\n");
137 AP.OutStreamer.EmitIntValue(CSInfos.size(), 4);
139 for (CallsiteInfoList::const_iterator CSII = CSInfos.begin(),
140 CSIE = CSInfos.end();
141 CSII != CSIE; ++CSII) {
143 unsigned CallsiteID = CSII->ID;
144 const LocationVec &CSLocs = CSII->Locations;
146 DEBUG(dbgs() << WSMP << "callsite " << CallsiteID << "\n");
148 // Verify stack map entry. It's better to communicate a problem to the
149 // runtime than crash in case of in-process compilation. Currently, we do
150 // simple overflow checks, but we may eventually communicate other
151 // compilation errors this way.
152 if (CSLocs.size() > UINT16_MAX) {
153 AP.OutStreamer.EmitIntValue(UINT32_MAX, 4); // Invalid ID.
154 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
155 AP.OutStreamer.EmitIntValue(0, 2); // Reserved.
156 AP.OutStreamer.EmitIntValue(0, 2); // 0 locations.
160 AP.OutStreamer.EmitIntValue(CallsiteID, 4);
161 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
163 // Reserved for flags.
164 AP.OutStreamer.EmitIntValue(0, 2);
166 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
168 AP.OutStreamer.EmitIntValue(CSLocs.size(), 2);
170 unsigned operIdx = 0;
171 for (LocationVec::const_iterator LocI = CSLocs.begin(), LocE = CSLocs.end();
172 LocI != LocE; ++LocI, ++operIdx) {
173 const Location &Loc = *LocI;
175 dbgs() << WSMP << " Loc " << operIdx << ": ";
176 switch (Loc.LocType) {
177 case Location::Unprocessed:
178 dbgs() << "<Unprocessed operand>";
180 case Location::Register:
181 dbgs() << "Register " << MCRI.getName(Loc.Reg);
183 case Location::Direct:
184 dbgs() << "Direct " << MCRI.getName(Loc.Reg);
186 dbgs() << " + " << Loc.Offset;
188 case Location::Indirect:
189 dbgs() << "Indirect " << MCRI.getName(Loc.Reg)
190 << " + " << Loc.Offset;
192 case Location::Constant:
193 dbgs() << "Constant " << Loc.Offset;
195 case Location::ConstantIndex:
196 dbgs() << "Constant Index " << Loc.Offset;
204 RegNo = MCRI.getDwarfRegNum(Loc.Reg, false);
205 for (MCSuperRegIterator SR(Loc.Reg, TRI);
206 SR.isValid() && (int)RegNo < 0; ++SR) {
207 RegNo = TRI->getDwarfRegNum(*SR, false);
211 assert((Loc.LocType != Location::Register
212 && Loc.LocType != Location::Register) &&
213 "Missing location register");
215 AP.OutStreamer.EmitIntValue(Loc.LocType, 1);
216 AP.OutStreamer.EmitIntValue(0, 1); // Reserved location flags.
217 AP.OutStreamer.EmitIntValue(RegNo, 2);
218 AP.OutStreamer.EmitIntValue(Loc.Offset, 4);
222 AP.OutStreamer.AddBlankLine();