]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ira-color.c
71e3f68aca078405129fb282652e5dba091659f7
[thirdparty/gcc.git] / gcc / ira-color.c
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>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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 "splay-tree.h"
41 #include "ira-int.h"
42
43 /* This file contains code for regional graph coloring, spill/restore
44 code placement optimization, and code helping the reload pass to do
45 a better job. */
46
47 /* Bitmap of allocnos which should be colored. */
48 static bitmap coloring_allocno_bitmap;
49
50 /* Bitmap of allocnos which should be taken into account during
51 coloring. In general case it contains allocnos from
52 coloring_allocno_bitmap plus other already colored conflicting
53 allocnos. */
54 static bitmap consideration_allocno_bitmap;
55
56 /* TRUE if we coalesced some allocnos. In other words, if we got
57 loops formed by members first_coalesced_allocno and
58 next_coalesced_allocno containing more one allocno. */
59 static bool allocno_coalesced_p;
60
61 /* Bitmap used to prevent a repeated allocno processing because of
62 coalescing. */
63 static bitmap processed_coalesced_allocno_bitmap;
64
65 /* All allocnos sorted according their priorities. */
66 static ira_allocno_t *sorted_allocnos;
67
68 /* Vec representing the stack of allocnos used during coloring. */
69 static VEC(ira_allocno_t,heap) *allocno_stack_vec;
70
71 /* Vec representing conflict allocnos used during assigning. */
72 static VEC(ira_allocno_t,heap) *conflict_allocno_vec;
73
74 /* Array used to choose an allocno for spilling. */
75 static ira_allocno_t *allocnos_for_spilling;
76
77 /* Pool for splay tree nodes. */
78 static alloc_pool splay_tree_node_pool;
79
80 /* When an allocno is removed from the splay tree, it is put in the
81 following vector for subsequent inserting it into the splay tree
82 after putting all colorable allocnos onto the stack. The allocno
83 could be removed from and inserted to the splay tree every time
84 when its spilling priority is changed but such solution would be
85 more costly although simpler. */
86 static VEC(ira_allocno_t,heap) *removed_splay_allocno_vec;
87
88 \f
89
90 /* This page contains functions used to choose hard registers for
91 allocnos. */
92
93 /* Array whose element value is TRUE if the corresponding hard
94 register was already allocated for an allocno. */
95 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
96
97 /* Array used to check already processed allocnos during the current
98 update_copy_costs call. */
99 static int *allocno_update_cost_check;
100
101 /* The current value of update_copy_cost call count. */
102 static int update_cost_check;
103
104 /* Allocate and initialize data necessary for function
105 update_copy_costs. */
106 static void
107 initiate_cost_update (void)
108 {
109 allocno_update_cost_check
110 = (int *) ira_allocate (ira_allocnos_num * sizeof (int));
111 memset (allocno_update_cost_check, 0, ira_allocnos_num * sizeof (int));
112 update_cost_check = 0;
113 }
114
115 /* Deallocate data used by function update_copy_costs. */
116 static void
117 finish_cost_update (void)
118 {
119 ira_free (allocno_update_cost_check);
120 }
121
122 /* When we traverse allocnos to update hard register costs, the cost
123 divisor will be multiplied by the following macro value for each
124 hop from given allocno to directly connected allocnos. */
125 #define COST_HOP_DIVISOR 4
126
127 /* This recursive function updates costs (decrease if DECR_P) of the
128 unassigned allocnos connected by copies with ALLOCNO. This update
129 increases chances to remove some copies. Copy cost is proportional
130 the copy frequency divided by DIVISOR. */
131 static void
132 update_copy_costs_1 (ira_allocno_t allocno, int hard_regno,
133 bool decr_p, int divisor)
134 {
135 int i, cost, update_cost;
136 enum machine_mode mode;
137 enum reg_class rclass, cover_class;
138 ira_allocno_t another_allocno;
139 ira_copy_t cp, next_cp;
140
141 cover_class = ALLOCNO_COVER_CLASS (allocno);
142 if (cover_class == NO_REGS)
143 return;
144 if (allocno_update_cost_check[ALLOCNO_NUM (allocno)] == update_cost_check)
145 return;
146 allocno_update_cost_check[ALLOCNO_NUM (allocno)] = update_cost_check;
147 ira_assert (hard_regno >= 0);
148 i = ira_class_hard_reg_index[cover_class][hard_regno];
149 ira_assert (i >= 0);
150 rclass = REGNO_REG_CLASS (hard_regno);
151 mode = ALLOCNO_MODE (allocno);
152 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
153 {
154 if (cp->first == allocno)
155 {
156 next_cp = cp->next_first_allocno_copy;
157 another_allocno = cp->second;
158 }
159 else if (cp->second == allocno)
160 {
161 next_cp = cp->next_second_allocno_copy;
162 another_allocno = cp->first;
163 }
164 else
165 gcc_unreachable ();
166 if (cover_class
167 != ALLOCNO_COVER_CLASS (another_allocno)
168 || ALLOCNO_ASSIGNED_P (another_allocno))
169 continue;
170 cost = (cp->second == allocno
171 ? ira_register_move_cost[mode][rclass]
172 [ALLOCNO_COVER_CLASS (another_allocno)]
173 : ira_register_move_cost[mode]
174 [ALLOCNO_COVER_CLASS (another_allocno)][rclass]);
175 if (decr_p)
176 cost = -cost;
177 ira_allocate_and_set_or_copy_costs
178 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno), cover_class,
179 ALLOCNO_COVER_CLASS_COST (another_allocno),
180 ALLOCNO_HARD_REG_COSTS (another_allocno));
181 ira_allocate_and_set_or_copy_costs
182 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
183 cover_class, 0,
184 ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
185 update_cost = cp->freq * cost / divisor;
186 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno)[i] += update_cost;
187 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno)[i]
188 += update_cost;
189 if (update_cost != 0)
190 update_copy_costs_1 (another_allocno, hard_regno,
191 decr_p, divisor * COST_HOP_DIVISOR);
192 }
193 }
194
195 /* Update the cost of allocnos to increase chances to remove some
196 copies as the result of subsequent assignment. */
197 static void
198 update_copy_costs (ira_allocno_t allocno, bool decr_p)
199 {
200 update_cost_check++;
201 update_copy_costs_1 (allocno, ALLOCNO_HARD_REGNO (allocno), decr_p, 1);
202 }
203
204 /* This recursive function updates COSTS (decrease if DECR_P) by
205 conflict costs of the unassigned allocnos connected by copies with
206 ALLOCNO. This update increases chances to remove some copies.
207 Copy cost is proportional to the copy frequency divided by
208 DIVISOR. */
209 static void
210 update_conflict_hard_regno_costs (int *costs, ira_allocno_t allocno,
211 int divisor, bool decr_p)
212 {
213 int i, cost, class_size, mult, div;
214 int *conflict_costs;
215 bool cont_p;
216 enum machine_mode mode;
217 enum reg_class cover_class;
218 ira_allocno_t another_allocno;
219 ira_copy_t cp, next_cp;
220
221 cover_class = ALLOCNO_COVER_CLASS (allocno);
222 /* Probably 5 hops will be enough. */
223 if (divisor > (COST_HOP_DIVISOR * COST_HOP_DIVISOR
224 * COST_HOP_DIVISOR * COST_HOP_DIVISOR * COST_HOP_DIVISOR))
225 return;
226 if (cover_class == NO_REGS)
227 return;
228 /* Check that it was already visited. */
229 if (allocno_update_cost_check[ALLOCNO_NUM (allocno)] == update_cost_check)
230 return;
231 allocno_update_cost_check[ALLOCNO_NUM (allocno)] = update_cost_check;
232 mode = ALLOCNO_MODE (allocno);
233 class_size = ira_class_hard_regs_num[cover_class];
234 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
235 {
236 if (cp->first == allocno)
237 {
238 next_cp = cp->next_first_allocno_copy;
239 another_allocno = cp->second;
240 }
241 else if (cp->second == allocno)
242 {
243 next_cp = cp->next_second_allocno_copy;
244 another_allocno = cp->first;
245 }
246 else
247 gcc_unreachable ();
248 if (cover_class != ALLOCNO_COVER_CLASS (another_allocno)
249 || ALLOCNO_ASSIGNED_P (another_allocno)
250 || ALLOCNO_MAY_BE_SPILLED_P (another_allocno))
251 continue;
252 ira_allocate_and_copy_costs
253 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
254 cover_class, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
255 conflict_costs
256 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
257 if (conflict_costs == NULL)
258 cont_p = true;
259 else
260 {
261 ira_assert (ALLOCNO_FREQ (another_allocno) != 0);
262 mult = cp->freq;
263 div = ALLOCNO_FREQ (another_allocno) * divisor;
264 cont_p = false;
265 for (i = class_size - 1; i >= 0; i--)
266 {
267 cost = conflict_costs [i] * mult / div;
268 if (cost == 0)
269 continue;
270 cont_p = true;
271 if (decr_p)
272 cost = -cost;
273 costs[i] += cost;
274 }
275 }
276 if (cont_p)
277 update_conflict_hard_regno_costs (costs, another_allocno,
278 divisor * COST_HOP_DIVISOR, decr_p);
279 }
280 }
281
282 /* Sort allocnos according to the profit of usage of a hard register
283 instead of memory for them. */
284 static int
285 allocno_cost_compare_func (const void *v1p, const void *v2p)
286 {
287 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
288 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
289 int c1, c2;
290
291 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_COVER_CLASS_COST (p1);
292 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_COVER_CLASS_COST (p2);
293 if (c1 - c2)
294 return c1 - c2;
295
296 /* If regs are equally good, sort by allocno numbers, so that the
297 results of qsort leave nothing to chance. */
298 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
299 }
300
301 /* Print all allocnos coalesced with ALLOCNO. */
302 static void
303 print_coalesced_allocno (ira_allocno_t allocno)
304 {
305 ira_allocno_t a;
306
307 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
308 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
309 {
310 ira_print_expanded_allocno (a);
311 if (a == allocno)
312 break;
313 fprintf (ira_dump_file, "+");
314 }
315 }
316
317 /* Choose a hard register for ALLOCNO (or for all coalesced allocnos
318 represented by ALLOCNO). If RETRY_P is TRUE, it means that the
319 function called from function `ira_reassign_conflict_allocnos' and
320 `allocno_reload_assign'. This function implements the optimistic
321 coalescing too: if we failed to assign a hard register to set of
322 the coalesced allocnos, we put them onto the coloring stack for
323 subsequent separate assigning. */
324 static bool
325 assign_hard_reg (ira_allocno_t allocno, bool retry_p)
326 {
327 HARD_REG_SET conflicting_regs;
328 int i, j, hard_regno, best_hard_regno, class_size;
329 int cost, mem_cost, min_cost, full_cost, min_full_cost, add_cost;
330 int *a_costs;
331 int *conflict_costs;
332 enum reg_class cover_class, rclass;
333 enum machine_mode mode;
334 ira_allocno_t a, conflict_allocno;
335 ira_allocno_conflict_iterator aci;
336 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
337 #ifdef STACK_REGS
338 bool no_stack_reg_p;
339 #endif
340
341 ira_assert (! ALLOCNO_ASSIGNED_P (allocno));
342 cover_class = ALLOCNO_COVER_CLASS (allocno);
343 class_size = ira_class_hard_regs_num[cover_class];
344 mode = ALLOCNO_MODE (allocno);
345 CLEAR_HARD_REG_SET (conflicting_regs);
346 best_hard_regno = -1;
347 memset (full_costs, 0, sizeof (int) * class_size);
348 mem_cost = 0;
349 if (allocno_coalesced_p)
350 bitmap_clear (processed_coalesced_allocno_bitmap);
351 memset (costs, 0, sizeof (int) * class_size);
352 memset (full_costs, 0, sizeof (int) * class_size);
353 #ifdef STACK_REGS
354 no_stack_reg_p = false;
355 #endif
356 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
357 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
358 {
359 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
360 IOR_HARD_REG_SET (conflicting_regs,
361 ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
362 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
363 cover_class, ALLOCNO_HARD_REG_COSTS (a));
364 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
365 #ifdef STACK_REGS
366 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
367 #endif
368 for (cost = ALLOCNO_COVER_CLASS_COST (a), i = 0; i < class_size; i++)
369 if (a_costs != NULL)
370 {
371 costs[i] += a_costs[i];
372 full_costs[i] += a_costs[i];
373 }
374 else
375 {
376 costs[i] += cost;
377 full_costs[i] += cost;
378 }
379 /* Take preferences of conflicting allocnos into account. */
380 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
381 /* Reload can give another class so we need to check all
382 allocnos. */
383 if (retry_p || bitmap_bit_p (consideration_allocno_bitmap,
384 ALLOCNO_NUM (conflict_allocno)))
385 {
386 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_allocno));
387 if (allocno_coalesced_p)
388 {
389 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
390 ALLOCNO_NUM (conflict_allocno)))
391 continue;
392 bitmap_set_bit (processed_coalesced_allocno_bitmap,
393 ALLOCNO_NUM (conflict_allocno));
394 }
395 if (ALLOCNO_ASSIGNED_P (conflict_allocno))
396 {
397 if ((hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno)) >= 0)
398 {
399 IOR_HARD_REG_SET
400 (conflicting_regs,
401 ira_reg_mode_hard_regset
402 [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
403 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
404 conflicting_regs))
405 goto fail;
406 }
407 continue;
408 }
409 else if (! ALLOCNO_MAY_BE_SPILLED_P (conflict_allocno))
410 {
411 ira_allocate_and_copy_costs
412 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno),
413 cover_class,
414 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_allocno));
415 conflict_costs
416 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_allocno);
417 if (conflict_costs != NULL)
418 for (j = class_size - 1; j >= 0; j--)
419 full_costs[j] -= conflict_costs[j];
420 VEC_safe_push (ira_allocno_t, heap, conflict_allocno_vec,
421 conflict_allocno);
422 }
423 }
424 if (a == allocno)
425 break;
426 }
427 /* Take into account preferences of allocnos connected by copies to
428 the conflict allocnos. */
429 update_cost_check++;
430 while (VEC_length (ira_allocno_t, conflict_allocno_vec) != 0)
431 {
432 conflict_allocno = VEC_pop (ira_allocno_t, conflict_allocno_vec);
433 update_conflict_hard_regno_costs (full_costs, conflict_allocno,
434 COST_HOP_DIVISOR, true);
435 }
436 update_cost_check++;
437 /* Take preferences of allocnos connected by copies into
438 account. */
439 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
440 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
441 {
442 update_conflict_hard_regno_costs (full_costs, a,
443 COST_HOP_DIVISOR, false);
444 if (a == allocno)
445 break;
446 }
447 min_cost = min_full_cost = INT_MAX;
448 /* We don't care about giving callee saved registers to allocnos no
449 living through calls because call clobbered registers are
450 allocated first (it is usual practice to put them first in
451 REG_ALLOC_ORDER). */
452 for (i = 0; i < class_size; i++)
453 {
454 hard_regno = ira_class_hard_regs[cover_class][i];
455 #ifdef STACK_REGS
456 if (no_stack_reg_p
457 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
458 continue;
459 #endif
460 if (! ira_hard_reg_not_in_set_p (hard_regno, mode, conflicting_regs)
461 || TEST_HARD_REG_BIT (prohibited_class_mode_regs[cover_class][mode],
462 hard_regno))
463 continue;
464 cost = costs[i];
465 full_cost = full_costs[i];
466 if (! allocated_hardreg_p[hard_regno]
467 && ira_hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set))
468 /* We need to save/restore the hard register in
469 epilogue/prologue. Therefore we increase the cost. */
470 {
471 /* ??? If only part is call clobbered. */
472 rclass = REGNO_REG_CLASS (hard_regno);
473 add_cost = (ira_memory_move_cost[mode][rclass][0]
474 + ira_memory_move_cost[mode][rclass][1] - 1);
475 cost += add_cost;
476 full_cost += add_cost;
477 }
478 if (min_cost > cost)
479 min_cost = cost;
480 if (min_full_cost > full_cost)
481 {
482 min_full_cost = full_cost;
483 best_hard_regno = hard_regno;
484 ira_assert (hard_regno >= 0);
485 }
486 }
487 if (min_full_cost > mem_cost)
488 {
489 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
490 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
491 mem_cost, min_full_cost);
492 best_hard_regno = -1;
493 }
494 fail:
495 if (best_hard_regno < 0
496 && ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno) != allocno)
497 {
498 for (j = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
499 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
500 {
501 sorted_allocnos[j++] = a;
502 if (a == allocno)
503 break;
504 }
505 qsort (sorted_allocnos, j, sizeof (ira_allocno_t),
506 allocno_cost_compare_func);
507 for (i = 0; i < j; i++)
508 {
509 a = sorted_allocnos[i];
510 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
511 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
512 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, a);
513 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
514 {
515 fprintf (ira_dump_file, " Pushing");
516 print_coalesced_allocno (a);
517 fprintf (ira_dump_file, "\n");
518 }
519 }
520 return false;
521 }
522 if (best_hard_regno >= 0)
523 allocated_hardreg_p[best_hard_regno] = true;
524 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
525 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
526 {
527 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
528 ALLOCNO_ASSIGNED_P (a) = true;
529 if (best_hard_regno >= 0)
530 update_copy_costs (a, true);
531 ira_assert (ALLOCNO_COVER_CLASS (a) == cover_class);
532 /* We don't need updated costs anymore: */
533 ira_free_allocno_updated_costs (a);
534 if (a == allocno)
535 break;
536 }
537 return best_hard_regno >= 0;
538 }
539
540 \f
541
542 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
543
544 /* Bucket of allocnos that can colored currently without spilling. */
545 static ira_allocno_t colorable_allocno_bucket;
546
547 /* Bucket of allocnos that might be not colored currently without
548 spilling. */
549 static ira_allocno_t uncolorable_allocno_bucket;
550
551 /* Each element of the array contains the current number of allocnos
552 of given *cover* class in the uncolorable_bucket. */
553 static int uncolorable_allocnos_num[N_REG_CLASSES];
554
555 /* Add ALLOCNO to bucket *BUCKET_PTR. ALLOCNO should be not in a bucket
556 before the call. */
557 static void
558 add_ira_allocno_to_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
559 {
560 ira_allocno_t first_allocno;
561 enum reg_class cover_class;
562
563 if (bucket_ptr == &uncolorable_allocno_bucket
564 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
565 {
566 uncolorable_allocnos_num[cover_class]++;
567 ira_assert (uncolorable_allocnos_num[cover_class] > 0);
568 }
569 first_allocno = *bucket_ptr;
570 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = first_allocno;
571 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = NULL;
572 if (first_allocno != NULL)
573 ALLOCNO_PREV_BUCKET_ALLOCNO (first_allocno) = allocno;
574 *bucket_ptr = allocno;
575 }
576
577 /* The function returns frequency and number of available hard
578 registers for allocnos coalesced with ALLOCNO. */
579 static void
580 get_coalesced_allocnos_attributes (ira_allocno_t allocno, int *freq, int *num)
581 {
582 ira_allocno_t a;
583
584 *freq = 0;
585 *num = 0;
586 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
587 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
588 {
589 *freq += ALLOCNO_FREQ (a);
590 *num += ALLOCNO_AVAILABLE_REGS_NUM (a);
591 if (a == allocno)
592 break;
593 }
594 }
595
596 /* Compare two allocnos to define which allocno should be pushed first
597 into the coloring stack. If the return is a negative number, the
598 allocno given by the first parameter will be pushed first. In this
599 case such allocno has less priority than the second one and the
600 hard register will be assigned to it after assignment to the second
601 one. As the result of such assignment order, the second allocno
602 has a better chance to get the best hard register. */
603 static int
604 bucket_allocno_compare_func (const void *v1p, const void *v2p)
605 {
606 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
607 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
608 int diff, a1_freq, a2_freq, a1_num, a2_num;
609
610 if ((diff = (int) ALLOCNO_COVER_CLASS (a2) - ALLOCNO_COVER_CLASS (a1)) != 0)
611 return diff;
612 get_coalesced_allocnos_attributes (a1, &a1_freq, &a1_num);
613 get_coalesced_allocnos_attributes (a2, &a2_freq, &a2_num);
614 if ((diff = a2_num - a1_num) != 0)
615 return diff;
616 else if ((diff = a1_freq - a2_freq) != 0)
617 return diff;
618 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
619 }
620
621 /* Sort bucket *BUCKET_PTR and return the result through
622 BUCKET_PTR. */
623 static void
624 sort_bucket (ira_allocno_t *bucket_ptr)
625 {
626 ira_allocno_t a, head;
627 int n;
628
629 for (n = 0, a = *bucket_ptr; a != NULL; a = ALLOCNO_NEXT_BUCKET_ALLOCNO (a))
630 sorted_allocnos[n++] = a;
631 if (n <= 1)
632 return;
633 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
634 bucket_allocno_compare_func);
635 head = NULL;
636 for (n--; n >= 0; n--)
637 {
638 a = sorted_allocnos[n];
639 ALLOCNO_NEXT_BUCKET_ALLOCNO (a) = head;
640 ALLOCNO_PREV_BUCKET_ALLOCNO (a) = NULL;
641 if (head != NULL)
642 ALLOCNO_PREV_BUCKET_ALLOCNO (head) = a;
643 head = a;
644 }
645 *bucket_ptr = head;
646 }
647
648 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
649 their priority. ALLOCNO should be not in a bucket before the
650 call. */
651 static void
652 add_ira_allocno_to_ordered_bucket (ira_allocno_t allocno,
653 ira_allocno_t *bucket_ptr)
654 {
655 ira_allocno_t before, after;
656 enum reg_class cover_class;
657
658 if (bucket_ptr == &uncolorable_allocno_bucket
659 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
660 {
661 uncolorable_allocnos_num[cover_class]++;
662 ira_assert (uncolorable_allocnos_num[cover_class] > 0);
663 }
664 for (before = *bucket_ptr, after = NULL;
665 before != NULL;
666 after = before, before = ALLOCNO_NEXT_BUCKET_ALLOCNO (before))
667 if (bucket_allocno_compare_func (&allocno, &before) < 0)
668 break;
669 ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno) = before;
670 ALLOCNO_PREV_BUCKET_ALLOCNO (allocno) = after;
671 if (after == NULL)
672 *bucket_ptr = allocno;
673 else
674 ALLOCNO_NEXT_BUCKET_ALLOCNO (after) = allocno;
675 if (before != NULL)
676 ALLOCNO_PREV_BUCKET_ALLOCNO (before) = allocno;
677 }
678
679 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
680 the call. */
681 static void
682 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
683 {
684 ira_allocno_t prev_allocno, next_allocno;
685 enum reg_class cover_class;
686
687 if (bucket_ptr == &uncolorable_allocno_bucket
688 && (cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
689 {
690 uncolorable_allocnos_num[cover_class]--;
691 ira_assert (uncolorable_allocnos_num[cover_class] >= 0);
692 }
693 prev_allocno = ALLOCNO_PREV_BUCKET_ALLOCNO (allocno);
694 next_allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno);
695 if (prev_allocno != NULL)
696 ALLOCNO_NEXT_BUCKET_ALLOCNO (prev_allocno) = next_allocno;
697 else
698 {
699 ira_assert (*bucket_ptr == allocno);
700 *bucket_ptr = next_allocno;
701 }
702 if (next_allocno != NULL)
703 ALLOCNO_PREV_BUCKET_ALLOCNO (next_allocno) = prev_allocno;
704 }
705
706 /* Splay tree for each cover class. The trees are indexed by the
707 corresponding cover classes. Splay trees contain uncolorable
708 allocnos. */
709 static splay_tree uncolorable_allocnos_splay_tree[N_REG_CLASSES];
710
711 /* If the following macro is TRUE, splay tree is used to choose an
712 allocno of the corresponding cover class for spilling. When the
713 number uncolorable allocnos of given cover class decreases to some
714 threshold, linear array search is used to find the best allocno for
715 spilling. This threshold is actually pretty big because, although
716 splay trees asymptotically is much faster, each splay tree
717 operation is sufficiently costly especially taking cache locality
718 into account. */
719 #define USE_SPLAY_P(CLASS) (uncolorable_allocnos_num[CLASS] > 4000)
720
721 /* Put ALLOCNO onto the coloring stack without removing it from its
722 bucket. Pushing allocno to the coloring stack can result in moving
723 conflicting allocnos from the uncolorable bucket to the colorable
724 one. */
725 static void
726 push_ira_allocno_to_stack (ira_allocno_t allocno)
727 {
728 int conflicts_num, conflict_size, size;
729 ira_allocno_t a, conflict_allocno;
730 enum reg_class cover_class;
731 ira_allocno_conflict_iterator aci;
732
733 ALLOCNO_IN_GRAPH_P (allocno) = false;
734 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, allocno);
735 cover_class = ALLOCNO_COVER_CLASS (allocno);
736 if (cover_class == NO_REGS)
737 return;
738 size = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)];
739 if (allocno_coalesced_p)
740 bitmap_clear (processed_coalesced_allocno_bitmap);
741 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
742 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
743 {
744 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
745 if (bitmap_bit_p (coloring_allocno_bitmap,
746 ALLOCNO_NUM (conflict_allocno)))
747 {
748 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_allocno));
749 if (allocno_coalesced_p)
750 {
751 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
752 ALLOCNO_NUM (conflict_allocno)))
753 continue;
754 bitmap_set_bit (processed_coalesced_allocno_bitmap,
755 ALLOCNO_NUM (conflict_allocno));
756 }
757 if (ALLOCNO_IN_GRAPH_P (conflict_allocno)
758 && ! ALLOCNO_ASSIGNED_P (conflict_allocno))
759 {
760 conflicts_num = ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno);
761 conflict_size
762 = (ira_reg_class_nregs
763 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
764 ira_assert
765 (ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) >= size);
766 if (conflicts_num + conflict_size
767 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
768 {
769 ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) -= size;
770 continue;
771 }
772 conflicts_num
773 = ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) - size;
774 if (uncolorable_allocnos_splay_tree[cover_class] != NULL
775 && !ALLOCNO_SPLAY_REMOVED_P (conflict_allocno)
776 && USE_SPLAY_P (cover_class))
777 {
778 ira_assert
779 (splay_tree_lookup
780 (uncolorable_allocnos_splay_tree[cover_class],
781 (splay_tree_key) conflict_allocno) != NULL);
782 splay_tree_remove
783 (uncolorable_allocnos_splay_tree[cover_class],
784 (splay_tree_key) conflict_allocno);
785 ALLOCNO_SPLAY_REMOVED_P (conflict_allocno) = true;
786 VEC_safe_push (ira_allocno_t, heap,
787 removed_splay_allocno_vec,
788 conflict_allocno);
789 }
790 ALLOCNO_LEFT_CONFLICTS_NUM (conflict_allocno) = conflicts_num;
791 if (conflicts_num + conflict_size
792 <= ALLOCNO_AVAILABLE_REGS_NUM (conflict_allocno))
793 {
794 delete_allocno_from_bucket (conflict_allocno,
795 &uncolorable_allocno_bucket);
796 add_ira_allocno_to_ordered_bucket (conflict_allocno,
797 &colorable_allocno_bucket);
798 }
799 }
800 }
801 if (a == allocno)
802 break;
803 }
804 }
805
806 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
807 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
808 static void
809 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
810 {
811 enum reg_class cover_class;
812
813 if (colorable_p)
814 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
815 else
816 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
817 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
818 {
819 fprintf (ira_dump_file, " Pushing");
820 print_coalesced_allocno (allocno);
821 fprintf (ira_dump_file, "%s\n", colorable_p ? "" : "(potential spill)");
822 }
823 cover_class = ALLOCNO_COVER_CLASS (allocno);
824 ira_assert ((colorable_p
825 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
826 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
827 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno)))
828 || (! colorable_p
829 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
830 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE
831 (allocno)]
832 > ALLOCNO_AVAILABLE_REGS_NUM (allocno))));
833 if (! colorable_p)
834 ALLOCNO_MAY_BE_SPILLED_P (allocno) = true;
835 push_ira_allocno_to_stack (allocno);
836 }
837
838 /* Put all allocnos from colorable bucket onto the coloring stack. */
839 static void
840 push_only_colorable (void)
841 {
842 sort_bucket (&colorable_allocno_bucket);
843 for (;colorable_allocno_bucket != NULL;)
844 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
845 }
846
847 /* Puts ALLOCNO chosen for potential spilling onto the coloring
848 stack. */
849 static void
850 push_ira_allocno_to_spill (ira_allocno_t allocno)
851 {
852 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
853 ALLOCNO_MAY_BE_SPILLED_P (allocno) = true;
854 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
855 fprintf (ira_dump_file, " Pushing p%d(%d) (potential spill)\n",
856 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
857 push_ira_allocno_to_stack (allocno);
858 }
859
860 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
861 loop given by its LOOP_NODE. */
862 int
863 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
864 {
865 int freq, i;
866 edge_iterator ei;
867 edge e;
868 VEC (edge, heap) *edges;
869
870 ira_assert (loop_node->loop != NULL
871 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
872 freq = 0;
873 if (! exit_p)
874 {
875 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
876 if (e->src != loop_node->loop->latch
877 && (regno < 0
878 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
879 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
880 freq += EDGE_FREQUENCY (e);
881 }
882 else
883 {
884 edges = get_loop_exit_edges (loop_node->loop);
885 for (i = 0; VEC_iterate (edge, edges, i, e); i++)
886 if (regno < 0
887 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
888 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
889 freq += EDGE_FREQUENCY (e);
890 VEC_free (edge, heap, edges);
891 }
892
893 return REG_FREQ_FROM_EDGE_FREQ (freq);
894 }
895
896 /* Calculate and return the cost of putting allocno A into memory. */
897 static int
898 calculate_allocno_spill_cost (ira_allocno_t a)
899 {
900 int regno, cost;
901 enum machine_mode mode;
902 enum reg_class rclass;
903 ira_allocno_t parent_allocno;
904 ira_loop_tree_node_t parent_node, loop_node;
905
906 regno = ALLOCNO_REGNO (a);
907 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a);
908 if (ALLOCNO_CAP (a) != NULL)
909 return cost;
910 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
911 if ((parent_node = loop_node->parent) == NULL)
912 return cost;
913 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
914 return cost;
915 mode = ALLOCNO_MODE (a);
916 rclass = ALLOCNO_COVER_CLASS (a);
917 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
918 cost -= (ira_memory_move_cost[mode][rclass][0]
919 * ira_loop_edge_freq (loop_node, regno, true)
920 + ira_memory_move_cost[mode][rclass][1]
921 * ira_loop_edge_freq (loop_node, regno, false));
922 else
923 cost += ((ira_memory_move_cost[mode][rclass][1]
924 * ira_loop_edge_freq (loop_node, regno, true)
925 + ira_memory_move_cost[mode][rclass][0]
926 * ira_loop_edge_freq (loop_node, regno, false))
927 - (ira_register_move_cost[mode][rclass][rclass]
928 * (ira_loop_edge_freq (loop_node, regno, false)
929 + ira_loop_edge_freq (loop_node, regno, true))));
930 return cost;
931 }
932
933 /* Compare keys in the splay tree used to choose best allocno for
934 spilling. The best allocno has the minimal key. */
935 static int
936 allocno_spill_priority_compare (splay_tree_key k1, splay_tree_key k2)
937 {
938 int pri1, pri2, diff;
939 ira_allocno_t a1 = (ira_allocno_t) k1, a2 = (ira_allocno_t) k2;
940
941 pri1 = (IRA_ALLOCNO_TEMP (a1)
942 / (ALLOCNO_LEFT_CONFLICTS_NUM (a1)
943 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a1)][ALLOCNO_MODE (a1)]
944 + 1));
945 pri2 = (IRA_ALLOCNO_TEMP (a2)
946 / (ALLOCNO_LEFT_CONFLICTS_NUM (a2)
947 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a2)][ALLOCNO_MODE (a2)]
948 + 1));
949 if ((diff = pri1 - pri2) != 0)
950 return diff;
951 if ((diff = IRA_ALLOCNO_TEMP (a1) - IRA_ALLOCNO_TEMP (a2)) != 0)
952 return diff;
953 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
954 }
955
956 /* Allocate data of SIZE for the splay trees. We allocate only spay
957 tree roots or splay tree nodes. If you change this, please rewrite
958 the function. */
959 static void *
960 splay_tree_allocate (int size, void *data ATTRIBUTE_UNUSED)
961 {
962 if (size != sizeof (struct splay_tree_node_s))
963 return ira_allocate (size);
964 return pool_alloc (splay_tree_node_pool);
965 }
966
967 /* Free data NODE for the splay trees. We allocate and free only spay
968 tree roots or splay tree nodes. If you change this, please rewrite
969 the function. */
970 static void
971 splay_tree_free (void *node, void *data ATTRIBUTE_UNUSED)
972 {
973 int i;
974 enum reg_class cover_class;
975
976 for (i = 0; i < ira_reg_class_cover_size; i++)
977 {
978 cover_class = ira_reg_class_cover[i];
979 if (node == uncolorable_allocnos_splay_tree[cover_class])
980 {
981 ira_free (node);
982 return;
983 }
984 }
985 pool_free (splay_tree_node_pool, node);
986 }
987
988 /* Push allocnos to the coloring stack. The order of allocnos in the
989 stack defines the order for the subsequent coloring. */
990 static void
991 push_allocnos_to_stack (void)
992 {
993 ira_allocno_t allocno, a, i_allocno, *allocno_vec;
994 enum reg_class cover_class, rclass;
995 int allocno_pri, i_allocno_pri, allocno_cost, i_allocno_cost;
996 int i, j, num, cover_class_allocnos_num[N_REG_CLASSES];
997 ira_allocno_t *cover_class_allocnos[N_REG_CLASSES];
998 int cost;
999
1000 /* Initialize. */
1001 VEC_truncate(ira_allocno_t, removed_splay_allocno_vec, 0);
1002 for (i = 0; i < ira_reg_class_cover_size; i++)
1003 {
1004 cover_class = ira_reg_class_cover[i];
1005 cover_class_allocnos_num[cover_class] = 0;
1006 cover_class_allocnos[cover_class] = NULL;
1007 uncolorable_allocnos_splay_tree[cover_class] = NULL;
1008 }
1009 /* Calculate uncolorable allocno spill costs. */
1010 for (allocno = uncolorable_allocno_bucket;
1011 allocno != NULL;
1012 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
1013 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
1014 {
1015 cover_class_allocnos_num[cover_class]++;
1016 cost = 0;
1017 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1018 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1019 {
1020 cost += calculate_allocno_spill_cost (a);
1021 if (a == allocno)
1022 break;
1023 }
1024 /* ??? Remove cost of copies between the coalesced
1025 allocnos. */
1026 IRA_ALLOCNO_TEMP (allocno) = cost;
1027 }
1028 /* Define place where to put uncolorable allocnos of the same cover
1029 class. */
1030 for (num = i = 0; i < ira_reg_class_cover_size; i++)
1031 {
1032 cover_class = ira_reg_class_cover[i];
1033 ira_assert (cover_class_allocnos_num[cover_class]
1034 == uncolorable_allocnos_num[cover_class]);
1035 if (cover_class_allocnos_num[cover_class] != 0)
1036 {
1037 cover_class_allocnos[cover_class] = allocnos_for_spilling + num;
1038 num += cover_class_allocnos_num[cover_class];
1039 cover_class_allocnos_num[cover_class] = 0;
1040 }
1041 if (USE_SPLAY_P (cover_class))
1042 uncolorable_allocnos_splay_tree[cover_class]
1043 = splay_tree_new_with_allocator (allocno_spill_priority_compare,
1044 NULL, NULL, splay_tree_allocate,
1045 splay_tree_free, NULL);
1046 }
1047 ira_assert (num <= ira_allocnos_num);
1048 /* Collect uncolorable allocnos of each cover class. */
1049 for (allocno = uncolorable_allocno_bucket;
1050 allocno != NULL;
1051 allocno = ALLOCNO_NEXT_BUCKET_ALLOCNO (allocno))
1052 if ((cover_class = ALLOCNO_COVER_CLASS (allocno)) != NO_REGS)
1053 {
1054 cover_class_allocnos
1055 [cover_class][cover_class_allocnos_num[cover_class]++] = allocno;
1056 if (uncolorable_allocnos_splay_tree[cover_class] != NULL)
1057 splay_tree_insert (uncolorable_allocnos_splay_tree[cover_class],
1058 (splay_tree_key) allocno,
1059 (splay_tree_value) allocno);
1060 }
1061 for (;;)
1062 {
1063 push_only_colorable ();
1064 allocno = uncolorable_allocno_bucket;
1065 if (allocno == NULL)
1066 break;
1067 cover_class = ALLOCNO_COVER_CLASS (allocno);
1068 if (cover_class == NO_REGS)
1069 {
1070 push_ira_allocno_to_spill (allocno);
1071 continue;
1072 }
1073 /* Potential spilling. */
1074 ira_assert
1075 (ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)] > 0);
1076 if (USE_SPLAY_P (cover_class))
1077 {
1078 for (;VEC_length (ira_allocno_t, removed_splay_allocno_vec) != 0;)
1079 {
1080 allocno = VEC_pop (ira_allocno_t, removed_splay_allocno_vec);
1081 ALLOCNO_SPLAY_REMOVED_P (allocno) = false;
1082 rclass = ALLOCNO_COVER_CLASS (allocno);
1083 if (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
1084 + ira_reg_class_nregs [rclass][ALLOCNO_MODE (allocno)]
1085 > ALLOCNO_AVAILABLE_REGS_NUM (allocno))
1086 splay_tree_insert
1087 (uncolorable_allocnos_splay_tree[rclass],
1088 (splay_tree_key) allocno, (splay_tree_value) allocno);
1089 }
1090 allocno = ((ira_allocno_t)
1091 splay_tree_min
1092 (uncolorable_allocnos_splay_tree[cover_class])->key);
1093 splay_tree_remove (uncolorable_allocnos_splay_tree[cover_class],
1094 (splay_tree_key) allocno);
1095 }
1096 else
1097 {
1098 num = cover_class_allocnos_num[cover_class];
1099 ira_assert (num > 0);
1100 allocno_vec = cover_class_allocnos[cover_class];
1101 allocno = NULL;
1102 allocno_pri = allocno_cost = 0;
1103 /* Sort uncolorable allocno to find the one with the lowest
1104 spill cost. */
1105 for (i = 0, j = num - 1; i <= j;)
1106 {
1107 i_allocno = allocno_vec[i];
1108 if (! ALLOCNO_IN_GRAPH_P (i_allocno)
1109 && ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
1110 {
1111 i_allocno = allocno_vec[j];
1112 allocno_vec[j] = allocno_vec[i];
1113 allocno_vec[i] = i_allocno;
1114 }
1115 if (ALLOCNO_IN_GRAPH_P (i_allocno))
1116 {
1117 i++;
1118 if (IRA_ALLOCNO_TEMP (i_allocno) == INT_MAX)
1119 {
1120 ira_allocno_t a;
1121 int cost = 0;
1122
1123 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (i_allocno);;
1124 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1125 {
1126 cost += calculate_allocno_spill_cost (i_allocno);
1127 if (a == i_allocno)
1128 break;
1129 }
1130 /* ??? Remove cost of copies between the coalesced
1131 allocnos. */
1132 IRA_ALLOCNO_TEMP (i_allocno) = cost;
1133 }
1134 i_allocno_cost = IRA_ALLOCNO_TEMP (i_allocno);
1135 i_allocno_pri
1136 = (i_allocno_cost
1137 / (ALLOCNO_LEFT_CONFLICTS_NUM (i_allocno)
1138 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS
1139 (i_allocno)]
1140 [ALLOCNO_MODE (i_allocno)] + 1));
1141 if (allocno == NULL || allocno_pri > i_allocno_pri
1142 || (allocno_pri == i_allocno_pri
1143 && (allocno_cost > i_allocno_cost
1144 || (allocno_cost == i_allocno_cost
1145 && (ALLOCNO_NUM (allocno)
1146 > ALLOCNO_NUM (i_allocno))))))
1147 {
1148 allocno = i_allocno;
1149 allocno_cost = i_allocno_cost;
1150 allocno_pri = i_allocno_pri;
1151 }
1152 }
1153 if (! ALLOCNO_IN_GRAPH_P (allocno_vec[j]))
1154 j--;
1155 }
1156 ira_assert (allocno != NULL && j >= 0);
1157 cover_class_allocnos_num[cover_class] = j + 1;
1158 }
1159 ira_assert (ALLOCNO_IN_GRAPH_P (allocno)
1160 && ALLOCNO_COVER_CLASS (allocno) == cover_class
1161 && (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
1162 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE
1163 (allocno)]
1164 > ALLOCNO_AVAILABLE_REGS_NUM (allocno)));
1165 remove_allocno_from_bucket_and_push (allocno, false);
1166 }
1167 ira_assert (colorable_allocno_bucket == NULL
1168 && uncolorable_allocno_bucket == NULL);
1169 for (i = 0; i < ira_reg_class_cover_size; i++)
1170 {
1171 cover_class = ira_reg_class_cover[i];
1172 ira_assert (uncolorable_allocnos_num[cover_class] == 0);
1173 if (uncolorable_allocnos_splay_tree[cover_class] != NULL)
1174 splay_tree_delete (uncolorable_allocnos_splay_tree[cover_class]);
1175 }
1176 }
1177
1178 /* Pop the coloring stack and assign hard registers to the popped
1179 allocnos. */
1180 static void
1181 pop_allocnos_from_stack (void)
1182 {
1183 ira_allocno_t allocno;
1184 enum reg_class cover_class;
1185
1186 for (;VEC_length (ira_allocno_t, allocno_stack_vec) != 0;)
1187 {
1188 allocno = VEC_pop (ira_allocno_t, allocno_stack_vec);
1189 cover_class = ALLOCNO_COVER_CLASS (allocno);
1190 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1191 {
1192 fprintf (ira_dump_file, " Popping");
1193 print_coalesced_allocno (allocno);
1194 fprintf (ira_dump_file, " -- ");
1195 }
1196 if (cover_class == NO_REGS)
1197 {
1198 ALLOCNO_HARD_REGNO (allocno) = -1;
1199 ALLOCNO_ASSIGNED_P (allocno) = true;
1200 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
1201 ira_assert
1202 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
1203 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1204 fprintf (ira_dump_file, "assign memory\n");
1205 }
1206 else if (assign_hard_reg (allocno, false))
1207 {
1208 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1209 fprintf (ira_dump_file, "assign reg %d\n",
1210 ALLOCNO_HARD_REGNO (allocno));
1211 }
1212 else if (ALLOCNO_ASSIGNED_P (allocno))
1213 {
1214 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1215 fprintf (ira_dump_file, "spill\n");
1216 }
1217 ALLOCNO_IN_GRAPH_P (allocno) = true;
1218 }
1219 }
1220
1221 /* Set up number of available hard registers for ALLOCNO. */
1222 static void
1223 setup_allocno_available_regs_num (ira_allocno_t allocno)
1224 {
1225 int i, n, hard_regs_num;
1226 enum reg_class cover_class;
1227 ira_allocno_t a;
1228 HARD_REG_SET temp_set;
1229
1230 cover_class = ALLOCNO_COVER_CLASS (allocno);
1231 ALLOCNO_AVAILABLE_REGS_NUM (allocno) = ira_available_class_regs[cover_class];
1232 if (cover_class == NO_REGS)
1233 return;
1234 CLEAR_HARD_REG_SET (temp_set);
1235 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1236 hard_regs_num = ira_class_hard_regs_num[cover_class];
1237 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1238 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1239 {
1240 IOR_HARD_REG_SET (temp_set, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
1241 if (a == allocno)
1242 break;
1243 }
1244 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
1245 if (TEST_HARD_REG_BIT (temp_set, ira_class_hard_regs[cover_class][i]))
1246 n++;
1247 if (internal_flag_ira_verbose > 2 && n > 0 && ira_dump_file != NULL)
1248 fprintf (ira_dump_file, " Reg %d of %s has %d regs less\n",
1249 ALLOCNO_REGNO (allocno), reg_class_names[cover_class], n);
1250 ALLOCNO_AVAILABLE_REGS_NUM (allocno) -= n;
1251 }
1252
1253 /* Set up ALLOCNO_LEFT_CONFLICTS_NUM for ALLOCNO. */
1254 static void
1255 setup_allocno_left_conflicts_num (ira_allocno_t allocno)
1256 {
1257 int i, hard_regs_num, hard_regno, conflict_allocnos_size;
1258 ira_allocno_t a, conflict_allocno;
1259 enum reg_class cover_class;
1260 HARD_REG_SET temp_set;
1261 ira_allocno_conflict_iterator aci;
1262
1263 cover_class = ALLOCNO_COVER_CLASS (allocno);
1264 hard_regs_num = ira_class_hard_regs_num[cover_class];
1265 CLEAR_HARD_REG_SET (temp_set);
1266 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) == allocno);
1267 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1268 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1269 {
1270 IOR_HARD_REG_SET (temp_set, ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
1271 if (a == allocno)
1272 break;
1273 }
1274 AND_HARD_REG_SET (temp_set, reg_class_contents[cover_class]);
1275 AND_COMPL_HARD_REG_SET (temp_set, ira_no_alloc_regs);
1276 conflict_allocnos_size = 0;
1277 if (! hard_reg_set_equal_p (temp_set, ira_zero_hard_reg_set))
1278 for (i = 0; i < (int) hard_regs_num; i++)
1279 {
1280 hard_regno = ira_class_hard_regs[cover_class][i];
1281 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
1282 {
1283 conflict_allocnos_size++;
1284 CLEAR_HARD_REG_BIT (temp_set, hard_regno);
1285 if (hard_reg_set_equal_p (temp_set, ira_zero_hard_reg_set))
1286 break;
1287 }
1288 }
1289 CLEAR_HARD_REG_SET (temp_set);
1290 if (allocno_coalesced_p)
1291 bitmap_clear (processed_coalesced_allocno_bitmap);
1292 if (cover_class != NO_REGS)
1293 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
1294 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1295 {
1296 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
1297 if (bitmap_bit_p (consideration_allocno_bitmap,
1298 ALLOCNO_NUM (conflict_allocno)))
1299 {
1300 ira_assert (cover_class
1301 == ALLOCNO_COVER_CLASS (conflict_allocno));
1302 if (allocno_coalesced_p)
1303 {
1304 if (bitmap_bit_p (processed_coalesced_allocno_bitmap,
1305 ALLOCNO_NUM (conflict_allocno)))
1306 continue;
1307 bitmap_set_bit (processed_coalesced_allocno_bitmap,
1308 ALLOCNO_NUM (conflict_allocno));
1309 }
1310 if (! ALLOCNO_ASSIGNED_P (conflict_allocno))
1311 conflict_allocnos_size
1312 += (ira_reg_class_nregs
1313 [cover_class][ALLOCNO_MODE (conflict_allocno)]);
1314 else if ((hard_regno = ALLOCNO_HARD_REGNO (conflict_allocno))
1315 >= 0)
1316 {
1317 int last = (hard_regno
1318 + hard_regno_nregs
1319 [hard_regno][ALLOCNO_MODE (conflict_allocno)]);
1320
1321 while (hard_regno < last)
1322 {
1323 if (! TEST_HARD_REG_BIT (temp_set, hard_regno))
1324 {
1325 conflict_allocnos_size++;
1326 SET_HARD_REG_BIT (temp_set, hard_regno);
1327 }
1328 hard_regno++;
1329 }
1330 }
1331 }
1332 if (a == allocno)
1333 break;
1334 }
1335 ALLOCNO_LEFT_CONFLICTS_NUM (allocno) = conflict_allocnos_size;
1336 }
1337
1338 /* Put ALLOCNO in a bucket corresponding to its number and size of its
1339 conflicting allocnos and hard registers. */
1340 static void
1341 put_allocno_into_bucket (ira_allocno_t allocno)
1342 {
1343 int hard_regs_num;
1344 enum reg_class cover_class;
1345
1346 cover_class = ALLOCNO_COVER_CLASS (allocno);
1347 hard_regs_num = ira_class_hard_regs_num[cover_class];
1348 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
1349 return;
1350 ALLOCNO_IN_GRAPH_P (allocno) = true;
1351 setup_allocno_left_conflicts_num (allocno);
1352 setup_allocno_available_regs_num (allocno);
1353 if (ALLOCNO_LEFT_CONFLICTS_NUM (allocno)
1354 + ira_reg_class_nregs[cover_class][ALLOCNO_MODE (allocno)]
1355 <= ALLOCNO_AVAILABLE_REGS_NUM (allocno))
1356 add_ira_allocno_to_bucket (allocno, &colorable_allocno_bucket);
1357 else
1358 add_ira_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
1359 }
1360
1361 /* The function is used to sort allocnos according to their execution
1362 frequencies. */
1363 static int
1364 copy_freq_compare_func (const void *v1p, const void *v2p)
1365 {
1366 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
1367 int pri1, pri2;
1368
1369 pri1 = cp1->freq;
1370 pri2 = cp2->freq;
1371 if (pri2 - pri1)
1372 return pri2 - pri1;
1373
1374 /* If freqencies are equal, sort by copies, so that the results of
1375 qsort leave nothing to chance. */
1376 return cp1->num - cp2->num;
1377 }
1378
1379 /* Merge two sets of coalesced allocnos given correspondingly by
1380 allocnos A1 and A2 (more accurately merging A2 set into A1
1381 set). */
1382 static void
1383 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
1384 {
1385 ira_allocno_t a, first, last, next;
1386
1387 first = ALLOCNO_FIRST_COALESCED_ALLOCNO (a1);
1388 if (first == ALLOCNO_FIRST_COALESCED_ALLOCNO (a2))
1389 return;
1390 for (last = a2, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1391 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1392 {
1393 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = first;
1394 if (a == a2)
1395 break;
1396 last = a;
1397 }
1398 next = ALLOCNO_NEXT_COALESCED_ALLOCNO (first);
1399 ALLOCNO_NEXT_COALESCED_ALLOCNO (first) = a2;
1400 ALLOCNO_NEXT_COALESCED_ALLOCNO (last) = next;
1401 }
1402
1403 /* Return TRUE if there are conflicting allocnos from two sets of
1404 coalesced allocnos given correspondingly by allocnos A1 and A2. If
1405 RELOAD_P is TRUE, we use live ranges to find conflicts because
1406 conflicts are represented only for allocnos of the same cover class
1407 and during the reload pass we coalesce allocnos for sharing stack
1408 memory slots. */
1409 static bool
1410 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2,
1411 bool reload_p)
1412 {
1413 ira_allocno_t a, conflict_allocno;
1414 ira_allocno_conflict_iterator aci;
1415
1416 if (allocno_coalesced_p)
1417 {
1418 bitmap_clear (processed_coalesced_allocno_bitmap);
1419 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1420 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1421 {
1422 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
1423 if (a == a1)
1424 break;
1425 }
1426 }
1427 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a2);;
1428 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
1429 {
1430 if (reload_p)
1431 {
1432 for (conflict_allocno = ALLOCNO_NEXT_COALESCED_ALLOCNO (a1);;
1433 conflict_allocno
1434 = ALLOCNO_NEXT_COALESCED_ALLOCNO (conflict_allocno))
1435 {
1436 if (ira_allocno_live_ranges_intersect_p (a, conflict_allocno))
1437 return true;
1438 if (conflict_allocno == a1)
1439 break;
1440 }
1441 }
1442 else
1443 {
1444 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
1445 if (conflict_allocno == a1
1446 || (allocno_coalesced_p
1447 && bitmap_bit_p (processed_coalesced_allocno_bitmap,
1448 ALLOCNO_NUM (conflict_allocno))))
1449 return true;
1450 }
1451 if (a == a2)
1452 break;
1453 }
1454 return false;
1455 }
1456
1457 /* The major function for aggressive allocno coalescing. For the
1458 reload pass (RELOAD_P) we coalesce only spilled allocnos. If some
1459 allocnos have been coalesced, we set up flag
1460 allocno_coalesced_p. */
1461 static void
1462 coalesce_allocnos (bool reload_p)
1463 {
1464 ira_allocno_t a;
1465 ira_copy_t cp, next_cp, *sorted_copies;
1466 enum reg_class cover_class;
1467 enum machine_mode mode;
1468 unsigned int j;
1469 int i, n, cp_num, regno;
1470 bitmap_iterator bi;
1471
1472 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
1473 * sizeof (ira_copy_t));
1474 cp_num = 0;
1475 /* Collect copies. */
1476 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
1477 {
1478 a = ira_allocnos[j];
1479 regno = ALLOCNO_REGNO (a);
1480 if ((! reload_p && ALLOCNO_ASSIGNED_P (a))
1481 || (reload_p
1482 && (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
1483 || (regno < ira_reg_equiv_len
1484 && (ira_reg_equiv_const[regno] != NULL_RTX
1485 || ira_reg_equiv_invariant_p[regno])))))
1486 continue;
1487 cover_class = ALLOCNO_COVER_CLASS (a);
1488 mode = ALLOCNO_MODE (a);
1489 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1490 {
1491 if (cp->first == a)
1492 {
1493 next_cp = cp->next_first_allocno_copy;
1494 regno = ALLOCNO_REGNO (cp->second);
1495 if ((reload_p
1496 || (ALLOCNO_COVER_CLASS (cp->second) == cover_class
1497 && ALLOCNO_MODE (cp->second) == mode))
1498 && cp->insn != NULL
1499 && ((! reload_p && ! ALLOCNO_ASSIGNED_P (cp->second))
1500 || (reload_p
1501 && ALLOCNO_ASSIGNED_P (cp->second)
1502 && ALLOCNO_HARD_REGNO (cp->second) < 0
1503 && (regno >= ira_reg_equiv_len
1504 || (! ira_reg_equiv_invariant_p[regno]
1505 && ira_reg_equiv_const[regno] == NULL_RTX)))))
1506 sorted_copies[cp_num++] = cp;
1507 }
1508 else if (cp->second == a)
1509 next_cp = cp->next_second_allocno_copy;
1510 else
1511 gcc_unreachable ();
1512 }
1513 }
1514 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
1515 /* Coalesced copies, most frequently executed first. */
1516 for (; cp_num != 0;)
1517 {
1518 for (i = 0; i < cp_num; i++)
1519 {
1520 cp = sorted_copies[i];
1521 if (! coalesced_allocno_conflict_p (cp->first, cp->second, reload_p))
1522 {
1523 allocno_coalesced_p = true;
1524 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1525 fprintf
1526 (ira_dump_file,
1527 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
1528 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1529 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
1530 cp->freq);
1531 merge_allocnos (cp->first, cp->second);
1532 i++;
1533 break;
1534 }
1535 }
1536 /* Collect the rest of copies. */
1537 for (n = 0; i < cp_num; i++)
1538 {
1539 cp = sorted_copies[i];
1540 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->first)
1541 != ALLOCNO_FIRST_COALESCED_ALLOCNO (cp->second))
1542 sorted_copies[n++] = cp;
1543 }
1544 cp_num = n;
1545 }
1546 ira_free (sorted_copies);
1547 }
1548
1549 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
1550 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
1551 static void
1552 color_allocnos (void)
1553 {
1554 unsigned int i;
1555 bitmap_iterator bi;
1556 ira_allocno_t a;
1557
1558 allocno_coalesced_p = false;
1559 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
1560 if (flag_ira_coalesce)
1561 coalesce_allocnos (false);
1562 /* Put the allocnos into the corresponding buckets. */
1563 colorable_allocno_bucket = NULL;
1564 uncolorable_allocno_bucket = NULL;
1565 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1566 {
1567 a = ira_allocnos[i];
1568 if (ALLOCNO_COVER_CLASS (a) == NO_REGS)
1569 {
1570 ALLOCNO_HARD_REGNO (a) = -1;
1571 ALLOCNO_ASSIGNED_P (a) = true;
1572 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
1573 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
1574 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1575 {
1576 fprintf (ira_dump_file, " Spill");
1577 print_coalesced_allocno (a);
1578 fprintf (ira_dump_file, "\n");
1579 }
1580 continue;
1581 }
1582 put_allocno_into_bucket (a);
1583 }
1584 push_allocnos_to_stack ();
1585 pop_allocnos_from_stack ();
1586 if (flag_ira_coalesce)
1587 /* We don't need coalesced allocnos for ira_reassign_pseudos. */
1588 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1589 {
1590 a = ira_allocnos[i];
1591 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
1592 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
1593 }
1594 ira_free_bitmap (processed_coalesced_allocno_bitmap);
1595 allocno_coalesced_p = false;
1596 }
1597
1598 \f
1599
1600 /* Output information about the loop given by its LOOP_TREE_NODE. */
1601 static void
1602 print_loop_title (ira_loop_tree_node_t loop_tree_node)
1603 {
1604 unsigned int j;
1605 bitmap_iterator bi;
1606
1607 ira_assert (loop_tree_node->loop != NULL);
1608 fprintf (ira_dump_file,
1609 "\n Loop %d (parent %d, header bb%d, depth %d)\n ref:",
1610 loop_tree_node->loop->num,
1611 (loop_tree_node->parent == NULL
1612 ? -1 : loop_tree_node->parent->loop->num),
1613 loop_tree_node->loop->header->index,
1614 loop_depth (loop_tree_node->loop));
1615 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->mentioned_allocnos, 0, j, bi)
1616 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
1617 fprintf (ira_dump_file, "\n modified regnos:");
1618 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
1619 fprintf (ira_dump_file, " %d", j);
1620 fprintf (ira_dump_file, "\n border:");
1621 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
1622 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
1623 fprintf (ira_dump_file, "\n Pressure:");
1624 for (j = 0; (int) j < ira_reg_class_cover_size; j++)
1625 {
1626 enum reg_class cover_class;
1627
1628 cover_class = ira_reg_class_cover[j];
1629 if (loop_tree_node->reg_pressure[cover_class] == 0)
1630 continue;
1631 fprintf (ira_dump_file, " %s=%d", reg_class_names[cover_class],
1632 loop_tree_node->reg_pressure[cover_class]);
1633 }
1634 fprintf (ira_dump_file, "\n");
1635 }
1636
1637 /* Color the allocnos inside loop (in the extreme case it can be all
1638 of the function) given the corresponding LOOP_TREE_NODE. The
1639 function is called for each loop during top-down traverse of the
1640 loop tree. */
1641 static void
1642 color_pass (ira_loop_tree_node_t loop_tree_node)
1643 {
1644 int regno, hard_regno, index = -1;
1645 int cost, exit_freq, enter_freq;
1646 unsigned int j;
1647 bitmap_iterator bi;
1648 enum machine_mode mode;
1649 enum reg_class rclass, cover_class;
1650 ira_allocno_t a, subloop_allocno;
1651 ira_loop_tree_node_t subloop_node;
1652
1653 ira_assert (loop_tree_node->bb == NULL);
1654 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1655 print_loop_title (loop_tree_node);
1656
1657 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->mentioned_allocnos);
1658 bitmap_ior_into (coloring_allocno_bitmap, loop_tree_node->border_allocnos);
1659 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
1660 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
1661 {
1662 a = ira_allocnos[j];
1663 if (! ALLOCNO_ASSIGNED_P (a))
1664 continue;
1665 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
1666 }
1667 /* Color all mentioned allocnos including transparent ones. */
1668 color_allocnos ();
1669 /* Process caps. They are processed just once. */
1670 if (flag_ira_algorithm == IRA_ALGORITHM_MIXED
1671 || flag_ira_algorithm == IRA_ALGORITHM_REGIONAL)
1672 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->mentioned_allocnos, 0, j, bi)
1673 {
1674 a = ira_allocnos[j];
1675 if (ALLOCNO_CAP_MEMBER (a) == NULL)
1676 continue;
1677 /* Remove from processing in the next loop. */
1678 bitmap_clear_bit (consideration_allocno_bitmap, j);
1679 rclass = ALLOCNO_COVER_CLASS (a);
1680 if ((flag_ira_algorithm == IRA_ALGORITHM_MIXED
1681 && loop_tree_node->reg_pressure[rclass]
1682 <= ira_available_class_regs[rclass]))
1683 {
1684 mode = ALLOCNO_MODE (a);
1685 hard_regno = ALLOCNO_HARD_REGNO (a);
1686 if (hard_regno >= 0)
1687 {
1688 index = ira_class_hard_reg_index[rclass][hard_regno];
1689 ira_assert (index >= 0);
1690 }
1691 regno = ALLOCNO_REGNO (a);
1692 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
1693 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
1694 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
1695 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1696 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
1697 if (hard_regno >= 0)
1698 update_copy_costs (subloop_allocno, true);
1699 /* We don't need updated costs anymore: */
1700 ira_free_allocno_updated_costs (subloop_allocno);
1701 }
1702 }
1703 /* Update costs of the corresponding allocnos (not caps) in the
1704 subloops. */
1705 for (subloop_node = loop_tree_node->subloops;
1706 subloop_node != NULL;
1707 subloop_node = subloop_node->subloop_next)
1708 {
1709 ira_assert (subloop_node->bb == NULL);
1710 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
1711 {
1712 a = ira_allocnos[j];
1713 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1714 mode = ALLOCNO_MODE (a);
1715 rclass = ALLOCNO_COVER_CLASS (a);
1716 hard_regno = ALLOCNO_HARD_REGNO (a);
1717 if (hard_regno >= 0)
1718 {
1719 index = ira_class_hard_reg_index[rclass][hard_regno];
1720 ira_assert (index >= 0);
1721 }
1722 regno = ALLOCNO_REGNO (a);
1723 /* ??? conflict costs */
1724 subloop_allocno = subloop_node->regno_allocno_map[regno];
1725 if (subloop_allocno == NULL
1726 || ALLOCNO_CAP (subloop_allocno) != NULL)
1727 continue;
1728 if ((flag_ira_algorithm == IRA_ALGORITHM_MIXED
1729 && (loop_tree_node->reg_pressure[rclass]
1730 <= ira_available_class_regs[rclass]))
1731 || (hard_regno < 0
1732 && ! bitmap_bit_p (subloop_node->mentioned_allocnos,
1733 ALLOCNO_NUM (subloop_allocno))))
1734 {
1735 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
1736 {
1737 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1738 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
1739 if (hard_regno >= 0)
1740 update_copy_costs (subloop_allocno, true);
1741 /* We don't need updated costs anymore: */
1742 ira_free_allocno_updated_costs (subloop_allocno);
1743 }
1744 continue;
1745 }
1746 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
1747 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
1748 ira_assert (regno < ira_reg_equiv_len);
1749 if (ira_reg_equiv_invariant_p[regno]
1750 || ira_reg_equiv_const[regno] != NULL_RTX)
1751 {
1752 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
1753 {
1754 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
1755 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
1756 if (hard_regno >= 0)
1757 update_copy_costs (subloop_allocno, true);
1758 /* We don't need updated costs anymore: */
1759 ira_free_allocno_updated_costs (subloop_allocno);
1760 }
1761 }
1762 else if (hard_regno < 0)
1763 {
1764 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
1765 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
1766 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
1767 }
1768 else
1769 {
1770 cover_class = ALLOCNO_COVER_CLASS (subloop_allocno);
1771 ira_allocate_and_set_costs
1772 (&ALLOCNO_HARD_REG_COSTS (subloop_allocno), cover_class,
1773 ALLOCNO_COVER_CLASS_COST (subloop_allocno));
1774 ira_allocate_and_set_costs
1775 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno),
1776 cover_class, 0);
1777 cost = (ira_register_move_cost[mode][rclass][rclass]
1778 * (exit_freq + enter_freq));
1779 ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
1780 ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
1781 -= cost;
1782 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
1783 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
1784 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
1785 if (ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1786 > ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index])
1787 ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1788 = ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index];
1789 }
1790 }
1791 }
1792 }
1793
1794 /* Initialize the common data for coloring and calls functions to do
1795 Chaitin-Briggs and regional coloring. */
1796 static void
1797 do_coloring (void)
1798 {
1799 coloring_allocno_bitmap = ira_allocate_bitmap ();
1800 allocnos_for_spilling
1801 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
1802 * ira_allocnos_num);
1803 splay_tree_node_pool = create_alloc_pool ("splay tree nodes",
1804 sizeof (struct splay_tree_node_s),
1805 100);
1806 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1807 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
1808
1809 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
1810
1811 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1812 ira_print_disposition (ira_dump_file);
1813
1814 free_alloc_pool (splay_tree_node_pool);
1815 ira_free_bitmap (coloring_allocno_bitmap);
1816 ira_free (allocnos_for_spilling);
1817 }
1818
1819 \f
1820
1821 /* Move spill/restore code, which are to be generated in ira-emit.c,
1822 to less frequent points (if it is profitable) by reassigning some
1823 allocnos (in loop with subloops containing in another loop) to
1824 memory which results in longer live-range where the corresponding
1825 pseudo-registers will be in memory. */
1826 static void
1827 move_spill_restore (void)
1828 {
1829 int cost, regno, hard_regno, hard_regno2, index;
1830 bool changed_p;
1831 int enter_freq, exit_freq;
1832 enum machine_mode mode;
1833 enum reg_class rclass;
1834 ira_allocno_t a, parent_allocno, subloop_allocno;
1835 ira_loop_tree_node_t parent, loop_node, subloop_node;
1836 ira_allocno_iterator ai;
1837
1838 for (;;)
1839 {
1840 changed_p = false;
1841 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1842 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
1843 FOR_EACH_ALLOCNO (a, ai)
1844 {
1845 regno = ALLOCNO_REGNO (a);
1846 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
1847 if (ALLOCNO_CAP_MEMBER (a) != NULL
1848 || ALLOCNO_CAP (a) != NULL
1849 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
1850 || loop_node->children == NULL
1851 /* don't do the optimization because it can create
1852 copies and the reload pass can spill the allocno set
1853 by copy although the allocno will not get memory
1854 slot. */
1855 || ira_reg_equiv_invariant_p[regno]
1856 || ira_reg_equiv_const[regno] != NULL_RTX
1857 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
1858 continue;
1859 mode = ALLOCNO_MODE (a);
1860 rclass = ALLOCNO_COVER_CLASS (a);
1861 index = ira_class_hard_reg_index[rclass][hard_regno];
1862 ira_assert (index >= 0);
1863 cost = (ALLOCNO_MEMORY_COST (a)
1864 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
1865 ? ALLOCNO_COVER_CLASS_COST (a)
1866 : ALLOCNO_HARD_REG_COSTS (a)[index]));
1867 for (subloop_node = loop_node->subloops;
1868 subloop_node != NULL;
1869 subloop_node = subloop_node->subloop_next)
1870 {
1871 ira_assert (subloop_node->bb == NULL);
1872 subloop_allocno = subloop_node->regno_allocno_map[regno];
1873 if (subloop_allocno == NULL)
1874 continue;
1875 /* We have accumulated cost. To get the real cost of
1876 allocno usage in the loop we should subtract costs of
1877 the subloop allocnos. */
1878 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
1879 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
1880 ? ALLOCNO_COVER_CLASS_COST (subloop_allocno)
1881 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
1882 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
1883 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
1884 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
1885 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
1886 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
1887 else
1888 {
1889 cost
1890 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
1891 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
1892 if (hard_regno2 != hard_regno)
1893 cost -= (ira_register_move_cost[mode][rclass][rclass]
1894 * (exit_freq + enter_freq));
1895 }
1896 }
1897 if ((parent = loop_node->parent) != NULL
1898 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
1899 {
1900 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
1901 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
1902 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
1903 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
1904 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
1905 else
1906 {
1907 cost
1908 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
1909 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
1910 if (hard_regno2 != hard_regno)
1911 cost -= (ira_register_move_cost[mode][rclass][rclass]
1912 * (exit_freq + enter_freq));
1913 }
1914 }
1915 if (cost < 0)
1916 {
1917 ALLOCNO_HARD_REGNO (a) = -1;
1918 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1919 {
1920 fprintf
1921 (ira_dump_file,
1922 " Moving spill/restore for a%dr%d up from loop %d",
1923 ALLOCNO_NUM (a), regno, loop_node->loop->num);
1924 fprintf (ira_dump_file, " - profit %d\n", -cost);
1925 }
1926 changed_p = true;
1927 }
1928 }
1929 if (! changed_p)
1930 break;
1931 }
1932 }
1933
1934 \f
1935
1936 /* Update current hard reg costs and current conflict hard reg costs
1937 for allocno A. It is done by processing its copies containing
1938 other allocnos already assigned. */
1939 static void
1940 update_curr_costs (ira_allocno_t a)
1941 {
1942 int i, hard_regno, cost;
1943 enum machine_mode mode;
1944 enum reg_class cover_class, rclass;
1945 ira_allocno_t another_a;
1946 ira_copy_t cp, next_cp;
1947
1948 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1949 cover_class = ALLOCNO_COVER_CLASS (a);
1950 if (cover_class == NO_REGS)
1951 return;
1952 mode = ALLOCNO_MODE (a);
1953 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1954 {
1955 if (cp->first == a)
1956 {
1957 next_cp = cp->next_first_allocno_copy;
1958 another_a = cp->second;
1959 }
1960 else if (cp->second == a)
1961 {
1962 next_cp = cp->next_second_allocno_copy;
1963 another_a = cp->first;
1964 }
1965 else
1966 gcc_unreachable ();
1967 if (cover_class != ALLOCNO_COVER_CLASS (another_a)
1968 || ! ALLOCNO_ASSIGNED_P (another_a)
1969 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
1970 continue;
1971 rclass = REGNO_REG_CLASS (hard_regno);
1972 i = ira_class_hard_reg_index[cover_class][hard_regno];
1973 ira_assert (i >= 0);
1974 cost = (cp->first == a
1975 ? ira_register_move_cost[mode][rclass][cover_class]
1976 : ira_register_move_cost[mode][cover_class][rclass]);
1977 ira_allocate_and_set_or_copy_costs
1978 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1979 cover_class, ALLOCNO_COVER_CLASS_COST (a),
1980 ALLOCNO_HARD_REG_COSTS (a));
1981 ira_allocate_and_set_or_copy_costs
1982 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1983 cover_class, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
1984 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
1985 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
1986 }
1987 }
1988
1989 /* Map: allocno number -> allocno priority. */
1990 static int *allocno_priorities;
1991
1992 /* Allocate array ALLOCNO_PRIORITIES and set up priorities for N allocnos in
1993 array CONSIDERATION_ALLOCNOS. */
1994 static void
1995 start_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
1996 {
1997 int i, length;
1998 ira_allocno_t a;
1999 allocno_live_range_t r;
2000
2001 for (i = 0; i < n; i++)
2002 {
2003 a = consideration_allocnos[i];
2004 for (length = 0, r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2005 length += r->finish - r->start + 1;
2006 if (length == 0)
2007 {
2008 allocno_priorities[ALLOCNO_NUM (a)] = 0;
2009 continue;
2010 }
2011 ira_assert (length > 0 && ALLOCNO_NREFS (a) >= 0);
2012 allocno_priorities[ALLOCNO_NUM (a)]
2013 = (((double) (floor_log2 (ALLOCNO_NREFS (a)) * ALLOCNO_FREQ (a))
2014 / length)
2015 * (10000 / REG_FREQ_MAX) * PSEUDO_REGNO_SIZE (ALLOCNO_REGNO (a)));
2016 }
2017 }
2018
2019 /* Sort allocnos according to their priorities which are calculated
2020 analogous to ones in file `global.c'. */
2021 static int
2022 allocno_priority_compare_func (const void *v1p, const void *v2p)
2023 {
2024 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2025 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2026 int pri1, pri2;
2027
2028 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
2029 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
2030 if (pri2 - pri1)
2031 return pri2 - pri1;
2032
2033 /* If regs are equally good, sort by allocnos, so that the results of
2034 qsort leave nothing to chance. */
2035 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2036 }
2037
2038 /* Try to assign hard registers to the unassigned allocnos and
2039 allocnos conflicting with them or conflicting with allocnos whose
2040 regno >= START_REGNO. The function is called after ira_flattening,
2041 so more allocnos (including ones created in ira-emit.c) will have a
2042 chance to get a hard register. We use simple assignment algorithm
2043 based on priorities. */
2044 void
2045 ira_reassign_conflict_allocnos (int start_regno)
2046 {
2047 int i, allocnos_to_color_num;
2048 ira_allocno_t a, conflict_a;
2049 ira_allocno_conflict_iterator aci;
2050 enum reg_class cover_class;
2051 bitmap allocnos_to_color;
2052 ira_allocno_iterator ai;
2053
2054 allocnos_to_color = ira_allocate_bitmap ();
2055 allocnos_to_color_num = 0;
2056 FOR_EACH_ALLOCNO (a, ai)
2057 {
2058 if (! ALLOCNO_ASSIGNED_P (a)
2059 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
2060 {
2061 if (ALLOCNO_COVER_CLASS (a) != NO_REGS)
2062 sorted_allocnos[allocnos_to_color_num++] = a;
2063 else
2064 {
2065 ALLOCNO_ASSIGNED_P (a) = true;
2066 ALLOCNO_HARD_REGNO (a) = -1;
2067 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2068 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2069 }
2070 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
2071 }
2072 if (ALLOCNO_REGNO (a) < start_regno
2073 || (cover_class = ALLOCNO_COVER_CLASS (a)) == NO_REGS)
2074 continue;
2075 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
2076 {
2077 ira_assert (cover_class == ALLOCNO_COVER_CLASS (conflict_a));
2078 if (bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
2079 continue;
2080 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a));
2081 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
2082 }
2083 }
2084 ira_free_bitmap (allocnos_to_color);
2085 if (allocnos_to_color_num > 1)
2086 {
2087 start_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
2088 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
2089 allocno_priority_compare_func);
2090 }
2091 for (i = 0; i < allocnos_to_color_num; i++)
2092 {
2093 a = sorted_allocnos[i];
2094 ALLOCNO_ASSIGNED_P (a) = false;
2095 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2096 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2097 update_curr_costs (a);
2098 }
2099 for (i = 0; i < allocnos_to_color_num; i++)
2100 {
2101 a = sorted_allocnos[i];
2102 if (assign_hard_reg (a, true))
2103 {
2104 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2105 fprintf
2106 (ira_dump_file,
2107 " Secondary allocation: assign hard reg %d to reg %d\n",
2108 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
2109 }
2110 }
2111 }
2112
2113 \f
2114
2115 /* This page contains code to coalesce memory stack slots used by
2116 spilled allocnos. This results in smaller stack frame, better data
2117 locality, and in smaller code for some architectures like
2118 x86/x86_64 where insn size depends on address displacement value.
2119 On the other hand, it can worsen insn scheduling after the RA but
2120 in practice it is less important than smaller stack frames. */
2121
2122 /* Usage cost and order number of coalesced allocno set to which
2123 given pseudo register belongs to. */
2124 static int *regno_coalesced_allocno_cost;
2125 static int *regno_coalesced_allocno_num;
2126
2127 /* Sort pseudos according frequencies of coalesced allocno sets they
2128 belong to (putting most frequently ones first), and according to
2129 coalesced allocno set order numbers. */
2130 static int
2131 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
2132 {
2133 const int regno1 = *(const int *) v1p;
2134 const int regno2 = *(const int *) v2p;
2135 int diff;
2136
2137 if ((diff = (regno_coalesced_allocno_cost[regno2]
2138 - regno_coalesced_allocno_cost[regno1])) != 0)
2139 return diff;
2140 if ((diff = (regno_coalesced_allocno_num[regno1]
2141 - regno_coalesced_allocno_num[regno2])) != 0)
2142 return diff;
2143 return regno1 - regno2;
2144 }
2145
2146 /* Widest width in which each pseudo reg is referred to (via subreg).
2147 It is used for sorting pseudo registers. */
2148 static unsigned int *regno_max_ref_width;
2149
2150 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
2151 #ifdef STACK_GROWS_DOWNWARD
2152 # undef STACK_GROWS_DOWNWARD
2153 # define STACK_GROWS_DOWNWARD 1
2154 #else
2155 # define STACK_GROWS_DOWNWARD 0
2156 #endif
2157
2158 /* Sort pseudos according their slot numbers (putting ones with
2159 smaller numbers first, or last when the frame pointer is not
2160 needed). */
2161 static int
2162 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
2163 {
2164 const int regno1 = *(const int *) v1p;
2165 const int regno2 = *(const int *) v2p;
2166 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
2167 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
2168 int diff, slot_num1, slot_num2;
2169 int total_size1, total_size2;
2170
2171 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
2172 {
2173 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
2174 return (const int *) v1p - (const int *) v2p; /* Save the order. */
2175 return 1;
2176 }
2177 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
2178 return -1;
2179 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
2180 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
2181 if ((diff = slot_num1 - slot_num2) != 0)
2182 return (frame_pointer_needed
2183 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
2184 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1), regno_max_ref_width[regno1]);
2185 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2), regno_max_ref_width[regno2]);
2186 if ((diff = total_size2 - total_size1) != 0)
2187 return diff;
2188 return (const int *) v1p - (const int *) v2p; /* Save the order. */
2189 }
2190
2191 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
2192 for coalesced allocno sets containing allocnos with their regnos
2193 given in array PSEUDO_REGNOS of length N. */
2194 static void
2195 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
2196 {
2197 int i, num, regno, cost;
2198 ira_allocno_t allocno, a;
2199
2200 for (num = i = 0; i < n; i++)
2201 {
2202 regno = pseudo_regnos[i];
2203 allocno = ira_regno_allocno_map[regno];
2204 if (allocno == NULL)
2205 {
2206 regno_coalesced_allocno_cost[regno] = 0;
2207 regno_coalesced_allocno_num[regno] = ++num;
2208 continue;
2209 }
2210 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2211 continue;
2212 num++;
2213 for (cost = 0, a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2214 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2215 {
2216 cost += ALLOCNO_FREQ (a);
2217 if (a == allocno)
2218 break;
2219 }
2220 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2221 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2222 {
2223 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
2224 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
2225 if (a == allocno)
2226 break;
2227 }
2228 }
2229 }
2230
2231 /* Collect spilled allocnos representing coalesced allocno sets (the
2232 first coalesced allocno). The collected allocnos are returned
2233 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
2234 number of the collected allocnos. The allocnos are given by their
2235 regnos in array PSEUDO_REGNOS of length N. */
2236 static int
2237 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
2238 ira_allocno_t *spilled_coalesced_allocnos)
2239 {
2240 int i, num, regno;
2241 ira_allocno_t allocno;
2242
2243 for (num = i = 0; i < n; i++)
2244 {
2245 regno = pseudo_regnos[i];
2246 allocno = ira_regno_allocno_map[regno];
2247 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
2248 || ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno)
2249 continue;
2250 spilled_coalesced_allocnos[num++] = allocno;
2251 }
2252 return num;
2253 }
2254
2255 /* We have coalesced allocnos involving in copies. Coalesce allocnos
2256 further in order to share the same memory stack slot. Allocnos
2257 representing sets of allocnos coalesced before the call are given
2258 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
2259 some allocnos were coalesced in the function. */
2260 static bool
2261 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
2262 {
2263 int i, j;
2264 ira_allocno_t allocno, a;
2265 bool merged_p = false;
2266
2267 /* Coalesce non-conflicting spilled allocnos preferring most
2268 frequently used. */
2269 for (i = 0; i < num; i++)
2270 {
2271 allocno = spilled_coalesced_allocnos[i];
2272 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2273 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
2274 && (ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)]
2275 || ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX)))
2276 continue;
2277 for (j = 0; j < i; j++)
2278 {
2279 a = spilled_coalesced_allocnos[j];
2280 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) != a
2281 || (ALLOCNO_REGNO (a) < ira_reg_equiv_len
2282 && (ira_reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
2283 || ira_reg_equiv_const[ALLOCNO_REGNO (a)] != NULL_RTX))
2284 || coalesced_allocno_conflict_p (allocno, a, true))
2285 continue;
2286 allocno_coalesced_p = true;
2287 merged_p = true;
2288 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2289 fprintf (ira_dump_file,
2290 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
2291 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
2292 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2293 merge_allocnos (a, allocno);
2294 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a);
2295 }
2296 }
2297 return merged_p;
2298 }
2299
2300 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
2301 subsequent assigning stack slots to them in the reload pass. To do
2302 this we coalesce spilled allocnos first to decrease the number of
2303 memory-memory move insns. This function is called by the
2304 reload. */
2305 void
2306 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
2307 unsigned int *reg_max_ref_width)
2308 {
2309 int max_regno = max_reg_num ();
2310 int i, regno, num, slot_num;
2311 ira_allocno_t allocno, a;
2312 ira_allocno_iterator ai;
2313 ira_allocno_t *spilled_coalesced_allocnos;
2314
2315 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
2316 /* Set up allocnos can be coalesced. */
2317 coloring_allocno_bitmap = ira_allocate_bitmap ();
2318 for (i = 0; i < n; i++)
2319 {
2320 regno = pseudo_regnos[i];
2321 allocno = ira_regno_allocno_map[regno];
2322 if (allocno != NULL)
2323 bitmap_set_bit (coloring_allocno_bitmap,
2324 ALLOCNO_NUM (allocno));
2325 }
2326 allocno_coalesced_p = false;
2327 coalesce_allocnos (true);
2328 ira_free_bitmap (coloring_allocno_bitmap);
2329 regno_coalesced_allocno_cost
2330 = (int *) ira_allocate (max_regno * sizeof (int));
2331 regno_coalesced_allocno_num
2332 = (int *) ira_allocate (max_regno * sizeof (int));
2333 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
2334 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2335 /* Sort regnos according frequencies of the corresponding coalesced
2336 allocno sets. */
2337 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
2338 spilled_coalesced_allocnos
2339 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
2340 * sizeof (ira_allocno_t));
2341 /* Collect allocnos representing the spilled coalesced allocno
2342 sets. */
2343 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2344 spilled_coalesced_allocnos);
2345 if (flag_ira_share_spill_slots
2346 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
2347 {
2348 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
2349 qsort (pseudo_regnos, n, sizeof (int),
2350 coalesced_pseudo_reg_freq_compare);
2351 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
2352 spilled_coalesced_allocnos);
2353 }
2354 ira_free_bitmap (processed_coalesced_allocno_bitmap);
2355 allocno_coalesced_p = false;
2356 /* Assign stack slot numbers to spilled allocno sets, use smaller
2357 numbers for most frequently used coalesced allocnos. -1 is
2358 reserved for dynamic search of stack slots for pseudos spilled by
2359 the reload. */
2360 slot_num = 1;
2361 for (i = 0; i < num; i++)
2362 {
2363 allocno = spilled_coalesced_allocnos[i];
2364 if (ALLOCNO_FIRST_COALESCED_ALLOCNO (allocno) != allocno
2365 || ALLOCNO_HARD_REGNO (allocno) >= 0
2366 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
2367 && (ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)]
2368 || ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX)))
2369 continue;
2370 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2371 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
2372 slot_num++;
2373 for (a = ALLOCNO_NEXT_COALESCED_ALLOCNO (allocno);;
2374 a = ALLOCNO_NEXT_COALESCED_ALLOCNO (a))
2375 {
2376 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
2377 ALLOCNO_HARD_REGNO (a) = -slot_num;
2378 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2379 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
2380 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
2381 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
2382 reg_max_ref_width[ALLOCNO_REGNO (a)]));
2383
2384 if (a == allocno)
2385 break;
2386 }
2387 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2388 fprintf (ira_dump_file, "\n");
2389 }
2390 ira_spilled_reg_stack_slots_num = slot_num - 1;
2391 ira_free (spilled_coalesced_allocnos);
2392 /* Sort regnos according the slot numbers. */
2393 regno_max_ref_width = reg_max_ref_width;
2394 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
2395 /* Uncoalesce allocnos which is necessary for (re)assigning during
2396 the reload pass. */
2397 FOR_EACH_ALLOCNO (a, ai)
2398 {
2399 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
2400 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
2401 }
2402 ira_free (regno_coalesced_allocno_num);
2403 ira_free (regno_coalesced_allocno_cost);
2404 }
2405
2406 \f
2407
2408 /* This page contains code used by the reload pass to improve the
2409 final code. */
2410
2411 /* The function is called from reload to mark changes in the
2412 allocation of REGNO made by the reload. Remember that reg_renumber
2413 reflects the change result. */
2414 void
2415 ira_mark_allocation_change (int regno)
2416 {
2417 ira_allocno_t a = ira_regno_allocno_map[regno];
2418 int old_hard_regno, hard_regno, cost;
2419 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
2420
2421 ira_assert (a != NULL);
2422 hard_regno = reg_renumber[regno];
2423 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
2424 return;
2425 if (old_hard_regno < 0)
2426 cost = -ALLOCNO_MEMORY_COST (a);
2427 else
2428 {
2429 ira_assert (ira_class_hard_reg_index[cover_class][old_hard_regno] >= 0);
2430 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
2431 ? ALLOCNO_COVER_CLASS_COST (a)
2432 : ALLOCNO_HARD_REG_COSTS (a)
2433 [ira_class_hard_reg_index[cover_class][old_hard_regno]]);
2434 update_copy_costs (a, false);
2435 }
2436 ira_overall_cost -= cost;
2437 ALLOCNO_HARD_REGNO (a) = hard_regno;
2438 if (hard_regno < 0)
2439 {
2440 ALLOCNO_HARD_REGNO (a) = -1;
2441 cost += ALLOCNO_MEMORY_COST (a);
2442 }
2443 else if (ira_class_hard_reg_index[cover_class][hard_regno] >= 0)
2444 {
2445 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
2446 ? ALLOCNO_COVER_CLASS_COST (a)
2447 : ALLOCNO_HARD_REG_COSTS (a)
2448 [ira_class_hard_reg_index[cover_class][hard_regno]]);
2449 update_copy_costs (a, true);
2450 }
2451 else
2452 /* Reload changed class of the allocno. */
2453 cost = 0;
2454 ira_overall_cost += cost;
2455 }
2456
2457 /* This function is called when reload deletes memory-memory move. In
2458 this case we marks that the allocation of the corresponding
2459 allocnos should be not changed in future. Otherwise we risk to get
2460 a wrong code. */
2461 void
2462 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
2463 {
2464 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
2465 ira_allocno_t src = ira_regno_allocno_map[src_regno];
2466
2467 ira_assert (dst != NULL && src != NULL
2468 && ALLOCNO_HARD_REGNO (dst) < 0
2469 && ALLOCNO_HARD_REGNO (src) < 0);
2470 ALLOCNO_DONT_REASSIGN_P (dst) = true;
2471 ALLOCNO_DONT_REASSIGN_P (src) = true;
2472 }
2473
2474 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
2475 allocno A and return TRUE in the case of success. That is an
2476 analog of retry_global_alloc for IRA. */
2477 static bool
2478 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
2479 {
2480 int hard_regno;
2481 enum reg_class cover_class;
2482 int regno = ALLOCNO_REGNO (a);
2483
2484 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), forbidden_regs);
2485 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2486 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), call_used_reg_set);
2487 ALLOCNO_ASSIGNED_P (a) = false;
2488 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2489 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2490 cover_class = ALLOCNO_COVER_CLASS (a);
2491 update_curr_costs (a);
2492 assign_hard_reg (a, true);
2493 hard_regno = ALLOCNO_HARD_REGNO (a);
2494 reg_renumber[regno] = hard_regno;
2495 if (hard_regno < 0)
2496 ALLOCNO_HARD_REGNO (a) = -1;
2497 else
2498 {
2499 ira_assert (ira_class_hard_reg_index[cover_class][hard_regno] >= 0);
2500 ira_overall_cost -= (ALLOCNO_MEMORY_COST (a)
2501 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
2502 ? ALLOCNO_COVER_CLASS_COST (a)
2503 : ALLOCNO_HARD_REG_COSTS (a)
2504 [ira_class_hard_reg_index
2505 [cover_class][hard_regno]]));
2506 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
2507 && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
2508 call_used_reg_set))
2509 {
2510 ira_assert (flag_caller_saves);
2511 caller_save_needed = 1;
2512 }
2513 }
2514
2515 /* If we found a hard register, modify the RTL for the pseudo
2516 register to show the hard register, and mark the pseudo register
2517 live. */
2518 if (reg_renumber[regno] >= 0)
2519 {
2520 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2521 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
2522 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
2523 mark_home_live (regno);
2524 }
2525 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2526 fprintf (ira_dump_file, "\n");
2527
2528 return reg_renumber[regno] >= 0;
2529 }
2530
2531 /* Sort pseudos according their usage frequencies (putting most
2532 frequently ones first). */
2533 static int
2534 pseudo_reg_compare (const void *v1p, const void *v2p)
2535 {
2536 int regno1 = *(const int *) v1p;
2537 int regno2 = *(const int *) v2p;
2538 int diff;
2539
2540 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
2541 return diff;
2542 return regno1 - regno2;
2543 }
2544
2545 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
2546 NUM of them) or spilled pseudos conflicting with pseudos in
2547 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
2548 allocation has been changed. The function doesn't use
2549 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
2550 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
2551 is called by the reload pass at the end of each reload
2552 iteration. */
2553 bool
2554 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
2555 HARD_REG_SET bad_spill_regs,
2556 HARD_REG_SET *pseudo_forbidden_regs,
2557 HARD_REG_SET *pseudo_previous_regs, bitmap spilled)
2558 {
2559 int i, m, n, regno;
2560 bool changed_p;
2561 ira_allocno_t a, conflict_a;
2562 HARD_REG_SET forbidden_regs;
2563 ira_allocno_conflict_iterator aci;
2564
2565 if (num > 1)
2566 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
2567 changed_p = false;
2568 /* Try to assign hard registers to pseudos from
2569 SPILLED_PSEUDO_REGS. */
2570 for (m = i = 0; i < num; i++)
2571 {
2572 regno = spilled_pseudo_regs[i];
2573 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
2574 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
2575 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
2576 gcc_assert (reg_renumber[regno] < 0);
2577 a = ira_regno_allocno_map[regno];
2578 ira_mark_allocation_change (regno);
2579 ira_assert (reg_renumber[regno] < 0);
2580 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2581 fprintf (ira_dump_file,
2582 " Spill %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
2583 ALLOCNO_MEMORY_COST (a)
2584 - ALLOCNO_COVER_CLASS_COST (a));
2585 allocno_reload_assign (a, forbidden_regs);
2586 if (reg_renumber[regno] >= 0)
2587 {
2588 CLEAR_REGNO_REG_SET (spilled, regno);
2589 changed_p = true;
2590 }
2591 else
2592 spilled_pseudo_regs[m++] = regno;
2593 }
2594 if (m == 0)
2595 return changed_p;
2596 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2597 {
2598 fprintf (ira_dump_file, " Spilled regs");
2599 for (i = 0; i < m; i++)
2600 fprintf (ira_dump_file, " %d", spilled_pseudo_regs[i]);
2601 fprintf (ira_dump_file, "\n");
2602 }
2603 /* Try to assign hard registers to pseudos conflicting with ones
2604 from SPILLED_PSEUDO_REGS. */
2605 for (i = n = 0; i < m; i++)
2606 {
2607 regno = spilled_pseudo_regs[i];
2608 a = ira_regno_allocno_map[regno];
2609 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
2610 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
2611 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
2612 && ! bitmap_bit_p (consideration_allocno_bitmap,
2613 ALLOCNO_NUM (conflict_a)))
2614 {
2615 sorted_allocnos[n++] = conflict_a;
2616 bitmap_set_bit (consideration_allocno_bitmap,
2617 ALLOCNO_NUM (conflict_a));
2618 }
2619 }
2620 if (n != 0)
2621 {
2622 start_allocno_priorities (sorted_allocnos, n);
2623 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2624 allocno_priority_compare_func);
2625 for (i = 0; i < n; i++)
2626 {
2627 a = sorted_allocnos[i];
2628 regno = ALLOCNO_REGNO (a);
2629 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
2630 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
2631 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
2632 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2633 fprintf (ira_dump_file,
2634 " Try assign %d(a%d), cost=%d",
2635 regno, ALLOCNO_NUM (a),
2636 ALLOCNO_MEMORY_COST (a)
2637 - ALLOCNO_COVER_CLASS_COST (a));
2638 if (allocno_reload_assign (a, forbidden_regs))
2639 {
2640 changed_p = true;
2641 bitmap_clear_bit (spilled, regno);
2642 }
2643 }
2644 }
2645 return changed_p;
2646 }
2647
2648 /* The function is called by reload and returns already allocated
2649 stack slot (if any) for REGNO with given INHERENT_SIZE and
2650 TOTAL_SIZE. In the case of failure to find a slot which can be
2651 used for REGNO, the function returns NULL. */
2652 rtx
2653 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
2654 unsigned int total_size)
2655 {
2656 unsigned int i;
2657 int slot_num, best_slot_num;
2658 int cost, best_cost;
2659 ira_copy_t cp, next_cp;
2660 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
2661 rtx x;
2662 bitmap_iterator bi;
2663 struct ira_spilled_reg_stack_slot *slot = NULL;
2664
2665 ira_assert (flag_ira && inherent_size == PSEUDO_REGNO_BYTES (regno)
2666 && inherent_size <= total_size
2667 && ALLOCNO_HARD_REGNO (allocno) < 0);
2668 if (! flag_ira_share_spill_slots)
2669 return NULL_RTX;
2670 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
2671 if (slot_num != -1)
2672 {
2673 slot = &ira_spilled_reg_stack_slots[slot_num];
2674 x = slot->mem;
2675 }
2676 else
2677 {
2678 best_cost = best_slot_num = -1;
2679 x = NULL_RTX;
2680 /* It means that the pseudo was spilled in the reload pass, try
2681 to reuse a slot. */
2682 for (slot_num = 0;
2683 slot_num < ira_spilled_reg_stack_slots_num;
2684 slot_num++)
2685 {
2686 slot = &ira_spilled_reg_stack_slots[slot_num];
2687 if (slot->mem == NULL_RTX)
2688 continue;
2689 if (slot->width < total_size
2690 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
2691 continue;
2692
2693 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2694 FIRST_PSEUDO_REGISTER, i, bi)
2695 {
2696 another_allocno = ira_regno_allocno_map[i];
2697 if (ira_allocno_live_ranges_intersect_p (allocno,
2698 another_allocno))
2699 goto cont;
2700 }
2701 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
2702 cp != NULL;
2703 cp = next_cp)
2704 {
2705 if (cp->first == allocno)
2706 {
2707 next_cp = cp->next_first_allocno_copy;
2708 another_allocno = cp->second;
2709 }
2710 else if (cp->second == allocno)
2711 {
2712 next_cp = cp->next_second_allocno_copy;
2713 another_allocno = cp->first;
2714 }
2715 else
2716 gcc_unreachable ();
2717 if (cp->insn == NULL_RTX)
2718 continue;
2719 if (bitmap_bit_p (&slot->spilled_regs,
2720 ALLOCNO_REGNO (another_allocno)))
2721 cost += cp->freq;
2722 }
2723 if (cost > best_cost)
2724 {
2725 best_cost = cost;
2726 best_slot_num = slot_num;
2727 }
2728 cont:
2729 ;
2730 }
2731 if (best_cost >= 0)
2732 {
2733 slot = &ira_spilled_reg_stack_slots[best_slot_num];
2734 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2735 x = slot->mem;
2736 ALLOCNO_HARD_REGNO (allocno) = -best_slot_num - 2;
2737 }
2738 }
2739 if (x != NULL_RTX)
2740 {
2741 ira_assert (slot->width >= total_size);
2742 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2743 FIRST_PSEUDO_REGISTER, i, bi)
2744 {
2745 ira_assert (! ira_pseudo_live_ranges_intersect_p (regno, i));
2746 }
2747 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2748 if (internal_flag_ira_verbose > 3 && ira_dump_file)
2749 {
2750 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
2751 regno, REG_FREQ (regno), slot_num);
2752 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
2753 FIRST_PSEUDO_REGISTER, i, bi)
2754 {
2755 if ((unsigned) regno != i)
2756 fprintf (ira_dump_file, " %d", i);
2757 }
2758 fprintf (ira_dump_file, "\n");
2759 }
2760 }
2761 return x;
2762 }
2763
2764 /* This is called by reload every time a new stack slot X with
2765 TOTAL_SIZE was allocated for REGNO. We store this info for
2766 subsequent ira_reuse_stack_slot calls. */
2767 void
2768 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
2769 {
2770 struct ira_spilled_reg_stack_slot *slot;
2771 int slot_num;
2772 ira_allocno_t allocno;
2773
2774 ira_assert (flag_ira && PSEUDO_REGNO_BYTES (regno) <= total_size);
2775 allocno = ira_regno_allocno_map[regno];
2776 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
2777 if (slot_num == -1)
2778 {
2779 slot_num = ira_spilled_reg_stack_slots_num++;
2780 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
2781 }
2782 slot = &ira_spilled_reg_stack_slots[slot_num];
2783 INIT_REG_SET (&slot->spilled_regs);
2784 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
2785 slot->mem = x;
2786 slot->width = total_size;
2787 if (internal_flag_ira_verbose > 3 && ira_dump_file)
2788 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
2789 regno, REG_FREQ (regno), slot_num);
2790 }
2791
2792
2793 /* Return spill cost for pseudo-registers whose numbers are in array
2794 REGNOS (with a negative number as an end marker) for reload with
2795 given IN and OUT for INSN. Return also number points (through
2796 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
2797 the register pressure is high, number of references of the
2798 pseudo-registers (through NREFS), number of callee-clobbered
2799 hard-registers occupied by the pseudo-registers (through
2800 CALL_USED_COUNT), and the first hard regno occupied by the
2801 pseudo-registers (through FIRST_HARD_REGNO). */
2802 static int
2803 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
2804 int *excess_pressure_live_length,
2805 int *nrefs, int *call_used_count, int *first_hard_regno)
2806 {
2807 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
2808 bool in_p, out_p;
2809 int length;
2810 ira_allocno_t a;
2811
2812 *nrefs = 0;
2813 for (length = count = cost = i = 0;; i++)
2814 {
2815 regno = regnos[i];
2816 if (regno < 0)
2817 break;
2818 *nrefs += REG_N_REFS (regno);
2819 hard_regno = reg_renumber[regno];
2820 ira_assert (hard_regno >= 0);
2821 a = ira_regno_allocno_map[regno];
2822 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2823 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_COVER_CLASS_COST (a);
2824 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
2825 for (j = 0; j < nregs; j++)
2826 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
2827 break;
2828 if (j == nregs)
2829 count++;
2830 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
2831 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
2832 if ((in_p || out_p)
2833 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
2834 {
2835 saved_cost = 0;
2836 if (in_p)
2837 saved_cost += ira_memory_move_cost
2838 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][1];
2839 if (out_p)
2840 saved_cost
2841 += ira_memory_move_cost
2842 [ALLOCNO_MODE (a)][ALLOCNO_COVER_CLASS (a)][0];
2843 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
2844 }
2845 }
2846 *excess_pressure_live_length = length;
2847 *call_used_count = count;
2848 hard_regno = -1;
2849 if (regnos[0] >= 0)
2850 {
2851 hard_regno = reg_renumber[regnos[0]];
2852 }
2853 *first_hard_regno = hard_regno;
2854 return cost;
2855 }
2856
2857 /* Return TRUE if spilling pseudo-registers whose numbers are in array
2858 REGNOS is better than spilling pseudo-registers with numbers in
2859 OTHER_REGNOS for reload with given IN and OUT for INSN. The
2860 function used by the reload pass to make better register spilling
2861 decisions. */
2862 bool
2863 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
2864 rtx in, rtx out, rtx insn)
2865 {
2866 int cost, other_cost;
2867 int length, other_length;
2868 int nrefs, other_nrefs;
2869 int call_used_count, other_call_used_count;
2870 int hard_regno, other_hard_regno;
2871
2872 cost = calculate_spill_cost (regnos, in, out, insn,
2873 &length, &nrefs, &call_used_count, &hard_regno);
2874 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
2875 &other_length, &other_nrefs,
2876 &other_call_used_count,
2877 &other_hard_regno);
2878 if (nrefs == 0 && other_nrefs != 0)
2879 return true;
2880 if (nrefs != 0 && other_nrefs == 0)
2881 return false;
2882 if (cost != other_cost)
2883 return cost < other_cost;
2884 if (length != other_length)
2885 return length > other_length;
2886 #ifdef REG_ALLOC_ORDER
2887 if (hard_regno >= 0 && other_hard_regno >= 0)
2888 return (inv_reg_alloc_order[hard_regno]
2889 < inv_reg_alloc_order[other_hard_regno]);
2890 #else
2891 if (call_used_count != other_call_used_count)
2892 return call_used_count > other_call_used_count;
2893 #endif
2894 return false;
2895 }
2896
2897 \f
2898
2899 /* Allocate and initialize data necessary for assign_hard_reg. */
2900 void
2901 ira_initiate_assign (void)
2902 {
2903 sorted_allocnos
2904 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2905 * ira_allocnos_num);
2906 consideration_allocno_bitmap = ira_allocate_bitmap ();
2907 initiate_cost_update ();
2908 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
2909 }
2910
2911 /* Deallocate data used by assign_hard_reg. */
2912 void
2913 ira_finish_assign (void)
2914 {
2915 ira_free (sorted_allocnos);
2916 ira_free_bitmap (consideration_allocno_bitmap);
2917 finish_cost_update ();
2918 ira_free (allocno_priorities);
2919 }
2920
2921 \f
2922
2923 /* Entry function doing color-based register allocation. */
2924 void
2925 ira_color (void)
2926 {
2927 allocno_stack_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
2928 conflict_allocno_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
2929 removed_splay_allocno_vec
2930 = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
2931 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
2932 ira_initiate_assign ();
2933 do_coloring ();
2934 ira_finish_assign ();
2935 VEC_free (ira_allocno_t, heap, removed_splay_allocno_vec);
2936 VEC_free (ira_allocno_t, heap, conflict_allocno_vec);
2937 VEC_free (ira_allocno_t, heap, allocno_stack_vec);
2938 move_spill_restore ();
2939 }
2940
2941 \f
2942
2943 /* This page contains a simple register allocator without usage of
2944 allocno conflicts. This is used for fast allocation for -O0. */
2945
2946 /* Do register allocation by not using allocno conflicts. It uses
2947 only allocno live ranges. The algorithm is close to Chow's
2948 priority coloring. */
2949 void
2950 ira_fast_allocation (void)
2951 {
2952 int i, j, k, l, num, class_size, hard_regno;
2953 #ifdef STACK_REGS
2954 bool no_stack_reg_p;
2955 #endif
2956 enum reg_class cover_class;
2957 enum machine_mode mode;
2958 ira_allocno_t a;
2959 ira_allocno_iterator ai;
2960 allocno_live_range_t r;
2961 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
2962
2963 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
2964 FOR_EACH_ALLOCNO (a, ai)
2965 {
2966 l = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2967 if (l <= 0)
2968 l = 1;
2969 allocno_priorities[ALLOCNO_NUM (a)]
2970 = (((double) (floor_log2 (ALLOCNO_NREFS (a))
2971 * (ALLOCNO_MEMORY_COST (a)
2972 - ALLOCNO_COVER_CLASS_COST (a))) / l)
2973 * (10000 / REG_FREQ_MAX)
2974 * ira_reg_class_nregs[ALLOCNO_COVER_CLASS (a)][ALLOCNO_MODE (a)]);
2975 }
2976 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
2977 * ira_max_point);
2978 for (i = 0; i < ira_max_point; i++)
2979 CLEAR_HARD_REG_SET (used_hard_regs[i]);
2980 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2981 * ira_allocnos_num);
2982 num = 0;
2983 FOR_EACH_ALLOCNO (a, ai)
2984 sorted_allocnos[num++] = a;
2985 qsort (sorted_allocnos, ira_allocnos_num, sizeof (ira_allocno_t),
2986 allocno_priority_compare_func);
2987 for (i = 0; i < num; i++)
2988 {
2989 a = sorted_allocnos[i];
2990 ALLOCNO_ASSIGNED_P (a) = true;
2991 ALLOCNO_HARD_REGNO (a) = -1;
2992 /* Live info about hard registers are absent when OPTIMIZE==0.
2993 So try to assign hard-registers only to local allocnos. */
2994 if (!optimize && REG_BASIC_BLOCK (ALLOCNO_REGNO (a)) == REG_BLOCK_GLOBAL)
2995 continue;
2996 COPY_HARD_REG_SET (conflict_hard_regs, ALLOCNO_CONFLICT_HARD_REGS (a));
2997 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2998 for (j = r->start; j <= r->finish; j++)
2999 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
3000 cover_class = ALLOCNO_COVER_CLASS (a);
3001 if (hard_reg_set_subset_p (reg_class_contents[cover_class],
3002 conflict_hard_regs))
3003 continue;
3004 mode = ALLOCNO_MODE (a);
3005 #ifdef STACK_REGS
3006 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
3007 #endif
3008 class_size = ira_class_hard_regs_num[cover_class];
3009 for (j = 0; j < class_size; j++)
3010 {
3011 hard_regno = ira_class_hard_regs[cover_class][j];
3012 #ifdef STACK_REGS
3013 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
3014 && hard_regno <= LAST_STACK_REG)
3015 continue;
3016 #endif
3017 if (!ira_hard_reg_not_in_set_p (hard_regno, mode, conflict_hard_regs)
3018 || (TEST_HARD_REG_BIT
3019 (prohibited_class_mode_regs[cover_class][mode], hard_regno)))
3020 continue;
3021 ALLOCNO_HARD_REGNO (a) = hard_regno;
3022 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
3023 for (k = r->start; k <= r->finish; k++)
3024 IOR_HARD_REG_SET (used_hard_regs[k],
3025 ira_reg_mode_hard_regset[hard_regno][mode]);
3026 break;
3027 }
3028 }
3029 ira_free (sorted_allocnos);
3030 ira_free (used_hard_regs);
3031 ira_free (allocno_priorities);
3032 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3033 ira_print_disposition (ira_dump_file);
3034 }