19#include "llvm/IR/IntrinsicsSPIRV.h"
23#define DEBUG_TYPE "spirv-postlegalizer"
56 Register SrcReg =
I->getOperand(
I->getNumOperands() - 1).getReg();
59 assert(DefType->getOpcode() == SPIRV::OpTypeVector);
66 for (
unsigned i = 0; i <
I->getNumDefs() && !ScalarType; ++i) {
67 for (
const auto &
Use :
68 MRI.use_nodbg_instructions(
I->getOperand(i).getReg())) {
69 assert(
Use.getOpcode() == TargetOpcode::G_BUILD_VECTOR &&
70 "Expected use of G_UNMERGE_VALUES to be a G_BUILD_VECTOR");
83 for (
unsigned i = 0; i <
I->getNumDefs(); ++i) {
84 Register DefReg =
I->getOperand(i).getReg();
88 LLT DefLLT =
MRI.getType(DefReg);
107 Register ResVReg =
I->getOperand(0).getReg();
121 unsigned StartOp,
unsigned EndOp) {
123 for (
unsigned i = StartOp; i < EndOp; ++i) {
125#ifdef EXPENSIVE_CHECKS
126 assert(!ResType ||
Type == ResType &&
"Conflicting type from operands.");
162 switch (
Use.getOpcode()) {
163 case TargetOpcode::G_BUILD_VECTOR:
164 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
165 case TargetOpcode::G_UNMERGE_VALUES:
179 Register ResVReg =
I->getOperand(0).getReg();
180 switch (
I->getOpcode()) {
181 case TargetOpcode::G_CONSTANT:
182 case TargetOpcode::G_ANYEXT:
184 case TargetOpcode::G_BUILD_VECTOR:
186 case TargetOpcode::G_SHUFFLE_VECTOR:
189 if (
I->getNumDefs() == 1 &&
I->getNumOperands() > 1 &&
190 I->getOperand(1).isReg())
201 Register ResVReg =
I->getOperand(0).getReg();
206 if (
I->getOpcode() == TargetOpcode::G_UNMERGE_VALUES)
222 if (!
MRI.getRegClassOrNull(ResVReg)) {
231 LLVM_DEBUG(
dbgs() <<
"Checking if instruction requires a SPIR-V type: "
233 if (
I.getNumDefs() == 0) {
234 LLVM_DEBUG(
dbgs() <<
"Instruction does not have a definition.\n");
238 if (!
I.isPreISelOpcode()) {
239 LLVM_DEBUG(
dbgs() <<
"Instruction is not a generic instruction.\n");
243 Register ResultRegister =
I.defs().begin()->getReg();
246 if (!
MRI.getRegClassOrNull(ResultRegister)) {
269 if (Worklist.
empty()) {
275 for (
auto *
I : Worklist) {
I->dump(); });
290 Worklist = std::move(NextWorklist);
291 LLVM_DEBUG(
dbgs() <<
"Worklist size: " << Worklist.size() <<
"\n");
294 if (Worklist.
empty())
297 for (
auto *
I : Worklist) {
299 Register ResVReg =
I->getOperand(0).getReg();
300 const LLT &ResLLT =
MRI.getType(ResVReg);
311 <<
", defaulting to " << *ResType <<
"\n");
318 if (UseInstr.getOpcode() == SPIRV::ASSIGN_TYPE) {
329 <<
printReg(ResultRegister,
MRI.getTargetRegisterInfo())
330 <<
" with type: " << *ResultType);
338 MRI.createGenericVirtualRegister(
MRI.getType(ResultRegister));
339 const auto *RegClass = GR->
getRegClass(ResultType);
340 MRI.setRegClass(NewReg, RegClass);
341 MRI.setRegClass(ResultRegister, RegClass);
355 for (
unsigned I = 0,
E =
MI.getNumDefs();
I !=
E; ++
I) {
357 if (MO.
getReg() == ResultRegister) {
366 LLVM_DEBUG(
dbgs() <<
"Entering ensureAssignTypeForTypeFolding for function "
376 Register ResultRegister =
MI.defs().begin()->getReg();
393 std::stack<MachineBasicBlock *> ToVisit;
396 ToVisit.push(&Start);
397 Seen.
insert(ToVisit.top());
398 while (ToVisit.size() != 0) {
404 for (
auto Succ :
MBB->successors()) {
422 SPIRVGlobalRegistry *GR =
ST.getSPIRVGlobalRegistry();
423 GR->setCurrentFunc(MF);
432char SPIRVPostLegalizer::
ID = 0;
435 return new SPIRVPostLegalizer();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool deduceAndAssignSpirvType(MachineInstr *I, MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB)
static SPIRVType * deduceIntTypeFromResult(Register ResVReg, MachineIRBuilder &MIB, SPIRVGlobalRegistry *GR)
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
static void registerSpirvTypeForNewInstructions(MachineFunction &MF, SPIRVGlobalRegistry *GR)
static bool hasAssignType(Register Reg, MachineRegisterInfo &MRI)
static SPIRVType * deduceResultTypeFromOperands(MachineInstr *I, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB)
static SPIRVType * deduceTypeFromOperandRange(MachineInstr *I, MachineIRBuilder &MIB, SPIRVGlobalRegistry *GR, unsigned StartOp, unsigned EndOp)
static SPIRVType * deduceTypeFromUses(Register Reg, MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB)
static SPIRVType * deduceTypeForResultRegister(MachineInstr *Use, Register UseRegister, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB)
static void generateAssignType(MachineInstr &MI, Register ResultRegister, SPIRVType *ResultType, SPIRVGlobalRegistry *GR, MachineRegisterInfo &MRI)
static bool deduceAndAssignTypeForGUnmerge(MachineInstr *I, MachineFunction &MF, SPIRVGlobalRegistry *GR)
static void ensureAssignTypeForTypeFolding(MachineFunction &MF, SPIRVGlobalRegistry *GR)
static bool requiresSpirvType(MachineInstr &I, SPIRVGlobalRegistry *GR, MachineRegisterInfo &MRI)
static SPIRVType * deduceTypeFromSingleOperand(MachineInstr *I, MachineIRBuilder &MIB, SPIRVGlobalRegistry *GR, unsigned OpIdx)
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Helper class to build MachineInstr.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
MachineRegisterInfo * getMRI()
Getter for MRI.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
Wrapper class representing virtual and physical registers.
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, const MachineFunction &MF)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getScalarOrVectorComponentType(Register VReg) const
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
const SPIRVInstrInfo * getInstrInfo() const override
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
A Use represents the edge between a Value definition and its users.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
bool isTypeFoldingSupported(unsigned Opcode)
void updateRegType(Register Reg, Type *Ty, SPIRVType *SpirvTy, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Helper external function for assigning SPIRVType to a register, ensuring the register class and type ...
void processInstr(MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, SPIRVGlobalRegistry *GR, SPIRVType *KnownResType)
FunctionPass * createSPIRVPostLegalizerPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void setRegClassType(Register Reg, SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF, bool Force)
const MachineInstr SPIRVType
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.