]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/tree-into-ssa.c
Update copyright years.
[thirdparty/gcc.git] / gcc / tree-into-ssa.c
index ceafa68b9ea0c7f46c9cd4a4f3e41a053f67d102..ef99060403039082435c0a6fc6d8922e3b375237 100644 (file)
@@ -1,5 +1,5 @@
 /* Rewrite a program in Normal form into SSA.
-   Copyright (C) 2001-2016 Free Software Foundation, Inc.
+   Copyright (C) 2001-2021 Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
 This file is part of GCC.
@@ -38,6 +38,10 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa.h"
 #include "domwalk.h"
 #include "statistics.h"
+#include "stringpool.h"
+#include "attribs.h"
+#include "asan.h"
+#include "attr-fnspec.h"
 
 #define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y))
 
@@ -320,7 +324,7 @@ get_ssa_name_ann (tree name)
 
   /* Re-allocate the vector at most once per update/into-SSA.  */
   if (ver >= len)
-    info_for_ssa_name.safe_grow_cleared (num_ssa_names);
+    info_for_ssa_name.safe_grow_cleared (num_ssa_names, true);
 
   /* But allocate infos lazily.  */
   info = info_for_ssa_name[ver];
@@ -937,14 +941,18 @@ mark_phi_for_rewrite (basic_block bb, gphi *phi)
   if (!blocks_with_phis_to_rewrite)
     return;
 
-  bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
-
-  n = (unsigned) last_basic_block_for_fn (cfun) + 1;
-  if (phis_to_rewrite.length () < n)
-    phis_to_rewrite.safe_grow_cleared (n);
+  if (bitmap_set_bit (blocks_with_phis_to_rewrite, idx))
+    {
+      n = (unsigned) last_basic_block_for_fn (cfun) + 1;
+      if (phis_to_rewrite.length () < n)
+       phis_to_rewrite.safe_grow_cleared (n, true);
 
-  phis = phis_to_rewrite[idx];
-  phis.reserve (10);
+      phis = phis_to_rewrite[idx];
+      gcc_assert (!phis.exists ());
+      phis.create (10);
+    }
+  else
+    phis = phis_to_rewrite[idx];
 
   phis.safe_push (phi);
   phis_to_rewrite[idx] = phis;
@@ -1065,6 +1073,59 @@ insert_phi_nodes (bitmap_head *dfs)
 
   timevar_push (TV_TREE_INSERT_PHI_NODES);
 
+  /* When the gimplifier introduces SSA names it cannot easily avoid
+     situations where abnormal edges added by CFG construction break
+     the use-def dominance requirement.  For this case rewrite SSA
+     names with broken use-def dominance out-of-SSA and register them
+     for PHI insertion.  We only need to do this if abnormal edges
+     can appear in the function.  */
+  tree name;
+  if (cfun->calls_setjmp
+      || cfun->has_nonlocal_label)
+    FOR_EACH_SSA_NAME (i, name, cfun)
+      {
+       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
+       if (SSA_NAME_IS_DEFAULT_DEF (name))
+         continue;
+
+       basic_block def_bb = gimple_bb (def_stmt);
+       imm_use_iterator it;
+       gimple *use_stmt;
+       bool need_phis = false;
+       FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
+         {
+           basic_block use_bb = gimple_bb (use_stmt);
+           if (use_bb != def_bb
+               && ! dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
+             need_phis = true;
+         }
+       if (need_phis)
+         {
+           tree var = create_tmp_reg (TREE_TYPE (name));
+           use_operand_p use_p;
+           FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
+             {
+               basic_block use_bb = gimple_bb (use_stmt);
+               FOR_EACH_IMM_USE_ON_STMT (use_p, it)
+                   SET_USE (use_p, var);
+               update_stmt (use_stmt);
+               set_livein_block (var, use_bb);
+               set_rewrite_uses (use_stmt, true);
+               bitmap_set_bit (interesting_blocks, use_bb->index);
+             }
+           def_operand_p def_p;
+           ssa_op_iter dit;
+           FOR_EACH_SSA_DEF_OPERAND (def_p, def_stmt, dit, SSA_OP_DEF)
+             if (DEF_FROM_PTR (def_p) == name)
+               SET_DEF (def_p, var);
+           update_stmt (def_stmt);
+           set_def_block (var, def_bb, false);
+           set_register_defs (def_stmt, true);
+           bitmap_set_bit (interesting_blocks, def_bb->index);
+           release_ssa_name (name);
+         }
+      }
+
   auto_vec<var_info *> vars (var_infos->elements ());
   FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi)
     if (info->info.need_phi_state != NEED_PHI_STATE_NO)
