Skip to content

Commit

Permalink
AArch64: Stop storing MachineFunction in MachineFunctionInfo
Browse files Browse the repository at this point in the history
The constructor should not depend on the MachineFunction
  • Loading branch information
arsenm committed Dec 16, 2022
1 parent f3e602a commit 588ecc1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
23 changes: 12 additions & 11 deletions llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Expand Up @@ -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);
}
Expand Down Expand Up @@ -1355,7 +1355,7 @@ static void emitShadowCallStackEpilogue(const TargetInstrInfo &TII,
.addImm(-8)
.setMIFlag(MachineInstr::FrameDestroy);

if (MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo()) {
if (MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(MF)) {
unsigned CFIIndex =
MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, 18));
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Expand All @@ -1374,7 +1374,7 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
MachineModuleInfo &MMI = MF.getMMI();
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
bool EmitCFI = AFI->needsDwarfUnwindInfo();
bool EmitCFI = AFI->needsDwarfUnwindInfo(MF);
bool HasFP = hasFP(MF);
bool NeedsWinCFI = needsWinCFI(MF);
bool HasWinCFI = false;
Expand All @@ -1394,9 +1394,9 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
const auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
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))
Expand Down Expand Up @@ -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<AArch64FunctionInfo>();
if (!MFI.shouldSignReturnAddress())
if (!MFI.shouldSignReturnAddress(MF))
return;
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Expand Down Expand Up @@ -1928,7 +1928,8 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
DebugLoc DL;
bool NeedsWinCFI = needsWinCFI(MF);
bool EmitCFI = MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo();
bool EmitCFI =
MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(MF);
bool HasWinCFI = false;
bool IsFunclet = false;
auto WinCFI = make_scope_exit([&]() { assert(HasWinCFI == MF.hasWinCFI()); });
Expand Down Expand Up @@ -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<AArch64FunctionInfo>()
->needsAsyncDwarfUnwindInfo());
TSE.emitCode(
InsertI, TFI, /*TryMergeSPUpdate = */
!MF->getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(*MF));

return InsertI;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Expand Up @@ -7843,7 +7843,7 @@ static void signOutlinedFunction(MachineFunction &MF, MachineBasicBlock &MBB,
.addReg(AArch64::SP, RegState::InternalRead);
MI.setMIFlag(MachineInstr::FrameSetup);

if (MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo()) {
if (MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo(MF)) {
unsigned CFIIndex =
MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::CFI_INSTRUCTION))
Expand Down Expand Up @@ -7942,7 +7942,7 @@ void AArch64InstrInfo::buildOutlinedFrame(
.addImm(-16);
It = MBB.insert(It, STRXpre);

if (MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo()) {
if (MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo(MF)) {
const TargetSubtargetInfo &STI = MF.getSubtarget();
const MCRegisterInfo *MRI = STI.getRegisterInfo();
unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true);
Expand Down
38 changes: 20 additions & 18 deletions llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp
Expand Up @@ -66,12 +66,12 @@ static std::pair<bool, bool> 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<ConstantInt>(
F.getParent()->getModuleFlag("sign-return-address-with-bkey")))
return BKey->getZExtValue();
if (MF.getTarget().getTargetTriple().isOSWindows())
if (STI.getTargetTriple().isOSWindows())
return true;
return false;
}
Expand All @@ -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<AArch64Subtarget>();

// 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);

Expand All @@ -112,9 +113,7 @@ MachineFunctionInfo *AArch64FunctionInfo::clone(
BumpPtrAllocator &Allocator, MachineFunction &DestMF,
const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
const {
AArch64FunctionInfo *InfoClone = DestMF.cloneInfo<AArch64FunctionInfo>(*this);
InfoClone->MF = &DestMF;
return InfoClone;
return DestMF.cloneInfo<AArch64FunctionInfo>(*this);
}

bool AArch64FunctionInfo::shouldSignReturnAddress(bool SpillsLR) const {
Expand All @@ -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();
}
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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; }
Expand All @@ -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.
Expand Down

0 comments on commit 588ecc1

Please sign in to comment.