2014-09-18 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ipa-pure-const.c
blob459d08db3b3a72c10d9b8fddb147d1df86a7b125
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 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
894 node_duplication_hook_holder =
895 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
896 function_insertion_hook_holder =
897 symtab->add_cgraph_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 *, symtab->cgraph_count);
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 && e->cannot_lead_to_return_p ())
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 e->cannot_lead_to_return_p ());
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 ie->cannot_lead_to_return_p ());
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 break;
1277 default:
1278 gcc_unreachable ();
1280 better_state (&ref_state, &ref_looping,
1281 w_l->state_previously_known,
1282 w_l->looping_previously_known);
1283 worse_state (&pure_const_state, &looping,
1284 ref_state, ref_looping);
1285 if (pure_const_state == IPA_NEITHER)
1286 break;
1288 w_info = (struct ipa_dfs_info *) w->aux;
1289 w = w_info->next_cycle;
1291 if (dump_file && (dump_flags & TDF_DETAILS))
1292 fprintf (dump_file, "Result %s looping %i\n",
1293 pure_const_names [pure_const_state],
1294 looping);
1296 /* Copy back the region's pure_const_state which is shared by
1297 all nodes in the region. */
1298 w = node;
1299 while (w)
1301 funct_state w_l = get_function_state (w);
1302 enum pure_const_state_e this_state = pure_const_state;
1303 bool this_looping = looping;
1305 if (w_l->state_previously_known != IPA_NEITHER
1306 && this_state > w_l->state_previously_known)
1308 this_state = w_l->state_previously_known;
1309 this_looping |= w_l->looping_previously_known;
1311 if (!this_looping && self_recursive_p (w))
1312 this_looping = true;
1313 if (!w_l->looping_previously_known)
1314 this_looping = false;
1316 /* All nodes within a cycle share the same info. */
1317 w_l->pure_const_state = this_state;
1318 w_l->looping = this_looping;
1320 /* Inline clones share declaration with their offline copies;
1321 do not modify their declarations since the offline copy may
1322 be different. */
1323 if (!w->global.inlined_to)
1324 switch (this_state)
1326 case IPA_CONST:
1327 if (!TREE_READONLY (w->decl))
1329 warn_function_const (w->decl, !this_looping);
1330 if (dump_file)
1331 fprintf (dump_file, "Function found to be %sconst: %s\n",
1332 this_looping ? "looping " : "",
1333 w->name ());
1335 w->set_const_flag (true, this_looping);
1336 break;
1338 case IPA_PURE:
1339 if (!DECL_PURE_P (w->decl))
1341 warn_function_pure (w->decl, !this_looping);
1342 if (dump_file)
1343 fprintf (dump_file, "Function found to be %spure: %s\n",
1344 this_looping ? "looping " : "",
1345 w->name ());
1347 w->set_pure_flag (true, this_looping);
1348 break;
1350 default:
1351 break;
1353 w_info = (struct ipa_dfs_info *) w->aux;
1354 w = w_info->next_cycle;
1358 ipa_free_postorder_info ();
1359 free (order);
1362 /* Produce transitive closure over the callgraph and compute nothrow
1363 attributes. */
1365 static void
1366 propagate_nothrow (void)
1368 struct cgraph_node *node;
1369 struct cgraph_node *w;
1370 struct cgraph_node **order =
1371 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1372 int order_pos;
1373 int i;
1374 struct ipa_dfs_info * w_info;
1376 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1377 if (dump_file)
1379 cgraph_node::dump_cgraph (dump_file);
1380 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1383 /* Propagate the local information through the call graph to produce
1384 the global information. All the nodes within a cycle will have
1385 the same info so we collapse cycles first. Then we can do the
1386 propagation in one pass from the leaves to the roots. */
1387 for (i = 0; i < order_pos; i++ )
1389 bool can_throw = false;
1390 node = order[i];
1392 if (node->alias)
1393 continue;
1395 /* Find the worst state for any node in the cycle. */
1396 w = node;
1397 while (w)
1399 struct cgraph_edge *e, *ie;
1400 funct_state w_l = get_function_state (w);
1402 if (w_l->can_throw
1403 || w->get_availability () == AVAIL_INTERPOSABLE)
1404 can_throw = true;
1406 if (can_throw)
1407 break;
1409 for (e = w->callees; e; e = e->next_callee)
1411 enum availability avail;
1412 struct cgraph_node *y = e->callee->function_symbol (&avail);
1414 if (avail > AVAIL_INTERPOSABLE)
1416 funct_state y_l = get_function_state (y);
1418 if (can_throw)
1419 break;
1420 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1421 && e->can_throw_external)
1422 can_throw = true;
1424 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1425 can_throw = true;
1427 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1428 if (ie->can_throw_external)
1430 can_throw = true;
1431 break;
1433 w_info = (struct ipa_dfs_info *) w->aux;
1434 w = w_info->next_cycle;
1437 /* Copy back the region's pure_const_state which is shared by
1438 all nodes in the region. */
1439 w = node;
1440 while (w)
1442 funct_state w_l = get_function_state (w);
1443 if (!can_throw && !TREE_NOTHROW (w->decl))
1445 /* Inline clones share declaration with their offline copies;
1446 do not modify their declarations since the offline copy may
1447 be different. */
1448 if (!w->global.inlined_to)
1450 w->set_nothrow_flag (true);
1451 if (dump_file)
1452 fprintf (dump_file, "Function found to be nothrow: %s\n",
1453 w->name ());
1456 else if (can_throw && !TREE_NOTHROW (w->decl))
1457 w_l->can_throw = true;
1458 w_info = (struct ipa_dfs_info *) w->aux;
1459 w = w_info->next_cycle;
1463 ipa_free_postorder_info ();
1464 free (order);
1468 /* Produce the global information by preforming a transitive closure
1469 on the local information that was produced by generate_summary. */
1471 static unsigned int
1472 propagate (void)
1474 struct cgraph_node *node;
1476 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1477 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1478 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1480 /* Nothrow makes more function to not lead to return and improve
1481 later analysis. */
1482 propagate_nothrow ();
1483 propagate_pure_const ();
1485 /* Cleanup. */
1486 FOR_EACH_FUNCTION (node)
1487 if (has_function_state (node))
1488 free (get_function_state (node));
1489 funct_state_vec.release ();
1490 return 0;
1493 static bool
1494 gate_pure_const (void)
1496 return (flag_ipa_pure_const
1497 /* Don't bother doing anything if the program has errors. */
1498 && !seen_error ());
1501 namespace {
1503 const pass_data pass_data_ipa_pure_const =
1505 IPA_PASS, /* type */
1506 "pure-const", /* name */
1507 OPTGROUP_NONE, /* optinfo_flags */
1508 TV_IPA_PURE_CONST, /* tv_id */
1509 0, /* properties_required */
1510 0, /* properties_provided */
1511 0, /* properties_destroyed */
1512 0, /* todo_flags_start */
1513 0, /* todo_flags_finish */
1516 class pass_ipa_pure_const : public ipa_opt_pass_d
1518 public:
1519 pass_ipa_pure_const (gcc::context *ctxt)
1520 : ipa_opt_pass_d (pass_data_ipa_pure_const, ctxt,
1521 pure_const_generate_summary, /* generate_summary */
1522 pure_const_write_summary, /* write_summary */
1523 pure_const_read_summary, /* read_summary */
1524 NULL, /* write_optimization_summary */
1525 NULL, /* read_optimization_summary */
1526 NULL, /* stmt_fixup */
1527 0, /* function_transform_todo_flags_start */
1528 NULL, /* function_transform */
1529 NULL) /* variable_transform */
1532 /* opt_pass methods: */
1533 virtual bool gate (function *) { return gate_pure_const (); }
1534 virtual unsigned int execute (function *) { return propagate (); }
1536 }; // class pass_ipa_pure_const
1538 } // anon namespace
1540 ipa_opt_pass_d *
1541 make_pass_ipa_pure_const (gcc::context *ctxt)
1543 return new pass_ipa_pure_const (ctxt);
1546 /* Return true if function should be skipped for local pure const analysis. */
1548 static bool
1549 skip_function_for_local_pure_const (struct cgraph_node *node)
1551 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1552 we must not promote functions that are called by already processed functions. */
1554 if (function_called_by_processed_nodes_p ())
1556 if (dump_file)
1557 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1558 return true;
1560 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1562 if (dump_file)
1563 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1564 return true;
1566 return false;
1569 /* Simple local pass for pure const discovery reusing the analysis from
1570 ipa_pure_const. This pass is effective when executed together with
1571 other optimization passes in early optimization pass queue. */
1573 namespace {
1575 const pass_data pass_data_local_pure_const =
1577 GIMPLE_PASS, /* type */
1578 "local-pure-const", /* name */
1579 OPTGROUP_NONE, /* optinfo_flags */
1580 TV_IPA_PURE_CONST, /* tv_id */
1581 0, /* properties_required */
1582 0, /* properties_provided */
1583 0, /* properties_destroyed */
1584 0, /* todo_flags_start */
1585 0, /* todo_flags_finish */
1588 class pass_local_pure_const : public gimple_opt_pass
1590 public:
1591 pass_local_pure_const (gcc::context *ctxt)
1592 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1595 /* opt_pass methods: */
1596 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1597 virtual bool gate (function *) { return gate_pure_const (); }
1598 virtual unsigned int execute (function *);
1600 }; // class pass_local_pure_const
1602 unsigned int
1603 pass_local_pure_const::execute (function *fun)
1605 bool changed = false;
1606 funct_state l;
1607 bool skip;
1608 struct cgraph_node *node;
1610 node = cgraph_node::get (current_function_decl);
1611 skip = skip_function_for_local_pure_const (node);
1612 if (!warn_suggest_attribute_const
1613 && !warn_suggest_attribute_pure
1614 && skip)
1615 return 0;
1617 l = analyze_function (node, false);
1619 /* Do NORETURN discovery. */
1620 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1621 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1623 warn_function_noreturn (fun->decl);
1624 if (dump_file)
1625 fprintf (dump_file, "Function found to be noreturn: %s\n",
1626 current_function_name ());
1628 /* Update declaration and reduce profile to executed once. */
1629 TREE_THIS_VOLATILE (current_function_decl) = 1;
1630 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1631 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1633 changed = true;
1636 switch (l->pure_const_state)
1638 case IPA_CONST:
1639 if (!TREE_READONLY (current_function_decl))
1641 warn_function_const (current_function_decl, !l->looping);
1642 if (!skip)
1644 node->set_const_flag (true, l->looping);
1645 changed = true;
1647 if (dump_file)
1648 fprintf (dump_file, "Function found to be %sconst: %s\n",
1649 l->looping ? "looping " : "",
1650 current_function_name ());
1652 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1653 && !l->looping)
1655 if (!skip)
1657 node->set_const_flag (true, false);
1658 changed = true;
1660 if (dump_file)
1661 fprintf (dump_file, "Function found to be non-looping: %s\n",
1662 current_function_name ());
1664 break;
1666 case IPA_PURE:
1667 if (!DECL_PURE_P (current_function_decl))
1669 if (!skip)
1671 node->set_pure_flag (true, l->looping);
1672 changed = true;
1674 warn_function_pure (current_function_decl, !l->looping);
1675 if (dump_file)
1676 fprintf (dump_file, "Function found to be %spure: %s\n",
1677 l->looping ? "looping " : "",
1678 current_function_name ());
1680 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1681 && !l->looping)
1683 if (!skip)
1685 node->set_pure_flag (true, false);
1686 changed = true;
1688 if (dump_file)
1689 fprintf (dump_file, "Function found to be non-looping: %s\n",
1690 current_function_name ());
1692 break;
1694 default:
1695 break;
1697 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1699 node->set_nothrow_flag (true);
1700 changed = true;
1701 if (dump_file)
1702 fprintf (dump_file, "Function found to be nothrow: %s\n",
1703 current_function_name ());
1705 free (l);
1706 if (changed)
1707 return execute_fixup_cfg ();
1708 else
1709 return 0;
1712 } // anon namespace
1714 gimple_opt_pass *
1715 make_pass_local_pure_const (gcc::context *ctxt)
1717 return new pass_local_pure_const (ctxt);
1720 /* Emit noreturn warnings. */
1722 namespace {
1724 const pass_data pass_data_warn_function_noreturn =
1726 GIMPLE_PASS, /* type */
1727 "*warn_function_noreturn", /* name */
1728 OPTGROUP_NONE, /* optinfo_flags */
1729 TV_NONE, /* tv_id */
1730 PROP_cfg, /* properties_required */
1731 0, /* properties_provided */
1732 0, /* properties_destroyed */
1733 0, /* todo_flags_start */
1734 0, /* todo_flags_finish */
1737 class pass_warn_function_noreturn : public gimple_opt_pass
1739 public:
1740 pass_warn_function_noreturn (gcc::context *ctxt)
1741 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1744 /* opt_pass methods: */
1745 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1746 virtual unsigned int execute (function *fun)
1748 if (!TREE_THIS_VOLATILE (current_function_decl)
1749 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1750 warn_function_noreturn (current_function_decl);
1751 return 0;
1754 }; // class pass_warn_function_noreturn
1756 } // anon namespace
1758 gimple_opt_pass *
1759 make_pass_warn_function_noreturn (gcc::context *ctxt)
1761 return new pass_warn_function_noreturn (ctxt);