Skip to content

Commit

Permalink
[InstCombine] Handle scalable splat in `getFlippedStrictnessPredicate…
Browse files Browse the repository at this point in the history
…AndConstant` (#83980)

This patch adds support for canonicalization of icmp with a scalable
splat. Some optimizations assume that `icmp pred X, APInt C` is in
canonical form.

Fixes #83931.
  • Loading branch information
dtcxzyw committed Mar 5, 2024
1 parent d95a0d7 commit d51fcd4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6544,6 +6544,13 @@ InstCombiner::getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred,
if (!SafeReplacementConstant)
SafeReplacementConstant = CI;
}
} else if (isa<VectorType>(C->getType())) {
// Handle scalable splat
Value *SplatC = C->getSplatValue();
auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
// Bail out if the constant can't be safely incremented/decremented.
if (!CI || !ConstantIsOk(CI))
return std::nullopt;
} else {
// ConstantExpr?
return std::nullopt;
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/Transforms/InstCombine/pr83931.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -S -passes=instcombine < %s | FileCheck %s

define <vscale x 2 x i1> @dont_crash(<vscale x 2 x i64> %x) {
; CHECK-LABEL: define <vscale x 2 x i1> @dont_crash(
; CHECK-SAME: <vscale x 2 x i64> [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <vscale x 2 x i64> [[X]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 -309383, i64 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
; CHECK-NEXT: ret <vscale x 2 x i1> [[RET]]
;
entry:
%div = sdiv <vscale x 2 x i64> %x, splat (i64 309383)
%ret = icmp sge <vscale x 2 x i64> %div, zeroinitializer
ret <vscale x 2 x i1> %ret
}
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3423,7 +3423,7 @@ define <vscale x 2 x i32> @scalable_sign_bits(<vscale x 2 x i8> %x) {
define <vscale x 2 x i1> @scalable_non_zero(<vscale x 2 x i32> %x) {
; CHECK-LABEL: @scalable_non_zero(
; CHECK-NEXT: [[A:%.*]] = or <vscale x 2 x i32> [[X:%.*]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 1, i64 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
; CHECK-NEXT: [[CMP:%.*]] = icmp ule <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 56, i64 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 57, i64 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
; CHECK-NEXT: ret <vscale x 2 x i1> [[CMP]]
;
%a = or <vscale x 2 x i32> %x, splat (i32 1)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/vscale_cmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

define <vscale x 2 x i1> @sge(<vscale x 2 x i8> %x) {
; CHECK-LABEL: @sge(
; CHECK-NEXT: [[CMP:%.*]] = icmp sge <vscale x 2 x i8> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <vscale x 2 x i8> [[X:%.*]], shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 -1, i64 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
; CHECK-NEXT: ret <vscale x 2 x i1> [[CMP]]
;
%cmp = icmp sge <vscale x 2 x i8> %x, zeroinitializer
Expand Down

0 comments on commit d51fcd4

Please sign in to comment.