OSDN Git Service

2010-06-07 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / ira-color.c
index 4f1d302..b46801c 100644 (file)
@@ -1,5 +1,5 @@
 /* IRA allocation based on graph coloring.
-   Copyright (C) 2006, 2007, 2008, 2009
+   Copyright (C) 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
 
@@ -285,8 +285,8 @@ update_copy_costs (ira_allocno_t allocno, bool decr_p)
            continue;
 
          cost = (cp->second == allocno
-                 ? ira_register_move_cost[mode][rclass][cover_class]
-                 : ira_register_move_cost[mode][cover_class][rclass]);
+                 ? ira_get_register_move_cost (mode, rclass, cover_class)
+                 : ira_get_register_move_cost (mode, cover_class, rclass));
          if (decr_p)
            cost = -cost;
 
@@ -441,14 +441,18 @@ assign_hard_reg (ira_allocno_t allocno, bool retry_p)
 {
   HARD_REG_SET conflicting_regs;
   int i, j, k, hard_regno, best_hard_regno, class_size;
-  int cost, mem_cost, min_cost, full_cost, min_full_cost, add_cost;
+  int cost, mem_cost, min_cost, full_cost, min_full_cost;
   int *a_costs;
   int *conflict_costs;
-  enum reg_class cover_class, rclass, conflict_cover_class;
+  enum reg_class cover_class, conflict_cover_class;
   enum machine_mode mode;
   ira_allocno_t a, conflict_allocno;
   ira_allocno_conflict_iterator aci;
   static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
+#ifndef HONOR_REG_ALLOC_ORDER
+  enum reg_class rclass;
+  int add_cost;
+#endif
 #ifdef STACK_REGS
   bool no_stack_reg_p;
 #endif
@@ -586,6 +590,7 @@ assign_hard_reg (ira_allocno_t allocno, bool retry_p)
        continue;
       cost = costs[i];
       full_cost = full_costs[i];
+#ifndef HONOR_REG_ALLOC_ORDER
       if (! allocated_hardreg_p[hard_regno]
          && ira_hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set))
        /* We need to save/restore the hard register in
@@ -598,6 +603,7 @@ assign_hard_reg (ira_allocno_t allocno, bool retry_p)
          cost += add_cost;
          full_cost += add_cost;
        }
+#endif
       if (min_cost > cost)
        min_cost = cost;
       if (min_full_cost > full_cost)
@@ -627,7 +633,7 @@ assign_hard_reg (ira_allocno_t allocno, bool retry_p)
          if (a == allocno)
            break;
        }
-      qsort (sorted_allocnos, j, sizeof (ira_allocno_t), 
+      qsort (sorted_allocnos, j, sizeof (ira_allocno_t),
             allocno_cost_compare_func);
       for (i = 0; i < j; i++)
        {
@@ -865,7 +871,7 @@ push_allocno_to_stack (ira_allocno_t allocno)
   ira_allocno_t a, conflict_allocno;
   enum reg_class cover_class;
   ira_allocno_conflict_iterator aci;
-  
+
   ALLOCNO_IN_GRAPH_P (allocno) = false;
   VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, allocno);
   cover_class = ALLOCNO_COVER_CLASS (allocno);
@@ -1005,7 +1011,7 @@ push_allocno_to_spill (ira_allocno_t allocno)
 }
 
 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
-   loop given by its LOOP_NODE.  */ 
+   loop given by its LOOP_NODE.  */
 int
 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
 {
@@ -1071,7 +1077,7 @@ calculate_allocno_spill_cost (ira_allocno_t a)
              * ira_loop_edge_freq (loop_node, regno, true)
              + ira_memory_move_cost[mode][rclass][0]
              * ira_loop_edge_freq (loop_node, regno, false))
-            - (ira_register_move_cost[mode][rclass][rclass]
+            - (ira_get_register_move_cost (mode, rclass, rclass)
                * (ira_loop_edge_freq (loop_node, regno, false)
                   + ira_loop_edge_freq (loop_node, regno, true))));
   return cost;
