Skip to content

Commit 09bc6ab

Browse files
authored
[MachineFrameInfo] Refactoring around computeMaxcallFrameSize() (NFC) (llvm#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,

0 commit comments

Comments
 (0)