OSDN Git Service

* config/rs6000/rs6000.md (strlensi): Emit barrier after
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-ivopts.c
index e5a8d85..9e31041 100644 (file)
@@ -121,17 +121,10 @@ struct version_info
   bool preserve_biv;   /* For the original biv, whether to preserve it.  */
 };
 
-/* Information attached to loop.  */
-struct loop_data
-{
-  unsigned regs_used;  /* Number of registers used.  */
-};
-
 /* Types of uses.  */
 enum use_type
 {
   USE_NONLINEAR_EXPR,  /* Use in a nonlinear expression.  */
-  USE_OUTER,           /* The induction variable is used outside the loop.  */
   USE_ADDRESS,         /* Use in an address.  */
   USE_COMPARE          /* Use is a compare.  */
 };
@@ -210,6 +203,9 @@ struct ivopts_data
   /* The currently optimized loop.  */
   struct loop *current_loop;
 
+  /* Number of registers used in it.  */
+  unsigned regs_used;
+
   /* Numbers of iterations for all exits of the current loop.  */
   htab_t niters;
 
@@ -348,14 +344,6 @@ iv_cand (struct ivopts_data *data, unsigned i)
   return VEC_index (iv_cand_p, data->iv_candidates, i);
 }
 
-/* The data for LOOP.  */
-
-static inline struct loop_data *
-loop_data (struct loop *loop)
-{
-  return loop->aux;
-}
-
 /* The single loop exit if it dominates the latch, NULL otherwise.  */
 
 edge
@@ -431,10 +419,6 @@ dump_use (FILE *file, struct iv_use *use)
       fprintf (file, "  generic\n");
       break;
 
-    case USE_OUTER:
-      fprintf (file, "  outside\n");
-      break;
-
     case USE_ADDRESS:
       fprintf (file, "  address\n");
       break;
@@ -741,25 +725,17 @@ niter_for_single_dom_exit (struct ivopts_data *data)
 }
 
 /* Initializes data structures used by the iv optimization pass, stored
-   in DATA.  LOOPS is the loop tree.  */
+   in DATA.  */
 
 static void
