2007-01-03 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ipa-pure-const.c
blob5f19af8c61ea3cec91e61bf79fb68a1ddfb0138f
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005 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 2, or (at your option) any later
10 version.
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
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This file mark functions as being either const (TREE_READONLY) or
23 pure (DECL_IS_PURE).
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. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "tree.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"
44 #include "ggc.h"
45 #include "ipa-utils.h"
46 #include "c-common.h"
47 #include "tree-gimple.h"
48 #include "cgraph.h"
49 #include "output.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "diagnostic.h"
53 #include "langhooks.h"
54 #include "target.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
60 what is found. */
61 enum pure_const_state_e
63 IPA_CONST,
64 IPA_PURE,
65 IPA_NEITHER
68 /* Holder inserted into the ipa_dfs_info aux field to hold the
69 const_state. */
70 struct funct_state_d
72 enum pure_const_state_e pure_const_state;
73 bool state_set_in_source;
76 typedef struct funct_state_d * funct_state;
78 /* Return the function state from NODE. */
80 static inline funct_state
81 get_function_state (struct cgraph_node *node)
83 struct ipa_dfs_info * info = node->aux;
84 return info->aux;
87 /* Check to see if the use (or definition when CHECHING_WRITE is true)
88 variable T is legal in a function that is either pure or const. */
90 static inline void
91 check_decl (funct_state local,
92 tree t, bool checking_write)
94 /* If the variable has the "used" attribute, treat it as if it had a
95 been touched by the devil. */
96 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
98 local->pure_const_state = IPA_NEITHER;
99 return;
102 /* Do not want to do anything with volatile except mark any
103 function that uses one to be not const or pure. */
104 if (TREE_THIS_VOLATILE (t))
106 local->pure_const_state = IPA_NEITHER;
107 return;
110 /* Do not care about a local automatic that is not static. */
111 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
112 return;
114 /* Since we have dealt with the locals and params cases above, if we
115 are CHECKING_WRITE, this cannot be a pure or constant
116 function. */
117 if (checking_write)
118 local->pure_const_state = IPA_NEITHER;
120 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
122 /* If the front end set the variable to be READONLY and
123 constant, we can allow this variable in pure or const
124 functions but the scope is too large for our analysis to set
125 these bits ourselves. */
127 if (TREE_READONLY (t)
128 && DECL_INITIAL (t)
129 && is_gimple_min_invariant (DECL_INITIAL (t)))
130 ; /* Read of a constant, do not change the function state. */
131 else
133 /* Just a regular read. */
134 if (local->pure_const_state == IPA_CONST)
135 local->pure_const_state = IPA_PURE;
139 /* Compilation level statics can be read if they are readonly
140 variables. */
141 if (TREE_READONLY (t))
142 return;
144 /* Just a regular read. */
145 if (local->pure_const_state == IPA_CONST)
146 local->pure_const_state = IPA_PURE;
149 /* If T is a VAR_DECL check to see if it is an allowed reference. */
151 static void
152 check_operand (funct_state local,
153 tree t, bool checking_write)
155 if (!t) return;
157 if (TREE_CODE (t) == VAR_DECL)
158 check_decl (local, t, checking_write);
161 /* Examine tree T for references. */
163 static void
164 check_tree (funct_state local, tree t, bool checking_write)
166 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR)
167 || TREE_CODE (t) == SSA_NAME)
168 return;
170 /* Any tree which is volatile disqualifies thie function from being
171 const or pure. */
172 if (TREE_THIS_VOLATILE (t))
174 local->pure_const_state = IPA_NEITHER;
175 return;
178 while (TREE_CODE (t) == REALPART_EXPR
179 || TREE_CODE (t) == IMAGPART_EXPR
180 || handled_component_p (t))
182 if (TREE_CODE (t) == ARRAY_REF)
183 check_operand (local, TREE_OPERAND (t, 1), false);
184 t = TREE_OPERAND (t, 0);
187 /* The bottom of an indirect reference can only be read, not
188 written. */
189 if (INDIRECT_REF_P (t))
191 check_tree (local, TREE_OPERAND (t, 0), false);
193 /* Any indirect reference that occurs on the lhs
194 disqualifies the function from being pure or const. Any
195 indirect reference that occurs on the rhs disqualifies the
196 function from being const. */
197 if (checking_write)
199 local->pure_const_state = IPA_NEITHER;
200 return;
202 else if (local->pure_const_state == IPA_CONST)
203 local->pure_const_state = IPA_PURE;
206 if (SSA_VAR_P (t))
207 check_operand (local, t, checking_write);
210 /* Scan tree T to see if there are any addresses taken in within T. */
212 static void
213 look_for_address_of (funct_state local, tree t)
215 if (TREE_CODE (t) == ADDR_EXPR)
217 tree x = get_base_var (t);
218 if (TREE_CODE (x) == VAR_DECL)
220 check_decl (local, x, false);
222 /* Taking the address of something appears to be reasonable
223 in PURE code. Not allowed in const. */
224 if (local->pure_const_state == IPA_CONST)
225 local->pure_const_state = IPA_PURE;
230 /* Check to see if T is a read or address of operation on a var we are
231 interested in analyzing. LOCAL is passed in to get access to its
232 bit vectors. */
234 static void
235 check_rhs_var (funct_state local, tree t)
237 look_for_address_of (local, t);
239 /* Memcmp and strlen can both trap and they are declared pure. */
240 if (tree_could_trap_p (t)
241 && local->pure_const_state == IPA_CONST)
242 local->pure_const_state = IPA_PURE;
244 check_tree(local, t, false);
247 /* Check to see if T is an assignment to a var we are interested in
248 analyzing. LOCAL is passed in to get access to its bit vectors. */
250 static void
251 check_lhs_var (funct_state local, tree t)
253 /* Memcmp and strlen can both trap and they are declared pure.
254 Which seems to imply that we can apply the same rule here. */
255 if (tree_could_trap_p (t)
256 && local->pure_const_state == IPA_CONST)
257 local->pure_const_state = IPA_PURE;
259 check_tree(local, t, true);
262 /* This is a scaled down version of get_asm_expr_operands from
263 tree_ssa_operands.c. The version there runs much later and assumes
264 that aliasing information is already available. Here we are just
265 trying to find if the set of inputs and outputs contain references
266 or address of operations to local static variables. STMT is the
267 actual asm statement. */
269 static void
270 get_asm_expr_operands (funct_state local, tree stmt)
272 int noutputs = list_length (ASM_OUTPUTS (stmt));
273 const char **oconstraints
274 = (const char **) alloca ((noutputs) * sizeof (const char *));
275 int i;
276 tree link;
277 const char *constraint;
278 bool allows_mem, allows_reg, is_inout;
280 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
282 oconstraints[i] = constraint
283 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
284 parse_output_constraint (&constraint, i, 0, 0,
285 &allows_mem, &allows_reg, &is_inout);
287 check_lhs_var (local, TREE_VALUE (link));
290 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
292 constraint
293 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
294 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
295 oconstraints, &allows_mem, &allows_reg);
297 check_rhs_var (local, TREE_VALUE (link));
300 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
301 if (simple_cst_equal(TREE_VALUE (link), memory_identifier_string) == 1)
302 /* Abandon all hope, ye who enter here. */
303 local->pure_const_state = IPA_NEITHER;
305 if (ASM_VOLATILE_P (stmt))
306 local->pure_const_state = IPA_NEITHER;
309 /* Check the parameters of a function call to CALL_EXPR to see if
310 there are any references in the parameters that are not allowed for
311 pure or const functions. Also check to see if this is either an
312 indirect call, a call outside the compilation unit, or has special
313 attributes that may also effect the purity. The CALL_EXPR node for
314 the entire call expression. */
316 static void
317 check_call (funct_state local, tree call_expr)
319 int flags = call_expr_flags(call_expr);
320 tree operand_list = TREE_OPERAND (call_expr, 1);
321 tree operand;
322 tree callee_t = get_callee_fndecl (call_expr);
323 struct cgraph_node* callee;
324 enum availability avail = AVAIL_NOT_AVAILABLE;
326 for (operand = operand_list;
327 operand != NULL_TREE;
328 operand = TREE_CHAIN (operand))
330 tree argument = TREE_VALUE (operand);
331 check_rhs_var (local, argument);
334 /* The const and pure flags are set by a variety of places in the
335 compiler (including here). If someone has already set the flags
336 for the callee, (such as for some of the builtins) we will use
337 them, otherwise we will compute our own information.
339 Const and pure functions have less clobber effects than other
340 functions so we process these first. Otherwise if it is a call
341 outside the compilation unit or an indirect call we punt. This
342 leaves local calls which will be processed by following the call
343 graph. */
344 if (callee_t)
346 callee = cgraph_node(callee_t);
347 avail = cgraph_function_body_availability (callee);
349 /* When bad things happen to bad functions, they cannot be const
350 or pure. */
351 if (setjmp_call_p (callee_t))
352 local->pure_const_state = IPA_NEITHER;
354 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
355 switch (DECL_FUNCTION_CODE (callee_t))
357 case BUILT_IN_LONGJMP:
358 case BUILT_IN_NONLOCAL_GOTO:
359 local->pure_const_state = IPA_NEITHER;
360 break;
361 default:
362 break;
366 /* The callee is either unknown (indirect call) or there is just no
367 scannable code for it (external call) . We look to see if there
368 are any bits available for the callee (such as by declaration or
369 because it is builtin) and process solely on the basis of those
370 bits. */
371 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
373 if (flags & ECF_PURE)
375 if (local->pure_const_state == IPA_CONST)
376 local->pure_const_state = IPA_PURE;
378 else
379 local->pure_const_state = IPA_NEITHER;
381 else
383 /* We have the code and we will scan it for the effects. */
384 if (flags & ECF_PURE)
386 if (local->pure_const_state == IPA_CONST)
387 local->pure_const_state = IPA_PURE;
392 /* TP is the part of the tree currently under the microscope.
393 WALK_SUBTREES is part of the walk_tree api but is unused here.
394 DATA is cgraph_node of the function being walked. */
396 /* FIXME: When this is converted to run over SSA form, this code
397 should be converted to use the operand scanner. */
399 static tree
400 scan_function (tree *tp,
401 int *walk_subtrees,
402 void *data)
404 struct cgraph_node *fn = data;
405 tree t = *tp;
406 funct_state local = get_function_state (fn);
408 switch (TREE_CODE (t))
410 case VAR_DECL:
411 if (DECL_INITIAL (t))
412 walk_tree (&DECL_INITIAL (t), scan_function, fn, visited_nodes);
413 *walk_subtrees = 0;
414 break;
416 case GIMPLE_MODIFY_STMT:
418 /* First look on the lhs and see what variable is stored to */
419 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
420 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
421 check_lhs_var (local, lhs);
423 /* For the purposes of figuring out what the cast affects */
425 /* Next check the operands on the rhs to see if they are ok. */
426 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
428 case tcc_binary:
430 tree op0 = TREE_OPERAND (rhs, 0);
431 tree op1 = TREE_OPERAND (rhs, 1);
432 check_rhs_var (local, op0);
433 check_rhs_var (local, op1);
435 break;
436 case tcc_unary:
438 tree op0 = TREE_OPERAND (rhs, 0);
439 check_rhs_var (local, op0);
442 break;
443 case tcc_reference:
444 check_rhs_var (local, rhs);
445 break;
446 case tcc_declaration:
447 check_rhs_var (local, rhs);
448 break;
449 case tcc_expression:
450 switch (TREE_CODE (rhs))
452 case ADDR_EXPR:
453 check_rhs_var (local, rhs);
454 break;
455 case CALL_EXPR:
456 check_call (local, rhs);
457 break;
458 default:
459 break;
461 break;
462 default:
463 break;
465 *walk_subtrees = 0;
467 break;
469 case ADDR_EXPR:
470 /* This case is here to find addresses on rhs of constructors in
471 decl_initial of static variables. */
472 check_rhs_var (local, t);
473 *walk_subtrees = 0;
474 break;
476 case LABEL_EXPR:
477 if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
478 /* Target of long jump. */
479 local->pure_const_state = IPA_NEITHER;
480 break;
482 case CALL_EXPR:
483 check_call (local, t);
484 *walk_subtrees = 0;
485 break;
487 case ASM_EXPR:
488 get_asm_expr_operands (local, t);
489 *walk_subtrees = 0;
490 break;
492 default:
493 break;
495 return NULL;
498 /* This is the main routine for finding the reference patterns for
499 global variables within a function FN. */
501 static void
502 analyze_function (struct cgraph_node *fn)
504 funct_state l = XCNEW (struct funct_state_d);
505 tree decl = fn->decl;
506 struct ipa_dfs_info * w_info = fn->aux;
508 w_info->aux = l;
510 l->pure_const_state = IPA_CONST;
511 l->state_set_in_source = false;
513 /* If this function does not return normally or does not bind local,
514 do not touch this unless it has been marked as const or pure by the
515 front end. */
516 if (TREE_THIS_VOLATILE (decl)
517 || !targetm.binds_local_p (decl))
519 l->pure_const_state = IPA_NEITHER;
520 return;
523 if (TREE_READONLY (decl))
525 l->pure_const_state = IPA_CONST;
526 l->state_set_in_source = true;
528 if (DECL_IS_PURE (decl))
530 l->pure_const_state = IPA_PURE;
531 l->state_set_in_source = true;
534 if (dump_file)
536 fprintf (dump_file, "\n local analysis of %s with initial value = %d\n ",
537 cgraph_node_name (fn),
538 l->pure_const_state);
541 if (!l->state_set_in_source)
543 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
544 basic_block this_block;
546 FOR_EACH_BB_FN (this_block, this_cfun)
548 block_stmt_iterator bsi;
549 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
551 walk_tree (bsi_stmt_ptr (bsi), scan_function,
552 fn, visited_nodes);
553 if (l->pure_const_state == IPA_NEITHER)
554 goto end;
558 if (l->pure_const_state != IPA_NEITHER)
560 tree old_decl = current_function_decl;
561 /* Const functions cannot have back edges (an
562 indication of possible infinite loop side
563 effect. */
565 current_function_decl = fn->decl;
567 /* The C++ front end, has a tendency to some times jerk away
568 a function after it has created it. This should have
569 been fixed. */
570 gcc_assert (DECL_STRUCT_FUNCTION (fn->decl));
572 push_cfun (DECL_STRUCT_FUNCTION (fn->decl));
574 if (mark_dfs_back_edges ())
575 l->pure_const_state = IPA_NEITHER;
577 current_function_decl = old_decl;
578 pop_cfun ();
582 end:
583 if (dump_file)
585 fprintf (dump_file, "after local analysis of %s with initial value = %d\n ",
586 cgraph_node_name (fn),
587 l->pure_const_state);
592 /* Produce the global information by preforming a transitive closure
593 on the local information that was produced by ipa_analyze_function
594 and ipa_analyze_variable. */
596 static unsigned int
597 static_execute (void)
599 struct cgraph_node *node;
600 struct cgraph_node *w;
601 struct cgraph_node **order =
602 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
603 int order_pos = order_pos = ipa_utils_reduced_inorder (order, true, false);
604 int i;
605 struct ipa_dfs_info * w_info;
607 if (!memory_identifier_string)
608 memory_identifier_string = build_string(7, "memory");
610 /* There are some shared nodes, in particular the initializers on
611 static declarations. We do not need to scan them more than once
612 since all we would be interested in are the addressof
613 operations. */
614 visited_nodes = pointer_set_create ();
616 /* Process all of the functions.
618 We do not want to process any of the clones so we check that this
619 is a master clone. However, we do NOT process any
620 AVAIL_OVERWRITABLE functions (these are never clones) we cannot
621 guarantee that what we learn about the one we see will be true
622 for the one that overriders it.
624 for (node = cgraph_nodes; node; node = node->next)
625 if (node->analyzed && cgraph_is_master_clone (node))
626 analyze_function (node);
628 pointer_set_destroy (visited_nodes);
629 visited_nodes = NULL;
630 if (dump_file)
632 dump_cgraph (dump_file);
633 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
636 /* Propagate the local information thru the call graph to produce
637 the global information. All the nodes within a cycle will have
638 the same info so we collapse cycles first. Then we can do the
639 propagation in one pass from the leaves to the roots. */
640 for (i = 0; i < order_pos; i++ )
642 enum pure_const_state_e pure_const_state = IPA_CONST;
643 node = order[i];
645 /* Find the worst state for any node in the cycle. */
646 w = node;
647 while (w)
649 funct_state w_l = get_function_state (w);
650 if (pure_const_state < w_l->pure_const_state)
651 pure_const_state = w_l->pure_const_state;
653 if (pure_const_state == IPA_NEITHER)
654 break;
656 if (!w_l->state_set_in_source)
658 struct cgraph_edge *e;
659 for (e = w->callees; e; e = e->next_callee)
661 struct cgraph_node *y = e->callee;
662 /* Only look at the master nodes and skip external nodes. */
663 y = cgraph_master_clone (y);
664 if (y)
666 funct_state y_l = get_function_state (y);
667 if (pure_const_state < y_l->pure_const_state)
668 pure_const_state = y_l->pure_const_state;
669 if (pure_const_state == IPA_NEITHER)
670 break;
674 w_info = w->aux;
675 w = w_info->next_cycle;
678 /* Copy back the region's pure_const_state which is shared by
679 all nodes in the region. */
680 w = node;
681 while (w)
683 funct_state w_l = get_function_state (w);
685 /* All nodes within a cycle share the same info. */
686 if (!w_l->state_set_in_source)
688 w_l->pure_const_state = pure_const_state;
689 switch (pure_const_state)
691 case IPA_CONST:
692 TREE_READONLY (w->decl) = 1;
693 if (dump_file)
694 fprintf (dump_file, "Function found to be const: %s\n",
695 lang_hooks.decl_printable_name(w->decl, 2));
696 break;
698 case IPA_PURE:
699 DECL_IS_PURE (w->decl) = 1;
700 if (dump_file)
701 fprintf (dump_file, "Function found to be pure: %s\n",
702 lang_hooks.decl_printable_name(w->decl, 2));
703 break;
705 default:
706 break;
709 w_info = w->aux;
710 w = w_info->next_cycle;
714 /* Cleanup. */
715 for (node = cgraph_nodes; node; node = node->next)
716 /* Get rid of the aux information. */
717 if (node->aux)
719 w_info = node->aux;
720 if (w_info->aux)
721 free (w_info->aux);
722 free (node->aux);
723 node->aux = NULL;
726 free (order);
727 return 0;
730 static bool
731 gate_pure_const (void)
733 return (flag_unit_at_a_time != 0 && flag_ipa_pure_const
734 /* Don't bother doing anything if the program has errors. */
735 && !(errorcount || sorrycount));
738 struct tree_opt_pass pass_ipa_pure_const =
740 "pure-const", /* name */
741 gate_pure_const, /* gate */
742 static_execute, /* execute */
743 NULL, /* sub */
744 NULL, /* next */
745 0, /* static_pass_number */
746 TV_IPA_PURE_CONST, /* tv_id */
747 0, /* properties_required */
748 0, /* properties_provided */
749 0, /* properties_destroyed */
750 0, /* todo_flags_start */
751 0, /* todo_flags_finish */
752 0 /* letter */