forked from illumos/gcc
/
cgraphunit.c
2389 lines (2070 loc) · 70.6 KB
/
cgraphunit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Callgraph based interprocedural optimizations.
Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011 Free Software Foundation, Inc.
Contributed by Jan Hubicka
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* This module implements main driver of compilation process as well as
few basic interprocedural optimizers.
The main scope of this file is to act as an interface in between
tree based frontends and the backend (and middle end)
The front-end is supposed to use following functionality:
- cgraph_finalize_function
This function is called once front-end has parsed whole body of function
and it is certain that the function body nor the declaration will change.
(There is one exception needed for implementing GCC extern inline
function.)
- varpool_finalize_variable
This function has same behavior as the above but is used for static
variables.
- cgraph_finalize_compilation_unit
This function is called once (source level) compilation unit is finalized
and it will no longer change.
In the the call-graph construction and local function
analysis takes place here. Bodies of unreachable functions are released
to conserve memory usage.
The function can be called multiple times when multiple source level
compilation units are combined (such as in C frontend)
- cgraph_optimize
In this unit-at-a-time compilation the intra procedural analysis takes
place here. In particular the static functions whose address is never
taken are marked as local. Backend can then use this information to
modify calling conventions, do better inlining or similar optimizations.
- cgraph_mark_needed_node
- varpool_mark_needed_node
When function or variable is referenced by some hidden way the call-graph
data structure must be updated accordingly by this function.
There should be little need to call this function and all the references
should be made explicit to cgraph code. At present these functions are
used by C++ frontend to explicitly mark the keyed methods.
- analyze_expr callback
This function is responsible for lowering tree nodes not understood by
generic code into understandable ones or alternatively marking
callgraph and varpool nodes referenced by the as needed.
??? On the tree-ssa genericizing should take place here and we will avoid
need for these hooks (replacing them by genericizing hook)
Analyzing of all functions is deferred
to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
In cgraph_finalize_compilation_unit the reachable functions are
analyzed. During analysis the call-graph edges from reachable
functions are constructed and their destinations are marked as
reachable. References to functions and variables are discovered too
and variables found to be needed output to the assembly file. Via
mark_referenced call in assemble_variable functions referenced by
static variables are noticed too.
The intra-procedural information is produced and its existence
indicated by global_info_ready. Once this flag is set it is impossible
to change function from !reachable to reachable and thus
assemble_variable no longer call mark_referenced.
Finally the call-graph is topologically sorted and all reachable functions
that has not been completely inlined or are not external are output.
??? It is possible that reference to function or variable is optimized
out. We can not deal with this nicely because topological order is not
suitable for it. For tree-ssa we may consider another pass doing
optimization and re-discovering reachable functions.
??? Reorganize code so variables are output very last and only if they
really has been referenced by produced code, so we catch more cases
where reference has been optimized out. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "tree-flow.h"
#include "tree-inline.h"
#include "langhooks.h"
#include "pointer-set.h"
#include "toplev.h"
#include "flags.h"
#include "ggc.h"
#include "debug.h"
#include "target.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "tree-pretty-print.h"
#include "gimple-pretty-print.h"
#include "timevar.h"
#include "params.h"
#include "fibheap.h"
#include "intl.h"
#include "function.h"
#include "ipa-prop.h"
#include "gimple.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "output.h"
#include "coverage.h"
#include "plugin.h"
static void cgraph_expand_all_functions (void);
static void cgraph_mark_functions_to_output (void);
static void cgraph_expand_function (struct cgraph_node *);
static void cgraph_output_pending_asms (void);
static void cgraph_analyze_function (struct cgraph_node *);
FILE *cgraph_dump_file;
/* Used for vtable lookup in thunk adjusting. */
static GTY (()) tree vtable_entry_type;
/* Determine if function DECL is needed. That is, visible to something
either outside this translation unit, something magic in the system
configury. */
bool
cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
{
/* If the user told us it is used, then it must be so. */
if (node->local.externally_visible)
return true;
/* ??? If the assembler name is set by hand, it is possible to assemble
the name later after finalizing the function and the fact is noticed
in assemble_name then. This is arguably a bug. */
if (DECL_ASSEMBLER_NAME_SET_P (decl)
&& TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
return true;
/* With -fkeep-inline-functions we are keeping all inline functions except
for extern inline ones. */
if (flag_keep_inline_functions
&& DECL_DECLARED_INLINE_P (decl)
&& !DECL_EXTERNAL (decl)
&& !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
return true;
/* If we decided it was needed before, but at the time we didn't have
the body of the function available, then it's still needed. We have
to go back and re-check its dependencies now. */
if (node->needed)
return true;
/* Externally visible functions must be output. The exception is
COMDAT functions that must be output only when they are needed.
When not optimizing, also output the static functions. (see
PR24561), but don't do so for always_inline functions, functions
declared inline and nested functions. These were optimized out
in the original implementation and it is unclear whether we want
to change the behavior here. */
if (((TREE_PUBLIC (decl)
|| (!optimize
&& !node->local.disregard_inline_limits
&& !DECL_DECLARED_INLINE_P (decl)
&& !(DECL_CONTEXT (decl)
&& TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)))
&& !flag_whole_program
&& !flag_lto)
&& !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
return true;
return false;
}
/* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
functions into callgraph in a way so they look like ordinary reachable
functions inserted into callgraph already at construction time. */
bool
cgraph_process_new_functions (void)
{
bool output = false;
tree fndecl;
struct cgraph_node *node;
varpool_analyze_pending_decls ();
/* Note that this queue may grow as its being processed, as the new
functions may generate new ones. */
while (cgraph_new_nodes)
{
node = cgraph_new_nodes;
fndecl = node->decl;
cgraph_new_nodes = cgraph_new_nodes->next_needed;
switch (cgraph_state)
{
case CGRAPH_STATE_CONSTRUCTION:
/* At construction time we just need to finalize function and move
it into reachable functions list. */
node->next_needed = NULL;
cgraph_finalize_function (fndecl, false);
cgraph_mark_reachable_node (node);
output = true;
break;
case CGRAPH_STATE_IPA:
case CGRAPH_STATE_IPA_SSA:
/* When IPA optimization already started, do all essential
transformations that has been already performed on the whole
cgraph but not on this function. */
gimple_register_cfg_hooks ();
if (!node->analyzed)
cgraph_analyze_function (node);
push_cfun (DECL_STRUCT_FUNCTION (fndecl));
current_function_decl = fndecl;
if ((cgraph_state == CGRAPH_STATE_IPA_SSA
&& !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
/* When not optimizing, be sure we run early local passes anyway
to expand OMP. */
|| !optimize)
execute_pass_list (pass_early_local_passes.pass.sub);
else
compute_inline_parameters (node);
free_dominance_info (CDI_POST_DOMINATORS);
free_dominance_info (CDI_DOMINATORS);
pop_cfun ();
current_function_decl = NULL;
break;
case CGRAPH_STATE_EXPANSION:
/* Functions created during expansion shall be compiled
directly. */
node->process = 0;
cgraph_expand_function (node);
break;
default:
gcc_unreachable ();
break;
}
cgraph_call_function_insertion_hooks (node);
varpool_analyze_pending_decls ();
}
return output;
}
/* As an GCC extension we allow redefinition of the function. The
semantics when both copies of bodies differ is not well defined.
We replace the old body with new body so in unit at a time mode
we always use new body, while in normal mode we may end up with
old body inlined into some functions and new body expanded and
inlined in others.
??? It may make more sense to use one body for inlining and other
body for expanding the function but this is difficult to do. */
static void
cgraph_reset_node (struct cgraph_node *node)
{
/* If node->process is set, then we have already begun whole-unit analysis.
This is *not* testing for whether we've already emitted the function.
That case can be sort-of legitimately seen with real function redefinition
errors. I would argue that the front end should never present us with
such a case, but don't enforce that for now. */
gcc_assert (!node->process);
/* Reset our data structures so we can analyze the function again. */
memset (&node->local, 0, sizeof (node->local));
memset (&node->global, 0, sizeof (node->global));
memset (&node->rtl, 0, sizeof (node->rtl));
node->analyzed = false;
node->local.redefined_extern_inline = true;
node->local.finalized = false;
cgraph_node_remove_callees (node);
/* We may need to re-queue the node for assembling in case
we already proceeded it and ignored as not needed or got
a re-declaration in IMA mode. */
if (node->reachable)
{
struct cgraph_node *n;
for (n = cgraph_nodes_queue; n; n = n->next_needed)
if (n == node)
break;
if (!n)
node->reachable = 0;
}
}
static void
cgraph_lower_function (struct cgraph_node *node)
{
if (node->lowered)
return;
if (node->nested)
lower_nested_functions (node->decl);
gcc_assert (!node->nested);
tree_lowering_passes (node->decl);
node->lowered = true;
}
/* DECL has been parsed. Take it, queue it, compile it at the whim of the
logic in effect. If NESTED is true, then our caller cannot stand to have
the garbage collector run at the moment. We would need to either create
a new GC context, or just not compile right now. */
void
cgraph_finalize_function (tree decl, bool nested)
{
struct cgraph_node *node = cgraph_node (decl);
if (node->local.finalized)
cgraph_reset_node (node);
node->pid = cgraph_max_pid ++;
notice_global_symbol (decl);
node->local.finalized = true;
node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
node->finalized_by_frontend = true;
if (cgraph_decide_is_function_needed (node, decl))
cgraph_mark_needed_node (node);
/* Since we reclaim unreachable nodes at the end of every language
level unit, we need to be conservative about possible entry points
there. */
if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
|| DECL_STATIC_CONSTRUCTOR (decl)
|| DECL_STATIC_DESTRUCTOR (decl)
/* COMDAT virtual functions may be referenced by vtable from
other compilation unit. Still we want to devirtualize calls
to those so we need to analyze them.
FIXME: We should introduce may edges for this purpose and update
their handling in unreachable function removal and inliner too. */
|| (DECL_VIRTUAL_P (decl) && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
cgraph_mark_reachable_node (node);
/* If we've not yet emitted decl, tell the debug info about it. */
if (!TREE_ASM_WRITTEN (decl))
(*debug_hooks->deferred_inline_function) (decl);
/* Possibly warn about unused parameters. */
if (warn_unused_parameter)
do_warn_unused_parameter (decl);
if (!nested)
ggc_collect ();
}
/* C99 extern inline keywords allow changing of declaration after function
has been finalized. We need to re-decide if we want to mark the function as
needed then. */
void
cgraph_mark_if_needed (tree decl)
{
struct cgraph_node *node = cgraph_node (decl);
if (node->local.finalized && cgraph_decide_is_function_needed (node, decl))
cgraph_mark_needed_node (node);
}
/* Return TRUE if NODE2 is equivalent to NODE or its clone. */
static bool
clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
{
while (node != node2 && node2)
node2 = node2->clone_of;
return node2 != NULL;
}
/* Verify edge E count and frequency. */
static bool
verify_edge_count_and_frequency (struct cgraph_edge *e)
{
bool error_found = false;
if (e->count < 0)
{
error ("caller edge count is negative");
error_found = true;
}
if (e->frequency < 0)
{
error ("caller edge frequency is negative");
error_found = true;
}
if (e->frequency > CGRAPH_FREQ_MAX)
{
error ("caller edge frequency is too large");
error_found = true;
}
if (gimple_has_body_p (e->caller->decl)
&& !e->caller->global.inlined_to
&& (e->frequency
!= compute_call_stmt_bb_frequency (e->caller->decl,
gimple_bb (e->call_stmt))))
{
error ("caller edge frequency %i does not match BB frequency %i",
e->frequency,
compute_call_stmt_bb_frequency (e->caller->decl,
gimple_bb (e->call_stmt)));
error_found = true;
}
return error_found;
}
/* Switch to THIS_CFUN if needed and print STMT to stderr. */
static void
cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
{
/* debug_gimple_stmt needs correct cfun */
if (cfun != this_cfun)
set_cfun (this_cfun);
debug_gimple_stmt (stmt);
}
/* Verify cgraph nodes of given cgraph node. */
DEBUG_FUNCTION void
verify_cgraph_node (struct cgraph_node *node)
{
struct cgraph_edge *e;
struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
basic_block this_block;
gimple_stmt_iterator gsi;
bool error_found = false;
if (seen_error ())
return;
timevar_push (TV_CGRAPH_VERIFY);
for (e = node->callees; e; e = e->next_callee)
if (e->aux)
{
error ("aux field set for edge %s->%s",
identifier_to_locale (cgraph_node_name (e->caller)),
identifier_to_locale (cgraph_node_name (e->callee)));
error_found = true;
}
if (node->count < 0)
{
error ("execution count is negative");
error_found = true;
}
if (node->global.inlined_to && node->local.externally_visible)
{
error ("externally visible inline clone");
error_found = true;
}
if (node->global.inlined_to && node->address_taken)
{
error ("inline clone with address taken");
error_found = true;
}
if (node->global.inlined_to && node->needed)
{
error ("inline clone is needed");
error_found = true;
}
for (e = node->indirect_calls; e; e = e->next_callee)
{
if (e->aux)
{
error ("aux field set for indirect edge from %s",
identifier_to_locale (cgraph_node_name (e->caller)));
error_found = true;
}
if (!e->indirect_unknown_callee
|| !e->indirect_info)
{
error ("An indirect edge from %s is not marked as indirect or has "
"associated indirect_info, the corresponding statement is: ",
identifier_to_locale (cgraph_node_name (e->caller)));
cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
error_found = true;
}
}
for (e = node->callers; e; e = e->next_caller)
{
if (verify_edge_count_and_frequency (e))
error_found = true;
if (!e->inline_failed)
{
if (node->global.inlined_to
!= (e->caller->global.inlined_to
? e->caller->global.inlined_to : e->caller))
{
error ("inlined_to pointer is wrong");
error_found = true;
}
if (node->callers->next_caller)
{
error ("multiple inline callers");
error_found = true;
}
}
else
if (node->global.inlined_to)
{
error ("inlined_to pointer set for noninline callers");
error_found = true;
}
}
for (e = node->indirect_calls; e; e = e->next_callee)
if (verify_edge_count_and_frequency (e))
error_found = true;
if (!node->callers && node->global.inlined_to)
{
error ("inlined_to pointer is set but no predecessors found");
error_found = true;
}
if (node->global.inlined_to == node)
{
error ("inlined_to pointer refers to itself");
error_found = true;
}
if (!cgraph_node (node->decl))
{
error ("node not found in cgraph_hash");
error_found = true;
}
if (node->clone_of)
{
struct cgraph_node *n;
for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
if (n == node)
break;
if (!n)
{
error ("node has wrong clone_of");
error_found = true;
}
}
if (node->clones)
{
struct cgraph_node *n;
for (n = node->clones; n; n = n->next_sibling_clone)
if (n->clone_of != node)
break;
if (n)
{
error ("node has wrong clone list");
error_found = true;
}
}
if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
{
error ("node is in clone list but it is not clone");
error_found = true;
}
if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
{
error ("node has wrong prev_clone pointer");
error_found = true;
}
if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
{
error ("double linked list of clones corrupted");
error_found = true;
}
if (node->same_comdat_group)
{
struct cgraph_node *n = node->same_comdat_group;
if (!DECL_ONE_ONLY (node->decl))
{
error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
error_found = true;
}
if (n == node)
{
error ("node is alone in a comdat group");
error_found = true;
}
do
{
if (!n->same_comdat_group)
{
error ("same_comdat_group is not a circular list");
error_found = true;
break;
}
n = n->same_comdat_group;
}
while (n != node);
}
if (node->analyzed && gimple_has_body_p (node->decl)
&& !TREE_ASM_WRITTEN (node->decl)
&& (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
&& !flag_wpa)
{
if (this_cfun->cfg)
{
/* The nodes we're interested in are never shared, so walk
the tree ignoring duplicates. */
struct pointer_set_t *visited_nodes = pointer_set_create ();
/* Reach the trees by walking over the CFG, and note the
enclosing basic-blocks in the call edges. */
FOR_EACH_BB_FN (this_block, this_cfun)
for (gsi = gsi_start_bb (this_block);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
if (is_gimple_call (stmt))
{
struct cgraph_edge *e = cgraph_edge (node, stmt);
tree decl = gimple_call_fndecl (stmt);
if (e)
{
if (e->aux)
{
error ("shared call_stmt:");
cgraph_debug_gimple_stmt (this_cfun, stmt);
error_found = true;
}
if (!e->indirect_unknown_callee)
{
struct cgraph_node *n;
if (e->callee->same_body_alias)
{
error ("edge points to same body alias:");
debug_tree (e->callee->decl);
error_found = true;
}
else if (!e->callee->global.inlined_to
&& decl
&& cgraph_get_node (decl)
&& (e->callee->former_clone_of
!= cgraph_get_node (decl)->decl)
&& !clone_of_p (cgraph_node (decl),
e->callee))
{
error ("edge points to wrong declaration:");
debug_tree (e->callee->decl);
fprintf (stderr," Instead of:");
debug_tree (decl);
error_found = true;
}
else if (decl
&& (n = cgraph_get_node_or_alias (decl))
&& (n->same_body_alias
&& n->thunk.thunk_p))
{
error ("a call to thunk improperly represented "
"in the call graph:");
cgraph_debug_gimple_stmt (this_cfun, stmt);
error_found = true;
}
}
else if (decl)
{
error ("an indirect edge with unknown callee "
"corresponding to a call_stmt with "
"a known declaration:");
error_found = true;
cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
}
e->aux = (void *)1;
}
else if (decl)
{
error ("missing callgraph edge for call stmt:");
cgraph_debug_gimple_stmt (this_cfun, stmt);
error_found = true;
}
}
}
pointer_set_destroy (visited_nodes);
}
else
/* No CFG available?! */
gcc_unreachable ();
for (e = node->callees; e; e = e->next_callee)
{
if (!e->aux)
{
error ("edge %s->%s has no corresponding call_stmt",
identifier_to_locale (cgraph_node_name (e->caller)),
identifier_to_locale (cgraph_node_name (e->callee)));
cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
error_found = true;
}
e->aux = 0;
}
for (e = node->indirect_calls; e; e = e->next_callee)
{
if (!e->aux)
{
error ("an indirect edge from %s has no corresponding call_stmt",
identifier_to_locale (cgraph_node_name (e->caller)));
cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
error_found = true;
}
e->aux = 0;
}
}
if (error_found)
{
dump_cgraph_node (stderr, node);
internal_error ("verify_cgraph_node failed");
}
timevar_pop (TV_CGRAPH_VERIFY);
}
/* Verify whole cgraph structure. */
DEBUG_FUNCTION void
verify_cgraph (void)
{
struct cgraph_node *node;
if (seen_error ())
return;
for (node = cgraph_nodes; node; node = node->next)
verify_cgraph_node (node);
}
/* Output all asm statements we have stored up to be output. */
static void
cgraph_output_pending_asms (void)
{
struct cgraph_asm_node *can;
if (seen_error ())
return;
for (can = cgraph_asm_nodes; can; can = can->next)
assemble_asm (can->asm_str);
cgraph_asm_nodes = NULL;
}
/* Analyze the function scheduled to be output. */
static void
cgraph_analyze_function (struct cgraph_node *node)
{
tree save = current_function_decl;
tree decl = node->decl;
current_function_decl = decl;
push_cfun (DECL_STRUCT_FUNCTION (decl));
assign_assembler_name_if_neeeded (node->decl);
/* disregard_inline_limits affects topological order of the early optimization,
so we need to compute it ahead of rest of inline parameters. */
node->local.disregard_inline_limits
= DECL_DISREGARD_INLINE_LIMITS (node->decl);
/* Make sure to gimplify bodies only once. During analyzing a
function we lower it, which will require gimplified nested
functions, so we can end up here with an already gimplified
body. */
if (!gimple_body (decl))
gimplify_function_tree (decl);
dump_function (TDI_generic, decl);
cgraph_lower_function (node);
node->analyzed = true;
pop_cfun ();
current_function_decl = save;
}
/* Process attributes common for vars and functions. */
static void
process_common_attributes (tree decl)
{
tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
{
warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
"%<weakref%> attribute should be accompanied with"
" an %<alias%> attribute");
DECL_WEAK (decl) = 0;
DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
DECL_ATTRIBUTES (decl));
}
}
/* Look for externally_visible and used attributes and mark cgraph nodes
accordingly.
We cannot mark the nodes at the point the attributes are processed (in
handle_*_attribute) because the copy of the declarations available at that
point may not be canonical. For example, in:
void f();
void f() __attribute__((used));
the declaration we see in handle_used_attribute will be the second
declaration -- but the front end will subsequently merge that declaration
with the original declaration and discard the second declaration.
Furthermore, we can't mark these nodes in cgraph_finalize_function because:
void f() {}
void f() __attribute__((externally_visible));
is valid.
So, we walk the nodes at the end of the translation unit, applying the
attributes at that point. */
static void
process_function_and_variable_attributes (struct cgraph_node *first,
struct varpool_node *first_var)
{
struct cgraph_node *node;
struct varpool_node *vnode;
for (node = cgraph_nodes; node != first; node = node->next)
{
tree decl = node->decl;
if (DECL_PRESERVE_P (decl))
cgraph_mark_needed_node (node);
if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
&& lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
&& TREE_PUBLIC (node->decl))
{
if (node->local.finalized)
cgraph_mark_needed_node (node);
}
else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
{
if (! TREE_PUBLIC (node->decl))
warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
"%<externally_visible%>"
" attribute have effect only on public objects");
else if (node->local.finalized)
cgraph_mark_needed_node (node);
}
if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
&& node->local.finalized)
{
warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
"%<weakref%> attribute ignored"
" because function is defined");
DECL_WEAK (decl) = 0;
DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
DECL_ATTRIBUTES (decl));
}
process_common_attributes (decl);
}
for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
{
tree decl = vnode->decl;
if (DECL_PRESERVE_P (decl))
{
vnode->force_output = true;
if (vnode->finalized)
varpool_mark_needed_node (vnode);
}
if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
&& lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
&& TREE_PUBLIC (vnode->decl))
{
if (vnode->finalized)
varpool_mark_needed_node (vnode);
}
else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
{
if (! TREE_PUBLIC (vnode->decl))
warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
"%<externally_visible%>"
" attribute have effect only on public objects");
else if (vnode->finalized)
varpool_mark_needed_node (vnode);
}
if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
&& vnode->finalized
&& DECL_INITIAL (decl))
{
warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
"%<weakref%> attribute ignored"
" because variable is initialized");
DECL_WEAK (decl) = 0;
DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
DECL_ATTRIBUTES (decl));
}
process_common_attributes (decl);
}
}
/* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
each reachable functions) and build cgraph.
The function can be called multiple times after inserting new nodes
into beginning of queue. Just the new part of queue is re-scanned then. */
static void
cgraph_analyze_functions (void)
{
/* Keep track of already processed nodes when called multiple times for
intermodule optimization. */
static struct cgraph_node *first_analyzed;
struct cgraph_node *first_processed = first_analyzed;
static struct varpool_node *first_analyzed_var;
struct cgraph_node *node, *next;
bitmap_obstack_initialize (NULL);
process_function_and_variable_attributes (first_processed,
first_analyzed_var);
first_processed = cgraph_nodes;
first_analyzed_var = varpool_nodes;
varpool_analyze_pending_decls ();
if (cgraph_dump_file)
{
fprintf (cgraph_dump_file, "Initial entry points:");
for (node = cgraph_nodes; node != first_analyzed; node = node->next)
if (node->needed)
fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
fprintf (cgraph_dump_file, "\n");
}
cgraph_process_new_functions ();
/* Propagate reachability flag and lower representation of all reachable
functions. In the future, lowering will introduce new functions and
new entry points on the way (by template instantiation and virtual
method table generation for instance). */
while (cgraph_nodes_queue)
{
struct cgraph_edge *edge;
tree decl = cgraph_nodes_queue->decl;
node = cgraph_nodes_queue;
cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
node->next_needed = NULL;
/* ??? It is possible to create extern inline function and later using
weak alias attribute to kill its body. See
gcc.c-torture/compile/20011119-1.c */
if (!DECL_STRUCT_FUNCTION (decl))
{
cgraph_reset_node (node);
continue;
}
if (!node->analyzed)
cgraph_analyze_function (node);
for (edge = node->callees; edge; edge = edge->next_callee)
if (!edge->callee->reachable)
cgraph_mark_reachable_node (edge->callee);
if (node->same_comdat_group)
{
for (next = node->same_comdat_group;
next != node;
next = next->same_comdat_group)
cgraph_mark_reachable_node (next);
}
/* If decl is a clone of an abstract function, mark that abstract
function so that we don't release its body. The DECL_INITIAL() of that
abstract function declaration will be later needed to output debug info. */
if (DECL_ABSTRACT_ORIGIN (decl))