aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd21
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-10.c26
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-11.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-12.c17
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-13.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-14.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-15.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-16.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-17.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-18.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-19.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-2.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-20.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-21.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-22.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-23.c16
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-3.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-4.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-5.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-6.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-7.c24
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-8.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask-9.c25
-rw-r--r--gcc/testsuite/gcc.dg/bic-bitmask.h43
-rw-r--r--gcc/testsuite/gcc.target/aarch64/bic-bitmask-1.c13
-rw-r--r--gcc/tree.c53
-rw-r--r--gcc/tree.h5
27 files changed, 658 insertions, 1 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 60b4ad5..3e54e2cf 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -38,7 +38,8 @@ along with GCC; see the file COPYING3. If not see
uniform_integer_cst_p
HONOR_NANS
uniform_vector_p
- expand_vec_cmp_expr_p)
+ expand_vec_cmp_expr_p
+ bitmask_inv_cst_vector_p)
/* Operator lists. */
(define_operator_list tcc_comparison
@@ -5207,6 +5208,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
{ build_zero_cst (ty); }))))))
+/* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
+ where ~Y + 1 == pow2 and Z = ~Y. */
+(for cst (VECTOR_CST INTEGER_CST)
+ (for cmp (le eq ne ge gt)
+ icmp (le le gt le gt)
+ (simplify
+ (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
+ (with { tree csts = bitmask_inv_cst_vector_p (@1); }
+ (switch
+ (if (csts && TYPE_UNSIGNED (TREE_TYPE (@1))
+ && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
+ (icmp @0 { csts; }))
+ (if (csts && !TYPE_UNSIGNED (TREE_TYPE (@1))
+ && (cmp == EQ_EXPR || cmp == NE_EXPR)
+ && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
+ (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
+ (icmp (convert:utype @0) { csts; }))))))))
+
/* -A CMP -B -> B CMP A. */
(for cmp (tcc_comparison)
scmp (swapped_tcc_comparison)
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-10.c b/gcc/testsuite/gcc.dg/bic-bitmask-10.c
new file mode 100644
index 0000000..0d04160
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-10.c
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(int32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(int32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#define TYPE int32_t
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump {<=\s*.+\{ 255,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967290,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-11.c b/gcc/testsuite/gcc.dg/bic-bitmask-11.c
new file mode 100644
index 0000000..0e589c9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-11.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) != 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) != 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump {>\s*.+\{ 255,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967290,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-12.c b/gcc/testsuite/gcc.dg/bic-bitmask-12.c
new file mode 100644
index 0000000..50eb563
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-12.c
@@ -0,0 +1,17 @@
+/* { dg-do assemble } */
+/* { dg-options "-O3 -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+typedef unsigned int v4si __attribute__ ((vector_size (16)));
+
+__attribute__((noinline, noipa))
+void fun(v4si *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+/* { dg-final { scan-tree-dump {<=\s*.+\{ 255,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967290,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-13.c b/gcc/testsuite/gcc.dg/bic-bitmask-13.c
new file mode 100644
index 0000000..bac86c2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-13.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O0 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s* 255} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967040} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-14.c b/gcc/testsuite/gcc.dg/bic-bitmask-14.c
new file mode 100644
index 0000000..ec3bd6a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-14.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s* 255} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967040} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-15.c b/gcc/testsuite/gcc.dg/bic-bitmask-15.c
new file mode 100644
index 0000000..8bdf1ea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-15.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) >= 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) >= 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {=\s* 1} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967040} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-16.c b/gcc/testsuite/gcc.dg/bic-bitmask-16.c
new file mode 100644
index 0000000..cfea925
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-16.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) > 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) > 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {>\s* 255} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967040} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-17.c b/gcc/testsuite/gcc.dg/bic-bitmask-17.c
new file mode 100644
index 0000000..86873b9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-17.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) <= 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) <= 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s* 255} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967040} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-18.c b/gcc/testsuite/gcc.dg/bic-bitmask-18.c
new file mode 100644
index 0000000..9d11b3b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-18.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) < 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) < 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {= 0} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-19.c b/gcc/testsuite/gcc.dg/bic-bitmask-19.c
new file mode 100644
index 0000000..c4620df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-19.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) != 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) != 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {>\s* 1} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967294} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-2.c b/gcc/testsuite/gcc.dg/bic-bitmask-2.c
new file mode 100644
index 0000000..59ba9a4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-2.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s*.+\{ 255,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967040,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-20.c b/gcc/testsuite/gcc.dg/bic-bitmask-20.c
new file mode 100644
index 0000000..a114122
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-20.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~5)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~5)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-not {<=\s* 4294967289} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump {&\s* 4294967290} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-21.c b/gcc/testsuite/gcc.dg/bic-bitmask-21.c
new file mode 100644
index 0000000..bd12a58
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-21.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(int32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(int32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#define TYPE int32_t
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump {<=\s* 255} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967290} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-22.c b/gcc/testsuite/gcc.dg/bic-bitmask-22.c
new file mode 100644
index 0000000..a9f0867
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-22.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) != 0;
+}
+
+__attribute__((noinline, noipa, optimize("O0")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) != 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump {>\s* 255} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s* 4294967290} dce7 { target vect_int } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-23.c b/gcc/testsuite/gcc.dg/bic-bitmask-23.c
new file mode 100644
index 0000000..b41651b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-23.c
@@ -0,0 +1,16 @@
+/* { dg-do assemble } */
+/* { dg-options "-O1 -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+typedef unsigned int v4si __attribute__ ((vector_size (16)));
+
+__attribute__((noinline, noipa))
+v4si fun(v4si x)
+{
+ v4si mask = { 255, 15, 1, 0xFFFF };
+ v4si zeros = {0};
+ return (x & ~mask) == zeros;
+}
+
+/* { dg-final { scan-tree-dump {<=\s*.+\{ 255, 15, 1, 65535 \}} dce7 { target vect_int } } } */
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-3.c b/gcc/testsuite/gcc.dg/bic-bitmask-3.c
new file mode 100644
index 0000000..59ba9a4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-3.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s*.+\{ 255,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967040,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-4.c b/gcc/testsuite/gcc.dg/bic-bitmask-4.c
new file mode 100644
index 0000000..7e0614d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-4.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) >= 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) >= 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {=\s*.+\{ 1,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967040,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-5.c b/gcc/testsuite/gcc.dg/bic-bitmask-5.c
new file mode 100644
index 0000000..e71b17d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-5.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) > 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) > 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {>\s*.+\{ 255,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967040,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-6.c b/gcc/testsuite/gcc.dg/bic-bitmask-6.c
new file mode 100644
index 0000000..a48a226
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-6.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) <= 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~255)) <= 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {<=\s*.+\{ 255,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967040,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-7.c b/gcc/testsuite/gcc.dg/bic-bitmask-7.c
new file mode 100644
index 0000000..bc49f29
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-7.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) < 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) < 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {__builtin_memset} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-8.c b/gcc/testsuite/gcc.dg/bic-bitmask-8.c
new file mode 100644
index 0000000..cd06e0c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-8.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) != 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~1)) != 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-times {>\s*.+\{ 1,.+\}} 1 dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {&\s*.+\{ 4294967294,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask-9.c b/gcc/testsuite/gcc.dg/bic-bitmask-9.c
new file mode 100644
index 0000000..3d88b74
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask-9.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -save-temps -fdump-tree-dce" } */
+
+#include <stdint.h>
+
+__attribute__((noinline, noipa))
+void fun1(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~5)) == 0;
+}
+
+__attribute__((noinline, noipa, optimize("O1")))
+void fun2(uint32_t *x, int n)
+{
+ for (int i = 0; i < (n & -16); i++)
+ x[i] = (x[i]&(~5)) == 0;
+}
+
+#include "bic-bitmask.h"
+
+/* { dg-final { scan-tree-dump-not {<=\s*.+\{ 4294967289,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump {&\s*.+\{ 4294967290,.+\}} dce7 { target vect_int } } } */
+/* { dg-final { scan-tree-dump-not {\s+bic\s+} dce7 { target { aarch64*-*-* } } } } */
+
diff --git a/gcc/testsuite/gcc.dg/bic-bitmask.h b/gcc/testsuite/gcc.dg/bic-bitmask.h
new file mode 100644
index 0000000..faf80b9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bic-bitmask.h
@@ -0,0 +1,43 @@
+#include <stdio.h>
+
+#ifndef N
+#define N 65
+#endif
+
+#ifndef TYPE
+#define TYPE uint32_t
+#endif
+
+#ifndef DEBUG
+#define DEBUG 0
+#endif
+
+#define BASE ((TYPE) -1 < 0 ? -126 : 4)
+
+int main ()
+{
+ TYPE a[N];
+ TYPE b[N];
+
+ for (int i = 0; i < N; ++i)
+ {
+ a[i] = BASE + i * 13;
+ b[i] = BASE + i * 13;
+ if (DEBUG)
+ printf ("%d: 0x%x\n", i, a[i]);
+ }
+
+ fun1 (a, N);
+ fun2 (b, N);
+
+ for (int i = 0; i < N; ++i)
+ {
+ if (DEBUG)
+ printf ("%d = 0x%x == 0x%x\n", i, a[i], b[i]);
+
+ if (a[i] != b[i])
+ __builtin_abort ();
+ }
+ return 0;
+}
+
diff --git a/gcc/testsuite/gcc.target/aarch64/bic-bitmask-1.c b/gcc/testsuite/gcc.target/aarch64/bic-bitmask-1.c
new file mode 100644
index 0000000..568c1ff
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/bic-bitmask-1.c
@@ -0,0 +1,13 @@
+/* { dg-do assemble } */
+/* { dg-options "-O2 -save-temps" } */
+
+#include <arm_neon.h>
+
+uint32x4_t foo (int32x4_t a)
+{
+ int32x4_t cst = vdupq_n_s32 (255);
+ int32x4_t zero = vdupq_n_s32 (0);
+ return vceqq_s32 (vbicq_s32 (a, cst), zero);
+}
+
+/* { dg-final { scan-assembler-not {\tbic\t} { xfail { aarch64*-*-* } } } } */
diff --git a/gcc/tree.c b/gcc/tree.c
index 62d9d78..910fb06 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -10273,6 +10273,59 @@ uniform_integer_cst_p (tree t)
return NULL_TREE;
}
+/* Checks to see if T is a constant or a constant vector and if each element E
+ adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
+
+tree
+bitmask_inv_cst_vector_p (tree t)
+{
+
+ tree_code code = TREE_CODE (t);
+ tree type = TREE_TYPE (t);
+
+ if (!INTEGRAL_TYPE_P (type)
+ && !VECTOR_INTEGER_TYPE_P (type))
+ return NULL_TREE;
+
+ unsigned HOST_WIDE_INT nelts = 1;
+ tree cst;
+ unsigned int idx = 0;
+ bool uniform = uniform_integer_cst_p (t);
+ tree newtype = unsigned_type_for (type);
+ tree_vector_builder builder;
+ if (code == INTEGER_CST)
+ cst = t;
+ else
+ {
+ if (!VECTOR_CST_NELTS (t).is_constant (&nelts))
+ return NULL_TREE;
+
+ cst = vector_cst_elt (t, 0);
+ builder.new_vector (newtype, nelts, 1);
+ }
+
+ tree ty = unsigned_type_for (TREE_TYPE (cst));
+
+ do {
+ if (idx > 0)
+ cst = vector_cst_elt (t, idx);
+ wide_int icst = wi::to_wide (cst);
+ wide_int inv = wi::bit_not (icst);
+ icst = wi::add (1, inv);
+ if (wi::popcount (icst) != 1)
+ return NULL_TREE;
+
+ tree newcst = wide_int_to_tree (ty, inv);
+
+ if (uniform)
+ return build_uniform_cst (newtype, newcst);
+
+ builder.quick_push (newcst);
+ } while (++idx < nelts);
+
+ return builder.build ();
+}
+
/* If VECTOR_CST T has a single nonzero element, return the index of that
element, otherwise return -1. */
diff --git a/gcc/tree.h b/gcc/tree.h
index 03719b18..f0e72b5 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -4922,6 +4922,11 @@ extern bool integer_minus_onep (const_tree);
extern bool integer_pow2p (const_tree);
+/* Checks to see if T is a constant or a constant vector and if each element E
+ adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
+
+extern tree bitmask_inv_cst_vector_p (tree);
+
/* integer_nonzerop (tree x) is nonzero if X is an integer constant
with a nonzero value. */