GCC Middle and Back End API Reference
cgraphunit.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "output.h"
#include "rtl.h"
#include "gimple.h"
#include "gimple-ssa.h"
#include "tree-cfg.h"
#include "tree-into-ssa.h"
#include "tree-ssa.h"
#include "tree-inline.h"
#include "langhooks.h"
#include "pointer-set.h"
#include "toplev.h"
#include "flags.h"
#include "ggc.h"
#include "debug.h"
#include "target.h"
#include "diagnostic.h"
#include "params.h"
#include "fibheap.h"
#include "intl.h"
#include "function.h"
#include "ipa-prop.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "gimple-pretty-print.h"
#include "coverage.h"
#include "plugin.h"
#include "ipa-inline.h"
#include "ipa-utils.h"
#include "lto-streamer.h"
#include "except.h"
#include "cfgloop.h"
#include "regset.h"
#include "context.h"
#include "pass_manager.h"
#include "gt-cgraphunit.h"
Include dependency graph for cgraphunit.c:

Data Structures

struct  cgraph_order_sort

Enumerations

enum  cgraph_order_sort_kind { ORDER_UNDEFINED = 0, ORDER_FUNCTION, ORDER_VAR, ORDER_ASM }

Functions

static void expand_all_functions (void)
static void mark_functions_to_output (void)
static void expand_function (struct cgraph_node *)
static void analyze_function (struct cgraph_node *)
static void handle_alias_pairs (void)
bool decide_is_symbol_needed ()
static void enqueue_node ()
bool cgraph_process_new_functions ()
void cgraph_reset_node ()
static bool referred_to_p ()
void cgraph_finalize_function ()
void cgraph_add_new_function ()
struct asm_nodeadd_asm_node ()
static void output_asm_statements ()
static void analyze_function ()
void cgraph_process_same_body_aliases ()
static void process_common_attributes ()
static void process_function_and_variable_attributes (struct cgraph_node *first, struct varpool_node *first_var)
void varpool_finalize_decl ()
static void walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets, struct cgraph_edge *edge)
static void analyze_functions ()
basic_block init_lowered_empty_function ()
static tree thunk_adjust (gimple_stmt_iterator *bsi, tree ptr, bool this_adjusting, HOST_WIDE_INT fixed_offset, tree virtual_offset)
bool expand_thunk ()
static void assemble_thunks_and_aliases ()
static void expand_function ()
static void output_in_order ()
static void ipa_passes ()
static tree get_alias_symbol ()
static void output_weakrefs ()
void init_cgraph ()
void compile ()
void finalize_compilation_unit ()

Variables

cgraph_node_set cgraph_new_nodes
FILE * cgraph_dump_file
struct asm_nodeasm_nodes
static struct asm_nodeasm_last_node
static tree vtable_entry_type
static symtab_node first = (symtab_node)(void *)1

Enumeration Type Documentation

This is used to sort the node types by the cgraph order number.

Enumerator:
ORDER_UNDEFINED 
ORDER_FUNCTION 
ORDER_VAR 
ORDER_ASM 

Function Documentation

struct asm_node* add_asm_node ( )
read

Add a top-level asm statement to the list.

static void analyze_function ( struct cgraph_node )
static
static void analyze_function ( )
static

Analyze the function scheduled to be output.

     Generate the dispatcher body of multi-versioned functions.   
     Make sure to gimplify bodies only once.  During analyzing a
     function we lower it, which will require gimplified nested
     functions, so we can end up here with an already gimplified
     body.   
     Lower the function.   
static void analyze_functions ( )
static

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

 Keep track of already processed nodes when called multiple times for
 intermodule optimization.   
 Ugly, but the fixup can not happen at a time same body alias is created;
 C++ FE is confused about the COMDAT groups being right.   
 Analysis adds static variables that in turn adds references to new functions.
 So we need to iterate the process until it stabilize.   
     First identify the trivially needed symbols.   
     Lower representation, build callgraph edges and references for all trivially
     needed symbols and all symbols referred by them.   
             ??? It is possible to create extern inline function
          and later using weak alias attribute to kill its body.
          See gcc.c-torture/compile/20011119-1.c   
             If decl is a clone of an abstract function,
          mark that abstract function so that we don't release its body.
          The DECL_INITIAL() of that abstract function declaration
          will be later needed to output debug info.   
 Collect entry points to the unit.   
 Initialize assembler name hash, in particular we want to trigger C++
 mangling and same body alias creation before we free DECL_ARGUMENTS
 used by it.   
static void assemble_thunks_and_aliases ( )
static

Assemble thunks and aliases associated to NODE.

Force assemble_alias to really output the alias this time instead of buffering it in same alias pairs.