@@ -1084,7 +1090,7 @@ allocno_spill_priority_compare (splay_tree_key k1, splay_tree_key k2)
 {
   int pri1, pri2, diff;
   ira_allocno_t a1 = (ira_allocno_t) k1, a2 = (ira_allocno_t) k2;
-  
+
   pri1 = (ALLOCNO_TEMP (a1)
          / (ALLOCNO_LEFT_CONFLICTS_SIZE (a1)
             * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a1)][ALLOCNO_MODE (a1)]
@@ -1273,7 +1279,7 @@ push_allocnos_to_stack (void)
                          && (allocno_pri > i_allocno_pri
                              || (allocno_pri == i_allocno_pri
                                  && (allocno_cost > i_allocno_cost
-                                     || (allocno_cost == i_allocno_cost 
+                                     || (allocno_cost == i_allocno_cost
                                          && (ALLOCNO_NUM (allocno)
                                              > ALLOCNO_NUM (i_allocno))))))))
                    {
@@ -1354,7 +1360,8 @@ pop_allocnos_from_stack (void)
 static void
 setup_allocno_available_regs_num (ira_allocno_t allocno)
 {
-  int i, n, hard_regs_num;
+  int i, n, hard_regs_num, hard_regno;
+  enum machine_mode mode;
   enum reg_class cover_class;
   ira_allocno_t a;
   HARD_REG_SET temp_set;
@@ -1373,9 +1380,15 @@ setup_allocno_available_regs_num (ira_allocno_t allocno)
       if (a == allocno)
        break;
     }
+  mode = ALLOCNO_MODE (allocno);
   for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
-    if (TEST_HARD_REG_BIT (temp_set, ira_class_hard_regs[cover_class][i]))
-      n++;
+    {
+      hard_regno = ira_class_hard_regs[cover_class][i];
+      if (TEST_HARD_REG_BIT (temp_set, hard_regno)
+         || TEST_HARD_REG_BIT (prohibited_class_mode_regs[cover_class][mode],
+                               hard_regno))
+       n++;
+    }
   if (internal_flag_ira_verbose > 2 && n > 0 && ira_dump_file != NULL)
     fprintf (ira_dump_file, "    Reg %d of %s has %d regs less\n",
             ALLOCNO_REGNO (allocno), reg_class_names[cover_class], n);
@@ -1452,7 +1465,7 @@ setup_allocno_left_conflicts_size (ira_allocno_t allocno)
                    int last = (hard_regno
                                + hard_regno_nregs
                                [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
-                   
+
                    while (hard_regno < last)
                      {
                        if (! TEST_HARD_REG_BIT (temp_set, hard_regno))
@@ -1476,11 +1489,9 @@ setup_allocno_left_conflicts_size (ira_allocno_t allocno)
 static void
 put_allocno_into_bucket (ira_allocno_t allocno)
 {
-  int hard_regs_num;
   enum reg_class cover_class;
 
   cover_class = ALLOCNO_COVER_CLASS (allocno);
-  hard_regs_num = ira_class_hard_regs_num[cover_class];
   if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
     return;
   ALLOCNO_IN_GRAPH_P (allocno) = true;
@@ -1895,7 +1906,7 @@ print_loop_title (ira_loop_tree_node_t loop_tree_node)
   for (j = 0; (int) j < ira_reg_class_cover_size; j++)
     {
       enum reg_class cover_class;
-      
+
       cover_class = ira_reg_class_cover[j];
       if (loop_tree_node->reg_pressure[cover_class] == 0)
        continue;
@@ -2039,7 +2050,7 @@ color_pass (ira_loop_tree_node_t loop_tree_node)
          else
            {
              cover_class = ALLOCNO_COVER_CLASS (subloop_allocno);
-             cost = (ira_register_move_cost[mode][rclass][rclass] 
+             cost = (ira_get_register_move_cost (mode, rclass, rclass)
                      * (exit_freq + enter_freq));
              ira_allocate_and_set_or_copy_costs
                (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), cover_class,
@@ -2078,7 +2089,7 @@ do_coloring (void)
                                            100);
   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
     fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
-  
+
   ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
 
   if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
@@ -2164,7 +2175,7 @@ move_spill_restore (void)
                    += (ira_memory_move_cost[mode][rclass][0] * exit_freq
                        + ira_memory_move_cost[mode][rclass][1] * enter_freq);
                  if (hard_regno2 != hard_regno)
-                   cost -= (ira_register_move_cost[mode][rclass][rclass]
+                   cost -= (ira_get_register_move_cost (mode, rclass, rclass)
                             * (exit_freq + enter_freq));
                }
            }
@@ -2183,7 +2194,7 @@ move_spill_restore (void)
                    += (ira_memory_move_cost[mode][rclass][1] * exit_freq
                        + ira_memory_move_cost[mode][rclass][0] * enter_freq);
                  if (hard_regno2 != hard_regno)
-                   cost -= (ira_register_move_cost[mode][rclass][rclass]
+                   cost -= (ira_get_register_move_cost (mode, rclass, rclass)
                             * (exit_freq + enter_freq));
                }
            }
@@ -2220,6 +2231,7 @@ update_curr_costs (ira_allocno_t a)
   ira_allocno_t another_a;
   ira_copy_t cp, next_cp;
 
+  ira_free_allocno_updated_costs (a);
   ira_assert (! ALLOCNO_ASSIGNED_P (a));
   cover_class = ALLOCNO_COVER_CLASS (a);
   if (cover_class == NO_REGS)
@@ -2249,8 +2261,8 @@ update_curr_costs (ira_allocno_t a)
       if (i < 0)
        continue;
       cost = (cp->first == a
-             ? ira_register_move_cost[mode][rclass][cover_class]
-             : ira_register_move_cost[mode][cover_class][rclass]);
+             ? ira_get_register_move_cost (mode, rclass, cover_class)
+             : ira_get_register_move_cost (mode, cover_class, rclass));
       ira_allocate_and_set_or_copy_costs
        (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
         cover_class, ALLOCNO_COVER_CLASS_COST (a),
@@ -2321,8 +2333,6 @@ ira_reassign_conflict_allocnos (int start_regno)
     {
       a = sorted_allocnos[i];
       ALLOCNO_ASSIGNED_P (a) = false;
-      ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
-      ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
       update_curr_costs (a);
     }
   for (i = 0; i < allocnos_to_color_num; i++)
@@ -2682,7 +2692,7 @@ ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
                     ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
                     MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
                          reg_max_ref_width[ALLOCNO_REGNO (a)]));
-             
+
          if (a == allocno)
            break;
        }
@@ -2781,13 +2791,13 @@ allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
   int hard_regno;
   enum reg_class cover_class;
   int regno = ALLOCNO_REGNO (a);
+  HARD_REG_SET saved;
 
+  COPY_HARD_REG_SET (saved, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
   IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), forbidden_regs);
   if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
     IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), call_used_reg_set);
   ALLOCNO_ASSIGNED_P (a) = false;
