diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2239,7 +2239,7 @@ // those conditions rather than checking them. This is difficult because of // undef/poison (PR34838). if (IsAShr) { - if (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT || IsExact) { + if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) { // When ShAmtC can be shifted losslessly: // icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC) // icmp slt/ult (ashr X, ShAmtC), C --> icmp slt/ult X, (C << ShAmtC) @@ -2254,6 +2254,12 @@ (ShiftedC + 1).ashr(ShAmtVal) == (C + 1)) return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); } + if (Pred == CmpInst::ICMP_UGT) { + // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1 + APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1; + if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1)) + return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC)); + } // If the compare constant has significant bits above the lowest sign-bit, // then convert an unsigned cmp to a test of the sign-bit: diff --git a/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll b/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll --- a/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll +++ b/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll @@ -2344,8 +2344,7 @@ define i1 @ashr_ugt_noexact(i8 %x) { ; CHECK-LABEL: @ashr_ugt_noexact( -; CHECK-NEXT: [[S:%.*]] = ashr i8 [[X:%.*]], 3 -; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[S]], 10 +; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[X:%.*]], 87 ; CHECK-NEXT: ret i1 [[C]] ; %s = ashr i8 %x, 3 @@ -2356,8 +2355,7 @@ define i1 @ashr_uge_noexact(i8 %x) { ; CHECK-LABEL: @ashr_uge_noexact( -; CHECK-NEXT: [[S:%.*]] = ashr i8 [[X:%.*]], 3 -; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[S]], 9 +; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[X:%.*]], 79 ; CHECK-NEXT: ret i1 [[C]] ; %s = ashr i8 %x, 3 @@ -2451,6 +2449,15 @@ ret <4 x i1> %c } +define i1 @ashr_sgt_overflow(i8 %x) { +; CHECK-LABEL: @ashr_sgt_overflow( +; CHECK-NEXT: ret i1 false +; + %s = ashr i8 %x, 1 + %c = icmp sgt i8 %s, 63 + ret i1 %c +} + define i1 @lshrult_01_00_exact(i4 %x) { ; CHECK-LABEL: @lshrult_01_00_exact( ; CHECK-NEXT: ret i1 false diff --git a/llvm/test/Transforms/InstCombine/icmp-shr.ll b/llvm/test/Transforms/InstCombine/icmp-shr.ll --- a/llvm/test/Transforms/InstCombine/icmp-shr.ll +++ b/llvm/test/Transforms/InstCombine/icmp-shr.ll @@ -574,12 +574,9 @@ ret i1 %r } -; negative test - define i1 @ashr_ugt_1(i4 %x) { ; CHECK-LABEL: @ashr_ugt_1( -; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 -; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[S]], 1 +; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X:%.*]], 3 ; CHECK-NEXT: ret i1 [[R]] ; %s = ashr i4 %x, 1 @@ -587,12 +584,9 @@ ret i1 %r } -; negative test - define i1 @ashr_ugt_2(i4 %x) { ; CHECK-LABEL: @ashr_ugt_2( -; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 -; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[S]], 2 +; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X:%.*]], 5 ; CHECK-NEXT: ret i1 [[R]] ; %s = ashr i4 %x, 1 @@ -694,12 +688,9 @@ ret i1 %r } -; negative test - define i1 @ashr_ugt_12(i4 %x) { ; CHECK-LABEL: @ashr_ugt_12( -; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 -; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[S]], -4 +; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X:%.*]], -7 ; CHECK-NEXT: ret i1 [[R]] ; %s = ashr i4 %x, 1 @@ -707,12 +698,9 @@ ret i1 %r } -; negative test - define i1 @ashr_ugt_13(i4 %x) { ; CHECK-LABEL: @ashr_ugt_13( -; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 -; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[S]], -3 +; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X:%.*]], -5 ; CHECK-NEXT: ret i1 [[R]] ; %s = ashr i4 %x, 1