OSDN Git Service

2010-04-28 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ira-build.c
index fa0a39c..c879efa 100644 (file)
@@ -1,5 +1,5 @@
 /* Building internal representation for IRA.
-   Copyright (C) 2006, 2007, 2008
+   Copyright (C) 2006, 2007, 2008, 2009
    Free Software Foundation, Inc.
    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
 
@@ -58,7 +58,7 @@ ira_loop_tree_node_t ira_bb_nodes;
    array.  */
 ira_loop_tree_node_t ira_loop_nodes;
 
-/* Map regno -> allocnos with given regno (see comments for 
+/* Map regno -> allocnos with given regno (see comments for
    allocno member `next_regno_allocno').  */
 ira_allocno_t *ira_regno_allocno_map;
 
@@ -456,6 +456,7 @@ ira_create_allocno (int regno, bool cap_p, ira_loop_tree_node_t loop_tree_node)
   ALLOCNO_SOMEWHERE_RENAMED_P (a) = false;
   ALLOCNO_CHILD_RENAMED_P (a) = false;
   ALLOCNO_DONT_REASSIGN_P (a) = false;
+  ALLOCNO_BAD_SPILL_P (a) = false;
   ALLOCNO_IN_GRAPH_P (a) = false;
   ALLOCNO_ASSIGNED_P (a) = false;
   ALLOCNO_MAY_BE_SPILLED_P (a) = false;
@@ -467,8 +468,9 @@ ira_create_allocno (int regno, bool cap_p, ira_loop_tree_node_t loop_tree_node)
   ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
   ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
   ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
-  ALLOCNO_LEFT_CONFLICTS_NUM (a) = -1;
+  ALLOCNO_LEFT_CONFLICTS_SIZE (a) = -1;
   ALLOCNO_COVER_CLASS (a) = NO_REGS;
+  ALLOCNO_UPDATED_COVER_CLASS_COST (a) = 0;
   ALLOCNO_COVER_CLASS_COST (a) = 0;
   ALLOCNO_MEMORY_COST (a) = 0;
   ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
@@ -769,12 +771,12 @@ create_cap_allocno (ira_allocno_t a)
   ALLOCNO_CAP (a) = cap;
   ALLOCNO_COVER_CLASS_COST (cap) = ALLOCNO_COVER_CLASS_COST (a);
   ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
-  ALLOCNO_UPDATED_MEMORY_COST (cap) = ALLOCNO_UPDATED_MEMORY_COST (a);
   ira_allocate_and_copy_costs
     (&ALLOCNO_HARD_REG_COSTS (cap), cover_class, ALLOCNO_HARD_REG_COSTS (a));
   ira_allocate_and_copy_costs
     (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), cover_class,
      ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
+  ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
   ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
   ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
   ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
@@ -824,8 +826,8 @@ copy_allocno_live_range (allocno_live_range_t r)
 
 /* Copy allocno live range list given by its head R and return the
    result.  */
-static allocno_live_range_t
-copy_allocno_live_range_list (allocno_live_range_t r)
+allocno_live_range_t
+ira_copy_allocno_live_range_list (allocno_live_range_t r)
 {
   allocno_live_range_t p, first, last;
 
@@ -843,6 +845,103 @@ copy_allocno_live_range_list (allocno_live_range_t r)
   return first;
 }
 
+/* Merge ranges R1 and R2 and returns the result.  The function
+   maintains the order of ranges and tries to minimize number of the
+   result ranges.  */
+allocno_live_range_t
+ira_merge_allocno_live_ranges (allocno_live_range_t r1,
+                              allocno_live_range_t r2)
+{
+  allocno_live_range_t first, last, temp;
+
+  if (r1 == NULL)
+    return r2;
+  if (r2 == NULL)
+    return r1;
+  for (first = last = NULL; r1 != NULL && r2 != NULL;)
+    {
+      if (r1->start < r2->start)
+       {
+         temp = r1;
+         r1 = r2;
+         r2 = temp;
+       }
+      if (r1->start <= r2->finish + 1)
+       {
+         /* Intersected ranges: merge r1 and r2 into r1.  */
+         r1->start = r2->start;
+         if (r1->finish < r2->finish)
+           r1->finish = r2->finish;
+         temp = r2;
+         r2 = r2->next;
+         ira_finish_allocno_live_range (temp);
+         if (r2 == NULL)
+           {
+             /* To try to merge with subsequent ranges in r1.  */
+             r2 = r1->next;
+             r1->next = NULL;
+           }
+       }
+      else
+       {
+         /* Add r1 to the result.  */
+         if (first == NULL)
+           first = last = r1;
+         else
+           {
+             last->next = r1;
+             last = r1;
+           }
+         r1 = r1->next;
+         if (r1 == NULL)
+           {
+             /* To try to merge with subsequent ranges in r2.  */
+             r1 = r2->next;
+             r2->next = NULL;
+           }
+       }
+    }
+  if (r1 != NULL)
+    {
+      if (first == NULL)
+       first = r1;
+      else
+       last->next = r1;
+      ira_assert (r1->next == NULL);
+    }
+  else if (r2 != NULL)
+    {
+      if (first == NULL)
+       first = r2;
+      else
+       last->next = r2;
+      ira_assert (r2->next == NULL);
+    }
+  else
+    {
+      ira_assert (last->next == NULL);
+    }
+  return first;
+}
+
+/* Return TRUE if live ranges R1 and R2 intersect.  */
+bool
+ira_allocno_live_ranges_intersect_p (allocno_live_range_t r1,
+                                    allocno_live_range_t r2)
+{
+  /* Remember the live ranges are always kept ordered.  */
+  while (r1 != NULL && r2 != NULL)
+    {
+      if (r1->start > r2->finish)
+       r1 = r1->next;
+      else if (r2->start > r1->finish)
+       r2 = r2->next;
+      else
+       return true;
+    }
+  return false;
+}
+
 /* Free allocno live range R.  */
 void
 ira_finish_allocno_live_range (allocno_live_range_t r)
