GCC Middle and Back End API Reference
cgraphunit.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "gimple.h"
#include "cfghooks.h"
#include "regset.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "stringpool.h"
#include "gimple-ssa.h"
#include "cgraph.h"
#include "coverage.h"
#include "lto-streamer.h"
#include "fold-const.h"
#include "varasm.h"
#include "stor-layout.h"
#include "output.h"
#include "cfgcleanup.h"
#include "gimple-iterator.h"
#include "gimple-fold.h"
#include "gimplify.h"
#include "gimplify-me.h"
#include "tree-cfg.h"
#include "tree-into-ssa.h"
#include "tree-ssa.h"
#include "langhooks.h"
#include "toplev.h"
#include "debug.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "sreal.h"
#include "ipa-cp.h"
#include "ipa-prop.h"
#include "gimple-pretty-print.h"
#include "plugin.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "except.h"
#include "cfgloop.h"
#include "context.h"
#include "pass_manager.h"
#include "tree-nested.h"
#include "dbgcnt.h"
#include "lto-section-names.h"
#include "attribs.h"
#include "ipa-inline.h"
#include "omp-offload.h"
#include "symtab-thunks.h"

Data Structures

struct  cgraph_order_sort
 

Enumerations

enum  cgraph_order_sort_kind { ORDER_FUNCTION , ORDER_VAR , ORDER_VAR_UNDEF , ORDER_ASM }
 

Functions

static void expand_all_functions (void)
 
static void mark_functions_to_output (void)
 
static void handle_alias_pairs (void)
 
static void enqueue_node (symtab_node *node)
 
static void process_symver_attribute (symtab_node *n)
 
static void process_common_attributes (symtab_node *node, tree decl)
 
static void process_function_and_variable_attributes (cgraph_node *first, varpool_node *first_var)
 
static void walk_polymorphic_call_targets (hash_set< void * > *reachable_call_targets, cgraph_edge *edge)
 
static void check_global_declaration (symtab_node *snode)
 
static void analyze_functions (bool first_time)
 
static void maybe_diag_incompatible_alias (tree alias, tree target)
 
basic_block init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
 
int tp_first_run_node_cmp (const void *pa, const void *pb)
 
static int cgraph_order_cmp (const void *a_p, const void *b_p)
 
static void output_in_order (void)
 
static void ipa_passes (void)
 
void debuginfo_early_init (void)
 
void debuginfo_init (void)
 
void debuginfo_fini (void)
 
void debuginfo_start (void)
 
void debuginfo_stop (void)
 
void debuginfo_early_start (void)
 
void debuginfo_early_stop (void)
 
void cgraphunit_cc_finalize (void)
 

Variables

vec< cgraph_node * > cgraph_new_nodes
 
static symtab_node symtab_terminator (SYMTAB_SYMBOL)
 
static symtab_nodequeued_nodes = &symtab_terminator
 
static cgraph_nodefirst_analyzed
 
static varpool_nodefirst_analyzed_var
 
static int debuginfo_early_dump_nr
 
static FILEdebuginfo_early_dump_file
 
static dump_flags_t debuginfo_early_dump_flags
 
static int debuginfo_dump_nr
 
static FILEdebuginfo_dump_file
 
static dump_flags_t debuginfo_dump_flags
 

Enumeration Type Documentation

◆ cgraph_order_sort_kind

This is used to sort the node types by the cgraph order number.   
Enumerator
ORDER_FUNCTION 
ORDER_VAR 
ORDER_VAR_UNDEF 
ORDER_ASM 

Function Documentation

◆ analyze_functions()

static void analyze_functions ( bool first_time)
static
FIRST_TIME is set to TRUE for the first time we are called for a
translation unit from finalize_compilation_unit() or false
otherwise.   

References symtab_node::alias, symtab_node::aux, bitmap_obstack_initialize(), bitmap_obstack_release(), build_type_inheritance_graph(), changed, check_global_declaration(), symtab_node::comdat_local_p(), CONSTRUCTION, symtab_node::cpp_implicit_alias, symbol_table::cpp_implicit_aliases_done, debug_hooks, symtab_node::decl, DECL_ABSTRACT_ORIGIN, DECL_ATTRIBUTES, DECL_EXTERNAL, decl_function_context(), DECL_STRUCT_FUNCTION, symtab_node::definition, symbol_table::dump(), symtab_node::dump_asm_name(), symbol_table::dump_file, symtab_node::dump_name(), enqueue_node(), first_analyzed, first_analyzed_var, symbol_table::first_function(), symbol_table::first_symbol(), symbol_table::first_variable(), symtab_node::fixup_same_cpp_alias_visibility(), FOR_EACH_SYMBOL, gcc_assert, symtab_node::get_alias_target(), symtab_node::get_comdat_group_id(), cgraph_node::get_create(), ggc_alloc(), gimple_has_body_p(), i, input_location, symtab_node::iterate_reference(), gcc_debug_hooks::late_global_decl, lookup_attribute(), symtab_node::needed_p(), symtab_node::next, cgraph_edge::next_callee, NULL, omp_discover_implicit_declare_target(), opt_for_fn, thunk_info::process_early_thunks(), process_function_and_variable_attributes(), symbol_table::process_new_functions(), queued_nodes, ipa_ref::referred, symtab_node::referred_to_p(), symtab_node::remove(), symtab_node::same_comdat_group, seen_error(), symbol_table::state, symtab, symbol_table::symtab_initialize_asm_name_hash(), symtab_terminator, TREE_CODE, TREE_READONLY, TREE_TYPE, TYPE_NEEDS_CONSTRUCTING, TYPE_P, UNKNOWN_LOCATION, update_type_inheritance_graph(), VAR_P, and walk_polymorphic_call_targets().