References compare_tree_int(), TREE_INT_CST_LOW, TYPE_SIZE_UNIT, and warning().

void cgraph_add_new_function ( )

Add the function FNDECL to the call graph. Unlike cgraph_finalize_function, this function is intended to be used by middle end and allows insertion of new function at arbitrary point of compilation. The function can be either in high, low or SSA form GIMPLE.

The function is assumed to be reachable and have address taken (so no API breaking optimizations are performed on it).

Main work done by this function is to enqueue the function for later processing to avoid need the passes to be re-entrant.

       Just enqueue function to be processed at nearest occurrence.   
       Bring the function into finalized state and enqueue for later
       analyzing and compilation.   
       At the very end of compilation we have to do all the work up
       to expansion.   
 Set a personality if required and we already passed EH lowering.   
void cgraph_finalize_function ( )

DECL has been parsed. Take it, queue it, compile it at the whim of the logic in effect. If NO_COLLECT is true, then our caller cannot stand to have the garbage collector run at the moment. We would need to either create a new GC context, or just not compile right now.

Nested functions should only be defined once.   
 With -fkeep-inline-functions we are keeping all inline functions except
 for extern inline ones.   


 When not optimizing, also output the static functions. (see
 PR24561), but don't do so for always_inline functions, functions
 declared inline and nested functions.  These were optimized out
 in the original implementation and it is unclear whether we want
 to change the behavior here.   
 If we've not yet emitted decl, tell the debug info about it.   
 Possibly warn about unused parameters.   

References debug_hooks.

bool cgraph_process_new_functions ( void  )

Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these functions into callgraph in a way so they look like ordinary reachable functions inserted into callgraph already at construction time.

  Note that this queue may grow as its being processed, as the new
  functions may generate new ones.   
         At construction time we just need to finalize function and move
         it into reachable functions list.   
         When IPA optimization already started, do all essential
         transformations that has been already performed on the whole
         cgraph but not on this function.   
         Functions created during expansion shall be compiled
         directly.   

References analyze_function(), symtab_node_base::analyzed, CDI_DOMINATORS, CDI_POST_DOMINATORS, cgraph_call_function_insertion_hooks(), cgraph_finalize_function(), CGRAPH_STATE_CONSTRUCTION, CGRAPH_STATE_EXPANSION, CGRAPH_STATE_IPA, CGRAPH_STATE_IPA_SSA, compute_inline_parameters(), DECL_STRUCT_FUNCTION, enqueue_node(), gcc::pass_manager::execute_early_local_passes(), expand_function(), free_dominance_info(), g, gcc_unreachable, gcc::context::get_passes(), gimple_in_ssa_p(), gimple_register_cfg_hooks(), inline_summary_vec, NULL, pop_cfun(), cgraph_node::process, and push_cfun().

void cgraph_process_same_body_aliases ( void  )

C++ frontend produce same body aliases all over the place, even before PCH gets streamed out. It relies on us linking the aliases with their function in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we first produce aliases without links, but once C++ FE is sure he won't sream PCH we build the links via this function.

void cgraph_reset_node ( )

As an GCC extension we allow redefinition of the function. The semantics when both copies of bodies differ is not well defined. We replace the old body with new body so in unit at a time mode we always use new body, while in normal mode we may end up with old body inlined into some functions and new body expanded and inlined in others.

??? It may make more sense to use one body for inlining and other body for expanding the function but this is difficult to do.

If node->process is set, then we have already begun whole-unit analysis. This is not testing for whether we've already emitted the function. That case can be sort-of legitimately seen with real function redefinition errors. I would argue that the front end should never present us with such a case, but don't enforce that for now.

 Reset our data structures so we can analyze the function again.   
void compile ( void  )

Perform simple optimizations based on callgraph.

 If LTO is enabled, initialize the streamer hooks needed by GIMPLE.   
 Don't run the IPA passes if there was any error or sorry messages.   
 Do nothing else if any IPA pass found errors or if we are just streaming LTO.   
 This pass remove bodies of extern inline functions we never inlined.
 Do this later so other IPA passes see what is really going on.   
 Output everything.   
 When weakref support is missing, we autmatically translate all
 references to NODE to references to its ultimate alias target.
 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
 TREE_CHAIN.

 Set up this mapping before we output any assembler but once we are sure
 that all symbol renaming is done.

 FIXME: All this uglyness can go away if we just do renaming at gimple
 level by physically rewritting the IL.  At the moment we can only redirect
 calls, so we need infrastructure for renaming references as well.   

References dump_cgraph_node().

bool decide_is_symbol_needed ( )

