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) {
35 MCContext &OutContext = AP.OutStreamer.getContext();
36 MCSymbol *MILabel = OutContext.CreateTempSymbol();
37 AP.OutStreamer.EmitLabel(MILabel);
39 LocationVec CallsiteLocs;
42 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
45 Location &Loc = ParseResult.first;
47 // Move large constants into the constant pool.
48 if (Loc.LocType == Location::Constant && (Loc.Offset & ~0xFFFFFFFFULL)) {
49 Loc.LocType = Location::ConstantIndex;
50 Loc.Offset = ConstPool.getConstantIndex(Loc.Offset);
53 CallsiteLocs.push_back(Loc);
54 MOI = ParseResult.second;
57 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
58 MCSymbolRefExpr::Create(MILabel, OutContext),
59 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
62 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, CallsiteLocs));
65 /// serializeToStackMapSection conceptually populates the following fields:
67 /// uint32 : Reserved (header)
68 /// uint32 : NumConstants
69 /// int64 : Constants[NumConstants]
70 /// uint32 : NumRecords
71 /// StkMapRecord[NumRecords] {
72 /// uint32 : PatchPoint ID
73 /// uint32 : Instruction Offset
74 /// uint16 : Reserved (record flags)
75 /// uint16 : NumLocations
76 /// Location[NumLocations] {
77 /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
78 /// uint8 : Reserved (location flags)
79 /// uint16 : Dwarf RegNum
84 /// Location Encoding, Type, Value:
85 /// 0x1, Register, Reg (value in register)
86 /// 0x2, Direct, Reg + Offset (frame index)
87 /// 0x3, Indirect, [Reg + Offset] (spilled value)
88 /// 0x4, Constant, Offset (small constant)
89 /// 0x5, ConstIndex, Constants[Offset] (large constant)
91 void StackMaps::serializeToStackMapSection() {
92 // Bail out if there's no stack map data.
96 MCContext &OutContext = AP.OutStreamer.getContext();
97 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
99 // Create the section.
100 const MCSection *StackMapSection =
101 OutContext.getObjectFileInfo()->getStackMapSection();
102 AP.OutStreamer.SwitchSection(StackMapSection);
104 // Emit a dummy symbol to force section inclusion.
105 AP.OutStreamer.EmitLabel(
106 OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
109 const char *WSMP = "Stack Maps: ";
111 const MCRegisterInfo &MCRI = *OutContext.getRegisterInfo();
113 DEBUG(dbgs() << "********** Stack Map Output **********\n");
116 AP.OutStreamer.EmitIntValue(0, 4);
119 AP.OutStreamer.EmitIntValue(ConstPool.getNumConstants(), 4);
121 // Constant pool entries.
122 for (unsigned i = 0; i < ConstPool.getNumConstants(); ++i)
123 AP.OutStreamer.EmitIntValue(ConstPool.getConstant(i), 8);
125 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << "\n");
126 AP.OutStreamer.EmitIntValue(CSInfos.size(), 4);
128 for (CallsiteInfoList::const_iterator CSII = CSInfos.begin(),
129 CSIE = CSInfos.end();
130 CSII != CSIE; ++CSII) {
132 unsigned CallsiteID = CSII->ID;
133 const LocationVec &CSLocs = CSII->Locations;
135 DEBUG(dbgs() << WSMP << "callsite " << CallsiteID << "\n");
137 // Verify stack map entry. It's better to communicate a problem to the
138 // runtime than crash in case of in-process compilation. Currently, we do
139 // simple overflow checks, but we may eventually communicate other
140 // compilation errors this way.
141 if (CSLocs.size() > UINT16_MAX) {
142 AP.OutStreamer.EmitIntValue(UINT32_MAX, 4); // Invalid ID.
143 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
144 AP.OutStreamer.EmitIntValue(0, 2); // Reserved.
145 AP.OutStreamer.EmitIntValue(0, 2); // 0 locations.
149 AP.OutStreamer.EmitIntValue(CallsiteID, 4);
150 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
152 // Reserved for flags.
153 AP.OutStreamer.EmitIntValue(0, 2);
155 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
157 AP.OutStreamer.EmitIntValue(CSLocs.size(), 2);
159 unsigned operIdx = 0;
160 for (LocationVec::const_iterator LocI = CSLocs.begin(), LocE = CSLocs.end();
161 LocI != LocE; ++LocI, ++operIdx) {
162 const Location &Loc = *LocI;
164 dbgs() << WSMP << " Loc " << operIdx << ": ";
165 switch (Loc.LocType) {
166 case Location::Unprocessed:
167 dbgs() << "<Unprocessed operand>";
169 case Location::Register:
170 dbgs() << "Register " << MCRI.getName(Loc.Reg);
172 case Location::Direct:
173 dbgs() << "Direct " << MCRI.getName(Loc.Reg);
175 dbgs() << " + " << Loc.Offset;
177 case Location::Indirect:
178 dbgs() << "Indirect " << MCRI.getName(Loc.Reg)
179 << " + " << Loc.Offset;
181 case Location::Constant:
182 dbgs() << "Constant " << Loc.Offset;
184 case Location::ConstantIndex:
185 dbgs() << "Constant Index " << Loc.Offset;
193 RegNo = MCRI.getDwarfRegNum(Loc.Reg, false);
194 for (MCSuperRegIterator SR(Loc.Reg, TRI);
195 SR.isValid() && (int)RegNo < 0; ++SR) {
196 RegNo = TRI->getDwarfRegNum(*SR, false);
200 assert((Loc.LocType != Location::Register
201 && Loc.LocType != Location::Register) &&
202 "Missing location register");
204 AP.OutStreamer.EmitIntValue(Loc.LocType, 1);
205 AP.OutStreamer.EmitIntValue(0, 1); // Reserved location flags.
206 AP.OutStreamer.EmitIntValue(RegNo, 2);
207 AP.OutStreamer.EmitIntValue(Loc.Offset, 4);
211 AP.OutStreamer.AddBlankLine();