Skip to content

Commit 5e587b1

Browse files
authored
[RISCV] Lower (setugt X, 2047) as (setne (srl X, 11), 0) (#155541)
This matches 4095 and other pow2-1 constants larger simm12. We normally do this through a DAGCombine controlled by isLegalICmpImmediate. 2047 is considered a legal immediate because we have a setult instruction. In this case we have setugt which isn't natively supported. I added tests for 4095 for comparison.
1 parent de99aab commit 5e587b1

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8193,6 +8193,13 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
81938193
DL, VT, LHS, DAG.getSignedConstant(Imm + 1, DL, OpVT), CCVal);
81948194
return DAG.getLogicalNOT(DL, SetCC, VT);
81958195
}
8196+
// Lower (setugt X, 2047) as (setne (srl X, 11), 0).
8197+
if (CCVal == ISD::SETUGT && Imm == 2047) {
8198+
SDValue Shift = DAG.getNode(ISD::SRL, DL, OpVT, LHS,
8199+
DAG.getShiftAmountConstant(11, OpVT, DL));
8200+
return DAG.getSetCC(DL, VT, Shift, DAG.getConstant(0, DL, OpVT),
8201+
ISD::SETNE);
8202+
}
81968203
}
81978204

81988205
// Not a constant we could handle, swap the operands and condition code to

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,37 @@ define i32 @icmp_ugt_constant_zero(i32 %a) nounwind {
366366
define i32 @icmp_ugt_constant_2047(i32 %a) nounwind {
367367
; RV32I-LABEL: icmp_ugt_constant_2047:
368368
; RV32I: # %bb.0:
369-
; RV32I-NEXT: li a1, 2047
370-
; RV32I-NEXT: sltu a0, a1, a0
369+
; RV32I-NEXT: srli a0, a0, 11
370+
; RV32I-NEXT: snez a0, a0
371371
; RV32I-NEXT: ret
372372
;
373373
; RV32XQCILIA-LABEL: icmp_ugt_constant_2047:
374374
; RV32XQCILIA: # %bb.0:
375-
; RV32XQCILIA-NEXT: li a1, 2047
376-
; RV32XQCILIA-NEXT: sltu a0, a1, a0
375+
; RV32XQCILIA-NEXT: srli a0, a0, 11
376+
; RV32XQCILIA-NEXT: snez a0, a0
377377
; RV32XQCILIA-NEXT: ret
378378
%1 = icmp ugt i32 %a, 2047
379379
%2 = zext i1 %1 to i32
380380
ret i32 %2
381381
}
382382

383+
define i32 @icmp_ugt_constant_4095(i32 %a) nounwind {
384+
; RV32I-LABEL: icmp_ugt_constant_4095:
385+
; RV32I: # %bb.0:
386+
; RV32I-NEXT: srli a0, a0, 12
387+
; RV32I-NEXT: snez a0, a0
388+
; RV32I-NEXT: ret
389+
;
390+
; RV32XQCILIA-LABEL: icmp_ugt_constant_4095:
391+
; RV32XQCILIA: # %bb.0:
392+
; RV32XQCILIA-NEXT: srli a0, a0, 12
393+
; RV32XQCILIA-NEXT: snez a0, a0
394+
; RV32XQCILIA-NEXT: ret
395+
%1 = icmp ugt i32 %a, 4095
396+
%2 = zext i1 %1 to i32
397+
ret i32 %2
398+
}
399+
383400
define i32 @icmp_ugt_constant_2046(i32 %a) nounwind {
384401
; RV32I-LABEL: icmp_ugt_constant_2046:
385402
; RV32I: # %bb.0:
@@ -487,14 +504,14 @@ define i32 @icmp_uge_constant_2047(i32 %a) nounwind {
487504
define i32 @icmp_uge_constant_2048(i32 %a) nounwind {
488505
; RV32I-LABEL: icmp_uge_constant_2048:
489506
; RV32I: # %bb.0:
490-
; RV32I-NEXT: li a1, 2047
491-
; RV32I-NEXT: sltu a0, a1, a0
507+
; RV32I-NEXT: srli a0, a0, 11
508+
; RV32I-NEXT: snez a0, a0
492509
; RV32I-NEXT: ret
493510
;
494511
; RV32XQCILIA-LABEL: icmp_uge_constant_2048:
495512
; RV32XQCILIA: # %bb.0:
496-
; RV32XQCILIA-NEXT: li a1, 2047
497-
; RV32XQCILIA-NEXT: sltu a0, a1, a0
513+
; RV32XQCILIA-NEXT: srli a0, a0, 11
514+
; RV32XQCILIA-NEXT: snez a0, a0
498515
; RV32XQCILIA-NEXT: ret
499516
%1 = icmp uge i32 %a, 2048
500517
%2 = zext i1 %1 to i32

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,25 @@ define i64 @icmp_ugt_constant_zero(i64 %a) nounwind {
191191
define i64 @icmp_ugt_constant_2047(i64 %a) nounwind {
192192
; RV64I-LABEL: icmp_ugt_constant_2047:
193193
; RV64I: # %bb.0:
194-
; RV64I-NEXT: li a1, 2047
195-
; RV64I-NEXT: sltu a0, a1, a0
194+
; RV64I-NEXT: srli a0, a0, 11
195+
; RV64I-NEXT: snez a0, a0
196196
; RV64I-NEXT: ret
197197
%1 = icmp ugt i64 %a, 2047
198198
%2 = zext i1 %1 to i64
199199
ret i64 %2
200200
}
201201

202+
define i64 @icmp_ugt_constant_4095(i64 %a) nounwind {
203+
; RV64I-LABEL: icmp_ugt_constant_4095:
204+
; RV64I: # %bb.0:
205+
; RV64I-NEXT: srli a0, a0, 12
206+
; RV64I-NEXT: snez a0, a0
207+
; RV64I-NEXT: ret
208+
%1 = icmp ugt i64 %a, 4095
209+
%2 = zext i1 %1 to i64
210+
ret i64 %2
211+
}
212+
202213
define i64 @icmp_ugt_constant_2046(i64 %a) nounwind {
203214
; RV64I-LABEL: icmp_ugt_constant_2046:
204215
; RV64I: # %bb.0:
@@ -270,8 +281,8 @@ define i64 @icmp_uge_constant_2047(i64 %a) nounwind {
270281
define i64 @icmp_uge_constant_2048(i64 %a) nounwind {
271282
; RV64I-LABEL: icmp_uge_constant_2048:
272283
; RV64I: # %bb.0:
273-
; RV64I-NEXT: li a1, 2047
274-
; RV64I-NEXT: sltu a0, a1, a0
284+
; RV64I-NEXT: srli a0, a0, 11
285+
; RV64I-NEXT: snez a0, a0
275286
; RV64I-NEXT: ret
276287
%1 = icmp uge i64 %a, 2048
277288
%2 = zext i1 %1 to i64
@@ -754,10 +765,10 @@ define i64 @icmp_ne_zext_inreg_umin(i64 %a) nounwind {
754765
; RV64I: # %bb.0:
755766
; RV64I-NEXT: lui a1, 30141
756767
; RV64I-NEXT: addi a1, a1, -747
757-
; RV64I-NEXT: bltu a0, a1, .LBB67_2
768+
; RV64I-NEXT: bltu a0, a1, .LBB68_2
758769
; RV64I-NEXT: # %bb.1:
759770
; RV64I-NEXT: mv a0, a1
760-
; RV64I-NEXT: .LBB67_2:
771+
; RV64I-NEXT: .LBB68_2:
761772
; RV64I-NEXT: addi a0, a0, -123
762773
; RV64I-NEXT: snez a0, a0
763774
; RV64I-NEXT: ret

0 commit comments

Comments
 (0)