IA MCU psABI support: changes to libraries
[gcc.git] / gcc / ipa-reference.c
index d999cb1a6b2a1895ea04dd6359bb39c713b45699..d53abb6ffe885f7df978f17477c963f50102974b 100644 (file)
@@ -1,5 +1,5 @@
 /* Callgraph based analysis of static variables.
-   Copyright (C) 2004-2014 Free Software Foundation, Inc.
+   Copyright (C) 2004-2015 Free Software Foundation, Inc.
    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
 
 This file is part of GCC.
@@ -40,18 +40,26 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
 #include "tree.h"
+#include "fold-const.h"
 #include "calls.h"
+#include "predict.h"
+#include "hard-reg-set.h"
+#include "function.h"
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "tree-inline.h"
 #include "tree-pass.h"
 #include "splay-tree.h"
+#include "cgraph.h"
 #include "ipa-utils.h"
+#include "bitmap.h"
 #include "ipa-reference.h"
 #include "flags.h"
 #include "diagnostic.h"
@@ -112,6 +120,9 @@ static splay_tree reference_vars_to_consider;
    static we are considering.  This is added to the local info when asm
    code is found that clobbers all memory.  */
 static bitmap all_module_statics;
+/* Set of all statics that should be ignored becuase they are touched by
+   -fno-ipa-reference code.  */
+static bitmap ignore_module_statics;
 
 /* Obstack holding bitmaps of local analysis (live from analysis to
    propagation)  */
@@ -127,6 +138,7 @@ static struct cgraph_node_hook_list *node_removal_hook_holder;
    Indexed by UID of call graph nodes.  */
 static vec<ipa_reference_vars_info_t> ipa_reference_vars_vector;
 
+/* TODO: find a place where we should release the vector.  */
 static vec<ipa_reference_optimization_summary_t> ipa_reference_opt_sum_vector;
 
 /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
