31 cl::desc(
"Enable the LFI guard elimination optimization"),
70#define GET_LFIVariantTable_DECL
71#define GET_PairVariantTable_DECL
72#define GET_SIMDPostTable_DECL
73#define GET_MemInfoTable_DECL
74#define GET_LFIVariantTable_IMPL
75#define GET_PairVariantTable_IMPL
76#define GET_SIMDPostTable_IMPL
77#define GET_MemInfoTable_IMPL
80#include "AArch64GenSystemOperands.inc"
103 return Reg == AArch64SysReg::TPIDR_EL1 ||
Reg == AArch64SysReg::TPIDR_EL2 ||
104 Reg == AArch64SysReg::TPIDR_EL3;
108 return Inst.
getOpcode() == AArch64::MRS &&
113 return Inst.
getOpcode() == AArch64::MSR &&
149 case AArch64::PRFMroW:
150 case AArch64::PRFMroX:
151 case AArch64::PRFMui:
152 case AArch64::PRFUMi:
184 assert(New.getOperand(Idx).isReg());
185 New.getOperand(Idx).setReg(NewReg);
199 if (!
E ||
E->AddrMode != ExpectedMode)
200 return AArch64::INSTRUCTION_LIST_END;
208 return AArch64::INSTRUCTION_LIST_END;
231 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(
Op)) {
235 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(
Op)) {
239 return AArch64::INSTRUCTION_LIST_END;
242bool AArch64MCLFIRewriter::mayModifySP(
const MCInst &Inst)
const {
246MCRegister AArch64MCLFIRewriter::mayModifyReserved(
const MCInst &Inst)
const {
256 ActiveGuardReg = std::nullopt;
263 if (ActiveGuardReg) {
265 if (
Desc.mayAffectControlFlow(Inst, *RegInfo) ||
269 ActiveGuardReg = std::nullopt;
291 emitInst(Inst, Out, STI);
295 ActiveGuardReg = Src;
304 emitInst(Branch, Out, STI);
307void AArch64MCLFIRewriter::emitPendingTLSDescCall(
MCStreamer &Out,
309 if (!PendingTLSDescCall)
314 PendingTLSDescCall =
nullptr;
315 emitInst(Marker, Out, STI);
328 emitInst(Inst, Out, STI);
334 assert(std::abs(Imm) <= 4095);
351 emitInst(Inst, Out, STI);
366 emitInst(Inst, Out, STI);
385 emitInst(Inst, Out, STI);
388void AArch64MCLFIRewriter::emitMemRoW(
unsigned Opcode,
const MCOperand &DataOp,
399 emitInst(Inst, Out, STI);
406void AArch64MCLFIRewriter::rewriteIndirectBranch(
const MCInst &Inst,
410 "expected register operand");
416 emitPendingTLSDescCall(Out, STI);
427void AArch64MCLFIRewriter::rewriteReturn(
const MCInst &Inst,
MCStreamer &Out,
430 "expected register operand");
433 rewriteIndirectBranch(Inst, Out, STI);
435 emitInst(Inst, Out, STI);
442void AArch64MCLFIRewriter::rewriteLRModification(
const MCInst &Inst,
447 rewriteLoadStore(Inst, Out, STI);
449 emitInst(Inst, Out, STI);
450 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
466 Load.setOpcode(AArch64::LDURXi);
470 emitInst(Load, Out, STI);
473 emitBranch(AArch64::BLR, AArch64::LR, Out, STI);
482void AArch64MCLFIRewriter::rewriteTPRead(
const MCInst &Inst,
MCStreamer &Out,
487 Load.setOpcode(AArch64::LDRXui);
491 emitInst(Load, Out, STI);
497void AArch64MCLFIRewriter::rewriteTPWrite(
const MCInst &Inst,
MCStreamer &Out,
502 Store.setOpcode(AArch64::STRXui);
506 emitInst(Store, Out, STI);
509bool AArch64MCLFIRewriter::rewriteLoadStoreRoW(
const MCInst &Inst,
518 AArch64::INSTRUCTION_LIST_END) {
520 if (BaseReg == AArch64::SP)
524 emitMemRoW(MemOp, Inst.
getOperand(0), BaseReg, Out, STI);
533 AArch64::INSTRUCTION_LIST_END) {
535 if (BaseReg == AArch64::SP)
538 emitAddImm(BaseReg, BaseReg, Imm, Out, STI);
539 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
546 AArch64::INSTRUCTION_LIST_END) {
548 if (BaseReg == AArch64::SP)
551 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
552 emitAddImm(BaseReg, BaseReg, Imm, Out, STI);
605void AArch64MCLFIRewriter::rewriteLoadStoreBase(
const MCInst &Inst,
609 const AArch64::MemInfoEntry *
Info = AArch64::lookupMemInfoByOpcode(Opcode);
612 warning(Inst,
"unknown addressing mode for memory instruction in LFI");
613 return emitInst(Inst, Out, STI);
617 return error(Inst,
"PC-relative literal loads are not supported in LFI");
623 bool BaseIsSP =
BaseReg == AArch64::SP;
626 return emitInst(Inst, Out, STI);
628 if (OffReg == AArch64::XZR || OffReg == AArch64::WZR)
629 return emitInst(Inst, Out, STI);
636 if (!
Info->IsPrePost) {
639 emitInst(NewInst, Out, STI);
644 bool IsNoOffset =
false;
647 if (BaseOpcode == AArch64::INSTRUCTION_LIST_END)
648 return error(Inst,
"unhandled pre/post-index instruction in LFI rewriter");
656 for (
int I = 1;
I <
Info->BaseIdx; ++
I)
660 MCRegister AccessBase = BaseIsSP ? AArch64::SP :
LFIAddrReg;
664 if (IsPre &&
Info->HasOffset)
666 else if (!IsNoOffset)
669 emitInst(NewInst, Out, STI);
671 if (!
Info->HasOffset)
683 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(Opcode))
686 emitAddImm(BaseReg, BaseReg,
Offset, Out, STI);
689 MCRegister OffReg =
OffsetOp.getReg();
690 if (OffReg == AArch64::XZR) {
691 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(Opcode))
692 emitAddImm(BaseReg, BaseReg,
E->NaturalOffset, Out, STI);
693 }
else if (OffReg != AArch64::WZR) {
699 emitAddReg(BaseReg, BaseReg, OffReg, 0, Out, STI);
705void AArch64MCLFIRewriter::rewriteLoadStore(
const MCInst &Inst,
MCStreamer &Out,
710 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
711 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
713 if ((!IsLoad || SkipLoads) && (!IsStore || SkipStores))
714 return emitInst(Inst, Out, STI);
716 if (rewriteLoadStoreRoW(Inst, Out, STI))
719 rewriteLoadStoreBase(Inst, Out, STI);
726void AArch64MCLFIRewriter::rewriteSPModification(
const MCInst &Inst,
734 return rewriteLRModification(Inst, Out, STI);
735 return rewriteLoadStore(Inst, Out, STI);
739 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
740 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
741 if (SkipLoads && SkipStores)
742 return emitInst(Inst, Out, STI);
751 emitInst(ModInst, Out, STI);
759void AArch64MCLFIRewriter::rewriteVASysOp(
const MCInst &Inst,
MCStreamer &Out,
772 emitInst(NewInst, Out, STI);
777void AArch64MCLFIRewriter::doRewriteInst(
const MCInst &Inst,
MCStreamer &Out,
779 if (Inst.
getOpcode() == AArch64::TLSDESCCALL) {
785 if (MCRegister
Reg = mayModifyReserved(Inst)) {
786 error(Inst, Twine(
"illegal modification of reserved LFI register ") +
793 return rewriteSyscall(Inst, Out, STI);
796 return rewriteTPRead(Inst, Out, STI);
799 return rewriteTPWrite(Inst, Out, STI);
802 error(Inst,
"illegal access to privileged thread pointer register");
807 return rewriteVASysOp(Inst, Out, STI);
812 return rewriteReturn(Inst, Out, STI);
815 return rewriteIndirectBranch(Inst, Out, STI);
819 if (mayModifySP(Inst))
820 return rewriteSPModification(Inst, Out, STI);
824 return rewriteLRModification(Inst, Out, STI);
829 return rewriteLoadStore(Inst, Out, STI);
831 emitInst(Inst, Out, STI);
838 AArch64::INSTRUCTION_LIST_END)
841 AArch64::INSTRUCTION_LIST_END)
843 bool IsPre, IsNoOffset;
845 AArch64::INSTRUCTION_LIST_END)
854 ActiveGuardReg = std::nullopt;
862 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 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 constexpr MCRegister LFICtxReg
static bool isPrivilegedTP(int64_t Reg)
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 constexpr MCRegister LFIBaseReg
static constexpr int LFISyscallOffset
static bool isTPWrite(const MCInst &Inst)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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) override
bool rewriteInst(const MCInst &Inst, MCStreamer &Out, const MCSubtargetInfo &STI) override
Instances of this class represent a single low-level machine instruction.
unsigned getNumOperands() const
unsigned getOpcode() const
void addOperand(const MCOperand Op)
void setOpcode(unsigned Op)
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 void warning(const MCInst &Inst, const Twine &Msg)
std::unique_ptr< MCRegisterInfo > RegInfo
LLVM_ABI bool mayStore(const MCInst &Inst) const
LLVM_ABI bool explicitlyModifiesRegister(const MCInst &Inst, MCRegister Reg) const
Instances of this class represent operands of the MCInst class.
static MCOperand createExpr(const MCExpr *Val)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
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.
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)