Skip to content
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

[SOL] Use ADD for subtracting stack pointer #70

Merged
merged 3 commits into from
Dec 12, 2023
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
12 changes: 5 additions & 7 deletions llvm/lib/Target/SBF/SBFFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"

using namespace llvm;

namespace {

void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator &MBBI,
unsigned int Opcode) {
MachineBasicBlock::iterator &MBBI, bool IsSubtract) {
MachineFrameInfo &MFI = MF.getFrameInfo();
int NumBytes = (int)MFI.getStackSize();
if (NumBytes) {
DebugLoc Dl;
const SBFInstrInfo &TII =
*static_cast<const SBFInstrInfo *>(MF.getSubtarget().getInstrInfo());
BuildMI(MBB, MBBI, Dl, TII.get(Opcode), SBF::R11)
BuildMI(MBB, MBBI, Dl, TII.get(SBF::ADD_ri), SBF::R11)
.addReg(SBF::R11)
.addImm(NumBytes);
.addImm(IsSubtract ? -NumBytes : NumBytes);
}
}

Expand All @@ -47,7 +45,7 @@ void SBFFrameLowering::emitPrologue(MachineFunction &MF,
return;
}
MachineBasicBlock::iterator MBBI = MBB.begin();
adjustStackPointer(MF, MBB, MBBI, SBF::SUB_ri);
adjustStackPointer(MF, MBB, MBBI, true);
}

void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
Expand All @@ -56,7 +54,7 @@ void SBFFrameLowering::emitEpilogue(MachineFunction &MF,
return;
}
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
adjustStackPointer(MF, MBB, MBBI, SBF::ADD_ri);
adjustStackPointer(MF, MBB, MBBI, false);
}

void SBFFrameLowering::determineCalleeSaves(MachineFunction &MF,
Expand Down
31 changes: 31 additions & 0 deletions llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; RUN: llc < %s -march=sbf --mattr=+dynamic-frames | FileCheck %s
;
; Source:
; int test_func(int * vec, int idx) {
; vec[idx] = idx-1;
; return idx;
; }
; Compilation flag:
; clang -S -emit-llvm test.c


; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
define i32 @test_func(ptr noundef %vec, i32 noundef %idx) #0 {
; CHECK-LABEL: test_func:
; CHECK: add64 r11, -16
; CHECK: add64 r11, 16
entry:
%vec.addr = alloca ptr, align 8
%idx.addr = alloca i32, align 4
store ptr %vec, ptr %vec.addr, align 8
store i32 %idx, ptr %idx.addr, align 4
%0 = load i32, ptr %idx.addr, align 4
%sub = sub nsw i32 %0, 1
%1 = load ptr, ptr %vec.addr, align 8
%2 = load i32, ptr %idx.addr, align 4
%idxprom = sext i32 %2 to i64
%arrayidx = getelementptr inbounds i32, ptr %1, i64 %idxprom
store i32 %sub, ptr %arrayidx, align 4
%3 = load i32, ptr %idx.addr, align 4
ret i32 %3
}
Loading