Referenced by symbol_table::finalize_compilation_unit().

◆ cgraph_order_cmp()

static int cgraph_order_cmp ( const void * a_p,
const void * b_p )
static
Compare cgraph_order_sort by order.   

References ggc_alloc().

Referenced by output_in_order().

◆ cgraphunit_cc_finalize()

void cgraphunit_cc_finalize ( void )
Reset all state within cgraphunit.cc so that we can rerun the compiler
within the same process.  For use by toplev::finalize.   

References cgraph_new_nodes, first_analyzed, first_analyzed_var, gcc_assert, NULL, queued_nodes, and symtab_terminator.

Referenced by toplev::finalize().

◆ check_global_declaration()

◆ debuginfo_early_init()

void debuginfo_early_init ( void )
Register the debug and earlydebug dump files.   

References debuginfo_dump_nr, debuginfo_early_dump_nr, DK_tree, g, gcc::context::get_dumps(), ggc_alloc(), and OPTGROUP_NONE.

Referenced by general_init().

◆ debuginfo_early_start()

void debuginfo_early_start ( void )
Set dump_file to the earlydebug dump file.   

References debuginfo_early_dump_file, and set_dump_file().

Referenced by symbol_table::finalize_compilation_unit().

◆ debuginfo_early_stop()

void debuginfo_early_stop ( void )
Undo setting dump_file to the earlydebug dump file.   

References NULL, and set_dump_file().

Referenced by symbol_table::finalize_compilation_unit().

◆ debuginfo_fini()

void debuginfo_fini ( void )
Finalize the debug and earlydebug dump files.   

References debuginfo_dump_file, debuginfo_dump_nr, debuginfo_early_dump_file, debuginfo_early_dump_nr, and dump_end().

Referenced by finalize().

◆ debuginfo_init()

◆ debuginfo_start()

void debuginfo_start ( void )
Set dump_file to the debug dump file.   

References debuginfo_dump_file, and set_dump_file().

Referenced by compile_file().

◆ debuginfo_stop()

void debuginfo_stop ( void )
Undo setting dump_file to the debug dump file.   

References NULL, and set_dump_file().

Referenced by compile_file().

◆ enqueue_node()

static void enqueue_node ( symtab_node * node)
static

◆ expand_all_functions()

static void expand_all_functions ( void )
static
Expand all functions that must be output.

Attempt to topologically sort the nodes so function is output when
all called functions are already assembled to allow data to be
propagated across the callgraph.  Use a stack to get smaller distance
between a function and its callees (later we may choose to use a more
sophisticated algorithm for function reordering; we will likely want
to use subsections to make the output functions appear in top-down
order).   

References CDI_DOMINATORS, CDI_POST_DOMINATORS, symbol_table::cgraph_count, symtab_node::decl, DECL_ASSEMBLER_NAME, DECL_STRUCT_FUNCTION, symtab_node::dump_asm_name(), symbol_table::dump_file, dump_file, cgraph_node::expand(), free(), free_dominance_info(), free_gimplify_stack(), cgraph_node::gc_candidate, gcc_assert, ggc_alloc(), i, IDENTIFIER_POINTER, ipa_reverse_postorder(), ipa_saved_clone_sources, NULL, opt_for_fn, cgraph_node::process, symbol_table::process_new_functions(), qsort, cgraph_node::release_body(), symtab, cgraph_node::tp_first_run, and tp_first_run_node_cmp().

Referenced by symbol_table::compile().

◆ handle_alias_pairs()

◆ init_lowered_empty_function()

basic_block init_lowered_empty_function ( tree decl,
bool in_ssa,
profile_count count )

◆ ipa_passes()

◆ mark_functions_to_output()

◆ maybe_diag_incompatible_alias()

