Skip to content

Commit

Permalink
[LVI][CVP] Add support for vector comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 5, 2024
1 parent f92bfca commit 130f0f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
6 changes: 1 addition & 5 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,12 +1784,8 @@ static Constant *getPredicateResult(CmpInst::Predicate Pred, Constant *C,

Type *ResTy = CmpInst::makeCmpResultType(C->getType());
if (Val.isConstantRange()) {
ConstantInt *CI = dyn_cast<ConstantInt>(C);
if (!CI)
return nullptr;

const ConstantRange &CR = Val.getConstantRange();
ConstantRange RHS(CI->getValue());
ConstantRange RHS = C->toConstantRange();
if (CR.icmp(Pred, RHS))
return ConstantInt::getTrue(ResTy);
if (CR.icmp(CmpInst::getInversePredicate(Pred), RHS))
Expand Down
32 changes: 26 additions & 6 deletions llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=correlated-propagation < %s | FileCheck %s

; TODO: Add support for this.
define <2 x i1> @cmp1(<2 x i8> %a) {
; CHECK-LABEL: define <2 x i1> @cmp1(
; CHECK-SAME: <2 x i8> [[A:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i8> [[A]], <i8 1, i8 1>
; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[ADD]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CMP]]
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
;
%add = add nuw <2 x i8> %a, splat (i8 1)
%cmp = icmp ne <2 x i8> %add, zeroinitializer
ret <2 x i1> %cmp
}

; TODO: Add support for this.
define <2 x i1> @cmp2(<2 x i8> %a) {
; CHECK-LABEL: define <2 x i1> @cmp2(
; CHECK-SAME: <2 x i8> [[A:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i8> [[A]], <i8 5, i8 5>
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i8> [[ADD]], <i8 2, i8 2>
; CHECK-NEXT: ret <2 x i1> [[CMP]]
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
;
%add = add nuw <2 x i8> %a, splat (i8 5)
%cmp = icmp ugt <2 x i8> %add, splat (i8 2)
ret <2 x i1> %cmp
}

define <2 x i1> @cmp_nonsplat(<2 x i8> %a) {
; CHECK-LABEL: define <2 x i1> @cmp_nonsplat(
; CHECK-SAME: <2 x i8> [[A:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i8> [[A]], <i8 4, i8 5>
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
;
%add = add nuw <2 x i8> %a, <i8 4, i8 5>
%cmp = icmp ugt <2 x i8> %add, <i8 2, i8 3>
ret <2 x i1> %cmp
}

; Handling this would require keeping track of ranges on a per-element basis.
define <2 x i1> @cmp_nonsplat_fail(<2 x i8> %a) {
; CHECK-LABEL: define <2 x i1> @cmp_nonsplat_fail(
; CHECK-SAME: <2 x i8> [[A:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i8> [[A]], <i8 3, i8 4>
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i8> [[ADD]], <i8 2, i8 3>
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%add = add nuw <2 x i8> %a, <i8 3, i8 4>
%cmp = icmp ugt <2 x i8> %add, <i8 2, i8 3>
ret <2 x i1> %cmp
}

define <2 x i1> @cmp_signedness(<2 x i8> %a) {
; CHECK-LABEL: define <2 x i1> @cmp_signedness(
; CHECK-SAME: <2 x i8> [[A:%.*]]) {
Expand Down

0 comments on commit 130f0f5

Please sign in to comment.