Rebase.
[official-gcc.git] / gcc / ipa-pure-const.c
blobd7688d54feb6e01aa2ec04cadb09c88a22b93994
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"
68 #include "hash-set.h"
70 /* Lattice values for const and pure functions. Everything starts out
71 being const, then may drop to pure and then neither depending on
72 what is found. */
73 enum pure_const_state_e
75 IPA_CONST,
76 IPA_PURE,
77 IPA_NEITHER
80 const char *pure_const_names[3] = {"const", "pure", "neither"};
82 /* Holder for the const_state. There is one of these per function
83 decl. */
84 struct funct_state_d
86 /* See above. */
87 enum pure_const_state_e pure_const_state;
88 /* What user set here; we can be always sure about this. */
89 enum pure_const_state_e state_previously_known;
90 bool looping_previously_known;
92 /* True if the function could possibly infinite loop. There are a
93 lot of ways that this could be determined. We are pretty
94 conservative here. While it is possible to cse pure and const
95 calls, it is not legal to have dce get rid of the call if there
96 is a possibility that the call could infinite loop since this is
97 a behavioral change. */
98 bool looping;
100 bool can_throw;
103 /* State used when we know nothing about function. */
104 static struct funct_state_d varying_state
105 = { IPA_NEITHER, IPA_NEITHER, true, true, true };
108 typedef struct funct_state_d * funct_state;
110 /* The storage of the funct_state is abstracted because there is the
111 possibility that it may be desirable to move this to the cgraph
112 local info. */
114 /* Array, indexed by cgraph node uid, of function states. */
116 static vec<funct_state> funct_state_vec;
118 /* Holders of ipa cgraph hooks: */
119 static struct cgraph_node_hook_list *function_insertion_hook_holder;
120 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
121 static struct cgraph_node_hook_list *node_removal_hook_holder;
123 /* Try to guess if function body will always be visible to compiler
124 when compiling the call and whether compiler will be able
125 to propagate the information by itself. */
127 static bool
128 function_always_visible_to_compiler_p (tree decl)
130 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
133 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
134 is true if the function is known to be finite. The diagnostic is
135 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
136 OPTION, this function may initialize it and it is always returned
137 by the function. */
139 static hash_set<tree> *
140 suggest_attribute (int option, tree decl, bool known_finite,
141 hash_set<tree> *warned_about,
142 const char * attrib_name)
144 if (!option_enabled (option, &global_options))
145 return warned_about;
146 if (TREE_THIS_VOLATILE (decl)
147 || (known_finite && function_always_visible_to_compiler_p (decl)))
148 return warned_about;
150 if (!warned_about)
151 warned_about = new hash_set<tree>;
152 if (warned_about->contains (decl))
153 return warned_about;
154 warned_about->add (decl);
155 warning_at (DECL_SOURCE_LOCATION (decl),
156 option,
157 known_finite
158 ? _("function might be candidate for attribute %<%s%>")
159 : _("function might be candidate for attribute %<%s%>"
160 " if it is known to return normally"), attrib_name);
161 return warned_about;
164 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
165 is true if the function is known to be finite. */
167 static void
168 warn_function_pure (tree decl, bool known_finite)
170 static hash_set<tree> *warned_about;
172 warned_about
173 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
174 known_finite, warned_about, "pure");
177 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
178 is true if the function is known to be finite. */
180 static void
181 warn_function_const (tree decl, bool known_finite)
183 static hash_set<tree> *warned_about;
184 warned_about
185 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
186 known_finite, warned_about, "const");
189 static void
190 warn_function_noreturn (tree decl)
192 static hash_set<tree> *warned_about;
193 if (!lang_hooks.missing_noreturn_ok_p (decl)
194 && targetm.warn_func_return (decl))
195 warned_about
196 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
197 true, warned_about, "noreturn");
200 /* Return true if we have a function state for NODE. */
202 static inline bool
203 has_function_state (struct cgraph_node *node)
205 if (!funct_state_vec.exists ()
206 || funct_state_vec.length () <= (unsigned int)node->uid)
207 return false;
208 return funct_state_vec[node->uid] != NULL;
211 /* Return the function state from NODE. */
213 static inline funct_state
214 get_function_state (struct cgraph_node *node)
216 if (!funct_state_vec.exists ()
217 || funct_state_vec.length () <= (unsigned int)node->uid
218 || !funct_state_vec[node->uid])
219 /* We might want to put correct previously_known state into varying. */
220 return &varying_state;
221 return funct_state_vec[node->uid];
224 /* Set the function state S for NODE. */
226 static inline void
227 set_function_state (struct cgraph_node *node, funct_state s)
229 if (!funct_state_vec.exists ()
230 || funct_state_vec.length () <= (unsigned int)node->uid)
231 funct_state_vec.safe_grow_cleared (node->uid + 1);
232 funct_state_vec[node->uid] = s;
235 /* Check to see if the use (or definition when CHECKING_WRITE is true)
236 variable T is legal in a function that is either pure or const. */
238 static inline void
239 check_decl (funct_state local,
240 tree t, bool checking_write, bool ipa)
242 /* Do not want to do anything with volatile except mark any
243 function that uses one to be not const or pure. */
244 if (TREE_THIS_VOLATILE (t))
246 local->pure_const_state = IPA_NEITHER;
247 if (dump_file)
248 fprintf (dump_file, " Volatile operand is not const/pure");
249 return;
252 /* Do not care about a local automatic that is not static. */
253 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
254 return;
256 /* If the variable has the "used" attribute, treat it as if it had a
257 been touched by the devil. */
258 if (DECL_PRESERVE_P (t))
260 local->pure_const_state = IPA_NEITHER;
261 if (dump_file)
262 fprintf (dump_file, " Used static/global variable is not const/pure\n");
263 return;
266 /* In IPA mode we are not interested in checking actual loads and stores;
267 they will be processed at propagation time using ipa_ref. */
268 if (ipa)
269 return;
271 /* Since we have dealt with the locals and params cases above, if we
272 are CHECKING_WRITE, this cannot be a pure or constant
273 function. */
274 if (checking_write)
276 local->pure_const_state = IPA_NEITHER;
277 if (dump_file)
278 fprintf (dump_file, " static/global memory write is not const/pure\n");
279 return;
282 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
284 /* Readonly reads are safe. */
285 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
286 return; /* Read of a constant, do not change the function state. */
287 else
289 if (dump_file)
290 fprintf (dump_file, " global memory read is not const\n");
291 /* Just a regular read. */
292 if (local->pure_const_state == IPA_CONST)
293 local->pure_const_state = IPA_PURE;
296 else
298 /* Compilation level statics can be read if they are readonly
299 variables. */
300 if (TREE_READONLY (t))
301 return;
303 if (dump_file)
304 fprintf (dump_file, " static memory read is not const\n");
305 /* Just a regular read. */
306 if (local->pure_const_state == IPA_CONST)
307 local->pure_const_state = IPA_PURE;
312 /* Check to see if the use (or definition when CHECKING_WRITE is true)
313 variable T is legal in a function that is either pure or const. */
315 static inline void
316 check_op (funct_state local, tree t, bool checking_write)
318 t = get_base_address (t);
319 if (t && TREE_THIS_VOLATILE (t))
321 local->pure_const_state = IPA_NEITHER;
322 if (dump_file)
323 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
324 return;
326 else if (t
327 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
328 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
329 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
331 if (dump_file)
332 fprintf (dump_file, " Indirect ref to local memory is OK\n");
333 return;
335 else if (checking_write)
337 local->pure_const_state = IPA_NEITHER;
338 if (dump_file)
339 fprintf (dump_file, " Indirect ref write is not const/pure\n");
340 return;
342 else
344 if (dump_file)
345 fprintf (dump_file, " Indirect ref read is not const\n");
346 if (local->pure_const_state == IPA_CONST)
347 local->pure_const_state = IPA_PURE;
351 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
353 static void
354 state_from_flags (enum pure_const_state_e *state, bool *looping,
355 int flags, bool cannot_lead_to_return)
357 *looping = false;
358 if (flags & ECF_LOOPING_CONST_OR_PURE)
360 *looping = true;
361 if (dump_file && (dump_flags & TDF_DETAILS))
362 fprintf (dump_file, " looping");
364 if (flags & ECF_CONST)
366 *state = IPA_CONST;
367 if (dump_file && (dump_flags & TDF_DETAILS))
368 fprintf (dump_file, " const\n");
370 else if (flags & ECF_PURE)
372 *state = IPA_PURE;
373 if (dump_file && (dump_flags & TDF_DETAILS))
374 fprintf (dump_file, " pure\n");
376 else if (cannot_lead_to_return)
378 *state = IPA_PURE;
379 *looping = true;
380 if (dump_file && (dump_flags & TDF_DETAILS))
381 fprintf (dump_file, " ignoring side effects->pure looping\n");
383 else
385 if (dump_file && (dump_flags & TDF_DETAILS))
386 fprintf (dump_file, " neither\n");
387 *state = IPA_NEITHER;
388 *looping = true;
392 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
393 into STATE and LOOPING better of the two variants.
394 Be sure to merge looping correctly. IPA_NEITHER functions
395 have looping 0 even if they don't have to return. */
397 static inline void
398 better_state (enum pure_const_state_e *state, bool *looping,
399 enum pure_const_state_e state2, bool looping2)
401 if (state2 < *state)
403 if (*state == IPA_NEITHER)
404 *looping = looping2;
405 else
406 *looping = MIN (*looping, looping2);
407 *state = state2;
409 else if (state2 != IPA_NEITHER)
410 *looping = MIN (*looping, looping2);
413 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
414 into STATE and LOOPING worse of the two variants. */
416 static inline void
417 worse_state (enum pure_const_state_e *state, bool *looping,
418 enum pure_const_state_e state2, bool looping2)
420 *state = MAX (*state, state2);
421 *looping = MAX (*looping, looping2);
424 /* Recognize special cases of builtins that are by themselves not pure or const
425 but function using them is. */
426 static bool
427 special_builtin_state (enum pure_const_state_e *state, bool *looping,
428 tree callee)
430 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
431 switch (DECL_FUNCTION_CODE (callee))
433 case BUILT_IN_RETURN:
434 case BUILT_IN_UNREACHABLE:
435 case BUILT_IN_ALLOCA:
436 case BUILT_IN_ALLOCA_WITH_ALIGN:
437 case BUILT_IN_STACK_SAVE:
438 case BUILT_IN_STACK_RESTORE:
439 case BUILT_IN_EH_POINTER:
440 case BUILT_IN_EH_FILTER:
441 case BUILT_IN_UNWIND_RESUME:
442 case BUILT_IN_CXA_END_CLEANUP:
443 case BUILT_IN_EH_COPY_VALUES:
444 case BUILT_IN_FRAME_ADDRESS:
445 case BUILT_IN_APPLY:
446 case BUILT_IN_APPLY_ARGS:
447 *looping = false;
448 *state = IPA_CONST;
449 return true;
450 case BUILT_IN_PREFETCH:
451 *looping = true;
452 *state = IPA_CONST;
453 return true;
455 return false;
458 /* Check the parameters of a function call to CALL_EXPR to see if
459 there are any references in the parameters that are not allowed for
460 pure or const functions. Also check to see if this is either an
461 indirect call, a call outside the compilation unit, or has special
462 attributes that may also effect the purity. The CALL_EXPR node for
463 the entire call expression. */
465 static void
466 check_call (funct_state local, gimple call, bool ipa)
468 int flags = gimple_call_flags (call);
469 tree callee_t = gimple_call_fndecl (call);
470 bool possibly_throws = stmt_could_throw_p (call);
471 bool possibly_throws_externally = (possibly_throws
472 && stmt_can_throw_external (call));
474 if (possibly_throws)
476 unsigned int i;
477 for (i = 0; i < gimple_num_ops (call); i++)
478 if (gimple_op (call, i)
479 && tree_could_throw_p (gimple_op (call, i)))
481 if (possibly_throws && cfun->can_throw_non_call_exceptions)
483 if (dump_file)
484 fprintf (dump_file, " operand can throw; looping\n");
485 local->looping = true;
487 if (possibly_throws_externally)
489 if (dump_file)
490 fprintf (dump_file, " operand can throw externally\n");
491 local->can_throw = true;
496 /* The const and pure flags are set by a variety of places in the
497 compiler (including here). If someone has already set the flags
498 for the callee, (such as for some of the builtins) we will use
499 them, otherwise we will compute our own information.
501 Const and pure functions have less clobber effects than other
502 functions so we process these first. Otherwise if it is a call
503 outside the compilation unit or an indirect call we punt. This
504 leaves local calls which will be processed by following the call
505 graph. */
506 if (callee_t)
508 enum pure_const_state_e call_state;
509 bool call_looping;
511 if (special_builtin_state (&call_state, &call_looping, callee_t))
513 worse_state (&local->pure_const_state, &local->looping,
514 call_state, call_looping);
515 return;
517 /* When bad things happen to bad functions, they cannot be const
518 or pure. */
519 if (setjmp_call_p (callee_t))
521 if (dump_file)
522 fprintf (dump_file, " setjmp is not const/pure\n");
523 local->looping = true;
524 local->pure_const_state = IPA_NEITHER;
527 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
528 switch (DECL_FUNCTION_CODE (callee_t))
530 case BUILT_IN_LONGJMP:
531 case BUILT_IN_NONLOCAL_GOTO:
532 if (dump_file)
533 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
534 local->pure_const_state = IPA_NEITHER;
535 local->looping = true;
536 break;
537 default:
538 break;
542 /* When not in IPA mode, we can still handle self recursion. */
543 if (!ipa && callee_t
544 && recursive_call_p (current_function_decl, callee_t))
546 if (dump_file)
547 fprintf (dump_file, " Recursive call can loop.\n");
548 local->looping = true;
550 /* Either callee is unknown or we are doing local analysis.
551 Look to see if there are any bits available for the callee (such as by
552 declaration or because it is builtin) and process solely on the basis of
553 those bits. */
554 else if (!ipa)
556 enum pure_const_state_e call_state;
557 bool call_looping;
558 if (possibly_throws && cfun->can_throw_non_call_exceptions)
560 if (dump_file)
561 fprintf (dump_file, " can throw; looping\n");
562 local->looping = true;
564 if (possibly_throws_externally)
566 if (dump_file)
568 fprintf (dump_file, " can throw externally to lp %i\n",
569 lookup_stmt_eh_lp (call));
570 if (callee_t)
571 fprintf (dump_file, " callee:%s\n",
572 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
574 local->can_throw = true;
576 if (dump_file && (dump_flags & TDF_DETAILS))
577 fprintf (dump_file, " checking flags for call:");
578 state_from_flags (&call_state, &call_looping, flags,
579 ((flags & (ECF_NORETURN | ECF_NOTHROW))
580 == (ECF_NORETURN | ECF_NOTHROW))
581 || (!flag_exceptions && (flags & ECF_NORETURN)));
582 worse_state (&local->pure_const_state, &local->looping,
583 call_state, call_looping);
585 /* Direct functions calls are handled by IPA propagation. */
588 /* Wrapper around check_decl for loads in local more. */
590 static bool
591 check_load (gimple, tree op, tree, void *data)
593 if (DECL_P (op))
594 check_decl ((funct_state)data, op, false, false);
595 else
596 check_op ((funct_state)data, op, false);
597 return false;
600 /* Wrapper around check_decl for stores in local more. */
602 static bool
603 check_store (gimple, tree op, tree, void *data)
605 if (DECL_P (op))
606 check_decl ((funct_state)data, op, true, false);
607 else
608 check_op ((funct_state)data, op, true);
609 return false;
612 /* Wrapper around check_decl for loads in ipa mode. */
614 static bool
615 check_ipa_load (gimple, tree op, tree, void *data)
617 if (DECL_P (op))
618 check_decl ((funct_state)data, op, false, true);
619 else
620 check_op ((funct_state)data, op, false);
621 return false;
624 /* Wrapper around check_decl for stores in ipa mode. */
626 static bool
627 check_ipa_store (gimple, tree op, tree, void *data)
629 if (DECL_P (op))
630 check_decl ((funct_state)data, op, true, true);
631 else
632 check_op ((funct_state)data, op, true);
633 return false;
636 /* Look into pointer pointed to by GSIP and figure out what interesting side
637 effects it has. */
638 static void
639 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
641 gimple stmt = gsi_stmt (*gsip);
643 if (is_gimple_debug (stmt))
644 return;
646 if (dump_file)
648 fprintf (dump_file, " scanning: ");
649 print_gimple_stmt (dump_file, stmt, 0, 0);
652 if (gimple_has_volatile_ops (stmt)
653 && !gimple_clobber_p (stmt))
655 local->pure_const_state = IPA_NEITHER;
656 if (dump_file)
657 fprintf (dump_file, " Volatile stmt is not const/pure\n");
660 /* Look for loads and stores. */
661 walk_stmt_load_store_ops (stmt, local,
662 ipa ? check_ipa_load : check_load,
663 ipa ? check_ipa_store : check_store);
665 if (gimple_code (stmt) != GIMPLE_CALL
666 && stmt_could_throw_p (stmt))
668 if (cfun->can_throw_non_call_exceptions)
670 if (dump_file)
671 fprintf (dump_file, " can throw; looping\n");
672 local->looping = true;
674 if (stmt_can_throw_external (stmt))
676 if (dump_file)
677 fprintf (dump_file, " can throw externally\n");
678 local->can_throw = true;
680 else
681 if (dump_file)
682 fprintf (dump_file, " can throw\n");
684 switch (gimple_code (stmt))
686 case GIMPLE_CALL:
687 check_call (local, stmt, ipa);
688 break;
689 case GIMPLE_LABEL:
690 if (DECL_NONLOCAL (gimple_label_label (stmt)))
691 /* Target of long jump. */
693 if (dump_file)
694 fprintf (dump_file, " nonlocal label is not const/pure\n");
695 local->pure_const_state = IPA_NEITHER;
697 break;
698 case GIMPLE_ASM:
699 if (gimple_asm_clobbers_memory_p (stmt))
701 if (dump_file)
702 fprintf (dump_file, " memory asm clobber is not const/pure\n");
703 /* Abandon all hope, ye who enter here. */
704 local->pure_const_state = IPA_NEITHER;
706 if (gimple_asm_volatile_p (stmt))
708 if (dump_file)
709 fprintf (dump_file, " volatile is not const/pure\n");
710 /* Abandon all hope, ye who enter here. */
711 local->pure_const_state = IPA_NEITHER;
712 local->looping = true;
714 return;
715 default:
716 break;
721 /* This is the main routine for finding the reference patterns for
722 global variables within a function FN. */
724 static funct_state
725 analyze_function (struct cgraph_node *fn, bool ipa)
727 tree decl = fn->decl;
728 funct_state l;
729 basic_block this_block;
731 l = XCNEW (struct funct_state_d);
732 l->pure_const_state = IPA_CONST;
733 l->state_previously_known = IPA_NEITHER;
734 l->looping_previously_known = true;
735 l->looping = false;
736 l->can_throw = false;
737 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
738 flags_from_decl_or_type (fn->decl),
739 fn->cannot_return_p ());
741 if (fn->thunk.thunk_p || fn->alias)
743 /* Thunk gets propagated through, so nothing interesting happens. */
744 gcc_assert (ipa);
745 return l;
748 if (dump_file)
750 fprintf (dump_file, "\n\n local analysis of %s\n ",
751 fn->name ());
754 push_cfun (DECL_STRUCT_FUNCTION (decl));
756 FOR_EACH_BB_FN (this_block, cfun)
758 gimple_stmt_iterator gsi;
759 struct walk_stmt_info wi;
761 memset (&wi, 0, sizeof (wi));
762 for (gsi = gsi_start_bb (this_block);
763 !gsi_end_p (gsi);
764 gsi_next (&gsi))
766 check_stmt (&gsi, l, ipa);
767 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
768 goto end;
772 end:
773 if (l->pure_const_state != IPA_NEITHER)
775 /* Const functions cannot have back edges (an
776 indication of possible infinite loop side
777 effect. */
778 if (mark_dfs_back_edges ())
780 /* Preheaders are needed for SCEV to work.
781 Simple latches and recorded exits improve chances that loop will
782 proved to be finite in testcases such as in loop-15.c
783 and loop-24.c */
784 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
785 | LOOPS_HAVE_SIMPLE_LATCHES
786 | LOOPS_HAVE_RECORDED_EXITS);
787 if (dump_file && (dump_flags & TDF_DETAILS))
788 flow_loops_dump (dump_file, NULL, 0);
789 if (mark_irreducible_loops ())
791 if (dump_file)
792 fprintf (dump_file, " has irreducible loops\n");
793 l->looping = true;
795 else
797 struct loop *loop;
798 scev_initialize ();
799 FOR_EACH_LOOP (loop, 0)
800 if (!finite_loop_p (loop))
802 if (dump_file)
803 fprintf (dump_file, " can not prove finiteness of "
804 "loop %i\n", loop->num);
805 l->looping =true;
806 break;
808 scev_finalize ();
810 loop_optimizer_finalize ();
814 if (dump_file && (dump_flags & TDF_DETAILS))
815 fprintf (dump_file, " checking previously known:");
817 better_state (&l->pure_const_state, &l->looping,
818 l->state_previously_known,
819 l->looping_previously_known);
820 if (TREE_NOTHROW (decl))
821 l->can_throw = false;
823 pop_cfun ();
824 if (dump_file)
826 if (l->looping)
827 fprintf (dump_file, "Function is locally looping.\n");
828 if (l->can_throw)
829 fprintf (dump_file, "Function is locally throwing.\n");
830 if (l->pure_const_state == IPA_CONST)
831 fprintf (dump_file, "Function is locally const.\n");
832 if (l->pure_const_state == IPA_PURE)
833 fprintf (dump_file, "Function is locally pure.\n");
835 return l;
838 /* Called when new function is inserted to callgraph late. */
839 static void
840 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
842 if (node->get_availability () < AVAIL_INTERPOSABLE)
843 return;
844 /* There are some shared nodes, in particular the initializers on
845 static declarations. We do not need to scan them more than once
846 since all we would be interested in are the addressof
847 operations. */
848 if (node->get_availability () > AVAIL_INTERPOSABLE)
849 set_function_state (node, analyze_function (node, true));
852 /* Called when new clone is inserted to callgraph late. */
854 static void
855 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
856 void *data ATTRIBUTE_UNUSED)
858 if (has_function_state (src))
860 funct_state l = XNEW (struct funct_state_d);
861 gcc_assert (!has_function_state (dst));
862 memcpy (l, get_function_state (src), sizeof (*l));
863 set_function_state (dst, l);
867 /* Called when new clone is inserted to callgraph late. */
869 static void
870 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
872 if (has_function_state (node))
874 funct_state l = get_function_state (node);
875 if (l != &varying_state)
876 free (l);
877 set_function_state (node, NULL);
882 static void
883 register_hooks (void)
885 static bool init_p = false;
887 if (init_p)
888 return;
890 init_p = true;
892 node_removal_hook_holder =
893 cgraph_add_node_removal_hook (&remove_node_data, NULL);
894 node_duplication_hook_holder =
895 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
896 function_insertion_hook_holder =
897 cgraph_add_function_insertion_hook (&add_new_function, NULL);
901 /* Analyze each function in the cgraph to see if it is locally PURE or
902 CONST. */
904 static void
905 pure_const_generate_summary (void)
907 struct cgraph_node *node;
909 register_hooks ();
911 /* Process all of the functions.
913 We process AVAIL_INTERPOSABLE functions. We can not use the results
914 by default, but the info can be used at LTO with -fwhole-program or
915 when function got cloned and the clone is AVAILABLE. */
917 FOR_EACH_DEFINED_FUNCTION (node)
918 if (node->get_availability () >= AVAIL_INTERPOSABLE)
919 set_function_state (node, analyze_function (node, true));
923 /* Serialize the ipa info for lto. */
925 static void
926 pure_const_write_summary (void)
928 struct cgraph_node *node;
929 struct lto_simple_output_block *ob
930 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
931 unsigned int count = 0;
932 lto_symtab_encoder_iterator lsei;
933 lto_symtab_encoder_t encoder;
935 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
937 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
938 lsei_next_function_in_partition (&lsei))
940 node = lsei_cgraph_node (lsei);
941 if (node->definition && has_function_state (node))
942 count++;
945 streamer_write_uhwi_stream (ob->main_stream, count);
947 /* Process all of the functions. */
948 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
949 lsei_next_function_in_partition (&lsei))
951 node = lsei_cgraph_node (lsei);
952 if (node->definition && has_function_state (node))
954 struct bitpack_d bp;
955 funct_state fs;
956 int node_ref;
957 lto_symtab_encoder_t encoder;
959 fs = get_function_state (node);
961 encoder = ob->decl_state->symtab_node_encoder;
962 node_ref = lto_symtab_encoder_encode (encoder, node);
963 streamer_write_uhwi_stream (ob->main_stream, node_ref);
965 /* Note that flags will need to be read in the opposite
966 order as we are pushing the bitflags into FLAGS. */
967 bp = bitpack_create (ob->main_stream);
968 bp_pack_value (&bp, fs->pure_const_state, 2);
969 bp_pack_value (&bp, fs->state_previously_known, 2);
970 bp_pack_value (&bp, fs->looping_previously_known, 1);
971 bp_pack_value (&bp, fs->looping, 1);
972 bp_pack_value (&bp, fs->can_throw, 1);
973 streamer_write_bitpack (&bp);
977 lto_destroy_simple_output_block (ob);
981 /* Deserialize the ipa info for lto. */
983 static void
984 pure_const_read_summary (void)
986 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
987 struct lto_file_decl_data *file_data;
988 unsigned int j = 0;
990 register_hooks ();
991 while ((file_data = file_data_vec[j++]))
993 const char *data;
994 size_t len;
995 struct lto_input_block *ib
996 = lto_create_simple_input_block (file_data,
997 LTO_section_ipa_pure_const,
998 &data, &len);
999 if (ib)
1001 unsigned int i;
1002 unsigned int count = streamer_read_uhwi (ib);
1004 for (i = 0; i < count; i++)
1006 unsigned int index;
1007 struct cgraph_node *node;
1008 struct bitpack_d bp;
1009 funct_state fs;
1010 lto_symtab_encoder_t encoder;
1012 fs = XCNEW (struct funct_state_d);
1013 index = streamer_read_uhwi (ib);
1014 encoder = file_data->symtab_node_encoder;
1015 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1016 index));
1017 set_function_state (node, fs);
1019 /* Note that the flags must be read in the opposite
1020 order in which they were written (the bitflags were
1021 pushed into FLAGS). */
1022 bp = streamer_read_bitpack (ib);
1023 fs->pure_const_state
1024 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1025 fs->state_previously_known
1026 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1027 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1028 fs->looping = bp_unpack_value (&bp, 1);
1029 fs->can_throw = bp_unpack_value (&bp, 1);
1030 if (dump_file)
1032 int flags = flags_from_decl_or_type (node->decl);
1033 fprintf (dump_file, "Read info for %s/%i ",
1034 node->name (),
1035 node->order);
1036 if (flags & ECF_CONST)
1037 fprintf (dump_file, " const");
1038 if (flags & ECF_PURE)
1039 fprintf (dump_file, " pure");
1040 if (flags & ECF_NOTHROW)
1041 fprintf (dump_file, " nothrow");
1042 fprintf (dump_file, "\n pure const state: %s\n",
1043 pure_const_names[fs->pure_const_state]);
1044 fprintf (dump_file, " previously known state: %s\n",
1045 pure_const_names[fs->looping_previously_known]);
1046 if (fs->looping)
1047 fprintf (dump_file," function is locally looping\n");
1048 if (fs->looping_previously_known)
1049 fprintf (dump_file," function is previously known looping\n");
1050 if (fs->can_throw)
1051 fprintf (dump_file," function is locally throwing\n");
1055 lto_destroy_simple_input_block (file_data,
1056 LTO_section_ipa_pure_const,
1057 ib, data, len);
1063 static bool
1064 ignore_edge (struct cgraph_edge *e)
1066 return (!e->can_throw_external);
1069 /* Return true if NODE is self recursive function.
1070 Indirectly recursive functions appears as non-trivial strongly
1071 connected components, so we need to care about self recursion
1072 only. */
1074 static bool
1075 self_recursive_p (struct cgraph_node *node)
1077 struct cgraph_edge *e;
1078 for (e = node->callees; e; e = e->next_callee)
1079 if (e->callee->function_symbol () == node)
1080 return true;
1081 return false;
1084 /* Produce transitive closure over the callgraph and compute pure/const
1085 attributes. */
1087 static void
1088 propagate_pure_const (void)
1090 struct cgraph_node *node;
1091 struct cgraph_node *w;
1092 struct cgraph_node **order =
1093 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1094 int order_pos;
1095 int i;
1096 struct ipa_dfs_info * w_info;
1098 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1099 if (dump_file)
1101 cgraph_node::dump_cgraph (dump_file);
1102 ipa_print_order (dump_file, "reduced", order, order_pos);
1105 /* Propagate the local information through the call graph to produce
1106 the global information. All the nodes within a cycle will have
1107 the same info so we collapse cycles first. Then we can do the
1108 propagation in one pass from the leaves to the roots. */
1109 for (i = 0; i < order_pos; i++ )
1111 enum pure_const_state_e pure_const_state = IPA_CONST;
1112 bool looping = false;
1113 int count = 0;
1114 node = order[i];
1116 if (node->alias)
1117 continue;
1119 if (dump_file && (dump_flags & TDF_DETAILS))
1120 fprintf (dump_file, "Starting cycle\n");
1122 /* Find the worst state for any node in the cycle. */
1123 w = node;
1124 while (w && pure_const_state != IPA_NEITHER)
1126 struct cgraph_edge *e;
1127 struct cgraph_edge *ie;
1128 int i;
1129 struct ipa_ref *ref = NULL;
1131 funct_state w_l = get_function_state (w);
1132 if (dump_file && (dump_flags & TDF_DETAILS))
1133 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1134 w->name (),
1135 w->order,
1136 pure_const_names[w_l->pure_const_state],
1137 w_l->looping);
1139 /* First merge in function body properties. */
1140 worse_state (&pure_const_state, &looping,
1141 w_l->pure_const_state, w_l->looping);
1142 if (pure_const_state == IPA_NEITHER)
1143 break;
1145 /* For overwritable nodes we can not assume anything. */
1146 if (w->get_availability () == AVAIL_INTERPOSABLE)
1148 worse_state (&pure_const_state, &looping,
1149 w_l->state_previously_known,
1150 w_l->looping_previously_known);
1151 if (dump_file && (dump_flags & TDF_DETAILS))
1153 fprintf (dump_file,
1154 " Overwritable. state %s looping %i\n",
1155 pure_const_names[w_l->state_previously_known],
1156 w_l->looping_previously_known);
1158 break;
1161 count++;
1163 /* We consider recursive cycles as possibly infinite.
1164 This might be relaxed since infinite recursion leads to stack
1165 overflow. */
1166 if (count > 1)
1167 looping = true;
1169 /* Now walk the edges and merge in callee properties. */
1170 for (e = w->callees; e; e = e->next_callee)
1172 enum availability avail;
1173 struct cgraph_node *y = e->callee->function_symbol (&avail);
1174 enum pure_const_state_e edge_state = IPA_CONST;
1175 bool edge_looping = false;
1177 if (dump_file && (dump_flags & TDF_DETAILS))
1179 fprintf (dump_file,
1180 " Call to %s/%i",
1181 e->callee->name (),
1182 e->callee->order);
1184 if (avail > AVAIL_INTERPOSABLE)
1186 funct_state y_l = get_function_state (y);
1187 if (dump_file && (dump_flags & TDF_DETAILS))
1189 fprintf (dump_file,
1190 " state:%s looping:%i\n",
1191 pure_const_names[y_l->pure_const_state],
1192 y_l->looping);
1194 if (y_l->pure_const_state > IPA_PURE
1195 && cgraph_edge_cannot_lead_to_return (e))
1197 if (dump_file && (dump_flags & TDF_DETAILS))
1198 fprintf (dump_file,
1199 " Ignoring side effects"
1200 " -> pure, looping\n");
1201 edge_state = IPA_PURE;
1202 edge_looping = true;
1204 else
1206 edge_state = y_l->pure_const_state;
1207 edge_looping = y_l->looping;
1210 else if (special_builtin_state (&edge_state, &edge_looping,
1211 y->decl))
1213 else
1214 state_from_flags (&edge_state, &edge_looping,
1215 flags_from_decl_or_type (y->decl),
1216 cgraph_edge_cannot_lead_to_return (e));
1218 /* Merge the results with what we already know. */
1219 better_state (&edge_state, &edge_looping,
1220 w_l->state_previously_known,
1221 w_l->looping_previously_known);
1222 worse_state (&pure_const_state, &looping,
1223 edge_state, edge_looping);
1224 if (pure_const_state == IPA_NEITHER)
1225 break;
1227 if (pure_const_state == IPA_NEITHER)
1228 break;
1230 /* Now process the indirect call. */
1231 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1233 enum pure_const_state_e edge_state = IPA_CONST;
1234 bool edge_looping = false;
1236 if (dump_file && (dump_flags & TDF_DETAILS))
1237 fprintf (dump_file, " Indirect call");
1238 state_from_flags (&edge_state, &edge_looping,
1239 ie->indirect_info->ecf_flags,
1240 cgraph_edge_cannot_lead_to_return (ie));
1241 /* Merge the results with what we already know. */
1242 better_state (&edge_state, &edge_looping,
1243 w_l->state_previously_known,
1244 w_l->looping_previously_known);
1245 worse_state (&pure_const_state, &looping,
1246 edge_state, edge_looping);
1247 if (pure_const_state == IPA_NEITHER)
1248 break;
1250 if (pure_const_state == IPA_NEITHER)
1251 break;
1253 /* And finally all loads and stores. */
1254 for (i = 0; w->iterate_reference (i, ref); i++)
1256 enum pure_const_state_e ref_state = IPA_CONST;
1257 bool ref_looping = false;
1258 switch (ref->use)
1260 case IPA_REF_LOAD:
1261 /* readonly reads are safe. */
1262 if (TREE_READONLY (ref->referred->decl))
1263 break;
1264 if (dump_file && (dump_flags & TDF_DETAILS))
1265 fprintf (dump_file, " nonreadonly global var read\n");
1266 ref_state = IPA_PURE;
1267 break;
1268 case IPA_REF_STORE:
1269 if (ref->cannot_lead_to_return ())
1270 break;
1271 ref_state = IPA_NEITHER;
1272 if (dump_file && (dump_flags & TDF_DETAILS))
1273 fprintf (dump_file, " global var write\n");
1274 break;
1275 case IPA_REF_ADDR:
1276 case IPA_REF_CHKP:
1277 break;
1278 default:
1279 gcc_unreachable ();
1281 better_state (&ref_state, &ref_looping,
1282 w_l->state_previously_known,
1283 w_l->looping_previously_known);
1284 worse_state (&pure_const_state, &looping,
1285 ref_state, ref_looping);
1286 if (pure_const_state == IPA_NEITHER)
1287 break;
1289 w_info = (struct ipa_dfs_info *) w->aux;
1290 w = w_info->next_cycle;
1292 if (dump_file && (dump_flags & TDF_DETAILS))
1293 fprintf (dump_file, "Result %s looping %i\n",
1294 pure_const_names [pure_const_state],
1295 looping);
1297 /* Copy back the region's pure_const_state which is shared by
1298 all nodes in the region. */
1299 w = node;
1300 while (w)
1302 funct_state w_l = get_function_state (w);
1303 enum pure_const_state_e this_state = pure_const_state;
1304 bool this_looping = looping;
1306 if (w_l->state_previously_known != IPA_NEITHER
1307 && this_state > w_l->state_previously_known)
1309 this_state = w_l->state_previously_known;
1310 this_looping |= w_l->looping_previously_known;
1312 if (!this_looping && self_recursive_p (w))
1313 this_looping = true;
1314 if (!w_l->looping_previously_known)
1315 this_looping = false;
1317 /* All nodes within a cycle share the same info. */
1318 w_l->pure_const_state = this_state;
1319 w_l->looping = this_looping;
1321 /* Inline clones share declaration with their offline copies;
1322 do not modify their declarations since the offline copy may
1323 be different. */
1324 if (!w->global.inlined_to)
1325 switch (this_state)
1327 case IPA_CONST:
1328 if (!TREE_READONLY (w->decl))
1330 warn_function_const (w->decl, !this_looping);
1331 if (dump_file)
1332 fprintf (dump_file, "Function found to be %sconst: %s\n",
1333 this_looping ? "looping " : "",
1334 w->name ());
1336 w->set_const_flag (true, this_looping);
1337 break;
1339 case IPA_PURE:
1340 if (!DECL_PURE_P (w->decl))
1342 warn_function_pure (w->decl, !this_looping);
1343 if (dump_file)
1344 fprintf (dump_file, "Function found to be %spure: %s\n",
1345 this_looping ? "looping " : "",
1346 w->name ());
1348 w->set_pure_flag (true, this_looping);
1349 break;
1351 default:
1352 break;
1354 w_info = (struct ipa_dfs_info *) w->aux;
1355 w = w_info->next_cycle;
1359 ipa_free_postorder_info ();
1360 free (order);
1363 /* Produce transitive closure over the callgraph and compute nothrow
1364 attributes. */
1366 static void
1367 propagate_nothrow (void)
1369 struct cgraph_node *node;
1370 struct cgraph_node *w;
1371 struct cgraph_node **order =
1372 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1373 int order_pos;
1374 int i;
1375 struct ipa_dfs_info * w_info;
1377 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1378 if (dump_file)
1380 cgraph_node::dump_cgraph (dump_file);
1381 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1384 /* Propagate the local information through the call graph to produce
1385 the global information. All the nodes within a cycle will have
1386 the same info so we collapse cycles first. Then we can do the
1387 propagation in one pass from the leaves to the roots. */
1388 for (i = 0; i < order_pos; i++ )
1390 bool can_throw = false;
1391 node = order[i];
1393 if (node->alias)
1394 continue;
1396 /* Find the worst state for any node in the cycle. */
1397 w = node;
1398 while (w)
1400 struct cgraph_edge *e, *ie;
1401 funct_state w_l = get_function_state (w);
1403 if (w_l->can_throw
1404 || w->get_availability () == AVAIL_INTERPOSABLE)
1405 can_throw = true;
1407 if (can_throw)
1408 break;
1410 for (e = w->callees; e; e = e->next_callee)
1412 enum availability avail;
1413 struct cgraph_node *y = e->callee->function_symbol (&avail);
1415 if (avail > AVAIL_INTERPOSABLE)
1417 funct_state y_l = get_function_state (y);
1419 if (can_throw)
1420 break;
1421 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1422 && e->can_throw_external)
1423 can_throw = true;
1425 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1426 can_throw = true;
1428 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1429 if (ie->can_throw_external)
1431 can_throw = true;
1432 break;
1434 w_info = (struct ipa_dfs_info *) w->aux;
1435 w = w_info->next_cycle;
1438 /* Copy back the region's pure_const_state which is shared by
1439 all nodes in the region. */
1440 w = node;
1441 while (w)
1443 funct_state w_l = get_function_state (w);
1444 if (!can_throw && !TREE_NOTHROW (w->decl))
1446 /* Inline clones share declaration with their offline copies;
1447 do not modify their declarations since the offline copy may
1448 be different. */
1449 if (!w->global.inlined_to)
1451 w->set_nothrow_flag (true);
1452 if (dump_file)
1453 fprintf (dump_file, "Function found to be nothrow: %s\n",
1454 w->name ());
1457 else if (can_throw && !TREE_NOTHROW (w->decl))
1458 w_l->can_throw = true;
1459 w_info = (struct ipa_dfs_info *) w->aux;
1460 w = w_info->next_cycle;
1464 ipa_free_postorder_info ();
1465 free (order);
1469 /* Produce the global information by preforming a transitive closure
1470 on the local information that was produced by generate_summary. */
1472 static unsigned int
1473 propagate (void)
1475 struct cgraph_node *node;
1477 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1478 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1479 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1481 /* Nothrow makes more function to not lead to return and improve
1482 later analysis. */
1483 propagate_nothrow ();
1484 propagate_pure_const ();
1486 /* Cleanup. */
1487 FOR_EACH_FUNCTION (node)
1488 if (has_function_state (node))
1489 free (get_function_state (node));
1490 funct_state_vec.release ();
1491 return 0;
1494 static bool
1495 gate_pure_const (void)
1497 return (flag_ipa_pure_const
1498 /* Don't bother doing anything if the program has errors. */
1499 && !seen_error ());
1502 namespace {
1504 const pass_data pass_data_ipa_pure_const =
1506 IPA_PASS, /* type */
1507 "pure-const", /* name */
1508 OPTGROUP_NONE, /* optinfo_flags */
1509 TV_IPA_PURE_CONST, /* tv_id */
1510 0, /* properties_required */
1511 0, /* properties_provided */
1512 0, /* properties_destroyed */
1513 0, /* todo_flags_start */
1514 0, /* todo_flags_finish */
1517 class pass_ipa_pure_const : public ipa_opt_pass_d
1519 public:
1520 pass_ipa_pure_const (gcc::context *ctxt)
1521 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1522 pure_const_generate_summary, /* generate_summary */
1523 pure_const_write_summary, /* write_summary */
1524 pure_const_read_summary, /* read_summary */
1525 NULL, /* write_optimization_summary */
1526 NULL, /* read_optimization_summary */
1527 NULL, /* stmt_fixup */
1528 0, /* function_transform_todo_flags_start */
1529 NULL, /* function_transform */
1530 NULL) /* variable_transform */
1533 /* opt_pass methods: */
1534 virtual bool gate (function *) { return gate_pure_const (); }
1535 virtual unsigned int execute (function *) { return propagate (); }
1537 }; // class pass_ipa_pure_const
1539 } // anon namespace
1541 ipa_opt_pass_d *
1542 make_pass_ipa_pure_const (gcc::context *ctxt)
1544 return new pass_ipa_pure_const (ctxt);
1547 /* Return true if function should be skipped for local pure const analysis. */
1549 static bool
1550 skip_function_for_local_pure_const (struct cgraph_node *node)
1552 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1553 we must not promote functions that are called by already processed functions. */
1555 if (function_called_by_processed_nodes_p ())
1557 if (dump_file)
1558 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1559 return true;
1561 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1563 if (dump_file)
1564 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1565 return true;
1567 return false;
1570 /* Simple local pass for pure const discovery reusing the analysis from
1571 ipa_pure_const. This pass is effective when executed together with
1572 other optimization passes in early optimization pass queue. */
1574 namespace {
1576 const pass_data pass_data_local_pure_const =
1578 GIMPLE_PASS, /* type */
1579 "local-pure-const", /* name */
1580 OPTGROUP_NONE, /* optinfo_flags */
1581 TV_IPA_PURE_CONST, /* tv_id */
1582 0, /* properties_required */
1583 0, /* properties_provided */
1584 0, /* properties_destroyed */
1585 0, /* todo_flags_start */
1586 0, /* todo_flags_finish */
1589 class pass_local_pure_const : public gimple_opt_pass
1591 public:
1592 pass_local_pure_const (gcc::context *ctxt)
1593 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1596 /* opt_pass methods: */
1597 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1598 virtual bool gate (function *) { return gate_pure_const (); }
1599 virtual unsigned int execute (function *);
1601 }; // class pass_local_pure_const
1603 unsigned int
1604 pass_local_pure_const::execute (function *fun)
1606 bool changed = false;
1607 funct_state l;
1608 bool skip;
1609 struct cgraph_node *node;
1611 node = cgraph_node::get (current_function_decl);
1612 skip = skip_function_for_local_pure_const (node);
1613 if (!warn_suggest_attribute_const
1614 && !warn_suggest_attribute_pure
1615 && skip)
1616 return 0;
1618 l = analyze_function (node, false);
1620 /* Do NORETURN discovery. */
1621 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1622 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1624 warn_function_noreturn (fun->decl);
1625 if (dump_file)
1626 fprintf (dump_file, "Function found to be noreturn: %s\n",
1627 current_function_name ());
1629 /* Update declaration and reduce profile to executed once. */
1630 TREE_THIS_VOLATILE (current_function_decl) = 1;
1631 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1632 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1634 changed = true;
1637 switch (l->pure_const_state)
1639 case IPA_CONST:
1640 if (!TREE_READONLY (current_function_decl))
1642 warn_function_const (current_function_decl, !l->looping);
1643 if (!skip)
1645 node->set_const_flag (true, l->looping);
1646 changed = true;
1648 if (dump_file)
1649 fprintf (dump_file, "Function found to be %sconst: %s\n",
1650 l->looping ? "looping " : "",
1651 current_function_name ());
1653 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1654 && !l->looping)
1656 if (!skip)
1658 node->set_const_flag (true, false);
1659 changed = true;
1661 if (dump_file)
1662 fprintf (dump_file, "Function found to be non-looping: %s\n",
1663 current_function_name ());
1665 break;
1667 case IPA_PURE:
1668 if (!DECL_PURE_P (current_function_decl))
1670 if (!skip)
1672 node->set_pure_flag (true, l->looping);
1673 changed = true;
1675 warn_function_pure (current_function_decl, !l->looping);
1676 if (dump_file)
1677 fprintf (dump_file, "Function found to be %spure: %s\n",
1678 l->looping ? "looping " : "",
1679 current_function_name ());
1681 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1682 && !l->looping)
1684 if (!skip)
1686 node->set_pure_flag (true, false);
1687 changed = true;
1689 if (dump_file)
1690 fprintf (dump_file, "Function found to be non-looping: %s\n",
1691 current_function_name ());
1693 break;
1695 default:
1696 break;
1698 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1700 node->set_nothrow_flag (true);
1701 changed = true;
1702 if (dump_file)
1703 fprintf (dump_file, "Function found to be nothrow: %s\n",
1704 current_function_name ());
1706 free (l);
1707 if (changed)
1708 return execute_fixup_cfg ();
1709 else
1710 return 0;
1713 } // anon namespace
1715 gimple_opt_pass *
1716 make_pass_local_pure_const (gcc::context *ctxt)
1718 return new pass_local_pure_const (ctxt);
1721 /* Emit noreturn warnings. */
1723 namespace {
1725 const pass_data pass_data_warn_function_noreturn =
1727 GIMPLE_PASS, /* type */
1728 "*warn_function_noreturn", /* name */
1729 OPTGROUP_NONE, /* optinfo_flags */
1730 TV_NONE, /* tv_id */
1731 PROP_cfg, /* properties_required */
1732 0, /* properties_provided */
1733 0, /* properties_destroyed */
1734 0, /* todo_flags_start */
1735 0, /* todo_flags_finish */
1738 class pass_warn_function_noreturn : public gimple_opt_pass
1740 public:
1741 pass_warn_function_noreturn (gcc::context *ctxt)
1742 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1745 /* opt_pass methods: */
1746 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1747 virtual unsigned int execute (function *fun)
1749 if (!TREE_THIS_VOLATILE (current_function_decl)
1750 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1751 warn_function_noreturn (current_function_decl);
1752 return 0;
1755 }; // class pass_warn_function_noreturn
1757 } // anon namespace
1759 gimple_opt_pass *
1760 make_pass_warn_function_noreturn (gcc::context *ctxt)
1762 return new pass_warn_function_noreturn (ctxt);