Skip to content

Commit 7de2345

Browse files
committed
[InstCombine] Fold (X * (Y << K)) u>> K -> X * Y when highbits are not demanded
1 parent 5bee7a7 commit 7de2345

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,15 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
770770
return InsertNewInstWith(Shl, I->getIterator());
771771
}
772772
}
773+
774+
const APInt *Factor;
775+
if (match(I->getOperand(0),
776+
m_OneUse(m_Mul(m_Value(X), m_APInt(Factor)))) &&
777+
Factor->countr_zero() >= ShiftAmt) {
778+
BinaryOperator *Mul = BinaryOperator::CreateMul(
779+
X, ConstantInt::get(X->getType(), Factor->lshr(ShiftAmt)));
780+
return InsertNewInstWith(Mul, I->getIterator());
781+
}
773782
}
774783

775784
// Unsigned shift right.

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,8 @@ define <2 x i8> @bool_add_lshr_vec_wrong_shift_amt(<2 x i1> %a, <2 x i1> %b) {
15271527
define i32 @lowbits_of_lshr_mul(i64 %x) {
15281528
; CHECK-LABEL: @lowbits_of_lshr_mul(
15291529
; CHECK-NEXT: entry:
1530-
; CHECK-NEXT: [[MUL:%.*]] = mul i64 [[X:%.*]], 64424509440
1531-
; CHECK-NEXT: [[SHIFT:%.*]] = lshr exact i64 [[MUL]], 32
1532-
; CHECK-NEXT: [[CONV:%.*]] = trunc nuw i64 [[SHIFT]] to i32
1530+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[X:%.*]] to i32
1531+
; CHECK-NEXT: [[CONV:%.*]] = mul i32 [[TMP0]], 15
15331532
; CHECK-NEXT: ret i32 [[CONV]]
15341533
;
15351534
entry:
@@ -1542,9 +1541,8 @@ entry:
15421541
define i32 @lowbits_of_lshr_mul_mask(i32 %x) {
15431542
; CHECK-LABEL: @lowbits_of_lshr_mul_mask(
15441543
; CHECK-NEXT: entry:
1545-
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[X:%.*]], 104857600
1546-
; CHECK-NEXT: [[SHIFT:%.*]] = lshr exact i32 [[MUL]], 16
1547-
; CHECK-NEXT: [[CONV:%.*]] = and i32 [[SHIFT]], 32704
1544+
; CHECK-NEXT: [[TMP0:%.*]] = mul i32 [[X:%.*]], 1600
1545+
; CHECK-NEXT: [[CONV:%.*]] = and i32 [[TMP0]], 32704
15481546
; CHECK-NEXT: ret i32 [[CONV]]
15491547
;
15501548
entry:

0 commit comments

Comments
 (0)