-tree_ssa_iv_optimize_init (struct loops *loops, struct ivopts_data *data)
+tree_ssa_iv_optimize_init (struct ivopts_data *data)
 {
-  unsigned i;
-
   data->version_info_size = 2 * num_ssa_names;
-  data->version_info = xcalloc (data->version_info_size,
-                               sizeof (struct version_info));
+  data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
   data->relevant = BITMAP_ALLOC (NULL);
   data->important_candidates = BITMAP_ALLOC (NULL);
   data->max_inv_id = 0;
   data->niters = htab_create (10, nfe_hash, nfe_eq, free);
-
-  for (i = 1; i < loops->num; i++)
-    if (loops->parray[i])
-      loops->parray[i]->aux = xcalloc (1, sizeof (struct loop_data));
-
   data->iv_uses = VEC_alloc (iv_use_p, heap, 20);
   data->iv_candidates = VEC_alloc (iv_cand_p, heap, 20);
   decl_rtl_to_reset = VEC_alloc (tree, heap, 20);
@@ -825,7 +801,7 @@ determine_base_object (tree expr)
 static struct iv *
 alloc_iv (tree base, tree step)
 {
-  struct iv *iv = xcalloc (1, sizeof (struct iv));
+  struct iv *iv = XCNEW (struct iv);
 
   if (step && integer_zerop (step))
     step = NULL_TREE;
@@ -881,18 +857,16 @@ static tree
 determine_biv_step (tree phi)
 {
   struct loop *loop = bb_for_stmt (phi)->loop_father;
-  tree name = PHI_RESULT (phi), base, step;
+  tree name = PHI_RESULT (phi);
+  affine_iv iv;
 
   if (!is_gimple_reg (name))
     return NULL_TREE;
 
-  if (!simple_iv (loop, phi, name, &base, &step, true))
-    return NULL_TREE;
-
-  if (zero_p (step))
+  if (!simple_iv (loop, phi, name, &iv, true))
     return NULL_TREE;
 
-  return step;
+  return (zero_p (iv.step) ? NULL_TREE : iv.step);
 }
 
 /* Returns true if EXP is a ssa name that occurs in an abnormal phi node.  */
@@ -1044,17 +1018,16 @@ mark_bivs (struct ivopts_data *data)
 }
 
 /* Checks whether STMT defines a linear induction variable and stores its
-   parameters to BASE and STEP.  */
+   parameters to IV.  */
 
 static bool
-find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt,
-                       tree *base, tree *step)
+find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt, affine_iv *iv)
 {
   tree lhs;
   struct loop *loop = data->current_loop;
 
-  *base = NULL_TREE;
-  *step = NULL_TREE;
+  iv->base = NULL_TREE;
+  iv->step = NULL_TREE;
 
   if (TREE_CODE (stmt) != MODIFY_EXPR)
     return false;
@@ -1063,12 +1036,12 @@ find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt,
   if (TREE_CODE (lhs) != SSA_NAME)
     return false;
 
-  if (!simple_iv (loop, stmt, TREE_OPERAND (stmt, 1), base, step, true))
+  if (!simple_iv (loop, stmt, TREE_OPERAND (stmt, 1), iv, true))
     return false;
-  *base = expand_simple_operations (*base);
+  iv->base = expand_simple_operations (iv->base);
 
-  if (contains_abnormal_ssa_name_p (*base)
-      || contains_abnormal_ssa_name_p (*step))
+  if (contains_abnormal_ssa_name_p (iv->base)
+      || contains_abnormal_ssa_name_p (iv->step))
     return false;
 
   return true;
@@ -1079,12 +1052,12 @@ find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt,
 static void
 find_givs_in_stmt (struct ivopts_data *data, tree stmt)
 {
-  tree base, step;
+  affine_iv iv;
 
-  if (!find_givs_in_stmt_scev (data, stmt, &base, &step))
+  if (!find_givs_in_stmt_scev (data, stmt, &iv))
     return;
 
-  set_iv (data, TREE_OPERAND (stmt, 0), base, step);
+  set_iv (data, TREE_OPERAND (stmt, 0), iv.base, iv.step);
 }
 
 /* Finds general ivs in basic block BB.  */
@@ -1163,7 +1136,7 @@ static struct iv_use *
 record_use (struct ivopts_data *data, tree *use_p, struct iv *iv,
            tree stmt, enum use_type use_type)
 {
-  struct iv_use *use = xcalloc (1, sizeof (struct iv_use));
+  struct iv_use *use = XCNEW (struct iv_use);
 
   use->id = n_iv_uses (data);
   use->type = use_type;
@@ -1211,12 +1184,10 @@ record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
 }
 
-/* Checks whether the use OP is interesting and if so, records it
-   as TYPE.  */
+/* Checks whether the use OP is interesting and if so, records it.  */
 
 static struct iv_use *
-find_interesting_uses_outer_or_nonlin (struct ivopts_data *data, tree op,
-                                      enum use_type type)
+find_interesting_uses_op (struct ivopts_data *data, tree op)
 {
   struct iv *iv;
   struct iv *civ;
@@ -1234,11 +1205,7 @@ find_interesting_uses_outer_or_nonlin (struct ivopts_data *data, tree op,
     {
       use = iv_use (data, iv->use_id);
 
-      gcc_assert (use->type == USE_NONLINEAR_EXPR
-                 || use->type == USE_OUTER);
-
-      if (type == USE_NONLINEAR_EXPR)
-       use->type = USE_NONLINEAR_EXPR;
+      gcc_assert (use->type == USE_NONLINEAR_EXPR);
       return use;
     }
 
@@ -1249,36 +1216,19 @@ find_interesting_uses_outer_or_nonlin (struct ivopts_data *data, tree op,
     }
   iv->have_use_for = true;
 
-  civ = xmalloc (sizeof (struct iv));
+  civ = XNEW (struct iv);
   *civ = *iv;
 
   stmt = SSA_NAME_DEF_STMT (op);
   gcc_assert (TREE_CODE (stmt) == PHI_NODE
              || TREE_CODE (stmt) == MODIFY_EXPR);
 
-  use = record_use (data, NULL, civ, stmt, type);
+  use = record_use (data, NULL, civ, stmt, USE_NONLINEAR_EXPR);
   iv->use_id = use->id;
 
   return use;
 }
 
-/* Checks whether the use OP is interesting and if so, records it.  */
-
-static struct iv_use *
-find_interesting_uses_op (struct ivopts_data *data, tree op)
-{
-  return find_interesting_uses_outer_or_nonlin (data, op, USE_NONLINEAR_EXPR);
-}
-
-/* Records a definition of induction variable OP that is used outside of the
-   loop.  */
-
-static struct iv_use *
-find_interesting_uses_outer (struct ivopts_data *data, tree op)
-{
-  return find_interesting_uses_outer_or_nonlin (data, op, USE_OUTER);
-}
-
 /* Checks whether the condition *COND_P in STMT is interesting
    and if so, records it.  */
 
@@ -1336,7 +1286,7 @@ find_interesting_uses_cond (struct ivopts_data *data, tree stmt, tree *cond_p)
       return;
     }
 
-  civ = xmalloc (sizeof (struct iv));
+  civ = XNEW (struct iv);
   *civ = zero_p (iv0->step) ? *iv1: *iv0;
   record_use (data, cond_p, civ, stmt, USE_COMPARE);
 }
