LLVM  4.0.0
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"
19 
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 
27 using namespace llvm;
28 
29 namespace llvm {
30  void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
31  MCInst &MCB, HexagonAsmPrinter &AP);
32 }
33 
35  HexagonAsmPrinter &Printer, bool MustExtend) {
36  MCContext &MC = Printer.OutContext;
37  const MCExpr *ME;
38 
39  // Populate the relocation type based on Hexagon target flags
40  // set on an operand
41  MCSymbolRefExpr::VariantKind RelocationType;
42  switch (MO.getTargetFlags()) {
43  default:
44  RelocationType = MCSymbolRefExpr::VK_None;
45  break;
47  RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
48  break;
49  case HexagonII::MO_GOT:
50  RelocationType = MCSymbolRefExpr::VK_GOT;
51  break;
52  case HexagonII::MO_LO16:
53  RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
54  break;
55  case HexagonII::MO_HI16:
56  RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
57  break;
59  RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
60  break;
62  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
63  break;
65  RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
66  break;
67  case HexagonII::MO_IE:
68  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
69  break;
71  RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
72  break;
74  RelocationType = MCSymbolRefExpr::VK_TPREL;
75  break;
76  }
77 
78  ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
79 
80  if (!MO.isJTI() && MO.getOffset())
82  MC);
83 
84  ME = HexagonMCExpr::create(ME, MC);
85  HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
86  return MCOperand::createExpr(ME);
87 }
88 
89 // Create an MCInst from a MachineInstr
91  MCInst &MCB, HexagonAsmPrinter &AP) {
92  if (MI->getOpcode() == Hexagon::ENDLOOP0) {
94  return;
95  }
96  if (MI->getOpcode() == Hexagon::ENDLOOP1) {
98  return;
99  }
100  MCInst *MCI = new (AP.OutContext) MCInst;
101  MCI->setOpcode(MI->getOpcode());
102  assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
103  "MCI opcode should have been set on construction");
104 
105  for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
106  const MachineOperand &MO = MI->getOperand(i);
107  MCOperand MCO;
108  bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
109 
110  switch (MO.getType()) {
111  default:
112  MI->dump();
113  llvm_unreachable("unknown operand type");
115  // Ignore all implicit register operands.
116  if (MO.isImplicit()) continue;
117  MCO = MCOperand::createReg(MO.getReg());
118  break;
120  APFloat Val = MO.getFPImm()->getValueAPF();
121  // FP immediates are used only when setting GPRs, so they may be dealt
122  // with like regular immediates from this point on.
123  auto Expr = HexagonMCExpr::create(
125  AP.OutContext),
126  AP.OutContext);
127  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
128  MCO = MCOperand::createExpr(Expr);
129  break;
130  }
132  auto Expr = HexagonMCExpr::create(
134  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
135  MCO = MCOperand::createExpr(Expr);
136  break;
137  }
139  MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
140  AP.OutContext);
141  Expr = HexagonMCExpr::create(Expr, AP.OutContext);
142  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
143  MCO = MCOperand::createExpr(Expr);
144  break;
145  }
147  MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
148  break;
151  AP, MustExtend);
152  break;
154  MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
155  break;
157  MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
158  break;
160  MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
161  MustExtend);
162  break;
163  }
164 
165  MCI->addOperand(MCO);
166  }
167  AP.HexagonProcessInstruction(*MCI, *MI);
168  HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
170 }
bool isImplicit() const
const GlobalValue * getGlobal() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:371
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
size_t i
const ConstantFP * getFPImm() const
MachineBasicBlock * getMBB() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:79
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
const char * getSymbolName() const
print alias Alias Set Printer
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:111
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Name of external global symbol.
APInt bitcastToAPInt() const
Definition: APFloat.h:1012
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:277
Context object for machine code objects.
Definition: MCContext.h:51
void extendIfNeeded(MCContext &Context, MCInstrInfo const &MCII, MCInst &MCB, MCInst const &MCI)
void setMustExtend(MCExpr const &Expr, bool Val=true)
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:429
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:273
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI, MCInst &MCB, HexagonAsmPrinter &AP)
void HexagonProcessInstruction(MCInst &Inst, const MachineInstr &MBB)
Address of a global value.
unsigned getTargetFlags() const
static HexagonMCExpr * create(MCExpr const *Expr, MCContext &Ctx)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, HexagonAsmPrinter &Printer, bool MustExtend)
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
Address of a basic block.
int64_t getOffset() const
Return the offset from the symbol in this operand.
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:158
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...
void dump(const TargetInstrInfo *TII=nullptr) const
unsigned getOpcode() const
Definition: MCInst.h:159
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:135
Representation of each machine instruction.
Definition: MachineInstr.h:52
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
MO_GOT - Indicates a GOT-relative relocation.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:578
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
const APFloat & getValueAPF() const
Definition: Constants.h:300
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Floating-point immediate operand.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149