Skip to content

Commit a3a8e1c

Browse files
authored
[TargetLowering][RISCV] Use sra for (X & -256) == 256 -> (X >> 8) == 1 if it yields a better icmp constant. (llvm#151762)
If using srl does not produce a legal constant for the RHS of the final compare, try to use sra instead. Because the AND constant is negative, the sign bits participate in the compare. Using an arithmetic shift right duplicates that bit.
1 parent 2fe9643 commit a3a8e1c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5363,10 +5363,25 @@ SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
53635363
if (AndRHSC.isNegatedPowerOf2() && C1.isSubsetOf(AndRHSC)) {
53645364
unsigned ShiftBits = AndRHSC.countr_zero();
53655365
if (!shouldAvoidTransformToShift(ShValTy, ShiftBits)) {
5366+
// If using an unsigned shift doesn't yield a legal compare
5367+
// immediate, try using sra instead.
5368+
APInt NewC = C1.lshr(ShiftBits);
5369+
if (NewC.getSignificantBits() <= 64 &&
5370+
!isLegalICmpImmediate(NewC.getSExtValue())) {
5371+
APInt SignedC = C1.ashr(ShiftBits);
5372+
if (SignedC.getSignificantBits() <= 64 &&
5373+
isLegalICmpImmediate(SignedC.getSExtValue())) {
5374+
SDValue Shift = DAG.getNode(
5375+
ISD::SRA, dl, ShValTy, N0.getOperand(0),
5376+
DAG.getShiftAmountConstant(ShiftBits, ShValTy, dl));
5377+
SDValue CmpRHS = DAG.getConstant(SignedC, dl, ShValTy);
5378+
return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
5379+
}
5380+
}
53665381
SDValue Shift = DAG.getNode(
53675382
ISD::SRL, dl, ShValTy, N0.getOperand(0),
53685383
DAG.getShiftAmountConstant(ShiftBits, ShValTy, dl));
5369-
SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, ShValTy);
5384+
SDValue CmpRHS = DAG.getConstant(NewC, dl, ShValTy);
53705385
return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
53715386
}
53725387
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s
3+
4+
define i1 @src(i64 %x) {
5+
; CHECK-LABEL: src:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: srai a0, a0, 30
8+
; CHECK-NEXT: addi a0, a0, 2
9+
; CHECK-NEXT: seqz a0, a0
10+
; CHECK-NEXT: ret
11+
%a = and i64 %x, -1073741824
12+
%b = icmp eq i64 %a, -2147483648
13+
ret i1 %b
14+
}

0 commit comments

Comments
 (0)