Skip to content

Commit a20e9f2

Browse files
committed
[InstCombine] lshr (mul (X, 2^N + 1)), N -> add (X, lshr(X, N))
A generalization of the proposed x * 3/2 -> x + (x >> 1) transformation but applies to all multiplications that are one more than a power of 2. Proof: https://alive2.llvm.org/ce/z/WaZYvn
1 parent bc625cf commit a20e9f2

File tree

3 files changed

+36
-61
lines changed

3 files changed

+36
-61
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,13 +1411,24 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14111411

14121412
const APInt *MulC;
14131413
if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC)))) {
1414-
// Look for a "splat" mul pattern - it replicates bits across each half of
1415-
// a value, so a right shift is just a mask of the low bits:
1416-
// lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1417-
// TODO: Generalize to allow more than just half-width shifts?
1418-
if (BitWidth > 2 && ShAmtC * 2 == BitWidth && (*MulC - 1).isPowerOf2() &&
1419-
MulC->logBase2() == ShAmtC)
1420-
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1414+
if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1415+
MulC->logBase2() == ShAmtC) {
1416+
// Look for a "splat" mul pattern - it replicates bits across each half
1417+
// of a value, so a right shift is just a mask of the low bits:
1418+
// lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1419+
if (ShAmtC * 2 == BitWidth)
1420+
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1421+
1422+
// lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N))
1423+
if (Op0->hasOneUse()) {
1424+
auto *NewAdd = BinaryOperator::CreateNUWAdd(
1425+
X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1426+
I.isExact()));
1427+
NewAdd->setHasNoSignedWrap(
1428+
cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1429+
return NewAdd;
1430+
}
1431+
}
14211432

14221433
// The one-use check is not strictly necessary, but codegen may not be
14231434
// able to invert the transform and perf may suffer with an extra mul
@@ -1437,6 +1448,16 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
14371448
}
14381449
}
14391450

1451+
// lshr (mul nsw (X, 2^N + 1)), N -> add nsw (X, lshr(X, N))
1452+
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(MulC))))) {
1453+
if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1454+
MulC->logBase2() == ShAmtC) {
1455+
return BinaryOperator::CreateNSWAdd(
1456+
X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1457+
I.isExact()));
1458+
}
1459+
}
1460+
14401461
// Try to narrow bswap.
14411462
// In the case where the shift amount equals the bitwidth difference, the
14421463
// shift is eliminated.

llvm/test/Transforms/InstCombine/ashr-lshr.ll

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -605,56 +605,10 @@ define <2 x i8> @ashr_known_pos_exact_vec(<2 x i8> %x, <2 x i8> %y) {
605605
ret <2 x i8> %r
606606
}
607607

608-
define i32 @ashr_mul_times_3_div_2(i32 %0) {
609-
; CHECK-LABEL: @ashr_mul_times_3_div_2(
610-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i32 [[TMP0:%.*]], 3
611-
; CHECK-NEXT: [[ASHR:%.*]] = ashr i32 [[MUL]], 1
612-
; CHECK-NEXT: ret i32 [[ASHR]]
613-
;
614-
%mul = mul nsw nuw i32 %0, 3
615-
%ashr = ashr i32 %mul, 1
616-
ret i32 %ashr
617-
}
618-
619-
define i32 @ashr_mul_times_3_div_2_exact(i32 %x) {
620-
; CHECK-LABEL: @ashr_mul_times_3_div_2_exact(
621-
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[X:%.*]], 3
622-
; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i32 [[MUL]], 1
623-
; CHECK-NEXT: ret i32 [[ASHR]]
624-
;
625-
%mul = mul nsw i32 %x, 3
626-
%ashr = ashr exact i32 %mul, 1
627-
ret i32 %ashr
628-
}
629-
630-
define i32 @mul_times_3_div_2_multiuse(i32 %x) {
631-
; CHECK-LABEL: @mul_times_3_div_2_multiuse(
632-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i32 [[X:%.*]], 3
633-
; CHECK-NEXT: [[RES:%.*]] = ashr i32 [[MUL]], 1
634-
; CHECK-NEXT: call void @use(i32 [[MUL]])
635-
; CHECK-NEXT: ret i32 [[RES]]
636-
;
637-
%mul = mul nuw i32 %x, 3
638-
%res = ashr i32 %mul, 1
639-
call void @use (i32 %mul)
640-
ret i32 %res
641-
}
642-
643-
define i32 @ashr_mul_times_3_div_2_exact_2(i32 %x) {
644-
; CHECK-LABEL: @ashr_mul_times_3_div_2_exact_2(
645-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i32 [[X:%.*]], 3
646-
; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i32 [[MUL]], 1
647-
; CHECK-NEXT: ret i32 [[ASHR]]
648-
;
649-
%mul = mul nuw i32 %x, 3
650-
%ashr = ashr exact i32 %mul, 1
651-
ret i32 %ashr
652-
}
653-
654608
define i32 @lshr_mul_times_3_div_2(i32 %0) {
655609
; CHECK-LABEL: @lshr_mul_times_3_div_2(
656-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i32 [[TMP0:%.*]], 3
657-
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[MUL]], 1
610+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP0:%.*]], 1
611+
; CHECK-NEXT: [[LSHR:%.*]] = add nuw nsw i32 [[TMP2]], [[TMP0]]
658612
; CHECK-NEXT: ret i32 [[LSHR]]
659613
;
660614
%mul = mul nsw nuw i32 %0, 3
@@ -664,8 +618,8 @@ define i32 @lshr_mul_times_3_div_2(i32 %0) {
664618

665619
define i32 @lshr_mul_times_3_div_2_exact(i32 %x) {
666620
; CHECK-LABEL: @lshr_mul_times_3_div_2_exact(
667-
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[X:%.*]], 3
668-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[MUL]], 1
621+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[X:%.*]], 1
622+
; CHECK-NEXT: [[LSHR:%.*]] = add nsw i32 [[TMP1]], [[X]]
669623
; CHECK-NEXT: ret i32 [[LSHR]]
670624
;
671625
%mul = mul nsw i32 %x, 3
@@ -688,8 +642,8 @@ define i32 @mul_times_3_div_2_multiuse_lshr(i32 %x) {
688642

689643
define i32 @lshr_mul_times_3_div_2_exact_2(i32 %x) {
690644
; CHECK-LABEL: @lshr_mul_times_3_div_2_exact_2(
691-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i32 [[X:%.*]], 3
692-
; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[MUL]], 1
645+
; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i32 [[X:%.*]], 1
646+
; CHECK-NEXT: [[LSHR:%.*]] = add nuw i32 [[TMP1]], [[X]]
693647
; CHECK-NEXT: ret i32 [[LSHR]]
694648
;
695649
%mul = mul nuw i32 %x, 3

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ define i32 @mul_splat_fold_wrong_lshr_const(i32 %x) {
390390

391391
define i32 @mul_splat_fold_no_nuw(i32 %x) {
392392
; CHECK-LABEL: @mul_splat_fold_no_nuw(
393-
; CHECK-NEXT: [[M:%.*]] = mul nsw i32 [[X:%.*]], 65537
394-
; CHECK-NEXT: [[T:%.*]] = lshr i32 [[M]], 16
393+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 16
394+
; CHECK-NEXT: [[T:%.*]] = add nsw i32 [[TMP1]], [[X]]
395395
; CHECK-NEXT: ret i32 [[T]]
396396
;
397397
%m = mul nsw i32 %x, 65537

0 commit comments

Comments
 (0)