67#define GET_LFIVariantTable_DECL
68#define GET_PairVariantTable_DECL
69#define GET_SIMDPostTable_DECL
70#define GET_MemInfoTable_DECL
71#define GET_LFIVariantTable_IMPL
72#define GET_PairVariantTable_IMPL
73#define GET_SIMDPostTable_IMPL
74#define GET_MemInfoTable_IMPL
77#include "AArch64GenSystemOperands.inc"
96 std::unique_ptr<MCRegisterInfo> &&RI,
97 std::unique_ptr<MCInstrInfo> &&
II)
99 CLOpts(
AArch64_MC::getCLOpts(Ctx.getTargetOptions())) {}
106 return Reg == AArch64SysReg::TPIDR_EL1 ||
Reg == AArch64SysReg::TPIDR_EL2 ||
107 Reg == AArch64SysReg::TPIDR_EL3;
111 return Inst.
getOpcode() == AArch64::MRS &&
116 return Inst.
getOpcode() == AArch64::MSR &&
152 case AArch64::PRFMroW:
153 case AArch64::PRFMroX:
154 case AArch64::PRFMui:
155 case AArch64::PRFUMi:
177 case AArch64::BLRAAZ:
179 case AArch64::BLRABZ:
187 return Opcode == AArch64::RETAA || Opcode == AArch64::RETAB;
191 return Opcode == AArch64::ERET || Opcode == AArch64::ERETAA ||
192 Opcode == AArch64::ERETAB;
197 case AArch64::AUTIASP:
198 case AArch64::AUTIBSP:
199 case AArch64::AUTIAZ:
200 case AArch64::AUTIBZ:
201 case AArch64::XPACLRI:
209 return Inst.
getOpcode() == AArch64::HINT &&
239 assert(New.getOperand(Idx).isReg());
240 New.getOperand(Idx).setReg(NewReg);
254 if (!
E ||
E->AddrMode != ExpectedMode)
255 return AArch64::INSTRUCTION_LIST_END;
263 return AArch64::INSTRUCTION_LIST_END;
286 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(
Op)) {
290 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(
Op)) {
294 return AArch64::INSTRUCTION_LIST_END;
297bool AArch64MCLFIRewriter::mayModifySP(
const MCInst &Inst)
const {
301MCRegister AArch64MCLFIRewriter::mayModifyReserved(
const MCInst &Inst)
const {
315 if (DeferredLRGuard && LastSTI && !Symbol->isTemporary()) {
316 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
317 DeferredLRGuard =
false;
321 ActiveGuardReg = std::nullopt;
326 if (DeferredLRGuard && LastSTI) {
327 emitAddMask(AArch64::LR, AArch64::LR, Out, *LastSTI);
328 DeferredLRGuard =
false;
336 if (ActiveGuardReg) {
338 if (
Desc.mayAffectControlFlow(Inst, *RegInfo) ||
342 ActiveGuardReg = std::nullopt;
345 Out.emitInstruction(Inst, STI);
353 if (CLOpts.lfi_guard_elim && Dest ==
LFIAddrReg && ActiveGuardReg == Src)
357 emitInst(MCInstBuilder(AArch64::ADDXrx)
366 ActiveGuardReg = Src;
372 emitInst(MCInstBuilder(Opcode).addReg(Target), Out, STI);
375void AArch64MCLFIRewriter::emitPendingTLSDescCall(
MCStreamer &Out,
377 if (!PendingTLSDescCall)
379 const MCExpr *Expr = PendingTLSDescCall;
380 PendingTLSDescCall =
nullptr;
381 emitInst(MCInstBuilder(AArch64::TLSDESCCALL).addExpr(Expr), Out, STI);
388 emitInst(MCInstBuilder(AArch64::ORRXrs)
390 .addReg(AArch64::XZR)
401 unsigned Opcode =
Imm >= 0 ? AArch64::ADDXri : AArch64::SUBXri;
402 emitInst(MCInstBuilder(Opcode)
405 .addImm(std::abs(
Imm))
415 emitInst(MCInstBuilder(AArch64::ADDXrs)
432 emitInst(MCInstBuilder(Opcode).addReg(Dest).addReg(Src1).addReg(Src2).addImm(
437void AArch64MCLFIRewriter::emitMemRoW(
unsigned Opcode,
const MCOperand &DataOp,
441 emitInst(MCInstBuilder(Opcode)
454void AArch64MCLFIRewriter::rewriteIndirectBranch(
const MCInst &Inst,
458 "expected register operand");
464 emitPendingTLSDescCall(Out, STI);
475void AArch64MCLFIRewriter::rewriteReturn(
const MCInst &Inst,
MCStreamer &Out,
478 "expected register operand");
481 rewriteIndirectBranch(Inst, Out, STI);
483 emitInst(Inst, Out, STI);
490void AArch64MCLFIRewriter::rewriteLRModification(
const MCInst &Inst,
495 rewriteLoadStore(Inst, Out, STI);
497 emitInst(Inst, Out, STI);
502 DeferredLRGuard =
true;
510void AArch64MCLFIRewriter::rewriteAuthenticatedReturn(
512 emitInst(MCInstBuilder(Inst.
getOpcode() == AArch64::RETAA ? AArch64::AUTIASP
516 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
517 emitBranch(AArch64::RET, AArch64::LR, Out, STI);
525void AArch64MCLFIRewriter::rewriteAuthenticatedBranchOrCall(
535 AuthOpcode = AArch64::AUTIA;
539 AuthOpcode = AArch64::AUTIB;
542 case AArch64::BLRAAZ:
543 AuthOpcode = AArch64::AUTIZA;
546 case AArch64::BLRABZ:
547 AuthOpcode = AArch64::AUTIZB;
553 MCInstBuilder Auth(AuthOpcode);
554 Auth.addReg(TargetReg);
555 Auth.addReg(TargetReg);
556 if (AuthOpcode == AArch64::AUTIA || AuthOpcode == AArch64::AUTIB)
558 emitInst(Auth, Out, STI);
562 emitBranch(BranchOpcode,
LFIAddrReg, Out, STI);
577 emitInst(MCInstBuilder(AArch64::LDURXi)
584 emitBranch(AArch64::BLR, AArch64::LR, Out, STI);
593void AArch64MCLFIRewriter::rewriteTPRead(
const MCInst &Inst,
MCStreamer &Out,
597 emitInst(MCInstBuilder(AArch64::LDRXui)
607void AArch64MCLFIRewriter::rewriteTPWrite(
const MCInst &Inst,
MCStreamer &Out,
611 emitInst(MCInstBuilder(AArch64::STRXui)
618bool AArch64MCLFIRewriter::rewriteLoadStoreRoW(
const MCInst &Inst,
627 AArch64::INSTRUCTION_LIST_END) {
629 if (BaseReg == AArch64::SP)
633 emitMemRoW(MemOp, Inst.
getOperand(0), BaseReg, Out, STI);
642 AArch64::INSTRUCTION_LIST_END) {
644 if (BaseReg == AArch64::SP)
647 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
648 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
655 AArch64::INSTRUCTION_LIST_END) {
657 if (BaseReg == AArch64::SP)
660 emitMemRoW(MemOp, Inst.
getOperand(1), BaseReg, Out, STI);
661 emitAddImm(BaseReg, BaseReg,
Imm, Out, STI);
714void AArch64MCLFIRewriter::rewriteLoadStoreBase(
const MCInst &Inst,
718 const AArch64::MemInfoEntry *
Info = AArch64::lookupMemInfoByOpcode(Opcode);
721 warning(Inst,
"unknown addressing mode for memory instruction in LFI");
722 return emitInst(Inst, Out, STI);
726 return error(Inst,
"PC-relative literal loads are not supported in LFI");
732 bool BaseIsSP =
BaseReg == AArch64::SP;
735 return emitInst(Inst, Out, STI);
737 if (OffReg == AArch64::XZR || OffReg == AArch64::WZR)
738 return emitInst(Inst, Out, STI);
745 if (!
Info->IsPrePost) {
748 emitInst(NewInst, Out, STI);
753 bool IsNoOffset =
false;
756 if (BaseOpcode == AArch64::INSTRUCTION_LIST_END)
757 return error(Inst,
"unhandled pre/post-index instruction in LFI rewriter");
760 MCInstBuilder NewInst(BaseOpcode);
761 NewInst.setLoc(Inst.
getLoc());
764 for (
int I = 1;
I <
Info->BaseIdx; ++
I)
768 NewInst.addReg(BaseIsSP ? AArch64::SP :
LFIAddrReg);
771 if (IsPre &&
Info->HasOffset)
773 else if (!IsNoOffset)
776 emitInst(NewInst, Out, STI);
778 if (!
Info->HasOffset)
790 if (
const auto *
E = AArch64::lookupPairVariantByOpcode(Opcode))
793 emitAddImm(BaseReg, BaseReg,
Offset, Out, STI);
796 MCRegister OffReg =
OffsetOp.getReg();
797 if (OffReg == AArch64::XZR) {
798 if (
const auto *
E = AArch64::lookupSIMDPostByOpcode(Opcode))
799 emitAddImm(BaseReg, BaseReg,
E->NaturalOffset, Out, STI);
800 }
else if (OffReg != AArch64::WZR) {
806 emitAddReg(BaseReg, BaseReg, OffReg, 0, Out, STI);
812void AArch64MCLFIRewriter::rewriteLoadStore(
const MCInst &Inst,
MCStreamer &Out,
817 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
818 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
820 if ((!IsLoad || SkipLoads) && (!IsStore || SkipStores))
821 return emitInst(Inst, Out, STI);
823 if (rewriteLoadStoreRoW(Inst, Out, STI))
826 rewriteLoadStoreBase(Inst, Out, STI);
833void AArch64MCLFIRewriter::rewriteSPModification(
const MCInst &Inst,
841 return rewriteLRModification(Inst, Out, STI);
842 return rewriteLoadStore(Inst, Out, STI);
846 bool SkipLoads = STI.
hasFeature(AArch64::FeatureNoLFILoads);
847 bool SkipStores = STI.
hasFeature(AArch64::FeatureNoLFIStores);
848 if (SkipLoads && SkipStores)
849 return emitInst(Inst, Out, STI);
858 emitInst(ModInst, Out, STI);
866void AArch64MCLFIRewriter::rewriteVASysOp(
const MCInst &Inst,
MCStreamer &Out,
872 emitInst(MCInstBuilder(AArch64::SYSxt)
883void AArch64MCLFIRewriter::doRewriteInst(
const MCInst &Inst,
MCStreamer &Out,
885 if (Inst.
getOpcode() == AArch64::TLSDESCCALL) {
891 if (MCRegister
Reg = mayModifyReserved(Inst)) {
892 error(Inst, Twine(
"illegal modification of reserved LFI register ") +
899 return rewriteSyscall(Inst, Out, STI);
902 return rewriteTPRead(Inst, Out, STI);
905 return rewriteTPWrite(Inst, Out, STI);
908 error(Inst,
"illegal access to privileged thread pointer register");
913 return rewriteVASysOp(Inst, Out, STI);
926 return emitInst(MCInstBuilder(AArch64::HINT)
932 error(Inst,
"exception returns are not supported by LFI");
940 DeferredLRGuard =
false;
941 return rewriteAuthenticatedReturn(Inst, Out, STI);
948 emitAddMask(AArch64::LR, AArch64::LR, Out, STI);
949 DeferredLRGuard =
false;
954 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BR, Out, STI);
956 return rewriteAuthenticatedBranchOrCall(Inst, AArch64::BLR, Out, STI);
961 return rewriteReturn(Inst, Out, STI);
964 return rewriteIndirectBranch(Inst, Out, STI);
968 if (mayModifySP(Inst))
969 return rewriteSPModification(Inst, Out, STI);
974 return rewriteLRModification(Inst, Out, STI);
979 return rewriteLoadStore(Inst, Out, STI);
981 emitInst(Inst, Out, STI);
988 AArch64::INSTRUCTION_LIST_END)
991 AArch64::INSTRUCTION_LIST_END)
993 bool IsPre, IsNoOffset;
995 AArch64::INSTRUCTION_LIST_END)
1004 ActiveGuardReg = std::nullopt;
1016 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 bool isAuthenticatedBranch(unsigned Opcode)
static constexpr MCRegister LFICtxReg
static bool isPrivilegedTP(int64_t Reg)
static bool isBTIC(const MCInst &Inst)
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.
uint64_t IntrinsicInst * II
AArch64MCLFIRewriter(MCContext &Ctx, std::unique_ptr< MCRegisterInfo > &&RI, std::unique_ptr< MCInstrInfo > &&II)
void onLabel(const MCSymbol *Symbol, MCStreamer &Out) override
bool rewriteInst(const MCInst &Inst, MCStreamer &Out, const MCSubtargetInfo &STI) override
void finish(MCStreamer &Out) override
Context object for machine code objects.
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
MCLFIRewriter(MCContext &Ctx, std::unique_ptr< MCRegisterInfo > &&RI, std::unique_ptr< MCInstrInfo > &&II)
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 ==...
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
static unsigned getBTIHintNum(bool CallTarget, bool JumpTarget)
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)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.