static void maybe_diag_incompatible_alias ( tree alias,
tree target )
static
Check declaration of the type of ALIAS for compatibility with its TARGET
(which may be an ifunc resolver) and issue a diagnostic when they are
not compatible according to language rules (plus a C++ extension for
non-static member functions).   

References build_function_type(), build_pointer_type(), DECL_SOURCE_LOCATION, error_at(), FUNC_OR_METHOD_TYPE_P, cgraph_node::get(), ggc_alloc(), symtab_node::ifunc_resolver, inform(), POINTER_TYPE_P, prototype_p(), TREE_CODE, TREE_TYPE, TYPE_ARG_TYPES, types_compatible_p(), VOID_TYPE_P, and warning_at().

Referenced by handle_alias_pairs().

◆ output_in_order()

static void output_in_order ( void )
static
Output all functions, variables, and asm statements in the order
according to their order fields, which is the order in which they
appeared in the file.  This implements -fno-toplevel-reorder.  In
this mode we may output functions and variables which don't really
need to be output.   

References cgraph_order_cmp(), symbol_table::clear_asm_symbols(), DECL_HARD_REGISTER, DECL_HAS_VALUE_EXPR_P, varpool_node::finalize_named_section_flags(), symbol_table::first_asm_symbol(), FOR_EACH_DEFINED_FUNCTION, FOR_EACH_VARIABLE, FOR_EACH_VEC_ELT, ggc_alloc(), i, cgraph_order_sort::kind, ORDER_VAR, cgraph_order_sort::process(), symtab, cgraph_order_sort::u, and cgraph_order_sort::v.

Referenced by symbol_table::compile().

◆ process_common_attributes()

static void process_common_attributes ( symtab_node * node,
tree decl )
static

◆ process_function_and_variable_attributes()

static void process_function_and_variable_attributes ( cgraph_node * first,
varpool_node * first_var )
static
Look for externally_visible and used attributes and mark cgraph nodes
accordingly.

We cannot mark the nodes at the point the attributes are processed (in
handle_*_attribute) because the copy of the declarations available at that
point may not be canonical.  For example, in:

 void f();
 void f() __attribute__((used));

the declaration we see in handle_used_attribute will be the second
declaration -- but the front end will subsequently merge that declaration
with the original declaration and discard the second declaration.

Furthermore, we can't mark these nodes in finalize_function because:

 void f() {}
 void f() __attribute__((externally_visible));

is valid.

So, we walk the nodes at the end of the translation unit, applying the
attributes at that point.   

References symtab_node::alias, symtab_node::decl, DECL_ATTRIBUTES, DECL_DECLARED_INLINE_P, DECL_EXTERNAL, DECL_INITIAL, DECL_P, DECL_PRESERVE_P, DECL_SOURCE_LOCATION, DECL_UNINLINABLE, DECL_WEAK, symtab_node::definition, error_mark_node, varpool_node::finalize_decl(), symbol_table::first_function(), symbol_table::first_variable(), symtab_node::get_alias_target_tree(), ggc_alloc(), lookup_attribute(), cgraph_node::mark_force_output(), symbol_table::next_function(), symbol_table::next_variable(), process_common_attributes(), remove_attribute(), symtab, symtab_node::transparent_alias, TREE_PUBLIC, warning_at(), and symtab_node::weakref.

Referenced by analyze_functions().

◆ process_symver_attribute()

◆ tp_first_run_node_cmp()

int tp_first_run_node_cmp ( const void * pa,
const void * pb )
Node comparator that is responsible for the order that corresponds
to time when a function was launched for the first time.   

References a, b, ggc_alloc(), INT_MAX, and opt_for_fn.

Referenced by expand_all_functions().

◆ walk_polymorphic_call_targets()

static void walk_polymorphic_call_targets ( hash_set< void * > * reachable_call_targets,
cgraph_edge * edge )
static

Variable Documentation

◆ cgraph_new_nodes

