Skip to content

Commit 41b7713

Browse files
committed
[DAGCombine] Transform shl X, cttz(Y) to mul (Y & -Y), X if cttz is unsupported
1 parent d961f70 commit 41b7713

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9962,6 +9962,18 @@ SDValue DAGCombiner::visitSHL(SDNode *N) {
99629962
if (SDValue NewSHL = visitShiftByConstant(N))
99639963
return NewSHL;
99649964

9965+
// fold (shl X, cttz(Y)) -> (mul (Y & -Y), X) if cttz is unsupported on the
9966+
// target.
9967+
if ((N1.getOpcode() == ISD::CTTZ || N1.getOpcode() == ISD::CTTZ_ZERO_UNDEF) &&
9968+
N1.hasOneUse() && !TLI.isOperationLegalOrCustom(ISD::CTTZ, VT) &&
9969+
TLI.isOperationLegalOrCustom(ISD::MUL, VT)) {
9970+
SDValue Y = N1.getOperand(0);
9971+
SDLoc DL(N);
9972+
SDValue NegY = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Y);
9973+
SDValue And = DAG.getNode(ISD::AND, DL, VT, Y, NegY);
9974+
return DAG.getNode(ISD::MUL, DL, VT, And, N0);
9975+
}
9976+
99659977
if (SimplifyDemandedBits(SDValue(N, 0)))
99669978
return SDValue(N, 0);
99679979

llvm/test/CodeGen/RISCV/shl-cttz.ll

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,7 @@ define i32 @shl_cttz_i32(i32 %x, i32 %y) {
383383
; RV32I: # %bb.0: # %entry
384384
; RV32I-NEXT: neg a2, a1
385385
; RV32I-NEXT: and a1, a1, a2
386-
; RV32I-NEXT: lui a2, 30667
387-
; RV32I-NEXT: addi a2, a2, 1329
388-
; RV32I-NEXT: mul a1, a1, a2
389-
; RV32I-NEXT: srli a1, a1, 27
390-
; RV32I-NEXT: lui a2, %hi(.LCPI4_0)
391-
; RV32I-NEXT: addi a2, a2, %lo(.LCPI4_0)
392-
; RV32I-NEXT: add a1, a2, a1
393-
; RV32I-NEXT: lbu a1, 0(a1)
394-
; RV32I-NEXT: sll a0, a0, a1
386+
; RV32I-NEXT: mul a0, a1, a0
395387
; RV32I-NEXT: ret
396388
;
397389
; RV32ZBB-LABEL: shl_cttz_i32:
@@ -492,16 +484,7 @@ define i32 @shl_cttz_constant_i32(i32 %y) {
492484
; RV32I: # %bb.0: # %entry
493485
; RV32I-NEXT: neg a1, a0
494486
; RV32I-NEXT: and a0, a0, a1
495-
; RV32I-NEXT: lui a1, 30667
496-
; RV32I-NEXT: addi a1, a1, 1329
497-
; RV32I-NEXT: mul a0, a0, a1
498-
; RV32I-NEXT: srli a0, a0, 27
499-
; RV32I-NEXT: lui a1, %hi(.LCPI6_0)
500-
; RV32I-NEXT: addi a1, a1, %lo(.LCPI6_0)
501-
; RV32I-NEXT: add a0, a1, a0
502-
; RV32I-NEXT: lbu a0, 0(a0)
503-
; RV32I-NEXT: li a1, 4
504-
; RV32I-NEXT: sll a0, a1, a0
487+
; RV32I-NEXT: slli a0, a0, 2
505488
; RV32I-NEXT: ret
506489
;
507490
; RV32ZBB-LABEL: shl_cttz_constant_i32:
@@ -719,17 +702,9 @@ define i64 @shl_cttz_i64(i64 %x, i64 %y) {
719702
;
720703
; RV64I-LABEL: shl_cttz_i64:
721704
; RV64I: # %bb.0: # %entry
722-
; RV64I-NEXT: lui a2, %hi(.LCPI8_0)
723-
; RV64I-NEXT: ld a2, %lo(.LCPI8_0)(a2)
724-
; RV64I-NEXT: neg a3, a1
725-
; RV64I-NEXT: and a1, a1, a3
726-
; RV64I-NEXT: mul a1, a1, a2
727-
; RV64I-NEXT: srli a1, a1, 58
728-
; RV64I-NEXT: lui a2, %hi(.LCPI8_1)
729-
; RV64I-NEXT: addi a2, a2, %lo(.LCPI8_1)
730-
; RV64I-NEXT: add a1, a2, a1
731-
; RV64I-NEXT: lbu a1, 0(a1)
732-
; RV64I-NEXT: sll a0, a0, a1
705+
; RV64I-NEXT: neg a2, a1
706+
; RV64I-NEXT: and a1, a1, a2
707+
; RV64I-NEXT: mul a0, a1, a0
733708
; RV64I-NEXT: ret
734709
;
735710
; RV64ZBB-LABEL: shl_cttz_i64:
@@ -812,18 +787,9 @@ define i64 @shl_cttz_constant_i64(i64 %y) {
812787
;
813788
; RV64I-LABEL: shl_cttz_constant_i64:
814789
; RV64I: # %bb.0: # %entry
815-
; RV64I-NEXT: lui a1, %hi(.LCPI9_0)
816-
; RV64I-NEXT: ld a1, %lo(.LCPI9_0)(a1)
817-
; RV64I-NEXT: neg a2, a0
818-
; RV64I-NEXT: and a0, a0, a2
819-
; RV64I-NEXT: mul a0, a0, a1
820-
; RV64I-NEXT: srli a0, a0, 58
821-
; RV64I-NEXT: lui a1, %hi(.LCPI9_1)
822-
; RV64I-NEXT: addi a1, a1, %lo(.LCPI9_1)
823-
; RV64I-NEXT: add a0, a1, a0
824-
; RV64I-NEXT: lbu a0, 0(a0)
825-
; RV64I-NEXT: li a1, 4
826-
; RV64I-NEXT: sll a0, a1, a0
790+
; RV64I-NEXT: neg a1, a0
791+
; RV64I-NEXT: and a0, a0, a1
792+
; RV64I-NEXT: slli a0, a0, 2
827793
; RV64I-NEXT: ret
828794
;
829795
; RV64ZBB-LABEL: shl_cttz_constant_i64:

0 commit comments

Comments
 (0)