GCC Middle and Back End API Reference
postreload-gcse.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "diagnostic-core.h"
#include "hash-table.h"
#include "rtl.h"
#include "tree.h"
#include "tm_p.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "flags.h"
#include "insn-config.h"
#include "recog.h"
#include "basic-block.h"
#include "function.h"
#include "expr.h"
#include "except.h"
#include "intl.h"
#include "obstack.h"
#include "hashtab.h"
#include "params.h"
#include "target.h"
#include "tree-pass.h"
#include "dbgcnt.h"
Include dependency graph for postreload-gcse.c:

Data Structures

struct  expr
struct  expr_hasher
struct  occr
struct  unoccr
struct  modifies_mem

Macros

#define INSN_CUID(INSN)   (uid_cuid[INSN_UID (INSN)])

Functions

static hashval_t hash_expr ()
static void alloc_mem (void)
static void free_mem (void)
static bool oprs_unchanged_p (rtx, rtx, bool)
static void record_last_reg_set_info (rtx, rtx)
static void record_last_reg_set_info_regno (rtx, int)
static void record_last_mem_set_info (rtx)
static void record_last_set_info (rtx, const_rtx, void *)
static void record_opr_changes (rtx)
static void find_mem_conflicts (rtx, const_rtx, void *)
static int load_killed_in_block_p (int, rtx, bool)
static void reset_opr_set_tables (void)
static hashval_t hash_expr (rtx, int *)
static void insert_expr_in_table (rtx, rtx)
static struct exprlookup_expr_in_table (rtx)
static void dump_hash_table (FILE *)
static bool reg_killed_on_edge (rtx, edge)
static bool reg_used_on_edge (rtx, edge)
static rtx get_avail_load_store_reg (rtx)
static bool bb_has_well_behaved_predecessors (basic_block)
static struct occrget_bb_avail_insn (basic_block, struct occr *)
static void hash_scan_set (rtx)
static void compute_hash_table (void)
static void eliminate_partially_redundant_load (basic_block, rtx, struct expr *)
static void eliminate_partially_redundant_loads (void)
static void insert_expr_in_table ()
static struct exprlookup_expr_in_table ()
int dump_expr_hash_table_entry ()
static void dump_hash_table ()
static bool reg_changed_after_insn_p ()
static bool oprs_unchanged_p ()
static int load_killed_in_block_p ()
static void record_last_reg_set_info ()
static void record_last_reg_set_info_regno ()
static void record_last_mem_set_info ()
static void record_last_set_info ()
static void record_opr_changes ()
static void hash_scan_set ()
static bool reg_killed_on_edge ()
static bool reg_used_on_edge ()
static rtx get_avail_load_store_reg ()
static bool bb_has_well_behaved_predecessors ()
static struct occrget_bb_avail_insn ()
int delete_redundant_insns_1 ()
static void delete_redundant_insns ()
static void gcse_after_reload_main ()
static bool gate_handle_gcse2 ()
static unsigned int rest_of_handle_gcse2 ()
rtl_opt_passmake_pass_gcse2 ()

Variables

struct {
   int   moves_inserted
   int   copies_inserted
   int   insns_deleted
stats
static hash_table< expr_hasherexpr_table
static struct obstack expr_obstack
static struct obstack occr_obstack
static struct obstack unoccr_obstack
static int * reg_avail_info
static struct modifies_memmodifies_mem_list
static struct obstack modifies_mem_obstack
static struct modifies_memmodifies_mem_obstack_bottom
static int * uid_cuid
static int mems_conflict_p

Macro Definition Documentation

#define INSN_CUID (   INSN)    (uid_cuid[INSN_UID (INSN)])

Referenced by load_killed_in_block_p().


Function Documentation

static void alloc_mem ( void  )
static

Helpers for memory allocation/freeing.

Allocate memory for the CUID mapping array and register/memory tracking tables.