@@ -1443,6 +1393,8 @@ idx_find_step (tree base, tree *idx, void *data)
     /* The step for pointer arithmetics already is 1 byte.  */
     step = build_int_cst (sizetype, 1);
 
+  /* FIXME: convert_step should not be used outside chrec_convert: fix
+     this by calling chrec_convert.  */
   iv_step = convert_step (dta->ivopts_data->current_loop,
                          sizetype, iv->base, iv->step, dta->stmt);
 
@@ -1722,7 +1674,7 @@ find_interesting_uses_outside (struct ivopts_data *data, edge exit)
   for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi))
     {
       def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
-      find_interesting_uses_outer (data, def);
+      find_interesting_uses_op (data, def);
     }
 }
 
@@ -2033,7 +1985,7 @@ add_candidate_1 (struct ivopts_data *data,
 
   if (i == n_iv_cands (data))
     {
-      cand = xcalloc (1, sizeof (struct iv_cand));
+      cand = XCNEW (struct iv_cand);
       cand->id = i;
 
       if (!base && !step)
@@ -2210,23 +2162,6 @@ add_iv_value_candidates (struct ivopts_data *data,
     add_candidate (data, base, iv->step, false, use);
 }
 
-/* Possibly adds pseudocandidate for replacing the final value of USE by
-   a direct computation.  */
-
-static void
-add_iv_outer_candidates (struct ivopts_data *data, struct iv_use *use)
-{
-  struct tree_niter_desc *niter;
-
-  /* We must know where we exit the loop and how many times does it roll.  */
-  niter = niter_for_single_dom_exit (data);
-  if (!niter
-      || !zero_p (niter->may_be_zero))
-    return;
-
-  add_candidate_1 (data, NULL, NULL, false, IP_NORMAL, use, NULL_TREE);
-}
-
 /* Adds candidates based on the uses.  */
 
 static void
@@ -2250,14 +2185,6 @@ add_derived_ivs_candidates (struct ivopts_data *data)
          add_iv_value_candidates (data, use->iv, use);
          break;
 
-       case USE_OUTER:
-         add_iv_value_candidates (data, use->iv, use);
-
-         /* Additionally, add the pseudocandidate for the possibility to
-            replace the final value by a direct computation.  */
-         add_iv_outer_candidates (data, use);
-         break;
-
        default:
          gcc_unreachable ();
        }
@@ -2351,7 +2278,7 @@ alloc_use_cost_map (struct ivopts_data *data)
        }
 
       use->n_map_members = size;
-      use->cost_map = xcalloc (size, sizeof (struct cost_pair));
+      use->cost_map = XCNEWVEC (struct cost_pair, size);
     }
 }
 
