blob: ffe46288b9bef29aa4a4c6b612ff015bfbce57f5 [file] [log] [blame]
Andrew Trick3d74dea2013-10-31 22:11:56 +00001//===---------------------------- StackMaps.cpp ---------------------------===//
2//
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
10#define DEBUG_TYPE "stackmaps"
11
12#include "llvm/CodeGen/StackMaps.h"
13
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"
25
26#include <iterator>
27
28using namespace llvm;
29
30void StackMaps::recordStackMap(const MachineInstr &MI, uint32_t ID,
31 MachineInstr::const_mop_iterator MOI,
32 MachineInstr::const_mop_iterator MOE) {
33
34 MCContext &OutContext = AP.OutStreamer.getContext();
35 MCSymbol *MILabel = OutContext.CreateTempSymbol();
36 AP.OutStreamer.EmitLabel(MILabel);
37
38 LocationVec CallsiteLocs;
39
40 while (MOI != MOE) {
41 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
42 OpParser(MOI, MOE);
43
44 Location &Loc = ParseResult.first;
45
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);
50 }
51
52 CallsiteLocs.push_back(Loc);
53 MOI = ParseResult.second;
54 }
55
56 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
57 MCSymbolRefExpr::Create(MILabel, OutContext),
58 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
59 OutContext);
60
61 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, CallsiteLocs));
62}
63
64/// serializeToStackMapSection conceptually populates the following fields:
65///
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
79/// int32 : Offset
80/// }
81/// }
82///
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)
89///
90void StackMaps::serializeToStackMapSection() {
91 // Bail out if there's no stack map data.
92 if (CSInfos.empty())
93 return;
94
95 MCContext &OutContext = AP.OutStreamer.getContext();
96 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
97
98 // Create the section.
99 const MCSection *StackMapSection =
100 OutContext.getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0,
101 SectionKind::getMetadata());
102 AP.OutStreamer.SwitchSection(StackMapSection);
103
104 // Emit a dummy symbol to force section inclusion.
105 AP.OutStreamer.EmitLabel(
106 OutContext.GetOrCreateSymbol(Twine("__LLVM_StackMaps")));
107
108 // Serialize data.
109 const char *WSMP = "Stack Maps: ";
110 const MCRegisterInfo &MCRI = *OutContext.getRegisterInfo();
111
112 DEBUG(dbgs() << "********** Stack Map Output **********\n");
113
114 // Header.
115 AP.OutStreamer.EmitIntValue(0, 4);
116
117 // Num constants.
118 AP.OutStreamer.EmitIntValue(ConstPool.getNumConstants(), 4);
119
120 // Constant pool entries.
121 for (unsigned i = 0; i < ConstPool.getNumConstants(); ++i)
122 AP.OutStreamer.EmitIntValue(ConstPool.getConstant(i), 8);
123
124 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << "\n");
125 AP.OutStreamer.EmitIntValue(CSInfos.size(), 4);
126
127 for (CallsiteInfoList::const_iterator CSII = CSInfos.begin(),
128 CSIE = CSInfos.end();
129 CSII != CSIE; ++CSII) {
130
131 unsigned CallsiteID = CSII->ID;
132 const LocationVec &CSLocs = CSII->Locations;
133
134 DEBUG(dbgs() << WSMP << "callsite " << CallsiteID << "\n");
135
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.
145 continue;
146 }
147
148 AP.OutStreamer.EmitIntValue(CallsiteID, 4);
149 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
150
151 // Reserved for flags.
152 AP.OutStreamer.EmitIntValue(0, 2);
153
154 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
155
156 AP.OutStreamer.EmitIntValue(CSLocs.size(), 2);
157
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;
162 DEBUG(
163 dbgs() << WSMP << " Loc " << operIdx << ": ";
164 switch (Loc.LocType) {
165 case Location::Unprocessed:
166 dbgs() << "<Unprocessed operand>";
167 break;
168 case Location::Register:
169 dbgs() << "Register " << MCRI.getName(Loc.Reg);
170 break;
171 case Location::Direct:
172 dbgs() << "Direct " << MCRI.getName(Loc.Reg);
173 if (Loc.Offset)
174 dbgs() << " + " << Loc.Offset;
175 break;
176 case Location::Indirect:
177 dbgs() << "Indirect " << MCRI.getName(Loc.Reg)
178 << " + " << Loc.Offset;
179 break;
180 case Location::Constant:
181 dbgs() << "Constant " << Loc.Offset;
182 break;
183 case Location::ConstantIndex:
184 dbgs() << "Constant Index " << Loc.Offset;
185 break;
186 }
187 dbgs() << "\n";
188 );
189
190 unsigned RegNo = 0;
191 if(Loc.Reg) {
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);
196 }
197 }
198 else {
199 assert((Loc.LocType != Location::Register
200 && Loc.LocType != Location::Register) &&
201 "Missing location register");
202 }
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);
207 }
208 }
209
210 AP.OutStreamer.AddBlankLine();
211
212 CSInfos.clear();
213}