 Find the largest UID and create a mapping from UIDs to CUIDs.   
 Allocate the available expressions hash table.  We don't want to
 make the hash table too small, but unnecessarily making it too large
 also doesn't help.  The i/4 is a gcse.c relic, and seems like a
 reasonable choice.   
 We allocate everything on obstacks because we often can roll back
 the whole obstack to some point.  Freeing obstacks is very fast.   
 Working array used to track the last set for each register
 in the current block.   
 Put a dummy modifies_mem object on the modifies_mem_obstack, so we
 can roll it back in reset_opr_set_tables.   
static bool bb_has_well_behaved_predecessors ( basic_block  )
static
static bool bb_has_well_behaved_predecessors ( )
static

Return nonzero if the predecessors of BB are "well behaved".

static void compute_hash_table ( void  )
static

Create hash table of memory expressions available at end of basic blocks. Basically you should think of this hash table as the representation of AVAIL_OUT. This is the set of expressions that is generated in a basic block and not killed before the end of the same basic block. Notice that this is really a local computation.

First pass over the instructions records information used to determine when registers and memory are last set. Since we compute a "local" AVAIL_OUT, reset the tables that help us keep track of what has been modified since the start of the block.

     The next pass actually builds the hash table.   

References BB_END, cfun, EDGE_COUNT, EDGE_CRITICAL_P, edge_def::flags, FOR_EACH_EDGE, NULL, basic_block_def::preds, edge_def::src, and tablejump_p().

static void delete_redundant_insns ( )
static
int delete_redundant_insns_1 ( )

Go over the expression hash table and delete insns that were marked for later deletion. This helper is called via htab_traverse.

References execute(), gate_handle_gcse2(), and rest_of_handle_gcse2().

int dump_expr_hash_table_entry ( )

Dump all expressions and occurrences that are currently in the expression hash table to FILE. This helper is called via htab_traverse.

static void dump_hash_table ( FILE *  )
static
static void dump_hash_table ( )
static
static void eliminate_partially_redundant_load ( basic_block  bb,
rtx  insn,
struct expr expr 
)
static

The work horses of this pass.

This handles the case where several stores feed a partially redundant load. It checks if the redundancy elimination is possible and if it's worth it.

Redundancy elimination is possible if, 1) None of the operands of an insn have been modified since the start of the current basic block. 2) In any predecessor of the current basic block, the same expression is generated.

See the function body for the heuristics that determine if eliminating a redundancy is also worth doing, assuming it is possible.

 The execution count of the loads to be added to make the
 load fully redundant.   
 Check that the loaded register is not used, set, or killed from the
 beginning of the block.   
 Check potential for replacing load with copy for predecessors.   
         Check if the loaded register is not used.   
         Make sure we can generate a move from register avail_reg to
         dest.   
           AVAIL_INSN remains non-null.   
             Check if there is going to be a split.   
         Adding a load on a critical edge will cause a split.   
     Prevent exploding the code.   
     If we don't have profile information we cannot tell if splitting
     a critical edge is profitable or not so don't do it.   
 Check if it's worth applying the partial redundancy elimination.   
 Generate moves to the loaded register from where
 the memory is available.   
     Set avail_reg to be the register having the value of the
     memory.   
 Regenerate loads where the memory is unavailable.   
 Delete the insn if it is not available in this block and mark it
 for deletion if it is available. If insn is available it may help
 discover additional redundancies, so mark it for later deletion.   
static void eliminate_partially_redundant_loads ( )
static

Performing the redundancy elimination as described before.

 Note we start at block 1.   
     Don't try anything on basic blocks with strange predecessors.   
     Do not try anything on cold basic blocks.   
     Reset the table of things changed since the start of the current
     basic block.   
     Look at all insns in the current basic block and see if there are
     any loads in it that we can record.   
         Is it a load - of the form (set (reg) (mem))?   
                 Are the operands unchanged since the start of the
                 block?   
                 Is the expression recorded?   
                 We now have a load (insn) and an available memory at
                 its BB start (expr). Try to remove the loads if it is
                 redundant.   
         Keep track of everything modified by this insn, so that we
         know what has been modified since the start of the current
         basic block.   
static void find_mem_conflicts ( rtx  dest,
const_rtx  setter,
void *  data 
)
static

DEST is the output of an instruction. If it is a memory reference, and possibly conflicts with the load found in DATA, then set mems_conflict_p to a nonzero value.

If DEST is not a MEM, then it will not conflict with the load. Note that function calls are assumed to clobber memory, but are handled elsewhere.

static void free_mem ( )
static

Free memory allocated by alloc_mem.

static bool gate_handle_gcse2 ( )
static
static void gcse_after_reload_main ( )
static

Main entry point of the GCSE after reload - clean some redundant loads due to spilling.

 Allocate memory for this pass.
 Also computes and initializes the insns' CUIDs.   
 We need alias analysis.   
 We are finished with alias.   
static rtx get_avail_load_store_reg ( rtx  )
static
static rtx get_avail_load_store_reg ( )
static

Return the loaded/stored register of a load/store instruction.