@@ -850,6 +949,19 @@ ira_finish_allocno_live_range (allocno_live_range_t r)
   pool_free (allocno_live_range_pool, r);
 }
 
+/* Free list of allocno live ranges starting with R.  */
+void
+ira_finish_allocno_live_range_list (allocno_live_range_t r)
+{
+  allocno_live_range_t next_r;
+
+  for (; r != NULL; r = next_r)
+    {
+      next_r = r->next;
+      ira_finish_allocno_live_range (r);
+    }
+}
+
 /* Free updated register costs of allocno A.  */
 void
 ira_free_allocno_updated_costs (ira_allocno_t a)
@@ -870,7 +982,6 @@ ira_free_allocno_updated_costs (ira_allocno_t a)
 static void
 finish_allocno (ira_allocno_t a)
 {
-  allocno_live_range_t r, next_r;
   enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
 
   ira_allocnos[ALLOCNO_NUM (a)] = NULL;
@@ -886,11 +997,7 @@ finish_allocno (ira_allocno_t a)
   if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
     ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
                          cover_class);
-  for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = next_r)
-    {
-      next_r = r->next;
-      ira_finish_allocno_live_range (r);
-    }
+  ira_finish_allocno_live_range_list (ALLOCNO_LIVE_RANGES (a));
   pool_free (allocno_pool, a);
 }
 
@@ -961,9 +1068,10 @@ find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx insn,
 }
 
 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
-   SECOND, FREQ, and INSN.  */
+   SECOND, FREQ, CONSTRAINT_P, and INSN.  */
 ira_copy_t
-ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq, rtx insn,
+ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
+                bool constraint_p, rtx insn,
                 ira_loop_tree_node_t loop_tree_node)
 {
   ira_copy_t cp;
@@ -973,6 +1081,7 @@ ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq, rtx insn,
   cp->first = first;
   cp->second = second;
   cp->freq = freq;
+  cp->constraint_p = constraint_p;
   cp->insn = insn;
   cp->loop_tree_node = loop_tree_node;
   VEC_safe_push (ira_copy_t, heap, copy_vec, cp);
@@ -1081,7 +1190,8 @@ ira_swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
    LOOP_TREE_NODE.  */
 ira_copy_t
 ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
-                     rtx insn, ira_loop_tree_node_t loop_tree_node)
+                     bool constraint_p, rtx insn,
+                     ira_loop_tree_node_t loop_tree_node)
 {
   ira_copy_t cp;
 
@@ -1090,7 +1200,8 @@ ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
       cp->freq += freq;
       return cp;
     }
-  cp = ira_create_copy (first, second, freq, insn, loop_tree_node);
+  cp = ira_create_copy (first, second, freq, constraint_p, insn,
+                       loop_tree_node);
   ira_assert (first != NULL && second != NULL);
   ira_add_allocno_copy_to_list (cp);
   ira_swap_allocno_copy_ends_if_necessary (cp);
@@ -1101,9 +1212,11 @@ ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
 static void
 print_copy (FILE *f, ira_copy_t cp)
 {
-  fprintf (f, "  cp%d:a%d(r%d)<->a%d(r%d)@%d\n", cp->num,
+  fprintf (f, "  cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
           ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
-          ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq);
+          ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
+          cp->insn != NULL
+          ? "move" : cp->constraint_p ? "constraint" : "shuffle");
 }
 
 /* Print info about copy CP into stderr.  */
@@ -1266,7 +1379,7 @@ ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
 
   if (preorder_func != NULL)
     (*preorder_func) (loop_node);
-  
+
   if (bb_p)
     for (subloop_node = loop_node->children;
         subloop_node != NULL;
@@ -1275,11 +1388,11 @@ ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
        {
          if (preorder_func != NULL)
            (*preorder_func) (subloop_node);
-  
+
          if (postorder_func != NULL)
            (*postorder_func) (subloop_node);
        }
-  
+
   for (subloop_node = loop_node->subloops;
        subloop_node != NULL;
        subloop_node = subloop_node->subloop_next)
@@ -1321,7 +1434,7 @@ create_insn_allocnos (rtx x, bool output_p)
 
          if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
            a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
-         
+
          ALLOCNO_NREFS (a)++;
          ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
          if (output_p)
@@ -1345,7 +1458,7 @@ create_insn_allocnos (rtx x, bool output_p)
       create_insn_allocnos (XEXP (x, 0), false);
       return;
     }
-  else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC || 
+  else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
           code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
     {
       create_insn_allocnos (XEXP (x, 0), true);
@@ -1378,7 +1491,7 @@ create_bb_allocnos (ira_loop_tree_node_t bb_node)
   curr_bb = bb = bb_node->bb;
   ira_assert (bb != NULL);
   FOR_BB_INSNS_REVERSE (bb, insn)
-    if (INSN_P (insn))
+    if (NONDEBUG_INSN_P (insn))
       create_insn_allocnos (PATTERN (insn), false);
   /* It might be a allocno living through from one subloop to
      another.  */
@@ -1436,7 +1549,7 @@ create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
       FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
        if (e->src != loop_node->loop->latch)
          create_loop_allocnos (e);
-      
+
       edges = get_loop_exit_edges (loop_node->loop);
       for (i = 0; VEC_iterate (edge, edges, i, e); i++)
        create_loop_allocnos (e);
@@ -1470,8 +1583,8 @@ propagate_allocno_info (void)
   ira_loop_tree_node_t parent;
   enum reg_class cover_class;
 
-  if (flag_ira_algorithm != IRA_ALGORITHM_REGIONAL
-      && flag_ira_algorithm != IRA_ALGORITHM_MIXED)
+  if (flag_ira_region != IRA_REGION_ALL
+      && flag_ira_region != IRA_REGION_MIXED)
     return;
   for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
     for (a = ira_regno_allocno_map[i];
@@ -1484,6 +1597,8 @@ propagate_allocno_info (void)
          && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
                           ALLOCNO_NUM (a)))
        {
+         if (! ALLOCNO_BAD_SPILL_P (a))
+           ALLOCNO_BAD_SPILL_P (parent_a) = false;
          ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
          ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
          ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
@@ -1509,8 +1624,6 @@ propagate_allocno_info (void)
          ALLOCNO_COVER_CLASS_COST (parent_a)
            += ALLOCNO_COVER_CLASS_COST (a);
          ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
-         ALLOCNO_UPDATED_MEMORY_COST (parent_a)
-           += ALLOCNO_UPDATED_MEMORY_COST (a);
        }
 }
 
