Skip to content

Commit df36576

Browse files
committed
[llvm] Consistently respect naked fn attribute in TargetFrameLowering::hasFP().
Some targets (e.g. PPC and Hexagon) already did this. I think it's best to do this consistently so that frontend authors don't run into inconsistent results when they emit naked functions. For example, in Zig, we had to change our emit code to also set frame-pointer=none to get reliable results across targets.
1 parent 675c748 commit df36576

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+120
-74
lines changed

llvm/include/llvm/CodeGen/TargetFrameLowering.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ class TargetFrameLowering {
286286
/// hasFP - Return true if the specified function should have a dedicated
287287
/// frame pointer register. For most targets this is true only if the function
288288
/// has variable sized allocas or if frame pointer elimination is disabled.
289-
virtual bool hasFP(const MachineFunction &MF) const = 0;
289+
/// For all targets, this is false if the function has the naked attribute
290+
/// since there is no prologue to set up the frame pointer.
291+
bool hasFP(const MachineFunction &MF) const {
292+
return !MF.getFunction().hasFnAttribute(Attribute::Naked) && hasFPImpl(MF);
293+
}
290294

291295
/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
292296
/// not required, we reserve argument space for call sites in the function
@@ -483,6 +487,9 @@ class TargetFrameLowering {
483487
/// targets can emit remarks based on the final frame layout.
484488
virtual void emitRemarks(const MachineFunction &MF,
485489
MachineOptimizationRemarkEmitter *ORE) const {};
490+
491+
protected:
492+
virtual bool hasFPImpl(const MachineFunction &MF) const = 0;
486493
};
487494

488495
} // End llvm namespace

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const {
475475
getSVEStackSize(MF) || LowerQRegCopyThroughMem);
476476
}
477477

