GCC Middle and Back End API Reference
ipa.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "cgraph.h"
#include "tree-pass.h"
#include "gimple.h"
#include "ggc.h"
#include "flags.h"
#include "pointer-set.h"
#include "target.h"
#include "tree-iterator.h"
#include "ipa-utils.h"
#include "ipa-inline.h"
#include "tree-inline.h"
#include "profile.h"
#include "params.h"
Include dependency graph for ipa.c:

Functions

static bool cgraph_non_local_node_p_1 ()
static bool cgraph_local_node_p ()
static bool has_addr_references_p (struct cgraph_node *node, void *data)
static void update_inlined_to_pointer ()
static void enqueue_node (symtab_node node, symtab_node *first, struct pointer_set_t *reachable)
static void process_references (struct ipa_ref_list *list, symtab_node *first, bool before_inlining_p, struct pointer_set_t *reachable)
static void walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets, struct cgraph_edge *edge, symtab_node *first, pointer_set_t *reachable, bool before_inlining_p)
bool symtab_remove_unreachable_nodes ()
void ipa_discover_readonly_nonaddressable_vars ()
static bool address_taken_from_non_vtable_p ()
static bool comdat_can_be_unshared_p_1 ()
static bool comdat_can_be_unshared_p ()
static bool cgraph_externally_visible_p (struct cgraph_node *node, bool whole_program)
bool varpool_externally_visible_p ()
bool can_replace_by_local_alias ()
static unsigned int function_and_variable_visibility ()
static unsigned int local_function_and_variable_visibility ()
simple_ipa_opt_passmake_pass_ipa_function_and_variable_visibility ()
static unsigned free_inline_summary ()
simple_ipa_opt_passmake_pass_ipa_free_inline_summary ()
static bool gate_whole_program_function_and_variable_visibility ()
static unsigned int whole_program_function_and_variable_visibility ()
ipa_opt_pass_dmake_pass_ipa_whole_program_visibility ()
static void cgraph_build_static_cdtor_1 ()
void cgraph_build_static_cdtor ()
static void record_cdtor_fn ()
static void build_cdtor ()
static int compare_ctor ()
static int compare_dtor ()
static void build_cdtor_fns ()
static unsigned int ipa_cdtor_merge ()
static bool gate_ipa_cdtor_merge ()
ipa_opt_pass_dmake_pass_ipa_cdtor_merge ()

Variables

static vec< treestatic_ctors
static vec< treestatic_dtors

Function Documentation

static bool address_taken_from_non_vtable_p ( )
static

Return true when there is a reference to node and it is not vtable.

static void build_cdtor ( )
static

Define global constructors/destructor functions for the CDTORS, of which they are LEN. The CDTORS are sorted by initialization priority. If CTOR_P is true, these are constructors; otherwise, they are destructors.

     When there is only one cdtor and target supports them, do nothing.   
     Find the next batch of constructors/destructors with the same
     initialization priority.   
         We do not want to optimize away pure/const calls here.
         When optimizing, these should be already removed, when not
         optimizing, we want user to be able to breakpoint in them.   
     Generate a function to call all the function of like
     priority.   

References DECL_FINI_PRIORITY, DECL_UID, f1, and f2.

static void build_cdtor_fns ( )
static

Generate functions to call static constructors and destructors for targets that do not support .ctors/.dtors sections. These functions have magic names which are detected by collect2.

bool can_replace_by_local_alias ( )

Return true if reference to NODE can be replaced by a local alias. Local aliases save dynamic linking overhead and enable more optimizations.

void cgraph_build_static_cdtor ( )

Generate and emit a static constructor or destructor. WHICH must be one of 'I' (for a constructor), 'D' (for a destructor), 'P' (for chkp static vars constructor) or 'B' (for chkp static bounds constructor). BODY is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the initialization priority for this constructor or destructor.

static void cgraph_build_static_cdtor_1 ( )
static

Generate and emit a static constructor or destructor. WHICH must be one of 'I' (for a constructor), 'D' (for a destructor), 'P' (for chp static vars constructor) or 'B' (for chkp static bounds constructor). BODY is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the initialization priority for this constructor or destructor.

FINAL specify whether the externally visible name for collect2 should be produced.

The priority is encoded in the constructor or destructor name. collect2 will sort the names and arrange that they are called at program startup.

 Proudce sane name but one not recognizable by collect2, just for the
 case we fail to inline the function.   
static bool cgraph_externally_visible_p ( struct cgraph_node node,
bool  whole_program 
)
static