@@ -1535,84 +1648,6 @@ create_allocnos (void)
    will hardly improve the result.  As a result we speed up regional
    register allocation.  */
 
-/* Merge ranges R1 and R2 and returns the result.  The function
-   maintains the order of ranges and tries to minimize number of the
-   result ranges.  */
-static allocno_live_range_t 
-merge_ranges (allocno_live_range_t r1, allocno_live_range_t r2)
-{
-  allocno_live_range_t first, last, temp;
-
-  if (r1 == NULL)
-    return r2;
-  if (r2 == NULL)
-    return r1;
-  for (first = last = NULL; r1 != NULL && r2 != NULL;)
-    {
-      if (r1->start < r2->start)
-       {
-         temp = r1;
-         r1 = r2;
-         r2 = temp;
-       }
-      if (r1->start <= r2->finish + 1)
-       {
-         /* Intersected ranges: merge r1 and r2 into r1.  */
-         r1->start = r2->start;
-         if (r1->finish < r2->finish)
-           r1->finish = r2->finish;
-         temp = r2;
-         r2 = r2->next;
-         ira_finish_allocno_live_range (temp);
-         if (r2 == NULL)
-           {
-             /* To try to merge with subsequent ranges in r1.  */
-             r2 = r1->next;
-             r1->next = NULL;
-           }
-       }
-      else
-       {
-         /* Add r1 to the result.  */
-         if (first == NULL)
-           first = last = r1;
-         else
-           {
-             last->next = r1;
-             last = r1;
-           }
-         r1 = r1->next;
-         if (r1 == NULL)
-           {
-             /* To try to merge with subsequent ranges in r2.  */
-             r1 = r2->next;
-             r2->next = NULL;
-           }
-       }
-    }
-  if (r1 != NULL)
-    {
-      if (first == NULL)
-       first = r1;
-      else
-       last->next = r1;
-      ira_assert (r1->next == NULL);
-    }
-  else if (r2 != NULL)
-    {
-      if (first == NULL)
-       first = r2;
-      else
-       last->next = r2;
-      ira_assert (r2->next == NULL);
-    }
-  else
-    {
-      ira_assert (last->next == NULL);
-    }
-  return first;
-}
-
 /* The function changes allocno in range list given by R onto A.  */
 static void
 change_allocno_in_range_list (allocno_live_range_t r, ira_allocno_t a)
@@ -1628,10 +1663,10 @@ low_pressure_loop_node_p (ira_loop_tree_node_t node)
 {
   int i;
   enum reg_class cover_class;
-  
+
   if (node->bb != NULL)
     return false;
-  
+
   for (i = 0; i < ira_reg_class_cover_size; i++)
     {
       cover_class = ira_reg_class_cover[i];
@@ -1642,18 +1677,106 @@ low_pressure_loop_node_p (ira_loop_tree_node_t node)
   return true;
 }
 
-/* Return TRUE if NODE represents a loop with should be removed from
-   regional allocation.  We remove a loop with low register pressure
-   inside another loop with register pressure.  In this case a
-   separate allocation of the loop hardly helps (for irregular
-   register file architecture it could help by choosing a better hard
-   register in the loop but we prefer faster allocation even in this
-   case).  */
-static bool
-loop_node_to_be_removed_p (ira_loop_tree_node_t node)
+/* Sort loops for marking them for removal.  We put already marked
+   loops first, then less frequent loops next, and then outer loops
+   next.  */
+static int
+loop_compare_func (const void *v1p, const void *v2p)
+{
+  int diff;
+  ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
+  ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
+
+  ira_assert (l1->parent != NULL && l2->parent != NULL);
+  if (l1->to_remove_p && ! l2->to_remove_p)
+    return -1;
+  if (! l1->to_remove_p && l2->to_remove_p)
+    return 1;
+  if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
+    return diff;
+  if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
+    return diff;
+  /* Make sorting stable.  */
+  return l1->loop->num - l2->loop->num;
+}
+
+
+/* Mark loops which should be removed from regional allocation.  We
+   remove a loop with low register pressure inside another loop with
+   register pressure.  In this case a separate allocation of the loop
+   hardly helps (for irregular register file architecture it could
+   help by choosing a better hard register in the loop but we prefer
+   faster allocation even in this case).  We also remove cheap loops
+   if there are more than IRA_MAX_LOOPS_NUM of them.  */
+static void
+mark_loops_for_removal (void)
 {
-  return (node->parent != NULL && low_pressure_loop_node_p (node->parent)
-         && low_pressure_loop_node_p (node));
+  int i, n;
+  ira_loop_tree_node_t *sorted_loops;
+  loop_p loop;
+
+  sorted_loops
+    = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
+                                            * VEC_length (loop_p,
+                                                          ira_loops.larray));
+  for (n = i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
+    if (ira_loop_nodes[i].regno_allocno_map != NULL)
+      {
+       if (ira_loop_nodes[i].parent == NULL)
+         {
+           /* Don't remove the root.  */
+           ira_loop_nodes[i].to_remove_p = false;
+           continue;
+         }
+       sorted_loops[n++] = &ira_loop_nodes[i];
+       ira_loop_nodes[i].to_remove_p
+         = (low_pressure_loop_node_p (ira_loop_nodes[i].parent)
+            && low_pressure_loop_node_p (&ira_loop_nodes[i]));
+      }
+  qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
+  for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
+    {
+      sorted_loops[i]->to_remove_p = true;
+      if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
+       fprintf
+         (ira_dump_file,
+          "  Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
+          sorted_loops[i]->loop->num, sorted_loops[i]->loop->header->index,
+          sorted_loops[i]->loop->header->frequency,
+          loop_depth (sorted_loops[i]->loop),
+          low_pressure_loop_node_p (sorted_loops[i]->parent)
+          && low_pressure_loop_node_p (sorted_loops[i])
+          ? "low pressure" : "cheap loop");
+    }
+  ira_free (sorted_loops);
+}
+
+/* Mark all loops but root for removing.  */
+static void
+mark_all_loops_for_removal (void)
+{
+  int i;
+  loop_p loop;
+
+  for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
+    if (ira_loop_nodes[i].regno_allocno_map != NULL)
+      {
+       if (ira_loop_nodes[i].parent == NULL)
+         {
+           /* Don't remove the root.  */
+           ira_loop_nodes[i].to_remove_p = false;
+           continue;
+         }
+       ira_loop_nodes[i].to_remove_p = true;
+       if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
+         fprintf
+           (ira_dump_file,
+            "  Mark loop %d (header %d, freq %d, depth %d) for removal\n",
+            ira_loop_nodes[i].loop->num,
+            ira_loop_nodes[i].loop->header->index,
+            ira_loop_nodes[i].loop->header->frequency,
+            loop_depth (ira_loop_nodes[i].loop));
+      }
 }
 
 /* Definition of vector of loop tree nodes.  */
@@ -1675,7 +1798,7 @@ remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
   bool remove_p;
   ira_loop_tree_node_t subnode;
 
-  remove_p = loop_node_to_be_removed_p (node);
+  remove_p = node->to_remove_p;
   if (! remove_p)
     VEC_safe_push (ira_loop_tree_node_t, heap, children_vec, node);
   start = VEC_length (ira_loop_tree_node_t, children_vec);
@@ -1704,109 +1827,263 @@ remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
     }
 }
 