@@ -2489,7 +2416,7 @@ prepare_decl_rtl (tree *expr_p, int *ws, void *data)
           expr_p = &TREE_OPERAND (*expr_p, 0))
        continue;
       obj = *expr_p;
-      if (DECL_P (obj))
+      if (DECL_P (obj) && !DECL_RTL_SET_P (obj))
         x = produce_memory_decl_rtl (obj, regno);
       break;
 
@@ -2759,6 +2686,15 @@ aff_combination_add_elt (struct affine_tree_combination *comb, tree elt,
        comb->n--;
        comb->coefs[i] = comb->coefs[comb->n];
        comb->elts[i] = comb->elts[comb->n];
+
+       if (comb->rest)
+         {
+           gcc_assert (comb->n == MAX_AFF_ELTS - 1);
+           comb->coefs[comb->n] = 1;
+           comb->elts[comb->n] = comb->rest;
+           comb->rest = NULL_TREE;
+           comb->n++;
+         }
        return;
       }
   if (comb->n < MAX_AFF_ELTS)
@@ -2791,7 +2727,7 @@ aff_combination_add (struct affine_tree_combination *comb1,
   unsigned i;
 
   comb1->offset = (comb1->offset + comb2->offset) & comb1->mask;
-  for (i = 0; i < comb2-> n; i++)
+  for (i = 0; i < comb2->n; i++)
     aff_combination_add_elt (comb1, comb2->elts[i], comb2->coefs[i]);
   if (comb2->rest)
     aff_combination_add_elt (comb1, comb2->rest, 1);
@@ -3223,7 +3159,7 @@ multiply_by_cost (HOST_WIDE_INT cst, enum machine_mode mode)
   if (*cached)
     return (*cached)->cost;
 
-  *cached = xmalloc (sizeof (struct mbc_entry));
+  *cached = XNEW (struct mbc_entry);
   (*cached)->mode = mode;
   (*cached)->cst = cst;
 
@@ -3373,6 +3309,7 @@ get_address_cost (bool symbol_present, bool var_present,
   acost = costs[symbol_present][var_present][offset_p][ratio_p];
   if (!acost)
     {
+      int old_cse_not_expected;
       acost = 0;
       
       addr = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1);
@@ -3401,7 +3338,12 @@ get_address_cost (bool symbol_present, bool var_present,
        addr = gen_rtx_fmt_ee (PLUS, Pmode, addr, base);
   
       start_sequence ();
+      /* To avoid splitting addressing modes, pretend that no cse will
+        follow.  */
+      old_cse_not_expected = cse_not_expected;
+      cse_not_expected = true;
       addr = memory_address (Pmode, addr);
+      cse_not_expected = old_cse_not_expected;
       seq = get_insns ();
       end_sequence ();
   
@@ -4099,95 +4041,6 @@ determine_use_iv_cost_condition (struct ivopts_data *data,
   return cost != INFTY;
 }
 
-/* Checks whether it is possible to replace the final value of USE by
-   a direct computation.  If so, the formula is stored to *VALUE.  */
-
-static bool
-may_replace_final_value (struct ivopts_data *data, struct iv_use *use,
-                        tree *value)
-{
-  struct loop *loop = data->current_loop;
-  edge exit;
-  struct tree_niter_desc *niter;
-
-  exit = single_dom_exit (loop);
-  if (!exit)
-    return false;
-
-  gcc_assert (dominated_by_p (CDI_DOMINATORS, exit->src,
-                             bb_for_stmt (use->stmt)));
-
-  niter = niter_for_single_dom_exit (data);
-  if (!niter
-      || !zero_p (niter->may_be_zero))
-    return false;
-
-  *value = iv_value (use->iv, niter->niter);
-
-  return true;
-}
-
-/* Determines cost of replacing final value of USE using CAND.  */
-
-static bool
-determine_use_iv_cost_outer (struct ivopts_data *data,
-                            struct iv_use *use, struct iv_cand *cand)
-{
-  bitmap depends_on;
-  unsigned cost;
-  edge exit;
-  tree value = NULL_TREE;
-  struct loop *loop = data->current_loop;
-
-  /* The simple case first -- if we need to express value of the preserved
-     original biv, the cost is 0.  This also prevents us from counting the
-     cost of increment twice -- once at this use and once in the cost of
-     the candidate.  */
-  if (cand->pos == IP_ORIGINAL
-      && cand->incremented_at == use->stmt)
-    {
-      set_use_iv_cost (data, use, cand, 0, NULL, NULL_TREE);
-      return true;
-    }
-
-  if (!cand->iv)
-    {
-      if (!may_replace_final_value (data, use, &value))
-       {
-         set_use_iv_cost (data, use, cand, INFTY, NULL, NULL_TREE);
-         return false;
-       }
-
-      depends_on = NULL;
-      cost = force_var_cost (data, value, &depends_on);
-
-      cost /= AVG_LOOP_NITER (loop);
-
-      set_use_iv_cost (data, use, cand, cost, depends_on, value);
-      return cost != INFTY;
-    }
-
-  exit = single_dom_exit (loop);
-  if (exit)
-    {
-      /* If there is just a single exit, we may use value of the candidate
-        after we take it to determine the value of use.  */
-      cost = get_computation_cost_at (data, use, cand, false, &depends_on,
-                                     last_stmt (exit->src));
-      if (cost != INFTY)
-       cost /= AVG_LOOP_NITER (loop);
-    }
-  else
-    {
-      /* Otherwise we just need to compute the iv.  */
-      cost = get_computation_cost (data, use, cand, false, &depends_on);
-    }
-                                  
-  set_use_iv_cost (data, use, cand, cost, depends_on, NULL_TREE);
-
-  return cost != INFTY;
-}
-
 /* Determines cost of basing replacement of USE on CAND.  Returns false
    if USE cannot be based on CAND.  */
 
@@ -4200,9 +4053,6 @@ determine_use_iv_cost (struct ivopts_data *data,
     case USE_NONLINEAR_EXPR:
       return determine_use_iv_cost_generic (data, use, cand);
 
-    case USE_OUTER:
-      return determine_use_iv_cost_outer (data, use, cand);
-
     case USE_ADDRESS:
       return determine_use_iv_cost_address (data, use, cand);
 
@@ -4359,9 +4209,7 @@ if (dump_file && (dump_flags & TDF_DETAILS))
 static unsigned
 ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size)
 {
-  return global_cost_for_size (size,
-                              loop_data (data->current_loop)->regs_used,
-                              n_iv_uses (data));
+  return global_cost_for_size (size, data->regs_used, n_iv_uses (data));
 }
 
 /* For each size of the induction variable set determine the penalty.  */
@@ -4425,7 +4273,7 @@ determine_set_costs (struct ivopts_data *data)
        n++;
     }
 
-  loop_data (loop)->regs_used = n;
+  data->regs_used = n;
   if (dump_file && (dump_flags & TDF_DETAILS))
     fprintf (dump_file, "  regs_used %d\n", n);
 
@@ -4656,7 +4504,7 @@ static struct iv_ca_delta *
 iv_ca_delta_add (struct iv_use *use, struct cost_pair *old_cp,
                 struct cost_pair *new_cp, struct iv_ca_delta *next_change)
 {
-  struct iv_ca_delta *change = xmalloc (sizeof (struct iv_ca_delta));
+  struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
 
   change->use = use;
   change->old_cp = old_cp;
@@ -4779,18 +4627,18 @@ iv_ca_delta_free (struct iv_ca_delta **delta)
 static struct iv_ca *
 iv_ca_new (struct ivopts_data *data)
 {
-  struct iv_ca *nw = xmalloc (sizeof (struct iv_ca));
+  struct iv_ca *nw = XNEW (struct iv_ca);
 
   nw->upto = 0;
   nw->bad_uses = 0;
-  nw->cand_for_use = xcalloc (n_iv_uses (data), sizeof (struct cost_pair *));
-  nw->n_cand_uses = xcalloc (n_iv_cands (data), sizeof (unsigned));
+  nw->cand_for_use = XCNEWVEC (struct cost_pair *, n_iv_uses (data));
+  nw->n_cand_uses = XCNEWVEC (unsigned, n_iv_cands (data));
   nw->cands = BITMAP_ALLOC (NULL);
   nw->n_cands = 0;
   nw->n_regs = 0;
   nw->cand_use_cost = 0;
   nw->cand_cost = 0;
-  nw->n_invariant_uses = xcalloc (data->max_inv_id + 1, sizeof (unsigned));
+  nw->n_invariant_uses = XCNEWVEC (unsigned, data->max_inv_id + 1);
   nw->cost = 0;
 
   return nw;
@@ -5309,7 +5157,7 @@ remove_statement (tree stmt, bool including_defined_name)
     {
       block_stmt_iterator bsi = bsi_for_stmt (stmt);
 
-      bsi_remove (&bsi);
+      bsi_remove (&bsi, true);
     }
 }
 
@@ -5329,22 +5177,58 @@ rewrite_use_nonlinear_expr (struct ivopts_data *data,
      introduce a new computation (that might also need casting the
      variable to unsigned and back).  */
   if (cand->pos == IP_ORIGINAL
-      && TREE_CODE (use->stmt) == MODIFY_EXPR
-      && TREE_OPERAND (use->stmt, 0) == cand->var_after)
+      && cand->incremented_at == use->stmt)
     {
+      tree step, ctype, utype;
+      enum tree_code incr_code = PLUS_EXPR;
+
+      gcc_assert (TREE_CODE (use->stmt) == MODIFY_EXPR);
+      gcc_assert (TREE_OPERAND (use->stmt, 0) == cand->var_after);
+
+      step = cand->iv->step;
+      ctype = TREE_TYPE (step);
+      utype = TREE_TYPE (cand->var_after);
+      if (TREE_CODE (step) == NEGATE_EXPR)
+       {
+         incr_code = MINUS_EXPR;
+         step = TREE_OPERAND (step, 0);
+       }
+
+      /* Check whether we may leave the computation unchanged.
+        This is the case only if it does not rely on other
+        computations in the loop -- otherwise, the computation
+        we rely upon may be removed in remove_unused_ivs,
+        thus leading to ICE.  */
       op = TREE_OPERAND (use->stmt, 1);
+      if (TREE_CODE (op) == PLUS_EXPR
+         || TREE_CODE (op) == MINUS_EXPR)
+       {
+         if (TREE_OPERAND (op, 0) == cand->var_before)
+           op = TREE_OPERAND (op, 1);
+         else if (TREE_CODE (op) == PLUS_EXPR
+                  && TREE_OPERAND (op, 1) == cand->var_before)
+           op = TREE_OPERAND (op, 0);
+         else
+           op = NULL_TREE;
+       }
+      else
+       op = NULL_TREE;
 
-      /* Be a bit careful.  In case variable is expressed in some
-        complicated way, rewrite it so that we may get rid of this
-        complicated expression.  */
-      if ((TREE_CODE (op) == PLUS_EXPR
-          || TREE_CODE (op) == MINUS_EXPR)
-         && TREE_OPERAND (op, 0) == cand->var_before
-         && TREE_CODE (TREE_OPERAND (op, 1)) == INTEGER_CST)
+      if (op
+         && (TREE_CODE (op) == INTEGER_CST
+             || operand_equal_p (op, step, 0)))
        return;
+
+      /* Otherwise, add the necessary computations to express
+        the iv.  */
+      op = fold_convert (ctype, cand->var_before);
+      comp = fold_convert (utype,
+                          build2 (incr_code, ctype, op,
+                                  unshare_expr (step)));
     }
+  else
+    comp = get_computation (data->current_loop, use, cand);
 
-  comp = get_computation (data->current_loop, use, cand);
   switch (TREE_CODE (use->stmt))
     {
     case PHI_NODE:
@@ -5437,18 +5321,33 @@ unshare_and_remove_ssa_names (tree ref)
    and extracts this single useful piece of information.  */
 
 static tree
-get_ref_tag (tree ref)
+get_ref_tag (tree ref, tree orig)
 {
   tree var = get_base_address (ref);
-  tree tag;
+  tree aref = NULL_TREE, tag, sv;
+  HOST_WIDE_INT offset, size, maxsize;
+
+  for (sv = orig; handled_component_p (sv); sv = TREE_OPERAND (sv, 0))
+    {
+      aref = get_ref_base_and_extent (sv, &offset, &size, &maxsize);
+      if (ref)
+       break;
+    }
+
+  if (aref && SSA_VAR_P (aref) && get_subvars_for_var (aref))
+    return unshare_expr (sv);
 
   if (!var)
     return NULL_TREE;
 
   if (TREE_CODE (var) == INDIRECT_REF)
-    var = TREE_OPERAND (var, 0);
-  if (TREE_CODE (var) == SSA_NAME)
     {
+      /* If the base is a dereference of a pointer, first check its name memory
+        tag.  If it does not have one, use its symbol memory tag.  */
+      var = TREE_OPERAND (var, 0);
+      if (TREE_CODE (var) != SSA_NAME)
+       return NULL_TREE;
+
       if (SSA_NAME_PTR_INFO (var))
        {
          tag = SSA_NAME_PTR_INFO (var)->name_mem_tag;
@@ -5457,18 +5356,21 @@ get_ref_tag (tree ref)
        }
  
       var = SSA_NAME_VAR (var);
+      tag = var_ann (var)->symbol_mem_tag;
+      gcc_assert (tag != NULL_TREE);
+      return tag;
     }
-  if (DECL_P (var))
-    {
-      tag = var_ann (var)->type_mem_tag;
+  else
+    { 
+      if (!DECL_P (var))
+       return NULL_TREE;
+
+      tag = var_ann (var)->symbol_mem_tag;
       if (tag)
        return tag;
 
       return var;
     }
-
-  return NULL_TREE;
 }
 
 /* Copies the reference information from OLD_REF to NEW_REF.  */
@@ -5480,8 +5382,8 @@ copy_ref_info (tree new_ref, tree old_ref)
     copy_mem_ref_info (new_ref, old_ref);
   else
     {
-      TMR_TAG (new_ref) = get_ref_tag (old_ref);
       TMR_ORIGINAL (new_ref) = unshare_and_remove_ssa_names (old_ref);
+      TMR_TAG (new_ref) = get_ref_tag (old_ref, TMR_ORIGINAL (new_ref));
     }
 }
 
@@ -5628,14 +5530,15 @@ compute_phi_arg_on_exit (edge exit, tree stmts, tree op)
     {
       for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
         {
-         bsi_insert_after (&bsi, tsi_stmt (tsi), BSI_NEW_STMT);
-         protect_loop_closed_ssa_form (exit, bsi_stmt (bsi));
+         tree stmt = tsi_stmt (tsi);
+         bsi_insert_before (&bsi, stmt, BSI_SAME_STMT);
+         protect_loop_closed_ssa_form (exit, stmt);
        }
     }
   else
     {
-      bsi_insert_after (&bsi, stmts, BSI_NEW_STMT);
-      protect_loop_closed_ssa_form (exit, bsi_stmt (bsi));
+      bsi_insert_before (&bsi, stmts, BSI_SAME_STMT);
+      protect_loop_closed_ssa_form (exit, stmts);
     }
 
   if (!op)
@@ -5652,81 +5555,11 @@ compute_phi_arg_on_exit (edge exit, tree stmts, tree op)
          stmt = build2 (MODIFY_EXPR, TREE_TYPE (op),
                        def, op);
          SSA_NAME_DEF_STMT (def) = stmt;
-         bsi_insert_after (&bsi, stmt, BSI_CONTINUE_LINKING);
+         bsi_insert_before (&bsi, stmt, BSI_SAME_STMT);
        }
     }
 }
 
-/* Rewrites the final value of USE (that is only needed outside of the loop)
-   using candidate CAND.  */
-
-static void
-rewrite_use_outer (struct ivopts_data *data,
-                  struct iv_use *use, struct iv_cand *cand)
-{
-  edge exit;
-  tree value, op, stmts, tgt;
-  tree phi;
-
-  switch (TREE_CODE (use->stmt))
-    {
-    case PHI_NODE:
-      tgt = PHI_RESULT (use->stmt);
-      break;
-    case MODIFY_EXPR:
-      tgt = TREE_OPERAND (use->stmt, 0);
-      break;
-    default:
-      gcc_unreachable ();
-    }
-
-  exit = single_dom_exit (data->current_loop);
-
-  if (exit)
-    {
-      if (!cand->iv)
-       {
-         struct cost_pair *cp = get_use_iv_cost (data, use, cand);
-         value = unshare_expr (cp->value);
-       }
-      else
-       value = get_computation_at (data->current_loop,
-                                   use, cand, last_stmt (exit->src));
-
-      op = force_gimple_operand (value, &stmts, true, SSA_NAME_VAR (tgt));
-         
-      /* If we will preserve the iv anyway and we would need to perform
-        some computation to replace the final value, do nothing.  */
-      if (stmts && name_info (data, tgt)->preserve_biv)
-       return;
-
-      for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi))
-       {
-         use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, exit);
-
-         if (USE_FROM_PTR (use_p) == tgt)
-           SET_USE (use_p, op);
-       }
-
-      if (stmts)
-       compute_phi_arg_on_exit (exit, stmts, op);
-
-      /* Enable removal of the statement.  We cannot remove it directly,
-        since we may still need the aliasing information attached to the
-        ssa name defined by it.  */
-      name_info (data, tgt)->iv->have_use_for = false;
-      return;
-    }
-
-  /* If the variable is going to be preserved anyway, there is nothing to
-     do.  */
-  if (name_info (data, tgt)->preserve_biv)
-    return;
-
-  /* Otherwise we just need to compute the iv.  */
-  rewrite_use_nonlinear_expr (data, use, cand);
-}
-
 /* Rewrites USE using candidate CAND.  */
 
 static void
