LLVM  9.0.0svn
AArch64MCInstLower.cpp
Go to the documentation of this file.
1 //==-- AArch64MCInstLower.cpp - Convert AArch64 MachineInstr to an MCInst --==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code to lower AArch64 MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64MCInstLower.h"
16 #include "Utils/AArch64BaseInfo.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/Support/CodeGen.h"
29 using namespace llvm;
30 
32 
34  : Ctx(ctx), Printer(printer) {}
35 
36 MCSymbol *
38  const GlobalValue *GV = MO.getGlobal();
39  unsigned TargetFlags = MO.getTargetFlags();
40  const Triple &TheTriple = Printer.TM.getTargetTriple();
41  if (!TheTriple.isOSBinFormatCOFF())
42  return Printer.getSymbol(GV);
43 
44  assert(TheTriple.isOSWindows() &&
45  "Windows is the only supported COFF target");
46 
47  bool IsIndirect = (TargetFlags & (AArch64II::MO_DLLIMPORT | AArch64II::MO_COFFSTUB));
48  if (!IsIndirect)
49  return Printer.getSymbol(GV);
50 
52  if (TargetFlags & AArch64II::MO_DLLIMPORT)
53  Name = "__imp_";
54  else if (TargetFlags & AArch64II::MO_COFFSTUB)
55  Name = ".refptr.";
56  Printer.TM.getNameWithPrefix(Name, GV,
57  Printer.getObjFileLowering().getMangler());
58 
59  MCSymbol *MCSym = Ctx.getOrCreateSymbol(Name);
60 
61  if (TargetFlags & AArch64II::MO_COFFSTUB) {
62  MachineModuleInfoCOFF &MMICOFF =
65  MMICOFF.getGVStubEntry(MCSym);
66 
67  if (!StubSym.getPointer())
68  StubSym = MachineModuleInfoImpl::StubValueTy(Printer.getSymbol(GV), true);
69  }
70 
71  return MCSym;
72 }
73 
74 MCSymbol *
76  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
77 }
78 
80  MCSymbol *Sym) const {
81  // FIXME: We would like an efficient form for this, so we don't have to do a
82  // lot of extra uniquing.
84  if ((MO.getTargetFlags() & AArch64II::MO_GOT) != 0) {
87  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
90  else
91  llvm_unreachable("Unexpected target flags with MO_GOT on GV operand");
92  } else if ((MO.getTargetFlags() & AArch64II::MO_TLS) != 0) {
95  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
98  else
99  llvm_unreachable("Unexpected target flags with MO_TLS on GV operand");
100  } else {
102  RefKind = MCSymbolRefExpr::VK_PAGE;
103  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
105  RefKind = MCSymbolRefExpr::VK_PAGEOFF;
106  }
107  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
108  if (!MO.isJTI() && MO.getOffset())
110  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
111  return MCOperand::createExpr(Expr);
112 }
113 
115  MCSymbol *Sym) const {
116  uint32_t RefFlags = 0;
117 
119  RefFlags |= AArch64MCExpr::VK_GOT;
120  else if (MO.getTargetFlags() & AArch64II::MO_TLS) {
122  if (MO.isGlobal()) {
123  const GlobalValue *GV = MO.getGlobal();
124  Model = Printer.TM.getTLSModel(GV);
125  if (!EnableAArch64ELFLocalDynamicTLSGeneration &&
126  Model == TLSModel::LocalDynamic)
127  Model = TLSModel::GeneralDynamic;
128 
129  } else {
130  assert(MO.isSymbol() &&
131  StringRef(MO.getSymbolName()) == "_TLS_MODULE_BASE_" &&
132  "unexpected external TLS symbol");
133  // The general dynamic access sequence is used to get the
134  // address of _TLS_MODULE_BASE_.
135  Model = TLSModel::GeneralDynamic;
136  }
137  switch (Model) {
139  RefFlags |= AArch64MCExpr::VK_GOTTPREL;
140  break;
141  case TLSModel::LocalExec:
142  RefFlags |= AArch64MCExpr::VK_TPREL;
143  break;
145  RefFlags |= AArch64MCExpr::VK_DTPREL;
146  break;
148  RefFlags |= AArch64MCExpr::VK_TLSDESC;
149  break;
150  }
151  } else {
152  // No modifier means this is a generic reference, classified as absolute for
153  // the cases where it matters (:abs_g0: etc).
154  RefFlags |= AArch64MCExpr::VK_ABS;
155  }
156 
158  RefFlags |= AArch64MCExpr::VK_PAGE;
159  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
161  RefFlags |= AArch64MCExpr::VK_PAGEOFF;
163  RefFlags |= AArch64MCExpr::VK_G3;
165  RefFlags |= AArch64MCExpr::VK_G2;
167  RefFlags |= AArch64MCExpr::VK_G1;
169  RefFlags |= AArch64MCExpr::VK_G0;
171  RefFlags |= AArch64MCExpr::VK_HI12;
172 
173  if (MO.getTargetFlags() & AArch64II::MO_NC)
174  RefFlags |= AArch64MCExpr::VK_NC;
175 
176  const MCExpr *Expr =
178  if (!MO.isJTI() && MO.getOffset())
180  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
181 
183  RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
184  Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
185 
186  return MCOperand::createExpr(Expr);
187 }
188 
190  MCSymbol *Sym) const {
191  uint32_t RefFlags = 0;
192 
193  if (MO.getTargetFlags() & AArch64II::MO_TLS) {
195  RefFlags |= AArch64MCExpr::VK_SECREL_LO12;
196  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
198  RefFlags |= AArch64MCExpr::VK_SECREL_HI12;
199 
200  } else if (MO.getTargetFlags() & AArch64II::MO_S) {
201  RefFlags |= AArch64MCExpr::VK_SABS;
202  } else {
203  RefFlags |= AArch64MCExpr::VK_ABS;
204  }
205 
207  RefFlags |= AArch64MCExpr::VK_G3;
209  RefFlags |= AArch64MCExpr::VK_G2;
211  RefFlags |= AArch64MCExpr::VK_G1;
213  RefFlags |= AArch64MCExpr::VK_G0;
214 
215  // FIXME: Currently we only set VK_NC for MO_G3/MO_G2/MO_G1/MO_G0. This is
216  // because setting VK_NC for others would mean setting their respective
217  // RefFlags correctly. We should do this in a separate patch.
218  if (MO.getTargetFlags() & AArch64II::MO_NC) {
219  auto MOFrag = (MO.getTargetFlags() & AArch64II::MO_FRAGMENT);
220  if (MOFrag == AArch64II::MO_G3 || MOFrag == AArch64II::MO_G2 ||
221  MOFrag == AArch64II::MO_G1 || MOFrag == AArch64II::MO_G0)
222  RefFlags |= AArch64MCExpr::VK_NC;
223  }
224 
225  const MCExpr *Expr =
227  if (!MO.isJTI() && MO.getOffset())
229  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
230 
231  auto RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
232  assert(RefKind != AArch64MCExpr::VK_INVALID &&
233  "Invalid relocation requested");
234  Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
235 
236  return MCOperand::createExpr(Expr);
237 }
238 
240  MCSymbol *Sym) const {
241  if (Printer.TM.getTargetTriple().isOSDarwin())
242  return lowerSymbolOperandDarwin(MO, Sym);
243  if (Printer.TM.getTargetTriple().isOSBinFormatCOFF())
244  return lowerSymbolOperandCOFF(MO, Sym);
245 
246  assert(Printer.TM.getTargetTriple().isOSBinFormatELF() && "Invalid target");
247  return lowerSymbolOperandELF(MO, Sym);
248 }
249 
251  MCOperand &MCOp) const {
252  switch (MO.getType()) {
253  default:
254  llvm_unreachable("unknown operand type");
256  // Ignore all implicit register operands.
257  if (MO.isImplicit())
258  return false;
259  MCOp = MCOperand::createReg(MO.getReg());
260  break;
262  // Regmasks are like implicit defs.
263  return false;
265  MCOp = MCOperand::createImm(MO.getImm());
266  break;
268  MCOp = MCOperand::createExpr(
270  break;
273  break;
276  break;
278  MCOp = LowerSymbolOperand(MO, MO.getMCSymbol());
279  break;
281  MCOp = LowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
282  break;
284  MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
285  break;
287  MCOp = LowerSymbolOperand(
288  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
289  break;
290  }
291  return true;
292 }
293 
294 void AArch64MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
295  OutMI.setOpcode(MI->getOpcode());
296 
297  for (const MachineOperand &MO : MI->operands()) {
298  MCOperand MCOp;
299  if (lowerOperand(MO, MCOp))
300  OutMI.addOperand(MCOp);
301  }
302 
303  switch (OutMI.getOpcode()) {
304  case AArch64::CATCHRET:
305  OutMI = MCInst();
306  OutMI.setOpcode(AArch64::RET);
307  OutMI.addOperand(MCOperand::createReg(AArch64::LR));
308  break;
309  case AArch64::CLEANUPRET:
310  OutMI = MCInst();
311  OutMI.setOpcode(AArch64::RET);
312  OutMI.addOperand(MCOperand::createReg(AArch64::LR));
313  break;
314  }
315 }
unsigned getTargetFlags() const
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:479
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:212
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address...
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:321
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static const AArch64MCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:617
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
unsigned getReg() const
getReg - Returns the register number.
Address of indexed Jump Table for switch.
MCOperand lowerSymbolOperandCOFF(const MachineOperand &MO, MCSymbol *Sym) const
MachineBasicBlock reference.
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:458
print alias Alias Set Printer
Mask of preserved registers.
MO_G2 - A symbol operand with this flag (granule 2) represents the bits 32-47 of a 64-bit address...
CLEANUPRET - Represents a return from a cleanup block funclet.
Definition: ISDOpcodes.h:721
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
MachineModuleInfoCOFF - This is a MachineModuleInfoImpl implementation for COFF targets.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:62
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
MO_GOT - This flag indicates that a symbol operand represents the address of the GOT entry for the sy...
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:459
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:99
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:532
Address of a global value.
MO_G1 - A symbol operand with this flag (granule 1) represents the bits 16-31 of a 64-bit address...
PointerIntPair - This class implements a pair of a pointer and small integer.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:622
const GlobalValue * getGlobal() const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:81
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Address of a basic block.
const Triple & getTargetTriple() const
Ty & getObjFileInfo()
Keep track of various per-function pieces of information for backends that would like to do so...
MO_HI12 - This flag indicates that a symbol operand represents the bits 13-24 of a 64-bit address...
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MO_TLS - Indicates that the operand being accessed is some kind of thread-local symbol.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
void setOpcode(unsigned Op)
Definition: MCInst.h:170
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.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:441
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
MCSymbol reference (for debug/eh info)
MCOperand lowerSymbolOperandELF(const MachineOperand &MO, MCSymbol *Sym) const
StubValueTy & getGVStubEntry(MCSymbol *Sym)
AArch64MCInstLower(MCContext &ctx, AsmPrinter &printer)
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MO_S - Indicates that the bits of the symbol operand represented by MO_G0 etc are signed...
CATCHRET - Represents a return from a catch block funclet.
Definition: ISDOpcodes.h:717
Representation of each machine instruction.
Definition: MachineInstr.h:63
MCOperand lowerSymbolOperandDarwin(const MachineOperand &MO, MCSymbol *Sym) const
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
MO_COFFSTUB - On a symbol operand "FOO", this indicates that the reference is actually to the "...
MO_DLLIMPORT - On a symbol operand, this represents that the reference to the symbol is for an import...
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.
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page...
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * getMCSymbol() const
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow...
Address of indexed Constant in Constant Pool.
unsigned getOpcode() const
Definition: MCInst.h:171
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
cl::opt< bool > EnableAArch64ELFLocalDynamicTLSGeneration
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
bool isImplicit() const