This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH]: Interprocedural detection of readonly and non-addressablestatic variables
- From: Daniel Berlin <dberlin at dberlin dot org>
- To: Kenneth Zadeck <zadeck at naturalbridge dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 08 Jun 2005 10:30:32 -0400
- Subject: [PATCH]: Interprocedural detection of readonly and non-addressablestatic variables
This is part 1 (of 7) of the interprocedural work Kenny and I have been
doing for static variables, type-escape analysis, and pure/const
detection.
This is the scanning code that finds information about which static
variables are written/referenced/address taken in a given procedure.
2005-06-06 Kenneth Zadeck <zadeck@naturalbridge.com>
Danny Berlin <dberlin@dberlin.org>
Interprocedual detection of readonly and non-addressable static
variables.
* ipa-reference.c, ipa-reference.h, ipa-utils.c, ipa-utils.h: new
files.
* common.opt: Added "fipa-reference".
* opts.c: Added "flag_ipa_reference".
* timevar.def: Added TV_IPA_REFERENCE.
* tree-dfa.c (find_reference_vars) Addition of call to reset
per function information.
* tree-flow.h: Addition of reference_vars_info field for
FUNCTION_DECLS.
* tree-optimize.c: Added pass.
* tree-pass.h: Added pass.
Bootstrapped and regtested on i686-pc-linux-gnu and powerpc-linux-gnu.
Okay for mainline?
/* Callgraph based analysis of static variables.
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
/* This file gathers information about how variables whose scope is
confined to the compilation unit are used.
There are two categories of information produced by this pass:
1) The addressable (TREE_ADDRESSABLE) bit and readonly
(TREE_READONLY) bit associated with these variables is properly set
based on scanning all of the code withing the compilation unit.
2) The transitive call site specific clobber effects are computed
for the variables whose scope is contained within this compilation
unit.
First each function and static variable initialization is analyzed
to determine which local static variables are either read, written,
or have their address taken. Any local static that has its address
taken is removed from consideration. Once the local read and
writes are determined, a transitive closure of this information is
performed over the call graph to determine the worst case set of
side effects of each call. In later parts of the compiler, these
local and global sets are examined to make the call clobbering less
traumatic, promote some statics to registers, and improve aliasing
information.
Currently must be run after inlining decisions have been made since
otherwise, the local sets will not contain information that is
consistent with post inlined state. The global sets are not prone
to this problem since they are by definition transitive.
*/
/* FIXME -- PROFILE-RESTRUCTURE: There are a large number of places in
this module that will need to be changed when the final engineering
is done to support ssa form in more than a single function at a
time.
The problem is that the ssa parts of the compiler work in terms of
the UID field in the var_ann structure. Local variables are not
unique and statics are not stable. Thus, all analysis within this
module is done using the DECL_UID numbering. The information is
converted to a local var_ann numbering for the function being
compiled.
It is not clear what the final solution to this is. There is a plan
to make var_ann numbering assign low stable numbers to the static
variables and start numbering the locals after the last
static. This would allow the conversion code to be dumped but only
if this module is not generalized to perform the same analysis on
local variables.
To make it transparent, all variables that matter are suffixed with
either "_by_ann_uid" or "_by_decl_uid".
*/
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tree-flow.h"
#include "tree-inline.h"
#include "tree-pass.h"
#include "langhooks.h"
#include "pointer-set.h"
#include "ggc.h"
#include "ipa-utils.h"
#include "ipa-reference.h"
#include "c-common.h"
#include "tree-gimple.h"
#include "cgraph.h"
#include "output.h"
#include "flags.h"
#include "timevar.h"
#include "diagnostic.h"
#include "langhooks.h"
/* This splay tree contains all of the static variables that are
being considered by the compilation level alias analysis. For
module_at_a_time compilation, this is the set of static but not
public variables. Any variables that either have their address
taken or participate in otherwise unsavory operations are deleted
from this list. */
static GTY((param1_is(int), param2_is(tree)))
splay_tree reference_vars_to_consider_by_decl_uid;
/* This bitmap is used to knock out the module static variables whose
addresses have been taken and passed around. */
static bitmap module_statics_escape_by_decl_uid;
/* This bitmap is used to knock out the module static variables that
are not readonly. */
static bitmap module_statics_written_by_decl_uid;
/* A bit is set for every module static we are considering. This is
ored into the local info when asm code is found that clobbers all
memory. */
static bitmap all_module_statics_by_decl_uid;
static struct pointer_set_t *visited_nodes;
static bitmap_obstack ipa_obstack;
enum initialization_status_t
{
UNINITIALIZED,
RUNNING,
FINISHED
};
tree memory_identifier_string;
/* Convert IN_DECL bitmap which is indexed by DECL_UID to IN_ANN, a
bitmap indexed by var_ann (VAR_DECL)->uid. */
static void
convert_UIDs_in_bitmap (bitmap in_ann, bitmap in_decl)
{
unsigned int index;
bitmap_iterator bi;
EXECUTE_IF_SET_IN_BITMAP(in_decl, 0, index, bi)
{
splay_tree_node n =
splay_tree_lookup (reference_vars_to_consider_by_decl_uid,
index);
if (n != NULL)
{
tree t = (tree)n->value;
var_ann_t va = var_ann (t);
if (va)
bitmap_set_bit (in_ann, va->uid);
}
}
}
/* Return the ipa_reference_vars structure starting from the cgraph NODE. */
static inline ipa_reference_vars_info_t
get_reference_vars_info_from_cgraph (struct cgraph_node * node)
{
return get_var_ann (node->decl)->reference_vars_info;
}
/* The bitmaps used to represent the static global variables are
indexed by DECL_UID however, this is not used inside of functions
to index the ssa variables. The denser var_ann (VAR_DECL)->uid is
used there. This function is called from
tree_dfa:find_referenced_vars after the denser representation is
built. This function invalidates any cached indexes. */
void
ipa_reference_reset_maps (void)
{
struct cgraph_node *node;
for (node = cgraph_nodes; node; node = node->next)
{
ipa_reference_vars_info_t info =
get_reference_vars_info_from_cgraph (node);
if (info)
{
ipa_reference_local_vars_info_t l = info->local;
ipa_reference_global_vars_info_t g = info->global;
if (l->var_anns_valid)
{
bitmap_clear (l->statics_read_by_ann_uid);
bitmap_clear (l->statics_written_by_ann_uid);
l->var_anns_valid = false;
}
/* There may be orphans in the cgraph so must check that
there is global info. */
if (g && g->var_anns_valid)
{
bitmap_clear (g->statics_read_by_ann_uid);
bitmap_clear (g->statics_written_by_ann_uid);
bitmap_clear (g->statics_not_read_by_ann_uid);
bitmap_clear (g->statics_not_written_by_ann_uid);
g->var_anns_valid = false;
}
}
}
}
/* Get the cgraph_node for the local function and make sure the
var_ann bitmaps are properly converted. */
static ipa_reference_local_vars_info_t
get_local_reference_vars_info (tree fn)
{
ipa_reference_local_vars_info_t l;
/* Was not compiled -O2 or higher. */
ipa_reference_vars_info_t info = get_var_ann (fn)->reference_vars_info;
if (!info)
return NULL;
l = info->local;
if (!l->var_anns_valid)
{
convert_UIDs_in_bitmap (l->statics_read_by_ann_uid,
l->statics_read_by_decl_uid);
convert_UIDs_in_bitmap (l->statics_written_by_ann_uid,
l->statics_written_by_decl_uid);
l->var_anns_valid = true;
}
return l;
}
/* Get the global reference_vars_info structure for the function FN and
make sure the ann_uid's bitmaps are properly converted. */
static ipa_reference_global_vars_info_t
get_global_reference_vars_info (tree fn)
{
ipa_reference_global_vars_info_t g;
/* Was not compiled -O2 or higher. */
ipa_reference_vars_info_t info = get_var_ann (fn)->reference_vars_info;
if (!info)
return NULL;
g = info->global;
if (!g)
return NULL;
if (!g->var_anns_valid)
{
convert_UIDs_in_bitmap (g->statics_read_by_ann_uid,
g->statics_read_by_decl_uid);
convert_UIDs_in_bitmap (g->statics_written_by_ann_uid,
g->statics_written_by_decl_uid);
convert_UIDs_in_bitmap (g->statics_not_read_by_ann_uid,
g->statics_not_read_by_decl_uid);
convert_UIDs_in_bitmap (g->statics_not_written_by_ann_uid,
g->statics_not_written_by_decl_uid);
g->var_anns_valid = true;
}
return g;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that may be read locally by the execution of the function
fn. Returns NULL if no data is available, such as it was not
compiled with -O2 or higher. */
bitmap
ipa_reference_get_read_local (tree fn)
{
ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
if (l)
return l->statics_read_by_ann_uid;
else
return NULL;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that may be written locally by the execution of the function
fn. Returns NULL if no data is available, such as it was not
compiled with -O2 or higher or the function was not available. */
bitmap
ipa_reference_get_written_local (tree fn)
{
ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
if (l)
return l->statics_written_by_ann_uid;
else
return NULL;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that are read during the execution of the function
FN. Returns NULL if no data is available, such as it was not
compiled with -O2 or higher or the function was not available. */
bitmap
ipa_reference_get_read_global (tree fn)
{
ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
if (g)
return g->statics_read_by_ann_uid;
else
return NULL;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that are written during the execution of the function
FN. Note that variables written may or may not be read during the
function call. Returns NULL if no data is available, such as it
was not compiled with -O2 or higher or the function was not available. */
bitmap
ipa_reference_get_written_global (tree fn)
{
ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
if (g)
return g->statics_written_by_ann_uid;
else
return NULL;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that are not read during the execution of the function
FN. Returns NULL if no data is available, such as it was not
compiled with -O2 or higher or the function was not available. */
bitmap
ipa_reference_get_not_read_global (tree fn)
{
ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
if (g)
return g->statics_not_read_by_ann_uid;
else
return NULL;
}
/* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
variables that are not written during the execution of the function
FN. Note that variables written may or may not be read during the
function call. Returns NULL if no data is available, such as it
was not compiled with -O2 or higher or the function was not available. */
bitmap
ipa_reference_get_not_written_global (tree fn)
{
ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
if (g)
return g->statics_not_written_by_ann_uid;
else
return NULL;
}
/* Add VAR to all_module_statics_by_decl_uid and the two
reference_vars_to_consider* sets. */
static inline void
add_static_var (tree var)
{
int uid = DECL_UID (var);
if (!bitmap_bit_p (all_module_statics_by_decl_uid, uid))
{
splay_tree_insert (reference_vars_to_consider_by_decl_uid,
uid, (splay_tree_value)var);
bitmap_set_bit (all_module_statics_by_decl_uid, uid);
}
}
/* Return true if the variable T is the right kind of static variable to
perform compilation unit scope escape analysis. */
static inline bool
has_proper_scope_for_analysis (tree t)
{
/* If the variable has the "used" attribute, treat it as if it had a
been touched by the devil. */
if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
return false;
/* Do not want to do anything with volatile except mark any
function that uses one to be not const or pure. */
if (TREE_THIS_VOLATILE (t))
return false;
/* Do not care about a local automatic that is not static. */
if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
return false;
if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
return false;
/* 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 (!bitmap_bit_p (all_module_statics_by_decl_uid, DECL_UID (t)))
add_static_var (t);
return true;
}
/* If T is a VAR_DECL for a static that we are interrested in, add the
uid to the bitmap. */
static void
check_operand (ipa_reference_local_vars_info_t local,
tree t, bool checking_write)
{
if (!t) return;
if ((TREE_CODE (t) == VAR_DECL)
&& (has_proper_scope_for_analysis (t)))
{
if (checking_write)
{
if (local)
bitmap_set_bit (local->statics_written_by_decl_uid, DECL_UID (t));
/* Mark the write so we can tell which statics are
readonly. */
bitmap_set_bit (module_statics_written_by_decl_uid, DECL_UID (t));
}
else if (local)
bitmap_set_bit (local->statics_read_by_decl_uid, DECL_UID (t));
}
}
/* Examine tree T for references to static variables. All internal
references like array references or indirect references are added
to the READ_BM. Direct references are added to either READ_BM or
WRITE_BM depending on the value of CHECKING_WRITE. */
static void
check_tree (ipa_reference_local_vars_info_t local, tree t, bool checking_write)
{
if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
return;
while (TREE_CODE (t) == REALPART_EXPR
|| TREE_CODE (t) == IMAGPART_EXPR
|| handled_component_p (t))
{
if (TREE_CODE (t) == ARRAY_REF)
check_operand (local, TREE_OPERAND (t, 1), false);
t = TREE_OPERAND (t, 0);
}
/* The bottom of an indirect reference can only be read, not
written. So just recurse and whatever we find, check it against
the read bitmaps. */
/* if (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF) */
/* FIXME when we have array_ref's of pointers. */
if (INDIRECT_REF_P (t))
check_tree (local, TREE_OPERAND (t, 0), false);
if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
check_operand (local, t, checking_write);
}
/* Given a memory reference T, will return the variable at the bottom
of the access. Unlike get_base_address, this will recurse thru
INDIRECT_REFS. */
static tree
get_base_var (tree t)
{
if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
return t;
while (!SSA_VAR_P (t)
&& (!CONSTANT_CLASS_P (t))
&& TREE_CODE (t) != LABEL_DECL
&& TREE_CODE (t) != FUNCTION_DECL
&& TREE_CODE (t) != CONST_DECL)
{
t = TREE_OPERAND (t, 0);
}
return t;
}
/* Scan tree T to see if there are any addresses taken in within T. */
static void
look_for_address_of (tree t)
{
if (TREE_CODE (t) == ADDR_EXPR)
{
tree x = get_base_var (t);
if (TREE_CODE (x) == VAR_DECL)
if (has_proper_scope_for_analysis (x))
bitmap_set_bit (module_statics_escape_by_decl_uid, DECL_UID (x));
}
}
/* Check to see if T is a read or address of operation on a static var
we are interested in analyzing. LOCAL is passed in to get access
to its bit vectors. Local is NULL if this is called from a static
initializer. */
static void
check_rhs_var (ipa_reference_local_vars_info_t local, tree t)
{
look_for_address_of (t);
if (local == NULL)
return;
check_tree(local, t, false);
}
/* Check to see if T is an assignment to a static var we are
interrested in analyzing. LOCAL is passed in to get access to its bit
vectors.
*/
static void
check_lhs_var (ipa_reference_local_vars_info_t local, tree t)
{
if (local == NULL)
return;
check_tree(local, t, true);
}
/* This is a scaled down version of get_asm_expr_operands from
tree_ssa_operands.c. The version there runs much later and assumes
that aliasing information is already available. Here we are just
trying to find if the set of inputs and outputs contain references
or address of operations to local static variables. FN is the
function being analyzed and STMT is the actual asm statement. */
static void
get_asm_expr_operands (ipa_reference_local_vars_info_t local, tree stmt)
{
int noutputs = list_length (ASM_OUTPUTS (stmt));
const char **oconstraints
= (const char **) alloca ((noutputs) * sizeof (const char *));
int i;
tree link;
const char *constraint;
bool allows_mem, allows_reg, is_inout;
for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
{
oconstraints[i] = constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
parse_output_constraint (&constraint, i, 0, 0,
&allows_mem, &allows_reg, &is_inout);
check_lhs_var (local, TREE_VALUE (link));
}
for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
{
constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
parse_input_constraint (&constraint, 0, 0, noutputs, 0,
oconstraints, &allows_mem, &allows_reg);
check_rhs_var (local, TREE_VALUE (link));
}
for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
if (simple_cst_equal(TREE_VALUE (link), memory_identifier_string) == 1)
{
/* Abandon all hope, ye who enter here. */
local->calls_read_all = true;
local->calls_write_all = true;
}
}
/* Check the parameters of a function call from CALLER to CALL_EXPR to
see if any of them are static vars. Also check to see if this is
either an indirect call, a call outside the compilation unit, or
has special attributes that effect the clobbers. The caller
parameter is the tree node for the caller and the second operand is
the tree node for the entire call expression. */
static void
check_call (ipa_reference_local_vars_info_t local, tree call_expr)
{
int flags = call_expr_flags (call_expr);
tree operand_list = TREE_OPERAND (call_expr, 1);
tree operand;
tree callee_t = get_callee_fndecl (call_expr);
enum availability avail = AVAIL_NOT_AVAILABLE;
for (operand = operand_list;
operand != NULL_TREE;
operand = TREE_CHAIN (operand))
{
tree argument = TREE_VALUE (operand);
check_rhs_var (local, argument);
}
if (callee_t)
{
struct cgraph_node* callee = cgraph_node(callee_t);
avail = cgraph_function_body_availability (callee);
}
if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
if (local)
{
if (flags & ECF_PURE)
local->calls_read_all = true;
else
{
local->calls_read_all = true;
local->calls_write_all = true;
}
}
}
/* Walk tree and record all calls. Called via walk_tree. The data is
the function that is being scanned. */
/* TP is the part of the tree currently under the
microscope. WALK_SUBTREES is part of the walk_tree api but is
unused here. DATA is cgraph_node of the function being walked. */
static tree
scan_for_static_refs (tree *tp,
int *walk_subtrees,
void *data)
{
struct cgraph_node *fn = data;
tree t = *tp;
ipa_reference_local_vars_info_t local = NULL;
if (fn)
local = get_reference_vars_info_from_cgraph (fn)->local;
switch (TREE_CODE (t))
{
case VAR_DECL:
if (DECL_INITIAL (t))
walk_tree (&DECL_INITIAL (t), scan_for_static_refs, fn, visited_nodes);
*walk_subtrees = 0;
break;
case MODIFY_EXPR:
{
/* First look on the lhs and see what variable is stored to */
tree lhs = TREE_OPERAND (t, 0);
tree rhs = TREE_OPERAND (t, 1);
check_lhs_var (local, lhs);
/* For the purposes of figuring out what the cast affects */
/* Next check the operands on the rhs to see if they are ok. */
switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
{
case tcc_binary:
{
tree op0 = TREE_OPERAND (rhs, 0);
tree op1 = TREE_OPERAND (rhs, 1);
check_rhs_var (local, op0);
check_rhs_var (local, op1);
}
break;
case tcc_unary:
{
tree op0 = TREE_OPERAND (rhs, 0);
check_rhs_var (local, op0);
}
break;
case tcc_reference:
check_rhs_var (local, rhs);
break;
case tcc_declaration:
check_rhs_var (local, rhs);
break;
case tcc_expression:
switch (TREE_CODE (rhs))
{
case ADDR_EXPR:
check_rhs_var (local, rhs);
break;
case CALL_EXPR:
check_call (local, rhs);
break;
default:
break;
}
break;
default:
break;
}
*walk_subtrees = 0;
}
break;
case ADDR_EXPR:
/* This case is here to find addresses on rhs of constructors in
decl_initial of static variables. */
check_rhs_var (local, t);
*walk_subtrees = 0;
break;
case LABEL_EXPR:
if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
{
/* Target of long jump. */
local->calls_read_all = true;
local->calls_write_all = true;
}
break;
case CALL_EXPR:
check_call (local, t);
*walk_subtrees = 0;
break;
case ASM_EXPR:
get_asm_expr_operands (local, t);
*walk_subtrees = 0;
break;
default:
break;
}
return NULL;
}
/* Lookup the tree node for the static variable that has UID. */
static tree
get_static_decl_by_decl_uid (int index)
{
splay_tree_node stn =
splay_tree_lookup (reference_vars_to_consider_by_decl_uid, index);
if (stn)
return (tree)stn->value;
return NULL;
}
/* Lookup the tree node for the static variable that has UID and
conver the name to a string for debugging. */
static const char *
get_static_name_by_decl_uid (int index)
{
splay_tree_node stn =
splay_tree_lookup (reference_vars_to_consider_by_decl_uid, index);
if (stn)
return lang_hooks.decl_printable_name ((tree)(stn->value), 2);
return NULL;
}
/* Or in all of the bits from every callee into X, the caller's, bit
vector. There are several cases to check to avoid the sparse
bitmap oring. */
static void
propagate_bits (struct cgraph_node *x)
{
ipa_reference_vars_info_t x_info = get_reference_vars_info_from_cgraph (x);
ipa_reference_global_vars_info_t x_global = x_info->global;
struct cgraph_edge *e;
for (e = x->callees; e; e = e->next_callee)
{
struct cgraph_node *y = e->callee;
/* Only look at the master nodes and skip external nodes. */
y = cgraph_master_clone (y);
if (y)
{
if (get_reference_vars_info_from_cgraph (y))
{
ipa_reference_vars_info_t y_info = get_reference_vars_info_from_cgraph (y);
ipa_reference_global_vars_info_t y_global = y_info->global;
if (x_global->statics_read_by_decl_uid
!= all_module_statics_by_decl_uid)
{
if (y_global->statics_read_by_decl_uid
== all_module_statics_by_decl_uid)
{
BITMAP_FREE (x_global->statics_read_by_decl_uid);
x_global->statics_read_by_decl_uid
= all_module_statics_by_decl_uid;
}
/* Skip bitmaps that are pointer equal to node's bitmap
(no reason to spin within the cycle). */
else if (x_global->statics_read_by_decl_uid
!= y_global->statics_read_by_decl_uid)
bitmap_ior_into (x_global->statics_read_by_decl_uid,
y_global->statics_read_by_decl_uid);
}
if (x_global->statics_written_by_decl_uid
!= all_module_statics_by_decl_uid)
{
if (y_global->statics_written_by_decl_uid
== all_module_statics_by_decl_uid)
{
BITMAP_FREE (x_global->statics_written_by_decl_uid);
x_global->statics_written_by_decl_uid
= all_module_statics_by_decl_uid;
}
/* Skip bitmaps that are pointer equal to node's bitmap
(no reason to spin within the cycle). */
else if (x_global->statics_written_by_decl_uid
!= y_global->statics_written_by_decl_uid)
bitmap_ior_into (x_global->statics_written_by_decl_uid,
y_global->statics_written_by_decl_uid);
}
}
else
{
gcc_unreachable ();
}
}
}
}
/* Look at all of the callees of X to see which ones represent inlined
calls. For each of these callees, merge their local info into
TARGET and check their children recursively.
This function goes away when Jan changes the inliner and IPA
analysis so that this is not run between the time when inlining
decisions are made and when the inlining actually occurs. */
static void
merge_callee_local_info (struct cgraph_node *target,
struct cgraph_node *x)
{
struct cgraph_edge *e;
ipa_reference_local_vars_info_t x_l =
get_reference_vars_info_from_cgraph (target)->local;
/* Make the world safe for tail recursion. */
struct ipa_dfs_info *node_info = x->aux;
if (node_info->aux)
return;
node_info->aux = x;
for (e = x->callees; e; e = e->next_callee)
{
struct cgraph_node *y = e->callee;
if (y->global.inlined_to)
{
ipa_reference_vars_info_t y_info;
ipa_reference_local_vars_info_t y_l;
struct cgraph_node* orig_y = y;
y = cgraph_master_clone (y);
if (y)
{
y_info = get_reference_vars_info_from_cgraph (y);
y_l = y_info->local;
if (x_l != y_l)
{
bitmap_ior_into (x_l->statics_read_by_decl_uid,
y_l->statics_read_by_decl_uid);
bitmap_ior_into (x_l->statics_written_by_decl_uid,
y_l->statics_written_by_decl_uid);
}
x_l->calls_read_all |= y_l->calls_read_all;
x_l->calls_write_all |= y_l->calls_write_all;
merge_callee_local_info (target, y);
}
else
{
fprintf(stderr, "suspect inlining of ");
dump_cgraph_node (stderr, orig_y);
fprintf(stderr, "\ninto ");
dump_cgraph_node (stderr, target);
dump_cgraph (stderr);
gcc_assert(false);
}
}
}
node_info->aux = NULL;
}
/* The init routine for analyzing global static variable usage. See
comments at top for description. */
static void
ipa_init (void)
{
memory_identifier_string = build_string(7, "memory");
reference_vars_to_consider_by_decl_uid =
splay_tree_new_ggc (splay_tree_compare_ints);
bitmap_obstack_initialize (&ipa_obstack);
module_statics_escape_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
module_statics_written_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
all_module_statics_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
/* There are some shared nodes, in particular the initializers on
static declarations. We do not need to scan them more than once
since all we would be interrested in are the addressof
operations. */
visited_nodes = pointer_set_create ();
}
/* Check out the rhs of a static or global initialization VNODE to see
if any of them contain addressof operations. Note that some of
these variables may not even be referenced in the code in this
compilation unit but their right hand sides may contain references
to variables defined within this unit. */
static void
analyze_variable (struct cgraph_varpool_node *vnode)
{
tree global = vnode->decl;
if (TREE_CODE (global) == VAR_DECL)
{
if (DECL_INITIAL (global))
walk_tree (&DECL_INITIAL (global), scan_for_static_refs,
NULL, visited_nodes);
}
gcc_unreachable ();
}
/* This is the main routine for finding the reference patterns for
global variables within a function FN. */
static void
analyze_function (struct cgraph_node *fn)
{
ipa_reference_vars_info_t info
= xcalloc (1, sizeof (struct ipa_reference_vars_info_d));
ipa_reference_local_vars_info_t l
= xcalloc (1, sizeof (struct ipa_reference_local_vars_info_d));
tree decl = fn->decl;
var_ann_t var_ann = get_var_ann (decl);
/* Add the info to the tree's annotation. */
var_ann->reference_vars_info = info;
info->local = l;
l->statics_read_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
l->statics_written_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
l->statics_read_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
l->statics_written_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
l->var_anns_valid = false;
if (dump_file)
fprintf (dump_file, "\n local analysis of %s\n", cgraph_node_name (fn));
{
struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
basic_block this_block;
FOR_EACH_BB_FN (this_block, this_cfun)
{
block_stmt_iterator bsi;
for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
walk_tree (bsi_stmt_ptr (bsi), scan_for_static_refs,
fn, visited_nodes);
}
}
/* FIXME - When Jan gets the local statics promoted to the global
variable list, the next two loops go away. */
/* Walk over any private statics that may take addresses of functions. */
if (TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
{
tree step;
for (step = BLOCK_VARS (DECL_INITIAL (decl));
step;
step = TREE_CHAIN (step))
{
if (DECL_INITIAL (step))
walk_tree (&DECL_INITIAL (step), scan_for_static_refs,
fn, visited_nodes);
}
}
/* Also look here for private statics. */
if (DECL_STRUCT_FUNCTION (decl))
{
tree step;
for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
step;
step = TREE_CHAIN (step))
{
tree var = TREE_VALUE (step);
if (DECL_INITIAL (var) && TREE_STATIC (var))
walk_tree (&DECL_INITIAL (var), scan_for_static_refs,
fn, visited_nodes);
}
}
}
/* If FN is avail == AVAIL_OVERWRITABLE, replace the effects bit
vectors with worst case bit vectors. We had to analyze it above to
find out if it took the address of any statics. However, now that
we know that, we can get rid of all of the other side effects. */
static void
clean_function (struct cgraph_node *fn)
{
ipa_reference_vars_info_t info = get_reference_vars_info_from_cgraph (fn);
ipa_reference_local_vars_info_t l = info->local;
ipa_reference_global_vars_info_t g = info->global;
var_ann_t var_ann = get_var_ann (fn->decl);
if (l)
{
if (l->statics_read_by_decl_uid
&& l->statics_read_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (l->statics_read_by_decl_uid);
if (l->statics_written_by_decl_uid
&&l->statics_written_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (l->statics_written_by_decl_uid);
free (l);
}
if (g)
{
if (g->statics_read_by_decl_uid
&& g->statics_read_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (g->statics_read_by_decl_uid);
if (g->statics_written_by_decl_uid
&& g->statics_written_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (g->statics_written_by_decl_uid);
if (g->statics_not_read_by_decl_uid
&& g->statics_not_read_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (g->statics_not_read_by_decl_uid);
if (g->statics_not_written_by_decl_uid
&& g->statics_not_written_by_decl_uid !=
all_module_statics_by_decl_uid)
BITMAP_FREE (g->statics_not_written_by_decl_uid);
free (g);
}
free (var_ann->reference_vars_info);
var_ann->reference_vars_info = NULL;
}
/* Produce the global information by preforming a transitive closure
on the local information that was produced by ipa_analyze_function
and ipa_analyze_variable. */
static void
static_execute (void)
{
struct cgraph_node *node;
struct cgraph_varpool_node *vnode;
struct cgraph_node *w;
struct cgraph_node **order =
xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
int order_pos = order_pos = ipa_utils_reduced_inorder (order, false, true);
int i;
ipa_init ();
/* Process all of the variables first. */
for (vnode = cgraph_varpool_nodes_queue; vnode; vnode = vnode->next_needed)
analyze_variable (vnode);
/* Process all of the functions next.
We do not want to process any of the clones so we check that this
is a master clone. However, we do need to process any
AVAIL_OVERWRITABLE functions (these are never clones) because
they may cause a static variable to escape. The code that can
overwrite such a function cannot access the statics because it
would not be in the same compilation unit. When the analysis is
finished, the computed information of these AVAIL_OVERWRITABLE is
replaced with worst case info.
*/
for (node = cgraph_nodes; node; node = node->next)
if (node->analyzed
&& (cgraph_is_master_clone (node)
|| (cgraph_function_body_availability (node)
== AVAIL_OVERWRITABLE)))
analyze_function (node);
pointer_set_destroy (visited_nodes);
visited_nodes = NULL;
if (dump_file)
dump_cgraph (dump_file);
/* Prune out the variables that were found to behave badly
(i.e. have there address taken). */
{
unsigned int index;
bitmap_iterator bi;
bitmap module_statics_readonly = BITMAP_ALLOC (&ipa_obstack);
bitmap module_statics_const = BITMAP_ALLOC (&ipa_obstack);
bitmap bm_temp = BITMAP_ALLOC (&ipa_obstack);
EXECUTE_IF_SET_IN_BITMAP (module_statics_escape_by_decl_uid, 0, index, bi)
{
splay_tree_remove (reference_vars_to_consider_by_decl_uid, index);
}
bitmap_and_compl_into (all_module_statics_by_decl_uid,
module_statics_escape_by_decl_uid);
bitmap_and_compl (module_statics_readonly, all_module_statics_by_decl_uid,
module_statics_written_by_decl_uid);
/* If the address is not taken, we can unset the addressable bit
on this variable. */
EXECUTE_IF_SET_IN_BITMAP (all_module_statics_by_decl_uid, 0, index, bi)
{
tree var = get_static_decl_by_decl_uid (index);
TREE_ADDRESSABLE (var) = 0;
if (dump_file)
fprintf (dump_file, "Not TREE_ADDRESSABLE var %s\n",
get_static_name_by_decl_uid (index));
}
/* If the variable is never written, we can set the TREE_READONLY
flag. Additionally if it has a DECL_INITIAL that is made up of
constants we can treat the entire global as a constant. */
bitmap_and_compl (module_statics_readonly, all_module_statics_by_decl_uid,
module_statics_written_by_decl_uid);
EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly, 0, index, bi)
{
tree var = get_static_decl_by_decl_uid (index);
TREE_READONLY (var) = 1;
if (dump_file)
fprintf (dump_file, "read-only var %s\n",
get_static_name_by_decl_uid (index));
if (DECL_INITIAL (var)
&& is_gimple_min_invariant (DECL_INITIAL (var)))
{
bitmap_set_bit (module_statics_const, index);
if (dump_file)
fprintf (dump_file, "read-only constant %s\n",
get_static_name_by_decl_uid (index));
}
}
BITMAP_FREE(module_statics_escape_by_decl_uid);
BITMAP_FREE(module_statics_written_by_decl_uid);
if (dump_file)
EXECUTE_IF_SET_IN_BITMAP (all_module_statics_by_decl_uid, 0, index, bi)
{
fprintf (dump_file, "\nPromotable global:%s",
get_static_name_by_decl_uid (index));
}
for (i = 0; i < order_pos; i++ )
{
ipa_reference_local_vars_info_t l;
node = order[i];
l = get_reference_vars_info_from_cgraph (node)->local;
/* Any variables that are not in all_module_statics_by_decl_uid are
removed from the local maps. This will include all of the
variables that were found to escape in the function
scanning. */
bitmap_and_into (l->statics_read_by_decl_uid,
all_module_statics_by_decl_uid);
bitmap_and_into (l->statics_written_by_decl_uid,
all_module_statics_by_decl_uid);
}
BITMAP_FREE(module_statics_readonly);
BITMAP_FREE(module_statics_const);
BITMAP_FREE(bm_temp);
}
if (dump_file)
{
for (i = 0; i < order_pos; i++ )
{
unsigned int index;
ipa_reference_local_vars_info_t l;
bitmap_iterator bi;
node = order[i];
l = get_reference_vars_info_from_cgraph (node)->local;
fprintf (dump_file,
"\nFunction name:%s/%i:",
cgraph_node_name (node), node->uid);
fprintf (dump_file, "\n locals read: ");
EXECUTE_IF_SET_IN_BITMAP (l->statics_read_by_decl_uid,
0, index, bi)
{
fprintf (dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
fprintf (dump_file, "\n locals written: ");
EXECUTE_IF_SET_IN_BITMAP (l->statics_written_by_decl_uid,
0, index, bi)
{
fprintf(dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
}
}
/* Propagate the local information thru 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_utils_reduced_inorder (order, true, true);
if (dump_file)
ipa_utils_print_order(dump_file, "reduced", order, order_pos);
for (i = 0; i < order_pos; i++ )
{
ipa_reference_vars_info_t node_info;
ipa_reference_global_vars_info_t node_g =
xcalloc (1, sizeof (struct ipa_reference_global_vars_info_d));
ipa_reference_local_vars_info_t node_l;
bool read_all;
bool write_all;
struct ipa_dfs_info * w_info;
node = order[i];
node_info = get_reference_vars_info_from_cgraph (node);
if (!node_info)
{
dump_cgraph_node (stderr, node);
dump_cgraph (stderr);
gcc_unreachable ();
}
node_info->global = node_g;
node_l = node_info->local;
read_all = node_l->calls_read_all;
write_all = node_l->calls_write_all;
/* If any node in a cycle is calls_read_all or calls_write_all
they all are. */
w_info = node->aux;
w = w_info->next_cycle;
while (w)
{
ipa_reference_local_vars_info_t w_l =
get_reference_vars_info_from_cgraph (w)->local;
read_all |= w_l->calls_read_all;
write_all |= w_l->calls_write_all;
w_info = w->aux;
w = w_info->next_cycle;
}
/* Initialized the bitmaps for the reduced nodes */
if (read_all)
node_g->statics_read_by_decl_uid = all_module_statics_by_decl_uid;
else
{
node_g->statics_read_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
bitmap_copy (node_g->statics_read_by_decl_uid,
node_l->statics_read_by_decl_uid);
}
if (write_all)
node_g->statics_written_by_decl_uid = all_module_statics_by_decl_uid;
else
{
node_g->statics_written_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
bitmap_copy (node_g->statics_written_by_decl_uid,
node_l->statics_written_by_decl_uid);
}
w_info = node->aux;
w = w_info->next_cycle;
while (w)
{
ipa_reference_vars_info_t w_ri = get_reference_vars_info_from_cgraph (w);
ipa_reference_local_vars_info_t w_l = w_ri->local;
/* All nodes within a cycle share the same global info bitmaps. */
w_ri->global = node_g;
/* These global bitmaps are initialized from the local info
of all of the nodes in the region. However there is no
need to do any work if the bitmaps were set to
all_module_statics_by_decl_uid. */
if (!read_all)
bitmap_ior_into (node_g->statics_read_by_decl_uid,
w_l->statics_read_by_decl_uid);
if (!write_all)
bitmap_ior_into (node_g->statics_written_by_decl_uid,
w_l->statics_written_by_decl_uid);
w_info = w->aux;
w = w_info->next_cycle;
}
w = node;
while (w)
{
propagate_bits (w);
w_info = w->aux;
w = w_info->next_cycle;
}
}
/* Need to fix up the local information sets. The information that
has been gathered so far is preinlining. However, the
compilation will progress post inlining so the local sets for the
inlined calls need to be merged into the callers. Note that the
local sets are not shared between all of the nodes in a cycle so
those nodes in the cycle must be processed explicitly. */
for (i = 0; i < order_pos; i++ )
{
struct ipa_dfs_info * w_info;
node = order[i];
merge_callee_local_info (node, node);
w_info = node->aux;
w = w_info->next_cycle;
while (w)
{
merge_callee_local_info (w, w);
w_info = w->aux;
w = w_info->next_cycle;
}
}
if (dump_file)
{
for (i = 0; i < order_pos; i++ )
{
ipa_reference_vars_info_t node_info;
ipa_reference_global_vars_info_t node_g;
ipa_reference_local_vars_info_t node_l;
unsigned int index;
bitmap_iterator bi;
struct ipa_dfs_info * w_info;
node = order[i];
node_info = get_reference_vars_info_from_cgraph (node);
node_g = node_info->global;
node_l = node_info->local;
fprintf (dump_file,
"\nFunction name:%s/%i:",
cgraph_node_name (node), node->uid);
fprintf (dump_file, "\n locals read: ");
EXECUTE_IF_SET_IN_BITMAP (node_l->statics_read_by_decl_uid,
0, index, bi)
{
fprintf (dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
fprintf (dump_file, "\n locals written: ");
EXECUTE_IF_SET_IN_BITMAP (node_l->statics_written_by_decl_uid,
0, index, bi)
{
fprintf(dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
w_info = node->aux;
w = w_info->next_cycle;
while (w)
{
ipa_reference_vars_info_t w_ri =
get_reference_vars_info_from_cgraph (w);
ipa_reference_local_vars_info_t w_l = w_ri->local;
fprintf (dump_file, "\n next cycle: %s/%i ",
cgraph_node_name (w), w->uid);
fprintf (dump_file, "\n locals read: ");
EXECUTE_IF_SET_IN_BITMAP (w_l->statics_read_by_decl_uid,
0, index, bi)
{
fprintf (dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
fprintf (dump_file, "\n locals written: ");
EXECUTE_IF_SET_IN_BITMAP (w_l->statics_written_by_decl_uid,
0, index, bi)
{
fprintf(dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
w_info = w->aux;
w = w_info->next_cycle;
}
fprintf (dump_file, "\n globals read: ");
EXECUTE_IF_SET_IN_BITMAP (node_g->statics_read_by_decl_uid,
0, index, bi)
{
fprintf (dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
fprintf (dump_file, "\n globals written: ");
EXECUTE_IF_SET_IN_BITMAP (node_g->statics_written_by_decl_uid,
0, index, bi)
{
fprintf (dump_file, "%s ",
get_static_name_by_decl_uid (index));
}
}
}
/* Cleanup. */
for (i = 0; i < order_pos; i++ )
{
ipa_reference_vars_info_t node_info;
ipa_reference_global_vars_info_t node_g;
node = order[i];
node_info = get_reference_vars_info_from_cgraph (node);
node_g = node_info->global;
node_g->var_anns_valid = false;
/* Create the complimentary sets. These are more useful for
certain apis. */
node_g->statics_not_read_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
node_g->statics_not_written_by_decl_uid = BITMAP_ALLOC (&ipa_obstack);
if (node_g->statics_read_by_decl_uid != all_module_statics_by_decl_uid)
{
bitmap_and_compl (node_g->statics_not_read_by_decl_uid,
all_module_statics_by_decl_uid,
node_g->statics_read_by_decl_uid);
}
if (node_g->statics_written_by_decl_uid
!= all_module_statics_by_decl_uid)
bitmap_and_compl (node_g->statics_not_written_by_decl_uid,
all_module_statics_by_decl_uid,
node_g->statics_written_by_decl_uid);
node_g->statics_read_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
node_g->statics_written_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
node_g->statics_not_read_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
node_g->statics_not_written_by_ann_uid = BITMAP_ALLOC (&ipa_obstack);
}
free (order);
for (node = cgraph_nodes; node; node = node->next)
{
/* Get rid of the aux information. */
if (node->aux)
{
free (node->aux);
node->aux = NULL;
}
if (node->analyzed
&& (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE))
clean_function (node);
}
}
static bool
gate_reference (void)
{
return (flag_unit_at_a_time != 0 && flag_ipa_reference
/* Don't bother doing anything if the program has errors. */
&& !(errorcount || sorrycount));
}
struct tree_opt_pass pass_ipa_reference =
{
"static-var", /* name */
gate_reference, /* gate */
static_execute, /* execute */
NULL, /* sub */
NULL, /* next */
0, /* static_pass_number */
TV_IPA_REFERENCE, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
0 /* letter */
};
#include "gt-ipa-reference.h"
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Makefile.in,v
retrieving revision 1.1496
diff -u -p -r1.1496 Makefile.in
--- Makefile.in 4 Jun 2005 17:07:50 -0000 1.1496
+++ Makefile.in 6 Jun 2005 20:58:50 -0000
@@ -736,6 +736,8 @@ SCHED_INT_H = sched-int.h $(INSN_ATTR_H)
INTEGRATE_H = integrate.h varray.h
CFGLAYOUT_H = cfglayout.h $(BASIC_BLOCK_H)
CFGLOOP_H = cfgloop.h $(BASIC_BLOCK_H) $(RTL_H)
+IPA_UTILS_H = ipa-utils.h $(TREE_H) $(CGRAPH_H)
+IPA_REFERENCE_H = ipa-reference.h bitmap.h $(TREE_H)
CGRAPH_H = cgraph.h tree.h
DF_H = df.h bitmap.h sbitmap.h $(BASIC_BLOCK_H)
DDG_H = ddg.h sbitmap.h $(DF_H)
@@ -757,7 +760,7 @@ TREE_DUMP_H = tree-dump.h $(SPLAY_TREE_H
TREE_GIMPLE_H = tree-gimple.h tree-iterator.h
TREE_FLOW_H = tree-flow.h tree-flow-inline.h tree-ssa-operands.h \
bitmap.h $(BASIC_BLOCK_H) hard-reg-set.h $(TREE_GIMPLE_H) \
- $(HASHTAB_H) $(CGRAPH_H)
+ $(HASHTAB_H) $(CGRAPH_H) $(IPA_REFERENCE_H)
TREE_SSA_LIVE_H = tree-ssa-live.h $(PARTITION_H)
PRETTY_PRINT_H = pretty-print.h input.h $(OBSTACK_H)
DIAGNOSTIC_H = diagnostic.h diagnostic.def $(PRETTY_PRINT_H)
@@ -966,7 +969,8 @@ OBJS-common = \
OBJS-md = $(out_object_file)
OBJS-archive = $(EXTRA_OBJS) $(host_hook_obj) tree-inline.o \
- cgraph.o cgraphunit.o tree-nomudflap.o ipa.o ipa-inline.o
+ cgraph.o cgraphunit.o tree-nomudflap.o ipa.o ipa-inline.o \
+ ipa-utils.o ipa-reference.o
OBJS = $(OBJS-common) $(out_object_file) $(OBJS-archive)
@@ -1782,7 +1786,7 @@ tree-dfa.o : tree-dfa.c $(TREE_FLOW_H) $
tree-inline.h $(HASHTAB_H) pointer-set.h $(FLAGS_H) function.h \
$(TIMEVAR_H) convert.h $(TM_H) coretypes.h langhooks.h $(TREE_DUMP_H) \
tree-pass.h $(PARAMS_H) $(CGRAPH_H) $(BASIC_BLOCK_H) hard-reg-set.h \
- $(TREE_GIMPLE_H)
+ $(TREE_GIMPLE_H) $(IPA_REFERENCE_H)
tree-ssa-operands.o : tree-ssa-operands.c $(TREE_FLOW_H) $(CONFIG_H) \
$(SYSTEM_H) $(TREE_H) $(GGC_H) $(DIAGNOSTIC_H) tree-inline.h \
$(FLAGS_H) function.h $(TM_H) $(TIMEVAR_H) tree-pass.h toplev.h \
@@ -2073,6 +2078,14 @@ ipa-inline.o : ipa-inline.c $(CONFIG_H)
$(TREE_H) langhooks.h tree-inline.h $(FLAGS_H) $(CGRAPH_H) intl.h \
$(DIAGNOSTIC_H) $(FIBHEAP_H) $(PARAMS_H) $(TIMEVAR_H) tree-pass.h \
$(COVERAGE_H)
+ipa-utils.o : ipa-utils.c $(IPA_UTILS_H) $(CONFIG_H) $(SYSTEM_H) \
+ coretypes.h $(TM_H) $(TREE_H) $(TREE_FLOW_H) tree-inline.h langhooks.h \
+ pointer-set.h $(GGC_H) $(C_COMMON_H) $(TREE_GIMPLE_H) \
+ $(CGRAPH_H) output.h $(FLAGS_H) tree-pass.h $(DIAGNOSTIC_H)
+ipa-reference.o : ipa-reference.c $(CONFIG_H) $(SYSTEM_H) \
+ coretypes.h $(TM_H) $(TREE_H) $(TREE_FLOW_H) tree-inline.h langhooks.h \
+ pointer-set.h $(GGC_H) $(IPA_REFERENCE_H) $(IPA_UTILS_H) $(C_COMMON_H) \
+ $(TREE_GIMPLE_H) $(CGRAPH_H) output.h $(FLAGS_H) tree-pass.h $(DIAGNOSTIC_H)
coverage.o : coverage.c $(GCOV_IO_H) $(CONFIG_H) $(SYSTEM_H) coretypes.h \
$(TM_H) $(RTL_H) $(TREE_H) $(FLAGS_H) output.h $(REGS_H) $(EXPR_H) \
function.h toplev.h $(GGC_H) langhooks.h $(COVERAGE_H) gt-coverage.h \
@@ -2602,6 +2626,7 @@ GTFILES = $(srcdir)/input.h $(srcdir)/co
$(srcdir)/coverage.c $(srcdir)/function.h $(srcdir)/rtl.h \
$(srcdir)/optabs.h $(srcdir)/tree.h $(srcdir)/libfuncs.h $(SYMTAB_H) \
$(srcdir)/real.h $(srcdir)/varray.h $(srcdir)/insn-addr.h $(srcdir)/hwint.h \
+ $(srcdir)/ipa-reference.h \
$(srcdir)/cselib.h $(srcdir)/basic-block.h $(srcdir)/cgraph.h \
$(srcdir)/c-common.h $(srcdir)/c-tree.h $(srcdir)/reload.h \
$(srcdir)/alias.c $(srcdir)/bitmap.c $(srcdir)/cselib.c $(srcdir)/cgraph.c \
@@ -2623,6 +2648,7 @@ GTFILES = $(srcdir)/input.h $(srcdir)/co
$(srcdir)/tree-chrec.h $(srcdir)/tree-vect-generic.c \
$(srcdir)/tree-ssa-operands.h $(srcdir)/tree-ssa-operands.c \
$(srcdir)/tree-profile.c $(srcdir)/rtl-profile.c $(srcdir)/tree-nested.c \
+ $(srcdir)/ipa-reference.c \
$(out_file) \
@all_gtfiles@
Index: common.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/common.opt,v
retrieving revision 1.73
diff -u -p -r1.73 common.opt
--- common.opt 4 Jun 2005 17:07:55 -0000 1.73
+++ common.opt 6 Jun 2005 21:59:41 -0000
@@ -483,6 +483,10 @@ finstrument-functions
Common Report Var(flag_instrument_function_entry_exit)
Instrument function entry and exit with profiling calls
+fipa-reference
+Common Report Var(flag_ipa_reference) Init(0)
+Discover readonly and non addressable static variables.
+
fivopts
Common Report Var(flag_ivopts) Init(1)
Optimize induction variables on trees
Index: opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/opts.c,v
retrieving revision 1.113
diff -u -p -r1.113 opts.c
--- opts.c 1 Jun 2005 07:02:17 -0000 1.113
+++ opts.c 6 Jun 2005 20:58:56 -0000
@@ -522,6 +522,7 @@ decode_options (unsigned int argc, const
flag_loop_optimize = 1;
flag_if_conversion = 1;
flag_if_conversion2 = 1;
+ flag_ipa_reference = 1;
flag_tree_ccp = 1;
flag_tree_dce = 1;
flag_tree_dom = 1;
Index: timevar.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/timevar.def,v
retrieving revision 1.49
diff -u -p -r1.49 timevar.def
--- timevar.def 25 May 2005 12:33:35 -0000 1.49
+++ timevar.def 6 Jun 2005 20:58:57 -0000
@@ -42,6 +42,7 @@ DEFTIMEVAR (TV_DUMP , "
DEFTIMEVAR (TV_CGRAPH , "callgraph construction")
DEFTIMEVAR (TV_CGRAPHOPT , "callgraph optimization")
+DEFTIMEVAR (TV_IPA_REFERENCE , "ipa reference")
/* Time spent by constructing CFG. */
DEFTIMEVAR (TV_CFG , "cfg construction")
/* Time spent by cleaning up CFG. */
Index: tree-flow.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-flow.h,v
retrieving revision 2.115
diff -u -p -r2.115 tree-flow.h
--- tree-flow.h 2 Jun 2005 02:57:00 -0000 2.115
+++ tree-flow.h 6 Jun 2005 21:39:33 -0000
@@ -29,6 +29,7 @@ Boston, MA 02111-1307, USA. */
#include "tree-gimple.h"
#include "tree-ssa-operands.h"
#include "cgraph.h"
+#include "ipa-reference.h"
/* Forward declare structures for the garbage collector GTY markers. */
#ifndef GCC_BASIC_BLOCK_H
@@ -224,6 +225,11 @@ struct var_ann_d GTY(())
current version of this variable (an SSA_NAME). */
tree current_def;
+ /* Pointer to the structure that contains the sets of global
+ variables modified by function calls. This field is only used
+ for FUNCTION_DECLs. */
+ ipa_reference_vars_info_t GTY ((skip)) reference_vars_info;
+
/* If this variable is a structure, this fields holds a list of
symbols representing each of the fields of the structure. */
subvar_t subvars;
Index: tree-dfa.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-dfa.c,v
retrieving revision 2.55
diff -u -p -r2.55 tree-dfa.c
--- tree-dfa.c 1 Jun 2005 02:50:55 -0000 2.55
+++ tree-dfa.c 6 Jun 2005 20:58:57 -0000
@@ -46,6 +46,7 @@ Boston, MA 02111-1307, USA. */
#include "convert.h"
#include "params.h"
#include "cgraph.h"
+#include "ipa-reference.h"
/* Build and maintain data flow information for trees. */
@@ -105,6 +106,8 @@ find_referenced_vars (void)
block_stmt_iterator si;
struct walk_state walk_state;
+ ipa_reference_reset_maps ();
+
vars_found = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
memset (&walk_state, 0, sizeof (walk_state));
walk_state.vars_found = vars_found;
Index: tree-optimize.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-optimize.c,v
retrieving revision 2.104
diff -u -p -r2.104 tree-optimize.c
--- tree-optimize.c 3 Jun 2005 02:11:04 -0000 2.104
+++ tree-optimize.c 6 Jun 2005 20:58:57 -0000
@@ -364,6 +364,7 @@ init_tree_optimization_passes (void)
/* Intraprocedural optimization passes. */
p = &all_ipa_passes;
NEXT_PASS (pass_ipa_inline);
+ NEXT_PASS (pass_ipa_reference);
*p = NULL;
/* All passes needed to lower the function into shape optimizers can operate
Index: tree-pass.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-pass.h,v
retrieving revision 2.39
diff -u -p -r2.39 tree-pass.h
--- tree-pass.h 3 Jun 2005 02:11:05 -0000 2.39
+++ tree-pass.h 6 Jun 2005 20:58:57 -0000
@@ -220,8 +220,9 @@ extern struct tree_opt_pass pass_store_c
extern struct tree_opt_pass pass_store_copy_prop;
extern struct tree_opt_pass pass_vrp;
extern struct tree_opt_pass pass_create_structure_vars;
extern struct tree_opt_pass pass_uncprop;
extern struct tree_opt_pass pass_ipa_inline;
+extern struct tree_opt_pass pass_ipa_reference;
#endif /* GCC_TREE_PASS_H */
/* IPA handling of references.
Copyright (C) 2004-2005 Free Software Foundation, Inc.
Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#ifndef GCC_IPA_REFERENCE_H
#define GCC_IPA_REFERENCE_H
#include "bitmap.h"
#include "tree.h"
/* The static variables defined within the compilation unit that are
loaded or stored directly by function that owns this structure. */
struct ipa_reference_local_vars_info_d
{
bitmap statics_read_by_decl_uid;
bitmap statics_written_by_decl_uid;
bitmap statics_read_by_ann_uid;
bitmap statics_written_by_ann_uid;
/* Var_anns_valid is reset at the start of compilation for each
function because the indexing that the "_var_anns" is based
on is invalidated between function compilations. This allows for
lazy creation of the "_var_ann" variables. */
bool var_anns_valid;
/* Set when this function calls another function external to the
compilation unit or if the function has a asm clobber of memory.
In general, such calls are modeled as reading and writing all
variables (both bits on) but sometime there are attributes on the
called function so we can do better. */
bool calls_read_all;
bool calls_write_all;
};
struct ipa_reference_global_vars_info_d
{
bitmap statics_read_by_decl_uid;
bitmap statics_written_by_decl_uid;
bitmap statics_read_by_ann_uid;
bitmap statics_written_by_ann_uid;
bitmap statics_not_read_by_decl_uid;
bitmap statics_not_written_by_decl_uid;
bitmap statics_not_read_by_ann_uid;
bitmap statics_not_written_by_ann_uid;
/* Var_anns_valid is reset at the start of compilation for each
function because the indexing that the "_var_anns" is based
on is invalidated between function compilations. This allows for
lazy creation of the "_var_ann" variables. */
bool var_anns_valid;
};
/* Statics that are read and written by some set of functions. The
local ones are based on the loads and stores local to the function.
The global ones are based on the local info as well as the
transitive closure of the functions that are called. The
structures are separated to allow the global structures to be
shared between several functions since every function within a
strongly connected component will have the same information. This
sharing saves both time and space in the computation of the vectors
as well as their translation from decl_uid form to ann_uid
form. */
typedef struct ipa_reference_local_vars_info_d *ipa_reference_local_vars_info_t;
typedef struct ipa_reference_global_vars_info_d *ipa_reference_global_vars_info_t;
struct ipa_reference_vars_info_d
{
ipa_reference_local_vars_info_t local;
ipa_reference_global_vars_info_t global;
};
typedef struct ipa_reference_vars_info_d *ipa_reference_vars_info_t;
/* In ipa-reference.c */
void ipa_reference_reset_maps (void);
bitmap ipa_reference_get_read_local (tree fn);
bitmap ipa_reference_get_written_local (tree fn);
bitmap ipa_reference_get_read_global (tree fn);
bitmap ipa_reference_get_written_global (tree fn);
bitmap ipa_reference_get_not_read_global (tree fn);
bitmap ipa_reference_get_not_written_global (tree fn);
#endif /* GCC_IPA_REFERENCE_H */
/* Utilities for ipa analysis.
Copyright (C) 2005 Free Software Foundation, Inc.
Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tree-flow.h"
#include "tree-inline.h"
#include "tree-pass.h"
#include "langhooks.h"
#include "pointer-set.h"
#include "ggc.h"
#include "ipa-utils.h"
#include "ipa-reference.h"
#include "c-common.h"
#include "tree-gimple.h"
#include "cgraph.h"
#include "output.h"
#include "flags.h"
#include "timevar.h"
#include "diagnostic.h"
#include "langhooks.h"
/* Debugging function for postorder and inorder code. NOTE is a string
that is printed before the nodes are printed. ORDER is an array of
cgraph_nodes that has COUNT useful nodes in it. */
void
ipa_utils_print_order (FILE* out,
const char * note,
struct cgraph_node** order,
int count)
{
int i;
fprintf (out, "\n\n ordered call graph: %s\n", note);
for (i = count - 1; i >= 0; i--)
dump_cgraph_node(dump_file, order[i]);
fprintf (out, "\n");
fflush(out);
}
struct searchc_env {
struct cgraph_node **stack;
int stack_size;
struct cgraph_node **result;
int order_pos;
splay_tree nodes_marked_new;
bool reduce;
int count;
};
/* This is an implementation of Tarjan's strongly connected region
finder as reprinted in Aho Hopcraft and Ullman's The Design and
Analysis of Computer Programs (1975) pages 192-193. This version
has been customized for cgraph_nodes. The env parameter is because
it is recursive and there are no nested functions here. This
function should only be called from itself or
cgraph_reduced_inorder. ENV is a stack env and would be
unnecessary if C had nested functions. V is the node to start
searching from. */
static void
searchc (struct searchc_env* env, struct cgraph_node *v)
{
struct cgraph_edge *edge;
struct ipa_dfs_info *v_info = v->aux;
/* mark node as old */
v_info->new = false;
splay_tree_remove (env->nodes_marked_new, v->uid);
v_info->dfn_number = env->count;
v_info->low_link = env->count;
env->count++;
env->stack[(env->stack_size)++] = v;
v_info->on_stack = true;
for (edge = v->callees; edge; edge = edge->next_callee)
{
struct ipa_dfs_info * w_info;
struct cgraph_node *w = edge->callee;
/* Bypass the clones and only look at the master node. Skip
external and other bogus nodes. */
w = cgraph_master_clone (w);
if (w && w->aux)
{
w_info = w->aux;
if (w_info->new)
{
searchc (env, w);
v_info->low_link =
(v_info->low_link < w_info->low_link) ?
v_info->low_link : w_info->low_link;
}
else
if ((w_info->dfn_number < v_info->dfn_number)
&& (w_info->on_stack))
v_info->low_link =
(w_info->dfn_number < v_info->low_link) ?
w_info->dfn_number : v_info->low_link;
}
}
if (v_info->low_link == v_info->dfn_number)
{
struct cgraph_node *last = NULL;
struct cgraph_node *x;
struct ipa_dfs_info *x_info;
do {
x = env->stack[--(env->stack_size)];
x_info = x->aux;
x_info->on_stack = false;
if (env->reduce)
{
x_info->next_cycle = last;
last = x;
}
else
env->result[env->order_pos++] = x;
}
while (v != x);
if (env->reduce)
env->result[env->order_pos++] = v;
}
}
/* Topsort the call graph by caller relation. Put the result in ORDER.
The REDUCE flag is true if you want the cycles reduced to single
nodes. Only consider nodes that have the output bit set. */
int
ipa_utils_reduced_inorder (struct cgraph_node **order,
bool reduce, bool allow_overwritable)
{
struct cgraph_node *node;
struct searchc_env env;
splay_tree_node result;
env.stack = xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
env.stack_size = 0;
env.result = order;
env.order_pos = 0;
env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
env.count = 1;
env.reduce = reduce;
for (node = cgraph_nodes; node; node = node->next)
if ((node->analyzed)
&& (cgraph_is_master_clone (node)
|| (allow_overwritable
&& (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE))))
{
/* Reuse the info if it is already there. */
struct ipa_dfs_info *info = node->aux;
if (!info)
info = xcalloc (1, sizeof (struct ipa_dfs_info));
info->new = true;
info->on_stack = false;
info->next_cycle = NULL;
node->aux = info;
splay_tree_insert (env.nodes_marked_new,
(splay_tree_key)node->uid,
(splay_tree_value)node);
}
else
node->aux = NULL;
result = splay_tree_min (env.nodes_marked_new);
while (result)
{
node = (struct cgraph_node *)result->value;
searchc (&env, node);
result = splay_tree_min (env.nodes_marked_new);
}
splay_tree_delete (env.nodes_marked_new);
free (env.stack);
return env.order_pos;
}
/* Utilities for ipa analysis.
Copyright (C) 2004-2005 Free Software Foundation, Inc.
Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#ifndef GCC_IPA_UTILS_H
#define GCC_IPA_UTILS_H
#include "tree.h"
#include "cgraph.h"
/* Used for parsing attributes of asm code. */
extern tree memory_identifier_string;
struct ipa_dfs_info {
int dfn_number;
int low_link;
bool new;
bool on_stack;
struct cgraph_node* next_cycle;
PTR aux;
};
/* In ipa-utils.c */
void ipa_utils_print_order (FILE*, const char *, struct cgraph_node**, int);
int ipa_utils_reduced_inorder (struct cgraph_node **, bool, bool);
#endif /* GCC_IPA_UTILS_H */