Skip to content

Commit a271bff

Browse files
committed
Handled the uimm9 offset while FrameIndex folding.
1 parent 742147b commit a271bff

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,8 +2942,8 @@ bool RISCVDAGToDAGISel::SelectAddrRegImm(SDValue Addr, SDValue &Base,
29422942
/// Similar to SelectAddrRegImm, except that the offset is restricted to uimm9.
29432943
bool RISCVDAGToDAGISel::SelectAddrRegImm9(SDValue Addr, SDValue &Base,
29442944
SDValue &Offset) {
2945-
// FIXME: Support FrameIndex. Need to teach eliminateFrameIndex that only
2946-
// a 9-bit immediate can be folded.
2945+
if (SelectAddrFrameIndex(Addr, Base, Offset))
2946+
return true;
29472947

29482948
SDLoc DL(Addr);
29492949
MVT VT = Addr.getSimpleValueType();
@@ -2953,8 +2953,8 @@ bool RISCVDAGToDAGISel::SelectAddrRegImm9(SDValue Addr, SDValue &Base,
29532953
if (isUInt<9>(CVal)) {
29542954
Base = Addr.getOperand(0);
29552955

2956-
// FIXME: Support FrameIndex. Need to teach eliminateFrameIndex that only
2957-
// a 9-bit immediate can be folded.
2956+
if (auto *FIN = dyn_cast<FrameIndexSDNode>(Base))
2957+
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), VT);
29582958
Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
29592959
return true;
29602960
}

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
573573
int64_t Val = Offset.getFixed();
574574
int64_t Lo12 = SignExtend64<12>(Val);
575575
unsigned Opc = MI.getOpcode();
576+
int64_t Imm9Val = SignExtend64<9>(Val);
577+
auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
578+
576579
if (Opc == RISCV::ADDI && !isInt<12>(Val)) {
577580
// We chose to emit the canonical immediate sequence rather than folding
578581
// the offset into the using add under the theory that doing so doesn't
@@ -585,6 +588,11 @@ bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
585588
(Lo12 & 0b11111) != 0) {
586589
// Prefetch instructions require the offset to be 32 byte aligned.
587590
MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
591+
} else if ((Opc == RISCV::PREFETCH_I || Opc == RISCV::PREFETCH_R ||
592+
Opc == RISCV::PREFETCH_W) &&
593+
Subtarget.hasVendorXMIPSCBOP() && !isUInt<9>(Imm9Val)) {
594+
// MIPS Prefetch instructions require the offset to be 9 bits encoded.
595+
MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
588596
} else if ((Opc == RISCV::PseudoRV32ZdinxLD ||
589597
Opc == RISCV::PseudoRV32ZdinxSD) &&
590598
Lo12 >= 2044) {

llvm/test/CodeGen/RISCV/xmips-cbop.ll

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,47 @@ define void @prefetch_inst_read(ptr noundef %ptr) nounwind {
4949
tail call void @llvm.prefetch.p0(ptr nonnull %arrayidx, i32 0, i32 0, i32 0)
5050
ret void
5151
}
52+
53+
define void @prefetch_frameindex_test_neg() nounwind {
54+
; RV32XMIPSPREFETCH-LABEL: prefetch_frameindex_test_neg:
55+
; RV32XMIPSPREFETCH: # %bb.0:
56+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, -512
57+
; RV32XMIPSPREFETCH-NEXT: addi a0, sp, -32
58+
; RV32XMIPSPREFETCH-NEXT: mips.pref 8, 0(a0)
59+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, 512
60+
; RV32XMIPSPREFETCH-NEXT: ret
61+
;
62+
; RV64XMIPSPREFETCH-LABEL: prefetch_frameindex_test_neg:
63+
; RV64XMIPSPREFETCH: # %bb.0:
64+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, -512
65+
; RV64XMIPSPREFETCH-NEXT: addi a0, sp, -32
66+
; RV64XMIPSPREFETCH-NEXT: mips.pref 8, 0(a0)
67+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, 512
68+
; RV64XMIPSPREFETCH-NEXT: ret
69+
%data = alloca [128 x i32], align 4
70+
%base = bitcast ptr %data to ptr
71+
%ptr = getelementptr [128 x i32], ptr %base, i32 0, i32 -8
72+
call void @llvm.prefetch(ptr %ptr, i32 0, i32 0, i32 1)
73+
ret void
74+
}
75+
76+
define void @prefetch_frameindex_test() nounwind {
77+
; RV32XMIPSPREFETCH-LABEL: prefetch_frameindex_test:
78+
; RV32XMIPSPREFETCH: # %bb.0:
79+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, -512
80+
; RV32XMIPSPREFETCH-NEXT: mips.pref 8, 32(sp)
81+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, 512
82+
; RV32XMIPSPREFETCH-NEXT: ret
83+
;
84+
; RV64XMIPSPREFETCH-LABEL: prefetch_frameindex_test:
85+
; RV64XMIPSPREFETCH: # %bb.0:
86+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, -512
87+
; RV64XMIPSPREFETCH-NEXT: mips.pref 8, 32(sp)
88+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, 512
89+
; RV64XMIPSPREFETCH-NEXT: ret
90+
%data = alloca [128 x i32], align 4
91+
%base = bitcast ptr %data to ptr
92+
%ptr = getelementptr [128 x i32], ptr %base, i32 0, i32 8
93+
call void @llvm.prefetch(ptr %ptr, i32 0, i32 0, i32 1)
94+
ret void
95+
}

0 commit comments

Comments
 (0)