LLVM  13.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 
32 using namespace llvm;
33 
34 namespace llvm {
35 
36 void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
37  MCInst &MCB, HexagonAsmPrinter &AP);
38 
39 } // end namespace llvm
40 
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;
56  case HexagonII::MO_GOT:
58  break;
59  case HexagonII::MO_LO16:
61  break;
62  case HexagonII::MO_HI16:
64  break;
67  break;
70  break;
73  break;
74  case HexagonII::MO_IE:
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  MCInst *MCI = AP.OutContext.createMCInst();
108  MCI->setOpcode(MI->getOpcode());
109  assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
110  "MCI opcode should have been set on construction");
111 
112  for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
113  const MachineOperand &MO = MI->getOperand(i);
114  MCOperand MCO;
115  bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
116 
117  switch (MO.getType()) {
118  default:
119  MI->print(errs());
120  llvm_unreachable("unknown operand type");
122  continue;
124  // Ignore all implicit register operands.
125  if (MO.isImplicit())
126  continue;
127  MCO = MCOperand::createReg(MO.getReg());
128  break;
130  APFloat Val = MO.getFPImm()->getValueAPF();
131  // FP immediates are used only when setting GPRs, so they may be dealt
132  // with like regular immediates from this point on.
133  auto Expr = HexagonMCExpr::create(
135  AP.OutContext),
136  AP.OutContext);
137  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
138  MCO = MCOperand::createExpr(Expr);
139  break;
140  }
142  auto Expr = HexagonMCExpr::create(
144  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
145  MCO = MCOperand::createExpr(Expr);
146  break;
147  }
149  MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
150  AP.OutContext);
151  Expr = HexagonMCExpr::create(Expr, AP.OutContext);
152  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
153  MCO = MCOperand::createExpr(Expr);
154  break;
155  }
157  MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
158  break;
161  AP, MustExtend);
162  break;
164  MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
165  break;
167  MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
168  break;
170  MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
171  MustExtend);
172  break;
173  }
174 
175  MCI->addOperand(MCO);
176  }
177  AP.HexagonProcessInstruction(*MCI, *MI);
178  HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
180 }
i
i
Definition: README.txt:29
HexagonMCTargetDesc.h
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
llvm::HexagonMCInstrInfo::setMustExtend
void setMustExtend(MCExpr const &Expr, bool Val=true)
Definition: HexagonMCInstrInfo.cpp:873
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
MachineInstr.h
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm::HexagonII::MO_HI16
@ MO_HI16
Definition: HexagonBaseInfo.h:188
llvm
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCSymbolRefExpr::VK_GOT
@ VK_GOT
Definition: MCExpr.h:198
llvm::MCOperand::createExpr
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:161
llvm::HexagonII::MO_GDGOT
@ MO_GDGOT
Definition: HexagonBaseInfo.h:195
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
llvm::HexagonII::MO_LO16
@ MO_LO16
Definition: HexagonBaseInfo.h:187
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
llvm::MachineOperand::getBlockAddress
const BlockAddress * getBlockAddress() const
Definition: MachineOperand.h:568
ErrorHandling.h
llvm::MCSymbolRefExpr::VK_Hexagon_GD_PLT
@ VK_Hexagon_GD_PLT
Definition: MCExpr.h:322
MachineBasicBlock.h
APInt.h
llvm::MachineOperand::isJTI
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
Definition: MachineOperand.h:337
HexagonAsmPrinter.h
llvm::ConstantFP::getValueAPF
const APFloat & getValueAPF() const
Definition: Constants.h:295
llvm::HexagonMCInstrInfo::extendIfNeeded
void extendIfNeeded(MCContext &Context, MCInstrInfo const &MCII, MCInst &MCB, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:227
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::HexagonMCInstrInfo::setInnerLoop
void setInnerLoop(MCInst &MCI)
Definition: HexagonMCInstrInfo.cpp:989
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:183
llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT
@ VK_Hexagon_GD_GOT
Definition: MCExpr.h:320
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
llvm::HexagonII::MO_GOT
@ MO_GOT
MO_GOT - Indicates a GOT-relative relocation.
Definition: HexagonBaseInfo.h:184
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:196
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:59
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:762
llvm::HexagonLowerToMC
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI, MCInst &MCB, HexagonAsmPrinter &AP)
Definition: HexagonMCInstLower.cpp:97
llvm::MachineOperand::isImplicit
bool isImplicit() const
Definition: MachineOperand.h:380
Constants.h
llvm::HexagonII::MO_IEGOT
@ MO_IEGOT
Definition: HexagonBaseInfo.h:207
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
HexagonMCInstrInfo.h
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::MCSymbolRefExpr::VK_Hexagon_LO16
@ VK_Hexagon_LO16
Definition: MCExpr.h:317
MCContext.h
llvm::HexagonMCExpr::create
static HexagonMCExpr * create(MCExpr const *Expr, MCContext &Ctx)
Definition: HexagonMCExpr.cpp:22
llvm::MCContext::createMCInst
MCInst * createMCInst()
Create and return a new MC instruction.
Definition: MCContext.cpp:179
MCInst.h
HexagonMCExpr.h
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
APFloat.h
This file declares a class to represent arbitrary precision floating point values and provide a varie...
llvm::APFloat::bitcastToAPInt
APInt bitcastToAPInt() const
Definition: APFloat.h:1132
Hexagon.h
llvm::MCOperand::createInst
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:168
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:209
llvm::Pass::print
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:125
llvm::APFloat
Definition: APFloat.h:701
llvm::MCSymbolRefExpr::VK_Hexagon_HI16
@ VK_Hexagon_HI16
Definition: MCExpr.h:318
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3062
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::HexagonII::MO_PCREL
@ MO_PCREL
MO_PCREL - On a symbol operand, indicates a PC-relative relocation Used for computing a global addres...
Definition: HexagonBaseInfo.h:181
llvm::APInt::getRawData
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:694
llvm::HexagonAsmPrinter::HexagonProcessInstruction
void HexagonProcessInstruction(MCInst &Inst, const MachineInstr &MBB)
Definition: HexagonAsmPrinter.cpp:265
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3053
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::HexagonII::MO_TPREL
@ MO_TPREL
Definition: HexagonBaseInfo.h:211
llvm::MCSymbolRefExpr::VK_PCREL
@ VK_PCREL
Definition: MCExpr.h:201
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:219
llvm::MachineOperand::getFPImm
const ConstantFP * getFPImm() const
Definition: MachineOperand.h:547
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:478
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:92
llvm::MachineOperand::MO_FPImmediate
@ MO_FPImmediate
Floating-point immediate operand.
Definition: MachineOperand.h:55
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:360
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:523
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3109
llvm::MachineOperand::getMBB
MachineBasicBlock * getMBB() const
Definition: MachineOperand.h:552
llvm::HexagonII::HMOTF_ConstExtended
@ HMOTF_ConstExtended
Definition: HexagonBaseInfo.h:216
GetSymbolRef
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, HexagonAsmPrinter &Printer, bool MustExtend)
Definition: HexagonMCInstLower.cpp:41
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3090
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:133
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
llvm::HexagonMCInstrInfo::setOuterLoop
void setOuterLoop(MCInst &MCI)
Definition: HexagonMCInstrInfo.cpp:1002
llvm::MCSymbolRefExpr::VK_TPREL
@ VK_TPREL
Definition: MCExpr.h:355
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:25
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:557
llvm::HexagonII::MO_IE
@ MO_IE
Definition: HexagonBaseInfo.h:203
llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT
@ VK_Hexagon_IE_GOT
Definition: MCExpr.h:325
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:197
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:384
llvm::XCOFF::RelocationType
RelocationType
Definition: XCOFF.h:195
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::HexagonII::MO_GDPLT
@ MO_GDPLT
Definition: HexagonBaseInfo.h:199
llvm::MCSymbolRefExpr::VK_Hexagon_GPREL
@ VK_Hexagon_GPREL
Definition: MCExpr.h:319
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:608
MachineOperand.h
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
raw_ostream.h
llvm::MCSymbolRefExpr::VK_Hexagon_IE
@ VK_Hexagon_IE
Definition: MCExpr.h:324
MCExpr.h
llvm::HexagonII::MO_GPREL
@ MO_GPREL
Definition: HexagonBaseInfo.h:191
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58
llvm::HexagonAsmPrinter
Definition: HexagonAsmPrinter.h:29