re PR c++/62127 (ICE with VLA in constructor)
[official-gcc.git] / gcc / ipa-pure-const.c
blobb5ded3e73ed5e998a989e5a23b4b9d97291df823
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;
454 default:
455 break;
457 return false;
460 /* Check the parameters of a function call to CALL_EXPR to see if
461 there are any references in the parameters that are not allowed for
462 pure or const functions. Also check to see if this is either an
463 indirect call, a call outside the compilation unit, or has special
464 attributes that may also effect the purity. The CALL_EXPR node for
465 the entire call expression. */
467 static void
468 check_call (funct_state local, gimple call, bool ipa)
470 int flags = gimple_call_flags (call);
471 tree callee_t = gimple_call_fndecl (call);
472 bool possibly_throws = stmt_could_throw_p (call);
473 bool possibly_throws_externally = (possibly_throws
474 && stmt_can_throw_external (call));
476 if (possibly_throws)
478 unsigned int i;
479 for (i = 0; i < gimple_num_ops (call); i++)
480 if (gimple_op (call, i)
481 && tree_could_throw_p (gimple_op (call, i)))
483 if (possibly_throws && cfun->can_throw_non_call_exceptions)
485 if (dump_file)
486 fprintf (dump_file, " operand can throw; looping\n");
487 local->looping = true;
489 if (possibly_throws_externally)
491 if (dump_file)
492 fprintf (dump_file, " operand can throw externally\n");
493 local->can_throw = true;
498 /* The const and pure flags are set by a variety of places in the
499 compiler (including here). If someone has already set the flags
500 for the callee, (such as for some of the builtins) we will use
501 them, otherwise we will compute our own information.
503 Const and pure functions have less clobber effects than other
504 functions so we process these first. Otherwise if it is a call
505 outside the compilation unit or an indirect call we punt. This
506 leaves local calls which will be processed by following the call
507 graph. */
508 if (callee_t)
510 enum pure_const_state_e call_state;
511 bool call_looping;
513 if (special_builtin_state (&call_state, &call_looping, callee_t))
515 worse_state (&local->pure_const_state, &local->looping,
516 call_state, call_looping);
517 return;
519 /* When bad things happen to bad functions, they cannot be const
520 or pure. */
521 if (setjmp_call_p (callee_t))
523 if (dump_file)
524 fprintf (dump_file, " setjmp is not const/pure\n");
525 local->looping = true;
526 local->pure_const_state = IPA_NEITHER;
529 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
530 switch (DECL_FUNCTION_CODE (callee_t))
532 case BUILT_IN_LONGJMP:
533 case BUILT_IN_NONLOCAL_GOTO:
534 if (dump_file)
535 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
536 local->pure_const_state = IPA_NEITHER;
537 local->looping = true;
538 break;
539 default:
540 break;
544 /* When not in IPA mode, we can still handle self recursion. */
545 if (!ipa && callee_t
546 && recursive_call_p (current_function_decl, callee_t))
548 if (dump_file)
549 fprintf (dump_file, " Recursive call can loop.\n");
550 local->looping = true;
552 /* Either callee is unknown or we are doing local analysis.
553 Look to see if there are any bits available for the callee (such as by
554 declaration or because it is builtin) and process solely on the basis of
555 those bits. */
556 else if (!ipa)
558 enum pure_const_state_e call_state;
559 bool call_looping;
560 if (possibly_throws && cfun->can_throw_non_call_exceptions)
562 if (dump_file)
563 fprintf (dump_file, " can throw; looping\n");
564 local->looping = true;
566 if (possibly_throws_externally)
568 if (dump_file)
570 fprintf (dump_file, " can throw externally to lp %i\n",
571 lookup_stmt_eh_lp (call));
572 if (callee_t)
573 fprintf (dump_file, " callee:%s\n",
574 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
576 local->can_throw = true;
578 if (dump_file && (dump_flags & TDF_DETAILS))
579 fprintf (dump_file, " checking flags for call:");
580 state_from_flags (&call_state, &call_looping, flags,
581 ((flags & (ECF_NORETURN | ECF_NOTHROW))
582 == (ECF_NORETURN | ECF_NOTHROW))
583 || (!flag_exceptions && (flags & ECF_NORETURN)));
584 worse_state (&local->pure_const_state, &local->looping,
585 call_state, call_looping);
587 /* Direct functions calls are handled by IPA propagation. */
590 /* Wrapper around check_decl for loads in local more. */
592 static bool
593 check_load (gimple, tree op, tree, void *data)
595 if (DECL_P (op))
596 check_decl ((funct_state)data, op, false, false);
597 else
598 check_op ((funct_state)data, op, false);
599 return false;
602 /* Wrapper around check_decl for stores in local more. */
604 static bool
605 check_store (gimple, tree op, tree, void *data)
607 if (DECL_P (op))
608 check_decl ((funct_state)data, op, true, false);
609 else
610 check_op ((funct_state)data, op, true);
611 return false;
614 /* Wrapper around check_decl for loads in ipa mode. */
616 static bool
617 check_ipa_load (gimple, tree op, tree, void *data)
619 if (DECL_P (op))
620 check_decl ((funct_state)data, op, false, true);
621 else
622 check_op ((funct_state)data, op, false);
623 return false;
626 /* Wrapper around check_decl for stores in ipa mode. */
628 static bool
629 check_ipa_store (gimple, tree op, tree, void *data)
631 if (DECL_P (op))
632 check_decl ((funct_state)data, op, true, true);
633 else
634 check_op ((funct_state)data, op, true);
635 return false;
638 /* Look into pointer pointed to by GSIP and figure out what interesting side
639 effects it has. */
640 static void
641 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
643 gimple stmt = gsi_stmt (*gsip);
645 if (is_gimple_debug (stmt))
646 return;
648 if (dump_file)
650 fprintf (dump_file, " scanning: ");
651 print_gimple_stmt (dump_file, stmt, 0, 0);
654 if (gimple_has_volatile_ops (stmt)
655 && !gimple_clobber_p (stmt))
657 local->pure_const_state = IPA_NEITHER;
658 if (dump_file)
659 fprintf (dump_file, " Volatile stmt is not const/pure\n");
662 /* Look for loads and stores. */
663 walk_stmt_load_store_ops (stmt, local,
664 ipa ? check_ipa_load : check_load,
665 ipa ? check_ipa_store : check_store);
667 if (gimple_code (stmt) != GIMPLE_CALL
668 && stmt_could_throw_p (stmt))
670 if (cfun->can_throw_non_call_exceptions)
672 if (dump_file)
673 fprintf (dump_file, " can throw; looping\n");
674 local->looping = true;
676 if (stmt_can_throw_external (stmt))
678 if (dump_file)
679 fprintf (dump_file, " can throw externally\n");
680 local->can_throw = true;
682 else
683 if (dump_file)
684 fprintf (dump_file, " can throw\n");
686 switch (gimple_code (stmt))
688 case GIMPLE_CALL:
689 check_call (local, stmt, ipa);
690 break;
691 case GIMPLE_LABEL:
692 if (DECL_NONLOCAL (gimple_label_label (stmt)))
693 /* Target of long jump. */
695 if (dump_file)
696 fprintf (dump_file, " nonlocal label is not const/pure\n");
697 local->pure_const_state = IPA_NEITHER;
699 break;
700 case GIMPLE_ASM:
701 if (gimple_asm_clobbers_memory_p (stmt))
703 if (dump_file)
704 fprintf (dump_file, " memory asm clobber is not const/pure\n");
705 /* Abandon all hope, ye who enter here. */
706 local->pure_const_state = IPA_NEITHER;
708 if (gimple_asm_volatile_p (stmt))
710 if (dump_file)
711 fprintf (dump_file, " volatile is not const/pure\n");
712 /* Abandon all hope, ye who enter here. */
713 local->pure_const_state = IPA_NEITHER;
714 local->looping = true;
716 return;
717 default:
718 break;
723 /* This is the main routine for finding the reference patterns for
724 global variables within a function FN. */
726 static funct_state
727 analyze_function (struct cgraph_node *fn, bool ipa)
729 tree decl = fn->decl;
730 funct_state l;
731 basic_block this_block;
733 l = XCNEW (struct funct_state_d);
734 l->pure_const_state = IPA_CONST;
735 l->state_previously_known = IPA_NEITHER;
736 l->looping_previously_known = true;
737 l->looping = false;
738 l->can_throw = false;
739 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
740 flags_from_decl_or_type (fn->decl),
741 fn->cannot_return_p ());
743 if (fn->thunk.thunk_p || fn->alias)
745 /* Thunk gets propagated through, so nothing interesting happens. */
746 gcc_assert (ipa);
747 return l;
750 if (dump_file)
752 fprintf (dump_file, "\n\n local analysis of %s\n ",
753 fn->name ());
756 push_cfun (DECL_STRUCT_FUNCTION (decl));
758 FOR_EACH_BB_FN (this_block, cfun)
760 gimple_stmt_iterator gsi;
761 struct walk_stmt_info wi;
763 memset (&wi, 0, sizeof (wi));
764 for (gsi = gsi_start_bb (this_block);
765 !gsi_end_p (gsi);
766 gsi_next (&gsi))
768 check_stmt (&gsi, l, ipa);
769 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
770 goto end;
774 end:
775 if (l->pure_const_state != IPA_NEITHER)
777 /* Const functions cannot have back edges (an
778 indication of possible infinite loop side
779 effect. */
780 if (mark_dfs_back_edges ())
782 /* Preheaders are needed for SCEV to work.
783 Simple latches and recorded exits improve chances that loop will
784 proved to be finite in testcases such as in loop-15.c
785 and loop-24.c */
786 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
787 | LOOPS_HAVE_SIMPLE_LATCHES
788 | LOOPS_HAVE_RECORDED_EXITS);
789 if (dump_file && (dump_flags & TDF_DETAILS))
790 flow_loops_dump (dump_file, NULL, 0);
791 if (mark_irreducible_loops ())
793 if (dump_file)
794 fprintf (dump_file, " has irreducible loops\n");
795 l->looping = true;
797 else
799 struct loop *loop;
800 scev_initialize ();
801 FOR_EACH_LOOP (loop, 0)
802 if (!finite_loop_p (loop))
804 if (dump_file)
805 fprintf (dump_file, " can not prove finiteness of "
806 "loop %i\n", loop->num);
807 l->looping =true;
808 break;
810 scev_finalize ();
812 loop_optimizer_finalize ();
816 if (dump_file && (dump_flags & TDF_DETAILS))
817 fprintf (dump_file, " checking previously known:");
819 better_state (&l->pure_const_state, &l->looping,
820 l->state_previously_known,
821 l->looping_previously_known);
822 if (TREE_NOTHROW (decl))
823 l->can_throw = false;
825 pop_cfun ();
826 if (dump_file)
828 if (l->looping)
829 fprintf (dump_file, "Function is locally looping.\n");
830 if (l->can_throw)
831 fprintf (dump_file, "Function is locally throwing.\n");
832 if (l->pure_const_state == IPA_CONST)
833 fprintf (dump_file, "Function is locally const.\n");
834 if (l->pure_const_state == IPA_PURE)
835 fprintf (dump_file, "Function is locally pure.\n");
837 return l;
840 /* Called when new function is inserted to callgraph late. */
841 static void
842 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
844 if (node->get_availability () < AVAIL_INTERPOSABLE)
845 return;
846 /* There are some shared nodes, in particular the initializers on
847 static declarations. We do not need to scan them more than once
848 since all we would be interested in are the addressof
849 operations. */
850 if (node->get_availability () > AVAIL_INTERPOSABLE)
851 set_function_state (node, analyze_function (node, true));
854 /* Called when new clone is inserted to callgraph late. */
856 static void
857 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
858 void *data ATTRIBUTE_UNUSED)
860 if (has_function_state (src))
862 funct_state l = XNEW (struct funct_state_d);
863 gcc_assert (!has_function_state (dst));
864 memcpy (l, get_function_state (src), sizeof (*l));
865 set_function_state (dst, l);
869 /* Called when new clone is inserted to callgraph late. */
871 static void
872 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
874 if (has_function_state (node))
876 funct_state l = get_function_state (node);
877 if (l != &varying_state)
878 free (l);
879 set_function_state (node, NULL);
884 static void
885 register_hooks (void)
887 static bool init_p = false;
889 if (init_p)
890 return;
892 init_p = true;
894 node_removal_hook_holder =
895 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
896 node_duplication_hook_holder =
897 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
898 function_insertion_hook_holder =
899 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
903 /* Analyze each function in the cgraph to see if it is locally PURE or
904 CONST. */
906 static void
907 pure_const_generate_summary (void)
909 struct cgraph_node *node;
911 register_hooks ();
913 /* Process all of the functions.
915 We process AVAIL_INTERPOSABLE 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 (node->get_availability () >= AVAIL_INTERPOSABLE)
921 set_function_state (node, analyze_function (node, true));
925 /* Serialize the ipa info for lto. */
927 static void
928 pure_const_write_summary (void)
930 struct cgraph_node *node;
931 struct lto_simple_output_block *ob
932 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
933 unsigned int count = 0;
934 lto_symtab_encoder_iterator lsei;
935 lto_symtab_encoder_t encoder;
937 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
939 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
940 lsei_next_function_in_partition (&lsei))
942 node = lsei_cgraph_node (lsei);
943 if (node->definition && has_function_state (node))
944 count++;
947 streamer_write_uhwi_stream (ob->main_stream, count);
949 /* Process all of the functions. */
950 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
951 lsei_next_function_in_partition (&lsei))
953 node = lsei_cgraph_node (lsei);
954 if (node->definition && has_function_state (node))
956 struct bitpack_d bp;
957 funct_state fs;
958 int node_ref;
959 lto_symtab_encoder_t encoder;
961 fs = get_function_state (node);
963 encoder = ob->decl_state->symtab_node_encoder;
964 node_ref = lto_symtab_encoder_encode (encoder, node);
965 streamer_write_uhwi_stream (ob->main_stream, node_ref);
967 /* Note that flags will need to be read in the opposite
968 order as we are pushing the bitflags into FLAGS. */
969 bp = bitpack_create (ob->main_stream);
970 bp_pack_value (&bp, fs->pure_const_state, 2);
971 bp_pack_value (&bp, fs->state_previously_known, 2);
972 bp_pack_value (&bp, fs->looping_previously_known, 1);
973 bp_pack_value (&bp, fs->looping, 1);
974 bp_pack_value (&bp, fs->can_throw, 1);
975 streamer_write_bitpack (&bp);
979 lto_destroy_simple_output_block (ob);
983 /* Deserialize the ipa info for lto. */
985 static void
986 pure_const_read_summary (void)
988 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
989 struct lto_file_decl_data *file_data;
990 unsigned int j = 0;
992 register_hooks ();
993 while ((file_data = file_data_vec[j++]))
995 const char *data;
996 size_t len;
997 struct lto_input_block *ib
998 = lto_create_simple_input_block (file_data,
999 LTO_section_ipa_pure_const,
1000 &data, &len);
1001 if (ib)
1003 unsigned int i;
1004 unsigned int count = streamer_read_uhwi (ib);
1006 for (i = 0; i < count; i++)
1008 unsigned int index;
1009 struct cgraph_node *node;
1010 struct bitpack_d bp;
1011 funct_state fs;
1012 lto_symtab_encoder_t encoder;
1014 fs = XCNEW (struct funct_state_d);
1015 index = streamer_read_uhwi (ib);
1016 encoder = file_data->symtab_node_encoder;
1017 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1018 index));
1019 set_function_state (node, fs);
1021 /* Note that the flags must be read in the opposite
1022 order in which they were written (the bitflags were
1023 pushed into FLAGS). */
1024 bp = streamer_read_bitpack (ib);
1025 fs->pure_const_state
1026 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1027 fs->state_previously_known
1028 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1029 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1030 fs->looping = bp_unpack_value (&bp, 1);
1031 fs->can_throw = bp_unpack_value (&bp, 1);
1032 if (dump_file)
1034 int flags = flags_from_decl_or_type (node->decl);
1035 fprintf (dump_file, "Read info for %s/%i ",
1036 node->name (),
1037 node->order);
1038 if (flags & ECF_CONST)
1039 fprintf (dump_file, " const");
1040 if (flags & ECF_PURE)
1041 fprintf (dump_file, " pure");
1042 if (flags & ECF_NOTHROW)
1043 fprintf (dump_file, " nothrow");
1044 fprintf (dump_file, "\n pure const state: %s\n",
1045 pure_const_names[fs->pure_const_state]);
1046 fprintf (dump_file, " previously known state: %s\n",
1047 pure_const_names[fs->looping_previously_known]);
1048 if (fs->looping)
1049 fprintf (dump_file," function is locally looping\n");
1050 if (fs->looping_previously_known)
1051 fprintf (dump_file," function is previously known looping\n");
1052 if (fs->can_throw)
1053 fprintf (dump_file," function is locally throwing\n");
1057 lto_destroy_simple_input_block (file_data,
1058 LTO_section_ipa_pure_const,
1059 ib, data, len);
1065 static bool
1066 ignore_edge (struct cgraph_edge *e)
1068 return (!e->can_throw_external);
1071 /* Return true if NODE is self recursive function.
1072 Indirectly recursive functions appears as non-trivial strongly
1073 connected components, so we need to care about self recursion
1074 only. */
1076 static bool
1077 self_recursive_p (struct cgraph_node *node)
1079 struct cgraph_edge *e;
1080 for (e = node->callees; e; e = e->next_callee)
1081 if (e->callee->function_symbol () == node)
1082 return true;
1083 return false;
1086 /* Produce transitive closure over the callgraph and compute pure/const
1087 attributes. */
1089 static void
1090 propagate_pure_const (void)
1092 struct cgraph_node *node;
1093 struct cgraph_node *w;
1094 struct cgraph_node **order =
1095 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1096 int order_pos;
1097 int i;
1098 struct ipa_dfs_info * w_info;
1100 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1101 if (dump_file)
1103 cgraph_node::dump_cgraph (dump_file);
1104 ipa_print_order (dump_file, "reduced", order, order_pos);
1107 /* Propagate the local information through the call graph to produce
1108 the global information. All the nodes within a cycle will have
1109 the same info so we collapse cycles first. Then we can do the
1110 propagation in one pass from the leaves to the roots. */
1111 for (i = 0; i < order_pos; i++ )
1113 enum pure_const_state_e pure_const_state = IPA_CONST;
1114 bool looping = false;
1115 int count = 0;
1116 node = order[i];
1118 if (node->alias)
1119 continue;
1121 if (dump_file && (dump_flags & TDF_DETAILS))
1122 fprintf (dump_file, "Starting cycle\n");
1124 /* Find the worst state for any node in the cycle. */
1125 w = node;
1126 while (w && pure_const_state != IPA_NEITHER)
1128 struct cgraph_edge *e;
1129 struct cgraph_edge *ie;
1130 int i;
1131 struct ipa_ref *ref = NULL;
1133 funct_state w_l = get_function_state (w);
1134 if (dump_file && (dump_flags & TDF_DETAILS))
1135 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1136 w->name (),
1137 w->order,
1138 pure_const_names[w_l->pure_const_state],
1139 w_l->looping);
1141 /* First merge in function body properties. */
1142 worse_state (&pure_const_state, &looping,
1143 w_l->pure_const_state, w_l->looping);
1144 if (pure_const_state == IPA_NEITHER)
1145 break;
1147 /* For overwritable nodes we can not assume anything. */
1148 if (w->get_availability () == AVAIL_INTERPOSABLE)
1150 worse_state (&pure_const_state, &looping,
1151 w_l->state_previously_known,
1152 w_l->looping_previously_known);
1153 if (dump_file && (dump_flags & TDF_DETAILS))
1155 fprintf (dump_file,
1156 " Overwritable. state %s looping %i\n",
1157 pure_const_names[w_l->state_previously_known],
1158 w_l->looping_previously_known);
1160 break;
1163 count++;
1165 /* We consider recursive cycles as possibly infinite.
1166 This might be relaxed since infinite recursion leads to stack
1167 overflow. */
1168 if (count > 1)
1169 looping = true;
1171 /* Now walk the edges and merge in callee properties. */
1172 for (e = w->callees; e; e = e->next_callee)
1174 enum availability avail;
1175 struct cgraph_node *y = e->callee->function_symbol (&avail);
1176 enum pure_const_state_e edge_state = IPA_CONST;
1177 bool edge_looping = false;
1179 if (dump_file && (dump_flags & TDF_DETAILS))
1181 fprintf (dump_file,
1182 " Call to %s/%i",
1183 e->callee->name (),
1184 e->callee->order);
1186 if (avail > AVAIL_INTERPOSABLE)
1188 funct_state y_l = get_function_state (y);
1189 if (dump_file && (dump_flags & TDF_DETAILS))
1191 fprintf (dump_file,
1192 " state:%s looping:%i\n",
1193 pure_const_names[y_l->pure_const_state],
1194 y_l->looping);
1196 if (y_l->pure_const_state > IPA_PURE
1197 && e->cannot_lead_to_return_p ())
1199 if (dump_file && (dump_flags & TDF_DETAILS))
1200 fprintf (dump_file,
1201 " Ignoring side effects"
1202 " -> pure, looping\n");
1203 edge_state = IPA_PURE;
1204 edge_looping = true;
1206 else
1208 edge_state = y_l->pure_const_state;
1209 edge_looping = y_l->looping;
1212 else if (special_builtin_state (&edge_state, &edge_looping,
1213 y->decl))
1215 else
1216 state_from_flags (&edge_state, &edge_looping,
1217 flags_from_decl_or_type (y->decl),
1218 e->cannot_lead_to_return_p ());
1220 /* Merge the results with what we already know. */
1221 better_state (&edge_state, &edge_looping,
1222 w_l->state_previously_known,
1223 w_l->looping_previously_known);
1224 worse_state (&pure_const_state, &looping,
1225 edge_state, edge_looping);
1226 if (pure_const_state == IPA_NEITHER)
1227 break;
1229 if (pure_const_state == IPA_NEITHER)
1230 break;
1232 /* Now process the indirect call. */
1233 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1235 enum pure_const_state_e edge_state = IPA_CONST;
1236 bool edge_looping = false;
1238 if (dump_file && (dump_flags & TDF_DETAILS))
1239 fprintf (dump_file, " Indirect call");
1240 state_from_flags (&edge_state, &edge_looping,
1241 ie->indirect_info->ecf_flags,
1242 ie->cannot_lead_to_return_p ());
1243 /* Merge the results with what we already know. */
1244 better_state (&edge_state, &edge_looping,
1245 w_l->state_previously_known,
1246 w_l->looping_previously_known);
1247 worse_state (&pure_const_state, &looping,
1248 edge_state, edge_looping);
1249 if (pure_const_state == IPA_NEITHER)
1250 break;
1252 if (pure_const_state == IPA_NEITHER)
1253 break;
1255 /* And finally all loads and stores. */
1256 for (i = 0; w->iterate_reference (i, ref); i++)
1258 enum pure_const_state_e ref_state = IPA_CONST;
1259 bool ref_looping = false;
1260 switch (ref->use)
1262 case IPA_REF_LOAD:
1263 /* readonly reads are safe. */
1264 if (TREE_READONLY (ref->referred->decl))
1265 break;
1266 if (dump_file && (dump_flags & TDF_DETAILS))
1267 fprintf (dump_file, " nonreadonly global var read\n");
1268 ref_state = IPA_PURE;
1269 break;
1270 case IPA_REF_STORE:
1271 if (ref->cannot_lead_to_return ())
1272 break;
1273 ref_state = IPA_NEITHER;
1274 if (dump_file && (dump_flags & TDF_DETAILS))
1275 fprintf (dump_file, " global var write\n");
1276 break;
1277 case IPA_REF_ADDR:
1278 break;
1279 default:
1280 gcc_unreachable ();
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 /* Inline clones share declaration with their offline copies;
1323 do not modify their declarations since the offline copy may
1324 be different. */
1325 if (!w->global.inlined_to)
1326 switch (this_state)
1328 case IPA_CONST:
1329 if (!TREE_READONLY (w->decl))
1331 warn_function_const (w->decl, !this_looping);
1332 if (dump_file)
1333 fprintf (dump_file, "Function found to be %sconst: %s\n",
1334 this_looping ? "looping " : "",
1335 w->name ());
1337 w->set_const_flag (true, this_looping);
1338 break;
1340 case IPA_PURE:
1341 if (!DECL_PURE_P (w->decl))
1343 warn_function_pure (w->decl, !this_looping);
1344 if (dump_file)
1345 fprintf (dump_file, "Function found to be %spure: %s\n",
1346 this_looping ? "looping " : "",
1347 w->name ());
1349 w->set_pure_flag (true, this_looping);
1350 break;
1352 default:
1353 break;
1355 w_info = (struct ipa_dfs_info *) w->aux;
1356 w = w_info->next_cycle;
1360 ipa_free_postorder_info ();
1361 free (order);
1364 /* Produce transitive closure over the callgraph and compute nothrow
1365 attributes. */
1367 static void
1368 propagate_nothrow (void)
1370 struct cgraph_node *node;
1371 struct cgraph_node *w;
1372 struct cgraph_node **order =
1373 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1374 int order_pos;
1375 int i;
1376 struct ipa_dfs_info * w_info;
1378 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1379 if (dump_file)
1381 cgraph_node::dump_cgraph (dump_file);
1382 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1385 /* Propagate the local information through the call graph to produce
1386 the global information. All the nodes within a cycle will have
1387 the same info so we collapse cycles first. Then we can do the
1388 propagation in one pass from the leaves to the roots. */
1389 for (i = 0; i < order_pos; i++ )
1391 bool can_throw = false;
1392 node = order[i];
1394 if (node->alias)
1395 continue;
1397 /* Find the worst state for any node in the cycle. */
1398 w = node;
1399 while (w)
1401 struct cgraph_edge *e, *ie;
1402 funct_state w_l = get_function_state (w);
1404 if (w_l->can_throw
1405 || w->get_availability () == AVAIL_INTERPOSABLE)
1406 can_throw = true;
1408 if (can_throw)
1409 break;
1411 for (e = w->callees; e; e = e->next_callee)
1413 enum availability avail;
1414 struct cgraph_node *y = e->callee->function_symbol (&avail);
1416 if (avail > AVAIL_INTERPOSABLE)
1418 funct_state y_l = get_function_state (y);
1420 if (can_throw)
1421 break;
1422 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1423 && e->can_throw_external)
1424 can_throw = true;
1426 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1427 can_throw = true;
1429 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1430 if (ie->can_throw_external)
1432 can_throw = true;
1433 break;
1435 w_info = (struct ipa_dfs_info *) w->aux;
1436 w = w_info->next_cycle;
1439 /* Copy back the region's pure_const_state which is shared by
1440 all nodes in the region. */
1441 w = node;
1442 while (w)
1444 funct_state w_l = get_function_state (w);
1445 if (!can_throw && !TREE_NOTHROW (w->decl))
1447 /* Inline clones share declaration with their offline copies;
1448 do not modify their declarations since the offline copy may
1449 be different. */
1450 if (!w->global.inlined_to)
1452 w->set_nothrow_flag (true);
1453 if (dump_file)
1454 fprintf (dump_file, "Function found to be nothrow: %s\n",
1455 w->name ());
1458 else if (can_throw && !TREE_NOTHROW (w->decl))
1459 w_l->can_throw = true;
1460 w_info = (struct ipa_dfs_info *) w->aux;
1461 w = w_info->next_cycle;
1465 ipa_free_postorder_info ();
1466 free (order);
1470 /* Produce the global information by preforming a transitive closure
1471 on the local information that was produced by generate_summary. */
1473 static unsigned int
1474 propagate (void)
1476 struct cgraph_node *node;
1478 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1479 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1480 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1482 /* Nothrow makes more function to not lead to return and improve
1483 later analysis. */
1484 propagate_nothrow ();
1485 propagate_pure_const ();
1487 /* Cleanup. */
1488 FOR_EACH_FUNCTION (node)
1489 if (has_function_state (node))
1490 free (get_function_state (node));
1491 funct_state_vec.release ();
1492 return 0;
1495 static bool
1496 gate_pure_const (void)
1498 return (flag_ipa_pure_const
1499 /* Don't bother doing anything if the program has errors. */
1500 && !seen_error ());
1503 namespace {
1505 const pass_data pass_data_ipa_pure_const =
1507 IPA_PASS, /* type */
1508 "pure-const", /* name */
1509 OPTGROUP_NONE, /* optinfo_flags */
1510 TV_IPA_PURE_CONST, /* tv_id */
1511 0, /* properties_required */
1512 0, /* properties_provided */
1513 0, /* properties_destroyed */
1514 0, /* todo_flags_start */
1515 0, /* todo_flags_finish */
1518 class pass_ipa_pure_const : public ipa_opt_pass_d
1520 public:
1521 pass_ipa_pure_const (gcc::context *ctxt)
1522 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1523 pure_const_generate_summary, /* generate_summary */
1524 pure_const_write_summary, /* write_summary */
1525 pure_const_read_summary, /* read_summary */
1526 NULL, /* write_optimization_summary */
1527 NULL, /* read_optimization_summary */
1528 NULL, /* stmt_fixup */
1529 0, /* function_transform_todo_flags_start */
1530 NULL, /* function_transform */
1531 NULL) /* variable_transform */
1534 /* opt_pass methods: */
1535 virtual bool gate (function *) { return gate_pure_const (); }
1536 virtual unsigned int execute (function *) { return propagate (); }
1538 }; // class pass_ipa_pure_const
1540 } // anon namespace
1542 ipa_opt_pass_d *
1543 make_pass_ipa_pure_const (gcc::context *ctxt)
1545 return new pass_ipa_pure_const (ctxt);
1548 /* Return true if function should be skipped for local pure const analysis. */
1550 static bool
1551 skip_function_for_local_pure_const (struct cgraph_node *node)
1553 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1554 we must not promote functions that are called by already processed functions. */
1556 if (function_called_by_processed_nodes_p ())
1558 if (dump_file)
1559 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1560 return true;
1562 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1564 if (dump_file)
1565 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1566 return true;
1568 return false;
1571 /* Simple local pass for pure const discovery reusing the analysis from
1572 ipa_pure_const. This pass is effective when executed together with
1573 other optimization passes in early optimization pass queue. */
1575 namespace {
1577 const pass_data pass_data_local_pure_const =
1579 GIMPLE_PASS, /* type */
1580 "local-pure-const", /* name */
1581 OPTGROUP_NONE, /* optinfo_flags */
1582 TV_IPA_PURE_CONST, /* tv_id */
1583 0, /* properties_required */
1584 0, /* properties_provided */
1585 0, /* properties_destroyed */
1586 0, /* todo_flags_start */
1587 0, /* todo_flags_finish */
1590 class pass_local_pure_const : public gimple_opt_pass
1592 public:
1593 pass_local_pure_const (gcc::context *ctxt)
1594 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1597 /* opt_pass methods: */
1598 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1599 virtual bool gate (function *) { return gate_pure_const (); }
1600 virtual unsigned int execute (function *);
1602 }; // class pass_local_pure_const
1604 unsigned int
1605 pass_local_pure_const::execute (function *fun)
1607 bool changed = false;
1608 funct_state l;
1609 bool skip;
1610 struct cgraph_node *node;
1612 node = cgraph_node::get (current_function_decl);
1613 skip = skip_function_for_local_pure_const (node);
1614 if (!warn_suggest_attribute_const
1615 && !warn_suggest_attribute_pure
1616 && skip)
1617 return 0;
1619 l = analyze_function (node, false);
1621 /* Do NORETURN discovery. */
1622 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1623 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1625 warn_function_noreturn (fun->decl);
1626 if (dump_file)
1627 fprintf (dump_file, "Function found to be noreturn: %s\n",
1628 current_function_name ());
1630 /* Update declaration and reduce profile to executed once. */
1631 TREE_THIS_VOLATILE (current_function_decl) = 1;
1632 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1633 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1635 changed = true;
1638 switch (l->pure_const_state)
1640 case IPA_CONST:
1641 if (!TREE_READONLY (current_function_decl))
1643 warn_function_const (current_function_decl, !l->looping);
1644 if (!skip)
1646 node->set_const_flag (true, l->looping);
1647 changed = true;
1649 if (dump_file)
1650 fprintf (dump_file, "Function found to be %sconst: %s\n",
1651 l->looping ? "looping " : "",
1652 current_function_name ());
1654 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1655 && !l->looping)
1657 if (!skip)
1659 node->set_const_flag (true, false);
1660 changed = true;
1662 if (dump_file)
1663 fprintf (dump_file, "Function found to be non-looping: %s\n",
1664 current_function_name ());
1666 break;
1668 case IPA_PURE:
1669 if (!DECL_PURE_P (current_function_decl))
1671 if (!skip)
1673 node->set_pure_flag (true, l->looping);
1674 changed = true;
1676 warn_function_pure (current_function_decl, !l->looping);
1677 if (dump_file)
1678 fprintf (dump_file, "Function found to be %spure: %s\n",
1679 l->looping ? "looping " : "",
1680 current_function_name ());
1682 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1683 && !l->looping)
1685 if (!skip)
1687 node->set_pure_flag (true, false);
1688 changed = true;
1690 if (dump_file)
1691 fprintf (dump_file, "Function found to be non-looping: %s\n",
1692 current_function_name ());
1694 break;
1696 default:
1697 break;
1699 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1701 node->set_nothrow_flag (true);
1702 changed = true;
1703 if (dump_file)
1704 fprintf (dump_file, "Function found to be nothrow: %s\n",
1705 current_function_name ());
1707 free (l);
1708 if (changed)
1709 return execute_fixup_cfg ();
1710 else
1711 return 0;
1714 } // anon namespace
1716 gimple_opt_pass *
1717 make_pass_local_pure_const (gcc::context *ctxt)
1719 return new pass_local_pure_const (ctxt);
1722 /* Emit noreturn warnings. */
1724 namespace {
1726 const pass_data pass_data_warn_function_noreturn =
1728 GIMPLE_PASS, /* type */
1729 "*warn_function_noreturn", /* name */
1730 OPTGROUP_NONE, /* optinfo_flags */
1731 TV_NONE, /* tv_id */
1732 PROP_cfg, /* properties_required */
1733 0, /* properties_provided */
1734 0, /* properties_destroyed */
1735 0, /* todo_flags_start */
1736 0, /* todo_flags_finish */
1739 class pass_warn_function_noreturn : public gimple_opt_pass
1741 public:
1742 pass_warn_function_noreturn (gcc::context *ctxt)
1743 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1746 /* opt_pass methods: */
1747 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1748 virtual unsigned int execute (function *fun)
1750 if (!TREE_THIS_VOLATILE (current_function_decl)
1751 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1752 warn_function_noreturn (current_function_decl);
1753 return 0;
1756 }; // class pass_warn_function_noreturn
1758 } // anon namespace
1760 gimple_opt_pass *
1761 make_pass_warn_function_noreturn (gcc::context *ctxt)
1763 return new pass_warn_function_noreturn (ctxt);