/* 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.
#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"
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) */
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. */
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 (cgraph_function_node (fn, NULL));
+ get_reference_optimization_summary (fn->function_symbol (NULL));
if (info)
return info->statics_not_read;
else if (flags_from_decl_or_type (fn->decl) & ECF_LEAF)
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)
}
\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. */
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;
}
e = e->next_callee)
{
enum availability avail;
- struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
+ struct cgraph_node *y = e->callee->function_symbol (&avail);
if (!y)
continue;
/* Only look into nodes we can propagate something. */
int flags = flags_from_decl_or_type (y->decl);
- if (avail > AVAIL_OVERWRITABLE
- || (avail == AVAIL_OVERWRITABLE && (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))
{
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,
}
}
+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);
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);
}
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++)
{
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:
}
}
- if (cgraph_node_cannot_return (fn))
+ if (fn->cannot_return_p ())
bitmap_clear (local->statics_written);
}
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);
if (dump_file)
FOR_EACH_DEFINED_FUNCTION (node)
- if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
+ if (node->get_availability () >= AVAIL_INTERPOSABLE
+ && opt_for_fn (node->decl, flag_ipa_reference))
{
ipa_reference_local_vars_info_t l;
unsigned int index;
tree decl = node->decl;
int flags = flags_from_decl_or_type (decl);
if ((flags & ECF_LEAF)
- && cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
+ && node->get_availability () < AVAIL_INTERPOSABLE)
;
else if (flags & ECF_CONST)
;
- else if ((flags & ECF_PURE)
- || cgraph_node_cannot_return (node))
+ else if ((flags & ECF_PURE) || node->cannot_return_p ())
{
read_all = true;
if (dump_file && (dump_flags & TDF_DETAILS))
struct cgraph_edge *e, *ie;
/* When function is overwritable, we can not assume anything. */
- if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
+ 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;
e = e->next_callee)
{
enum availability avail;
- struct cgraph_node *callee = cgraph_function_node (e->callee, &avail);
+ struct cgraph_node *callee = e->callee->function_symbol (&avail);
gcc_checking_assert (callee);
- if (avail <= AVAIL_OVERWRITABLE)
+ if (avail <= AVAIL_INTERPOSABLE
+ || (callee->analyzed && !opt_for_fn (callee->decl, flag_ipa_reference)))
read_write_all_from_decl (callee, read_all, write_all);
}
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))
}
}
+/* 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. */
{
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)
- dump_cgraph (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);
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);
fprintf (dump_file, "Starting cycle with %s/%i\n",
node->asm_name (), node->order);
- vec<cgraph_node_ptr> cycle_nodes = ipa_get_nodes_in_cycle (node);
+ vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
/* If any node in a cycle is read_all or write_all, they all are. */
FOR_EACH_VEC_ELT (cycle_nodes, x, w)
read_all = union_static_var_sets (node_g->statics_read,
w_l->statics_read);
if (!(flags & ECF_PURE)
- && !cgraph_node_cannot_return (w))
+ && !w->cannot_return_p ())
write_all = union_static_var_sets (node_g->statics_written,
w_l->statics_written);
}
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,
ipa_reference_vars_info_t node_info = get_reference_vars_info (node);
ipa_reference_global_vars_info_t node_g = &node_info->global;
- vec<cgraph_node_ptr> cycle_nodes = ipa_get_nodes_in_cycle (node);
+ vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
FOR_EACH_VEC_ELT (cycle_nodes, x, w)
{
ipa_reference_vars_info_t w_ri = get_reference_vars_info (w);
ipa_reference_optimization_summary_t opt;
node_info = get_reference_vars_info (node);
- if (!node->alias
- && (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE
+ 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)))
{
node_g = &node_info->global;
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. */
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++]))
index = streamer_read_uhwi (ib);
encoder = file_data->symtab_node_encoder;
- node = cgraph (lto_symtab_encoder_deref (encoder, index));
+ node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref
+ (encoder, index));
info = XCNEW (struct ipa_reference_optimization_summary_d);
set_reference_optimization_summary (node, info);
info->statics_not_read = BITMAP_ALLOC (&optimization_summary_obstack);
/* 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");
}
}
/* 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 ());
}
{
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;
+ }
+}