* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / gcc / ipa-pure-const.c
blob915423559cb439f8c2bb2eddf50c02e5f9dd9c82
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)
160 || DECL_COMDAT (decl));
163 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
164 is true if the function is known to be finite. The diagnostic is
165 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
166 OPTION, this function may initialize it and it is always returned
167 by the function. */
169 static hash_set<tree> *
170 suggest_attribute (int option, tree decl, bool known_finite,
171 hash_set<tree> *warned_about,
172 const char * attrib_name)
174 if (!option_enabled (option, &global_options))
175 return warned_about;
176 if (TREE_THIS_VOLATILE (decl)
177 || (known_finite && function_always_visible_to_compiler_p (decl)))
178 return warned_about;
180 if (!warned_about)
181 warned_about = new hash_set<tree>;
182 if (warned_about->contains (decl))
183 return warned_about;
184 warned_about->add (decl);
185 warning_at (DECL_SOURCE_LOCATION (decl),
186 option,
187 known_finite
188 ? G_("function might be candidate for attribute %qs")
189 : G_("function might be candidate for attribute %qs"
190 " if it is known to return normally"), attrib_name);
191 return warned_about;
194 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
195 is true if the function is known to be finite. */
197 static void
198 warn_function_pure (tree decl, bool known_finite)
200 static hash_set<tree> *warned_about;
202 warned_about
203 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
204 known_finite, warned_about, "pure");
207 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
208 is true if the function is known to be finite. */
210 static void
211 warn_function_const (tree decl, bool known_finite)
213 static hash_set<tree> *warned_about;
214 warned_about
215 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
216 known_finite, warned_about, "const");
219 static void
220 warn_function_noreturn (tree decl)
222 tree original_decl = decl;
224 cgraph_node *node = cgraph_node::get (decl);
225 if (node->instrumentation_clone)
226 decl = node->instrumented_version->decl;
228 static hash_set<tree> *warned_about;
229 if (!lang_hooks.missing_noreturn_ok_p (decl)
230 && targetm.warn_func_return (decl))
231 warned_about
232 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, original_decl,
233 true, warned_about, "noreturn");
236 void
237 warn_function_cold (tree decl)
239 tree original_decl = decl;
241 cgraph_node *node = cgraph_node::get (decl);
242 if (node->instrumentation_clone)
243 decl = node->instrumented_version->decl;
245 static hash_set<tree> *warned_about;
246 warned_about
247 = suggest_attribute (OPT_Wsuggest_attribute_cold, original_decl,
248 true, warned_about, "cold");
251 /* Return true if we have a function state for NODE. */
253 static inline bool
254 has_function_state (struct cgraph_node *node)
256 if (!funct_state_vec.exists ()
257 || funct_state_vec.length () <= (unsigned int)node->uid)
258 return false;
259 return funct_state_vec[node->uid] != NULL;
262 /* Return the function state from NODE. */
264 static inline funct_state
265 get_function_state (struct cgraph_node *node)
267 if (!funct_state_vec.exists ()
268 || funct_state_vec.length () <= (unsigned int)node->uid
269 || !funct_state_vec[node->uid])
270 /* We might want to put correct previously_known state into varying. */
271 return &varying_state;
272 return funct_state_vec[node->uid];
275 /* Set the function state S for NODE. */
277 static inline void
278 set_function_state (struct cgraph_node *node, funct_state s)
280 if (!funct_state_vec.exists ()
281 || funct_state_vec.length () <= (unsigned int)node->uid)
282 funct_state_vec.safe_grow_cleared (node->uid + 1);
284 /* If funct_state_vec already contains a funct_state, we have to release
285 it before it's going to be ovewritten. */
286 if (funct_state_vec[node->uid] != NULL
287 && funct_state_vec[node->uid] != &varying_state)
288 free (funct_state_vec[node->uid]);
290 funct_state_vec[node->uid] = s;
293 /* Check to see if the use (or definition when CHECKING_WRITE is true)
294 variable T is legal in a function that is either pure or const. */
296 static inline void
297 check_decl (funct_state local,
298 tree t, bool checking_write, bool ipa)
300 /* Do not want to do anything with volatile except mark any
301 function that uses one to be not const or pure. */
302 if (TREE_THIS_VOLATILE (t))
304 local->pure_const_state = IPA_NEITHER;
305 if (dump_file)
306 fprintf (dump_file, " Volatile operand is not const/pure");
307 return;
310 /* Do not care about a local automatic that is not static. */
311 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
312 return;
314 /* If the variable has the "used" attribute, treat it as if it had a
315 been touched by the devil. */
316 if (DECL_PRESERVE_P (t))
318 local->pure_const_state = IPA_NEITHER;
319 if (dump_file)
320 fprintf (dump_file, " Used static/global variable is not const/pure\n");
321 return;
324 /* In IPA mode we are not interested in checking actual loads and stores;
325 they will be processed at propagation time using ipa_ref. */
326 if (ipa)
327 return;
329 /* Since we have dealt with the locals and params cases above, if we
330 are CHECKING_WRITE, this cannot be a pure or constant
331 function. */
332 if (checking_write)
334 local->pure_const_state = IPA_NEITHER;
335 if (dump_file)
336 fprintf (dump_file, " static/global memory write is not const/pure\n");
337 return;
340 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
342 /* Readonly reads are safe. */
343 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
344 return; /* Read of a constant, do not change the function state. */
345 else
347 if (dump_file)
348 fprintf (dump_file, " global memory read is not const\n");
349 /* Just a regular read. */
350 if (local->pure_const_state == IPA_CONST)
351 local->pure_const_state = IPA_PURE;
354 else
356 /* Compilation level statics can be read if they are readonly
357 variables. */
358 if (TREE_READONLY (t))
359 return;
361 if (dump_file)
362 fprintf (dump_file, " static memory read is not const\n");
363 /* Just a regular read. */
364 if (local->pure_const_state == IPA_CONST)
365 local->pure_const_state = IPA_PURE;
370 /* Check to see if the use (or definition when CHECKING_WRITE is true)
371 variable T is legal in a function that is either pure or const. */
373 static inline void
374 check_op (funct_state local, tree t, bool checking_write)
376 t = get_base_address (t);
377 if (t && TREE_THIS_VOLATILE (t))
379 local->pure_const_state = IPA_NEITHER;
380 if (dump_file)
381 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
382 return;
384 else if (t
385 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
386 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
387 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
389 if (dump_file)
390 fprintf (dump_file, " Indirect ref to local memory is OK\n");
391 return;
393 else if (checking_write)
395 local->pure_const_state = IPA_NEITHER;
396 if (dump_file)
397 fprintf (dump_file, " Indirect ref write is not const/pure\n");
398 return;
400 else
402 if (dump_file)
403 fprintf (dump_file, " Indirect ref read is not const\n");
404 if (local->pure_const_state == IPA_CONST)
405 local->pure_const_state = IPA_PURE;
409 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
411 static void
412 state_from_flags (enum pure_const_state_e *state, bool *looping,
413 int flags, bool cannot_lead_to_return)
415 *looping = false;
416 if (flags & ECF_LOOPING_CONST_OR_PURE)
418 *looping = true;
419 if (dump_file && (dump_flags & TDF_DETAILS))
420 fprintf (dump_file, " looping");
422 if (flags & ECF_CONST)
424 *state = IPA_CONST;
425 if (dump_file && (dump_flags & TDF_DETAILS))
426 fprintf (dump_file, " const\n");
428 else if (flags & ECF_PURE)
430 *state = IPA_PURE;
431 if (dump_file && (dump_flags & TDF_DETAILS))
432 fprintf (dump_file, " pure\n");
434 else if (cannot_lead_to_return)
436 *state = IPA_PURE;
437 *looping = true;
438 if (dump_file && (dump_flags & TDF_DETAILS))
439 fprintf (dump_file, " ignoring side effects->pure looping\n");
441 else
443 if (dump_file && (dump_flags & TDF_DETAILS))
444 fprintf (dump_file, " neither\n");
445 *state = IPA_NEITHER;
446 *looping = true;
450 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
451 into STATE and LOOPING better of the two variants.
452 Be sure to merge looping correctly. IPA_NEITHER functions
453 have looping 0 even if they don't have to return. */
455 static inline void
456 better_state (enum pure_const_state_e *state, bool *looping,
457 enum pure_const_state_e state2, bool looping2)
459 if (state2 < *state)
461 if (*state == IPA_NEITHER)
462 *looping = looping2;
463 else
464 *looping = MIN (*looping, looping2);
465 *state = state2;
467 else if (state2 != IPA_NEITHER)
468 *looping = MIN (*looping, looping2);
471 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
472 into STATE and LOOPING worse of the two variants.
473 N is the actual node called. */
475 static inline void
476 worse_state (enum pure_const_state_e *state, bool *looping,
477 enum pure_const_state_e state2, bool looping2,
478 struct symtab_node *from,
479 struct symtab_node *to)
481 /* Consider function:
483 bool a(int *p)
485 return *p==*p;
488 During early optimization we will turn this into:
490 bool a(int *p)
492 return true;
495 Now if this function will be detected as CONST however when interposed it
496 may end up being just pure. We always must assume the worst scenario here.
498 if (*state == IPA_CONST && state2 == IPA_CONST
499 && to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from))
501 if (dump_file && (dump_flags & TDF_DETAILS))
502 fprintf (dump_file, "Dropping state to PURE because call to %s may not "
503 "bind to current def.\n", to->name ());
504 state2 = IPA_PURE;
506 *state = MAX (*state, state2);
507 *looping = MAX (*looping, looping2);
510 /* Recognize special cases of builtins that are by themselves not pure or const
511 but function using them is. */
512 static bool
513 special_builtin_state (enum pure_const_state_e *state, bool *looping,
514 tree callee)
516 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
517 switch (DECL_FUNCTION_CODE (callee))
519 case BUILT_IN_RETURN:
520 case BUILT_IN_UNREACHABLE:
521 case BUILT_IN_ALLOCA:
522 case BUILT_IN_ALLOCA_WITH_ALIGN:
523 case BUILT_IN_STACK_SAVE:
524 case BUILT_IN_STACK_RESTORE:
525 case BUILT_IN_EH_POINTER:
526 case BUILT_IN_EH_FILTER:
527 case BUILT_IN_UNWIND_RESUME:
528 case BUILT_IN_CXA_END_CLEANUP:
529 case BUILT_IN_EH_COPY_VALUES:
530 case BUILT_IN_FRAME_ADDRESS:
531 case BUILT_IN_APPLY:
532 case BUILT_IN_APPLY_ARGS:
533 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
534 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
535 *looping = false;
536 *state = IPA_CONST;
537 return true;
538 case BUILT_IN_PREFETCH:
539 *looping = true;
540 *state = IPA_CONST;
541 return true;
542 default:
543 break;
545 return false;
548 /* Check the parameters of a function call to CALL_EXPR to see if
549 there are any references in the parameters that are not allowed for
550 pure or const functions. Also check to see if this is either an
551 indirect call, a call outside the compilation unit, or has special
552 attributes that may also effect the purity. The CALL_EXPR node for
553 the entire call expression. */
555 static void
556 check_call (funct_state local, gcall *call, bool ipa)
558 int flags = gimple_call_flags (call);
559 tree callee_t = gimple_call_fndecl (call);
560 bool possibly_throws = stmt_could_throw_p (call);
561 bool possibly_throws_externally = (possibly_throws
562 && stmt_can_throw_external (call));
564 if (possibly_throws)
566 unsigned int i;
567 for (i = 0; i < gimple_num_ops (call); i++)
568 if (gimple_op (call, i)
569 && tree_could_throw_p (gimple_op (call, i)))
571 if (possibly_throws && cfun->can_throw_non_call_exceptions)
573 if (dump_file)
574 fprintf (dump_file, " operand can throw; looping\n");
575 local->looping = true;
577 if (possibly_throws_externally)
579 if (dump_file)
580 fprintf (dump_file, " operand can throw externally\n");
581 local->can_throw = true;
586 /* The const and pure flags are set by a variety of places in the
587 compiler (including here). If someone has already set the flags
588 for the callee, (such as for some of the builtins) we will use
589 them, otherwise we will compute our own information.
591 Const and pure functions have less clobber effects than other
592 functions so we process these first. Otherwise if it is a call
593 outside the compilation unit or an indirect call we punt. This
594 leaves local calls which will be processed by following the call
595 graph. */
596 if (callee_t)
598 enum pure_const_state_e call_state;
599 bool call_looping;
601 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
602 && !nonfreeing_call_p (call))
603 local->can_free = true;
605 if (special_builtin_state (&call_state, &call_looping, callee_t))
607 worse_state (&local->pure_const_state, &local->looping,
608 call_state, call_looping,
609 NULL, NULL);
610 return;
612 /* When bad things happen to bad functions, they cannot be const
613 or pure. */
614 if (setjmp_call_p (callee_t))
616 if (dump_file)
617 fprintf (dump_file, " setjmp is not const/pure\n");
618 local->looping = true;
619 local->pure_const_state = IPA_NEITHER;
622 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
623 switch (DECL_FUNCTION_CODE (callee_t))
625 case BUILT_IN_LONGJMP:
626 case BUILT_IN_NONLOCAL_GOTO:
627 if (dump_file)
628 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
629 local->pure_const_state = IPA_NEITHER;
630 local->looping = true;
631 break;
632 default:
633 break;
636 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
637 local->can_free = true;
639 /* When not in IPA mode, we can still handle self recursion. */
640 if (!ipa && callee_t
641 && recursive_call_p (current_function_decl, callee_t))
643 if (dump_file)
644 fprintf (dump_file, " Recursive call can loop.\n");
645 local->looping = true;
647 /* Either callee is unknown or we are doing local analysis.
648 Look to see if there are any bits available for the callee (such as by
649 declaration or because it is builtin) and process solely on the basis of
650 those bits. Handle internal calls always, those calls don't have
651 corresponding cgraph edges and thus aren't processed during
652 the propagation. */
653 else if (!ipa || gimple_call_internal_p (call))
655 enum pure_const_state_e call_state;
656 bool call_looping;
657 if (possibly_throws && cfun->can_throw_non_call_exceptions)
659 if (dump_file)
660 fprintf (dump_file, " can throw; looping\n");
661 local->looping = true;
663 if (possibly_throws_externally)
665 if (dump_file)
667 fprintf (dump_file, " can throw externally to lp %i\n",
668 lookup_stmt_eh_lp (call));
669 if (callee_t)
670 fprintf (dump_file, " callee:%s\n",
671 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
673 local->can_throw = true;
675 if (dump_file && (dump_flags & TDF_DETAILS))
676 fprintf (dump_file, " checking flags for call:");
677 state_from_flags (&call_state, &call_looping, flags,
678 ((flags & (ECF_NORETURN | ECF_NOTHROW))
679 == (ECF_NORETURN | ECF_NOTHROW))
680 || (!flag_exceptions && (flags & ECF_NORETURN)));
681 worse_state (&local->pure_const_state, &local->looping,
682 call_state, call_looping, NULL, NULL);
684 /* Direct functions calls are handled by IPA propagation. */
687 /* Wrapper around check_decl for loads in local more. */
689 static bool
690 check_load (gimple *, tree op, tree, void *data)
692 if (DECL_P (op))
693 check_decl ((funct_state)data, op, false, false);
694 else
695 check_op ((funct_state)data, op, false);
696 return false;
699 /* Wrapper around check_decl for stores in local more. */
701 static bool
702 check_store (gimple *, tree op, tree, void *data)
704 if (DECL_P (op))
705 check_decl ((funct_state)data, op, true, false);
706 else
707 check_op ((funct_state)data, op, true);
708 return false;
711 /* Wrapper around check_decl for loads in ipa mode. */
713 static bool
714 check_ipa_load (gimple *, tree op, tree, void *data)
716 if (DECL_P (op))
717 check_decl ((funct_state)data, op, false, true);
718 else
719 check_op ((funct_state)data, op, false);
720 return false;
723 /* Wrapper around check_decl for stores in ipa mode. */
725 static bool
726 check_ipa_store (gimple *, tree op, tree, void *data)
728 if (DECL_P (op))
729 check_decl ((funct_state)data, op, true, true);
730 else
731 check_op ((funct_state)data, op, true);
732 return false;
735 /* Look into pointer pointed to by GSIP and figure out what interesting side
736 effects it has. */
737 static void
738 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
740 gimple *stmt = gsi_stmt (*gsip);
742 if (is_gimple_debug (stmt))
743 return;
745 /* Do consider clobber as side effects before IPA, so we rather inline
746 C++ destructors and keep clobber semantics than eliminate them.
748 TODO: We may get smarter during early optimizations on these and let
749 functions containing only clobbers to be optimized more. This is a common
750 case of C++ destructors. */
752 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
753 return;
755 if (dump_file)
757 fprintf (dump_file, " scanning: ");
758 print_gimple_stmt (dump_file, stmt, 0);
761 if (gimple_has_volatile_ops (stmt)
762 && !gimple_clobber_p (stmt))
764 local->pure_const_state = IPA_NEITHER;
765 if (dump_file)
766 fprintf (dump_file, " Volatile stmt is not const/pure\n");
769 /* Look for loads and stores. */
770 walk_stmt_load_store_ops (stmt, local,
771 ipa ? check_ipa_load : check_load,
772 ipa ? check_ipa_store : check_store);
774 if (gimple_code (stmt) != GIMPLE_CALL
775 && stmt_could_throw_p (stmt))
777 if (cfun->can_throw_non_call_exceptions)
779 if (dump_file)
780 fprintf (dump_file, " can throw; looping\n");
781 local->looping = true;
783 if (stmt_can_throw_external (stmt))
785 if (dump_file)
786 fprintf (dump_file, " can throw externally\n");
787 local->can_throw = true;
789 else
790 if (dump_file)
791 fprintf (dump_file, " can throw\n");
793 switch (gimple_code (stmt))
795 case GIMPLE_CALL:
796 check_call (local, as_a <gcall *> (stmt), ipa);
797 break;
798 case GIMPLE_LABEL:
799 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
800 /* Target of long jump. */
802 if (dump_file)
803 fprintf (dump_file, " nonlocal label is not const/pure\n");
804 local->pure_const_state = IPA_NEITHER;
806 break;
807 case GIMPLE_ASM:
808 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
810 if (dump_file)
811 fprintf (dump_file, " memory asm clobber is not const/pure\n");
812 /* Abandon all hope, ye who enter here. */
813 local->pure_const_state = IPA_NEITHER;
814 local->can_free = true;
816 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
818 if (dump_file)
819 fprintf (dump_file, " volatile is not const/pure\n");
820 /* Abandon all hope, ye who enter here. */
821 local->pure_const_state = IPA_NEITHER;
822 local->looping = true;
823 local->can_free = true;
825 return;
826 default:
827 break;
832 /* This is the main routine for finding the reference patterns for
833 global variables within a function FN. */
835 static funct_state
836 analyze_function (struct cgraph_node *fn, bool ipa)
838 tree decl = fn->decl;
839 funct_state l;
840 basic_block this_block;
842 l = XCNEW (struct funct_state_d);
843 l->pure_const_state = IPA_CONST;
844 l->state_previously_known = IPA_NEITHER;
845 l->looping_previously_known = true;
846 l->looping = false;
847 l->can_throw = false;
848 l->can_free = false;
849 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
850 flags_from_decl_or_type (fn->decl),
851 fn->cannot_return_p ());
853 if (fn->thunk.thunk_p || fn->alias)
855 /* Thunk gets propagated through, so nothing interesting happens. */
856 gcc_assert (ipa);
857 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
858 l->pure_const_state = IPA_NEITHER;
859 return l;
862 if (dump_file)
864 fprintf (dump_file, "\n\n local analysis of %s\n ",
865 fn->name ());
868 push_cfun (DECL_STRUCT_FUNCTION (decl));
870 FOR_EACH_BB_FN (this_block, cfun)
872 gimple_stmt_iterator gsi;
873 struct walk_stmt_info wi;
875 memset (&wi, 0, sizeof (wi));
876 for (gsi = gsi_start_bb (this_block);
877 !gsi_end_p (gsi);
878 gsi_next (&gsi))
880 check_stmt (&gsi, l, ipa);
881 if (l->pure_const_state == IPA_NEITHER
882 && l->looping
883 && l->can_throw
884 && l->can_free)
885 goto end;
889 end:
890 if (l->pure_const_state != IPA_NEITHER)
892 /* Const functions cannot have back edges (an
893 indication of possible infinite loop side
894 effect. */
895 if (mark_dfs_back_edges ())
897 /* Preheaders are needed for SCEV to work.
898 Simple latches and recorded exits improve chances that loop will
899 proved to be finite in testcases such as in loop-15.c
900 and loop-24.c */
901 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
902 | LOOPS_HAVE_SIMPLE_LATCHES
903 | LOOPS_HAVE_RECORDED_EXITS);
904 if (dump_file && (dump_flags & TDF_DETAILS))
905 flow_loops_dump (dump_file, NULL, 0);
906 if (mark_irreducible_loops ())
908 if (dump_file)
909 fprintf (dump_file, " has irreducible loops\n");
910 l->looping = true;
912 else
914 struct loop *loop;
915 scev_initialize ();
916 FOR_EACH_LOOP (loop, 0)
917 if (!finite_loop_p (loop))
919 if (dump_file)
920 fprintf (dump_file, " can not prove finiteness of "
921 "loop %i\n", loop->num);
922 l->looping =true;
923 break;
925 scev_finalize ();
927 loop_optimizer_finalize ();
931 if (dump_file && (dump_flags & TDF_DETAILS))
932 fprintf (dump_file, " checking previously known:");
934 better_state (&l->pure_const_state, &l->looping,
935 l->state_previously_known,
936 l->looping_previously_known);
937 if (TREE_NOTHROW (decl))
938 l->can_throw = false;
940 pop_cfun ();
941 if (dump_file)
943 if (l->looping)
944 fprintf (dump_file, "Function is locally looping.\n");
945 if (l->can_throw)
946 fprintf (dump_file, "Function is locally throwing.\n");
947 if (l->pure_const_state == IPA_CONST)
948 fprintf (dump_file, "Function is locally const.\n");
949 if (l->pure_const_state == IPA_PURE)
950 fprintf (dump_file, "Function is locally pure.\n");
951 if (l->can_free)
952 fprintf (dump_file, "Function can locally free.\n");
954 return l;
957 /* Called when new function is inserted to callgraph late. */
958 static void
959 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
961 /* There are some shared nodes, in particular the initializers on
962 static declarations. We do not need to scan them more than once
963 since all we would be interested in are the addressof
964 operations. */
965 if (opt_for_fn (node->decl, flag_ipa_pure_const))
966 set_function_state (node, analyze_function (node, true));
969 /* Called when new clone is inserted to callgraph late. */
971 static void
972 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
973 void *data ATTRIBUTE_UNUSED)
975 if (has_function_state (src))
977 funct_state l = XNEW (struct funct_state_d);
978 gcc_assert (!has_function_state (dst));
979 memcpy (l, get_function_state (src), sizeof (*l));
980 set_function_state (dst, l);
984 /* Called when new clone is inserted to callgraph late. */
986 static void
987 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
989 if (has_function_state (node))
990 set_function_state (node, NULL);
994 void
995 pass_ipa_pure_const::
996 register_hooks (void)
998 if (init_p)
999 return;
1001 init_p = true;
1003 node_removal_hook_holder =
1004 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
1005 node_duplication_hook_holder =
1006 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
1007 function_insertion_hook_holder =
1008 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
1012 /* Analyze each function in the cgraph to see if it is locally PURE or
1013 CONST. */
1015 static void
1016 pure_const_generate_summary (void)
1018 struct cgraph_node *node;
1020 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1021 pass->register_hooks ();
1023 /* Process all of the functions.
1025 We process AVAIL_INTERPOSABLE functions. We can not use the results
1026 by default, but the info can be used at LTO with -fwhole-program or
1027 when function got cloned and the clone is AVAILABLE. */
1029 FOR_EACH_DEFINED_FUNCTION (node)
1030 if (opt_for_fn (node->decl, flag_ipa_pure_const))
1031 set_function_state (node, analyze_function (node, true));
1035 /* Serialize the ipa info for lto. */
1037 static void
1038 pure_const_write_summary (void)
1040 struct cgraph_node *node;
1041 struct lto_simple_output_block *ob
1042 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1043 unsigned int count = 0;
1044 lto_symtab_encoder_iterator lsei;
1045 lto_symtab_encoder_t encoder;
1047 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1049 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1050 lsei_next_function_in_partition (&lsei))
1052 node = lsei_cgraph_node (lsei);
1053 if (node->definition && has_function_state (node))
1054 count++;
1057 streamer_write_uhwi_stream (ob->main_stream, count);
1059 /* Process all of the functions. */
1060 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1061 lsei_next_function_in_partition (&lsei))
1063 node = lsei_cgraph_node (lsei);
1064 if (node->definition && has_function_state (node))
1066 struct bitpack_d bp;
1067 funct_state fs;
1068 int node_ref;
1069 lto_symtab_encoder_t encoder;
1071 fs = get_function_state (node);
1073 encoder = ob->decl_state->symtab_node_encoder;
1074 node_ref = lto_symtab_encoder_encode (encoder, node);
1075 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1077 /* Note that flags will need to be read in the opposite
1078 order as we are pushing the bitflags into FLAGS. */
1079 bp = bitpack_create (ob->main_stream);
1080 bp_pack_value (&bp, fs->pure_const_state, 2);
1081 bp_pack_value (&bp, fs->state_previously_known, 2);
1082 bp_pack_value (&bp, fs->looping_previously_known, 1);
1083 bp_pack_value (&bp, fs->looping, 1);
1084 bp_pack_value (&bp, fs->can_throw, 1);
1085 bp_pack_value (&bp, fs->can_free, 1);
1086 streamer_write_bitpack (&bp);
1090 lto_destroy_simple_output_block (ob);
1094 /* Deserialize the ipa info for lto. */
1096 static void
1097 pure_const_read_summary (void)
1099 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1100 struct lto_file_decl_data *file_data;
1101 unsigned int j = 0;
1103 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1104 pass->register_hooks ();
1106 while ((file_data = file_data_vec[j++]))
1108 const char *data;
1109 size_t len;
1110 struct lto_input_block *ib
1111 = lto_create_simple_input_block (file_data,
1112 LTO_section_ipa_pure_const,
1113 &data, &len);
1114 if (ib)
1116 unsigned int i;
1117 unsigned int count = streamer_read_uhwi (ib);
1119 for (i = 0; i < count; i++)
1121 unsigned int index;
1122 struct cgraph_node *node;
1123 struct bitpack_d bp;
1124 funct_state fs;
1125 lto_symtab_encoder_t encoder;
1127 fs = XCNEW (struct funct_state_d);
1128 index = streamer_read_uhwi (ib);
1129 encoder = file_data->symtab_node_encoder;
1130 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1131 index));
1132 set_function_state (node, fs);
1134 /* Note that the flags must be read in the opposite
1135 order in which they were written (the bitflags were
1136 pushed into FLAGS). */
1137 bp = streamer_read_bitpack (ib);
1138 fs->pure_const_state
1139 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1140 fs->state_previously_known
1141 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1142 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1143 fs->looping = bp_unpack_value (&bp, 1);
1144 fs->can_throw = bp_unpack_value (&bp, 1);
1145 fs->can_free = bp_unpack_value (&bp, 1);
1146 if (dump_file)
1148 int flags = flags_from_decl_or_type (node->decl);
1149 fprintf (dump_file, "Read info for %s ", node->dump_name ());
1150 if (flags & ECF_CONST)
1151 fprintf (dump_file, " const");
1152 if (flags & ECF_PURE)
1153 fprintf (dump_file, " pure");
1154 if (flags & ECF_NOTHROW)
1155 fprintf (dump_file, " nothrow");
1156 fprintf (dump_file, "\n pure const state: %s\n",
1157 pure_const_names[fs->pure_const_state]);
1158 fprintf (dump_file, " previously known state: %s\n",
1159 pure_const_names[fs->state_previously_known]);
1160 if (fs->looping)
1161 fprintf (dump_file," function is locally looping\n");
1162 if (fs->looping_previously_known)
1163 fprintf (dump_file," function is previously known looping\n");
1164 if (fs->can_throw)
1165 fprintf (dump_file," function is locally throwing\n");
1166 if (fs->can_free)
1167 fprintf (dump_file," function can locally free\n");
1171 lto_destroy_simple_input_block (file_data,
1172 LTO_section_ipa_pure_const,
1173 ib, data, len);
1178 /* We only propagate across edges that can throw externally and their callee
1179 is not interposable. */
1181 static bool
1182 ignore_edge_for_nothrow (struct cgraph_edge *e)
1184 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1185 return true;
1187 enum availability avail;
1188 cgraph_node *n = e->callee->function_or_virtual_thunk_symbol (&avail,
1189 e->caller);
1190 if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (n->decl))
1191 return true;
1192 return opt_for_fn (e->callee->decl, flag_non_call_exceptions)
1193 && !e->callee->binds_to_current_def_p (e->caller);
1196 /* Return true if NODE is self recursive function.
1197 Indirectly recursive functions appears as non-trivial strongly
1198 connected components, so we need to care about self recursion
1199 only. */
1201 static bool
1202 self_recursive_p (struct cgraph_node *node)
1204 struct cgraph_edge *e;
1205 for (e = node->callees; e; e = e->next_callee)
1206 if (e->callee->function_symbol () == node)
1207 return true;
1208 return false;
1211 /* Return true if N is cdtor that is not const or pure. In this case we may
1212 need to remove unreachable function if it is marked const/pure. */
1214 static bool
1215 cdtor_p (cgraph_node *n, void *)
1217 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1218 return ((!TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl))
1219 || DECL_LOOPING_CONST_OR_PURE_P (n->decl));
1220 return false;
1223 /* We only propagate across edges with non-interposable callee. */
1225 static bool
1226 ignore_edge_for_pure_const (struct cgraph_edge *e)
1228 enum availability avail;
1229 e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
1230 return (avail <= AVAIL_INTERPOSABLE);
1234 /* Produce transitive closure over the callgraph and compute pure/const
1235 attributes. */
1237 static bool
1238 propagate_pure_const (void)
1240 struct cgraph_node *node;
1241 struct cgraph_node *w;
1242 struct cgraph_node **order =
1243 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1244 int order_pos;
1245 int i;
1246 struct ipa_dfs_info * w_info;
1247 bool remove_p = false;
1248 bool has_cdtor;
1250 order_pos = ipa_reduced_postorder (order, true, false,
1251 ignore_edge_for_pure_const);
1252 if (dump_file)
1254 cgraph_node::dump_cgraph (dump_file);
1255 ipa_print_order (dump_file, "reduced", order, order_pos);
1258 /* Propagate the local information through the call graph to produce
1259 the global information. All the nodes within a cycle will have
1260 the same info so we collapse cycles first. Then we can do the
1261 propagation in one pass from the leaves to the roots. */
1262 for (i = 0; i < order_pos; i++ )
1264 enum pure_const_state_e pure_const_state = IPA_CONST;
1265 bool looping = false;
1266 int count = 0;
1267 node = order[i];
1269 if (node->alias)
1270 continue;
1272 if (dump_file && (dump_flags & TDF_DETAILS))
1273 fprintf (dump_file, "Starting cycle\n");
1275 /* Find the worst state for any node in the cycle. */
1276 w = node;
1277 while (w && pure_const_state != IPA_NEITHER)
1279 struct cgraph_edge *e;
1280 struct cgraph_edge *ie;
1281 int i;
1282 struct ipa_ref *ref = NULL;
1284 funct_state w_l = get_function_state (w);
1285 if (dump_file && (dump_flags & TDF_DETAILS))
1286 fprintf (dump_file, " Visiting %s state:%s looping %i\n",
1287 w->dump_name (),
1288 pure_const_names[w_l->pure_const_state],
1289 w_l->looping);
1291 /* First merge in function body properties.
1292 We are safe to pass NULL as FROM and TO because we will take care
1293 of possible interposition when walking callees. */
1294 worse_state (&pure_const_state, &looping,
1295 w_l->pure_const_state, w_l->looping,
1296 NULL, NULL);
1297 if (pure_const_state == IPA_NEITHER)
1298 break;
1300 count++;
1302 /* We consider recursive cycles as possibly infinite.
1303 This might be relaxed since infinite recursion leads to stack
1304 overflow. */
1305 if (count > 1)
1306 looping = true;
1308 /* Now walk the edges and merge in callee properties. */
1309 for (e = w->callees; e && pure_const_state != IPA_NEITHER;
1310 e = e->next_callee)
1312 enum availability avail;
1313 struct cgraph_node *y = e->callee->
1314 function_or_virtual_thunk_symbol (&avail,
1315 e->caller);
1316 enum pure_const_state_e edge_state = IPA_CONST;
1317 bool edge_looping = false;
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1321 fprintf (dump_file, " Call to %s",
1322 e->callee->dump_name ());
1324 if (avail > AVAIL_INTERPOSABLE)
1326 funct_state y_l = get_function_state (y);
1327 if (dump_file && (dump_flags & TDF_DETAILS))
1329 fprintf (dump_file,
1330 " state:%s looping:%i\n",
1331 pure_const_names[y_l->pure_const_state],
1332 y_l->looping);
1334 if (y_l->pure_const_state > IPA_PURE
1335 && e->cannot_lead_to_return_p ())
1337 if (dump_file && (dump_flags & TDF_DETAILS))
1338 fprintf (dump_file,
1339 " Ignoring side effects"
1340 " -> pure, looping\n");
1341 edge_state = IPA_PURE;
1342 edge_looping = true;
1344 else
1346 edge_state = y_l->pure_const_state;
1347 edge_looping = y_l->looping;
1350 else if (special_builtin_state (&edge_state, &edge_looping,
1351 y->decl))
1353 else
1354 state_from_flags (&edge_state, &edge_looping,
1355 flags_from_decl_or_type (y->decl),
1356 e->cannot_lead_to_return_p ());
1358 /* Merge the results with what we already know. */
1359 better_state (&edge_state, &edge_looping,
1360 w_l->state_previously_known,
1361 w_l->looping_previously_known);
1362 worse_state (&pure_const_state, &looping,
1363 edge_state, edge_looping, e->caller, e->callee);
1364 if (pure_const_state == IPA_NEITHER)
1365 break;
1368 /* Now process the indirect call. */
1369 for (ie = w->indirect_calls;
1370 ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee)
1372 enum pure_const_state_e edge_state = IPA_CONST;
1373 bool edge_looping = false;
1375 if (dump_file && (dump_flags & TDF_DETAILS))
1376 fprintf (dump_file, " Indirect call");
1377 state_from_flags (&edge_state, &edge_looping,
1378 ie->indirect_info->ecf_flags,
1379 ie->cannot_lead_to_return_p ());
1380 /* Merge the results with what we already know. */
1381 better_state (&edge_state, &edge_looping,
1382 w_l->state_previously_known,
1383 w_l->looping_previously_known);
1384 worse_state (&pure_const_state, &looping,
1385 edge_state, edge_looping, NULL, NULL);
1386 if (pure_const_state == IPA_NEITHER)
1387 break;
1390 /* And finally all loads and stores. */
1391 for (i = 0; w->iterate_reference (i, ref)
1392 && pure_const_state != IPA_NEITHER; i++)
1394 enum pure_const_state_e ref_state = IPA_CONST;
1395 bool ref_looping = false;
1396 switch (ref->use)
1398 case IPA_REF_LOAD:
1399 /* readonly reads are safe. */
1400 if (TREE_READONLY (ref->referred->decl))
1401 break;
1402 if (dump_file && (dump_flags & TDF_DETAILS))
1403 fprintf (dump_file, " nonreadonly global var read\n");
1404 ref_state = IPA_PURE;
1405 break;
1406 case IPA_REF_STORE:
1407 if (ref->cannot_lead_to_return ())
1408 break;
1409 ref_state = IPA_NEITHER;
1410 if (dump_file && (dump_flags & TDF_DETAILS))
1411 fprintf (dump_file, " global var write\n");
1412 break;
1413 case IPA_REF_ADDR:
1414 case IPA_REF_CHKP:
1415 break;
1416 default:
1417 gcc_unreachable ();
1419 better_state (&ref_state, &ref_looping,
1420 w_l->state_previously_known,
1421 w_l->looping_previously_known);
1422 worse_state (&pure_const_state, &looping,
1423 ref_state, ref_looping, NULL, NULL);
1424 if (pure_const_state == IPA_NEITHER)
1425 break;
1427 w_info = (struct ipa_dfs_info *) w->aux;
1428 w = w_info->next_cycle;
1430 if (dump_file && (dump_flags & TDF_DETAILS))
1431 fprintf (dump_file, "Result %s looping %i\n",
1432 pure_const_names [pure_const_state],
1433 looping);
1435 /* Find the worst state of can_free for any node in the cycle. */
1436 bool can_free = false;
1437 w = node;
1438 while (w && !can_free)
1440 struct cgraph_edge *e;
1441 funct_state w_l = get_function_state (w);
1443 if (w_l->can_free
1444 || w->get_availability () == AVAIL_INTERPOSABLE
1445 || w->indirect_calls)
1446 can_free = true;
1448 for (e = w->callees; e && !can_free; e = e->next_callee)
1450 enum availability avail;
1451 struct cgraph_node *y = e->callee->
1452 function_or_virtual_thunk_symbol (&avail,
1453 e->caller);
1455 if (avail > AVAIL_INTERPOSABLE)
1456 can_free = get_function_state (y)->can_free;
1457 else
1458 can_free = true;
1460 w_info = (struct ipa_dfs_info *) w->aux;
1461 w = w_info->next_cycle;
1464 /* Copy back the region's pure_const_state which is shared by
1465 all nodes in the region. */
1466 w = node;
1467 while (w)
1469 funct_state w_l = get_function_state (w);
1470 enum pure_const_state_e this_state = pure_const_state;
1471 bool this_looping = looping;
1473 w_l->can_free = can_free;
1474 w->nonfreeing_fn = !can_free;
1475 if (!can_free && dump_file)
1476 fprintf (dump_file, "Function found not to call free: %s\n",
1477 w->name ());
1479 if (w_l->state_previously_known != IPA_NEITHER
1480 && this_state > w_l->state_previously_known)
1482 this_state = w_l->state_previously_known;
1483 if (this_state == IPA_NEITHER)
1484 this_looping = w_l->looping_previously_known;
1486 if (!this_looping && self_recursive_p (w))
1487 this_looping = true;
1488 if (!w_l->looping_previously_known)
1489 this_looping = false;
1491 /* All nodes within a cycle share the same info. */
1492 w_l->pure_const_state = this_state;
1493 w_l->looping = this_looping;
1495 /* Inline clones share declaration with their offline copies;
1496 do not modify their declarations since the offline copy may
1497 be different. */
1498 if (!w->global.inlined_to)
1499 switch (this_state)
1501 case IPA_CONST:
1502 if (!TREE_READONLY (w->decl))
1504 warn_function_const (w->decl, !this_looping);
1505 if (dump_file)
1506 fprintf (dump_file, "Function found to be %sconst: %s\n",
1507 this_looping ? "looping " : "",
1508 w->name ());
1510 /* Turning constructor or destructor to non-looping const/pure
1511 enables us to possibly remove the function completely. */
1512 if (this_looping)
1513 has_cdtor = false;
1514 else
1515 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1516 NULL, true);
1517 if (w->set_const_flag (true, this_looping))
1519 if (dump_file)
1520 fprintf (dump_file,
1521 "Declaration updated to be %sconst: %s\n",
1522 this_looping ? "looping " : "",
1523 w->name ());
1524 remove_p |= has_cdtor;
1526 break;
1528 case IPA_PURE:
1529 if (!DECL_PURE_P (w->decl))
1531 warn_function_pure (w->decl, !this_looping);
1532 if (dump_file)
1533 fprintf (dump_file, "Function found to be %spure: %s\n",
1534 this_looping ? "looping " : "",
1535 w->name ());
1537 if (this_looping)
1538 has_cdtor = false;
1539 else
1540 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1541 NULL, true);
1542 if (w->set_pure_flag (true, this_looping))
1544 if (dump_file)
1545 fprintf (dump_file,
1546 "Declaration updated to be %spure: %s\n",
1547 this_looping ? "looping " : "",
1548 w->name ());
1549 remove_p |= has_cdtor;
1551 break;
1553 default:
1554 break;
1556 w_info = (struct ipa_dfs_info *) w->aux;
1557 w = w_info->next_cycle;
1561 ipa_free_postorder_info ();
1562 free (order);
1563 return remove_p;
1566 /* Produce transitive closure over the callgraph and compute nothrow
1567 attributes. */
1569 static void
1570 propagate_nothrow (void)
1572 struct cgraph_node *node;
1573 struct cgraph_node *w;
1574 struct cgraph_node **order =
1575 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1576 int order_pos;
1577 int i;
1578 struct ipa_dfs_info * w_info;
1580 order_pos = ipa_reduced_postorder (order, true, false,
1581 ignore_edge_for_nothrow);
1582 if (dump_file)
1584 cgraph_node::dump_cgraph (dump_file);
1585 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1588 /* Propagate the local information through the call graph to produce
1589 the global information. All the nodes within a cycle will have
1590 the same info so we collapse cycles first. Then we can do the
1591 propagation in one pass from the leaves to the roots. */
1592 for (i = 0; i < order_pos; i++ )
1594 bool can_throw = false;
1595 node = order[i];
1597 if (node->alias)
1598 continue;
1600 /* Find the worst state for any node in the cycle. */
1601 w = node;
1602 while (w && !can_throw)
1604 struct cgraph_edge *e, *ie;
1606 if (!TREE_NOTHROW (w->decl))
1608 funct_state w_l = get_function_state (w);
1610 if (w_l->can_throw
1611 || w->get_availability () == AVAIL_INTERPOSABLE)
1612 can_throw = true;
1614 for (e = w->callees; e && !can_throw; e = e->next_callee)
1616 enum availability avail;
1618 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1619 continue;
1621 struct cgraph_node *y = e->callee->
1622 function_or_virtual_thunk_symbol (&avail,
1623 e->caller);
1625 /* We can use info about the callee only if we know it can
1626 not be interposed.
1627 When callee is compiled with non-call exceptions we also
1628 must check that the declaration is bound to current
1629 body as other semantically equivalent body may still
1630 throw. */
1631 if (avail <= AVAIL_INTERPOSABLE
1632 || (!TREE_NOTHROW (y->decl)
1633 && (get_function_state (y)->can_throw
1634 || (opt_for_fn (y->decl, flag_non_call_exceptions)
1635 && !e->callee->binds_to_current_def_p (w)))))
1636 can_throw = true;
1638 for (ie = w->indirect_calls; ie && !can_throw;
1639 ie = ie->next_callee)
1640 if (ie->can_throw_external
1641 && !(ie->indirect_info->ecf_flags & ECF_NOTHROW))
1642 can_throw = true;
1644 w_info = (struct ipa_dfs_info *) w->aux;
1645 w = w_info->next_cycle;
1648 /* Copy back the region's pure_const_state which is shared by
1649 all nodes in the region. */
1650 w = node;
1651 while (w)
1653 funct_state w_l = get_function_state (w);
1654 if (!can_throw && !TREE_NOTHROW (w->decl))
1656 /* Inline clones share declaration with their offline copies;
1657 do not modify their declarations since the offline copy may
1658 be different. */
1659 if (!w->global.inlined_to)
1661 w->set_nothrow_flag (true);
1662 if (dump_file)
1663 fprintf (dump_file, "Function found to be nothrow: %s\n",
1664 w->name ());
1667 else if (can_throw && !TREE_NOTHROW (w->decl))
1668 w_l->can_throw = true;
1669 w_info = (struct ipa_dfs_info *) w->aux;
1670 w = w_info->next_cycle;
1674 ipa_free_postorder_info ();
1675 free (order);
1679 /* Produce the global information by preforming a transitive closure
1680 on the local information that was produced by generate_summary. */
1682 unsigned int
1683 pass_ipa_pure_const::
1684 execute (function *)
1686 struct cgraph_node *node;
1687 bool remove_p;
1689 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1690 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1691 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1693 /* Nothrow makes more function to not lead to return and improve
1694 later analysis. */
1695 propagate_nothrow ();
1696 remove_p = propagate_pure_const ();
1698 /* Cleanup. */
1699 FOR_EACH_FUNCTION (node)
1700 if (has_function_state (node))
1701 free (get_function_state (node));
1702 funct_state_vec.release ();
1703 return remove_p ? TODO_remove_functions : 0;
1706 static bool
1707 gate_pure_const (void)
1709 return flag_ipa_pure_const || in_lto_p;
1712 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1713 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1714 pure_const_generate_summary, /* generate_summary */
1715 pure_const_write_summary, /* write_summary */
1716 pure_const_read_summary, /* read_summary */
1717 NULL, /* write_optimization_summary */
1718 NULL, /* read_optimization_summary */
1719 NULL, /* stmt_fixup */
1720 0, /* function_transform_todo_flags_start */
1721 NULL, /* function_transform */
1722 NULL), /* variable_transform */
1723 init_p(false),
1724 function_insertion_hook_holder(NULL),
1725 node_duplication_hook_holder(NULL),
1726 node_removal_hook_holder(NULL)
1730 ipa_opt_pass_d *
1731 make_pass_ipa_pure_const (gcc::context *ctxt)
1733 return new pass_ipa_pure_const (ctxt);
1736 /* Return true if function should be skipped for local pure const analysis. */
1738 static bool
1739 skip_function_for_local_pure_const (struct cgraph_node *node)
1741 /* Because we do not schedule pass_fixup_cfg over whole program after early
1742 optimizations we must not promote functions that are called by already
1743 processed functions. */
1745 if (function_called_by_processed_nodes_p ())
1747 if (dump_file)
1748 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1749 return true;
1751 /* Save some work and do not analyze functions which are interposable and
1752 do not have any non-interposable aliases. */
1753 if (node->get_availability () <= AVAIL_INTERPOSABLE
1754 && !node->has_aliases_p ())
1756 if (dump_file)
1757 fprintf (dump_file,
1758 "Function is interposable; not analyzing.\n");
1759 return true;
1761 return false;
1764 /* Simple local pass for pure const discovery reusing the analysis from
1765 ipa_pure_const. This pass is effective when executed together with
1766 other optimization passes in early optimization pass queue. */
1768 namespace {
1770 const pass_data pass_data_local_pure_const =
1772 GIMPLE_PASS, /* type */
1773 "local-pure-const", /* name */
1774 OPTGROUP_NONE, /* optinfo_flags */
1775 TV_IPA_PURE_CONST, /* tv_id */
1776 0, /* properties_required */
1777 0, /* properties_provided */
1778 0, /* properties_destroyed */
1779 0, /* todo_flags_start */
1780 0, /* todo_flags_finish */
1783 class pass_local_pure_const : public gimple_opt_pass
1785 public:
1786 pass_local_pure_const (gcc::context *ctxt)
1787 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1790 /* opt_pass methods: */
1791 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1792 virtual bool gate (function *) { return gate_pure_const (); }
1793 virtual unsigned int execute (function *);
1795 }; // class pass_local_pure_const
1797 unsigned int
1798 pass_local_pure_const::execute (function *fun)
1800 bool changed = false;
1801 funct_state l;
1802 bool skip;
1803 struct cgraph_node *node;
1805 node = cgraph_node::get (current_function_decl);
1806 skip = skip_function_for_local_pure_const (node);
1808 if (!warn_suggest_attribute_const
1809 && !warn_suggest_attribute_pure
1810 && skip)
1811 return 0;
1813 l = analyze_function (node, false);
1815 /* Do NORETURN discovery. */
1816 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1817 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1819 warn_function_noreturn (fun->decl);
1820 if (dump_file)
1821 fprintf (dump_file, "Function found to be noreturn: %s\n",
1822 current_function_name ());
1824 /* Update declaration and reduce profile to executed once. */
1825 TREE_THIS_VOLATILE (current_function_decl) = 1;
1826 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1827 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1829 changed = true;
1832 switch (l->pure_const_state)
1834 case IPA_CONST:
1835 if (!TREE_READONLY (current_function_decl))
1837 warn_function_const (current_function_decl, !l->looping);
1838 if (dump_file)
1839 fprintf (dump_file, "Function found to be %sconst: %s\n",
1840 l->looping ? "looping " : "",
1841 current_function_name ());
1843 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1844 && !l->looping)
1846 if (dump_file)
1847 fprintf (dump_file, "Function found to be non-looping: %s\n",
1848 current_function_name ());
1850 if (!skip && node->set_const_flag (true, l->looping))
1852 if (dump_file)
1853 fprintf (dump_file, "Declaration updated to be %sconst: %s\n",
1854 l->looping ? "looping " : "",
1855 current_function_name ());
1856 changed = true;
1858 break;
1860 case IPA_PURE:
1861 if (!DECL_PURE_P (current_function_decl))
1863 warn_function_pure (current_function_decl, !l->looping);
1864 if (dump_file)
1865 fprintf (dump_file, "Function found to be %spure: %s\n",
1866 l->looping ? "looping " : "",
1867 current_function_name ());
1869 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1870 && !l->looping)
1872 if (dump_file)
1873 fprintf (dump_file, "Function found to be non-looping: %s\n",
1874 current_function_name ());
1876 if (!skip && node->set_pure_flag (true, l->looping))
1878 if (dump_file)
1879 fprintf (dump_file, "Declaration updated to be %spure: %s\n",
1880 l->looping ? "looping " : "",
1881 current_function_name ());
1882 changed = true;
1884 break;
1886 default:
1887 break;
1889 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1891 node->set_nothrow_flag (true);
1892 changed = true;
1893 if (dump_file)
1894 fprintf (dump_file, "Function found to be nothrow: %s\n",
1895 current_function_name ());
1897 free (l);
1898 if (changed)
1899 return execute_fixup_cfg ();
1900 else
1901 return 0;
1904 } // anon namespace
1906 gimple_opt_pass *
1907 make_pass_local_pure_const (gcc::context *ctxt)
1909 return new pass_local_pure_const (ctxt);
1912 /* Emit noreturn warnings. */
1914 namespace {
1916 const pass_data pass_data_warn_function_noreturn =
1918 GIMPLE_PASS, /* type */
1919 "*warn_function_noreturn", /* name */
1920 OPTGROUP_NONE, /* optinfo_flags */
1921 TV_NONE, /* tv_id */
1922 PROP_cfg, /* properties_required */
1923 0, /* properties_provided */
1924 0, /* properties_destroyed */
1925 0, /* todo_flags_start */
1926 0, /* todo_flags_finish */
1929 class pass_warn_function_noreturn : public gimple_opt_pass
1931 public:
1932 pass_warn_function_noreturn (gcc::context *ctxt)
1933 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1936 /* opt_pass methods: */
1937 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1938 virtual unsigned int execute (function *fun)
1940 if (!TREE_THIS_VOLATILE (current_function_decl)
1941 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1942 warn_function_noreturn (current_function_decl);
1943 return 0;
1946 }; // class pass_warn_function_noreturn
1948 } // anon namespace
1950 gimple_opt_pass *
1951 make_pass_warn_function_noreturn (gcc::context *ctxt)
1953 return new pass_warn_function_noreturn (ctxt);
1956 /* Simple local pass for pure const discovery reusing the analysis from
1957 ipa_pure_const. This pass is effective when executed together with
1958 other optimization passes in early optimization pass queue. */
1960 namespace {
1962 const pass_data pass_data_nothrow =
1964 GIMPLE_PASS, /* type */
1965 "nothrow", /* name */
1966 OPTGROUP_NONE, /* optinfo_flags */
1967 TV_IPA_PURE_CONST, /* tv_id */
1968 0, /* properties_required */
1969 0, /* properties_provided */
1970 0, /* properties_destroyed */
1971 0, /* todo_flags_start */
1972 0, /* todo_flags_finish */
1975 class pass_nothrow : public gimple_opt_pass
1977 public:
1978 pass_nothrow (gcc::context *ctxt)
1979 : gimple_opt_pass (pass_data_nothrow, ctxt)
1982 /* opt_pass methods: */
1983 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1984 virtual bool gate (function *) { return optimize; }
1985 virtual unsigned int execute (function *);
1987 }; // class pass_nothrow
1989 unsigned int
1990 pass_nothrow::execute (function *)
1992 struct cgraph_node *node;
1993 basic_block this_block;
1995 if (TREE_NOTHROW (current_function_decl))
1996 return 0;
1998 node = cgraph_node::get (current_function_decl);
2000 /* We run during lowering, we can not really use availability yet. */
2001 if (cgraph_node::get (current_function_decl)->get_availability ()
2002 <= AVAIL_INTERPOSABLE)
2004 if (dump_file)
2005 fprintf (dump_file, "Function is interposable;"
2006 " not analyzing.\n");
2007 return true;
2010 FOR_EACH_BB_FN (this_block, cfun)
2012 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
2013 !gsi_end_p (gsi);
2014 gsi_next (&gsi))
2015 if (stmt_can_throw_external (gsi_stmt (gsi)))
2017 if (is_gimple_call (gsi_stmt (gsi)))
2019 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
2020 if (callee_t && recursive_call_p (current_function_decl,
2021 callee_t))
2022 continue;
2025 if (dump_file)
2027 fprintf (dump_file, "Statement can throw: ");
2028 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
2030 return 0;
2034 node->set_nothrow_flag (true);
2036 bool cfg_changed = false;
2037 if (self_recursive_p (node))
2038 FOR_EACH_BB_FN (this_block, cfun)
2039 if (gimple *g = last_stmt (this_block))
2040 if (is_gimple_call (g))
2042 tree callee_t = gimple_call_fndecl (g);
2043 if (callee_t
2044 && recursive_call_p (current_function_decl, callee_t)
2045 && maybe_clean_eh_stmt (g)
2046 && gimple_purge_dead_eh_edges (this_block))
2047 cfg_changed = true;
2050 if (dump_file)
2051 fprintf (dump_file, "Function found to be nothrow: %s\n",
2052 current_function_name ());
2053 return cfg_changed ? TODO_cleanup_cfg : 0;
2056 } // anon namespace
2058 gimple_opt_pass *
2059 make_pass_nothrow (gcc::context *ctxt)
2061 return new pass_nothrow (ctxt);