* config/m68k/m68k.md (bungt_rev): New pattern.
[official-gcc.git] / gcc / ipa-pure-const.c
blob041cf2984b116ab744b6937de1639bf84f36f05e
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 return;
169 while (TREE_CODE (t) == REALPART_EXPR
170 || TREE_CODE (t) == IMAGPART_EXPR
171 || handled_component_p (t))
173 if (TREE_CODE (t) == ARRAY_REF)
174 check_operand (local, TREE_OPERAND (t, 1), false);
175 t = TREE_OPERAND (t, 0);
178 /* The bottom of an indirect reference can only be read, not
179 written. */
180 if (INDIRECT_REF_P (t))
182 check_tree (local, TREE_OPERAND (t, 0), false);
184 /* Any indirect reference that occurs on the lhs
185 disqualifies the function from being pure or const. Any
186 indirect reference to a volatile disqualifies the
187 function from being pure or const. Any indirect
188 reference that occurs on the rhs disqualifies the
189 function from being const. */
190 if (checking_write || TREE_THIS_VOLATILE (t))
191 local->pure_const_state = IPA_NEITHER;
192 else if (local->pure_const_state == IPA_CONST)
193 local->pure_const_state = IPA_PURE;
196 if (SSA_VAR_P (t))
197 check_operand (local, t, checking_write);
200 /* Scan tree T to see if there are any addresses taken in within T. */
202 static void
203 look_for_address_of (funct_state local, tree t)
205 if (TREE_CODE (t) == ADDR_EXPR)
207 tree x = get_base_var (t);
208 if (TREE_CODE (x) == VAR_DECL)
210 check_decl (local, x, false);
212 /* Taking the address of something appears to be reasonable
213 in PURE code. Not allowed in const. */
214 if (local->pure_const_state == IPA_CONST)
215 local->pure_const_state = IPA_PURE;
220 /* Check to see if T is a read or address of operation on a var we are
221 interested in analyzing. LOCAL is passed in to get access to its
222 bit vectors. */
224 static void
225 check_rhs_var (funct_state local, tree t)
227 look_for_address_of (local, t);
229 /* Memcmp and strlen can both trap and they are declared pure. */
230 if (tree_could_trap_p (t)
231 && local->pure_const_state == IPA_CONST)
232 local->pure_const_state = IPA_PURE;
234 check_tree(local, t, false);
237 /* Check to see if T is an assignment to a var we are interested in
238 analyzing. LOCAL is passed in to get access to its bit vectors. */
240 static void
241 check_lhs_var (funct_state local, tree t)
243 /* Memcmp and strlen can both trap and they are declared pure.
244 Which seems to imply that we can apply the same rule here. */
245 if (tree_could_trap_p (t)
246 && local->pure_const_state == IPA_CONST)
247 local->pure_const_state = IPA_PURE;
249 check_tree(local, t, true);
252 /* This is a scaled down version of get_asm_expr_operands from
253 tree_ssa_operands.c. The version there runs much later and assumes
254 that aliasing information is already available. Here we are just
255 trying to find if the set of inputs and outputs contain references
256 or address of operations to local static variables. STMT is the
257 actual asm statement. */
259 static void
260 get_asm_expr_operands (funct_state local, tree stmt)
262 int noutputs = list_length (ASM_OUTPUTS (stmt));
263 const char **oconstraints
264 = (const char **) alloca ((noutputs) * sizeof (const char *));
265 int i;
266 tree link;
267 const char *constraint;
268 bool allows_mem, allows_reg, is_inout;
270 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
272 oconstraints[i] = constraint
273 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
274 parse_output_constraint (&constraint, i, 0, 0,
275 &allows_mem, &allows_reg, &is_inout);
277 check_lhs_var (local, TREE_VALUE (link));
280 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
282 constraint
283 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
284 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
285 oconstraints, &allows_mem, &allows_reg);
287 check_rhs_var (local, TREE_VALUE (link));
290 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
291 if (simple_cst_equal(TREE_VALUE (link), memory_identifier_string) == 1)
292 /* Abandon all hope, ye who enter here. */
293 local->pure_const_state = IPA_NEITHER;
295 if (ASM_VOLATILE_P (stmt))
296 local->pure_const_state = IPA_NEITHER;
299 /* Check the parameters of a function call to CALL_EXPR to see if
300 there are any references in the parameters that are not allowed for
301 pure or const functions. Also check to see if this is either an
302 indirect call, a call outside the compilation unit, or has special
303 attributes that may also effect the purity. The CALL_EXPR node for
304 the entire call expression. */
306 static void
307 check_call (funct_state local, tree call_expr)
309 int flags = call_expr_flags(call_expr);
310 tree operand_list = TREE_OPERAND (call_expr, 1);
311 tree operand;
312 tree callee_t = get_callee_fndecl (call_expr);
313 struct cgraph_node* callee;
314 enum availability avail = AVAIL_NOT_AVAILABLE;
316 for (operand = operand_list;
317 operand != NULL_TREE;
318 operand = TREE_CHAIN (operand))
320 tree argument = TREE_VALUE (operand);
321 check_rhs_var (local, argument);
324 /* The const and pure flags are set by a variety of places in the
325 compiler (including here). If someone has already set the flags
326 for the callee, (such as for some of the builtins) we will use
327 them, otherwise we will compute our own information.
329 Const and pure functions have less clobber effects than other
330 functions so we process these first. Otherwise if it is a call
331 outside the compilation unit or an indirect call we punt. This
332 leaves local calls which will be processed by following the call
333 graph. */
334 if (callee_t)
336 callee = cgraph_node(callee_t);
337 avail = cgraph_function_body_availability (callee);
339 /* When bad things happen to bad functions, they cannot be const
340 or pure. */
341 if (setjmp_call_p (callee_t))
342 local->pure_const_state = IPA_NEITHER;
344 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
345 switch (DECL_FUNCTION_CODE (callee_t))
347 case BUILT_IN_LONGJMP:
348 case BUILT_IN_NONLOCAL_GOTO:
349 local->pure_const_state = IPA_NEITHER;
350 break;
351 default:
352 break;
356 /* The callee is either unknown (indirect call) or there is just no
357 scannable code for it (external call) . We look to see if there
358 are any bits available for the callee (such as by declaration or
359 because it is builtin) and process solely on the basis of those
360 bits. */
361 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
363 if (flags & ECF_PURE)
365 if (local->pure_const_state == IPA_CONST)
366 local->pure_const_state = IPA_PURE;
368 else
369 local->pure_const_state = IPA_NEITHER;
371 else
373 /* We have the code and we will scan it for the effects. */
374 if (flags & ECF_PURE)
376 if (local->pure_const_state == IPA_CONST)
377 local->pure_const_state = IPA_PURE;
382 /* TP is the part of the tree currently under the microscope.
383 WALK_SUBTREES is part of the walk_tree api but is unused here.
384 DATA is cgraph_node of the function being walked. */
386 /* FIXME: When this is converted to run over SSA form, this code
387 should be converted to use the operand scanner. */
389 static tree
390 scan_function (tree *tp,
391 int *walk_subtrees,
392 void *data)
394 struct cgraph_node *fn = data;
395 tree t = *tp;
396 funct_state local = get_function_state (fn);
398 switch (TREE_CODE (t))
400 case VAR_DECL:
401 if (DECL_INITIAL (t))
402 walk_tree (&DECL_INITIAL (t), scan_function, fn, visited_nodes);
403 *walk_subtrees = 0;
404 break;
406 case MODIFY_EXPR:
408 /* First look on the lhs and see what variable is stored to */
409 tree lhs = TREE_OPERAND (t, 0);
410 tree rhs = TREE_OPERAND (t, 1);
411 check_lhs_var (local, lhs);
413 /* For the purposes of figuring out what the cast affects */
415 /* Next check the operands on the rhs to see if they are ok. */
416 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
418 case tcc_binary:
420 tree op0 = TREE_OPERAND (rhs, 0);
421 tree op1 = TREE_OPERAND (rhs, 1);
422 check_rhs_var (local, op0);
423 check_rhs_var (local, op1);
425 break;
426 case tcc_unary:
428 tree op0 = TREE_OPERAND (rhs, 0);
429 check_rhs_var (local, op0);
432 break;
433 case tcc_reference:
434 check_rhs_var (local, rhs);
435 break;
436 case tcc_declaration:
437 check_rhs_var (local, rhs);
438 break;
439 case tcc_expression:
440 switch (TREE_CODE (rhs))
442 case ADDR_EXPR:
443 check_rhs_var (local, rhs);
444 break;
445 case CALL_EXPR:
446 check_call (local, rhs);
447 break;
448 default:
449 break;
451 break;
452 default:
453 break;
455 *walk_subtrees = 0;
457 break;
459 case ADDR_EXPR:
460 /* This case is here to find addresses on rhs of constructors in
461 decl_initial of static variables. */
462 check_rhs_var (local, t);
463 *walk_subtrees = 0;
464 break;
466 case LABEL_EXPR:
467 if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
468 /* Target of long jump. */
469 local->pure_const_state = IPA_NEITHER;
470 break;
472 case CALL_EXPR:
473 check_call (local, t);
474 *walk_subtrees = 0;
475 break;
477 case ASM_EXPR:
478 get_asm_expr_operands (local, t);
479 *walk_subtrees = 0;
480 break;
482 default:
483 break;
485 return NULL;
488 /* This is the main routine for finding the reference patterns for
489 global variables within a function FN. */
491 static void
492 analyze_function (struct cgraph_node *fn)
494 funct_state l = XCNEW (struct funct_state_d);
495 tree decl = fn->decl;
496 struct ipa_dfs_info * w_info = fn->aux;
498 w_info->aux = l;
500 l->pure_const_state = IPA_CONST;
501 l->state_set_in_source = false;
503 /* If this function does not return normally or does not bind local,
504 do not touch this unless it has been marked as const or pure by the
505 front end. */
506 if (TREE_THIS_VOLATILE (decl)
507 || !targetm.binds_local_p (decl))
509 l->pure_const_state = IPA_NEITHER;
510 return;
513 if (TREE_READONLY (decl))
515 l->pure_const_state = IPA_CONST;
516 l->state_set_in_source = true;
518 if (DECL_IS_PURE (decl))
520 l->pure_const_state = IPA_PURE;
521 l->state_set_in_source = true;
524 if (dump_file)
526 fprintf (dump_file, "\n local analysis of %s with initial value = %d\n ",
527 cgraph_node_name (fn),
528 l->pure_const_state);
531 if (!l->state_set_in_source)
533 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
534 basic_block this_block;
536 FOR_EACH_BB_FN (this_block, this_cfun)
538 block_stmt_iterator bsi;
539 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
541 walk_tree (bsi_stmt_ptr (bsi), scan_function,
542 fn, visited_nodes);
543 if (l->pure_const_state == IPA_NEITHER)
544 return;
548 if (l->pure_const_state != IPA_NEITHER)
550 tree old_decl = current_function_decl;
551 /* Const functions cannot have back edges (an
552 indication of possible infinite loop side
553 effect. */
555 current_function_decl = fn->decl;
557 /* The C++ front end, has a tendency to some times jerk away
558 a function after it has created it. This should have
559 been fixed. */
560 gcc_assert (DECL_STRUCT_FUNCTION (fn->decl));
562 push_cfun (DECL_STRUCT_FUNCTION (fn->decl));
564 if (mark_dfs_back_edges ())
565 l->pure_const_state = IPA_NEITHER;
567 current_function_decl = old_decl;
568 pop_cfun ();
574 /* Produce the global information by preforming a transitive closure
575 on the local information that was produced by ipa_analyze_function
576 and ipa_analyze_variable. */
578 static unsigned int
579 static_execute (void)
581 struct cgraph_node *node;
582 struct cgraph_node *w;
583 struct cgraph_node **order =
584 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
585 int order_pos = order_pos = ipa_utils_reduced_inorder (order, true, false);
586 int i;
587 struct ipa_dfs_info * w_info;
589 if (!memory_identifier_string)
590 memory_identifier_string = build_string(7, "memory");
592 /* There are some shared nodes, in particular the initializers on
593 static declarations. We do not need to scan them more than once
594 since all we would be interested in are the addressof
595 operations. */
596 visited_nodes = pointer_set_create ();
598 /* Process all of the functions.
600 We do not want to process any of the clones so we check that this
601 is a master clone. However, we do NOT process any
602 AVAIL_OVERWRITABLE functions (these are never clones) we cannot
603 guarantee that what we learn about the one we see will be true
604 for the one that overriders it.
606 for (node = cgraph_nodes; node; node = node->next)
607 if (node->analyzed && cgraph_is_master_clone (node))
608 analyze_function (node);
610 pointer_set_destroy (visited_nodes);
611 visited_nodes = NULL;
612 if (dump_file)
614 dump_cgraph (dump_file);
615 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
618 /* Propagate the local information thru the call graph to produce
619 the global information. All the nodes within a cycle will have
620 the same info so we collapse cycles first. Then we can do the
621 propagation in one pass from the leaves to the roots. */
622 for (i = 0; i < order_pos; i++ )
624 enum pure_const_state_e pure_const_state = IPA_CONST;
625 node = order[i];
627 /* Find the worst state for any node in the cycle. */
628 w = node;
629 while (w)
631 funct_state w_l = get_function_state (w);
632 if (pure_const_state < w_l->pure_const_state)
633 pure_const_state = w_l->pure_const_state;
635 if (pure_const_state == IPA_NEITHER)
636 break;
638 if (!w_l->state_set_in_source)
640 struct cgraph_edge *e;
641 for (e = w->callees; e; e = e->next_callee)
643 struct cgraph_node *y = e->callee;
644 /* Only look at the master nodes and skip external nodes. */
645 y = cgraph_master_clone (y);
646 if (y)
648 funct_state y_l = get_function_state (y);
649 if (pure_const_state < y_l->pure_const_state)
650 pure_const_state = y_l->pure_const_state;
651 if (pure_const_state == IPA_NEITHER)
652 break;
656 w_info = w->aux;
657 w = w_info->next_cycle;
660 /* Copy back the region's pure_const_state which is shared by
661 all nodes in the region. */
662 w = node;
663 while (w)
665 funct_state w_l = get_function_state (w);
667 /* All nodes within a cycle share the same info. */
668 if (!w_l->state_set_in_source)
670 w_l->pure_const_state = pure_const_state;
671 switch (pure_const_state)
673 case IPA_CONST:
674 TREE_READONLY (w->decl) = 1;
675 if (dump_file)
676 fprintf (dump_file, "Function found to be const: %s\n",
677 lang_hooks.decl_printable_name(w->decl, 2));
678 break;
680 case IPA_PURE:
681 DECL_IS_PURE (w->decl) = 1;
682 if (dump_file)
683 fprintf (dump_file, "Function found to be pure: %s\n",
684 lang_hooks.decl_printable_name(w->decl, 2));
685 break;
687 default:
688 break;
691 w_info = w->aux;
692 w = w_info->next_cycle;
696 /* Cleanup. */
697 for (node = cgraph_nodes; node; node = node->next)
698 /* Get rid of the aux information. */
699 if (node->aux)
701 w_info = node->aux;
702 if (w_info->aux)
703 free (w_info->aux);
704 free (node->aux);
705 node->aux = NULL;
708 free (order);
709 return 0;
712 static bool
713 gate_pure_const (void)
715 return (flag_unit_at_a_time != 0 && flag_ipa_pure_const
716 /* Don't bother doing anything if the program has errors. */
717 && !(errorcount || sorrycount));
720 struct tree_opt_pass pass_ipa_pure_const =
722 "pure-const", /* name */
723 gate_pure_const, /* gate */
724 static_execute, /* execute */
725 NULL, /* sub */
726 NULL, /* next */
727 0, /* static_pass_number */
728 TV_IPA_PURE_CONST, /* tv_id */
729 0, /* properties_required */
730 0, /* properties_provided */
731 0, /* properties_destroyed */
732 0, /* todo_flags_start */
733 0, /* todo_flags_finish */
734 0 /* letter */