LLVM 19.0.0git
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"
21#include "llvm/IR/Function.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#include "llvm/MC/MCStreamer.h"
27#include "llvm/Object/COFF.h"
32using namespace llvm;
33using namespace llvm::object;
34
36
38 : Ctx(ctx), Printer(printer) {}
39
43}
44
46 unsigned TargetFlags) const {
47 const Triple &TheTriple = Printer.TM.getTargetTriple();
48 if (!TheTriple.isOSBinFormatCOFF())
49 return Printer.getSymbolPreferLocal(*GV);
50
51 assert(TheTriple.isOSWindows() &&
52 "Windows is the only supported COFF target");
53
54 bool IsIndirect =
56 if (!IsIndirect) {
57 // For ARM64EC, symbol lookup in the MSVC linker has limited awareness
58 // of ARM64EC mangling ("#"/"$$h"). So object files need to refer to both
59 // the mangled and unmangled names of ARM64EC symbols, even if they aren't
60 // actually used by any relocations. Emit the necessary references here.
61 if (!TheTriple.isWindowsArm64EC() || !isa<Function>(GV) ||
62 !GV->hasExternalLinkage())
63 return Printer.getSymbol(GV);
64
65 StringRef Name = Printer.getSymbol(GV)->getName();
66 // Don't mangle ARM64EC runtime functions.
67 static constexpr StringLiteral ExcludedFns[] = {
68 "__os_arm64x_check_icall_cfg", "__os_arm64x_dispatch_call_no_redirect",
69 "__os_arm64x_check_icall"};
70 if (is_contained(ExcludedFns, Name))
71 return Printer.getSymbol(GV);
72
73 if (std::optional<std::string> MangledName =
75 MCSymbol *MangledSym = Ctx.getOrCreateSymbol(MangledName.value());
76 if (!cast<Function>(GV)->hasMetadata("arm64ec_hasguestexit")) {
77 Printer.OutStreamer->emitSymbolAttribute(Printer.getSymbol(GV),
79 Printer.OutStreamer->emitAssignment(
80 Printer.getSymbol(GV),
82 Ctx));
83 Printer.OutStreamer->emitSymbolAttribute(MangledSym, MCSA_WeakAntiDep);
84 Printer.OutStreamer->emitAssignment(
85 MangledSym,
88 }
89
90 if (TargetFlags & AArch64II::MO_ARM64EC_CALLMANGLE)
91 return MangledSym;
92 }
93
94 return Printer.getSymbol(GV);
95 }
96
98
99 if ((TargetFlags & AArch64II::MO_DLLIMPORT) &&
100 TheTriple.isWindowsArm64EC() &&
101 !(TargetFlags & AArch64II::MO_ARM64EC_CALLMANGLE) &&
102 isa<Function>(GV)) {
103 // __imp_aux is specific to arm64EC; it represents the actual address of
104 // an imported function without any thunks.
105 //
106 // If we see a reference to an "aux" symbol, also emit a reference to the
107 // corresponding non-aux symbol. Otherwise, the Microsoft linker behaves
108 // strangely when linking against x64 import libararies.
109 //
110 // emitSymbolAttribute() doesn't have any real effect here; it just
111 // ensures the symbol name appears in the assembly without any
112 // side-effects. It might make sense to design a cleaner way to express
113 // this.
114 Name = "__imp_";
115 Printer.TM.getNameWithPrefix(Name, GV,
116 Printer.getObjFileLowering().getMangler());
117 MCSymbol *ExtraSym = Ctx.getOrCreateSymbol(Name);
118 Printer.OutStreamer->emitSymbolAttribute(ExtraSym, MCSA_Global);
119
120 Name = "__imp_aux_";
121 } else if (TargetFlags & AArch64II::MO_DLLIMPORT) {
122 Name = "__imp_";
123 } else if (TargetFlags & AArch64II::MO_COFFSTUB) {
124 Name = ".refptr.";
125 }
126 Printer.TM.getNameWithPrefix(Name, GV,
127 Printer.getObjFileLowering().getMangler());
128
129 MCSymbol *MCSym = Ctx.getOrCreateSymbol(Name);
130
131 if (TargetFlags & AArch64II::MO_COFFSTUB) {
132 MachineModuleInfoCOFF &MMICOFF =
135 MMICOFF.getGVStubEntry(MCSym);
136
137 if (!StubSym.getPointer())
138 StubSym = MachineModuleInfoImpl::StubValueTy(Printer.getSymbol(GV), true);
139 }
140
141 return MCSym;
142}
143
144MCSymbol *
146 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
147}
148
150 MCSymbol *Sym) const {
151 // FIXME: We would like an efficient form for this, so we don't have to do a
152 // lot of extra uniquing.
154 if ((MO.getTargetFlags() & AArch64II::MO_GOT) != 0) {
157 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
160 else
161 llvm_unreachable("Unexpected target flags with MO_GOT on GV operand");
162 } else if ((MO.getTargetFlags() & AArch64II::MO_TLS) != 0) {
165 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
168 else
169 llvm_unreachable("Unexpected target flags with MO_TLS on GV operand");
170 } else {
172 RefKind = MCSymbolRefExpr::VK_PAGE;
173 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
176 }
177 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, RefKind, Ctx);
178 if (!MO.isJTI() && MO.getOffset())
180 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
181 return MCOperand::createExpr(Expr);
182}
183
185 MCSymbol *Sym) const {
186 uint32_t RefFlags = 0;
187
189 RefFlags |= AArch64MCExpr::VK_GOT;
190 else if (MO.getTargetFlags() & AArch64II::MO_TLS) {
191 TLSModel::Model Model;
192 if (MO.isGlobal()) {
193 const GlobalValue *GV = MO.getGlobal();
194 Model = Printer.TM.getTLSModel(GV);
196 Model == TLSModel::LocalDynamic)
198
199 } else {
200 assert(MO.isSymbol() &&
201 StringRef(MO.getSymbolName()) == "_TLS_MODULE_BASE_" &&
202 "unexpected external TLS symbol");
203 // The general dynamic access sequence is used to get the
204 // address of _TLS_MODULE_BASE_.
206 }
207 switch (Model) {
209 RefFlags |= AArch64MCExpr::VK_GOTTPREL;
210 break;
212 RefFlags |= AArch64MCExpr::VK_TPREL;
213 break;
215 RefFlags |= AArch64MCExpr::VK_DTPREL;
216 break;
218 RefFlags |= AArch64MCExpr::VK_TLSDESC;
219 break;
220 }
221 } else if (MO.getTargetFlags() & AArch64II::MO_PREL) {
222 RefFlags |= AArch64MCExpr::VK_PREL;
223 } else {
224 // No modifier means this is a generic reference, classified as absolute for
225 // the cases where it matters (:abs_g0: etc).
226 RefFlags |= AArch64MCExpr::VK_ABS;
227 }
228
230 RefFlags |= AArch64MCExpr::VK_PAGE;
231 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
233 RefFlags |= AArch64MCExpr::VK_PAGEOFF;
235 RefFlags |= AArch64MCExpr::VK_G3;
237 RefFlags |= AArch64MCExpr::VK_G2;
239 RefFlags |= AArch64MCExpr::VK_G1;
241 RefFlags |= AArch64MCExpr::VK_G0;
243 RefFlags |= AArch64MCExpr::VK_HI12;
244
246 RefFlags |= AArch64MCExpr::VK_NC;
247
248 const MCExpr *Expr =
250 if (!MO.isJTI() && MO.getOffset())
252 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
253
255 RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
256 Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
257
258 return MCOperand::createExpr(Expr);
259}
260
262 MCSymbol *Sym) const {
263 uint32_t RefFlags = 0;
264
268 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
271
272 } else if (MO.getTargetFlags() & AArch64II::MO_S) {
273 RefFlags |= AArch64MCExpr::VK_SABS;
274 } else {
275 RefFlags |= AArch64MCExpr::VK_ABS;
276
278 RefFlags |= AArch64MCExpr::VK_PAGE;
279 else if ((MO.getTargetFlags() & AArch64II::MO_FRAGMENT) ==
282 }
283
285 RefFlags |= AArch64MCExpr::VK_G3;
287 RefFlags |= AArch64MCExpr::VK_G2;
289 RefFlags |= AArch64MCExpr::VK_G1;
291 RefFlags |= AArch64MCExpr::VK_G0;
292
293 // FIXME: Currently we only set VK_NC for MO_G3/MO_G2/MO_G1/MO_G0. This is
294 // because setting VK_NC for others would mean setting their respective
295 // RefFlags correctly. We should do this in a separate patch.
296 if (MO.getTargetFlags() & AArch64II::MO_NC) {
297 auto MOFrag = (MO.getTargetFlags() & AArch64II::MO_FRAGMENT);
298 if (MOFrag == AArch64II::MO_G3 || MOFrag == AArch64II::MO_G2 ||
299 MOFrag == AArch64II::MO_G1 || MOFrag == AArch64II::MO_G0)
300 RefFlags |= AArch64MCExpr::VK_NC;
301 }
302
303 const MCExpr *Expr =
305 if (!MO.isJTI() && MO.getOffset())
307 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
308
309 auto RefKind = static_cast<AArch64MCExpr::VariantKind>(RefFlags);
311 "Invalid relocation requested");
312 Expr = AArch64MCExpr::create(Expr, RefKind, Ctx);
313
314 return MCOperand::createExpr(Expr);
315}
316
318 MCSymbol *Sym) const {
319 if (Printer.TM.getTargetTriple().isOSBinFormatMachO())
320 return lowerSymbolOperandMachO(MO, Sym);
321 if (Printer.TM.getTargetTriple().isOSBinFormatCOFF())
322 return lowerSymbolOperandCOFF(MO, Sym);
323
324 assert(Printer.TM.getTargetTriple().isOSBinFormatELF() && "Invalid target");
325 return lowerSymbolOperandELF(MO, Sym);
326}
327
329 MCOperand &MCOp) const {
330 switch (MO.getType()) {
331 default:
332 llvm_unreachable("unknown operand type");
334 // Ignore all implicit register operands.
335 if (MO.isImplicit())
336 return false;
337 MCOp = MCOperand::createReg(MO.getReg());
338 break;
340 // Regmasks are like implicit defs.
341 return false;
343 MCOp = MCOperand::createImm(MO.getImm());
344 break;
348 break;
351 break;
354 break;
356 MCOp = LowerSymbolOperand(MO, MO.getMCSymbol());
357 break;
359 MCOp = LowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
360 break;
362 MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
363 break;
365 MCOp = LowerSymbolOperand(
366 MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
367 break;
368 }
369 return true;
370}
371
373 OutMI.setOpcode(MI->getOpcode());
374
375 for (const MachineOperand &MO : MI->operands()) {
376 MCOperand MCOp;
377 if (lowerOperand(MO, MCOp))
378 OutMI.addOperand(MCOp);
379 }
380
381 switch (OutMI.getOpcode()) {
382 case AArch64::CATCHRET:
383 OutMI = MCInst();
384 OutMI.setOpcode(AArch64::RET);
385 OutMI.addOperand(MCOperand::createReg(AArch64::LR));
386 break;
387 case AArch64::CLEANUPRET:
388 OutMI = MCInst();
389 OutMI.setOpcode(AArch64::RET);
390 OutMI.addOperand(MCOperand::createReg(AArch64::LR));
391 break;
392 }
393}
cl::opt< bool > EnableAArch64ELFLocalDynamicTLSGeneration("aarch64-elf-ldtls-generation", cl::Hidden, cl::desc("Allow AArch64 Local Dynamic TLS code generation"), cl::init(false))
cl::opt< bool > EnableAArch64ELFLocalDynamicTLSGeneration
dxil pretty printer
dxil pretty DXIL Metadata Pretty Printer
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const AArch64MCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
MCSymbol * GetExternalSymbolSymbol(const MachineOperand &MO) const
MCOperand lowerSymbolOperandCOFF(const MachineOperand &MO, MCSymbol *Sym) const
MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const
MCSymbol * GetGlobalValueSymbol(const GlobalValue *GV, unsigned TargetFlags) const
MCOperand lowerSymbolOperandELF(const MachineOperand &MO, MCSymbol *Sym) const
AArch64MCInstLower(MCContext &ctx, AsmPrinter &printer)
void Lower(const MachineInstr *MI, MCInst &OutMI) const
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
MCSymbol * GetGlobalAddressSymbol(const MachineOperand &MO) const
MCOperand lowerSymbolOperandMachO(const MachineOperand &MO, MCSymbol *Sym) const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:383
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:89
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:680
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:107
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
bool hasExternalLinkage() const
Definition: GlobalValue.h:511
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
unsigned getOpcode() const
Definition: MCInst.h:198
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineModuleInfoCOFF - This is a MachineModuleInfoImpl implementation for COFF targets.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
Ty & getObjFileInfo()
Keep track of various per-module pieces of information for backends that would like to do so.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isImplicit() const
MachineBasicBlock * getMBB() const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
MCSymbol * getMCSymbol() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
PointerIntPair - This class implements a pair of a pointer and small integer.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:846
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
const Triple & getTargetTriple() const
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:730
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:722
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:622
bool isWindowsArm64EC() const
Definition: Triple.h:638
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:717
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_DLLIMPORT
MO_DLLIMPORT - On a symbol operand, this represents that the reference to the symbol is for an import...
@ MO_NC
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow.
@ MO_G1
MO_G1 - A symbol operand with this flag (granule 1) represents the bits 16-31 of a 64-bit address,...
@ MO_S
MO_S - Indicates that the bits of the symbol operand represented by MO_G0 etc are signed.
@ MO_PAGEOFF
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page.
@ MO_GOT
MO_GOT - This flag indicates that a symbol operand represents the address of the GOT entry for the sy...
@ MO_PREL
MO_PREL - Indicates that the bits of the symbol operand represented by MO_G0 etc are PC relative.
@ MO_G0
MO_G0 - A symbol operand with this flag (granule 0) represents the bits 0-15 of a 64-bit address,...
@ MO_ARM64EC_CALLMANGLE
MO_ARM64EC_CALLMANGLE - Operand refers to the Arm64EC-mangled version of a symbol,...
@ MO_PAGE
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
@ MO_HI12
MO_HI12 - This flag indicates that a symbol operand represents the bits 13-24 of a 64-bit address,...
@ MO_TLS
MO_TLS - Indicates that the operand being accessed is some kind of thread-local symbol.
@ MO_G2
MO_G2 - A symbol operand with this flag (granule 2) represents the bits 32-47 of a 64-bit address,...
@ MO_G3
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address,...
@ MO_COFFSTUB
MO_COFFSTUB - On a symbol operand "FOO", this indicates that the reference is actually to the "....
@ GeneralDynamic
Definition: CodeGen.h:46
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< std::string > getArm64ECMangledFunctionName(StringRef Name)
Definition: Mangler.cpp:293
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_WeakAntiDep
.weak_anti_dep (COFF)
Definition: MCDirectives.h:49