diff gcc/postreload-gcse.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
line wrap: on
line diff
--- a/gcc/postreload-gcse.c	Sun Aug 21 07:07:55 2011 +0900
+++ b/gcc/postreload-gcse.c	Fri Oct 27 22:46:09 2017 +0900
@@ -1,6 +1,5 @@
 /* Post reload partially redundant load elimination
-   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010
-   Free Software Foundation, Inc.
+   Copyright (C) 2004-2017 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -21,30 +20,25 @@
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
-#include "diagnostic-core.h"
-
+#include "backend.h"
+#include "target.h"
 #include "rtl.h"
 #include "tree.h"
+#include "predict.h"
+#include "df.h"
+#include "memmodel.h"
 #include "tm_p.h"
-#include "regs.h"
-#include "hard-reg-set.h"
-#include "flags.h"
 #include "insn-config.h"
+#include "emit-rtl.h"
 #include "recog.h"
-#include "basic-block.h"
-#include "output.h"
-#include "function.h"
+
+#include "cfgrtl.h"
+#include "profile.h"
 #include "expr.h"
-#include "except.h"
-#include "intl.h"
-#include "obstack.h"
-#include "hashtab.h"
 #include "params.h"
-#include "target.h"
-#include "timevar.h"
 #include "tree-pass.h"
 #include "dbgcnt.h"
+#include "gcse-common.h"
 
 /* The following code implements gcse after reload, the purpose of this
    pass is to cleanup redundant loads generated by reload and other
@@ -90,9 +84,6 @@
    type 'struct expr', and for each expression there is a single linked
    list of occurrences.  */
 
-/* The table itself.  */
-static htab_t expr_table;
-
 /* Expression elements in the hash table.  */
 struct expr
 {
@@ -102,10 +93,61 @@
   /* The same hash for this entry.  */
   hashval_t hash;
 
+  /* Index in the transparent bitmaps.  */
+  unsigned int bitmap_index;
+
   /* List of available occurrence in basic blocks in the function.  */
   struct occr *avail_occr;
 };
 
+/* Hashtable helpers.  */
+
+struct expr_hasher : nofree_ptr_hash <expr>
+{
+  static inline hashval_t hash (const expr *);
+  static inline bool equal (const expr *, const expr *);
+};
+
+
+/* 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 x, int *do_not_record_p)
+{
+  *do_not_record_p = 0;
+  return hash_rtx (x, GET_MODE (x), do_not_record_p,
+		   NULL,  /*have_reg_qty=*/false);
+}
+
+/* Callback for hashtab.
+   Return the hash value for expression EXP.  We don't actually hash
+   here, we just return the cached hash value.  */
+
+inline hashval_t
+expr_hasher::hash (const expr *exp)
+{
+  return exp->hash;
+}
+
+/* Callback for hashtab.
+   Return nonzero if exp1 is equivalent to exp2.  */
+
+inline bool
+expr_hasher::equal (const expr *exp1, const expr *exp2)
+{
+  int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
+
+  gcc_assert (!equiv_p || exp1->hash == exp2->hash);
+  return equiv_p;
+}
+
+/* The table itself.  */
+static hash_table<expr_hasher> *expr_table;
+
+
 static struct obstack expr_obstack;
 
 /* Occurrence of an expression.
@@ -117,7 +159,7 @@
   /* Next occurrence of this expression.  */
   struct occr *next;
   /* The insn that computes the expression.  */
-  rtx insn;
+  rtx_insn *insn;
   /* Nonzero if this [anticipatable] occurrence has been deleted.  */
   char deleted_p;
 };
@@ -130,7 +172,7 @@
 {
   struct unoccr *next;
   edge pred;
-  rtx insn;
+  rtx_insn *insn;
 };
 
 static struct obstack unoccr_obstack;
@@ -149,7 +191,7 @@
 /* A list of insns that may modify memory within the current basic block.  */
 struct modifies_mem
 {
-  rtx insn;
+  rtx_insn *insn;
   struct modifies_mem *next;
 };
 static struct modifies_mem *modifies_mem_list;
