public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@adacore.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [patch] Perform anonymous constant propagation during inlining
Date: Mon, 11 May 2015 14:07:00 -0000	[thread overview]
Message-ID: <1725052.qXkxHEI06s@polaris> (raw)
In-Reply-To: <2D4B40F1-0670-48E8-8FE9-C686EF953F79@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1465 bytes --]

> >Would you be OK with a slight variation of your earlier idea, i.e.
> >calling fold_stmt with a specific valueizer from fold_marked_statements
> >instead of the implicit no_follow_ssa_edges in the inliner?  Something
> >like:
> >
> >tree
> >follow_anonymous_single_use_edges (tree val)
> >{
> >
> >  if (TREE_CODE (val) == SSA_NAME
> >  
> >      && (!SSA_NAME_VAR (val) || DECL_IGNORED_P (SSA_NAME_VAR
> >      (var)))
> >      && has_single_use (val))
> >    
> >    return val
> >  
> >  return NULL_TREE;
> >
> >}
> 
> Yes, that works for me as well.

But not for GCC. :-)  The propagation per se works but, since the statement is 
not folded in the end, no replacement is made at all...

So we're back to square one and anonymous constant propagation seems to be the 
only way out at -O0.  The attached patch implements it in a less hackish way 
(and enables it unconditionally when optimizing as suggested by Jan) by doing 
it just before invoking fold_stmt on the marked statements so this should make 
folding more effective in the process. 

Tested (compiler only) on x86_64-suse-linux, OK for the mainline?


2015-05-11  Eric Botcazou  <ebotcazou@adacore.com>

	* tree-inline.c: Include tree-ssa-propagate.h.
	(replace_constant_uses_in): New function.
	(fold_marked_statements): Call it before folding the statement.

	* gimple-expr.h (is_gimple_constant): Reorder.
	* tree-ssa-propagate.c (before_dom_children): Use inline accessor.


-- 
Eric Botcazou

[-- Attachment #2: p.diff --]
[-- Type: text/x-patch, Size: 3854 bytes --]

Index: tree-ssa-propagate.c
===================================================================
--- tree-ssa-propagate.c	(revision 222673)
+++ tree-ssa-propagate.c	(working copy)
@@ -1246,9 +1246,7 @@ substitute_and_fold_dom_walker::before_d
 	      && gimple_call_noreturn_p (stmt))
 	    stmts_to_fixup.safe_push (stmt);
 
-	  if (is_gimple_assign (stmt)
-	      && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
-		  == GIMPLE_SINGLE_RHS))
+	  if (gimple_assign_single_p (stmt))
 	    {
 	      tree rhs = gimple_assign_rhs1 (stmt);
 
Index: tree-inline.c
===================================================================
--- tree-inline.c	(revision 222673)
+++ tree-inline.c	(working copy)
@@ -82,6 +82,7 @@ along with GCC; see the file COPYING3.
 #include "expr.h"
 #include "tree-dfa.h"
 #include "tree-ssa.h"
+#include "tree-ssa-propagate.h"
 #include "tree-pretty-print.h"
 #include "except.h"
 #include "debug.h"
@@ -4801,6 +4802,54 @@ gimple_expand_calls_inline (basic_block
   return inlined;
 }
 
+/* Replace USE references in statement STMT with constant values.  Return
+   true if at least one reference was replaced.
+
+   We do it at -O0 for anonymous SSA names or SSA names of anonymous decls,
+   this makes it possible to generate reasonable code for operators that are
+   implemented as functions and inlined on constants.  */
+
+static bool
+replace_constant_uses_in (gimple stmt)
+{
+  bool replaced = false;
+  use_operand_p use;
+  ssa_op_iter iter;
+
+  FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
+    {
+      tree tuse = USE_FROM_PTR (use);
+
+      if (TREE_CODE (tuse) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (tuse))
+	continue;
+
+      if (!optimize
+	  && SSA_NAME_VAR (tuse)
+	  && !DECL_IGNORED_P (SSA_NAME_VAR (tuse)))
+	continue;
+
+      if (!gimple_assign_single_p (SSA_NAME_DEF_STMT (tuse)))
+	continue;
+
+      tree rhs = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (tuse));
+      if (!is_gimple_constant (rhs))
+	continue;
+
+      propagate_value (use, rhs);
+
+      replaced = true;
+    }
+
+  if (replaced && gimple_assign_single_p (stmt))
+    {
+      tree rhs = gimple_assign_rhs1 (stmt);
+
+      if (TREE_CODE (rhs) == ADDR_EXPR)
+	recompute_tree_invariant_for_addr_expr (rhs);
+    }
+
+  return replaced;
+}
 
 /* Walk all basic blocks created after FIRST and try to fold every statement
    in the STATEMENTS pointer set.  */
@@ -4819,7 +4868,10 @@ fold_marked_statements (int first, hash_
 	  if (statements->contains (gsi_stmt (gsi)))
 	    {
 	      gimple old_stmt = gsi_stmt (gsi);
-	      tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
+	      tree old_decl
+		= is_gimple_call (old_stmt)
+		  ? gimple_call_fndecl (old_stmt) : NULL_TREE;
+	      bool replaced = replace_constant_uses_in (old_stmt);
 
 	      if (old_decl && DECL_BUILT_IN (old_decl))
 		{
@@ -4827,7 +4879,7 @@ fold_marked_statements (int first, hash_
 		     we need to look at all of them.  */
 		  gimple_stmt_iterator i2 = gsi;
 		  gsi_prev (&i2);
-		  if (fold_stmt (&gsi))
+		  if (fold_stmt (&gsi) || replaced)
 		    {
 		      gimple new_stmt;
 		      /* If a builtin at the end of a bb folded into nothing,
@@ -4871,7 +4923,7 @@ fold_marked_statements (int first, hash_
 			}
 		    }
 		}
-	      else if (fold_stmt (&gsi))
+	      else if (fold_stmt (&gsi) || replaced)
 		{
 		  /* Re-read the statement from GSI as fold_stmt() may
 		     have changed it.  */
Index: gimple-expr.h
===================================================================
--- gimple-expr.h	(revision 222673)
+++ gimple-expr.h	(working copy)
@@ -136,9 +136,9 @@ is_gimple_constant (const_tree t)
     case INTEGER_CST:
     case REAL_CST:
     case FIXED_CST:
-    case STRING_CST:
     case COMPLEX_CST:
     case VECTOR_CST:
+    case STRING_CST:
       return true;
 
     default:

  reply	other threads:[~2015-05-11 14:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-29 10:23 Eric Botcazou
2015-04-29 12:06 ` Richard Biener
2015-05-01 10:29   ` Eric Botcazou
2015-05-01 14:19     ` Richard Biener
2015-05-01 16:44       ` Eric Botcazou
2015-05-01 18:11         ` Eric Botcazou
2015-05-04  8:46           ` Richard Biener
2015-05-04  9:32             ` Eric Botcazou
2015-05-04 21:40           ` Eric Botcazou
2015-05-05  5:43             ` Richard Biener
2015-05-11 14:07               ` Eric Botcazou [this message]
2015-05-12  8:46                 ` Richard Biener
2015-04-29 13:29 ` Jan Hubicka
2015-04-29 13:50   ` Richard Biener
2015-04-29 14:31     ` Jan Hubicka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1725052.qXkxHEI06s@polaris \
    --to=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).