Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2028,6 +2028,27 @@ And, Constant::getNullValue(ShType)); } + // Simplify 'shl' inequality test into 'and' equality test. + if (Cmp.isUnsigned() && Shl->hasOneUse()) { + // (X l<< C2) u<=/u> C1 iff C1+1 is power of two -> X & (~C1 l>> C2) ==/!= 0 + if ((C + 1).isPowerOf2() && + (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_UGT)) { + Value *And = Builder.CreateAnd(X, (~C).lshr(ShiftAmt->getZExtValue())); + return new ICmpInst(Pred == ICmpInst::ICMP_ULE ? ICmpInst::ICMP_EQ + : ICmpInst::ICMP_NE, + And, Constant::getNullValue(ShType)); + } + // (X l<< C2) u= C1 iff C1 is power of two -> X & (-C1 l>> C2) ==/!= 0 + if (C.isPowerOf2() && + (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) { + Value *And = + Builder.CreateAnd(X, (~(C - 1)).lshr(ShiftAmt->getZExtValue())); + return new ICmpInst(Pred == ICmpInst::ICMP_ULT ? ICmpInst::ICMP_EQ + : ICmpInst::ICMP_NE, + And, Constant::getNullValue(ShType)); + } + } + // Transform (icmp pred iM (shl iM %v, N), C) // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N)) // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N. Index: llvm/test/Transforms/InstCombine/pr17827.ll =================================================================== --- llvm/test/Transforms/InstCombine/pr17827.ll +++ llvm/test/Transforms/InstCombine/pr17827.ll @@ -66,8 +66,8 @@ ; Unsigned compare allows a transformation to compare against 0. define i1 @test_shift_and_cmp_changed2(i8 %p) { ; CHECK-LABEL: @test_shift_and_cmp_changed2( -; CHECK-NEXT: [[SHLP:%.*]] = shl i8 [[P:%.*]], 5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[SHLP]], 64 +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[P:%.*]], 6 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shlp = shl i8 %p, 5 @@ -78,8 +78,8 @@ define <2 x i1> @test_shift_and_cmp_changed2_vec(<2 x i8> %p) { ; CHECK-LABEL: @test_shift_and_cmp_changed2_vec( -; CHECK-NEXT: [[SHLP:%.*]] = shl <2 x i8> [[P:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> [[SHLP]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[P:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %shlp = shl <2 x i8> %p, Index: llvm/test/Transforms/InstCombine/shl-unsigned-cmp-const.ll =================================================================== --- llvm/test/Transforms/InstCombine/shl-unsigned-cmp-const.ll +++ llvm/test/Transforms/InstCombine/shl-unsigned-cmp-const.ll @@ -9,8 +9,8 @@ ; C2 Shift amount smaller than C1 trailing zeros. define i1 @scalar_i8_shl_ult_const_1(i8 %x) { ; CHECK-LABEL: @scalar_i8_shl_ult_const_1( -; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[SHL]], 64 +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 6 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i8 %x, 5 @@ -45,8 +45,8 @@ ; C2 Shift amount smaller than C1 trailing zeros. define i1 @scalar_i16_shl_ult_const(i16 %x) { ; CHECK-LABEL: @scalar_i16_shl_ult_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i16 [[X:%.*]], 8 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 [[SHL]], 1024 +; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[X:%.*]], 252 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i16 %x, 8 @@ -56,8 +56,8 @@ define i1 @scalar_i32_shl_ult_const(i32 %x) { ; CHECK-LABEL: @scalar_i32_shl_ult_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 11 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[SHL]], 131072 +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 2097088 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i32 %x, 11 @@ -67,8 +67,8 @@ define i1 @scalar_i64_shl_ult_const(i64 %x) { ; CHECK-LABEL: @scalar_i64_shl_ult_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[X:%.*]], 25 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[SHL]], 8589934592 +; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], 549755813632 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i64 %x, 25 @@ -79,8 +79,8 @@ ; Check 'uge' predicate define i1 @scalar_i8_shl_uge_const(i8 %x) { ; CHECK-LABEL: @scalar_i8_shl_uge_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[SHL]], 63 +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 6 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i8 %x, 5 @@ -91,8 +91,8 @@ ; Check 'ule' predicate define i1 @scalar_i8_shl_ule_const(i8 %x) { ; CHECK-LABEL: @scalar_i8_shl_ule_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[SHL]], 64 +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 6 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i8 %x, 5 @@ -103,8 +103,8 @@ ; Check 'ugt' predicate define i1 @scalar_i8_shl_ugt_const(i8 %x) { ; CHECK-LABEL: @scalar_i8_shl_ugt_const( -; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], 5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[SHL]], 63 +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 6 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %shl = shl i8 %x, 5 @@ -116,8 +116,8 @@ define <4 x i1> @vector_4xi32_shl_ult_const(<4 x i32> %x) { ; CHECK-LABEL: @vector_4xi32_shl_ult_const( -; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <4 x i32> [[SHL]], +; CHECK-NEXT: [[TMP1:%.*]] = and <4 x i32> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; %shl = shl <4 x i32> %x, @@ -161,8 +161,8 @@ ; Check 'uge' predicate define <4 x i1> @vector_4xi32_shl_uge_const(<4 x i32> %x) { ; CHECK-LABEL: @vector_4xi32_shl_uge_const( -; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <4 x i32> [[SHL]], +; CHECK-NEXT: [[TMP1:%.*]] = and <4 x i32> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <4 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; %shl = shl <4 x i32> %x, @@ -173,8 +173,8 @@ ; Check 'ule' predicate define <4 x i1> @vector_4xi32_shl_ule_const(<4 x i32> %x) { ; CHECK-LABEL: @vector_4xi32_shl_ule_const( -; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <4 x i32> [[SHL]], +; CHECK-NEXT: [[TMP1:%.*]] = and <4 x i32> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; %shl = shl <4 x i32> %x, @@ -185,8 +185,8 @@ ; Check 'ugt' predicate define <4 x i1> @vector_4xi32_shl_ugt_const(<4 x i32> %x) { ; CHECK-LABEL: @vector_4xi32_shl_ugt_const( -; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <4 x i32> [[SHL]], +; CHECK-NEXT: [[TMP1:%.*]] = and <4 x i32> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <4 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; %shl = shl <4 x i32> %x,