LLVM  10.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 if (MO.getTargetFlags() & AArch64II::MO_PREL) {
152  RefFlags |= AArch64MCExpr::VK_PREL;
153  } else {
154  // No modifier means this is a generic reference, classified as absolute for
155  // the cases where it matters (:abs_g0: etc).
156  RefFlags |= AArch64MCExpr::VK_ABS;
157  }
158 
160  RefFlags |= AArch64MCExpr::VK_PAGE;
161  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
163  RefFlags |= AArch64MCExpr::VK_PAGEOFF;
165  RefFlags |= AArch64MCExpr::VK_G3;
167  RefFlags |= AArch64MCExpr::VK_G2;
169  RefFlags |= AArch64MCExpr::VK_G1;
171  RefFlags |= AArch64MCExpr::VK_G0;
173  RefFlags |= AArch64MCExpr::VK_HI12;
174 
175  if (MO.getTargetFlags() & AArch64II::MO_NC)
176  RefFlags |= AArch64MCExpr::VK_NC;
177 
178  const MCExpr *Expr =
180  if (!MO.isJTI() && MO.getOffset())
182  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
183 
185  RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
186  Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
187 
188  return MCOperand::createExpr(Expr);
189 }
190 
192  MCSymbol *Sym) const {
193  uint32_t RefFlags = 0;
194 
195  if (MO.getTargetFlags() & AArch64II::MO_TLS) {
197  RefFlags |= AArch64MCExpr::VK_SECREL_LO12;
198  else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
200  RefFlags |= AArch64MCExpr::VK_SECREL_HI12;
201 
202  } else if (MO.getTargetFlags() & AArch64II::MO_S) {
203  RefFlags |= AArch64MCExpr::VK_SABS;
204  } else {
205  RefFlags |= AArch64MCExpr::VK_ABS;
206  }
207 
209  RefFlags |= AArch64MCExpr::VK_G3;
211  RefFlags |= AArch64MCExpr::VK_G2;
213  RefFlags |= AArch64MCExpr::VK_G1;
215  RefFlags |= AArch64MCExpr::VK_G0;
216 
217  // FIXME: Currently we only set VK_NC for MO_G3/MO_G2/MO_G1/MO_G0. This is
218  // because setting VK_NC for others would mean setting their respective
219  // RefFlags correctly. We should do this in a separate patch.
220  if (MO.getTargetFlags() & AArch64II::MO_NC) {
221  auto MOFrag = (MO.getTargetFlags() & AArch64II::MO_FRAGMENT);
222  if (MOFrag == AArch64II::MO_G3 || MOFrag == AArch64II::MO_G2 ||
223  MOFrag == AArch64II::MO_G1 || MOFrag == AArch64II::MO_G0)
224  RefFlags |= AArch64MCExpr::VK_NC;
225  }
226 
227  const MCExpr *Expr =
229  if (!MO.isJTI() && MO.getOffset())
231  Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
232 
233  auto RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
234  assert(RefKind != AArch64MCExpr::VK_INVALID &&
235  "Invalid relocation requested");
236  Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
237 
238  return MCOperand::createExpr(Expr);
239 }
240 
242  MCSymbol *Sym) const {
243  if (Printer.TM.getTargetTriple().isOSDarwin())
244  return lowerSymbolOperandDarwin(MO, Sym);
245  if (Printer.TM.getTargetTriple().isOSBinFormatCOFF())
246  return lowerSymbolOperandCOFF(MO, Sym);
247 
248  assert(Printer.TM.getTargetTriple().isOSBinFormatELF() && "Invalid target");
249  return lowerSymbolOperandELF(MO, Sym);
250 }
251 
253  MCOperand &MCOp) const {
254  switch (MO.getType()) {
255  default:
256  llvm_unreachable("unknown operand type");
258  // Ignore all implicit register operands.
259  if (MO.isImplicit())
260  return false;
261  MCOp = MCOperand::createReg(MO.getReg());
262  break;
264  // Regmasks are like implicit defs.
265  return false;
267  MCOp = MCOperand::createImm(MO.getImm());
268  break;
270  MCOp = MCOperand::createExpr(
272  break;
275  break;
278  break;
280  MCOp = LowerSymbolOperand(MO, MO.getMCSymbol());
281  break;
283  MCOp = LowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
284  break;
286  MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
287  break;
289  MCOp = LowerSymbolOperand(
290  MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
291  break;
292  }
293  return true;
294 }
295 
296 void AArch64MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
297  OutMI.setOpcode(MI->getOpcode());
298 
299  for (const MachineOperand &MO : MI->operands()) {
300  MCOperand MCOp;
301  if (lowerOperand(MO, MCOp))
302  OutMI.addOperand(MCOp);
303  }
304 
305  switch (OutMI.getOpcode()) {
306  case AArch64::CATCHRET:
307  OutMI = MCInst();
308  OutMI.setOpcode(AArch64::RET);
309  OutMI.addOperand(MCOperand::createReg(AArch64::LR));
310  break;
311  case AArch64::CLEANUPRET:
312  OutMI = MCInst();
313  OutMI.setOpcode(AArch64::RET);
314  OutMI.addOperand(MCOperand::createReg(AArch64::LR));
315  break;
316  }
317 }
unsigned getTargetFlags() const
MO_PREL - Indicates that the bits of the symbol operand represented by MO_G0 etc are PC relative...
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:481
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:215
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:327
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:623
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
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:477
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:411
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:65
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:465
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
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:538
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:628
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:444
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:64
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:129
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.
Register getReg() const
getReg - Returns the register number.
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
bool isImplicit() const