Skip to content

Commit c3383d7

Browse files
authored
[RISCV][GlobalIsel] Remove redundant sext.w for ADDIW (llvm#159597)
This is the minimal case generated by clang at `-O0`; I'm not sure if writing the test this way is appropriate.
1 parent 6ec0813 commit c3383d7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ bool RISCVInstructionSelector::hasAllNBitUsers(const MachineInstr &MI,
226226
case RISCV::ADDW:
227227
case RISCV::ADDIW:
228228
case RISCV::SUBW:
229+
case RISCV::FCVT_D_W:
230+
case RISCV::FCVT_S_W:
229231
if (Bits >= 32)
230232
break;
231233
return false;

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ def : Pat<(anyext (i32 GPR:$src)), (COPY GPR:$src)>;
143143
def : Pat<(sext (i32 GPR:$src)), (ADDIW GPR:$src, 0)>;
144144
def : Pat<(i32 (trunc GPR:$src)), (COPY GPR:$src)>;
145145

146+
def : Pat<(sext_inreg (i64 (add GPR:$rs1, simm12:$imm)), i32),
147+
(ADDIW GPR:$rs1, simm12:$imm)>;
148+
146149
// Use sext if the sign bit of the input is 0.
147150
def : Pat<(zext_is_sext (i32 GPR:$src)), (ADDIW GPR:$src, 0)>;
148151
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv64 -global-isel -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s --check-prefix=RV64I
4+
5+
define i32 @addiw_sext_inreg(i32 noundef signext %A) #0 {
6+
; RV64I-LABEL: addiw_sext_inreg:
7+
; RV64I: # %bb.0: # %entry
8+
; RV64I-NEXT: addiw a0, a0, 123
9+
; RV64I-NEXT: ret
10+
entry:
11+
%add = add nsw i32 123, %A
12+
ret i32 %add
13+
}
14+
15+
attributes #0 = { noinline nounwind optnone }

0 commit comments

Comments
 (0)