From 8453fbf0889e22cf9bbb74c65e36cf8abbcec7b4 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 8 Sep 2020 21:06:59 +0200 Subject: [PATCH] [ValueTracking] Compute known bits of min/max intrinsics Implement known bits for the min/max intrinsics based on the recently added KnownBits primitives. --- llvm/lib/Analysis/ValueTracking.cpp | 20 ++++++++++++++++++++ .../test/Transforms/InstCombine/minmax-intrinsics.ll | 20 ++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 6e5a719..5eb66e9 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -1739,6 +1739,26 @@ static void computeKnownBitsFromOperator(const Operator *I, } break; } + case Intrinsic::umin: + computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); + computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); + Known = KnownBits::umin(Known, Known2); + break; + case Intrinsic::umax: + computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); + computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); + Known = KnownBits::umax(Known, Known2); + break; + case Intrinsic::smin: + computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); + computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); + Known = KnownBits::smin(Known, Known2); + break; + case Intrinsic::smax: + computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); + computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); + Known = KnownBits::smax(Known, Known2); + break; case Intrinsic::x86_sse42_crc32_64_64: Known.Zero.setBitsFrom(32); break; diff --git a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll index d808d5f..797f85d 100644 --- a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll +++ b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll @@ -8,10 +8,7 @@ declare i8 @llvm.smax.i8(i8, i8) define i8 @umin_known_bits(i8 %x, i8 %y) { ; CHECK-LABEL: @umin_known_bits( -; CHECK-NEXT: [[X2:%.*]] = and i8 [[X:%.*]], 127 -; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[X2]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = and i8 [[M]], -128 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %x2 = and i8 %x, 127 %m = call i8 @llvm.umin.i8(i8 %x2, i8 %y) @@ -21,10 +18,7 @@ define i8 @umin_known_bits(i8 %x, i8 %y) { define i8 @umax_known_bits(i8 %x, i8 %y) { ; CHECK-LABEL: @umax_known_bits( -; CHECK-NEXT: [[X2:%.*]] = or i8 [[X:%.*]], -128 -; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[X2]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = and i8 [[M]], -128 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 -128 ; %x2 = or i8 %x, -128 %m = call i8 @llvm.umax.i8(i8 %x2, i8 %y) @@ -34,10 +28,7 @@ define i8 @umax_known_bits(i8 %x, i8 %y) { define i8 @smin_known_bits(i8 %x, i8 %y) { ; CHECK-LABEL: @smin_known_bits( -; CHECK-NEXT: [[X2:%.*]] = or i8 [[X:%.*]], -128 -; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[X2]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = and i8 [[M]], -128 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 -128 ; %x2 = or i8 %x, -128 %m = call i8 @llvm.smin.i8(i8 %x2, i8 %y) @@ -47,10 +38,7 @@ define i8 @smin_known_bits(i8 %x, i8 %y) { define i8 @smax_known_bits(i8 %x, i8 %y) { ; CHECK-LABEL: @smax_known_bits( -; CHECK-NEXT: [[X2:%.*]] = and i8 [[X:%.*]], 127 -; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[X2]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = and i8 [[M]], -128 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %x2 = and i8 %x, 127 %m = call i8 @llvm.smax.i8(i8 %x2, i8 %y) -- 2.7.4