* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / ipa-pure-const.c
blobdbceb0481764fd2bc60397c87e4a2f076d35c5ff
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 "alias.h"
39 #include "symtab.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "print-tree.h"
43 #include "calls.h"
44 #include "predict.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "dominance.h"
48 #include "cfg.h"
49 #include "cfganal.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "tree-eh.h"
54 #include "gimple-expr.h"
55 #include "gimple.h"
56 #include "gimple-iterator.h"
57 #include "gimple-walk.h"
58 #include "tree-cfg.h"
59 #include "tree-ssa-loop-niter.h"
60 #include "tree-inline.h"
61 #include "tree-pass.h"
62 #include "langhooks.h"
63 #include "cgraph.h"
64 #include "ipa-utils.h"
65 #include "flags.h"
66 #include "diagnostic.h"
67 #include "gimple-pretty-print.h"
68 #include "langhooks.h"
69 #include "target.h"
70 #include "lto-streamer.h"
71 #include "data-streamer.h"
72 #include "tree-streamer.h"
73 #include "cfgloop.h"
74 #include "tree-scalar-evolution.h"
75 #include "intl.h"
76 #include "opts.h"
77 #include "varasm.h"
79 /* Lattice values for const and pure functions. Everything starts out
80 being const, then may drop to pure and then neither depending on
81 what is found. */
82 enum pure_const_state_e
84 IPA_CONST,
85 IPA_PURE,
86 IPA_NEITHER
89 const char *pure_const_names[3] = {"const", "pure", "neither"};
91 /* Holder for the const_state. There is one of these per function
92 decl. */
93 struct funct_state_d
95 /* See above. */
96 enum pure_const_state_e pure_const_state;
97 /* What user set here; we can be always sure about this. */
98 enum pure_const_state_e state_previously_known;
99 bool looping_previously_known;
101 /* True if the function could possibly infinite loop. There are a
102 lot of ways that this could be determined. We are pretty
103 conservative here. While it is possible to cse pure and const
104 calls, it is not legal to have dce get rid of the call if there
105 is a possibility that the call could infinite loop since this is
106 a behavioral change. */
107 bool looping;
109 bool can_throw;
111 /* If function can call free, munmap or otherwise make previously
112 non-trapping memory accesses trapping. */
113 bool can_free;
116 /* State used when we know nothing about function. */
117 static struct funct_state_d varying_state
118 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
121 typedef struct funct_state_d * funct_state;
123 /* The storage of the funct_state is abstracted because there is the
124 possibility that it may be desirable to move this to the cgraph
125 local info. */
127 /* Array, indexed by cgraph node uid, of function states. */
129 static vec<funct_state> funct_state_vec;
131 static bool gate_pure_const (void);
133 namespace {
135 const pass_data pass_data_ipa_pure_const =
137 IPA_PASS, /* type */
138 "pure-const", /* name */
139 OPTGROUP_NONE, /* optinfo_flags */
140 TV_IPA_PURE_CONST, /* tv_id */
141 0, /* properties_required */
142 0, /* properties_provided */
143 0, /* properties_destroyed */
144 0, /* todo_flags_start */
145 0, /* todo_flags_finish */
148 class pass_ipa_pure_const : public ipa_opt_pass_d
150 public:
151 pass_ipa_pure_const(gcc::context *ctxt);
153 /* opt_pass methods: */
154 bool gate (function *) { return gate_pure_const (); }
155 unsigned int execute (function *fun);
157 void register_hooks (void);
159 private:
160 bool init_p;
162 /* Holders of ipa cgraph hooks: */
163 struct cgraph_node_hook_list *function_insertion_hook_holder;
164 struct cgraph_2node_hook_list *node_duplication_hook_holder;
165 struct cgraph_node_hook_list *node_removal_hook_holder;
167 }; // class pass_ipa_pure_const
169 } // anon namespace
171 /* Try to guess if function body will always be visible to compiler
172 when compiling the call and whether compiler will be able
173 to propagate the information by itself. */
175 static bool
176 function_always_visible_to_compiler_p (tree decl)
178 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
181 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
182 is true if the function is known to be finite. The diagnostic is
183 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
184 OPTION, this function may initialize it and it is always returned
185 by the function. */
187 static hash_set<tree> *
188 suggest_attribute (int option, tree decl, bool known_finite,
189 hash_set<tree> *warned_about,
190 const char * attrib_name)
192 if (!option_enabled (option, &global_options))
193 return warned_about;
194 if (TREE_THIS_VOLATILE (decl)
195 || (known_finite && function_always_visible_to_compiler_p (decl)))
196 return warned_about;
198 if (!warned_about)
199 warned_about = new hash_set<tree>;
200 if (warned_about->contains (decl))
201 return warned_about;
202 warned_about->add (decl);
203 warning_at (DECL_SOURCE_LOCATION (decl),
204 option,
205 known_finite
206 ? _("function might be candidate for attribute %<%s%>")
207 : _("function might be candidate for attribute %<%s%>"
208 " if it is known to return normally"), attrib_name);
209 return warned_about;
212 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
213 is true if the function is known to be finite. */
215 static void
216 warn_function_pure (tree decl, bool known_finite)
218 static hash_set<tree> *warned_about;
220 warned_about
221 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
222 known_finite, warned_about, "pure");
225 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
226 is true if the function is known to be finite. */
228 static void
229 warn_function_const (tree decl, bool known_finite)
231 static hash_set<tree> *warned_about;
232 warned_about
233 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
234 known_finite, warned_about, "const");
237 static void
238 warn_function_noreturn (tree decl)
240 static hash_set<tree> *warned_about;
241 if (!lang_hooks.missing_noreturn_ok_p (decl)
242 && targetm.warn_func_return (decl))
243 warned_about
244 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
245 true, warned_about, "noreturn");
248 /* Return true if we have a function state for NODE. */
250 static inline bool
251 has_function_state (struct cgraph_node *node)
253 if (!funct_state_vec.exists ()
254 || funct_state_vec.length () <= (unsigned int)node->uid)
255 return false;
256 return funct_state_vec[node->uid] != NULL;
259 /* Return the function state from NODE. */
261 static inline funct_state
262 get_function_state (struct cgraph_node *node)
264 if (!funct_state_vec.exists ()
265 || funct_state_vec.length () <= (unsigned int)node->uid
266 || !funct_state_vec[node->uid])
267 /* We might want to put correct previously_known state into varying. */
268 return &varying_state;
269 return funct_state_vec[node->uid];
272 /* Set the function state S for NODE. */
274 static inline void
275 set_function_state (struct cgraph_node *node, funct_state s)
277 if (!funct_state_vec.exists ()
278 || funct_state_vec.length () <= (unsigned int)node->uid)
279 funct_state_vec.safe_grow_cleared (node->uid + 1);
280 funct_state_vec[node->uid] = s;
283 /* Check to see if the use (or definition when CHECKING_WRITE is true)
284 variable T is legal in a function that is either pure or const. */
286 static inline void
287 check_decl (funct_state local,
288 tree t, bool checking_write, bool ipa)
290 /* Do not want to do anything with volatile except mark any
291 function that uses one to be not const or pure. */
292 if (TREE_THIS_VOLATILE (t))
294 local->pure_const_state = IPA_NEITHER;
295 if (dump_file)
296 fprintf (dump_file, " Volatile operand is not const/pure");
297 return;
300 /* Do not care about a local automatic that is not static. */
301 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
302 return;
304 /* If the variable has the "used" attribute, treat it as if it had a
305 been touched by the devil. */
306 if (DECL_PRESERVE_P (t))
308 local->pure_const_state = IPA_NEITHER;
309 if (dump_file)
310 fprintf (dump_file, " Used static/global variable is not const/pure\n");
311 return;
314 /* In IPA mode we are not interested in checking actual loads and stores;
315 they will be processed at propagation time using ipa_ref. */
316 if (ipa)
317 return;
319 /* Since we have dealt with the locals and params cases above, if we
320 are CHECKING_WRITE, this cannot be a pure or constant
321 function. */
322 if (checking_write)
324 local->pure_const_state = IPA_NEITHER;
325 if (dump_file)
326 fprintf (dump_file, " static/global memory write is not const/pure\n");
327 return;
330 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
332 /* Readonly reads are safe. */
333 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
334 return; /* Read of a constant, do not change the function state. */
335 else
337 if (dump_file)
338 fprintf (dump_file, " global memory read is not const\n");
339 /* Just a regular read. */
340 if (local->pure_const_state == IPA_CONST)
341 local->pure_const_state = IPA_PURE;
344 else
346 /* Compilation level statics can be read if they are readonly
347 variables. */
348 if (TREE_READONLY (t))
349 return;
351 if (dump_file)
352 fprintf (dump_file, " static memory read is not const\n");
353 /* Just a regular read. */
354 if (local->pure_const_state == IPA_CONST)
355 local->pure_const_state = IPA_PURE;
360 /* Check to see if the use (or definition when CHECKING_WRITE is true)
361 variable T is legal in a function that is either pure or const. */
363 static inline void
364 check_op (funct_state local, tree t, bool checking_write)
366 t = get_base_address (t);
367 if (t && TREE_THIS_VOLATILE (t))
369 local->pure_const_state = IPA_NEITHER;
370 if (dump_file)
371 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
372 return;
374 else if (t
375 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
376 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
377 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
379 if (dump_file)
380 fprintf (dump_file, " Indirect ref to local memory is OK\n");
381 return;
383 else if (checking_write)
385 local->pure_const_state = IPA_NEITHER;
386 if (dump_file)
387 fprintf (dump_file, " Indirect ref write is not const/pure\n");
388 return;
390 else
392 if (dump_file)
393 fprintf (dump_file, " Indirect ref read is not const\n");
394 if (local->pure_const_state == IPA_CONST)
395 local->pure_const_state = IPA_PURE;
399 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
401 static void
402 state_from_flags (enum pure_const_state_e *state, bool *looping,
403 int flags, bool cannot_lead_to_return)
405 *looping = false;
406 if (flags & ECF_LOOPING_CONST_OR_PURE)
408 *looping = true;
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, " looping");
412 if (flags & ECF_CONST)
414 *state = IPA_CONST;
415 if (dump_file && (dump_flags & TDF_DETAILS))
416 fprintf (dump_file, " const\n");
418 else if (flags & ECF_PURE)
420 *state = IPA_PURE;
421 if (dump_file && (dump_flags & TDF_DETAILS))
422 fprintf (dump_file, " pure\n");
424 else if (cannot_lead_to_return)
426 *state = IPA_PURE;
427 *looping = true;
428 if (dump_file && (dump_flags & TDF_DETAILS))
429 fprintf (dump_file, " ignoring side effects->pure looping\n");
431 else
433 if (dump_file && (dump_flags & TDF_DETAILS))
434 fprintf (dump_file, " neither\n");
435 *state = IPA_NEITHER;
436 *looping = true;
440 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
441 into STATE and LOOPING better of the two variants.
442 Be sure to merge looping correctly. IPA_NEITHER functions
443 have looping 0 even if they don't have to return. */
445 static inline void
446 better_state (enum pure_const_state_e *state, bool *looping,
447 enum pure_const_state_e state2, bool looping2)
449 if (state2 < *state)
451 if (*state == IPA_NEITHER)
452 *looping = looping2;
453 else
454 *looping = MIN (*looping, looping2);
455 *state = state2;
457 else if (state2 != IPA_NEITHER)
458 *looping = MIN (*looping, looping2);
461 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
462 into STATE and LOOPING worse of the two variants. */
464 static inline void
465 worse_state (enum pure_const_state_e *state, bool *looping,
466 enum pure_const_state_e state2, bool looping2)
468 *state = MAX (*state, state2);
469 *looping = MAX (*looping, looping2);
472 /* Recognize special cases of builtins that are by themselves not pure or const
473 but function using them is. */
474 static bool
475 special_builtin_state (enum pure_const_state_e *state, bool *looping,
476 tree callee)
478 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
479 switch (DECL_FUNCTION_CODE (callee))
481 case BUILT_IN_RETURN:
482 case BUILT_IN_UNREACHABLE:
483 case BUILT_IN_ALLOCA:
484 case BUILT_IN_ALLOCA_WITH_ALIGN:
485 case BUILT_IN_STACK_SAVE:
486 case BUILT_IN_STACK_RESTORE:
487 case BUILT_IN_EH_POINTER:
488 case BUILT_IN_EH_FILTER:
489 case BUILT_IN_UNWIND_RESUME:
490 case BUILT_IN_CXA_END_CLEANUP:
491 case BUILT_IN_EH_COPY_VALUES:
492 case BUILT_IN_FRAME_ADDRESS:
493 case BUILT_IN_APPLY:
494 case BUILT_IN_APPLY_ARGS:
495 *looping = false;
496 *state = IPA_CONST;
497 return true;
498 case BUILT_IN_PREFETCH:
499 *looping = true;
500 *state = IPA_CONST;
501 return true;
502 default:
503 break;
505 return false;
508 /* Check the parameters of a function call to CALL_EXPR to see if
509 there are any references in the parameters that are not allowed for
510 pure or const functions. Also check to see if this is either an
511 indirect call, a call outside the compilation unit, or has special
512 attributes that may also effect the purity. The CALL_EXPR node for
513 the entire call expression. */
515 static void
516 check_call (funct_state local, gcall *call, bool ipa)
518 int flags = gimple_call_flags (call);
519 tree callee_t = gimple_call_fndecl (call);
520 bool possibly_throws = stmt_could_throw_p (call);
521 bool possibly_throws_externally = (possibly_throws
522 && stmt_can_throw_external (call));
524 if (possibly_throws)
526 unsigned int i;
527 for (i = 0; i < gimple_num_ops (call); i++)
528 if (gimple_op (call, i)
529 && tree_could_throw_p (gimple_op (call, i)))
531 if (possibly_throws && cfun->can_throw_non_call_exceptions)
533 if (dump_file)
534 fprintf (dump_file, " operand can throw; looping\n");
535 local->looping = true;
537 if (possibly_throws_externally)
539 if (dump_file)
540 fprintf (dump_file, " operand can throw externally\n");
541 local->can_throw = true;
546 /* The const and pure flags are set by a variety of places in the
547 compiler (including here). If someone has already set the flags
548 for the callee, (such as for some of the builtins) we will use
549 them, otherwise we will compute our own information.
551 Const and pure functions have less clobber effects than other
552 functions so we process these first. Otherwise if it is a call
553 outside the compilation unit or an indirect call we punt. This
554 leaves local calls which will be processed by following the call
555 graph. */
556 if (callee_t)
558 enum pure_const_state_e call_state;
559 bool call_looping;
561 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
562 && !nonfreeing_call_p (call))
563 local->can_free = true;
565 if (special_builtin_state (&call_state, &call_looping, callee_t))
567 worse_state (&local->pure_const_state, &local->looping,
568 call_state, call_looping);
569 return;
571 /* When bad things happen to bad functions, they cannot be const
572 or pure. */
573 if (setjmp_call_p (callee_t))
575 if (dump_file)
576 fprintf (dump_file, " setjmp is not const/pure\n");
577 local->looping = true;
578 local->pure_const_state = IPA_NEITHER;
581 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
582 switch (DECL_FUNCTION_CODE (callee_t))
584 case BUILT_IN_LONGJMP:
585 case BUILT_IN_NONLOCAL_GOTO:
586 if (dump_file)
587 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
588 local->pure_const_state = IPA_NEITHER;
589 local->looping = true;
590 break;
591 default:
592 break;
595 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
596 local->can_free = true;
598 /* When not in IPA mode, we can still handle self recursion. */
599 if (!ipa && callee_t
600 && recursive_call_p (current_function_decl, callee_t))
602 if (dump_file)
603 fprintf (dump_file, " Recursive call can loop.\n");
604 local->looping = true;
606 /* Either callee is unknown or we are doing local analysis.
607 Look to see if there are any bits available for the callee (such as by
608 declaration or because it is builtin) and process solely on the basis of
609 those bits. */
610 else if (!ipa)
612 enum pure_const_state_e call_state;
613 bool call_looping;
614 if (possibly_throws && cfun->can_throw_non_call_exceptions)
616 if (dump_file)
617 fprintf (dump_file, " can throw; looping\n");
618 local->looping = true;
620 if (possibly_throws_externally)
622 if (dump_file)
624 fprintf (dump_file, " can throw externally to lp %i\n",
625 lookup_stmt_eh_lp (call));
626 if (callee_t)
627 fprintf (dump_file, " callee:%s\n",
628 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
630 local->can_throw = true;
632 if (dump_file && (dump_flags & TDF_DETAILS))
633 fprintf (dump_file, " checking flags for call:");
634 state_from_flags (&call_state, &call_looping, flags,
635 ((flags & (ECF_NORETURN | ECF_NOTHROW))
636 == (ECF_NORETURN | ECF_NOTHROW))
637 || (!flag_exceptions && (flags & ECF_NORETURN)));
638 worse_state (&local->pure_const_state, &local->looping,
639 call_state, call_looping);
641 /* Direct functions calls are handled by IPA propagation. */
644 /* Wrapper around check_decl for loads in local more. */
646 static bool
647 check_load (gimple, tree op, tree, void *data)
649 if (DECL_P (op))
650 check_decl ((funct_state)data, op, false, false);
651 else
652 check_op ((funct_state)data, op, false);
653 return false;
656 /* Wrapper around check_decl for stores in local more. */
658 static bool
659 check_store (gimple, tree op, tree, void *data)
661 if (DECL_P (op))
662 check_decl ((funct_state)data, op, true, false);
663 else
664 check_op ((funct_state)data, op, true);
665 return false;
668 /* Wrapper around check_decl for loads in ipa mode. */
670 static bool
671 check_ipa_load (gimple, tree op, tree, void *data)
673 if (DECL_P (op))
674 check_decl ((funct_state)data, op, false, true);
675 else
676 check_op ((funct_state)data, op, false);
677 return false;
680 /* Wrapper around check_decl for stores in ipa mode. */
682 static bool
683 check_ipa_store (gimple, tree op, tree, void *data)
685 if (DECL_P (op))
686 check_decl ((funct_state)data, op, true, true);
687 else
688 check_op ((funct_state)data, op, true);
689 return false;
692 /* Look into pointer pointed to by GSIP and figure out what interesting side
693 effects it has. */
694 static void
695 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
697 gimple stmt = gsi_stmt (*gsip);
699 if (is_gimple_debug (stmt))
700 return;
702 /* Do consider clobber as side effects before IPA, so we rather inline
703 C++ destructors and keep clobber semantics than eliminate them.
705 TODO: We may get smarter during early optimizations on these and let
706 functions containing only clobbers to be optimized more. This is a common
707 case of C++ destructors. */
709 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
710 return;
712 if (dump_file)
714 fprintf (dump_file, " scanning: ");
715 print_gimple_stmt (dump_file, stmt, 0, 0);
718 if (gimple_has_volatile_ops (stmt)
719 && !gimple_clobber_p (stmt))
721 local->pure_const_state = IPA_NEITHER;
722 if (dump_file)
723 fprintf (dump_file, " Volatile stmt is not const/pure\n");
726 /* Look for loads and stores. */
727 walk_stmt_load_store_ops (stmt, local,
728 ipa ? check_ipa_load : check_load,
729 ipa ? check_ipa_store : check_store);
731 if (gimple_code (stmt) != GIMPLE_CALL
732 && stmt_could_throw_p (stmt))
734 if (cfun->can_throw_non_call_exceptions)
736 if (dump_file)
737 fprintf (dump_file, " can throw; looping\n");
738 local->looping = true;
740 if (stmt_can_throw_external (stmt))
742 if (dump_file)
743 fprintf (dump_file, " can throw externally\n");
744 local->can_throw = true;
746 else
747 if (dump_file)
748 fprintf (dump_file, " can throw\n");
750 switch (gimple_code (stmt))
752 case GIMPLE_CALL:
753 check_call (local, as_a <gcall *> (stmt), ipa);
754 break;
755 case GIMPLE_LABEL:
756 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
757 /* Target of long jump. */
759 if (dump_file)
760 fprintf (dump_file, " nonlocal label is not const/pure\n");
761 local->pure_const_state = IPA_NEITHER;
763 break;
764 case GIMPLE_ASM:
765 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
767 if (dump_file)
768 fprintf (dump_file, " memory asm clobber is not const/pure\n");
769 /* Abandon all hope, ye who enter here. */
770 local->pure_const_state = IPA_NEITHER;
771 local->can_free = true;
773 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
775 if (dump_file)
776 fprintf (dump_file, " volatile is not const/pure\n");
777 /* Abandon all hope, ye who enter here. */
778 local->pure_const_state = IPA_NEITHER;
779 local->looping = true;
780 local->can_free = true;
782 return;
783 default:
784 break;
789 /* This is the main routine for finding the reference patterns for
790 global variables within a function FN. */
792 static funct_state
793 analyze_function (struct cgraph_node *fn, bool ipa)
795 tree decl = fn->decl;
796 funct_state l;
797 basic_block this_block;
799 l = XCNEW (struct funct_state_d);
800 l->pure_const_state = IPA_CONST;
801 l->state_previously_known = IPA_NEITHER;
802 l->looping_previously_known = true;
803 l->looping = false;
804 l->can_throw = false;
805 l->can_free = false;
806 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
807 flags_from_decl_or_type (fn->decl),
808 fn->cannot_return_p ());
810 if (fn->thunk.thunk_p || fn->alias)
812 /* Thunk gets propagated through, so nothing interesting happens. */
813 gcc_assert (ipa);
814 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
815 l->pure_const_state = IPA_NEITHER;
816 return l;
819 if (dump_file)
821 fprintf (dump_file, "\n\n local analysis of %s\n ",
822 fn->name ());
825 push_cfun (DECL_STRUCT_FUNCTION (decl));
827 FOR_EACH_BB_FN (this_block, cfun)
829 gimple_stmt_iterator gsi;
830 struct walk_stmt_info wi;
832 memset (&wi, 0, sizeof (wi));
833 for (gsi = gsi_start_bb (this_block);
834 !gsi_end_p (gsi);
835 gsi_next (&gsi))
837 check_stmt (&gsi, l, ipa);
838 if (l->pure_const_state == IPA_NEITHER
839 && l->looping
840 && l->can_throw
841 && l->can_free)
842 goto end;
846 end:
847 if (l->pure_const_state != IPA_NEITHER)
849 /* Const functions cannot have back edges (an
850 indication of possible infinite loop side
851 effect. */
852 if (mark_dfs_back_edges ())
854 /* Preheaders are needed for SCEV to work.
855 Simple latches and recorded exits improve chances that loop will
856 proved to be finite in testcases such as in loop-15.c
857 and loop-24.c */
858 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
859 | LOOPS_HAVE_SIMPLE_LATCHES
860 | LOOPS_HAVE_RECORDED_EXITS);
861 if (dump_file && (dump_flags & TDF_DETAILS))
862 flow_loops_dump (dump_file, NULL, 0);
863 if (mark_irreducible_loops ())
865 if (dump_file)
866 fprintf (dump_file, " has irreducible loops\n");
867 l->looping = true;
869 else
871 struct loop *loop;
872 scev_initialize ();
873 FOR_EACH_LOOP (loop, 0)
874 if (!finite_loop_p (loop))
876 if (dump_file)
877 fprintf (dump_file, " can not prove finiteness of "
878 "loop %i\n", loop->num);
879 l->looping =true;
880 break;
882 scev_finalize ();
884 loop_optimizer_finalize ();
888 if (dump_file && (dump_flags & TDF_DETAILS))
889 fprintf (dump_file, " checking previously known:");
891 better_state (&l->pure_const_state, &l->looping,
892 l->state_previously_known,
893 l->looping_previously_known);
894 if (TREE_NOTHROW (decl))
895 l->can_throw = false;
897 pop_cfun ();
898 if (dump_file)
900 if (l->looping)
901 fprintf (dump_file, "Function is locally looping.\n");
902 if (l->can_throw)
903 fprintf (dump_file, "Function is locally throwing.\n");
904 if (l->pure_const_state == IPA_CONST)
905 fprintf (dump_file, "Function is locally const.\n");
906 if (l->pure_const_state == IPA_PURE)
907 fprintf (dump_file, "Function is locally pure.\n");
908 if (l->can_free)
909 fprintf (dump_file, "Function can locally free.\n");
911 return l;
914 /* Called when new function is inserted to callgraph late. */
915 static void
916 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
918 if (node->get_availability () < AVAIL_INTERPOSABLE)
919 return;
920 /* There are some shared nodes, in particular the initializers on
921 static declarations. We do not need to scan them more than once
922 since all we would be interested in are the addressof
923 operations. */
924 if (node->get_availability () > AVAIL_INTERPOSABLE
925 && opt_for_fn (node->decl, flag_ipa_pure_const))
926 set_function_state (node, analyze_function (node, true));
929 /* Called when new clone is inserted to callgraph late. */
931 static void
932 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
933 void *data ATTRIBUTE_UNUSED)
935 if (has_function_state (src))
937 funct_state l = XNEW (struct funct_state_d);
938 gcc_assert (!has_function_state (dst));
939 memcpy (l, get_function_state (src), sizeof (*l));
940 set_function_state (dst, l);
944 /* Called when new clone is inserted to callgraph late. */
946 static void
947 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
949 if (has_function_state (node))
951 funct_state l = get_function_state (node);
952 if (l != &varying_state)
953 free (l);
954 set_function_state (node, NULL);
959 void
960 pass_ipa_pure_const::
961 register_hooks (void)
963 if (init_p)
964 return;
966 init_p = true;
968 node_removal_hook_holder =
969 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
970 node_duplication_hook_holder =
971 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
972 function_insertion_hook_holder =
973 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
977 /* Analyze each function in the cgraph to see if it is locally PURE or
978 CONST. */
980 static void
981 pure_const_generate_summary (void)
983 struct cgraph_node *node;
985 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
986 pass->register_hooks ();
988 /* Process all of the functions.
990 We process AVAIL_INTERPOSABLE functions. We can not use the results
991 by default, but the info can be used at LTO with -fwhole-program or
992 when function got cloned and the clone is AVAILABLE. */
994 FOR_EACH_DEFINED_FUNCTION (node)
995 if (node->get_availability () >= AVAIL_INTERPOSABLE
996 && opt_for_fn (node->decl, flag_ipa_pure_const))
997 set_function_state (node, analyze_function (node, true));
1001 /* Serialize the ipa info for lto. */
1003 static void
1004 pure_const_write_summary (void)
1006 struct cgraph_node *node;
1007 struct lto_simple_output_block *ob
1008 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1009 unsigned int count = 0;
1010 lto_symtab_encoder_iterator lsei;
1011 lto_symtab_encoder_t encoder;
1013 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1015 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1016 lsei_next_function_in_partition (&lsei))
1018 node = lsei_cgraph_node (lsei);
1019 if (node->definition && has_function_state (node))
1020 count++;
1023 streamer_write_uhwi_stream (ob->main_stream, count);
1025 /* Process all of the functions. */
1026 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1027 lsei_next_function_in_partition (&lsei))
1029 node = lsei_cgraph_node (lsei);
1030 if (node->definition && has_function_state (node))
1032 struct bitpack_d bp;
1033 funct_state fs;
1034 int node_ref;
1035 lto_symtab_encoder_t encoder;
1037 fs = get_function_state (node);
1039 encoder = ob->decl_state->symtab_node_encoder;
1040 node_ref = lto_symtab_encoder_encode (encoder, node);
1041 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1043 /* Note that flags will need to be read in the opposite
1044 order as we are pushing the bitflags into FLAGS. */
1045 bp = bitpack_create (ob->main_stream);
1046 bp_pack_value (&bp, fs->pure_const_state, 2);
1047 bp_pack_value (&bp, fs->state_previously_known, 2);
1048 bp_pack_value (&bp, fs->looping_previously_known, 1);
1049 bp_pack_value (&bp, fs->looping, 1);
1050 bp_pack_value (&bp, fs->can_throw, 1);
1051 bp_pack_value (&bp, fs->can_free, 1);
1052 streamer_write_bitpack (&bp);
1056 lto_destroy_simple_output_block (ob);
1060 /* Deserialize the ipa info for lto. */
1062 static void
1063 pure_const_read_summary (void)
1065 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1066 struct lto_file_decl_data *file_data;
1067 unsigned int j = 0;
1069 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1070 pass->register_hooks ();
1072 while ((file_data = file_data_vec[j++]))
1074 const char *data;
1075 size_t len;
1076 struct lto_input_block *ib
1077 = lto_create_simple_input_block (file_data,
1078 LTO_section_ipa_pure_const,
1079 &data, &len);
1080 if (ib)
1082 unsigned int i;
1083 unsigned int count = streamer_read_uhwi (ib);
1085 for (i = 0; i < count; i++)
1087 unsigned int index;
1088 struct cgraph_node *node;
1089 struct bitpack_d bp;
1090 funct_state fs;
1091 lto_symtab_encoder_t encoder;
1093 fs = XCNEW (struct funct_state_d);
1094 index = streamer_read_uhwi (ib);
1095 encoder = file_data->symtab_node_encoder;
1096 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1097 index));
1098 set_function_state (node, fs);
1100 /* Note that the flags must be read in the opposite
1101 order in which they were written (the bitflags were
1102 pushed into FLAGS). */
1103 bp = streamer_read_bitpack (ib);
1104 fs->pure_const_state
1105 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1106 fs->state_previously_known
1107 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1108 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1109 fs->looping = bp_unpack_value (&bp, 1);
1110 fs->can_throw = bp_unpack_value (&bp, 1);
1111 fs->can_free = bp_unpack_value (&bp, 1);
1112 if (dump_file)
1114 int flags = flags_from_decl_or_type (node->decl);
1115 fprintf (dump_file, "Read info for %s/%i ",
1116 node->name (),
1117 node->order);
1118 if (flags & ECF_CONST)
1119 fprintf (dump_file, " const");
1120 if (flags & ECF_PURE)
1121 fprintf (dump_file, " pure");
1122 if (flags & ECF_NOTHROW)
1123 fprintf (dump_file, " nothrow");
1124 fprintf (dump_file, "\n pure const state: %s\n",
1125 pure_const_names[fs->pure_const_state]);
1126 fprintf (dump_file, " previously known state: %s\n",
1127 pure_const_names[fs->looping_previously_known]);
1128 if (fs->looping)
1129 fprintf (dump_file," function is locally looping\n");
1130 if (fs->looping_previously_known)
1131 fprintf (dump_file," function is previously known looping\n");
1132 if (fs->can_throw)
1133 fprintf (dump_file," function is locally throwing\n");
1134 if (fs->can_free)
1135 fprintf (dump_file," function can locally free\n");
1139 lto_destroy_simple_input_block (file_data,
1140 LTO_section_ipa_pure_const,
1141 ib, data, len);
1147 static bool
1148 ignore_edge (struct cgraph_edge *e)
1150 return (!e->can_throw_external);
1153 /* Return true if NODE is self recursive function.
1154 Indirectly recursive functions appears as non-trivial strongly
1155 connected components, so we need to care about self recursion
1156 only. */
1158 static bool
1159 self_recursive_p (struct cgraph_node *node)
1161 struct cgraph_edge *e;
1162 for (e = node->callees; e; e = e->next_callee)
1163 if (e->callee->function_symbol () == node)
1164 return true;
1165 return false;
1168 /* Return true if N is cdtor that is not const or pure. In this case we may
1169 need to remove unreachable function if it is marked const/pure. */
1171 static bool
1172 cdtor_p (cgraph_node *n, void *)
1174 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1175 return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl);
1176 return false;
1179 /* Produce transitive closure over the callgraph and compute pure/const
1180 attributes. */
1182 static bool
1183 propagate_pure_const (void)
1185 struct cgraph_node *node;
1186 struct cgraph_node *w;
1187 struct cgraph_node **order =
1188 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1189 int order_pos;
1190 int i;
1191 struct ipa_dfs_info * w_info;
1192 bool remove_p = false;
1194 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1195 if (dump_file)
1197 cgraph_node::dump_cgraph (dump_file);
1198 ipa_print_order (dump_file, "reduced", order, order_pos);
1201 /* Propagate the local information through the call graph to produce
1202 the global information. All the nodes within a cycle will have
1203 the same info so we collapse cycles first. Then we can do the
1204 propagation in one pass from the leaves to the roots. */
1205 for (i = 0; i < order_pos; i++ )
1207 enum pure_const_state_e pure_const_state = IPA_CONST;
1208 bool looping = false;
1209 int count = 0;
1210 node = order[i];
1212 if (node->alias)
1213 continue;
1215 if (dump_file && (dump_flags & TDF_DETAILS))
1216 fprintf (dump_file, "Starting cycle\n");
1218 /* Find the worst state for any node in the cycle. */
1219 w = node;
1220 while (w && pure_const_state != IPA_NEITHER)
1222 struct cgraph_edge *e;
1223 struct cgraph_edge *ie;
1224 int i;
1225 struct ipa_ref *ref = NULL;
1227 funct_state w_l = get_function_state (w);
1228 if (dump_file && (dump_flags & TDF_DETAILS))
1229 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1230 w->name (),
1231 w->order,
1232 pure_const_names[w_l->pure_const_state],
1233 w_l->looping);
1235 /* First merge in function body properties. */
1236 worse_state (&pure_const_state, &looping,
1237 w_l->pure_const_state, w_l->looping);
1238 if (pure_const_state == IPA_NEITHER)
1239 break;
1241 /* For overwritable nodes we can not assume anything. */
1242 if (w->get_availability () == AVAIL_INTERPOSABLE)
1244 worse_state (&pure_const_state, &looping,
1245 w_l->state_previously_known,
1246 w_l->looping_previously_known);
1247 if (dump_file && (dump_flags & TDF_DETAILS))
1249 fprintf (dump_file,
1250 " Overwritable. state %s looping %i\n",
1251 pure_const_names[w_l->state_previously_known],
1252 w_l->looping_previously_known);
1254 break;
1257 count++;
1259 /* We consider recursive cycles as possibly infinite.
1260 This might be relaxed since infinite recursion leads to stack
1261 overflow. */
1262 if (count > 1)
1263 looping = true;
1265 /* Now walk the edges and merge in callee properties. */
1266 for (e = w->callees; e; e = e->next_callee)
1268 enum availability avail;
1269 struct cgraph_node *y = e->callee->
1270 function_or_virtual_thunk_symbol (&avail);
1271 enum pure_const_state_e edge_state = IPA_CONST;
1272 bool edge_looping = false;
1274 if (dump_file && (dump_flags & TDF_DETAILS))
1276 fprintf (dump_file,
1277 " Call to %s/%i",
1278 e->callee->name (),
1279 e->callee->order);
1281 if (avail > AVAIL_INTERPOSABLE)
1283 funct_state y_l = get_function_state (y);
1284 if (dump_file && (dump_flags & TDF_DETAILS))
1286 fprintf (dump_file,
1287 " state:%s looping:%i\n",
1288 pure_const_names[y_l->pure_const_state],
1289 y_l->looping);
1291 if (y_l->pure_const_state > IPA_PURE
1292 && e->cannot_lead_to_return_p ())
1294 if (dump_file && (dump_flags & TDF_DETAILS))
1295 fprintf (dump_file,
1296 " Ignoring side effects"
1297 " -> pure, looping\n");
1298 edge_state = IPA_PURE;
1299 edge_looping = true;
1301 else
1303 edge_state = y_l->pure_const_state;
1304 edge_looping = y_l->looping;
1307 else if (special_builtin_state (&edge_state, &edge_looping,
1308 y->decl))
1310 else
1311 state_from_flags (&edge_state, &edge_looping,
1312 flags_from_decl_or_type (y->decl),
1313 e->cannot_lead_to_return_p ());
1315 /* Merge the results with what we already know. */
1316 better_state (&edge_state, &edge_looping,
1317 w_l->state_previously_known,
1318 w_l->looping_previously_known);
1319 worse_state (&pure_const_state, &looping,
1320 edge_state, edge_looping);
1321 if (pure_const_state == IPA_NEITHER)
1322 break;
1324 if (pure_const_state == IPA_NEITHER)
1325 break;
1327 /* Now process the indirect call. */
1328 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1330 enum pure_const_state_e edge_state = IPA_CONST;
1331 bool edge_looping = false;
1333 if (dump_file && (dump_flags & TDF_DETAILS))
1334 fprintf (dump_file, " Indirect call");
1335 state_from_flags (&edge_state, &edge_looping,
1336 ie->indirect_info->ecf_flags,
1337 ie->cannot_lead_to_return_p ());
1338 /* Merge the results with what we already know. */
1339 better_state (&edge_state, &edge_looping,
1340 w_l->state_previously_known,
1341 w_l->looping_previously_known);
1342 worse_state (&pure_const_state, &looping,
1343 edge_state, edge_looping);
1344 if (pure_const_state == IPA_NEITHER)
1345 break;
1347 if (pure_const_state == IPA_NEITHER)
1348 break;
1350 /* And finally all loads and stores. */
1351 for (i = 0; w->iterate_reference (i, ref); i++)
1353 enum pure_const_state_e ref_state = IPA_CONST;
1354 bool ref_looping = false;
1355 switch (ref->use)
1357 case IPA_REF_LOAD:
1358 /* readonly reads are safe. */
1359 if (TREE_READONLY (ref->referred->decl))
1360 break;
1361 if (dump_file && (dump_flags & TDF_DETAILS))
1362 fprintf (dump_file, " nonreadonly global var read\n");
1363 ref_state = IPA_PURE;
1364 break;
1365 case IPA_REF_STORE:
1366 if (ref->cannot_lead_to_return ())
1367 break;
1368 ref_state = IPA_NEITHER;
1369 if (dump_file && (dump_flags & TDF_DETAILS))
1370 fprintf (dump_file, " global var write\n");
1371 break;
1372 case IPA_REF_ADDR:
1373 case IPA_REF_CHKP:
1374 break;
1375 default:
1376 gcc_unreachable ();
1378 better_state (&ref_state, &ref_looping,
1379 w_l->state_previously_known,
1380 w_l->looping_previously_known);
1381 worse_state (&pure_const_state, &looping,
1382 ref_state, ref_looping);
1383 if (pure_const_state == IPA_NEITHER)
1384 break;
1386 w_info = (struct ipa_dfs_info *) w->aux;
1387 w = w_info->next_cycle;
1389 if (dump_file && (dump_flags & TDF_DETAILS))
1390 fprintf (dump_file, "Result %s looping %i\n",
1391 pure_const_names [pure_const_state],
1392 looping);
1394 /* Find the worst state of can_free for any node in the cycle. */
1395 bool can_free = false;
1396 w = node;
1397 while (w && !can_free)
1399 struct cgraph_edge *e;
1400 funct_state w_l = get_function_state (w);
1402 if (w_l->can_free
1403 || w->get_availability () == AVAIL_INTERPOSABLE
1404 || w->indirect_calls)
1405 can_free = true;
1407 for (e = w->callees; e && !can_free; e = e->next_callee)
1409 enum availability avail;
1410 struct cgraph_node *y = e->callee->
1411 function_or_virtual_thunk_symbol (&avail);
1413 if (avail > AVAIL_INTERPOSABLE)
1414 can_free = get_function_state (y)->can_free;
1415 else
1416 can_free = true;
1418 w_info = (struct ipa_dfs_info *) w->aux;
1419 w = w_info->next_cycle;
1422 /* Copy back the region's pure_const_state which is shared by
1423 all nodes in the region. */
1424 w = node;
1425 while (w)
1427 funct_state w_l = get_function_state (w);
1428 enum pure_const_state_e this_state = pure_const_state;
1429 bool this_looping = looping;
1431 w_l->can_free = can_free;
1432 w->nonfreeing_fn = !can_free;
1433 if (!can_free && dump_file)
1434 fprintf (dump_file, "Function found not to call free: %s\n",
1435 w->name ());
1437 if (w_l->state_previously_known != IPA_NEITHER
1438 && this_state > w_l->state_previously_known)
1440 this_state = w_l->state_previously_known;
1441 this_looping |= w_l->looping_previously_known;
1443 if (!this_looping && self_recursive_p (w))
1444 this_looping = true;
1445 if (!w_l->looping_previously_known)
1446 this_looping = false;
1448 /* All nodes within a cycle share the same info. */
1449 w_l->pure_const_state = this_state;
1450 w_l->looping = this_looping;
1452 /* Inline clones share declaration with their offline copies;
1453 do not modify their declarations since the offline copy may
1454 be different. */
1455 if (!w->global.inlined_to)
1456 switch (this_state)
1458 case IPA_CONST:
1459 if (!TREE_READONLY (w->decl))
1461 warn_function_const (w->decl, !this_looping);
1462 if (dump_file)
1463 fprintf (dump_file, "Function found to be %sconst: %s\n",
1464 this_looping ? "looping " : "",
1465 w->name ());
1467 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1468 NULL, true);
1469 w->set_const_flag (true, this_looping);
1470 break;
1472 case IPA_PURE:
1473 if (!DECL_PURE_P (w->decl))
1475 warn_function_pure (w->decl, !this_looping);
1476 if (dump_file)
1477 fprintf (dump_file, "Function found to be %spure: %s\n",
1478 this_looping ? "looping " : "",
1479 w->name ());
1481 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1482 NULL, true);
1483 w->set_pure_flag (true, this_looping);
1484 break;
1486 default:
1487 break;
1489 w_info = (struct ipa_dfs_info *) w->aux;
1490 w = w_info->next_cycle;
1494 ipa_free_postorder_info ();
1495 free (order);
1496 return remove_p;
1499 /* Produce transitive closure over the callgraph and compute nothrow
1500 attributes. */
1502 static void
1503 propagate_nothrow (void)
1505 struct cgraph_node *node;
1506 struct cgraph_node *w;
1507 struct cgraph_node **order =
1508 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1509 int order_pos;
1510 int i;
1511 struct ipa_dfs_info * w_info;
1513 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1514 if (dump_file)
1516 cgraph_node::dump_cgraph (dump_file);
1517 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1520 /* Propagate the local information through the call graph to produce
1521 the global information. All the nodes within a cycle will have
1522 the same info so we collapse cycles first. Then we can do the
1523 propagation in one pass from the leaves to the roots. */
1524 for (i = 0; i < order_pos; i++ )
1526 bool can_throw = false;
1527 node = order[i];
1529 if (node->alias)
1530 continue;
1532 /* Find the worst state for any node in the cycle. */
1533 w = node;
1534 while (w && !can_throw)
1536 struct cgraph_edge *e, *ie;
1537 funct_state w_l = get_function_state (w);
1539 if (w_l->can_throw
1540 || w->get_availability () == AVAIL_INTERPOSABLE)
1541 can_throw = true;
1543 for (e = w->callees; e && !can_throw; e = e->next_callee)
1545 enum availability avail;
1546 struct cgraph_node *y = e->callee->
1547 function_or_virtual_thunk_symbol (&avail);
1549 if (avail > AVAIL_INTERPOSABLE)
1551 funct_state y_l = get_function_state (y);
1553 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1554 && e->can_throw_external)
1555 can_throw = true;
1557 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1558 can_throw = true;
1560 for (ie = w->indirect_calls; ie && !can_throw; ie = ie->next_callee)
1561 if (ie->can_throw_external)
1562 can_throw = true;
1563 w_info = (struct ipa_dfs_info *) w->aux;
1564 w = w_info->next_cycle;
1567 /* Copy back the region's pure_const_state which is shared by
1568 all nodes in the region. */
1569 w = node;
1570 while (w)
1572 funct_state w_l = get_function_state (w);
1573 if (!can_throw && !TREE_NOTHROW (w->decl))
1575 /* Inline clones share declaration with their offline copies;
1576 do not modify their declarations since the offline copy may
1577 be different. */
1578 if (!w->global.inlined_to)
1580 w->set_nothrow_flag (true);
1581 if (dump_file)
1582 fprintf (dump_file, "Function found to be nothrow: %s\n",
1583 w->name ());
1586 else if (can_throw && !TREE_NOTHROW (w->decl))
1587 w_l->can_throw = true;
1588 w_info = (struct ipa_dfs_info *) w->aux;
1589 w = w_info->next_cycle;
1593 ipa_free_postorder_info ();
1594 free (order);
1598 /* Produce the global information by preforming a transitive closure
1599 on the local information that was produced by generate_summary. */
1601 unsigned int
1602 pass_ipa_pure_const::
1603 execute (function *)
1605 struct cgraph_node *node;
1606 bool remove_p;
1608 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1609 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1610 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1612 /* Nothrow makes more function to not lead to return and improve
1613 later analysis. */
1614 propagate_nothrow ();
1615 remove_p = propagate_pure_const ();
1617 /* Cleanup. */
1618 FOR_EACH_FUNCTION (node)
1619 if (has_function_state (node))
1620 free (get_function_state (node));
1621 funct_state_vec.release ();
1622 return remove_p ? TODO_remove_functions : 0;
1625 static bool
1626 gate_pure_const (void)
1628 return flag_ipa_pure_const || in_lto_p;
1631 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1632 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1633 pure_const_generate_summary, /* generate_summary */
1634 pure_const_write_summary, /* write_summary */
1635 pure_const_read_summary, /* read_summary */
1636 NULL, /* write_optimization_summary */
1637 NULL, /* read_optimization_summary */
1638 NULL, /* stmt_fixup */
1639 0, /* function_transform_todo_flags_start */
1640 NULL, /* function_transform */
1641 NULL), /* variable_transform */
1642 init_p(false),
1643 function_insertion_hook_holder(NULL),
1644 node_duplication_hook_holder(NULL),
1645 node_removal_hook_holder(NULL)
1649 ipa_opt_pass_d *
1650 make_pass_ipa_pure_const (gcc::context *ctxt)
1652 return new pass_ipa_pure_const (ctxt);
1655 /* Return true if function should be skipped for local pure const analysis. */
1657 static bool
1658 skip_function_for_local_pure_const (struct cgraph_node *node)
1660 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1661 we must not promote functions that are called by already processed functions. */
1663 if (function_called_by_processed_nodes_p ())
1665 if (dump_file)
1666 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1667 return true;
1669 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1671 if (dump_file)
1672 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1673 return true;
1675 return false;
1678 /* Simple local pass for pure const discovery reusing the analysis from
1679 ipa_pure_const. This pass is effective when executed together with
1680 other optimization passes in early optimization pass queue. */
1682 namespace {
1684 const pass_data pass_data_local_pure_const =
1686 GIMPLE_PASS, /* type */
1687 "local-pure-const", /* name */
1688 OPTGROUP_NONE, /* optinfo_flags */
1689 TV_IPA_PURE_CONST, /* tv_id */
1690 0, /* properties_required */
1691 0, /* properties_provided */
1692 0, /* properties_destroyed */
1693 0, /* todo_flags_start */
1694 0, /* todo_flags_finish */
1697 class pass_local_pure_const : public gimple_opt_pass
1699 public:
1700 pass_local_pure_const (gcc::context *ctxt)
1701 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1704 /* opt_pass methods: */
1705 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1706 virtual bool gate (function *) { return gate_pure_const (); }
1707 virtual unsigned int execute (function *);
1709 }; // class pass_local_pure_const
1711 unsigned int
1712 pass_local_pure_const::execute (function *fun)
1714 bool changed = false;
1715 funct_state l;
1716 bool skip;
1717 struct cgraph_node *node;
1719 node = cgraph_node::get (current_function_decl);
1720 skip = skip_function_for_local_pure_const (node);
1721 if (!warn_suggest_attribute_const
1722 && !warn_suggest_attribute_pure
1723 && skip)
1724 return 0;
1726 l = analyze_function (node, false);
1728 /* Do NORETURN discovery. */
1729 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1730 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1732 warn_function_noreturn (fun->decl);
1733 if (dump_file)
1734 fprintf (dump_file, "Function found to be noreturn: %s\n",
1735 current_function_name ());
1737 /* Update declaration and reduce profile to executed once. */
1738 TREE_THIS_VOLATILE (current_function_decl) = 1;
1739 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1740 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1742 changed = true;
1745 switch (l->pure_const_state)
1747 case IPA_CONST:
1748 if (!TREE_READONLY (current_function_decl))
1750 warn_function_const (current_function_decl, !l->looping);
1751 if (!skip)
1753 node->set_const_flag (true, l->looping);
1754 changed = true;
1756 if (dump_file)
1757 fprintf (dump_file, "Function found to be %sconst: %s\n",
1758 l->looping ? "looping " : "",
1759 current_function_name ());
1761 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1762 && !l->looping)
1764 if (!skip)
1766 node->set_const_flag (true, false);
1767 changed = true;
1769 if (dump_file)
1770 fprintf (dump_file, "Function found to be non-looping: %s\n",
1771 current_function_name ());
1773 break;
1775 case IPA_PURE:
1776 if (!DECL_PURE_P (current_function_decl))
1778 if (!skip)
1780 node->set_pure_flag (true, l->looping);
1781 changed = true;
1783 warn_function_pure (current_function_decl, !l->looping);
1784 if (dump_file)
1785 fprintf (dump_file, "Function found to be %spure: %s\n",
1786 l->looping ? "looping " : "",
1787 current_function_name ());
1789 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1790 && !l->looping)
1792 if (!skip)
1794 node->set_pure_flag (true, false);
1795 changed = true;
1797 if (dump_file)
1798 fprintf (dump_file, "Function found to be non-looping: %s\n",
1799 current_function_name ());
1801 break;
1803 default:
1804 break;
1806 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1808 node->set_nothrow_flag (true);
1809 changed = true;
1810 if (dump_file)
1811 fprintf (dump_file, "Function found to be nothrow: %s\n",
1812 current_function_name ());
1814 free (l);
1815 if (changed)
1816 return execute_fixup_cfg ();
1817 else
1818 return 0;
1821 } // anon namespace
1823 gimple_opt_pass *
1824 make_pass_local_pure_const (gcc::context *ctxt)
1826 return new pass_local_pure_const (ctxt);
1829 /* Emit noreturn warnings. */
1831 namespace {
1833 const pass_data pass_data_warn_function_noreturn =
1835 GIMPLE_PASS, /* type */
1836 "*warn_function_noreturn", /* name */
1837 OPTGROUP_NONE, /* optinfo_flags */
1838 TV_NONE, /* tv_id */
1839 PROP_cfg, /* properties_required */
1840 0, /* properties_provided */
1841 0, /* properties_destroyed */
1842 0, /* todo_flags_start */
1843 0, /* todo_flags_finish */
1846 class pass_warn_function_noreturn : public gimple_opt_pass
1848 public:
1849 pass_warn_function_noreturn (gcc::context *ctxt)
1850 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1853 /* opt_pass methods: */
1854 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1855 virtual unsigned int execute (function *fun)
1857 if (!TREE_THIS_VOLATILE (current_function_decl)
1858 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1859 warn_function_noreturn (current_function_decl);
1860 return 0;
1863 }; // class pass_warn_function_noreturn
1865 } // anon namespace
1867 gimple_opt_pass *
1868 make_pass_warn_function_noreturn (gcc::context *ctxt)
1870 return new pass_warn_function_noreturn (ctxt);
1873 /* Simple local pass for pure const discovery reusing the analysis from
1874 ipa_pure_const. This pass is effective when executed together with
1875 other optimization passes in early optimization pass queue. */
1877 namespace {
1879 const pass_data pass_data_nothrow =
1881 GIMPLE_PASS, /* type */
1882 "nothrow", /* name */
1883 OPTGROUP_NONE, /* optinfo_flags */
1884 TV_IPA_PURE_CONST, /* tv_id */
1885 0, /* properties_required */
1886 0, /* properties_provided */
1887 0, /* properties_destroyed */
1888 0, /* todo_flags_start */
1889 0, /* todo_flags_finish */
1892 class pass_nothrow : public gimple_opt_pass
1894 public:
1895 pass_nothrow (gcc::context *ctxt)
1896 : gimple_opt_pass (pass_data_nothrow, ctxt)
1899 /* opt_pass methods: */
1900 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1901 virtual bool gate (function *) { return optimize; }
1902 virtual unsigned int execute (function *);
1904 }; // class pass_nothrow
1906 unsigned int
1907 pass_nothrow::execute (function *)
1909 struct cgraph_node *node;
1910 basic_block this_block;
1912 if (TREE_NOTHROW (current_function_decl))
1913 return 0;
1915 node = cgraph_node::get (current_function_decl);
1917 /* We run during lowering, we can not really use availability yet. */
1918 if (cgraph_node::get (current_function_decl)->get_availability ()
1919 <= AVAIL_INTERPOSABLE)
1921 if (dump_file)
1922 fprintf (dump_file, "Function is interposable;"
1923 " not analyzing.\n");
1924 return true;
1927 FOR_EACH_BB_FN (this_block, cfun)
1929 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1930 !gsi_end_p (gsi);
1931 gsi_next (&gsi))
1932 if (stmt_can_throw_external (gsi_stmt (gsi)))
1934 if (is_gimple_call (gsi_stmt (gsi)))
1936 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
1937 if (callee_t && recursive_call_p (current_function_decl,
1938 callee_t))
1939 continue;
1942 if (dump_file)
1944 fprintf (dump_file, "Statement can throw: ");
1945 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1947 return 0;
1951 node->set_nothrow_flag (true);
1952 if (dump_file)
1953 fprintf (dump_file, "Function found to be nothrow: %s\n",
1954 current_function_name ());
1955 return 0;
1958 } // anon namespace
1960 gimple_opt_pass *
1961 make_pass_nothrow (gcc::context *ctxt)
1963 return new pass_nothrow (ctxt);