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