vec<cgraph_node *> cgraph_new_nodes
Driver of optimization process
   Copyright (C) 2003-2024 Free Software Foundation, Inc.
   Contributed by Jan Hubicka

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.   
This module implements main driver of compilation process.

  The main scope of this file is to act as an interface in between
  tree based frontends and the backend.

  The front-end is supposed to use following functionality:

   - finalize_function

     This function is called once front-end has parsed whole body of function
     and it is certain that the function body nor the declaration will change.

     (There is one exception needed for implementing GCC extern inline
       function.)

   - varpool_finalize_decl

     This function has same behavior as the above but is used for static
     variables.

   - add_asm_node

     Insert new toplevel ASM statement

   - finalize_compilation_unit

     This function is called once (source level) compilation unit is finalized
     and it will no longer change.

     The symbol table is constructed starting from the trivially needed
     symbols finalized by the frontend.  Functions are lowered into
     GIMPLE representation and callgraph/reference lists are constructed.
     Those are used to discover other necessary functions and variables.

     At the end the bodies of unreachable functions are removed.

     The function can be called multiple times when multiple source level
     compilation units are combined.

   - compile

     This passes control to the back-end.  Optimizations are performed and
     final assembler is generated.  This is done in the following way. Note
     that with link time optimization the process is split into three
     stages (compile time, linktime analysis and parallel linktime as
     indicated bellow).

     Compile time:

       1) Inter-procedural optimization.
          (ipa_passes)

          This part is further split into:

          a) early optimizations. These are local passes executed in
             the topological order on the callgraph.

             The purpose of early optimizations is to optimize away simple
             things that may otherwise confuse IP analysis. Very simple
             propagation across the callgraph is done i.e. to discover
             functions without side effects and simple inlining is performed.

          b) early small interprocedural passes.

             Those are interprocedural passes executed only at compilation
             time.  These include, for example, transactional memory lowering,
             unreachable code removal and other simple transformations.

          c) IP analysis stage.  All interprocedural passes do their
             analysis.

             Interprocedural passes differ from small interprocedural
             passes by their ability to operate across whole program
             at linktime.  Their analysis stage is performed early to
             both reduce linking times and linktime memory usage by     
             not having to represent whole program in memory.

          d) LTO streaming.  When doing LTO, everything important gets
             streamed into the object file.

      Compile time and or linktime analysis stage (WPA):

             At linktime units gets streamed back and symbol table is
             merged.  Function bodies are not streamed in and not
             available.
          e) IP propagation stage.  All IP passes execute their
             IP propagation. This is done based on the earlier analysis
             without having function bodies at hand.
          f) Ltrans streaming.  When doing WHOPR LTO, the program
             is partitioned and streamed into multiple object files.

      Compile time and/or parallel linktime stage (ltrans)

             Each of the object files is streamed back and compiled
             separately.  Now the function bodies becomes available
             again.

        2) Virtual clone materialization
           (cgraph_materialize_clone)

           IP passes can produce copies of existing functions (such
           as versioned clones or inline clones) without actually
           manipulating their bodies by creating virtual clones in
           the callgraph. At this time the virtual clones are
           turned into real functions
        3) IP transformation

           All IP passes transform function bodies based on earlier
           decision of the IP propagation.

        4) late small IP passes

           Simple IP passes working within single program partition.

        5) Expansion
           (expand_all_functions)

           At this stage functions that needs to be output into
           assembler are identified and compiled in topological order
        6) Output of variables and aliases
           Now it is known what variable references was not optimized
           out and thus all variables are output to the file.

           Note that with -fno-toplevel-reorder passes 5 and 6
           are combined together in cgraph_output_in_order.  

  Finally there are functions to manipulate the callgraph from
  backend.
   - cgraph_add_new_function is used to add backend produced
     functions introduced after the unit is finalized.
     The functions are enqueue for later processing and inserted
     into callgraph with cgraph_process_new_functions.

   - cgraph_function_versioning

     produces a copy of function into new one (a version)
     and apply simple transformations
Queue of cgraph nodes scheduled to be added into cgraph.  This is a
secondary queue used during optimization to accommodate passes that
may generate new functions that need to be optimized and expanded.   

Referenced by cgraph_node::add_new_function(), can_remove_node_now_p_1(), cgraphunit_cc_finalize(), and symbol_table::process_new_functions().

◆ debuginfo_dump_file

FILE* debuginfo_dump_file
static

◆ debuginfo_dump_flags

dump_flags_t debuginfo_dump_flags
static

Referenced by debuginfo_init().

◆ debuginfo_dump_nr

int debuginfo_dump_nr
static
Debug dump file, flags, and number.   

Referenced by debuginfo_early_init(), debuginfo_fini(), and debuginfo_init().

◆ debuginfo_early_dump_file

FILE* debuginfo_early_dump_file
static

◆ debuginfo_early_dump_flags

dump_flags_t debuginfo_early_dump_flags
static

Referenced by debuginfo_init().

◆ debuginfo_early_dump_nr

int debuginfo_early_dump_nr
static
Earlydebug dump file, flags, and number.   

Referenced by debuginfo_early_init(), debuginfo_fini(), and debuginfo_init().

◆ first_analyzed

cgraph_node* first_analyzed
static
Discover all functions and variables that are trivially needed, analyze
them as well as all functions and variables referred by them   

Referenced by analyze_functions(), and cgraphunit_cc_finalize().

◆ first_analyzed_var

varpool_node* first_analyzed_var
static

◆ queued_nodes

◆ symtab_terminator

symtab_node symtab_terminator(SYMTAB_SYMBOL) ( SYMTAB_SYMBOL )
static
Head and terminator of the queue of nodes to be processed while building
callgraph.   

Referenced by analyze_functions(), and cgraphunit_cc_finalize().