Return true when function NODE should be considered externally visible.

 Do not try to localize built-in functions yet.  One of problems is that we
 end up mangling their asm for WHOPR that makes it impossible to call them
 using the implicit built-in declarations anymore.  Similarly this enables
 us to remove them as unreachable before actual calls may appear during
 expansion or folding.   
 If linker counts on us, we must preserve the function.   
 When doing LTO or whole program, we can bring COMDAT functoins static.
 This improves code quality and we know we will duplicate them at most twice
 (in the case that we are not using plugin and link with object file
  implementing same COMDAT)   
 When doing link time optimizations, hidden symbols become local.   
     Be sure that node is defined in IR file, not in other object
     file.  In that case we don't set used_from_other_object_file.   
static bool cgraph_local_node_p ( )
static

Return true when function can be marked local.

FIXME: thunks can be considered local, but we need prevent i386 from attempting to change calling convention of them.

static bool cgraph_non_local_node_p_1 ( )
static

Basic IPA optimizations and utilities. Copyright (C) 2003-2013 Free Software Foundation, Inc.

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/. Return true when NODE can not be local. Worker for cgraph_local_node_p.

FIXME: Aliases can be local, but i386 gets thunks wrong then.

static bool comdat_can_be_unshared_p ( )
static

COMDAT functions must be shared only if they have address taken, otherwise we can produce our own private implementation with -fwhole-program. Return true when turning COMDAT functoin static can not lead to wrong code when the resulting object links with a library defining same COMDAT.

Virtual functions do have their addresses taken from the vtables, but in C++ there is no way to compare their addresses for equality.

If more than one function is in the same COMDAT group, it must be shared even if just one function in the comdat group has address taken.

static bool comdat_can_be_unshared_p_1 ( )
static

A helper for comdat_can_be_unshared_p.

 When address is taken, we don't know if equality comparison won't
 break eventually. Exception are virutal functions, C++
 constructors/destructors and vtables, where this is not possible by
 language standard.   
 If the symbol is used in some weird way, better to not touch it.   
 Explicit instantiations needs to be output when possibly
 used externally.   
 Non-readonly and volatile variables can not be duplicated.   

References symtab_node_base::next, and symtab_node_base::same_comdat_group.

static int compare_ctor ( )
static

Comparison function for qsort. P1 and P2 are actually of type "tree *" and point to static constructors. DECL_INIT_PRIORITY is used to determine the sort order.

Ensure a stable sort. Constructors are executed in backwarding order to make LTO initialize braries first.

References targetm.

static int compare_dtor ( )
static

Comparison function for qsort. P1 and P2 are actually of type "tree *" and point to static destructors. DECL_FINI_PRIORITY is used to determine the sort order.

Ensure a stable sort.

static void enqueue_node ( symtab_node  node,
symtab_node first,
struct pointer_set_t reachable 
)
static

Add symtab NODE to queue starting at FIRST.

The queue is linked via AUX pointers and terminated by pointer to 1. We enqueue nodes at two occasions: when we find them reachable or when we find their bodies needed for further clonning. In the second case we mark them by pointer to 2 after processing so they are re-queue when they become reachable.

Node is still in queue; do nothing.

 Node was already processed as unreachable, re-enqueue
 only if it became reachable now.   

Referenced by symtab_remove_unreachable_nodes(), and walk_polymorphic_call_targets().

static unsigned free_inline_summary ( )
static
static unsigned int function_and_variable_visibility ( )
static

Mark visibility of all functions.

A local function is one whose calls can occur only in the current compilation unit and all its calls are explicit, so we can change its calling convention. We simply mark all static functions whose address is not taken as local.

We also change the TREE_PUBLIC flag of all declarations that are public in language point of view but we want to overwrite this default via visibilities for the backend point of view.

 All aliases should be procssed at this point.   
     Optimize away PURE and CONST constructors and destructors.   
     Frontends and alias code marks nodes as needed before parsing is finished.
     We may end up marking as node external nodes where this flag is meaningless
     strip it.   
     C++ FE on lack of COMDAT support create local COMDAT functions
     (that ought to be shared but can not due to object format
     limitations).  It is necessary to keep the flag to make rest of C++ FE
     happy.  Clear the flag here to avoid confusion in middle-end.   
     For external decls stop tracking same_comdat_group. It doesn't matter
     what comdat group they are in when they won't be emitted in this TU.   
           cgraph_externally_visible_p has already checked all other nodes
           in the group and they will all be made local.  We need to
           dissolve the group at once so that the predicate does not
           segfault though.  
         Thunks have the same visibility as function they are attached to.
         Make sure the C++ front end set this up properly.   
     If we know that function can not be overwritten by a different semantics
     and moreover its section can not be discarded, replace all direct calls
     by calls to an nonoverwritable alias.  This make dynamic linking
     cheaper and enable more optimization.

     TODO: We can also update virtual tables.   
     weak flag makes no sense on local variables.   
     In several cases declarations can not be common:

     - when declaration has initializer
     - when it is in weak
     - when it has specific section
     - when it resides in non-generic address space.
     - if declaration is local, it will get into .local common section
       so common flag is not needed.  Frontends still produce these in
       certain cases, such as for:

         static int a __attribute__ ((common))

     Canonicalize things here and clear the redundant flag.   

