Skip to content

Commit

Permalink
[H2BLB][InstrInfo] Implement hooks for spills
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
qcolombet committed Oct 20, 2024
1 parent 14ccc32 commit 7e50b0c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
45 changes: 45 additions & 0 deletions llvm/lib/Target/H2BLB/H2BLBInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/Target/H2BLB/H2BLBInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions llvm/test/CodeGen/H2BLB/SDISel/e2e-abi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7e50b0c

Please sign in to comment.