#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 "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"
#include "target.h"
static struct pointer_set_t *visited_nodes;
enum pure_const_state_e
{
IPA_CONST,
IPA_PURE,
IPA_NEITHER
};
struct funct_state_d
{
enum pure_const_state_e pure_const_state;
bool state_set_in_source;
};
typedef struct funct_state_d * funct_state;
static inline funct_state
get_function_state (struct cgraph_node *node)
{
struct ipa_dfs_info * info = node->aux;
return info->aux;
}
static inline void
check_decl (funct_state local,
tree t, bool checking_write)
{
if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
{
local->pure_const_state = IPA_NEITHER;
return;
}
if (TREE_THIS_VOLATILE (t))
{
local->pure_const_state = IPA_NEITHER;
return;
}
if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
return;
if (checking_write)
local->pure_const_state = IPA_NEITHER;
if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
{
if (TREE_READONLY (t)
&& DECL_INITIAL (t)
&& is_gimple_min_invariant (DECL_INITIAL (t)))
;
else
{
if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
}
if (TREE_READONLY (t))
return;
if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
static void
check_operand (funct_state local,
tree t, bool checking_write)
{
if (!t) return;
if (TREE_CODE (t) == VAR_DECL)
check_decl (local, t, checking_write);
}
static void
check_tree (funct_state local, tree t, bool checking_write)
{
if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
return;
if (TREE_THIS_VOLATILE (t))
{
local->pure_const_state = IPA_NEITHER;
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);
}
if (INDIRECT_REF_P (t))
{
check_tree (local, TREE_OPERAND (t, 0), false);
if (checking_write)
{
local->pure_const_state = IPA_NEITHER;
return;
}
else if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
if (SSA_VAR_P (t))
check_operand (local, t, checking_write);
}
static void
look_for_address_of (funct_state local, tree t)
{
if (TREE_CODE (t) == ADDR_EXPR)
{
tree x = get_base_var (t);
if (TREE_CODE (x) == VAR_DECL)
{
check_decl (local, x, false);
if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
}
}
static void
check_rhs_var (funct_state local, tree t)
{
look_for_address_of (local, t);
if (tree_could_trap_p (t)
&& local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
check_tree(local, t, false);
}
static void
check_lhs_var (funct_state local, tree t)
{
if (tree_could_trap_p (t)
&& local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
check_tree(local, t, true);
}
static void
get_asm_expr_operands (funct_state 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)
local->pure_const_state = IPA_NEITHER;
if (ASM_VOLATILE_P (stmt))
local->pure_const_state = IPA_NEITHER;
}
static void
check_call (funct_state 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);
struct cgraph_node* callee;
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)
{
callee = cgraph_node(callee_t);
avail = cgraph_function_body_availability (callee);
if (setjmp_call_p (callee_t))
local->pure_const_state = IPA_NEITHER;
if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
switch (DECL_FUNCTION_CODE (callee_t))
{
case BUILT_IN_LONGJMP:
case BUILT_IN_NONLOCAL_GOTO:
local->pure_const_state = IPA_NEITHER;
break;
default:
break;
}
}
if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
{
if (flags & ECF_PURE)
{
if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
else
local->pure_const_state = IPA_NEITHER;
}
else
{
if (flags & ECF_PURE)
{
if (local->pure_const_state == IPA_CONST)
local->pure_const_state = IPA_PURE;
}
}
}
static tree
scan_function (tree *tp,
int *walk_subtrees,
void *data)
{
struct cgraph_node *fn = data;
tree t = *tp;
funct_state local = get_function_state (fn);
switch (TREE_CODE (t))
{
case VAR_DECL:
if (DECL_INITIAL (t))
walk_tree (&DECL_INITIAL (t), scan_function, fn, visited_nodes);
*walk_subtrees = 0;
break;
case MODIFY_EXPR:
{
tree lhs = TREE_OPERAND (t, 0);
tree rhs = TREE_OPERAND (t, 1);
check_lhs_var (local, lhs);
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:
check_rhs_var (local, t);
*walk_subtrees = 0;
break;
case LABEL_EXPR:
if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
local->pure_const_state = IPA_NEITHER;
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;
}
static void
analyze_function (struct cgraph_node *fn)
{
funct_state l = XCNEW (struct funct_state_d);
tree decl = fn->decl;
struct ipa_dfs_info * w_info = fn->aux;
w_info->aux = l;
l->pure_const_state = IPA_CONST;
l->state_set_in_source = false;
if (TREE_THIS_VOLATILE (decl)
|| !targetm.binds_local_p (decl))
{
l->pure_const_state = IPA_NEITHER;
return;
}
if (TREE_READONLY (decl))
{
l->pure_const_state = IPA_CONST;
l->state_set_in_source = true;
}
if (DECL_IS_PURE (decl))
{
l->pure_const_state = IPA_PURE;
l->state_set_in_source = true;
}
if (dump_file)
{
fprintf (dump_file, "\n local analysis of %s with initial value = %d\n ",
cgraph_node_name (fn),
l->pure_const_state);
}
if (!l->state_set_in_source)
{
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_function,
fn, visited_nodes);
if (l->pure_const_state == IPA_NEITHER)
goto end;
}
}
if (l->pure_const_state != IPA_NEITHER)
{
tree old_decl = current_function_decl;
current_function_decl = fn->decl;
gcc_assert (DECL_STRUCT_FUNCTION (fn->decl));
push_cfun (DECL_STRUCT_FUNCTION (fn->decl));
if (mark_dfs_back_edges ())
l->pure_const_state = IPA_NEITHER;
current_function_decl = old_decl;
pop_cfun ();
}
}
end:
if (dump_file)
{
fprintf (dump_file, "after local analysis of %s with initial value = %d\n ",
cgraph_node_name (fn),
l->pure_const_state);
}
}
static unsigned int
static_execute (void)
{
struct cgraph_node *node;
struct cgraph_node *w;
struct cgraph_node **order =
XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
int order_pos = order_pos = ipa_utils_reduced_inorder (order, true, false);
int i;
struct ipa_dfs_info * w_info;
if (!memory_identifier_string)
memory_identifier_string = build_string(7, "memory");
visited_nodes = pointer_set_create ();
for (node = cgraph_nodes; node; node = node->next)
if (node->analyzed && cgraph_is_master_clone (node))
analyze_function (node);
pointer_set_destroy (visited_nodes);
visited_nodes = NULL;
if (dump_file)
{
dump_cgraph (dump_file);
ipa_utils_print_order(dump_file, "reduced", order, order_pos);
}
for (i = 0; i < order_pos; i++ )
{
enum pure_const_state_e pure_const_state = IPA_CONST;
node = order[i];
w = node;
while (w)
{
funct_state w_l = get_function_state (w);
if (pure_const_state < w_l->pure_const_state)
pure_const_state = w_l->pure_const_state;
if (pure_const_state == IPA_NEITHER)
break;
if (!w_l->state_set_in_source)
{
struct cgraph_edge *e;
for (e = w->callees; e; e = e->next_callee)
{
struct cgraph_node *y = e->callee;
y = cgraph_master_clone (y);
if (y)
{
funct_state y_l = get_function_state (y);
if (pure_const_state < y_l->pure_const_state)
pure_const_state = y_l->pure_const_state;
if (pure_const_state == IPA_NEITHER)
break;
}
}
}
w_info = w->aux;
w = w_info->next_cycle;
}
w = node;
while (w)
{
funct_state w_l = get_function_state (w);
if (!w_l->state_set_in_source)
{
w_l->pure_const_state = pure_const_state;
switch (pure_const_state)
{
case IPA_CONST:
TREE_READONLY (w->decl) = 1;
if (dump_file)
fprintf (dump_file, "Function found to be const: %s\n",
lang_hooks.decl_printable_name(w->decl, 2));
break;
case IPA_PURE:
DECL_IS_PURE (w->decl) = 1;
if (dump_file)
fprintf (dump_file, "Function found to be pure: %s\n",
lang_hooks.decl_printable_name(w->decl, 2));
break;
default:
break;
}
}
w_info = w->aux;
w = w_info->next_cycle;
}
}
for (node = cgraph_nodes; node; node = node->next)
if (node->aux)
{
w_info = node->aux;
if (w_info->aux)
free (w_info->aux);
free (node->aux);
node->aux = NULL;
}
free (order);
return 0;
}
static bool
gate_pure_const (void)
{
return (flag_unit_at_a_time != 0 && flag_ipa_pure_const
&& !(errorcount || sorrycount));
}
struct tree_opt_pass pass_ipa_pure_const =
{
"pure-const",
gate_pure_const,
static_execute,
NULL,
NULL,
0,
TV_IPA_PURE_CONST,
0,
0,
0,
0,
0,
0
};