LLVM 20.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 "SPIRV.h"
16#include "SPIRVModuleAnalysis.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->getOpcode());
26 // Propagate previously set flags
27 OutMI.setFlags(MI->getAsmPrinterFlags());
28 const MachineFunction *MF = MI->getMF();
29 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
30 const MachineOperand &MO = MI->getOperand(i);
31 MCOperand MCOp;
32 switch (MO.getType()) {
33 default:
34 llvm_unreachable("unknown operand type");
36 Register FuncReg = MAI->getFuncReg(dyn_cast<Function>(MO.getGlobal()));
37 if (!FuncReg.isValid()) {
38 std::string DiagMsg;
39 raw_string_ostream OS(DiagMsg);
40 MI->print(OS);
41 DiagMsg = "Unknown function in:" + DiagMsg;
42 report_fatal_error(DiagMsg.c_str());
43 }
44 MCOp = MCOperand::createReg(FuncReg);
45 break;
46 }
49 break;
51 Register NewReg = MAI->getRegisterAlias(MF, MO.getReg());
52 MCOp = MCOperand::createReg(NewReg.isValid() ? NewReg : MO.getReg());
53 break;
54 }
56 if (MI->getOpcode() == SPIRV::OpExtInst && i == 2) {
57 Register Reg = MAI->getExtInstSetReg(MO.getImm());
58 MCOp = MCOperand::createReg(Reg);
59 } else {
60 MCOp = MCOperand::createImm(MO.getImm());
61 }
62 break;
66 break;
67 }
68
69 OutMI.addOperand(MCOp);
70 }
71}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
raw_pwrite_stream & OS
float convertToFloat() const
Converts this APFloat to host float value.
Definition: APFloat.cpp:5555
const APFloat & getValueAPF() const
Definition: Constants.h:314
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
void setFlags(unsigned F)
Definition: MCInst.h:201
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
void setOpcode(unsigned Op)
Definition: MCInst.h:198
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:142
static MCOperand createDFPImm(uint64_t Val)
Definition: MCInst.h:156
Representation of each machine instruction.
Definition: MachineInstr.h:69
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.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isValid() const
Definition: Register.h:116
void lower(const MachineInstr *MI, MCInst &OutMI, SPIRV::ModuleAnalysisInfo *MAI) const
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#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.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
Register getRegisterAlias(const MachineFunction *MF, Register Reg)
Register getExtInstSetReg(unsigned SetNum)
Register getOrCreateMBBRegister(const MachineBasicBlock &MBB)
Register getFuncReg(const Function *F)