OSDN Git Service

* cgraphbuild.c (record_reference): Use cgraph_mark_address_taken_node.
[pf3gnuchains/gcc-fork.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
35 /* Walk tree and record all calls and references to functions/variables.
36    Called via walk_tree: TP is pointer to tree to be examined.  */
37
38 static tree
39 record_reference (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
40 {
41   tree t = *tp;
42   tree decl;
43
44   switch (TREE_CODE (t))
45     {
46     case VAR_DECL:
47       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
48         {
49           varpool_mark_needed_node (varpool_node (t));
50           if (lang_hooks.callgraph.analyze_expr)
51             return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
52         }
53       break;
54
55     case FDESC_EXPR:
56     case ADDR_EXPR:
57       /* Record dereferences to the functions.  This makes the
58          functions reachable unconditionally.  */
59       decl = TREE_OPERAND (*tp, 0);
60       if (TREE_CODE (decl) == FUNCTION_DECL)
61         cgraph_mark_address_taken_node (cgraph_node (decl));
62       break;
63
64     default:
65       /* Save some cycles by not walking types and declaration as we
66          won't find anything useful there anyway.  */
67       if (IS_TYPE_OR_DECL_P (*tp))
68         {
69           *walk_subtrees = 0;
70           break;
71         }
72
73       if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
74         return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
75       break;
76     }
77
78   return NULL_TREE;
79 }
80
81 /* Computes the frequency of the call statement so that it can be stored in
82    cgraph_edge.  BB is the basic block of the call statement.  */
83 int
84 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
85 {
86   int entry_freq = ENTRY_BLOCK_PTR->frequency;
87   int freq = bb->frequency;
88
89   if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
90     return CGRAPH_FREQ_BASE;
91
92   if (!entry_freq)
93     entry_freq = 1, freq++;
94
95   freq = freq * CGRAPH_FREQ_BASE / entry_freq;
96   if (freq > CGRAPH_FREQ_MAX)
97     freq = CGRAPH_FREQ_MAX;
98
99   return freq;
100 }
101
102 /* Create cgraph edges for function calls.
103    Also look for functions and variables having addresses taken.  */
104
105 static unsigned int
106 build_cgraph_edges (void)
107 {
108   basic_block bb;
109   struct cgraph_node *node = cgraph_node (current_function_decl);
110   struct pointer_set_t *visited_nodes = pointer_set_create ();
111   gimple_stmt_iterator gsi;
112   tree step;
113
114   /* Create the callgraph edges and record the nodes referenced by the function.
115      body.  */
116   FOR_EACH_BB (bb)
117     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
118       {
119         gimple stmt = gsi_stmt (gsi);
120         tree decl;
121
122         if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
123           {
124             size_t i;
125             size_t n = gimple_call_num_args (stmt);
126             cgraph_create_edge (node, cgraph_node (decl), stmt,
127                                 bb->count, compute_call_stmt_bb_frequency (current_function_decl, bb),
128                                 bb->loop_depth);
129             for (i = 0; i < n; i++)
130               walk_tree (gimple_call_arg_ptr (stmt, i), record_reference,
131                          node, visited_nodes);
132             if (gimple_call_lhs (stmt))
133               walk_tree (gimple_call_lhs_ptr (stmt), record_reference, node,
134                          visited_nodes);
135           }
136         else
137           {
138             struct walk_stmt_info wi;
139             memset (&wi, 0, sizeof (wi));
140             wi.info = node;
141             wi.pset = visited_nodes;
142             walk_gimple_op (stmt, record_reference, &wi);
143             if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
144                 && gimple_omp_parallel_child_fn (stmt))
145               {
146                 tree fn = gimple_omp_parallel_child_fn (stmt);
147                 cgraph_mark_needed_node (cgraph_node (fn));
148               }
149             if (gimple_code (stmt) == GIMPLE_OMP_TASK)
150               {
151                 tree fn = gimple_omp_task_child_fn (stmt);
152                 if (fn)
153                   cgraph_mark_needed_node (cgraph_node (fn));
154                 fn = gimple_omp_task_copy_fn (stmt);
155                 if (fn)
156                   cgraph_mark_needed_node (cgraph_node (fn));
157               }
158           }
159       }
160
161   /* Look for initializers of constant variables and private statics.  */
162   for (step = cfun->local_decls;
163        step;
164        step = TREE_CHAIN (step))
165     {
166       tree decl = TREE_VALUE (step);
167       if (TREE_CODE (decl) == VAR_DECL
168           && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
169         varpool_finalize_decl (decl);
170       else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
171         walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
172     }
173
174   pointer_set_destroy (visited_nodes);
175   return 0;
176 }
177
178 struct gimple_opt_pass pass_build_cgraph_edges =
179 {
180  {
181   GIMPLE_PASS,
182   NULL,                                 /* name */
183   NULL,                                 /* gate */
184   build_cgraph_edges,                   /* execute */
185   NULL,                                 /* sub */
186   NULL,                                 /* next */
187   0,                                    /* static_pass_number */
188   TV_NONE,                              /* tv_id */
189   PROP_cfg,                             /* properties_required */
190   0,                                    /* properties_provided */
191   0,                                    /* properties_destroyed */
192   0,                                    /* todo_flags_start */
193   0                                     /* todo_flags_finish */
194  }
195 };
196
197 /* Record references to functions and other variables present in the
198    initial value of DECL, a variable.  */
199
200 void
201 record_references_in_initializer (tree decl)
202 {
203   struct pointer_set_t *visited_nodes = pointer_set_create ();
204   walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
205   pointer_set_destroy (visited_nodes);
206 }
207
208 /* Rebuild cgraph edges for current function node.  This needs to be run after
209    passes that don't update the cgraph.  */
210
211 unsigned int
212 rebuild_cgraph_edges (void)
213 {
214   basic_block bb;
215   struct cgraph_node *node = cgraph_node (current_function_decl);
216   gimple_stmt_iterator gsi;
217
218   cgraph_node_remove_callees (node);
219
220   node->count = ENTRY_BLOCK_PTR->count;
221
222   FOR_EACH_BB (bb)
223     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
224       {
225         gimple stmt = gsi_stmt (gsi);
226         tree decl;
227
228         if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
229           cgraph_create_edge (node, cgraph_node (decl), stmt,
230                               bb->count,
231                               compute_call_stmt_bb_frequency
232                                 (current_function_decl, bb),
233                               bb->loop_depth);
234
235       }
236   gcc_assert (!node->global.inlined_to);
237
238   return 0;
239 }
240
241 struct gimple_opt_pass pass_rebuild_cgraph_edges =
242 {
243  {
244   GIMPLE_PASS,
245   NULL,                                 /* name */
246   NULL,                                 /* gate */
247   rebuild_cgraph_edges,                 /* execute */
248   NULL,                                 /* sub */
249   NULL,                                 /* next */
250   0,                                    /* static_pass_number */
251   TV_NONE,                              /* tv_id */
252   PROP_cfg,                             /* properties_required */
253   0,                                    /* properties_provided */
254   0,                                    /* properties_destroyed */
255   0,                                    /* todo_flags_start */
256   0,                                    /* todo_flags_finish */
257  }
258 };
259
260
261 static unsigned int
262 remove_cgraph_callee_edges (void)
263 {
264   cgraph_node_remove_callees (cgraph_node (current_function_decl));
265   return 0;
266 }
267
268 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
269 {
270  {
271   GIMPLE_PASS,
272   NULL,                                 /* name */
273   NULL,                                 /* gate */
274   remove_cgraph_callee_edges,           /* execute */
275   NULL,                                 /* sub */
276   NULL,                                 /* next */
277   0,                                    /* static_pass_number */
278   TV_NONE,                              /* tv_id */
279   0,                                    /* properties_required */
280   0,                                    /* properties_provided */
281   0,                                    /* properties_destroyed */
282   0,                                    /* todo_flags_start */
283   0,                                    /* todo_flags_finish */
284  }
285 };