1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006, 2007, 2008
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
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
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/>. */
24 #include "coretypes.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
40 #include "splay-tree.h"
43 /* This file contains code for regional graph coloring, spill/restore
44 code placement optimization, and code helping the reload pass to do
47 /* Bitmap of allocnos which should be colored. */
48 static bitmap coloring_allocno_bitmap
;
50 /* Bitmap of allocnos which should be taken into account during
51 coloring. In general case it contains allocnos from
52 coloring_allocno_bitmap plus other already colored conflicting
54 static bitmap consideration_allocno_bitmap
;
56 /* TRUE if we coalesced some allocnos. In other words, if we got
57 loops formed by members first_coalesced_allocno and
58 next_coalesced_allocno containing more one allocno. */
59 static bool allocno_coalesced_p
;
61 /* Bitmap used to prevent a repeated allocno processing because of
63 static bitmap processed_coalesced_allocno_bitmap
;
65 /* All allocnos sorted according their priorities. */
66 static ira_allocno_t
*sorted_allocnos
;
68 /* Vec representing the stack of allocnos used during coloring. */
69 static VEC(ira_allocno_t
,heap
) *allocno_stack_vec
;
71 /* Array used to choose an allocno for spilling. */
72 static ira_allocno_t
*allocnos_for_spilling
;
74 /* Pool for splay tree nodes. */
75 static alloc_pool splay_tree_node_pool
;
77 /* When an allocno is removed from the splay tree, it is put in the
78 following vector for subsequent inserting it into the splay tree
79 after putting all colorable allocnos onto the stack. The allocno
80 could be removed from and inserted to the splay tree every time
81 when its spilling priority is changed but such solution would be
82 more costly although simpler. */
83 static VEC(ira_allocno_t
,heap
) *removed_splay_allocno_vec
;
87 /* This page contains functions used to find conflicts using allocno
90 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
91 used to find a conflict for new allocnos or allocnos with the
92 different cover classes. */
94 allocnos_have_intersected_live_ranges_p (ira_allocno_t a1
, ira_allocno_t a2
)
98 if (ALLOCNO_REG (a1
) != NULL
&& ALLOCNO_REG (a2
) != NULL
99 && (ORIGINAL_REGNO (ALLOCNO_REG (a1
))
100 == ORIGINAL_REGNO (ALLOCNO_REG (a2
))))
102 return ira_allocno_live_ranges_intersect_p (ALLOCNO_LIVE_RANGES (a1
),
103 ALLOCNO_LIVE_RANGES (a2
));
106 #ifdef ENABLE_IRA_CHECKING
108 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
109 intersect. This should be used when there is only one region.
110 Currently this is used during reload. */
112 pseudos_have_intersected_live_ranges_p (int regno1
, int regno2
)
114 ira_allocno_t a1
, a2
;
116 ira_assert (regno1
>= FIRST_PSEUDO_REGISTER
117 && regno2
>= FIRST_PSEUDO_REGISTER
);
118 /* Reg info caclulated by dataflow infrastructure can be different
119 from one calculated by regclass. */
120 if ((a1
= ira_loop_tree_root
->regno_allocno_map
[regno1
]) == NULL
121 || (a2
= ira_loop_tree_root
->regno_allocno_map
[regno2
]) == NULL
)
123 return allocnos_have_intersected_live_ranges_p (a1
, a2
);
130 /* This page contains functions used to choose hard registers for
133 /* Array whose element value is TRUE if the corresponding hard
134 register was already allocated for an allocno. */
135 static bool allocated_hardreg_p
[FIRST_PSEUDO_REGISTER
];
137 /* Describes one element in a queue of allocnos whose costs need to be
138 updated. Each allocno in the queue is known to have a cover class. */
139 struct update_cost_queue_elem
141 /* This element is in the queue iff CHECK == update_cost_check. */
144 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
145 connecting this allocno to the one being allocated. */
148 /* The next allocno in the queue, or null if this is the last element. */
152 /* The first element in a queue of allocnos whose copy costs need to be
153 updated. Null if the queue is empty. */
154 static ira_allocno_t update_cost_queue
;
156 /* The last element in the queue described by update_cost_queue.
157 Not valid if update_cost_queue is null. */
158 static struct update_cost_queue_elem
*update_cost_queue_tail
;
160 /* A pool of elements in the queue described by update_cost_queue.
161 Elements are indexed by ALLOCNO_NUM. */
162 static struct update_cost_queue_elem
*update_cost_queue_elems
;
164 /* The current value of update_copy_cost call count. */
165 static int update_cost_check
;
167 /* Allocate and initialize data necessary for function
168 update_copy_costs. */
170 initiate_cost_update (void)
174 size
= ira_allocnos_num
* sizeof (struct update_cost_queue_elem
);
175 update_cost_queue_elems
176 = (struct update_cost_queue_elem
*) ira_allocate (size
);
177 memset (update_cost_queue_elems
, 0, size
);
178 update_cost_check
= 0;
181 /* Deallocate data used by function update_copy_costs. */
183 finish_cost_update (void)
185 ira_free (update_cost_queue_elems
);
188 /* When we traverse allocnos to update hard register costs, the cost
189 divisor will be multiplied by the following macro value for each
190 hop from given allocno to directly connected allocnos. */
191 #define COST_HOP_DIVISOR 4
193 /* Start a new cost-updating pass. */
195 start_update_cost (void)
198 update_cost_queue
= NULL
;
201 /* Add (ALLOCNO, DIVISOR) to the end of update_cost_queue,
202 unless ALLOCNO is already in the queue, or has no cover class. */
204 queue_update_cost (ira_allocno_t allocno
, int divisor
)
206 struct update_cost_queue_elem
*elem
;
208 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (allocno
)];
209 if (elem
->check
!= update_cost_check
210 && ALLOCNO_COVER_CLASS (allocno
) != NO_REGS
)
212 elem
->check
= update_cost_check
;
213 elem
->divisor
= divisor
;
215 if (update_cost_queue
== NULL
)
216 update_cost_queue
= allocno
;
218 update_cost_queue_tail
->next
= allocno
;
219 update_cost_queue_tail
= elem
;
223 /* Try to remove the first element from update_cost_queue. Return false
224 if the queue was empty, otherwise make (*ALLOCNO, *DIVISOR) describe
225 the removed element. */
227 get_next_update_cost (ira_allocno_t
*allocno
, int *divisor
)
229 struct update_cost_queue_elem
*elem
;
231 if (update_cost_queue
== NULL
)
234 *allocno
= update_cost_queue
;
235 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (*allocno
)];
236 *divisor
= elem
->divisor
;
237 update_cost_queue
= elem
->next
;
241 /* Update the cost of allocnos to increase chances to remove some
242 copies as the result of subsequent assignment. */
244 update_copy_costs (ira_allocno_t allocno
, bool decr_p
)
246 int i
, cost
, update_cost
, hard_regno
, divisor
;
247 enum machine_mode mode
;
248 enum reg_class rclass
, cover_class
;
249 ira_allocno_t another_allocno
;
250 ira_copy_t cp
, next_cp
;
252 hard_regno
= ALLOCNO_HARD_REGNO (allocno
);
253 ira_assert (hard_regno
>= 0);
255 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
256 if (cover_class
== NO_REGS
)
258 i
= ira_class_hard_reg_index
[cover_class
][hard_regno
];
260 rclass
= REGNO_REG_CLASS (hard_regno
);
262 start_update_cost ();
266 mode
= ALLOCNO_MODE (allocno
);
267 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
269 if (cp
->first
== allocno
)
271 next_cp
= cp
->next_first_allocno_copy
;
272 another_allocno
= cp
->second
;
274 else if (cp
->second
== allocno
)
276 next_cp
= cp
->next_second_allocno_copy
;
277 another_allocno
= cp
->first
;
282 if (cover_class
!= ALLOCNO_COVER_CLASS (another_allocno
)
283 || ALLOCNO_ASSIGNED_P (another_allocno
))
286 cost
= (cp
->second
== allocno
287 ? ira_register_move_cost
[mode
][rclass
][cover_class
]
288 : ira_register_move_cost
[mode
][cover_class
][rclass
]);
292 update_cost
= cp
->freq
* cost
/ divisor
;
293 if (update_cost
== 0)
296 ira_allocate_and_set_or_copy_costs
297 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno
), cover_class
,
298 ALLOCNO_UPDATED_COVER_CLASS_COST (another_allocno
),
299 ALLOCNO_HARD_REG_COSTS (another_allocno
));
300 ira_allocate_and_set_or_copy_costs
301 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
),
303 ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno
));
304 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno
)[i
] += update_cost
;
305 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
)[i
]
308 queue_update_cost (another_allocno
, divisor
* COST_HOP_DIVISOR
);
311 while (get_next_update_cost (&allocno
, &divisor
));
314 /* This function updates COSTS (decrease if DECR_P) by conflict costs
315 of the unassigned allocnos connected by copies with allocnos in
316 update_cost_queue. This update increases chances to remove some
319 update_conflict_hard_regno_costs (int *costs
, bool decr_p
)
321 int i
, cost
, class_size
, freq
, mult
, div
, divisor
;
324 enum reg_class cover_class
;
325 ira_allocno_t allocno
, another_allocno
;
326 ira_copy_t cp
, next_cp
;
328 while (get_next_update_cost (&allocno
, &divisor
))
329 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
331 if (cp
->first
== allocno
)
333 next_cp
= cp
->next_first_allocno_copy
;
334 another_allocno
= cp
->second
;
336 else if (cp
->second
== allocno
)
338 next_cp
= cp
->next_second_allocno_copy
;
339 another_allocno
= cp
->first
;
343 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
344 if (cover_class
!= ALLOCNO_COVER_CLASS (another_allocno
)
345 || ALLOCNO_ASSIGNED_P (another_allocno
)
346 || ALLOCNO_MAY_BE_SPILLED_P (ALLOCNO_FIRST_COALESCED_ALLOCNO
349 class_size
= ira_class_hard_regs_num
[cover_class
];
350 ira_allocate_and_copy_costs
351 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
),
352 cover_class
, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno
));
354 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
);
355 if (conflict_costs
== NULL
)
360 freq
= ALLOCNO_FREQ (another_allocno
);
363 div
= freq
* divisor
;
365 for (i
= class_size
- 1; i
>= 0; i
--)
367 cost
= conflict_costs
[i
] * mult
/ div
;
376 /* Probably 5 hops will be enough. */
378 && divisor
<= (COST_HOP_DIVISOR
382 queue_update_cost (another_allocno
, divisor
* COST_HOP_DIVISOR
);
386 /* Sort allocnos according to the profit of usage of a hard register
387 instead of memory for them. */
389 allocno_cost_compare_func (const void *v1p
, const void *v2p
)
391 ira_allocno_t p1
= *(const ira_allocno_t
*) v1p
;
392 ira_allocno_t p2
= *(const ira_allocno_t
*) v2p
;
395 c1
= ALLOCNO_UPDATED_MEMORY_COST (p1
) - ALLOCNO_UPDATED_COVER_CLASS_COST (p1
);
396 c2
= ALLOCNO_UPDATED_MEMORY_COST (p2
) - ALLOCNO_UPDATED_COVER_CLASS_COST (p2
);
400 /* If regs are equally good, sort by allocno numbers, so that the
401 results of qsort leave nothing to chance. */
402 return ALLOCNO_NUM (p1
) - ALLOCNO_NUM (p2
);
405 /* Print all allocnos coalesced with ALLOCNO. */
407 print_coalesced_allocno (ira_allocno_t allocno
)
411 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
412 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
414 ira_print_expanded_allocno (a
);
417 fprintf (ira_dump_file
, "+");
421 /* Choose a hard register for ALLOCNO (or for all coalesced allocnos
422 represented by ALLOCNO). If RETRY_P is TRUE, it means that the
423 function called from function `ira_reassign_conflict_allocnos' and
424 `allocno_reload_assign'. This function implements the optimistic
425 coalescing too: if we failed to assign a hard register to set of
426 the coalesced allocnos, we put them onto the coloring stack for
427 subsequent separate assigning. */
429 assign_hard_reg (ira_allocno_t allocno
, bool retry_p
)
431 HARD_REG_SET conflicting_regs
;
432 int i
, j
, hard_regno
, best_hard_regno
, class_size
;
433 int cost
, mem_cost
, min_cost
, full_cost
, min_full_cost
, add_cost
;
436 enum reg_class cover_class
, rclass
;
437 enum machine_mode mode
;
438 ira_allocno_t a
, conflict_allocno
;
439 ira_allocno_conflict_iterator aci
;
440 static int costs
[FIRST_PSEUDO_REGISTER
], full_costs
[FIRST_PSEUDO_REGISTER
];
445 ira_assert (! ALLOCNO_ASSIGNED_P (allocno
));
446 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
447 class_size
= ira_class_hard_regs_num
[cover_class
];
448 mode
= ALLOCNO_MODE (allocno
);
449 CLEAR_HARD_REG_SET (conflicting_regs
);
450 best_hard_regno
= -1;
451 memset (full_costs
, 0, sizeof (int) * class_size
);
453 if (allocno_coalesced_p
)
454 bitmap_clear (processed_coalesced_allocno_bitmap
);
455 memset (costs
, 0, sizeof (int) * class_size
);
456 memset (full_costs
, 0, sizeof (int) * class_size
);
458 no_stack_reg_p
= false;
460 start_update_cost ();
461 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
462 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
464 mem_cost
+= ALLOCNO_UPDATED_MEMORY_COST (a
);
465 IOR_HARD_REG_SET (conflicting_regs
,
466 ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a
));
467 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
),
468 cover_class
, ALLOCNO_HARD_REG_COSTS (a
));
469 a_costs
= ALLOCNO_UPDATED_HARD_REG_COSTS (a
);
471 no_stack_reg_p
= no_stack_reg_p
|| ALLOCNO_TOTAL_NO_STACK_REG_P (a
);
473 for (cost
= ALLOCNO_UPDATED_COVER_CLASS_COST (a
), i
= 0;
478 costs
[i
] += a_costs
[i
];
479 full_costs
[i
] += a_costs
[i
];
484 full_costs
[i
] += cost
;
486 /* Take preferences of conflicting allocnos into account. */
487 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_allocno
, aci
)
488 /* Reload can give another class so we need to check all
490 if (retry_p
|| bitmap_bit_p (consideration_allocno_bitmap
,
491 ALLOCNO_NUM (conflict_allocno
)))
493 ira_assert (cover_class
== ALLOCNO_COVER_CLASS (conflict_allocno
));
494 if (allocno_coalesced_p
)
496 if (bitmap_bit_p (processed_coalesced_allocno_bitmap
,
497 ALLOCNO_NUM (conflict_allocno
)))
499 bitmap_set_bit (processed_coalesced_allocno_bitmap
,
500 ALLOCNO_NUM (conflict_allocno
));
502 if (ALLOCNO_ASSIGNED_P (conflict_allocno
))
504 if ((hard_regno
= ALLOCNO_HARD_REGNO (conflict_allocno
)) >= 0)
508 ira_reg_mode_hard_regset
509 [hard_regno
][ALLOCNO_MODE (conflict_allocno
)]);
510 if (hard_reg_set_subset_p (reg_class_contents
[cover_class
],
516 else if (! ALLOCNO_MAY_BE_SPILLED_P (ALLOCNO_FIRST_COALESCED_ALLOCNO
519 ira_allocate_and_copy_costs
520 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno
),
522 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_allocno
));
524 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno
);
525 if (conflict_costs
!= NULL
)
526 for (j
= class_size
- 1; j
>= 0; j
--)
527 full_costs
[j
] -= conflict_costs
[j
];
528 queue_update_cost (conflict_allocno
, COST_HOP_DIVISOR
);
534 /* Take into account preferences of allocnos connected by copies to
535 the conflict allocnos. */
536 update_conflict_hard_regno_costs (full_costs
, true);
538 /* Take preferences of allocnos connected by copies into
540 start_update_cost ();
541 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
542 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
544 queue_update_cost (a
, COST_HOP_DIVISOR
);
548 update_conflict_hard_regno_costs (full_costs
, false);
549 min_cost
= min_full_cost
= INT_MAX
;
550 /* We don't care about giving callee saved registers to allocnos no
551 living through calls because call clobbered registers are
552 allocated first (it is usual practice to put them first in
554 for (i
= 0; i
< class_size
; i
++)
556 hard_regno
= ira_class_hard_regs
[cover_class
][i
];
559 && FIRST_STACK_REG
<= hard_regno
&& hard_regno
<= LAST_STACK_REG
)
562 if (! ira_hard_reg_not_in_set_p (hard_regno
, mode
, conflicting_regs
)
563 || TEST_HARD_REG_BIT (prohibited_class_mode_regs
[cover_class
][mode
],
567 full_cost
= full_costs
[i
];
568 if (! allocated_hardreg_p
[hard_regno
]
569 && ira_hard_reg_not_in_set_p (hard_regno
, mode
, call_used_reg_set
))
570 /* We need to save/restore the hard register in
571 epilogue/prologue. Therefore we increase the cost. */
573 /* ??? If only part is call clobbered. */
574 rclass
= REGNO_REG_CLASS (hard_regno
);
575 add_cost
= (ira_memory_move_cost
[mode
][rclass
][0]
576 + ira_memory_move_cost
[mode
][rclass
][1] - 1);
578 full_cost
+= add_cost
;
582 if (min_full_cost
> full_cost
)
584 min_full_cost
= full_cost
;
585 best_hard_regno
= hard_regno
;
586 ira_assert (hard_regno
>= 0);
589 if (min_full_cost
> mem_cost
)
591 if (! retry_p
&& internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
592 fprintf (ira_dump_file
, "(memory is more profitable %d vs %d) ",
593 mem_cost
, min_full_cost
);
594 best_hard_regno
= -1;
597 if (best_hard_regno
< 0
598 && ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
) != allocno
)
600 for (j
= 0, a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
601 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
603 ira_assert (! ALLOCNO_IN_GRAPH_P (a
));
604 sorted_allocnos
[j
++] = a
;
608 qsort (sorted_allocnos
, j
, sizeof (ira_allocno_t
),
609 allocno_cost_compare_func
);
610 for (i
= 0; i
< j
; i
++)
612 a
= sorted_allocnos
[i
];
613 ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) = a
;
614 ALLOCNO_NEXT_COALESCED_ALLOCNO (a
) = a
;
615 VEC_safe_push (ira_allocno_t
, heap
, allocno_stack_vec
, a
);
616 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
618 fprintf (ira_dump_file
, " Pushing");
619 print_coalesced_allocno (a
);
620 fprintf (ira_dump_file
, "\n");
625 if (best_hard_regno
>= 0)
626 allocated_hardreg_p
[best_hard_regno
] = true;
627 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
628 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
630 ALLOCNO_HARD_REGNO (a
) = best_hard_regno
;
631 ALLOCNO_ASSIGNED_P (a
) = true;
632 if (best_hard_regno
>= 0)
633 update_copy_costs (a
, true);
634 ira_assert (ALLOCNO_COVER_CLASS (a
) == cover_class
);
635 /* We don't need updated costs anymore: */
636 ira_free_allocno_updated_costs (a
);
640 return best_hard_regno
>= 0;
645 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
647 /* Bucket of allocnos that can colored currently without spilling. */
648 static ira_allocno_t colorable_allocno_bucket
;
650 /* Bucket of allocnos that might be not colored currently without
652 static ira_allocno_t uncolorable_allocno_bucket
;
654 /* Each element of the array contains the current number of allocnos
655 of given *cover* class in the uncolorable_bucket. */
656 static int uncolorable_allocnos_num
[N_REG_CLASSES
];
658 /* Add ALLOCNO to bucket *BUCKET_PTR. ALLOCNO should be not in a bucket
661 add_allocno_to_bucket (ira_allocno_t allocno
, ira_allocno_t
*bucket_ptr
)
663 ira_allocno_t first_allocno
;
664 enum reg_class cover_class
;
666 if (bucket_ptr
== &uncolorable_allocno_bucket
667 && (cover_class
= ALLOCNO_COVER_CLASS (allocno
)) != NO_REGS
)
669 uncolorable_allocnos_num
[cover_class
]++;
670 ira_assert (uncolorable_allocnos_num
[cover_class
] > 0);
672 first_allocno
= *bucket_ptr
;
673 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno
) = first_allocno
;
674 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno
) = NULL
;
675 if (first_allocno
!= NULL
)
676 ALLOCNO_PREV_BUCKET_ALLOCNO (first_allocno
) = allocno
;
677 *bucket_ptr
= allocno
;
680 /* The function returns frequency and number of available hard
681 registers for allocnos coalesced with ALLOCNO. */
683 get_coalesced_allocnos_attributes (ira_allocno_t allocno
, int *freq
, int *num
)
689 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
690 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
692 *freq
+= ALLOCNO_FREQ (a
);
693 *num
+= ALLOCNO_AVAILABLE_REGS_NUM (a
);
699 /* Compare two allocnos to define which allocno should be pushed first
700 into the coloring stack. If the return is a negative number, the
701 allocno given by the first parameter will be pushed first. In this
702 case such allocno has less priority than the second one and the
703 hard register will be assigned to it after assignment to the second
704 one. As the result of such assignment order, the second allocno
705 has a better chance to get the best hard register. */
707 bucket_allocno_compare_func (const void *v1p
, const void *v2p
)
709 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
710 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
711 int diff
, a1_freq
, a2_freq
, a1_num
, a2_num
;
713 if ((diff
= (int) ALLOCNO_COVER_CLASS (a2
) - ALLOCNO_COVER_CLASS (a1
)) != 0)
715 get_coalesced_allocnos_attributes (a1
, &a1_freq
, &a1_num
);
716 get_coalesced_allocnos_attributes (a2
, &a2_freq
, &a2_num
);
717 if ((diff
= a2_num
- a1_num
) != 0)
719 else if ((diff
= a1_freq
- a2_freq
) != 0)
721 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
724 /* Sort bucket *BUCKET_PTR and return the result through
727 sort_bucket (ira_allocno_t
*bucket_ptr
)
729 ira_allocno_t a
, head
;
732 for (n
= 0, a
= *bucket_ptr
; a
!= NULL
; a
= ALLOCNO_NEXT_BUCKET_ALLOCNO (a
))
733 sorted_allocnos
[n
++] = a
;
736 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
737 bucket_allocno_compare_func
);
739 for (n
--; n
>= 0; n
--)
741 a
= sorted_allocnos
[n
];
742 ALLOCNO_NEXT_BUCKET_ALLOCNO (a
) = head
;
743 ALLOCNO_PREV_BUCKET_ALLOCNO (a
) = NULL
;
745 ALLOCNO_PREV_BUCKET_ALLOCNO (head
) = a
;
751 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
752 their priority. ALLOCNO should be not in a bucket before the
755 add_allocno_to_ordered_bucket (ira_allocno_t allocno
,
756 ira_allocno_t
*bucket_ptr
)
758 ira_allocno_t before
, after
;
759 enum reg_class cover_class
;
761 if (bucket_ptr
== &uncolorable_allocno_bucket
762 && (cover_class
= ALLOCNO_COVER_CLASS (allocno
)) != NO_REGS
)
764 uncolorable_allocnos_num
[cover_class
]++;
765 ira_assert (uncolorable_allocnos_num
[cover_class
] > 0);
767 for (before
= *bucket_ptr
, after
= NULL
;
769 after
= before
, before
= ALLOCNO_NEXT_BUCKET_ALLOCNO (before
))
770 if (bucket_allocno_compare_func (&allocno
, &before
) < 0)
772 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno
) = before
;
773 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno
) = after
;
775 *bucket_ptr
= allocno
;
777 ALLOCNO_NEXT_BUCKET_ALLOCNO (after
) = allocno
;
779 ALLOCNO_PREV_BUCKET_ALLOCNO (before
) = allocno
;
782 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
785 delete_allocno_from_bucket (ira_allocno_t allocno
, ira_allocno_t
*bucket_ptr
)
787 ira_allocno_t prev_allocno
, next_allocno
;
788 enum reg_class cover_class
;
790 if (bucket_ptr
== &uncolorable_allocno_bucket
791 && (cover_class
= ALLOCNO_COVER_CLASS (allocno
)) != NO_REGS
)
793 uncolorable_allocnos_num
[cover_class
]--;
794 ira_assert (uncolorable_allocnos_num
[cover_class
] >= 0);
796 prev_allocno
= ALLOCNO_PREV_BUCKET_ALLOCNO (allocno
);
797 next_allocno
= ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno
);
798 if (prev_allocno
!= NULL
)
799 ALLOCNO_NEXT_BUCKET_ALLOCNO (prev_allocno
) = next_allocno
;
802 ira_assert (*bucket_ptr
== allocno
);
803 *bucket_ptr
= next_allocno
;
805 if (next_allocno
!= NULL
)
806 ALLOCNO_PREV_BUCKET_ALLOCNO (next_allocno
) = prev_allocno
;
809 /* Splay tree for each cover class. The trees are indexed by the
810 corresponding cover classes. Splay trees contain uncolorable
812 static splay_tree uncolorable_allocnos_splay_tree
[N_REG_CLASSES
];
814 /* If the following macro is TRUE, splay tree is used to choose an
815 allocno of the corresponding cover class for spilling. When the
816 number uncolorable allocnos of given cover class decreases to some
817 threshold, linear array search is used to find the best allocno for
818 spilling. This threshold is actually pretty big because, although
819 splay trees asymptotically is much faster, each splay tree
820 operation is sufficiently costly especially taking cache locality
822 #define USE_SPLAY_P(CLASS) (uncolorable_allocnos_num[CLASS] > 4000)
824 /* Put ALLOCNO onto the coloring stack without removing it from its
825 bucket. Pushing allocno to the coloring stack can result in moving
826 conflicting allocnos from the uncolorable bucket to the colorable
829 push_allocno_to_stack (ira_allocno_t allocno
)
831 int conflicts_num
, conflict_size
, size
;
832 ira_allocno_t a
, conflict_allocno
;
833 enum reg_class cover_class
;
834 ira_allocno_conflict_iterator aci
;
836 ALLOCNO_IN_GRAPH_P (allocno
) = false;
837 VEC_safe_push (ira_allocno_t
, heap
, allocno_stack_vec
, allocno
);
838 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
839 if (cover_class
== NO_REGS
)
841 size
= ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE (allocno
)];
842 if (allocno_coalesced_p
)
843 bitmap_clear (processed_coalesced_allocno_bitmap
);
844 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
845 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
847 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_allocno
, aci
)
849 conflict_allocno
= ALLOCNO_FIRST_COALESCED_ALLOCNO (conflict_allocno
);
850 if (bitmap_bit_p (coloring_allocno_bitmap
,
851 ALLOCNO_NUM (conflict_allocno
)))
853 ira_assert (cover_class
854 == ALLOCNO_COVER_CLASS (conflict_allocno
));
855 if (allocno_coalesced_p
)
857 if (bitmap_bit_p (processed_coalesced_allocno_bitmap
,
858 ALLOCNO_NUM (conflict_allocno
)))
860 bitmap_set_bit (processed_coalesced_allocno_bitmap
,
861 ALLOCNO_NUM (conflict_allocno
));
863 if (ALLOCNO_IN_GRAPH_P (conflict_allocno
)
864 && ! ALLOCNO_ASSIGNED_P (conflict_allocno
))
866 conflicts_num
= ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno
);
868 = (ira_reg_class_nregs
869 [cover_class
][ALLOCNO_MODE (conflict_allocno
)]);
871 (ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno
) >= size
);
872 if (conflicts_num
+ conflict_size
873 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno
))
875 ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno
) -= size
;
879 = ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno
) - size
;
880 if (uncolorable_allocnos_splay_tree
[cover_class
] != NULL
881 && !ALLOCNO_SPLAY_REMOVED_P (conflict_allocno
)
882 && USE_SPLAY_P (cover_class
))
886 (uncolorable_allocnos_splay_tree
[cover_class
],
887 (splay_tree_key
) conflict_allocno
) != NULL
);
889 (uncolorable_allocnos_splay_tree
[cover_class
],
890 (splay_tree_key
) conflict_allocno
);
891 ALLOCNO_SPLAY_REMOVED_P (conflict_allocno
) = true;
892 VEC_safe_push (ira_allocno_t
, heap
,
893 removed_splay_allocno_vec
,
896 ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno
) = conflicts_num
;
897 if (conflicts_num
+ conflict_size
898 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno
))
900 delete_allocno_from_bucket
901 (conflict_allocno
, &uncolorable_allocno_bucket
);
902 add_allocno_to_ordered_bucket
903 (conflict_allocno
, &colorable_allocno_bucket
);
913 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
914 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
916 remove_allocno_from_bucket_and_push (ira_allocno_t allocno
, bool colorable_p
)
918 enum reg_class cover_class
;
921 delete_allocno_from_bucket (allocno
, &colorable_allocno_bucket
);
923 delete_allocno_from_bucket (allocno
, &uncolorable_allocno_bucket
);
924 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
926 fprintf (ira_dump_file
, " Pushing");
927 print_coalesced_allocno (allocno
);
928 fprintf (ira_dump_file
, "%s\n", colorable_p
? "" : "(potential spill)");
930 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
931 ira_assert ((colorable_p
932 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno
)
933 + ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE (allocno
)]
934 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno
)))
936 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno
)
937 + ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE
939 > ALLOCNO_AVAILABLE_REGS_NUM (allocno
))));
941 ALLOCNO_MAY_BE_SPILLED_P (allocno
) = true;
942 push_allocno_to_stack (allocno
);
945 /* Put all allocnos from colorable bucket onto the coloring stack. */
947 push_only_colorable (void)
949 sort_bucket (&colorable_allocno_bucket
);
950 for (;colorable_allocno_bucket
!= NULL
;)
951 remove_allocno_from_bucket_and_push (colorable_allocno_bucket
, true);
954 /* Puts ALLOCNO chosen for potential spilling onto the coloring
957 push_allocno_to_spill (ira_allocno_t allocno
)
959 delete_allocno_from_bucket (allocno
, &uncolorable_allocno_bucket
);
960 ALLOCNO_MAY_BE_SPILLED_P (allocno
) = true;
961 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
962 fprintf (ira_dump_file
, " Pushing p%d(%d) (potential spill)\n",
963 ALLOCNO_NUM (allocno
), ALLOCNO_REGNO (allocno
));
964 push_allocno_to_stack (allocno
);
967 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
968 loop given by its LOOP_NODE. */
970 ira_loop_edge_freq (ira_loop_tree_node_t loop_node
, int regno
, bool exit_p
)
975 VEC (edge
, heap
) *edges
;
977 ira_assert (loop_node
->loop
!= NULL
978 && (regno
< 0 || regno
>= FIRST_PSEUDO_REGISTER
));
982 FOR_EACH_EDGE (e
, ei
, loop_node
->loop
->header
->preds
)
983 if (e
->src
!= loop_node
->loop
->latch
985 || (bitmap_bit_p (DF_LR_OUT (e
->src
), regno
)
986 && bitmap_bit_p (DF_LR_IN (e
->dest
), regno
))))
987 freq
+= EDGE_FREQUENCY (e
);
991 edges
= get_loop_exit_edges (loop_node
->loop
);
992 for (i
= 0; VEC_iterate (edge
, edges
, i
, e
); i
++)
994 || (bitmap_bit_p (DF_LR_OUT (e
->src
), regno
)
995 && bitmap_bit_p (DF_LR_IN (e
->dest
), regno
)))
996 freq
+= EDGE_FREQUENCY (e
);
997 VEC_free (edge
, heap
, edges
);
1000 return REG_FREQ_FROM_EDGE_FREQ (freq
);
1003 /* Calculate and return the cost of putting allocno A into memory. */
1005 calculate_allocno_spill_cost (ira_allocno_t a
)
1008 enum machine_mode mode
;
1009 enum reg_class rclass
;
1010 ira_allocno_t parent_allocno
;
1011 ira_loop_tree_node_t parent_node
, loop_node
;
1013 regno
= ALLOCNO_REGNO (a
);
1014 cost
= ALLOCNO_UPDATED_MEMORY_COST (a
) - ALLOCNO_UPDATED_COVER_CLASS_COST (a
);
1015 if (ALLOCNO_CAP (a
) != NULL
)
1017 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
1018 if ((parent_node
= loop_node
->parent
) == NULL
)
1020 if ((parent_allocno
= parent_node
->regno_allocno_map
[regno
]) == NULL
)
1022 mode
= ALLOCNO_MODE (a
);
1023 rclass
= ALLOCNO_COVER_CLASS (a
);
1024 if (ALLOCNO_HARD_REGNO (parent_allocno
) < 0)
1025 cost
-= (ira_memory_move_cost
[mode
][rclass
][0]
1026 * ira_loop_edge_freq (loop_node
, regno
, true)
1027 + ira_memory_move_cost
[mode
][rclass
][1]
1028 * ira_loop_edge_freq (loop_node
, regno
, false));
1030 cost
+= ((ira_memory_move_cost
[mode
][rclass
][1]
1031 * ira_loop_edge_freq (loop_node
, regno
, true)
1032 + ira_memory_move_cost
[mode
][rclass
][0]
1033 * ira_loop_edge_freq (loop_node
, regno
, false))
1034 - (ira_register_move_cost
[mode
][rclass
][rclass
]
1035 * (ira_loop_edge_freq (loop_node
, regno
, false)
1036 + ira_loop_edge_freq (loop_node
, regno
, true))));
1040 /* Compare keys in the splay tree used to choose best allocno for
1041 spilling. The best allocno has the minimal key. */
1043 allocno_spill_priority_compare (splay_tree_key k1
, splay_tree_key k2
)
1045 int pri1
, pri2
, diff
;
1046 ira_allocno_t a1
= (ira_allocno_t
) k1
, a2
= (ira_allocno_t
) k2
;
1048 pri1
= (ALLOCNO_TEMP (a1
)
1049 / (ALLOCNO_LEFT_CONFLICTS_NUM (a1
)
1050 * ira_reg_class_nregs
[ALLOCNO_COVER_CLASS (a1
)][ALLOCNO_MODE (a1
)]
1052 pri2
= (ALLOCNO_TEMP (a2
)
1053 / (ALLOCNO_LEFT_CONFLICTS_NUM (a2
)
1054 * ira_reg_class_nregs
[ALLOCNO_COVER_CLASS (a2
)][ALLOCNO_MODE (a2
)]
1056 if ((diff
= pri1
- pri2
) != 0)
1058 if ((diff
= ALLOCNO_TEMP (a1
) - ALLOCNO_TEMP (a2
)) != 0)
1060 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
1063 /* Allocate data of SIZE for the splay trees. We allocate only spay
1064 tree roots or splay tree nodes. If you change this, please rewrite
1067 splay_tree_allocate (int size
, void *data ATTRIBUTE_UNUSED
)
1069 if (size
!= sizeof (struct splay_tree_node_s
))
1070 return ira_allocate (size
);
1071 return pool_alloc (splay_tree_node_pool
);
1074 /* Free data NODE for the splay trees. We allocate and free only spay
1075 tree roots or splay tree nodes. If you change this, please rewrite
1078 splay_tree_free (void *node
, void *data ATTRIBUTE_UNUSED
)
1081 enum reg_class cover_class
;
1083 for (i
= 0; i
< ira_reg_class_cover_size
; i
++)
1085 cover_class
= ira_reg_class_cover
[i
];
1086 if (node
== uncolorable_allocnos_splay_tree
[cover_class
])
1092 pool_free (splay_tree_node_pool
, node
);
1095 /* Push allocnos to the coloring stack. The order of allocnos in the
1096 stack defines the order for the subsequent coloring. */
1098 push_allocnos_to_stack (void)
1100 ira_allocno_t allocno
, a
, i_allocno
, *allocno_vec
;
1101 enum reg_class cover_class
, rclass
;
1102 int allocno_pri
, i_allocno_pri
, allocno_cost
, i_allocno_cost
;
1103 int i
, j
, num
, cover_class_allocnos_num
[N_REG_CLASSES
];
1104 ira_allocno_t
*cover_class_allocnos
[N_REG_CLASSES
];
1108 VEC_truncate(ira_allocno_t
, removed_splay_allocno_vec
, 0);
1109 for (i
= 0; i
< ira_reg_class_cover_size
; i
++)
1111 cover_class
= ira_reg_class_cover
[i
];
1112 cover_class_allocnos_num
[cover_class
] = 0;
1113 cover_class_allocnos
[cover_class
] = NULL
;
1114 uncolorable_allocnos_splay_tree
[cover_class
] = NULL
;
1116 /* Calculate uncolorable allocno spill costs. */
1117 for (allocno
= uncolorable_allocno_bucket
;
1119 allocno
= ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno
))
1120 if ((cover_class
= ALLOCNO_COVER_CLASS (allocno
)) != NO_REGS
)
1122 cover_class_allocnos_num
[cover_class
]++;
1124 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
1125 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1127 cost
+= calculate_allocno_spill_cost (a
);
1131 /* ??? Remove cost of copies between the coalesced
1133 ALLOCNO_TEMP (allocno
) = cost
;
1135 /* Define place where to put uncolorable allocnos of the same cover
1137 for (num
= i
= 0; i
< ira_reg_class_cover_size
; i
++)
1139 cover_class
= ira_reg_class_cover
[i
];
1140 ira_assert (cover_class_allocnos_num
[cover_class
]
1141 == uncolorable_allocnos_num
[cover_class
]);
1142 if (cover_class_allocnos_num
[cover_class
] != 0)
1144 cover_class_allocnos
[cover_class
] = allocnos_for_spilling
+ num
;
1145 num
+= cover_class_allocnos_num
[cover_class
];
1146 cover_class_allocnos_num
[cover_class
] = 0;
1148 if (USE_SPLAY_P (cover_class
))
1149 uncolorable_allocnos_splay_tree
[cover_class
]
1150 = splay_tree_new_with_allocator (allocno_spill_priority_compare
,
1151 NULL
, NULL
, splay_tree_allocate
,
1152 splay_tree_free
, NULL
);
1154 ira_assert (num
<= ira_allocnos_num
);
1155 /* Collect uncolorable allocnos of each cover class. */
1156 for (allocno
= uncolorable_allocno_bucket
;
1158 allocno
= ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno
))
1159 if ((cover_class
= ALLOCNO_COVER_CLASS (allocno
)) != NO_REGS
)
1161 cover_class_allocnos
1162 [cover_class
][cover_class_allocnos_num
[cover_class
]++] = allocno
;
1163 if (uncolorable_allocnos_splay_tree
[cover_class
] != NULL
)
1164 splay_tree_insert (uncolorable_allocnos_splay_tree
[cover_class
],
1165 (splay_tree_key
) allocno
,
1166 (splay_tree_value
) allocno
);
1170 push_only_colorable ();
1171 allocno
= uncolorable_allocno_bucket
;
1172 if (allocno
== NULL
)
1174 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
1175 if (cover_class
== NO_REGS
)
1177 push_allocno_to_spill (allocno
);
1180 /* Potential spilling. */
1182 (ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE (allocno
)] > 0);
1183 if (USE_SPLAY_P (cover_class
))
1185 for (;VEC_length (ira_allocno_t
, removed_splay_allocno_vec
) != 0;)
1187 allocno
= VEC_pop (ira_allocno_t
, removed_splay_allocno_vec
);
1188 ALLOCNO_SPLAY_REMOVED_P (allocno
) = false;
1189 rclass
= ALLOCNO_COVER_CLASS (allocno
);
1190 if (ALLOCNO_LEFT_CONFLICTS_NUM (allocno
)
1191 + ira_reg_class_nregs
[rclass
][ALLOCNO_MODE (allocno
)]
1192 > ALLOCNO_AVAILABLE_REGS_NUM (allocno
))
1194 (uncolorable_allocnos_splay_tree
[rclass
],
1195 (splay_tree_key
) allocno
, (splay_tree_value
) allocno
);
1197 allocno
= ((ira_allocno_t
)
1199 (uncolorable_allocnos_splay_tree
[cover_class
])->key
);
1200 splay_tree_remove (uncolorable_allocnos_splay_tree
[cover_class
],
1201 (splay_tree_key
) allocno
);
1205 num
= cover_class_allocnos_num
[cover_class
];
1206 ira_assert (num
> 0);
1207 allocno_vec
= cover_class_allocnos
[cover_class
];
1209 allocno_pri
= allocno_cost
= 0;
1210 /* Sort uncolorable allocno to find the one with the lowest
1212 for (i
= 0, j
= num
- 1; i
<= j
;)
1214 i_allocno
= allocno_vec
[i
];
1215 if (! ALLOCNO_IN_GRAPH_P (i_allocno
)
1216 && ALLOCNO_IN_GRAPH_P (allocno_vec
[j
]))
1218 i_allocno
= allocno_vec
[j
];
1219 allocno_vec
[j
] = allocno_vec
[i
];
1220 allocno_vec
[i
] = i_allocno
;
1222 if (ALLOCNO_IN_GRAPH_P (i_allocno
))
1225 ira_assert (ALLOCNO_TEMP (i_allocno
) != INT_MAX
);
1226 i_allocno_cost
= ALLOCNO_TEMP (i_allocno
);
1229 / (ALLOCNO_LEFT_CONFLICTS_NUM (i_allocno
)
1230 * ira_reg_class_nregs
[ALLOCNO_COVER_CLASS
1232 [ALLOCNO_MODE (i_allocno
)] + 1));
1234 || (! ALLOCNO_BAD_SPILL_P (i_allocno
)
1235 && ALLOCNO_BAD_SPILL_P (allocno
))
1236 || allocno_pri
> i_allocno_pri
1237 || (allocno_pri
== i_allocno_pri
1238 && (allocno_cost
> i_allocno_cost
1239 || (allocno_cost
== i_allocno_cost
1240 && (ALLOCNO_NUM (allocno
)
1241 > ALLOCNO_NUM (i_allocno
))))))
1243 allocno
= i_allocno
;
1244 allocno_cost
= i_allocno_cost
;
1245 allocno_pri
= i_allocno_pri
;
1248 if (! ALLOCNO_IN_GRAPH_P (allocno_vec
[j
]))
1251 ira_assert (allocno
!= NULL
&& j
>= 0);
1252 cover_class_allocnos_num
[cover_class
] = j
+ 1;
1254 ira_assert (ALLOCNO_IN_GRAPH_P (allocno
)
1255 && ALLOCNO_COVER_CLASS (allocno
) == cover_class
1256 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno
)
1257 + ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE
1259 > ALLOCNO_AVAILABLE_REGS_NUM (allocno
)));
1260 remove_allocno_from_bucket_and_push (allocno
, false);
1262 ira_assert (colorable_allocno_bucket
== NULL
1263 && uncolorable_allocno_bucket
== NULL
);
1264 for (i
= 0; i
< ira_reg_class_cover_size
; i
++)
1266 cover_class
= ira_reg_class_cover
[i
];
1267 ira_assert (uncolorable_allocnos_num
[cover_class
] == 0);
1268 if (uncolorable_allocnos_splay_tree
[cover_class
] != NULL
)
1269 splay_tree_delete (uncolorable_allocnos_splay_tree
[cover_class
]);
1273 /* Pop the coloring stack and assign hard registers to the popped
1276 pop_allocnos_from_stack (void)
1278 ira_allocno_t allocno
;
1279 enum reg_class cover_class
;
1281 for (;VEC_length (ira_allocno_t
, allocno_stack_vec
) != 0;)
1283 allocno
= VEC_pop (ira_allocno_t
, allocno_stack_vec
);
1284 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
1285 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1287 fprintf (ira_dump_file
, " Popping");
1288 print_coalesced_allocno (allocno
);
1289 fprintf (ira_dump_file
, " -- ");
1291 if (cover_class
== NO_REGS
)
1293 ALLOCNO_HARD_REGNO (allocno
) = -1;
1294 ALLOCNO_ASSIGNED_P (allocno
) = true;
1295 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
) == NULL
);
1297 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
) == NULL
);
1298 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1299 fprintf (ira_dump_file
, "assign memory\n");
1301 else if (assign_hard_reg (allocno
, false))
1303 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1304 fprintf (ira_dump_file
, "assign reg %d\n",
1305 ALLOCNO_HARD_REGNO (allocno
));
1307 else if (ALLOCNO_ASSIGNED_P (allocno
))
1309 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1310 fprintf (ira_dump_file
, "spill\n");
1312 ALLOCNO_IN_GRAPH_P (allocno
) = true;
1316 /* Set up number of available hard registers for ALLOCNO. */
1318 setup_allocno_available_regs_num (ira_allocno_t allocno
)
1320 int i
, n
, hard_regs_num
;
1321 enum reg_class cover_class
;
1323 HARD_REG_SET temp_set
;
1325 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
1326 ALLOCNO_AVAILABLE_REGS_NUM (allocno
) = ira_available_class_regs
[cover_class
];
1327 if (cover_class
== NO_REGS
)
1329 CLEAR_HARD_REG_SET (temp_set
);
1330 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) == allocno
);
1331 hard_regs_num
= ira_class_hard_regs_num
[cover_class
];
1332 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
1333 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1335 IOR_HARD_REG_SET (temp_set
, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a
));
1339 for (n
= 0, i
= hard_regs_num
- 1; i
>= 0; i
--)
1340 if (TEST_HARD_REG_BIT (temp_set
, ira_class_hard_regs
[cover_class
][i
]))
1342 if (internal_flag_ira_verbose
> 2 && n
> 0 && ira_dump_file
!= NULL
)
1343 fprintf (ira_dump_file
, " Reg %d of %s has %d regs less\n",
1344 ALLOCNO_REGNO (allocno
), reg_class_names
[cover_class
], n
);
1345 ALLOCNO_AVAILABLE_REGS_NUM (allocno
) -= n
;
1348 /* Set up ALLOCNO_LEFT_CONFLICTS_NUM for ALLOCNO. */
1350 setup_allocno_left_conflicts_num (ira_allocno_t allocno
)
1352 int i
, hard_regs_num
, hard_regno
, conflict_allocnos_size
;
1353 ira_allocno_t a
, conflict_allocno
;
1354 enum reg_class cover_class
;
1355 HARD_REG_SET temp_set
;
1356 ira_allocno_conflict_iterator aci
;
1358 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
1359 hard_regs_num
= ira_class_hard_regs_num
[cover_class
];
1360 CLEAR_HARD_REG_SET (temp_set
);
1361 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) == allocno
);
1362 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
1363 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1365 IOR_HARD_REG_SET (temp_set
, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a
));
1369 AND_HARD_REG_SET (temp_set
, reg_class_contents
[cover_class
]);
1370 AND_COMPL_HARD_REG_SET (temp_set
, ira_no_alloc_regs
);
1371 conflict_allocnos_size
= 0;
1372 if (! hard_reg_set_empty_p (temp_set
))
1373 for (i
= 0; i
< (int) hard_regs_num
; i
++)
1375 hard_regno
= ira_class_hard_regs
[cover_class
][i
];
1376 if (TEST_HARD_REG_BIT (temp_set
, hard_regno
))
1378 conflict_allocnos_size
++;
1379 CLEAR_HARD_REG_BIT (temp_set
, hard_regno
);
1380 if (hard_reg_set_empty_p (temp_set
))
1384 CLEAR_HARD_REG_SET (temp_set
);
1385 if (allocno_coalesced_p
)
1386 bitmap_clear (processed_coalesced_allocno_bitmap
);
1387 if (cover_class
!= NO_REGS
)
1388 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
1389 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1391 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_allocno
, aci
)
1394 = ALLOCNO_FIRST_COALESCED_ALLOCNO (conflict_allocno
);
1395 if (bitmap_bit_p (consideration_allocno_bitmap
,
1396 ALLOCNO_NUM (conflict_allocno
)))
1398 ira_assert (cover_class
1399 == ALLOCNO_COVER_CLASS (conflict_allocno
));
1400 if (allocno_coalesced_p
)
1402 if (bitmap_bit_p (processed_coalesced_allocno_bitmap
,
1403 ALLOCNO_NUM (conflict_allocno
)))
1405 bitmap_set_bit (processed_coalesced_allocno_bitmap
,
1406 ALLOCNO_NUM (conflict_allocno
));
1408 if (! ALLOCNO_ASSIGNED_P (conflict_allocno
))
1409 conflict_allocnos_size
1410 += (ira_reg_class_nregs
1411 [cover_class
][ALLOCNO_MODE (conflict_allocno
)]);
1412 else if ((hard_regno
= ALLOCNO_HARD_REGNO (conflict_allocno
))
1415 int last
= (hard_regno
1417 [hard_regno
][ALLOCNO_MODE (conflict_allocno
)]);
1419 while (hard_regno
< last
)
1421 if (! TEST_HARD_REG_BIT (temp_set
, hard_regno
))
1423 conflict_allocnos_size
++;
1424 SET_HARD_REG_BIT (temp_set
, hard_regno
);
1434 ALLOCNO_LEFT_CONFLICTS_NUM (allocno
) = conflict_allocnos_size
;
1437 /* Put ALLOCNO in a bucket corresponding to its number and size of its
1438 conflicting allocnos and hard registers. */
1440 put_allocno_into_bucket (ira_allocno_t allocno
)
1443 enum reg_class cover_class
;
1445 cover_class
= ALLOCNO_COVER_CLASS (allocno
);
1446 hard_regs_num
= ira_class_hard_regs_num
[cover_class
];
1447 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) != allocno
)
1449 ALLOCNO_IN_GRAPH_P (allocno
) = true;
1450 setup_allocno_left_conflicts_num (allocno
);
1451 setup_allocno_available_regs_num (allocno
);
1452 if (ALLOCNO_LEFT_CONFLICTS_NUM (allocno
)
1453 + ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE (allocno
)]
1454 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno
))
1455 add_allocno_to_bucket (allocno
, &colorable_allocno_bucket
);
1457 add_allocno_to_bucket (allocno
, &uncolorable_allocno_bucket
);
1460 /* The function is used to sort allocnos according to their execution
1463 copy_freq_compare_func (const void *v1p
, const void *v2p
)
1465 ira_copy_t cp1
= *(const ira_copy_t
*) v1p
, cp2
= *(const ira_copy_t
*) v2p
;
1473 /* If freqencies are equal, sort by copies, so that the results of
1474 qsort leave nothing to chance. */
1475 return cp1
->num
- cp2
->num
;
1478 /* Merge two sets of coalesced allocnos given correspondingly by
1479 allocnos A1 and A2 (more accurately merging A2 set into A1
1482 merge_allocnos (ira_allocno_t a1
, ira_allocno_t a2
)
1484 ira_allocno_t a
, first
, last
, next
;
1486 first
= ALLOCNO_FIRST_COALESCED_ALLOCNO (a1
);
1487 if (first
== ALLOCNO_FIRST_COALESCED_ALLOCNO (a2
))
1489 for (last
= a2
, a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a2
);;
1490 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1492 ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) = first
;
1497 next
= ALLOCNO_NEXT_COALESCED_ALLOCNO (first
);
1498 ALLOCNO_NEXT_COALESCED_ALLOCNO (first
) = a2
;
1499 ALLOCNO_NEXT_COALESCED_ALLOCNO (last
) = next
;
1502 /* Return TRUE if there are conflicting allocnos from two sets of
1503 coalesced allocnos given correspondingly by allocnos A1 and A2. If
1504 RELOAD_P is TRUE, we use live ranges to find conflicts because
1505 conflicts are represented only for allocnos of the same cover class
1506 and during the reload pass we coalesce allocnos for sharing stack
1509 coalesced_allocno_conflict_p (ira_allocno_t a1
, ira_allocno_t a2
,
1512 ira_allocno_t a
, conflict_allocno
;
1513 ira_allocno_conflict_iterator aci
;
1515 if (allocno_coalesced_p
)
1517 bitmap_clear (processed_coalesced_allocno_bitmap
);
1518 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a1
);;
1519 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1521 bitmap_set_bit (processed_coalesced_allocno_bitmap
, ALLOCNO_NUM (a
));
1526 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a2
);;
1527 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
1531 for (conflict_allocno
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a1
);;
1533 = ALLOCNO_NEXT_COALESCED_ALLOCNO (conflict_allocno
))
1535 if (allocnos_have_intersected_live_ranges_p (a
,
1538 if (conflict_allocno
== a1
)
1544 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_allocno
, aci
)
1545 if (conflict_allocno
== a1
1546 || (allocno_coalesced_p
1547 && bitmap_bit_p (processed_coalesced_allocno_bitmap
,
1548 ALLOCNO_NUM (conflict_allocno
))))
1557 /* The major function for aggressive allocno coalescing. For the
1558 reload pass (RELOAD_P) we coalesce only spilled allocnos. If some
1559 allocnos have been coalesced, we set up flag
1560 allocno_coalesced_p. */
1562 coalesce_allocnos (bool reload_p
)
1565 ira_copy_t cp
, next_cp
, *sorted_copies
;
1566 enum reg_class cover_class
;
1567 enum machine_mode mode
;
1569 int i
, n
, cp_num
, regno
;
1572 sorted_copies
= (ira_copy_t
*) ira_allocate (ira_copies_num
1573 * sizeof (ira_copy_t
));
1575 /* Collect copies. */
1576 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, j
, bi
)
1578 a
= ira_allocnos
[j
];
1579 regno
= ALLOCNO_REGNO (a
);
1580 if ((! reload_p
&& ALLOCNO_ASSIGNED_P (a
))
1582 && (! ALLOCNO_ASSIGNED_P (a
) || ALLOCNO_HARD_REGNO (a
) >= 0
1583 || (regno
< ira_reg_equiv_len
1584 && (ira_reg_equiv_const
[regno
] != NULL_RTX
1585 || ira_reg_equiv_invariant_p
[regno
])))))
1587 cover_class
= ALLOCNO_COVER_CLASS (a
);
1588 mode
= ALLOCNO_MODE (a
);
1589 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
1593 next_cp
= cp
->next_first_allocno_copy
;
1594 regno
= ALLOCNO_REGNO (cp
->second
);
1596 || (ALLOCNO_COVER_CLASS (cp
->second
) == cover_class
1597 && ALLOCNO_MODE (cp
->second
) == mode
))
1598 && (cp
->insn
!= NULL
|| cp
->constraint_p
)
1599 && ((! reload_p
&& ! ALLOCNO_ASSIGNED_P (cp
->second
))
1601 && ALLOCNO_ASSIGNED_P (cp
->second
)
1602 && ALLOCNO_HARD_REGNO (cp
->second
) < 0
1603 && (regno
>= ira_reg_equiv_len
1604 || (! ira_reg_equiv_invariant_p
[regno
]
1605 && ira_reg_equiv_const
[regno
] == NULL_RTX
)))))
1606 sorted_copies
[cp_num
++] = cp
;
1608 else if (cp
->second
== a
)
1609 next_cp
= cp
->next_second_allocno_copy
;
1614 qsort (sorted_copies
, cp_num
, sizeof (ira_copy_t
), copy_freq_compare_func
);
1615 /* Coalesced copies, most frequently executed first. */
1616 for (; cp_num
!= 0;)
1618 for (i
= 0; i
< cp_num
; i
++)
1620 cp
= sorted_copies
[i
];
1621 if (! coalesced_allocno_conflict_p (cp
->first
, cp
->second
, reload_p
))
1623 allocno_coalesced_p
= true;
1624 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1627 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
1628 cp
->num
, ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
1629 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
),
1631 merge_allocnos (cp
->first
, cp
->second
);
1636 /* Collect the rest of copies. */
1637 for (n
= 0; i
< cp_num
; i
++)
1639 cp
= sorted_copies
[i
];
1640 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (cp
->first
)
1641 != ALLOCNO_FIRST_COALESCED_ALLOCNO (cp
->second
))
1642 sorted_copies
[n
++] = cp
;
1646 ira_free (sorted_copies
);
1649 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
1650 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
1652 color_allocnos (void)
1658 allocno_coalesced_p
= false;
1659 processed_coalesced_allocno_bitmap
= ira_allocate_bitmap ();
1660 if (flag_ira_coalesce
)
1661 coalesce_allocnos (false);
1662 /* Put the allocnos into the corresponding buckets. */
1663 colorable_allocno_bucket
= NULL
;
1664 uncolorable_allocno_bucket
= NULL
;
1665 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1667 a
= ira_allocnos
[i
];
1668 if (ALLOCNO_COVER_CLASS (a
) == NO_REGS
)
1670 ALLOCNO_HARD_REGNO (a
) = -1;
1671 ALLOCNO_ASSIGNED_P (a
) = true;
1672 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
1673 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
1674 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1676 fprintf (ira_dump_file
, " Spill");
1677 print_coalesced_allocno (a
);
1678 fprintf (ira_dump_file
, "\n");
1682 put_allocno_into_bucket (a
);
1684 push_allocnos_to_stack ();
1685 pop_allocnos_from_stack ();
1686 if (flag_ira_coalesce
)
1687 /* We don't need coalesced allocnos for ira_reassign_pseudos. */
1688 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1690 a
= ira_allocnos
[i
];
1691 ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) = a
;
1692 ALLOCNO_NEXT_COALESCED_ALLOCNO (a
) = a
;
1694 ira_free_bitmap (processed_coalesced_allocno_bitmap
);
1695 allocno_coalesced_p
= false;
1700 /* Output information about the loop given by its LOOP_TREE_NODE. */
1702 print_loop_title (ira_loop_tree_node_t loop_tree_node
)
1707 ira_assert (loop_tree_node
->loop
!= NULL
);
1708 fprintf (ira_dump_file
,
1709 "\n Loop %d (parent %d, header bb%d, depth %d)\n all:",
1710 loop_tree_node
->loop
->num
,
1711 (loop_tree_node
->parent
== NULL
1712 ? -1 : loop_tree_node
->parent
->loop
->num
),
1713 loop_tree_node
->loop
->header
->index
,
1714 loop_depth (loop_tree_node
->loop
));
1715 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
1716 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
1717 fprintf (ira_dump_file
, "\n modified regnos:");
1718 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->modified_regnos
, 0, j
, bi
)
1719 fprintf (ira_dump_file
, " %d", j
);
1720 fprintf (ira_dump_file
, "\n border:");
1721 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->border_allocnos
, 0, j
, bi
)
1722 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
1723 fprintf (ira_dump_file
, "\n Pressure:");
1724 for (j
= 0; (int) j
< ira_reg_class_cover_size
; j
++)
1726 enum reg_class cover_class
;
1728 cover_class
= ira_reg_class_cover
[j
];
1729 if (loop_tree_node
->reg_pressure
[cover_class
] == 0)
1731 fprintf (ira_dump_file
, " %s=%d", reg_class_names
[cover_class
],
1732 loop_tree_node
->reg_pressure
[cover_class
]);
1734 fprintf (ira_dump_file
, "\n");
1737 /* Color the allocnos inside loop (in the extreme case it can be all
1738 of the function) given the corresponding LOOP_TREE_NODE. The
1739 function is called for each loop during top-down traverse of the
1742 color_pass (ira_loop_tree_node_t loop_tree_node
)
1744 int regno
, hard_regno
, index
= -1;
1745 int cost
, exit_freq
, enter_freq
;
1748 enum machine_mode mode
;
1749 enum reg_class rclass
, cover_class
;
1750 ira_allocno_t a
, subloop_allocno
;
1751 ira_loop_tree_node_t subloop_node
;
1753 ira_assert (loop_tree_node
->bb
== NULL
);
1754 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
1755 print_loop_title (loop_tree_node
);
1757 bitmap_copy (coloring_allocno_bitmap
, loop_tree_node
->all_allocnos
);
1758 bitmap_copy (consideration_allocno_bitmap
, coloring_allocno_bitmap
);
1759 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
1761 a
= ira_allocnos
[j
];
1762 if (! ALLOCNO_ASSIGNED_P (a
))
1764 bitmap_clear_bit (coloring_allocno_bitmap
, ALLOCNO_NUM (a
));
1766 /* Color all mentioned allocnos including transparent ones. */
1768 /* Process caps. They are processed just once. */
1769 if (flag_ira_algorithm
== IRA_ALGORITHM_MIXED
1770 || flag_ira_algorithm
== IRA_ALGORITHM_REGIONAL
)
1771 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
1773 a
= ira_allocnos
[j
];
1774 if (ALLOCNO_CAP_MEMBER (a
) == NULL
)
1776 /* Remove from processing in the next loop. */
1777 bitmap_clear_bit (consideration_allocno_bitmap
, j
);
1778 rclass
= ALLOCNO_COVER_CLASS (a
);
1779 if ((flag_ira_algorithm
== IRA_ALGORITHM_MIXED
1780 && loop_tree_node
->reg_pressure
[rclass
]
1781 <= ira_available_class_regs
[rclass
]))
1783 mode
= ALLOCNO_MODE (a
);
1784 hard_regno
= ALLOCNO_HARD_REGNO (a
);
1785 if (hard_regno
>= 0)
1787 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
1788 ira_assert (index
>= 0);
1790 regno
= ALLOCNO_REGNO (a
);
1791 subloop_allocno
= ALLOCNO_CAP_MEMBER (a
);
1792 subloop_node
= ALLOCNO_LOOP_TREE_NODE (subloop_allocno
);
1793 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno
));
1794 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
1795 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
1796 if (hard_regno
>= 0)
1797 update_copy_costs (subloop_allocno
, true);
1798 /* We don't need updated costs anymore: */
1799 ira_free_allocno_updated_costs (subloop_allocno
);
1802 /* Update costs of the corresponding allocnos (not caps) in the
1804 for (subloop_node
= loop_tree_node
->subloops
;
1805 subloop_node
!= NULL
;
1806 subloop_node
= subloop_node
->subloop_next
)
1808 ira_assert (subloop_node
->bb
== NULL
);
1809 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
1811 a
= ira_allocnos
[j
];
1812 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
1813 mode
= ALLOCNO_MODE (a
);
1814 rclass
= ALLOCNO_COVER_CLASS (a
);
1815 hard_regno
= ALLOCNO_HARD_REGNO (a
);
1816 if (hard_regno
>= 0)
1818 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
1819 ira_assert (index
>= 0);
1821 regno
= ALLOCNO_REGNO (a
);
1822 /* ??? conflict costs */
1823 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
1824 if (subloop_allocno
== NULL
1825 || ALLOCNO_CAP (subloop_allocno
) != NULL
)
1827 ira_assert (bitmap_bit_p (subloop_node
->all_allocnos
,
1828 ALLOCNO_NUM (subloop_allocno
)));
1829 if (flag_ira_algorithm
== IRA_ALGORITHM_MIXED
1830 && (loop_tree_node
->reg_pressure
[rclass
]
1831 <= ira_available_class_regs
[rclass
]))
1833 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
1835 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
1836 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
1837 if (hard_regno
>= 0)
1838 update_copy_costs (subloop_allocno
, true);
1839 /* We don't need updated costs anymore: */
1840 ira_free_allocno_updated_costs (subloop_allocno
);
1844 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
1845 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
1846 ira_assert (regno
< ira_reg_equiv_len
);
1847 if (ira_reg_equiv_invariant_p
[regno
]
1848 || ira_reg_equiv_const
[regno
] != NULL_RTX
)
1850 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
1852 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
1853 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
1854 if (hard_regno
>= 0)
1855 update_copy_costs (subloop_allocno
, true);
1856 /* We don't need updated costs anymore: */
1857 ira_free_allocno_updated_costs (subloop_allocno
);
1860 else if (hard_regno
< 0)
1862 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
1863 -= ((ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
)
1864 + (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
));
1868 cover_class
= ALLOCNO_COVER_CLASS (subloop_allocno
);
1869 cost
= (ira_register_move_cost
[mode
][rclass
][rclass
]
1870 * (exit_freq
+ enter_freq
));
1871 ira_allocate_and_set_or_copy_costs
1872 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
), cover_class
,
1873 ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno
),
1874 ALLOCNO_HARD_REG_COSTS (subloop_allocno
));
1875 ira_allocate_and_set_or_copy_costs
1876 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
),
1878 ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno
));
1879 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
] -= cost
;
1880 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
)[index
]
1882 if (ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno
)
1883 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
])
1884 ALLOCNO_UPDATED_COVER_CLASS_COST (subloop_allocno
)
1885 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
];
1886 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
1887 += (ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
1888 + ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
);
1894 /* Initialize the common data for coloring and calls functions to do
1895 Chaitin-Briggs and regional coloring. */
1899 coloring_allocno_bitmap
= ira_allocate_bitmap ();
1900 allocnos_for_spilling
1901 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
1902 * ira_allocnos_num
);
1903 splay_tree_node_pool
= create_alloc_pool ("splay tree nodes",
1904 sizeof (struct splay_tree_node_s
),
1906 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
1907 fprintf (ira_dump_file
, "\n**** Allocnos coloring:\n\n");
1909 ira_traverse_loop_tree (false, ira_loop_tree_root
, color_pass
, NULL
);
1911 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
1912 ira_print_disposition (ira_dump_file
);
1914 free_alloc_pool (splay_tree_node_pool
);
1915 ira_free_bitmap (coloring_allocno_bitmap
);
1916 ira_free (allocnos_for_spilling
);
1921 /* Move spill/restore code, which are to be generated in ira-emit.c,
1922 to less frequent points (if it is profitable) by reassigning some
1923 allocnos (in loop with subloops containing in another loop) to
1924 memory which results in longer live-range where the corresponding
1925 pseudo-registers will be in memory. */
1927 move_spill_restore (void)
1929 int cost
, regno
, hard_regno
, hard_regno2
, index
;
1931 int enter_freq
, exit_freq
;
1932 enum machine_mode mode
;
1933 enum reg_class rclass
;
1934 ira_allocno_t a
, parent_allocno
, subloop_allocno
;
1935 ira_loop_tree_node_t parent
, loop_node
, subloop_node
;
1936 ira_allocno_iterator ai
;
1941 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
1942 fprintf (ira_dump_file
, "New iteration of spill/restore move\n");
1943 FOR_EACH_ALLOCNO (a
, ai
)
1945 regno
= ALLOCNO_REGNO (a
);
1946 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
1947 if (ALLOCNO_CAP_MEMBER (a
) != NULL
1948 || ALLOCNO_CAP (a
) != NULL
1949 || (hard_regno
= ALLOCNO_HARD_REGNO (a
)) < 0
1950 || loop_node
->children
== NULL
1951 /* don't do the optimization because it can create
1952 copies and the reload pass can spill the allocno set
1953 by copy although the allocno will not get memory
1955 || ira_reg_equiv_invariant_p
[regno
]
1956 || ira_reg_equiv_const
[regno
] != NULL_RTX
1957 || !bitmap_bit_p (loop_node
->border_allocnos
, ALLOCNO_NUM (a
)))
1959 mode
= ALLOCNO_MODE (a
);
1960 rclass
= ALLOCNO_COVER_CLASS (a
);
1961 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
1962 ira_assert (index
>= 0);
1963 cost
= (ALLOCNO_MEMORY_COST (a
)
1964 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
1965 ? ALLOCNO_COVER_CLASS_COST (a
)
1966 : ALLOCNO_HARD_REG_COSTS (a
)[index
]));
1967 for (subloop_node
= loop_node
->subloops
;
1968 subloop_node
!= NULL
;
1969 subloop_node
= subloop_node
->subloop_next
)
1971 ira_assert (subloop_node
->bb
== NULL
);
1972 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
1973 if (subloop_allocno
== NULL
)
1975 /* We have accumulated cost. To get the real cost of
1976 allocno usage in the loop we should subtract costs of
1977 the subloop allocnos. */
1978 cost
-= (ALLOCNO_MEMORY_COST (subloop_allocno
)
1979 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno
) == NULL
1980 ? ALLOCNO_COVER_CLASS_COST (subloop_allocno
)
1981 : ALLOCNO_HARD_REG_COSTS (subloop_allocno
)[index
]));
1982 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
1983 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
1984 if ((hard_regno2
= ALLOCNO_HARD_REGNO (subloop_allocno
)) < 0)
1985 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
1986 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
1990 += (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
1991 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
1992 if (hard_regno2
!= hard_regno
)
1993 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
1994 * (exit_freq
+ enter_freq
));
1997 if ((parent
= loop_node
->parent
) != NULL
1998 && (parent_allocno
= parent
->regno_allocno_map
[regno
]) != NULL
)
2000 exit_freq
= ira_loop_edge_freq (loop_node
, regno
, true);
2001 enter_freq
= ira_loop_edge_freq (loop_node
, regno
, false);
2002 if ((hard_regno2
= ALLOCNO_HARD_REGNO (parent_allocno
)) < 0)
2003 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
2004 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
2008 += (ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
2009 + ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
);
2010 if (hard_regno2
!= hard_regno
)
2011 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
2012 * (exit_freq
+ enter_freq
));
2017 ALLOCNO_HARD_REGNO (a
) = -1;
2018 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2022 " Moving spill/restore for a%dr%d up from loop %d",
2023 ALLOCNO_NUM (a
), regno
, loop_node
->loop
->num
);
2024 fprintf (ira_dump_file
, " - profit %d\n", -cost
);
2036 /* Update current hard reg costs and current conflict hard reg costs
2037 for allocno A. It is done by processing its copies containing
2038 other allocnos already assigned. */
2040 update_curr_costs (ira_allocno_t a
)
2042 int i
, hard_regno
, cost
;
2043 enum machine_mode mode
;
2044 enum reg_class cover_class
, rclass
;
2045 ira_allocno_t another_a
;
2046 ira_copy_t cp
, next_cp
;
2048 ira_assert (! ALLOCNO_ASSIGNED_P (a
));
2049 cover_class
= ALLOCNO_COVER_CLASS (a
);
2050 if (cover_class
== NO_REGS
)
2052 mode
= ALLOCNO_MODE (a
);
2053 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
2057 next_cp
= cp
->next_first_allocno_copy
;
2058 another_a
= cp
->second
;
2060 else if (cp
->second
== a
)
2062 next_cp
= cp
->next_second_allocno_copy
;
2063 another_a
= cp
->first
;
2067 if (cover_class
!= ALLOCNO_COVER_CLASS (another_a
)
2068 || ! ALLOCNO_ASSIGNED_P (another_a
)
2069 || (hard_regno
= ALLOCNO_HARD_REGNO (another_a
)) < 0)
2071 rclass
= REGNO_REG_CLASS (hard_regno
);
2072 i
= ira_class_hard_reg_index
[cover_class
][hard_regno
];
2073 ira_assert (i
>= 0);
2074 cost
= (cp
->first
== a
2075 ? ira_register_move_cost
[mode
][rclass
][cover_class
]
2076 : ira_register_move_cost
[mode
][cover_class
][rclass
]);
2077 ira_allocate_and_set_or_copy_costs
2078 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
),
2079 cover_class
, ALLOCNO_COVER_CLASS_COST (a
),
2080 ALLOCNO_HARD_REG_COSTS (a
));
2081 ira_allocate_and_set_or_copy_costs
2082 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
2083 cover_class
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
2084 ALLOCNO_UPDATED_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
2085 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
2089 /* Map: allocno number -> allocno priority. */
2090 static int *allocno_priorities
;
2092 /* Set up priorities for N allocnos in array
2093 CONSIDERATION_ALLOCNOS. */
2095 setup_allocno_priorities (ira_allocno_t
*consideration_allocnos
, int n
)
2097 int i
, length
, nrefs
, priority
, max_priority
, mult
;
2101 for (i
= 0; i
< n
; i
++)
2103 a
= consideration_allocnos
[i
];
2104 nrefs
= ALLOCNO_NREFS (a
);
2105 ira_assert (nrefs
>= 0);
2106 mult
= floor_log2 (ALLOCNO_NREFS (a
)) + 1;
2107 ira_assert (mult
>= 0);
2108 allocno_priorities
[ALLOCNO_NUM (a
)]
2111 * (ALLOCNO_MEMORY_COST (a
) - ALLOCNO_COVER_CLASS_COST (a
))
2112 * ira_reg_class_nregs
[ALLOCNO_COVER_CLASS (a
)][ALLOCNO_MODE (a
)]);
2114 priority
= -priority
;
2115 if (max_priority
< priority
)
2116 max_priority
= priority
;
2118 mult
= max_priority
== 0 ? 1 : INT_MAX
/ max_priority
;
2119 for (i
= 0; i
< n
; i
++)
2121 a
= consideration_allocnos
[i
];
2122 length
= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2125 allocno_priorities
[ALLOCNO_NUM (a
)]
2126 = allocno_priorities
[ALLOCNO_NUM (a
)] * mult
/ length
;
2130 /* Sort allocnos according to their priorities which are calculated
2131 analogous to ones in file `global.c'. */
2133 allocno_priority_compare_func (const void *v1p
, const void *v2p
)
2135 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2136 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2139 pri1
= allocno_priorities
[ALLOCNO_NUM (a1
)];
2140 pri2
= allocno_priorities
[ALLOCNO_NUM (a2
)];
2144 /* If regs are equally good, sort by allocnos, so that the results of
2145 qsort leave nothing to chance. */
2146 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2149 /* Try to assign hard registers to the unassigned allocnos and
2150 allocnos conflicting with them or conflicting with allocnos whose
2151 regno >= START_REGNO. The function is called after ira_flattening,
2152 so more allocnos (including ones created in ira-emit.c) will have a
2153 chance to get a hard register. We use simple assignment algorithm
2154 based on priorities. */
2156 ira_reassign_conflict_allocnos (int start_regno
)
2158 int i
, allocnos_to_color_num
;
2159 ira_allocno_t a
, conflict_a
;
2160 ira_allocno_conflict_iterator aci
;
2161 enum reg_class cover_class
;
2162 bitmap allocnos_to_color
;
2163 ira_allocno_iterator ai
;
2165 allocnos_to_color
= ira_allocate_bitmap ();
2166 allocnos_to_color_num
= 0;
2167 FOR_EACH_ALLOCNO (a
, ai
)
2169 if (! ALLOCNO_ASSIGNED_P (a
)
2170 && ! bitmap_bit_p (allocnos_to_color
, ALLOCNO_NUM (a
)))
2172 if (ALLOCNO_COVER_CLASS (a
) != NO_REGS
)
2173 sorted_allocnos
[allocnos_to_color_num
++] = a
;
2176 ALLOCNO_ASSIGNED_P (a
) = true;
2177 ALLOCNO_HARD_REGNO (a
) = -1;
2178 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
2179 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
2181 bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (a
));
2183 if (ALLOCNO_REGNO (a
) < start_regno
2184 || (cover_class
= ALLOCNO_COVER_CLASS (a
)) == NO_REGS
)
2186 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_a
, aci
)
2188 ira_assert (cover_class
== ALLOCNO_COVER_CLASS (conflict_a
));
2189 if (bitmap_bit_p (allocnos_to_color
, ALLOCNO_NUM (conflict_a
)))
2191 bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (conflict_a
));
2192 sorted_allocnos
[allocnos_to_color_num
++] = conflict_a
;
2195 ira_free_bitmap (allocnos_to_color
);
2196 if (allocnos_to_color_num
> 1)
2198 setup_allocno_priorities (sorted_allocnos
, allocnos_to_color_num
);
2199 qsort (sorted_allocnos
, allocnos_to_color_num
, sizeof (ira_allocno_t
),
2200 allocno_priority_compare_func
);
2202 for (i
= 0; i
< allocnos_to_color_num
; i
++)
2204 a
= sorted_allocnos
[i
];
2205 ALLOCNO_ASSIGNED_P (a
) = false;
2206 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
2207 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
2208 update_curr_costs (a
);
2210 for (i
= 0; i
< allocnos_to_color_num
; i
++)
2212 a
= sorted_allocnos
[i
];
2213 if (assign_hard_reg (a
, true))
2215 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2218 " Secondary allocation: assign hard reg %d to reg %d\n",
2219 ALLOCNO_HARD_REGNO (a
), ALLOCNO_REGNO (a
));
2226 /* This page contains code to coalesce memory stack slots used by
2227 spilled allocnos. This results in smaller stack frame, better data
2228 locality, and in smaller code for some architectures like
2229 x86/x86_64 where insn size depends on address displacement value.
2230 On the other hand, it can worsen insn scheduling after the RA but
2231 in practice it is less important than smaller stack frames. */
2233 /* Usage cost and order number of coalesced allocno set to which
2234 given pseudo register belongs to. */
2235 static int *regno_coalesced_allocno_cost
;
2236 static int *regno_coalesced_allocno_num
;
2238 /* Sort pseudos according frequencies of coalesced allocno sets they
2239 belong to (putting most frequently ones first), and according to
2240 coalesced allocno set order numbers. */
2242 coalesced_pseudo_reg_freq_compare (const void *v1p
, const void *v2p
)
2244 const int regno1
= *(const int *) v1p
;
2245 const int regno2
= *(const int *) v2p
;
2248 if ((diff
= (regno_coalesced_allocno_cost
[regno2
]
2249 - regno_coalesced_allocno_cost
[regno1
])) != 0)
2251 if ((diff
= (regno_coalesced_allocno_num
[regno1
]
2252 - regno_coalesced_allocno_num
[regno2
])) != 0)
2254 return regno1
- regno2
;
2257 /* Widest width in which each pseudo reg is referred to (via subreg).
2258 It is used for sorting pseudo registers. */
2259 static unsigned int *regno_max_ref_width
;
2261 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
2262 #ifdef STACK_GROWS_DOWNWARD
2263 # undef STACK_GROWS_DOWNWARD
2264 # define STACK_GROWS_DOWNWARD 1
2266 # define STACK_GROWS_DOWNWARD 0
2269 /* Sort pseudos according their slot numbers (putting ones with
2270 smaller numbers first, or last when the frame pointer is not
2273 coalesced_pseudo_reg_slot_compare (const void *v1p
, const void *v2p
)
2275 const int regno1
= *(const int *) v1p
;
2276 const int regno2
= *(const int *) v2p
;
2277 ira_allocno_t a1
= ira_regno_allocno_map
[regno1
];
2278 ira_allocno_t a2
= ira_regno_allocno_map
[regno2
];
2279 int diff
, slot_num1
, slot_num2
;
2280 int total_size1
, total_size2
;
2282 if (a1
== NULL
|| ALLOCNO_HARD_REGNO (a1
) >= 0)
2284 if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
2285 return regno1
- regno2
;
2288 else if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
2290 slot_num1
= -ALLOCNO_HARD_REGNO (a1
);
2291 slot_num2
= -ALLOCNO_HARD_REGNO (a2
);
2292 if ((diff
= slot_num1
- slot_num2
) != 0)
2293 return (frame_pointer_needed
2294 || !FRAME_GROWS_DOWNWARD
== STACK_GROWS_DOWNWARD
? diff
: -diff
);
2295 total_size1
= MAX (PSEUDO_REGNO_BYTES (regno1
), regno_max_ref_width
[regno1
]);
2296 total_size2
= MAX (PSEUDO_REGNO_BYTES (regno2
), regno_max_ref_width
[regno2
]);
2297 if ((diff
= total_size2
- total_size1
) != 0)
2299 return regno1
- regno2
;
2302 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
2303 for coalesced allocno sets containing allocnos with their regnos
2304 given in array PSEUDO_REGNOS of length N. */
2306 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos
, int n
)
2308 int i
, num
, regno
, cost
;
2309 ira_allocno_t allocno
, a
;
2311 for (num
= i
= 0; i
< n
; i
++)
2313 regno
= pseudo_regnos
[i
];
2314 allocno
= ira_regno_allocno_map
[regno
];
2315 if (allocno
== NULL
)
2317 regno_coalesced_allocno_cost
[regno
] = 0;
2318 regno_coalesced_allocno_num
[regno
] = ++num
;
2321 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) != allocno
)
2324 for (cost
= 0, a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
2325 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
2327 cost
+= ALLOCNO_FREQ (a
);
2331 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
2332 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
2334 regno_coalesced_allocno_num
[ALLOCNO_REGNO (a
)] = num
;
2335 regno_coalesced_allocno_cost
[ALLOCNO_REGNO (a
)] = cost
;
2342 /* Collect spilled allocnos representing coalesced allocno sets (the
2343 first coalesced allocno). The collected allocnos are returned
2344 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
2345 number of the collected allocnos. The allocnos are given by their
2346 regnos in array PSEUDO_REGNOS of length N. */
2348 collect_spilled_coalesced_allocnos (int *pseudo_regnos
, int n
,
2349 ira_allocno_t
*spilled_coalesced_allocnos
)
2352 ira_allocno_t allocno
;
2354 for (num
= i
= 0; i
< n
; i
++)
2356 regno
= pseudo_regnos
[i
];
2357 allocno
= ira_regno_allocno_map
[regno
];
2358 if (allocno
== NULL
|| ALLOCNO_HARD_REGNO (allocno
) >= 0
2359 || ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) != allocno
)
2361 spilled_coalesced_allocnos
[num
++] = allocno
;
2366 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
2367 given slot contains live ranges of coalesced allocnos assigned to
2369 static allocno_live_range_t
*slot_coalesced_allocnos_live_ranges
;
2371 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
2372 ranges intersected with live ranges of coalesced allocnos assigned
2373 to slot with number N. */
2375 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno
, int n
)
2379 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
2380 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
2382 if (ira_allocno_live_ranges_intersect_p
2383 (slot_coalesced_allocnos_live_ranges
[n
], ALLOCNO_LIVE_RANGES (a
)))
2391 /* Update live ranges of slot to which coalesced allocnos represented
2392 by ALLOCNO were assigned. */
2394 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno
)
2398 allocno_live_range_t r
;
2400 n
= ALLOCNO_TEMP (allocno
);
2401 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
2402 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
2404 r
= ira_copy_allocno_live_range_list (ALLOCNO_LIVE_RANGES (a
));
2405 slot_coalesced_allocnos_live_ranges
[n
]
2406 = ira_merge_allocno_live_ranges
2407 (slot_coalesced_allocnos_live_ranges
[n
], r
);
2413 /* We have coalesced allocnos involving in copies. Coalesce allocnos
2414 further in order to share the same memory stack slot. Allocnos
2415 representing sets of allocnos coalesced before the call are given
2416 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
2417 some allocnos were coalesced in the function. */
2419 coalesce_spill_slots (ira_allocno_t
*spilled_coalesced_allocnos
, int num
)
2421 int i
, j
, n
, last_coalesced_allocno_num
;
2422 ira_allocno_t allocno
, a
;
2423 bool merged_p
= false;
2425 slot_coalesced_allocnos_live_ranges
2426 = (allocno_live_range_t
*) ira_allocate (sizeof (allocno_live_range_t
)
2427 * ira_allocnos_num
);
2428 memset (slot_coalesced_allocnos_live_ranges
, 0,
2429 sizeof (allocno_live_range_t
) * ira_allocnos_num
);
2430 last_coalesced_allocno_num
= 0;
2431 /* Coalesce non-conflicting spilled allocnos preferring most
2433 for (i
= 0; i
< num
; i
++)
2435 allocno
= spilled_coalesced_allocnos
[i
];
2436 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) != allocno
2437 || (ALLOCNO_REGNO (allocno
) < ira_reg_equiv_len
2438 && (ira_reg_equiv_const
[ALLOCNO_REGNO (allocno
)] != NULL_RTX
2439 || ira_reg_equiv_invariant_p
[ALLOCNO_REGNO (allocno
)])))
2441 for (j
= 0; j
< i
; j
++)
2443 a
= spilled_coalesced_allocnos
[j
];
2444 n
= ALLOCNO_TEMP (a
);
2445 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) == a
2446 && (ALLOCNO_REGNO (a
) >= ira_reg_equiv_len
2447 || (! ira_reg_equiv_invariant_p
[ALLOCNO_REGNO (a
)]
2448 && ira_reg_equiv_const
[ALLOCNO_REGNO (a
)] == NULL_RTX
))
2449 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno
, n
))
2454 /* No coalescing: set up number for coalesced allocnos
2455 represented by ALLOCNO. */
2456 ALLOCNO_TEMP (allocno
) = last_coalesced_allocno_num
++;
2457 setup_slot_coalesced_allocno_live_ranges (allocno
);
2461 allocno_coalesced_p
= true;
2463 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2464 fprintf (ira_dump_file
,
2465 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
2466 ALLOCNO_NUM (allocno
), ALLOCNO_REGNO (allocno
),
2467 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
2468 ALLOCNO_TEMP (allocno
) = ALLOCNO_TEMP (a
);
2469 setup_slot_coalesced_allocno_live_ranges (allocno
);
2470 merge_allocnos (a
, allocno
);
2471 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) == a
);
2474 for (i
= 0; i
< ira_allocnos_num
; i
++)
2475 ira_finish_allocno_live_range_list
2476 (slot_coalesced_allocnos_live_ranges
[i
]);
2477 ira_free (slot_coalesced_allocnos_live_ranges
);
2481 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
2482 subsequent assigning stack slots to them in the reload pass. To do
2483 this we coalesce spilled allocnos first to decrease the number of
2484 memory-memory move insns. This function is called by the
2487 ira_sort_regnos_for_alter_reg (int *pseudo_regnos
, int n
,
2488 unsigned int *reg_max_ref_width
)
2490 int max_regno
= max_reg_num ();
2491 int i
, regno
, num
, slot_num
;
2492 ira_allocno_t allocno
, a
;
2493 ira_allocno_iterator ai
;
2494 ira_allocno_t
*spilled_coalesced_allocnos
;
2496 processed_coalesced_allocno_bitmap
= ira_allocate_bitmap ();
2497 /* Set up allocnos can be coalesced. */
2498 coloring_allocno_bitmap
= ira_allocate_bitmap ();
2499 for (i
= 0; i
< n
; i
++)
2501 regno
= pseudo_regnos
[i
];
2502 allocno
= ira_regno_allocno_map
[regno
];
2503 if (allocno
!= NULL
)
2504 bitmap_set_bit (coloring_allocno_bitmap
,
2505 ALLOCNO_NUM (allocno
));
2507 allocno_coalesced_p
= false;
2508 coalesce_allocnos (true);
2509 ira_free_bitmap (coloring_allocno_bitmap
);
2510 regno_coalesced_allocno_cost
2511 = (int *) ira_allocate (max_regno
* sizeof (int));
2512 regno_coalesced_allocno_num
2513 = (int *) ira_allocate (max_regno
* sizeof (int));
2514 memset (regno_coalesced_allocno_num
, 0, max_regno
* sizeof (int));
2515 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
2516 /* Sort regnos according frequencies of the corresponding coalesced
2518 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_freq_compare
);
2519 spilled_coalesced_allocnos
2520 = (ira_allocno_t
*) ira_allocate (ira_allocnos_num
2521 * sizeof (ira_allocno_t
));
2522 /* Collect allocnos representing the spilled coalesced allocno
2524 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
2525 spilled_coalesced_allocnos
);
2526 if (flag_ira_share_spill_slots
2527 && coalesce_spill_slots (spilled_coalesced_allocnos
, num
))
2529 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
2530 qsort (pseudo_regnos
, n
, sizeof (int),
2531 coalesced_pseudo_reg_freq_compare
);
2532 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
2533 spilled_coalesced_allocnos
);
2535 ira_free_bitmap (processed_coalesced_allocno_bitmap
);
2536 allocno_coalesced_p
= false;
2537 /* Assign stack slot numbers to spilled allocno sets, use smaller
2538 numbers for most frequently used coalesced allocnos. -1 is
2539 reserved for dynamic search of stack slots for pseudos spilled by
2542 for (i
= 0; i
< num
; i
++)
2544 allocno
= spilled_coalesced_allocnos
[i
];
2545 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno
) != allocno
2546 || ALLOCNO_HARD_REGNO (allocno
) >= 0
2547 || (ALLOCNO_REGNO (allocno
) < ira_reg_equiv_len
2548 && (ira_reg_equiv_const
[ALLOCNO_REGNO (allocno
)] != NULL_RTX
2549 || ira_reg_equiv_invariant_p
[ALLOCNO_REGNO (allocno
)])))
2551 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2552 fprintf (ira_dump_file
, " Slot %d (freq,size):", slot_num
);
2554 for (a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno
);;
2555 a
= ALLOCNO_NEXT_COALESCED_ALLOCNO (a
))
2557 ira_assert (ALLOCNO_HARD_REGNO (a
) < 0);
2558 ALLOCNO_HARD_REGNO (a
) = -slot_num
;
2559 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2560 fprintf (ira_dump_file
, " a%dr%d(%d,%d)",
2561 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
), ALLOCNO_FREQ (a
),
2562 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a
)),
2563 reg_max_ref_width
[ALLOCNO_REGNO (a
)]));
2568 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2569 fprintf (ira_dump_file
, "\n");
2571 ira_spilled_reg_stack_slots_num
= slot_num
- 1;
2572 ira_free (spilled_coalesced_allocnos
);
2573 /* Sort regnos according the slot numbers. */
2574 regno_max_ref_width
= reg_max_ref_width
;
2575 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_slot_compare
);
2576 /* Uncoalesce allocnos which is necessary for (re)assigning during
2578 FOR_EACH_ALLOCNO (a
, ai
)
2580 ALLOCNO_FIRST_COALESCED_ALLOCNO (a
) = a
;
2581 ALLOCNO_NEXT_COALESCED_ALLOCNO (a
) = a
;
2583 ira_free (regno_coalesced_allocno_num
);
2584 ira_free (regno_coalesced_allocno_cost
);
2589 /* This page contains code used by the reload pass to improve the
2592 /* The function is called from reload to mark changes in the
2593 allocation of REGNO made by the reload. Remember that reg_renumber
2594 reflects the change result. */
2596 ira_mark_allocation_change (int regno
)
2598 ira_allocno_t a
= ira_regno_allocno_map
[regno
];
2599 int old_hard_regno
, hard_regno
, cost
;
2600 enum reg_class cover_class
= ALLOCNO_COVER_CLASS (a
);
2602 ira_assert (a
!= NULL
);
2603 hard_regno
= reg_renumber
[regno
];
2604 if ((old_hard_regno
= ALLOCNO_HARD_REGNO (a
)) == hard_regno
)
2606 if (old_hard_regno
< 0)
2607 cost
= -ALLOCNO_MEMORY_COST (a
);
2610 ira_assert (ira_class_hard_reg_index
[cover_class
][old_hard_regno
] >= 0);
2611 cost
= -(ALLOCNO_HARD_REG_COSTS (a
) == NULL
2612 ? ALLOCNO_COVER_CLASS_COST (a
)
2613 : ALLOCNO_HARD_REG_COSTS (a
)
2614 [ira_class_hard_reg_index
[cover_class
][old_hard_regno
]]);
2615 update_copy_costs (a
, false);
2617 ira_overall_cost
-= cost
;
2618 ALLOCNO_HARD_REGNO (a
) = hard_regno
;
2621 ALLOCNO_HARD_REGNO (a
) = -1;
2622 cost
+= ALLOCNO_MEMORY_COST (a
);
2624 else if (ira_class_hard_reg_index
[cover_class
][hard_regno
] >= 0)
2626 cost
+= (ALLOCNO_HARD_REG_COSTS (a
) == NULL
2627 ? ALLOCNO_COVER_CLASS_COST (a
)
2628 : ALLOCNO_HARD_REG_COSTS (a
)
2629 [ira_class_hard_reg_index
[cover_class
][hard_regno
]]);
2630 update_copy_costs (a
, true);
2633 /* Reload changed class of the allocno. */
2635 ira_overall_cost
+= cost
;
2638 /* This function is called when reload deletes memory-memory move. In
2639 this case we marks that the allocation of the corresponding
2640 allocnos should be not changed in future. Otherwise we risk to get
2643 ira_mark_memory_move_deletion (int dst_regno
, int src_regno
)
2645 ira_allocno_t dst
= ira_regno_allocno_map
[dst_regno
];
2646 ira_allocno_t src
= ira_regno_allocno_map
[src_regno
];
2648 ira_assert (dst
!= NULL
&& src
!= NULL
2649 && ALLOCNO_HARD_REGNO (dst
) < 0
2650 && ALLOCNO_HARD_REGNO (src
) < 0);
2651 ALLOCNO_DONT_REASSIGN_P (dst
) = true;
2652 ALLOCNO_DONT_REASSIGN_P (src
) = true;
2655 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
2656 allocno A and return TRUE in the case of success. That is an
2657 analog of retry_global_alloc for IRA. */
2659 allocno_reload_assign (ira_allocno_t a
, HARD_REG_SET forbidden_regs
)
2662 enum reg_class cover_class
;
2663 int regno
= ALLOCNO_REGNO (a
);
2665 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a
), forbidden_regs
);
2666 if (! flag_caller_saves
&& ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
2667 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a
), call_used_reg_set
);
2668 ALLOCNO_ASSIGNED_P (a
) = false;
2669 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
2670 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
2671 cover_class
= ALLOCNO_COVER_CLASS (a
);
2672 update_curr_costs (a
);
2673 assign_hard_reg (a
, true);
2674 hard_regno
= ALLOCNO_HARD_REGNO (a
);
2675 reg_renumber
[regno
] = hard_regno
;
2677 ALLOCNO_HARD_REGNO (a
) = -1;
2680 ira_assert (ira_class_hard_reg_index
[cover_class
][hard_regno
] >= 0);
2681 ira_overall_cost
-= (ALLOCNO_MEMORY_COST (a
)
2682 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
2683 ? ALLOCNO_COVER_CLASS_COST (a
)
2684 : ALLOCNO_HARD_REG_COSTS (a
)
2685 [ira_class_hard_reg_index
2686 [cover_class
][hard_regno
]]));
2687 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0
2688 && ! ira_hard_reg_not_in_set_p (hard_regno
, ALLOCNO_MODE (a
),
2691 ira_assert (flag_caller_saves
);
2692 caller_save_needed
= 1;
2696 /* If we found a hard register, modify the RTL for the pseudo
2697 register to show the hard register, and mark the pseudo register
2699 if (reg_renumber
[regno
] >= 0)
2701 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2702 fprintf (ira_dump_file
, ": reassign to %d\n", reg_renumber
[regno
]);
2703 SET_REGNO (regno_reg_rtx
[regno
], reg_renumber
[regno
]);
2704 mark_home_live (regno
);
2706 else if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2707 fprintf (ira_dump_file
, "\n");
2709 return reg_renumber
[regno
] >= 0;
2712 /* Sort pseudos according their usage frequencies (putting most
2713 frequently ones first). */
2715 pseudo_reg_compare (const void *v1p
, const void *v2p
)
2717 int regno1
= *(const int *) v1p
;
2718 int regno2
= *(const int *) v2p
;
2721 if ((diff
= REG_FREQ (regno2
) - REG_FREQ (regno1
)) != 0)
2723 return regno1
- regno2
;
2726 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
2727 NUM of them) or spilled pseudos conflicting with pseudos in
2728 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
2729 allocation has been changed. The function doesn't use
2730 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
2731 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
2732 is called by the reload pass at the end of each reload
2735 ira_reassign_pseudos (int *spilled_pseudo_regs
, int num
,
2736 HARD_REG_SET bad_spill_regs
,
2737 HARD_REG_SET
*pseudo_forbidden_regs
,
2738 HARD_REG_SET
*pseudo_previous_regs
, bitmap spilled
)
2742 ira_allocno_t a
, conflict_a
;
2743 HARD_REG_SET forbidden_regs
;
2744 ira_allocno_conflict_iterator aci
;
2747 qsort (spilled_pseudo_regs
, num
, sizeof (int), pseudo_reg_compare
);
2749 /* Try to assign hard registers to pseudos from
2750 SPILLED_PSEUDO_REGS. */
2751 for (m
= i
= 0; i
< num
; i
++)
2753 regno
= spilled_pseudo_regs
[i
];
2754 COPY_HARD_REG_SET (forbidden_regs
, bad_spill_regs
);
2755 IOR_HARD_REG_SET (forbidden_regs
, pseudo_forbidden_regs
[regno
]);
2756 IOR_HARD_REG_SET (forbidden_regs
, pseudo_previous_regs
[regno
]);
2757 gcc_assert (reg_renumber
[regno
] < 0);
2758 a
= ira_regno_allocno_map
[regno
];
2759 ira_mark_allocation_change (regno
);
2760 ira_assert (reg_renumber
[regno
] < 0);
2761 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2762 fprintf (ira_dump_file
,
2763 " Spill %d(a%d), cost=%d", regno
, ALLOCNO_NUM (a
),
2764 ALLOCNO_MEMORY_COST (a
)
2765 - ALLOCNO_COVER_CLASS_COST (a
));
2766 allocno_reload_assign (a
, forbidden_regs
);
2767 if (reg_renumber
[regno
] >= 0)
2769 CLEAR_REGNO_REG_SET (spilled
, regno
);
2773 spilled_pseudo_regs
[m
++] = regno
;
2777 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2779 fprintf (ira_dump_file
, " Spilled regs");
2780 for (i
= 0; i
< m
; i
++)
2781 fprintf (ira_dump_file
, " %d", spilled_pseudo_regs
[i
]);
2782 fprintf (ira_dump_file
, "\n");
2784 /* Try to assign hard registers to pseudos conflicting with ones
2785 from SPILLED_PSEUDO_REGS. */
2786 for (i
= n
= 0; i
< m
; i
++)
2788 regno
= spilled_pseudo_regs
[i
];
2789 a
= ira_regno_allocno_map
[regno
];
2790 FOR_EACH_ALLOCNO_CONFLICT (a
, conflict_a
, aci
)
2791 if (ALLOCNO_HARD_REGNO (conflict_a
) < 0
2792 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a
)
2793 && ! bitmap_bit_p (consideration_allocno_bitmap
,
2794 ALLOCNO_NUM (conflict_a
)))
2796 sorted_allocnos
[n
++] = conflict_a
;
2797 bitmap_set_bit (consideration_allocno_bitmap
,
2798 ALLOCNO_NUM (conflict_a
));
2803 setup_allocno_priorities (sorted_allocnos
, n
);
2804 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
2805 allocno_priority_compare_func
);
2806 for (i
= 0; i
< n
; i
++)
2808 a
= sorted_allocnos
[i
];
2809 regno
= ALLOCNO_REGNO (a
);
2810 COPY_HARD_REG_SET (forbidden_regs
, bad_spill_regs
);
2811 IOR_HARD_REG_SET (forbidden_regs
, pseudo_forbidden_regs
[regno
]);
2812 IOR_HARD_REG_SET (forbidden_regs
, pseudo_previous_regs
[regno
]);
2813 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2814 fprintf (ira_dump_file
,
2815 " Try assign %d(a%d), cost=%d",
2816 regno
, ALLOCNO_NUM (a
),
2817 ALLOCNO_MEMORY_COST (a
)
2818 - ALLOCNO_COVER_CLASS_COST (a
));
2819 if (allocno_reload_assign (a
, forbidden_regs
))
2822 bitmap_clear_bit (spilled
, regno
);
2829 /* The function is called by reload and returns already allocated
2830 stack slot (if any) for REGNO with given INHERENT_SIZE and
2831 TOTAL_SIZE. In the case of failure to find a slot which can be
2832 used for REGNO, the function returns NULL. */
2834 ira_reuse_stack_slot (int regno
, unsigned int inherent_size
,
2835 unsigned int total_size
)
2838 int slot_num
, best_slot_num
;
2839 int cost
, best_cost
;
2840 ira_copy_t cp
, next_cp
;
2841 ira_allocno_t another_allocno
, allocno
= ira_regno_allocno_map
[regno
];
2844 struct ira_spilled_reg_stack_slot
*slot
= NULL
;
2846 ira_assert (flag_ira
&& inherent_size
== PSEUDO_REGNO_BYTES (regno
)
2847 && inherent_size
<= total_size
2848 && ALLOCNO_HARD_REGNO (allocno
) < 0);
2849 if (! flag_ira_share_spill_slots
)
2851 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
2854 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
2859 best_cost
= best_slot_num
= -1;
2861 /* It means that the pseudo was spilled in the reload pass, try
2864 slot_num
< ira_spilled_reg_stack_slots_num
;
2867 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
2868 if (slot
->mem
== NULL_RTX
)
2870 if (slot
->width
< total_size
2871 || GET_MODE_SIZE (GET_MODE (slot
->mem
)) < inherent_size
)
2874 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
2875 FIRST_PSEUDO_REGISTER
, i
, bi
)
2877 another_allocno
= ira_regno_allocno_map
[i
];
2878 if (allocnos_have_intersected_live_ranges_p (allocno
,
2882 for (cost
= 0, cp
= ALLOCNO_COPIES (allocno
);
2886 if (cp
->first
== allocno
)
2888 next_cp
= cp
->next_first_allocno_copy
;
2889 another_allocno
= cp
->second
;
2891 else if (cp
->second
== allocno
)
2893 next_cp
= cp
->next_second_allocno_copy
;
2894 another_allocno
= cp
->first
;
2898 if (cp
->insn
== NULL_RTX
)
2900 if (bitmap_bit_p (&slot
->spilled_regs
,
2901 ALLOCNO_REGNO (another_allocno
)))
2904 if (cost
> best_cost
)
2907 best_slot_num
= slot_num
;
2914 slot_num
= best_slot_num
;
2915 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
2916 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
2918 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
2923 ira_assert (slot
->width
>= total_size
);
2924 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
2925 FIRST_PSEUDO_REGISTER
, i
, bi
)
2927 ira_assert (! pseudos_have_intersected_live_ranges_p (regno
, i
));
2929 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
2930 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
2932 fprintf (ira_dump_file
, " Assigning %d(freq=%d) slot %d of",
2933 regno
, REG_FREQ (regno
), slot_num
);
2934 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
2935 FIRST_PSEUDO_REGISTER
, i
, bi
)
2937 if ((unsigned) regno
!= i
)
2938 fprintf (ira_dump_file
, " %d", i
);
2940 fprintf (ira_dump_file
, "\n");
2946 /* This is called by reload every time a new stack slot X with
2947 TOTAL_SIZE was allocated for REGNO. We store this info for
2948 subsequent ira_reuse_stack_slot calls. */
2950 ira_mark_new_stack_slot (rtx x
, int regno
, unsigned int total_size
)
2952 struct ira_spilled_reg_stack_slot
*slot
;
2954 ira_allocno_t allocno
;
2956 ira_assert (flag_ira
&& PSEUDO_REGNO_BYTES (regno
) <= total_size
);
2957 allocno
= ira_regno_allocno_map
[regno
];
2958 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
2961 slot_num
= ira_spilled_reg_stack_slots_num
++;
2962 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
2964 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
2965 INIT_REG_SET (&slot
->spilled_regs
);
2966 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
2968 slot
->width
= total_size
;
2969 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
2970 fprintf (ira_dump_file
, " Assigning %d(freq=%d) a new slot %d\n",
2971 regno
, REG_FREQ (regno
), slot_num
);
2975 /* Return spill cost for pseudo-registers whose numbers are in array
2976 REGNOS (with a negative number as an end marker) for reload with
2977 given IN and OUT for INSN. Return also number points (through
2978 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
2979 the register pressure is high, number of references of the
2980 pseudo-registers (through NREFS), number of callee-clobbered
2981 hard-registers occupied by the pseudo-registers (through
2982 CALL_USED_COUNT), and the first hard regno occupied by the
2983 pseudo-registers (through FIRST_HARD_REGNO). */
2985 calculate_spill_cost (int *regnos
, rtx in
, rtx out
, rtx insn
,
2986 int *excess_pressure_live_length
,
2987 int *nrefs
, int *call_used_count
, int *first_hard_regno
)
2989 int i
, cost
, regno
, hard_regno
, j
, count
, saved_cost
, nregs
;
2995 for (length
= count
= cost
= i
= 0;; i
++)
3000 *nrefs
+= REG_N_REFS (regno
);
3001 hard_regno
= reg_renumber
[regno
];
3002 ira_assert (hard_regno
>= 0);
3003 a
= ira_regno_allocno_map
[regno
];
3004 length
+= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
3005 cost
+= ALLOCNO_MEMORY_COST (a
) - ALLOCNO_COVER_CLASS_COST (a
);
3006 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (a
)];
3007 for (j
= 0; j
< nregs
; j
++)
3008 if (! TEST_HARD_REG_BIT (call_used_reg_set
, hard_regno
+ j
))
3012 in_p
= in
&& REG_P (in
) && (int) REGNO (in
) == hard_regno
;
3013 out_p
= out
&& REG_P (out
) && (int) REGNO (out
) == hard_regno
;
3015 && find_regno_note (insn
, REG_DEAD
, hard_regno
) != NULL_RTX
)
3019 saved_cost
+= ira_memory_move_cost
3020 [ALLOCNO_MODE (a
)][ALLOCNO_COVER_CLASS (a
)][1];
3023 += ira_memory_move_cost
3024 [ALLOCNO_MODE (a
)][ALLOCNO_COVER_CLASS (a
)][0];
3025 cost
-= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn
)) * saved_cost
;
3028 *excess_pressure_live_length
= length
;
3029 *call_used_count
= count
;
3033 hard_regno
= reg_renumber
[regnos
[0]];
3035 *first_hard_regno
= hard_regno
;
3039 /* Return TRUE if spilling pseudo-registers whose numbers are in array
3040 REGNOS is better than spilling pseudo-registers with numbers in
3041 OTHER_REGNOS for reload with given IN and OUT for INSN. The
3042 function used by the reload pass to make better register spilling
3045 ira_better_spill_reload_regno_p (int *regnos
, int *other_regnos
,
3046 rtx in
, rtx out
, rtx insn
)
3048 int cost
, other_cost
;
3049 int length
, other_length
;
3050 int nrefs
, other_nrefs
;
3051 int call_used_count
, other_call_used_count
;
3052 int hard_regno
, other_hard_regno
;
3054 cost
= calculate_spill_cost (regnos
, in
, out
, insn
,
3055 &length
, &nrefs
, &call_used_count
, &hard_regno
);
3056 other_cost
= calculate_spill_cost (other_regnos
, in
, out
, insn
,
3057 &other_length
, &other_nrefs
,
3058 &other_call_used_count
,
3060 if (nrefs
== 0 && other_nrefs
!= 0)
3062 if (nrefs
!= 0 && other_nrefs
== 0)
3064 if (cost
!= other_cost
)
3065 return cost
< other_cost
;
3066 if (length
!= other_length
)
3067 return length
> other_length
;
3068 #ifdef REG_ALLOC_ORDER
3069 if (hard_regno
>= 0 && other_hard_regno
>= 0)
3070 return (inv_reg_alloc_order
[hard_regno
]
3071 < inv_reg_alloc_order
[other_hard_regno
]);
3073 if (call_used_count
!= other_call_used_count
)
3074 return call_used_count
> other_call_used_count
;
3081 /* Allocate and initialize data necessary for assign_hard_reg. */
3083 ira_initiate_assign (void)
3086 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
3087 * ira_allocnos_num
);
3088 consideration_allocno_bitmap
= ira_allocate_bitmap ();
3089 initiate_cost_update ();
3090 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
3093 /* Deallocate data used by assign_hard_reg. */
3095 ira_finish_assign (void)
3097 ira_free (sorted_allocnos
);
3098 ira_free_bitmap (consideration_allocno_bitmap
);
3099 finish_cost_update ();
3100 ira_free (allocno_priorities
);
3105 /* Entry function doing color-based register allocation. */
3109 allocno_stack_vec
= VEC_alloc (ira_allocno_t
, heap
, ira_allocnos_num
);
3110 removed_splay_allocno_vec
3111 = VEC_alloc (ira_allocno_t
, heap
, ira_allocnos_num
);
3112 memset (allocated_hardreg_p
, 0, sizeof (allocated_hardreg_p
));
3113 ira_initiate_assign ();
3115 ira_finish_assign ();
3116 VEC_free (ira_allocno_t
, heap
, removed_splay_allocno_vec
);
3117 VEC_free (ira_allocno_t
, heap
, allocno_stack_vec
);
3118 move_spill_restore ();
3123 /* This page contains a simple register allocator without usage of
3124 allocno conflicts. This is used for fast allocation for -O0. */
3126 /* Do register allocation by not using allocno conflicts. It uses
3127 only allocno live ranges. The algorithm is close to Chow's
3128 priority coloring. */
3130 fast_allocation (void)
3132 int i
, j
, k
, num
, class_size
, hard_regno
;
3134 bool no_stack_reg_p
;
3136 enum reg_class cover_class
;
3137 enum machine_mode mode
;
3139 ira_allocno_iterator ai
;
3140 allocno_live_range_t r
;
3141 HARD_REG_SET conflict_hard_regs
, *used_hard_regs
;
3143 sorted_allocnos
= (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
3144 * ira_allocnos_num
);
3146 FOR_EACH_ALLOCNO (a
, ai
)
3147 sorted_allocnos
[num
++] = a
;
3148 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
3149 setup_allocno_priorities (sorted_allocnos
, num
);
3150 used_hard_regs
= (HARD_REG_SET
*) ira_allocate (sizeof (HARD_REG_SET
)
3152 for (i
= 0; i
< ira_max_point
; i
++)
3153 CLEAR_HARD_REG_SET (used_hard_regs
[i
]);
3154 qsort (sorted_allocnos
, ira_allocnos_num
, sizeof (ira_allocno_t
),
3155 allocno_priority_compare_func
);
3156 for (i
= 0; i
< num
; i
++)
3158 a
= sorted_allocnos
[i
];
3159 COPY_HARD_REG_SET (conflict_hard_regs
, ALLOCNO_CONFLICT_HARD_REGS (a
));
3160 for (r
= ALLOCNO_LIVE_RANGES (a
); r
!= NULL
; r
= r
->next
)
3161 for (j
= r
->start
; j
<= r
->finish
; j
++)
3162 IOR_HARD_REG_SET (conflict_hard_regs
, used_hard_regs
[j
]);
3163 cover_class
= ALLOCNO_COVER_CLASS (a
);
3164 ALLOCNO_ASSIGNED_P (a
) = true;
3165 ALLOCNO_HARD_REGNO (a
) = -1;
3166 if (hard_reg_set_subset_p (reg_class_contents
[cover_class
],
3167 conflict_hard_regs
))
3169 mode
= ALLOCNO_MODE (a
);
3171 no_stack_reg_p
= ALLOCNO_NO_STACK_REG_P (a
);
3173 class_size
= ira_class_hard_regs_num
[cover_class
];
3174 for (j
= 0; j
< class_size
; j
++)
3176 hard_regno
= ira_class_hard_regs
[cover_class
][j
];
3178 if (no_stack_reg_p
&& FIRST_STACK_REG
<= hard_regno
3179 && hard_regno
<= LAST_STACK_REG
)
3182 if (!ira_hard_reg_not_in_set_p (hard_regno
, mode
, conflict_hard_regs
)
3183 || (TEST_HARD_REG_BIT
3184 (prohibited_class_mode_regs
[cover_class
][mode
], hard_regno
)))
3186 ALLOCNO_HARD_REGNO (a
) = hard_regno
;
3187 for (r
= ALLOCNO_LIVE_RANGES (a
); r
!= NULL
; r
= r
->next
)
3188 for (k
= r
->start
; k
<= r
->finish
; k
++)
3189 IOR_HARD_REG_SET (used_hard_regs
[k
],
3190 ira_reg_mode_hard_regset
[hard_regno
][mode
]);
3194 ira_free (sorted_allocnos
);
3195 ira_free (used_hard_regs
);
3196 ira_free (allocno_priorities
);
3197 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
3198 ira_print_disposition (ira_dump_file
);
3203 /* Entry function doing coloring. */
3208 ira_allocno_iterator ai
;
3210 /* Setup updated costs. */
3211 FOR_EACH_ALLOCNO (a
, ai
)
3213 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
3214 ALLOCNO_UPDATED_COVER_CLASS_COST (a
) = ALLOCNO_COVER_CLASS_COST (a
);