alias.c: Reorder #include statements and remove duplicates.
[gcc.git] / gcc / ipa-pure-const.c
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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).
24
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. */
29
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. */
33
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 "alias.h"
46 #include "fold-const.h"
47 #include "print-tree.h"
48 #include "calls.h"
49 #include "cfganal.h"
50 #include "internal-fn.h"
51 #include "tree-eh.h"
52 #include "gimple-iterator.h"
53 #include "gimple-walk.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-inline.h"
57 #include "langhooks.h"
58 #include "ipa-utils.h"
59 #include "flags.h"
60 #include "gimple-pretty-print.h"
61 #include "cfgloop.h"
62 #include "tree-scalar-evolution.h"
63 #include "intl.h"
64 #include "opts.h"
65 #include "varasm.h"
66
67 /* Lattice values for const and pure functions. Everything starts out
68 being const, then may drop to pure and then neither depending on
69 what is found. */
70 enum pure_const_state_e
71 {
72 IPA_CONST,
73 IPA_PURE,
74 IPA_NEITHER
75 };
76
77 const char *pure_const_names[3] = {"const", "pure", "neither"};
78
79 /* Holder for the const_state. There is one of these per function
80 decl. */
81 struct funct_state_d
82 {
83 /* See above. */
84 enum pure_const_state_e pure_const_state;
85 /* What user set here; we can be always sure about this. */
86 enum pure_const_state_e state_previously_known;
87 bool looping_previously_known;
88
89 /* True if the function could possibly infinite loop. There are a
90 lot of ways that this could be determined. We are pretty
91 conservative here. While it is possible to cse pure and const
92 calls, it is not legal to have dce get rid of the call if there
93 is a possibility that the call could infinite loop since this is
94 a behavioral change. */
95 bool looping;
96
97 bool can_throw;
98
99 /* If function can call free, munmap or otherwise make previously
100 non-trapping memory accesses trapping. */
101 bool can_free;
102 };
103
104 /* State used when we know nothing about function. */
105 static struct funct_state_d varying_state
106 = { IPA_NEITHER, IPA_NEITHER, true, true, true, true };
107
108
109 typedef struct funct_state_d * funct_state;
110
111 /* The storage of the funct_state is abstracted because there is the
112 possibility that it may be desirable to move this to the cgraph
113 local info. */
114
115 /* Array, indexed by cgraph node uid, of function states. */
116
117 static vec<funct_state> funct_state_vec;
118
119 static bool gate_pure_const (void);
120
121 namespace {
122
123 const pass_data pass_data_ipa_pure_const =
124 {
125 IPA_PASS, /* type */
126 "pure-const", /* name */
127 OPTGROUP_NONE, /* optinfo_flags */
128 TV_IPA_PURE_CONST, /* tv_id */
129 0, /* properties_required */
130 0, /* properties_provided */
131 0, /* properties_destroyed */
132 0, /* todo_flags_start */
133 0, /* todo_flags_finish */
134 };
135
136 class pass_ipa_pure_const : public ipa_opt_pass_d
137 {
138 public:
139 pass_ipa_pure_const(gcc::context *ctxt);
140
141 /* opt_pass methods: */
142 bool gate (function *) { return gate_pure_const (); }
143 unsigned int execute (function *fun);
144
145 void register_hooks (void);
146
147 private:
148 bool init_p;
149
150 /* Holders of ipa cgraph hooks: */
151 struct cgraph_node_hook_list *function_insertion_hook_holder;
152 struct cgraph_2node_hook_list *node_duplication_hook_holder;
153 struct cgraph_node_hook_list *node_removal_hook_holder;
154
155 }; // class pass_ipa_pure_const
156
157 } // anon namespace
158
159 /* Try to guess if function body will always be visible to compiler
160 when compiling the call and whether compiler will be able
161 to propagate the information by itself. */
162
163 static bool
164 function_always_visible_to_compiler_p (tree decl)
165 {
166 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
167 }
168
169 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
170 is true if the function is known to be finite. The diagnostic is
171 controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for
172 OPTION, this function may initialize it and it is always returned
173 by the function. */
174
175 static hash_set<tree> *
176 suggest_attribute (int option, tree decl, bool known_finite,
177 hash_set<tree> *warned_about,
178 const char * attrib_name)
179 {
180 if (!option_enabled (option, &global_options))
181 return warned_about;
182 if (TREE_THIS_VOLATILE (decl)
183 || (known_finite && function_always_visible_to_compiler_p (decl)))
184 return warned_about;
185
186 if (!warned_about)
187 warned_about = new hash_set<tree>;
188 if (warned_about->contains (decl))
189 return warned_about;
190 warned_about->add (decl);
191 warning_at (DECL_SOURCE_LOCATION (decl),
192 option,
193 known_finite
194 ? _("function might be candidate for attribute %<%s%>")
195 : _("function might be candidate for attribute %<%s%>"
196 " if it is known to return normally"), attrib_name);
197 return warned_about;
198 }
199
200 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
201 is true if the function is known to be finite. */
202
203 static void
204 warn_function_pure (tree decl, bool known_finite)
205 {
206 static hash_set<tree> *warned_about;
207
208 warned_about
209 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
210 known_finite, warned_about, "pure");
211 }
212
213 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
214 is true if the function is known to be finite. */
215
216 static void
217 warn_function_const (tree decl, bool known_finite)
218 {
219 static hash_set<tree> *warned_about;
220 warned_about
221 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
222 known_finite, warned_about, "const");
223 }
224
225 static void
226 warn_function_noreturn (tree decl)
227 {
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, decl,
233 true, warned_about, "noreturn");
234 }
235
236 /* Return true if we have a function state for NODE. */
237
238 static inline bool
239 has_function_state (struct cgraph_node *node)
240 {
241 if (!funct_state_vec.exists ()
242 || funct_state_vec.length () <= (unsigned int)node->uid)
243 return false;
244 return funct_state_vec[node->uid] != NULL;
245 }
246
247 /* Return the function state from NODE. */
248
249 static inline funct_state
250 get_function_state (struct cgraph_node *node)
251 {
252 if (!funct_state_vec.exists ()
253 || funct_state_vec.length () <= (unsigned int)node->uid
254 || !funct_state_vec[node->uid])
255 /* We might want to put correct previously_known state into varying. */
256 return &varying_state;
257 return funct_state_vec[node->uid];
258 }
259
260 /* Set the function state S for NODE. */
261
262 static inline void
263 set_function_state (struct cgraph_node *node, funct_state s)
264 {
265 if (!funct_state_vec.exists ()
266 || funct_state_vec.length () <= (unsigned int)node->uid)
267 funct_state_vec.safe_grow_cleared (node->uid + 1);
268 funct_state_vec[node->uid] = s;
269 }
270
271 /* Check to see if the use (or definition when CHECKING_WRITE is true)
272 variable T is legal in a function that is either pure or const. */
273
274 static inline void
275 check_decl (funct_state local,
276 tree t, bool checking_write, bool ipa)
277 {
278 /* Do not want to do anything with volatile except mark any
279 function that uses one to be not const or pure. */
280 if (TREE_THIS_VOLATILE (t))
281 {
282 local->pure_const_state = IPA_NEITHER;
283 if (dump_file)
284 fprintf (dump_file, " Volatile operand is not const/pure");
285 return;
286 }
287
288 /* Do not care about a local automatic that is not static. */
289 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
290 return;
291
292 /* If the variable has the "used" attribute, treat it as if it had a
293 been touched by the devil. */
294 if (DECL_PRESERVE_P (t))
295 {
296 local->pure_const_state = IPA_NEITHER;
297 if (dump_file)
298 fprintf (dump_file, " Used static/global variable is not const/pure\n");
299 return;
300 }
301
302 /* In IPA mode we are not interested in checking actual loads and stores;
303 they will be processed at propagation time using ipa_ref. */
304 if (ipa)
305 return;
306
307 /* Since we have dealt with the locals and params cases above, if we
308 are CHECKING_WRITE, this cannot be a pure or constant
309 function. */
310 if (checking_write)
311 {
312 local->pure_const_state = IPA_NEITHER;
313 if (dump_file)
314 fprintf (dump_file, " static/global memory write is not const/pure\n");
315 return;
316 }
317
318 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
319 {
320 /* Readonly reads are safe. */
321 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
322 return; /* Read of a constant, do not change the function state. */
323 else
324 {
325 if (dump_file)
326 fprintf (dump_file, " global memory read is not const\n");
327 /* Just a regular read. */
328 if (local->pure_const_state == IPA_CONST)
329 local->pure_const_state = IPA_PURE;
330 }
331 }
332 else
333 {
334 /* Compilation level statics can be read if they are readonly
335 variables. */
336 if (TREE_READONLY (t))
337 return;
338
339 if (dump_file)
340 fprintf (dump_file, " static memory read is not const\n");
341 /* Just a regular read. */
342 if (local->pure_const_state == IPA_CONST)
343 local->pure_const_state = IPA_PURE;
344 }
345 }
346
347
348 /* Check to see if the use (or definition when CHECKING_WRITE is true)
349 variable T is legal in a function that is either pure or const. */
350
351 static inline void
352 check_op (funct_state local, tree t, bool checking_write)
353 {
354 t = get_base_address (t);
355 if (t && TREE_THIS_VOLATILE (t))
356 {
357 local->pure_const_state = IPA_NEITHER;
358 if (dump_file)
359 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
360 return;
361 }
362 else if (t
363 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
364 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
365 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
366 {
367 if (dump_file)
368 fprintf (dump_file, " Indirect ref to local memory is OK\n");
369 return;
370 }
371 else if (checking_write)
372 {
373 local->pure_const_state = IPA_NEITHER;
374 if (dump_file)
375 fprintf (dump_file, " Indirect ref write is not const/pure\n");
376 return;
377 }
378 else
379 {
380 if (dump_file)
381 fprintf (dump_file, " Indirect ref read is not const\n");
382 if (local->pure_const_state == IPA_CONST)
383 local->pure_const_state = IPA_PURE;
384 }
385 }
386
387 /* compute state based on ECF FLAGS and store to STATE and LOOPING. */
388
389 static void
390 state_from_flags (enum pure_const_state_e *state, bool *looping,
391 int flags, bool cannot_lead_to_return)
392 {
393 *looping = false;
394 if (flags & ECF_LOOPING_CONST_OR_PURE)
395 {
396 *looping = true;
397 if (dump_file && (dump_flags & TDF_DETAILS))
398 fprintf (dump_file, " looping");
399 }
400 if (flags & ECF_CONST)
401 {
402 *state = IPA_CONST;
403 if (dump_file && (dump_flags & TDF_DETAILS))
404 fprintf (dump_file, " const\n");
405 }
406 else if (flags & ECF_PURE)
407 {
408 *state = IPA_PURE;
409 if (dump_file && (dump_flags & TDF_DETAILS))
410 fprintf (dump_file, " pure\n");
411 }
412 else if (cannot_lead_to_return)
413 {
414 *state = IPA_PURE;
415 *looping = true;
416 if (dump_file && (dump_flags & TDF_DETAILS))
417 fprintf (dump_file, " ignoring side effects->pure looping\n");
418 }
419 else
420 {
421 if (dump_file && (dump_flags & TDF_DETAILS))
422 fprintf (dump_file, " neither\n");
423 *state = IPA_NEITHER;
424 *looping = true;
425 }
426 }
427
428 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
429 into STATE and LOOPING better of the two variants.
430 Be sure to merge looping correctly. IPA_NEITHER functions
431 have looping 0 even if they don't have to return. */
432
433 static inline void
434 better_state (enum pure_const_state_e *state, bool *looping,
435 enum pure_const_state_e state2, bool looping2)
436 {
437 if (state2 < *state)
438 {
439 if (*state == IPA_NEITHER)
440 *looping = looping2;
441 else
442 *looping = MIN (*looping, looping2);
443 *state = state2;
444 }
445 else if (state2 != IPA_NEITHER)
446 *looping = MIN (*looping, looping2);
447 }
448
449 /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
450 into STATE and LOOPING worse of the two variants. */
451
452 static inline void
453 worse_state (enum pure_const_state_e *state, bool *looping,
454 enum pure_const_state_e state2, bool looping2)
455 {
456 *state = MAX (*state, state2);
457 *looping = MAX (*looping, looping2);
458 }
459
460 /* Recognize special cases of builtins that are by themselves not pure or const
461 but function using them is. */
462 static bool
463 special_builtin_state (enum pure_const_state_e *state, bool *looping,
464 tree callee)
465 {
466 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
467 switch (DECL_FUNCTION_CODE (callee))
468 {
469 case BUILT_IN_RETURN:
470 case BUILT_IN_UNREACHABLE:
471 case BUILT_IN_ALLOCA:
472 case BUILT_IN_ALLOCA_WITH_ALIGN:
473 case BUILT_IN_STACK_SAVE:
474 case BUILT_IN_STACK_RESTORE:
475 case BUILT_IN_EH_POINTER:
476 case BUILT_IN_EH_FILTER:
477 case BUILT_IN_UNWIND_RESUME:
478 case BUILT_IN_CXA_END_CLEANUP:
479 case BUILT_IN_EH_COPY_VALUES:
480 case BUILT_IN_FRAME_ADDRESS:
481 case BUILT_IN_APPLY:
482 case BUILT_IN_APPLY_ARGS:
483 *looping = false;
484 *state = IPA_CONST;
485 return true;
486 case BUILT_IN_PREFETCH:
487 *looping = true;
488 *state = IPA_CONST;
489 return true;
490 default:
491 break;
492 }
493 return false;
494 }
495
496 /* Check the parameters of a function call to CALL_EXPR to see if
497 there are any references in the parameters that are not allowed for
498 pure or const functions. Also check to see if this is either an
499 indirect call, a call outside the compilation unit, or has special
500 attributes that may also effect the purity. The CALL_EXPR node for
501 the entire call expression. */
502
503 static void
504 check_call (funct_state local, gcall *call, bool ipa)
505 {
506 int flags = gimple_call_flags (call);
507 tree callee_t = gimple_call_fndecl (call);
508 bool possibly_throws = stmt_could_throw_p (call);
509 bool possibly_throws_externally = (possibly_throws
510 && stmt_can_throw_external (call));
511
512 if (possibly_throws)
513 {
514 unsigned int i;
515 for (i = 0; i < gimple_num_ops (call); i++)
516 if (gimple_op (call, i)
517 && tree_could_throw_p (gimple_op (call, i)))
518 {
519 if (possibly_throws && cfun->can_throw_non_call_exceptions)
520 {
521 if (dump_file)
522 fprintf (dump_file, " operand can throw; looping\n");
523 local->looping = true;
524 }
525 if (possibly_throws_externally)
526 {
527 if (dump_file)
528 fprintf (dump_file, " operand can throw externally\n");
529 local->can_throw = true;
530 }
531 }
532 }
533
534 /* The const and pure flags are set by a variety of places in the
535 compiler (including here). If someone has already set the flags
536 for the callee, (such as for some of the builtins) we will use
537 them, otherwise we will compute our own information.
538
539 Const and pure functions have less clobber effects than other
540 functions so we process these first. Otherwise if it is a call
541 outside the compilation unit or an indirect call we punt. This
542 leaves local calls which will be processed by following the call
543 graph. */
544 if (callee_t)
545 {
546 enum pure_const_state_e call_state;
547 bool call_looping;
548
549 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
550 && !nonfreeing_call_p (call))
551 local->can_free = true;
552
553 if (special_builtin_state (&call_state, &call_looping, callee_t))
554 {
555 worse_state (&local->pure_const_state, &local->looping,
556 call_state, call_looping);
557 return;
558 }
559 /* When bad things happen to bad functions, they cannot be const
560 or pure. */
561 if (setjmp_call_p (callee_t))
562 {
563 if (dump_file)
564 fprintf (dump_file, " setjmp is not const/pure\n");
565 local->looping = true;
566 local->pure_const_state = IPA_NEITHER;
567 }
568
569 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
570 switch (DECL_FUNCTION_CODE (callee_t))
571 {
572 case BUILT_IN_LONGJMP:
573 case BUILT_IN_NONLOCAL_GOTO:
574 if (dump_file)
575 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
576 local->pure_const_state = IPA_NEITHER;
577 local->looping = true;
578 break;
579 default:
580 break;
581 }
582 }
583 else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
584 local->can_free = true;
585
586 /* When not in IPA mode, we can still handle self recursion. */
587 if (!ipa && callee_t
588 && recursive_call_p (current_function_decl, callee_t))
589 {
590 if (dump_file)
591 fprintf (dump_file, " Recursive call can loop.\n");
592 local->looping = true;
593 }
594 /* Either callee is unknown or we are doing local analysis.
595 Look to see if there are any bits available for the callee (such as by
596 declaration or because it is builtin) and process solely on the basis of
597 those bits. */
598 else if (!ipa)
599 {
600 enum pure_const_state_e call_state;
601 bool call_looping;
602 if (possibly_throws && cfun->can_throw_non_call_exceptions)
603 {
604 if (dump_file)
605 fprintf (dump_file, " can throw; looping\n");
606 local->looping = true;
607 }
608 if (possibly_throws_externally)
609 {
610 if (dump_file)
611 {
612 fprintf (dump_file, " can throw externally to lp %i\n",
613 lookup_stmt_eh_lp (call));
614 if (callee_t)
615 fprintf (dump_file, " callee:%s\n",
616 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
617 }
618 local->can_throw = true;
619 }
620 if (dump_file && (dump_flags & TDF_DETAILS))
621 fprintf (dump_file, " checking flags for call:");
622 state_from_flags (&call_state, &call_looping, flags,
623 ((flags & (ECF_NORETURN | ECF_NOTHROW))
624 == (ECF_NORETURN | ECF_NOTHROW))
625 || (!flag_exceptions && (flags & ECF_NORETURN)));
626 worse_state (&local->pure_const_state, &local->looping,
627 call_state, call_looping);
628 }
629 /* Direct functions calls are handled by IPA propagation. */
630 }
631
632 /* Wrapper around check_decl for loads in local more. */
633
634 static bool
635 check_load (gimple *, tree op, tree, void *data)
636 {
637 if (DECL_P (op))
638 check_decl ((funct_state)data, op, false, false);
639 else
640 check_op ((funct_state)data, op, false);
641 return false;
642 }
643
644 /* Wrapper around check_decl for stores in local more. */
645
646 static bool
647 check_store (gimple *, tree op, tree, void *data)
648 {
649 if (DECL_P (op))
650 check_decl ((funct_state)data, op, true, false);
651 else
652 check_op ((funct_state)data, op, true);
653 return false;
654 }
655
656 /* Wrapper around check_decl for loads in ipa mode. */
657
658 static bool
659 check_ipa_load (gimple *, tree op, tree, void *data)
660 {
661 if (DECL_P (op))
662 check_decl ((funct_state)data, op, false, true);
663 else
664 check_op ((funct_state)data, op, false);
665 return false;
666 }
667
668 /* Wrapper around check_decl for stores in ipa mode. */
669
670 static bool
671 check_ipa_store (gimple *, tree op, tree, void *data)
672 {
673 if (DECL_P (op))
674 check_decl ((funct_state)data, op, true, true);
675 else
676 check_op ((funct_state)data, op, true);
677 return false;
678 }
679
680 /* Look into pointer pointed to by GSIP and figure out what interesting side
681 effects it has. */
682 static void
683 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
684 {
685 gimple *stmt = gsi_stmt (*gsip);
686
687 if (is_gimple_debug (stmt))
688 return;
689
690 /* Do consider clobber as side effects before IPA, so we rather inline
691 C++ destructors and keep clobber semantics than eliminate them.
692
693 TODO: We may get smarter during early optimizations on these and let
694 functions containing only clobbers to be optimized more. This is a common
695 case of C++ destructors. */
696
697 if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
698 return;
699
700 if (dump_file)
701 {
702 fprintf (dump_file, " scanning: ");
703 print_gimple_stmt (dump_file, stmt, 0, 0);
704 }
705
706 if (gimple_has_volatile_ops (stmt)
707 && !gimple_clobber_p (stmt))
708 {
709 local->pure_const_state = IPA_NEITHER;
710 if (dump_file)
711 fprintf (dump_file, " Volatile stmt is not const/pure\n");
712 }
713
714 /* Look for loads and stores. */
715 walk_stmt_load_store_ops (stmt, local,
716 ipa ? check_ipa_load : check_load,
717 ipa ? check_ipa_store : check_store);
718
719 if (gimple_code (stmt) != GIMPLE_CALL
720 && stmt_could_throw_p (stmt))
721 {
722 if (cfun->can_throw_non_call_exceptions)
723 {
724 if (dump_file)
725 fprintf (dump_file, " can throw; looping\n");
726 local->looping = true;
727 }
728 if (stmt_can_throw_external (stmt))
729 {
730 if (dump_file)
731 fprintf (dump_file, " can throw externally\n");
732 local->can_throw = true;
733 }
734 else
735 if (dump_file)
736 fprintf (dump_file, " can throw\n");
737 }
738 switch (gimple_code (stmt))
739 {
740 case GIMPLE_CALL:
741 check_call (local, as_a <gcall *> (stmt), ipa);
742 break;
743 case GIMPLE_LABEL:
744 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
745 /* Target of long jump. */
746 {
747 if (dump_file)
748 fprintf (dump_file, " nonlocal label is not const/pure\n");
749 local->pure_const_state = IPA_NEITHER;
750 }
751 break;
752 case GIMPLE_ASM:
753 if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
754 {
755 if (dump_file)
756 fprintf (dump_file, " memory asm clobber is not const/pure\n");
757 /* Abandon all hope, ye who enter here. */
758 local->pure_const_state = IPA_NEITHER;
759 local->can_free = true;
760 }
761 if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
762 {
763 if (dump_file)
764 fprintf (dump_file, " volatile is not const/pure\n");
765 /* Abandon all hope, ye who enter here. */
766 local->pure_const_state = IPA_NEITHER;
767 local->looping = true;
768 local->can_free = true;
769 }
770 return;
771 default:
772 break;
773 }
774 }
775
776
777 /* This is the main routine for finding the reference patterns for
778 global variables within a function FN. */
779
780 static funct_state
781 analyze_function (struct cgraph_node *fn, bool ipa)
782 {
783 tree decl = fn->decl;
784 funct_state l;
785 basic_block this_block;
786
787 l = XCNEW (struct funct_state_d);
788 l->pure_const_state = IPA_CONST;
789 l->state_previously_known = IPA_NEITHER;
790 l->looping_previously_known = true;
791 l->looping = false;
792 l->can_throw = false;
793 l->can_free = false;
794 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
795 flags_from_decl_or_type (fn->decl),
796 fn->cannot_return_p ());
797
798 if (fn->thunk.thunk_p || fn->alias)
799 {
800 /* Thunk gets propagated through, so nothing interesting happens. */
801 gcc_assert (ipa);
802 if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p)
803 l->pure_const_state = IPA_NEITHER;
804 return l;
805 }
806
807 if (dump_file)
808 {
809 fprintf (dump_file, "\n\n local analysis of %s\n ",
810 fn->name ());
811 }
812
813 push_cfun (DECL_STRUCT_FUNCTION (decl));
814
815 FOR_EACH_BB_FN (this_block, cfun)
816 {
817 gimple_stmt_iterator gsi;
818 struct walk_stmt_info wi;
819
820 memset (&wi, 0, sizeof (wi));
821 for (gsi = gsi_start_bb (this_block);
822 !gsi_end_p (gsi);
823 gsi_next (&gsi))
824 {
825 check_stmt (&gsi, l, ipa);
826 if (l->pure_const_state == IPA_NEITHER
827 && l->looping
828 && l->can_throw
829 && l->can_free)
830 goto end;
831 }
832 }
833
834 end:
835 if (l->pure_const_state != IPA_NEITHER)
836 {
837 /* Const functions cannot have back edges (an
838 indication of possible infinite loop side
839 effect. */
840 if (mark_dfs_back_edges ())
841 {
842 /* Preheaders are needed for SCEV to work.
843 Simple latches and recorded exits improve chances that loop will
844 proved to be finite in testcases such as in loop-15.c
845 and loop-24.c */
846 loop_optimizer_init (LOOPS_HAVE_PREHEADERS
847 | LOOPS_HAVE_SIMPLE_LATCHES
848 | LOOPS_HAVE_RECORDED_EXITS);
849 if (dump_file && (dump_flags & TDF_DETAILS))
850 flow_loops_dump (dump_file, NULL, 0);
851 if (mark_irreducible_loops ())
852 {
853 if (dump_file)
854 fprintf (dump_file, " has irreducible loops\n");
855 l->looping = true;
856 }
857 else
858 {
859 struct loop *loop;
860 scev_initialize ();
861 FOR_EACH_LOOP (loop, 0)
862 if (!finite_loop_p (loop))
863 {
864 if (dump_file)
865 fprintf (dump_file, " can not prove finiteness of "
866 "loop %i\n", loop->num);
867 l->looping =true;
868 break;
869 }
870 scev_finalize ();
871 }
872 loop_optimizer_finalize ();
873 }
874 }
875
876 if (dump_file && (dump_flags & TDF_DETAILS))
877 fprintf (dump_file, " checking previously known:");
878
879 better_state (&l->pure_const_state, &l->looping,
880 l->state_previously_known,
881 l->looping_previously_known);
882 if (TREE_NOTHROW (decl))
883 l->can_throw = false;
884
885 pop_cfun ();
886 if (dump_file)
887 {
888 if (l->looping)
889 fprintf (dump_file, "Function is locally looping.\n");
890 if (l->can_throw)
891 fprintf (dump_file, "Function is locally throwing.\n");
892 if (l->pure_const_state == IPA_CONST)
893 fprintf (dump_file, "Function is locally const.\n");
894 if (l->pure_const_state == IPA_PURE)
895 fprintf (dump_file, "Function is locally pure.\n");
896 if (l->can_free)
897 fprintf (dump_file, "Function can locally free.\n");
898 }
899 return l;
900 }
901
902 /* Called when new function is inserted to callgraph late. */
903 static void
904 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
905 {
906 if (node->get_availability () < AVAIL_INTERPOSABLE)
907 return;
908 /* There are some shared nodes, in particular the initializers on
909 static declarations. We do not need to scan them more than once
910 since all we would be interested in are the addressof
911 operations. */
912 if (node->get_availability () > AVAIL_INTERPOSABLE
913 && opt_for_fn (node->decl, flag_ipa_pure_const))
914 set_function_state (node, analyze_function (node, true));
915 }
916
917 /* Called when new clone is inserted to callgraph late. */
918
919 static void
920 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
921 void *data ATTRIBUTE_UNUSED)
922 {
923 if (has_function_state (src))
924 {
925 funct_state l = XNEW (struct funct_state_d);
926 gcc_assert (!has_function_state (dst));
927 memcpy (l, get_function_state (src), sizeof (*l));
928 set_function_state (dst, l);
929 }
930 }
931
932 /* Called when new clone is inserted to callgraph late. */
933
934 static void
935 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
936 {
937 if (has_function_state (node))
938 {
939 funct_state l = get_function_state (node);
940 if (l != &varying_state)
941 free (l);
942 set_function_state (node, NULL);
943 }
944 }
945
946 \f
947 void
948 pass_ipa_pure_const::
949 register_hooks (void)
950 {
951 if (init_p)
952 return;
953
954 init_p = true;
955
956 node_removal_hook_holder =
957 symtab->add_cgraph_removal_hook (&remove_node_data, NULL);
958 node_duplication_hook_holder =
959 symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL);
960 function_insertion_hook_holder =
961 symtab->add_cgraph_insertion_hook (&add_new_function, NULL);
962 }
963
964
965 /* Analyze each function in the cgraph to see if it is locally PURE or
966 CONST. */
967
968 static void
969 pure_const_generate_summary (void)
970 {
971 struct cgraph_node *node;
972
973 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
974 pass->register_hooks ();
975
976 /* Process all of the functions.
977
978 We process AVAIL_INTERPOSABLE functions. We can not use the results
979 by default, but the info can be used at LTO with -fwhole-program or
980 when function got cloned and the clone is AVAILABLE. */
981
982 FOR_EACH_DEFINED_FUNCTION (node)
983 if (node->get_availability () >= AVAIL_INTERPOSABLE
984 && opt_for_fn (node->decl, flag_ipa_pure_const))
985 set_function_state (node, analyze_function (node, true));
986 }
987
988
989 /* Serialize the ipa info for lto. */
990
991 static void
992 pure_const_write_summary (void)
993 {
994 struct cgraph_node *node;
995 struct lto_simple_output_block *ob
996 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
997 unsigned int count = 0;
998 lto_symtab_encoder_iterator lsei;
999 lto_symtab_encoder_t encoder;
1000
1001 encoder = lto_get_out_decl_state ()->symtab_node_encoder;
1002
1003 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1004 lsei_next_function_in_partition (&lsei))
1005 {
1006 node = lsei_cgraph_node (lsei);
1007 if (node->definition && has_function_state (node))
1008 count++;
1009 }
1010
1011 streamer_write_uhwi_stream (ob->main_stream, count);
1012
1013 /* Process all of the functions. */
1014 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
1015 lsei_next_function_in_partition (&lsei))
1016 {
1017 node = lsei_cgraph_node (lsei);
1018 if (node->definition && has_function_state (node))
1019 {
1020 struct bitpack_d bp;
1021 funct_state fs;
1022 int node_ref;
1023 lto_symtab_encoder_t encoder;
1024
1025 fs = get_function_state (node);
1026
1027 encoder = ob->decl_state->symtab_node_encoder;
1028 node_ref = lto_symtab_encoder_encode (encoder, node);
1029 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1030
1031 /* Note that flags will need to be read in the opposite
1032 order as we are pushing the bitflags into FLAGS. */
1033 bp = bitpack_create (ob->main_stream);
1034 bp_pack_value (&bp, fs->pure_const_state, 2);
1035 bp_pack_value (&bp, fs->state_previously_known, 2);
1036 bp_pack_value (&bp, fs->looping_previously_known, 1);
1037 bp_pack_value (&bp, fs->looping, 1);
1038 bp_pack_value (&bp, fs->can_throw, 1);
1039 bp_pack_value (&bp, fs->can_free, 1);
1040 streamer_write_bitpack (&bp);
1041 }
1042 }
1043
1044 lto_destroy_simple_output_block (ob);
1045 }
1046
1047
1048 /* Deserialize the ipa info for lto. */
1049
1050 static void
1051 pure_const_read_summary (void)
1052 {
1053 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1054 struct lto_file_decl_data *file_data;
1055 unsigned int j = 0;
1056
1057 pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
1058 pass->register_hooks ();
1059
1060 while ((file_data = file_data_vec[j++]))
1061 {
1062 const char *data;
1063 size_t len;
1064 struct lto_input_block *ib
1065 = lto_create_simple_input_block (file_data,
1066 LTO_section_ipa_pure_const,
1067 &data, &len);
1068 if (ib)
1069 {
1070 unsigned int i;
1071 unsigned int count = streamer_read_uhwi (ib);
1072
1073 for (i = 0; i < count; i++)
1074 {
1075 unsigned int index;
1076 struct cgraph_node *node;
1077 struct bitpack_d bp;
1078 funct_state fs;
1079 lto_symtab_encoder_t encoder;
1080
1081 fs = XCNEW (struct funct_state_d);
1082 index = streamer_read_uhwi (ib);
1083 encoder = file_data->symtab_node_encoder;
1084 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
1085 index));
1086 set_function_state (node, fs);
1087
1088 /* Note that the flags must be read in the opposite
1089 order in which they were written (the bitflags were
1090 pushed into FLAGS). */
1091 bp = streamer_read_bitpack (ib);
1092 fs->pure_const_state
1093 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1094 fs->state_previously_known
1095 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
1096 fs->looping_previously_known = bp_unpack_value (&bp, 1);
1097 fs->looping = bp_unpack_value (&bp, 1);
1098 fs->can_throw = bp_unpack_value (&bp, 1);
1099 fs->can_free = bp_unpack_value (&bp, 1);
1100 if (dump_file)
1101 {
1102 int flags = flags_from_decl_or_type (node->decl);
1103 fprintf (dump_file, "Read info for %s/%i ",
1104 node->name (),
1105 node->order);
1106 if (flags & ECF_CONST)
1107 fprintf (dump_file, " const");
1108 if (flags & ECF_PURE)
1109 fprintf (dump_file, " pure");
1110 if (flags & ECF_NOTHROW)
1111 fprintf (dump_file, " nothrow");
1112 fprintf (dump_file, "\n pure const state: %s\n",
1113 pure_const_names[fs->pure_const_state]);
1114 fprintf (dump_file, " previously known state: %s\n",
1115 pure_const_names[fs->looping_previously_known]);
1116 if (fs->looping)
1117 fprintf (dump_file," function is locally looping\n");
1118 if (fs->looping_previously_known)
1119 fprintf (dump_file," function is previously known looping\n");
1120 if (fs->can_throw)
1121 fprintf (dump_file," function is locally throwing\n");
1122 if (fs->can_free)
1123 fprintf (dump_file," function can locally free\n");
1124 }
1125 }
1126
1127 lto_destroy_simple_input_block (file_data,
1128 LTO_section_ipa_pure_const,
1129 ib, data, len);
1130 }
1131 }
1132 }
1133
1134
1135 static bool
1136 ignore_edge (struct cgraph_edge *e)
1137 {
1138 return (!e->can_throw_external);
1139 }
1140
1141 /* Return true if NODE is self recursive function.
1142 Indirectly recursive functions appears as non-trivial strongly
1143 connected components, so we need to care about self recursion
1144 only. */
1145
1146 static bool
1147 self_recursive_p (struct cgraph_node *node)
1148 {
1149 struct cgraph_edge *e;
1150 for (e = node->callees; e; e = e->next_callee)
1151 if (e->callee->function_symbol () == node)
1152 return true;
1153 return false;
1154 }
1155
1156 /* Return true if N is cdtor that is not const or pure. In this case we may
1157 need to remove unreachable function if it is marked const/pure. */
1158
1159 static bool
1160 cdtor_p (cgraph_node *n, void *)
1161 {
1162 if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
1163 return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl);
1164 return false;
1165 }
1166
1167 /* Produce transitive closure over the callgraph and compute pure/const
1168 attributes. */
1169
1170 static bool
1171 propagate_pure_const (void)
1172 {
1173 struct cgraph_node *node;
1174 struct cgraph_node *w;
1175 struct cgraph_node **order =
1176 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1177 int order_pos;
1178 int i;
1179 struct ipa_dfs_info * w_info;
1180 bool remove_p = false;
1181
1182 order_pos = ipa_reduced_postorder (order, true, false, NULL);
1183 if (dump_file)
1184 {
1185 cgraph_node::dump_cgraph (dump_file);
1186 ipa_print_order (dump_file, "reduced", order, order_pos);
1187 }
1188
1189 /* Propagate the local information through the call graph to produce
1190 the global information. All the nodes within a cycle will have
1191 the same info so we collapse cycles first. Then we can do the
1192 propagation in one pass from the leaves to the roots. */
1193 for (i = 0; i < order_pos; i++ )
1194 {
1195 enum pure_const_state_e pure_const_state = IPA_CONST;
1196 bool looping = false;
1197 int count = 0;
1198 node = order[i];
1199
1200 if (node->alias)
1201 continue;
1202
1203 if (dump_file && (dump_flags & TDF_DETAILS))
1204 fprintf (dump_file, "Starting cycle\n");
1205
1206 /* Find the worst state for any node in the cycle. */
1207 w = node;
1208 while (w && pure_const_state != IPA_NEITHER)
1209 {
1210 struct cgraph_edge *e;
1211 struct cgraph_edge *ie;
1212 int i;
1213 struct ipa_ref *ref = NULL;
1214
1215 funct_state w_l = get_function_state (w);
1216 if (dump_file && (dump_flags & TDF_DETAILS))
1217 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
1218 w->name (),
1219 w->order,
1220 pure_const_names[w_l->pure_const_state],
1221 w_l->looping);
1222
1223 /* First merge in function body properties. */
1224 worse_state (&pure_const_state, &looping,
1225 w_l->pure_const_state, w_l->looping);
1226 if (pure_const_state == IPA_NEITHER)
1227 break;
1228
1229 /* For overwritable nodes we can not assume anything. */
1230 if (w->get_availability () == AVAIL_INTERPOSABLE)
1231 {
1232 worse_state (&pure_const_state, &looping,
1233 w_l->state_previously_known,
1234 w_l->looping_previously_known);
1235 if (dump_file && (dump_flags & TDF_DETAILS))
1236 {
1237 fprintf (dump_file,
1238 " Overwritable. state %s looping %i\n",
1239 pure_const_names[w_l->state_previously_known],
1240 w_l->looping_previously_known);
1241 }
1242 break;
1243 }
1244
1245 count++;
1246
1247 /* We consider recursive cycles as possibly infinite.
1248 This might be relaxed since infinite recursion leads to stack
1249 overflow. */
1250 if (count > 1)
1251 looping = true;
1252
1253 /* Now walk the edges and merge in callee properties. */
1254 for (e = w->callees; e; e = e->next_callee)
1255 {
1256 enum availability avail;
1257 struct cgraph_node *y = e->callee->
1258 function_or_virtual_thunk_symbol (&avail);
1259 enum pure_const_state_e edge_state = IPA_CONST;
1260 bool edge_looping = false;
1261
1262 if (dump_file && (dump_flags & TDF_DETAILS))
1263 {
1264 fprintf (dump_file,
1265 " Call to %s/%i",
1266 e->callee->name (),
1267 e->callee->order);
1268 }
1269 if (avail > AVAIL_INTERPOSABLE)
1270 {
1271 funct_state y_l = get_function_state (y);
1272 if (dump_file && (dump_flags & TDF_DETAILS))
1273 {
1274 fprintf (dump_file,
1275 " state:%s looping:%i\n",
1276 pure_const_names[y_l->pure_const_state],
1277 y_l->looping);
1278 }
1279 if (y_l->pure_const_state > IPA_PURE
1280 && e->cannot_lead_to_return_p ())
1281 {
1282 if (dump_file && (dump_flags & TDF_DETAILS))
1283 fprintf (dump_file,
1284 " Ignoring side effects"
1285 " -> pure, looping\n");
1286 edge_state = IPA_PURE;
1287 edge_looping = true;
1288 }
1289 else
1290 {
1291 edge_state = y_l->pure_const_state;
1292 edge_looping = y_l->looping;
1293 }
1294 }
1295 else if (special_builtin_state (&edge_state, &edge_looping,
1296 y->decl))
1297 ;
1298 else
1299 state_from_flags (&edge_state, &edge_looping,
1300 flags_from_decl_or_type (y->decl),
1301 e->cannot_lead_to_return_p ());
1302
1303 /* Merge the results with what we already know. */
1304 better_state (&edge_state, &edge_looping,
1305 w_l->state_previously_known,
1306 w_l->looping_previously_known);
1307 worse_state (&pure_const_state, &looping,
1308 edge_state, edge_looping);
1309 if (pure_const_state == IPA_NEITHER)
1310 break;
1311 }
1312 if (pure_const_state == IPA_NEITHER)
1313 break;
1314
1315 /* Now process the indirect call. */
1316 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
1317 {
1318 enum pure_const_state_e edge_state = IPA_CONST;
1319 bool edge_looping = false;
1320
1321 if (dump_file && (dump_flags & TDF_DETAILS))
1322 fprintf (dump_file, " Indirect call");
1323 state_from_flags (&edge_state, &edge_looping,
1324 ie->indirect_info->ecf_flags,
1325 ie->cannot_lead_to_return_p ());
1326 /* Merge the results with what we already know. */
1327 better_state (&edge_state, &edge_looping,
1328 w_l->state_previously_known,
1329 w_l->looping_previously_known);
1330 worse_state (&pure_const_state, &looping,
1331 edge_state, edge_looping);
1332 if (pure_const_state == IPA_NEITHER)
1333 break;
1334 }
1335 if (pure_const_state == IPA_NEITHER)
1336 break;
1337
1338 /* And finally all loads and stores. */
1339 for (i = 0; w->iterate_reference (i, ref); i++)
1340 {
1341 enum pure_const_state_e ref_state = IPA_CONST;
1342 bool ref_looping = false;
1343 switch (ref->use)
1344 {
1345 case IPA_REF_LOAD:
1346 /* readonly reads are safe. */
1347 if (TREE_READONLY (ref->referred->decl))
1348 break;
1349 if (dump_file && (dump_flags & TDF_DETAILS))
1350 fprintf (dump_file, " nonreadonly global var read\n");
1351 ref_state = IPA_PURE;
1352 break;
1353 case IPA_REF_STORE:
1354 if (ref->cannot_lead_to_return ())
1355 break;
1356 ref_state = IPA_NEITHER;
1357 if (dump_file && (dump_flags & TDF_DETAILS))
1358 fprintf (dump_file, " global var write\n");
1359 break;
1360 case IPA_REF_ADDR:
1361 case IPA_REF_CHKP:
1362 break;
1363 default:
1364 gcc_unreachable ();
1365 }
1366 better_state (&ref_state, &ref_looping,
1367 w_l->state_previously_known,
1368 w_l->looping_previously_known);
1369 worse_state (&pure_const_state, &looping,
1370 ref_state, ref_looping);
1371 if (pure_const_state == IPA_NEITHER)
1372 break;
1373 }
1374 w_info = (struct ipa_dfs_info *) w->aux;
1375 w = w_info->next_cycle;
1376 }
1377 if (dump_file && (dump_flags & TDF_DETAILS))
1378 fprintf (dump_file, "Result %s looping %i\n",
1379 pure_const_names [pure_const_state],
1380 looping);
1381
1382 /* Find the worst state of can_free for any node in the cycle. */
1383 bool can_free = false;
1384 w = node;
1385 while (w && !can_free)
1386 {
1387 struct cgraph_edge *e;
1388 funct_state w_l = get_function_state (w);
1389
1390 if (w_l->can_free
1391 || w->get_availability () == AVAIL_INTERPOSABLE
1392 || w->indirect_calls)
1393 can_free = true;
1394
1395 for (e = w->callees; e && !can_free; e = e->next_callee)
1396 {
1397 enum availability avail;
1398 struct cgraph_node *y = e->callee->
1399 function_or_virtual_thunk_symbol (&avail);
1400
1401 if (avail > AVAIL_INTERPOSABLE)
1402 can_free = get_function_state (y)->can_free;
1403 else
1404 can_free = true;
1405 }
1406 w_info = (struct ipa_dfs_info *) w->aux;
1407 w = w_info->next_cycle;
1408 }
1409
1410 /* Copy back the region's pure_const_state which is shared by
1411 all nodes in the region. */
1412 w = node;
1413 while (w)
1414 {
1415 funct_state w_l = get_function_state (w);
1416 enum pure_const_state_e this_state = pure_const_state;
1417 bool this_looping = looping;
1418
1419 w_l->can_free = can_free;
1420 w->nonfreeing_fn = !can_free;
1421 if (!can_free && dump_file)
1422 fprintf (dump_file, "Function found not to call free: %s\n",
1423 w->name ());
1424
1425 if (w_l->state_previously_known != IPA_NEITHER
1426 && this_state > w_l->state_previously_known)
1427 {
1428 this_state = w_l->state_previously_known;
1429 this_looping |= w_l->looping_previously_known;
1430 }
1431 if (!this_looping && self_recursive_p (w))
1432 this_looping = true;
1433 if (!w_l->looping_previously_known)
1434 this_looping = false;
1435
1436 /* All nodes within a cycle share the same info. */
1437 w_l->pure_const_state = this_state;
1438 w_l->looping = this_looping;
1439
1440 /* Inline clones share declaration with their offline copies;
1441 do not modify their declarations since the offline copy may
1442 be different. */
1443 if (!w->global.inlined_to)
1444 switch (this_state)
1445 {
1446 case IPA_CONST:
1447 if (!TREE_READONLY (w->decl))
1448 {
1449 warn_function_const (w->decl, !this_looping);
1450 if (dump_file)
1451 fprintf (dump_file, "Function found to be %sconst: %s\n",
1452 this_looping ? "looping " : "",
1453 w->name ());
1454 }
1455 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1456 NULL, true);
1457 w->set_const_flag (true, this_looping);
1458 break;
1459
1460 case IPA_PURE:
1461 if (!DECL_PURE_P (w->decl))
1462 {
1463 warn_function_pure (w->decl, !this_looping);
1464 if (dump_file)
1465 fprintf (dump_file, "Function found to be %spure: %s\n",
1466 this_looping ? "looping " : "",
1467 w->name ());
1468 }
1469 remove_p |= w->call_for_symbol_and_aliases (cdtor_p,
1470 NULL, true);
1471 w->set_pure_flag (true, this_looping);
1472 break;
1473
1474 default:
1475 break;
1476 }
1477 w_info = (struct ipa_dfs_info *) w->aux;
1478 w = w_info->next_cycle;
1479 }
1480 }
1481
1482 ipa_free_postorder_info ();
1483 free (order);
1484 return remove_p;
1485 }
1486
1487 /* Produce transitive closure over the callgraph and compute nothrow
1488 attributes. */
1489
1490 static void
1491 propagate_nothrow (void)
1492 {
1493 struct cgraph_node *node;
1494 struct cgraph_node *w;
1495 struct cgraph_node **order =
1496 XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
1497 int order_pos;
1498 int i;
1499 struct ipa_dfs_info * w_info;
1500
1501 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
1502 if (dump_file)
1503 {
1504 cgraph_node::dump_cgraph (dump_file);
1505 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
1506 }
1507
1508 /* Propagate the local information through the call graph to produce
1509 the global information. All the nodes within a cycle will have
1510 the same info so we collapse cycles first. Then we can do the
1511 propagation in one pass from the leaves to the roots. */
1512 for (i = 0; i < order_pos; i++ )
1513 {
1514 bool can_throw = false;
1515 node = order[i];
1516
1517 if (node->alias)
1518 continue;
1519
1520 /* Find the worst state for any node in the cycle. */
1521 w = node;
1522 while (w && !can_throw)
1523 {
1524 struct cgraph_edge *e, *ie;
1525 funct_state w_l = get_function_state (w);
1526
1527 if (w_l->can_throw
1528 || w->get_availability () == AVAIL_INTERPOSABLE)
1529 can_throw = true;
1530
1531 for (e = w->callees; e && !can_throw; e = e->next_callee)
1532 {
1533 enum availability avail;
1534 struct cgraph_node *y = e->callee->
1535 function_or_virtual_thunk_symbol (&avail);
1536
1537 if (avail > AVAIL_INTERPOSABLE)
1538 {
1539 funct_state y_l = get_function_state (y);
1540
1541 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1542 && e->can_throw_external)
1543 can_throw = true;
1544 }
1545 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1546 can_throw = true;
1547 }
1548 for (ie = w->indirect_calls; ie && !can_throw; ie = ie->next_callee)
1549 if (ie->can_throw_external)
1550 can_throw = true;
1551 w_info = (struct ipa_dfs_info *) w->aux;
1552 w = w_info->next_cycle;
1553 }
1554
1555 /* Copy back the region's pure_const_state which is shared by
1556 all nodes in the region. */
1557 w = node;
1558 while (w)
1559 {
1560 funct_state w_l = get_function_state (w);
1561 if (!can_throw && !TREE_NOTHROW (w->decl))
1562 {
1563 /* Inline clones share declaration with their offline copies;
1564 do not modify their declarations since the offline copy may
1565 be different. */
1566 if (!w->global.inlined_to)
1567 {
1568 w->set_nothrow_flag (true);
1569 if (dump_file)
1570 fprintf (dump_file, "Function found to be nothrow: %s\n",
1571 w->name ());
1572 }
1573 }
1574 else if (can_throw && !TREE_NOTHROW (w->decl))
1575 w_l->can_throw = true;
1576 w_info = (struct ipa_dfs_info *) w->aux;
1577 w = w_info->next_cycle;
1578 }
1579 }
1580
1581 ipa_free_postorder_info ();
1582 free (order);
1583 }
1584
1585
1586 /* Produce the global information by preforming a transitive closure
1587 on the local information that was produced by generate_summary. */
1588
1589 unsigned int
1590 pass_ipa_pure_const::
1591 execute (function *)
1592 {
1593 struct cgraph_node *node;
1594 bool remove_p;
1595
1596 symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
1597 symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder);
1598 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
1599
1600 /* Nothrow makes more function to not lead to return and improve
1601 later analysis. */
1602 propagate_nothrow ();
1603 remove_p = propagate_pure_const ();
1604
1605 /* Cleanup. */
1606 FOR_EACH_FUNCTION (node)
1607 if (has_function_state (node))
1608 free (get_function_state (node));
1609 funct_state_vec.release ();
1610 return remove_p ? TODO_remove_functions : 0;
1611 }
1612
1613 static bool
1614 gate_pure_const (void)
1615 {
1616 return flag_ipa_pure_const || in_lto_p;
1617 }
1618
1619 pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
1620 : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
1621 pure_const_generate_summary, /* generate_summary */
1622 pure_const_write_summary, /* write_summary */
1623 pure_const_read_summary, /* read_summary */
1624 NULL, /* write_optimization_summary */
1625 NULL, /* read_optimization_summary */
1626 NULL, /* stmt_fixup */
1627 0, /* function_transform_todo_flags_start */
1628 NULL, /* function_transform */
1629 NULL), /* variable_transform */
1630 init_p(false),
1631 function_insertion_hook_holder(NULL),
1632 node_duplication_hook_holder(NULL),
1633 node_removal_hook_holder(NULL)
1634 {
1635 }
1636
1637 ipa_opt_pass_d *
1638 make_pass_ipa_pure_const (gcc::context *ctxt)
1639 {
1640 return new pass_ipa_pure_const (ctxt);
1641 }
1642
1643 /* Return true if function should be skipped for local pure const analysis. */
1644
1645 static bool
1646 skip_function_for_local_pure_const (struct cgraph_node *node)
1647 {
1648 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1649 we must not promote functions that are called by already processed functions. */
1650
1651 if (function_called_by_processed_nodes_p ())
1652 {
1653 if (dump_file)
1654 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1655 return true;
1656 }
1657 if (node->get_availability () <= AVAIL_INTERPOSABLE)
1658 {
1659 if (dump_file)
1660 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
1661 return true;
1662 }
1663 return false;
1664 }
1665
1666 /* Simple local pass for pure const discovery reusing the analysis from
1667 ipa_pure_const. This pass is effective when executed together with
1668 other optimization passes in early optimization pass queue. */
1669
1670 namespace {
1671
1672 const pass_data pass_data_local_pure_const =
1673 {
1674 GIMPLE_PASS, /* type */
1675 "local-pure-const", /* name */
1676 OPTGROUP_NONE, /* optinfo_flags */
1677 TV_IPA_PURE_CONST, /* tv_id */
1678 0, /* properties_required */
1679 0, /* properties_provided */
1680 0, /* properties_destroyed */
1681 0, /* todo_flags_start */
1682 0, /* todo_flags_finish */
1683 };
1684
1685 class pass_local_pure_const : public gimple_opt_pass
1686 {
1687 public:
1688 pass_local_pure_const (gcc::context *ctxt)
1689 : gimple_opt_pass (pass_data_local_pure_const, ctxt)
1690 {}
1691
1692 /* opt_pass methods: */
1693 opt_pass * clone () { return new pass_local_pure_const (m_ctxt); }
1694 virtual bool gate (function *) { return gate_pure_const (); }
1695 virtual unsigned int execute (function *);
1696
1697 }; // class pass_local_pure_const
1698
1699 unsigned int
1700 pass_local_pure_const::execute (function *fun)
1701 {
1702 bool changed = false;
1703 funct_state l;
1704 bool skip;
1705 struct cgraph_node *node;
1706
1707 node = cgraph_node::get (current_function_decl);
1708 skip = skip_function_for_local_pure_const (node);
1709 if (!warn_suggest_attribute_const
1710 && !warn_suggest_attribute_pure
1711 && skip)
1712 return 0;
1713
1714 l = analyze_function (node, false);
1715
1716 /* Do NORETURN discovery. */
1717 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1718 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1719 {
1720 warn_function_noreturn (fun->decl);
1721 if (dump_file)
1722 fprintf (dump_file, "Function found to be noreturn: %s\n",
1723 current_function_name ());
1724
1725 /* Update declaration and reduce profile to executed once. */
1726 TREE_THIS_VOLATILE (current_function_decl) = 1;
1727 if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1728 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1729
1730 changed = true;
1731 }
1732
1733 switch (l->pure_const_state)
1734 {
1735 case IPA_CONST:
1736 if (!TREE_READONLY (current_function_decl))
1737 {
1738 warn_function_const (current_function_decl, !l->looping);
1739 if (!skip)
1740 {
1741 node->set_const_flag (true, l->looping);
1742 changed = true;
1743 }
1744 if (dump_file)
1745 fprintf (dump_file, "Function found to be %sconst: %s\n",
1746 l->looping ? "looping " : "",
1747 current_function_name ());
1748 }
1749 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1750 && !l->looping)
1751 {
1752 if (!skip)
1753 {
1754 node->set_const_flag (true, false);
1755 changed = true;
1756 }
1757 if (dump_file)
1758 fprintf (dump_file, "Function found to be non-looping: %s\n",
1759 current_function_name ());
1760 }
1761 break;
1762
1763 case IPA_PURE:
1764 if (!DECL_PURE_P (current_function_decl))
1765 {
1766 if (!skip)
1767 {
1768 node->set_pure_flag (true, l->looping);
1769 changed = true;
1770 }
1771 warn_function_pure (current_function_decl, !l->looping);
1772 if (dump_file)
1773 fprintf (dump_file, "Function found to be %spure: %s\n",
1774 l->looping ? "looping " : "",
1775 current_function_name ());
1776 }
1777 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1778 && !l->looping)
1779 {
1780 if (!skip)
1781 {
1782 node->set_pure_flag (true, false);
1783 changed = true;
1784 }
1785 if (dump_file)
1786 fprintf (dump_file, "Function found to be non-looping: %s\n",
1787 current_function_name ());
1788 }
1789 break;
1790
1791 default:
1792 break;
1793 }
1794 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1795 {
1796 node->set_nothrow_flag (true);
1797 changed = true;
1798 if (dump_file)
1799 fprintf (dump_file, "Function found to be nothrow: %s\n",
1800 current_function_name ());
1801 }
1802 free (l);
1803 if (changed)
1804 return execute_fixup_cfg ();
1805 else
1806 return 0;
1807 }
1808
1809 } // anon namespace
1810
1811 gimple_opt_pass *
1812 make_pass_local_pure_const (gcc::context *ctxt)
1813 {
1814 return new pass_local_pure_const (ctxt);
1815 }
1816
1817 /* Emit noreturn warnings. */
1818
1819 namespace {
1820
1821 const pass_data pass_data_warn_function_noreturn =
1822 {
1823 GIMPLE_PASS, /* type */
1824 "*warn_function_noreturn", /* name */
1825 OPTGROUP_NONE, /* optinfo_flags */
1826 TV_NONE, /* tv_id */
1827 PROP_cfg, /* properties_required */
1828 0, /* properties_provided */
1829 0, /* properties_destroyed */
1830 0, /* todo_flags_start */
1831 0, /* todo_flags_finish */
1832 };
1833
1834 class pass_warn_function_noreturn : public gimple_opt_pass
1835 {
1836 public:
1837 pass_warn_function_noreturn (gcc::context *ctxt)
1838 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1839 {}
1840
1841 /* opt_pass methods: */
1842 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
1843 virtual unsigned int execute (function *fun)
1844 {
1845 if (!TREE_THIS_VOLATILE (current_function_decl)
1846 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
1847 warn_function_noreturn (current_function_decl);
1848 return 0;
1849 }
1850
1851 }; // class pass_warn_function_noreturn
1852
1853 } // anon namespace
1854
1855 gimple_opt_pass *
1856 make_pass_warn_function_noreturn (gcc::context *ctxt)
1857 {
1858 return new pass_warn_function_noreturn (ctxt);
1859 }
1860
1861 /* Simple local pass for pure const discovery reusing the analysis from
1862 ipa_pure_const. This pass is effective when executed together with
1863 other optimization passes in early optimization pass queue. */
1864
1865 namespace {
1866
1867 const pass_data pass_data_nothrow =
1868 {
1869 GIMPLE_PASS, /* type */
1870 "nothrow", /* name */
1871 OPTGROUP_NONE, /* optinfo_flags */
1872 TV_IPA_PURE_CONST, /* tv_id */
1873 0, /* properties_required */
1874 0, /* properties_provided */
1875 0, /* properties_destroyed */
1876 0, /* todo_flags_start */
1877 0, /* todo_flags_finish */
1878 };
1879
1880 class pass_nothrow : public gimple_opt_pass
1881 {
1882 public:
1883 pass_nothrow (gcc::context *ctxt)
1884 : gimple_opt_pass (pass_data_nothrow, ctxt)
1885 {}
1886
1887 /* opt_pass methods: */
1888 opt_pass * clone () { return new pass_nothrow (m_ctxt); }
1889 virtual bool gate (function *) { return optimize; }
1890 virtual unsigned int execute (function *);
1891
1892 }; // class pass_nothrow
1893
1894 unsigned int
1895 pass_nothrow::execute (function *)
1896 {
1897 struct cgraph_node *node;
1898 basic_block this_block;
1899
1900 if (TREE_NOTHROW (current_function_decl))
1901 return 0;
1902
1903 node = cgraph_node::get (current_function_decl);
1904
1905 /* We run during lowering, we can not really use availability yet. */
1906 if (cgraph_node::get (current_function_decl)->get_availability ()
1907 <= AVAIL_INTERPOSABLE)
1908 {
1909 if (dump_file)
1910 fprintf (dump_file, "Function is interposable;"
1911 " not analyzing.\n");
1912 return true;
1913 }
1914
1915 FOR_EACH_BB_FN (this_block, cfun)
1916 {
1917 for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
1918 !gsi_end_p (gsi);
1919 gsi_next (&gsi))
1920 if (stmt_can_throw_external (gsi_stmt (gsi)))
1921 {
1922 if (is_gimple_call (gsi_stmt (gsi)))
1923 {
1924 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
1925 if (callee_t && recursive_call_p (current_function_decl,
1926 callee_t))
1927 continue;
1928 }
1929
1930 if (dump_file)
1931 {
1932 fprintf (dump_file, "Statement can throw: ");
1933 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1934 }
1935 return 0;
1936 }
1937 }
1938
1939 node->set_nothrow_flag (true);
1940 if (dump_file)
1941 fprintf (dump_file, "Function found to be nothrow: %s\n",
1942 current_function_name ());
1943 return 0;
1944 }
1945
1946 } // anon namespace
1947
1948 gimple_opt_pass *
1949 make_pass_nothrow (gcc::context *ctxt)
1950 {
1951 return new pass_nothrow (ctxt);
1952 }