OSDN Git Service

2008-07-06 Andreas Tobler <a.tobler@schweiz.org>
[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 /* Create cgraph edges for function calls.
126    Also look for functions and variables having addresses taken.  */
127
128 static unsigned int
129 build_cgraph_edges (void)
130 {
131   basic_block bb;
132   struct cgraph_node *node = cgraph_node (current_function_decl);
133   struct pointer_set_t *visited_nodes = pointer_set_create ();
134   block_stmt_iterator bsi;
135   tree step;
136   int entry_freq = ENTRY_BLOCK_PTR->frequency;
137
138   if (!entry_freq)
139     entry_freq = 1;
140
141   /* Create the callgraph edges and record the nodes referenced by the function.
142      body.  */
143   FOR_EACH_BB (bb)
144     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
145       {
146         tree stmt = bsi_stmt (bsi);
147         tree call = get_call_expr_in (stmt);
148         tree decl;
149
150         if (call && (decl = get_callee_fndecl (call)))
151           {
152             int i;
153             int n = call_expr_nargs (call);
154             int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
155                         : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
156             if (freq > CGRAPH_FREQ_MAX)
157               freq = CGRAPH_FREQ_MAX;
158             cgraph_create_edge (node, cgraph_node (decl), stmt,
159                                 bb->count, freq,
160                                 bb->loop_depth);
161             for (i = 0; i < n; i++)
162               walk_tree (&CALL_EXPR_ARG (call, i),
163                          record_reference, node, visited_nodes);
164             if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
165               walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
166                          record_reference, node, visited_nodes);
167           }
168         else
169           walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
170       }
171
172   /* Look for initializers of constant variables and private statics.  */
173   for (step = cfun->local_decls;
174        step;
175        step = TREE_CHAIN (step))
176     {
177       tree decl = TREE_VALUE (step);
178       if (TREE_CODE (decl) == VAR_DECL
179           && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
180           && flag_unit_at_a_time)
181         varpool_finalize_decl (decl);
182       else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
183         walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
184     }
185
186   pointer_set_destroy (visited_nodes);
187   initialize_inline_failed (node);
188   return 0;
189 }
190
191 struct gimple_opt_pass pass_build_cgraph_edges =
192 {
193  {
194   GIMPLE_PASS,
195   NULL,                                 /* name */
196   NULL,                                 /* gate */
197   build_cgraph_edges,                   /* execute */
198   NULL,                                 /* sub */
199   NULL,                                 /* next */
200   0,                                    /* static_pass_number */
201   0,                                    /* tv_id */
202   PROP_cfg,                             /* properties_required */
203   0,                                    /* properties_provided */
204   0,                                    /* properties_destroyed */
205   0,                                    /* todo_flags_start */
206   0                                     /* todo_flags_finish */
207  }
208 };
209
210 /* Record references to functions and other variables present in the
211    initial value of DECL, a variable.  */
212
213 void
214 record_references_in_initializer (tree decl)
215 {
216   struct pointer_set_t *visited_nodes = pointer_set_create ();
217   walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
218   pointer_set_destroy (visited_nodes);
219 }
220
221 /* Rebuild cgraph edges for current function node.  This needs to be run after
222    passes that don't update the cgraph.  */
223
224 unsigned int
225 rebuild_cgraph_edges (void)
226 {
227   basic_block bb;
228   struct cgraph_node *node = cgraph_node (current_function_decl);
229   block_stmt_iterator bsi;
230   int entry_freq = ENTRY_BLOCK_PTR->frequency;
231
232   if (!entry_freq)
233     entry_freq = 1;
234
235   cgraph_node_remove_callees (node);
236
237   node->count = ENTRY_BLOCK_PTR->count;
238
239   FOR_EACH_BB (bb)
240     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
241       {
242         tree stmt = bsi_stmt (bsi);
243         tree call = get_call_expr_in (stmt);
244         tree decl;
245
246         if (call && (decl = get_callee_fndecl (call)))
247           {
248             int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
249                         : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
250             if (freq > CGRAPH_FREQ_MAX)
251               freq = CGRAPH_FREQ_MAX;
252             cgraph_create_edge (node, cgraph_node (decl), stmt,
253                                 bb->count, freq, bb->loop_depth);
254            }
255       }
256   initialize_inline_failed (node);
257   gcc_assert (!node->global.inlined_to);
258   return 0;
259 }
260
261 struct gimple_opt_pass pass_rebuild_cgraph_edges =
262 {
263  {
264   GIMPLE_PASS,
265   NULL,                                 /* name */
266   NULL,                                 /* gate */
267   rebuild_cgraph_edges,                 /* execute */
268   NULL,                                 /* sub */
269   NULL,                                 /* next */
270   0,                                    /* static_pass_number */
271   0,                                    /* tv_id */
272   PROP_cfg,                             /* properties_required */
273   0,                                    /* properties_provided */
274   0,                                    /* properties_destroyed */
275   0,                                    /* todo_flags_start */
276   0,                                    /* todo_flags_finish */
277  }
278 };