References symtab_node_base::externally_visible, and symtab_node_base::forced_by_abi.

static bool gate_ipa_cdtor_merge ( )
static

Perform the pass when we have no ctors/dtors support or at LTO time to merge multiple constructors into single function.

static bool gate_whole_program_function_and_variable_visibility ( )
static

Do not re-run on ltrans stage.

Referenced by free_inline_summary().

static bool has_addr_references_p ( struct cgraph_node node,
void *  data 
)
static

Return true when NODE has ADDR reference.

static unsigned int ipa_cdtor_merge ( )
static

Look for constructors and destructors and produce function calling them. This is needed for targets not supporting ctors or dtors, but we perform the transformation also at linktime to merge possibly numerous constructors/destructors into single function to improve code locality and reduce size.

void ipa_discover_readonly_nonaddressable_vars ( void  )

Discover variables that have no longer address taken or that are read only and update their flags.

FIXME: This can not be done in between gimplify and omp_expand since readonly flag plays role on what is shared and what is not. Currently we do this transformation as part of whole program visibility and re-do at ipa-reference pass (to take into account clonning), but it would make sense to do it before early optimizations.

Making variable in explicit section readonly can cause section type conflict. See e.g. gcc.c-torture/compile/pr23237.c

References symtab_node_base::decl, dump_file, TREE_READONLY, and varpool_node_name().

static unsigned int local_function_and_variable_visibility ( )
static

Local function pass handling visibilities. This happens before LTO streaming so in particular -fwhole-program should be ignored at this level.

ipa_opt_pass_d* make_pass_ipa_cdtor_merge ( )
simple_ipa_opt_pass* make_pass_ipa_free_inline_summary ( )
simple_ipa_opt_pass* make_pass_ipa_function_and_variable_visibility ( )
ipa_opt_pass_d* make_pass_ipa_whole_program_visibility ( )
static void process_references ( struct ipa_ref_list list,
symtab_node first,
bool  before_inlining_p,
struct pointer_set_t reachable 
)
static

Process references.

We use variable constructors during late complation for constant folding. Keep references alive so partitioning knows about potential references.

static void record_cdtor_fn ( )
static

When target does not have ctors and dtors, we call all constructor and destructor by special initialization/destruction function recognized by collect2.

When we are going to build this function, collect all constructors and destructors and turn them into normal functions.

bool symtab_remove_unreachable_nodes ( )

Perform reachability analysis and reclaim all unreachable nodes.

The algorithm is basically mark&sweep but with some extra refinements:

  • reachable extern inline functions needs special handling; the bodies needs to stay in memory until inlining in hope that they will be inlined. After inlining we release their bodies and turn them into unanalyzed nodes even when they are reachable.

    BEFORE_INLINING_P specify whether we are before or after inlining.

  • virtual functions are kept in callgraph even if they seem unreachable in hope calls to them will be devirtualized.

    Again we remove them after inlining. In late optimization some devirtualization may happen, but it is not importnat since we won't inline the call. In theory early opts and IPA should work out all important cases.

  • virtual clones needs bodies of their origins for later materialization; this means that we want to keep the body even if the origin is unreachable otherwise. To avoid origin from sitting in the callgraph and being walked by IPA passes, we turn them into unanalyzed nodes with body defined.

    We maintain set of function declaration where body needs to stay in body_needed_for_clonning

    Inline clones represent special case: their declaration match the declaration of origin and cgraph_remove_node already knows how to reshape callgraph and preserve body when offline copy of function or inline clone is being removed.

  • C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL variables with DECL_INITIAL set. We finalize these and keep reachable ones around for constant folding purposes. After inlining we however stop walking their references to let everything static referneced by them to be removed when it is otherwise unreachable.

We maintain queue of both reachable symbols (i.e. defined symbols that needs to stay) and symbols that are in boundary (i.e. external symbols referenced by reachable symbols or origins of clones). The queue is represented as linked list by AUX pointer terminated by 1.

A the end we keep all reachable symbols. For symbols in boundary we always turn definition into a declaration, but we may keep function body around based on body_needed_for_clonning

All symbols that enter the queue have AUX pointer non-zero and are in the boundary. Pointer set REACHABLE is used to track reachable symbols.

