blob: 51580b479d845ac325807e83d3f444d05d0e4ab3 [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"
Lang Hamesde753f42013-11-08 22:30:52 +000018#include "llvm/MC/MCObjectFileInfo.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000019#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"
26
27#include <iterator>
28
29using namespace llvm;
30
31void StackMaps::recordStackMap(const MachineInstr &MI, uint32_t ID,
32 MachineInstr::const_mop_iterator MOI,
33 MachineInstr::const_mop_iterator MOE) {
34
35 MCContext &OutContext = AP.OutStreamer.getContext();
36 MCSymbol *MILabel = OutContext.CreateTempSymbol();
37 AP.OutStreamer.EmitLabel(MILabel);
38
39 LocationVec CallsiteLocs;
40
41 while (MOI != MOE) {
42 std::pair<Location, MachineInstr::const_mop_iterator> ParseResult =
43 OpParser(MOI, MOE);
44
45 Location &Loc = ParseResult.first;
46
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);
51 }
52
53 CallsiteLocs.push_back(Loc);
54 MOI = ParseResult.second;
55 }
56
57 const MCExpr *CSOffsetExpr = MCBinaryExpr::CreateSub(
58 MCSymbolRefExpr::Create(MILabel, OutContext),
59 MCSymbolRefExpr::Create(AP.CurrentFnSym, OutContext),
60 OutContext);
61
62 CSInfos.push_back(CallsiteInfo(CSOffsetExpr, ID, CallsiteLocs));
63}
64
65/// serializeToStackMapSection conceptually populates the following fields:
66///
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
80/// int32 : Offset
81/// }
82/// }
83///
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)
90///
91void StackMaps::serializeToStackMapSection() {
92 // Bail out if there's no stack map data.
93 if (CSInfos.empty())
94 return;
95
96 MCContext &OutContext = AP.OutStreamer.getContext();
97 const TargetRegisterInfo *TRI = AP.TM.getRegisterInfo();
98
99 // Create the section.
100 const MCSection *StackMapSection =
Lang Hamesde753f42013-11-08 22:30:52 +0000101 OutContext.getObjectFileInfo()->getStackMapSection();
Andrew Trick3d74dea2013-10-31 22:11:56 +0000102 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: ";
Andrew Trick21cd3602013-10-31 22:42:20 +0000110 (void)WSMP;
Andrew Trick3d74dea2013-10-31 22:11:56 +0000111 const MCRegisterInfo &MCRI = *OutContext.getRegisterInfo();
112
113 DEBUG(dbgs() << "********** Stack Map Output **********\n");
114
115 // Header.
116 AP.OutStreamer.EmitIntValue(0, 4);
117
118 // Num constants.
119 AP.OutStreamer.EmitIntValue(ConstPool.getNumConstants(), 4);
120
121 // Constant pool entries.
122 for (unsigned i = 0; i < ConstPool.getNumConstants(); ++i)
123 AP.OutStreamer.EmitIntValue(ConstPool.getConstant(i), 8);
124
125 DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << "\n");
126 AP.OutStreamer.EmitIntValue(CSInfos.size(), 4);
127
128 for (CallsiteInfoList::const_iterator CSII = CSInfos.begin(),
129 CSIE = CSInfos.end();
130 CSII != CSIE; ++CSII) {
131
132 unsigned CallsiteID = CSII->ID;
133 const LocationVec &CSLocs = CSII->Locations;
134
135 DEBUG(dbgs() << WSMP << "callsite " << CallsiteID << "\n");
136
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.
146 continue;
147 }
148
149 AP.OutStreamer.EmitIntValue(CallsiteID, 4);
150 AP.OutStreamer.EmitValue(CSII->CSOffsetExpr, 4);
151
152 // Reserved for flags.
153 AP.OutStreamer.EmitIntValue(0, 2);
154
155 DEBUG(dbgs() << WSMP << " has " << CSLocs.size() << " locations\n");
156
157 AP.OutStreamer.EmitIntValue(CSLocs.size(), 2);
158
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;
163 DEBUG(
164 dbgs() << WSMP << " Loc " << operIdx << ": ";
165 switch (Loc.LocType) {
166 case Location::Unprocessed:
167 dbgs() << "<Unprocessed operand>";
168 break;
169 case Location::Register:
170 dbgs() << "Register " << MCRI.getName(Loc.Reg);
171 break;
172 case Location::Direct:
173 dbgs() << "Direct " << MCRI.getName(Loc.Reg);
174 if (Loc.Offset)
175 dbgs() << " + " << Loc.Offset;
176 break;
177 case Location::Indirect:
178 dbgs() << "Indirect " << MCRI.getName(Loc.Reg)
179 << " + " << Loc.Offset;
180 break;
181 case Location::Constant:
182 dbgs() << "Constant " << Loc.Offset;
183 break;
184 case Location::ConstantIndex:
185 dbgs() << "Constant Index " << Loc.Offset;
186 break;
187 }
188 dbgs() << "\n";
189 );
190
191 unsigned RegNo = 0;
192 if(Loc.Reg) {
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);
197 }
198 }
199 else {
200 assert((Loc.LocType != Location::Register
201 && Loc.LocType != Location::Register) &&
202 "Missing location register");
203 }
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);
208 }
209 }
210
211 AP.OutStreamer.AddBlankLine();
212
213 CSInfos.clear();
214}