Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4548,6 +4548,18 @@ } } + // For unsigned predicates / eq / ne: + // icmp pred (x << 1), x --> icmp getSignedPredicate(pred) x, 0 + // icmp pred x, (x << 1) --> icmp getSignedPredicate(pred) 0, x + if (!ICmpInst::isSigned(Pred)) { + if (match(Op0, m_Shl(m_Specific(Op1), m_One()))) + return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1, + Constant::getNullValue(Op1->getType())); + else if (match(Op1, m_Shl(m_Specific(Op0), m_One()))) + return new ICmpInst(ICmpInst::getSignedPredicate(Pred), + Constant::getNullValue(Op0->getType()), Op0); + } + if (Value *V = foldMultiplicationOverflowCheck(I)) return replaceInstUsesWith(I, V); Index: llvm/test/Transforms/InstCombine/icmp-shl-1-overflow.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/icmp-shl-1-overflow.ll @@ -0,0 +1,163 @@ +; RUN: opt < %s -passes=instcombine -S -debug | FileCheck %s + +declare void @use16(i16) +declare void @usev4(<4 x i4>) + +define i1 @icmp_shl_ugt_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_ugt_1( +; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ugt i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ugt_2(<2 x i32> %x) { +; CHECK-LABEL: @icmp_shl_ugt_2( +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i32> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i32> %x, + %cmp = icmp ugt <2 x i32> %x, %add + ret <2 x i1> %cmp +} + +define <3 x i1> @icmp_shl_uge_1(<3 x i7> %x) { +; CHECK-LABEL: @icmp_shl_uge_1( +; CHECK-NEXT: [[R:%.*]] = icmp sgt <3 x i7> [[X:%.*]], +; CHECK-NEXT: ret <3 x i1> [[R]] +; + %add = shl <3 x i7> %x, + %cmp = icmp uge <3 x i7> %add, %x + ret <3 x i1> %cmp +} + +define i1 @icmp_shl_uge_2(i5 %x) { +; CHECK-LABEL: @icmp_shl_uge_2( +; CHECK-NEXT: [[R:%.*]] = icmp slt i5 [[X:%.*]], 1 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i5 %x, 1 + %cmp = icmp uge i5 %x, %add + ret i1 %cmp +} + +define i1 @icmp_shl_ult_1(i16 %x) { +; CHECK-LABEL: @icmp_shl_ult_1( +; CHECK-NEXT: [[A:%.*]] = shl i16 [[X:%.*]], 1 +; CHECK-NEXT: call void @use16(i16 [[A]]) +; CHECK-NEXT: [[R:%.*]] = icmp slt i16 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i16 %x, 1 + call void @use16(i16 %add) ; extra use of shl + %cmp = icmp ult i16 %add, %x + ret i1 %cmp +} + +define <4 x i1> @icmp_shl_ult_2(<4 x i4> %x) { +; CHECK-LABEL: @icmp_shl_ult_2( +; CHECK-NEXT: [[A:%.*]] = shl <4 x i4> [[X:%.*]], +; CHECK-NEXT: call void @usev4(<4 x i4> [[A]]) +; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i4> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <4 x i1> [[R]] +; + %add = shl <4 x i4> %x, + call void @usev4(<4 x i4> %add) ; extra use of shl + %cmp = icmp ult <4 x i4> %x, %add + ret <4 x i1> %cmp +} + +define <2 x i1> @icmp_shl_ule_1(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ule_1( +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ule <2 x i8> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ule_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_ule_2( +; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X:%.*]], -1 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ule i8 %x, %add + ret i1 %cmp +} + +define i1 @icmp_shl_eq_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_eq_1( +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp eq i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_eq_2(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_eq_2( +; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp eq <2 x i8> %x, %add + ret <2 x i1> %cmp +} + +define <2 x i1> @icmp_shl_ne_1(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ne_1( +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ne <2 x i8> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ne_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_ne_2( +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ne i8 %x, %add + ret i1 %cmp +} + +define i1 @negative_test_signed_pred(i8 %x) { +; CHECK-LABEL: @negative_test_signed_pred( +; CHECK-NEXT: [[A:%.*]] = shl i8 [[X:%.*]], 1 +; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[A:%.*]], [[X:%.*]] +; CHECK-NEXT: ret i1 [[C]] +; + %add = shl i8 %x, 1 + %cmp = icmp slt i8 %add, %x + ret i1 %cmp +} + +define i1 @negative_test_shl_more_than_1(i16 %x) { +; CHECK-LABEL: @negative_test_shl_more_than_1( +; CHECK-NEXT: [[A:%.*]] = shl i16 [[X:%.*]], 2 +; CHECK-NEXT: [[C:%.*]] = icmp ult i16 [[A:%.*]], [[X:%.*]] +; CHECK-NEXT: ret i1 [[C]] +; + %add = shl i16 %x, 2 + %cmp = icmp ult i16 %add, %x + ret i1 %cmp +} + +define i1 @negative_test_compare_with_different_value(i16 %x, i16 %y) { +; CHECK-LABEL: @negative_test_compare_with_different_value( +; CHECK-NEXT: [[A:%.*]] = shl i16 [[X:%.*]], 1 +; CHECK-NEXT: [[C:%.*]] = icmp ult i16 [[A:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[C]] +; + %add = shl i16 %x, 1 + %cmp = icmp ult i16 %add, %y + ret i1 %cmp +}