OSDN Git Service

Add dbg count support for ccp
[pf3gnuchains/gcc-fork.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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_needed_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 /* Give initial reasons why inlining would fail on all calls from
82    NODE.  Those get either nullified or usually overwritten by more precise
83    reason later.  */
84
85 static void
86 initialize_inline_failed (struct cgraph_node *node)
87 {
88   struct cgraph_edge *e;
89
90   for (e = node->callers; e; e = e->next_caller)
91     {
92       gcc_assert (!e->callee->global.inlined_to);
93       gcc_assert (e->inline_failed);
94       if (node->local.redefined_extern_inline)
95         e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
96       else if (!node->local.inlinable)
97         e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
98       else if (gimple_call_cannot_inline_p (e->call_stmt))
99         e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
100       else
101         e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
102     }
103 }
104
105 /* Computes the frequency of the call statement so that it can be stored in
106    cgraph_edge.  BB is the basic block of the call statement.  */
107 int
108 compute_call_stmt_bb_frequency (basic_block bb)
109 {
110   int entry_freq = ENTRY_BLOCK_PTR->frequency;
111   int freq = bb->frequency;
112
113   if (!entry_freq)
114     entry_freq = 1, freq++;
115
116   freq = freq * CGRAPH_FREQ_BASE / entry_freq;
117   if (freq > CGRAPH_FREQ_MAX)
118     freq = CGRAPH_FREQ_MAX;
119
120   return freq;
121 }
122
123 /* Create cgraph edges for function calls.
124    Also look for functions and variables having addresses taken.  */
125
126 static unsigned int
127 build_cgraph_edges (void)
128 {
129   basic_block bb;
130   struct cgraph_node *node = cgraph_node (current_function_decl);
131   struct pointer_set_t *visited_nodes = pointer_set_create ();
132   gimple_stmt_iterator gsi;
133   tree step;
134
135   /* Create the callgraph edges and record the nodes referenced by the function.
136      body.  */
137   FOR_EACH_BB (bb)
138     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
139       {
140         gimple stmt = gsi_stmt (gsi);
141         tree decl;
142
143         if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
144           {
145             size_t i;
146             size_t n = gimple_call_num_args (stmt);
147             cgraph_create_edge (node, cgraph_node (decl), stmt,
148                                 bb->count, compute_call_stmt_bb_frequency (bb),
149                                 bb->loop_depth);
150             for (i = 0; i < n; i++)
151               walk_tree (gimple_call_arg_ptr (stmt, i), record_reference,
152                          node, visited_nodes);
153             if (gimple_call_lhs (stmt))
154               walk_tree (gimple_call_lhs_ptr (stmt), record_reference, node,
155                          visited_nodes);
156           }
157         else
158           {
159             struct walk_stmt_info wi;
160             memset (&wi, 0, sizeof (wi));
161             wi.info = node;
162             wi.pset = visited_nodes;
163             walk_gimple_op (stmt, record_reference, &wi);
164             if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
165                 && gimple_omp_parallel_child_fn (stmt))
166               {
167                 tree fn = gimple_omp_parallel_child_fn (stmt);
168                 cgraph_mark_needed_node (cgraph_node (fn));
169               }
170             if (gimple_code (stmt) == GIMPLE_OMP_TASK)
171               {
172                 tree fn = gimple_omp_task_child_fn (stmt);
173                 if (fn)
174                   cgraph_mark_needed_node (cgraph_node (fn));
175                 fn = gimple_omp_task_copy_fn (stmt);
176                 if (fn)
177                   cgraph_mark_needed_node (cgraph_node (fn));
178               }
179           }
180       }
181
182   /* Look for initializers of constant variables and private statics.  */
183   for (step = cfun->local_decls;
184        step;
185        step = TREE_CHAIN (step))
186     {
187       tree decl = TREE_VALUE (step);
188       if (TREE_CODE (decl) == VAR_DECL
189           && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
190         varpool_finalize_decl (decl);
191       else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
192         walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
193     }
194
195   pointer_set_destroy (visited_nodes);
196   initialize_inline_failed (node);
197   return 0;
198 }
199
200 struct gimple_opt_pass pass_build_cgraph_edges =
201 {
202  {
203   GIMPLE_PASS,
204   NULL,                                 /* name */
205   NULL,                                 /* gate */
206   build_cgraph_edges,                   /* execute */
207   NULL,                                 /* sub */
208   NULL,                                 /* next */
209   0,                                    /* static_pass_number */
210   0,                                    /* tv_id */
211   PROP_cfg,                             /* properties_required */
212   0,                                    /* properties_provided */
213   0,                                    /* properties_destroyed */
214   0,                                    /* todo_flags_start */
215   0                                     /* todo_flags_finish */
216  }
217 };
218
219 /* Record references to functions and other variables present in the
220    initial value of DECL, a variable.  */
221
222 void
223 record_references_in_initializer (tree decl)
224 {
225   struct pointer_set_t *visited_nodes = pointer_set_create ();
226   walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
227   pointer_set_destroy (visited_nodes);
228 }
229
230 /* Rebuild cgraph edges for current function node.  This needs to be run after
231    passes that don't update the cgraph.  */
232
233 unsigned int
234 rebuild_cgraph_edges (void)
235 {
236   basic_block bb;
237   struct cgraph_node *node = cgraph_node (current_function_decl);
238   gimple_stmt_iterator gsi;
239
240   cgraph_node_remove_callees (node);
241
242   node->count = ENTRY_BLOCK_PTR->count;
243
244   FOR_EACH_BB (bb)
245     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
246       {
247         gimple stmt = gsi_stmt (gsi);
248         tree decl;
249
250         if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
251           cgraph_create_edge (node, cgraph_node (decl), stmt,
252                               bb->count, compute_call_stmt_bb_frequency (bb),
253                               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 };