This makes sure that we propagate comparisons that we cannot invert
into inverted conditions by swapping edges or the conditional ops.
For the testcase it transforms

<bb 2>:
  D.2724_4 = xx_2(D) < xy_3(D);
  p_5 = (int) D.2724_4;
  D.2725_6 = p_5 == 0;
  np_7 = (int) D.2725_6;
  if (np_7 != 0)

to

<bb 2>:
  if (xx_2(D) < xy_3(D))

while without the patch we would be left with

<bb 2>:
  D.2724_4 = xx_2(D) < xy_3(D);
  if (D.2724_4 == 0)

Bootstrap and regtest ongoing on x86_64-unknown-linux-gnu.

Richard.

2011-09-07  Richard Guenther  <rguent...@suse.de>

        * tree-ssa-forwprop.c (forward_propagate_into_gimple_cond):
        Canonicalize negated predicates by swapping edges.
        (forward_propagate_into_cond): Likewise.

        * gcc.dg/tree-ssa/forwprop-16.c: New testcase.

Index: gcc/tree-ssa-forwprop.c
===================================================================
*** gcc/tree-ssa-forwprop.c     (revision 178633)
--- gcc/tree-ssa-forwprop.c     (working copy)
*************** forward_propagate_into_gimple_cond (gimp
*** 534,539 ****
--- 534,556 ----
        return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
      }
  
+   /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges.  
*/
+   if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE
+        || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
+          && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
+       && ((code == EQ_EXPR
+          && integer_zerop (rhs2))
+         || (code == NE_EXPR
+             && integer_onep (rhs2))))
+     {
+       basic_block bb = gimple_bb (stmt);
+       gimple_cond_set_code (stmt, NE_EXPR);
+       gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1)));
+       EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
+       EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
+       return 1;
+     }
+ 
    return 0;
  }
  
*************** forward_propagate_into_cond (gimple_stmt
*** 548,553 ****
--- 565,571 ----
    gimple stmt = gsi_stmt (*gsi_p);
    tree tmp = NULL_TREE;
    tree cond = gimple_assign_rhs1 (stmt);
+   bool swap = false;
  
    /* We can do tree combining on SSA_NAME and comparison expressions.  */
    if (COMPARISON_CLASS_P (cond))
*************** forward_propagate_into_cond (gimple_stmt
*** 557,573 ****
                                               TREE_OPERAND (cond, 1));
    else if (TREE_CODE (cond) == SSA_NAME)
      {
        tree name = cond;
        gimple def_stmt = get_prop_source_stmt (name, true, NULL);
        if (!def_stmt || !can_propagate_from (def_stmt))
        return 0;
  
!       if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == 
tcc_comparison)
        tmp = fold_build2_loc (gimple_location (def_stmt),
!                              gimple_assign_rhs_code (def_stmt),
                               boolean_type_node,
                               gimple_assign_rhs1 (def_stmt),
                               gimple_assign_rhs2 (def_stmt));
      }
  
    if (tmp)
--- 575,601 ----
                                               TREE_OPERAND (cond, 1));
    else if (TREE_CODE (cond) == SSA_NAME)
      {
+       enum tree_code code;
        tree name = cond;
        gimple def_stmt = get_prop_source_stmt (name, true, NULL);
        if (!def_stmt || !can_propagate_from (def_stmt))
        return 0;
  
!       code = gimple_assign_rhs_code (def_stmt);
!       if (TREE_CODE_CLASS (code) == tcc_comparison)
        tmp = fold_build2_loc (gimple_location (def_stmt),
!                              code,
                               boolean_type_node,
                               gimple_assign_rhs1 (def_stmt),
                               gimple_assign_rhs2 (def_stmt));
+       else if ((code == BIT_NOT_EXPR
+               && TYPE_PRECISION (TREE_TYPE (cond)) == 1)
+              || (code == BIT_XOR_EXPR
+                  && integer_onep (gimple_assign_rhs2 (def_stmt))))
+       {
+         tmp = gimple_assign_rhs1 (def_stmt);
+         swap = true;
+       }
      }
  
    if (tmp)
*************** forward_propagate_into_cond (gimple_stmt
*** 586,592 ****
        else if (integer_zerop (tmp))
        gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs3 (stmt));
        else
!       gimple_assign_set_rhs1 (stmt, unshare_expr (tmp));
        stmt = gsi_stmt (*gsi_p);
        update_stmt (stmt);
  
--- 614,628 ----
        else if (integer_zerop (tmp))
        gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs3 (stmt));
        else
!       {
!         gimple_assign_set_rhs1 (stmt, unshare_expr (tmp));
!         if (swap)
!           {
!             tree t = gimple_assign_rhs2 (stmt);
!             gimple_assign_set_rhs2 (stmt, gimple_assign_rhs3 (stmt));
!             gimple_assign_set_rhs3 (stmt, t);
!           }
!       }
        stmt = gsi_stmt (*gsi_p);
        update_stmt (stmt);
  
Index: gcc/testsuite/gcc.dg/tree-ssa/forwprop-16.c
===================================================================
*** gcc/testsuite/gcc.dg/tree-ssa/forwprop-16.c (revision 0)
--- gcc/testsuite/gcc.dg/tree-ssa/forwprop-16.c (revision 0)
***************
*** 0 ****
--- 1,14 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -fdump-tree-forwprop1" } */
+ 
+ int foo (double xx, double xy)
+ {
+   int p = xx < xy;
+   int np = !p; 
+   if (np)
+     return 5;
+   return 2;
+ }
+ 
+ /* { dg-final { scan-tree-dump "if \\\(x" "forwprop1" } } */
+ /* { dg-final { cleanup-tree-dump "forwprop1" } } */

Reply via email to