diff --git a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp index 7c969498c2f982..e4814612e768f9 100644 --- a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp @@ -606,7 +606,7 @@ void AArch64FrameLowering::resetCFIToInitialState( BuildMI(MBB, InsertPt, DL, CFIDesc).addCFIIndex(CFIIndex); // Flip the RA sign state. - if (MFI.shouldSignReturnAddress()) { + if (MFI.shouldSignReturnAddress(MF)) { CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr)); BuildMI(MBB, InsertPt, DL, CFIDesc).addCFIIndex(CFIIndex); } @@ -1355,7 +1355,7 @@ static void emitShadowCallStackEpilogue(const TargetInstrInfo &TII, .addImm(-8) .setMIFlag(MachineInstr::FrameDestroy); - if (MF.getInfo()->needsAsyncDwarfUnwindInfo()) { + if (MF.getInfo()->needsAsyncDwarfUnwindInfo(MF)) { unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, 18)); BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) @@ -1374,7 +1374,7 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF, const TargetInstrInfo *TII = Subtarget.getInstrInfo(); MachineModuleInfo &MMI = MF.getMMI(); AArch64FunctionInfo *AFI = MF.getInfo(); - bool EmitCFI = AFI->needsDwarfUnwindInfo(); + bool EmitCFI = AFI->needsDwarfUnwindInfo(MF); bool HasFP = hasFP(MF); bool NeedsWinCFI = needsWinCFI(MF); bool HasWinCFI = false; @@ -1394,9 +1394,9 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF, const auto &MFnI = *MF.getInfo(); if (needsShadowCallStackPrologueEpilogue(MF)) emitShadowCallStackPrologue(*TII, MF, MBB, MBBI, DL, NeedsWinCFI, - MFnI.needsDwarfUnwindInfo()); + MFnI.needsDwarfUnwindInfo(MF)); - if (MFnI.shouldSignReturnAddress()) { + if (MFnI.shouldSignReturnAddress(MF)) { unsigned PACI; if (MFnI.shouldSignWithBKey()) { BuildMI(MBB, MBBI, DL, TII->get(AArch64::EMITBKEY)) @@ -1868,7 +1868,7 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF, static void InsertReturnAddressAuth(MachineFunction &MF, MachineBasicBlock &MBB, bool NeedsWinCFI, bool *HasWinCFI) { const auto &MFI = *MF.getInfo(); - if (!MFI.shouldSignReturnAddress()) + if (!MFI.shouldSignReturnAddress(MF)) return; const AArch64Subtarget &Subtarget = MF.getSubtarget(); const TargetInstrInfo *TII = Subtarget.getInstrInfo(); @@ -1928,7 +1928,8 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF, const TargetInstrInfo *TII = Subtarget.getInstrInfo(); DebugLoc DL; bool NeedsWinCFI = needsWinCFI(MF); - bool EmitCFI = MF.getInfo()->needsAsyncDwarfUnwindInfo(); + bool EmitCFI = + MF.getInfo()->needsAsyncDwarfUnwindInfo(MF); bool HasWinCFI = false; bool IsFunclet = false; auto WinCFI = make_scope_exit([&]() { assert(HasWinCFI == MF.hasWinCFI()); }); @@ -3744,11 +3745,11 @@ MachineBasicBlock::iterator tryMergeAdjacentSTG(MachineBasicBlock::iterator II, EndOffset = Instr.Offset + Instr.Size; } + const MachineFunction *MF = MBB->getParent(); // Multiple FP/SP updates in a loop cannot be described by CFI instructions. - TSE.emitCode(InsertI, TFI, /*TryMergeSPUpdate = */ - !MBB->getParent() - ->getInfo() - ->needsAsyncDwarfUnwindInfo()); + TSE.emitCode( + InsertI, TFI, /*TryMergeSPUpdate = */ + !MF->getInfo()->needsAsyncDwarfUnwindInfo(*MF)); return InsertI; } diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp index d6f926d6a0190a..c0a8754a9cf8e0 100644 --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -7843,7 +7843,7 @@ static void signOutlinedFunction(MachineFunction &MF, MachineBasicBlock &MBB, .addReg(AArch64::SP, RegState::InternalRead); MI.setMIFlag(MachineInstr::FrameSetup); - if (MF.getInfo()->needsDwarfUnwindInfo()) { + if (MF.getInfo()->needsDwarfUnwindInfo(MF)) { unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr)); BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::CFI_INSTRUCTION)) @@ -7942,7 +7942,7 @@ void AArch64InstrInfo::buildOutlinedFrame( .addImm(-16); It = MBB.insert(It, STRXpre); - if (MF.getInfo()->needsDwarfUnwindInfo()) { + if (MF.getInfo()->needsDwarfUnwindInfo(MF)) { const TargetSubtargetInfo &STI = MF.getSubtarget(); const MCRegisterInfo *MRI = STI.getRegisterInfo(); unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true); diff --git a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp index 469e1448602c07..0f2c353e748b12 100644 --- a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp @@ -66,12 +66,12 @@ static std::pair GetSignReturnAddress(const Function &F) { return {true, false}; } -static bool ShouldSignWithBKey(const Function &F, const MachineFunction &MF) { +static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) { if (!F.hasFnAttribute("sign-return-address-key")) { if (const auto *BKey = mdconst::extract_or_null( F.getParent()->getModuleFlag("sign-return-address-with-bkey"))) return BKey->getZExtValue(); - if (MF.getTarget().getTargetTriple().isOSWindows()) + if (STI.getTargetTriple().isOSWindows()) return true; return false; } @@ -82,15 +82,16 @@ static bool ShouldSignWithBKey(const Function &F, const MachineFunction &MF) { return Key.equals_insensitive("b_key"); } -AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF_) : MF(&MF_) { +AArch64FunctionInfo::AArch64FunctionInfo(MachineFunction &MF) { + const Function &F = MF.getFunction(); + const AArch64Subtarget &STI = MF.getSubtarget(); + // If we already know that the function doesn't have a redzone, set // HasRedZone here. - if (MF->getFunction().hasFnAttribute(Attribute::NoRedZone)) + if (F.hasFnAttribute(Attribute::NoRedZone)) HasRedZone = false; - - const Function &F = MF->getFunction(); std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F); - SignWithBKey = ShouldSignWithBKey(F, *MF); + SignWithBKey = ShouldSignWithBKey(F, STI); // TODO: skip functions that have no instrumented allocas for optimization IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag); @@ -112,9 +113,7 @@ MachineFunctionInfo *AArch64FunctionInfo::clone( BumpPtrAllocator &Allocator, MachineFunction &DestMF, const DenseMap &Src2DstMBB) const { - AArch64FunctionInfo *InfoClone = DestMF.cloneInfo(*this); - InfoClone->MF = &DestMF; - return InfoClone; + return DestMF.cloneInfo(*this); } bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const { @@ -125,27 +124,30 @@ bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const { return SpillsLR; } -bool AArch64FunctionInfo::shouldSignReturnAddress() const { +bool AArch64FunctionInfo::shouldSignReturnAddress( + const MachineFunction &MF) const { return shouldSignReturnAddress(llvm::any_of( - MF->getFrameInfo().getCalleeSavedInfo(), + MF.getFrameInfo().getCalleeSavedInfo(), [](const auto &Info) { return Info.getReg() == AArch64::LR; })); } -bool AArch64FunctionInfo::needsDwarfUnwindInfo() const { +bool AArch64FunctionInfo::needsDwarfUnwindInfo( + const MachineFunction &MF) const { if (!NeedsDwarfUnwindInfo) - NeedsDwarfUnwindInfo = MF->needsFrameMoves() && - !MF->getTarget().getMCAsmInfo()->usesWindowsCFI(); + NeedsDwarfUnwindInfo = MF.needsFrameMoves() && + !MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); return *NeedsDwarfUnwindInfo; } -bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo() const { +bool AArch64FunctionInfo::needsAsyncDwarfUnwindInfo( + const MachineFunction &MF) const { if (!NeedsAsyncDwarfUnwindInfo) { - const Function &F = MF->getFunction(); + const Function &F = MF.getFunction(); // The check got "minsize" is because epilogue unwind info is not emitted // (yet) for homogeneous epilogues, outlined functions, and functions // outlined from. - NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo() && + NeedsAsyncDwarfUnwindInfo = needsDwarfUnwindInfo(MF) && F.getUWTableKind() == UWTableKind::Async && !F.hasMinSize(); } diff --git a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h index 164c9576b9440a..b3dbf04676f940 100644 --- a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h +++ b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h @@ -36,9 +36,6 @@ class MachineInstr; /// AArch64FunctionInfo - This class is derived from MachineFunctionInfo and /// contains private AArch64-specific information for each MachineFunction. class AArch64FunctionInfo final : public MachineFunctionInfo { - /// Backreference to the machine function. - MachineFunction *MF; - /// Number of bytes of arguments this function has on the stack. If the callee /// is expected to restore the argument stack this should be a multiple of 16, /// all usable during a tail call. @@ -428,7 +425,7 @@ class AArch64FunctionInfo final : public MachineFunctionInfo { CalleeSaveBaseToFrameRecordOffset = Offset; } - bool shouldSignReturnAddress() const; + bool shouldSignReturnAddress(const MachineFunction &MF) const; bool shouldSignReturnAddress(bool SpillsLR) const; bool shouldSignWithBKey() const { return SignWithBKey; } @@ -446,8 +443,8 @@ class AArch64FunctionInfo final : public MachineFunctionInfo { } int getSwiftAsyncContextFrameIdx() const { return SwiftAsyncContextFrameIdx; } - bool needsDwarfUnwindInfo() const; - bool needsAsyncDwarfUnwindInfo() const; + bool needsDwarfUnwindInfo(const MachineFunction &MF) const; + bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const; private: // Hold the lists of LOHs.