Skip to content

[InstCombine] Infer nuw on mul nsw with non-negative operands #90170

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
Apr 29, 2024
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
3 changes: 2 additions & 1 deletion llvm/include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,8 @@ enum class OverflowResult {
};

OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS,
const SimplifyQuery &SQ);
const SimplifyQuery &SQ,
bool IsNSW = false);
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
const SimplifyQuery &SQ);
OverflowResult
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {

OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForUnsignedMul(LHS, RHS,
SQ.getWithInstruction(CxtI));
const Instruction *CxtI,
bool IsNSW = false) const {
return llvm::computeOverflowForUnsignedMul(
LHS, RHS, SQ.getWithInstruction(CxtI), IsNSW);
}

OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6686,9 +6686,15 @@ llvm::computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,

OverflowResult llvm::computeOverflowForUnsignedMul(const Value *LHS,
const Value *RHS,
const SimplifyQuery &SQ) {
const SimplifyQuery &SQ,
bool IsNSW) {
KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);

// mul nsw of two non-negative numbers is also nuw.
if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
return OverflowResult::NeverOverflows;

ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
}

bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
const Instruction &CxtI) const {
return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
const Instruction &CxtI,
bool IsNSW = false) const {
return computeOverflowForUnsignedMul(LHS, RHS, &CxtI, IsNSW) ==
OverflowResult::NeverOverflows;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
I.setHasNoSignedWrap(true);
}

if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1, I)) {
if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1, I, I.hasNoSignedWrap())) {
Changed = true;
I.setHasNoUnsignedWrap(true);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/mul.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,7 @@ define i8 @mul_nsw_nonneg(i8 %x, i8 %y) {
; CHECK-NEXT: call void @llvm.assume(i1 [[X_NNEG]])
; CHECK-NEXT: [[Y_NNEG:%.*]] = icmp sgt i8 [[Y:%.*]], -1
; CHECK-NEXT: call void @llvm.assume(i1 [[Y_NNEG]])
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[X]], [[Y]]
; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i8 [[X]], [[Y]]
; CHECK-NEXT: ret i8 [[MUL]]
;
%x.nneg = icmp sge i8 %x, 0
Expand Down