LLVM  10.0.0svn
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
48  MCSymbolRefExpr::VariantKind RelocationType;
49  switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
50  default:
51  RelocationType = MCSymbolRefExpr::VK_None;
52  break;
54  RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
55  break;
56  case HexagonII::MO_GOT:
57  RelocationType = MCSymbolRefExpr::VK_GOT;
58  break;
59  case HexagonII::MO_LO16:
60  RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
61  break;
62  case HexagonII::MO_HI16:
63  RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
64  break;
66  RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
67  break;
69  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
70  break;
72  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
73  break;
74  case HexagonII::MO_IE:
75  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
76  break;
78  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
79  break;
81  RelocationType = MCSymbolRefExpr::VK_TPREL;
82  break;
83  }
84 
85  ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
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 = new (AP.OutContext) MCInst;
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 }
unsigned getTargetFlags() const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineBasicBlock * getMBB() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:331
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
print alias Alias Set Printer
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
const ConstantFP * getFPImm() const
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:414
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
const char * getSymbolName() const
This file implements a class to represent arbitrary precision integral constant values and operations...
Context object for machine code objects.
Definition: MCContext.h:64
void extendIfNeeded(MCContext &Context, MCInstrInfo const &MCII, MCInst &MCB, MCInst const &MCI)
void setMustExtend(MCExpr const &Expr, bool Val=true)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:469
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI, MCInst &MCB, HexagonAsmPrinter &AP)
void HexagonProcessInstruction(MCInst &Inst, const MachineInstr &MBB)
Address of a global value.
static HexagonMCExpr * create(MCExpr const *Expr, MCContext &Ctx)
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)
const GlobalValue * getGlobal() const
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
This file declares a class to represent arbitrary precision floating point values and provide a varie...
Address of a basic block.
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APFloat & getValueAPF() const
Definition: Constants.h:302
void setOpcode(unsigned Op)
Definition: MCInst.h:170
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
MachineOperand class - Representation of each machine instruction operand.
MO_PCREL - On a symbol operand, indicates a PC-relative relocation Used for computing a global addres...
int64_t getImm() const
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:143
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:674
Representation of each machine instruction.
Definition: MachineInstr.h:64
MO_GOT - Indicates a GOT-relative relocation.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Floating-point immediate operand.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
APInt bitcastToAPInt() const
Definition: APFloat.h:1104
Address of indexed Constant in Constant Pool.
Register getReg() const
getReg - Returns the register number.
unsigned getOpcode() const
Definition: MCInst.h:171
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool isImplicit() const