Skip to content

[InstSimplify] Canonicalize predicates into strict versions in simplifyICmpInst #151642

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3025,9 +3025,6 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
*MulC != 0 && C->srem(*MulC) != 0)))
return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);

if (Pred == ICmpInst::ICMP_UGE && C->isOne() && isKnownNonZero(LHS, Q))
return ConstantInt::getTrue(ITy);

return nullptr;
}

Expand Down Expand Up @@ -3774,6 +3771,16 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
return V;

const APInt *C;
if (match(RHS, m_APIntAllowPoison(C)) &&
ICmpInst::isNonStrictPredicate(Pred) && !C->isZero()) {
if (auto Flipped = getFlippedStrictnessPredicateAndConstant(
Pred, ConstantInt::get(LHS->getType(), *C))) {
Pred = Flipped->first;
RHS = Flipped->second;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use getFlippedStrictnessPredicateAndConstant() to generalize this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the current implementation provides special handling only for comparisons against zero. I'm not sure if flipping other constant and predicate pairs is profitable. For instance, converting x sgt 0 to x sge -1 causes regressions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What regressions do you see?

The non-strict predicates are the canonical form, so I'd generally expect code to handle them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What regressions do you see?

el-ev@ee94499

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What regressions do you see without the !isZero() check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transforms/Attributor/lvi-after-jumpthreading.ll
Transforms/InstSimplify/abs_intrinsic.ll
Transforms/InstSimplify/compare.ll


// TODO: Sink/common this with other potentially expensive calls that use
// ValueTracking? See comment below for isKnownNonEqual().
if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/Transforms/InstSimplify/compare.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,9 @@ define <2 x i1> @icmp_shl_1_ule_signmask_poison(<2 x i8> %V) {

define <2 x i1> @icmp_shl_1_ule_signmask_poison2(<2 x i8> %V) {
; CHECK-LABEL: @icmp_shl_1_ule_signmask_poison2(
; CHECK-NEXT: ret <2 x i1> splat (i1 true)
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i8> <i8 1, i8 poison>, [[V:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ule <2 x i8> [[SHL]], <i8 poison, i8 -128>
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%shl = shl <2 x i8> <i8 1, i8 poison>, %V
%cmp = icmp ule <2 x i8> %shl, <i8 poison, i8 128>
Expand Down
6 changes: 1 addition & 5 deletions llvm/test/Transforms/InstSimplify/select-icmp.ll
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s

; TODO: https://alive2.llvm.org/ce/z/3ybZRl
define i32 @pr54735_slt(i32 %x, i32 %y) {
; CHECK-LABEL: @pr54735_slt(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
; CHECK: cond.true:
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
; CHECK-NEXT: [[ABSCOND:%.*]] = icmp sle i32 [[SUB]], -1
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
; CHECK-NEXT: ret i32 [[ABS]]
; CHECK-NEXT: ret i32 [[NEG]]
; CHECK: cond.end:
; CHECK-NEXT: ret i32 0
;
Expand Down