Skip to content

[InstCombine] Don't mix X << Y / Z << Y with X << Y / X << Z #69302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,7 @@ static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
return Remainder.isMinValue();
}

static Instruction *foldIDivShl(BinaryOperator &I,
InstCombiner::BuilderTy &Builder) {
static Value *foldIDivShl(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
assert((I.getOpcode() == Instruction::SDiv ||
I.getOpcode() == Instruction::UDiv) &&
"Expected integer divide");
Expand All @@ -933,7 +932,6 @@ static Instruction *foldIDivShl(BinaryOperator &I,
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();

Instruction *Ret = nullptr;
Value *X, *Y, *Z;

// With appropriate no-wrap constraints, remove a common factor in the
Expand All @@ -948,12 +946,12 @@ static Instruction *foldIDivShl(BinaryOperator &I,

// (X * Y) u/ (X << Z) --> Y u>> Z
if (!IsSigned && HasNUW)
Ret = BinaryOperator::CreateLShr(Y, Z);
return Builder.CreateLShr(Y, Z, "", I.isExact());

// (X * Y) s/ (X << Z) --> Y s/ (1 << Z)
if (IsSigned && HasNSW && (Op0->hasOneUse() || Op1->hasOneUse())) {
Value *Shl = Builder.CreateShl(ConstantInt::get(Ty, 1), Z);
Ret = BinaryOperator::CreateSDiv(Y, Shl);
return Builder.CreateSDiv(Y, Shl, "", I.isExact());
}
}

Expand All @@ -971,13 +969,13 @@ static Instruction *foldIDivShl(BinaryOperator &I,
((Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap()) ||
(Shl0->hasNoUnsignedWrap() && Shl0->hasNoSignedWrap() &&
Shl1->hasNoSignedWrap())))
Ret = BinaryOperator::CreateUDiv(X, Y);
return Builder.CreateUDiv(X, Y, "", I.isExact());

// For signed div, we need 'nsw' on both shifts + 'nuw' on the divisor.
// (X << Z) / (Y << Z) --> X / Y
if (IsSigned && Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap() &&
Shl1->hasNoUnsignedWrap())
Ret = BinaryOperator::CreateSDiv(X, Y);
return Builder.CreateSDiv(X, Y, "", I.isExact());
}

// If X << Y and X << Z does not overflow, then:
Expand All @@ -998,15 +996,11 @@ static Instruction *foldIDivShl(BinaryOperator &I,
/*HasNSW*/
IsSigned ? (Shl0->hasNoUnsignedWrap() || Shl1->hasNoUnsignedWrap())
: Shl0->hasNoSignedWrap());
Ret = BinaryOperator::CreateLShr(Dividend, Z);
return Builder.CreateLShr(Dividend, Z, "", I.isExact());
}
}

if (!Ret)
return nullptr;

Ret->setIsExact(I.isExact());
return Ret;
return nullptr;
}

/// This function implements the transforms common to both integer division
Expand Down Expand Up @@ -1183,8 +1177,8 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
return NewDiv;
}

if (Instruction *R = foldIDivShl(I, Builder))
return R;
if (Value *R = foldIDivShl(I, Builder))
return replaceInstUsesWith(I, R);

// With the appropriate no-wrap constraint, remove a multiply by the divisor
// after peeking through another divide:
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Transforms/InstCombine/div-shift.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,17 @@ entry:
%div = sdiv i32 %lhs, %rhs
ret i32 %div
}

@a = external global i32
define i32 @pr69291() {
; CHECK-LABEL: @pr69291(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i32 1
;
entry:
%conv = load i32, ptr @a, align 1
%add = shl nuw nsw i32 %conv, 1
%add2 = shl nuw nsw i32 %conv, 1
%div = sdiv i32 %add, %add2
ret i32 %div
}