478-
/// hasFP - Return true if the specified function should have a dedicated frame
478+
/// hasFPImpl - Return true if the specified function should have a dedicated frame
479479
/// pointer register.
480-
bool AArch64FrameLowering::hasFP(const MachineFunction &MF) const {
480+
bool AArch64FrameLowering::hasFPImpl(const MachineFunction &MF) const {
481481
const MachineFrameInfo &MFI = MF.getFrameInfo();
482482
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
483483

llvm/lib/Target/AArch64/AArch64FrameLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class AArch64FrameLowering : public TargetFrameLowering {
6565
/// Can this function use the red zone for local allocations.
6666
bool canUseRedZone(const MachineFunction &MF) const;
6767

68-
bool hasFP(const MachineFunction &MF) const override;
6968
bool hasReservedCallFrame(const MachineFunction &MF) const override;
7069

7170
bool assignCalleeSavedSpillSlots(MachineFunction &MF,
@@ -125,6 +124,9 @@ class AArch64FrameLowering : public TargetFrameLowering {
125124
orderFrameObjects(const MachineFunction &MF,
126125
SmallVectorImpl<int> &ObjectsToAllocate) const override;
127126

127+
protected:
128+
bool hasFPImpl(const MachineFunction &MF) const override;
129+
128130
private:
129131
/// Returns true if a homogeneous prolog or epilog code can be emitted
130132
/// for the size optimization. If so, HOM_Prolog/HOM_Epilog pseudo

llvm/lib/Target/AMDGPU/R600FrameLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class R600FrameLowering : public AMDGPUFrameLowering {
2727
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
2828
Register &FrameReg) const override;
2929

30-
bool hasFP(const MachineFunction &MF) const override {
30+
protected:
31+
bool hasFPImpl(const MachineFunction &MF) const override {
3132
return false;
3233
}
3334
};

llvm/lib/Target/AMDGPU/SIFrameLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ static bool frameTriviallyRequiresSP(const MachineFrameInfo &MFI) {
18111811
// The FP for kernels is always known 0, so we never really need to setup an
18121812
// explicit register for it. However, DisableFramePointerElim will force us to
18131813
// use a register for it.
1814-
bool SIFrameLowering::hasFP(const MachineFunction &MF) const {
1814+
bool SIFrameLowering::hasFPImpl(const MachineFunction &MF) const {
18151815
const MachineFrameInfo &MFI = MF.getFrameInfo();
18161816

18171817
// For entry & chain functions we can use an immediate offset in most cases,

llvm/lib/Target/AMDGPU/SIFrameLowering.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class SIFrameLowering final : public AMDGPUFrameLowering {
6666
MachineBasicBlock &MBB,
6767
MachineBasicBlock::iterator MI) const override;
6868

69+
protected:
70+
bool hasFPImpl(const MachineFunction &MF) const override;
71+
6972
private:
7073
void emitEntryFunctionFlatScratchInit(MachineFunction &MF,
7174
MachineBasicBlock &MBB,
@@ -82,8 +85,6 @@ class SIFrameLowering final : public AMDGPUFrameLowering {
8285
Register ScratchWaveOffsetReg) const;
8386

8487
public:
85-
bool hasFP(const MachineFunction &MF) const override;
86-
8788
bool requiresStackPointerReference(const MachineFunction &MF) const;
8889
};
8990

llvm/lib/Target/ARC/ARCFrameLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ MachineBasicBlock::iterator ARCFrameLowering::eliminateCallFramePseudoInstr(
487487
return MBB.erase(I);
488488
}
489489

490-
bool ARCFrameLowering::hasFP(const MachineFunction &MF) const {
490+
bool ARCFrameLowering::hasFPImpl(const MachineFunction &MF) const {
491491
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
492492
bool HasFP = MF.getTarget().Options.DisableFramePointerElim(MF) ||
493493
MF.getFrameInfo().hasVarSizedObjects() ||

llvm/lib/Target/ARC/ARCFrameLowering.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class ARCFrameLowering : public TargetFrameLowering {
5454
void processFunctionBeforeFrameFinalized(MachineFunction &MF,
5555
RegScavenger *RS) const override;
5656

57-
bool hasFP(const MachineFunction &MF) const override;
58-
5957
MachineBasicBlock::iterator
6058
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
6159
MachineBasicBlock::iterator I) const override;
@@ -64,6 +62,9 @@ class ARCFrameLowering : public TargetFrameLowering {
6462
llvm::MachineFunction &, const llvm::TargetRegisterInfo *,
6563
std::vector<llvm::CalleeSavedInfo> &) const override;
6664

65+
protected:
66+
bool hasFPImpl(const MachineFunction &MF) const override;
67+
6768
private:
6869
void adjustStackToMatchRecords(MachineBasicBlock &MBB,
6970
MachineBasicBlock::iterator MI,

llvm/lib/Target/ARM/ARMFrameLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ bool ARMFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
196196
return true;
197197
}
198198

199-
/// hasFP - Return true if the specified function should have a dedicated frame
199+
/// hasFPImpl - Return true if the specified function should have a dedicated frame
200200
/// pointer register. This is true if the function has variable sized allocas
201201
/// or if frame pointer elimination is disabled.
202-
bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
202+
bool ARMFrameLowering::hasFPImpl(const MachineFunction &MF) const {
203203
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
204204
const MachineFrameInfo &MFI = MF.getFrameInfo();
205205

llvm/lib/Target/ARM/ARMFrameLowering.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ARMFrameLowering : public TargetFrameLowering {
4545

4646
bool enableCalleeSaveSkip(const MachineFunction &MF) const override;
4747

48-
bool hasFP(const MachineFunction &MF) const override;
4948
bool isFPReserved(const MachineFunction &MF) const;
5049
bool requiresAAPCSFrameRecord(const MachineFunction &MF) const;
5150
bool hasReservedCallFrame(const MachineFunction &MF) const override;
@@ -87,6 +86,9 @@ class ARMFrameLowering : public TargetFrameLowering {
8786
const SpillSlot *
8887
getCalleeSavedSpillSlots(unsigned &NumEntries) const override;
8988

89+
protected:
90+
bool hasFPImpl(const MachineFunction &MF) const override;
91+
9092
private:
9193
void emitPushInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
9294
ArrayRef<CalleeSavedInfo> CSI, unsigned StmOpc,

0 commit comments

Comments
 (0)