diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -911,6 +911,32 @@ CxtI.getName() + ".simplified"); } +/// Fold: +/// (ctpop(X) == N) || (X != 0) --> X != 0 +/// (ctpop(X) != N) && (X == 0) --> X == 0 +/// where N > 0 +static Value *foldAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1, + bool IsAnd, + InstCombiner::BuilderTy &Builder) { + CmpInst::Predicate Pred0, Pred1; + Value *X; + const APInt *N; + if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic(m_Value(X)), + m_APInt(N))) || + !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_SpecificInt(0))) || + !N->ugt(0)) + return nullptr; + + // (ctpop(X) == N) || (X != 0) --> X != 0 where N > 0 + if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE) + return Builder.CreateICmpNE(X, ConstantInt::getNullValue(X->getType())); + // (ctpop(X) != N) && (X == 0) --> X == 0 where N > 0 + if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ) + return Builder.CreateICmpEQ(X, ConstantInt::getNullValue(X->getType())); + + return nullptr; +} + /// Fold (icmp eq ctpop(X) 1) | (icmp eq X 0) into (icmp ult ctpop(X) 2) and /// fold (icmp ne ctpop(X) 1) & (icmp ne X 0) into (icmp ugt ctpop(X) 1). static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd, @@ -1262,6 +1288,11 @@ if (Value *V = foldIsPowerOf2OrZero(RHS, LHS, /*IsAnd=*/true, Builder)) return V; + if (Value *V = foldAndOrOfICmpsWithCtpop(LHS, RHS, /*IsAnd=*/true, Builder)) + return V; + if (Value *V = foldAndOrOfICmpsWithCtpop(RHS, LHS, /*IsAnd=*/true, Builder)) + return V; + // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false)) return V; @@ -2625,6 +2656,11 @@ if (Value *V = foldIsPowerOf2OrZero(RHS, LHS, /*IsAnd=*/false, Builder)) return V; + if (Value *V = foldAndOrOfICmpsWithCtpop(LHS, RHS, /*IsAnd=*/false, Builder)) + return V; + if (Value *V = foldAndOrOfICmpsWithCtpop(RHS, LHS, /*IsAnd=*/false, Builder)) + return V; + // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true)) return V; diff --git a/llvm/test/Transforms/InstCombine/ispow2.ll b/llvm/test/Transforms/InstCombine/ispow2.ll --- a/llvm/test/Transforms/InstCombine/ispow2.ll +++ b/llvm/test/Transforms/InstCombine/ispow2.ll @@ -735,6 +735,324 @@ ret <2 x i1> %r } +; (ctpop(X) == N) || (X != 0) --> X != 0 +; where N > 0 + +define i1 @is_pow2ornon0_ctpop(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp eq i32 %t0, 10 + %notzero = icmp ne i32 %x, 0 + %r = or i1 %notzero, %cmp + ret i1 %r +} + +define i1 @is_pow2ornon0_ctpop_swap_cmp(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_swap_cmp( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp eq i32 %t0, 20 + %notzero = icmp ne i32 %x, 0 + %r = or i1 %cmp, %notzero + ret i1 %r +} + +define i1 @is_pow2ornon0_ctpop_logical(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_logical( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp eq i32 %t0, 30 + %notzero = icmp ne i32 %x, 0 + %r = select i1 %notzero, i1 true, i1 %cmp + ret i1 %r +} + +define <2 x i1> @is_pow2ornon0_ctpop_commute_vec(<2 x i8> %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_commute_vec( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[TMP1]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp eq <2 x i8> %t0, + %iszero = icmp ne <2 x i8> %x, + %r = or <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + +; Extra uses don't change the fold. + +define i1 @is_pow2ornon0_ctpop_extra_uses(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_extra_uses( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: call void @use(i32 [[T0]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1 +; CHECK-NEXT: call void @use_i1(i1 [[CMP]]) +; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: call void @use_i1(i1 [[NOTZERO]]) +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + call void @use(i32 %t0) + %cmp = icmp eq i32 %t0, 1 + call void @use_i1(i1 %cmp) + %notzero = icmp ne i32 %x, 0 + call void @use_i1(i1 %notzero) + %r = or i1 %notzero, %cmp + ret i1 %r +} + +define i1 @is_pow2ornon0_ctpop_logical_extra_uses(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_logical_extra_uses( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: call void @use(i32 [[T0]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1 +; CHECK-NEXT: call void @use_i1(i1 [[CMP]]) +; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: call void @use_i1(i1 [[NOTZERO]]) +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + call void @use(i32 %t0) + %cmp = icmp eq i32 %t0, 1 + call void @use_i1(i1 %cmp) + %notzero = icmp ne i32 %x, 0 + call void @use_i1(i1 %notzero) + %r = select i1 %notzero, i1 true, i1 %cmp + ret i1 %r +} + +; Negative test - wrong constant. + +define <2 x i1> @is_pow2ornon0_ctpop_commute_vec_wrong_cmp_op1(<2 x i8> %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_commute_vec_wrong_cmp_op1( +; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[T0]], +; CHECK-NEXT: [[ISZERO:%.*]] = icmp ne <2 x i8> [[X]], zeroinitializer +; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp eq <2 x i8> %t0, + %iszero = icmp ne <2 x i8> %x, + %r = or <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + +; Negative test - wrong predicate. + +define i1 @is_pow2ornon0_ctpop_wrong_pred1(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_wrong_pred1( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 1 +; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: [[R:%.*]] = or i1 [[NOTZERO]], [[CMP]] +; CHECK-NEXT: ret i1 [[R]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp ne i32 %t0, 1 + %notzero = icmp ne i32 %x, 0 + %r = or i1 %notzero, %cmp + ret i1 %r +} + +define i1 @is_pow2ornon0_ctpop_wrong_pred2_logical(i32 %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_wrong_pred2_logical( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 2 +; CHECK-NEXT: [[NOTZERO:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: [[R:%.*]] = or i1 [[NOTZERO]], [[CMP]] +; CHECK-NEXT: ret i1 [[R]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp ne i32 %t0, 2 + %notzero = icmp eq i32 %x, 0 + %r = select i1 %notzero, i1 true, i1 %cmp + ret i1 %r +} + +define <2 x i1> @is_pow2ornon0_ctpop_commute_vec_wrong_pred3(<2 x i8> %x) { +; CHECK-LABEL: @is_pow2ornon0_ctpop_commute_vec_wrong_pred3( +; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[T0]], +; CHECK-NEXT: [[ISZERO:%.*]] = icmp ne <2 x i8> [[X]], zeroinitializer +; CHECK-NEXT: [[R:%.*]] = and <2 x i1> [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp eq <2 x i8> %t0, + %iszero = icmp ne <2 x i8> %x, + %r = and <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + +; (ctpop(X) != N) && (X == 0) --> X == 0 +; where N > 0 + +define i1 @isnot_pow2but0_ctpop(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp ne i32 %t0, 1 + %iszero = icmp eq i32 %x, 0 + %r = and i1 %iszero, %cmp + ret i1 %r +} + +define i1 @isnot_pow2but0_ctpop_swap_cmp(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_swap_cmp( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp ne i32 %t0, 1 + %iszero = icmp eq i32 %x, 0 + %r = and i1 %cmp, %iszero + ret i1 %r +} + +define i1 @isnot_pow2but0_ctpop_logical(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_logical( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp ne i32 %t0, 1 + %iszero = icmp eq i32 %x, 0 + %r = select i1 %iszero, i1 %cmp, i1 false + ret i1 %r +} + +define <2 x i1> @isnot_pow2but0_ctpop_commute_vec(<2 x i8> %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_commute_vec( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: ret <2 x i1> [[TMP1]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp ne <2 x i8> %t0, + %iszero = icmp eq <2 x i8> %x, + %r = and <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + +; Extra uses don't change the fold. + +define i1 @isnot_pow2but0_ctpop_extra_uses(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_extra_uses( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: call void @use(i32 [[T0]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 1 +; CHECK-NEXT: call void @use_i1(i1 [[CMP]]) +; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: call void @use_i1(i1 [[ISZERO]]) +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + call void @use(i32 %t0) + %cmp = icmp ne i32 %t0, 1 + call void @use_i1(i1 %cmp) + %iszero = icmp eq i32 %x, 0 + call void @use_i1(i1 %iszero) + %r = and i1 %iszero, %cmp + ret i1 %r +} + +define i1 @isnot_pow2but0_ctpop_logical_extra_uses(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_logical_extra_uses( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: call void @use(i32 [[T0]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 1 +; CHECK-NEXT: call void @use_i1(i1 [[CMP]]) +; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: call void @use_i1(i1 [[ISZERO]]) +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + call void @use(i32 %t0) + %cmp = icmp ne i32 %t0, 1 + call void @use_i1(i1 %cmp) + %iszero = icmp eq i32 %x, 0 + call void @use_i1(i1 %iszero) + %r = select i1 %iszero, i1 %cmp, i1 false + ret i1 %r +} + +; Negative test - wrong constant. + +define <2 x i1> @isnot_pow2but0_ctpop_commute_vec_wrong_cmp_op1(<2 x i8> %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_commute_vec_wrong_cmp_op1( +; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[T0]], +; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq <2 x i8> [[X]], zeroinitializer +; CHECK-NEXT: [[R:%.*]] = and <2 x i1> [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp ne <2 x i8> %t0, + %iszero = icmp eq <2 x i8> %x, + %r = and <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + +; Negative test - wrong predicate. + +define i1 @isnot_pow2but0_ctpop_wrong_pred1(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_wrong_pred1( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1 +; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0 +; CHECK-NEXT: [[R:%.*]] = and i1 [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret i1 [[R]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp eq i32 %t0, 1 + %iszero = icmp eq i32 %x, 0 + %r = and i1 %iszero, %cmp + ret i1 %r +} + +define i1 @isnot_pow2but0_ctpop_wrong_pred2_logical(i32 %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_wrong_pred2_logical( +; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1 +; CHECK-NEXT: [[ISZERO:%.*]] = icmp ne i32 [[X]], 0 +; CHECK-NEXT: [[R:%.*]] = and i1 [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret i1 [[R]] +; + %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) + %cmp = icmp eq i32 %t0, 1 + %iszero = icmp ne i32 %x, 0 + %r = select i1 %iszero, i1 %cmp, i1 false + ret i1 %r +} + +define <2 x i1> @isnot_pow2but0_ctpop_commute_vec_wrong_pred3(<2 x i8> %x) { +; CHECK-LABEL: @isnot_pow2but0_ctpop_commute_vec_wrong_pred3( +; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[T0]], +; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq <2 x i8> [[X]], zeroinitializer +; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[ISZERO]], [[CMP]] +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %t0 = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) + %cmp = icmp ne <2 x i8> %t0, + %iszero = icmp eq <2 x i8> %x, + %r = or <2 x i1> %iszero, %cmp + ret <2 x i1> %r +} + ; (ctpop(X) == 1) || (X == 0) --> ctpop(X) u< 2 define i1 @is_pow2or0_ctpop(i32 %x) { @@ -899,11 +1217,8 @@ define i1 @is_pow2or0_ctpop_wrong_pred2_logical(i32 %x) { ; CHECK-LABEL: @is_pow2or0_ctpop_wrong_pred2_logical( -; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1 -; CHECK-NEXT: [[ISZERO:%.*]] = icmp ne i32 [[X]], 0 -; CHECK-NEXT: [[R:%.*]] = or i1 [[ISZERO]], [[CMP]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] ; %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) %cmp = icmp eq i32 %t0, 1 @@ -1091,11 +1406,8 @@ define i1 @isnot_pow2nor0_ctpop_wrong_pred2_logical(i32 %x) { ; CHECK-LABEL: @isnot_pow2nor0_ctpop_wrong_pred2_logical( -; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]] -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 1 -; CHECK-NEXT: [[NOTZERO:%.*]] = icmp eq i32 [[X]], 0 -; CHECK-NEXT: [[R:%.*]] = and i1 [[NOTZERO]], [[CMP]] -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: ret i1 [[TMP1]] ; %t0 = tail call i32 @llvm.ctpop.i32(i32 %x) %cmp = icmp ne i32 %t0, 1