1 /* Graph coloring register allocator
2 Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Michael Matz <matz@suse.de>
4 and Daniel Berlin <dan@cgsoftware.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 You should have received a copy of the GNU General Public License along
18 with GCC; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
35 /* This file is part of the graph coloring register allocator.
36 It contains the graph colorizer. Given an interference graph
37 as set up in ra-build.c the toplevel function in this file
38 (ra_colorize_graph) colorizes the graph, leaving a list
39 of colored, coalesced and spilled nodes.
41 The algorithm used is a merge of George & Appels iterative coalescing
42 and optimistic coalescing, switchable at runtime. The current default
43 is "optimistic coalescing +", which is based on the normal Briggs/Cooper
44 framework. We can also use biased coloring. Most of the structure
45 here follows the different papers.
47 Additionally there is a custom step to locally improve the overall
48 spill cost of the colored graph (recolor_spills). */
50 static void push_list (struct dlist
*, struct dlist
**);
51 static void push_list_end (struct dlist
*, struct dlist
**);
52 static void free_dlist (struct dlist
**);
53 static void put_web_at_end (struct web
*, enum ra_node_type
);
54 static void put_move (struct move
*, enum move_type
);
55 static void build_worklists (struct df
*);
56 static void enable_move (struct web
*);
57 static void decrement_degree (struct web
*, int);
58 static void simplify (void);
59 static void remove_move_1 (struct web
*, struct move
*);
60 static void remove_move (struct web
*, struct move
*);
61 static void add_worklist (struct web
*);
62 static int ok (struct web
*, struct web
*);
63 static int conservative (struct web
*, struct web
*);
64 static inline unsigned int simplify_p (enum ra_node_type
);
65 static void combine (struct web
*, struct web
*);
66 static void coalesce (void);
67 static void freeze_moves (struct web
*);
68 static void freeze (void);
69 static void select_spill (void);
70 static int color_usable_p (int, HARD_REG_SET
, HARD_REG_SET
,
72 int get_free_reg (HARD_REG_SET
, HARD_REG_SET
, enum machine_mode
);
73 static int get_biased_reg (HARD_REG_SET
, HARD_REG_SET
, HARD_REG_SET
,
74 HARD_REG_SET
, enum machine_mode
);
75 static int count_long_blocks (HARD_REG_SET
, int);
76 static char * hardregset_to_string (HARD_REG_SET
);
77 static void calculate_dont_begin (struct web
*, HARD_REG_SET
*);
78 static void colorize_one_web (struct web
*, int);
79 static void assign_colors (void);
80 static void try_recolor_web (struct web
*);
81 static void insert_coalesced_conflicts (void);
82 static int comp_webs_maxcost (const void *, const void *);
83 static void recolor_spills (void);
84 static void check_colors (void);
85 static void restore_conflicts_from_coalesce (struct web
*);
86 static void break_coalesced_spills (void);
87 static void unalias_web (struct web
*);
88 static void break_aliases_to_web (struct web
*);
89 static void break_precolored_alias (struct web
*);
90 static void init_web_pairs (void);
91 static void add_web_pair_cost (struct web
*, struct web
*,
92 unsigned HOST_WIDE_INT
, unsigned int);
93 static int comp_web_pairs (const void *, const void *);
94 static void sort_and_combine_web_pairs (int);
95 static int ok_class (struct web
*, struct web
*);
96 static void aggressive_coalesce (void);
97 static void extended_coalesce_2 (void);
98 static void check_uncoalesced_moves (void);
100 static struct dlist
*mv_worklist
, *mv_coalesced
, *mv_constrained
;
101 static struct dlist
*mv_frozen
, *mv_active
;
103 /* Push a node onto the front of the list. */
106 push_list (struct dlist
*x
, struct dlist
**list
)
108 if (x
->next
|| x
->prev
)
117 push_list_end (struct dlist
*x
, struct dlist
**list
)
119 if (x
->prev
|| x
->next
)
126 while ((*list
)->next
)
127 list
= &((*list
)->next
);
132 /* Remove a node from the list. */
135 remove_list (struct dlist
*x
, struct dlist
**list
)
137 struct dlist
*y
= x
->prev
;
145 x
->next
= x
->prev
= NULL
;
148 /* Pop the front of the list. */
151 pop_list (struct dlist
**list
)
153 struct dlist
*r
= *list
;
155 remove_list (r
, list
);
159 /* Free the given double linked list. */
162 free_dlist (struct dlist
**list
)
167 /* The web WEB should get the given new TYPE. Put it onto the
169 Inline, because it's called with constant TYPE every time. */
172 put_web (struct web
*web
, enum ra_node_type type
)
184 push_list (web
->dlink
, &WEBS(type
));
187 push_list (web
->dlink
, &WEBS(INITIAL
));
191 push_list (web
->dlink
, &WEBS(type
= SIMPLIFY_SPILL
));
192 else if (web
->add_hardregs
)
193 push_list (web
->dlink
, &WEBS(type
= SIMPLIFY_FAT
));
195 push_list (web
->dlink
, &WEBS(SIMPLIFY
));
203 /* After we are done with the whole pass of coloring/spilling,
204 we reset the lists of webs, in preparation of the next pass.
205 The spilled webs become free, colored webs go to the initial list,
206 coalesced webs become free or initial, according to what type of web
207 they are coalesced to. */
214 if (WEBS(SIMPLIFY
) || WEBS(SIMPLIFY_SPILL
) || WEBS(SIMPLIFY_FAT
)
215 || WEBS(FREEZE
) || WEBS(SPILL
) || WEBS(SELECT
))
218 while ((d
= pop_list (&WEBS(COALESCED
))) != NULL
)
220 struct web
*web
= DLIST_WEB (d
);
221 struct web
*aweb
= alias (web
);
222 /* Note, how alias() becomes invalid through the two put_web()'s
223 below. It might set the type of a web to FREE (from COALESCED),
224 which itself is a target of aliasing (i.e. in the middle of
225 an alias chain). We can handle this by checking also for
226 type == FREE. Note nevertheless, that alias() is invalid
228 if (aweb
->type
== SPILLED
|| aweb
->type
== FREE
)
231 put_web (web
, INITIAL
);
233 while ((d
= pop_list (&WEBS(SPILLED
))) != NULL
)
234 put_web (DLIST_WEB (d
), FREE
);
235 while ((d
= pop_list (&WEBS(COLORED
))) != NULL
)
236 put_web (DLIST_WEB (d
), INITIAL
);
238 /* All free webs have no conflicts anymore. */
239 for (d
= WEBS(FREE
); d
; d
= d
->next
)
241 struct web
*web
= DLIST_WEB (d
);
242 BITMAP_XFREE (web
->useless_conflicts
);
243 web
->useless_conflicts
= NULL
;
246 /* Sanity check, that we only have free, initial or precolored webs. */
247 for (i
= 0; i
< num_webs
; i
++)
249 struct web
*web
= ID2WEB (i
);
250 if (web
->type
!= INITIAL
&& web
->type
!= FREE
&& web
->type
!= PRECOLORED
)
253 free_dlist (&mv_worklist
);
254 free_dlist (&mv_coalesced
);
255 free_dlist (&mv_constrained
);
256 free_dlist (&mv_frozen
);
257 free_dlist (&mv_active
);
260 /* Similar to put_web(), but add the web to the end of the appropriate
261 list. Additionally TYPE may not be SIMPLIFY. */
264 put_web_at_end (struct web
*web
, enum ra_node_type type
)
266 if (type
== PRECOLORED
)
268 else if (type
== SIMPLIFY
)
270 push_list_end (web
->dlink
, &WEBS(type
));
274 /* Unlink WEB from the list it's currently on (which corresponds to
275 its current type). */
278 remove_web_from_list (struct web
*web
)
280 if (web
->type
== PRECOLORED
)
281 remove_list (web
->dlink
, &WEBS(INITIAL
));
283 remove_list (web
->dlink
, &WEBS(web
->type
));
286 /* Give MOVE the TYPE, and link it into the correct list. */
289 put_move (struct move
*move
, enum move_type type
)
294 push_list (move
->dlink
, &mv_worklist
);
297 push_list (move
->dlink
, &mv_coalesced
);
300 push_list (move
->dlink
, &mv_constrained
);
303 push_list (move
->dlink
, &mv_frozen
);
306 push_list (move
->dlink
, &mv_active
);
314 /* Build the worklists we are going to process. */
317 build_worklists (struct df
*df ATTRIBUTE_UNUSED
)
319 struct dlist
*d
, *d_next
;
320 struct move_list
*ml
;
322 /* If we are not the first pass, put all stackwebs (which are still
323 backed by a new pseudo, but conceptually can stand for a stackslot,
324 i.e. it doesn't really matter if they get a color or not), on
325 the SELECT stack first, those with lowest cost first. This way
326 they will be colored last, so do not constrain the coloring of the
327 normal webs. But still those with the highest count are colored
328 before, i.e. get a color more probable. The use of stackregs is
329 a pure optimization, and all would work, if we used real stackslots
333 unsigned int i
, num
, max_num
;
334 struct web
**order2web
;
335 max_num
= num_webs
- num_subwebs
;
336 order2web
= xmalloc (max_num
* sizeof (order2web
[0]));
337 for (i
= 0, num
= 0; i
< max_num
; i
++)
338 if (id2web
[i
]->regno
>= max_normal_pseudo
)
339 order2web
[num
++] = id2web
[i
];
342 qsort (order2web
, num
, sizeof (order2web
[0]), comp_webs_maxcost
);
343 for (i
= num
- 1;; i
--)
345 struct web
*web
= order2web
[i
];
346 struct conflict_link
*wl
;
347 remove_list (web
->dlink
, &WEBS(INITIAL
));
348 put_web (web
, SELECT
);
349 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
351 struct web
*pweb
= wl
->t
;
352 pweb
->num_conflicts
-= 1 + web
->add_hardregs
;
361 /* For all remaining initial webs, classify them. */
362 for (d
= WEBS(INITIAL
); d
; d
= d_next
)
364 struct web
*web
= DLIST_WEB (d
);
366 if (web
->type
== PRECOLORED
)
369 remove_list (d
, &WEBS(INITIAL
));
370 if (web
->num_conflicts
>= NUM_REGS (web
))
371 put_web (web
, SPILL
);
373 put_web (web
, FREEZE
);
375 put_web (web
, SIMPLIFY
);
378 /* And put all moves on the worklist for iterated coalescing.
379 Note, that if iterated coalescing is off, then wl_moves doesn't
380 contain any moves. */
381 for (ml
= wl_moves
; ml
; ml
= ml
->next
)
384 struct move
*m
= ml
->move
;
385 d
= ra_calloc (sizeof (struct dlist
));
388 put_move (m
, WORKLIST
);
392 /* Enable the active moves, in which WEB takes part, to be processed. */
395 enable_move (struct web
*web
)
397 struct move_list
*ml
;
398 for (ml
= web
->moves
; ml
; ml
= ml
->next
)
399 if (ml
->move
->type
== ACTIVE
)
401 remove_list (ml
->move
->dlink
, &mv_active
);
402 put_move (ml
->move
, WORKLIST
);
406 /* Decrement the degree of node WEB by the amount DEC.
407 Possibly change the type of WEB, if the number of conflicts is
408 now smaller than its freedom. */
411 decrement_degree (struct web
*web
, int dec
)
413 int before
= web
->num_conflicts
;
414 web
->num_conflicts
-= dec
;
415 if (web
->num_conflicts
< NUM_REGS (web
) && before
>= NUM_REGS (web
))
417 struct conflict_link
*a
;
419 for (a
= web
->conflict_list
; a
; a
= a
->next
)
421 struct web
*aweb
= a
->t
;
422 if (aweb
->type
!= SELECT
&& aweb
->type
!= COALESCED
)
425 if (web
->type
!= FREEZE
)
427 remove_web_from_list (web
);
429 put_web (web
, FREEZE
);
431 put_web (web
, SIMPLIFY
);
436 /* Repeatedly simplify the nodes on the simplify worklists. */
443 struct conflict_link
*wl
;
446 /* We try hard to color all the webs resulting from spills first.
447 Without that on register starved machines (x86 e.g) with some live
448 DImode pseudos, -fPIC, and an asm requiring %edx, it might be, that
449 we do rounds over rounds, because the conflict graph says, we can
450 simplify those short webs, but later due to irregularities we can't
451 color those pseudos. So we have to spill them, which in later rounds
452 leads to other spills. */
453 d
= pop_list (&WEBS(SIMPLIFY
));
455 d
= pop_list (&WEBS(SIMPLIFY_FAT
));
457 d
= pop_list (&WEBS(SIMPLIFY_SPILL
));
461 ra_debug_msg (DUMP_PROCESS
, " simplifying web %3d, conflicts = %d\n",
462 web
->id
, web
->num_conflicts
);
463 put_web (web
, SELECT
);
464 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
466 struct web
*pweb
= wl
->t
;
467 if (pweb
->type
!= SELECT
&& pweb
->type
!= COALESCED
)
469 decrement_degree (pweb
, 1 + web
->add_hardregs
);
475 /* Helper function to remove a move from the movelist of the web. */
478 remove_move_1 (struct web
*web
, struct move
*move
)
480 struct move_list
*ml
= web
->moves
;
483 if (ml
->move
== move
)
485 web
->moves
= ml
->next
;
488 for (; ml
->next
&& ml
->next
->move
!= move
; ml
= ml
->next
) ;
491 ml
->next
= ml
->next
->next
;
494 /* Remove a move from the movelist of the web. Actually this is just a
495 wrapper around remove_move_1(), making sure, the removed move really is
496 not in the list anymore. */
499 remove_move (struct web
*web
, struct move
*move
)
501 struct move_list
*ml
;
502 remove_move_1 (web
, move
);
503 for (ml
= web
->moves
; ml
; ml
= ml
->next
)
504 if (ml
->move
== move
)
508 /* Merge the moves for the two webs into the first web's movelist. */
511 merge_moves (struct web
*u
, struct web
*v
)
514 struct move_list
*ml
, *ml_next
;
516 seen
= BITMAP_XMALLOC ();
517 for (ml
= u
->moves
; ml
; ml
= ml
->next
)
518 bitmap_set_bit (seen
, INSN_UID (ml
->move
->insn
));
519 for (ml
= v
->moves
; ml
; ml
= ml_next
)
522 if (! bitmap_bit_p (seen
, INSN_UID (ml
->move
->insn
)))
532 /* Add a web to the simplify worklist, from the freeze worklist. */
535 add_worklist (struct web
*web
)
537 if (web
->type
!= PRECOLORED
&& !web
->moves
538 && web
->num_conflicts
< NUM_REGS (web
))
540 remove_list (web
->dlink
, &WEBS(FREEZE
));
541 put_web (web
, SIMPLIFY
);
545 /* Precolored node coalescing heuristic. */
548 ok (struct web
*target
, struct web
*source
)
550 struct conflict_link
*wl
;
552 int color
= source
->color
;
555 /* Normally one would think, the next test wouldn't be needed.
556 We try to coalesce S and T, and S has already a color, and we checked
557 when processing the insns, that both have the same mode. So naively
558 we could conclude, that of course that mode was valid for this color.
559 Hah. But there is sparc. Before reload there are copy insns
560 (e.g. the ones copying arguments to locals) which happily refer to
561 colors in invalid modes. We can't coalesce those things. */
562 if (! HARD_REGNO_MODE_OK (source
->color
, GET_MODE (target
->orig_x
)))
565 /* Sanity for funny modes. */
566 size
= hard_regno_nregs
[color
][GET_MODE (target
->orig_x
)];
570 /* We can't coalesce target with a precolored register which isn't in
573 if (TEST_HARD_REG_BIT (never_use_colors
, color
+ i
)
574 || !TEST_HARD_REG_BIT (target
->usable_regs
, color
+ i
)
575 /* Before usually calling ok() at all, we already test, if the
576 candidates conflict in sup_igraph. But when wide webs are
577 coalesced to hardregs, we only test the hardweb coalesced into.
578 This is only the begin color. When actually coalescing both,
579 it will also take the following size colors, i.e. their webs.
580 We nowhere checked if the candidate possibly conflicts with
581 one of _those_, which is possible with partial conflicts,
582 so we simply do it here (this does one bit-test more than
583 necessary, the first color). Note, that if X is precolored
584 bit [X*num_webs + Y] can't be set (see add_conflict_edge()). */
585 || TEST_BIT (sup_igraph
,
586 target
->id
* num_webs
+ hardreg2web
[color
+ i
]->id
))
589 for (wl
= target
->conflict_list
; wl
; wl
= wl
->next
)
591 struct web
*pweb
= wl
->t
;
592 if (pweb
->type
== SELECT
|| pweb
->type
== COALESCED
)
595 /* Coalescing target (T) and source (S) is o.k, if for
596 all conflicts C of T it is true, that:
597 1) C will be colored, or
598 2) C is a hardreg (precolored), or
599 3) C already conflicts with S too, or
600 4) a web which contains C conflicts already with S.
601 XXX: we handle here only the special case of 4), that C is
602 a subreg, and the containing thing is the reg itself, i.e.
603 we dont handle the situation, were T conflicts with
604 (subreg:SI x 1), and S conflicts with (subreg:DI x 0), which
605 would be allowed also, as the S-conflict overlaps
607 So, we first test the whole web for any of these conditions, and
608 continue with the next C, if 1, 2 or 3 is true. */
609 if (pweb
->num_conflicts
< NUM_REGS (pweb
)
610 || pweb
->type
== PRECOLORED
611 || TEST_BIT (igraph
, igraph_index (source
->id
, pweb
->id
)) )
614 /* This is reached, if not one of 1, 2 or 3 was true. In the case C has
615 no subwebs, 4 can't be true either, so we can't coalesce S and T. */
620 /* The main webs do _not_ conflict, only some parts of both. This
621 means, that 4 is possibly true, so we need to check this too.
622 For this we go through all sub conflicts between T and C, and see if
623 the target part of C already conflicts with S. When this is not
624 the case we disallow coalescing. */
625 struct sub_conflict
*sl
;
626 for (sl
= wl
->sub
; sl
; sl
= sl
->next
)
628 if (!TEST_BIT (igraph
, igraph_index (source
->id
, sl
->t
->id
)))
636 /* Non-precolored node coalescing heuristic. */
639 conservative (struct web
*target
, struct web
*source
)
644 struct conflict_link
*wl
;
645 unsigned int num_regs
= NUM_REGS (target
); /* XXX */
647 /* k counts the resulting conflict weight, if target and source
648 would be merged, and all low-degree neighbors would be
650 k
= 0 * MAX (target
->add_hardregs
, source
->add_hardregs
);
651 seen
= BITMAP_XMALLOC ();
652 for (loop
= 0; loop
< 2; loop
++)
653 for (wl
= ((loop
== 0) ? target
: source
)->conflict_list
;
656 struct web
*pweb
= wl
->t
;
657 if (pweb
->type
!= SELECT
&& pweb
->type
!= COALESCED
658 && pweb
->num_conflicts
>= NUM_REGS (pweb
)
659 && ! REGNO_REG_SET_P (seen
, pweb
->id
))
661 SET_REGNO_REG_SET (seen
, pweb
->id
);
662 k
+= 1 + pweb
->add_hardregs
;
672 /* If the web is coalesced, return it's alias. Otherwise, return what
676 alias (struct web
*web
)
678 while (web
->type
== COALESCED
)
683 /* Returns nonzero, if the TYPE belongs to one of those representing
686 static inline unsigned int
687 simplify_p (enum ra_node_type type
)
689 return type
== SIMPLIFY
|| type
== SIMPLIFY_SPILL
|| type
== SIMPLIFY_FAT
;
692 /* Actually combine two webs, that can be coalesced. */
695 combine (struct web
*u
, struct web
*v
)
698 struct conflict_link
*wl
;
699 if (u
== v
|| v
->type
== COALESCED
)
701 if ((u
->regno
>= max_normal_pseudo
) != (v
->regno
>= max_normal_pseudo
))
703 remove_web_from_list (v
);
704 put_web (v
, COALESCED
);
708 u
->num_aliased
+= 1 + v
->num_aliased
;
709 if (flag_ra_merge_spill_costs
&& u
->type
!= PRECOLORED
)
710 u
->spill_cost
+= v
->spill_cost
;
711 /*u->spill_cost = MAX (u->spill_cost, v->spill_cost);*/
713 /* combine add_hardregs's of U and V. */
715 for (wl
= v
->conflict_list
; wl
; wl
= wl
->next
)
717 struct web
*pweb
= wl
->t
;
718 /* We don't strictly need to move conflicts between webs which are
719 already coalesced or selected, if we do iterated coalescing, or
720 better if we need not to be able to break aliases again.
721 I.e. normally we would use the condition
722 (pweb->type != SELECT && pweb->type != COALESCED).
723 But for now we simply merge all conflicts. It doesn't take that
728 int nregs
= 1 + v
->add_hardregs
;
729 if (u
->type
== PRECOLORED
)
730 nregs
= hard_regno_nregs
[u
->color
][GET_MODE (v
->orig_x
)];
732 /* For precolored U's we need to make conflicts between V's
733 neighbors and as many hardregs from U as V needed if it gets
734 color U. For now we approximate this by V->add_hardregs, which
735 could be too much in multi-length classes. We should really
736 count how many hardregs are needed for V with color U. When U
737 isn't precolored this loop breaks out after one iteration. */
738 for (i
= 0; i
< nregs
; i
++)
740 if (u
->type
== PRECOLORED
)
741 web
= hardreg2web
[i
+ u
->color
];
743 record_conflict (web
, pweb
);
746 struct sub_conflict
*sl
;
747 /* So, between V and PWEB there are sub_conflicts. We
748 need to relocate those conflicts to be between WEB (==
749 U when it wasn't precolored) and PWEB. In the case
750 only a part of V conflicted with (part of) PWEB we
751 nevertheless make the new conflict between the whole U
752 and the (part of) PWEB. Later we might try to find in
753 U the correct subpart corresponding (by size and
754 offset) to the part of V (sl->s) which was the source
756 for (sl
= wl
->sub
; sl
; sl
= sl
->next
)
758 /* Beware: sl->s is no subweb of web (== U) but of V.
759 We try to search a corresponding subpart of U.
760 If we found none we let it conflict with the whole U.
761 Note that find_subweb() only looks for mode and
762 subreg_byte of the REG rtx but not for the pseudo
763 reg number (otherwise it would be guaranteed to
764 _not_ find any subpart). */
765 struct web
*sweb
= NULL
;
766 if (SUBWEB_P (sl
->s
))
767 sweb
= find_subweb (web
, sl
->s
->orig_x
);
770 record_conflict (sweb
, sl
->t
);
773 if (u
->type
!= PRECOLORED
)
776 if (pweb
->type
!= SELECT
&& pweb
->type
!= COALESCED
)
777 decrement_degree (pweb
, 1 + v
->add_hardregs
);
781 /* Now merge the usable_regs together. */
782 /* XXX That merging might normally make it necessary to
783 adjust add_hardregs, which also means to adjust neighbors. This can
784 result in making some more webs trivially colorable, (or the opposite,
785 if this increases our add_hardregs). Because we intersect the
786 usable_regs it should only be possible to decrease add_hardregs. So a
787 conservative solution for now is to simply don't change it. */
789 AND_HARD_REG_SET (u
->usable_regs
, v
->usable_regs
);
790 u
->regclass
= reg_class_subunion
[u
->regclass
][v
->regclass
];
791 /* Count number of possible hardregs. This might make U a spillweb,
792 but that could also happen, if U and V together had too many
794 u
->num_freedom
= hard_regs_count (u
->usable_regs
);
795 u
->num_freedom
-= u
->add_hardregs
;
796 /* The next would mean an invalid coalesced move (both webs have no
797 possible hardreg in common), so abort. */
801 if (u
->num_conflicts
>= NUM_REGS (u
)
802 && (u
->type
== FREEZE
|| simplify_p (u
->type
)))
804 remove_web_from_list (u
);
808 /* We want the most relaxed combination of spill_temp state.
809 I.e. if any was no spilltemp or a spilltemp2, the result is so too,
810 otherwise if any is short, the result is too. It remains, when both
811 are normal spilltemps. */
812 if (v
->spill_temp
== 0)
814 else if (v
->spill_temp
== 2 && u
->spill_temp
!= 0)
816 else if (v
->spill_temp
== 3 && u
->spill_temp
== 1)
820 /* Attempt to coalesce the first thing on the move worklist.
821 This is used only for iterated coalescing. */
826 struct dlist
*d
= pop_list (&mv_worklist
);
827 struct move
*m
= DLIST_MOVE (d
);
828 struct web
*source
= alias (m
->source_web
);
829 struct web
*target
= alias (m
->target_web
);
831 if (target
->type
== PRECOLORED
)
833 struct web
*h
= source
;
837 if (source
== target
)
839 remove_move (source
, m
);
840 put_move (m
, MV_COALESCED
);
841 add_worklist (source
);
843 else if (target
->type
== PRECOLORED
844 || TEST_BIT (sup_igraph
, source
->id
* num_webs
+ target
->id
)
845 || TEST_BIT (sup_igraph
, target
->id
* num_webs
+ source
->id
)
846 || !ok_class (target
, source
))
848 remove_move (source
, m
);
849 remove_move (target
, m
);
850 put_move (m
, CONSTRAINED
);
851 add_worklist (source
);
852 add_worklist (target
);
854 else if ((source
->type
== PRECOLORED
&& ok (target
, source
))
855 || (source
->type
!= PRECOLORED
856 && conservative (target
, source
)))
858 remove_move (source
, m
);
859 remove_move (target
, m
);
860 put_move (m
, MV_COALESCED
);
861 combine (source
, target
);
862 add_worklist (source
);
865 put_move (m
, ACTIVE
);
868 /* Freeze the moves associated with the web. Used for iterated coalescing. */
871 freeze_moves (struct web
*web
)
873 struct move_list
*ml
, *ml_next
;
874 for (ml
= web
->moves
; ml
; ml
= ml_next
)
876 struct move
*m
= ml
->move
;
877 struct web
*src
, *dest
;
879 if (m
->type
== ACTIVE
)
880 remove_list (m
->dlink
, &mv_active
);
882 remove_list (m
->dlink
, &mv_worklist
);
883 put_move (m
, FROZEN
);
884 remove_move (web
, m
);
885 src
= alias (m
->source_web
);
886 dest
= alias (m
->target_web
);
887 src
= (src
== web
) ? dest
: src
;
888 remove_move (src
, m
);
889 /* XXX GA use the original v, instead of alias(v) */
890 if (!src
->moves
&& src
->num_conflicts
< NUM_REGS (src
))
892 remove_list (src
->dlink
, &WEBS(FREEZE
));
893 put_web (src
, SIMPLIFY
);
898 /* Freeze the first thing on the freeze worklist (only for iterated
904 struct dlist
*d
= pop_list (&WEBS(FREEZE
));
905 put_web (DLIST_WEB (d
), SIMPLIFY
);
906 freeze_moves (DLIST_WEB (d
));
909 /* The current spill heuristic. Returns a number for a WEB.
910 Webs with higher numbers are selected later. */
912 static unsigned HOST_WIDE_INT (*spill_heuristic
) (struct web
*);
914 static unsigned HOST_WIDE_INT
default_spill_heuristic (struct web
*);
916 /* Our default heuristic is similar to spill_cost / num_conflicts.
917 Just scaled for integer arithmetic, and it favors coalesced webs,
918 and webs which span more insns with deaths. */
920 static unsigned HOST_WIDE_INT
921 default_spill_heuristic (struct web
*web
)
923 unsigned HOST_WIDE_INT ret
;
924 unsigned int divisor
= 1;
925 /* Make coalesce targets cheaper to spill, because they will be broken
926 up again into smaller parts. */
927 if (flag_ra_break_aliases
)
928 divisor
+= web
->num_aliased
;
929 divisor
+= web
->num_conflicts
;
930 ret
= ((web
->spill_cost
<< 8) + divisor
- 1) / divisor
;
931 /* It is better to spill webs that span more insns (deaths in our
932 case) than other webs with the otherwise same spill_cost. So make
933 them a little bit cheaper. Remember that spill_cost is unsigned. */
934 if (web
->span_deaths
< ret
)
935 ret
-= web
->span_deaths
;
939 /* Select the cheapest spill to be potentially spilled (we don't
940 *actually* spill until we need to). */
945 unsigned HOST_WIDE_INT best
= (unsigned HOST_WIDE_INT
) -1;
946 struct dlist
*bestd
= NULL
;
947 unsigned HOST_WIDE_INT best2
= (unsigned HOST_WIDE_INT
) -1;
948 struct dlist
*bestd2
= NULL
;
950 for (d
= WEBS(SPILL
); d
; d
= d
->next
)
952 struct web
*w
= DLIST_WEB (d
);
953 unsigned HOST_WIDE_INT cost
= spill_heuristic (w
);
954 if ((!w
->spill_temp
) && cost
< best
)
959 /* Specially marked spill temps can be spilled. Also coalesce
960 targets can. Eventually they will be broken up later in the
961 colorizing process, so if we have nothing better take that. */
962 else if ((w
->spill_temp
== 2 || w
->is_coalesced
) && cost
< best2
)
976 /* Note the potential spill. */
977 DLIST_WEB (bestd
)->was_spilled
= 1;
978 remove_list (bestd
, &WEBS(SPILL
));
979 put_web (DLIST_WEB (bestd
), SIMPLIFY
);
980 freeze_moves (DLIST_WEB (bestd
));
981 ra_debug_msg (DUMP_PROCESS
, " potential spill web %3d, conflicts = %d\n",
982 DLIST_WEB (bestd
)->id
, DLIST_WEB (bestd
)->num_conflicts
);
985 /* Given a set of forbidden colors to begin at, and a set of still
986 free colors, and MODE, returns nonzero of color C is still usable. */
989 color_usable_p (int c
, HARD_REG_SET dont_begin_colors
,
990 HARD_REG_SET free_colors
, enum machine_mode mode
)
992 if (!TEST_HARD_REG_BIT (dont_begin_colors
, c
)
993 && TEST_HARD_REG_BIT (free_colors
, c
)
994 && HARD_REGNO_MODE_OK (c
, mode
))
997 size
= hard_regno_nregs
[c
][mode
];
998 for (i
= 1; i
< size
&& TEST_HARD_REG_BIT (free_colors
, c
+ i
); i
++);
1005 /* I don't want to clutter up the actual code with ifdef's. */
1006 #ifdef REG_ALLOC_ORDER
1007 #define INV_REG_ALLOC_ORDER(c) inv_reg_alloc_order[c]
1009 #define INV_REG_ALLOC_ORDER(c) c
1012 /* Searches in FREE_COLORS for a block of hardregs of the right length
1013 for MODE, which doesn't begin at a hardreg mentioned in DONT_BEGIN_COLORS.
1014 If it needs more than one hardreg it prefers blocks beginning
1015 at an even hardreg, and only gives an odd begin reg if no other
1016 block could be found. */
1019 get_free_reg (HARD_REG_SET dont_begin_colors
, HARD_REG_SET free_colors
,
1020 enum machine_mode mode
)
1023 int last_resort_reg
= -1;
1025 int pref_reg_order
= INT_MAX
;
1026 int last_resort_reg_order
= INT_MAX
;
1028 for (c
= 0; c
< FIRST_PSEUDO_REGISTER
; c
++)
1029 if (!TEST_HARD_REG_BIT (dont_begin_colors
, c
)
1030 && TEST_HARD_REG_BIT (free_colors
, c
)
1031 && HARD_REGNO_MODE_OK (c
, mode
))
1034 size
= hard_regno_nregs
[c
][mode
];
1035 for (i
= 1; i
< size
&& TEST_HARD_REG_BIT (free_colors
, c
+ i
); i
++);
1043 if (size
< 2 || (c
& 1) == 0)
1045 if (INV_REG_ALLOC_ORDER (c
) < pref_reg_order
)
1048 pref_reg_order
= INV_REG_ALLOC_ORDER (c
);
1051 else if (INV_REG_ALLOC_ORDER (c
) < last_resort_reg_order
)
1053 last_resort_reg
= c
;
1054 last_resort_reg_order
= INV_REG_ALLOC_ORDER (c
);
1060 return pref_reg
>= 0 ? pref_reg
: last_resort_reg
;
1063 /* Similar to get_free_reg(), but first search in colors provided
1064 by BIAS _and_ PREFER_COLORS, then in BIAS alone, then in PREFER_COLORS
1065 alone, and only then for any free color. If flag_ra_biased is zero
1066 only do the last two steps. */
1069 get_biased_reg (HARD_REG_SET dont_begin_colors
, HARD_REG_SET bias
,
1070 HARD_REG_SET prefer_colors
, HARD_REG_SET free_colors
,
1071 enum machine_mode mode
)
1077 COPY_HARD_REG_SET (s
, dont_begin_colors
);
1078 IOR_COMPL_HARD_REG_SET (s
, bias
);
1079 IOR_COMPL_HARD_REG_SET (s
, prefer_colors
);
1080 c
= get_free_reg (s
, free_colors
, mode
);
1083 COPY_HARD_REG_SET (s
, dont_begin_colors
);
1084 IOR_COMPL_HARD_REG_SET (s
, bias
);
1085 c
= get_free_reg (s
, free_colors
, mode
);
1089 COPY_HARD_REG_SET (s
, dont_begin_colors
);
1090 IOR_COMPL_HARD_REG_SET (s
, prefer_colors
);
1091 c
= get_free_reg (s
, free_colors
, mode
);
1094 c
= get_free_reg (dont_begin_colors
, free_colors
, mode
);
1098 /* Counts the number of non-overlapping bitblocks of length LEN
1102 count_long_blocks (HARD_REG_SET free_colors
, int len
)
1106 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1108 if (!TEST_HARD_REG_BIT (free_colors
, i
))
1110 for (j
= 1; j
< len
; j
++)
1111 if (!TEST_HARD_REG_BIT (free_colors
, i
+ j
))
1113 /* Bits [i .. i+j-1] are free. */
1121 /* Given a hardreg set S, return a string representing it.
1122 Either as 0/1 string, or as hex value depending on the implementation
1123 of hardreg sets. Note that this string is statically allocated. */
1126 hardregset_to_string (HARD_REG_SET s
)
1128 static char string
[/*FIRST_PSEUDO_REGISTER + 30*/1024];
1129 #if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDE_INT
1130 sprintf (string
, HOST_WIDE_INT_PRINT_HEX
, s
);
1134 c
+= sprintf (c
, "{ ");
1135 for (i
= 0;i
< HARD_REG_SET_LONGS
; i
++)
1137 for (j
= 0; j
< HOST_BITS_PER_WIDE_INT
; j
++)
1138 c
+= sprintf (c
, "%s", ( 1 << j
) & s
[i
] ? "1" : "0");
1139 c
+= sprintf (c
, "%s", i
? ", " : "");
1141 c
+= sprintf (c
, " }");
1146 /* For WEB, look at its already colored neighbors, and calculate
1147 the set of hardregs which is not allowed as color for WEB. Place
1148 that set int *RESULT. Note that the set of forbidden begin colors
1149 is not the same as all colors taken up by neighbors. E.g. suppose
1150 two DImode webs, but only the lo-part from one conflicts with the
1151 hipart from the other, and suppose the other gets colors 2 and 3
1152 (it needs two SImode hardregs). Now the first can take also color
1153 1 or 2, although in those cases there's a partial overlap. Only
1154 3 can't be used as begin color. */
1157 calculate_dont_begin (struct web
*web
, HARD_REG_SET
*result
)
1159 struct conflict_link
*wl
;
1160 HARD_REG_SET dont_begin
;
1161 /* The bits set in dont_begin correspond to the hardregs, at which
1162 WEB may not begin. This differs from the set of _all_ hardregs which
1163 are taken by WEB's conflicts in the presence of wide webs, where only
1164 some parts conflict with others. */
1165 CLEAR_HARD_REG_SET (dont_begin
);
1166 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1169 struct web
*ptarget
= alias (wl
->t
);
1170 struct sub_conflict
*sl
= wl
->sub
;
1171 w
= sl
? sl
->t
: wl
->t
;
1174 if (ptarget
->type
== COLORED
|| ptarget
->type
== PRECOLORED
)
1176 struct web
*source
= (sl
) ? sl
->s
: web
;
1177 unsigned int tsize
= hard_regno_nregs
[ptarget
->color
]
1178 [GET_MODE (w
->orig_x
)];
1179 /* ssize is only a first guess for the size. */
1180 unsigned int ssize
= hard_regno_nregs
[ptarget
->color
][GET_MODE
1182 unsigned int tofs
= 0;
1183 unsigned int sofs
= 0;
1184 /* C1 and C2 can become negative, so unsigned
1189 && GET_MODE_SIZE (GET_MODE (w
->orig_x
)) >= UNITS_PER_WORD
)
1190 tofs
= (SUBREG_BYTE (w
->orig_x
) / UNITS_PER_WORD
);
1191 if (SUBWEB_P (source
)
1192 && GET_MODE_SIZE (GET_MODE (source
->orig_x
))
1194 sofs
= (SUBREG_BYTE (source
->orig_x
) / UNITS_PER_WORD
);
1195 c1
= ptarget
->color
+ tofs
- sofs
- ssize
+ 1;
1196 c2
= ptarget
->color
+ tofs
+ tsize
- 1 - sofs
;
1201 /* Because ssize was only guessed above, which influenced our
1202 begin color (c1), we need adjustment, if for that color
1203 another size would be needed. This is done by moving
1204 c1 to a place, where the last of sources hardregs does not
1205 overlap the first of targets colors. */
1207 + hard_regno_nregs
[c1
][GET_MODE (source
->orig_x
)] - 1
1208 < ptarget
->color
+ tofs
)
1210 while (c1
> 0 && c1
+ sofs
1211 + hard_regno_nregs
[c1
][GET_MODE (source
->orig_x
)] - 1
1212 > ptarget
->color
+ tofs
)
1214 for (; c1
<= c2
; c1
++)
1215 SET_HARD_REG_BIT (dont_begin
, c1
);
1218 /* The next if() only gets true, if there was no wl->sub at all, in
1219 which case we are only making one go through this loop with W being
1224 w
= sl
? sl
->t
: NULL
;
1227 COPY_HARD_REG_SET (*result
, dont_begin
);
1230 /* Try to assign a color to WEB. If HARD if nonzero, we try many
1231 tricks to get it one color, including respilling already colored
1234 We also trie very hard, to not constrain the uncolored non-spill
1235 neighbors, which need more hardregs than we. Consider a situation, 2
1236 hardregs free for us (0 and 1), and one of our neighbors needs 2
1237 hardregs, and only conflicts with us. There are 3 hardregs at all. Now
1238 a simple minded method might choose 1 as color for us. Then our neighbor
1239 has two free colors (0 and 2) as it should, but they are not consecutive,
1240 so coloring it later would fail. This leads to nasty problems on
1241 register starved machines, so we try to avoid this. */
1244 colorize_one_web (struct web
*web
, int hard
)
1246 struct conflict_link
*wl
;
1247 HARD_REG_SET colors
, dont_begin
;
1250 int neighbor_needs
= 0;
1251 struct web
*fats_parent
= NULL
;
1253 int long_blocks
= 0;
1254 int best_long_blocks
= -1;
1255 HARD_REG_SET fat_colors
;
1258 CLEAR_HARD_REG_SET (fat_colors
);
1260 if (web
->regno
>= max_normal_pseudo
)
1263 /* First we want to know the colors at which we can't begin. */
1264 calculate_dont_begin (web
, &dont_begin
);
1265 CLEAR_HARD_REG_SET (bias
);
1267 /* Now setup the set of colors used by our neighbors neighbors,
1268 and search the biggest noncolored neighbor. */
1269 neighbor_needs
= web
->add_hardregs
+ 1;
1270 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1273 struct web
*ptarget
= alias (wl
->t
);
1274 struct sub_conflict
*sl
= wl
->sub
;
1275 IOR_HARD_REG_SET (bias
, ptarget
->bias_colors
);
1276 w
= sl
? sl
->t
: wl
->t
;
1277 if (ptarget
->type
!= COLORED
&& ptarget
->type
!= PRECOLORED
1278 && !ptarget
->was_spilled
)
1281 if (find_web_for_subweb (w
)->type
!= COALESCED
1282 && w
->add_hardregs
>= neighbor_needs
)
1284 neighbor_needs
= w
->add_hardregs
;
1285 fats_parent
= ptarget
;
1291 w
= sl
? sl
->t
: NULL
;
1295 ra_debug_msg (DUMP_COLORIZE
, "colorize web %d [don't begin at %s]", web
->id
,
1296 hardregset_to_string (dont_begin
));
1298 /* If there are some fat neighbors, remember their usable regs,
1299 and how many blocks are free in it for that neighbor. */
1302 COPY_HARD_REG_SET (fat_colors
, fats_parent
->usable_regs
);
1303 long_blocks
= count_long_blocks (fat_colors
, neighbor_needs
+ 1);
1306 /* We break out, if we found a color which doesn't constrain
1307 neighbors, or if we can't find any colors. */
1310 HARD_REG_SET call_clobbered
;
1312 /* Here we choose a hard-reg for the current web. For non spill
1313 temporaries we first search in the hardregs for it's preferred
1314 class, then, if we found nothing appropriate, in those of the
1315 alternate class. For spill temporaries we only search in
1316 usable_regs of this web (which is probably larger than that of
1317 the preferred or alternate class). All searches first try to
1318 find a non-call-clobbered hard-reg.
1319 XXX this should be more finegraned... First look into preferred
1320 non-callclobbered hardregs, then _if_ the web crosses calls, in
1321 alternate non-cc hardregs, and only _then_ also in preferred cc
1322 hardregs (and alternate ones). Currently we don't track the number
1323 of calls crossed for webs. We should. */
1324 if (web
->use_my_regs
)
1326 COPY_HARD_REG_SET (colors
, web
->usable_regs
);
1327 AND_HARD_REG_SET (colors
,
1328 usable_regs
[reg_preferred_class (web
->regno
)]);
1331 COPY_HARD_REG_SET (colors
,
1332 usable_regs
[reg_preferred_class (web
->regno
)]);
1333 #ifdef CANNOT_CHANGE_MODE_CLASS
1334 if (web
->mode_changed
)
1335 AND_COMPL_HARD_REG_SET (colors
, invalid_mode_change_regs
);
1337 COPY_HARD_REG_SET (call_clobbered
, colors
);
1338 AND_HARD_REG_SET (call_clobbered
, call_used_reg_set
);
1340 /* If this web got a color in the last pass, try to give it the
1341 same color again. This will to much better colorization
1342 down the line, as we spilled for a certain coloring last time. */
1345 c
= web
->old_color
- 1;
1346 if (!color_usable_p (c
, dont_begin
, colors
,
1347 PSEUDO_REGNO_MODE (web
->regno
)))
1353 c
= get_biased_reg (dont_begin
, bias
, web
->prefer_colors
,
1354 call_clobbered
, PSEUDO_REGNO_MODE (web
->regno
));
1356 c
= get_biased_reg (dont_begin
, bias
, web
->prefer_colors
,
1357 colors
, PSEUDO_REGNO_MODE (web
->regno
));
1361 if (web
->use_my_regs
)
1362 IOR_HARD_REG_SET (colors
, web
->usable_regs
);
1364 IOR_HARD_REG_SET (colors
, usable_regs
1365 [reg_alternate_class (web
->regno
)]);
1366 #ifdef CANNOT_CHANGE_MODE_CLASS
1367 if (web
->mode_changed
)
1368 AND_COMPL_HARD_REG_SET (colors
, invalid_mode_change_regs
);
1370 COPY_HARD_REG_SET (call_clobbered
, colors
);
1371 AND_HARD_REG_SET (call_clobbered
, call_used_reg_set
);
1373 c
= get_biased_reg (dont_begin
, bias
, web
->prefer_colors
,
1374 call_clobbered
, PSEUDO_REGNO_MODE (web
->regno
));
1376 c
= get_biased_reg (dont_begin
, bias
, web
->prefer_colors
,
1377 colors
, PSEUDO_REGNO_MODE (web
->regno
));
1383 /* If one of the yet uncolored neighbors, which is not a potential
1384 spill needs a block of hardregs be sure, not to destroy such a block
1385 by coloring one reg in the middle. */
1390 HARD_REG_SET colors1
;
1391 COPY_HARD_REG_SET (colors1
, fat_colors
);
1392 for (i
= 0; i
< 1 + web
->add_hardregs
; i
++)
1393 CLEAR_HARD_REG_BIT (colors1
, c
+ i
);
1394 new_long
= count_long_blocks (colors1
, neighbor_needs
+ 1);
1395 /* If we changed the number of long blocks, and it's now smaller
1396 than needed, we try to avoid this color. */
1397 if (long_blocks
!= new_long
&& new_long
< num_fat
)
1399 if (new_long
> best_long_blocks
)
1401 best_long_blocks
= new_long
;
1404 SET_HARD_REG_BIT (dont_begin
, c
);
1405 ra_debug_msg (DUMP_COLORIZE
, " avoid %d", c
);
1408 /* We found a color which doesn't destroy a block. */
1411 /* If we havee no fat neighbors, the current color won't become
1412 "better", so we've found it. */
1416 ra_debug_msg (DUMP_COLORIZE
, " --> got %d", c
< 0 ? bestc
: c
);
1417 if (bestc
>= 0 && c
< 0 && !web
->was_spilled
)
1419 /* This is a non-potential-spill web, which got a color, which did
1420 destroy a hardreg block for one of it's neighbors. We color
1421 this web anyway and hope for the best for the neighbor, if we are
1423 if (1 || web
->spill_temp
)
1425 ra_debug_msg (DUMP_COLORIZE
, " [constrains neighbors]");
1427 ra_debug_msg (DUMP_COLORIZE
, "\n");
1431 /* Guard against a simplified node being spilled. */
1432 /* Don't abort. This can happen, when e.g. enough registers
1433 are available in colors, but they are not consecutive. This is a
1434 very serious issue if this web is a short live one, because
1435 even if we spill this one here, the situation won't become better
1436 in the next iteration. It probably will have the same conflicts,
1437 those will have the same colors, and we would come here again, for
1438 all parts, in which this one gets split by the spill. This
1439 can result in endless iteration spilling the same register again and
1440 again. That's why we try to find a neighbor, which spans more
1441 instructions that ourself, and got a color, and try to spill _that_.
1443 if (DLIST_WEB (d)->was_spilled < 0)
1445 if (hard
&& (!web
->was_spilled
|| web
->spill_temp
))
1448 struct web
*try = NULL
;
1449 struct web
*candidates
[8];
1451 ra_debug_msg (DUMP_COLORIZE
, " *** %d spilled, although %s ***\n",
1452 web
->id
, web
->spill_temp
? "spilltemp" : "non-spill");
1453 /* We make multiple passes over our conflicts, first trying to
1454 spill those webs, which only got a color by chance, but
1455 were potential spill ones, and if that isn't enough, in a second
1456 pass also to spill normal colored webs. If we still didn't find
1457 a candidate, but we are a spill-temp, we make a third pass
1458 and include also webs, which were targets for coalescing, and
1460 memset (candidates
, 0, sizeof candidates
);
1461 #define set_cand(i, w) \
1463 if (!candidates[(i)] \
1464 || (candidates[(i)]->spill_cost < (w)->spill_cost)) \
1465 candidates[(i)] = (w); \
1467 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1469 struct web
*w
= wl
->t
;
1470 struct web
*aw
= alias (w
);
1471 /* If we are a spill-temp, we also look at webs coalesced
1472 to precolored ones. Otherwise we only look at webs which
1473 themselves were colored, or coalesced to one. */
1474 if (aw
->type
== PRECOLORED
&& w
!= aw
&& web
->spill_temp
1475 && flag_ra_optimistic_coalescing
)
1479 else if (web
->spill_temp
== 2
1480 && w
->spill_temp
== 2
1481 && w
->spill_cost
< web
->spill_cost
)
1483 else if (web
->spill_temp
!= 2
1484 && (w
->spill_temp
== 2
1485 || w
->spill_cost
< web
->spill_cost
))
1489 if (aw
->type
!= COLORED
)
1491 if (w
->type
== COLORED
&& !w
->spill_temp
&& !w
->is_coalesced
1494 if (w
->spill_cost
< web
->spill_cost
)
1496 else if (web
->spill_temp
)
1499 if (w
->type
== COLORED
&& !w
->spill_temp
&& !w
->is_coalesced
1502 if (w
->spill_cost
< web
->spill_cost
)
1504 else if (web
->spill_temp
&& web
->spill_temp
!= 2)
1507 if (web
->spill_temp
)
1509 if (w
->type
== COLORED
&& w
->spill_temp
== 2
1511 && (w
->spill_cost
< web
->spill_cost
1512 || web
->spill_temp
!= 2))
1514 if (!aw
->spill_temp
)
1516 if (aw
->spill_temp
== 2
1517 && (aw
->spill_cost
< web
->spill_cost
1518 || web
->spill_temp
!= 2))
1520 /* For boehm-gc/misc.c. If we are a difficult spilltemp,
1521 also coalesced neighbors are a chance, _even_ if they
1522 too are spilltemps. At least their coalescing can be
1523 broken up, which may be reset usable_regs, and makes
1524 it easier colorable. */
1525 if (web
->spill_temp
!= 2 && aw
->is_coalesced
1526 && flag_ra_optimistic_coalescing
)
1530 for (loop
= 0; try == NULL
&& loop
< 8; loop
++)
1531 if (candidates
[loop
])
1532 try = candidates
[loop
];
1536 int old_c
= try->color
;
1537 if (try->type
== COALESCED
)
1539 if (alias (try)->type
!= PRECOLORED
)
1541 ra_debug_msg (DUMP_COLORIZE
, " breaking alias %d -> %d\n",
1542 try->id
, alias (try)->id
);
1543 break_precolored_alias (try);
1544 colorize_one_web (web
, hard
);
1548 remove_list (try->dlink
, &WEBS(COLORED
));
1549 put_web (try, SPILLED
);
1550 /* Now try to colorize us again. Can recursively make other
1551 webs also spill, until there are no more unspilled
1553 ra_debug_msg (DUMP_COLORIZE
, " trying to spill %d\n", try->id
);
1554 colorize_one_web (web
, hard
);
1555 if (web
->type
!= COLORED
)
1557 /* We tried recursively to spill all already colored
1558 neighbors, but we are still uncolorable. So it made
1559 no sense to spill those neighbors. Recolor them. */
1560 remove_list (try->dlink
, &WEBS(SPILLED
));
1561 put_web (try, COLORED
);
1563 ra_debug_msg (DUMP_COLORIZE
,
1564 " spilling %d was useless\n", try->id
);
1568 ra_debug_msg (DUMP_COLORIZE
,
1569 " to spill %d was a good idea\n",
1571 remove_list (try->dlink
, &WEBS(SPILLED
));
1572 if (try->was_spilled
)
1573 colorize_one_web (try, 0);
1575 colorize_one_web (try, hard
- 1);
1580 /* No more chances to get a color, so give up hope and
1582 put_web (web
, SPILLED
);
1585 put_web (web
, SPILLED
);
1589 put_web (web
, COLORED
);
1593 int nregs
= hard_regno_nregs
[c
][GET_MODE (web
->orig_x
)];
1594 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1596 struct web
*ptarget
= alias (wl
->t
);
1598 for (i
= 0; i
< nregs
; i
++)
1599 SET_HARD_REG_BIT (ptarget
->bias_colors
, c
+ i
);
1603 if (web
->regno
>= max_normal_pseudo
&& web
->type
== SPILLED
)
1605 web
->color
= an_unusable_color
;
1606 remove_list (web
->dlink
, &WEBS(SPILLED
));
1607 put_web (web
, COLORED
);
1609 if (web
->type
== SPILLED
&& flag_ra_optimistic_coalescing
1610 && web
->is_coalesced
)
1612 ra_debug_msg (DUMP_COLORIZE
, "breaking aliases to web %d:", web
->id
);
1613 restore_conflicts_from_coalesce (web
);
1614 break_aliases_to_web (web
);
1615 insert_coalesced_conflicts ();
1616 ra_debug_msg (DUMP_COLORIZE
, "\n");
1617 remove_list (web
->dlink
, &WEBS(SPILLED
));
1618 put_web (web
, SELECT
);
1623 /* Assign the colors to all nodes on the select stack. And update the
1624 colors of coalesced webs. */
1627 assign_colors (void)
1631 while (WEBS(SELECT
))
1633 d
= pop_list (&WEBS(SELECT
));
1634 colorize_one_web (DLIST_WEB (d
), 1);
1637 for (d
= WEBS(COALESCED
); d
; d
= d
->next
)
1639 struct web
*a
= alias (DLIST_WEB (d
));
1640 DLIST_WEB (d
)->color
= a
->color
;
1644 /* WEB is a spilled web. Look if we can improve the cost of the graph,
1645 by coloring WEB, even if we then need to spill some of it's neighbors.
1646 For this we calculate the cost for each color C, that results when we
1647 _would_ give WEB color C (i.e. the cost of the then spilled neighbors).
1648 If the lowest cost among them is smaller than the spillcost of WEB, we
1649 do that recoloring, and instead spill the neighbors.
1651 This can sometime help, when due to irregularities in register file,
1652 and due to multi word pseudos, the colorization is suboptimal. But
1653 be aware, that currently this pass is quite slow. */
1656 try_recolor_web (struct web
*web
)
1658 struct conflict_link
*wl
;
1659 unsigned HOST_WIDE_INT
*cost_neighbors
;
1660 unsigned int *min_color
;
1662 HARD_REG_SET precolored_neighbors
, spill_temps
;
1663 HARD_REG_SET possible_begin
, wide_seen
;
1664 cost_neighbors
= xcalloc (FIRST_PSEUDO_REGISTER
, sizeof (cost_neighbors
[0]));
1665 /* For each hard-regs count the number of preceding hardregs, which
1666 would overlap this color, if used in WEB's mode. */
1667 min_color
= xcalloc (FIRST_PSEUDO_REGISTER
, sizeof (int));
1668 CLEAR_HARD_REG_SET (possible_begin
);
1669 for (c
= 0; c
< FIRST_PSEUDO_REGISTER
; c
++)
1672 if (!HARD_REGNO_MODE_OK (c
, GET_MODE (web
->orig_x
)))
1674 nregs
= hard_regno_nregs
[c
][GET_MODE (web
->orig_x
)];
1675 for (i
= 0; i
< nregs
; i
++)
1676 if (!TEST_HARD_REG_BIT (web
->usable_regs
, c
+ i
))
1678 if (i
< nregs
|| nregs
== 0)
1680 SET_HARD_REG_BIT (possible_begin
, c
);
1682 if (!min_color
[c
+ nregs
])
1683 min_color
[c
+ nregs
] = 1 + c
;
1685 CLEAR_HARD_REG_SET (precolored_neighbors
);
1686 CLEAR_HARD_REG_SET (spill_temps
);
1687 CLEAR_HARD_REG_SET (wide_seen
);
1688 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1690 HARD_REG_SET dont_begin
;
1691 struct web
*web2
= alias (wl
->t
);
1692 struct conflict_link
*nn
;
1695 if (wl
->t
->type
== COALESCED
|| web2
->type
!= COLORED
)
1697 if (web2
->type
== PRECOLORED
)
1699 c1
= min_color
[web2
->color
];
1700 c1
= (c1
== 0) ? web2
->color
: (c1
- 1);
1702 for (; c1
<= c2
; c1
++)
1703 SET_HARD_REG_BIT (precolored_neighbors
, c1
);
1707 /* Mark colors for which some wide webs are involved. For
1708 those the independent sets are not simply one-node graphs, so
1709 they can't be recolored independent from their neighborhood. This
1710 means, that our cost calculation can be incorrect (assuming it
1711 can avoid spilling a web because it thinks some colors are available,
1712 although it's neighbors which itself need recoloring might take
1713 away exactly those colors). */
1714 if (web2
->add_hardregs
)
1716 for (nn
= web2
->conflict_list
; nn
&& !wide_p
; nn
= nn
->next
)
1717 if (alias (nn
->t
)->add_hardregs
)
1719 calculate_dont_begin (web2
, &dont_begin
);
1720 c1
= min_color
[web2
->color
];
1721 /* Note that min_color[] contains 1-based values (zero means
1723 c1
= c1
== 0 ? web2
->color
: (c1
- 1);
1724 c2
= web2
->color
+ hard_regno_nregs
[web2
->color
][GET_MODE
1725 (web2
->orig_x
)] - 1;
1726 for (; c1
<= c2
; c1
++)
1727 if (TEST_HARD_REG_BIT (possible_begin
, c1
))
1730 HARD_REG_SET colors
;
1731 nregs
= hard_regno_nregs
[c1
][GET_MODE (web
->orig_x
)];
1732 COPY_HARD_REG_SET (colors
, web2
->usable_regs
);
1734 CLEAR_HARD_REG_BIT (colors
, c1
+ nregs
);
1736 SET_HARD_REG_BIT (wide_seen
, c1
);
1737 if (get_free_reg (dont_begin
, colors
,
1738 GET_MODE (web2
->orig_x
)) < 0)
1740 if (web2
->spill_temp
)
1741 SET_HARD_REG_BIT (spill_temps
, c1
);
1743 cost_neighbors
[c1
] += web2
->spill_cost
;
1748 for (c
= 0; c
< FIRST_PSEUDO_REGISTER
; c
++)
1749 if (TEST_HARD_REG_BIT (possible_begin
, c
)
1750 && !TEST_HARD_REG_BIT (precolored_neighbors
, c
)
1751 && !TEST_HARD_REG_BIT (spill_temps
, c
)
1753 || cost_neighbors
[c
] < cost_neighbors
[newcol
]))
1755 if (newcol
>= 0 && cost_neighbors
[newcol
] < web
->spill_cost
)
1757 int nregs
= hard_regno_nregs
[newcol
][GET_MODE (web
->orig_x
)];
1758 unsigned HOST_WIDE_INT cost
= 0;
1760 struct conflict_link
*wl_next
;
1761 ra_debug_msg (DUMP_COLORIZE
, "try to set web %d to color %d\n", web
->id
,
1763 remove_list (web
->dlink
, &WEBS(SPILLED
));
1764 put_web (web
, COLORED
);
1765 web
->color
= newcol
;
1766 old_colors
= xcalloc (num_webs
, sizeof (int));
1767 for (wl
= web
->conflict_list
; wl
; wl
= wl_next
)
1769 struct web
*web2
= alias (wl
->t
);
1770 /* If web2 is a coalesce-target, and will become spilled
1771 below in colorize_one_web(), and the current conflict wl
1772 between web and web2 was only the result of that coalescing
1773 this conflict will be deleted, making wl invalid. So save
1774 the next conflict right now. Note that if web2 has indeed
1775 such state, then wl->next can not be deleted in this
1778 if (web2
->type
== COLORED
)
1780 int nregs2
= hard_regno_nregs
[web2
->color
][GET_MODE
1782 if (web
->color
>= web2
->color
+ nregs2
1783 || web2
->color
>= web
->color
+ nregs
)
1785 old_colors
[web2
->id
] = web2
->color
+ 1;
1787 remove_list (web2
->dlink
, &WEBS(COLORED
));
1788 web2
->type
= SELECT
;
1789 /* Allow webs to be spilled. */
1790 if (web2
->spill_temp
== 0 || web2
->spill_temp
== 2)
1791 web2
->was_spilled
= 1;
1792 colorize_one_web (web2
, 1);
1793 if (web2
->type
== SPILLED
)
1794 cost
+= web2
->spill_cost
;
1797 /* The actual cost may be smaller than the guessed one, because
1798 partial conflicts could result in some conflicting webs getting
1799 a color, where we assumed it must be spilled. See the comment
1800 above what happens, when wide webs are involved, and why in that
1801 case there might actually be some webs spilled although thought to
1803 if (cost
> cost_neighbors
[newcol
]
1804 && nregs
== 1 && !TEST_HARD_REG_BIT (wide_seen
, newcol
))
1806 /* But if the new spill-cost is higher than our own, then really loose.
1807 Respill us and recolor neighbors as before. */
1808 if (cost
> web
->spill_cost
)
1810 ra_debug_msg (DUMP_COLORIZE
,
1811 "reset coloring of web %d, too expensive\n", web
->id
);
1812 remove_list (web
->dlink
, &WEBS(COLORED
));
1814 put_web (web
, SPILLED
);
1815 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1817 struct web
*web2
= alias (wl
->t
);
1818 if (old_colors
[web2
->id
])
1820 if (web2
->type
== SPILLED
)
1822 remove_list (web2
->dlink
, &WEBS(SPILLED
));
1823 web2
->color
= old_colors
[web2
->id
] - 1;
1824 put_web (web2
, COLORED
);
1826 else if (web2
->type
== COLORED
)
1827 web2
->color
= old_colors
[web2
->id
] - 1;
1828 else if (web2
->type
== SELECT
)
1829 /* This means, that WEB2 once was a part of a coalesced
1830 web, which got spilled in the above colorize_one_web()
1831 call, and whose parts then got split and put back
1832 onto the SELECT stack. As the cause for that splitting
1833 (the coloring of WEB) was worthless, we should again
1834 coalesce the parts, as they were before. For now we
1835 simply leave them SELECTed, for our caller to take
1846 free (cost_neighbors
);
1849 /* This ensures that all conflicts of coalesced webs are seen from
1850 the webs coalesced into. combine() only adds the conflicts which
1851 at the time of combining were not already SELECTed or COALESCED
1852 to not destroy num_conflicts. Here we add all remaining conflicts
1853 and thereby destroy num_conflicts. This should be used when num_conflicts
1854 isn't used anymore, e.g. on a completely colored graph. */
1857 insert_coalesced_conflicts (void)
1860 for (d
= WEBS(COALESCED
); 0 && d
; d
= d
->next
)
1862 struct web
*web
= DLIST_WEB (d
);
1863 struct web
*aweb
= alias (web
);
1864 struct conflict_link
*wl
;
1865 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
1867 struct web
*tweb
= aweb
;
1869 int nregs
= 1 + web
->add_hardregs
;
1870 if (aweb
->type
== PRECOLORED
)
1871 nregs
= hard_regno_nregs
[aweb
->color
][GET_MODE (web
->orig_x
)];
1872 for (i
= 0; i
< nregs
; i
++)
1874 if (aweb
->type
== PRECOLORED
)
1875 tweb
= hardreg2web
[i
+ aweb
->color
];
1876 /* There might be some conflict edges laying around
1877 where the usable_regs don't intersect. This can happen
1878 when first some webs were coalesced and conflicts
1879 propagated, then some combining narrowed usable_regs and
1880 further coalescing ignored those conflicts. Now there are
1881 some edges to COALESCED webs but not to it's alias.
1882 So abort only when they really should conflict. */
1883 if ((!(tweb
->type
== PRECOLORED
1884 || TEST_BIT (sup_igraph
, tweb
->id
* num_webs
+ wl
->t
->id
))
1885 || !(wl
->t
->type
== PRECOLORED
1886 || TEST_BIT (sup_igraph
,
1887 wl
->t
->id
* num_webs
+ tweb
->id
)))
1888 && hard_regs_intersect_p (&tweb
->usable_regs
,
1889 &wl
->t
->usable_regs
))
1891 /*if (wl->sub == NULL)
1892 record_conflict (tweb, wl->t);
1895 struct sub_conflict *sl;
1896 for (sl = wl->sub; sl; sl = sl->next)
1897 record_conflict (tweb, sl->t);
1899 if (aweb
->type
!= PRECOLORED
)
1906 /* A function suitable to pass to qsort(). Compare the spill costs
1907 of webs W1 and W2. When used by qsort, this would order webs with
1908 largest cost first. */
1911 comp_webs_maxcost (const void *w1
, const void *w2
)
1913 struct web
*web1
= *(struct web
**)w1
;
1914 struct web
*web2
= *(struct web
**)w2
;
1915 if (web1
->spill_cost
> web2
->spill_cost
)
1917 else if (web1
->spill_cost
< web2
->spill_cost
)
1923 /* This tries to recolor all spilled webs. See try_recolor_web()
1924 how this is done. This just calls it for each spilled web. */
1927 recolor_spills (void)
1929 unsigned int i
, num
;
1930 struct web
**order2web
;
1931 num
= num_webs
- num_subwebs
;
1932 order2web
= xmalloc (num
* sizeof (order2web
[0]));
1933 for (i
= 0; i
< num
; i
++)
1935 order2web
[i
] = id2web
[i
];
1936 /* If we aren't breaking aliases, combine() wasn't merging the
1937 spill_costs. So do that here to have sane measures. */
1938 if (!flag_ra_merge_spill_costs
&& id2web
[i
]->type
== COALESCED
)
1939 alias (id2web
[i
])->spill_cost
+= id2web
[i
]->spill_cost
;
1941 qsort (order2web
, num
, sizeof (order2web
[0]), comp_webs_maxcost
);
1942 insert_coalesced_conflicts ();
1943 dump_graph_cost (DUMP_COSTS
, "before spill-recolor");
1944 for (i
= 0; i
< num
; i
++)
1946 struct web
*web
= order2web
[i
];
1947 if (web
->type
== SPILLED
)
1948 try_recolor_web (web
);
1950 /* It might have been decided in try_recolor_web() (in colorize_one_web())
1951 that a coalesced web should be spilled, so it was put on the
1952 select stack. Those webs need recoloring again, and all remaining
1953 coalesced webs might need their color updated, so simply call
1954 assign_colors() again. */
1959 /* This checks the current color assignment for obvious errors,
1960 like two conflicting webs overlapping in colors, or the used colors
1961 not being in usable regs. */
1967 for (i
= 0; i
< num_webs
- num_subwebs
; i
++)
1969 struct web
*web
= id2web
[i
];
1970 struct web
*aweb
= alias (web
);
1971 struct conflict_link
*wl
;
1973 if (aweb
->type
== SPILLED
|| web
->regno
>= max_normal_pseudo
)
1975 else if (aweb
->type
== COLORED
)
1976 nregs
= hard_regno_nregs
[aweb
->color
][GET_MODE (web
->orig_x
)];
1977 else if (aweb
->type
== PRECOLORED
)
1981 /* The color must be valid for the original usable_regs. */
1982 for (c
= 0; c
< nregs
; c
++)
1983 if (!TEST_HARD_REG_BIT (web
->usable_regs
, aweb
->color
+ c
))
1985 /* Search the original (pre-coalesce) conflict list. In the current
1986 one some imprecise conflicts may be noted (due to combine() or
1987 insert_coalesced_conflicts() relocating partial conflicts) making
1988 it look like some wide webs are in conflict and having the same
1990 wl
= (web
->have_orig_conflicts
? web
->orig_conflict_list
1991 : web
->conflict_list
);
1992 for (; wl
; wl
= wl
->next
)
1993 if (wl
->t
->regno
>= max_normal_pseudo
)
1997 struct web
*web2
= alias (wl
->t
);
1999 if (web2
->type
== COLORED
)
2000 nregs2
= hard_regno_nregs
[web2
->color
][GET_MODE (web2
->orig_x
)];
2001 else if (web2
->type
== PRECOLORED
)
2005 if (aweb
->color
>= web2
->color
+ nregs2
2006 || web2
->color
>= aweb
->color
+ nregs
)
2012 struct sub_conflict
*sl
;
2013 int scol
= aweb
->color
;
2014 int tcol
= alias (wl
->t
)->color
;
2015 if (alias (wl
->t
)->type
== SPILLED
)
2017 for (sl
= wl
->sub
; sl
; sl
= sl
->next
)
2019 int ssize
= hard_regno_nregs
[scol
][GET_MODE (sl
->s
->orig_x
)];
2020 int tsize
= hard_regno_nregs
[tcol
][GET_MODE (sl
->t
->orig_x
)];
2021 int sofs
= 0, tofs
= 0;
2022 if (SUBWEB_P (sl
->t
)
2023 && GET_MODE_SIZE (GET_MODE (sl
->t
->orig_x
)) >= UNITS_PER_WORD
)
2024 tofs
= (SUBREG_BYTE (sl
->t
->orig_x
) / UNITS_PER_WORD
);
2025 if (SUBWEB_P (sl
->s
)
2026 && GET_MODE_SIZE (GET_MODE (sl
->s
->orig_x
))
2028 sofs
= (SUBREG_BYTE (sl
->s
->orig_x
) / UNITS_PER_WORD
);
2029 if ((tcol
+ tofs
>= scol
+ sofs
+ ssize
)
2030 || (scol
+ sofs
>= tcol
+ tofs
+ tsize
))
2038 /* WEB was a coalesced web. Make it unaliased again, and put it
2039 back onto SELECT stack. */
2042 unalias_web (struct web
*web
)
2045 web
->is_coalesced
= 0;
2047 /* Well, initially everything was spilled, so it isn't incorrect,
2048 that also the individual parts can be spilled.
2049 XXX this isn't entirely correct, as we also relaxed the
2050 spill_temp flag in combine(), which might have made components
2051 spill, although they were a short or spilltemp web. */
2052 web
->was_spilled
= 1;
2053 remove_list (web
->dlink
, &WEBS(COALESCED
));
2054 /* Spilltemps must be colored right now (i.e. as early as possible),
2055 other webs can be deferred to the end (the code building the
2056 stack assumed that in this stage only one web was colored). */
2057 if (web
->spill_temp
&& web
->spill_temp
!= 2)
2058 put_web (web
, SELECT
);
2060 put_web_at_end (web
, SELECT
);
2063 /* WEB is a _target_ for coalescing which got spilled.
2064 Break all aliases to WEB, and restore some of its member to the state
2065 they were before coalescing. Due to the suboptimal structure of
2066 the interference graph we need to go through all coalesced webs.
2067 Somewhen we'll change this to be more sane. */
2070 break_aliases_to_web (struct web
*web
)
2072 struct dlist
*d
, *d_next
;
2073 if (web
->type
!= SPILLED
)
2075 for (d
= WEBS(COALESCED
); d
; d
= d_next
)
2077 struct web
*other
= DLIST_WEB (d
);
2079 /* Beware: Don't use alias() here. We really want to check only
2080 one level of aliasing, i.e. only break up webs directly
2081 aliased to WEB, not also those aliased through other webs. */
2082 if (other
->alias
== web
)
2084 unalias_web (other
);
2085 ra_debug_msg (DUMP_COLORIZE
, " %d", other
->id
);
2088 web
->spill_temp
= web
->orig_spill_temp
;
2089 web
->spill_cost
= web
->orig_spill_cost
;
2090 /* Beware: The following possibly widens usable_regs again. While
2091 it was narrower there might have been some conflicts added which got
2092 ignored because of non-intersecting hardregsets. All those conflicts
2093 would now matter again. Fortunately we only add conflicts when
2094 coalescing, which is also the time of narrowing. And we remove all
2095 those added conflicts again now that we unalias this web.
2096 Therefore this is safe to do. */
2097 COPY_HARD_REG_SET (web
->usable_regs
, web
->orig_usable_regs
);
2098 web
->is_coalesced
= 0;
2099 web
->num_aliased
= 0;
2100 web
->was_spilled
= 1;
2101 /* Reset is_coalesced flag for webs which itself are target of coalescing.
2102 It was cleared above if it was coalesced to WEB. */
2103 for (d
= WEBS(COALESCED
); d
; d
= d
->next
)
2104 DLIST_WEB (d
)->alias
->is_coalesced
= 1;
2107 /* WEB is a web coalesced into a precolored one. Break that alias,
2108 making WEB SELECTed again. Also restores the conflicts which resulted
2109 from initially coalescing both. */
2112 break_precolored_alias (struct web
*web
)
2114 struct web
*pre
= web
->alias
;
2115 struct conflict_link
*wl
;
2116 unsigned int c
= pre
->color
;
2117 unsigned int nregs
= hard_regno_nregs
[c
][GET_MODE (web
->orig_x
)];
2118 if (pre
->type
!= PRECOLORED
)
2121 /* Now we need to look at each conflict X of WEB, if it conflicts
2122 with [PRE, PRE+nregs), and remove such conflicts, of X has not other
2123 conflicts, which are coalesced into those precolored webs. */
2124 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
2126 struct web
*x
= wl
->t
;
2129 struct conflict_link
*wl2
;
2130 struct conflict_link
**pcl
;
2132 if (!x
->have_orig_conflicts
)
2134 /* First look at which colors can not go away, due to other coalesces
2136 CLEAR_HARD_REG_SET (regs
);
2137 for (i
= 0; i
< nregs
; i
++)
2138 SET_HARD_REG_BIT (regs
, c
+ i
);
2139 for (wl2
= x
->conflict_list
; wl2
; wl2
= wl2
->next
)
2140 if (wl2
->t
->type
== COALESCED
&& alias (wl2
->t
)->type
== PRECOLORED
)
2141 CLEAR_HARD_REG_BIT (regs
, alias (wl2
->t
)->color
);
2142 /* Now also remove the colors of those conflicts which already
2143 were there before coalescing at all. */
2144 for (wl2
= x
->orig_conflict_list
; wl2
; wl2
= wl2
->next
)
2145 if (wl2
->t
->type
== PRECOLORED
)
2146 CLEAR_HARD_REG_BIT (regs
, wl2
->t
->color
);
2147 /* The colors now still set are those for which WEB was the last
2148 cause, i.e. those which can be removed. */
2150 for (i
= 0; i
< nregs
; i
++)
2151 if (TEST_HARD_REG_BIT (regs
, c
+ i
))
2154 y
= hardreg2web
[c
+ i
];
2155 RESET_BIT (sup_igraph
, x
->id
* num_webs
+ y
->id
);
2156 RESET_BIT (sup_igraph
, y
->id
* num_webs
+ x
->id
);
2157 RESET_BIT (igraph
, igraph_index (x
->id
, y
->id
));
2158 for (sub
= x
->subreg_next
; sub
; sub
= sub
->subreg_next
)
2159 RESET_BIT (igraph
, igraph_index (sub
->id
, y
->id
));
2163 pcl
= &(x
->conflict_list
);
2166 struct web
*y
= (*pcl
)->t
;
2167 if (y
->type
!= PRECOLORED
|| !TEST_HARD_REG_BIT (regs
, y
->color
))
2168 pcl
= &((*pcl
)->next
);
2170 *pcl
= (*pcl
)->next
;
2175 /* WEB is a spilled web which was target for coalescing.
2176 Delete all interference edges which were added due to that coalescing,
2177 and break up the coalescing. */
2180 restore_conflicts_from_coalesce (struct web
*web
)
2182 struct conflict_link
**pcl
;
2183 struct conflict_link
*wl
;
2184 pcl
= &(web
->conflict_list
);
2185 /* No original conflict list means no conflict was added at all
2186 after building the graph. So neither we nor any neighbors have
2187 conflicts due to this coalescing. */
2188 if (!web
->have_orig_conflicts
)
2192 struct web
*other
= (*pcl
)->t
;
2193 for (wl
= web
->orig_conflict_list
; wl
; wl
= wl
->next
)
2198 /* We found this conflict also in the original list, so this
2199 was no new conflict. */
2200 pcl
= &((*pcl
)->next
);
2204 /* This is a new conflict, so delete it from us and
2206 struct conflict_link
**opcl
;
2207 struct conflict_link
*owl
;
2208 struct sub_conflict
*sl
;
2211 if (!other
->have_orig_conflicts
&& other
->type
!= PRECOLORED
)
2213 for (owl
= other
->orig_conflict_list
; owl
; owl
= owl
->next
)
2218 opcl
= &(other
->conflict_list
);
2221 if ((*opcl
)->t
== web
)
2229 opcl
= &((*opcl
)->next
);
2232 if (!owl
&& other
->type
!= PRECOLORED
)
2234 /* wl and owl contain the edge data to be deleted. */
2235 RESET_BIT (sup_igraph
, web
->id
* num_webs
+ other
->id
);
2236 RESET_BIT (sup_igraph
, other
->id
* num_webs
+ web
->id
);
2237 RESET_BIT (igraph
, igraph_index (web
->id
, other
->id
));
2238 for (sl
= wl
->sub
; sl
; sl
= sl
->next
)
2239 RESET_BIT (igraph
, igraph_index (sl
->s
->id
, sl
->t
->id
));
2240 if (other
->type
!= PRECOLORED
)
2242 for (sl
= owl
->sub
; sl
; sl
= sl
->next
)
2243 RESET_BIT (igraph
, igraph_index (sl
->s
->id
, sl
->t
->id
));
2248 /* We must restore usable_regs because record_conflict will use it. */
2249 COPY_HARD_REG_SET (web
->usable_regs
, web
->orig_usable_regs
);
2250 /* We might have deleted some conflicts above, which really are still
2251 there (diamond pattern coalescing). This is because we don't reference
2252 count interference edges but some of them were the result of different
2254 for (wl
= web
->conflict_list
; wl
; wl
= wl
->next
)
2255 if (wl
->t
->type
== COALESCED
)
2258 for (tweb
= wl
->t
->alias
; tweb
; tweb
= tweb
->alias
)
2260 if (wl
->sub
== NULL
)
2261 record_conflict (web
, tweb
);
2264 struct sub_conflict
*sl
;
2265 for (sl
= wl
->sub
; sl
; sl
= sl
->next
)
2267 struct web
*sweb
= NULL
;
2268 if (SUBWEB_P (sl
->t
))
2269 sweb
= find_subweb (tweb
, sl
->t
->orig_x
);
2272 record_conflict (sl
->s
, sweb
);
2275 if (tweb
->type
!= COALESCED
)
2281 /* Repeatedly break aliases for spilled webs, which were target for
2282 coalescing, and recolorize the resulting parts. Do this as long as
2283 there are any spilled coalesce targets. */
2286 break_coalesced_spills (void)
2293 for (d
= WEBS(SPILLED
); d
; d
= d
->next
)
2294 if (DLIST_WEB (d
)->is_coalesced
)
2299 web
= DLIST_WEB (d
);
2300 ra_debug_msg (DUMP_COLORIZE
, "breaking aliases to web %d:", web
->id
);
2301 restore_conflicts_from_coalesce (web
);
2302 break_aliases_to_web (web
);
2303 /* WEB was a spilled web and isn't anymore. Everything coalesced
2304 to WEB is now SELECTed and might potentially get a color.
2305 If those other webs were itself targets of coalescing it might be
2306 that there are still some conflicts from aliased webs missing,
2307 because they were added in combine() right into the now
2308 SELECTed web. So we need to add those missing conflicts here. */
2309 insert_coalesced_conflicts ();
2310 ra_debug_msg (DUMP_COLORIZE
, "\n");
2311 remove_list (d
, &WEBS(SPILLED
));
2312 put_web (web
, SELECT
);
2314 while (WEBS(SELECT
))
2316 d
= pop_list (&WEBS(SELECT
));
2317 colorize_one_web (DLIST_WEB (d
), 1);
2323 for (d
= WEBS(COALESCED
); d
; d
= d
->next
)
2325 struct web
*a
= alias (DLIST_WEB (d
));
2326 DLIST_WEB (d
)->color
= a
->color
;
2329 dump_graph_cost (DUMP_COSTS
, "after alias-breaking");
2332 /* A structure for fast hashing of a pair of webs.
2333 Used to cumulate savings (from removing copy insns) for coalesced webs.
2334 All the pairs are also put into a single linked list. */
2337 struct web_pair
*next_hash
;
2338 struct web_pair
*next_list
;
2339 struct web
*smaller
;
2341 unsigned int conflicts
;
2342 unsigned HOST_WIDE_INT cost
;
2345 /* The actual hash table. */
2346 #define WEB_PAIR_HASH_SIZE 8192
2347 static struct web_pair
*web_pair_hash
[WEB_PAIR_HASH_SIZE
];
2348 static struct web_pair
*web_pair_list
;
2349 static unsigned int num_web_pairs
;
2351 /* Clear the hash table of web pairs. */
2354 init_web_pairs (void)
2356 memset (web_pair_hash
, 0, sizeof web_pair_hash
);
2358 web_pair_list
= NULL
;
2361 /* Given two webs connected by a move with cost COST which together
2362 have CONFLICTS conflicts, add that pair to the hash table, or if
2363 already in, cumulate the costs and conflict number. */
2366 add_web_pair_cost (struct web
*web1
, struct web
*web2
,
2367 unsigned HOST_WIDE_INT cost
, unsigned int conflicts
)
2371 if (web1
->id
> web2
->id
)
2373 struct web
*h
= web1
;
2377 hash
= (web1
->id
* num_webs
+ web2
->id
) % WEB_PAIR_HASH_SIZE
;
2378 for (p
= web_pair_hash
[hash
]; p
; p
= p
->next_hash
)
2379 if (p
->smaller
== web1
&& p
->larger
== web2
)
2382 p
->conflicts
+= conflicts
;
2385 p
= ra_alloc (sizeof *p
);
2386 p
->next_hash
= web_pair_hash
[hash
];
2387 p
->next_list
= web_pair_list
;
2390 p
->conflicts
= conflicts
;
2392 web_pair_hash
[hash
] = p
;
2397 /* Suitable to be passed to qsort(). Sort web pairs so, that those
2398 with more conflicts and higher cost (which actually is a saving
2399 when the moves are removed) come first. */
2402 comp_web_pairs (const void *w1
, const void *w2
)
2404 struct web_pair
*p1
= *(struct web_pair
**)w1
;
2405 struct web_pair
*p2
= *(struct web_pair
**)w2
;
2406 if (p1
->conflicts
> p2
->conflicts
)
2408 else if (p1
->conflicts
< p2
->conflicts
)
2410 else if (p1
->cost
> p2
->cost
)
2412 else if (p1
->cost
< p2
->cost
)
2418 /* Given the list of web pairs, begin to combine them from the one
2419 with the most savings. */
2422 sort_and_combine_web_pairs (int for_move
)
2425 struct web_pair
**sorted
;
2429 sorted
= xmalloc (num_web_pairs
* sizeof (sorted
[0]));
2430 for (p
= web_pair_list
, i
= 0; p
; p
= p
->next_list
)
2432 if (i
!= num_web_pairs
)
2434 qsort (sorted
, num_web_pairs
, sizeof (sorted
[0]), comp_web_pairs
);
2436 /* After combining one pair, we actually should adjust the savings
2437 of the other pairs, if they are connected to one of the just coalesced
2439 for (i
= 0; i
< num_web_pairs
; i
++)
2441 struct web
*w1
, *w2
;
2443 w1
= alias (p
->smaller
);
2444 w2
= alias (p
->larger
);
2445 if (!for_move
&& (w1
->type
== PRECOLORED
|| w2
->type
== PRECOLORED
))
2447 else if (w2
->type
== PRECOLORED
)
2454 && !TEST_BIT (sup_igraph
, w1
->id
* num_webs
+ w2
->id
)
2455 && !TEST_BIT (sup_igraph
, w2
->id
* num_webs
+ w1
->id
)
2456 && w2
->type
!= PRECOLORED
2457 && hard_regs_intersect_p (&w1
->usable_regs
, &w2
->usable_regs
))
2459 if (w1
->type
!= PRECOLORED
2460 || (w1
->type
== PRECOLORED
&& ok (w2
, w1
)))
2462 else if (w1
->type
== PRECOLORED
)
2463 SET_HARD_REG_BIT (w2
->prefer_colors
, w1
->color
);
2469 /* Returns nonzero if source/target reg classes are ok for coalesce. */
2472 ok_class (struct web
*target
, struct web
*source
)
2474 /* Don't coalesce if preferred classes are different and at least one
2475 of them has a size of 1. This was preventing things such as the
2476 branch on count transformation (i.e. DoLoop) since the target, which
2477 prefers the CTR, was being coalesced with a source which preferred
2478 GENERAL_REGS. If only one web has a preferred class with 1 free reg
2479 then set it as the preferred color of the other web. */
2480 enum reg_class t_class
, s_class
;
2481 t_class
= reg_preferred_class (target
->regno
);
2482 s_class
= reg_preferred_class (source
->regno
);
2483 if (t_class
!= s_class
)
2485 if (num_free_regs
[t_class
] == 1)
2487 if (num_free_regs
[s_class
] != 1)
2488 SET_HARD_REG_BIT (source
->prefer_colors
,
2489 single_reg_in_regclass
[t_class
]);
2492 else if (num_free_regs
[s_class
] == 1)
2494 SET_HARD_REG_BIT (target
->prefer_colors
,
2495 single_reg_in_regclass
[s_class
]);
2502 /* Greedily coalesce all moves possible. Begin with the web pair
2503 giving the most saving if coalesced. */
2506 aggressive_coalesce (void)
2511 while ((d
= pop_list (&mv_worklist
)) != NULL
)
2512 if ((m
= DLIST_MOVE (d
)))
2514 struct web
*s
= alias (m
->source_web
);
2515 struct web
*t
= alias (m
->target_web
);
2516 if (t
->type
== PRECOLORED
)
2523 && t
->type
!= PRECOLORED
2524 && !TEST_BIT (sup_igraph
, s
->id
* num_webs
+ t
->id
)
2525 && !TEST_BIT (sup_igraph
, t
->id
* num_webs
+ s
->id
)
2528 if ((s
->type
== PRECOLORED
&& ok (t
, s
))
2529 || s
->type
!= PRECOLORED
)
2531 put_move (m
, MV_COALESCED
);
2532 add_web_pair_cost (s
, t
, BLOCK_FOR_INSN (m
->insn
)->frequency
,
2535 else if (s
->type
== PRECOLORED
)
2536 /* It is !ok(t, s). But later when coloring the graph it might
2537 be possible to take that color. So we remember the preferred
2538 color to try that first. */
2540 put_move (m
, CONSTRAINED
);
2541 SET_HARD_REG_BIT (t
->prefer_colors
, s
->color
);
2546 put_move (m
, CONSTRAINED
);
2549 sort_and_combine_web_pairs (1);
2552 /* This is the difference between optimistic coalescing and
2553 optimistic coalescing+. Extended coalesce tries to coalesce also
2554 non-conflicting nodes, not related by a move. The criteria here is,
2555 the one web must be a source, the other a destination of the same insn.
2556 This actually makes sense, as (because they are in the same insn) they
2557 share many of their neighbors, and if they are coalesced, reduce the
2558 number of conflicts of those neighbors by one. For this we sort the
2559 candidate pairs again according to savings (and this time also conflict
2562 This is also a comparatively slow operation, as we need to go through
2563 all insns, and for each insn, through all defs and uses. */
2566 extended_coalesce_2 (void)
2569 struct ra_insn_info info
;
2572 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2573 if (INSN_P (insn
) && (info
= insn_df
[INSN_UID (insn
)]).num_defs
)
2574 for (n
= 0; n
< info
.num_defs
; n
++)
2576 struct web
*dest
= def2web
[DF_REF_ID (info
.defs
[n
])];
2577 dest
= alias (find_web_for_subweb (dest
));
2578 if (dest
->type
!= PRECOLORED
&& dest
->regno
< max_normal_pseudo
)
2581 for (n2
= 0; n2
< info
.num_uses
; n2
++)
2583 struct web
*source
= use2web
[DF_REF_ID (info
.uses
[n2
])];
2584 source
= alias (find_web_for_subweb (source
));
2585 if (source
->type
!= PRECOLORED
2587 && source
->regno
< max_normal_pseudo
2588 /* Coalesced webs end up using the same REG rtx in
2589 emit_colors(). So we can only coalesce something
2591 && GET_MODE (source
->orig_x
) == GET_MODE (dest
->orig_x
)
2592 && !TEST_BIT (sup_igraph
,
2593 dest
->id
* num_webs
+ source
->id
)
2594 && !TEST_BIT (sup_igraph
,
2595 source
->id
* num_webs
+ dest
->id
)
2596 && ok_class (dest
, source
)
2597 && hard_regs_intersect_p (&source
->usable_regs
,
2598 &dest
->usable_regs
))
2599 add_web_pair_cost (dest
, source
,
2600 BLOCK_FOR_INSN (insn
)->frequency
,
2602 + source
->num_conflicts
);
2606 sort_and_combine_web_pairs (0);
2609 /* Check if we forgot to coalesce some moves. */
2612 check_uncoalesced_moves (void)
2614 struct move_list
*ml
;
2616 for (ml
= wl_moves
; ml
; ml
= ml
->next
)
2619 struct web
*s
= alias (m
->source_web
);
2620 struct web
*t
= alias (m
->target_web
);
2621 if (t
->type
== PRECOLORED
)
2628 && m
->type
!= CONSTRAINED
2629 /* Following can happen when a move was coalesced, but later
2630 broken up again. Then s!=t, but m is still MV_COALESCED. */
2631 && m
->type
!= MV_COALESCED
2632 && t
->type
!= PRECOLORED
2633 && ((s
->type
== PRECOLORED
&& ok (t
, s
))
2634 || s
->type
!= PRECOLORED
)
2635 && !TEST_BIT (sup_igraph
, s
->id
* num_webs
+ t
->id
)
2636 && !TEST_BIT (sup_igraph
, t
->id
* num_webs
+ s
->id
))
2641 /* The toplevel function in this file. Precondition is, that
2642 the interference graph is built completely by ra-build.c. This
2643 produces a list of spilled, colored and coalesced nodes. */
2646 ra_colorize_graph (struct df
*df
)
2650 build_worklists (df
);
2652 /* With optimistic coalescing we coalesce everything we can. */
2653 if (flag_ra_optimistic_coalescing
)
2655 aggressive_coalesce ();
2656 extended_coalesce_2 ();
2659 /* Now build the select stack. */
2665 else if (WEBS(FREEZE
))
2667 else if (WEBS(SPILL
))
2670 while (WEBS(SIMPLIFY
) || WEBS(SIMPLIFY_FAT
) || WEBS(SIMPLIFY_SPILL
)
2671 || mv_worklist
|| WEBS(FREEZE
) || WEBS(SPILL
));
2672 if (flag_ra_optimistic_coalescing
)
2673 check_uncoalesced_moves ();
2675 /* Actually colorize the webs from the select stack. */
2678 dump_graph_cost (DUMP_COSTS
, "initially");
2679 if (flag_ra_break_aliases
)
2680 break_coalesced_spills ();
2683 /* And try to improve the cost by recoloring spilled webs. */
2685 dump_graph_cost (DUMP_COSTS
, "after spill-recolor");
2689 /* Initialize this module. */
2691 void ra_colorize_init (void)
2693 /* FIXME: Choose spill heuristic for platform if we have one */
2694 spill_heuristic
= default_spill_heuristic
;
2697 /* Free all memory. (Note that we don't need to free any per pass
2701 ra_colorize_free_all (void)
2704 while ((d
= pop_list (&WEBS(FREE
))) != NULL
)
2705 put_web (DLIST_WEB (d
), INITIAL
);
2706 while ((d
= pop_list (&WEBS(INITIAL
))) != NULL
)
2708 struct web
*web
= DLIST_WEB (d
);
2710 web
->orig_conflict_list
= NULL
;
2711 web
->conflict_list
= NULL
;
2712 for (web
= web
->subreg_next
; web
; web
= wnext
)
2714 wnext
= web
->subreg_next
;
2717 free (DLIST_WEB (d
));
2722 vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4: