Andrew Trick | 3d74dea | 2013-10-31 22:11:56 +0000 | [diff] [blame] | 1 | //===---------------------------- 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 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | void 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 | /// |
| 90 | void 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: "; |
Andrew Trick | 21cd360 | 2013-10-31 22:42:20 +0000 | [diff] [blame^] | 110 | (void)WSMP; |
Andrew Trick | 3d74dea | 2013-10-31 22:11:56 +0000 | [diff] [blame] | 111 | 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 | } |