Skip to content

Commit 09bc6ab

Browse files
authored
[MachineFrameInfo] Refactoring around computeMaxcallFrameSize() (NFC) (#78001)
- Use computeMaxCallFrameSize() in PEI::calculateCallFrameInfo() instead of duplicating the code. - Set AdjustsStack in FinalizeISel instead of in computeMaxCallFrameSize().
1 parent 9253950 commit 09bc6ab

35 files changed

+79
-50
lines changed

llvm/include/llvm/CodeGen/MachineFrameInfo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,17 @@ class MachineFrameInfo {
638638
bool hasTailCall() const { return HasTailCall; }
639639
void setHasTailCall(bool V = true) { HasTailCall = V; }
640640

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

649653
/// Return the maximum size of a call frame that must be
650654
/// allocated for an outgoing function call. This is only available if

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class TargetInstrInfo : public MCInstrInfo {
204204
/// if they exist (-1 otherwise). Some targets use pseudo instructions in
205205
/// order to abstract away the difference between operating with a frame
206206
/// pointer and operating without, through the use of these two instructions.
207+
/// A FrameSetup MI in MF implies MFI::AdjustsStack.
207208
///
208209
unsigned getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
209210
unsigned getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }

llvm/lib/CodeGen/FinalizeISel.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "llvm/CodeGen/MachineFrameInfo.h"
1718
#include "llvm/CodeGen/MachineFunction.h"
1819
#include "llvm/CodeGen/MachineFunctionPass.h"
20+
#include "llvm/CodeGen/TargetInstrInfo.h"
1921
#include "llvm/CodeGen/TargetLowering.h"
2022
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2123
#include "llvm/InitializePasses.h"
@@ -45,6 +47,7 @@ INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
4547

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

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

60+
// Set AdjustsStack to true if the instruction selector emits a stack
61+
// frame setup instruction or a stack aligning inlineasm.
62+
if (MI.getOpcode() == TII->getCallFrameSetupOpcode() ||
63+
MI.isStackAligningInlineAsm())
64+
MF.getFrameInfo().setAdjustsStack(true);
65+
5766
// If MI is a pseudo, expand it.
5867
if (MI.usesCustomInsertionHook()) {
5968
Changed = true;

llvm/lib/CodeGen/MachineFrameInfo.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,23 @@ uint64_t MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
184184
return alignTo(Offset, StackAlign);
185185
}
186186

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

194195
MaxCallFrameSize = 0;
195-
for (const MachineBasicBlock &MBB : MF) {
196-
for (const MachineInstr &MI : MBB) {
196+
for (MachineBasicBlock &MBB : MF) {
197+
for (MachineInstr &MI : MBB) {
197198
unsigned Opcode = MI.getOpcode();
198199
if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
199200
unsigned Size = TII.getFrameSize(MI);
200201
MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
201-
AdjustsStack = true;
202-
} else if (MI.isInlineAsm()) {
203-
// Some inline asm's need a stack frame, as indicated by operand 1.
204-
unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
205-
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
206-
AdjustsStack = true;
202+
if (FrameSDOps != nullptr)
203+
FrameSDOps->push_back(&MI);
207204
}
208205
}
209206
}

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
228228
FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
229229
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
230230

231-
// Calculate the MaxCallFrameSize and AdjustsStack variables for the
232-
// function's frame information. Also eliminates call frame pseudo
233-
// instructions.
231+
// Calculate the MaxCallFrameSize value for the function's frame
232+
// information. Also eliminates call frame pseudo instructions.
234233
calculateCallFrameInfo(MF);
235234

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

353-
/// Calculate the MaxCallFrameSize and AdjustsStack
354-
/// variables for the function's frame information and eliminate call frame
355-
/// pseudo instructions.
352+
/// Calculate the MaxCallFrameSize variable for the function's frame
353+
/// information and eliminate call frame pseudo instructions.
356354
void PEI::calculateCallFrameInfo(MachineFunction &MF) {
357355
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
358356
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
359357
MachineFrameInfo &MFI = MF.getFrameInfo();
360358

361-
unsigned MaxCallFrameSize = 0;
362-
bool AdjustsStack = MFI.adjustsStack();
363-
364359
// Get the function call frame set-up and tear-down instruction opcode
365360
unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
366361
unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
@@ -370,26 +365,15 @@ void PEI::calculateCallFrameInfo(MachineFunction &MF) {
370365
if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
371366
return;
372367

368+
// (Re-)Compute the MaxCallFrameSize.
369+
uint32_t MaxCFSIn =
370+
MFI.isMaxCallFrameSizeComputed() ? MFI.getMaxCallFrameSize() : UINT32_MAX;
373371
std::vector<MachineBasicBlock::iterator> FrameSDOps;
374-
for (MachineBasicBlock &BB : MF)
375-
for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I)
376-
if (TII.isFrameInstr(*I)) {
377-
unsigned Size = TII.getFrameSize(*I);
378-
if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
379-
AdjustsStack = true;
380-
FrameSDOps.push_back(I);
381-
} else if (I->isInlineAsm()) {
382-
// Some inline asm's need a stack frame, as indicated by operand 1.
383-
unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
384-
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
385-
AdjustsStack = true;
386-
}
387-
388-
assert(!MFI.isMaxCallFrameSizeComputed() ||
389-
(MFI.getMaxCallFrameSize() >= MaxCallFrameSize &&
390-
!(AdjustsStack && !MFI.adjustsStack())));
391-
MFI.setAdjustsStack(AdjustsStack);
392-
MFI.setMaxCallFrameSize(MaxCallFrameSize);
372+
MFI.computeMaxCallFrameSize(MF, &FrameSDOps);
373+
assert(MFI.getMaxCallFrameSize() <= MaxCFSIn &&
374+
"Recomputing MaxCFS gave a larger value.");
375+
assert((FrameSDOps.empty() || MF.getFrameInfo().adjustsStack()) &&
376+
"AdjustsStack not set in presence of a frame pseudo instruction.");
393377

394378
if (TFI->canSimplifyCallFramePseudos(MF)) {
395379
// If call frames are not being included as part of the stack frame, and

llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "PPCInstrInfo.h"
2626
#include "PPCTargetMachine.h"
2727
#include "llvm/CodeGen/LiveIntervals.h"
28+
#include "llvm/CodeGen/MachineFrameInfo.h"
2829
#include "llvm/CodeGen/MachineFunctionPass.h"
2930
#include "llvm/CodeGen/MachineInstrBuilder.h"
3031
#include "llvm/InitializePasses.h"
@@ -159,9 +160,11 @@ namespace {
159160
// We don't really need to save data to the stack - the clobbered
160161
// registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
161162
// gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
162-
if (NeedFence)
163+
if (NeedFence) {
164+
MBB.getParent()->getFrameInfo().setAdjustsStack(true);
163165
BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
164166
.addImm(0);
167+
}
165168

166169
if (IsAIX) {
167170
if (IsTLSLDAIXMI) {

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35228,6 +35228,7 @@ X86TargetLowering::EmitLoweredTLSAddr(MachineInstr &MI,
3522835228
MachineFunction &MF = *BB->getParent();
3522935229

3523035230
// Emit CALLSEQ_START right before the instruction.
35231+
BB->getParent()->getFrameInfo().setAdjustsStack(true);
3523135232
unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3523235233
MachineInstrBuilder CallseqStart =
3523335234
BuildMI(MF, MIMD, TII.get(AdjStackDown)).addImm(0).addImm(0).addImm(0);

llvm/test/CodeGen/AArch64/avoid-zero-copy.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
...
2020
---
2121
name: foo
22+
frameInfo:
23+
adjustsStack: true
2224
body: |
2325
bb.0 (%ir-block.0):
2426
; CHECK-LABEL: name: foo

llvm/test/CodeGen/AArch64/stack-probing-no-scratch-reg.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tracksRegLiveness: true
2929
liveins:
3030
- { reg: '$w0', virtual-reg: '' }
3131
frameInfo:
32+
adjustsStack: true
3233
localFrameSize: 150000
3334
stack:
3435
- { id: 0, name: a, type: default, offset: 0, size: 150000, alignment: 8,

llvm/test/CodeGen/AArch64/stack-probing-shrink-wrap.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tracksRegLiveness: true
3131
liveins:
3232
- { reg: '$w0', virtual-reg: '' }
3333
frameInfo:
34+
adjustsStack: true
3435
localFrameSize: 150000
3536
stack:
3637
- { id: 0, name: a, type: default, offset: 0, size: 150000, alignment: 8,

llvm/test/CodeGen/AArch64/wrong-callee-save-size-after-livedebugvariables.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
name: foo
6565
tracksRegLiveness: true
6666
frameInfo:
67+
adjustsStack: true
6768
hasCalls: true
6869
fixedStack: []
6970
stack:

llvm/test/CodeGen/AMDGPU/av_spill_cross_bb_usage.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
---
1515
name: test_av_spill_cross_bb_usage
1616
tracksRegLiveness: true
17+
frameInfo:
18+
adjustsStack: true
1719
stack:
1820
- { id: 0, name: '', type: spill-slot, offset: 0, size: 4, alignment: 4 }
1921
machineFunctionInfo:

llvm/test/CodeGen/Hexagon/livephysregs-regmask-clobber.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
name: f0
1919
tracksRegLiveness: true
20+
frameInfo:
21+
adjustsStack: true
2022
stack:
2123
- { id: 0, offset: 0, size: 128, alignment: 128 }
2224
- { id: 1, offset: 128, size: 128, alignment: 128 }

llvm/test/CodeGen/MIR/AMDGPU/stack-id-assert.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ liveins:
2929
- { reg: '$vgpr0', virtual-reg: '' }
3030
- { reg: '$vgpr1', virtual-reg: '' }
3131
- { reg: '$vgpr2', virtual-reg: '' }
32+
frameInfo:
33+
adjustsStack: true
3234
stack:
3335
- { id: 0, name: '', type: spill-slot, offset: 0, size: 8, alignment: 4,
3436
stack-id: sgpr-spill, callee-saved-register: '', callee-saved-restored: true,

llvm/test/CodeGen/Mips/avoid-zero-copy.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
...
2020
---
2121
name: a
22+
frameInfo:
23+
adjustsStack: true
2224
body: |
2325
bb.0 (%ir-block.0):
2426
liveins: $a0_64, $t9_64, $ra_64, $fp_64, $gp_64

llvm/test/CodeGen/Mips/msa/emergency-spill.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ frameInfo:
9090
stackSize: 0
9191
offsetAdjustment: 0
9292
maxAlignment: 16
93-
adjustsStack: false
93+
adjustsStack: true
9494
hasCalls: true
9595
stackProtector: ''
9696
maxCallFrameSize: 4294967295

llvm/test/CodeGen/RISCV/live-sp.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ frameInfo:
4444
stackSize: 0
4545
offsetAdjustment: 0
4646
maxAlignment: 4
47-
adjustsStack: false
47+
adjustsStack: true
4848
hasCalls: true
4949
stackProtector: ''
5050
maxCallFrameSize: 4294967295

llvm/test/CodeGen/RISCV/rvv/addi-rvv-stack-object.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ frameInfo:
2222
stackSize: 0
2323
offsetAdjustment: 0
2424
maxAlignment: 16
25-
adjustsStack: false
25+
adjustsStack: true
2626
hasCalls: true
2727
stackProtector: ''
2828
functionContext: ''

llvm/test/CodeGen/RISCV/rvv/rvv-stack-align.mir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ frameInfo:
159159
stackSize: 0
160160
offsetAdjustment: 0
161161
maxAlignment: 8
162-
adjustsStack: false
162+
adjustsStack: true
163163
hasCalls: true
164164
stackProtector: ''
165165
maxCallFrameSize: 4294967295
@@ -204,7 +204,7 @@ frameInfo:
204204
stackSize: 0
205205
offsetAdjustment: 0
206206
maxAlignment: 16
207-
adjustsStack: false
207+
adjustsStack: true
208208
hasCalls: true
209209
stackProtector: ''
210210
maxCallFrameSize: 4294967295
@@ -249,7 +249,7 @@ frameInfo:
249249
stackSize: 0
250250
offsetAdjustment: 0
251251
maxAlignment: 32
252-
adjustsStack: false
252+
adjustsStack: true
253253
hasCalls: true
254254
stackProtector: ''
255255
maxCallFrameSize: 4294967295

llvm/test/CodeGen/RISCV/rvv/wrong-stack-offset-for-rvv-object.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ frameInfo:
8383
stackSize: 0
8484
offsetAdjustment: 0
8585
maxAlignment: 8
86-
adjustsStack: false
86+
adjustsStack: true
8787
hasCalls: true
8888
stackProtector: ''
8989
maxCallFrameSize: 4294967295

llvm/test/CodeGen/RISCV/stack-inst-compress.mir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ alignment: 2
3232
tracksRegLiveness: true
3333
frameInfo:
3434
maxAlignment: 4
35+
adjustsStack: true
3536
hasCalls: true
3637
localFrameSize: 2048
3738
stack:
@@ -117,6 +118,7 @@ alignment: 2
117118
tracksRegLiveness: true
118119
frameInfo:
119120
maxAlignment: 4
121+
adjustsStack: true
120122
hasCalls: true
121123
localFrameSize: 4096
122124
stack:
@@ -210,6 +212,7 @@ alignment: 2
210212
tracksRegLiveness: true
211213
frameInfo:
212214
maxAlignment: 4
215+
adjustsStack: true
213216
hasCalls: true
214217
localFrameSize: 8192
215218
stack:

llvm/test/CodeGen/SystemZ/cond-move-04.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ registers:
5353
- { id: 10, class: gr64bit }
5454
- { id: 11, class: gr32bit }
5555
frameInfo:
56+
adjustsStack: true
5657
hasCalls: true
5758
body: |
5859
bb.0 (%ir-block.1):

llvm/test/CodeGen/SystemZ/cond-move-08.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ registers:
116116
- { id: 27, class: grx32bit }
117117
- { id: 28, class: addr64bit }
118118
frameInfo:
119+
adjustsStack: true
119120
hasCalls: true
120121
body: |
121122
bb.0.bb5:

llvm/test/CodeGen/SystemZ/cond-move-regalloc-hints-02.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ registers:
3030
- { id: 11, class: gr32bit }
3131
frameInfo:
3232
maxAlignment: 1
33+
adjustsStack: true
3334
hasCalls: true
3435
machineFunctionInfo: {}
3536
body: |

llvm/test/CodeGen/SystemZ/cond-move-regalloc-hints.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ liveins:
192192
- { reg: '$r2d', virtual-reg: '%31' }
193193
- { reg: '$r3d', virtual-reg: '%32' }
194194
frameInfo:
195+
adjustsStack: true
195196
hasCalls: true
196197
body: |
197198
bb.0.bb:

llvm/test/CodeGen/SystemZ/frame-28.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ body: |
162162
---
163163
name: fun4
164164
tracksRegLiveness: true
165+
frameInfo:
166+
adjustsStack: true
165167
stack:
166168
- { id: 0, size: 5000 }
167169
- { id: 1, size: 2500 }

llvm/test/CodeGen/X86/fast-regalloc-live-out-debug-values.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
name: foo
120120
tracksRegLiveness: true
121121
frameInfo:
122+
adjustsStack: true
122123
hasCalls: true
123124
stack:
124125
- { id: 0, name: a.addr, size: 4, alignment: 4, debug-info-variable: '!11',

0 commit comments

Comments
 (0)