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