Skip to content

[InstSimplify] Teach simplifyICmpWithConstant about samesign #125899

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

Closed
wants to merge 3 commits into from
Closed
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
37 changes: 24 additions & 13 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,21 +2999,32 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
return ConstantInt::getBool(ITy, !TrueIfSigned);
}

// Rule out tautological comparisons (eg., ult 0 or uge 0).
ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
if (RHS_CR.isEmptySet())
return ConstantInt::getFalse(ITy);
if (RHS_CR.isFullSet())
return ConstantInt::getTrue(ITy);

ConstantRange LHS_CR =
ConstantRange LCR =
computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
if (!LHS_CR.isFullSet()) {
if (RHS_CR.contains(LHS_CR))
return ConstantInt::getTrue(ITy);
if (RHS_CR.inverse().contains(LHS_CR))

auto CheckCR = [&](const ConstantRange &CR) -> Constant * {
// Rule out tautological comparisons (eg., ult 0 or uge 0).
if (CR.isEmptySet())
return ConstantInt::getFalse(ITy);
}
if (CR.isFullSet())
return ConstantInt::getTrue(ITy);

if (!LCR.isFullSet()) {
if (CR.contains(LCR))
return ConstantInt::getTrue(ITy);
if (CR.inverse().contains(LCR))
return ConstantInt::getFalse(ITy);
}
return nullptr;
};

// Check unsigned and signed versions of relational predicates with samesign.
if (auto *K = CheckCR(ConstantRange::makeExactICmpRegion(Pred, *C)))
return K;
if (IIQ.UseInstrInfo && Pred.hasSameSign() && ICmpInst::isRelational(Pred))
if (auto *K = CheckCR(ConstantRange::makeExactICmpRegion(
ICmpInst::getSignedPredicate(Pred), *C)))
return K;

// (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
// (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/Analysis/ValueTracking/constant-ranges.ll
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ define i1 @srem_posC_okay0(i8 %x) {
ret i1 %r
}

define i1 @srem_posC_okay0_samesign(i8 %x) {
; CHECK-LABEL: @srem_posC_okay0_samesign(
; CHECK-NEXT: ret i1 true
;
%val = srem i8 34, %x
%r = icmp samesign ule i8 %val, 34
ret i1 %r
}

define i1 @srem_posC_okay1(i8 %x) {
; CHECK-LABEL: @srem_posC_okay1(
; CHECK-NEXT: ret i1 true
Expand All @@ -169,6 +178,15 @@ define i1 @srem_posC_okay1(i8 %x) {
ret i1 %r
}

define i1 @srem_posC_okay1_samesign(i8 %x) {
; CHECK-LABEL: @srem_posC_okay1_samesign(
; CHECK-NEXT: ret i1 false
;
%val = srem i8 34, %x
%r = icmp samesign uge i8 %val, -3
ret i1 %r
}

define i1 @srem_negC_okay0(i8 %x) {
; CHECK-LABEL: @srem_negC_okay0(
; CHECK-NEXT: ret i1 true
Expand All @@ -178,6 +196,15 @@ define i1 @srem_negC_okay0(i8 %x) {
ret i1 %r
}

define i1 @srem_negC_okay0_samesign(i8 %x) {
; CHECK-LABEL: @srem_negC_okay0_samesign(
; CHECK-NEXT: ret i1 true
;
%val = srem i8 -34, %x
%r = icmp samesign ule i8 %val, 0
ret i1 %r
}

define i1 @srem_negC_okay1(i8 %x) {
; CHECK-LABEL: @srem_negC_okay1(
; CHECK-NEXT: ret i1 true
Expand All @@ -187,6 +214,15 @@ define i1 @srem_negC_okay1(i8 %x) {
ret i1 %r
}

define i1 @srem_negC_okay1_samesign(i8 %x) {
; CHECK-LABEL: @srem_negC_okay1_samesign(
; CHECK-NEXT: ret i1 true
;
%val = srem i8 -34, %x
%r = icmp samesign uge i8 %val, -34
ret i1 %r
}

define i1 @srem_posC_fail0(i8 %x) {
; CHECK-LABEL: @srem_posC_fail0(
; CHECK-NEXT: [[VAL:%.*]] = srem i8 34, [[X:%.*]]
Expand Down