-  ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
-  ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
   cover_class = ALLOCNO_COVER_CLASS (a);
   update_curr_costs (a);
   assign_hard_reg (a, true);
@@ -2825,7 +2835,7 @@ allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
     }
   else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
     fprintf (ira_dump_file, "\n");
-
+  COPY_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), saved);
   return reg_renumber[regno] >= 0;
 }
 
@@ -2855,20 +2865,48 @@ bool
 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
                      HARD_REG_SET bad_spill_regs,
                      HARD_REG_SET *pseudo_forbidden_regs,
-                     HARD_REG_SET *pseudo_previous_regs,  bitmap spilled)
+                     HARD_REG_SET *pseudo_previous_regs,
+                     bitmap spilled)
 {
-  int i, m, n, regno;
+  int i, n, regno;
   bool changed_p;
   ira_allocno_t a, conflict_a;
   HARD_REG_SET forbidden_regs;
   ira_allocno_conflict_iterator aci;
+  bitmap temp = BITMAP_ALLOC (NULL);
+
+  /* Add pseudos which conflict with pseudos already in
+     SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS.  This is preferable
+     to allocating in two steps as some of the conflicts might have
+     a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS.  */
+  for (i = 0; i < num; i++)
+    bitmap_set_bit (temp, spilled_pseudo_regs[i]);
+
+  for (i = 0, n = num; i < n; i++)
+    {
+      int regno = spilled_pseudo_regs[i];
+      bitmap_set_bit (temp, regno);
+
+      a = ira_regno_allocno_map[regno];
+      FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
+       if (ALLOCNO_HARD_REGNO (conflict_a) < 0
+           && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
+           && ! bitmap_bit_p (temp, ALLOCNO_REGNO (conflict_a)))
+         {
+           spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
+           bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a));
+           /* ?!? This seems wrong.  */
+           bitmap_set_bit (consideration_allocno_bitmap,
+                           ALLOCNO_NUM (conflict_a));
+         }
+    }
 
   if (num > 1)
     qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
   changed_p = false;
   /* Try to assign hard registers to pseudos from
      SPILLED_PSEUDO_REGS.  */
