Fix to expose more LIM when creating mem_ref
[official-gcc.git] / gcc / ira-color.c
blob6aefdd1ae136ea681c9082475c3842adbaa6c5ff
1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "target.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "sbitmap.h"
32 #include "bitmap.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "expr.h"
36 #include "diagnostic-core.h"
37 #include "toplev.h"
38 #include "reload.h"
39 #include "params.h"
40 #include "df.h"
41 #include "splay-tree.h"
42 #include "ira-int.h"
44 /* This file contains code for regional graph coloring, spill/restore
45 code placement optimization, and code helping the reload pass to do
46 a better job. */
48 /* Bitmap of allocnos which should be colored. */
49 static bitmap coloring_allocno_bitmap;
51 /* Bitmap of allocnos which should be taken into account during
52 coloring. In general case it contains allocnos from
53 coloring_allocno_bitmap plus other already colored conflicting
54 allocnos. */
55 static bitmap consideration_allocno_bitmap;
57 /* TRUE if we coalesced some allocnos. In other words, if we got
58 loops formed by members first_coalesced_allocno and
59 next_coalesced_allocno containing more one allocno. */
60 static bool allocno_coalesced_p;
62 /* Bitmap used to prevent a repeated allocno processing because of
63 coalescing. */
64 static bitmap processed_coalesced_allocno_bitmap;
66 /* All allocnos sorted according their priorities. */
67 static ira_allocno_t *sorted_allocnos;
69 /* Vec representing the stack of allocnos used during coloring. */
70 static VEC(ira_allocno_t,heap) *allocno_stack_vec;
72 /* Array used to choose an allocno for spilling. */
73 static ira_allocno_t *allocnos_for_spilling;
75 /* Pool for splay tree nodes. */
76 static alloc_pool splay_tree_node_pool;
78 /* When an allocno is removed from the splay tree, it is put in the
79 following vector for subsequent inserting it into the splay tree
80 after putting all colorable allocnos onto the stack. The allocno
81 could be removed from and inserted to the splay tree every time
82 when its spilling priority is changed but such solution would be
83 more costly although simpler. */
84 static VEC(ira_allocno_t,heap) *removed_splay_allocno_vec;
88 /* This page contains functions used to find conflicts using allocno
89 live ranges. */
91 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
92 used to find a conflict for new allocnos or allocnos with the
93 different cover classes. */
94 static bool
95 allocnos_have_intersected_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
97 int i, j;
98 int n1 = ALLOCNO_NUM_OBJECTS (a1);
99 int n2 = ALLOCNO_NUM_OBJECTS (a2);
101 if (a1 == a2)
102 return false;
103 if (ALLOCNO_REG (a1) != NULL && ALLOCNO_REG (a2) != NULL
104 && (ORIGINAL_REGNO (ALLOCNO_REG (a1))
105 == ORIGINAL_REGNO (ALLOCNO_REG (a2))))
106 return false;
108 for (i = 0; i < n1; i++)
110 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
111 for (j = 0; j < n2; j++)
113 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
114 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
115 OBJECT_LIVE_RANGES (c2)))
116 return true;
119 return false;
122 #ifdef ENABLE_IRA_CHECKING
124 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
125 intersect. This should be used when there is only one region.
126 Currently this is used during reload. */
127 static bool
128 pseudos_have_intersected_live_ranges_p (int regno1, int regno2)
130 ira_allocno_t a1, a2;
132 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
133 && regno2 >= FIRST_PSEUDO_REGISTER);
134 /* Reg info caclulated by dataflow infrastructure can be different
135 from one calculated by regclass. */
136 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
137 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
138 return false;
139 return allocnos_have_intersected_live_ranges_p (a1, a2);
142 #endif
146 /* This page contains functions used to choose hard registers for
147 allocnos. */
149 /* Array whose element value is TRUE if the corresponding hard
150 register was already allocated for an allocno. */
151 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
153 /* Describes one element in a queue of allocnos whose costs need to be
154 updated. Each allocno in the queue is known to have a cover class. */
155 struct update_cost_queue_elem
157 /* This element is in the queue iff CHECK == update_cost_check. */
158 int check;
160 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
161 connecting this allocno to the one being allocated. */
162 int divisor;
164 /* The next allocno in the queue, or null if this is the last element. */
165 ira_allocno_t next;
168 /* The first element in a queue of allocnos whose copy costs need to be
169 updated. Null if the queue is empty. */
170 static ira_allocno_t update_cost_queue;
172 /* The last element in the queue described by update_cost_queue.
173 Not valid if update_cost_queue is null. */
174 static struct update_cost_queue_elem *update_cost_queue_tail;
176 /* A pool of elements in the queue described by update_cost_queue.
177 Elements are indexed by ALLOCNO_NUM. */
178 static struct update_cost_queue_elem *update_cost_queue_elems;
180 /* The current value of update_copy_cost call count. */
181 static int update_cost_check;
183 /* Allocate and initialize data necessary for function
184 update_copy_costs. */
185 static void
186 initiate_cost_update (void)
188 size_t size;
190 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
191 update_cost_queue_elems
192 = (struct update_cost_queue_elem *) ira_allocate (size);
193 memset (update_cost_queue_elems, 0, size);
194 update_cost_check = 0;
197 /* Deallocate data used by function update_copy_costs. */
198 static void
199 finish_cost_update (void)
201 ira_free (update_cost_queue_elems);
204 /* When we traverse allocnos to update hard register costs, the cost
205 divisor will be multiplied by the following macro value for each
206 hop from given allocno to directly connected allocnos. */
207 #define COST_HOP_DIVISOR 4
209 /* Start a new cost-updating pass. */
210 static void
211 start_update_cost (void)
213 update_cost_check++;
214 update_cost_queue = NULL;
217 /* Add (ALLOCNO, DIVISOR) to the end of update_cost_queue,
218 unless ALLOCNO is already in the queue, or has no cover class. */
219 static inline void
220 queue_update_cost (ira_allocno_t allocno, int divisor)
222 struct update_cost_queue_elem *elem;
224 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
225 if (elem->check != update_cost_check
226 && ALLOCNO_COVER_CLASS (allocno) != NO_REGS)
228 elem->check = update_cost_check;
229 elem->divisor = divisor;
230 elem->next = NULL;
231 if (update_cost_queue == NULL)
232 update_cost_queue = allocno;
233 else
234 update_cost_queue_tail->next = allocno;
235 update_cost_queue_tail = elem;
239 /* Try to remove the first element from update_cost_queue. Return false
240 if the queue was empty, otherwise make (*ALLOCNO, *DIVISOR) describe
241 the removed element. */
242 static inline bool
243 get_next_update_cost (ira_allocno_t *allocno, int *divisor)
245 struct update_cost_queue_elem *elem;
247 if (update_cost_queue == NULL)
248 return false;
250 *allocno = update_cost_queue;
251 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
252 *divisor = elem->divisor;
253 update_cost_queue = elem->next;
254 return true;
257 /* Update the cost of allocnos to increase chances to remove some
258 copies as the result of subsequent assignment. */
259 static void
260 update_copy_costs (ira_allocno_t allocno, bool decr_p)
262 int i, cost, update_cost, hard_regno, divisor;
263 enum machine_mode mode;
264 enum reg_class rclass, cover_class;
265 ira_allocno_t another_allocno;
266 ira_copy_t cp, next_cp;
268 hard_regno = ALLOCNO_HARD_REGNO (allocno);
269 ira_assert (hard_regno >= 0);
271 cover_class = ALLOCNO_COVER_CLASS (allocno);
272 if (cover_class == NO_REGS)
273 return;
274 i = ira_class_hard_reg_index[cover_class][hard_regno];
275 ira_assert (i >= 0);
276 rclass = REGNO_REG_CLASS (hard_regno);
278 start_update_cost ();
279 divisor = 1;
282 mode = ALLOCNO_MODE (allocno);
283 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
285 if (cp->first == allocno)
287 next_cp = cp->next_first_allocno_copy;
288 another_allocno = cp->second;
290 else if (cp->second == allocno)
292 next_cp = cp->next_second_allocno_copy;
293 another_allocno = cp->first;
295 else
296 gcc_unreachable ();
298 cover_class = ALLOCNO_COVER_CLASS (another_allocno);
299 if (! ira_reg_classes_intersect_p[rclass][cover_class]
300 || ALLOCNO_ASSIGNED_P (another_allocno))
301 continue;
303 cost = (cp->second == allocno
304 ? ira_get_register_move_cost (mode, rclass, cover_class)
305 : ira_get_register_move_cost (mode, cover_class, rclass));
306 if (decr_p)
307 cost = -cost;
309 update_cost = cp->freq * cost / divisor;
310 if (update_cost == 0)
311 continue;
313 ira_allocate_and_set_or_copy_costs
314 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno), cover_class,
315 ALLOCNO_UPDATED_COVER_CLASS_COST (another_allocno),
316 ALLOCNO_HARD_REG_COSTS (another_allocno));
317 ira_allocate_and_set_or_copy_costs
318 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
319 cover_class, 0,
320 ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
321 i = ira_class_hard_reg_index[cover_class][hard_regno];
322 ira_assert (i >= 0);
323 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno)[i] += update_cost;
324 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno)[i]
325 += update_cost;
327 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
330 while (get_next_update_cost (&allocno, &divisor));
333 /* This function updates COSTS (decrease if DECR_P) for hard_registers
334 of COVER_CLASS by conflict costs of the unassigned allocnos
335 connected by copies with allocnos in update_cost_queue. This
336 update increases chances to remove some copies. */
337 static void
338 update_conflict_hard_regno_costs (int *costs, enum reg_class cover_class,
339 bool decr_p)
341 int i, cost, class_size, freq, mult, div, divisor;
342 int index, hard_regno;
343 int *conflict_costs;
344 bool cont_p;
345 enum reg_class another_cover_class;
346 ira_allocno_t allocno, another_allocno;
347 ira_copy_t cp, next_cp;
349 while (get_next_update_cost (&allocno, &divisor))
350 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
352 if (cp->first == allocno)
354 next_cp = cp->next_first_allocno_copy;
355 another_allocno = cp->second;
357 else if (cp->second == allocno)
359 next_cp = cp->next_second_allocno_copy;
360 another_allocno = cp->first;
362 else
363 gcc_unreachable ();
364 another_cover_class = ALLOCNO_COVER_CLASS (another_allocno);
365 if (! ira_reg_classes_intersect_p[cover_class][another_cover_class]
366 || ALLOCNO_ASSIGNED_P (another_allocno)
367 || ALLOCNO_MAY_BE_SPILLED_P (ALLOCNO_FIRST_COALESCED_ALLOCNO
368 (another_allocno)))
369 continue;
370 class_size = ira_class_hard_regs_num[another_cover_class];
371 ira_allocate_and_copy_costs
372 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
373 another_cover_class,
374 ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
375 conflict_costs
376 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
377 if (conflict_costs == NULL)
378 cont_p = true;
379 else
381 mult = cp->freq;
382 freq = ALLOCNO_FREQ (another_allocno);
383 if (freq == 0)
384 freq = 1;
385 div = freq * divisor;
386 cont_p = false;
387 for (i = class_size - 1; i >= 0; i--)
389 hard_regno = ira_class_hard_regs[another_cover_class][i];
390 ira_assert (hard_regno >= 0);
391 index = ira_class_hard_reg_index[cover_class][hard_regno];
392 if (index < 0)
393 continue;
394 cost = conflict_costs [i] * mult / div;
395 if (cost == 0)
396 continue;
397 cont_p = true;
398 if (decr_p)
399 cost = -cost;
400 costs[index] += cost;
403 /* Probably 5 hops will be enough. */
404 if (cont_p
405 && divisor <= (COST_HOP_DIVISOR
406 * COST_HOP_DIVISOR
407 * COST_HOP_DIVISOR
408 * COST_HOP_DIVISOR))
409 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
413 /* Sort allocnos according to the profit of usage of a hard register
414 instead of memory for them. */
415 static int
416 allocno_cost_compare_func (const void *v1p, const void *v2p)
418 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
419 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
420 int c1, c2;
422 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_COVER_CLASS_COST (p1);
423 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_COVER_CLASS_COST (p2);
424 if (c1 - c2)
425 return c1 - c2;
427 /* If regs are equally good, sort by allocno numbers, so that the
428 results of qsort leave nothing to chance. */
429 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
432 /* Print all allocnos coalesced with ALLOCNO. */
433 static void
434 print_coalesced_allocno (ira_allocno_t allocno)
436 ira_allocno_t a;
438 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
439 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
441 ira_print_expanded_allocno (a);
442 if (a == allocno)
443 break;
444 fprintf (ira_dump_file, "+");
448 /* Choose a hard register for ALLOCNO (or for all coalesced allocnos
449 represented by ALLOCNO). If RETRY_P is TRUE, it means that the
450 function called from function `ira_reassign_conflict_allocnos' and
451 `allocno_reload_assign'. This function implements the optimistic
452 coalescing too: if we failed to assign a hard register to set of
453 the coalesced allocnos, we put them onto the coloring stack for
454 subsequent separate assigning. */
455 static bool
456 assign_hard_reg (ira_allocno_t allocno, bool retry_p)
458 HARD_REG_SET conflicting_regs[2];
459 int i, j, hard_regno, nregs, best_hard_regno, class_size;
460 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords;
461 int *a_costs;
462 enum reg_class cover_class;
463 enum machine_mode mode;
464 ira_allocno_t a;
465 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
466 #ifndef HONOR_REG_ALLOC_ORDER
467 enum reg_class rclass;
468 int add_cost;
469 #endif
470 #ifdef STACK_REGS
471 bool no_stack_reg_p;
472 #endif
474 nwords = ALLOCNO_NUM_OBJECTS (allocno);
475 ira_assert (! ALLOCNO_ASSIGNED_P (allocno));
476 cover_class = ALLOCNO_COVER_CLASS (allocno);
477 class_size = ira_class_hard_regs_num[cover_class];
478 mode = ALLOCNO_MODE (allocno);
479 for (i = 0; i < nwords; i++)
480 CLEAR_HARD_REG_SET (conflicting_regs[i]);
481 best_hard_regno = -1;
482 memset (full_costs, 0, sizeof (int) * class_size);
483 mem_cost = 0;
484 if (allocno_coalesced_p)
485 bitmap_clear (processed_coalesced_allocno_bitmap);
486 memset (costs, 0, sizeof (int) * class_size);
487 memset (full_costs, 0, sizeof (int) * class_size);
488 #ifdef STACK_REGS
489 no_stack_reg_p = false;
490 #endif
491 start_update_cost ();
492 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
493 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
495 int word;
496 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
498 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
499 cover_class, ALLOCNO_HARD_REG_COSTS (a));
500 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
501 #ifdef STACK_REGS
502 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
503 #endif
504 cost = ALLOCNO_UPDATED_COVER_CLASS_COST (a);
505 for (i = 0; i < class_size; i++)
506 if (a_costs != NULL)
508 costs[i] += a_costs[i];
509 full_costs[i] += a_costs[i];
511 else
513 costs[i] += cost;
514 full_costs[i] += cost;
516 for (word = 0; word < nwords; word++)
518 ira_object_t conflict_obj;
519 ira_object_t obj = ALLOCNO_OBJECT (allocno, word);
520 ira_object_conflict_iterator oci;
522 IOR_HARD_REG_SET (conflicting_regs[word],
523 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
524 /* Take preferences of conflicting allocnos into account. */
525 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
527 ira_allocno_t conflict_allocno = OBJECT_ALLOCNO (conflict_obj);
528 enum reg_class conflict_cover_class;
529 /* Reload can give another class so we need to check all
530 allocnos. */
531 if (!retry_p && !bitmap_bit_p (consideration_allocno_bitmap,
532 ALLOCNO_NUM (conflict_allocno)))
533 continue;
534 conflict_cover_class = ALLOCNO_COVER_CLASS (conflict_allocno);
535 ira_assert (ira_reg_classes_intersect_p
536 [cover_class][conflict_cover_class]);
537 if (ALLOCNO_ASSIGNED_P (conflict_allocno))
539 hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno);
540 if (hard_regno >= 0
541 && ira_class_hard_reg_index[cover_class][hard_regno] >= 0)
543 enum machine_mode mode = ALLOCNO_MODE (conflict_allocno);
544 int conflict_nregs = hard_regno_nregs[hard_regno][mode];
545 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_allocno);
546 if (conflict_nregs == n_objects && conflict_nregs > 1)
548 int num = OBJECT_SUBWORD (conflict_obj);
549 if (WORDS_BIG_ENDIAN)
550 SET_HARD_REG_BIT (conflicting_regs[word],
551 hard_regno + n_objects - num - 1);
552 else
553 SET_HARD_REG_BIT (conflicting_regs[word],
554 hard_regno + num);
556 else
557 IOR_HARD_REG_SET (conflicting_regs[word],
558 ira_reg_mode_hard_regset[hard_regno][mode]);
559 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
560 conflicting_regs[word]))
561 goto fail;
564 else if (! ALLOCNO_MAY_BE_SPILLED_P (ALLOCNO_FIRST_COALESCED_ALLOCNO
565 (conflict_allocno)))
567 int k, *conflict_costs;
569 if (allocno_coalesced_p)
571 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
572 ALLOCNO_NUM (conflict_allocno)))
573 continue;
574 bitmap_set_bit (processed_coalesced_allocno_bitmap,
575 ALLOCNO_NUM (conflict_allocno));
578 ira_allocate_and_copy_costs
579 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno),
580 conflict_cover_class,
581 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_allocno));
582 conflict_costs
583 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno);
584 if (conflict_costs != NULL)
585 for (j = class_size - 1; j >= 0; j--)
587 hard_regno = ira_class_hard_regs[cover_class][j];
588 ira_assert (hard_regno >= 0);
589 k = (ira_class_hard_reg_index
590 [conflict_cover_class][hard_regno]);
591 if (k < 0)
592 continue;
593 full_costs[j] -= conflict_costs[k];
595 queue_update_cost (conflict_allocno, COST_HOP_DIVISOR);
599 if (a == allocno)
600 break;
602 /* Take into account preferences of allocnos connected by copies to
603 the conflict allocnos. */
604 update_conflict_hard_regno_costs (full_costs, cover_class, true);
606 /* Take preferences of allocnos connected by copies into
607 account. */
608 start_update_cost ();
609 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
610 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
612 queue_update_cost (a, COST_HOP_DIVISOR);
613 if (a == allocno)
614 break;
616 update_conflict_hard_regno_costs (full_costs, cover_class, false);
617 min_cost = min_full_cost = INT_MAX;
619 /* We don't care about giving callee saved registers to allocnos no
620 living through calls because call clobbered registers are
621 allocated first (it is usual practice to put them first in
622 REG_ALLOC_ORDER). */
623 for (i = 0; i < class_size; i++)
625 hard_regno = ira_class_hard_regs[cover_class][i];
626 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (allocno)];
627 #ifdef STACK_REGS
628 if (no_stack_reg_p
629 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
630 continue;
631 #endif
632 if (TEST_HARD_REG_BIT (prohibited_class_mode_regs[cover_class][mode],
633 hard_regno))
634 continue;
635 for (j = 0; j < nregs; j++)
637 int k;
638 int set_to_test_start = 0, set_to_test_end = nwords;
639 if (nregs == nwords)
641 if (WORDS_BIG_ENDIAN)
642 set_to_test_start = nwords - j - 1;
643 else
644 set_to_test_start = j;
645 set_to_test_end = set_to_test_start + 1;
647 for (k = set_to_test_start; k < set_to_test_end; k++)
648 if (TEST_HARD_REG_BIT (conflicting_regs[k], hard_regno + j))
649 break;
650 if (k != set_to_test_end)
651 break;
653 if (j != nregs)
654 continue;
655 cost = costs[i];
656 full_cost = full_costs[i];
657 #ifndef HONOR_REG_ALLOC_ORDER
658 if (! allocated_hardreg_p[hard_regno]
659 && ira_hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set))
660 /* We need to save/restore the hard register in
661 epilogue/prologue. Therefore we increase the cost. */
663 /* ??? If only part is call clobbered. */
664 rclass = REGNO_REG_CLASS (hard_regno);
665 add_cost = (ira_memory_move_cost[mode][rclass][0]
666 + ira_memory_move_cost[mode][rclass][1] - 1);
667 cost += add_cost;
668 full_cost += add_cost;
670 #endif
671 if (min_cost > cost)
672 min_cost = cost;
673 if (min_full_cost > full_cost)
675 min_full_cost = full_cost;
676 best_hard_regno = hard_regno;
677 ira_assert (hard_regno >= 0);
680 if (min_full_cost > mem_cost)
682 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
683 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
684 mem_cost, min_full_cost);
685 best_hard_regno = -1;
687 fail:
688 if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
689 && best_hard_regno < 0
690 && ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno) != allocno)
692 for (j = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
693 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
695 ira_assert (! ALLOCNO_IN_GRAPH_P (a));
696 sorted_allocnos[j++] = a;
697 if (a == allocno)
698 break;
700 qsort (sorted_allocnos, j, sizeof (ira_allocno_t),
701 allocno_cost_compare_func);
702 for (i = 0; i < j; i++)
704 a = sorted_allocnos[i];
705 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
706 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
707 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, a);
708 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
710 fprintf (ira_dump_file, " Pushing");
711 print_coalesced_allocno (a);
712 fprintf (ira_dump_file, "\n");
715 return false;
717 if (best_hard_regno >= 0)
718 allocated_hardreg_p[best_hard_regno] = true;
719 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
720 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
722 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
723 ALLOCNO_ASSIGNED_P (a) = true;
724 if (best_hard_regno >= 0)
725 update_copy_costs (a, true);
726 ira_assert (ALLOCNO_COVER_CLASS (a) == cover_class);
727 /* We don't need updated costs anymore: */
728 ira_free_allocno_updated_costs (a);
729 if (a == allocno)
730 break;
732 return best_hard_regno >= 0;
737 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
739 /* Bucket of allocnos that can colored currently without spilling. */
740 static ira_allocno_t colorable_allocno_bucket;
742 /* Bucket of allocnos that might be not colored currently without
743 spilling. */
744 static ira_allocno_t uncolorable_allocno_bucket;
746 /* Each element of the array contains the current number of allocnos
747 of given *cover* class in the uncolorable_bucket. */
748 static int uncolorable_allocnos_num[N_REG_CLASSES];
750 /* Return the current spill priority of allocno A. The less the
751 number, the more preferable the allocno for spilling. */
752 static int
753 allocno_spill_priority (ira_allocno_t a)
755 return (ALLOCNO_TEMP (a)
756 / (ALLOCNO_LEFT_CONFLICTS_SIZE (a)
757 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a)][ALLOCNO_MODE (a)]
758 + 1));
761 /* Add ALLOCNO to bucket *BUCKET_PTR. ALLOCNO should be not in a bucket
762 before the call. */
763 static void
764 add_allocno_to_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
766 ira_allocno_t first_allocno;
767 enum reg_class cover_class;
769 if (bucket_ptr == &uncolorable_allocno_bucket
770 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
772 uncolorable_allocnos_num[cover_class]++;
773 ira_assert (uncolorable_allocnos_num[cover_class] > 0);
775 first_allocno = *bucket_ptr;
776 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = first_allocno;
777 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = NULL;
778 if (first_allocno != NULL)
779 ALLOCNO_PREV_BUCKET_ALLOCNO (first_allocno) = allocno;
780 *bucket_ptr = allocno;
783 /* The function returns frequency and number of available hard
784 registers for allocnos coalesced with ALLOCNO. */
785 static void
786 get_coalesced_allocnos_attributes (ira_allocno_t allocno, int *freq, int *num)
788 ira_allocno_t a;
790 *freq = 0;
791 *num = 0;
792 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
793 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
795 *freq += ALLOCNO_FREQ (a);
796 *num += ALLOCNO_AVAILABLE_REGS_NUM (a);
797 if (a == allocno)
798 break;
802 /* Compare two allocnos to define which allocno should be pushed first
803 into the coloring stack. If the return is a negative number, the
804 allocno given by the first parameter will be pushed first. In this
805 case such allocno has less priority than the second one and the
806 hard register will be assigned to it after assignment to the second
807 one. As the result of such assignment order, the second allocno
808 has a better chance to get the best hard register. */
809 static int
810 bucket_allocno_compare_func (const void *v1p, const void *v2p)
812 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
813 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
814 int diff, a1_freq, a2_freq, a1_num, a2_num;
816 if ((diff = (int) ALLOCNO_COVER_CLASS (a2) - ALLOCNO_COVER_CLASS (a1)) != 0)
817 return diff;
818 get_coalesced_allocnos_attributes (a1, &a1_freq, &a1_num);
819 get_coalesced_allocnos_attributes (a2, &a2_freq, &a2_num);
820 if ((diff = a2_num - a1_num) != 0)
821 return diff;
822 else if ((diff = a1_freq - a2_freq) != 0)
823 return diff;
824 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
827 /* Sort bucket *BUCKET_PTR and return the result through
828 BUCKET_PTR. */
829 static void
830 sort_bucket (ira_allocno_t *bucket_ptr)
832 ira_allocno_t a, head;
833 int n;
835 for (n = 0, a = *bucket_ptr; a != NULL; a = ALLOCNO_NEXT_BUCKET_ALLOCNO (a))
836 sorted_allocnos[n++] = a;
837 if (n <= 1)
838 return;
839 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
840 bucket_allocno_compare_func);
841 head = NULL;
842 for (n--; n >= 0; n--)
844 a = sorted_allocnos[n];
845 ALLOCNO_NEXT_BUCKET_ALLOCNO (a) = head;
846 ALLOCNO_PREV_BUCKET_ALLOCNO (a) = NULL;
847 if (head != NULL)
848 ALLOCNO_PREV_BUCKET_ALLOCNO (head) = a;
849 head = a;
851 *bucket_ptr = head;
854 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
855 their priority. ALLOCNO should be not in a bucket before the
856 call. */
857 static void
858 add_allocno_to_ordered_bucket (ira_allocno_t allocno,
859 ira_allocno_t *bucket_ptr)
861 ira_allocno_t before, after;
862 enum reg_class cover_class;
864 if (bucket_ptr == &uncolorable_allocno_bucket
865 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
867 uncolorable_allocnos_num[cover_class]++;
868 ira_assert (uncolorable_allocnos_num[cover_class] > 0);
870 for (before = *bucket_ptr, after = NULL;
871 before != NULL;
872 after = before, before = ALLOCNO_NEXT_BUCKET_ALLOCNO (before))
873 if (bucket_allocno_compare_func (&allocno, &before) < 0)
874 break;
875 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = before;
876 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = after;
877 if (after == NULL)
878 *bucket_ptr = allocno;
879 else
880 ALLOCNO_NEXT_BUCKET_ALLOCNO (after) = allocno;
881 if (before != NULL)
882 ALLOCNO_PREV_BUCKET_ALLOCNO (before) = allocno;
885 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
886 the call. */
887 static void
888 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
890 ira_allocno_t prev_allocno, next_allocno;
891 enum reg_class cover_class;
893 if (bucket_ptr == &uncolorable_allocno_bucket
894 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
896 uncolorable_allocnos_num[cover_class]--;
897 ira_assert (uncolorable_allocnos_num[cover_class] >= 0);
899 prev_allocno = ALLOCNO_PREV_BUCKET_ALLOCNO (allocno);
900 next_allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno);
901 if (prev_allocno != NULL)
902 ALLOCNO_NEXT_BUCKET_ALLOCNO (prev_allocno) = next_allocno;
903 else
905 ira_assert (*bucket_ptr == allocno);
906 *bucket_ptr = next_allocno;
908 if (next_allocno != NULL)
909 ALLOCNO_PREV_BUCKET_ALLOCNO (next_allocno) = prev_allocno;
912 /* Splay tree for each cover class. The trees are indexed by the
913 corresponding cover classes. Splay trees contain uncolorable
914 allocnos. */
915 static splay_tree uncolorable_allocnos_splay_tree[N_REG_CLASSES];
917 /* If the following macro is TRUE, splay tree is used to choose an
918 allocno of the corresponding cover class for spilling. When the
919 number uncolorable allocnos of given cover class decreases to some
920 threshold, linear array search is used to find the best allocno for
921 spilling. This threshold is actually pretty big because, although
922 splay trees asymptotically is much faster, each splay tree
923 operation is sufficiently costly especially taking cache locality
924 into account. */
925 #define USE_SPLAY_P(CLASS) (uncolorable_allocnos_num[CLASS] > 4000)
927 /* Put ALLOCNO onto the coloring stack without removing it from its
928 bucket. Pushing allocno to the coloring stack can result in moving
929 conflicting allocnos from the uncolorable bucket to the colorable
930 one. */
931 static void
932 push_allocno_to_stack (ira_allocno_t allocno)
934 int size;
935 ira_allocno_t a;
936 enum reg_class cover_class;
938 ALLOCNO_IN_GRAPH_P (allocno) = false;
939 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, allocno);
940 cover_class = ALLOCNO_COVER_CLASS (allocno);
941 if (cover_class == NO_REGS)
942 return;
943 size = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)];
944 if (ALLOCNO_NUM_OBJECTS (allocno) > 1)
946 /* We will deal with the subwords individually. */
947 gcc_assert (size == ALLOCNO_NUM_OBJECTS (allocno));
948 size = 1;
950 if (allocno_coalesced_p)
951 bitmap_clear (processed_coalesced_allocno_bitmap);
953 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
954 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
956 int i, n = ALLOCNO_NUM_OBJECTS (a);
957 for (i = 0; i < n; i++)
959 ira_object_t obj = ALLOCNO_OBJECT (a, i);
960 int conflict_size;
961 ira_object_t conflict_obj;
962 ira_object_conflict_iterator oci;
964 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
966 ira_allocno_t conflict_allocno = OBJECT_ALLOCNO (conflict_obj);
967 int left_conflicts_size;
969 conflict_allocno = ALLOCNO_FIRST_COALESCED_ALLOCNO (conflict_allocno);
970 if (!bitmap_bit_p (coloring_allocno_bitmap,
971 ALLOCNO_NUM (conflict_allocno)))
972 continue;
974 ira_assert (cover_class
975 == ALLOCNO_COVER_CLASS (conflict_allocno));
976 if (allocno_coalesced_p)
978 conflict_obj = ALLOCNO_OBJECT (conflict_allocno,
979 OBJECT_SUBWORD (conflict_obj));
980 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
981 OBJECT_CONFLICT_ID (conflict_obj)))
982 continue;
983 bitmap_set_bit (processed_coalesced_allocno_bitmap,
984 OBJECT_CONFLICT_ID (conflict_obj));
987 if (!ALLOCNO_IN_GRAPH_P (conflict_allocno)
988 || ALLOCNO_ASSIGNED_P (conflict_allocno))
989 continue;
991 left_conflicts_size = ALLOCNO_LEFT_CONFLICTS_SIZE (conflict_allocno);
992 conflict_size
993 = (ira_reg_class_nregs
994 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
995 ira_assert (left_conflicts_size >= size);
996 if (left_conflicts_size + conflict_size
997 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
999 ALLOCNO_LEFT_CONFLICTS_SIZE (conflict_allocno) -= size;
1000 continue;
1002 left_conflicts_size -= size;
1003 if (uncolorable_allocnos_splay_tree[cover_class] != NULL
1004 && !ALLOCNO_SPLAY_REMOVED_P (conflict_allocno)
1005 && USE_SPLAY_P (cover_class))
1007 ira_assert
1008 (splay_tree_lookup
1009 (uncolorable_allocnos_splay_tree[cover_class],
1010 (splay_tree_key) conflict_allocno) != NULL);
1011 splay_tree_remove
1012 (uncolorable_allocnos_splay_tree[cover_class],
1013 (splay_tree_key) conflict_allocno);
1014 ALLOCNO_SPLAY_REMOVED_P (conflict_allocno) = true;
1015 VEC_safe_push (ira_allocno_t, heap,
1016 removed_splay_allocno_vec,
1017 conflict_allocno);
1019 ALLOCNO_LEFT_CONFLICTS_SIZE (conflict_allocno)
1020 = left_conflicts_size;
1021 if (left_conflicts_size + conflict_size
1022 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
1024 delete_allocno_from_bucket
1025 (conflict_allocno, &uncolorable_allocno_bucket);
1026 add_allocno_to_ordered_bucket
1027 (conflict_allocno, &colorable_allocno_bucket);
1031 if (a == allocno)
1032 break;
1036 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
1037 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
1038 static void
1039 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
1041 enum reg_class cover_class;
1043 if (colorable_p)
1044 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
1045 else
1046 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
1047 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1049 fprintf (ira_dump_file, " Pushing");
1050 print_coalesced_allocno (allocno);
1051 if (colorable_p)
1052 fprintf (ira_dump_file, "\n");
1053 else
1054 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
1055 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
1056 allocno_spill_priority (allocno), ALLOCNO_TEMP (allocno));
1058 cover_class = ALLOCNO_COVER_CLASS (allocno);
1059 ira_assert ((colorable_p
1060 && (ALLOCNO_LEFT_CONFLICTS_SIZE (allocno)
1061 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
1062 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno)))
1063 || (! colorable_p
1064 && (ALLOCNO_LEFT_CONFLICTS_SIZE (allocno)
1065 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE
1066 (allocno)]
1067 > ALLOCNO_AVAILABLE_REGS_NUM (allocno))));
1068 if (! colorable_p)
1069 ALLOCNO_MAY_BE_SPILLED_P (allocno) = true;
1070 push_allocno_to_stack (allocno);
1073 /* Put all allocnos from colorable bucket onto the coloring stack. */
1074 static void
1075 push_only_colorable (void)
1077 sort_bucket (&colorable_allocno_bucket);
1078 for (;colorable_allocno_bucket != NULL;)
1079 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
1082 /* Puts ALLOCNO chosen for potential spilling onto the coloring
1083 stack. */
1084 static void
1085 push_allocno_to_spill (ira_allocno_t allocno)
1087 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
1088 ALLOCNO_MAY_BE_SPILLED_P (allocno) = true;
1089 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1090 fprintf (ira_dump_file, " Pushing p%d(%d) (spill for NO_REGS)\n",
1091 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1092 push_allocno_to_stack (allocno);
1095 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
1096 loop given by its LOOP_NODE. */
1098 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
1100 int freq, i;
1101 edge_iterator ei;
1102 edge e;
1103 VEC (edge, heap) *edges;
1105 ira_assert (loop_node->loop != NULL
1106 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
1107 freq = 0;
1108 if (! exit_p)
1110 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1111 if (e->src != loop_node->loop->latch
1112 && (regno < 0
1113 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
1114 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
1115 freq += EDGE_FREQUENCY (e);
1117 else
1119 edges = get_loop_exit_edges (loop_node->loop);
1120 for (i = 0; VEC_iterate (edge, edges, i, e); i++)
1121 if (regno < 0
1122 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
1123 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
1124 freq += EDGE_FREQUENCY (e);
1125 VEC_free (edge, heap, edges);
1128 return REG_FREQ_FROM_EDGE_FREQ (freq);
1131 /* Calculate and return the cost of putting allocno A into memory. */
1132 static int
1133 calculate_allocno_spill_cost (ira_allocno_t a)
1135 int regno, cost;
1136 enum machine_mode mode;
1137 enum reg_class rclass;
1138 ira_allocno_t parent_allocno;
1139 ira_loop_tree_node_t parent_node, loop_node;
1141 regno = ALLOCNO_REGNO (a);
1142 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_COVER_CLASS_COST (a);
1143 if (ALLOCNO_CAP (a) != NULL)
1144 return cost;
1145 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
1146 if ((parent_node = loop_node->parent) == NULL)
1147 return cost;
1148 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
1149 return cost;
1150 mode = ALLOCNO_MODE (a);
1151 rclass = ALLOCNO_COVER_CLASS (a);
1152 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
1153 cost -= (ira_memory_move_cost[mode][rclass][0]
1154 * ira_loop_edge_freq (loop_node, regno, true)
1155 + ira_memory_move_cost[mode][rclass][1]
1156 * ira_loop_edge_freq (loop_node, regno, false));
1157 else
1158 cost += ((ira_memory_move_cost[mode][rclass][1]
1159 * ira_loop_edge_freq (loop_node, regno, true)
1160 + ira_memory_move_cost[mode][rclass][0]
1161 * ira_loop_edge_freq (loop_node, regno, false))
1162 - (ira_get_register_move_cost (mode, rclass, rclass)
1163 * (ira_loop_edge_freq (loop_node, regno, false)
1164 + ira_loop_edge_freq (loop_node, regno, true))));
1165 return cost;
1168 /* Compare keys in the splay tree used to choose best allocno for
1169 spilling. The best allocno has the minimal key. */
1170 static int
1171 allocno_spill_priority_compare (splay_tree_key k1, splay_tree_key k2)
1173 int pri1, pri2, diff;
1174 ira_allocno_t a1 = (ira_allocno_t) k1, a2 = (ira_allocno_t) k2;
1176 pri1 = (ALLOCNO_TEMP (a1)
1177 / (ALLOCNO_LEFT_CONFLICTS_SIZE (a1)
1178 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a1)][ALLOCNO_MODE (a1)]
1179 + 1));
1180 pri2 = (ALLOCNO_TEMP (a2)
1181 / (ALLOCNO_LEFT_CONFLICTS_SIZE (a2)
1182 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a2)][ALLOCNO_MODE (a2)]
1183 + 1));
1184 if ((diff = pri1 - pri2) != 0)
1185 return diff;
1186 if ((diff = ALLOCNO_TEMP (a1) - ALLOCNO_TEMP (a2)) != 0)
1187 return diff;
1188 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
1191 /* Allocate data of SIZE for the splay trees. We allocate only spay
1192 tree roots or splay tree nodes. If you change this, please rewrite
1193 the function. */
1194 static void *
1195 splay_tree_allocate (int size, void *data ATTRIBUTE_UNUSED)
1197 if (size != sizeof (struct splay_tree_node_s))
1198 return ira_allocate (size);
1199 return pool_alloc (splay_tree_node_pool);
1202 /* Free data NODE for the splay trees. We allocate and free only spay
1203 tree roots or splay tree nodes. If you change this, please rewrite
1204 the function. */
1205 static void
1206 splay_tree_free (void *node, void *data ATTRIBUTE_UNUSED)
1208 int i;
1209 enum reg_class cover_class;
1211 for (i = 0; i < ira_reg_class_cover_size; i++)
1213 cover_class = ira_reg_class_cover[i];
1214 if (node == uncolorable_allocnos_splay_tree[cover_class])
1216 ira_free (node);
1217 return;
1220 pool_free (splay_tree_node_pool, node);
1223 /* Push allocnos to the coloring stack. The order of allocnos in the
1224 stack defines the order for the subsequent coloring. */
1225 static void
1226 push_allocnos_to_stack (void)
1228 ira_allocno_t allocno, a, i_allocno, *allocno_vec;
1229 enum reg_class cover_class, rclass;
1230 int allocno_pri, i_allocno_pri, allocno_cost, i_allocno_cost;
1231 int i, j, num, cover_class_allocnos_num[N_REG_CLASSES];
1232 ira_allocno_t *cover_class_allocnos[N_REG_CLASSES];
1233 int cost;
1235 /* Initialize. */
1236 VEC_truncate(ira_allocno_t, removed_splay_allocno_vec, 0);
1237 for (i = 0; i < ira_reg_class_cover_size; i++)
1239 cover_class = ira_reg_class_cover[i];
1240 cover_class_allocnos_num[cover_class] = 0;
1241 cover_class_allocnos[cover_class] = NULL;
1242 uncolorable_allocnos_splay_tree[cover_class] = NULL;
1244 /* Calculate uncolorable allocno spill costs. */
1245 for (allocno = uncolorable_allocno_bucket;
1246 allocno != NULL;
1247 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
1248 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
1250 cover_class_allocnos_num[cover_class]++;
1251 cost = 0;
1252 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1253 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1255 cost += calculate_allocno_spill_cost (a);
1256 if (a == allocno)
1257 break;
1259 /* ??? Remove cost of copies between the coalesced
1260 allocnos. */
1261 ALLOCNO_TEMP (allocno) = cost;
1263 /* Define place where to put uncolorable allocnos of the same cover
1264 class. */
1265 for (num = i = 0; i < ira_reg_class_cover_size; i++)
1267 cover_class = ira_reg_class_cover[i];
1268 ira_assert (cover_class_allocnos_num[cover_class]
1269 == uncolorable_allocnos_num[cover_class]);
1270 if (cover_class_allocnos_num[cover_class] != 0)
1272 cover_class_allocnos[cover_class] = allocnos_for_spilling + num;
1273 num += cover_class_allocnos_num[cover_class];
1274 cover_class_allocnos_num[cover_class] = 0;
1276 if (USE_SPLAY_P (cover_class))
1277 uncolorable_allocnos_splay_tree[cover_class]
1278 = splay_tree_new_with_allocator (allocno_spill_priority_compare,
1279 NULL, NULL, splay_tree_allocate,
1280 splay_tree_free, NULL);
1282 ira_assert (num <= ira_allocnos_num);
1283 /* Collect uncolorable allocnos of each cover class. */
1284 for (allocno = uncolorable_allocno_bucket;
1285 allocno != NULL;
1286 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
1287 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
1289 cover_class_allocnos
1290 [cover_class][cover_class_allocnos_num[cover_class]++] = allocno;
1291 if (uncolorable_allocnos_splay_tree[cover_class] != NULL)
1292 splay_tree_insert (uncolorable_allocnos_splay_tree[cover_class],
1293 (splay_tree_key) allocno,
1294 (splay_tree_value) allocno);
1296 for (;;)
1298 push_only_colorable ();
1299 allocno = uncolorable_allocno_bucket;
1300 if (allocno == NULL)
1301 break;
1302 cover_class = ALLOCNO_COVER_CLASS (allocno);
1303 if (cover_class == NO_REGS)
1305 push_allocno_to_spill (allocno);
1306 continue;
1308 /* Potential spilling. */
1309 ira_assert
1310 (ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)] > 0);
1311 if (USE_SPLAY_P (cover_class))
1313 for (;VEC_length (ira_allocno_t, removed_splay_allocno_vec) != 0;)
1315 allocno = VEC_pop (ira_allocno_t, removed_splay_allocno_vec);
1316 ALLOCNO_SPLAY_REMOVED_P (allocno) = false;
1317 rclass = ALLOCNO_COVER_CLASS (allocno);
1318 if (ALLOCNO_LEFT_CONFLICTS_SIZE (allocno)
1319 + ira_reg_class_nregs [rclass][ALLOCNO_MODE (allocno)]
1320 > ALLOCNO_AVAILABLE_REGS_NUM (allocno))
1321 splay_tree_insert
1322 (uncolorable_allocnos_splay_tree[rclass],
1323 (splay_tree_key) allocno, (splay_tree_value) allocno);
1325 allocno = ((ira_allocno_t)
1326 splay_tree_min
1327 (uncolorable_allocnos_splay_tree[cover_class])->key);
1328 splay_tree_remove (uncolorable_allocnos_splay_tree[cover_class],
1329 (splay_tree_key) allocno);
1331 else
1333 num = cover_class_allocnos_num[cover_class];
1334 ira_assert (num > 0);
1335 allocno_vec = cover_class_allocnos[cover_class];
1336 allocno = NULL;
1337 allocno_pri = allocno_cost = 0;
1338 /* Sort uncolorable allocno to find the one with the lowest
1339 spill cost. */
1340 for (i = 0, j = num - 1; i <= j;)
1342 i_allocno = allocno_vec[i];
1343 if (! ALLOCNO_IN_GRAPH_P (i_allocno)
1344 && ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
1346 i_allocno = allocno_vec[j];
1347 allocno_vec[j] = allocno_vec[i];
1348 allocno_vec[i] = i_allocno;
1350 if (ALLOCNO_IN_GRAPH_P (i_allocno))
1352 i++;
1353 ira_assert (ALLOCNO_TEMP (i_allocno) != INT_MAX);
1354 i_allocno_cost = ALLOCNO_TEMP (i_allocno);
1355 i_allocno_pri = allocno_spill_priority (i_allocno);
1356 if (allocno == NULL
1357 || (! ALLOCNO_BAD_SPILL_P (i_allocno)
1358 && ALLOCNO_BAD_SPILL_P (allocno))
1359 || (! (ALLOCNO_BAD_SPILL_P (i_allocno)
1360 && ! ALLOCNO_BAD_SPILL_P (allocno))
1361 && (allocno_pri > i_allocno_pri
1362 || (allocno_pri == i_allocno_pri
1363 && (allocno_cost > i_allocno_cost
1364 || (allocno_cost == i_allocno_cost
1365 && (ALLOCNO_NUM (allocno)
1366 > ALLOCNO_NUM (i_allocno))))))))
1368 allocno = i_allocno;
1369 allocno_cost = i_allocno_cost;
1370 allocno_pri = i_allocno_pri;
1373 if (! ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
1374 j--;
1376 ira_assert (allocno != NULL && j >= 0);
1377 cover_class_allocnos_num[cover_class] = j + 1;
1379 ira_assert (ALLOCNO_IN_GRAPH_P (allocno)
1380 && ALLOCNO_COVER_CLASS (allocno) == cover_class
1381 && (ALLOCNO_LEFT_CONFLICTS_SIZE (allocno)
1382 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE
1383 (allocno)]
1384 > ALLOCNO_AVAILABLE_REGS_NUM (allocno)));
1385 remove_allocno_from_bucket_and_push (allocno, false);
1387 ira_assert (colorable_allocno_bucket == NULL
1388 && uncolorable_allocno_bucket == NULL);
1389 for (i = 0; i < ira_reg_class_cover_size; i++)
1391 cover_class = ira_reg_class_cover[i];
1392 ira_assert (uncolorable_allocnos_num[cover_class] == 0);
1393 if (uncolorable_allocnos_splay_tree[cover_class] != NULL)
1394 splay_tree_delete (uncolorable_allocnos_splay_tree[cover_class]);
1398 /* Pop the coloring stack and assign hard registers to the popped
1399 allocnos. */
1400 static void
1401 pop_allocnos_from_stack (void)
1403 ira_allocno_t allocno;
1404 enum reg_class cover_class;
1406 for (;VEC_length (ira_allocno_t, allocno_stack_vec) != 0;)
1408 allocno = VEC_pop (ira_allocno_t, allocno_stack_vec);
1409 cover_class = ALLOCNO_COVER_CLASS (allocno);
1410 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1412 fprintf (ira_dump_file, " Popping");
1413 print_coalesced_allocno (allocno);
1414 fprintf (ira_dump_file, " -- ");
1416 if (cover_class == NO_REGS)
1418 ALLOCNO_HARD_REGNO (allocno) = -1;
1419 ALLOCNO_ASSIGNED_P (allocno) = true;
1420 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
1421 ira_assert
1422 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
1423 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1424 fprintf (ira_dump_file, "assign memory\n");
1426 else if (assign_hard_reg (allocno, false))
1428 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1429 fprintf (ira_dump_file, "assign reg %d\n",
1430 ALLOCNO_HARD_REGNO (allocno));
1432 else if (ALLOCNO_ASSIGNED_P (allocno))
1434 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1435 fprintf (ira_dump_file, "spill\n");
1437 ALLOCNO_IN_GRAPH_P (allocno) = true;
1441 /* Loop over all coalesced allocnos of ALLOCNO and their subobjects, collecting
1442 total hard register conflicts in PSET (which the caller must initialize). */
1443 static void
1444 all_conflicting_hard_regs_coalesced (ira_allocno_t allocno, HARD_REG_SET *pset)
1446 ira_allocno_t a;
1448 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1449 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1451 int i;
1452 int n = ALLOCNO_NUM_OBJECTS (a);
1453 for (i = 0; i < n; i++)
1455 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1456 IOR_HARD_REG_SET (*pset, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1458 if (a == allocno)
1459 break;
1463 /* Set up number of available hard registers for ALLOCNO. */
1464 static void
1465 setup_allocno_available_regs_num (ira_allocno_t allocno)
1467 int i, n, hard_regs_num, hard_regno;
1468 enum machine_mode mode;
1469 enum reg_class cover_class;
1470 HARD_REG_SET temp_set;
1472 cover_class = ALLOCNO_COVER_CLASS (allocno);
1473 ALLOCNO_AVAILABLE_REGS_NUM (allocno) = ira_available_class_regs[cover_class];
1474 if (cover_class == NO_REGS)
1475 return;
1476 CLEAR_HARD_REG_SET (temp_set);
1477 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1478 hard_regs_num = ira_class_hard_regs_num[cover_class];
1479 all_conflicting_hard_regs_coalesced (allocno, &temp_set);
1481 mode = ALLOCNO_MODE (allocno);
1482 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
1484 hard_regno = ira_class_hard_regs[cover_class][i];
1485 if (TEST_HARD_REG_BIT (temp_set, hard_regno)
1486 || TEST_HARD_REG_BIT (prohibited_class_mode_regs[cover_class][mode],
1487 hard_regno))
1488 n++;
1490 if (internal_flag_ira_verbose > 2 && n > 0 && ira_dump_file != NULL)
1491 fprintf (ira_dump_file, " Reg %d of %s has %d regs less\n",
1492 ALLOCNO_REGNO (allocno), reg_class_names[cover_class], n);
1493 ALLOCNO_AVAILABLE_REGS_NUM (allocno) -= n;
1496 /* Set up ALLOCNO_LEFT_CONFLICTS_SIZE for ALLOCNO. */
1497 static void
1498 setup_allocno_left_conflicts_size (ira_allocno_t allocno)
1500 int i, hard_regs_num, hard_regno, conflict_allocnos_size;
1501 ira_allocno_t a;
1502 enum reg_class cover_class;
1503 HARD_REG_SET temp_set;
1505 cover_class = ALLOCNO_COVER_CLASS (allocno);
1506 hard_regs_num = ira_class_hard_regs_num[cover_class];
1507 CLEAR_HARD_REG_SET (temp_set);
1508 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1509 all_conflicting_hard_regs_coalesced (allocno, &temp_set);
1511 AND_HARD_REG_SET (temp_set, reg_class_contents[cover_class]);
1512 AND_COMPL_HARD_REG_SET (temp_set, ira_no_alloc_regs);
1514 conflict_allocnos_size = 0;
1515 if (! hard_reg_set_empty_p (temp_set))
1516 for (i = 0; i < (int) hard_regs_num; i++)
1518 hard_regno = ira_class_hard_regs[cover_class][i];
1519 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
1521 conflict_allocnos_size++;
1522 CLEAR_HARD_REG_BIT (temp_set, hard_regno);
1523 if (hard_reg_set_empty_p (temp_set))
1524 break;
1527 CLEAR_HARD_REG_SET (temp_set);
1528 if (allocno_coalesced_p)
1529 bitmap_clear (processed_coalesced_allocno_bitmap);
1530 if (cover_class != NO_REGS)
1531 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1532 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1534 int n = ALLOCNO_NUM_OBJECTS (a);
1535 for (i = 0; i < n; i++)
1537 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1538 ira_object_t conflict_obj;
1539 ira_object_conflict_iterator oci;
1541 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1543 ira_allocno_t conflict_allocno = OBJECT_ALLOCNO (conflict_obj);
1545 conflict_allocno
1546 = ALLOCNO_FIRST_COALESCED_ALLOCNO (conflict_allocno);
1547 if (!bitmap_bit_p (consideration_allocno_bitmap,
1548 ALLOCNO_NUM (conflict_allocno)))
1549 continue;
1551 ira_assert (cover_class
1552 == ALLOCNO_COVER_CLASS (conflict_allocno));
1553 if (allocno_coalesced_p)
1555 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
1556 ALLOCNO_NUM (conflict_allocno)))
1557 continue;
1558 bitmap_set_bit (processed_coalesced_allocno_bitmap,
1559 ALLOCNO_NUM (conflict_allocno));
1562 if (! ALLOCNO_ASSIGNED_P (conflict_allocno))
1563 conflict_allocnos_size
1564 += (ira_reg_class_nregs
1565 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
1566 else if ((hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno))
1567 >= 0)
1569 int last = (hard_regno
1570 + hard_regno_nregs
1571 [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
1573 while (hard_regno < last)
1575 if (! TEST_HARD_REG_BIT (temp_set, hard_regno))
1577 conflict_allocnos_size++;
1578 SET_HARD_REG_BIT (temp_set, hard_regno);
1580 hard_regno++;
1585 if (a == allocno)
1586 break;
1588 ALLOCNO_LEFT_CONFLICTS_SIZE (allocno) = conflict_allocnos_size;
1591 /* Put ALLOCNO in a bucket corresponding to its number and size of its
1592 conflicting allocnos and hard registers. */
1593 static void
1594 put_allocno_into_bucket (ira_allocno_t allocno)
1596 enum reg_class cover_class;
1598 cover_class = ALLOCNO_COVER_CLASS (allocno);
1599 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
1600 return;
1601 ALLOCNO_IN_GRAPH_P (allocno) = true;
1602 setup_allocno_left_conflicts_size (allocno);
1603 setup_allocno_available_regs_num (allocno);
1604 if (ALLOCNO_LEFT_CONFLICTS_SIZE (allocno)
1605 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
1606 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno))
1607 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
1608 else
1609 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
1612 /* The function is used to sort allocnos according to their execution
1613 frequencies. */
1614 static int
1615 copy_freq_compare_func (const void *v1p, const void *v2p)
1617 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
1618 int pri1, pri2;
1620 pri1 = cp1->freq;
1621 pri2 = cp2->freq;
1622 if (pri2 - pri1)
1623 return pri2 - pri1;
1625 /* If freqencies are equal, sort by copies, so that the results of
1626 qsort leave nothing to chance. */
1627 return cp1->num - cp2->num;
1630 /* Merge two sets of coalesced allocnos given correspondingly by
1631 allocnos A1 and A2 (more accurately merging A2 set into A1
1632 set). */
1633 static void
1634 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
1636 ira_allocno_t a, first, last, next;
1638 first = ALLOCNO_FIRST_COALESCED_ALLOCNO (a1);
1639 if (first == ALLOCNO_FIRST_COALESCED_ALLOCNO (a2))
1640 return;
1641 for (last = a2, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1642 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1644 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = first;
1645 if (a == a2)
1646 break;
1647 last = a;
1649 next = ALLOCNO_NEXT_COALESCED_ALLOCNO (first);
1650 ALLOCNO_NEXT_COALESCED_ALLOCNO (first) = a2;
1651 ALLOCNO_NEXT_COALESCED_ALLOCNO (last) = next;
1654 /* Given two sets of coalesced sets of allocnos, A1 and A2, this
1655 function determines if any conflicts exist between the two sets.
1656 If RELOAD_P is TRUE, we use live ranges to find conflicts because
1657 conflicts are represented only for allocnos of the same cover class
1658 and during the reload pass we coalesce allocnos for sharing stack
1659 memory slots. */
1660 static bool
1661 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2,
1662 bool reload_p)
1664 ira_allocno_t a, conflict_allocno;
1666 /* When testing for conflicts, it is sufficient to examine only the
1667 subobjects of order 0, due to the canonicalization of conflicts
1668 we do in record_object_conflict. */
1670 bitmap_clear (processed_coalesced_allocno_bitmap);
1671 if (allocno_coalesced_p)
1673 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1674 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1676 bitmap_set_bit (processed_coalesced_allocno_bitmap,
1677 OBJECT_CONFLICT_ID (ALLOCNO_OBJECT (a, 0)));
1678 if (a == a1)
1679 break;
1682 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1683 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1685 if (reload_p)
1687 for (conflict_allocno = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1688 conflict_allocno
1689 = ALLOCNO_NEXT_COALESCED_ALLOCNO (conflict_allocno))
1691 if (allocnos_have_intersected_live_ranges_p (a,
1692 conflict_allocno))
1693 return true;
1694 if (conflict_allocno == a1)
1695 break;
1698 else
1700 ira_object_t a_obj = ALLOCNO_OBJECT (a, 0);
1701 ira_object_t conflict_obj;
1702 ira_object_conflict_iterator oci;
1703 FOR_EACH_OBJECT_CONFLICT (a_obj, conflict_obj, oci)
1704 if (conflict_obj == ALLOCNO_OBJECT (a1, 0)
1705 || (allocno_coalesced_p
1706 && bitmap_bit_p (processed_coalesced_allocno_bitmap,
1707 OBJECT_CONFLICT_ID (conflict_obj))))
1708 return true;
1711 if (a == a2)
1712 break;
1714 return false;
1717 /* The major function for aggressive allocno coalescing. For the
1718 reload pass (RELOAD_P) we coalesce only spilled allocnos. If some
1719 allocnos have been coalesced, we set up flag
1720 allocno_coalesced_p. */
1721 static void
1722 coalesce_allocnos (bool reload_p)
1724 ira_allocno_t a;
1725 ira_copy_t cp, next_cp, *sorted_copies;
1726 enum reg_class cover_class;
1727 enum machine_mode mode;
1728 unsigned int j;
1729 int i, n, cp_num, regno;
1730 bitmap_iterator bi;
1732 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
1733 * sizeof (ira_copy_t));
1734 cp_num = 0;
1735 /* Collect copies. */
1736 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
1738 a = ira_allocnos[j];
1739 regno = ALLOCNO_REGNO (a);
1740 if ((! reload_p && ALLOCNO_ASSIGNED_P (a))
1741 || (reload_p
1742 && (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
1743 || (regno < ira_reg_equiv_len
1744 && (ira_reg_equiv_const[regno] != NULL_RTX
1745 || ira_reg_equiv_invariant_p[regno])))))
1746 continue;
1747 cover_class = ALLOCNO_COVER_CLASS (a);
1748 mode = ALLOCNO_MODE (a);
1749 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1751 if (cp->first == a)
1753 next_cp = cp->next_first_allocno_copy;
1754 regno = ALLOCNO_REGNO (cp->second);
1755 /* For priority coloring we coalesce allocnos only with
1756 the same cover class not with intersected cover
1757 classes as it were possible. It is done for
1758 simplicity. */
1759 if ((reload_p
1760 || (ALLOCNO_COVER_CLASS (cp->second) == cover_class
1761 && ALLOCNO_MODE (cp->second) == mode))
1762 && (cp->insn != NULL || cp->constraint_p)
1763 && ((! reload_p && ! ALLOCNO_ASSIGNED_P (cp->second))
1764 || (reload_p
1765 && ALLOCNO_ASSIGNED_P (cp->second)
1766 && ALLOCNO_HARD_REGNO (cp->second) < 0
1767 && (regno >= ira_reg_equiv_len
1768 || (! ira_reg_equiv_invariant_p[regno]
1769 && ira_reg_equiv_const[regno] == NULL_RTX)))))
1770 sorted_copies[cp_num++] = cp;
1772 else if (cp->second == a)
1773 next_cp = cp->next_second_allocno_copy;
1774 else
1775 gcc_unreachable ();
1778 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
1779 /* Coalesced copies, most frequently executed first. */
1780 for (; cp_num != 0;)
1782 for (i = 0; i < cp_num; i++)
1784 cp = sorted_copies[i];
1785 if (! coalesced_allocno_conflict_p (cp->first, cp->second, reload_p))
1787 allocno_coalesced_p = true;
1788 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1789 fprintf
1790 (ira_dump_file,
1791 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
1792 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1793 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
1794 cp->freq);
1795 merge_allocnos (cp->first, cp->second);
1796 i++;
1797 break;
1800 /* Collect the rest of copies. */
1801 for (n = 0; i < cp_num; i++)
1803 cp = sorted_copies[i];
1804 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->first)
1805 != ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->second))
1806 sorted_copies[n++] = cp;
1808 cp_num = n;
1810 ira_free (sorted_copies);
1813 /* Map: allocno number -> allocno priority. */
1814 static int *allocno_priorities;
1816 /* Set up priorities for N allocnos in array
1817 CONSIDERATION_ALLOCNOS. */
1818 static void
1819 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
1821 int i, length, nrefs, priority, max_priority, mult;
1822 ira_allocno_t a;
1824 max_priority = 0;
1825 for (i = 0; i < n; i++)
1827 a = consideration_allocnos[i];
1828 nrefs = ALLOCNO_NREFS (a);
1829 ira_assert (nrefs >= 0);
1830 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
1831 ira_assert (mult >= 0);
1832 allocno_priorities[ALLOCNO_NUM (a)]
1833 = priority
1834 = (mult
1835 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a))
1836 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a)][ALLOCNO_MODE (a)]);
1837 if (priority < 0)
1838 priority = -priority;
1839 if (max_priority < priority)
1840 max_priority = priority;
1842 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
1843 for (i = 0; i < n; i++)
1845 a = consideration_allocnos[i];
1846 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
1847 if (ALLOCNO_NUM_OBJECTS (a) > 1)
1848 length /= ALLOCNO_NUM_OBJECTS (a);
1849 if (length <= 0)
1850 length = 1;
1851 allocno_priorities[ALLOCNO_NUM (a)]
1852 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
1856 /* Sort allocnos according to their priorities which are calculated
1857 analogous to ones in file `global.c'. */
1858 static int
1859 allocno_priority_compare_func (const void *v1p, const void *v2p)
1861 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
1862 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
1863 int pri1, pri2;
1865 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
1866 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
1867 if (pri2 - pri1)
1868 return pri2 - pri1;
1870 /* If regs are equally good, sort by allocnos, so that the results of
1871 qsort leave nothing to chance. */
1872 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
1875 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
1876 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
1877 static void
1878 color_allocnos (void)
1880 unsigned int i, n;
1881 bitmap_iterator bi;
1882 ira_allocno_t a;
1884 allocno_coalesced_p = false;
1885 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
1886 if (flag_ira_coalesce)
1887 coalesce_allocnos (false);
1888 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
1890 n = 0;
1891 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1893 a = ira_allocnos[i];
1894 if (ALLOCNO_COVER_CLASS (a) == NO_REGS)
1896 ALLOCNO_HARD_REGNO (a) = -1;
1897 ALLOCNO_ASSIGNED_P (a) = true;
1898 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1899 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1900 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1902 fprintf (ira_dump_file, " Spill");
1903 print_coalesced_allocno (a);
1904 fprintf (ira_dump_file, "\n");
1906 continue;
1908 sorted_allocnos[n++] = a;
1910 if (n != 0)
1912 setup_allocno_priorities (sorted_allocnos, n);
1913 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
1914 allocno_priority_compare_func);
1915 for (i = 0; i < n; i++)
1917 a = sorted_allocnos[i];
1918 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1920 fprintf (ira_dump_file, " ");
1921 print_coalesced_allocno (a);
1922 fprintf (ira_dump_file, " -- ");
1924 if (assign_hard_reg (a, false))
1926 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1927 fprintf (ira_dump_file, "assign hard reg %d\n",
1928 ALLOCNO_HARD_REGNO (a));
1930 else
1932 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1933 fprintf (ira_dump_file, "assign memory\n");
1938 else
1940 /* Put the allocnos into the corresponding buckets. */
1941 colorable_allocno_bucket = NULL;
1942 uncolorable_allocno_bucket = NULL;
1943 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1945 a = ira_allocnos[i];
1946 if (ALLOCNO_COVER_CLASS (a) == NO_REGS)
1948 ALLOCNO_HARD_REGNO (a) = -1;
1949 ALLOCNO_ASSIGNED_P (a) = true;
1950 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1951 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1952 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1954 fprintf (ira_dump_file, " Spill");
1955 print_coalesced_allocno (a);
1956 fprintf (ira_dump_file, "\n");
1958 continue;
1960 put_allocno_into_bucket (a);
1962 push_allocnos_to_stack ();
1963 pop_allocnos_from_stack ();
1965 if (flag_ira_coalesce)
1966 /* We don't need coalesced allocnos for ira_reassign_pseudos. */
1967 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1969 a = ira_allocnos[i];
1970 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
1971 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
1973 ira_free_bitmap (processed_coalesced_allocno_bitmap);
1974 allocno_coalesced_p = false;
1979 /* Output information about the loop given by its LOOP_TREE_NODE. */
1980 static void
1981 print_loop_title (ira_loop_tree_node_t loop_tree_node)
1983 unsigned int j;
1984 bitmap_iterator bi;
1985 ira_loop_tree_node_t subloop_node, dest_loop_node;
1986 edge e;
1987 edge_iterator ei;
1989 ira_assert (loop_tree_node->loop != NULL);
1990 fprintf (ira_dump_file,
1991 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
1992 loop_tree_node->loop->num,
1993 (loop_tree_node->parent == NULL
1994 ? -1 : loop_tree_node->parent->loop->num),
1995 loop_tree_node->loop->header->index,
1996 loop_depth (loop_tree_node->loop));
1997 for (subloop_node = loop_tree_node->children;
1998 subloop_node != NULL;
1999 subloop_node = subloop_node->next)
2000 if (subloop_node->bb != NULL)
2002 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
2003 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
2004 if (e->dest != EXIT_BLOCK_PTR
2005 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
2006 != loop_tree_node))
2007 fprintf (ira_dump_file, "(->%d:l%d)",
2008 e->dest->index, dest_loop_node->loop->num);
2010 fprintf (ira_dump_file, "\n all:");
2011 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2012 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2013 fprintf (ira_dump_file, "\n modified regnos:");
2014 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
2015 fprintf (ira_dump_file, " %d", j);
2016 fprintf (ira_dump_file, "\n border:");
2017 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
2018 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2019 fprintf (ira_dump_file, "\n Pressure:");
2020 for (j = 0; (int) j < ira_reg_class_cover_size; j++)
2022 enum reg_class cover_class;
2024 cover_class = ira_reg_class_cover[j];
2025 if (loop_tree_node->reg_pressure[cover_class] == 0)
2026 continue;
2027 fprintf (ira_dump_file, " %s=%d", reg_class_names[cover_class],
2028 loop_tree_node->reg_pressure[cover_class]);
2030 fprintf (ira_dump_file, "\n");
2033 /* Color the allocnos inside loop (in the extreme case it can be all
2034 of the function) given the corresponding LOOP_TREE_NODE. The
2035 function is called for each loop during top-down traverse of the
2036 loop tree. */
2037 static void
2038 color_pass (ira_loop_tree_node_t loop_tree_node)
2040 int regno, hard_regno, index = -1;
2041 int cost, exit_freq, enter_freq;
2042 unsigned int j;
2043 bitmap_iterator bi;
2044 enum machine_mode mode;
2045 enum reg_class rclass, cover_class;
2046 ira_allocno_t a, subloop_allocno;
2047 ira_loop_tree_node_t subloop_node;
2049 ira_assert (loop_tree_node->bb == NULL);
2050 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2051 print_loop_title (loop_tree_node);
2053 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
2054 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
2055 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2057 a = ira_allocnos[j];
2058 if (ALLOCNO_ASSIGNED_P (a))
2059 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
2061 /* Color all mentioned allocnos including transparent ones. */
2062 color_allocnos ();
2063 /* Process caps. They are processed just once. */
2064 if (flag_ira_region == IRA_REGION_MIXED
2065 || flag_ira_region == IRA_REGION_ALL)
2066 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2068 a = ira_allocnos[j];
2069 if (ALLOCNO_CAP_MEMBER (a) == NULL)
2070 continue;
2071 /* Remove from processing in the next loop. */
2072 bitmap_clear_bit (consideration_allocno_bitmap, j);
2073 rclass = ALLOCNO_COVER_CLASS (a);
2074 if (flag_ira_region == IRA_REGION_MIXED
2075 && (loop_tree_node->reg_pressure[rclass]
2076 <= ira_available_class_regs[rclass]))
2078 mode = ALLOCNO_MODE (a);
2079 hard_regno = ALLOCNO_HARD_REGNO (a);
2080 if (hard_regno >= 0)
2082 index = ira_class_hard_reg_index[rclass][hard_regno];
2083 ira_assert (index >= 0);
2085 regno = ALLOCNO_REGNO (a);
2086 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
2087 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
2088 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
2089 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2090 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2091 if (hard_regno >= 0)
2092 update_copy_costs (subloop_allocno, true);
2093 /* We don't need updated costs anymore: */
2094 ira_free_allocno_updated_costs (subloop_allocno);
2097 /* Update costs of the corresponding allocnos (not caps) in the
2098 subloops. */
2099 for (subloop_node = loop_tree_node->subloops;
2100 subloop_node != NULL;
2101 subloop_node = subloop_node->subloop_next)
2103 ira_assert (subloop_node->bb == NULL);
2104 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2106 a = ira_allocnos[j];
2107 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2108 mode = ALLOCNO_MODE (a);
2109 rclass = ALLOCNO_COVER_CLASS (a);
2110 hard_regno = ALLOCNO_HARD_REGNO (a);
2111 /* Use hard register class here. ??? */
2112 if (hard_regno >= 0)
2114 index = ira_class_hard_reg_index[rclass][hard_regno];
2115 ira_assert (index >= 0);
2117 regno = ALLOCNO_REGNO (a);
2118 /* ??? conflict costs */
2119 subloop_allocno = subloop_node->regno_allocno_map[regno];
2120 if (subloop_allocno == NULL
2121 || ALLOCNO_CAP (subloop_allocno) != NULL)
2122 continue;
2123 ira_assert (ALLOCNO_COVER_CLASS (subloop_allocno) == rclass);
2124 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
2125 ALLOCNO_NUM (subloop_allocno)));
2126 if ((flag_ira_region == IRA_REGION_MIXED)
2127 && (loop_tree_node->reg_pressure[rclass]
2128 <= ira_available_class_regs[rclass]))
2130 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2132 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2133 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2134 if (hard_regno >= 0)
2135 update_copy_costs (subloop_allocno, true);
2136 /* We don't need updated costs anymore: */
2137 ira_free_allocno_updated_costs (subloop_allocno);
2139 continue;
2141 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
2142 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
2143 ira_assert (regno < ira_reg_equiv_len);
2144 if (ira_reg_equiv_invariant_p[regno]
2145 || ira_reg_equiv_const[regno] != NULL_RTX)
2147 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2149 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2150 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2151 if (hard_regno >= 0)
2152 update_copy_costs (subloop_allocno, true);
2153 /* We don't need updated costs anymore: */
2154 ira_free_allocno_updated_costs (subloop_allocno);
2157 else if (hard_regno < 0)
2159 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2160 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
2161 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
2163 else
2165 cover_class = ALLOCNO_COVER_CLASS (subloop_allocno);
2166 cost = (ira_get_register_move_cost (mode, rclass, rclass)
2167 * (exit_freq + enter_freq));
2168 ira_allocate_and_set_or_copy_costs
2169 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), cover_class,
2170 ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno),
2171 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
2172 ira_allocate_and_set_or_copy_costs
2173 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
2174 cover_class, 0,
2175 ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
2176 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
2177 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
2178 -= cost;
2179 if (ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno)
2180 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
2181 ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno)
2182 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
2183 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2184 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
2185 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
2191 /* Initialize the common data for coloring and calls functions to do
2192 Chaitin-Briggs and regional coloring. */
2193 static void
2194 do_coloring (void)
2196 coloring_allocno_bitmap = ira_allocate_bitmap ();
2197 allocnos_for_spilling
2198 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2199 * ira_allocnos_num);
2200 splay_tree_node_pool = create_alloc_pool ("splay tree nodes",
2201 sizeof (struct splay_tree_node_s),
2202 100);
2203 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2204 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
2206 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
2208 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2209 ira_print_disposition (ira_dump_file);
2211 free_alloc_pool (splay_tree_node_pool);
2212 ira_free_bitmap (coloring_allocno_bitmap);
2213 ira_free (allocnos_for_spilling);
2218 /* Move spill/restore code, which are to be generated in ira-emit.c,
2219 to less frequent points (if it is profitable) by reassigning some
2220 allocnos (in loop with subloops containing in another loop) to
2221 memory which results in longer live-range where the corresponding
2222 pseudo-registers will be in memory. */
2223 static void
2224 move_spill_restore (void)
2226 int cost, regno, hard_regno, hard_regno2, index;
2227 bool changed_p;
2228 int enter_freq, exit_freq;
2229 enum machine_mode mode;
2230 enum reg_class rclass;
2231 ira_allocno_t a, parent_allocno, subloop_allocno;
2232 ira_loop_tree_node_t parent, loop_node, subloop_node;
2233 ira_allocno_iterator ai;
2235 for (;;)
2237 changed_p = false;
2238 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2239 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
2240 FOR_EACH_ALLOCNO (a, ai)
2242 regno = ALLOCNO_REGNO (a);
2243 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2244 if (ALLOCNO_CAP_MEMBER (a) != NULL
2245 || ALLOCNO_CAP (a) != NULL
2246 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
2247 || loop_node->children == NULL
2248 /* don't do the optimization because it can create
2249 copies and the reload pass can spill the allocno set
2250 by copy although the allocno will not get memory
2251 slot. */
2252 || ira_reg_equiv_invariant_p[regno]
2253 || ira_reg_equiv_const[regno] != NULL_RTX
2254 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
2255 continue;
2256 mode = ALLOCNO_MODE (a);
2257 rclass = ALLOCNO_COVER_CLASS (a);
2258 index = ira_class_hard_reg_index[rclass][hard_regno];
2259 ira_assert (index >= 0);
2260 cost = (ALLOCNO_MEMORY_COST (a)
2261 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
2262 ? ALLOCNO_COVER_CLASS_COST (a)
2263 : ALLOCNO_HARD_REG_COSTS (a)[index]));
2264 for (subloop_node = loop_node->subloops;
2265 subloop_node != NULL;
2266 subloop_node = subloop_node->subloop_next)
2268 ira_assert (subloop_node->bb == NULL);
2269 subloop_allocno = subloop_node->regno_allocno_map[regno];
2270 if (subloop_allocno == NULL)
2271 continue;
2272 ira_assert (rclass == ALLOCNO_COVER_CLASS (subloop_allocno));
2273 /* We have accumulated cost. To get the real cost of
2274 allocno usage in the loop we should subtract costs of
2275 the subloop allocnos. */
2276 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
2277 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
2278 ? ALLOCNO_COVER_CLASS_COST (subloop_allocno)
2279 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
2280 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
2281 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
2282 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
2283 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
2284 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
2285 else
2287 cost
2288 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
2289 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
2290 if (hard_regno2 != hard_regno)
2291 cost -= (ira_get_register_move_cost (mode, rclass, rclass)
2292 * (exit_freq + enter_freq));
2295 if ((parent = loop_node->parent) != NULL
2296 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
2298 ira_assert (rclass == ALLOCNO_COVER_CLASS (parent_allocno));
2299 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
2300 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
2301 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
2302 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
2303 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
2304 else
2306 cost
2307 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
2308 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
2309 if (hard_regno2 != hard_regno)
2310 cost -= (ira_get_register_move_cost (mode, rclass, rclass)
2311 * (exit_freq + enter_freq));
2314 if (cost < 0)
2316 ALLOCNO_HARD_REGNO (a) = -1;
2317 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2319 fprintf
2320 (ira_dump_file,
2321 " Moving spill/restore for a%dr%d up from loop %d",
2322 ALLOCNO_NUM (a), regno, loop_node->loop->num);
2323 fprintf (ira_dump_file, " - profit %d\n", -cost);
2325 changed_p = true;
2328 if (! changed_p)
2329 break;
2335 /* Update current hard reg costs and current conflict hard reg costs
2336 for allocno A. It is done by processing its copies containing
2337 other allocnos already assigned. */
2338 static void
2339 update_curr_costs (ira_allocno_t a)
2341 int i, hard_regno, cost;
2342 enum machine_mode mode;
2343 enum reg_class cover_class, rclass;
2344 ira_allocno_t another_a;
2345 ira_copy_t cp, next_cp;
2347 ira_free_allocno_updated_costs (a);
2348 ira_assert (! ALLOCNO_ASSIGNED_P (a));
2349 cover_class = ALLOCNO_COVER_CLASS (a);
2350 if (cover_class == NO_REGS)
2351 return;
2352 mode = ALLOCNO_MODE (a);
2353 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2355 if (cp->first == a)
2357 next_cp = cp->next_first_allocno_copy;
2358 another_a = cp->second;
2360 else if (cp->second == a)
2362 next_cp = cp->next_second_allocno_copy;
2363 another_a = cp->first;
2365 else
2366 gcc_unreachable ();
2367 if (! ira_reg_classes_intersect_p[cover_class][ALLOCNO_COVER_CLASS
2368 (another_a)]
2369 || ! ALLOCNO_ASSIGNED_P (another_a)
2370 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
2371 continue;
2372 rclass = REGNO_REG_CLASS (hard_regno);
2373 i = ira_class_hard_reg_index[cover_class][hard_regno];
2374 if (i < 0)
2375 continue;
2376 cost = (cp->first == a
2377 ? ira_get_register_move_cost (mode, rclass, cover_class)
2378 : ira_get_register_move_cost (mode, cover_class, rclass));
2379 ira_allocate_and_set_or_copy_costs
2380 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
2381 cover_class, ALLOCNO_COVER_CLASS_COST (a),
2382 ALLOCNO_HARD_REG_COSTS (a));
2383 ira_allocate_and_set_or_copy_costs
2384 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
2385 cover_class, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
2386 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
2387 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
2391 /* Try to assign hard registers to the unassigned allocnos and
2392 allocnos conflicting with them or conflicting with allocnos whose
2393 regno >= START_REGNO. The function is called after ira_flattening,
2394 so more allocnos (including ones created in ira-emit.c) will have a
2395 chance to get a hard register. We use simple assignment algorithm
2396 based on priorities. */
2397 void
2398 ira_reassign_conflict_allocnos (int start_regno)
2400 int i, allocnos_to_color_num;
2401 ira_allocno_t a;
2402 enum reg_class cover_class;
2403 bitmap allocnos_to_color;
2404 ira_allocno_iterator ai;
2406 allocnos_to_color = ira_allocate_bitmap ();
2407 allocnos_to_color_num = 0;
2408 FOR_EACH_ALLOCNO (a, ai)
2410 int n = ALLOCNO_NUM_OBJECTS (a);
2412 if (! ALLOCNO_ASSIGNED_P (a)
2413 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
2415 if (ALLOCNO_COVER_CLASS (a) != NO_REGS)
2416 sorted_allocnos[allocnos_to_color_num++] = a;
2417 else
2419 ALLOCNO_ASSIGNED_P (a) = true;
2420 ALLOCNO_HARD_REGNO (a) = -1;
2421 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2422 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2424 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
2426 if (ALLOCNO_REGNO (a) < start_regno
2427 || (cover_class = ALLOCNO_COVER_CLASS (a)) == NO_REGS)
2428 continue;
2429 for (i = 0; i < n; i++)
2431 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2432 ira_object_t conflict_obj;
2433 ira_object_conflict_iterator oci;
2434 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2436 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2437 ira_assert (ira_reg_classes_intersect_p
2438 [cover_class][ALLOCNO_COVER_CLASS (conflict_a)]);
2439 if (bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
2440 continue;
2441 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a));
2442 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
2446 ira_free_bitmap (allocnos_to_color);
2447 if (allocnos_to_color_num > 1)
2449 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
2450 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
2451 allocno_priority_compare_func);
2453 for (i = 0; i < allocnos_to_color_num; i++)
2455 a = sorted_allocnos[i];
2456 ALLOCNO_ASSIGNED_P (a) = false;
2457 update_curr_costs (a);
2459 for (i = 0; i < allocnos_to_color_num; i++)
2461 a = sorted_allocnos[i];
2462 if (assign_hard_reg (a, true))
2464 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2465 fprintf
2466 (ira_dump_file,
2467 " Secondary allocation: assign hard reg %d to reg %d\n",
2468 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
2475 /* This page contains code to coalesce memory stack slots used by
2476 spilled allocnos. This results in smaller stack frame, better data
2477 locality, and in smaller code for some architectures like
2478 x86/x86_64 where insn size depends on address displacement value.
2479 On the other hand, it can worsen insn scheduling after the RA but
2480 in practice it is less important than smaller stack frames. */
2482 /* Usage cost and order number of coalesced allocno set to which
2483 given pseudo register belongs to. */
2484 static int *regno_coalesced_allocno_cost;
2485 static int *regno_coalesced_allocno_num;
2487 /* Sort pseudos according frequencies of coalesced allocno sets they
2488 belong to (putting most frequently ones first), and according to
2489 coalesced allocno set order numbers. */
2490 static int
2491 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
2493 const int regno1 = *(const int *) v1p;
2494 const int regno2 = *(const int *) v2p;
2495 int diff;
2497 if ((diff = (regno_coalesced_allocno_cost[regno2]
2498 - regno_coalesced_allocno_cost[regno1])) != 0)
2499 return diff;
2500 if ((diff = (regno_coalesced_allocno_num[regno1]
2501 - regno_coalesced_allocno_num[regno2])) != 0)
2502 return diff;
2503 return regno1 - regno2;
2506 /* Widest width in which each pseudo reg is referred to (via subreg).
2507 It is used for sorting pseudo registers. */
2508 static unsigned int *regno_max_ref_width;
2510 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
2511 #ifdef STACK_GROWS_DOWNWARD
2512 # undef STACK_GROWS_DOWNWARD
2513 # define STACK_GROWS_DOWNWARD 1
2514 #else
2515 # define STACK_GROWS_DOWNWARD 0
2516 #endif
2518 /* Sort pseudos according their slot numbers (putting ones with
2519 smaller numbers first, or last when the frame pointer is not
2520 needed). */
2521 static int
2522 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
2524 const int regno1 = *(const int *) v1p;
2525 const int regno2 = *(const int *) v2p;
2526 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
2527 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
2528 int diff, slot_num1, slot_num2;
2529 int total_size1, total_size2;
2531 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
2533 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
2534 return regno1 - regno2;
2535 return 1;
2537 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
2538 return -1;
2539 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
2540 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
2541 if ((diff = slot_num1 - slot_num2) != 0)
2542 return (frame_pointer_needed
2543 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
2544 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1), regno_max_ref_width[regno1]);
2545 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2), regno_max_ref_width[regno2]);
2546 if ((diff = total_size2 - total_size1) != 0)
2547 return diff;
2548 return regno1 - regno2;
2551 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
2552 for coalesced allocno sets containing allocnos with their regnos
2553 given in array PSEUDO_REGNOS of length N. */
2554 static void
2555 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
2557 int i, num, regno, cost;
2558 ira_allocno_t allocno, a;
2560 for (num = i = 0; i < n; i++)
2562 regno = pseudo_regnos[i];
2563 allocno = ira_regno_allocno_map[regno];
2564 if (allocno == NULL)
2566 regno_coalesced_allocno_cost[regno] = 0;
2567 regno_coalesced_allocno_num[regno] = ++num;
2568 continue;
2570 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2571 continue;
2572 num++;
2573 for (cost = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2574 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2576 cost += ALLOCNO_FREQ (a);
2577 if (a == allocno)
2578 break;
2580 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2581 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2583 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
2584 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
2585 if (a == allocno)
2586 break;
2591 /* Collect spilled allocnos representing coalesced allocno sets (the
2592 first coalesced allocno). The collected allocnos are returned
2593 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
2594 number of the collected allocnos. The allocnos are given by their
2595 regnos in array PSEUDO_REGNOS of length N. */
2596 static int
2597 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
2598 ira_allocno_t *spilled_coalesced_allocnos)
2600 int i, num, regno;
2601 ira_allocno_t allocno;
2603 for (num = i = 0; i < n; i++)
2605 regno = pseudo_regnos[i];
2606 allocno = ira_regno_allocno_map[regno];
2607 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
2608 || ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2609 continue;
2610 spilled_coalesced_allocnos[num++] = allocno;
2612 return num;
2615 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
2616 given slot contains live ranges of coalesced allocnos assigned to
2617 given slot. */
2618 static live_range_t *slot_coalesced_allocnos_live_ranges;
2620 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
2621 ranges intersected with live ranges of coalesced allocnos assigned
2622 to slot with number N. */
2623 static bool
2624 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
2626 ira_allocno_t a;
2628 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2629 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2631 int i;
2632 int nr = ALLOCNO_NUM_OBJECTS (a);
2633 for (i = 0; i < nr; i++)
2635 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2636 if (ira_live_ranges_intersect_p (slot_coalesced_allocnos_live_ranges[n],
2637 OBJECT_LIVE_RANGES (obj)))
2638 return true;
2640 if (a == allocno)
2641 break;
2643 return false;
2646 /* Update live ranges of slot to which coalesced allocnos represented
2647 by ALLOCNO were assigned. */
2648 static void
2649 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
2651 int i, n;
2652 ira_allocno_t a;
2653 live_range_t r;
2655 n = ALLOCNO_TEMP (allocno);
2656 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2657 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2659 int nr = ALLOCNO_NUM_OBJECTS (a);
2660 for (i = 0; i < nr; i++)
2662 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2663 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
2664 slot_coalesced_allocnos_live_ranges[n]
2665 = ira_merge_live_ranges
2666 (slot_coalesced_allocnos_live_ranges[n], r);
2668 if (a == allocno)
2669 break;
2673 /* We have coalesced allocnos involving in copies. Coalesce allocnos
2674 further in order to share the same memory stack slot. Allocnos
2675 representing sets of allocnos coalesced before the call are given
2676 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
2677 some allocnos were coalesced in the function. */
2678 static bool
2679 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
2681 int i, j, n, last_coalesced_allocno_num;
2682 ira_allocno_t allocno, a;
2683 bool merged_p = false;
2684 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
2686 slot_coalesced_allocnos_live_ranges
2687 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
2688 memset (slot_coalesced_allocnos_live_ranges, 0,
2689 sizeof (live_range_t) * ira_allocnos_num);
2690 last_coalesced_allocno_num = 0;
2691 /* Coalesce non-conflicting spilled allocnos preferring most
2692 frequently used. */
2693 for (i = 0; i < num; i++)
2695 allocno = spilled_coalesced_allocnos[i];
2696 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2697 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
2698 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
2699 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
2700 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
2701 continue;
2702 for (j = 0; j < i; j++)
2704 a = spilled_coalesced_allocnos[j];
2705 n = ALLOCNO_TEMP (a);
2706 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a
2707 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
2708 && (ALLOCNO_REGNO (a) >= ira_reg_equiv_len
2709 || (! ira_reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
2710 && ira_reg_equiv_const[ALLOCNO_REGNO (a)] == NULL_RTX))
2711 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
2712 break;
2714 if (j >= i)
2716 /* No coalescing: set up number for coalesced allocnos
2717 represented by ALLOCNO. */
2718 ALLOCNO_TEMP (allocno) = last_coalesced_allocno_num++;
2719 setup_slot_coalesced_allocno_live_ranges (allocno);
2721 else
2723 allocno_coalesced_p = true;
2724 merged_p = true;
2725 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2726 fprintf (ira_dump_file,
2727 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
2728 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
2729 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2730 ALLOCNO_TEMP (allocno) = ALLOCNO_TEMP (a);
2731 setup_slot_coalesced_allocno_live_ranges (allocno);
2732 merge_allocnos (a, allocno);
2733 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a);
2736 for (i = 0; i < ira_allocnos_num; i++)
2737 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
2738 ira_free (slot_coalesced_allocnos_live_ranges);
2739 return merged_p;
2742 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
2743 subsequent assigning stack slots to them in the reload pass. To do
2744 this we coalesce spilled allocnos first to decrease the number of
2745 memory-memory move insns. This function is called by the
2746 reload. */
2747 void
2748 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
2749 unsigned int *reg_max_ref_width)
2751 int max_regno = max_reg_num ();
2752 int i, regno, num, slot_num;
2753 ira_allocno_t allocno, a;
2754 ira_allocno_iterator ai;
2755 ira_allocno_t *spilled_coalesced_allocnos;
2757 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
2758 /* Set up allocnos can be coalesced. */
2759 coloring_allocno_bitmap = ira_allocate_bitmap ();
2760 for (i = 0; i < n; i++)
2762 regno = pseudo_regnos[i];
2763 allocno = ira_regno_allocno_map[regno];
2764 if (allocno != NULL)
2765 bitmap_set_bit (coloring_allocno_bitmap,
2766 ALLOCNO_NUM (allocno));
2768 allocno_coalesced_p = false;
2769 coalesce_allocnos (true);
2770 ira_free_bitmap (coloring_allocno_bitmap);
2771 regno_coalesced_allocno_cost
2772 = (int *) ira_allocate (max_regno * sizeof (int));
2773 regno_coalesced_allocno_num
2774 = (int *) ira_allocate (max_regno * sizeof (int));
2775 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
2776 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2777 /* Sort regnos according frequencies of the corresponding coalesced
2778 allocno sets. */
2779 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
2780 spilled_coalesced_allocnos
2781 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
2782 * sizeof (ira_allocno_t));
2783 /* Collect allocnos representing the spilled coalesced allocno
2784 sets. */
2785 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2786 spilled_coalesced_allocnos);
2787 if (flag_ira_share_spill_slots
2788 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
2790 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2791 qsort (pseudo_regnos, n, sizeof (int),
2792 coalesced_pseudo_reg_freq_compare);
2793 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2794 spilled_coalesced_allocnos);
2796 ira_free_bitmap (processed_coalesced_allocno_bitmap);
2797 allocno_coalesced_p = false;
2798 /* Assign stack slot numbers to spilled allocno sets, use smaller
2799 numbers for most frequently used coalesced allocnos. -1 is
2800 reserved for dynamic search of stack slots for pseudos spilled by
2801 the reload. */
2802 slot_num = 1;
2803 for (i = 0; i < num; i++)
2805 allocno = spilled_coalesced_allocnos[i];
2806 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2807 || ALLOCNO_HARD_REGNO (allocno) >= 0
2808 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
2809 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
2810 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
2811 continue;
2812 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2813 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
2814 slot_num++;
2815 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2816 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2818 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
2819 ALLOCNO_HARD_REGNO (a) = -slot_num;
2820 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2821 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
2822 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
2823 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
2824 reg_max_ref_width[ALLOCNO_REGNO (a)]));
2826 if (a == allocno)
2827 break;
2829 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2830 fprintf (ira_dump_file, "\n");
2832 ira_spilled_reg_stack_slots_num = slot_num - 1;
2833 ira_free (spilled_coalesced_allocnos);
2834 /* Sort regnos according the slot numbers. */
2835 regno_max_ref_width = reg_max_ref_width;
2836 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
2837 /* Uncoalesce allocnos which is necessary for (re)assigning during
2838 the reload pass. */
2839 FOR_EACH_ALLOCNO (a, ai)
2841 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
2842 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
2844 ira_free (regno_coalesced_allocno_num);
2845 ira_free (regno_coalesced_allocno_cost);
2850 /* This page contains code used by the reload pass to improve the
2851 final code. */
2853 /* The function is called from reload to mark changes in the
2854 allocation of REGNO made by the reload. Remember that reg_renumber
2855 reflects the change result. */
2856 void
2857 ira_mark_allocation_change (int regno)
2859 ira_allocno_t a = ira_regno_allocno_map[regno];
2860 int old_hard_regno, hard_regno, cost;
2861 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
2863 ira_assert (a != NULL);
2864 hard_regno = reg_renumber[regno];
2865 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
2866 return;
2867 if (old_hard_regno < 0)
2868 cost = -ALLOCNO_MEMORY_COST (a);
2869 else
2871 ira_assert (ira_class_hard_reg_index[cover_class][old_hard_regno] >= 0);
2872 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
2873 ? ALLOCNO_COVER_CLASS_COST (a)
2874 : ALLOCNO_HARD_REG_COSTS (a)
2875 [ira_class_hard_reg_index[cover_class][old_hard_regno]]);
2876 update_copy_costs (a, false);
2878 ira_overall_cost -= cost;
2879 ALLOCNO_HARD_REGNO (a) = hard_regno;
2880 if (hard_regno < 0)
2882 ALLOCNO_HARD_REGNO (a) = -1;
2883 cost += ALLOCNO_MEMORY_COST (a);
2885 else if (ira_class_hard_reg_index[cover_class][hard_regno] >= 0)
2887 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
2888 ? ALLOCNO_COVER_CLASS_COST (a)
2889 : ALLOCNO_HARD_REG_COSTS (a)
2890 [ira_class_hard_reg_index[cover_class][hard_regno]]);
2891 update_copy_costs (a, true);
2893 else
2894 /* Reload changed class of the allocno. */
2895 cost = 0;
2896 ira_overall_cost += cost;
2899 /* This function is called when reload deletes memory-memory move. In
2900 this case we marks that the allocation of the corresponding
2901 allocnos should be not changed in future. Otherwise we risk to get
2902 a wrong code. */
2903 void
2904 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
2906 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
2907 ira_allocno_t src = ira_regno_allocno_map[src_regno];
2909 ira_assert (dst != NULL && src != NULL
2910 && ALLOCNO_HARD_REGNO (dst) < 0
2911 && ALLOCNO_HARD_REGNO (src) < 0);
2912 ALLOCNO_DONT_REASSIGN_P (dst) = true;
2913 ALLOCNO_DONT_REASSIGN_P (src) = true;
2916 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
2917 allocno A and return TRUE in the case of success. */
2918 static bool
2919 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
2921 int hard_regno;
2922 enum reg_class cover_class;
2923 int regno = ALLOCNO_REGNO (a);
2924 HARD_REG_SET saved[2];
2925 int i, n;
2927 n = ALLOCNO_NUM_OBJECTS (a);
2928 for (i = 0; i < n; i++)
2930 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2931 COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
2932 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
2933 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2934 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2935 call_used_reg_set);
2937 ALLOCNO_ASSIGNED_P (a) = false;
2938 cover_class = ALLOCNO_COVER_CLASS (a);
2939 update_curr_costs (a);
2940 assign_hard_reg (a, true);
2941 hard_regno = ALLOCNO_HARD_REGNO (a);
2942 reg_renumber[regno] = hard_regno;
2943 if (hard_regno < 0)
2944 ALLOCNO_HARD_REGNO (a) = -1;
2945 else
2947 ira_assert (ira_class_hard_reg_index[cover_class][hard_regno] >= 0);
2948 ira_overall_cost -= (ALLOCNO_MEMORY_COST (a)
2949 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
2950 ? ALLOCNO_COVER_CLASS_COST (a)
2951 : ALLOCNO_HARD_REG_COSTS (a)
2952 [ira_class_hard_reg_index
2953 [cover_class][hard_regno]]));
2954 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
2955 && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
2956 call_used_reg_set))
2958 ira_assert (flag_caller_saves);
2959 caller_save_needed = 1;
2963 /* If we found a hard register, modify the RTL for the pseudo
2964 register to show the hard register, and mark the pseudo register
2965 live. */
2966 if (reg_renumber[regno] >= 0)
2968 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2969 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
2970 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
2971 mark_home_live (regno);
2973 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2974 fprintf (ira_dump_file, "\n");
2975 for (i = 0; i < n; i++)
2977 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2978 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
2980 return reg_renumber[regno] >= 0;
2983 /* Sort pseudos according their usage frequencies (putting most
2984 frequently ones first). */
2985 static int
2986 pseudo_reg_compare (const void *v1p, const void *v2p)
2988 int regno1 = *(const int *) v1p;
2989 int regno2 = *(const int *) v2p;
2990 int diff;
2992 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
2993 return diff;
2994 return regno1 - regno2;
2997 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
2998 NUM of them) or spilled pseudos conflicting with pseudos in
2999 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
3000 allocation has been changed. The function doesn't use
3001 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
3002 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
3003 is called by the reload pass at the end of each reload
3004 iteration. */
3005 bool
3006 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
3007 HARD_REG_SET bad_spill_regs,
3008 HARD_REG_SET *pseudo_forbidden_regs,
3009 HARD_REG_SET *pseudo_previous_regs,
3010 bitmap spilled)
3012 int i, n, regno;
3013 bool changed_p;
3014 ira_allocno_t a;
3015 HARD_REG_SET forbidden_regs;
3016 bitmap temp = BITMAP_ALLOC (NULL);
3018 /* Add pseudos which conflict with pseudos already in
3019 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
3020 to allocating in two steps as some of the conflicts might have
3021 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
3022 for (i = 0; i < num; i++)
3023 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
3025 for (i = 0, n = num; i < n; i++)
3027 int nr, j;
3028 int regno = spilled_pseudo_regs[i];
3029 bitmap_set_bit (temp, regno);
3031 a = ira_regno_allocno_map[regno];
3032 nr = ALLOCNO_NUM_OBJECTS (a);
3033 for (j = 0; j < nr; j++)
3035 ira_object_t conflict_obj;
3036 ira_object_t obj = ALLOCNO_OBJECT (a, j);
3037 ira_object_conflict_iterator oci;
3039 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3041 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3042 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
3043 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
3044 && ! bitmap_bit_p (temp, ALLOCNO_REGNO (conflict_a)))
3046 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
3047 bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a));
3048 /* ?!? This seems wrong. */
3049 bitmap_set_bit (consideration_allocno_bitmap,
3050 ALLOCNO_NUM (conflict_a));
3056 if (num > 1)
3057 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
3058 changed_p = false;
3059 /* Try to assign hard registers to pseudos from
3060 SPILLED_PSEUDO_REGS. */
3061 for (i = 0; i < num; i++)
3063 regno = spilled_pseudo_regs[i];
3064 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
3065 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
3066 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
3067 gcc_assert (reg_renumber[regno] < 0);
3068 a = ira_regno_allocno_map[regno];
3069 ira_mark_allocation_change (regno);
3070 ira_assert (reg_renumber[regno] < 0);
3071 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3072 fprintf (ira_dump_file,
3073 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
3074 ALLOCNO_MEMORY_COST (a)
3075 - ALLOCNO_COVER_CLASS_COST (a));
3076 allocno_reload_assign (a, forbidden_regs);
3077 if (reg_renumber[regno] >= 0)
3079 CLEAR_REGNO_REG_SET (spilled, regno);
3080 changed_p = true;
3083 BITMAP_FREE (temp);
3084 return changed_p;
3087 /* The function is called by reload and returns already allocated
3088 stack slot (if any) for REGNO with given INHERENT_SIZE and
3089 TOTAL_SIZE. In the case of failure to find a slot which can be
3090 used for REGNO, the function returns NULL. */
3092 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
3093 unsigned int total_size)
3095 unsigned int i;
3096 int slot_num, best_slot_num;
3097 int cost, best_cost;
3098 ira_copy_t cp, next_cp;
3099 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
3100 rtx x;
3101 bitmap_iterator bi;
3102 struct ira_spilled_reg_stack_slot *slot = NULL;
3104 ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
3105 && inherent_size <= total_size
3106 && ALLOCNO_HARD_REGNO (allocno) < 0);
3107 if (! flag_ira_share_spill_slots)
3108 return NULL_RTX;
3109 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
3110 if (slot_num != -1)
3112 slot = &ira_spilled_reg_stack_slots[slot_num];
3113 x = slot->mem;
3115 else
3117 best_cost = best_slot_num = -1;
3118 x = NULL_RTX;
3119 /* It means that the pseudo was spilled in the reload pass, try
3120 to reuse a slot. */
3121 for (slot_num = 0;
3122 slot_num < ira_spilled_reg_stack_slots_num;
3123 slot_num++)
3125 slot = &ira_spilled_reg_stack_slots[slot_num];
3126 if (slot->mem == NULL_RTX)
3127 continue;
3128 if (slot->width < total_size
3129 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
3130 continue;
3132 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
3133 FIRST_PSEUDO_REGISTER, i, bi)
3135 another_allocno = ira_regno_allocno_map[i];
3136 if (allocnos_have_intersected_live_ranges_p (allocno,
3137 another_allocno))
3138 goto cont;
3140 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
3141 cp != NULL;
3142 cp = next_cp)
3144 if (cp->first == allocno)
3146 next_cp = cp->next_first_allocno_copy;
3147 another_allocno = cp->second;
3149 else if (cp->second == allocno)
3151 next_cp = cp->next_second_allocno_copy;
3152 another_allocno = cp->first;
3154 else
3155 gcc_unreachable ();
3156 if (cp->insn == NULL_RTX)
3157 continue;
3158 if (bitmap_bit_p (&slot->spilled_regs,
3159 ALLOCNO_REGNO (another_allocno)))
3160 cost += cp->freq;
3162 if (cost > best_cost)
3164 best_cost = cost;
3165 best_slot_num = slot_num;
3167 cont:
3170 if (best_cost >= 0)
3172 slot_num = best_slot_num;
3173 slot = &ira_spilled_reg_stack_slots[slot_num];
3174 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
3175 x = slot->mem;
3176 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
3179 if (x != NULL_RTX)
3181 ira_assert (slot->width >= total_size);
3182 #ifdef ENABLE_IRA_CHECKING
3183 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
3184 FIRST_PSEUDO_REGISTER, i, bi)
3186 ira_assert (! pseudos_have_intersected_live_ranges_p (regno, i));
3188 #endif
3189 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
3190 if (internal_flag_ira_verbose > 3 && ira_dump_file)
3192 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
3193 regno, REG_FREQ (regno), slot_num);
3194 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
3195 FIRST_PSEUDO_REGISTER, i, bi)
3197 if ((unsigned) regno != i)
3198 fprintf (ira_dump_file, " %d", i);
3200 fprintf (ira_dump_file, "\n");
3203 return x;
3206 /* This is called by reload every time a new stack slot X with
3207 TOTAL_SIZE was allocated for REGNO. We store this info for
3208 subsequent ira_reuse_stack_slot calls. */
3209 void
3210 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
3212 struct ira_spilled_reg_stack_slot *slot;
3213 int slot_num;
3214 ira_allocno_t allocno;
3216 ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
3217 allocno = ira_regno_allocno_map[regno];
3218 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
3219 if (slot_num == -1)
3221 slot_num = ira_spilled_reg_stack_slots_num++;
3222 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
3224 slot = &ira_spilled_reg_stack_slots[slot_num];
3225 INIT_REG_SET (&slot->spilled_regs);
3226 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
3227 slot->mem = x;
3228 slot->width = total_size;
3229 if (internal_flag_ira_verbose > 3 && ira_dump_file)
3230 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
3231 regno, REG_FREQ (regno), slot_num);
3235 /* Return spill cost for pseudo-registers whose numbers are in array
3236 REGNOS (with a negative number as an end marker) for reload with
3237 given IN and OUT for INSN. Return also number points (through
3238 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
3239 the register pressure is high, number of references of the
3240 pseudo-registers (through NREFS), number of callee-clobbered
3241 hard-registers occupied by the pseudo-registers (through
3242 CALL_USED_COUNT), and the first hard regno occupied by the
3243 pseudo-registers (through FIRST_HARD_REGNO). */
3244 static int
3245 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
3246 int *excess_pressure_live_length,
3247 int *nrefs, int *call_used_count, int *first_hard_regno)
3249 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
3250 bool in_p, out_p;
3251 int length;
3252 ira_allocno_t a;
3254 *nrefs = 0;
3255 for (length = count = cost = i = 0;; i++)
3257 regno = regnos[i];
3258 if (regno < 0)
3259 break;
3260 *nrefs += REG_N_REFS (regno);
3261 hard_regno = reg_renumber[regno];
3262 ira_assert (hard_regno >= 0);
3263 a = ira_regno_allocno_map[regno];
3264 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
3265 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a);
3266 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
3267 for (j = 0; j < nregs; j++)
3268 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
3269 break;
3270 if (j == nregs)
3271 count++;
3272 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
3273 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
3274 if ((in_p || out_p)
3275 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
3277 saved_cost = 0;
3278 if (in_p)
3279 saved_cost += ira_memory_move_cost
3280 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][1];
3281 if (out_p)
3282 saved_cost
3283 += ira_memory_move_cost
3284 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][0];
3285 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
3288 *excess_pressure_live_length = length;
3289 *call_used_count = count;
3290 hard_regno = -1;
3291 if (regnos[0] >= 0)
3293 hard_regno = reg_renumber[regnos[0]];
3295 *first_hard_regno = hard_regno;
3296 return cost;
3299 /* Return TRUE if spilling pseudo-registers whose numbers are in array
3300 REGNOS is better than spilling pseudo-registers with numbers in
3301 OTHER_REGNOS for reload with given IN and OUT for INSN. The
3302 function used by the reload pass to make better register spilling
3303 decisions. */
3304 bool
3305 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
3306 rtx in, rtx out, rtx insn)
3308 int cost, other_cost;
3309 int length, other_length;
3310 int nrefs, other_nrefs;
3311 int call_used_count, other_call_used_count;
3312 int hard_regno, other_hard_regno;
3314 cost = calculate_spill_cost (regnos, in, out, insn,
3315 &length, &nrefs, &call_used_count, &hard_regno);
3316 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
3317 &other_length, &other_nrefs,
3318 &other_call_used_count,
3319 &other_hard_regno);
3320 if (nrefs == 0 && other_nrefs != 0)
3321 return true;
3322 if (nrefs != 0 && other_nrefs == 0)
3323 return false;
3324 if (cost != other_cost)
3325 return cost < other_cost;
3326 if (length != other_length)
3327 return length > other_length;
3328 #ifdef REG_ALLOC_ORDER
3329 if (hard_regno >= 0 && other_hard_regno >= 0)
3330 return (inv_reg_alloc_order[hard_regno]
3331 < inv_reg_alloc_order[other_hard_regno]);
3332 #else
3333 if (call_used_count != other_call_used_count)
3334 return call_used_count > other_call_used_count;
3335 #endif
3336 return false;
3341 /* Allocate and initialize data necessary for assign_hard_reg. */
3342 void
3343 ira_initiate_assign (void)
3345 sorted_allocnos
3346 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
3347 * ira_allocnos_num);
3348 consideration_allocno_bitmap = ira_allocate_bitmap ();
3349 initiate_cost_update ();
3350 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
3353 /* Deallocate data used by assign_hard_reg. */
3354 void
3355 ira_finish_assign (void)
3357 ira_free (sorted_allocnos);
3358 ira_free_bitmap (consideration_allocno_bitmap);
3359 finish_cost_update ();
3360 ira_free (allocno_priorities);
3365 /* Entry function doing color-based register allocation. */
3366 static void
3367 color (void)
3369 allocno_stack_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
3370 removed_splay_allocno_vec
3371 = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
3372 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
3373 ira_initiate_assign ();
3374 do_coloring ();
3375 ira_finish_assign ();
3376 VEC_free (ira_allocno_t, heap, removed_splay_allocno_vec);
3377 VEC_free (ira_allocno_t, heap, allocno_stack_vec);
3378 move_spill_restore ();
3383 /* This page contains a simple register allocator without usage of
3384 allocno conflicts. This is used for fast allocation for -O0. */
3386 /* Do register allocation by not using allocno conflicts. It uses
3387 only allocno live ranges. The algorithm is close to Chow's
3388 priority coloring. */
3389 static void
3390 fast_allocation (void)
3392 int i, j, k, num, class_size, hard_regno;
3393 #ifdef STACK_REGS
3394 bool no_stack_reg_p;
3395 #endif
3396 enum reg_class cover_class;
3397 enum machine_mode mode;
3398 ira_allocno_t a;
3399 ira_allocno_iterator ai;
3400 live_range_t r;
3401 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
3403 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
3404 * ira_allocnos_num);
3405 num = 0;
3406 FOR_EACH_ALLOCNO (a, ai)
3407 sorted_allocnos[num++] = a;
3408 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
3409 setup_allocno_priorities (sorted_allocnos, num);
3410 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
3411 * ira_max_point);
3412 for (i = 0; i < ira_max_point; i++)
3413 CLEAR_HARD_REG_SET (used_hard_regs[i]);
3414 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
3415 allocno_priority_compare_func);
3416 for (i = 0; i < num; i++)
3418 int nr, l;
3420 a = sorted_allocnos[i];
3421 nr = ALLOCNO_NUM_OBJECTS (a);
3422 CLEAR_HARD_REG_SET (conflict_hard_regs);
3423 for (l = 0; l < nr; l++)
3425 ira_object_t obj = ALLOCNO_OBJECT (a, l);
3426 IOR_HARD_REG_SET (conflict_hard_regs,
3427 OBJECT_CONFLICT_HARD_REGS (obj));
3428 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3429 for (j = r->start; j <= r->finish; j++)
3430 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
3432 cover_class = ALLOCNO_COVER_CLASS (a);
3433 ALLOCNO_ASSIGNED_P (a) = true;
3434 ALLOCNO_HARD_REGNO (a) = -1;
3435 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
3436 conflict_hard_regs))
3437 continue;
3438 mode = ALLOCNO_MODE (a);
3439 #ifdef STACK_REGS
3440 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
3441 #endif
3442 class_size = ira_class_hard_regs_num[cover_class];
3443 for (j = 0; j < class_size; j++)
3445 hard_regno = ira_class_hard_regs[cover_class][j];
3446 #ifdef STACK_REGS
3447 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
3448 && hard_regno <= LAST_STACK_REG)
3449 continue;
3450 #endif
3451 if (!ira_hard_reg_not_in_set_p (hard_regno, mode, conflict_hard_regs)
3452 || (TEST_HARD_REG_BIT
3453 (prohibited_class_mode_regs[cover_class][mode], hard_regno)))
3454 continue;
3455 ALLOCNO_HARD_REGNO (a) = hard_regno;
3456 for (l = 0; l < nr; l++)
3458 ira_object_t obj = ALLOCNO_OBJECT (a, l);
3459 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3460 for (k = r->start; k <= r->finish; k++)
3461 IOR_HARD_REG_SET (used_hard_regs[k],
3462 ira_reg_mode_hard_regset[hard_regno][mode]);
3464 break;
3467 ira_free (sorted_allocnos);
3468 ira_free (used_hard_regs);
3469 ira_free (allocno_priorities);
3470 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3471 ira_print_disposition (ira_dump_file);
3476 /* Entry function doing coloring. */
3477 void
3478 ira_color (void)
3480 ira_allocno_t a;
3481 ira_allocno_iterator ai;
3483 /* Setup updated costs. */
3484 FOR_EACH_ALLOCNO (a, ai)
3486 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
3487 ALLOCNO_UPDATED_COVER_CLASS_COST (a) = ALLOCNO_COVER_CLASS_COST (a);
3489 if (ira_conflicts_p)
3490 color ();
3491 else
3492 fast_allocation ();