hkmatsumoto updated this revision to Diff 418405.
hkmatsumoto added a comment.
Reflect code reviews
- Move all added tests to ispow2.ll from icmp-or.ll since now that they
contain tests for "and" operand
- Add tests to confirm extra uses don't affect the optimization
- Add negative tests whose order of the op/and operand being swapped
(named *_swap_cmp)
- Add negative tests for wrong predicates
- Test various wrong constants
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D122077/new/
https://reviews.llvm.org/D122077
Files:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/ispow2.ll
Index: llvm/test/Transforms/InstCombine/ispow2.ll
===================================================================
--- llvm/test/Transforms/InstCombine/ispow2.ll
+++ llvm/test/Transforms/InstCombine/ispow2.ll
@@ -535,38 +535,6 @@
ret i1 %r
}
-; Negative test - wrong predicate (but this could reduce).
-
-define i1 @isnot_pow2_ctpop_wrong_pred1(i32 %x) {
-; CHECK-LABEL: @isnot_pow2_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:%.*]] = or 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 = or i1 %iszero, %cmp
- ret i1 %r
-}
-
-define i1 @isnot_pow2_ctpop_wrong_pred1_logical(i32 %x) {
-; CHECK-LABEL: @isnot_pow2_ctpop_wrong_pred1_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 eq i32 [[X]], 0
-; CHECK-NEXT: [[R:%.*]] = or 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 = select i1 %iszero, i1 true, i1 %cmp
- ret i1 %r
-}
-
; Negative test - wrong predicate.
define i1 @isnot_pow2_ctpop_wrong_pred2(i32 %x) {
@@ -766,3 +734,387 @@
%r = or <2 x i1> %cmp, %iszero
ret <2 x i1> %r
}
+
+; (ctpop(X) == 1) || (X == 0) --> ctpop(X) u< 2
+
+define i1 @is_pow2or0_ctpop(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[T0]], 2
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 1
+ %iszero = icmp eq i32 %x, 0
+ %r = or i1 %iszero, %cmp
+ ret i1 %r
+}
+
+define i1 @is_pow2or0_ctpop_swap_cmp(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop_swap_cmp(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[T0]], 2
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 1
+ %iszero = icmp eq i32 %x, 0
+ %r = or i1 %cmp, %iszero
+ ret i1 %r
+}
+
+define i1 @is_pow2or0_ctpop_logical(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop_logical(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[T0]], 2
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 1
+ %iszero = icmp eq i32 %x, 0
+ %r = select i1 %iszero, i1 true, i1 %cmp
+ ret i1 %r
+}
+
+define <2 x i1> @is_pow2or0_ctpop_commute_vec(<2 x i8> %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop_commute_vec(
+; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i8> [[T0]], <i8 2, i8 2>
+; 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, <i8 1, i8 1>
+ %iszero = icmp eq <2 x i8> %x, <i8 0, i8 0>
+ %r = or <2 x i1> %iszero, %cmp
+ ret <2 x i1> %r
+}
+
+; Extra uses don't change the fold.
+
+define i1 @is_pow2or0_ctpop_extra_uses(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_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: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT: call void @use_i1(i1 [[ISZERO]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[T0]], 2
+; 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)
+ %iszero = icmp eq i32 %x, 0
+ call void @use_i1(i1 %iszero)
+ %r = or i1 %iszero, %cmp
+ ret i1 %r
+}
+
+define i1 @is_pow2or0_ctpop_logical_extra_uses(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_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: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT: call void @use_i1(i1 [[ISZERO]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[T0]], 2
+; 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)
+ %iszero = icmp eq i32 %x, 0
+ call void @use_i1(i1 %iszero)
+ %r = select i1 %iszero, i1 true, i1 %cmp
+ ret i1 %r
+}
+
+; Negative test - wrong constant.
+
+define i1 @is_pow2or0_ctpop_wrong_cmp_op1(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop_wrong_cmp_op1(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 2
+; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = or i1 [[ISZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 2
+ %iszero = icmp eq i32 %x, 0
+ %r = or i1 %iszero, %cmp
+ ret i1 %r
+}
+
+define i1 @is_pow2or0_ctpop_wrong_cmp_op1_logical(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_ctpop_wrong_cmp_op1_logical(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 3
+; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = or i1 [[ISZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 3
+ %iszero = icmp eq i32 %x, 0
+ %r = select i1 %iszero, i1 true, i1 %cmp
+ ret i1 %r
+}
+
+define <2 x i1> @is_pow2or0_ctpop_commute_vec_wrong_cmp_op1(<2 x i8> %x) {
+; CHECK-LABEL: @is_pow2or0_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]], <i8 -1, i8 1>
+; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[CMP]], [[ISZERO]]
+; 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, <i8 -1, i8 1>
+ %iszero = icmp eq <2 x i8> %x, <i8 0, i8 0>
+ %r = or <2 x i1> %cmp, %iszero
+ ret <2 x i1> %r
+}
+
+; Negative test - wrong predicate.
+
+define i1 @is_pow2or0_ctpop_wrong_pred1(i32 %x) {
+; CHECK-LABEL: @is_pow2or0_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: [[ISZERO:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = or i1 [[ISZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 1
+ %iszero = icmp eq i32 %x, 0
+ %r = or i1 %iszero, %cmp
+ ret i1 %r
+}
+
+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]]
+;
+ %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 true, i1 %cmp
+ ret i1 %r
+}
+
+define <2 x i1> @is_pow2or0_ctpop_commute_vec_wrong_pred3(<2 x i8> %x) {
+; CHECK-LABEL: @is_pow2or0_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]], <i8 1, i8 1>
+; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = and <2 x i1> [[CMP]], [[ISZERO]]
+; 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, <i8 1, i8 1>
+ %iszero = icmp eq <2 x i8> %x, <i8 0, i8 0>
+ %r = and <2 x i1> %cmp, %iszero
+ ret <2 x i1> %r
+}
+
+; (ctpop(X) != 1) && (X != 0) --> ctpop(X) u> 1
+
+define i1 @isnot_pow2nor0_ctpop(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[T0]], 1
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 1
+ %notzero = icmp ne i32 %x, 0
+ %r = and i1 %notzero, %cmp
+ ret i1 %r
+}
+
+define i1 @isnot_pow2nor0_ctpop_swap_cmp(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop_swap_cmp(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[T0]], 1
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 1
+ %notzero = icmp ne i32 %x, 0
+ %r = and i1 %cmp, %notzero
+ ret i1 %r
+}
+
+define i1 @isnot_pow2nor0_ctpop_logical(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop_logical(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[T0]], 1
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 1
+ %notzero = icmp ne i32 %x, 0
+ %r = select i1 %notzero, i1 %cmp, i1 false
+ ret i1 %r
+}
+
+define <2 x i1> @isnot_pow2nor0_ctpop_commute_vec(<2 x i8> %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop_commute_vec(
+; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <2 x i8> [[T0]], <i8 1, i8 1>
+; 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, <i8 1, i8 1>
+ %notzero = icmp ne <2 x i8> %x, <i8 0, i8 0>
+ %r = and <2 x i1> %notzero, %cmp
+ ret <2 x i1> %r
+}
+
+; Extra uses don't change the fold.
+
+define i1 @isnot_pow2nor0_ctpop_extra_uses(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_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: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0
+; CHECK-NEXT: call void @use_i1(i1 [[NOTZERO]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[T0]], 1
+; 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)
+ %notzero = icmp ne i32 %x, 0
+ call void @use_i1(i1 %notzero)
+ %r = and i1 %notzero, %cmp
+ ret i1 %r
+}
+
+define i1 @isnot_pow2nor0_ctpop_logical_extra_uses(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_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: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0
+; CHECK-NEXT: call void @use_i1(i1 [[NOTZERO]])
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[T0]], 1
+; 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)
+ %notzero = icmp ne i32 %x, 0
+ call void @use_i1(i1 %notzero)
+ %r = select i1 %notzero, i1 %cmp, i1 false
+ ret i1 %r
+}
+
+; Negative test - wrong constant.
+
+define i1 @isnot_pow2nor0_ctpop_wrong_cmp_op1(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop_wrong_cmp_op1(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 4
+; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOTZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 4
+ %notzero = icmp ne i32 %x, 0
+ %r = and i1 %notzero, %cmp
+ ret i1 %r
+}
+
+define i1 @isnot_pow2nor0_ctpop_wrong_cmp_op1_logical(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_ctpop_wrong_cmp_op1_logical(
+; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 5
+; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOTZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 5
+ %notzero = icmp ne i32 %x, 0
+ %r = select i1 %notzero, i1 %cmp, i1 false
+ ret i1 %r
+}
+
+define <2 x i1> @isnot_pow2nor0_ctpop_commute_vec_wrong_cmp_op1(<2 x i8> %x) {
+; CHECK-LABEL: @isnot_pow2nor0_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]], <i8 0, i8 -1>
+; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = and <2 x i1> [[CMP]], [[NOTZERO]]
+; 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, <i8 0, i8 -1>
+ %notzero = icmp ne <2 x i8> %x, <i8 0, i8 0>
+ %r = and <2 x i1> %cmp, %notzero
+ ret <2 x i1> %r
+}
+
+; Negative test - wrong predicate.
+
+define i1 @isnot_pow2nor0_ctpop_wrong_pred1(i32 %x) {
+; CHECK-LABEL: @isnot_pow2nor0_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: [[NOTZERO:%.*]] = icmp ne i32 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOTZERO]], [[CMP]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp eq i32 %t0, 1
+ %notzero = icmp ne i32 %x, 0
+ %r = and i1 %notzero, %cmp
+ ret i1 %r
+}
+
+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]]
+;
+ %t0 = tail call i32 @llvm.ctpop.i32(i32 %x)
+ %cmp = icmp ne i32 %t0, 1
+ %notzero = icmp eq i32 %x, 0
+ %r = select i1 %notzero, i1 %cmp, i1 false
+ ret i1 %r
+}
+
+define <2 x i1> @isnot_pow2nor0_wrong_pred3_ctpop_commute_vec(<2 x i8> %x) {
+; CHECK-LABEL: @isnot_pow2nor0_wrong_pred3_ctpop_commute_vec(
+; CHECK-NEXT: [[T0:%.*]] = tail call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> [[X:%.*]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[T0]], <i8 1, i8 1>
+; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[CMP]], [[NOTZERO]]
+; 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, <i8 1, i8 1>
+ %notzero = icmp ne <2 x i8> %x, <i8 0, i8 0>
+ %r = or <2 x i1> %cmp, %notzero
+ ret <2 x i1> %r
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -911,6 +911,26 @@
CxtI.getName() + ".simplified");
}
+/// 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 uge ctpop(X) 2).
+static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
+ InstCombiner::BuilderTy &Builder) {
+ CmpInst::Predicate Pred0, Pred1;
+ Value *X;
+ if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
+ m_SpecificInt(1))) ||
+ !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())))
+ return nullptr;
+
+ Value *CtPop = Cmp0->getOperand(0);
+ if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_NE)
+ return Builder.CreateICmpUGE(CtPop, ConstantInt::get(CtPop->getType(), 2));
+ if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_EQ)
+ return Builder.CreateICmpULT(CtPop, ConstantInt::get(CtPop->getType(), 2));
+
+ return nullptr;
+}
+
/// Reduce a pair of compares that check if a value has exactly 1 bit set.
static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
InstCombiner::BuilderTy &Builder) {
@@ -1237,6 +1257,11 @@
if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, And, Builder, Q))
return V;
+ if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, /*IsAnd=*/true, Builder))
+ return V;
+ if (Value *V = foldIsPowerOf2OrZero(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;
@@ -2595,6 +2620,11 @@
if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, Or, Builder, Q))
return V;
+ if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, /*IsAnd=*/false, Builder))
+ return V;
+ if (Value *V = foldIsPowerOf2OrZero(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;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits