LLVM  8.0.1
ARCMCInstLower.cpp
Go to the documentation of this file.
1 //===- ARCMCInstLower.cpp - ARC MachineInstr to MCInst ----------*- C++ -*-===//
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 /// \file
11 /// This file contains code to lower ARC MachineInstrs to their
12 /// corresponding MCInst records.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "ARCMCInstLower.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 
25 using namespace llvm;
26 
28  : Ctx(C), Printer(AsmPrinter) {}
29 
30 MCOperand ARCMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
31  MachineOperandType MOTy,
32  unsigned Offset) const {
34  const MCSymbol *Symbol;
35 
36  switch (MOTy) {
38  Symbol = MO.getMBB()->getSymbol();
39  break;
41  Symbol = Printer.getSymbol(MO.getGlobal());
42  Offset += MO.getOffset();
43  break;
45  Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
46  Offset += MO.getOffset();
47  break;
49  Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
50  Offset += MO.getOffset();
51  break;
53  Symbol = Printer.GetJTISymbol(MO.getIndex());
54  break;
56  Symbol = Printer.GetCPISymbol(MO.getIndex());
57  Offset += MO.getOffset();
58  break;
59  default:
60  llvm_unreachable("<unknown operand type>");
61  }
62 
63  assert(Symbol && "Symbol creation failed.\n");
64  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
65 
66  if (!Offset)
67  return MCOperand::createExpr(MCSym);
68 
69  // Assume offset is never negative.
70  assert(Offset > 0);
71 
72  const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
73  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
74  return MCOperand::createExpr(Add);
75 }
76 
78  unsigned Offset) const {
79  MachineOperandType MOTy = MO.getType();
80 
81  switch (MOTy) {
82  default:
83  llvm_unreachable("unknown operand type");
85  // Ignore all implicit register operands.
86  if (MO.isImplicit())
87  break;
88  return MCOperand::createReg(MO.getReg());
90  return MCOperand::createImm(MO.getImm() + Offset);
97  return LowerSymbolOperand(MO, MOTy, Offset);
99  break;
100  }
101 
102  return {};
103 }
104 
105 void ARCMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
106  OutMI.setOpcode(MI->getOpcode());
107 
108  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
109  const MachineOperand &MO = MI->getOperand(i);
110  MCOperand MCOp = LowerOperand(MO);
111 
112  if (MCOp.isValid())
113  OutMI.addOperand(MCOp);
114  }
115 }
uint64_t CallInst * C
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
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
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
Name of external global symbol.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:63
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
Address of a global value.
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
Address of a basic block.
ARCMCInstLower(MCContext *C, AsmPrinter &asmprinter)
Binary assembler expressions.
Definition: MCExpr.h:417
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.
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.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:430
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
Representation of each machine instruction.
Definition: MachineInstr.h:64
MCOperand LowerOperand(const MachineOperand &MO, unsigned offset=0) const
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.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
bool isValid() const
Definition: MCInst.h:57
Address of indexed Constant in Constant Pool.
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 MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
bool isImplicit() const