Merged r157428 through r157652 into branch.
[official-gcc.git] / gcc / ipa-pure-const.c
blobfa9fbb865c4dfc13b1921c9f2ac1fe3d0fac7669
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009 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
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 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. */
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 "gimple.h"
47 #include "cgraph.h"
48 #include "output.h"
49 #include "flags.h"
50 #include "timevar.h"
51 #include "diagnostic.h"
52 #include "langhooks.h"
53 #include "target.h"
54 #include "lto-streamer.h"
55 #include "cfgloop.h"
56 #include "tree-scalar-evolution.h"
58 static struct pointer_set_t *visited_nodes;
60 /* Lattice values for const and pure functions. Everything starts out
61 being const, then may drop to pure and then neither depending on
62 what is found. */
63 enum pure_const_state_e
65 IPA_CONST,
66 IPA_PURE,
67 IPA_NEITHER
70 /* Holder for the const_state. There is one of these per function
71 decl. */
72 struct funct_state_d
74 /* See above. */
75 enum pure_const_state_e pure_const_state;
76 /* What user set here; we can be always sure about this. */
77 enum pure_const_state_e state_previously_known;
78 bool looping_previously_known;
80 /* True if the function could possibly infinite loop. There are a
81 lot of ways that this could be determined. We are pretty
82 conservative here. While it is possible to cse pure and const
83 calls, it is not legal to have dce get rid of the call if there
84 is a possibility that the call could infinite loop since this is
85 a behavioral change. */
86 bool looping;
88 bool can_throw;
91 typedef struct funct_state_d * funct_state;
93 /* The storage of the funct_state is abstracted because there is the
94 possibility that it may be desirable to move this to the cgraph
95 local info. */
97 /* Array, indexed by cgraph node uid, of function states. */
99 DEF_VEC_P (funct_state);
100 DEF_VEC_ALLOC_P (funct_state, heap);
101 static VEC (funct_state, heap) *funct_state_vec;
103 /* Holders of ipa cgraph hooks: */
104 static struct cgraph_node_hook_list *function_insertion_hook_holder;
105 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
106 static struct cgraph_node_hook_list *node_removal_hook_holder;
108 /* Init the function state. */
110 static void
111 finish_state (void)
113 free (funct_state_vec);
117 /* Return the function state from NODE. */
119 static inline funct_state
120 get_function_state (struct cgraph_node *node)
122 if (!funct_state_vec
123 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
124 return NULL;
125 return VEC_index (funct_state, funct_state_vec, node->uid);
128 /* Set the function state S for NODE. */
130 static inline void
131 set_function_state (struct cgraph_node *node, funct_state s)
133 if (!funct_state_vec
134 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
135 VEC_safe_grow_cleared (funct_state, heap, funct_state_vec, node->uid + 1);
136 VEC_replace (funct_state, funct_state_vec, node->uid, s);
139 /* Check to see if the use (or definition when CHECKING_WRITE is true)
140 variable T is legal in a function that is either pure or const. */
142 static inline void
143 check_decl (funct_state local,
144 tree t, bool checking_write)
146 /* Do not want to do anything with volatile except mark any
147 function that uses one to be not const or pure. */
148 if (TREE_THIS_VOLATILE (t))
150 local->pure_const_state = IPA_NEITHER;
151 if (dump_file)
152 fprintf (dump_file, " Volatile operand is not const/pure");
153 return;
156 /* Do not care about a local automatic that is not static. */
157 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
158 return;
160 /* If the variable has the "used" attribute, treat it as if it had a
161 been touched by the devil. */
162 if (DECL_PRESERVE_P (t))
164 local->pure_const_state = IPA_NEITHER;
165 if (dump_file)
166 fprintf (dump_file, " Used static/global variable is not const/pure\n");
167 return;
170 /* Since we have dealt with the locals and params cases above, if we
171 are CHECKING_WRITE, this cannot be a pure or constant
172 function. */
173 if (checking_write)
175 local->pure_const_state = IPA_NEITHER;
176 if (dump_file)
177 fprintf (dump_file, " static/global memory write is not const/pure\n");
178 return;
181 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
183 /* Readonly reads are safe. */
184 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
185 return; /* Read of a constant, do not change the function state. */
186 else
188 if (dump_file)
189 fprintf (dump_file, " global memory read is not const\n");
190 /* Just a regular read. */
191 if (local->pure_const_state == IPA_CONST)
192 local->pure_const_state = IPA_PURE;
195 else
197 /* Compilation level statics can be read if they are readonly
198 variables. */
199 if (TREE_READONLY (t))
200 return;
202 if (dump_file)
203 fprintf (dump_file, " static memory read is not const\n");
204 /* Just a regular read. */
205 if (local->pure_const_state == IPA_CONST)
206 local->pure_const_state = IPA_PURE;
211 /* Check to see if the use (or definition when CHECKING_WRITE is true)
212 variable T is legal in a function that is either pure or const. */
214 static inline void
215 check_op (funct_state local, tree t, bool checking_write)
217 t = get_base_address (t);
218 if (t && TREE_THIS_VOLATILE (t))
220 local->pure_const_state = IPA_NEITHER;
221 if (dump_file)
222 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
223 return;
225 else if (t
226 && INDIRECT_REF_P (t)
227 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
228 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
230 if (dump_file)
231 fprintf (dump_file, " Indirect ref to local memory is OK\n");
232 return;
234 else if (checking_write)
236 local->pure_const_state = IPA_NEITHER;
237 if (dump_file)
238 fprintf (dump_file, " Indirect ref write is not const/pure\n");
239 return;
241 else
243 if (dump_file)
244 fprintf (dump_file, " Indirect ref read is not const\n");
245 if (local->pure_const_state == IPA_CONST)
246 local->pure_const_state = IPA_PURE;
250 /* Check the parameters of a function call to CALL_EXPR to see if
251 there are any references in the parameters that are not allowed for
252 pure or const functions. Also check to see if this is either an
253 indirect call, a call outside the compilation unit, or has special
254 attributes that may also effect the purity. The CALL_EXPR node for
255 the entire call expression. */
257 static void
258 check_call (funct_state local, gimple call, bool ipa)
260 int flags = gimple_call_flags (call);
261 tree callee_t = gimple_call_fndecl (call);
262 bool possibly_throws = stmt_could_throw_p (call);
263 bool possibly_throws_externally = (possibly_throws
264 && stmt_can_throw_external (call));
266 if (possibly_throws)
268 unsigned int i;
269 for (i = 0; i < gimple_num_ops (call); i++)
270 if (gimple_op (call, i)
271 && tree_could_throw_p (gimple_op (call, i)))
273 if (possibly_throws && flag_non_call_exceptions)
275 if (dump_file)
276 fprintf (dump_file, " operand can throw; looping\n");
277 local->looping = true;
279 if (possibly_throws_externally)
281 if (dump_file)
282 fprintf (dump_file, " operand can throw externally\n");
283 local->can_throw = true;
288 /* The const and pure flags are set by a variety of places in the
289 compiler (including here). If someone has already set the flags
290 for the callee, (such as for some of the builtins) we will use
291 them, otherwise we will compute our own information.
293 Const and pure functions have less clobber effects than other
294 functions so we process these first. Otherwise if it is a call
295 outside the compilation unit or an indirect call we punt. This
296 leaves local calls which will be processed by following the call
297 graph. */
298 if (callee_t)
300 /* When bad things happen to bad functions, they cannot be const
301 or pure. */
302 if (setjmp_call_p (callee_t))
304 if (dump_file)
305 fprintf (dump_file, " setjmp is not const/pure\n");
306 local->looping = true;
307 local->pure_const_state = IPA_NEITHER;
310 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
311 switch (DECL_FUNCTION_CODE (callee_t))
313 case BUILT_IN_LONGJMP:
314 case BUILT_IN_NONLOCAL_GOTO:
315 if (dump_file)
316 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
317 local->pure_const_state = IPA_NEITHER;
318 local->looping = true;
319 break;
320 default:
321 break;
325 /* When not in IPA mode, we can still handle self recursion. */
326 if (!ipa && callee_t == current_function_decl)
327 local->looping = true;
328 /* Either calle is unknown or we are doing local analysis.
329 Look to see if there are any bits available for the callee (such as by
330 declaration or because it is builtin) and process solely on the basis of
331 those bits. */
332 else if (!ipa || !callee_t)
334 if (possibly_throws && flag_non_call_exceptions)
336 if (dump_file)
337 fprintf (dump_file, " can throw; looping\n");
338 local->looping = true;
340 if (possibly_throws_externally)
342 if (dump_file)
344 fprintf (dump_file, " can throw externally to lp %i\n",
345 lookup_stmt_eh_lp (call));
346 if (callee_t)
347 fprintf (dump_file, " callee:%s\n",
348 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
350 local->can_throw = true;
352 if (flags & ECF_CONST)
354 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
355 local->looping = true;
357 else if (flags & ECF_PURE)
359 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
360 local->looping = true;
361 if (dump_file)
362 fprintf (dump_file, " pure function call in not const\n");
363 if (local->pure_const_state == IPA_CONST)
364 local->pure_const_state = IPA_PURE;
366 else
368 if (dump_file)
369 fprintf (dump_file, " uknown function call is not const/pure\n");
370 local->pure_const_state = IPA_NEITHER;
371 local->looping = true;
374 /* Direct functions calls are handled by IPA propagation. */
377 /* Wrapper around check_decl for loads. */
379 static bool
380 check_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
382 if (DECL_P (op))
383 check_decl ((funct_state)data, op, false);
384 else
385 check_op ((funct_state)data, op, false);
386 return false;
389 /* Wrapper around check_decl for stores. */
391 static bool
392 check_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
394 if (DECL_P (op))
395 check_decl ((funct_state)data, op, true);
396 else
397 check_op ((funct_state)data, op, true);
398 return false;
401 /* Look into pointer pointed to by GSIP and figure out what interesting side
402 effects it has. */
403 static void
404 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
406 gimple stmt = gsi_stmt (*gsip);
407 unsigned int i = 0;
409 if (is_gimple_debug (stmt))
410 return;
412 if (dump_file)
414 fprintf (dump_file, " scanning: ");
415 print_gimple_stmt (dump_file, stmt, 0, 0);
418 /* Look for loads and stores. */
419 walk_stmt_load_store_ops (stmt, local, check_load, check_store);
421 if (gimple_code (stmt) != GIMPLE_CALL
422 && stmt_could_throw_p (stmt))
424 if (flag_non_call_exceptions)
426 if (dump_file)
427 fprintf (dump_file, " can throw; looping");
428 local->looping = true;
430 if (stmt_can_throw_external (stmt))
432 if (dump_file)
433 fprintf (dump_file, " can throw externally");
434 local->can_throw = true;
437 switch (gimple_code (stmt))
439 case GIMPLE_CALL:
440 check_call (local, stmt, ipa);
441 break;
442 case GIMPLE_LABEL:
443 if (DECL_NONLOCAL (gimple_label_label (stmt)))
444 /* Target of long jump. */
446 if (dump_file)
447 fprintf (dump_file, " nonlocal label is not const/pure");
448 local->pure_const_state = IPA_NEITHER;
450 break;
451 case GIMPLE_ASM:
452 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
454 tree op = gimple_asm_clobber_op (stmt, i);
455 if (simple_cst_equal(TREE_VALUE (op), memory_identifier_string) == 1)
457 if (dump_file)
458 fprintf (dump_file, " memory asm clobber is not const/pure");
459 /* Abandon all hope, ye who enter here. */
460 local->pure_const_state = IPA_NEITHER;
463 if (gimple_asm_volatile_p (stmt))
465 if (dump_file)
466 fprintf (dump_file, " volatile is not const/pure");
467 /* Abandon all hope, ye who enter here. */
468 local->pure_const_state = IPA_NEITHER;
469 local->looping = true;
471 return;
472 default:
473 break;
478 /* This is the main routine for finding the reference patterns for
479 global variables within a function FN. */
481 static funct_state
482 analyze_function (struct cgraph_node *fn, bool ipa)
484 tree decl = fn->decl;
485 tree old_decl;
486 funct_state l;
487 basic_block this_block;
489 l = XCNEW (struct funct_state_d);
490 l->state_previously_known = IPA_NEITHER;
491 l->looping_previously_known = true;
492 l->looping = false;
493 l->can_throw = false;
494 if (DECL_IS_IFUNC (decl))
496 l->pure_const_state = IPA_NEITHER;
497 goto skip;
499 else
500 l->pure_const_state = IPA_CONST;
502 if (dump_file)
504 fprintf (dump_file, "\n\n local analysis of %s\n ",
505 cgraph_node_name (fn));
508 old_decl = current_function_decl;
509 push_cfun (DECL_STRUCT_FUNCTION (decl));
510 current_function_decl = decl;
512 FOR_EACH_BB (this_block)
514 gimple_stmt_iterator gsi;
515 struct walk_stmt_info wi;
517 memset (&wi, 0, sizeof(wi));
518 for (gsi = gsi_start_bb (this_block);
519 !gsi_end_p (gsi);
520 gsi_next (&gsi))
522 check_stmt (&gsi, l, ipa);
523 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
524 goto end;
528 end:
529 if (l->pure_const_state != IPA_NEITHER)
531 /* Const functions cannot have back edges (an
532 indication of possible infinite loop side
533 effect. */
534 if (mark_dfs_back_edges ())
536 /* Preheaders are needed for SCEV to work.
537 Simple lateches and recorded exits improve chances that loop will
538 proved to be finite in testcases such as in loop-15.c and loop-24.c */
539 loop_optimizer_init (LOOPS_NORMAL
540 | LOOPS_HAVE_RECORDED_EXITS);
541 if (dump_file && (dump_flags & TDF_DETAILS))
542 flow_loops_dump (dump_file, NULL, 0);
543 if (mark_irreducible_loops ())
545 if (dump_file)
546 fprintf (dump_file, " has irreducible loops\n");
547 l->looping = true;
549 else
551 loop_iterator li;
552 struct loop *loop;
553 scev_initialize ();
554 FOR_EACH_LOOP (li, loop, 0)
555 if (!finite_loop_p (loop))
557 if (dump_file)
558 fprintf (dump_file, " can not prove finiteness of loop %i\n", loop->num);
559 l->looping =true;
560 break;
562 scev_finalize ();
564 loop_optimizer_finalize ();
568 if (TREE_READONLY (decl))
570 l->pure_const_state = IPA_CONST;
571 l->state_previously_known = IPA_CONST;
572 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
573 l->looping = false, l->looping_previously_known = false;
575 if (DECL_PURE_P (decl))
577 if (l->pure_const_state != IPA_CONST)
578 l->pure_const_state = IPA_PURE;
579 l->state_previously_known = IPA_PURE;
580 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
581 l->looping = false, l->looping_previously_known = false;
583 if (TREE_NOTHROW (decl))
584 l->can_throw = false;
586 pop_cfun ();
587 current_function_decl = old_decl;
589 skip:
590 if (dump_file)
592 if (l->looping)
593 fprintf (dump_file, "Function is locally looping.\n");
594 if (l->can_throw)
595 fprintf (dump_file, "Function is locally throwing.\n");
596 if (l->pure_const_state == IPA_CONST)
597 fprintf (dump_file, "Function is locally const.\n");
598 if (l->pure_const_state == IPA_PURE)
599 fprintf (dump_file, "Function is locally pure.\n");
601 return l;
604 /* Called when new function is inserted to callgraph late. */
605 static void
606 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
608 if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
609 return;
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 ();
615 set_function_state (node, analyze_function (node, true));
616 pointer_set_destroy (visited_nodes);
617 visited_nodes = NULL;
620 /* Called when new clone is inserted to callgraph late. */
622 static void
623 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
624 void *data ATTRIBUTE_UNUSED)
626 if (get_function_state (src))
628 funct_state l = XNEW (struct funct_state_d);
629 gcc_assert (!get_function_state (dst));
630 memcpy (l, get_function_state (src), sizeof (*l));
631 set_function_state (dst, l);
635 /* Called when new clone is inserted to callgraph late. */
637 static void
638 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
640 if (get_function_state (node))
642 free (get_function_state (node));
643 set_function_state (node, NULL);
648 static void
649 register_hooks (void)
651 static bool init_p = false;
653 if (init_p)
654 return;
656 init_p = true;
658 node_removal_hook_holder =
659 cgraph_add_node_removal_hook (&remove_node_data, NULL);
660 node_duplication_hook_holder =
661 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
662 function_insertion_hook_holder =
663 cgraph_add_function_insertion_hook (&add_new_function, NULL);
667 /* Analyze each function in the cgraph to see if it is locally PURE or
668 CONST. */
670 static void
671 generate_summary (void)
673 struct cgraph_node *node;
675 register_hooks ();
677 /* There are some shared nodes, in particular the initializers on
678 static declarations. We do not need to scan them more than once
679 since all we would be interested in are the addressof
680 operations. */
681 visited_nodes = pointer_set_create ();
683 /* Process all of the functions.
685 We process AVAIL_OVERWRITABLE functions. We can not use the results
686 by default, but the info can be used at LTO with -fwhole-program or
687 when function got clonned and the clone is AVAILABLE. */
689 for (node = cgraph_nodes; node; node = node->next)
690 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
691 set_function_state (node, analyze_function (node, true));
693 pointer_set_destroy (visited_nodes);
694 visited_nodes = NULL;
698 /* Serialize the ipa info for lto. */
700 static void
701 pure_const_write_summary (cgraph_node_set set)
703 struct cgraph_node *node;
704 struct lto_simple_output_block *ob
705 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
706 unsigned int count = 0;
707 cgraph_node_set_iterator csi;
709 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
711 node = csi_node (csi);
712 if (node->analyzed && get_function_state (node) != NULL)
713 count++;
716 lto_output_uleb128_stream (ob->main_stream, count);
718 /* Process all of the functions. */
719 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
721 node = csi_node (csi);
722 if (node->analyzed && get_function_state (node) != NULL)
724 struct bitpack_d *bp;
725 funct_state fs;
726 int node_ref;
727 lto_cgraph_encoder_t encoder;
729 fs = get_function_state (node);
731 encoder = ob->decl_state->cgraph_node_encoder;
732 node_ref = lto_cgraph_encoder_encode (encoder, node);
733 lto_output_uleb128_stream (ob->main_stream, node_ref);
735 /* Note that flags will need to be read in the opposite
736 order as we are pushing the bitflags into FLAGS. */
737 bp = bitpack_create ();
738 bp_pack_value (bp, fs->pure_const_state, 2);
739 bp_pack_value (bp, fs->state_previously_known, 2);
740 bp_pack_value (bp, fs->looping_previously_known, 1);
741 bp_pack_value (bp, fs->looping, 1);
742 bp_pack_value (bp, fs->can_throw, 1);
743 lto_output_bitpack (ob->main_stream, bp);
744 bitpack_delete (bp);
748 lto_destroy_simple_output_block (ob);
752 /* Deserialize the ipa info for lto. */
754 static void
755 pure_const_read_summary (void)
757 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
758 struct lto_file_decl_data *file_data;
759 unsigned int j = 0;
761 register_hooks ();
762 while ((file_data = file_data_vec[j++]))
764 const char *data;
765 size_t len;
766 struct lto_input_block *ib
767 = lto_create_simple_input_block (file_data,
768 LTO_section_ipa_pure_const,
769 &data, &len);
770 if (ib)
772 unsigned int i;
773 unsigned int count = lto_input_uleb128 (ib);
775 for (i = 0; i < count; i++)
777 unsigned int index;
778 struct cgraph_node *node;
779 struct bitpack_d *bp;
780 funct_state fs;
781 lto_cgraph_encoder_t encoder;
783 fs = XCNEW (struct funct_state_d);
784 index = lto_input_uleb128 (ib);
785 encoder = file_data->cgraph_node_encoder;
786 node = lto_cgraph_encoder_deref (encoder, index);
787 set_function_state (node, fs);
789 /* Note that the flags must be read in the opposite
790 order in which they were written (the bitflags were
791 pushed into FLAGS). */
792 bp = lto_input_bitpack (ib);
793 fs->pure_const_state
794 = (enum pure_const_state_e) bp_unpack_value (bp, 2);
795 fs->state_previously_known
796 = (enum pure_const_state_e) bp_unpack_value (bp, 2);
797 fs->looping_previously_known = bp_unpack_value (bp, 1);
798 fs->looping = bp_unpack_value (bp, 1);
799 fs->can_throw = bp_unpack_value (bp, 1);
800 bitpack_delete (bp);
803 lto_destroy_simple_input_block (file_data,
804 LTO_section_ipa_pure_const,
805 ib, data, len);
811 static bool
812 ignore_edge (struct cgraph_edge *e)
814 return (!e->can_throw_external);
817 /* Return true if NODE is self recursive function. */
819 static bool
820 self_recursive_p (struct cgraph_node *node)
822 struct cgraph_edge *e;
823 for (e = node->callees; e; e = e->next_callee)
824 if (e->callee == node)
825 return true;
826 return false;
829 /* Produce the global information by preforming a transitive closure
830 on the local information that was produced by generate_summary.
831 Note that there is no function_transform pass since this only
832 updates the function_decl. */
834 static unsigned int
835 propagate (void)
837 struct cgraph_node *node;
838 struct cgraph_node *w;
839 struct cgraph_node **order =
840 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
841 int order_pos;
842 int i;
843 struct ipa_dfs_info * w_info;
845 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
846 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
847 cgraph_remove_node_removal_hook (node_removal_hook_holder);
848 order_pos = ipa_utils_reduced_inorder (order, true, false, NULL);
849 if (dump_file)
851 dump_cgraph (dump_file);
852 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
855 /* Propagate the local information thru the call graph to produce
856 the global information. All the nodes within a cycle will have
857 the same info so we collapse cycles first. Then we can do the
858 propagation in one pass from the leaves to the roots. */
859 for (i = 0; i < order_pos; i++ )
861 enum pure_const_state_e pure_const_state = IPA_CONST;
862 bool looping = false;
863 int count = 0;
864 node = order[i];
866 /* Find the worst state for any node in the cycle. */
867 w = node;
868 while (w)
870 struct cgraph_edge *e;
871 funct_state w_l = get_function_state (w);
872 if (pure_const_state < w_l->pure_const_state)
873 pure_const_state = w_l->pure_const_state;
875 if (w_l->looping)
876 looping = true;
877 if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
879 looping |= w_l->looping_previously_known;
880 if (pure_const_state < w_l->state_previously_known)
881 pure_const_state = w_l->state_previously_known;
884 if (pure_const_state == IPA_NEITHER)
885 break;
887 count++;
889 if (count > 1)
890 looping = true;
892 for (e = w->callees; e; e = e->next_callee)
894 struct cgraph_node *y = e->callee;
896 if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
898 funct_state y_l = get_function_state (y);
899 if (pure_const_state < y_l->pure_const_state)
900 pure_const_state = y_l->pure_const_state;
901 if (pure_const_state == IPA_NEITHER)
902 break;
903 if (y_l->looping)
904 looping = true;
906 else
908 int flags = flags_from_decl_or_type (y->decl);
910 if (flags & ECF_LOOPING_CONST_OR_PURE)
911 looping = true;
912 if (flags & ECF_CONST)
914 else if ((flags & ECF_PURE) && pure_const_state == IPA_CONST)
915 pure_const_state = IPA_PURE;
916 else
917 pure_const_state = IPA_NEITHER, looping = true;
921 w_info = (struct ipa_dfs_info *) w->aux;
922 w = w_info->next_cycle;
925 /* Copy back the region's pure_const_state which is shared by
926 all nodes in the region. */
927 w = node;
928 while (w)
930 funct_state w_l = get_function_state (w);
931 enum pure_const_state_e this_state = pure_const_state;
932 bool this_looping = looping;
934 if (w_l->state_previously_known != IPA_NEITHER
935 && this_state > w_l->state_previously_known)
936 this_state = w_l->state_previously_known;
937 if (!this_looping && self_recursive_p (w))
938 this_looping = true;
939 if (!w_l->looping_previously_known)
940 this_looping = false;
942 /* All nodes within a cycle share the same info. */
943 w_l->pure_const_state = this_state;
944 w_l->looping = this_looping;
946 switch (this_state)
948 case IPA_CONST:
949 if (!TREE_READONLY (w->decl) && dump_file)
950 fprintf (dump_file, "Function found to be %sconst: %s\n",
951 this_looping ? "looping " : "",
952 cgraph_node_name (w));
953 cgraph_set_readonly_flag (w, true);
954 cgraph_set_looping_const_or_pure_flag (w, this_looping);
955 break;
957 case IPA_PURE:
958 if (!DECL_PURE_P (w->decl) && dump_file)
959 fprintf (dump_file, "Function found to be %spure: %s\n",
960 this_looping ? "looping " : "",
961 cgraph_node_name (w));
962 cgraph_set_pure_flag (w, true);
963 cgraph_set_looping_const_or_pure_flag (w, this_looping);
964 break;
966 default:
967 break;
969 w_info = (struct ipa_dfs_info *) w->aux;
970 w = w_info->next_cycle;
974 /* Cleanup. */
975 for (node = cgraph_nodes; node; node = node->next)
977 /* Get rid of the aux information. */
978 if (node->aux)
980 w_info = (struct ipa_dfs_info *) node->aux;
981 free (node->aux);
982 node->aux = NULL;
985 order_pos = ipa_utils_reduced_inorder (order, true, false, ignore_edge);
986 if (dump_file)
988 dump_cgraph (dump_file);
989 ipa_utils_print_order(dump_file, "reduced for nothrow", order, order_pos);
991 /* Propagate the local information thru the call graph to produce
992 the global information. All the nodes within a cycle will have
993 the same info so we collapse cycles first. Then we can do the
994 propagation in one pass from the leaves to the roots. */
995 for (i = 0; i < order_pos; i++ )
997 bool can_throw = false;
998 node = order[i];
1000 /* Find the worst state for any node in the cycle. */
1001 w = node;
1002 while (w)
1004 struct cgraph_edge *e;
1005 funct_state w_l = get_function_state (w);
1007 if (w_l->can_throw
1008 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1009 can_throw = true;
1011 if (can_throw)
1012 break;
1014 for (e = w->callees; e; e = e->next_callee)
1016 struct cgraph_node *y = e->callee;
1018 if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
1020 funct_state y_l = get_function_state (y);
1022 if (can_throw)
1023 break;
1024 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1025 && e->can_throw_external)
1026 can_throw = true;
1028 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1029 can_throw = true;
1031 w_info = (struct ipa_dfs_info *) w->aux;
1032 w = w_info->next_cycle;
1035 /* Copy back the region's pure_const_state which is shared by
1036 all nodes in the region. */
1037 w = node;
1038 while (w)
1040 funct_state w_l = get_function_state (w);
1041 if (!can_throw && !TREE_NOTHROW (w->decl))
1043 struct cgraph_edge *e;
1044 cgraph_set_nothrow_flag (w, true);
1045 for (e = w->callers; e; e = e->next_caller)
1046 e->can_throw_external = false;
1047 if (dump_file)
1048 fprintf (dump_file, "Function found to be nothrow: %s\n",
1049 cgraph_node_name (w));
1051 else if (can_throw && !TREE_NOTHROW (w->decl))
1052 w_l->can_throw = true;
1053 w_info = (struct ipa_dfs_info *) w->aux;
1054 w = w_info->next_cycle;
1058 /* Cleanup. */
1059 for (node = cgraph_nodes; node; node = node->next)
1061 /* Get rid of the aux information. */
1062 if (node->aux)
1064 w_info = (struct ipa_dfs_info *) node->aux;
1065 free (node->aux);
1066 node->aux = NULL;
1068 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
1069 free (get_function_state (node));
1072 free (order);
1073 VEC_free (funct_state, heap, funct_state_vec);
1074 finish_state ();
1075 return 0;
1078 static bool
1079 gate_pure_const (void)
1081 return (flag_ipa_pure_const
1082 /* Don't bother doing anything if the program has errors. */
1083 && !(errorcount || sorrycount));
1086 struct ipa_opt_pass_d pass_ipa_pure_const =
1089 IPA_PASS,
1090 "pure-const", /* name */
1091 gate_pure_const, /* gate */
1092 propagate, /* execute */
1093 NULL, /* sub */
1094 NULL, /* next */
1095 0, /* static_pass_number */
1096 TV_IPA_PURE_CONST, /* tv_id */
1097 0, /* properties_required */
1098 0, /* properties_provided */
1099 0, /* properties_destroyed */
1100 0, /* todo_flags_start */
1101 0 /* todo_flags_finish */
1103 generate_summary, /* generate_summary */
1104 pure_const_write_summary, /* write_summary */
1105 pure_const_read_summary, /* read_summary */
1106 NULL, /* function_read_summary */
1107 NULL, /* stmt_fixup */
1108 0, /* TODOs */
1109 NULL, /* function_transform */
1110 NULL /* variable_transform */
1113 /* Simple local pass for pure const discovery reusing the analysis from
1114 ipa_pure_const. This pass is effective when executed together with
1115 other optimization passes in early optimization pass queue. */
1117 static unsigned int
1118 local_pure_const (void)
1120 bool changed = false;
1121 funct_state l;
1122 struct cgraph_node *node;
1124 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1125 we must not promote functions that are called by already processed functions. */
1127 if (function_called_by_processed_nodes_p ())
1129 if (dump_file)
1130 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1131 return 0;
1133 node = cgraph_node (current_function_decl);
1134 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1136 if (dump_file)
1137 fprintf (dump_file, "Function has wrong visibility; ignoring\n");
1138 return 0;
1141 l = analyze_function (node, false);
1143 switch (l->pure_const_state)
1145 case IPA_CONST:
1146 if (!TREE_READONLY (current_function_decl))
1148 cgraph_set_readonly_flag (node, true);
1149 cgraph_set_looping_const_or_pure_flag (node, l->looping);
1150 changed = true;
1151 if (dump_file)
1152 fprintf (dump_file, "Function found to be %sconst: %s\n",
1153 l->looping ? "looping " : "",
1154 lang_hooks.decl_printable_name (current_function_decl,
1155 2));
1157 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1158 && !l->looping)
1160 cgraph_set_looping_const_or_pure_flag (node, false);
1161 changed = true;
1162 if (dump_file)
1163 fprintf (dump_file, "Function found to be non-looping: %s\n",
1164 lang_hooks.decl_printable_name (current_function_decl,
1165 2));
1167 break;
1169 case IPA_PURE:
1170 if (!TREE_READONLY (current_function_decl))
1172 cgraph_set_pure_flag (node, true);
1173 cgraph_set_looping_const_or_pure_flag (node, l->looping);
1174 changed = true;
1175 if (dump_file)
1176 fprintf (dump_file, "Function found to be %spure: %s\n",
1177 l->looping ? "looping " : "",
1178 lang_hooks.decl_printable_name (current_function_decl,
1179 2));
1181 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1182 && !l->looping)
1184 cgraph_set_looping_const_or_pure_flag (node, false);
1185 changed = true;
1186 if (dump_file)
1187 fprintf (dump_file, "Function found to be non-looping: %s\n",
1188 lang_hooks.decl_printable_name (current_function_decl,
1189 2));
1191 break;
1193 default:
1194 break;
1196 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1198 struct cgraph_edge *e;
1200 cgraph_set_nothrow_flag (node, true);
1201 for (e = node->callers; e; e = e->next_caller)
1202 e->can_throw_external = false;
1203 changed = true;
1204 if (dump_file)
1205 fprintf (dump_file, "Function found to be nothrow: %s\n",
1206 lang_hooks.decl_printable_name (current_function_decl,
1207 2));
1209 if (l)
1210 free (l);
1211 if (changed)
1212 return execute_fixup_cfg ();
1213 else
1214 return 0;
1217 struct gimple_opt_pass pass_local_pure_const =
1220 GIMPLE_PASS,
1221 "local-pure-const", /* name */
1222 gate_pure_const, /* gate */
1223 local_pure_const, /* execute */
1224 NULL, /* sub */
1225 NULL, /* next */
1226 0, /* static_pass_number */
1227 TV_IPA_PURE_CONST, /* tv_id */
1228 0, /* properties_required */
1229 0, /* properties_provided */
1230 0, /* properties_destroyed */
1231 0, /* todo_flags_start */
1232 0 /* todo_flags_finish */