* tree-loop-distribution.c (bb_top_order_index): New.
[official-gcc.git] / gcc / ipa-pure-const.c
blobdac8f0d5f214425a0d32cedc75167b0483797c99
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2017 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 "backend.h"
38 #include "target.h"
39 #include "tree.h"
40 #include "gimple.h"
41 #include "tree-pass.h"
42 #include "tree-streamer.h"
43 #include "cgraph.h"
44 #include "diagnostic.h"
45 #include "calls.h"
46 #include "cfganal.h"
47 #include "tree-eh.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "tree-cfg.h"
51 #include "tree-ssa-loop-niter.h"
52 #include "langhooks.h"
53 #include "ipa-utils.h"
54 #include "gimple-pretty-print.h"
55 #include "cfgloop.h"
56 #include "tree-scalar-evolution.h"
57 #include "intl.h"
58 #include "opts.h"
60 /* Lattice values for const and pure functions. Everything starts out
61 being const, then may drop to pure and then neither depending on
62 what is found. */
63 enum pure_const_state_e
65 IPA_CONST,
66 IPA_PURE,
67 IPA_NEITHER
70 const char *pure_const_names[3] = {"const", "pure", "neither"};
72 /* Holder for the const_state. There is one of these per function
73 decl. */
74 struct funct_state_d
76 /* See above. */
77 enum pure_const_state_e pure_const_state;
78 /* What user set here; we can be always sure about this. */
79 enum pure_const_state_e state_previously_known;
80 bool looping_previously_known;
82 /* True if the function could possibly infinite loop. There are a
83 lot of ways that this could be determined. We are pretty
84 conservative here. While it is possible to cse pure and const
85 calls, it is not legal to have dce get rid of the call if there
86 is a possibility that the call could infinite loop since this is
87 a behavioral change. */
88 bool looping;
90 bool can_throw;
92 /* If function can call free, munmap or otherwise make previously
93 non-trapping memory accesses trapping. */
94 bool can_free;
97 /* State used when we know nothing about function. */
98 static struct funct_state_d varying_state
99 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
102 typedef struct funct_state_d * funct_state;
104 /* The storage of the funct_state is abstracted because there is the
105 possibility that it may be desirable to move this to the cgraph
106 local info. */
108 /* Array, indexed by cgraph node uid, of function states. */
110 static vec<funct_state> funct_state_vec;
112 static bool gate_pure_const (void);
114 namespace {
116 const pass_data pass_data_ipa_pure_const =
118 IPA_PASS, /* type */
119 "pure-const", /* name */
120 OPTGROUP_NONE, /* optinfo_flags */
121 TV_IPA_PURE_CONST, /* tv_id */
122 0, /* properties_required */
123 0, /* properties_provided */
124 0, /* properties_destroyed */
125 0, /* todo_flags_start */
126 0, /* todo_flags_finish */
129 class pass_ipa_pure_const : public ipa_opt_pass_d
131 public:
132 pass_ipa_pure_const(gcc::context *ctxt);
134 /* opt_pass methods: */
135 bool gate (function *) { return gate_pure_const (); }
136 unsigned int execute (function *fun);
138 void register_hooks (void);
140 private:
141 bool init_p;
143 /* Holders of ipa cgraph hooks: */
144 struct cgraph_node_hook_list *function_insertion_hook_holder;
145 struct cgraph_2node_hook_list *node_duplication_hook_holder;
146 struct cgraph_node_hook_list *node_removal_hook_holder;
148 }; // class pass_ipa_pure_const
150 } // anon namespace
152 /* Try to guess if function body will always be visible to compiler
153 when compiling the call and whether compiler will be able
154 to propagate the information by itself. */
156 static bool
157 function_always_visible_to_compiler_p (tree decl)
159 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
162 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
163 is true if the function is known to be finite. The diagnostic is
164 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
165 OPTION, this function may initialize it and it is always returned
166 by the function. */
168 static hash_set<tree> *
169 suggest_attribute (int option, tree decl, bool known_finite,
170 hash_set<tree> *warned_about,
171 const char * attrib_name)
173 if (!option_enabled (option, &global_options))
174 return warned_about;
175 if (TREE_THIS_VOLATILE (decl)
176 || (known_finite && function_always_visible_to_compiler_p (decl)))
177 return warned_about;
179 if (!warned_about)
180 warned_about = new hash_set<tree>;
181 if (warned_about->contains (decl))
182 return warned_about;
183 warned_about->add (decl);
184 warning_at (DECL_SOURCE_LOCATION (decl),
185 option,
186 known_finite
187 ? G_("function might be candidate for attribute %qs")
188 : G_("function might be candidate for attribute %qs"
189 " if it is known to return normally"), attrib_name);
190 return warned_about;
193 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
194 is true if the function is known to be finite. */
196 static void
197 warn_function_pure (tree decl, bool known_finite)
199 static hash_set<tree> *warned_about;
201 warned_about
202 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
203 known_finite, warned_about, "pure");
206 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
207 is true if the function is known to be finite. */
209 static void
210 warn_function_const (tree decl, bool known_finite)
212 static hash_set<tree> *warned_about;
213 warned_about
214 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
215 known_finite, warned_about, "const");
218 static void
219 warn_function_noreturn (tree decl)
221 tree original_decl = decl;
223 cgraph_node *node = cgraph_node::get (decl);
224 if (node->instrumentation_clone)
225 decl = node->instrumented_version->decl;
227 static hash_set<tree> *warned_about;
228 if (!lang_hooks.missing_noreturn_ok_p (decl)
229 && targetm.warn_func_return (decl))
230 warned_about
231 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, original_decl,
232 true, warned_about, "noreturn");
235 /* Return true if we have a function state for NODE. */
237 static inline bool
238 has_function_state (struct cgraph_node *node)
240 if (!funct_state_vec.exists ()
241 || funct_state_vec.length () <= (unsigned int)node->uid)
242 return false;
243 return funct_state_vec[node->uid] != NULL;
246 /* Return the function state from NODE. */
248 static inline funct_state
249 get_function_state (struct cgraph_node *node)
251 if (!funct_state_vec.exists ()
252 || funct_state_vec.length () <= (unsigned int)node->uid
253 || !funct_state_vec[node->uid])
254 /* We might want to put correct previously_known state into varying. */
255 return &varying_state;
256 return funct_state_vec[node->uid];
259 /* Set the function state S for NODE. */
261 static inline void
262 set_function_state (struct cgraph_node *node, funct_state s)
264 if (!funct_state_vec.exists ()
265 || funct_state_vec.length () <= (unsigned int)node->uid)
266 funct_state_vec.safe_grow_cleared (node->uid + 1);
268 /* If funct_state_vec already contains a funct_state, we have to release
269 it before it's going to be ovewritten. */
270 if (funct_state_vec[node->uid] != NULL
271 && funct_state_vec[node->uid] != &varying_state)
272 free (funct_state_vec[node->uid]);
274 funct_state_vec[node->uid] = s;
277 /* Check to see if the use (or definition when CHECKING_WRITE is true)
278 variable T is legal in a function that is either pure or const. */
280 static inline void
281 check_decl (funct_state local,
282 tree t, bool checking_write, bool ipa)
284 /* Do not want to do anything with volatile except mark any
285 function that uses one to be not const or pure. */
286 if (TREE_THIS_VOLATILE (t))
288 local->pure_const_state = IPA_NEITHER;
289 if (dump_file)
290 fprintf (dump_file, " Volatile operand is not const/pure");
291 return;
294 /* Do not care about a local automatic that is not static. */
295 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
296 return;
298 /* If the variable has the "used" attribute, treat it as if it had a
299 been touched by the devil. */
300 if (DECL_PRESERVE_P (t))
302 local->pure_const_state = IPA_NEITHER;
303 if (dump_file)
304 fprintf (dump_file, " Used static/global variable is not const/pure\n");
305 return;
308 /* In IPA mode we are not interested in checking actual loads and stores;
309 they will be processed at propagation time using ipa_ref. */
310 if (ipa)
311 return;
313 /* Since we have dealt with the locals and params cases above, if we
314 are CHECKING_WRITE, this cannot be a pure or constant
315 function. */
316 if (checking_write)
318 local->pure_const_state = IPA_NEITHER;
319 if (dump_file)
320 fprintf (dump_file, " static/global memory write is not const/pure\n");
321 return;
324 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
326 /* Readonly reads are safe. */
327 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
328 return; /* Read of a constant, do not change the function state. */
329 else
331 if (dump_file)
332 fprintf (dump_file, " global memory read is not const\n");
333 /* Just a regular read. */
334 if (local->pure_const_state == IPA_CONST)
335 local->pure_const_state = IPA_PURE;
338 else
340 /* Compilation level statics can be read if they are readonly
341 variables. */
342 if (TREE_READONLY (t))
343 return;
345 if (dump_file)
346 fprintf (dump_file, " static memory read is not const\n");
347 /* Just a regular read. */
348 if (local->pure_const_state == IPA_CONST)
349 local->pure_const_state = IPA_PURE;
354 /* Check to see if the use (or definition when CHECKING_WRITE is true)
355 variable T is legal in a function that is either pure or const. */
357 static inline void
358 check_op (funct_state local, tree t, bool checking_write)
360 t = get_base_address (t);
361 if (t && TREE_THIS_VOLATILE (t))
363 local->pure_const_state = IPA_NEITHER;
364 if (dump_file)
365 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
366 return;
368 else if (t
369 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
370 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
371 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
373 if (dump_file)
374 fprintf (dump_file, " Indirect ref to local memory is OK\n");
375 return;
377 else if (checking_write)
379 local->pure_const_state = IPA_NEITHER;
380 if (dump_file)
381 fprintf (dump_file, " Indirect ref write is not const/pure\n");
382 return;
384 else
386 if (dump_file)
387 fprintf (dump_file, " Indirect ref read is not const\n");
388 if (local->pure_const_state == IPA_CONST)
389 local->pure_const_state = IPA_PURE;
393 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
395 static void
396 state_from_flags (enum pure_const_state_e *state, bool *looping,
397 int flags, bool cannot_lead_to_return)
399 *looping = false;
400 if (flags & ECF_LOOPING_CONST_OR_PURE)
402 *looping = true;
403 if (dump_file && (dump_flags & TDF_DETAILS))
404 fprintf (dump_file, " looping");
406 if (flags & ECF_CONST)
408 *state = IPA_CONST;
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, " const\n");
412 else if (flags & ECF_PURE)
414 *state = IPA_PURE;
415 if (dump_file && (dump_flags & TDF_DETAILS))
416 fprintf (dump_file, " pure\n");
418 else if (cannot_lead_to_return)
420 *state = IPA_PURE;
421 *looping = true;
422 if (dump_file && (dump_flags & TDF_DETAILS))
423 fprintf (dump_file, " ignoring side effects->pure looping\n");
425 else
427 if (dump_file && (dump_flags & TDF_DETAILS))
428 fprintf (dump_file, " neither\n");
429 *state = IPA_NEITHER;
430 *looping = true;
434 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
435 into STATE and LOOPING better of the two variants.
436 Be sure to merge looping correctly. IPA_NEITHER functions
437 have looping 0 even if they don't have to return. */
439 static inline void
440 better_state (enum pure_const_state_e *state, bool *looping,
441 enum pure_const_state_e state2, bool looping2)
443 if (state2 < *state)
445 if (*state == IPA_NEITHER)
446 *looping = looping2;
447 else
448 *looping = MIN (*looping, looping2);
449 *state = state2;
451 else if (state2 != IPA_NEITHER)
452 *looping = MIN (*looping, looping2);
455 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
456 into STATE and LOOPING worse of the two variants.
457 N is the actual node called. */
459 static inline void
460 worse_state (enum pure_const_state_e *state, bool *looping,
461 enum pure_const_state_e state2, bool looping2,
462 struct symtab_node *from,
463 struct symtab_node *to)
465 /* Consider function:
467 bool a(int *p)
469 return *p==*p;
472 During early optimization we will turn this into:
474 bool a(int *p)
476 return true;
479 Now if this function will be detected as CONST however when interposed it
480 may end up being just pure. We always must assume the worst scenario here.
482 if (*state == IPA_CONST && state2 == IPA_CONST
483 && to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from))
485 if (dump_file && (dump_flags & TDF_DETAILS))
486 fprintf (dump_file, "Dropping state to PURE because call to %s may not "
487 "bind to current def.\n", to->name ());
488 state2 = IPA_PURE;
490 *state = MAX (*state, state2);
491 *looping = MAX (*looping, looping2);
494 /* Recognize special cases of builtins that are by themselves not pure or const
495 but function using them is. */
496 static bool
497 special_builtin_state (enum pure_const_state_e *state, bool *looping,
498 tree callee)
500 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
501 switch (DECL_FUNCTION_CODE (callee))
503 case BUILT_IN_RETURN:
504 case BUILT_IN_UNREACHABLE:
505 case BUILT_IN_ALLOCA:
506 case BUILT_IN_ALLOCA_WITH_ALIGN:
507 case BUILT_IN_STACK_SAVE:
508 case BUILT_IN_STACK_RESTORE:
509 case BUILT_IN_EH_POINTER:
510 case BUILT_IN_EH_FILTER:
511 case BUILT_IN_UNWIND_RESUME:
512 case BUILT_IN_CXA_END_CLEANUP:
513 case BUILT_IN_EH_COPY_VALUES:
514 case BUILT_IN_FRAME_ADDRESS:
515 case BUILT_IN_APPLY:
516 case BUILT_IN_APPLY_ARGS:
517 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
518 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
519 *looping = false;
520 *state = IPA_CONST;
521 return true;
522 case BUILT_IN_PREFETCH:
523 *looping = true;
524 *state = IPA_CONST;
525 return true;
526 default:
527 break;
529 return false;
532 /* Check the parameters of a function call to CALL_EXPR to see if
533 there are any references in the parameters that are not allowed for
534 pure or const functions. Also check to see if this is either an
535 indirect call, a call outside the compilation unit, or has special
536 attributes that may also effect the purity. The CALL_EXPR node for
537 the entire call expression. */
539 static void
540 check_call (funct_state local, gcall *call, bool ipa)
542 int flags = gimple_call_flags (call);
543 tree callee_t = gimple_call_fndecl (call);
544 bool possibly_throws = stmt_could_throw_p (call);
545 bool possibly_throws_externally = (possibly_throws
546 && stmt_can_throw_external (call));
548 if (possibly_throws)
550 unsigned int i;
551 for (i = 0; i < gimple_num_ops (call); i++)
552 if (gimple_op (call, i)
553 && tree_could_throw_p (gimple_op (call, i)))
555 if (possibly_throws && cfun->can_throw_non_call_exceptions)
557 if (dump_file)
558 fprintf (dump_file, " operand can throw; looping\n");
559 local->looping = true;
561 if (possibly_throws_externally)
563 if (dump_file)
564 fprintf (dump_file, " operand can throw externally\n");
565 local->can_throw = true;
570 /* The const and pure flags are set by a variety of places in the
571 compiler (including here). If someone has already set the flags
572 for the callee, (such as for some of the builtins) we will use
573 them, otherwise we will compute our own information.
575 Const and pure functions have less clobber effects than other
576 functions so we process these first. Otherwise if it is a call
577 outside the compilation unit or an indirect call we punt. This
578 leaves local calls which will be processed by following the call
579 graph. */
580 if (callee_t)
582 enum pure_const_state_e call_state;
583 bool call_looping;
585 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
586 && !nonfreeing_call_p (call))
587 local->can_free = true;
589 if (special_builtin_state (&call_state, &call_looping, callee_t))
591 worse_state (&local->pure_const_state, &local->looping,
592 call_state, call_looping,
593 NULL, NULL);
594 return;
596 /* When bad things happen to bad functions, they cannot be const
597 or pure. */
598 if (setjmp_call_p (callee_t))
600 if (dump_file)
601 fprintf (dump_file, " setjmp is not const/pure\n");
602 local->looping = true;
603 local->pure_const_state = IPA_NEITHER;
606 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
607 switch (DECL_FUNCTION_CODE (callee_t))
609 case BUILT_IN_LONGJMP:
610 case BUILT_IN_NONLOCAL_GOTO:
611 if (dump_file)
612 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
613 local->pure_const_state = IPA_NEITHER;
614 local->looping = true;
615 break;
616 default:
617 break;
620 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
621 local->can_free = true;
623 /* When not in IPA mode, we can still handle self recursion. */
624 if (!ipa && callee_t
625 && recursive_call_p (current_function_decl, callee_t))
627 if (dump_file)
628 fprintf (dump_file, " Recursive call can loop.\n");
629 local->looping = true;
631 /* Either callee is unknown or we are doing local analysis.
632 Look to see if there are any bits available for the callee (such as by
633 declaration or because it is builtin) and process solely on the basis of
634 those bits. Handle internal calls always, those calls don't have
635 corresponding cgraph edges and thus aren't processed during
636 the propagation. */
637 else if (!ipa || gimple_call_internal_p (call))
639 enum pure_const_state_e call_state;
640 bool call_looping;
641 if (possibly_throws && cfun->can_throw_non_call_exceptions)
643 if (dump_file)
644 fprintf (dump_file, " can throw; looping\n");
645 local->looping = true;
647 if (possibly_throws_externally)
649 if (dump_file)
651 fprintf (dump_file, " can throw externally to lp %i\n",
652 lookup_stmt_eh_lp (call));
653 if (callee_t)
654 fprintf (dump_file, " callee:%s\n",
655 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
657 local->can_throw = true;
659 if (dump_file && (dump_flags & TDF_DETAILS))
660 fprintf (dump_file, " checking flags for call:");
661 state_from_flags (&call_state, &call_looping, flags,
662 ((flags & (ECF_NORETURN | ECF_NOTHROW))
663 == (ECF_NORETURN | ECF_NOTHROW))
664 || (!flag_exceptions && (flags & ECF_NORETURN)));
665 worse_state (&local->pure_const_state, &local->looping,
666 call_state, call_looping, NULL, NULL);
668 /* Direct functions calls are handled by IPA propagation. */
671 /* Wrapper around check_decl for loads in local more. */
673 static bool
674 check_load (gimple *, tree op, tree, void *data)
676 if (DECL_P (op))
677 check_decl ((funct_state)data, op, false, false);
678 else
679 check_op ((funct_state)data, op, false);
680 return false;
683 /* Wrapper around check_decl for stores in local more. */
685 static bool
686 check_store (gimple *, tree op, tree, void *data)
688 if (DECL_P (op))
689 check_decl ((funct_state)data, op, true, false);
690 else
691 check_op ((funct_state)data, op, true);
692 return false;
695 /* Wrapper around check_decl for loads in ipa mode. */
697 static bool
698 check_ipa_load (gimple *, tree op, tree, void *data)
700 if (DECL_P (op))
701 check_decl ((funct_state)data, op, false, true);
702 else
703 check_op ((funct_state)data, op, false);
704 return false;
707 /* Wrapper around check_decl for stores in ipa mode. */
709 static bool
710 check_ipa_store (gimple *, tree op, tree, void *data)
712 if (DECL_P (op))
713 check_decl ((funct_state)data, op, true, true);
714 else
715 check_op ((funct_state)data, op, true);
716 return false;
719 /* Look into pointer pointed to by GSIP and figure out what interesting side
720 effects it has. */
721 static void
722 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
724 gimple *stmt = gsi_stmt (*gsip);
726 if (is_gimple_debug (stmt))
727 return;
729 /* Do consider clobber as side effects before IPA, so we rather inline
730 C++ destructors and keep clobber semantics than eliminate them.
732 TODO: We may get smarter during early optimizations on these and let
733 functions containing only clobbers to be optimized more. This is a common
734 case of C++ destructors. */
736 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
737 return;
739 if (dump_file)
741 fprintf (dump_file, " scanning: ");
742 print_gimple_stmt (dump_file, stmt, 0);
745 if (gimple_has_volatile_ops (stmt)
746 && !gimple_clobber_p (stmt))
748 local->pure_const_state = IPA_NEITHER;
749 if (dump_file)
750 fprintf (dump_file, " Volatile stmt is not const/pure\n");
753 /* Look for loads and stores. */
754 walk_stmt_load_store_ops (stmt, local,
755 ipa ? check_ipa_load : check_load,
756 ipa ? check_ipa_store : check_store);
758 if (gimple_code (stmt) != GIMPLE_CALL
759 && stmt_could_throw_p (stmt))
761 if (cfun->can_throw_non_call_exceptions)
763 if (dump_file)
764 fprintf (dump_file, " can throw; looping\n");
765 local->looping = true;
767 if (stmt_can_throw_external (stmt))
769 if (dump_file)
770 fprintf (dump_file, " can throw externally\n");
771 local->can_throw = true;
773 else
774 if (dump_file)
775 fprintf (dump_file, " can throw\n");
777 switch (gimple_code (stmt))
779 case GIMPLE_CALL:
780 check_call (local, as_a <gcall *> (stmt), ipa);
781 break;
782 case GIMPLE_LABEL:
783 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
784 /* Target of long jump. */
786 if (dump_file)
787 fprintf (dump_file, " nonlocal label is not const/pure\n");
788 local->pure_const_state = IPA_NEITHER;
790 break;
791 case GIMPLE_ASM:
792 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
794 if (dump_file)
795 fprintf (dump_file, " memory asm clobber is not const/pure\n");
796 /* Abandon all hope, ye who enter here. */
797 local->pure_const_state = IPA_NEITHER;
798 local->can_free = true;
800 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
802 if (dump_file)
803 fprintf (dump_file, " volatile is not const/pure\n");
804 /* Abandon all hope, ye who enter here. */
805 local->pure_const_state = IPA_NEITHER;
806 local->looping = true;
807 local->can_free = true;
809 return;
810 default:
811 break;
816 /* This is the main routine for finding the reference patterns for
817 global variables within a function FN. */
819 static funct_state
820 analyze_function (struct cgraph_node *fn, bool ipa)
822 tree decl = fn->decl;
823 funct_state l;
824 basic_block this_block;
826 l = XCNEW (struct funct_state_d);
827 l->pure_const_state = IPA_CONST;
828 l->state_previously_known = IPA_NEITHER;
829 l->looping_previously_known = true;
830 l->looping = false;
831 l->can_throw = false;
832 l->can_free = false;
833 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
834 flags_from_decl_or_type (fn->decl),
835 fn->cannot_return_p ());
837 if (fn->thunk.thunk_p || fn->alias)
839 /* Thunk gets propagated through, so nothing interesting happens. */
840 gcc_assert (ipa);
841 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
842 l->pure_const_state = IPA_NEITHER;
843 return l;
846 if (dump_file)
848 fprintf (dump_file, "\n\n local analysis of %s\n ",
849 fn->name ());
852 push_cfun (DECL_STRUCT_FUNCTION (decl));
854 FOR_EACH_BB_FN (this_block, cfun)
856 gimple_stmt_iterator gsi;
857 struct walk_stmt_info wi;
859 memset (&wi, 0, sizeof (wi));
860 for (gsi = gsi_start_bb (this_block);
861 !gsi_end_p (gsi);
862 gsi_next (&gsi))
864 check_stmt (&gsi, l, ipa);
865 if (l->pure_const_state == IPA_NEITHER
866 && l->looping
867 && l->can_throw
868 && l->can_free)
869 goto end;
873 end:
874 if (l->pure_const_state != IPA_NEITHER)
876 /* Const functions cannot have back edges (an
877 indication of possible infinite loop side
878 effect. */
879 if (mark_dfs_back_edges ())
881 /* Preheaders are needed for SCEV to work.
882 Simple latches and recorded exits improve chances that loop will
883 proved to be finite in testcases such as in loop-15.c
884 and loop-24.c */
885 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
886 | LOOPS_HAVE_SIMPLE_LATCHES
887 | LOOPS_HAVE_RECORDED_EXITS);
888 if (dump_file && (dump_flags & TDF_DETAILS))
889 flow_loops_dump (dump_file, NULL, 0);
890 if (mark_irreducible_loops ())
892 if (dump_file)
893 fprintf (dump_file, " has irreducible loops\n");
894 l->looping = true;
896 else
898 struct loop *loop;
899 scev_initialize ();
900 FOR_EACH_LOOP (loop, 0)
901 if (!finite_loop_p (loop))
903 if (dump_file)
904 fprintf (dump_file, " can not prove finiteness of "
905 "loop %i\n", loop->num);
906 l->looping =true;
907 break;
909 scev_finalize ();
911 loop_optimizer_finalize ();
915 if (dump_file && (dump_flags & TDF_DETAILS))
916 fprintf (dump_file, " checking previously known:");
918 better_state (&l->pure_const_state, &l->looping,
919 l->state_previously_known,
920 l->looping_previously_known);
921 if (TREE_NOTHROW (decl))
922 l->can_throw = false;
924 pop_cfun ();
925 if (dump_file)
927 if (l->looping)
928 fprintf (dump_file, "Function is locally looping.\n");
929 if (l->can_throw)
930 fprintf (dump_file, "Function is locally throwing.\n");
931 if (l->pure_const_state == IPA_CONST)
932 fprintf (dump_file, "Function is locally const.\n");
933 if (l->pure_const_state == IPA_PURE)
934 fprintf (dump_file, "Function is locally pure.\n");
935 if (l->can_free)
936 fprintf (dump_file, "Function can locally free.\n");
938 return l;
941 /* Called when new function is inserted to callgraph late. */
942 static void
943 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
945 /* There are some shared nodes, in particular the initializers on
946 static declarations. We do not need to scan them more than once
947 since all we would be interested in are the addressof
948 operations. */
949 if (opt_for_fn (node->decl, flag_ipa_pure_const))
950 set_function_state (node, analyze_function (node, true));
953 /* Called when new clone is inserted to callgraph late. */
955 static void
956 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
957 void *data ATTRIBUTE_UNUSED)
959 if (has_function_state (src))
961 funct_state l = XNEW (struct funct_state_d);
962 gcc_assert (!has_function_state (dst));
963 memcpy (l, get_function_state (src), sizeof (*l));
964 set_function_state (dst, l);
968 /* Called when new clone is inserted to callgraph late. */
970 static void
971 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
973 if (has_function_state (node))
974 set_function_state (node, NULL);
978 void
979 pass_ipa_pure_const::
980 register_hooks (void)
982 if (init_p)
983 return;
985 init_p = true;
987 node_removal_hook_holder =
988 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
989 node_duplication_hook_holder =
990 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
991 function_insertion_hook_holder =
992 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
996 /* Analyze each function in the cgraph to see if it is locally PURE or
997 CONST. */
999 static void
1000 pure_const_generate_summary (void)
1002 struct cgraph_node *node;
1004 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1005 pass->register_hooks ();
1007 /* Process all of the functions.
1009 We process AVAIL_INTERPOSABLE functions. We can not use the results
1010 by default, but the info can be used at LTO with -fwhole-program or
1011 when function got cloned and the clone is AVAILABLE. */
1013 FOR_EACH_DEFINED_FUNCTION (node)
1014 if (opt_for_fn (node->decl, flag_ipa_pure_const))
1015 set_function_state (node, analyze_function (node, true));
1019 /* Serialize the ipa info for lto. */
1021 static void
1022 pure_const_write_summary (void)
1024 struct cgraph_node *node;
1025 struct lto_simple_output_block *ob
1026 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1027 unsigned int count = 0;
1028 lto_symtab_encoder_iterator lsei;
1029 lto_symtab_encoder_t encoder;
1031 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1033 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1034 lsei_next_function_in_partition (&lsei))
1036 node = lsei_cgraph_node (lsei);
1037 if (node->definition && has_function_state (node))
1038 count++;
1041 streamer_write_uhwi_stream (ob->main_stream, count);
1043 /* Process all of the functions. */
1044 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1045 lsei_next_function_in_partition (&lsei))
1047 node = lsei_cgraph_node (lsei);
1048 if (node->definition && has_function_state (node))
1050 struct bitpack_d bp;
1051 funct_state fs;
1052 int node_ref;
1053 lto_symtab_encoder_t encoder;
1055 fs = get_function_state (node);
1057 encoder = ob->decl_state->symtab_node_encoder;
1058 node_ref = lto_symtab_encoder_encode (encoder, node);
1059 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1061 /* Note that flags will need to be read in the opposite
1062 order as we are pushing the bitflags into FLAGS. */
1063 bp = bitpack_create (ob->main_stream);
1064 bp_pack_value (&bp, fs->pure_const_state, 2);
1065 bp_pack_value (&bp, fs->state_previously_known, 2);
1066 bp_pack_value (&bp, fs->looping_previously_known, 1);
1067 bp_pack_value (&bp, fs->looping, 1);
1068 bp_pack_value (&bp, fs->can_throw, 1);
1069 bp_pack_value (&bp, fs->can_free, 1);
1070 streamer_write_bitpack (&bp);
1074 lto_destroy_simple_output_block (ob);
1078 /* Deserialize the ipa info for lto. */
1080 static void
1081 pure_const_read_summary (void)
1083 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1084 struct lto_file_decl_data *file_data;
1085 unsigned int j = 0;
1087 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1088 pass->register_hooks ();
1090 while ((file_data = file_data_vec[j++]))
1092 const char *data;
1093 size_t len;
1094 struct lto_input_block *ib
1095 = lto_create_simple_input_block (file_data,
1096 LTO_section_ipa_pure_const,
1097 &data, &len);
1098 if (ib)
1100 unsigned int i;
1101 unsigned int count = streamer_read_uhwi (ib);
1103 for (i = 0; i < count; i++)
1105 unsigned int index;
1106 struct cgraph_node *node;
1107 struct bitpack_d bp;
1108 funct_state fs;
1109 lto_symtab_encoder_t encoder;
1111 fs = XCNEW (struct funct_state_d);
1112 index = streamer_read_uhwi (ib);
1113 encoder = file_data->symtab_node_encoder;
1114 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1115 index));
1116 set_function_state (node, fs);
1118 /* Note that the flags must be read in the opposite
1119 order in which they were written (the bitflags were
1120 pushed into FLAGS). */
1121 bp = streamer_read_bitpack (ib);
1122 fs->pure_const_state
1123 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1124 fs->state_previously_known
1125 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1126 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1127 fs->looping = bp_unpack_value (&bp, 1);
1128 fs->can_throw = bp_unpack_value (&bp, 1);
1129 fs->can_free = bp_unpack_value (&bp, 1);
1130 if (dump_file)
1132 int flags = flags_from_decl_or_type (node->decl);
1133 fprintf (dump_file, "Read info for %s ", node->dump_name ());
1134 if (flags & ECF_CONST)
1135 fprintf (dump_file, " const");
1136 if (flags & ECF_PURE)
1137 fprintf (dump_file, " pure");
1138 if (flags & ECF_NOTHROW)
1139 fprintf (dump_file, " nothrow");
1140 fprintf (dump_file, "\n pure const state: %s\n",
1141 pure_const_names[fs->pure_const_state]);
1142 fprintf (dump_file, " previously known state: %s\n",
1143 pure_const_names[fs->state_previously_known]);
1144 if (fs->looping)
1145 fprintf (dump_file," function is locally looping\n");
1146 if (fs->looping_previously_known)
1147 fprintf (dump_file," function is previously known looping\n");
1148 if (fs->can_throw)
1149 fprintf (dump_file," function is locally throwing\n");
1150 if (fs->can_free)
1151 fprintf (dump_file," function can locally free\n");
1155 lto_destroy_simple_input_block (file_data,
1156 LTO_section_ipa_pure_const,
1157 ib, data, len);
1162 /* We only propagate across edges that can throw externally and their callee
1163 is not interposable. */
1165 static bool
1166 ignore_edge_for_nothrow (struct cgraph_edge *e)
1168 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1169 return true;
1171 enum availability avail;
1172 cgraph_node *n = e->callee->function_or_virtual_thunk_symbol (&avail,
1173 e->caller);
1174 if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (n->decl))
1175 return true;
1176 return opt_for_fn (e->callee->decl, flag_non_call_exceptions)
1177 && !e->callee->binds_to_current_def_p (e->caller);
1180 /* Return true if NODE is self recursive function.
1181 Indirectly recursive functions appears as non-trivial strongly
1182 connected components, so we need to care about self recursion
1183 only. */
1185 static bool
1186 self_recursive_p (struct cgraph_node *node)
1188 struct cgraph_edge *e;
1189 for (e = node->callees; e; e = e->next_callee)
1190 if (e->callee->function_symbol () == node)
1191 return true;
1192 return false;
1195 /* Return true if N is cdtor that is not const or pure. In this case we may
1196 need to remove unreachable function if it is marked const/pure. */
1198 static bool
1199 cdtor_p (cgraph_node *n, void *)
1201 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1202 return ((!TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl))
1203 || DECL_LOOPING_CONST_OR_PURE_P (n->decl));
1204 return false;
1207 /* We only propagate across edges with non-interposable callee. */
1209 static bool
1210 ignore_edge_for_pure_const (struct cgraph_edge *e)
1212 enum availability avail;
1213 e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
1214 return (avail <= AVAIL_INTERPOSABLE);
1218 /* Produce transitive closure over the callgraph and compute pure/const
1219 attributes. */
1221 static bool
1222 propagate_pure_const (void)
1224 struct cgraph_node *node;
1225 struct cgraph_node *w;
1226 struct cgraph_node **order =
1227 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1228 int order_pos;
1229 int i;
1230 struct ipa_dfs_info * w_info;
1231 bool remove_p = false;
1232 bool has_cdtor;
1234 order_pos = ipa_reduced_postorder (order, true, false,
1235 ignore_edge_for_pure_const);
1236 if (dump_file)
1238 cgraph_node::dump_cgraph (dump_file);
1239 ipa_print_order (dump_file, "reduced", order, order_pos);
1242 /* Propagate the local information through the call graph to produce
1243 the global information. All the nodes within a cycle will have
1244 the same info so we collapse cycles first. Then we can do the
1245 propagation in one pass from the leaves to the roots. */
1246 for (i = 0; i < order_pos; i++ )
1248 enum pure_const_state_e pure_const_state = IPA_CONST;
1249 bool looping = false;
1250 int count = 0;
1251 node = order[i];
1253 if (node->alias)
1254 continue;
1256 if (dump_file && (dump_flags & TDF_DETAILS))
1257 fprintf (dump_file, "Starting cycle\n");
1259 /* Find the worst state for any node in the cycle. */
1260 w = node;
1261 while (w && pure_const_state != IPA_NEITHER)
1263 struct cgraph_edge *e;
1264 struct cgraph_edge *ie;
1265 int i;
1266 struct ipa_ref *ref = NULL;
1268 funct_state w_l = get_function_state (w);
1269 if (dump_file && (dump_flags & TDF_DETAILS))
1270 fprintf (dump_file, " Visiting %s state:%s looping %i\n",
1271 w->dump_name (),
1272 pure_const_names[w_l->pure_const_state],
1273 w_l->looping);
1275 /* First merge in function body properties.
1276 We are safe to pass NULL as FROM and TO because we will take care
1277 of possible interposition when walking callees. */
1278 worse_state (&pure_const_state, &looping,
1279 w_l->pure_const_state, w_l->looping,
1280 NULL, NULL);
1281 if (pure_const_state == IPA_NEITHER)
1282 break;
1284 count++;
1286 /* We consider recursive cycles as possibly infinite.
1287 This might be relaxed since infinite recursion leads to stack
1288 overflow. */
1289 if (count > 1)
1290 looping = true;
1292 /* Now walk the edges and merge in callee properties. */
1293 for (e = w->callees; e && pure_const_state != IPA_NEITHER;
1294 e = e->next_callee)
1296 enum availability avail;
1297 struct cgraph_node *y = e->callee->
1298 function_or_virtual_thunk_symbol (&avail,
1299 e->caller);
1300 enum pure_const_state_e edge_state = IPA_CONST;
1301 bool edge_looping = false;
1303 if (dump_file && (dump_flags & TDF_DETAILS))
1305 fprintf (dump_file, " Call to %s",
1306 e->callee->dump_name ());
1308 if (avail > AVAIL_INTERPOSABLE)
1310 funct_state y_l = get_function_state (y);
1311 if (dump_file && (dump_flags & TDF_DETAILS))
1313 fprintf (dump_file,
1314 " state:%s looping:%i\n",
1315 pure_const_names[y_l->pure_const_state],
1316 y_l->looping);
1318 if (y_l->pure_const_state > IPA_PURE
1319 && e->cannot_lead_to_return_p ())
1321 if (dump_file && (dump_flags & TDF_DETAILS))
1322 fprintf (dump_file,
1323 " Ignoring side effects"
1324 " -> pure, looping\n");
1325 edge_state = IPA_PURE;
1326 edge_looping = true;
1328 else
1330 edge_state = y_l->pure_const_state;
1331 edge_looping = y_l->looping;
1334 else if (special_builtin_state (&edge_state, &edge_looping,
1335 y->decl))
1337 else
1338 state_from_flags (&edge_state, &edge_looping,
1339 flags_from_decl_or_type (y->decl),
1340 e->cannot_lead_to_return_p ());
1342 /* Merge the results with what we already know. */
1343 better_state (&edge_state, &edge_looping,
1344 w_l->state_previously_known,
1345 w_l->looping_previously_known);
1346 worse_state (&pure_const_state, &looping,
1347 edge_state, edge_looping, e->caller, e->callee);
1348 if (pure_const_state == IPA_NEITHER)
1349 break;
1352 /* Now process the indirect call. */
1353 for (ie = w->indirect_calls;
1354 ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee)
1356 enum pure_const_state_e edge_state = IPA_CONST;
1357 bool edge_looping = false;
1359 if (dump_file && (dump_flags & TDF_DETAILS))
1360 fprintf (dump_file, " Indirect call");
1361 state_from_flags (&edge_state, &edge_looping,
1362 ie->indirect_info->ecf_flags,
1363 ie->cannot_lead_to_return_p ());
1364 /* Merge the results with what we already know. */
1365 better_state (&edge_state, &edge_looping,
1366 w_l->state_previously_known,
1367 w_l->looping_previously_known);
1368 worse_state (&pure_const_state, &looping,
1369 edge_state, edge_looping, NULL, NULL);
1370 if (pure_const_state == IPA_NEITHER)
1371 break;
1374 /* And finally all loads and stores. */
1375 for (i = 0; w->iterate_reference (i, ref)
1376 && pure_const_state != IPA_NEITHER; i++)
1378 enum pure_const_state_e ref_state = IPA_CONST;
1379 bool ref_looping = false;
1380 switch (ref->use)
1382 case IPA_REF_LOAD:
1383 /* readonly reads are safe. */
1384 if (TREE_READONLY (ref->referred->decl))
1385 break;
1386 if (dump_file && (dump_flags & TDF_DETAILS))
1387 fprintf (dump_file, " nonreadonly global var read\n");
1388 ref_state = IPA_PURE;
1389 break;
1390 case IPA_REF_STORE:
1391 if (ref->cannot_lead_to_return ())
1392 break;
1393 ref_state = IPA_NEITHER;
1394 if (dump_file && (dump_flags & TDF_DETAILS))
1395 fprintf (dump_file, " global var write\n");
1396 break;
1397 case IPA_REF_ADDR:
1398 case IPA_REF_CHKP:
1399 break;
1400 default:
1401 gcc_unreachable ();
1403 better_state (&ref_state, &ref_looping,
1404 w_l->state_previously_known,
1405 w_l->looping_previously_known);
1406 worse_state (&pure_const_state, &looping,
1407 ref_state, ref_looping, NULL, NULL);
1408 if (pure_const_state == IPA_NEITHER)
1409 break;
1411 w_info = (struct ipa_dfs_info *) w->aux;
1412 w = w_info->next_cycle;
1414 if (dump_file && (dump_flags & TDF_DETAILS))
1415 fprintf (dump_file, "Result %s looping %i\n",
1416 pure_const_names [pure_const_state],
1417 looping);
1419 /* Find the worst state of can_free for any node in the cycle. */
1420 bool can_free = false;
1421 w = node;
1422 while (w && !can_free)
1424 struct cgraph_edge *e;
1425 funct_state w_l = get_function_state (w);
1427 if (w_l->can_free
1428 || w->get_availability () == AVAIL_INTERPOSABLE
1429 || w->indirect_calls)
1430 can_free = true;
1432 for (e = w->callees; e && !can_free; e = e->next_callee)
1434 enum availability avail;
1435 struct cgraph_node *y = e->callee->
1436 function_or_virtual_thunk_symbol (&avail,
1437 e->caller);
1439 if (avail > AVAIL_INTERPOSABLE)
1440 can_free = get_function_state (y)->can_free;
1441 else
1442 can_free = true;
1444 w_info = (struct ipa_dfs_info *) w->aux;
1445 w = w_info->next_cycle;
1448 /* Copy back the region's pure_const_state which is shared by
1449 all nodes in the region. */
1450 w = node;
1451 while (w)
1453 funct_state w_l = get_function_state (w);
1454 enum pure_const_state_e this_state = pure_const_state;
1455 bool this_looping = looping;
1457 w_l->can_free = can_free;
1458 w->nonfreeing_fn = !can_free;
1459 if (!can_free && dump_file)
1460 fprintf (dump_file, "Function found not to call free: %s\n",
1461 w->name ());
1463 if (w_l->state_previously_known != IPA_NEITHER
1464 && this_state > w_l->state_previously_known)
1466 this_state = w_l->state_previously_known;
1467 if (this_state == IPA_NEITHER)
1468 this_looping = w_l->looping_previously_known;
1470 if (!this_looping && self_recursive_p (w))
1471 this_looping = true;
1472 if (!w_l->looping_previously_known)
1473 this_looping = false;
1475 /* All nodes within a cycle share the same info. */
1476 w_l->pure_const_state = this_state;
1477 w_l->looping = this_looping;
1479 /* Inline clones share declaration with their offline copies;
1480 do not modify their declarations since the offline copy may
1481 be different. */
1482 if (!w->global.inlined_to)
1483 switch (this_state)
1485 case IPA_CONST:
1486 if (!TREE_READONLY (w->decl))
1488 warn_function_const (w->decl, !this_looping);
1489 if (dump_file)
1490 fprintf (dump_file, "Function found to be %sconst: %s\n",
1491 this_looping ? "looping " : "",
1492 w->name ());
1494 /* Turning constructor or destructor to non-looping const/pure
1495 enables us to possibly remove the function completely. */
1496 if (this_looping)
1497 has_cdtor = false;
1498 else
1499 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1500 NULL, true);
1501 if (w->set_const_flag (true, this_looping))
1503 if (dump_file)
1504 fprintf (dump_file,
1505 "Declaration updated to be %sconst: %s\n",
1506 this_looping ? "looping " : "",
1507 w->name ());
1508 remove_p |= has_cdtor;
1510 break;
1512 case IPA_PURE:
1513 if (!DECL_PURE_P (w->decl))
1515 warn_function_pure (w->decl, !this_looping);
1516 if (dump_file)
1517 fprintf (dump_file, "Function found to be %spure: %s\n",
1518 this_looping ? "looping " : "",
1519 w->name ());
1521 if (this_looping)
1522 has_cdtor = false;
1523 else
1524 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1525 NULL, true);
1526 if (w->set_pure_flag (true, this_looping))
1528 if (dump_file)
1529 fprintf (dump_file,
1530 "Declaration updated to be %spure: %s\n",
1531 this_looping ? "looping " : "",
1532 w->name ());
1533 remove_p |= has_cdtor;
1535 break;
1537 default:
1538 break;
1540 w_info = (struct ipa_dfs_info *) w->aux;
1541 w = w_info->next_cycle;
1545 ipa_free_postorder_info ();
1546 free (order);
1547 return remove_p;
1550 /* Produce transitive closure over the callgraph and compute nothrow
1551 attributes. */
1553 static void
1554 propagate_nothrow (void)
1556 struct cgraph_node *node;
1557 struct cgraph_node *w;
1558 struct cgraph_node **order =
1559 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1560 int order_pos;
1561 int i;
1562 struct ipa_dfs_info * w_info;
1564 order_pos = ipa_reduced_postorder (order, true, false,
1565 ignore_edge_for_nothrow);
1566 if (dump_file)
1568 cgraph_node::dump_cgraph (dump_file);
1569 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1572 /* Propagate the local information through the call graph to produce
1573 the global information. All the nodes within a cycle will have
1574 the same info so we collapse cycles first. Then we can do the
1575 propagation in one pass from the leaves to the roots. */
1576 for (i = 0; i < order_pos; i++ )
1578 bool can_throw = false;
1579 node = order[i];
1581 if (node->alias)
1582 continue;
1584 /* Find the worst state for any node in the cycle. */
1585 w = node;
1586 while (w && !can_throw)
1588 struct cgraph_edge *e, *ie;
1590 if (!TREE_NOTHROW (w->decl))
1592 funct_state w_l = get_function_state (w);
1594 if (w_l->can_throw
1595 || w->get_availability () == AVAIL_INTERPOSABLE)
1596 can_throw = true;
1598 for (e = w->callees; e && !can_throw; e = e->next_callee)
1600 enum availability avail;
1602 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1603 continue;
1605 struct cgraph_node *y = e->callee->
1606 function_or_virtual_thunk_symbol (&avail,
1607 e->caller);
1609 /* We can use info about the callee only if we know it can
1610 not be interposed.
1611 When callee is compiled with non-call exceptions we also
1612 must check that the declaration is bound to current
1613 body as other semantically equivalent body may still
1614 throw. */
1615 if (avail <= AVAIL_INTERPOSABLE
1616 || (!TREE_NOTHROW (y->decl)
1617 && (get_function_state (y)->can_throw
1618 || (opt_for_fn (y->decl, flag_non_call_exceptions)
1619 && !e->callee->binds_to_current_def_p (w)))))
1620 can_throw = true;
1622 for (ie = w->indirect_calls; ie && !can_throw;
1623 ie = ie->next_callee)
1624 if (ie->can_throw_external
1625 && !(ie->indirect_info->ecf_flags & ECF_NOTHROW))
1626 can_throw = true;
1628 w_info = (struct ipa_dfs_info *) w->aux;
1629 w = w_info->next_cycle;
1632 /* Copy back the region's pure_const_state which is shared by
1633 all nodes in the region. */
1634 w = node;
1635 while (w)
1637 funct_state w_l = get_function_state (w);
1638 if (!can_throw && !TREE_NOTHROW (w->decl))
1640 /* Inline clones share declaration with their offline copies;
1641 do not modify their declarations since the offline copy may
1642 be different. */
1643 if (!w->global.inlined_to)
1645 w->set_nothrow_flag (true);
1646 if (dump_file)
1647 fprintf (dump_file, "Function found to be nothrow: %s\n",
1648 w->name ());
1651 else if (can_throw && !TREE_NOTHROW (w->decl))
1652 w_l->can_throw = true;
1653 w_info = (struct ipa_dfs_info *) w->aux;
1654 w = w_info->next_cycle;
1658 ipa_free_postorder_info ();
1659 free (order);
1663 /* Produce the global information by preforming a transitive closure
1664 on the local information that was produced by generate_summary. */
1666 unsigned int
1667 pass_ipa_pure_const::
1668 execute (function *)
1670 struct cgraph_node *node;
1671 bool remove_p;
1673 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1674 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1675 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1677 /* Nothrow makes more function to not lead to return and improve
1678 later analysis. */
1679 propagate_nothrow ();
1680 remove_p = propagate_pure_const ();
1682 /* Cleanup. */
1683 FOR_EACH_FUNCTION (node)
1684 if (has_function_state (node))
1685 free (get_function_state (node));
1686 funct_state_vec.release ();
1687 return remove_p ? TODO_remove_functions : 0;
1690 static bool
1691 gate_pure_const (void)
1693 return flag_ipa_pure_const || in_lto_p;
1696 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1697 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1698 pure_const_generate_summary, /* generate_summary */
1699 pure_const_write_summary, /* write_summary */
1700 pure_const_read_summary, /* read_summary */
1701 NULL, /* write_optimization_summary */
1702 NULL, /* read_optimization_summary */
1703 NULL, /* stmt_fixup */
1704 0, /* function_transform_todo_flags_start */
1705 NULL, /* function_transform */
1706 NULL), /* variable_transform */
1707 init_p(false),
1708 function_insertion_hook_holder(NULL),
1709 node_duplication_hook_holder(NULL),
1710 node_removal_hook_holder(NULL)
1714 ipa_opt_pass_d *
1715 make_pass_ipa_pure_const (gcc::context *ctxt)
1717 return new pass_ipa_pure_const (ctxt);
1720 /* Return true if function should be skipped for local pure const analysis. */
1722 static bool
1723 skip_function_for_local_pure_const (struct cgraph_node *node)
1725 /* Because we do not schedule pass_fixup_cfg over whole program after early
1726 optimizations we must not promote functions that are called by already
1727 processed functions. */
1729 if (function_called_by_processed_nodes_p ())
1731 if (dump_file)
1732 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1733 return true;
1735 /* Save some work and do not analyze functions which are interposable and
1736 do not have any non-interposable aliases. */
1737 if (node->get_availability () <= AVAIL_INTERPOSABLE
1738 && !node->has_aliases_p ())
1740 if (dump_file)
1741 fprintf (dump_file,
1742 "Function is interposable; not analyzing.\n");
1743 return true;
1745 return false;
1748 /* Simple local pass for pure const discovery reusing the analysis from
1749 ipa_pure_const. This pass is effective when executed together with
1750 other optimization passes in early optimization pass queue. */
1752 namespace {
1754 const pass_data pass_data_local_pure_const =
1756 GIMPLE_PASS, /* type */
1757 "local-pure-const", /* name */
1758 OPTGROUP_NONE, /* optinfo_flags */
1759 TV_IPA_PURE_CONST, /* tv_id */
1760 0, /* properties_required */
1761 0, /* properties_provided */
1762 0, /* properties_destroyed */
1763 0, /* todo_flags_start */
1764 0, /* todo_flags_finish */
1767 class pass_local_pure_const : public gimple_opt_pass
1769 public:
1770 pass_local_pure_const (gcc::context *ctxt)
1771 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1774 /* opt_pass methods: */
1775 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1776 virtual bool gate (function *) { return gate_pure_const (); }
1777 virtual unsigned int execute (function *);
1779 }; // class pass_local_pure_const
1781 unsigned int
1782 pass_local_pure_const::execute (function *fun)
1784 bool changed = false;
1785 funct_state l;
1786 bool skip;
1787 struct cgraph_node *node;
1789 node = cgraph_node::get (current_function_decl);
1790 skip = skip_function_for_local_pure_const (node);
1791 if (!warn_suggest_attribute_const
1792 && !warn_suggest_attribute_pure
1793 && skip)
1794 return 0;
1796 l = analyze_function (node, false);
1798 /* Do NORETURN discovery. */
1799 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1800 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1802 warn_function_noreturn (fun->decl);
1803 if (dump_file)
1804 fprintf (dump_file, "Function found to be noreturn: %s\n",
1805 current_function_name ());
1807 /* Update declaration and reduce profile to executed once. */
1808 TREE_THIS_VOLATILE (current_function_decl) = 1;
1809 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1810 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1812 changed = true;
1815 switch (l->pure_const_state)
1817 case IPA_CONST:
1818 if (!TREE_READONLY (current_function_decl))
1820 warn_function_const (current_function_decl, !l->looping);
1821 if (dump_file)
1822 fprintf (dump_file, "Function found to be %sconst: %s\n",
1823 l->looping ? "looping " : "",
1824 current_function_name ());
1826 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1827 && !l->looping)
1829 if (dump_file)
1830 fprintf (dump_file, "Function found to be non-looping: %s\n",
1831 current_function_name ());
1833 if (!skip && node->set_const_flag (true, l->looping))
1835 if (dump_file)
1836 fprintf (dump_file, "Declaration updated to be %sconst: %s\n",
1837 l->looping ? "looping " : "",
1838 current_function_name ());
1839 changed = true;
1841 break;
1843 case IPA_PURE:
1844 if (!DECL_PURE_P (current_function_decl))
1846 warn_function_pure (current_function_decl, !l->looping);
1847 if (dump_file)
1848 fprintf (dump_file, "Function found to be %spure: %s\n",
1849 l->looping ? "looping " : "",
1850 current_function_name ());
1852 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1853 && !l->looping)
1855 if (dump_file)
1856 fprintf (dump_file, "Function found to be non-looping: %s\n",
1857 current_function_name ());
1859 if (!skip && node->set_pure_flag (true, l->looping))
1861 if (dump_file)
1862 fprintf (dump_file, "Declaration updated to be %spure: %s\n",
1863 l->looping ? "looping " : "",
1864 current_function_name ());
1865 changed = true;
1867 break;
1869 default:
1870 break;
1872 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1874 node->set_nothrow_flag (true);
1875 changed = true;
1876 if (dump_file)
1877 fprintf (dump_file, "Function found to be nothrow: %s\n",
1878 current_function_name ());
1880 free (l);
1881 if (changed)
1882 return execute_fixup_cfg ();
1883 else
1884 return 0;
1887 } // anon namespace
1889 gimple_opt_pass *
1890 make_pass_local_pure_const (gcc::context *ctxt)
1892 return new pass_local_pure_const (ctxt);
1895 /* Emit noreturn warnings. */
1897 namespace {
1899 const pass_data pass_data_warn_function_noreturn =
1901 GIMPLE_PASS, /* type */
1902 "*warn_function_noreturn", /* name */
1903 OPTGROUP_NONE, /* optinfo_flags */
1904 TV_NONE, /* tv_id */
1905 PROP_cfg, /* properties_required */
1906 0, /* properties_provided */
1907 0, /* properties_destroyed */
1908 0, /* todo_flags_start */
1909 0, /* todo_flags_finish */
1912 class pass_warn_function_noreturn : public gimple_opt_pass
1914 public:
1915 pass_warn_function_noreturn (gcc::context *ctxt)
1916 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1919 /* opt_pass methods: */
1920 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1921 virtual unsigned int execute (function *fun)
1923 if (!TREE_THIS_VOLATILE (current_function_decl)
1924 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1925 warn_function_noreturn (current_function_decl);
1926 return 0;
1929 }; // class pass_warn_function_noreturn
1931 } // anon namespace
1933 gimple_opt_pass *
1934 make_pass_warn_function_noreturn (gcc::context *ctxt)
1936 return new pass_warn_function_noreturn (ctxt);
1939 /* Simple local pass for pure const discovery reusing the analysis from
1940 ipa_pure_const. This pass is effective when executed together with
1941 other optimization passes in early optimization pass queue. */
1943 namespace {
1945 const pass_data pass_data_nothrow =
1947 GIMPLE_PASS, /* type */
1948 "nothrow", /* name */
1949 OPTGROUP_NONE, /* optinfo_flags */
1950 TV_IPA_PURE_CONST, /* tv_id */
1951 0, /* properties_required */
1952 0, /* properties_provided */
1953 0, /* properties_destroyed */
1954 0, /* todo_flags_start */
1955 0, /* todo_flags_finish */
1958 class pass_nothrow : public gimple_opt_pass
1960 public:
1961 pass_nothrow (gcc::context *ctxt)
1962 : gimple_opt_pass (pass_data_nothrow, ctxt)
1965 /* opt_pass methods: */
1966 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1967 virtual bool gate (function *) { return optimize; }
1968 virtual unsigned int execute (function *);
1970 }; // class pass_nothrow
1972 unsigned int
1973 pass_nothrow::execute (function *)
1975 struct cgraph_node *node;
1976 basic_block this_block;
1978 if (TREE_NOTHROW (current_function_decl))
1979 return 0;
1981 node = cgraph_node::get (current_function_decl);
1983 /* We run during lowering, we can not really use availability yet. */
1984 if (cgraph_node::get (current_function_decl)->get_availability ()
1985 <= AVAIL_INTERPOSABLE)
1987 if (dump_file)
1988 fprintf (dump_file, "Function is interposable;"
1989 " not analyzing.\n");
1990 return true;
1993 FOR_EACH_BB_FN (this_block, cfun)
1995 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1996 !gsi_end_p (gsi);
1997 gsi_next (&gsi))
1998 if (stmt_can_throw_external (gsi_stmt (gsi)))
2000 if (is_gimple_call (gsi_stmt (gsi)))
2002 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
2003 if (callee_t && recursive_call_p (current_function_decl,
2004 callee_t))
2005 continue;
2008 if (dump_file)
2010 fprintf (dump_file, "Statement can throw: ");
2011 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
2013 return 0;
2017 node->set_nothrow_flag (true);
2019 bool cfg_changed = false;
2020 if (self_recursive_p (node))
2021 FOR_EACH_BB_FN (this_block, cfun)
2022 if (gimple *g = last_stmt (this_block))
2023 if (is_gimple_call (g))
2025 tree callee_t = gimple_call_fndecl (g);
2026 if (callee_t
2027 && recursive_call_p (current_function_decl, callee_t)
2028 && maybe_clean_eh_stmt (g)
2029 && gimple_purge_dead_eh_edges (this_block))
2030 cfg_changed = true;
2033 if (dump_file)
2034 fprintf (dump_file, "Function found to be nothrow: %s\n",
2035 current_function_name ());
2036 return cfg_changed ? TODO_cleanup_cfg : 0;
2039 } // anon namespace
2041 gimple_opt_pass *
2042 make_pass_nothrow (gcc::context *ctxt)
2044 return new pass_nothrow (ctxt);