1349cc55cSDimitry Andric //===- R600MCInstLower.cpp - Lower R600 MachineInstr to an MCInst ---------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric //
9349cc55cSDimitry Andric /// \file
10349cc55cSDimitry Andric /// Code to lower R600 MachineInstrs to their corresponding MCInst.
11349cc55cSDimitry Andric //
12349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
13349cc55cSDimitry Andric //
14349cc55cSDimitry Andric
15349cc55cSDimitry Andric #include "AMDGPUMCInstLower.h"
16753f127fSDimitry Andric #include "MCTargetDesc/R600MCTargetDesc.h"
17349cc55cSDimitry Andric #include "R600AsmPrinter.h"
18349cc55cSDimitry Andric #include "R600Subtarget.h"
19349cc55cSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
20349cc55cSDimitry Andric #include "llvm/MC/MCContext.h"
21349cc55cSDimitry Andric #include "llvm/MC/MCExpr.h"
22349cc55cSDimitry Andric
23*bdd1243dSDimitry Andric namespace {
24349cc55cSDimitry Andric class R600MCInstLower : public AMDGPUMCInstLower {
25349cc55cSDimitry Andric public:
26349cc55cSDimitry Andric R600MCInstLower(MCContext &ctx, const R600Subtarget &ST,
27349cc55cSDimitry Andric const AsmPrinter &AP);
28349cc55cSDimitry Andric
29349cc55cSDimitry Andric /// Lower a MachineInstr to an MCInst
30349cc55cSDimitry Andric void lower(const MachineInstr *MI, MCInst &OutMI) const;
31349cc55cSDimitry Andric };
32*bdd1243dSDimitry Andric } // namespace
33349cc55cSDimitry Andric
R600MCInstLower(MCContext & Ctx,const R600Subtarget & ST,const AsmPrinter & AP)34349cc55cSDimitry Andric R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST,
35349cc55cSDimitry Andric const AsmPrinter &AP)
36349cc55cSDimitry Andric : AMDGPUMCInstLower(Ctx, ST, AP) {}
37349cc55cSDimitry Andric
lower(const MachineInstr * MI,MCInst & OutMI) const38349cc55cSDimitry Andric void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
39349cc55cSDimitry Andric OutMI.setOpcode(MI->getOpcode());
40349cc55cSDimitry Andric for (const MachineOperand &MO : MI->explicit_operands()) {
41349cc55cSDimitry Andric MCOperand MCOp;
42349cc55cSDimitry Andric lowerOperand(MO, MCOp);
43349cc55cSDimitry Andric OutMI.addOperand(MCOp);
44349cc55cSDimitry Andric }
45349cc55cSDimitry Andric }
46349cc55cSDimitry Andric
emitInstruction(const MachineInstr * MI)47349cc55cSDimitry Andric void R600AsmPrinter::emitInstruction(const MachineInstr *MI) {
48753f127fSDimitry Andric R600_MC::verifyInstructionPredicates(MI->getOpcode(),
49753f127fSDimitry Andric getSubtargetInfo().getFeatureBits());
50753f127fSDimitry Andric
51349cc55cSDimitry Andric const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>();
52349cc55cSDimitry Andric R600MCInstLower MCInstLowering(OutContext, STI, *this);
53349cc55cSDimitry Andric
54349cc55cSDimitry Andric StringRef Err;
55349cc55cSDimitry Andric if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
56349cc55cSDimitry Andric LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
57349cc55cSDimitry Andric C.emitError("Illegal instruction detected: " + Err);
58349cc55cSDimitry Andric MI->print(errs());
59349cc55cSDimitry Andric }
60349cc55cSDimitry Andric
61349cc55cSDimitry Andric if (MI->isBundle()) {
62349cc55cSDimitry Andric const MachineBasicBlock *MBB = MI->getParent();
63349cc55cSDimitry Andric MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
64349cc55cSDimitry Andric while (I != MBB->instr_end() && I->isInsideBundle()) {
65349cc55cSDimitry Andric emitInstruction(&*I);
66349cc55cSDimitry Andric ++I;
67349cc55cSDimitry Andric }
68349cc55cSDimitry Andric } else {
69349cc55cSDimitry Andric MCInst TmpInst;
70349cc55cSDimitry Andric MCInstLowering.lower(MI, TmpInst);
71349cc55cSDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
72349cc55cSDimitry Andric }
73349cc55cSDimitry Andric }
74349cc55cSDimitry Andric
lowerConstant(const Constant * CV)75349cc55cSDimitry Andric const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) {
76349cc55cSDimitry Andric if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
77349cc55cSDimitry Andric return E;
78349cc55cSDimitry Andric return AsmPrinter::lowerConstant(CV);
79349cc55cSDimitry Andric }
80