Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4548,6 +4548,20 @@ } } + // 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 (BO0 && BO0->getOpcode() == Instruction::Shl && + match(BO0->getOperand(1), m_One()) && Op1 == BO0->getOperand(0)) + return new ICmpInst(ICmpInst::getSignedPredicate(Pred), Op1, + Constant::getNullValue(Op1->getType())); + else if (BO1 && BO1->getOpcode() == Instruction::Shl && + match(BO1->getOperand(1), m_One()) && Op0 == BO1->getOperand(0)) + 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,241 @@ +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +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_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ugt_1_vec( +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ugt <2 x i8> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ugt_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_ugt_2( +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ugt i8 %x, %add + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ugt_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ugt_2_vec( +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ugt <2 x i8> %x, %add + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_uge_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_uge_1( +; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X:%.*]], -1 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp uge i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_uge_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_uge_1_vec( +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp uge <2 x i8> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_uge_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_uge_2( +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], 1 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp uge i8 %x, %add + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_uge_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_uge_2_vec( +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp uge <2 x i8> %x, %add + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ult_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_ult_1( +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ult i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ult_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ult_1_vec( +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ult <2 x i8> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ult_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_ult_2( +; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ult i8 %x, %add + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ult_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ult_2_vec( +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ult <2 x i8> %x, %add + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_ule_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_ule_1( +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], 1 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ule i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ule_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ule_1_vec( +; 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 <2 x i1> @icmp_shl_ule_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ule_2_vec( +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %add = shl <2 x i8> %x, + %cmp = icmp ule <2 x i8> %x, %add + ret <2 x 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_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_eq_1_vec( +; 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> %add, %x + ret <2 x i1> %cmp +} + +define i1 @icmp_shl_eq_2(i8 %x) { +; CHECK-LABEL: @icmp_shl_eq_2( +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp eq i8 %x, %add + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_eq_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_eq_2_vec( +; 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 i1 @icmp_shl_ne_1(i8 %x) { +; CHECK-LABEL: @icmp_shl_ne_1( +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %add = shl i8 %x, 1 + %cmp = icmp ne i8 %add, %x + ret i1 %cmp +} + +define <2 x i1> @icmp_shl_ne_1_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ne_1_vec( +; 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 <2 x i1> @icmp_shl_ne_2_vec(<2 x i8> %x) { +; CHECK-LABEL: @icmp_shl_ne_2_vec( +; 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> %x, %add + ret <2 x i1> %cmp +}