Skip to content

Commit 1c00a00

Browse files
authored
[RISCV] Merge ADDI and SIGN_EXTEND_INREG to ADDIW during selectSETCC. (llvm#162614)
That we can merge ADDI with its LHS to ADDIW if its LHS is a SIGN_EXTEND_INREG.
1 parent 3e22438 commit 1c00a00

File tree

7 files changed

+29
-30
lines changed

7 files changed

+29
-30
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,14 +3353,20 @@ bool RISCVDAGToDAGISel::selectSETCC(SDValue N, ISD::CondCode ExpectedCCVal,
33533353
0);
33543354
return true;
33553355
}
3356-
// If the RHS is [-2047,2048], we can use addi with -RHS to produce 0 if the
3357-
// LHS is equal to the RHS and non-zero otherwise.
3356+
// If the RHS is [-2047,2048], we can use addi/addiw with -RHS to produce 0
3357+
// if the LHS is equal to the RHS and non-zero otherwise.
33583358
if (isInt<12>(CVal) || CVal == 2048) {
3359-
Val = SDValue(
3360-
CurDAG->getMachineNode(
3361-
RISCV::ADDI, DL, N->getValueType(0), LHS,
3362-
CurDAG->getSignedTargetConstant(-CVal, DL, N->getValueType(0))),
3363-
0);
3359+
unsigned Opc = RISCV::ADDI;
3360+
if (LHS.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3361+
cast<VTSDNode>(LHS.getOperand(1))->getVT() == MVT::i32) {
3362+
Opc = RISCV::ADDIW;
3363+
LHS = LHS.getOperand(0);
3364+
}
3365+
3366+
Val = SDValue(CurDAG->getMachineNode(Opc, DL, N->getValueType(0), LHS,
3367+
CurDAG->getSignedTargetConstant(
3368+
-CVal, DL, N->getValueType(0))),
3369+
0);
33643370
return true;
33653371
}
33663372
if (isPowerOf2_64(CVal) && Subtarget->hasStdExtZbs()) {

llvm/test/CodeGen/RISCV/and-negpow2-cmp.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ define i64 @test12(i64 %0) #0 {
221221
;
222222
; RV64-LABEL: test12:
223223
; RV64: # %bb.0: # %entry
224-
; RV64-NEXT: addiw a0, a0, -16
225-
; RV64-NEXT: addi a0, a0, 13
224+
; RV64-NEXT: addi a0, a0, -16
225+
; RV64-NEXT: addiw a0, a0, 13
226226
; RV64-NEXT: seqz a0, a0
227227
; RV64-NEXT: ret
228228
entry:

llvm/test/CodeGen/RISCV/i64-icmp.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ define i64 @icmp_sle_constant_neg_2050(i64 %a) nounwind {
708708
define i64 @icmp_eq_zext_inreg_small_constant(i64 %a) nounwind {
709709
; RV64I-LABEL: icmp_eq_zext_inreg_small_constant:
710710
; RV64I: # %bb.0:
711-
; RV64I-NEXT: sext.w a0, a0
712-
; RV64I-NEXT: addi a0, a0, -123
711+
; RV64I-NEXT: addiw a0, a0, -123
713712
; RV64I-NEXT: seqz a0, a0
714713
; RV64I-NEXT: ret
715714
%1 = and i64 %a, 4294967295
@@ -748,8 +747,7 @@ define i64 @icmp_ne_zext_inreg_small_constant(i64 %a) nounwind {
748747
define i64 @icmp_ne_zext_inreg_large_constant(i64 %a) nounwind {
749748
; RV64I-LABEL: icmp_ne_zext_inreg_large_constant:
750749
; RV64I: # %bb.0:
751-
; RV64I-NEXT: sext.w a0, a0
752-
; RV64I-NEXT: addi a0, a0, 2
750+
; RV64I-NEXT: addiw a0, a0, 2
753751
; RV64I-NEXT: snez a0, a0
754752
; RV64I-NEXT: ret
755753
%1 = and i64 %a, 4294967295

llvm/test/CodeGen/RISCV/select-to-and-zext.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ define i32 @from_cmpeq(i32 %xx, i32 %y) {
1515
;
1616
; RV64I-LABEL: from_cmpeq:
1717
; RV64I: # %bb.0:
18-
; RV64I-NEXT: sext.w a0, a0
19-
; RV64I-NEXT: addi a0, a0, -9
18+
; RV64I-NEXT: addiw a0, a0, -9
2019
; RV64I-NEXT: seqz a0, a0
2120
; RV64I-NEXT: and a0, a0, a1
2221
; RV64I-NEXT: ret
@@ -39,8 +38,7 @@ define i32 @from_cmpeq_fail_bad_andmask(i32 %xx, i32 %y) {
3938
;
4039
; RV64I-LABEL: from_cmpeq_fail_bad_andmask:
4140
; RV64I: # %bb.0:
42-
; RV64I-NEXT: sext.w a0, a0
43-
; RV64I-NEXT: addi a0, a0, -9
41+
; RV64I-NEXT: addiw a0, a0, -9
4442
; RV64I-NEXT: snez a0, a0
4543
; RV64I-NEXT: addi a0, a0, -1
4644
; RV64I-NEXT: and a0, a1, a0

llvm/test/CodeGen/RISCV/setcc-logic.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ define i1 @and_icmps_const_not1bit_diff(i32 %x) nounwind {
104104
;
105105
; RV64I-LABEL: and_icmps_const_not1bit_diff:
106106
; RV64I: # %bb.0:
107-
; RV64I-NEXT: sext.w a0, a0
108-
; RV64I-NEXT: addi a1, a0, -44
109-
; RV64I-NEXT: addi a0, a0, -92
107+
; RV64I-NEXT: addiw a1, a0, -44
108+
; RV64I-NEXT: addiw a0, a0, -92
110109
; RV64I-NEXT: snez a1, a1
111110
; RV64I-NEXT: snez a0, a0
112111
; RV64I-NEXT: and a0, a1, a0

llvm/test/CodeGen/RISCV/sext-zext-trunc.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,7 @@ define i32 @sext_of_not_cmp_i32(i32 %x) {
674674
;
675675
; RV64-LABEL: sext_of_not_cmp_i32:
676676
; RV64: # %bb.0:
677-
; RV64-NEXT: sext.w a0, a0
678-
; RV64-NEXT: addi a0, a0, -7
677+
; RV64-NEXT: addiw a0, a0, -7
679678
; RV64-NEXT: seqz a0, a0
680679
; RV64-NEXT: addi a0, a0, -1
681680
; RV64-NEXT: ret
@@ -718,8 +717,7 @@ define i32 @dec_of_zexted_cmp_i32(i32 %x) {
718717
;
719718
; RV64-LABEL: dec_of_zexted_cmp_i32:
720719
; RV64: # %bb.0:
721-
; RV64-NEXT: sext.w a0, a0
722-
; RV64-NEXT: addi a0, a0, -7
720+
; RV64-NEXT: addiw a0, a0, -7
723721
; RV64-NEXT: seqz a0, a0
724722
; RV64-NEXT: addi a0, a0, -1
725723
; RV64-NEXT: ret

llvm/test/CodeGen/RISCV/xaluo.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ define zeroext i1 @usubo.i32.constant.lhs(i32 signext %v1, ptr %res) {
10471047
; RV64-LABEL: usubo.i32.constant.lhs:
10481048
; RV64: # %bb.0: # %entry
10491049
; RV64-NEXT: li a2, -2
1050-
; RV64-NEXT: subw a2, a2, a0
1051-
; RV64-NEXT: addi a0, a2, 1
1050+
; RV64-NEXT: sub a2, a2, a0
1051+
; RV64-NEXT: addiw a0, a2, 1
10521052
; RV64-NEXT: seqz a0, a0
10531053
; RV64-NEXT: sw a2, 0(a1)
10541054
; RV64-NEXT: ret
@@ -1065,8 +1065,8 @@ define zeroext i1 @usubo.i32.constant.lhs(i32 signext %v1, ptr %res) {
10651065
; RV64ZBA-LABEL: usubo.i32.constant.lhs:
10661066
; RV64ZBA: # %bb.0: # %entry
10671067
; RV64ZBA-NEXT: li a2, -2
1068-
; RV64ZBA-NEXT: subw a2, a2, a0
1069-
; RV64ZBA-NEXT: addi a0, a2, 1
1068+
; RV64ZBA-NEXT: sub a2, a2, a0
1069+
; RV64ZBA-NEXT: addiw a0, a2, 1
10701070
; RV64ZBA-NEXT: seqz a0, a0
10711071
; RV64ZBA-NEXT: sw a2, 0(a1)
10721072
; RV64ZBA-NEXT: ret
@@ -1083,8 +1083,8 @@ define zeroext i1 @usubo.i32.constant.lhs(i32 signext %v1, ptr %res) {
10831083
; RV64ZICOND-LABEL: usubo.i32.constant.lhs:
10841084
; RV64ZICOND: # %bb.0: # %entry
10851085
; RV64ZICOND-NEXT: li a2, -2
1086-
; RV64ZICOND-NEXT: subw a2, a2, a0
1087-
; RV64ZICOND-NEXT: addi a0, a2, 1
1086+
; RV64ZICOND-NEXT: sub a2, a2, a0
1087+
; RV64ZICOND-NEXT: addiw a0, a2, 1
10881088
; RV64ZICOND-NEXT: seqz a0, a0
10891089
; RV64ZICOND-NEXT: sw a2, 0(a1)
10901090
; RV64ZICOND-NEXT: ret

0 commit comments

Comments
 (0)