Skip to content

Commit 66b34bc

Browse files
[RISCV] Handled the uimm9 offset while FrameIndex folding. (#149303)
Reverted the #148779 changes and - handled the uimm9 offset in eliminateFrameIndex () - updated the testcase.
1 parent eb9e526 commit 66b34bc

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
576576
int64_t Val = Offset.getFixed();
577577
int64_t Lo12 = SignExtend64<12>(Val);
578578
unsigned Opc = MI.getOpcode();
579+
auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
580+
579581
if (Opc == RISCV::ADDI && !isInt<12>(Val)) {
580582
// We chose to emit the canonical immediate sequence rather than folding
581583
// the offset into the using add under the theory that doing so doesn't
@@ -588,6 +590,9 @@ bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
588590
(Lo12 & 0b11111) != 0) {
589591
// Prefetch instructions require the offset to be 32 byte aligned.
590592
MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
593+
} else if (Opc == RISCV::MIPS_PREFETCH && !isUInt<9>(Val)) {
594+
// MIPS Prefetch instructions require the offset to be 9 bits encoded.
595+
MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
591596
} else if ((Opc == RISCV::PseudoRV32ZdinxLD ||
592597
Opc == RISCV::PseudoRV32ZdinxSD) &&
593598
Lo12 >= 2044) {

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2-
; RUN: llc -mtriple=riscv32 -mattr=+xmipscbop -mattr=+m -verify-machineinstrs < %s \
2+
; RUN: llc -mtriple=riscv32 -mattr=+xmipscbop -verify-machineinstrs < %s \
33
; RUN: | FileCheck %s -check-prefix=RV32XMIPSPREFETCH
4-
; RUN: llc -mtriple=riscv64 -mattr=+xmipscbop -mattr=+m -verify-machineinstrs < %s \
4+
; RUN: llc -mtriple=riscv64 -mattr=+xmipscbop -verify-machineinstrs < %s \
55
; RUN: | FileCheck %s -check-prefix=RV64XMIPSPREFETCH
66

77
define void @prefetch_data_read(ptr noundef %ptr) nounwind {
@@ -49,3 +49,54 @@ 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: lui a0, 1
57+
; RV32XMIPSPREFETCH-NEXT: addi a0, a0, 16
58+
; RV32XMIPSPREFETCH-NEXT: sub sp, sp, a0
59+
; RV32XMIPSPREFETCH-NEXT: addi a0, sp, 524
60+
; RV32XMIPSPREFETCH-NEXT: mips.pref 8, 0(a0)
61+
; RV32XMIPSPREFETCH-NEXT: lui a0, 1
62+
; RV32XMIPSPREFETCH-NEXT: addi a0, a0, 16
63+
; RV32XMIPSPREFETCH-NEXT: add sp, sp, a0
64+
; RV32XMIPSPREFETCH-NEXT: ret
65+
;
66+
; RV64XMIPSPREFETCH-LABEL: prefetch_frameindex_test_neg:
67+
; RV64XMIPSPREFETCH: # %bb.0:
68+
; RV64XMIPSPREFETCH-NEXT: lui a0, 1
69+
; RV64XMIPSPREFETCH-NEXT: addi a0, a0, 16
70+
; RV64XMIPSPREFETCH-NEXT: sub sp, sp, a0
71+
; RV64XMIPSPREFETCH-NEXT: addi a0, sp, 524
72+
; RV64XMIPSPREFETCH-NEXT: mips.pref 8, 0(a0)
73+
; RV64XMIPSPREFETCH-NEXT: lui a0, 1
74+
; RV64XMIPSPREFETCH-NEXT: addi a0, a0, 16
75+
; RV64XMIPSPREFETCH-NEXT: add sp, sp, a0
76+
; RV64XMIPSPREFETCH-NEXT: ret
77+
%data = alloca [1024 x i32], align 4
78+
%ptr = getelementptr [1024 x i32], ptr %data, i32 0, i32 127
79+
call void @llvm.prefetch(ptr %ptr, i32 0, i32 0, i32 1)
80+
ret void
81+
}
82+
83+
define void @prefetch_frameindex_test() nounwind {
84+
; RV32XMIPSPREFETCH-LABEL: prefetch_frameindex_test:
85+
; RV32XMIPSPREFETCH: # %bb.0:
86+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, -512
87+
; RV32XMIPSPREFETCH-NEXT: mips.pref 8, 32(sp)
88+
; RV32XMIPSPREFETCH-NEXT: addi sp, sp, 512
89+
; RV32XMIPSPREFETCH-NEXT: ret
90+
;
91+
; RV64XMIPSPREFETCH-LABEL: prefetch_frameindex_test:
92+
; RV64XMIPSPREFETCH: # %bb.0:
93+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, -512
94+
; RV64XMIPSPREFETCH-NEXT: mips.pref 8, 32(sp)
95+
; RV64XMIPSPREFETCH-NEXT: addi sp, sp, 512
96+
; RV64XMIPSPREFETCH-NEXT: ret
97+
%data = alloca [128 x i32], align 4
98+
%base = bitcast ptr %data to ptr
99+
%ptr = getelementptr [128 x i32], ptr %base, i32 0, i32 8
100+
call void @llvm.prefetch(ptr %ptr, i32 0, i32 0, i32 1)
101+
ret void
102+
}

0 commit comments

Comments
 (0)