Skip to content

Commit

Permalink
[InstCombine] Fold adds + shifts with nsw and nuw flags
Browse files Browse the repository at this point in the history
I also added mul nsw/nuw 3, div 2 since this was the canonical version of ((x << 1) + x) / 2, which is a specific expression which canonicalization causes the InstCombine to miss it.
  • Loading branch information
AZero13 committed Apr 9, 2024
1 parent aaf0fe9 commit 8967469
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
52 changes: 50 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,19 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
match(Op1, m_SpecificIntAllowUndef(BitWidth - 1)))
return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);

// Special Case:
// if both the add and the shift are nuw, we can omit the AND entirely
// ((X << Y) nuw + Z nuw) >>u Z --> (X + (Y >>u Z))
Value *Y;
if (match(Op0, m_OneUse(m_c_NUWAdd((m_NUWShl(m_Value(X), m_Specific(Op1))),
m_Value(Y))))) {
Value *NewLshr = Builder.CreateLShr(Y, Op1, "", I.isExact());
auto *newAdd = BinaryOperator::CreateNUWAdd(NewLshr, X);
if (auto *Op0Bin = cast<BinaryOperator>(Op0))
newAdd->setHasNoSignedWrap(Op0Bin->hasNoSignedWrap());
return newAdd;
}

if (match(Op1, m_APInt(C))) {
unsigned ShAmtC = C->getZExtValue();
auto *II = dyn_cast<IntrinsicInst>(Op0);
Expand All @@ -1283,7 +1296,6 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
return new ZExtInst(Cmp, Ty);
}

Value *X;
const APInt *C1;
if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
if (C1->ult(ShAmtC)) {
Expand Down Expand Up @@ -1328,7 +1340,7 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
// ((X << C) + Y) >>u C --> (X + (Y >>u C)) & (-1 >>u C)
// TODO: Consolidate with the more general transform that starts from shl
// (the shifts are in the opposite order).
Value *Y;

if (match(Op0,
m_OneUse(m_c_Add(m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))),
m_Value(Y))))) {
Expand Down Expand Up @@ -1450,9 +1462,25 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
NewMul->setHasNoSignedWrap(true);
return NewMul;
}

// Special case:
// lshr nuw (mul (X, 3), 1) -> add nuw nsw (X, lshr(X, 1)
if (ShAmtC == 1 && MulC->getZExtValue() == 3) {
auto *NewAdd = BinaryOperator::CreateNUWAdd(
X,
Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact()));
NewAdd->setHasNoSignedWrap(true);
return NewAdd;
}
}
}

// // lshr nsw (mul (X, 3), 1) -> add nsw (X, lshr(X, 1)
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_SpecificInt(3)))) &&
ShAmtC == 1)
return BinaryOperator::CreateNSWAdd(
X, Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact()));

// Try to narrow bswap.
// In the case where the shift amount equals the bitwidth difference, the
// shift is eliminated.
Expand Down Expand Up @@ -1656,6 +1684,26 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
}

// Special case: ashr nuw (mul (X, 3), 1) -> add nuw nsw (X, lshr(X, 1)
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_SpecificInt(3)))) &&
ShAmt == 1) {
Value *Shift;
if (auto *Op0Bin = cast<BinaryOperator>(Op0)) {
if (Op0Bin->hasNoUnsignedWrap())
// We can use lshr if the mul is nuw and nsw
Shift =
Builder.CreateLShr(X, ConstantInt::get(Ty, 1), "", I.isExact());
else
Shift =
Builder.CreateAShr(X, ConstantInt::get(Ty, 1), "", I.isExact());

auto *NewAdd = BinaryOperator::CreateNSWAdd(X, Shift);
NewAdd->setHasNoUnsignedWrap(Op0Bin->hasNoUnsignedWrap());

return NewAdd;
}
}
}

const SimplifyQuery Q = SQ.getWithInstruction(&I);
Expand Down
25 changes: 12 additions & 13 deletions llvm/test/Transforms/InstCombine/lshr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ define <3 x i14> @mul_splat_fold_vec(<3 x i14> %x) {

define i32 @mul_times_3_div_2 (i32 %x) {
; CHECK-LABEL: @mul_times_3_div_2(
; CHECK-NEXT: [[TMP1:%.*]] = mul nuw nsw i32 [[X:%.*]], 3
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], 1
; CHECK-NEXT: ret i32 [[TMP2]]
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1:%.*]], 1
; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], [[TMP1]]
; CHECK-NEXT: ret i32 [[TMP3]]
;
%2 = mul nsw nuw i32 %x, 3
%3 = lshr i32 %2, 1
Expand All @@ -373,21 +373,20 @@ define i32 @mul_times_3_div_2 (i32 %x) {

define i32 @shl_add_lshr (i32 %x, i32 %c, i32 %y) {
; CHECK-LABEL: @shl_add_lshr(
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: [[TMP3:%.*]] = lshr exact i32 [[TMP2]], [[C]]
; CHECK-NEXT: ret i32 [[TMP3]]
; CHECK-NEXT: [[TMP3:%.*]] = lshr exact i32 [[TMP2:%.*]], [[C:%.*]]
; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i32 [[TMP3]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[TMP4]]
;
%2 = shl nuw i32 %x, %c
%3 = add nsw nuw i32 %2, %y
%3 = add nuw nsw i32 %2, %y
%4 = lshr exact i32 %3, %c
ret i32 %4
}

define i32 @ashr_mul_times_3_div_2 (i32 %0) {
; CHECK-LABEL: @ashr_mul_times_3_div_2(
; CHECK-NEXT: [[TMP2:%.*]] = mul nuw nsw i32 [[TMP0:%.*]], 3
; CHECK-NEXT: [[TMP3:%.*]] = ashr i32 [[TMP2]], 1
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP0:%.*]], 1
; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], [[TMP0]]
; CHECK-NEXT: ret i32 [[TMP3]]
;
%2 = mul nsw nuw i32 %0, 3
Expand All @@ -397,9 +396,9 @@ define i32 @mul_times_3_div_2 (i32 %x) {

define i32 @ashr_mul_times_3_div_2_exact (i32 %0) {
; CHECK-LABEL: @ashr_mul_times_3_div_2_exact(
; CHECK-NEXT: [[TMP2:%.*]] = mul nsw i32 [[TMP0:%.*]], 3
; CHECK-NEXT: [[TMP3:%.*]] = ashr exact i32 [[TMP2]], 1
; CHECK-NEXT: ret i32 [[TMP3]]
; CHECK-NEXT: [[TMP3:%.*]] = ashr exact i32 [[TMP2:%.*]], 1
; CHECK-NEXT: [[TMP4:%.*]] = add nsw i32 [[TMP3]], [[TMP2]]
; CHECK-NEXT: ret i32 [[TMP4]]
;
%2 = mul nsw i32 %0, 3
%3 = ashr exact i32 %2, 1
Expand Down

0 comments on commit 8967469

Please sign in to comment.