@@ -166,6 +208,24 @@
    block, have no gaps, and only apply to real insns.  */
 static int *uid_cuid;
 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
+
+/* Bitmap of blocks which have memory stores.  */
+static bitmap modify_mem_list_set;
+
+/* Bitmap of blocks which have calls.  */
+static bitmap blocks_with_calls;
+
+/* Vector indexed by block # with a list of all the insns that
+   modify memory within the block.  */
+static vec<rtx_insn *> *modify_mem_list;
+
+/* Vector indexed by block # with a canonicalized list of insns
+   that modify memory in the block.  */
+static vec<modify_pair> *canon_modify_mem_list;
+
+/* Vector of simple bitmaps indexed by block number.  Each component sbitmap
+   indicates which expressions are transparent through the block.  */
+static sbitmap *transp;
 
 
 /* Helpers for memory allocation/freeing.  */
@@ -173,12 +233,12 @@
 static void free_mem (void);
 
 /* Support for hash table construction and transformations.  */
-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 bool oprs_unchanged_p (rtx, rtx_insn *, bool);
+static void record_last_reg_set_info (rtx_insn *, rtx);
+static void record_last_reg_set_info_regno (rtx_insn *, int);
+static void record_last_mem_set_info (rtx_insn *);
 static void record_last_set_info (rtx, const_rtx, void *);
-static void record_opr_changes (rtx);
+static void record_opr_changes (rtx_insn *);
 
 static void find_mem_conflicts (rtx, const_rtx, void *);
 static int load_killed_in_block_p (int, rtx, bool);
@@ -186,27 +246,24 @@
 
 /* Hash table support.  */
 static hashval_t hash_expr (rtx, int *);
-static hashval_t hash_expr_for_htab (const void *);
-static int expr_equiv_p (const void *, const void *);
-static void insert_expr_in_table (rtx, rtx);
+static void insert_expr_in_table (rtx, rtx_insn *);
 static struct expr *lookup_expr_in_table (rtx);
-static int dump_hash_table_entry (void **, void *);
 static void dump_hash_table (FILE *);
 
 /* Helpers for eliminate_partially_redundant_load.  */
 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 rtx get_avail_load_store_reg (rtx_insn *);
 
 static bool bb_has_well_behaved_predecessors (basic_block);
-static struct occr* get_bb_avail_insn (basic_block, struct occr *);
-static void hash_scan_set (rtx);
+static struct occr* get_bb_avail_insn (basic_block, struct occr *, int);
+static void hash_scan_set (rtx_insn *);
 static void compute_hash_table (void);
 
 /* The work horses of this pass.  */
 static void eliminate_partially_redundant_load (basic_block,
-						rtx,
+						rtx_insn *,
 						struct expr *);
 static void eliminate_partially_redundant_loads (void);
 
@@ -219,12 +276,12 @@
 {
   int i;
   basic_block bb;
-  rtx insn;
+  rtx_insn *insn;
 
   /* Find the largest UID and create a mapping from UIDs to CUIDs.  */
   uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
   i = 1;
-  FOR_EACH_BB (bb)
+  FOR_EACH_BB_FN (bb, cfun)
     FOR_BB_INSNS (bb, insn)
       {
         if (INSN_P (insn))
@@ -237,8 +294,7 @@
      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.  */
-  expr_table = htab_create (MAX (i / 4, 13),
-			    hash_expr_for_htab, expr_equiv_p, NULL);
+  expr_table = new hash_table<expr_hasher> (MAX (i / 4, 13));
 
   /* We allocate everything on obstacks because we often can roll back
      the whole obstack to some point.  Freeing obstacks is very fast.  */
@@ -256,6 +312,15 @@
   modifies_mem_obstack_bottom =
     (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
 					   sizeof (struct modifies_mem));
+
+  blocks_with_calls = BITMAP_ALLOC (NULL);
+  modify_mem_list_set = BITMAP_ALLOC (NULL);
+
+  modify_mem_list = (vec_rtx_heap *) xcalloc (last_basic_block_for_fn (cfun),
+					      sizeof (vec_rtx_heap));
+  canon_modify_mem_list
+    = (vec_modify_pair_heap *) xcalloc (last_basic_block_for_fn (cfun),
+					sizeof (vec_modify_pair_heap));
 }
 
 /* Free memory allocated by alloc_mem.  */
@@ -265,53 +330,27 @@
 {
   free (uid_cuid);
 
-  htab_delete (expr_table);
+  delete expr_table;
+  expr_table = NULL;
 
   obstack_free (&expr_obstack, NULL);
   obstack_free (&occr_obstack, NULL);
   obstack_free (&unoccr_obstack, NULL);
   obstack_free (&modifies_mem_obstack, NULL);
 
-  free (reg_avail_info);
-}
-
-
-/* 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 x, int *do_not_record_p)
-{
-  *do_not_record_p = 0;
-  return hash_rtx (x, GET_MODE (x), do_not_record_p,
-		   NULL,  /*have_reg_qty=*/false);
-}
+  unsigned i;
+  bitmap_iterator bi;
+  EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
+    {
+      modify_mem_list[i].release ();
+      canon_modify_mem_list[i].release ();
+    }
 
