* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / gcc / ipa-pure-const.c
blob9732cbff12e401d183a08678c3f0fad359751430
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2016 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 ? _("function might be candidate for attribute %<%s%>")
188 : _("function might be candidate for attribute %<%s%>"
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 static hash_set<tree> *warned_about;
222 if (!lang_hooks.missing_noreturn_ok_p (decl)
223 && targetm.warn_func_return (decl))
224 warned_about
225 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
226 true, warned_about, "noreturn");
229 /* Return true if we have a function state for NODE. */
231 static inline bool
232 has_function_state (struct cgraph_node *node)
234 if (!funct_state_vec.exists ()
235 || funct_state_vec.length () <= (unsigned int)node->uid)
236 return false;
237 return funct_state_vec[node->uid] != NULL;
240 /* Return the function state from NODE. */
242 static inline funct_state
243 get_function_state (struct cgraph_node *node)
245 if (!funct_state_vec.exists ()
246 || funct_state_vec.length () <= (unsigned int)node->uid
247 || !funct_state_vec[node->uid])
248 /* We might want to put correct previously_known state into varying. */
249 return &varying_state;
250 return funct_state_vec[node->uid];
253 /* Set the function state S for NODE. */
255 static inline void
256 set_function_state (struct cgraph_node *node, funct_state s)
258 if (!funct_state_vec.exists ()
259 || funct_state_vec.length () <= (unsigned int)node->uid)
260 funct_state_vec.safe_grow_cleared (node->uid + 1);
262 /* If funct_state_vec already contains a funct_state, we have to release
263 it before it's going to be ovewritten. */
264 if (funct_state_vec[node->uid] != NULL
265 && funct_state_vec[node->uid] != &varying_state)
266 free (funct_state_vec[node->uid]);
268 funct_state_vec[node->uid] = s;
271 /* Check to see if the use (or definition when CHECKING_WRITE is true)
272 variable T is legal in a function that is either pure or const. */
274 static inline void
275 check_decl (funct_state local,
276 tree t, bool checking_write, bool ipa)
278 /* Do not want to do anything with volatile except mark any
279 function that uses one to be not const or pure. */
280 if (TREE_THIS_VOLATILE (t))
282 local->pure_const_state = IPA_NEITHER;
283 if (dump_file)
284 fprintf (dump_file, " Volatile operand is not const/pure");
285 return;
288 /* Do not care about a local automatic that is not static. */
289 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
290 return;
292 /* If the variable has the "used" attribute, treat it as if it had a
293 been touched by the devil. */
294 if (DECL_PRESERVE_P (t))
296 local->pure_const_state = IPA_NEITHER;
297 if (dump_file)
298 fprintf (dump_file, " Used static/global variable is not const/pure\n");
299 return;
302 /* In IPA mode we are not interested in checking actual loads and stores;
303 they will be processed at propagation time using ipa_ref. */
304 if (ipa)
305 return;
307 /* Since we have dealt with the locals and params cases above, if we
308 are CHECKING_WRITE, this cannot be a pure or constant
309 function. */
310 if (checking_write)
312 local->pure_const_state = IPA_NEITHER;
313 if (dump_file)
314 fprintf (dump_file, " static/global memory write is not const/pure\n");
315 return;
318 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
320 /* Readonly reads are safe. */
321 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
322 return; /* Read of a constant, do not change the function state. */
323 else
325 if (dump_file)
326 fprintf (dump_file, " global memory read is not const\n");
327 /* Just a regular read. */
328 if (local->pure_const_state == IPA_CONST)
329 local->pure_const_state = IPA_PURE;
332 else
334 /* Compilation level statics can be read if they are readonly
335 variables. */
336 if (TREE_READONLY (t))
337 return;
339 if (dump_file)
340 fprintf (dump_file, " static memory read is not const\n");
341 /* Just a regular read. */
342 if (local->pure_const_state == IPA_CONST)
343 local->pure_const_state = IPA_PURE;
348 /* Check to see if the use (or definition when CHECKING_WRITE is true)
349 variable T is legal in a function that is either pure or const. */
351 static inline void
352 check_op (funct_state local, tree t, bool checking_write)
354 t = get_base_address (t);
355 if (t && TREE_THIS_VOLATILE (t))
357 local->pure_const_state = IPA_NEITHER;
358 if (dump_file)
359 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
360 return;
362 else if (t
363 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
364 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
365 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
367 if (dump_file)
368 fprintf (dump_file, " Indirect ref to local memory is OK\n");
369 return;
371 else if (checking_write)
373 local->pure_const_state = IPA_NEITHER;
374 if (dump_file)
375 fprintf (dump_file, " Indirect ref write is not const/pure\n");
376 return;
378 else
380 if (dump_file)
381 fprintf (dump_file, " Indirect ref read is not const\n");
382 if (local->pure_const_state == IPA_CONST)
383 local->pure_const_state = IPA_PURE;
387 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
389 static void
390 state_from_flags (enum pure_const_state_e *state, bool *looping,
391 int flags, bool cannot_lead_to_return)
393 *looping = false;
394 if (flags & ECF_LOOPING_CONST_OR_PURE)
396 *looping = true;
397 if (dump_file && (dump_flags & TDF_DETAILS))
398 fprintf (dump_file, " looping");
400 if (flags & ECF_CONST)
402 *state = IPA_CONST;
403 if (dump_file && (dump_flags & TDF_DETAILS))
404 fprintf (dump_file, " const\n");
406 else if (flags & ECF_PURE)
408 *state = IPA_PURE;
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, " pure\n");
412 else if (cannot_lead_to_return)
414 *state = IPA_PURE;
415 *looping = true;
416 if (dump_file && (dump_flags & TDF_DETAILS))
417 fprintf (dump_file, " ignoring side effects->pure looping\n");
419 else
421 if (dump_file && (dump_flags & TDF_DETAILS))
422 fprintf (dump_file, " neither\n");
423 *state = IPA_NEITHER;
424 *looping = true;
428 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
429 into STATE and LOOPING better of the two variants.
430 Be sure to merge looping correctly. IPA_NEITHER functions
431 have looping 0 even if they don't have to return. */
433 static inline void
434 better_state (enum pure_const_state_e *state, bool *looping,
435 enum pure_const_state_e state2, bool looping2)
437 if (state2 < *state)
439 if (*state == IPA_NEITHER)
440 *looping = looping2;
441 else
442 *looping = MIN (*looping, looping2);
443 *state = state2;
445 else if (state2 != IPA_NEITHER)
446 *looping = MIN (*looping, looping2);
449 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
450 into STATE and LOOPING worse of the two variants.
451 N is the actual node called. */
453 static inline void
454 worse_state (enum pure_const_state_e *state, bool *looping,
455 enum pure_const_state_e state2, bool looping2,
456 struct symtab_node *from,
457 struct symtab_node *to)
459 /* Consider function:
461 bool a(int *p)
463 return *p==*p;
466 During early optimization we will turn this into:
468 bool a(int *p)
470 return true;
473 Now if this function will be detected as CONST however when interposed it
474 may end up being just pure. We always must assume the worst scenario here.
476 if (*state == IPA_CONST && state2 == IPA_CONST
477 && to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from))
479 if (dump_file && (dump_flags & TDF_DETAILS))
480 fprintf (dump_file, "Dropping state to PURE because call to %s may not "
481 "bind to current def.\n", to->name ());
482 state2 = IPA_PURE;
484 *state = MAX (*state, state2);
485 *looping = MAX (*looping, looping2);
488 /* Recognize special cases of builtins that are by themselves not pure or const
489 but function using them is. */
490 static bool
491 special_builtin_state (enum pure_const_state_e *state, bool *looping,
492 tree callee)
494 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
495 switch (DECL_FUNCTION_CODE (callee))
497 case BUILT_IN_RETURN:
498 case BUILT_IN_UNREACHABLE:
499 case BUILT_IN_ALLOCA:
500 case BUILT_IN_ALLOCA_WITH_ALIGN:
501 case BUILT_IN_STACK_SAVE:
502 case BUILT_IN_STACK_RESTORE:
503 case BUILT_IN_EH_POINTER:
504 case BUILT_IN_EH_FILTER:
505 case BUILT_IN_UNWIND_RESUME:
506 case BUILT_IN_CXA_END_CLEANUP:
507 case BUILT_IN_EH_COPY_VALUES:
508 case BUILT_IN_FRAME_ADDRESS:
509 case BUILT_IN_APPLY:
510 case BUILT_IN_APPLY_ARGS:
511 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
512 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
513 *looping = false;
514 *state = IPA_CONST;
515 return true;
516 case BUILT_IN_PREFETCH:
517 *looping = true;
518 *state = IPA_CONST;
519 return true;
520 default:
521 break;
523 return false;
526 /* Check the parameters of a function call to CALL_EXPR to see if
527 there are any references in the parameters that are not allowed for
528 pure or const functions. Also check to see if this is either an
529 indirect call, a call outside the compilation unit, or has special
530 attributes that may also effect the purity. The CALL_EXPR node for
531 the entire call expression. */
533 static void
534 check_call (funct_state local, gcall *call, bool ipa)
536 int flags = gimple_call_flags (call);
537 tree callee_t = gimple_call_fndecl (call);
538 bool possibly_throws = stmt_could_throw_p (call);
539 bool possibly_throws_externally = (possibly_throws
540 && stmt_can_throw_external (call));
542 if (possibly_throws)
544 unsigned int i;
545 for (i = 0; i < gimple_num_ops (call); i++)
546 if (gimple_op (call, i)
547 && tree_could_throw_p (gimple_op (call, i)))
549 if (possibly_throws && cfun->can_throw_non_call_exceptions)
551 if (dump_file)
552 fprintf (dump_file, " operand can throw; looping\n");
553 local->looping = true;
555 if (possibly_throws_externally)
557 if (dump_file)
558 fprintf (dump_file, " operand can throw externally\n");
559 local->can_throw = true;
564 /* The const and pure flags are set by a variety of places in the
565 compiler (including here). If someone has already set the flags
566 for the callee, (such as for some of the builtins) we will use
567 them, otherwise we will compute our own information.
569 Const and pure functions have less clobber effects than other
570 functions so we process these first. Otherwise if it is a call
571 outside the compilation unit or an indirect call we punt. This
572 leaves local calls which will be processed by following the call
573 graph. */
574 if (callee_t)
576 enum pure_const_state_e call_state;
577 bool call_looping;
579 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
580 && !nonfreeing_call_p (call))
581 local->can_free = true;
583 if (special_builtin_state (&call_state, &call_looping, callee_t))
585 worse_state (&local->pure_const_state, &local->looping,
586 call_state, call_looping,
587 NULL, NULL);
588 return;
590 /* When bad things happen to bad functions, they cannot be const
591 or pure. */
592 if (setjmp_call_p (callee_t))
594 if (dump_file)
595 fprintf (dump_file, " setjmp is not const/pure\n");
596 local->looping = true;
597 local->pure_const_state = IPA_NEITHER;
600 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
601 switch (DECL_FUNCTION_CODE (callee_t))
603 case BUILT_IN_LONGJMP:
604 case BUILT_IN_NONLOCAL_GOTO:
605 if (dump_file)
606 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
607 local->pure_const_state = IPA_NEITHER;
608 local->looping = true;
609 break;
610 default:
611 break;
614 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
615 local->can_free = true;
617 /* When not in IPA mode, we can still handle self recursion. */
618 if (!ipa && callee_t
619 && recursive_call_p (current_function_decl, callee_t))
621 if (dump_file)
622 fprintf (dump_file, " Recursive call can loop.\n");
623 local->looping = true;
625 /* Either callee is unknown or we are doing local analysis.
626 Look to see if there are any bits available for the callee (such as by
627 declaration or because it is builtin) and process solely on the basis of
628 those bits. Handle internal calls always, those calls don't have
629 corresponding cgraph edges and thus aren't processed during
630 the propagation. */
631 else if (!ipa || gimple_call_internal_p (call))
633 enum pure_const_state_e call_state;
634 bool call_looping;
635 if (possibly_throws && cfun->can_throw_non_call_exceptions)
637 if (dump_file)
638 fprintf (dump_file, " can throw; looping\n");
639 local->looping = true;
641 if (possibly_throws_externally)
643 if (dump_file)
645 fprintf (dump_file, " can throw externally to lp %i\n",
646 lookup_stmt_eh_lp (call));
647 if (callee_t)
648 fprintf (dump_file, " callee:%s\n",
649 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
651 local->can_throw = true;
653 if (dump_file && (dump_flags & TDF_DETAILS))
654 fprintf (dump_file, " checking flags for call:");
655 state_from_flags (&call_state, &call_looping, flags,
656 ((flags & (ECF_NORETURN | ECF_NOTHROW))
657 == (ECF_NORETURN | ECF_NOTHROW))
658 || (!flag_exceptions && (flags & ECF_NORETURN)));
659 worse_state (&local->pure_const_state, &local->looping,
660 call_state, call_looping, NULL, NULL);
662 /* Direct functions calls are handled by IPA propagation. */
665 /* Wrapper around check_decl for loads in local more. */
667 static bool
668 check_load (gimple *, tree op, tree, void *data)
670 if (DECL_P (op))
671 check_decl ((funct_state)data, op, false, false);
672 else
673 check_op ((funct_state)data, op, false);
674 return false;
677 /* Wrapper around check_decl for stores in local more. */
679 static bool
680 check_store (gimple *, tree op, tree, void *data)
682 if (DECL_P (op))
683 check_decl ((funct_state)data, op, true, false);
684 else
685 check_op ((funct_state)data, op, true);
686 return false;
689 /* Wrapper around check_decl for loads in ipa mode. */
691 static bool
692 check_ipa_load (gimple *, tree op, tree, void *data)
694 if (DECL_P (op))
695 check_decl ((funct_state)data, op, false, true);
696 else
697 check_op ((funct_state)data, op, false);
698 return false;
701 /* Wrapper around check_decl for stores in ipa mode. */
703 static bool
704 check_ipa_store (gimple *, tree op, tree, void *data)
706 if (DECL_P (op))
707 check_decl ((funct_state)data, op, true, true);
708 else
709 check_op ((funct_state)data, op, true);
710 return false;
713 /* Look into pointer pointed to by GSIP and figure out what interesting side
714 effects it has. */
715 static void
716 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
718 gimple *stmt = gsi_stmt (*gsip);
720 if (is_gimple_debug (stmt))
721 return;
723 /* Do consider clobber as side effects before IPA, so we rather inline
724 C++ destructors and keep clobber semantics than eliminate them.
726 TODO: We may get smarter during early optimizations on these and let
727 functions containing only clobbers to be optimized more. This is a common
728 case of C++ destructors. */
730 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
731 return;
733 if (dump_file)
735 fprintf (dump_file, " scanning: ");
736 print_gimple_stmt (dump_file, stmt, 0, 0);
739 if (gimple_has_volatile_ops (stmt)
740 && !gimple_clobber_p (stmt))
742 local->pure_const_state = IPA_NEITHER;
743 if (dump_file)
744 fprintf (dump_file, " Volatile stmt is not const/pure\n");
747 /* Look for loads and stores. */
748 walk_stmt_load_store_ops (stmt, local,
749 ipa ? check_ipa_load : check_load,
750 ipa ? check_ipa_store : check_store);
752 if (gimple_code (stmt) != GIMPLE_CALL
753 && stmt_could_throw_p (stmt))
755 if (cfun->can_throw_non_call_exceptions)
757 if (dump_file)
758 fprintf (dump_file, " can throw; looping\n");
759 local->looping = true;
761 if (stmt_can_throw_external (stmt))
763 if (dump_file)
764 fprintf (dump_file, " can throw externally\n");
765 local->can_throw = true;
767 else
768 if (dump_file)
769 fprintf (dump_file, " can throw\n");
771 switch (gimple_code (stmt))
773 case GIMPLE_CALL:
774 check_call (local, as_a <gcall *> (stmt), ipa);
775 break;
776 case GIMPLE_LABEL:
777 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
778 /* Target of long jump. */
780 if (dump_file)
781 fprintf (dump_file, " nonlocal label is not const/pure\n");
782 local->pure_const_state = IPA_NEITHER;
784 break;
785 case GIMPLE_ASM:
786 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
788 if (dump_file)
789 fprintf (dump_file, " memory asm clobber is not const/pure\n");
790 /* Abandon all hope, ye who enter here. */
791 local->pure_const_state = IPA_NEITHER;
792 local->can_free = true;
794 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
796 if (dump_file)
797 fprintf (dump_file, " volatile is not const/pure\n");
798 /* Abandon all hope, ye who enter here. */
799 local->pure_const_state = IPA_NEITHER;
800 local->looping = true;
801 local->can_free = true;
803 return;
804 default:
805 break;
810 /* This is the main routine for finding the reference patterns for
811 global variables within a function FN. */
813 static funct_state
814 analyze_function (struct cgraph_node *fn, bool ipa)
816 tree decl = fn->decl;
817 funct_state l;
818 basic_block this_block;
820 l = XCNEW (struct funct_state_d);
821 l->pure_const_state = IPA_CONST;
822 l->state_previously_known = IPA_NEITHER;
823 l->looping_previously_known = true;
824 l->looping = false;
825 l->can_throw = false;
826 l->can_free = false;
827 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
828 flags_from_decl_or_type (fn->decl),
829 fn->cannot_return_p ());
831 if (fn->thunk.thunk_p || fn->alias)
833 /* Thunk gets propagated through, so nothing interesting happens. */
834 gcc_assert (ipa);
835 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
836 l->pure_const_state = IPA_NEITHER;
837 return l;
840 if (dump_file)
842 fprintf (dump_file, "\n\n local analysis of %s\n ",
843 fn->name ());
846 push_cfun (DECL_STRUCT_FUNCTION (decl));
848 FOR_EACH_BB_FN (this_block, cfun)
850 gimple_stmt_iterator gsi;
851 struct walk_stmt_info wi;
853 memset (&wi, 0, sizeof (wi));
854 for (gsi = gsi_start_bb (this_block);
855 !gsi_end_p (gsi);
856 gsi_next (&gsi))
858 check_stmt (&gsi, l, ipa);
859 if (l->pure_const_state == IPA_NEITHER
860 && l->looping
861 && l->can_throw
862 && l->can_free)
863 goto end;
867 end:
868 if (l->pure_const_state != IPA_NEITHER)
870 /* Const functions cannot have back edges (an
871 indication of possible infinite loop side
872 effect. */
873 if (mark_dfs_back_edges ())
875 /* Preheaders are needed for SCEV to work.
876 Simple latches and recorded exits improve chances that loop will
877 proved to be finite in testcases such as in loop-15.c
878 and loop-24.c */
879 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
880 | LOOPS_HAVE_SIMPLE_LATCHES
881 | LOOPS_HAVE_RECORDED_EXITS);
882 if (dump_file && (dump_flags & TDF_DETAILS))
883 flow_loops_dump (dump_file, NULL, 0);
884 if (mark_irreducible_loops ())
886 if (dump_file)
887 fprintf (dump_file, " has irreducible loops\n");
888 l->looping = true;
890 else
892 struct loop *loop;
893 scev_initialize ();
894 FOR_EACH_LOOP (loop, 0)
895 if (!finite_loop_p (loop))
897 if (dump_file)
898 fprintf (dump_file, " can not prove finiteness of "
899 "loop %i\n", loop->num);
900 l->looping =true;
901 break;
903 scev_finalize ();
905 loop_optimizer_finalize ();
909 if (dump_file && (dump_flags & TDF_DETAILS))
910 fprintf (dump_file, " checking previously known:");
912 better_state (&l->pure_const_state, &l->looping,
913 l->state_previously_known,
914 l->looping_previously_known);
915 if (TREE_NOTHROW (decl))
916 l->can_throw = false;
918 pop_cfun ();
919 if (dump_file)
921 if (l->looping)
922 fprintf (dump_file, "Function is locally looping.\n");
923 if (l->can_throw)
924 fprintf (dump_file, "Function is locally throwing.\n");
925 if (l->pure_const_state == IPA_CONST)
926 fprintf (dump_file, "Function is locally const.\n");
927 if (l->pure_const_state == IPA_PURE)
928 fprintf (dump_file, "Function is locally pure.\n");
929 if (l->can_free)
930 fprintf (dump_file, "Function can locally free.\n");
932 return l;
935 /* Called when new function is inserted to callgraph late. */
936 static void
937 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
939 /* There are some shared nodes, in particular the initializers on
940 static declarations. We do not need to scan them more than once
941 since all we would be interested in are the addressof
942 operations. */
943 if (opt_for_fn (node->decl, flag_ipa_pure_const))
944 set_function_state (node, analyze_function (node, true));
947 /* Called when new clone is inserted to callgraph late. */
949 static void
950 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
951 void *data ATTRIBUTE_UNUSED)
953 if (has_function_state (src))
955 funct_state l = XNEW (struct funct_state_d);
956 gcc_assert (!has_function_state (dst));
957 memcpy (l, get_function_state (src), sizeof (*l));
958 set_function_state (dst, l);
962 /* Called when new clone is inserted to callgraph late. */
964 static void
965 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
967 if (has_function_state (node))
968 set_function_state (node, NULL);
972 void
973 pass_ipa_pure_const::
974 register_hooks (void)
976 if (init_p)
977 return;
979 init_p = true;
981 node_removal_hook_holder =
982 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
983 node_duplication_hook_holder =
984 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
985 function_insertion_hook_holder =
986 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
990 /* Analyze each function in the cgraph to see if it is locally PURE or
991 CONST. */
993 static void
994 pure_const_generate_summary (void)
996 struct cgraph_node *node;
998 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
999 pass->register_hooks ();
1001 /* Process all of the functions.
1003 We process AVAIL_INTERPOSABLE functions. We can not use the results
1004 by default, but the info can be used at LTO with -fwhole-program or
1005 when function got cloned and the clone is AVAILABLE. */
1007 FOR_EACH_DEFINED_FUNCTION (node)
1008 if (opt_for_fn (node->decl, flag_ipa_pure_const))
1009 set_function_state (node, analyze_function (node, true));
1013 /* Serialize the ipa info for lto. */
1015 static void
1016 pure_const_write_summary (void)
1018 struct cgraph_node *node;
1019 struct lto_simple_output_block *ob
1020 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1021 unsigned int count = 0;
1022 lto_symtab_encoder_iterator lsei;
1023 lto_symtab_encoder_t encoder;
1025 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1027 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1028 lsei_next_function_in_partition (&lsei))
1030 node = lsei_cgraph_node (lsei);
1031 if (node->definition && has_function_state (node))
1032 count++;
1035 streamer_write_uhwi_stream (ob->main_stream, count);
1037 /* Process all of the functions. */
1038 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1039 lsei_next_function_in_partition (&lsei))
1041 node = lsei_cgraph_node (lsei);
1042 if (node->definition && has_function_state (node))
1044 struct bitpack_d bp;
1045 funct_state fs;
1046 int node_ref;
1047 lto_symtab_encoder_t encoder;
1049 fs = get_function_state (node);
1051 encoder = ob->decl_state->symtab_node_encoder;
1052 node_ref = lto_symtab_encoder_encode (encoder, node);
1053 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1055 /* Note that flags will need to be read in the opposite
1056 order as we are pushing the bitflags into FLAGS. */
1057 bp = bitpack_create (ob->main_stream);
1058 bp_pack_value (&bp, fs->pure_const_state, 2);
1059 bp_pack_value (&bp, fs->state_previously_known, 2);
1060 bp_pack_value (&bp, fs->looping_previously_known, 1);
1061 bp_pack_value (&bp, fs->looping, 1);
1062 bp_pack_value (&bp, fs->can_throw, 1);
1063 bp_pack_value (&bp, fs->can_free, 1);
1064 streamer_write_bitpack (&bp);
1068 lto_destroy_simple_output_block (ob);
1072 /* Deserialize the ipa info for lto. */
1074 static void
1075 pure_const_read_summary (void)
1077 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1078 struct lto_file_decl_data *file_data;
1079 unsigned int j = 0;
1081 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1082 pass->register_hooks ();
1084 while ((file_data = file_data_vec[j++]))
1086 const char *data;
1087 size_t len;
1088 struct lto_input_block *ib
1089 = lto_create_simple_input_block (file_data,
1090 LTO_section_ipa_pure_const,
1091 &data, &len);
1092 if (ib)
1094 unsigned int i;
1095 unsigned int count = streamer_read_uhwi (ib);
1097 for (i = 0; i < count; i++)
1099 unsigned int index;
1100 struct cgraph_node *node;
1101 struct bitpack_d bp;
1102 funct_state fs;
1103 lto_symtab_encoder_t encoder;
1105 fs = XCNEW (struct funct_state_d);
1106 index = streamer_read_uhwi (ib);
1107 encoder = file_data->symtab_node_encoder;
1108 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1109 index));
1110 set_function_state (node, fs);
1112 /* Note that the flags must be read in the opposite
1113 order in which they were written (the bitflags were
1114 pushed into FLAGS). */
1115 bp = streamer_read_bitpack (ib);
1116 fs->pure_const_state
1117 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1118 fs->state_previously_known
1119 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1120 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1121 fs->looping = bp_unpack_value (&bp, 1);
1122 fs->can_throw = bp_unpack_value (&bp, 1);
1123 fs->can_free = bp_unpack_value (&bp, 1);
1124 if (dump_file)
1126 int flags = flags_from_decl_or_type (node->decl);
1127 fprintf (dump_file, "Read info for %s/%i ",
1128 node->name (),
1129 node->order);
1130 if (flags & ECF_CONST)
1131 fprintf (dump_file, " const");
1132 if (flags & ECF_PURE)
1133 fprintf (dump_file, " pure");
1134 if (flags & ECF_NOTHROW)
1135 fprintf (dump_file, " nothrow");
1136 fprintf (dump_file, "\n pure const state: %s\n",
1137 pure_const_names[fs->pure_const_state]);
1138 fprintf (dump_file, " previously known state: %s\n",
1139 pure_const_names[fs->state_previously_known]);
1140 if (fs->looping)
1141 fprintf (dump_file," function is locally looping\n");
1142 if (fs->looping_previously_known)
1143 fprintf (dump_file," function is previously known looping\n");
1144 if (fs->can_throw)
1145 fprintf (dump_file," function is locally throwing\n");
1146 if (fs->can_free)
1147 fprintf (dump_file," function can locally free\n");
1151 lto_destroy_simple_input_block (file_data,
1152 LTO_section_ipa_pure_const,
1153 ib, data, len);
1158 /* We only propagate across edges that can throw externally and their callee
1159 is not interposable. */
1161 static bool
1162 ignore_edge_for_nothrow (struct cgraph_edge *e)
1164 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1165 return true;
1167 enum availability avail;
1168 cgraph_node *n = e->callee->function_or_virtual_thunk_symbol (&avail,
1169 e->caller);
1170 if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (n->decl))
1171 return true;
1172 return opt_for_fn (e->callee->decl, flag_non_call_exceptions)
1173 && !e->callee->binds_to_current_def_p (e->caller);
1176 /* Return true if NODE is self recursive function.
1177 Indirectly recursive functions appears as non-trivial strongly
1178 connected components, so we need to care about self recursion
1179 only. */
1181 static bool
1182 self_recursive_p (struct cgraph_node *node)
1184 struct cgraph_edge *e;
1185 for (e = node->callees; e; e = e->next_callee)
1186 if (e->callee->function_symbol () == node)
1187 return true;
1188 return false;
1191 /* Return true if N is cdtor that is not const or pure. In this case we may
1192 need to remove unreachable function if it is marked const/pure. */
1194 static bool
1195 cdtor_p (cgraph_node *n, void *)
1197 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1198 return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl);
1199 return false;
1202 /* We only propagate across edges with non-interposable callee. */
1204 static bool
1205 ignore_edge_for_pure_const (struct cgraph_edge *e)
1207 enum availability avail;
1208 e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
1209 return (avail <= AVAIL_INTERPOSABLE);
1213 /* Produce transitive closure over the callgraph and compute pure/const
1214 attributes. */
1216 static bool
1217 propagate_pure_const (void)
1219 struct cgraph_node *node;
1220 struct cgraph_node *w;
1221 struct cgraph_node **order =
1222 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1223 int order_pos;
1224 int i;
1225 struct ipa_dfs_info * w_info;
1226 bool remove_p = false;
1227 bool has_cdtor;
1229 order_pos = ipa_reduced_postorder (order, true, false,
1230 ignore_edge_for_pure_const);
1231 if (dump_file)
1233 cgraph_node::dump_cgraph (dump_file);
1234 ipa_print_order (dump_file, "reduced", order, order_pos);
1237 /* Propagate the local information through the call graph to produce
1238 the global information. All the nodes within a cycle will have
1239 the same info so we collapse cycles first. Then we can do the
1240 propagation in one pass from the leaves to the roots. */
1241 for (i = 0; i < order_pos; i++ )
1243 enum pure_const_state_e pure_const_state = IPA_CONST;
1244 bool looping = false;
1245 int count = 0;
1246 node = order[i];
1248 if (node->alias)
1249 continue;
1251 if (dump_file && (dump_flags & TDF_DETAILS))
1252 fprintf (dump_file, "Starting cycle\n");
1254 /* Find the worst state for any node in the cycle. */
1255 w = node;
1256 while (w && pure_const_state != IPA_NEITHER)
1258 struct cgraph_edge *e;
1259 struct cgraph_edge *ie;
1260 int i;
1261 struct ipa_ref *ref = NULL;
1263 funct_state w_l = get_function_state (w);
1264 if (dump_file && (dump_flags & TDF_DETAILS))
1265 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1266 w->name (),
1267 w->order,
1268 pure_const_names[w_l->pure_const_state],
1269 w_l->looping);
1271 /* First merge in function body properties.
1272 We are safe to pass NULL as FROM and TO because we will take care
1273 of possible interposition when walking callees. */
1274 worse_state (&pure_const_state, &looping,
1275 w_l->pure_const_state, w_l->looping,
1276 NULL, NULL);
1277 if (pure_const_state == IPA_NEITHER)
1278 break;
1280 count++;
1282 /* We consider recursive cycles as possibly infinite.
1283 This might be relaxed since infinite recursion leads to stack
1284 overflow. */
1285 if (count > 1)
1286 looping = true;
1288 /* Now walk the edges and merge in callee properties. */
1289 for (e = w->callees; e && pure_const_state != IPA_NEITHER;
1290 e = e->next_callee)
1292 enum availability avail;
1293 struct cgraph_node *y = e->callee->
1294 function_or_virtual_thunk_symbol (&avail,
1295 e->caller);
1296 enum pure_const_state_e edge_state = IPA_CONST;
1297 bool edge_looping = false;
1299 if (dump_file && (dump_flags & TDF_DETAILS))
1301 fprintf (dump_file,
1302 " Call to %s/%i",
1303 e->callee->name (),
1304 e->callee->order);
1306 if (avail > AVAIL_INTERPOSABLE)
1308 funct_state y_l = get_function_state (y);
1309 if (dump_file && (dump_flags & TDF_DETAILS))
1311 fprintf (dump_file,
1312 " state:%s looping:%i\n",
1313 pure_const_names[y_l->pure_const_state],
1314 y_l->looping);
1316 if (y_l->pure_const_state > IPA_PURE
1317 && e->cannot_lead_to_return_p ())
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1320 fprintf (dump_file,
1321 " Ignoring side effects"
1322 " -> pure, looping\n");
1323 edge_state = IPA_PURE;
1324 edge_looping = true;
1326 else
1328 edge_state = y_l->pure_const_state;
1329 edge_looping = y_l->looping;
1332 else if (special_builtin_state (&edge_state, &edge_looping,
1333 y->decl))
1335 else
1336 state_from_flags (&edge_state, &edge_looping,
1337 flags_from_decl_or_type (y->decl),
1338 e->cannot_lead_to_return_p ());
1340 /* Merge the results with what we already know. */
1341 better_state (&edge_state, &edge_looping,
1342 w_l->state_previously_known,
1343 w_l->looping_previously_known);
1344 worse_state (&pure_const_state, &looping,
1345 edge_state, edge_looping, e->caller, e->callee);
1346 if (pure_const_state == IPA_NEITHER)
1347 break;
1350 /* Now process the indirect call. */
1351 for (ie = w->indirect_calls;
1352 ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee)
1354 enum pure_const_state_e edge_state = IPA_CONST;
1355 bool edge_looping = false;
1357 if (dump_file && (dump_flags & TDF_DETAILS))
1358 fprintf (dump_file, " Indirect call");
1359 state_from_flags (&edge_state, &edge_looping,
1360 ie->indirect_info->ecf_flags,
1361 ie->cannot_lead_to_return_p ());
1362 /* Merge the results with what we already know. */
1363 better_state (&edge_state, &edge_looping,
1364 w_l->state_previously_known,
1365 w_l->looping_previously_known);
1366 worse_state (&pure_const_state, &looping,
1367 edge_state, edge_looping, NULL, NULL);
1368 if (pure_const_state == IPA_NEITHER)
1369 break;
1372 /* And finally all loads and stores. */
1373 for (i = 0; w->iterate_reference (i, ref)
1374 && pure_const_state != IPA_NEITHER; i++)
1376 enum pure_const_state_e ref_state = IPA_CONST;
1377 bool ref_looping = false;
1378 switch (ref->use)
1380 case IPA_REF_LOAD:
1381 /* readonly reads are safe. */
1382 if (TREE_READONLY (ref->referred->decl))
1383 break;
1384 if (dump_file && (dump_flags & TDF_DETAILS))
1385 fprintf (dump_file, " nonreadonly global var read\n");
1386 ref_state = IPA_PURE;
1387 break;
1388 case IPA_REF_STORE:
1389 if (ref->cannot_lead_to_return ())
1390 break;
1391 ref_state = IPA_NEITHER;
1392 if (dump_file && (dump_flags & TDF_DETAILS))
1393 fprintf (dump_file, " global var write\n");
1394 break;
1395 case IPA_REF_ADDR:
1396 case IPA_REF_CHKP:
1397 break;
1398 default:
1399 gcc_unreachable ();
1401 better_state (&ref_state, &ref_looping,
1402 w_l->state_previously_known,
1403 w_l->looping_previously_known);
1404 worse_state (&pure_const_state, &looping,
1405 ref_state, ref_looping, NULL, NULL);
1406 if (pure_const_state == IPA_NEITHER)
1407 break;
1409 w_info = (struct ipa_dfs_info *) w->aux;
1410 w = w_info->next_cycle;
1412 if (dump_file && (dump_flags & TDF_DETAILS))
1413 fprintf (dump_file, "Result %s looping %i\n",
1414 pure_const_names [pure_const_state],
1415 looping);
1417 /* Find the worst state of can_free for any node in the cycle. */
1418 bool can_free = false;
1419 w = node;
1420 while (w && !can_free)
1422 struct cgraph_edge *e;
1423 funct_state w_l = get_function_state (w);
1425 if (w_l->can_free
1426 || w->get_availability () == AVAIL_INTERPOSABLE
1427 || w->indirect_calls)
1428 can_free = true;
1430 for (e = w->callees; e && !can_free; e = e->next_callee)
1432 enum availability avail;
1433 struct cgraph_node *y = e->callee->
1434 function_or_virtual_thunk_symbol (&avail,
1435 e->caller);
1437 if (avail > AVAIL_INTERPOSABLE)
1438 can_free = get_function_state (y)->can_free;
1439 else
1440 can_free = true;
1442 w_info = (struct ipa_dfs_info *) w->aux;
1443 w = w_info->next_cycle;
1446 /* Copy back the region's pure_const_state which is shared by
1447 all nodes in the region. */
1448 w = node;
1449 while (w)
1451 funct_state w_l = get_function_state (w);
1452 enum pure_const_state_e this_state = pure_const_state;
1453 bool this_looping = looping;
1455 w_l->can_free = can_free;
1456 w->nonfreeing_fn = !can_free;
1457 if (!can_free && dump_file)
1458 fprintf (dump_file, "Function found not to call free: %s\n",
1459 w->name ());
1461 if (w_l->state_previously_known != IPA_NEITHER
1462 && this_state > w_l->state_previously_known)
1464 this_state = w_l->state_previously_known;
1465 if (this_state == IPA_NEITHER)
1466 this_looping = w_l->looping_previously_known;
1468 if (!this_looping && self_recursive_p (w))
1469 this_looping = true;
1470 if (!w_l->looping_previously_known)
1471 this_looping = false;
1473 /* All nodes within a cycle share the same info. */
1474 w_l->pure_const_state = this_state;
1475 w_l->looping = this_looping;
1477 /* Inline clones share declaration with their offline copies;
1478 do not modify their declarations since the offline copy may
1479 be different. */
1480 if (!w->global.inlined_to)
1481 switch (this_state)
1483 case IPA_CONST:
1484 if (!TREE_READONLY (w->decl))
1486 warn_function_const (w->decl, !this_looping);
1487 if (dump_file)
1488 fprintf (dump_file, "Function found to be %sconst: %s\n",
1489 this_looping ? "looping " : "",
1490 w->name ());
1492 /* Turning constructor or destructor to non-looping const/pure
1493 enables us to possibly remove the function completely. */
1494 if (this_looping)
1495 has_cdtor = false;
1496 else
1497 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1498 NULL, true);
1499 if (w->set_const_flag (true, this_looping))
1501 if (dump_file)
1502 fprintf (dump_file,
1503 "Declaration updated to be %sconst: %s\n",
1504 this_looping ? "looping " : "",
1505 w->name ());
1506 remove_p |= has_cdtor;
1508 break;
1510 case IPA_PURE:
1511 if (!DECL_PURE_P (w->decl))
1513 warn_function_pure (w->decl, !this_looping);
1514 if (dump_file)
1515 fprintf (dump_file, "Function found to be %spure: %s\n",
1516 this_looping ? "looping " : "",
1517 w->name ());
1519 if (this_looping)
1520 has_cdtor = false;
1521 else
1522 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1523 NULL, true);
1524 if (w->set_pure_flag (true, this_looping))
1526 if (dump_file)
1527 fprintf (dump_file,
1528 "Declaration updated to be %spure: %s\n",
1529 this_looping ? "looping " : "",
1530 w->name ());
1531 remove_p |= has_cdtor;
1533 break;
1535 default:
1536 break;
1538 w_info = (struct ipa_dfs_info *) w->aux;
1539 w = w_info->next_cycle;
1543 ipa_free_postorder_info ();
1544 free (order);
1545 return remove_p;
1548 /* Produce transitive closure over the callgraph and compute nothrow
1549 attributes. */
1551 static void
1552 propagate_nothrow (void)
1554 struct cgraph_node *node;
1555 struct cgraph_node *w;
1556 struct cgraph_node **order =
1557 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1558 int order_pos;
1559 int i;
1560 struct ipa_dfs_info * w_info;
1562 order_pos = ipa_reduced_postorder (order, true, false,
1563 ignore_edge_for_nothrow);
1564 if (dump_file)
1566 cgraph_node::dump_cgraph (dump_file);
1567 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1570 /* Propagate the local information through the call graph to produce
1571 the global information. All the nodes within a cycle will have
1572 the same info so we collapse cycles first. Then we can do the
1573 propagation in one pass from the leaves to the roots. */
1574 for (i = 0; i < order_pos; i++ )
1576 bool can_throw = false;
1577 node = order[i];
1579 if (node->alias)
1580 continue;
1582 /* Find the worst state for any node in the cycle. */
1583 w = node;
1584 while (w && !can_throw)
1586 struct cgraph_edge *e, *ie;
1588 if (!TREE_NOTHROW (w->decl))
1590 funct_state w_l = get_function_state (w);
1592 if (w_l->can_throw
1593 || w->get_availability () == AVAIL_INTERPOSABLE)
1594 can_throw = true;
1596 for (e = w->callees; e && !can_throw; e = e->next_callee)
1598 enum availability avail;
1600 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1601 continue;
1603 struct cgraph_node *y = e->callee->
1604 function_or_virtual_thunk_symbol (&avail,
1605 e->caller);
1607 /* We can use info about the callee only if we know it can
1608 not be interposed.
1609 When callee is compiled with non-call exceptions we also
1610 must check that the declaration is bound to current
1611 body as other semantically equivalent body may still
1612 throw. */
1613 if (avail <= AVAIL_INTERPOSABLE
1614 || (!TREE_NOTHROW (y->decl)
1615 && (get_function_state (y)->can_throw
1616 || (opt_for_fn (y->decl, flag_non_call_exceptions)
1617 && !e->callee->binds_to_current_def_p (w)))))
1618 can_throw = true;
1620 for (ie = w->indirect_calls; ie && !can_throw;
1621 ie = ie->next_callee)
1622 if (ie->can_throw_external
1623 && !(ie->indirect_info->ecf_flags & ECF_NOTHROW))
1624 can_throw = true;
1626 w_info = (struct ipa_dfs_info *) w->aux;
1627 w = w_info->next_cycle;
1630 /* Copy back the region's pure_const_state which is shared by
1631 all nodes in the region. */
1632 w = node;
1633 while (w)
1635 funct_state w_l = get_function_state (w);
1636 if (!can_throw && !TREE_NOTHROW (w->decl))
1638 /* Inline clones share declaration with their offline copies;
1639 do not modify their declarations since the offline copy may
1640 be different. */
1641 if (!w->global.inlined_to)
1643 w->set_nothrow_flag (true);
1644 if (dump_file)
1645 fprintf (dump_file, "Function found to be nothrow: %s\n",
1646 w->name ());
1649 else if (can_throw && !TREE_NOTHROW (w->decl))
1650 w_l->can_throw = true;
1651 w_info = (struct ipa_dfs_info *) w->aux;
1652 w = w_info->next_cycle;
1656 ipa_free_postorder_info ();
1657 free (order);
1661 /* Produce the global information by preforming a transitive closure
1662 on the local information that was produced by generate_summary. */
1664 unsigned int
1665 pass_ipa_pure_const::
1666 execute (function *)
1668 struct cgraph_node *node;
1669 bool remove_p;
1671 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1672 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1673 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1675 /* Nothrow makes more function to not lead to return and improve
1676 later analysis. */
1677 propagate_nothrow ();
1678 remove_p = propagate_pure_const ();
1680 /* Cleanup. */
1681 FOR_EACH_FUNCTION (node)
1682 if (has_function_state (node))
1683 free (get_function_state (node));
1684 funct_state_vec.release ();
1685 return remove_p ? TODO_remove_functions : 0;
1688 static bool
1689 gate_pure_const (void)
1691 return flag_ipa_pure_const || in_lto_p;
1694 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1695 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1696 pure_const_generate_summary, /* generate_summary */
1697 pure_const_write_summary, /* write_summary */
1698 pure_const_read_summary, /* read_summary */
1699 NULL, /* write_optimization_summary */
1700 NULL, /* read_optimization_summary */
1701 NULL, /* stmt_fixup */
1702 0, /* function_transform_todo_flags_start */
1703 NULL, /* function_transform */
1704 NULL), /* variable_transform */
1705 init_p(false),
1706 function_insertion_hook_holder(NULL),
1707 node_duplication_hook_holder(NULL),
1708 node_removal_hook_holder(NULL)
1712 ipa_opt_pass_d *
1713 make_pass_ipa_pure_const (gcc::context *ctxt)
1715 return new pass_ipa_pure_const (ctxt);
1718 /* Return true if function should be skipped for local pure const analysis. */
1720 static bool
1721 skip_function_for_local_pure_const (struct cgraph_node *node)
1723 /* Because we do not schedule pass_fixup_cfg over whole program after early
1724 optimizations we must not promote functions that are called by already
1725 processed functions. */
1727 if (function_called_by_processed_nodes_p ())
1729 if (dump_file)
1730 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1731 return true;
1733 /* Save some work and do not analyze functions which are interposable and
1734 do not have any non-interposable aliases. */
1735 if (node->get_availability () <= AVAIL_INTERPOSABLE
1736 && !node->has_aliases_p ())
1738 if (dump_file)
1739 fprintf (dump_file,
1740 "Function is interposable; not analyzing.\n");
1741 return true;
1743 return false;
1746 /* Simple local pass for pure const discovery reusing the analysis from
1747 ipa_pure_const. This pass is effective when executed together with
1748 other optimization passes in early optimization pass queue. */
1750 namespace {
1752 const pass_data pass_data_local_pure_const =
1754 GIMPLE_PASS, /* type */
1755 "local-pure-const", /* name */
1756 OPTGROUP_NONE, /* optinfo_flags */
1757 TV_IPA_PURE_CONST, /* tv_id */
1758 0, /* properties_required */
1759 0, /* properties_provided */
1760 0, /* properties_destroyed */
1761 0, /* todo_flags_start */
1762 0, /* todo_flags_finish */
1765 class pass_local_pure_const : public gimple_opt_pass
1767 public:
1768 pass_local_pure_const (gcc::context *ctxt)
1769 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1772 /* opt_pass methods: */
1773 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1774 virtual bool gate (function *) { return gate_pure_const (); }
1775 virtual unsigned int execute (function *);
1777 }; // class pass_local_pure_const
1779 unsigned int
1780 pass_local_pure_const::execute (function *fun)
1782 bool changed = false;
1783 funct_state l;
1784 bool skip;
1785 struct cgraph_node *node;
1787 node = cgraph_node::get (current_function_decl);
1788 skip = skip_function_for_local_pure_const (node);
1789 if (!warn_suggest_attribute_const
1790 && !warn_suggest_attribute_pure
1791 && skip)
1792 return 0;
1794 l = analyze_function (node, false);
1796 /* Do NORETURN discovery. */
1797 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1798 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1800 warn_function_noreturn (fun->decl);
1801 if (dump_file)
1802 fprintf (dump_file, "Function found to be noreturn: %s\n",
1803 current_function_name ());
1805 /* Update declaration and reduce profile to executed once. */
1806 TREE_THIS_VOLATILE (current_function_decl) = 1;
1807 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1808 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1810 changed = true;
1813 switch (l->pure_const_state)
1815 case IPA_CONST:
1816 if (!TREE_READONLY (current_function_decl))
1818 warn_function_const (current_function_decl, !l->looping);
1819 if (dump_file)
1820 fprintf (dump_file, "Function found to be %sconst: %s\n",
1821 l->looping ? "looping " : "",
1822 current_function_name ());
1824 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1825 && !l->looping)
1827 if (dump_file)
1828 fprintf (dump_file, "Function found to be non-looping: %s\n",
1829 current_function_name ());
1831 if (!skip && node->set_const_flag (true, l->looping))
1833 if (dump_file)
1834 fprintf (dump_file, "Declaration updated to be %sconst: %s\n",
1835 l->looping ? "looping " : "",
1836 current_function_name ());
1837 changed = true;
1839 break;
1841 case IPA_PURE:
1842 if (!DECL_PURE_P (current_function_decl))
1844 warn_function_pure (current_function_decl, !l->looping);
1845 if (dump_file)
1846 fprintf (dump_file, "Function found to be %spure: %s\n",
1847 l->looping ? "looping " : "",
1848 current_function_name ());
1850 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1851 && !l->looping)
1853 if (dump_file)
1854 fprintf (dump_file, "Function found to be non-looping: %s\n",
1855 current_function_name ());
1857 if (!skip && node->set_pure_flag (true, l->looping))
1859 if (dump_file)
1860 fprintf (dump_file, "Declaration updated to be %spure: %s\n",
1861 l->looping ? "looping " : "",
1862 current_function_name ());
1863 changed = true;
1865 break;
1867 default:
1868 break;
1870 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1872 node->set_nothrow_flag (true);
1873 changed = true;
1874 if (dump_file)
1875 fprintf (dump_file, "Function found to be nothrow: %s\n",
1876 current_function_name ());
1878 free (l);
1879 if (changed)
1880 return execute_fixup_cfg ();
1881 else
1882 return 0;
1885 } // anon namespace
1887 gimple_opt_pass *
1888 make_pass_local_pure_const (gcc::context *ctxt)
1890 return new pass_local_pure_const (ctxt);
1893 /* Emit noreturn warnings. */
1895 namespace {
1897 const pass_data pass_data_warn_function_noreturn =
1899 GIMPLE_PASS, /* type */
1900 "*warn_function_noreturn", /* name */
1901 OPTGROUP_NONE, /* optinfo_flags */
1902 TV_NONE, /* tv_id */
1903 PROP_cfg, /* properties_required */
1904 0, /* properties_provided */
1905 0, /* properties_destroyed */
1906 0, /* todo_flags_start */
1907 0, /* todo_flags_finish */
1910 class pass_warn_function_noreturn : public gimple_opt_pass
1912 public:
1913 pass_warn_function_noreturn (gcc::context *ctxt)
1914 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1917 /* opt_pass methods: */
1918 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1919 virtual unsigned int execute (function *fun)
1921 if (!TREE_THIS_VOLATILE (current_function_decl)
1922 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1923 warn_function_noreturn (current_function_decl);
1924 return 0;
1927 }; // class pass_warn_function_noreturn
1929 } // anon namespace
1931 gimple_opt_pass *
1932 make_pass_warn_function_noreturn (gcc::context *ctxt)
1934 return new pass_warn_function_noreturn (ctxt);
1937 /* Simple local pass for pure const discovery reusing the analysis from
1938 ipa_pure_const. This pass is effective when executed together with
1939 other optimization passes in early optimization pass queue. */
1941 namespace {
1943 const pass_data pass_data_nothrow =
1945 GIMPLE_PASS, /* type */
1946 "nothrow", /* name */
1947 OPTGROUP_NONE, /* optinfo_flags */
1948 TV_IPA_PURE_CONST, /* tv_id */
1949 0, /* properties_required */
1950 0, /* properties_provided */
1951 0, /* properties_destroyed */
1952 0, /* todo_flags_start */
1953 0, /* todo_flags_finish */
1956 class pass_nothrow : public gimple_opt_pass
1958 public:
1959 pass_nothrow (gcc::context *ctxt)
1960 : gimple_opt_pass (pass_data_nothrow, ctxt)
1963 /* opt_pass methods: */
1964 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1965 virtual bool gate (function *) { return optimize; }
1966 virtual unsigned int execute (function *);
1968 }; // class pass_nothrow
1970 unsigned int
1971 pass_nothrow::execute (function *)
1973 struct cgraph_node *node;
1974 basic_block this_block;
1976 if (TREE_NOTHROW (current_function_decl))
1977 return 0;
1979 node = cgraph_node::get (current_function_decl);
1981 /* We run during lowering, we can not really use availability yet. */
1982 if (cgraph_node::get (current_function_decl)->get_availability ()
1983 <= AVAIL_INTERPOSABLE)
1985 if (dump_file)
1986 fprintf (dump_file, "Function is interposable;"
1987 " not analyzing.\n");
1988 return true;
1991 FOR_EACH_BB_FN (this_block, cfun)
1993 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1994 !gsi_end_p (gsi);
1995 gsi_next (&gsi))
1996 if (stmt_can_throw_external (gsi_stmt (gsi)))
1998 if (is_gimple_call (gsi_stmt (gsi)))
2000 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
2001 if (callee_t && recursive_call_p (current_function_decl,
2002 callee_t))
2003 continue;
2006 if (dump_file)
2008 fprintf (dump_file, "Statement can throw: ");
2009 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
2011 return 0;
2015 node->set_nothrow_flag (true);
2017 bool cfg_changed = false;
2018 if (self_recursive_p (node))
2019 FOR_EACH_BB_FN (this_block, cfun)
2020 if (gimple *g = last_stmt (this_block))
2021 if (is_gimple_call (g))
2023 tree callee_t = gimple_call_fndecl (g);
2024 if (callee_t
2025 && recursive_call_p (current_function_decl, callee_t)
2026 && maybe_clean_eh_stmt (g)
2027 && gimple_purge_dead_eh_edges (this_block))
2028 cfg_changed = true;
2031 if (dump_file)
2032 fprintf (dump_file, "Function found to be nothrow: %s\n",
2033 current_function_name ());
2034 return cfg_changed ? TODO_cleanup_cfg : 0;
2037 } // anon namespace
2039 gimple_opt_pass *
2040 make_pass_nothrow (gcc::context *ctxt)
2042 return new pass_nothrow (ctxt);