OSDN Git Service

2011-04-29 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.h"
30 #include "cgraph.h"
31 #include "intl.h"
32 #include "gimple.h"
33 #include "tree-pass.h"
34 #include "ipa-utils.h"
35 #include "except.h"
36 #include "ipa-inline.h"
37
38 /* Context of record_reference.  */
39 struct record_reference_ctx
40 {
41   bool only_vars;
42   struct varpool_node *varpool_node;
43 };
44
45 /* Walk tree and record all calls and references to functions/variables.
46    Called via walk_tree: TP is pointer to tree to be examined.
47    When DATA is non-null, record references to callgraph.
48    */
49
50 static tree
51 record_reference (tree *tp, int *walk_subtrees, void *data)
52 {
53   tree t = *tp;
54   tree decl;
55   struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
56
57   t = canonicalize_constructor_val (t);
58   if (!t)
59     t = *tp;
60   else if (t != *tp)
61     *tp = t;
62
63   switch (TREE_CODE (t))
64     {
65     case VAR_DECL:
66     case FUNCTION_DECL:
67       gcc_unreachable ();
68       break;
69
70     case FDESC_EXPR:
71     case ADDR_EXPR:
72       /* Record dereferences to the functions.  This makes the
73          functions reachable unconditionally.  */
74       decl = get_base_var (*tp);
75       if (TREE_CODE (decl) == FUNCTION_DECL)
76         {
77           struct cgraph_node *node = cgraph_get_create_node (decl);
78           if (!ctx->only_vars)
79             cgraph_mark_address_taken_node (node);
80           ipa_record_reference (NULL, ctx->varpool_node, node, NULL,
81                                 IPA_REF_ADDR, NULL);
82         }
83
84       if (TREE_CODE (decl) == VAR_DECL)
85         {
86           struct varpool_node *vnode = varpool_node (decl);
87           if (lang_hooks.callgraph.analyze_expr)
88             lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
89           varpool_mark_needed_node (vnode);
90           if (vnode->alias && vnode->extra_name)
91             vnode = vnode->extra_name;
92           ipa_record_reference (NULL, ctx->varpool_node,
93                                 NULL, vnode,
94                                 IPA_REF_ADDR, NULL);
95         }
96       *walk_subtrees = 0;
97       break;
98
99     default:
100       /* Save some cycles by not walking types and declaration as we
101          won't find anything useful there anyway.  */
102       if (IS_TYPE_OR_DECL_P (*tp))
103         {
104           *walk_subtrees = 0;
105           break;
106         }
107
108       if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
109         return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
110       break;
111     }
112
113   return NULL_TREE;
114 }
115
116 /* Record references to typeinfos in the type list LIST.  */
117
118 static void
119 record_type_list (struct cgraph_node *node, tree list)
120 {
121   for (; list; list = TREE_CHAIN (list))
122     {
123       tree type = TREE_VALUE (list);
124       
125       if (TYPE_P (type))
126         type = lookup_type_for_runtime (type);
127       STRIP_NOPS (type);
128       if (TREE_CODE (type) == ADDR_EXPR)
129         {
130           type = TREE_OPERAND (type, 0);
131           if (TREE_CODE (type) == VAR_DECL)
132             {
133               struct varpool_node *vnode = varpool_node (type);
134               varpool_mark_needed_node (vnode);
135               ipa_record_reference (node, NULL,
136                                     NULL, vnode,
137                                     IPA_REF_ADDR, NULL);
138             }
139         }
140     }
141 }
142
143 /* Record all references we will introduce by producing EH tables
144    for NODE.  */
145
146 static void
147 record_eh_tables (struct cgraph_node *node, struct function *fun)
148 {
149   eh_region i;
150
151   if (DECL_FUNCTION_PERSONALITY (node->decl))
152     ipa_record_reference (node, NULL,
153                cgraph_get_create_node (DECL_FUNCTION_PERSONALITY (node->decl)),
154                NULL, IPA_REF_ADDR, NULL);
155
156   i = fun->eh->region_tree;
157   if (!i)
158     return;
159
160   while (1)
161     {
162       switch (i->type)
163         {
164         case ERT_CLEANUP:
165         case ERT_MUST_NOT_THROW:
166           break;
167
168         case ERT_TRY:
169           {
170             eh_catch c;
171             for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
172               record_type_list (node, c->type_list);
173           }
174           break;
175
176         case ERT_ALLOWED_EXCEPTIONS:
177           record_type_list (node, i->u.allowed.type_list);
178           break;
179         }
180       /* If there are sub-regions, process them.  */
181       if (i->inner)
182         i = i->inner;
183       /* If there are peers, process them.  */
184       else if (i->next_peer)
185         i = i->next_peer;
186       /* Otherwise, step back up the tree to the next peer.  */
187       else
188         {
189           do
190             {
191               i = i->outer;
192               if (i == NULL)
193                 return;
194             }
195           while (i->next_peer == NULL);
196           i = i->next_peer;
197         }
198     }
199 }
200
201 /* Reset inlining information of all incoming call edges of NODE.  */
202
203 void
204 reset_inline_failed (struct cgraph_node *node)
205 {
206   struct cgraph_edge *e;
207
208   for (e = node->callers; e; e = e->next_caller)
209     {
210       e->callee->global.inlined_to = NULL;
211       initialize_inline_failed (e);
212     }
213 }
214
215 /* Computes the frequency of the call statement so that it can be stored in
216    cgraph_edge.  BB is the basic block of the call statement.  */
217 int
218 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
219 {
220   int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
221                      (DECL_STRUCT_FUNCTION (decl))->frequency;
222   int freq = bb->frequency;
223
224   if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
225     return CGRAPH_FREQ_BASE;
226
227   if (!entry_freq)
228     entry_freq = 1, freq++;
229
230   freq = freq * CGRAPH_FREQ_BASE / entry_freq;
231   if (freq > CGRAPH_FREQ_MAX)
232     freq = CGRAPH_FREQ_MAX;
233
234   return freq;
235 }
236
237 /* Mark address taken in STMT.  */
238
239 static bool
240 mark_address (gimple stmt, tree addr, void *data)
241 {
242   addr = get_base_address (addr);
243   if (TREE_CODE (addr) == FUNCTION_DECL)
244     {
245       struct cgraph_node *node = cgraph_get_create_node (addr);
246       cgraph_mark_address_taken_node (node);
247       ipa_record_reference ((struct cgraph_node *)data, NULL,
248                             node, NULL,
249                             IPA_REF_ADDR, stmt);
250     }
251   else if (addr && TREE_CODE (addr) == VAR_DECL
252            && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
253     {
254       struct varpool_node *vnode = varpool_node (addr);
255       int walk_subtrees;
256
257       if (lang_hooks.callgraph.analyze_expr)
258         lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
259       varpool_mark_needed_node (vnode);
260       if (vnode->alias && vnode->extra_name)
261         vnode = vnode->extra_name;
262       ipa_record_reference ((struct cgraph_node *)data, NULL,
263                             NULL, vnode,
264                             IPA_REF_ADDR, stmt);
265     }
266
267   return false;
268 }
269
270 /* Mark load of T.  */
271
272 static bool
273 mark_load (gimple stmt, tree t, void *data)
274 {
275   t = get_base_address (t);
276   if (t && TREE_CODE (t) == FUNCTION_DECL)
277     {
278       /* ??? This can happen on platforms with descriptors when these are
279          directly manipulated in the code.  Pretend that it's an address.  */
280       struct cgraph_node *node = cgraph_get_create_node (t);
281       cgraph_mark_address_taken_node (node);
282       ipa_record_reference ((struct cgraph_node *)data, NULL,
283                             node, NULL,
284                             IPA_REF_ADDR, stmt);
285     }
286   else if (t && TREE_CODE (t) == VAR_DECL
287            && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
288     {
289       struct varpool_node *vnode = varpool_node (t);
290       int walk_subtrees;
291
292       if (lang_hooks.callgraph.analyze_expr)
293         lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
294       varpool_mark_needed_node (vnode);
295       if (vnode->alias && vnode->extra_name)
296         vnode = vnode->extra_name;
297       ipa_record_reference ((struct cgraph_node *)data, NULL,
298                             NULL, vnode,
299                             IPA_REF_LOAD, stmt);
300     }
301   return false;
302 }
303
304 /* Mark store of T.  */
305
306 static bool
307 mark_store (gimple stmt, tree t, void *data)
308 {
309   t = get_base_address (t);
310   if (t && TREE_CODE (t) == VAR_DECL
311       && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
312     {
313       struct varpool_node *vnode = varpool_node (t);
314       int walk_subtrees;
315
316       if (lang_hooks.callgraph.analyze_expr)
317         lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
318       varpool_mark_needed_node (vnode);
319       if (vnode->alias && vnode->extra_name)
320         vnode = vnode->extra_name;
321       ipa_record_reference ((struct cgraph_node *)data, NULL,
322                             NULL, vnode,
323                             IPA_REF_STORE, stmt);
324      }
325   return false;
326 }
327
328 /* Create cgraph edges for function calls.
329    Also look for functions and variables having addresses taken.  */
330
331 static unsigned int
332 build_cgraph_edges (void)
333 {
334   basic_block bb;
335   struct cgraph_node *node = cgraph_get_node (current_function_decl);
336   struct pointer_set_t *visited_nodes = pointer_set_create ();
337   gimple_stmt_iterator gsi;
338   tree decl;
339   unsigned ix;
340
341   /* Create the callgraph edges and record the nodes referenced by the function.
342      body.  */
343   FOR_EACH_BB (bb)
344     {
345       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
346         {
347           gimple stmt = gsi_stmt (gsi);
348           tree decl;
349
350           if (is_gimple_call (stmt))
351             {
352               int freq = compute_call_stmt_bb_frequency (current_function_decl,
353                                                          bb);
354               decl = gimple_call_fndecl (stmt);
355               if (decl)
356                 cgraph_create_edge (node, cgraph_get_create_node (decl),
357                                     stmt, bb->count, freq);
358               else
359                 cgraph_create_indirect_edge (node, stmt,
360                                              gimple_call_flags (stmt),
361                                              bb->count, freq);
362             }
363           walk_stmt_load_store_addr_ops (stmt, node, mark_load,
364                                          mark_store, mark_address);
365           if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
366               && gimple_omp_parallel_child_fn (stmt))
367             {
368               tree fn = gimple_omp_parallel_child_fn (stmt);
369               ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
370                                     NULL, IPA_REF_ADDR, stmt);
371             }
372           if (gimple_code (stmt) == GIMPLE_OMP_TASK)
373             {
374               tree fn = gimple_omp_task_child_fn (stmt);
375               if (fn)
376                 ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
377                                       NULL, IPA_REF_ADDR, stmt);
378               fn = gimple_omp_task_copy_fn (stmt);
379               if (fn)
380                 ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
381                                       NULL, IPA_REF_ADDR, stmt);
382             }
383         }
384       for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
385         walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
386                                        mark_load, mark_store, mark_address);
387    }
388
389   /* Look for initializers of constant variables and private statics.  */
390   FOR_EACH_LOCAL_DECL (cfun, ix, decl)
391     if (TREE_CODE (decl) == VAR_DECL
392         && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
393       varpool_finalize_decl (decl);
394   record_eh_tables (node, cfun);
395
396   pointer_set_destroy (visited_nodes);
397   return 0;
398 }
399
400 struct gimple_opt_pass pass_build_cgraph_edges =
401 {
402  {
403   GIMPLE_PASS,
404   "*build_cgraph_edges",                        /* name */
405   NULL,                                 /* gate */
406   build_cgraph_edges,                   /* execute */
407   NULL,                                 /* sub */
408   NULL,                                 /* next */
409   0,                                    /* static_pass_number */
410   TV_NONE,                              /* tv_id */
411   PROP_cfg,                             /* properties_required */
412   0,                                    /* properties_provided */
413   0,                                    /* properties_destroyed */
414   0,                                    /* todo_flags_start */
415   0                                     /* todo_flags_finish */
416  }
417 };
418
419 /* Record references to functions and other variables present in the
420    initial value of DECL, a variable.
421    When ONLY_VARS is true, we mark needed only variables, not functions.  */
422
423 void
424 record_references_in_initializer (tree decl, bool only_vars)
425 {
426   struct pointer_set_t *visited_nodes = pointer_set_create ();
427   struct varpool_node *node = varpool_node (decl);
428   struct record_reference_ctx ctx = {false, NULL};
429
430   ctx.varpool_node = node;
431   ctx.only_vars = only_vars;
432   walk_tree (&DECL_INITIAL (decl), record_reference,
433              &ctx, visited_nodes);
434   pointer_set_destroy (visited_nodes);
435 }
436
437 /* Rebuild cgraph edges for current function node.  This needs to be run after
438    passes that don't update the cgraph.  */
439
440 unsigned int
441 rebuild_cgraph_edges (void)
442 {
443   basic_block bb;
444   struct cgraph_node *node = cgraph_get_node (current_function_decl);
445   gimple_stmt_iterator gsi;
446
447   cgraph_node_remove_callees (node);
448   ipa_remove_all_references (&node->ref_list);
449
450   node->count = ENTRY_BLOCK_PTR->count;
451
452   FOR_EACH_BB (bb)
453     {
454       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
455         {
456           gimple stmt = gsi_stmt (gsi);
457           tree decl;
458
459           if (is_gimple_call (stmt))
460             {
461               int freq = compute_call_stmt_bb_frequency (current_function_decl,
462                                                          bb);
463               decl = gimple_call_fndecl (stmt);
464               if (decl)
465                 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
466                                     bb->count, freq);
467               else
468                 cgraph_create_indirect_edge (node, stmt,
469                                              gimple_call_flags (stmt),
470                                              bb->count, freq);
471             }
472           walk_stmt_load_store_addr_ops (stmt, node, mark_load,
473                                          mark_store, mark_address);
474
475         }
476       for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
477         walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
478                                        mark_load, mark_store, mark_address);
479     }
480   record_eh_tables (node, cfun);
481   gcc_assert (!node->global.inlined_to);
482
483   return 0;
484 }
485
486 /* Rebuild cgraph edges for current function node.  This needs to be run after
487    passes that don't update the cgraph.  */
488
489 void
490 cgraph_rebuild_references (void)
491 {
492   basic_block bb;
493   struct cgraph_node *node = cgraph_get_node (current_function_decl);
494   gimple_stmt_iterator gsi;
495
496   ipa_remove_all_references (&node->ref_list);
497
498   node->count = ENTRY_BLOCK_PTR->count;
499
500   FOR_EACH_BB (bb)
501     {
502       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
503         {
504           gimple stmt = gsi_stmt (gsi);
505
506           walk_stmt_load_store_addr_ops (stmt, node, mark_load,
507                                          mark_store, mark_address);
508
509         }
510       for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
511         walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
512                                        mark_load, mark_store, mark_address);
513     }
514   record_eh_tables (node, cfun);
515 }
516
517 struct gimple_opt_pass pass_rebuild_cgraph_edges =
518 {
519  {
520   GIMPLE_PASS,
521   "*rebuild_cgraph_edges",              /* name */
522   NULL,                                 /* gate */
523   rebuild_cgraph_edges,                 /* execute */
524   NULL,                                 /* sub */
525   NULL,                                 /* next */
526   0,                                    /* static_pass_number */
527   TV_CGRAPH,                            /* tv_id */
528   PROP_cfg,                             /* properties_required */
529   0,                                    /* properties_provided */
530   0,                                    /* properties_destroyed */
531   0,                                    /* todo_flags_start */
532   0,                                    /* todo_flags_finish */
533  }
534 };
535
536
537 static unsigned int
538 remove_cgraph_callee_edges (void)
539 {
540   cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
541   return 0;
542 }
543
544 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
545 {
546  {
547   GIMPLE_PASS,
548   "*remove_cgraph_callee_edges",                /* name */
549   NULL,                                 /* gate */
550   remove_cgraph_callee_edges,           /* execute */
551   NULL,                                 /* sub */
552   NULL,                                 /* next */
553   0,                                    /* static_pass_number */
554   TV_NONE,                              /* tv_id */
555   0,                                    /* properties_required */
556   0,                                    /* properties_provided */
557   0,                                    /* properties_destroyed */
558   0,                                    /* todo_flags_start */
559   0,                                    /* todo_flags_finish */
560  }
561 };