LLVM API Documentation
00001 //===------------------- StackMaps.h - StackMaps ----------------*- C++ -*-===// 00002 00003 // 00004 // The LLVM Compiler Infrastructure 00005 // 00006 // This file is distributed under the University of Illinois Open Source 00007 // License. See LICENSE.TXT for details. 00008 // 00009 //===----------------------------------------------------------------------===// 00010 00011 #ifndef LLVM_CODEGEN_STACKMAPS_H 00012 #define LLVM_CODEGEN_STACKMAPS_H 00013 00014 #include "llvm/ADT/MapVector.h" 00015 #include "llvm/ADT/SmallVector.h" 00016 #include "llvm/CodeGen/MachineInstr.h" 00017 #include <map> 00018 #include <vector> 00019 00020 namespace llvm { 00021 00022 class AsmPrinter; 00023 class MCExpr; 00024 class MCStreamer; 00025 00026 /// \brief MI-level patchpoint operands. 00027 /// 00028 /// MI patchpoint operations take the form: 00029 /// [<def>], <id>, <numBytes>, <target>, <numArgs>, <cc>, ... 00030 /// 00031 /// IR patchpoint intrinsics do not have the <cc> operand because calling 00032 /// convention is part of the subclass data. 00033 /// 00034 /// SD patchpoint nodes do not have a def operand because it is part of the 00035 /// SDValue. 00036 /// 00037 /// Patchpoints following the anyregcc convention are handled specially. For 00038 /// these, the stack map also records the location of the return value and 00039 /// arguments. 00040 class PatchPointOpers { 00041 public: 00042 /// Enumerate the meta operands. 00043 enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd }; 00044 private: 00045 const MachineInstr *MI; 00046 bool HasDef; 00047 bool IsAnyReg; 00048 public: 00049 explicit PatchPointOpers(const MachineInstr *MI); 00050 00051 bool isAnyReg() const { return IsAnyReg; } 00052 bool hasDef() const { return HasDef; } 00053 00054 unsigned getMetaIdx(unsigned Pos = 0) const { 00055 assert(Pos < MetaEnd && "Meta operand index out of range."); 00056 return (HasDef ? 1 : 0) + Pos; 00057 } 00058 00059 const MachineOperand &getMetaOper(unsigned Pos) { 00060 return MI->getOperand(getMetaIdx(Pos)); 00061 } 00062 00063 unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; } 00064 00065 /// Get the operand index of the variable list of non-argument operands. 00066 /// These hold the "live state". 00067 unsigned getVarIdx() const { 00068 return getMetaIdx() + MetaEnd 00069 + MI->getOperand(getMetaIdx(NArgPos)).getImm(); 00070 } 00071 00072 /// Get the index at which stack map locations will be recorded. 00073 /// Arguments are not recorded unless the anyregcc convention is used. 00074 unsigned getStackMapStartIdx() const { 00075 if (IsAnyReg) 00076 return getArgIdx(); 00077 return getVarIdx(); 00078 } 00079 00080 /// \brief Get the next scratch register operand index. 00081 unsigned getNextScratchIdx(unsigned StartIdx = 0) const; 00082 }; 00083 00084 class StackMaps { 00085 public: 00086 struct Location { 00087 enum LocationType { Unprocessed, Register, Direct, Indirect, Constant, 00088 ConstantIndex }; 00089 LocationType LocType; 00090 unsigned Size; 00091 unsigned Reg; 00092 int64_t Offset; 00093 Location() : LocType(Unprocessed), Size(0), Reg(0), Offset(0) {} 00094 Location(LocationType LocType, unsigned Size, unsigned Reg, int64_t Offset) 00095 : LocType(LocType), Size(Size), Reg(Reg), Offset(Offset) {} 00096 }; 00097 00098 struct LiveOutReg { 00099 unsigned short Reg; 00100 unsigned short RegNo; 00101 unsigned short Size; 00102 00103 LiveOutReg() : Reg(0), RegNo(0), Size(0) {} 00104 LiveOutReg(unsigned short Reg, unsigned short RegNo, unsigned short Size) 00105 : Reg(Reg), RegNo(RegNo), Size(Size) {} 00106 00107 void MarkInvalid() { Reg = 0; } 00108 00109 // Only sort by the dwarf register number. 00110 bool operator< (const LiveOutReg &LO) const { return RegNo < LO.RegNo; } 00111 static bool IsInvalid(const LiveOutReg &LO) { return LO.Reg == 0; } 00112 }; 00113 00114 // OpTypes are used to encode information about the following logical 00115 // operand (which may consist of several MachineOperands) for the 00116 // OpParser. 00117 typedef enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp } OpType; 00118 00119 StackMaps(AsmPrinter &AP); 00120 00121 void reset() { 00122 CSInfos.clear(); 00123 ConstPool.clear(); 00124 FnStackSize.clear(); 00125 } 00126 00127 /// \brief Generate a stackmap record for a stackmap instruction. 00128 /// 00129 /// MI must be a raw STACKMAP, not a PATCHPOINT. 00130 void recordStackMap(const MachineInstr &MI); 00131 00132 /// \brief Generate a stackmap record for a patchpoint instruction. 00133 void recordPatchPoint(const MachineInstr &MI); 00134 00135 /// If there is any stack map data, create a stack map section and serialize 00136 /// the map info into it. This clears the stack map data structures 00137 /// afterwards. 00138 void serializeToStackMapSection(); 00139 00140 private: 00141 static const char *WSMP; 00142 00143 typedef SmallVector<Location, 8> LocationVec; 00144 typedef SmallVector<LiveOutReg, 8> LiveOutVec; 00145 typedef MapVector<int64_t, int64_t> ConstantPool; 00146 typedef MapVector<const MCSymbol *, uint64_t> FnStackSizeMap; 00147 00148 struct CallsiteInfo { 00149 const MCExpr *CSOffsetExpr; 00150 uint64_t ID; 00151 LocationVec Locations; 00152 LiveOutVec LiveOuts; 00153 CallsiteInfo() : CSOffsetExpr(nullptr), ID(0) {} 00154 CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID, 00155 LocationVec &Locations, LiveOutVec &LiveOuts) 00156 : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(Locations), 00157 LiveOuts(LiveOuts) {} 00158 }; 00159 00160 typedef std::vector<CallsiteInfo> CallsiteInfoList; 00161 00162 AsmPrinter &AP; 00163 CallsiteInfoList CSInfos; 00164 ConstantPool ConstPool; 00165 FnStackSizeMap FnStackSize; 00166 00167 MachineInstr::const_mop_iterator 00168 parseOperand(MachineInstr::const_mop_iterator MOI, 00169 MachineInstr::const_mop_iterator MOE, 00170 LocationVec &Locs, LiveOutVec &LiveOuts) const; 00171 00172 /// \brief Create a live-out register record for the given register @p Reg. 00173 LiveOutReg createLiveOutReg(unsigned Reg, 00174 const TargetRegisterInfo *TRI) const; 00175 00176 /// \brief Parse the register live-out mask and return a vector of live-out 00177 /// registers that need to be recorded in the stackmap. 00178 LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const; 00179 00180 /// This should be called by the MC lowering code _immediately_ before 00181 /// lowering the MI to an MCInst. It records where the operands for the 00182 /// instruction are stored, and outputs a label to record the offset of 00183 /// the call from the start of the text section. In special cases (e.g. AnyReg 00184 /// calling convention) the return register is also recorded if requested. 00185 void recordStackMapOpers(const MachineInstr &MI, uint64_t ID, 00186 MachineInstr::const_mop_iterator MOI, 00187 MachineInstr::const_mop_iterator MOE, 00188 bool recordResult = false); 00189 00190 /// \brief Emit the stackmap header. 00191 void emitStackmapHeader(MCStreamer &OS); 00192 00193 /// \brief Emit the function frame record for each function. 00194 void emitFunctionFrameRecords(MCStreamer &OS); 00195 00196 /// \brief Emit the constant pool. 00197 void emitConstantPoolEntries(MCStreamer &OS); 00198 00199 /// \brief Emit the callsite info for each stackmap/patchpoint intrinsic call. 00200 void emitCallsiteEntries(MCStreamer &OS, const TargetRegisterInfo *TRI); 00201 }; 00202 00203 } 00204 00205 #endif