LLVM  6.0.0svn
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() & ~HexagonII::HMOTF_ConstExtended) {
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->print(errs());
113  llvm_unreachable("unknown operand type");
115  continue;
117  // Ignore all implicit register operands.
118  if (MO.isImplicit())
119  continue;
120  MCO = MCOperand::createReg(MO.getReg());
121  break;
123  APFloat Val = MO.getFPImm()->getValueAPF();
124  // FP immediates are used only when setting GPRs, so they may be dealt
125  // with like regular immediates from this point on.
126  auto Expr = HexagonMCExpr::create(
128  AP.OutContext),
129  AP.OutContext);
130  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
131  MCO = MCOperand::createExpr(Expr);
132  break;
133  }
135  auto Expr = HexagonMCExpr::create(
137  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
138  MCO = MCOperand::createExpr(Expr);
139  break;
140  }
142  MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
143  AP.OutContext);
144  Expr = HexagonMCExpr::create(Expr, AP.OutContext);
145  HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
146  MCO = MCOperand::createExpr(Expr);
147  break;
148  }
150  MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
151  break;
154  AP, MustExtend);
155  break;
157  MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
158  break;
160  MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
161  break;
163  MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
164  MustExtend);
165  break;
166  }
167 
168  MCI->addOperand(MCO);
169  }
170  AP.HexagonProcessInstruction(*MCI, *MI);
171  HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
173 }
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:305
Compute iterated dominance frontiers using a linear time algorithm.
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:88
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
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
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:279
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:59
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:443
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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
Address of a basic block.
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:294
void setOpcode(unsigned Op)
Definition: MCInst.h:167
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:420
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:668
Representation of each machine instruction.
Definition: MachineInstr.h:59
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.
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Floating-point immediate operand.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
APInt bitcastToAPInt() const
Definition: APFloat.h:1094
Address of indexed Constant in Constant Pool.
unsigned getOpcode() const
Definition: MCInst.h:168
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
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:159
bool isImplicit() const