2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-pure-const.c
blob2150186dfc5290f8db161ae2199409aa1918d5e3
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2015 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 "input.h"
39 #include "alias.h"
40 #include "symtab.h"
41 #include "tree.h"
42 #include "fold-const.h"
43 #include "print-tree.h"
44 #include "calls.h"
45 #include "predict.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "dominance.h"
50 #include "cfg.h"
51 #include "cfganal.h"
52 #include "basic-block.h"
53 #include "tree-ssa-alias.h"
54 #include "internal-fn.h"
55 #include "tree-eh.h"
56 #include "gimple-expr.h"
57 #include "is-a.h"
58 #include "gimple.h"
59 #include "gimple-iterator.h"
60 #include "gimple-walk.h"
61 #include "tree-cfg.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-inline.h"
64 #include "tree-pass.h"
65 #include "langhooks.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "ipa-utils.h"
70 #include "flags.h"
71 #include "diagnostic.h"
72 #include "gimple-pretty-print.h"
73 #include "langhooks.h"
74 #include "target.h"
75 #include "lto-streamer.h"
76 #include "data-streamer.h"
77 #include "tree-streamer.h"
78 #include "cfgloop.h"
79 #include "tree-scalar-evolution.h"
80 #include "intl.h"
81 #include "opts.h"
82 #include "varasm.h"
84 /* Lattice values for const and pure functions. Everything starts out
85 being const, then may drop to pure and then neither depending on
86 what is found. */
87 enum pure_const_state_e
89 IPA_CONST,
90 IPA_PURE,
91 IPA_NEITHER
94 const char *pure_const_names[3] = {"const", "pure", "neither"};
96 /* Holder for the const_state. There is one of these per function
97 decl. */
98 struct funct_state_d
100 /* See above. */
101 enum pure_const_state_e pure_const_state;
102 /* What user set here; we can be always sure about this. */
103 enum pure_const_state_e state_previously_known;
104 bool looping_previously_known;
106 /* True if the function could possibly infinite loop. There are a
107 lot of ways that this could be determined. We are pretty
108 conservative here. While it is possible to cse pure and const
109 calls, it is not legal to have dce get rid of the call if there
110 is a possibility that the call could infinite loop since this is
111 a behavioral change. */
112 bool looping;
114 bool can_throw;
116 /* If function can call free, munmap or otherwise make previously
117 non-trapping memory accesses trapping. */
118 bool can_free;
121 /* State used when we know nothing about function. */
122 static struct funct_state_d varying_state
123 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
126 typedef struct funct_state_d * funct_state;
128 /* The storage of the funct_state is abstracted because there is the
129 possibility that it may be desirable to move this to the cgraph
130 local info. */
132 /* Array, indexed by cgraph node uid, of function states. */
134 static vec<funct_state> funct_state_vec;
136 static bool gate_pure_const (void);
138 namespace {
140 const pass_data pass_data_ipa_pure_const =
142 IPA_PASS, /* type */
143 "pure-const", /* name */
144 OPTGROUP_NONE, /* optinfo_flags */
145 TV_IPA_PURE_CONST, /* tv_id */
146 0, /* properties_required */
147 0, /* properties_provided */
148 0, /* properties_destroyed */
149 0, /* todo_flags_start */
150 0, /* todo_flags_finish */
153 class pass_ipa_pure_const : public ipa_opt_pass_d
155 public:
156 pass_ipa_pure_const(gcc::context *ctxt);
158 /* opt_pass methods: */
159 bool gate (function *) { return gate_pure_const (); }
160 unsigned int execute (function *fun);
162 void register_hooks (void);
164 private:
165 bool init_p;
167 /* Holders of ipa cgraph hooks: */
168 struct cgraph_node_hook_list *function_insertion_hook_holder;
169 struct cgraph_2node_hook_list *node_duplication_hook_holder;
170 struct cgraph_node_hook_list *node_removal_hook_holder;
172 }; // class pass_ipa_pure_const
174 } // anon namespace
176 /* Try to guess if function body will always be visible to compiler
177 when compiling the call and whether compiler will be able
178 to propagate the information by itself. */
180 static bool
181 function_always_visible_to_compiler_p (tree decl)
183 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
186 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
187 is true if the function is known to be finite. The diagnostic is
188 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
189 OPTION, this function may initialize it and it is always returned
190 by the function. */
192 static hash_set<tree> *
193 suggest_attribute (int option, tree decl, bool known_finite,
194 hash_set<tree> *warned_about,
195 const char * attrib_name)
197 if (!option_enabled (option, &global_options))
198 return warned_about;
199 if (TREE_THIS_VOLATILE (decl)
200 || (known_finite && function_always_visible_to_compiler_p (decl)))
201 return warned_about;
203 if (!warned_about)
204 warned_about = new hash_set<tree>;
205 if (warned_about->contains (decl))
206 return warned_about;
207 warned_about->add (decl);
208 warning_at (DECL_SOURCE_LOCATION (decl),
209 option,
210 known_finite
211 ? _("function might be candidate for attribute %<%s%>")
212 : _("function might be candidate for attribute %<%s%>"
213 " if it is known to return normally"), attrib_name);
214 return warned_about;
217 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
218 is true if the function is known to be finite. */
220 static void
221 warn_function_pure (tree decl, bool known_finite)
223 static hash_set<tree> *warned_about;
225 warned_about
226 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
227 known_finite, warned_about, "pure");
230 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
231 is true if the function is known to be finite. */
233 static void
234 warn_function_const (tree decl, bool known_finite)
236 static hash_set<tree> *warned_about;
237 warned_about
238 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
239 known_finite, warned_about, "const");
242 static void
243 warn_function_noreturn (tree decl)
245 static hash_set<tree> *warned_about;
246 if (!lang_hooks.missing_noreturn_ok_p (decl)
247 && targetm.warn_func_return (decl))
248 warned_about
249 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
250 true, warned_about, "noreturn");
253 /* Return true if we have a function state for NODE. */
255 static inline bool
256 has_function_state (struct cgraph_node *node)
258 if (!funct_state_vec.exists ()
259 || funct_state_vec.length () <= (unsigned int)node->uid)
260 return false;
261 return funct_state_vec[node->uid] != NULL;
264 /* Return the function state from NODE. */
266 static inline funct_state
267 get_function_state (struct cgraph_node *node)
269 if (!funct_state_vec.exists ()
270 || funct_state_vec.length () <= (unsigned int)node->uid
271 || !funct_state_vec[node->uid])
272 /* We might want to put correct previously_known state into varying. */
273 return &varying_state;
274 return funct_state_vec[node->uid];
277 /* Set the function state S for NODE. */
279 static inline void
280 set_function_state (struct cgraph_node *node, funct_state s)
282 if (!funct_state_vec.exists ()
283 || funct_state_vec.length () <= (unsigned int)node->uid)
284 funct_state_vec.safe_grow_cleared (node->uid + 1);
285 funct_state_vec[node->uid] = s;
288 /* Check to see if the use (or definition when CHECKING_WRITE is true)
289 variable T is legal in a function that is either pure or const. */
291 static inline void
292 check_decl (funct_state local,
293 tree t, bool checking_write, bool ipa)
295 /* Do not want to do anything with volatile except mark any
296 function that uses one to be not const or pure. */
297 if (TREE_THIS_VOLATILE (t))
299 local->pure_const_state = IPA_NEITHER;
300 if (dump_file)
301 fprintf (dump_file, " Volatile operand is not const/pure");
302 return;
305 /* Do not care about a local automatic that is not static. */
306 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
307 return;
309 /* If the variable has the "used" attribute, treat it as if it had a
310 been touched by the devil. */
311 if (DECL_PRESERVE_P (t))
313 local->pure_const_state = IPA_NEITHER;
314 if (dump_file)
315 fprintf (dump_file, " Used static/global variable is not const/pure\n");
316 return;
319 /* In IPA mode we are not interested in checking actual loads and stores;
320 they will be processed at propagation time using ipa_ref. */
321 if (ipa)
322 return;
324 /* Since we have dealt with the locals and params cases above, if we
325 are CHECKING_WRITE, this cannot be a pure or constant
326 function. */
327 if (checking_write)
329 local->pure_const_state = IPA_NEITHER;
330 if (dump_file)
331 fprintf (dump_file, " static/global memory write is not const/pure\n");
332 return;
335 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
337 /* Readonly reads are safe. */
338 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
339 return; /* Read of a constant, do not change the function state. */
340 else
342 if (dump_file)
343 fprintf (dump_file, " global memory read is not const\n");
344 /* Just a regular read. */
345 if (local->pure_const_state == IPA_CONST)
346 local->pure_const_state = IPA_PURE;
349 else
351 /* Compilation level statics can be read if they are readonly
352 variables. */
353 if (TREE_READONLY (t))
354 return;
356 if (dump_file)
357 fprintf (dump_file, " static memory read is not const\n");
358 /* Just a regular read. */
359 if (local->pure_const_state == IPA_CONST)
360 local->pure_const_state = IPA_PURE;
365 /* Check to see if the use (or definition when CHECKING_WRITE is true)
366 variable T is legal in a function that is either pure or const. */
368 static inline void
369 check_op (funct_state local, tree t, bool checking_write)
371 t = get_base_address (t);
372 if (t && TREE_THIS_VOLATILE (t))
374 local->pure_const_state = IPA_NEITHER;
375 if (dump_file)
376 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
377 return;
379 else if (t
380 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
381 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
382 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
384 if (dump_file)
385 fprintf (dump_file, " Indirect ref to local memory is OK\n");
386 return;
388 else if (checking_write)
390 local->pure_const_state = IPA_NEITHER;
391 if (dump_file)
392 fprintf (dump_file, " Indirect ref write is not const/pure\n");
393 return;
395 else
397 if (dump_file)
398 fprintf (dump_file, " Indirect ref read is not const\n");
399 if (local->pure_const_state == IPA_CONST)
400 local->pure_const_state = IPA_PURE;
404 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
406 static void
407 state_from_flags (enum pure_const_state_e *state, bool *looping,
408 int flags, bool cannot_lead_to_return)
410 *looping = false;
411 if (flags & ECF_LOOPING_CONST_OR_PURE)
413 *looping = true;
414 if (dump_file && (dump_flags & TDF_DETAILS))
415 fprintf (dump_file, " looping");
417 if (flags & ECF_CONST)
419 *state = IPA_CONST;
420 if (dump_file && (dump_flags & TDF_DETAILS))
421 fprintf (dump_file, " const\n");
423 else if (flags & ECF_PURE)
425 *state = IPA_PURE;
426 if (dump_file && (dump_flags & TDF_DETAILS))
427 fprintf (dump_file, " pure\n");
429 else if (cannot_lead_to_return)
431 *state = IPA_PURE;
432 *looping = true;
433 if (dump_file && (dump_flags & TDF_DETAILS))
434 fprintf (dump_file, " ignoring side effects->pure looping\n");
436 else
438 if (dump_file && (dump_flags & TDF_DETAILS))
439 fprintf (dump_file, " neither\n");
440 *state = IPA_NEITHER;
441 *looping = true;
445 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
446 into STATE and LOOPING better of the two variants.
447 Be sure to merge looping correctly. IPA_NEITHER functions
448 have looping 0 even if they don't have to return. */
450 static inline void
451 better_state (enum pure_const_state_e *state, bool *looping,
452 enum pure_const_state_e state2, bool looping2)
454 if (state2 < *state)
456 if (*state == IPA_NEITHER)
457 *looping = looping2;
458 else
459 *looping = MIN (*looping, looping2);
460 *state = state2;
462 else if (state2 != IPA_NEITHER)
463 *looping = MIN (*looping, looping2);
466 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
467 into STATE and LOOPING worse of the two variants. */
469 static inline void
470 worse_state (enum pure_const_state_e *state, bool *looping,
471 enum pure_const_state_e state2, bool looping2)
473 *state = MAX (*state, state2);
474 *looping = MAX (*looping, looping2);
477 /* Recognize special cases of builtins that are by themselves not pure or const
478 but function using them is. */
479 static bool
480 special_builtin_state (enum pure_const_state_e *state, bool *looping,
481 tree callee)
483 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
484 switch (DECL_FUNCTION_CODE (callee))
486 case BUILT_IN_RETURN:
487 case BUILT_IN_UNREACHABLE:
488 case BUILT_IN_ALLOCA:
489 case BUILT_IN_ALLOCA_WITH_ALIGN:
490 case BUILT_IN_STACK_SAVE:
491 case BUILT_IN_STACK_RESTORE:
492 case BUILT_IN_EH_POINTER:
493 case BUILT_IN_EH_FILTER:
494 case BUILT_IN_UNWIND_RESUME:
495 case BUILT_IN_CXA_END_CLEANUP:
496 case BUILT_IN_EH_COPY_VALUES:
497 case BUILT_IN_FRAME_ADDRESS:
498 case BUILT_IN_APPLY:
499 case BUILT_IN_APPLY_ARGS:
500 *looping = false;
501 *state = IPA_CONST;
502 return true;
503 case BUILT_IN_PREFETCH:
504 *looping = true;
505 *state = IPA_CONST;
506 return true;
507 default:
508 break;
510 return false;
513 /* Check the parameters of a function call to CALL_EXPR to see if
514 there are any references in the parameters that are not allowed for
515 pure or const functions. Also check to see if this is either an
516 indirect call, a call outside the compilation unit, or has special
517 attributes that may also effect the purity. The CALL_EXPR node for
518 the entire call expression. */
520 static void
521 check_call (funct_state local, gcall *call, bool ipa)
523 int flags = gimple_call_flags (call);
524 tree callee_t = gimple_call_fndecl (call);
525 bool possibly_throws = stmt_could_throw_p (call);
526 bool possibly_throws_externally = (possibly_throws
527 && stmt_can_throw_external (call));
529 if (possibly_throws)
531 unsigned int i;
532 for (i = 0; i < gimple_num_ops (call); i++)
533 if (gimple_op (call, i)
534 && tree_could_throw_p (gimple_op (call, i)))
536 if (possibly_throws && cfun->can_throw_non_call_exceptions)
538 if (dump_file)
539 fprintf (dump_file, " operand can throw; looping\n");
540 local->looping = true;
542 if (possibly_throws_externally)
544 if (dump_file)
545 fprintf (dump_file, " operand can throw externally\n");
546 local->can_throw = true;
551 /* The const and pure flags are set by a variety of places in the
552 compiler (including here). If someone has already set the flags
553 for the callee, (such as for some of the builtins) we will use
554 them, otherwise we will compute our own information.
556 Const and pure functions have less clobber effects than other
557 functions so we process these first. Otherwise if it is a call
558 outside the compilation unit or an indirect call we punt. This
559 leaves local calls which will be processed by following the call
560 graph. */
561 if (callee_t)
563 enum pure_const_state_e call_state;
564 bool call_looping;
566 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
567 && !nonfreeing_call_p (call))
568 local->can_free = true;
570 if (special_builtin_state (&call_state, &call_looping, callee_t))
572 worse_state (&local->pure_const_state, &local->looping,
573 call_state, call_looping);
574 return;
576 /* When bad things happen to bad functions, they cannot be const
577 or pure. */
578 if (setjmp_call_p (callee_t))
580 if (dump_file)
581 fprintf (dump_file, " setjmp is not const/pure\n");
582 local->looping = true;
583 local->pure_const_state = IPA_NEITHER;
586 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
587 switch (DECL_FUNCTION_CODE (callee_t))
589 case BUILT_IN_LONGJMP:
590 case BUILT_IN_NONLOCAL_GOTO:
591 if (dump_file)
592 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
593 local->pure_const_state = IPA_NEITHER;
594 local->looping = true;
595 break;
596 default:
597 break;
600 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
601 local->can_free = true;
603 /* When not in IPA mode, we can still handle self recursion. */
604 if (!ipa && callee_t
605 && recursive_call_p (current_function_decl, callee_t))
607 if (dump_file)
608 fprintf (dump_file, " Recursive call can loop.\n");
609 local->looping = true;
611 /* Either callee is unknown or we are doing local analysis.
612 Look to see if there are any bits available for the callee (such as by
613 declaration or because it is builtin) and process solely on the basis of
614 those bits. */
615 else if (!ipa)
617 enum pure_const_state_e call_state;
618 bool call_looping;
619 if (possibly_throws && cfun->can_throw_non_call_exceptions)
621 if (dump_file)
622 fprintf (dump_file, " can throw; looping\n");
623 local->looping = true;
625 if (possibly_throws_externally)
627 if (dump_file)
629 fprintf (dump_file, " can throw externally to lp %i\n",
630 lookup_stmt_eh_lp (call));
631 if (callee_t)
632 fprintf (dump_file, " callee:%s\n",
633 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
635 local->can_throw = true;
637 if (dump_file && (dump_flags & TDF_DETAILS))
638 fprintf (dump_file, " checking flags for call:");
639 state_from_flags (&call_state, &call_looping, flags,
640 ((flags & (ECF_NORETURN | ECF_NOTHROW))
641 == (ECF_NORETURN | ECF_NOTHROW))
642 || (!flag_exceptions && (flags & ECF_NORETURN)));
643 worse_state (&local->pure_const_state, &local->looping,
644 call_state, call_looping);
646 /* Direct functions calls are handled by IPA propagation. */
649 /* Wrapper around check_decl for loads in local more. */
651 static bool
652 check_load (gimple, tree op, tree, void *data)
654 if (DECL_P (op))
655 check_decl ((funct_state)data, op, false, false);
656 else
657 check_op ((funct_state)data, op, false);
658 return false;
661 /* Wrapper around check_decl for stores in local more. */
663 static bool
664 check_store (gimple, tree op, tree, void *data)
666 if (DECL_P (op))
667 check_decl ((funct_state)data, op, true, false);
668 else
669 check_op ((funct_state)data, op, true);
670 return false;
673 /* Wrapper around check_decl for loads in ipa mode. */
675 static bool
676 check_ipa_load (gimple, tree op, tree, void *data)
678 if (DECL_P (op))
679 check_decl ((funct_state)data, op, false, true);
680 else
681 check_op ((funct_state)data, op, false);
682 return false;
685 /* Wrapper around check_decl for stores in ipa mode. */
687 static bool
688 check_ipa_store (gimple, tree op, tree, void *data)
690 if (DECL_P (op))
691 check_decl ((funct_state)data, op, true, true);
692 else
693 check_op ((funct_state)data, op, true);
694 return false;
697 /* Look into pointer pointed to by GSIP and figure out what interesting side
698 effects it has. */
699 static void
700 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
702 gimple stmt = gsi_stmt (*gsip);
704 if (is_gimple_debug (stmt))
705 return;
707 /* Do consider clobber as side effects before IPA, so we rather inline
708 C++ destructors and keep clobber semantics than eliminate them.
710 TODO: We may get smarter during early optimizations on these and let
711 functions containing only clobbers to be optimized more. This is a common
712 case of C++ destructors. */
714 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
715 return;
717 if (dump_file)
719 fprintf (dump_file, " scanning: ");
720 print_gimple_stmt (dump_file, stmt, 0, 0);
723 if (gimple_has_volatile_ops (stmt)
724 && !gimple_clobber_p (stmt))
726 local->pure_const_state = IPA_NEITHER;
727 if (dump_file)
728 fprintf (dump_file, " Volatile stmt is not const/pure\n");
731 /* Look for loads and stores. */
732 walk_stmt_load_store_ops (stmt, local,
733 ipa ? check_ipa_load : check_load,
734 ipa ? check_ipa_store : check_store);
736 if (gimple_code (stmt) != GIMPLE_CALL
737 && stmt_could_throw_p (stmt))
739 if (cfun->can_throw_non_call_exceptions)
741 if (dump_file)
742 fprintf (dump_file, " can throw; looping\n");
743 local->looping = true;
745 if (stmt_can_throw_external (stmt))
747 if (dump_file)
748 fprintf (dump_file, " can throw externally\n");
749 local->can_throw = true;
751 else
752 if (dump_file)
753 fprintf (dump_file, " can throw\n");
755 switch (gimple_code (stmt))
757 case GIMPLE_CALL:
758 check_call (local, as_a <gcall *> (stmt), ipa);
759 break;
760 case GIMPLE_LABEL:
761 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
762 /* Target of long jump. */
764 if (dump_file)
765 fprintf (dump_file, " nonlocal label is not const/pure\n");
766 local->pure_const_state = IPA_NEITHER;
768 break;
769 case GIMPLE_ASM:
770 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
772 if (dump_file)
773 fprintf (dump_file, " memory asm clobber is not const/pure\n");
774 /* Abandon all hope, ye who enter here. */
775 local->pure_const_state = IPA_NEITHER;
776 local->can_free = true;
778 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
780 if (dump_file)
781 fprintf (dump_file, " volatile is not const/pure\n");
782 /* Abandon all hope, ye who enter here. */
783 local->pure_const_state = IPA_NEITHER;
784 local->looping = true;
785 local->can_free = true;
787 return;
788 default:
789 break;
794 /* This is the main routine for finding the reference patterns for
795 global variables within a function FN. */
797 static funct_state
798 analyze_function (struct cgraph_node *fn, bool ipa)
800 tree decl = fn->decl;
801 funct_state l;
802 basic_block this_block;
804 l = XCNEW (struct funct_state_d);
805 l->pure_const_state = IPA_CONST;
806 l->state_previously_known = IPA_NEITHER;
807 l->looping_previously_known = true;
808 l->looping = false;
809 l->can_throw = false;
810 l->can_free = false;
811 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
812 flags_from_decl_or_type (fn->decl),
813 fn->cannot_return_p ());
815 if (fn->thunk.thunk_p || fn->alias)
817 /* Thunk gets propagated through, so nothing interesting happens. */
818 gcc_assert (ipa);
819 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
820 l->pure_const_state = IPA_NEITHER;
821 return l;
824 if (dump_file)
826 fprintf (dump_file, "\n\n local analysis of %s\n ",
827 fn->name ());
830 push_cfun (DECL_STRUCT_FUNCTION (decl));
832 FOR_EACH_BB_FN (this_block, cfun)
834 gimple_stmt_iterator gsi;
835 struct walk_stmt_info wi;
837 memset (&wi, 0, sizeof (wi));
838 for (gsi = gsi_start_bb (this_block);
839 !gsi_end_p (gsi);
840 gsi_next (&gsi))
842 check_stmt (&gsi, l, ipa);
843 if (l->pure_const_state == IPA_NEITHER
844 && l->looping
845 && l->can_throw
846 && l->can_free)
847 goto end;
851 end:
852 if (l->pure_const_state != IPA_NEITHER)
854 /* Const functions cannot have back edges (an
855 indication of possible infinite loop side
856 effect. */
857 if (mark_dfs_back_edges ())
859 /* Preheaders are needed for SCEV to work.
860 Simple latches and recorded exits improve chances that loop will
861 proved to be finite in testcases such as in loop-15.c
862 and loop-24.c */
863 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
864 | LOOPS_HAVE_SIMPLE_LATCHES
865 | LOOPS_HAVE_RECORDED_EXITS);
866 if (dump_file && (dump_flags & TDF_DETAILS))
867 flow_loops_dump (dump_file, NULL, 0);
868 if (mark_irreducible_loops ())
870 if (dump_file)
871 fprintf (dump_file, " has irreducible loops\n");
872 l->looping = true;
874 else
876 struct loop *loop;
877 scev_initialize ();
878 FOR_EACH_LOOP (loop, 0)
879 if (!finite_loop_p (loop))
881 if (dump_file)
882 fprintf (dump_file, " can not prove finiteness of "
883 "loop %i\n", loop->num);
884 l->looping =true;
885 break;
887 scev_finalize ();
889 loop_optimizer_finalize ();
893 if (dump_file && (dump_flags & TDF_DETAILS))
894 fprintf (dump_file, " checking previously known:");
896 better_state (&l->pure_const_state, &l->looping,
897 l->state_previously_known,
898 l->looping_previously_known);
899 if (TREE_NOTHROW (decl))
900 l->can_throw = false;
902 pop_cfun ();
903 if (dump_file)
905 if (l->looping)
906 fprintf (dump_file, "Function is locally looping.\n");
907 if (l->can_throw)
908 fprintf (dump_file, "Function is locally throwing.\n");
909 if (l->pure_const_state == IPA_CONST)
910 fprintf (dump_file, "Function is locally const.\n");
911 if (l->pure_const_state == IPA_PURE)
912 fprintf (dump_file, "Function is locally pure.\n");
913 if (l->can_free)
914 fprintf (dump_file, "Function can locally free.\n");
916 return l;
919 /* Called when new function is inserted to callgraph late. */
920 static void
921 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
923 if (node->get_availability () < AVAIL_INTERPOSABLE)
924 return;
925 /* There are some shared nodes, in particular the initializers on
926 static declarations. We do not need to scan them more than once
927 since all we would be interested in are the addressof
928 operations. */
929 if (node->get_availability () > AVAIL_INTERPOSABLE
930 && opt_for_fn (node->decl, flag_ipa_pure_const))
931 set_function_state (node, analyze_function (node, true));
934 /* Called when new clone is inserted to callgraph late. */
936 static void
937 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
938 void *data ATTRIBUTE_UNUSED)
940 if (has_function_state (src))
942 funct_state l = XNEW (struct funct_state_d);
943 gcc_assert (!has_function_state (dst));
944 memcpy (l, get_function_state (src), sizeof (*l));
945 set_function_state (dst, l);
949 /* Called when new clone is inserted to callgraph late. */
951 static void
952 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
954 if (has_function_state (node))
956 funct_state l = get_function_state (node);
957 if (l != &varying_state)
958 free (l);
959 set_function_state (node, NULL);
964 void
965 pass_ipa_pure_const::
966 register_hooks (void)
968 if (init_p)
969 return;
971 init_p = true;
973 node_removal_hook_holder =
974 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
975 node_duplication_hook_holder =
976 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
977 function_insertion_hook_holder =
978 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
982 /* Analyze each function in the cgraph to see if it is locally PURE or
983 CONST. */
985 static void
986 pure_const_generate_summary (void)
988 struct cgraph_node *node;
990 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
991 pass->register_hooks ();
993 /* Process all of the functions.
995 We process AVAIL_INTERPOSABLE functions. We can not use the results
996 by default, but the info can be used at LTO with -fwhole-program or
997 when function got cloned and the clone is AVAILABLE. */
999 FOR_EACH_DEFINED_FUNCTION (node)
1000 if (node->get_availability () >= AVAIL_INTERPOSABLE
1001 && opt_for_fn (node->decl, flag_ipa_pure_const))
1002 set_function_state (node, analyze_function (node, true));
1006 /* Serialize the ipa info for lto. */
1008 static void
1009 pure_const_write_summary (void)
1011 struct cgraph_node *node;
1012 struct lto_simple_output_block *ob
1013 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1014 unsigned int count = 0;
1015 lto_symtab_encoder_iterator lsei;
1016 lto_symtab_encoder_t encoder;
1018 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1020 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1021 lsei_next_function_in_partition (&lsei))
1023 node = lsei_cgraph_node (lsei);
1024 if (node->definition && has_function_state (node))
1025 count++;
1028 streamer_write_uhwi_stream (ob->main_stream, count);
1030 /* Process all of the functions. */
1031 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1032 lsei_next_function_in_partition (&lsei))
1034 node = lsei_cgraph_node (lsei);
1035 if (node->definition && has_function_state (node))
1037 struct bitpack_d bp;
1038 funct_state fs;
1039 int node_ref;
1040 lto_symtab_encoder_t encoder;
1042 fs = get_function_state (node);
1044 encoder = ob->decl_state->symtab_node_encoder;
1045 node_ref = lto_symtab_encoder_encode (encoder, node);
1046 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1048 /* Note that flags will need to be read in the opposite
1049 order as we are pushing the bitflags into FLAGS. */
1050 bp = bitpack_create (ob->main_stream);
1051 bp_pack_value (&bp, fs->pure_const_state, 2);
1052 bp_pack_value (&bp, fs->state_previously_known, 2);
1053 bp_pack_value (&bp, fs->looping_previously_known, 1);
1054 bp_pack_value (&bp, fs->looping, 1);
1055 bp_pack_value (&bp, fs->can_throw, 1);
1056 bp_pack_value (&bp, fs->can_free, 1);
1057 streamer_write_bitpack (&bp);
1061 lto_destroy_simple_output_block (ob);
1065 /* Deserialize the ipa info for lto. */
1067 static void
1068 pure_const_read_summary (void)
1070 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1071 struct lto_file_decl_data *file_data;
1072 unsigned int j = 0;
1074 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1075 pass->register_hooks ();
1077 while ((file_data = file_data_vec[j++]))
1079 const char *data;
1080 size_t len;
1081 struct lto_input_block *ib
1082 = lto_create_simple_input_block (file_data,
1083 LTO_section_ipa_pure_const,
1084 &data, &len);
1085 if (ib)
1087 unsigned int i;
1088 unsigned int count = streamer_read_uhwi (ib);
1090 for (i = 0; i < count; i++)
1092 unsigned int index;
1093 struct cgraph_node *node;
1094 struct bitpack_d bp;
1095 funct_state fs;
1096 lto_symtab_encoder_t encoder;
1098 fs = XCNEW (struct funct_state_d);
1099 index = streamer_read_uhwi (ib);
1100 encoder = file_data->symtab_node_encoder;
1101 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1102 index));
1103 set_function_state (node, fs);
1105 /* Note that the flags must be read in the opposite
1106 order in which they were written (the bitflags were
1107 pushed into FLAGS). */
1108 bp = streamer_read_bitpack (ib);
1109 fs->pure_const_state
1110 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1111 fs->state_previously_known
1112 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1113 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1114 fs->looping = bp_unpack_value (&bp, 1);
1115 fs->can_throw = bp_unpack_value (&bp, 1);
1116 fs->can_free = bp_unpack_value (&bp, 1);
1117 if (dump_file)
1119 int flags = flags_from_decl_or_type (node->decl);
1120 fprintf (dump_file, "Read info for %s/%i ",
1121 node->name (),
1122 node->order);
1123 if (flags & ECF_CONST)
1124 fprintf (dump_file, " const");
1125 if (flags & ECF_PURE)
1126 fprintf (dump_file, " pure");
1127 if (flags & ECF_NOTHROW)
1128 fprintf (dump_file, " nothrow");
1129 fprintf (dump_file, "\n pure const state: %s\n",
1130 pure_const_names[fs->pure_const_state]);
1131 fprintf (dump_file, " previously known state: %s\n",
1132 pure_const_names[fs->looping_previously_known]);
1133 if (fs->looping)
1134 fprintf (dump_file," function is locally looping\n");
1135 if (fs->looping_previously_known)
1136 fprintf (dump_file," function is previously known looping\n");
1137 if (fs->can_throw)
1138 fprintf (dump_file," function is locally throwing\n");
1139 if (fs->can_free)
1140 fprintf (dump_file," function can locally free\n");
1144 lto_destroy_simple_input_block (file_data,
1145 LTO_section_ipa_pure_const,
1146 ib, data, len);
1152 static bool
1153 ignore_edge (struct cgraph_edge *e)
1155 return (!e->can_throw_external);
1158 /* Return true if NODE is self recursive function.
1159 Indirectly recursive functions appears as non-trivial strongly
1160 connected components, so we need to care about self recursion
1161 only. */
1163 static bool
1164 self_recursive_p (struct cgraph_node *node)
1166 struct cgraph_edge *e;
1167 for (e = node->callees; e; e = e->next_callee)
1168 if (e->callee->function_symbol () == node)
1169 return true;
1170 return false;
1173 /* Return true if N is cdtor that is not const or pure. In this case we may
1174 need to remove unreachable function if it is marked const/pure. */
1176 static bool
1177 cdtor_p (cgraph_node *n, void *)
1179 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1180 return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl);
1181 return false;
1184 /* Produce transitive closure over the callgraph and compute pure/const
1185 attributes. */
1187 static bool
1188 propagate_pure_const (void)
1190 struct cgraph_node *node;
1191 struct cgraph_node *w;
1192 struct cgraph_node **order =
1193 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1194 int order_pos;
1195 int i;
1196 struct ipa_dfs_info * w_info;
1197 bool remove_p = false;
1199 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1200 if (dump_file)
1202 cgraph_node::dump_cgraph (dump_file);
1203 ipa_print_order (dump_file, "reduced", order, order_pos);
1206 /* Propagate the local information through the call graph to produce
1207 the global information. All the nodes within a cycle will have
1208 the same info so we collapse cycles first. Then we can do the
1209 propagation in one pass from the leaves to the roots. */
1210 for (i = 0; i < order_pos; i++ )
1212 enum pure_const_state_e pure_const_state = IPA_CONST;
1213 bool looping = false;
1214 int count = 0;
1215 node = order[i];
1217 if (node->alias)
1218 continue;
1220 if (dump_file && (dump_flags & TDF_DETAILS))
1221 fprintf (dump_file, "Starting cycle\n");
1223 /* Find the worst state for any node in the cycle. */
1224 w = node;
1225 while (w && pure_const_state != IPA_NEITHER)
1227 struct cgraph_edge *e;
1228 struct cgraph_edge *ie;
1229 int i;
1230 struct ipa_ref *ref = NULL;
1232 funct_state w_l = get_function_state (w);
1233 if (dump_file && (dump_flags & TDF_DETAILS))
1234 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1235 w->name (),
1236 w->order,
1237 pure_const_names[w_l->pure_const_state],
1238 w_l->looping);
1240 /* First merge in function body properties. */
1241 worse_state (&pure_const_state, &looping,
1242 w_l->pure_const_state, w_l->looping);
1243 if (pure_const_state == IPA_NEITHER)
1244 break;
1246 /* For overwritable nodes we can not assume anything. */
1247 if (w->get_availability () == AVAIL_INTERPOSABLE)
1249 worse_state (&pure_const_state, &looping,
1250 w_l->state_previously_known,
1251 w_l->looping_previously_known);
1252 if (dump_file && (dump_flags & TDF_DETAILS))
1254 fprintf (dump_file,
1255 " Overwritable. state %s looping %i\n",
1256 pure_const_names[w_l->state_previously_known],
1257 w_l->looping_previously_known);
1259 break;
1262 count++;
1264 /* We consider recursive cycles as possibly infinite.
1265 This might be relaxed since infinite recursion leads to stack
1266 overflow. */
1267 if (count > 1)
1268 looping = true;
1270 /* Now walk the edges and merge in callee properties. */
1271 for (e = w->callees; e; e = e->next_callee)
1273 enum availability avail;
1274 struct cgraph_node *y = e->callee->
1275 function_or_virtual_thunk_symbol (&avail);
1276 enum pure_const_state_e edge_state = IPA_CONST;
1277 bool edge_looping = false;
1279 if (dump_file && (dump_flags & TDF_DETAILS))
1281 fprintf (dump_file,
1282 " Call to %s/%i",
1283 e->callee->name (),
1284 e->callee->order);
1286 if (avail > AVAIL_INTERPOSABLE)
1288 funct_state y_l = get_function_state (y);
1289 if (dump_file && (dump_flags & TDF_DETAILS))
1291 fprintf (dump_file,
1292 " state:%s looping:%i\n",
1293 pure_const_names[y_l->pure_const_state],
1294 y_l->looping);
1296 if (y_l->pure_const_state > IPA_PURE
1297 && e->cannot_lead_to_return_p ())
1299 if (dump_file && (dump_flags & TDF_DETAILS))
1300 fprintf (dump_file,
1301 " Ignoring side effects"
1302 " -> pure, looping\n");
1303 edge_state = IPA_PURE;
1304 edge_looping = true;
1306 else
1308 edge_state = y_l->pure_const_state;
1309 edge_looping = y_l->looping;
1312 else if (special_builtin_state (&edge_state, &edge_looping,
1313 y->decl))
1315 else
1316 state_from_flags (&edge_state, &edge_looping,
1317 flags_from_decl_or_type (y->decl),
1318 e->cannot_lead_to_return_p ());
1320 /* Merge the results with what we already know. */
1321 better_state (&edge_state, &edge_looping,
1322 w_l->state_previously_known,
1323 w_l->looping_previously_known);
1324 worse_state (&pure_const_state, &looping,
1325 edge_state, edge_looping);
1326 if (pure_const_state == IPA_NEITHER)
1327 break;
1329 if (pure_const_state == IPA_NEITHER)
1330 break;
1332 /* Now process the indirect call. */
1333 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1335 enum pure_const_state_e edge_state = IPA_CONST;
1336 bool edge_looping = false;
1338 if (dump_file && (dump_flags & TDF_DETAILS))
1339 fprintf (dump_file, " Indirect call");
1340 state_from_flags (&edge_state, &edge_looping,
1341 ie->indirect_info->ecf_flags,
1342 ie->cannot_lead_to_return_p ());
1343 /* Merge the results with what we already know. */
1344 better_state (&edge_state, &edge_looping,
1345 w_l->state_previously_known,
1346 w_l->looping_previously_known);
1347 worse_state (&pure_const_state, &looping,
1348 edge_state, edge_looping);
1349 if (pure_const_state == IPA_NEITHER)
1350 break;
1352 if (pure_const_state == IPA_NEITHER)
1353 break;
1355 /* And finally all loads and stores. */
1356 for (i = 0; w->iterate_reference (i, ref); i++)
1358 enum pure_const_state_e ref_state = IPA_CONST;
1359 bool ref_looping = false;
1360 switch (ref->use)
1362 case IPA_REF_LOAD:
1363 /* readonly reads are safe. */
1364 if (TREE_READONLY (ref->referred->decl))
1365 break;
1366 if (dump_file && (dump_flags & TDF_DETAILS))
1367 fprintf (dump_file, " nonreadonly global var read\n");
1368 ref_state = IPA_PURE;
1369 break;
1370 case IPA_REF_STORE:
1371 if (ref->cannot_lead_to_return ())
1372 break;
1373 ref_state = IPA_NEITHER;
1374 if (dump_file && (dump_flags & TDF_DETAILS))
1375 fprintf (dump_file, " global var write\n");
1376 break;
1377 case IPA_REF_ADDR:
1378 case IPA_REF_CHKP:
1379 break;
1380 default:
1381 gcc_unreachable ();
1383 better_state (&ref_state, &ref_looping,
1384 w_l->state_previously_known,
1385 w_l->looping_previously_known);
1386 worse_state (&pure_const_state, &looping,
1387 ref_state, ref_looping);
1388 if (pure_const_state == IPA_NEITHER)
1389 break;
1391 w_info = (struct ipa_dfs_info *) w->aux;
1392 w = w_info->next_cycle;
1394 if (dump_file && (dump_flags & TDF_DETAILS))
1395 fprintf (dump_file, "Result %s looping %i\n",
1396 pure_const_names [pure_const_state],
1397 looping);
1399 /* Find the worst state of can_free for any node in the cycle. */
1400 bool can_free = false;
1401 w = node;
1402 while (w && !can_free)
1404 struct cgraph_edge *e;
1405 funct_state w_l = get_function_state (w);
1407 if (w_l->can_free
1408 || w->get_availability () == AVAIL_INTERPOSABLE
1409 || w->indirect_calls)
1410 can_free = true;
1412 for (e = w->callees; e && !can_free; e = e->next_callee)
1414 enum availability avail;
1415 struct cgraph_node *y = e->callee->
1416 function_or_virtual_thunk_symbol (&avail);
1418 if (avail > AVAIL_INTERPOSABLE)
1419 can_free = get_function_state (y)->can_free;
1420 else
1421 can_free = true;
1423 w_info = (struct ipa_dfs_info *) w->aux;
1424 w = w_info->next_cycle;
1427 /* Copy back the region's pure_const_state which is shared by
1428 all nodes in the region. */
1429 w = node;
1430 while (w)
1432 funct_state w_l = get_function_state (w);
1433 enum pure_const_state_e this_state = pure_const_state;
1434 bool this_looping = looping;
1436 w_l->can_free = can_free;
1437 w->nonfreeing_fn = !can_free;
1438 if (!can_free && dump_file)
1439 fprintf (dump_file, "Function found not to call free: %s\n",
1440 w->name ());
1442 if (w_l->state_previously_known != IPA_NEITHER
1443 && this_state > w_l->state_previously_known)
1445 this_state = w_l->state_previously_known;
1446 this_looping |= w_l->looping_previously_known;
1448 if (!this_looping && self_recursive_p (w))
1449 this_looping = true;
1450 if (!w_l->looping_previously_known)
1451 this_looping = false;
1453 /* All nodes within a cycle share the same info. */
1454 w_l->pure_const_state = this_state;
1455 w_l->looping = this_looping;
1457 /* Inline clones share declaration with their offline copies;
1458 do not modify their declarations since the offline copy may
1459 be different. */
1460 if (!w->global.inlined_to)
1461 switch (this_state)
1463 case IPA_CONST:
1464 if (!TREE_READONLY (w->decl))
1466 warn_function_const (w->decl, !this_looping);
1467 if (dump_file)
1468 fprintf (dump_file, "Function found to be %sconst: %s\n",
1469 this_looping ? "looping " : "",
1470 w->name ());
1472 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1473 NULL, true);
1474 w->set_const_flag (true, this_looping);
1475 break;
1477 case IPA_PURE:
1478 if (!DECL_PURE_P (w->decl))
1480 warn_function_pure (w->decl, !this_looping);
1481 if (dump_file)
1482 fprintf (dump_file, "Function found to be %spure: %s\n",
1483 this_looping ? "looping " : "",
1484 w->name ());
1486 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1487 NULL, true);
1488 w->set_pure_flag (true, this_looping);
1489 break;
1491 default:
1492 break;
1494 w_info = (struct ipa_dfs_info *) w->aux;
1495 w = w_info->next_cycle;
1499 ipa_free_postorder_info ();
1500 free (order);
1501 return remove_p;
1504 /* Produce transitive closure over the callgraph and compute nothrow
1505 attributes. */
1507 static void
1508 propagate_nothrow (void)
1510 struct cgraph_node *node;
1511 struct cgraph_node *w;
1512 struct cgraph_node **order =
1513 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1514 int order_pos;
1515 int i;
1516 struct ipa_dfs_info * w_info;
1518 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1519 if (dump_file)
1521 cgraph_node::dump_cgraph (dump_file);
1522 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1525 /* Propagate the local information through the call graph to produce
1526 the global information. All the nodes within a cycle will have
1527 the same info so we collapse cycles first. Then we can do the
1528 propagation in one pass from the leaves to the roots. */
1529 for (i = 0; i < order_pos; i++ )
1531 bool can_throw = false;
1532 node = order[i];
1534 if (node->alias)
1535 continue;
1537 /* Find the worst state for any node in the cycle. */
1538 w = node;
1539 while (w && !can_throw)
1541 struct cgraph_edge *e, *ie;
1542 funct_state w_l = get_function_state (w);
1544 if (w_l->can_throw
1545 || w->get_availability () == AVAIL_INTERPOSABLE)
1546 can_throw = true;
1548 for (e = w->callees; e && !can_throw; e = e->next_callee)
1550 enum availability avail;
1551 struct cgraph_node *y = e->callee->
1552 function_or_virtual_thunk_symbol (&avail);
1554 if (avail > AVAIL_INTERPOSABLE)
1556 funct_state y_l = get_function_state (y);
1558 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1559 && e->can_throw_external)
1560 can_throw = true;
1562 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1563 can_throw = true;
1565 for (ie = w->indirect_calls; ie && !can_throw; ie = ie->next_callee)
1566 if (ie->can_throw_external)
1567 can_throw = true;
1568 w_info = (struct ipa_dfs_info *) w->aux;
1569 w = w_info->next_cycle;
1572 /* Copy back the region's pure_const_state which is shared by
1573 all nodes in the region. */
1574 w = node;
1575 while (w)
1577 funct_state w_l = get_function_state (w);
1578 if (!can_throw && !TREE_NOTHROW (w->decl))
1580 /* Inline clones share declaration with their offline copies;
1581 do not modify their declarations since the offline copy may
1582 be different. */
1583 if (!w->global.inlined_to)
1585 w->set_nothrow_flag (true);
1586 if (dump_file)
1587 fprintf (dump_file, "Function found to be nothrow: %s\n",
1588 w->name ());
1591 else if (can_throw && !TREE_NOTHROW (w->decl))
1592 w_l->can_throw = true;
1593 w_info = (struct ipa_dfs_info *) w->aux;
1594 w = w_info->next_cycle;
1598 ipa_free_postorder_info ();
1599 free (order);
1603 /* Produce the global information by preforming a transitive closure
1604 on the local information that was produced by generate_summary. */
1606 unsigned int
1607 pass_ipa_pure_const::
1608 execute (function *)
1610 struct cgraph_node *node;
1611 bool remove_p;
1613 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1614 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1615 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1617 /* Nothrow makes more function to not lead to return and improve
1618 later analysis. */
1619 propagate_nothrow ();
1620 remove_p = propagate_pure_const ();
1622 /* Cleanup. */
1623 FOR_EACH_FUNCTION (node)
1624 if (has_function_state (node))
1625 free (get_function_state (node));
1626 funct_state_vec.release ();
1627 return remove_p ? TODO_remove_functions : 0;
1630 static bool
1631 gate_pure_const (void)
1633 return flag_ipa_pure_const || in_lto_p;
1636 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1637 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1638 pure_const_generate_summary, /* generate_summary */
1639 pure_const_write_summary, /* write_summary */
1640 pure_const_read_summary, /* read_summary */
1641 NULL, /* write_optimization_summary */
1642 NULL, /* read_optimization_summary */
1643 NULL, /* stmt_fixup */
1644 0, /* function_transform_todo_flags_start */
1645 NULL, /* function_transform */
1646 NULL), /* variable_transform */
1647 init_p(false),
1648 function_insertion_hook_holder(NULL),
1649 node_duplication_hook_holder(NULL),
1650 node_removal_hook_holder(NULL)
1654 ipa_opt_pass_d *
1655 make_pass_ipa_pure_const (gcc::context *ctxt)
1657 return new pass_ipa_pure_const (ctxt);
1660 /* Return true if function should be skipped for local pure const analysis. */
1662 static bool
1663 skip_function_for_local_pure_const (struct cgraph_node *node)
1665 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1666 we must not promote functions that are called by already processed functions. */
1668 if (function_called_by_processed_nodes_p ())
1670 if (dump_file)
1671 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1672 return true;
1674 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1676 if (dump_file)
1677 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1678 return true;
1680 return false;
1683 /* Simple local pass for pure const discovery reusing the analysis from
1684 ipa_pure_const. This pass is effective when executed together with
1685 other optimization passes in early optimization pass queue. */
1687 namespace {
1689 const pass_data pass_data_local_pure_const =
1691 GIMPLE_PASS, /* type */
1692 "local-pure-const", /* name */
1693 OPTGROUP_NONE, /* optinfo_flags */
1694 TV_IPA_PURE_CONST, /* tv_id */
1695 0, /* properties_required */
1696 0, /* properties_provided */
1697 0, /* properties_destroyed */
1698 0, /* todo_flags_start */
1699 0, /* todo_flags_finish */
1702 class pass_local_pure_const : public gimple_opt_pass
1704 public:
1705 pass_local_pure_const (gcc::context *ctxt)
1706 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1709 /* opt_pass methods: */
1710 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1711 virtual bool gate (function *) { return gate_pure_const (); }
1712 virtual unsigned int execute (function *);
1714 }; // class pass_local_pure_const
1716 unsigned int
1717 pass_local_pure_const::execute (function *fun)
1719 bool changed = false;
1720 funct_state l;
1721 bool skip;
1722 struct cgraph_node *node;
1724 node = cgraph_node::get (current_function_decl);
1725 skip = skip_function_for_local_pure_const (node);
1726 if (!warn_suggest_attribute_const
1727 && !warn_suggest_attribute_pure
1728 && skip)
1729 return 0;
1731 l = analyze_function (node, false);
1733 /* Do NORETURN discovery. */
1734 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1735 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1737 warn_function_noreturn (fun->decl);
1738 if (dump_file)
1739 fprintf (dump_file, "Function found to be noreturn: %s\n",
1740 current_function_name ());
1742 /* Update declaration and reduce profile to executed once. */
1743 TREE_THIS_VOLATILE (current_function_decl) = 1;
1744 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1745 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1747 changed = true;
1750 switch (l->pure_const_state)
1752 case IPA_CONST:
1753 if (!TREE_READONLY (current_function_decl))
1755 warn_function_const (current_function_decl, !l->looping);
1756 if (!skip)
1758 node->set_const_flag (true, l->looping);
1759 changed = true;
1761 if (dump_file)
1762 fprintf (dump_file, "Function found to be %sconst: %s\n",
1763 l->looping ? "looping " : "",
1764 current_function_name ());
1766 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1767 && !l->looping)
1769 if (!skip)
1771 node->set_const_flag (true, false);
1772 changed = true;
1774 if (dump_file)
1775 fprintf (dump_file, "Function found to be non-looping: %s\n",
1776 current_function_name ());
1778 break;
1780 case IPA_PURE:
1781 if (!DECL_PURE_P (current_function_decl))
1783 if (!skip)
1785 node->set_pure_flag (true, l->looping);
1786 changed = true;
1788 warn_function_pure (current_function_decl, !l->looping);
1789 if (dump_file)
1790 fprintf (dump_file, "Function found to be %spure: %s\n",
1791 l->looping ? "looping " : "",
1792 current_function_name ());
1794 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1795 && !l->looping)
1797 if (!skip)
1799 node->set_pure_flag (true, false);
1800 changed = true;
1802 if (dump_file)
1803 fprintf (dump_file, "Function found to be non-looping: %s\n",
1804 current_function_name ());
1806 break;
1808 default:
1809 break;
1811 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1813 node->set_nothrow_flag (true);
1814 changed = true;
1815 if (dump_file)
1816 fprintf (dump_file, "Function found to be nothrow: %s\n",
1817 current_function_name ());
1819 free (l);
1820 if (changed)
1821 return execute_fixup_cfg ();
1822 else
1823 return 0;
1826 } // anon namespace
1828 gimple_opt_pass *
1829 make_pass_local_pure_const (gcc::context *ctxt)
1831 return new pass_local_pure_const (ctxt);
1834 /* Emit noreturn warnings. */
1836 namespace {
1838 const pass_data pass_data_warn_function_noreturn =
1840 GIMPLE_PASS, /* type */
1841 "*warn_function_noreturn", /* name */
1842 OPTGROUP_NONE, /* optinfo_flags */
1843 TV_NONE, /* tv_id */
1844 PROP_cfg, /* properties_required */
1845 0, /* properties_provided */
1846 0, /* properties_destroyed */
1847 0, /* todo_flags_start */
1848 0, /* todo_flags_finish */
1851 class pass_warn_function_noreturn : public gimple_opt_pass
1853 public:
1854 pass_warn_function_noreturn (gcc::context *ctxt)
1855 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1858 /* opt_pass methods: */
1859 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1860 virtual unsigned int execute (function *fun)
1862 if (!TREE_THIS_VOLATILE (current_function_decl)
1863 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1864 warn_function_noreturn (current_function_decl);
1865 return 0;
1868 }; // class pass_warn_function_noreturn
1870 } // anon namespace
1872 gimple_opt_pass *
1873 make_pass_warn_function_noreturn (gcc::context *ctxt)
1875 return new pass_warn_function_noreturn (ctxt);
1878 /* Simple local pass for pure const discovery reusing the analysis from
1879 ipa_pure_const. This pass is effective when executed together with
1880 other optimization passes in early optimization pass queue. */
1882 namespace {
1884 const pass_data pass_data_nothrow =
1886 GIMPLE_PASS, /* type */
1887 "nothrow", /* name */
1888 OPTGROUP_NONE, /* optinfo_flags */
1889 TV_IPA_PURE_CONST, /* tv_id */
1890 0, /* properties_required */
1891 0, /* properties_provided */
1892 0, /* properties_destroyed */
1893 0, /* todo_flags_start */
1894 0, /* todo_flags_finish */
1897 class pass_nothrow : public gimple_opt_pass
1899 public:
1900 pass_nothrow (gcc::context *ctxt)
1901 : gimple_opt_pass (pass_data_nothrow, ctxt)
1904 /* opt_pass methods: */
1905 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1906 virtual bool gate (function *) { return optimize; }
1907 virtual unsigned int execute (function *);
1909 }; // class pass_nothrow
1911 unsigned int
1912 pass_nothrow::execute (function *)
1914 struct cgraph_node *node;
1915 basic_block this_block;
1917 if (TREE_NOTHROW (current_function_decl))
1918 return 0;
1920 node = cgraph_node::get (current_function_decl);
1922 /* We run during lowering, we can not really use availability yet. */
1923 if (cgraph_node::get (current_function_decl)->get_availability ()
1924 <= AVAIL_INTERPOSABLE)
1926 if (dump_file)
1927 fprintf (dump_file, "Function is interposable;"
1928 " not analyzing.\n");
1929 return true;
1932 FOR_EACH_BB_FN (this_block, cfun)
1934 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1935 !gsi_end_p (gsi);
1936 gsi_next (&gsi))
1937 if (stmt_can_throw_external (gsi_stmt (gsi)))
1939 if (is_gimple_call (gsi_stmt (gsi)))
1941 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
1942 if (callee_t && recursive_call_p (current_function_decl,
1943 callee_t))
1944 continue;
1947 if (dump_file)
1949 fprintf (dump_file, "Statement can throw: ");
1950 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1952 return 0;
1956 node->set_nothrow_flag (true);
1957 if (dump_file)
1958 fprintf (dump_file, "Function found to be nothrow: %s\n",
1959 current_function_name ());
1960 return 0;
1963 } // anon namespace
1965 gimple_opt_pass *
1966 make_pass_nothrow (gcc::context *ctxt)
1968 return new pass_nothrow (ctxt);