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/MCSectionMachO.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetOpcodes.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
30 void StackMaps::recordStackMap(const MachineInstr &MI, uint32_t ID,
31 MachineInstr::const_mop_iterator MOI,
32 MachineInstr::const_mop_iterator MOE) {
34 MCContext &OutContext = AP.OutStreamer.getContext();
35 MCSymbol *MILabel = OutContext.CreateTempSymbol();
36 AP.OutStreamer.EmitLabel(MILabel);
38 LocationVec CallsiteLocs;
41 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
44 Location &Loc = ParseResult.first;
46 // Move large constants into the constant pool.
47 if (Loc.LocType == Location::Constant && (Loc.Offset & ~0xFFFFFFFFULL)) {
48 Loc.LocType = Location::ConstantIndex;
49 Loc.Offset = ConstPool.getConstantIndex(Loc.Offset);
52 CallsiteLocs.push_back(Loc);
53 MOI = ParseResult.second;
56 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
57 MCSymbolRefExpr::Create(MILabel, OutContext),
58 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
61 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, CallsiteLocs));
64 /// serializeToStackMapSection conceptually populates the following fields:
66 /// uint32 : Reserved (header)
67 /// uint32 : NumConstants
68 /// int64 : Constants[NumConstants]
69 /// uint32 : NumRecords
70 /// StkMapRecord[NumRecords] {
71 /// uint32 : PatchPoint ID
72 /// uint32 : Instruction Offset
73 /// uint16 : Reserved (record flags)
74 /// uint16 : NumLocations
75 /// Location[NumLocations] {
76 /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
77 /// uint8 : Reserved (location flags)
78 /// uint16 : Dwarf RegNum
83 /// Location Encoding, Type, Value:
84 /// 0x1, Register, Reg (value in register)
85 /// 0x2, Direct, Reg + Offset (frame index)
86 /// 0x3, Indirect, [Reg + Offset] (spilled value)
87 /// 0x4, Constant, Offset (small constant)
88 /// 0x5, ConstIndex, Constants[Offset] (large constant)
90 void StackMaps::serializeToStackMapSection() {
91 // Bail out if there's no stack map data.
95 MCContext &OutContext = AP.OutStreamer.getContext();
96 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
98 // Create the section.
99 const MCSection *StackMapSection =
100 OutContext.getObjectFileInfo().getStackMapSection();
101 AP.OutStreamer.SwitchSection(StackMapSection);
103 // Emit a dummy symbol to force section inclusion.
104 AP.OutStreamer.EmitLabel(
105 OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
108 const char *WSMP = "Stack Maps: ";
110 const MCRegisterInfo &MCRI = *OutContext.getRegisterInfo();
112 DEBUG(dbgs() << "********** Stack Map Output **********\n");
115 AP.OutStreamer.EmitIntValue(0, 4);
118 AP.OutStreamer.EmitIntValue(ConstPool.getNumConstants(), 4);
120 // Constant pool entries.
121 for (unsigned i = 0; i < ConstPool.getNumConstants(); ++i)
122 AP.OutStreamer.EmitIntValue(ConstPool.getConstant(i), 8);
124 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << "\n");
125 AP.OutStreamer.EmitIntValue(CSInfos.size(), 4);
127 for (CallsiteInfoList::const_iterator CSII = CSInfos.begin(),
128 CSIE = CSInfos.end();
129 CSII != CSIE; ++CSII) {
131 unsigned CallsiteID = CSII->ID;
132 const LocationVec &CSLocs = CSII->Locations;
134 DEBUG(dbgs() << WSMP << "callsite " << CallsiteID << "\n");
136 // Verify stack map entry. It's better to communicate a problem to the
137 // runtime than crash in case of in-process compilation. Currently, we do
138 // simple overflow checks, but we may eventually communicate other
139 // compilation errors this way.
140 if (CSLocs.size() > UINT16_MAX) {
141 AP.OutStreamer.EmitIntValue(UINT32_MAX, 4); // Invalid ID.
142 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
143 AP.OutStreamer.EmitIntValue(0, 2); // Reserved.
144 AP.OutStreamer.EmitIntValue(0, 2); // 0 locations.
148 AP.OutStreamer.EmitIntValue(CallsiteID, 4);
149 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
151 // Reserved for flags.
152 AP.OutStreamer.EmitIntValue(0, 2);
154 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
156 AP.OutStreamer.EmitIntValue(CSLocs.size(), 2);
158 unsigned operIdx = 0;
159 for (LocationVec::const_iterator LocI = CSLocs.begin(), LocE = CSLocs.end();
160 LocI != LocE; ++LocI, ++operIdx) {
161 const Location &Loc = *LocI;
163 dbgs() << WSMP << " Loc " << operIdx << ": ";
164 switch (Loc.LocType) {
165 case Location::Unprocessed:
166 dbgs() << "<Unprocessed operand>";
168 case Location::Register:
169 dbgs() << "Register " << MCRI.getName(Loc.Reg);
171 case Location::Direct:
172 dbgs() << "Direct " << MCRI.getName(Loc.Reg);
174 dbgs() << " + " << Loc.Offset;
176 case Location::Indirect:
177 dbgs() << "Indirect " << MCRI.getName(Loc.Reg)
178 << " + " << Loc.Offset;
180 case Location::Constant:
181 dbgs() << "Constant " << Loc.Offset;
183 case Location::ConstantIndex:
184 dbgs() << "Constant Index " << Loc.Offset;
192 RegNo = MCRI.getDwarfRegNum(Loc.Reg, false);
193 for (MCSuperRegIterator SR(Loc.Reg, TRI);
194 SR.isValid() && (int)RegNo < 0; ++SR) {
195 RegNo = TRI->getDwarfRegNum(*SR, false);
199 assert((Loc.LocType != Location::Register
200 && Loc.LocType != Location::Register) &&
201 "Missing location register");
203 AP.OutStreamer.EmitIntValue(Loc.LocType, 1);
204 AP.OutStreamer.EmitIntValue(0, 1); // Reserved location flags.
205 AP.OutStreamer.EmitIntValue(RegNo, 2);
206 AP.OutStreamer.EmitIntValue(Loc.Offset, 4);
210 AP.OutStreamer.AddBlankLine();