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