+/* Return TRUE if NODE is inside PARENT.  */
+static bool
+loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
+{
+  for (node = node->parent; node != NULL; node = node->parent)
+    if (node == parent)
+      return true;
+  return false;
+}
+
+/* Sort allocnos according to their order in regno allocno list.  */
+static int
+regno_allocno_order_compare_func (const void *v1p, const void *v2p)
+{
+  ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
+  ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
+  ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
+  ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
+
+  if (loop_is_inside_p (n1, n2))
+    return -1;
+  else if (loop_is_inside_p (n2, n1))
+    return 1;
+  /* If allocnos are equally good, sort by allocno numbers, so that
+     the results of qsort leave nothing to chance.  We put allocnos
+     with higher number first in the list because it is the original
+     order for allocnos from loops on the same levels.  */
+  return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
+}
+
+/* This array is used to sort allocnos to restore allocno order in
+   the regno allocno list.  */
+static ira_allocno_t *regno_allocnos;
+
+/* Restore allocno order for REGNO in the regno allocno list.  */
+static void
+ira_rebuild_regno_allocno_list (int regno)
+{
+  int i, n;
+  ira_allocno_t a;
+
+  for (n = 0, a = ira_regno_allocno_map[regno];
+       a != NULL;
+       a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
+    regno_allocnos[n++] = a;
+  ira_assert (n > 0);
+  qsort (regno_allocnos, n, sizeof (ira_allocno_t),
+        regno_allocno_order_compare_func);
+  for (i = 1; i < n; i++)
+    ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
+  ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
+  ira_regno_allocno_map[regno] = regno_allocnos[0];
+  if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
+    fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
+}
+
+/* Propagate info from allocno FROM_A to allocno A.  */
+static void
+propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
+{
+  enum reg_class cover_class;
+
+  IOR_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (a),
+                   ALLOCNO_CONFLICT_HARD_REGS (from_a));
+#ifdef STACK_REGS
+  if (ALLOCNO_NO_STACK_REG_P (from_a))
+    ALLOCNO_NO_STACK_REG_P (a) = true;
+#endif
+  ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
+  ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
+  ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
+  IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a),
+                   ALLOCNO_TOTAL_CONFLICT_HARD_REGS (from_a));
+  ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
+  ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
+    += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
+  if (! ALLOCNO_BAD_SPILL_P (from_a))
+    ALLOCNO_BAD_SPILL_P (a) = false;
+#ifdef STACK_REGS
+  if (ALLOCNO_TOTAL_NO_STACK_REG_P (from_a))
+    ALLOCNO_TOTAL_NO_STACK_REG_P (a) = true;
+#endif
+  cover_class = ALLOCNO_COVER_CLASS (from_a);
+  ira_assert (cover_class == ALLOCNO_COVER_CLASS (a));
+  ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
+                                    ALLOCNO_HARD_REG_COSTS (from_a));
+  ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
+                                    cover_class,
+                                    ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
+  ALLOCNO_COVER_CLASS_COST (a) += ALLOCNO_COVER_CLASS_COST (from_a);
+  ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
+}
+
 /* Remove allocnos from loops removed from the allocation
    consideration.  */
 static void
 remove_unnecessary_allocnos (void)
 {
   int regno;
-  bool merged_p;
-  enum reg_class cover_class;
+  bool merged_p, rebuild_p;
   ira_allocno_t a, prev_a, next_a, parent_a;
   ira_loop_tree_node_t a_node, parent;
   allocno_live_range_t r;
 
   merged_p = false;
+  regno_allocnos = NULL;
   for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
-    for (prev_a = NULL, a = ira_regno_allocno_map[regno];
-        a != NULL;
-        a = next_a)
-      {
-       next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
-       a_node = ALLOCNO_LOOP_TREE_NODE (a);
-       if (! loop_node_to_be_removed_p (a_node))
-         prev_a = a;
-       else
-         {
-           for (parent = a_node->parent;
-                (parent_a = parent->regno_allocno_map[regno]) == NULL
-                  && loop_node_to_be_removed_p (parent);
-                parent = parent->parent)
-             ;
-           if (parent_a == NULL)
-             {
-               /* There are no allocnos with the same regno in upper
-                  region -- just move the allocno to the upper
-                  region.  */
-               prev_a = a;
-               ALLOCNO_LOOP_TREE_NODE (a) = parent;
-               parent->regno_allocno_map[regno] = a;
-               bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
-             }
-           else
-             {
-               /* Remove the allocno and update info of allocno in
-                  the upper region.  */
-               if (prev_a == NULL)
-                 ira_regno_allocno_map[regno] = next_a;
-               else
-                 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
-               r = ALLOCNO_LIVE_RANGES (a);
-               change_allocno_in_range_list (r, parent_a);
-               ALLOCNO_LIVE_RANGES (parent_a)
-                 = merge_ranges (r, ALLOCNO_LIVE_RANGES (parent_a));
-               merged_p = true;
-               ALLOCNO_LIVE_RANGES (a) = NULL;
-               IOR_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (parent_a),
-                                 ALLOCNO_CONFLICT_HARD_REGS (a));
-#ifdef STACK_REGS
-               if (ALLOCNO_NO_STACK_REG_P (a))
-                 ALLOCNO_NO_STACK_REG_P (parent_a) = true;
-#endif
-               ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
-               ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
-               ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
-               IOR_HARD_REG_SET
-                 (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (parent_a),
-                  ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
-               ALLOCNO_CALLS_CROSSED_NUM (parent_a)
-                 += ALLOCNO_CALLS_CROSSED_NUM (a);
-               ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
-                 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
+    {
+      rebuild_p = false;
+      for (prev_a = NULL, a = ira_regno_allocno_map[regno];
+          a != NULL;
+          a = next_a)
+       {
+         next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
+         a_node = ALLOCNO_LOOP_TREE_NODE (a);
+         if (! a_node->to_remove_p)
+           prev_a = a;
+         else
+           {
+             for (parent = a_node->parent;
+                  (parent_a = parent->regno_allocno_map[regno]) == NULL
+                    && parent->to_remove_p;
+                  parent = parent->parent)
+               ;
+             if (parent_a == NULL)
+               {
+                 /* There are no allocnos with the same regno in
+                    upper region -- just move the allocno to the
+                    upper region.  */
+                 prev_a = a;
+                 ALLOCNO_LOOP_TREE_NODE (a) = parent;
+                 parent->regno_allocno_map[regno] = a;
+                 bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
+                 rebuild_p = true;
+               }
+             else
+               {
+                 /* Remove the allocno and update info of allocno in
+                    the upper region.  */
+                 if (prev_a == NULL)
+                   ira_regno_allocno_map[regno] = next_a;
+                 else
+                   ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
+                 r = ALLOCNO_LIVE_RANGES (a);
+                 change_allocno_in_range_list (r, parent_a);
+                 ALLOCNO_LIVE_RANGES (parent_a)
+                   = ira_merge_allocno_live_ranges
+                     (r, ALLOCNO_LIVE_RANGES (parent_a));
+                 merged_p = true;
+                 ALLOCNO_LIVE_RANGES (a) = NULL;
+                 propagate_some_info_from_allocno (parent_a, a);
+                 /* Remove it from the corresponding regno allocno
+                    map to avoid info propagation of subsequent
+                    allocno into this already removed allocno.  */
+                 a_node->regno_allocno_map[regno] = NULL;
+                 finish_allocno (a);
+               }
+           }
+       }
+      if (rebuild_p)
+       /* We need to restore the order in regno allocno list.  */
+       {
+         if (regno_allocnos == NULL)
+           regno_allocnos
+             = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
+                                               * ira_allocnos_num);
+         ira_rebuild_regno_allocno_list (regno);
+       }
+    }
+  if (merged_p)
+    ira_rebuild_start_finish_chains ();
+  if (regno_allocnos != NULL)
+    ira_free (regno_allocnos);
+}
+
+/* Remove allocnos from all loops but the root.  */
+static void
+remove_low_level_allocnos (void)
+{
+  int regno;
+  bool merged_p, propagate_p;
+  ira_allocno_t a, top_a;
+  ira_loop_tree_node_t a_node, parent;
+  allocno_live_range_t r;
+  ira_allocno_iterator ai;
+
+  merged_p = false;
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      a_node = ALLOCNO_LOOP_TREE_NODE (a);
+      if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
+       continue;
+      regno = ALLOCNO_REGNO (a);
+      if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
+       {
+         ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
+         ira_loop_tree_root->regno_allocno_map[regno] = a;
+         continue;
+       }
+      propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
+      /* Remove the allocno and update info of allocno in the upper
+        region.  */
+      r = ALLOCNO_LIVE_RANGES (a);
+      change_allocno_in_range_list (r, top_a);
+      ALLOCNO_LIVE_RANGES (top_a)
+       = ira_merge_allocno_live_ranges (r, ALLOCNO_LIVE_RANGES (top_a));
+      merged_p = true;
+      ALLOCNO_LIVE_RANGES (a) = NULL;
+      if (propagate_p)
+       propagate_some_info_from_allocno (top_a, a);
+    }
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      a_node = ALLOCNO_LOOP_TREE_NODE (a);
+      if (a_node == ira_loop_tree_root)
+       continue;
+      parent = a_node->parent;
+      regno = ALLOCNO_REGNO (a);
+      if (ALLOCNO_CAP_MEMBER (a) != NULL)
+       ira_assert (ALLOCNO_CAP (a) != NULL);
+      else if (ALLOCNO_CAP (a) == NULL)
+       ira_assert (parent->regno_allocno_map[regno] != NULL);
+    }
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      regno = ALLOCNO_REGNO (a);
+      if (ira_loop_tree_root->regno_allocno_map[regno] == a)
+       {
+         ira_regno_allocno_map[regno] = a;
+         ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
+         ALLOCNO_CAP_MEMBER (a) = NULL;
+         COPY_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (a),
+                            ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
 #ifdef STACK_REGS
-               if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
-                 ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a) = true;
+         if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
+           ALLOCNO_NO_STACK_REG_P (a) = true;
 #endif
-               cover_class = ALLOCNO_COVER_CLASS (a);
-               ira_assert (cover_class == ALLOCNO_COVER_CLASS (parent_a));
-               ira_allocate_and_accumulate_costs
-                 (&ALLOCNO_HARD_REG_COSTS (parent_a), cover_class,
-                  ALLOCNO_HARD_REG_COSTS (a));
-               ira_allocate_and_accumulate_costs
-                 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
-                  cover_class,
-                  ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
-               ALLOCNO_COVER_CLASS_COST (parent_a)
-                 += ALLOCNO_COVER_CLASS_COST (a);
-               ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
-               ALLOCNO_UPDATED_MEMORY_COST (parent_a)
-                 += ALLOCNO_UPDATED_MEMORY_COST (a);
-               finish_allocno (a);
-             }
-         }
-      }
+       }
+      else
+       finish_allocno (a);
+    }
   if (merged_p)
     ira_rebuild_start_finish_chains ();
 }
 