@@ -1170,6 +1231,8 @@ get_reaching_def (tree var)
   if (currdef == NULL_TREE)
     {
       tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
+      if (! sym)
+       sym = create_tmp_reg (TREE_TYPE (var));
       currdef = get_or_create_ssa_default_def (cfun, sym);
     }
 
@@ -1229,7 +1292,7 @@ rewrite_debug_stmt_uses (gimple *stmt)
                  def_temp = gimple_build_debug_source_bind (def, var, NULL);
                  DECL_ARTIFICIAL (def) = 1;
                  TREE_TYPE (def) = TREE_TYPE (var);
-                 DECL_MODE (def) = DECL_MODE (var);
+                 SET_DECL_MODE (def, DECL_MODE (var));
                  gsi =
                 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
                  gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
@@ -1339,8 +1402,7 @@ rewrite_stmt (gimple_stmt_iterator *si)
          {
            /* If we rewrite a DECL into SSA form then drop its
               clobber stmts and replace uses with a new default def.  */
-           gcc_checking_assert (TREE_CODE (var) == VAR_DECL
-                                && !gimple_vdef (stmt));
+           gcc_checking_assert (VAR_P (var) && !gimple_vdef (stmt));
            gsi_replace (si, gimple_build_nop (), true);
            register_new_def (get_or_create_ssa_default_def (cfun, var), var);
            break;
@@ -1350,6 +1412,10 @@ rewrite_stmt (gimple_stmt_iterator *si)
        SET_DEF (def_p, name);
        register_new_def (DEF_FROM_PTR (def_p), var);
 
+       /* Do not insert debug stmts if the stmt ends the BB.  */
+       if (stmt_ends_bb_p (stmt))
+         continue;
+       
        tracked_var = target_for_debug_bind (var);
        if (tracked_var)
          {
@@ -1398,7 +1464,8 @@ rewrite_add_phi_arguments (basic_block bb)
 class rewrite_dom_walker : public dom_walker
 {
 public:
-  rewrite_dom_walker (cdi_direction direction) : dom_walker (direction) {}
+  rewrite_dom_walker (cdi_direction direction)
+    : dom_walker (direction, ALL_BLOCKS, NULL) {}
 
   virtual edge before_dom_children (basic_block);
   virtual void after_dom_children (basic_block);
@@ -1547,10 +1614,10 @@ dump_defs_stack (FILE *file, int n)
        }
 
       fprintf (file, "    Previous CURRDEF (");
-      print_generic_expr (file, var, 0);
+      print_generic_expr (file, var);
       fprintf (file, ") = ");
       if (name)
-       print_generic_expr (file, name, 0);
+       print_generic_expr (file, name);
       else
        fprintf (file, "<NIL>");
       fprintf (file, "\n");
@@ -1586,10 +1653,10 @@ dump_currdefs (FILE *file)
     {
       common_info *info = get_common_info (var);
       fprintf (file, "CURRDEF (");
-      print_generic_expr (file, var, 0);
+      print_generic_expr (file, var);
       fprintf (file, ") = ");
       if (info->current_def)
-       print_generic_expr (file, info->current_def, 0);
+       print_generic_expr (file, info->current_def);
       else
        fprintf (file, "<NIL>");
       fprintf (file, "\n");
@@ -1800,6 +1867,26 @@ maybe_replace_use_in_debug_stmt (use_operand_p use_p)
 }
 
 
+/* If DEF has x_5 = ASAN_POISON () as its current def, add
+   ASAN_POISON_USE (x_5) stmt before GSI to denote the stmt writes into
+   a poisoned (out of scope) variable.  */
+
+static void
+maybe_add_asan_poison_write (tree def, gimple_stmt_iterator *gsi)
+{
+  tree cdef = get_current_def (def);
+  if (cdef != NULL
+      && TREE_CODE (cdef) == SSA_NAME
+      && gimple_call_internal_p (SSA_NAME_DEF_STMT (cdef), IFN_ASAN_POISON))
+    {
+      gcall *call
+       = gimple_build_call_internal (IFN_ASAN_POISON_USE, 1, cdef);
+      gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
+      gsi_insert_before (gsi, call, GSI_SAME_STMT);
+    }
+}
+
+
 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
    or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
    register it as the current definition for the names replaced by
@@ -1821,7 +1908,7 @@ maybe_register_def (def_operand_p def_p, gimple *stmt,
        {
          if (gimple_clobber_p (stmt) && is_gimple_reg (sym))
            {
-             gcc_checking_assert (TREE_CODE (sym) == VAR_DECL);
+             gcc_checking_assert (VAR_P (sym));
              /* Replace clobber stmts with a default def. This new use of a
                 default definition may make it look like SSA_NAMEs have
                 conflicting lifetimes, so we need special code to let them
@@ -1830,7 +1917,11 @@ maybe_register_def (def_operand_p def_p, gimple *stmt,
              def = get_or_create_ssa_default_def (cfun, sym);
            }
          else
-           def = make_ssa_name (def, stmt);
+           {
+             if (asan_sanitize_use_after_scope ())
+               maybe_add_asan_poison_write (def, &gsi);
+             def = make_ssa_name (def, stmt);
+           }
          SET_DEF (def_p, def);
 
          tree tracked_var = target_for_debug_bind (sym);
@@ -2029,7 +2120,7 @@ rewrite_update_phi_arguments (basic_block bb)
           /* Update the argument if there is a reaching def.  */
          if (reaching_def)
            {
-             source_location locus;
+             location_t locus;
              int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
 
              SET_USE (arg_p, reaching_def);
@@ -2064,7 +2155,8 @@ rewrite_update_phi_arguments (basic_block bb)
 class rewrite_update_dom_walker : public dom_walker
 {
 public:
-  rewrite_update_dom_walker (cdi_direction direction) : dom_walker (direction) {}
+  rewrite_update_dom_walker (cdi_direction direction)
+    : dom_walker (direction, ALL_BLOCKS, NULL) {}
 
   virtual edge before_dom_children (basic_block);
   virtual void after_dom_children (basic_block);
@@ -2233,7 +2325,7 @@ private:
 };
 
 mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction)
-  : dom_walker (direction), m_kills (BITMAP_ALLOC (NULL))
+  : dom_walker (direction, ALL_BLOCKS, NULL), m_kills (BITMAP_ALLOC (NULL))
 {
 }
 
@@ -2341,7 +2433,11 @@ pass_build_ssa::execute (function *fun)
 {
   bitmap_head *dfs;
   basic_block bb;
-  unsigned i;
+
+  /* Increase the set of variables we can rewrite into SSA form
+     by clearing TREE_ADDRESSABLE and transform the IL to support this.  */
+  if (optimize)
+    execute_update_addresses_taken ();
 
   /* Initialize operand data structures.  */
   init_ssa_operands (fun);
@@ -2385,20 +2481,43 @@ pass_build_ssa::execute (function *fun)
   /* Try to get rid of all gimplifier generated temporaries by making
      its SSA names anonymous.  This way we can garbage collect them
      all after removing unused locals which we do in our TODO.  */
-  for (i = 1; i < num_ssa_names; ++i)
+  unsigned i;
+  tree name;
+
+  FOR_EACH_SSA_NAME (i, name, cfun)
     {
-      tree decl, name = ssa_name (i);
-      if (!name
-         || SSA_NAME_IS_DEFAULT_DEF (name))
+      if (SSA_NAME_IS_DEFAULT_DEF (name))
        continue;
-      decl = SSA_NAME_VAR (name);
+      tree decl = SSA_NAME_VAR (name);
       if (decl
-         && TREE_CODE (decl) == VAR_DECL
+         && VAR_P (decl)
          && !VAR_DECL_IS_VIRTUAL_OPERAND (decl)
          && DECL_IGNORED_P (decl))
        SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl));
     }
 
+  /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY.  */
+  tree fnspec_tree
+        = lookup_attribute ("fn spec",
+                            TYPE_ATTRIBUTES (TREE_TYPE (fun->decl)));
+  if (fnspec_tree)
+    {
+      attr_fnspec fnspec (TREE_VALUE (TREE_VALUE (fnspec_tree)));
+      unsigned i = 0;
+      for (tree arg = DECL_ARGUMENTS (cfun->decl);
+          arg; arg = DECL_CHAIN (arg), ++i)
+       {
+         if (!fnspec.arg_specified_p (i))
+          break;
+         if (fnspec.arg_readonly_p (i))
+           {
+             tree name = ssa_default_def (fun, arg);
+             if (name)
+               SSA_NAME_POINTS_TO_READONLY_MEMORY (name) = 1;
+           }
+       }
+    }
+
   return 0;
 }
 
@@ -2478,11 +2597,9 @@ mark_use_interesting (tree var, gimple *stmt, basic_block bb,
     }
 }
 
-
-/* Do a dominator walk starting at BB processing statements that
-   reference symbols in SSA operands.  This is very similar to
-   mark_def_sites, but the scan handles statements whose operands may
-   already be SSA names.
+/* Processing statements in BB that reference symbols in SSA operands.
+   This is very similar to mark_def_sites, but the scan handles
+   statements whose operands may already be SSA names.
 
    If INSERT_PHI_P is true, mark those uses as live in the
    corresponding block.  This is later used by the PHI placement
@@ -2495,9 +2612,8 @@ mark_use_interesting (tree var, gimple *stmt, basic_block bb,
           that.  */
 
 static void
-prepare_block_for_update (basic_block bb, bool insert_phi_p)
+prepare_block_for_update_1 (basic_block bb, bool insert_phi_p)
 {
-  basic_block son;
   edge e;
   edge_iterator ei;
 
@@ -2579,13 +2695,51 @@ prepare_block_for_update (basic_block bb, bool insert_phi_p)
        }
     }
 
-  /* Now visit all the blocks dominated by BB.  */
-  for (son = first_dom_son (CDI_DOMINATORS, bb);
-       son;
-       son = next_dom_son (CDI_DOMINATORS, son))
-    prepare_block_for_update (son, insert_phi_p);
 }
 
