]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/tree-outof-ssa.c
Fix missed IPA-CP on by-ref argument directly passed through (PR 93429)
[thirdparty/gcc.git] / gcc / tree-outof-ssa.c
index be57ce4e24278462b47191b5d14010f6bacc66e4..908b033a3c75a26991c6ab1a1bc6adbba667cd83 100644 (file)
@@ -1,5 +1,5 @@
 /* Convert a program in SSA form into Normal form.
-   Copyright (C) 2004-2016 Free Software Foundation, Inc.
+   Copyright (C) 2004-2020 Free Software Foundation, Inc.
    Contributed by Andrew Macleod <amacleod@redhat.com>
 
 This file is part of GCC.
@@ -27,9 +27,12 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "cfghooks.h"
 #include "ssa.h"
+#include "tree-ssa.h"
+#include "memmodel.h"
 #include "emit-rtl.h"
 #include "gimple-pretty-print.h"
 #include "diagnostic-core.h"
+#include "tree-dfa.h"
 #include "stor-layout.h"
 #include "cfgrtl.h"
 #include "cfganal.h"
@@ -62,7 +65,7 @@ ssa_is_replaceable_p (gimple *stmt)
     return false;
 
   /* If the statement may throw an exception, it cannot be replaced.  */
-  if (stmt_could_throw_p (stmt))
+  if (stmt_could_throw_p (cfun, stmt))
     return false;
 
   /* Punt if there is more than 1 def.  */
@@ -126,8 +129,9 @@ ssa_is_replaceable_p (gimple *stmt)
    rarely more than 6, and in the bootstrap of gcc, the maximum number
    of nodes encountered was 12.  */
 
-struct elim_graph
+class elim_graph
 {
+public:
   elim_graph (var_map map);
 
   /* Size of the elimination vectors.  */
@@ -140,7 +144,7 @@ struct elim_graph
   auto_vec<int> edge_list;
 
   /* Source locus on each edge */
-  auto_vec<source_location> edge_locus;
+  auto_vec<location_t> edge_locus;
 
   /* Visited vector.  */
   auto_sbitmap visited;
@@ -159,7 +163,7 @@ struct elim_graph
   auto_vec<tree> const_copies;
 
   /* Source locations for any constant copies.  */
-  auto_vec<source_location> copy_locus;
+  auto_vec<location_t> copy_locus;
 };
 
 
@@ -168,14 +172,43 @@ struct elim_graph
    use its location.  Otherwise search instructions in predecessors
    of E for a location, and use that one.  That makes sense because
    we insert on edges for PHI nodes, and effects of PHIs happen on
