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"
53 #include "diagnostic.h"
54 #include "gimple-pretty-print.h"
55 #include "langhooks.h"
57 #include "lto-streamer.h"
59 #include "tree-scalar-evolution.h"
63 static struct pointer_set_t
*visited_nodes
;
65 /* Lattice values for const and pure functions. Everything starts out
66 being const, then may drop to pure and then neither depending on
68 enum pure_const_state_e
75 /* Holder for the const_state. There is one of these per function
80 enum pure_const_state_e pure_const_state
;
81 /* What user set here; we can be always sure about this. */
82 enum pure_const_state_e state_previously_known
;
83 bool looping_previously_known
;
85 /* True if the function could possibly infinite loop. There are a
86 lot of ways that this could be determined. We are pretty
87 conservative here. While it is possible to cse pure and const
88 calls, it is not legal to have dce get rid of the call if there
89 is a possibility that the call could infinite loop since this is
90 a behavioral change. */
96 typedef struct funct_state_d
* funct_state
;
98 /* The storage of the funct_state is abstracted because there is the
99 possibility that it may be desirable to move this to the cgraph
102 /* Array, indexed by cgraph node uid, of function states. */
104 DEF_VEC_P (funct_state
);
105 DEF_VEC_ALLOC_P (funct_state
, heap
);
106 static VEC (funct_state
, heap
) *funct_state_vec
;
108 /* Holders of ipa cgraph hooks: */
109 static struct cgraph_node_hook_list
*function_insertion_hook_holder
;
110 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
111 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
113 /* Try to guess if function body will always be visible to compiler
114 when compiling the call and whether compiler will be able
115 to propagate the information by itself. */
118 function_always_visible_to_compiler_p (tree decl
)
120 return (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
));
123 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
124 is true if the function is known to be finite. The diagnostic is
125 controlled by OPTION. WARNED_ABOUT is a pointer_set unique for
126 OPTION, this function may initialize it and it is always returned
129 static struct pointer_set_t
*
130 suggest_attribute (int option
, tree decl
, bool known_finite
,
131 struct pointer_set_t
*warned_about
,
132 const char * attrib_name
)
134 if (!option_enabled (option
))
136 if (TREE_THIS_VOLATILE (decl
)
137 || (known_finite
&& function_always_visible_to_compiler_p (decl
)))
141 warned_about
= pointer_set_create ();
142 if (pointer_set_contains (warned_about
, decl
))
144 pointer_set_insert (warned_about
, decl
);
145 warning_at (DECL_SOURCE_LOCATION (decl
),
148 ? _("function might be candidate for attribute %<%s%>")
149 : _("function might be candidate for attribute %<%s%>"
150 " if it is known to return normally"), attrib_name
);
154 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
155 is true if the function is known to be finite. */
158 warn_function_pure (tree decl
, bool known_finite
)
160 static struct pointer_set_t
*warned_about
;
163 = suggest_attribute (OPT_Wsuggest_attribute_pure
, decl
,
164 known_finite
, warned_about
, "pure");
167 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
168 is true if the function is known to be finite. */
171 warn_function_const (tree decl
, bool known_finite
)
173 static struct pointer_set_t
*warned_about
;
175 = suggest_attribute (OPT_Wsuggest_attribute_const
, decl
,
176 known_finite
, warned_about
, "const");
178 /* Init the function state. */
183 free (funct_state_vec
);
187 /* Return the function state from NODE. */
189 static inline funct_state
190 get_function_state (struct cgraph_node
*node
)
193 || VEC_length (funct_state
, funct_state_vec
) <= (unsigned int)node
->uid
)
195 return VEC_index (funct_state
, funct_state_vec
, node
->uid
);
198 /* Set the function state S for NODE. */
201 set_function_state (struct cgraph_node
*node
, funct_state s
)
204 || VEC_length (funct_state
, funct_state_vec
) <= (unsigned int)node
->uid
)
205 VEC_safe_grow_cleared (funct_state
, heap
, funct_state_vec
, node
->uid
+ 1);
206 VEC_replace (funct_state
, funct_state_vec
, node
->uid
, s
);
209 /* Check to see if the use (or definition when CHECKING_WRITE is true)
210 variable T is legal in a function that is either pure or const. */
213 check_decl (funct_state local
,
214 tree t
, bool checking_write
)
216 /* Do not want to do anything with volatile except mark any
217 function that uses one to be not const or pure. */
218 if (TREE_THIS_VOLATILE (t
))
220 local
->pure_const_state
= IPA_NEITHER
;
222 fprintf (dump_file
, " Volatile operand is not const/pure");
226 /* Do not care about a local automatic that is not static. */
227 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
230 /* If the variable has the "used" attribute, treat it as if it had a
231 been touched by the devil. */
232 if (DECL_PRESERVE_P (t
))
234 local
->pure_const_state
= IPA_NEITHER
;
236 fprintf (dump_file
, " Used static/global variable is not const/pure\n");
240 /* Since we have dealt with the locals and params cases above, if we
241 are CHECKING_WRITE, this cannot be a pure or constant
245 local
->pure_const_state
= IPA_NEITHER
;
247 fprintf (dump_file
, " static/global memory write is not const/pure\n");
251 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
253 /* Readonly reads are safe. */
254 if (TREE_READONLY (t
) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t
)))
255 return; /* Read of a constant, do not change the function state. */
259 fprintf (dump_file
, " global memory read is not const\n");
260 /* Just a regular read. */
261 if (local
->pure_const_state
== IPA_CONST
)
262 local
->pure_const_state
= IPA_PURE
;
267 /* Compilation level statics can be read if they are readonly
269 if (TREE_READONLY (t
))
273 fprintf (dump_file
, " static memory read is not const\n");
274 /* Just a regular read. */
275 if (local
->pure_const_state
== IPA_CONST
)
276 local
->pure_const_state
= IPA_PURE
;
281 /* Check to see if the use (or definition when CHECKING_WRITE is true)
282 variable T is legal in a function that is either pure or const. */
285 check_op (funct_state local
, tree t
, bool checking_write
)
287 t
= get_base_address (t
);
288 if (t
&& TREE_THIS_VOLATILE (t
))
290 local
->pure_const_state
= IPA_NEITHER
;
292 fprintf (dump_file
, " Volatile indirect ref is not const/pure\n");
296 && INDIRECT_REF_P (t
)
297 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
298 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t
, 0)))
301 fprintf (dump_file
, " Indirect ref to local memory is OK\n");
304 else if (checking_write
)
306 local
->pure_const_state
= IPA_NEITHER
;
308 fprintf (dump_file
, " Indirect ref write is not const/pure\n");
314 fprintf (dump_file
, " Indirect ref read is not const\n");
315 if (local
->pure_const_state
== IPA_CONST
)
316 local
->pure_const_state
= IPA_PURE
;
320 /* Check the parameters of a function call to CALL_EXPR to see if
321 there are any references in the parameters that are not allowed for
322 pure or const functions. Also check to see if this is either an
323 indirect call, a call outside the compilation unit, or has special
324 attributes that may also effect the purity. The CALL_EXPR node for
325 the entire call expression. */
328 check_call (funct_state local
, gimple call
, bool ipa
)
330 int flags
= gimple_call_flags (call
);
331 tree callee_t
= gimple_call_fndecl (call
);
332 bool possibly_throws
= stmt_could_throw_p (call
);
333 bool possibly_throws_externally
= (possibly_throws
334 && stmt_can_throw_external (call
));
339 for (i
= 0; i
< gimple_num_ops (call
); i
++)
340 if (gimple_op (call
, i
)
341 && tree_could_throw_p (gimple_op (call
, i
)))
343 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
346 fprintf (dump_file
, " operand can throw; looping\n");
347 local
->looping
= true;
349 if (possibly_throws_externally
)
352 fprintf (dump_file
, " operand can throw externally\n");
353 local
->can_throw
= true;
358 /* The const and pure flags are set by a variety of places in the
359 compiler (including here). If someone has already set the flags
360 for the callee, (such as for some of the builtins) we will use
361 them, otherwise we will compute our own information.
363 Const and pure functions have less clobber effects than other
364 functions so we process these first. Otherwise if it is a call
365 outside the compilation unit or an indirect call we punt. This
366 leaves local calls which will be processed by following the call
370 /* When bad things happen to bad functions, they cannot be const
372 if (setjmp_call_p (callee_t
))
375 fprintf (dump_file
, " setjmp is not const/pure\n");
376 local
->looping
= true;
377 local
->pure_const_state
= IPA_NEITHER
;
380 if (DECL_BUILT_IN_CLASS (callee_t
) == BUILT_IN_NORMAL
)
381 switch (DECL_FUNCTION_CODE (callee_t
))
383 case BUILT_IN_LONGJMP
:
384 case BUILT_IN_NONLOCAL_GOTO
:
386 fprintf (dump_file
, " longjmp and nonlocal goto is not const/pure\n");
387 local
->pure_const_state
= IPA_NEITHER
;
388 local
->looping
= true;
395 /* When not in IPA mode, we can still handle self recursion. */
396 if (!ipa
&& callee_t
== current_function_decl
)
399 fprintf (dump_file
, " Recursive call can loop.\n");
400 local
->looping
= true;
402 /* Either calle is unknown or we are doing local analysis.
403 Look to see if there are any bits available for the callee (such as by
404 declaration or because it is builtin) and process solely on the basis of
406 else if (!ipa
|| !callee_t
)
408 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
411 fprintf (dump_file
, " can throw; looping\n");
412 local
->looping
= true;
414 if (possibly_throws_externally
)
418 fprintf (dump_file
, " can throw externally to lp %i\n",
419 lookup_stmt_eh_lp (call
));
421 fprintf (dump_file
, " callee:%s\n",
422 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t
)));
424 local
->can_throw
= true;
426 if (flags
& ECF_CONST
)
428 if (callee_t
&& DECL_LOOPING_CONST_OR_PURE_P (callee_t
))
431 fprintf (dump_file
, " calls looping pure.\n");
432 local
->looping
= true;
435 else if (flags
& ECF_PURE
)
437 if (callee_t
&& DECL_LOOPING_CONST_OR_PURE_P (callee_t
))
440 fprintf (dump_file
, " calls looping const.\n");
441 local
->looping
= true;
444 fprintf (dump_file
, " pure function call in not const\n");
445 if (local
->pure_const_state
== IPA_CONST
)
446 local
->pure_const_state
= IPA_PURE
;
451 fprintf (dump_file
, " uknown function call is not const/pure\n");
452 local
->pure_const_state
= IPA_NEITHER
;
453 local
->looping
= true;
456 /* Direct functions calls are handled by IPA propagation. */
459 /* Wrapper around check_decl for loads. */
462 check_load (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
465 check_decl ((funct_state
)data
, op
, false);
467 check_op ((funct_state
)data
, op
, false);
471 /* Wrapper around check_decl for stores. */
474 check_store (gimple stmt ATTRIBUTE_UNUSED
, tree op
, void *data
)
477 check_decl ((funct_state
)data
, op
, true);
479 check_op ((funct_state
)data
, op
, true);
483 /* Look into pointer pointed to by GSIP and figure out what interesting side
486 check_stmt (gimple_stmt_iterator
*gsip
, funct_state local
, bool ipa
)
488 gimple stmt
= gsi_stmt (*gsip
);
491 if (is_gimple_debug (stmt
))
496 fprintf (dump_file
, " scanning: ");
497 print_gimple_stmt (dump_file
, stmt
, 0, 0);
500 /* Look for loads and stores. */
501 walk_stmt_load_store_ops (stmt
, local
, check_load
, check_store
);
503 if (gimple_code (stmt
) != GIMPLE_CALL
504 && stmt_could_throw_p (stmt
))
506 if (cfun
->can_throw_non_call_exceptions
)
509 fprintf (dump_file
, " can throw; looping");
510 local
->looping
= true;
512 if (stmt_can_throw_external (stmt
))
515 fprintf (dump_file
, " can throw externally");
516 local
->can_throw
= true;
519 switch (gimple_code (stmt
))
522 check_call (local
, stmt
, ipa
);
525 if (DECL_NONLOCAL (gimple_label_label (stmt
)))
526 /* Target of long jump. */
529 fprintf (dump_file
, " nonlocal label is not const/pure");
530 local
->pure_const_state
= IPA_NEITHER
;
534 for (i
= 0; i
< gimple_asm_nclobbers (stmt
); i
++)
536 tree op
= gimple_asm_clobber_op (stmt
, i
);
537 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op
)), "memory") == 0)
540 fprintf (dump_file
, " memory asm clobber is not const/pure");
541 /* Abandon all hope, ye who enter here. */
542 local
->pure_const_state
= IPA_NEITHER
;
545 if (gimple_asm_volatile_p (stmt
))
548 fprintf (dump_file
, " volatile is not const/pure");
549 /* Abandon all hope, ye who enter here. */
550 local
->pure_const_state
= IPA_NEITHER
;
551 local
->looping
= true;
560 /* This is the main routine for finding the reference patterns for
561 global variables within a function FN. */
564 analyze_function (struct cgraph_node
*fn
, bool ipa
)
566 tree decl
= fn
->decl
;
567 tree old_decl
= current_function_decl
;
569 basic_block this_block
;
571 l
= XCNEW (struct funct_state_d
);
572 l
->pure_const_state
= IPA_CONST
;
573 l
->state_previously_known
= IPA_NEITHER
;
574 l
->looping_previously_known
= true;
576 l
->can_throw
= false;
580 fprintf (dump_file
, "\n\n local analysis of %s\n ",
581 cgraph_node_name (fn
));
584 push_cfun (DECL_STRUCT_FUNCTION (decl
));
585 current_function_decl
= decl
;
587 FOR_EACH_BB (this_block
)
589 gimple_stmt_iterator gsi
;
590 struct walk_stmt_info wi
;
592 memset (&wi
, 0, sizeof(wi
));
593 for (gsi
= gsi_start_bb (this_block
);
597 check_stmt (&gsi
, l
, ipa
);
598 if (l
->pure_const_state
== IPA_NEITHER
&& l
->looping
&& l
->can_throw
)
604 if (l
->pure_const_state
!= IPA_NEITHER
)
606 /* Const functions cannot have back edges (an
607 indication of possible infinite loop side
609 if (mark_dfs_back_edges ())
611 /* Preheaders are needed for SCEV to work.
612 Simple lateches and recorded exits improve chances that loop will
613 proved to be finite in testcases such as in loop-15.c and loop-24.c */
614 loop_optimizer_init (LOOPS_NORMAL
615 | LOOPS_HAVE_RECORDED_EXITS
);
616 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
617 flow_loops_dump (dump_file
, NULL
, 0);
618 if (mark_irreducible_loops ())
621 fprintf (dump_file
, " has irreducible loops\n");
629 FOR_EACH_LOOP (li
, loop
, 0)
630 if (!finite_loop_p (loop
))
633 fprintf (dump_file
, " can not prove finiteness of loop %i\n", loop
->num
);
639 loop_optimizer_finalize ();
643 if (TREE_READONLY (decl
))
645 l
->pure_const_state
= IPA_CONST
;
646 l
->state_previously_known
= IPA_CONST
;
647 if (!DECL_LOOPING_CONST_OR_PURE_P (decl
))
648 l
->looping
= false, l
->looping_previously_known
= false;
650 if (DECL_PURE_P (decl
))
652 if (l
->pure_const_state
!= IPA_CONST
)
653 l
->pure_const_state
= IPA_PURE
;
654 l
->state_previously_known
= IPA_PURE
;
655 if (!DECL_LOOPING_CONST_OR_PURE_P (decl
))
656 l
->looping
= false, l
->looping_previously_known
= false;
658 if (TREE_NOTHROW (decl
))
659 l
->can_throw
= false;
662 current_function_decl
= old_decl
;
666 fprintf (dump_file
, "Function is locally looping.\n");
668 fprintf (dump_file
, "Function is locally throwing.\n");
669 if (l
->pure_const_state
== IPA_CONST
)
670 fprintf (dump_file
, "Function is locally const.\n");
671 if (l
->pure_const_state
== IPA_PURE
)
672 fprintf (dump_file
, "Function is locally pure.\n");
677 /* Called when new function is inserted to callgraph late. */
679 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
681 if (cgraph_function_body_availability (node
) < AVAIL_OVERWRITABLE
)
683 /* There are some shared nodes, in particular the initializers on
684 static declarations. We do not need to scan them more than once
685 since all we would be interested in are the addressof
687 visited_nodes
= pointer_set_create ();
688 if (cgraph_function_body_availability (node
) > AVAIL_OVERWRITABLE
)
689 set_function_state (node
, analyze_function (node
, true));
690 pointer_set_destroy (visited_nodes
);
691 visited_nodes
= NULL
;
694 /* Called when new clone is inserted to callgraph late. */
697 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
698 void *data ATTRIBUTE_UNUSED
)
700 if (get_function_state (src
))
702 funct_state l
= XNEW (struct funct_state_d
);
703 gcc_assert (!get_function_state (dst
));
704 memcpy (l
, get_function_state (src
), sizeof (*l
));
705 set_function_state (dst
, l
);
709 /* Called when new clone is inserted to callgraph late. */
712 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
714 if (get_function_state (node
))
716 free (get_function_state (node
));
717 set_function_state (node
, NULL
);
723 register_hooks (void)
725 static bool init_p
= false;
732 node_removal_hook_holder
=
733 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
734 node_duplication_hook_holder
=
735 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
736 function_insertion_hook_holder
=
737 cgraph_add_function_insertion_hook (&add_new_function
, NULL
);
741 /* Analyze each function in the cgraph to see if it is locally PURE or
745 generate_summary (void)
747 struct cgraph_node
*node
;
751 /* There are some shared nodes, in particular the initializers on
752 static declarations. We do not need to scan them more than once
753 since all we would be interested in are the addressof
755 visited_nodes
= pointer_set_create ();
757 /* Process all of the functions.
759 We process AVAIL_OVERWRITABLE functions. We can not use the results
760 by default, but the info can be used at LTO with -fwhole-program or
761 when function got clonned and the clone is AVAILABLE. */
763 for (node
= cgraph_nodes
; node
; node
= node
->next
)
764 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
765 set_function_state (node
, analyze_function (node
, true));
767 pointer_set_destroy (visited_nodes
);
768 visited_nodes
= NULL
;
772 /* Serialize the ipa info for lto. */
775 pure_const_write_summary (cgraph_node_set set
,
776 varpool_node_set vset ATTRIBUTE_UNUSED
)
778 struct cgraph_node
*node
;
779 struct lto_simple_output_block
*ob
780 = lto_create_simple_output_block (LTO_section_ipa_pure_const
);
781 unsigned int count
= 0;
782 cgraph_node_set_iterator csi
;
784 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
786 node
= csi_node (csi
);
787 if (node
->analyzed
&& get_function_state (node
) != NULL
)
791 lto_output_uleb128_stream (ob
->main_stream
, count
);
793 /* Process all of the functions. */
794 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
796 node
= csi_node (csi
);
797 if (node
->analyzed
&& get_function_state (node
) != NULL
)
799 struct bitpack_d
*bp
;
802 lto_cgraph_encoder_t encoder
;
804 fs
= get_function_state (node
);
806 encoder
= ob
->decl_state
->cgraph_node_encoder
;
807 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
808 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
810 /* Note that flags will need to be read in the opposite
811 order as we are pushing the bitflags into FLAGS. */
812 bp
= bitpack_create ();
813 bp_pack_value (bp
, fs
->pure_const_state
, 2);
814 bp_pack_value (bp
, fs
->state_previously_known
, 2);
815 bp_pack_value (bp
, fs
->looping_previously_known
, 1);
816 bp_pack_value (bp
, fs
->looping
, 1);
817 bp_pack_value (bp
, fs
->can_throw
, 1);
818 lto_output_bitpack (ob
->main_stream
, bp
);
823 lto_destroy_simple_output_block (ob
);
827 /* Deserialize the ipa info for lto. */
830 pure_const_read_summary (void)
832 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
833 struct lto_file_decl_data
*file_data
;
837 while ((file_data
= file_data_vec
[j
++]))
841 struct lto_input_block
*ib
842 = lto_create_simple_input_block (file_data
,
843 LTO_section_ipa_pure_const
,
848 unsigned int count
= lto_input_uleb128 (ib
);
850 for (i
= 0; i
< count
; i
++)
853 struct cgraph_node
*node
;
854 struct bitpack_d
*bp
;
856 lto_cgraph_encoder_t encoder
;
858 fs
= XCNEW (struct funct_state_d
);
859 index
= lto_input_uleb128 (ib
);
860 encoder
= file_data
->cgraph_node_encoder
;
861 node
= lto_cgraph_encoder_deref (encoder
, index
);
862 set_function_state (node
, fs
);
864 /* Note that the flags must be read in the opposite
865 order in which they were written (the bitflags were
866 pushed into FLAGS). */
867 bp
= lto_input_bitpack (ib
);
869 = (enum pure_const_state_e
) bp_unpack_value (bp
, 2);
870 fs
->state_previously_known
871 = (enum pure_const_state_e
) bp_unpack_value (bp
, 2);
872 fs
->looping_previously_known
= bp_unpack_value (bp
, 1);
873 fs
->looping
= bp_unpack_value (bp
, 1);
874 fs
->can_throw
= bp_unpack_value (bp
, 1);
878 lto_destroy_simple_input_block (file_data
,
879 LTO_section_ipa_pure_const
,
887 ignore_edge (struct cgraph_edge
*e
)
889 return (!e
->can_throw_external
);
892 /* Return true if NODE is self recursive function. */
895 self_recursive_p (struct cgraph_node
*node
)
897 struct cgraph_edge
*e
;
898 for (e
= node
->callees
; e
; e
= e
->next_callee
)
899 if (e
->callee
== node
)
904 /* Produce the global information by preforming a transitive closure
905 on the local information that was produced by generate_summary.
906 Note that there is no function_transform pass since this only
907 updates the function_decl. */
912 struct cgraph_node
*node
;
913 struct cgraph_node
*w
;
914 struct cgraph_node
**order
=
915 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
918 struct ipa_dfs_info
* w_info
;
920 cgraph_remove_function_insertion_hook (function_insertion_hook_holder
);
921 cgraph_remove_node_duplication_hook (node_duplication_hook_holder
);
922 cgraph_remove_node_removal_hook (node_removal_hook_holder
);
923 order_pos
= ipa_utils_reduced_inorder (order
, true, false, NULL
);
926 dump_cgraph (dump_file
);
927 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
930 /* Propagate the local information thru the call graph to produce
931 the global information. All the nodes within a cycle will have
932 the same info so we collapse cycles first. Then we can do the
933 propagation in one pass from the leaves to the roots. */
934 for (i
= 0; i
< order_pos
; i
++ )
936 enum pure_const_state_e pure_const_state
= IPA_CONST
;
937 bool looping
= false;
941 /* Find the worst state for any node in the cycle. */
945 struct cgraph_edge
*e
;
946 funct_state w_l
= get_function_state (w
);
947 if (pure_const_state
< w_l
->pure_const_state
)
948 pure_const_state
= w_l
->pure_const_state
;
952 if (cgraph_function_body_availability (w
) == AVAIL_OVERWRITABLE
)
954 looping
|= w_l
->looping_previously_known
;
955 if (pure_const_state
< w_l
->state_previously_known
)
956 pure_const_state
= w_l
->state_previously_known
;
959 if (pure_const_state
== IPA_NEITHER
)
967 for (e
= w
->callees
; e
; e
= e
->next_callee
)
969 struct cgraph_node
*y
= e
->callee
;
971 if (cgraph_function_body_availability (y
) > AVAIL_OVERWRITABLE
)
973 funct_state y_l
= get_function_state (y
);
974 if (pure_const_state
< y_l
->pure_const_state
)
975 pure_const_state
= y_l
->pure_const_state
;
976 if (pure_const_state
== IPA_NEITHER
)
983 int flags
= flags_from_decl_or_type (y
->decl
);
985 if (flags
& ECF_LOOPING_CONST_OR_PURE
)
987 if (flags
& ECF_CONST
)
989 else if ((flags
& ECF_PURE
) && pure_const_state
== IPA_CONST
)
990 pure_const_state
= IPA_PURE
;
992 pure_const_state
= IPA_NEITHER
, looping
= true;
996 w_info
= (struct ipa_dfs_info
*) w
->aux
;
997 w
= w_info
->next_cycle
;
1000 /* Copy back the region's pure_const_state which is shared by
1001 all nodes in the region. */
1005 funct_state w_l
= get_function_state (w
);
1006 enum pure_const_state_e this_state
= pure_const_state
;
1007 bool this_looping
= looping
;
1009 if (w_l
->state_previously_known
!= IPA_NEITHER
1010 && this_state
> w_l
->state_previously_known
)
1011 this_state
= w_l
->state_previously_known
;
1012 if (!this_looping
&& self_recursive_p (w
))
1013 this_looping
= true;
1014 if (!w_l
->looping_previously_known
)
1015 this_looping
= false;
1017 /* All nodes within a cycle share the same info. */
1018 w_l
->pure_const_state
= this_state
;
1019 w_l
->looping
= this_looping
;
1024 if (!TREE_READONLY (w
->decl
))
1026 warn_function_const (w
->decl
, !this_looping
);
1028 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1029 this_looping
? "looping " : "",
1030 cgraph_node_name (w
));
1032 cgraph_set_readonly_flag (w
, true);
1033 cgraph_set_looping_const_or_pure_flag (w
, this_looping
);
1037 if (!DECL_PURE_P (w
->decl
))
1039 warn_function_pure (w
->decl
, !this_looping
);
1041 fprintf (dump_file
, "Function found to be %spure: %s\n",
1042 this_looping
? "looping " : "",
1043 cgraph_node_name (w
));
1045 cgraph_set_pure_flag (w
, true);
1046 cgraph_set_looping_const_or_pure_flag (w
, this_looping
);
1052 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1053 w
= w_info
->next_cycle
;
1058 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1060 /* Get rid of the aux information. */
1063 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1068 order_pos
= ipa_utils_reduced_inorder (order
, true, false, ignore_edge
);
1071 dump_cgraph (dump_file
);
1072 ipa_utils_print_order(dump_file
, "reduced for nothrow", order
, order_pos
);
1074 /* Propagate the local information thru the call graph to produce
1075 the global information. All the nodes within a cycle will have
1076 the same info so we collapse cycles first. Then we can do the
1077 propagation in one pass from the leaves to the roots. */
1078 for (i
= 0; i
< order_pos
; i
++ )
1080 bool can_throw
= false;
1083 /* Find the worst state for any node in the cycle. */
1087 struct cgraph_edge
*e
;
1088 funct_state w_l
= get_function_state (w
);
1091 || cgraph_function_body_availability (w
) == AVAIL_OVERWRITABLE
)
1097 for (e
= w
->callees
; e
; e
= e
->next_callee
)
1099 struct cgraph_node
*y
= e
->callee
;
1101 if (cgraph_function_body_availability (y
) > AVAIL_OVERWRITABLE
)
1103 funct_state y_l
= get_function_state (y
);
1107 if (y_l
->can_throw
&& !TREE_NOTHROW (w
->decl
)
1108 && e
->can_throw_external
)
1111 else if (e
->can_throw_external
&& !TREE_NOTHROW (y
->decl
))
1114 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1115 w
= w_info
->next_cycle
;
1118 /* Copy back the region's pure_const_state which is shared by
1119 all nodes in the region. */
1123 funct_state w_l
= get_function_state (w
);
1124 if (!can_throw
&& !TREE_NOTHROW (w
->decl
))
1126 struct cgraph_edge
*e
;
1127 cgraph_set_nothrow_flag (w
, true);
1128 for (e
= w
->callers
; e
; e
= e
->next_caller
)
1129 e
->can_throw_external
= false;
1131 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1132 cgraph_node_name (w
));
1134 else if (can_throw
&& !TREE_NOTHROW (w
->decl
))
1135 w_l
->can_throw
= true;
1136 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1137 w
= w_info
->next_cycle
;
1142 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1144 /* Get rid of the aux information. */
1147 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1151 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
1152 free (get_function_state (node
));
1156 VEC_free (funct_state
, heap
, funct_state_vec
);
1162 gate_pure_const (void)
1164 return (flag_ipa_pure_const
1165 /* Don't bother doing anything if the program has errors. */
1169 struct ipa_opt_pass_d pass_ipa_pure_const
=
1173 "pure-const", /* name */
1174 gate_pure_const
, /* gate */
1175 propagate
, /* execute */
1178 0, /* static_pass_number */
1179 TV_IPA_PURE_CONST
, /* tv_id */
1180 0, /* properties_required */
1181 0, /* properties_provided */
1182 0, /* properties_destroyed */
1183 0, /* todo_flags_start */
1184 0 /* todo_flags_finish */
1186 generate_summary
, /* generate_summary */
1187 pure_const_write_summary
, /* write_summary */
1188 pure_const_read_summary
, /* read_summary */
1189 NULL
, /* write_optimization_summary */
1190 NULL
, /* read_optimization_summary */
1191 NULL
, /* stmt_fixup */
1193 NULL
, /* function_transform */
1194 NULL
/* variable_transform */
1197 /* Return true if function should be skipped for local pure const analysis. */
1200 skip_function_for_local_pure_const (struct cgraph_node
*node
)
1202 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1203 we must not promote functions that are called by already processed functions. */
1205 if (function_called_by_processed_nodes_p ())
1208 fprintf (dump_file
, "Function called in recursive cycle; ignoring\n");
1211 if (cgraph_function_body_availability (node
) <= AVAIL_OVERWRITABLE
)
1214 fprintf (dump_file
, "Function is not available or overwrittable; not analyzing.\n");
1220 /* Simple local pass for pure const discovery reusing the analysis from
1221 ipa_pure_const. This pass is effective when executed together with
1222 other optimization passes in early optimization pass queue. */
1225 local_pure_const (void)
1227 bool changed
= false;
1230 struct cgraph_node
*node
;
1232 node
= cgraph_node (current_function_decl
);
1233 skip
= skip_function_for_local_pure_const (node
);
1234 if (!warn_suggest_attribute_const
1235 && !warn_suggest_attribute_pure
1238 l
= analyze_function (node
, false);
1240 switch (l
->pure_const_state
)
1243 if (!TREE_READONLY (current_function_decl
))
1245 warn_function_const (current_function_decl
, !l
->looping
);
1248 cgraph_set_readonly_flag (node
, true);
1249 cgraph_set_looping_const_or_pure_flag (node
, l
->looping
);
1253 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1254 l
->looping
? "looping " : "",
1255 lang_hooks
.decl_printable_name (current_function_decl
,
1258 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1263 cgraph_set_looping_const_or_pure_flag (node
, false);
1267 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1268 lang_hooks
.decl_printable_name (current_function_decl
,
1274 if (!DECL_PURE_P (current_function_decl
))
1278 cgraph_set_pure_flag (node
, true);
1279 cgraph_set_looping_const_or_pure_flag (node
, l
->looping
);
1282 warn_function_pure (current_function_decl
, !l
->looping
);
1284 fprintf (dump_file
, "Function found to be %spure: %s\n",
1285 l
->looping
? "looping " : "",
1286 lang_hooks
.decl_printable_name (current_function_decl
,
1289 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1294 cgraph_set_looping_const_or_pure_flag (node
, false);
1298 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1299 lang_hooks
.decl_printable_name (current_function_decl
,
1307 if (!l
->can_throw
&& !TREE_NOTHROW (current_function_decl
))
1309 struct cgraph_edge
*e
;
1311 cgraph_set_nothrow_flag (node
, true);
1312 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1313 e
->can_throw_external
= false;
1316 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1317 lang_hooks
.decl_printable_name (current_function_decl
,
1323 return execute_fixup_cfg ();
1328 struct gimple_opt_pass pass_local_pure_const
=
1332 "local-pure-const", /* name */
1333 gate_pure_const
, /* gate */
1334 local_pure_const
, /* execute */
1337 0, /* static_pass_number */
1338 TV_IPA_PURE_CONST
, /* tv_id */
1339 0, /* properties_required */
1340 0, /* properties_provided */
1341 0, /* properties_destroyed */
1342 0, /* todo_flags_start */
1343 0 /* todo_flags_finish */