* asan.c (handle_builtin_alloca): Deal with all alloca variants.
[official-gcc.git] / gcc / ipa-pure-const.c
blob3c06e2d303328343be557e66a0f9dc32098fbef7
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file marks functions as being either const (TREE_READONLY) or
22 pure (DECL_PURE_P). It can also set a variant of these that
23 are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
25 This must be run after inlining decisions have been made since
26 otherwise, the local sets will not contain information that is
27 consistent with post inlined state. The global sets are not prone
28 to this problem since they are by definition transitive. */
30 /* The code in this module is called by the ipa pass manager. It
31 should be one of the later passes since it's information is used by
32 the rest of the compilation. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "backend.h"
38 #include "target.h"
39 #include "tree.h"
40 #include "gimple.h"
41 #include "tree-pass.h"
42 #include "tree-streamer.h"
43 #include "cgraph.h"
44 #include "diagnostic.h"
45 #include "calls.h"
46 #include "cfganal.h"
47 #include "tree-eh.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "tree-cfg.h"
51 #include "tree-ssa-loop-niter.h"
52 #include "langhooks.h"
53 #include "ipa-utils.h"
54 #include "gimple-pretty-print.h"
55 #include "cfgloop.h"
56 #include "tree-scalar-evolution.h"
57 #include "intl.h"
58 #include "opts.h"
60 /* Lattice values for const and pure functions. Everything starts out
61 being const, then may drop to pure and then neither depending on
62 what is found. */
63 enum pure_const_state_e
65 IPA_CONST,
66 IPA_PURE,
67 IPA_NEITHER
70 const char *pure_const_names[3] = {"const", "pure", "neither"};
72 /* Holder for the const_state. There is one of these per function
73 decl. */
74 struct funct_state_d
76 /* See above. */
77 enum pure_const_state_e pure_const_state;
78 /* What user set here; we can be always sure about this. */
79 enum pure_const_state_e state_previously_known;
80 bool looping_previously_known;
82 /* True if the function could possibly infinite loop. There are a
83 lot of ways that this could be determined. We are pretty
84 conservative here. While it is possible to cse pure and const
85 calls, it is not legal to have dce get rid of the call if there
86 is a possibility that the call could infinite loop since this is
87 a behavioral change. */
88 bool looping;
90 bool can_throw;
92 /* If function can call free, munmap or otherwise make previously
93 non-trapping memory accesses trapping. */
94 bool can_free;
97 /* State used when we know nothing about function. */
98 static struct funct_state_d varying_state
99 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
102 typedef struct funct_state_d * funct_state;
104 /* The storage of the funct_state is abstracted because there is the
105 possibility that it may be desirable to move this to the cgraph
106 local info. */
108 /* Array, indexed by cgraph node uid, of function states. */
110 static vec<funct_state> funct_state_vec;
112 static bool gate_pure_const (void);
114 namespace {
116 const pass_data pass_data_ipa_pure_const =
118 IPA_PASS, /* type */
119 "pure-const", /* name */
120 OPTGROUP_NONE, /* optinfo_flags */
121 TV_IPA_PURE_CONST, /* tv_id */
122 0, /* properties_required */
123 0, /* properties_provided */
124 0, /* properties_destroyed */
125 0, /* todo_flags_start */
126 0, /* todo_flags_finish */
129 class pass_ipa_pure_const : public ipa_opt_pass_d
131 public:
132 pass_ipa_pure_const(gcc::context *ctxt);
134 /* opt_pass methods: */
135 bool gate (function *) { return gate_pure_const (); }
136 unsigned int execute (function *fun);
138 void register_hooks (void);
140 private:
141 bool init_p;
143 /* Holders of ipa cgraph hooks: */
144 struct cgraph_node_hook_list *function_insertion_hook_holder;
145 struct cgraph_2node_hook_list *node_duplication_hook_holder;
146 struct cgraph_node_hook_list *node_removal_hook_holder;
148 }; // class pass_ipa_pure_const
150 } // anon namespace
152 /* Try to guess if function body will always be visible to compiler
153 when compiling the call and whether compiler will be able
154 to propagate the information by itself. */
156 static bool
157 function_always_visible_to_compiler_p (tree decl)
159 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)
160 || DECL_COMDAT (decl));
163 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
164 is true if the function is known to be finite. The diagnostic is
165 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
166 OPTION, this function may initialize it and it is always returned
167 by the function. */
169 static hash_set<tree> *
170 suggest_attribute (int option, tree decl, bool known_finite,
171 hash_set<tree> *warned_about,
172 const char * attrib_name)
174 if (!option_enabled (option, &global_options))
175 return warned_about;
176 if (TREE_THIS_VOLATILE (decl)
177 || (known_finite && function_always_visible_to_compiler_p (decl)))
178 return warned_about;
180 if (!warned_about)
181 warned_about = new hash_set<tree>;
182 if (warned_about->contains (decl))
183 return warned_about;
184 warned_about->add (decl);
185 warning_at (DECL_SOURCE_LOCATION (decl),
186 option,
187 known_finite
188 ? G_("function might be candidate for attribute %qs")
189 : G_("function might be candidate for attribute %qs"
190 " if it is known to return normally"), attrib_name);
191 return warned_about;
194 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
195 is true if the function is known to be finite. */
197 static void
198 warn_function_pure (tree decl, bool known_finite)
200 static hash_set<tree> *warned_about;
202 warned_about
203 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
204 known_finite, warned_about, "pure");
207 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
208 is true if the function is known to be finite. */
210 static void
211 warn_function_const (tree decl, bool known_finite)
213 static hash_set<tree> *warned_about;
214 warned_about
215 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
216 known_finite, warned_about, "const");
219 static void
220 warn_function_noreturn (tree decl)
222 tree original_decl = decl;
224 cgraph_node *node = cgraph_node::get (decl);
225 if (node->instrumentation_clone)
226 decl = node->instrumented_version->decl;
228 static hash_set<tree> *warned_about;
229 if (!lang_hooks.missing_noreturn_ok_p (decl)
230 && targetm.warn_func_return (decl))
231 warned_about
232 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, original_decl,
233 true, warned_about, "noreturn");
236 void
237 warn_function_cold (tree decl)
239 tree original_decl = decl;
241 cgraph_node *node = cgraph_node::get (decl);
242 if (node->instrumentation_clone)
243 decl = node->instrumented_version->decl;
245 static hash_set<tree> *warned_about;
246 warned_about
247 = suggest_attribute (OPT_Wsuggest_attribute_cold, original_decl,
248 true, warned_about, "cold");
251 /* Return true if we have a function state for NODE. */
253 static inline bool
254 has_function_state (struct cgraph_node *node)
256 if (!funct_state_vec.exists ()
257 || funct_state_vec.length () <= (unsigned int)node->uid)
258 return false;
259 return funct_state_vec[node->uid] != NULL;
262 /* Return the function state from NODE. */
264 static inline funct_state
265 get_function_state (struct cgraph_node *node)
267 if (!funct_state_vec.exists ()
268 || funct_state_vec.length () <= (unsigned int)node->uid
269 || !funct_state_vec[node->uid])
270 /* We might want to put correct previously_known state into varying. */
271 return &varying_state;
272 return funct_state_vec[node->uid];
275 /* Set the function state S for NODE. */
277 static inline void
278 set_function_state (struct cgraph_node *node, funct_state s)
280 if (!funct_state_vec.exists ()
281 || funct_state_vec.length () <= (unsigned int)node->uid)
282 funct_state_vec.safe_grow_cleared (node->uid + 1);
284 /* If funct_state_vec already contains a funct_state, we have to release
285 it before it's going to be ovewritten. */
286 if (funct_state_vec[node->uid] != NULL
287 && funct_state_vec[node->uid] != &varying_state)
288 free (funct_state_vec[node->uid]);
290 funct_state_vec[node->uid] = s;
293 /* Check to see if the use (or definition when CHECKING_WRITE is true)
294 variable T is legal in a function that is either pure or const. */
296 static inline void
297 check_decl (funct_state local,
298 tree t, bool checking_write, bool ipa)
300 /* Do not want to do anything with volatile except mark any
301 function that uses one to be not const or pure. */
302 if (TREE_THIS_VOLATILE (t))
304 local->pure_const_state = IPA_NEITHER;
305 if (dump_file)
306 fprintf (dump_file, " Volatile operand is not const/pure");
307 return;
310 /* Do not care about a local automatic that is not static. */
311 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
312 return;
314 /* If the variable has the "used" attribute, treat it as if it had a
315 been touched by the devil. */
316 if (DECL_PRESERVE_P (t))
318 local->pure_const_state = IPA_NEITHER;
319 if (dump_file)
320 fprintf (dump_file, " Used static/global variable is not const/pure\n");
321 return;
324 /* In IPA mode we are not interested in checking actual loads and stores;
325 they will be processed at propagation time using ipa_ref. */
326 if (ipa)
327 return;
329 /* Since we have dealt with the locals and params cases above, if we
330 are CHECKING_WRITE, this cannot be a pure or constant
331 function. */
332 if (checking_write)
334 local->pure_const_state = IPA_NEITHER;
335 if (dump_file)
336 fprintf (dump_file, " static/global memory write is not const/pure\n");
337 return;
340 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
342 /* Readonly reads are safe. */
343 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
344 return; /* Read of a constant, do not change the function state. */
345 else
347 if (dump_file)
348 fprintf (dump_file, " global memory read is not const\n");
349 /* Just a regular read. */
350 if (local->pure_const_state == IPA_CONST)
351 local->pure_const_state = IPA_PURE;
354 else
356 /* Compilation level statics can be read if they are readonly
357 variables. */
358 if (TREE_READONLY (t))
359 return;
361 if (dump_file)
362 fprintf (dump_file, " static memory read is not const\n");
363 /* Just a regular read. */
364 if (local->pure_const_state == IPA_CONST)
365 local->pure_const_state = IPA_PURE;
370 /* Check to see if the use (or definition when CHECKING_WRITE is true)
371 variable T is legal in a function that is either pure or const. */
373 static inline void
374 check_op (funct_state local, tree t, bool checking_write)
376 t = get_base_address (t);
377 if (t && TREE_THIS_VOLATILE (t))
379 local->pure_const_state = IPA_NEITHER;
380 if (dump_file)
381 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
382 return;
384 else if (t
385 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
386 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
387 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
389 if (dump_file)
390 fprintf (dump_file, " Indirect ref to local memory is OK\n");
391 return;
393 else if (checking_write)
395 local->pure_const_state = IPA_NEITHER;
396 if (dump_file)
397 fprintf (dump_file, " Indirect ref write is not const/pure\n");
398 return;
400 else
402 if (dump_file)
403 fprintf (dump_file, " Indirect ref read is not const\n");
404 if (local->pure_const_state == IPA_CONST)
405 local->pure_const_state = IPA_PURE;
409 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
411 static void
412 state_from_flags (enum pure_const_state_e *state, bool *looping,
413 int flags, bool cannot_lead_to_return)
415 *looping = false;
416 if (flags & ECF_LOOPING_CONST_OR_PURE)
418 *looping = true;
419 if (dump_file && (dump_flags & TDF_DETAILS))
420 fprintf (dump_file, " looping");
422 if (flags & ECF_CONST)
424 *state = IPA_CONST;
425 if (dump_file && (dump_flags & TDF_DETAILS))
426 fprintf (dump_file, " const\n");
428 else if (flags & ECF_PURE)
430 *state = IPA_PURE;
431 if (dump_file && (dump_flags & TDF_DETAILS))
432 fprintf (dump_file, " pure\n");
434 else if (cannot_lead_to_return)
436 *state = IPA_PURE;
437 *looping = true;
438 if (dump_file && (dump_flags & TDF_DETAILS))
439 fprintf (dump_file, " ignoring side effects->pure looping\n");
441 else
443 if (dump_file && (dump_flags & TDF_DETAILS))
444 fprintf (dump_file, " neither\n");
445 *state = IPA_NEITHER;
446 *looping = true;
450 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
451 into STATE and LOOPING better of the two variants.
452 Be sure to merge looping correctly. IPA_NEITHER functions
453 have looping 0 even if they don't have to return. */
455 static inline void
456 better_state (enum pure_const_state_e *state, bool *looping,
457 enum pure_const_state_e state2, bool looping2)
459 if (state2 < *state)
461 if (*state == IPA_NEITHER)
462 *looping = looping2;
463 else
464 *looping = MIN (*looping, looping2);
465 *state = state2;
467 else if (state2 != IPA_NEITHER)
468 *looping = MIN (*looping, looping2);
471 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
472 into STATE and LOOPING worse of the two variants.
473 N is the actual node called. */
475 static inline void
476 worse_state (enum pure_const_state_e *state, bool *looping,
477 enum pure_const_state_e state2, bool looping2,
478 struct symtab_node *from,
479 struct symtab_node *to)
481 /* Consider function:
483 bool a(int *p)
485 return *p==*p;
488 During early optimization we will turn this into:
490 bool a(int *p)
492 return true;
495 Now if this function will be detected as CONST however when interposed it
496 may end up being just pure. We always must assume the worst scenario here.
498 if (*state == IPA_CONST && state2 == IPA_CONST
499 && to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from))
501 if (dump_file && (dump_flags & TDF_DETAILS))
502 fprintf (dump_file, "Dropping state to PURE because call to %s may not "
503 "bind to current def.\n", to->name ());
504 state2 = IPA_PURE;
506 *state = MAX (*state, state2);
507 *looping = MAX (*looping, looping2);
510 /* Recognize special cases of builtins that are by themselves not pure or const
511 but function using them is. */
512 static bool
513 special_builtin_state (enum pure_const_state_e *state, bool *looping,
514 tree callee)
516 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
517 switch (DECL_FUNCTION_CODE (callee))
519 case BUILT_IN_RETURN:
520 case BUILT_IN_UNREACHABLE:
521 CASE_BUILT_IN_ALLOCA:
522 case BUILT_IN_STACK_SAVE:
523 case BUILT_IN_STACK_RESTORE:
524 case BUILT_IN_EH_POINTER:
525 case BUILT_IN_EH_FILTER:
526 case BUILT_IN_UNWIND_RESUME:
527 case BUILT_IN_CXA_END_CLEANUP:
528 case BUILT_IN_EH_COPY_VALUES:
529 case BUILT_IN_FRAME_ADDRESS:
530 case BUILT_IN_APPLY:
531 case BUILT_IN_APPLY_ARGS:
532 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
533 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
534 *looping = false;
535 *state = IPA_CONST;
536 return true;
537 case BUILT_IN_PREFETCH:
538 *looping = true;
539 *state = IPA_CONST;
540 return true;
541 default:
542 break;
544 return false;
547 /* Check the parameters of a function call to CALL_EXPR to see if
548 there are any references in the parameters that are not allowed for
549 pure or const functions. Also check to see if this is either an
550 indirect call, a call outside the compilation unit, or has special
551 attributes that may also effect the purity. The CALL_EXPR node for
552 the entire call expression. */
554 static void
555 check_call (funct_state local, gcall *call, bool ipa)
557 int flags = gimple_call_flags (call);
558 tree callee_t = gimple_call_fndecl (call);
559 bool possibly_throws = stmt_could_throw_p (call);
560 bool possibly_throws_externally = (possibly_throws
561 && stmt_can_throw_external (call));
563 if (possibly_throws)
565 unsigned int i;
566 for (i = 0; i < gimple_num_ops (call); i++)
567 if (gimple_op (call, i)
568 && tree_could_throw_p (gimple_op (call, i)))
570 if (possibly_throws && cfun->can_throw_non_call_exceptions)
572 if (dump_file)
573 fprintf (dump_file, " operand can throw; looping\n");
574 local->looping = true;
576 if (possibly_throws_externally)
578 if (dump_file)
579 fprintf (dump_file, " operand can throw externally\n");
580 local->can_throw = true;
585 /* The const and pure flags are set by a variety of places in the
586 compiler (including here). If someone has already set the flags
587 for the callee, (such as for some of the builtins) we will use
588 them, otherwise we will compute our own information.
590 Const and pure functions have less clobber effects than other
591 functions so we process these first. Otherwise if it is a call
592 outside the compilation unit or an indirect call we punt. This
593 leaves local calls which will be processed by following the call
594 graph. */
595 if (callee_t)
597 enum pure_const_state_e call_state;
598 bool call_looping;
600 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
601 && !nonfreeing_call_p (call))
602 local->can_free = true;
604 if (special_builtin_state (&call_state, &call_looping, callee_t))
606 worse_state (&local->pure_const_state, &local->looping,
607 call_state, call_looping,
608 NULL, NULL);
609 return;
611 /* When bad things happen to bad functions, they cannot be const
612 or pure. */
613 if (setjmp_call_p (callee_t))
615 if (dump_file)
616 fprintf (dump_file, " setjmp is not const/pure\n");
617 local->looping = true;
618 local->pure_const_state = IPA_NEITHER;
621 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
622 switch (DECL_FUNCTION_CODE (callee_t))
624 case BUILT_IN_LONGJMP:
625 case BUILT_IN_NONLOCAL_GOTO:
626 if (dump_file)
627 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
628 local->pure_const_state = IPA_NEITHER;
629 local->looping = true;
630 break;
631 default:
632 break;
635 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
636 local->can_free = true;
638 /* When not in IPA mode, we can still handle self recursion. */
639 if (!ipa && callee_t
640 && recursive_call_p (current_function_decl, callee_t))
642 if (dump_file)
643 fprintf (dump_file, " Recursive call can loop.\n");
644 local->looping = true;
646 /* Either callee is unknown or we are doing local analysis.
647 Look to see if there are any bits available for the callee (such as by
648 declaration or because it is builtin) and process solely on the basis of
649 those bits. Handle internal calls always, those calls don't have
650 corresponding cgraph edges and thus aren't processed during
651 the propagation. */
652 else if (!ipa || gimple_call_internal_p (call))
654 enum pure_const_state_e call_state;
655 bool call_looping;
656 if (possibly_throws && cfun->can_throw_non_call_exceptions)
658 if (dump_file)
659 fprintf (dump_file, " can throw; looping\n");
660 local->looping = true;
662 if (possibly_throws_externally)
664 if (dump_file)
666 fprintf (dump_file, " can throw externally to lp %i\n",
667 lookup_stmt_eh_lp (call));
668 if (callee_t)
669 fprintf (dump_file, " callee:%s\n",
670 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
672 local->can_throw = true;
674 if (dump_file && (dump_flags & TDF_DETAILS))
675 fprintf (dump_file, " checking flags for call:");
676 state_from_flags (&call_state, &call_looping, flags,
677 ((flags & (ECF_NORETURN | ECF_NOTHROW))
678 == (ECF_NORETURN | ECF_NOTHROW))
679 || (!flag_exceptions && (flags & ECF_NORETURN)));
680 worse_state (&local->pure_const_state, &local->looping,
681 call_state, call_looping, NULL, NULL);
683 /* Direct functions calls are handled by IPA propagation. */
686 /* Wrapper around check_decl for loads in local more. */
688 static bool
689 check_load (gimple *, tree op, tree, void *data)
691 if (DECL_P (op))
692 check_decl ((funct_state)data, op, false, false);
693 else
694 check_op ((funct_state)data, op, false);
695 return false;
698 /* Wrapper around check_decl for stores in local more. */
700 static bool
701 check_store (gimple *, tree op, tree, void *data)
703 if (DECL_P (op))
704 check_decl ((funct_state)data, op, true, false);
705 else
706 check_op ((funct_state)data, op, true);
707 return false;
710 /* Wrapper around check_decl for loads in ipa mode. */
712 static bool
713 check_ipa_load (gimple *, tree op, tree, void *data)
715 if (DECL_P (op))
716 check_decl ((funct_state)data, op, false, true);
717 else
718 check_op ((funct_state)data, op, false);
719 return false;
722 /* Wrapper around check_decl for stores in ipa mode. */
724 static bool
725 check_ipa_store (gimple *, tree op, tree, void *data)
727 if (DECL_P (op))
728 check_decl ((funct_state)data, op, true, true);
729 else
730 check_op ((funct_state)data, op, true);
731 return false;
734 /* Look into pointer pointed to by GSIP and figure out what interesting side
735 effects it has. */
736 static void
737 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
739 gimple *stmt = gsi_stmt (*gsip);
741 if (is_gimple_debug (stmt))
742 return;
744 /* Do consider clobber as side effects before IPA, so we rather inline
745 C++ destructors and keep clobber semantics than eliminate them.
747 TODO: We may get smarter during early optimizations on these and let
748 functions containing only clobbers to be optimized more. This is a common
749 case of C++ destructors. */
751 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
752 return;
754 if (dump_file)
756 fprintf (dump_file, " scanning: ");
757 print_gimple_stmt (dump_file, stmt, 0);
760 if (gimple_has_volatile_ops (stmt)
761 && !gimple_clobber_p (stmt))
763 local->pure_const_state = IPA_NEITHER;
764 if (dump_file)
765 fprintf (dump_file, " Volatile stmt is not const/pure\n");
768 /* Look for loads and stores. */
769 walk_stmt_load_store_ops (stmt, local,
770 ipa ? check_ipa_load : check_load,
771 ipa ? check_ipa_store : check_store);
773 if (gimple_code (stmt) != GIMPLE_CALL
774 && stmt_could_throw_p (stmt))
776 if (cfun->can_throw_non_call_exceptions)
778 if (dump_file)
779 fprintf (dump_file, " can throw; looping\n");
780 local->looping = true;
782 if (stmt_can_throw_external (stmt))
784 if (dump_file)
785 fprintf (dump_file, " can throw externally\n");
786 local->can_throw = true;
788 else
789 if (dump_file)
790 fprintf (dump_file, " can throw\n");
792 switch (gimple_code (stmt))
794 case GIMPLE_CALL:
795 check_call (local, as_a <gcall *> (stmt), ipa);
796 break;
797 case GIMPLE_LABEL:
798 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
799 /* Target of long jump. */
801 if (dump_file)
802 fprintf (dump_file, " nonlocal label is not const/pure\n");
803 local->pure_const_state = IPA_NEITHER;
805 break;
806 case GIMPLE_ASM:
807 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
809 if (dump_file)
810 fprintf (dump_file, " memory asm clobber is not const/pure\n");
811 /* Abandon all hope, ye who enter here. */
812 local->pure_const_state = IPA_NEITHER;
813 local->can_free = true;
815 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
817 if (dump_file)
818 fprintf (dump_file, " volatile is not const/pure\n");
819 /* Abandon all hope, ye who enter here. */
820 local->pure_const_state = IPA_NEITHER;
821 local->looping = true;
822 local->can_free = true;
824 return;
825 default:
826 break;
831 /* This is the main routine for finding the reference patterns for
832 global variables within a function FN. */
834 static funct_state
835 analyze_function (struct cgraph_node *fn, bool ipa)
837 tree decl = fn->decl;
838 funct_state l;
839 basic_block this_block;
841 l = XCNEW (struct funct_state_d);
842 l->pure_const_state = IPA_CONST;
843 l->state_previously_known = IPA_NEITHER;
844 l->looping_previously_known = true;
845 l->looping = false;
846 l->can_throw = false;
847 l->can_free = false;
848 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
849 flags_from_decl_or_type (fn->decl),
850 fn->cannot_return_p ());
852 if (fn->thunk.thunk_p || fn->alias)
854 /* Thunk gets propagated through, so nothing interesting happens. */
855 gcc_assert (ipa);
856 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
857 l->pure_const_state = IPA_NEITHER;
858 return l;
861 if (dump_file)
863 fprintf (dump_file, "\n\n local analysis of %s\n ",
864 fn->name ());
867 push_cfun (DECL_STRUCT_FUNCTION (decl));
869 FOR_EACH_BB_FN (this_block, cfun)
871 gimple_stmt_iterator gsi;
872 struct walk_stmt_info wi;
874 memset (&wi, 0, sizeof (wi));
875 for (gsi = gsi_start_bb (this_block);
876 !gsi_end_p (gsi);
877 gsi_next (&gsi))
879 check_stmt (&gsi, l, ipa);
880 if (l->pure_const_state == IPA_NEITHER
881 && l->looping
882 && l->can_throw
883 && l->can_free)
884 goto end;
888 end:
889 if (l->pure_const_state != IPA_NEITHER)
891 /* Const functions cannot have back edges (an
892 indication of possible infinite loop side
893 effect. */
894 if (mark_dfs_back_edges ())
896 /* Preheaders are needed for SCEV to work.
897 Simple latches and recorded exits improve chances that loop will
898 proved to be finite in testcases such as in loop-15.c
899 and loop-24.c */
900 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
901 | LOOPS_HAVE_SIMPLE_LATCHES
902 | LOOPS_HAVE_RECORDED_EXITS);
903 if (dump_file && (dump_flags & TDF_DETAILS))
904 flow_loops_dump (dump_file, NULL, 0);
905 if (mark_irreducible_loops ())
907 if (dump_file)
908 fprintf (dump_file, " has irreducible loops\n");
909 l->looping = true;
911 else
913 struct loop *loop;
914 scev_initialize ();
915 FOR_EACH_LOOP (loop, 0)
916 if (!finite_loop_p (loop))
918 if (dump_file)
919 fprintf (dump_file, " can not prove finiteness of "
920 "loop %i\n", loop->num);
921 l->looping =true;
922 break;
924 scev_finalize ();
926 loop_optimizer_finalize ();
930 if (dump_file && (dump_flags & TDF_DETAILS))
931 fprintf (dump_file, " checking previously known:");
933 better_state (&l->pure_const_state, &l->looping,
934 l->state_previously_known,
935 l->looping_previously_known);
936 if (TREE_NOTHROW (decl))
937 l->can_throw = false;
939 pop_cfun ();
940 if (dump_file)
942 if (l->looping)
943 fprintf (dump_file, "Function is locally looping.\n");
944 if (l->can_throw)
945 fprintf (dump_file, "Function is locally throwing.\n");
946 if (l->pure_const_state == IPA_CONST)
947 fprintf (dump_file, "Function is locally const.\n");
948 if (l->pure_const_state == IPA_PURE)
949 fprintf (dump_file, "Function is locally pure.\n");
950 if (l->can_free)
951 fprintf (dump_file, "Function can locally free.\n");
953 return l;
956 /* Called when new function is inserted to callgraph late. */
957 static void
958 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
960 /* There are some shared nodes, in particular the initializers on
961 static declarations. We do not need to scan them more than once
962 since all we would be interested in are the addressof
963 operations. */
964 if (opt_for_fn (node->decl, flag_ipa_pure_const))
965 set_function_state (node, analyze_function (node, true));
968 /* Called when new clone is inserted to callgraph late. */
970 static void
971 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
972 void *data ATTRIBUTE_UNUSED)
974 if (has_function_state (src))
976 funct_state l = XNEW (struct funct_state_d);
977 gcc_assert (!has_function_state (dst));
978 memcpy (l, get_function_state (src), sizeof (*l));
979 set_function_state (dst, l);
983 /* Called when new clone is inserted to callgraph late. */
985 static void
986 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
988 if (has_function_state (node))
989 set_function_state (node, NULL);
993 void
994 pass_ipa_pure_const::
995 register_hooks (void)
997 if (init_p)
998 return;
1000 init_p = true;
1002 node_removal_hook_holder =
1003 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
1004 node_duplication_hook_holder =
1005 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
1006 function_insertion_hook_holder =
1007 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
1011 /* Analyze each function in the cgraph to see if it is locally PURE or
1012 CONST. */
1014 static void
1015 pure_const_generate_summary (void)
1017 struct cgraph_node *node;
1019 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1020 pass->register_hooks ();
1022 /* Process all of the functions.
1024 We process AVAIL_INTERPOSABLE functions. We can not use the results
1025 by default, but the info can be used at LTO with -fwhole-program or
1026 when function got cloned and the clone is AVAILABLE. */
1028 FOR_EACH_DEFINED_FUNCTION (node)
1029 if (opt_for_fn (node->decl, flag_ipa_pure_const))
1030 set_function_state (node, analyze_function (node, true));
1034 /* Serialize the ipa info for lto. */
1036 static void
1037 pure_const_write_summary (void)
1039 struct cgraph_node *node;
1040 struct lto_simple_output_block *ob
1041 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
1042 unsigned int count = 0;
1043 lto_symtab_encoder_iterator lsei;
1044 lto_symtab_encoder_t encoder;
1046 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1048 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1049 lsei_next_function_in_partition (&lsei))
1051 node = lsei_cgraph_node (lsei);
1052 if (node->definition && has_function_state (node))
1053 count++;
1056 streamer_write_uhwi_stream (ob->main_stream, count);
1058 /* Process all of the functions. */
1059 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1060 lsei_next_function_in_partition (&lsei))
1062 node = lsei_cgraph_node (lsei);
1063 if (node->definition && has_function_state (node))
1065 struct bitpack_d bp;
1066 funct_state fs;
1067 int node_ref;
1068 lto_symtab_encoder_t encoder;
1070 fs = get_function_state (node);
1072 encoder = ob->decl_state->symtab_node_encoder;
1073 node_ref = lto_symtab_encoder_encode (encoder, node);
1074 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1076 /* Note that flags will need to be read in the opposite
1077 order as we are pushing the bitflags into FLAGS. */
1078 bp = bitpack_create (ob->main_stream);
1079 bp_pack_value (&bp, fs->pure_const_state, 2);
1080 bp_pack_value (&bp, fs->state_previously_known, 2);
1081 bp_pack_value (&bp, fs->looping_previously_known, 1);
1082 bp_pack_value (&bp, fs->looping, 1);
1083 bp_pack_value (&bp, fs->can_throw, 1);
1084 bp_pack_value (&bp, fs->can_free, 1);
1085 streamer_write_bitpack (&bp);
1089 lto_destroy_simple_output_block (ob);
1093 /* Deserialize the ipa info for lto. */
1095 static void
1096 pure_const_read_summary (void)
1098 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1099 struct lto_file_decl_data *file_data;
1100 unsigned int j = 0;
1102 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1103 pass->register_hooks ();
1105 while ((file_data = file_data_vec[j++]))
1107 const char *data;
1108 size_t len;
1109 struct lto_input_block *ib
1110 = lto_create_simple_input_block (file_data,
1111 LTO_section_ipa_pure_const,
1112 &data, &len);
1113 if (ib)
1115 unsigned int i;
1116 unsigned int count = streamer_read_uhwi (ib);
1118 for (i = 0; i < count; i++)
1120 unsigned int index;
1121 struct cgraph_node *node;
1122 struct bitpack_d bp;
1123 funct_state fs;
1124 lto_symtab_encoder_t encoder;
1126 fs = XCNEW (struct funct_state_d);
1127 index = streamer_read_uhwi (ib);
1128 encoder = file_data->symtab_node_encoder;
1129 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1130 index));
1131 set_function_state (node, fs);
1133 /* Note that the flags must be read in the opposite
1134 order in which they were written (the bitflags were
1135 pushed into FLAGS). */
1136 bp = streamer_read_bitpack (ib);
1137 fs->pure_const_state
1138 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1139 fs->state_previously_known
1140 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1141 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1142 fs->looping = bp_unpack_value (&bp, 1);
1143 fs->can_throw = bp_unpack_value (&bp, 1);
1144 fs->can_free = bp_unpack_value (&bp, 1);
1145 if (dump_file)
1147 int flags = flags_from_decl_or_type (node->decl);
1148 fprintf (dump_file, "Read info for %s ", node->dump_name ());
1149 if (flags & ECF_CONST)
1150 fprintf (dump_file, " const");
1151 if (flags & ECF_PURE)
1152 fprintf (dump_file, " pure");
1153 if (flags & ECF_NOTHROW)
1154 fprintf (dump_file, " nothrow");
1155 fprintf (dump_file, "\n pure const state: %s\n",
1156 pure_const_names[fs->pure_const_state]);
1157 fprintf (dump_file, " previously known state: %s\n",
1158 pure_const_names[fs->state_previously_known]);
1159 if (fs->looping)
1160 fprintf (dump_file," function is locally looping\n");
1161 if (fs->looping_previously_known)
1162 fprintf (dump_file," function is previously known looping\n");
1163 if (fs->can_throw)
1164 fprintf (dump_file," function is locally throwing\n");
1165 if (fs->can_free)
1166 fprintf (dump_file," function can locally free\n");
1170 lto_destroy_simple_input_block (file_data,
1171 LTO_section_ipa_pure_const,
1172 ib, data, len);
1177 /* We only propagate across edges that can throw externally and their callee
1178 is not interposable. */
1180 static bool
1181 ignore_edge_for_nothrow (struct cgraph_edge *e)
1183 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1184 return true;
1186 enum availability avail;
1187 cgraph_node *n = e->callee->function_or_virtual_thunk_symbol (&avail,
1188 e->caller);
1189 if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (n->decl))
1190 return true;
1191 return opt_for_fn (e->callee->decl, flag_non_call_exceptions)
1192 && !e->callee->binds_to_current_def_p (e->caller);
1195 /* Return true if NODE is self recursive function.
1196 Indirectly recursive functions appears as non-trivial strongly
1197 connected components, so we need to care about self recursion
1198 only. */
1200 static bool
1201 self_recursive_p (struct cgraph_node *node)
1203 struct cgraph_edge *e;
1204 for (e = node->callees; e; e = e->next_callee)
1205 if (e->callee->function_symbol () == node)
1206 return true;
1207 return false;
1210 /* Return true if N is cdtor that is not const or pure. In this case we may
1211 need to remove unreachable function if it is marked const/pure. */
1213 static bool
1214 cdtor_p (cgraph_node *n, void *)
1216 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1217 return ((!TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl))
1218 || DECL_LOOPING_CONST_OR_PURE_P (n->decl));
1219 return false;
1222 /* We only propagate across edges with non-interposable callee. */
1224 static bool
1225 ignore_edge_for_pure_const (struct cgraph_edge *e)
1227 enum availability avail;
1228 e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
1229 return (avail <= AVAIL_INTERPOSABLE);
1233 /* Produce transitive closure over the callgraph and compute pure/const
1234 attributes. */
1236 static bool
1237 propagate_pure_const (void)
1239 struct cgraph_node *node;
1240 struct cgraph_node *w;
1241 struct cgraph_node **order =
1242 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1243 int order_pos;
1244 int i;
1245 struct ipa_dfs_info * w_info;
1246 bool remove_p = false;
1247 bool has_cdtor;
1249 order_pos = ipa_reduced_postorder (order, true, false,
1250 ignore_edge_for_pure_const);
1251 if (dump_file)
1253 cgraph_node::dump_cgraph (dump_file);
1254 ipa_print_order (dump_file, "reduced", order, order_pos);
1257 /* Propagate the local information through the call graph to produce
1258 the global information. All the nodes within a cycle will have
1259 the same info so we collapse cycles first. Then we can do the
1260 propagation in one pass from the leaves to the roots. */
1261 for (i = 0; i < order_pos; i++ )
1263 enum pure_const_state_e pure_const_state = IPA_CONST;
1264 bool looping = false;
1265 int count = 0;
1266 node = order[i];
1268 if (node->alias)
1269 continue;
1271 if (dump_file && (dump_flags & TDF_DETAILS))
1272 fprintf (dump_file, "Starting cycle\n");
1274 /* Find the worst state for any node in the cycle. */
1275 w = node;
1276 while (w && pure_const_state != IPA_NEITHER)
1278 struct cgraph_edge *e;
1279 struct cgraph_edge *ie;
1280 int i;
1281 struct ipa_ref *ref = NULL;
1283 funct_state w_l = get_function_state (w);
1284 if (dump_file && (dump_flags & TDF_DETAILS))
1285 fprintf (dump_file, " Visiting %s state:%s looping %i\n",
1286 w->dump_name (),
1287 pure_const_names[w_l->pure_const_state],
1288 w_l->looping);
1290 /* First merge in function body properties.
1291 We are safe to pass NULL as FROM and TO because we will take care
1292 of possible interposition when walking callees. */
1293 worse_state (&pure_const_state, &looping,
1294 w_l->pure_const_state, w_l->looping,
1295 NULL, NULL);
1296 if (pure_const_state == IPA_NEITHER)
1297 break;
1299 count++;
1301 /* We consider recursive cycles as possibly infinite.
1302 This might be relaxed since infinite recursion leads to stack
1303 overflow. */
1304 if (count > 1)
1305 looping = true;
1307 /* Now walk the edges and merge in callee properties. */
1308 for (e = w->callees; e && pure_const_state != IPA_NEITHER;
1309 e = e->next_callee)
1311 enum availability avail;
1312 struct cgraph_node *y = e->callee->
1313 function_or_virtual_thunk_symbol (&avail,
1314 e->caller);
1315 enum pure_const_state_e edge_state = IPA_CONST;
1316 bool edge_looping = false;
1318 if (dump_file && (dump_flags & TDF_DETAILS))
1320 fprintf (dump_file, " Call to %s",
1321 e->callee->dump_name ());
1323 if (avail > AVAIL_INTERPOSABLE)
1325 funct_state y_l = get_function_state (y);
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1328 fprintf (dump_file,
1329 " state:%s looping:%i\n",
1330 pure_const_names[y_l->pure_const_state],
1331 y_l->looping);
1333 if (y_l->pure_const_state > IPA_PURE
1334 && e->cannot_lead_to_return_p ())
1336 if (dump_file && (dump_flags & TDF_DETAILS))
1337 fprintf (dump_file,
1338 " Ignoring side effects"
1339 " -> pure, looping\n");
1340 edge_state = IPA_PURE;
1341 edge_looping = true;
1343 else
1345 edge_state = y_l->pure_const_state;
1346 edge_looping = y_l->looping;
1349 else if (special_builtin_state (&edge_state, &edge_looping,
1350 y->decl))
1352 else
1353 state_from_flags (&edge_state, &edge_looping,
1354 flags_from_decl_or_type (y->decl),
1355 e->cannot_lead_to_return_p ());
1357 /* Merge the results with what we already know. */
1358 better_state (&edge_state, &edge_looping,
1359 w_l->state_previously_known,
1360 w_l->looping_previously_known);
1361 worse_state (&pure_const_state, &looping,
1362 edge_state, edge_looping, e->caller, e->callee);
1363 if (pure_const_state == IPA_NEITHER)
1364 break;
1367 /* Now process the indirect call. */
1368 for (ie = w->indirect_calls;
1369 ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee)
1371 enum pure_const_state_e edge_state = IPA_CONST;
1372 bool edge_looping = false;
1374 if (dump_file && (dump_flags & TDF_DETAILS))
1375 fprintf (dump_file, " Indirect call");
1376 state_from_flags (&edge_state, &edge_looping,
1377 ie->indirect_info->ecf_flags,
1378 ie->cannot_lead_to_return_p ());
1379 /* Merge the results with what we already know. */
1380 better_state (&edge_state, &edge_looping,
1381 w_l->state_previously_known,
1382 w_l->looping_previously_known);
1383 worse_state (&pure_const_state, &looping,
1384 edge_state, edge_looping, NULL, NULL);
1385 if (pure_const_state == IPA_NEITHER)
1386 break;
1389 /* And finally all loads and stores. */
1390 for (i = 0; w->iterate_reference (i, ref)
1391 && pure_const_state != IPA_NEITHER; i++)
1393 enum pure_const_state_e ref_state = IPA_CONST;
1394 bool ref_looping = false;
1395 switch (ref->use)
1397 case IPA_REF_LOAD:
1398 /* readonly reads are safe. */
1399 if (TREE_READONLY (ref->referred->decl))
1400 break;
1401 if (dump_file && (dump_flags & TDF_DETAILS))
1402 fprintf (dump_file, " nonreadonly global var read\n");
1403 ref_state = IPA_PURE;
1404 break;
1405 case IPA_REF_STORE:
1406 if (ref->cannot_lead_to_return ())
1407 break;
1408 ref_state = IPA_NEITHER;
1409 if (dump_file && (dump_flags & TDF_DETAILS))
1410 fprintf (dump_file, " global var write\n");
1411 break;
1412 case IPA_REF_ADDR:
1413 case IPA_REF_CHKP:
1414 break;
1415 default:
1416 gcc_unreachable ();
1418 better_state (&ref_state, &ref_looping,
1419 w_l->state_previously_known,
1420 w_l->looping_previously_known);
1421 worse_state (&pure_const_state, &looping,
1422 ref_state, ref_looping, NULL, NULL);
1423 if (pure_const_state == IPA_NEITHER)
1424 break;
1426 w_info = (struct ipa_dfs_info *) w->aux;
1427 w = w_info->next_cycle;
1429 if (dump_file && (dump_flags & TDF_DETAILS))
1430 fprintf (dump_file, "Result %s looping %i\n",
1431 pure_const_names [pure_const_state],
1432 looping);
1434 /* Find the worst state of can_free for any node in the cycle. */
1435 bool can_free = false;
1436 w = node;
1437 while (w && !can_free)
1439 struct cgraph_edge *e;
1440 funct_state w_l = get_function_state (w);
1442 if (w_l->can_free
1443 || w->get_availability () == AVAIL_INTERPOSABLE
1444 || w->indirect_calls)
1445 can_free = true;
1447 for (e = w->callees; e && !can_free; e = e->next_callee)
1449 enum availability avail;
1450 struct cgraph_node *y = e->callee->
1451 function_or_virtual_thunk_symbol (&avail,
1452 e->caller);
1454 if (avail > AVAIL_INTERPOSABLE)
1455 can_free = get_function_state (y)->can_free;
1456 else
1457 can_free = true;
1459 w_info = (struct ipa_dfs_info *) w->aux;
1460 w = w_info->next_cycle;
1463 /* Copy back the region's pure_const_state which is shared by
1464 all nodes in the region. */
1465 w = node;
1466 while (w)
1468 funct_state w_l = get_function_state (w);
1469 enum pure_const_state_e this_state = pure_const_state;
1470 bool this_looping = looping;
1472 w_l->can_free = can_free;
1473 w->nonfreeing_fn = !can_free;
1474 if (!can_free && dump_file)
1475 fprintf (dump_file, "Function found not to call free: %s\n",
1476 w->name ());
1478 if (w_l->state_previously_known != IPA_NEITHER
1479 && this_state > w_l->state_previously_known)
1481 this_state = w_l->state_previously_known;
1482 if (this_state == IPA_NEITHER)
1483 this_looping = w_l->looping_previously_known;
1485 if (!this_looping && self_recursive_p (w))
1486 this_looping = true;
1487 if (!w_l->looping_previously_known)
1488 this_looping = false;
1490 /* All nodes within a cycle share the same info. */
1491 w_l->pure_const_state = this_state;
1492 w_l->looping = this_looping;
1494 /* Inline clones share declaration with their offline copies;
1495 do not modify their declarations since the offline copy may
1496 be different. */
1497 if (!w->global.inlined_to)
1498 switch (this_state)
1500 case IPA_CONST:
1501 if (!TREE_READONLY (w->decl))
1503 warn_function_const (w->decl, !this_looping);
1504 if (dump_file)
1505 fprintf (dump_file, "Function found to be %sconst: %s\n",
1506 this_looping ? "looping " : "",
1507 w->name ());
1509 /* Turning constructor or destructor to non-looping const/pure
1510 enables us to possibly remove the function completely. */
1511 if (this_looping)
1512 has_cdtor = false;
1513 else
1514 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1515 NULL, true);
1516 if (w->set_const_flag (true, this_looping))
1518 if (dump_file)
1519 fprintf (dump_file,
1520 "Declaration updated to be %sconst: %s\n",
1521 this_looping ? "looping " : "",
1522 w->name ());
1523 remove_p |= has_cdtor;
1525 break;
1527 case IPA_PURE:
1528 if (!DECL_PURE_P (w->decl))
1530 warn_function_pure (w->decl, !this_looping);
1531 if (dump_file)
1532 fprintf (dump_file, "Function found to be %spure: %s\n",
1533 this_looping ? "looping " : "",
1534 w->name ());
1536 if (this_looping)
1537 has_cdtor = false;
1538 else
1539 has_cdtor = w->call_for_symbol_and_aliases (cdtor_p,
1540 NULL, true);
1541 if (w->set_pure_flag (true, this_looping))
1543 if (dump_file)
1544 fprintf (dump_file,
1545 "Declaration updated to be %spure: %s\n",
1546 this_looping ? "looping " : "",
1547 w->name ());
1548 remove_p |= has_cdtor;
1550 break;
1552 default:
1553 break;
1555 w_info = (struct ipa_dfs_info *) w->aux;
1556 w = w_info->next_cycle;
1560 ipa_free_postorder_info ();
1561 free (order);
1562 return remove_p;
1565 /* Produce transitive closure over the callgraph and compute nothrow
1566 attributes. */
1568 static void
1569 propagate_nothrow (void)
1571 struct cgraph_node *node;
1572 struct cgraph_node *w;
1573 struct cgraph_node **order =
1574 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1575 int order_pos;
1576 int i;
1577 struct ipa_dfs_info * w_info;
1579 order_pos = ipa_reduced_postorder (order, true, false,
1580 ignore_edge_for_nothrow);
1581 if (dump_file)
1583 cgraph_node::dump_cgraph (dump_file);
1584 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1587 /* Propagate the local information through the call graph to produce
1588 the global information. All the nodes within a cycle will have
1589 the same info so we collapse cycles first. Then we can do the
1590 propagation in one pass from the leaves to the roots. */
1591 for (i = 0; i < order_pos; i++ )
1593 bool can_throw = false;
1594 node = order[i];
1596 if (node->alias)
1597 continue;
1599 /* Find the worst state for any node in the cycle. */
1600 w = node;
1601 while (w && !can_throw)
1603 struct cgraph_edge *e, *ie;
1605 if (!TREE_NOTHROW (w->decl))
1607 funct_state w_l = get_function_state (w);
1609 if (w_l->can_throw
1610 || w->get_availability () == AVAIL_INTERPOSABLE)
1611 can_throw = true;
1613 for (e = w->callees; e && !can_throw; e = e->next_callee)
1615 enum availability avail;
1617 if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
1618 continue;
1620 struct cgraph_node *y = e->callee->
1621 function_or_virtual_thunk_symbol (&avail,
1622 e->caller);
1624 /* We can use info about the callee only if we know it can
1625 not be interposed.
1626 When callee is compiled with non-call exceptions we also
1627 must check that the declaration is bound to current
1628 body as other semantically equivalent body may still
1629 throw. */
1630 if (avail <= AVAIL_INTERPOSABLE
1631 || (!TREE_NOTHROW (y->decl)
1632 && (get_function_state (y)->can_throw
1633 || (opt_for_fn (y->decl, flag_non_call_exceptions)
1634 && !e->callee->binds_to_current_def_p (w)))))
1635 can_throw = true;
1637 for (ie = w->indirect_calls; ie && !can_throw;
1638 ie = ie->next_callee)
1639 if (ie->can_throw_external
1640 && !(ie->indirect_info->ecf_flags & ECF_NOTHROW))
1641 can_throw = true;
1643 w_info = (struct ipa_dfs_info *) w->aux;
1644 w = w_info->next_cycle;
1647 /* Copy back the region's pure_const_state which is shared by
1648 all nodes in the region. */
1649 w = node;
1650 while (w)
1652 funct_state w_l = get_function_state (w);
1653 if (!can_throw && !TREE_NOTHROW (w->decl))
1655 /* Inline clones share declaration with their offline copies;
1656 do not modify their declarations since the offline copy may
1657 be different. */
1658 if (!w->global.inlined_to)
1660 w->set_nothrow_flag (true);
1661 if (dump_file)
1662 fprintf (dump_file, "Function found to be nothrow: %s\n",
1663 w->name ());
1666 else if (can_throw && !TREE_NOTHROW (w->decl))
1667 w_l->can_throw = true;
1668 w_info = (struct ipa_dfs_info *) w->aux;
1669 w = w_info->next_cycle;
1673 ipa_free_postorder_info ();
1674 free (order);
1678 /* Produce the global information by preforming a transitive closure
1679 on the local information that was produced by generate_summary. */
1681 unsigned int
1682 pass_ipa_pure_const::
1683 execute (function *)
1685 struct cgraph_node *node;
1686 bool remove_p;
1688 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1689 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1690 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1692 /* Nothrow makes more function to not lead to return and improve
1693 later analysis. */
1694 propagate_nothrow ();
1695 remove_p = propagate_pure_const ();
1697 /* Cleanup. */
1698 FOR_EACH_FUNCTION (node)
1699 if (has_function_state (node))
1700 free (get_function_state (node));
1701 funct_state_vec.release ();
1702 return remove_p ? TODO_remove_functions : 0;
1705 static bool
1706 gate_pure_const (void)
1708 return flag_ipa_pure_const || in_lto_p;
1711 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1712 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1713 pure_const_generate_summary, /* generate_summary */
1714 pure_const_write_summary, /* write_summary */
1715 pure_const_read_summary, /* read_summary */
1716 NULL, /* write_optimization_summary */
1717 NULL, /* read_optimization_summary */
1718 NULL, /* stmt_fixup */
1719 0, /* function_transform_todo_flags_start */
1720 NULL, /* function_transform */
1721 NULL), /* variable_transform */
1722 init_p(false),
1723 function_insertion_hook_holder(NULL),
1724 node_duplication_hook_holder(NULL),
1725 node_removal_hook_holder(NULL)
1729 ipa_opt_pass_d *
1730 make_pass_ipa_pure_const (gcc::context *ctxt)
1732 return new pass_ipa_pure_const (ctxt);
1735 /* Return true if function should be skipped for local pure const analysis. */
1737 static bool
1738 skip_function_for_local_pure_const (struct cgraph_node *node)
1740 /* Because we do not schedule pass_fixup_cfg over whole program after early
1741 optimizations we must not promote functions that are called by already
1742 processed functions. */
1744 if (function_called_by_processed_nodes_p ())
1746 if (dump_file)
1747 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1748 return true;
1750 /* Save some work and do not analyze functions which are interposable and
1751 do not have any non-interposable aliases. */
1752 if (node->get_availability () <= AVAIL_INTERPOSABLE
1753 && !node->has_aliases_p ())
1755 if (dump_file)
1756 fprintf (dump_file,
1757 "Function is interposable; not analyzing.\n");
1758 return true;
1760 return false;
1763 /* Simple local pass for pure const discovery reusing the analysis from
1764 ipa_pure_const. This pass is effective when executed together with
1765 other optimization passes in early optimization pass queue. */
1767 namespace {
1769 const pass_data pass_data_local_pure_const =
1771 GIMPLE_PASS, /* type */
1772 "local-pure-const", /* name */
1773 OPTGROUP_NONE, /* optinfo_flags */
1774 TV_IPA_PURE_CONST, /* tv_id */
1775 0, /* properties_required */
1776 0, /* properties_provided */
1777 0, /* properties_destroyed */
1778 0, /* todo_flags_start */
1779 0, /* todo_flags_finish */
1782 class pass_local_pure_const : public gimple_opt_pass
1784 public:
1785 pass_local_pure_const (gcc::context *ctxt)
1786 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1789 /* opt_pass methods: */
1790 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1791 virtual bool gate (function *) { return gate_pure_const (); }
1792 virtual unsigned int execute (function *);
1794 }; // class pass_local_pure_const
1796 unsigned int
1797 pass_local_pure_const::execute (function *fun)
1799 bool changed = false;
1800 funct_state l;
1801 bool skip;
1802 struct cgraph_node *node;
1804 node = cgraph_node::get (current_function_decl);
1805 skip = skip_function_for_local_pure_const (node);
1807 if (!warn_suggest_attribute_const
1808 && !warn_suggest_attribute_pure
1809 && skip)
1810 return 0;
1812 l = analyze_function (node, false);
1814 /* Do NORETURN discovery. */
1815 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1816 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1818 warn_function_noreturn (fun->decl);
1819 if (dump_file)
1820 fprintf (dump_file, "Function found to be noreturn: %s\n",
1821 current_function_name ());
1823 /* Update declaration and reduce profile to executed once. */
1824 TREE_THIS_VOLATILE (current_function_decl) = 1;
1825 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1826 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1828 changed = true;
1831 switch (l->pure_const_state)
1833 case IPA_CONST:
1834 if (!TREE_READONLY (current_function_decl))
1836 warn_function_const (current_function_decl, !l->looping);
1837 if (dump_file)
1838 fprintf (dump_file, "Function found to be %sconst: %s\n",
1839 l->looping ? "looping " : "",
1840 current_function_name ());
1842 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1843 && !l->looping)
1845 if (dump_file)
1846 fprintf (dump_file, "Function found to be non-looping: %s\n",
1847 current_function_name ());
1849 if (!skip && node->set_const_flag (true, l->looping))
1851 if (dump_file)
1852 fprintf (dump_file, "Declaration updated to be %sconst: %s\n",
1853 l->looping ? "looping " : "",
1854 current_function_name ());
1855 changed = true;
1857 break;
1859 case IPA_PURE:
1860 if (!DECL_PURE_P (current_function_decl))
1862 warn_function_pure (current_function_decl, !l->looping);
1863 if (dump_file)
1864 fprintf (dump_file, "Function found to be %spure: %s\n",
1865 l->looping ? "looping " : "",
1866 current_function_name ());
1868 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1869 && !l->looping)
1871 if (dump_file)
1872 fprintf (dump_file, "Function found to be non-looping: %s\n",
1873 current_function_name ());
1875 if (!skip && node->set_pure_flag (true, l->looping))
1877 if (dump_file)
1878 fprintf (dump_file, "Declaration updated to be %spure: %s\n",
1879 l->looping ? "looping " : "",
1880 current_function_name ());
1881 changed = true;
1883 break;
1885 default:
1886 break;
1888 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1890 node->set_nothrow_flag (true);
1891 changed = true;
1892 if (dump_file)
1893 fprintf (dump_file, "Function found to be nothrow: %s\n",
1894 current_function_name ());
1896 free (l);
1897 if (changed)
1898 return execute_fixup_cfg ();
1899 else
1900 return 0;
1903 } // anon namespace
1905 gimple_opt_pass *
1906 make_pass_local_pure_const (gcc::context *ctxt)
1908 return new pass_local_pure_const (ctxt);
1911 /* Emit noreturn warnings. */
1913 namespace {
1915 const pass_data pass_data_warn_function_noreturn =
1917 GIMPLE_PASS, /* type */
1918 "*warn_function_noreturn", /* name */
1919 OPTGROUP_NONE, /* optinfo_flags */
1920 TV_NONE, /* tv_id */
1921 PROP_cfg, /* properties_required */
1922 0, /* properties_provided */
1923 0, /* properties_destroyed */
1924 0, /* todo_flags_start */
1925 0, /* todo_flags_finish */
1928 class pass_warn_function_noreturn : public gimple_opt_pass
1930 public:
1931 pass_warn_function_noreturn (gcc::context *ctxt)
1932 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1935 /* opt_pass methods: */
1936 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1937 virtual unsigned int execute (function *fun)
1939 if (!TREE_THIS_VOLATILE (current_function_decl)
1940 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1941 warn_function_noreturn (current_function_decl);
1942 return 0;
1945 }; // class pass_warn_function_noreturn
1947 } // anon namespace
1949 gimple_opt_pass *
1950 make_pass_warn_function_noreturn (gcc::context *ctxt)
1952 return new pass_warn_function_noreturn (ctxt);
1955 /* Simple local pass for pure const discovery reusing the analysis from
1956 ipa_pure_const. This pass is effective when executed together with
1957 other optimization passes in early optimization pass queue. */
1959 namespace {
1961 const pass_data pass_data_nothrow =
1963 GIMPLE_PASS, /* type */
1964 "nothrow", /* name */
1965 OPTGROUP_NONE, /* optinfo_flags */
1966 TV_IPA_PURE_CONST, /* tv_id */
1967 0, /* properties_required */
1968 0, /* properties_provided */
1969 0, /* properties_destroyed */
1970 0, /* todo_flags_start */
1971 0, /* todo_flags_finish */
1974 class pass_nothrow : public gimple_opt_pass
1976 public:
1977 pass_nothrow (gcc::context *ctxt)
1978 : gimple_opt_pass (pass_data_nothrow, ctxt)
1981 /* opt_pass methods: */
1982 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1983 virtual bool gate (function *) { return optimize; }
1984 virtual unsigned int execute (function *);
1986 }; // class pass_nothrow
1988 unsigned int
1989 pass_nothrow::execute (function *)
1991 struct cgraph_node *node;
1992 basic_block this_block;
1994 if (TREE_NOTHROW (current_function_decl))
1995 return 0;
1997 node = cgraph_node::get (current_function_decl);
1999 /* We run during lowering, we can not really use availability yet. */
2000 if (cgraph_node::get (current_function_decl)->get_availability ()
2001 <= AVAIL_INTERPOSABLE)
2003 if (dump_file)
2004 fprintf (dump_file, "Function is interposable;"
2005 " not analyzing.\n");
2006 return true;
2009 FOR_EACH_BB_FN (this_block, cfun)
2011 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
2012 !gsi_end_p (gsi);
2013 gsi_next (&gsi))
2014 if (stmt_can_throw_external (gsi_stmt (gsi)))
2016 if (is_gimple_call (gsi_stmt (gsi)))
2018 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
2019 if (callee_t && recursive_call_p (current_function_decl,
2020 callee_t))
2021 continue;
2024 if (dump_file)
2026 fprintf (dump_file, "Statement can throw: ");
2027 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
2029 return 0;
2033 node->set_nothrow_flag (true);
2035 bool cfg_changed = false;
2036 if (self_recursive_p (node))
2037 FOR_EACH_BB_FN (this_block, cfun)
2038 if (gimple *g = last_stmt (this_block))
2039 if (is_gimple_call (g))
2041 tree callee_t = gimple_call_fndecl (g);
2042 if (callee_t
2043 && recursive_call_p (current_function_decl, callee_t)
2044 && maybe_clean_eh_stmt (g)
2045 && gimple_purge_dead_eh_edges (this_block))
2046 cfg_changed = true;
2049 if (dump_file)
2050 fprintf (dump_file, "Function found to be nothrow: %s\n",
2051 current_function_name ());
2052 return cfg_changed ? TODO_cleanup_cfg : 0;
2055 } // anon namespace
2057 gimple_opt_pass *
2058 make_pass_nothrow (gcc::context *ctxt)
2060 return new pass_nothrow (ctxt);