Skip to content

Commit 27ba202

Browse files
committed
Optimize stack usage
1 parent aa4803f commit 27ba202

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

llvm/lib/Target/SBF/SBFFrameLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void SBFFrameLowering::emitPrologue(MachineFunction &MF,
3737

3838
if (Subtarget.isDynamicFramesV1())
3939
NumBytes = -NumBytes;
40-
else if (NumBytes <= FrameSize)
40+
else if (NumBytes <= FrameSize && !Subtarget.getOptimizeStackSpace())
4141
// In V3, we don't bump if the number of bytes is less than the default
4242
// frame size.
4343
return;

llvm/lib/Target/SBF/SBFRegisterInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ int SBFRegisterInfo::resolveInternalFrameIndex(
179179
if (SubTarget.isDynamicFramesV1())
180180
return Offset + static_cast<int>(StackSize);
181181

182+
if (SubTarget.getOptimizeStackSpace())
183+
return -(Offset + static_cast<int>(StackSize));
184+
182185
return -(Offset + std::max(static_cast<int>(StackSize), static_cast<int>(FrameLength)));
183186
}
184187

llvm/lib/Target/SBF/SBFSubtarget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class SBFSubtarget : public SBFGenSubtargetInfo {
9595
// Whether we are dealing with dynamic stack frames in SBPFv3
9696
bool HasDynamicFramesV3;
9797

98+
// Whether we should bump down the frame pointer in SBPFv3
99+
bool OptimizeStackSpace;
100+
98101
std::unique_ptr<CallLowering> CallLoweringInfo;
99102
std::unique_ptr<InstructionSelector> InstSelector;
100103
std::unique_ptr<LegalizerInfo> Legalizer;
@@ -130,6 +133,7 @@ class SBFSubtarget : public SBFGenSubtargetInfo {
130133
bool getNewMemEncoding() const { return NewMemEncoding; }
131134
bool getHasStaticSyscalls() const { return HasStaticSyscalls; }
132135
bool getHasJmp32() const { return HasJmp32; }
136+
bool getOptimizeStackSpace() const { return OptimizeStackSpace; }
133137
const SBFInstrInfo *getInstrInfo() const override { return &InstrInfo; }
134138
const SBFFrameLowering *getFrameLowering() const override {
135139
return &FrameLowering;

llvm/lib/Target/SBF/SBFTargetFeatures.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def FeatureJmp32 : SubtargetFeature<"jmp-32", "HasJmp32", "true",
5858
def FeatureDynamicFramesV3 : SubtargetFeature<"dynamic-frames-v3", "HasDynamicFramesV3", "true",
5959
"Enable dynamic frames in SBPFv3">;
6060

61+
def OptimizeStackSpace : SubtargetFeature<"optimize-stack-space", "OptimizeStackSpace", "true",
62+
"Optimize stack space usage in SBPFv3">;
63+
6164
class Proc<string Name, list<SubtargetFeature> Features>
6265
: Processor<Name, NoItineraries, Features>;
6366

llvm/test/CodeGen/SBF/dynamic_stack_frame_add_and_sub.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; RUN: llc < %s -march=sbf -mattr=+dynamic-frames | FileCheck %s
22
; RUN: llc -march=sbf -mattr=+dynamic-frames-v3,+alu32 < %s | FileCheck --check-prefix=CHECK-V3 %s
3+
; RUN: llc -march=sbf -mattr=+dynamic-frames-v3,+optimize-stack-space < %s | FileCheck --check-prefix=CHECK-OPT %s
34
;
45
; Source:
56
; int test_func(int * vec, int idx) {
@@ -15,6 +16,7 @@ define i32 @test_func(ptr noundef %vec, i32 noundef %idx) #0 {
1516
; CHECK-LABEL: test_func:
1617
; CHECK: add64 r10, -128
1718
; CHECK-V3-NOT: add64 r10, 128
19+
; CHECK-OPT: add64 r10, -3968
1820
entry:
1921
%vec.addr = alloca ptr, align 8
2022
%idx.addr = alloca i512, align 4
@@ -36,6 +38,7 @@ declare i64 @read_ptr(ptr %a);
3638
define i64 @test_func_4096(i64 %idx) {
3739
; CHECK-LABEL: test_func_4096
3840
; CHECK-V3-NOT: add64 r10, 4096
41+
; CHECK-OPT-NOT: add64 r10, 4096
3942
entry:
4043
%large_var = alloca [4096 x i8], align 8
4144
%val = call i64 @read_ptr(ptr %large_var)
@@ -45,6 +48,7 @@ entry:
4548
define i64 @test_func_4128(i64 %idx) {
4649
; CHECK-LABEL: test_func_4128
4750
; CHECK-V3: add64 r10, 64
51+
; CHECK-OPT: add64 r10, 64
4852
; The stack is aligned at 64, so we bump 64 to have a stack size of 4096+64=4160,
4953
; so we can fit the 4128 bytes of the array.
5054
entry:

0 commit comments

Comments
 (0)