+/* Do a dominator walk starting at BB processing statements that
+   reference symbols in SSA operands.  This is very similar to
+   mark_def_sites, but the scan handles statements whose operands may
+   already be SSA names.
+
+   If INSERT_PHI_P is true, mark those uses as live in the
+   corresponding block.  This is later used by the PHI placement
+   algorithm to make PHI pruning decisions.
+
+   FIXME.  Most of this would be unnecessary if we could associate a
+          symbol to all the SSA names that reference it.  But that
+          sounds like it would be expensive to maintain.  Still, it
+          would be interesting to see if it makes better sense to do
+          that.  */
+static void
+prepare_block_for_update (basic_block bb, bool insert_phi_p)
+{
+  size_t sp = 0;
+  basic_block *worklist;
+
+  /* Allocate the worklist.  */
+  worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
+  /* Add the BB to the worklist.  */
+  worklist[sp++] = bb;
+
+  while (sp)
+    {
+      basic_block bb;
+      basic_block son;
+
+      /* Pick a block from the worklist.  */
+      bb = worklist[--sp];
+
+      prepare_block_for_update_1 (bb, insert_phi_p);
+
+      /* Now add all the blocks dominated by BB to the worklist.  */
+      for (son = first_dom_son (CDI_DOMINATORS, bb);
+          son;
+          son = next_dom_son (CDI_DOMINATORS, son))
+       worklist[sp++] = son;
+    }
+  free (worklist);
+}
 
 /* Helper for prepare_names_to_update.  Mark all the use sites for
    NAME as interesting.  BLOCKS and INSERT_PHI_P are as in
@@ -2699,13 +2853,13 @@ dump_names_replaced_by (FILE *file, tree name)
   bitmap old_set;
   bitmap_iterator bi;
 
-  print_generic_expr (file, name, 0);
+  print_generic_expr (file, name);
   fprintf (file, " -> { ");
 
   old_set = names_replaced_by (name);
   EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
     {
-      print_generic_expr (file, ssa_name (i), 0);
+      print_generic_expr (file, ssa_name (i));
       fprintf (file, " ");
     }
 
@@ -2757,7 +2911,7 @@ dump_update_ssa (FILE *file)
       fprintf (file, "\nSSA names to release after updating the SSA web\n\n");
       EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
        {
-         print_generic_expr (file, ssa_name (i), 0);
+         print_generic_expr (file, ssa_name (i));
          fprintf (file, " ");
        }
       fprintf (file, "\n");
@@ -2826,11 +2980,7 @@ delete_update_ssa (void)
 
   if (blocks_with_phis_to_rewrite)
     EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
-      {
-       vec<gphi *> phis = phis_to_rewrite[i];
-       phis.release ();
-       phis_to_rewrite[i].create (0);
-      }
+      phis_to_rewrite[i].release ();
 
   BITMAP_FREE (blocks_with_phis_to_rewrite);
   BITMAP_FREE (blocks_to_update);
@@ -3199,10 +3349,10 @@ update_ssa (unsigned update_flags)
 
                  if (SSA_NAME_IN_FREE_LIST (use))
                    {
-                     error ("statement uses released SSA name:");
+                     error ("statement uses released SSA name");
                      debug_gimple_stmt (stmt);
                      fprintf (stderr, "The use of ");
-                     print_generic_expr (stderr, use, 0);
+                     print_generic_expr (stderr, use);
                      fprintf (stderr," should have been replaced\n");
                      err = true;
                    }
@@ -3283,12 +3433,12 @@ update_ssa (unsigned update_flags)
         placement heuristics.  */
       prepare_block_for_update (start_bb, insert_phi_p);
 
+      tree name;
+
       if (flag_checking)
-       for (i = 1; i < num_ssa_names; ++i)
+       FOR_EACH_SSA_NAME (i, name, cfun)
          {
-           tree name = ssa_name (i);
-           if (!name
-               || virtual_operand_p (name))
+           if (virtual_operand_p (name))
              continue;
 
            /* For all but virtual operands, which do not have SSA names