GCC Middle and Back End API Reference
tree-ssa-coalesce.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "flags.h"
#include "tree-pretty-print.h"
#include "bitmap.h"
#include "dumpfile.h"
#include "gimple.h"
#include "gimple-ssa.h"
#include "tree-phinodes.h"
#include "ssa-iterators.h"
#include "tree-ssanames.h"
#include "hash-table.h"
#include "tree-ssa-live.h"
#include "tree-ssa-coalesce.h"
#include "diagnostic-core.h"
Include dependency graph for tree-ssa-coalesce.c:

Data Structures

struct  coalesce_pair
struct  coalesce_pair_hasher
struct  cost_one_pair_d
struct  coalesce_list_d
struct  ssa_conflicts_d
struct  live_track_d
struct  ssa_name_var_hash

Macros

#define NO_BEST_COALESCE   -1
#define MUST_COALESCE_COST   INT_MAX
#define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL)   FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))

Typedefs

typedef struct coalesce_paircoalesce_pair_p
typedef struct coalesce_pairconst_coalesce_pair_p
typedef hash_table
< coalesce_pair_hasher
coalesce_table_type
typedef
coalesce_table_type::iterator 
coalesce_iterator_type
typedef struct cost_one_pair_dcost_one_pair_p
typedef struct coalesce_list_dcoalesce_list_p
typedef struct ssa_conflicts_dssa_conflicts_p
typedef struct live_track_dlive_track_p

Functions

static int coalesce_cost ()
static int coalesce_cost_bb ()
static int coalesce_cost_edge ()
static int pop_cost_one_pair ()
static int pop_best_coalesce ()
static coalesce_list_p create_coalesce_list ()
static void delete_coalesce_list ()
static coalesce_pair_p find_coalesce_pair ()
static void add_cost_one_coalesce ()
static void add_coalesce ()
static int compare_pairs ()
static int num_coalesce_pairs ()
static void sort_coalesce_list ()
static void dump_coalesce_list ()
static ssa_conflicts_p ssa_conflicts_new ()
static void ssa_conflicts_delete ()
static bool ssa_conflicts_test_p ()
static void ssa_conflicts_add_one ()
static void ssa_conflicts_add ()
static void ssa_conflicts_merge ()
static void ssa_conflicts_dump ()
static live_track_p new_live_track ()
static void delete_live_track ()
static void live_track_remove_partition ()
static void live_track_add_partition ()
static void live_track_clear_var ()
static bool live_track_live_p ()
static void live_track_process_use ()
static void live_track_process_def ()
static void live_track_init ()
static void live_track_clear_base_vars ()
static ssa_conflicts_p build_ssa_conflict_graph ()
static void print_exprs (FILE *f, const char *str1, tree expr1, const char *str2, tree expr2, const char *str3)
static void fail_abnormal_edge_coalesce ()
static var_map create_outofssa_var_map ()
static bool attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y, FILE *debug)
static void coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl, FILE *debug)
var_map coalesce_ssa_name ()

Macro Definition Documentation

#define FOR_EACH_PARTITION_PAIR (   PAIR,
  ITER,
  CL 
)    FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))

Iterate over CL using ITER, returning values in PAIR.

#define MUST_COALESCE_COST   INT_MAX
#define NO_BEST_COALESCE   -1

Typedef Documentation

typedef coalesce_table_type::iterator coalesce_iterator_type
typedef struct coalesce_list_d * coalesce_list_p

This structure maintains the list of coalesce pairs.

typedef struct coalesce_pair * coalesce_pair_p

Coalesce SSA_NAMES together for the out-of-ssa pass. Copyright (C) 2004-2013 Free Software Foundation, Inc. Contributed by Andrew MacLeod amacl.nosp@m.eod@.nosp@m.redha.nosp@m.t.co.nosp@m.m

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 set of routines implements a coalesce_list. This is an object which is used to track pairs of ssa_names which are desirable to coalesce together to avoid copies. Costs are associated with each pair, and when all desired information has been collected, the object can be used to order the pairs for processing. This structure defines a pair entry.

typedef struct cost_one_pair_d * cost_one_pair_p
typedef struct live_track_d * live_track_p

