LLVM 17.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 "Hexagon.h"
15#include "HexagonAsmPrinter.h"
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
30#include <cassert>
31
32using namespace llvm;
33
34namespace llvm {
35
36void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
37 MCInst &MCB, HexagonAsmPrinter &AP);
38
39} // end namespace llvm
40
41static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
42 HexagonAsmPrinter &Printer, bool MustExtend) {
43 MCContext &MC = Printer.OutContext;
44 const MCExpr *ME;
45
46 // Populate the relocation type based on Hexagon target flags
47 // set on an operand
50 default:
52 break;
55 break;
58 break;
61 break;
64 break;
67 break;
70 break;
73 break;
76 break;
79 break;
82 break;
83 }
84
86
87 if (!MO.isJTI() && MO.getOffset())
89 MC);
90
91 ME = HexagonMCExpr::create(ME, MC);
92 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
93 return MCOperand::createExpr(ME);
94}
95
96// Create an MCInst from a MachineInstr
98 MCInst &MCB, HexagonAsmPrinter &AP) {
99 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
101 return;
102 }
103 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
105 return;
106 }
107 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_ENTER) {
108 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::FUNCTION_ENTER);
109 return;
110 }
111 if (MI->getOpcode() == Hexagon::PATCHABLE_FUNCTION_EXIT) {
112 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::FUNCTION_EXIT);
113 return;
114 }
115 if (MI->getOpcode() == Hexagon::PATCHABLE_TAIL_CALL) {
116 AP.EmitSled(*MI, HexagonAsmPrinter::SledKind::TAIL_CALL);
117 return;
118 }
119
120 MCInst *MCI = AP.OutContext.createMCInst();
121 MCI->setOpcode(MI->getOpcode());
122 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
123 "MCI opcode should have been set on construction");
124
125 for (const MachineOperand &MO : MI->operands()) {
126 MCOperand MCO;
127 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
128
129 switch (MO.getType()) {
130 default:
131 MI->print(errs());
132 llvm_unreachable("unknown operand type");
134 continue;
136 // Ignore all implicit register operands.
137 if (MO.isImplicit())
138 continue;
139 MCO = MCOperand::createReg(MO.getReg());
140 break;
142 APFloat Val = MO.getFPImm()->getValueAPF();
143 // FP immediates are used only when setting GPRs, so they may be dealt
144 // with like regular immediates from this point on.
145 auto Expr = HexagonMCExpr::create(
147 AP.OutContext),
148 AP.OutContext);
149 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
150 MCO = MCOperand::createExpr(Expr);
151 break;
152 }
154 auto Expr = HexagonMCExpr::create(
155 MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
156 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
157 MCO = MCOperand::createExpr(Expr);
158 break;
159 }
161 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
162 AP.OutContext);
163 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
164 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
165 MCO = MCOperand::createExpr(Expr);
166 break;
167 }
169 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
170 break;
172 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
173 AP, MustExtend);
174 break;
176 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
177 break;
179 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
180 break;
182 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
183 MustExtend);
184 break;
185 }
186
187 MCI->addOperand(MCO);
188 }
189 AP.HexagonProcessInstruction(*MCI, *MI);
192}
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...
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, HexagonAsmPrinter &Printer, bool MustExtend)
IRTranslator LLVM IR MI
Memory true print Memory SSA Printer
Definition: MemorySSA.cpp:78
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
APInt bitcastToAPInt() const
Definition: APFloat.h:1184
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:557
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:662
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:94
MCSymbol * GetExternalSymbolSymbol(StringRef 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:525
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:76
MCInst * createMCInst()
Create and return a new MC instruction.
Definition: MCContext.cpp:193
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
unsigned getOpcode() const
Definition: MCInst.h:198
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:169
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
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:68
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:261
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.