A load.

     A store.   
static struct occr* get_bb_avail_insn ( basic_block  ,
struct occr  
)
staticread
static struct occr* get_bb_avail_insn ( )
staticread

Search for the occurrences of expression in BB.

static hashval_t hash_expr ( )
static

Hash expression X. DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found or if the expression contains something we don't want to insert in the table.

static hashval_t hash_expr ( rtx  ,
int *   
)
static

Hash table support.

static void hash_scan_set ( rtx  )
static
static void hash_scan_set ( )
static

Scan the pattern of INSN and add an entry to the hash TABLE. After reload we are interested in loads/stores only.

 We are only interested in loads and stores.   
 Don't mess with jumps and nops.   
         Is SET_SRC something we want to gcse?   
         An expression is not available if its operands are
         subsequently modified, including this insn.   
     Only record sets of pseudo-regs in the hash table.   
         Is SET_DEST something we want to gcse?   
         Check if the memory expression is killed after insn.   

References FOR_BB_INSNS, GET_CODE, hash_scan_set(), modifies_mem::insn, INSN_P, PATTERN, record_opr_changes(), reset_opr_set_tables(), and SET.

static void insert_expr_in_table ( rtx  ,
rtx   
)
static
static void insert_expr_in_table ( )
static

Insert expression X in INSN in the hash TABLE. If it is already present, record it as the last occurrence in INSN's basic block.

 Do not insert expression in the table if it contains volatile operands,
 or if hash_expr determines the expression is something we don't want
 to or can't handle.   
 We anticipate that redundant expressions are rare, so for convenience
 allocate a new hash table element here already and set its fields.
 If we don't do this, we need a hack with a static struct expr.  Anyway,
 obstack_free is really fast and one more obstack_alloc doesn't hurt if
 we're going to see more expressions later on.   
   The expression isn't found, so insert it.   
     The expression is already in the table, so roll back the
     obstack and use the existing table entry.   
 Search for another occurrence in the same basic block.   
     If an occurrence isn't found, save a pointer to the end of
     the list.   
   Found another instance of the expression in the same basic block.
   Prefer this occurrence to the currently recorded one.  We want
   the last one in the block and the block is scanned from start
   to end.   
     First occurrence of this expression in this basic block.   
     First occurrence of this expression in any block?   
static int load_killed_in_block_p ( int  ,
rtx  ,
bool   
)
static
static int load_killed_in_block_p ( )
static

Return nonzero if the expression in X (a memory reference) is killed in the current basic block before (if AFTER_INSN is false) or after (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.

This function assumes that the modifies_mem table is flushed when the hash table construction or redundancy elimination phases start processing a new basic block.

     Ignore entries in the list that do not apply.   
     If SETTER is a call everything is clobbered.  Note that calls
     to pure functions are never put on the list, so we need not
     worry about them.   
     SETTER must be an insn of some kind that sets memory.  Call
     note_stores to examine each hunk of memory that is modified.
     It will set mems_conflict_p to nonzero if there may be a
     conflict between X and SETTER.   

References INSN_CUID.

static struct expr* lookup_expr_in_table ( rtx  )
staticread
static struct expr* lookup_expr_in_table ( )
staticread

Lookup pattern PAT in the expression hash table. The result is a pointer to the table entry, or NULL if not found.

rtl_opt_pass* make_pass_gcse2 ( )
static bool oprs_unchanged_p ( rtx  ,
rtx  ,
bool   
)
static

Support for hash table construction and transformations.

static bool oprs_unchanged_p ( )
static

Return nonzero if the operands of expression X are unchanged 1) from the start of INSN's basic block up to but not including INSN if AFTER_INSN is false, or 2) from INSN to the end of INSN's basic block if AFTER_INSN is true.

We are called after register allocation.

References oprs_unchanged_p(), XEXP, XVECEXP, and XVECLEN.

static void record_last_mem_set_info ( rtx  )
static
static void record_last_mem_set_info ( )
static

Record memory modification information for INSN. We do not actually care about the memory location(s) that are set, or even how they are set (consider a CALL_INSN). We merely need to record which insns modify memory.

static void record_last_reg_set_info ( rtx  ,
rtx   
)
static
static void record_last_reg_set_info ( )
inlinestatic

Record register first/last/block set information for REGNO in INSN.

static void record_last_reg_set_info_regno ( rtx  ,
int   
)
static
static void record_last_reg_set_info_regno ( )
inlinestatic
static void record_last_set_info ( rtx  ,
const_rtx  ,
void *   
)
static
static void record_last_set_info ( )
static

