]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraph.c
predict.c (always_optimize_for_size_p): New function.
[thirdparty/gcc.git] / gcc / cgraph.c
CommitLineData
e72fcfe8 1/* Callgraph handling code.
2bafad93
JJ
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
e72fcfe8
JH
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
e72fcfe8
JH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
e72fcfe8 21
8a4a83ed 22/* This file contains basic routines manipulating call graph
c22cacf3 23
18c6ada9
JH
24The callgraph:
25
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
28 sharing.
29
30 The call-graph consist of nodes and edges represented via linked lists.
0550e7b7 31 Each function (external or not) corresponds to the unique node.
18c6ada9
JH
32
33 The mapping from declarations to call-graph nodes is done using hash table
0550e7b7
JH
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
18c6ada9
JH
36
37 The callgraph at the moment does not represent indirect calls or calls
38 from other compilation unit. Flag NEEDED is set for each node that may
1f838355 39 be accessed in such an invisible way and it shall be considered an
18c6ada9
JH
40 entry point to the callgraph.
41
a418679d 42 Interprocedural information:
18c6ada9 43
a418679d 44 Callgraph is place to store data needed for interprocedural optimization.
1ae58c30 45 All data structures are divided into three components: local_info that
18c6ada9 46 is produced while analyzing the function, global_info that is result
1ae58c30 47 of global walking of the callgraph on the end of compilation and
18c6ada9
JH
48 rtl_info used by RTL backend to propagate data from already compiled
49 functions to their callers.
50
51 Inlining plans:
52
53 The function inlining information is decided in advance and maintained
54 in the callgraph as so called inline plan.
1ae58c30 55 For each inlined call, the callee's node is cloned to represent the
1ea7e6ad 56 new function copy produced by inliner.
1ae58c30
KH
57 Each inlined call gets a unique corresponding clone node of the callee
58 and the data structure is updated while inlining is performed, so
59 the clones are eliminated and their callee edges redirected to the
c22cacf3 60 caller.
18c6ada9
JH
61
62 Each edge has "inline_failed" field. When the field is set to NULL,
2b8a92de 63 the call will be inlined. When it is non-NULL it contains a reason
8a4a83ed 64 why inlining wasn't performed. */
18c6ada9 65
e72fcfe8
JH
66#include "config.h"
67#include "system.h"
68#include "coretypes.h"
69#include "tm.h"
70#include "tree.h"
cd9c7bd2 71#include "tree-inline.h"
e72fcfe8
JH
72#include "langhooks.h"
73#include "hashtab.h"
74#include "toplev.h"
75#include "flags.h"
76#include "ggc.h"
77#include "debug.h"
78#include "target.h"
cd9c7bd2 79#include "basic-block.h"
1c4a429a 80#include "cgraph.h"
988d1653 81#include "varray.h"
e69529cd 82#include "output.h"
dc0bfe6a 83#include "intl.h"
726a989a 84#include "gimple.h"
ef330312 85#include "tree-dump.h"
7a388ee4 86#include "tree-flow.h"
988d1653 87
2563c224
RG
88static void cgraph_node_remove_callers (struct cgraph_node *node);
89static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
90static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
91
e72fcfe8 92/* Hash table used to convert declarations into nodes. */
ed2df68b 93static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
266ad5c8
JH
94/* Hash table used to convert assembler names into nodes. */
95static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
e72fcfe8
JH
96
97/* The linked list of cgraph nodes. */
1c4a429a 98struct cgraph_node *cgraph_nodes;
e72fcfe8 99
1668aabc
JH
100/* Queue of cgraph nodes scheduled to be lowered. */
101struct cgraph_node *cgraph_nodes_queue;
102
f45e0ad1 103/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
c0220ea4 104 secondary queue used during optimization to accommodate passes that
50674e96 105 may generate new functions that need to be optimized and expanded. */
f45e0ad1 106struct cgraph_node *cgraph_new_nodes;
953ff289 107
e72fcfe8 108/* Number of nodes in existence. */
1c4a429a 109int cgraph_n_nodes;
e72fcfe8 110
b58b1157
JH
111/* Maximal uid used in cgraph nodes. */
112int cgraph_max_uid;
113
9088c1cc
MJ
114/* Maximal uid used in cgraph edges. */
115int cgraph_edge_max_uid;
116
6bad2617
TB
117/* Maximal pid used for profiling */
118int cgraph_max_pid;
119
dafc5b82
JH
120/* Set when whole unit has been analyzed so we can access global info. */
121bool cgraph_global_info_ready = false;
122
f45e0ad1
JH
123/* What state callgraph is in right now. */
124enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
125
cd9c7bd2
JH
126/* Set when the cgraph is fully build and the basic flags are computed. */
127bool cgraph_function_flags_ready = false;
128
474eccc6
ILT
129/* Linked list of cgraph asm nodes. */
130struct cgraph_asm_node *cgraph_asm_nodes;
131
132/* Last node in cgraph_asm_nodes. */
133static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
134
135/* The order index of the next cgraph node to be created. This is
136 used so that we can sort the cgraph nodes in order by when we saw
137 them, to support -fno-toplevel-reorder. */
138int cgraph_order;
139
9088c1cc
MJ
140/* List of hooks trigerred on cgraph_edge events. */
141struct cgraph_edge_hook_list {
142 cgraph_edge_hook hook;
143 void *data;
144 struct cgraph_edge_hook_list *next;
145};
146
147/* List of hooks trigerred on cgraph_node events. */
148struct cgraph_node_hook_list {
149 cgraph_node_hook hook;
150 void *data;
151 struct cgraph_node_hook_list *next;
152};
153
154/* List of hooks trigerred on events involving two cgraph_edges. */
155struct cgraph_2edge_hook_list {
156 cgraph_2edge_hook hook;
157 void *data;
158 struct cgraph_2edge_hook_list *next;
159};
160
161/* List of hooks trigerred on events involving two cgraph_nodes. */
162struct cgraph_2node_hook_list {
163 cgraph_2node_hook hook;
164 void *data;
165 struct cgraph_2node_hook_list *next;
166};
167
168/* List of hooks triggered when an edge is removed. */
169struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
170/* List of hooks triggered when a node is removed. */
171struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
172/* List of hooks triggered when an edge is duplicated. */
173struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
174/* List of hooks triggered when a node is duplicated. */
175struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
176
177
178/* Register HOOK to be called with DATA on each removed edge. */
179struct cgraph_edge_hook_list *
180cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
181{
182 struct cgraph_edge_hook_list *entry;
183 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
184
185 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
186 entry->hook = hook;
187 entry->data = data;
188 entry->next = NULL;
189 while (*ptr)
190 ptr = &(*ptr)->next;
191 *ptr = entry;
192 return entry;
193}
194
195/* Remove ENTRY from the list of hooks called on removing edges. */
196void
197cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
198{
199 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
200
201 while (*ptr != entry)
202 ptr = &(*ptr)->next;
203 *ptr = entry->next;
204}
205
206/* Call all edge removal hooks. */
207static void
208cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
209{
210 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
211 while (entry)
212 {
213 entry->hook (e, entry->data);
214 entry = entry->next;
215 }
216}
217
218/* Register HOOK to be called with DATA on each removed node. */
219struct cgraph_node_hook_list *
220cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
221{
222 struct cgraph_node_hook_list *entry;
223 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
224
225 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
226 entry->hook = hook;
227 entry->data = data;
228 entry->next = NULL;
229 while (*ptr)
230 ptr = &(*ptr)->next;
231 *ptr = entry;
232 return entry;
233}
234
235/* Remove ENTRY from the list of hooks called on removing nodes. */
236void
237cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
238{
239 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
240
241 while (*ptr != entry)
242 ptr = &(*ptr)->next;
243 *ptr = entry->next;
244}
245
246/* Call all node removal hooks. */
247static void
248cgraph_call_node_removal_hooks (struct cgraph_node *node)
249{
250 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
251 while (entry)
252 {
253 entry->hook (node, entry->data);
254 entry = entry->next;
255 }
256}
257
258/* Register HOOK to be called with DATA on each duplicated edge. */
259struct cgraph_2edge_hook_list *
260cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
261{
262 struct cgraph_2edge_hook_list *entry;
263 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
264
265 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
266 entry->hook = hook;
267 entry->data = data;
268 entry->next = NULL;
269 while (*ptr)
270 ptr = &(*ptr)->next;
271 *ptr = entry;
272 return entry;
273}
274
275/* Remove ENTRY from the list of hooks called on duplicating edges. */
276void
277cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
278{
279 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
280
281 while (*ptr != entry)
282 ptr = &(*ptr)->next;
283 *ptr = entry->next;
284}
285
286/* Call all edge duplication hooks. */
287static void
288cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
289 struct cgraph_edge *cs2)
290{
291 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
292 while (entry)
293 {
294 entry->hook (cs1, cs2, entry->data);
295 entry = entry->next;
296 }
297}
298
299/* Register HOOK to be called with DATA on each duplicated node. */
300struct cgraph_2node_hook_list *
301cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
302{
303 struct cgraph_2node_hook_list *entry;
304 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
305
306 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
307 entry->hook = hook;
308 entry->data = data;
309 entry->next = NULL;
310 while (*ptr)
311 ptr = &(*ptr)->next;
312 *ptr = entry;
313 return entry;
314}
315
316/* Remove ENTRY from the list of hooks called on duplicating nodes. */
317void
318cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
319{
320 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
321
322 while (*ptr != entry)
323 ptr = &(*ptr)->next;
324 *ptr = entry->next;
325}
326
327/* Call all node duplication hooks. */
328static void
329cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
330 struct cgraph_node *node2)
331{
332 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
333 while (entry)
334 {
335 entry->hook (node1, node2, entry->data);
336 entry = entry->next;
337 }
338}
339
e72fcfe8
JH
340/* Returns a hash code for P. */
341
342static hashval_t
439f7bc3 343hash_node (const void *p)
e72fcfe8 344{
cceb1885 345 const struct cgraph_node *n = (const struct cgraph_node *) p;
6f312d18 346 return (hashval_t) DECL_UID (n->decl);
e72fcfe8
JH
347}
348
6356f892 349/* Returns nonzero if P1 and P2 are equal. */
e72fcfe8
JH
350
351static int
439f7bc3 352eq_node (const void *p1, const void *p2)
e72fcfe8 353{
cceb1885
GDR
354 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
355 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
6f312d18 356 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
e72fcfe8
JH
357}
358
1ae58c30 359/* Allocate new callgraph node and insert it into basic data structures. */
0550e7b7 360
18c6ada9
JH
361static struct cgraph_node *
362cgraph_create_node (void)
363{
364 struct cgraph_node *node;
365
cceb1885 366 node = GGC_CNEW (struct cgraph_node);
18c6ada9
JH
367 node->next = cgraph_nodes;
368 node->uid = cgraph_max_uid++;
6bad2617 369 node->pid = -1;
474eccc6 370 node->order = cgraph_order++;
18c6ada9
JH
371 if (cgraph_nodes)
372 cgraph_nodes->previous = node;
373 node->previous = NULL;
670cd5c5 374 node->global.estimated_growth = INT_MIN;
18c6ada9
JH
375 cgraph_nodes = node;
376 cgraph_n_nodes++;
377 return node;
378}
379
e72fcfe8 380/* Return cgraph node assigned to DECL. Create new one when needed. */
0550e7b7 381
1c4a429a 382struct cgraph_node *
439f7bc3 383cgraph_node (tree decl)
e72fcfe8 384{
6f312d18 385 struct cgraph_node key, *node, **slot;
e72fcfe8 386
341c100f 387 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
988d1653 388
e72fcfe8 389 if (!cgraph_hash)
ed2df68b 390 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
e72fcfe8 391
6f312d18
ZW
392 key.decl = decl;
393
394 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
395
e72fcfe8 396 if (*slot)
6b02a499
JH
397 {
398 node = *slot;
399 if (!node->master_clone)
400 node->master_clone = node;
401 return node;
402 }
18c6ada9
JH
403
404 node = cgraph_create_node ();
e72fcfe8 405 node->decl = decl;
e72fcfe8 406 *slot = node;
5c2e00ee 407 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
e72fcfe8
JH
408 {
409 node->origin = cgraph_node (DECL_CONTEXT (decl));
410 node->next_nested = node->origin->nested;
411 node->origin->nested = node;
6b02a499 412 node->master_clone = node;
e72fcfe8 413 }
266ad5c8 414
e72fcfe8
JH
415 return node;
416}
417
ea99e0be
JH
418/* Insert already constructed node into hashtable. */
419
420void
421cgraph_insert_node_to_hashtable (struct cgraph_node *node)
422{
423 struct cgraph_node **slot;
424
425 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
426
427 gcc_assert (!*slot);
428 *slot = node;
429}
430
266ad5c8
JH
431/* Returns a hash code for P. */
432
433static hashval_t
434hash_node_by_assembler_name (const void *p)
435{
436 const struct cgraph_node *n = (const struct cgraph_node *) p;
437 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
438}
439
440/* Returns nonzero if P1 and P2 are equal. */
441
442static int
443eq_assembler_name (const void *p1, const void *p2)
444{
445 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
446 const_tree name = (const_tree)p2;
447 return (decl_assembler_name_equal (n1->decl, name));
448}
bedb9fc0
RH
449
450/* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
451 Return NULL if there's no such node. */
452
453struct cgraph_node *
454cgraph_node_for_asm (tree asmname)
455{
456 struct cgraph_node *node;
266ad5c8 457 void **slot;
bedb9fc0 458
266ad5c8
JH
459 if (!assembler_name_hash)
460 {
461 assembler_name_hash =
462 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
463 NULL);
464 for (node = cgraph_nodes; node; node = node->next)
465 if (!node->global.inlined_to)
466 {
467 tree name = DECL_ASSEMBLER_NAME (node->decl);
468 slot = htab_find_slot_with_hash (assembler_name_hash, name,
469 decl_assembler_name_hash (name),
470 INSERT);
471 /* We can have multiple declarations with same assembler name. For C++
472 it is __builtin_strlen and strlen, for instance. Do we need to
473 record them all? Original implementation marked just first one
474 so lets hope for the best. */
475 if (*slot)
476 continue;
477 *slot = node;
478 }
479 }
480
481 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
482 decl_assembler_name_hash (asmname),
483 NO_INSERT);
bedb9fc0 484
266ad5c8
JH
485 if (slot)
486 return (struct cgraph_node *) *slot;
bedb9fc0
RH
487 return NULL;
488}
489
70d539ce
JH
490/* Returns a hash value for X (which really is a die_struct). */
491
492static hashval_t
493edge_hash (const void *x)
494{
741ac903 495 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
70d539ce
JH
496}
497
498/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
499
500static int
501edge_eq (const void *x, const void *y)
502{
741ac903 503 return ((const struct cgraph_edge *) x)->call_stmt == y;
70d539ce
JH
504}
505
726a989a
RB
506
507/* Return the callgraph edge representing the GIMPLE_CALL statement
508 CALL_STMT. */
509
18c6ada9 510struct cgraph_edge *
726a989a 511cgraph_edge (struct cgraph_node *node, gimple call_stmt)
18c6ada9 512{
70d539ce
JH
513 struct cgraph_edge *e, *e2;
514 int n = 0;
515
516 if (node->call_site_hash)
f883e0a7
KG
517 return (struct cgraph_edge *)
518 htab_find_with_hash (node->call_site_hash, call_stmt,
519 htab_hash_pointer (call_stmt));
18c6ada9
JH
520
521 /* This loop may turn out to be performance problem. In such case adding
522 hashtables into call nodes with very many edges is probably best
2b8a92de 523 solution. It is not good idea to add pointer into CALL_EXPR itself
18c6ada9
JH
524 because we want to make possible having multiple cgraph nodes representing
525 different clones of the same body before the body is actually cloned. */
526 for (e = node->callees; e; e= e->next_callee)
70d539ce
JH
527 {
528 if (e->call_stmt == call_stmt)
529 break;
530 n++;
531 }
726a989a 532
70d539ce
JH
533 if (n > 100)
534 {
535 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
536 for (e2 = node->callees; e2; e2 = e2->next_callee)
537 {
538 void **slot;
539 slot = htab_find_slot_with_hash (node->call_site_hash,
540 e2->call_stmt,
541 htab_hash_pointer (e2->call_stmt),
542 INSERT);
543 gcc_assert (!*slot);
544 *slot = e2;
545 }
546 }
726a989a 547
18c6ada9
JH
548 return e;
549}
550
726a989a
RB
551
552/* Change field call_smt of edge E to NEW_STMT. */
0550e7b7 553
70d539ce 554void
726a989a 555cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
70d539ce
JH
556{
557 if (e->caller->call_site_hash)
558 {
559 htab_remove_elt_with_hash (e->caller->call_site_hash,
560 e->call_stmt,
561 htab_hash_pointer (e->call_stmt));
562 }
563 e->call_stmt = new_stmt;
564 if (e->caller->call_site_hash)
565 {
566 void **slot;
567 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
568 e->call_stmt,
569 htab_hash_pointer
570 (e->call_stmt), INSERT);
571 gcc_assert (!*slot);
572 *slot = e;
573 }
574}
575
e72fcfe8
JH
576/* Create edge from CALLER to CALLEE in the cgraph. */
577
18c6ada9
JH
578struct cgraph_edge *
579cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
726a989a 580 gimple call_stmt, gcov_type count, int freq, int nest)
e72fcfe8 581{
cceb1885 582 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
18c6ada9
JH
583#ifdef ENABLE_CHECKING
584 struct cgraph_edge *e;
585
586 for (e = caller->callees; e; e = e->next_callee)
e0704a46 587 gcc_assert (e->call_stmt != call_stmt);
18c6ada9
JH
588#endif
589
726a989a 590 gcc_assert (is_gimple_call (call_stmt));
b58b1157 591
726a989a 592 if (!gimple_body (callee->decl))
dc0bfe6a 593 edge->inline_failed = N_("function body not available");
95c755e9
JH
594 else if (callee->local.redefined_extern_inline)
595 edge->inline_failed = N_("redefined extern inline functions are not "
596 "considered for inlining");
dc0bfe6a
JH
597 else if (callee->local.inlinable)
598 edge->inline_failed = N_("function not considered for inlining");
599 else
600 edge->inline_failed = N_("function not inlinable");
601
18c6ada9 602 edge->aux = NULL;
e72fcfe8
JH
603
604 edge->caller = caller;
605 edge->callee = callee;
e0704a46 606 edge->call_stmt = call_stmt;
2563c224 607 edge->prev_caller = NULL;
e72fcfe8 608 edge->next_caller = callee->callers;
2563c224
RG
609 if (callee->callers)
610 callee->callers->prev_caller = edge;
611 edge->prev_callee = NULL;
e72fcfe8 612 edge->next_callee = caller->callees;
2563c224
RG
613 if (caller->callees)
614 caller->callees->prev_callee = edge;
e72fcfe8
JH
615 caller->callees = edge;
616 callee->callers = edge;
e42922b1 617 edge->count = count;
45a80bb9
JH
618 gcc_assert (count >= 0);
619 edge->frequency = freq;
620 gcc_assert (freq >= 0);
621 gcc_assert (freq <= CGRAPH_FREQ_MAX);
e42922b1 622 edge->loop_nest = nest;
3e293154 623 edge->indirect_call = 0;
9088c1cc 624 edge->uid = cgraph_edge_max_uid++;
70d539ce
JH
625 if (caller->call_site_hash)
626 {
627 void **slot;
628 slot = htab_find_slot_with_hash (caller->call_site_hash,
629 edge->call_stmt,
630 htab_hash_pointer
631 (edge->call_stmt),
632 INSERT);
633 gcc_assert (!*slot);
634 *slot = edge;
635 }
e72fcfe8
JH
636 return edge;
637}
638
2563c224
RG
639/* Remove the edge E from the list of the callers of the callee. */
640
641static inline void
642cgraph_edge_remove_callee (struct cgraph_edge *e)
643{
644 if (e->prev_caller)
645 e->prev_caller->next_caller = e->next_caller;
646 if (e->next_caller)
647 e->next_caller->prev_caller = e->prev_caller;
648 if (!e->prev_caller)
649 e->callee->callers = e->next_caller;
650}
651
652/* Remove the edge E from the list of the callees of the caller. */
653
654static inline void
655cgraph_edge_remove_caller (struct cgraph_edge *e)
656{
657 if (e->prev_callee)
658 e->prev_callee->next_callee = e->next_callee;
659 if (e->next_callee)
660 e->next_callee->prev_callee = e->prev_callee;
661 if (!e->prev_callee)
662 e->caller->callees = e->next_callee;
70d539ce
JH
663 if (e->caller->call_site_hash)
664 htab_remove_elt_with_hash (e->caller->call_site_hash,
665 e->call_stmt,
666 htab_hash_pointer (e->call_stmt));
2563c224
RG
667}
668
669/* Remove the edge E in the cgraph. */
e72fcfe8 670
cb967da5 671void
18c6ada9 672cgraph_remove_edge (struct cgraph_edge *e)
e72fcfe8 673{
9088c1cc 674 cgraph_call_edge_removal_hooks (e);
2563c224
RG
675 /* Remove from callers list of the callee. */
676 cgraph_edge_remove_callee (e);
677
678 /* Remove from callees list of the callers. */
679 cgraph_edge_remove_caller (e);
e72fcfe8
JH
680}
681
18c6ada9
JH
682/* Redirect callee of E to N. The function does not update underlying
683 call expression. */
684
685void
686cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
687{
2563c224
RG
688 /* Remove from callers list of the current callee. */
689 cgraph_edge_remove_callee (e);
18c6ada9 690
2563c224
RG
691 /* Insert to callers list of the new callee. */
692 e->prev_caller = NULL;
693 if (n->callers)
694 n->callers->prev_caller = e;
18c6ada9
JH
695 e->next_caller = n->callers;
696 n->callers = e;
2563c224
RG
697 e->callee = n;
698}
699
726a989a
RB
700
701/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
702 OLD_STMT changed into NEW_STMT. */
2bafad93
JJ
703
704void
726a989a 705cgraph_update_edges_for_call_stmt (gimple old_stmt, gimple new_stmt)
2bafad93 706{
726a989a
RB
707 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fn (new_stmt) : 0;
708 tree old_call = (is_gimple_call (old_stmt)) ? gimple_call_fn (old_stmt) : 0;
2bafad93
JJ
709 struct cgraph_node *node = cgraph_node (cfun->decl);
710
711 if (old_call != new_call)
712 {
713 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
714 struct cgraph_edge *ne = NULL;
715 tree new_decl;
716
717 if (e)
718 {
719 gcov_type count = e->count;
720 int frequency = e->frequency;
721 int loop_nest = e->loop_nest;
722
723 cgraph_remove_edge (e);
724 if (new_call)
725 {
726a989a 726 new_decl = gimple_call_fndecl (new_stmt);
2bafad93
JJ
727 if (new_decl)
728 {
729 ne = cgraph_create_edge (node, cgraph_node (new_decl),
730 new_stmt, count, frequency,
731 loop_nest);
732 gcc_assert (ne->inline_failed);
733 }
734 }
735 }
736 }
737 else if (old_stmt != new_stmt)
738 {
739 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
740
741 if (e)
742 cgraph_set_call_stmt (e, new_stmt);
743 }
744}
745
726a989a 746
2563c224
RG
747/* Remove all callees from the node. */
748
749void
750cgraph_node_remove_callees (struct cgraph_node *node)
751{
752 struct cgraph_edge *e;
753
754 /* It is sufficient to remove the edges from the lists of callers of
755 the callees. The callee list of the node can be zapped with one
756 assignment. */
757 for (e = node->callees; e; e = e->next_callee)
9088c1cc
MJ
758 {
759 cgraph_call_edge_removal_hooks (e);
760 cgraph_edge_remove_callee (e);
761 }
2563c224 762 node->callees = NULL;
70d539ce
JH
763 if (node->call_site_hash)
764 {
765 htab_delete (node->call_site_hash);
766 node->call_site_hash = NULL;
767 }
2563c224
RG
768}
769
770/* Remove all callers from the node. */
771
772static void
773cgraph_node_remove_callers (struct cgraph_node *node)
774{
775 struct cgraph_edge *e;
776
777 /* It is sufficient to remove the edges from the lists of callees of
778 the callers. The caller list of the node can be zapped with one
779 assignment. */
780 for (e = node->callers; e; e = e->next_caller)
9088c1cc
MJ
781 {
782 cgraph_call_edge_removal_hooks (e);
783 cgraph_edge_remove_caller (e);
784 }
2563c224 785 node->callers = NULL;
18c6ada9
JH
786}
787
3a40c18a
JH
788/* Release memory used to represent body of function NODE. */
789
790void
791cgraph_release_function_body (struct cgraph_node *node)
792{
793 if (DECL_STRUCT_FUNCTION (node->decl)
794 && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
795 {
796 tree old_decl = current_function_decl;
797 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
798 current_function_decl = node->decl;
799 delete_tree_ssa ();
800 delete_tree_cfg_annotations ();
4437b50d 801 cfun->eh = NULL;
726a989a 802 gimple_set_body (node->decl, NULL);
3a40c18a
JH
803 current_function_decl = old_decl;
804 pop_cfun();
805 }
806 DECL_SAVED_TREE (node->decl) = NULL;
807 DECL_STRUCT_FUNCTION (node->decl) = NULL;
808 DECL_INITIAL (node->decl) = error_mark_node;
809}
810
18d13f34
JH
811/* Remove the node from cgraph. */
812
813void
439f7bc3 814cgraph_remove_node (struct cgraph_node *node)
18d13f34 815{
2ee1067b 816 void **slot;
4a76d91a 817 bool kill_body = false;
18c6ada9 818
9088c1cc 819 cgraph_call_node_removal_hooks (node);
2563c224
RG
820 cgraph_node_remove_callers (node);
821 cgraph_node_remove_callees (node);
266ad5c8 822
96fc428c
JH
823 /* Incremental inlining access removed nodes stored in the postorder list.
824 */
825 node->needed = node->reachable = false;
18d13f34
JH
826 while (node->nested)
827 cgraph_remove_node (node->nested);
828 if (node->origin)
829 {
830 struct cgraph_node **node2 = &node->origin->nested;
831
832 while (*node2 != node)
833 node2 = &(*node2)->next_nested;
834 *node2 = node->next_nested;
835 }
836 if (node->previous)
837 node->previous->next = node->next;
838 else
9b0436b7 839 cgraph_nodes = node->next;
18d13f34
JH
840 if (node->next)
841 node->next->previous = node->previous;
96fc428c
JH
842 node->next = NULL;
843 node->previous = NULL;
6f312d18 844 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
18c6ada9
JH
845 if (*slot == node)
846 {
847 if (node->next_clone)
1655dc9d 848 {
6b02a499
JH
849 struct cgraph_node *new_node = node->next_clone;
850 struct cgraph_node *n;
851
852 /* Make the next clone be the master clone */
c22cacf3 853 for (n = new_node; n; n = n->next_clone)
6b02a499 854 n->master_clone = new_node;
c22cacf3 855
6b02a499 856 *slot = new_node;
1655dc9d
JH
857 node->next_clone->prev_clone = NULL;
858 }
18c6ada9
JH
859 else
860 {
c22cacf3 861 htab_clear_slot (cgraph_hash, slot);
4a76d91a 862 kill_body = true;
18c6ada9
JH
863 }
864 }
865 else
866 {
1655dc9d
JH
867 node->prev_clone->next_clone = node->next_clone;
868 if (node->next_clone)
c22cacf3 869 node->next_clone->prev_clone = node->prev_clone;
18c6ada9
JH
870 }
871
c22cacf3 872 /* While all the clones are removed after being proceeded, the function
4a76d91a
JH
873 itself is kept in the cgraph even after it is compiled. Check whether
874 we are done with this body and reclaim it proactively if this is the case.
875 */
876 if (!kill_body && *slot)
18c6ada9 877 {
cceb1885 878 struct cgraph_node *n = (struct cgraph_node *) *slot;
4a76d91a 879 if (!n->next_clone && !n->global.inlined_to
d63db217
JH
880 && (cgraph_global_info_ready
881 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
4a76d91a
JH
882 kill_body = true;
883 }
266ad5c8
JH
884 if (assembler_name_hash)
885 {
886 tree name = DECL_ASSEMBLER_NAME (node->decl);
887 slot = htab_find_slot_with_hash (assembler_name_hash, name,
888 decl_assembler_name_hash (name),
889 NO_INSERT);
890 /* Inline clones are not hashed. */
891 if (slot && *slot == node)
892 htab_clear_slot (assembler_name_hash, slot);
893 }
18c6ada9 894
7e8b322a 895 if (kill_body)
3a40c18a 896 cgraph_release_function_body (node);
96fc428c 897 node->decl = NULL;
70d539ce
JH
898 if (node->call_site_hash)
899 {
900 htab_delete (node->call_site_hash);
901 node->call_site_hash = NULL;
902 }
18c6ada9 903 cgraph_n_nodes--;
18d13f34
JH
904 /* Do not free the structure itself so the walk over chain can continue. */
905}
906
8dafba3c
RH
907/* Notify finalize_compilation_unit that given node is reachable. */
908
1668aabc 909void
8dafba3c 910cgraph_mark_reachable_node (struct cgraph_node *node)
1668aabc 911{
ba245151 912 if (!node->reachable && node->local.finalized)
1668aabc 913 {
ba245151 914 notice_global_symbol (node->decl);
1668aabc 915 node->reachable = 1;
45676d2b 916 gcc_assert (!cgraph_global_info_ready);
e767b5be
JH
917
918 node->next_needed = cgraph_nodes_queue;
919 cgraph_nodes_queue = node;
1668aabc
JH
920 }
921}
922
8dafba3c
RH
923/* Likewise indicate that a node is needed, i.e. reachable via some
924 external means. */
925
926void
927cgraph_mark_needed_node (struct cgraph_node *node)
928{
929 node->needed = 1;
930 cgraph_mark_reachable_node (node);
931}
18d13f34 932
dafc5b82
JH
933/* Return local info for the compiled function. */
934
935struct cgraph_local_info *
439f7bc3 936cgraph_local_info (tree decl)
dafc5b82
JH
937{
938 struct cgraph_node *node;
c22cacf3 939
341c100f 940 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
dafc5b82
JH
941 node = cgraph_node (decl);
942 return &node->local;
943}
944
945/* Return local info for the compiled function. */
946
947struct cgraph_global_info *
439f7bc3 948cgraph_global_info (tree decl)
dafc5b82
JH
949{
950 struct cgraph_node *node;
c22cacf3 951
341c100f 952 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
dafc5b82
JH
953 node = cgraph_node (decl);
954 return &node->global;
955}
956
b255a036
JH
957/* Return local info for the compiled function. */
958
959struct cgraph_rtl_info *
439f7bc3 960cgraph_rtl_info (tree decl)
b255a036
JH
961{
962 struct cgraph_node *node;
c22cacf3 963
341c100f 964 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
b255a036
JH
965 node = cgraph_node (decl);
966 if (decl != current_function_decl
967 && !TREE_ASM_WRITTEN (node->decl))
968 return NULL;
969 return &node->rtl;
970}
971
a194aa56
JH
972/* Return name of the node used in debug output. */
973const char *
439f7bc3 974cgraph_node_name (struct cgraph_node *node)
a194aa56 975{
ae2bcd98 976 return lang_hooks.decl_printable_name (node->decl, 2);
a194aa56 977}
dafc5b82 978
6b02a499 979/* Names used to print out the availability enum. */
8a4a83ed 980const char * const cgraph_availability_names[] =
fa10beec 981 {"unset", "not_available", "overwritable", "available", "local"};
6b02a499 982
c4e622b6
DN
983
984/* Dump call graph node NODE to file F. */
985
18c6ada9
JH
986void
987dump_cgraph_node (FILE *f, struct cgraph_node *node)
988{
989 struct cgraph_edge *edge;
6bad2617 990 fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
18c6ada9
JH
991 if (node->global.inlined_to)
992 fprintf (f, " (inline copy in %s/%i)",
993 cgraph_node_name (node->global.inlined_to),
994 node->global.inlined_to->uid);
6b02a499 995 if (cgraph_function_flags_ready)
c22cacf3 996 fprintf (f, " availability:%s",
8a4a83ed 997 cgraph_availability_names [cgraph_function_body_availability (node)]);
6b02a499
JH
998 if (node->master_clone && node->master_clone->uid != node->uid)
999 fprintf (f, "(%i)", node->master_clone->uid);
e42922b1
JH
1000 if (node->count)
1001 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1002 (HOST_WIDEST_INT)node->count);
95622280
JH
1003 if (node->local.inline_summary.self_insns)
1004 fprintf (f, " %i insns", node->local.inline_summary.self_insns);
1005 if (node->global.insns && node->global.insns
1006 != node->local.inline_summary.self_insns)
18c6ada9 1007 fprintf (f, " (%i after inlining)", node->global.insns);
95622280
JH
1008 if (node->local.inline_summary.estimated_self_stack_size)
1009 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1010 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
ff28a94d 1011 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
18c6ada9
JH
1012 if (node->origin)
1013 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1014 if (node->needed)
1015 fprintf (f, " needed");
1016 else if (node->reachable)
1017 fprintf (f, " reachable");
726a989a
RB
1018 if (gimple_body (node->decl))
1019 fprintf (f, " body");
18c6ada9
JH
1020 if (node->output)
1021 fprintf (f, " output");
18c6ada9
JH
1022 if (node->local.local)
1023 fprintf (f, " local");
e7d6beb0
JH
1024 if (node->local.externally_visible)
1025 fprintf (f, " externally_visible");
1026 if (node->local.finalized)
1027 fprintf (f, " finalized");
18c6ada9
JH
1028 if (node->local.disregard_inline_limits)
1029 fprintf (f, " always_inline");
1030 else if (node->local.inlinable)
1031 fprintf (f, " inlinable");
e7d6beb0
JH
1032 if (node->local.redefined_extern_inline)
1033 fprintf (f, " redefined_extern_inline");
18c6ada9
JH
1034 if (TREE_ASM_WRITTEN (node->decl))
1035 fprintf (f, " asm_written");
1036
1037 fprintf (f, "\n called by: ");
1038 for (edge = node->callers; edge; edge = edge->next_caller)
1039 {
1040 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1041 edge->caller->uid);
e42922b1
JH
1042 if (edge->count)
1043 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1044 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1045 if (edge->frequency)
1046 fprintf (f, "(%.2f per call) ",
1047 edge->frequency / (double)CGRAPH_FREQ_BASE);
18c6ada9
JH
1048 if (!edge->inline_failed)
1049 fprintf(f, "(inlined) ");
3e293154
MJ
1050 if (edge->indirect_call)
1051 fprintf(f, "(indirect) ");
18c6ada9
JH
1052 }
1053
1054 fprintf (f, "\n calls: ");
1055 for (edge = node->callees; edge; edge = edge->next_callee)
1056 {
1057 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1058 edge->callee->uid);
1059 if (!edge->inline_failed)
1060 fprintf(f, "(inlined) ");
3e293154
MJ
1061 if (edge->indirect_call)
1062 fprintf(f, "(indirect) ");
6b02a499
JH
1063 if (edge->count)
1064 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1065 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1066 if (edge->frequency)
1067 fprintf (f, "(%.2f per call) ",
1068 edge->frequency / (double)CGRAPH_FREQ_BASE);
6b02a499
JH
1069 if (edge->loop_nest)
1070 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
18c6ada9
JH
1071 }
1072 fprintf (f, "\n");
1073}
1074
c4e622b6
DN
1075
1076/* Dump call graph node NODE to stderr. */
1077
1078void
1079debug_cgraph_node (struct cgraph_node *node)
1080{
1081 dump_cgraph_node (stderr, node);
1082}
1083
1084
1085/* Dump the callgraph to file F. */
e72fcfe8
JH
1086
1087void
439f7bc3 1088dump_cgraph (FILE *f)
e72fcfe8
JH
1089{
1090 struct cgraph_node *node;
1091
7d82fe7c 1092 fprintf (f, "callgraph:\n\n");
e72fcfe8 1093 for (node = cgraph_nodes; node; node = node->next)
18c6ada9 1094 dump_cgraph_node (f, node);
e72fcfe8 1095}
988d1653 1096
c4e622b6
DN
1097
1098/* Dump the call graph to stderr. */
1099
1100void
1101debug_cgraph (void)
1102{
1103 dump_cgraph (stderr);
1104}
1105
1106
fccc4eb2 1107/* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
c4e622b6 1108
fccc4eb2
JH
1109void
1110change_decl_assembler_name (tree decl, tree name)
1111{
266ad5c8 1112 gcc_assert (!assembler_name_hash);
fccc4eb2
JH
1113 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1114 {
1115 SET_DECL_ASSEMBLER_NAME (decl, name);
1116 return;
1117 }
1118 if (name == DECL_ASSEMBLER_NAME (decl))
1119 return;
1120
df964a18
JH
1121 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1122 && DECL_RTL_SET_P (decl))
d4ee4d25 1123 warning (0, "%D renamed after being referenced in assembly", decl);
fccc4eb2 1124
fccc4eb2 1125 SET_DECL_ASSEMBLER_NAME (decl, name);
e69529cd
JH
1126}
1127
474eccc6
ILT
1128/* Add a top-level asm statement to the list. */
1129
1130struct cgraph_asm_node *
1131cgraph_add_asm_node (tree asm_str)
1132{
1133 struct cgraph_asm_node *node;
1134
1135 node = GGC_CNEW (struct cgraph_asm_node);
1136 node->asm_str = asm_str;
1137 node->order = cgraph_order++;
1138 node->next = NULL;
1139 if (cgraph_asm_nodes == NULL)
1140 cgraph_asm_nodes = node;
1141 else
1142 cgraph_asm_last_node->next = node;
1143 cgraph_asm_last_node = node;
1144 return node;
1145}
1146
1bb17c21
JH
1147/* Return true when the DECL can possibly be inlined. */
1148bool
1149cgraph_function_possibly_inlined_p (tree decl)
1150{
1bb17c21 1151 if (!cgraph_global_info_ready)
c37f4ba4 1152 return !DECL_UNINLINABLE (decl) && !flag_really_no_inline;
6f312d18 1153 return DECL_POSSIBLY_INLINED (decl);
18c6ada9
JH
1154}
1155
1156/* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
1157struct cgraph_edge *
e42922b1 1158cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
726a989a 1159 gimple call_stmt, gcov_type count_scale, int freq_scale,
45a80bb9 1160 int loop_nest, bool update_original)
18c6ada9 1161{
e42922b1 1162 struct cgraph_edge *new;
45a80bb9
JH
1163 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
1164 gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
e42922b1 1165
45a80bb9
JH
1166 if (freq > CGRAPH_FREQ_MAX)
1167 freq = CGRAPH_FREQ_MAX;
1168 new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
c22cacf3 1169 e->loop_nest + loop_nest);
18c6ada9
JH
1170
1171 new->inline_failed = e->inline_failed;
3e293154 1172 new->indirect_call = e->indirect_call;
c5a4444c 1173 if (update_original)
d63f0fe5
JH
1174 {
1175 e->count -= new->count;
1176 if (e->count < 0)
1177 e->count = 0;
1178 }
9088c1cc 1179 cgraph_call_edge_duplication_hooks (e, new);
18c6ada9 1180 return new;
1bb17c21 1181}
e69529cd 1182
e42922b1 1183/* Create node representing clone of N executed COUNT times. Decrease
c22cacf3 1184 the execution counts from original node too.
c5a4444c
JH
1185
1186 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1187 function's profile to reflect the fact that part of execution is handled
1188 by node. */
18c6ada9 1189struct cgraph_node *
726a989a
RB
1190cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq,
1191 int loop_nest, bool update_original)
18c6ada9
JH
1192{
1193 struct cgraph_node *new = cgraph_create_node ();
1194 struct cgraph_edge *e;
06191a23 1195 gcov_type count_scale;
18c6ada9
JH
1196
1197 new->decl = n->decl;
1198 new->origin = n->origin;
1199 if (new->origin)
1200 {
1201 new->next_nested = new->origin->nested;
1202 new->origin->nested = new;
1203 }
1204 new->analyzed = n->analyzed;
1205 new->local = n->local;
1206 new->global = n->global;
1207 new->rtl = n->rtl;
6b02a499 1208 new->master_clone = n->master_clone;
e42922b1
JH
1209 new->count = count;
1210 if (n->count)
1211 count_scale = new->count * REG_BR_PROB_BASE / n->count;
1212 else
1213 count_scale = 0;
c5a4444c 1214 if (update_original)
d63f0fe5
JH
1215 {
1216 n->count -= count;
1217 if (n->count < 0)
1218 n->count = 0;
1219 }
18c6ada9
JH
1220
1221 for (e = n->callees;e; e=e->next_callee)
45a80bb9 1222 cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
c5a4444c 1223 update_original);
18c6ada9
JH
1224
1225 new->next_clone = n->next_clone;
1655dc9d 1226 new->prev_clone = n;
18c6ada9 1227 n->next_clone = new;
1655dc9d
JH
1228 if (new->next_clone)
1229 new->next_clone->prev_clone = new;
18c6ada9 1230
9088c1cc 1231 cgraph_call_node_duplication_hooks (n, new);
18c6ada9
JH
1232 return new;
1233}
8f235343 1234
6b02a499
JH
1235/* Return true if N is an master_clone, (see cgraph_master_clone). */
1236
1237bool
1238cgraph_is_master_clone (struct cgraph_node *n)
1239{
1240 return (n == cgraph_master_clone (n));
1241}
1242
1243struct cgraph_node *
1244cgraph_master_clone (struct cgraph_node *n)
1245{
1246 enum availability avail = cgraph_function_body_availability (n);
c22cacf3 1247
6b02a499
JH
1248 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1249 return NULL;
1250
c22cacf3 1251 if (!n->master_clone)
6b02a499 1252 n->master_clone = cgraph_node (n->decl);
c22cacf3 1253
6b02a499
JH
1254 return n->master_clone;
1255}
1256
8f235343
JH
1257/* NODE is no longer nested function; update cgraph accordingly. */
1258void
1259cgraph_unnest_node (struct cgraph_node *node)
1260{
1261 struct cgraph_node **node2 = &node->origin->nested;
1262 gcc_assert (node->origin);
1263
1264 while (*node2 != node)
1265 node2 = &(*node2)->next_nested;
1266 *node2 = node->next_nested;
1267 node->origin = NULL;
1268}
6b02a499
JH
1269
1270/* Return function availability. See cgraph.h for description of individual
1271 return values. */
1272enum availability
1273cgraph_function_body_availability (struct cgraph_node *node)
1274{
1275 enum availability avail;
1276 gcc_assert (cgraph_function_flags_ready);
093c2329 1277 if (!node->analyzed)
6b02a499
JH
1278 avail = AVAIL_NOT_AVAILABLE;
1279 else if (node->local.local)
1280 avail = AVAIL_LOCAL;
1281 else if (node->local.externally_visible)
1282 avail = AVAIL_AVAILABLE;
1283
1284 /* If the function can be overwritten, return OVERWRITABLE. Take
1285 care at least of two notable extensions - the COMDAT functions
1286 used to share template instantiations in C++ (this is symmetric
1287 to code cp_cannot_inline_tree_fn and probably shall be shared and
ff5c4582 1288 the inlinability hooks completely eliminated).
6b02a499
JH
1289
1290 ??? Does the C++ one definition rule allow us to always return
1291 AVAIL_AVAILABLE here? That would be good reason to preserve this
1292 hook Similarly deal with extern inline functions - this is again
ff5c4582 1293 necessary to get C++ shared functions having keyed templates
6b02a499
JH
1294 right and in the C extension documentation we probably should
1295 document the requirement of both versions of function (extern
1296 inline and offline) having same side effect characteristics as
1297 good optimization is what this optimization is about. */
c22cacf3 1298
6b02a499
JH
1299 else if (!(*targetm.binds_local_p) (node->decl)
1300 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1301 avail = AVAIL_OVERWRITABLE;
1302 else avail = AVAIL_AVAILABLE;
1303
1304 return avail;
1305}
1306
f45e0ad1
JH
1307/* Add the function FNDECL to the call graph.
1308 Unlike cgraph_finalize_function, this function is intended to be used
1309 by middle end and allows insertion of new function at arbitrary point
1310 of compilation. The function can be either in high, low or SSA form
1311 GIMPLE.
50674e96 1312
f45e0ad1
JH
1313 The function is assumed to be reachable and have address taken (so no
1314 API breaking optimizations are performed on it).
50674e96 1315
f45e0ad1
JH
1316 Main work done by this function is to enqueue the function for later
1317 processing to avoid need the passes to be re-entrant. */
953ff289
DN
1318
1319void
f45e0ad1 1320cgraph_add_new_function (tree fndecl, bool lowered)
953ff289 1321{
f45e0ad1
JH
1322 struct cgraph_node *node;
1323 switch (cgraph_state)
1324 {
1325 case CGRAPH_STATE_CONSTRUCTION:
fa10beec 1326 /* Just enqueue function to be processed at nearest occurrence. */
f45e0ad1
JH
1327 node = cgraph_node (fndecl);
1328 node->next_needed = cgraph_new_nodes;
1329 if (lowered)
1330 node->lowered = true;
1331 cgraph_new_nodes = node;
1332 break;
1333
1334 case CGRAPH_STATE_IPA:
7a388ee4 1335 case CGRAPH_STATE_IPA_SSA:
f45e0ad1
JH
1336 case CGRAPH_STATE_EXPANSION:
1337 /* Bring the function into finalized state and enqueue for later
1338 analyzing and compilation. */
1339 node = cgraph_node (fndecl);
1340 node->local.local = false;
1341 node->local.finalized = true;
1342 node->reachable = node->needed = true;
ff2c88a5
JH
1343 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
1344 {
1345 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1346 current_function_decl = fndecl;
726a989a
RB
1347 gimple_register_cfg_hooks ();
1348 tree_lowering_passes (fndecl);
ff2c88a5
JH
1349 bitmap_obstack_initialize (NULL);
1350 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1351 execute_pass_list (pass_early_local_passes.pass.sub);
1352 bitmap_obstack_release (NULL);
1353 pop_cfun ();
1354 current_function_decl = NULL;
1355
1356 lowered = true;
1357 }
f45e0ad1
JH
1358 if (lowered)
1359 node->lowered = true;
1360 node->next_needed = cgraph_new_nodes;
1361 cgraph_new_nodes = node;
1362 break;
1363
1364 case CGRAPH_STATE_FINISHED:
1365 /* At the very end of compilation we have to do all the work up
1366 to expansion. */
1367 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1368 current_function_decl = fndecl;
726a989a 1369 gimple_register_cfg_hooks ();
f45e0ad1
JH
1370 if (!lowered)
1371 tree_lowering_passes (fndecl);
7a388ee4 1372 bitmap_obstack_initialize (NULL);
c72321c9 1373 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
8ddbbcae 1374 execute_pass_list (pass_early_local_passes.pass.sub);
7a388ee4 1375 bitmap_obstack_release (NULL);
f45e0ad1
JH
1376 tree_rest_of_compilation (fndecl);
1377 pop_cfun ();
1378 current_function_decl = NULL;
1379 break;
1380 }
953ff289
DN
1381}
1382
988d1653 1383#include "gt-cgraph.h"