2010-05-28 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / ipa-pure-const.c
blob02a1e0386ab20acb555ec10a0bb6a525a775c0b4
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file marks functions as being either const (TREE_READONLY) or
23 pure (DECL_PURE_P). It can also set a variant of these that
24 are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
26 This must be run after inlining decisions have been made since
27 otherwise, the local sets will not contain information that is
28 consistent with post inlined state. The global sets are not prone
29 to this problem since they are by definition transitive. */
31 /* The code in this module is called by the ipa pass manager. It
32 should be one of the later passes since it's information is used by
33 the rest of the compilation. */
35 #include "config.h"
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "tree.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "langhooks.h"
44 #include "pointer-set.h"
45 #include "ggc.h"
46 #include "ipa-utils.h"
47 #include "gimple.h"
48 #include "cgraph.h"
49 #include "output.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "toplev.h"
53 #include "diagnostic.h"
54 #include "gimple-pretty-print.h"
55 #include "langhooks.h"
56 #include "target.h"
57 #include "lto-streamer.h"
58 #include "cfgloop.h"
59 #include "tree-scalar-evolution.h"
60 #include "intl.h"
61 #include "opts.h"
63 static struct pointer_set_t *visited_nodes;
65 /* Lattice values for const and pure functions. Everything starts out
66 being const, then may drop to pure and then neither depending on
67 what is found. */
68 enum pure_const_state_e
70 IPA_CONST,
71 IPA_PURE,
72 IPA_NEITHER
75 /* Holder for the const_state. There is one of these per function
76 decl. */
77 struct funct_state_d
79 /* See above. */
80 enum pure_const_state_e pure_const_state;
81 /* What user set here; we can be always sure about this. */
82 enum pure_const_state_e state_previously_known;
83 bool looping_previously_known;
85 /* True if the function could possibly infinite loop. There are a
86 lot of ways that this could be determined. We are pretty
87 conservative here. While it is possible to cse pure and const
88 calls, it is not legal to have dce get rid of the call if there
89 is a possibility that the call could infinite loop since this is
90 a behavioral change. */
91 bool looping;
93 bool can_throw;
96 typedef struct funct_state_d * funct_state;
98 /* The storage of the funct_state is abstracted because there is the
99 possibility that it may be desirable to move this to the cgraph
100 local info. */
102 /* Array, indexed by cgraph node uid, of function states. */
104 DEF_VEC_P (funct_state);
105 DEF_VEC_ALLOC_P (funct_state, heap);
106 static VEC (funct_state, heap) *funct_state_vec;
108 /* Holders of ipa cgraph hooks: */
109 static struct cgraph_node_hook_list *function_insertion_hook_holder;
110 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
111 static struct cgraph_node_hook_list *node_removal_hook_holder;
113 /* Try to guess if function body will always be visible to compiler
114 when compiling the call and whether compiler will be able
115 to propagate the information by itself. */
117 static bool
118 function_always_visible_to_compiler_p (tree decl)
120 return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
123 /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE
124 is true if the function is known to be finite. The diagnostic is
125 controlled by OPTION. WARNED_ABOUT is a pointer_set unique for
126 OPTION, this function may initialize it and it is always returned
127 by the function. */
129 static struct pointer_set_t *
130 suggest_attribute (int option, tree decl, bool known_finite,
131 struct pointer_set_t *warned_about,
132 const char * attrib_name)
134 if (!option_enabled (option))
135 return warned_about;
136 if (TREE_THIS_VOLATILE (decl)
137 || (known_finite && function_always_visible_to_compiler_p (decl)))
138 return warned_about;
140 if (!warned_about)
141 warned_about = pointer_set_create ();
142 if (pointer_set_contains (warned_about, decl))
143 return warned_about;
144 pointer_set_insert (warned_about, decl);
145 warning_at (DECL_SOURCE_LOCATION (decl),
146 option,
147 known_finite
148 ? _("function might be candidate for attribute %<%s%>")
149 : _("function might be candidate for attribute %<%s%>"
150 " if it is known to return normally"), attrib_name);
151 return warned_about;
154 /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE
155 is true if the function is known to be finite. */
157 static void
158 warn_function_pure (tree decl, bool known_finite)
160 static struct pointer_set_t *warned_about;
162 warned_about
163 = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
164 known_finite, warned_about, "pure");
167 /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE
168 is true if the function is known to be finite. */
170 static void
171 warn_function_const (tree decl, bool known_finite)
173 static struct pointer_set_t *warned_about;
174 warned_about
175 = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
176 known_finite, warned_about, "const");
178 /* Init the function state. */
180 static void
181 finish_state (void)
183 free (funct_state_vec);
187 /* Return the function state from NODE. */
189 static inline funct_state
190 get_function_state (struct cgraph_node *node)
192 if (!funct_state_vec
193 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
194 return NULL;
195 return VEC_index (funct_state, funct_state_vec, node->uid);
198 /* Set the function state S for NODE. */
200 static inline void
201 set_function_state (struct cgraph_node *node, funct_state s)
203 if (!funct_state_vec
204 || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
205 VEC_safe_grow_cleared (funct_state, heap, funct_state_vec, node->uid + 1);
206 VEC_replace (funct_state, funct_state_vec, node->uid, s);
209 /* Check to see if the use (or definition when CHECKING_WRITE is true)
210 variable T is legal in a function that is either pure or const. */
212 static inline void
213 check_decl (funct_state local,
214 tree t, bool checking_write)
216 /* Do not want to do anything with volatile except mark any
217 function that uses one to be not const or pure. */
218 if (TREE_THIS_VOLATILE (t))
220 local->pure_const_state = IPA_NEITHER;
221 if (dump_file)
222 fprintf (dump_file, " Volatile operand is not const/pure");
223 return;
226 /* Do not care about a local automatic that is not static. */
227 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
228 return;
230 /* If the variable has the "used" attribute, treat it as if it had a
231 been touched by the devil. */
232 if (DECL_PRESERVE_P (t))
234 local->pure_const_state = IPA_NEITHER;
235 if (dump_file)
236 fprintf (dump_file, " Used static/global variable is not const/pure\n");
237 return;
240 /* Since we have dealt with the locals and params cases above, if we
241 are CHECKING_WRITE, this cannot be a pure or constant
242 function. */
243 if (checking_write)
245 local->pure_const_state = IPA_NEITHER;
246 if (dump_file)
247 fprintf (dump_file, " static/global memory write is not const/pure\n");
248 return;
251 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
253 /* Readonly reads are safe. */
254 if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
255 return; /* Read of a constant, do not change the function state. */
256 else
258 if (dump_file)
259 fprintf (dump_file, " global memory read is not const\n");
260 /* Just a regular read. */
261 if (local->pure_const_state == IPA_CONST)
262 local->pure_const_state = IPA_PURE;
265 else
267 /* Compilation level statics can be read if they are readonly
268 variables. */
269 if (TREE_READONLY (t))
270 return;
272 if (dump_file)
273 fprintf (dump_file, " static memory read is not const\n");
274 /* Just a regular read. */
275 if (local->pure_const_state == IPA_CONST)
276 local->pure_const_state = IPA_PURE;
281 /* Check to see if the use (or definition when CHECKING_WRITE is true)
282 variable T is legal in a function that is either pure or const. */
284 static inline void
285 check_op (funct_state local, tree t, bool checking_write)
287 t = get_base_address (t);
288 if (t && TREE_THIS_VOLATILE (t))
290 local->pure_const_state = IPA_NEITHER;
291 if (dump_file)
292 fprintf (dump_file, " Volatile indirect ref is not const/pure\n");
293 return;
295 else if (t
296 && INDIRECT_REF_P (t)
297 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
298 && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
300 if (dump_file)
301 fprintf (dump_file, " Indirect ref to local memory is OK\n");
302 return;
304 else if (checking_write)
306 local->pure_const_state = IPA_NEITHER;
307 if (dump_file)
308 fprintf (dump_file, " Indirect ref write is not const/pure\n");
309 return;
311 else
313 if (dump_file)
314 fprintf (dump_file, " Indirect ref read is not const\n");
315 if (local->pure_const_state == IPA_CONST)
316 local->pure_const_state = IPA_PURE;
320 /* Check the parameters of a function call to CALL_EXPR to see if
321 there are any references in the parameters that are not allowed for
322 pure or const functions. Also check to see if this is either an
323 indirect call, a call outside the compilation unit, or has special
324 attributes that may also effect the purity. The CALL_EXPR node for
325 the entire call expression. */
327 static void
328 check_call (funct_state local, gimple call, bool ipa)
330 int flags = gimple_call_flags (call);
331 tree callee_t = gimple_call_fndecl (call);
332 bool possibly_throws = stmt_could_throw_p (call);
333 bool possibly_throws_externally = (possibly_throws
334 && stmt_can_throw_external (call));
336 if (possibly_throws)
338 unsigned int i;
339 for (i = 0; i < gimple_num_ops (call); i++)
340 if (gimple_op (call, i)
341 && tree_could_throw_p (gimple_op (call, i)))
343 if (possibly_throws && cfun->can_throw_non_call_exceptions)
345 if (dump_file)
346 fprintf (dump_file, " operand can throw; looping\n");
347 local->looping = true;
349 if (possibly_throws_externally)
351 if (dump_file)
352 fprintf (dump_file, " operand can throw externally\n");
353 local->can_throw = true;
358 /* The const and pure flags are set by a variety of places in the
359 compiler (including here). If someone has already set the flags
360 for the callee, (such as for some of the builtins) we will use
361 them, otherwise we will compute our own information.
363 Const and pure functions have less clobber effects than other
364 functions so we process these first. Otherwise if it is a call
365 outside the compilation unit or an indirect call we punt. This
366 leaves local calls which will be processed by following the call
367 graph. */
368 if (callee_t)
370 /* When bad things happen to bad functions, they cannot be const
371 or pure. */
372 if (setjmp_call_p (callee_t))
374 if (dump_file)
375 fprintf (dump_file, " setjmp is not const/pure\n");
376 local->looping = true;
377 local->pure_const_state = IPA_NEITHER;
380 if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
381 switch (DECL_FUNCTION_CODE (callee_t))
383 case BUILT_IN_LONGJMP:
384 case BUILT_IN_NONLOCAL_GOTO:
385 if (dump_file)
386 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
387 local->pure_const_state = IPA_NEITHER;
388 local->looping = true;
389 break;
390 default:
391 break;
395 /* When not in IPA mode, we can still handle self recursion. */
396 if (!ipa && callee_t == current_function_decl)
398 if (dump_file)
399 fprintf (dump_file, " Recursive call can loop.\n");
400 local->looping = true;
402 /* Either calle is unknown or we are doing local analysis.
403 Look to see if there are any bits available for the callee (such as by
404 declaration or because it is builtin) and process solely on the basis of
405 those bits. */
406 else if (!ipa || !callee_t)
408 if (possibly_throws && cfun->can_throw_non_call_exceptions)
410 if (dump_file)
411 fprintf (dump_file, " can throw; looping\n");
412 local->looping = true;
414 if (possibly_throws_externally)
416 if (dump_file)
418 fprintf (dump_file, " can throw externally to lp %i\n",
419 lookup_stmt_eh_lp (call));
420 if (callee_t)
421 fprintf (dump_file, " callee:%s\n",
422 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
424 local->can_throw = true;
426 if (flags & ECF_CONST)
428 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
430 if (dump_file)
431 fprintf (dump_file, " calls looping pure.\n");
432 local->looping = true;
435 else if (flags & ECF_PURE)
437 if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
439 if (dump_file)
440 fprintf (dump_file, " calls looping const.\n");
441 local->looping = true;
443 if (dump_file)
444 fprintf (dump_file, " pure function call in not const\n");
445 if (local->pure_const_state == IPA_CONST)
446 local->pure_const_state = IPA_PURE;
448 else
450 if (dump_file)
451 fprintf (dump_file, " uknown function call is not const/pure\n");
452 local->pure_const_state = IPA_NEITHER;
453 local->looping = true;
456 /* Direct functions calls are handled by IPA propagation. */
459 /* Wrapper around check_decl for loads. */
461 static bool
462 check_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
464 if (DECL_P (op))
465 check_decl ((funct_state)data, op, false);
466 else
467 check_op ((funct_state)data, op, false);
468 return false;
471 /* Wrapper around check_decl for stores. */
473 static bool
474 check_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
476 if (DECL_P (op))
477 check_decl ((funct_state)data, op, true);
478 else
479 check_op ((funct_state)data, op, true);
480 return false;
483 /* Look into pointer pointed to by GSIP and figure out what interesting side
484 effects it has. */
485 static void
486 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
488 gimple stmt = gsi_stmt (*gsip);
489 unsigned int i = 0;
491 if (is_gimple_debug (stmt))
492 return;
494 if (dump_file)
496 fprintf (dump_file, " scanning: ");
497 print_gimple_stmt (dump_file, stmt, 0, 0);
500 /* Look for loads and stores. */
501 walk_stmt_load_store_ops (stmt, local, check_load, check_store);
503 if (gimple_code (stmt) != GIMPLE_CALL
504 && stmt_could_throw_p (stmt))
506 if (cfun->can_throw_non_call_exceptions)
508 if (dump_file)
509 fprintf (dump_file, " can throw; looping");
510 local->looping = true;
512 if (stmt_can_throw_external (stmt))
514 if (dump_file)
515 fprintf (dump_file, " can throw externally");
516 local->can_throw = true;
519 switch (gimple_code (stmt))
521 case GIMPLE_CALL:
522 check_call (local, stmt, ipa);
523 break;
524 case GIMPLE_LABEL:
525 if (DECL_NONLOCAL (gimple_label_label (stmt)))
526 /* Target of long jump. */
528 if (dump_file)
529 fprintf (dump_file, " nonlocal label is not const/pure");
530 local->pure_const_state = IPA_NEITHER;
532 break;
533 case GIMPLE_ASM:
534 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
536 tree op = gimple_asm_clobber_op (stmt, i);
537 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
539 if (dump_file)
540 fprintf (dump_file, " memory asm clobber is not const/pure");
541 /* Abandon all hope, ye who enter here. */
542 local->pure_const_state = IPA_NEITHER;
545 if (gimple_asm_volatile_p (stmt))
547 if (dump_file)
548 fprintf (dump_file, " volatile is not const/pure");
549 /* Abandon all hope, ye who enter here. */
550 local->pure_const_state = IPA_NEITHER;
551 local->looping = true;
553 return;
554 default:
555 break;
560 /* This is the main routine for finding the reference patterns for
561 global variables within a function FN. */
563 static funct_state
564 analyze_function (struct cgraph_node *fn, bool ipa)
566 tree decl = fn->decl;
567 tree old_decl = current_function_decl;
568 funct_state l;
569 basic_block this_block;
571 l = XCNEW (struct funct_state_d);
572 l->pure_const_state = IPA_CONST;
573 l->state_previously_known = IPA_NEITHER;
574 l->looping_previously_known = true;
575 l->looping = false;
576 l->can_throw = false;
578 if (dump_file)
580 fprintf (dump_file, "\n\n local analysis of %s\n ",
581 cgraph_node_name (fn));
584 push_cfun (DECL_STRUCT_FUNCTION (decl));
585 current_function_decl = decl;
587 FOR_EACH_BB (this_block)
589 gimple_stmt_iterator gsi;
590 struct walk_stmt_info wi;
592 memset (&wi, 0, sizeof(wi));
593 for (gsi = gsi_start_bb (this_block);
594 !gsi_end_p (gsi);
595 gsi_next (&gsi))
597 check_stmt (&gsi, l, ipa);
598 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
599 goto end;
603 end:
604 if (l->pure_const_state != IPA_NEITHER)
606 /* Const functions cannot have back edges (an
607 indication of possible infinite loop side
608 effect. */
609 if (mark_dfs_back_edges ())
611 /* Preheaders are needed for SCEV to work.
612 Simple lateches and recorded exits improve chances that loop will
613 proved to be finite in testcases such as in loop-15.c and loop-24.c */
614 loop_optimizer_init (LOOPS_NORMAL
615 | LOOPS_HAVE_RECORDED_EXITS);
616 if (dump_file && (dump_flags & TDF_DETAILS))
617 flow_loops_dump (dump_file, NULL, 0);
618 if (mark_irreducible_loops ())
620 if (dump_file)
621 fprintf (dump_file, " has irreducible loops\n");
622 l->looping = true;
624 else
626 loop_iterator li;
627 struct loop *loop;
628 scev_initialize ();
629 FOR_EACH_LOOP (li, loop, 0)
630 if (!finite_loop_p (loop))
632 if (dump_file)
633 fprintf (dump_file, " can not prove finiteness of loop %i\n", loop->num);
634 l->looping =true;
635 break;
637 scev_finalize ();
639 loop_optimizer_finalize ();
643 if (TREE_READONLY (decl))
645 l->pure_const_state = IPA_CONST;
646 l->state_previously_known = IPA_CONST;
647 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
648 l->looping = false, l->looping_previously_known = false;
650 if (DECL_PURE_P (decl))
652 if (l->pure_const_state != IPA_CONST)
653 l->pure_const_state = IPA_PURE;
654 l->state_previously_known = IPA_PURE;
655 if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
656 l->looping = false, l->looping_previously_known = false;
658 if (TREE_NOTHROW (decl))
659 l->can_throw = false;
661 pop_cfun ();
662 current_function_decl = old_decl;
663 if (dump_file)
665 if (l->looping)
666 fprintf (dump_file, "Function is locally looping.\n");
667 if (l->can_throw)
668 fprintf (dump_file, "Function is locally throwing.\n");
669 if (l->pure_const_state == IPA_CONST)
670 fprintf (dump_file, "Function is locally const.\n");
671 if (l->pure_const_state == IPA_PURE)
672 fprintf (dump_file, "Function is locally pure.\n");
674 return l;
677 /* Called when new function is inserted to callgraph late. */
678 static void
679 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
681 if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
682 return;
683 /* There are some shared nodes, in particular the initializers on
684 static declarations. We do not need to scan them more than once
685 since all we would be interested in are the addressof
686 operations. */
687 visited_nodes = pointer_set_create ();
688 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
689 set_function_state (node, analyze_function (node, true));
690 pointer_set_destroy (visited_nodes);
691 visited_nodes = NULL;
694 /* Called when new clone is inserted to callgraph late. */
696 static void
697 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
698 void *data ATTRIBUTE_UNUSED)
700 if (get_function_state (src))
702 funct_state l = XNEW (struct funct_state_d);
703 gcc_assert (!get_function_state (dst));
704 memcpy (l, get_function_state (src), sizeof (*l));
705 set_function_state (dst, l);
709 /* Called when new clone is inserted to callgraph late. */
711 static void
712 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
714 if (get_function_state (node))
716 free (get_function_state (node));
717 set_function_state (node, NULL);
722 static void
723 register_hooks (void)
725 static bool init_p = false;
727 if (init_p)
728 return;
730 init_p = true;
732 node_removal_hook_holder =
733 cgraph_add_node_removal_hook (&remove_node_data, NULL);
734 node_duplication_hook_holder =
735 cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
736 function_insertion_hook_holder =
737 cgraph_add_function_insertion_hook (&add_new_function, NULL);
741 /* Analyze each function in the cgraph to see if it is locally PURE or
742 CONST. */
744 static void
745 generate_summary (void)
747 struct cgraph_node *node;
749 register_hooks ();
751 /* There are some shared nodes, in particular the initializers on
752 static declarations. We do not need to scan them more than once
753 since all we would be interested in are the addressof
754 operations. */
755 visited_nodes = pointer_set_create ();
757 /* Process all of the functions.
759 We process AVAIL_OVERWRITABLE functions. We can not use the results
760 by default, but the info can be used at LTO with -fwhole-program or
761 when function got clonned and the clone is AVAILABLE. */
763 for (node = cgraph_nodes; node; node = node->next)
764 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
765 set_function_state (node, analyze_function (node, true));
767 pointer_set_destroy (visited_nodes);
768 visited_nodes = NULL;
772 /* Serialize the ipa info for lto. */
774 static void
775 pure_const_write_summary (cgraph_node_set set,
776 varpool_node_set vset ATTRIBUTE_UNUSED)
778 struct cgraph_node *node;
779 struct lto_simple_output_block *ob
780 = lto_create_simple_output_block (LTO_section_ipa_pure_const);
781 unsigned int count = 0;
782 cgraph_node_set_iterator csi;
784 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
786 node = csi_node (csi);
787 if (node->analyzed && get_function_state (node) != NULL)
788 count++;
791 lto_output_uleb128_stream (ob->main_stream, count);
793 /* Process all of the functions. */
794 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
796 node = csi_node (csi);
797 if (node->analyzed && get_function_state (node) != NULL)
799 struct bitpack_d *bp;
800 funct_state fs;
801 int node_ref;
802 lto_cgraph_encoder_t encoder;
804 fs = get_function_state (node);
806 encoder = ob->decl_state->cgraph_node_encoder;
807 node_ref = lto_cgraph_encoder_encode (encoder, node);
808 lto_output_uleb128_stream (ob->main_stream, node_ref);
810 /* Note that flags will need to be read in the opposite
811 order as we are pushing the bitflags into FLAGS. */
812 bp = bitpack_create ();
813 bp_pack_value (bp, fs->pure_const_state, 2);
814 bp_pack_value (bp, fs->state_previously_known, 2);
815 bp_pack_value (bp, fs->looping_previously_known, 1);
816 bp_pack_value (bp, fs->looping, 1);
817 bp_pack_value (bp, fs->can_throw, 1);
818 lto_output_bitpack (ob->main_stream, bp);
819 bitpack_delete (bp);
823 lto_destroy_simple_output_block (ob);
827 /* Deserialize the ipa info for lto. */
829 static void
830 pure_const_read_summary (void)
832 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
833 struct lto_file_decl_data *file_data;
834 unsigned int j = 0;
836 register_hooks ();
837 while ((file_data = file_data_vec[j++]))
839 const char *data;
840 size_t len;
841 struct lto_input_block *ib
842 = lto_create_simple_input_block (file_data,
843 LTO_section_ipa_pure_const,
844 &data, &len);
845 if (ib)
847 unsigned int i;
848 unsigned int count = lto_input_uleb128 (ib);
850 for (i = 0; i < count; i++)
852 unsigned int index;
853 struct cgraph_node *node;
854 struct bitpack_d *bp;
855 funct_state fs;
856 lto_cgraph_encoder_t encoder;
858 fs = XCNEW (struct funct_state_d);
859 index = lto_input_uleb128 (ib);
860 encoder = file_data->cgraph_node_encoder;
861 node = lto_cgraph_encoder_deref (encoder, index);
862 set_function_state (node, fs);
864 /* Note that the flags must be read in the opposite
865 order in which they were written (the bitflags were
866 pushed into FLAGS). */
867 bp = lto_input_bitpack (ib);
868 fs->pure_const_state
869 = (enum pure_const_state_e) bp_unpack_value (bp, 2);
870 fs->state_previously_known
871 = (enum pure_const_state_e) bp_unpack_value (bp, 2);
872 fs->looping_previously_known = bp_unpack_value (bp, 1);
873 fs->looping = bp_unpack_value (bp, 1);
874 fs->can_throw = bp_unpack_value (bp, 1);
875 bitpack_delete (bp);
878 lto_destroy_simple_input_block (file_data,
879 LTO_section_ipa_pure_const,
880 ib, data, len);
886 static bool
887 ignore_edge (struct cgraph_edge *e)
889 return (!e->can_throw_external);
892 /* Return true if NODE is self recursive function. */
894 static bool
895 self_recursive_p (struct cgraph_node *node)
897 struct cgraph_edge *e;
898 for (e = node->callees; e; e = e->next_callee)
899 if (e->callee == node)
900 return true;
901 return false;
904 /* Produce the global information by preforming a transitive closure
905 on the local information that was produced by generate_summary.
906 Note that there is no function_transform pass since this only
907 updates the function_decl. */
909 static unsigned int
910 propagate (void)
912 struct cgraph_node *node;
913 struct cgraph_node *w;
914 struct cgraph_node **order =
915 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
916 int order_pos;
917 int i;
918 struct ipa_dfs_info * w_info;
920 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
921 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
922 cgraph_remove_node_removal_hook (node_removal_hook_holder);
923 order_pos = ipa_utils_reduced_inorder (order, true, false, NULL);
924 if (dump_file)
926 dump_cgraph (dump_file);
927 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
930 /* Propagate the local information thru the call graph to produce
931 the global information. All the nodes within a cycle will have
932 the same info so we collapse cycles first. Then we can do the
933 propagation in one pass from the leaves to the roots. */
934 for (i = 0; i < order_pos; i++ )
936 enum pure_const_state_e pure_const_state = IPA_CONST;
937 bool looping = false;
938 int count = 0;
939 node = order[i];
941 /* Find the worst state for any node in the cycle. */
942 w = node;
943 while (w)
945 struct cgraph_edge *e;
946 funct_state w_l = get_function_state (w);
947 if (pure_const_state < w_l->pure_const_state)
948 pure_const_state = w_l->pure_const_state;
950 if (w_l->looping)
951 looping = true;
952 if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
954 looping |= w_l->looping_previously_known;
955 if (pure_const_state < w_l->state_previously_known)
956 pure_const_state = w_l->state_previously_known;
959 if (pure_const_state == IPA_NEITHER)
960 break;
962 count++;
964 if (count > 1)
965 looping = true;
967 for (e = w->callees; e; e = e->next_callee)
969 struct cgraph_node *y = e->callee;
971 if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
973 funct_state y_l = get_function_state (y);
974 if (pure_const_state < y_l->pure_const_state)
975 pure_const_state = y_l->pure_const_state;
976 if (pure_const_state == IPA_NEITHER)
977 break;
978 if (y_l->looping)
979 looping = true;
981 else
983 int flags = flags_from_decl_or_type (y->decl);
985 if (flags & ECF_LOOPING_CONST_OR_PURE)
986 looping = true;
987 if (flags & ECF_CONST)
989 else if ((flags & ECF_PURE) && pure_const_state == IPA_CONST)
990 pure_const_state = IPA_PURE;
991 else
992 pure_const_state = IPA_NEITHER, looping = true;
996 w_info = (struct ipa_dfs_info *) w->aux;
997 w = w_info->next_cycle;
1000 /* Copy back the region's pure_const_state which is shared by
1001 all nodes in the region. */
1002 w = node;
1003 while (w)
1005 funct_state w_l = get_function_state (w);
1006 enum pure_const_state_e this_state = pure_const_state;
1007 bool this_looping = looping;
1009 if (w_l->state_previously_known != IPA_NEITHER
1010 && this_state > w_l->state_previously_known)
1011 this_state = w_l->state_previously_known;
1012 if (!this_looping && self_recursive_p (w))
1013 this_looping = true;
1014 if (!w_l->looping_previously_known)
1015 this_looping = false;
1017 /* All nodes within a cycle share the same info. */
1018 w_l->pure_const_state = this_state;
1019 w_l->looping = this_looping;
1021 switch (this_state)
1023 case IPA_CONST:
1024 if (!TREE_READONLY (w->decl))
1026 warn_function_const (w->decl, !this_looping);
1027 if (dump_file)
1028 fprintf (dump_file, "Function found to be %sconst: %s\n",
1029 this_looping ? "looping " : "",
1030 cgraph_node_name (w));
1032 cgraph_set_readonly_flag (w, true);
1033 cgraph_set_looping_const_or_pure_flag (w, this_looping);
1034 break;
1036 case IPA_PURE:
1037 if (!DECL_PURE_P (w->decl))
1039 warn_function_pure (w->decl, !this_looping);
1040 if (dump_file)
1041 fprintf (dump_file, "Function found to be %spure: %s\n",
1042 this_looping ? "looping " : "",
1043 cgraph_node_name (w));
1045 cgraph_set_pure_flag (w, true);
1046 cgraph_set_looping_const_or_pure_flag (w, this_looping);
1047 break;
1049 default:
1050 break;
1052 w_info = (struct ipa_dfs_info *) w->aux;
1053 w = w_info->next_cycle;
1057 /* Cleanup. */
1058 for (node = cgraph_nodes; node; node = node->next)
1060 /* Get rid of the aux information. */
1061 if (node->aux)
1063 w_info = (struct ipa_dfs_info *) node->aux;
1064 free (node->aux);
1065 node->aux = NULL;
1068 order_pos = ipa_utils_reduced_inorder (order, true, false, ignore_edge);
1069 if (dump_file)
1071 dump_cgraph (dump_file);
1072 ipa_utils_print_order(dump_file, "reduced for nothrow", order, order_pos);
1074 /* Propagate the local information thru the call graph to produce
1075 the global information. All the nodes within a cycle will have
1076 the same info so we collapse cycles first. Then we can do the
1077 propagation in one pass from the leaves to the roots. */
1078 for (i = 0; i < order_pos; i++ )
1080 bool can_throw = false;
1081 node = order[i];
1083 /* Find the worst state for any node in the cycle. */
1084 w = node;
1085 while (w)
1087 struct cgraph_edge *e;
1088 funct_state w_l = get_function_state (w);
1090 if (w_l->can_throw
1091 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1092 can_throw = true;
1094 if (can_throw)
1095 break;
1097 for (e = w->callees; e; e = e->next_callee)
1099 struct cgraph_node *y = e->callee;
1101 if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
1103 funct_state y_l = get_function_state (y);
1105 if (can_throw)
1106 break;
1107 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1108 && e->can_throw_external)
1109 can_throw = true;
1111 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1112 can_throw = true;
1114 w_info = (struct ipa_dfs_info *) w->aux;
1115 w = w_info->next_cycle;
1118 /* Copy back the region's pure_const_state which is shared by
1119 all nodes in the region. */
1120 w = node;
1121 while (w)
1123 funct_state w_l = get_function_state (w);
1124 if (!can_throw && !TREE_NOTHROW (w->decl))
1126 struct cgraph_edge *e;
1127 cgraph_set_nothrow_flag (w, true);
1128 for (e = w->callers; e; e = e->next_caller)
1129 e->can_throw_external = false;
1130 if (dump_file)
1131 fprintf (dump_file, "Function found to be nothrow: %s\n",
1132 cgraph_node_name (w));
1134 else if (can_throw && !TREE_NOTHROW (w->decl))
1135 w_l->can_throw = true;
1136 w_info = (struct ipa_dfs_info *) w->aux;
1137 w = w_info->next_cycle;
1141 /* Cleanup. */
1142 for (node = cgraph_nodes; node; node = node->next)
1144 /* Get rid of the aux information. */
1145 if (node->aux)
1147 w_info = (struct ipa_dfs_info *) node->aux;
1148 free (node->aux);
1149 node->aux = NULL;
1151 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
1152 free (get_function_state (node));
1155 free (order);
1156 VEC_free (funct_state, heap, funct_state_vec);
1157 finish_state ();
1158 return 0;
1161 static bool
1162 gate_pure_const (void)
1164 return (flag_ipa_pure_const
1165 /* Don't bother doing anything if the program has errors. */
1166 && !seen_error ());
1169 struct ipa_opt_pass_d pass_ipa_pure_const =
1172 IPA_PASS,
1173 "pure-const", /* name */
1174 gate_pure_const, /* gate */
1175 propagate, /* execute */
1176 NULL, /* sub */
1177 NULL, /* next */
1178 0, /* static_pass_number */
1179 TV_IPA_PURE_CONST, /* tv_id */
1180 0, /* properties_required */
1181 0, /* properties_provided */
1182 0, /* properties_destroyed */
1183 0, /* todo_flags_start */
1184 0 /* todo_flags_finish */
1186 generate_summary, /* generate_summary */
1187 pure_const_write_summary, /* write_summary */
1188 pure_const_read_summary, /* read_summary */
1189 NULL, /* write_optimization_summary */
1190 NULL, /* read_optimization_summary */
1191 NULL, /* stmt_fixup */
1192 0, /* TODOs */
1193 NULL, /* function_transform */
1194 NULL /* variable_transform */
1197 /* Return true if function should be skipped for local pure const analysis. */
1199 static bool
1200 skip_function_for_local_pure_const (struct cgraph_node *node)
1202 /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1203 we must not promote functions that are called by already processed functions. */
1205 if (function_called_by_processed_nodes_p ())
1207 if (dump_file)
1208 fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1209 return true;
1211 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1213 if (dump_file)
1214 fprintf (dump_file, "Function is not available or overwrittable; not analyzing.\n");
1215 return true;
1217 return false;
1220 /* Simple local pass for pure const discovery reusing the analysis from
1221 ipa_pure_const. This pass is effective when executed together with
1222 other optimization passes in early optimization pass queue. */
1224 static unsigned int
1225 local_pure_const (void)
1227 bool changed = false;
1228 funct_state l;
1229 bool skip;
1230 struct cgraph_node *node;
1232 node = cgraph_node (current_function_decl);
1233 skip = skip_function_for_local_pure_const (node);
1234 if (!warn_suggest_attribute_const
1235 && !warn_suggest_attribute_pure
1236 && skip)
1237 return 0;
1238 l = analyze_function (node, false);
1240 switch (l->pure_const_state)
1242 case IPA_CONST:
1243 if (!TREE_READONLY (current_function_decl))
1245 warn_function_const (current_function_decl, !l->looping);
1246 if (!skip)
1248 cgraph_set_readonly_flag (node, true);
1249 cgraph_set_looping_const_or_pure_flag (node, l->looping);
1250 changed = true;
1252 if (dump_file)
1253 fprintf (dump_file, "Function found to be %sconst: %s\n",
1254 l->looping ? "looping " : "",
1255 lang_hooks.decl_printable_name (current_function_decl,
1256 2));
1258 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1259 && !l->looping)
1261 if (!skip)
1263 cgraph_set_looping_const_or_pure_flag (node, false);
1264 changed = true;
1266 if (dump_file)
1267 fprintf (dump_file, "Function found to be non-looping: %s\n",
1268 lang_hooks.decl_printable_name (current_function_decl,
1269 2));
1271 break;
1273 case IPA_PURE:
1274 if (!DECL_PURE_P (current_function_decl))
1276 if (!skip)
1278 cgraph_set_pure_flag (node, true);
1279 cgraph_set_looping_const_or_pure_flag (node, l->looping);
1280 changed = true;
1282 warn_function_pure (current_function_decl, !l->looping);
1283 if (dump_file)
1284 fprintf (dump_file, "Function found to be %spure: %s\n",
1285 l->looping ? "looping " : "",
1286 lang_hooks.decl_printable_name (current_function_decl,
1287 2));
1289 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1290 && !l->looping)
1292 if (!skip)
1294 cgraph_set_looping_const_or_pure_flag (node, false);
1295 changed = true;
1297 if (dump_file)
1298 fprintf (dump_file, "Function found to be non-looping: %s\n",
1299 lang_hooks.decl_printable_name (current_function_decl,
1300 2));
1302 break;
1304 default:
1305 break;
1307 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1309 struct cgraph_edge *e;
1311 cgraph_set_nothrow_flag (node, true);
1312 for (e = node->callers; e; e = e->next_caller)
1313 e->can_throw_external = false;
1314 changed = true;
1315 if (dump_file)
1316 fprintf (dump_file, "Function found to be nothrow: %s\n",
1317 lang_hooks.decl_printable_name (current_function_decl,
1318 2));
1320 if (l)
1321 free (l);
1322 if (changed)
1323 return execute_fixup_cfg ();
1324 else
1325 return 0;
1328 struct gimple_opt_pass pass_local_pure_const =
1331 GIMPLE_PASS,
1332 "local-pure-const", /* name */
1333 gate_pure_const, /* gate */
1334 local_pure_const, /* execute */
1335 NULL, /* sub */
1336 NULL, /* next */
1337 0, /* static_pass_number */
1338 TV_IPA_PURE_CONST, /* tv_id */
1339 0, /* properties_required */
1340 0, /* properties_provided */
1341 0, /* properties_destroyed */
1342 0, /* todo_flags_start */
1343 0 /* todo_flags_finish */