LLVM 24.0.0git
SPIRVMCInstLower.cpp
Go to the documentation of this file.
1//=- SPIRVMCInstLower.cpp - Convert SPIR-V MachineInstr to MCInst -*- 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// This file contains code to lower SPIR-V MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRVMCInstLower.h"
15#include "SPIRVModuleAnalysis.h"
16#include "SPIRVSubtarget.h"
17#include "SPIRVUtils.h"
19#include "llvm/IR/Constants.h"
20
21using namespace llvm;
22
24 SPIRV::ModuleAnalysisInfo *MAI) const {
25 OutMI.setOpcode(MI->isPHI() ? SPIRV::OpPhi : MI->getOpcode());
26 // Propagate previously set flags
27 if (MI->getAsmPrinterFlags() & SPIRV::ASM_PRINTER_WIDTH16)
29 if (MI->getAsmPrinterFlags() & SPIRV::ASM_PRINTER_WIDTH64)
31 const MachineFunction *MF = MI->getMF();
32 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
33 const MachineOperand &MO = MI->getOperand(i);
34 MCOperand MCOp;
35 switch (MO.getType()) {
36 default:
37 llvm_unreachable("unknown operand type");
39 MCRegister Reg =
41 if (!Reg.isValid()) {
42 std::string DiagMsg;
43 raw_string_ostream OS(DiagMsg);
44 MI->print(OS);
45 DiagMsg = "Unknown global object in:" + DiagMsg;
46 report_fatal_error(DiagMsg.c_str());
47 }
48 MCOp = MCOperand::createReg(Reg);
49 break;
50 }
53 break;
55 MCRegister NewReg = MAI->getRegisterAlias(MF, MO.getReg());
56 MCOp = MCOperand::createReg(NewReg.isValid() ? NewReg
57 : MO.getReg().asMCReg());
58 break;
59 }
61 if (MI->getOpcode() == SPIRV::OpExtInst && i == 2) {
62 MCRegister Reg = MAI->getExtInstSetReg(MO.getImm());
63 MCOp = MCOperand::createReg(Reg);
64 } else {
65 MCOp = MCOperand::createImm(MO.getImm());
66 }
67 break;
71 break;
72 }
73
74 OutMI.addOperand(MCOp);
75
76 // OpPhi takes the result type right after the result.
77 if (i == 0 && MI->isPHI()) {
79 MF->getSubtarget<SPIRVSubtarget>().getSPIRVGlobalRegistry();
80 Register TypeReg =
81 GR->getSPIRVTypeID(GR->getSPIRVTypeForVReg(MO.getReg(), MF));
82 MCRegister TypeAlias = MAI->getRegisterAlias(MF, TypeReg);
83 assert(TypeAlias.isValid() && "OpPhi result type has no global ID");
84 OutMI.addOperand(MCOperand::createReg(TypeAlias));
85 }
86 }
87}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
Definition APFloat.cpp:6121
const APFloat & getValueAPF() const
Definition Constants.h:463
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void setFlags(unsigned F)
Definition MCInst.h:204
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
static MCOperand createDFPImm(uint64_t Val)
Definition MCInst.h:159
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isValid() const
Definition MCRegister.h:84
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_FPImmediate
Floating-point immediate operand.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const
SPIRVTypeInst getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
void lower(const MachineInstr *MI, MCInst &OutMI, SPIRV::ModuleAnalysisInfo *MAI) const
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
MCRegister getExtInstSetReg(unsigned SetNum)
MCRegister getRegisterAlias(const MachineFunction *MF, Register Reg)
MCRegister getOrCreateMBBRegister(const MachineBasicBlock &MBB)
MCRegister getGlobalObjReg(const GlobalObject *GO)