From 7e50b0c4523102a7cc428f46012e751e7023792e Mon Sep 17 00:00:00 2001 From: Quentin Colombet Date: Sun, 20 Oct 2024 19:47:24 +0200 Subject: [PATCH] [H2BLB][InstrInfo] Implement hooks for spills This commit implements the two hooks used to spill and reload values. Although this will be used by the register allocator when spilling value, they are also used by the target-independent code in the prologue/epilogue inserter to save and restore the callee saved registers. For the frame lowering, the CSRs can also be handled with the following two hooks: - TargetFrameLowering::spillCalleeSavedRegisters - TargetFrameLowering::restoreCalleeSavedRegisters For our target we do not use them because the regular spill/reload sequence is good enough. --- llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp | 45 ++++++++++++++++++++++ llvm/lib/Target/H2BLB/H2BLBInstrInfo.h | 12 ++++++ llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll | 46 +++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp b/llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp index ca571c7ed8f1..358e1a677fb2 100644 --- a/llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp +++ b/llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp @@ -15,6 +15,7 @@ #include "H2BLBRegisterInfo.h" #include "llvm/ADT/SmallVector.h" #include "llvm/CodeGen/MachineBasicBlock.h" +#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/IR/DebugLoc.h" #include "llvm/Support/ErrorHandling.h" @@ -29,6 +30,50 @@ using namespace llvm; H2BLBInstrInfo::H2BLBInstrInfo() : H2BLBGenInstrInfo(H2BLB::ADJCALLSTACKDOWN, H2BLB::ADJCALLSTACKUP) {} +void H2BLBInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, + Register SrcReg, bool isKill, int FI, + const TargetRegisterClass *RC, + const TargetRegisterInfo *TRI, + Register VReg) const { + MachineFunction &MF = *MBB.getParent(); + MachineFrameInfo &MFI = MF.getFrameInfo(); + + MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI); + MachineMemOperand *MMO = + MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore, + MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); + + unsigned Opc = TRI->getSpillSize(*RC) == 2 ? H2BLB::STRSP16 : H2BLB::STRSP32; + MFI.setStackID(FI, TargetStackID::Default); + BuildMI(MBB, MBBI, DebugLoc(), get(Opc)) + .addReg(SrcReg, getKillRegState(isKill)) + .addFrameIndex(FI) + .addImm(0) + .addMemOperand(MMO); +} + +void H2BLBInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, + Register DestReg, int FI, + const TargetRegisterClass *RC, + const TargetRegisterInfo *TRI, + Register VReg) const { + MachineFunction &MF = *MBB.getParent(); + MachineFrameInfo &MFI = MF.getFrameInfo(); + MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI); + MachineMemOperand *MMO = + MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad, + MFI.getObjectSize(FI), MFI.getObjectAlign(FI)); + + unsigned Opc = TRI->getSpillSize(*RC) == 2 ? H2BLB::LDRSP16 : H2BLB::LDRSP32; + MFI.setStackID(FI, TargetStackID::Default); + BuildMI(MBB, MBBI, DebugLoc(), get(Opc), DestReg) + .addFrameIndex(FI) + .addImm(0) + .addMemOperand(MMO); +} + void H2BLBInstrInfo::copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, MCRegister DestReg, diff --git a/llvm/lib/Target/H2BLB/H2BLBInstrInfo.h b/llvm/lib/Target/H2BLB/H2BLBInstrInfo.h index 79a8cecec7fa..abb778d61de0 100644 --- a/llvm/lib/Target/H2BLB/H2BLBInstrInfo.h +++ b/llvm/lib/Target/H2BLB/H2BLBInstrInfo.h @@ -25,6 +25,18 @@ class H2BLBInstrInfo : public H2BLBGenInstrInfo { public: H2BLBInstrInfo(); + void storeRegToStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, Register SrcReg, + bool isKill, int FI, const TargetRegisterClass *RC, + const TargetRegisterInfo *TRI, + Register VReg) const override; + + void loadRegFromStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, Register DestReg, + int FI, const TargetRegisterClass *RC, + const TargetRegisterInfo *TRI, + Register VReg) const override; + /// Callback to materialize a register-to-regiter copy before \p MI in /// \p MBB. The copy to materialize is DestReg = COPY SrcReg. The opcode /// of the COPY needs to be the actual target-specific opcode. diff --git a/llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll b/llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll index 491e1c9d6d37..c1c38b605abb 100644 --- a/llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll +++ b/llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll @@ -53,3 +53,49 @@ define <2 x i16> @twoArgsi16(i16 %arg, i16 %arg1) { %res = insertelement <2 x i16> %partial, i16 %arg1, i32 1 ret <2 x i16> %res } + +; Check that we properly set r1 as the input argument for the call. +; The store and load are here to save and restore our link register. +define i16 @callAFctWithOneArg(i16 %arg) { +; CHECK-LABEL: callAFctWithOneArg: +; CHECK: # %bb.0: +; CHECK-NEXT: subsp sp, sp, 8 +; CHECK-NEXT: strsp16 r0, sp, 6 # 2-byte Folded Spill +; CHECK-NEXT: call oneArgi16 +; CHECK-NEXT: ldrsp16 r0, sp, 6 # 2-byte Folded Reload +; CHECK-NEXT: addsp sp, sp, 8 +; CHECK-NEXT: ret + %res = call i16 @oneArgi16(i16 %arg) + ret i16 %res +} + +declare i16 @arg16_32(i16, i32) + +; Check that we set r1 and d1 as the input argument for the call. +define i16 @callAFctWithArg16_32(i16 %arg, i32 %arg1) { +; CHECK-LABEL: callAFctWithArg16_32: +; CHECK: # %bb.0: +; CHECK-NEXT: subsp sp, sp, 8 +; CHECK-NEXT: strsp16 r0, sp, 6 # 2-byte Folded Spill +; CHECK-NEXT: call arg16_32 +; CHECK-NEXT: ldrsp16 r0, sp, 6 # 2-byte Folded Reload +; CHECK-NEXT: addsp sp, sp, 8 +; CHECK-NEXT: ret + %res = call i16 @arg16_32(i16 %arg, i32 %arg1) + ret i16 %res +} + +declare i16 @arg16_16(i16, i16) + +define i16 @callAFctWithTwoI16Arg(i16 %arg, i16 %arg1) { +; CHECK-LABEL: callAFctWithTwoI16Arg: +; CHECK: # %bb.0: +; CHECK-NEXT: subsp sp, sp, 8 +; CHECK-NEXT: strsp16 r0, sp, 6 # 2-byte Folded Spill +; CHECK-NEXT: call arg16_16 +; CHECK-NEXT: ldrsp16 r0, sp, 6 # 2-byte Folded Reload +; CHECK-NEXT: addsp sp, sp, 8 +; CHECK-NEXT: ret + %res = call i16 @arg16_16(i16 %arg, i16 %arg1) + ret i16 %res +}