@@ -178,6 +190,9 @@ set_reference_optimization_summary (struct cgraph_node *node,
 bitmap
 ipa_reference_get_not_read_global (struct cgraph_node *fn)
 {
+  if (!opt_for_fn (fn->decl, flag_ipa_reference)
+      || !opt_for_fn (current_function_decl, flag_ipa_reference))
+    return NULL;
   ipa_reference_optimization_summary_t info =
     get_reference_optimization_summary (fn->function_symbol (NULL));
   if (info)
@@ -196,6 +211,9 @@ ipa_reference_get_not_read_global (struct cgraph_node *fn)
 bitmap
 ipa_reference_get_not_written_global (struct cgraph_node *fn)
 {
+  if (!opt_for_fn (fn->decl, flag_ipa_reference)
+      || !opt_for_fn (current_function_decl, flag_ipa_reference))
+    return NULL;
   ipa_reference_optimization_summary_t info =
     get_reference_optimization_summary (fn);
   if (info)
@@ -207,21 +225,6 @@ ipa_reference_get_not_written_global (struct cgraph_node *fn)
 }
 
 \f
-
-/* Add VAR to all_module_statics and the two
-   reference_vars_to_consider* sets.  */
-
-static inline void
-add_static_var (tree var)
-{
-  int uid = DECL_UID (var);
-  gcc_assert (TREE_CODE (var) == VAR_DECL);
-  if (dump_file)
-    splay_tree_insert (reference_vars_to_consider,
-                      uid, (splay_tree_value)var);
-  bitmap_set_bit (all_module_statics, uid);
-}
-
 /* Return true if the variable T is the right kind of static variable to
    perform compilation unit scope escape analysis.  */
 
@@ -253,12 +256,8 @@ is_proper_for_analysis (tree t)
     return false;
 
   /* TODO: Check aliases.  */
-
-  /* This is a variable we care about.  Check if we have seen it
-     before, and if not add it the set of variables we care about.  */
-  if (all_module_statics
-      && !bitmap_bit_p (all_module_statics, DECL_UID (t)))
-    add_static_var (t);
+  if (bitmap_bit_p (ignore_module_statics, DECL_UID (t)))
+    return false;
 
   return true;
 }
@@ -361,8 +360,9 @@ propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x
 
       /* Only look into nodes we can propagate something.  */
       int flags = flags_from_decl_or_type (y->decl);
-      if (avail > AVAIL_INTERPOSABLE
-         || (avail == AVAIL_INTERPOSABLE && (flags & ECF_LEAF)))
+      if (opt_for_fn (y->decl, flag_ipa_reference)
+         && (avail > AVAIL_INTERPOSABLE
+             || (avail == AVAIL_INTERPOSABLE && (flags & ECF_LEAF))))
        {
          if (get_reference_vars_info (y))
            {
@@ -387,7 +387,7 @@ propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x
                 seems so to local analysis.  If we cannot return from
                 the function, we can safely ignore the call.  */
              if ((flags & ECF_PURE)
-                 || cgraph_edge_cannot_lead_to_return (e))
+                 || e->cannot_lead_to_return_p ())
                continue;
 
              union_static_var_sets (x_global->statics_written,
@@ -399,17 +399,17 @@ propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x
     }
 }
 
+static bool ipa_init_p = false;
+
 /* The init routine for analyzing global static variable usage.  See
    comments at top for description.  */
 static void
 ipa_init (void)
 {
-  static bool init_p = false;
-
-  if (init_p)
+  if (ipa_init_p)
     return;
 
-  init_p = true;
+  ipa_init_p = true;
 
   if (dump_file)
     reference_vars_to_consider = splay_tree_new (splay_tree_compare_ints, 0, 0);
@@ -417,11 +417,12 @@ ipa_init (void)
   bitmap_obstack_initialize (&local_info_obstack);
   bitmap_obstack_initialize (&optimization_summary_obstack);
   all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
+  ignore_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
 
   node_removal_hook_holder =
-      cgraph_add_node_removal_hook (&remove_node_data, NULL);
+      symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
   node_duplication_hook_holder =
-      cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
+      symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
 }
 
 
@@ -454,6 +455,8 @@ analyze_function (struct cgraph_node *fn)
   int i;
   tree var;
 
+  if (!opt_for_fn (fn->decl, flag_ipa_reference))
+    return;
   local = init_function_info (fn);
   for (i = 0; fn->iterate_reference (i, ref); i++)
     {
@@ -462,6 +465,15 @@ analyze_function (struct cgraph_node *fn)
       var = ref->referred->decl;
       if (!is_proper_for_analysis (var))
        continue;
+      /* This is a variable we care about.  Check if we have seen it
+        before, and if not add it the set of variables we care about.  */
+      if (all_module_statics
+         && bitmap_set_bit (all_module_statics, DECL_UID (var)))
+       {
+         if (dump_file)
+           splay_tree_insert (reference_vars_to_consider,
+                              DECL_UID (var), (splay_tree_value)var);
+       }
       switch (ref->use)
        {
        case IPA_REF_LOAD:
@@ -538,6 +550,22 @@ generate_summary (void)
   ipa_init ();
 
   /* Process all of the functions next.  */
+  FOR_EACH_DEFINED_FUNCTION (node)
+    if (!node->alias && !opt_for_fn (node->decl, flag_ipa_reference))
+      {
+        struct ipa_ref *ref = NULL;
+        int i;
+        tree var;
+       for (i = 0; node->iterate_reference (i, ref); i++)
+         {
+           if (!is_a <varpool_node *> (ref->referred))
+             continue;
+           var = ref->referred->decl;
+           if (!is_proper_for_analysis (var))
+             continue;
+           bitmap_set_bit (ignore_module_statics, DECL_UID (var));
+         }
+      }
   FOR_EACH_DEFINED_FUNCTION (node)
     analyze_function (node);
 
@@ -550,7 +578,8 @@ generate_summary (void)
 
   if (dump_file)
     FOR_EACH_DEFINED_FUNCTION (node)
-      if (node->get_availability () >= AVAIL_INTERPOSABLE)
+      if (node->get_availability () >= AVAIL_INTERPOSABLE
+         && opt_for_fn (node->decl, flag_ipa_reference))
        {
          ipa_reference_local_vars_info_t l;
          unsigned int index;
@@ -587,7 +616,7 @@ read_write_all_from_decl (struct cgraph_node *node,
   tree decl = node->decl;
   int flags = flags_from_decl_or_type (decl);
   if ((flags & ECF_LEAF)
-      && node->get_availability () <= AVAIL_INTERPOSABLE)
+      && node->get_availability () < AVAIL_INTERPOSABLE)
     ;
   else if (flags & ECF_CONST)
     ;
@@ -620,7 +649,8 @@ get_read_write_all_from_node (struct cgraph_node *node,
   struct cgraph_edge *e, *ie;
 
   /* When function is overwritable, we can not assume anything.  */
-  if (node->get_availability () <= AVAIL_INTERPOSABLE)
+  if (node->get_availability () <= AVAIL_INTERPOSABLE
+      || (node->analyzed && !opt_for_fn (node->decl, flag_ipa_reference)))
     read_write_all_from_decl (node, read_all, write_all);
 
   for (e = node->callees;
@@ -630,7 +660,8 @@ get_read_write_all_from_node (struct cgraph_node *node,
       enum availability avail;
       struct cgraph_node *callee = e->callee->function_symbol (&avail);
       gcc_checking_assert (callee);
-      if (avail <= AVAIL_INTERPOSABLE)
+      if (avail <= AVAIL_INTERPOSABLE
+          || (callee->analyzed && !opt_for_fn (callee->decl, flag_ipa_reference)))
        read_write_all_from_decl (callee, read_all, write_all);
     }
 
@@ -642,7 +673,7 @@ get_read_write_all_from_node (struct cgraph_node *node,
        read_all = true;
        if (dump_file && (dump_flags & TDF_DETAILS))
          fprintf (dump_file, "   indirect call -> read all\n");
-       if (!cgraph_edge_cannot_lead_to_return (ie)
+       if (!ie->cannot_lead_to_return_p ()
            && !(ie->indirect_info->ecf_flags & ECF_PURE))
          {
            if (dump_file && (dump_flags & TDF_DETAILS))
@@ -652,6 +683,18 @@ get_read_write_all_from_node (struct cgraph_node *node,
       }
 }
 
+/* Skip edges from and to nodes without ipa_reference enables.  This leave
+   them out of strongy connected coponents and makes them easyto skip in the
+   propagation loop bellow.  */
+
+static bool
+ignore_edge_p (cgraph_edge *e)
+{
+  return (!opt_for_fn (e->caller->decl, flag_ipa_reference)
+          || !opt_for_fn (e->callee->function_symbol ()->decl,
+                         flag_ipa_reference));
+}
+
 /* Produce the global information by preforming a transitive closure
    on the local information that was produced by ipa_analyze_function.  */
 
@@ -660,21 +703,22 @@ propagate (void)
 {
   struct cgraph_node *node;
   struct cgraph_node **order =
-    XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
+    XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
   int order_pos;
   int i;
+  bool remove_p;
 
   if (dump_file)
     cgraph_node::dump_cgraph (dump_file);
 
-  ipa_discover_readonly_nonaddressable_vars ();
+  remove_p = ipa_discover_readonly_nonaddressable_vars ();
   generate_summary ();
 
   /* Propagate the local information through the call graph to produce
      the global information.  All the nodes within a cycle will have
      the same info so we collapse cycles first.  Then we can do the
      propagation in one pass from the leaves to the roots.  */
-  order_pos = ipa_reduced_postorder (order, true, true, NULL);
+  order_pos = ipa_reduced_postorder (order, true, true, ignore_edge_p);
   if (dump_file)
     ipa_print_order (dump_file, "reduced", order, order_pos);
 
@@ -689,7 +733,7 @@ propagate (void)
       bool write_all = false;
 
       node = order[i];
-      if (node->alias)
+      if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference))
        continue;
 
       node_info = get_reference_vars_info (node);
@@ -767,7 +811,7 @@ propagate (void)
          struct cgraph_node *w;
 
          node = order[i];
-         if (node->alias)
+          if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference))
            continue;
 
          fprintf (dump_file,
@@ -808,7 +852,7 @@ propagate (void)
       ipa_reference_optimization_summary_t opt;
 
       node_info = get_reference_vars_info (node);
-      if (!node->alias
+      if (!node->alias && opt_for_fn (node->decl, flag_ipa_reference)
          && (node->get_availability () > AVAIL_INTERPOSABLE
              || (flags_from_decl_or_type (node->decl) & ECF_LEAF)))
        {
@@ -854,7 +898,7 @@ propagate (void)
   if (dump_file)
     splay_tree_delete (reference_vars_to_consider);
   reference_vars_to_consider = NULL;
-  return 0;
+  return remove_p ? TODO_remove_functions : 0;
 }
 
 /* Return true if we need to write summary of NODE. */
@@ -1011,9 +1055,9 @@ ipa_reference_read_optimization_summary (void)
   bitmap_obstack_initialize (&optimization_summary_obstack);
 
   node_removal_hook_holder =
-      cgraph_add_node_removal_hook (&remove_node_data, NULL);
+      symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
   node_duplication_hook_holder =
-      cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
+      symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
   all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
 
   while ((file_data = file_data_vec[j++]))
@@ -1117,7 +1161,8 @@ ipa_reference_read_optimization_summary (void)
        /* Fatal error here.  We do not want to support compiling ltrans units with
           different version of compiler or different flags than the WPA unit, so
           this should never happen.  */
-       fatal_error ("ipa reference summary is missing in ltrans unit");
+       fatal_error (input_location,
+                    "ipa reference summary is missing in ltrans unit");
     }
 }
 
@@ -1157,7 +1202,7 @@ public:
   /* opt_pass methods: */
   virtual bool gate (function *)
     {
-      return (flag_ipa_reference
+      return ((in_lto_p || flag_ipa_reference)
              /* Don't bother doing anything if the program has errors.  */
              && !seen_error ());
     }
@@ -1173,3 +1218,27 @@ make_pass_ipa_reference (gcc::context *ctxt)
 {
   return new pass_ipa_reference (ctxt);
 }
+
+/* Reset all state within ipa-reference.c so that we can rerun the compiler
+   within the same process.  For use by toplev::finalize.  */
+
+void
+ipa_reference_c_finalize (void)
+{
+  if (ipa_init_p)
+    {
+      bitmap_obstack_release (&optimization_summary_obstack);
+      ipa_init_p = false;
+    }
+
+  if (node_removal_hook_holder)
+    {
+      symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
+      node_removal_hook_holder = NULL;
+    }
+  if (node_duplication_hook_holder)
+    {
+      symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
+      node_duplication_hook_holder = NULL;
+    }
+}