OSDN Git Service

2008-07-23 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tree-flow.h"
27 #include "langhooks.h"
28 #include "pointer-set.h"
29 #include "cgraph.h"
30 #include "intl.h"
31 #include "tree-gimple.h"
32 #include "tree-pass.h"
33
34 /* Walk tree and record all calls and references to functions/variables.
35    Called via walk_tree: TP is pointer to tree to be examined.  */
36
37 static tree
38 record_reference (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
39 {
40   tree t = *tp;
41
42   switch (TREE_CODE (t))
43     {
44     case VAR_DECL:
45       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
46         {
47           varpool_mark_needed_node (varpool_node (t));
48           if (lang_hooks.callgraph.analyze_expr)
49             return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
50         }
51       break;
52
53     case FDESC_EXPR:
54     case ADDR_EXPR:
55       if (flag_unit_at_a_time)
56         {
57           /* Record dereferences to the functions.  This makes the
58              functions reachable unconditionally.  */
59           tree decl = TREE_OPERAND (*tp, 0);
60           if (TREE_CODE (decl) == FUNCTION_DECL)
61             cgraph_mark_needed_node (cgraph_node (decl));
62         }
63       break;
64
65     case OMP_PARALLEL:
66       if (flag_unit_at_a_time)
67         {
68           if (OMP_PARALLEL_FN (*tp))
69             cgraph_mark_needed_node (cgraph_node (OMP_PARALLEL_FN (*tp)));
70         }
71       break;
72
73     case OMP_TASK:
74       if (flag_unit_at_a_time)
75         {
76           if (OMP_TASK_FN (*tp))
77             cgraph_mark_needed_node (cgraph_node (OMP_TASK_FN (*tp)));
78           if (OMP_TASK_COPYFN (*tp))
79             cgraph_mark_needed_node (cgraph_node (OMP_TASK_COPYFN (*tp)));
80         }
81       break;
82
83     default:
84       /* Save some cycles by not walking types and declaration as we
85          won't find anything useful there anyway.  */
86       if (IS_TYPE_OR_DECL_P (*tp))
87         {
88           *walk_subtrees = 0;
89           break;
90         }
91
92       if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
93         return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
94       break;
95     }
96
97   return NULL_TREE;
98 }
99
100 /* Give initial reasons why inlining would fail on all calls from
101    NODE.  Those get either nullified or usually overwritten by more precise
102    reason later.  */
103
104 static void
105 initialize_inline_failed (struct cgraph_node *node)
106 {
107   struct cgraph_edge *e;
108
109   for (e = node->callers; e; e = e->next_caller)
110     {
111       gcc_assert (!e->callee->global.inlined_to);
112       gcc_assert (e->inline_failed);
113       if (node->local.redefined_extern_inline)
114         e->inline_failed = N_("redefined extern inline functions are not "
115                            "considered for inlining");
116       else if (!node->local.inlinable)
117         e->inline_failed = N_("function not inlinable");
118       else if (CALL_STMT_CANNOT_INLINE_P (e->call_stmt))
119         e->inline_failed = N_("mismatched arguments");
120       else
121         e->inline_failed = N_("function not considered for inlining");
122     }
123 }
124
125 /* Computes the frequency of the call statement so that it can be stored in
126    cgraph_edge.  BB is the basic block of the call statement.  */
127 int
128 compute_call_stmt_bb_frequency (basic_block bb)
129 {
130   int entry_freq = ENTRY_BLOCK_PTR->frequency;
131   int freq;
132
133   if (!entry_freq)
134     entry_freq = 1;
135
136   freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
137               : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
138   if (freq > CGRAPH_FREQ_MAX)
139     freq = CGRAPH_FREQ_MAX;
140
141   return freq;
142 }
143
144 /* Create cgraph edges for function calls.
145    Also look for functions and variables having addresses taken.  */
146
147 static unsigned int
148 build_cgraph_edges (void)
149 {
150   basic_block bb;
151   struct cgraph_node *node = cgraph_node (current_function_decl);
152   struct pointer_set_t *visited_nodes = pointer_set_create ();
153   block_stmt_iterator bsi;
154   tree step;
155
156   /* Create the callgraph edges and record the nodes referenced by the function.
157      body.  */
158   FOR_EACH_BB (bb)
159     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
160       {
161         tree stmt = bsi_stmt (bsi);
162         tree call = get_call_expr_in (stmt);
163         tree decl;
164
165         if (call && (decl = get_callee_fndecl (call)))
166           {
167             int i;
168             int n = call_expr_nargs (call);
169             cgraph_create_edge (node, cgraph_node (decl), stmt,
170                                 bb->count, compute_call_stmt_bb_frequency (bb),
171                                 bb->loop_depth);
172             for (i = 0; i < n; i++)
173               walk_tree (&CALL_EXPR_ARG (call, i),
174                          record_reference, node, visited_nodes);
175             if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
176               walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
177                          record_reference, node, visited_nodes);
178           }
179         else
180           walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
181       }
182
183   /* Look for initializers of constant variables and private statics.  */
184   for (step = cfun->local_decls;
185        step;
186        step = TREE_CHAIN (step))
187     {
188       tree decl = TREE_VALUE (step);
189       if (TREE_CODE (decl) == VAR_DECL
190           && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
191           && flag_unit_at_a_time)
192         varpool_finalize_decl (decl);
193       else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
194         walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
195     }
196
197   pointer_set_destroy (visited_nodes);
198   initialize_inline_failed (node);
199   return 0;
200 }
201
202 struct gimple_opt_pass pass_build_cgraph_edges =
203 {
204  {
205   GIMPLE_PASS,
206   NULL,                                 /* name */
207   NULL,                                 /* gate */
208   build_cgraph_edges,                   /* execute */
209   NULL,                                 /* sub */
210   NULL,                                 /* next */
211   0,                                    /* static_pass_number */
212   0,                                    /* tv_id */
213   PROP_cfg,                             /* properties_required */
214   0,                                    /* properties_provided */
215   0,                                    /* properties_destroyed */
216   0,                                    /* todo_flags_start */
217   0                                     /* todo_flags_finish */
218  }
219 };
220
221 /* Record references to functions and other variables present in the
222    initial value of DECL, a variable.  */
223
224 void
225 record_references_in_initializer (tree decl)
226 {
227   struct pointer_set_t *visited_nodes = pointer_set_create ();
228   walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
229   pointer_set_destroy (visited_nodes);
230 }
231
232 /* Rebuild cgraph edges for current function node.  This needs to be run after
233    passes that don't update the cgraph.  */
234
235 unsigned int
236 rebuild_cgraph_edges (void)
237 {
238   basic_block bb;
239   struct cgraph_node *node = cgraph_node (current_function_decl);
240   block_stmt_iterator bsi;
241
242   cgraph_node_remove_callees (node);
243
244   node->count = ENTRY_BLOCK_PTR->count;
245
246   FOR_EACH_BB (bb)
247     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
248       {
249         tree stmt = bsi_stmt (bsi);
250         tree call = get_call_expr_in (stmt);
251         tree decl;
252
253         if (call && (decl = get_callee_fndecl (call)))
254           cgraph_create_edge (node, cgraph_node (decl), stmt,
255                               bb->count, compute_call_stmt_bb_frequency (bb),
256                               bb->loop_depth);
257       }
258   initialize_inline_failed (node);
259   gcc_assert (!node->global.inlined_to);
260   return 0;
261 }
262
263 struct gimple_opt_pass pass_rebuild_cgraph_edges =
264 {
265  {
266   GIMPLE_PASS,
267   NULL,                                 /* name */
268   NULL,                                 /* gate */
269   rebuild_cgraph_edges,                 /* execute */
270   NULL,                                 /* sub */
271   NULL,                                 /* next */
272   0,                                    /* static_pass_number */
273   0,                                    /* tv_id */
274   PROP_cfg,                             /* properties_required */
275   0,                                    /* properties_provided */
276   0,                                    /* properties_destroyed */
277   0,                                    /* todo_flags_start */
278   0,                                    /* todo_flags_finish */
279  }
280 };