Skip to content

[MachineFrameInfo] Refactoring with computeMaxcallFrameSize() (NFC) #78001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/include/llvm/CodeGen/MachineFrameInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,17 @@ class MachineFrameInfo {
bool hasTailCall() const { return HasTailCall; }
void setHasTailCall(bool V = true) { HasTailCall = V; }

/// Computes the maximum size of a callframe and the AdjustsStack property.
/// Computes the maximum size of a callframe.
/// This only works for targets defining
/// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
/// and getFrameSize().
/// This is usually computed by the prologue epilogue inserter but some
/// targets may call this to compute it earlier.
void computeMaxCallFrameSize(const MachineFunction &MF);
/// If FrameSDOps is passed, the frame instructions in the MF will be
/// inserted into it.
void computeMaxCallFrameSize(
MachineFunction &MF,
std::vector<MachineBasicBlock::iterator> *FrameSDOps = nullptr);

/// Return the maximum size of a call frame that must be
/// allocated for an outgoing function call. This is only available if
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class TargetInstrInfo : public MCInstrInfo {
/// if they exist (-1 otherwise). Some targets use pseudo instructions in
/// order to abstract away the difference between operating with a frame
/// pointer and operating without, through the use of these two instructions.
/// A FrameSetup MI in MF implies MFI::AdjustsStack.
///
unsigned getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
unsigned getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/CodeGen/FinalizeISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/InitializePasses.h"
Expand Down Expand Up @@ -45,6 +47,7 @@ INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,

bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
bool Changed = false;
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();

// Iterate through each instruction in the function, looking for pseudos.
Expand All @@ -54,6 +57,12 @@ bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
MBBI != MBBE; ) {
MachineInstr &MI = *MBBI++;

// Set AdjustsStack to true if the instruction selector emits a stack
// frame setup instruction or a stack aligning inlineasm.
if (MI.getOpcode() == TII->getCallFrameSetupOpcode() ||
MI.isStackAligningInlineAsm())
MF.getFrameInfo().setAdjustsStack(true);

// If MI is a pseudo, expand it.
if (MI.usesCustomInsertionHook()) {
Changed = true;
Expand Down
15 changes: 6 additions & 9 deletions llvm/lib/CodeGen/MachineFrameInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,23 @@ uint64_t MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
return alignTo(Offset, StackAlign);
}

void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
void MachineFrameInfo::computeMaxCallFrameSize(
MachineFunction &MF, std::vector<MachineBasicBlock::iterator> *FrameSDOps) {
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
"Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");

MaxCallFrameSize = 0;
for (const MachineBasicBlock &MBB : MF) {
for (const MachineInstr &MI : MBB) {
for (MachineBasicBlock &MBB : MF) {
for (MachineInstr &MI : MBB) {
unsigned Opcode = MI.getOpcode();
if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
unsigned Size = TII.getFrameSize(MI);
MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
AdjustsStack = true;
} else if (MI.isInlineAsm()) {
// Some inline asm's need a stack frame, as indicated by operand 1.
unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
AdjustsStack = true;
if (FrameSDOps != nullptr)
FrameSDOps->push_back(&MI);
}
}
}
Expand Down
40 changes: 12 additions & 28 deletions llvm/lib/CodeGen/PrologEpilogInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();

// Calculate the MaxCallFrameSize and AdjustsStack variables for the
// function's frame information. Also eliminates call frame pseudo
// instructions.
// Calculate the MaxCallFrameSize value for the function's frame
// information. Also eliminates call frame pseudo instructions.
calculateCallFrameInfo(MF);

// Determine placement of CSR spill/restore code and prolog/epilog code:
Expand Down Expand Up @@ -350,17 +349,13 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
return true;
}

/// Calculate the MaxCallFrameSize and AdjustsStack
/// variables for the function's frame information and eliminate call frame
/// pseudo instructions.
/// Calculate the MaxCallFrameSize variable for the function's frame
/// information and eliminate call frame pseudo instructions.
void PEI::calculateCallFrameInfo(MachineFunction &MF) {
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
MachineFrameInfo &MFI = MF.getFrameInfo();

unsigned MaxCallFrameSize = 0;
bool AdjustsStack = MFI.adjustsStack();

// Get the function call frame set-up and tear-down instruction opcode
unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
Expand All @@ -370,26 +365,15 @@ void PEI::calculateCallFrameInfo(MachineFunction &MF) {
if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
return;

// (Re-)Compute the MaxCallFrameSize.
uint32_t MaxCFSIn =
MFI.isMaxCallFrameSizeComputed() ? MFI.getMaxCallFrameSize() : UINT32_MAX;
std::vector<MachineBasicBlock::iterator> FrameSDOps;
for (MachineBasicBlock &BB : MF)
for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I)
if (TII.isFrameInstr(*I)) {
unsigned Size = TII.getFrameSize(*I);
if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
AdjustsStack = true;
FrameSDOps.push_back(I);
} else if (I->isInlineAsm()) {
// Some inline asm's need a stack frame, as indicated by operand 1.
unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
AdjustsStack = true;
}

assert(!MFI.isMaxCallFrameSizeComputed() ||
(MFI.getMaxCallFrameSize() >= MaxCallFrameSize &&
!(AdjustsStack && !MFI.adjustsStack())));
MFI.setAdjustsStack(AdjustsStack);
MFI.setMaxCallFrameSize(MaxCallFrameSize);
MFI.computeMaxCallFrameSize(MF, &FrameSDOps);
assert(MFI.getMaxCallFrameSize() <= MaxCFSIn &&
"Recomputing MaxCFS gave a larger value.");
assert((FrameSDOps.empty() || MF.getFrameInfo().adjustsStack()) &&
"AdjustsStack not set in presence of a frame pseudo instruction.");
Comment on lines +375 to +376
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably should go in the verifier. It's really unfortunate when MIR asserts and you have to debug just to realize it was supposed to be invalid MIR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do a follow-up patch for that.


if (TFI->canSimplifyCallFramePseudos(MF)) {
// If call frames are not being included as part of the stack frame, and
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "PPCInstrInfo.h"
#include "PPCTargetMachine.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/InitializePasses.h"
Expand Down Expand Up @@ -159,9 +160,11 @@ namespace {
// We don't really need to save data to the stack - the clobbered
// registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
// gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
if (NeedFence)
if (NeedFence) {
MBB.getParent()->getFrameInfo().setAdjustsStack(true);
BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
.addImm(0);
}

if (IsAIX) {
if (IsTLSLDAIXMI) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35228,6 +35228,7 @@ X86TargetLowering::EmitLoweredTLSAddr(MachineInstr &MI,
MachineFunction &MF = *BB->getParent();

// Emit CALLSEQ_START right before the instruction.
BB->getParent()->getFrameInfo().setAdjustsStack(true);
unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
MachineInstrBuilder CallseqStart =
BuildMI(MF, MIMD, TII.get(AdjStackDown)).addImm(0).addImm(0).addImm(0);
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/AArch64/avoid-zero-copy.mir
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
...
---
name: foo
frameInfo:
adjustsStack: true
body: |
bb.0 (%ir-block.0):
; CHECK-LABEL: name: foo
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/stack-probing-no-scratch-reg.mir
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tracksRegLiveness: true
liveins:
- { reg: '$w0', virtual-reg: '' }
frameInfo:
adjustsStack: true
localFrameSize: 150000
stack:
- { id: 0, name: a, type: default, offset: 0, size: 150000, alignment: 8,
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/stack-probing-shrink-wrap.mir
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tracksRegLiveness: true
liveins:
- { reg: '$w0', virtual-reg: '' }
frameInfo:
adjustsStack: true
localFrameSize: 150000
stack:
- { id: 0, name: a, type: default, offset: 0, size: 150000, alignment: 8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
name: foo
tracksRegLiveness: true
frameInfo:
adjustsStack: true
hasCalls: true
fixedStack: []
stack:
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/AMDGPU/av_spill_cross_bb_usage.mir
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
---
name: test_av_spill_cross_bb_usage
tracksRegLiveness: true
frameInfo:
adjustsStack: true
stack:
- { id: 0, name: '', type: spill-slot, offset: 0, size: 4, alignment: 4 }
machineFunctionInfo:
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/Hexagon/livephysregs-regmask-clobber.mir
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

name: f0
tracksRegLiveness: true
frameInfo:
adjustsStack: true
stack:
- { id: 0, offset: 0, size: 128, alignment: 128 }
- { id: 1, offset: 128, size: 128, alignment: 128 }
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/MIR/AMDGPU/stack-id-assert.mir
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ liveins:
- { reg: '$vgpr0', virtual-reg: '' }
- { reg: '$vgpr1', virtual-reg: '' }
- { reg: '$vgpr2', virtual-reg: '' }
frameInfo:
adjustsStack: true
stack:
- { id: 0, name: '', type: spill-slot, offset: 0, size: 8, alignment: 4,
stack-id: sgpr-spill, callee-saved-register: '', callee-saved-restored: true,
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/Mips/avoid-zero-copy.mir
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
...
---
name: a
frameInfo:
adjustsStack: true
body: |
bb.0 (%ir-block.0):
liveins: $a0_64, $t9_64, $ra_64, $fp_64, $gp_64
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/Mips/msa/emergency-spill.mir
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 16
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/RISCV/live-sp.mir
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 4
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/RISCV/rvv/addi-rvv-stack-object.mir
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 16
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
functionContext: ''
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/RISCV/rvv/rvv-stack-align.mir
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 8
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down Expand Up @@ -204,7 +204,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 16
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down Expand Up @@ -249,7 +249,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 32
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ frameInfo:
stackSize: 0
offsetAdjustment: 0
maxAlignment: 8
adjustsStack: false
adjustsStack: true
hasCalls: true
stackProtector: ''
maxCallFrameSize: 4294967295
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/CodeGen/RISCV/stack-inst-compress.mir
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ alignment: 2
tracksRegLiveness: true
frameInfo:
maxAlignment: 4
adjustsStack: true
hasCalls: true
localFrameSize: 2048
stack:
Expand Down Expand Up @@ -117,6 +118,7 @@ alignment: 2
tracksRegLiveness: true
frameInfo:
maxAlignment: 4
adjustsStack: true
hasCalls: true
localFrameSize: 4096
stack:
Expand Down Expand Up @@ -210,6 +212,7 @@ alignment: 2
tracksRegLiveness: true
frameInfo:
maxAlignment: 4
adjustsStack: true
hasCalls: true
localFrameSize: 8192
stack:
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/SystemZ/cond-move-04.mir
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ registers:
- { id: 10, class: gr64bit }
- { id: 11, class: gr32bit }
frameInfo:
adjustsStack: true
hasCalls: true
body: |
bb.0 (%ir-block.1):
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/SystemZ/cond-move-08.mir
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ registers:
- { id: 27, class: grx32bit }
- { id: 28, class: addr64bit }
frameInfo:
adjustsStack: true
hasCalls: true
body: |
bb.0.bb5:
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/SystemZ/cond-move-regalloc-hints-02.mir
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ registers:
- { id: 11, class: gr32bit }
frameInfo:
maxAlignment: 1
adjustsStack: true
hasCalls: true
machineFunctionInfo: {}
body: |
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/SystemZ/cond-move-regalloc-hints.mir
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ liveins:
- { reg: '$r2d', virtual-reg: '%31' }
- { reg: '$r3d', virtual-reg: '%32' }
frameInfo:
adjustsStack: true
hasCalls: true
body: |
bb.0.bb:
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/SystemZ/frame-28.mir
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ body: |
---
name: fun4
tracksRegLiveness: true
frameInfo:
adjustsStack: true
stack:
- { id: 0, size: 5000 }
- { id: 1, size: 2500 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
name: foo
tracksRegLiveness: true
frameInfo:
adjustsStack: true
hasCalls: true
stack:
- { id: 0, name: a.addr, size: 4, alignment: 4, debug-info-variable: '!11',
Expand Down
Loading