This structure is used to efficiently record the current status of live SSA_NAMES when building a conflict graph. LIVE_BASE_VAR has a bit set for each base variable which has at least one ssa version live. LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an index, and is used to track what partitions of each base variable are live. This makes it easy to add conflicts between just live partitions with the same base variable. The values in LIVE_BASE_PARTITIONS are only valid if the base variable is marked as being live. This delays clearing of these bitmaps until they are actually needed again.

typedef struct ssa_conflicts_d * ssa_conflicts_p

This represents a conflict graph. Implemented as an array of bitmaps. A full matrix is used for conflicts rather than just upper triangular form. this make sit much simpler and faster to perform conflict merges.


Function Documentation

static void add_coalesce ( )
inlinestatic

Add a coalesce between P1 and P2 in list CL with a cost of VALUE.

Once the value is at least MUST_COALESCE_COST - 1, leave it that way.

References coalesce_pair::cost.

Referenced by coalesce_partitions(), create_outofssa_var_map(), and print_exprs().

static void add_cost_one_coalesce ( )
inlinestatic

Referenced by print_exprs().

static bool attempt_coalesce ( var_map  map,
ssa_conflicts_p  graph,
int  x,
int  y,
FILE *  debug 
)
inlinestatic

Attempt to coalesce ssa versions X and Y together using the partition mapping in MAP and checking conflicts in GRAPH. Output any debug info to DEBUG, if it is nun-NULL.

z is the new combined partition. Remove the other partition from the list, and merge the conflicts.

References edge_def::dest_idx, fail_abnormal_edge_coalesce(), edge_def::flags, FOR_EACH_EDGE, gsi_end_p(), gsi_next(), gsi_start_phis(), gsi_stmt(), PHI_ARG_DEF, PHI_RESULT, basic_block_def::preds, and SSA_NAME_VERSION.

static ssa_conflicts_p build_ssa_conflict_graph ( )
static

Build a conflict graph based on LIVEINFO. Any partitions which are in the partition view of the var_map liveinfo is based on get entries in the conflict graph. Only conflicts between ssa_name partitions with the same base variable are added.

     Start with live on exit temporaries.   
         A copy between 2 partitions does not introduce an interference
         by itself.  If they did, you would never be able to coalesce
         two things which are copied.  If the two variables really do
         conflict, they will conflict elsewhere in the program.

         This is handled by simply removing the SRC of the copy from the
         live list, and processing the stmt normally.   
     If result of a PHI is unused, looping over the statements will not
     record any conflicts since the def was never live.  Since the PHI node
     is going to be translated out of SSA form, it will insert a copy.
     There must be a conflict recorded between the result of the PHI and
     any variables that are live.  Otherwise the out-of-ssa translation
     may create incorrect code.   
static int coalesce_cost ( )
inlinestatic

Return cost of execution of copy instruction with FREQUENCY.

Base costs on BB frequencies bounded by 1.

Referenced by create_outofssa_var_map().

static int coalesce_cost_bb ( )
inlinestatic

Return the cost of executing a copy instruction in basic block BB.

References EDGE_CRITICAL_P, edge_def::flags, mult, and MUST_COALESCE_COST.

Referenced by create_outofssa_var_map().

static int coalesce_cost_edge ( )
inlinestatic

Return the cost of executing a copy instruction on edge E.

 Inserting copy on critical edge costs more than inserting it elsewhere.   
           Putting code on EH edge that leads to BB
           with multiple predecestors imply splitting of
           edge too.   
           If there are multiple EH predecestors, we
           also copy EH regions and produce separate
           landing pad.  This is expensive.   

References edge_def::flags.

Referenced by print_exprs().

static void coalesce_partitions ( var_map  map,
ssa_conflicts_p  graph,
coalesce_list_p  cl,
FILE *  debug 
)
static

Attempt to Coalesce partitions in MAP which occur in the list CL using GRAPH. Debug output is sent to DEBUG if it is non-NULL.

 First, coalesce all the copies across abnormal edges.  These are not placed
 in the coalesce list because they do not need to be sorted, and simply
 consume extra memory/compilation time in large programs.   
 Now process the items in the coalesce list.   
     Assert the coalesces have the same base variable.   

