From 5783903589ef373a76033b99dcc29e067435df00 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Fri, 8 Dec 2023 16:12:59 -0300 Subject: [PATCH 1/3] Use ADD for subtracting stack pointer --- llvm/lib/Target/SBF/SBFFrameLowering.cpp | 11 ++++--- .../SBF/dynamic_stack_frame_add_not_sub.ll | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 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..ca03a64d3ac7d3 100644 --- a/llvm/lib/Target/SBF/SBFFrameLowering.cpp +++ b/llvm/lib/Target/SBF/SBFFrameLowering.cpp @@ -16,7 +16,6 @@ #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineInstrBuilder.h" -#include "llvm/CodeGen/MachineRegisterInfo.h" using namespace llvm; @@ -24,14 +23,16 @@ namespace { void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, - unsigned int Opcode) { + bool Subtract) { MachineFrameInfo &MFI = MF.getFrameInfo(); int NumBytes = (int)MFI.getStackSize(); + if (Subtract) + NumBytes = - NumBytes; 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); } @@ -47,7 +48,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 +57,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 From ae723648d5e44f2a7060392f514dc74b94f3ba64 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Mon, 11 Dec 2023 11:48:57 -0300 Subject: [PATCH 2/3] Adjust variable name --- llvm/lib/Target/SBF/SBFFrameLowering.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Target/SBF/SBFFrameLowering.cpp b/llvm/lib/Target/SBF/SBFFrameLowering.cpp index ca03a64d3ac7d3..83f14eabcfcdc2 100644 --- a/llvm/lib/Target/SBF/SBFFrameLowering.cpp +++ b/llvm/lib/Target/SBF/SBFFrameLowering.cpp @@ -23,18 +23,16 @@ namespace { void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, - bool Subtract) { + bool IsSubtract) { MachineFrameInfo &MFI = MF.getFrameInfo(); int NumBytes = (int)MFI.getStackSize(); - if (Subtract) - NumBytes = - NumBytes; if (NumBytes) { DebugLoc Dl; const SBFInstrInfo &TII = *static_cast(MF.getSubtarget().getInstrInfo()); BuildMI(MBB, MBBI, Dl, TII.get(SBF::ADD_ri), SBF::R11) .addReg(SBF::R11) - .addImm(NumBytes); + .addImm(IsSubtract? -NumBytes : NumBytes); } } From 09676d633968c45a71d85c68c5f967bc6f907ebc Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Mon, 11 Dec 2023 13:54:20 -0300 Subject: [PATCH 3/3] Run clang-format --- llvm/lib/Target/SBF/SBFFrameLowering.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Target/SBF/SBFFrameLowering.cpp b/llvm/lib/Target/SBF/SBFFrameLowering.cpp index 83f14eabcfcdc2..c29af66e9281f0 100644 --- a/llvm/lib/Target/SBF/SBFFrameLowering.cpp +++ b/llvm/lib/Target/SBF/SBFFrameLowering.cpp @@ -22,8 +22,7 @@ using namespace llvm; namespace { void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB, - MachineBasicBlock::iterator &MBBI, - bool IsSubtract) { + MachineBasicBlock::iterator &MBBI, bool IsSubtract) { MachineFrameInfo &MFI = MF.getFrameInfo(); int NumBytes = (int)MFI.getStackSize(); if (NumBytes) { @@ -32,7 +31,7 @@ void adjustStackPointer(MachineFunction &MF, MachineBasicBlock &MBB, *static_cast(MF.getSubtarget().getInstrInfo()); BuildMI(MBB, MBBI, Dl, TII.get(SBF::ADD_ri), SBF::R11) .addReg(SBF::R11) - .addImm(IsSubtract? -NumBytes : NumBytes); + .addImm(IsSubtract ? -NumBytes : NumBytes); } }