1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2015 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 "double-int.h"
48 #include "fold-const.h"
49 #include "print-tree.h"
52 #include "hard-reg-set.h"
55 #include "dominance.h"
58 #include "basic-block.h"
59 #include "tree-ssa-alias.h"
60 #include "internal-fn.h"
62 #include "gimple-expr.h"
65 #include "gimple-iterator.h"
66 #include "gimple-walk.h"
68 #include "tree-ssa-loop-niter.h"
69 #include "tree-inline.h"
70 #include "tree-pass.h"
71 #include "langhooks.h"
73 #include "plugin-api.h"
76 #include "ipa-utils.h"
78 #include "diagnostic.h"
79 #include "gimple-pretty-print.h"
80 #include "langhooks.h"
82 #include "lto-streamer.h"
83 #include "data-streamer.h"
84 #include "tree-streamer.h"
86 #include "tree-scalar-evolution.h"
90 /* Lattice values for const and pure functions. Everything starts out
91 being const, then may drop to pure and then neither depending on
93 enum pure_const_state_e
100 const char *pure_const_names
[3] = {"const", "pure", "neither"};
102 /* Holder for the const_state. There is one of these per function
107 enum pure_const_state_e pure_const_state
;
108 /* What user set here; we can be always sure about this. */
109 enum pure_const_state_e state_previously_known
;
110 bool looping_previously_known
;
112 /* True if the function could possibly infinite loop. There are a
113 lot of ways that this could be determined. We are pretty
114 conservative here. While it is possible to cse pure and const
115 calls, it is not legal to have dce get rid of the call if there
116 is a possibility that the call could infinite loop since this is
117 a behavioral change. */
122 /* If function can call free, munmap or otherwise make previously
123 non-trapping memory accesses trapping. */
127 /* State used when we know nothing about function. */
128 static struct funct_state_d varying_state
129 = { IPA_NEITHER
, IPA_NEITHER
, true, true, true, true };
132 typedef struct funct_state_d
* funct_state
;
134 /* The storage of the funct_state is abstracted because there is the
135 possibility that it may be desirable to move this to the cgraph
138 /* Array, indexed by cgraph node uid, of function states. */
140 static vec
<funct_state
> funct_state_vec
;
142 static bool gate_pure_const (void);
146 const pass_data pass_data_ipa_pure_const
=
149 "pure-const", /* name */
150 OPTGROUP_NONE
, /* optinfo_flags */
151 TV_IPA_PURE_CONST
, /* tv_id */
152 0, /* properties_required */
153 0, /* properties_provided */
154 0, /* properties_destroyed */
155 0, /* todo_flags_start */
156 0, /* todo_flags_finish */
159 class pass_ipa_pure_const
: public ipa_opt_pass_d
162 pass_ipa_pure_const(gcc::context
*ctxt
);
164 /* opt_pass methods: */
165 bool gate (function
*) { return gate_pure_const (); }
166 unsigned int execute (function
*fun
);
168 void register_hooks (void);
173 /* Holders of ipa cgraph hooks: */
174 struct cgraph_node_hook_list
*function_insertion_hook_holder
;
175 struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
176 struct cgraph_node_hook_list
*node_removal_hook_holder
;
178 }; // class pass_ipa_pure_const
182 /* Try to guess if function body will always be visible to compiler
183 when compiling the call and whether compiler will be able
184 to propagate the information by itself. */
187 function_always_visible_to_compiler_p (tree decl
)
189 return (!TREE_PUBLIC (decl
) || DECL_DECLARED_INLINE_P (decl
));
192 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
193 is true if the function is known to be finite. The diagnostic is
194 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
195 OPTION, this function may initialize it and it is always returned
198 static hash_set
<tree
> *
199 suggest_attribute (int option
, tree decl
, bool known_finite
,
200 hash_set
<tree
> *warned_about
,
201 const char * attrib_name
)
203 if (!option_enabled (option
, &global_options
))
205 if (TREE_THIS_VOLATILE (decl
)
206 || (known_finite
&& function_always_visible_to_compiler_p (decl
)))
210 warned_about
= new hash_set
<tree
>;
211 if (warned_about
->contains (decl
))
213 warned_about
->add (decl
);
214 warning_at (DECL_SOURCE_LOCATION (decl
),
217 ? _("function might be candidate for attribute %<%s%>")
218 : _("function might be candidate for attribute %<%s%>"
219 " if it is known to return normally"), attrib_name
);
223 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
224 is true if the function is known to be finite. */
227 warn_function_pure (tree decl
, bool known_finite
)
229 static hash_set
<tree
> *warned_about
;
232 = suggest_attribute (OPT_Wsuggest_attribute_pure
, decl
,
233 known_finite
, warned_about
, "pure");
236 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
237 is true if the function is known to be finite. */
240 warn_function_const (tree decl
, bool known_finite
)
242 static hash_set
<tree
> *warned_about
;
244 = suggest_attribute (OPT_Wsuggest_attribute_const
, decl
,
245 known_finite
, warned_about
, "const");
249 warn_function_noreturn (tree decl
)
251 static hash_set
<tree
> *warned_about
;
252 if (!lang_hooks
.missing_noreturn_ok_p (decl
)
253 && targetm
.warn_func_return (decl
))
255 = suggest_attribute (OPT_Wsuggest_attribute_noreturn
, decl
,
256 true, warned_about
, "noreturn");
259 /* Return true if we have a function state for NODE. */
262 has_function_state (struct cgraph_node
*node
)
264 if (!funct_state_vec
.exists ()
265 || funct_state_vec
.length () <= (unsigned int)node
->uid
)
267 return funct_state_vec
[node
->uid
] != NULL
;
270 /* Return the function state from NODE. */
272 static inline funct_state
273 get_function_state (struct cgraph_node
*node
)
275 if (!funct_state_vec
.exists ()
276 || funct_state_vec
.length () <= (unsigned int)node
->uid
277 || !funct_state_vec
[node
->uid
])
278 /* We might want to put correct previously_known state into varying. */
279 return &varying_state
;
280 return funct_state_vec
[node
->uid
];
283 /* Set the function state S for NODE. */
286 set_function_state (struct cgraph_node
*node
, funct_state s
)
288 if (!funct_state_vec
.exists ()
289 || funct_state_vec
.length () <= (unsigned int)node
->uid
)
290 funct_state_vec
.safe_grow_cleared (node
->uid
+ 1);
291 funct_state_vec
[node
->uid
] = s
;
294 /* Check to see if the use (or definition when CHECKING_WRITE is true)
295 variable T is legal in a function that is either pure or const. */
298 check_decl (funct_state local
,
299 tree t
, bool checking_write
, bool ipa
)
301 /* Do not want to do anything with volatile except mark any
302 function that uses one to be not const or pure. */
303 if (TREE_THIS_VOLATILE (t
))
305 local
->pure_const_state
= IPA_NEITHER
;
307 fprintf (dump_file
, " Volatile operand is not const/pure");
311 /* Do not care about a local automatic that is not static. */
312 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
315 /* If the variable has the "used" attribute, treat it as if it had a
316 been touched by the devil. */
317 if (DECL_PRESERVE_P (t
))
319 local
->pure_const_state
= IPA_NEITHER
;
321 fprintf (dump_file
, " Used static/global variable is not const/pure\n");
325 /* In IPA mode we are not interested in checking actual loads and stores;
326 they will be processed at propagation time using ipa_ref. */
330 /* Since we have dealt with the locals and params cases above, if we
331 are CHECKING_WRITE, this cannot be a pure or constant
335 local
->pure_const_state
= IPA_NEITHER
;
337 fprintf (dump_file
, " static/global memory write is not const/pure\n");
341 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
343 /* Readonly reads are safe. */
344 if (TREE_READONLY (t
) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t
)))
345 return; /* Read of a constant, do not change the function state. */
349 fprintf (dump_file
, " global memory read is not const\n");
350 /* Just a regular read. */
351 if (local
->pure_const_state
== IPA_CONST
)
352 local
->pure_const_state
= IPA_PURE
;
357 /* Compilation level statics can be read if they are readonly
359 if (TREE_READONLY (t
))
363 fprintf (dump_file
, " static memory read is not const\n");
364 /* Just a regular read. */
365 if (local
->pure_const_state
== IPA_CONST
)
366 local
->pure_const_state
= IPA_PURE
;
371 /* Check to see if the use (or definition when CHECKING_WRITE is true)
372 variable T is legal in a function that is either pure or const. */
375 check_op (funct_state local
, tree t
, bool checking_write
)
377 t
= get_base_address (t
);
378 if (t
&& TREE_THIS_VOLATILE (t
))
380 local
->pure_const_state
= IPA_NEITHER
;
382 fprintf (dump_file
, " Volatile indirect ref is not const/pure\n");
386 && (INDIRECT_REF_P (t
) || TREE_CODE (t
) == MEM_REF
)
387 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
388 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t
, 0)))
391 fprintf (dump_file
, " Indirect ref to local memory is OK\n");
394 else if (checking_write
)
396 local
->pure_const_state
= IPA_NEITHER
;
398 fprintf (dump_file
, " Indirect ref write is not const/pure\n");
404 fprintf (dump_file
, " Indirect ref read is not const\n");
405 if (local
->pure_const_state
== IPA_CONST
)
406 local
->pure_const_state
= IPA_PURE
;
410 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
413 state_from_flags (enum pure_const_state_e
*state
, bool *looping
,
414 int flags
, bool cannot_lead_to_return
)
417 if (flags
& ECF_LOOPING_CONST_OR_PURE
)
420 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
421 fprintf (dump_file
, " looping");
423 if (flags
& ECF_CONST
)
426 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
427 fprintf (dump_file
, " const\n");
429 else if (flags
& ECF_PURE
)
432 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
433 fprintf (dump_file
, " pure\n");
435 else if (cannot_lead_to_return
)
439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
440 fprintf (dump_file
, " ignoring side effects->pure looping\n");
444 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
445 fprintf (dump_file
, " neither\n");
446 *state
= IPA_NEITHER
;
451 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
452 into STATE and LOOPING better of the two variants.
453 Be sure to merge looping correctly. IPA_NEITHER functions
454 have looping 0 even if they don't have to return. */
457 better_state (enum pure_const_state_e
*state
, bool *looping
,
458 enum pure_const_state_e state2
, bool looping2
)
462 if (*state
== IPA_NEITHER
)
465 *looping
= MIN (*looping
, looping2
);
468 else if (state2
!= IPA_NEITHER
)
469 *looping
= MIN (*looping
, looping2
);
472 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
473 into STATE and LOOPING worse of the two variants. */
476 worse_state (enum pure_const_state_e
*state
, bool *looping
,
477 enum pure_const_state_e state2
, bool looping2
)
479 *state
= MAX (*state
, state2
);
480 *looping
= MAX (*looping
, looping2
);
483 /* Recognize special cases of builtins that are by themselves not pure or const
484 but function using them is. */
486 special_builtin_state (enum pure_const_state_e
*state
, bool *looping
,
489 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
490 switch (DECL_FUNCTION_CODE (callee
))
492 case BUILT_IN_RETURN
:
493 case BUILT_IN_UNREACHABLE
:
494 case BUILT_IN_ALLOCA
:
495 case BUILT_IN_ALLOCA_WITH_ALIGN
:
496 case BUILT_IN_STACK_SAVE
:
497 case BUILT_IN_STACK_RESTORE
:
498 case BUILT_IN_EH_POINTER
:
499 case BUILT_IN_EH_FILTER
:
500 case BUILT_IN_UNWIND_RESUME
:
501 case BUILT_IN_CXA_END_CLEANUP
:
502 case BUILT_IN_EH_COPY_VALUES
:
503 case BUILT_IN_FRAME_ADDRESS
:
505 case BUILT_IN_APPLY_ARGS
:
509 case BUILT_IN_PREFETCH
:
519 /* Check the parameters of a function call to CALL_EXPR to see if
520 there are any references in the parameters that are not allowed for
521 pure or const functions. Also check to see if this is either an
522 indirect call, a call outside the compilation unit, or has special
523 attributes that may also effect the purity. The CALL_EXPR node for
524 the entire call expression. */
527 check_call (funct_state local
, gcall
*call
, bool ipa
)
529 int flags
= gimple_call_flags (call
);
530 tree callee_t
= gimple_call_fndecl (call
);
531 bool possibly_throws
= stmt_could_throw_p (call
);
532 bool possibly_throws_externally
= (possibly_throws
533 && stmt_can_throw_external (call
));
538 for (i
= 0; i
< gimple_num_ops (call
); i
++)
539 if (gimple_op (call
, i
)
540 && tree_could_throw_p (gimple_op (call
, i
)))
542 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
545 fprintf (dump_file
, " operand can throw; looping\n");
546 local
->looping
= true;
548 if (possibly_throws_externally
)
551 fprintf (dump_file
, " operand can throw externally\n");
552 local
->can_throw
= true;
557 /* The const and pure flags are set by a variety of places in the
558 compiler (including here). If someone has already set the flags
559 for the callee, (such as for some of the builtins) we will use
560 them, otherwise we will compute our own information.
562 Const and pure functions have less clobber effects than other
563 functions so we process these first. Otherwise if it is a call
564 outside the compilation unit or an indirect call we punt. This
565 leaves local calls which will be processed by following the call
569 enum pure_const_state_e call_state
;
572 if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
)
573 && !nonfreeing_call_p (call
))
574 local
->can_free
= true;
576 if (special_builtin_state (&call_state
, &call_looping
, callee_t
))
578 worse_state (&local
->pure_const_state
, &local
->looping
,
579 call_state
, call_looping
);
582 /* When bad things happen to bad functions, they cannot be const
584 if (setjmp_call_p (callee_t
))
587 fprintf (dump_file
, " setjmp is not const/pure\n");
588 local
->looping
= true;
589 local
->pure_const_state
= IPA_NEITHER
;
592 if (DECL_BUILT_IN_CLASS (callee_t
) == BUILT_IN_NORMAL
)
593 switch (DECL_FUNCTION_CODE (callee_t
))
595 case BUILT_IN_LONGJMP
:
596 case BUILT_IN_NONLOCAL_GOTO
:
598 fprintf (dump_file
, " longjmp and nonlocal goto is not const/pure\n");
599 local
->pure_const_state
= IPA_NEITHER
;
600 local
->looping
= true;
606 else if (gimple_call_internal_p (call
) && !nonfreeing_call_p (call
))
607 local
->can_free
= true;
609 /* When not in IPA mode, we can still handle self recursion. */
611 && recursive_call_p (current_function_decl
, callee_t
))
614 fprintf (dump_file
, " Recursive call can loop.\n");
615 local
->looping
= true;
617 /* Either callee is unknown or we are doing local analysis.
618 Look to see if there are any bits available for the callee (such as by
619 declaration or because it is builtin) and process solely on the basis of
623 enum pure_const_state_e call_state
;
625 if (possibly_throws
&& cfun
->can_throw_non_call_exceptions
)
628 fprintf (dump_file
, " can throw; looping\n");
629 local
->looping
= true;
631 if (possibly_throws_externally
)
635 fprintf (dump_file
, " can throw externally to lp %i\n",
636 lookup_stmt_eh_lp (call
));
638 fprintf (dump_file
, " callee:%s\n",
639 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t
)));
641 local
->can_throw
= true;
643 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
644 fprintf (dump_file
, " checking flags for call:");
645 state_from_flags (&call_state
, &call_looping
, flags
,
646 ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
647 == (ECF_NORETURN
| ECF_NOTHROW
))
648 || (!flag_exceptions
&& (flags
& ECF_NORETURN
)));
649 worse_state (&local
->pure_const_state
, &local
->looping
,
650 call_state
, call_looping
);
652 /* Direct functions calls are handled by IPA propagation. */
655 /* Wrapper around check_decl for loads in local more. */
658 check_load (gimple
, tree op
, tree
, void *data
)
661 check_decl ((funct_state
)data
, op
, false, false);
663 check_op ((funct_state
)data
, op
, false);
667 /* Wrapper around check_decl for stores in local more. */
670 check_store (gimple
, tree op
, tree
, void *data
)
673 check_decl ((funct_state
)data
, op
, true, false);
675 check_op ((funct_state
)data
, op
, true);
679 /* Wrapper around check_decl for loads in ipa mode. */
682 check_ipa_load (gimple
, tree op
, tree
, void *data
)
685 check_decl ((funct_state
)data
, op
, false, true);
687 check_op ((funct_state
)data
, op
, false);
691 /* Wrapper around check_decl for stores in ipa mode. */
694 check_ipa_store (gimple
, tree op
, tree
, void *data
)
697 check_decl ((funct_state
)data
, op
, true, true);
699 check_op ((funct_state
)data
, op
, true);
703 /* Look into pointer pointed to by GSIP and figure out what interesting side
706 check_stmt (gimple_stmt_iterator
*gsip
, funct_state local
, bool ipa
)
708 gimple stmt
= gsi_stmt (*gsip
);
710 if (is_gimple_debug (stmt
))
715 fprintf (dump_file
, " scanning: ");
716 print_gimple_stmt (dump_file
, stmt
, 0, 0);
719 if (gimple_has_volatile_ops (stmt
)
720 && !gimple_clobber_p (stmt
))
722 local
->pure_const_state
= IPA_NEITHER
;
724 fprintf (dump_file
, " Volatile stmt is not const/pure\n");
727 /* Look for loads and stores. */
728 walk_stmt_load_store_ops (stmt
, local
,
729 ipa
? check_ipa_load
: check_load
,
730 ipa
? check_ipa_store
: check_store
);
732 if (gimple_code (stmt
) != GIMPLE_CALL
733 && stmt_could_throw_p (stmt
))
735 if (cfun
->can_throw_non_call_exceptions
)
738 fprintf (dump_file
, " can throw; looping\n");
739 local
->looping
= true;
741 if (stmt_can_throw_external (stmt
))
744 fprintf (dump_file
, " can throw externally\n");
745 local
->can_throw
= true;
749 fprintf (dump_file
, " can throw\n");
751 switch (gimple_code (stmt
))
754 check_call (local
, as_a
<gcall
*> (stmt
), ipa
);
757 if (DECL_NONLOCAL (gimple_label_label (as_a
<glabel
*> (stmt
))))
758 /* Target of long jump. */
761 fprintf (dump_file
, " nonlocal label is not const/pure\n");
762 local
->pure_const_state
= IPA_NEITHER
;
766 if (gimple_asm_clobbers_memory_p (as_a
<gasm
*> (stmt
)))
769 fprintf (dump_file
, " memory asm clobber is not const/pure\n");
770 /* Abandon all hope, ye who enter here. */
771 local
->pure_const_state
= IPA_NEITHER
;
772 local
->can_free
= true;
774 if (gimple_asm_volatile_p (as_a
<gasm
*> (stmt
)))
777 fprintf (dump_file
, " volatile is not const/pure\n");
778 /* Abandon all hope, ye who enter here. */
779 local
->pure_const_state
= IPA_NEITHER
;
780 local
->looping
= true;
781 local
->can_free
= true;
790 /* This is the main routine for finding the reference patterns for
791 global variables within a function FN. */
794 analyze_function (struct cgraph_node
*fn
, bool ipa
)
796 tree decl
= fn
->decl
;
798 basic_block this_block
;
800 l
= XCNEW (struct funct_state_d
);
801 l
->pure_const_state
= IPA_CONST
;
802 l
->state_previously_known
= IPA_NEITHER
;
803 l
->looping_previously_known
= true;
805 l
->can_throw
= false;
807 state_from_flags (&l
->state_previously_known
, &l
->looping_previously_known
,
808 flags_from_decl_or_type (fn
->decl
),
809 fn
->cannot_return_p ());
811 if (fn
->thunk
.thunk_p
|| fn
->alias
)
813 /* Thunk gets propagated through, so nothing interesting happens. */
815 if (fn
->thunk
.thunk_p
&& fn
->thunk
.virtual_offset_p
)
816 l
->pure_const_state
= IPA_NEITHER
;
822 fprintf (dump_file
, "\n\n local analysis of %s\n ",
826 push_cfun (DECL_STRUCT_FUNCTION (decl
));
828 FOR_EACH_BB_FN (this_block
, cfun
)
830 gimple_stmt_iterator gsi
;
831 struct walk_stmt_info wi
;
833 memset (&wi
, 0, sizeof (wi
));
834 for (gsi
= gsi_start_bb (this_block
);
838 check_stmt (&gsi
, l
, ipa
);
839 if (l
->pure_const_state
== IPA_NEITHER
848 if (l
->pure_const_state
!= IPA_NEITHER
)
850 /* Const functions cannot have back edges (an
851 indication of possible infinite loop side
853 if (mark_dfs_back_edges ())
855 /* Preheaders are needed for SCEV to work.
856 Simple latches and recorded exits improve chances that loop will
857 proved to be finite in testcases such as in loop-15.c
859 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
860 | LOOPS_HAVE_SIMPLE_LATCHES
861 | LOOPS_HAVE_RECORDED_EXITS
);
862 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
863 flow_loops_dump (dump_file
, NULL
, 0);
864 if (mark_irreducible_loops ())
867 fprintf (dump_file
, " has irreducible loops\n");
874 FOR_EACH_LOOP (loop
, 0)
875 if (!finite_loop_p (loop
))
878 fprintf (dump_file
, " can not prove finiteness of "
879 "loop %i\n", loop
->num
);
885 loop_optimizer_finalize ();
889 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
890 fprintf (dump_file
, " checking previously known:");
892 better_state (&l
->pure_const_state
, &l
->looping
,
893 l
->state_previously_known
,
894 l
->looping_previously_known
);
895 if (TREE_NOTHROW (decl
))
896 l
->can_throw
= false;
902 fprintf (dump_file
, "Function is locally looping.\n");
904 fprintf (dump_file
, "Function is locally throwing.\n");
905 if (l
->pure_const_state
== IPA_CONST
)
906 fprintf (dump_file
, "Function is locally const.\n");
907 if (l
->pure_const_state
== IPA_PURE
)
908 fprintf (dump_file
, "Function is locally pure.\n");
910 fprintf (dump_file
, "Function can locally free.\n");
915 /* Called when new function is inserted to callgraph late. */
917 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
919 if (node
->get_availability () < AVAIL_INTERPOSABLE
)
921 /* There are some shared nodes, in particular the initializers on
922 static declarations. We do not need to scan them more than once
923 since all we would be interested in are the addressof
925 if (node
->get_availability () > AVAIL_INTERPOSABLE
926 && opt_for_fn (node
->decl
, flag_ipa_pure_const
))
927 set_function_state (node
, analyze_function (node
, true));
930 /* Called when new clone is inserted to callgraph late. */
933 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
934 void *data ATTRIBUTE_UNUSED
)
936 if (has_function_state (src
))
938 funct_state l
= XNEW (struct funct_state_d
);
939 gcc_assert (!has_function_state (dst
));
940 memcpy (l
, get_function_state (src
), sizeof (*l
));
941 set_function_state (dst
, l
);
945 /* Called when new clone is inserted to callgraph late. */
948 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
950 if (has_function_state (node
))
952 funct_state l
= get_function_state (node
);
953 if (l
!= &varying_state
)
955 set_function_state (node
, NULL
);
961 pass_ipa_pure_const::
962 register_hooks (void)
969 node_removal_hook_holder
=
970 symtab
->add_cgraph_removal_hook (&remove_node_data
, NULL
);
971 node_duplication_hook_holder
=
972 symtab
->add_cgraph_duplication_hook (&duplicate_node_data
, NULL
);
973 function_insertion_hook_holder
=
974 symtab
->add_cgraph_insertion_hook (&add_new_function
, NULL
);
978 /* Analyze each function in the cgraph to see if it is locally PURE or
982 pure_const_generate_summary (void)
984 struct cgraph_node
*node
;
986 pass_ipa_pure_const
*pass
= static_cast <pass_ipa_pure_const
*> (current_pass
);
987 pass
->register_hooks ();
989 /* Process all of the functions.
991 We process AVAIL_INTERPOSABLE functions. We can not use the results
992 by default, but the info can be used at LTO with -fwhole-program or
993 when function got cloned and the clone is AVAILABLE. */
995 FOR_EACH_DEFINED_FUNCTION (node
)
996 if (node
->get_availability () >= AVAIL_INTERPOSABLE
997 && opt_for_fn (node
->decl
, flag_ipa_pure_const
))
998 set_function_state (node
, analyze_function (node
, true));
1002 /* Serialize the ipa info for lto. */
1005 pure_const_write_summary (void)
1007 struct cgraph_node
*node
;
1008 struct lto_simple_output_block
*ob
1009 = lto_create_simple_output_block (LTO_section_ipa_pure_const
);
1010 unsigned int count
= 0;
1011 lto_symtab_encoder_iterator lsei
;
1012 lto_symtab_encoder_t encoder
;
1014 encoder
= lto_get_out_decl_state ()->symtab_node_encoder
;
1016 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
1017 lsei_next_function_in_partition (&lsei
))
1019 node
= lsei_cgraph_node (lsei
);
1020 if (node
->definition
&& has_function_state (node
))
1024 streamer_write_uhwi_stream (ob
->main_stream
, count
);
1026 /* Process all of the functions. */
1027 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
1028 lsei_next_function_in_partition (&lsei
))
1030 node
= lsei_cgraph_node (lsei
);
1031 if (node
->definition
&& has_function_state (node
))
1033 struct bitpack_d bp
;
1036 lto_symtab_encoder_t encoder
;
1038 fs
= get_function_state (node
);
1040 encoder
= ob
->decl_state
->symtab_node_encoder
;
1041 node_ref
= lto_symtab_encoder_encode (encoder
, node
);
1042 streamer_write_uhwi_stream (ob
->main_stream
, node_ref
);
1044 /* Note that flags will need to be read in the opposite
1045 order as we are pushing the bitflags into FLAGS. */
1046 bp
= bitpack_create (ob
->main_stream
);
1047 bp_pack_value (&bp
, fs
->pure_const_state
, 2);
1048 bp_pack_value (&bp
, fs
->state_previously_known
, 2);
1049 bp_pack_value (&bp
, fs
->looping_previously_known
, 1);
1050 bp_pack_value (&bp
, fs
->looping
, 1);
1051 bp_pack_value (&bp
, fs
->can_throw
, 1);
1052 bp_pack_value (&bp
, fs
->can_free
, 1);
1053 streamer_write_bitpack (&bp
);
1057 lto_destroy_simple_output_block (ob
);
1061 /* Deserialize the ipa info for lto. */
1064 pure_const_read_summary (void)
1066 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1067 struct lto_file_decl_data
*file_data
;
1070 pass_ipa_pure_const
*pass
= static_cast <pass_ipa_pure_const
*> (current_pass
);
1071 pass
->register_hooks ();
1073 while ((file_data
= file_data_vec
[j
++]))
1077 struct lto_input_block
*ib
1078 = lto_create_simple_input_block (file_data
,
1079 LTO_section_ipa_pure_const
,
1084 unsigned int count
= streamer_read_uhwi (ib
);
1086 for (i
= 0; i
< count
; i
++)
1089 struct cgraph_node
*node
;
1090 struct bitpack_d bp
;
1092 lto_symtab_encoder_t encoder
;
1094 fs
= XCNEW (struct funct_state_d
);
1095 index
= streamer_read_uhwi (ib
);
1096 encoder
= file_data
->symtab_node_encoder
;
1097 node
= dyn_cast
<cgraph_node
*> (lto_symtab_encoder_deref (encoder
,
1099 set_function_state (node
, fs
);
1101 /* Note that the flags must be read in the opposite
1102 order in which they were written (the bitflags were
1103 pushed into FLAGS). */
1104 bp
= streamer_read_bitpack (ib
);
1105 fs
->pure_const_state
1106 = (enum pure_const_state_e
) bp_unpack_value (&bp
, 2);
1107 fs
->state_previously_known
1108 = (enum pure_const_state_e
) bp_unpack_value (&bp
, 2);
1109 fs
->looping_previously_known
= bp_unpack_value (&bp
, 1);
1110 fs
->looping
= bp_unpack_value (&bp
, 1);
1111 fs
->can_throw
= bp_unpack_value (&bp
, 1);
1112 fs
->can_free
= bp_unpack_value (&bp
, 1);
1115 int flags
= flags_from_decl_or_type (node
->decl
);
1116 fprintf (dump_file
, "Read info for %s/%i ",
1119 if (flags
& ECF_CONST
)
1120 fprintf (dump_file
, " const");
1121 if (flags
& ECF_PURE
)
1122 fprintf (dump_file
, " pure");
1123 if (flags
& ECF_NOTHROW
)
1124 fprintf (dump_file
, " nothrow");
1125 fprintf (dump_file
, "\n pure const state: %s\n",
1126 pure_const_names
[fs
->pure_const_state
]);
1127 fprintf (dump_file
, " previously known state: %s\n",
1128 pure_const_names
[fs
->looping_previously_known
]);
1130 fprintf (dump_file
," function is locally looping\n");
1131 if (fs
->looping_previously_known
)
1132 fprintf (dump_file
," function is previously known looping\n");
1134 fprintf (dump_file
," function is locally throwing\n");
1136 fprintf (dump_file
," function can locally free\n");
1140 lto_destroy_simple_input_block (file_data
,
1141 LTO_section_ipa_pure_const
,
1149 ignore_edge (struct cgraph_edge
*e
)
1151 return (!e
->can_throw_external
);
1154 /* Return true if NODE is self recursive function.
1155 Indirectly recursive functions appears as non-trivial strongly
1156 connected components, so we need to care about self recursion
1160 self_recursive_p (struct cgraph_node
*node
)
1162 struct cgraph_edge
*e
;
1163 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1164 if (e
->callee
->function_symbol () == node
)
1169 /* Return true if N is cdtor that is not const or pure. In this case we may
1170 need to remove unreachable function if it is marked const/pure. */
1173 cdtor_p (cgraph_node
*n
, void *)
1175 if (DECL_STATIC_CONSTRUCTOR (n
->decl
) || DECL_STATIC_DESTRUCTOR (n
->decl
))
1176 return !TREE_READONLY (n
->decl
) && !DECL_PURE_P (n
->decl
);
1180 /* Produce transitive closure over the callgraph and compute pure/const
1184 propagate_pure_const (void)
1186 struct cgraph_node
*node
;
1187 struct cgraph_node
*w
;
1188 struct cgraph_node
**order
=
1189 XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
1192 struct ipa_dfs_info
* w_info
;
1193 bool remove_p
= false;
1195 order_pos
= ipa_reduced_postorder (order
, true, false, NULL
);
1198 cgraph_node::dump_cgraph (dump_file
);
1199 ipa_print_order (dump_file
, "reduced", order
, order_pos
);
1202 /* Propagate the local information through the call graph to produce
1203 the global information. All the nodes within a cycle will have
1204 the same info so we collapse cycles first. Then we can do the
1205 propagation in one pass from the leaves to the roots. */
1206 for (i
= 0; i
< order_pos
; i
++ )
1208 enum pure_const_state_e pure_const_state
= IPA_CONST
;
1209 bool looping
= false;
1216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1217 fprintf (dump_file
, "Starting cycle\n");
1219 /* Find the worst state for any node in the cycle. */
1221 while (w
&& pure_const_state
!= IPA_NEITHER
)
1223 struct cgraph_edge
*e
;
1224 struct cgraph_edge
*ie
;
1226 struct ipa_ref
*ref
= NULL
;
1228 funct_state w_l
= get_function_state (w
);
1229 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1230 fprintf (dump_file
, " Visiting %s/%i state:%s looping %i\n",
1233 pure_const_names
[w_l
->pure_const_state
],
1236 /* First merge in function body properties. */
1237 worse_state (&pure_const_state
, &looping
,
1238 w_l
->pure_const_state
, w_l
->looping
);
1239 if (pure_const_state
== IPA_NEITHER
)
1242 /* For overwritable nodes we can not assume anything. */
1243 if (w
->get_availability () == AVAIL_INTERPOSABLE
)
1245 worse_state (&pure_const_state
, &looping
,
1246 w_l
->state_previously_known
,
1247 w_l
->looping_previously_known
);
1248 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1251 " Overwritable. state %s looping %i\n",
1252 pure_const_names
[w_l
->state_previously_known
],
1253 w_l
->looping_previously_known
);
1260 /* We consider recursive cycles as possibly infinite.
1261 This might be relaxed since infinite recursion leads to stack
1266 /* Now walk the edges and merge in callee properties. */
1267 for (e
= w
->callees
; e
; e
= e
->next_callee
)
1269 enum availability avail
;
1270 struct cgraph_node
*y
= e
->callee
->
1271 function_or_virtual_thunk_symbol (&avail
);
1272 enum pure_const_state_e edge_state
= IPA_CONST
;
1273 bool edge_looping
= false;
1275 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1282 if (avail
> AVAIL_INTERPOSABLE
)
1284 funct_state y_l
= get_function_state (y
);
1285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1288 " state:%s looping:%i\n",
1289 pure_const_names
[y_l
->pure_const_state
],
1292 if (y_l
->pure_const_state
> IPA_PURE
1293 && e
->cannot_lead_to_return_p ())
1295 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1297 " Ignoring side effects"
1298 " -> pure, looping\n");
1299 edge_state
= IPA_PURE
;
1300 edge_looping
= true;
1304 edge_state
= y_l
->pure_const_state
;
1305 edge_looping
= y_l
->looping
;
1308 else if (special_builtin_state (&edge_state
, &edge_looping
,
1312 state_from_flags (&edge_state
, &edge_looping
,
1313 flags_from_decl_or_type (y
->decl
),
1314 e
->cannot_lead_to_return_p ());
1316 /* Merge the results with what we already know. */
1317 better_state (&edge_state
, &edge_looping
,
1318 w_l
->state_previously_known
,
1319 w_l
->looping_previously_known
);
1320 worse_state (&pure_const_state
, &looping
,
1321 edge_state
, edge_looping
);
1322 if (pure_const_state
== IPA_NEITHER
)
1325 if (pure_const_state
== IPA_NEITHER
)
1328 /* Now process the indirect call. */
1329 for (ie
= w
->indirect_calls
; ie
; ie
= ie
->next_callee
)
1331 enum pure_const_state_e edge_state
= IPA_CONST
;
1332 bool edge_looping
= false;
1334 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1335 fprintf (dump_file
, " Indirect call");
1336 state_from_flags (&edge_state
, &edge_looping
,
1337 ie
->indirect_info
->ecf_flags
,
1338 ie
->cannot_lead_to_return_p ());
1339 /* Merge the results with what we already know. */
1340 better_state (&edge_state
, &edge_looping
,
1341 w_l
->state_previously_known
,
1342 w_l
->looping_previously_known
);
1343 worse_state (&pure_const_state
, &looping
,
1344 edge_state
, edge_looping
);
1345 if (pure_const_state
== IPA_NEITHER
)
1348 if (pure_const_state
== IPA_NEITHER
)
1351 /* And finally all loads and stores. */
1352 for (i
= 0; w
->iterate_reference (i
, ref
); i
++)
1354 enum pure_const_state_e ref_state
= IPA_CONST
;
1355 bool ref_looping
= false;
1359 /* readonly reads are safe. */
1360 if (TREE_READONLY (ref
->referred
->decl
))
1362 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1363 fprintf (dump_file
, " nonreadonly global var read\n");
1364 ref_state
= IPA_PURE
;
1367 if (ref
->cannot_lead_to_return ())
1369 ref_state
= IPA_NEITHER
;
1370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1371 fprintf (dump_file
, " global var write\n");
1379 better_state (&ref_state
, &ref_looping
,
1380 w_l
->state_previously_known
,
1381 w_l
->looping_previously_known
);
1382 worse_state (&pure_const_state
, &looping
,
1383 ref_state
, ref_looping
);
1384 if (pure_const_state
== IPA_NEITHER
)
1387 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1388 w
= w_info
->next_cycle
;
1390 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1391 fprintf (dump_file
, "Result %s looping %i\n",
1392 pure_const_names
[pure_const_state
],
1395 /* Find the worst state of can_free for any node in the cycle. */
1396 bool can_free
= false;
1398 while (w
&& !can_free
)
1400 struct cgraph_edge
*e
;
1401 funct_state w_l
= get_function_state (w
);
1404 || w
->get_availability () == AVAIL_INTERPOSABLE
1405 || w
->indirect_calls
)
1408 for (e
= w
->callees
; e
&& !can_free
; e
= e
->next_callee
)
1410 enum availability avail
;
1411 struct cgraph_node
*y
= e
->callee
->
1412 function_or_virtual_thunk_symbol (&avail
);
1414 if (avail
> AVAIL_INTERPOSABLE
)
1415 can_free
= get_function_state (y
)->can_free
;
1419 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1420 w
= w_info
->next_cycle
;
1423 /* Copy back the region's pure_const_state which is shared by
1424 all nodes in the region. */
1428 funct_state w_l
= get_function_state (w
);
1429 enum pure_const_state_e this_state
= pure_const_state
;
1430 bool this_looping
= looping
;
1432 w_l
->can_free
= can_free
;
1433 w
->nonfreeing_fn
= !can_free
;
1434 if (!can_free
&& dump_file
)
1435 fprintf (dump_file
, "Function found not to call free: %s\n",
1438 if (w_l
->state_previously_known
!= IPA_NEITHER
1439 && this_state
> w_l
->state_previously_known
)
1441 this_state
= w_l
->state_previously_known
;
1442 this_looping
|= w_l
->looping_previously_known
;
1444 if (!this_looping
&& self_recursive_p (w
))
1445 this_looping
= true;
1446 if (!w_l
->looping_previously_known
)
1447 this_looping
= false;
1449 /* All nodes within a cycle share the same info. */
1450 w_l
->pure_const_state
= this_state
;
1451 w_l
->looping
= this_looping
;
1453 /* Inline clones share declaration with their offline copies;
1454 do not modify their declarations since the offline copy may
1456 if (!w
->global
.inlined_to
)
1460 if (!TREE_READONLY (w
->decl
))
1462 warn_function_const (w
->decl
, !this_looping
);
1464 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1465 this_looping
? "looping " : "",
1468 remove_p
|= w
->call_for_symbol_and_aliases (cdtor_p
,
1470 w
->set_const_flag (true, this_looping
);
1474 if (!DECL_PURE_P (w
->decl
))
1476 warn_function_pure (w
->decl
, !this_looping
);
1478 fprintf (dump_file
, "Function found to be %spure: %s\n",
1479 this_looping
? "looping " : "",
1482 remove_p
|= w
->call_for_symbol_and_aliases (cdtor_p
,
1484 w
->set_pure_flag (true, this_looping
);
1490 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1491 w
= w_info
->next_cycle
;
1495 ipa_free_postorder_info ();
1500 /* Produce transitive closure over the callgraph and compute nothrow
1504 propagate_nothrow (void)
1506 struct cgraph_node
*node
;
1507 struct cgraph_node
*w
;
1508 struct cgraph_node
**order
=
1509 XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
1512 struct ipa_dfs_info
* w_info
;
1514 order_pos
= ipa_reduced_postorder (order
, true, false, ignore_edge
);
1517 cgraph_node::dump_cgraph (dump_file
);
1518 ipa_print_order (dump_file
, "reduced for nothrow", order
, order_pos
);
1521 /* Propagate the local information through the call graph to produce
1522 the global information. All the nodes within a cycle will have
1523 the same info so we collapse cycles first. Then we can do the
1524 propagation in one pass from the leaves to the roots. */
1525 for (i
= 0; i
< order_pos
; i
++ )
1527 bool can_throw
= false;
1533 /* Find the worst state for any node in the cycle. */
1535 while (w
&& !can_throw
)
1537 struct cgraph_edge
*e
, *ie
;
1538 funct_state w_l
= get_function_state (w
);
1541 || w
->get_availability () == AVAIL_INTERPOSABLE
)
1544 for (e
= w
->callees
; e
&& !can_throw
; e
= e
->next_callee
)
1546 enum availability avail
;
1547 struct cgraph_node
*y
= e
->callee
->
1548 function_or_virtual_thunk_symbol (&avail
);
1550 if (avail
> AVAIL_INTERPOSABLE
)
1552 funct_state y_l
= get_function_state (y
);
1554 if (y_l
->can_throw
&& !TREE_NOTHROW (w
->decl
)
1555 && e
->can_throw_external
)
1558 else if (e
->can_throw_external
&& !TREE_NOTHROW (y
->decl
))
1561 for (ie
= w
->indirect_calls
; ie
&& !can_throw
; ie
= ie
->next_callee
)
1562 if (ie
->can_throw_external
)
1564 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1565 w
= w_info
->next_cycle
;
1568 /* Copy back the region's pure_const_state which is shared by
1569 all nodes in the region. */
1573 funct_state w_l
= get_function_state (w
);
1574 if (!can_throw
&& !TREE_NOTHROW (w
->decl
))
1576 /* Inline clones share declaration with their offline copies;
1577 do not modify their declarations since the offline copy may
1579 if (!w
->global
.inlined_to
)
1581 w
->set_nothrow_flag (true);
1583 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1587 else if (can_throw
&& !TREE_NOTHROW (w
->decl
))
1588 w_l
->can_throw
= true;
1589 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1590 w
= w_info
->next_cycle
;
1594 ipa_free_postorder_info ();
1599 /* Produce the global information by preforming a transitive closure
1600 on the local information that was produced by generate_summary. */
1603 pass_ipa_pure_const::
1604 execute (function
*)
1606 struct cgraph_node
*node
;
1609 symtab
->remove_cgraph_insertion_hook (function_insertion_hook_holder
);
1610 symtab
->remove_cgraph_duplication_hook (node_duplication_hook_holder
);
1611 symtab
->remove_cgraph_removal_hook (node_removal_hook_holder
);
1613 /* Nothrow makes more function to not lead to return and improve
1615 propagate_nothrow ();
1616 remove_p
= propagate_pure_const ();
1619 FOR_EACH_FUNCTION (node
)
1620 if (has_function_state (node
))
1621 free (get_function_state (node
));
1622 funct_state_vec
.release ();
1623 return remove_p
? TODO_remove_functions
: 0;
1627 gate_pure_const (void)
1629 return flag_ipa_pure_const
|| in_lto_p
;
1632 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context
*ctxt
)
1633 : ipa_opt_pass_d(pass_data_ipa_pure_const
, ctxt
,
1634 pure_const_generate_summary
, /* generate_summary */
1635 pure_const_write_summary
, /* write_summary */
1636 pure_const_read_summary
, /* read_summary */
1637 NULL
, /* write_optimization_summary */
1638 NULL
, /* read_optimization_summary */
1639 NULL
, /* stmt_fixup */
1640 0, /* function_transform_todo_flags_start */
1641 NULL
, /* function_transform */
1642 NULL
), /* variable_transform */
1644 function_insertion_hook_holder(NULL
),
1645 node_duplication_hook_holder(NULL
),
1646 node_removal_hook_holder(NULL
)
1651 make_pass_ipa_pure_const (gcc::context
*ctxt
)
1653 return new pass_ipa_pure_const (ctxt
);
1656 /* Return true if function should be skipped for local pure const analysis. */
1659 skip_function_for_local_pure_const (struct cgraph_node
*node
)
1661 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1662 we must not promote functions that are called by already processed functions. */
1664 if (function_called_by_processed_nodes_p ())
1667 fprintf (dump_file
, "Function called in recursive cycle; ignoring\n");
1670 if (node
->get_availability () <= AVAIL_INTERPOSABLE
)
1673 fprintf (dump_file
, "Function is not available or overwritable; not analyzing.\n");
1679 /* Simple local pass for pure const discovery reusing the analysis from
1680 ipa_pure_const. This pass is effective when executed together with
1681 other optimization passes in early optimization pass queue. */
1685 const pass_data pass_data_local_pure_const
=
1687 GIMPLE_PASS
, /* type */
1688 "local-pure-const", /* name */
1689 OPTGROUP_NONE
, /* optinfo_flags */
1690 TV_IPA_PURE_CONST
, /* tv_id */
1691 0, /* properties_required */
1692 0, /* properties_provided */
1693 0, /* properties_destroyed */
1694 0, /* todo_flags_start */
1695 0, /* todo_flags_finish */
1698 class pass_local_pure_const
: public gimple_opt_pass
1701 pass_local_pure_const (gcc::context
*ctxt
)
1702 : gimple_opt_pass (pass_data_local_pure_const
, ctxt
)
1705 /* opt_pass methods: */
1706 opt_pass
* clone () { return new pass_local_pure_const (m_ctxt
); }
1707 virtual bool gate (function
*) { return gate_pure_const (); }
1708 virtual unsigned int execute (function
*);
1710 }; // class pass_local_pure_const
1713 pass_local_pure_const::execute (function
*fun
)
1715 bool changed
= false;
1718 struct cgraph_node
*node
;
1720 node
= cgraph_node::get (current_function_decl
);
1721 skip
= skip_function_for_local_pure_const (node
);
1722 if (!warn_suggest_attribute_const
1723 && !warn_suggest_attribute_pure
1727 l
= analyze_function (node
, false);
1729 /* Do NORETURN discovery. */
1730 if (!skip
&& !TREE_THIS_VOLATILE (current_function_decl
)
1731 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun
)->preds
) == 0)
1733 warn_function_noreturn (fun
->decl
);
1735 fprintf (dump_file
, "Function found to be noreturn: %s\n",
1736 current_function_name ());
1738 /* Update declaration and reduce profile to executed once. */
1739 TREE_THIS_VOLATILE (current_function_decl
) = 1;
1740 if (node
->frequency
> NODE_FREQUENCY_EXECUTED_ONCE
)
1741 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
1746 switch (l
->pure_const_state
)
1749 if (!TREE_READONLY (current_function_decl
))
1751 warn_function_const (current_function_decl
, !l
->looping
);
1754 node
->set_const_flag (true, l
->looping
);
1758 fprintf (dump_file
, "Function found to be %sconst: %s\n",
1759 l
->looping
? "looping " : "",
1760 current_function_name ());
1762 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1767 node
->set_const_flag (true, false);
1771 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1772 current_function_name ());
1777 if (!DECL_PURE_P (current_function_decl
))
1781 node
->set_pure_flag (true, l
->looping
);
1784 warn_function_pure (current_function_decl
, !l
->looping
);
1786 fprintf (dump_file
, "Function found to be %spure: %s\n",
1787 l
->looping
? "looping " : "",
1788 current_function_name ());
1790 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl
)
1795 node
->set_pure_flag (true, false);
1799 fprintf (dump_file
, "Function found to be non-looping: %s\n",
1800 current_function_name ());
1807 if (!l
->can_throw
&& !TREE_NOTHROW (current_function_decl
))
1809 node
->set_nothrow_flag (true);
1812 fprintf (dump_file
, "Function found to be nothrow: %s\n",
1813 current_function_name ());
1817 return execute_fixup_cfg ();
1825 make_pass_local_pure_const (gcc::context
*ctxt
)
1827 return new pass_local_pure_const (ctxt
);
1830 /* Emit noreturn warnings. */
1834 const pass_data pass_data_warn_function_noreturn
=
1836 GIMPLE_PASS
, /* type */
1837 "*warn_function_noreturn", /* name */
1838 OPTGROUP_NONE
, /* optinfo_flags */
1839 TV_NONE
, /* tv_id */
1840 PROP_cfg
, /* properties_required */
1841 0, /* properties_provided */
1842 0, /* properties_destroyed */
1843 0, /* todo_flags_start */
1844 0, /* todo_flags_finish */
1847 class pass_warn_function_noreturn
: public gimple_opt_pass
1850 pass_warn_function_noreturn (gcc::context
*ctxt
)
1851 : gimple_opt_pass (pass_data_warn_function_noreturn
, ctxt
)
1854 /* opt_pass methods: */
1855 virtual bool gate (function
*) { return warn_suggest_attribute_noreturn
; }
1856 virtual unsigned int execute (function
*fun
)
1858 if (!TREE_THIS_VOLATILE (current_function_decl
)
1859 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun
)->preds
) == 0)
1860 warn_function_noreturn (current_function_decl
);
1864 }; // class pass_warn_function_noreturn
1869 make_pass_warn_function_noreturn (gcc::context
*ctxt
)
1871 return new pass_warn_function_noreturn (ctxt
);