Patchwork [tree-optimization] : 6 of 6 Improve reassoc for bitwise operations

login
register
mail settings
Submitter Kai Tietz
Date Oct. 7, 2011, 4:39 p.m.
Message ID <CAEwic4bitKXDCF-i805PAA8JWpO703yeHa-Ws-pqOeFJvsp1uQ@mail.gmail.com>
Download mbox | patch
Permalink /patch/118352/
State New
Headers show

Comments

Kai Tietz - Oct. 7, 2011, 4:39 p.m.
Hello,

This patch adds to the repropagation code the conversion for ~X ==/!=
CST to X ==/!= CST' (with CST' = ~ CST).
We need to do this back-conversion after initial bitwise-binary
repropagation loop, as otherwise
it would interfer.

ChangeLog

2011-10-07  Kai Tietz  <ktietz@redhat.com>

	* tree-ssa-reassoc.c (repropagate_bitwise): Add handling
	for ~X !=/== CST conversion.

2011-10-07  Kai Tietz  <ktietz@redhat.com>

	* gcc.dg/tree-ssa/reassoc-repro_cmpior-4.c: New file.
	* gcc.dg/tree-ssa/reassoc-repro_cmpior-5.c: New file.
	* gcc.dg/tree-ssa/reassoc-repro_cmpior-6.c: New file.

Bootstrapped and regression-tested for all languages plus Ada and
Obj-C++ on x86_64-pc-linux-gnu.
Ok for apply?

Regards,
Kai

Patch

Index: gcc/gcc/tree-ssa-reassoc.c
===================================================================
--- gcc.orig/gcc/tree-ssa-reassoc.c
+++ gcc/gcc/tree-ssa-reassoc.c
@@ -3712,6 +3712,37 @@  repropagate_bitwise (basic_block bb)
       remove_stmt_chain (r);
     }

+  /* Now we are doing transformation ~X !=/== CST -> X !=/== ~CST.  */
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    {
+      gimple sub;
+      gimple stmt = gsi_stmt (gsi);
+      enum tree_code code;
+      tree l, r;
+
+      if (!is_gimple_assign (stmt))
+	continue;
+
+      code = gimple_assign_rhs_code (stmt);
+      if (code != EQ_EXPR && code != NE_EXPR)
+	continue;
+      l = gimple_assign_rhs1 (stmt);
+      r = gimple_assign_rhs2 (stmt);
+      if (TREE_CODE (l) != SSA_NAME
+          || TREE_CODE (r) != INTEGER_CST
+          || (sub = SSA_NAME_DEF_STMT (l)) == NULL
+          || !is_gimple_assign (sub)
+          || gimple_assign_rhs_code (sub) != BIT_NOT_EXPR
+	  || !INTEGRAL_TYPE_P (TREE_TYPE (l)))
+	continue;
+      gimple_assign_set_rhs_with_ops (&gsi, code, gimple_assign_rhs1 (sub),
+				      fold_build1 (BIT_NOT_EXPR,
+						   TREE_TYPE (l), r));
+      update_stmt (stmt);
+      remove_stmt_chain (l);
+      remove_stmt_chain (r);
+    }
+
   for (son = first_dom_son (CDI_DOMINATORS, bb);
        son;
        son = next_dom_son (CDI_DOMINATORS, son))
Index: gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-4.c
===================================================================
--- /dev/null
+++ gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-4.c
@@ -0,0 +1,21 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -ffast-math" } */
+
+int foo (int a, int b, int c, int d)
+{
+  int r = (a == -1) & (b == -1);
+  int q = (c == -1) & (d == -1);
+  return r & q;
+}
+
+int bar (int a, int b, int c, int d)
+{
+  int r = (a != -1) | (b != -1);
+  int q = (c != -1) | (d != -1);
+  return r | q;
+}
+
+/* { dg-final { scan-tree-dump-times " == -1" 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " != -1" 1 "optimized"} } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+
Index: gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-5.c
===================================================================
--- /dev/null
+++ gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-5.c
@@ -0,0 +1,21 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -ffast-math" } */
+
+int foo (int a, char b, short c, long d)
+{
+  int r = (a == -1) & (b == -1);
+  int q = (c == -1) & (d == -1);
+  return r & q;
+}
+
+int bar (int a, char b, short c, long d)
+{
+  int r = (a != -1) | (b != -1);
+  int q = (c != -1) | (d != -1);
+  return r | q;
+}
+
+/* { dg-final { scan-tree-dump-times " == -1" 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " != -1" 1 "optimized"} } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+
Index: gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-6.c
===================================================================
--- /dev/null
+++ gcc/gcc/testsuite/gcc.dg/tree-ssa/reassoc-repro_cmpior-6.c
@@ -0,0 +1,27 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -ffast-math" } */
+
+int foo (int a, unsigned char b, unsigned short c, unsigned long d)
+{
+  unsigned char b1 = ~0;
+  unsigned short c1 = ~0;
+  unsigned long d1 = ~0;
+  int r = (a == -1) & (b == b1);
+  int q = (c == c1) & (d == d1);
+  return r & q;
+}
+
+int bar (int a, unsigned char b, unsigned short c, unsigned long d)
+{
+  unsigned char b1 = ~0;
+  unsigned short c1 = ~0;
+  unsigned long d1 = ~0;
+  int r = (a != -1) | (b != b1);
+  int q = (c != c1) | (d != d1);
+  return r | q;
+}
+
+/* { dg-final { scan-tree-dump-times " == -1" 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " != -1" 1 "optimized"} } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+