2013-11-08 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / ipa-pure-const.c
blob50bf500b1728c7a82e7fade45563a89d6797d48a
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 "gimple.h"
40 #include "tree-cfg.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-inline.h"
43 #include "tree-pass.h"
44 #include "langhooks.h"
45 #include "pointer-set.h"
46 #include "ggc.h"
47 #include "ipa-utils.h"
48 #include "flags.h"
49 #include "diagnostic.h"
50 #include "gimple-pretty-print.h"
51 #include "langhooks.h"
52 #include "target.h"
53 #include "lto-streamer.h"
54 #include "data-streamer.h"
55 #include "tree-streamer.h"
56 #include "cfgloop.h"
57 #include "tree-scalar-evolution.h"
58 #include "intl.h"
59 #include "opts.h"
61 static struct pointer_set_t *visited_nodes;
63 /* Lattice values for const and pure functions. Everything starts out
64 being const, then may drop to pure and then neither depending on
65 what is found. */
66 enum pure_const_state_e
68 IPA_CONST,
69 IPA_PURE,
70 IPA_NEITHER
73 const char *pure_const_names[3] = {"const", "pure", "neither"};
75 /* Holder for the const_state. There is one of these per function
76 decl. */
77 struct funct_state_d
79 /* See above. */
80 enum pure_const_state_e pure_const_state;
81 /* What user set here; we can be always sure about this. */
82 enum pure_const_state_e state_previously_known;
83 bool looping_previously_known;
85 /* True if the function could possibly infinite loop. There are a
86 lot of ways that this could be determined. We are pretty
87 conservative here. While it is possible to cse pure and const
88 calls, it is not legal to have dce get rid of the call if there
89 is a possibility that the call could infinite loop since this is
90 a behavioral change. */
91 bool looping;
93 bool can_throw;
96 /* State used when we know nothing about function. */
97 static struct funct_state_d varying_state
98 = { IPA_NEITHER, IPA_NEITHER, true, true, true };
101 typedef struct funct_state_d * funct_state;
103 /* The storage of the funct_state is abstracted because there is the
104 possibility that it may be desirable to move this to the cgraph
105 local info. */
107 /* Array, indexed by cgraph node uid, of function states. */
109 static vec<funct_state> funct_state_vec;
111 /* Holders of ipa cgraph hooks: */
112 static struct cgraph_node_hook_list *function_insertion_hook_holder;
113 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
114 static struct cgraph_node_hook_list *node_removal_hook_holder;
116 /* Try to guess if function body will always be visible to compiler
117 when compiling the call and whether compiler will be able
118 to propagate the information by itself. */
120 static bool
121 function_always_visible_to_compiler_p (tree decl)
123 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
126 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
127 is true if the function is known to be finite. The diagnostic is
128 controlled by OPTION. WARNED_ABOUT is a pointer_set unique for
129 OPTION, this function may initialize it and it is always returned
130 by the function. */
132 static struct pointer_set_t *
133 suggest_attribute (int option, tree decl, bool known_finite,
134 struct pointer_set_t *warned_about,
135 const char * attrib_name)
137 if (!option_enabled (option, &global_options))
138 return warned_about;
139 if (TREE_THIS_VOLATILE (decl)
140 || (known_finite && function_always_visible_to_compiler_p (decl)))
141 return warned_about;
143 if (!warned_about)
144 warned_about = pointer_set_create ();
145 if (pointer_set_contains (warned_about, decl))
146 return warned_about;
147 pointer_set_insert (warned_about, decl);
148 warning_at (DECL_SOURCE_LOCATION (decl),
149 option,
150 known_finite
151 ? _("function might be candidate for attribute %<%s%>")
152 : _("function might be candidate for attribute %<%s%>"
153 " if it is known to return normally"), attrib_name);
154 return warned_about;
157 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
158 is true if the function is known to be finite. */
160 static void
161 warn_function_pure (tree decl, bool known_finite)
163 static struct pointer_set_t *warned_about;
165 warned_about
166 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
167 known_finite, warned_about, "pure");
170 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
171 is true if the function is known to be finite. */
173 static void
174 warn_function_const (tree decl, bool known_finite)
176 static struct pointer_set_t *warned_about;
177 warned_about
178 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
179 known_finite, warned_about, "const");
182 static void
183 warn_function_noreturn (tree decl)
185 static struct pointer_set_t *warned_about;
186 if (!lang_hooks.missing_noreturn_ok_p (decl)
187 && targetm.warn_func_return (decl))
188 warned_about
189 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
190 true, warned_about, "noreturn");
193 /* Return true if we have a function state for NODE. */
195 static inline bool
196 has_function_state (struct cgraph_node *node)
198 if (!funct_state_vec.exists ()
199 || funct_state_vec.length () <= (unsigned int)node->uid)
200 return false;
201 return funct_state_vec[node->uid] != NULL;
204 /* Return the function state from NODE. */
206 static inline funct_state
207 get_function_state (struct cgraph_node *node)
209 if (!funct_state_vec.exists ()
210 || funct_state_vec.length () <= (unsigned int)node->uid
211 || !funct_state_vec[node->uid])
212 /* We might want to put correct previously_known state into varying. */
213 return &varying_state;
214 return funct_state_vec[node->uid];
217 /* Set the function state S for NODE. */
219 static inline void
220 set_function_state (struct cgraph_node *node, funct_state s)
222 if (!funct_state_vec.exists ()
223 || funct_state_vec.length () <= (unsigned int)node->uid)
224 funct_state_vec.safe_grow_cleared (node->uid + 1);
225 funct_state_vec[node->uid] = s;
228 /* Check to see if the use (or definition when CHECKING_WRITE is true)
229 variable T is legal in a function that is either pure or const. */
231 static inline void
232 check_decl (funct_state local,
233 tree t, bool checking_write, bool ipa)
235 /* Do not want to do anything with volatile except mark any
236 function that uses one to be not const or pure. */
237 if (TREE_THIS_VOLATILE (t))
239 local->pure_const_state = IPA_NEITHER;
240 if (dump_file)
241 fprintf (dump_file, " Volatile operand is not const/pure");
242 return;
245 /* Do not care about a local automatic that is not static. */
246 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
247 return;
249 /* If the variable has the "used" attribute, treat it as if it had a
250 been touched by the devil. */
251 if (DECL_PRESERVE_P (t))
253 local->pure_const_state = IPA_NEITHER;
254 if (dump_file)
255 fprintf (dump_file, " Used static/global variable is not const/pure\n");
256 return;
259 /* In IPA mode we are not interested in checking actual loads and stores;
260 they will be processed at propagation time using ipa_ref. */
261 if (ipa)
262 return;
264 /* Since we have dealt with the locals and params cases above, if we
265 are CHECKING_WRITE, this cannot be a pure or constant
266 function. */
267 if (checking_write)
269 local->pure_const_state = IPA_NEITHER;
270 if (dump_file)
271 fprintf (dump_file, " static/global memory write is not const/pure\n");
272 return;
275 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
277 /* Readonly reads are safe. */
278 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
279 return; /* Read of a constant, do not change the function state. */
280 else
282 if (dump_file)
283 fprintf (dump_file, " global memory read is not const\n");
284 /* Just a regular read. */
285 if (local->pure_const_state == IPA_CONST)
286 local->pure_const_state = IPA_PURE;
289 else
291 /* Compilation level statics can be read if they are readonly
292 variables. */
293 if (TREE_READONLY (t))
294 return;
296 if (dump_file)
297 fprintf (dump_file, " static memory read is not const\n");
298 /* Just a regular read. */
299 if (local->pure_const_state == IPA_CONST)
300 local->pure_const_state = IPA_PURE;
305 /* Check to see if the use (or definition when CHECKING_WRITE is true)
306 variable T is legal in a function that is either pure or const. */
308 static inline void
309 check_op (funct_state local, tree t, bool checking_write)
311 t = get_base_address (t);
312 if (t && TREE_THIS_VOLATILE (t))
314 local->pure_const_state = IPA_NEITHER;
315 if (dump_file)
316 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
317 return;
319 else if (t
320 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
321 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
322 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
324 if (dump_file)
325 fprintf (dump_file, " Indirect ref to local memory is OK\n");
326 return;
328 else if (checking_write)
330 local->pure_const_state = IPA_NEITHER;
331 if (dump_file)
332 fprintf (dump_file, " Indirect ref write is not const/pure\n");
333 return;
335 else
337 if (dump_file)
338 fprintf (dump_file, " Indirect ref read is not const\n");
339 if (local->pure_const_state == IPA_CONST)
340 local->pure_const_state = IPA_PURE;
344 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
346 static void
347 state_from_flags (enum pure_const_state_e *state, bool *looping,
348 int flags, bool cannot_lead_to_return)
350 *looping = false;
351 if (flags & ECF_LOOPING_CONST_OR_PURE)
353 *looping = true;
354 if (dump_file && (dump_flags & TDF_DETAILS))
355 fprintf (dump_file, " looping");
357 if (flags & ECF_CONST)
359 *state = IPA_CONST;
360 if (dump_file && (dump_flags & TDF_DETAILS))
361 fprintf (dump_file, " const\n");
363 else if (flags & ECF_PURE)
365 *state = IPA_PURE;
366 if (dump_file && (dump_flags & TDF_DETAILS))
367 fprintf (dump_file, " pure\n");
369 else if (cannot_lead_to_return)
371 *state = IPA_PURE;
372 *looping = true;
373 if (dump_file && (dump_flags & TDF_DETAILS))
374 fprintf (dump_file, " ignoring side effects->pure looping\n");
376 else
378 if (dump_file && (dump_flags & TDF_DETAILS))
379 fprintf (dump_file, " neither\n");
380 *state = IPA_NEITHER;
381 *looping = true;
385 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
386 into STATE and LOOPING better of the two variants.
387 Be sure to merge looping correctly. IPA_NEITHER functions
388 have looping 0 even if they don't have to return. */
390 static inline void
391 better_state (enum pure_const_state_e *state, bool *looping,
392 enum pure_const_state_e state2, bool looping2)
394 if (state2 < *state)
396 if (*state == IPA_NEITHER)
397 *looping = looping2;
398 else
399 *looping = MIN (*looping, looping2);
401 else if (state2 != IPA_NEITHER)
402 *looping = MIN (*looping, looping2);
405 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
406 into STATE and LOOPING worse of the two variants. */
408 static inline void
409 worse_state (enum pure_const_state_e *state, bool *looping,
410 enum pure_const_state_e state2, bool looping2)
412 *state = MAX (*state, state2);
413 *looping = MAX (*looping, looping2);
416 /* Recognize special cases of builtins that are by themselves not pure or const
417 but function using them is. */
418 static bool
419 special_builtin_state (enum pure_const_state_e *state, bool *looping,
420 tree callee)
422 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
423 switch (DECL_FUNCTION_CODE (callee))
425 case BUILT_IN_RETURN:
426 case BUILT_IN_UNREACHABLE:
427 case BUILT_IN_ALLOCA:
428 case BUILT_IN_ALLOCA_WITH_ALIGN:
429 case BUILT_IN_STACK_SAVE:
430 case BUILT_IN_STACK_RESTORE:
431 case BUILT_IN_EH_POINTER:
432 case BUILT_IN_EH_FILTER:
433 case BUILT_IN_UNWIND_RESUME:
434 case BUILT_IN_CXA_END_CLEANUP:
435 case BUILT_IN_EH_COPY_VALUES:
436 case BUILT_IN_FRAME_ADDRESS:
437 case BUILT_IN_APPLY:
438 case BUILT_IN_APPLY_ARGS:
439 *looping = false;
440 *state = IPA_CONST;
441 return true;
442 case BUILT_IN_PREFETCH:
443 *looping = true;
444 *state = IPA_CONST;
445 return true;
447 return false;
450 /* Check the parameters of a function call to CALL_EXPR to see if
451 there are any references in the parameters that are not allowed for
452 pure or const functions. Also check to see if this is either an
453 indirect call, a call outside the compilation unit, or has special
454 attributes that may also effect the purity. The CALL_EXPR node for
455 the entire call expression. */
457 static void
458 check_call (funct_state local, gimple call, bool ipa)
460 int flags = gimple_call_flags (call);
461 tree callee_t = gimple_call_fndecl (call);
462 bool possibly_throws = stmt_could_throw_p (call);
463 bool possibly_throws_externally = (possibly_throws
464 && stmt_can_throw_external (call));
466 if (possibly_throws)
468 unsigned int i;
469 for (i = 0; i < gimple_num_ops (call); i++)
470 if (gimple_op (call, i)
471 && tree_could_throw_p (gimple_op (call, i)))
473 if (possibly_throws && cfun->can_throw_non_call_exceptions)
475 if (dump_file)
476 fprintf (dump_file, " operand can throw; looping\n");
477 local->looping = true;
479 if (possibly_throws_externally)
481 if (dump_file)
482 fprintf (dump_file, " operand can throw externally\n");
483 local->can_throw = true;
488 /* The const and pure flags are set by a variety of places in the
489 compiler (including here). If someone has already set the flags
490 for the callee, (such as for some of the builtins) we will use
491 them, otherwise we will compute our own information.
493 Const and pure functions have less clobber effects than other
494 functions so we process these first. Otherwise if it is a call
495 outside the compilation unit or an indirect call we punt. This
496 leaves local calls which will be processed by following the call
497 graph. */
498 if (callee_t)
500 enum pure_const_state_e call_state;
501 bool call_looping;
503 if (special_builtin_state (&call_state, &call_looping, callee_t))
505 worse_state (&local->pure_const_state, &local->looping,
506 call_state, call_looping);
507 return;
509 /* When bad things happen to bad functions, they cannot be const
510 or pure. */
511 if (setjmp_call_p (callee_t))
513 if (dump_file)
514 fprintf (dump_file, " setjmp is not const/pure\n");
515 local->looping = true;
516 local->pure_const_state = IPA_NEITHER;
519 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
520 switch (DECL_FUNCTION_CODE (callee_t))
522 case BUILT_IN_LONGJMP:
523 case BUILT_IN_NONLOCAL_GOTO:
524 if (dump_file)
525 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
526 local->pure_const_state = IPA_NEITHER;
527 local->looping = true;
528 break;
529 default:
530 break;
534 /* When not in IPA mode, we can still handle self recursion. */
535 if (!ipa && callee_t
536 && recursive_call_p (current_function_decl, callee_t))
538 if (dump_file)
539 fprintf (dump_file, " Recursive call can loop.\n");
540 local->looping = true;
542 /* Either callee is unknown or we are doing local analysis.
543 Look to see if there are any bits available for the callee (such as by
544 declaration or because it is builtin) and process solely on the basis of
545 those bits. */
546 else if (!ipa)
548 enum pure_const_state_e call_state;
549 bool call_looping;
550 if (possibly_throws && cfun->can_throw_non_call_exceptions)
552 if (dump_file)
553 fprintf (dump_file, " can throw; looping\n");
554 local->looping = true;
556 if (possibly_throws_externally)
558 if (dump_file)
560 fprintf (dump_file, " can throw externally to lp %i\n",
561 lookup_stmt_eh_lp (call));
562 if (callee_t)
563 fprintf (dump_file, " callee:%s\n",
564 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
566 local->can_throw = true;
568 if (dump_file && (dump_flags & TDF_DETAILS))
569 fprintf (dump_file, " checking flags for call:");
570 state_from_flags (&call_state, &call_looping, flags,
571 ((flags & (ECF_NORETURN | ECF_NOTHROW))
572 == (ECF_NORETURN | ECF_NOTHROW))
573 || (!flag_exceptions && (flags & ECF_NORETURN)));
574 worse_state (&local->pure_const_state, &local->looping,
575 call_state, call_looping);
577 /* Direct functions calls are handled by IPA propagation. */
580 /* Wrapper around check_decl for loads in local more. */
582 static bool
583 check_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
585 if (DECL_P (op))
586 check_decl ((funct_state)data, op, false, false);
587 else
588 check_op ((funct_state)data, op, false);
589 return false;
592 /* Wrapper around check_decl for stores in local more. */
594 static bool
595 check_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
597 if (DECL_P (op))
598 check_decl ((funct_state)data, op, true, false);
599 else
600 check_op ((funct_state)data, op, true);
601 return false;
604 /* Wrapper around check_decl for loads in ipa mode. */
606 static bool
607 check_ipa_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
609 if (DECL_P (op))
610 check_decl ((funct_state)data, op, false, true);
611 else
612 check_op ((funct_state)data, op, false);
613 return false;
616 /* Wrapper around check_decl for stores in ipa mode. */
618 static bool
619 check_ipa_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
621 if (DECL_P (op))
622 check_decl ((funct_state)data, op, true, true);
623 else
624 check_op ((funct_state)data, op, true);
625 return false;
628 /* Look into pointer pointed to by GSIP and figure out what interesting side
629 effects it has. */
630 static void
631 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
633 gimple stmt = gsi_stmt (*gsip);
635 if (is_gimple_debug (stmt))
636 return;
638 if (dump_file)
640 fprintf (dump_file, " scanning: ");
641 print_gimple_stmt (dump_file, stmt, 0, 0);
644 if (gimple_has_volatile_ops (stmt)
645 && !gimple_clobber_p (stmt))
647 local->pure_const_state = IPA_NEITHER;
648 if (dump_file)
649 fprintf (dump_file, " Volatile stmt is not const/pure\n");
652 /* Look for loads and stores. */
653 walk_stmt_load_store_ops (stmt, local,
654 ipa ? check_ipa_load : check_load,
655 ipa ? check_ipa_store : check_store);
657 if (gimple_code (stmt) != GIMPLE_CALL
658 && stmt_could_throw_p (stmt))
660 if (cfun->can_throw_non_call_exceptions)
662 if (dump_file)
663 fprintf (dump_file, " can throw; looping\n");
664 local->looping = true;
666 if (stmt_can_throw_external (stmt))
668 if (dump_file)
669 fprintf (dump_file, " can throw externally\n");
670 local->can_throw = true;
672 else
673 if (dump_file)
674 fprintf (dump_file, " can throw\n");
676 switch (gimple_code (stmt))
678 case GIMPLE_CALL:
679 check_call (local, stmt, ipa);
680 break;
681 case GIMPLE_LABEL:
682 if (DECL_NONLOCAL (gimple_label_label (stmt)))
683 /* Target of long jump. */
685 if (dump_file)
686 fprintf (dump_file, " nonlocal label is not const/pure\n");
687 local->pure_const_state = IPA_NEITHER;
689 break;
690 case GIMPLE_ASM:
691 if (gimple_asm_clobbers_memory_p (stmt))
693 if (dump_file)
694 fprintf (dump_file, " memory asm clobber is not const/pure\n");
695 /* Abandon all hope, ye who enter here. */
696 local->pure_const_state = IPA_NEITHER;
698 if (gimple_asm_volatile_p (stmt))
700 if (dump_file)
701 fprintf (dump_file, " volatile is not const/pure\n");
702 /* Abandon all hope, ye who enter here. */
703 local->pure_const_state = IPA_NEITHER;
704 local->looping = true;
706 return;
707 default:
708 break;
713 /* This is the main routine for finding the reference patterns for
714 global variables within a function FN. */
716 static funct_state
717 analyze_function (struct cgraph_node *fn, bool ipa)
719 tree decl = fn->decl;
720 funct_state l;
721 basic_block this_block;
723 l = XCNEW (struct funct_state_d);
724 l->pure_const_state = IPA_CONST;
725 l->state_previously_known = IPA_NEITHER;
726 l->looping_previously_known = true;
727 l->looping = false;
728 l->can_throw = false;
729 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
730 flags_from_decl_or_type (fn->decl),
731 cgraph_node_cannot_return (fn));
733 if (fn->thunk.thunk_p || fn->alias)
735 /* Thunk gets propagated through, so nothing interesting happens. */
736 gcc_assert (ipa);
737 return l;
740 if (dump_file)
742 fprintf (dump_file, "\n\n local analysis of %s\n ",
743 cgraph_node_name (fn));
746 push_cfun (DECL_STRUCT_FUNCTION (decl));
748 FOR_EACH_BB (this_block)
750 gimple_stmt_iterator gsi;
751 struct walk_stmt_info wi;
753 memset (&wi, 0, sizeof (wi));
754 for (gsi = gsi_start_bb (this_block);
755 !gsi_end_p (gsi);
756 gsi_next (&gsi))
758 check_stmt (&gsi, l, ipa);
759 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
760 goto end;
764 end:
765 if (l->pure_const_state != IPA_NEITHER)
767 /* Const functions cannot have back edges (an
768 indication of possible infinite loop side
769 effect. */
770 if (mark_dfs_back_edges ())
772 /* Preheaders are needed for SCEV to work.
773 Simple latches and recorded exits improve chances that loop will
774 proved to be finite in testcases such as in loop-15.c
775 and loop-24.c */
776 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
777 | LOOPS_HAVE_SIMPLE_LATCHES
778 | LOOPS_HAVE_RECORDED_EXITS);
779 if (dump_file && (dump_flags & TDF_DETAILS))
780 flow_loops_dump (dump_file, NULL, 0);
781 if (mark_irreducible_loops ())
783 if (dump_file)
784 fprintf (dump_file, " has irreducible loops\n");
785 l->looping = true;
787 else
789 loop_iterator li;
790 struct loop *loop;
791 scev_initialize ();
792 FOR_EACH_LOOP (li, loop, 0)
793 if (!finite_loop_p (loop))
795 if (dump_file)
796 fprintf (dump_file, " can not prove finiteness of "
797 "loop %i\n", loop->num);
798 l->looping =true;
799 FOR_EACH_LOOP_BREAK (li);
801 scev_finalize ();
803 loop_optimizer_finalize ();
807 if (dump_file && (dump_flags & TDF_DETAILS))
808 fprintf (dump_file, " checking previously known:");
810 better_state (&l->pure_const_state, &l->looping,
811 l->state_previously_known,
812 l->looping_previously_known);
813 if (TREE_NOTHROW (decl))
814 l->can_throw = false;
816 pop_cfun ();
817 if (dump_file)
819 if (l->looping)
820 fprintf (dump_file, "Function is locally looping.\n");
821 if (l->can_throw)
822 fprintf (dump_file, "Function is locally throwing.\n");
823 if (l->pure_const_state == IPA_CONST)
824 fprintf (dump_file, "Function is locally const.\n");
825 if (l->pure_const_state == IPA_PURE)
826 fprintf (dump_file, "Function is locally pure.\n");
828 return l;
831 /* Called when new function is inserted to callgraph late. */
832 static void
833 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
835 if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
836 return;
837 /* There are some shared nodes, in particular the initializers on
838 static declarations. We do not need to scan them more than once
839 since all we would be interested in are the addressof
840 operations. */
841 visited_nodes = pointer_set_create ();
842 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
843 set_function_state (node, analyze_function (node, true));
844 pointer_set_destroy (visited_nodes);
845 visited_nodes = NULL;
848 /* Called when new clone is inserted to callgraph late. */
850 static void
851 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
852 void *data ATTRIBUTE_UNUSED)
854 if (has_function_state (src))
856 funct_state l = XNEW (struct funct_state_d);
857 gcc_assert (!has_function_state (dst));
858 memcpy (l, get_function_state (src), sizeof (*l));
859 set_function_state (dst, l);
863 /* Called when new clone is inserted to callgraph late. */
865 static void
866 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
868 if (has_function_state (node))
870 funct_state l = get_function_state (node);
871 if (l != &varying_state)
872 free (l);
873 set_function_state (node, NULL);
878 static void
879 register_hooks (void)
881 static bool init_p = false;
883 if (init_p)
884 return;
886 init_p = true;
888 node_removal_hook_holder =
889 cgraph_add_node_removal_hook (&remove_node_data, NULL);
890 node_duplication_hook_holder =
891 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
892 function_insertion_hook_holder =
893 cgraph_add_function_insertion_hook (&add_new_function, NULL);
897 /* Analyze each function in the cgraph to see if it is locally PURE or
898 CONST. */
900 static void
901 pure_const_generate_summary (void)
903 struct cgraph_node *node;
905 register_hooks ();
907 /* There are some shared nodes, in particular the initializers on
908 static declarations. We do not need to scan them more than once
909 since all we would be interested in are the addressof
910 operations. */
911 visited_nodes = pointer_set_create ();
913 /* Process all of the functions.
915 We process AVAIL_OVERWRITABLE functions. We can not use the results
916 by default, but the info can be used at LTO with -fwhole-program or
917 when function got cloned and the clone is AVAILABLE. */
919 FOR_EACH_DEFINED_FUNCTION (node)
920 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
921 set_function_state (node, analyze_function (node, true));
923 pointer_set_destroy (visited_nodes);
924 visited_nodes = NULL;
928 /* Serialize the ipa info for lto. */
930 static void
931 pure_const_write_summary (void)
933 struct cgraph_node *node;
934 struct lto_simple_output_block *ob
935 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
936 unsigned int count = 0;
937 lto_symtab_encoder_iterator lsei;
938 lto_symtab_encoder_t encoder;
940 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
942 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
943 lsei_next_function_in_partition (&lsei))
945 node = lsei_cgraph_node (lsei);
946 if (node->definition && has_function_state (node))
947 count++;
950 streamer_write_uhwi_stream (ob->main_stream, count);
952 /* Process all of the functions. */
953 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
954 lsei_next_function_in_partition (&lsei))
956 node = lsei_cgraph_node (lsei);
957 if (node->definition && has_function_state (node))
959 struct bitpack_d bp;
960 funct_state fs;
961 int node_ref;
962 lto_symtab_encoder_t encoder;
964 fs = get_function_state (node);
966 encoder = ob->decl_state->symtab_node_encoder;
967 node_ref = lto_symtab_encoder_encode (encoder, node);
968 streamer_write_uhwi_stream (ob->main_stream, node_ref);
970 /* Note that flags will need to be read in the opposite
971 order as we are pushing the bitflags into FLAGS. */
972 bp = bitpack_create (ob->main_stream);
973 bp_pack_value (&bp, fs->pure_const_state, 2);
974 bp_pack_value (&bp, fs->state_previously_known, 2);
975 bp_pack_value (&bp, fs->looping_previously_known, 1);
976 bp_pack_value (&bp, fs->looping, 1);
977 bp_pack_value (&bp, fs->can_throw, 1);
978 streamer_write_bitpack (&bp);
982 lto_destroy_simple_output_block (ob);
986 /* Deserialize the ipa info for lto. */
988 static void
989 pure_const_read_summary (void)
991 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
992 struct lto_file_decl_data *file_data;
993 unsigned int j = 0;
995 register_hooks ();
996 while ((file_data = file_data_vec[j++]))
998 const char *data;
999 size_t len;
1000 struct lto_input_block *ib
1001 = lto_create_simple_input_block (file_data,
1002 LTO_section_ipa_pure_const,
1003 &data, &len);
1004 if (ib)
1006 unsigned int i;
1007 unsigned int count = streamer_read_uhwi (ib);
1009 for (i = 0; i < count; i++)
1011 unsigned int index;
1012 struct cgraph_node *node;
1013 struct bitpack_d bp;
1014 funct_state fs;
1015 lto_symtab_encoder_t encoder;
1017 fs = XCNEW (struct funct_state_d);
1018 index = streamer_read_uhwi (ib);
1019 encoder = file_data->symtab_node_encoder;
1020 node = cgraph (lto_symtab_encoder_deref (encoder, index));
1021 set_function_state (node, fs);
1023 /* Note that the flags must be read in the opposite
1024 order in which they were written (the bitflags were
1025 pushed into FLAGS). */
1026 bp = streamer_read_bitpack (ib);
1027 fs->pure_const_state
1028 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1029 fs->state_previously_known
1030 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1031 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1032 fs->looping = bp_unpack_value (&bp, 1);
1033 fs->can_throw = bp_unpack_value (&bp, 1);
1034 if (dump_file)
1036 int flags = flags_from_decl_or_type (node->decl);
1037 fprintf (dump_file, "Read info for %s/%i ",
1038 cgraph_node_name (node),
1039 node->order);
1040 if (flags & ECF_CONST)
1041 fprintf (dump_file, " const");
1042 if (flags & ECF_PURE)
1043 fprintf (dump_file, " pure");
1044 if (flags & ECF_NOTHROW)
1045 fprintf (dump_file, " nothrow");
1046 fprintf (dump_file, "\n pure const state: %s\n",
1047 pure_const_names[fs->pure_const_state]);
1048 fprintf (dump_file, " previously known state: %s\n",
1049 pure_const_names[fs->looping_previously_known]);
1050 if (fs->looping)
1051 fprintf (dump_file," function is locally looping\n");
1052 if (fs->looping_previously_known)
1053 fprintf (dump_file," function is previously known looping\n");
1054 if (fs->can_throw)
1055 fprintf (dump_file," function is locally throwing\n");
1059 lto_destroy_simple_input_block (file_data,
1060 LTO_section_ipa_pure_const,
1061 ib, data, len);
1067 static bool
1068 ignore_edge (struct cgraph_edge *e)
1070 return (!e->can_throw_external);
1073 /* Return true if NODE is self recursive function.
1074 Indirectly recursive functions appears as non-trivial strongly
1075 connected components, so we need to care about self recursion
1076 only. */
1078 static bool
1079 self_recursive_p (struct cgraph_node *node)
1081 struct cgraph_edge *e;
1082 for (e = node->callees; e; e = e->next_callee)
1083 if (cgraph_function_node (e->callee, NULL) == node)
1084 return true;
1085 return false;
1088 /* Produce transitive closure over the callgraph and compute pure/const
1089 attributes. */
1091 static void
1092 propagate_pure_const (void)
1094 struct cgraph_node *node;
1095 struct cgraph_node *w;
1096 struct cgraph_node **order =
1097 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1098 int order_pos;
1099 int i;
1100 struct ipa_dfs_info * w_info;
1102 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1103 if (dump_file)
1105 dump_cgraph (dump_file);
1106 ipa_print_order (dump_file, "reduced", order, order_pos);
1109 /* Propagate the local information through the call graph to produce
1110 the global information. All the nodes within a cycle will have
1111 the same info so we collapse cycles first. Then we can do the
1112 propagation in one pass from the leaves to the roots. */
1113 for (i = 0; i < order_pos; i++ )
1115 enum pure_const_state_e pure_const_state = IPA_CONST;
1116 bool looping = false;
1117 int count = 0;
1118 node = order[i];
1120 if (node->alias)
1121 continue;
1123 if (dump_file && (dump_flags & TDF_DETAILS))
1124 fprintf (dump_file, "Starting cycle\n");
1126 /* Find the worst state for any node in the cycle. */
1127 w = node;
1128 while (w && pure_const_state != IPA_NEITHER)
1130 struct cgraph_edge *e;
1131 struct cgraph_edge *ie;
1132 int i;
1133 struct ipa_ref *ref;
1135 funct_state w_l = get_function_state (w);
1136 if (dump_file && (dump_flags & TDF_DETAILS))
1137 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1138 cgraph_node_name (w),
1139 w->order,
1140 pure_const_names[w_l->pure_const_state],
1141 w_l->looping);
1143 /* First merge in function body properties. */
1144 worse_state (&pure_const_state, &looping,
1145 w_l->pure_const_state, w_l->looping);
1146 if (pure_const_state == IPA_NEITHER)
1147 break;
1149 /* For overwritable nodes we can not assume anything. */
1150 if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1152 worse_state (&pure_const_state, &looping,
1153 w_l->state_previously_known,
1154 w_l->looping_previously_known);
1155 if (dump_file && (dump_flags & TDF_DETAILS))
1157 fprintf (dump_file,
1158 " Overwritable. state %s looping %i\n",
1159 pure_const_names[w_l->state_previously_known],
1160 w_l->looping_previously_known);
1162 break;
1165 count++;
1167 /* We consider recursive cycles as possibly infinite.
1168 This might be relaxed since infinite recursion leads to stack
1169 overflow. */
1170 if (count > 1)
1171 looping = true;
1173 /* Now walk the edges and merge in callee properties. */
1174 for (e = w->callees; e; e = e->next_callee)
1176 enum availability avail;
1177 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
1178 enum pure_const_state_e edge_state = IPA_CONST;
1179 bool edge_looping = false;
1181 if (dump_file && (dump_flags & TDF_DETAILS))
1183 fprintf (dump_file,
1184 " Call to %s/%i",
1185 cgraph_node_name (e->callee),
1186 e->callee->order);
1188 if (avail > AVAIL_OVERWRITABLE)
1190 funct_state y_l = get_function_state (y);
1191 if (dump_file && (dump_flags & TDF_DETAILS))
1193 fprintf (dump_file,
1194 " state:%s looping:%i\n",
1195 pure_const_names[y_l->pure_const_state],
1196 y_l->looping);
1198 if (y_l->pure_const_state > IPA_PURE
1199 && cgraph_edge_cannot_lead_to_return (e))
1201 if (dump_file && (dump_flags & TDF_DETAILS))
1202 fprintf (dump_file,
1203 " Ignoring side effects"
1204 " -> pure, looping\n");
1205 edge_state = IPA_PURE;
1206 edge_looping = true;
1208 else
1210 edge_state = y_l->pure_const_state;
1211 edge_looping = y_l->looping;
1214 else if (special_builtin_state (&edge_state, &edge_looping,
1215 y->decl))
1217 else
1218 state_from_flags (&edge_state, &edge_looping,
1219 flags_from_decl_or_type (y->decl),
1220 cgraph_edge_cannot_lead_to_return (e));
1222 /* Merge the results with what we already know. */
1223 better_state (&edge_state, &edge_looping,
1224 w_l->state_previously_known,
1225 w_l->looping_previously_known);
1226 worse_state (&pure_const_state, &looping,
1227 edge_state, edge_looping);
1228 if (pure_const_state == IPA_NEITHER)
1229 break;
1231 if (pure_const_state == IPA_NEITHER)
1232 break;
1234 /* Now process the indirect call. */
1235 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1237 enum pure_const_state_e edge_state = IPA_CONST;
1238 bool edge_looping = false;
1240 if (dump_file && (dump_flags & TDF_DETAILS))
1241 fprintf (dump_file, " Indirect call");
1242 state_from_flags (&edge_state, &edge_looping,
1243 ie->indirect_info->ecf_flags,
1244 cgraph_edge_cannot_lead_to_return (ie));
1245 /* Merge the results with what we already know. */
1246 better_state (&edge_state, &edge_looping,
1247 w_l->state_previously_known,
1248 w_l->looping_previously_known);
1249 worse_state (&pure_const_state, &looping,
1250 edge_state, edge_looping);
1251 if (pure_const_state == IPA_NEITHER)
1252 break;
1254 if (pure_const_state == IPA_NEITHER)
1255 break;
1257 /* And finally all loads and stores. */
1258 for (i = 0; ipa_ref_list_reference_iterate (&w->ref_list, i, ref); i++)
1260 enum pure_const_state_e ref_state = IPA_CONST;
1261 bool ref_looping = false;
1262 switch (ref->use)
1264 case IPA_REF_LOAD:
1265 /* readonly reads are safe. */
1266 if (TREE_READONLY (ipa_ref_varpool_node (ref)->decl))
1267 break;
1268 if (dump_file && (dump_flags & TDF_DETAILS))
1269 fprintf (dump_file, " nonreadonly global var read\n");
1270 ref_state = IPA_PURE;
1271 break;
1272 case IPA_REF_STORE:
1273 if (ipa_ref_cannot_lead_to_return (ref))
1274 break;
1275 ref_state = IPA_NEITHER;
1276 if (dump_file && (dump_flags & TDF_DETAILS))
1277 fprintf (dump_file, " global var write\n");
1278 break;
1279 case IPA_REF_ADDR:
1280 break;
1282 better_state (&ref_state, &ref_looping,
1283 w_l->state_previously_known,
1284 w_l->looping_previously_known);
1285 worse_state (&pure_const_state, &looping,
1286 ref_state, ref_looping);
1287 if (pure_const_state == IPA_NEITHER)
1288 break;
1290 w_info = (struct ipa_dfs_info *) w->aux;
1291 w = w_info->next_cycle;
1293 if (dump_file && (dump_flags & TDF_DETAILS))
1294 fprintf (dump_file, "Result %s looping %i\n",
1295 pure_const_names [pure_const_state],
1296 looping);
1298 /* Copy back the region's pure_const_state which is shared by
1299 all nodes in the region. */
1300 w = node;
1301 while (w)
1303 funct_state w_l = get_function_state (w);
1304 enum pure_const_state_e this_state = pure_const_state;
1305 bool this_looping = looping;
1307 if (w_l->state_previously_known != IPA_NEITHER
1308 && this_state > w_l->state_previously_known)
1310 this_state = w_l->state_previously_known;
1311 this_looping |= w_l->looping_previously_known;
1313 if (!this_looping && self_recursive_p (w))
1314 this_looping = true;
1315 if (!w_l->looping_previously_known)
1316 this_looping = false;
1318 /* All nodes within a cycle share the same info. */
1319 w_l->pure_const_state = this_state;
1320 w_l->looping = this_looping;
1322 switch (this_state)
1324 case IPA_CONST:
1325 if (!TREE_READONLY (w->decl))
1327 warn_function_const (w->decl, !this_looping);
1328 if (dump_file)
1329 fprintf (dump_file, "Function found to be %sconst: %s\n",
1330 this_looping ? "looping " : "",
1331 cgraph_node_name (w));
1333 cgraph_set_const_flag (w, true, this_looping);
1334 break;
1336 case IPA_PURE:
1337 if (!DECL_PURE_P (w->decl))
1339 warn_function_pure (w->decl, !this_looping);
1340 if (dump_file)
1341 fprintf (dump_file, "Function found to be %spure: %s\n",
1342 this_looping ? "looping " : "",
1343 cgraph_node_name (w));
1345 cgraph_set_pure_flag (w, true, this_looping);
1346 break;
1348 default:
1349 break;
1351 w_info = (struct ipa_dfs_info *) w->aux;
1352 w = w_info->next_cycle;
1356 ipa_free_postorder_info ();
1357 free (order);
1360 /* Produce transitive closure over the callgraph and compute nothrow
1361 attributes. */
1363 static void
1364 propagate_nothrow (void)
1366 struct cgraph_node *node;
1367 struct cgraph_node *w;
1368 struct cgraph_node **order =
1369 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1370 int order_pos;
1371 int i;
1372 struct ipa_dfs_info * w_info;
1374 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1375 if (dump_file)
1377 dump_cgraph (dump_file);
1378 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1381 /* Propagate the local information through the call graph to produce
1382 the global information. All the nodes within a cycle will have
1383 the same info so we collapse cycles first. Then we can do the
1384 propagation in one pass from the leaves to the roots. */
1385 for (i = 0; i < order_pos; i++ )
1387 bool can_throw = false;
1388 node = order[i];
1390 if (node->alias)
1391 continue;
1393 /* Find the worst state for any node in the cycle. */
1394 w = node;
1395 while (w)
1397 struct cgraph_edge *e, *ie;
1398 funct_state w_l = get_function_state (w);
1400 if (w_l->can_throw
1401 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1402 can_throw = true;
1404 if (can_throw)
1405 break;
1407 for (e = w->callees; e; e = e->next_callee)
1409 enum availability avail;
1410 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
1412 if (avail > AVAIL_OVERWRITABLE)
1414 funct_state y_l = get_function_state (y);
1416 if (can_throw)
1417 break;
1418 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1419 && e->can_throw_external)
1420 can_throw = true;
1422 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1423 can_throw = true;
1425 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1426 if (ie->can_throw_external)
1428 can_throw = true;
1429 break;
1431 w_info = (struct ipa_dfs_info *) w->aux;
1432 w = w_info->next_cycle;
1435 /* Copy back the region's pure_const_state which is shared by
1436 all nodes in the region. */
1437 w = node;
1438 while (w)
1440 funct_state w_l = get_function_state (w);
1441 if (!can_throw && !TREE_NOTHROW (w->decl))
1443 cgraph_set_nothrow_flag (w, true);
1444 if (dump_file)
1445 fprintf (dump_file, "Function found to be nothrow: %s\n",
1446 cgraph_node_name (w));
1448 else if (can_throw && !TREE_NOTHROW (w->decl))
1449 w_l->can_throw = true;
1450 w_info = (struct ipa_dfs_info *) w->aux;
1451 w = w_info->next_cycle;
1455 ipa_free_postorder_info ();
1456 free (order);
1460 /* Produce the global information by preforming a transitive closure
1461 on the local information that was produced by generate_summary. */
1463 static unsigned int
1464 propagate (void)
1466 struct cgraph_node *node;
1468 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1469 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1470 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1472 /* Nothrow makes more function to not lead to return and improve
1473 later analysis. */
1474 propagate_nothrow ();
1475 propagate_pure_const ();
1477 /* Cleanup. */
1478 FOR_EACH_FUNCTION (node)
1479 if (has_function_state (node))
1480 free (get_function_state (node));
1481 funct_state_vec.release ();
1482 return 0;
1485 static bool
1486 gate_pure_const (void)
1488 return (flag_ipa_pure_const
1489 /* Don't bother doing anything if the program has errors. */
1490 && !seen_error ());
1493 namespace {
1495 const pass_data pass_data_ipa_pure_const =
1497 IPA_PASS, /* type */
1498 "pure-const", /* name */
1499 OPTGROUP_NONE, /* optinfo_flags */
1500 true, /* has_gate */
1501 true, /* has_execute */
1502 TV_IPA_PURE_CONST, /* tv_id */
1503 0, /* properties_required */
1504 0, /* properties_provided */
1505 0, /* properties_destroyed */
1506 0, /* todo_flags_start */
1507 0, /* todo_flags_finish */
1510 class pass_ipa_pure_const : public ipa_opt_pass_d
1512 public:
1513 pass_ipa_pure_const (gcc::context *ctxt)
1514 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1515 pure_const_generate_summary, /* generate_summary */
1516 pure_const_write_summary, /* write_summary */
1517 pure_const_read_summary, /* read_summary */
1518 NULL, /* write_optimization_summary */
1519 NULL, /* read_optimization_summary */
1520 NULL, /* stmt_fixup */
1521 0, /* function_transform_todo_flags_start */
1522 NULL, /* function_transform */
1523 NULL) /* variable_transform */
1526 /* opt_pass methods: */
1527 bool gate () { return gate_pure_const (); }
1528 unsigned int execute () { return propagate (); }
1530 }; // class pass_ipa_pure_const
1532 } // anon namespace
1534 ipa_opt_pass_d *
1535 make_pass_ipa_pure_const (gcc::context *ctxt)
1537 return new pass_ipa_pure_const (ctxt);
1540 /* Return true if function should be skipped for local pure const analysis. */
1542 static bool
1543 skip_function_for_local_pure_const (struct cgraph_node *node)
1545 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1546 we must not promote functions that are called by already processed functions. */
1548 if (function_called_by_processed_nodes_p ())
1550 if (dump_file)
1551 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1552 return true;
1554 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1556 if (dump_file)
1557 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1558 return true;
1560 return false;
1563 /* Simple local pass for pure const discovery reusing the analysis from
1564 ipa_pure_const. This pass is effective when executed together with
1565 other optimization passes in early optimization pass queue. */
1567 static unsigned int
1568 local_pure_const (void)
1570 bool changed = false;
1571 funct_state l;
1572 bool skip;
1573 struct cgraph_node *node;
1575 node = cgraph_get_node (current_function_decl);
1576 skip = skip_function_for_local_pure_const (node);
1577 if (!warn_suggest_attribute_const
1578 && !warn_suggest_attribute_pure
1579 && skip)
1580 return 0;
1582 l = analyze_function (node, false);
1584 /* Do NORETURN discovery. */
1585 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1586 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0)
1588 warn_function_noreturn (cfun->decl);
1589 if (dump_file)
1590 fprintf (dump_file, "Function found to be noreturn: %s\n",
1591 current_function_name ());
1593 /* Update declaration and reduce profile to executed once. */
1594 TREE_THIS_VOLATILE (current_function_decl) = 1;
1595 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1596 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1598 changed = true;
1601 switch (l->pure_const_state)
1603 case IPA_CONST:
1604 if (!TREE_READONLY (current_function_decl))
1606 warn_function_const (current_function_decl, !l->looping);
1607 if (!skip)
1609 cgraph_set_const_flag (node, true, l->looping);
1610 changed = true;
1612 if (dump_file)
1613 fprintf (dump_file, "Function found to be %sconst: %s\n",
1614 l->looping ? "looping " : "",
1615 current_function_name ());
1617 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1618 && !l->looping)
1620 if (!skip)
1622 cgraph_set_const_flag (node, true, false);
1623 changed = true;
1625 if (dump_file)
1626 fprintf (dump_file, "Function found to be non-looping: %s\n",
1627 current_function_name ());
1629 break;
1631 case IPA_PURE:
1632 if (!DECL_PURE_P (current_function_decl))
1634 if (!skip)
1636 cgraph_set_pure_flag (node, true, l->looping);
1637 changed = true;
1639 warn_function_pure (current_function_decl, !l->looping);
1640 if (dump_file)
1641 fprintf (dump_file, "Function found to be %spure: %s\n",
1642 l->looping ? "looping " : "",
1643 current_function_name ());
1645 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1646 && !l->looping)
1648 if (!skip)
1650 cgraph_set_pure_flag (node, true, false);
1651 changed = true;
1653 if (dump_file)
1654 fprintf (dump_file, "Function found to be non-looping: %s\n",
1655 current_function_name ());
1657 break;
1659 default:
1660 break;
1662 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1664 cgraph_set_nothrow_flag (node, true);
1665 changed = true;
1666 if (dump_file)
1667 fprintf (dump_file, "Function found to be nothrow: %s\n",
1668 current_function_name ());
1670 free (l);
1671 if (changed)
1672 return execute_fixup_cfg ();
1673 else
1674 return 0;
1677 namespace {
1679 const pass_data pass_data_local_pure_const =
1681 GIMPLE_PASS, /* type */
1682 "local-pure-const", /* name */
1683 OPTGROUP_NONE, /* optinfo_flags */
1684 true, /* has_gate */
1685 true, /* has_execute */
1686 TV_IPA_PURE_CONST, /* tv_id */
1687 0, /* properties_required */
1688 0, /* properties_provided */
1689 0, /* properties_destroyed */
1690 0, /* todo_flags_start */
1691 0, /* todo_flags_finish */
1694 class pass_local_pure_const : public gimple_opt_pass
1696 public:
1697 pass_local_pure_const (gcc::context *ctxt)
1698 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1701 /* opt_pass methods: */
1702 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1703 bool gate () { return gate_pure_const (); }
1704 unsigned int execute () { return local_pure_const (); }
1706 }; // class pass_local_pure_const
1708 } // anon namespace
1710 gimple_opt_pass *
1711 make_pass_local_pure_const (gcc::context *ctxt)
1713 return new pass_local_pure_const (ctxt);
1716 /* Emit noreturn warnings. */
1718 static unsigned int
1719 execute_warn_function_noreturn (void)
1721 if (!TREE_THIS_VOLATILE (current_function_decl)
1722 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0)
1723 warn_function_noreturn (current_function_decl);
1724 return 0;
1727 static bool
1728 gate_warn_function_noreturn (void)
1730 return warn_suggest_attribute_noreturn;
1733 namespace {
1735 const pass_data pass_data_warn_function_noreturn =
1737 GIMPLE_PASS, /* type */
1738 "*warn_function_noreturn", /* name */
1739 OPTGROUP_NONE, /* optinfo_flags */
1740 true, /* has_gate */
1741 true, /* has_execute */
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 bool gate () { return gate_warn_function_noreturn (); }
1759 unsigned int execute () { return execute_warn_function_noreturn (); }
1761 }; // class pass_warn_function_noreturn
1763 } // anon namespace
1765 gimple_opt_pass *
1766 make_pass_warn_function_noreturn (gcc::context *ctxt)
1768 return new pass_warn_function_noreturn (ctxt);