1 //===- StackMaps.h - StackMaps ----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 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/Support/Compiler.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 MCSymbol; 27 class MCExpr; 28 class MCStreamer; 29 class raw_ostream; 30 class TargetRegisterInfo; 31 32 /// MI-level stackmap operands. 33 /// 34 /// MI stackmap operations take the form: 35 /// <id>, <numBytes>, live args... 36 class StackMapOpers { 37 public: 38 /// Enumerate the meta operands. 39 enum { IDPos, NBytesPos }; 40 41 private: 42 const MachineInstr* MI; 43 44 public: 45 LLVM_ABI explicit StackMapOpers(const MachineInstr *MI); 46 47 /// Return the ID for the given stackmap 48 uint64_t getID() const { return MI->getOperand(IDPos).getImm(); } 49 50 /// Return the number of patchable bytes the given stackmap should emit. 51 uint32_t getNumPatchBytes() const { 52 return MI->getOperand(NBytesPos).getImm(); 53 } 54 55 /// Get the operand index of the variable list of non-argument operands. 56 /// These hold the "live state". 57 unsigned getVarIdx() const { 58 // Skip ID, nShadowBytes. 59 return 2; 60 } 61 }; 62 63 /// MI-level patchpoint operands. 64 /// 65 /// MI patchpoint operations take the form: 66 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ... 67 /// 68 /// IR patchpoint intrinsics do not have the <cc> operand because calling 69 /// convention is part of the subclass data. 70 /// 71 /// SD patchpoint nodes do not have a def operand because it is part of the 72 /// SDValue. 73 /// 74 /// Patchpoints following the anyregcc convention are handled specially. For 75 /// these, the stack map also records the location of the return value and 76 /// arguments. 77 class PatchPointOpers { 78 public: 79 /// Enumerate the meta operands. 80 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd }; 81 82 private: 83 const MachineInstr *MI; 84 bool HasDef; 85 86 unsigned getMetaIdx(unsigned Pos = 0) const { 87 assert(Pos < MetaEnd && "Meta operand index out of range."); 88 return (HasDef ? 1 : 0) + Pos; 89 } 90 91 const MachineOperand &getMetaOper(unsigned Pos) const { 92 return MI->getOperand(getMetaIdx(Pos)); 93 } 94 95 public: 96 LLVM_ABI explicit PatchPointOpers(const MachineInstr *MI); 97 98 bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); } 99 bool hasDef() const { return HasDef; } 100 101 /// Return the ID for the given patchpoint. 102 uint64_t getID() const { return getMetaOper(IDPos).getImm(); } 103 104 /// Return the number of patchable bytes the given patchpoint should emit. 105 uint32_t getNumPatchBytes() const { 106 return getMetaOper(NBytesPos).getImm(); 107 } 108 109 /// Returns the target of the underlying call. 110 const MachineOperand &getCallTarget() const { 111 return getMetaOper(TargetPos); 112 } 113 114 /// Returns the calling convention 115 CallingConv::ID getCallingConv() const { 116 return getMetaOper(CCPos).getImm(); 117 } 118 119 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; } 120 121 /// Return the number of call arguments 122 uint32_t getNumCallArgs() const { 123 return MI->getOperand(getMetaIdx(NArgPos)).getImm(); 124 } 125 126 /// Get the operand index of the variable list of non-argument operands. 127 /// These hold the "live state". 128 unsigned getVarIdx() const { 129 return getMetaIdx() + MetaEnd + getNumCallArgs(); 130 } 131 132 /// Get the index at which stack map locations will be recorded. 133 /// Arguments are not recorded unless the anyregcc convention is used. 134 unsigned getStackMapStartIdx() const { 135 if (isAnyReg()) 136 return getArgIdx(); 137 return getVarIdx(); 138 } 139 140 /// Get the next scratch register operand index. 141 LLVM_ABI unsigned getNextScratchIdx(unsigned StartIdx = 0) const; 142 }; 143 144 /// MI-level Statepoint operands 145 /// 146 /// Statepoint operands take the form: 147 /// <id>, <num patch bytes >, <num call arguments>, <call target>, 148 /// [call arguments...], 149 /// <StackMaps::ConstantOp>, <calling convention>, 150 /// <StackMaps::ConstantOp>, <statepoint flags>, 151 /// <StackMaps::ConstantOp>, <num deopt args>, [deopt args...], 152 /// <StackMaps::ConstantOp>, <num gc pointer args>, [gc pointer args...], 153 /// <StackMaps::ConstantOp>, <num gc allocas>, [gc allocas args...], 154 /// <StackMaps::ConstantOp>, <num entries in gc map>, [base/derived pairs] 155 /// base/derived pairs in gc map are logical indices into <gc pointer args> 156 /// section. 157 /// All gc pointers assigned to VRegs produce new value (in form of MI Def 158 /// operand) and are tied to it. 159 class StatepointOpers { 160 // TODO:: we should change the STATEPOINT representation so that CC and 161 // Flags should be part of meta operands, with args and deopt operands, and 162 // gc operands all prefixed by their length and a type code. This would be 163 // much more consistent. 164 165 // These values are absolute offsets into the operands of the statepoint 166 // instruction. 167 enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd }; 168 169 // These values are relative offsets from the start of the statepoint meta 170 // arguments (i.e. the end of the call arguments). 171 enum { CCOffset = 1, FlagsOffset = 3, NumDeoptOperandsOffset = 5 }; 172 173 public: 174 explicit StatepointOpers(const MachineInstr *MI) : MI(MI) { 175 NumDefs = MI->getNumDefs(); 176 } 177 178 /// Get index of statepoint ID operand. 179 unsigned getIDPos() const { return NumDefs + IDPos; } 180 181 /// Get index of Num Patch Bytes operand. 182 unsigned getNBytesPos() const { return NumDefs + NBytesPos; } 183 184 /// Get index of Num Call Arguments operand. 185 unsigned getNCallArgsPos() const { return NumDefs + NCallArgsPos; } 186 187 /// Get starting index of non call related arguments 188 /// (calling convention, statepoint flags, vm state and gc state). 189 unsigned getVarIdx() const { 190 return MI->getOperand(NumDefs + NCallArgsPos).getImm() + MetaEnd + NumDefs; 191 } 192 193 /// Get index of Calling Convention operand. 194 unsigned getCCIdx() const { return getVarIdx() + CCOffset; } 195 196 /// Get index of Flags operand. 197 unsigned getFlagsIdx() const { return getVarIdx() + FlagsOffset; } 198 199 /// Get index of Number Deopt Arguments operand. 200 unsigned getNumDeoptArgsIdx() const { 201 return getVarIdx() + NumDeoptOperandsOffset; 202 } 203 204 /// Return the ID for the given statepoint. 205 uint64_t getID() const { return MI->getOperand(NumDefs + IDPos).getImm(); } 206 207 /// Return the number of patchable bytes the given statepoint should emit. 208 uint32_t getNumPatchBytes() const { 209 return MI->getOperand(NumDefs + NBytesPos).getImm(); 210 } 211 212 /// Return the target of the underlying call. 213 const MachineOperand &getCallTarget() const { 214 return MI->getOperand(NumDefs + CallTargetPos); 215 } 216 217 /// Return the calling convention. 218 CallingConv::ID getCallingConv() const { 219 return MI->getOperand(getCCIdx()).getImm(); 220 } 221 222 /// Return the statepoint flags. 223 uint64_t getFlags() const { return MI->getOperand(getFlagsIdx()).getImm(); } 224 225 uint64_t getNumDeoptArgs() const { 226 return MI->getOperand(getNumDeoptArgsIdx()).getImm(); 227 } 228 229 /// Get index of number of gc map entries. 230 LLVM_ABI unsigned getNumGcMapEntriesIdx(); 231 232 /// Get index of number of gc allocas. 233 LLVM_ABI unsigned getNumAllocaIdx(); 234 235 /// Get index of number of GC pointers. 236 LLVM_ABI unsigned getNumGCPtrIdx(); 237 238 /// Get index of first GC pointer operand of -1 if there are none. 239 LLVM_ABI int getFirstGCPtrIdx(); 240 241 /// Get vector of base/derived pairs from statepoint. 242 /// Elements are indices into GC Pointer operand list (logical). 243 /// Returns number of elements in GCMap. 244 LLVM_ABI unsigned 245 getGCPointerMap(SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap); 246 247 /// Return true if Reg is used only in operands which can be folded to 248 /// stack usage. 249 LLVM_ABI bool isFoldableReg(Register Reg) const; 250 251 /// Return true if Reg is used only in operands of MI which can be folded to 252 /// stack usage and MI is a statepoint instruction. 253 LLVM_ABI static bool isFoldableReg(const MachineInstr *MI, Register Reg); 254 255 private: 256 const MachineInstr *MI; 257 unsigned NumDefs; 258 }; 259 260 class StackMaps { 261 public: 262 struct Location { 263 enum LocationType : uint16_t { 264 Unprocessed, 265 Register, 266 Direct, 267 Indirect, 268 Constant, 269 ConstantIndex 270 }; 271 LocationType Type = Unprocessed; 272 uint16_t Size = 0; 273 uint16_t Reg = 0; 274 int32_t Offset = 0; 275 276 Location() = default; 277 Location(LocationType Type, uint16_t Size, uint16_t Reg, int32_t Offset) 278 : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {} 279 }; 280 281 struct LiveOutReg { 282 uint16_t Reg = 0; 283 uint16_t DwarfRegNum = 0; 284 uint16_t Size = 0; 285 286 LiveOutReg() = default; 287 LiveOutReg(uint16_t Reg, uint16_t DwarfRegNum, uint16_t Size) 288 : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {} 289 }; 290 291 // OpTypes are used to encode information about the following logical 292 // operand (which may consist of several MachineOperands) for the 293 // OpParser. 294 using OpType = enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp }; 295 296 LLVM_ABI StackMaps(AsmPrinter &AP); 297 298 /// Get index of next meta operand. 299 /// Similar to parseOperand, but does not actually parses operand meaning. 300 LLVM_ABI static unsigned getNextMetaArgIdx(const MachineInstr *MI, 301 unsigned CurIdx); 302 303 void reset() { 304 CSInfos.clear(); 305 ConstPool.clear(); 306 FnInfos.clear(); 307 } 308 309 using LocationVec = SmallVector<Location, 8>; 310 using LiveOutVec = SmallVector<LiveOutReg, 8>; 311 using ConstantPool = MapVector<uint64_t, uint64_t>; 312 313 struct FunctionInfo { 314 uint64_t StackSize = 0; 315 uint64_t RecordCount = 1; 316 317 FunctionInfo() = default; 318 explicit FunctionInfo(uint64_t StackSize) : StackSize(StackSize) {} 319 }; 320 321 struct CallsiteInfo { 322 const MCExpr *CSOffsetExpr = nullptr; 323 uint64_t ID = 0; 324 LocationVec Locations; 325 LiveOutVec LiveOuts; 326 327 CallsiteInfo() = default; 328 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID, 329 LocationVec &&Locations, LiveOutVec &&LiveOuts) 330 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)), 331 LiveOuts(std::move(LiveOuts)) {} 332 }; 333 334 using FnInfoMap = MapVector<const MCSymbol *, FunctionInfo>; 335 using CallsiteInfoList = std::vector<CallsiteInfo>; 336 337 /// Generate a stackmap record for a stackmap instruction. 338 /// 339 /// MI must be a raw STACKMAP, not a PATCHPOINT. 340 LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI); 341 342 /// Generate a stackmap record for a patchpoint instruction. 343 LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI); 344 345 /// Generate a stackmap record for a statepoint instruction. 346 LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI); 347 348 /// If there is any stack map data, create a stack map section and serialize 349 /// the map info into it. This clears the stack map data structures 350 /// afterwards. 351 LLVM_ABI void serializeToStackMapSection(); 352 353 /// Get call site info. 354 CallsiteInfoList &getCSInfos() { return CSInfos; } 355 356 /// Get function info. 357 FnInfoMap &getFnInfos() { return FnInfos; } 358 359 private: 360 static const char *WSMP; 361 362 AsmPrinter &AP; 363 CallsiteInfoList CSInfos; 364 ConstantPool ConstPool; 365 FnInfoMap FnInfos; 366 367 MachineInstr::const_mop_iterator 368 parseOperand(MachineInstr::const_mop_iterator MOI, 369 MachineInstr::const_mop_iterator MOE, LocationVec &Locs, 370 LiveOutVec &LiveOuts); 371 372 /// Specialized parser of statepoint operands. 373 /// They do not directly correspond to StackMap record entries. 374 void parseStatepointOpers(const MachineInstr &MI, 375 MachineInstr::const_mop_iterator MOI, 376 MachineInstr::const_mop_iterator MOE, 377 LocationVec &Locations, LiveOutVec &LiveOuts); 378 379 /// Create a live-out register record for the given register @p Reg. 380 LiveOutReg createLiveOutReg(unsigned Reg, 381 const TargetRegisterInfo *TRI) const; 382 383 /// Parse the register live-out mask and return a vector of live-out 384 /// registers that need to be recorded in the stackmap. 385 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const; 386 387 /// Record the locations of the operands of the provided instruction in a 388 /// record keyed by the provided label. For instructions w/AnyReg calling 389 /// convention the return register is also recorded if requested. For 390 /// STACKMAP, and PATCHPOINT the label is expected to immediately *preceed* 391 /// lowering of the MI to MCInsts. For STATEPOINT, it expected to 392 /// immediately *follow*. It's not clear this difference was intentional, 393 /// but it exists today. 394 void recordStackMapOpers(const MCSymbol &L, 395 const MachineInstr &MI, uint64_t ID, 396 MachineInstr::const_mop_iterator MOI, 397 MachineInstr::const_mop_iterator MOE, 398 bool recordResult = false); 399 400 /// Emit the stackmap header. 401 void emitStackmapHeader(MCStreamer &OS); 402 403 /// Emit the function frame record for each function. 404 void emitFunctionFrameRecords(MCStreamer &OS); 405 406 /// Emit the constant pool. 407 void emitConstantPoolEntries(MCStreamer &OS); 408 409 /// Emit the callsite info for each stackmap/patchpoint intrinsic call. 410 void emitCallsiteEntries(MCStreamer &OS); 411 412 LLVM_ABI void print(raw_ostream &OS); 413 void debug() { print(dbgs()); } 414 }; 415 416 } // end namespace llvm 417 418 #endif // LLVM_CODEGEN_STACKMAPS_H 419