-   the end of the predecessor conceptually.  */
+   the end of the predecessor conceptually.  An exception is made
+   for EH edges because we don't want to drag the source location
+   of unrelated statements at the beginning of handlers; they would
+   be further reused for various EH constructs, which would damage
+   the coverage information.  */
 
 static void
 set_location_for_edge (edge e)
 {
   if (e->goto_locus)
+    set_curr_insn_location (e->goto_locus);
+  else if (e->flags & EDGE_EH)
     {
-      set_curr_insn_location (e->goto_locus);
+      basic_block bb = e->dest;
+      gimple_stmt_iterator gsi;
+
+      do
+       {
+         for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+           {
+             gimple *stmt = gsi_stmt (gsi);
+             if (is_gimple_debug (stmt))
+               continue;
+             if (gimple_has_location (stmt) || gimple_block (stmt))
+               {
+                 set_curr_insn_location (gimple_location (stmt));
+                 return;
+               }
+           }
+         /* Nothing found in this basic block.  Make a half-assed attempt
+            to continue with another block.  */
+         if (single_succ_p (bb))
+           bb = single_succ (bb);
+         else
+           bb = e->dest;
+       }
+      while (bb != e->dest);
     }
   else
     {
@@ -235,13 +268,13 @@ emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
 /* Insert a copy instruction from partition SRC to DEST onto edge E.  */
 
 static void
-insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
+insert_partition_copy_on_edge (edge e, int dest, int src, location_t locus)
 {
   tree var;
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file,
-              "Inserting a partition copy on edge BB%d->BB%d :"
+              "Inserting a partition copy on edge BB%d->BB%d : "
               "PART.%d = PART.%d",
               e->src->index,
               e->dest->index, dest, src);
@@ -269,7 +302,7 @@ insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
    onto edge E.  */
 
 static void
-insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
+insert_value_copy_on_edge (edge e, int dest, tree src, location_t locus)
 {
   rtx dest_rtx, seq, x;
   machine_mode dest_mode, src_mode;
@@ -330,7 +363,7 @@ insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
 
 static void
 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
-                           source_location locus)
+                           location_t locus)
 {
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
@@ -364,7 +397,7 @@ insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
    onto edge E.  */
 
 static void
-insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
+insert_part_to_rtx_on_edge (edge e, rtx dest, int src, location_t locus)
 {
   tree var;
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -441,7 +474,7 @@ elim_graph_add_node (elim_graph *g, int node)
 /* Add the edge PRED->SUCC to graph G.  */
 
 static inline void
-elim_graph_add_edge (elim_graph *g, int pred, int succ, source_location locus)
+elim_graph_add_edge (elim_graph *g, int pred, int succ, location_t locus)
 {
   g->edge_list.safe_push (pred);
   g->edge_list.safe_push (succ);
@@ -453,7 +486,7 @@ elim_graph_add_edge (elim_graph *g, int pred, int succ, source_location locus)
    return the successor node.  -1 is returned if there is no such edge.  */
 
 static inline int
-elim_graph_remove_succ_edge (elim_graph *g, int node, source_location *locus)
+elim_graph_remove_succ_edge (elim_graph *g, int node, location_t *locus)
 {
   int y;
   unsigned x;
@@ -553,7 +586,7 @@ eliminate_build (elim_graph *g)
   for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
     {
       gphi *phi = gsi.phi ();
-      source_location locus;
+      location_t locus;
 
       p0 = var_to_partition (g->map, gimple_phi_result (phi));
       /* Ignore results which are not in partitions.  */
@@ -561,7 +594,11 @@ eliminate_build (elim_graph *g)
        continue;
 
       Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
-      locus = gimple_phi_arg_location_from_edge (phi, g->e);
+      /* See set_location_for_edge for the rationale.  */
+      if (g->e->flags & EDGE_EH)
+       locus = UNKNOWN_LOCATION;
+      else
+       locus = gimple_phi_arg_location_from_edge (phi, g->e);
 
       /* If this argument is a constant, or a SSA_NAME which is being
         left in SSA form, just queue a copy to be emitted on this
@@ -594,7 +631,7 @@ static void
 elim_forward (elim_graph *g, int T)
 {
   int S;
-  source_location locus;
+  location_t locus;
 
   bitmap_set_bit (g->visited, T);
   FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
@@ -612,7 +649,7 @@ static int
 elim_unvisited_predecessor (elim_graph *g, int T)
 {
   int P;
-  source_location locus;
+  location_t locus;
 
   FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
     {
@@ -628,7 +665,7 @@ static void
 elim_backward (elim_graph *g, int T)
 {
   int P;
-  source_location locus;
+  location_t locus;
 
   bitmap_set_bit (g->visited, T);
   FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
@@ -650,6 +687,8 @@ get_temp_reg (tree name)
   tree type = TREE_TYPE (name);
   int unsignedp;
   machine_mode reg_mode = promote_ssa_mode (name, &unsignedp);
+  if (reg_mode == BLKmode)
+    return assign_temp (type, 0, 0);
   rtx x = gen_reg_rtx (reg_mode);
   if (POINTER_TYPE_P (type))
     mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
@@ -663,7 +702,7 @@ static void
 elim_create (elim_graph *g, int T)
 {
   int P, S;
-  source_location locus;
+  location_t locus;
 
   if (elim_unvisited_predecessor (g, T))
     {
@@ -738,7 +777,7 @@ eliminate_phi (edge e, elim_graph *g)
     {
       int dest;
       tree src;
-      source_location locus;
+      location_t locus;
 
       src = g->const_copies.pop ();
       dest = g->const_dests.pop ();
@@ -806,26 +845,7 @@ eliminate_useless_phis (void)
          gphi *phi = gsi.phi ();
          result = gimple_phi_result (phi);
          if (virtual_operand_p (result))
-           {
-             /* There should be no arguments which are not virtual, or the
-                results will be incorrect.  */
-             if (flag_checking)
-               for (size_t i = 0; i < gimple_phi_num_args (phi); i++)
-                 {
-                   tree arg = PHI_ARG_DEF (phi, i);
-                   if (TREE_CODE (arg) == SSA_NAME
-                       && !virtual_operand_p (arg))
-                     {
-                       fprintf (stderr, "Argument of PHI is not virtual (");
-                       print_generic_expr (stderr, arg, TDF_SLIM);
-                       fprintf (stderr, "), but the result is :");
-                       print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
-                       internal_error ("SSA corruption");
-                     }
-                 }
-
-             remove_phi_node (&gsi, true);
-           }
+           remove_phi_node (&gsi, true);
           else
            {
              /* Also remove real PHIs with no uses.  */
@@ -887,6 +907,102 @@ rewrite_trees (var_map map)
     }
 }
 
+/* Create a default def for VAR.  */
+
+static void
+create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
+{
+  if (!is_gimple_reg (var))
+    return;
+
+  tree ssa = get_or_create_ssa_default_def (cfun, var);
+  gcc_assert (ssa);
+}
+
+/* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
+   assign_parms may ask for a default partition.  */
+
+static void
+for_all_parms (void (*callback)(tree var, void *arg), void *arg)
+{
+  for (tree var = DECL_ARGUMENTS (current_function_decl); var;
+       var = DECL_CHAIN (var))
+    callback (var, arg);
+  if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
+    callback (DECL_RESULT (current_function_decl), arg);
+  if (cfun->static_chain_decl)
+    callback (cfun->static_chain_decl, arg);
+}
+
+/* We need to pass two arguments to set_parm_default_def_partition,
+   but for_all_parms only supports one.  Use a pair.  */
+
+typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
+
+/* Set in ARG's PARTS bitmap the bit corresponding to the partition in
+   ARG's MAP containing VAR's default def.  */
+
+static void
+set_parm_default_def_partition (tree var, void *arg_)
+{
+  parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
+  var_map map = arg->first;
+  bitmap parts = arg->second;
+
+  if (!is_gimple_reg (var))
+    return;
+
+  tree ssa = ssa_default_def (cfun, var);
+  gcc_assert (ssa);
+
+  int version = var_to_partition (map, ssa);
+  gcc_assert (version != NO_PARTITION);
+
+  bool changed = bitmap_set_bit (parts, version);
+  gcc_assert (changed);
+}
+
+/* Allocate and return a bitmap that has a bit set for each partition
+   that contains a default def for a parameter.  */
+
+static bitmap
+get_parm_default_def_partitions (var_map map)
+{
+  bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
+
+  parm_default_def_partition_arg
+    arg = std::make_pair (map, parm_default_def_parts);
+
+  for_all_parms (set_parm_default_def_partition, &arg);
+
+  return parm_default_def_parts;
+}
+
+/* Allocate and return a bitmap that has a bit set for each partition
+   that contains an undefined value.  */
+
+static bitmap
+get_undefined_value_partitions (var_map map)
+{
+  bitmap undefined_value_parts = BITMAP_ALLOC (NULL);
+
+  for (unsigned int i = 1; i < num_ssa_names; i++)
+    {
+      tree var = ssa_name (i);
+      if (var
+         && !virtual_operand_p (var)
+         && !has_zero_uses (var)
+         && ssa_undefined_value_p (var))
+       {
+         const int p = var_to_partition (map, var);
+         if (p != NO_PARTITION)
+           bitmap_set_bit (undefined_value_parts, p);
+       }
+    }
+
+  return undefined_value_parts;
+}
+
 /* Given the out-of-ssa info object SA (with prepared partitions)
    eliminate all phi nodes in all basic blocks.  Afterwards no
    basic block will have phi nodes anymore and there are possibly
@@ -944,7 +1060,9 @@ remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
   bitmap values = NULL;
   var_map map;
 
-  map = coalesce_ssa_name ();
+  for_all_parms (create_default_def, NULL);
+  map = init_var_map (num_ssa_names);
+  coalesce_ssa_name (map);
 
   /* Return to viewing the variable list as just all reference variables after
      coalescing has been performed.  */
@@ -968,6 +1086,7 @@ remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
   sa->map = map;
   sa->values = values;
   sa->partitions_for_parm_default_defs = get_parm_default_def_partitions (map);
+  sa->partitions_for_undefined_values = get_undefined_value_partitions (map);
 }
 
 
@@ -1069,15 +1188,19 @@ insert_backedge_copies (void)
            {
              tree arg = gimple_phi_arg_def (phi, i);
              edge e = gimple_phi_arg_edge (phi, i);
+             /* We are only interested in copies emitted on critical
+                 backedges.  */
+             if (!(e->flags & EDGE_DFS_BACK)
+                 || !EDGE_CRITICAL_P (e))
+               continue;
 
              /* If the argument is not an SSA_NAME, then we will need a
-                constant initialization.  If the argument is an SSA_NAME with
-                a different underlying variable then a copy statement will be
-                needed.  */
-             if ((e->flags & EDGE_DFS_BACK)
-                 && (TREE_CODE (arg) != SSA_NAME
-                     || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
-                     || trivially_conflicts_p (bb, result, arg)))
+                constant initialization.  If the argument is an SSA_NAME then
+                a copy statement may be needed.  First handle the case
+                where we cannot insert before the argument definition.  */
+             if (TREE_CODE (arg) != SSA_NAME
+                 || (gimple_code (SSA_NAME_DEF_STMT (arg)) == GIMPLE_PHI
+                     && trivially_conflicts_p (bb, result, arg)))
                {
                  tree name;
                  gassign *stmt;
@@ -1124,6 +1247,34 @@ insert_backedge_copies (void)
                    gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
                  SET_PHI_ARG_DEF (phi, i, name);
                }
+             /* Insert a copy before the definition of the backedge value
+                and adjust all conflicting uses.  */
+             else if (trivially_conflicts_p (bb, result, arg))
+               {
+                 gimple *def = SSA_NAME_DEF_STMT (arg);
+                 if (gimple_nop_p (def)
+                     || gimple_code (def) == GIMPLE_PHI)
+                   continue;
+                 tree name = copy_ssa_name (result);
+                 gimple *stmt = gimple_build_assign (name, result);
+                 imm_use_iterator imm_iter;
+                 gimple *use_stmt;
+                 /* The following matches trivially_conflicts_p.  */
+                 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, result)
+                   {
+                     if (gimple_bb (use_stmt) != bb
+                         || (gimple_code (use_stmt) != GIMPLE_PHI
+                             && (maybe_renumber_stmts_bb (bb), true)
+                             && gimple_uid (use_stmt) > gimple_uid (def)))
+                       {
+                         use_operand_p use;
+                         FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
+                           SET_USE (use, name);
+                       }
+                   }
+                 gimple_stmt_iterator gsi = gsi_for_stmt (def);
+                 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
+               }
            }
        }
 
@@ -1143,6 +1294,7 @@ finish_out_of_ssa (struct ssaexpand *sa)
     BITMAP_FREE (sa->values);
   delete_var_map (sa->map);
   BITMAP_FREE (sa->partitions_for_parm_default_defs);
+  BITMAP_FREE (sa->partitions_for_undefined_values);
   memset (sa, 0, sizeof *sa);
 }