2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / ipa-pure-const.c
blob2c281be0acb2ba50805f77e02a95aa1e92a1349d
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 fn->cannot_return_p ());
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 (node->get_availability () < AVAIL_INTERPOSABLE)
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 (node->get_availability () > AVAIL_INTERPOSABLE)
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_INTERPOSABLE 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 (node->get_availability () >= AVAIL_INTERPOSABLE)
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 = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1029 index));
1030 set_function_state (node, fs);
1032 /* Note that the flags must be read in the opposite
1033 order in which they were written (the bitflags were
1034 pushed into FLAGS). */
1035 bp = streamer_read_bitpack (ib);
1036 fs->pure_const_state
1037 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1038 fs->state_previously_known
1039 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1040 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1041 fs->looping = bp_unpack_value (&bp, 1);
1042 fs->can_throw = bp_unpack_value (&bp, 1);
1043 if (dump_file)
1045 int flags = flags_from_decl_or_type (node->decl);
1046 fprintf (dump_file, "Read info for %s/%i ",
1047 node->name (),
1048 node->order);
1049 if (flags & ECF_CONST)
1050 fprintf (dump_file, " const");
1051 if (flags & ECF_PURE)
1052 fprintf (dump_file, " pure");
1053 if (flags & ECF_NOTHROW)
1054 fprintf (dump_file, " nothrow");
1055 fprintf (dump_file, "\n pure const state: %s\n",
1056 pure_const_names[fs->pure_const_state]);
1057 fprintf (dump_file, " previously known state: %s\n",
1058 pure_const_names[fs->looping_previously_known]);
1059 if (fs->looping)
1060 fprintf (dump_file," function is locally looping\n");
1061 if (fs->looping_previously_known)
1062 fprintf (dump_file," function is previously known looping\n");
1063 if (fs->can_throw)
1064 fprintf (dump_file," function is locally throwing\n");
1068 lto_destroy_simple_input_block (file_data,
1069 LTO_section_ipa_pure_const,
1070 ib, data, len);
1076 static bool
1077 ignore_edge (struct cgraph_edge *e)
1079 return (!e->can_throw_external);
1082 /* Return true if NODE is self recursive function.
1083 Indirectly recursive functions appears as non-trivial strongly
1084 connected components, so we need to care about self recursion
1085 only. */
1087 static bool
1088 self_recursive_p (struct cgraph_node *node)
1090 struct cgraph_edge *e;
1091 for (e = node->callees; e; e = e->next_callee)
1092 if (e->callee->function_symbol () == node)
1093 return true;
1094 return false;
1097 /* Produce transitive closure over the callgraph and compute pure/const
1098 attributes. */
1100 static void
1101 propagate_pure_const (void)
1103 struct cgraph_node *node;
1104 struct cgraph_node *w;
1105 struct cgraph_node **order =
1106 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1107 int order_pos;
1108 int i;
1109 struct ipa_dfs_info * w_info;
1111 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1112 if (dump_file)
1114 cgraph_node::dump_cgraph (dump_file);
1115 ipa_print_order (dump_file, "reduced", order, order_pos);
1118 /* Propagate the local information through the call graph to produce
1119 the global information. All the nodes within a cycle will have
1120 the same info so we collapse cycles first. Then we can do the
1121 propagation in one pass from the leaves to the roots. */
1122 for (i = 0; i < order_pos; i++ )
1124 enum pure_const_state_e pure_const_state = IPA_CONST;
1125 bool looping = false;
1126 int count = 0;
1127 node = order[i];
1129 if (node->alias)
1130 continue;
1132 if (dump_file && (dump_flags & TDF_DETAILS))
1133 fprintf (dump_file, "Starting cycle\n");
1135 /* Find the worst state for any node in the cycle. */
1136 w = node;
1137 while (w && pure_const_state != IPA_NEITHER)
1139 struct cgraph_edge *e;
1140 struct cgraph_edge *ie;
1141 int i;
1142 struct ipa_ref *ref = NULL;
1144 funct_state w_l = get_function_state (w);
1145 if (dump_file && (dump_flags & TDF_DETAILS))
1146 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1147 w->name (),
1148 w->order,
1149 pure_const_names[w_l->pure_const_state],
1150 w_l->looping);
1152 /* First merge in function body properties. */
1153 worse_state (&pure_const_state, &looping,
1154 w_l->pure_const_state, w_l->looping);
1155 if (pure_const_state == IPA_NEITHER)
1156 break;
1158 /* For overwritable nodes we can not assume anything. */
1159 if (w->get_availability () == AVAIL_INTERPOSABLE)
1161 worse_state (&pure_const_state, &looping,
1162 w_l->state_previously_known,
1163 w_l->looping_previously_known);
1164 if (dump_file && (dump_flags & TDF_DETAILS))
1166 fprintf (dump_file,
1167 " Overwritable. state %s looping %i\n",
1168 pure_const_names[w_l->state_previously_known],
1169 w_l->looping_previously_known);
1171 break;
1174 count++;
1176 /* We consider recursive cycles as possibly infinite.
1177 This might be relaxed since infinite recursion leads to stack
1178 overflow. */
1179 if (count > 1)
1180 looping = true;
1182 /* Now walk the edges and merge in callee properties. */
1183 for (e = w->callees; e; e = e->next_callee)
1185 enum availability avail;
1186 struct cgraph_node *y = e->callee->function_symbol (&avail);
1187 enum pure_const_state_e edge_state = IPA_CONST;
1188 bool edge_looping = false;
1190 if (dump_file && (dump_flags & TDF_DETAILS))
1192 fprintf (dump_file,
1193 " Call to %s/%i",
1194 e->callee->name (),
1195 e->callee->order);
1197 if (avail > AVAIL_INTERPOSABLE)
1199 funct_state y_l = get_function_state (y);
1200 if (dump_file && (dump_flags & TDF_DETAILS))
1202 fprintf (dump_file,
1203 " state:%s looping:%i\n",
1204 pure_const_names[y_l->pure_const_state],
1205 y_l->looping);
1207 if (y_l->pure_const_state > IPA_PURE
1208 && cgraph_edge_cannot_lead_to_return (e))
1210 if (dump_file && (dump_flags & TDF_DETAILS))
1211 fprintf (dump_file,
1212 " Ignoring side effects"
1213 " -> pure, looping\n");
1214 edge_state = IPA_PURE;
1215 edge_looping = true;
1217 else
1219 edge_state = y_l->pure_const_state;
1220 edge_looping = y_l->looping;
1223 else if (special_builtin_state (&edge_state, &edge_looping,
1224 y->decl))
1226 else
1227 state_from_flags (&edge_state, &edge_looping,
1228 flags_from_decl_or_type (y->decl),
1229 cgraph_edge_cannot_lead_to_return (e));
1231 /* Merge the results with what we already know. */
1232 better_state (&edge_state, &edge_looping,
1233 w_l->state_previously_known,
1234 w_l->looping_previously_known);
1235 worse_state (&pure_const_state, &looping,
1236 edge_state, edge_looping);
1237 if (pure_const_state == IPA_NEITHER)
1238 break;
1240 if (pure_const_state == IPA_NEITHER)
1241 break;
1243 /* Now process the indirect call. */
1244 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1246 enum pure_const_state_e edge_state = IPA_CONST;
1247 bool edge_looping = false;
1249 if (dump_file && (dump_flags & TDF_DETAILS))
1250 fprintf (dump_file, " Indirect call");
1251 state_from_flags (&edge_state, &edge_looping,
1252 ie->indirect_info->ecf_flags,
1253 cgraph_edge_cannot_lead_to_return (ie));
1254 /* Merge the results with what we already know. */
1255 better_state (&edge_state, &edge_looping,
1256 w_l->state_previously_known,
1257 w_l->looping_previously_known);
1258 worse_state (&pure_const_state, &looping,
1259 edge_state, edge_looping);
1260 if (pure_const_state == IPA_NEITHER)
1261 break;
1263 if (pure_const_state == IPA_NEITHER)
1264 break;
1266 /* And finally all loads and stores. */
1267 for (i = 0; w->iterate_reference (i, ref); i++)
1269 enum pure_const_state_e ref_state = IPA_CONST;
1270 bool ref_looping = false;
1271 switch (ref->use)
1273 case IPA_REF_LOAD:
1274 /* readonly reads are safe. */
1275 if (TREE_READONLY (ref->referred->decl))
1276 break;
1277 if (dump_file && (dump_flags & TDF_DETAILS))
1278 fprintf (dump_file, " nonreadonly global var read\n");
1279 ref_state = IPA_PURE;
1280 break;
1281 case IPA_REF_STORE:
1282 if (ref->cannot_lead_to_return ())
1283 break;
1284 ref_state = IPA_NEITHER;
1285 if (dump_file && (dump_flags & TDF_DETAILS))
1286 fprintf (dump_file, " global var write\n");
1287 break;
1288 case IPA_REF_ADDR:
1289 break;
1290 default:
1291 gcc_unreachable ();
1293 better_state (&ref_state, &ref_looping,
1294 w_l->state_previously_known,
1295 w_l->looping_previously_known);
1296 worse_state (&pure_const_state, &looping,
1297 ref_state, ref_looping);
1298 if (pure_const_state == IPA_NEITHER)
1299 break;
1301 w_info = (struct ipa_dfs_info *) w->aux;
1302 w = w_info->next_cycle;
1304 if (dump_file && (dump_flags & TDF_DETAILS))
1305 fprintf (dump_file, "Result %s looping %i\n",
1306 pure_const_names [pure_const_state],
1307 looping);
1309 /* Copy back the region's pure_const_state which is shared by
1310 all nodes in the region. */
1311 w = node;
1312 while (w)
1314 funct_state w_l = get_function_state (w);
1315 enum pure_const_state_e this_state = pure_const_state;
1316 bool this_looping = looping;
1318 if (w_l->state_previously_known != IPA_NEITHER
1319 && this_state > w_l->state_previously_known)
1321 this_state = w_l->state_previously_known;
1322 this_looping |= w_l->looping_previously_known;
1324 if (!this_looping && self_recursive_p (w))
1325 this_looping = true;
1326 if (!w_l->looping_previously_known)
1327 this_looping = false;
1329 /* All nodes within a cycle share the same info. */
1330 w_l->pure_const_state = this_state;
1331 w_l->looping = this_looping;
1333 /* Inline clones share declaration with their offline copies;
1334 do not modify their declarations since the offline copy may
1335 be different. */
1336 if (!w->global.inlined_to)
1337 switch (this_state)
1339 case IPA_CONST:
1340 if (!TREE_READONLY (w->decl))
1342 warn_function_const (w->decl, !this_looping);
1343 if (dump_file)
1344 fprintf (dump_file, "Function found to be %sconst: %s\n",
1345 this_looping ? "looping " : "",
1346 w->name ());
1348 w->set_const_flag (true, this_looping);
1349 break;
1351 case IPA_PURE:
1352 if (!DECL_PURE_P (w->decl))
1354 warn_function_pure (w->decl, !this_looping);
1355 if (dump_file)
1356 fprintf (dump_file, "Function found to be %spure: %s\n",
1357 this_looping ? "looping " : "",
1358 w->name ());
1360 w->set_pure_flag (true, this_looping);
1361 break;
1363 default:
1364 break;
1366 w_info = (struct ipa_dfs_info *) w->aux;
1367 w = w_info->next_cycle;
1371 ipa_free_postorder_info ();
1372 free (order);
1375 /* Produce transitive closure over the callgraph and compute nothrow
1376 attributes. */
1378 static void
1379 propagate_nothrow (void)
1381 struct cgraph_node *node;
1382 struct cgraph_node *w;
1383 struct cgraph_node **order =
1384 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1385 int order_pos;
1386 int i;
1387 struct ipa_dfs_info * w_info;
1389 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1390 if (dump_file)
1392 cgraph_node::dump_cgraph (dump_file);
1393 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1396 /* Propagate the local information through the call graph to produce
1397 the global information. All the nodes within a cycle will have
1398 the same info so we collapse cycles first. Then we can do the
1399 propagation in one pass from the leaves to the roots. */
1400 for (i = 0; i < order_pos; i++ )
1402 bool can_throw = false;
1403 node = order[i];
1405 if (node->alias)
1406 continue;
1408 /* Find the worst state for any node in the cycle. */
1409 w = node;
1410 while (w)
1412 struct cgraph_edge *e, *ie;
1413 funct_state w_l = get_function_state (w);
1415 if (w_l->can_throw
1416 || w->get_availability () == AVAIL_INTERPOSABLE)
1417 can_throw = true;
1419 if (can_throw)
1420 break;
1422 for (e = w->callees; e; e = e->next_callee)
1424 enum availability avail;
1425 struct cgraph_node *y = e->callee->function_symbol (&avail);
1427 if (avail > AVAIL_INTERPOSABLE)
1429 funct_state y_l = get_function_state (y);
1431 if (can_throw)
1432 break;
1433 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1434 && e->can_throw_external)
1435 can_throw = true;
1437 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1438 can_throw = true;
1440 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1441 if (ie->can_throw_external)
1443 can_throw = true;
1444 break;
1446 w_info = (struct ipa_dfs_info *) w->aux;
1447 w = w_info->next_cycle;
1450 /* Copy back the region's pure_const_state which is shared by
1451 all nodes in the region. */
1452 w = node;
1453 while (w)
1455 funct_state w_l = get_function_state (w);
1456 if (!can_throw && !TREE_NOTHROW (w->decl))
1458 /* Inline clones share declaration with their offline copies;
1459 do not modify their declarations since the offline copy may
1460 be different. */
1461 if (!w->global.inlined_to)
1463 w->set_nothrow_flag (true);
1464 if (dump_file)
1465 fprintf (dump_file, "Function found to be nothrow: %s\n",
1466 w->name ());
1469 else if (can_throw && !TREE_NOTHROW (w->decl))
1470 w_l->can_throw = true;
1471 w_info = (struct ipa_dfs_info *) w->aux;
1472 w = w_info->next_cycle;
1476 ipa_free_postorder_info ();
1477 free (order);
1481 /* Produce the global information by preforming a transitive closure
1482 on the local information that was produced by generate_summary. */
1484 static unsigned int
1485 propagate (void)
1487 struct cgraph_node *node;
1489 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1490 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1491 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1493 /* Nothrow makes more function to not lead to return and improve
1494 later analysis. */
1495 propagate_nothrow ();
1496 propagate_pure_const ();
1498 /* Cleanup. */
1499 FOR_EACH_FUNCTION (node)
1500 if (has_function_state (node))
1501 free (get_function_state (node));
1502 funct_state_vec.release ();
1503 return 0;
1506 static bool
1507 gate_pure_const (void)
1509 return (flag_ipa_pure_const
1510 /* Don't bother doing anything if the program has errors. */
1511 && !seen_error ());
1514 namespace {
1516 const pass_data pass_data_ipa_pure_const =
1518 IPA_PASS, /* type */
1519 "pure-const", /* name */
1520 OPTGROUP_NONE, /* optinfo_flags */
1521 TV_IPA_PURE_CONST, /* tv_id */
1522 0, /* properties_required */
1523 0, /* properties_provided */
1524 0, /* properties_destroyed */
1525 0, /* todo_flags_start */
1526 0, /* todo_flags_finish */
1529 class pass_ipa_pure_const : public ipa_opt_pass_d
1531 public:
1532 pass_ipa_pure_const (gcc::context *ctxt)
1533 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1534 pure_const_generate_summary, /* generate_summary */
1535 pure_const_write_summary, /* write_summary */
1536 pure_const_read_summary, /* read_summary */
1537 NULL, /* write_optimization_summary */
1538 NULL, /* read_optimization_summary */
1539 NULL, /* stmt_fixup */
1540 0, /* function_transform_todo_flags_start */
1541 NULL, /* function_transform */
1542 NULL) /* variable_transform */
1545 /* opt_pass methods: */
1546 virtual bool gate (function *) { return gate_pure_const (); }
1547 virtual unsigned int execute (function *) { return propagate (); }
1549 }; // class pass_ipa_pure_const
1551 } // anon namespace
1553 ipa_opt_pass_d *
1554 make_pass_ipa_pure_const (gcc::context *ctxt)
1556 return new pass_ipa_pure_const (ctxt);
1559 /* Return true if function should be skipped for local pure const analysis. */
1561 static bool
1562 skip_function_for_local_pure_const (struct cgraph_node *node)
1564 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1565 we must not promote functions that are called by already processed functions. */
1567 if (function_called_by_processed_nodes_p ())
1569 if (dump_file)
1570 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1571 return true;
1573 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1575 if (dump_file)
1576 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1577 return true;
1579 return false;
1582 /* Simple local pass for pure const discovery reusing the analysis from
1583 ipa_pure_const. This pass is effective when executed together with
1584 other optimization passes in early optimization pass queue. */
1586 namespace {
1588 const pass_data pass_data_local_pure_const =
1590 GIMPLE_PASS, /* type */
1591 "local-pure-const", /* name */
1592 OPTGROUP_NONE, /* optinfo_flags */
1593 TV_IPA_PURE_CONST, /* tv_id */
1594 0, /* properties_required */
1595 0, /* properties_provided */
1596 0, /* properties_destroyed */
1597 0, /* todo_flags_start */
1598 0, /* todo_flags_finish */
1601 class pass_local_pure_const : public gimple_opt_pass
1603 public:
1604 pass_local_pure_const (gcc::context *ctxt)
1605 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1608 /* opt_pass methods: */
1609 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1610 virtual bool gate (function *) { return gate_pure_const (); }
1611 virtual unsigned int execute (function *);
1613 }; // class pass_local_pure_const
1615 unsigned int
1616 pass_local_pure_const::execute (function *fun)
1618 bool changed = false;
1619 funct_state l;
1620 bool skip;
1621 struct cgraph_node *node;
1623 node = cgraph_node::get (current_function_decl);
1624 skip = skip_function_for_local_pure_const (node);
1625 if (!warn_suggest_attribute_const
1626 && !warn_suggest_attribute_pure
1627 && skip)
1628 return 0;
1630 l = analyze_function (node, false);
1632 /* Do NORETURN discovery. */
1633 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1634 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1636 warn_function_noreturn (fun->decl);
1637 if (dump_file)
1638 fprintf (dump_file, "Function found to be noreturn: %s\n",
1639 current_function_name ());
1641 /* Update declaration and reduce profile to executed once. */
1642 TREE_THIS_VOLATILE (current_function_decl) = 1;
1643 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1644 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1646 changed = true;
1649 switch (l->pure_const_state)
1651 case IPA_CONST:
1652 if (!TREE_READONLY (current_function_decl))
1654 warn_function_const (current_function_decl, !l->looping);
1655 if (!skip)
1657 node->set_const_flag (true, l->looping);
1658 changed = true;
1660 if (dump_file)
1661 fprintf (dump_file, "Function found to be %sconst: %s\n",
1662 l->looping ? "looping " : "",
1663 current_function_name ());
1665 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1666 && !l->looping)
1668 if (!skip)
1670 node->set_const_flag (true, false);
1671 changed = true;
1673 if (dump_file)
1674 fprintf (dump_file, "Function found to be non-looping: %s\n",
1675 current_function_name ());
1677 break;
1679 case IPA_PURE:
1680 if (!DECL_PURE_P (current_function_decl))
1682 if (!skip)
1684 node->set_pure_flag (true, l->looping);
1685 changed = true;
1687 warn_function_pure (current_function_decl, !l->looping);
1688 if (dump_file)
1689 fprintf (dump_file, "Function found to be %spure: %s\n",
1690 l->looping ? "looping " : "",
1691 current_function_name ());
1693 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1694 && !l->looping)
1696 if (!skip)
1698 node->set_pure_flag (true, false);
1699 changed = true;
1701 if (dump_file)
1702 fprintf (dump_file, "Function found to be non-looping: %s\n",
1703 current_function_name ());
1705 break;
1707 default:
1708 break;
1710 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1712 node->set_nothrow_flag (true);
1713 changed = true;
1714 if (dump_file)
1715 fprintf (dump_file, "Function found to be nothrow: %s\n",
1716 current_function_name ());
1718 free (l);
1719 if (changed)
1720 return execute_fixup_cfg ();
1721 else
1722 return 0;
1725 } // anon namespace
1727 gimple_opt_pass *
1728 make_pass_local_pure_const (gcc::context *ctxt)
1730 return new pass_local_pure_const (ctxt);
1733 /* Emit noreturn warnings. */
1735 namespace {
1737 const pass_data pass_data_warn_function_noreturn =
1739 GIMPLE_PASS, /* type */
1740 "*warn_function_noreturn", /* name */
1741 OPTGROUP_NONE, /* optinfo_flags */
1742 TV_NONE, /* tv_id */
1743 PROP_cfg, /* properties_required */
1744 0, /* properties_provided */
1745 0, /* properties_destroyed */
1746 0, /* todo_flags_start */
1747 0, /* todo_flags_finish */
1750 class pass_warn_function_noreturn : public gimple_opt_pass
1752 public:
1753 pass_warn_function_noreturn (gcc::context *ctxt)
1754 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1757 /* opt_pass methods: */
1758 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1759 virtual unsigned int execute (function *fun)
1761 if (!TREE_THIS_VOLATILE (current_function_decl)
1762 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1763 warn_function_noreturn (current_function_decl);
1764 return 0;
1767 }; // class pass_warn_function_noreturn
1769 } // anon namespace
1771 gimple_opt_pass *
1772 make_pass_warn_function_noreturn (gcc::context *ctxt)
1774 return new pass_warn_function_noreturn (ctxt);