Every symbol can be visited twice - once as part of boundary and once as real reachable symbol. enqueue_node needs to decide whether the node needs to be re-queued for second processing. For this purpose we set AUX pointer of processed symbols in the boundary to constant 2.

 Mark functions whose bodies are obviously needed.
 This is mostly when they can be referenced externally.  Inline clones
 are special since their declarations are shared with master clone and thus
 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them.   
 Mark variables that are obviously needed.   
 Perform reachability analysis.   
     If we are processing symbol in boundary, mark its AUX pointer for
     possible later re-processing in enqueue_node.   
         If any symbol in a comdat group is reachable, force
         all other in the same comdat group to be also reachable.   
         Mark references as reachable.   
         Mark the callees reachable unless they are direct calls to extern
         inline functions we decided to not inline.   
             Keep alive possible targets for devirtualization.   
             When inline clone exists, mark body to be preserved so when removing
             offline copy of the function we don't kill it.   
             For non-inline clones, force their origins to the boundary and ensure
             that body is not removed.   
     When we see constructor of external variable, keep referred nodes in the
    boundary.  This will also hold initializers of the external vars NODE
    refers to.   
 Remove unreachable functions.    
     If node is not needed at all, remove it.   
     If node is unreachable, remove its body.   
 Inline clones might be kept around so their materializing allows further
 cloning.  If the function the clone is inlined into is removed, we need
 to turn it into normal cone.   
 Remove unreachable variables.   
         For can_refer_decl_in_current_unit_p we want to track for
         all external variables if they are defined in other partition
         or not.   
         Keep body if it may be useful for constant folding.   
 Now update address_taken flags and try to promote functions to be local.   
 If we removed something, perhaps profile could be improved.   

References symtab_node_base::aux, cgraph_can_remove_if_no_direct_calls_and_refs_p(), symtab_node_base::definition, enqueue_node(), gcc_assert, cgraph_node::global, symtab_node_base::in_other_partition, cgraph_global_info::inlined_to, pointer_set_insert(), and cgraph_node::used_as_abstract_origin.

static void update_inlined_to_pointer ( )
static

Look for all functions inlined to NODE and update their inlined_to pointers to INLINED_TO.

bool varpool_externally_visible_p ( )

Return true when variable VNODE should be considered externally visible.

 If linker counts on us, we must preserve the function.   
 See if we have linker information about symbol not being used or
 if we need to make guess based on the declaration.

 Even if the linker clams the symbol is unused, never bring internal
 symbols that are declared by user as used or externally visible.
 This is needed for i.e. references from asm statements.    
 As a special case, the COMDAT virtual tables can be unshared.
 In LTO mode turn vtables into static variables.  The variable is readonly,
 so this does not enable more optimization, but referring static var
 is faster for dynamic linking.  Also this match logic hidding vtables
 from LTO symbol tables.   
 When doing link time optimizations, hidden symbols become local.   
     Be sure that node is defined in IR file, not in other object
     file.  In that case we don't set used_from_other_object_file.   
 Do not attempt to privatize COMDATS by default.
 This would break linking with C++ libraries sharing
 inline definitions.

 FIXME: We can do so for readonly vars with no address taken and
 possibly also for vtables since no direct pointer comparsion is done.
 It might be interesting to do so to reduce linking overhead.   
static void walk_polymorphic_call_targets ( pointer_set_t reachable_call_targets,
struct cgraph_edge edge,
symtab_node first,
pointer_set_t reachable,
bool  before_inlining_p 
)
static

EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark all its potential targets as reachable to permit later inlining if devirtualization happens. After inlining still keep their declarations around, so we can devirtualize to a direct call.

Also try to make trivial devirutalization when no or only one target is possible.

         Do not bother to mark virtual methods in anonymous namespace;
         either we will find use of virtual table defining it, or it is
         unused.   
         Prior inlining, keep alive bodies of possible targets for
         devirtualization.   
         Even after inlining we want to keep the possible targets in the
         boundary, so late passes can still produce direct call even if
         the chance for inlining is lost.   
 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.   

References symtab_node_base::decl, symtab_node_base::definition, enqueue_node(), method_class_type(), pointer_set_insert(), TREE_CODE, TREE_TYPE, and type_in_anonymous_namespace_p().

static unsigned int whole_program_function_and_variable_visibility ( )
static

Bring functionss local at LTO time with -fwhole-program.

References targetm.

Referenced by free_inline_summary().


Variable Documentation

vec<tree> static_ctors
static

A vector of FUNCTION_DECLs declared as static constructors.

vec<tree> static_dtors
static

A vector of FUNCTION_DECLs declared as static destructors.