2015-08-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / ipa-pure-const.c
blob8fd8c365bb4df5c1422de08d2891ca9429b94b9c
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file marks functions as being either const (TREE_READONLY) or
22 pure (DECL_PURE_P). It can also set a variant of these that
23 are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
25 This must be run after inlining decisions have been made since
26 otherwise, the local sets will not contain information that is
27 consistent with post inlined state. The global sets are not prone
28 to this problem since they are by definition transitive. */
30 /* The code in this module is called by the ipa pass manager. It
31 should be one of the later passes since it's information is used by
32 the rest of the compilation. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "backend.h"
38 #include "tree.h"
39 #include "gimple.h"
40 #include "hard-reg-set.h"
41 #include "alias.h"
42 #include "fold-const.h"
43 #include "print-tree.h"
44 #include "calls.h"
45 #include "cfganal.h"
46 #include "internal-fn.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 "tree-inline.h"
53 #include "tree-pass.h"
54 #include "langhooks.h"
55 #include "cgraph.h"
56 #include "ipa-utils.h"
57 #include "flags.h"
58 #include "diagnostic.h"
59 #include "gimple-pretty-print.h"
60 #include "langhooks.h"
61 #include "target.h"
62 #include "tree-streamer.h"
63 #include "cfgloop.h"
64 #include "tree-scalar-evolution.h"
65 #include "intl.h"
66 #include "opts.h"
67 #include "varasm.h"
69 /* Lattice values for const and pure functions. Everything starts out
70 being const, then may drop to pure and then neither depending on
71 what is found. */
72 enum pure_const_state_e
74 IPA_CONST,
75 IPA_PURE,
76 IPA_NEITHER
79 const char *pure_const_names[3] = {"const", "pure", "neither"};
81 /* Holder for the const_state. There is one of these per function
82 decl. */
83 struct funct_state_d
85 /* See above. */
86 enum pure_const_state_e pure_const_state;
87 /* What user set here; we can be always sure about this. */
88 enum pure_const_state_e state_previously_known;
89 bool looping_previously_known;
91 /* True if the function could possibly infinite loop. There are a
92 lot of ways that this could be determined. We are pretty
93 conservative here. While it is possible to cse pure and const
94 calls, it is not legal to have dce get rid of the call if there
95 is a possibility that the call could infinite loop since this is
96 a behavioral change. */
97 bool looping;
99 bool can_throw;
101 /* If function can call free, munmap or otherwise make previously
102 non-trapping memory accesses trapping. */
103 bool can_free;
106 /* State used when we know nothing about function. */
107 static struct funct_state_d varying_state
108 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
111 typedef struct funct_state_d * funct_state;
113 /* The storage of the funct_state is abstracted because there is the
114 possibility that it may be desirable to move this to the cgraph
115 local info. */
117 /* Array, indexed by cgraph node uid, of function states. */
119 static vec<funct_state> funct_state_vec;
121 static bool gate_pure_const (void);
123 namespace {
125 const pass_data pass_data_ipa_pure_const =
127 IPA_PASS, /* type */
128 "pure-const", /* name */
129 OPTGROUP_NONE, /* optinfo_flags */
130 TV_IPA_PURE_CONST, /* tv_id */
131 0, /* properties_required */
132 0, /* properties_provided */
133 0, /* properties_destroyed */
134 0, /* todo_flags_start */
135 0, /* todo_flags_finish */
138 class pass_ipa_pure_const : public ipa_opt_pass_d
140 public:
141 pass_ipa_pure_const(gcc::context *ctxt);
143 /* opt_pass methods: */
144 bool gate (function *) { return gate_pure_const (); }
145 unsigned int execute (function *fun);
147 void register_hooks (void);
149 private:
150 bool init_p;
152 /* Holders of ipa cgraph hooks: */
153 struct cgraph_node_hook_list *function_insertion_hook_holder;
154 struct cgraph_2node_hook_list *node_duplication_hook_holder;
155 struct cgraph_node_hook_list *node_removal_hook_holder;
157 }; // class pass_ipa_pure_const
159 } // anon namespace
161 /* Try to guess if function body will always be visible to compiler
162 when compiling the call and whether compiler will be able
163 to propagate the information by itself. */
165 static bool
166 function_always_visible_to_compiler_p (tree decl)
168 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
171 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
172 is true if the function is known to be finite. The diagnostic is
173 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
174 OPTION, this function may initialize it and it is always returned
175 by the function. */
177 static hash_set<tree> *
178 suggest_attribute (int option, tree decl, bool known_finite,
179 hash_set<tree> *warned_about,
180 const char * attrib_name)
182 if (!option_enabled (option, &global_options))
183 return warned_about;
184 if (TREE_THIS_VOLATILE (decl)
185 || (known_finite && function_always_visible_to_compiler_p (decl)))
186 return warned_about;
188 if (!warned_about)
189 warned_about = new hash_set<tree>;
190 if (warned_about->contains (decl))
191 return warned_about;
192 warned_about->add (decl);
193 warning_at (DECL_SOURCE_LOCATION (decl),
194 option,
195 known_finite
196 ? _("function might be candidate for attribute %<%s%>")
197 : _("function might be candidate for attribute %<%s%>"
198 " if it is known to return normally"), attrib_name);
199 return warned_about;
202 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
203 is true if the function is known to be finite. */
205 static void
206 warn_function_pure (tree decl, bool known_finite)
208 static hash_set<tree> *warned_about;
210 warned_about
211 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
212 known_finite, warned_about, "pure");
215 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
216 is true if the function is known to be finite. */
218 static void
219 warn_function_const (tree decl, bool known_finite)
221 static hash_set<tree> *warned_about;
222 warned_about
223 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
224 known_finite, warned_about, "const");
227 static void
228 warn_function_noreturn (tree decl)
230 static hash_set<tree> *warned_about;
231 if (!lang_hooks.missing_noreturn_ok_p (decl)
232 && targetm.warn_func_return (decl))
233 warned_about
234 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
235 true, warned_about, "noreturn");
238 /* Return true if we have a function state for NODE. */
240 static inline bool
241 has_function_state (struct cgraph_node *node)
243 if (!funct_state_vec.exists ()
244 || funct_state_vec.length () <= (unsigned int)node->uid)
245 return false;
246 return funct_state_vec[node->uid] != NULL;
249 /* Return the function state from NODE. */
251 static inline funct_state
252 get_function_state (struct cgraph_node *node)
254 if (!funct_state_vec.exists ()
255 || funct_state_vec.length () <= (unsigned int)node->uid
256 || !funct_state_vec[node->uid])
257 /* We might want to put correct previously_known state into varying. */
258 return &varying_state;
259 return funct_state_vec[node->uid];
262 /* Set the function state S for NODE. */
264 static inline void
265 set_function_state (struct cgraph_node *node, funct_state s)
267 if (!funct_state_vec.exists ()
268 || funct_state_vec.length () <= (unsigned int)node->uid)
269 funct_state_vec.safe_grow_cleared (node->uid + 1);
270 funct_state_vec[node->uid] = s;
273 /* Check to see if the use (or definition when CHECKING_WRITE is true)
274 variable T is legal in a function that is either pure or const. */
276 static inline void
277 check_decl (funct_state local,
278 tree t, bool checking_write, bool ipa)
280 /* Do not want to do anything with volatile except mark any
281 function that uses one to be not const or pure. */
282 if (TREE_THIS_VOLATILE (t))
284 local->pure_const_state = IPA_NEITHER;
285 if (dump_file)
286 fprintf (dump_file, " Volatile operand is not const/pure");
287 return;
290 /* Do not care about a local automatic that is not static. */
291 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
292 return;
294 /* If the variable has the "used" attribute, treat it as if it had a
295 been touched by the devil. */
296 if (DECL_PRESERVE_P (t))
298 local->pure_const_state = IPA_NEITHER;
299 if (dump_file)
300 fprintf (dump_file, " Used static/global variable is not const/pure\n");
301 return;
304 /* In IPA mode we are not interested in checking actual loads and stores;
305 they will be processed at propagation time using ipa_ref. */
306 if (ipa)
307 return;
309 /* Since we have dealt with the locals and params cases above, if we
310 are CHECKING_WRITE, this cannot be a pure or constant
311 function. */
312 if (checking_write)
314 local->pure_const_state = IPA_NEITHER;
315 if (dump_file)
316 fprintf (dump_file, " static/global memory write is not const/pure\n");
317 return;
320 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
322 /* Readonly reads are safe. */
323 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
324 return; /* Read of a constant, do not change the function state. */
325 else
327 if (dump_file)
328 fprintf (dump_file, " global memory read is not const\n");
329 /* Just a regular read. */
330 if (local->pure_const_state == IPA_CONST)
331 local->pure_const_state = IPA_PURE;
334 else
336 /* Compilation level statics can be read if they are readonly
337 variables. */
338 if (TREE_READONLY (t))
339 return;
341 if (dump_file)
342 fprintf (dump_file, " static memory read is not const\n");
343 /* Just a regular read. */
344 if (local->pure_const_state == IPA_CONST)
345 local->pure_const_state = IPA_PURE;
350 /* Check to see if the use (or definition when CHECKING_WRITE is true)
351 variable T is legal in a function that is either pure or const. */
353 static inline void
354 check_op (funct_state local, tree t, bool checking_write)
356 t = get_base_address (t);
357 if (t && TREE_THIS_VOLATILE (t))
359 local->pure_const_state = IPA_NEITHER;
360 if (dump_file)
361 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
362 return;
364 else if (t
365 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
366 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
367 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
369 if (dump_file)
370 fprintf (dump_file, " Indirect ref to local memory is OK\n");
371 return;
373 else if (checking_write)
375 local->pure_const_state = IPA_NEITHER;
376 if (dump_file)
377 fprintf (dump_file, " Indirect ref write is not const/pure\n");
378 return;
380 else
382 if (dump_file)
383 fprintf (dump_file, " Indirect ref read is not const\n");
384 if (local->pure_const_state == IPA_CONST)
385 local->pure_const_state = IPA_PURE;
389 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
391 static void
392 state_from_flags (enum pure_const_state_e *state, bool *looping,
393 int flags, bool cannot_lead_to_return)
395 *looping = false;
396 if (flags & ECF_LOOPING_CONST_OR_PURE)
398 *looping = true;
399 if (dump_file && (dump_flags & TDF_DETAILS))
400 fprintf (dump_file, " looping");
402 if (flags & ECF_CONST)
404 *state = IPA_CONST;
405 if (dump_file && (dump_flags & TDF_DETAILS))
406 fprintf (dump_file, " const\n");
408 else if (flags & ECF_PURE)
410 *state = IPA_PURE;
411 if (dump_file && (dump_flags & TDF_DETAILS))
412 fprintf (dump_file, " pure\n");
414 else if (cannot_lead_to_return)
416 *state = IPA_PURE;
417 *looping = true;
418 if (dump_file && (dump_flags & TDF_DETAILS))
419 fprintf (dump_file, " ignoring side effects->pure looping\n");
421 else
423 if (dump_file && (dump_flags & TDF_DETAILS))
424 fprintf (dump_file, " neither\n");
425 *state = IPA_NEITHER;
426 *looping = true;
430 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
431 into STATE and LOOPING better of the two variants.
432 Be sure to merge looping correctly. IPA_NEITHER functions
433 have looping 0 even if they don't have to return. */
435 static inline void
436 better_state (enum pure_const_state_e *state, bool *looping,
437 enum pure_const_state_e state2, bool looping2)
439 if (state2 < *state)
441 if (*state == IPA_NEITHER)
442 *looping = looping2;
443 else
444 *looping = MIN (*looping, looping2);
445 *state = state2;
447 else if (state2 != IPA_NEITHER)
448 *looping = MIN (*looping, looping2);
451 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
452 into STATE and LOOPING worse of the two variants. */
454 static inline void
455 worse_state (enum pure_const_state_e *state, bool *looping,
456 enum pure_const_state_e state2, bool looping2)
458 *state = MAX (*state, state2);
459 *looping = MAX (*looping, looping2);
462 /* Recognize special cases of builtins that are by themselves not pure or const
463 but function using them is. */
464 static bool
465 special_builtin_state (enum pure_const_state_e *state, bool *looping,
466 tree callee)
468 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
469 switch (DECL_FUNCTION_CODE (callee))
471 case BUILT_IN_RETURN:
472 case BUILT_IN_UNREACHABLE:
473 case BUILT_IN_ALLOCA:
474 case BUILT_IN_ALLOCA_WITH_ALIGN:
475 case BUILT_IN_STACK_SAVE:
476 case BUILT_IN_STACK_RESTORE:
477 case BUILT_IN_EH_POINTER:
478 case BUILT_IN_EH_FILTER:
479 case BUILT_IN_UNWIND_RESUME:
480 case BUILT_IN_CXA_END_CLEANUP:
481 case BUILT_IN_EH_COPY_VALUES:
482 case BUILT_IN_FRAME_ADDRESS:
483 case BUILT_IN_APPLY:
484 case BUILT_IN_APPLY_ARGS:
485 *looping = false;
486 *state = IPA_CONST;
487 return true;
488 case BUILT_IN_PREFETCH:
489 *looping = true;
490 *state = IPA_CONST;
491 return true;
492 default:
493 break;
495 return false;
498 /* Check the parameters of a function call to CALL_EXPR to see if
499 there are any references in the parameters that are not allowed for
500 pure or const functions. Also check to see if this is either an
501 indirect call, a call outside the compilation unit, or has special
502 attributes that may also effect the purity. The CALL_EXPR node for
503 the entire call expression. */
505 static void
506 check_call (funct_state local, gcall *call, bool ipa)
508 int flags = gimple_call_flags (call);
509 tree callee_t = gimple_call_fndecl (call);
510 bool possibly_throws = stmt_could_throw_p (call);
511 bool possibly_throws_externally = (possibly_throws
512 && stmt_can_throw_external (call));
514 if (possibly_throws)
516 unsigned int i;
517 for (i = 0; i < gimple_num_ops (call); i++)
518 if (gimple_op (call, i)
519 && tree_could_throw_p (gimple_op (call, i)))
521 if (possibly_throws && cfun->can_throw_non_call_exceptions)
523 if (dump_file)
524 fprintf (dump_file, " operand can throw; looping\n");
525 local->looping = true;
527 if (possibly_throws_externally)
529 if (dump_file)
530 fprintf (dump_file, " operand can throw externally\n");
531 local->can_throw = true;
536 /* The const and pure flags are set by a variety of places in the
537 compiler (including here). If someone has already set the flags
538 for the callee, (such as for some of the builtins) we will use
539 them, otherwise we will compute our own information.
541 Const and pure functions have less clobber effects than other
542 functions so we process these first. Otherwise if it is a call
543 outside the compilation unit or an indirect call we punt. This
544 leaves local calls which will be processed by following the call
545 graph. */
546 if (callee_t)
548 enum pure_const_state_e call_state;
549 bool call_looping;
551 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
552 && !nonfreeing_call_p (call))
553 local->can_free = true;
555 if (special_builtin_state (&call_state, &call_looping, callee_t))
557 worse_state (&local->pure_const_state, &local->looping,
558 call_state, call_looping);
559 return;
561 /* When bad things happen to bad functions, they cannot be const
562 or pure. */
563 if (setjmp_call_p (callee_t))
565 if (dump_file)
566 fprintf (dump_file, " setjmp is not const/pure\n");
567 local->looping = true;
568 local->pure_const_state = IPA_NEITHER;
571 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
572 switch (DECL_FUNCTION_CODE (callee_t))
574 case BUILT_IN_LONGJMP:
575 case BUILT_IN_NONLOCAL_GOTO:
576 if (dump_file)
577 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
578 local->pure_const_state = IPA_NEITHER;
579 local->looping = true;
580 break;
581 default:
582 break;
585 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
586 local->can_free = true;
588 /* When not in IPA mode, we can still handle self recursion. */
589 if (!ipa && callee_t
590 && recursive_call_p (current_function_decl, callee_t))
592 if (dump_file)
593 fprintf (dump_file, " Recursive call can loop.\n");
594 local->looping = true;
596 /* Either callee is unknown or we are doing local analysis.
597 Look to see if there are any bits available for the callee (such as by
598 declaration or because it is builtin) and process solely on the basis of
599 those bits. */
600 else if (!ipa)
602 enum pure_const_state_e call_state;
603 bool call_looping;
604 if (possibly_throws && cfun->can_throw_non_call_exceptions)
606 if (dump_file)
607 fprintf (dump_file, " can throw; looping\n");
608 local->looping = true;
610 if (possibly_throws_externally)
612 if (dump_file)
614 fprintf (dump_file, " can throw externally to lp %i\n",
615 lookup_stmt_eh_lp (call));
616 if (callee_t)
617 fprintf (dump_file, " callee:%s\n",
618 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
620 local->can_throw = true;
622 if (dump_file && (dump_flags & TDF_DETAILS))
623 fprintf (dump_file, " checking flags for call:");
624 state_from_flags (&call_state, &call_looping, flags,
625 ((flags & (ECF_NORETURN | ECF_NOTHROW))
626 == (ECF_NORETURN | ECF_NOTHROW))
627 || (!flag_exceptions && (flags & ECF_NORETURN)));
628 worse_state (&local->pure_const_state, &local->looping,
629 call_state, call_looping);
631 /* Direct functions calls are handled by IPA propagation. */
634 /* Wrapper around check_decl for loads in local more. */
636 static bool
637 check_load (gimple, tree op, tree, void *data)
639 if (DECL_P (op))
640 check_decl ((funct_state)data, op, false, false);
641 else
642 check_op ((funct_state)data, op, false);
643 return false;
646 /* Wrapper around check_decl for stores in local more. */
648 static bool
649 check_store (gimple, tree op, tree, void *data)
651 if (DECL_P (op))
652 check_decl ((funct_state)data, op, true, false);
653 else
654 check_op ((funct_state)data, op, true);
655 return false;
658 /* Wrapper around check_decl for loads in ipa mode. */
660 static bool
661 check_ipa_load (gimple, tree op, tree, void *data)
663 if (DECL_P (op))
664 check_decl ((funct_state)data, op, false, true);
665 else
666 check_op ((funct_state)data, op, false);
667 return false;
670 /* Wrapper around check_decl for stores in ipa mode. */
672 static bool
673 check_ipa_store (gimple, tree op, tree, void *data)
675 if (DECL_P (op))
676 check_decl ((funct_state)data, op, true, true);
677 else
678 check_op ((funct_state)data, op, true);
679 return false;
682 /* Look into pointer pointed to by GSIP and figure out what interesting side
683 effects it has. */
684 static void
685 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
687 gimple stmt = gsi_stmt (*gsip);
689 if (is_gimple_debug (stmt))
690 return;
692 /* Do consider clobber as side effects before IPA, so we rather inline
693 C++ destructors and keep clobber semantics than eliminate them.
695 TODO: We may get smarter during early optimizations on these and let
696 functions containing only clobbers to be optimized more. This is a common
697 case of C++ destructors. */
699 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
700 return;
702 if (dump_file)
704 fprintf (dump_file, " scanning: ");
705 print_gimple_stmt (dump_file, stmt, 0, 0);
708 if (gimple_has_volatile_ops (stmt)
709 && !gimple_clobber_p (stmt))
711 local->pure_const_state = IPA_NEITHER;
712 if (dump_file)
713 fprintf (dump_file, " Volatile stmt is not const/pure\n");
716 /* Look for loads and stores. */
717 walk_stmt_load_store_ops (stmt, local,
718 ipa ? check_ipa_load : check_load,
719 ipa ? check_ipa_store : check_store);
721 if (gimple_code (stmt) != GIMPLE_CALL
722 && stmt_could_throw_p (stmt))
724 if (cfun->can_throw_non_call_exceptions)
726 if (dump_file)
727 fprintf (dump_file, " can throw; looping\n");
728 local->looping = true;
730 if (stmt_can_throw_external (stmt))
732 if (dump_file)
733 fprintf (dump_file, " can throw externally\n");
734 local->can_throw = true;
736 else
737 if (dump_file)
738 fprintf (dump_file, " can throw\n");
740 switch (gimple_code (stmt))
742 case GIMPLE_CALL:
743 check_call (local, as_a <gcall *> (stmt), ipa);
744 break;
745 case GIMPLE_LABEL:
746 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
747 /* Target of long jump. */
749 if (dump_file)
750 fprintf (dump_file, " nonlocal label is not const/pure\n");
751 local->pure_const_state = IPA_NEITHER;
753 break;
754 case GIMPLE_ASM:
755 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
757 if (dump_file)
758 fprintf (dump_file, " memory asm clobber is not const/pure\n");
759 /* Abandon all hope, ye who enter here. */
760 local->pure_const_state = IPA_NEITHER;
761 local->can_free = true;
763 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
765 if (dump_file)
766 fprintf (dump_file, " volatile is not const/pure\n");
767 /* Abandon all hope, ye who enter here. */
768 local->pure_const_state = IPA_NEITHER;
769 local->looping = true;
770 local->can_free = true;
772 return;
773 default:
774 break;
779 /* This is the main routine for finding the reference patterns for
780 global variables within a function FN. */
782 static funct_state
783 analyze_function (struct cgraph_node *fn, bool ipa)
785 tree decl = fn->decl;
786 funct_state l;
787 basic_block this_block;
789 l = XCNEW (struct funct_state_d);
790 l->pure_const_state = IPA_CONST;
791 l->state_previously_known = IPA_NEITHER;
792 l->looping_previously_known = true;
793 l->looping = false;
794 l->can_throw = false;
795 l->can_free = false;
796 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
797 flags_from_decl_or_type (fn->decl),
798 fn->cannot_return_p ());
800 if (fn->thunk.thunk_p || fn->alias)
802 /* Thunk gets propagated through, so nothing interesting happens. */
803 gcc_assert (ipa);
804 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
805 l->pure_const_state = IPA_NEITHER;
806 return l;
809 if (dump_file)
811 fprintf (dump_file, "\n\n local analysis of %s\n ",
812 fn->name ());
815 push_cfun (DECL_STRUCT_FUNCTION (decl));
817 FOR_EACH_BB_FN (this_block, cfun)
819 gimple_stmt_iterator gsi;
820 struct walk_stmt_info wi;
822 memset (&wi, 0, sizeof (wi));
823 for (gsi = gsi_start_bb (this_block);
824 !gsi_end_p (gsi);
825 gsi_next (&gsi))
827 check_stmt (&gsi, l, ipa);
828 if (l->pure_const_state == IPA_NEITHER
829 && l->looping
830 && l->can_throw
831 && l->can_free)
832 goto end;
836 end:
837 if (l->pure_const_state != IPA_NEITHER)
839 /* Const functions cannot have back edges (an
840 indication of possible infinite loop side
841 effect. */
842 if (mark_dfs_back_edges ())
844 /* Preheaders are needed for SCEV to work.
845 Simple latches and recorded exits improve chances that loop will
846 proved to be finite in testcases such as in loop-15.c
847 and loop-24.c */
848 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
849 | LOOPS_HAVE_SIMPLE_LATCHES
850 | LOOPS_HAVE_RECORDED_EXITS);
851 if (dump_file && (dump_flags & TDF_DETAILS))
852 flow_loops_dump (dump_file, NULL, 0);
853 if (mark_irreducible_loops ())
855 if (dump_file)
856 fprintf (dump_file, " has irreducible loops\n");
857 l->looping = true;
859 else
861 struct loop *loop;
862 scev_initialize ();
863 FOR_EACH_LOOP (loop, 0)
864 if (!finite_loop_p (loop))
866 if (dump_file)
867 fprintf (dump_file, " can not prove finiteness of "
868 "loop %i\n", loop->num);
869 l->looping =true;
870 break;
872 scev_finalize ();
874 loop_optimizer_finalize ();
878 if (dump_file && (dump_flags & TDF_DETAILS))
879 fprintf (dump_file, " checking previously known:");
881 better_state (&l->pure_const_state, &l->looping,
882 l->state_previously_known,
883 l->looping_previously_known);
884 if (TREE_NOTHROW (decl))
885 l->can_throw = false;
887 pop_cfun ();
888 if (dump_file)
890 if (l->looping)
891 fprintf (dump_file, "Function is locally looping.\n");
892 if (l->can_throw)
893 fprintf (dump_file, "Function is locally throwing.\n");
894 if (l->pure_const_state == IPA_CONST)
895 fprintf (dump_file, "Function is locally const.\n");
896 if (l->pure_const_state == IPA_PURE)
897 fprintf (dump_file, "Function is locally pure.\n");
898 if (l->can_free)
899 fprintf (dump_file, "Function can locally free.\n");
901 return l;
904 /* Called when new function is inserted to callgraph late. */
905 static void
906 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
908 if (node->get_availability () < AVAIL_INTERPOSABLE)
909 return;
910 /* There are some shared nodes, in particular the initializers on
911 static declarations. We do not need to scan them more than once
912 since all we would be interested in are the addressof
913 operations. */
914 if (node->get_availability () > AVAIL_INTERPOSABLE
915 && opt_for_fn (node->decl, flag_ipa_pure_const))
916 set_function_state (node, analyze_function (node, true));
919 /* Called when new clone is inserted to callgraph late. */
921 static void
922 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
923 void *data ATTRIBUTE_UNUSED)
925 if (has_function_state (src))
927 funct_state l = XNEW (struct funct_state_d);
928 gcc_assert (!has_function_state (dst));
929 memcpy (l, get_function_state (src), sizeof (*l));
930 set_function_state (dst, l);
934 /* Called when new clone is inserted to callgraph late. */
936 static void
937 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
939 if (has_function_state (node))
941 funct_state l = get_function_state (node);
942 if (l != &varying_state)
943 free (l);
944 set_function_state (node, NULL);
949 void
950 pass_ipa_pure_const::
951 register_hooks (void)
953 if (init_p)
954 return;
956 init_p = true;
958 node_removal_hook_holder =
959 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
960 node_duplication_hook_holder =
961 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
962 function_insertion_hook_holder =
963 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
967 /* Analyze each function in the cgraph to see if it is locally PURE or
968 CONST. */
970 static void
971 pure_const_generate_summary (void)
973 struct cgraph_node *node;
975 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
976 pass->register_hooks ();
978 /* Process all of the functions.
980 We process AVAIL_INTERPOSABLE functions. We can not use the results
981 by default, but the info can be used at LTO with -fwhole-program or
982 when function got cloned and the clone is AVAILABLE. */
984 FOR_EACH_DEFINED_FUNCTION (node)
985 if (node->get_availability () >= AVAIL_INTERPOSABLE
986 && opt_for_fn (node->decl, flag_ipa_pure_const))
987 set_function_state (node, analyze_function (node, true));
991 /* Serialize the ipa info for lto. */
993 static void
994 pure_const_write_summary (void)
996 struct cgraph_node *node;
997 struct lto_simple_output_block *ob
998 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
999 unsigned int count = 0;
1000 lto_symtab_encoder_iterator lsei;
1001 lto_symtab_encoder_t encoder;
1003 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1005 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1006 lsei_next_function_in_partition (&lsei))
1008 node = lsei_cgraph_node (lsei);
1009 if (node->definition && has_function_state (node))
1010 count++;
1013 streamer_write_uhwi_stream (ob->main_stream, count);
1015 /* Process all of the functions. */
1016 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1017 lsei_next_function_in_partition (&lsei))
1019 node = lsei_cgraph_node (lsei);
1020 if (node->definition && has_function_state (node))
1022 struct bitpack_d bp;
1023 funct_state fs;
1024 int node_ref;
1025 lto_symtab_encoder_t encoder;
1027 fs = get_function_state (node);
1029 encoder = ob->decl_state->symtab_node_encoder;
1030 node_ref = lto_symtab_encoder_encode (encoder, node);
1031 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1033 /* Note that flags will need to be read in the opposite
1034 order as we are pushing the bitflags into FLAGS. */
1035 bp = bitpack_create (ob->main_stream);
1036 bp_pack_value (&bp, fs->pure_const_state, 2);
1037 bp_pack_value (&bp, fs->state_previously_known, 2);
1038 bp_pack_value (&bp, fs->looping_previously_known, 1);
1039 bp_pack_value (&bp, fs->looping, 1);
1040 bp_pack_value (&bp, fs->can_throw, 1);
1041 bp_pack_value (&bp, fs->can_free, 1);
1042 streamer_write_bitpack (&bp);
1046 lto_destroy_simple_output_block (ob);
1050 /* Deserialize the ipa info for lto. */
1052 static void
1053 pure_const_read_summary (void)
1055 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1056 struct lto_file_decl_data *file_data;
1057 unsigned int j = 0;
1059 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1060 pass->register_hooks ();
1062 while ((file_data = file_data_vec[j++]))
1064 const char *data;
1065 size_t len;
1066 struct lto_input_block *ib
1067 = lto_create_simple_input_block (file_data,
1068 LTO_section_ipa_pure_const,
1069 &data, &len);
1070 if (ib)
1072 unsigned int i;
1073 unsigned int count = streamer_read_uhwi (ib);
1075 for (i = 0; i < count; i++)
1077 unsigned int index;
1078 struct cgraph_node *node;
1079 struct bitpack_d bp;
1080 funct_state fs;
1081 lto_symtab_encoder_t encoder;
1083 fs = XCNEW (struct funct_state_d);
1084 index = streamer_read_uhwi (ib);
1085 encoder = file_data->symtab_node_encoder;
1086 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1087 index));
1088 set_function_state (node, fs);
1090 /* Note that the flags must be read in the opposite
1091 order in which they were written (the bitflags were
1092 pushed into FLAGS). */
1093 bp = streamer_read_bitpack (ib);
1094 fs->pure_const_state
1095 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1096 fs->state_previously_known
1097 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1098 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1099 fs->looping = bp_unpack_value (&bp, 1);
1100 fs->can_throw = bp_unpack_value (&bp, 1);
1101 fs->can_free = bp_unpack_value (&bp, 1);
1102 if (dump_file)
1104 int flags = flags_from_decl_or_type (node->decl);
1105 fprintf (dump_file, "Read info for %s/%i ",
1106 node->name (),
1107 node->order);
1108 if (flags & ECF_CONST)
1109 fprintf (dump_file, " const");
1110 if (flags & ECF_PURE)
1111 fprintf (dump_file, " pure");
1112 if (flags & ECF_NOTHROW)
1113 fprintf (dump_file, " nothrow");
1114 fprintf (dump_file, "\n pure const state: %s\n",
1115 pure_const_names[fs->pure_const_state]);
1116 fprintf (dump_file, " previously known state: %s\n",
1117 pure_const_names[fs->looping_previously_known]);
1118 if (fs->looping)
1119 fprintf (dump_file," function is locally looping\n");
1120 if (fs->looping_previously_known)
1121 fprintf (dump_file," function is previously known looping\n");
1122 if (fs->can_throw)
1123 fprintf (dump_file," function is locally throwing\n");
1124 if (fs->can_free)
1125 fprintf (dump_file," function can locally free\n");
1129 lto_destroy_simple_input_block (file_data,
1130 LTO_section_ipa_pure_const,
1131 ib, data, len);
1137 static bool
1138 ignore_edge (struct cgraph_edge *e)
1140 return (!e->can_throw_external);
1143 /* Return true if NODE is self recursive function.
1144 Indirectly recursive functions appears as non-trivial strongly
1145 connected components, so we need to care about self recursion
1146 only. */
1148 static bool
1149 self_recursive_p (struct cgraph_node *node)
1151 struct cgraph_edge *e;
1152 for (e = node->callees; e; e = e->next_callee)
1153 if (e->callee->function_symbol () == node)
1154 return true;
1155 return false;
1158 /* Return true if N is cdtor that is not const or pure. In this case we may
1159 need to remove unreachable function if it is marked const/pure. */
1161 static bool
1162 cdtor_p (cgraph_node *n, void *)
1164 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1165 return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl);
1166 return false;
1169 /* Produce transitive closure over the callgraph and compute pure/const
1170 attributes. */
1172 static bool
1173 propagate_pure_const (void)
1175 struct cgraph_node *node;
1176 struct cgraph_node *w;
1177 struct cgraph_node **order =
1178 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1179 int order_pos;
1180 int i;
1181 struct ipa_dfs_info * w_info;
1182 bool remove_p = false;
1184 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1185 if (dump_file)
1187 cgraph_node::dump_cgraph (dump_file);
1188 ipa_print_order (dump_file, "reduced", order, order_pos);
1191 /* Propagate the local information through the call graph to produce
1192 the global information. All the nodes within a cycle will have
1193 the same info so we collapse cycles first. Then we can do the
1194 propagation in one pass from the leaves to the roots. */
1195 for (i = 0; i < order_pos; i++ )
1197 enum pure_const_state_e pure_const_state = IPA_CONST;
1198 bool looping = false;
1199 int count = 0;
1200 node = order[i];
1202 if (node->alias)
1203 continue;
1205 if (dump_file && (dump_flags & TDF_DETAILS))
1206 fprintf (dump_file, "Starting cycle\n");
1208 /* Find the worst state for any node in the cycle. */
1209 w = node;
1210 while (w && pure_const_state != IPA_NEITHER)
1212 struct cgraph_edge *e;
1213 struct cgraph_edge *ie;
1214 int i;
1215 struct ipa_ref *ref = NULL;
1217 funct_state w_l = get_function_state (w);
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1219 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1220 w->name (),
1221 w->order,
1222 pure_const_names[w_l->pure_const_state],
1223 w_l->looping);
1225 /* First merge in function body properties. */
1226 worse_state (&pure_const_state, &looping,
1227 w_l->pure_const_state, w_l->looping);
1228 if (pure_const_state == IPA_NEITHER)
1229 break;
1231 /* For overwritable nodes we can not assume anything. */
1232 if (w->get_availability () == AVAIL_INTERPOSABLE)
1234 worse_state (&pure_const_state, &looping,
1235 w_l->state_previously_known,
1236 w_l->looping_previously_known);
1237 if (dump_file && (dump_flags & TDF_DETAILS))
1239 fprintf (dump_file,
1240 " Overwritable. state %s looping %i\n",
1241 pure_const_names[w_l->state_previously_known],
1242 w_l->looping_previously_known);
1244 break;
1247 count++;
1249 /* We consider recursive cycles as possibly infinite.
1250 This might be relaxed since infinite recursion leads to stack
1251 overflow. */
1252 if (count > 1)
1253 looping = true;
1255 /* Now walk the edges and merge in callee properties. */
1256 for (e = w->callees; e; e = e->next_callee)
1258 enum availability avail;
1259 struct cgraph_node *y = e->callee->
1260 function_or_virtual_thunk_symbol (&avail);
1261 enum pure_const_state_e edge_state = IPA_CONST;
1262 bool edge_looping = false;
1264 if (dump_file && (dump_flags & TDF_DETAILS))
1266 fprintf (dump_file,
1267 " Call to %s/%i",
1268 e->callee->name (),
1269 e->callee->order);
1271 if (avail > AVAIL_INTERPOSABLE)
1273 funct_state y_l = get_function_state (y);
1274 if (dump_file && (dump_flags & TDF_DETAILS))
1276 fprintf (dump_file,
1277 " state:%s looping:%i\n",
1278 pure_const_names[y_l->pure_const_state],
1279 y_l->looping);
1281 if (y_l->pure_const_state > IPA_PURE
1282 && e->cannot_lead_to_return_p ())
1284 if (dump_file && (dump_flags & TDF_DETAILS))
1285 fprintf (dump_file,
1286 " Ignoring side effects"
1287 " -> pure, looping\n");
1288 edge_state = IPA_PURE;
1289 edge_looping = true;
1291 else
1293 edge_state = y_l->pure_const_state;
1294 edge_looping = y_l->looping;
1297 else if (special_builtin_state (&edge_state, &edge_looping,
1298 y->decl))
1300 else
1301 state_from_flags (&edge_state, &edge_looping,
1302 flags_from_decl_or_type (y->decl),
1303 e->cannot_lead_to_return_p ());
1305 /* Merge the results with what we already know. */
1306 better_state (&edge_state, &edge_looping,
1307 w_l->state_previously_known,
1308 w_l->looping_previously_known);
1309 worse_state (&pure_const_state, &looping,
1310 edge_state, edge_looping);
1311 if (pure_const_state == IPA_NEITHER)
1312 break;
1314 if (pure_const_state == IPA_NEITHER)
1315 break;
1317 /* Now process the indirect call. */
1318 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1320 enum pure_const_state_e edge_state = IPA_CONST;
1321 bool edge_looping = false;
1323 if (dump_file && (dump_flags & TDF_DETAILS))
1324 fprintf (dump_file, " Indirect call");
1325 state_from_flags (&edge_state, &edge_looping,
1326 ie->indirect_info->ecf_flags,
1327 ie->cannot_lead_to_return_p ());
1328 /* Merge the results with what we already know. */
1329 better_state (&edge_state, &edge_looping,
1330 w_l->state_previously_known,
1331 w_l->looping_previously_known);
1332 worse_state (&pure_const_state, &looping,
1333 edge_state, edge_looping);
1334 if (pure_const_state == IPA_NEITHER)
1335 break;
1337 if (pure_const_state == IPA_NEITHER)
1338 break;
1340 /* And finally all loads and stores. */
1341 for (i = 0; w->iterate_reference (i, ref); i++)
1343 enum pure_const_state_e ref_state = IPA_CONST;
1344 bool ref_looping = false;
1345 switch (ref->use)
1347 case IPA_REF_LOAD:
1348 /* readonly reads are safe. */
1349 if (TREE_READONLY (ref->referred->decl))
1350 break;
1351 if (dump_file && (dump_flags & TDF_DETAILS))
1352 fprintf (dump_file, " nonreadonly global var read\n");
1353 ref_state = IPA_PURE;
1354 break;
1355 case IPA_REF_STORE:
1356 if (ref->cannot_lead_to_return ())
1357 break;
1358 ref_state = IPA_NEITHER;
1359 if (dump_file && (dump_flags & TDF_DETAILS))
1360 fprintf (dump_file, " global var write\n");
1361 break;
1362 case IPA_REF_ADDR:
1363 case IPA_REF_CHKP:
1364 break;
1365 default:
1366 gcc_unreachable ();
1368 better_state (&ref_state, &ref_looping,
1369 w_l->state_previously_known,
1370 w_l->looping_previously_known);
1371 worse_state (&pure_const_state, &looping,
1372 ref_state, ref_looping);
1373 if (pure_const_state == IPA_NEITHER)
1374 break;
1376 w_info = (struct ipa_dfs_info *) w->aux;
1377 w = w_info->next_cycle;
1379 if (dump_file && (dump_flags & TDF_DETAILS))
1380 fprintf (dump_file, "Result %s looping %i\n",
1381 pure_const_names [pure_const_state],
1382 looping);
1384 /* Find the worst state of can_free for any node in the cycle. */
1385 bool can_free = false;
1386 w = node;
1387 while (w && !can_free)
1389 struct cgraph_edge *e;
1390 funct_state w_l = get_function_state (w);
1392 if (w_l->can_free
1393 || w->get_availability () == AVAIL_INTERPOSABLE
1394 || w->indirect_calls)
1395 can_free = true;
1397 for (e = w->callees; e && !can_free; e = e->next_callee)
1399 enum availability avail;
1400 struct cgraph_node *y = e->callee->
1401 function_or_virtual_thunk_symbol (&avail);
1403 if (avail > AVAIL_INTERPOSABLE)
1404 can_free = get_function_state (y)->can_free;
1405 else
1406 can_free = true;
1408 w_info = (struct ipa_dfs_info *) w->aux;
1409 w = w_info->next_cycle;
1412 /* Copy back the region's pure_const_state which is shared by
1413 all nodes in the region. */
1414 w = node;
1415 while (w)
1417 funct_state w_l = get_function_state (w);
1418 enum pure_const_state_e this_state = pure_const_state;
1419 bool this_looping = looping;
1421 w_l->can_free = can_free;
1422 w->nonfreeing_fn = !can_free;
1423 if (!can_free && dump_file)
1424 fprintf (dump_file, "Function found not to call free: %s\n",
1425 w->name ());
1427 if (w_l->state_previously_known != IPA_NEITHER
1428 && this_state > w_l->state_previously_known)
1430 this_state = w_l->state_previously_known;
1431 this_looping |= w_l->looping_previously_known;
1433 if (!this_looping && self_recursive_p (w))
1434 this_looping = true;
1435 if (!w_l->looping_previously_known)
1436 this_looping = false;
1438 /* All nodes within a cycle share the same info. */
1439 w_l->pure_const_state = this_state;
1440 w_l->looping = this_looping;
1442 /* Inline clones share declaration with their offline copies;
1443 do not modify their declarations since the offline copy may
1444 be different. */
1445 if (!w->global.inlined_to)
1446 switch (this_state)
1448 case IPA_CONST:
1449 if (!TREE_READONLY (w->decl))
1451 warn_function_const (w->decl, !this_looping);
1452 if (dump_file)
1453 fprintf (dump_file, "Function found to be %sconst: %s\n",
1454 this_looping ? "looping " : "",
1455 w->name ());
1457 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1458 NULL, true);
1459 w->set_const_flag (true, this_looping);
1460 break;
1462 case IPA_PURE:
1463 if (!DECL_PURE_P (w->decl))
1465 warn_function_pure (w->decl, !this_looping);
1466 if (dump_file)
1467 fprintf (dump_file, "Function found to be %spure: %s\n",
1468 this_looping ? "looping " : "",
1469 w->name ());
1471 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1472 NULL, true);
1473 w->set_pure_flag (true, this_looping);
1474 break;
1476 default:
1477 break;
1479 w_info = (struct ipa_dfs_info *) w->aux;
1480 w = w_info->next_cycle;
1484 ipa_free_postorder_info ();
1485 free (order);
1486 return remove_p;
1489 /* Produce transitive closure over the callgraph and compute nothrow
1490 attributes. */
1492 static void
1493 propagate_nothrow (void)
1495 struct cgraph_node *node;
1496 struct cgraph_node *w;
1497 struct cgraph_node **order =
1498 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1499 int order_pos;
1500 int i;
1501 struct ipa_dfs_info * w_info;
1503 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1504 if (dump_file)
1506 cgraph_node::dump_cgraph (dump_file);
1507 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1510 /* Propagate the local information through the call graph to produce
1511 the global information. All the nodes within a cycle will have
1512 the same info so we collapse cycles first. Then we can do the
1513 propagation in one pass from the leaves to the roots. */
1514 for (i = 0; i < order_pos; i++ )
1516 bool can_throw = false;
1517 node = order[i];
1519 if (node->alias)
1520 continue;
1522 /* Find the worst state for any node in the cycle. */
1523 w = node;
1524 while (w && !can_throw)
1526 struct cgraph_edge *e, *ie;
1527 funct_state w_l = get_function_state (w);
1529 if (w_l->can_throw
1530 || w->get_availability () == AVAIL_INTERPOSABLE)
1531 can_throw = true;
1533 for (e = w->callees; e && !can_throw; e = e->next_callee)
1535 enum availability avail;
1536 struct cgraph_node *y = e->callee->
1537 function_or_virtual_thunk_symbol (&avail);
1539 if (avail > AVAIL_INTERPOSABLE)
1541 funct_state y_l = get_function_state (y);
1543 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1544 && e->can_throw_external)
1545 can_throw = true;
1547 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1548 can_throw = true;
1550 for (ie = w->indirect_calls; ie && !can_throw; ie = ie->next_callee)
1551 if (ie->can_throw_external)
1552 can_throw = true;
1553 w_info = (struct ipa_dfs_info *) w->aux;
1554 w = w_info->next_cycle;
1557 /* Copy back the region's pure_const_state which is shared by
1558 all nodes in the region. */
1559 w = node;
1560 while (w)
1562 funct_state w_l = get_function_state (w);
1563 if (!can_throw && !TREE_NOTHROW (w->decl))
1565 /* Inline clones share declaration with their offline copies;
1566 do not modify their declarations since the offline copy may
1567 be different. */
1568 if (!w->global.inlined_to)
1570 w->set_nothrow_flag (true);
1571 if (dump_file)
1572 fprintf (dump_file, "Function found to be nothrow: %s\n",
1573 w->name ());
1576 else if (can_throw && !TREE_NOTHROW (w->decl))
1577 w_l->can_throw = true;
1578 w_info = (struct ipa_dfs_info *) w->aux;
1579 w = w_info->next_cycle;
1583 ipa_free_postorder_info ();
1584 free (order);
1588 /* Produce the global information by preforming a transitive closure
1589 on the local information that was produced by generate_summary. */
1591 unsigned int
1592 pass_ipa_pure_const::
1593 execute (function *)
1595 struct cgraph_node *node;
1596 bool remove_p;
1598 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1599 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1600 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1602 /* Nothrow makes more function to not lead to return and improve
1603 later analysis. */
1604 propagate_nothrow ();
1605 remove_p = propagate_pure_const ();
1607 /* Cleanup. */
1608 FOR_EACH_FUNCTION (node)
1609 if (has_function_state (node))
1610 free (get_function_state (node));
1611 funct_state_vec.release ();
1612 return remove_p ? TODO_remove_functions : 0;
1615 static bool
1616 gate_pure_const (void)
1618 return flag_ipa_pure_const || in_lto_p;
1621 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1622 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1623 pure_const_generate_summary, /* generate_summary */
1624 pure_const_write_summary, /* write_summary */
1625 pure_const_read_summary, /* read_summary */
1626 NULL, /* write_optimization_summary */
1627 NULL, /* read_optimization_summary */
1628 NULL, /* stmt_fixup */
1629 0, /* function_transform_todo_flags_start */
1630 NULL, /* function_transform */
1631 NULL), /* variable_transform */
1632 init_p(false),
1633 function_insertion_hook_holder(NULL),
1634 node_duplication_hook_holder(NULL),
1635 node_removal_hook_holder(NULL)
1639 ipa_opt_pass_d *
1640 make_pass_ipa_pure_const (gcc::context *ctxt)
1642 return new pass_ipa_pure_const (ctxt);
1645 /* Return true if function should be skipped for local pure const analysis. */
1647 static bool
1648 skip_function_for_local_pure_const (struct cgraph_node *node)
1650 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1651 we must not promote functions that are called by already processed functions. */
1653 if (function_called_by_processed_nodes_p ())
1655 if (dump_file)
1656 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1657 return true;
1659 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1661 if (dump_file)
1662 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1663 return true;
1665 return false;
1668 /* Simple local pass for pure const discovery reusing the analysis from
1669 ipa_pure_const. This pass is effective when executed together with
1670 other optimization passes in early optimization pass queue. */
1672 namespace {
1674 const pass_data pass_data_local_pure_const =
1676 GIMPLE_PASS, /* type */
1677 "local-pure-const", /* name */
1678 OPTGROUP_NONE, /* optinfo_flags */
1679 TV_IPA_PURE_CONST, /* tv_id */
1680 0, /* properties_required */
1681 0, /* properties_provided */
1682 0, /* properties_destroyed */
1683 0, /* todo_flags_start */
1684 0, /* todo_flags_finish */
1687 class pass_local_pure_const : public gimple_opt_pass
1689 public:
1690 pass_local_pure_const (gcc::context *ctxt)
1691 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1694 /* opt_pass methods: */
1695 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1696 virtual bool gate (function *) { return gate_pure_const (); }
1697 virtual unsigned int execute (function *);
1699 }; // class pass_local_pure_const
1701 unsigned int
1702 pass_local_pure_const::execute (function *fun)
1704 bool changed = false;
1705 funct_state l;
1706 bool skip;
1707 struct cgraph_node *node;
1709 node = cgraph_node::get (current_function_decl);
1710 skip = skip_function_for_local_pure_const (node);
1711 if (!warn_suggest_attribute_const
1712 && !warn_suggest_attribute_pure
1713 && skip)
1714 return 0;
1716 l = analyze_function (node, false);
1718 /* Do NORETURN discovery. */
1719 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1720 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1722 warn_function_noreturn (fun->decl);
1723 if (dump_file)
1724 fprintf (dump_file, "Function found to be noreturn: %s\n",
1725 current_function_name ());
1727 /* Update declaration and reduce profile to executed once. */
1728 TREE_THIS_VOLATILE (current_function_decl) = 1;
1729 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1730 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1732 changed = true;
1735 switch (l->pure_const_state)
1737 case IPA_CONST:
1738 if (!TREE_READONLY (current_function_decl))
1740 warn_function_const (current_function_decl, !l->looping);
1741 if (!skip)
1743 node->set_const_flag (true, l->looping);
1744 changed = true;
1746 if (dump_file)
1747 fprintf (dump_file, "Function found to be %sconst: %s\n",
1748 l->looping ? "looping " : "",
1749 current_function_name ());
1751 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1752 && !l->looping)
1754 if (!skip)
1756 node->set_const_flag (true, false);
1757 changed = true;
1759 if (dump_file)
1760 fprintf (dump_file, "Function found to be non-looping: %s\n",
1761 current_function_name ());
1763 break;
1765 case IPA_PURE:
1766 if (!DECL_PURE_P (current_function_decl))
1768 if (!skip)
1770 node->set_pure_flag (true, l->looping);
1771 changed = true;
1773 warn_function_pure (current_function_decl, !l->looping);
1774 if (dump_file)
1775 fprintf (dump_file, "Function found to be %spure: %s\n",
1776 l->looping ? "looping " : "",
1777 current_function_name ());
1779 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1780 && !l->looping)
1782 if (!skip)
1784 node->set_pure_flag (true, false);
1785 changed = true;
1787 if (dump_file)
1788 fprintf (dump_file, "Function found to be non-looping: %s\n",
1789 current_function_name ());
1791 break;
1793 default:
1794 break;
1796 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1798 node->set_nothrow_flag (true);
1799 changed = true;
1800 if (dump_file)
1801 fprintf (dump_file, "Function found to be nothrow: %s\n",
1802 current_function_name ());
1804 free (l);
1805 if (changed)
1806 return execute_fixup_cfg ();
1807 else
1808 return 0;
1811 } // anon namespace
1813 gimple_opt_pass *
1814 make_pass_local_pure_const (gcc::context *ctxt)
1816 return new pass_local_pure_const (ctxt);
1819 /* Emit noreturn warnings. */
1821 namespace {
1823 const pass_data pass_data_warn_function_noreturn =
1825 GIMPLE_PASS, /* type */
1826 "*warn_function_noreturn", /* name */
1827 OPTGROUP_NONE, /* optinfo_flags */
1828 TV_NONE, /* tv_id */
1829 PROP_cfg, /* properties_required */
1830 0, /* properties_provided */
1831 0, /* properties_destroyed */
1832 0, /* todo_flags_start */
1833 0, /* todo_flags_finish */
1836 class pass_warn_function_noreturn : public gimple_opt_pass
1838 public:
1839 pass_warn_function_noreturn (gcc::context *ctxt)
1840 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1843 /* opt_pass methods: */
1844 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1845 virtual unsigned int execute (function *fun)
1847 if (!TREE_THIS_VOLATILE (current_function_decl)
1848 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1849 warn_function_noreturn (current_function_decl);
1850 return 0;
1853 }; // class pass_warn_function_noreturn
1855 } // anon namespace
1857 gimple_opt_pass *
1858 make_pass_warn_function_noreturn (gcc::context *ctxt)
1860 return new pass_warn_function_noreturn (ctxt);
1863 /* Simple local pass for pure const discovery reusing the analysis from
1864 ipa_pure_const. This pass is effective when executed together with
1865 other optimization passes in early optimization pass queue. */
1867 namespace {
1869 const pass_data pass_data_nothrow =
1871 GIMPLE_PASS, /* type */
1872 "nothrow", /* name */
1873 OPTGROUP_NONE, /* optinfo_flags */
1874 TV_IPA_PURE_CONST, /* tv_id */
1875 0, /* properties_required */
1876 0, /* properties_provided */
1877 0, /* properties_destroyed */
1878 0, /* todo_flags_start */
1879 0, /* todo_flags_finish */
1882 class pass_nothrow : public gimple_opt_pass
1884 public:
1885 pass_nothrow (gcc::context *ctxt)
1886 : gimple_opt_pass (pass_data_nothrow, ctxt)
1889 /* opt_pass methods: */
1890 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1891 virtual bool gate (function *) { return optimize; }
1892 virtual unsigned int execute (function *);
1894 }; // class pass_nothrow
1896 unsigned int
1897 pass_nothrow::execute (function *)
1899 struct cgraph_node *node;
1900 basic_block this_block;
1902 if (TREE_NOTHROW (current_function_decl))
1903 return 0;
1905 node = cgraph_node::get (current_function_decl);
1907 /* We run during lowering, we can not really use availability yet. */
1908 if (cgraph_node::get (current_function_decl)->get_availability ()
1909 <= AVAIL_INTERPOSABLE)
1911 if (dump_file)
1912 fprintf (dump_file, "Function is interposable;"
1913 " not analyzing.\n");
1914 return true;
1917 FOR_EACH_BB_FN (this_block, cfun)
1919 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1920 !gsi_end_p (gsi);
1921 gsi_next (&gsi))
1922 if (stmt_can_throw_external (gsi_stmt (gsi)))
1924 if (is_gimple_call (gsi_stmt (gsi)))
1926 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
1927 if (callee_t && recursive_call_p (current_function_decl,
1928 callee_t))
1929 continue;
1932 if (dump_file)
1934 fprintf (dump_file, "Statement can throw: ");
1935 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1937 return 0;
1941 node->set_nothrow_flag (true);
1942 if (dump_file)
1943 fprintf (dump_file, "Function found to be nothrow: %s\n",
1944 current_function_name ());
1945 return 0;
1948 } // anon namespace
1950 gimple_opt_pass *
1951 make_pass_nothrow (gcc::context *ctxt)
1953 return new pass_nothrow (ctxt);