2008-05-08 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-color.c
blobdbb8f50ee4e66992e8943c54d4da45e4a18e96ef
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
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "target.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "sbitmap.h"
32 #include "bitmap.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "expr.h"
36 #include "toplev.h"
37 #include "reload.h"
38 #include "params.h"
39 #include "df.h"
40 #include "ira-int.h"
42 /* This file contains code for regional graph coloring, spill/restore
43 code placement optimization, and code helping the reload pass to do
44 a better job. */
46 static void initiate_cost_update (void);
47 static void finish_cost_update (void);
48 static void update_copy_costs_1 (allocno_t, int, int, int);
49 static void update_copy_costs (allocno_t, int);
50 static int allocno_cost_compare_func (const void *, const void *);
51 static void print_coalesced_allocno (allocno_t);
52 static int assign_hard_reg (allocno_t, int);
54 static void add_allocno_to_bucket (allocno_t, allocno_t *);
55 static void get_coalesced_allocnos_attributes (allocno_t, int *, int *);
56 static int bucket_allocno_compare_func (const void *, const void *);
57 static void sort_bucket (allocno_t *);
58 static void add_allocno_to_ordered_bucket (allocno_t, allocno_t *);
59 static void delete_allocno_from_bucket (allocno_t, allocno_t *);
60 static void push_allocno_to_stack (allocno_t);
61 static void remove_allocno_from_bucket_and_push (allocno_t, int);
62 static void push_only_colorable (void);
63 static void push_allocno_to_spill (allocno_t);
64 static int calculate_allocno_spill_cost (allocno_t);
65 static void push_allocnos_to_stack (void);
66 static void pop_allocnos_from_stack (void);
67 static void setup_allocno_available_regs_num (allocno_t);
68 static void setup_allocno_left_conflicts_num (allocno_t);
69 static void put_allocno_into_bucket (allocno_t);
70 static int copy_freq_compare_func (const void *, const void *);
71 static void merge_allocnos (allocno_t, allocno_t);
72 static int coalesced_allocno_conflict_p (allocno_t, allocno_t, int);
73 static void coalesce_allocnos (int);
74 static void color_allocnos (void);
76 static void print_loop_title (loop_tree_node_t);
77 static void color_pass (loop_tree_node_t);
78 static void do_coloring (void);
80 static void move_spill_restore (void);
82 static void update_curr_costs (allocno_t);
83 static void start_allocno_priorities (allocno_t *, int);
84 static int allocno_priority_compare_func (const void *, const void *);
86 static int coalesced_pseudo_reg_freq_compare (const void *, const void *);
87 static int coalesced_pseudo_reg_slot_compare (const void *, const void *);
88 static void setup_coalesced_allocno_costs_and_nums (int *, int);
89 static int collect_spilled_coalesced_allocnos (int *, int, allocno_t *);
90 static int coalesce_spill_slots (allocno_t *, int);
92 static int allocno_reload_assign (allocno_t, HARD_REG_SET);
93 static int pseudo_reg_compare (const void *, const void *);
95 static int calculate_spill_cost (int *, rtx, rtx, rtx,
96 int *, int *, int *, int*);
98 /* Bitmap of allocnos which should be colored. */
99 static bitmap coloring_allocno_bitmap;
101 /* Bitmap of allocnos which should be taken into account during
102 coloring. In general case it contains allocnos from
103 coloring_allocno_bitmap plus other already colored conflicting
104 allocnos. */
105 static bitmap consideration_allocno_bitmap;
107 /* TRUE if we coalesced some allocnos. In other words, if we got
108 loops formed by members first_coalesced_allocno and
109 next_coalesced_allocno containing more one allocno. */
110 static int allocno_coalesced_p;
112 /* Bitmap used to prevent a repeated allocno processing because of
113 coalescing. */
114 static bitmap processed_coalesced_allocno_bitmap;
116 /* All allocnos sorted according their priorities. */
117 static allocno_t *sorted_allocnos;
119 /* Array used to sort allocnos to choose an allocno for spilling. */
120 static allocno_t *sorted_allocnos_for_spilling;
122 /* Definition of vector of allocnos. */
123 DEF_VEC_P(allocno_t);
124 DEF_VEC_ALLOC_P(allocno_t, heap);
126 /* Vec representing the stack of allocnos used during coloring. */
127 static VEC(allocno_t,heap) *allocno_stack_vec;
131 /* This page contains functions used to choose hard registers for
132 allocnos. */
134 /* Array whose element value is TRUE if the corresponding hard
135 register was already allocated for an allocno. */
136 static int allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
138 /* Array used to check already processed allocnos during the current
139 update_copy_costs call. */
140 static int *allocno_update_cost_check;
142 /* The current value of update_copy_cost call count. */
143 static int update_cost_check;
145 /* Allocate and initialize data necessary for function
146 update_copy_costs. */
147 static void
148 initiate_cost_update (void)
150 allocno_update_cost_check = ira_allocate (allocnos_num * sizeof (int));
151 memset (allocno_update_cost_check, 0, allocnos_num * sizeof (int));
152 update_cost_check = 0;
155 /* Deallocate data used by function update_copy_costs. */
156 static void
157 finish_cost_update (void)
159 ira_free (allocno_update_cost_check);
162 /* This recursive function updates costs (decrease if DECR_P) of the
163 unassigned allocnos connected by copies with ALLOCNO. This update
164 increases chances to remove some copies. Copy cost is proportional
165 the copy frequency divided by DIVISOR. */
166 static void
167 update_copy_costs_1 (allocno_t allocno, int hard_regno,
168 int decr_p, int divisor)
170 int i, cost, update_cost;
171 enum machine_mode mode;
172 enum reg_class class, cover_class;
173 allocno_t another_allocno;
174 copy_t cp, next_cp;
176 cover_class = ALLOCNO_COVER_CLASS (allocno);
177 if (cover_class == NO_REGS)
178 return;
179 if (allocno_update_cost_check[ALLOCNO_NUM (allocno)] == update_cost_check)
180 return;
181 allocno_update_cost_check[ALLOCNO_NUM (allocno)] = update_cost_check;
182 ira_assert (hard_regno >= 0);
183 i = class_hard_reg_index[cover_class][hard_regno];
184 ira_assert (i >= 0);
185 class = REGNO_REG_CLASS (hard_regno);
186 mode = ALLOCNO_MODE (allocno);
187 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
189 if (cp->first == allocno)
191 next_cp = cp->next_first_allocno_copy;
192 another_allocno = cp->second;
194 else if (cp->second == allocno)
196 next_cp = cp->next_second_allocno_copy;
197 another_allocno = cp->first;
199 else
200 gcc_unreachable ();
201 if (cover_class
202 != ALLOCNO_COVER_CLASS (another_allocno)
203 || ALLOCNO_ASSIGNED_P (another_allocno))
204 continue;
205 cost = (cp->second == allocno
206 ? register_move_cost[mode][class]
207 [ALLOCNO_COVER_CLASS (another_allocno)]
208 : register_move_cost[mode]
209 [ALLOCNO_COVER_CLASS (another_allocno)][class]);
210 if (decr_p)
211 cost = -cost;
212 allocate_and_set_or_copy_costs
213 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno), cover_class,
214 ALLOCNO_COVER_CLASS_COST (another_allocno),
215 ALLOCNO_HARD_REG_COSTS (another_allocno));
216 allocate_and_set_or_copy_costs
217 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
218 cover_class, 0,
219 ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
220 update_cost = cp->freq * cost / divisor;
221 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno)[i] += update_cost;
222 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno)[i]
223 += update_cost;
224 if (update_cost != 0)
225 update_copy_costs_1 (another_allocno, hard_regno,
226 decr_p, divisor * 4);
230 /* Entry function to update costs of allocnos to increase chances to
231 remove some copies as the result of subsequent assignment. */
232 static void
233 update_copy_costs (allocno_t allocno, int decr_p)
235 update_cost_check++;
236 update_copy_costs_1 (allocno, ALLOCNO_HARD_REGNO (allocno), decr_p, 1);
239 /* The function is used to sort allocnos according to the profit of
240 usage of a hard register instead of memory for them. */
241 static int
242 allocno_cost_compare_func (const void *v1p, const void *v2p)
244 allocno_t p1 = *(const allocno_t *) v1p, p2 = *(const allocno_t *) v2p;
245 int c1, c2;
247 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_COVER_CLASS_COST (p1);
248 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_COVER_CLASS_COST (p2);
249 if (c1 - c2)
250 return c1 - c2;
252 /* If regs are equally good, sort by allocno numbers, so that the
253 results of qsort leave nothing to chance. */
254 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
257 /* Print all allocnos coalesced with ALLOCNO. */
258 static void
259 print_coalesced_allocno (allocno_t allocno)
261 allocno_t a;
263 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
264 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
266 print_expanded_allocno (a);
267 if (a == allocno)
268 break;
269 fprintf (ira_dump_file, "+");
273 /* Function choosing a hard register for ALLOCNO (or for all coalesced
274 allocnos represented by ALLOCNO). If RETRY_P is TRUE, it means
275 that the function called from function `reassign_conflict_allocnos'
276 and `allocno_reload_assign'. The function implements the
277 optimistic coalescing too: if we failed to assign a hard register
278 to set of the coalesced allocnos, we put them onto the coloring
279 stack for subsequent separate assigning. */
280 static int
281 assign_hard_reg (allocno_t allocno, int retry_p)
283 HARD_REG_SET conflicting_regs;
284 int i, j, hard_regno, best_hard_regno, class_size;
285 int cost, mem_cost, min_cost, full_cost, min_full_cost, add_cost;
286 int *a_costs;
287 int *conflict_costs;
288 enum reg_class cover_class, class;
289 enum machine_mode mode;
290 allocno_t a, conflict_allocno;
291 allocno_t another_allocno;
292 allocno_conflict_iterator aci;
293 copy_t cp, next_cp;
294 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
295 #ifdef STACK_REGS
296 int no_stack_reg_p;
297 #endif
299 ira_assert (! ALLOCNO_ASSIGNED_P (allocno));
300 cover_class = ALLOCNO_COVER_CLASS (allocno);
301 class_size = class_hard_regs_num[cover_class];
302 mode = ALLOCNO_MODE (allocno);
303 CLEAR_HARD_REG_SET (conflicting_regs);
304 best_hard_regno = -1;
305 memset (full_costs, 0, sizeof (int) * class_size);
306 mem_cost = 0;
307 if (allocno_coalesced_p)
308 bitmap_clear (processed_coalesced_allocno_bitmap);
309 memset (costs, 0, sizeof (int) * class_size);
310 memset (full_costs, 0, sizeof (int) * class_size);
311 #ifdef STACK_REGS
312 no_stack_reg_p = FALSE;
313 #endif
314 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
315 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
317 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
318 IOR_HARD_REG_SET (conflicting_regs,
319 ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
320 allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
321 cover_class, ALLOCNO_HARD_REG_COSTS (a));
322 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
323 #ifdef STACK_REGS
324 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
325 #endif
326 for (cost = ALLOCNO_COVER_CLASS_COST (a), i = 0; i < class_size; i++)
327 if (a_costs != NULL)
329 costs[i] += a_costs[i];
330 full_costs[i] += a_costs[i];
332 else
334 costs[i] += cost;
335 full_costs[i] += cost;
337 /* Take preferences of conflicting allocnos into account. */
338 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
339 /* Reload can give another class so we need to check all
340 allocnos. */
341 if (retry_p || bitmap_bit_p (consideration_allocno_bitmap,
342 ALLOCNO_NUM (conflict_allocno)))
344 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_allocno));
345 if (allocno_coalesced_p)
347 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
348 ALLOCNO_NUM (conflict_allocno)))
349 continue;
350 bitmap_set_bit (processed_coalesced_allocno_bitmap,
351 ALLOCNO_NUM (conflict_allocno));
353 if (ALLOCNO_ASSIGNED_P (conflict_allocno))
355 if ((hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno)) >= 0)
357 IOR_HARD_REG_SET
358 (conflicting_regs,
359 reg_mode_hard_regset
360 [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
361 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
362 conflicting_regs))
363 goto fail;
365 continue;
367 else if (! ALLOCNO_MAY_BE_SPILLED_P (conflict_allocno))
369 allocate_and_copy_costs
370 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno),
371 cover_class,
372 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_allocno));
373 conflict_costs
374 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno);
375 if (conflict_costs != NULL)
376 for (j = class_size - 1; j >= 0; j--)
377 full_costs[j] -= conflict_costs[j];
380 if (a == allocno)
381 break;
383 /* Take copies into account. */
384 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
385 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
387 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
389 if (cp->first == a)
391 next_cp = cp->next_first_allocno_copy;
392 another_allocno = cp->second;
394 else if (cp->second == a)
396 next_cp = cp->next_second_allocno_copy;
397 another_allocno = cp->first;
399 else
400 gcc_unreachable ();
401 if (cover_class != ALLOCNO_COVER_CLASS (another_allocno)
402 || ALLOCNO_ASSIGNED_P (another_allocno))
403 continue;
404 allocate_and_copy_costs
405 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
406 cover_class, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
407 conflict_costs
408 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
409 if (conflict_costs != NULL
410 && ! ALLOCNO_MAY_BE_SPILLED_P (another_allocno))
411 for (j = class_size - 1; j >= 0; j--)
412 full_costs[j] += conflict_costs[j];
414 if (a == allocno)
415 break;
417 min_cost = min_full_cost = INT_MAX;
418 /* We don't care about giving callee saved registers to allocnos no
419 living through calls because call clobbered registers are
420 allocated first (it is usual practice to put them first in
421 REG_ALLOC_ORDER). */
422 for (i = 0; i < class_size; i++)
424 hard_regno = class_hard_regs[cover_class][i];
425 #ifdef STACK_REGS
426 if (no_stack_reg_p
427 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
428 continue;
429 #endif
430 if (! hard_reg_not_in_set_p (hard_regno, mode, conflicting_regs)
431 || TEST_HARD_REG_BIT (prohibited_class_mode_regs[cover_class][mode],
432 hard_regno))
433 continue;
434 cost = costs[i];
435 full_cost = full_costs[i];
436 if (! allocated_hardreg_p[hard_regno]
437 && hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set))
438 /* We need to save/restore the hard register in
439 epilogue/prologue. Therefore we increase the cost. */
441 /* ??? If only part is call clobbered. */
442 class = REGNO_REG_CLASS (hard_regno);
443 add_cost = (memory_move_cost[mode][class][0]
444 + memory_move_cost[mode][class][1] - 1);
445 cost += add_cost;
446 full_cost += add_cost;
448 if (min_cost > cost)
449 min_cost = cost;
450 if (min_full_cost > full_cost)
452 min_full_cost = full_cost;
453 best_hard_regno = hard_regno;
454 ira_assert (hard_regno >= 0);
457 if (min_full_cost > mem_cost)
459 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
460 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
461 mem_cost, min_full_cost);
462 best_hard_regno = -1;
464 fail:
465 if (best_hard_regno < 0
466 && ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno) != allocno)
468 for (j = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
469 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
471 sorted_allocnos[j++] = a;
472 if (a == allocno)
473 break;
475 qsort (sorted_allocnos, j, sizeof (allocno_t),
476 allocno_cost_compare_func);
477 for (i = 0; i < j; i++)
479 a = sorted_allocnos[i];
480 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
481 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
482 VEC_safe_push (allocno_t, heap, allocno_stack_vec, a);
483 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
485 fprintf (ira_dump_file, " Pushing");
486 print_coalesced_allocno (a);
487 fprintf (ira_dump_file, "\n");
490 return FALSE;
492 if (best_hard_regno >= 0)
493 allocated_hardreg_p[best_hard_regno] = TRUE;
494 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
495 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
497 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
498 ALLOCNO_ASSIGNED_P (a) = TRUE;
499 if (best_hard_regno >= 0)
500 update_copy_costs (a, TRUE);
501 ira_assert (ALLOCNO_COVER_CLASS (a) == cover_class);
502 /* We don't need updated costs anymore: */
503 free_allocno_updated_costs (a);
504 if (a == allocno)
505 break;
507 return best_hard_regno >= 0;
512 /* This page contains allocator based on Chaitin-Briggs algorithm. */
514 /* Bucket of allocnos that can colored currently without spilling. */
515 static allocno_t colorable_allocno_bucket;
517 /* Bucket of allocnos that might be not colored currently without
518 spilling. */
519 static allocno_t uncolorable_allocno_bucket;
521 /* Add ALLOCNO to bucket *BUCKET_PTR. ALLOCNO should be not in a bucket
522 before the call. */
523 static void
524 add_allocno_to_bucket (allocno_t allocno, allocno_t *bucket_ptr)
526 allocno_t first_allocno;
528 first_allocno = *bucket_ptr;
529 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = first_allocno;
530 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = NULL;
531 if (first_allocno != NULL)
532 ALLOCNO_PREV_BUCKET_ALLOCNO (first_allocno) = allocno;
533 *bucket_ptr = allocno;
536 /* The function returns frequency and number of available hard
537 registers for allocnos coalesced with ALLOCNO. */
538 static void
539 get_coalesced_allocnos_attributes (allocno_t allocno, int *freq, int *num)
541 allocno_t a;
543 *freq = 0;
544 *num = 0;
545 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
546 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
548 *freq += ALLOCNO_FREQ (a);
549 *num += ALLOCNO_AVAILABLE_REGS_NUM (a);
550 if (a == allocno)
551 break;
555 /* The function compares two allocnos to define what allocno should be
556 pushed first into the coloring stack. If the function returns a
557 negative number, the allocno given by the first parameter will be
558 pushed first. In this case such allocno has less priority than the
559 second one and the hard register will be assigned to it after
560 assignment to the second one. As the result of such assignment
561 order, the second allocno has a better chance to get the best hard
562 register. */
563 static int
564 bucket_allocno_compare_func (const void *v1p, const void *v2p)
566 allocno_t a1 = *(const allocno_t *) v1p, a2 = *(const allocno_t *) v2p;
567 int diff, a1_freq, a2_freq, a1_num, a2_num;
569 if ((diff = (int) ALLOCNO_COVER_CLASS (a2) - ALLOCNO_COVER_CLASS (a1)) != 0)
570 return diff;
571 get_coalesced_allocnos_attributes (a1, &a1_freq, &a1_num);
572 get_coalesced_allocnos_attributes (a2, &a2_freq, &a2_num);
573 if ((diff = a2_num - a1_num) != 0)
574 return diff;
575 else if ((diff = a1_freq - a2_freq) != 0)
576 return diff;
577 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
580 /* Function sorts bucket *BUCKET_PTR and returns the result through
581 BUCKET_PTR. */
582 static void
583 sort_bucket (allocno_t *bucket_ptr)
585 allocno_t a, head;
586 int n;
588 for (n = 0, a = *bucket_ptr; a != NULL; a = ALLOCNO_NEXT_BUCKET_ALLOCNO (a))
589 sorted_allocnos[n++] = a;
590 if (n <= 1)
591 return;
592 qsort (sorted_allocnos, n, sizeof (allocno_t), bucket_allocno_compare_func);
593 head = NULL;
594 for (n--; n >= 0; n--)
596 a = sorted_allocnos[n];
597 ALLOCNO_NEXT_BUCKET_ALLOCNO (a) = head;
598 ALLOCNO_PREV_BUCKET_ALLOCNO (a) = NULL;
599 if (head != NULL)
600 ALLOCNO_PREV_BUCKET_ALLOCNO (head) = a;
601 head = a;
603 *bucket_ptr = head;
606 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
607 their priority. ALLOCNO should be not in a bucket before the
608 call. */
609 static void
610 add_allocno_to_ordered_bucket (allocno_t allocno, allocno_t *bucket_ptr)
612 allocno_t before, after;
614 for (before = *bucket_ptr, after = NULL;
615 before != NULL;
616 after = before, before = ALLOCNO_NEXT_BUCKET_ALLOCNO (before))
617 if (bucket_allocno_compare_func (&allocno, &before) < 0)
618 break;
619 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = before;
620 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = after;
621 if (after == NULL)
622 *bucket_ptr = allocno;
623 else
624 ALLOCNO_NEXT_BUCKET_ALLOCNO (after) = allocno;
625 if (before != NULL)
626 ALLOCNO_PREV_BUCKET_ALLOCNO (before) = allocno;
629 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
630 the call. */
631 static void
632 delete_allocno_from_bucket (allocno_t allocno, allocno_t *bucket_ptr)
634 allocno_t prev_allocno, next_allocno;
636 prev_allocno = ALLOCNO_PREV_BUCKET_ALLOCNO (allocno);
637 next_allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno);
638 if (prev_allocno != NULL)
639 ALLOCNO_NEXT_BUCKET_ALLOCNO (prev_allocno) = next_allocno;
640 else
642 ira_assert (*bucket_ptr == allocno);
643 *bucket_ptr = next_allocno;
645 if (next_allocno != NULL)
646 ALLOCNO_PREV_BUCKET_ALLOCNO (next_allocno) = prev_allocno;
649 /* The function puts ALLOCNO onto the coloring stack without removing
650 it from its bucket. Pushing allocno to the coloring stack can
651 result in moving conflicting allocnos from the uncolorable bucket
652 to the colorable one. */
653 static void
654 push_allocno_to_stack (allocno_t allocno)
656 int conflicts_num, conflict_size, size;
657 allocno_t a, conflict_allocno;
658 enum reg_class cover_class;
659 allocno_conflict_iterator aci;
661 ALLOCNO_IN_GRAPH_P (allocno) = FALSE;
662 VEC_safe_push (allocno_t, heap, allocno_stack_vec, allocno);
663 cover_class = ALLOCNO_COVER_CLASS (allocno);
664 if (cover_class == NO_REGS)
665 return;
666 size = reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)];
667 if (allocno_coalesced_p)
668 bitmap_clear (processed_coalesced_allocno_bitmap);
669 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
670 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
672 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
673 if (bitmap_bit_p (coloring_allocno_bitmap,
674 ALLOCNO_NUM (conflict_allocno)))
676 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_allocno));
677 if (allocno_coalesced_p)
679 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
680 ALLOCNO_NUM (conflict_allocno)))
681 continue;
682 bitmap_set_bit (processed_coalesced_allocno_bitmap,
683 ALLOCNO_NUM (conflict_allocno));
685 if (ALLOCNO_IN_GRAPH_P (conflict_allocno)
686 && ! ALLOCNO_ASSIGNED_P (conflict_allocno))
688 conflicts_num = ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno);
689 conflict_size
690 = (reg_class_nregs
691 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
692 ira_assert
693 (ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) >= size);
694 ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) -= size;
695 if (conflicts_num + conflict_size
696 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
697 continue;
698 conflicts_num = ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno);
699 if (conflicts_num + conflict_size
700 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
702 delete_allocno_from_bucket
703 (conflict_allocno, &uncolorable_allocno_bucket);
704 add_allocno_to_ordered_bucket (conflict_allocno,
705 &colorable_allocno_bucket);
709 if (a == allocno)
710 break;
714 /* The function puts ALLOCNO onto the coloring stack and removes it
715 from its bucket. The allocno is in the colorable bucket if
716 COLORABLE_P is TRUE. */
717 static void
718 remove_allocno_from_bucket_and_push (allocno_t allocno, int colorable_p)
720 enum reg_class cover_class;
721 allocno_t *bucket_ptr;
723 bucket_ptr = (colorable_p
724 ? &colorable_allocno_bucket : &uncolorable_allocno_bucket);
725 delete_allocno_from_bucket (allocno, bucket_ptr);
726 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
728 fprintf (ira_dump_file, " Pushing");
729 print_coalesced_allocno (allocno);
730 fprintf (ira_dump_file, "%s\n", colorable_p ? "" : "(potential spill)");
732 cover_class = ALLOCNO_COVER_CLASS (allocno);
733 ira_assert ((colorable_p
734 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
735 + reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
736 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno)))
737 || (! colorable_p
738 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
739 + reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
740 > ALLOCNO_AVAILABLE_REGS_NUM (allocno))));
741 if (! colorable_p)
742 ALLOCNO_MAY_BE_SPILLED_P (allocno) = TRUE;
743 push_allocno_to_stack (allocno);
746 /* The function puts all allocnos from colorable bucket onto the
747 coloring stack. */
748 static void
749 push_only_colorable (void)
751 sort_bucket (&colorable_allocno_bucket);
752 for (;colorable_allocno_bucket != NULL;)
753 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, TRUE);
756 /* The function puts ALLOCNO chosen for potential spilling onto the
757 coloring stack. */
758 static void
759 push_allocno_to_spill (allocno_t allocno)
761 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
762 ALLOCNO_MAY_BE_SPILLED_P (allocno) = TRUE;
763 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
764 fprintf (ira_dump_file, " Pushing p%d(%d) (potential spill)\n",
765 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
766 push_allocno_to_stack (allocno);
769 /* The function returns frequency of exit edges (if EXIT_P) or entry
770 from/to the loop given by its LOOP_NODE. */
772 loop_edge_freq (loop_tree_node_t loop_node, int regno, int exit_p)
774 int freq, i;
775 edge_iterator ei;
776 edge e;
777 VEC (edge, heap) *edges;
779 ira_assert (loop_node->loop != NULL
780 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
781 freq = 0;
782 if (! exit_p)
784 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
785 if (e->src != loop_node->loop->latch
786 && (regno < 0
787 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
788 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
789 freq += EDGE_FREQUENCY (e);
791 else
793 edges = get_loop_exit_edges (loop_node->loop);
794 for (i = 0; VEC_iterate (edge, edges, i, e); i++)
795 if (regno < 0
796 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
797 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
798 freq += EDGE_FREQUENCY (e);
799 VEC_free (edge, heap, edges);
802 return REG_FREQ_FROM_EDGE_FREQ (freq);
805 /* The function calculates and returns cost of putting allocno A into
806 memory. */
807 static int
808 calculate_allocno_spill_cost (allocno_t a)
810 int regno, cost;
811 enum machine_mode mode;
812 enum reg_class class;
813 allocno_t father_allocno;
814 loop_tree_node_t father_node, loop_node;
816 regno = ALLOCNO_REGNO (a);
817 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a);
818 if (ALLOCNO_CAP (a) != NULL)
819 return cost;
820 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
821 if ((father_node = loop_node->father) == NULL)
822 return cost;
823 if ((father_allocno = father_node->regno_allocno_map[regno]) == NULL)
824 return cost;
825 mode = ALLOCNO_MODE (a);
826 class = ALLOCNO_COVER_CLASS (a);
827 if (ALLOCNO_HARD_REGNO (father_allocno) < 0)
828 cost -= (memory_move_cost[mode][class][0]
829 * loop_edge_freq (loop_node, regno, TRUE)
830 + memory_move_cost[mode][class][1]
831 * loop_edge_freq (loop_node, regno, FALSE));
832 else
833 cost += ((memory_move_cost[mode][class][1]
834 * loop_edge_freq (loop_node, regno, TRUE)
835 + memory_move_cost[mode][class][0]
836 * loop_edge_freq (loop_node, regno, FALSE))
837 - (register_move_cost[mode][class][class]
838 * (loop_edge_freq (loop_node, regno, FALSE)
839 + loop_edge_freq (loop_node, regno, TRUE))));
840 return cost;
843 /* Push allocnos to the coloring stack. The order of allocnos in the
844 stack defines the order for the subsequent coloring. */
845 static void
846 push_allocnos_to_stack (void)
848 int i, j;
849 int allocno_pri, i_allocno_pri;
850 int allocno_cost, i_allocno_cost;
851 allocno_t allocno, i_allocno;
852 allocno_t *allocno_vec;
853 enum reg_class cover_class;
854 int num, cover_class_allocnos_num[N_REG_CLASSES];
855 allocno_t *cover_class_allocnos[N_REG_CLASSES];
857 /* Initialize. */
858 for (i = 0; i < reg_class_cover_size; i++)
860 cover_class = reg_class_cover[i];
861 cover_class_allocnos_num[cover_class] = 0;
862 cover_class_allocnos[cover_class] = NULL;
864 /* Calculate uncolorable allocnos of each cover class. */
865 for (allocno = uncolorable_allocno_bucket;
866 allocno != NULL;
867 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
868 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
870 cover_class_allocnos_num[cover_class]++;
871 ALLOCNO_TEMP (allocno) = INT_MAX;
873 /* Define place where to put uncolorable allocnos of the same cover
874 class. */
875 for (num = i = 0; i < reg_class_cover_size; i++)
877 cover_class = reg_class_cover[i];
878 if (cover_class_allocnos_num[cover_class] != 0)
880 cover_class_allocnos[cover_class]
881 = sorted_allocnos_for_spilling + num;
882 num += cover_class_allocnos_num[cover_class];
883 cover_class_allocnos_num[cover_class] = 0;
886 ira_assert (num <= allocnos_num);
887 /* Put uncolorable allocnos of the same cover class together. */
888 for (allocno = uncolorable_allocno_bucket;
889 allocno != NULL;
890 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
891 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
892 cover_class_allocnos
893 [cover_class][cover_class_allocnos_num[cover_class]++] = allocno;
894 for (;;)
896 push_only_colorable ();
897 allocno = uncolorable_allocno_bucket;
898 if (allocno == NULL)
899 break;
900 cover_class = ALLOCNO_COVER_CLASS (allocno);
901 if (cover_class == NO_REGS)
903 push_allocno_to_spill (allocno);
904 continue;
906 /* Potential spilling. */
907 ira_assert (reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)] > 0);
908 num = cover_class_allocnos_num[cover_class];
909 ira_assert (num > 0);
910 allocno_vec = cover_class_allocnos[cover_class];
911 allocno = NULL;
912 allocno_pri = allocno_cost = 0;
913 /* Sort uncolorable allocno to find the one with the lowest spill
914 cost. */
915 for (i = 0, j = num - 1; i <= j;)
917 i_allocno = allocno_vec[i];
918 if (! ALLOCNO_IN_GRAPH_P (i_allocno)
919 && ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
921 i_allocno = allocno_vec[j];
922 allocno_vec[j] = allocno_vec[i];
923 allocno_vec[i] = i_allocno;
925 if (ALLOCNO_IN_GRAPH_P (i_allocno))
927 i++;
928 if (ALLOCNO_TEMP (i_allocno) == INT_MAX)
930 allocno_t a;
931 int cost = 0;
933 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (i_allocno);;
934 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
936 cost += calculate_allocno_spill_cost (i_allocno);
937 if (a == i_allocno)
938 break;
940 /* ??? Remove cost of copies between the coalesced
941 allocnos. */
942 ALLOCNO_TEMP (i_allocno) = cost;
944 i_allocno_cost = ALLOCNO_TEMP (i_allocno);
945 i_allocno_pri
946 = (i_allocno_cost
947 / (ALLOCNO_LEFT_CONFLICTS_NUM (i_allocno)
948 * reg_class_nregs[ALLOCNO_COVER_CLASS (i_allocno)]
949 [ALLOCNO_MODE (i_allocno)] + 1));
950 if (allocno == NULL || allocno_pri > i_allocno_pri
951 || (allocno_pri == i_allocno_pri
952 && (allocno_cost > i_allocno_cost
953 || (allocno_cost == i_allocno_cost
954 && (ALLOCNO_NUM (allocno)
955 > ALLOCNO_NUM (i_allocno))))))
957 allocno = i_allocno;
958 allocno_cost = i_allocno_cost;
959 allocno_pri = i_allocno_pri;
962 if (! ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
963 j--;
965 ira_assert (allocno != NULL && j >= 0);
966 cover_class_allocnos_num[cover_class] = j + 1;
967 ira_assert (ALLOCNO_IN_GRAPH_P (allocno)
968 && ALLOCNO_COVER_CLASS (allocno) == cover_class
969 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
970 + reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
971 > ALLOCNO_AVAILABLE_REGS_NUM (allocno)));
972 remove_allocno_from_bucket_and_push (allocno, FALSE);
976 /* Pop the coloring stack and assign hard registers to the popped
977 allocnos. */
978 static void
979 pop_allocnos_from_stack (void)
981 allocno_t allocno;
982 enum reg_class cover_class;
984 for (;VEC_length (allocno_t, allocno_stack_vec) != 0;)
986 allocno = VEC_pop (allocno_t, allocno_stack_vec);
987 cover_class = ALLOCNO_COVER_CLASS (allocno);
988 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
990 fprintf (ira_dump_file, " Popping");
991 print_coalesced_allocno (allocno);
992 fprintf (ira_dump_file, " -- ");
994 if (cover_class == NO_REGS)
996 ALLOCNO_HARD_REGNO (allocno) = -1;
997 ALLOCNO_ASSIGNED_P (allocno) = TRUE;
998 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
999 ira_assert
1000 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
1001 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1002 fprintf (ira_dump_file, "assign memory\n");
1004 else if (assign_hard_reg (allocno, FALSE))
1006 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1007 fprintf (ira_dump_file, "assign reg %d\n",
1008 ALLOCNO_HARD_REGNO (allocno));
1010 else if (ALLOCNO_ASSIGNED_P (allocno))
1012 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1013 fprintf (ira_dump_file, "spill\n");
1015 ALLOCNO_IN_GRAPH_P (allocno) = TRUE;
1019 /* Set up number of available hard registers for ALLOCNO. */
1020 static void
1021 setup_allocno_available_regs_num (allocno_t allocno)
1023 int i, n, hard_regs_num;
1024 enum reg_class cover_class;
1025 allocno_t a;
1026 HARD_REG_SET temp_set;
1028 cover_class = ALLOCNO_COVER_CLASS (allocno);
1029 ALLOCNO_AVAILABLE_REGS_NUM (allocno) = available_class_regs[cover_class];
1030 if (cover_class == NO_REGS)
1031 return;
1032 CLEAR_HARD_REG_SET (temp_set);
1033 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1034 hard_regs_num = class_hard_regs_num[cover_class];
1035 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1036 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1038 IOR_HARD_REG_SET (temp_set, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
1039 if (a == allocno)
1040 break;
1042 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
1043 if (TEST_HARD_REG_BIT (temp_set, class_hard_regs[cover_class][i]))
1044 n++;
1045 if (internal_flag_ira_verbose > 2 && n > 0 && ira_dump_file != NULL)
1046 fprintf (ira_dump_file, " Reg %d of %s has %d regs less\n",
1047 ALLOCNO_REGNO (allocno), reg_class_names[cover_class], n);
1048 ALLOCNO_AVAILABLE_REGS_NUM (allocno) -= n;
1051 /* Set up ALLOCNO_LEFT_CONFLICTS_NUM for ALLOCNO. */
1052 static void
1053 setup_allocno_left_conflicts_num (allocno_t allocno)
1055 int i, hard_regs_num, hard_regno, conflict_allocnos_size;
1056 allocno_t a, conflict_allocno;
1057 enum reg_class cover_class;
1058 HARD_REG_SET temp_set;
1059 allocno_conflict_iterator aci;
1061 cover_class = ALLOCNO_COVER_CLASS (allocno);
1062 hard_regs_num = class_hard_regs_num[cover_class];
1063 CLEAR_HARD_REG_SET (temp_set);
1064 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1065 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1066 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1068 IOR_HARD_REG_SET (temp_set, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
1069 if (a == allocno)
1070 break;
1072 AND_HARD_REG_SET (temp_set, reg_class_contents[cover_class]);
1073 AND_COMPL_HARD_REG_SET (temp_set, no_alloc_regs);
1074 conflict_allocnos_size = 0;
1075 if (! hard_reg_set_equal_p (temp_set, zero_hard_reg_set))
1076 for (i = 0; i < (int) hard_regs_num; i++)
1078 hard_regno = class_hard_regs[cover_class][i];
1079 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
1081 conflict_allocnos_size++;
1082 CLEAR_HARD_REG_BIT (temp_set, hard_regno);
1083 if (hard_reg_set_equal_p (temp_set, zero_hard_reg_set))
1084 break;
1087 CLEAR_HARD_REG_SET (temp_set);
1088 if (allocno_coalesced_p)
1089 bitmap_clear (processed_coalesced_allocno_bitmap);
1090 if (cover_class != NO_REGS)
1091 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1092 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1094 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
1095 if (bitmap_bit_p (consideration_allocno_bitmap,
1096 ALLOCNO_NUM (conflict_allocno)))
1098 ira_assert (cover_class
1099 == ALLOCNO_COVER_CLASS (conflict_allocno));
1100 if (allocno_coalesced_p)
1102 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
1103 ALLOCNO_NUM (conflict_allocno)))
1104 continue;
1105 bitmap_set_bit (processed_coalesced_allocno_bitmap,
1106 ALLOCNO_NUM (conflict_allocno));
1108 if (! ALLOCNO_ASSIGNED_P (conflict_allocno))
1109 conflict_allocnos_size
1110 += (reg_class_nregs
1111 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
1112 else if ((hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno))
1113 >= 0)
1115 int last = (hard_regno
1116 + hard_regno_nregs
1117 [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
1119 while (hard_regno < last)
1121 if (! TEST_HARD_REG_BIT (temp_set, hard_regno))
1123 conflict_allocnos_size++;
1124 SET_HARD_REG_BIT (temp_set, hard_regno);
1126 hard_regno++;
1130 if (a == allocno)
1131 break;
1133 ALLOCNO_LEFT_CONFLICTS_NUM (allocno) = conflict_allocnos_size;
1136 /* The function put ALLOCNO in a bucket corresponding to its number and
1137 size of its conflicting allocnos and hard registers. */
1138 static void
1139 put_allocno_into_bucket (allocno_t allocno)
1141 int hard_regs_num;
1142 enum reg_class cover_class;
1144 cover_class = ALLOCNO_COVER_CLASS (allocno);
1145 hard_regs_num = class_hard_regs_num[cover_class];
1146 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
1147 return;
1148 ALLOCNO_IN_GRAPH_P (allocno) = TRUE;
1149 setup_allocno_left_conflicts_num (allocno);
1150 setup_allocno_available_regs_num (allocno);
1151 if (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
1152 + reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
1153 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno))
1154 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
1155 else
1156 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
1159 /* The function is used to sort allocnos according to their execution
1160 frequencies. */
1161 static int
1162 copy_freq_compare_func (const void *v1p, const void *v2p)
1164 copy_t cp1 = *(const copy_t *) v1p, cp2 = *(const copy_t *) v2p;
1165 int pri1, pri2;
1167 pri1 = cp1->freq;
1168 pri2 = cp2->freq;
1169 if (pri2 - pri1)
1170 return pri2 - pri1;
1172 /* If freqencies are equal, sort by copies, so that the results of
1173 qsort leave nothing to chance. */
1174 return cp1->num - cp2->num;
1177 /* The function merges two sets of coalesced allocnos given
1178 correspondingly by allocnos A1 and A2 (more accurately merging A2
1179 set into A1 set). */
1180 static void
1181 merge_allocnos (allocno_t a1, allocno_t a2)
1183 allocno_t a, first, last, next;
1185 first = ALLOCNO_FIRST_COALESCED_ALLOCNO (a1);
1186 if (first == ALLOCNO_FIRST_COALESCED_ALLOCNO (a2))
1187 return;
1188 for (last = a2, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1189 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1191 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = first;
1192 if (a == a2)
1193 break;
1194 last = a;
1196 next = ALLOCNO_NEXT_COALESCED_ALLOCNO (first);
1197 ALLOCNO_NEXT_COALESCED_ALLOCNO (first) = a2;
1198 ALLOCNO_NEXT_COALESCED_ALLOCNO (last) = next;
1201 /* The function returns TRUE if there are conflicting allocnos from
1202 two sets of coalesced allocnos given correspondingly by allocnos
1203 A1 and A2. If RELOAD_P is TRUE, we use live ranges to find
1204 conflicts because conflicts are represented only for allocnos of
1205 the same cover class and during the reload pass we coalesce
1206 allocnos for sharing stack memory slots. */
1207 static int
1208 coalesced_allocno_conflict_p (allocno_t a1, allocno_t a2, int reload_p)
1210 allocno_t a, conflict_allocno;
1211 allocno_conflict_iterator aci;
1213 if (allocno_coalesced_p)
1215 bitmap_clear (processed_coalesced_allocno_bitmap);
1216 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1217 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1219 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
1220 if (a == a1)
1221 break;
1224 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1225 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1227 if (reload_p)
1229 for (conflict_allocno = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1230 conflict_allocno
1231 = ALLOCNO_NEXT_COALESCED_ALLOCNO (conflict_allocno))
1233 if (allocno_live_ranges_intersect_p (a, conflict_allocno))
1234 return TRUE;
1235 if (conflict_allocno == a1)
1236 break;
1239 else
1241 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
1242 if (conflict_allocno == a1
1243 || (allocno_coalesced_p
1244 && bitmap_bit_p (processed_coalesced_allocno_bitmap,
1245 ALLOCNO_NUM (conflict_allocno))))
1246 return TRUE;
1248 if (a == a2)
1249 break;
1251 return FALSE;
1254 /* The major function for aggressive allocno coalescing. For the
1255 reload pass (RELOAD_P) we coalesce only spilled allocnos. If some
1256 allocnos have been coalesced, we set up flag
1257 allocno_coalesced_p. */
1258 static void
1259 coalesce_allocnos (int reload_p)
1261 allocno_t a;
1262 copy_t cp, next_cp, *sorted_copies;
1263 enum reg_class cover_class;
1264 enum machine_mode mode;
1265 unsigned int j;
1266 int i, n, cp_num, regno;
1267 bitmap_iterator bi;
1269 sorted_copies = ira_allocate (copies_num * sizeof (copy_t));
1270 cp_num = 0;
1271 /* Collect copies. */
1272 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
1274 a = allocnos[j];
1275 regno = ALLOCNO_REGNO (a);
1276 if ((! reload_p && ALLOCNO_ASSIGNED_P (a))
1277 || (reload_p
1278 && (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
1279 || (regno < reg_equiv_len
1280 && (reg_equiv_const[regno] != NULL_RTX
1281 || reg_equiv_invariant_p[regno])))))
1282 continue;
1283 cover_class = ALLOCNO_COVER_CLASS (a);
1284 mode = ALLOCNO_MODE (a);
1285 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1287 if (cp->first == a)
1289 next_cp = cp->next_first_allocno_copy;
1290 regno = ALLOCNO_REGNO (cp->second);
1291 if ((reload_p
1292 || (ALLOCNO_COVER_CLASS (cp->second) == cover_class
1293 && ALLOCNO_MODE (cp->second) == mode))
1294 && cp->insn != NULL
1295 && ((! reload_p && ! ALLOCNO_ASSIGNED_P (cp->second))
1296 || (reload_p
1297 && ALLOCNO_ASSIGNED_P (cp->second)
1298 && ALLOCNO_HARD_REGNO (cp->second) < 0
1299 && (regno >= reg_equiv_len
1300 || (! reg_equiv_invariant_p[regno]
1301 && reg_equiv_const[regno] == NULL_RTX)))))
1302 sorted_copies[cp_num++] = cp;
1304 else if (cp->second == a)
1305 next_cp = cp->next_second_allocno_copy;
1306 else
1307 gcc_unreachable ();
1310 qsort (sorted_copies, cp_num, sizeof (copy_t), copy_freq_compare_func);
1311 /* Coalesced copies, most frequently executed first. */
1312 for (; cp_num != 0;)
1314 for (i = 0; i < cp_num; i++)
1316 cp = sorted_copies[i];
1317 if (! coalesced_allocno_conflict_p (cp->first, cp->second, reload_p))
1319 allocno_coalesced_p = TRUE;
1320 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1321 fprintf
1322 (ira_dump_file,
1323 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
1324 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1325 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
1326 cp->freq);
1327 merge_allocnos (cp->first, cp->second);
1328 i++;
1329 break;
1332 /* Collect the rest of copies. */
1333 for (n = 0; i < cp_num; i++)
1335 cp = sorted_copies[i];
1336 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->first)
1337 != ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->second))
1338 sorted_copies[n++] = cp;
1340 cp_num = n;
1342 ira_free (sorted_copies);
1345 /* Function implements Chaitin-Briggs coloring for allocnos in
1346 COLORING_ALLOCNO_BITMAP taking into account allocnos in
1347 CONSIDERATION_ALLOCNO_BITMAP. */
1348 static void
1349 color_allocnos (void)
1351 unsigned int i;
1352 bitmap_iterator bi;
1353 allocno_t a;
1355 allocno_coalesced_p = FALSE;
1356 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
1357 if (flag_ira_coalesce)
1358 coalesce_allocnos (FALSE);
1359 /* Put the allocnos into the corresponding buckets. */
1360 colorable_allocno_bucket = NULL;
1361 uncolorable_allocno_bucket = NULL;
1362 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1364 a = allocnos[i];
1365 if (ALLOCNO_COVER_CLASS (a) == NO_REGS)
1367 ALLOCNO_HARD_REGNO (a) = -1;
1368 ALLOCNO_ASSIGNED_P (a) = TRUE;
1369 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1370 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1371 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1373 fprintf (ira_dump_file, " Spill");
1374 print_coalesced_allocno (a);
1375 fprintf (ira_dump_file, "\n");
1377 continue;
1379 put_allocno_into_bucket (a);
1381 push_allocnos_to_stack ();
1382 pop_allocnos_from_stack ();
1383 if (flag_ira_coalesce)
1384 /* We don't need coalesced allocnos for reassign_pseudos. */
1385 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1387 a = allocnos[i];
1388 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
1389 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
1391 ira_free_bitmap (processed_coalesced_allocno_bitmap);
1392 allocno_coalesced_p = FALSE;
1397 /* The function outputs information about the loop given by its
1398 LOOP_TREE_NODE. */
1399 static void
1400 print_loop_title (loop_tree_node_t loop_tree_node)
1402 unsigned int j;
1403 bitmap_iterator bi;
1405 ira_assert (loop_tree_node->loop != NULL);
1406 fprintf (ira_dump_file,
1407 "\n Loop %d (father %d, header bb%d, depth %d)\n ref:",
1408 loop_tree_node->loop->num,
1409 (loop_tree_node->father == NULL
1410 ? -1 : loop_tree_node->father->loop->num),
1411 loop_tree_node->loop->header->index,
1412 loop_depth (loop_tree_node->loop));
1413 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->mentioned_allocnos, 0, j, bi)
1414 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (allocnos[j]));
1415 fprintf (ira_dump_file, "\n modified regnos:");
1416 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
1417 fprintf (ira_dump_file, " %d", j);
1418 fprintf (ira_dump_file, "\n border:");
1419 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
1420 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (allocnos[j]));
1421 fprintf (ira_dump_file, "\n Pressure:");
1422 for (j = 0; (int) j < reg_class_cover_size; j++)
1424 enum reg_class cover_class;
1426 cover_class = reg_class_cover[j];
1427 if (loop_tree_node->reg_pressure[cover_class] == 0)
1428 continue;
1429 fprintf (ira_dump_file, " %s=%d", reg_class_names[cover_class],
1430 loop_tree_node->reg_pressure[cover_class]);
1432 fprintf (ira_dump_file, "\n");
1435 /* The function does coloring for allocnos inside loop (in extreme
1436 case it can be all function) given by the corresponding
1437 LOOP_TREE_NODE. The function is called for each loop during
1438 top-down traverse of the loop tree. */
1439 static void
1440 color_pass (loop_tree_node_t loop_tree_node)
1442 int regno, hard_regno, index = -1;
1443 int cost, exit_freq, enter_freq;
1444 unsigned int j;
1445 bitmap_iterator bi;
1446 enum machine_mode mode;
1447 enum reg_class class, cover_class;
1448 allocno_t a, subloop_allocno;
1449 loop_tree_node_t subloop_node;
1451 ira_assert (loop_tree_node->bb == NULL);
1452 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1453 print_loop_title (loop_tree_node);
1455 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->mentioned_allocnos);
1456 bitmap_ior_into (coloring_allocno_bitmap, loop_tree_node->border_allocnos);
1457 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
1458 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
1460 a = allocnos[j];
1461 if (! ALLOCNO_ASSIGNED_P (a))
1462 continue;
1463 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
1465 /* Color all mentioned allocnos including transparent ones. */
1466 color_allocnos ();
1467 /* Update costs of the corresponding allocnos in the subloops. */
1468 for (subloop_node = loop_tree_node->subloops;
1469 subloop_node != NULL;
1470 subloop_node = subloop_node->subloop_next)
1472 ira_assert (subloop_node->bb == NULL);
1473 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
1475 a = allocnos[j];
1476 mode = ALLOCNO_MODE (a);
1477 class = ALLOCNO_COVER_CLASS (a);
1478 hard_regno = ALLOCNO_HARD_REGNO (a);
1479 if (hard_regno >= 0)
1481 index = class_hard_reg_index[class][hard_regno];
1482 ira_assert (index >= 0);
1484 regno = ALLOCNO_REGNO (a);
1485 /* ??? conflict costs */
1486 if (ALLOCNO_CAP_MEMBER (a) == NULL)
1488 subloop_allocno = subloop_node->regno_allocno_map[regno];
1489 if (subloop_allocno == NULL)
1490 continue;
1491 if ((flag_ira_algorithm == IRA_ALGORITHM_MIXED
1492 && (loop_tree_node->reg_pressure[class]
1493 <= available_class_regs[class]))
1494 || (hard_regno < 0
1495 && ! bitmap_bit_p (subloop_node->mentioned_allocnos,
1496 ALLOCNO_NUM (subloop_allocno))))
1498 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
1500 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1501 ALLOCNO_ASSIGNED_P (subloop_allocno) = TRUE;
1502 if (hard_regno >= 0)
1503 update_copy_costs (subloop_allocno, TRUE);
1504 /* We don't need updated costs anymore: */
1505 free_allocno_updated_costs (subloop_allocno);
1507 continue;
1509 exit_freq = loop_edge_freq (subloop_node, regno, TRUE);
1510 enter_freq = loop_edge_freq (subloop_node, regno, FALSE);
1511 ira_assert (regno < reg_equiv_len);
1512 if (reg_equiv_invariant_p[regno]
1513 || reg_equiv_const[regno] != NULL_RTX)
1515 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
1517 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1518 ALLOCNO_ASSIGNED_P (subloop_allocno) = TRUE;
1519 if (hard_regno >= 0)
1520 update_copy_costs (subloop_allocno, TRUE);
1521 /* We don't need updated costs anymore: */
1522 free_allocno_updated_costs (subloop_allocno);
1525 else if (hard_regno < 0)
1527 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
1528 -= ((memory_move_cost[mode][class][1] * enter_freq)
1529 + (memory_move_cost[mode][class][0] * exit_freq));
1531 else
1533 cover_class = ALLOCNO_COVER_CLASS (subloop_allocno);
1534 allocate_and_set_costs
1535 (&ALLOCNO_HARD_REG_COSTS (subloop_allocno), cover_class,
1536 ALLOCNO_COVER_CLASS_COST (subloop_allocno));
1537 allocate_and_set_costs
1538 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno),
1539 cover_class, 0);
1540 cost = (register_move_cost[mode][class][class]
1541 * (exit_freq + enter_freq));
1542 ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
1543 ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
1544 -= cost;
1545 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
1546 += (memory_move_cost[mode][class][0] * enter_freq
1547 + memory_move_cost[mode][class][1] * exit_freq);
1548 if (ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1549 > ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index])
1550 ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1551 = ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index];
1554 else
1556 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
1557 if (ALLOCNO_LOOP_TREE_NODE (subloop_allocno) != subloop_node)
1558 continue;
1559 if ((flag_ira_algorithm == IRA_ALGORITHM_MIXED
1560 && loop_tree_node->reg_pressure[class]
1561 <= available_class_regs[class])
1562 || (hard_regno < 0
1563 && ! bitmap_bit_p (subloop_node->mentioned_allocnos,
1564 ALLOCNO_NUM (subloop_allocno))))
1566 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
1568 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1569 ALLOCNO_ASSIGNED_P (subloop_allocno) = TRUE;
1570 if (hard_regno >= 0)
1571 update_copy_costs (subloop_allocno, TRUE);
1572 /* We don't need updated costs anymore: */
1573 free_allocno_updated_costs (subloop_allocno);
1576 else if (flag_ira_propagate_cost && hard_regno >= 0)
1578 exit_freq = loop_edge_freq (subloop_node, -1, TRUE);
1579 enter_freq = loop_edge_freq (subloop_node, -1, FALSE);
1580 cost = (register_move_cost[mode][class][class]
1581 * (exit_freq + enter_freq));
1582 cover_class = ALLOCNO_COVER_CLASS (subloop_allocno);
1583 allocate_and_set_costs
1584 (&ALLOCNO_HARD_REG_COSTS (subloop_allocno), cover_class,
1585 ALLOCNO_COVER_CLASS_COST (subloop_allocno));
1586 allocate_and_set_costs
1587 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno),
1588 cover_class, 0);
1589 ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
1590 ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
1591 -= cost;
1592 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
1593 += (memory_move_cost[mode][class][0] * enter_freq
1594 + memory_move_cost[mode][class][1] * exit_freq);
1595 if (ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1596 > ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index])
1597 ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1598 = ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index];
1605 /* The function initialized common data for coloring and calls
1606 functions to do Chaitin-Briggs and regional coloring. */
1607 static void
1608 do_coloring (void)
1610 coloring_allocno_bitmap = ira_allocate_bitmap ();
1612 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1613 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
1615 traverse_loop_tree (FALSE, ira_loop_tree_root, color_pass, NULL);
1617 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1618 print_disposition (ira_dump_file);
1620 ira_free_bitmap (coloring_allocno_bitmap);
1625 /* The functions moves spill/restore code, which are to be generated
1626 in ira-emit.c, to less frequent points (if it is profitable) by
1627 reassigning some allocnos (in loop with subloops containing in
1628 another loop) to memory which results in longer live-range where
1629 the corresponding pseudo-registers will be in memory. */
1630 static void
1631 move_spill_restore (void)
1633 int cost, changed_p, regno, hard_regno, hard_regno2, index;
1634 int enter_freq, exit_freq;
1635 enum machine_mode mode;
1636 enum reg_class class;
1637 allocno_t a, father_allocno, subloop_allocno;
1638 loop_tree_node_t father, loop_node, subloop_node;
1639 allocno_iterator ai;
1641 for (;;)
1643 changed_p = FALSE;
1644 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1645 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
1646 FOR_EACH_ALLOCNO (a, ai)
1648 regno = ALLOCNO_REGNO (a);
1649 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
1650 if (ALLOCNO_CAP_MEMBER (a) != NULL
1651 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
1652 || loop_node->children == NULL
1653 /* don't do the optimization because it can create
1654 copies and the reload pass can spill the allocno set
1655 by copy although the allocno will not get memory
1656 slot. */
1657 || reg_equiv_invariant_p[regno]
1658 || reg_equiv_const[regno] != NULL_RTX)
1659 continue;
1660 mode = ALLOCNO_MODE (a);
1661 class = ALLOCNO_COVER_CLASS (a);
1662 index = class_hard_reg_index[class][hard_regno];
1663 ira_assert (index >= 0);
1664 cost = (ALLOCNO_MEMORY_COST (a)
1665 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
1666 ? ALLOCNO_COVER_CLASS_COST (a)
1667 : ALLOCNO_HARD_REG_COSTS (a)[index]));
1668 for (subloop_node = loop_node->subloops;
1669 subloop_node != NULL;
1670 subloop_node = subloop_node->subloop_next)
1672 ira_assert (subloop_node->bb == NULL);
1673 subloop_allocno = subloop_node->regno_allocno_map[regno];
1674 if (subloop_allocno == NULL)
1675 continue;
1676 /* We have accumulated cost. To get the real cost of
1677 allocno usage in the loop we should subtract costs of
1678 the subloop allocnos. */
1679 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
1680 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
1681 ? ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1682 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
1683 exit_freq = loop_edge_freq (subloop_node, regno, TRUE);
1684 enter_freq = loop_edge_freq (subloop_node, regno, FALSE);
1685 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
1686 cost -= (memory_move_cost[mode][class][0] * exit_freq
1687 + memory_move_cost[mode][class][1] * enter_freq);
1688 else
1690 cost += (memory_move_cost[mode][class][0] * exit_freq
1691 + memory_move_cost[mode][class][1] * enter_freq);
1692 if (hard_regno2 != hard_regno)
1693 cost -= (register_move_cost[mode][class][class]
1694 * (exit_freq + enter_freq));
1697 if ((father = loop_node->father) != NULL
1698 && (father_allocno = father->regno_allocno_map[regno]) != NULL)
1700 exit_freq = loop_edge_freq (loop_node, regno, TRUE);
1701 enter_freq = loop_edge_freq (loop_node, regno, FALSE);
1702 if ((hard_regno2 = ALLOCNO_HARD_REGNO (father_allocno)) < 0)
1703 cost -= (memory_move_cost[mode][class][0] * exit_freq
1704 + memory_move_cost[mode][class][1] * enter_freq);
1705 else
1707 cost += (memory_move_cost[mode][class][1] * exit_freq
1708 + memory_move_cost[mode][class][0] * enter_freq);
1709 if (hard_regno2 != hard_regno)
1710 cost -= (register_move_cost[mode][class][class]
1711 * (exit_freq + enter_freq));
1714 if (cost < 0)
1716 ALLOCNO_HARD_REGNO (a) = -1;
1717 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1719 fprintf
1720 (ira_dump_file,
1721 " Moving spill/restore for a%dr%d up from loop %d",
1722 ALLOCNO_NUM (a), regno, loop_node->loop->num);
1723 fprintf (ira_dump_file, " - profit %d\n", -cost);
1725 changed_p = TRUE;
1728 if (! changed_p)
1729 break;
1735 /* Update current hard reg costs and current conflict hard reg costs
1736 for allocno A. It is done by processing its copies containing
1737 other allocnos already assigned. */
1738 static void
1739 update_curr_costs (allocno_t a)
1741 int i, hard_regno, cost;
1742 enum machine_mode mode;
1743 enum reg_class cover_class, class;
1744 allocno_t another_a;
1745 copy_t cp, next_cp;
1747 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1748 cover_class = ALLOCNO_COVER_CLASS (a);
1749 if (cover_class == NO_REGS)
1750 return;
1751 mode = ALLOCNO_MODE (a);
1752 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1754 if (cp->first == a)
1756 next_cp = cp->next_first_allocno_copy;
1757 another_a = cp->second;
1759 else if (cp->second == a)
1761 next_cp = cp->next_second_allocno_copy;
1762 another_a = cp->first;
1764 else
1765 gcc_unreachable ();
1766 if (cover_class != ALLOCNO_COVER_CLASS (another_a)
1767 || ! ALLOCNO_ASSIGNED_P (another_a)
1768 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
1769 continue;
1770 class = REGNO_REG_CLASS (hard_regno);
1771 i = class_hard_reg_index[cover_class][hard_regno];
1772 ira_assert (i >= 0);
1773 cost = (cp->first == a
1774 ? register_move_cost[mode][class][cover_class]
1775 : register_move_cost[mode][cover_class][class]);
1776 allocate_and_set_or_copy_costs
1777 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1778 cover_class, ALLOCNO_COVER_CLASS_COST (a),
1779 ALLOCNO_HARD_REG_COSTS (a));
1780 allocate_and_set_or_copy_costs
1781 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1782 cover_class, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
1783 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
1784 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
1788 /* Map: allocno number -> allocno priority. */
1789 static int *allocno_priorities;
1791 /* Allocate array ALLOCNO_PRIORITIES and set up priorities for N allocnos in
1792 array CONSIDERATION_ALLOCNOS. */
1793 static void
1794 start_allocno_priorities (allocno_t *consideration_allocnos, int n)
1796 int i, length;
1797 allocno_t a;
1798 allocno_live_range_t r;
1800 for (i = 0; i < n; i++)
1802 a = consideration_allocnos[i];
1803 for (length = 0, r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
1804 length += r->finish - r->start + 1;
1805 if (length == 0)
1807 allocno_priorities[ALLOCNO_NUM (a)] = 0;
1808 continue;
1810 ira_assert (length > 0 && ALLOCNO_NREFS (a) >= 0);
1811 allocno_priorities[ALLOCNO_NUM (a)]
1812 = (((double) (floor_log2 (ALLOCNO_NREFS (a)) * ALLOCNO_FREQ (a))
1813 / length)
1814 * (10000 / REG_FREQ_MAX) * PSEUDO_REGNO_SIZE (ALLOCNO_REGNO (a)));
1818 /* The function is used to sort allocnos according to their priorities
1819 which are calculated analogous to ones in file `global.c'. */
1820 static int
1821 allocno_priority_compare_func (const void *v1p, const void *v2p)
1823 allocno_t a1 = *(const allocno_t *) v1p, a2 = *(const allocno_t *) v2p;
1824 int pri1, pri2;
1826 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
1827 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
1828 if (pri2 - pri1)
1829 return pri2 - pri1;
1831 /* If regs are equally good, sort by allocnos, so that the results of
1832 qsort leave nothing to chance. */
1833 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
1836 /* Try to assign hard registers to the unassigned allocnos and
1837 allocnos conflicting with them or conflicting with allocnos whose
1838 regno >= START_REGNO. The function is called after spill/restore
1839 placement optimization and ira_flattening, so more allocnos
1840 (including ones created in ira-emit.c) will have a chance to get a
1841 hard register. We use simple assignment algorithm based on
1842 priorities. */
1843 void
1844 reassign_conflict_allocnos (int start_regno)
1846 int i, allocnos_to_color_num;
1847 allocno_t a, conflict_a;
1848 allocno_conflict_iterator aci;
1849 enum reg_class cover_class;
1850 bitmap allocnos_to_color;
1851 allocno_iterator ai;
1853 allocnos_to_color = ira_allocate_bitmap ();
1854 allocnos_to_color_num = 0;
1855 FOR_EACH_ALLOCNO (a, ai)
1857 if (! ALLOCNO_ASSIGNED_P (a)
1858 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
1860 if (ALLOCNO_COVER_CLASS (a) != NO_REGS)
1861 sorted_allocnos[allocnos_to_color_num++] = a;
1862 else
1864 ALLOCNO_ASSIGNED_P (a) = TRUE;
1865 ALLOCNO_HARD_REGNO (a) = -1;
1866 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1867 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1869 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
1871 if (ALLOCNO_REGNO (a) < start_regno
1872 || (cover_class = ALLOCNO_COVER_CLASS (a)) == NO_REGS)
1873 continue;
1874 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
1876 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_a));
1877 if (bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
1878 continue;
1879 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a));
1880 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
1883 ira_free_bitmap (allocnos_to_color);
1884 if (allocnos_to_color_num > 1)
1886 start_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
1887 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (allocno_t),
1888 allocno_priority_compare_func);
1890 for (i = 0; i < allocnos_to_color_num; i++)
1892 a = sorted_allocnos[i];
1893 ALLOCNO_ASSIGNED_P (a) = FALSE;
1894 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1895 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1896 update_curr_costs (a);
1898 for (i = 0; i < allocnos_to_color_num; i++)
1900 a = sorted_allocnos[i];
1901 if (assign_hard_reg (a, TRUE))
1903 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1904 fprintf
1905 (ira_dump_file,
1906 " Secondary allocation: assign hard reg %d to reg %d\n",
1907 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
1914 /* This page contains code to coalesce memory stack slots used by
1915 spilled allocnos. This results in smaller stack frame, better data
1916 locality, and in smaller code for some architectures like
1917 x86/x86_64 where insn size depends on address displacement value.
1918 On the other hand, it can worsen insn scheduling after the RA but
1919 in practice it is less important than smaller stack frames. */
1921 /* Usage cost and order number of coalesced allocno set to which
1922 given pseudo register belongs to. */
1923 static int *regno_coalesced_allocno_cost;
1924 static int *regno_coalesced_allocno_num;
1926 /* The function is used to sort pseudos according frequencies of
1927 coalesced allocno sets they belong to (putting most frequently ones
1928 first), and according to coalesced allocno set order numbers. */
1929 static int
1930 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
1932 const int regno1 = *(const int *) v1p;
1933 const int regno2 = *(const int *) v2p;
1934 int diff;
1936 if ((diff = (regno_coalesced_allocno_cost[regno2]
1937 - regno_coalesced_allocno_cost[regno1])) != 0)
1938 return diff;
1939 if ((diff = (regno_coalesced_allocno_num[regno1]
1940 - regno_coalesced_allocno_num[regno2])) != 0)
1941 return diff;
1942 return regno1 - regno2;
1945 /* Widest width in which each pseudo reg is referred to (via subreg).
1946 It is used for sorting pseudo registers. */
1947 static unsigned int *regno_max_ref_width;
1949 /* The function is used to sort pseudos according their slot numbers
1950 (putting ones with smaller numbers first, or last when the frame
1951 pointer is not needed). */
1952 static int
1953 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
1955 const int regno1 = *(const int *) v1p;
1956 const int regno2 = *(const int *) v2p;
1957 allocno_t a1 = regno_allocno_map[regno1];
1958 allocno_t a2 = regno_allocno_map[regno2];
1959 int diff, slot_num1, slot_num2;
1960 int total_size1, total_size2;
1962 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
1964 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
1965 return (const int *) v1p - (const int *) v2p; /* Save the order. */
1966 return 1;
1968 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
1969 return -1;
1970 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
1971 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
1972 if ((diff = slot_num1 - slot_num2) != 0)
1973 return frame_pointer_needed ? diff : -diff;
1974 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1), regno_max_ref_width[regno1]);
1975 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2), regno_max_ref_width[regno2]);
1976 if ((diff = total_size2 - total_size1) != 0)
1977 return diff;
1978 return (const int *) v1p - (const int *) v2p; /* Save the order. */
1981 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
1982 for coalesced allocno sets containing allocnos with their regnos
1983 given in array PSEUDO_REGNOS of length N. */
1984 static void
1985 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
1987 int i, num, regno, cost;
1988 allocno_t allocno, a;
1990 for (num = i = 0; i < n; i++)
1992 regno = pseudo_regnos[i];
1993 allocno = regno_allocno_map[regno];
1994 if (allocno == NULL)
1996 regno_coalesced_allocno_cost[regno] = 0;
1997 regno_coalesced_allocno_num[regno] = ++num;
1998 continue;
2000 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2001 continue;
2002 num++;
2003 for (cost = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2004 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2006 cost += ALLOCNO_FREQ (a);
2007 if (a == allocno)
2008 break;
2010 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2011 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2013 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
2014 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
2015 if (a == allocno)
2016 break;
2021 /* The function collects spilled allocnos representing coalesced
2022 allocno sets (the first coalesced allocno). The collected allocnos
2023 are returned through array SPILLED_COALESCED_ALLOCNOS. The
2024 function returns the number of the collected allocnos. The
2025 allocnos are given by their regnos in array PSEUDO_REGNOS of length
2026 N. */
2027 static int
2028 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
2029 allocno_t *spilled_coalesced_allocnos)
2031 int i, num, regno;
2032 allocno_t allocno;
2034 for (num = i = 0; i < n; i++)
2036 regno = pseudo_regnos[i];
2037 allocno = regno_allocno_map[regno];
2038 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
2039 || ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2040 continue;
2041 spilled_coalesced_allocnos[num++] = allocno;
2043 return num;
2046 /* We have coalesced allocnos involving in copies. This function
2047 coalesces allocnos further in order to share the same memory stack
2048 slot. Allocnos representing sets of allocnos coalesced before the
2049 call are given in array SPILLED_COALESCED_ALLOCNOS of length NUM.
2050 Return TRUE if some allocnos were coalesced in the function. */
2051 static int
2052 coalesce_spill_slots (allocno_t *spilled_coalesced_allocnos, int num)
2054 int i, j;
2055 allocno_t allocno, a;
2056 int merged_p = FALSE;
2058 /* Coalesce non-conflicting spilled allocnos preferring most
2059 frequently used. */
2060 for (i = 0; i < num; i++)
2062 allocno = spilled_coalesced_allocnos[i];
2063 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2064 || (ALLOCNO_REGNO (allocno) < reg_equiv_len
2065 && (reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)]
2066 || reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX)))
2067 continue;
2068 for (j = 0; j < i; j++)
2070 a = spilled_coalesced_allocnos[j];
2071 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) != a
2072 || (ALLOCNO_REGNO (a) < reg_equiv_len
2073 && (reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
2074 || reg_equiv_const[ALLOCNO_REGNO (a)] != NULL_RTX))
2075 || coalesced_allocno_conflict_p (allocno, a, TRUE))
2076 continue;
2077 allocno_coalesced_p = TRUE;
2078 merged_p = TRUE;
2079 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2080 fprintf (ira_dump_file,
2081 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
2082 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
2083 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2084 merge_allocnos (a, allocno);
2085 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a);
2088 return merged_p;
2091 /* The function sorts pseudo-register numbers in array PSEUDO_REGNOS
2092 of length N for subsequent assigning stack slots to them in the
2093 reload pass. To do this we coalesce spilled allocnos first to
2094 decrease the number of memory-memory move insns. This function is
2095 called by the reload. */
2096 void
2097 sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
2098 unsigned int *reg_max_ref_width)
2100 int max_regno = max_reg_num ();
2101 int i, regno, num, slot_num;
2102 allocno_t allocno, a;
2103 allocno_iterator ai;
2104 allocno_t *spilled_coalesced_allocnos;
2106 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
2107 /* Set up allocnos can be coalesced. */
2108 coloring_allocno_bitmap = ira_allocate_bitmap ();
2109 for (i = 0; i < n; i++)
2111 regno = pseudo_regnos[i];
2112 allocno = regno_allocno_map[regno];
2113 if (allocno != NULL)
2114 bitmap_set_bit (coloring_allocno_bitmap,
2115 ALLOCNO_NUM (allocno));
2117 allocno_coalesced_p = FALSE;
2118 coalesce_allocnos (TRUE);
2119 ira_free_bitmap (coloring_allocno_bitmap);
2120 regno_coalesced_allocno_cost = ira_allocate (max_regno * sizeof (int));
2121 regno_coalesced_allocno_num = ira_allocate (max_regno * sizeof (int));
2122 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
2123 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2124 /* Sort regnos according frequencies of the corresponding coalesced
2125 allocno sets. */
2126 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
2127 spilled_coalesced_allocnos
2128 = ira_allocate (allocnos_num * sizeof (allocno_t));
2129 /* Collect allocnos representing the spilled coalesced allocno
2130 sets. */
2131 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2132 spilled_coalesced_allocnos);
2133 if (flag_ira_share_spill_slots
2134 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
2136 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2137 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
2138 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2139 spilled_coalesced_allocnos);
2141 ira_free_bitmap (processed_coalesced_allocno_bitmap);
2142 allocno_coalesced_p = FALSE;
2143 /* Assign stack slot numbers to spilled allocno sets, use smaller
2144 numbers for most frequently used coalesced allocnos. -1 is
2145 reserved for dynamic search of stack slots for pseudos spilled by
2146 the reload. */
2147 slot_num = 1;
2148 for (i = 0; i < num; i++)
2150 allocno = spilled_coalesced_allocnos[i];
2151 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2152 || ALLOCNO_HARD_REGNO (allocno) >= 0
2153 || (ALLOCNO_REGNO (allocno) < reg_equiv_len
2154 && (reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)]
2155 || reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX)))
2156 continue;
2157 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2158 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
2159 slot_num++;
2160 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2161 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2163 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
2164 ALLOCNO_HARD_REGNO (a) = -slot_num;
2165 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2166 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
2167 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
2168 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
2169 reg_max_ref_width[ALLOCNO_REGNO (a)]));
2171 if (a == allocno)
2172 break;
2174 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2175 fprintf (ira_dump_file, "\n");
2177 spilled_reg_stack_slots_num = slot_num - 1;
2178 ira_free (spilled_coalesced_allocnos);
2179 /* Sort regnos according the slot numbers. */
2180 regno_max_ref_width = reg_max_ref_width;
2181 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
2182 /* Uncoalesce allocnos which is necessary for (re)assigning during
2183 the reload pass. */
2184 FOR_EACH_ALLOCNO (a, ai)
2186 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
2187 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
2189 ira_free (regno_coalesced_allocno_num);
2190 ira_free (regno_coalesced_allocno_cost);
2195 /* This page contains code used by the reload pass to improve the
2196 final code. */
2198 /* The function is called from the reload pass to mark changes in the
2199 allocation of REGNO made by the reload. Remember that reg_renumber
2200 reflects the change result. */
2201 void
2202 mark_allocation_change (int regno)
2204 allocno_t a = regno_allocno_map[regno];
2205 int old_hard_regno, hard_regno, cost;
2206 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
2208 ira_assert (a != NULL);
2209 hard_regno = reg_renumber[regno];
2210 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
2211 return;
2212 if (old_hard_regno < 0)
2213 cost = -ALLOCNO_MEMORY_COST (a);
2214 else
2216 ira_assert (class_hard_reg_index[cover_class][old_hard_regno] >= 0);
2217 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
2218 ? ALLOCNO_COVER_CLASS_COST (a)
2219 : ALLOCNO_HARD_REG_COSTS (a)
2220 [class_hard_reg_index[cover_class][old_hard_regno]]);
2221 update_copy_costs (a, FALSE);
2223 overall_cost -= cost;
2224 ALLOCNO_HARD_REGNO (a) = hard_regno;
2225 if (hard_regno < 0)
2227 ALLOCNO_HARD_REGNO (a) = -1;
2228 cost += ALLOCNO_MEMORY_COST (a);
2230 else if (class_hard_reg_index[cover_class][hard_regno] >= 0)
2232 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
2233 ? ALLOCNO_COVER_CLASS_COST (a)
2234 : ALLOCNO_HARD_REG_COSTS (a)
2235 [class_hard_reg_index[cover_class][hard_regno]]);
2236 update_copy_costs (a, TRUE);
2238 else
2239 /* Reload changed class of the allocno. */
2240 cost = 0;
2241 overall_cost += cost;
2244 /* This function is called when the reload deletes memory-memory move.
2245 In this case we marks that the allocation of the corresponding
2246 allocnos should be not changed in future. Otherwise we risk to get
2247 a wrong code. */
2248 void
2249 mark_memory_move_deletion (int dst_regno, int src_regno)
2251 allocno_t dst = regno_allocno_map[dst_regno];
2252 allocno_t src = regno_allocno_map[src_regno];
2254 ira_assert (dst != NULL && src != NULL
2255 && ALLOCNO_HARD_REGNO (dst) < 0
2256 && ALLOCNO_HARD_REGNO (src) < 0);
2257 ALLOCNO_DONT_REASSIGN_P (dst) = TRUE;
2258 ALLOCNO_DONT_REASSIGN_P (src) = TRUE;
2261 /* The function tries to assign a hard register (except for
2262 FORBIDDEN_REGS) to allocno A and return TRUE in the case of
2263 success. That is an analog of retry_global_alloc for IRA. */
2264 static int
2265 allocno_reload_assign (allocno_t a, HARD_REG_SET forbidden_regs)
2267 int hard_regno;
2268 enum reg_class cover_class;
2269 int regno = ALLOCNO_REGNO (a);
2271 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), forbidden_regs);
2272 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2273 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), call_used_reg_set);
2274 ALLOCNO_ASSIGNED_P (a) = FALSE;
2275 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2276 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2277 cover_class = ALLOCNO_COVER_CLASS (a);
2278 update_curr_costs (a);
2279 assign_hard_reg (a, TRUE);
2280 hard_regno = ALLOCNO_HARD_REGNO (a);
2281 reg_renumber[regno] = hard_regno;
2282 if (hard_regno < 0)
2283 ALLOCNO_HARD_REGNO (a) = -1;
2284 else
2286 ira_assert (class_hard_reg_index[cover_class][hard_regno] >= 0);
2287 overall_cost -= (ALLOCNO_MEMORY_COST (a)
2288 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
2289 ? ALLOCNO_COVER_CLASS_COST (a)
2290 : ALLOCNO_HARD_REG_COSTS (a)
2291 [class_hard_reg_index[cover_class][hard_regno]]));
2292 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
2293 && ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
2294 call_used_reg_set))
2296 ira_assert (flag_caller_saves);
2297 caller_save_needed = 1;
2301 /* If we found a hard register, modify the RTL for the pseudo
2302 register to show the hard register, and mark the pseudo register
2303 live. */
2304 if (reg_renumber[regno] >= 0)
2306 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2307 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
2308 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
2309 mark_home_live (regno);
2311 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2312 fprintf (ira_dump_file, "\n");
2314 return reg_renumber[regno] >= 0;
2317 /* The function is used to sort pseudos according their usage
2318 frequencies (putting most frequently ones first). */
2319 static int
2320 pseudo_reg_compare (const void *v1p, const void *v2p)
2322 int regno1 = *(const int *) v1p;
2323 int regno2 = *(const int *) v2p;
2324 int diff;
2326 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
2327 return diff;
2328 return regno1 - regno2;
2331 /* The function tries to allocate hard registers to
2332 SPILLED_PSEUDO_REGS (there are NUM of them) or spilled pseudos
2333 conflicting with pseudos in SPILLED_PSEUDO_REGS. It returns TRUE
2334 and update SPILLED, if the allocation has been changed. The
2335 function doesn't use BAD_SPILL_REGS and hard registers in
2336 PSEUDO_FORBIDDEN_REGS and PSEUDO_PREVIOUS_REGS for the
2337 corresponding pseudos. The function is called by the reload pass
2338 at the end of each reload iteration. */
2340 reassign_pseudos (int *spilled_pseudo_regs, int num,
2341 HARD_REG_SET bad_spill_regs,
2342 HARD_REG_SET *pseudo_forbidden_regs,
2343 HARD_REG_SET *pseudo_previous_regs, bitmap spilled)
2345 int i, m, n, regno, changed_p;
2346 allocno_t a, conflict_a;
2347 HARD_REG_SET forbidden_regs;
2348 allocno_conflict_iterator aci;
2350 if (num > 1)
2351 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
2352 changed_p = FALSE;
2353 /* Try to assign hard registers to pseudos from
2354 SPILLED_PSEUDO_REGS. */
2355 for (m = i = 0; i < num; i++)
2357 regno = spilled_pseudo_regs[i];
2358 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
2359 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
2360 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
2361 gcc_assert (reg_renumber[regno] < 0);
2362 a = regno_allocno_map[regno];
2363 mark_allocation_change (regno);
2364 ira_assert (reg_renumber[regno] < 0);
2365 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2366 fprintf (ira_dump_file,
2367 " Spill %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
2368 ALLOCNO_MEMORY_COST (a)
2369 - ALLOCNO_COVER_CLASS_COST (a));
2370 allocno_reload_assign (a, forbidden_regs);
2371 if (reg_renumber[regno] >= 0)
2373 CLEAR_REGNO_REG_SET (spilled, regno);
2374 changed_p = TRUE;
2376 else
2377 spilled_pseudo_regs[m++] = regno;
2379 if (m == 0)
2380 return changed_p;
2381 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2383 fprintf (ira_dump_file, " Spilled regs");
2384 for (i = 0; i < m; i++)
2385 fprintf (ira_dump_file, " %d", spilled_pseudo_regs[i]);
2386 fprintf (ira_dump_file, "\n");
2388 /* Try to assign hard registers to pseudos conflicting with ones
2389 from SPILLED_PSEUDO_REGS. */
2390 for (i = n = 0; i < m; i++)
2392 regno = spilled_pseudo_regs[i];
2393 a = regno_allocno_map[regno];
2394 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
2395 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
2396 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
2397 && ! bitmap_bit_p (consideration_allocno_bitmap,
2398 ALLOCNO_NUM (conflict_a)))
2400 sorted_allocnos[n++] = conflict_a;
2401 bitmap_set_bit (consideration_allocno_bitmap,
2402 ALLOCNO_NUM (conflict_a));
2405 if (n != 0)
2407 start_allocno_priorities (sorted_allocnos, n);
2408 qsort (sorted_allocnos, n, sizeof (allocno_t),
2409 allocno_priority_compare_func);
2410 for (i = 0; i < n; i++)
2412 a = sorted_allocnos[i];
2413 regno = ALLOCNO_REGNO (a);
2414 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
2415 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
2416 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
2417 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2418 fprintf (ira_dump_file,
2419 " Try assign %d(a%d), cost=%d",
2420 regno, ALLOCNO_NUM (a),
2421 ALLOCNO_MEMORY_COST (a)
2422 - ALLOCNO_COVER_CLASS_COST (a));
2423 if (allocno_reload_assign (a, forbidden_regs))
2425 changed_p = TRUE;
2426 bitmap_clear_bit (spilled, regno);
2430 return changed_p;
2433 /* The function is called by the reload pass and returns already
2434 allocated stack slot (if any) for REGNO with given INHERENT_SIZE
2435 and TOTAL_SIZE. In the case of failure to find a slot which can be
2436 used for REGNO, the function returns NULL. */
2438 reuse_stack_slot (int regno, unsigned int inherent_size,
2439 unsigned int total_size)
2441 unsigned int i;
2442 int slot_num, best_slot_num;
2443 int cost, best_cost;
2444 copy_t cp, next_cp;
2445 allocno_t another_allocno, allocno = regno_allocno_map[regno];
2446 rtx x;
2447 bitmap_iterator bi;
2448 struct spilled_reg_stack_slot *slot = NULL;
2450 ira_assert (flag_ira && inherent_size == PSEUDO_REGNO_BYTES (regno)
2451 && inherent_size <= total_size
2452 && ALLOCNO_HARD_REGNO (allocno) < 0);
2453 if (! flag_ira_share_spill_slots)
2454 return NULL_RTX;
2455 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
2456 if (slot_num != -1)
2458 slot = &spilled_reg_stack_slots[slot_num];
2459 x = slot->mem;
2461 else
2463 best_cost = best_slot_num = -1;
2464 x = NULL_RTX;
2465 /* It means that the pseudo was spilled in the reload pass, try
2466 to reuse a slot. */
2467 for (slot_num = 0; slot_num < spilled_reg_stack_slots_num; slot_num++)
2469 slot = &spilled_reg_stack_slots[slot_num];
2470 if (slot->mem == NULL_RTX)
2471 continue;
2472 if (slot->width < total_size
2473 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
2474 continue;
2476 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2477 FIRST_PSEUDO_REGISTER, i, bi)
2479 another_allocno = regno_allocno_map[i];
2480 if (allocno_live_ranges_intersect_p (allocno, another_allocno))
2481 goto cont;
2483 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
2484 cp != NULL;
2485 cp = next_cp)
2487 if (cp->first == allocno)
2489 next_cp = cp->next_first_allocno_copy;
2490 another_allocno = cp->second;
2492 else if (cp->second == allocno)
2494 next_cp = cp->next_second_allocno_copy;
2495 another_allocno = cp->first;
2497 else
2498 gcc_unreachable ();
2499 if (cp->insn == NULL_RTX)
2500 continue;
2501 if (bitmap_bit_p (&slot->spilled_regs,
2502 ALLOCNO_REGNO (another_allocno)))
2503 cost += cp->freq;
2505 if (cost > best_cost)
2507 best_cost = cost;
2508 best_slot_num = slot_num;
2510 cont:
2513 if (best_cost >= 0)
2515 slot = &spilled_reg_stack_slots[best_slot_num];
2516 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2517 x = slot->mem;
2518 ALLOCNO_HARD_REGNO (allocno) = -best_slot_num - 2;
2521 if (x != NULL_RTX)
2523 ira_assert (slot->width >= total_size);
2524 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2525 FIRST_PSEUDO_REGISTER, i, bi)
2527 ira_assert (! pseudo_live_ranges_intersect_p (regno, i));
2529 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2530 if (internal_flag_ira_verbose > 3 && ira_dump_file)
2532 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
2533 regno, REG_FREQ (regno), slot_num);
2534 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2535 FIRST_PSEUDO_REGISTER, i, bi)
2537 if ((unsigned) regno != i)
2538 fprintf (ira_dump_file, " %d", i);
2540 fprintf (ira_dump_file, "\n");
2543 return x;
2546 /* The function is called by the reload pass every time when a new
2547 stack slot X with TOTAL_SIZE was allocated for REGNO. We store
2548 this info for subsequent reuse_stack_slot calls. */
2549 void
2550 mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
2552 struct spilled_reg_stack_slot *slot;
2553 int slot_num;
2554 allocno_t allocno;
2556 ira_assert (flag_ira && PSEUDO_REGNO_BYTES (regno) <= total_size);
2557 allocno = regno_allocno_map[regno];
2558 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
2559 if (slot_num == -1)
2561 slot_num = spilled_reg_stack_slots_num++;
2562 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
2564 slot = &spilled_reg_stack_slots[slot_num];
2565 INIT_REG_SET (&slot->spilled_regs);
2566 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2567 slot->mem = x;
2568 slot->width = total_size;
2569 if (internal_flag_ira_verbose > 3 && ira_dump_file)
2570 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
2571 regno, REG_FREQ (regno), slot_num);
2575 /* Return spill cost for pseudo-registers whose numbers are in array
2576 REGNOS (with a negative number as an end marker) for reload with
2577 given IN and OUT for INSN. Return also number points (through
2578 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
2579 the register pressure is high, number of references of the
2580 pseudo-registers (through NREFS), number of callee-clobbered
2581 hard-registers occupied by the pseudo-registers (through
2582 CALL_USED_COUNT), and the first hard regno occupied by the
2583 pseudo-registers (through FIRST_HARD_REGNO). */
2584 static int
2585 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
2586 int *excess_pressure_live_length,
2587 int *nrefs, int *call_used_count, int *first_hard_regno)
2589 int i, cost, regno, hard_regno, j, count, saved_cost, nregs, in_p, out_p;
2590 int length;
2591 allocno_t a;
2593 *nrefs = 0;
2594 for (length = count = cost = i = 0;; i++)
2596 regno = regnos[i];
2597 if (regno < 0)
2598 break;
2599 *nrefs += REG_N_REFS (regno);
2600 hard_regno = reg_renumber[regno];
2601 ira_assert (hard_regno >= 0);
2602 a = regno_allocno_map[regno];
2603 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2604 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a);
2605 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
2606 for (j = 0; j < nregs; j++)
2607 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
2608 break;
2609 if (j == nregs)
2610 count++;
2611 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
2612 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
2613 if ((in_p || out_p)
2614 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
2616 saved_cost = 0;
2617 if (in_p)
2618 saved_cost += memory_move_cost
2619 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][1];
2620 if (out_p)
2621 saved_cost
2622 += memory_move_cost
2623 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][0];
2624 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
2627 *excess_pressure_live_length = length;
2628 *call_used_count = count;
2629 hard_regno = -1;
2630 if (regnos[0] >= 0)
2632 hard_regno = reg_renumber[regnos[0]];
2634 *first_hard_regno = hard_regno;
2635 return cost;
2638 /* Return TRUE if spilling pseudo-registers whose numbers are in array
2639 REGNOS is better than spilling pseudo-registers with numbers in
2640 OTHER_REGNOS for reload with given IN and OUT for INSN. The
2641 function used by the reload pass to make better register spilling
2642 decisions. */
2644 better_spill_reload_regno_p (int *regnos, int *other_regnos,
2645 rtx in, rtx out, rtx insn)
2647 int cost, other_cost;
2648 int length, other_length;
2649 int nrefs, other_nrefs;
2650 int call_used_count, other_call_used_count;
2651 int hard_regno, other_hard_regno;
2653 cost = calculate_spill_cost (regnos, in, out, insn,
2654 &length, &nrefs, &call_used_count, &hard_regno);
2655 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
2656 &other_length, &other_nrefs,
2657 &other_call_used_count,
2658 &other_hard_regno);
2659 if (cost != other_cost)
2660 return cost < other_cost;
2661 if (length != other_length)
2662 return length > other_length;
2663 #ifdef REG_ALLOC_ORDER
2664 if (hard_regno >= 0 && other_hard_regno >= 0)
2665 return (inv_reg_alloc_order[hard_regno]
2666 < inv_reg_alloc_order[other_hard_regno]);
2667 #else
2668 if (call_used_count != other_call_used_count)
2669 return call_used_count > other_call_used_count;
2670 #endif
2671 return FALSE;
2676 /* The function returns (through CALL_CLOBBERED_REGS) hard registers
2677 changed by all function calls inside REGNO live range. The
2678 function is used to improve code for saving/restore callee-clobbered
2679 hard registers around calls (see caller-saves.c). */
2680 void
2681 collect_pseudo_call_clobbered_regs (int regno,
2682 HARD_REG_SET (*call_clobbered_regs))
2684 int i;
2685 allocno_t a;
2686 HARD_REG_SET clobbered_regs;
2687 rtx call, *allocno_calls;
2689 a = regno_allocno_map[regno];
2690 CLEAR_HARD_REG_SET (*call_clobbered_regs);
2691 allocno_calls = (VEC_address (rtx, regno_calls[regno])
2692 + ALLOCNO_CALLS_CROSSED_START (a));
2693 for (i = ALLOCNO_CALLS_CROSSED_NUM (a) - 1; i >= 0; i--)
2695 call = allocno_calls[i];
2696 get_call_invalidated_used_regs (call, &clobbered_regs, FALSE);
2697 IOR_HARD_REG_SET (*call_clobbered_regs, clobbered_regs);
2703 /* Allocate and initialize data necessary for assign_hard_reg. */
2704 void
2705 initiate_ira_assign (void)
2707 sorted_allocnos = ira_allocate (sizeof (allocno_t) * allocnos_num);
2708 sorted_allocnos_for_spilling
2709 = ira_allocate (sizeof (allocno_t) * allocnos_num);
2710 consideration_allocno_bitmap = ira_allocate_bitmap ();
2711 initiate_cost_update ();
2712 allocno_priorities = ira_allocate (sizeof (int) * allocnos_num);
2715 /* Deallocate data used by assign_hard_reg. */
2716 void
2717 finish_ira_assign (void)
2719 ira_free (sorted_allocnos_for_spilling);
2720 ira_free (sorted_allocnos);
2721 ira_free_bitmap (consideration_allocno_bitmap);
2722 finish_cost_update ();
2723 ira_free (allocno_priorities);
2728 /* Entry function doing color-based register allocation. */
2729 void
2730 ira_color (void)
2732 allocno_stack_vec = VEC_alloc (allocno_t, heap, allocnos_num);
2733 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
2734 initiate_ira_assign ();
2735 do_coloring ();
2736 finish_ira_assign ();
2737 VEC_free (allocno_t, heap, allocno_stack_vec);
2738 move_spill_restore ();
2743 /* This page contains a simple register allocator without usage of
2744 allocno conflicts. This is used for fast allocation for -O0. */
2746 /* The function does register allocation not using allocno conflicts.
2747 It uses only allocno live ranges. The algorithm is close to Chow's
2748 priority coloring. */
2749 void
2750 ira_fast_allocation (void)
2752 int i, j, k, l, class_size, hard_regno;
2753 #ifdef STACK_REGS
2754 int no_stack_reg_p;
2755 #endif
2756 enum reg_class cover_class;
2757 enum machine_mode mode;
2758 allocno_t a;
2759 allocno_iterator ai;
2760 allocno_live_range_t r;
2761 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
2763 allocno_priorities = ira_allocate (sizeof (int) * allocnos_num);
2764 FOR_EACH_ALLOCNO (a, ai)
2766 l = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2767 if (l <= 0)
2768 l = 1;
2769 allocno_priorities[ALLOCNO_NUM (a)]
2770 = (((double) (floor_log2 (ALLOCNO_NREFS (a))
2771 * (ALLOCNO_MEMORY_COST (a)
2772 - ALLOCNO_COVER_CLASS_COST (a))) / l)
2773 * (10000 / REG_FREQ_MAX)
2774 * reg_class_nregs [ALLOCNO_COVER_CLASS (a)][ALLOCNO_MODE (a)]);
2776 used_hard_regs = ira_allocate (sizeof (HARD_REG_SET) * max_point);
2777 for (i = 0; i < max_point; i++)
2778 CLEAR_HARD_REG_SET (used_hard_regs[i]);
2779 sorted_allocnos = ira_allocate (sizeof (allocno_t) * allocnos_num);
2780 memcpy (sorted_allocnos, allocnos, sizeof (allocno_t) * allocnos_num);
2781 qsort (sorted_allocnos, allocnos_num, sizeof (allocno_t),
2782 allocno_priority_compare_func);
2783 for (i = 0; i < allocnos_num; i++)
2785 a = sorted_allocnos[i];
2786 COPY_HARD_REG_SET (conflict_hard_regs, ALLOCNO_CONFLICT_HARD_REGS (a));
2787 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2788 for (j = r->start; j <= r->finish; j++)
2789 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
2790 cover_class = ALLOCNO_COVER_CLASS (a);
2791 ALLOCNO_ASSIGNED_P (a) = TRUE;
2792 ALLOCNO_HARD_REGNO (a) = -1;
2793 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
2794 conflict_hard_regs))
2795 continue;
2796 mode = ALLOCNO_MODE (a);
2797 #ifdef STACK_REGS
2798 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
2799 #endif
2800 class_size = class_hard_regs_num[cover_class];
2801 for (j = 0; j < class_size; j++)
2803 hard_regno = class_hard_regs[cover_class][j];
2804 #ifdef STACK_REGS
2805 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
2806 && hard_regno <= LAST_STACK_REG)
2807 continue;
2808 #endif
2809 if (!hard_reg_not_in_set_p (hard_regno, mode, conflict_hard_regs)
2810 || (TEST_HARD_REG_BIT
2811 (prohibited_class_mode_regs[cover_class][mode], hard_regno)))
2812 continue;
2813 ALLOCNO_HARD_REGNO (a) = hard_regno;
2814 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2815 for (k = r->start; k <= r->finish; k++)
2816 IOR_HARD_REG_SET (used_hard_regs[k],
2817 reg_mode_hard_regset[hard_regno][mode]);
2818 break;
2821 ira_free (sorted_allocnos);
2822 ira_free (used_hard_regs);
2823 ira_free (allocno_priorities);
2824 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2825 print_disposition (ira_dump_file);