Skip to content

Commit e6549b8

Browse files
authored
[RISCV][ISel] Allow emitting addiw with u32simm12 rhs (#111116)
In InstCombine, we shrink the constant by setting unused bits to zero (e.g. `((X + -2) & 4294967295) -> ((X + 4294967294) & 4294967295)`). However, this canonicalization blocks emitting `addiw` and creates redundant li for simm32 rhs: ``` ; bin/llc -mtriple=riscv64 -mattr=+zba test.ll -o - define i64 @add_u32simm32_zextw(i64 %x) nounwind { entry: %add = add i64 %x, 4294967294 %and = and i64 %add, 4294967295 ret i64 %and } ``` ``` add_u32simm32_zextw: # @add_u32simm32_zextw # %bb.0: # %entry li a1, -2 add a0, a0, a1 zext.w a0, a0 ret ``` This patch addresses the issue by matching u32simm12 rhs.
1 parent b5f6689 commit e6549b8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ def : PatGprGpr<shiftopw<riscv_sraw>, SRAW>;
18961896
// Select W instructions if only the lower 32 bits of the result are used.
18971897
def : PatGprGpr<binop_allwusers<add>, ADDW>;
18981898
def : PatGprSimm12<binop_allwusers<add>, ADDIW>;
1899+
def : PatGprImm<binop_allwusers<add>, ADDIW, u32simm12>;
18991900
def : PatGprGpr<binop_allwusers<sub>, SUBW>;
19001901
def : PatGprImm<binop_allwusers<shl>, SLLIW, uimm5>;
19011902

llvm/test/CodeGen/RISCV/rv64zba.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,3 +3217,25 @@ entry:
32173217
%z = and i64 %y, -8192
32183218
ret i64 %z
32193219
}
3220+
3221+
define i64 @add_u32simm32_zextw(i64 %x) nounwind {
3222+
; RV64I-LABEL: add_u32simm32_zextw:
3223+
; RV64I: # %bb.0: # %entry
3224+
; RV64I-NEXT: li a1, 1
3225+
; RV64I-NEXT: slli a1, a1, 32
3226+
; RV64I-NEXT: addi a1, a1, -2
3227+
; RV64I-NEXT: add a0, a0, a1
3228+
; RV64I-NEXT: addi a1, a1, 1
3229+
; RV64I-NEXT: and a0, a0, a1
3230+
; RV64I-NEXT: ret
3231+
;
3232+
; RV64ZBA-LABEL: add_u32simm32_zextw:
3233+
; RV64ZBA: # %bb.0: # %entry
3234+
; RV64ZBA-NEXT: addi a0, a0, -2
3235+
; RV64ZBA-NEXT: zext.w a0, a0
3236+
; RV64ZBA-NEXT: ret
3237+
entry:
3238+
%add = add i64 %x, 4294967294
3239+
%and = and i64 %add, 4294967295
3240+
ret i64 %and
3241+
}

0 commit comments

Comments
 (0)