The following tries to address the issue that with delayed folding
and the general attempt to do folding on GIMPLE we have "unfolded"
trees up to the point where we go into SSA (as otherwise the
match-and-simplify machinery on GIMPLE can't get to the defs of uses
to match larger expressions).

The obvious solution is to do folding at gimplification time where
we already process GENERIC bottom-up via recursion.

Of course that requires a solution for the missing use->def
information in pre-SSA GIMPLE.

The solution is quite obvious in making the gimplifier use SSA
names for all temporaries it creates when gimplifying expressions.
In fact it can already do this when operating on SSA GIMPLE.

So the following proof-of-concept patch makes the gimplifier
do that (and it passes a surprisingly large amount of testcases
without issues - most ICEs are for VLAs because we gimplify
sizes to SSA temps which eventually get released).  It's also 
way cheaper to allocate SSA names compared to decls for each temporary.
Note the prototype doesn't try to use match-and-simplify yet.

With this in place the gimplifier can be changed to build GIMPLE
stmts with the gimple_build interface (into the appropriate
pre/post sequence) and thus automatically (re-)fold all expressions
(but not across statement border - user vars would stay in non-SSA).

A final patch would get rid of the into_ssa flag in the gimplifier
context and instead annotate the few places where we can't do
with SSA temps (like for gimplifying COND_EXPRs into control-flow).
After all the gimplifier wouldn't create PHI nodes (we don't have
a CFG).  The gimplifier also wouldn't set up SSA operands so this
is only about having SSA_NAME_DEF_STMT ready.

Any comments?

Thanks,
Richard.

2015-12-18  Richard Biener  <rguent...@suse.de>

        * gimplify.c (gimplify_modify_expr): Adjust assert.
        (gimplify_body): Gimplify into-SSA.
        (gimplify_function_tree): Init GIMPLE SSA data structures.
        * passes.def (pass_init_datastructures): Remove.
        * tree-into-ssa.c (mark_def_sites): Ignore existing SSA names.
        (rewrite_stmt): Likewise.

Index: gcc/gimplify.c
===================================================================
--- gcc/gimplify.c      (revision 231805)
+++ gcc/gimplify.c      (working copy)
@@ -4841,7 +4841,8 @@ gimplify_modify_expr (tree *expr_p, gimp
   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
     {
       /* We should have got an SSA name from the start.  */
-      gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
+      gcc_assert (TREE_CODE (*to_p) == SSA_NAME
+                 || ! gimple_in_ssa_p (cfun));
     }
 
   gimplify_seq_add_stmt (pre_p, assign);
@@ -11215,7 +11216,7 @@ gimplify_body (tree fndecl, bool do_parm
   default_rtl_profile ();
 
   gcc_assert (gimplify_ctxp == NULL);
-  push_gimplify_context ();
+  push_gimplify_context (true);
 
   if (flag_openacc || flag_openmp)
     {
@@ -11382,6 +11383,8 @@ gimplify_function_tree (tree fndecl)
      if necessary.  */
   cfun->curr_properties |= PROP_gimple_lva;
 
+  init_tree_ssa (cfun);
+
   for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
     {
       /* Preliminarily mark non-addressed complex variables as eligible
Index: gcc/passes.def
===================================================================
--- gcc/passes.def      (revision 231805)
+++ gcc/passes.def      (working copy)
@@ -54,7 +54,6 @@ along with GCC; see the file COPYING3.
   NEXT_PASS (pass_build_ssa_passes);
   PUSH_INSERT_PASSES_WITHIN (pass_build_ssa_passes)
       NEXT_PASS (pass_fixup_cfg);
-      NEXT_PASS (pass_init_datastructures);
       NEXT_PASS (pass_build_ssa);
       NEXT_PASS (pass_ubsan);
       NEXT_PASS (pass_early_warn_uninitialized);
Index: gcc/tree-into-ssa.c
===================================================================
--- gcc/tree-into-ssa.c (revision 231805)
+++ gcc/tree-into-ssa.c (working copy)
@@ -666,6 +666,8 @@ mark_def_sites (basic_block bb, gimple *
   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
     {
       tree sym = USE_FROM_PTR (use_p);
+      if (TREE_CODE (sym) == SSA_NAME)
+       continue;
       gcc_checking_assert (DECL_P (sym));
       if (!bitmap_bit_p (kills, DECL_UID (sym)))
        set_livein_block (sym, bb);
@@ -676,6 +678,8 @@ mark_def_sites (basic_block bb, gimple *
      each def to the set of killed symbols.  */
   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
     {
+      if (TREE_CODE (def) == SSA_NAME)
+       continue;
       gcc_checking_assert (DECL_P (def));
       set_def_block (def, bb, false);
       bitmap_set_bit (kills, DECL_UID (def));
@@ -1310,6 +1314,8 @@ rewrite_stmt (gimple_stmt_iterator *si)
        FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
          {
            tree var = USE_FROM_PTR (use_p);
+           if (TREE_CODE (var) == SSA_NAME)
+             continue;
            gcc_checking_assert (DECL_P (var));
            SET_USE (use_p, get_reaching_def (var));
          }
@@ -1323,6 +1329,8 @@ rewrite_stmt (gimple_stmt_iterator *si)
        tree name;
        tree tracked_var;
 
+       if (TREE_CODE (var) == SSA_NAME)
+         continue;
        gcc_checking_assert (DECL_P (var));
 
        if (gimple_clobber_p (stmt)

Reply via email to