This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix X | ~X and X ^ ~X optimizations for vectors (PR middle-end/32912)
On Mon, Aug 20, 2007 at 03:19:36PM +0200, Richard Guenther wrote:
> Hm, for the similar problem with ones we introduced build_one_cst (which
> avoids the check for VECTOR_TYPE all over the place). So can you instead
> create a new build_all_ones_cst () function for this? I don't have a
> very strong
> opinion about this though. Another alternative would be a build_zero_cst which
> we could convert to all ones via constant folding and a unary not...
I have done the latter (though I had to add support for folding
BIT_NOT_EXPR of VECTOR_CST in fold_unary, which is IMHO a good thing
anyway).
If these are the only places which would use build_all_ones_cst, introducing
that would be IMHO unnecessary, but I don't have strong opinion about it
either.
2007-08-20 Jakub Jelinek <jakub@redhat.com>
PR middle-end/32912
* fold-const.c (fold_unary): Optimize BIT_NOT_EXPR of VECTOR_CST.
(fold_binary): Handle vectors in X | ~X and X ^ ~X optimizations.
* gcc.dg/pr32912-1.c: New test.
* gcc.dg/pr32912-2.c: New test.
* gcc.dg/pr32912-3.c: New test.
--- gcc/fold-const.c.jj 2007-08-13 15:11:18.000000000 +0200
+++ gcc/fold-const.c 2007-08-20 15:49:05.000000000 +0200
@@ -8397,6 +8397,29 @@ fold_unary (enum tree_code code, tree ty
TREE_OPERAND (arg0, 1)))))
return fold_build2 (BIT_XOR_EXPR, type,
fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
+ /* Perform BIT_NOT_EXPR on each element individually. */
+ else if (TREE_CODE (arg0) == VECTOR_CST)
+ {
+ tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
+ int count = TYPE_VECTOR_SUBPARTS (type), i;
+
+ for (i = 0; i < count; i++)
+ {
+ if (elements)
+ {
+ elem = TREE_VALUE (elements);
+ elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
+ if (elem == NULL_TREE)
+ break;
+ elements = TREE_CHAIN (elements);
+ }
+ else
+ elem = build_int_cst (TREE_TYPE (type), -1);
+ list = tree_cons (NULL_TREE, elem, list);
+ }
+ if (i == count)
+ return build_vector (type, nreverse (list));
+ }
return NULL_TREE;
@@ -10480,7 +10503,8 @@ fold_binary (enum tree_code code, tree t
if (TREE_CODE (arg0) == BIT_NOT_EXPR
&& operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
{
- t1 = build_int_cst_type (type, -1);
+ t1 = fold_convert (type, integer_zero_node);
+ t1 = fold_unary (BIT_NOT_EXPR, type, t1);
return omit_one_operand (type, t1, arg1);
}
@@ -10488,7 +10512,8 @@ fold_binary (enum tree_code code, tree t
if (TREE_CODE (arg1) == BIT_NOT_EXPR
&& operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
{
- t1 = build_int_cst_type (type, -1);
+ t1 = fold_convert (type, integer_zero_node);
+ t1 = fold_unary (BIT_NOT_EXPR, type, t1);
return omit_one_operand (type, t1, arg0);
}
@@ -10594,7 +10619,8 @@ fold_binary (enum tree_code code, tree t
if (TREE_CODE (arg0) == BIT_NOT_EXPR
&& operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
{
- t1 = build_int_cst_type (type, -1);
+ t1 = fold_convert (type, integer_zero_node);
+ t1 = fold_unary (BIT_NOT_EXPR, type, t1);
return omit_one_operand (type, t1, arg1);
}
@@ -10602,7 +10628,8 @@ fold_binary (enum tree_code code, tree t
if (TREE_CODE (arg1) == BIT_NOT_EXPR
&& operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
{
- t1 = build_int_cst_type (type, -1);
+ t1 = fold_convert (type, integer_zero_node);
+ t1 = fold_unary (BIT_NOT_EXPR, type, t1);
return omit_one_operand (type, t1, arg0);
}
--- gcc/testsuite/gcc.dg/pr32912-1.c.jj 2007-08-20 14:43:05.000000000 +0200
+++ gcc/testsuite/gcc.dg/pr32912-1.c 2007-08-20 14:43:23.000000000 +0200
@@ -0,0 +1,44 @@
+/* PR middle-end/32912 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+
+typedef int __m128i __attribute__ ((__vector_size__ (16)));
+
+__m128i a, b, c, d, e, f;
+
+void
+foo (__m128i x)
+{
+ a = x ^ ~x;
+ b = ~x ^ x;
+ c = x | ~x;
+ d = ~x | x;
+ e = x & ~x;
+ f = ~x & x;
+}
+
+int
+main (void)
+{
+ union { __m128i v; int i[sizeof (__m128i) / sizeof (int)]; } u;
+ int i;
+
+ for (i = 0; i < sizeof (u.i) / sizeof (u.i[0]); i++)
+ u.i[i] = i * 49 - 36;
+ foo (u.v);
+#define check(x, val) \
+ u.v = (x); \
+ for (i = 0; i < sizeof (u.i) / sizeof (u.i[0]); i++) \
+ if (u.i[i] != (val)) \
+ abort ()
+
+ check (a, ~0);
+ check (b, ~0);
+ check (c, ~0);
+ check (d, ~0);
+ check (e, 0);
+ check (f, 0);
+ return 0;
+}
--- gcc/testsuite/gcc.dg/pr32912-2.c.jj 2007-08-20 15:58:47.000000000 +0200
+++ gcc/testsuite/gcc.dg/pr32912-2.c 2007-08-20 15:55:32.000000000 +0200
@@ -0,0 +1,45 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+
+typedef int __m128i __attribute__ ((__vector_size__ (16)));
+
+__m128i a, b, c, d, e, f;
+
+__m128i
+foo (void)
+{
+ __m128i x = { 0x11111111, 0x22222222, 0x44444444 };
+ return x;
+}
+
+__m128i
+bar (void)
+{
+ __m128i x = { 0x11111111, 0x22222222, 0x44444444 };
+ return ~x;
+}
+
+int
+main (void)
+{
+ union { __m128i v; int i[sizeof (__m128i) / sizeof (int)]; } u, v;
+ int i;
+
+ u.v = foo ();
+ v.v = bar ();
+ for (i = 0; i < sizeof (u.i) / sizeof (u.i[0]); i++)
+ {
+ if (u.i[i] != ~v.i[i])
+ abort ();
+ if (i < 3)
+ {
+ if (u.i[i] != (0x11111111 << i))
+ abort ();
+ }
+ else if (u.i[i])
+ abort ();
+ }
+ return 0;
+}
--- gcc/testsuite/gcc.dg/pr32912-3.c.jj 2007-08-20 15:58:47.000000000 +0200
+++ gcc/testsuite/gcc.dg/pr32912-3.c 2007-08-20 16:01:53.000000000 +0200
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef int __m128i __attribute__ ((__vector_size__ (16)));
+
+__m128i
+bar (void)
+{
+ __m128i x = { 0x11111111, 0x22222222, 0x44444444 };
+ return ~x;
+}
+
+/* { dg-final { scan-tree-dump-not "~" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
Jakub