32 cl::desc(
"Enable the LFI guard elimination optimization"),
71#define GET_LFIVariantTable_DECL
72#define GET_PairVariantTable_DECL
73#define GET_SIMDPostTable_DECL
74#define GET_MemInfoTable_DECL
75#define GET_LFIVariantTable_IMPL
76#define GET_PairVariantTable_IMPL
77#define GET_SIMDPostTable_IMPL
78#define GET_MemInfoTable_IMPL
81#include "AArch64GenSystemOperands.inc"
104 return Reg == AArch64SysReg::TPIDR_EL1 ||
Reg == AArch64SysReg::TPIDR_EL2 ||
105 Reg == AArch64SysReg::TPIDR_EL3;
109 return Inst.
getOpcode() == AArch64::MRS &&
114 return Inst.
getOpcode() == AArch64::MSR &&
150 case AArch64::PRFMroW:
151 case AArch64::PRFMroX:
152 case AArch64::PRFMui:
153 case AArch64::PRFUMi:
175 case AArch64::BLRAAZ:
177 case AArch64::BLRABZ:
185 return Opcode == AArch64::RETAA || Opcode == AArch64::RETAB;
189 return Opcode == AArch64::ERET || Opcode == AArch64::ERETAA ||
190 Opcode == AArch64::ERETAB;
195 case AArch64::AUTIASP:
196 case AArch64::AUTIBSP:
197 case AArch64::AUTIAZ:
198 case AArch64::AUTIBZ:
199 case AArch64::XPACLRI:
231 assert(New.getOperand(Idx).isReg());
232 New.getOperand(Idx).setReg(NewReg);
246 if (!
E ||
E->AddrMode != ExpectedMode)
247 return AArch64::INSTRUCTION_LIST_END;
255 return AArch64::INSTRUCTION_LIST_END;
278 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(
Op)) {
282 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(
Op)) {
286 return AArch64::INSTRUCTION_LIST_END;
289bool AArch64MCLFIRewriter::mayModifySP(
const MCInst &Inst)
const {
293MCRegister AArch64MCLFIRewriter::mayModifyReserved(
const MCInst &Inst)
const {
307 if (DeferredLRGuard && LastSTI) {
308 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
309 DeferredLRGuard =
false;
313 ActiveGuardReg = std::nullopt;
318 if (DeferredLRGuard && LastSTI) {
319 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
320 DeferredLRGuard =
false;
328 if (ActiveGuardReg) {
330 if (
Desc.mayAffectControlFlow(Inst, *RegInfo) ||
334 ActiveGuardReg = std::nullopt;
349 emitInst(MCInstBuilder(AArch64::ADDXrx)
358 ActiveGuardReg = Src;
364 emitInst(MCInstBuilder(Opcode).addReg(Target), Out, STI);
367void AArch64MCLFIRewriter::emitPendingTLSDescCall(
MCStreamer &Out,
369 if (!PendingTLSDescCall)
371 const MCExpr *Expr = PendingTLSDescCall;
372 PendingTLSDescCall =
nullptr;
373 emitInst(MCInstBuilder(AArch64::TLSDESCCALL).addExpr(Expr), Out, STI);
380 emitInst(MCInstBuilder(AArch64::ORRXrs)
382 .addReg(AArch64::XZR)
393 unsigned Opcode =
Imm >= 0 ? AArch64::ADDXri : AArch64::SUBXri;
394 emitInst(MCInstBuilder(Opcode)
397 .addImm(std::abs(
Imm))
407 emitInst(MCInstBuilder(AArch64::ADDXrs)
424 emitInst(MCInstBuilder(Opcode).addReg(Dest).addReg(Src1).addReg(Src2).addImm(
429void AArch64MCLFIRewriter::emitMemRoW(
unsigned Opcode,
const MCOperand &DataOp,
433 emitInst(MCInstBuilder(Opcode)
446void AArch64MCLFIRewriter::rewriteIndirectBranch(
const MCInst &Inst,
450 "expected register operand");
456 emitPendingTLSDescCall(Out, STI);
467void AArch64MCLFIRewriter::rewriteReturn(
const MCInst &Inst,
MCStreamer &Out,
470 "expected register operand");
473 rewriteIndirectBranch(Inst, Out, STI);
475 emitInst(Inst, Out, STI);
482void AArch64MCLFIRewriter::rewriteLRModification(
const MCInst &Inst,
487 rewriteLoadStore(Inst, Out, STI);
489 emitInst(Inst, Out, STI);
494 DeferredLRGuard =
true;
502void AArch64MCLFIRewriter::rewriteAuthenticatedReturn(
504 emitInst(MCInstBuilder(Inst.
getOpcode() == AArch64::RETAA ? AArch64::AUTIASP
508 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
509 emitBranch(AArch64::RET, AArch64::LR, Out, STI);
517void AArch64MCLFIRewriter::rewriteAuthenticatedBranchOrCall(
527 AuthOpcode = AArch64::AUTIA;
531 AuthOpcode = AArch64::AUTIB;
534 case AArch64::BLRAAZ:
535 AuthOpcode = AArch64::AUTIZA;
538 case AArch64::BLRABZ:
539 AuthOpcode = AArch64::AUTIZB;
545 MCInstBuilder Auth(AuthOpcode);
546 Auth.addReg(TargetReg);
547 Auth.addReg(TargetReg);
548 if (AuthOpcode == AArch64::AUTIA || AuthOpcode == AArch64::AUTIB)
550 emitInst(Auth, Out, STI);
554 emitBranch(BranchOpcode,
LFIAddrReg, Out, STI);
569 emitInst(MCInstBuilder(AArch64::LDURXi)
576 emitBranch(AArch64::BLR, AArch64::LR, Out, STI);
585void AArch64MCLFIRewriter::rewriteTPRead(
const MCInst &Inst,
MCStreamer &Out,
589 emitInst(MCInstBuilder(AArch64::LDRXui)
599void AArch64MCLFIRewriter::rewriteTPWrite(
const MCInst &Inst,
MCStreamer &Out,
603 emitInst(MCInstBuilder(AArch64::STRXui)
610bool AArch64MCLFIRewriter::rewriteLoadStoreRoW(
const MCInst &Inst,
619 AArch64::INSTRUCTION_LIST_END) {
621 if (BaseReg == AArch64::SP)
625 emitMemRoW(MemOp, Inst.
getOperand(0), BaseReg, Out, STI);
634 AArch64::INSTRUCTION_LIST_END) {
636 if (BaseReg == AArch64::SP)
639 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
640 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
647 AArch64::INSTRUCTION_LIST_END) {
649 if (BaseReg == AArch64::SP)
652 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
653 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
706void AArch64MCLFIRewriter::rewriteLoadStoreBase(
const MCInst &Inst,
710 const AArch64::MemInfoEntry *
Info = AArch64::lookupMemInfoByOpcode(Opcode);
713 warning(Inst,
"unknown addressing mode for memory instruction in LFI");
714 return emitInst(Inst, Out, STI);
718 return error(Inst,
"PC-relative literal loads are not supported in LFI");
724 bool BaseIsSP =
BaseReg == AArch64::SP;
727 return emitInst(Inst, Out, STI);
729 if (OffReg == AArch64::XZR || OffReg == AArch64::WZR)
730 return emitInst(Inst, Out, STI);
737 if (!
Info->IsPrePost) {
740 emitInst(NewInst, Out, STI);
745 bool IsNoOffset =
false;
748 if (BaseOpcode == AArch64::INSTRUCTION_LIST_END)
749 return error(Inst,
"unhandled pre/post-index instruction in LFI rewriter");
752 MCInstBuilder NewInst(BaseOpcode);
753 NewInst.setLoc(Inst.
getLoc());
756 for (
int I = 1;
I <
Info->BaseIdx; ++
I)
760 NewInst.addReg(BaseIsSP ? AArch64::SP :
LFIAddrReg);
763 if (IsPre &&
Info->HasOffset)
765 else if (!IsNoOffset)
768 emitInst(NewInst, Out, STI);
770 if (!
Info->HasOffset)
782 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(Opcode))
785 emitAddImm(BaseReg, BaseReg,
Offset, Out, STI);
788 MCRegister OffReg =
OffsetOp.getReg();
789 if (OffReg == AArch64::XZR) {
790 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(Opcode))
791 emitAddImm(BaseReg, BaseReg,
E->NaturalOffset, Out, STI);
792 }
else if (OffReg != AArch64::WZR) {
798 emitAddReg(BaseReg, BaseReg, OffReg, 0, Out, STI);
804void AArch64MCLFIRewriter::rewriteLoadStore(
const MCInst &Inst,
MCStreamer &Out,
809 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
810 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
812 if ((!IsLoad || SkipLoads) && (!IsStore || SkipStores))
813 return emitInst(Inst, Out, STI);
815 if (rewriteLoadStoreRoW(Inst, Out, STI))
818 rewriteLoadStoreBase(Inst, Out, STI);
825void AArch64MCLFIRewriter::rewriteSPModification(
const MCInst &Inst,
833 return rewriteLRModification(Inst, Out, STI);
834 return rewriteLoadStore(Inst, Out, STI);
838 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
839 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
840 if (SkipLoads && SkipStores)
841 return emitInst(Inst, Out, STI);
850 emitInst(ModInst, Out, STI);
858void AArch64MCLFIRewriter::rewriteVASysOp(
const MCInst &Inst,
MCStreamer &Out,
864 emitInst(MCInstBuilder(AArch64::SYSxt)
875void AArch64MCLFIRewriter::doRewriteInst(
const MCInst &Inst,
MCStreamer &Out,
877 if (Inst.
getOpcode() == AArch64::TLSDESCCALL) {
883 if (MCRegister
Reg = mayModifyReserved(Inst)) {
884 error(Inst, Twine(
"illegal modification of reserved LFI register ") +
891 return rewriteSyscall(Inst, Out, STI);
894 return rewriteTPRead(Inst, Out, STI);
897 return rewriteTPWrite(Inst, Out, STI);
900 error(Inst,
"illegal access to privileged thread pointer register");
905 return rewriteVASysOp(Inst, Out, STI);
908 error(Inst,
"exception returns are not supported by LFI");
916 DeferredLRGuard =
false;
917 return rewriteAuthenticatedReturn(Inst, Out, STI);
924 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
925 DeferredLRGuard =
false;
930 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BR, Out, STI);
932 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BLR, Out, STI);
937 return rewriteReturn(Inst, Out, STI);
940 return rewriteIndirectBranch(Inst, Out, STI);
944 if (mayModifySP(Inst))
945 return rewriteSPModification(Inst, Out, STI);
950 return rewriteLRModification(Inst, Out, STI);
955 return rewriteLoadStore(Inst, Out, STI);
957 emitInst(Inst, Out, STI);
964 AArch64::INSTRUCTION_LIST_END)
967 AArch64::INSTRUCTION_LIST_END)
969 bool IsPre, IsNoOffset;
971 AArch64::INSTRUCTION_LIST_END)
980 ActiveGuardReg = std::nullopt;
992 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.
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
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)