@@ -5739,10 +5572,6 @@ rewrite_use (struct ivopts_data *data,
        rewrite_use_nonlinear_expr (data, use, cand);
        break;
 
-      case USE_OUTER:
-       rewrite_use_outer (data, use, cand);
-       break;
-
       case USE_ADDRESS:
        rewrite_use_address (data, use, cand);
        break;
@@ -5754,7 +5583,7 @@ rewrite_use (struct ivopts_data *data,
       default:
        gcc_unreachable ();
     }
-  update_stmt (use->stmt);
+  mark_new_vars_to_rename (use->stmt);
 }
 
 /* Rewrite the uses using the selected induction variables.  */
@@ -5854,8 +5683,7 @@ free_loop_data (struct ivopts_data *data)
     {
       data->version_info_size = 2 * num_ssa_names;
       free (data->version_info);
-      data->version_info = xcalloc (data->version_info_size,
-                                   sizeof (struct version_info));
+      data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
     }
 
   data->max_inv_id = 0;
@@ -5870,17 +5698,8 @@ free_loop_data (struct ivopts_data *data)
    loop tree.  */
 
 static void
-tree_ssa_iv_optimize_finalize (struct loops *loops, struct ivopts_data *data)
+tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
 {
-  unsigned i;
-
-  for (i = 1; i < loops->num; i++)
-    if (loops->parray[i])
-      {
-       free (loops->parray[i]->aux);
-       loops->parray[i]->aux = NULL;
-      }
-
   free_loop_data (data);
   free (data->version_info);
   BITMAP_FREE (data->relevant);
@@ -5972,7 +5791,7 @@ tree_ssa_iv_optimize (struct loops *loops)
   struct loop *loop;
   struct ivopts_data data;
 
-  tree_ssa_iv_optimize_init (loops, &data);
+  tree_ssa_iv_optimize_init (&data);
 
   /* Optimize the loops starting with the innermost ones.  */
   loop = loops->tree_root;
@@ -5997,5 +5816,5 @@ tree_ssa_iv_optimize (struct loops *loops)
        loop = loop->outer;
     }
 
-  tree_ssa_iv_optimize_finalize (loops, &data);
+  tree_ssa_iv_optimize_finalize (&data);
 }