References add_coalesce(), BITMAP_ALLOC, BITMAP_FREE, bitmap_set_bit, calculate_live_ranges(), hash_table< Descriptor, Allocator >::create(), create_coalesce_list(), create_outofssa_var_map(), DECL_IGNORED_P, delete_coalesce_list(), hash_table< Descriptor, Allocator >::dispose(), dump_file, dump_flags, dump_var_map(), hash_table< Descriptor, Allocator >::find_slot(), graph, has_zero_uses(), map, MUST_COALESCE_COST, NULL, num_ssa_names, num_var_partitions(), partition_view_bitmap(), ssa_name, SSA_NAME_IS_DEFAULT_DEF, SSA_NAME_VAR, SSA_NAME_VERSION, and TDF_DETAILS.

var_map coalesce_ssa_name ( void  )

Reduce the number of copies by coalescing variables in the function. Return a partition map with the resulting coalesces.

 We need to coalesce all names originating same SSA_NAME_VAR
 so debug info remains undisturbed.   
 Don't calculate live ranges for variables not in the coalesce list.   
 Build a conflict graph.   
 First, coalesce all live on entry variables to their base variable.
 This will ensure the first use is coming from the correct location.   
 Now coalesce everything in the list.   
static int compare_pairs ( )
static

Comparison function to allow qsort to sort P1 and P2 in Ascending order.

Since qsort does not guarantee stability we use the elements as a secondary key. This provides us with independence from the host's implementation of the sorting algorithm.

References hash_table< Descriptor, Allocator >::elements(), and coalesce_list_d::list.

static coalesce_list_p create_coalesce_list ( )
inlinestatic
static var_map create_outofssa_var_map ( )
static

This function creates a var_map for the current function as well as creating a coalesce list for use later in the out of ssa process.

         Register ssa_names and coalesces between the args and the result
         of all PHI.   
         Register USE and DEF operands in each statement.   
         Check for copy coalesces.   
 Now process result decls and live on entry variables for entry into
 the coalesce list.   
         Add coalesces between all the result decls.   
         Mark any default_def variables as being in the coalesce list
         since they will have to be coalesced with the base variable.  If
         not marked as present, they won't be in the coalesce view.  

References add_coalesce(), alloca, bitmap_set_bit, coalesce_cost(), coalesce_cost_bb(), gimple_asm_input_op(), gimple_asm_ninputs(), gimple_asm_noutputs(), gimple_asm_output_op(), gimple_assign_lhs(), gimple_assign_rhs1(), gimple_assign_ssa_name_copy_p(), gimple_can_coalesce_p(), optimize_bb_for_size_p(), REG_BR_PROB_BASE, SSA_NAME_VERSION, TREE_CODE, TREE_PURPOSE, TREE_STRING_POINTER, and TREE_VALUE.

Referenced by coalesce_partitions().

static void delete_coalesce_list ( )
inlinestatic

Delete coalesce list CL.

Referenced by coalesce_partitions().

static void delete_live_track ( )
static

This routine will free the memory associated with PTR.

static void dump_coalesce_list ( )
static

Send debug info for coalesce list CL to file F.

static void fail_abnormal_edge_coalesce ( )
inlinestatic

Print a failure to coalesce a MUST_COALESCE pair X and Y.

Referenced by attempt_coalesce().

static coalesce_pair_p find_coalesce_pair ( )
static

Find a matching coalesce pair object in CL for the pair P1 and P2. If one isn't found, return NULL if CREATE is false, otherwise create a new coalesce pair object and return it.

Normalize so that p1 is the smaller value.

References coalesce_pair::cost, coalesce_pair::first_element, gcc_assert, NULL, coalesce_pair::second_element, and coalesce_list_d::sorted.

static void live_track_add_partition ( )
inlinestatic

This function will adds PARTITION to the live list in PTR.

If this base var wasn't live before, it is now. Clear the element list since it was delayed until needed.

Referenced by live_track_live_p().

static void live_track_clear_base_vars ( )
inlinestatic

This routine will clear all live partitions in PTR.

Simply clear the live base list. Anything marked as live in the element lists will be cleared later if/when the base variable ever comes alive again.

References gsi_stmt(), live_track_live_p(), live_track_process_def(), and PHI_RESULT.

static void live_track_clear_var ( )
inlinestatic

Clear the live bit for VAR in PTR.

Referenced by live_track_init().

static void live_track_init ( )
inlinestatic

Initialize PTR with the partitions set in INIT.

Mark all live on exit partitions.

References gimple_assign_copy_p(), gimple_assign_lhs(), gimple_assign_rhs1(), live_track_clear_var(), and TREE_CODE.