-  for (m = i = 0; i < num; i++)
+  for (i = 0; i < num; i++)
     {
       regno = spilled_pseudo_regs[i];
       COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
@@ -2880,7 +2918,7 @@ ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
       ira_assert (reg_renumber[regno] < 0);
       if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
        fprintf (ira_dump_file,
-                "      Spill %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
+                "      Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
                 ALLOCNO_MEMORY_COST (a)
                 - ALLOCNO_COVER_CLASS_COST (a));
       allocno_reload_assign (a, forbidden_regs);
@@ -2889,60 +2927,8 @@ ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
          CLEAR_REGNO_REG_SET (spilled, regno);
          changed_p = true;
        }
-      else
-       spilled_pseudo_regs[m++] = regno;
-    }
-  if (m == 0)
-    return changed_p;
-  if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
-    {
-      fprintf (ira_dump_file, "      Spilled regs");
-      for (i = 0; i < m; i++)
-       fprintf (ira_dump_file, " %d", spilled_pseudo_regs[i]);
-      fprintf (ira_dump_file, "\n");
-    }
-  /* Try to assign hard registers to pseudos conflicting with ones
-     from SPILLED_PSEUDO_REGS.  */
-  for (i = n = 0; i < m; i++)
-    {
-      regno = spilled_pseudo_regs[i];
-      a = ira_regno_allocno_map[regno];
-      FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
-       if (ALLOCNO_HARD_REGNO (conflict_a) < 0
-           && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
-           && ! bitmap_bit_p (consideration_allocno_bitmap,
-                              ALLOCNO_NUM (conflict_a)))
-         {
-           sorted_allocnos[n++] = conflict_a;
-           bitmap_set_bit (consideration_allocno_bitmap,
-                           ALLOCNO_NUM (conflict_a));
-         }
-    }
-  if (n != 0)
-    {
-      setup_allocno_priorities (sorted_allocnos, n);
-      qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
-            allocno_priority_compare_func);
-      for (i = 0; i < n; i++)
-       {
-         a = sorted_allocnos[i];
-         regno = ALLOCNO_REGNO (a);
-         COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
-         IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
-         IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
-         if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
-           fprintf (ira_dump_file,
-                    "        Try assign %d(a%d), cost=%d",
-                    regno, ALLOCNO_NUM (a),
-                    ALLOCNO_MEMORY_COST (a)
-                    - ALLOCNO_COVER_CLASS_COST (a));
-         if (allocno_reload_assign (a, forbidden_regs))
-           {
-             changed_p = true;
-             bitmap_clear_bit (spilled, regno);
-           }
-       }
     }
+  BITMAP_FREE (temp);
   return changed_p;
 }
 
@@ -2990,7 +2976,7 @@ ira_reuse_stack_slot (int regno, unsigned int inherent_size,
          if (slot->width < total_size
              || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
            continue;
-         
+
          EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
                                    FIRST_PSEUDO_REGISTER, i, bi)
            {
@@ -3173,7 +3159,7 @@ ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
   int call_used_count, other_call_used_count;
   int hard_regno, other_hard_regno;
 
-  cost = calculate_spill_cost (regnos, in, out, insn, 
+  cost = calculate_spill_cost (regnos, in, out, insn,
                               &length, &nrefs, &call_used_count, &hard_regno);
   other_cost = calculate_spill_cost (other_regnos, in, out, insn,
                                     &other_length, &other_nrefs,