LLVM  5.0.0
AArch64MCInstLower.cpp
Go to the documentation of this file.
1 //==-- AArch64MCInstLower.cpp - Convert AArch64 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 AArch64 MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AArch64MCInstLower.h"
17 #include "Utils/AArch64BaseInfo.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Support/CodeGen.h"
27 using namespace llvm;
28 
30 
32  : Ctx(ctx), Printer(printer) {}
33 
34 MCSymbol *
36  return Printer.getSymbol(MO.getGlobal());
37 }
38 
39 MCSymbol *
41  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
42 }
43 
45  MCSymbol *Sym) const {
46  // FIXME: We would like an efficient form for this, so we don't have to do a
47  // lot of extra uniquing.
49  if ((MO.getTargetFlags() & AArch64II::MO_GOT) != 0) {
52  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
55  else
56  llvm_unreachable("Unexpected target flags with MO_GOT on GV operand");
57  } else if ((MO.getTargetFlags() & AArch64II::MO_TLS) != 0) {
60  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
63  else
64  llvm_unreachable("Unexpected target flags with MO_TLS on GV operand");
65  } else {
67  RefKind = MCSymbolRefExpr::VK_PAGE;
68  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
71  }
72  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
73  if (!MO.isJTI() && MO.getOffset())
75  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
76  return MCOperand::createExpr(Expr);
77 }
78 
80  MCSymbol *Sym) const {
81  uint32_t RefFlags = 0;
82 
84  RefFlags |= AArch64MCExpr::VK_GOT;
85  else if (MO.getTargetFlags() & AArch64II::MO_TLS) {
87  if (MO.isGlobal()) {
88  const GlobalValue *GV = MO.getGlobal();
89  Model = Printer.TM.getTLSModel(GV);
91  Model == TLSModel::LocalDynamic)
93 
94  } else {
95  assert(MO.isSymbol() &&
96  StringRef(MO.getSymbolName()) == "_TLS_MODULE_BASE_" &&
97  "unexpected external TLS symbol");
98  // The general dynamic access sequence is used to get the
99  // address of _TLS_MODULE_BASE_.
100  Model = TLSModel::GeneralDynamic;
101  }
102  switch (Model) {
104  RefFlags |= AArch64MCExpr::VK_GOTTPREL;
105  break;
106  case TLSModel::LocalExec:
107  RefFlags |= AArch64MCExpr::VK_TPREL;
108  break;
110  RefFlags |= AArch64MCExpr::VK_DTPREL;
111  break;
113  RefFlags |= AArch64MCExpr::VK_TLSDESC;
114  break;
115  }
116  } else {
117  // No modifier means this is a generic reference, classified as absolute for
118  // the cases where it matters (:abs_g0: etc).
119  RefFlags |= AArch64MCExpr::VK_ABS;
120  }
121 
123  RefFlags |= AArch64MCExpr::VK_PAGE;
124  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
126  RefFlags |= AArch64MCExpr::VK_PAGEOFF;
128  RefFlags |= AArch64MCExpr::VK_G3;
130  RefFlags |= AArch64MCExpr::VK_G2;
132  RefFlags |= AArch64MCExpr::VK_G1;
134  RefFlags |= AArch64MCExpr::VK_G0;
136  RefFlags |= AArch64MCExpr::VK_HI12;
137 
138  if (MO.getTargetFlags() & AArch64II::MO_NC)
139  RefFlags |= AArch64MCExpr::VK_NC;
140 
141  const MCExpr *Expr =
143  if (!MO.isJTI() && MO.getOffset())
145  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
146 
148  RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
149  Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
150 
151  return MCOperand::createExpr(Expr);
152 }
153 
155  MCSymbol *Sym) const {
157  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
158  if (!MO.isJTI() && MO.getOffset())
160  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
161  return MCOperand::createExpr(Expr);
162 }
163 
165  MCSymbol *Sym) const {
166  if (Printer.TM.getTargetTriple().isOSDarwin())
167  return lowerSymbolOperandDarwin(MO, Sym);
168  if (Printer.TM.getTargetTriple().isOSBinFormatCOFF())
169  return lowerSymbolOperandCOFF(MO, Sym);
170 
171  assert(Printer.TM.getTargetTriple().isOSBinFormatELF() && "Invalid target");
172  return lowerSymbolOperandELF(MO, Sym);
173 }
174 
176  MCOperand &MCOp) const {
177  switch (MO.getType()) {
178  default:
179  llvm_unreachable("unknown operand type");
181  // Ignore all implicit register operands.
182  if (MO.isImplicit())
183  return false;
184  MCOp = MCOperand::createReg(MO.getReg());
185  break;
187  // Regmasks are like implicit defs.
188  return false;
190  MCOp = MCOperand::createImm(MO.getImm());
191  break;
193  MCOp = MCOperand::createExpr(
195  break;
198  break;
201  break;
203  MCOp = LowerSymbolOperand(MO, MO.getMCSymbol());
204  break;
206  MCOp = LowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
207  break;
209  MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
210  break;
212  MCOp = LowerSymbolOperand(
213  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
214  break;
215  }
216  return true;
217 }
218 
219 void AArch64MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
220  OutMI.setOpcode(MI->getOpcode());
221 
222  for (const MachineOperand &MO : MI->operands()) {
223  MCOperand MCOp;
224  if (lowerOperand(MO, MCOp))
225  OutMI.addOperand(MCOp);
226  }
227 }
bool isImplicit() const
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address...
const GlobalValue * getGlobal() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:420
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
MachineBasicBlock * getMBB() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
static const AArch64MCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
MCOperand lowerSymbolOperandCOFF(const MachineOperand &MO, MCSymbol *Sym) const
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
Address of indexed Jump Table for switch.
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MachineBasicBlock reference.
const char * getSymbolName() const
MO_G0 - A symbol operand with this flag (granule 0) represents the bits 0-15 of a 64-bit address...
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:306
print alias Alias Set Printer
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
Mask of preserved registers.
const Triple & getTargetTriple() const
MO_G2 - A symbol operand with this flag (granule 2) represents the bits 32-47 of a 64-bit address...
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Context object for machine code objects.
Definition: MCContext.h:59
MO_GOT - This flag indicates that a symbol operand represents the address of the GOT entry for the sy...
int64_t getImm() const
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
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
Address of a global value.
unsigned getTargetFlags() const
MO_G1 - A symbol operand with this flag (granule 1) represents the bits 16-31 of a 64-bit address...
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:80
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:76
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Address of a basic block.
MCOperand lowerSymbolOperandDarwin(const MachineOperand &MO, MCSymbol *Sym) const
int64_t getOffset() const
Return the offset from the symbol in this operand.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:582
MCOperand lowerSymbolOperandELF(const MachineOperand &MO, MCSymbol *Sym) const
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
MO_HI12 - This flag indicates that a symbol operand represents the bits 13-24 of a 64-bit address...
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
MO_TLS - Indicates that the operand being accessed is some kind of thread-local symbol.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:466
void setOpcode(unsigned Op)
Definition: MCInst.h:167
MachineOperand class - Representation of each machine instruction operand.
MCSymbol reference (for debug/eh info)
AArch64MCInstLower(MCContext &ctx, AsmPrinter &printer)
llvm_unreachable("Unknown RegBank!")
Representation of each machine instruction.
Definition: MachineInstr.h:59
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:577
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page...
MCSymbol * getMCSymbol() const
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const BlockAddress * getBlockAddress() const
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow...
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
cl::opt< bool > EnableAArch64ELFLocalDynamicTLSGeneration
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159