PR 41952 -- simple folding rule

View: New views
1 Messages — Rating Filter:   Alert me  

PR 41952 -- simple folding rule

by Xinliang David Li :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, the patch is to used to delete redundant comparisons and branches
resulting from inlining. While it does not address any particular bug
in 4.5, it is safe to be considered.

Tested: bootstrap and regression tested: x86_64/linux (with 4.5).

Ok to get in?

Thanks,

David

[fold.patch]

Index: fold-const.c
===================================================================
--- fold-const.c (revision 153643)
+++ fold-const.c (working copy)
@@ -9572,6 +9572,26 @@ fold_comparison (location_t loc, enum tr
       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
     }
 
+  /* Fold comparison using aliasing guarantee:
+     &local_var != parameter_default
+     &local_var == parameter_default
+
+     A local variable can never be pointed to by
+     the default SSA name of an incoming parameter.  */
+
+  if (TREE_CODE (arg0) == ADDR_EXPR
+      && TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL
+      && auto_var_in_fn_p (TREE_OPERAND (arg0, 0), current_function_decl)
+      && TREE_CODE (arg1) == SSA_NAME
+      && TREE_CODE (SSA_NAME_VAR (arg1)) == PARM_DECL
+      && SSA_NAME_IS_DEFAULT_DEF (arg1))
+    {
+      if (code == NE_EXPR)
+        return constant_boolean_node (1, type);
+      else if (code == EQ_EXPR)
+        return constant_boolean_node (0, type);
+    }
+
   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
   if (tem)
     return tem;
Index: testsuite/g++.dg/tree-ssa/fold-compare.C
===================================================================
--- testsuite/g++.dg/tree-ssa/fold-compare.C (revision 0)
+++ testsuite/g++.dg/tree-ssa/fold-compare.C (revision 0)
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+struct ExtentsBase {
+ ExtentsBase() : startx_(), endx_() { }
+ ExtentsBase(const ExtentsBase &b) {
+  *this = b;
+ }
+
+ const ExtentsBase & operator=(const ExtentsBase &b) {
+  if (this != &b) {
+    startx_ = b.startx_;
+  }
+  return *this;
+ }
+
+ int startx_;
+  int endx_;
+};
+
+int f(const ExtentsBase &e1) {
+ ExtentsBase my_extents = e1;
+ return my_extents.startx_;
+}
+
+/* { dg-final { scan-tree-dump-not "&my_extents" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */




fold.cl (182 bytes) Download Attachment
fold.cl2 (130 bytes) Download Attachment