1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2017 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"
41 #include "tree-pass.h"
42 #include "tree-streamer.h"
44 #include "diagnostic.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
51 #include "tree-ssa-loop-niter.h"
52 #include "langhooks.h"
53 #include "ipa-utils.h"
54 #include "gimple-pretty-print.h"
56 #include "tree-scalar-evolution.h"
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 const char *pure_const_names
[3] = {"const", "pure", "neither"};
72 /* Holder for the const_state. There is one of these per function
77 enum pure_const_state_e pure_const_state
;
78 /* What user set here; we can be always sure about this. */
79 enum pure_const_state_e state_previously_known
;
80 bool looping_previously_known
;
82 /* True if the function could possibly infinite loop. There are a
83 lot of ways that this could be determined. We are pretty
84 conservative here. While it is possible to cse pure and const
85 calls, it is not legal to have dce get rid of the call if there
86 is a possibility that the call could infinite loop since this is
87 a behavioral change. */
92 /* If function can call free, munmap or otherwise make previously
93 non-trapping memory accesses trapping. */
97 /* State used when we know nothing about function. */
98 static struct funct_state_d varying_state
99 = { IPA_NEITHER
, IPA_NEITHER
, true, true, true, true };
102 typedef struct funct_state_d
* funct_state
;
104 /* The storage of the funct_state is abstracted because there is the
105 possibility that it may be desirable to move this to the cgraph
108 /* Array, indexed by cgraph node uid, of function states. */
110 static vec
<funct_state
> funct_state_vec
;
112 static bool gate_pure_const (void);
116 const pass_data pass_data_ipa_pure_const
=
119 "pure-const", /* name */
120 OPTGROUP_NONE
, /* optinfo_flags */
121 TV_IPA_PURE_CONST
, /* tv_id */
122 0, /* properties_required */
123 0, /* properties_provided */
124 0, /* properties_destroyed */
125 0, /* todo_flags_start */
126 0, /* todo_flags_finish */
129 class pass_ipa_pure_const
: public ipa_opt_pass_d
132 pass_ipa_pure_const(gcc::context
*ctxt
);
134 /* opt_pass methods: */
135 bool gate (function
*) { return gate_pure_const (); }
136 unsigned int execute (function
*fun
);
138 void register_hooks (void);
143 /* Holders of ipa cgraph hooks: */
144 struct cgraph_node_hook_list
*function_insertion_hook_holder
;
145 struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
146 struct cgraph_node_hook_list
*node_removal_hook_holder
;
148 }; // class pass_ipa_pure_const
152 /* Try to guess if function body will always be visible to compiler
153 when compiling the call and whether compiler will be able
154 to propagate the information by itself. */
157 function_always_visible_to_compiler_p (tree decl
)
159 return (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
)
160 || DECL_COMDAT (decl
));
163 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
164 is true if the function is known to be finite. The diagnostic is
165 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
166 OPTION, this function may initialize it and it is always returned
169 static hash_set
<tree
> *
170 suggest_attribute (int option
, tree decl
, bool known_finite
,
171 hash_set
<tree
> *warned_about
,
172 const char * attrib_name
)
174 if (!option_enabled (option
, &global_options
))
176 if (TREE_THIS_VOLATILE (decl
)
177 || (known_finite
&& function_always_visible_to_compiler_p (decl
)))
181 warned_about
= new hash_set
<tree
>;
182 if (warned_about
->contains (decl
))
184 warned_about
->add (decl
);
185 warning_at (DECL_SOURCE_LOCATION (decl
),
188 ? G_("function might be candidate for attribute %qs")
189 : G_("function might be candidate for attribute %qs"
190 " if it is known to return normally"), attrib_name
);
194 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
195 is true if the function is known to be finite. */
198 warn_function_pure (tree decl
, bool known_finite
)
200 static hash_set
<tree
> *warned_about
;
203 = suggest_attribute (OPT_Wsuggest_attribute_pure
, decl
,
204 known_finite
, warned_about
, "pure");
207 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
208 is true if the function is known to be finite. */
211 warn_function_const (tree decl
, bool known_finite
)
213 static hash_set
<tree
> *warned_about
;
215 = suggest_attribute (OPT_Wsuggest_attribute_const
, decl
,
216 known_finite
, warned_about
, "const");
220 warn_function_noreturn (tree decl
)
222 tree original_decl
= decl
;
224 cgraph_node
*node
= cgraph_node::get (decl
);
225 if (node
->instrumentation_clone
)
226 decl
= node
->instrumented_version
->decl
;
228 static hash_set
<tree
> *warned_about
;
229 if (!lang_hooks
.missing_noreturn_ok_p (decl
)
230 && targetm
.warn_func_return (decl
))
232 = suggest_attribute (OPT_Wsuggest_attribute_noreturn
, original_decl
,
233 true, warned_about
, "noreturn");
237 warn_function_cold (tree decl
)
239 tree original_decl
= decl
;
241 cgraph_node
*node
= cgraph_node::get (decl
);
242 if (node
->instrumentation_clone
)
243 decl
= node
->instrumented_version
->decl
;
245 static hash_set
<tree
> *warned_about
;
247 = suggest_attribute (OPT_Wsuggest_attribute_cold
, original_decl
,
248 true, warned_about
, "cold");
251 /* Return true if we have a function state for NODE. */
254 has_function_state (struct cgraph_node
*node
)
256 if (!funct_state_vec
.exists ()
257 || funct_state_vec
.length () <= (unsigned int)node
->uid
)
259 return funct_state_vec
[node
->uid
] != NULL
;
262 /* Return the function state from NODE. */
264 static inline funct_state
265 get_function_state (struct cgraph_node
*node
)
267 if (!funct_state_vec
.exists ()
268 || funct_state_vec
.length () <= (unsigned int)node
->uid
269 || !funct_state_vec
[node
->uid
])
270 /* We might want to put correct previously_known state into varying. */
271 return &varying_state
;
272 return funct_state_vec
[node
->uid
];
275 /* Set the function state S for NODE. */
278 set_function_state (struct cgraph_node
*node
, funct_state s
)
280 if (!funct_state_vec
.exists ()
281 || funct_state_vec
.length () <= (unsigned int)node
->uid
)
282 funct_state_vec
.safe_grow_cleared (node
->uid
+ 1);
284 /* If funct_state_vec already contains a funct_state, we have to release
285 it before it's going to be ovewritten. */
286 if (funct_state_vec
[node
->uid
] != NULL
287 && funct_state_vec
[node
->uid
] != &varying_state
)
288 free (funct_state_vec
[node
->uid
]);
290 funct_state_vec
[node
->uid
] = s
;
293 /* Check to see if the use (or definition when CHECKING_WRITE is true)
294 variable T is legal in a function that is either pure or const. */
297 check_decl (funct_state local
,
298 tree t
, bool checking_write
, bool ipa
)
300 /* Do not want to do anything with volatile except mark any
301 function that uses one to be not const or pure. */
302 if (TREE_THIS_VOLATILE (t
))
304 local
->pure_const_state
= IPA_NEITHER
;
306 fprintf (dump_file
, " Volatile operand is not const/pure");
310 /* Do not care about a local automatic that is not static. */
311 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
314 /* If the variable has the "used" attribute, treat it as if it had a
315 been touched by the devil. */
316 if (DECL_PRESERVE_P (t
))
318 local
->pure_const_state
= IPA_NEITHER
;
320 fprintf (dump_file
, " Used static/global variable is not const/pure\n");
324 /* In IPA mode we are not interested in checking actual loads and stores;
325 they will be processed at propagation time using ipa_ref. */
329 /* Since we have dealt with the locals and params cases above, if we
330 are CHECKING_WRITE, this cannot be a pure or constant
334 local
->pure_const_state
= IPA_NEITHER
;
336 fprintf (dump_file
, " static/global memory write is not const/pure\n");
340 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
342 /* Readonly reads are safe. */
343 if (TREE_READONLY (t
) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t
)))
344 return; /* Read of a constant, do not change the function state. */
348 fprintf (dump_file
, " global memory read is not const\n");
349 /* Just a regular read. */
350 if (local
->pure_const_state
== IPA_CONST
)
351 local
->pure_const_state
= IPA_PURE
;
356 /* Compilation level statics can be read if they are readonly
358 if (TREE_READONLY (t
))
362 fprintf (dump_file
, " static memory read is not const\n");
363 /* Just a regular read. */
364 if (local
->pure_const_state
== IPA_CONST
)
365 local
->pure_const_state
= IPA_PURE
;
370 /* Check to see if the use (or definition when CHECKING_WRITE is true)
371 variable T is legal in a function that is either pure or const. */
374 check_op (funct_state local
, tree t
, bool checking_write
)
376 t
= get_base_address (t
);
377 if (t
&& TREE_THIS_VOLATILE (t
))
379 local
->pure_const_state
= IPA_NEITHER
;
381 fprintf (dump_file
, " Volatile indirect ref is not const/pure\n");
385 && (INDIRECT_REF_P (t
) || TREE_CODE (t
) == MEM_REF
)
386 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
387 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t
, 0)))
390 fprintf (dump_file
, " Indirect ref to local memory is OK\n");
393 else if (checking_write
)
395 local
->pure_const_state
= IPA_NEITHER
;
397 fprintf (dump_file
, " Indirect ref write is not const/pure\n");
403 fprintf (dump_file
, " Indirect ref read is not const\n");
404 if (local
->pure_const_state
== IPA_CONST
)
405 local
->pure_const_state
= IPA_PURE
;
409 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
412 state_from_flags (enum pure_const_state_e
*state
, bool *looping
,
413 int flags
, bool cannot_lead_to_return
)
416 if (flags
& ECF_LOOPING_CONST_OR_PURE
)
419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
420 fprintf (dump_file
, " looping");
422 if (flags
& ECF_CONST
)
425 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
426 fprintf (dump_file
, " const\n");
428 else if (flags
& ECF_PURE
)
431 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
432 fprintf (dump_file
, " pure\n");
434 else if (cannot_lead_to_return
)
438 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
439 fprintf (dump_file
, " ignoring side effects->pure looping\n");
443 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
444 fprintf (dump_file
, " neither\n");
445 *state
= IPA_NEITHER
;
450 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
451 into STATE and LOOPING better of the two variants.
452 Be sure to merge looping correctly. IPA_NEITHER functions
453 have looping 0 even if they don't have to return. */
456 better_state (enum pure_const_state_e
*state
, bool *looping
,
457 enum pure_const_state_e state2
, bool looping2
)
461 if (*state
== IPA_NEITHER
)
464 *looping
= MIN (*looping
, looping2
);
467 else if (state2
!= IPA_NEITHER
)
468 *looping
= MIN (*looping
, looping2
);
471 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
472 into STATE and LOOPING worse of the two variants.
473 N is the actual node called. */
476 worse_state (enum pure_const_state_e
*state
, bool *looping
,
477 enum pure_const_state_e state2
, bool looping2
,
478 struct symtab_node
*from
,
479 struct symtab_node
*to
)
481 /* Consider function:
488 During early optimization we will turn this into:
495 Now if this function will be detected as CONST however when interposed it
496 may end up being just pure. We always must assume the worst scenario here.
498 if (*state
== IPA_CONST
&& state2
== IPA_CONST
499 && to
&& !TREE_READONLY (to
->decl
) && !to
->binds_to_current_def_p (from
))
501 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
502 fprintf (dump_file
, "Dropping state to PURE because call to %s may not "
503 "bind to current def.\n", to
->name ());
506 *state
= MAX (*state
, state2
);
507 *looping
= MAX (*looping
, looping2
);
510 /* Recognize special cases of builtins that are by themselves not pure or const
511 but function using them is. */
513 special_builtin_state (enum pure_const_state_e
*state
, bool *looping
,
516 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
517 switch (DECL_FUNCTION_CODE (callee
))
519 case BUILT_IN_RETURN
:
520 case BUILT_IN_UNREACHABLE
:
521 case BUILT_IN_ALLOCA
:
522 case BUILT_IN_ALLOCA_WITH_ALIGN
:
523 case BUILT_IN_STACK_SAVE
:
524 case BUILT_IN_STACK_RESTORE
:
525 case BUILT_IN_EH_POINTER
:
526 case BUILT_IN_EH_FILTER
:
527 case BUILT_IN_UNWIND_RESUME
:
528 case BUILT_IN_CXA_END_CLEANUP
:
529 case BUILT_IN_EH_COPY_VALUES
:
530 case BUILT_IN_FRAME_ADDRESS
:
532 case BUILT_IN_APPLY_ARGS
:
533 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT
:
534 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT
:
538 case BUILT_IN_PREFETCH
:
548 /* Check the parameters of a function call to CALL_EXPR to see if
549 there are any references in the parameters that are not allowed for
550 pure or const functions. Also check to see if this is either an
551 indirect call, a call outside the compilation unit, or has special
552 attributes that may also effect the purity. The CALL_EXPR node for
553 the entire call expression. */
556 check_call (funct_state local
, gcall
*call
, bool ipa
)
558 int flags
= gimple_call_flags (call
);
559 tree callee_t
= gimple_call_fndecl (call
);
560 bool possibly_throws
= stmt_could_throw_p (call
);
561 bool possibly_throws_externally
= (possibly_throws
562 && stmt_can_throw_external (call
));
567 for (i
= 0; i
< gimple_num_ops (call
); i
++)
568 if (gimple_op (call
, i
)
569 && tree_could_throw_p (gimple_op (call
, i
)))
571 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
574 fprintf (dump_file
, " operand can throw; looping\n");
575 local
->looping
= true;
577 if (possibly_throws_externally
)
580 fprintf (dump_file
, " operand can throw externally\n");
581 local
->can_throw
= true;
586 /* The const and pure flags are set by a variety of places in the
587 compiler (including here). If someone has already set the flags
588 for the callee, (such as for some of the builtins) we will use
589 them, otherwise we will compute our own information.
591 Const and pure functions have less clobber effects than other
592 functions so we process these first. Otherwise if it is a call
593 outside the compilation unit or an indirect call we punt. This
594 leaves local calls which will be processed by following the call
598 enum pure_const_state_e call_state
;
601 if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
)
602 && !nonfreeing_call_p (call
))
603 local
->can_free
= true;
605 if (special_builtin_state (&call_state
, &call_looping
, callee_t
))
607 worse_state (&local
->pure_const_state
, &local
->looping
,
608 call_state
, call_looping
,
612 /* When bad things happen to bad functions, they cannot be const
614 if (setjmp_call_p (callee_t
))
617 fprintf (dump_file
, " setjmp is not const/pure\n");
618 local
->looping
= true;
619 local
->pure_const_state
= IPA_NEITHER
;
622 if (DECL_BUILT_IN_CLASS (callee_t
) == BUILT_IN_NORMAL
)
623 switch (DECL_FUNCTION_CODE (callee_t
))
625 case BUILT_IN_LONGJMP
:
626 case BUILT_IN_NONLOCAL_GOTO
:
628 fprintf (dump_file
, " longjmp and nonlocal goto is not const/pure\n");
629 local
->pure_const_state
= IPA_NEITHER
;
630 local
->looping
= true;
636 else if (gimple_call_internal_p (call
) && !nonfreeing_call_p (call
))
637 local
->can_free
= true;
639 /* When not in IPA mode, we can still handle self recursion. */
641 && recursive_call_p (current_function_decl
, callee_t
))
644 fprintf (dump_file
, " Recursive call can loop.\n");
645 local
->looping
= true;
647 /* Either callee is unknown or we are doing local analysis.
648 Look to see if there are any bits available for the callee (such as by
649 declaration or because it is builtin) and process solely on the basis of
650 those bits. Handle internal calls always, those calls don't have
651 corresponding cgraph edges and thus aren't processed during
653 else if (!ipa
|| gimple_call_internal_p (call
))
655 enum pure_const_state_e call_state
;
657 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
660 fprintf (dump_file
, " can throw; looping\n");
661 local
->looping
= true;
663 if (possibly_throws_externally
)
667 fprintf (dump_file
, " can throw externally to lp %i\n",
668 lookup_stmt_eh_lp (call
));
670 fprintf (dump_file
, " callee:%s\n",
671 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t
)));
673 local
->can_throw
= true;
675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
676 fprintf (dump_file
, " checking flags for call:");
677 state_from_flags (&call_state
, &call_looping
, flags
,
678 ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
679 == (ECF_NORETURN
| ECF_NOTHROW
))
680 || (!flag_exceptions
&& (flags
& ECF_NORETURN
)));
681 worse_state (&local
->pure_const_state
, &local
->looping
,
682 call_state
, call_looping
, NULL
, NULL
);
684 /* Direct functions calls are handled by IPA propagation. */
687 /* Wrapper around check_decl for loads in local more. */
690 check_load (gimple
*, tree op
, tree
, void *data
)
693 check_decl ((funct_state
)data
, op
, false, false);
695 check_op ((funct_state
)data
, op
, false);
699 /* Wrapper around check_decl for stores in local more. */
702 check_store (gimple
*, tree op
, tree
, void *data
)
705 check_decl ((funct_state
)data
, op
, true, false);
707 check_op ((funct_state
)data
, op
, true);
711 /* Wrapper around check_decl for loads in ipa mode. */
714 check_ipa_load (gimple
*, tree op
, tree
, void *data
)
717 check_decl ((funct_state
)data
, op
, false, true);
719 check_op ((funct_state
)data
, op
, false);
723 /* Wrapper around check_decl for stores in ipa mode. */
726 check_ipa_store (gimple
*, tree op
, tree
, void *data
)
729 check_decl ((funct_state
)data
, op
, true, true);
731 check_op ((funct_state
)data
, op
, true);
735 /* Look into pointer pointed to by GSIP and figure out what interesting side
738 check_stmt (gimple_stmt_iterator
*gsip
, funct_state local
, bool ipa
)
740 gimple
*stmt
= gsi_stmt (*gsip
);
742 if (is_gimple_debug (stmt
))
745 /* Do consider clobber as side effects before IPA, so we rather inline
746 C++ destructors and keep clobber semantics than eliminate them.
748 TODO: We may get smarter during early optimizations on these and let
749 functions containing only clobbers to be optimized more. This is a common
750 case of C++ destructors. */
752 if ((ipa
|| cfun
->after_inlining
) && gimple_clobber_p (stmt
))
757 fprintf (dump_file
, " scanning: ");
758 print_gimple_stmt (dump_file
, stmt
, 0);
761 if (gimple_has_volatile_ops (stmt
)
762 && !gimple_clobber_p (stmt
))
764 local
->pure_const_state
= IPA_NEITHER
;
766 fprintf (dump_file
, " Volatile stmt is not const/pure\n");
769 /* Look for loads and stores. */
770 walk_stmt_load_store_ops (stmt
, local
,
771 ipa
? check_ipa_load
: check_load
,
772 ipa
? check_ipa_store
: check_store
);
774 if (gimple_code (stmt
) != GIMPLE_CALL
775 && stmt_could_throw_p (stmt
))
777 if (cfun
->can_throw_non_call_exceptions
)
780 fprintf (dump_file
, " can throw; looping\n");
781 local
->looping
= true;
783 if (stmt_can_throw_external (stmt
))
786 fprintf (dump_file
, " can throw externally\n");
787 local
->can_throw
= true;
791 fprintf (dump_file
, " can throw\n");
793 switch (gimple_code (stmt
))
796 check_call (local
, as_a
<gcall
*> (stmt
), ipa
);
799 if (DECL_NONLOCAL (gimple_label_label (as_a
<glabel
*> (stmt
))))
800 /* Target of long jump. */
803 fprintf (dump_file
, " nonlocal label is not const/pure\n");
804 local
->pure_const_state
= IPA_NEITHER
;
808 if (gimple_asm_clobbers_memory_p (as_a
<gasm
*> (stmt
)))
811 fprintf (dump_file
, " memory asm clobber is not const/pure\n");
812 /* Abandon all hope, ye who enter here. */
813 local
->pure_const_state
= IPA_NEITHER
;
814 local
->can_free
= true;
816 if (gimple_asm_volatile_p (as_a
<gasm
*> (stmt
)))
819 fprintf (dump_file
, " volatile is not const/pure\n");
820 /* Abandon all hope, ye who enter here. */
821 local
->pure_const_state
= IPA_NEITHER
;
822 local
->looping
= true;
823 local
->can_free
= true;
832 /* This is the main routine for finding the reference patterns for
833 global variables within a function FN. */
836 analyze_function (struct cgraph_node
*fn
, bool ipa
)
838 tree decl
= fn
->decl
;
840 basic_block this_block
;
842 l
= XCNEW (struct funct_state_d
);
843 l
->pure_const_state
= IPA_CONST
;
844 l
->state_previously_known
= IPA_NEITHER
;
845 l
->looping_previously_known
= true;
847 l
->can_throw
= false;
849 state_from_flags (&l
->state_previously_known
, &l
->looping_previously_known
,
850 flags_from_decl_or_type (fn
->decl
),
851 fn
->cannot_return_p ());
853 if (fn
->thunk
.thunk_p
|| fn
->alias
)
855 /* Thunk gets propagated through, so nothing interesting happens. */
857 if (fn
->thunk
.thunk_p
&& fn
->thunk
.virtual_offset_p
)
858 l
->pure_const_state
= IPA_NEITHER
;
864 fprintf (dump_file
, "\n\n local analysis of %s\n ",
868 push_cfun (DECL_STRUCT_FUNCTION (decl
));
870 FOR_EACH_BB_FN (this_block
, cfun
)
872 gimple_stmt_iterator gsi
;
873 struct walk_stmt_info wi
;
875 memset (&wi
, 0, sizeof (wi
));
876 for (gsi
= gsi_start_bb (this_block
);
880 check_stmt (&gsi
, l
, ipa
);
881 if (l
->pure_const_state
== IPA_NEITHER
890 if (l
->pure_const_state
!= IPA_NEITHER
)
892 /* Const functions cannot have back edges (an
893 indication of possible infinite loop side
895 if (mark_dfs_back_edges ())
897 /* Preheaders are needed for SCEV to work.
898 Simple latches and recorded exits improve chances that loop will
899 proved to be finite in testcases such as in loop-15.c
901 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
902 | LOOPS_HAVE_SIMPLE_LATCHES
903 | LOOPS_HAVE_RECORDED_EXITS
);
904 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
905 flow_loops_dump (dump_file
, NULL
, 0);
906 if (mark_irreducible_loops ())
909 fprintf (dump_file
, " has irreducible loops\n");
916 FOR_EACH_LOOP (loop
, 0)
917 if (!finite_loop_p (loop
))
920 fprintf (dump_file
, " can not prove finiteness of "
921 "loop %i\n", loop
->num
);
927 loop_optimizer_finalize ();
931 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
932 fprintf (dump_file
, " checking previously known:");
934 better_state (&l
->pure_const_state
, &l
->looping
,
935 l
->state_previously_known
,
936 l
->looping_previously_known
);
937 if (TREE_NOTHROW (decl
))
938 l
->can_throw
= false;
944 fprintf (dump_file
, "Function is locally looping.\n");
946 fprintf (dump_file
, "Function is locally throwing.\n");
947 if (l
->pure_const_state
== IPA_CONST
)
948 fprintf (dump_file
, "Function is locally const.\n");
949 if (l
->pure_const_state
== IPA_PURE
)
950 fprintf (dump_file
, "Function is locally pure.\n");
952 fprintf (dump_file
, "Function can locally free.\n");
957 /* Called when new function is inserted to callgraph late. */
959 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
961 /* There are some shared nodes, in particular the initializers on
962 static declarations. We do not need to scan them more than once
963 since all we would be interested in are the addressof
965 if (opt_for_fn (node
->decl
, flag_ipa_pure_const
))
966 set_function_state (node
, analyze_function (node
, true));
969 /* Called when new clone is inserted to callgraph late. */
972 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
973 void *data ATTRIBUTE_UNUSED
)
975 if (has_function_state (src
))
977 funct_state l
= XNEW (struct funct_state_d
);
978 gcc_assert (!has_function_state (dst
));
979 memcpy (l
, get_function_state (src
), sizeof (*l
));
980 set_function_state (dst
, l
);
984 /* Called when new clone is inserted to callgraph late. */
987 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
989 if (has_function_state (node
))
990 set_function_state (node
, NULL
);
995 pass_ipa_pure_const::
996 register_hooks (void)
1003 node_removal_hook_holder
=
1004 symtab
->add_cgraph_removal_hook (&remove_node_data
, NULL
);
1005 node_duplication_hook_holder
=
1006 symtab
->add_cgraph_duplication_hook (&duplicate_node_data
, NULL
);
1007 function_insertion_hook_holder
=
1008 symtab
->add_cgraph_insertion_hook (&add_new_function
, NULL
);
1012 /* Analyze each function in the cgraph to see if it is locally PURE or
1016 pure_const_generate_summary (void)
1018 struct cgraph_node
*node
;
1020 pass_ipa_pure_const
*pass
= static_cast <pass_ipa_pure_const
*> (current_pass
);
1021 pass
->register_hooks ();
1023 /* Process all of the functions.
1025 We process AVAIL_INTERPOSABLE functions. We can not use the results
1026 by default, but the info can be used at LTO with -fwhole-program or
1027 when function got cloned and the clone is AVAILABLE. */
1029 FOR_EACH_DEFINED_FUNCTION (node
)
1030 if (opt_for_fn (node
->decl
, flag_ipa_pure_const
))
1031 set_function_state (node
, analyze_function (node
, true));
1035 /* Serialize the ipa info for lto. */
1038 pure_const_write_summary (void)
1040 struct cgraph_node
*node
;
1041 struct lto_simple_output_block
*ob
1042 = lto_create_simple_output_block (LTO_section_ipa_pure_const
);
1043 unsigned int count
= 0;
1044 lto_symtab_encoder_iterator lsei
;
1045 lto_symtab_encoder_t encoder
;
1047 encoder
= lto_get_out_decl_state ()->symtab_node_encoder
;
1049 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
1050 lsei_next_function_in_partition (&lsei
))
1052 node
= lsei_cgraph_node (lsei
);
1053 if (node
->definition
&& has_function_state (node
))
1057 streamer_write_uhwi_stream (ob
->main_stream
, count
);
1059 /* Process all of the functions. */
1060 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
1061 lsei_next_function_in_partition (&lsei
))
1063 node
= lsei_cgraph_node (lsei
);
1064 if (node
->definition
&& has_function_state (node
))
1066 struct bitpack_d bp
;
1069 lto_symtab_encoder_t encoder
;
1071 fs
= get_function_state (node
);
1073 encoder
= ob
->decl_state
->symtab_node_encoder
;
1074 node_ref
= lto_symtab_encoder_encode (encoder
, node
);
1075 streamer_write_uhwi_stream (ob
->main_stream
, node_ref
);
1077 /* Note that flags will need to be read in the opposite
1078 order as we are pushing the bitflags into FLAGS. */
1079 bp
= bitpack_create (ob
->main_stream
);
1080 bp_pack_value (&bp
, fs
->pure_const_state
, 2);
1081 bp_pack_value (&bp
, fs
->state_previously_known
, 2);
1082 bp_pack_value (&bp
, fs
->looping_previously_known
, 1);
1083 bp_pack_value (&bp
, fs
->looping
, 1);
1084 bp_pack_value (&bp
, fs
->can_throw
, 1);
1085 bp_pack_value (&bp
, fs
->can_free
, 1);
1086 streamer_write_bitpack (&bp
);
1090 lto_destroy_simple_output_block (ob
);
1094 /* Deserialize the ipa info for lto. */
1097 pure_const_read_summary (void)
1099 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1100 struct lto_file_decl_data
*file_data
;
1103 pass_ipa_pure_const
*pass
= static_cast <pass_ipa_pure_const
*> (current_pass
);
1104 pass
->register_hooks ();
1106 while ((file_data
= file_data_vec
[j
++]))
1110 struct lto_input_block
*ib
1111 = lto_create_simple_input_block (file_data
,
1112 LTO_section_ipa_pure_const
,
1117 unsigned int count
= streamer_read_uhwi (ib
);
1119 for (i
= 0; i
< count
; i
++)
1122 struct cgraph_node
*node
;
1123 struct bitpack_d bp
;
1125 lto_symtab_encoder_t encoder
;
1127 fs
= XCNEW (struct funct_state_d
);
1128 index
= streamer_read_uhwi (ib
);
1129 encoder
= file_data
->symtab_node_encoder
;
1130 node
= dyn_cast
<cgraph_node
*> (lto_symtab_encoder_deref (encoder
,
1132 set_function_state (node
, fs
);
1134 /* Note that the flags must be read in the opposite
1135 order in which they were written (the bitflags were
1136 pushed into FLAGS). */
1137 bp
= streamer_read_bitpack (ib
);
1138 fs
->pure_const_state
1139 = (enum pure_const_state_e
) bp_unpack_value (&bp
, 2);
1140 fs
->state_previously_known
1141 = (enum pure_const_state_e
) bp_unpack_value (&bp
, 2);
1142 fs
->looping_previously_known
= bp_unpack_value (&bp
, 1);
1143 fs
->looping
= bp_unpack_value (&bp
, 1);
1144 fs
->can_throw
= bp_unpack_value (&bp
, 1);
1145 fs
->can_free
= bp_unpack_value (&bp
, 1);
1148 int flags
= flags_from_decl_or_type (node
->decl
);
1149 fprintf (dump_file
, "Read info for %s ", node
->dump_name ());
1150 if (flags
& ECF_CONST
)
1151 fprintf (dump_file
, " const");
1152 if (flags
& ECF_PURE
)
1153 fprintf (dump_file
, " pure");
1154 if (flags
& ECF_NOTHROW
)
1155 fprintf (dump_file
, " nothrow");
1156 fprintf (dump_file
, "\n pure const state: %s\n",
1157 pure_const_names
[fs
->pure_const_state
]);
1158 fprintf (dump_file
, " previously known state: %s\n",
1159 pure_const_names
[fs
->state_previously_known
]);
1161 fprintf (dump_file
," function is locally looping\n");
1162 if (fs
->looping_previously_known
)
1163 fprintf (dump_file
," function is previously known looping\n");
1165 fprintf (dump_file
," function is locally throwing\n");
1167 fprintf (dump_file
," function can locally free\n");
1171 lto_destroy_simple_input_block (file_data
,
1172 LTO_section_ipa_pure_const
,
1178 /* We only propagate across edges that can throw externally and their callee
1179 is not interposable. */
1182 ignore_edge_for_nothrow (struct cgraph_edge
*e
)
1184 if (!e
->can_throw_external
|| TREE_NOTHROW (e
->callee
->decl
))
1187 enum availability avail
;
1188 cgraph_node
*n
= e
->callee
->function_or_virtual_thunk_symbol (&avail
,
1190 if (avail
<= AVAIL_INTERPOSABLE
|| TREE_NOTHROW (n
->decl
))
1192 return opt_for_fn (e
->callee
->decl
, flag_non_call_exceptions
)
1193 && !e
->callee
->binds_to_current_def_p (e
->caller
);
1196 /* Return true if NODE is self recursive function.
1197 Indirectly recursive functions appears as non-trivial strongly
1198 connected components, so we need to care about self recursion
1202 self_recursive_p (struct cgraph_node
*node
)
1204 struct cgraph_edge
*e
;
1205 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1206 if (e
->callee
->function_symbol () == node
)
1211 /* Return true if N is cdtor that is not const or pure. In this case we may
1212 need to remove unreachable function if it is marked const/pure. */
1215 cdtor_p (cgraph_node
*n
, void *)
1217 if (DECL_STATIC_CONSTRUCTOR (n
->decl
) || DECL_STATIC_DESTRUCTOR (n
->decl
))
1218 return ((!TREE_READONLY (n
->decl
) && !DECL_PURE_P (n
->decl
))
1219 || DECL_LOOPING_CONST_OR_PURE_P (n
->decl
));
1223 /* We only propagate across edges with non-interposable callee. */
1226 ignore_edge_for_pure_const (struct cgraph_edge
*e
)
1228 enum availability avail
;
1229 e
->callee
->function_or_virtual_thunk_symbol (&avail
, e
->caller
);
1230 return (avail
<= AVAIL_INTERPOSABLE
);
1234 /* Produce transitive closure over the callgraph and compute pure/const
1238 propagate_pure_const (void)
1240 struct cgraph_node
*node
;
1241 struct cgraph_node
*w
;
1242 struct cgraph_node
**order
=
1243 XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
1246 struct ipa_dfs_info
* w_info
;
1247 bool remove_p
= false;
1250 order_pos
= ipa_reduced_postorder (order
, true, false,
1251 ignore_edge_for_pure_const
);
1254 cgraph_node::dump_cgraph (dump_file
);
1255 ipa_print_order (dump_file
, "reduced", order
, order_pos
);
1258 /* Propagate the local information through the call graph to produce
1259 the global information. All the nodes within a cycle will have
1260 the same info so we collapse cycles first. Then we can do the
1261 propagation in one pass from the leaves to the roots. */
1262 for (i
= 0; i
< order_pos
; i
++ )
1264 enum pure_const_state_e pure_const_state
= IPA_CONST
;
1265 bool looping
= false;
1272 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1273 fprintf (dump_file
, "Starting cycle\n");
1275 /* Find the worst state for any node in the cycle. */
1277 while (w
&& pure_const_state
!= IPA_NEITHER
)
1279 struct cgraph_edge
*e
;
1280 struct cgraph_edge
*ie
;
1282 struct ipa_ref
*ref
= NULL
;
1284 funct_state w_l
= get_function_state (w
);
1285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1286 fprintf (dump_file
, " Visiting %s state:%s looping %i\n",
1288 pure_const_names
[w_l
->pure_const_state
],
1291 /* First merge in function body properties.
1292 We are safe to pass NULL as FROM and TO because we will take care
1293 of possible interposition when walking callees. */
1294 worse_state (&pure_const_state
, &looping
,
1295 w_l
->pure_const_state
, w_l
->looping
,
1297 if (pure_const_state
== IPA_NEITHER
)
1302 /* We consider recursive cycles as possibly infinite.
1303 This might be relaxed since infinite recursion leads to stack
1308 /* Now walk the edges and merge in callee properties. */
1309 for (e
= w
->callees
; e
&& pure_const_state
!= IPA_NEITHER
;
1312 enum availability avail
;
1313 struct cgraph_node
*y
= e
->callee
->
1314 function_or_virtual_thunk_symbol (&avail
,
1316 enum pure_const_state_e edge_state
= IPA_CONST
;
1317 bool edge_looping
= false;
1319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1321 fprintf (dump_file
, " Call to %s",
1322 e
->callee
->dump_name ());
1324 if (avail
> AVAIL_INTERPOSABLE
)
1326 funct_state y_l
= get_function_state (y
);
1327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1330 " state:%s looping:%i\n",
1331 pure_const_names
[y_l
->pure_const_state
],
1334 if (y_l
->pure_const_state
> IPA_PURE
1335 && e
->cannot_lead_to_return_p ())
1337 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1339 " Ignoring side effects"
1340 " -> pure, looping\n");
1341 edge_state
= IPA_PURE
;
1342 edge_looping
= true;
1346 edge_state
= y_l
->pure_const_state
;
1347 edge_looping
= y_l
->looping
;
1350 else if (special_builtin_state (&edge_state
, &edge_looping
,
1354 state_from_flags (&edge_state
, &edge_looping
,
1355 flags_from_decl_or_type (y
->decl
),
1356 e
->cannot_lead_to_return_p ());
1358 /* Merge the results with what we already know. */
1359 better_state (&edge_state
, &edge_looping
,
1360 w_l
->state_previously_known
,
1361 w_l
->looping_previously_known
);
1362 worse_state (&pure_const_state
, &looping
,
1363 edge_state
, edge_looping
, e
->caller
, e
->callee
);
1364 if (pure_const_state
== IPA_NEITHER
)
1368 /* Now process the indirect call. */
1369 for (ie
= w
->indirect_calls
;
1370 ie
&& pure_const_state
!= IPA_NEITHER
; ie
= ie
->next_callee
)
1372 enum pure_const_state_e edge_state
= IPA_CONST
;
1373 bool edge_looping
= false;
1375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1376 fprintf (dump_file
, " Indirect call");
1377 state_from_flags (&edge_state
, &edge_looping
,
1378 ie
->indirect_info
->ecf_flags
,
1379 ie
->cannot_lead_to_return_p ());
1380 /* Merge the results with what we already know. */
1381 better_state (&edge_state
, &edge_looping
,
1382 w_l
->state_previously_known
,
1383 w_l
->looping_previously_known
);
1384 worse_state (&pure_const_state
, &looping
,
1385 edge_state
, edge_looping
, NULL
, NULL
);
1386 if (pure_const_state
== IPA_NEITHER
)
1390 /* And finally all loads and stores. */
1391 for (i
= 0; w
->iterate_reference (i
, ref
)
1392 && pure_const_state
!= IPA_NEITHER
; i
++)
1394 enum pure_const_state_e ref_state
= IPA_CONST
;
1395 bool ref_looping
= false;
1399 /* readonly reads are safe. */
1400 if (TREE_READONLY (ref
->referred
->decl
))
1402 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1403 fprintf (dump_file
, " nonreadonly global var read\n");
1404 ref_state
= IPA_PURE
;
1407 if (ref
->cannot_lead_to_return ())
1409 ref_state
= IPA_NEITHER
;
1410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1411 fprintf (dump_file
, " global var write\n");
1419 better_state (&ref_state
, &ref_looping
,
1420 w_l
->state_previously_known
,
1421 w_l
->looping_previously_known
);
1422 worse_state (&pure_const_state
, &looping
,
1423 ref_state
, ref_looping
, NULL
, NULL
);
1424 if (pure_const_state
== IPA_NEITHER
)
1427 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1428 w
= w_info
->next_cycle
;
1430 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1431 fprintf (dump_file
, "Result %s looping %i\n",
1432 pure_const_names
[pure_const_state
],
1435 /* Find the worst state of can_free for any node in the cycle. */
1436 bool can_free
= false;
1438 while (w
&& !can_free
)
1440 struct cgraph_edge
*e
;
1441 funct_state w_l
= get_function_state (w
);
1444 || w
->get_availability () == AVAIL_INTERPOSABLE
1445 || w
->indirect_calls
)
1448 for (e
= w
->callees
; e
&& !can_free
; e
= e
->next_callee
)
1450 enum availability avail
;
1451 struct cgraph_node
*y
= e
->callee
->
1452 function_or_virtual_thunk_symbol (&avail
,
1455 if (avail
> AVAIL_INTERPOSABLE
)
1456 can_free
= get_function_state (y
)->can_free
;
1460 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1461 w
= w_info
->next_cycle
;
1464 /* Copy back the region's pure_const_state which is shared by
1465 all nodes in the region. */
1469 funct_state w_l
= get_function_state (w
);
1470 enum pure_const_state_e this_state
= pure_const_state
;
1471 bool this_looping
= looping
;
1473 w_l
->can_free
= can_free
;
1474 w
->nonfreeing_fn
= !can_free
;
1475 if (!can_free
&& dump_file
)
1476 fprintf (dump_file
, "Function found not to call free: %s\n",
1479 if (w_l
->state_previously_known
!= IPA_NEITHER
1480 && this_state
> w_l
->state_previously_known
)
1482 this_state
= w_l
->state_previously_known
;
1483 if (this_state
== IPA_NEITHER
)
1484 this_looping
= w_l
->looping_previously_known
;
1486 if (!this_looping
&& self_recursive_p (w
))
1487 this_looping
= true;
1488 if (!w_l
->looping_previously_known
)
1489 this_looping
= false;
1491 /* All nodes within a cycle share the same info. */
1492 w_l
->pure_const_state
= this_state
;
1493 w_l
->looping
= this_looping
;
1495 /* Inline clones share declaration with their offline copies;
1496 do not modify their declarations since the offline copy may
1498 if (!w
->global
.inlined_to
)
1502 if (!TREE_READONLY (w
->decl
))
1504 warn_function_const (w
->decl
, !this_looping
);
1506 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1507 this_looping
? "looping " : "",
1510 /* Turning constructor or destructor to non-looping const/pure
1511 enables us to possibly remove the function completely. */
1515 has_cdtor
= w
->call_for_symbol_and_aliases (cdtor_p
,
1517 if (w
->set_const_flag (true, this_looping
))
1521 "Declaration updated to be %sconst: %s\n",
1522 this_looping
? "looping " : "",
1524 remove_p
|= has_cdtor
;
1529 if (!DECL_PURE_P (w
->decl
))
1531 warn_function_pure (w
->decl
, !this_looping
);
1533 fprintf (dump_file
, "Function found to be %spure: %s\n",
1534 this_looping
? "looping " : "",
1540 has_cdtor
= w
->call_for_symbol_and_aliases (cdtor_p
,
1542 if (w
->set_pure_flag (true, this_looping
))
1546 "Declaration updated to be %spure: %s\n",
1547 this_looping
? "looping " : "",
1549 remove_p
|= has_cdtor
;
1556 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1557 w
= w_info
->next_cycle
;
1561 ipa_free_postorder_info ();
1566 /* Produce transitive closure over the callgraph and compute nothrow
1570 propagate_nothrow (void)
1572 struct cgraph_node
*node
;
1573 struct cgraph_node
*w
;
1574 struct cgraph_node
**order
=
1575 XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
1578 struct ipa_dfs_info
* w_info
;
1580 order_pos
= ipa_reduced_postorder (order
, true, false,
1581 ignore_edge_for_nothrow
);
1584 cgraph_node::dump_cgraph (dump_file
);
1585 ipa_print_order (dump_file
, "reduced for nothrow", order
, order_pos
);
1588 /* Propagate the local information through the call graph to produce
1589 the global information. All the nodes within a cycle will have
1590 the same info so we collapse cycles first. Then we can do the
1591 propagation in one pass from the leaves to the roots. */
1592 for (i
= 0; i
< order_pos
; i
++ )
1594 bool can_throw
= false;
1600 /* Find the worst state for any node in the cycle. */
1602 while (w
&& !can_throw
)
1604 struct cgraph_edge
*e
, *ie
;
1606 if (!TREE_NOTHROW (w
->decl
))
1608 funct_state w_l
= get_function_state (w
);
1611 || w
->get_availability () == AVAIL_INTERPOSABLE
)
1614 for (e
= w
->callees
; e
&& !can_throw
; e
= e
->next_callee
)
1616 enum availability avail
;
1618 if (!e
->can_throw_external
|| TREE_NOTHROW (e
->callee
->decl
))
1621 struct cgraph_node
*y
= e
->callee
->
1622 function_or_virtual_thunk_symbol (&avail
,
1625 /* We can use info about the callee only if we know it can
1627 When callee is compiled with non-call exceptions we also
1628 must check that the declaration is bound to current
1629 body as other semantically equivalent body may still
1631 if (avail
<= AVAIL_INTERPOSABLE
1632 || (!TREE_NOTHROW (y
->decl
)
1633 && (get_function_state (y
)->can_throw
1634 || (opt_for_fn (y
->decl
, flag_non_call_exceptions
)
1635 && !e
->callee
->binds_to_current_def_p (w
)))))
1638 for (ie
= w
->indirect_calls
; ie
&& !can_throw
;
1639 ie
= ie
->next_callee
)
1640 if (ie
->can_throw_external
1641 && !(ie
->indirect_info
->ecf_flags
& ECF_NOTHROW
))
1644 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1645 w
= w_info
->next_cycle
;
1648 /* Copy back the region's pure_const_state which is shared by
1649 all nodes in the region. */
1653 funct_state w_l
= get_function_state (w
);
1654 if (!can_throw
&& !TREE_NOTHROW (w
->decl
))
1656 /* Inline clones share declaration with their offline copies;
1657 do not modify their declarations since the offline copy may
1659 if (!w
->global
.inlined_to
)
1661 w
->set_nothrow_flag (true);
1663 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1667 else if (can_throw
&& !TREE_NOTHROW (w
->decl
))
1668 w_l
->can_throw
= true;
1669 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1670 w
= w_info
->next_cycle
;
1674 ipa_free_postorder_info ();
1679 /* Produce the global information by preforming a transitive closure
1680 on the local information that was produced by generate_summary. */
1683 pass_ipa_pure_const::
1684 execute (function
*)
1686 struct cgraph_node
*node
;
1689 symtab
->remove_cgraph_insertion_hook (function_insertion_hook_holder
);
1690 symtab
->remove_cgraph_duplication_hook (node_duplication_hook_holder
);
1691 symtab
->remove_cgraph_removal_hook (node_removal_hook_holder
);
1693 /* Nothrow makes more function to not lead to return and improve
1695 propagate_nothrow ();
1696 remove_p
= propagate_pure_const ();
1699 FOR_EACH_FUNCTION (node
)
1700 if (has_function_state (node
))
1701 free (get_function_state (node
));
1702 funct_state_vec
.release ();
1703 return remove_p
? TODO_remove_functions
: 0;
1707 gate_pure_const (void)
1709 return flag_ipa_pure_const
|| in_lto_p
;
1712 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context
*ctxt
)
1713 : ipa_opt_pass_d(pass_data_ipa_pure_const
, ctxt
,
1714 pure_const_generate_summary
, /* generate_summary */
1715 pure_const_write_summary
, /* write_summary */
1716 pure_const_read_summary
, /* read_summary */
1717 NULL
, /* write_optimization_summary */
1718 NULL
, /* read_optimization_summary */
1719 NULL
, /* stmt_fixup */
1720 0, /* function_transform_todo_flags_start */
1721 NULL
, /* function_transform */
1722 NULL
), /* variable_transform */
1724 function_insertion_hook_holder(NULL
),
1725 node_duplication_hook_holder(NULL
),
1726 node_removal_hook_holder(NULL
)
1731 make_pass_ipa_pure_const (gcc::context
*ctxt
)
1733 return new pass_ipa_pure_const (ctxt
);
1736 /* Return true if function should be skipped for local pure const analysis. */
1739 skip_function_for_local_pure_const (struct cgraph_node
*node
)
1741 /* Because we do not schedule pass_fixup_cfg over whole program after early
1742 optimizations we must not promote functions that are called by already
1743 processed functions. */
1745 if (function_called_by_processed_nodes_p ())
1748 fprintf (dump_file
, "Function called in recursive cycle; ignoring\n");
1751 /* Save some work and do not analyze functions which are interposable and
1752 do not have any non-interposable aliases. */
1753 if (node
->get_availability () <= AVAIL_INTERPOSABLE
1754 && !node
->has_aliases_p ())
1758 "Function is interposable; not analyzing.\n");
1764 /* Simple local pass for pure const discovery reusing the analysis from
1765 ipa_pure_const. This pass is effective when executed together with
1766 other optimization passes in early optimization pass queue. */
1770 const pass_data pass_data_local_pure_const
=
1772 GIMPLE_PASS
, /* type */
1773 "local-pure-const", /* name */
1774 OPTGROUP_NONE
, /* optinfo_flags */
1775 TV_IPA_PURE_CONST
, /* tv_id */
1776 0, /* properties_required */
1777 0, /* properties_provided */
1778 0, /* properties_destroyed */
1779 0, /* todo_flags_start */
1780 0, /* todo_flags_finish */
1783 class pass_local_pure_const
: public gimple_opt_pass
1786 pass_local_pure_const (gcc::context
*ctxt
)
1787 : gimple_opt_pass (pass_data_local_pure_const
, ctxt
)
1790 /* opt_pass methods: */
1791 opt_pass
* clone () { return new pass_local_pure_const (m_ctxt
); }
1792 virtual bool gate (function
*) { return gate_pure_const (); }
1793 virtual unsigned int execute (function
*);
1795 }; // class pass_local_pure_const
1798 pass_local_pure_const::execute (function
*fun
)
1800 bool changed
= false;
1803 struct cgraph_node
*node
;
1805 node
= cgraph_node::get (current_function_decl
);
1806 skip
= skip_function_for_local_pure_const (node
);
1808 if (!warn_suggest_attribute_const
1809 && !warn_suggest_attribute_pure
1813 l
= analyze_function (node
, false);
1815 /* Do NORETURN discovery. */
1816 if (!skip
&& !TREE_THIS_VOLATILE (current_function_decl
)
1817 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun
)->preds
) == 0)
1819 warn_function_noreturn (fun
->decl
);
1821 fprintf (dump_file
, "Function found to be noreturn: %s\n",
1822 current_function_name ());
1824 /* Update declaration and reduce profile to executed once. */
1825 TREE_THIS_VOLATILE (current_function_decl
) = 1;
1826 if (node
->frequency
> NODE_FREQUENCY_EXECUTED_ONCE
)
1827 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
1832 switch (l
->pure_const_state
)
1835 if (!TREE_READONLY (current_function_decl
))
1837 warn_function_const (current_function_decl
, !l
->looping
);
1839 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1840 l
->looping
? "looping " : "",
1841 current_function_name ());
1843 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1847 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1848 current_function_name ());
1850 if (!skip
&& node
->set_const_flag (true, l
->looping
))
1853 fprintf (dump_file
, "Declaration updated to be %sconst: %s\n",
1854 l
->looping
? "looping " : "",
1855 current_function_name ());
1861 if (!DECL_PURE_P (current_function_decl
))
1863 warn_function_pure (current_function_decl
, !l
->looping
);
1865 fprintf (dump_file
, "Function found to be %spure: %s\n",
1866 l
->looping
? "looping " : "",
1867 current_function_name ());
1869 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1873 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1874 current_function_name ());
1876 if (!skip
&& node
->set_pure_flag (true, l
->looping
))
1879 fprintf (dump_file
, "Declaration updated to be %spure: %s\n",
1880 l
->looping
? "looping " : "",
1881 current_function_name ());
1889 if (!l
->can_throw
&& !TREE_NOTHROW (current_function_decl
))
1891 node
->set_nothrow_flag (true);
1894 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1895 current_function_name ());
1899 return execute_fixup_cfg ();
1907 make_pass_local_pure_const (gcc::context
*ctxt
)
1909 return new pass_local_pure_const (ctxt
);
1912 /* Emit noreturn warnings. */
1916 const pass_data pass_data_warn_function_noreturn
=
1918 GIMPLE_PASS
, /* type */
1919 "*warn_function_noreturn", /* name */
1920 OPTGROUP_NONE
, /* optinfo_flags */
1921 TV_NONE
, /* tv_id */
1922 PROP_cfg
, /* properties_required */
1923 0, /* properties_provided */
1924 0, /* properties_destroyed */
1925 0, /* todo_flags_start */
1926 0, /* todo_flags_finish */
1929 class pass_warn_function_noreturn
: public gimple_opt_pass
1932 pass_warn_function_noreturn (gcc::context
*ctxt
)
1933 : gimple_opt_pass (pass_data_warn_function_noreturn
, ctxt
)
1936 /* opt_pass methods: */
1937 virtual bool gate (function
*) { return warn_suggest_attribute_noreturn
; }
1938 virtual unsigned int execute (function
*fun
)
1940 if (!TREE_THIS_VOLATILE (current_function_decl
)
1941 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun
)->preds
) == 0)
1942 warn_function_noreturn (current_function_decl
);
1946 }; // class pass_warn_function_noreturn
1951 make_pass_warn_function_noreturn (gcc::context
*ctxt
)
1953 return new pass_warn_function_noreturn (ctxt
);
1956 /* Simple local pass for pure const discovery reusing the analysis from
1957 ipa_pure_const. This pass is effective when executed together with
1958 other optimization passes in early optimization pass queue. */
1962 const pass_data pass_data_nothrow
=
1964 GIMPLE_PASS
, /* type */
1965 "nothrow", /* name */
1966 OPTGROUP_NONE
, /* optinfo_flags */
1967 TV_IPA_PURE_CONST
, /* tv_id */
1968 0, /* properties_required */
1969 0, /* properties_provided */
1970 0, /* properties_destroyed */
1971 0, /* todo_flags_start */
1972 0, /* todo_flags_finish */
1975 class pass_nothrow
: public gimple_opt_pass
1978 pass_nothrow (gcc::context
*ctxt
)
1979 : gimple_opt_pass (pass_data_nothrow
, ctxt
)
1982 /* opt_pass methods: */
1983 opt_pass
* clone () { return new pass_nothrow (m_ctxt
); }
1984 virtual bool gate (function
*) { return optimize
; }
1985 virtual unsigned int execute (function
*);
1987 }; // class pass_nothrow
1990 pass_nothrow::execute (function
*)
1992 struct cgraph_node
*node
;
1993 basic_block this_block
;
1995 if (TREE_NOTHROW (current_function_decl
))
1998 node
= cgraph_node::get (current_function_decl
);
2000 /* We run during lowering, we can not really use availability yet. */
2001 if (cgraph_node::get (current_function_decl
)->get_availability ()
2002 <= AVAIL_INTERPOSABLE
)
2005 fprintf (dump_file
, "Function is interposable;"
2006 " not analyzing.\n");
2010 FOR_EACH_BB_FN (this_block
, cfun
)
2012 for (gimple_stmt_iterator gsi
= gsi_start_bb (this_block
);
2015 if (stmt_can_throw_external (gsi_stmt (gsi
)))
2017 if (is_gimple_call (gsi_stmt (gsi
)))
2019 tree callee_t
= gimple_call_fndecl (gsi_stmt (gsi
));
2020 if (callee_t
&& recursive_call_p (current_function_decl
,
2027 fprintf (dump_file
, "Statement can throw: ");
2028 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0);
2034 node
->set_nothrow_flag (true);
2036 bool cfg_changed
= false;
2037 if (self_recursive_p (node
))
2038 FOR_EACH_BB_FN (this_block
, cfun
)
2039 if (gimple
*g
= last_stmt (this_block
))
2040 if (is_gimple_call (g
))
2042 tree callee_t
= gimple_call_fndecl (g
);
2044 && recursive_call_p (current_function_decl
, callee_t
)
2045 && maybe_clean_eh_stmt (g
)
2046 && gimple_purge_dead_eh_edges (this_block
))
2051 fprintf (dump_file
, "Function found to be nothrow: %s\n",
2052 current_function_name ());
2053 return cfg_changed
? TODO_cleanup_cfg
: 0;
2059 make_pass_nothrow (gcc::context
*ctxt
)
2061 return new pass_nothrow (ctxt
);