Determine if symbol DECL is needed. That is, visible to something either outside this translation unit, something magic in the system configury

 Double check that no one output the function into assembly file
 early.   
 If the user told us it is used, then it must be so.   
 ABI forced symbols are needed when they are external.   
Keep constructors, destructors and virtual functions.   
 Externally visible variables must be output.  The exception is
 COMDAT variables that must be output only when they are needed.   
static void enqueue_node ( )
static

Add NODE to queue starting at FIRST. The queue is linked via AUX pointers and terminated by pointer to 1.

References csi_end_p(), csi_next(), csi_node(), csi_start(), and handle_alias_pairs().

Referenced by cgraph_process_new_functions(), and varpool_finalize_decl().

static void expand_all_functions ( )
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).

Garbage collector may remove inline clones we eliminate during optimization. So we must be sure to not reference them.

References cgraph_order_sort::a, asm_node::asm_str, assemble_asm(), expand_function(), cgraph_order_sort::f, gcc_unreachable, ORDER_ASM, ORDER_FUNCTION, ORDER_UNDEFINED, ORDER_VAR, cgraph_node::process, cgraph_order_sort::u, cgraph_order_sort::v, and varpool_assemble_decl().

static void expand_function ( struct cgraph_node )
static
static void expand_function ( )
static

Expand function specified by NODE.

 We ought to not compile any inline clones.   
 Generate RTL for the body of DECL.   
 Initialize the default bitmap obstack.   
 Initialize the RTL code for the function.   
 Perform all tree transforms and optimizations.   
 Signal the start of passes.   
 Signal the end of passes.   
 Release the default bitmap obstack.   
 If requested, warn about function definitions where the function will
 return a value (usually of some struct or union type) which itself will
 take up a lot of stack space.   
     Stop pointing to the local nodes about to be freed.
     But DECL_INITIAL must remain nonzero so we know this
     was an actual function definition.
     For a nested function, this is done in c_pop_function_context.
     If rest_of_compilation set this to 0, leave it 0.   
 Make sure that BE didn't give up on compiling.   
 It would make a lot more sense to output thunks before function body to get more
 forward and lest backwarding jumps.  This however would need solving problem
 with comdats. See PR48668.  Also aliases must come after function itself to
 make one pass assemblers, like one on AIX, happy.  See PR 50689.
 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
 groups.   
 Eliminate all call edges.  This is important so the GIMPLE_CALL no longer
 points to the dead function body.   
bool expand_thunk ( )

Expand thunk NODE to gimple if possible. When OUTPUT_ASM_THUNK is true, also produce assembler for thunks that are not lowered.

     Ensure thunks are emitted in their correct sections.   
     The back end expects DECL_INITIAL to contain a BLOCK, so we
     create one.   
     Ensure thunks are emitted in their correct sections.   
     Build the return declaration for the function.   
     Build call to the function being thunked.   
                 If the return type is a pointer, we need to
                 protect against NULL.  We know there will be an
                 adjustment, because that's why we're emitting a
                 thunk.   
         Build return value.   
     FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks.   
     Since we want to emit the thunk, we explicitly mark its name as
     referenced.   
void finalize_compilation_unit ( void  )

Analyze the whole compilation unit once it is parsed completely.

 If we're here there's no current function anymore.  Some frontends
 are lazy in clearing these.   
 Do not skip analyzing the functions if there were errors, we
 miss diagnostics for following functions otherwise.   
 Emit size functions we didn't inline.   
 Mark alias targets necessary and emit diagnostics.   
 Gimplify and lower all functions, compute reachability and
 remove unreachable nodes.   
 Mark alias targets necessary and emit diagnostics.   
 Gimplify and lower thunks.   
 Finally drive the pass manager.   
static tree get_alias_symbol ( )
static

Return string alias is alias of.

static void handle_alias_pairs ( )
static

Translate the ugly representation of aliases as alias pairs into nice representation in callgraph. We don't handle all cases yet, unfortunately.

Weakrefs with target not defined in current unit are easy to handle: they behave just as external variables except we need to note the alias flag to later output the weakref pseudo op into asm file.

         We use local aliases for C++ thunks to force the tailcall
         to bind locally.  This is a hack - to keep it working do
         the following (which is not strictly correct).   

References alias_pairs, symtab_node_base::decl, alias_pair::decl, error(), and warning().

Referenced by enqueue_node().

void init_cgraph ( void  )

Initialize callgraph dump file.

References cgraph_dump_file, and dump_symtab().

basic_block init_lowered_empty_function ( )

DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function in lowered gimple form. IN_SSA is true if the gimple is in SSA.

Set current_function_decl and cfun to newly constructed empty function body. return basic block in the function body.

Create BB for body of the function and connect it properly.

