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
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"
51 #include "diagnostic.h"
52 #include "langhooks.h"
54 #include "lto-streamer.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
63 enum pure_const_state_e
70 /* Holder for the const_state. There is one of these per function
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. */
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
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. */
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
)
123 || VEC_length (funct_state
, funct_state_vec
) <= (unsigned int)node
->uid
)
125 return VEC_index (funct_state
, funct_state_vec
, node
->uid
);
128 /* Set the function state S for NODE. */
131 set_function_state (struct cgraph_node
*node
, funct_state s
)
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. */
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
;
152 fprintf (dump_file
, " Volatile operand is not const/pure");
156 /* Do not care about a local automatic that is not static. */
157 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
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
;
166 fprintf (dump_file
, " Used static/global variable is not const/pure\n");
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
175 local
->pure_const_state
= IPA_NEITHER
;
177 fprintf (dump_file
, " static/global memory write is not const/pure\n");
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. */
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
;
197 /* Compilation level statics can be read if they are readonly
199 if (TREE_READONLY (t
))
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. */
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
;
222 fprintf (dump_file
, " Volatile indirect ref is not const/pure\n");
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)))
231 fprintf (dump_file
, " Indirect ref to local memory is OK\n");
234 else if (checking_write
)
236 local
->pure_const_state
= IPA_NEITHER
;
238 fprintf (dump_file
, " Indirect ref write is not const/pure\n");
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. */
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
));
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
)
276 fprintf (dump_file
, " operand can throw; looping\n");
277 local
->looping
= true;
279 if (possibly_throws_externally
)
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
300 /* When bad things happen to bad functions, they cannot be const
302 if (setjmp_call_p (callee_t
))
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
:
316 fprintf (dump_file
, " longjmp and nonlocal goto is not const/pure\n");
317 local
->pure_const_state
= IPA_NEITHER
;
318 local
->looping
= true;
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
332 else if (!ipa
|| !callee_t
)
334 if (possibly_throws
&& flag_non_call_exceptions
)
337 fprintf (dump_file
, " can throw; looping\n");
338 local
->looping
= true;
340 if (possibly_throws_externally
)
344 fprintf (dump_file
, " can throw externally to lp %i\n",
345 lookup_stmt_eh_lp (call
));
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;
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
;
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. */
380 check_load (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
383 check_decl ((funct_state
)data
, op
, false);
385 check_op ((funct_state
)data
, op
, false);
389 /* Wrapper around check_decl for stores. */
392 check_store (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
395 check_decl ((funct_state
)data
, op
, true);
397 check_op ((funct_state
)data
, op
, true);
401 /* Look into pointer pointed to by GSIP and figure out what interesting side
404 check_stmt (gimple_stmt_iterator
*gsip
, funct_state local
, bool ipa
)
406 gimple stmt
= gsi_stmt (*gsip
);
409 if (is_gimple_debug (stmt
))
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
)
427 fprintf (dump_file
, " can throw; looping");
428 local
->looping
= true;
430 if (stmt_can_throw_external (stmt
))
433 fprintf (dump_file
, " can throw externally");
434 local
->can_throw
= true;
437 switch (gimple_code (stmt
))
440 check_call (local
, stmt
, ipa
);
443 if (DECL_NONLOCAL (gimple_label_label (stmt
)))
444 /* Target of long jump. */
447 fprintf (dump_file
, " nonlocal label is not const/pure");
448 local
->pure_const_state
= IPA_NEITHER
;
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)
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
))
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;
478 /* This is the main routine for finding the reference patterns for
479 global variables within a function FN. */
482 analyze_function (struct cgraph_node
*fn
, bool ipa
)
484 tree decl
= fn
->decl
;
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;
493 l
->can_throw
= false;
494 if (DECL_IS_IFUNC (decl
))
496 l
->pure_const_state
= IPA_NEITHER
;
500 l
->pure_const_state
= IPA_CONST
;
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
);
522 check_stmt (&gsi
, l
, ipa
);
523 if (l
->pure_const_state
== IPA_NEITHER
&& l
->looping
&& l
->can_throw
)
529 if (l
->pure_const_state
!= IPA_NEITHER
)
531 /* Const functions cannot have back edges (an
532 indication of possible infinite loop side
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 ())
546 fprintf (dump_file
, " has irreducible loops\n");
554 FOR_EACH_LOOP (li
, loop
, 0)
555 if (!finite_loop_p (loop
))
558 fprintf (dump_file
, " can not prove finiteness of loop %i\n", loop
->num
);
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;
587 current_function_decl
= old_decl
;
593 fprintf (dump_file
, "Function is locally looping.\n");
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");
604 /* Called when new function is inserted to callgraph late. */
606 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
608 if (cgraph_function_body_availability (node
) < AVAIL_OVERWRITABLE
)
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
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. */
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. */
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
);
649 register_hooks (void)
651 static bool init_p
= false;
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
671 generate_summary (void)
673 struct cgraph_node
*node
;
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
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. */
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
)
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
;
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
);
748 lto_destroy_simple_output_block (ob
);
752 /* Deserialize the ipa info for lto. */
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
;
762 while ((file_data
= file_data_vec
[j
++]))
766 struct lto_input_block
*ib
767 = lto_create_simple_input_block (file_data
,
768 LTO_section_ipa_pure_const
,
773 unsigned int count
= lto_input_uleb128 (ib
);
775 for (i
= 0; i
< count
; i
++)
778 struct cgraph_node
*node
;
779 struct bitpack_d
*bp
;
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
);
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);
803 lto_destroy_simple_input_block (file_data
,
804 LTO_section_ipa_pure_const
,
812 ignore_edge (struct cgraph_edge
*e
)
814 return (!e
->can_throw_external
);
817 /* Return true if NODE is self recursive function. */
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
)
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. */
837 struct cgraph_node
*node
;
838 struct cgraph_node
*w
;
839 struct cgraph_node
**order
=
840 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
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
);
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;
866 /* Find the worst state for any node in the cycle. */
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
;
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
)
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
)
908 int flags
= flags_from_decl_or_type (y
->decl
);
910 if (flags
& ECF_LOOPING_CONST_OR_PURE
)
912 if (flags
& ECF_CONST
)
914 else if ((flags
& ECF_PURE
) && pure_const_state
== IPA_CONST
)
915 pure_const_state
= IPA_PURE
;
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. */
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
))
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
;
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
);
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
);
969 w_info
= (struct ipa_dfs_info
*) w
->aux
;
970 w
= w_info
->next_cycle
;
975 for (node
= cgraph_nodes
; node
; node
= node
->next
)
977 /* Get rid of the aux information. */
980 w_info
= (struct ipa_dfs_info
*) node
->aux
;
985 order_pos
= ipa_utils_reduced_inorder (order
, true, false, ignore_edge
);
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;
1000 /* Find the worst state for any node in the cycle. */
1004 struct cgraph_edge
*e
;
1005 funct_state w_l
= get_function_state (w
);
1008 || cgraph_function_body_availability (w
) == AVAIL_OVERWRITABLE
)
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
);
1024 if (y_l
->can_throw
&& !TREE_NOTHROW (w
->decl
)
1025 && e
->can_throw_external
)
1028 else if (e
->can_throw_external
&& !TREE_NOTHROW (y
->decl
))
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. */
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;
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
;
1059 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1061 /* Get rid of the aux information. */
1064 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1068 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
1069 free (get_function_state (node
));
1073 VEC_free (funct_state
, heap
, funct_state_vec
);
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
=
1090 "pure-const", /* name */
1091 gate_pure_const
, /* gate */
1092 propagate
, /* execute */
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 */
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. */
1118 local_pure_const (void)
1120 bool changed
= false;
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 ())
1130 fprintf (dump_file
, "Function called in recursive cycle; ignoring\n");
1133 node
= cgraph_node (current_function_decl
);
1134 if (cgraph_function_body_availability (node
) <= AVAIL_OVERWRITABLE
)
1137 fprintf (dump_file
, "Function has wrong visibility; ignoring\n");
1141 l
= analyze_function (node
, false);
1143 switch (l
->pure_const_state
)
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
);
1152 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1153 l
->looping
? "looping " : "",
1154 lang_hooks
.decl_printable_name (current_function_decl
,
1157 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1160 cgraph_set_looping_const_or_pure_flag (node
, false);
1163 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1164 lang_hooks
.decl_printable_name (current_function_decl
,
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
);
1176 fprintf (dump_file
, "Function found to be %spure: %s\n",
1177 l
->looping
? "looping " : "",
1178 lang_hooks
.decl_printable_name (current_function_decl
,
1181 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1184 cgraph_set_looping_const_or_pure_flag (node
, false);
1187 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1188 lang_hooks
.decl_printable_name (current_function_decl
,
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;
1205 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1206 lang_hooks
.decl_printable_name (current_function_decl
,
1212 return execute_fixup_cfg ();
1217 struct gimple_opt_pass pass_local_pure_const
=
1221 "local-pure-const", /* name */
1222 gate_pure_const
, /* gate */
1223 local_pure_const
, /* execute */
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 */