| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CODEGEN_STACKMAPS_H |
| 10 |
#define LLVM_CODEGEN_STACKMAPS_H |
| 11 |
|
| 12 |
#include "llvm/ADT/MapVector.h" |
| 13 |
#include "llvm/ADT/SmallVector.h" |
| 14 |
#include "llvm/CodeGen/MachineInstr.h" |
| 15 |
#include "llvm/IR/CallingConv.h" |
| 16 |
#include "llvm/MC/MCSymbol.h" |
| 17 |
#include "llvm/Support/Debug.h" |
| 18 |
#include <algorithm> |
| 19 |
#include <cassert> |
| 20 |
#include <cstdint> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
namespace llvm { |
| 24 |
|
| 25 |
class AsmPrinter; |
| 26 |
class MCExpr; |
| 27 |
class MCStreamer; |
| 28 |
class raw_ostream; |
| 29 |
class TargetRegisterInfo; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class StackMapOpers { |
| 36 |
public: |
| 37 |
|
| 38 |
enum { IDPos, NBytesPos }; |
| 39 |
|
| 40 |
private: |
| 41 |
const MachineInstr* MI; |
| 42 |
|
| 43 |
public: |
| 44 |
explicit StackMapOpers(const MachineInstr *MI); |
| 45 |
|
| 46 |
|
| 47 |
uint64_t getID() const { return MI->getOperand(IDPos).getImm(); } |
| 48 |
|
| 49 |
|
| 50 |
uint32_t getNumPatchBytes() const { |
| 51 |
return MI->getOperand(NBytesPos).getImm(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
unsigned getVarIdx() const { |
| 57 |
|
| 58 |
return 2; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
class PatchPointOpers { |
| 77 |
public: |
| 78 |
|
| 79 |
enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd }; |
| 80 |
|
| 81 |
private: |
| 82 |
const MachineInstr *MI; |
| 83 |
bool HasDef; |
| 84 |
|
| 85 |
unsigned getMetaIdx(unsigned Pos = 0) const { |
| 86 |
assert(Pos < MetaEnd && "Meta operand index out of range."); |
| 87 |
return (HasDef ? 1 : 0) + Pos; |
| 88 |
} |
| 89 |
|
| 90 |
const MachineOperand &getMetaOper(unsigned Pos) const { |
| 91 |
return MI->getOperand(getMetaIdx(Pos)); |
| 92 |
} |
| 93 |
|
| 94 |
public: |
| 95 |
explicit PatchPointOpers(const MachineInstr *MI); |
| 96 |
|
| 97 |
bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); } |
| 98 |
bool hasDef() const { return HasDef; } |
| 99 |
|
| 100 |
|
| 101 |
uint64_t getID() const { return getMetaOper(IDPos).getImm(); } |
| 102 |
|
| 103 |
|
| 104 |
uint32_t getNumPatchBytes() const { |
| 105 |
return getMetaOper(NBytesPos).getImm(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
const MachineOperand &getCallTarget() const { |
| 110 |
return getMetaOper(TargetPos); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
CallingConv::ID getCallingConv() const { |
| 115 |
return getMetaOper(CCPos).getImm(); |
| 116 |
} |
| 117 |
|
| 118 |
unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; } |
| 119 |
|
| 120 |
|
| 121 |
uint32_t getNumCallArgs() const { |
| 122 |
return MI->getOperand(getMetaIdx(NArgPos)).getImm(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
unsigned getVarIdx() const { |
| 128 |
return getMetaIdx() + MetaEnd + getNumCallArgs(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
unsigned getStackMapStartIdx() const { |
| 134 |
if (isAnyReg()) |
| 135 |
return getArgIdx(); |
| 136 |
return getVarIdx(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
unsigned getNextScratchIdx(unsigned StartIdx = 0) const; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
class StatepointOpers { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd }; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
enum { CCOffset = 1, FlagsOffset = 3, NumDeoptOperandsOffset = 5 }; |
| 171 |
|
| 172 |
public: |
| 173 |
explicit StatepointOpers(const MachineInstr *MI) : MI(MI) { |
| 174 |
NumDefs = MI->getNumDefs(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
unsigned getIDPos() const { return NumDefs + IDPos; } |
| 179 |
|
| 180 |
|
| 181 |
unsigned getNBytesPos() const { return NumDefs + NBytesPos; } |
| 182 |
|
| 183 |
|
| 184 |
unsigned getNCallArgsPos() const { return NumDefs + NCallArgsPos; } |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
unsigned getVarIdx() const { |
| 189 |
return MI->getOperand(NumDefs + NCallArgsPos).getImm() + MetaEnd + NumDefs; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
unsigned getCCIdx() const { return getVarIdx() + CCOffset; } |
| 194 |
|
| 195 |
|
| 196 |
unsigned getFlagsIdx() const { return getVarIdx() + FlagsOffset; } |
| 197 |
|
| 198 |
|
| 199 |
unsigned getNumDeoptArgsIdx() const { |
| 200 |
return getVarIdx() + NumDeoptOperandsOffset; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
uint64_t getID() const { return MI->getOperand(NumDefs + IDPos).getImm(); } |
| 205 |
|
| 206 |
|
| 207 |
uint32_t getNumPatchBytes() const { |
| 208 |
return MI->getOperand(NumDefs + NBytesPos).getImm(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
const MachineOperand &getCallTarget() const { |
| 213 |
return MI->getOperand(NumDefs + CallTargetPos); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
CallingConv::ID getCallingConv() const { |
| 218 |
return MI->getOperand(getCCIdx()).getImm(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
uint64_t getFlags() const { return MI->getOperand(getFlagsIdx()).getImm(); } |
| 223 |
|
| 224 |
uint64_t getNumDeoptArgs() const { |
| 225 |
return MI->getOperand(getNumDeoptArgsIdx()).getImm(); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
unsigned getNumGcMapEntriesIdx(); |
| 230 |
|
| 231 |
|
| 232 |
unsigned getNumAllocaIdx(); |
| 233 |
|
| 234 |
|
| 235 |
unsigned getNumGCPtrIdx(); |
| 236 |
|
| 237 |
|
| 238 |
int getFirstGCPtrIdx(); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
unsigned |
| 244 |
getGCPointerMap(SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap); |
| 245 |
|
| 246 |
private: |
| 247 |
const MachineInstr *MI; |
| 248 |
unsigned NumDefs; |
| 249 |
}; |
| 250 |
|
| 251 |
class StackMaps { |
| 252 |
public: |
| 253 |
struct Location { |
| 254 |
enum LocationType { |
| 255 |
Unprocessed, |
| 256 |
Register, |
| 257 |
Direct, |
| 258 |
Indirect, |
| 259 |
Constant, |
| 260 |
ConstantIndex |
| 261 |
}; |
| 262 |
LocationType Type = Unprocessed; |
| 263 |
unsigned Size = 0; |
| 264 |
unsigned Reg = 0; |
| 265 |
int64_t Offset = 0; |
| 266 |
|
| 267 |
Location() = default; |
| 268 |
Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset) |
| 269 |
: Type(Type), Size(Size), Reg(Reg), Offset(Offset) {} |
| 270 |
}; |
| 271 |
|
| 272 |
struct LiveOutReg { |
| 273 |
unsigned short Reg = 0; |
| 274 |
unsigned short DwarfRegNum = 0; |
| 275 |
unsigned short Size = 0; |
| 276 |
|
| 277 |
LiveOutReg() = default; |
| 278 |
LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum, |
| 279 |
unsigned short Size) |
| 280 |
: Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {} |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
using OpType = enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp }; |
| 287 |
|
| 288 |
StackMaps(AsmPrinter &AP); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
static unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx); |
| 293 |
|
| 294 |
void reset() { |
| 295 |
CSInfos.clear(); |
| 296 |
ConstPool.clear(); |
| 297 |
FnInfos.clear(); |
| 298 |
} |
| 299 |
|
| 300 |
using LocationVec = SmallVector<Location, 8>; |
| 301 |
using LiveOutVec = SmallVector<LiveOutReg, 8>; |
| 302 |
using ConstantPool = MapVector<uint64_t, uint64_t>; |
| 303 |
|
| 304 |
struct FunctionInfo { |
| 305 |
uint64_t StackSize = 0; |
| 306 |
uint64_t RecordCount = 1; |
| 307 |
|
| 308 |
FunctionInfo() = default; |
| 309 |
explicit FunctionInfo(uint64_t StackSize) : StackSize(StackSize) {} |
| 310 |
}; |
| 311 |
|
| 312 |
struct CallsiteInfo { |
| 313 |
const MCExpr *CSOffsetExpr = nullptr; |
| 314 |
uint64_t ID = 0; |
| 315 |
LocationVec Locations; |
| 316 |
LiveOutVec LiveOuts; |
| 317 |
|
| 318 |
CallsiteInfo() = default; |
| 319 |
CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID, |
| 320 |
LocationVec &&Locations, LiveOutVec &&LiveOuts) |
| 321 |
: CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)), |
| 322 |
LiveOuts(std::move(LiveOuts)) {} |
| 323 |
}; |
| 324 |
|
| 325 |
using FnInfoMap = MapVector<const MCSymbol *, FunctionInfo>; |
| 326 |
using CallsiteInfoList = std::vector<CallsiteInfo>; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
void recordStackMap(const MCSymbol &L, |
| 332 |
const MachineInstr &MI); |
| 333 |
|
| 334 |
|
| 335 |
void recordPatchPoint(const MCSymbol &L, |
| 336 |
const MachineInstr &MI); |
| 337 |
|
| 338 |
|
| 339 |
void recordStatepoint(const MCSymbol &L, |
| 340 |
const MachineInstr &MI); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
void serializeToStackMapSection(); |
| 346 |
|
| 347 |
|
| 348 |
CallsiteInfoList &getCSInfos() { return CSInfos; } |
| 349 |
|
| 350 |
|
| 351 |
FnInfoMap &getFnInfos() { return FnInfos; } |
| 352 |
|
| 353 |
private: |
| 354 |
static const char *WSMP; |
| 355 |
|
| 356 |
AsmPrinter &AP; |
| 357 |
CallsiteInfoList CSInfos; |
| 358 |
ConstantPool ConstPool; |
| 359 |
FnInfoMap FnInfos; |
| 360 |
|
| 361 |
MachineInstr::const_mop_iterator |
| 362 |
parseOperand(MachineInstr::const_mop_iterator MOI, |
| 363 |
MachineInstr::const_mop_iterator MOE, LocationVec &Locs, |
| 364 |
LiveOutVec &LiveOuts) const; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
void parseStatepointOpers(const MachineInstr &MI, |
| 369 |
MachineInstr::const_mop_iterator MOI, |
| 370 |
MachineInstr::const_mop_iterator MOE, |
| 371 |
LocationVec &Locations, LiveOutVec &LiveOuts); |
| 372 |
|
| 373 |
|
| 374 |
LiveOutReg createLiveOutReg(unsigned Reg, |
| 375 |
const TargetRegisterInfo *TRI) const; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
void recordStackMapOpers(const MCSymbol &L, |
| 389 |
const MachineInstr &MI, uint64_t ID, |
| 390 |
MachineInstr::const_mop_iterator MOI, |
| 391 |
MachineInstr::const_mop_iterator MOE, |
| 392 |
bool recordResult = false); |
| 393 |
|
| 394 |
|
| 395 |
void emitStackmapHeader(MCStreamer &OS); |
| 396 |
|
| 397 |
|
| 398 |
void emitFunctionFrameRecords(MCStreamer &OS); |
| 399 |
|
| 400 |
|
| 401 |
void emitConstantPoolEntries(MCStreamer &OS); |
| 402 |
|
| 403 |
|
| 404 |
void emitCallsiteEntries(MCStreamer &OS); |
| 405 |
|
| 406 |
void print(raw_ostream &OS); |
| 407 |
void debug() { print(dbgs()); } |
| 408 |
}; |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
#endif |