Skip to content

Commit e2040d3

Browse files
committed
[ValueTracking] Support min/max intrinsics in computeConstantRange()
The implementation is the same as for the SPF_* case.
1 parent c1abd47 commit e2040d3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6414,6 +6414,33 @@ static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
64146414
}
64156415
}
64166416
break;
6417+
case Intrinsic::umin:
6418+
case Intrinsic::umax:
6419+
case Intrinsic::smin:
6420+
case Intrinsic::smax:
6421+
if (!match(II.getOperand(0), m_APInt(C)) &&
6422+
!match(II.getOperand(1), m_APInt(C)))
6423+
break;
6424+
6425+
switch (II.getIntrinsicID()) {
6426+
case Intrinsic::umin:
6427+
Upper = *C + 1;
6428+
break;
6429+
case Intrinsic::umax:
6430+
Lower = *C;
6431+
break;
6432+
case Intrinsic::smin:
6433+
Lower = APInt::getSignedMinValue(Width);
6434+
Upper = *C + 1;
6435+
break;
6436+
case Intrinsic::smax:
6437+
Lower = *C;
6438+
Upper = APInt::getSignedMaxValue(Width) + 1;
6439+
break;
6440+
default:
6441+
llvm_unreachable("Must be min/max intrinsic");
6442+
}
6443+
break;
64176444
default:
64186445
break;
64196446
}

llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,9 +1856,7 @@ define i8 @umin_umin_umin(i8 %x, i8 %y) {
18561856

18571857
define i1 @umin_ult_diff_const(i8 %x) {
18581858
; CHECK-LABEL: @umin_ult_diff_const(
1859-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 10)
1860-
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[M]], 20
1861-
; CHECK-NEXT: ret i1 [[C]]
1859+
; CHECK-NEXT: ret i1 true
18621860
;
18631861
%m = call i8 @llvm.umin.i8(i8 %x, i8 10)
18641862
%c = icmp ult i8 %m, 20
@@ -1867,9 +1865,7 @@ define i1 @umin_ult_diff_const(i8 %x) {
18671865

18681866
define i1 @umax_ugt_diff_const(i8 %x) {
18691867
; CHECK-LABEL: @umax_ugt_diff_const(
1870-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 10)
1871-
; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[M]], 5
1872-
; CHECK-NEXT: ret i1 [[C]]
1868+
; CHECK-NEXT: ret i1 true
18731869
;
18741870
%m = call i8 @llvm.umax.i8(i8 %x, i8 10)
18751871
%c = icmp ugt i8 %m, 5
@@ -1878,9 +1874,7 @@ define i1 @umax_ugt_diff_const(i8 %x) {
18781874

18791875
define i1 @smin_slt_diff_const(i8 %x) {
18801876
; CHECK-LABEL: @smin_slt_diff_const(
1881-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 10)
1882-
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[M]], 20
1883-
; CHECK-NEXT: ret i1 [[C]]
1877+
; CHECK-NEXT: ret i1 true
18841878
;
18851879
%m = call i8 @llvm.smin.i8(i8 %x, i8 10)
18861880
%c = icmp slt i8 %m, 20
@@ -1889,9 +1883,7 @@ define i1 @smin_slt_diff_const(i8 %x) {
18891883

18901884
define i1 @smax_sgt_diff_const(i8 %x) {
18911885
; CHECK-LABEL: @smax_sgt_diff_const(
1892-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 10)
1893-
; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[M]], 5
1894-
; CHECK-NEXT: ret i1 [[C]]
1886+
; CHECK-NEXT: ret i1 true
18951887
;
18961888
%m = call i8 @llvm.smax.i8(i8 %x, i8 10)
18971889
%c = icmp sgt i8 %m, 5

0 commit comments

Comments
 (0)