Skip to content

Commit

Permalink
[ValueTracking] Support min/max intrinsics in computeConstantRange()
Browse files Browse the repository at this point in the history
The implementation is the same as for the SPF_* case.
  • Loading branch information
nikic committed Aug 12, 2020
1 parent c1abd47 commit e2040d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
27 changes: 27 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -6414,6 +6414,33 @@ static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
}
}
break;
case Intrinsic::umin:
case Intrinsic::umax:
case Intrinsic::smin:
case Intrinsic::smax:
if (!match(II.getOperand(0), m_APInt(C)) &&
!match(II.getOperand(1), m_APInt(C)))
break;

switch (II.getIntrinsicID()) {
case Intrinsic::umin:
Upper = *C + 1;
break;
case Intrinsic::umax:
Lower = *C;
break;
case Intrinsic::smin:
Lower = APInt::getSignedMinValue(Width);
Upper = *C + 1;
break;
case Intrinsic::smax:
Lower = *C;
Upper = APInt::getSignedMaxValue(Width) + 1;
break;
default:
llvm_unreachable("Must be min/max intrinsic");
}
break;
default:
break;
}
Expand Down
16 changes: 4 additions & 12 deletions llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
Expand Up @@ -1856,9 +1856,7 @@ define i8 @umin_umin_umin(i8 %x, i8 %y) {

define i1 @umin_ult_diff_const(i8 %x) {
; CHECK-LABEL: @umin_ult_diff_const(
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 10)
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[M]], 20
; CHECK-NEXT: ret i1 [[C]]
; CHECK-NEXT: ret i1 true
;
%m = call i8 @llvm.umin.i8(i8 %x, i8 10)
%c = icmp ult i8 %m, 20
Expand All @@ -1867,9 +1865,7 @@ define i1 @umin_ult_diff_const(i8 %x) {

define i1 @umax_ugt_diff_const(i8 %x) {
; CHECK-LABEL: @umax_ugt_diff_const(
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 10)
; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[M]], 5
; CHECK-NEXT: ret i1 [[C]]
; CHECK-NEXT: ret i1 true
;
%m = call i8 @llvm.umax.i8(i8 %x, i8 10)
%c = icmp ugt i8 %m, 5
Expand All @@ -1878,9 +1874,7 @@ define i1 @umax_ugt_diff_const(i8 %x) {

define i1 @smin_slt_diff_const(i8 %x) {
; CHECK-LABEL: @smin_slt_diff_const(
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 10)
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[M]], 20
; CHECK-NEXT: ret i1 [[C]]
; CHECK-NEXT: ret i1 true
;
%m = call i8 @llvm.smin.i8(i8 %x, i8 10)
%c = icmp slt i8 %m, 20
Expand All @@ -1889,9 +1883,7 @@ define i1 @smin_slt_diff_const(i8 %x) {

define i1 @smax_sgt_diff_const(i8 %x) {
; CHECK-LABEL: @smax_sgt_diff_const(
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 10)
; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[M]], 5
; CHECK-NEXT: ret i1 [[C]]
; CHECK-NEXT: ret i1 true
;
%m = call i8 @llvm.smax.i8(i8 %x, i8 10)
%c = icmp sgt i8 %m, 5
Expand Down

0 comments on commit e2040d3

Please sign in to comment.