static bool live_track_live_p ( )
inlinestatic

Return TRUE if VAR is live in PTR.

References EXECUTE_IF_SET_IN_BITMAP, and live_track_add_partition().

Referenced by live_track_clear_base_vars().

static void live_track_process_def ( )
inlinestatic

This routine will process a DEF in PTR. DEF will be removed from the live lists, and if there are any other live partitions with the same base variable, conflicts will be added to GRAPH.

Clear the liveness bit.

 If the bitmap isn't empty now, conflicts need to be added.   

Referenced by live_track_clear_base_vars().

static void live_track_process_use ( )
inlinestatic

This routine will add USE to PTR. USE will be marked as live in both the ssa live map and the live bitmap for the root of USE.

Mark as live in the appropriate live list.

References bitmap_clear(), and live_track_d::live_base_var.

static void live_track_remove_partition ( )
inlinestatic

This function will remove PARTITION from the live list in PTR.

If the element list is empty, make the base variable not live either.

References basevar_index(), bitmap_bit_p, live_track_d::live_base_partitions, live_track_d::live_base_var, and live_track_d::map.

static live_track_p new_live_track ( )
static

This routine will create a new live track structure based on the partitions in MAP.

Make sure there is a partition view in place.

References basevar_index(), bitmap_clear(), bitmap_set_bit, live_track_d::live_base_partitions, live_track_d::live_base_var, and live_track_d::map.

static int num_coalesce_pairs ( )
inlinestatic

Return the number of unique coalesce pairs in CL.

static int pop_best_coalesce ( )
inlinestatic

Retrieve the most expensive remaining pair to coalesce from CL. Returns the 2 elements via P1 and P2. Their calculated cost is returned by the function. NO_BEST_COALESCE is returned if the coalesce list is empty.

static int pop_cost_one_pair ( )
inlinestatic

Retrieve a pair to coalesce from the cost_one_list in CL. Returns the 2 elements via P1 and P2. 1 is returned by the function if there is a pair, NO_BEST_COALESCE is returned if there aren't any.

static void print_exprs ( FILE *  f,
const char *  str1,
tree  expr1,
const char *  str2,
tree  expr2,
const char *  str3 
)
inlinestatic

Shortcut routine to print messages to file F of the form: "STR1 EXPR1 STR2 EXPR2 STR3."

References add_coalesce(), add_cost_one_coalesce(), bitmap_set_bit, coalesce_cost_edge(), edge_def::flags, has_single_use(), and SSA_NAME_VERSION.

static void sort_coalesce_list ( )
static

Prepare CL for removal of preferred pairs. When finished they are sorted in order from most important coalesce to least important.

 Allocate a vector for the pair pointers.   
 Populate the vector with pointers to the pairs.   
 Already sorted.   
 If there are only 2, just pick swap them if the order isn't correct.   
 Only call qsort if there are more than 2 items.   

References coalesce_pair::cost, and coalesce_list_d::sorted.

static void ssa_conflicts_add ( )
inlinestatic

Add conflicts between X and Y in graph PTR.

static void ssa_conflicts_add_one ( )
inlinestatic

Add a conflict with Y to the bitmap for X in graph PTR.

If there are no conflicts yet, allocate the bitmap and set bit.

References bitmap_set_bit, and ssa_conflicts_d::conflicts.

static void ssa_conflicts_delete ( )
inlinestatic

Free storage for conflict graph PTR.

static void ssa_conflicts_dump ( )
static

Dump a conflicts graph.

static void ssa_conflicts_merge ( )
inlinestatic

Merge all Y's conflict into X in graph PTR.

 Add a conflict between X and every one Y has.  If the bitmap doesn't
 exist, then it has already been coalesced, and we don't need to add a
 conflict.   
     If X has conflicts, add Y's to X.   
     If X has no conflicts, simply use Y's.   
static ssa_conflicts_p ssa_conflicts_new ( )
inlinestatic

Return an empty new conflict graph for SIZE elements.

static bool ssa_conflicts_test_p ( )
inlinestatic

Test if elements X and Y conflict in graph PTR.

Avoid the lookup if Y has no conflicts.

References ssa_conflicts_d::conflicts, EXECUTE_IF_SET_IN_BITMAP, and gcc_checking_assert.