-/* Remove loops from consideration.  We remove loops for which a
-   separate allocation will not improve the result.  We have to do
-   this after allocno creation and their costs and cover class
-   evaluation because only after that the register pressure can be
-   known and is calculated.  */
+/* Remove loops from consideration.  We remove all loops except for
+   root if ALL_P or loops for which a separate allocation will not
+   improve the result.  We have to do this after allocno creation and
+   their costs and cover class evaluation because only after that the
+   register pressure can be known and is calculated.  */
 static void
-remove_unnecessary_regions (void)
+remove_unnecessary_regions (bool all_p)
 {
+  if (all_p)
+    mark_all_loops_for_removal ();
+  else
+    mark_loops_for_removal ();
   children_vec
     = VEC_alloc (ira_loop_tree_node_t, heap,
                 last_basic_block + VEC_length (loop_p, ira_loops.larray));
@@ -1815,7 +2092,10 @@ remove_unnecessary_regions (void)
                 last_basic_block + VEC_length (loop_p, ira_loops.larray));
   remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root) ;
   VEC_free (ira_loop_tree_node_t, heap, children_vec);
-  remove_unnecessary_allocnos ();
+  if (all_p)
+    remove_low_level_allocnos ();
+  else
+    remove_unnecessary_allocnos ();
   while (VEC_length (ira_loop_tree_node_t, removed_loop_vec) > 0)
     finish_loop_tree_node (VEC_pop (ira_loop_tree_node_t, removed_loop_vec));
   VEC_free (ira_loop_tree_node_t, heap, removed_loop_vec);
