#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/MC/MCSymbol.h"
#include "llvm/Support/Debug.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <vector>
namespace llvm {
class AsmPrinter;
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 {
public:
enum { IDPos, NBytesPos, NCallArgsPos, CallTargetPos, MetaEnd };
enum { CCOffset = 1, FlagsOffset = 3, NumDeoptOperandsOffset = 5 };
explicit StatepointOpers(const MachineInstr *MI) : MI(MI) {}
unsigned getVarIdx() const {
return MI->getOperand(NCallArgsPos).getImm() + MetaEnd;
}
uint64_t getID() const { return MI->getOperand(IDPos).getImm(); }
uint32_t getNumPatchBytes() const {
return MI->getOperand(NBytesPos).getImm();
}
const MachineOperand &getCallTarget() const {
return MI->getOperand(CallTargetPos);
}
private:
const MachineInstr *MI;
};
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);
void reset() {
CSInfos.clear();
ConstPool.clear();
FnInfos.clear();
}
void recordStackMap(const MachineInstr &MI);
void recordPatchPoint(const MachineInstr &MI);
void recordStatepoint(const MachineInstr &MI);
void serializeToStackMapSection();
private:
static const char *WSMP;
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>;
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;
LiveOutReg createLiveOutReg(unsigned Reg,
const TargetRegisterInfo *TRI) const;
LiveOutVec parseRegisterLiveOutMask(const uint32_t *Mask) const;
void recordStackMapOpers(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