33 cl::desc(
"Enable the LFI guard elimination optimization"),
72#define GET_LFIVariantTable_DECL
73#define GET_PairVariantTable_DECL
74#define GET_SIMDPostTable_DECL
75#define GET_MemInfoTable_DECL
76#define GET_LFIVariantTable_IMPL
77#define GET_PairVariantTable_IMPL
78#define GET_SIMDPostTable_IMPL
79#define GET_MemInfoTable_IMPL
82#include "AArch64GenSystemOperands.inc"
105 return Reg == AArch64SysReg::TPIDR_EL1 ||
Reg == AArch64SysReg::TPIDR_EL2 ||
106 Reg == AArch64SysReg::TPIDR_EL3;
110 return Inst.
getOpcode() == AArch64::MRS &&
115 return Inst.
getOpcode() == AArch64::MSR &&
151 case AArch64::PRFMroW:
152 case AArch64::PRFMroX:
153 case AArch64::PRFMui:
154 case AArch64::PRFUMi:
176 case AArch64::BLRAAZ:
178 case AArch64::BLRABZ:
186 return Opcode == AArch64::RETAA || Opcode == AArch64::RETAB;
190 return Opcode == AArch64::ERET || Opcode == AArch64::ERETAA ||
191 Opcode == AArch64::ERETAB;
196 case AArch64::AUTIASP:
197 case AArch64::AUTIBSP:
198 case AArch64::AUTIAZ:
199 case AArch64::AUTIBZ:
200 case AArch64::XPACLRI:
232 assert(New.getOperand(Idx).isReg());
233 New.getOperand(Idx).setReg(NewReg);
247 if (!
E ||
E->AddrMode != ExpectedMode)
248 return AArch64::INSTRUCTION_LIST_END;
256 return AArch64::INSTRUCTION_LIST_END;
279 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(
Op)) {
283 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(
Op)) {
287 return AArch64::INSTRUCTION_LIST_END;
290bool AArch64MCLFIRewriter::mayModifySP(
const MCInst &Inst)
const {
294MCRegister AArch64MCLFIRewriter::mayModifyReserved(
const MCInst &Inst)
const {
308 if (DeferredLRGuard && LastSTI && !Symbol->isTemporary()) {
309 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
310 DeferredLRGuard =
false;
314 ActiveGuardReg = std::nullopt;
319 if (DeferredLRGuard && LastSTI) {
320 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
321 DeferredLRGuard =
false;
329 if (ActiveGuardReg) {
331 if (
Desc.mayAffectControlFlow(Inst, *RegInfo) ||
335 ActiveGuardReg = std::nullopt;
338 Out.emitInstruction(Inst, STI);
350 emitInst(MCInstBuilder(AArch64::ADDXrx)
359 ActiveGuardReg = Src;
365 emitInst(MCInstBuilder(Opcode).addReg(Target), Out, STI);
368void AArch64MCLFIRewriter::emitPendingTLSDescCall(
MCStreamer &Out,
370 if (!PendingTLSDescCall)
372 const MCExpr *Expr = PendingTLSDescCall;
373 PendingTLSDescCall =
nullptr;
374 emitInst(MCInstBuilder(AArch64::TLSDESCCALL).addExpr(Expr), Out, STI);
381 emitInst(MCInstBuilder(AArch64::ORRXrs)
383 .addReg(AArch64::XZR)
394 unsigned Opcode =
Imm >= 0 ? AArch64::ADDXri : AArch64::SUBXri;
395 emitInst(MCInstBuilder(Opcode)
398 .addImm(std::abs(
Imm))
408 emitInst(MCInstBuilder(AArch64::ADDXrs)
425 emitInst(MCInstBuilder(Opcode).addReg(Dest).addReg(Src1).addReg(Src2).addImm(
430void AArch64MCLFIRewriter::emitMemRoW(
unsigned Opcode,
const MCOperand &DataOp,
434 emitInst(MCInstBuilder(Opcode)
447void AArch64MCLFIRewriter::rewriteIndirectBranch(
const MCInst &Inst,
451 "expected register operand");
457 emitPendingTLSDescCall(Out, STI);
468void AArch64MCLFIRewriter::rewriteReturn(
const MCInst &Inst,
MCStreamer &Out,
471 "expected register operand");
474 rewriteIndirectBranch(Inst, Out, STI);
476 emitInst(Inst, Out, STI);
483void AArch64MCLFIRewriter::rewriteLRModification(
const MCInst &Inst,
488 rewriteLoadStore(Inst, Out, STI);
490 emitInst(Inst, Out, STI);
495 DeferredLRGuard =
true;
503void AArch64MCLFIRewriter::rewriteAuthenticatedReturn(
505 emitInst(MCInstBuilder(Inst.
getOpcode() == AArch64::RETAA ? AArch64::AUTIASP
509 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
510 emitBranch(AArch64::RET, AArch64::LR, Out, STI);
518void AArch64MCLFIRewriter::rewriteAuthenticatedBranchOrCall(
528 AuthOpcode = AArch64::AUTIA;
532 AuthOpcode = AArch64::AUTIB;
535 case AArch64::BLRAAZ:
536 AuthOpcode = AArch64::AUTIZA;
539 case AArch64::BLRABZ:
540 AuthOpcode = AArch64::AUTIZB;
546 MCInstBuilder Auth(AuthOpcode);
547 Auth.addReg(TargetReg);
548 Auth.addReg(TargetReg);
549 if (AuthOpcode == AArch64::AUTIA || AuthOpcode == AArch64::AUTIB)
551 emitInst(Auth, Out, STI);
555 emitBranch(BranchOpcode,
LFIAddrReg, Out, STI);
570 emitInst(MCInstBuilder(AArch64::LDURXi)
577 emitBranch(AArch64::BLR, AArch64::LR, Out, STI);
586void AArch64MCLFIRewriter::rewriteTPRead(
const MCInst &Inst,
MCStreamer &Out,
590 emitInst(MCInstBuilder(AArch64::LDRXui)
600void AArch64MCLFIRewriter::rewriteTPWrite(
const MCInst &Inst,
MCStreamer &Out,
604 emitInst(MCInstBuilder(AArch64::STRXui)
611bool AArch64MCLFIRewriter::rewriteLoadStoreRoW(
const MCInst &Inst,
620 AArch64::INSTRUCTION_LIST_END) {
622 if (BaseReg == AArch64::SP)
626 emitMemRoW(MemOp, Inst.
getOperand(0), BaseReg, Out, STI);
635 AArch64::INSTRUCTION_LIST_END) {
637 if (BaseReg == AArch64::SP)
640 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
641 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
648 AArch64::INSTRUCTION_LIST_END) {
650 if (BaseReg == AArch64::SP)
653 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
654 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
707void AArch64MCLFIRewriter::rewriteLoadStoreBase(
const MCInst &Inst,
711 const AArch64::MemInfoEntry *
Info = AArch64::lookupMemInfoByOpcode(Opcode);
714 warning(Inst,
"unknown addressing mode for memory instruction in LFI");
715 return emitInst(Inst, Out, STI);
719 return error(Inst,
"PC-relative literal loads are not supported in LFI");
725 bool BaseIsSP =
BaseReg == AArch64::SP;
728 return emitInst(Inst, Out, STI);
730 if (OffReg == AArch64::XZR || OffReg == AArch64::WZR)
731 return emitInst(Inst, Out, STI);
738 if (!
Info->IsPrePost) {
741 emitInst(NewInst, Out, STI);
746 bool IsNoOffset =
false;
749 if (BaseOpcode == AArch64::INSTRUCTION_LIST_END)
750 return error(Inst,
"unhandled pre/post-index instruction in LFI rewriter");
753 MCInstBuilder NewInst(BaseOpcode);
754 NewInst.setLoc(Inst.
getLoc());
757 for (
int I = 1;
I <
Info->BaseIdx; ++
I)
761 NewInst.addReg(BaseIsSP ? AArch64::SP :
LFIAddrReg);
764 if (IsPre &&
Info->HasOffset)
766 else if (!IsNoOffset)
769 emitInst(NewInst, Out, STI);
771 if (!
Info->HasOffset)
783 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(Opcode))
786 emitAddImm(BaseReg, BaseReg,
Offset, Out, STI);
789 MCRegister OffReg =
OffsetOp.getReg();
790 if (OffReg == AArch64::XZR) {
791 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(Opcode))
792 emitAddImm(BaseReg, BaseReg,
E->NaturalOffset, Out, STI);
793 }
else if (OffReg != AArch64::WZR) {
799 emitAddReg(BaseReg, BaseReg, OffReg, 0, Out, STI);
805void AArch64MCLFIRewriter::rewriteLoadStore(
const MCInst &Inst,
MCStreamer &Out,
810 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
811 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
813 if ((!IsLoad || SkipLoads) && (!IsStore || SkipStores))
814 return emitInst(Inst, Out, STI);
816 if (rewriteLoadStoreRoW(Inst, Out, STI))
819 rewriteLoadStoreBase(Inst, Out, STI);
826void AArch64MCLFIRewriter::rewriteSPModification(
const MCInst &Inst,
834 return rewriteLRModification(Inst, Out, STI);
835 return rewriteLoadStore(Inst, Out, STI);
839 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
840 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
841 if (SkipLoads && SkipStores)
842 return emitInst(Inst, Out, STI);
851 emitInst(ModInst, Out, STI);
859void AArch64MCLFIRewriter::rewriteVASysOp(
const MCInst &Inst,
MCStreamer &Out,
865 emitInst(MCInstBuilder(AArch64::SYSxt)
876void AArch64MCLFIRewriter::doRewriteInst(
const MCInst &Inst,
MCStreamer &Out,
878 if (Inst.
getOpcode() == AArch64::TLSDESCCALL) {
884 if (MCRegister
Reg = mayModifyReserved(Inst)) {
885 error(Inst, Twine(
"illegal modification of reserved LFI register ") +
892 return rewriteSyscall(Inst, Out, STI);
895 return rewriteTPRead(Inst, Out, STI);
898 return rewriteTPWrite(Inst, Out, STI);
901 error(Inst,
"illegal access to privileged thread pointer register");
906 return rewriteVASysOp(Inst, Out, STI);
909 error(Inst,
"exception returns are not supported by LFI");
917 DeferredLRGuard =
false;
918 return rewriteAuthenticatedReturn(Inst, Out, STI);
925 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
926 DeferredLRGuard =
false;
931 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BR, Out, STI);
933 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BLR, Out, STI);
938 return rewriteReturn(Inst, Out, STI);
941 return rewriteIndirectBranch(Inst, Out, STI);
945 if (mayModifySP(Inst))
946 return rewriteSPModification(Inst, Out, STI);
951 return rewriteLRModification(Inst, Out, STI);
956 return rewriteLoadStore(Inst, Out, STI);
958 emitInst(Inst, Out, STI);
965 AArch64::INSTRUCTION_LIST_END)
968 AArch64::INSTRUCTION_LIST_END)
970 bool IsPre, IsNoOffset;
972 AArch64::INSTRUCTION_LIST_END)
981 ActiveGuardReg = std::nullopt;
993 doRewriteInst(Inst, Out, STI);
static unsigned convertPrePostToBase(unsigned Op, bool &IsPre, bool &IsNoOffset)
static bool isFakeMemAccess(const MCInst &Inst)
static constexpr unsigned LFITPOffset
static constexpr MCRegister LFIScratchReg
static bool pacWritesLR(const MCInst &Inst)
static bool isPrivilegedTPAccess(const MCInst &Inst)
static cl::opt< bool > LFIGuardElim("aarch64-lfi-guard-elim", cl::Hidden, cl::desc("Enable the LFI guard elimination optimization"), cl::init(true))
static bool isAuthenticatedBranch(unsigned Opcode)
static constexpr MCRegister LFICtxReg
static bool isPrivilegedTP(int64_t Reg)
static bool isAuthenticatedReturn(unsigned Opcode)
static bool getRoWShift(unsigned Op, unsigned &Shift)
static bool isVASysOp(const MCInst &Inst)
static bool isTPRead(const MCInst &Inst)
static bool mayPrefetch(const MCInst &Inst)
static bool isSyscall(const MCInst &Inst)
static MCInst replaceRegAt(const MCInst &Inst, unsigned Idx, MCRegister NewReg)
static unsigned convertVariantToRoW(unsigned Op, unsigned ExpectedMode)
static constexpr MCRegister LFIAddrReg
static unsigned convertRoXToRoW(unsigned Op, unsigned &Shift)
static bool isExceptionReturn(unsigned Opcode)
static constexpr MCRegister LFIBaseReg
static constexpr int LFISyscallOffset
static bool isTPWrite(const MCInst &Inst)
static bool isAuthenticatedCall(unsigned Opcode)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
void onLabel(const MCSymbol *Symbol, MCStreamer &Out) override
bool rewriteInst(const MCInst &Inst, MCStreamer &Out, const MCSubtargetInfo &STI) override
void finish(MCStreamer &Out) override
Instances of this class represent a single low-level machine instruction.
unsigned getNumOperands() const
unsigned getOpcode() const
const MCOperand & getOperand(unsigned i) const
Describe properties that are true of each instruction in the target description file.
LLVM_ABI bool mayModifyRegister(const MCInst &Inst, MCRegister Reg) const
LLVM_ABI bool mayLoad(const MCInst &Inst) const
LLVM_ABI bool isIndirectBranch(const MCInst &Inst) const
LLVM_ABI void warning(const MCInst &Inst, const Twine &Msg)
LLVM_ABI bool isCall(const MCInst &Inst) const
LLVM_ABI bool isReturn(const MCInst &Inst) const
std::unique_ptr< MCRegisterInfo > RegInfo
LLVM_ABI bool mayStore(const MCInst &Inst) const
LLVM_ABI bool explicitlyModifiesRegister(const MCInst &Inst, MCRegister Reg) const
LLVM_ABI bool isBranch(const MCInst &Inst) const
Instances of this class represent operands of the MCInst class.
MCRegister getReg() const
Returns the register number.
const MCExpr * getExpr() const
Wrapper class representing physical registers. Should be passed by value.
Streaming machine code generation interface.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getArithExtendImm(AArch64_AM::ShiftExtendType ET, unsigned Imm)
getArithExtendImm - Encode the extend type and shift amount for an arithmetic instruction: imm: 3-bit...
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 ==...
initializer< Ty > init(const Ty &Val)
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
bool isLFIPrePostMemAccess(unsigned Opcode)
Returns true if Opcode is a pre- or post-indexed memory access that the LFI rewriter expands with a b...
DWARFExpression::Operation Op
static MCRegister getWRegFromXReg(MCRegister Reg)