@@ -1823,6 +2103,69 @@ remove_unnecessary_regions (void)
 
 \f
 
+/* At this point true value of allocno attribute bad_spill_p means
+   that there is an insn where allocno occurs and where the allocno
+   can not be used as memory.  The function updates the attribute, now
+   it can be true only for allocnos which can not be used as memory in
+   an insn and in whose live ranges there is other allocno deaths.
+   Spilling allocnos with true value will not improve the code because
+   it will not make other allocnos colorable and additional reloads
+   for the corresponding pseudo will be generated in reload pass for
+   each insn it occurs.
+
+   This is a trick mentioned in one classic article of Chaitin etc
+   which is frequently omitted in other implementations of RA based on
+   graph coloring.  */
+static void
+update_bad_spill_attribute (void)
+{
+  int i;
+  ira_allocno_t a;
+  ira_allocno_iterator ai;
+  allocno_live_range_t r;
+  enum reg_class cover_class;
+  bitmap_head dead_points[N_REG_CLASSES];
+
+  for (i = 0; i < ira_reg_class_cover_size; i++)
+    {
+      cover_class = ira_reg_class_cover[i];
+      bitmap_initialize (&dead_points[cover_class], &reg_obstack);
+    }
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      cover_class = ALLOCNO_COVER_CLASS (a);
+      if (cover_class == NO_REGS)
+       continue;
+      for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
+       bitmap_set_bit (&dead_points[cover_class], r->finish);
+    }
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      cover_class = ALLOCNO_COVER_CLASS (a);
+      if (cover_class == NO_REGS)
+       continue;
+      if (! ALLOCNO_BAD_SPILL_P (a))
+       continue;
+      for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
+       {
+         for (i = r->start + 1; i < r->finish; i++)
+           if (bitmap_bit_p (&dead_points[cover_class], i))
+             break;
+         if (i < r->finish)
+           break;
+       }
+      if (r != NULL)
+       ALLOCNO_BAD_SPILL_P (a) = false;
+    }
+  for (i = 0; i < ira_reg_class_cover_size; i++)
+    {
+      cover_class = ira_reg_class_cover[i];
+      bitmap_clear (&dead_points[cover_class]);
+    }
+}
+
+\f
+
 /* Set up minimal and maximal live range points for allocnos.  */
 static void
 setup_min_max_allocno_live_range_point (void)
@@ -1883,9 +2226,10 @@ setup_min_max_allocno_live_range_point (void)
 }
 
 /* Sort allocnos according to their live ranges.  Allocnos with
-   smaller cover class are put first.  Allocnos with the same cove
-   class are ordered according their start (min).  Allocnos with the
-   same start are ordered according their finish (max).  */
+   smaller cover class are put first unless we use priority coloring.
+   Allocnos with the same cove class are ordered according their start
+   (min).  Allocnos with the same start are ordered according their
+   finish (max).  */
 static int
 allocno_range_compare_func (const void *v1p, const void *v2p)
 {
@@ -1893,7 +2237,8 @@ allocno_range_compare_func (const void *v1p, const void *v2p)
   ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
   ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
 
-  if ((diff = ALLOCNO_COVER_CLASS (a1) - ALLOCNO_COVER_CLASS (a2)) != 0)
+  if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
+      && (diff = ALLOCNO_COVER_CLASS (a1) - ALLOCNO_COVER_CLASS (a2)) != 0)
     return diff;
   if ((diff = ALLOCNO_MIN (a1) - ALLOCNO_MIN (a2)) != 0)
     return diff;