-/* Callback for hashtab.
-   Return the hash value for expression EXP.  We don't actually hash
-   here, we just return the cached hash value.  */
-
-static hashval_t
-hash_expr_for_htab (const void *expp)
-{
-  const struct expr *const exp = (const struct expr *) expp;
-  return exp->hash;
-}
-
-/* Callback for hashtab.
-   Return nonzero if exp1 is equivalent to exp2.  */
-
-static int
-expr_equiv_p (const void *exp1p, const void *exp2p)
-{
-  const struct expr *const exp1 = (const struct expr *) exp1p;
-  const struct expr *const exp2 = (const struct expr *) exp2p;
-  int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
-
-  gcc_assert (!equiv_p || exp1->hash == exp2->hash);
-  return equiv_p;
+  BITMAP_FREE (blocks_with_calls);
+  BITMAP_FREE (modify_mem_list_set);
+  free (reg_avail_info);
+  free (modify_mem_list);
+  free (canon_modify_mem_list);
 }
 
 
@@ -320,7 +359,7 @@
    basic block.  */
 
 static void
-insert_expr_in_table (rtx x, rtx insn)
+insert_expr_in_table (rtx x, rtx_insn *insn)
 {
   int do_not_record_p;
   hashval_t hash;
@@ -346,12 +385,18 @@
   cur_expr->hash = hash;
   cur_expr->avail_occr = NULL;
 
-  slot = (struct expr **) htab_find_slot_with_hash (expr_table, cur_expr,
-						    hash, INSERT);
+  slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
 
   if (! (*slot))
-    /* The expression isn't found, so insert it.  */
-    *slot = cur_expr;
+    {
+      /* The expression isn't found, so insert it.  */
+      *slot = cur_expr;
+
+      /* Anytime we add an entry to the table, record the index
+	 of the new entry.  The bitmap index starts counting
+	 at zero.  */
+      cur_expr->bitmap_index = expr_table->elements () - 1;
+    }
   else
     {
       /* The expression is already in the table, so roll back the
@@ -415,8 +460,7 @@
   tmp_expr->hash = hash;
   tmp_expr->avail_occr = NULL;
 
-  slot = (struct expr **) htab_find_slot_with_hash (expr_table, tmp_expr,
-                                                    hash, INSERT);
+  slot = expr_table->find_slot_with_hash (tmp_expr, hash, INSERT);
   obstack_free (&expr_obstack, tmp_expr);
 
   if (!slot)
@@ -430,21 +474,20 @@
    expression hash table to FILE.  */
 
 /* This helper is called via htab_traverse.  */
-static int
-dump_hash_table_entry (void **slot, void *filep)
+int
+dump_expr_hash_table_entry (expr **slot, FILE *file)
 {
-  struct expr *expr = (struct expr *) *slot;
-  FILE *file = (FILE *) filep;
+  struct expr *exprs = *slot;
   struct occr *occr;
 
   fprintf (file, "expr: ");
-  print_rtl (file, expr->expr);
-  fprintf (file,"\nhashcode: %u\n", expr->hash);
+  print_rtl (file, exprs->expr);
+  fprintf (file,"\nhashcode: %u\n", exprs->hash);
   fprintf (file,"list of occurrences:\n");
-  occr = expr->avail_occr;
+  occr = exprs->avail_occr;
   while (occr)
     {
-      rtx insn = occr->insn;
+      rtx_insn *insn = occr->insn;
       print_rtl_single (file, insn);
       fprintf (file, "\n");
       occr = occr->next;
@@ -458,13 +501,13 @@
 {
   fprintf (file, "\n\nexpression hash table\n");
   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
-           (long) htab_size (expr_table),
-           (long) htab_elements (expr_table),
-           htab_collisions (expr_table));
-  if (htab_elements (expr_table) > 0)
+           (long) expr_table->size (),
+           (long) expr_table->elements (),
+           expr_table->collisions ());
+  if (expr_table->elements () > 0)
     {
       fprintf (file, "\n\ntable entries:\n");
-      htab_traverse (expr_table, dump_hash_table_entry, file);
+      expr_table->traverse <FILE *, dump_expr_hash_table_entry> (file);
     }
   fprintf (file, "\n");
 }
@@ -478,7 +521,7 @@
   unsigned int regno, end_regno;
 
   regno = REGNO (x);
-  end_regno = END_HARD_REGNO (x);
+  end_regno = END_REGNO (x);
   do
     if (reg_avail_info[regno] > cuid)
       return true;
@@ -492,7 +535,7 @@
    2) from INSN to the end of INSN's basic block if AFTER_INSN is true.  */
 
 static bool
-oprs_unchanged_p (rtx x, rtx insn, bool after_insn)
+oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
 {
   int i, j;
   enum rtx_code code;
@@ -521,10 +564,7 @@
     case PC:
     case CC0: /*FIXME*/
     case CONST:
-    case CONST_INT:
-    case CONST_DOUBLE:
-    case CONST_FIXED:
-    case CONST_VECTOR:
+    CASE_CONST_ANY:
     case SYMBOL_REF:
     case LABEL_REF:
     case ADDR_VEC:
@@ -589,8 +629,7 @@
   if (! MEM_P (dest))
     return;
 
-  if (true_dependence (dest, GET_MODE (dest), mem_op,
-		       rtx_addr_varies_p))
+  if (true_dependence (dest, GET_MODE (dest), mem_op))
     mems_conflict_p = 1;
 }
 
@@ -610,7 +649,7 @@
 
   while (list_entry)
     {
-      rtx setter = list_entry->insn;
+      rtx_insn *setter = list_entry->insn;
 
       /* Ignore entries in the list that do not apply.  */
       if ((after_insn
@@ -646,19 +685,19 @@
 /* Record register first/last/block set information for REGNO in INSN.  */
 
 static inline void
-record_last_reg_set_info (rtx insn, rtx reg)
+record_last_reg_set_info (rtx_insn *insn, rtx reg)
 {
   unsigned int regno, end_regno;
 
   regno = REGNO (reg);
-  end_regno = END_HARD_REGNO (reg);
+  end_regno = END_REGNO (reg);
   do
     reg_avail_info[regno] = INSN_CUID (insn);
   while (++regno < end_regno);
 }
 
 static inline void
-record_last_reg_set_info_regno (rtx insn, int regno)
+record_last_reg_set_info_regno (rtx_insn *insn, int regno)
 {
   reg_avail_info[regno] = INSN_CUID (insn);
 }
@@ -669,7 +708,7 @@
    a CALL_INSN).  We merely need to record which insns modify memory.  */
 
 static void
-record_last_mem_set_info (rtx insn)
+record_last_mem_set_info (rtx_insn *insn)
 {
   struct modifies_mem *list_entry;
 
@@ -678,6 +717,11 @@
   list_entry->insn = insn;
   list_entry->next = modifies_mem_list;
   modifies_mem_list = list_entry;
+
+  record_last_mem_set_info_common (insn, modify_mem_list,
+				   canon_modify_mem_list,
+				   modify_mem_list_set,
+				   blocks_with_calls);
 }
 
 /* Called from compute_hash_table via note_stores to handle one
@@ -687,7 +731,7 @@
 static void
 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
 {
-  rtx last_set_insn = (rtx) data;
+  rtx_insn *last_set_insn = (rtx_insn *) data;
 
   if (GET_CODE (dest) == SUBREG)
     dest = SUBREG_REG (dest);
@@ -725,7 +769,7 @@
    This data is used by oprs_unchanged_p.  */
 
 static void
-record_opr_changes (rtx insn)
+record_opr_changes (rtx_insn *insn)
 {
   rtx note;
 
@@ -742,10 +786,9 @@
     {
       unsigned int regno;
       rtx link, x;
-
-      for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
-	if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
-	  record_last_reg_set_info_regno (insn, regno);
+      hard_reg_set_iterator hrsi;
+      EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, regno, hrsi)
+	record_last_reg_set_info_regno (insn, regno);
 
       for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
 	if (GET_CODE (XEXP (link, 0)) == CLOBBER)
@@ -768,7 +811,7 @@
    After reload we are interested in loads/stores only.  */
 
 static void
-hash_scan_set (rtx insn)
+hash_scan_set (rtx_insn *insn)
 {
   rtx pat = PATTERN (insn);
   rtx src = SET_SRC (pat);
@@ -834,9 +877,9 @@
 {
   basic_block bb;
 
-  FOR_EACH_BB (bb)
+  FOR_EACH_BB_FN (bb, cfun)
     {
-      rtx insn;
+      rtx_insn *insn;
 
       /* First pass over the instructions records information used to
 	 determine when registers and memory are last set.
@@ -865,7 +908,7 @@
 static bool
 reg_killed_on_edge (rtx reg, edge e)
 {
-  rtx insn;
+  rtx_insn *insn;
 
   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
     if (INSN_P (insn) && reg_set_p (reg, insn))
@@ -882,7 +925,7 @@
 static bool
 reg_used_on_edge (rtx reg, edge e)
 {
-  rtx insn;
+  rtx_insn *insn;
 
   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
     if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
@@ -894,11 +937,11 @@
 /* Return the loaded/stored register of a load/store instruction.  */
 
 static rtx
-get_avail_load_store_reg (rtx insn)
+get_avail_load_store_reg (rtx_insn *insn)
 {
   if (REG_P (SET_DEST (PATTERN (insn))))
     /* A load.  */
-    return SET_DEST(PATTERN(insn));
+    return SET_DEST (PATTERN (insn));
   else
     {
       /* A store.  */
@@ -920,10 +963,15 @@
 
   FOR_EACH_EDGE (pred, ei, bb->preds)
     {
-      if ((pred->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (pred))
+      /* commit_one_edge_insertion refuses to insert on abnormal edges even if
+	 the source has only one successor so EDGE_CRITICAL_P is too weak.  */
+      if ((pred->flags & EDGE_ABNORMAL) && !single_pred_p (pred->dest))
 	return false;
 
-      if (JUMP_TABLE_DATA_P (BB_END (pred->src)))
+      if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
+	return false;
+
+      if (tablejump_p (BB_END (pred->src), NULL, NULL))
 	return false;
     }
   return true;
@@ -933,15 +981,45 @@
 /* Search for the occurrences of expression in BB.  */
 
 static struct occr*
-get_bb_avail_insn (basic_block bb, struct occr *occr)
+get_bb_avail_insn (basic_block bb, struct occr *orig_occr, int bitmap_index)
 {
+  struct occr *occr = orig_occr;
+
   for (; occr != NULL; occr = occr->next)
     if (BLOCK_FOR_INSN (occr->insn) == bb)
       return occr;
+
+  /* If we could not find an occurrence in BB, see if BB
+     has a single predecessor with an occurrence that is
+     transparent through BB.  */
+  if (single_pred_p (bb)
+      && bitmap_bit_p (transp[bb->index], bitmap_index)
+      && (occr = get_bb_avail_insn (single_pred (bb), orig_occr, bitmap_index)))
+    {
+      rtx avail_reg = get_avail_load_store_reg (occr->insn);
+      if (!reg_set_between_p (avail_reg,
+			      PREV_INSN (BB_HEAD (bb)),
+			      NEXT_INSN (BB_END (bb)))
+	  && !reg_killed_on_edge (avail_reg, single_pred_edge (bb)))
+	return occr;
+    }
+
   return NULL;
 }
 
 
+/* This helper is called via htab_traverse.  */
+int
+compute_expr_transp (expr **slot, FILE *dump_file ATTRIBUTE_UNUSED)
+{
+  struct expr *expr = *slot;
+
+  compute_transp (expr->expr, expr->bitmap_index, transp,
+		  blocks_with_calls, modify_mem_list_set,
+		  canon_modify_mem_list);
+  return 1;
+}
+
 /* 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.
@@ -956,25 +1034,27 @@
    a redundancy is also worth doing, assuming it is possible.  */
 
 static void
-eliminate_partially_redundant_load (basic_block bb, rtx insn,
+eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
 				    struct expr *expr)
 {
   edge pred;
-  rtx avail_insn = NULL_RTX;
+  rtx_insn *avail_insn = NULL;
   rtx avail_reg;
   rtx dest, pat;
   struct occr *a_occr;
   struct unoccr *occr, *avail_occrs = NULL;
   struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
   int npred_ok = 0;
-  gcov_type ok_count = 0; /* Redundant load execution count.  */
-  gcov_type critical_count = 0; /* Execution count of critical edges.  */
+  profile_count ok_count = profile_count::zero ();
+		 /* Redundant load execution count.  */
+  profile_count critical_count = profile_count::zero ();
+		 /* Execution count of critical edges.  */
   edge_iterator ei;
   bool critical_edge_split = false;
 
   /* The execution count of the loads to be added to make the
      load fully redundant.  */
-  gcov_type not_ok_count = 0;
+  profile_count not_ok_count = profile_count::zero ();
   basic_block pred_bb;
 
   pat = PATTERN (insn);
@@ -989,14 +1069,18 @@
   /* Check potential for replacing load with copy for predecessors.  */
   FOR_EACH_EDGE (pred, ei, bb->preds)
     {
-      rtx next_pred_bb_end;
+      rtx_insn *next_pred_bb_end;
 
-      avail_insn = NULL_RTX;
+      avail_insn = NULL;
       avail_reg = NULL_RTX;
       pred_bb = pred->src;
-      next_pred_bb_end = NEXT_INSN (BB_END (pred_bb));
-      for (a_occr = get_bb_avail_insn (pred_bb, expr->avail_occr); a_occr;
-	   a_occr = get_bb_avail_insn (pred_bb, a_occr->next))
+      for (a_occr = get_bb_avail_insn (pred_bb,
+				       expr->avail_occr,
+				       expr->bitmap_index);
+	   a_occr;
+	   a_occr = get_bb_avail_insn (pred_bb,
+				       a_occr->next,
+				       expr->bitmap_index))
 	{
 	  /* Check if the loaded register is not used.  */
 	  avail_insn = a_occr->insn;
@@ -1005,15 +1089,18 @@
 
 	  /* Make sure we can generate a move from register avail_reg to
 	     dest.  */
-	  extract_insn (gen_move_insn (copy_rtx (dest),
-				       copy_rtx (avail_reg)));
-	  if (! constrain_operands (1)
+	  rtx_insn *move = gen_move_insn (copy_rtx (dest),
+					  copy_rtx (avail_reg));
+	  extract_insn (move);
+	  if (! constrain_operands (1, get_preferred_alternatives (insn,
+								   pred_bb))
 	      || reg_killed_on_edge (avail_reg, pred)
 	      || reg_used_on_edge (dest, pred))
 	    {
 	      avail_insn = NULL;
 	      continue;
 	    }
+	  next_pred_bb_end = NEXT_INSN (BB_END (BLOCK_FOR_INSN (avail_insn)));
 	  if (!reg_set_between_p (avail_reg, avail_insn, next_pred_bb_end))
 	    /* AVAIL_INSN remains non-null.  */
 	    break;
@@ -1021,13 +1108,14 @@
 	    avail_insn = NULL;
 	}
 
-      if (EDGE_CRITICAL_P (pred))
-	critical_count += pred->count;
+      if (EDGE_CRITICAL_P (pred) && pred->count ().initialized_p ())
+	critical_count += pred->count ();
 
       if (avail_insn != NULL_RTX)
 	{
 	  npred_ok++;
-	  ok_count += pred->count;
+	  if (pred->count ().initialized_p ())
+	    ok_count = ok_count + pred->count ();
 	  if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest),
 						    copy_rtx (avail_reg)))))
 	    {
@@ -1051,10 +1139,11 @@
 	  /* Adding a load on a critical edge will cause a split.  */
 	  if (EDGE_CRITICAL_P (pred))
 	    critical_edge_split = true;
-	  not_ok_count += pred->count;
+	  if (pred->count ().initialized_p ())
+	    not_ok_count = not_ok_count + pred->count ();
 	  unoccr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
 						    sizeof (struct unoccr));
-	  unoccr->insn = NULL_RTX;
+	  unoccr->insn = NULL;
 	  unoccr->pred = pred;
 	  unoccr->next = unavail_occrs;
 	  unavail_occrs = unoccr;
@@ -1069,15 +1158,17 @@
       || (optimize_bb_for_size_p (bb) && npred_ok > 1)
       /* If we don't have profile information we cannot tell if splitting
          a critical edge is profitable or not so don't do it.  */
-      || ((! profile_info || ! flag_branch_probabilities
+      || ((! profile_info || profile_status_for_fn (cfun) != PROFILE_READ
 	   || targetm.cannot_modify_jumps_p ())
 	  && critical_edge_split))
     goto cleanup;
 
   /* Check if it's worth applying the partial redundancy elimination.  */
-  if (ok_count < GCSE_AFTER_RELOAD_PARTIAL_FRACTION * not_ok_count)
+  if (ok_count.to_gcov_type ()
+      < GCSE_AFTER_RELOAD_PARTIAL_FRACTION * not_ok_count.to_gcov_type ())
     goto cleanup;
-  if (ok_count < GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count)
+  if (ok_count.to_gcov_type ()
+      < GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count.to_gcov_type ())
     goto cleanup;
 
   /* Generate moves to the loaded register from where
@@ -1126,9 +1217,10 @@
   /* 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.  */
-  for (a_occr = get_bb_avail_insn (bb, expr->avail_occr);
+  for (a_occr = get_bb_avail_insn (bb, expr->avail_occr, expr->bitmap_index);
        a_occr && (a_occr->insn != insn);
-       a_occr = get_bb_avail_insn (bb, a_occr->next));
+       a_occr = get_bb_avail_insn (bb, a_occr->next, expr->bitmap_index))
+    ;
 
   if (!a_occr)
     {
@@ -1155,17 +1247,17 @@
 static void
 eliminate_partially_redundant_loads (void)
 {
-  rtx insn;
+  rtx_insn *insn;
   basic_block bb;
 
   /* Note we start at block 1.  */
 
-  if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
+  if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
     return;
 
   FOR_BB_BETWEEN (bb,
-		  ENTRY_BLOCK_PTR->next_bb->next_bb,
-		  EXIT_BLOCK_PTR,
+		  ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
+		  EXIT_BLOCK_PTR_FOR_FN (cfun),
 		  next_bb)
     {
       /* Don't try anything on basic blocks with strange predecessors.  */
@@ -1227,13 +1319,13 @@
    marked for later deletion.  */
 
 /* This helper is called via htab_traverse.  */
-static int
-delete_redundant_insns_1 (void **slot, void *data ATTRIBUTE_UNUSED)
+int
+delete_redundant_insns_1 (expr **slot, void *data ATTRIBUTE_UNUSED)
 {
-  struct expr *expr = (struct expr *) *slot;
+  struct expr *exprs = *slot;
   struct occr *occr;
 
-  for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
+  for (occr = exprs->avail_occr; occr != NULL; occr = occr->next)
     {
       if (occr->deleted_p && dbg_cnt (gcse2_delete))
 	{
@@ -1255,7 +1347,7 @@
 static void
 delete_redundant_insns (void)
 {
-  htab_traverse (expr_table, delete_redundant_insns_1, NULL);
+  expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
   if (dump_file)
     fprintf (dump_file, "\n");
 }
@@ -1281,10 +1373,21 @@
   if (dump_file)
     dump_hash_table (dump_file);
 
-  if (htab_elements (expr_table) > 0)
+  if (expr_table->elements () > 0)
     {
+      /* Knowing which MEMs are transparent through a block can signifiantly
+	 increase the number of redundant loads found.  So compute transparency
+	 information for each memory expression in the hash table.  */
+      df_analyze ();
+      /* This can not be part of the normal allocation routine because
+	 we have to know the number of elements in the hash table.  */
+      transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
+				     expr_table->elements ());
+      bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
+      expr_table->traverse <FILE *, compute_expr_transp> (dump_file);
       eliminate_partially_redundant_loads ();
       delete_redundant_insns ();
+      sbitmap_vector_free (transp);
 
       if (dump_file)
 	{
@@ -1294,6 +1397,13 @@
 	  fprintf (dump_file, "insns deleted:   %d\n", stats.insns_deleted);
 	  fprintf (dump_file, "\n\n");
 	}
+
+      statistics_counter_event (cfun, "copies inserted",
+				stats.copies_inserted);
+      statistics_counter_event (cfun, "moves inserted",
+				stats.moves_inserted);
+      statistics_counter_event (cfun, "insns deleted",
+				stats.insns_deleted);
     }
 
   /* We are finished with alias.  */
@@ -1303,13 +1413,6 @@
 }
 
 
-static bool
-gate_handle_gcse2 (void)
-{
-  return (optimize > 0 && flag_gcse_after_reload
-	  && optimize_function_for_speed_p (cfun));
-}
-
 
 static unsigned int
 rest_of_handle_gcse2 (void)
@@ -1319,23 +1422,43 @@
   return 0;
 }
 
-struct rtl_opt_pass pass_gcse2 =
+namespace {
+
+const pass_data pass_data_gcse2 =
 {
- {
-  RTL_PASS,
-  "gcse2",                              /* name */
-  gate_handle_gcse2,                    /* gate */
-  rest_of_handle_gcse2,                 /* execute */
-  NULL,                                 /* sub */
-  NULL,                                 /* next */
-  0,                                    /* static_pass_number */
-  TV_GCSE_AFTER_RELOAD,                 /* tv_id */
-  0,                                    /* properties_required */
-  0,                                    /* properties_provided */
-  0,                                    /* properties_destroyed */
-  0,                                    /* todo_flags_start */
-  TODO_dump_func | TODO_verify_rtl_sharing
-  | TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
- }
+  RTL_PASS, /* type */
+  "gcse2", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_GCSE_AFTER_RELOAD, /* tv_id */
+  0, /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  0, /* todo_flags_finish */
 };
 
+class pass_gcse2 : public rtl_opt_pass
+{
+public:
+  pass_gcse2 (gcc::context *ctxt)
+    : rtl_opt_pass (pass_data_gcse2, ctxt)
+  {}
+
+  /* opt_pass methods: */
+  virtual bool gate (function *fun)
+    {
+      return (optimize > 0 && flag_gcse_after_reload
+	      && optimize_function_for_speed_p (fun));
+    }
+
+  virtual unsigned int execute (function *) { return rest_of_handle_gcse2 (); }
+
+}; // class pass_gcse2
+
+} // anon namespace
+
+rtl_opt_pass *
+make_pass_gcse2 (gcc::context *ctxt)
+{
+  return new pass_gcse2 (ctxt);
+}