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;
74 /* What user set here; we can be always sure about this. */
75 enum pure_const_state_e state_set_in_source;
77 /* True if the function could possibly infinite loop. There are a
78 lot of ways that this could be determined. We are pretty
79 conservative here. While it is possible to cse pure and const
80 calls, it is not legal to have dce get rid of the call if there
81 is a possibility that the call could infinite loop since this is
82 a behavioral change. */
88 typedef struct funct_state_d * funct_state;
90 /* The storage of the funct_state is abstracted because there is the
91 possibility that it may be desirable to move this to the cgraph
94 /* Array, indexed by cgraph node uid, of function states. */
96 DEF_VEC_P (funct_state);
97 DEF_VEC_ALLOC_P (funct_state, heap);
98 static VEC (funct_state, heap) *funct_state_vec;
100 /* Holders of ipa cgraph hooks: */
101 static struct cgraph_node_hook_list *function_insertion_hook_holder;
102 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
103 static struct cgraph_node_hook_list *node_removal_hook_holder;
105 /* Init the function state. */
110 free (funct_state_vec);
114 /* Return the function state from NODE. */
116 static inline funct_state
117 get_function_state (struct cgraph_node *node)
120 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
122 return VEC_index (funct_state, funct_state_vec, node->uid);
125 /* Set the function state S for NODE. */
128 set_function_state (struct cgraph_node *node, funct_state s)
131 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
132 VEC_safe_grow_cleared (funct_state, heap, funct_state_vec, node->uid + 1);
133 VEC_replace (funct_state, 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 /* Do not want to do anything with volatile except mark any
144 function that uses one to be not const or pure. */
145 if (TREE_THIS_VOLATILE (t))
147 local->pure_const_state = IPA_NEITHER;
149 fprintf (dump_file, " Volatile operand is not const/pure");
153 /* Do not care about a local automatic that is not static. */
154 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
157 /* If the variable has the "used" attribute, treat it as if it had a
158 been touched by the devil. */
159 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
161 local->pure_const_state = IPA_NEITHER;
163 fprintf (dump_file, " Used static/global variable is not const/pure\n");
167 /* Since we have dealt with the locals and params cases above, if we
168 are CHECKING_WRITE, this cannot be a pure or constant
172 local->pure_const_state = IPA_NEITHER;
174 fprintf (dump_file, " static/global memory write is not const/pure\n");
178 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
180 /* Readonly reads are safe. */
181 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
182 return; /* Read of a constant, do not change the function state. */
186 fprintf (dump_file, " global memory read is not const\n");
187 /* Just a regular read. */
188 if (local->pure_const_state == IPA_CONST)
189 local->pure_const_state = IPA_PURE;
194 /* Compilation level statics can be read if they are readonly
196 if (TREE_READONLY (t))
200 fprintf (dump_file, " static memory read is not const\n");
201 /* Just a regular read. */
202 if (local->pure_const_state == IPA_CONST)
203 local->pure_const_state = IPA_PURE;
208 /* Check to see if the use (or definition when CHECKING_WRITE is true)
209 variable T is legal in a function that is either pure or const. */
212 check_op (funct_state local, tree t, bool checking_write)
214 if (TREE_THIS_VOLATILE (t))
216 local->pure_const_state = IPA_NEITHER;
218 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
221 else if (checking_write)
223 local->pure_const_state = IPA_NEITHER;
225 fprintf (dump_file, " Indirect ref write is not const/pure\n");
231 fprintf (dump_file, " Indirect ref read is not const\n");
232 if (local->pure_const_state == IPA_CONST)
233 local->pure_const_state = IPA_PURE;
237 /* Check the parameters of a function call to CALL_EXPR to see if
238 there are any references in the parameters that are not allowed for
239 pure or const functions. Also check to see if this is either an
240 indirect call, a call outside the compilation unit, or has special
241 attributes that may also effect the purity. The CALL_EXPR node for
242 the entire call expression. */
245 check_call (funct_state local, gimple call, bool ipa)
247 int flags = gimple_call_flags (call);
248 tree callee_t = gimple_call_fndecl (call);
249 struct cgraph_node* callee;
250 enum availability avail = AVAIL_NOT_AVAILABLE;
251 bool possibly_throws = stmt_could_throw_p (call);
252 bool possibly_throws_externally = (possibly_throws
253 && stmt_can_throw_external (call));
258 for (i = 0; i < gimple_num_ops (call); i++)
259 if (gimple_op (call, i)
260 && tree_could_throw_p (gimple_op (call, i)))
262 if (possibly_throws && flag_non_call_exceptions)
265 fprintf (dump_file, " operand can throw; looping\n");
266 local->looping = true;
268 if (possibly_throws_externally)
271 fprintf (dump_file, " operand can throw externally\n");
272 local->can_throw = true;
277 /* The const and pure flags are set by a variety of places in the
278 compiler (including here). If someone has already set the flags
279 for the callee, (such as for some of the builtins) we will use
280 them, otherwise we will compute our own information.
282 Const and pure functions have less clobber effects than other
283 functions so we process these first. Otherwise if it is a call
284 outside the compilation unit or an indirect call we punt. This
285 leaves local calls which will be processed by following the call
289 callee = cgraph_node(callee_t);
290 avail = cgraph_function_body_availability (callee);
292 /* When bad things happen to bad functions, they cannot be const
294 if (setjmp_call_p (callee_t))
297 fprintf (dump_file, " setjmp is not const/pure\n");
298 local->looping = true;
299 local->pure_const_state = IPA_NEITHER;
302 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
303 switch (DECL_FUNCTION_CODE (callee_t))
305 case BUILT_IN_LONGJMP:
306 case BUILT_IN_NONLOCAL_GOTO:
308 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
309 local->pure_const_state = IPA_NEITHER;
310 local->looping = true;
317 /* When not in IPA mode, we can still handle self recursion. */
318 if (!ipa && callee_t == current_function_decl)
319 local->looping = true;
320 /* The callee is either unknown (indirect call) or there is just no
321 scannable code for it (external call) . We look to see if there
322 are any bits available for the callee (such as by declaration or
323 because it is builtin) and process solely on the basis of those
325 else if (avail <= AVAIL_OVERWRITABLE || !ipa)
327 if (possibly_throws && flag_non_call_exceptions)
330 fprintf (dump_file, " can throw; looping\n");
331 local->looping = true;
333 if (possibly_throws_externally)
337 fprintf (dump_file, " can throw externally in region %i\n",
338 lookup_stmt_eh_region (call));
340 fprintf (dump_file, " callee:%s\n",
341 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
343 local->can_throw = true;
345 if (flags & ECF_CONST)
347 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
348 local->looping = true;
350 else if (flags & ECF_PURE)
352 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
353 local->looping = true;
355 fprintf (dump_file, " pure function call in not const\n");
356 if (local->pure_const_state == IPA_CONST)
357 local->pure_const_state = IPA_PURE;
362 fprintf (dump_file, " uknown function call is not const/pure\n");
363 local->pure_const_state = IPA_NEITHER;
364 local->looping = true;
367 /* Direct functions calls are handled by IPA propagation. */
370 /* Wrapper around check_decl for loads. */
373 check_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
376 check_decl ((funct_state)data, op, false);
378 check_op ((funct_state)data, op, false);
382 /* Wrapper around check_decl for stores. */
385 check_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
388 check_decl ((funct_state)data, op, true);
390 check_op ((funct_state)data, op, true);
394 /* Look into pointer pointed to by GSIP and figure out what interesting side
397 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
399 gimple stmt = gsi_stmt (*gsip);
404 fprintf (dump_file, " scanning: ");
405 print_gimple_stmt (dump_file, stmt, 0, 0);
408 /* Look for loads and stores. */
409 walk_stmt_load_store_ops (stmt, local, check_load, check_store);
411 if (gimple_code (stmt) != GIMPLE_CALL
412 && stmt_could_throw_p (stmt))
414 if (flag_non_call_exceptions)
417 fprintf (dump_file, " can throw; looping");
418 local->looping = true;
420 if (stmt_can_throw_external (stmt))
423 fprintf (dump_file, " can throw externally");
424 local->can_throw = true;
427 switch (gimple_code (stmt))
430 check_call (local, stmt, ipa);
433 if (DECL_NONLOCAL (gimple_label_label (stmt)))
434 /* Target of long jump. */
437 fprintf (dump_file, " nonlocal label is not const/pure");
438 local->pure_const_state = IPA_NEITHER;
442 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
444 tree op = gimple_asm_clobber_op (stmt, i);
445 if (simple_cst_equal(TREE_VALUE (op), memory_identifier_string) == 1)
448 fprintf (dump_file, " memory asm clobber is not const/pure");
449 /* Abandon all hope, ye who enter here. */
450 local->pure_const_state = IPA_NEITHER;
453 if (gimple_asm_volatile_p (stmt))
456 fprintf (dump_file, " volatile is not const/pure");
457 /* Abandon all hope, ye who enter here. */
458 local->pure_const_state = IPA_NEITHER;
459 local->looping = true;
468 /* This is the main routine for finding the reference patterns for
469 global variables within a function FN. */
472 analyze_function (struct cgraph_node *fn, bool ipa)
474 tree decl = fn->decl;
475 tree old_decl = current_function_decl;
477 basic_block this_block;
479 if (cgraph_function_body_availability (fn) <= AVAIL_OVERWRITABLE)
482 fprintf (dump_file, "Function is not available or overwrittable; not analyzing.\n");
486 l = XCNEW (struct funct_state_d);
487 l->pure_const_state = IPA_CONST;
488 l->state_set_in_source = IPA_NEITHER;
490 l->can_throw = false;
494 fprintf (dump_file, "\n\n local analysis of %s\n ",
495 cgraph_node_name (fn));
498 push_cfun (DECL_STRUCT_FUNCTION (decl));
499 current_function_decl = decl;
501 FOR_EACH_BB (this_block)
503 gimple_stmt_iterator gsi;
504 struct walk_stmt_info wi;
506 memset (&wi, 0, sizeof(wi));
507 for (gsi = gsi_start_bb (this_block);
511 check_stmt (&gsi, l, ipa);
512 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
518 if (l->pure_const_state != IPA_NEITHER)
520 /* Const functions cannot have back edges (an
521 indication of possible infinite loop side
523 if (mark_dfs_back_edges ())
528 if (TREE_READONLY (decl))
530 l->pure_const_state = IPA_CONST;
531 l->state_set_in_source = IPA_CONST;
532 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
535 if (DECL_PURE_P (decl))
537 if (l->pure_const_state != IPA_CONST)
538 l->pure_const_state = IPA_PURE;
539 l->state_set_in_source = IPA_PURE;
540 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
543 if (TREE_NOTHROW (decl))
544 l->can_throw = false;
547 current_function_decl = old_decl;
551 fprintf (dump_file, "Function is locally looping.\n");
553 fprintf (dump_file, "Function is locally throwing.\n");
554 if (l->pure_const_state == IPA_CONST)
555 fprintf (dump_file, "Function is locally const.\n");
556 if (l->pure_const_state == IPA_PURE)
557 fprintf (dump_file, "Function is locally pure.\n");
562 /* Called when new function is inserted to callgraph late. */
564 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
566 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
568 /* There are some shared nodes, in particular the initializers on
569 static declarations. We do not need to scan them more than once
570 since all we would be interested in are the addressof
572 visited_nodes = pointer_set_create ();
573 set_function_state (node, analyze_function (node, true));
574 pointer_set_destroy (visited_nodes);
575 visited_nodes = NULL;
578 /* Called when new clone is inserted to callgraph late. */
581 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
582 void *data ATTRIBUTE_UNUSED)
584 if (get_function_state (src))
586 funct_state l = XNEW (struct funct_state_d);
587 gcc_assert (!get_function_state (dst));
588 memcpy (l, get_function_state (src), sizeof (*l));
589 set_function_state (dst, l);
593 /* Called when new clone is inserted to callgraph late. */
596 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
598 if (get_function_state (node))
600 free (get_function_state (node));
601 set_function_state (node, NULL);
606 /* Analyze each function in the cgraph to see if it is locally PURE or
610 generate_summary (void)
612 struct cgraph_node *node;
614 node_removal_hook_holder =
615 cgraph_add_node_removal_hook (&remove_node_data, NULL);
616 node_duplication_hook_holder =
617 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
618 function_insertion_hook_holder =
619 cgraph_add_function_insertion_hook (&add_new_function, NULL);
620 /* There are some shared nodes, in particular the initializers on
621 static declarations. We do not need to scan them more than once
622 since all we would be interested in are the addressof
624 visited_nodes = pointer_set_create ();
626 /* Process all of the functions.
628 We do NOT process any AVAIL_OVERWRITABLE functions, we cannot
629 guarantee that what we learn about the one we see will be true
630 for the one that overrides it.
632 for (node = cgraph_nodes; node; node = node->next)
633 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
634 set_function_state (node, analyze_function (node, true));
636 pointer_set_destroy (visited_nodes);
637 visited_nodes = NULL;
640 /* Produce the global information by preforming a transitive closure
641 on the local information that was produced by generate_summary.
642 Note that there is no function_transform pass since this only
643 updates the function_decl. */
648 struct cgraph_node *node;
649 struct cgraph_node *w;
650 struct cgraph_node **order =
651 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
654 struct ipa_dfs_info * w_info;
656 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
657 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
658 cgraph_remove_node_removal_hook (node_removal_hook_holder);
659 order_pos = ipa_utils_reduced_inorder (order, true, false);
662 dump_cgraph (dump_file);
663 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
666 /* Propagate the local information thru the call graph to produce
667 the global information. All the nodes within a cycle will have
668 the same info so we collapse cycles first. Then we can do the
669 propagation in one pass from the leaves to the roots. */
670 for (i = 0; i < order_pos; i++ )
672 enum pure_const_state_e pure_const_state = IPA_CONST;
673 bool looping = false;
674 bool can_throw = false;
678 /* Find the worst state for any node in the cycle. */
682 struct cgraph_edge *e;
683 funct_state w_l = get_function_state (w);
684 if (pure_const_state < w_l->pure_const_state)
685 pure_const_state = w_l->pure_const_state;
692 if (pure_const_state == IPA_NEITHER
701 for (e = w->callees; e; e = e->next_callee)
703 struct cgraph_node *y = e->callee;
705 if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
707 funct_state y_l = get_function_state (y);
708 if (pure_const_state < y_l->pure_const_state)
709 pure_const_state = y_l->pure_const_state;
710 if (pure_const_state == IPA_NEITHER
715 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
716 /* FIXME: We should check that the throw can get external.
717 We also should handle only loops formed by can throw external
722 w_info = (struct ipa_dfs_info *) w->aux;
723 w = w_info->next_cycle;
726 /* Copy back the region's pure_const_state which is shared by
727 all nodes in the region. */
731 funct_state w_l = get_function_state (w);
732 enum pure_const_state_e this_state = pure_const_state;
733 bool this_looping = looping;
735 if (w_l->state_set_in_source != IPA_NEITHER)
737 if (this_state > w_l->state_set_in_source)
738 this_state = w_l->state_set_in_source;
739 this_looping = false;
742 /* All nodes within a cycle share the same info. */
743 w_l->pure_const_state = this_state;
744 w_l->looping = this_looping;
749 if (!TREE_READONLY (w->decl) && dump_file)
750 fprintf (dump_file, "Function found to be %sconst: %s\n",
751 this_looping ? "looping " : "",
752 cgraph_node_name (w));
753 TREE_READONLY (w->decl) = 1;
754 DECL_LOOPING_CONST_OR_PURE_P (w->decl) = this_looping;
758 if (!DECL_PURE_P (w->decl) && dump_file)
759 fprintf (dump_file, "Function found to be %spure: %s\n",
760 this_looping ? "looping " : "",
761 cgraph_node_name (w));
762 DECL_PURE_P (w->decl) = 1;
763 DECL_LOOPING_CONST_OR_PURE_P (w->decl) = this_looping;
769 if (!can_throw && !TREE_NOTHROW (w->decl))
771 /* FIXME: TREE_NOTHROW is not set because passmanager will execute
772 verify_ssa and verify_cfg on every function. Before fixup_cfg is done,
773 those functions are going to have NOTHROW calls in EH regions reulting
776 fprintf (dump_file, "Function found to be nothrow: %s\n",
777 cgraph_node_name (w));
779 w_info = (struct ipa_dfs_info *) w->aux;
780 w = w_info->next_cycle;
785 for (node = cgraph_nodes; node; node = node->next)
787 /* Get rid of the aux information. */
790 w_info = (struct ipa_dfs_info *) node->aux;
794 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
795 free (get_function_state (node));
799 VEC_free (funct_state, heap, funct_state_vec);
805 gate_pure_const (void)
807 return (flag_ipa_pure_const
808 /* Don't bother doing anything if the program has errors. */
809 && !(errorcount || sorrycount));
812 struct ipa_opt_pass pass_ipa_pure_const =
816 "pure-const", /* name */
817 gate_pure_const, /* gate */
818 propagate, /* execute */
821 0, /* static_pass_number */
822 TV_IPA_PURE_CONST, /* tv_id */
823 0, /* properties_required */
824 0, /* properties_provided */
825 0, /* properties_destroyed */
826 0, /* todo_flags_start */
827 0 /* todo_flags_finish */
829 generate_summary, /* generate_summary */
830 NULL, /* write_summary */
831 NULL, /* read_summary */
832 NULL, /* function_read_summary */
834 NULL, /* function_transform */
835 NULL /* variable_transform */
838 /* Simple local pass for pure const discovery reusing the analysis from
839 ipa_pure_const. This pass is effective when executed together with
840 other optimization passes in early optimization pass queue. */
843 local_pure_const (void)
845 bool changed = false;
848 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
849 we must not promote functions that are called by already processed functions. */
851 if (function_called_by_processed_nodes_p ())
854 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
858 l = analyze_function (cgraph_node (current_function_decl), false);
862 fprintf (dump_file, "Function has wrong visibility; ignoring\n");
866 switch (l->pure_const_state)
869 if (!TREE_READONLY (current_function_decl))
871 TREE_READONLY (current_function_decl) = 1;
872 DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) = l->looping;
875 fprintf (dump_file, "Function found to be %sconst: %s\n",
876 l->looping ? "looping " : "",
877 lang_hooks.decl_printable_name (current_function_decl,
880 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
883 DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) = false;
886 fprintf (dump_file, "Function found to be non-looping: %s\n",
887 lang_hooks.decl_printable_name (current_function_decl,
893 if (!TREE_READONLY (current_function_decl))
895 DECL_PURE_P (current_function_decl) = 1;
896 DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) = l->looping;
899 fprintf (dump_file, "Function found to be %spure: %s\n",
900 l->looping ? "looping " : "",
901 lang_hooks.decl_printable_name (current_function_decl,
904 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
907 DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) = false;
910 fprintf (dump_file, "Function found to be non-looping: %s\n",
911 lang_hooks.decl_printable_name (current_function_decl,
919 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
921 TREE_NOTHROW (current_function_decl) = 1;
924 fprintf (dump_file, "Function found to be nothrow: %s\n",
925 lang_hooks.decl_printable_name (current_function_decl,
931 return execute_fixup_cfg ();
936 struct gimple_opt_pass pass_local_pure_const =
940 "local-pure-const", /* name */
941 gate_pure_const, /* gate */
942 local_pure_const, /* execute */
945 0, /* static_pass_number */
946 TV_IPA_PURE_CONST, /* tv_id */
947 0, /* properties_required */
948 0, /* properties_provided */
949 0, /* properties_destroyed */
950 0, /* todo_flags_start */
951 0 /* todo_flags_finish */