LLVM 24.0.0git
AArch64AsmPrinter.cpp
Go to the documentation of this file.
1//===- AArch64AsmPrinter.cpp - AArch64 LLVM assembly writer ---------------===//
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 a printer that converts from our internal representation
10// of machine-dependent LLVM code to the AArch64 assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AArch64AsmPrinter.h"
15#include "AArch64.h"
16#include "AArch64MCInstLower.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
28#include "llvm/ADT/ScopeExit.h"
31#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/Twine.h"
48#include "llvm/IR/Analysis.h"
49#include "llvm/IR/DataLayout.h"
51#include "llvm/IR/Mangler.h"
52#include "llvm/IR/Module.h"
53#include "llvm/IR/PassManager.h"
54#include "llvm/MC/MCAsmInfo.h"
55#include "llvm/MC/MCContext.h"
56#include "llvm/MC/MCExpr.h"
57#include "llvm/MC/MCInst.h"
61#include "llvm/MC/MCStreamer.h"
62#include "llvm/MC/MCSymbol.h"
63#include "llvm/MC/MCValue.h"
73#include <cassert>
74#include <cstdint>
75#include <map>
76#include <memory>
77
78using namespace llvm;
80
81#define DEBUG_TYPE "AArch64AsmPrinter"
82
83// Doesn't count FPR128 ZCZ instructions which are handled
84// by TableGen pattern matching
85STATISTIC(NumZCZeroingInstrsFPR,
86 "Number of zero-cycle FPR zeroing instructions expanded from "
87 "canonical pseudo instructions");
88
89namespace {
90
91class AArch64AsmPrinter : public AsmPrinter {
92 AArch64MCInstLower MCInstLowering;
93 FaultMaps FM;
94 const AArch64Subtarget *STI;
95 bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;
96 bool PtrauthInitFini = false;
97 bool PtrauthInitFiniAddressDisc = false;
98#ifndef NDEBUG
99 unsigned InstsEmitted;
100#endif
101 bool EnableImportCallOptimization = false;
103 SectionToImportedFunctionCalls;
104 unsigned PAuthIFuncNextUniqueID = 1;
105
106public:
107 static char ID;
108
109 AArch64AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
110 : AsmPrinter(TM, std::move(Streamer), ID),
111 MCInstLowering(OutContext, *this), FM(*this) {}
112
113 StringRef getPassName() const override { return "AArch64 Assembly Printer"; }
114
115 /// Wrapper for MCInstLowering.lowerOperand() for the
116 /// tblgen'erated pseudo lowering.
117 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const {
118 return MCInstLowering.lowerOperand(MO, MCOp);
119 }
120
121 const MCExpr *lowerConstantPtrAuth(const ConstantPtrAuth &CPA) override;
122
123 const MCExpr *lowerBlockAddressConstant(const BlockAddress &BA) override;
124
125 void emitStartOfAsmFile(Module &M) override;
126 void emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
127 ArrayRef<unsigned> JumpTableIndices) override;
128 std::tuple<const MCSymbol *, uint64_t, const MCSymbol *,
130 getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr,
131 const MCSymbol *BranchLabel) const override;
132
133 void emitFunctionEntryLabel() override;
134
135 void emitXXStructor(const DataLayout &DL, const Constant *CV) override;
136
137 void LowerJumpTableDest(MCStreamer &OutStreamer, const MachineInstr &MI);
138
139 void LowerHardenedBRJumpTable(const MachineInstr &MI);
140
141 void LowerMOPS(MCStreamer &OutStreamer, const MachineInstr &MI);
142
143 void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
144 const MachineInstr &MI);
145 void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
146 const MachineInstr &MI);
147 void LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
148 const MachineInstr &MI);
149 void LowerFAULTING_OP(const MachineInstr &MI);
150
151 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI);
152 void LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI);
153 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI);
154 void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, bool Typed);
155
156 typedef std::tuple<unsigned, bool, uint32_t, bool, uint64_t>
157 HwasanMemaccessTuple;
158 std::map<HwasanMemaccessTuple, MCSymbol *> HwasanMemaccessSymbols;
159 void LowerKCFI_CHECK(const MachineInstr &MI);
160 void LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI);
161 void emitHwasanMemaccessSymbols(Module &M);
162
163 void emitSled(const MachineInstr &MI, SledKind Kind);
164
165 // Returns whether Reg may be used to store sensitive temporary values when
166 // expanding PtrAuth pseudos. Some OSes may take extra care to protect a
167 // small subset of GPRs on context switches - use these registers then.
168 //
169 // If there are no preferred registers, returns true for any Reg.
170 bool isPtrauthRegSafe(Register Reg) const {
171 if (STI->isX16X17Safer())
172 return Reg == AArch64::X16 || Reg == AArch64::X17;
173
174 return true;
175 }
176
177 // Emit the sequence for BRA/BLRA (authenticate + branch/call).
178 void emitPtrauthBranch(const MachineInstr *MI);
179
180 void emitPtrauthCheckAuthenticatedValue(Register TestedReg,
181 Register ScratchReg,
184 const MCSymbol *OnFailure = nullptr);
185
186 // Check authenticated LR before tail calling.
187 void emitPtrauthTailCallHardening(const MachineInstr *TC);
188
189 struct PtrAuthSchema {
190 static PtrAuthSchema CreateImmReg(AArch64PACKey::ID Key, uint64_t IntDisc,
191 const MachineOperand &AddrDiscOp);
192 static PtrAuthSchema CreateRegReg(AArch64PACKey::ID Key, Register AddrDisc,
193 Register PCDisc);
194
196 uint64_t IntDisc;
197 Register AddrDisc;
198 bool AddrDiscIsKilled;
199 Register PCDisc;
200
201 bool addrDiscIsKilledAndNoneOf(std::initializer_list<Register> Regs) {
202 return AddrDiscIsKilled && !llvm::is_contained(Regs, AddrDisc);
203 }
204 };
205
206 // Helper for emitting AUTRELLOADPAC: increment Pointer by Addend and then by
207 // a 32-bit signed value loaded from memory. The instructions emitted are
208 //
209 // ldrsw Scratch, [Pointer, #Addend]!
210 // add Pointer, Pointer, Scratch
211 //
212 // for small Addend value, with longer sequences required for wider Addend.
213 void emitPtrauthApplyIndirectAddend(Register Pointer, Register Scratch,
214 int64_t Addend);
215
216 // Emit the sequence for AUT or AUTPAC (or their PC-blending variants).
217 // Addend is only used for AUTRELLOADPAC.
218 void emitPtrauthAuthResign(Register Pointer, Register Scratch,
219 PtrAuthSchema AuthSchema,
220 std::optional<PtrAuthSchema> SignSchema,
221 std::optional<int64_t> Addend, Value *DS);
222
223 // Emit R_AARCH64_PATCHINST, the deactivation symbol relocation. Returns true
224 // if no instruction should be emitted because the deactivation symbol is
225 // defined in the current module so this function emitted a NOP instead.
226 bool emitDeactivationSymbolRelocation(Value *DS);
227
228 // Emit the sequence for PAC.
229 void emitPtrauthSign(const MachineInstr *MI);
230
231 // Emit the sequence to compute the discriminator.
232 //
233 // The Scratch register passed to this function must be safe, as returned by
234 // isPtrauthRegSafe(ScratchReg).
235 //
236 // The returned register is either ScratchReg, AddrDisc, or XZR. Furthermore,
237 // it is guaranteed to be safe (or XZR), with the only exception of
238 // passing-through an *unmodified* unsafe AddrDisc register.
239 //
240 // If the expanded pseudo is allowed to clobber AddrDisc register, setting
241 // MayClobberAddrDisc may save one MOV instruction, provided
242 // isPtrauthRegSafe(AddrDisc) is true:
243 //
244 // mov x17, x16
245 // movk x17, #1234, lsl #48
246 // ; x16 is not used anymore
247 //
248 // can be replaced by
249 //
250 // movk x16, #1234, lsl #48
251 Register emitPtrauthDiscriminator(uint64_t Disc, Register AddrDisc,
252 Register ScratchReg,
253 bool MayClobberAddrDisc = false);
254
255 // Emit the sequence for LOADauthptrstatic
256 void LowerLOADauthptrstatic(const MachineInstr &MI);
257
258 // Emit the sequence for LOADgotPAC/MOVaddrPAC (either GOT adrp-ldr or
259 // adrp-add followed by PAC sign)
260 void LowerMOVaddrPAC(const MachineInstr &MI);
261
262 // Emit the sequence for LOADgotAUTH (load signed pointer from signed ELF GOT
263 // and authenticate it with, if FPAC bit is not set, check+trap sequence after
264 // authenticating)
265 void LowerLOADgotAUTH(const MachineInstr &MI);
266
267 void emitAddImm(MCRegister Val, int64_t Addend, MCRegister Tmp);
268 void emitAddress(MCRegister Reg, const MCExpr *Expr, MCRegister Tmp,
269 bool DSOLocal, const MCSubtargetInfo &STI);
270
271 const MCExpr *emitPAuthRelocationAsIRelative(
272 const MCExpr *Target, uint64_t Disc, AArch64PACKey::ID KeyID,
273 bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr);
274
275 /// tblgen'erated driver function for lowering simple MI->MC
276 /// pseudo instructions.
277 bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
278
279 // Emit Build Attributes
280 void emitAttributes(unsigned Flags, uint64_t PAuthABIPlatform,
281 uint64_t PAuthABIVersion, AArch64TargetStreamer *TS);
282
283 // Emit expansion of Compare-and-branch pseudo instructions
284 void emitCBPseudoExpansion(const MachineInstr *MI);
285
286 // Emit expansion of atomic store with hint pseudo instructions
287 void emitAtomicHintPseudoExpansion(const MachineInstr *MI);
288 void emitAtomicHintPseudoExpansionRO(const MachineInstr *MI);
289 void emitAtomicHintPseudoExpansionImm(const MachineInstr *MI);
290
291 // Emit expansion of atomic fetch with hint pseudo instructions
292 void emitAtomicFetchHintPseudoExpansion(const MachineInstr *MI);
293
294 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
295 void EmitToStreamer(const MCInst &Inst) {
296 EmitToStreamer(*OutStreamer, Inst);
297 }
298
299 void emitInstruction(const MachineInstr *MI) override;
300
301 void emitFunctionHeaderComment() override;
302
303 void getAnalysisUsage(AnalysisUsage &AU) const override {
305 AU.setPreservesAll();
306 }
307
308 bool runOnMachineFunction(MachineFunction &MF) override {
309 if (auto *PSIW = getAnalysisIfAvailable<ProfileSummaryInfoWrapperPass>())
310 PSI = &PSIW->getPSI();
311 if (auto *SDPIW =
312 getAnalysisIfAvailable<StaticDataProfileInfoWrapperPass>())
313 SDPI = &SDPIW->getStaticDataProfileInfo();
314
315 AArch64FI = MF.getInfo<AArch64FunctionInfo>();
316 STI = &MF.getSubtarget<AArch64Subtarget>();
317
318 SetupMachineFunction(MF);
319
320 if (STI->isTargetCOFF()) {
321 bool Local = MF.getFunction().hasLocalLinkage();
324 int Type =
326
327 OutStreamer->beginCOFFSymbolDef(CurrentFnSym);
328 OutStreamer->emitCOFFSymbolStorageClass(Scl);
329 OutStreamer->emitCOFFSymbolType(Type);
330 OutStreamer->endCOFFSymbolDef();
331 }
332
333 // Emit the rest of the function body.
334 emitFunctionBody();
335
336 // Emit the XRay table for this function.
337 emitXRayTable();
338
339 // We didn't modify anything.
340 return false;
341 }
342
343 const MCExpr *lowerConstant(const Constant *CV,
344 const Constant *BaseCV = nullptr,
345 uint64_t Offset = 0) override;
346
347private:
348 void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O);
349 bool printAsmMRegister(const MachineOperand &MO, char Mode, raw_ostream &O);
350 bool printAsmRegInClass(const MachineOperand &MO,
351 const TargetRegisterClass *RC, unsigned AltName,
352 raw_ostream &O);
353
354 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
355 const char *ExtraCode, raw_ostream &O) override;
356 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
357 const char *ExtraCode, raw_ostream &O) override;
358
359 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
360
361 void emitFunctionBodyEnd() override;
362 void emitBasicBlockEnd(const MachineBasicBlock &MBB) override;
363 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
364
365 MCSymbol *GetCPISymbol(unsigned CPID) const override;
366 void emitEndOfAsmFile(Module &M) override;
367
368 AArch64FunctionInfo *AArch64FI = nullptr;
369
370 /// Emit the LOHs contained in AArch64FI.
371 void emitLOHs();
372
373 void emitMovXReg(Register Dest, Register Src);
374 void emitMOVZ(Register Dest, uint64_t Imm, unsigned Shift);
375 void emitMOVK(Register Dest, uint64_t Imm, unsigned Shift);
376
377 void emitAUT(AArch64PACKey::ID Key, Register Pointer, Register Disc);
378 void emitPAC(AArch64PACKey::ID Key, Register Pointer, Register Disc);
379 void emitBLRA(bool IsCall, AArch64PACKey::ID Key, Register Target,
380 Register Disc);
381
382 /// Emit instruction to set float register to zero.
383 void emitFMov0(const MachineInstr &MI);
384 void emitFMov0AsFMov(const MachineInstr &MI, Register DestReg);
385
386 using MInstToMCSymbol = std::map<const MachineInstr *, MCSymbol *>;
387
388 MInstToMCSymbol LOHInstToLabel;
389
390 bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const override {
391 return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;
392 }
393
394 const MCSubtargetInfo *getIFuncMCSubtargetInfo() const override {
395 assert(STI);
396 return STI;
397 }
398 void emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI,
399 MCSymbol *LazyPointer) override;
400 void emitMachOIFuncStubHelperBody(Module &M, const GlobalIFunc &GI,
401 MCSymbol *LazyPointer) override;
402
403 /// Checks if this instruction is part of a sequence that is eligle for import
404 /// call optimization and, if so, records it to be emitted in the import call
405 /// section.
406 void recordIfImportCall(const MachineInstr *BranchInst);
407};
408
409} // end anonymous namespace
410
411// Get boolean module flag (0 or 1), treating absent flag as having value 0.
413 Metadata *Flag = M.getModuleFlag(Name);
414 if (!Flag)
415 return false;
416
417 uint64_t Value = mdconst::extract<ConstantInt>(Flag)->getZExtValue();
418 assert((Value == 0 || Value == 1) && "Boolean flag is expected, if present");
419 return Value;
420}
421
422void AArch64AsmPrinter::emitStartOfAsmFile(Module &M) {
423 const Triple &TT = M.getTargetTriple();
424
425 if (TT.isOSBinFormatCOFF()) {
426 emitCOFFFeatureSymbol(M);
427 emitCOFFReplaceableFunctionData(M);
428
429 if (M.getModuleFlag("import-call-optimization"))
430 EnableImportCallOptimization = true;
431 }
432
433 PtrauthInitFini = getOptionalBooleanModuleFlag(M, "ptrauth-init-fini");
434 PtrauthInitFiniAddressDisc = getOptionalBooleanModuleFlag(
435 M, "ptrauth-init-fini-address-discrimination");
436
437 if (!TT.isOSBinFormatELF())
438 return;
439
440 // For emitting build attributes and .note.gnu.property section
441 auto *TS =
442 static_cast<AArch64TargetStreamer *>(OutStreamer->getTargetStreamer());
443 // Assemble feature flags that may require creation of build attributes and a
444 // note section.
445 unsigned BAFlags = 0;
446 unsigned GNUFlags = 0;
447 if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
448 M.getModuleFlag("branch-target-enforcement"))) {
449 if (!BTE->isZero()) {
450 BAFlags |= AArch64BuildAttributes::FeatureAndBitsFlag::Feature_BTI_Flag;
452 }
453 }
454
455 if (const auto *GCS = mdconst::extract_or_null<ConstantInt>(
456 M.getModuleFlag("guarded-control-stack"))) {
457 if (!GCS->isZero()) {
458 BAFlags |= AArch64BuildAttributes::FeatureAndBitsFlag::Feature_GCS_Flag;
460 }
461 }
462
464 M.getModuleFlag("sign-return-address"))) {
465 if (!Sign->isZero()) {
466 BAFlags |= AArch64BuildAttributes::FeatureAndBitsFlag::Feature_PAC_Flag;
468 }
469 }
470
471 uint64_t PAuthABIPlatform = -1;
472 if (const auto *PAP = mdconst::extract_or_null<ConstantInt>(
473 M.getModuleFlag("aarch64-elf-pauthabi-platform"))) {
474 PAuthABIPlatform = PAP->getZExtValue();
475 }
476
477 uint64_t PAuthABIVersion = -1;
478 if (const auto *PAV = mdconst::extract_or_null<ConstantInt>(
479 M.getModuleFlag("aarch64-elf-pauthabi-version"))) {
480 PAuthABIVersion = PAV->getZExtValue();
481 }
482
483 // For LLVM_LINUX experimental platform, version value of 0 means no PAuth
484 // support. Do not emit corresponding PAuthABI GNU property note and AArch64
485 // build attributes for this case to keep Linux binaries not using PAuth
486 // unaffected.
487 if (PAuthABIPlatform == ELF::AARCH64_PAUTH_PLATFORM_LLVM_LINUX &&
488 PAuthABIVersion == 0) {
489 PAuthABIPlatform = uint64_t(-1);
490 PAuthABIVersion = uint64_t(-1);
491 }
492
493 // Emit AArch64 Build Attributes
494 emitAttributes(BAFlags, PAuthABIPlatform, PAuthABIVersion, TS);
495 // Emit a .note.gnu.property section with the flags.
496 TS->emitNoteSection(GNUFlags, PAuthABIPlatform, PAuthABIVersion);
497}
498
499void AArch64AsmPrinter::emitFunctionHeaderComment() {
500 const AArch64FunctionInfo *FI = MF->getInfo<AArch64FunctionInfo>();
501 std::optional<std::string> OutlinerString = FI->getOutliningStyle();
502 if (OutlinerString != std::nullopt)
503 OutStreamer->getCommentOS() << ' ' << OutlinerString;
504}
505
506void AArch64AsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI)
507{
508 const Function &F = MF->getFunction();
509 if (F.hasFnAttribute("patchable-function-entry")) {
510 unsigned Num;
511 if (F.getFnAttribute("patchable-function-entry")
512 .getValueAsString()
513 .getAsInteger(10, Num))
514 return;
515 emitNops(Num);
516 return;
517 }
518
519 emitSled(MI, SledKind::FUNCTION_ENTER);
520}
521
522void AArch64AsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
523 emitSled(MI, SledKind::FUNCTION_EXIT);
524}
525
526void AArch64AsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
527 emitSled(MI, SledKind::TAIL_CALL);
528}
529
530void AArch64AsmPrinter::emitSled(const MachineInstr &MI, SledKind Kind) {
531 static const int8_t NoopsInSledCount = 7;
532 // We want to emit the following pattern:
533 //
534 // .Lxray_sled_N:
535 // ALIGN
536 // B #32
537 // ; 7 NOP instructions (28 bytes)
538 // .tmpN
539 //
540 // We need the 28 bytes (7 instructions) because at runtime, we'd be patching
541 // over the full 32 bytes (8 instructions) with the following pattern:
542 //
543 // STP X0, X30, [SP, #-16]! ; push X0 and the link register to the stack
544 // LDR W17, #12 ; W17 := function ID
545 // LDR X16,#12 ; X16 := addr of __xray_FunctionEntry or __xray_FunctionExit
546 // BLR X16 ; call the tracing trampoline
547 // ;DATA: 32 bits of function ID
548 // ;DATA: lower 32 bits of the address of the trampoline
549 // ;DATA: higher 32 bits of the address of the trampoline
550 // LDP X0, X30, [SP], #16 ; pop X0 and the link register from the stack
551 //
552 OutStreamer->emitCodeAlignment(Align(4), getSubtargetInfo());
553 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
554 OutStreamer->emitLabel(CurSled);
555 auto Target = OutContext.createTempSymbol();
556
557 // Emit "B #32" instruction, which jumps over the next 28 bytes.
558 // The operand has to be the number of 4-byte instructions to jump over,
559 // including the current instruction.
560 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::B).addImm(8));
561
562 for (int8_t I = 0; I < NoopsInSledCount; I++)
563 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::NOP));
564
565 OutStreamer->emitLabel(Target);
566 recordSled(CurSled, MI, Kind, 2);
567}
568
569void AArch64AsmPrinter::emitAttributes(unsigned Flags,
570 uint64_t PAuthABIPlatform,
571 uint64_t PAuthABIVersion,
572 AArch64TargetStreamer *TS) {
573
574 PAuthABIPlatform = (uint64_t(-1) == PAuthABIPlatform) ? 0 : PAuthABIPlatform;
575 PAuthABIVersion = (uint64_t(-1) == PAuthABIVersion) ? 0 : PAuthABIVersion;
576
577 if (PAuthABIPlatform || PAuthABIVersion) {
581 AArch64BuildAttributes::SubsectionOptional::REQUIRED,
582 AArch64BuildAttributes::SubsectionType::ULEB128);
586 PAuthABIPlatform, "");
590 "");
591 }
592
593 unsigned BTIValue =
595 unsigned PACValue =
597 unsigned GCSValue =
599
600 if (BTIValue || PACValue || GCSValue) {
604 AArch64BuildAttributes::SubsectionOptional::OPTIONAL,
605 AArch64BuildAttributes::SubsectionType::ULEB128);
615 }
616}
617
618// Emit the following code for Intrinsic::{xray_customevent,xray_typedevent}
619// (built-in functions __xray_customevent/__xray_typedevent).
620//
621// .Lxray_event_sled_N:
622// b 1f
623// save x0 and x1 (and also x2 for TYPED_EVENT_CALL)
624// set up x0 and x1 (and also x2 for TYPED_EVENT_CALL)
625// bl __xray_CustomEvent or __xray_TypedEvent
626// restore x0 and x1 (and also x2 for TYPED_EVENT_CALL)
627// 1:
628//
629// There are 6 instructions for EVENT_CALL and 9 for TYPED_EVENT_CALL.
630//
631// Then record a sled of kind CUSTOM_EVENT or TYPED_EVENT.
632// After patching, b .+N will become a nop.
633void AArch64AsmPrinter::LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI,
634 bool Typed) {
635 auto &O = *OutStreamer;
636 MCSymbol *CurSled = OutContext.createTempSymbol("xray_sled_", true);
637 O.emitLabel(CurSled);
638 bool MachO = MMI->getModule()->getTargetTriple().isOSBinFormatMachO();
639 auto *Sym = MCSymbolRefExpr::create(
640 OutContext.getOrCreateSymbol(
641 Twine(MachO ? "_" : "") +
642 (Typed ? "__xray_TypedEvent" : "__xray_CustomEvent")),
643 OutContext);
644 if (Typed) {
645 O.AddComment("Begin XRay typed event");
646 EmitToStreamer(O, MCInstBuilder(AArch64::B).addImm(9));
647 EmitToStreamer(O, MCInstBuilder(AArch64::STPXpre)
648 .addReg(AArch64::SP)
649 .addReg(AArch64::X0)
650 .addReg(AArch64::X1)
651 .addReg(AArch64::SP)
652 .addImm(-4));
653 EmitToStreamer(O, MCInstBuilder(AArch64::STRXui)
654 .addReg(AArch64::X2)
655 .addReg(AArch64::SP)
656 .addImm(2));
657 emitMovXReg(AArch64::X0, MI.getOperand(0).getReg());
658 emitMovXReg(AArch64::X1, MI.getOperand(1).getReg());
659 emitMovXReg(AArch64::X2, MI.getOperand(2).getReg());
660 EmitToStreamer(O, MCInstBuilder(AArch64::BL).addExpr(Sym));
661 EmitToStreamer(O, MCInstBuilder(AArch64::LDRXui)
662 .addReg(AArch64::X2)
663 .addReg(AArch64::SP)
664 .addImm(2));
665 O.AddComment("End XRay typed event");
666 EmitToStreamer(O, MCInstBuilder(AArch64::LDPXpost)
667 .addReg(AArch64::SP)
668 .addReg(AArch64::X0)
669 .addReg(AArch64::X1)
670 .addReg(AArch64::SP)
671 .addImm(4));
672
673 recordSled(CurSled, MI, SledKind::TYPED_EVENT, 2);
674 } else {
675 O.AddComment("Begin XRay custom event");
676 EmitToStreamer(O, MCInstBuilder(AArch64::B).addImm(6));
677 EmitToStreamer(O, MCInstBuilder(AArch64::STPXpre)
678 .addReg(AArch64::SP)
679 .addReg(AArch64::X0)
680 .addReg(AArch64::X1)
681 .addReg(AArch64::SP)
682 .addImm(-2));
683 emitMovXReg(AArch64::X0, MI.getOperand(0).getReg());
684 emitMovXReg(AArch64::X1, MI.getOperand(1).getReg());
685 EmitToStreamer(O, MCInstBuilder(AArch64::BL).addExpr(Sym));
686 O.AddComment("End XRay custom event");
687 EmitToStreamer(O, MCInstBuilder(AArch64::LDPXpost)
688 .addReg(AArch64::SP)
689 .addReg(AArch64::X0)
690 .addReg(AArch64::X1)
691 .addReg(AArch64::SP)
692 .addImm(2));
693
694 recordSled(CurSled, MI, SledKind::CUSTOM_EVENT, 2);
695 }
696}
697
698void AArch64AsmPrinter::LowerKCFI_CHECK(const MachineInstr &MI) {
699 Register AddrReg = MI.getOperand(0).getReg();
700 assert(std::next(MI.getIterator())->isCall() &&
701 "KCFI_CHECK not followed by a call instruction");
702 assert(std::next(MI.getIterator())->getOperand(0).getReg() == AddrReg &&
703 "KCFI_CHECK call target doesn't match call operand");
704
705 // Default to using the intra-procedure-call temporary registers for
706 // comparing the hashes.
707 unsigned ScratchRegs[] = {AArch64::W16, AArch64::W17};
708 if (AddrReg == AArch64::XZR) {
709 // Checking XZR makes no sense. Instead of emitting a load, zero
710 // ScratchRegs[0] and use it for the ESR AddrIndex below.
711 AddrReg = getXRegFromWReg(ScratchRegs[0]);
712 emitMovXReg(AddrReg, AArch64::XZR);
713 } else {
714 // If one of the scratch registers is used for the call target (e.g.
715 // with AArch64::TCRETURNriBTI), we can clobber another caller-saved
716 // temporary register instead (in this case, AArch64::W9) as the check
717 // is immediately followed by the call instruction.
718 for (auto &Reg : ScratchRegs) {
719 if (Reg == getWRegFromXReg(AddrReg)) {
720 Reg = AArch64::W9;
721 break;
722 }
723 }
724 assert(ScratchRegs[0] != AddrReg && ScratchRegs[1] != AddrReg &&
725 "Invalid scratch registers for KCFI_CHECK");
726
727 // Adjust the offset for patchable-function-prefix. This assumes that
728 // patchable-function-prefix is the same for all functions.
729 int64_t PrefixNops =
730 MI.getMF()->getFunction().getFnAttributeAsParsedInteger(
731 "patchable-function-prefix");
732
733 // Load the target function type hash.
734 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::LDURWi)
735 .addReg(ScratchRegs[0])
736 .addReg(AddrReg)
737 .addImm(-(PrefixNops * 4 + 4)));
738 }
739
740 // Load the expected type hash.
741 const int64_t Type = MI.getOperand(1).getImm();
742 emitMOVK(ScratchRegs[1], Type & 0xFFFF, 0);
743 emitMOVK(ScratchRegs[1], (Type >> 16) & 0xFFFF, 16);
744
745 // Compare the hashes and trap if there's a mismatch.
746 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::SUBSWrs)
747 .addReg(AArch64::WZR)
748 .addReg(ScratchRegs[0])
749 .addReg(ScratchRegs[1])
750 .addImm(0));
751
752 MCSymbol *Pass = OutContext.createTempSymbol();
753 EmitToStreamer(*OutStreamer,
754 MCInstBuilder(AArch64::Bcc)
755 .addImm(AArch64CC::EQ)
756 .addExpr(MCSymbolRefExpr::create(Pass, OutContext)));
757
758 // The base ESR is 0x8000 and the register information is encoded in bits
759 // 0-9 as follows:
760 // - 0-4: n, where the register Xn contains the target address
761 // - 5-9: m, where the register Wm contains the expected type hash
762 // Where n, m are in [0, 30].
763 unsigned TypeIndex = ScratchRegs[1] - AArch64::W0;
764 unsigned AddrIndex;
765 switch (AddrReg) {
766 default:
767 AddrIndex = AddrReg - AArch64::X0;
768 break;
769 case AArch64::FP:
770 AddrIndex = 29;
771 break;
772 case AArch64::LR:
773 AddrIndex = 30;
774 break;
775 }
776
777 assert(AddrIndex < 31 && TypeIndex < 31);
778
779 unsigned ESR = 0x8000 | ((TypeIndex & 31) << 5) | (AddrIndex & 31);
780 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::BRK).addImm(ESR));
781 OutStreamer->emitLabel(Pass);
782}
783
784void AArch64AsmPrinter::LowerHWASAN_CHECK_MEMACCESS(const MachineInstr &MI) {
785 Register Reg = MI.getOperand(0).getReg();
786
787 // The HWASan pass won't emit a CHECK_MEMACCESS intrinsic with a pointer
788 // statically known to be zero. However, conceivably, the HWASan pass may
789 // encounter a "cannot currently statically prove to be null" pointer (and is
790 // therefore unable to omit the intrinsic) that later optimization passes
791 // convert into a statically known-null pointer.
792 if (Reg == AArch64::XZR)
793 return;
794
795 bool IsShort =
796 ((MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES) ||
797 (MI.getOpcode() ==
798 AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW));
799 uint32_t AccessInfo = MI.getOperand(1).getImm();
800 bool IsFixedShadow =
801 ((MI.getOpcode() == AArch64::HWASAN_CHECK_MEMACCESS_FIXEDSHADOW) ||
802 (MI.getOpcode() ==
803 AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW));
804 uint64_t FixedShadowOffset = IsFixedShadow ? MI.getOperand(2).getImm() : 0;
805
806 MCSymbol *&Sym = HwasanMemaccessSymbols[HwasanMemaccessTuple(
807 Reg, IsShort, AccessInfo, IsFixedShadow, FixedShadowOffset)];
808 if (!Sym) {
809 // FIXME: Make this work on non-ELF.
810 if (!TM.getTargetTriple().isOSBinFormatELF())
811 report_fatal_error("llvm.hwasan.check.memaccess only supported on ELF");
812
813 std::string SymName = "__hwasan_check_x" + utostr(Reg - AArch64::X0) + "_" +
814 utostr(AccessInfo);
815 if (IsFixedShadow)
816 SymName += "_fixed_" + utostr(FixedShadowOffset);
817 if (IsShort)
818 SymName += "_short_v2";
819 Sym = OutContext.getOrCreateSymbol(SymName);
820 }
821
822 EmitToStreamer(*OutStreamer,
823 MCInstBuilder(AArch64::BL)
824 .addExpr(MCSymbolRefExpr::create(Sym, OutContext)));
825}
826
827void AArch64AsmPrinter::emitHwasanMemaccessSymbols(Module &M) {
828 if (HwasanMemaccessSymbols.empty())
829 return;
830
831 const Triple &TT = M.getTargetTriple();
832 assert(TT.isOSBinFormatELF());
833 // AArch64Subtarget is huge, so heap allocate it so we don't run out of stack
834 // space.
835 auto STI = std::make_unique<AArch64Subtarget>(
836 TT, TM.getTargetCPU(), TM.getTargetCPU(), TM.getTargetFeatureString(), TM,
837 true);
838 this->STI = STI.get();
839
840 MCSymbol *HwasanTagMismatchV1Sym =
841 OutContext.getOrCreateSymbol("__hwasan_tag_mismatch");
842 MCSymbol *HwasanTagMismatchV2Sym =
843 OutContext.getOrCreateSymbol("__hwasan_tag_mismatch_v2");
844
845 const MCSymbolRefExpr *HwasanTagMismatchV1Ref =
846 MCSymbolRefExpr::create(HwasanTagMismatchV1Sym, OutContext);
847 const MCSymbolRefExpr *HwasanTagMismatchV2Ref =
848 MCSymbolRefExpr::create(HwasanTagMismatchV2Sym, OutContext);
849
850 for (auto &P : HwasanMemaccessSymbols) {
851 unsigned Reg = std::get<0>(P.first);
852 bool IsShort = std::get<1>(P.first);
853 uint32_t AccessInfo = std::get<2>(P.first);
854 bool IsFixedShadow = std::get<3>(P.first);
855 uint64_t FixedShadowOffset = std::get<4>(P.first);
856 const MCSymbolRefExpr *HwasanTagMismatchRef =
857 IsShort ? HwasanTagMismatchV2Ref : HwasanTagMismatchV1Ref;
858 MCSymbol *Sym = P.second;
859
860 bool HasMatchAllTag =
861 (AccessInfo >> HWASanAccessInfo::HasMatchAllShift) & 1;
862 uint8_t MatchAllTag =
863 (AccessInfo >> HWASanAccessInfo::MatchAllShift) & 0xff;
864 unsigned Size =
865 1 << ((AccessInfo >> HWASanAccessInfo::AccessSizeShift) & 0xf);
866 bool CompileKernel =
867 (AccessInfo >> HWASanAccessInfo::CompileKernelShift) & 1;
868
869 OutStreamer->switchSection(OutContext.getELFSection(
870 ".text.hot", ELF::SHT_PROGBITS,
872 /*IsComdat=*/true));
873
874 OutStreamer->emitSymbolAttribute(Sym, MCSA_ELF_TypeFunction);
875 OutStreamer->emitSymbolAttribute(Sym, MCSA_Weak);
876 OutStreamer->emitSymbolAttribute(Sym, MCSA_Hidden);
877 OutStreamer->emitLabel(Sym);
878
879 EmitToStreamer(MCInstBuilder(AArch64::SBFMXri)
880 .addReg(AArch64::X16)
881 .addReg(Reg)
882 .addImm(4)
883 .addImm(55));
884
885 if (IsFixedShadow) {
886 // Aarch64 makes it difficult to embed large constants in the code.
887 // Fortuitously, kShadowBaseAlignment == 32, so we use the 32-bit
888 // left-shift option in the MOV instruction. Combined with the 16-bit
889 // immediate, this is enough to represent any offset up to 2**48.
890 emitMOVZ(AArch64::X17, FixedShadowOffset >> 32, 32);
891 EmitToStreamer(MCInstBuilder(AArch64::LDRBBroX)
892 .addReg(AArch64::W16)
893 .addReg(AArch64::X17)
894 .addReg(AArch64::X16)
895 .addImm(0)
896 .addImm(0));
897 } else {
898 EmitToStreamer(MCInstBuilder(AArch64::LDRBBroX)
899 .addReg(AArch64::W16)
900 .addReg(IsShort ? AArch64::X20 : AArch64::X9)
901 .addReg(AArch64::X16)
902 .addImm(0)
903 .addImm(0));
904 }
905
906 EmitToStreamer(MCInstBuilder(AArch64::SUBSXrs)
907 .addReg(AArch64::XZR)
908 .addReg(AArch64::X16)
909 .addReg(Reg)
911 MCSymbol *HandleMismatchOrPartialSym = OutContext.createTempSymbol();
912 EmitToStreamer(MCInstBuilder(AArch64::Bcc)
913 .addImm(AArch64CC::NE)
915 HandleMismatchOrPartialSym, OutContext)));
916 MCSymbol *ReturnSym = OutContext.createTempSymbol();
917 OutStreamer->emitLabel(ReturnSym);
918 EmitToStreamer(MCInstBuilder(AArch64::RET).addReg(AArch64::LR));
919 OutStreamer->emitLabel(HandleMismatchOrPartialSym);
920
921 if (HasMatchAllTag) {
922 EmitToStreamer(MCInstBuilder(AArch64::UBFMXri)
923 .addReg(AArch64::X17)
924 .addReg(Reg)
925 .addImm(56)
926 .addImm(63));
927 EmitToStreamer(MCInstBuilder(AArch64::SUBSXri)
928 .addReg(AArch64::XZR)
929 .addReg(AArch64::X17)
930 .addImm(MatchAllTag)
931 .addImm(0));
932 EmitToStreamer(
933 MCInstBuilder(AArch64::Bcc)
934 .addImm(AArch64CC::EQ)
935 .addExpr(MCSymbolRefExpr::create(ReturnSym, OutContext)));
936 }
937
938 if (IsShort) {
939 EmitToStreamer(MCInstBuilder(AArch64::SUBSWri)
940 .addReg(AArch64::WZR)
941 .addReg(AArch64::W16)
942 .addImm(15)
943 .addImm(0));
944 MCSymbol *HandleMismatchSym = OutContext.createTempSymbol();
945 EmitToStreamer(
946 MCInstBuilder(AArch64::Bcc)
947 .addImm(AArch64CC::HI)
948 .addExpr(MCSymbolRefExpr::create(HandleMismatchSym, OutContext)));
949
950 EmitToStreamer(MCInstBuilder(AArch64::ANDXri)
951 .addReg(AArch64::X17)
952 .addReg(Reg)
953 .addImm(AArch64_AM::encodeLogicalImmediate(0xf, 64)));
954 if (Size != 1)
955 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
956 .addReg(AArch64::X17)
957 .addReg(AArch64::X17)
958 .addImm(Size - 1)
959 .addImm(0));
960 EmitToStreamer(MCInstBuilder(AArch64::SUBSWrs)
961 .addReg(AArch64::WZR)
962 .addReg(AArch64::W16)
963 .addReg(AArch64::W17)
964 .addImm(0));
965 EmitToStreamer(
966 MCInstBuilder(AArch64::Bcc)
967 .addImm(AArch64CC::LS)
968 .addExpr(MCSymbolRefExpr::create(HandleMismatchSym, OutContext)));
969
970 EmitToStreamer(MCInstBuilder(AArch64::ORRXri)
971 .addReg(AArch64::X16)
972 .addReg(Reg)
973 .addImm(AArch64_AM::encodeLogicalImmediate(0xf, 64)));
974 EmitToStreamer(MCInstBuilder(AArch64::LDRBBui)
975 .addReg(AArch64::W16)
976 .addReg(AArch64::X16)
977 .addImm(0));
978 EmitToStreamer(
979 MCInstBuilder(AArch64::SUBSXrs)
980 .addReg(AArch64::XZR)
981 .addReg(AArch64::X16)
982 .addReg(Reg)
984 EmitToStreamer(
985 MCInstBuilder(AArch64::Bcc)
986 .addImm(AArch64CC::EQ)
987 .addExpr(MCSymbolRefExpr::create(ReturnSym, OutContext)));
988
989 OutStreamer->emitLabel(HandleMismatchSym);
990 }
991
992 EmitToStreamer(MCInstBuilder(AArch64::STPXpre)
993 .addReg(AArch64::SP)
994 .addReg(AArch64::X0)
995 .addReg(AArch64::X1)
996 .addReg(AArch64::SP)
997 .addImm(-32));
998 EmitToStreamer(MCInstBuilder(AArch64::STPXi)
999 .addReg(AArch64::FP)
1000 .addReg(AArch64::LR)
1001 .addReg(AArch64::SP)
1002 .addImm(29));
1003
1004 if (Reg != AArch64::X0)
1005 emitMovXReg(AArch64::X0, Reg);
1006 emitMOVZ(AArch64::X1, AccessInfo & HWASanAccessInfo::RuntimeMask, 0);
1007
1008 if (CompileKernel) {
1009 // The Linux kernel's dynamic loader doesn't support GOT relative
1010 // relocations, but it doesn't support late binding either, so just call
1011 // the function directly.
1012 EmitToStreamer(MCInstBuilder(AArch64::B).addExpr(HwasanTagMismatchRef));
1013 } else {
1014 // Intentionally load the GOT entry and branch to it, rather than possibly
1015 // late binding the function, which may clobber the registers before we
1016 // have a chance to save them.
1017 EmitToStreamer(MCInstBuilder(AArch64::ADRP)
1018 .addReg(AArch64::X16)
1019 .addExpr(MCSpecifierExpr::create(HwasanTagMismatchRef,
1021 OutContext)));
1022 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
1023 .addReg(AArch64::X16)
1024 .addReg(AArch64::X16)
1025 .addExpr(MCSpecifierExpr::create(HwasanTagMismatchRef,
1027 OutContext)));
1028 EmitToStreamer(MCInstBuilder(AArch64::BR).addReg(AArch64::X16));
1029 }
1030 }
1031 this->STI = nullptr;
1032}
1033
1034static void emitAuthenticatedPointer(MCStreamer &OutStreamer,
1035 MCSymbol *StubLabel,
1036 const MCExpr *StubAuthPtrRef) {
1037 // sym$auth_ptr$key$disc:
1038 OutStreamer.emitLabel(StubLabel);
1039 OutStreamer.emitValue(StubAuthPtrRef, /*size=*/8);
1040}
1041
1042void AArch64AsmPrinter::emitEndOfAsmFile(Module &M) {
1043 emitHwasanMemaccessSymbols(M);
1044
1045 const Triple &TT = M.getTargetTriple();
1046 if (TT.isOSBinFormatMachO()) {
1047 // Output authenticated pointers as indirect symbols, if we have any.
1048 MachineModuleInfoMachO &MMIMacho =
1049 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1050
1051 auto Stubs = MMIMacho.getAuthGVStubList();
1052
1053 if (!Stubs.empty()) {
1054 // Switch to the "__auth_ptr" section.
1055 OutStreamer->switchSection(
1056 OutContext.getMachOSection("__DATA", "__auth_ptr", MachO::S_REGULAR,
1058 emitAlignment(Align(8));
1059
1060 for (const auto &Stub : Stubs)
1061 emitAuthenticatedPointer(*OutStreamer, Stub.first, Stub.second);
1062
1063 OutStreamer->addBlankLine();
1064 }
1065
1066 // Funny Darwin hack: This flag tells the linker that no global symbols
1067 // contain code that falls through to other global symbols (e.g. the obvious
1068 // implementation of multiple entry points). If this doesn't occur, the
1069 // linker can safely perform dead code stripping. Since LLVM never
1070 // generates code that does this, it is always safe to set.
1071 OutStreamer->emitSubsectionsViaSymbols();
1072 }
1073
1074 if (TT.isOSBinFormatELF()) {
1075 // Output authenticated pointers as indirect symbols, if we have any.
1076 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
1077
1078 auto Stubs = MMIELF.getAuthGVStubList();
1079
1080 if (!Stubs.empty()) {
1081 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
1082 OutStreamer->switchSection(TLOF.getDataSection());
1083 emitAlignment(Align(8));
1084
1085 for (const auto &Stub : Stubs)
1086 emitAuthenticatedPointer(*OutStreamer, Stub.first, Stub.second);
1087
1088 OutStreamer->addBlankLine();
1089 }
1090
1091 // With signed ELF GOT enabled, the linker looks at the symbol type to
1092 // choose between keys IA (for STT_FUNC) and DA (for other types). Symbols
1093 // for functions not defined in the module have STT_NOTYPE type by default.
1094 // This makes linker to emit signing schema with DA key (instead of IA) for
1095 // corresponding R_AARCH64_AUTH_GLOB_DAT dynamic reloc. To avoid that, force
1096 // all function symbols used in the module to have STT_FUNC type. See
1097 // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#default-signing-schema
1098 const auto *PtrAuthELFGOTFlag = mdconst::extract_or_null<ConstantInt>(
1099 M.getModuleFlag("ptrauth-elf-got"));
1100 if (PtrAuthELFGOTFlag && PtrAuthELFGOTFlag->getZExtValue() == 1)
1101 for (const GlobalValue &GV : M.global_values())
1102 if (!GV.use_empty() && isa<Function>(GV) &&
1103 !GV.getName().starts_with("llvm."))
1104 OutStreamer->emitSymbolAttribute(getSymbol(&GV),
1106 }
1107
1108 // Emit stack and fault map information.
1110
1111 // If import call optimization is enabled, emit the appropriate section.
1112 // We do this whether or not we recorded any import calls.
1113 if (EnableImportCallOptimization && TT.isOSBinFormatCOFF()) {
1114 OutStreamer->switchSection(getObjFileLowering().getImportCallSection());
1115
1116 // Section always starts with some magic.
1117 constexpr char ImpCallMagic[12] = "Imp_Call_V1";
1118 OutStreamer->emitBytes(StringRef{ImpCallMagic, sizeof(ImpCallMagic)});
1119
1120 // Layout of this section is:
1121 // Per section that contains calls to imported functions:
1122 // uint32_t SectionSize: Size in bytes for information in this section.
1123 // uint32_t Section Number
1124 // Per call to imported function in section:
1125 // uint32_t Kind: the kind of imported function.
1126 // uint32_t BranchOffset: the offset of the branch instruction in its
1127 // parent section.
1128 // uint32_t TargetSymbolId: the symbol id of the called function.
1129 for (auto &[Section, CallsToImportedFuncs] :
1130 SectionToImportedFunctionCalls) {
1131 unsigned SectionSize =
1132 sizeof(uint32_t) * (2 + 3 * CallsToImportedFuncs.size());
1133 OutStreamer->emitInt32(SectionSize);
1134 OutStreamer->emitCOFFSecNumber(Section->getBeginSymbol());
1135 for (auto &[CallsiteSymbol, CalledSymbol] : CallsToImportedFuncs) {
1136 // Kind is always IMAGE_REL_ARM64_DYNAMIC_IMPORT_CALL (0x13).
1137 OutStreamer->emitInt32(0x13);
1138 OutStreamer->emitCOFFSecOffset(CallsiteSymbol);
1139 OutStreamer->emitCOFFSymbolIndex(CalledSymbol);
1140 }
1141 }
1142 }
1143}
1144
1145void AArch64AsmPrinter::emitLOHs() {
1147
1148 for (const auto &D : AArch64FI->getLOHContainer()) {
1149 for (const MachineInstr *MI : D.getArgs()) {
1150 MInstToMCSymbol::iterator LabelIt = LOHInstToLabel.find(MI);
1151 assert(LabelIt != LOHInstToLabel.end() &&
1152 "Label hasn't been inserted for LOH related instruction");
1153 MCArgs.push_back(LabelIt->second);
1154 }
1155 OutStreamer->emitLOHDirective(D.getKind(), MCArgs);
1156 MCArgs.clear();
1157 }
1158}
1159
1160// Unwinding can get confused if the last instruction in an exception-handling
1161// region (function, funclet, try block, etc.) is a call, because the return
1162// address then points to the *next* region (see
1163// X86AsmPrinter::maybeEmitNopAfterCallForWindowsEH for the X86 equivalent).
1164// Trap after a noreturn call that would end a function or funclet, and elide
1165// the trap when the next instruction is in the same function.
1166void AArch64AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
1167 if (MBB.succ_empty() && MF->usesWindowsUnwindInfo()) {
1168 auto Next = std::next(MBB.getIterator());
1169 if (Next == MF->end() || Next->isEHFuncletEntry()) {
1170 for (const MachineInstr &MI : reverse(MBB)) {
1171 if (MI.isMetaInstruction() || AArch64InstrInfo::isSEHInstruction(MI))
1172 continue;
1173 if (MI.isCall() && !MI.isReturn())
1174 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::BRK).addImm(1));
1175 break;
1176 }
1177 }
1178 }
1179
1181}
1182
1183void AArch64AsmPrinter::emitFunctionBodyEnd() {
1184 if (!AArch64FI->getLOHRelated().empty())
1185 emitLOHs();
1186}
1187
1188/// GetCPISymbol - Return the symbol for the specified constant pool entry.
1189MCSymbol *AArch64AsmPrinter::GetCPISymbol(unsigned CPID) const {
1190 // Darwin uses a linker-private symbol name for constant-pools (to
1191 // avoid addends on the relocation?), ELF has no such concept and
1192 // uses a normal private symbol.
1193 if (!getDataLayout().getLinkerPrivateGlobalPrefix().empty())
1194 return OutContext.getOrCreateSymbol(
1195 Twine(getDataLayout().getLinkerPrivateGlobalPrefix()) + "CPI" +
1196 Twine(getFunctionNumber()) + "_" + Twine(CPID));
1197
1198 return AsmPrinter::GetCPISymbol(CPID);
1199}
1200
1201void AArch64AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNum,
1202 raw_ostream &O) {
1203 const MachineOperand &MO = MI->getOperand(OpNum);
1204 switch (MO.getType()) {
1205 default:
1206 llvm_unreachable("<unknown operand type>");
1208 Register Reg = MO.getReg();
1210 assert(!MO.getSubReg() && "Subregs should be eliminated!");
1212 break;
1213 }
1215 O << MO.getImm();
1216 break;
1217 }
1219 PrintSymbolOperand(MO, O);
1220 break;
1221 }
1223 MCSymbol *Sym = GetBlockAddressSymbol(MO.getBlockAddress());
1224 Sym->print(O, MAI);
1225 break;
1226 }
1227 }
1228}
1229
1230bool AArch64AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
1231 raw_ostream &O) {
1232 Register Reg = MO.getReg();
1233 switch (Mode) {
1234 default:
1235 return true; // Unknown mode.
1236 case 'w':
1238 break;
1239 case 'x':
1241 break;
1242 case 't':
1244 break;
1245 }
1246
1248 return false;
1249}
1250
1251// Prints the register in MO using class RC using the offset in the
1252// new register class. This should not be used for cross class
1253// printing.
1254bool AArch64AsmPrinter::printAsmRegInClass(const MachineOperand &MO,
1255 const TargetRegisterClass *RC,
1256 unsigned AltName, raw_ostream &O) {
1257 assert(MO.isReg() && "Should only get here with a register!");
1258 const TargetRegisterInfo *RI = STI->getRegisterInfo();
1259 Register Reg = MO.getReg();
1260 MCRegister RegToPrint = RC->getRegister(RI->getEncodingValue(Reg));
1261 if (!RI->regsOverlap(RegToPrint, Reg))
1262 return true;
1263 O << AArch64InstPrinter::getRegisterName(RegToPrint, AltName);
1264 return false;
1265}
1266
1267bool AArch64AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
1268 const char *ExtraCode, raw_ostream &O) {
1269 const MachineOperand &MO = MI->getOperand(OpNum);
1270
1271 // First try the generic code, which knows about modifiers like 'c' and 'n'.
1272 if (!AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O))
1273 return false;
1274
1275 // Does this asm operand have a single letter operand modifier?
1276 if (ExtraCode && ExtraCode[0]) {
1277 if (ExtraCode[1] != 0)
1278 return true; // Unknown modifier.
1279
1280 switch (ExtraCode[0]) {
1281 default:
1282 return true; // Unknown modifier.
1283 case 'w': // Print W register
1284 case 'x': // Print X register
1285 if (MO.isReg())
1286 return printAsmMRegister(MO, ExtraCode[0], O);
1287 if (MO.isImm() && MO.getImm() == 0) {
1288 unsigned Reg = ExtraCode[0] == 'w' ? AArch64::WZR : AArch64::XZR;
1290 return false;
1291 }
1292 printOperand(MI, OpNum, O);
1293 return false;
1294 case 'b': // Print B register.
1295 case 'h': // Print H register.
1296 case 's': // Print S register.
1297 case 'd': // Print D register.
1298 case 'q': // Print Q register.
1299 case 'z': // Print Z register.
1300 if (MO.isReg()) {
1301 const TargetRegisterClass *RC;
1302 switch (ExtraCode[0]) {
1303 case 'b':
1304 RC = &AArch64::FPR8RegClass;
1305 break;
1306 case 'h':
1307 RC = &AArch64::FPR16RegClass;
1308 break;
1309 case 's':
1310 RC = &AArch64::FPR32RegClass;
1311 break;
1312 case 'd':
1313 RC = &AArch64::FPR64RegClass;
1314 break;
1315 case 'q':
1316 RC = &AArch64::FPR128RegClass;
1317 break;
1318 case 'z':
1319 RC = &AArch64::ZPRRegClass;
1320 break;
1321 default:
1322 return true;
1323 }
1324 return printAsmRegInClass(MO, RC, AArch64::NoRegAltName, O);
1325 }
1326 printOperand(MI, OpNum, O);
1327 return false;
1328 }
1329 }
1330
1331 // According to ARM, we should emit x and v registers unless we have a
1332 // modifier.
1333 if (MO.isReg()) {
1334 Register Reg = MO.getReg();
1335
1336 // If this is a w or x register, print an x register.
1337 if (AArch64::GPR32allRegClass.contains(Reg) ||
1338 AArch64::GPR64allRegClass.contains(Reg))
1339 return printAsmMRegister(MO, 'x', O);
1340
1341 // If this is an x register tuple, print an x register.
1342 if (AArch64::GPR64x8ClassRegClass.contains(Reg))
1343 return printAsmMRegister(MO, 't', O);
1344
1345 unsigned AltName = AArch64::NoRegAltName;
1346 const TargetRegisterClass *RegClass;
1347 if (AArch64::ZPRRegClass.contains(Reg)) {
1348 RegClass = &AArch64::ZPRRegClass;
1349 } else if (AArch64::PPRRegClass.contains(Reg)) {
1350 RegClass = &AArch64::PPRRegClass;
1351 } else if (AArch64::PNRRegClass.contains(Reg)) {
1352 RegClass = &AArch64::PNRRegClass;
1353 } else {
1354 RegClass = &AArch64::FPR128RegClass;
1355 AltName = AArch64::vreg;
1356 }
1357
1358 // If this is a b, h, s, d, or q register, print it as a v register.
1359 return printAsmRegInClass(MO, RegClass, AltName, O);
1360 }
1361
1362 printOperand(MI, OpNum, O);
1363 return false;
1364}
1365
1366bool AArch64AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1367 unsigned OpNum,
1368 const char *ExtraCode,
1369 raw_ostream &O) {
1370 if (ExtraCode && ExtraCode[0] && ExtraCode[0] != 'a')
1371 return true; // Unknown modifier.
1372
1373 const MachineOperand &MO = MI->getOperand(OpNum);
1374 assert(MO.isReg() && "unexpected inline asm memory operand");
1375 O << "[" << AArch64InstPrinter::getRegisterName(MO.getReg()) << "]";
1376 return false;
1377}
1378
1379void AArch64AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1380 raw_ostream &OS) {
1381 unsigned NOps = MI->getNumOperands();
1382 assert(NOps == 4);
1383 OS << '\t' << MAI.getCommentString() << "DEBUG_VALUE: ";
1384 // cast away const; DIetc do not take const operands for some reason.
1385 OS << MI->getDebugVariable()->getName();
1386 OS << " <- ";
1387 // Frame address. Currently handles register +- offset only.
1388 assert(MI->isIndirectDebugValue());
1389 OS << '[';
1390 for (unsigned I = 0, E = llvm::size(MI->debug_operands()); I < E; ++I) {
1391 if (I != 0)
1392 OS << ", ";
1393 printOperand(MI, I, OS);
1394 }
1395 OS << ']';
1396 OS << "+";
1397 printOperand(MI, NOps - 2, OS);
1398}
1399
1400void AArch64AsmPrinter::emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
1401 ArrayRef<unsigned> JumpTableIndices) {
1402 // Fast return if there is nothing to emit to avoid creating empty sections.
1403 if (JumpTableIndices.empty())
1404 return;
1405 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
1406 const auto &F = MF->getFunction();
1408
1409 MCSection *ReadOnlySec = nullptr;
1410 if (TM.Options.EnableStaticDataPartitioning) {
1411 ReadOnlySec =
1412 TLOF.getSectionForJumpTable(F, TM, &JT[JumpTableIndices.front()]);
1413 } else {
1414 ReadOnlySec = TLOF.getSectionForJumpTable(F, TM);
1415 }
1416 OutStreamer->switchSection(ReadOnlySec);
1417
1418 auto AFI = MF->getInfo<AArch64FunctionInfo>();
1419 for (unsigned JTI : JumpTableIndices) {
1420 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1421
1422 // If this jump table was deleted, ignore it.
1423 if (JTBBs.empty()) continue;
1424
1425 unsigned Size = AFI->getJumpTableEntrySize(JTI);
1426 emitAlignment(Align(Size));
1427 OutStreamer->emitLabel(GetJTISymbol(JTI));
1428
1429 const MCSymbol *BaseSym = AArch64FI->getJumpTableEntryPCRelSymbol(JTI);
1430 const MCExpr *Base = MCSymbolRefExpr::create(BaseSym, OutContext);
1431
1432 for (auto *JTBB : JTBBs) {
1433 const MCExpr *Value =
1434 MCSymbolRefExpr::create(JTBB->getSymbol(), OutContext);
1435
1436 // Each entry is:
1437 // .byte/.hword (LBB - Lbase)>>2
1438 // or plain:
1439 // .word LBB - Lbase
1440 Value = MCBinaryExpr::createSub(Value, Base, OutContext);
1441 if (Size != 4)
1443 Value, MCConstantExpr::create(2, OutContext), OutContext);
1444
1445 OutStreamer->emitValue(Value, Size);
1446 }
1447 }
1448}
1449
1450std::tuple<const MCSymbol *, uint64_t, const MCSymbol *,
1452AArch64AsmPrinter::getCodeViewJumpTableInfo(int JTI,
1453 const MachineInstr *BranchInstr,
1454 const MCSymbol *BranchLabel) const {
1455 const auto AFI = MF->getInfo<AArch64FunctionInfo>();
1456 const auto Base = AArch64FI->getJumpTableEntryPCRelSymbol(JTI);
1458 switch (AFI->getJumpTableEntrySize(JTI)) {
1459 case 1:
1460 EntrySize = codeview::JumpTableEntrySize::UInt8ShiftLeft;
1461 break;
1462 case 2:
1463 EntrySize = codeview::JumpTableEntrySize::UInt16ShiftLeft;
1464 break;
1465 case 4:
1466 EntrySize = codeview::JumpTableEntrySize::Int32;
1467 break;
1468 default:
1469 llvm_unreachable("Unexpected jump table entry size");
1470 }
1471 return std::make_tuple(Base, 0, BranchLabel, EntrySize);
1472}
1473
1474void AArch64AsmPrinter::emitFunctionEntryLabel() {
1475 const Triple &TT = TM.getTargetTriple();
1476 if (TT.isOSBinFormatELF() &&
1477 (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall ||
1478 MF->getFunction().getCallingConv() ==
1479 CallingConv::AArch64_SVE_VectorCall ||
1480 MF->getInfo<AArch64FunctionInfo>()->isSVECC())) {
1481 auto *TS =
1482 static_cast<AArch64TargetStreamer *>(OutStreamer->getTargetStreamer());
1483 TS->emitDirectiveVariantPCS(CurrentFnSym);
1484 }
1485
1487
1488 if (TT.isWindowsArm64EC() && !MF->getFunction().hasLocalLinkage()) {
1489 // For ARM64EC targets, a function definition's name is mangled differently
1490 // from the normal symbol, emit required aliases here.
1491 auto emitFunctionAlias = [&](MCSymbol *Src, MCSymbol *Dst) {
1492 OutStreamer->emitSymbolAttribute(Src, MCSA_WeakAntiDep);
1493 OutStreamer->emitAssignment(
1494 Src, MCSymbolRefExpr::create(Dst, MMI->getContext()));
1495 };
1496
1497 auto getSymbolFromMetadata = [&](StringRef Name) {
1498 MCSymbol *Sym = nullptr;
1499 if (MDNode *Node = MF->getFunction().getMetadata(Name)) {
1500 StringRef NameStr = cast<MDString>(Node->getOperand(0))->getString();
1501 Sym = MMI->getContext().getOrCreateSymbol(NameStr);
1502 }
1503 return Sym;
1504 };
1505
1506 SmallVector<MDNode *> UnmangledNames;
1507 MF->getFunction().getMetadata("arm64ec_unmangled_name", UnmangledNames);
1508 for (MDNode *Node : UnmangledNames) {
1509 StringRef NameStr = cast<MDString>(Node->getOperand(0))->getString();
1510 MCSymbol *UnmangledSym = MMI->getContext().getOrCreateSymbol(NameStr);
1511 if (std::optional<std::string> MangledName =
1512 getArm64ECMangledFunctionName(UnmangledSym->getName())) {
1513 MCSymbol *ECMangledSym =
1514 MMI->getContext().getOrCreateSymbol(*MangledName);
1515 emitFunctionAlias(UnmangledSym, ECMangledSym);
1516 }
1517 }
1518 if (MCSymbol *ECMangledSym =
1519 getSymbolFromMetadata("arm64ec_ecmangled_name"))
1520 emitFunctionAlias(ECMangledSym, CurrentFnSym);
1521 }
1522}
1523
1524void AArch64AsmPrinter::emitXXStructor(const DataLayout &DL,
1525 const Constant *CV) {
1526 LLVMContext &C = CV->getContext();
1528 "ctors/dtors are to be signed by asm printer");
1529
1530 if (PtrauthInitFini) {
1531 IntegerType *Int32Ty = IntegerType::get(C, 32);
1532 IntegerType *Int64Ty = IntegerType::get(C, 64);
1533 PointerType *PtrTy = PointerType::get(C, 0);
1534
1535 ConstantInt *Key = ConstantInt::get(Int32Ty, AArch64PAuth::InitFiniKey);
1536 ConstantInt *IntDisc = ConstantInt::get(
1539 Constant *AddressDisc = Null;
1540 if (PtrauthInitFiniAddressDisc) {
1542 AddressDisc =
1543 ConstantExpr::getIntToPtr(ConstantInt::get(Int64Ty, Marker), PtrTy);
1544 }
1545
1546 CV = ConstantPtrAuth::get(const_cast<Constant *>(CV), Key, IntDisc,
1547 AddressDisc, /*DeactivationSymbol=*/Null);
1548 }
1549
1550 // Signed pointers will be lowered by AArch64AsmPrinter::lowerConstantPtrAuth.
1552}
1553
1554void AArch64AsmPrinter::emitGlobalAlias(const Module &M,
1555 const GlobalAlias &GA) {
1556 if (auto F = dyn_cast_or_null<Function>(GA.getAliasee())) {
1557 // Global aliases must point to a definition, but unmangled patchable
1558 // symbols are special and need to point to an undefined symbol with "EXP+"
1559 // prefix. Such undefined symbol is resolved by the linker by creating
1560 // x86 thunk that jumps back to the actual EC target.
1561 if (MDNode *Node = F->getMetadata("arm64ec_exp_name")) {
1562 StringRef ExpStr = cast<MDString>(Node->getOperand(0))->getString();
1563 MCSymbol *ExpSym = MMI->getContext().getOrCreateSymbol(ExpStr);
1564 MCSymbol *Sym = MMI->getContext().getOrCreateSymbol(GA.getName());
1565
1566 OutStreamer->beginCOFFSymbolDef(ExpSym);
1567 OutStreamer->emitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
1568 OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
1570 OutStreamer->endCOFFSymbolDef();
1571
1572 OutStreamer->beginCOFFSymbolDef(Sym);
1573 OutStreamer->emitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
1574 OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
1576 OutStreamer->endCOFFSymbolDef();
1577 OutStreamer->emitSymbolAttribute(Sym, MCSA_Weak);
1578 OutStreamer->emitAssignment(
1579 Sym, MCSymbolRefExpr::create(ExpSym, MMI->getContext()));
1580 return;
1581 }
1582 }
1584}
1585
1586/// Small jump tables contain an unsigned byte or half, representing the offset
1587/// from the lowest-addressed possible destination to the desired basic
1588/// block. Since all instructions are 4-byte aligned, this is further compressed
1589/// by counting in instructions rather than bytes (i.e. divided by 4). So, to
1590/// materialize the correct destination we need:
1591///
1592/// adr xDest, .LBB0_0
1593/// ldrb wScratch, [xTable, xEntry] (with "lsl #1" for ldrh).
1594/// add xDest, xDest, xScratch (with "lsl #2" for smaller entries)
1595void AArch64AsmPrinter::LowerJumpTableDest(llvm::MCStreamer &OutStreamer,
1596 const llvm::MachineInstr &MI) {
1597 Register DestReg = MI.getOperand(0).getReg();
1598 Register ScratchReg = MI.getOperand(1).getReg();
1599 Register ScratchRegW =
1600 STI->getRegisterInfo()->getSubReg(ScratchReg, AArch64::sub_32);
1601 Register TableReg = MI.getOperand(2).getReg();
1602 Register EntryReg = MI.getOperand(3).getReg();
1603 int JTIdx = MI.getOperand(4).getIndex();
1604 int Size = AArch64FI->getJumpTableEntrySize(JTIdx);
1605
1606 // This has to be first because the compression pass based its reachability
1607 // calculations on the start of the JumpTableDest instruction.
1608 auto Label =
1609 MF->getInfo<AArch64FunctionInfo>()->getJumpTableEntryPCRelSymbol(JTIdx);
1610
1611 // If we don't already have a symbol to use as the base, use the ADR
1612 // instruction itself.
1613 if (!Label) {
1615 AArch64FI->setJumpTableEntryInfo(JTIdx, Size, Label);
1616 OutStreamer.emitLabel(Label);
1617 }
1618
1619 auto LabelExpr = MCSymbolRefExpr::create(Label, MF->getContext());
1620 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::ADR)
1621 .addReg(DestReg)
1622 .addExpr(LabelExpr));
1623
1624 // Load the number of instruction-steps to offset from the label.
1625 unsigned LdrOpcode;
1626 switch (Size) {
1627 case 1: LdrOpcode = AArch64::LDRBBroX; break;
1628 case 2: LdrOpcode = AArch64::LDRHHroX; break;
1629 case 4: LdrOpcode = AArch64::LDRSWroX; break;
1630 default:
1631 llvm_unreachable("Unknown jump table size");
1632 }
1633
1634 EmitToStreamer(OutStreamer, MCInstBuilder(LdrOpcode)
1635 .addReg(Size == 4 ? ScratchReg : ScratchRegW)
1636 .addReg(TableReg)
1637 .addReg(EntryReg)
1638 .addImm(0)
1639 .addImm(Size == 1 ? 0 : 1));
1640
1641 // Add to the already materialized base label address, multiplying by 4 if
1642 // compressed.
1643 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::ADDXrs)
1644 .addReg(DestReg)
1645 .addReg(DestReg)
1646 .addReg(ScratchReg)
1647 .addImm(Size == 4 ? 0 : 2));
1648}
1649
1650void AArch64AsmPrinter::LowerHardenedBRJumpTable(const MachineInstr &MI) {
1651 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1652 assert(MJTI && "Can't lower jump-table dispatch without JTI");
1653
1654 const std::vector<MachineJumpTableEntry> &JTs = MJTI->getJumpTables();
1655 assert(!JTs.empty() && "Invalid JT index for jump-table dispatch");
1656
1657 // Emit:
1658 // mov x17, #<size of table> ; depending on table size, with MOVKs
1659 // cmp x16, x17 ; or #imm if table size fits in 12-bit
1660 // csel x16, x16, xzr, ls ; check for index overflow
1661 //
1662 // adrp x17, Ltable@PAGE ; materialize table address
1663 // add x17, Ltable@PAGEOFF
1664 // ldrsw x16, [x17, x16, lsl #2] ; load table entry
1665 //
1666 // Lanchor:
1667 // adr x17, Lanchor ; compute target address
1668 // add x16, x17, x16
1669 // br x16 ; branch to target
1670
1671 MachineOperand JTOp = MI.getOperand(0);
1672
1673 unsigned JTI = JTOp.getIndex();
1674 assert(!AArch64FI->getJumpTableEntryPCRelSymbol(JTI) &&
1675 "unsupported compressed jump table");
1676
1677 const uint64_t NumTableEntries = JTs[JTI].MBBs.size();
1678
1679 // cmp only supports a 12-bit immediate. If we need more, materialize the
1680 // immediate, using x17 as a scratch register.
1681 uint64_t MaxTableEntry = NumTableEntries - 1;
1682 if (isUInt<12>(MaxTableEntry)) {
1683 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::SUBSXri)
1684 .addReg(AArch64::XZR)
1685 .addReg(AArch64::X16)
1686 .addImm(MaxTableEntry)
1687 .addImm(0));
1688 } else {
1689 emitMOVZ(AArch64::X17, static_cast<uint16_t>(MaxTableEntry), 0);
1690 // It's sad that we have to manually materialize instructions, but we can't
1691 // trivially reuse the main pseudo expansion logic.
1692 // A MOVK sequence is easy enough to generate and handles the general case.
1693 for (int Offset = 16; Offset < 64; Offset += 16) {
1694 if ((MaxTableEntry >> Offset) == 0)
1695 break;
1696 emitMOVK(AArch64::X17, static_cast<uint16_t>(MaxTableEntry >> Offset),
1697 Offset);
1698 }
1699 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::SUBSXrs)
1700 .addReg(AArch64::XZR)
1701 .addReg(AArch64::X16)
1702 .addReg(AArch64::X17)
1703 .addImm(0));
1704 }
1705
1706 // This picks entry #0 on failure.
1707 // We might want to trap instead.
1708 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::CSELXr)
1709 .addReg(AArch64::X16)
1710 .addReg(AArch64::X16)
1711 .addReg(AArch64::XZR)
1712 .addImm(AArch64CC::LS));
1713
1714 // Prepare the @PAGE/@PAGEOFF low/high operands.
1715 MachineOperand JTMOHi(JTOp), JTMOLo(JTOp);
1716 MCOperand JTMCHi, JTMCLo;
1717
1718 JTMOHi.setTargetFlags(AArch64II::MO_PAGE);
1719 JTMOLo.setTargetFlags(AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
1720
1721 MCInstLowering.lowerOperand(JTMOHi, JTMCHi);
1722 MCInstLowering.lowerOperand(JTMOLo, JTMCLo);
1723
1724 EmitToStreamer(
1725 *OutStreamer,
1726 MCInstBuilder(AArch64::ADRP).addReg(AArch64::X17).addOperand(JTMCHi));
1727
1728 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::ADDXri)
1729 .addReg(AArch64::X17)
1730 .addReg(AArch64::X17)
1731 .addOperand(JTMCLo)
1732 .addImm(0));
1733
1734 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::LDRSWroX)
1735 .addReg(AArch64::X16)
1736 .addReg(AArch64::X17)
1737 .addReg(AArch64::X16)
1738 .addImm(0)
1739 .addImm(1));
1740
1741 MCSymbol *AdrLabel = MF->getContext().createTempSymbol();
1742 const auto *AdrLabelE = MCSymbolRefExpr::create(AdrLabel, MF->getContext());
1743 AArch64FI->setJumpTableEntryInfo(JTI, 4, AdrLabel);
1744
1745 OutStreamer->emitLabel(AdrLabel);
1746 EmitToStreamer(
1747 *OutStreamer,
1748 MCInstBuilder(AArch64::ADR).addReg(AArch64::X17).addExpr(AdrLabelE));
1749
1750 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::ADDXrs)
1751 .addReg(AArch64::X16)
1752 .addReg(AArch64::X17)
1753 .addReg(AArch64::X16)
1754 .addImm(0));
1755
1756 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::BR).addReg(AArch64::X16));
1757}
1758
1759void AArch64AsmPrinter::LowerMOPS(llvm::MCStreamer &OutStreamer,
1760 const llvm::MachineInstr &MI) {
1761 unsigned Opcode = MI.getOpcode();
1762 assert(STI->hasMOPS());
1763 assert(STI->hasMTE() || Opcode != AArch64::MOPSMemorySetTaggingPseudo);
1764
1765 const auto Ops = [Opcode]() -> std::array<unsigned, 3> {
1766 if (Opcode == AArch64::MOPSMemoryCopyPseudo)
1767 return {AArch64::CPYFP, AArch64::CPYFM, AArch64::CPYFE};
1768 if (Opcode == AArch64::MOPSMemoryMovePseudo)
1769 return {AArch64::CPYP, AArch64::CPYM, AArch64::CPYE};
1770 if (Opcode == AArch64::MOPSMemorySetPseudo)
1771 return {AArch64::SETP, AArch64::SETM, AArch64::SETE};
1772 if (Opcode == AArch64::MOPSMemorySetTaggingPseudo)
1773 return {AArch64::SETGP, AArch64::SETGM, AArch64::MOPSSETGE};
1774 llvm_unreachable("Unhandled memory operation pseudo");
1775 }();
1776 const bool IsSet = Opcode == AArch64::MOPSMemorySetPseudo ||
1777 Opcode == AArch64::MOPSMemorySetTaggingPseudo;
1778
1779 for (auto Op : Ops) {
1780 int i = 0;
1781 auto MCIB = MCInstBuilder(Op);
1782 // Destination registers
1783 MCIB.addReg(MI.getOperand(i++).getReg());
1784 MCIB.addReg(MI.getOperand(i++).getReg());
1785 if (!IsSet)
1786 MCIB.addReg(MI.getOperand(i++).getReg());
1787 // Input registers
1788 MCIB.addReg(MI.getOperand(i++).getReg());
1789 MCIB.addReg(MI.getOperand(i++).getReg());
1790 MCIB.addReg(MI.getOperand(i++).getReg());
1791
1792 EmitToStreamer(OutStreamer, MCIB);
1793 }
1794}
1795
1796void AArch64AsmPrinter::LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
1797 const MachineInstr &MI) {
1798 unsigned NumNOPBytes = StackMapOpers(&MI).getNumPatchBytes();
1799
1800 auto &Ctx = OutStreamer.getContext();
1801 MCSymbol *MILabel = Ctx.createTempSymbol();
1802 OutStreamer.emitLabel(MILabel);
1803
1804 SM.recordStackMap(*MILabel, MI);
1805 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
1806
1807 // Scan ahead to trim the shadow.
1808 const MachineBasicBlock &MBB = *MI.getParent();
1810 ++MII;
1811 while (NumNOPBytes > 0) {
1812 if (MII != MBB.end() && MII->isDebugInstr()) {
1813 ++MII;
1814 continue;
1815 }
1816 if (MII == MBB.end() || MII->isCall() ||
1817 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
1818 MII->getOpcode() == TargetOpcode::STACKMAP)
1819 break;
1820 ++MII;
1821 NumNOPBytes -= 4;
1822 }
1823
1824 // Emit nops.
1825 for (unsigned i = 0; i < NumNOPBytes; i += 4)
1826 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::NOP));
1827}
1828
1829// Lower a patchpoint of the form:
1830// [<def>], <id>, <numBytes>, <target>, <numArgs>
1831void AArch64AsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
1832 const MachineInstr &MI) {
1833 auto &Ctx = OutStreamer.getContext();
1834 MCSymbol *MILabel = Ctx.createTempSymbol();
1835 OutStreamer.emitLabel(MILabel);
1836 SM.recordPatchPoint(*MILabel, MI);
1837
1838 PatchPointOpers Opers(&MI);
1839
1840 int64_t CallTarget = Opers.getCallTarget().getImm();
1841 unsigned EncodedBytes = 0;
1842 if (CallTarget) {
1843 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
1844 "High 16 bits of call target should be zero.");
1845 Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
1846 EncodedBytes = 16;
1847 // Materialize the jump address:
1848 emitMOVZ(ScratchReg, (CallTarget >> 32) & 0xFFFF, 32);
1849 emitMOVK(ScratchReg, (CallTarget >> 16) & 0xFFFF, 16);
1850 emitMOVK(ScratchReg, CallTarget & 0xFFFF, 0);
1851 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::BLR).addReg(ScratchReg));
1852 }
1853 // Emit padding.
1854 unsigned NumBytes = Opers.getNumPatchBytes();
1855 assert(NumBytes >= EncodedBytes &&
1856 "Patchpoint can't request size less than the length of a call.");
1857 assert((NumBytes - EncodedBytes) % 4 == 0 &&
1858 "Invalid number of NOP bytes requested!");
1859 for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
1860 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::NOP));
1861}
1862
1863void AArch64AsmPrinter::LowerSTATEPOINT(MCStreamer &OutStreamer, StackMaps &SM,
1864 const MachineInstr &MI) {
1865 StatepointOpers SOpers(&MI);
1866 if (unsigned PatchBytes = SOpers.getNumPatchBytes()) {
1867 assert(PatchBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
1868 for (unsigned i = 0; i < PatchBytes; i += 4)
1869 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::NOP));
1870 } else {
1871 // Lower call target and choose correct opcode
1872 const MachineOperand &CallTarget = SOpers.getCallTarget();
1873 MCOperand CallTargetMCOp;
1874 unsigned CallOpcode;
1875 switch (CallTarget.getType()) {
1878 MCInstLowering.lowerOperand(CallTarget, CallTargetMCOp);
1879 CallOpcode = AArch64::BL;
1880 break;
1882 CallTargetMCOp = MCOperand::createImm(CallTarget.getImm());
1883 CallOpcode = AArch64::BL;
1884 break;
1886 CallTargetMCOp = MCOperand::createReg(CallTarget.getReg());
1887 CallOpcode = AArch64::BLR;
1888 break;
1889 default:
1890 llvm_unreachable("Unsupported operand type in statepoint call target");
1891 break;
1892 }
1893
1894 EmitToStreamer(OutStreamer,
1895 MCInstBuilder(CallOpcode).addOperand(CallTargetMCOp));
1896 }
1897
1898 auto &Ctx = OutStreamer.getContext();
1899 MCSymbol *MILabel = Ctx.createTempSymbol();
1900 OutStreamer.emitLabel(MILabel);
1901 SM.recordStatepoint(*MILabel, MI);
1902}
1903
1904void AArch64AsmPrinter::LowerFAULTING_OP(const MachineInstr &FaultingMI) {
1905 // FAULTING_LOAD_OP <def>, <faltinf type>, <MBB handler>,
1906 // <opcode>, <operands>
1907
1908 Register DefRegister = FaultingMI.getOperand(0).getReg();
1910 static_cast<FaultMaps::FaultKind>(FaultingMI.getOperand(1).getImm());
1911 MCSymbol *HandlerLabel = FaultingMI.getOperand(2).getMBB()->getSymbol();
1912 unsigned Opcode = FaultingMI.getOperand(3).getImm();
1913 unsigned OperandsBeginIdx = 4;
1914
1915 auto &Ctx = OutStreamer->getContext();
1916 MCSymbol *FaultingLabel = Ctx.createTempSymbol();
1917 OutStreamer->emitLabel(FaultingLabel);
1918
1919 assert(FK < FaultMaps::FaultKindMax && "Invalid Faulting Kind!");
1920 FM.recordFaultingOp(FK, FaultingLabel, HandlerLabel);
1921
1922 MCInst MI;
1923 MI.setOpcode(Opcode);
1924
1925 if (DefRegister != (Register)0)
1926 MI.addOperand(MCOperand::createReg(DefRegister));
1927
1928 for (const MachineOperand &MO :
1929 llvm::drop_begin(FaultingMI.operands(), OperandsBeginIdx)) {
1930 MCOperand Dest;
1931 lowerOperand(MO, Dest);
1932 MI.addOperand(Dest);
1933 }
1934
1935 OutStreamer->AddComment("on-fault: " + HandlerLabel->getName());
1936 EmitToStreamer(MI);
1937}
1938
1939void AArch64AsmPrinter::emitMovXReg(Register Dest, Register Src) {
1940 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::ORRXrs)
1941 .addReg(Dest)
1942 .addReg(AArch64::XZR)
1943 .addReg(Src)
1944 .addImm(0));
1945}
1946
1947void AArch64AsmPrinter::emitMOVZ(Register Dest, uint64_t Imm, unsigned Shift) {
1948 bool Is64Bit = AArch64::GPR64RegClass.contains(Dest);
1949 EmitToStreamer(*OutStreamer,
1950 MCInstBuilder(Is64Bit ? AArch64::MOVZXi : AArch64::MOVZWi)
1951 .addReg(Dest)
1952 .addImm(Imm)
1953 .addImm(Shift));
1954}
1955
1956void AArch64AsmPrinter::emitMOVK(Register Dest, uint64_t Imm, unsigned Shift) {
1957 bool Is64Bit = AArch64::GPR64RegClass.contains(Dest);
1958 EmitToStreamer(*OutStreamer,
1959 MCInstBuilder(Is64Bit ? AArch64::MOVKXi : AArch64::MOVKWi)
1960 .addReg(Dest)
1961 .addReg(Dest)
1962 .addImm(Imm)
1963 .addImm(Shift));
1964}
1965
1966void AArch64AsmPrinter::emitAUT(AArch64PACKey::ID Key, Register Pointer,
1967 Register Disc) {
1968 bool IsZeroDisc = Disc == AArch64::XZR;
1969 unsigned Opcode = getAUTOpcodeForKey(Key, IsZeroDisc);
1970
1971 // autiza x16 ; if IsZeroDisc
1972 // autia x16, x17 ; if !IsZeroDisc
1973 MCInst AUTInst;
1974 AUTInst.setOpcode(Opcode);
1975 AUTInst.addOperand(MCOperand::createReg(Pointer));
1976 AUTInst.addOperand(MCOperand::createReg(Pointer));
1977 if (!IsZeroDisc)
1978 AUTInst.addOperand(MCOperand::createReg(Disc));
1979
1980 EmitToStreamer(AUTInst);
1981}
1982
1983void AArch64AsmPrinter::emitPAC(AArch64PACKey::ID Key, Register Pointer,
1984 Register Disc) {
1985 bool IsZeroDisc = Disc == AArch64::XZR;
1986 unsigned Opcode = getPACOpcodeForKey(Key, IsZeroDisc);
1987
1988 // paciza x16 ; if IsZeroDisc
1989 // pacia x16, x17 ; if !IsZeroDisc
1990 MCInst PACInst;
1991 PACInst.setOpcode(Opcode);
1992 PACInst.addOperand(MCOperand::createReg(Pointer));
1993 PACInst.addOperand(MCOperand::createReg(Pointer));
1994 if (!IsZeroDisc)
1995 PACInst.addOperand(MCOperand::createReg(Disc));
1996
1997 EmitToStreamer(PACInst);
1998}
1999
2000void AArch64AsmPrinter::emitBLRA(bool IsCall, AArch64PACKey::ID Key,
2001 Register Target, Register Disc) {
2002 bool IsZeroDisc = Disc == AArch64::XZR;
2003 unsigned Opcode = getBranchOpcodeForKey(IsCall, Key, IsZeroDisc);
2004
2005 // blraaz x16 ; if IsZeroDisc
2006 // blraa x16, x17 ; if !IsZeroDisc
2007 MCInst Inst;
2008 Inst.setOpcode(Opcode);
2009 Inst.addOperand(MCOperand::createReg(Target));
2010 if (!IsZeroDisc)
2011 Inst.addOperand(MCOperand::createReg(Disc));
2012 EmitToStreamer(Inst);
2013}
2014
2015void AArch64AsmPrinter::emitFMov0(const MachineInstr &MI) {
2016 Register DestReg = MI.getOperand(0).getReg();
2017 if (!STI->hasZeroCycleZeroingFPWorkaround() && STI->isNeonAvailable()) {
2018 if (STI->hasZeroCycleZeroingFPR64()) {
2019 // Convert H/S register to corresponding D register
2020 const AArch64RegisterInfo *TRI = STI->getRegisterInfo();
2021 if (AArch64::FPR16RegClass.contains(DestReg))
2022 DestReg = TRI->getMatchingSuperReg(DestReg, AArch64::hsub,
2023 &AArch64::FPR64RegClass);
2024 else if (AArch64::FPR32RegClass.contains(DestReg))
2025 DestReg = TRI->getMatchingSuperReg(DestReg, AArch64::ssub,
2026 &AArch64::FPR64RegClass);
2027 else
2028 assert(AArch64::FPR64RegClass.contains(DestReg));
2029
2030 MCInst MOVI;
2031 MOVI.setOpcode(AArch64::MOVID);
2032 MOVI.addOperand(MCOperand::createReg(DestReg));
2034 EmitToStreamer(*OutStreamer, MOVI);
2035 ++NumZCZeroingInstrsFPR;
2036 } else if (STI->hasZeroCycleZeroingFPR128()) {
2037 // Convert H/S/D register to corresponding Q register
2038 const AArch64RegisterInfo *TRI = STI->getRegisterInfo();
2039 if (AArch64::FPR16RegClass.contains(DestReg)) {
2040 DestReg = TRI->getMatchingSuperReg(DestReg, AArch64::hsub,
2041 &AArch64::FPR128RegClass);
2042 } else if (AArch64::FPR32RegClass.contains(DestReg)) {
2043 DestReg = TRI->getMatchingSuperReg(DestReg, AArch64::ssub,
2044 &AArch64::FPR128RegClass);
2045 } else {
2046 assert(AArch64::FPR64RegClass.contains(DestReg));
2047 DestReg = TRI->getMatchingSuperReg(DestReg, AArch64::dsub,
2048 &AArch64::FPR128RegClass);
2049 }
2050
2051 MCInst MOVI;
2052 MOVI.setOpcode(AArch64::MOVIv2d_ns);
2053 MOVI.addOperand(MCOperand::createReg(DestReg));
2055 EmitToStreamer(*OutStreamer, MOVI);
2056 ++NumZCZeroingInstrsFPR;
2057 } else {
2058 emitFMov0AsFMov(MI, DestReg);
2059 }
2060 } else {
2061 emitFMov0AsFMov(MI, DestReg);
2062 }
2063}
2064
2065void AArch64AsmPrinter::emitFMov0AsFMov(const MachineInstr &MI,
2066 Register DestReg) {
2067 MCInst FMov;
2068 switch (MI.getOpcode()) {
2069 default:
2070 llvm_unreachable("Unexpected opcode");
2071 case AArch64::FMOVH0:
2072 FMov.setOpcode(STI->hasFullFP16() ? AArch64::FMOVWHr : AArch64::FMOVWSr);
2073 if (!STI->hasFullFP16())
2074 DestReg = (AArch64::S0 + (DestReg - AArch64::H0));
2075 FMov.addOperand(MCOperand::createReg(DestReg));
2076 FMov.addOperand(MCOperand::createReg(AArch64::WZR));
2077 break;
2078 case AArch64::FMOVS0:
2079 FMov.setOpcode(AArch64::FMOVWSr);
2080 FMov.addOperand(MCOperand::createReg(DestReg));
2081 FMov.addOperand(MCOperand::createReg(AArch64::WZR));
2082 break;
2083 case AArch64::FMOVD0:
2084 FMov.setOpcode(AArch64::FMOVXDr);
2085 FMov.addOperand(MCOperand::createReg(DestReg));
2086 FMov.addOperand(MCOperand::createReg(AArch64::XZR));
2087 break;
2088 }
2089 EmitToStreamer(*OutStreamer, FMov);
2090}
2091
2092Register AArch64AsmPrinter::emitPtrauthDiscriminator(uint64_t Disc,
2093 Register AddrDisc,
2094 Register ScratchReg,
2095 bool MayClobberAddrDisc) {
2096 assert(isPtrauthRegSafe(ScratchReg) &&
2097 "Safe scratch register must be provided by the caller");
2098 assert(isUInt<16>(Disc) && "Constant discriminator is too wide");
2099
2100 // So far we've used NoRegister in pseudos. Now we need real encodings.
2101 if (!AddrDisc.isValid())
2102 AddrDisc = AArch64::XZR;
2103
2104 // If there is no constant discriminator, there's no blend involved:
2105 // just use the address discriminator register as-is (XZR or not).
2106 if (!Disc)
2107 return AddrDisc;
2108
2109 // If there's only a constant discriminator, MOV it into the scratch register.
2110 if (AddrDisc == AArch64::XZR) {
2111 emitMOVZ(ScratchReg, Disc, 0);
2112 return ScratchReg;
2113 }
2114
2115 // If there are both, emit a blend into the scratch register.
2116
2117 // Check if we can save one MOV instruction.
2118 if (MayClobberAddrDisc && isPtrauthRegSafe(AddrDisc)) {
2119 ScratchReg = AddrDisc;
2120 } else {
2121 emitMovXReg(ScratchReg, AddrDisc);
2122 assert(ScratchReg != AddrDisc &&
2123 "Forbidden to clobber AddrDisc, but have to");
2124 }
2125
2126 emitMOVK(ScratchReg, Disc, 48);
2127 return ScratchReg;
2128}
2129
2130/// Emit a code sequence to check an authenticated pointer value.
2131///
2132/// This function emits a sequence of instructions that checks if TestedReg was
2133/// authenticated successfully. On success, execution continues at the next
2134/// instruction after the sequence.
2135///
2136/// The action performed on failure depends on the OnFailure argument:
2137/// * if OnFailure is not nullptr, control is transferred to that label after
2138/// clearing the PAC field
2139/// * otherwise, BRK instruction is emitted to generate an error
2140void AArch64AsmPrinter::emitPtrauthCheckAuthenticatedValue(
2141 Register TestedReg, Register ScratchReg, AArch64PACKey::ID Key,
2142 AArch64PAuth::AuthCheckMethod Method, const MCSymbol *OnFailure) {
2143 // Insert a sequence to check if authentication of TestedReg succeeded,
2144 // such as:
2145 //
2146 // - checked and clearing:
2147 // ; x16 is TestedReg, x17 is ScratchReg
2148 // mov x17, x16
2149 // xpaci x17
2150 // cmp x16, x17
2151 // b.eq Lsuccess
2152 // mov x16, x17
2153 // b Lend
2154 // Lsuccess:
2155 // ; skipped if authentication failed
2156 // Lend:
2157 // ...
2158 //
2159 // - checked and trapping:
2160 // mov x17, x16
2161 // xpaci x17
2162 // cmp x16, x17
2163 // b.eq Lsuccess
2164 // brk #<0xc470 + aut key>
2165 // Lsuccess:
2166 // ...
2167 //
2168 // See the documentation on AuthCheckMethod enumeration constants for
2169 // the specific code sequences that can be used to perform the check.
2171
2172 if (Method == AuthCheckMethod::None)
2173 return;
2174 if (Method == AuthCheckMethod::DummyLoad) {
2175 EmitToStreamer(MCInstBuilder(AArch64::LDRWui)
2176 .addReg(getWRegFromXReg(ScratchReg))
2177 .addReg(TestedReg)
2178 .addImm(0));
2179 assert(!OnFailure && "DummyLoad always traps on error");
2180 return;
2181 }
2182
2183 MCSymbol *SuccessSym = createTempSymbol("auth_success_");
2184 if (Method == AuthCheckMethod::XPAC || Method == AuthCheckMethod::XPACHint) {
2185 // mov Xscratch, Xtested
2186 emitMovXReg(ScratchReg, TestedReg);
2187
2188 if (Method == AuthCheckMethod::XPAC) {
2189 // xpac(i|d) Xscratch
2190 unsigned XPACOpc = getXPACOpcodeForKey(Key);
2191 EmitToStreamer(
2192 MCInstBuilder(XPACOpc).addReg(ScratchReg).addReg(ScratchReg));
2193 } else {
2194 // xpaclri
2195
2196 // Note that this method applies XPAC to TestedReg instead of ScratchReg.
2197 assert(TestedReg == AArch64::LR &&
2198 "XPACHint mode is only compatible with checking the LR register");
2200 "XPACHint mode is only compatible with I-keys");
2201 EmitToStreamer(MCInstBuilder(AArch64::XPACLRI));
2202 }
2203
2204 // cmp Xtested, Xscratch
2205 EmitToStreamer(MCInstBuilder(AArch64::SUBSXrs)
2206 .addReg(AArch64::XZR)
2207 .addReg(TestedReg)
2208 .addReg(ScratchReg)
2209 .addImm(0));
2210
2211 // b.eq Lsuccess
2212 EmitToStreamer(
2213 MCInstBuilder(AArch64::Bcc)
2214 .addImm(AArch64CC::EQ)
2215 .addExpr(MCSymbolRefExpr::create(SuccessSym, OutContext)));
2216 } else if (Method == AuthCheckMethod::HighBitsNoTBI) {
2217 // eor Xscratch, Xtested, Xtested, lsl #1
2218 EmitToStreamer(MCInstBuilder(AArch64::EORXrs)
2219 .addReg(ScratchReg)
2220 .addReg(TestedReg)
2221 .addReg(TestedReg)
2222 .addImm(1));
2223 // tbz Xscratch, #62, Lsuccess
2224 EmitToStreamer(
2225 MCInstBuilder(AArch64::TBZX)
2226 .addReg(ScratchReg)
2227 .addImm(62)
2228 .addExpr(MCSymbolRefExpr::create(SuccessSym, OutContext)));
2229 } else {
2230 llvm_unreachable("Unsupported check method");
2231 }
2232
2233 if (!OnFailure) {
2234 // Trapping sequences do a 'brk'.
2235 // brk #<0xc470 + aut key>
2236 EmitToStreamer(MCInstBuilder(AArch64::BRK).addImm(0xc470 | Key));
2237 } else {
2238 // Non-trapping checked sequences return the stripped result in TestedReg,
2239 // skipping over success-only code (such as re-signing the pointer) by
2240 // jumping to OnFailure label.
2241 // Note that this can introduce an authentication oracle (such as based on
2242 // the high bits of the re-signed value).
2243
2244 // FIXME: The XPAC method can be optimized by applying XPAC to TestedReg
2245 // instead of ScratchReg, thus eliminating one `mov` instruction.
2246 // Both XPAC and XPACHint can be further optimized by not using a
2247 // conditional branch jumping over an unconditional one.
2248
2249 switch (Method) {
2250 case AuthCheckMethod::XPACHint:
2251 // LR is already XPAC-ed at this point.
2252 break;
2253 case AuthCheckMethod::XPAC:
2254 // mov Xtested, Xscratch
2255 emitMovXReg(TestedReg, ScratchReg);
2256 break;
2257 default:
2258 // If Xtested was not XPAC-ed so far, emit XPAC here.
2259 // xpac(i|d) Xtested
2260 unsigned XPACOpc = getXPACOpcodeForKey(Key);
2261 EmitToStreamer(
2262 MCInstBuilder(XPACOpc).addReg(TestedReg).addReg(TestedReg));
2263 }
2264
2265 // b Lend
2266 const auto *OnFailureExpr = MCSymbolRefExpr::create(OnFailure, OutContext);
2267 EmitToStreamer(MCInstBuilder(AArch64::B).addExpr(OnFailureExpr));
2268 }
2269
2270 // If the auth check succeeds, we can continue.
2271 // Lsuccess:
2272 OutStreamer->emitLabel(SuccessSym);
2273}
2274
2275// With Pointer Authentication, it may be needed to explicitly check the
2276// authenticated value in LR before performing a tail call.
2277// Otherwise, the callee may re-sign the invalid return address,
2278// introducing a signing oracle.
2279void AArch64AsmPrinter::emitPtrauthTailCallHardening(const MachineInstr *TC) {
2280 if (!AArch64FI->shouldSignReturnAddress(*MF))
2281 return;
2282
2283 auto LRCheckMethod = STI->getAuthenticatedLRCheckMethod(*MF);
2284 if (LRCheckMethod == AArch64PAuth::AuthCheckMethod::None)
2285 return;
2286
2287 const AArch64RegisterInfo *TRI = STI->getRegisterInfo();
2288 Register ScratchReg =
2289 TC->readsRegister(AArch64::X16, TRI) ? AArch64::X17 : AArch64::X16;
2290 assert(!TC->readsRegister(ScratchReg, TRI) &&
2291 "Neither x16 nor x17 is available as a scratch register");
2294 emitPtrauthCheckAuthenticatedValue(AArch64::LR, ScratchReg, Key,
2295 LRCheckMethod);
2296}
2297
2298bool AArch64AsmPrinter::emitDeactivationSymbolRelocation(Value *DS) {
2299 if (!DS)
2300 return false;
2301
2302 if (isa<GlobalAlias>(DS)) {
2303 // Just emit the nop directly.
2304 EmitToStreamer(MCInstBuilder(AArch64::NOP));
2305 return true;
2306 }
2307 MCSymbol *Dot = OutContext.createTempSymbol();
2308 OutStreamer->emitLabel(Dot);
2309 const MCExpr *DeactDotExpr = MCSymbolRefExpr::create(Dot, OutContext);
2310
2311 const MCExpr *DSExpr = MCSymbolRefExpr::create(
2312 OutContext.getOrCreateSymbol(DS->getName()), OutContext);
2313 OutStreamer->emitRelocDirective(*DeactDotExpr, "R_AARCH64_PATCHINST", DSExpr,
2314 SMLoc());
2315 return false;
2316}
2317
2318AArch64AsmPrinter::PtrAuthSchema AArch64AsmPrinter::PtrAuthSchema::CreateImmReg(
2319 AArch64PACKey::ID Key, uint64_t IntDisc, const MachineOperand &AddrDiscOp) {
2320 PtrAuthSchema Schema;
2321 Schema.Key = Key;
2322 Schema.IntDisc = IntDisc;
2323 Schema.AddrDisc = AddrDiscOp.getReg();
2324 Schema.AddrDiscIsKilled = AddrDiscOp.isKill();
2325 Schema.PCDisc = Register();
2326 return Schema;
2327}
2328
2329AArch64AsmPrinter::PtrAuthSchema AArch64AsmPrinter::PtrAuthSchema::CreateRegReg(
2330 AArch64PACKey::ID Key, Register AddrDisc, Register PCDisc) {
2331 assert(PCDisc.isValid() && "Use CreateImmReg for non-PC schemas");
2332 PtrAuthSchema Schema;
2333 Schema.Key = Key;
2334 Schema.IntDisc = 0;
2335 Schema.AddrDisc = AddrDisc;
2336 Schema.AddrDiscIsKilled = false;
2337 Schema.PCDisc = PCDisc;
2338 return Schema;
2339}
2340
2341void AArch64AsmPrinter::emitPtrauthApplyIndirectAddend(Register Pointer,
2342 Register Scratch,
2343 int64_t Addend) {
2344 if (isInt<9>(Addend)) {
2345 // ldrsw Scratch, [Pointer, #Addend]! ; note: Pointer+Addend is used later.
2346 EmitToStreamer(MCInstBuilder(AArch64::LDRSWpre)
2347 .addReg(Pointer)
2348 .addReg(Scratch)
2349 .addReg(Pointer)
2350 .addImm(/*simm9:*/ Addend));
2351 } else {
2352 // Pointer += Addend computation has 2 variants
2353 if (isUInt<24>(Addend)) {
2354 // Variant 1: add Pointer, Pointer, (Addend >> shift12) lsl shift12
2355 // This can take up to 2 instructions.
2356 for (int BitPos = 0; BitPos != 24 && (Addend >> BitPos); BitPos += 12) {
2357 EmitToStreamer(
2358 MCInstBuilder(AArch64::ADDXri)
2359 .addReg(Pointer)
2360 .addReg(Pointer)
2361 .addImm((Addend >> BitPos) & 0xfff)
2362 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, BitPos)));
2363 }
2364 } else {
2365 // Variant 2: accumulate constant in Scratch 16 bits at a time,
2366 // and add it to Pointer. This can take 2-5 instructions.
2367 emitMOVZ(Scratch, Addend & 0xffff, 0);
2368 for (int Offset = 16; Offset < 64; Offset += 16) {
2369 if (unsigned Fragment = (Addend >> Offset) & 0xffff)
2370 emitMOVK(Scratch, Fragment, Offset);
2371 }
2372
2373 // add Pointer, Pointer, Scratch
2374 EmitToStreamer(MCInstBuilder(AArch64::ADDXrs)
2375 .addReg(Pointer)
2376 .addReg(Pointer)
2377 .addReg(Scratch)
2378 .addImm(0));
2379 }
2380 // ldrsw Scratch, [Pointer]
2381 EmitToStreamer(MCInstBuilder(AArch64::LDRSWui)
2382 .addReg(Scratch)
2383 .addReg(Pointer)
2384 .addImm(0));
2385 }
2386 // add Pointer, Pointer, Scratch
2387 EmitToStreamer(MCInstBuilder(AArch64::ADDXrs)
2388 .addReg(Pointer)
2389 .addReg(Pointer)
2390 .addReg(Scratch)
2391 .addImm(0));
2392}
2393
2394static PtrauthCheckMode getCheckMode(const MachineFunction *MF) {
2396
2397 // If an override is passed via command line argument, just use that value.
2398 if (std::optional<PtrauthCheckMode> Mode =
2399 STI.getCLOpts().ptrauth_auth_checks)
2400 return *Mode;
2401
2402 // Otherwise, on an FPAC CPU, you get traps whether you want them or not:
2403 // there's no point in emitting checks or traps.
2404 if (STI.hasFPAC())
2405 return PtrauthCheckMode::Unchecked;
2406
2407 bool ShouldTrap = MF->getFunction().hasFnAttribute("ptrauth-auth-traps");
2408 return ShouldTrap ? PtrauthCheckMode::Trap : PtrauthCheckMode::Poison;
2409}
2410
2411// We expand non-signing AUT* pseudo instructions into a sequence of the form
2412//
2413// ; 1. Authenticate Pointer
2414//
2415// or
2416//
2417// ; 1. Authenticate Pointer
2418// ; 2. Check that Pointer is valid, trap otherwise
2419//
2420// We expand AUT*PAC pseudo instructions into a sequence of the form
2421// (with addend only applied if Addend argument is given):
2422//
2423// ; 1. Authenticate Pointer
2424// ; 3. Apply addend and sign Pointer
2425//
2426// or
2427//
2428// ; 1. Authenticate Pointer
2429// ; 2. Check that Pointer is valid, trap otherwise
2430// ; 3. Apply addend and sign Pointer
2431//
2432// or
2433//
2434// ; 1. Authenticate Pointer
2435// ; 2. Check that Pointer is valid, jump to .Lon_failure otherwise
2436// ; 3. Apply addend and sign Pointer
2437// .Lon_failure:
2438//
2439void AArch64AsmPrinter::emitPtrauthAuthResign(
2440 Register Pointer, Register Scratch, PtrAuthSchema AuthSchema,
2441 std::optional<PtrAuthSchema> SignSchema, std::optional<int64_t> Addend,
2442 Value *DS) {
2443 const PtrauthCheckMode CheckMode = getCheckMode(MF);
2444 const bool IsAuthWithPC = AuthSchema.PCDisc.isValid();
2445 assert(!SignSchema || !SignSchema->PCDisc.isValid());
2446
2447 Register SignAddrDiscOrNone = SignSchema ? SignSchema->AddrDisc : Register();
2448
2449 // 1. Authenticate Pointer - this is the only common step.
2450 // It is more complex than signing because AUTI[AB]171615 may be used.
2451
2452 if (IsAuthWithPC) {
2453 assert(Pointer == AArch64::X17 && Scratch == AArch64::X16 &&
2454 "AUTPCPAC must use x17/x16 as Pointer/Scratch");
2455
2456 assert(AuthSchema.AddrDisc == AArch64::X16 &&
2457 "AUTPCPAC requires address discriminator in X16");
2458
2459 assert(AuthSchema.PCDisc == AArch64::X15 &&
2460 "AUTPCPAC requires PC discriminator in X15");
2461
2462 assert(AuthSchema.IntDisc == 0 && "AUTPCPAC does not support IntDisc");
2463
2464 assert((AuthSchema.Key == AArch64PACKey::IB ||
2465 AuthSchema.Key == AArch64PACKey::IA) &&
2466 "AUTPCPAC only supports AUT-ing with IA/IB");
2467
2468 if (!emitDeactivationSymbolRelocation(DS)) {
2469 unsigned AutOpc = (AuthSchema.Key == AArch64PACKey::IB)
2470 ? AArch64::AUTIB171615
2471 : AArch64::AUTIA171615;
2472 EmitToStreamer(MCInstBuilder(AutOpc));
2473 }
2474 } else {
2475 // emitPtrauthDiscriminator is allowed to clobber AuthSchema.AddrDisc as
2476 // long as it is not used past this point neither externally (the register
2477 // operand is "killed"), nor internally (it does not alias anything being
2478 // used later by this pseudo instruction).
2479 //
2480 // Note that, while rather unlikely, it is technically possible to use the
2481 // Pointer to compute its own discriminator.
2482 Register AUTDiscReg = emitPtrauthDiscriminator(
2483 AuthSchema.IntDisc, AuthSchema.AddrDisc, Scratch,
2484 AuthSchema.addrDiscIsKilledAndNoneOf({Pointer, SignAddrDiscOrNone}));
2485 if (!emitDeactivationSymbolRelocation(DS))
2486 emitAUT(AuthSchema.Key, Pointer, AUTDiscReg);
2487 }
2488
2489 // The other two steps are optional, define lambdas for them:
2490 // 2. Check that Pointer is valid, on failure jump to label or trap.
2491 auto EmitCheck = [&](MCSymbol *OnFailure = nullptr) {
2492 emitPtrauthCheckAuthenticatedValue(Pointer, Scratch, AuthSchema.Key,
2493 AArch64PAuth::AuthCheckMethod::XPAC,
2494 OnFailure);
2495 };
2496 // 3. Apply addend and sign Pointer.
2497 auto EmitResignOnSuccess = [&]() {
2498 if (Addend.has_value())
2499 emitPtrauthApplyIndirectAddend(Pointer, Scratch, *Addend);
2500
2501 assert(Pointer != SignSchema->AddrDisc && "Pointer is early-clobbered");
2502 Register PACDiscReg =
2503 emitPtrauthDiscriminator(SignSchema->IntDisc, SignSchema->AddrDisc,
2504 Scratch, SignSchema->AddrDiscIsKilled);
2505 emitPAC(SignSchema->Key, Pointer, PACDiscReg);
2506 };
2507
2508 // Emit checking and resigning as needed.
2509
2510 if (!SignSchema) {
2511 if (CheckMode == PtrauthCheckMode::Trap)
2512 EmitCheck();
2513 // For authentication-only pseudos, Poison is demoted to Unchecked.
2514 return;
2515 }
2516
2517 switch (CheckMode) {
2518 case PtrauthCheckMode::Unchecked:
2519 EmitResignOnSuccess();
2520 break;
2521 case PtrauthCheckMode::Trap:
2522 EmitCheck();
2523 EmitResignOnSuccess();
2524 break;
2525 case PtrauthCheckMode::Poison:
2526 MCSymbol *OnFailure = createTempSymbol("resign_end_");
2527 EmitCheck(OnFailure);
2528 EmitResignOnSuccess();
2529 OutStreamer->emitLabel(OnFailure);
2530 break;
2531 }
2532}
2533
2534void AArch64AsmPrinter::emitPtrauthSign(const MachineInstr *MI) {
2535 Register Val = MI->getOperand(1).getReg();
2536 auto Key = (AArch64PACKey::ID)MI->getOperand(2).getImm();
2537 uint64_t Disc = MI->getOperand(3).getImm();
2538 Register AddrDisc = MI->getOperand(4).getReg();
2539 bool AddrDiscKilled = MI->getOperand(4).isKill();
2540
2541 // As long as at least one of Val and AddrDisc is in GPR64noip, a scratch
2542 // register is available.
2543 Register ScratchReg = Val == AArch64::X16 ? AArch64::X17 : AArch64::X16;
2544 assert(ScratchReg != AddrDisc &&
2545 "Neither X16 nor X17 is available as a scratch register");
2546
2547 // Compute pac discriminator
2548 Register DiscReg = emitPtrauthDiscriminator(
2549 Disc, AddrDisc, ScratchReg, /*MayClobberAddrDisc=*/AddrDiscKilled);
2550
2551 if (emitDeactivationSymbolRelocation(MI->getDeactivationSymbol()))
2552 return;
2553
2554 emitPAC(Key, Val, DiscReg);
2555}
2556
2557void AArch64AsmPrinter::emitPtrauthBranch(const MachineInstr *MI) {
2558 bool IsCall = MI->getOpcode() == AArch64::BLRA;
2559 unsigned BrTarget = MI->getOperand(0).getReg();
2560
2561 auto Key = (AArch64PACKey::ID)MI->getOperand(1).getImm();
2562 uint64_t Disc = MI->getOperand(2).getImm();
2563
2564 unsigned AddrDisc = MI->getOperand(3).getReg();
2565
2566 // Make sure AddrDisc is solely used to compute the discriminator.
2567 // While hardly meaningful, it is still possible to describe an authentication
2568 // of a pointer against its own value (instead of storage address) with
2569 // intrinsics, so use report_fatal_error instead of assert.
2570 if (BrTarget == AddrDisc)
2571 report_fatal_error("Branch target is signed with its own value");
2572
2573 // If we are printing BLRA pseudo, try to save one MOV by making use of the
2574 // fact that x16 and x17 are described as clobbered by the MI instruction and
2575 // AddrDisc is not used as any other input.
2576 //
2577 // Back in the day, emitPtrauthDiscriminator was restricted to only returning
2578 // either x16 or x17, meaning the returned register is always among the
2579 // implicit-def'ed registers of BLRA pseudo. Now this property can be violated
2580 // if isX16X17Safer predicate is false, thus manually check if AddrDisc is
2581 // among x16 and x17 to prevent clobbering unexpected registers.
2582 //
2583 // Unlike BLRA, BRA pseudo is used to perform computed goto, and thus not
2584 // declared as clobbering x16/x17.
2585 //
2586 // FIXME: Make use of `killed` flags and register masks instead.
2587 bool AddrDiscIsImplicitDef =
2588 IsCall && (AddrDisc == AArch64::X16 || AddrDisc == AArch64::X17);
2589 Register DiscReg = emitPtrauthDiscriminator(Disc, AddrDisc, AArch64::X17,
2590 AddrDiscIsImplicitDef);
2591 emitBLRA(IsCall, Key, BrTarget, DiscReg);
2592}
2593
2594void AArch64AsmPrinter::emitAddImm(MCRegister Reg, int64_t Addend,
2595 MCRegister Tmp) {
2596 if (Addend != 0) {
2597 const uint64_t AbsOffset = (Addend > 0 ? Addend : -((uint64_t)Addend));
2598 const bool IsNeg = Addend < 0;
2599 if (isUInt<24>(AbsOffset)) {
2600 for (int BitPos = 0; BitPos != 24 && (AbsOffset >> BitPos);
2601 BitPos += 12) {
2602 EmitToStreamer(
2603 MCInstBuilder(IsNeg ? AArch64::SUBXri : AArch64::ADDXri)
2604 .addReg(Reg)
2605 .addReg(Reg)
2606 .addImm((AbsOffset >> BitPos) & 0xfff)
2607 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, BitPos)));
2608 }
2609 } else {
2610 const uint64_t UAddend = Addend;
2611 EmitToStreamer(MCInstBuilder(IsNeg ? AArch64::MOVNXi : AArch64::MOVZXi)
2612 .addReg(Tmp)
2613 .addImm((IsNeg ? ~UAddend : UAddend) & 0xffff)
2614 .addImm(/*shift=*/0));
2615 auto NeedMovk = [IsNeg, UAddend](int BitPos) -> bool {
2616 assert(BitPos == 16 || BitPos == 32 || BitPos == 48);
2617 uint64_t Shifted = UAddend >> BitPos;
2618 if (!IsNeg)
2619 return Shifted != 0;
2620 for (int I = 0; I != 64 - BitPos; I += 16)
2621 if (((Shifted >> I) & 0xffff) != 0xffff)
2622 return true;
2623 return false;
2624 };
2625 for (int BitPos = 16; BitPos != 64 && NeedMovk(BitPos); BitPos += 16)
2626 emitMOVK(Tmp, (UAddend >> BitPos) & 0xffff, BitPos);
2627
2628 EmitToStreamer(MCInstBuilder(AArch64::ADDXrs)
2629 .addReg(Reg)
2630 .addReg(Reg)
2631 .addReg(Tmp)
2632 .addImm(/*shift=*/0));
2633 }
2634 }
2635}
2636
2637void AArch64AsmPrinter::emitAddress(MCRegister Reg, const MCExpr *Expr,
2638 MCRegister Tmp, bool DSOLocal,
2639 const MCSubtargetInfo &STI) {
2640 MCValue Val;
2641 if (!Expr->evaluateAsRelocatable(Val, nullptr))
2642 report_fatal_error("emitAddress could not evaluate");
2643 if (DSOLocal) {
2644 EmitToStreamer(
2645 MCInstBuilder(AArch64::ADRP)
2646 .addReg(Reg)
2648 OutStreamer->getContext())));
2649 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
2650 .addReg(Reg)
2651 .addReg(Reg)
2652 .addExpr(MCSpecifierExpr::create(
2653 Expr, AArch64::S_LO12, OutStreamer->getContext()))
2654 .addImm(0));
2655 } else {
2656 auto *SymRef =
2657 MCSymbolRefExpr::create(Val.getAddSym(), OutStreamer->getContext());
2658 EmitToStreamer(
2659 MCInstBuilder(AArch64::ADRP)
2660 .addReg(Reg)
2662 OutStreamer->getContext())));
2663 EmitToStreamer(
2664 MCInstBuilder(AArch64::LDRXui)
2665 .addReg(Reg)
2666 .addReg(Reg)
2668 OutStreamer->getContext())));
2669 emitAddImm(Reg, Val.getConstant(), Tmp);
2670 }
2671}
2672
2674 // IFUNCs are ELF-only.
2675 if (!TT.isOSBinFormatELF())
2676 return false;
2677
2678 // IFUNCs are supported on glibc, bionic, and some but not all of the BSDs.
2679 return TT.isOSGlibc() || TT.isAndroid() || TT.isOSFreeBSD() ||
2680 TT.isOSDragonFly() || TT.isOSNetBSD();
2681}
2682
2683// Emit an ifunc resolver that returns a signed pointer to the specified target,
2684// and return a FUNCINIT reference to the resolver. In the linked binary, this
2685// function becomes the target of an IRELATIVE relocation. This resolver is used
2686// to relocate signed pointers in global variable initializers in special cases
2687// where the standard R_AARCH64_AUTH_ABS64 relocation would not work.
2688//
2689// Example (signed null pointer, not address discriminated):
2690//
2691// .8byte .Lpauth_ifunc0
2692// .pushsection .text.startup,"ax",@progbits
2693// .Lpauth_ifunc0:
2694// mov x0, #0
2695// mov x1, #12345
2696// b __emupac_pacda
2697//
2698// Example (signed null pointer, address discriminated):
2699//
2700// .Ltmp:
2701// .8byte .Lpauth_ifunc0
2702// .pushsection .text.startup,"ax",@progbits
2703// .Lpauth_ifunc0:
2704// mov x0, #0
2705// adrp x1, .Ltmp
2706// add x1, x1, :lo12:.Ltmp
2707// b __emupac_pacda
2708// .popsection
2709//
2710// Example (signed pointer to symbol, not address discriminated):
2711//
2712// .Ltmp:
2713// .8byte .Lpauth_ifunc0
2714// .pushsection .text.startup,"ax",@progbits
2715// .Lpauth_ifunc0:
2716// adrp x0, symbol
2717// add x0, x0, :lo12:symbol
2718// mov x1, #12345
2719// b __emupac_pacda
2720// .popsection
2721//
2722// Example (signed null pointer, not address discriminated, with deactivation
2723// symbol ds):
2724//
2725// .8byte .Lpauth_ifunc0
2726// .pushsection .text.startup,"ax",@progbits
2727// .Lpauth_ifunc0:
2728// mov x0, #0
2729// mov x1, #12345
2730// .reloc ., R_AARCH64_PATCHINST, ds
2731// b __emupac_pacda
2732// ret
2733// .popsection
2734const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
2735 const MCExpr *Target, uint64_t Disc, AArch64PACKey::ID KeyID,
2736 bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr) {
2737 const Triple &TT = TM.getTargetTriple();
2738
2739 // We only emit an IRELATIVE relocation if the target supports IRELATIVE.
2741 return nullptr;
2742
2743 // For now, only the DA key is supported.
2744 if (KeyID != AArch64PACKey::DA)
2745 return nullptr;
2746
2747 // AArch64Subtarget is huge, so heap allocate it so we don't run out of stack
2748 // space.
2749 auto STI = std::make_unique<AArch64Subtarget>(
2750 TT, TM.getTargetCPU(), TM.getTargetCPU(), TM.getTargetFeatureString(), TM,
2751 true);
2752 // This may be called while emitting a function, whose subtarget is restored.
2753 SaveAndRestore<const AArch64Subtarget *> SavedSTI(this->STI, STI.get());
2754
2755 MCSymbol *Place = OutStreamer->getContext().createTempSymbol();
2756 OutStreamer->emitLabel(Place);
2757 OutStreamer->pushSection();
2758
2759 const MCSymbolELF *Group =
2760 static_cast<MCSectionELF *>(OutStreamer->getCurrentSectionOnly())
2761 ->getGroup();
2763 if (Group)
2765 OutStreamer->switchSection(OutStreamer->getContext().getELFSection(
2766 ".text.startup", ELF::SHT_PROGBITS, Flags, 0, Group, true,
2767 Group ? MCSection::NonUniqueID : PAuthIFuncNextUniqueID++, nullptr));
2768
2769 MCSymbol *IRelativeSym =
2770 OutStreamer->getContext().createLinkerPrivateSymbol("pauth_ifunc");
2771 OutStreamer->emitLabel(IRelativeSym);
2772 if (isa<MCConstantExpr>(Target)) {
2773 OutStreamer->emitInstruction(MCInstBuilder(AArch64::MOVZXi)
2774 .addReg(AArch64::X0)
2775 .addExpr(Target)
2776 .addImm(0),
2777 *STI);
2778 } else {
2779 emitAddress(AArch64::X0, Target, AArch64::X16, IsDSOLocal, *STI);
2780 }
2781 if (HasAddressDiversity) {
2782 auto *PlacePlusDisc = MCBinaryExpr::createAdd(
2783 MCSymbolRefExpr::create(Place, OutStreamer->getContext()),
2784 MCConstantExpr::create(Disc, OutStreamer->getContext()),
2785 OutStreamer->getContext());
2786 emitAddress(AArch64::X1, PlacePlusDisc, AArch64::X16, /*IsDSOLocal=*/true,
2787 *STI);
2788 } else {
2789 if (!isUInt<16>(Disc)) {
2790 OutContext.reportError(SMLoc(), "AArch64 PAC Discriminator '" +
2791 Twine(Disc) +
2792 "' out of range [0, 0xFFFF]");
2793 }
2794 emitMOVZ(AArch64::X1, Disc, 0);
2795 }
2796
2797 if (DSExpr) {
2798 MCSymbol *PrePACInst = OutStreamer->getContext().createTempSymbol();
2799 OutStreamer->emitLabel(PrePACInst);
2800
2801 auto *PrePACInstExpr =
2802 MCSymbolRefExpr::create(PrePACInst, OutStreamer->getContext());
2803 OutStreamer->emitRelocDirective(*PrePACInstExpr, "R_AARCH64_PATCHINST",
2804 DSExpr, SMLoc());
2805 }
2806
2807 // We don't know the subtarget because this is being emitted for a global
2808 // initializer. Because the performance of IFUNC resolvers is unimportant, we
2809 // always call the EmuPAC runtime, which will end up using the PAC instruction
2810 // if the target supports PAC.
2811 MCSymbol *EmuPAC =
2812 OutStreamer->getContext().getOrCreateSymbol("__emupac_pacda");
2813 const MCSymbolRefExpr *EmuPACRef =
2814 MCSymbolRefExpr::create(EmuPAC, OutStreamer->getContext());
2815 OutStreamer->emitInstruction(MCInstBuilder(AArch64::B).addExpr(EmuPACRef),
2816 *STI);
2817
2818 // We need a RET despite the above tail call because the deactivation symbol
2819 // may replace the tail call with a NOP.
2820 if (DSExpr)
2821 OutStreamer->emitInstruction(
2822 MCInstBuilder(AArch64::RET).addReg(AArch64::LR), *STI);
2823 OutStreamer->popSection();
2824
2826 MCSymbolRefExpr::create(IRelativeSym, OutStreamer->getContext()),
2827 AArch64::S_FUNCINIT, OutStreamer->getContext());
2828}
2829
2830const MCExpr *
2831AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
2832 MCContext &Ctx = OutContext;
2833
2834 // Figure out the base symbol and the addend, if any.
2835 APInt Offset(64, 0);
2836 const Value *BaseGV = CPA.getPointer()->stripAndAccumulateConstantOffsets(
2837 getDataLayout(), Offset, /*AllowNonInbounds=*/true);
2838
2839 auto *BaseGVB = dyn_cast<GlobalValue>(BaseGV);
2840
2841 const MCExpr *Sym;
2842 if (BaseGVB) {
2843 // If there is an addend, turn that into the appropriate MCExpr.
2844 Sym = MCSymbolRefExpr::create(getSymbol(BaseGVB), Ctx);
2845 if (Offset.sgt(0))
2847 Sym, MCConstantExpr::create(Offset.getSExtValue(), Ctx), Ctx);
2848 else if (Offset.slt(0))
2850 Sym, MCConstantExpr::create((-Offset).getSExtValue(), Ctx), Ctx);
2851 } else if (isa<ConstantPointerNull>(BaseGV)) {
2852 Sym = MCConstantExpr::create(Offset.getSExtValue(), Ctx);
2853 } else {
2854 reportFatalUsageError("unsupported constant expression in ptrauth pointer");
2855 }
2856
2857 const MCExpr *DSExpr = nullptr;
2858 if (auto *DS = dyn_cast<GlobalValue>(CPA.getDeactivationSymbol())) {
2859 if (isa<GlobalAlias>(DS))
2860 return Sym;
2861 DSExpr = MCSymbolRefExpr::create(getSymbol(DS), Ctx);
2862 }
2863
2864 uint64_t KeyID = CPA.getKey()->getZExtValue();
2865 // We later rely on valid KeyID value in AArch64PACKeyIDToString call from
2866 // AArch64AuthMCExpr::printImpl, so fail fast.
2867 if (KeyID > AArch64PACKey::LAST) {
2868 CPA.getContext().emitError("AArch64 PAC Key ID '" + Twine(KeyID) +
2869 "' out of range [0, " +
2870 Twine((unsigned)AArch64PACKey::LAST) + "]");
2871 KeyID = 0;
2872 }
2873
2874 uint64_t Disc = CPA.getDiscriminator()->getZExtValue();
2875
2876 // Check if we can represent this with an IRELATIVE and emit it if so.
2877 if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
2878 Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
2879 BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
2880 return IFuncSym;
2881
2882 if (!isUInt<16>(Disc)) {
2883 CPA.getContext().emitError("AArch64 PAC Discriminator '" + Twine(Disc) +
2884 "' out of range [0, 0xFFFF]");
2885 Disc = 0;
2886 }
2887
2888 if (DSExpr)
2889 report_fatal_error("deactivation symbols unsupported in constant "
2890 "expressions on this target");
2891
2892 // Finally build the complete @AUTH expr.
2893 return AArch64AuthMCExpr::create(Sym, Disc, AArch64PACKey::ID(KeyID),
2894 CPA.hasAddressDiscriminator(), Ctx);
2895}
2896
2897void AArch64AsmPrinter::LowerLOADauthptrstatic(const MachineInstr &MI) {
2898 unsigned DstReg = MI.getOperand(0).getReg();
2899 const MachineOperand &GAOp = MI.getOperand(1);
2900 const uint64_t KeyC = MI.getOperand(2).getImm();
2901 assert(KeyC <= AArch64PACKey::LAST &&
2902 "key is out of range [0, AArch64PACKey::LAST]");
2903 const auto Key = (AArch64PACKey::ID)KeyC;
2904 const uint64_t Disc = MI.getOperand(3).getImm();
2905 assert(isUInt<16>(Disc) &&
2906 "constant discriminator is out of range [0, 0xffff]");
2907
2908 // Emit instruction sequence like the following:
2909 // ADRP x16, symbol$auth_ptr$key$disc
2910 // LDR x16, [x16, :lo12:symbol$auth_ptr$key$disc]
2911 //
2912 // Where the $auth_ptr$ symbol is the stub slot containing the signed pointer
2913 // to symbol.
2914 MCSymbol *AuthPtrStubSym;
2915 if (TM.getTargetTriple().isOSBinFormatELF()) {
2916 const auto &TLOF =
2917 static_cast<const AArch64_ELFTargetObjectFile &>(getObjFileLowering());
2918
2919 assert(GAOp.getOffset() == 0 &&
2920 "non-zero offset for $auth_ptr$ stub slots is not supported");
2921 const MCSymbol *GASym = TM.getSymbol(GAOp.getGlobal());
2922 AuthPtrStubSym = TLOF.getAuthPtrSlotSymbol(TM, MMI, GASym, Key, Disc);
2923 } else {
2924 assert(TM.getTargetTriple().isOSBinFormatMachO() &&
2925 "LOADauthptrstatic is implemented only for MachO/ELF");
2926
2927 const auto &TLOF = static_cast<const AArch64_MachoTargetObjectFile &>(
2928 getObjFileLowering());
2929
2930 assert(GAOp.getOffset() == 0 &&
2931 "non-zero offset for $auth_ptr$ stub slots is not supported");
2932 const MCSymbol *GASym = TM.getSymbol(GAOp.getGlobal());
2933 AuthPtrStubSym = TLOF.getAuthPtrSlotSymbol(TM, MMI, GASym, Key, Disc);
2934 }
2935
2936 MachineOperand StubMOHi =
2938 MachineOperand StubMOLo = MachineOperand::CreateMCSymbol(
2939 AuthPtrStubSym, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2940 MCOperand StubMCHi, StubMCLo;
2941
2942 MCInstLowering.lowerOperand(StubMOHi, StubMCHi);
2943 MCInstLowering.lowerOperand(StubMOLo, StubMCLo);
2944
2945 EmitToStreamer(
2946 *OutStreamer,
2947 MCInstBuilder(AArch64::ADRP).addReg(DstReg).addOperand(StubMCHi));
2948
2949 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::LDRXui)
2950 .addReg(DstReg)
2951 .addReg(DstReg)
2952 .addOperand(StubMCLo));
2953}
2954
2955void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
2956 const bool IsGOTLoad = MI.getOpcode() == AArch64::LOADgotPAC;
2957 const bool IsELFSignedGOT = MI.getParent()
2958 ->getParent()
2959 ->getInfo<AArch64FunctionInfo>()
2960 ->hasELFSignedGOT();
2961 MachineOperand GAOp = MI.getOperand(0);
2962 const uint64_t KeyC = MI.getOperand(1).getImm();
2963 assert(KeyC <= AArch64PACKey::LAST &&
2964 "key is out of range [0, AArch64PACKey::LAST]");
2965 const auto Key = (AArch64PACKey::ID)KeyC;
2966 const unsigned AddrDisc = MI.getOperand(2).getReg();
2967 const uint64_t Disc = MI.getOperand(3).getImm();
2968
2969 const int64_t Offset = GAOp.getOffset();
2970 GAOp.setOffset(0);
2971
2972 // Emit:
2973 // target materialization:
2974 // - via GOT:
2975 // - unsigned GOT:
2976 // adrp x16, :got:target
2977 // ldr x16, [x16, :got_lo12:target]
2978 // add offset to x16 if offset != 0
2979 // - ELF signed GOT:
2980 // adrp x17, :got:target
2981 // add x17, x17, :got_auth_lo12:target
2982 // ldr x16, [x17]
2983 // aut{i|d}a x16, x17
2984 // check+trap sequence (if no FPAC)
2985 // add offset to x16 if offset != 0
2986 //
2987 // - direct:
2988 // adrp x16, target
2989 // add x16, x16, :lo12:target
2990 // add offset to x16 if offset != 0
2991 //
2992 // add offset to x16:
2993 // - abs(offset) fits 24 bits:
2994 // add/sub x16, x16, #<offset>[, #lsl 12] (up to 2 instructions)
2995 // - abs(offset) does not fit 24 bits:
2996 // - offset < 0:
2997 // movn+movk sequence filling x17 register with the offset (up to 4
2998 // instructions)
2999 // add x16, x16, x17
3000 // - offset > 0:
3001 // movz+movk sequence filling x17 register with the offset (up to 4
3002 // instructions)
3003 // add x16, x16, x17
3004 //
3005 // signing:
3006 // - 0 discriminator:
3007 // paciza x16
3008 // - Non-0 discriminator, no address discriminator:
3009 // mov x17, #Disc
3010 // pacia x16, x17
3011 // - address discriminator (with potentially folded immediate discriminator):
3012 // pacia x16, xAddrDisc
3013
3014 MachineOperand GAMOHi(GAOp), GAMOLo(GAOp);
3015 MCOperand GAMCHi, GAMCLo;
3016
3017 GAMOHi.setTargetFlags(AArch64II::MO_PAGE);
3018 GAMOLo.setTargetFlags(AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
3019 if (IsGOTLoad) {
3020 GAMOHi.addTargetFlag(AArch64II::MO_GOT);
3021 GAMOLo.addTargetFlag(AArch64II::MO_GOT);
3022 }
3023
3024 MCInstLowering.lowerOperand(GAMOHi, GAMCHi);
3025 MCInstLowering.lowerOperand(GAMOLo, GAMCLo);
3026
3027 EmitToStreamer(
3028 MCInstBuilder(AArch64::ADRP)
3029 .addReg(IsGOTLoad && IsELFSignedGOT ? AArch64::X17 : AArch64::X16)
3030 .addOperand(GAMCHi));
3031
3032 if (IsGOTLoad) {
3033 if (IsELFSignedGOT) {
3034 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
3035 .addReg(AArch64::X17)
3036 .addReg(AArch64::X17)
3037 .addOperand(GAMCLo)
3038 .addImm(0));
3039
3040 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
3041 .addReg(AArch64::X16)
3042 .addReg(AArch64::X17)
3043 .addImm(0));
3044
3045 assert(GAOp.isGlobal());
3046 assert(GAOp.getGlobal()->getValueType() != nullptr);
3047
3048 bool IsFunctionTy = GAOp.getGlobal()->getValueType()->isFunctionTy();
3049 auto AuthKey = IsFunctionTy ? AArch64PACKey::IA : AArch64PACKey::DA;
3050 emitAUT(AuthKey, AArch64::X16, AArch64::X17);
3051
3052 if (!STI->hasFPAC())
3053 emitPtrauthCheckAuthenticatedValue(AArch64::X16, AArch64::X17, AuthKey,
3054 AArch64PAuth::AuthCheckMethod::XPAC);
3055 } else {
3056 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
3057 .addReg(AArch64::X16)
3058 .addReg(AArch64::X16)
3059 .addOperand(GAMCLo));
3060 }
3061 } else {
3062 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
3063 .addReg(AArch64::X16)
3064 .addReg(AArch64::X16)
3065 .addOperand(GAMCLo)
3066 .addImm(0));
3067 }
3068
3069 emitAddImm(AArch64::X16, Offset, AArch64::X17);
3070 Register DiscReg = emitPtrauthDiscriminator(Disc, AddrDisc, AArch64::X17);
3071
3072 emitPAC(Key, AArch64::X16, DiscReg);
3073}
3074
3075void AArch64AsmPrinter::LowerLOADgotAUTH(const MachineInstr &MI) {
3076 Register DstReg = MI.getOperand(0).getReg();
3077 Register AuthResultReg = STI->hasFPAC() ? DstReg : AArch64::X16;
3078 const MachineOperand &GAMO = MI.getOperand(1);
3079 assert(GAMO.getOffset() == 0);
3080
3081 if (MI.getMF()->getTarget().getCodeModel() == CodeModel::Tiny) {
3082 MCOperand GAMC;
3083 MCInstLowering.lowerOperand(GAMO, GAMC);
3084 EmitToStreamer(
3085 MCInstBuilder(AArch64::ADR).addReg(AArch64::X17).addOperand(GAMC));
3086 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
3087 .addReg(AuthResultReg)
3088 .addReg(AArch64::X17)
3089 .addImm(0));
3090 } else {
3091 MachineOperand GAHiOp(GAMO);
3092 MachineOperand GALoOp(GAMO);
3093 GAHiOp.addTargetFlag(AArch64II::MO_PAGE);
3094 GALoOp.addTargetFlag(AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
3095
3096 MCOperand GAMCHi, GAMCLo;
3097 MCInstLowering.lowerOperand(GAHiOp, GAMCHi);
3098 MCInstLowering.lowerOperand(GALoOp, GAMCLo);
3099
3100 EmitToStreamer(
3101 MCInstBuilder(AArch64::ADRP).addReg(AArch64::X17).addOperand(GAMCHi));
3102
3103 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
3104 .addReg(AArch64::X17)
3105 .addReg(AArch64::X17)
3106 .addOperand(GAMCLo)
3107 .addImm(0));
3108
3109 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
3110 .addReg(AuthResultReg)
3111 .addReg(AArch64::X17)
3112 .addImm(0));
3113 }
3114
3115 assert(GAMO.isGlobal());
3116 MCSymbol *UndefWeakSym;
3117 if (GAMO.getGlobal()->hasExternalWeakLinkage()) {
3118 UndefWeakSym = createTempSymbol("undef_weak");
3119 EmitToStreamer(
3120 MCInstBuilder(AArch64::CBZX)
3121 .addReg(AuthResultReg)
3122 .addExpr(MCSymbolRefExpr::create(UndefWeakSym, OutContext)));
3123 }
3124
3125 assert(GAMO.getGlobal()->getValueType() != nullptr);
3126
3127 bool IsFunctionTy = GAMO.getGlobal()->getValueType()->isFunctionTy();
3128 auto AuthKey = IsFunctionTy ? AArch64PACKey::IA : AArch64PACKey::DA;
3129 emitAUT(AuthKey, AuthResultReg, AArch64::X17);
3130
3131 if (GAMO.getGlobal()->hasExternalWeakLinkage())
3132 OutStreamer->emitLabel(UndefWeakSym);
3133
3134 if (!STI->hasFPAC()) {
3135 emitPtrauthCheckAuthenticatedValue(AuthResultReg, AArch64::X17, AuthKey,
3136 AArch64PAuth::AuthCheckMethod::XPAC);
3137
3138 emitMovXReg(DstReg, AuthResultReg);
3139 }
3140}
3141
3142const MCExpr *
3143AArch64AsmPrinter::lowerBlockAddressConstant(const BlockAddress &BA) {
3144 const MCExpr *BAE = AsmPrinter::lowerBlockAddressConstant(BA);
3145 const Function &Fn = *BA.getFunction();
3146
3147 if (std::optional<uint16_t> BADisc =
3148 STI->getPtrAuthBlockAddressDiscriminatorIfEnabled(Fn))
3149 return AArch64AuthMCExpr::create(BAE, *BADisc, AArch64PACKey::IA,
3150 /*HasAddressDiversity=*/false, OutContext);
3151
3152 return BAE;
3153}
3154
3155void AArch64AsmPrinter::emitCBPseudoExpansion(const MachineInstr *MI) {
3156 bool IsImm = false;
3157 unsigned Width = 0;
3158
3159 switch (MI->getOpcode()) {
3160 default:
3161 llvm_unreachable("This is not a CB pseudo instruction");
3162 case AArch64::CBBAssertExt:
3163 IsImm = false;
3164 Width = 8;
3165 break;
3166 case AArch64::CBHAssertExt:
3167 IsImm = false;
3168 Width = 16;
3169 break;
3170 case AArch64::CBWPrr:
3171 Width = 32;
3172 break;
3173 case AArch64::CBXPrr:
3174 Width = 64;
3175 break;
3176 case AArch64::CBWPri:
3177 IsImm = true;
3178 Width = 32;
3179 break;
3180 case AArch64::CBXPri:
3181 IsImm = true;
3182 Width = 64;
3183 break;
3184 }
3185
3187 static_cast<AArch64CC::CondCode>(MI->getOperand(0).getImm());
3188 bool NeedsRegSwap = false;
3189 bool NeedsImmDec = false;
3190 bool NeedsImmInc = false;
3191
3192#define GET_CB_OPC(IsImm, Width, ImmCond, RegCond) \
3193 (IsImm \
3194 ? (Width == 32 ? AArch64::CB##ImmCond##Wri : AArch64::CB##ImmCond##Xri) \
3195 : (Width == 8 \
3196 ? AArch64::CBB##RegCond##Wrr \
3197 : (Width == 16 ? AArch64::CBH##RegCond##Wrr \
3198 : (Width == 32 ? AArch64::CB##RegCond##Wrr \
3199 : AArch64::CB##RegCond##Xrr))))
3200 unsigned MCOpC;
3201
3202 // Decide if we need to either swap register operands or increment/decrement
3203 // immediate operands
3204 switch (CC) {
3205 default:
3206 llvm_unreachable("Invalid CB condition code");
3207 case AArch64CC::EQ:
3208 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ EQ, /* Reg-Reg */ EQ);
3209 break;
3210 case AArch64CC::NE:
3211 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ NE, /* Reg-Reg */ NE);
3212 break;
3213 case AArch64CC::HS:
3214 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ HI, /* Reg-Reg */ HS);
3215 NeedsImmDec = IsImm;
3216 break;
3217 case AArch64CC::LO:
3218 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ LO, /* Reg-Reg */ HI);
3219 NeedsRegSwap = !IsImm;
3220 break;
3221 case AArch64CC::HI:
3222 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ HI, /* Reg-Reg */ HI);
3223 break;
3224 case AArch64CC::LS:
3225 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ LO, /* Reg-Reg */ HS);
3226 NeedsRegSwap = !IsImm;
3227 NeedsImmInc = IsImm;
3228 break;
3229 case AArch64CC::GE:
3230 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ GT, /* Reg-Reg */ GE);
3231 NeedsImmDec = IsImm;
3232 break;
3233 case AArch64CC::LT:
3234 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ LT, /* Reg-Reg */ GT);
3235 NeedsRegSwap = !IsImm;
3236 break;
3237 case AArch64CC::GT:
3238 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ GT, /* Reg-Reg */ GT);
3239 break;
3240 case AArch64CC::LE:
3241 MCOpC = GET_CB_OPC(IsImm, Width, /* Reg-Imm */ LT, /* Reg-Reg */ GE);
3242 NeedsRegSwap = !IsImm;
3243 NeedsImmInc = IsImm;
3244 break;
3245 }
3246#undef GET_CB_OPC
3247
3248 MCInst Inst;
3249 Inst.setOpcode(MCOpC);
3250
3251 MCOperand Lhs, Rhs, Trgt;
3252 lowerOperand(MI->getOperand(1), Lhs);
3253 lowerOperand(MI->getOperand(2), Rhs);
3254 lowerOperand(MI->getOperand(3), Trgt);
3255
3256 // Now swap, increment or decrement
3257 if (NeedsRegSwap) {
3258 assert(Lhs.isReg() && "Expected register operand for CB");
3259 assert(Rhs.isReg() && "Expected register operand for CB");
3260 Inst.addOperand(Rhs);
3261 Inst.addOperand(Lhs);
3262 } else if (NeedsImmDec) {
3263 Rhs.setImm(Rhs.getImm() - 1);
3264 Inst.addOperand(Lhs);
3265 Inst.addOperand(Rhs);
3266 } else if (NeedsImmInc) {
3267 Rhs.setImm(Rhs.getImm() + 1);
3268 Inst.addOperand(Lhs);
3269 Inst.addOperand(Rhs);
3270 } else {
3271 Inst.addOperand(Lhs);
3272 Inst.addOperand(Rhs);
3273 }
3274
3275 assert((!IsImm || (Rhs.getImm() >= 0 && Rhs.getImm() < 64)) &&
3276 "CB immediate operand out-of-bounds");
3277
3278 Inst.addOperand(Trgt);
3279 EmitToStreamer(*OutStreamer, Inst);
3280}
3281
3282void AArch64AsmPrinter::emitAtomicHintPseudoExpansion(const MachineInstr *MI) {
3283
3284 unsigned StOpc;
3285 unsigned Relaxed = MI->getOperand(2).getImm();
3286 assert(Relaxed < 2 && "Atomic hint relaxed immediate out-of-bounds");
3287 switch (MI->getOpcode()) {
3288 case AArch64::ATOMIC_STORE_HINT_B:
3289 StOpc = Relaxed ? AArch64::STRBBui : AArch64::STLRB;
3290 break;
3291 case AArch64::ATOMIC_STORE_HINT_H:
3292 StOpc = Relaxed ? AArch64::STRHHui : AArch64::STLRH;
3293 break;
3294 case AArch64::ATOMIC_STORE_HINT_W:
3295 StOpc = Relaxed ? AArch64::STRWui : AArch64::STLRW;
3296 break;
3297 case AArch64::ATOMIC_STORE_HINT_X:
3298 StOpc = Relaxed ? AArch64::STRXui : AArch64::STLRX;
3299 break;
3300 default:
3301 llvm_unreachable("Unexpected atomic hint size.");
3302 }
3303
3304 EmitToStreamer(
3305 MCInstBuilder(AArch64::HINT).addImm(MI->getOperand(3).getImm()));
3306
3307 MCInst Store;
3308 Store.setOpcode(StOpc);
3309 Store.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
3310 Store.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
3311 Store.setFlags(MI->getFlags());
3312 if (Relaxed)
3313 Store.addOperand(MCOperand::createImm(0));
3314 EmitToStreamer(*OutStreamer, Store);
3315}
3316
3317void AArch64AsmPrinter::emitAtomicHintPseudoExpansionRO(
3318 const MachineInstr *MI) {
3319 unsigned StOpc;
3320 assert(MI->getOperand(5).getImm() == 1 &&
3321 "Atomic store addressing mode only supports relaxed stores");
3322
3323 switch (MI->getOpcode()) {
3324 case AArch64::ATOMIC_STORE_HINT_BroW:
3325 StOpc = AArch64::STRBBroW;
3326 break;
3327 case AArch64::ATOMIC_STORE_HINT_HroW:
3328 StOpc = AArch64::STRHHroW;
3329 break;
3330 case AArch64::ATOMIC_STORE_HINT_WroW:
3331 StOpc = AArch64::STRWroW;
3332 break;
3333 case AArch64::ATOMIC_STORE_HINT_XroW:
3334 StOpc = AArch64::STRXroW;
3335 break;
3336 case AArch64::ATOMIC_STORE_HINT_SroW:
3337 StOpc = AArch64::STRSroW;
3338 break;
3339 case AArch64::ATOMIC_STORE_HINT_DroW:
3340 StOpc = AArch64::STRDroW;
3341 break;
3342 case AArch64::ATOMIC_STORE_HINT_BroX:
3343 StOpc = AArch64::STRBBroX;
3344 break;
3345 case AArch64::ATOMIC_STORE_HINT_HroX:
3346 StOpc = AArch64::STRHHroX;
3347 break;
3348 case AArch64::ATOMIC_STORE_HINT_WroX:
3349 StOpc = AArch64::STRWroX;
3350 break;
3351 case AArch64::ATOMIC_STORE_HINT_XroX:
3352 StOpc = AArch64::STRXroX;
3353 break;
3354 case AArch64::ATOMIC_STORE_HINT_SroX:
3355 StOpc = AArch64::STRSroX;
3356 break;
3357 case AArch64::ATOMIC_STORE_HINT_DroX:
3358 StOpc = AArch64::STRDroX;
3359 break;
3360 default:
3361 llvm_unreachable("Unexpected atomic hint opcode.");
3362 }
3363
3364 EmitToStreamer(
3365 MCInstBuilder(AArch64::HINT).addImm(MI->getOperand(6).getImm()));
3366
3367 MCInst Store;
3368 Store.setOpcode(StOpc);
3369 Store.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); // Rn
3370 Store.addOperand(MCOperand::createReg(MI->getOperand(1).getReg())); // Rm
3371 Store.addOperand(MCOperand::createReg(MI->getOperand(2).getReg())); // Data
3372 Store.addOperand(MCOperand::createImm(MI->getOperand(3).getImm())); // Signed
3373 Store.addOperand(MCOperand::createImm(MI->getOperand(4).getImm())); // Shift
3374 Store.setFlags(MI->getFlags());
3375 EmitToStreamer(*OutStreamer, Store);
3376}
3377
3378void AArch64AsmPrinter::emitAtomicHintPseudoExpansionImm(
3379 const MachineInstr *MI) {
3380 unsigned StOpc;
3381 assert(MI->getOperand(3).getImm() == 1 &&
3382 "Atomic store addressing mode only supports relaxed stores");
3383
3384 switch (MI->getOpcode()) {
3385 case AArch64::ATOMIC_STORE_HINT_Bui:
3386 StOpc = AArch64::STRBBui;
3387 break;
3388 case AArch64::ATOMIC_STORE_HINT_Hui:
3389 StOpc = AArch64::STRHHui;
3390 break;
3391 case AArch64::ATOMIC_STORE_HINT_Wui:
3392 StOpc = AArch64::STRWui;
3393 break;
3394 case AArch64::ATOMIC_STORE_HINT_Xui:
3395 StOpc = AArch64::STRXui;
3396 break;
3397 case AArch64::ATOMIC_STORE_HINT_Sui:
3398 StOpc = AArch64::STRSui;
3399 break;
3400 case AArch64::ATOMIC_STORE_HINT_Dui:
3401 StOpc = AArch64::STRDui;
3402 break;
3403 case AArch64::ATOMIC_STORE_HINT_Bi:
3404 StOpc = AArch64::STURBBi;
3405 break;
3406 case AArch64::ATOMIC_STORE_HINT_Hi:
3407 StOpc = AArch64::STURHHi;
3408 break;
3409 case AArch64::ATOMIC_STORE_HINT_Wi:
3410 StOpc = AArch64::STURWi;
3411 break;
3412 case AArch64::ATOMIC_STORE_HINT_Xi:
3413 StOpc = AArch64::STURXi;
3414 break;
3415 case AArch64::ATOMIC_STORE_HINT_Si:
3416 StOpc = AArch64::STURSi;
3417 break;
3418 case AArch64::ATOMIC_STORE_HINT_Di:
3419 StOpc = AArch64::STURDi;
3420 break;
3421 default:
3422 llvm_unreachable("Unexpected atomic hint opcode.");
3423 }
3424
3425 EmitToStreamer(
3426 MCInstBuilder(AArch64::HINT).addImm(MI->getOperand(4).getImm()));
3427
3428 MCInst Store;
3429 Store.setOpcode(StOpc);
3430 Store.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); // Rn
3431 Store.addOperand(MCOperand::createReg(MI->getOperand(1).getReg())); // Data
3432 Store.addOperand(MCOperand::createImm(MI->getOperand(2).getImm())); // Imm
3433 Store.setFlags(MI->getFlags());
3434 EmitToStreamer(*OutStreamer, Store);
3435}
3436
3437void AArch64AsmPrinter::emitAtomicFetchHintPseudoExpansion(
3438 const MachineInstr *MI) {
3439 EmitToStreamer(
3440 MCInstBuilder(AArch64::HINT).addImm(MI->getOperand(4).getImm()));
3441
3442 MCInst Fetch;
3443 Fetch.setOpcode(MI->getOperand(3).getImm());
3444 Fetch.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
3445 Fetch.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
3446 Fetch.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
3447 Fetch.setFlags(MI->getFlags());
3448 EmitToStreamer(*OutStreamer, Fetch);
3449}
3450
3451// Simple pseudo-instructions have their lowering (with expansion to real
3452// instructions) auto-generated.
3453#include "AArch64GenMCPseudoLowering.inc"
3454
3455void AArch64AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
3456 S.emitInstruction(Inst, *STI);
3457#ifndef NDEBUG
3458 ++InstsEmitted;
3459#endif
3460}
3461
3462void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
3463 AArch64_MC::verifyInstructionPredicates(MI->getOpcode(), STI->getFeatureBits());
3464
3465#ifndef NDEBUG
3466 InstsEmitted = 0;
3467 llvm::scope_exit CheckMISize([&]() {
3468 assert(STI->getInstrInfo()->getInstSizeInBytes(*MI) >= InstsEmitted * 4);
3469 });
3470#endif
3471
3472 // Do any auto-generated pseudo lowerings.
3473 if (MCInst OutInst; lowerPseudoInstExpansion(MI, OutInst)) {
3474 EmitToStreamer(*OutStreamer, OutInst);
3475 return;
3476 }
3477
3478 if (MI->getOpcode() == AArch64::ADRP) {
3479 for (auto &Opd : MI->operands()) {
3480 if (Opd.isSymbol() && StringRef(Opd.getSymbolName()) ==
3481 "swift_async_extendedFramePointerFlags") {
3482 ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = true;
3483 }
3484 }
3485 }
3486
3487 if (AArch64FI->getLOHRelated().count(MI)) {
3488 // Generate a label for LOH related instruction
3489 MCSymbol *LOHLabel = createTempSymbol("loh");
3490 // Associate the instruction with the label
3491 LOHInstToLabel[MI] = LOHLabel;
3492 OutStreamer->emitLabel(LOHLabel);
3493 }
3494
3495 AArch64TargetStreamer *TS =
3496 static_cast<AArch64TargetStreamer *>(OutStreamer->getTargetStreamer());
3497 // Do any manual lowerings.
3498 switch (MI->getOpcode()) {
3499 default:
3501 "Unhandled tail call instruction");
3502 break;
3503 case AArch64::READ_REGISTER_GPR64:
3504 // Read of a named GPR: emit "mov Xt, Xn" (ORR Xt, XZR, Xn). The source
3505 // register is encoded as an immediate operand so that earlier passes do not
3506 // see a use of an undefined physical register.
3507 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::ORRXrs)
3508 .addReg(MI->getOperand(0).getReg())
3509 .addReg(AArch64::XZR)
3510 .addReg(MI->getOperand(1).getImm())
3511 .addImm(0));
3512 return;
3513 case AArch64::READ_REGISTER_FPR64:
3514 // Read of a named FP/SIMD d-register: emit "fmov Dt, Dn".
3515 EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::FMOVDr)
3516 .addReg(MI->getOperand(0).getReg())
3517 .addReg(MI->getOperand(1).getImm()));
3518 return;
3519 case AArch64::HINT: {
3520 // CurrentPatchableFunctionEntrySym can be CurrentFnBegin only for
3521 // -fpatchable-function-entry=N,0. The entry MBB is guaranteed to be
3522 // non-empty. If MI is the initial BTI, place the
3523 // __patchable_function_entries label after BTI.
3524 if (CurrentPatchableFunctionEntrySym &&
3525 CurrentPatchableFunctionEntrySym == CurrentFnBegin &&
3526 MI == &MF->front().front()) {
3527 int64_t Imm = MI->getOperand(0).getImm();
3528 if (Imm == 32 || Imm == 34 || Imm == 36 || Imm == 38) {
3529 MCInst Inst;
3530 MCInstLowering.Lower(MI, Inst);
3531 EmitToStreamer(*OutStreamer, Inst);
3532 CurrentPatchableFunctionEntrySym = createTempSymbol("patch");
3533 OutStreamer->emitLabel(CurrentPatchableFunctionEntrySym);
3534 return;
3535 }
3536 }
3537 break;
3538 }
3539 case AArch64::MOVMCSym: {
3540 Register DestReg = MI->getOperand(0).getReg();
3541 const MachineOperand &MO_Sym = MI->getOperand(1);
3542 MachineOperand Hi_MOSym(MO_Sym), Lo_MOSym(MO_Sym);
3543 MCOperand Hi_MCSym, Lo_MCSym;
3544
3545 Hi_MOSym.setTargetFlags(AArch64II::MO_G1 | AArch64II::MO_S);
3546 Lo_MOSym.setTargetFlags(AArch64II::MO_G0 | AArch64II::MO_NC);
3547
3548 MCInstLowering.lowerOperand(Hi_MOSym, Hi_MCSym);
3549 MCInstLowering.lowerOperand(Lo_MOSym, Lo_MCSym);
3550
3551 MCInst MovZ;
3552 MovZ.setOpcode(AArch64::MOVZXi);
3553 MovZ.addOperand(MCOperand::createReg(DestReg));
3554 MovZ.addOperand(Hi_MCSym);
3556 EmitToStreamer(*OutStreamer, MovZ);
3557
3558 MCInst MovK;
3559 MovK.setOpcode(AArch64::MOVKXi);
3560 MovK.addOperand(MCOperand::createReg(DestReg));
3561 MovK.addOperand(MCOperand::createReg(DestReg));
3562 MovK.addOperand(Lo_MCSym);
3564 EmitToStreamer(*OutStreamer, MovK);
3565 return;
3566 }
3567 case AArch64::MOVIv2d_ns:
3568 // It is generally beneficial to rewrite "fmov s0, wzr" to "movi d0, #0".
3569 // as movi is more efficient across all cores. Newer cores can eliminate
3570 // fmovs early and there is no difference with movi, but this not true for
3571 // all implementations.
3572 //
3573 // The floating-point version doesn't quite work in rare cases on older
3574 // CPUs, so on those targets we lower this instruction to movi.16b instead.
3575 if (STI->hasZeroCycleZeroingFPWorkaround() &&
3576 MI->getOperand(1).getImm() == 0) {
3577 MCInst TmpInst;
3578 TmpInst.setOpcode(AArch64::MOVIv16b_ns);
3579 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
3580 TmpInst.addOperand(MCOperand::createImm(0));
3581 EmitToStreamer(*OutStreamer, TmpInst);
3582 return;
3583 }
3584 break;
3585
3586 case AArch64::DBG_VALUE:
3587 case AArch64::DBG_VALUE_LIST:
3588 if (isVerbose() && OutStreamer->hasRawTextSupport()) {
3589 SmallString<128> TmpStr;
3590 raw_svector_ostream OS(TmpStr);
3591 PrintDebugValueComment(MI, OS);
3592 OutStreamer->emitRawText(StringRef(OS.str()));
3593 }
3594 return;
3595
3596 case AArch64::EMITBKEY:
3597 case AArch64::EMITNONHINTED: {
3598 ExceptionHandling ExceptionHandlingType = getModuleExceptionModel();
3599 if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
3600 ExceptionHandlingType != ExceptionHandling::ARM)
3601 return;
3602
3603 if (getFunctionCFISectionType(*MF) == CFISection::None)
3604 return;
3605
3606 if (MI->getOpcode() == AArch64::EMITBKEY)
3607 OutStreamer->emitCFIBKeyFrame();
3608 else
3609 OutStreamer->emitCFINonHintPAuthFrame();
3610
3611 return;
3612 }
3613
3614 case AArch64::EMITMTETAGGED: {
3615 ExceptionHandling ExceptionHandlingType = getModuleExceptionModel();
3616 if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
3617 ExceptionHandlingType != ExceptionHandling::ARM)
3618 return;
3619
3620 if (getFunctionCFISectionType(*MF) != CFISection::None)
3621 OutStreamer->emitCFIMTETaggedFrame();
3622 return;
3623 }
3624
3625 case AArch64::AUTx16x17: {
3626 const Register Pointer = AArch64::X16;
3627 const Register Scratch = AArch64::X17;
3628
3629 auto AuthSchema = PtrAuthSchema::CreateImmReg(
3630 (AArch64PACKey::ID)MI->getOperand(0).getImm(),
3631 MI->getOperand(1).getImm(), MI->getOperand(2));
3632
3633 emitPtrauthAuthResign(Pointer, Scratch, AuthSchema, std::nullopt,
3634 std::nullopt, MI->getDeactivationSymbol());
3635 return;
3636 }
3637
3638 case AArch64::AUTxMxN: {
3639 const Register Pointer = MI->getOperand(0).getReg();
3640 const Register Scratch = MI->getOperand(1).getReg();
3641
3642 auto AuthSchema = PtrAuthSchema::CreateImmReg(
3643 (AArch64PACKey::ID)MI->getOperand(3).getImm(),
3644 MI->getOperand(4).getImm(), MI->getOperand(5));
3645
3646 emitPtrauthAuthResign(Pointer, Scratch, AuthSchema, std::nullopt,
3647 std::nullopt, MI->getDeactivationSymbol());
3648 return;
3649 }
3650
3651 case AArch64::AUTPAC: {
3652 const Register Pointer = AArch64::X16;
3653 const Register Scratch = AArch64::X17;
3654
3655 auto AuthSchema = PtrAuthSchema::CreateImmReg(
3656 (AArch64PACKey::ID)MI->getOperand(0).getImm(),
3657 MI->getOperand(1).getImm(), MI->getOperand(2));
3658
3659 auto SignSchema = PtrAuthSchema::CreateImmReg(
3660 (AArch64PACKey::ID)MI->getOperand(3).getImm(),
3661 MI->getOperand(4).getImm(), MI->getOperand(5));
3662
3663 emitPtrauthAuthResign(Pointer, Scratch, AuthSchema, SignSchema,
3664 std::nullopt, MI->getDeactivationSymbol());
3665 return;
3666 }
3667
3668 case AArch64::AUTPCPAC: {
3669 auto AuthSchema = PtrAuthSchema::CreateRegReg(
3670 (AArch64PACKey::ID)MI->getOperand(0).getImm(), AArch64::X16,
3671 AArch64::X15);
3672
3673 auto SignSchema = PtrAuthSchema::CreateImmReg(
3674 (AArch64PACKey::ID)MI->getOperand(1).getImm(),
3675 MI->getOperand(2).getImm(), MI->getOperand(3));
3676
3677 emitPtrauthAuthResign(/*Pointer=*/AArch64::X17, /*Scratch=*/AArch64::X16,
3678 AuthSchema, SignSchema, std::nullopt,
3679 MI->getDeactivationSymbol());
3680 return;
3681 }
3682
3683 case AArch64::AUTRELLOADPAC: {
3684 const Register Pointer = AArch64::X16;
3685 const Register Scratch = AArch64::X17;
3686
3687 auto AuthSchema = PtrAuthSchema::CreateImmReg(
3688 (AArch64PACKey::ID)MI->getOperand(0).getImm(),
3689 MI->getOperand(1).getImm(), MI->getOperand(2));
3690
3691 auto SignSchema = PtrAuthSchema::CreateImmReg(
3692 (AArch64PACKey::ID)MI->getOperand(3).getImm(),
3693 MI->getOperand(4).getImm(), MI->getOperand(5));
3694
3695 emitPtrauthAuthResign(Pointer, Scratch, AuthSchema, SignSchema,
3696 MI->getOperand(6).getImm(),
3697 MI->getDeactivationSymbol());
3698
3699 return;
3700 }
3701
3702 case AArch64::PAC:
3703 emitPtrauthSign(MI);
3704 return;
3705
3706 case AArch64::LOADauthptrstatic:
3707 LowerLOADauthptrstatic(*MI);
3708 return;
3709
3710 case AArch64::LOADgotPAC:
3711 case AArch64::MOVaddrPAC:
3712 LowerMOVaddrPAC(*MI);
3713 return;
3714
3715 case AArch64::LOADgotAUTH:
3716 LowerLOADgotAUTH(*MI);
3717 return;
3718
3719 case AArch64::BRA:
3720 case AArch64::BLRA:
3721 emitPtrauthBranch(MI);
3722 return;
3723
3724 // Tail calls use pseudo instructions so they have the proper code-gen
3725 // attributes (isCall, isReturn, etc.). We lower them to the real
3726 // instruction here.
3727 case AArch64::AUTH_TCRETURN:
3728 case AArch64::AUTH_TCRETURN_BTI: {
3729 Register Callee = MI->getOperand(0).getReg();
3730 const auto Key = (AArch64PACKey::ID)MI->getOperand(2).getImm();
3731 const uint64_t Disc = MI->getOperand(3).getImm();
3732
3733 Register AddrDisc = MI->getOperand(4).getReg();
3734
3735 Register ScratchReg = Callee == AArch64::X16 ? AArch64::X17 : AArch64::X16;
3736
3737 emitPtrauthTailCallHardening(MI);
3738
3739 // See the comments in emitPtrauthBranch.
3740 if (Callee == AddrDisc)
3741 report_fatal_error("Call target is signed with its own value");
3742
3743 // After isX16X17Safer predicate was introduced, emitPtrauthDiscriminator is
3744 // no longer restricted to only reusing AddrDisc when it is X16 or X17
3745 // (which are implicit-def'ed by AUTH_TCRETURN pseudos), thus impose this
3746 // restriction manually not to clobber an unexpected register.
3747 bool AddrDiscIsImplicitDef =
3748 AddrDisc == AArch64::X16 || AddrDisc == AArch64::X17;
3749 Register DiscReg = emitPtrauthDiscriminator(Disc, AddrDisc, ScratchReg,
3750 AddrDiscIsImplicitDef);
3751 emitBLRA(/*IsCall*/ false, Key, Callee, DiscReg);
3752 return;
3753 }
3754
3755 case AArch64::TCRETURNri:
3756 case AArch64::TCRETURNrix16x17:
3757 case AArch64::TCRETURNrix17:
3758 case AArch64::TCRETURNrinotx16:
3759 case AArch64::TCRETURNriALL: {
3760 emitPtrauthTailCallHardening(MI);
3761
3762 recordIfImportCall(MI);
3763 MCInst TmpInst;
3764 TmpInst.setOpcode(AArch64::BR);
3765 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
3766 EmitToStreamer(*OutStreamer, TmpInst);
3767 return;
3768 }
3769 case AArch64::TCRETURNdi: {
3770 emitPtrauthTailCallHardening(MI);
3771
3772 MCOperand Dest;
3773 MCInstLowering.lowerOperand(MI->getOperand(0), Dest);
3774 recordIfImportCall(MI);
3775 MCInst TmpInst;
3776 TmpInst.setOpcode(AArch64::B);
3777 TmpInst.addOperand(Dest);
3778 EmitToStreamer(*OutStreamer, TmpInst);
3779 return;
3780 }
3781 case AArch64::SpeculationBarrierISBDSBEndBB: {
3782 // Print DSB SYS + ISB
3783 MCInst TmpInstDSB;
3784 TmpInstDSB.setOpcode(AArch64::DSB);
3785 TmpInstDSB.addOperand(MCOperand::createImm(0xf));
3786 EmitToStreamer(*OutStreamer, TmpInstDSB);
3787 MCInst TmpInstISB;
3788 TmpInstISB.setOpcode(AArch64::ISB);
3789 TmpInstISB.addOperand(MCOperand::createImm(0xf));
3790 EmitToStreamer(*OutStreamer, TmpInstISB);
3791 return;
3792 }
3793 case AArch64::SpeculationBarrierSBEndBB: {
3794 // Print SB
3795 MCInst TmpInstSB;
3796 TmpInstSB.setOpcode(AArch64::SB);
3797 EmitToStreamer(*OutStreamer, TmpInstSB);
3798 return;
3799 }
3800 case AArch64::TLSDESC_AUTH_CALLSEQ: {
3801 /// lower this to:
3802 /// adrp x0, :tlsdesc_auth:var
3803 /// ldr x16, [x0, #:tlsdesc_auth_lo12:var]
3804 /// add x0, x0, #:tlsdesc_auth_lo12:var
3805 /// .tlsauthdesccall var
3806 /// blraa x16, x0
3807 /// (TPIDR_EL0 offset now in x0)
3808 const MachineOperand &MO_Sym = MI->getOperand(0);
3809 MachineOperand MO_TLSDESC_LO12(MO_Sym), MO_TLSDESC(MO_Sym);
3810 MCOperand Sym, SymTLSDescLo12, SymTLSDesc;
3811 MO_TLSDESC_LO12.setTargetFlags(AArch64II::MO_TLS | AArch64II::MO_PAGEOFF);
3812 MO_TLSDESC.setTargetFlags(AArch64II::MO_TLS | AArch64II::MO_PAGE);
3813 MCInstLowering.lowerOperand(MO_Sym, Sym);
3814 MCInstLowering.lowerOperand(MO_TLSDESC_LO12, SymTLSDescLo12);
3815 MCInstLowering.lowerOperand(MO_TLSDESC, SymTLSDesc);
3816
3817 MCInst Adrp;
3818 Adrp.setOpcode(AArch64::ADRP);
3819 Adrp.addOperand(MCOperand::createReg(AArch64::X0));
3820 Adrp.addOperand(SymTLSDesc);
3821 EmitToStreamer(*OutStreamer, Adrp);
3822
3823 MCInst Ldr;
3824 Ldr.setOpcode(AArch64::LDRXui);
3825 Ldr.addOperand(MCOperand::createReg(AArch64::X16));
3826 Ldr.addOperand(MCOperand::createReg(AArch64::X0));
3827 Ldr.addOperand(SymTLSDescLo12);
3829 EmitToStreamer(*OutStreamer, Ldr);
3830
3831 MCInst Add;
3832 Add.setOpcode(AArch64::ADDXri);
3833 Add.addOperand(MCOperand::createReg(AArch64::X0));
3834 Add.addOperand(MCOperand::createReg(AArch64::X0));
3835 Add.addOperand(SymTLSDescLo12);
3837 EmitToStreamer(*OutStreamer, Add);
3838
3839 // Emit a relocation-annotation. This expands to no code, but requests
3840 // the following instruction gets an R_AARCH64_AUTH_TLSDESC_CALL.
3841 MCInst TLSAuthDescCall;
3842 TLSAuthDescCall.setOpcode(AArch64::TLSAUTHDESCCALL);
3843 TLSAuthDescCall.addOperand(Sym);
3844 EmitToStreamer(*OutStreamer, TLSAuthDescCall);
3845#ifndef NDEBUG
3846 --InstsEmitted; // no code emitted
3847#endif
3848
3849 MCInst Blraa;
3850 Blraa.setOpcode(AArch64::BLRAA);
3851 Blraa.addOperand(MCOperand::createReg(AArch64::X16));
3852 Blraa.addOperand(MCOperand::createReg(AArch64::X0));
3853 EmitToStreamer(*OutStreamer, Blraa);
3854
3855 return;
3856 }
3857 case AArch64::TLSDESC_CALLSEQ: {
3858 /// lower this to:
3859 /// adrp x0, :tlsdesc:var
3860 /// ldr x1, [x0, #:tlsdesc_lo12:var]
3861 /// add x0, x0, #:tlsdesc_lo12:var
3862 /// .tlsdesccall var
3863 /// blr x1
3864 /// (TPIDR_EL0 offset now in x0)
3865 const MachineOperand &MO_Sym = MI->getOperand(0);
3866 MachineOperand MO_TLSDESC_LO12(MO_Sym), MO_TLSDESC(MO_Sym);
3867 MCOperand Sym, SymTLSDescLo12, SymTLSDesc;
3868 MO_TLSDESC_LO12.setTargetFlags(AArch64II::MO_TLS | AArch64II::MO_PAGEOFF);
3869 MO_TLSDESC.setTargetFlags(AArch64II::MO_TLS | AArch64II::MO_PAGE);
3870 MCInstLowering.lowerOperand(MO_Sym, Sym);
3871 MCInstLowering.lowerOperand(MO_TLSDESC_LO12, SymTLSDescLo12);
3872 MCInstLowering.lowerOperand(MO_TLSDESC, SymTLSDesc);
3873
3874 MCInst Adrp;
3875 Adrp.setOpcode(AArch64::ADRP);
3876 Adrp.addOperand(MCOperand::createReg(AArch64::X0));
3877 Adrp.addOperand(SymTLSDesc);
3878 EmitToStreamer(*OutStreamer, Adrp);
3879
3880 MCInst Ldr;
3881 if (STI->isTargetILP32()) {
3882 Ldr.setOpcode(AArch64::LDRWui);
3883 Ldr.addOperand(MCOperand::createReg(AArch64::W1));
3884 } else {
3885 Ldr.setOpcode(AArch64::LDRXui);
3886 Ldr.addOperand(MCOperand::createReg(AArch64::X1));
3887 }
3888 Ldr.addOperand(MCOperand::createReg(AArch64::X0));
3889 Ldr.addOperand(SymTLSDescLo12);
3891 EmitToStreamer(*OutStreamer, Ldr);
3892
3893 MCInst Add;
3894 if (STI->isTargetILP32()) {
3895 Add.setOpcode(AArch64::ADDWri);
3896 Add.addOperand(MCOperand::createReg(AArch64::W0));
3897 Add.addOperand(MCOperand::createReg(AArch64::W0));
3898 } else {
3899 Add.setOpcode(AArch64::ADDXri);
3900 Add.addOperand(MCOperand::createReg(AArch64::X0));
3901 Add.addOperand(MCOperand::createReg(AArch64::X0));
3902 }
3903 Add.addOperand(SymTLSDescLo12);
3905 EmitToStreamer(*OutStreamer, Add);
3906
3907 // Emit a relocation-annotation. This expands to no code, but requests
3908 // the following instruction gets an R_AARCH64_TLSDESC_CALL.
3909 MCInst TLSDescCall;
3910 TLSDescCall.setOpcode(AArch64::TLSDESCCALL);
3911 TLSDescCall.addOperand(Sym);
3912 EmitToStreamer(*OutStreamer, TLSDescCall);
3913#ifndef NDEBUG
3914 --InstsEmitted; // no code emitted
3915#endif
3916
3917 MCInst Blr;
3918 Blr.setOpcode(AArch64::BLR);
3919 Blr.addOperand(MCOperand::createReg(AArch64::X1));
3920 EmitToStreamer(*OutStreamer, Blr);
3921
3922 return;
3923 }
3924
3925 case AArch64::JumpTableDest32:
3926 case AArch64::JumpTableDest16:
3927 case AArch64::JumpTableDest8:
3928 LowerJumpTableDest(*OutStreamer, *MI);
3929 return;
3930
3931 case AArch64::BR_JumpTable:
3932 LowerHardenedBRJumpTable(*MI);
3933 return;
3934
3935 case AArch64::FMOVH0:
3936 case AArch64::FMOVS0:
3937 case AArch64::FMOVD0:
3938 emitFMov0(*MI);
3939 return;
3940
3941 case AArch64::MOPSMemoryCopyPseudo:
3942 case AArch64::MOPSMemoryMovePseudo:
3943 case AArch64::MOPSMemorySetPseudo:
3944 case AArch64::MOPSMemorySetTaggingPseudo:
3945 LowerMOPS(*OutStreamer, *MI);
3946 return;
3947
3948 case TargetOpcode::STACKMAP:
3949 return LowerSTACKMAP(*OutStreamer, SM, *MI);
3950
3951 case TargetOpcode::PATCHPOINT:
3952 return LowerPATCHPOINT(*OutStreamer, SM, *MI);
3953
3954 case TargetOpcode::STATEPOINT:
3955 return LowerSTATEPOINT(*OutStreamer, SM, *MI);
3956
3957 case TargetOpcode::FAULTING_OP:
3958 return LowerFAULTING_OP(*MI);
3959
3960 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
3961 LowerPATCHABLE_FUNCTION_ENTER(*MI);
3962 return;
3963
3964 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
3965 LowerPATCHABLE_FUNCTION_EXIT(*MI);
3966 return;
3967
3968 case TargetOpcode::PATCHABLE_TAIL_CALL:
3969 LowerPATCHABLE_TAIL_CALL(*MI);
3970 return;
3971 case TargetOpcode::PATCHABLE_EVENT_CALL:
3972 return LowerPATCHABLE_EVENT_CALL(*MI, false);
3973 case TargetOpcode::PATCHABLE_TYPED_EVENT_CALL:
3974 return LowerPATCHABLE_EVENT_CALL(*MI, true);
3975
3976 case AArch64::KCFI_CHECK:
3977 LowerKCFI_CHECK(*MI);
3978 return;
3979
3980 case AArch64::HWASAN_CHECK_MEMACCESS:
3981 case AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES:
3982 case AArch64::HWASAN_CHECK_MEMACCESS_FIXEDSHADOW:
3983 case AArch64::HWASAN_CHECK_MEMACCESS_SHORTGRANULES_FIXEDSHADOW:
3984 LowerHWASAN_CHECK_MEMACCESS(*MI);
3985 return;
3986
3987 case AArch64::SEH_EndC:
3988 TS->emitARM64WinCFIEndC();
3989 return;
3990
3991 case AArch64::SEH_StackAlloc:
3992 TS->emitARM64WinCFIAllocStack(MI->getOperand(0).getImm());
3993 return;
3994
3995 case AArch64::SEH_SaveFPLR:
3996 TS->emitARM64WinCFISaveFPLR(MI->getOperand(0).getImm());
3997 return;
3998
3999 case AArch64::SEH_SaveFPLR_X:
4000 assert(MI->getOperand(0).getImm() < 0 &&
4001 "Pre increment SEH opcode must have a negative offset");
4002 TS->emitARM64WinCFISaveFPLRX(-MI->getOperand(0).getImm());
4003 return;
4004
4005 case AArch64::SEH_SaveReg:
4006 TS->emitARM64WinCFISaveReg(MI->getOperand(0).getImm(),
4007 MI->getOperand(1).getImm());
4008 return;
4009
4010 case AArch64::SEH_SaveReg_X:
4011 assert(MI->getOperand(1).getImm() < 0 &&
4012 "Pre increment SEH opcode must have a negative offset");
4013 TS->emitARM64WinCFISaveRegX(MI->getOperand(0).getImm(),
4014 -MI->getOperand(1).getImm());
4015 return;
4016
4017 case AArch64::SEH_SaveRegP:
4018 if (MI->getOperand(1).getImm() == 30 && MI->getOperand(0).getImm() >= 19 &&
4019 MI->getOperand(0).getImm() <= 28) {
4020 assert((MI->getOperand(0).getImm() - 19) % 2 == 0 &&
4021 "Register paired with LR must be odd");
4022 TS->emitARM64WinCFISaveLRPair(MI->getOperand(0).getImm(),
4023 MI->getOperand(2).getImm());
4024 return;
4025 }
4026 assert((MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1) &&
4027 "Non-consecutive registers not allowed for save_regp");
4028 TS->emitARM64WinCFISaveRegP(MI->getOperand(0).getImm(),
4029 MI->getOperand(2).getImm());
4030 return;
4031
4032 case AArch64::SEH_SaveRegP_X:
4033 assert((MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1) &&
4034 "Non-consecutive registers not allowed for save_regp_x");
4035 assert(MI->getOperand(2).getImm() < 0 &&
4036 "Pre increment SEH opcode must have a negative offset");
4037 TS->emitARM64WinCFISaveRegPX(MI->getOperand(0).getImm(),
4038 -MI->getOperand(2).getImm());
4039 return;
4040
4041 case AArch64::SEH_SaveFReg:
4042 TS->emitARM64WinCFISaveFReg(MI->getOperand(0).getImm(),
4043 MI->getOperand(1).getImm());
4044 return;
4045
4046 case AArch64::SEH_SaveFReg_X:
4047 assert(MI->getOperand(1).getImm() < 0 &&
4048 "Pre increment SEH opcode must have a negative offset");
4049 TS->emitARM64WinCFISaveFRegX(MI->getOperand(0).getImm(),
4050 -MI->getOperand(1).getImm());
4051 return;
4052
4053 case AArch64::SEH_SaveFRegP:
4054 assert((MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1) &&
4055 "Non-consecutive registers not allowed for save_regp");
4056 TS->emitARM64WinCFISaveFRegP(MI->getOperand(0).getImm(),
4057 MI->getOperand(2).getImm());
4058 return;
4059
4060 case AArch64::SEH_SaveFRegP_X:
4061 assert((MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1) &&
4062 "Non-consecutive registers not allowed for save_regp_x");
4063 assert(MI->getOperand(2).getImm() < 0 &&
4064 "Pre increment SEH opcode must have a negative offset");
4065 TS->emitARM64WinCFISaveFRegPX(MI->getOperand(0).getImm(),
4066 -MI->getOperand(2).getImm());
4067 return;
4068
4069 case AArch64::SEH_SetFP:
4071 return;
4072
4073 case AArch64::SEH_AddFP:
4074 TS->emitARM64WinCFIAddFP(MI->getOperand(0).getImm());
4075 return;
4076
4077 case AArch64::SEH_Nop:
4078 TS->emitARM64WinCFINop();
4079 return;
4080
4081 case AArch64::SEH_PrologEnd:
4083 return;
4084
4085 case AArch64::SEH_EpilogStart:
4087 return;
4088
4089 case AArch64::SEH_EpilogEnd:
4091 return;
4092
4093 case AArch64::SEH_PACSignLR:
4095 return;
4096
4097 case AArch64::SEH_SaveAnyRegI:
4098 assert((isShiftedUInt<6, 3>((uint64_t)MI->getOperand(1).getImm())) &&
4099 "SaveAnyRegI SEH opcode offset must fit into 6 bits");
4100 TS->emitARM64WinCFISaveAnyRegI(MI->getOperand(0).getImm(),
4101 MI->getOperand(1).getImm());
4102 return;
4103
4104 case AArch64::SEH_SaveAnyRegIP:
4105 assert(MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1 &&
4106 "Non-consecutive registers not allowed for save_any_reg");
4107 assert((isShiftedUInt<6, 4>((uint64_t)MI->getOperand(2).getImm())) &&
4108 "SaveAnyRegIP SEH opcode offset must fit into 6 bits");
4109 TS->emitARM64WinCFISaveAnyRegIP(MI->getOperand(0).getImm(),
4110 MI->getOperand(2).getImm());
4111 return;
4112
4113 case AArch64::SEH_SaveAnyRegQ:
4114 assert((isShiftedUInt<6, 4>((uint64_t)MI->getOperand(1).getImm())) &&
4115 "SaveAnyRegQ SEH opcode offset must fit into 6 bits");
4116 TS->emitARM64WinCFISaveAnyRegQ(MI->getOperand(0).getImm(),
4117 MI->getOperand(1).getImm());
4118 return;
4119
4120 case AArch64::SEH_SaveAnyRegQX:
4121 assert((isShiftedUInt<6, 4>(-(uint64_t)MI->getOperand(1).getImm())) &&
4122 "SaveAnyRegQX SEH opcode offset must fit into 6 bits");
4123 TS->emitARM64WinCFISaveAnyRegQX(MI->getOperand(0).getImm(),
4124 -MI->getOperand(1).getImm());
4125 return;
4126
4127 case AArch64::SEH_SaveAnyRegQP:
4128 assert(MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1 &&
4129 "Non-consecutive registers not allowed for save_any_reg");
4130 assert((isShiftedUInt<6, 4>((uint64_t)MI->getOperand(2).getImm())) &&
4131 "SaveAnyRegQP SEH opcode offset must fit into 6 bits");
4132 TS->emitARM64WinCFISaveAnyRegQP(MI->getOperand(0).getImm(),
4133 MI->getOperand(2).getImm());
4134 return;
4135
4136 case AArch64::SEH_SaveAnyRegQPX:
4137 assert(MI->getOperand(1).getImm() - MI->getOperand(0).getImm() == 1 &&
4138 "Non-consecutive registers not allowed for save_any_reg");
4139 assert((isShiftedUInt<6, 4>(-(uint64_t)MI->getOperand(2).getImm())) &&
4140 "SaveAnyRegQPX SEH opcode offset must fit into 6 bits");
4141 TS->emitARM64WinCFISaveAnyRegQPX(MI->getOperand(0).getImm(),
4142 -MI->getOperand(2).getImm());
4143 return;
4144
4145 case AArch64::SEH_AllocZ:
4146 assert(MI->getOperand(0).getImm() >= 0 &&
4147 "AllocZ SEH opcode offset must be non-negative");
4148 assert(MI->getOperand(0).getImm() <= 255 &&
4149 "AllocZ SEH opcode offset must fit into 8 bits");
4150 TS->emitARM64WinCFIAllocZ(MI->getOperand(0).getImm());
4151 return;
4152
4153 case AArch64::SEH_SaveZReg:
4154 assert(MI->getOperand(1).getImm() >= 0 &&
4155 "SaveZReg SEH opcode offset must be non-negative");
4156 assert(MI->getOperand(1).getImm() <= 255 &&
4157 "SaveZReg SEH opcode offset must fit into 8 bits");
4158 TS->emitARM64WinCFISaveZReg(MI->getOperand(0).getImm(),
4159 MI->getOperand(1).getImm());
4160 return;
4161
4162 case AArch64::SEH_SavePReg:
4163 assert(MI->getOperand(1).getImm() >= 0 &&
4164 "SavePReg SEH opcode offset must be non-negative");
4165 assert(MI->getOperand(1).getImm() <= 255 &&
4166 "SavePReg SEH opcode offset must fit into 8 bits");
4167 TS->emitARM64WinCFISavePReg(MI->getOperand(0).getImm(),
4168 MI->getOperand(1).getImm());
4169 return;
4170
4171 case AArch64::BLR:
4172 case AArch64::BR: {
4173 recordIfImportCall(MI);
4174 MCInst TmpInst;
4175 MCInstLowering.Lower(MI, TmpInst);
4176 EmitToStreamer(*OutStreamer, TmpInst);
4177 return;
4178 }
4179 case AArch64::CBWPri:
4180 case AArch64::CBXPri:
4181 case AArch64::CBBAssertExt:
4182 case AArch64::CBHAssertExt:
4183 case AArch64::CBWPrr:
4184 case AArch64::CBXPrr:
4185 emitCBPseudoExpansion(MI);
4186 return;
4187 case AArch64::ATOMIC_STORE_HINT_B:
4188 case AArch64::ATOMIC_STORE_HINT_H:
4189 case AArch64::ATOMIC_STORE_HINT_W:
4190 case AArch64::ATOMIC_STORE_HINT_X:
4191 emitAtomicHintPseudoExpansion(MI);
4192 return;
4193 case AArch64::ATOMIC_FETCH_HINT_W:
4194 case AArch64::ATOMIC_FETCH_HINT_X:
4195 emitAtomicFetchHintPseudoExpansion(MI);
4196 return;
4197 case AArch64::ATOMIC_STORE_HINT_BroW:
4198 case AArch64::ATOMIC_STORE_HINT_HroW:
4199 case AArch64::ATOMIC_STORE_HINT_WroW:
4200 case AArch64::ATOMIC_STORE_HINT_XroW:
4201 case AArch64::ATOMIC_STORE_HINT_SroW:
4202 case AArch64::ATOMIC_STORE_HINT_DroW:
4203 case AArch64::ATOMIC_STORE_HINT_BroX:
4204 case AArch64::ATOMIC_STORE_HINT_HroX:
4205 case AArch64::ATOMIC_STORE_HINT_WroX:
4206 case AArch64::ATOMIC_STORE_HINT_XroX:
4207 case AArch64::ATOMIC_STORE_HINT_SroX:
4208 case AArch64::ATOMIC_STORE_HINT_DroX:
4209 emitAtomicHintPseudoExpansionRO(MI);
4210 return;
4211 case AArch64::ATOMIC_STORE_HINT_Bui:
4212 case AArch64::ATOMIC_STORE_HINT_Hui:
4213 case AArch64::ATOMIC_STORE_HINT_Wui:
4214 case AArch64::ATOMIC_STORE_HINT_Xui:
4215 case AArch64::ATOMIC_STORE_HINT_Sui:
4216 case AArch64::ATOMIC_STORE_HINT_Dui:
4217 case AArch64::ATOMIC_STORE_HINT_Bi:
4218 case AArch64::ATOMIC_STORE_HINT_Hi:
4219 case AArch64::ATOMIC_STORE_HINT_Wi:
4220 case AArch64::ATOMIC_STORE_HINT_Xi:
4221 case AArch64::ATOMIC_STORE_HINT_Si:
4222 case AArch64::ATOMIC_STORE_HINT_Di:
4223 emitAtomicHintPseudoExpansionImm(MI);
4224 return;
4225 }
4226
4227 if (emitDeactivationSymbolRelocation(MI->getDeactivationSymbol()))
4228 return;
4229
4230 // Finally, do the automated lowerings for everything else.
4231 MCInst TmpInst;
4232 MCInstLowering.Lower(MI, TmpInst);
4233 EmitToStreamer(*OutStreamer, TmpInst);
4234}
4235
4236void AArch64AsmPrinter::recordIfImportCall(
4237 const llvm::MachineInstr *BranchInst) {
4238 if (!EnableImportCallOptimization)
4239 return;
4240
4241 auto [GV, OpFlags] = BranchInst->getMF()->tryGetCalledGlobal(BranchInst);
4242 if (GV && GV->hasDLLImportStorageClass()) {
4243 auto *CallSiteSymbol = MMI->getContext().createNamedTempSymbol("impcall");
4244 OutStreamer->emitLabel(CallSiteSymbol);
4245
4246 auto *CalledSymbol = MCInstLowering.GetGlobalValueSymbol(GV, OpFlags);
4247 SectionToImportedFunctionCalls[OutStreamer->getCurrentSectionOnly()]
4248 .push_back({CallSiteSymbol, CalledSymbol});
4249 }
4250}
4251
4252void AArch64AsmPrinter::emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI,
4253 MCSymbol *LazyPointer) {
4254 // _ifunc:
4255 // adrp x16, lazy_pointer@GOTPAGE
4256 // ldr x16, [x16, lazy_pointer@GOTPAGEOFF]
4257 // ldr x16, [x16]
4258 // br x16
4259
4260 {
4261 MCInst Adrp;
4262 Adrp.setOpcode(AArch64::ADRP);
4263 Adrp.addOperand(MCOperand::createReg(AArch64::X16));
4264 MCOperand SymPage;
4265 MCInstLowering.lowerOperand(
4268 SymPage);
4269 Adrp.addOperand(SymPage);
4270 EmitToStreamer(Adrp);
4271 }
4272
4273 {
4274 MCInst Ldr;
4275 Ldr.setOpcode(AArch64::LDRXui);
4276 Ldr.addOperand(MCOperand::createReg(AArch64::X16));
4277 Ldr.addOperand(MCOperand::createReg(AArch64::X16));
4278 MCOperand SymPageOff;
4279 MCInstLowering.lowerOperand(
4282 SymPageOff);
4283 Ldr.addOperand(SymPageOff);
4285 EmitToStreamer(Ldr);
4286 }
4287
4288 EmitToStreamer(MCInstBuilder(AArch64::LDRXui)
4289 .addReg(AArch64::X16)
4290 .addReg(AArch64::X16)
4291 .addImm(0));
4292
4293 EmitToStreamer(MCInstBuilder(TM.getTargetTriple().isArm64e() ? AArch64::BRAAZ
4294 : AArch64::BR)
4295 .addReg(AArch64::X16));
4296}
4297
4298void AArch64AsmPrinter::emitMachOIFuncStubHelperBody(Module &M,
4299 const GlobalIFunc &GI,
4300 MCSymbol *LazyPointer) {
4301 // These stub helpers are only ever called once, so here we're optimizing for
4302 // minimum size by using the pre-indexed store variants, which saves a few
4303 // bytes of instructions to bump & restore sp.
4304
4305 // _ifunc.stub_helper:
4306 // stp fp, lr, [sp, #-16]!
4307 // mov fp, sp
4308 // stp x1, x0, [sp, #-16]!
4309 // stp x3, x2, [sp, #-16]!
4310 // stp x5, x4, [sp, #-16]!
4311 // stp x7, x6, [sp, #-16]!
4312 // stp d1, d0, [sp, #-16]!
4313 // stp d3, d2, [sp, #-16]!
4314 // stp d5, d4, [sp, #-16]!
4315 // stp d7, d6, [sp, #-16]!
4316 // bl _resolver
4317 // adrp x16, lazy_pointer@GOTPAGE
4318 // ldr x16, [x16, lazy_pointer@GOTPAGEOFF]
4319 // str x0, [x16]
4320 // mov x16, x0
4321 // ldp d7, d6, [sp], #16
4322 // ldp d5, d4, [sp], #16
4323 // ldp d3, d2, [sp], #16
4324 // ldp d1, d0, [sp], #16
4325 // ldp x7, x6, [sp], #16
4326 // ldp x5, x4, [sp], #16
4327 // ldp x3, x2, [sp], #16
4328 // ldp x1, x0, [sp], #16
4329 // ldp fp, lr, [sp], #16
4330 // br x16
4331
4332 EmitToStreamer(MCInstBuilder(AArch64::STPXpre)
4333 .addReg(AArch64::SP)
4334 .addReg(AArch64::FP)
4335 .addReg(AArch64::LR)
4336 .addReg(AArch64::SP)
4337 .addImm(-2));
4338
4339 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
4340 .addReg(AArch64::FP)
4341 .addReg(AArch64::SP)
4342 .addImm(0)
4343 .addImm(0));
4344
4345 for (int I = 0; I != 4; ++I)
4346 EmitToStreamer(MCInstBuilder(AArch64::STPXpre)
4347 .addReg(AArch64::SP)
4348 .addReg(AArch64::X1 + 2 * I)
4349 .addReg(AArch64::X0 + 2 * I)
4350 .addReg(AArch64::SP)
4351 .addImm(-2));
4352
4353 for (int I = 0; I != 4; ++I)
4354 EmitToStreamer(MCInstBuilder(AArch64::STPDpre)
4355 .addReg(AArch64::SP)
4356 .addReg(AArch64::D1 + 2 * I)
4357 .addReg(AArch64::D0 + 2 * I)
4358 .addReg(AArch64::SP)
4359 .addImm(-2));
4360
4361 EmitToStreamer(
4362 MCInstBuilder(AArch64::BL)
4364
4365 {
4366 MCInst Adrp;
4367 Adrp.setOpcode(AArch64::ADRP);
4368 Adrp.addOperand(MCOperand::createReg(AArch64::X16));
4369 MCOperand SymPage;
4370 MCInstLowering.lowerOperand(
4371 MachineOperand::CreateES(LazyPointer->getName().data() + 1,
4373 SymPage);
4374 Adrp.addOperand(SymPage);
4375 EmitToStreamer(Adrp);
4376 }
4377
4378 {
4379 MCInst Ldr;
4380 Ldr.setOpcode(AArch64::LDRXui);
4381 Ldr.addOperand(MCOperand::createReg(AArch64::X16));
4382 Ldr.addOperand(MCOperand::createReg(AArch64::X16));
4383 MCOperand SymPageOff;
4384 MCInstLowering.lowerOperand(
4385 MachineOperand::CreateES(LazyPointer->getName().data() + 1,
4387 SymPageOff);
4388 Ldr.addOperand(SymPageOff);
4390 EmitToStreamer(Ldr);
4391 }
4392
4393 EmitToStreamer(MCInstBuilder(AArch64::STRXui)
4394 .addReg(AArch64::X0)
4395 .addReg(AArch64::X16)
4396 .addImm(0));
4397
4398 EmitToStreamer(MCInstBuilder(AArch64::ADDXri)
4399 .addReg(AArch64::X16)
4400 .addReg(AArch64::X0)
4401 .addImm(0)
4402 .addImm(0));
4403
4404 for (int I = 3; I != -1; --I)
4405 EmitToStreamer(MCInstBuilder(AArch64::LDPDpost)
4406 .addReg(AArch64::SP)
4407 .addReg(AArch64::D1 + 2 * I)
4408 .addReg(AArch64::D0 + 2 * I)
4409 .addReg(AArch64::SP)
4410 .addImm(2));
4411
4412 for (int I = 3; I != -1; --I)
4413 EmitToStreamer(MCInstBuilder(AArch64::LDPXpost)
4414 .addReg(AArch64::SP)
4415 .addReg(AArch64::X1 + 2 * I)
4416 .addReg(AArch64::X0 + 2 * I)
4417 .addReg(AArch64::SP)
4418 .addImm(2));
4419
4420 EmitToStreamer(MCInstBuilder(AArch64::LDPXpost)
4421 .addReg(AArch64::SP)
4422 .addReg(AArch64::FP)
4423 .addReg(AArch64::LR)
4424 .addReg(AArch64::SP)
4425 .addImm(2));
4426
4427 EmitToStreamer(MCInstBuilder(TM.getTargetTriple().isArm64e() ? AArch64::BRAAZ
4428 : AArch64::BR)
4429 .addReg(AArch64::X16));
4430}
4431
4432const MCExpr *AArch64AsmPrinter::lowerConstant(const Constant *CV,
4433 const Constant *BaseCV,
4434 uint64_t Offset) {
4435 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
4436 return MCSymbolRefExpr::create(MCInstLowering.GetGlobalValueSymbol(GV, 0),
4437 OutContext);
4438 }
4439
4440 return AsmPrinter::lowerConstant(CV, BaseCV, Offset);
4441}
4442
4443char AArch64AsmPrinter::ID = 0;
4444
4445INITIALIZE_PASS(AArch64AsmPrinter, "aarch64-asm-printer",
4446 "AArch64 Assembly Printer", false, false)
4447
4448// Force static initialization.
4449extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
4450LLVMInitializeAArch64AsmPrinter() {
4456}
4457
4460 AArch64AsmPrinter &AsmPrinter = static_cast<AArch64AsmPrinter &>(
4461 MAM.getResult<AsmPrinterAnalysis>(M).getPrinter());
4464 return PreservedAnalyses::all();
4465}
4466
4470 AArch64AsmPrinter &AsmPrinter = static_cast<AArch64AsmPrinter &>(
4472 .getCachedResult<AsmPrinterAnalysis>(*MF.getFunction().getParent())
4473 ->getPrinter());
4476 return PreservedAnalyses::all();
4477}
4478
4481 AArch64AsmPrinter &AsmPrinter = static_cast<AArch64AsmPrinter &>(
4482 MAM.getResult<AsmPrinterAnalysis>(M).getPrinter());
4485 return PreservedAnalyses::all();
4486}
#define GET_CB_OPC(IsImm, Width, ImmCond, RegCond)
static void emitAuthenticatedPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, const MCExpr *StubAuthPtrRef)
static bool getOptionalBooleanModuleFlag(Module &M, StringRef Name)
static bool targetSupportsIRelativeRelocation(const Triple &TT)
static PtrauthCheckMode getCheckMode(const MachineFunction *MF)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:656
Machine Check Debug Module
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static SDValue lowerConstant(SDValue Op, SelectionDAG &DAG, const RISCVSubtarget &Subtarget)
RISCVCC::CondCode CC
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file provides utility classes that use RAII to save and restore values.
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
This file defines the SmallString class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
std::unique_ptr< MCStreamer > && Streamer
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static bool printAsmMRegister(const X86AsmPrinter &P, const MachineInstr &MI, const MachineOperand &MO, char Mode, raw_ostream &O)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx, SMLoc Loc=SMLoc())
const SetOfInstructions & getLOHRelated() const
unsigned getJumpTableEntrySize(int Idx) const
MCSymbol * getJumpTableEntryPCRelSymbol(int Idx) const
static bool shouldSignReturnAddress(SignReturnAddress Condition, bool IsLRSpilled)
std::optional< std::string > getOutliningStyle() const
const MILOHContainer & getLOHContainer() const
void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym)
static const char * getRegisterName(MCRegister Reg, unsigned AltIdx=AArch64::NoRegAltName)
static bool isTailCallReturnInst(const MachineInstr &MI)
Returns true if MI is one of the TCRETURN* instructions.
AArch64MCInstLower - This class is used to lower an MachineInstr into an MCInst.
MCSymbol * GetGlobalValueSymbol(const GlobalValue *GV, unsigned TargetFlags) const
void Lower(const MachineInstr *MI, MCInst &OutMI) const
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
const AArch64Options & getCLOpts() const
virtual void emitARM64WinCFISaveRegP(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegQX(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveRegPX(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegQ(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegQP(unsigned Reg, int Offset)
virtual void emitAttributesSubsection(StringRef VendorName, AArch64BuildAttributes::SubsectionOptional IsOptional, AArch64BuildAttributes::SubsectionType ParameterType)
Build attributes implementation.
virtual void emitARM64WinCFISavePReg(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveFReg(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegI(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveFRegPX(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveRegX(unsigned Reg, int Offset)
virtual void emitARM64WinCFIAllocStack(unsigned Size)
virtual void emitARM64WinCFISaveFPLRX(int Offset)
virtual void emitARM64WinCFIAllocZ(int Offset)
virtual void emitAttribute(StringRef VendorName, unsigned Tag, uint64_t Value, std::string String)
virtual void emitDirectiveVariantPCS(MCSymbol *Symbol)
Callback used to implement the .variant_pcs directive.
virtual void emitARM64WinCFIAddFP(unsigned Size)
virtual void emitARM64WinCFISaveFPLR(int Offset)
virtual void emitARM64WinCFISaveFRegP(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegQPX(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveFRegX(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveZReg(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveReg(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveLRPair(unsigned Reg, int Offset)
virtual void emitARM64WinCFISaveAnyRegIP(unsigned Reg, int Offset)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:94
virtual void emitGlobalAlias(const Module &M, const GlobalAlias &GA)
virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB)
Targets can override this to emit stuff at the end of a basic block.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
virtual const MCExpr * lowerConstant(const Constant *CV, const Constant *BaseCV=nullptr, uint64_t Offset=0)
Lower the specified LLVM Constant to an MCExpr.
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
void getAnalysisUsage(AnalysisUsage &AU) const override
Record analysis usage.
bool doFinalization(Module &M) override
Shut down the asmprinter.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition AsmPrinter.h:477
virtual void emitXXStructor(const DataLayout &DL, const Constant *CV)
Targets can override this to change how global constants that are part of a C++ static/global constru...
Definition AsmPrinter.h:687
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
virtual const MCExpr * lowerBlockAddressConstant(const BlockAddress &BA)
Lower the specified BlockAddress to an MCExpr.
Function * getFunction() const
Definition Constants.h:1126
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Constant * getPointer() const
The pointer that is signed in this ptrauth signed pointer.
Definition Constants.h:1251
static LLVM_ABI ConstantPtrAuth * get(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc, Constant *AddrDisc, Constant *DeactivationSymbol)
Return a pointer signed with the specified parameters.
ConstantInt * getKey() const
The Key ID, an i32 constant.
Definition Constants.h:1254
Constant * getDeactivationSymbol() const
Definition Constants.h:1273
bool hasAddressDiscriminator() const
Whether there is any non-null address discriminator.
Definition Constants.h:1269
ConstantInt * getDiscriminator() const
The integer discriminator, an i64 constant, or 0.
Definition Constants.h:1257
LLVM_ABI void recordFaultingOp(FaultKind FaultTy, const MCSymbol *FaultingLabel, const MCSymbol *HandlerLabel)
Definition FaultMaps.cpp:27
LLVM_ABI void serializeToFaultMapSection()
Definition FaultMaps.cpp:44
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
const Constant * getAliasee() const
Definition GlobalAlias.h:87
const Constant * getResolver() const
Definition GlobalIFunc.h:73
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
bool hasLocalLinkage() const
bool hasExternalWeakLinkage() const
Type * getValueType() const
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:333
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static const MCBinaryExpr * createLShr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:422
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:427
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:550
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
LLVM_ABI MCSymbol * createLinkerPrivateSymbol(const Twine &Name)
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
void setFlags(unsigned F)
Definition MCInst.h:204
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
MCSection * getDataSection() const
void setImm(int64_t Val)
Definition MCInst.h:89
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
bool isReg() const
Definition MCInst.h:65
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
static constexpr unsigned NonUniqueID
Definition MCSection.h:585
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual void emitCFIBKeyFrame()
virtual bool popSection()
Restore the current and previous section from the section stack.
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
virtual void emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc={})
Record a relocation described by the .reloc directive.
virtual bool hasRawTextSupport() const
Return true if this asm streamer supports emitting unformatted text to the .s file with EmitRawText.
Definition MCStreamer.h:385
MCContext & getContext() const
Definition MCStreamer.h:326
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition MCStreamer.h:404
virtual void emitCFIMTETaggedFrame()
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
MCTargetStreamer * getTargetStreamer()
Definition MCStreamer.h:336
void pushSection()
Save the current and previous section on the section stack.
Definition MCStreamer.h:460
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition MCStreamer.h:438
void emitRawText(const Twine &String)
If this file is backed by a assembly streamer, this dumps the specified string in the output ....
virtual void emitCFINonHintPAuthFrame()
const FeatureBitset & getFeatureBits() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:60
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
const MCSymbol * getAddSym() const
Definition MCValue.h:49
int64_t getConstant() const
Definition MCValue.h:44
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
CalledGlobalInfo tryGetCalledGlobal(const MachineInstr *MI) const
Tries to get the global and target flags for a call site, if the instruction is a call to a global.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MCContext & getContext() const
Function & getFunction()
Return the LLVM function that this machine code represents.
bool usesWindowsUnwindInfo() const
True if this function describes its unwind information with Windows SEH unwind opcodes (....
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
mop_range operands()
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
const MachineOperand & getOperand(unsigned i) const
const std::vector< MachineJumpTableEntry > & getJumpTables() const
unsigned getSubReg() const
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
const GlobalValue * getGlobal() const
static MachineOperand CreateES(const char *SymName, unsigned TargetFlags=0)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const BlockAddress * getBlockAddress() const
void setOffset(int64_t Offset)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
int64_t getOffset() const
Return the offset from the symbol in this operand.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
Root of the metadata hierarchy.
Definition Metadata.h:65
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
static SectionKind getMetadata()
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void push_back(const T &Elt)
LLVM_ABI void recordStatepoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a statepoint instruction.
LLVM_ABI void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
LLVM_ABI void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
virtual MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const
Primary interface to the complete machine description for the target machine.
bool regsOverlap(Register RegA, Register RegB) const
Returns true if the two registers are equal or alias each other.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition Type.h:268
LLVM Value Representation.
Definition Value.h:75
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
self_iterator getIterator()
Definition ilist_node.h:123
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI StringRef getVendorName(unsigned const Vendor)
@ 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_G0
MO_G0 - A symbol operand with this flag (granule 0) represents the bits 0-15 of a 64-bit address,...
@ MO_PAGE
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
@ MO_TLS
MO_TLS - Indicates that the operand being accessed is some kind of thread-local symbol.
PtrauthCheckMode
Ways to check pointer authentication auth/resign failures.
constexpr AArch64PACKey::ID InitFiniKey
PAuth key to be used with function pointers in .init_array and .fini_array.
AuthCheckMethod
Variants of check performed on an authenticated pointer.
constexpr unsigned InitFiniPointerConstantDiscriminator
Constant discriminator to be used with function pointers in .init_array and .fini_array.
static unsigned getShiftValue(unsigned Imm)
getShiftValue - Extract the shift value.
static uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize)
encodeLogicalImmediate - Return the encoded immediate value for a logical immediate instruction of th...
static unsigned getShifterImm(AArch64_AM::ShiftExtendType ST, unsigned Imm)
getShifterImm - Encode the shift type and amount: imm: 6-bit shift amount shifter: 000 ==> lsl 001 ==...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ SectionSize
Definition COFF.h:61
SymbolStorageClass
Storage class tells where and what the symbol represents.
Definition COFF.h:218
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
Definition COFF.h:224
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition COFF.h:225
@ IMAGE_SYM_DTYPE_FUNCTION
A function that returns a base type.
Definition COFF.h:276
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition COFF.h:280
@ GNU_PROPERTY_AARCH64_FEATURE_1_BTI
Definition ELF.h:1716
@ GNU_PROPERTY_AARCH64_FEATURE_1_PAC
Definition ELF.h:1717
@ GNU_PROPERTY_AARCH64_FEATURE_1_GCS
Definition ELF.h:1718
@ SHF_ALLOC
Definition ELF.h:1098
@ SHF_GROUP
Definition ELF.h:1120
@ SHF_EXECINSTR
Definition ELF.h:1101
@ AARCH64_PAUTH_PLATFORM_LLVM_LINUX
Definition ELF.h:1725
@ SHT_PROGBITS
Definition ELF.h:996
@ S_REGULAR
S_REGULAR - Regular section.
Definition MachO.h:127
void emitInstruction(MCObjectStreamer &, const MCInst &Inst, const MCSubtargetInfo &STI)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:695
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:680
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
bool empty() const
Definition BasicBlock.h:101
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
LLVM_ABI std::optional< std::string > getArm64ECMangledFunctionName(StringRef Name)
Returns the ARM64EC mangled function name unless the input is already mangled.
Definition Mangler.cpp:292
SaveAndRestore(T &) -> SaveAndRestore< T >
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
scope_exit(Callable) -> scope_exit< Callable >
@ Store
The extracted value is stored (ExtractElement only).
static unsigned getXPACOpcodeForKey(AArch64PACKey::ID K)
Return XPAC opcode to be used for a ptrauth strip using the given key.
Target & getTheAArch64beTarget()
std::string utostr(uint64_t X, bool isNeg=false)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
static unsigned getBranchOpcodeForKey(bool IsCall, AArch64PACKey::ID K, bool Zero)
Return B(L)RA opcode to be used for an authenticated branch or call using the given key,...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
Target & getTheAArch64leTarget()
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
Target & getTheAArch64_32Target()
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI void setupModuleAsmPrinter(Module &M, ModuleAnalysisManager &MAM, AsmPrinter &AsmPrinter)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Target & getTheARM64_32Target()
static MCRegister getXRegFromWReg(MCRegister Reg)
@ Add
Sum of integers.
Target & getTheARM64Target()
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
static MCRegister getXRegFromXRegTuple(MCRegister RegTuple)
static unsigned getPACOpcodeForKey(AArch64PACKey::ID K, bool Zero)
Return PAC opcode to be used for a ptrauth sign using the given key, or its PAC*Z variant that doesn'...
static MCRegister getWRegFromXReg(MCRegister Reg)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
ExceptionHandling
Definition CodeGen.h:54
LLVM_ABI void setupMachineFunctionAsmPrinter(MachineFunctionAnalysisManager &MFAM, MachineFunction &MF, AsmPrinter &AsmPrinter)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
static unsigned getAUTOpcodeForKey(AArch64PACKey::ID K, bool Zero)
Return AUT opcode to be used for a ptrauth auth using the given key, or its AUT*Z variant that doesn'...
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:199
@ MCSA_Weak
.weak
@ MCSA_WeakAntiDep
.weak_anti_dep (COFF)
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
@ MCSA_Hidden
.hidden (ELF)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
#define EQ(a, b)
Definition regexec.c:65
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...