static void ipa_passes ( )
static
 We never run removal of unreachable nodes after early passes.  This is
 because TODO is run before the subpasses.  It is important to remove
 the unreachable functions to save works at IPA level and to get LTO
 symbol tables right.   
 If pass_all_early_optimizations was not scheduled, the state of
 the cgraph will not be properly updated.  Update it now.   
     Generate coverage variables and constructors.   
     Process new functions added.   
 Some targets need to handle LTO assembler output specially.   

References cgraph_dump_file, dump_begin(), NULL, and TDI_cgraph.

static void mark_functions_to_output ( )
static

Figure out what functions we want to assemble.

We need to output all local functions that are used and not always inlined, as well as those that are reachable from outside the current compilation unit.

         We should've reclaimed all functions that are not needed.   
static void output_asm_statements ( )
static

Output all asm statements we have stored up to be output.

References targetm.

static void output_in_order ( )
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.

In toplevel reorder mode we output all statics; mark them as needed.

static void output_weakrefs ( )
static

Weakrefs may be associated to external decls and thus not output at expansion time. Emit all necessary aliases.

Weakrefs are special by not requiring target definition in current compilation unit. It is thus bit hard to work out what we want to alias. When alias target is defined, we need to fetch it from symtab reference, otherwise it is pointed to by alias_target.

References symtab_node_base::alias_target, symtab_node_base::decl, DECL_ASSEMBLER_NAME, IDENTIFIER_TRANSPARENT_ALIAS, symtab_alias_target(), and TREE_CHAIN.

static void process_function_and_variable_attributes ( struct cgraph_node first,
struct 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 cgraph_finalize_function because:

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

is valid.

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

redefining extern inline function makes it DECL_UNINLINABLE.

References symtab_node_base::decl, DECL_SOURCE_LOCATION, TREE_PUBLIC, and warning_at().

static bool referred_to_p ( )
static

Return true when there are references to NODE.

See if there are any references at all.

 For functions check also calls.   

References cgraph_reset_node(), DECL_CONTEXT, gcc_assert, cgraph_node::local, cgraph_local_info::redefined_extern_inline, and TREE_CODE.

static tree thunk_adjust ( gimple_stmt_iterator bsi,
tree  ptr,
bool  this_adjusting,
HOST_WIDE_INT  fixed_offset,
tree  virtual_offset 
)
static

Adjust PTR by the constant FIXED_OFFSET, and by the vtable offset indicated by VIRTUAL_OFFSET, if that is non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and zero for a result adjusting thunk.

 If there's a virtual offset, look up that value in the vtable and
 adjust the pointer again.   
     The vptr is always at offset zero in the object.   
     Form the vtable address.   
     Find the entry with the vcall offset.   
     Get the offset itself.   
     Adjust the `this' pointer.   
   Adjust the pointer by the constant.   
 Emit the statement and gimplify the adjustment expression.   

References create_tmp_reg(), fold_build_pointer_plus_hwi_loc(), gimple_build_assign, gsi_insert_after(), GSI_NEW_STMT, input_location, TREE_CODE, and TREE_TYPE.

void varpool_finalize_decl ( )

Mark DECL as finalized. By finalizing the declaration, frontend instruct the middle end to output the variable to asm file, if needed or externally visible.

Traditionally we do not eliminate static variables when not
optimizing and when not doing toplevel reoder.   

Some frontends produce various interface variables after compilation finished.

References cgraph_dump_file, symtab_node_base::definition, dump_possible_polymorphic_call_targets(), enqueue_node(), method_class_type(), TREE_CODE, TREE_TYPE, and type_in_anonymous_namespace_p().

static void walk_polymorphic_call_targets ( pointer_set_t reachable_call_targets,
struct cgraph_edge edge 
)
static

EDGE is an polymorphic call. Mark all possible targets as reachable and if there is only one target, perform trivial devirtualization. REACHABLE_CALL_TARGETS collects target lists we already walked to avoid udplicate work.

    Do not bother to mark virtual methods in anonymous namespace;
    either we will find use of virtual table defining it, or it is
    unused.   

Very trivial devirtualization; when the type is final or anonymous (so we know all its derivation) and there is only one possible virtual call target, make the edge direct.


Variable Documentation

struct asm_node* asm_last_node
static

Last node in cgraph_asm_nodes.

struct asm_node* asm_nodes

Linked list of cgraph asm nodes.

cgraph_node_set cgraph_new_nodes

Driver of optimization process Copyright (C) 2003-2013 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:

  • cgraph_finalize_function

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

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

  • varpool_finalize_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 optimiations 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, transational 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 sreaming. 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 multple 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 functoins (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.

tree vtable_entry_type
static

Used for vtable lookup in thunk adjusting.