Called from compute_hash_table via note_stores to handle one SET or CLOBBER in an insn. DATA is really the instruction in which the SET is taking place.

Ignore pushes, they don't clobber memory. They may still clobber the stack pointer though. Some targets do argument pushes without adding REG_INC notes. See e.g. PR25196, where a pushsi2 on i386 doesn't have REG_INC notes. Note such changes here too.

References gcc_assert, HARD_REGISTER_P, record_last_reg_set_info(), REG_P, and XEXP.

static void record_opr_changes ( rtx  )
static

Referenced by hash_scan_set().

static void record_opr_changes ( )
static

Record things set by INSN. This data is used by oprs_unchanged_p.

 Find all stores and record them.   
 Also record autoincremented REGs for this insn as changed.   
 Finally, if this is a call, record all call clobbers.   

References insert_expr_in_table().

static bool reg_changed_after_insn_p ( )
static

Return true if register X is recorded as being set by an instruction whose CUID is greater than the one given.

static bool reg_killed_on_edge ( rtx  ,
edge   
)
static

Helpers for eliminate_partially_redundant_load.

static bool reg_killed_on_edge ( )
static

Check if register REG is killed in any insn waiting to be inserted on edge E. This function is required to check that our data flow analysis is still valid prior to commit_edge_insertions.

static bool reg_used_on_edge ( rtx  ,
edge   
)
static
static bool reg_used_on_edge ( )
static

Similar to above - check if register REG is used in any insn waiting to be inserted on edge E. Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p.

References NULL, and NULL_RTX.

static void reset_opr_set_tables ( void  )
static

Reset tables used to keep track of what's still available since the start of the block.

References can_copy_p(), general_operand(), and GET_MODE.

Referenced by hash_scan_set().

static unsigned int rest_of_handle_gcse2 ( )
static

Variable Documentation

int copies_inserted
struct obstack expr_obstack
static
hash_table<expr_hasher> expr_table
static

The table itself.

int insns_deleted
int mems_conflict_p
static

Used for communication between find_mem_conflicts and load_killed_in_block_p. Nonzero if find_mem_conflicts finds a conflict between two memory references. This is a bit of a hack to work around the limitations of note_stores.

struct modifies_mem* modifies_mem_list
static
struct obstack modifies_mem_obstack
static

The modifies_mem structs also go on an obstack, only this obstack is freed each time after completing the analysis or transformations on a basic block. So we allocate a dummy modifies_mem_obstack_bottom object on the obstack to keep track of the bottom of the obstack.

struct modifies_mem* modifies_mem_obstack_bottom
static
int moves_inserted
struct obstack occr_obstack
static
int* reg_avail_info
static

Array where each element is the CUID if the insn that last set the hard register with the number of the element, since the start of the current basic block.

This array is used during the building of the hash table (step 1) to determine if a reg is killed before the end of a basic block.

It is also used when eliminating partial redundancies (step 2) to see if a reg was modified since the start of a basic block.

struct { ... } stats

Post reload partially redundant load elimination Copyright (C) 2004-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/. The following code implements gcse after reload, the purpose of this pass is to cleanup redundant loads generated by reload and other optimizations that come after gcse. It searches for simple inter-block redundancies and tries to eliminate them by adding moves and loads in cold places.

Perform partially redundant load elimination, try to eliminate redundant loads created by the reload pass. We try to look for full or partial redundant loads fed by one or more loads/stores in predecessor BBs, and try adding loads to make them fully redundant. We also check if it's worth adding loads to be able to delete the redundant load.

Algorithm:

  1. Build available expressions hash table: For each load/store instruction, if the loaded/stored memory didn't change until the end of the basic block add this memory expression to the hash table.
  2. Perform Redundancy elimination: For each load instruction do the following: perform partial redundancy elimination, check if it's worth adding loads to make the load fully redundant. If so add loads and register copies and delete the load.
  3. Delete instructions made redundant in step 2.

Future enhancement: If the loaded register is used/defined between load and some store, look for some other free register between load and all its stores, and replace the load with a copy from this register to the loaded register. Keep statistics of this pass.

Referenced by ggc_splay_alloc().

int* uid_cuid
static

Mapping of insn UIDs to CUIDs. CUIDs are like UIDs except they increase monotonically in each basic block, have no gaps, and only apply to real insns.

struct obstack unoccr_obstack
static