1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file marks functions as being either const (TREE_READONLY) or
22 pure (DECL_PURE_P). It can also set a variant of these that
23 are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
25 This must be run after inlining decisions have been made since
26 otherwise, the local sets will not contain information that is
27 consistent with post inlined state. The global sets are not prone
28 to this problem since they are by definition transitive. */
30 /* The code in this module is called by the ipa pass manager. It
31 should be one of the later passes since it's information is used by
32 the rest of the compilation. */
36 #include "coretypes.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "langhooks.h"
43 #include "pointer-set.h"
45 #include "ipa-utils.h"
52 #include "diagnostic.h"
53 #include "langhooks.h"
56 static struct pointer_set_t *visited_nodes;
58 /* Lattice values for const and pure functions. Everything starts out
59 being const, then may drop to pure and then neither depending on
61 enum pure_const_state_e
68 /* Holder for the const_state. There is one of these per function
73 enum pure_const_state_e pure_const_state;
75 /* True if the function could possibly infinite loop. There are a
76 lot of ways that this could be determined. We are pretty
77 conservative here. While it is possible to cse pure and const
78 calls, it is not legal to have dce get rid of the call if there
79 is a possibility that the call could infinite loop since this is
80 a behavioral change. */
83 /* If the state of the function was set in the source, then assume
84 that it was done properly even if the analysis we do would be
86 bool state_set_in_source;
89 typedef struct funct_state_d * funct_state;
91 /* The storage of the funct_state is abstracted because there is the
92 possibility that it may be desirable to move this to the cgraph
95 /* Array, indexed by cgraph node uid, of function states. */
97 static funct_state *funct_state_vec;
99 /* Holders of ipa cgraph hooks: */
100 static struct cgraph_node_hook_list *function_insertion_hook_holder;
102 /* Init the function state. */
107 funct_state_vec = XCNEWVEC (funct_state, cgraph_max_uid);
111 /* Init the function state. */
116 free (funct_state_vec);
120 /* Return the function state from NODE. */
122 static inline funct_state
123 get_function_state (struct cgraph_node *node)
125 return funct_state_vec[node->uid];
128 /* Set the function state S for NODE. */
131 set_function_state (struct cgraph_node *node, funct_state s)
133 funct_state_vec[node->uid] = s;
136 /* Check to see if the use (or definition when CHECKING_WRITE is true)
137 variable T is legal in a function that is either pure or const. */
140 check_decl (funct_state local,
141 tree t, bool checking_write)
143 /* If the variable has the "used" attribute, treat it as if it had a
144 been touched by the devil. */
145 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
147 local->pure_const_state = IPA_NEITHER;
148 local->looping = false;
152 /* Do not want to do anything with volatile except mark any
153 function that uses one to be not const or pure. */
154 if (TREE_THIS_VOLATILE (t))
156 local->pure_const_state = IPA_NEITHER;
157 local->looping = false;
161 /* Do not care about a local automatic that is not static. */
162 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
165 /* Since we have dealt with the locals and params cases above, if we
166 are CHECKING_WRITE, this cannot be a pure or constant
170 local->pure_const_state = IPA_NEITHER;
171 local->looping = false;
175 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
177 /* If the front end set the variable to be READONLY and
178 constant, we can allow this variable in pure or const
179 functions but the scope is too large for our analysis to set
180 these bits ourselves. */
182 if (TREE_READONLY (t)
184 && is_gimple_min_invariant (DECL_INITIAL (t)))
185 ; /* Read of a constant, do not change the function state. */
188 /* Just a regular read. */
189 if (local->pure_const_state == IPA_CONST)
190 local->pure_const_state = IPA_PURE;
194 /* Compilation level statics can be read if they are readonly
196 if (TREE_READONLY (t))
199 /* Just a regular read. */
200 if (local->pure_const_state == IPA_CONST)
201 local->pure_const_state = IPA_PURE;
204 /* If T is a VAR_DECL check to see if it is an allowed reference. */
207 check_operand (funct_state local,
208 tree t, bool checking_write)
212 if (TREE_CODE (t) == VAR_DECL)
213 check_decl (local, t, checking_write);
216 /* Examine tree T for references. */
219 check_tree (funct_state local, tree t, bool checking_write)
221 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR)
222 || TREE_CODE (t) == SSA_NAME)
225 /* Any tree which is volatile disqualifies this function from being
227 if (TREE_THIS_VOLATILE (t))
229 local->pure_const_state = IPA_NEITHER;
230 local->looping = false;
234 while (TREE_CODE (t) == REALPART_EXPR
235 || TREE_CODE (t) == IMAGPART_EXPR
236 || handled_component_p (t))
238 if (TREE_CODE (t) == ARRAY_REF)
239 check_operand (local, TREE_OPERAND (t, 1), false);
240 t = TREE_OPERAND (t, 0);
243 /* The bottom of an indirect reference can only be read, not
245 if (INDIRECT_REF_P (t))
247 check_tree (local, TREE_OPERAND (t, 0), false);
249 /* Any indirect reference that occurs on the lhs
250 disqualifies the function from being pure or const. Any
251 indirect reference that occurs on the rhs disqualifies the
252 function from being const. */
255 local->pure_const_state = IPA_NEITHER;
256 local->looping = false;
259 else if (local->pure_const_state == IPA_CONST)
260 local->pure_const_state = IPA_PURE;
264 check_operand (local, t, checking_write);
267 /* Scan tree T to see if there are any addresses taken in within T. */
270 look_for_address_of (funct_state local, tree t)
272 if (TREE_CODE (t) == ADDR_EXPR)
274 tree x = get_base_var (t);
275 if (TREE_CODE (x) == VAR_DECL)
277 check_decl (local, x, false);
279 /* Taking the address of something appears to be reasonable
280 in PURE code. Not allowed in const. */
281 if (local->pure_const_state == IPA_CONST)
282 local->pure_const_state = IPA_PURE;
287 /* Check to see if T is a read or address of operation on a var we are
288 interested in analyzing. LOCAL is passed in to get access to its
292 check_rhs_var (funct_state local, tree t)
294 look_for_address_of (local, t);
296 /* Memcmp and strlen can both trap and they are declared pure. */
297 if (tree_could_trap_p (t)
298 && local->pure_const_state == IPA_CONST)
299 local->pure_const_state = IPA_PURE;
301 check_tree(local, t, false);
304 /* Check to see if T is an assignment to a var we are interested in
305 analyzing. LOCAL is passed in to get access to its bit vectors. */
308 check_lhs_var (funct_state local, tree t)
310 /* Memcmp and strlen can both trap and they are declared pure.
311 Which seems to imply that we can apply the same rule here. */
312 if (tree_could_trap_p (t)
313 && local->pure_const_state == IPA_CONST)
314 local->pure_const_state = IPA_PURE;
316 check_tree(local, t, true);
319 /* This is a scaled down version of get_asm_expr_operands from
320 tree_ssa_operands.c. The version there runs much later and assumes
321 that aliasing information is already available. Here we are just
322 trying to find if the set of inputs and outputs contain references
323 or address of operations to local static variables. STMT is the
324 actual asm statement. */
327 get_asm_expr_operands (funct_state local, gimple stmt)
329 size_t noutputs = gimple_asm_noutputs (stmt);
330 const char **oconstraints
331 = (const char **) alloca ((noutputs) * sizeof (const char *));
334 const char *constraint;
335 bool allows_mem, allows_reg, is_inout;
337 for (i = 0; i < noutputs; i++)
339 op = gimple_asm_output_op (stmt, i);
340 oconstraints[i] = constraint
341 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
342 parse_output_constraint (&constraint, i, 0, 0,
343 &allows_mem, &allows_reg, &is_inout);
345 check_lhs_var (local, TREE_VALUE (op));
348 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
350 op = gimple_asm_input_op (stmt, i);
352 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
353 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
354 oconstraints, &allows_mem, &allows_reg);
356 check_rhs_var (local, TREE_VALUE (op));
359 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
361 op = gimple_asm_clobber_op (stmt, i);
362 if (simple_cst_equal(TREE_VALUE (op), memory_identifier_string) == 1)
363 /* Abandon all hope, ye who enter here. */
364 local->pure_const_state = IPA_NEITHER;
367 if (gimple_asm_volatile_p (stmt))
368 local->pure_const_state = IPA_NEITHER;
371 /* Check the parameters of a function call to CALL_EXPR to see if
372 there are any references in the parameters that are not allowed for
373 pure or const functions. Also check to see if this is either an
374 indirect call, a call outside the compilation unit, or has special
375 attributes that may also effect the purity. The CALL_EXPR node for
376 the entire call expression. */
379 check_call (funct_state local, gimple call)
381 int flags = gimple_call_flags (call);
382 tree lhs, callee_t = gimple_call_fndecl (call);
383 struct cgraph_node* callee;
384 enum availability avail = AVAIL_NOT_AVAILABLE;
387 lhs = gimple_call_lhs (call);
389 check_lhs_var (local, lhs);
391 for (i = 0; i < gimple_call_num_args (call); i++)
392 check_rhs_var (local, gimple_call_arg (call, i));
394 /* The const and pure flags are set by a variety of places in the
395 compiler (including here). If someone has already set the flags
396 for the callee, (such as for some of the builtins) we will use
397 them, otherwise we will compute our own information.
399 Const and pure functions have less clobber effects than other
400 functions so we process these first. Otherwise if it is a call
401 outside the compilation unit or an indirect call we punt. This
402 leaves local calls which will be processed by following the call
406 callee = cgraph_node(callee_t);
407 avail = cgraph_function_body_availability (callee);
409 /* When bad things happen to bad functions, they cannot be const
411 if (setjmp_call_p (callee_t))
413 local->pure_const_state = IPA_NEITHER;
414 local->looping = false;
417 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
418 switch (DECL_FUNCTION_CODE (callee_t))
420 case BUILT_IN_LONGJMP:
421 case BUILT_IN_NONLOCAL_GOTO:
422 local->pure_const_state = IPA_NEITHER;
423 local->looping = false;
430 /* The callee is either unknown (indirect call) or there is just no
431 scannable code for it (external call) . We look to see if there
432 are any bits available for the callee (such as by declaration or
433 because it is builtin) and process solely on the basis of those
435 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
437 if (flags & ECF_PURE)
439 if (local->pure_const_state == IPA_CONST)
440 local->pure_const_state = IPA_PURE;
443 local->pure_const_state = IPA_NEITHER;
447 /* We have the code and we will scan it for the effects. */
448 if (flags & ECF_PURE)
450 if (local->pure_const_state == IPA_CONST)
451 local->pure_const_state = IPA_PURE;
456 /* TP is the part of the tree currently under the microscope.
457 WALK_SUBTREES is part of the walk_tree api but is unused here.
458 DATA is cgraph_node of the function being walked. */
460 /* FIXME: When this is converted to run over SSA form, this code
461 should be converted to use the operand scanner. */
464 scan_function_op (tree *tp, int *walk_subtrees, void *data)
466 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
467 struct cgraph_node *fn = (struct cgraph_node *) wi->info;
469 funct_state local = get_function_state (fn);
471 switch (TREE_CODE (t))
474 if (DECL_INITIAL (t))
475 walk_tree (&DECL_INITIAL (t), scan_function_op, data, visited_nodes);
480 /* This case is here to find addresses on rhs of constructors in
481 decl_initial of static variables. */
482 check_rhs_var (local, t);
493 scan_function_stmt (gimple_stmt_iterator *gsi_p,
495 struct walk_stmt_info *wi)
497 struct cgraph_node *fn = (struct cgraph_node *) wi->info;
498 gimple stmt = gsi_stmt (*gsi_p);
499 funct_state local = get_function_state (fn);
501 switch (gimple_code (stmt))
505 /* First look on the lhs and see what variable is stored to */
506 tree lhs = gimple_assign_lhs (stmt);
507 tree rhs1 = gimple_assign_rhs1 (stmt);
508 tree rhs2 = gimple_assign_rhs2 (stmt);
509 enum tree_code code = gimple_assign_rhs_code (stmt);
511 check_lhs_var (local, lhs);
513 /* For the purposes of figuring out what the cast affects */
515 /* Next check the operands on the rhs to see if they are ok. */
516 switch (TREE_CODE_CLASS (code))
520 check_rhs_var (local, rhs1);
521 check_rhs_var (local, rhs2);
526 check_rhs_var (local, rhs1);
531 check_rhs_var (local, rhs1);
533 case tcc_declaration:
534 check_rhs_var (local, rhs1);
540 check_rhs_var (local, rhs1);
549 *handled_ops_p = true;
554 if (DECL_NONLOCAL (gimple_label_label (stmt)))
555 /* Target of long jump. */
557 local->pure_const_state = IPA_NEITHER;
558 local->looping = false;
563 check_call (local, stmt);
564 *handled_ops_p = true;
568 get_asm_expr_operands (local, stmt);
569 *handled_ops_p = true;
579 /* This is the main routine for finding the reference patterns for
580 global variables within a function FN. */
583 analyze_function (struct cgraph_node *fn)
585 tree decl = fn->decl;
586 funct_state l = XCNEW (struct funct_state_d);
588 set_function_state (fn, l);
590 l->pure_const_state = IPA_CONST;
591 l->state_set_in_source = false;
592 if (DECL_LOOPING_CONST_OR_PURE_P (decl))
597 /* If this function does not return normally or does not bind local,
598 do not touch this unless it has been marked as const or pure by the
600 if (TREE_THIS_VOLATILE (decl)
601 || !targetm.binds_local_p (decl))
603 l->pure_const_state = IPA_NEITHER;
607 if (TREE_READONLY (decl))
609 l->pure_const_state = IPA_CONST;
610 l->state_set_in_source = true;
612 if (DECL_PURE_P (decl))
614 l->pure_const_state = IPA_PURE;
615 l->state_set_in_source = true;
620 fprintf (dump_file, "\n local analysis of %s with initial value = %d\n ",
621 cgraph_node_name (fn),
622 l->pure_const_state);
625 if (!l->state_set_in_source)
627 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
628 basic_block this_block;
630 FOR_EACH_BB_FN (this_block, this_cfun)
632 gimple_stmt_iterator gsi;
633 struct walk_stmt_info wi;
635 memset (&wi, 0, sizeof(wi));
636 for (gsi = gsi_start_bb (this_block);
641 wi.pset = visited_nodes;
642 walk_gimple_stmt (&gsi, scan_function_stmt, scan_function_op,
644 if (l->pure_const_state == IPA_NEITHER)
649 if (l->pure_const_state != IPA_NEITHER)
651 tree old_decl = current_function_decl;
652 /* Const functions cannot have back edges (an
653 indication of possible infinite loop side
656 current_function_decl = fn->decl;
658 /* The C++ front end, has a tendency to some times jerk away
659 a function after it has created it. This should have
661 gcc_assert (DECL_STRUCT_FUNCTION (fn->decl));
663 push_cfun (DECL_STRUCT_FUNCTION (fn->decl));
665 if (mark_dfs_back_edges ())
666 l->pure_const_state = IPA_NEITHER;
668 current_function_decl = old_decl;
676 fprintf (dump_file, "after local analysis of %s with initial value = %d\n ",
677 cgraph_node_name (fn),
678 l->pure_const_state);
682 /* Called when new function is inserted to callgraph late. */
684 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
686 funct_state_vec = XRESIZEVEC (funct_state, funct_state_vec, cgraph_max_uid);
687 /* There are some shared nodes, in particular the initializers on
688 static declarations. We do not need to scan them more than once
689 since all we would be interested in are the addressof
691 visited_nodes = pointer_set_create ();
692 analyze_function (node);
693 pointer_set_destroy (visited_nodes);
694 visited_nodes = NULL;
698 /* Analyze each function in the cgraph to see if it is locally PURE or
702 generate_summary (void)
704 struct cgraph_node *node;
706 function_insertion_hook_holder =
707 cgraph_add_function_insertion_hook (&add_new_function, NULL);
709 /* There are some shared nodes, in particular the initializers on
710 static declarations. We do not need to scan them more than once
711 since all we would be interested in are the addressof
713 visited_nodes = pointer_set_create ();
715 /* Process all of the functions.
717 We do not want to process any of the clones so we check that this
718 is a master clone. However, we do NOT process any
719 AVAIL_OVERWRITABLE functions (these are never clones) we cannot
720 guarantee that what we learn about the one we see will be true
721 for the one that overrides it.
723 for (node = cgraph_nodes; node; node = node->next)
724 if (node->analyzed && cgraph_is_master_clone (node))
725 analyze_function (node);
727 pointer_set_destroy (visited_nodes);
728 visited_nodes = NULL;
731 /* Produce the global information by preforming a transitive closure
732 on the local information that was produced by generate_summary.
733 Note that there is no function_transform pass since this only
734 updates the function_decl. */
739 struct cgraph_node *node;
740 struct cgraph_node *w;
741 struct cgraph_node **order =
742 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
745 struct ipa_dfs_info * w_info;
747 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
748 order_pos = ipa_utils_reduced_inorder (order, true, false);
751 dump_cgraph (dump_file);
752 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
755 /* Propagate the local information thru the call graph to produce
756 the global information. All the nodes within a cycle will have
757 the same info so we collapse cycles first. Then we can do the
758 propagation in one pass from the leaves to the roots. */
759 for (i = 0; i < order_pos; i++ )
761 enum pure_const_state_e pure_const_state = IPA_CONST;
762 bool looping = false;
766 /* Find the worst state for any node in the cycle. */
770 funct_state w_l = get_function_state (w);
771 if (pure_const_state < w_l->pure_const_state)
772 pure_const_state = w_l->pure_const_state;
777 if (pure_const_state == IPA_NEITHER)
780 if (!w_l->state_set_in_source)
782 struct cgraph_edge *e;
788 for (e = w->callees; e; e = e->next_callee)
790 struct cgraph_node *y = e->callee;
791 /* Only look at the master nodes and skip external nodes. */
792 y = cgraph_master_clone (y);
798 funct_state y_l = get_function_state (y);
799 if (pure_const_state < y_l->pure_const_state)
800 pure_const_state = y_l->pure_const_state;
801 if (pure_const_state == IPA_NEITHER)
808 w_info = (struct ipa_dfs_info *) w->aux;
809 w = w_info->next_cycle;
812 /* Copy back the region's pure_const_state which is shared by
813 all nodes in the region. */
817 funct_state w_l = get_function_state (w);
819 /* All nodes within a cycle share the same info. */
820 if (!w_l->state_set_in_source)
822 w_l->pure_const_state = pure_const_state;
823 w_l->looping = looping;
825 switch (pure_const_state)
828 TREE_READONLY (w->decl) = 1;
829 DECL_LOOPING_CONST_OR_PURE_P (w->decl) = looping;
831 fprintf (dump_file, "Function found to be %sconst: %s\n",
832 looping ? "looping " : "",
833 lang_hooks.decl_printable_name(w->decl, 2));
837 DECL_PURE_P (w->decl) = 1;
838 DECL_LOOPING_CONST_OR_PURE_P (w->decl) = looping;
840 fprintf (dump_file, "Function found to be %spure: %s\n",
841 looping ? "looping " : "",
842 lang_hooks.decl_printable_name(w->decl, 2));
849 w_info = (struct ipa_dfs_info *) w->aux;
850 w = w_info->next_cycle;
855 for (node = cgraph_nodes; node; node = node->next)
857 /* Get rid of the aux information. */
860 w_info = (struct ipa_dfs_info *) node->aux;
864 if (node->analyzed && cgraph_is_master_clone (node))
865 free (get_function_state (node));
874 gate_pure_const (void)
876 return (flag_ipa_pure_const
877 /* Don't bother doing anything if the program has errors. */
878 && !(errorcount || sorrycount));
881 struct ipa_opt_pass pass_ipa_pure_const =
885 "pure-const", /* name */
886 gate_pure_const, /* gate */
887 propagate, /* execute */
890 0, /* static_pass_number */
891 TV_IPA_PURE_CONST, /* tv_id */
892 0, /* properties_required */
893 0, /* properties_provided */
894 0, /* properties_destroyed */
895 0, /* todo_flags_start */
896 0 /* todo_flags_finish */
898 generate_summary, /* generate_summary */
899 NULL, /* write_summary */
900 NULL, /* read_summary */
901 NULL, /* function_read_summary */
903 NULL, /* function_transform */
904 NULL /* variable_transform */