* gcc/ChangeLog: Fix ChangeLog entry.
[official-gcc.git] / gcc / ipa-pure-const.c
blobd84b35fd7168ef15df6a8f8a07b5db73993c6690
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2013 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 stmt ATTRIBUTE_UNUSED, tree op, 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 stmt ATTRIBUTE_UNUSED, tree op, 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 stmt ATTRIBUTE_UNUSED, tree op, 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 stmt ATTRIBUTE_UNUSED, tree op, 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 (this_block)
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 switch (this_state)
1332 case IPA_CONST:
1333 if (!TREE_READONLY (w->decl))
1335 warn_function_const (w->decl, !this_looping);
1336 if (dump_file)
1337 fprintf (dump_file, "Function found to be %sconst: %s\n",
1338 this_looping ? "looping " : "",
1339 w->name ());
1341 cgraph_set_const_flag (w, true, this_looping);
1342 break;
1344 case IPA_PURE:
1345 if (!DECL_PURE_P (w->decl))
1347 warn_function_pure (w->decl, !this_looping);
1348 if (dump_file)
1349 fprintf (dump_file, "Function found to be %spure: %s\n",
1350 this_looping ? "looping " : "",
1351 w->name ());
1353 cgraph_set_pure_flag (w, true, this_looping);
1354 break;
1356 default:
1357 break;
1359 w_info = (struct ipa_dfs_info *) w->aux;
1360 w = w_info->next_cycle;
1364 ipa_free_postorder_info ();
1365 free (order);
1368 /* Produce transitive closure over the callgraph and compute nothrow
1369 attributes. */
1371 static void
1372 propagate_nothrow (void)
1374 struct cgraph_node *node;
1375 struct cgraph_node *w;
1376 struct cgraph_node **order =
1377 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1378 int order_pos;
1379 int i;
1380 struct ipa_dfs_info * w_info;
1382 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1383 if (dump_file)
1385 dump_cgraph (dump_file);
1386 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1389 /* Propagate the local information through the call graph to produce
1390 the global information. All the nodes within a cycle will have
1391 the same info so we collapse cycles first. Then we can do the
1392 propagation in one pass from the leaves to the roots. */
1393 for (i = 0; i < order_pos; i++ )
1395 bool can_throw = false;
1396 node = order[i];
1398 if (node->alias)
1399 continue;
1401 /* Find the worst state for any node in the cycle. */
1402 w = node;
1403 while (w)
1405 struct cgraph_edge *e, *ie;
1406 funct_state w_l = get_function_state (w);
1408 if (w_l->can_throw
1409 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1410 can_throw = true;
1412 if (can_throw)
1413 break;
1415 for (e = w->callees; e; e = e->next_callee)
1417 enum availability avail;
1418 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
1420 if (avail > AVAIL_OVERWRITABLE)
1422 funct_state y_l = get_function_state (y);
1424 if (can_throw)
1425 break;
1426 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1427 && e->can_throw_external)
1428 can_throw = true;
1430 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1431 can_throw = true;
1433 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1434 if (ie->can_throw_external)
1436 can_throw = true;
1437 break;
1439 w_info = (struct ipa_dfs_info *) w->aux;
1440 w = w_info->next_cycle;
1443 /* Copy back the region's pure_const_state which is shared by
1444 all nodes in the region. */
1445 w = node;
1446 while (w)
1448 funct_state w_l = get_function_state (w);
1449 if (!can_throw && !TREE_NOTHROW (w->decl))
1451 cgraph_set_nothrow_flag (w, true);
1452 if (dump_file)
1453 fprintf (dump_file, "Function found to be nothrow: %s\n",
1454 w->name ());
1456 else if (can_throw && !TREE_NOTHROW (w->decl))
1457 w_l->can_throw = true;
1458 w_info = (struct ipa_dfs_info *) w->aux;
1459 w = w_info->next_cycle;
1463 ipa_free_postorder_info ();
1464 free (order);
1468 /* Produce the global information by preforming a transitive closure
1469 on the local information that was produced by generate_summary. */
1471 static unsigned int
1472 propagate (void)
1474 struct cgraph_node *node;
1476 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1477 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1478 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1480 /* Nothrow makes more function to not lead to return and improve
1481 later analysis. */
1482 propagate_nothrow ();
1483 propagate_pure_const ();
1485 /* Cleanup. */
1486 FOR_EACH_FUNCTION (node)
1487 if (has_function_state (node))
1488 free (get_function_state (node));
1489 funct_state_vec.release ();
1490 return 0;
1493 static bool
1494 gate_pure_const (void)
1496 return (flag_ipa_pure_const
1497 /* Don't bother doing anything if the program has errors. */
1498 && !seen_error ());
1501 namespace {
1503 const pass_data pass_data_ipa_pure_const =
1505 IPA_PASS, /* type */
1506 "pure-const", /* name */
1507 OPTGROUP_NONE, /* optinfo_flags */
1508 true, /* has_gate */
1509 true, /* has_execute */
1510 TV_IPA_PURE_CONST, /* tv_id */
1511 0, /* properties_required */
1512 0, /* properties_provided */
1513 0, /* properties_destroyed */
1514 0, /* todo_flags_start */
1515 0, /* todo_flags_finish */
1518 class pass_ipa_pure_const : public ipa_opt_pass_d
1520 public:
1521 pass_ipa_pure_const (gcc::context *ctxt)
1522 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1523 pure_const_generate_summary, /* generate_summary */
1524 pure_const_write_summary, /* write_summary */
1525 pure_const_read_summary, /* read_summary */
1526 NULL, /* write_optimization_summary */
1527 NULL, /* read_optimization_summary */
1528 NULL, /* stmt_fixup */
1529 0, /* function_transform_todo_flags_start */
1530 NULL, /* function_transform */
1531 NULL) /* variable_transform */
1534 /* opt_pass methods: */
1535 bool gate () { return gate_pure_const (); }
1536 unsigned int execute () { return propagate (); }
1538 }; // class pass_ipa_pure_const
1540 } // anon namespace
1542 ipa_opt_pass_d *
1543 make_pass_ipa_pure_const (gcc::context *ctxt)
1545 return new pass_ipa_pure_const (ctxt);
1548 /* Return true if function should be skipped for local pure const analysis. */
1550 static bool
1551 skip_function_for_local_pure_const (struct cgraph_node *node)
1553 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1554 we must not promote functions that are called by already processed functions. */
1556 if (function_called_by_processed_nodes_p ())
1558 if (dump_file)
1559 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1560 return true;
1562 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1564 if (dump_file)
1565 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1566 return true;
1568 return false;
1571 /* Simple local pass for pure const discovery reusing the analysis from
1572 ipa_pure_const. This pass is effective when executed together with
1573 other optimization passes in early optimization pass queue. */
1575 static unsigned int
1576 local_pure_const (void)
1578 bool changed = false;
1579 funct_state l;
1580 bool skip;
1581 struct cgraph_node *node;
1583 node = cgraph_get_node (current_function_decl);
1584 skip = skip_function_for_local_pure_const (node);
1585 if (!warn_suggest_attribute_const
1586 && !warn_suggest_attribute_pure
1587 && skip)
1588 return 0;
1590 l = analyze_function (node, false);
1592 /* Do NORETURN discovery. */
1593 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1594 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 0)
1596 warn_function_noreturn (cfun->decl);
1597 if (dump_file)
1598 fprintf (dump_file, "Function found to be noreturn: %s\n",
1599 current_function_name ());
1601 /* Update declaration and reduce profile to executed once. */
1602 TREE_THIS_VOLATILE (current_function_decl) = 1;
1603 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1604 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1606 changed = true;
1609 switch (l->pure_const_state)
1611 case IPA_CONST:
1612 if (!TREE_READONLY (current_function_decl))
1614 warn_function_const (current_function_decl, !l->looping);
1615 if (!skip)
1617 cgraph_set_const_flag (node, true, l->looping);
1618 changed = true;
1620 if (dump_file)
1621 fprintf (dump_file, "Function found to be %sconst: %s\n",
1622 l->looping ? "looping " : "",
1623 current_function_name ());
1625 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1626 && !l->looping)
1628 if (!skip)
1630 cgraph_set_const_flag (node, true, false);
1631 changed = true;
1633 if (dump_file)
1634 fprintf (dump_file, "Function found to be non-looping: %s\n",
1635 current_function_name ());
1637 break;
1639 case IPA_PURE:
1640 if (!DECL_PURE_P (current_function_decl))
1642 if (!skip)
1644 cgraph_set_pure_flag (node, true, l->looping);
1645 changed = true;
1647 warn_function_pure (current_function_decl, !l->looping);
1648 if (dump_file)
1649 fprintf (dump_file, "Function found to be %spure: %s\n",
1650 l->looping ? "looping " : "",
1651 current_function_name ());
1653 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1654 && !l->looping)
1656 if (!skip)
1658 cgraph_set_pure_flag (node, true, false);
1659 changed = true;
1661 if (dump_file)
1662 fprintf (dump_file, "Function found to be non-looping: %s\n",
1663 current_function_name ());
1665 break;
1667 default:
1668 break;
1670 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1672 cgraph_set_nothrow_flag (node, true);
1673 changed = true;
1674 if (dump_file)
1675 fprintf (dump_file, "Function found to be nothrow: %s\n",
1676 current_function_name ());
1678 free (l);
1679 if (changed)
1680 return execute_fixup_cfg ();
1681 else
1682 return 0;
1685 namespace {
1687 const pass_data pass_data_local_pure_const =
1689 GIMPLE_PASS, /* type */
1690 "local-pure-const", /* name */
1691 OPTGROUP_NONE, /* optinfo_flags */
1692 true, /* has_gate */
1693 true, /* has_execute */
1694 TV_IPA_PURE_CONST, /* tv_id */
1695 0, /* properties_required */
1696 0, /* properties_provided */
1697 0, /* properties_destroyed */
1698 0, /* todo_flags_start */
1699 0, /* todo_flags_finish */
1702 class pass_local_pure_const : public gimple_opt_pass
1704 public:
1705 pass_local_pure_const (gcc::context *ctxt)
1706 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1709 /* opt_pass methods: */
1710 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1711 bool gate () { return gate_pure_const (); }
1712 unsigned int execute () { return local_pure_const (); }
1714 }; // class pass_local_pure_const
1716 } // anon namespace
1718 gimple_opt_pass *
1719 make_pass_local_pure_const (gcc::context *ctxt)
1721 return new pass_local_pure_const (ctxt);
1724 /* Emit noreturn warnings. */
1726 static unsigned int
1727 execute_warn_function_noreturn (void)
1729 if (!TREE_THIS_VOLATILE (current_function_decl)
1730 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 0)
1731 warn_function_noreturn (current_function_decl);
1732 return 0;
1735 static bool
1736 gate_warn_function_noreturn (void)
1738 return warn_suggest_attribute_noreturn;
1741 namespace {
1743 const pass_data pass_data_warn_function_noreturn =
1745 GIMPLE_PASS, /* type */
1746 "*warn_function_noreturn", /* name */
1747 OPTGROUP_NONE, /* optinfo_flags */
1748 true, /* has_gate */
1749 true, /* has_execute */
1750 TV_NONE, /* tv_id */
1751 PROP_cfg, /* properties_required */
1752 0, /* properties_provided */
1753 0, /* properties_destroyed */
1754 0, /* todo_flags_start */
1755 0, /* todo_flags_finish */
1758 class pass_warn_function_noreturn : public gimple_opt_pass
1760 public:
1761 pass_warn_function_noreturn (gcc::context *ctxt)
1762 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1765 /* opt_pass methods: */
1766 bool gate () { return gate_warn_function_noreturn (); }
1767 unsigned int execute () { return execute_warn_function_noreturn (); }
1769 }; // class pass_warn_function_noreturn
1771 } // anon namespace
1773 gimple_opt_pass *
1774 make_pass_warn_function_noreturn (gcc::context *ctxt)
1776 return new pass_warn_function_noreturn (ctxt);