123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- #pragma once
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #endif
- #ifndef LLVM_CODEGEN_STACKMAPS_H
- #define LLVM_CODEGEN_STACKMAPS_H
- #include "llvm/ADT/MapVector.h"
- #include "llvm/ADT/SmallVector.h"
- #include "llvm/CodeGen/MachineInstr.h"
- #include "llvm/IR/CallingConv.h"
- #include "llvm/Support/Debug.h"
- #include <algorithm>
- #include <cassert>
- #include <cstdint>
- #include <vector>
- namespace llvm {
- class AsmPrinter;
- class MCSymbol;
- class MCExpr;
- class MCStreamer;
- class raw_ostream;
- class TargetRegisterInfo;
- class StackMapOpers {
- public:
-
- enum { IDPos, NBytesPos };
- private:
- const MachineInstr* MI;
- public:
- explicit StackMapOpers(const MachineInstr *MI);
-
- uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
-
- uint32_t getNumPatchBytes() const {
- return MI->getOperand(NBytesPos).getImm();
- }
-
-
- unsigned getVarIdx() const {
-
- return 2;
- }
- };
- class PatchPointOpers {
- public:
-
- enum { IDPos, NBytesPos, TargetPos, NArgPos, CCPos, MetaEnd };
- private:
- const MachineInstr *MI;
- bool HasDef;
- unsigned getMetaIdx(unsigned Pos = 0) const {
- assert(Pos < MetaEnd && "Meta operand index out of range.");
- return (HasDef ? 1 : 0) + Pos;
- }
- const MachineOperand &getMetaOper(unsigned Pos) const {
- return MI->getOperand(getMetaIdx(Pos));
- }
- public:
- explicit PatchPointOpers(const MachineInstr *MI);
- bool isAnyReg() const { return (getCallingConv() == CallingConv::AnyReg); }
- bool hasDef() const { return HasDef; }
-
- uint64_t getID() const { return getMetaOper(IDPos).getImm(); }
-
- uint32_t getNumPatchBytes() const {
- return getMetaOper(NBytesPos).getImm();
- }
-
- const MachineOperand &getCallTarget() const {
- return getMetaOper(TargetPos);
- }
-
- CallingConv::ID getCallingConv() const {
- return getMetaOper(CCPos).getImm();
- }
- unsigned getArgIdx() const { return getMetaIdx() + MetaEnd; }
-
- uint32_t getNumCallArgs() const {
- return MI->getOperand(getMetaIdx(NArgPos)).getImm();
- }
-
-
- unsigned getVarIdx() const {
- return getMetaIdx() + MetaEnd + getNumCallArgs();
- }
-
-
- unsigned getStackMapStartIdx() const {
- if (isAnyReg())
- return getArgIdx();
- return getVarIdx();
- }
-
- unsigned getNextScratchIdx(unsigned StartIdx = 0) const;
- };
- class StatepointOpers {
-
-
-
-
-
-
- enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
-
-
- enum { CCOffset = 1, FlagsOffset = 3, NumDeoptOperandsOffset = 5 };
- public:
- explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {
- NumDefs = MI->getNumDefs();
- }
-
- unsigned getIDPos() const { return NumDefs + IDPos; }
-
- unsigned getNBytesPos() const { return NumDefs + NBytesPos; }
-
- unsigned getNCallArgsPos() const { return NumDefs + NCallArgsPos; }
-
-
- unsigned getVarIdx() const {
- return MI->getOperand(NumDefs + NCallArgsPos).getImm() + MetaEnd + NumDefs;
- }
-
- unsigned getCCIdx() const { return getVarIdx() + CCOffset; }
-
- unsigned getFlagsIdx() const { return getVarIdx() + FlagsOffset; }
-
- unsigned getNumDeoptArgsIdx() const {
- return getVarIdx() + NumDeoptOperandsOffset;
- }
-
- uint64_t getID() const { return MI->getOperand(NumDefs + IDPos).getImm(); }
-
- uint32_t getNumPatchBytes() const {
- return MI->getOperand(NumDefs + NBytesPos).getImm();
- }
-
- const MachineOperand &getCallTarget() const {
- return MI->getOperand(NumDefs + CallTargetPos);
- }
-
- CallingConv::ID getCallingConv() const {
- return MI->getOperand(getCCIdx()).getImm();
- }
-
- uint64_t getFlags() const { return MI->getOperand(getFlagsIdx()).getImm(); }
- uint64_t getNumDeoptArgs() const {
- return MI->getOperand(getNumDeoptArgsIdx()).getImm();
- }
-
- unsigned getNumGcMapEntriesIdx();
-
- unsigned getNumAllocaIdx();
-
- unsigned getNumGCPtrIdx();
-
- int getFirstGCPtrIdx();
-
-
-
- unsigned
- getGCPointerMap(SmallVectorImpl<std::pair<unsigned, unsigned>> &GCMap);
-
-
- bool isFoldableReg(Register Reg) const;
-
-
- static bool isFoldableReg(const MachineInstr *MI, Register Reg);
- private:
- const MachineInstr *MI;
- unsigned NumDefs;
- };
- class StackMaps {
- public:
- struct Location {
- enum LocationType {
- Unprocessed,
- Register,
- Direct,
- Indirect,
- Constant,
- ConstantIndex
- };
- LocationType Type = Unprocessed;
- unsigned Size = 0;
- unsigned Reg = 0;
- int64_t Offset = 0;
- Location() = default;
- Location(LocationType Type, unsigned Size, unsigned Reg, int64_t Offset)
- : Type(Type), Size(Size), Reg(Reg), Offset(Offset) {}
- };
- struct LiveOutReg {
- unsigned short Reg = 0;
- unsigned short DwarfRegNum = 0;
- unsigned short Size = 0;
- LiveOutReg() = default;
- LiveOutReg(unsigned short Reg, unsigned short DwarfRegNum,
- unsigned short Size)
- : Reg(Reg), DwarfRegNum(DwarfRegNum), Size(Size) {}
- };
-
-
-
- using OpType = enum { DirectMemRefOp, IndirectMemRefOp, ConstantOp };
- StackMaps(AsmPrinter &AP);
-
-
- static unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx);
- void reset() {
- CSInfos.clear();
- ConstPool.clear();
- FnInfos.clear();
- }
- using LocationVec = SmallVector<Location, 8>;
- using LiveOutVec = SmallVector<LiveOutReg, 8>;
- using ConstantPool = MapVector<uint64_t, uint64_t>;
- struct FunctionInfo {
- uint64_t StackSize = 0;
- uint64_t RecordCount = 1;
- FunctionInfo() = default;
- explicit FunctionInfo(uint64_t StackSize) : StackSize(StackSize) {}
- };
- struct CallsiteInfo {
- const MCExpr *CSOffsetExpr = nullptr;
- uint64_t ID = 0;
- LocationVec Locations;
- LiveOutVec LiveOuts;
- CallsiteInfo() = default;
- CallsiteInfo(const MCExpr *CSOffsetExpr, uint64_t ID,
- LocationVec &&Locations, LiveOutVec &&LiveOuts)
- : CSOffsetExpr(CSOffsetExpr), ID(ID), Locations(std::move(Locations)),
- LiveOuts(std::move(LiveOuts)) {}
- };
- using FnInfoMap = MapVector<const MCSymbol *, FunctionInfo>;
- using CallsiteInfoList = std::vector<CallsiteInfo>;
-
-
-
- void recordStackMap(const MCSymbol &L,
- const MachineInstr &MI);
-
- void recordPatchPoint(const MCSymbol &L,
- const MachineInstr &MI);
-
- void recordStatepoint(const MCSymbol &L,
- const MachineInstr &MI);
-
-
-
- void serializeToStackMapSection();
-
- CallsiteInfoList &getCSInfos() { return CSInfos; }
-
- FnInfoMap &getFnInfos() { return FnInfos; }
- private:
- static const char *WSMP;
- AsmPrinter &AP;
- CallsiteInfoList CSInfos;
- ConstantPool ConstPool;
- FnInfoMap FnInfos;
- MachineInstr::const_mop_iterator
- parseOperand(MachineInstr::const_mop_iterator MOI,
- MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
- LiveOutVec &LiveOuts) const;
-
-
- void parseStatepointOpers(const MachineInstr &MI,
- MachineInstr::const_mop_iterator MOI,
- MachineInstr::const_mop_iterator MOE,
- LocationVec &Locations, LiveOutVec &LiveOuts);
-
- LiveOutReg createLiveOutReg(unsigned Reg,
- const TargetRegisterInfo *TRI) const;
-
-
- LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
-
-
-
-
-
-
-
- void recordStackMapOpers(const MCSymbol &L,
- const MachineInstr &MI, uint64_t ID,
- MachineInstr::const_mop_iterator MOI,
- MachineInstr::const_mop_iterator MOE,
- bool recordResult = false);
-
- void emitStackmapHeader(MCStreamer &OS);
-
- void emitFunctionFrameRecords(MCStreamer &OS);
-
- void emitConstantPoolEntries(MCStreamer &OS);
-
- void emitCallsiteEntries(MCStreamer &OS);
- void print(raw_ostream &OS);
- void debug() { print(dbgs()); }
- };
- }
- #endif
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
|