xref: /netbsd/external/gpl3/gcc/dist/gcc/cgraphunit.cc (revision f0fbc68b)
1 /* Driver of optimization process
2    Copyright (C) 2003-2022 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 /* This module implements main driver of compilation process.
22 
23    The main scope of this file is to act as an interface in between
24    tree based frontends and the backend.
25 
26    The front-end is supposed to use following functionality:
27 
28     - finalize_function
29 
30       This function is called once front-end has parsed whole body of function
31       and it is certain that the function body nor the declaration will change.
32 
33       (There is one exception needed for implementing GCC extern inline
34 	function.)
35 
36     - varpool_finalize_decl
37 
38       This function has same behavior as the above but is used for static
39       variables.
40 
41     - add_asm_node
42 
43       Insert new toplevel ASM statement
44 
45     - finalize_compilation_unit
46 
47       This function is called once (source level) compilation unit is finalized
48       and it will no longer change.
49 
50       The symbol table is constructed starting from the trivially needed
51       symbols finalized by the frontend.  Functions are lowered into
52       GIMPLE representation and callgraph/reference lists are constructed.
53       Those are used to discover other necessary functions and variables.
54 
55       At the end the bodies of unreachable functions are removed.
56 
57       The function can be called multiple times when multiple source level
58       compilation units are combined.
59 
60     - compile
61 
62       This passes control to the back-end.  Optimizations are performed and
63       final assembler is generated.  This is done in the following way. Note
64       that with link time optimization the process is split into three
65       stages (compile time, linktime analysis and parallel linktime as
66       indicated bellow).
67 
68       Compile time:
69 
70 	1) Inter-procedural optimization.
71 	   (ipa_passes)
72 
73 	   This part is further split into:
74 
75 	   a) early optimizations. These are local passes executed in
76 	      the topological order on the callgraph.
77 
78 	      The purpose of early optimizations is to optimize away simple
79 	      things that may otherwise confuse IP analysis. Very simple
80 	      propagation across the callgraph is done i.e. to discover
81 	      functions without side effects and simple inlining is performed.
82 
83 	   b) early small interprocedural passes.
84 
85 	      Those are interprocedural passes executed only at compilation
86 	      time.  These include, for example, transactional memory lowering,
87 	      unreachable code removal and other simple transformations.
88 
89 	   c) IP analysis stage.  All interprocedural passes do their
90 	      analysis.
91 
92 	      Interprocedural passes differ from small interprocedural
93 	      passes by their ability to operate across whole program
94 	      at linktime.  Their analysis stage is performed early to
95 	      both reduce linking times and linktime memory usage by
96 	      not having to represent whole program in memory.
97 
98 	   d) LTO streaming.  When doing LTO, everything important gets
99 	      streamed into the object file.
100 
101        Compile time and or linktime analysis stage (WPA):
102 
103 	      At linktime units gets streamed back and symbol table is
104 	      merged.  Function bodies are not streamed in and not
105 	      available.
106 	   e) IP propagation stage.  All IP passes execute their
107 	      IP propagation. This is done based on the earlier analysis
108 	      without having function bodies at hand.
109 	   f) Ltrans streaming.  When doing WHOPR LTO, the program
110 	      is partitioned and streamed into multiple object files.
111 
112        Compile time and/or parallel linktime stage (ltrans)
113 
114 	      Each of the object files is streamed back and compiled
115 	      separately.  Now the function bodies becomes available
116 	      again.
117 
118 	 2) Virtual clone materialization
119 	    (cgraph_materialize_clone)
120 
121 	    IP passes can produce copies of existing functions (such
122 	    as versioned clones or inline clones) without actually
123 	    manipulating their bodies by creating virtual clones in
124 	    the callgraph. At this time the virtual clones are
125 	    turned into real functions
126 	 3) IP transformation
127 
128 	    All IP passes transform function bodies based on earlier
129 	    decision of the IP propagation.
130 
131 	 4) late small IP passes
132 
133 	    Simple IP passes working within single program partition.
134 
135 	 5) Expansion
136 	    (expand_all_functions)
137 
138 	    At this stage functions that needs to be output into
139 	    assembler are identified and compiled in topological order
140 	 6) Output of variables and aliases
141 	    Now it is known what variable references was not optimized
142 	    out and thus all variables are output to the file.
143 
144 	    Note that with -fno-toplevel-reorder passes 5 and 6
145 	    are combined together in cgraph_output_in_order.
146 
147    Finally there are functions to manipulate the callgraph from
148    backend.
149     - cgraph_add_new_function is used to add backend produced
150       functions introduced after the unit is finalized.
151       The functions are enqueue for later processing and inserted
152       into callgraph with cgraph_process_new_functions.
153 
154     - cgraph_function_versioning
155 
156       produces a copy of function into new one (a version)
157       and apply simple transformations
158 */
159 
160 #include "config.h"
161 #include "system.h"
162 #include "coretypes.h"
163 #include "backend.h"
164 #include "target.h"
165 #include "rtl.h"
166 #include "tree.h"
167 #include "gimple.h"
168 #include "cfghooks.h"
169 #include "regset.h"     /* FIXME: For reg_obstack.  */
170 #include "alloc-pool.h"
171 #include "tree-pass.h"
172 #include "stringpool.h"
173 #include "gimple-ssa.h"
174 #include "cgraph.h"
175 #include "coverage.h"
176 #include "lto-streamer.h"
177 #include "fold-const.h"
178 #include "varasm.h"
179 #include "stor-layout.h"
180 #include "output.h"
181 #include "cfgcleanup.h"
182 #include "gimple-fold.h"
183 #include "gimplify.h"
184 #include "gimple-iterator.h"
185 #include "gimplify-me.h"
186 #include "tree-cfg.h"
187 #include "tree-into-ssa.h"
188 #include "tree-ssa.h"
189 #include "langhooks.h"
190 #include "toplev.h"
191 #include "debug.h"
192 #include "symbol-summary.h"
193 #include "tree-vrp.h"
194 #include "ipa-prop.h"
195 #include "gimple-pretty-print.h"
196 #include "plugin.h"
197 #include "ipa-fnsummary.h"
198 #include "ipa-utils.h"
199 #include "except.h"
200 #include "cfgloop.h"
201 #include "context.h"
202 #include "pass_manager.h"
203 #include "tree-nested.h"
204 #include "dbgcnt.h"
205 #include "lto-section-names.h"
206 #include "stringpool.h"
207 #include "attribs.h"
208 #include "ipa-inline.h"
209 #include "omp-offload.h"
210 #include "symtab-thunks.h"
211 
212 /* Queue of cgraph nodes scheduled to be added into cgraph.  This is a
213    secondary queue used during optimization to accommodate passes that
214    may generate new functions that need to be optimized and expanded.  */
215 vec<cgraph_node *> cgraph_new_nodes;
216 
217 static void expand_all_functions (void);
218 static void mark_functions_to_output (void);
219 static void handle_alias_pairs (void);
220 
221 /* Return true if this symbol is a function from the C frontend specified
222    directly in RTL form (with "__RTL").  */
223 
224 bool
native_rtl_p() const225 symtab_node::native_rtl_p () const
226 {
227   if (TREE_CODE (decl) != FUNCTION_DECL)
228     return false;
229   if (!DECL_STRUCT_FUNCTION (decl))
230     return false;
231   return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
232 }
233 
234 /* Determine if symbol declaration is needed.  That is, visible to something
235    either outside this translation unit, something magic in the system
236    configury */
237 bool
needed_p(void)238 symtab_node::needed_p (void)
239 {
240   /* Double check that no one output the function into assembly file
241      early.  */
242   if (!native_rtl_p ())
243       gcc_checking_assert
244 	(!DECL_ASSEMBLER_NAME_SET_P (decl)
245 	 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
246 
247   if (!definition)
248     return false;
249 
250   if (DECL_EXTERNAL (decl))
251     return false;
252 
253   /* If the user told us it is used, then it must be so.  */
254   if (force_output)
255     return true;
256 
257   /* ABI forced symbols are needed when they are external.  */
258   if (forced_by_abi && TREE_PUBLIC (decl))
259     return true;
260 
261   /* Keep constructors, destructors and virtual functions.  */
262    if (TREE_CODE (decl) == FUNCTION_DECL
263        && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
264     return true;
265 
266   /* Externally visible variables must be output.  The exception is
267      COMDAT variables that must be output only when they are needed.  */
268   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
269     return true;
270 
271   return false;
272 }
273 
274 /* Head and terminator of the queue of nodes to be processed while building
275    callgraph.  */
276 
277 static symtab_node symtab_terminator (SYMTAB_SYMBOL);
278 static symtab_node *queued_nodes = &symtab_terminator;
279 
280 /* Add NODE to queue starting at QUEUED_NODES.
281    The queue is linked via AUX pointers and terminated by pointer to 1.  */
282 
283 static void
enqueue_node(symtab_node * node)284 enqueue_node (symtab_node *node)
285 {
286   if (node->aux)
287     return;
288   gcc_checking_assert (queued_nodes);
289   node->aux = queued_nodes;
290   queued_nodes = node;
291 }
292 
293 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
294    functions into callgraph in a way so they look like ordinary reachable
295    functions inserted into callgraph already at construction time.  */
296 
297 void
process_new_functions(void)298 symbol_table::process_new_functions (void)
299 {
300   tree fndecl;
301 
302   if (!cgraph_new_nodes.exists ())
303     return;
304 
305   handle_alias_pairs ();
306   /*  Note that this queue may grow as its being processed, as the new
307       functions may generate new ones.  */
308   for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
309     {
310       cgraph_node *node = cgraph_new_nodes[i];
311       fndecl = node->decl;
312       switch (state)
313 	{
314 	case CONSTRUCTION:
315 	  /* At construction time we just need to finalize function and move
316 	     it into reachable functions list.  */
317 
318 	  cgraph_node::finalize_function (fndecl, false);
319 	  call_cgraph_insertion_hooks (node);
320 	  enqueue_node (node);
321 	  break;
322 
323 	case IPA:
324 	case IPA_SSA:
325 	case IPA_SSA_AFTER_INLINING:
326 	  /* When IPA optimization already started, do all essential
327 	     transformations that has been already performed on the whole
328 	     cgraph but not on this function.  */
329 
330 	  gimple_register_cfg_hooks ();
331 	  if (!node->analyzed)
332 	    node->analyze ();
333 	  push_cfun (DECL_STRUCT_FUNCTION (fndecl));
334 	  if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
335 	      && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
336 	    {
337 	      bool summaried_computed = ipa_fn_summaries != NULL;
338 	      g->get_passes ()->execute_early_local_passes ();
339 	      /* Early passes compute inline parameters to do inlining
340 		 and splitting.  This is redundant for functions added late.
341 		 Just throw away whatever it did.  */
342 	      if (!summaried_computed)
343 		{
344 		  ipa_free_fn_summary ();
345 		  ipa_free_size_summary ();
346 		}
347 	    }
348 	  else if (ipa_fn_summaries != NULL)
349 	    compute_fn_summary (node, true);
350 	  free_dominance_info (CDI_POST_DOMINATORS);
351 	  free_dominance_info (CDI_DOMINATORS);
352 	  pop_cfun ();
353 	  call_cgraph_insertion_hooks (node);
354 	  break;
355 
356 	case EXPANSION:
357 	  /* Functions created during expansion shall be compiled
358 	     directly.  */
359 	  node->process = 0;
360 	  call_cgraph_insertion_hooks (node);
361 	  node->expand ();
362 	  break;
363 
364 	default:
365 	  gcc_unreachable ();
366 	  break;
367 	}
368     }
369 
370   cgraph_new_nodes.release ();
371 }
372 
373 /* As an GCC extension we allow redefinition of the function.  The
374    semantics when both copies of bodies differ is not well defined.
375    We replace the old body with new body so in unit at a time mode
376    we always use new body, while in normal mode we may end up with
377    old body inlined into some functions and new body expanded and
378    inlined in others.
379 
380    ??? It may make more sense to use one body for inlining and other
381    body for expanding the function but this is difficult to do.  */
382 
383 void
reset(void)384 cgraph_node::reset (void)
385 {
386   /* If process is set, then we have already begun whole-unit analysis.
387      This is *not* testing for whether we've already emitted the function.
388      That case can be sort-of legitimately seen with real function redefinition
389      errors.  I would argue that the front end should never present us with
390      such a case, but don't enforce that for now.  */
391   gcc_assert (!process);
392 
393   /* Reset our data structures so we can analyze the function again.  */
394   inlined_to = NULL;
395   memset (&rtl, 0, sizeof (rtl));
396   analyzed = false;
397   definition = false;
398   alias = false;
399   transparent_alias = false;
400   weakref = false;
401   cpp_implicit_alias = false;
402 
403   remove_callees ();
404   remove_all_references ();
405 }
406 
407 /* Return true when there are references to the node.  INCLUDE_SELF is
408    true if a self reference counts as a reference.  */
409 
410 bool
referred_to_p(bool include_self)411 symtab_node::referred_to_p (bool include_self)
412 {
413   ipa_ref *ref = NULL;
414 
415   /* See if there are any references at all.  */
416   if (iterate_referring (0, ref))
417     return true;
418   /* For functions check also calls.  */
419   cgraph_node *cn = dyn_cast <cgraph_node *> (this);
420   if (cn && cn->callers)
421     {
422       if (include_self)
423 	return true;
424       for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
425 	if (e->caller != this)
426 	  return true;
427     }
428   return false;
429 }
430 
431 /* DECL has been parsed.  Take it, queue it, compile it at the whim of the
432    logic in effect.  If NO_COLLECT is true, then our caller cannot stand to have
433    the garbage collector run at the moment.  We would need to either create
434    a new GC context, or just not compile right now.  */
435 
436 void
finalize_function(tree decl,bool no_collect)437 cgraph_node::finalize_function (tree decl, bool no_collect)
438 {
439   cgraph_node *node = cgraph_node::get_create (decl);
440 
441   if (node->definition)
442     {
443       /* Nested functions should only be defined once.  */
444       gcc_assert (!DECL_CONTEXT (decl)
445 		  || TREE_CODE (DECL_CONTEXT (decl)) !=	FUNCTION_DECL);
446       node->reset ();
447       node->redefined_extern_inline = true;
448     }
449 
450   /* Set definition first before calling notice_global_symbol so that
451      it is available to notice_global_symbol.  */
452   node->definition = true;
453   notice_global_symbol (decl);
454   node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
455   node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
456   if (!flag_toplevel_reorder)
457     node->no_reorder = true;
458 
459   /* With -fkeep-inline-functions we are keeping all inline functions except
460      for extern inline ones.  */
461   if (flag_keep_inline_functions
462       && DECL_DECLARED_INLINE_P (decl)
463       && !DECL_EXTERNAL (decl)
464       && !DECL_DISREGARD_INLINE_LIMITS (decl))
465     node->force_output = 1;
466 
467   /* __RTL functions were already output as soon as they were parsed (due
468      to the large amount of global state in the backend).
469      Mark such functions as "force_output" to reflect the fact that they
470      will be in the asm file when considering the symbols they reference.
471      The attempt to output them later on will bail out immediately.  */
472   if (node->native_rtl_p ())
473     node->force_output = 1;
474 
475   /* When not optimizing, also output the static functions. (see
476      PR24561), but don't do so for always_inline functions, functions
477      declared inline and nested functions.  These were optimized out
478      in the original implementation and it is unclear whether we want
479      to change the behavior here.  */
480   if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
481 	|| node->no_reorder)
482        && !node->cpp_implicit_alias
483        && !DECL_DISREGARD_INLINE_LIMITS (decl)
484        && !DECL_DECLARED_INLINE_P (decl)
485        && !(DECL_CONTEXT (decl)
486 	    && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
487       && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
488     node->force_output = 1;
489 
490   /* If we've not yet emitted decl, tell the debug info about it.  */
491   if (!TREE_ASM_WRITTEN (decl))
492     (*debug_hooks->deferred_inline_function) (decl);
493 
494   if (!no_collect)
495     ggc_collect ();
496 
497   if (symtab->state == CONSTRUCTION
498       && (node->needed_p () || node->referred_to_p ()))
499     enqueue_node (node);
500 }
501 
502 /* Add the function FNDECL to the call graph.
503    Unlike finalize_function, this function is intended to be used
504    by middle end and allows insertion of new function at arbitrary point
505    of compilation.  The function can be either in high, low or SSA form
506    GIMPLE.
507 
508    The function is assumed to be reachable and have address taken (so no
509    API breaking optimizations are performed on it).
510 
511    Main work done by this function is to enqueue the function for later
512    processing to avoid need the passes to be re-entrant.  */
513 
514 void
add_new_function(tree fndecl,bool lowered)515 cgraph_node::add_new_function (tree fndecl, bool lowered)
516 {
517   gcc::pass_manager *passes = g->get_passes ();
518   cgraph_node *node;
519 
520   if (dump_file)
521     {
522       struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
523       const char *function_type = ((gimple_has_body_p (fndecl))
524 				   ? (lowered
525 				      ? (gimple_in_ssa_p (fn)
526 					 ? "ssa gimple"
527 					 : "low gimple")
528 				      : "high gimple")
529 				   : "to-be-gimplified");
530       fprintf (dump_file,
531 	       "Added new %s function %s to callgraph\n",
532 	       function_type,
533 	       fndecl_name (fndecl));
534     }
535 
536   switch (symtab->state)
537     {
538       case PARSING:
539 	cgraph_node::finalize_function (fndecl, false);
540 	break;
541       case CONSTRUCTION:
542 	/* Just enqueue function to be processed at nearest occurrence.  */
543 	node = cgraph_node::get_create (fndecl);
544 	if (lowered)
545 	  node->lowered = true;
546 	cgraph_new_nodes.safe_push (node);
547         break;
548 
549       case IPA:
550       case IPA_SSA:
551       case IPA_SSA_AFTER_INLINING:
552       case EXPANSION:
553 	/* Bring the function into finalized state and enqueue for later
554 	   analyzing and compilation.  */
555 	node = cgraph_node::get_create (fndecl);
556 	node->local = false;
557 	node->definition = true;
558 	node->semantic_interposition = opt_for_fn (fndecl,
559 						   flag_semantic_interposition);
560 	node->force_output = true;
561 	if (TREE_PUBLIC (fndecl))
562 	  node->externally_visible = true;
563 	if (!lowered && symtab->state == EXPANSION)
564 	  {
565 	    push_cfun (DECL_STRUCT_FUNCTION (fndecl));
566 	    gimple_register_cfg_hooks ();
567 	    bitmap_obstack_initialize (NULL);
568 	    execute_pass_list (cfun, passes->all_lowering_passes);
569 	    passes->execute_early_local_passes ();
570 	    bitmap_obstack_release (NULL);
571 	    pop_cfun ();
572 
573 	    lowered = true;
574 	  }
575 	if (lowered)
576 	  node->lowered = true;
577 	cgraph_new_nodes.safe_push (node);
578         break;
579 
580       case FINISHED:
581 	/* At the very end of compilation we have to do all the work up
582 	   to expansion.  */
583 	node = cgraph_node::create (fndecl);
584 	if (lowered)
585 	  node->lowered = true;
586 	node->definition = true;
587 	node->semantic_interposition = opt_for_fn (fndecl,
588 						   flag_semantic_interposition);
589 	node->analyze ();
590 	push_cfun (DECL_STRUCT_FUNCTION (fndecl));
591 	gimple_register_cfg_hooks ();
592 	bitmap_obstack_initialize (NULL);
593 	if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
594 	  g->get_passes ()->execute_early_local_passes ();
595 	bitmap_obstack_release (NULL);
596 	pop_cfun ();
597 	node->expand ();
598 	break;
599 
600       default:
601 	gcc_unreachable ();
602     }
603 
604   /* Set a personality if required and we already passed EH lowering.  */
605   if (lowered
606       && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
607 	  == eh_personality_lang))
608     DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
609 }
610 
611 /* Analyze the function scheduled to be output.  */
612 void
analyze(void)613 cgraph_node::analyze (void)
614 {
615   if (native_rtl_p ())
616     {
617       analyzed = true;
618       return;
619     }
620 
621   tree decl = this->decl;
622   location_t saved_loc = input_location;
623   input_location = DECL_SOURCE_LOCATION (decl);
624   semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
625 
626   if (thunk)
627     {
628       thunk_info *info = thunk_info::get (this);
629       cgraph_node *t = cgraph_node::get (info->alias);
630 
631       create_edge (t, NULL, t->count);
632       callees->can_throw_external = !TREE_NOTHROW (t->decl);
633       /* Target code in expand_thunk may need the thunk's target
634 	 to be analyzed, so recurse here.  */
635       if (!t->analyzed && t->definition)
636 	t->analyze ();
637       if (t->alias)
638 	{
639 	  t = t->get_alias_target ();
640 	  if (!t->analyzed && t->definition)
641 	    t->analyze ();
642 	}
643       bool ret = expand_thunk (this, false, false);
644       thunk_info::get (this)->alias = NULL;
645       if (!ret)
646 	return;
647     }
648   if (alias)
649     resolve_alias (cgraph_node::get (alias_target), transparent_alias);
650   else if (dispatcher_function)
651     {
652       /* Generate the dispatcher body of multi-versioned functions.  */
653       cgraph_function_version_info *dispatcher_version_info
654 	= function_version ();
655       if (dispatcher_version_info != NULL
656           && (dispatcher_version_info->dispatcher_resolver
657 	      == NULL_TREE))
658 	{
659 	  tree resolver = NULL_TREE;
660 	  gcc_assert (targetm.generate_version_dispatcher_body);
661 	  resolver = targetm.generate_version_dispatcher_body (this);
662 	  gcc_assert (resolver != NULL_TREE);
663 	}
664     }
665   else
666     {
667       push_cfun (DECL_STRUCT_FUNCTION (decl));
668 
669       assign_assembler_name_if_needed (decl);
670 
671       /* Make sure to gimplify bodies only once.  During analyzing a
672 	 function we lower it, which will require gimplified nested
673 	 functions, so we can end up here with an already gimplified
674 	 body.  */
675       if (!gimple_has_body_p (decl))
676 	gimplify_function_tree (decl);
677 
678       /* Lower the function.  */
679       if (!lowered)
680 	{
681 	  if (first_nested_function (this))
682 	    lower_nested_functions (decl);
683 
684 	  gimple_register_cfg_hooks ();
685 	  bitmap_obstack_initialize (NULL);
686 	  execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
687 	  compact_blocks ();
688 	  bitmap_obstack_release (NULL);
689 	  lowered = true;
690 	}
691 
692       pop_cfun ();
693     }
694   analyzed = true;
695 
696   input_location = saved_loc;
697 }
698 
699 /* C++ frontend produce same body aliases all over the place, even before PCH
700    gets streamed out. It relies on us linking the aliases with their function
701    in order to do the fixups, but ipa-ref is not PCH safe.  Consequently we
702    first produce aliases without links, but once C++ FE is sure he won't stream
703    PCH we build the links via this function.  */
704 
705 void
process_same_body_aliases(void)706 symbol_table::process_same_body_aliases (void)
707 {
708   symtab_node *node;
709   FOR_EACH_SYMBOL (node)
710     if (node->cpp_implicit_alias && !node->analyzed)
711       node->resolve_alias
712 	(VAR_P (node->alias_target)
713 	 ? (symtab_node *)varpool_node::get_create (node->alias_target)
714 	 : (symtab_node *)cgraph_node::get_create (node->alias_target));
715   cpp_implicit_aliases_done = true;
716 }
717 
718 /* Process a symver attribute.  */
719 
720 static void
process_symver_attribute(symtab_node * n)721 process_symver_attribute (symtab_node *n)
722 {
723   tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
724 
725   for (; value != NULL; value = TREE_CHAIN (value))
726     {
727       /* Starting from bintuils 2.35 gas supports:
728 	  # Assign foo to bar@V1 and baz@V2.
729 	  .symver foo, bar@V1
730 	  .symver foo, baz@V2
731       */
732       const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
733       if (strcmp (purpose, "symver") != 0)
734 	continue;
735 
736       tree symver = get_identifier_with_length
737 	(TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
738 	 TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
739       symtab_node *def = symtab_node::get_for_asmname (symver);
740 
741       if (def)
742 	{
743 	  error_at (DECL_SOURCE_LOCATION (n->decl),
744 		    "duplicate definition of a symbol version");
745 	  inform (DECL_SOURCE_LOCATION (def->decl),
746 		  "same version was previously defined here");
747 	  return;
748 	}
749       if (!n->definition)
750 	{
751 	  error_at (DECL_SOURCE_LOCATION (n->decl),
752 		    "symbol needs to be defined to have a version");
753 	  return;
754 	}
755       if (DECL_COMMON (n->decl))
756 	{
757 	  error_at (DECL_SOURCE_LOCATION (n->decl),
758 		    "common symbol cannot be versioned");
759 	  return;
760 	}
761       if (DECL_COMDAT (n->decl))
762 	{
763 	  error_at (DECL_SOURCE_LOCATION (n->decl),
764 		    "comdat symbol cannot be versioned");
765 	  return;
766 	}
767       if (n->weakref)
768 	{
769 	  error_at (DECL_SOURCE_LOCATION (n->decl),
770 		    "%<weakref%> cannot be versioned");
771 	  return;
772 	}
773       if (!TREE_PUBLIC (n->decl))
774 	{
775 	  error_at (DECL_SOURCE_LOCATION (n->decl),
776 		    "versioned symbol must be public");
777 	  return;
778 	}
779       if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
780 	{
781 	  error_at (DECL_SOURCE_LOCATION (n->decl),
782 		    "versioned symbol must have default visibility");
783 	  return;
784 	}
785 
786       /* Create new symbol table entry representing the version.  */
787       tree new_decl = copy_node (n->decl);
788 
789       DECL_INITIAL (new_decl) = NULL_TREE;
790       if (TREE_CODE (new_decl) == FUNCTION_DECL)
791 	DECL_STRUCT_FUNCTION (new_decl) = NULL;
792       SET_DECL_ASSEMBLER_NAME (new_decl, symver);
793       TREE_PUBLIC (new_decl) = 1;
794       DECL_ATTRIBUTES (new_decl) = NULL;
795 
796       symtab_node *symver_node = symtab_node::get_create (new_decl);
797       symver_node->alias = true;
798       symver_node->definition = true;
799       symver_node->symver = true;
800       symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
801       symver_node->analyzed = true;
802     }
803 }
804 
805 /* Process attributes common for vars and functions.  */
806 
807 static void
process_common_attributes(symtab_node * node,tree decl)808 process_common_attributes (symtab_node *node, tree decl)
809 {
810   tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
811 
812   if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
813     {
814       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
815 		  "%<weakref%> attribute should be accompanied with"
816 		  " an %<alias%> attribute");
817       DECL_WEAK (decl) = 0;
818       DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
819 						 DECL_ATTRIBUTES (decl));
820     }
821 
822   if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
823     node->no_reorder = 1;
824   process_symver_attribute (node);
825 }
826 
827 /* Look for externally_visible and used attributes and mark cgraph nodes
828    accordingly.
829 
830    We cannot mark the nodes at the point the attributes are processed (in
831    handle_*_attribute) because the copy of the declarations available at that
832    point may not be canonical.  For example, in:
833 
834     void f();
835     void f() __attribute__((used));
836 
837    the declaration we see in handle_used_attribute will be the second
838    declaration -- but the front end will subsequently merge that declaration
839    with the original declaration and discard the second declaration.
840 
841    Furthermore, we can't mark these nodes in finalize_function because:
842 
843     void f() {}
844     void f() __attribute__((externally_visible));
845 
846    is valid.
847 
848    So, we walk the nodes at the end of the translation unit, applying the
849    attributes at that point.  */
850 
851 static void
process_function_and_variable_attributes(cgraph_node * first,varpool_node * first_var)852 process_function_and_variable_attributes (cgraph_node *first,
853                                           varpool_node *first_var)
854 {
855   cgraph_node *node;
856   varpool_node *vnode;
857 
858   for (node = symtab->first_function (); node != first;
859        node = symtab->next_function (node))
860     {
861       tree decl = node->decl;
862 
863       if (node->alias
864 	  && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
865 	{
866 	  tree tdecl = node->get_alias_target_tree ();
867 	  if (!tdecl || !DECL_P (tdecl)
868 	      || !lookup_attribute ("flatten", DECL_ATTRIBUTES (tdecl)))
869 	    warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
870 			"%<flatten%> attribute is ignored on aliases");
871 	}
872       if (DECL_PRESERVE_P (decl))
873 	node->mark_force_output ();
874       else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
875 	{
876 	  if (! TREE_PUBLIC (node->decl))
877 	    warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
878 			"%<externally_visible%>"
879 			" attribute have effect only on public objects");
880 	}
881       if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
882 	  && node->definition
883 	  && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
884 	{
885 	  /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
886 	     function declarations; DECL_INITIAL is non-null for invalid
887 	     weakref functions that are also defined.  */
888 	  warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
889 		      "%<weakref%> attribute ignored"
890 		      " because function is defined");
891 	  DECL_WEAK (decl) = 0;
892 	  DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
893 						     DECL_ATTRIBUTES (decl));
894 	  DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
895 						     DECL_ATTRIBUTES (decl));
896 	  node->alias = false;
897 	  node->weakref = false;
898 	  node->transparent_alias = false;
899 	}
900       else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
901 	  && node->definition
902 	  && !node->alias)
903 	warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
904 		    "%<alias%> attribute ignored"
905 		    " because function is defined");
906 
907       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
908 	  && !DECL_DECLARED_INLINE_P (decl)
909 	  /* redefining extern inline function makes it DECL_UNINLINABLE.  */
910 	  && !DECL_UNINLINABLE (decl))
911 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
912 		    "%<always_inline%> function might not be inlinable");
913 
914       process_common_attributes (node, decl);
915     }
916   for (vnode = symtab->first_variable (); vnode != first_var;
917        vnode = symtab->next_variable (vnode))
918     {
919       tree decl = vnode->decl;
920       if (DECL_EXTERNAL (decl)
921 	  && DECL_INITIAL (decl))
922 	varpool_node::finalize_decl (decl);
923       if (DECL_PRESERVE_P (decl))
924 	vnode->force_output = true;
925       else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
926 	{
927 	  if (! TREE_PUBLIC (vnode->decl))
928 	    warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
929 			"%<externally_visible%>"
930 			" attribute have effect only on public objects");
931 	}
932       if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
933 	  && vnode->definition
934 	  && DECL_INITIAL (decl))
935 	{
936 	  warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
937 		      "%<weakref%> attribute ignored"
938 		      " because variable is initialized");
939 	  DECL_WEAK (decl) = 0;
940 	  DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
941 						      DECL_ATTRIBUTES (decl));
942 	}
943       process_common_attributes (vnode, decl);
944     }
945 }
946 
947 /* Mark DECL as finalized.  By finalizing the declaration, frontend instruct the
948    middle end to output the variable to asm file, if needed or externally
949    visible.  */
950 
951 void
finalize_decl(tree decl)952 varpool_node::finalize_decl (tree decl)
953 {
954   varpool_node *node = varpool_node::get_create (decl);
955 
956   gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
957 
958   if (node->definition)
959     return;
960   /* Set definition first before calling notice_global_symbol so that
961      it is available to notice_global_symbol.  */
962   node->definition = true;
963   node->semantic_interposition = flag_semantic_interposition;
964   notice_global_symbol (decl);
965   if (!flag_toplevel_reorder)
966     node->no_reorder = true;
967   if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
968       /* Traditionally we do not eliminate static variables when not
969 	 optimizing and when not doing toplevel reorder.  */
970       || (node->no_reorder && !DECL_COMDAT (node->decl)
971 	  && !DECL_ARTIFICIAL (node->decl)))
972     node->force_output = true;
973 
974   if (symtab->state == CONSTRUCTION
975       && (node->needed_p () || node->referred_to_p ()))
976     enqueue_node (node);
977   if (symtab->state >= IPA_SSA)
978     node->analyze ();
979   /* Some frontends produce various interface variables after compilation
980      finished.  */
981   if (symtab->state == FINISHED
982       || (node->no_reorder
983 	  && symtab->state == EXPANSION))
984     node->assemble_decl ();
985 }
986 
987 /* EDGE is an polymorphic call.  Mark all possible targets as reachable
988    and if there is only one target, perform trivial devirtualization.
989    REACHABLE_CALL_TARGETS collects target lists we already walked to
990    avoid duplicate work.  */
991 
992 static void
walk_polymorphic_call_targets(hash_set<void * > * reachable_call_targets,cgraph_edge * edge)993 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
994 			       cgraph_edge *edge)
995 {
996   unsigned int i;
997   void *cache_token;
998   bool final;
999   vec <cgraph_node *>targets
1000     = possible_polymorphic_call_targets
1001 	(edge, &final, &cache_token);
1002 
1003   if (!reachable_call_targets->add (cache_token))
1004     {
1005       if (symtab->dump_file)
1006 	dump_possible_polymorphic_call_targets
1007 	  (symtab->dump_file, edge);
1008 
1009       for (i = 0; i < targets.length (); i++)
1010 	{
1011 	  /* Do not bother to mark virtual methods in anonymous namespace;
1012 	     either we will find use of virtual table defining it, or it is
1013 	     unused.  */
1014 	  if (targets[i]->definition
1015 	      && TREE_CODE
1016 		  (TREE_TYPE (targets[i]->decl))
1017 		   == METHOD_TYPE
1018 	      && !type_in_anonymous_namespace_p
1019 		   (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1020 	    enqueue_node (targets[i]);
1021 	}
1022     }
1023 
1024   /* Very trivial devirtualization; when the type is
1025      final or anonymous (so we know all its derivation)
1026      and there is only one possible virtual call target,
1027      make the edge direct.  */
1028   if (final)
1029     {
1030       if (targets.length () <= 1 && dbg_cnt (devirt))
1031 	{
1032 	  cgraph_node *target;
1033 	  if (targets.length () == 1)
1034 	    target = targets[0];
1035 	  else
1036 	    target = cgraph_node::create
1037 			(builtin_decl_implicit (BUILT_IN_UNREACHABLE));
1038 
1039 	  if (symtab->dump_file)
1040 	    {
1041 	      fprintf (symtab->dump_file,
1042 		       "Devirtualizing call: ");
1043 	      print_gimple_stmt (symtab->dump_file,
1044 				 edge->call_stmt, 0,
1045 				 TDF_SLIM);
1046 	    }
1047           if (dump_enabled_p ())
1048             {
1049 	      dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1050 			       "devirtualizing call in %s to %s\n",
1051 			       edge->caller->dump_name (),
1052 			       target->dump_name ());
1053 	    }
1054 
1055 	  edge = cgraph_edge::make_direct (edge, target);
1056 	  gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1057 
1058 	  if (symtab->dump_file)
1059 	    {
1060 	      fprintf (symtab->dump_file, "Devirtualized as: ");
1061 	      print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1062 	    }
1063 	}
1064     }
1065 }
1066 
1067 /* Issue appropriate warnings for the global declaration DECL.  */
1068 
1069 static void
check_global_declaration(symtab_node * snode)1070 check_global_declaration (symtab_node *snode)
1071 {
1072   const char *decl_file;
1073   tree decl = snode->decl;
1074 
1075   /* Warn about any function declared static but not defined.  We don't
1076      warn about variables, because many programs have static variables
1077      that exist only to get some text into the object file.  */
1078   if (TREE_CODE (decl) == FUNCTION_DECL
1079       && DECL_INITIAL (decl) == 0
1080       && DECL_EXTERNAL (decl)
1081       && ! DECL_ARTIFICIAL (decl)
1082       && ! TREE_PUBLIC (decl))
1083     {
1084       if (warning_suppressed_p (decl, OPT_Wunused))
1085 	;
1086       else if (snode->referred_to_p (/*include_self=*/false))
1087 	pedwarn (input_location, 0, "%q+F used but never defined", decl);
1088       else
1089 	warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
1090 				       "defined", decl);
1091       /* This symbol is effectively an "extern" declaration now.  */
1092       TREE_PUBLIC (decl) = 1;
1093     }
1094 
1095   /* Warn about static fns or vars defined but not used.  */
1096   if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1097        || (((warn_unused_variable && ! TREE_READONLY (decl))
1098 	    || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1099 		&& (warn_unused_const_variable == 2
1100 		    || (main_input_filename != NULL
1101 			&& (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1102 			&& filename_cmp (main_input_filename,
1103 					 decl_file) == 0))))
1104 	   && VAR_P (decl)))
1105       && ! DECL_IN_SYSTEM_HEADER (decl)
1106       && ! snode->referred_to_p (/*include_self=*/false)
1107       /* This TREE_USED check is needed in addition to referred_to_p
1108 	 above, because the `__unused__' attribute is not being
1109 	 considered for referred_to_p.  */
1110       && ! TREE_USED (decl)
1111       /* The TREE_USED bit for file-scope decls is kept in the identifier,
1112 	 to handle multiple external decls in different scopes.  */
1113       && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1114       && ! DECL_EXTERNAL (decl)
1115       && ! DECL_ARTIFICIAL (decl)
1116       && ! DECL_ABSTRACT_ORIGIN (decl)
1117       && ! TREE_PUBLIC (decl)
1118       /* A volatile variable might be used in some non-obvious way.  */
1119       && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1120       /* Global register variables must be declared to reserve them.  */
1121       && ! (VAR_P (decl) && DECL_REGISTER (decl))
1122       /* Global ctors and dtors are called by the runtime.  */
1123       && (TREE_CODE (decl) != FUNCTION_DECL
1124 	  || (!DECL_STATIC_CONSTRUCTOR (decl)
1125 	      && !DECL_STATIC_DESTRUCTOR (decl)))
1126       && (! VAR_P (decl) || !warning_suppressed_p (decl, OPT_Wunused_variable))
1127       /* Otherwise, ask the language.  */
1128       && lang_hooks.decls.warn_unused_global (decl))
1129     warning_at (DECL_SOURCE_LOCATION (decl),
1130 		(TREE_CODE (decl) == FUNCTION_DECL)
1131 		? OPT_Wunused_function
1132 		: (TREE_READONLY (decl)
1133 		   ? OPT_Wunused_const_variable_
1134 		   : OPT_Wunused_variable),
1135 		"%qD defined but not used", decl);
1136 }
1137 
1138 /* Discover all functions and variables that are trivially needed, analyze
1139    them as well as all functions and variables referred by them  */
1140 static cgraph_node *first_analyzed;
1141 static varpool_node *first_analyzed_var;
1142 
1143 /* FIRST_TIME is set to TRUE for the first time we are called for a
1144    translation unit from finalize_compilation_unit() or false
1145    otherwise.  */
1146 
1147 static void
analyze_functions(bool first_time)1148 analyze_functions (bool first_time)
1149 {
1150   /* Keep track of already processed nodes when called multiple times for
1151      intermodule optimization.  */
1152   cgraph_node *first_handled = first_analyzed;
1153   varpool_node *first_handled_var = first_analyzed_var;
1154   hash_set<void *> reachable_call_targets;
1155 
1156   symtab_node *node;
1157   symtab_node *next;
1158   int i;
1159   ipa_ref *ref;
1160   bool changed = true;
1161   location_t saved_loc = input_location;
1162 
1163   bitmap_obstack_initialize (NULL);
1164   symtab->state = CONSTRUCTION;
1165   input_location = UNKNOWN_LOCATION;
1166 
1167   thunk_info::process_early_thunks ();
1168 
1169   /* Ugly, but the fixup cannot happen at a time same body alias is created;
1170      C++ FE is confused about the COMDAT groups being right.  */
1171   if (symtab->cpp_implicit_aliases_done)
1172     FOR_EACH_SYMBOL (node)
1173       if (node->cpp_implicit_alias)
1174 	  node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1175   build_type_inheritance_graph ();
1176 
1177   if (flag_openmp && first_time)
1178     omp_discover_implicit_declare_target ();
1179 
1180   /* Analysis adds static variables that in turn adds references to new functions.
1181      So we need to iterate the process until it stabilize.  */
1182   while (changed)
1183     {
1184       changed = false;
1185       process_function_and_variable_attributes (first_analyzed,
1186 						first_analyzed_var);
1187 
1188       /* First identify the trivially needed symbols.  */
1189       for (node = symtab->first_symbol ();
1190 	   node != first_analyzed
1191 	   && node != first_analyzed_var; node = node->next)
1192 	{
1193 	  /* Convert COMDAT group designators to IDENTIFIER_NODEs.  */
1194 	  node->get_comdat_group_id ();
1195 	  if (node->needed_p ())
1196 	    {
1197 	      enqueue_node (node);
1198 	      if (!changed && symtab->dump_file)
1199 		fprintf (symtab->dump_file, "Trivially needed symbols:");
1200 	      changed = true;
1201 	      if (symtab->dump_file)
1202 		fprintf (symtab->dump_file, " %s", node->dump_asm_name ());
1203 	    }
1204 	  if (node == first_analyzed
1205 	      || node == first_analyzed_var)
1206 	    break;
1207 	}
1208       symtab->process_new_functions ();
1209       first_analyzed_var = symtab->first_variable ();
1210       first_analyzed = symtab->first_function ();
1211 
1212       if (changed && symtab->dump_file)
1213 	fprintf (symtab->dump_file, "\n");
1214 
1215       /* Lower representation, build callgraph edges and references for all trivially
1216          needed symbols and all symbols referred by them.  */
1217       while (queued_nodes != &symtab_terminator)
1218 	{
1219 	  changed = true;
1220 	  node = queued_nodes;
1221 	  queued_nodes = (symtab_node *)queued_nodes->aux;
1222 	  cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1223 	  if (cnode && cnode->definition)
1224 	    {
1225 	      cgraph_edge *edge;
1226 	      tree decl = cnode->decl;
1227 
1228 	      /* ??? It is possible to create extern inline function
1229 	      and later using weak alias attribute to kill its body.
1230 	      See gcc.c-torture/compile/20011119-1.c  */
1231 	      if (!DECL_STRUCT_FUNCTION (decl)
1232 		  && !cnode->alias
1233 		  && !cnode->thunk
1234 		  && !cnode->dispatcher_function)
1235 		{
1236 		  cnode->reset ();
1237 		  cnode->redefined_extern_inline = true;
1238 		  continue;
1239 		}
1240 
1241 	      if (!cnode->analyzed)
1242 		cnode->analyze ();
1243 
1244 	      for (edge = cnode->callees; edge; edge = edge->next_callee)
1245 		if (edge->callee->definition
1246 		    && (!DECL_EXTERNAL (edge->callee->decl)
1247 			/* When not optimizing, do not try to analyze extern
1248 			   inline functions.  Doing so is pointless.  */
1249 			|| opt_for_fn (edge->callee->decl, optimize)
1250 			/* Weakrefs needs to be preserved.  */
1251 			|| edge->callee->alias
1252 			/* always_inline functions are inlined even at -O0.  */
1253 		        || lookup_attribute
1254 				 ("always_inline",
1255 			          DECL_ATTRIBUTES (edge->callee->decl))
1256 			/* Multiversioned functions needs the dispatcher to
1257 			   be produced locally even for extern functions.  */
1258 			|| edge->callee->function_version ()))
1259 		   enqueue_node (edge->callee);
1260 	      if (opt_for_fn (cnode->decl, optimize)
1261 		  && opt_for_fn (cnode->decl, flag_devirtualize))
1262 		{
1263 		  cgraph_edge *next;
1264 
1265 		  for (edge = cnode->indirect_calls; edge; edge = next)
1266 		    {
1267 		      next = edge->next_callee;
1268 		      if (edge->indirect_info->polymorphic)
1269 			walk_polymorphic_call_targets (&reachable_call_targets,
1270 						       edge);
1271 		    }
1272 		}
1273 
1274 	      /* If decl is a clone of an abstract function,
1275 		 mark that abstract function so that we don't release its body.
1276 		 The DECL_INITIAL() of that abstract function declaration
1277 		 will be later needed to output debug info.  */
1278 	      if (DECL_ABSTRACT_ORIGIN (decl))
1279 		{
1280 		  cgraph_node *origin_node
1281 		    = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1282 		  origin_node->used_as_abstract_origin = true;
1283 		}
1284 	      /* Preserve a functions function context node.  It will
1285 		 later be needed to output debug info.  */
1286 	      if (tree fn = decl_function_context (decl))
1287 		{
1288 		  cgraph_node *origin_node = cgraph_node::get_create (fn);
1289 		  enqueue_node (origin_node);
1290 		}
1291 	    }
1292 	  else
1293 	    {
1294 	      varpool_node *vnode = dyn_cast <varpool_node *> (node);
1295 	      if (vnode && vnode->definition && !vnode->analyzed)
1296 		vnode->analyze ();
1297 	    }
1298 
1299 	  if (node->same_comdat_group)
1300 	    {
1301 	      symtab_node *next;
1302 	      for (next = node->same_comdat_group;
1303 		   next != node;
1304 		   next = next->same_comdat_group)
1305 		if (!next->comdat_local_p ())
1306 		  enqueue_node (next);
1307 	    }
1308 	  for (i = 0; node->iterate_reference (i, ref); i++)
1309 	    if (ref->referred->definition
1310 		&& (!DECL_EXTERNAL (ref->referred->decl)
1311 		    || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1312 			 && optimize)
1313 			|| (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1314 			    && opt_for_fn (ref->referred->decl, optimize))
1315 		    || node->alias
1316 		    || ref->referred->alias)))
1317 	      enqueue_node (ref->referred);
1318 	  symtab->process_new_functions ();
1319 	}
1320     }
1321   update_type_inheritance_graph ();
1322 
1323   /* Collect entry points to the unit.  */
1324   if (symtab->dump_file)
1325     {
1326       fprintf (symtab->dump_file, "\n\nInitial ");
1327       symtab->dump (symtab->dump_file);
1328     }
1329 
1330   if (first_time)
1331     {
1332       symtab_node *snode;
1333       FOR_EACH_SYMBOL (snode)
1334 	check_global_declaration (snode);
1335     }
1336 
1337   if (symtab->dump_file)
1338     fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1339 
1340   for (node = symtab->first_symbol ();
1341        node != first_handled
1342        && node != first_handled_var; node = next)
1343     {
1344       next = node->next;
1345       /* For symbols declared locally we clear TREE_READONLY when emitting
1346 	 the constructor (if one is needed).  For external declarations we can
1347 	 not safely assume that the type is readonly because we may be called
1348 	 during its construction.  */
1349       if (TREE_CODE (node->decl) == VAR_DECL
1350 	  && TYPE_P (TREE_TYPE (node->decl))
1351 	  && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1352 	  && DECL_EXTERNAL (node->decl))
1353 	TREE_READONLY (node->decl) = 0;
1354       if (!node->aux && !node->referred_to_p ())
1355 	{
1356 	  if (symtab->dump_file)
1357 	    fprintf (symtab->dump_file, " %s", node->dump_name ());
1358 
1359 	  /* See if the debugger can use anything before the DECL
1360 	     passes away.  Perhaps it can notice a DECL that is now a
1361 	     constant and can tag the early DIE with an appropriate
1362 	     attribute.
1363 
1364 	     Otherwise, this is the last chance the debug_hooks have
1365 	     at looking at optimized away DECLs, since
1366 	     late_global_decl will subsequently be called from the
1367 	     contents of the now pruned symbol table.  */
1368 	  if (VAR_P (node->decl)
1369 	      && !decl_function_context (node->decl))
1370 	    {
1371 	      /* We are reclaiming totally unreachable code and variables
1372 	         so they effectively appear as readonly.  Show that to
1373 		 the debug machinery.  */
1374 	      TREE_READONLY (node->decl) = 1;
1375 	      node->definition = false;
1376 	      (*debug_hooks->late_global_decl) (node->decl);
1377 	    }
1378 
1379 	  node->remove ();
1380 	  continue;
1381 	}
1382       if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1383 	{
1384 	  tree decl = node->decl;
1385 
1386 	  if (cnode->definition && !gimple_has_body_p (decl)
1387 	      && !cnode->alias
1388 	      && !cnode->thunk)
1389 	    cnode->reset ();
1390 
1391 	  gcc_assert (!cnode->definition || cnode->thunk
1392 		      || cnode->alias
1393 		      || gimple_has_body_p (decl)
1394 		      || cnode->native_rtl_p ());
1395 	  gcc_assert (cnode->analyzed == cnode->definition);
1396 	}
1397       node->aux = NULL;
1398     }
1399   for (;node; node = node->next)
1400     node->aux = NULL;
1401   first_analyzed = symtab->first_function ();
1402   first_analyzed_var = symtab->first_variable ();
1403   if (symtab->dump_file)
1404     {
1405       fprintf (symtab->dump_file, "\n\nReclaimed ");
1406       symtab->dump (symtab->dump_file);
1407     }
1408   bitmap_obstack_release (NULL);
1409   ggc_collect ();
1410   /* Initialize assembler name hash, in particular we want to trigger C++
1411      mangling and same body alias creation before we free DECL_ARGUMENTS
1412      used by it.  */
1413   if (!seen_error ())
1414     symtab->symtab_initialize_asm_name_hash ();
1415 
1416   input_location = saved_loc;
1417 }
1418 
1419 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1420    (which may be an ifunc resolver) and issue a diagnostic when they are
1421    not compatible according to language rules (plus a C++ extension for
1422    non-static member functions).  */
1423 
1424 static void
maybe_diag_incompatible_alias(tree alias,tree target)1425 maybe_diag_incompatible_alias (tree alias, tree target)
1426 {
1427   tree altype = TREE_TYPE (alias);
1428   tree targtype = TREE_TYPE (target);
1429 
1430   bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1431   tree funcptr = altype;
1432 
1433   if (ifunc)
1434     {
1435       /* Handle attribute ifunc first.  */
1436       if (TREE_CODE (altype) == METHOD_TYPE)
1437 	{
1438 	  /* Set FUNCPTR to the type of the alias target.  If the type
1439 	     is a non-static member function of class C, construct a type
1440 	     of an ordinary function taking C* as the first argument,
1441 	     followed by the member function argument list, and use it
1442 	     instead to check for incompatibility.  This conversion is
1443 	     not defined by the language but an extension provided by
1444 	     G++.  */
1445 
1446 	  tree rettype = TREE_TYPE (altype);
1447 	  tree args = TYPE_ARG_TYPES (altype);
1448 	  altype = build_function_type (rettype, args);
1449 	  funcptr = altype;
1450 	}
1451 
1452       targtype = TREE_TYPE (targtype);
1453 
1454       if (POINTER_TYPE_P (targtype))
1455 	{
1456 	  targtype = TREE_TYPE (targtype);
1457 
1458 	  /* Only issue Wattribute-alias for conversions to void* with
1459 	     -Wextra.  */
1460 	  if (VOID_TYPE_P (targtype) && !extra_warnings)
1461 	    return;
1462 
1463 	  /* Proceed to handle incompatible ifunc resolvers below.  */
1464 	}
1465       else
1466 	{
1467 	  funcptr = build_pointer_type (funcptr);
1468 
1469 	  error_at (DECL_SOURCE_LOCATION (target),
1470 		    "%<ifunc%> resolver for %qD must return %qT",
1471 		 alias, funcptr);
1472 	  inform (DECL_SOURCE_LOCATION (alias),
1473 		  "resolver indirect function declared here");
1474 	  return;
1475 	}
1476     }
1477 
1478   if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1479        || (prototype_p (altype)
1480 	   && prototype_p (targtype)
1481 	   && !types_compatible_p (altype, targtype))))
1482     {
1483       /* Warn for incompatibilities.  Avoid warning for functions
1484 	 without a prototype to make it possible to declare aliases
1485 	 without knowing the exact type, as libstdc++ does.  */
1486       if (ifunc)
1487 	{
1488 	  funcptr = build_pointer_type (funcptr);
1489 
1490 	  auto_diagnostic_group d;
1491 	  if (warning_at (DECL_SOURCE_LOCATION (target),
1492 			  OPT_Wattribute_alias_,
1493 			  "%<ifunc%> resolver for %qD should return %qT",
1494 			  alias, funcptr))
1495 	    inform (DECL_SOURCE_LOCATION (alias),
1496 		    "resolver indirect function declared here");
1497 	}
1498       else
1499 	{
1500 	  auto_diagnostic_group d;
1501 	  if (warning_at (DECL_SOURCE_LOCATION (alias),
1502 			    OPT_Wattribute_alias_,
1503 			    "%qD alias between functions of incompatible "
1504 			    "types %qT and %qT", alias, altype, targtype))
1505 	    inform (DECL_SOURCE_LOCATION (target),
1506 		    "aliased declaration here");
1507 	}
1508     }
1509 }
1510 
1511 /* Translate the ugly representation of aliases as alias pairs into nice
1512    representation in callgraph.  We don't handle all cases yet,
1513    unfortunately.  */
1514 
1515 static void
handle_alias_pairs(void)1516 handle_alias_pairs (void)
1517 {
1518   alias_pair *p;
1519   unsigned i;
1520 
1521   for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1522     {
1523       symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1524 
1525       /* Weakrefs with target not defined in current unit are easy to handle:
1526 	 they behave just as external variables except we need to note the
1527 	 alias flag to later output the weakref pseudo op into asm file.  */
1528       if (!target_node
1529 	  && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1530 	{
1531 	  symtab_node *node = symtab_node::get (p->decl);
1532 	  if (node)
1533 	    {
1534 	      node->alias_target = p->target;
1535 	      node->weakref = true;
1536 	      node->alias = true;
1537 	      node->transparent_alias = true;
1538 	    }
1539 	  alias_pairs->unordered_remove (i);
1540 	  continue;
1541 	}
1542       else if (!target_node)
1543 	{
1544 	  error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1545 	  symtab_node *node = symtab_node::get (p->decl);
1546 	  if (node)
1547 	    node->alias = false;
1548 	  alias_pairs->unordered_remove (i);
1549 	  continue;
1550 	}
1551 
1552       if (DECL_EXTERNAL (target_node->decl)
1553 	  /* We use local aliases for C++ thunks to force the tailcall
1554 	     to bind locally.  This is a hack - to keep it working do
1555 	     the following (which is not strictly correct).  */
1556 	  && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1557 	      || ! DECL_VIRTUAL_P (target_node->decl))
1558 	  && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1559 	{
1560 	  error ("%q+D aliased to external symbol %qE",
1561 		 p->decl, p->target);
1562 	}
1563 
1564       if (TREE_CODE (p->decl) == FUNCTION_DECL
1565           && target_node && is_a <cgraph_node *> (target_node))
1566 	{
1567 	  maybe_diag_incompatible_alias (p->decl, target_node->decl);
1568 
1569 	  maybe_diag_alias_attributes (p->decl, target_node->decl);
1570 
1571 	  cgraph_node *src_node = cgraph_node::get (p->decl);
1572 	  if (src_node && src_node->definition)
1573 	    src_node->reset ();
1574 	  cgraph_node::create_alias (p->decl, target_node->decl);
1575 	  alias_pairs->unordered_remove (i);
1576 	}
1577       else if (VAR_P (p->decl)
1578 	       && target_node && is_a <varpool_node *> (target_node))
1579 	{
1580 	  varpool_node::create_alias (p->decl, target_node->decl);
1581 	  alias_pairs->unordered_remove (i);
1582 	}
1583       else
1584 	{
1585 	  error ("%q+D alias between function and variable is not supported",
1586 		 p->decl);
1587 	  inform (DECL_SOURCE_LOCATION (target_node->decl),
1588 		  "aliased declaration here");
1589 
1590 	  alias_pairs->unordered_remove (i);
1591 	}
1592     }
1593   vec_free (alias_pairs);
1594 }
1595 
1596 
1597 /* Figure out what functions we want to assemble.  */
1598 
1599 static void
mark_functions_to_output(void)1600 mark_functions_to_output (void)
1601 {
1602   bool check_same_comdat_groups = false;
1603   cgraph_node *node;
1604 
1605   if (flag_checking)
1606     FOR_EACH_FUNCTION (node)
1607       gcc_assert (!node->process);
1608 
1609   FOR_EACH_FUNCTION (node)
1610     {
1611       tree decl = node->decl;
1612 
1613       gcc_assert (!node->process || node->same_comdat_group);
1614       if (node->process)
1615 	continue;
1616 
1617       /* We need to output all local functions that are used and not
1618 	 always inlined, as well as those that are reachable from
1619 	 outside the current compilation unit.  */
1620       if (node->analyzed
1621 	  && !node->thunk
1622 	  && !node->alias
1623 	  && !node->inlined_to
1624 	  && !TREE_ASM_WRITTEN (decl)
1625 	  && !DECL_EXTERNAL (decl))
1626 	{
1627 	  node->process = 1;
1628 	  if (node->same_comdat_group)
1629 	    {
1630 	      cgraph_node *next;
1631 	      for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1632 		   next != node;
1633 		   next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1634 		if (!next->thunk && !next->alias
1635 		    && !next->comdat_local_p ())
1636 		  next->process = 1;
1637 	    }
1638 	}
1639       else if (node->same_comdat_group)
1640 	{
1641 	  if (flag_checking)
1642 	    check_same_comdat_groups = true;
1643 	}
1644       else
1645 	{
1646 	  /* We should've reclaimed all functions that are not needed.  */
1647 	  if (flag_checking
1648 	      && !node->inlined_to
1649 	      && gimple_has_body_p (decl)
1650 	      /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1651 		 are inside partition, we can end up not removing the body since we no longer
1652 		 have analyzed node pointing to it.  */
1653 	      && !node->in_other_partition
1654 	      && !node->alias
1655 	      && !node->clones
1656 	      && !DECL_EXTERNAL (decl))
1657 	    {
1658 	      node->debug ();
1659 	      internal_error ("failed to reclaim unneeded function");
1660 	    }
1661 	  gcc_assert (node->inlined_to
1662 		      || !gimple_has_body_p (decl)
1663 		      || node->in_other_partition
1664 		      || node->clones
1665 		      || DECL_ARTIFICIAL (decl)
1666 		      || DECL_EXTERNAL (decl));
1667 
1668 	}
1669 
1670     }
1671   if (flag_checking && check_same_comdat_groups)
1672     FOR_EACH_FUNCTION (node)
1673       if (node->same_comdat_group && !node->process)
1674 	{
1675 	  tree decl = node->decl;
1676 	  if (!node->inlined_to
1677 	      && gimple_has_body_p (decl)
1678 	      /* FIXME: in an ltrans unit when the offline copy is outside a
1679 		 partition but inline copies are inside a partition, we can
1680 		 end up not removing the body since we no longer have an
1681 		 analyzed node pointing to it.  */
1682 	      && !node->in_other_partition
1683 	      && !node->clones
1684 	      && !DECL_EXTERNAL (decl))
1685 	    {
1686 	      node->debug ();
1687 	      internal_error ("failed to reclaim unneeded function in same "
1688 			      "comdat group");
1689 	    }
1690 	}
1691 }
1692 
1693 /* DECL is FUNCTION_DECL.  Initialize datastructures so DECL is a function
1694    in lowered gimple form.  IN_SSA is true if the gimple is in SSA.
1695 
1696    Set current_function_decl and cfun to newly constructed empty function body.
1697    return basic block in the function body.  */
1698 
1699 basic_block
init_lowered_empty_function(tree decl,bool in_ssa,profile_count count)1700 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1701 {
1702   basic_block bb;
1703   edge e;
1704 
1705   current_function_decl = decl;
1706   allocate_struct_function (decl, false);
1707   gimple_register_cfg_hooks ();
1708   init_empty_tree_cfg ();
1709   init_tree_ssa (cfun);
1710 
1711   if (in_ssa)
1712     {
1713       init_ssa_operands (cfun);
1714       cfun->gimple_df->in_ssa_p = true;
1715       cfun->curr_properties |= PROP_ssa;
1716     }
1717 
1718   DECL_INITIAL (decl) = make_node (BLOCK);
1719   BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1720 
1721   DECL_SAVED_TREE (decl) = error_mark_node;
1722   cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1723 			    | PROP_cfg | PROP_loops);
1724 
1725   set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1726   init_loops_structure (cfun, loops_for_fn (cfun), 1);
1727   loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1728 
1729   /* Create BB for body of the function and connect it properly.  */
1730   ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1731   EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1732   bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1733   bb->count = count;
1734   e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1735   e->probability = profile_probability::always ();
1736   e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1737   e->probability = profile_probability::always ();
1738   add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1739 
1740   return bb;
1741 }
1742 
1743 /* Assemble thunks and aliases associated to node.  */
1744 
1745 void
assemble_thunks_and_aliases(void)1746 cgraph_node::assemble_thunks_and_aliases (void)
1747 {
1748   cgraph_edge *e;
1749   ipa_ref *ref;
1750 
1751   for (e = callers; e;)
1752     if (e->caller->thunk
1753 	&& !e->caller->inlined_to)
1754       {
1755 	cgraph_node *thunk = e->caller;
1756 
1757 	e = e->next_caller;
1758 	expand_thunk (thunk, !rtl_dump_and_exit, false);
1759 	thunk->assemble_thunks_and_aliases ();
1760       }
1761     else
1762       e = e->next_caller;
1763 
1764   FOR_EACH_ALIAS (this, ref)
1765     {
1766       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1767       if (!alias->transparent_alias)
1768 	{
1769 	  bool saved_written = TREE_ASM_WRITTEN (decl);
1770 
1771 	  /* Force assemble_alias to really output the alias this time instead
1772 	     of buffering it in same alias pairs.  */
1773 	  TREE_ASM_WRITTEN (decl) = 1;
1774 	  if (alias->symver)
1775 	    do_assemble_symver (alias->decl,
1776 				DECL_ASSEMBLER_NAME (decl));
1777 	  else
1778 	    do_assemble_alias (alias->decl,
1779 			       DECL_ASSEMBLER_NAME (decl));
1780 	  alias->assemble_thunks_and_aliases ();
1781 	  TREE_ASM_WRITTEN (decl) = saved_written;
1782 	}
1783     }
1784 }
1785 
1786 /* Expand function specified by node.  */
1787 
1788 void
expand(void)1789 cgraph_node::expand (void)
1790 {
1791   location_t saved_loc;
1792 
1793   /* We ought to not compile any inline clones.  */
1794   gcc_assert (!inlined_to);
1795 
1796   /* __RTL functions are compiled as soon as they are parsed, so don't
1797      do it again.  */
1798   if (native_rtl_p ())
1799     return;
1800 
1801   announce_function (decl);
1802   process = 0;
1803   gcc_assert (lowered);
1804 
1805   /* Initialize the default bitmap obstack.  */
1806   bitmap_obstack_initialize (NULL);
1807   get_untransformed_body ();
1808 
1809   /* Generate RTL for the body of DECL.  */
1810 
1811   timevar_push (TV_REST_OF_COMPILATION);
1812 
1813   gcc_assert (symtab->global_info_ready);
1814 
1815   /* Initialize the RTL code for the function.  */
1816   saved_loc = input_location;
1817   input_location = DECL_SOURCE_LOCATION (decl);
1818 
1819   gcc_assert (DECL_STRUCT_FUNCTION (decl));
1820   push_cfun (DECL_STRUCT_FUNCTION (decl));
1821   init_function_start (decl);
1822 
1823   gimple_register_cfg_hooks ();
1824 
1825   bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1826 
1827   update_ssa (TODO_update_ssa_only_virtuals);
1828   if (ipa_transforms_to_apply.exists ())
1829     execute_all_ipa_transforms (false);
1830 
1831   /* Perform all tree transforms and optimizations.  */
1832 
1833   /* Signal the start of passes.  */
1834   invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1835 
1836   execute_pass_list (cfun, g->get_passes ()->all_passes);
1837 
1838   /* Signal the end of passes.  */
1839   invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1840 
1841   bitmap_obstack_release (&reg_obstack);
1842 
1843   /* Release the default bitmap obstack.  */
1844   bitmap_obstack_release (NULL);
1845 
1846   /* If requested, warn about function definitions where the function will
1847      return a value (usually of some struct or union type) which itself will
1848      take up a lot of stack space.  */
1849   if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1850     {
1851       tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1852 
1853       if (ret_type && TYPE_SIZE_UNIT (ret_type)
1854 	  && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1855 	  && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1856 			       warn_larger_than_size) > 0)
1857 	{
1858 	  unsigned int size_as_int
1859 	    = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1860 
1861 	  if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1862 	    warning (OPT_Wlarger_than_,
1863 		     "size of return value of %q+D is %u bytes",
1864                      decl, size_as_int);
1865 	  else
1866 	    warning (OPT_Wlarger_than_,
1867 		     "size of return value of %q+D is larger than %wu bytes",
1868 	             decl, warn_larger_than_size);
1869 	}
1870     }
1871 
1872   gimple_set_body (decl, NULL);
1873   if (DECL_STRUCT_FUNCTION (decl) == 0)
1874     {
1875       /* Stop pointing to the local nodes about to be freed.
1876 	 But DECL_INITIAL must remain nonzero so we know this
1877 	 was an actual function definition.  */
1878       if (DECL_INITIAL (decl) != 0)
1879 	DECL_INITIAL (decl) = error_mark_node;
1880     }
1881 
1882   input_location = saved_loc;
1883 
1884   ggc_collect ();
1885   timevar_pop (TV_REST_OF_COMPILATION);
1886 
1887   /* Make sure that BE didn't give up on compiling.  */
1888   gcc_assert (TREE_ASM_WRITTEN (decl));
1889   if (cfun)
1890     pop_cfun ();
1891 
1892   /* It would make a lot more sense to output thunks before function body to
1893      get more forward and fewer backward jumps.  This however would need
1894      solving problem with comdats.  See PR48668.  Also aliases must come after
1895      function itself to make one pass assemblers, like one on AIX, happy.
1896      See PR 50689.
1897      FIXME: Perhaps thunks should be move before function IFF they are not in
1898      comdat groups.  */
1899   assemble_thunks_and_aliases ();
1900   release_body ();
1901 }
1902 
1903 /* Node comparator that is responsible for the order that corresponds
1904    to time when a function was launched for the first time.  */
1905 
1906 int
tp_first_run_node_cmp(const void * pa,const void * pb)1907 tp_first_run_node_cmp (const void *pa, const void *pb)
1908 {
1909   const cgraph_node *a = *(const cgraph_node * const *) pa;
1910   const cgraph_node *b = *(const cgraph_node * const *) pb;
1911   unsigned int tp_first_run_a = a->tp_first_run;
1912   unsigned int tp_first_run_b = b->tp_first_run;
1913 
1914   if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
1915       || a->no_reorder)
1916     tp_first_run_a = 0;
1917   if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
1918       || b->no_reorder)
1919     tp_first_run_b = 0;
1920 
1921   if (tp_first_run_a == tp_first_run_b)
1922     return a->order - b->order;
1923 
1924   /* Functions with time profile must be before these without profile.  */
1925   tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
1926   tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
1927 
1928   return tp_first_run_a - tp_first_run_b;
1929 }
1930 
1931 /* Expand all functions that must be output.
1932 
1933    Attempt to topologically sort the nodes so function is output when
1934    all called functions are already assembled to allow data to be
1935    propagated across the callgraph.  Use a stack to get smaller distance
1936    between a function and its callees (later we may choose to use a more
1937    sophisticated algorithm for function reordering; we will likely want
1938    to use subsections to make the output functions appear in top-down
1939    order).  */
1940 
1941 static void
expand_all_functions(void)1942 expand_all_functions (void)
1943 {
1944   cgraph_node *node;
1945   cgraph_node **order = XCNEWVEC (cgraph_node *,
1946 					 symtab->cgraph_count);
1947   cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
1948 					 symtab->cgraph_count);
1949   unsigned int expanded_func_count = 0, profiled_func_count = 0;
1950   int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
1951   int i;
1952 
1953   order_pos = ipa_reverse_postorder (order);
1954   gcc_assert (order_pos == symtab->cgraph_count);
1955 
1956   /* Garbage collector may remove inline clones we eliminate during
1957      optimization.  So we must be sure to not reference them.  */
1958   for (i = 0; i < order_pos; i++)
1959     if (order[i]->process)
1960       {
1961 	if (order[i]->tp_first_run
1962 	    && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
1963 	  tp_first_run_order[tp_first_run_order_pos++] = order[i];
1964 	else
1965           order[new_order_pos++] = order[i];
1966       }
1967 
1968   /* First output functions with time profile in specified order.  */
1969   qsort (tp_first_run_order, tp_first_run_order_pos,
1970 	 sizeof (cgraph_node *), tp_first_run_node_cmp);
1971   for (i = 0; i < tp_first_run_order_pos; i++)
1972     {
1973       node = tp_first_run_order[i];
1974 
1975       if (node->process)
1976 	{
1977 	  expanded_func_count++;
1978 	  profiled_func_count++;
1979 
1980 	  if (symtab->dump_file)
1981 	    fprintf (symtab->dump_file,
1982 		     "Time profile order in expand_all_functions:%s:%d\n",
1983 		     node->dump_asm_name (), node->tp_first_run);
1984 	  node->process = 0;
1985 	  node->expand ();
1986 	}
1987     }
1988 
1989   /* Output functions in RPO so callees get optimized before callers.  This
1990      makes ipa-ra and other propagators to work.
1991      FIXME: This is far from optimal code layout.  */
1992   for (i = new_order_pos - 1; i >= 0; i--)
1993     {
1994       node = order[i];
1995 
1996       if (node->process)
1997 	{
1998 	  expanded_func_count++;
1999 	  node->process = 0;
2000 	  node->expand ();
2001 	}
2002     }
2003 
2004   if (dump_file)
2005     fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2006 	     main_input_filename, profiled_func_count, expanded_func_count);
2007 
2008   if (symtab->dump_file && tp_first_run_order_pos)
2009     fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2010              profiled_func_count, expanded_func_count);
2011 
2012   symtab->process_new_functions ();
2013   free_gimplify_stack ();
2014   delete ipa_saved_clone_sources;
2015   ipa_saved_clone_sources = NULL;
2016   free (order);
2017   free (tp_first_run_order);
2018 }
2019 
2020 /* This is used to sort the node types by the cgraph order number.  */
2021 
2022 enum cgraph_order_sort_kind
2023 {
2024   ORDER_FUNCTION,
2025   ORDER_VAR,
2026   ORDER_VAR_UNDEF,
2027   ORDER_ASM
2028 };
2029 
2030 struct cgraph_order_sort
2031 {
2032   /* Construct from a cgraph_node.  */
cgraph_order_sortcgraph_order_sort2033   cgraph_order_sort (cgraph_node *node)
2034   : kind (ORDER_FUNCTION), order (node->order)
2035   {
2036     u.f = node;
2037   }
2038 
2039   /* Construct from a varpool_node.  */
cgraph_order_sortcgraph_order_sort2040   cgraph_order_sort (varpool_node *node)
2041   : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2042   {
2043     u.v = node;
2044   }
2045 
2046   /* Construct from a asm_node.  */
cgraph_order_sortcgraph_order_sort2047   cgraph_order_sort (asm_node *node)
2048   : kind (ORDER_ASM), order (node->order)
2049   {
2050     u.a = node;
2051   }
2052 
2053   /* Assembly cgraph_order_sort based on its type.  */
2054   void process ();
2055 
2056   enum cgraph_order_sort_kind kind;
2057   union
2058   {
2059     cgraph_node *f;
2060     varpool_node *v;
2061     asm_node *a;
2062   } u;
2063   int order;
2064 };
2065 
2066 /* Assembly cgraph_order_sort based on its type.  */
2067 
2068 void
process()2069 cgraph_order_sort::process ()
2070 {
2071   switch (kind)
2072     {
2073     case ORDER_FUNCTION:
2074       u.f->process = 0;
2075       u.f->expand ();
2076       break;
2077     case ORDER_VAR:
2078       u.v->assemble_decl ();
2079       break;
2080     case ORDER_VAR_UNDEF:
2081       assemble_undefined_decl (u.v->decl);
2082       break;
2083     case ORDER_ASM:
2084       assemble_asm (u.a->asm_str);
2085       break;
2086     default:
2087       gcc_unreachable ();
2088     }
2089 }
2090 
2091 /* Compare cgraph_order_sort by order.  */
2092 
2093 static int
cgraph_order_cmp(const void * a_p,const void * b_p)2094 cgraph_order_cmp (const void *a_p, const void *b_p)
2095 {
2096   const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2097   const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2098 
2099   return nodea->order - nodeb->order;
2100 }
2101 
2102 /* Output all functions, variables, and asm statements in the order
2103    according to their order fields, which is the order in which they
2104    appeared in the file.  This implements -fno-toplevel-reorder.  In
2105    this mode we may output functions and variables which don't really
2106    need to be output.  */
2107 
2108 static void
output_in_order(void)2109 output_in_order (void)
2110 {
2111   int i;
2112   cgraph_node *cnode;
2113   varpool_node *vnode;
2114   asm_node *anode;
2115   auto_vec<cgraph_order_sort> nodes;
2116   cgraph_order_sort *node;
2117 
2118   FOR_EACH_DEFINED_FUNCTION (cnode)
2119     if (cnode->process && !cnode->thunk
2120 	&& !cnode->alias && cnode->no_reorder)
2121       nodes.safe_push (cgraph_order_sort (cnode));
2122 
2123   /* There is a similar loop in symbol_table::output_variables.
2124      Please keep them in sync.  */
2125   FOR_EACH_VARIABLE (vnode)
2126     if (vnode->no_reorder
2127 	&& !DECL_HARD_REGISTER (vnode->decl)
2128 	&& !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2129       nodes.safe_push (cgraph_order_sort (vnode));
2130 
2131   for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
2132     nodes.safe_push (cgraph_order_sort (anode));
2133 
2134   /* Sort nodes by order.  */
2135   nodes.qsort (cgraph_order_cmp);
2136 
2137   /* In toplevel reorder mode we output all statics; mark them as needed.  */
2138   FOR_EACH_VEC_ELT (nodes, i, node)
2139     if (node->kind == ORDER_VAR)
2140       node->u.v->finalize_named_section_flags ();
2141 
2142   FOR_EACH_VEC_ELT (nodes, i, node)
2143     node->process ();
2144 
2145   symtab->clear_asm_symbols ();
2146 }
2147 
2148 static void
ipa_passes(void)2149 ipa_passes (void)
2150 {
2151   gcc::pass_manager *passes = g->get_passes ();
2152 
2153   set_cfun (NULL);
2154   current_function_decl = NULL;
2155   gimple_register_cfg_hooks ();
2156   bitmap_obstack_initialize (NULL);
2157 
2158   invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2159 
2160   if (!in_lto_p)
2161     {
2162       execute_ipa_pass_list (passes->all_small_ipa_passes);
2163       if (seen_error ())
2164 	return;
2165     }
2166 
2167   /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2168      devirtualization and other changes where removal iterate.  */
2169   symtab->remove_unreachable_nodes (symtab->dump_file);
2170 
2171   /* If pass_all_early_optimizations was not scheduled, the state of
2172      the cgraph will not be properly updated.  Update it now.  */
2173   if (symtab->state < IPA_SSA)
2174     symtab->state = IPA_SSA;
2175 
2176   if (!in_lto_p)
2177     {
2178       /* Generate coverage variables and constructors.  */
2179       coverage_finish ();
2180 
2181       /* Process new functions added.  */
2182       set_cfun (NULL);
2183       current_function_decl = NULL;
2184       symtab->process_new_functions ();
2185 
2186       execute_ipa_summary_passes
2187 	((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2188     }
2189 
2190   /* Some targets need to handle LTO assembler output specially.  */
2191   if (flag_generate_lto || flag_generate_offload)
2192     targetm.asm_out.lto_start ();
2193 
2194   if (!in_lto_p
2195       || flag_incremental_link == INCREMENTAL_LINK_LTO)
2196     {
2197       if (!quiet_flag)
2198 	fprintf (stderr, "Streaming LTO\n");
2199       if (g->have_offload)
2200 	{
2201 	  section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2202 	  lto_stream_offload_p = true;
2203 	  ipa_write_summaries ();
2204 	  lto_stream_offload_p = false;
2205 	}
2206       if (flag_lto)
2207 	{
2208 	  section_name_prefix = LTO_SECTION_NAME_PREFIX;
2209 	  lto_stream_offload_p = false;
2210 	  ipa_write_summaries ();
2211 	}
2212     }
2213 
2214   if (flag_generate_lto || flag_generate_offload)
2215     targetm.asm_out.lto_end ();
2216 
2217   if (!flag_ltrans
2218       && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2219 	  || !flag_lto || flag_fat_lto_objects))
2220     execute_ipa_pass_list (passes->all_regular_ipa_passes);
2221   invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2222 
2223   bitmap_obstack_release (NULL);
2224 }
2225 
2226 
2227 /* Weakrefs may be associated to external decls and thus not output
2228    at expansion time.  Emit all necessary aliases.  */
2229 
2230 void
output_weakrefs(void)2231 symbol_table::output_weakrefs (void)
2232 {
2233   symtab_node *node;
2234   FOR_EACH_SYMBOL (node)
2235     if (node->alias
2236         && !TREE_ASM_WRITTEN (node->decl)
2237 	&& node->weakref)
2238       {
2239 	tree target;
2240 
2241 	/* Weakrefs are special by not requiring target definition in current
2242 	   compilation unit.  It is thus bit hard to work out what we want to
2243 	   alias.
2244 	   When alias target is defined, we need to fetch it from symtab reference,
2245 	   otherwise it is pointed to by alias_target.  */
2246 	if (node->alias_target)
2247 	  target = (DECL_P (node->alias_target)
2248 		    ? DECL_ASSEMBLER_NAME (node->alias_target)
2249 		    : node->alias_target);
2250 	else if (node->analyzed)
2251 	  target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2252 	else
2253 	  gcc_unreachable ();
2254         do_assemble_alias (node->decl, target);
2255       }
2256 }
2257 
2258 /* Perform simple optimizations based on callgraph.  */
2259 
2260 void
compile(void)2261 symbol_table::compile (void)
2262 {
2263   if (seen_error ())
2264     return;
2265 
2266   symtab_node::checking_verify_symtab_nodes ();
2267 
2268   timevar_push (TV_CGRAPHOPT);
2269   if (pre_ipa_mem_report)
2270     dump_memory_report ("Memory consumption before IPA");
2271   if (!quiet_flag)
2272     fprintf (stderr, "Performing interprocedural optimizations\n");
2273   state = IPA;
2274 
2275   /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE.  */
2276   if (flag_generate_lto || flag_generate_offload)
2277     lto_streamer_hooks_init ();
2278 
2279   /* Don't run the IPA passes if there was any error or sorry messages.  */
2280   if (!seen_error ())
2281   {
2282     timevar_start (TV_CGRAPH_IPA_PASSES);
2283     ipa_passes ();
2284     timevar_stop (TV_CGRAPH_IPA_PASSES);
2285   }
2286   /* Do nothing else if any IPA pass found errors or if we are just streaming LTO.  */
2287   if (seen_error ()
2288       || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2289 	  && flag_lto && !flag_fat_lto_objects))
2290     {
2291       timevar_pop (TV_CGRAPHOPT);
2292       return;
2293     }
2294 
2295   global_info_ready = true;
2296   if (dump_file)
2297     {
2298       fprintf (dump_file, "Optimized ");
2299       symtab->dump (dump_file);
2300     }
2301   if (post_ipa_mem_report)
2302     dump_memory_report ("Memory consumption after IPA");
2303   timevar_pop (TV_CGRAPHOPT);
2304 
2305   /* Output everything.  */
2306   switch_to_section (text_section);
2307   (*debug_hooks->assembly_start) ();
2308   if (!quiet_flag)
2309     fprintf (stderr, "Assembling functions:\n");
2310   symtab_node::checking_verify_symtab_nodes ();
2311 
2312   bitmap_obstack_initialize (NULL);
2313   execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2314   bitmap_obstack_release (NULL);
2315   mark_functions_to_output ();
2316 
2317   /* When weakref support is missing, we automatically translate all
2318      references to NODE to references to its ultimate alias target.
2319      The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2320      TREE_CHAIN.
2321 
2322      Set up this mapping before we output any assembler but once we are sure
2323      that all symbol renaming is done.
2324 
2325      FIXME: All this ugliness can go away if we just do renaming at gimple
2326      level by physically rewriting the IL.  At the moment we can only redirect
2327      calls, so we need infrastructure for renaming references as well.  */
2328 #ifndef ASM_OUTPUT_WEAKREF
2329   symtab_node *node;
2330 
2331   FOR_EACH_SYMBOL (node)
2332     if (node->alias
2333 	&& lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2334       {
2335 	IDENTIFIER_TRANSPARENT_ALIAS
2336 	   (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2337 	TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2338 	   = (node->alias_target ? node->alias_target
2339 	      : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2340       }
2341 #endif
2342 
2343   state = EXPANSION;
2344 
2345   /* Output first asm statements and anything ordered. The process
2346      flag is cleared for these nodes, so we skip them later.  */
2347   output_in_order ();
2348 
2349   timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2350   expand_all_functions ();
2351   timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2352 
2353   output_variables ();
2354 
2355   process_new_functions ();
2356   state = FINISHED;
2357   output_weakrefs ();
2358 
2359   if (dump_file)
2360     {
2361       fprintf (dump_file, "\nFinal ");
2362       symtab->dump (dump_file);
2363     }
2364   if (!flag_checking)
2365     return;
2366   symtab_node::verify_symtab_nodes ();
2367   /* Double check that all inline clones are gone and that all
2368      function bodies have been released from memory.  */
2369   if (!seen_error ())
2370     {
2371       cgraph_node *node;
2372       bool error_found = false;
2373 
2374       FOR_EACH_DEFINED_FUNCTION (node)
2375 	if (node->inlined_to
2376 	    || gimple_has_body_p (node->decl))
2377 	  {
2378 	    error_found = true;
2379 	    node->debug ();
2380 	  }
2381       if (error_found)
2382 	internal_error ("nodes with unreleased memory found");
2383     }
2384 }
2385 
2386 /* Earlydebug dump file, flags, and number.  */
2387 
2388 static int debuginfo_early_dump_nr;
2389 static FILE *debuginfo_early_dump_file;
2390 static dump_flags_t debuginfo_early_dump_flags;
2391 
2392 /* Debug dump file, flags, and number.  */
2393 
2394 static int debuginfo_dump_nr;
2395 static FILE *debuginfo_dump_file;
2396 static dump_flags_t debuginfo_dump_flags;
2397 
2398 /* Register the debug and earlydebug dump files.  */
2399 
2400 void
debuginfo_early_init(void)2401 debuginfo_early_init (void)
2402 {
2403   gcc::dump_manager *dumps = g->get_dumps ();
2404   debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2405 						  "earlydebug", DK_tree,
2406 						  OPTGROUP_NONE,
2407 						  false);
2408   debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2409 					     "debug", DK_tree,
2410 					     OPTGROUP_NONE,
2411 					     false);
2412 }
2413 
2414 /* Initialize the debug and earlydebug dump files.  */
2415 
2416 void
debuginfo_init(void)2417 debuginfo_init (void)
2418 {
2419   gcc::dump_manager *dumps = g->get_dumps ();
2420   debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2421   debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2422   debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2423   debuginfo_early_dump_flags
2424     = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2425 }
2426 
2427 /* Finalize the debug and earlydebug dump files.  */
2428 
2429 void
debuginfo_fini(void)2430 debuginfo_fini (void)
2431 {
2432   if (debuginfo_dump_file)
2433     dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2434   if (debuginfo_early_dump_file)
2435     dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2436 }
2437 
2438 /* Set dump_file to the debug dump file.  */
2439 
2440 void
debuginfo_start(void)2441 debuginfo_start (void)
2442 {
2443   set_dump_file (debuginfo_dump_file);
2444 }
2445 
2446 /* Undo setting dump_file to the debug dump file.  */
2447 
2448 void
debuginfo_stop(void)2449 debuginfo_stop (void)
2450 {
2451   set_dump_file (NULL);
2452 }
2453 
2454 /* Set dump_file to the earlydebug dump file.  */
2455 
2456 void
debuginfo_early_start(void)2457 debuginfo_early_start (void)
2458 {
2459   set_dump_file (debuginfo_early_dump_file);
2460 }
2461 
2462 /* Undo setting dump_file to the earlydebug dump file.  */
2463 
2464 void
debuginfo_early_stop(void)2465 debuginfo_early_stop (void)
2466 {
2467   set_dump_file (NULL);
2468 }
2469 
2470 /* Analyze the whole compilation unit once it is parsed completely.  */
2471 
2472 void
finalize_compilation_unit(void)2473 symbol_table::finalize_compilation_unit (void)
2474 {
2475   timevar_push (TV_CGRAPH);
2476 
2477   /* If we're here there's no current function anymore.  Some frontends
2478      are lazy in clearing these.  */
2479   current_function_decl = NULL;
2480   set_cfun (NULL);
2481 
2482   /* Do not skip analyzing the functions if there were errors, we
2483      miss diagnostics for following functions otherwise.  */
2484 
2485   /* Emit size functions we didn't inline.  */
2486   finalize_size_functions ();
2487 
2488   /* Mark alias targets necessary and emit diagnostics.  */
2489   handle_alias_pairs ();
2490 
2491   if (!quiet_flag)
2492     {
2493       fprintf (stderr, "\nAnalyzing compilation unit\n");
2494       fflush (stderr);
2495     }
2496 
2497   if (flag_dump_passes)
2498     dump_passes ();
2499 
2500   /* Gimplify and lower all functions, compute reachability and
2501      remove unreachable nodes.  */
2502   analyze_functions (/*first_time=*/true);
2503 
2504   /* Mark alias targets necessary and emit diagnostics.  */
2505   handle_alias_pairs ();
2506 
2507   /* Gimplify and lower thunks.  */
2508   analyze_functions (/*first_time=*/false);
2509 
2510   /* All nested functions should be lowered now.  */
2511   nested_function_info::release ();
2512 
2513   /* Offloading requires LTO infrastructure.  */
2514   if (!in_lto_p && g->have_offload)
2515     flag_generate_offload = 1;
2516 
2517   if (!seen_error ())
2518     {
2519       /* Give the frontends the chance to emit early debug based on
2520 	 what is still reachable in the TU.  */
2521       (*lang_hooks.finalize_early_debug) ();
2522 
2523       /* Clean up anything that needs cleaning up after initial debug
2524 	 generation.  */
2525       debuginfo_early_start ();
2526       (*debug_hooks->early_finish) (main_input_filename);
2527       debuginfo_early_stop ();
2528     }
2529 
2530   /* Finally drive the pass manager.  */
2531   compile ();
2532 
2533   timevar_pop (TV_CGRAPH);
2534 }
2535 
2536 /* Reset all state within cgraphunit.cc so that we can rerun the compiler
2537    within the same process.  For use by toplev::finalize.  */
2538 
2539 void
cgraphunit_cc_finalize(void)2540 cgraphunit_cc_finalize (void)
2541 {
2542   gcc_assert (cgraph_new_nodes.length () == 0);
2543   cgraph_new_nodes.truncate (0);
2544 
2545   queued_nodes = &symtab_terminator;
2546 
2547   first_analyzed = NULL;
2548   first_analyzed_var = NULL;
2549 }
2550 
2551 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2552    kind of wrapper method.  */
2553 
2554 void
create_wrapper(cgraph_node * target)2555 cgraph_node::create_wrapper (cgraph_node *target)
2556 {
2557   /* Preserve DECL_RESULT so we get right by reference flag.  */
2558   tree decl_result = DECL_RESULT (decl);
2559 
2560   /* Remove the function's body but keep arguments to be reused
2561      for thunk.  */
2562   release_body (true);
2563   reset ();
2564 
2565   DECL_UNINLINABLE (decl) = false;
2566   DECL_RESULT (decl) = decl_result;
2567   DECL_INITIAL (decl) = NULL;
2568   allocate_struct_function (decl, false);
2569   set_cfun (NULL);
2570 
2571   /* Turn alias into thunk and expand it into GIMPLE representation.  */
2572   definition = true;
2573   semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
2574 
2575   /* Create empty thunk, but be sure we did not keep former thunk around.
2576      In that case we would need to preserve the info.  */
2577   gcc_checking_assert (!thunk_info::get (this));
2578   thunk_info::get_create (this);
2579   thunk = true;
2580   create_edge (target, NULL, count);
2581   callees->can_throw_external = !TREE_NOTHROW (target->decl);
2582 
2583   tree arguments = DECL_ARGUMENTS (decl);
2584 
2585   while (arguments)
2586     {
2587       TREE_ADDRESSABLE (arguments) = false;
2588       arguments = TREE_CHAIN (arguments);
2589     }
2590 
2591   expand_thunk (this, false, true);
2592   thunk_info::remove (this);
2593 
2594   /* Inline summary set-up.  */
2595   analyze ();
2596   inline_analyze_function (this);
2597 }
2598