LLVM  8.0.1
HexagonMCInstLower.cpp
Go to the documentation of this file.
1 //===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to lower Hexagon MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "Hexagon.h"
16 #include "HexagonAsmPrinter.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
31 #include <cassert>
32 
33 using namespace llvm;
34 
35 namespace llvm {
36 
37 void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
38  MCInst &MCB, HexagonAsmPrinter &AP);
39 
40 } // end namespace llvm
41 
43  HexagonAsmPrinter &Printer, bool MustExtend) {
44  MCContext &MC = Printer.OutContext;
45  const MCExpr *ME;
46 
47  // Populate the relocation type based on Hexagon target flags
48  // set on an operand
49  MCSymbolRefExpr::VariantKind RelocationType;
50  switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
51  default:
52  RelocationType = MCSymbolRefExpr::VK_None;
53  break;
55  RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
56  break;
57  case HexagonII::MO_GOT:
58  RelocationType = MCSymbolRefExpr::VK_GOT;
59  break;
60  case HexagonII::MO_LO16:
61  RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
62  break;
63  case HexagonII::MO_HI16:
64  RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
65  break;
67  RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
68  break;
70  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
71  break;
73  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
74  break;
75  case HexagonII::MO_IE:
76  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
77  break;
79  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
80  break;
82  RelocationType = MCSymbolRefExpr::VK_TPREL;
83  break;
84  }
85 
86  ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
87 
88  if (!MO.isJTI() && MO.getOffset())
90  MC);
91 
92  ME = HexagonMCExpr::create(ME, MC);
93  HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
94  return MCOperand::createExpr(ME);
95 }
96 
97 // Create an MCInst from a MachineInstr
99  MCInst &MCB, HexagonAsmPrinter &AP) {
100  if (MI->getOpcode() == Hexagon::ENDLOOP0) {
102  return;
103  }
104  if (MI->getOpcode() == Hexagon::ENDLOOP1) {
106  return;
107  }
108  MCInst *MCI = new (AP.OutContext) MCInst;
109  MCI->setOpcode(MI->getOpcode());
110  assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
111  "MCI opcode should have been set on construction");
112 
113  for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
114  const MachineOperand &MO = MI->getOperand(i);
115  MCOperand MCO;
116  bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
117 
118  switch (MO.getType()) {
119  default:
120  MI->print(errs());
121  llvm_unreachable("unknown operand type");
123  continue;
125  // Ignore all implicit register operands.
126  if (MO.isImplicit())
127  continue;
128  MCO = MCOperand::createReg(MO.getReg());
129  break;
131  APFloat Val = MO.getFPImm()->getValueAPF();
132  // FP immediates are used only when setting GPRs, so they may be dealt
133  // with like regular immediates from this point on.
134  auto Expr = HexagonMCExpr::create(
136  AP.OutContext),
137  AP.OutContext);
138  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
139  MCO = MCOperand::createExpr(Expr);
140  break;
141  }
143  auto Expr = HexagonMCExpr::create(
145  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
146  MCO = MCOperand::createExpr(Expr);
147  break;
148  }
150  MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
151  AP.OutContext);
152  Expr = HexagonMCExpr::create(Expr, AP.OutContext);
153  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
154  MCO = MCOperand::createExpr(Expr);
155  break;
156  }
158  MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
159  break;
162  AP, MustExtend);
163  break;
165  MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
166  break;
168  MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
169  break;
171  MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
172  MustExtend);
173  break;
174  }
175 
176  MCI->addOperand(MCO);
177  }
178  AP.HexagonProcessInstruction(*MCI, *MI);
179  HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
181 }
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:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:89
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
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:116
const ConstantFP * getFPImm() const
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
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:63
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:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
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:24
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:303
void setOpcode(unsigned Op)
Definition: MCInst.h:173
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:430
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:144
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:675
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:186
APInt bitcastToAPInt() const
Definition: APFloat.h:1094
Address of indexed Constant in Constant Pool.
unsigned getOpcode() const
Definition: MCInst.h:174
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
bool isImplicit() const