1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file marks functions as being either const (TREE_READONLY) or
23 pure (DECL_PURE_P). It can also set a variant of these that
24 are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
26 This must be run after inlining decisions have been made since
27 otherwise, the local sets will not contain information that is
28 consistent with post inlined state. The global sets are not prone
29 to this problem since they are by definition transitive. */
31 /* The code in this module is called by the ipa pass manager. It
32 should be one of the later passes since it's information is used by
33 the rest of the compilation. */
37 #include "coretypes.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "langhooks.h"
44 #include "pointer-set.h"
46 #include "ipa-utils.h"
52 #include "diagnostic.h"
53 #include "langhooks.h"
55 #include "lto-streamer.h"
57 #include "tree-scalar-evolution.h"
59 static struct pointer_set_t
*visited_nodes
;
61 /* Lattice values for const and pure functions. Everything starts out
62 being const, then may drop to pure and then neither depending on
64 enum pure_const_state_e
71 /* Holder for the const_state. There is one of these per function
76 enum pure_const_state_e pure_const_state
;
77 /* What user set here; we can be always sure about this. */
78 enum pure_const_state_e state_previously_known
;
79 bool looping_previously_known
;
81 /* True if the function could possibly infinite loop. There are a
82 lot of ways that this could be determined. We are pretty
83 conservative here. While it is possible to cse pure and const
84 calls, it is not legal to have dce get rid of the call if there
85 is a possibility that the call could infinite loop since this is
86 a behavioral change. */
92 typedef struct funct_state_d
* funct_state
;
94 /* The storage of the funct_state is abstracted because there is the
95 possibility that it may be desirable to move this to the cgraph
98 /* Array, indexed by cgraph node uid, of function states. */
100 DEF_VEC_P (funct_state
);
101 DEF_VEC_ALLOC_P (funct_state
, heap
);
102 static VEC (funct_state
, heap
) *funct_state_vec
;
104 /* Holders of ipa cgraph hooks: */
105 static struct cgraph_node_hook_list
*function_insertion_hook_holder
;
106 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
107 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
109 /* Init the function state. */
114 free (funct_state_vec
);
118 /* Return the function state from NODE. */
120 static inline funct_state
121 get_function_state (struct cgraph_node
*node
)
124 || VEC_length (funct_state
, funct_state_vec
) <= (unsigned int)node
->uid
)
126 return VEC_index (funct_state
, funct_state_vec
, node
->uid
);
129 /* Set the function state S for NODE. */
132 set_function_state (struct cgraph_node
*node
, funct_state s
)
135 || VEC_length (funct_state
, funct_state_vec
) <= (unsigned int)node
->uid
)
136 VEC_safe_grow_cleared (funct_state
, heap
, funct_state_vec
, node
->uid
+ 1);
137 VEC_replace (funct_state
, funct_state_vec
, node
->uid
, s
);
140 /* Check to see if the use (or definition when CHECKING_WRITE is true)
141 variable T is legal in a function that is either pure or const. */
144 check_decl (funct_state local
,
145 tree t
, bool checking_write
)
147 /* Do not want to do anything with volatile except mark any
148 function that uses one to be not const or pure. */
149 if (TREE_THIS_VOLATILE (t
))
151 local
->pure_const_state
= IPA_NEITHER
;
153 fprintf (dump_file
, " Volatile operand is not const/pure");
157 /* Do not care about a local automatic that is not static. */
158 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
161 /* If the variable has the "used" attribute, treat it as if it had a
162 been touched by the devil. */
163 if (DECL_PRESERVE_P (t
))
165 local
->pure_const_state
= IPA_NEITHER
;
167 fprintf (dump_file
, " Used static/global variable is not const/pure\n");
171 /* Since we have dealt with the locals and params cases above, if we
172 are CHECKING_WRITE, this cannot be a pure or constant
176 local
->pure_const_state
= IPA_NEITHER
;
178 fprintf (dump_file
, " static/global memory write is not const/pure\n");
182 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
184 /* Readonly reads are safe. */
185 if (TREE_READONLY (t
) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t
)))
186 return; /* Read of a constant, do not change the function state. */
190 fprintf (dump_file
, " global memory read is not const\n");
191 /* Just a regular read. */
192 if (local
->pure_const_state
== IPA_CONST
)
193 local
->pure_const_state
= IPA_PURE
;
198 /* Compilation level statics can be read if they are readonly
200 if (TREE_READONLY (t
))
204 fprintf (dump_file
, " static memory read is not const\n");
205 /* Just a regular read. */
206 if (local
->pure_const_state
== IPA_CONST
)
207 local
->pure_const_state
= IPA_PURE
;
212 /* Check to see if the use (or definition when CHECKING_WRITE is true)
213 variable T is legal in a function that is either pure or const. */
216 check_op (funct_state local
, tree t
, bool checking_write
)
218 t
= get_base_address (t
);
219 if (t
&& TREE_THIS_VOLATILE (t
))
221 local
->pure_const_state
= IPA_NEITHER
;
223 fprintf (dump_file
, " Volatile indirect ref is not const/pure\n");
227 && INDIRECT_REF_P (t
)
228 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
229 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t
, 0)))
232 fprintf (dump_file
, " Indirect ref to local memory is OK\n");
235 else if (checking_write
)
237 local
->pure_const_state
= IPA_NEITHER
;
239 fprintf (dump_file
, " Indirect ref write is not const/pure\n");
245 fprintf (dump_file
, " Indirect ref read is not const\n");
246 if (local
->pure_const_state
== IPA_CONST
)
247 local
->pure_const_state
= IPA_PURE
;
251 /* Check the parameters of a function call to CALL_EXPR to see if
252 there are any references in the parameters that are not allowed for
253 pure or const functions. Also check to see if this is either an
254 indirect call, a call outside the compilation unit, or has special
255 attributes that may also effect the purity. The CALL_EXPR node for
256 the entire call expression. */
259 check_call (funct_state local
, gimple call
, bool ipa
)
261 int flags
= gimple_call_flags (call
);
262 tree callee_t
= gimple_call_fndecl (call
);
263 bool possibly_throws
= stmt_could_throw_p (call
);
264 bool possibly_throws_externally
= (possibly_throws
265 && stmt_can_throw_external (call
));
270 for (i
= 0; i
< gimple_num_ops (call
); i
++)
271 if (gimple_op (call
, i
)
272 && tree_could_throw_p (gimple_op (call
, i
)))
274 if (possibly_throws
&& flag_non_call_exceptions
)
277 fprintf (dump_file
, " operand can throw; looping\n");
278 local
->looping
= true;
280 if (possibly_throws_externally
)
283 fprintf (dump_file
, " operand can throw externally\n");
284 local
->can_throw
= true;
289 /* The const and pure flags are set by a variety of places in the
290 compiler (including here). If someone has already set the flags
291 for the callee, (such as for some of the builtins) we will use
292 them, otherwise we will compute our own information.
294 Const and pure functions have less clobber effects than other
295 functions so we process these first. Otherwise if it is a call
296 outside the compilation unit or an indirect call we punt. This
297 leaves local calls which will be processed by following the call
301 /* When bad things happen to bad functions, they cannot be const
303 if (setjmp_call_p (callee_t
))
306 fprintf (dump_file
, " setjmp is not const/pure\n");
307 local
->looping
= true;
308 local
->pure_const_state
= IPA_NEITHER
;
311 if (DECL_BUILT_IN_CLASS (callee_t
) == BUILT_IN_NORMAL
)
312 switch (DECL_FUNCTION_CODE (callee_t
))
314 case BUILT_IN_LONGJMP
:
315 case BUILT_IN_NONLOCAL_GOTO
:
317 fprintf (dump_file
, " longjmp and nonlocal goto is not const/pure\n");
318 local
->pure_const_state
= IPA_NEITHER
;
319 local
->looping
= true;
326 /* When not in IPA mode, we can still handle self recursion. */
327 if (!ipa
&& callee_t
== current_function_decl
)
328 local
->looping
= true;
329 /* Either calle is unknown or we are doing local analysis.
330 Look to see if there are any bits available for the callee (such as by
331 declaration or because it is builtin) and process solely on the basis of
333 else if (!ipa
|| !callee_t
)
335 if (possibly_throws
&& flag_non_call_exceptions
)
338 fprintf (dump_file
, " can throw; looping\n");
339 local
->looping
= true;
341 if (possibly_throws_externally
)
345 fprintf (dump_file
, " can throw externally to lp %i\n",
346 lookup_stmt_eh_lp (call
));
348 fprintf (dump_file
, " callee:%s\n",
349 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t
)));
351 local
->can_throw
= true;
353 if (flags
& ECF_CONST
)
355 if (callee_t
&& DECL_LOOPING_CONST_OR_PURE_P (callee_t
))
356 local
->looping
= true;
358 else if (flags
& ECF_PURE
)
360 if (callee_t
&& DECL_LOOPING_CONST_OR_PURE_P (callee_t
))
361 local
->looping
= true;
363 fprintf (dump_file
, " pure function call in not const\n");
364 if (local
->pure_const_state
== IPA_CONST
)
365 local
->pure_const_state
= IPA_PURE
;
370 fprintf (dump_file
, " uknown function call is not const/pure\n");
371 local
->pure_const_state
= IPA_NEITHER
;
372 local
->looping
= true;
375 /* Direct functions calls are handled by IPA propagation. */
378 /* Wrapper around check_decl for loads. */
381 check_load (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
384 check_decl ((funct_state
)data
, op
, false);
386 check_op ((funct_state
)data
, op
, false);
390 /* Wrapper around check_decl for stores. */
393 check_store (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
396 check_decl ((funct_state
)data
, op
, true);
398 check_op ((funct_state
)data
, op
, true);
402 /* Look into pointer pointed to by GSIP and figure out what interesting side
405 check_stmt (gimple_stmt_iterator
*gsip
, funct_state local
, bool ipa
)
407 gimple stmt
= gsi_stmt (*gsip
);
410 if (is_gimple_debug (stmt
))
415 fprintf (dump_file
, " scanning: ");
416 print_gimple_stmt (dump_file
, stmt
, 0, 0);
419 /* Look for loads and stores. */
420 walk_stmt_load_store_ops (stmt
, local
, check_load
, check_store
);
422 if (gimple_code (stmt
) != GIMPLE_CALL
423 && stmt_could_throw_p (stmt
))
425 if (flag_non_call_exceptions
)
428 fprintf (dump_file
, " can throw; looping");
429 local
->looping
= true;
431 if (stmt_can_throw_external (stmt
))
434 fprintf (dump_file
, " can throw externally");
435 local
->can_throw
= true;
438 switch (gimple_code (stmt
))
441 check_call (local
, stmt
, ipa
);
444 if (DECL_NONLOCAL (gimple_label_label (stmt
)))
445 /* Target of long jump. */
448 fprintf (dump_file
, " nonlocal label is not const/pure");
449 local
->pure_const_state
= IPA_NEITHER
;
453 for (i
= 0; i
< gimple_asm_nclobbers (stmt
); i
++)
455 tree op
= gimple_asm_clobber_op (stmt
, i
);
456 if (simple_cst_equal(TREE_VALUE (op
), memory_identifier_string
) == 1)
459 fprintf (dump_file
, " memory asm clobber is not const/pure");
460 /* Abandon all hope, ye who enter here. */
461 local
->pure_const_state
= IPA_NEITHER
;
464 if (gimple_asm_volatile_p (stmt
))
467 fprintf (dump_file
, " volatile is not const/pure");
468 /* Abandon all hope, ye who enter here. */
469 local
->pure_const_state
= IPA_NEITHER
;
470 local
->looping
= true;
479 /* This is the main routine for finding the reference patterns for
480 global variables within a function FN. */
483 analyze_function (struct cgraph_node
*fn
, bool ipa
)
485 tree decl
= fn
->decl
;
486 tree old_decl
= current_function_decl
;
488 basic_block this_block
;
490 l
= XCNEW (struct funct_state_d
);
491 l
->pure_const_state
= IPA_CONST
;
492 l
->state_previously_known
= IPA_NEITHER
;
493 l
->looping_previously_known
= true;
495 l
->can_throw
= false;
499 fprintf (dump_file
, "\n\n local analysis of %s\n ",
500 cgraph_node_name (fn
));
503 push_cfun (DECL_STRUCT_FUNCTION (decl
));
504 current_function_decl
= decl
;
506 FOR_EACH_BB (this_block
)
508 gimple_stmt_iterator gsi
;
509 struct walk_stmt_info wi
;
511 memset (&wi
, 0, sizeof(wi
));
512 for (gsi
= gsi_start_bb (this_block
);
516 check_stmt (&gsi
, l
, ipa
);
517 if (l
->pure_const_state
== IPA_NEITHER
&& l
->looping
&& l
->can_throw
)
523 if (l
->pure_const_state
!= IPA_NEITHER
)
525 /* Const functions cannot have back edges (an
526 indication of possible infinite loop side
528 if (mark_dfs_back_edges ())
530 /* Preheaders are needed for SCEV to work.
531 Simple lateches and recorded exits improve chances that loop will
532 proved to be finite in testcases such as in loop-15.c and loop-24.c */
533 loop_optimizer_init (LOOPS_NORMAL
534 | LOOPS_HAVE_RECORDED_EXITS
);
535 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
536 flow_loops_dump (dump_file
, NULL
, 0);
537 if (mark_irreducible_loops ())
540 fprintf (dump_file
, " has irreducible loops\n");
548 FOR_EACH_LOOP (li
, loop
, 0)
549 if (!finite_loop_p (loop
))
552 fprintf (dump_file
, " can not prove finiteness of loop %i\n", loop
->num
);
558 loop_optimizer_finalize ();
562 if (TREE_READONLY (decl
))
564 l
->pure_const_state
= IPA_CONST
;
565 l
->state_previously_known
= IPA_CONST
;
566 if (!DECL_LOOPING_CONST_OR_PURE_P (decl
))
567 l
->looping
= false, l
->looping_previously_known
= false;
569 if (DECL_PURE_P (decl
))
571 if (l
->pure_const_state
!= IPA_CONST
)
572 l
->pure_const_state
= IPA_PURE
;
573 l
->state_previously_known
= IPA_PURE
;
574 if (!DECL_LOOPING_CONST_OR_PURE_P (decl
))
575 l
->looping
= false, l
->looping_previously_known
= false;
577 if (TREE_NOTHROW (decl
))
578 l
->can_throw
= false;
581 current_function_decl
= old_decl
;
585 fprintf (dump_file
, "Function is locally looping.\n");
587 fprintf (dump_file
, "Function is locally throwing.\n");
588 if (l
->pure_const_state
== IPA_CONST
)
589 fprintf (dump_file
, "Function is locally const.\n");
590 if (l
->pure_const_state
== IPA_PURE
)
591 fprintf (dump_file
, "Function is locally pure.\n");
596 /* Called when new function is inserted to callgraph late. */
598 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
600 if (cgraph_function_body_availability (node
) < AVAIL_OVERWRITABLE
)
602 /* There are some shared nodes, in particular the initializers on
603 static declarations. We do not need to scan them more than once
604 since all we would be interested in are the addressof
606 visited_nodes
= pointer_set_create ();
607 set_function_state (node
, analyze_function (node
, true));
608 pointer_set_destroy (visited_nodes
);
609 visited_nodes
= NULL
;
612 /* Called when new clone is inserted to callgraph late. */
615 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
616 void *data ATTRIBUTE_UNUSED
)
618 if (get_function_state (src
))
620 funct_state l
= XNEW (struct funct_state_d
);
621 gcc_assert (!get_function_state (dst
));
622 memcpy (l
, get_function_state (src
), sizeof (*l
));
623 set_function_state (dst
, l
);
627 /* Called when new clone is inserted to callgraph late. */
630 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
632 if (get_function_state (node
))
634 free (get_function_state (node
));
635 set_function_state (node
, NULL
);
641 register_hooks (void)
643 static bool init_p
= false;
650 node_removal_hook_holder
=
651 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
652 node_duplication_hook_holder
=
653 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
654 function_insertion_hook_holder
=
655 cgraph_add_function_insertion_hook (&add_new_function
, NULL
);
659 /* Analyze each function in the cgraph to see if it is locally PURE or
663 generate_summary (void)
665 struct cgraph_node
*node
;
669 /* There are some shared nodes, in particular the initializers on
670 static declarations. We do not need to scan them more than once
671 since all we would be interested in are the addressof
673 visited_nodes
= pointer_set_create ();
675 /* Process all of the functions.
677 We process AVAIL_OVERWRITABLE functions. We can not use the results
678 by default, but the info can be used at LTO with -fwhole-program or
679 when function got clonned and the clone is AVAILABLE. */
681 for (node
= cgraph_nodes
; node
; node
= node
->next
)
682 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
683 set_function_state (node
, analyze_function (node
, true));
685 pointer_set_destroy (visited_nodes
);
686 visited_nodes
= NULL
;
690 /* Serialize the ipa info for lto. */
693 pure_const_write_summary (cgraph_node_set set
)
695 struct cgraph_node
*node
;
696 struct lto_simple_output_block
*ob
697 = lto_create_simple_output_block (LTO_section_ipa_pure_const
);
698 unsigned int count
= 0;
699 cgraph_node_set_iterator csi
;
701 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
703 node
= csi_node (csi
);
704 if (node
->analyzed
&& get_function_state (node
) != NULL
)
708 lto_output_uleb128_stream (ob
->main_stream
, count
);
710 /* Process all of the functions. */
711 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
713 node
= csi_node (csi
);
714 if (node
->analyzed
&& get_function_state (node
) != NULL
)
716 struct bitpack_d
*bp
;
719 lto_cgraph_encoder_t encoder
;
721 fs
= get_function_state (node
);
723 encoder
= ob
->decl_state
->cgraph_node_encoder
;
724 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
725 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
727 /* Note that flags will need to be read in the opposite
728 order as we are pushing the bitflags into FLAGS. */
729 bp
= bitpack_create ();
730 bp_pack_value (bp
, fs
->pure_const_state
, 2);
731 bp_pack_value (bp
, fs
->state_previously_known
, 2);
732 bp_pack_value (bp
, fs
->looping_previously_known
, 1);
733 bp_pack_value (bp
, fs
->looping
, 1);
734 bp_pack_value (bp
, fs
->can_throw
, 1);
735 lto_output_bitpack (ob
->main_stream
, bp
);
740 lto_destroy_simple_output_block (ob
);
744 /* Deserialize the ipa info for lto. */
747 pure_const_read_summary (void)
749 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
750 struct lto_file_decl_data
*file_data
;
754 while ((file_data
= file_data_vec
[j
++]))
758 struct lto_input_block
*ib
759 = lto_create_simple_input_block (file_data
,
760 LTO_section_ipa_pure_const
,
765 unsigned int count
= lto_input_uleb128 (ib
);
767 for (i
= 0; i
< count
; i
++)
770 struct cgraph_node
*node
;
771 struct bitpack_d
*bp
;
773 lto_cgraph_encoder_t encoder
;
775 fs
= XCNEW (struct funct_state_d
);
776 index
= lto_input_uleb128 (ib
);
777 encoder
= file_data
->cgraph_node_encoder
;
778 node
= lto_cgraph_encoder_deref (encoder
, index
);
779 set_function_state (node
, fs
);
781 /* Note that the flags must be read in the opposite
782 order in which they were written (the bitflags were
783 pushed into FLAGS). */
784 bp
= lto_input_bitpack (ib
);
786 = (enum pure_const_state_e
) bp_unpack_value (bp
, 2);
787 fs
->state_previously_known
788 = (enum pure_const_state_e
) bp_unpack_value (bp
, 2);
789 fs
->looping_previously_known
= bp_unpack_value (bp
, 1);
790 fs
->looping
= bp_unpack_value (bp
, 1);
791 fs
->can_throw
= bp_unpack_value (bp
, 1);
795 lto_destroy_simple_input_block (file_data
,
796 LTO_section_ipa_pure_const
,
804 ignore_edge (struct cgraph_edge
*e
)
806 return (!e
->can_throw_external
);
809 /* Return true if NODE is self recursive function. */
812 self_recursive_p (struct cgraph_node
*node
)
814 struct cgraph_edge
*e
;
815 for (e
= node
->callees
; e
; e
= e
->next_callee
)
816 if (e
->callee
== node
)
821 /* Produce the global information by preforming a transitive closure
822 on the local information that was produced by generate_summary.
823 Note that there is no function_transform pass since this only
824 updates the function_decl. */
829 struct cgraph_node
*node
;
830 struct cgraph_node
*w
;
831 struct cgraph_node
**order
=
832 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
835 struct ipa_dfs_info
* w_info
;
837 cgraph_remove_function_insertion_hook (function_insertion_hook_holder
);
838 cgraph_remove_node_duplication_hook (node_duplication_hook_holder
);
839 cgraph_remove_node_removal_hook (node_removal_hook_holder
);
840 order_pos
= ipa_utils_reduced_inorder (order
, true, false, NULL
);
843 dump_cgraph (dump_file
);
844 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
847 /* Propagate the local information thru the call graph to produce
848 the global information. All the nodes within a cycle will have
849 the same info so we collapse cycles first. Then we can do the
850 propagation in one pass from the leaves to the roots. */
851 for (i
= 0; i
< order_pos
; i
++ )
853 enum pure_const_state_e pure_const_state
= IPA_CONST
;
854 bool looping
= false;
858 /* Find the worst state for any node in the cycle. */
862 struct cgraph_edge
*e
;
863 funct_state w_l
= get_function_state (w
);
864 if (pure_const_state
< w_l
->pure_const_state
)
865 pure_const_state
= w_l
->pure_const_state
;
869 if (cgraph_function_body_availability (w
) == AVAIL_OVERWRITABLE
)
871 looping
|= w_l
->looping_previously_known
;
872 if (pure_const_state
< w_l
->state_previously_known
)
873 pure_const_state
= w_l
->state_previously_known
;
876 if (pure_const_state
== IPA_NEITHER
)
884 for (e
= w
->callees
; e
; e
= e
->next_callee
)
886 struct cgraph_node
*y
= e
->callee
;
888 if (cgraph_function_body_availability (y
) > AVAIL_OVERWRITABLE
)
890 funct_state y_l
= get_function_state (y
);
891 if (pure_const_state
< y_l
->pure_const_state
)
892 pure_const_state
= y_l
->pure_const_state
;
893 if (pure_const_state
== IPA_NEITHER
)
900 int flags
= flags_from_decl_or_type (y
->decl
);
902 if (flags
& ECF_LOOPING_CONST_OR_PURE
)
904 if (flags
& ECF_CONST
)
906 else if ((flags
& ECF_PURE
) && pure_const_state
== IPA_CONST
)
907 pure_const_state
= IPA_PURE
;
909 pure_const_state
= IPA_NEITHER
, looping
= true;
913 w_info
= (struct ipa_dfs_info
*) w
->aux
;
914 w
= w_info
->next_cycle
;
917 /* Copy back the region's pure_const_state which is shared by
918 all nodes in the region. */
922 funct_state w_l
= get_function_state (w
);
923 enum pure_const_state_e this_state
= pure_const_state
;
924 bool this_looping
= looping
;
926 if (w_l
->state_previously_known
!= IPA_NEITHER
927 && this_state
> w_l
->state_previously_known
)
928 this_state
= w_l
->state_previously_known
;
929 if (!this_looping
&& self_recursive_p (w
))
931 if (!w_l
->looping_previously_known
)
932 this_looping
= false;
934 /* All nodes within a cycle share the same info. */
935 w_l
->pure_const_state
= this_state
;
936 w_l
->looping
= this_looping
;
941 if (!TREE_READONLY (w
->decl
) && dump_file
)
942 fprintf (dump_file
, "Function found to be %sconst: %s\n",
943 this_looping
? "looping " : "",
944 cgraph_node_name (w
));
945 cgraph_set_readonly_flag (w
, true);
946 cgraph_set_looping_const_or_pure_flag (w
, this_looping
);
950 if (!DECL_PURE_P (w
->decl
) && dump_file
)
951 fprintf (dump_file
, "Function found to be %spure: %s\n",
952 this_looping
? "looping " : "",
953 cgraph_node_name (w
));
954 cgraph_set_pure_flag (w
, true);
955 cgraph_set_looping_const_or_pure_flag (w
, this_looping
);
961 w_info
= (struct ipa_dfs_info
*) w
->aux
;
962 w
= w_info
->next_cycle
;
967 for (node
= cgraph_nodes
; node
; node
= node
->next
)
969 /* Get rid of the aux information. */
972 w_info
= (struct ipa_dfs_info
*) node
->aux
;
977 order_pos
= ipa_utils_reduced_inorder (order
, true, false, ignore_edge
);
980 dump_cgraph (dump_file
);
981 ipa_utils_print_order(dump_file
, "reduced for nothrow", order
, order_pos
);
983 /* Propagate the local information thru the call graph to produce
984 the global information. All the nodes within a cycle will have
985 the same info so we collapse cycles first. Then we can do the
986 propagation in one pass from the leaves to the roots. */
987 for (i
= 0; i
< order_pos
; i
++ )
989 bool can_throw
= false;
992 /* Find the worst state for any node in the cycle. */
996 struct cgraph_edge
*e
;
997 funct_state w_l
= get_function_state (w
);
1000 || cgraph_function_body_availability (w
) == AVAIL_OVERWRITABLE
)
1006 for (e
= w
->callees
; e
; e
= e
->next_callee
)
1008 struct cgraph_node
*y
= e
->callee
;
1010 if (cgraph_function_body_availability (y
) > AVAIL_OVERWRITABLE
)
1012 funct_state y_l
= get_function_state (y
);
1016 if (y_l
->can_throw
&& !TREE_NOTHROW (w
->decl
)
1017 && e
->can_throw_external
)
1020 else if (e
->can_throw_external
&& !TREE_NOTHROW (y
->decl
))
1023 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1024 w
= w_info
->next_cycle
;
1027 /* Copy back the region's pure_const_state which is shared by
1028 all nodes in the region. */
1032 funct_state w_l
= get_function_state (w
);
1033 if (!can_throw
&& !TREE_NOTHROW (w
->decl
))
1035 struct cgraph_edge
*e
;
1036 cgraph_set_nothrow_flag (w
, true);
1037 for (e
= w
->callers
; e
; e
= e
->next_caller
)
1038 e
->can_throw_external
= false;
1040 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1041 cgraph_node_name (w
));
1043 else if (can_throw
&& !TREE_NOTHROW (w
->decl
))
1044 w_l
->can_throw
= true;
1045 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1046 w
= w_info
->next_cycle
;
1051 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1053 /* Get rid of the aux information. */
1056 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1060 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
1061 free (get_function_state (node
));
1065 VEC_free (funct_state
, heap
, funct_state_vec
);
1071 gate_pure_const (void)
1073 return (flag_ipa_pure_const
1074 /* Don't bother doing anything if the program has errors. */
1075 && !(errorcount
|| sorrycount
));
1078 struct ipa_opt_pass_d pass_ipa_pure_const
=
1082 "pure-const", /* name */
1083 gate_pure_const
, /* gate */
1084 propagate
, /* execute */
1087 0, /* static_pass_number */
1088 TV_IPA_PURE_CONST
, /* tv_id */
1089 0, /* properties_required */
1090 0, /* properties_provided */
1091 0, /* properties_destroyed */
1092 0, /* todo_flags_start */
1093 0 /* todo_flags_finish */
1095 generate_summary
, /* generate_summary */
1096 pure_const_write_summary
, /* write_summary */
1097 pure_const_read_summary
, /* read_summary */
1098 NULL
, /* function_read_summary */
1099 NULL
, /* stmt_fixup */
1101 NULL
, /* function_transform */
1102 NULL
/* variable_transform */
1105 /* Simple local pass for pure const discovery reusing the analysis from
1106 ipa_pure_const. This pass is effective when executed together with
1107 other optimization passes in early optimization pass queue. */
1110 local_pure_const (void)
1112 bool changed
= false;
1114 struct cgraph_node
*node
;
1116 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1117 we must not promote functions that are called by already processed functions. */
1119 if (function_called_by_processed_nodes_p ())
1122 fprintf (dump_file
, "Function called in recursive cycle; ignoring\n");
1125 node
= cgraph_node (current_function_decl
);
1126 if (cgraph_function_body_availability (node
) <= AVAIL_OVERWRITABLE
)
1129 fprintf (dump_file
, "Function has wrong visibility; ignoring\n");
1133 l
= analyze_function (node
, false);
1135 switch (l
->pure_const_state
)
1138 if (!TREE_READONLY (current_function_decl
))
1140 cgraph_set_readonly_flag (node
, true);
1141 cgraph_set_looping_const_or_pure_flag (node
, l
->looping
);
1144 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1145 l
->looping
? "looping " : "",
1146 lang_hooks
.decl_printable_name (current_function_decl
,
1149 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1152 cgraph_set_looping_const_or_pure_flag (node
, false);
1155 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1156 lang_hooks
.decl_printable_name (current_function_decl
,
1162 if (!TREE_READONLY (current_function_decl
))
1164 cgraph_set_pure_flag (node
, true);
1165 cgraph_set_looping_const_or_pure_flag (node
, l
->looping
);
1168 fprintf (dump_file
, "Function found to be %spure: %s\n",
1169 l
->looping
? "looping " : "",
1170 lang_hooks
.decl_printable_name (current_function_decl
,
1173 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1176 cgraph_set_looping_const_or_pure_flag (node
, false);
1179 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1180 lang_hooks
.decl_printable_name (current_function_decl
,
1188 if (!l
->can_throw
&& !TREE_NOTHROW (current_function_decl
))
1190 struct cgraph_edge
*e
;
1192 cgraph_set_nothrow_flag (node
, true);
1193 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1194 e
->can_throw_external
= false;
1197 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1198 lang_hooks
.decl_printable_name (current_function_decl
,
1204 return execute_fixup_cfg ();
1209 struct gimple_opt_pass pass_local_pure_const
=
1213 "local-pure-const", /* name */
1214 gate_pure_const
, /* gate */
1215 local_pure_const
, /* execute */
1218 0, /* static_pass_number */
1219 TV_IPA_PURE_CONST
, /* tv_id */
1220 0, /* properties_required */
1221 0, /* properties_provided */
1222 0, /* properties_destroyed */
1223 0, /* todo_flags_start */
1224 0 /* todo_flags_finish */