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 @@ -2375,7 +2375,7 @@ // constant-value-based preconditions in the folds below, then we could assert // those conditions rather than checking them. This is difficult because of // undef/poison (PR34838). - if (IsAShr) { + if (IsAShr && Shr->hasOneUse()) { 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) 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 @@ -897,6 +897,19 @@ ret i1 %c } +define i1 @ashrsgt_01_00_multiuse(i4 %x, ptr %p) { +; CHECK-LABEL: @ashrsgt_01_00_multiuse( +; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 +; CHECK-NEXT: [[C:%.*]] = icmp sgt i4 [[S]], 0 +; CHECK-NEXT: store i4 [[S]], ptr [[P:%.*]], align 1 +; CHECK-NEXT: ret i1 [[C]] +; + %s = ashr i4 %x, 1 + %c = icmp sgt i4 %s, 0 + store i4 %s, ptr %p + ret i1 %c +} + define i1 @ashrsgt_01_01(i4 %x) { ; CHECK-LABEL: @ashrsgt_01_01( ; CHECK-NEXT: [[C:%.*]] = icmp sgt i4 [[X:%.*]], 3 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 @@ -576,6 +576,19 @@ ret i1 %r } +define i1 @ashr_ugt_0_multiuse(i4 %x, ptr %p) { +; CHECK-LABEL: @ashr_ugt_0_multiuse( +; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 +; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X]], 1 +; CHECK-NEXT: store i4 [[S]], ptr [[P:%.*]], align 1 +; CHECK-NEXT: ret i1 [[R]] +; + %s = ashr i4 %x, 1 + %r = icmp ugt i4 %s, 0 ; 0b0000 + store i4 %s, ptr %p + ret i1 %r +} + define i1 @ashr_ugt_1(i4 %x) { ; CHECK-LABEL: @ashr_ugt_1( ; CHECK-NEXT: [[R:%.*]] = icmp ugt i4 [[X:%.*]], 3 @@ -764,6 +777,19 @@ ret i1 %r } +define i1 @ashr_ult_2_multiuse(i4 %x, ptr %p) { +; CHECK-LABEL: @ashr_ult_2_multiuse( +; CHECK-NEXT: [[S:%.*]] = ashr i4 [[X:%.*]], 1 +; CHECK-NEXT: [[R:%.*]] = icmp ult i4 [[X]], 4 +; CHECK-NEXT: store i4 [[S]], ptr [[P:%.*]], align 1 +; CHECK-NEXT: ret i1 [[R]] +; + %s = ashr i4 %x, 1 + %r = icmp ult i4 %s, 2 ; 0b0010 + store i4 %s, ptr %p + ret i1 %r +} + ; negative test define i1 @ashr_ult_3(i4 %x) {