Skip to content

Commit 4a935cc

Browse files
committed
[SOL] Use ADD for subtracting stack pointer (anza-xyz#70)
This PR replaces sub r11, imm by add r11, -imm and is the equivalent of solana-labs/rbpf#488 for the SBF target in LLVM. This is the first task in solana-labs/solana#34250
1 parent cd662f6 commit 4a935cc

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

llvm/lib/Target/SBF/SBFFrameLowering.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@
1616
#include "llvm/CodeGen/MachineFrameInfo.h"
1717
#include "llvm/CodeGen/MachineFunction.h"
1818
#include "llvm/CodeGen/MachineInstrBuilder.h"
19-
#include "llvm/CodeGen/MachineRegisterInfo.h"
2019

2120
using namespace llvm;
2221

2322
namespace {
2423

2524
void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB,
26-
MachineBasicBlock::iterator &MBBI,
27-
unsigned int Opcode) {
25+
MachineBasicBlock::iterator &MBBI, bool IsSubtract) {
2826
MachineFrameInfo &MFI = MF.getFrameInfo();
2927
int NumBytes = (int)MFI.getStackSize();
3028
if (NumBytes) {
3129
DebugLoc Dl;
3230
const SBFInstrInfo &TII =
3331
*static_cast<const SBFInstrInfo *>(MF.getSubtarget().getInstrInfo());
34-
BuildMI(MBB, MBBI, Dl, TII.get(Opcode), SBF::R11)
32+
BuildMI(MBB, MBBI, Dl, TII.get(SBF::ADD_ri), SBF::R11)
3533
.addReg(SBF::R11)
36-
.addImm(NumBytes);
34+
.addImm(IsSubtract ? -NumBytes : NumBytes);
3735
}
3836
}
3937

@@ -47,7 +45,7 @@ void SBFFrameLowering::emitPrologue(MachineFunction &MF,
4745
return;
4846
}
4947
MachineBasicBlock::iterator MBBI = MBB.begin();
50-
adjustStackPointer(MF, MBB, MBBI, SBF::SUB_ri);
48+
adjustStackPointer(MF, MBB, MBBI, true);
5149
}
5250

5351
void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
@@ -56,7 +54,7 @@ void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
5654
return;
5755
}
5856
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
59-
adjustStackPointer(MF, MBB, MBBI, SBF::ADD_ri);
57+
adjustStackPointer(MF, MBB, MBBI, false);
6058
}
6159

6260
void SBFFrameLowering::determineCalleeSaves(MachineFunction &MF,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: llc < %s -march=sbf --mattr=+dynamic-frames | FileCheck %s
2+
;
3+
; Source:
4+
; int test_func(int * vec, int idx) {
5+
; vec[idx] = idx-1;
6+
; return idx;
7+
; }
8+
; Compilation flag:
9+
; clang -S -emit-llvm test.c
10+
11+
12+
; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
13+
define i32 @test_func(ptr noundef %vec, i32 noundef %idx) #0 {
14+
; CHECK-LABEL: test_func:
15+
; CHECK: add64 r11, -16
16+
; CHECK: add64 r11, 16
17+
entry:
18+
%vec.addr = alloca ptr, align 8
19+
%idx.addr = alloca i32, align 4
20+
store ptr %vec, ptr %vec.addr, align 8
21+
store i32 %idx, ptr %idx.addr, align 4
22+
%0 = load i32, ptr %idx.addr, align 4
23+
%sub = sub nsw i32 %0, 1
24+
%1 = load ptr, ptr %vec.addr, align 8
25+
%2 = load i32, ptr %idx.addr, align 4
26+
%idxprom = sext i32 %2 to i64
27+
%arrayidx = getelementptr inbounds i32, ptr %1, i64 %idxprom
28+
store i32 %sub, ptr %arrayidx, align 4
29+
%3 = load i32, ptr %idx.addr, align 4
30+
ret i32 %3
31+
}

0 commit comments

Comments
 (0)