* Merge with edge-vector-mergepoint-20040918.
[official-gcc.git] / gcc / cgraphunit.c
blobe4348ca8b83134792a8d5c350c3125a3a184f75f
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This module implements main driver of compilation process as well as
23 few basic intraprocedural optimizers.
25 The main scope of this file is to act as an interface in between
26 tree based frontends and the backend (and middle end)
28 The front-end is supposed to use following functionality:
30 - cgraph_finalize_function
32 This function is called once front-end has parsed whole body of function
33 and it is certain that the function body nor the declaration will change.
35 (There is one exception needed for implementing GCC extern inline function.)
37 - cgraph_varpool_finalize_variable
39 This function has same behavior as the above but is used for static
40 variables.
42 - cgraph_finalize_compilation_unit
44 This function is called once compilation unit is finalized and it will
45 no longer change.
47 In the unit-at-a-time the call-graph construction and local function
48 analysis takes place here. Bodies of unreachable functions are released
49 to conserve memory usage.
51 ??? The compilation unit in this point of view should be compilation
52 unit as defined by the language - for instance C frontend allows multiple
53 compilation units to be parsed at once and it should call function each
54 time parsing is done so we save memory.
56 - cgraph_optimize
58 In this unit-at-a-time compilation the intra procedural analysis takes
59 place here. In particular the static functions whose address is never
60 taken are marked as local. Backend can then use this information to
61 modify calling conventions, do better inlining or similar optimizations.
63 - cgraph_assemble_pending_functions
64 - cgraph_varpool_assemble_pending_variables
66 In non-unit-at-a-time mode these functions can be used to force compilation
67 of functions or variables that are known to be needed at given stage
68 of compilation
70 - cgraph_mark_needed_node
71 - cgraph_varpool_mark_needed_node
73 When function or variable is referenced by some hidden way (for instance
74 via assembly code and marked by attribute "used"), the call-graph data structure
75 must be updated accordingly by this function.
77 - analyze_expr callback
79 This function is responsible for lowering tree nodes not understood by
80 generic code into understandable ones or alternatively marking
81 callgraph and varpool nodes referenced by the as needed.
83 ??? On the tree-ssa genericizing should take place here and we will avoid
84 need for these hooks (replacing them by genericizing hook)
86 - expand_function callback
88 This function is used to expand function and pass it into RTL back-end.
89 Front-end should not make any assumptions about when this function can be
90 called. In particular cgraph_assemble_pending_functions,
91 cgraph_varpool_assemble_pending_variables, cgraph_finalize_function,
92 cgraph_varpool_finalize_function, cgraph_optimize can cause arbitrarily
93 previously finalized functions to be expanded.
95 We implement two compilation modes.
97 - unit-at-a-time: In this mode analyzing of all functions is deferred
98 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
100 In cgraph_finalize_compilation_unit the reachable functions are
101 analyzed. During analysis the call-graph edges from reachable
102 functions are constructed and their destinations are marked as
103 reachable. References to functions and variables are discovered too
104 and variables found to be needed output to the assembly file. Via
105 mark_referenced call in assemble_variable functions referenced by
106 static variables are noticed too.
108 The intra-procedural information is produced and it's existence
109 indicated by global_info_ready. Once this flag is set it is impossible
110 to change function from !reachable to reachable and thus
111 assemble_variable no longer call mark_referenced.
113 Finally the call-graph is topologically sorted and all reachable functions
114 that has not been completely inlined or are not external are output.
116 ??? It is possible that reference to function or variable is optimized
117 out. We can not deal with this nicely because topological order is not
118 suitable for it. For tree-ssa we may consider another pass doing
119 optimization and re-discovering reachable functions.
121 ??? Reorganize code so variables are output very last and only if they
122 really has been referenced by produced code, so we catch more cases
123 where reference has been optimized out.
125 - non-unit-at-a-time
127 All functions are variables are output as early as possible to conserve
128 memory consumption. This may or may not result in less memory used but
129 it is still needed for some legacy code that rely on particular ordering
130 of things output from the compiler.
132 Varpool data structures are not used and variables are output directly.
134 Functions are output early using call of
135 cgraph_assemble_pending_function from cgraph_finalize_function. The
136 decision on whether function is needed is made more conservative so
137 uninlininable static functions are needed too. During the call-graph
138 construction the edge destinations are not marked as reachable and it
139 is completely relied upn assemble_variable to mark them.
141 Inlining decision heuristics
142 ??? Move this to separate file after tree-ssa merge.
144 We separate inlining decisions from the inliner itself and store it
145 inside callgraph as so called inline plan. Refer to cgraph.c
146 documentation about particular representation of inline plans in the
147 callgraph
149 The implementation of particular heuristics is separated from
150 the rest of code to make it easier to replace it with more complicated
151 implementation in the future. The rest of inlining code acts as a
152 library aimed to modify the callgraph and verify that the parameters
153 on code size growth fits.
155 To mark given call inline, use cgraph_mark_inline function, the
156 verification is performed by cgraph_default_inline_p and
157 cgraph_check_inline_limits.
159 The heuristics implements simple knapsack style algorithm ordering
160 all functions by their "profitability" (estimated by code size growth)
161 and inlining them in priority order.
163 cgraph_decide_inlining implements heuristics taking whole callgraph
164 into account, while cgraph_decide_inlining_incrementally considers
165 only one function at a time and is used in non-unit-at-a-time mode. */
168 /* Additionally this file gathers information about how local statics
169 are used. This is done in cgraph_characterize_statics. After the
170 call graph has been built, each function is analyzed to determine
171 which local static variables are either read or written or have
172 their address taken. Any local static that has its address taken
173 is removed from consideration. Once the local read and writes
174 are determined, a transitive closure of this information is
175 performed over the call graph to determine the worst case set of
176 side effects of each call. In a later part of the compiler, these
177 local and global sets are examined to make the call clobbering less
178 traumatic both with respect to aliasing and to code generation. */
180 #include "config.h"
181 #include "system.h"
182 #include "coretypes.h"
183 #include "tm.h"
184 #include "tree.h"
185 #include "rtl.h"
186 #include "tree-flow.h"
187 #include "tree-inline.h"
188 #include "langhooks.h"
189 #include "hashtab.h"
190 #include "toplev.h"
191 #include "flags.h"
192 #include "ggc.h"
193 #include "debug.h"
194 #include "target.h"
195 #include "cgraph.h"
196 #include "diagnostic.h"
197 #include "timevar.h"
198 #include "params.h"
199 #include "fibheap.h"
200 #include "c-common.h"
201 #include "intl.h"
202 #include "function.h"
203 #include "tree-gimple.h"
205 #define INSNS_PER_CALL 10
207 static void cgraph_expand_all_functions (void);
208 static void cgraph_mark_functions_to_output (void);
209 static void cgraph_expand_function (struct cgraph_node *);
210 static tree record_call_1 (tree *, int *, void *);
211 static void cgraph_mark_local_and_external_functions (void);
212 static bool cgraph_default_inline_p (struct cgraph_node *n);
213 static void cgraph_analyze_function (struct cgraph_node *node);
214 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
216 /* Statistics we collect about inlining algorithm. */
217 static int ncalls_inlined;
218 static int nfunctions_inlined;
219 static int initial_insns;
220 static int overall_insns;
222 /* Records tree nodes seen in cgraph_create_edges. Simply using
223 walk_tree_without_duplicates doesn't guarantee each node is visited
224 once because it gets a new htab upon each recursive call from
225 record_calls_1. */
226 static htab_t visited_nodes;
228 static FILE *cgraph_dump_file;
230 /* These splay trees contain all of the static variables that are
231 being considered by the compilation level alias analysis. For
232 module_at_a_time compilation, this is the set of static but not
233 public variables. Any variables that either have their address
234 taken or participate in otherwise unsavory operations are deleted
235 from this list. */
236 static GTY((param1_is(tree), param2_is(tree)))
237 splay_tree static_vars_to_consider_by_tree;
239 /* FIXME -- PROFILE-RESTRUCTURE: change comment from DECL_UID to var-ann. */
240 /* same as above but indexed by DECL_UID */
241 static GTY((param1_is(int), param2_is(tree)))
242 splay_tree static_vars_to_consider_by_uid;
244 /* This bitmap is used to knock out the module static variables whose
245 addresses have been taken and passed around. This is indexed by
246 uid. */
247 static bitmap module_statics_escape;
249 /* FIXME -- PROFILE-RESTRUCTURE: change comment from DECL_UID to var-ann. */
250 /* A bit is set for every module static we are considering and is
251 indexed by DECL_UID. This is ored into the local info when asm
252 code is found that clobbers all memory. */
253 static GTY(()) bitmap all_module_statics;
255 /* Holds the value of "memory". */
256 static tree memory_identifier;
258 /* Determine if function DECL is needed. That is, visible to something
259 either outside this translation unit, something magic in the system
260 configury, or (if not doing unit-at-a-time) to something we havn't
261 seen yet. */
263 static bool
264 decide_is_function_needed (struct cgraph_node *node, tree decl)
266 struct cgraph_node *origin;
268 /* If we decided it was needed before, but at the time we didn't have
269 the body of the function available, then it's still needed. We have
270 to go back and re-check its dependencies now. */
271 if (node->needed)
272 return true;
274 /* Externally visible functions must be output. The exception is
275 COMDAT functions that must be output only when they are needed. */
276 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
277 return true;
279 /* Constructors and destructors are reachable from the runtime by
280 some mechanism. */
281 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
282 return true;
284 /* If the user told us it is used, then it must be so. */
285 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
286 return true;
288 /* ??? If the assembler name is set by hand, it is possible to assemble
289 the name later after finalizing the function and the fact is noticed
290 in assemble_name then. This is arguably a bug. */
291 if (DECL_ASSEMBLER_NAME_SET_P (decl)
292 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
293 return true;
295 if (flag_unit_at_a_time)
296 return false;
298 /* If not doing unit at a time, then we'll only defer this function
299 if its marked for inlining. Otherwise we want to emit it now. */
301 /* "extern inline" functions are never output locally. */
302 if (DECL_EXTERNAL (decl))
303 return false;
304 /* Nested functions of extern inline function shall not be emit unless
305 we inlined the origin. */
306 for (origin = node->origin; origin; origin = origin->origin)
307 if (DECL_EXTERNAL (origin->decl))
308 return false;
309 /* We want to emit COMDAT functions only when absolutely necessary. */
310 if (DECL_COMDAT (decl))
311 return false;
312 if (!DECL_INLINE (decl)
313 || (!node->local.disregard_inline_limits
314 /* When declared inline, defer even the uninlinable functions.
315 This allows them to be eliminated when unused. */
316 && !DECL_DECLARED_INLINE_P (decl)
317 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
318 return true;
320 return false;
323 /* Debugging function for postorder and inorder code. NOTE is a string
324 that is printed before the nodes are printed. ORDER is an array of
325 cgraph_nodes that has COUNT useful nodes in it. */
327 static void
328 print_order (const char * note, struct cgraph_node** order, int count)
330 int i;
331 fprintf (cgraph_dump_file, "\n\n ordered call graph: %s\n", note);
333 for (i = count - 1; i >= 0; i--)
335 struct cgraph_edge *edge;
337 fprintf (cgraph_dump_file, "\n %s<-(", cgraph_node_name (order[i]));
339 for (edge = order[i]->callers; edge; edge = edge->next_caller)
340 fprintf (cgraph_dump_file, " %s", cgraph_node_name (edge->caller));
341 fprintf (cgraph_dump_file, ")");
343 fprintf (cgraph_dump_file, "\n");
346 /* FIXME -- PROFILE-RESTRUCTURE: Remove this function, it becomes a nop. */
347 /* Convert IN_DECL bitmap which is indexed by DECL_UID to IN_ANN, a
348 bitmap indexed by var_ann (VAR_DECL)->uid. */
350 static void
351 convert_UIDs_in_bitmap (bitmap in_ann, bitmap in_decl)
353 int index;
354 EXECUTE_IF_SET_IN_BITMAP(in_decl, 0, index,
356 splay_tree_node n =
357 splay_tree_lookup (static_vars_to_consider_by_uid, index);
358 if (n != NULL)
360 tree t = (tree)n->value;
361 var_ann_t va = var_ann (t);
362 if (va)
363 bitmap_set_bit(in_ann, va->uid);
368 /* FIXME -- PROFILE-RESTRUCTURE: Delete all stmts initing *_decl_uid
369 variables. Add code to create a var_ann for tree node within the
370 cgraph_node and have it point to the newly created
371 static_vars_info. */
372 /* Create a new static_vars_info structure and place it into
373 cgraph_node, NODE. INIT_GLOBAL causes the global part of the
374 structure to be initialized. */
375 static static_vars_info_t
376 new_static_vars_info(struct cgraph_node* node,
377 bool init_global)
379 static_vars_info_t info = ggc_calloc (1, sizeof (struct static_vars_info_d));
380 local_static_vars_info_t l
381 = ggc_calloc (1, sizeof (struct local_static_vars_info_d));
383 /* Add the info to the tree's annotation. */
384 var_ann_t var_ann = get_var_ann(node->decl);
385 node->static_vars_info = info;
386 var_ann->static_vars_info = info;
388 info->local = l;
389 l->statics_read_by_decl_uid = BITMAP_GGC_ALLOC ();
390 l->statics_written_by_decl_uid = BITMAP_GGC_ALLOC ();
392 if (init_global)
394 global_static_vars_info_t g
395 = ggc_calloc (1, sizeof (struct global_static_vars_info_d));
396 info->global = g;
397 g->statics_read_by_decl_uid = BITMAP_GGC_ALLOC ();
398 g->statics_written_by_decl_uid = BITMAP_GGC_ALLOC ();
399 g->statics_read_by_ann_uid = BITMAP_GGC_ALLOC ();
400 g->statics_written_by_ann_uid = BITMAP_GGC_ALLOC ();
401 g->statics_not_read_by_decl_uid = BITMAP_GGC_ALLOC ();
402 g->statics_not_written_by_decl_uid = BITMAP_GGC_ALLOC ();
403 g->statics_not_read_by_ann_uid = BITMAP_GGC_ALLOC ();
404 g->statics_not_written_by_ann_uid = BITMAP_GGC_ALLOC ();
406 return info;
410 /* FIXME -- PROFILE-RESTRUCTURE: Remove this function, it becomes a
411 nop. */
412 /* The bitmaps used to represent the static global variables are
413 indexed by DECL_UID however, this is not used inside of functions
414 to index the ssa variables. The denser var_ann (VAR_DECL)->uid is
415 used there. This function is called from
416 tree_dfa:find_referenced_vars after the denser representation is
417 built. This function invalidates any cached indexes. */
419 void
420 cgraph_reset_static_var_maps (void)
422 struct cgraph_node *node;
424 for (node = cgraph_nodes; node; node = node->next)
426 static_vars_info_t info = node->static_vars_info;
427 if (info)
429 global_static_vars_info_t g = info->global;
430 if (g->var_anns_valid)
432 bitmap_clear (g->statics_read_by_ann_uid);
433 bitmap_clear (g->statics_written_by_ann_uid);
434 bitmap_clear (g->statics_not_read_by_ann_uid);
435 bitmap_clear (g->statics_not_written_by_ann_uid);
436 g->var_anns_valid = false;
439 else
440 /* Handle the case where a cgraph node has been inserted
441 after the analysis. We know nothing. */
442 new_static_vars_info(node, true);
446 /* Get the global static_vars_info structure for the function FN and
447 make sure the ann_uid's bitmaps are properly converted. */
449 static global_static_vars_info_t
450 get_global_static_vars_info (tree fn)
452 global_static_vars_info_t g;
454 /* Was not compiled -O2 or higher. */
455 static_vars_info_t info = get_var_ann(fn)->static_vars_info;
456 if (!info)
457 return NULL;
459 g = info->global;
460 if (!g->var_anns_valid)
462 convert_UIDs_in_bitmap (g->statics_read_by_ann_uid,
463 g->statics_read_by_decl_uid);
464 convert_UIDs_in_bitmap (g->statics_written_by_ann_uid,
465 g->statics_written_by_decl_uid);
466 convert_UIDs_in_bitmap (g->statics_not_read_by_ann_uid,
467 g->statics_not_read_by_decl_uid);
468 convert_UIDs_in_bitmap (g->statics_not_written_by_ann_uid,
469 g->statics_not_written_by_decl_uid);
470 g->var_anns_valid = true;
472 return g;
475 /* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
476 variables that are not read during the execution of the function
477 FN. Returns NULL if no data is available, such as it was not
478 compiled with -O2 or higher. */
480 bitmap
481 get_global_statics_not_read (tree fn)
483 global_static_vars_info_t g = get_global_static_vars_info (fn);
484 if (g)
485 return g->statics_not_read_by_ann_uid;
486 else
487 return NULL;
490 /* Return a bitmap indexed by var_ann (VAR_DECL)->uid for the static
491 variables that are not written during the execution of the function
492 FN. Note that variables written may or may not be read during the
493 function call. Returns NULL if no data is available, such as it
494 was not compiled with -O2 or higher. */
496 bitmap
497 get_global_statics_not_written (tree fn)
499 global_static_vars_info_t g = get_global_static_vars_info (fn);
500 if (g)
501 return g->statics_not_written_by_ann_uid;
502 else
503 return NULL;
506 /* When not doing unit-at-a-time, output all functions enqueued.
507 Return true when such a functions were found. */
509 bool
510 cgraph_assemble_pending_functions (void)
512 bool output = false;
514 if (flag_unit_at_a_time)
515 return false;
517 while (cgraph_nodes_queue)
519 struct cgraph_node *n = cgraph_nodes_queue;
521 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
522 n->next_needed = NULL;
523 if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl))
525 cgraph_expand_function (n);
526 output = true;
530 return output;
533 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
534 logic in effect. If NESTED is true, then our caller cannot stand to have
535 the garbage collector run at the moment. We would need to either create
536 a new GC context, or just not compile right now. */
538 void
539 cgraph_finalize_function (tree decl, bool nested)
541 struct cgraph_node *node = cgraph_node (decl);
543 if (node->local.finalized)
545 /* As an GCC extension we allow redefinition of the function. The
546 semantics when both copies of bodies differ is not well defined.
547 We replace the old body with new body so in unit at a time mode
548 we always use new body, while in normal mode we may end up with
549 old body inlined into some functions and new body expanded and
550 inlined in others.
552 ??? It may make more sense to use one body for inlining and other
553 body for expanding the function but this is difficult to do. */
555 /* If node->output is set, then this is a unit-at-a-time compilation
556 and we have already begun whole-unit analysis. This is *not*
557 testing for whether we've already emitted the function. That
558 case can be sort-of legitimately seen with real function
559 redefinition errors. I would argue that the front end should
560 never present us with such a case, but don't enforce that for now. */
561 gcc_assert (!node->output);
563 /* Reset our data structures so we can analyze the function again. */
564 memset (&node->local, 0, sizeof (node->local));
565 memset (&node->global, 0, sizeof (node->global));
566 memset (&node->rtl, 0, sizeof (node->rtl));
567 node->analyzed = false;
568 node->local.redefined_extern_inline = true;
569 while (node->callees)
570 cgraph_remove_edge (node->callees);
572 /* We may need to re-queue the node for assembling in case
573 we already proceeded it and ignored as not needed. */
574 if (node->reachable && !flag_unit_at_a_time)
576 struct cgraph_node *n;
578 for (n = cgraph_nodes_queue; n; n = n->next_needed)
579 if (n == node)
580 break;
581 if (!n)
582 node->reachable = 0;
586 notice_global_symbol (decl);
587 node->decl = decl;
588 node->local.finalized = true;
590 /* If not unit at a time, then we need to create the call graph
591 now, so that called functions can be queued and emitted now. */
592 if (!flag_unit_at_a_time)
594 cgraph_analyze_function (node);
595 cgraph_decide_inlining_incrementally (node);
598 if (decide_is_function_needed (node, decl))
599 cgraph_mark_needed_node (node);
601 /* If not unit at a time, go ahead and emit everything we've found
602 to be reachable at this time. */
603 if (!nested)
605 if (!cgraph_assemble_pending_functions ())
606 ggc_collect ();
609 /* If we've not yet emitted decl, tell the debug info about it. */
610 if (!TREE_ASM_WRITTEN (decl))
611 (*debug_hooks->deferred_inline_function) (decl);
613 /* Possibly warn about unused parameters. */
614 if (warn_unused_parameter)
615 do_warn_unused_parameter (decl);
618 /* Walk tree and record all calls. Called via walk_tree. */
619 static tree
620 record_call_1 (tree *tp, int *walk_subtrees, void *data)
622 tree t = *tp;
624 switch (TREE_CODE (t))
626 case VAR_DECL:
627 /* ??? Really, we should mark this decl as *potentially* referenced
628 by this function and re-examine whether the decl is actually used
629 after rtl has been generated. */
630 if (TREE_STATIC (t))
632 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
633 if (lang_hooks.callgraph.analyze_expr)
634 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
635 data);
637 break;
639 case ADDR_EXPR:
640 if (flag_unit_at_a_time)
642 /* Record dereferences to the functions. This makes the
643 functions reachable unconditionally. */
644 tree decl = TREE_OPERAND (*tp, 0);
645 if (TREE_CODE (decl) == FUNCTION_DECL)
646 cgraph_mark_needed_node (cgraph_node (decl));
648 break;
650 case CALL_EXPR:
652 tree decl = get_callee_fndecl (*tp);
653 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
655 cgraph_create_edge (data, cgraph_node (decl), *tp);
657 /* When we see a function call, we don't want to look at the
658 function reference in the ADDR_EXPR that is hanging from
659 the CALL_EXPR we're examining here, because we would
660 conclude incorrectly that the function's address could be
661 taken by something that is not a function call. So only
662 walk the function parameter list, skip the other subtrees. */
664 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
665 visited_nodes);
666 *walk_subtrees = 0;
668 break;
671 default:
672 /* Save some cycles by not walking types and declaration as we
673 won't find anything useful there anyway. */
674 if (IS_TYPE_OR_DECL_P (*tp))
676 *walk_subtrees = 0;
677 break;
680 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
681 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
682 break;
685 return NULL;
688 /* Create cgraph edges for function calls inside BODY from NODE. */
690 void
691 cgraph_create_edges (struct cgraph_node *node, tree body)
693 /* The nodes we're interested in are never shared, so walk
694 the tree ignoring duplicates. */
695 visited_nodes = htab_create (37, htab_hash_pointer,
696 htab_eq_pointer, NULL);
697 walk_tree (&body, record_call_1, node, visited_nodes);
698 htab_delete (visited_nodes);
699 visited_nodes = NULL;
702 static bool error_found;
704 /* Callbrack of verify_cgraph_node. Check that all call_exprs have cgraph
705 nodes. */
707 static tree
708 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
710 tree t = *tp;
711 tree decl;
713 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
715 struct cgraph_edge *e = cgraph_edge (data, t);
716 if (e)
718 if (e->aux)
720 error ("Shared call_expr:");
721 debug_tree (t);
722 error_found = true;
724 if (e->callee->decl != cgraph_node (decl)->decl)
726 error ("Edge points to wrong declaration:");
727 debug_tree (e->callee->decl);
728 fprintf (stderr," Instead of:");
729 debug_tree (decl);
731 e->aux = (void *)1;
733 else
735 error ("Missing callgraph edge for call expr:");
736 debug_tree (t);
737 error_found = true;
741 /* Save some cycles by not walking types and declaration as we
742 won't find anything useful there anyway. */
743 if (IS_TYPE_OR_DECL_P (*tp))
744 *walk_subtrees = 0;
746 return NULL_TREE;
749 /* Verify cgraph nodes of given cgraph node. */
750 void
751 verify_cgraph_node (struct cgraph_node *node)
753 struct cgraph_edge *e;
754 struct cgraph_node *main_clone;
756 timevar_push (TV_CGRAPH_VERIFY);
757 error_found = false;
758 for (e = node->callees; e; e = e->next_callee)
759 if (e->aux)
761 error ("Aux field set for edge %s->%s",
762 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
763 error_found = true;
765 for (e = node->callers; e; e = e->next_caller)
767 if (!e->inline_failed)
769 if (node->global.inlined_to
770 != (e->caller->global.inlined_to
771 ? e->caller->global.inlined_to : e->caller))
773 error ("Inlined_to pointer is wrong");
774 error_found = true;
776 if (node->callers->next_caller)
778 error ("Multiple inline callers");
779 error_found = true;
782 else
783 if (node->global.inlined_to)
785 error ("Inlined_to pointer set for noninline callers");
786 error_found = true;
789 if (!node->callers && node->global.inlined_to)
791 error ("Inlined_to pointer is set but no predecesors found");
792 error_found = true;
794 if (node->global.inlined_to == node)
796 error ("Inlined_to pointer reffers to itself");
797 error_found = true;
800 for (main_clone = cgraph_node (node->decl); main_clone;
801 main_clone = main_clone->next_clone)
802 if (main_clone == node)
803 break;
804 if (!node)
806 error ("Node not found in DECL_ASSEMBLER_NAME hash");
807 error_found = true;
810 if (node->analyzed
811 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
812 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
814 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
815 verify_cgraph_node_1, node);
816 for (e = node->callees; e; e = e->next_callee)
818 if (!e->aux)
820 error ("Edge %s->%s has no corresponding call_expr",
821 cgraph_node_name (e->caller),
822 cgraph_node_name (e->callee));
823 error_found = true;
825 e->aux = 0;
828 if (error_found)
830 dump_cgraph_node (stderr, node);
831 internal_error ("verify_cgraph_node failed.");
833 timevar_pop (TV_CGRAPH_VERIFY);
836 /* Verify whole cgraph structure. */
837 void
838 verify_cgraph (void)
840 struct cgraph_node *node;
842 if (sorrycount || errorcount)
843 return;
845 for (node = cgraph_nodes; node; node = node->next)
846 verify_cgraph_node (node);
849 /* Analyze the function scheduled to be output. */
850 static void
851 cgraph_analyze_function (struct cgraph_node *node)
853 tree decl = node->decl;
854 struct cgraph_edge *e;
856 current_function_decl = decl;
858 /* First kill forward declaration so reverse inlining works properly. */
859 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
861 node->local.inlinable = tree_inlinable_function_p (decl);
862 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
863 if (node->local.inlinable)
864 node->local.disregard_inline_limits
865 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
866 for (e = node->callers; e; e = e->next_caller)
868 if (node->local.redefined_extern_inline)
869 e->inline_failed = N_("redefined extern inline functions are not "
870 "considered for inlining");
871 else if (!node->local.inlinable)
872 e->inline_failed = N_("function not inlinable");
873 else
874 e->inline_failed = N_("function not considered for inlining");
876 if (flag_really_no_inline && !node->local.disregard_inline_limits)
877 node->local.inlinable = 0;
878 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
879 node->global.insns = node->local.self_insns;
881 node->analyzed = true;
882 current_function_decl = NULL;
885 /* Analyze the whole compilation unit once it is parsed completely. */
887 void
888 cgraph_finalize_compilation_unit (void)
890 struct cgraph_node *node;
892 if (!flag_unit_at_a_time)
894 cgraph_assemble_pending_functions ();
895 return;
898 cgraph_varpool_assemble_pending_decls ();
899 if (!quiet_flag)
900 fprintf (stderr, "\nAnalyzing compilation unit\n");
902 timevar_push (TV_CGRAPH);
903 if (cgraph_dump_file)
905 fprintf (cgraph_dump_file, "Initial entry points:");
906 for (node = cgraph_nodes; node; node = node->next)
907 if (node->needed && DECL_SAVED_TREE (node->decl))
908 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
909 fprintf (cgraph_dump_file, "\n");
912 /* Propagate reachability flag and lower representation of all reachable
913 functions. In the future, lowering will introduce new functions and
914 new entry points on the way (by template instantiation and virtual
915 method table generation for instance). */
916 while (cgraph_nodes_queue)
918 struct cgraph_edge *edge;
919 tree decl = cgraph_nodes_queue->decl;
921 node = cgraph_nodes_queue;
922 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
923 node->next_needed = NULL;
925 /* ??? It is possible to create extern inline function and later using
926 weak alas attribute to kill its body. See
927 gcc.c-torture/compile/20011119-1.c */
928 if (!DECL_SAVED_TREE (decl))
929 continue;
931 gcc_assert (!node->analyzed && node->reachable);
932 gcc_assert (DECL_SAVED_TREE (decl));
934 cgraph_analyze_function (node);
936 for (edge = node->callees; edge; edge = edge->next_callee)
937 if (!edge->callee->reachable)
938 cgraph_mark_reachable_node (edge->callee);
940 cgraph_varpool_assemble_pending_decls ();
943 /* Collect entry points to the unit. */
945 if (cgraph_dump_file)
947 fprintf (cgraph_dump_file, "Unit entry points:");
948 for (node = cgraph_nodes; node; node = node->next)
949 if (node->needed && DECL_SAVED_TREE (node->decl))
950 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
951 fprintf (cgraph_dump_file, "\n\nInitial ");
952 dump_cgraph (cgraph_dump_file);
955 if (cgraph_dump_file)
956 fprintf (cgraph_dump_file, "\nReclaiming functions:");
958 for (node = cgraph_nodes; node; node = node->next)
960 tree decl = node->decl;
962 if (!node->reachable && DECL_SAVED_TREE (decl))
964 if (cgraph_dump_file)
965 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
966 cgraph_remove_node (node);
968 else
969 node->next_needed = NULL;
971 if (cgraph_dump_file)
973 fprintf (cgraph_dump_file, "\n\nReclaimed ");
974 dump_cgraph (cgraph_dump_file);
976 ggc_collect ();
977 timevar_pop (TV_CGRAPH);
979 /* Figure out what functions we want to assemble. */
981 static void
982 cgraph_mark_functions_to_output (void)
984 struct cgraph_node *node;
986 for (node = cgraph_nodes; node; node = node->next)
988 tree decl = node->decl;
989 struct cgraph_edge *e;
991 gcc_assert (!node->output);
993 for (e = node->callers; e; e = e->next_caller)
994 if (e->inline_failed)
995 break;
997 /* We need to output all local functions that are used and not
998 always inlined, as well as those that are reachable from
999 outside the current compilation unit. */
1000 if (DECL_SAVED_TREE (decl)
1001 && !node->global.inlined_to
1002 && (node->needed
1003 || (e && node->reachable))
1004 && !TREE_ASM_WRITTEN (decl)
1005 && !DECL_EXTERNAL (decl))
1006 node->output = 1;
1007 else
1009 /* We should've reclaimed all functions that are not needed. */
1010 #ifdef ENABLE_CHECKING
1011 if (!node->global.inlined_to && DECL_SAVED_TREE (decl)
1012 && !DECL_EXTERNAL (decl))
1014 dump_cgraph_node (stderr, node);
1015 internal_error ("failed to reclaim unneeded function");
1017 #endif
1018 gcc_assert (node->global.inlined_to || !DECL_SAVED_TREE (decl)
1019 || DECL_EXTERNAL (decl));
1026 /* Expand function specified by NODE. */
1028 static void
1029 cgraph_expand_function (struct cgraph_node *node)
1031 tree decl = node->decl;
1033 /* We ought to not compile any inline clones. */
1034 gcc_assert (!node->global.inlined_to);
1036 if (flag_unit_at_a_time)
1037 announce_function (decl);
1039 /* Generate RTL for the body of DECL. */
1040 lang_hooks.callgraph.expand_function (decl);
1042 /* Make sure that BE didn't give up on compiling. */
1043 /* ??? Can happen with nested function of extern inline. */
1044 gcc_assert (TREE_ASM_WRITTEN (node->decl));
1046 current_function_decl = NULL;
1047 if (!cgraph_preserve_function_body_p (node->decl))
1049 DECL_SAVED_TREE (node->decl) = NULL;
1050 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1051 DECL_INITIAL (node->decl) = error_mark_node;
1052 /* Eliminate all call edges. This is important so the call_expr no longer
1053 points to the dead function body. */
1054 while (node->callees)
1055 cgraph_remove_edge (node->callees);
1059 /* Fill array order with all nodes with output flag set in the reverse
1060 topological order. */
1062 static int
1063 cgraph_postorder (struct cgraph_node **order)
1065 struct cgraph_node *node, *node2;
1066 int stack_size = 0;
1067 int order_pos = 0;
1068 struct cgraph_edge *edge, last;
1070 struct cgraph_node **stack =
1071 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1073 /* We have to deal with cycles nicely, so use a depth first traversal
1074 output algorithm. Ignore the fact that some functions won't need
1075 to be output and put them into order as well, so we get dependencies
1076 right through intline functions. */
1077 for (node = cgraph_nodes; node; node = node->next)
1078 node->aux = NULL;
1079 for (node = cgraph_nodes; node; node = node->next)
1080 if (!node->aux)
1082 node2 = node;
1083 if (!node->callers)
1084 node->aux = &last;
1085 else
1086 node->aux = node->callers;
1087 while (node2)
1089 while (node2->aux != &last)
1091 edge = node2->aux;
1092 if (edge->next_caller)
1093 node2->aux = edge->next_caller;
1094 else
1095 node2->aux = &last;
1096 if (!edge->caller->aux)
1098 if (!edge->caller->callers)
1099 edge->caller->aux = &last;
1100 else
1101 edge->caller->aux = edge->caller->callers;
1102 stack[stack_size++] = node2;
1103 node2 = edge->caller;
1104 break;
1107 if (node2->aux == &last)
1109 order[order_pos++] = node2;
1110 if (stack_size)
1111 node2 = stack[--stack_size];
1112 else
1113 node2 = NULL;
1117 free (stack);
1118 return order_pos;
1121 struct searchc_env {
1122 struct cgraph_node **stack;
1123 int stack_size;
1124 struct cgraph_node **result;
1125 int order_pos;
1126 splay_tree nodes_marked_new;
1127 bool reduce;
1128 int count;
1131 struct dfs_info {
1132 int dfn_number;
1133 int low_link;
1134 bool new;
1135 bool on_stack;
1138 /* This is an implementation of Tarjan's strongly connected region
1139 finder as reprinted in Aho Hopcraft and Ullman's The Design and
1140 Analysis of Computer Programs (1975) pages 192-193. This version
1141 has been customized for cgraph_nodes. The env parameter is because
1142 it is recursive and there are no nested functions here. This
1143 function should only be called from itself or
1144 cgraph_reduced_inorder. ENV is a stack env and would be
1145 unnecessary if C had nested functions. V is the node to start
1146 searching from. */
1148 static void
1149 searchc (struct searchc_env* env, struct cgraph_node *v)
1151 struct cgraph_edge *edge;
1152 struct dfs_info *v_info = v->aux;
1154 /* mark node as old */
1155 v_info->new = false;
1156 splay_tree_remove (env->nodes_marked_new, v->uid);
1158 v_info->dfn_number = env->count;
1159 v_info->low_link = env->count;
1160 env->count++;
1161 env->stack[(env->stack_size)++] = v;
1162 v_info->on_stack = true;
1164 for (edge = v->callers; edge; edge = edge->next_caller)
1166 struct dfs_info * w_info;
1167 struct cgraph_node *w = edge->caller;
1168 /* skip the nodes that we are supposed to ignore */
1169 if (w->aux)
1171 w_info = w->aux;
1172 if (w_info->new)
1174 searchc (env, w);
1175 v_info->low_link =
1176 (v_info->low_link < w_info->low_link) ?
1177 v_info->low_link : w_info->low_link;
1179 else
1180 if ((w_info->dfn_number < v_info->dfn_number)
1181 && (w_info->on_stack))
1182 v_info->low_link =
1183 (w_info->dfn_number < v_info->low_link) ?
1184 w_info->dfn_number : v_info->low_link;
1189 if (v_info->low_link == v_info->dfn_number)
1191 struct cgraph_node *last = NULL;
1192 struct cgraph_node *x;
1193 struct dfs_info *x_info;
1194 do {
1195 x = env->stack[--(env->stack_size)];
1196 x_info = x->aux;
1197 x_info->on_stack = false;
1199 if (env->reduce)
1201 x->next_cycle = last;
1202 last = x;
1204 else
1205 env->result[env->order_pos++] = x;
1207 while (v != x);
1208 if (env->reduce)
1209 env->result[env->order_pos++] = v;
1213 /* Topsort the call graph by caller relation. Put the result in ORDER.
1215 The REDUCE flag is true if you want the cycles reduced to single
1216 nodes. Only consider nodes that have the output bit set. */
1218 static int
1219 cgraph_reduced_inorder (struct cgraph_node **order, bool reduce)
1221 struct cgraph_node *node;
1222 struct searchc_env env;
1223 splay_tree_node result;
1224 env.stack = xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1225 env.stack_size = 0;
1226 env.result = order;
1227 env.order_pos = 0;
1228 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
1229 env.count = 1;
1230 env.reduce = reduce;
1232 for (node = cgraph_nodes; node; node = node->next)
1233 if (node->output)
1235 struct dfs_info *info = xcalloc (1, sizeof (struct dfs_info));
1236 info->new = true;
1237 info->on_stack = false;
1238 node->aux = info;
1239 node->next_cycle = NULL;
1241 splay_tree_insert (env.nodes_marked_new,
1242 node->uid, (splay_tree_value)node);
1244 else
1245 node->aux = NULL;
1246 result = splay_tree_min (env.nodes_marked_new);
1247 while (result)
1249 node = (struct cgraph_node *)result->value;
1250 searchc (&env, node);
1251 result = splay_tree_min (env.nodes_marked_new);
1253 splay_tree_delete (env.nodes_marked_new);
1254 free (env.stack);
1256 for (node = cgraph_nodes; node; node = node->next)
1257 if (node->aux)
1259 free (node->aux);
1260 node->aux = NULL;
1262 return env.order_pos;
1265 /* Perform reachability analysis and reclaim all unreachable nodes.
1266 This function also remove unneeded bodies of extern inline functions
1267 and thus needs to be done only after inlining decisions has been made. */
1268 static bool
1269 cgraph_remove_unreachable_nodes (void)
1271 struct cgraph_node *first = (void *) 1;
1272 struct cgraph_node *node;
1273 bool changed = false;
1274 int insns = 0;
1276 #ifdef ENABLE_CHECKING
1277 verify_cgraph ();
1278 #endif
1279 if (cgraph_dump_file)
1280 fprintf (cgraph_dump_file, "\nReclaiming functions:");
1281 #ifdef ENABLE_CHECKING
1282 for (node = cgraph_nodes; node; node = node->next)
1283 gcc_assert (!node->aux);
1284 #endif
1285 for (node = cgraph_nodes; node; node = node->next)
1286 if (node->needed && !node->global.inlined_to
1287 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
1289 node->aux = first;
1290 first = node;
1292 else
1293 gcc_assert (!node->aux);
1295 /* Perform reachability analysis. As a special case do not consider
1296 extern inline functions not inlined as live because we won't output
1297 them at all. */
1298 while (first != (void *) 1)
1300 struct cgraph_edge *e;
1301 node = first;
1302 first = first->aux;
1304 for (e = node->callees; e; e = e->next_callee)
1305 if (!e->callee->aux
1306 && node->analyzed
1307 && (!e->inline_failed || !e->callee->analyzed
1308 || !DECL_EXTERNAL (e->callee->decl)))
1310 e->callee->aux = first;
1311 first = e->callee;
1315 /* Remove unreachable nodes. Extern inline functions need special care;
1316 Unreachable extern inline functions shall be removed.
1317 Reachable extern inline functions we never inlined shall get their bodies
1318 eliminated.
1319 Reachable extern inline functions we sometimes inlined will be turned into
1320 unanalyzed nodes so they look like for true extern functions to the rest
1321 of code. Body of such functions is released via remove_node once the
1322 inline clones are eliminated. */
1323 for (node = cgraph_nodes; node; node = node->next)
1325 if (!node->aux)
1327 int local_insns;
1328 tree decl = node->decl;
1330 node->global.inlined_to = NULL;
1331 if (DECL_STRUCT_FUNCTION (decl))
1332 local_insns = node->local.self_insns;
1333 else
1334 local_insns = 0;
1335 if (cgraph_dump_file)
1336 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1337 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
1338 cgraph_remove_node (node);
1339 else
1341 struct cgraph_edge *e;
1343 for (e = node->callers; e; e = e->next_caller)
1344 if (e->caller->aux)
1345 break;
1346 if (e || node->needed)
1348 struct cgraph_node *clone;
1350 for (clone = node->next_clone; clone;
1351 clone = clone->next_clone)
1352 if (clone->aux)
1353 break;
1354 if (!clone)
1356 DECL_SAVED_TREE (node->decl) = NULL;
1357 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1358 DECL_INITIAL (node->decl) = error_mark_node;
1360 while (node->callees)
1361 cgraph_remove_edge (node->callees);
1362 node->analyzed = false;
1364 else
1365 cgraph_remove_node (node);
1367 if (!DECL_SAVED_TREE (decl))
1368 insns += local_insns;
1369 changed = true;
1372 for (node = cgraph_nodes; node; node = node->next)
1373 node->aux = NULL;
1374 if (cgraph_dump_file)
1375 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
1376 return changed;
1379 /* Estimate size of the function after inlining WHAT into TO. */
1381 static int
1382 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
1383 struct cgraph_node *what)
1385 return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns;
1388 /* Estimate the growth caused by inlining NODE into all callees. */
1390 static int
1391 cgraph_estimate_growth (struct cgraph_node *node)
1393 int growth = 0;
1394 struct cgraph_edge *e;
1396 for (e = node->callers; e; e = e->next_caller)
1397 if (e->inline_failed)
1398 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1399 - e->caller->global.insns);
1401 /* ??? Wrong for self recursive functions or cases where we decide to not
1402 inline for different reasons, but it is not big deal as in that case
1403 we will keep the body around, but we will also avoid some inlining. */
1404 if (!node->needed && !DECL_EXTERNAL (node->decl))
1405 growth -= node->global.insns;
1407 return growth;
1410 /* E is expected to be an edge being inlined. Clone destination node of
1411 the edge and redirect it to the new clone.
1412 DUPLICATE is used for bookkeeping on whether we are actually creating new
1413 clones or re-using node originally representing out-of-line function call.
1415 void
1416 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1418 struct cgraph_node *n;
1420 /* We may eliminate the need for out-of-line copy to be output. In that
1421 case just go ahead and re-use it. */
1422 if (!e->callee->callers->next_caller
1423 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1424 && duplicate
1425 && flag_unit_at_a_time)
1427 gcc_assert (!e->callee->global.inlined_to);
1428 if (!DECL_EXTERNAL (e->callee->decl))
1429 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1430 duplicate = 0;
1432 else if (duplicate)
1434 n = cgraph_clone_node (e->callee);
1435 cgraph_redirect_edge_callee (e, n);
1438 if (e->caller->global.inlined_to)
1439 e->callee->global.inlined_to = e->caller->global.inlined_to;
1440 else
1441 e->callee->global.inlined_to = e->caller;
1443 /* Recursively clone all bodies. */
1444 for (e = e->callee->callees; e; e = e->next_callee)
1445 if (!e->inline_failed)
1446 cgraph_clone_inlined_nodes (e, duplicate);
1449 /* Mark edge E as inlined and update callgraph accordingly. */
1451 void
1452 cgraph_mark_inline_edge (struct cgraph_edge *e)
1454 int old_insns = 0, new_insns = 0;
1455 struct cgraph_node *to = NULL, *what;
1457 gcc_assert (e->inline_failed);
1458 e->inline_failed = NULL;
1460 if (!e->callee->global.inlined && flag_unit_at_a_time)
1461 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1462 e->callee->global.inlined = true;
1464 cgraph_clone_inlined_nodes (e, true);
1466 what = e->callee;
1468 /* Now update size of caller and all functions caller is inlined into. */
1469 for (;e && !e->inline_failed; e = e->caller->callers)
1471 old_insns = e->caller->global.insns;
1472 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1473 what);
1474 gcc_assert (new_insns >= 0);
1475 to = e->caller;
1476 to->global.insns = new_insns;
1478 gcc_assert (what->global.inlined_to == to);
1479 overall_insns += new_insns - old_insns;
1480 ncalls_inlined++;
1483 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1484 Return following unredirected edge in the list of callers
1485 of EDGE->CALLEE */
1487 static struct cgraph_edge *
1488 cgraph_mark_inline (struct cgraph_edge *edge)
1490 struct cgraph_node *to = edge->caller;
1491 struct cgraph_node *what = edge->callee;
1492 struct cgraph_edge *e, *next;
1493 int times = 0;
1495 /* Look for all calls, mark them inline and clone recursively
1496 all inlined functions. */
1497 for (e = what->callers; e; e = next)
1499 next = e->next_caller;
1500 if (e->caller == to && e->inline_failed)
1502 cgraph_mark_inline_edge (e);
1503 if (e == edge)
1504 edge = next;
1505 times++;
1508 gcc_assert (times);
1509 return edge;
1512 /* Return false when inlining WHAT into TO is not good idea
1513 as it would cause too large growth of function bodies. */
1515 static bool
1516 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1517 const char **reason)
1519 int times = 0;
1520 struct cgraph_edge *e;
1521 int newsize;
1522 int limit;
1524 if (to->global.inlined_to)
1525 to = to->global.inlined_to;
1527 for (e = to->callees; e; e = e->next_callee)
1528 if (e->callee == what)
1529 times++;
1531 /* When inlining large function body called once into small function,
1532 take the inlined function as base for limiting the growth. */
1533 if (to->local.self_insns > what->local.self_insns)
1534 limit = to->local.self_insns;
1535 else
1536 limit = what->local.self_insns;
1538 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1540 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1541 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1542 && newsize > limit)
1544 if (reason)
1545 *reason = N_("--param large-function-growth limit reached");
1546 return false;
1548 return true;
1551 /* Return true when function N is small enough to be inlined. */
1553 static bool
1554 cgraph_default_inline_p (struct cgraph_node *n)
1556 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1557 return false;
1558 if (DECL_DECLARED_INLINE_P (n->decl))
1559 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1560 else
1561 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1564 /* Return true when inlining WHAT would create recursive inlining.
1565 We call recursive inlining all cases where same function appears more than
1566 once in the single recursion nest path in the inline graph. */
1568 static bool
1569 cgraph_recursive_inlining_p (struct cgraph_node *to,
1570 struct cgraph_node *what,
1571 const char **reason)
1573 bool recursive;
1574 if (to->global.inlined_to)
1575 recursive = what->decl == to->global.inlined_to->decl;
1576 else
1577 recursive = what->decl == to->decl;
1578 /* Marking recursive function inline has sane semantic and thus we should
1579 not warn on it. */
1580 if (recursive && reason)
1581 *reason = (what->local.disregard_inline_limits
1582 ? N_("recursive inlining") : "");
1583 return recursive;
1586 /* Recompute heap nodes for each of callees. */
1587 static void
1588 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1589 struct cgraph_node *node)
1591 struct cgraph_edge *e;
1593 for (e = node->callees; e; e = e->next_callee)
1594 if (e->inline_failed && heap_node[e->callee->uid])
1595 fibheap_replace_key (heap, heap_node[e->callee->uid],
1596 cgraph_estimate_growth (e->callee));
1597 else if (!e->inline_failed)
1598 update_callee_keys (heap, heap_node, e->callee);
1601 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1602 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1603 int calls inlined within NODE. */
1604 static void
1605 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1606 struct cgraph_edge **first, struct cgraph_edge **last)
1608 struct cgraph_edge *e;
1609 for (e = where->callees; e; e = e->next_callee)
1610 if (e->callee == node)
1612 if (!*first)
1613 *first = e;
1614 else
1615 (*last)->aux = e;
1616 *last = e;
1618 for (e = where->callees; e; e = e->next_callee)
1619 if (!e->inline_failed)
1620 lookup_recursive_calls (node, e->callee, first, last);
1623 /* Decide on recursive inlining: in the case function has recursive calls,
1624 inline until body size reaches given argument. */
1625 static void
1626 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1628 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1629 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1630 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1631 struct cgraph_edge *last_in_current_depth;
1632 struct cgraph_edge *e;
1633 struct cgraph_node *master_clone;
1634 int depth = 0;
1635 int n = 0;
1637 if (DECL_DECLARED_INLINE_P (node->decl))
1639 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1640 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1643 /* Make sure that function is small enough to be considered for inlining. */
1644 if (!max_depth
1645 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1646 return;
1647 lookup_recursive_calls (node, node, &first_call, &last_call);
1648 if (!first_call)
1649 return;
1651 if (cgraph_dump_file)
1652 fprintf (cgraph_dump_file,
1653 "\nPerforming recursive inlining on %s\n",
1654 cgraph_node_name (node));
1656 /* We need original clone to copy around. */
1657 master_clone = cgraph_clone_node (node);
1658 master_clone->needed = true;
1659 for (e = master_clone->callees; e; e = e->next_callee)
1660 if (!e->inline_failed)
1661 cgraph_clone_inlined_nodes (e, true);
1663 /* Do the inlining and update list of recursive call during process. */
1664 last_in_current_depth = last_call;
1665 while (first_call
1666 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1668 struct cgraph_edge *curr = first_call;
1670 first_call = first_call->aux;
1671 curr->aux = NULL;
1673 cgraph_redirect_edge_callee (curr, master_clone);
1674 cgraph_mark_inline_edge (curr);
1675 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1677 if (last_in_current_depth
1678 && ++depth >= max_depth)
1679 break;
1680 n++;
1683 /* Cleanup queue pointers. */
1684 while (first_call)
1686 struct cgraph_edge *next = first_call->aux;
1687 first_call->aux = NULL;
1688 first_call = next;
1690 if (cgraph_dump_file)
1691 fprintf (cgraph_dump_file,
1692 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1693 master_clone->global.insns, node->global.insns);
1695 /* Remove master clone we used for inlining. We rely that clones inlined
1696 into master clone gets queued just before master clone so we don't
1697 need recursion. */
1698 for (node = cgraph_nodes; node != master_clone;
1699 node = node->next)
1700 if (node->global.inlined_to == master_clone)
1701 cgraph_remove_node (node);
1702 cgraph_remove_node (master_clone);
1705 /* Set inline_failed for all callers of given function to REASON. */
1707 static void
1708 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1710 struct cgraph_edge *e;
1712 if (cgraph_dump_file)
1713 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1714 for (e = node->callers; e; e = e->next_caller)
1715 if (e->inline_failed)
1716 e->inline_failed = reason;
1719 /* We use greedy algorithm for inlining of small functions:
1720 All inline candidates are put into prioritized heap based on estimated
1721 growth of the overall number of instructions and then update the estimates.
1723 INLINED and INLINED_CALEES are just pointers to arrays large enough
1724 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1726 static void
1727 cgraph_decide_inlining_of_small_functions (void)
1729 struct cgraph_node *node;
1730 fibheap_t heap = fibheap_new ();
1731 struct fibnode **heap_node =
1732 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1733 int max_insns = ((HOST_WIDEST_INT) initial_insns
1734 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1736 /* Put all inline candidates into the heap. */
1738 for (node = cgraph_nodes; node; node = node->next)
1740 if (!node->local.inlinable || !node->callers
1741 || node->local.disregard_inline_limits)
1742 continue;
1744 if (!cgraph_default_inline_p (node))
1746 cgraph_set_inline_failed (node,
1747 N_("--param max-inline-insns-single limit reached"));
1748 continue;
1750 heap_node[node->uid] =
1751 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1754 if (cgraph_dump_file)
1755 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1756 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1758 struct cgraph_edge *e, *next;
1759 int old_insns = overall_insns;
1761 heap_node[node->uid] = NULL;
1762 if (cgraph_dump_file)
1763 fprintf (cgraph_dump_file,
1764 "\nConsidering %s with %i insns\n"
1765 " Estimated growth is %+i insns.\n",
1766 cgraph_node_name (node), node->global.insns,
1767 cgraph_estimate_growth (node));
1768 if (!cgraph_default_inline_p (node))
1770 cgraph_set_inline_failed (node,
1771 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1772 continue;
1774 for (e = node->callers; e; e = next)
1776 next = e->next_caller;
1777 if (e->inline_failed)
1779 struct cgraph_node *where;
1781 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1782 &e->inline_failed)
1783 || !cgraph_check_inline_limits (e->caller, e->callee,
1784 &e->inline_failed))
1786 if (cgraph_dump_file)
1787 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1788 cgraph_node_name (e->caller), e->inline_failed);
1789 continue;
1791 next = cgraph_mark_inline (e);
1792 where = e->caller;
1793 if (where->global.inlined_to)
1794 where = where->global.inlined_to;
1796 if (heap_node[where->uid])
1797 fibheap_replace_key (heap, heap_node[where->uid],
1798 cgraph_estimate_growth (where));
1800 if (cgraph_dump_file)
1801 fprintf (cgraph_dump_file,
1802 " Inlined into %s which now has %i insns.\n",
1803 cgraph_node_name (e->caller),
1804 e->caller->global.insns);
1808 cgraph_decide_recursive_inlining (node);
1810 /* Similarly all functions called by the function we just inlined
1811 are now called more times; update keys. */
1812 update_callee_keys (heap, heap_node, node);
1814 if (cgraph_dump_file)
1815 fprintf (cgraph_dump_file,
1816 " Inlined for a net change of %+i insns.\n",
1817 overall_insns - old_insns);
1819 while ((node = fibheap_extract_min (heap)) != NULL)
1820 if (!node->local.disregard_inline_limits)
1821 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1822 fibheap_delete (heap);
1823 free (heap_node);
1826 /* Decide on the inlining. We do so in the topological order to avoid
1827 expenses on updating data structures. */
1829 static void
1830 cgraph_decide_inlining (void)
1832 struct cgraph_node *node;
1833 int nnodes;
1834 struct cgraph_node **order =
1835 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1836 int old_insns = 0;
1837 int i;
1839 for (node = cgraph_nodes; node; node = node->next)
1840 initial_insns += node->local.self_insns;
1841 overall_insns = initial_insns;
1843 nnodes = cgraph_postorder (order);
1845 if (cgraph_dump_file)
1846 fprintf (cgraph_dump_file,
1847 "\nDeciding on inlining. Starting with %i insns.\n",
1848 initial_insns);
1850 for (node = cgraph_nodes; node; node = node->next)
1851 node->aux = 0;
1853 if (cgraph_dump_file)
1854 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1856 /* In the first pass mark all always_inline edges. Do this with a priority
1857 so none of our later choices will make this impossible. */
1858 for (i = nnodes - 1; i >= 0; i--)
1860 struct cgraph_edge *e, *next;
1862 node = order[i];
1864 if (!node->local.disregard_inline_limits)
1865 continue;
1866 if (cgraph_dump_file)
1867 fprintf (cgraph_dump_file,
1868 "\nConsidering %s %i insns (always inline)\n",
1869 cgraph_node_name (node), node->global.insns);
1870 old_insns = overall_insns;
1871 for (e = node->callers; e; e = next)
1873 next = e->next_caller;
1874 if (!e->inline_failed)
1875 continue;
1876 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1877 &e->inline_failed))
1878 continue;
1879 cgraph_mark_inline_edge (e);
1880 if (cgraph_dump_file)
1881 fprintf (cgraph_dump_file,
1882 " Inlined into %s which now has %i insns.\n",
1883 cgraph_node_name (e->caller),
1884 e->caller->global.insns);
1886 if (cgraph_dump_file)
1887 fprintf (cgraph_dump_file,
1888 " Inlined for a net change of %+i insns.\n",
1889 overall_insns - old_insns);
1892 if (!flag_really_no_inline)
1894 cgraph_decide_inlining_of_small_functions ();
1896 if (cgraph_dump_file)
1897 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1899 /* And finally decide what functions are called once. */
1901 for (i = nnodes - 1; i >= 0; i--)
1903 node = order[i];
1905 if (node->callers && !node->callers->next_caller && !node->needed
1906 && node->local.inlinable && node->callers->inline_failed
1907 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1909 bool ok = true;
1910 struct cgraph_node *node1;
1912 /* Verify that we won't duplicate the caller. */
1913 for (node1 = node->callers->caller;
1914 node1->callers && !node1->callers->inline_failed
1915 && ok; node1 = node1->callers->caller)
1916 if (node1->callers->next_caller || node1->needed)
1917 ok = false;
1918 if (ok)
1920 if (cgraph_dump_file)
1921 fprintf (cgraph_dump_file,
1922 "\nConsidering %s %i insns.\n"
1923 " Called once from %s %i insns.\n",
1924 cgraph_node_name (node), node->global.insns,
1925 cgraph_node_name (node->callers->caller),
1926 node->callers->caller->global.insns);
1928 old_insns = overall_insns;
1930 if (cgraph_check_inline_limits (node->callers->caller, node,
1931 NULL))
1933 cgraph_mark_inline (node->callers);
1934 if (cgraph_dump_file)
1935 fprintf (cgraph_dump_file,
1936 " Inlined into %s which now has %i insns"
1937 " for a net change of %+i insns.\n",
1938 cgraph_node_name (node->callers->caller),
1939 node->callers->caller->global.insns,
1940 overall_insns - old_insns);
1942 else
1944 if (cgraph_dump_file)
1945 fprintf (cgraph_dump_file,
1946 " Inline limit reached, not inlined.\n");
1953 /* We will never output extern functions we didn't inline.
1954 ??? Perhaps we can prevent accounting of growth of external
1955 inline functions. */
1956 cgraph_remove_unreachable_nodes ();
1958 if (cgraph_dump_file)
1959 fprintf (cgraph_dump_file,
1960 "\nInlined %i calls, eliminated %i functions, "
1961 "%i insns turned to %i insns.\n\n",
1962 ncalls_inlined, nfunctions_inlined, initial_insns,
1963 overall_insns);
1964 free (order);
1967 /* Decide on the inlining. We do so in the topological order to avoid
1968 expenses on updating data structures. */
1970 static void
1971 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1973 struct cgraph_edge *e;
1975 /* First of all look for always inline functions. */
1976 for (e = node->callees; e; e = e->next_callee)
1977 if (e->callee->local.disregard_inline_limits
1978 && e->inline_failed
1979 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1980 /* ??? It is possible that renaming variable removed the function body
1981 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1982 && DECL_SAVED_TREE (e->callee->decl))
1983 cgraph_mark_inline (e);
1985 /* Now do the automatic inlining. */
1986 if (!flag_really_no_inline)
1987 for (e = node->callees; e; e = e->next_callee)
1988 if (e->callee->local.inlinable
1989 && e->inline_failed
1990 && !e->callee->local.disregard_inline_limits
1991 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1992 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1993 && DECL_SAVED_TREE (e->callee->decl))
1995 if (cgraph_default_inline_p (e->callee))
1996 cgraph_mark_inline (e);
1997 else
1998 e->inline_failed
1999 = N_("--param max-inline-insns-single limit reached");
2004 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
2006 bool
2007 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
2009 *reason = e->inline_failed;
2010 return !e->inline_failed;
2013 /* FIXME this needs to be enhanced. If we are compiling a single
2014 module this returns true if the variable is a module level static,
2015 but if we are doing whole program compilation, this could return
2016 true if TREE_PUBLIC is true. */
2017 /* Return true if the variable T is the right kind of static variable to
2018 perform compilation unit scope escape analysis. */
2020 static inline
2021 bool has_proper_scope_for_analysis (tree t)
2023 return (TREE_STATIC(t)) && !(TREE_PUBLIC(t)) && !(TREE_THIS_VOLATILE(t));
2026 /* Check to see if T is a read or address of operation on a static var
2027 we are interested in analyzing. FN is passed in to get access to
2028 its bit vectors. */
2030 static void
2031 check_rhs_var (struct cgraph_node *fn, tree t)
2033 if (TREE_CODE (t) == ADDR_EXPR)
2035 tree x = TREE_OPERAND (t, 0);
2036 if ((TREE_CODE (x) == VAR_DECL) && has_proper_scope_for_analysis (x))
2038 if (cgraph_dump_file)
2039 fprintf (cgraph_dump_file, "\nadding address:%s",
2040 lang_hooks.decl_printable_name (x, 2));
2042 /* FIXME -- PROFILE-RESTRUCTURE: Change the call from
2043 DECL_UID to get the uid from the var_ann field. */
2044 bitmap_set_bit (module_statics_escape, DECL_UID (x));
2047 t = get_base_address (t);
2048 if (!t) return;
2049 if ((TREE_CODE (t) == VAR_DECL) && has_proper_scope_for_analysis (t))
2051 if (cgraph_dump_file)
2052 fprintf (cgraph_dump_file, "\nadding rhs:%s",
2053 lang_hooks.decl_printable_name (t, 2));
2054 /* FIXME -- PROFILE-RESTRUCTURE: Change the call from
2055 DECL_UID to get the uid from the var_ann field. */
2056 bitmap_set_bit (fn->static_vars_info->local->statics_read_by_decl_uid,
2057 DECL_UID (t));
2061 /* Check to see if T is an assignment to a static var we are
2062 interrested in analyzing. FN is passed in to get access to its bit
2063 vectors.
2066 static void
2067 check_lhs_var (struct cgraph_node *fn, tree t)
2069 t = get_base_address (t);
2070 if (!t) return;
2071 if ((TREE_CODE (t) == VAR_DECL) && has_proper_scope_for_analysis (t))
2073 if (cgraph_dump_file)
2074 fprintf (cgraph_dump_file, "\nadding lhs:%s",
2075 lang_hooks.decl_printable_name (t, 2));
2077 /* FIXME -- PROFILE-RESTRUCTURE: Change the call from
2078 DECL_UID to get the uid from the var_ann field. */
2079 bitmap_set_bit (fn->static_vars_info->local->statics_written_by_decl_uid,
2080 DECL_UID (t));
2084 /* This is a scaled down version of get_asm_expr_operands from
2085 tree_ssa_operands.c. The version there runs much later and assumes
2086 that aliasing information is already available. Here we are just
2087 trying to find if the set of inputs and outputs contain references
2088 or address of operations to local static variables. FN is the
2089 function being analyzed and STMT is the actual asm statement. */
2091 static void
2092 get_asm_expr_operands (struct cgraph_node * fn, tree stmt)
2094 int noutputs = list_length (ASM_OUTPUTS (stmt));
2095 const char **oconstraints
2096 = (const char **) alloca ((noutputs) * sizeof (const char *));
2097 int i;
2098 tree link;
2099 const char *constraint;
2100 bool allows_mem, allows_reg, is_inout;
2102 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
2104 oconstraints[i] = constraint
2105 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2106 parse_output_constraint (&constraint, i, 0, 0,
2107 &allows_mem, &allows_reg, &is_inout);
2109 /* Memory operands are addressable. Note that STMT needs the
2110 address of this operand. */
2111 if (!allows_reg && allows_mem)
2113 check_lhs_var (fn, TREE_VALUE (link));
2117 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
2119 constraint
2120 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2121 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2122 oconstraints, &allows_mem, &allows_reg);
2124 /* Memory operands are addressable. Note that STMT needs the
2125 address of this operand. */
2126 if (!allows_reg && allows_mem)
2128 check_rhs_var (fn, TREE_VALUE (link));
2132 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
2133 if (TREE_VALUE (link) == memory_identifier)
2135 /* Abandon all hope, ye who enter here. */
2136 local_static_vars_info_t l = fn->static_vars_info->local;
2137 bitmap_a_or_b (l->statics_read_by_decl_uid,
2138 l->statics_read_by_decl_uid,
2139 all_module_statics);
2140 bitmap_a_or_b (l->statics_written_by_decl_uid,
2141 l->statics_written_by_decl_uid,
2142 all_module_statics);
2147 /* Check the parameters of a function call from CALLER to CALL_EXPR to
2148 see if any of them are static vars. Also check to see if this is
2149 either an indirect call, a call outside the compilation unit, or
2150 has special attributes that effect the clobbers. The caller
2151 parameter is the tree node for the caller and the second operand is
2152 the tree node for the entire call expression. */
2153 static void
2154 process_call_for_static_vars(struct cgraph_node * caller, tree call_expr)
2156 int flags = call_expr_flags(call_expr);
2157 tree operandList = TREE_OPERAND (call_expr, 1);
2158 tree operand;
2160 for (operand = operandList;
2161 operand != NULL_TREE;
2162 operand = TREE_CHAIN (operand))
2164 tree argument = TREE_VALUE (operand);
2165 check_rhs_var (caller, argument);
2168 /* Const and pure functions have less clobber effects than other
2169 functions so we process these first. Otherwise if it is a call
2170 outside the compilation unit or an indirect call we punt. This
2171 leaves local calls which will be processed by following the call
2172 graph. */
2173 if (flags & ECF_CONST)
2174 return;
2175 else if (flags & ECF_PURE)
2176 caller->local.calls_write_all = true;
2177 else
2179 tree callee_t = get_callee_fndecl (call_expr);
2180 if (callee_t == NULL)
2182 /* Indirect call. */
2183 caller->local.calls_read_all = true;
2184 caller->local.calls_write_all = true;
2186 else
2188 struct cgraph_node* callee = cgraph_node(callee_t);
2190 if (callee->local.external)
2192 caller->local.calls_read_all = true;
2193 caller->local.calls_write_all = true;
2199 /* FIXME -- PROFILE-RESTRUCTURE: Change to walk by explicitly walking
2200 the basic blocks rather than calling walktree. */
2202 /* Walk tree and record all calls. Called via walk_tree. FIXME When
2203 this is moved into the tree-profiling-branch, and is dealing with
2204 low GIMPLE, this routine should be changed to use tree iterators
2205 rather than being a walk_tree callback. The data is the function
2206 that is being scanned. */
2207 /* TP is the part of the tree currently under the
2208 microscope. WALK_SUBTREES is part of the walk_tree api but is
2209 unused here. DATA is cgraph_node of the function being walked. */
2211 static tree
2212 scan_for_static_refs (tree *tp,
2213 int *walk_subtrees ATTRIBUTE_UNUSED,
2214 void *data)
2216 struct cgraph_node *fn = data;
2217 tree t = *tp;
2219 switch (TREE_CODE (t))
2221 case MODIFY_EXPR:
2223 /* First look on the lhs and see what variable is stored to */
2224 tree rhs = TREE_OPERAND (t, 1);
2225 check_lhs_var (fn, TREE_OPERAND (t, 0));
2226 /* Next check the operands on the rhs to see if they are ok. */
2227 switch (TREE_CODE_CLASS (TREE_CODE (rhs))) {
2228 case tcc_binary:
2229 check_rhs_var (fn, TREE_OPERAND (rhs, 0));
2230 check_rhs_var (fn, TREE_OPERAND (rhs, 1));
2231 break;
2232 case tcc_unary:
2233 case tcc_reference:
2234 check_rhs_var (fn, TREE_OPERAND (rhs, 0));
2235 break;
2236 case tcc_declaration:
2237 check_rhs_var (fn, rhs);
2238 break;
2239 case tcc_expression:
2240 switch (TREE_CODE (rhs)) {
2241 case ADDR_EXPR:
2242 check_rhs_var (fn, rhs);
2243 break;
2244 case CALL_EXPR:
2245 process_call_for_static_vars (fn, rhs);
2246 break;
2247 default:
2248 break;
2250 break;
2251 default:
2252 break;
2255 break;
2258 case CALL_EXPR:
2259 process_call_for_static_vars (fn, t);
2260 break;
2262 case ASM_EXPR:
2263 get_asm_expr_operands (fn, t);
2264 break;
2266 default:
2267 break;
2269 return NULL;
2273 /* This is the main routine for finding the reference patterns for
2274 global variables within a function FN */
2275 static void
2276 cgraph_characterize_statics_local (struct cgraph_node *fn)
2278 tree decl = fn->decl;
2279 static_vars_info_t info = new_static_vars_info(fn, false);
2280 local_static_vars_info_t l = info->local;
2283 /* The nodes we're interested in are never shared, so walk
2284 the tree ignoring duplicates. */
2285 visited_nodes = htab_create (37, htab_hash_pointer,
2286 htab_eq_pointer, NULL);
2288 /* FIXME -- PROFILE-RESTRUCTURE: Remove creation of _decl_uid vars. */
2289 l->statics_read_by_decl_uid = BITMAP_GGC_ALLOC ();
2290 l->statics_written_by_decl_uid = BITMAP_GGC_ALLOC ();
2292 if (cgraph_dump_file)
2293 fprintf (cgraph_dump_file, "\n local analysis of %s", cgraph_node_name (fn));
2295 walk_tree (&DECL_SAVED_TREE (decl), scan_for_static_refs, fn, visited_nodes);
2296 htab_delete (visited_nodes);
2297 visited_nodes = NULL;
2300 /* Lookup the tree node for the static variable that has UID and
2301 conver the name to a string for debugging. */
2302 static const char *
2303 cgraph_get_static_name_by_uid (int index)
2305 splay_tree_node stn = splay_tree_lookup (static_vars_to_consider_by_uid, index);
2306 if (stn)
2307 return lang_hooks.decl_printable_name ((tree)(stn->value), 2);
2308 return NULL;
2311 /* Clear out any the static variable with uid INDEX from further
2312 consideration because it escapes (i.e. has had its address
2313 taken). */
2314 static void
2315 clear_static_vars_maps (int index)
2317 splay_tree_node stn = splay_tree_lookup (static_vars_to_consider_by_uid, index);
2318 if (stn)
2320 splay_tree_remove (static_vars_to_consider_by_tree, stn->value);
2321 splay_tree_remove (static_vars_to_consider_by_uid, index);
2325 /* FIXME -- PROFILE-RESTRUCTURE: Change all *_decl_uid to *_ann_uid. */
2327 /* Or in all of the bits from every callee into X, the caller's, bit
2328 vector. There are several cases to check to avoid the sparse
2329 bitmap oring. */
2330 static void
2331 cgraph_propagate_bits (struct cgraph_node *x)
2333 static_vars_info_t x_info = x->static_vars_info;
2334 global_static_vars_info_t x_global = x_info->global;
2336 struct cgraph_edge *e;
2337 for (e = x->callees; e; e = e->next_callee)
2339 struct cgraph_node *y = e->callee;
2341 /* We are only going to look at edges that point to nodes that
2342 have their output bit set. */
2343 if (y->output)
2345 static_vars_info_t y_info;
2346 global_static_vars_info_t y_global;
2347 y_info = y->static_vars_info;
2348 y_global = y_info->global;
2350 if (x_global->statics_read_by_decl_uid != all_module_statics)
2352 if (y_global->statics_read_by_decl_uid == all_module_statics)
2353 x_global->statics_read_by_decl_uid = all_module_statics;
2354 /* Skip bitmaps that are pointer equal to node's bitmap
2355 (no reason to spin within the cycle). */
2356 else if (x_global->statics_read_by_decl_uid != y_global->statics_read_by_decl_uid)
2357 bitmap_a_or_b (x_global->statics_read_by_decl_uid,
2358 x_global->statics_read_by_decl_uid,
2359 y_global->statics_read_by_decl_uid);
2362 if (x_global->statics_written_by_decl_uid != all_module_statics)
2364 if (y_global->statics_written_by_decl_uid == all_module_statics)
2365 x_global->statics_written_by_decl_uid = all_module_statics;
2366 /* Skip bitmaps that are pointer equal to node's bitmap
2367 (no reason to spin within the cycle). */
2368 else if (x_global->statics_written_by_decl_uid != y_global->statics_written_by_decl_uid)
2369 bitmap_a_or_b (x_global->statics_written_by_decl_uid,
2370 x_global->statics_written_by_decl_uid,
2371 y_global->statics_written_by_decl_uid);
2377 /* FIXME -- PROFILE-RESTRUCTURE: Change all *_decl_uid to *_ann_uid
2378 except where noted below. */
2380 /* The main routine for analyzing global static variable usage. See
2381 comments at top for description. */
2383 static void
2384 cgraph_characterize_statics (void)
2386 struct cgraph_node *node;
2387 struct cgraph_node *w;
2388 struct cgraph_node **order =
2389 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
2390 int order_pos = 0;
2391 int i;
2393 struct cgraph_varpool_node *vnode;
2394 tree global;
2396 /* get rid of the splay trees from the previous compilation unit. */
2398 static_vars_to_consider_by_tree =
2399 splay_tree_new_ggc (splay_tree_compare_pointers);
2400 static_vars_to_consider_by_uid =
2401 splay_tree_new_ggc (splay_tree_compare_ints);
2403 if (module_statics_escape)
2405 bitmap_clear (module_statics_escape);
2406 bitmap_clear (all_module_statics);
2408 else
2410 module_statics_escape = BITMAP_XMALLOC ();
2411 all_module_statics = BITMAP_GGC_ALLOC ();
2414 /* Find all of the global variables that we wish to analyze. */
2415 for (vnode = cgraph_varpool_nodes_queue; vnode; vnode = vnode->next_needed)
2417 global = vnode->decl;
2418 if ((TREE_CODE (global) == VAR_DECL) &&
2419 has_proper_scope_for_analysis (global))
2421 splay_tree_insert (static_vars_to_consider_by_tree,
2422 (splay_tree_key) global,
2423 (splay_tree_value) global);
2424 /* FIXME -- PROFILE-RESTRUCTURE: Change the call from
2425 DECL_UID to get the uid from the var_ann field. */
2426 splay_tree_insert (static_vars_to_consider_by_uid,
2427 DECL_UID (global), (splay_tree_value)global);
2429 if (cgraph_dump_file)
2430 fprintf (cgraph_dump_file, "\nConsidering global:%s",
2431 lang_hooks.decl_printable_name (global, 2));
2432 /* FIXME -- PROFILE-RESTRUCTURE: Change the call from
2433 DECL_UID to get the uid from the var_ann field. */
2434 bitmap_set_bit (all_module_statics, DECL_UID (global));
2438 order_pos = cgraph_reduced_inorder (order, false);
2439 if (cgraph_dump_file)
2440 print_order("new", order, order_pos);
2442 for (i = order_pos - 1; i >= 0; i--)
2444 node = order[i];
2446 /* Scan each function to determine the variable usage
2447 patterns. */
2448 cgraph_characterize_statics_local (node);
2451 /* Prune out the variables that were found to behave badly
2452 (i.e. have there address taken). */
2454 int index;
2455 EXECUTE_IF_SET_IN_BITMAP (module_statics_escape,
2456 0, index, clear_static_vars_maps (index));
2457 bitmap_operation (all_module_statics, all_module_statics,
2458 module_statics_escape, BITMAP_AND_COMPL);
2460 for (i = order_pos - 1; i >= 0; i--)
2462 local_static_vars_info_t l;
2463 node = order[i];
2464 l = node->static_vars_info->local;
2466 bitmap_operation (l->statics_read_by_decl_uid,
2467 l->statics_read_by_decl_uid,
2468 module_statics_escape,
2469 BITMAP_AND_COMPL);
2470 bitmap_operation (l->statics_written_by_decl_uid,
2471 l->statics_written_by_decl_uid,
2472 module_statics_escape,
2473 BITMAP_AND_COMPL);
2477 if (cgraph_dump_file)
2479 for (i = order_pos - 1; i >= 0; i--)
2481 int index;
2482 local_static_vars_info_t l;
2483 node = order[i];
2484 l = node->static_vars_info->local;
2485 fprintf (cgraph_dump_file,
2486 "\nFunction name:%s/%i:",
2487 cgraph_node_name (node), node->uid);
2488 fprintf (cgraph_dump_file, "\n locals read: ");
2489 EXECUTE_IF_SET_IN_BITMAP (l->statics_read_by_decl_uid,
2490 0, index,
2491 fprintf (cgraph_dump_file, "%s ",
2492 cgraph_get_static_name_by_uid (index)));
2493 fprintf (cgraph_dump_file, "\n locals written: ");
2494 EXECUTE_IF_SET_IN_BITMAP (l->statics_written_by_decl_uid,
2495 0, index,
2496 fprintf(cgraph_dump_file, "%s ",
2497 cgraph_get_static_name_by_uid (index)));
2501 /* Propagate the local information thru the call graph to produce
2502 the global information. All the nodes within a cycle will have
2503 the same info so we collapse cycles first. Then we can do the
2504 propagation in one pass from the leaves to the roots. */
2505 order_pos = cgraph_reduced_inorder (order, true);
2506 for (i = order_pos - 1; i >= 0; i--)
2508 static_vars_info_t node_info;
2509 global_static_vars_info_t node_g =
2510 ggc_calloc (1, sizeof (struct global_static_vars_info_d));
2511 local_static_vars_info_t node_l;
2514 bool read_all;
2515 bool write_all;
2517 node = order[i];
2518 node_info = node->static_vars_info;
2519 node_info->global = node_g;
2520 node_l = node_info->local;
2522 read_all = node->local.calls_read_all;
2523 write_all = node->local.calls_write_all;
2525 /* If any node in a cycle is calls_read_all or calls_write_all
2526 they all are. */
2527 w = node->next_cycle;
2528 while (w)
2530 read_all |= w->local.calls_read_all;
2531 write_all |= w->local.calls_write_all;
2532 w = w->next_cycle;
2535 /* Initialized the bitmaps for the reduced nodes */
2536 if (read_all)
2537 node_g->statics_read_by_decl_uid = all_module_statics;
2538 else
2540 node_g->statics_read_by_decl_uid = BITMAP_GGC_ALLOC ();
2541 bitmap_copy (node_g->statics_read_by_decl_uid,
2542 node_l->statics_read_by_decl_uid);
2545 if (write_all)
2546 node_g->statics_written_by_decl_uid = all_module_statics;
2547 else
2549 node_g->statics_written_by_decl_uid = BITMAP_GGC_ALLOC ();
2550 bitmap_copy (node_g->statics_written_by_decl_uid,
2551 node_l->statics_written_by_decl_uid);
2554 w = node->next_cycle;
2555 while (w)
2557 /* All nodes within a cycle share the same global info bitmaps. */
2558 static_vars_info_t w_info = w->static_vars_info;
2559 local_static_vars_info_t w_l;
2561 w_info->global = node_g;
2562 w_l = w_info->local;
2564 /* These global bitmaps are initialized from the local info
2565 of all of the nodes in the region. However there is no
2566 need to do any work if the bitmaps were set to
2567 all_module_statics. */
2568 if (!read_all)
2569 bitmap_a_or_b (node_g->statics_read_by_decl_uid,
2570 node_g->statics_read_by_decl_uid,
2571 w_l->statics_read_by_decl_uid);
2572 if (!write_all)
2573 bitmap_a_or_b (node_g->statics_written_by_decl_uid,
2574 node_g->statics_written_by_decl_uid,
2575 w_l->statics_written_by_decl_uid);
2576 w = w->next_cycle;
2579 cgraph_propagate_bits (node);
2581 w = node->next_cycle;
2582 while (w)
2584 cgraph_propagate_bits (w);
2585 w = w->next_cycle;
2589 if (cgraph_dump_file)
2591 for (i = order_pos - 1; i >= 0; i--)
2593 static_vars_info_t node_info;
2594 global_static_vars_info_t node_g;
2595 int index;
2596 node = order[i];
2597 node_info = node->static_vars_info;
2598 node_g = node_info->global;
2599 fprintf (cgraph_dump_file,
2600 "\nFunction name:%s/%i:",
2601 cgraph_node_name (node), node->uid);
2602 w = node->next_cycle;
2603 while (w)
2605 fprintf (cgraph_dump_file, "\n next cycle: %s/%i ",
2606 cgraph_node_name (w), w->uid);
2607 w = w->next_cycle;
2609 fprintf (cgraph_dump_file, "\n globals read: ");
2610 EXECUTE_IF_SET_IN_BITMAP (node_g->statics_read_by_decl_uid,
2611 0, index,
2612 fprintf (cgraph_dump_file, "%s ",
2613 cgraph_get_static_name_by_uid (index)));
2614 fprintf (cgraph_dump_file, "\n globals written: ");
2615 EXECUTE_IF_SET_IN_BITMAP (node_g->statics_written_by_decl_uid,
2616 0, index,
2617 fprintf (cgraph_dump_file, "%s ",
2618 cgraph_get_static_name_by_uid (index)));
2622 /* Cleanup. */
2623 for (i = order_pos - 1; i >= 0; i--)
2625 static_vars_info_t node_info;
2626 global_static_vars_info_t node_g;
2627 node = order[i];
2628 node_info = node->static_vars_info;
2629 node_g = node_info->global;
2631 node_g->var_anns_valid = false;
2633 /* Create the complimentary sets. These are more useful for
2634 certain apis. */
2635 node_g->statics_not_read_by_decl_uid = BITMAP_GGC_ALLOC ();
2636 node_g->statics_not_written_by_decl_uid = BITMAP_GGC_ALLOC ();
2638 /* FIXME -- PROFILE-RESTRUCTURE: Delete next 4 assignments. */
2639 node_g->statics_read_by_ann_uid = BITMAP_GGC_ALLOC ();
2640 node_g->statics_written_by_ann_uid = BITMAP_GGC_ALLOC ();
2641 node_g->statics_not_read_by_ann_uid = BITMAP_GGC_ALLOC ();
2642 node_g->statics_not_written_by_ann_uid = BITMAP_GGC_ALLOC ();
2644 if (node_g->statics_read_by_decl_uid != all_module_statics)
2646 bitmap_operation (node_g->statics_not_read_by_decl_uid,
2647 all_module_statics,
2648 node_g->statics_read_by_decl_uid,
2649 BITMAP_AND_COMPL);
2652 if (node_g->statics_written_by_decl_uid != all_module_statics)
2653 bitmap_operation (node_g->statics_not_written_by_decl_uid,
2654 all_module_statics,
2655 node_g->statics_written_by_decl_uid,
2656 BITMAP_AND_COMPL);
2658 w = node->next_cycle;
2660 while (w)
2662 struct cgraph_node * last = w;
2663 w = w->next_cycle;
2664 last->next_cycle = NULL;
2668 free (order);
2671 /* Expand all functions that must be output.
2673 Attempt to topologically sort the nodes so function is output when
2674 all called functions are already assembled to allow data to be
2675 propagated across the callgraph. Use a stack to get smaller distance
2676 between a function and its callees (later we may choose to use a more
2677 sophisticated algorithm for function reordering; we will likely want
2678 to use subsections to make the output functions appear in top-down
2679 order). */
2681 static void
2682 cgraph_expand_all_functions (void)
2684 struct cgraph_node *node;
2685 struct cgraph_node **order =
2686 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
2687 int order_pos = 0, new_order_pos = 0;
2688 int i;
2690 order_pos = cgraph_postorder (order);
2691 gcc_assert (order_pos == cgraph_n_nodes);
2693 /* Garbage collector may remove inline clones we eliminate during
2694 optimization. So we must be sure to not reference them. */
2695 for (i = 0; i < order_pos; i++)
2696 if (order[i]->output)
2697 order[new_order_pos++] = order[i];
2699 for (i = new_order_pos - 1; i >= 0; i--)
2701 node = order[i];
2702 if (node->output)
2704 gcc_assert (node->reachable);
2705 node->output = 0;
2706 cgraph_expand_function (node);
2709 free (order);
2712 /* Mark all local and external functions.
2714 A local function is one whose calls can occur only in the current
2715 compilation unit and all its calls are explicit, so we can change
2716 its calling convention. We simply mark all static functions whose
2717 address is not taken as local.
2719 An external function is one whose body is outside the current
2720 compilation unit. */
2722 static void
2723 cgraph_mark_local_and_external_functions (void)
2725 struct cgraph_node *node;
2727 /* Figure out functions we want to assemble. */
2728 for (node = cgraph_nodes; node; node = node->next)
2730 node->local.local = (!node->needed
2731 && DECL_SAVED_TREE (node->decl)
2732 && !TREE_PUBLIC (node->decl));
2733 node->local.external = (!DECL_SAVED_TREE (node->decl)
2734 && TREE_PUBLIC (node->decl));
2737 if (cgraph_dump_file)
2739 fprintf (cgraph_dump_file, "\nMarking local functions:");
2740 for (node = cgraph_nodes; node; node = node->next)
2741 if (node->local.local)
2742 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
2743 fprintf (cgraph_dump_file, "\n\n");
2745 fprintf (cgraph_dump_file, "\nMarking external functions:");
2746 for (node = cgraph_nodes; node; node = node->next)
2747 if (node->local.external)
2748 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
2749 fprintf (cgraph_dump_file, "\n\n");
2753 /* Return true when function body of DECL still needs to be kept around
2754 for later re-use. */
2755 bool
2756 cgraph_preserve_function_body_p (tree decl)
2758 struct cgraph_node *node;
2759 /* Keep the body; we're going to dump it. */
2760 if (dump_enabled_p (TDI_tree_all))
2761 return true;
2762 if (!cgraph_global_info_ready)
2763 return (DECL_INLINE (decl) && !flag_really_no_inline);
2764 /* Look if there is any clone around. */
2765 for (node = cgraph_node (decl); node; node = node->next_clone)
2766 if (node->global.inlined_to)
2767 return true;
2768 return false;
2771 /* Perform simple optimizations based on callgraph. */
2773 void
2774 cgraph_optimize (void)
2776 #ifdef ENABLE_CHECKING
2777 verify_cgraph ();
2778 #endif
2779 if (!flag_unit_at_a_time)
2780 return;
2781 timevar_push (TV_CGRAPHOPT);
2782 if (!quiet_flag)
2783 fprintf (stderr, "Performing intraprocedural optimizations\n");
2785 cgraph_mark_local_and_external_functions ();
2786 if (cgraph_dump_file)
2788 fprintf (cgraph_dump_file, "Marked ");
2789 dump_cgraph (cgraph_dump_file);
2792 if (flag_inline_trees)
2793 cgraph_decide_inlining ();
2794 cgraph_global_info_ready = true;
2795 if (cgraph_dump_file)
2797 fprintf (cgraph_dump_file, "Optimized ");
2798 dump_cgraph (cgraph_dump_file);
2800 timevar_pop (TV_CGRAPHOPT);
2802 /* Output everything. */
2803 if (!quiet_flag)
2804 fprintf (stderr, "Assembling functions:\n");
2805 #ifdef ENABLE_CHECKING
2806 verify_cgraph ();
2807 #endif
2809 /* This call was moved here from cgraph_expand_all_functions so that
2810 cgraph_characterize_statics could use the output flag of the cgraph
2811 node. */
2813 cgraph_mark_functions_to_output ();
2815 cgraph_characterize_statics ();
2817 cgraph_expand_all_functions ();
2818 if (cgraph_dump_file)
2820 fprintf (cgraph_dump_file, "\nFinal ");
2821 dump_cgraph (cgraph_dump_file);
2823 #ifdef ENABLE_CHECKING
2824 verify_cgraph ();
2825 /* Double check that all inline clones are gone and that all
2826 function bodies have been released from memory. */
2827 if (flag_unit_at_a_time
2828 && !dump_enabled_p (TDI_tree_all)
2829 && !(sorrycount || errorcount))
2831 struct cgraph_node *node;
2832 bool error_found = false;
2834 for (node = cgraph_nodes; node; node = node->next)
2835 if (node->analyzed
2836 && (node->global.inlined_to
2837 || DECL_SAVED_TREE (node->decl)))
2839 error_found = true;
2840 dump_cgraph_node (stderr, node);
2842 if (error_found)
2843 internal_error ("Nodes with no released memory found.");
2845 #endif
2848 /* Generate and emit a static constructor or destructor. WHICH must be
2849 one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing
2850 GENERIC statements. */
2852 void
2853 cgraph_build_static_cdtor (char which, tree body, int priority)
2855 static int counter = 0;
2856 char which_buf[16];
2857 tree decl, name, resdecl;
2859 sprintf (which_buf, "%c_%d", which, counter++);
2860 name = get_file_function_name_long (which_buf);
2862 decl = build_decl (FUNCTION_DECL, name,
2863 build_function_type (void_type_node, void_list_node));
2864 current_function_decl = decl;
2866 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
2867 DECL_ARTIFICIAL (resdecl) = 1;
2868 DECL_IGNORED_P (resdecl) = 1;
2869 DECL_RESULT (decl) = resdecl;
2871 allocate_struct_function (decl);
2873 TREE_STATIC (decl) = 1;
2874 TREE_USED (decl) = 1;
2875 DECL_ARTIFICIAL (decl) = 1;
2876 DECL_IGNORED_P (decl) = 1;
2877 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2878 DECL_SAVED_TREE (decl) = body;
2879 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
2880 DECL_UNINLINABLE (decl) = 1;
2882 DECL_INITIAL (decl) = make_node (BLOCK);
2883 TREE_USED (DECL_INITIAL (decl)) = 1;
2885 DECL_SOURCE_LOCATION (decl) = input_location;
2886 cfun->function_end_locus = input_location;
2888 switch (which)
2890 case 'I':
2891 DECL_STATIC_CONSTRUCTOR (decl) = 1;
2892 break;
2893 case 'D':
2894 DECL_STATIC_DESTRUCTOR (decl) = 1;
2895 break;
2896 default:
2897 gcc_unreachable ();
2900 gimplify_function_tree (decl);
2902 /* ??? We will get called LATE in the compilation process. */
2903 if (cgraph_global_info_ready)
2904 tree_rest_of_compilation (decl, false);
2905 else
2906 cgraph_finalize_function (decl, 0);
2908 if (targetm.have_ctors_dtors)
2910 void (*fn) (rtx, int);
2912 if (which == 'I')
2913 fn = targetm.asm_out.constructor;
2914 else
2915 fn = targetm.asm_out.destructor;
2916 fn (XEXP (DECL_RTL (decl), 0), priority);
2920 void
2921 init_cgraph (void)
2923 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
2924 memory_identifier = get_identifier("memory");
2926 #include "gt-cgraphunit.h"