From 3bddeb132c03090a4c96a86d1777388c1e2aeb58 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel <38472950+LucasSte@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:15:53 -0300 Subject: [PATCH] [SOL] Use ADD for subtracting stack pointer (#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 --- llvm/lib/Target/SBF/SBFFrameLowering.cpp | 12 +++---- .../SBF/dynamic_stack_frame_add_not_sub.ll | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll diff --git a/llvm/lib/Target/SBF/SBFFrameLowering.cpp b/llvm/lib/Target/SBF/SBFFrameLowering.cpp index f893e7f073e18d..c29af66e9281f0 100644 --- a/llvm/lib/Target/SBF/SBFFrameLowering.cpp +++ b/llvm/lib/Target/SBF/SBFFrameLowering.cpp @@ -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(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); } } @@ -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, @@ -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, diff --git a/llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll b/llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll new file mode 100644 index 00000000000000..307c50d0583004 --- /dev/null +++ b/llvm/test/CodeGen/SBF/dynamic_stack_frame_add_not_sub.ll @@ -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 +} \ No newline at end of file