@@ -1928,7 +2273,7 @@ sort_conflict_id_allocno_map (void)
 static void
 setup_min_max_conflict_allocno_ids (void)
 {
-  enum reg_class cover_class;
+  int cover_class;
   int i, j, min, max, start, finish, first_not_finished, filled_area_start;
   int *live_range_min, *last_lived;
   ira_allocno_t a;
@@ -1941,7 +2286,9 @@ setup_min_max_conflict_allocno_ids (void)
       a = ira_conflict_id_allocno_map[i];
       if (a == NULL)
        continue;
-      if (cover_class != ALLOCNO_COVER_CLASS (a))
+      if (cover_class < 0
+         || (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
+             && cover_class != (int) ALLOCNO_COVER_CLASS (a)))
        {
          cover_class = ALLOCNO_COVER_CLASS (a);
          min = i;
@@ -1959,7 +2306,7 @@ setup_min_max_conflict_allocno_ids (void)
                                         [first_not_finished]))
            first_not_finished++;
          min = first_not_finished;
-       }         
+       }
       if (min == i)
        /* We could increase min further in this case but it is good
           enough.  */
@@ -1975,7 +2322,9 @@ setup_min_max_conflict_allocno_ids (void)
       a = ira_conflict_id_allocno_map[i];
       if (a == NULL)
        continue;
-      if (cover_class != ALLOCNO_COVER_CLASS (a))
+      if (cover_class < 0
+         || (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
+             && cover_class != (int) ALLOCNO_COVER_CLASS (a)))
        {
          cover_class = ALLOCNO_COVER_CLASS (a);
          for (j = 0; j < ira_max_point; j++)
@@ -2042,13 +2391,15 @@ create_caps (void)
 static ira_allocno_t *regno_top_level_allocno_map;
 
 /* Process all allocnos originated from pseudo REGNO and copy live
-   ranges from low level allocnos to final allocnos which are
-   destinations of removed stores at a loop exit.  Return true if we
-   copied live ranges.  */
+   ranges, hard reg conflicts, and allocno stack reg attributes from
+   low level allocnos to final allocnos which are destinations of
+   removed stores at a loop exit.  Return true if we copied live
+   ranges.  */
 static bool
-copy_live_ranges_to_removed_store_destinations (int regno)
+copy_info_to_removed_store_destinations (int regno)
 {
-  ira_allocno_t a, parent_a;
+  ira_allocno_t a;
+  ira_allocno_t parent_a = NULL;
   ira_loop_tree_node_t parent;
   allocno_live_range_t r;
   bool merged_p;
@@ -2083,10 +2434,21 @@ copy_live_ranges_to_removed_store_destinations (int regno)
          ira_print_live_range_list (ira_dump_file,
                                     ALLOCNO_LIVE_RANGES (a));
        }
-      r = copy_allocno_live_range_list (ALLOCNO_LIVE_RANGES (a));
+      r = ira_copy_allocno_live_range_list (ALLOCNO_LIVE_RANGES (a));
       change_allocno_in_range_list (r, parent_a);
       ALLOCNO_LIVE_RANGES (parent_a)
-       = merge_ranges (r, ALLOCNO_LIVE_RANGES (parent_a));
+       = ira_merge_allocno_live_ranges (r, ALLOCNO_LIVE_RANGES (parent_a));
+      IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (parent_a),
+                       ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
+#ifdef STACK_REGS
+      if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
+       ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a) = true;
+#endif
+      ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
+      ALLOCNO_CALLS_CROSSED_NUM (parent_a)
+       += ALLOCNO_CALLS_CROSSED_NUM (a);
+      ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
+       += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
       merged_p = true;
     }
   return merged_p;
@@ -2102,7 +2464,7 @@ void
 ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
 {
   int i, j, num;
-  bool propagate_p, stop_p, keep_p;
+  bool keep_p;
   int hard_regs_num;
   bool new_pseudos_p, merged_p, mem_dest_p;
   unsigned int n;
@@ -2114,20 +2476,29 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
   ira_allocno_iterator ai;
   ira_copy_iterator ci;
   sparseset allocnos_live;
-  bool *allocno_propagated_p;
 
   regno_top_level_allocno_map
     = (ira_allocno_t *) ira_allocate (max_reg_num () * sizeof (ira_allocno_t));
   memset (regno_top_level_allocno_map, 0,
          max_reg_num () * sizeof (ira_allocno_t));
-  allocno_propagated_p
-    = (bool *) ira_allocate (ira_allocnos_num * sizeof (bool));
-  memset (allocno_propagated_p, 0, ira_allocnos_num * sizeof (bool));
   new_pseudos_p = merged_p = false;
+  FOR_EACH_ALLOCNO (a, ai)
+    {
+      if (ALLOCNO_CAP_MEMBER (a) != NULL)
+       /* Caps are not in the regno allocno maps and they are never
+          will be transformed into allocnos existing after IR
+          flattening.  */
+       continue;
+      COPY_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a),
+                        ALLOCNO_CONFLICT_HARD_REGS (a));
+#ifdef STACK_REGS
+      ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
+#endif
+    }
   /* Fix final allocno attributes.  */
   for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
     {
-      mem_dest_p = propagate_p = false;
+      mem_dest_p = false;
       for (a = ira_regno_allocno_map[i];
           a != NULL;
           a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
@@ -2145,27 +2516,17 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
              continue;
            }
          ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
+
          if (ALLOCNO_MEM_OPTIMIZED_DEST (a) != NULL)
            mem_dest_p = true;
-         if (propagate_p)
+         if (REGNO (ALLOCNO_REG (a)) == REGNO (ALLOCNO_REG (parent_a)))
            {
-             if (!allocno_propagated_p [ALLOCNO_NUM (parent_a)])
-               COPY_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (parent_a),
-                                  ALLOCNO_CONFLICT_HARD_REGS (parent_a));
              IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (parent_a),
                                ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
 #ifdef STACK_REGS
-             if (!allocno_propagated_p [ALLOCNO_NUM (parent_a)])
-               ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a)
-                 = ALLOCNO_NO_STACK_REG_P (parent_a);
-             ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a)
-               = (ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a)
-                  || ALLOCNO_TOTAL_NO_STACK_REG_P (a));
+             if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
+               ALLOCNO_TOTAL_NO_STACK_REG_P (parent_a) = true;
 #endif
