Sync with upstream 4.9 branch
[official-gcc.git] / embedded-4_9-branch / gcc / ipa-pure-const.c
blob8f1f5b1ff9237bc642e39c2df2ff95dbe2168907
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2014 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
10 version.
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
15 for more details.
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. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "tree.h"
39 #include "print-tree.h"
40 #include "calls.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "tree-cfg.h"
51 #include "tree-ssa-loop-niter.h"
52 #include "tree-inline.h"
53 #include "tree-pass.h"
54 #include "langhooks.h"
55 #include "ipa-utils.h"
56 #include "flags.h"
57 #include "diagnostic.h"
58 #include "gimple-pretty-print.h"
59 #include "langhooks.h"
60 #include "target.h"
61 #include "lto-streamer.h"
62 #include "data-streamer.h"
63 #include "tree-streamer.h"
64 #include "cfgloop.h"
65 #include "tree-scalar-evolution.h"
66 #include "intl.h"
67 #include "opts.h"
69 static struct pointer_set_t *visited_nodes;
71 /* Lattice values for const and pure functions. Everything starts out
72 being const, then may drop to pure and then neither depending on
73 what is found. */
74 enum pure_const_state_e
76 IPA_CONST,
77 IPA_PURE,
78 IPA_NEITHER
81 const char *pure_const_names[3] = {"const", "pure", "neither"};
83 /* Holder for the const_state. There is one of these per function
84 decl. */
85 struct funct_state_d
87 /* See above. */
88 enum pure_const_state_e pure_const_state;
89 /* What user set here; we can be always sure about this. */
90 enum pure_const_state_e state_previously_known;
91 bool looping_previously_known;
93 /* True if the function could possibly infinite loop. There are a
94 lot of ways that this could be determined. We are pretty
95 conservative here. While it is possible to cse pure and const
96 calls, it is not legal to have dce get rid of the call if there
97 is a possibility that the call could infinite loop since this is
98 a behavioral change. */
99 bool looping;
101 bool can_throw;
104 /* State used when we know nothing about function. */
105 static struct funct_state_d varying_state
106 = { IPA_NEITHER, IPA_NEITHER, true, true, true };
109 typedef struct funct_state_d * funct_state;
111 /* The storage of the funct_state is abstracted because there is the
112 possibility that it may be desirable to move this to the cgraph
113 local info. */
115 /* Array, indexed by cgraph node uid, of function states. */
117 static vec<funct_state> funct_state_vec;
119 /* Holders of ipa cgraph hooks: */
120 static struct cgraph_node_hook_list *function_insertion_hook_holder;
121 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
122 static struct cgraph_node_hook_list *node_removal_hook_holder;
124 /* Try to guess if function body will always be visible to compiler
125 when compiling the call and whether compiler will be able
126 to propagate the information by itself. */
128 static bool
129 function_always_visible_to_compiler_p (tree decl)
131 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
134 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
135 is true if the function is known to be finite. The diagnostic is
136 controlled by OPTION. WARNED_ABOUT is a pointer_set unique for
137 OPTION, this function may initialize it and it is always returned
138 by the function. */
140 static struct pointer_set_t *
141 suggest_attribute (int option, tree decl, bool known_finite,
142 struct pointer_set_t *warned_about,
143 const char * attrib_name)
145 if (!option_enabled (option, &global_options))
146 return warned_about;
147 if (TREE_THIS_VOLATILE (decl)
148 || (known_finite && function_always_visible_to_compiler_p (decl)))
149 return warned_about;
151 if (!warned_about)
152 warned_about = pointer_set_create ();
153 if (pointer_set_contains (warned_about, decl))
154 return warned_about;
155 pointer_set_insert (warned_about, decl);
156 warning_at (DECL_SOURCE_LOCATION (decl),
157 option,
158 known_finite
159 ? _("function might be candidate for attribute %<%s%>")
160 : _("function might be candidate for attribute %<%s%>"
161 " if it is known to return normally"), attrib_name);
162 return warned_about;
165 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
166 is true if the function is known to be finite. */
168 static void
169 warn_function_pure (tree decl, bool known_finite)
171 static struct pointer_set_t *warned_about;
173 warned_about
174 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
175 known_finite, warned_about, "pure");
178 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
179 is true if the function is known to be finite. */
181 static void
182 warn_function_const (tree decl, bool known_finite)
184 static struct pointer_set_t *warned_about;
185 warned_about
186 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
187 known_finite, warned_about, "const");
190 static void
191 warn_function_noreturn (tree decl)
193 static struct pointer_set_t *warned_about;
194 if (!lang_hooks.missing_noreturn_ok_p (decl)
195 && targetm.warn_func_return (decl))
196 warned_about
197 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
198 true, warned_about, "noreturn");
201 /* Return true if we have a function state for NODE. */
203 static inline bool
204 has_function_state (struct cgraph_node *node)
206 if (!funct_state_vec.exists ()
207 || funct_state_vec.length () <= (unsigned int)node->uid)
208 return false;
209 return funct_state_vec[node->uid] != NULL;
212 /* Return the function state from NODE. */
214 static inline funct_state
215 get_function_state (struct cgraph_node *node)
217 if (!funct_state_vec.exists ()
218 || funct_state_vec.length () <= (unsigned int)node->uid
219 || !funct_state_vec[node->uid])
220 /* We might want to put correct previously_known state into varying. */
221 return &varying_state;
222 return funct_state_vec[node->uid];
225 /* Set the function state S for NODE. */
227 static inline void
228 set_function_state (struct cgraph_node *node, funct_state s)
230 if (!funct_state_vec.exists ()
231 || funct_state_vec.length () <= (unsigned int)node->uid)
232 funct_state_vec.safe_grow_cleared (node->uid + 1);
233 funct_state_vec[node->uid] = s;
236 /* Check to see if the use (or definition when CHECKING_WRITE is true)
237 variable T is legal in a function that is either pure or const. */
239 static inline void
240 check_decl (funct_state local,
241 tree t, bool checking_write, bool ipa)
243 /* Do not want to do anything with volatile except mark any
244 function that uses one to be not const or pure. */
245 if (TREE_THIS_VOLATILE (t))
247 local->pure_const_state = IPA_NEITHER;
248 if (dump_file)
249 fprintf (dump_file, " Volatile operand is not const/pure");
250 return;
253 /* Do not care about a local automatic that is not static. */
254 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
255 return;
257 /* If the variable has the "used" attribute, treat it as if it had a
258 been touched by the devil. */
259 if (DECL_PRESERVE_P (t))
261 local->pure_const_state = IPA_NEITHER;
262 if (dump_file)
263 fprintf (dump_file, " Used static/global variable is not const/pure\n");
264 return;
267 /* In IPA mode we are not interested in checking actual loads and stores;
268 they will be processed at propagation time using ipa_ref. */
269 if (ipa)
270 return;
272 /* Since we have dealt with the locals and params cases above, if we
273 are CHECKING_WRITE, this cannot be a pure or constant
274 function. */
275 if (checking_write)
277 local->pure_const_state = IPA_NEITHER;
278 if (dump_file)
279 fprintf (dump_file, " static/global memory write is not const/pure\n");
280 return;
283 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
285 /* Readonly reads are safe. */
286 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
287 return; /* Read of a constant, do not change the function state. */
288 else
290 if (dump_file)
291 fprintf (dump_file, " global memory read is not const\n");
292 /* Just a regular read. */
293 if (local->pure_const_state == IPA_CONST)
294 local->pure_const_state = IPA_PURE;
297 else
299 /* Compilation level statics can be read if they are readonly
300 variables. */
301 if (TREE_READONLY (t))
302 return;
304 if (dump_file)
305 fprintf (dump_file, " static memory read is not const\n");
306 /* Just a regular read. */
307 if (local->pure_const_state == IPA_CONST)
308 local->pure_const_state = IPA_PURE;
313 /* Check to see if the use (or definition when CHECKING_WRITE is true)
314 variable T is legal in a function that is either pure or const. */
316 static inline void
317 check_op (funct_state local, tree t, bool checking_write)
319 t = get_base_address (t);
320 if (t && TREE_THIS_VOLATILE (t))
322 local->pure_const_state = IPA_NEITHER;
323 if (dump_file)
324 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
325 return;
327 else if (t
328 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
329 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
330 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
332 if (dump_file)
333 fprintf (dump_file, " Indirect ref to local memory is OK\n");
334 return;
336 else if (checking_write)
338 local->pure_const_state = IPA_NEITHER;
339 if (dump_file)
340 fprintf (dump_file, " Indirect ref write is not const/pure\n");
341 return;
343 else
345 if (dump_file)
346 fprintf (dump_file, " Indirect ref read is not const\n");
347 if (local->pure_const_state == IPA_CONST)
348 local->pure_const_state = IPA_PURE;
352 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
354 static void
355 state_from_flags (enum pure_const_state_e *state, bool *looping,
356 int flags, bool cannot_lead_to_return)
358 *looping = false;
359 if (flags & ECF_LOOPING_CONST_OR_PURE)
361 *looping = true;
362 if (dump_file && (dump_flags & TDF_DETAILS))
363 fprintf (dump_file, " looping");
365 if (flags & ECF_CONST)
367 *state = IPA_CONST;
368 if (dump_file && (dump_flags & TDF_DETAILS))
369 fprintf (dump_file, " const\n");
371 else if (flags & ECF_PURE)
373 *state = IPA_PURE;
374 if (dump_file && (dump_flags & TDF_DETAILS))
375 fprintf (dump_file, " pure\n");
377 else if (cannot_lead_to_return)
379 *state = IPA_PURE;
380 *looping = true;
381 if (dump_file && (dump_flags & TDF_DETAILS))
382 fprintf (dump_file, " ignoring side effects->pure looping\n");
384 else
386 if (dump_file && (dump_flags & TDF_DETAILS))
387 fprintf (dump_file, " neither\n");
388 *state = IPA_NEITHER;
389 *looping = true;
393 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
394 into STATE and LOOPING better of the two variants.
395 Be sure to merge looping correctly. IPA_NEITHER functions
396 have looping 0 even if they don't have to return. */
398 static inline void
399 better_state (enum pure_const_state_e *state, bool *looping,
400 enum pure_const_state_e state2, bool looping2)
402 if (state2 < *state)
404 if (*state == IPA_NEITHER)
405 *looping = looping2;
406 else
407 *looping = MIN (*looping, looping2);
408 *state = state2;
410 else if (state2 != IPA_NEITHER)
411 *looping = MIN (*looping, looping2);
414 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
415 into STATE and LOOPING worse of the two variants. */
417 static inline void
418 worse_state (enum pure_const_state_e *state, bool *looping,
419 enum pure_const_state_e state2, bool looping2)
421 *state = MAX (*state, state2);
422 *looping = MAX (*looping, looping2);
425 /* Recognize special cases of builtins that are by themselves not pure or const
426 but function using them is. */
427 static bool
428 special_builtin_state (enum pure_const_state_e *state, bool *looping,
429 tree callee)
431 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
432 switch (DECL_FUNCTION_CODE (callee))
434 case BUILT_IN_RETURN:
435 case BUILT_IN_UNREACHABLE:
436 case BUILT_IN_ALLOCA:
437 case BUILT_IN_ALLOCA_WITH_ALIGN:
438 case BUILT_IN_STACK_SAVE:
439 case BUILT_IN_STACK_RESTORE:
440 case BUILT_IN_EH_POINTER:
441 case BUILT_IN_EH_FILTER:
442 case BUILT_IN_UNWIND_RESUME:
443 case BUILT_IN_CXA_END_CLEANUP:
444 case BUILT_IN_EH_COPY_VALUES:
445 case BUILT_IN_FRAME_ADDRESS:
446 case BUILT_IN_APPLY:
447 case BUILT_IN_APPLY_ARGS:
448 *looping = false;
449 *state = IPA_CONST;
450 return true;
451 case BUILT_IN_PREFETCH:
452 *looping = true;
453 *state = IPA_CONST;
454 return true;
456 return false;
459 /* Check the parameters of a function call to CALL_EXPR to see if
460 there are any references in the parameters that are not allowed for
461 pure or const functions. Also check to see if this is either an
462 indirect call, a call outside the compilation unit, or has special
463 attributes that may also effect the purity. The CALL_EXPR node for
464 the entire call expression. */
466 static void
467 check_call (funct_state local, gimple call, bool ipa)
469 int flags = gimple_call_flags (call);
470 tree callee_t = gimple_call_fndecl (call);
471 bool possibly_throws = stmt_could_throw_p (call);
472 bool possibly_throws_externally = (possibly_throws
473 && stmt_can_throw_external (call));
475 if (possibly_throws)
477 unsigned int i;
478 for (i = 0; i < gimple_num_ops (call); i++)
479 if (gimple_op (call, i)
480 && tree_could_throw_p (gimple_op (call, i)))
482 if (possibly_throws && cfun->can_throw_non_call_exceptions)
484 if (dump_file)
485 fprintf (dump_file, " operand can throw; looping\n");
486 local->looping = true;
488 if (possibly_throws_externally)
490 if (dump_file)
491 fprintf (dump_file, " operand can throw externally\n");
492 local->can_throw = true;
497 /* The const and pure flags are set by a variety of places in the
498 compiler (including here). If someone has already set the flags
499 for the callee, (such as for some of the builtins) we will use
500 them, otherwise we will compute our own information.
502 Const and pure functions have less clobber effects than other
503 functions so we process these first. Otherwise if it is a call
504 outside the compilation unit or an indirect call we punt. This
505 leaves local calls which will be processed by following the call
506 graph. */
507 if (callee_t)
509 enum pure_const_state_e call_state;
510 bool call_looping;
512 if (special_builtin_state (&call_state, &call_looping, callee_t))
514 worse_state (&local->pure_const_state, &local->looping,
515 call_state, call_looping);
516 return;
518 /* When bad things happen to bad functions, they cannot be const
519 or pure. */
520 if (setjmp_call_p (callee_t))
522 if (dump_file)
523 fprintf (dump_file, " setjmp is not const/pure\n");
524 local->looping = true;
525 local->pure_const_state = IPA_NEITHER;
528 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
529 switch (DECL_FUNCTION_CODE (callee_t))
531 case BUILT_IN_LONGJMP:
532 case BUILT_IN_NONLOCAL_GOTO:
533 if (dump_file)
534 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
535 local->pure_const_state = IPA_NEITHER;
536 local->looping = true;
537 break;
538 default:
539 break;
543 /* When not in IPA mode, we can still handle self recursion. */
544 if (!ipa && callee_t
545 && recursive_call_p (current_function_decl, callee_t))
547 if (dump_file)
548 fprintf (dump_file, " Recursive call can loop.\n");
549 local->looping = true;
551 /* Either callee is unknown or we are doing local analysis.
552 Look to see if there are any bits available for the callee (such as by
553 declaration or because it is builtin) and process solely on the basis of
554 those bits. */
555 else if (!ipa)
557 enum pure_const_state_e call_state;
558 bool call_looping;
559 if (possibly_throws && cfun->can_throw_non_call_exceptions)
561 if (dump_file)
562 fprintf (dump_file, " can throw; looping\n");
563 local->looping = true;
565 if (possibly_throws_externally)
567 if (dump_file)
569 fprintf (dump_file, " can throw externally to lp %i\n",
570 lookup_stmt_eh_lp (call));
571 if (callee_t)
572 fprintf (dump_file, " callee:%s\n",
573 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
575 local->can_throw = true;
577 if (dump_file && (dump_flags & TDF_DETAILS))
578 fprintf (dump_file, " checking flags for call:");
579 state_from_flags (&call_state, &call_looping, flags,
580 ((flags & (ECF_NORETURN | ECF_NOTHROW))
581 == (ECF_NORETURN | ECF_NOTHROW))
582 || (!flag_exceptions && (flags & ECF_NORETURN)));
583 worse_state (&local->pure_const_state, &local->looping,
584 call_state, call_looping);
586 /* Direct functions calls are handled by IPA propagation. */
589 /* Wrapper around check_decl for loads in local more. */
591 static bool
592 check_load (gimple, tree op, tree, void *data)
594 if (DECL_P (op))
595 check_decl ((funct_state)data, op, false, false);
596 else
597 check_op ((funct_state)data, op, false);
598 return false;
601 /* Wrapper around check_decl for stores in local more. */
603 static bool
604 check_store (gimple, tree op, tree, void *data)
606 if (DECL_P (op))
607 check_decl ((funct_state)data, op, true, false);
608 else
609 check_op ((funct_state)data, op, true);
610 return false;
613 /* Wrapper around check_decl for loads in ipa mode. */
615 static bool
616 check_ipa_load (gimple, tree op, tree, void *data)
618 if (DECL_P (op))
619 check_decl ((funct_state)data, op, false, true);
620 else
621 check_op ((funct_state)data, op, false);
622 return false;
625 /* Wrapper around check_decl for stores in ipa mode. */
627 static bool
628 check_ipa_store (gimple, tree op, tree, void *data)
630 if (DECL_P (op))
631 check_decl ((funct_state)data, op, true, true);
632 else
633 check_op ((funct_state)data, op, true);
634 return false;
637 /* Look into pointer pointed to by GSIP and figure out what interesting side
638 effects it has. */
639 static void
640 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
642 gimple stmt = gsi_stmt (*gsip);
644 if (is_gimple_debug (stmt))
645 return;
647 if (dump_file)
649 fprintf (dump_file, " scanning: ");
650 print_gimple_stmt (dump_file, stmt, 0, 0);
653 if (gimple_has_volatile_ops (stmt)
654 && !gimple_clobber_p (stmt))
656 local->pure_const_state = IPA_NEITHER;
657 if (dump_file)
658 fprintf (dump_file, " Volatile stmt is not const/pure\n");
661 /* Look for loads and stores. */
662 walk_stmt_load_store_ops (stmt, local,
663 ipa ? check_ipa_load : check_load,
664 ipa ? check_ipa_store : check_store);
666 if (gimple_code (stmt) != GIMPLE_CALL
667 && stmt_could_throw_p (stmt))
669 if (cfun->can_throw_non_call_exceptions)
671 if (dump_file)
672 fprintf (dump_file, " can throw; looping\n");
673 local->looping = true;
675 if (stmt_can_throw_external (stmt))
677 if (dump_file)
678 fprintf (dump_file, " can throw externally\n");
679 local->can_throw = true;
681 else
682 if (dump_file)
683 fprintf (dump_file, " can throw\n");
685 switch (gimple_code (stmt))
687 case GIMPLE_CALL:
688 check_call (local, stmt, ipa);
689 break;
690 case GIMPLE_LABEL:
691 if (DECL_NONLOCAL (gimple_label_label (stmt)))
692 /* Target of long jump. */
694 if (dump_file)
695 fprintf (dump_file, " nonlocal label is not const/pure\n");
696 local->pure_const_state = IPA_NEITHER;
698 break;
699 case GIMPLE_ASM:
700 if (gimple_asm_clobbers_memory_p (stmt))
702 if (dump_file)
703 fprintf (dump_file, " memory asm clobber is not const/pure\n");
704 /* Abandon all hope, ye who enter here. */
705 local->pure_const_state = IPA_NEITHER;
707 if (gimple_asm_volatile_p (stmt))
709 if (dump_file)
710 fprintf (dump_file, " volatile is not const/pure\n");
711 /* Abandon all hope, ye who enter here. */
712 local->pure_const_state = IPA_NEITHER;
713 local->looping = true;
715 return;
716 default:
717 break;
722 /* This is the main routine for finding the reference patterns for
723 global variables within a function FN. */
725 static funct_state
726 analyze_function (struct cgraph_node *fn, bool ipa)
728 tree decl = fn->decl;
729 funct_state l;
730 basic_block this_block;
732 l = XCNEW (struct funct_state_d);
733 l->pure_const_state = IPA_CONST;
734 l->state_previously_known = IPA_NEITHER;
735 l->looping_previously_known = true;
736 l->looping = false;
737 l->can_throw = false;
738 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
739 flags_from_decl_or_type (fn->decl),
740 cgraph_node_cannot_return (fn));
742 if (fn->thunk.thunk_p || fn->alias)
744 /* Thunk gets propagated through, so nothing interesting happens. */
745 gcc_assert (ipa);
746 return l;
749 if (dump_file)
751 fprintf (dump_file, "\n\n local analysis of %s\n ",
752 fn->name ());
755 push_cfun (DECL_STRUCT_FUNCTION (decl));
757 FOR_EACH_BB_FN (this_block, cfun)
759 gimple_stmt_iterator gsi;
760 struct walk_stmt_info wi;
762 memset (&wi, 0, sizeof (wi));
763 for (gsi = gsi_start_bb (this_block);
764 !gsi_end_p (gsi);
765 gsi_next (&gsi))
767 check_stmt (&gsi, l, ipa);
768 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
769 goto end;
773 end:
774 if (l->pure_const_state != IPA_NEITHER)
776 /* Const functions cannot have back edges (an
777 indication of possible infinite loop side
778 effect. */
779 if (mark_dfs_back_edges ())
781 /* Preheaders are needed for SCEV to work.
782 Simple latches and recorded exits improve chances that loop will
783 proved to be finite in testcases such as in loop-15.c
784 and loop-24.c */
785 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
786 | LOOPS_HAVE_SIMPLE_LATCHES
787 | LOOPS_HAVE_RECORDED_EXITS);
788 if (dump_file && (dump_flags & TDF_DETAILS))
789 flow_loops_dump (dump_file, NULL, 0);
790 if (mark_irreducible_loops ())
792 if (dump_file)
793 fprintf (dump_file, " has irreducible loops\n");
794 l->looping = true;
796 else
798 struct loop *loop;
799 scev_initialize ();
800 FOR_EACH_LOOP (loop, 0)
801 if (!finite_loop_p (loop))
803 if (dump_file)
804 fprintf (dump_file, " can not prove finiteness of "
805 "loop %i\n", loop->num);
806 l->looping =true;
807 break;
809 scev_finalize ();
811 loop_optimizer_finalize ();
815 if (dump_file && (dump_flags & TDF_DETAILS))
816 fprintf (dump_file, " checking previously known:");
818 better_state (&l->pure_const_state, &l->looping,
819 l->state_previously_known,
820 l->looping_previously_known);
821 if (TREE_NOTHROW (decl))
822 l->can_throw = false;
824 pop_cfun ();
825 if (dump_file)
827 if (l->looping)
828 fprintf (dump_file, "Function is locally looping.\n");
829 if (l->can_throw)
830 fprintf (dump_file, "Function is locally throwing.\n");
831 if (l->pure_const_state == IPA_CONST)
832 fprintf (dump_file, "Function is locally const.\n");
833 if (l->pure_const_state == IPA_PURE)
834 fprintf (dump_file, "Function is locally pure.\n");
836 return l;
839 /* Called when new function is inserted to callgraph late. */
840 static void
841 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
843 if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
844 return;
845 /* There are some shared nodes, in particular the initializers on
846 static declarations. We do not need to scan them more than once
847 since all we would be interested in are the addressof
848 operations. */
849 visited_nodes = pointer_set_create ();
850 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
851 set_function_state (node, analyze_function (node, true));
852 pointer_set_destroy (visited_nodes);
853 visited_nodes = NULL;
856 /* Called when new clone is inserted to callgraph late. */
858 static void
859 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
860 void *data ATTRIBUTE_UNUSED)
862 if (has_function_state (src))
864 funct_state l = XNEW (struct funct_state_d);
865 gcc_assert (!has_function_state (dst));
866 memcpy (l, get_function_state (src), sizeof (*l));
867 set_function_state (dst, l);
871 /* Called when new clone is inserted to callgraph late. */
873 static void
874 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
876 if (has_function_state (node))
878 funct_state l = get_function_state (node);
879 if (l != &varying_state)
880 free (l);
881 set_function_state (node, NULL);
886 static void
887 register_hooks (void)
889 static bool init_p = false;
891 if (init_p)
892 return;
894 init_p = true;
896 node_removal_hook_holder =
897 cgraph_add_node_removal_hook (&remove_node_data, NULL);
898 node_duplication_hook_holder =
899 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
900 function_insertion_hook_holder =
901 cgraph_add_function_insertion_hook (&add_new_function, NULL);
905 /* Analyze each function in the cgraph to see if it is locally PURE or
906 CONST. */
908 static void
909 pure_const_generate_summary (void)
911 struct cgraph_node *node;
913 register_hooks ();
915 /* There are some shared nodes, in particular the initializers on
916 static declarations. We do not need to scan them more than once
917 since all we would be interested in are the addressof
918 operations. */
919 visited_nodes = pointer_set_create ();
921 /* Process all of the functions.
923 We process AVAIL_OVERWRITABLE functions. We can not use the results
924 by default, but the info can be used at LTO with -fwhole-program or
925 when function got cloned and the clone is AVAILABLE. */
927 FOR_EACH_DEFINED_FUNCTION (node)
928 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
929 set_function_state (node, analyze_function (node, true));
931 pointer_set_destroy (visited_nodes);
932 visited_nodes = NULL;
936 /* Serialize the ipa info for lto. */
938 static void
939 pure_const_write_summary (void)
941 struct cgraph_node *node;
942 struct lto_simple_output_block *ob
943 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
944 unsigned int count = 0;
945 lto_symtab_encoder_iterator lsei;
946 lto_symtab_encoder_t encoder;
948 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
950 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
951 lsei_next_function_in_partition (&lsei))
953 node = lsei_cgraph_node (lsei);
954 if (node->definition && has_function_state (node))
955 count++;
958 streamer_write_uhwi_stream (ob->main_stream, count);
960 /* Process all of the functions. */
961 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
962 lsei_next_function_in_partition (&lsei))
964 node = lsei_cgraph_node (lsei);
965 if (node->definition && has_function_state (node))
967 struct bitpack_d bp;
968 funct_state fs;
969 int node_ref;
970 lto_symtab_encoder_t encoder;
972 fs = get_function_state (node);
974 encoder = ob->decl_state->symtab_node_encoder;
975 node_ref = lto_symtab_encoder_encode (encoder, node);
976 streamer_write_uhwi_stream (ob->main_stream, node_ref);
978 /* Note that flags will need to be read in the opposite
979 order as we are pushing the bitflags into FLAGS. */
980 bp = bitpack_create (ob->main_stream);
981 bp_pack_value (&bp, fs->pure_const_state, 2);
982 bp_pack_value (&bp, fs->state_previously_known, 2);
983 bp_pack_value (&bp, fs->looping_previously_known, 1);
984 bp_pack_value (&bp, fs->looping, 1);
985 bp_pack_value (&bp, fs->can_throw, 1);
986 streamer_write_bitpack (&bp);
990 lto_destroy_simple_output_block (ob);
994 /* Deserialize the ipa info for lto. */
996 static void
997 pure_const_read_summary (void)
999 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1000 struct lto_file_decl_data *file_data;
1001 unsigned int j = 0;
1003 register_hooks ();
1004 while ((file_data = file_data_vec[j++]))
1006 const char *data;
1007 size_t len;
1008 struct lto_input_block *ib
1009 = lto_create_simple_input_block (file_data,
1010 LTO_section_ipa_pure_const,
1011 &data, &len);
1012 if (ib)
1014 unsigned int i;
1015 unsigned int count = streamer_read_uhwi (ib);
1017 for (i = 0; i < count; i++)
1019 unsigned int index;
1020 struct cgraph_node *node;
1021 struct bitpack_d bp;
1022 funct_state fs;
1023 lto_symtab_encoder_t encoder;
1025 fs = XCNEW (struct funct_state_d);
1026 index = streamer_read_uhwi (ib);
1027 encoder = file_data->symtab_node_encoder;
1028 node = cgraph (lto_symtab_encoder_deref (encoder, index));
1029 set_function_state (node, fs);
1031 /* Note that the flags must be read in the opposite
1032 order in which they were written (the bitflags were
1033 pushed into FLAGS). */
1034 bp = streamer_read_bitpack (ib);
1035 fs->pure_const_state
1036 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1037 fs->state_previously_known
1038 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1039 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1040 fs->looping = bp_unpack_value (&bp, 1);
1041 fs->can_throw = bp_unpack_value (&bp, 1);
1042 if (dump_file)
1044 int flags = flags_from_decl_or_type (node->decl);
1045 fprintf (dump_file, "Read info for %s/%i ",
1046 node->name (),
1047 node->order);
1048 if (flags & ECF_CONST)
1049 fprintf (dump_file, " const");
1050 if (flags & ECF_PURE)
1051 fprintf (dump_file, " pure");
1052 if (flags & ECF_NOTHROW)
1053 fprintf (dump_file, " nothrow");
1054 fprintf (dump_file, "\n pure const state: %s\n",
1055 pure_const_names[fs->pure_const_state]);
1056 fprintf (dump_file, " previously known state: %s\n",
1057 pure_const_names[fs->looping_previously_known]);
1058 if (fs->looping)
1059 fprintf (dump_file," function is locally looping\n");
1060 if (fs->looping_previously_known)
1061 fprintf (dump_file," function is previously known looping\n");
1062 if (fs->can_throw)
1063 fprintf (dump_file," function is locally throwing\n");
1067 lto_destroy_simple_input_block (file_data,
1068 LTO_section_ipa_pure_const,
1069 ib, data, len);
1075 static bool
1076 ignore_edge (struct cgraph_edge *e)
1078 return (!e->can_throw_external);
1081 /* Return true if NODE is self recursive function.
1082 Indirectly recursive functions appears as non-trivial strongly
1083 connected components, so we need to care about self recursion
1084 only. */
1086 static bool
1087 self_recursive_p (struct cgraph_node *node)
1089 struct cgraph_edge *e;
1090 for (e = node->callees; e; e = e->next_callee)
1091 if (cgraph_function_node (e->callee, NULL) == node)
1092 return true;
1093 return false;
1096 /* Produce transitive closure over the callgraph and compute pure/const
1097 attributes. */
1099 static void
1100 propagate_pure_const (void)
1102 struct cgraph_node *node;
1103 struct cgraph_node *w;
1104 struct cgraph_node **order =
1105 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1106 int order_pos;
1107 int i;
1108 struct ipa_dfs_info * w_info;
1110 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1111 if (dump_file)
1113 dump_cgraph (dump_file);
1114 ipa_print_order (dump_file, "reduced", order, order_pos);
1117 /* Propagate the local information through the call graph to produce
1118 the global information. All the nodes within a cycle will have
1119 the same info so we collapse cycles first. Then we can do the
1120 propagation in one pass from the leaves to the roots. */
1121 for (i = 0; i < order_pos; i++ )
1123 enum pure_const_state_e pure_const_state = IPA_CONST;
1124 bool looping = false;
1125 int count = 0;
1126 node = order[i];
1128 if (node->alias)
1129 continue;
1131 if (dump_file && (dump_flags & TDF_DETAILS))
1132 fprintf (dump_file, "Starting cycle\n");
1134 /* Find the worst state for any node in the cycle. */
1135 w = node;
1136 while (w && pure_const_state != IPA_NEITHER)
1138 struct cgraph_edge *e;
1139 struct cgraph_edge *ie;
1140 int i;
1141 struct ipa_ref *ref;
1143 funct_state w_l = get_function_state (w);
1144 if (dump_file && (dump_flags & TDF_DETAILS))
1145 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1146 w->name (),
1147 w->order,
1148 pure_const_names[w_l->pure_const_state],
1149 w_l->looping);
1151 /* First merge in function body properties. */
1152 worse_state (&pure_const_state, &looping,
1153 w_l->pure_const_state, w_l->looping);
1154 if (pure_const_state == IPA_NEITHER)
1155 break;
1157 /* For overwritable nodes we can not assume anything. */
1158 if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1160 worse_state (&pure_const_state, &looping,
1161 w_l->state_previously_known,
1162 w_l->looping_previously_known);
1163 if (dump_file && (dump_flags & TDF_DETAILS))
1165 fprintf (dump_file,
1166 " Overwritable. state %s looping %i\n",
1167 pure_const_names[w_l->state_previously_known],
1168 w_l->looping_previously_known);
1170 break;
1173 count++;
1175 /* We consider recursive cycles as possibly infinite.
1176 This might be relaxed since infinite recursion leads to stack
1177 overflow. */
1178 if (count > 1)
1179 looping = true;
1181 /* Now walk the edges and merge in callee properties. */
1182 for (e = w->callees; e; e = e->next_callee)
1184 enum availability avail;
1185 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
1186 enum pure_const_state_e edge_state = IPA_CONST;
1187 bool edge_looping = false;
1189 if (dump_file && (dump_flags & TDF_DETAILS))
1191 fprintf (dump_file,
1192 " Call to %s/%i",
1193 e->callee->name (),
1194 e->callee->order);
1196 if (avail > AVAIL_OVERWRITABLE)
1198 funct_state y_l = get_function_state (y);
1199 if (dump_file && (dump_flags & TDF_DETAILS))
1201 fprintf (dump_file,
1202 " state:%s looping:%i\n",
1203 pure_const_names[y_l->pure_const_state],
1204 y_l->looping);
1206 if (y_l->pure_const_state > IPA_PURE
1207 && cgraph_edge_cannot_lead_to_return (e))
1209 if (dump_file && (dump_flags & TDF_DETAILS))
1210 fprintf (dump_file,
1211 " Ignoring side effects"
1212 " -> pure, looping\n");
1213 edge_state = IPA_PURE;
1214 edge_looping = true;
1216 else
1218 edge_state = y_l->pure_const_state;
1219 edge_looping = y_l->looping;
1222 else if (special_builtin_state (&edge_state, &edge_looping,
1223 y->decl))
1225 else
1226 state_from_flags (&edge_state, &edge_looping,
1227 flags_from_decl_or_type (y->decl),
1228 cgraph_edge_cannot_lead_to_return (e));
1230 /* Merge the results with what we already know. */
1231 better_state (&edge_state, &edge_looping,
1232 w_l->state_previously_known,
1233 w_l->looping_previously_known);
1234 worse_state (&pure_const_state, &looping,
1235 edge_state, edge_looping);
1236 if (pure_const_state == IPA_NEITHER)
1237 break;
1239 if (pure_const_state == IPA_NEITHER)
1240 break;
1242 /* Now process the indirect call. */
1243 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1245 enum pure_const_state_e edge_state = IPA_CONST;
1246 bool edge_looping = false;
1248 if (dump_file && (dump_flags & TDF_DETAILS))
1249 fprintf (dump_file, " Indirect call");
1250 state_from_flags (&edge_state, &edge_looping,
1251 ie->indirect_info->ecf_flags,
1252 cgraph_edge_cannot_lead_to_return (ie));
1253 /* Merge the results with what we already know. */
1254 better_state (&edge_state, &edge_looping,
1255 w_l->state_previously_known,
1256 w_l->looping_previously_known);
1257 worse_state (&pure_const_state, &looping,
1258 edge_state, edge_looping);
1259 if (pure_const_state == IPA_NEITHER)
1260 break;
1262 if (pure_const_state == IPA_NEITHER)
1263 break;
1265 /* And finally all loads and stores. */
1266 for (i = 0; ipa_ref_list_reference_iterate (&w->ref_list, i, ref); i++)
1268 enum pure_const_state_e ref_state = IPA_CONST;
1269 bool ref_looping = false;
1270 switch (ref->use)
1272 case IPA_REF_LOAD:
1273 /* readonly reads are safe. */
1274 if (TREE_READONLY (ipa_ref_varpool_node (ref)->decl))
1275 break;
1276 if (dump_file && (dump_flags & TDF_DETAILS))
1277 fprintf (dump_file, " nonreadonly global var read\n");
1278 ref_state = IPA_PURE;
1279 break;
1280 case IPA_REF_STORE:
1281 if (ipa_ref_cannot_lead_to_return (ref))
1282 break;
1283 ref_state = IPA_NEITHER;
1284 if (dump_file && (dump_flags & TDF_DETAILS))
1285 fprintf (dump_file, " global var write\n");
1286 break;
1287 case IPA_REF_ADDR:
1288 break;
1290 better_state (&ref_state, &ref_looping,
1291 w_l->state_previously_known,
1292 w_l->looping_previously_known);
1293 worse_state (&pure_const_state, &looping,
1294 ref_state, ref_looping);
1295 if (pure_const_state == IPA_NEITHER)
1296 break;
1298 w_info = (struct ipa_dfs_info *) w->aux;
1299 w = w_info->next_cycle;
1301 if (dump_file && (dump_flags & TDF_DETAILS))
1302 fprintf (dump_file, "Result %s looping %i\n",
1303 pure_const_names [pure_const_state],
1304 looping);
1306 /* Copy back the region's pure_const_state which is shared by
1307 all nodes in the region. */
1308 w = node;
1309 while (w)
1311 funct_state w_l = get_function_state (w);
1312 enum pure_const_state_e this_state = pure_const_state;
1313 bool this_looping = looping;
1315 if (w_l->state_previously_known != IPA_NEITHER
1316 && this_state > w_l->state_previously_known)
1318 this_state = w_l->state_previously_known;
1319 this_looping |= w_l->looping_previously_known;
1321 if (!this_looping && self_recursive_p (w))
1322 this_looping = true;
1323 if (!w_l->looping_previously_known)
1324 this_looping = false;
1326 /* All nodes within a cycle share the same info. */
1327 w_l->pure_const_state = this_state;
1328 w_l->looping = this_looping;
1330 /* Inline clones share declaration with their offline copies;
1331 do not modify their declarations since the offline copy may
1332 be different. */
1333 if (!w->global.inlined_to)
1334 switch (this_state)
1336 case IPA_CONST:
1337 if (!TREE_READONLY (w->decl))
1339 warn_function_const (w->decl, !this_looping);
1340 if (dump_file)
1341 fprintf (dump_file, "Function found to be %sconst: %s\n",
1342 this_looping ? "looping " : "",
1343 w->name ());
1345 cgraph_set_const_flag (w, true, this_looping);
1346 break;
1348 case IPA_PURE:
1349 if (!DECL_PURE_P (w->decl))
1351 warn_function_pure (w->decl, !this_looping);
1352 if (dump_file)
1353 fprintf (dump_file, "Function found to be %spure: %s\n",
1354 this_looping ? "looping " : "",
1355 w->name ());
1357 cgraph_set_pure_flag (w, true, this_looping);
1358 break;
1360 default:
1361 break;
1363 w_info = (struct ipa_dfs_info *) w->aux;
1364 w = w_info->next_cycle;
1368 ipa_free_postorder_info ();
1369 free (order);
1372 /* Produce transitive closure over the callgraph and compute nothrow
1373 attributes. */
1375 static void
1376 propagate_nothrow (void)
1378 struct cgraph_node *node;
1379 struct cgraph_node *w;
1380 struct cgraph_node **order =
1381 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1382 int order_pos;
1383 int i;
1384 struct ipa_dfs_info * w_info;
1386 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1387 if (dump_file)
1389 dump_cgraph (dump_file);
1390 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1393 /* Propagate the local information through the call graph to produce
1394 the global information. All the nodes within a cycle will have
1395 the same info so we collapse cycles first. Then we can do the
1396 propagation in one pass from the leaves to the roots. */
1397 for (i = 0; i < order_pos; i++ )
1399 bool can_throw = false;
1400 node = order[i];
1402 if (node->alias)
1403 continue;
1405 /* Find the worst state for any node in the cycle. */
1406 w = node;
1407 while (w)
1409 struct cgraph_edge *e, *ie;
1410 funct_state w_l = get_function_state (w);
1412 if (w_l->can_throw
1413 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1414 can_throw = true;
1416 if (can_throw)
1417 break;
1419 for (e = w->callees; e; e = e->next_callee)
1421 enum availability avail;
1422 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
1424 if (avail > AVAIL_OVERWRITABLE)
1426 funct_state y_l = get_function_state (y);
1428 if (can_throw)
1429 break;
1430 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1431 && e->can_throw_external)
1432 can_throw = true;
1434 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1435 can_throw = true;
1437 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1438 if (ie->can_throw_external)
1440 can_throw = true;
1441 break;
1443 w_info = (struct ipa_dfs_info *) w->aux;
1444 w = w_info->next_cycle;
1447 /* Copy back the region's pure_const_state which is shared by
1448 all nodes in the region. */
1449 w = node;
1450 while (w)
1452 funct_state w_l = get_function_state (w);
1453 if (!can_throw && !TREE_NOTHROW (w->decl))
1455 /* Inline clones share declaration with their offline copies;
1456 do not modify their declarations since the offline copy may
1457 be different. */
1458 if (!w->global.inlined_to)
1460 cgraph_set_nothrow_flag (w, true);
1461 if (dump_file)
1462 fprintf (dump_file, "Function found to be nothrow: %s\n",
1463 w->name ());
1466 else if (can_throw && !TREE_NOTHROW (w->decl))
1467 w_l->can_throw = true;
1468 w_info = (struct ipa_dfs_info *) w->aux;
1469 w = w_info->next_cycle;
1473 ipa_free_postorder_info ();
1474 free (order);
1478 /* Produce the global information by preforming a transitive closure
1479 on the local information that was produced by generate_summary. */
1481 static unsigned int
1482 propagate (void)
1484 struct cgraph_node *node;
1486 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1487 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1488 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1490 /* Nothrow makes more function to not lead to return and improve
1491 later analysis. */
1492 propagate_nothrow ();
1493 propagate_pure_const ();
1495 /* Cleanup. */
1496 FOR_EACH_FUNCTION (node)
1497 if (has_function_state (node))
1498 free (get_function_state (node));
1499 funct_state_vec.release ();
1500 return 0;
1503 static bool
1504 gate_pure_const (void)
1506 return (flag_ipa_pure_const
1507 /* Don't bother doing anything if the program has errors. */
1508 && !seen_error ());
1511 namespace {
1513 const pass_data pass_data_ipa_pure_const =
1515 IPA_PASS, /* type */
1516 "pure-const", /* name */
1517 OPTGROUP_NONE, /* optinfo_flags */
1518 true, /* has_gate */
1519 true, /* has_execute */
1520 TV_IPA_PURE_CONST, /* tv_id */
1521 0, /* properties_required */
1522 0, /* properties_provided */
1523 0, /* properties_destroyed */
1524 0, /* todo_flags_start */
1525 0, /* todo_flags_finish */
1528 class pass_ipa_pure_const : public ipa_opt_pass_d
1530 public:
1531 pass_ipa_pure_const (gcc::context *ctxt)
1532 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1533 pure_const_generate_summary, /* generate_summary */
1534 pure_const_write_summary, /* write_summary */
1535 pure_const_read_summary, /* read_summary */
1536 NULL, /* write_optimization_summary */
1537 NULL, /* read_optimization_summary */
1538 NULL, /* stmt_fixup */
1539 0, /* function_transform_todo_flags_start */
1540 NULL, /* function_transform */
1541 NULL) /* variable_transform */
1544 /* opt_pass methods: */
1545 bool gate () { return gate_pure_const (); }
1546 unsigned int execute () { return propagate (); }
1548 }; // class pass_ipa_pure_const
1550 } // anon namespace
1552 ipa_opt_pass_d *
1553 make_pass_ipa_pure_const (gcc::context *ctxt)
1555 return new pass_ipa_pure_const (ctxt);
1558 /* Return true if function should be skipped for local pure const analysis. */
1560 static bool
1561 skip_function_for_local_pure_const (struct cgraph_node *node)
1563 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1564 we must not promote functions that are called by already processed functions. */
1566 if (function_called_by_processed_nodes_p ())
1568 if (dump_file)
1569 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1570 return true;
1572 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1574 if (dump_file)
1575 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1576 return true;
1578 return false;
1581 /* Simple local pass for pure const discovery reusing the analysis from
1582 ipa_pure_const. This pass is effective when executed together with
1583 other optimization passes in early optimization pass queue. */
1585 static unsigned int
1586 local_pure_const (void)
1588 bool changed = false;
1589 funct_state l;
1590 bool skip;
1591 struct cgraph_node *node;
1593 node = cgraph_get_node (current_function_decl);
1594 skip = skip_function_for_local_pure_const (node);
1595 if (!warn_suggest_attribute_const
1596 && !warn_suggest_attribute_pure
1597 && skip)
1598 return 0;
1600 l = analyze_function (node, false);
1602 /* Do NORETURN discovery. */
1603 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1604 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 0)
1606 warn_function_noreturn (cfun->decl);
1607 if (dump_file)
1608 fprintf (dump_file, "Function found to be noreturn: %s\n",
1609 current_function_name ());
1611 /* Update declaration and reduce profile to executed once. */
1612 TREE_THIS_VOLATILE (current_function_decl) = 1;
1613 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1614 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1616 changed = true;
1619 switch (l->pure_const_state)
1621 case IPA_CONST:
1622 if (!TREE_READONLY (current_function_decl))
1624 warn_function_const (current_function_decl, !l->looping);
1625 if (!skip)
1627 cgraph_set_const_flag (node, true, l->looping);
1628 changed = true;
1630 if (dump_file)
1631 fprintf (dump_file, "Function found to be %sconst: %s\n",
1632 l->looping ? "looping " : "",
1633 current_function_name ());
1635 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1636 && !l->looping)
1638 if (!skip)
1640 cgraph_set_const_flag (node, true, false);
1641 changed = true;
1643 if (dump_file)
1644 fprintf (dump_file, "Function found to be non-looping: %s\n",
1645 current_function_name ());
1647 break;
1649 case IPA_PURE:
1650 if (!DECL_PURE_P (current_function_decl))
1652 if (!skip)
1654 cgraph_set_pure_flag (node, true, l->looping);
1655 changed = true;
1657 warn_function_pure (current_function_decl, !l->looping);
1658 if (dump_file)
1659 fprintf (dump_file, "Function found to be %spure: %s\n",
1660 l->looping ? "looping " : "",
1661 current_function_name ());
1663 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1664 && !l->looping)
1666 if (!skip)
1668 cgraph_set_pure_flag (node, true, false);
1669 changed = true;
1671 if (dump_file)
1672 fprintf (dump_file, "Function found to be non-looping: %s\n",
1673 current_function_name ());
1675 break;
1677 default:
1678 break;
1680 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1682 cgraph_set_nothrow_flag (node, true);
1683 changed = true;
1684 if (dump_file)
1685 fprintf (dump_file, "Function found to be nothrow: %s\n",
1686 current_function_name ());
1688 free (l);
1689 if (changed)
1690 return execute_fixup_cfg ();
1691 else
1692 return 0;
1695 namespace {
1697 const pass_data pass_data_local_pure_const =
1699 GIMPLE_PASS, /* type */
1700 "local-pure-const", /* name */
1701 OPTGROUP_NONE, /* optinfo_flags */
1702 true, /* has_gate */
1703 true, /* has_execute */
1704 TV_IPA_PURE_CONST, /* tv_id */
1705 0, /* properties_required */
1706 0, /* properties_provided */
1707 0, /* properties_destroyed */
1708 0, /* todo_flags_start */
1709 0, /* todo_flags_finish */
1712 class pass_local_pure_const : public gimple_opt_pass
1714 public:
1715 pass_local_pure_const (gcc::context *ctxt)
1716 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1719 /* opt_pass methods: */
1720 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1721 bool gate () { return gate_pure_const (); }
1722 unsigned int execute () { return local_pure_const (); }
1724 }; // class pass_local_pure_const
1726 } // anon namespace
1728 gimple_opt_pass *
1729 make_pass_local_pure_const (gcc::context *ctxt)
1731 return new pass_local_pure_const (ctxt);
1734 /* Emit noreturn warnings. */
1736 static unsigned int
1737 execute_warn_function_noreturn (void)
1739 if (!TREE_THIS_VOLATILE (current_function_decl)
1740 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 0)
1741 warn_function_noreturn (current_function_decl);
1742 return 0;
1745 static bool
1746 gate_warn_function_noreturn (void)
1748 return warn_suggest_attribute_noreturn;
1751 namespace {
1753 const pass_data pass_data_warn_function_noreturn =
1755 GIMPLE_PASS, /* type */
1756 "*warn_function_noreturn", /* name */
1757 OPTGROUP_NONE, /* optinfo_flags */
1758 true, /* has_gate */
1759 true, /* has_execute */
1760 TV_NONE, /* tv_id */
1761 PROP_cfg, /* properties_required */
1762 0, /* properties_provided */
1763 0, /* properties_destroyed */
1764 0, /* todo_flags_start */
1765 0, /* todo_flags_finish */
1768 class pass_warn_function_noreturn : public gimple_opt_pass
1770 public:
1771 pass_warn_function_noreturn (gcc::context *ctxt)
1772 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1775 /* opt_pass methods: */
1776 bool gate () { return gate_warn_function_noreturn (); }
1777 unsigned int execute () { return execute_warn_function_noreturn (); }
1779 }; // class pass_warn_function_noreturn
1781 } // anon namespace
1783 gimple_opt_pass *
1784 make_pass_warn_function_noreturn (gcc::context *ctxt)
1786 return new pass_warn_function_noreturn (ctxt);