LLVM 20.0.0git
HexagonMCInstLower.cpp
Go to the documentation of this file.
1//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
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 Hexagon MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonAsmPrinter.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
29#include <cassert>
30
31using namespace llvm;
32
33namespace llvm {
34
35void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
36 MCInst &MCB, HexagonAsmPrinter &AP);
37
38} // end namespace llvm
39
40static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
41 HexagonAsmPrinter &Printer, bool MustExtend) {
42 MCContext &MC = Printer.OutContext;
43 const MCExpr *ME;
44
45 // Populate the relocation type based on Hexagon target flags
46 // set on an operand
49 default:
51 break;
54 break;
57 break;
60 break;
63 break;
66 break;
69 break;
72 break;
75 break;
78 break;
81 break;
82 }
83
85
86 if (!MO.isJTI() && MO.getOffset())
88 MC);
89
90 ME = HexagonMCExpr::create(ME, MC);
91 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
92 return MCOperand::createExpr(ME);
93}
94
95// Create an MCInst from a MachineInstr
97 MCInst &MCB, HexagonAsmPrinter &AP) {
98 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
100 return;
101 }
102 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
104 return;
105 }
106 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_ENTER) {
107 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::FUNCTION_ENTER);
108 return;
109 }
110 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_EXIT) {
111 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::FUNCTION_EXIT);
112 return;
113 }
114 if (MI->getOpcode() == Hexagon::PATCHABLE_TAIL_CALL) {
115 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::TAIL_CALL);
116 return;
117 }
118
119 MCInst *MCI = AP.OutContext.createMCInst();
120 MCI->setOpcode(MI->getOpcode());
121 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
122 "MCI opcode should have been set on construction");
123
124 for (const MachineOperand &MO : MI->operands()) {
125 MCOperand MCO;
126 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
127
128 switch (MO.getType()) {
129 default:
130 MI->print(errs());
131 llvm_unreachable("unknown operand type");
133 continue;
135 // Ignore all implicit register operands.
136 if (MO.isImplicit())
137 continue;
138 MCO = MCOperand::createReg(MO.getReg());
139 break;
141 APFloat Val = MO.getFPImm()->getValueAPF();
142 // FP immediates are used only when setting GPRs, so they may be dealt
143 // with like regular immediates from this point on.
144 auto Expr = HexagonMCExpr::create(
146 AP.OutContext),
147 AP.OutContext);
148 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
149 MCO = MCOperand::createExpr(Expr);
150 break;
151 }
153 auto Expr = HexagonMCExpr::create(
154 MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
155 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
156 MCO = MCOperand::createExpr(Expr);
157 break;
158 }
160 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
161 AP.OutContext);
162 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
163 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
164 MCO = MCOperand::createExpr(Expr);
165 break;
166 }
168 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
169 break;
171 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
172 AP, MustExtend);
173 break;
175 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
176 break;
178 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
179 break;
181 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
182 MustExtend);
183 break;
184 }
185
186 MCI->addOperand(MCO);
187 }
188 AP.HexagonProcessInstruction(*MCI, *MI);
191}
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil pretty DXIL Metadata Pretty Printer
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, HexagonAsmPrinter &Printer, bool MustExtend)
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
APInt bitcastToAPInt() const
Definition: APFloat.h:1346
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:569
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:701
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
void EmitSled(const MachineInstr &MI, SledKind Kind)
void HexagonProcessInstruction(MCInst &Inst, const MachineInstr &MBB)
static HexagonMCExpr * create(MCExpr const *Expr, MCContext &Ctx)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:537
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Context object for machine code objects.
Definition: MCContext.h:83
MCInst * createMCInst()
Create and return a new MC instruction.
Definition: MCContext.cpp:194
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
unsigned getOpcode() const
Definition: MCInst.h:199
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
void setOpcode(unsigned Op)
Definition: MCInst.h:198
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:163
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:170
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
unsigned getTargetFlags() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
@ MO_FPImmediate
Floating-point immediate operand.
int64_t getOffset() const
Return the offset from the symbol in this operand.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_PCREL
MO_PCREL - On a symbol operand, indicates a PC-relative relocation Used for computing a global addres...
@ MO_GOT
MO_GOT - Indicates a GOT-relative relocation.
void extendIfNeeded(MCContext &Context, MCInstrInfo const &MCII, MCInst &MCB, MCInst const &MCI)
void setMustExtend(MCExpr const &Expr, bool Val=true)
RelocationType
Definition: XCOFF.h:262
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI, MCInst &MCB, HexagonAsmPrinter &AP)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.