-             allocno_propagated_p [ALLOCNO_NUM (parent_a)] = true;
-           }
-         if (REGNO (ALLOCNO_REG (a)) == REGNO (ALLOCNO_REG (parent_a)))
-           {
              if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
                {
                  fprintf (ira_dump_file,
@@ -2178,8 +2539,8 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
                }
              change_allocno_in_range_list (ALLOCNO_LIVE_RANGES (a), parent_a);
              ALLOCNO_LIVE_RANGES (parent_a)
-               = merge_ranges (ALLOCNO_LIVE_RANGES (a),
-                               ALLOCNO_LIVE_RANGES (parent_a));
+               = ira_merge_allocno_live_ranges
+                 (ALLOCNO_LIVE_RANGES (a), ALLOCNO_LIVE_RANGES (parent_a));
              merged_p = true;
              ALLOCNO_LIVE_RANGES (a) = NULL;
              ALLOCNO_MEM_OPTIMIZED_DEST_P (parent_a)
@@ -2188,27 +2549,15 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
              continue;
            }
          new_pseudos_p = true;
-         propagate_p = true;
-         first = ALLOCNO_MEM_OPTIMIZED_DEST (a) == NULL ? NULL : a;
-         stop_p = false;
          for (;;)
            {
-             if (first == NULL
-                 && ALLOCNO_MEM_OPTIMIZED_DEST (parent_a) != NULL)
-               first = parent_a;
              ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
              ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
-             if (first != NULL
-                 && ALLOCNO_MEM_OPTIMIZED_DEST (first) == parent_a)
-               stop_p = true;
-             else if (!stop_p)
-               {
-                 ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
-                 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
-                   -= ALLOCNO_CALLS_CROSSED_NUM (a);
-                 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
-                   -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
-               }
+             ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
+             ALLOCNO_CALLS_CROSSED_NUM (parent_a)
+               -= ALLOCNO_CALLS_CROSSED_NUM (a);
+             ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
+               -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
              ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
                          && ALLOCNO_NREFS (parent_a) >= 0
                          && ALLOCNO_FREQ (parent_a) >= 0);
@@ -2237,10 +2586,9 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
          ALLOCNO_COPIES (a) = NULL;
          regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))] = a;
        }
-      if (mem_dest_p && copy_live_ranges_to_removed_store_destinations (i))
+      if (mem_dest_p && copy_info_to_removed_store_destinations (i))
        merged_p = true;
     }
-  ira_free (allocno_propagated_p);
   ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
   if (merged_p || ira_max_point_before_emit != ira_max_point)
     ira_rebuild_start_finish_chains ();
@@ -2272,13 +2620,14 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
                {
                  ira_allocno_t live_a = ira_allocnos[n];
 
-                 if (cover_class == ALLOCNO_COVER_CLASS (live_a)
+                 if (ira_reg_classes_intersect_p
+                     [cover_class][ALLOCNO_COVER_CLASS (live_a)]
                      /* Don't set up conflict for the allocno with itself.  */
                      && num != (int) n)
                    ira_add_allocno_conflict (a, live_a);
                }
            }
-         
+
          for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
            sparseset_clear_bit (allocnos_live, ALLOCNO_NUM (r->allocno));
        }
@@ -2349,7 +2698,9 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
       ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
       ALLOCNO_REGNO (a) = REGNO (ALLOCNO_REG (a));
       ALLOCNO_CAP (a) = NULL;
+      /* Restore updated costs for assignments from reload.  */
       ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
+      ALLOCNO_UPDATED_COVER_CLASS_COST (a) = ALLOCNO_COVER_CLASS_COST (a);
       if (! ALLOCNO_ASSIGNED_P (a))
        ira_free_allocno_updated_costs (a);
       ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
@@ -2371,6 +2722,8 @@ ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
       ira_swap_allocno_copy_ends_if_necessary (cp);
     }
   rebuild_regno_allocno_maps ();
+  if (ira_max_point != ira_max_point_before_emit)
+    ira_compress_allocno_live_ranges ();
   ira_free (regno_top_level_allocno_map);
 }
 
@@ -2426,7 +2779,9 @@ ira_build (bool loops_p)
   create_allocnos ();
   ira_costs ();
   ira_create_allocno_live_ranges ();
-  remove_unnecessary_regions ();
+  remove_unnecessary_regions (false);
+  ira_compress_allocno_live_ranges ();
+  update_bad_spill_attribute ();
   loops_p = more_one_region_p ();
   if (loops_p)
     {
@@ -2441,6 +2796,29 @@ ira_build (bool loops_p)
   sort_conflict_id_allocno_map ();
   setup_min_max_conflict_allocno_ids ();
   ira_build_conflicts ();
+  if (! ira_conflicts_p)
+    {
+      ira_allocno_t a;
+      ira_allocno_iterator ai;
+
+      /* Remove all regions but root one.  */
+      if (loops_p)
+       {
+         remove_unnecessary_regions (true);
+         loops_p = false;
+       }
+      /* We don't save hard registers around calls for fast allocation
+        -- add caller clobbered registers as conflicting ones to
+        allocno crossing calls.  */
+      FOR_EACH_ALLOCNO (a, ai)
+       if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
+         {
+           IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a),
+                             call_used_reg_set);
+           IOR_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (a),
+                             call_used_reg_set);
+         }
+    }
   if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
     print_copies (ira_dump_file);
   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)