OSDN Git Service

* fold-const.c (fold) <TRUNC_MOD_EXPR>: The transformation "X % -Y"
[pf3gnuchains/gcc-fork.git] / gcc / ra-colorize.c
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>.
5
6    This file is part of GCC.
7
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.
11
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
15    details.
16
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.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "df.h"
32 #include "output.h"
33 #include "ra.h"
34
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.
40
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.
46
47    Additionally there is a custom step to locally improve the overall
48    spill cost of the colored graph (recolor_spills).  */
49
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,
71                            enum machine_mode);
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);
99
100 static struct dlist *mv_worklist, *mv_coalesced, *mv_constrained;
101 static struct dlist *mv_frozen, *mv_active;
102
103 /* Push a node onto the front of the list.  */
104
105 static void
106 push_list (struct dlist *x, struct dlist **list)
107 {
108   if (x->next || x->prev)
109     abort ();
110   x->next = *list;
111   if (*list)
112     (*list)->prev = x;
113   *list = x;
114 }
115
116 static void
117 push_list_end (struct dlist *x, struct dlist **list)
118 {
119   if (x->prev || x->next)
120     abort ();
121   if (!*list)
122     {
123       *list = x;
124       return;
125     }
126   while ((*list)->next)
127     list = &((*list)->next);
128   x->prev = *list;
129   (*list)->next = x;
130 }
131
132 /* Remove a node from the list.  */
133
134 void
135 remove_list (struct dlist *x, struct dlist **list)
136 {
137   struct dlist *y = x->prev;
138   if (y)
139     y->next = x->next;
140   else
141     *list = x->next;
142   y = x->next;
143   if (y)
144     y->prev = x->prev;
145   x->next = x->prev = NULL;
146 }
147
148 /* Pop the front of the list.  */
149
150 struct dlist *
151 pop_list (struct dlist **list)
152 {
153   struct dlist *r = *list;
154   if (r)
155     remove_list (r, list);
156   return r;
157 }
158
159 /* Free the given double linked list.  */
160
161 static void
162 free_dlist (struct dlist **list)
163 {
164   *list = NULL;
165 }
166
167 /* The web WEB should get the given new TYPE.  Put it onto the
168    appropriate list.
169    Inline, because it's called with constant TYPE every time.  */
170
171 inline void
172 put_web (struct web *web, enum ra_node_type type)
173 {
174   switch (type)
175     {
176       case INITIAL:
177       case FREE:
178       case FREEZE:
179       case SPILL:
180       case SPILLED:
181       case COALESCED:
182       case COLORED:
183       case SELECT:
184         push_list (web->dlink, &WEBS(type));
185         break;
186       case PRECOLORED:
187         push_list (web->dlink, &WEBS(INITIAL));
188         break;
189       case SIMPLIFY:
190         if (web->spill_temp)
191           push_list (web->dlink, &WEBS(type = SIMPLIFY_SPILL));
192         else if (web->add_hardregs)
193           push_list (web->dlink, &WEBS(type = SIMPLIFY_FAT));
194         else
195           push_list (web->dlink, &WEBS(SIMPLIFY));
196         break;
197       default:
198         abort ();
199     }
200   web->type = type;
201 }
202
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.  */
208
209 void
210 reset_lists (void)
211 {
212   struct dlist *d;
213   unsigned int i;
214   if (WEBS(SIMPLIFY) || WEBS(SIMPLIFY_SPILL) || WEBS(SIMPLIFY_FAT)
215       || WEBS(FREEZE) || WEBS(SPILL) || WEBS(SELECT))
216     abort ();
217
218   while ((d = pop_list (&WEBS(COALESCED))) != NULL)
219     {
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
227          henceforth.  */
228       if (aweb->type == SPILLED || aweb->type == FREE)
229         put_web (web, FREE);
230       else
231         put_web (web, INITIAL);
232     }
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);
237
238   /* All free webs have no conflicts anymore.  */
239   for (d = WEBS(FREE); d; d = d->next)
240     {
241       struct web *web = DLIST_WEB (d);
242       BITMAP_XFREE (web->useless_conflicts);
243       web->useless_conflicts = NULL;
244     }
245
246   /* Sanity check, that we only have free, initial or precolored webs.  */
247   for (i = 0; i < num_webs; i++)
248     {
249       struct web *web = ID2WEB (i);
250       if (web->type != INITIAL && web->type != FREE && web->type != PRECOLORED)
251         abort ();
252     }
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);
258 }
259
260 /* Similar to put_web(), but add the web to the end of the appropriate
261    list.  Additionally TYPE may not be SIMPLIFY.  */
262
263 static void
264 put_web_at_end (struct web *web, enum ra_node_type type)
265 {
266   if (type == PRECOLORED)
267     type = INITIAL;
268   else if (type == SIMPLIFY)
269     abort ();
270   push_list_end (web->dlink, &WEBS(type));
271   web->type = type;
272 }
273
274 /* Unlink WEB from the list it's currently on (which corresponds to
275    its current type).  */
276
277 void
278 remove_web_from_list (struct web *web)
279 {
280   if (web->type == PRECOLORED)
281     remove_list (web->dlink, &WEBS(INITIAL));
282   else
283     remove_list (web->dlink, &WEBS(web->type));
284 }
285
286 /* Give MOVE the TYPE, and link it into the correct list.  */
287
288 static inline void
289 put_move (struct move *move, enum move_type type)
290 {
291   switch (type)
292     {
293       case WORKLIST:
294         push_list (move->dlink, &mv_worklist);
295         break;
296       case MV_COALESCED:
297         push_list (move->dlink, &mv_coalesced);
298         break;
299       case CONSTRAINED:
300         push_list (move->dlink, &mv_constrained);
301         break;
302       case FROZEN:
303         push_list (move->dlink, &mv_frozen);
304         break;
305       case ACTIVE:
306         push_list (move->dlink, &mv_active);
307         break;
308       default:
309         abort ();
310     }
311   move->type = type;
312 }
313
314 /* Build the worklists we are going to process.  */
315
316 static void
317 build_worklists (struct df *df ATTRIBUTE_UNUSED)
318 {
319   struct dlist *d, *d_next;
320   struct move_list *ml;
321
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
330      from the begin.  */
331   if (ra_pass > 1)
332     {
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];
340       if (num)
341         {
342           qsort (order2web, num, sizeof (order2web[0]), comp_webs_maxcost);
343           for (i = num - 1;; i--)
344             {
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)
350                 {
351                   struct web *pweb = wl->t;
352                   pweb->num_conflicts -= 1 + web->add_hardregs;
353                 }
354               if (i == 0)
355                 break;
356             }
357         }
358       free (order2web);
359     }
360
361   /* For all remaining initial webs, classify them.  */
362   for (d = WEBS(INITIAL); d; d = d_next)
363     {
364       struct web *web = DLIST_WEB (d);
365       d_next = d->next;
366       if (web->type == PRECOLORED)
367         continue;
368
369       remove_list (d, &WEBS(INITIAL));
370       if (web->num_conflicts >= NUM_REGS (web))
371         put_web (web, SPILL);
372       else if (web->moves)
373         put_web (web, FREEZE);
374       else
375         put_web (web, SIMPLIFY);
376     }
377
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)
382     if (ml->move)
383       {
384         struct move *m = ml->move;
385         d = ra_calloc (sizeof (struct dlist));
386         DLIST_MOVE (d) = m;
387         m->dlink = d;
388         put_move (m, WORKLIST);
389       }
390 }
391
392 /* Enable the active moves, in which WEB takes part, to be processed.  */
393
394 static void
395 enable_move (struct web *web)
396 {
397   struct move_list *ml;
398   for (ml = web->moves; ml; ml = ml->next)
399     if (ml->move->type == ACTIVE)
400       {
401         remove_list (ml->move->dlink, &mv_active);
402         put_move (ml->move, WORKLIST);
403       }
404 }
405
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.  */
409
410 static void
411 decrement_degree (struct web *web, int dec)
412 {
413   int before = web->num_conflicts;
414   web->num_conflicts -= dec;
415   if (web->num_conflicts < NUM_REGS (web) && before >= NUM_REGS (web))
416     {
417       struct conflict_link *a;
418       enable_move (web);
419       for (a = web->conflict_list; a; a = a->next)
420         {
421           struct web *aweb = a->t;
422           if (aweb->type != SELECT && aweb->type != COALESCED)
423             enable_move (aweb);
424         }
425       if (web->type != FREEZE)
426         {
427           remove_web_from_list (web);
428           if (web->moves)
429             put_web (web, FREEZE);
430           else
431             put_web (web, SIMPLIFY);
432         }
433     }
434 }
435
436 /* Repeatedly simplify the nodes on the simplify worklists.  */
437
438 static void
439 simplify (void)
440 {
441   struct dlist *d;
442   struct web *web;
443   struct conflict_link *wl;
444   while (1)
445     {
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));
454       if (!d)
455         d = pop_list (&WEBS(SIMPLIFY_FAT));
456       if (!d)
457         d = pop_list (&WEBS(SIMPLIFY_SPILL));
458       if (!d)
459         break;
460       web = DLIST_WEB (d);
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)
465         {
466           struct web *pweb = wl->t;
467           if (pweb->type != SELECT && pweb->type != COALESCED)
468             {
469               decrement_degree (pweb, 1 + web->add_hardregs);
470             }
471         }
472     }
473 }
474
475 /* Helper function to remove a move from the movelist of the web.  */
476
477 static void
478 remove_move_1 (struct web *web, struct move *move)
479 {
480   struct move_list *ml = web->moves;
481   if (!ml)
482     return;
483   if (ml->move == move)
484     {
485       web->moves = ml->next;
486       return;
487     }
488   for (; ml->next && ml->next->move != move; ml = ml->next) ;
489   if (!ml->next)
490     return;
491   ml->next = ml->next->next;
492 }
493
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.  */
497
498 static void
499 remove_move (struct web *web, struct move *move)
500 {
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)
505       abort ();
506 }
507
508 /* Merge the moves for the two webs into the first web's movelist.  */
509
510 void
511 merge_moves (struct web *u, struct web *v)
512 {
513   regset seen;
514   struct move_list *ml, *ml_next;
515
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)
520     {
521       ml_next = ml->next;
522       if (! bitmap_bit_p (seen, INSN_UID (ml->move->insn)))
523         {
524           ml->next = u->moves;
525           u->moves = ml;
526         }
527     }
528   BITMAP_XFREE (seen);
529   v->moves = NULL;
530 }
531
532 /* Add a web to the simplify worklist, from the freeze worklist.  */
533
534 static void
535 add_worklist (struct web *web)
536 {
537   if (web->type != PRECOLORED && !web->moves
538       && web->num_conflicts < NUM_REGS (web))
539     {
540       remove_list (web->dlink, &WEBS(FREEZE));
541       put_web (web, SIMPLIFY);
542     }
543 }
544
545 /* Precolored node coalescing heuristic.  */
546
547 static int
548 ok (struct web *target, struct web *source)
549 {
550   struct conflict_link *wl;
551   int i;
552   int color = source->color;
553   int size;
554
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)))
563     return 0;
564
565   /* Sanity for funny modes.  */
566   size = hard_regno_nregs[color][GET_MODE (target->orig_x)];
567   if (!size)
568     return 0;
569
570   /* We can't coalesce target with a precolored register which isn't in
571      usable_regs.  */
572   for (i = size; i--;)
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))
587       return 0;
588
589   for (wl = target->conflict_list; wl; wl = wl->next)
590     {
591       struct web *pweb = wl->t;
592       if (pweb->type == SELECT || pweb->type == COALESCED)
593         continue;
594
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
606          the T-conflict.
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)) )
612         continue;
613
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.  */
616       if (wl->sub == NULL)
617         return 0;
618       else
619         {
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)
627             {
628               if (!TEST_BIT (igraph, igraph_index (source->id, sl->t->id)))
629                 return 0;
630             }
631         }
632     }
633   return 1;
634 }
635
636 /* Non-precolored node coalescing heuristic.  */
637
638 static int
639 conservative (struct web *target, struct web *source)
640 {
641   unsigned int k;
642   unsigned int loop;
643   regset seen;
644   struct conflict_link *wl;
645   unsigned int num_regs = NUM_REGS (target); /* XXX */
646
647   /* k counts the resulting conflict weight, if target and source
648      would be merged, and all low-degree neighbors would be
649      removed.  */
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;
654          wl; wl = wl->next)
655       {
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))
660           {
661             SET_REGNO_REG_SET (seen, pweb->id);
662             k += 1 + pweb->add_hardregs;
663           }
664       }
665   BITMAP_XFREE (seen);
666
667   if (k >= num_regs)
668     return 0;
669   return 1;
670 }
671
672 /* If the web is coalesced, return it's alias.  Otherwise, return what
673    was passed in.  */
674
675 struct web *
676 alias (struct web *web)
677 {
678   while (web->type == COALESCED)
679     web = web->alias;
680   return web;
681 }
682
683 /* Returns nonzero, if the TYPE belongs to one of those representing
684    SIMPLIFY types.  */
685
686 static inline unsigned int
687 simplify_p (enum ra_node_type type)
688 {
689   return type == SIMPLIFY || type == SIMPLIFY_SPILL || type == SIMPLIFY_FAT;
690 }
691
692 /* Actually combine two webs, that can be coalesced.  */
693
694 static void
695 combine (struct web *u, struct web *v)
696 {
697   int i;
698   struct conflict_link *wl;
699   if (u == v || v->type == COALESCED)
700     abort ();
701   if ((u->regno >= max_normal_pseudo) != (v->regno >= max_normal_pseudo))
702     abort ();
703   remove_web_from_list (v);
704   put_web (v, COALESCED);
705   v->alias = u;
706   u->is_coalesced = 1;
707   v->is_coalesced = 1;
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);*/
712   merge_moves (u, v);
713   /* combine add_hardregs's of U and V.  */
714
715   for (wl = v->conflict_list; wl; wl = wl->next)
716     {
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
724          much time.  */
725       if (1)
726         {
727           struct web *web = u;
728           int nregs = 1 + v->add_hardregs;
729           if (u->type == PRECOLORED)
730             nregs = hard_regno_nregs[u->color][GET_MODE (v->orig_x)];
731
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++)
739             {
740               if (u->type == PRECOLORED)
741                 web = hardreg2web[i + u->color];
742               if (wl->sub == NULL)
743                 record_conflict (web, pweb);
744               else
745                 {
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
755                      of the conflict.  */
756                   for (sl = wl->sub; sl; sl = sl->next)
757                     {
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);
768                       if (!sweb)
769                         sweb = web;
770                       record_conflict (sweb, sl->t);
771                     }
772                 }
773               if (u->type != PRECOLORED)
774                 break;
775             }
776           if (pweb->type != SELECT && pweb->type != COALESCED)
777             decrement_degree (pweb, 1 + v->add_hardregs);
778         }
779     }
780
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.  */
788   u->use_my_regs = 1;
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
793      conflicts.  */
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.  */
798   if (!u->num_freedom)
799     abort();
800
801   if (u->num_conflicts >= NUM_REGS (u)
802       && (u->type == FREEZE || simplify_p (u->type)))
803     {
804       remove_web_from_list (u);
805       put_web (u, SPILL);
806     }
807
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)
813     u->spill_temp = 0;
814   else if (v->spill_temp == 2 && u->spill_temp != 0)
815     u->spill_temp = 2;
816   else if (v->spill_temp == 3 && u->spill_temp == 1)
817     u->spill_temp = 3;
818 }
819
820 /* Attempt to coalesce the first thing on the move worklist.
821    This is used only for iterated coalescing.  */
822
823 static void
824 coalesce (void)
825 {
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);
830
831   if (target->type == PRECOLORED)
832     {
833       struct web *h = source;
834       source = target;
835       target = h;
836     }
837   if (source == target)
838     {
839       remove_move (source, m);
840       put_move (m, MV_COALESCED);
841       add_worklist (source);
842     }
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))
847     {
848       remove_move (source, m);
849       remove_move (target, m);
850       put_move (m, CONSTRAINED);
851       add_worklist (source);
852       add_worklist (target);
853     }
854   else if ((source->type == PRECOLORED && ok (target, source))
855            || (source->type != PRECOLORED
856                && conservative (target, source)))
857     {
858       remove_move (source, m);
859       remove_move (target, m);
860       put_move (m, MV_COALESCED);
861       combine (source, target);
862       add_worklist (source);
863     }
864   else
865     put_move (m, ACTIVE);
866 }
867
868 /* Freeze the moves associated with the web.  Used for iterated coalescing.  */
869
870 static void
871 freeze_moves (struct web *web)
872 {
873   struct move_list *ml, *ml_next;
874   for (ml = web->moves; ml; ml = ml_next)
875     {
876       struct move *m = ml->move;
877       struct web *src, *dest;
878       ml_next = ml->next;
879       if (m->type == ACTIVE)
880         remove_list (m->dlink, &mv_active);
881       else
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))
891         {
892           remove_list (src->dlink, &WEBS(FREEZE));
893           put_web (src, SIMPLIFY);
894         }
895     }
896 }
897
898 /* Freeze the first thing on the freeze worklist (only for iterated
899    coalescing).  */
900
901 static void
902 freeze (void)
903 {
904   struct dlist *d = pop_list (&WEBS(FREEZE));
905   put_web (DLIST_WEB (d), SIMPLIFY);
906   freeze_moves (DLIST_WEB (d));
907 }
908
909 /* The current spill heuristic.  Returns a number for a WEB.
910    Webs with higher numbers are selected later.  */
911
912 static unsigned HOST_WIDE_INT (*spill_heuristic) (struct web *);
913
914 static unsigned HOST_WIDE_INT default_spill_heuristic (struct web *);
915
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.  */
919
920 static unsigned HOST_WIDE_INT
921 default_spill_heuristic (struct web *web)
922 {
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;
936   return ret;
937 }
938
939 /* Select the cheapest spill to be potentially spilled (we don't
940    *actually* spill until we need to).  */
941
942 static void
943 select_spill (void)
944 {
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;
949   struct dlist *d;
950   for (d = WEBS(SPILL); d; d = d->next)
951     {
952       struct web *w = DLIST_WEB (d);
953       unsigned HOST_WIDE_INT cost = spill_heuristic (w);
954       if ((!w->spill_temp) && cost < best)
955         {
956           best = cost;
957           bestd = d;
958         }
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)
963         {
964           best2 = cost;
965           bestd2 = d;
966         }
967     }
968   if (!bestd)
969     {
970       bestd = bestd2;
971       best = best2;
972     }
973   if (!bestd)
974     abort ();
975
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);
983 }
984
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.  */
987
988 static int
989 color_usable_p (int c, HARD_REG_SET dont_begin_colors,
990                 HARD_REG_SET free_colors, enum machine_mode  mode)
991 {
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))
995     {
996       int i, size;
997       size = hard_regno_nregs[c][mode];
998       for (i = 1; i < size && TEST_HARD_REG_BIT (free_colors, c + i); i++);
999       if (i == size)
1000         return 1;
1001     }
1002   return 0;
1003 }
1004
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]
1008 #else
1009 #define INV_REG_ALLOC_ORDER(c) c
1010 #endif
1011
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.  */
1017
1018 int
1019 get_free_reg (HARD_REG_SET dont_begin_colors, HARD_REG_SET free_colors,
1020               enum machine_mode mode)
1021 {
1022   int c;
1023   int last_resort_reg = -1;
1024   int pref_reg = -1;
1025   int pref_reg_order = INT_MAX;
1026   int last_resort_reg_order = INT_MAX;
1027
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))
1032       {
1033         int i, size;
1034         size = hard_regno_nregs[c][mode];
1035         for (i = 1; i < size && TEST_HARD_REG_BIT (free_colors, c + i); i++);
1036         if (i != size)
1037           {
1038             c += i;
1039             continue;
1040           }
1041         if (i == size)
1042           {
1043             if (size < 2 || (c & 1) == 0)
1044               {
1045                 if (INV_REG_ALLOC_ORDER (c) < pref_reg_order)
1046                   {
1047                     pref_reg = c;
1048                     pref_reg_order = INV_REG_ALLOC_ORDER (c);
1049                   }
1050               }
1051             else if (INV_REG_ALLOC_ORDER (c) < last_resort_reg_order)
1052               {
1053                 last_resort_reg = c;
1054                 last_resort_reg_order = INV_REG_ALLOC_ORDER (c);
1055               }
1056           }
1057         else
1058           c += i;
1059       }
1060   return pref_reg >= 0 ? pref_reg : last_resort_reg;
1061 }
1062
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.  */
1067
1068 static int
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)
1072 {
1073   int c = -1;
1074   HARD_REG_SET s;
1075   if (flag_ra_biased)
1076     {
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);
1081       if (c >= 0)
1082         return c;
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);
1086       if (c >= 0)
1087         return c;
1088     }
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);
1092   if (c >= 0)
1093       return c;
1094   c = get_free_reg (dont_begin_colors, free_colors, mode);
1095   return c;
1096 }
1097
1098 /* Counts the number of non-overlapping bitblocks of length LEN
1099    in FREE_COLORS.  */
1100
1101 static int
1102 count_long_blocks (HARD_REG_SET free_colors, int len)
1103 {
1104   int i, j;
1105   int count = 0;
1106   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1107     {
1108       if (!TEST_HARD_REG_BIT (free_colors, i))
1109         continue;
1110       for (j = 1; j < len; j++)
1111         if (!TEST_HARD_REG_BIT (free_colors, i + j))
1112           break;
1113       /* Bits [i .. i+j-1] are free.  */
1114       if (j == len)
1115         count++;
1116       i += j - 1;
1117     }
1118   return count;
1119 }
1120
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.  */
1124
1125 static char *
1126 hardregset_to_string (HARD_REG_SET s)
1127 {
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);
1131 #else
1132   char *c = string;
1133   int i,j;
1134   c += sprintf (c, "{ ");
1135   for (i = 0;i < HARD_REG_SET_LONGS; i++)
1136     {
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 ? ", " : "");
1140     }
1141   c += sprintf (c, " }");
1142 #endif
1143   return string;
1144 }
1145
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.  */
1155
1156 static void
1157 calculate_dont_begin (struct web *web, HARD_REG_SET *result)
1158 {
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)
1167     {
1168       struct web *w;
1169       struct web *ptarget = alias (wl->t);
1170       struct sub_conflict *sl = wl->sub;
1171       w = sl ? sl->t : wl->t;
1172       while (w)
1173         {
1174           if (ptarget->type == COLORED || ptarget->type == PRECOLORED)
1175             {
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
1181                                                     (source->orig_x)];
1182               unsigned int tofs = 0;
1183               unsigned int sofs = 0;
1184               /* C1 and C2 can become negative, so unsigned
1185                  would be wrong.  */
1186               int c1, c2;
1187
1188               if (SUBWEB_P (w)
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))
1193                      >= UNITS_PER_WORD)
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;
1197               if (c2 >= 0)
1198                 {
1199                   if (c1 < 0)
1200                     c1 = 0;
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.  */
1206                   while (c1 + sofs
1207                          + hard_regno_nregs[c1][GET_MODE (source->orig_x)] - 1
1208                          < ptarget->color + tofs)
1209                     c1++;
1210                   while (c1 > 0 && c1 + sofs
1211                          + hard_regno_nregs[c1][GET_MODE (source->orig_x)] - 1
1212                          > ptarget->color + tofs)
1213                     c1--;
1214                   for (; c1 <= c2; c1++)
1215                     SET_HARD_REG_BIT (dont_begin, c1);
1216                 }
1217             }
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
1220              a whole web.  */
1221           if (!sl)
1222             break;
1223           sl = sl->next;
1224           w = sl ? sl->t : NULL;
1225         }
1226     }
1227   COPY_HARD_REG_SET (*result, dont_begin);
1228 }
1229
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
1232    neighbors.
1233
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.  */
1242
1243 static void
1244 colorize_one_web (struct web *web, int hard)
1245 {
1246   struct conflict_link *wl;
1247   HARD_REG_SET colors, dont_begin;
1248   int c = -1;
1249   int bestc = -1;
1250   int neighbor_needs= 0;
1251   struct web *fats_parent = NULL;
1252   int num_fat = 0;
1253   int long_blocks = 0;
1254   int best_long_blocks = -1;
1255   HARD_REG_SET fat_colors;
1256   HARD_REG_SET bias;
1257
1258   CLEAR_HARD_REG_SET (fat_colors);
1259   
1260   if (web->regno >= max_normal_pseudo)
1261     hard = 0;
1262
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);
1266
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)
1271     {
1272       struct web *w;
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)
1279         while (w)
1280           {
1281             if (find_web_for_subweb (w)->type != COALESCED
1282                 && w->add_hardregs >= neighbor_needs)
1283               {
1284                 neighbor_needs = w->add_hardregs;
1285                 fats_parent = ptarget;
1286                 num_fat++;
1287               }
1288             if (!sl)
1289               break;
1290             sl = sl->next;
1291             w = sl ? sl->t : NULL;
1292           }
1293     }
1294
1295   ra_debug_msg (DUMP_COLORIZE, "colorize web %d [don't begin at %s]", web->id,
1296              hardregset_to_string (dont_begin));
1297
1298   /* If there are some fat neighbors, remember their usable regs,
1299      and how many blocks are free in it for that neighbor.  */
1300   if (num_fat)
1301     {
1302       COPY_HARD_REG_SET (fat_colors, fats_parent->usable_regs);
1303       long_blocks = count_long_blocks (fat_colors, neighbor_needs + 1);
1304     }
1305
1306   /* We break out, if we found a color which doesn't constrain
1307      neighbors, or if we can't find any colors.  */
1308   while (1)
1309     {
1310       HARD_REG_SET call_clobbered;
1311
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)
1325         {
1326           COPY_HARD_REG_SET (colors, web->usable_regs);
1327           AND_HARD_REG_SET (colors,
1328                             usable_regs[reg_preferred_class (web->regno)]);
1329         }
1330       else
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);
1336 #endif
1337       COPY_HARD_REG_SET (call_clobbered, colors);
1338       AND_HARD_REG_SET (call_clobbered, call_used_reg_set);
1339
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.  */
1343       if (web->old_color)
1344         {
1345           c = web->old_color - 1;
1346           if (!color_usable_p (c, dont_begin, colors,
1347                                PSEUDO_REGNO_MODE (web->regno)))
1348             c = -1;
1349         }
1350       else
1351         c = -1;
1352       if (c < 0)
1353         c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1354                             call_clobbered, PSEUDO_REGNO_MODE (web->regno));
1355       if (c < 0)
1356         c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1357                           colors, PSEUDO_REGNO_MODE (web->regno));
1358
1359       if (c < 0)
1360         {
1361           if (web->use_my_regs)
1362             IOR_HARD_REG_SET (colors, web->usable_regs);
1363           else
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);
1369 #endif
1370           COPY_HARD_REG_SET (call_clobbered, colors);
1371           AND_HARD_REG_SET (call_clobbered, call_used_reg_set);
1372
1373           c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1374                             call_clobbered, PSEUDO_REGNO_MODE (web->regno));
1375           if (c < 0)
1376             c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1377                               colors, PSEUDO_REGNO_MODE (web->regno));
1378         }
1379       if (c < 0)
1380         break;
1381       if (bestc < 0)
1382         bestc = c;
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.  */
1386       if (num_fat)
1387         {
1388           int i;
1389           int new_long;
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)
1398             {
1399               if (new_long > best_long_blocks)
1400                 {
1401                   best_long_blocks = new_long;
1402                   bestc = c;
1403                 }
1404               SET_HARD_REG_BIT (dont_begin, c);
1405               ra_debug_msg (DUMP_COLORIZE, " avoid %d", c);
1406             }
1407           else
1408             /* We found a color which doesn't destroy a block.  */
1409             break;
1410         }
1411       /* If we havee no fat neighbors, the current color won't become
1412          "better", so we've found it.  */
1413       else
1414         break;
1415     }
1416   ra_debug_msg (DUMP_COLORIZE, " --> got %d", c < 0 ? bestc : c);
1417   if (bestc >= 0 && c < 0 && !web->was_spilled)
1418     {
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
1422          a spill temp.  */
1423       if (1 || web->spill_temp)
1424         c = bestc;
1425       ra_debug_msg (DUMP_COLORIZE, " [constrains neighbors]");
1426     }
1427   ra_debug_msg (DUMP_COLORIZE, "\n");
1428
1429   if (c < 0)
1430     {
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_.
1442
1443          if (DLIST_WEB (d)->was_spilled < 0)
1444          abort (); */
1445       if (hard && (!web->was_spilled || web->spill_temp))
1446         {
1447           unsigned int loop;
1448           struct web *try = NULL;
1449           struct web *candidates[8];
1450
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
1459              spill those.  */
1460           memset (candidates, 0, sizeof candidates);
1461 #define set_cand(i, w) \
1462           do { \
1463               if (!candidates[(i)] \
1464                   || (candidates[(i)]->spill_cost < (w)->spill_cost)) \
1465                 candidates[(i)] = (w); \
1466           } while (0)
1467           for (wl = web->conflict_list; wl; wl = wl->next)
1468             {
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)
1476                 {
1477                   if (!w->spill_temp)
1478                     set_cand (4, w);
1479                   else if (web->spill_temp == 2
1480                            && w->spill_temp == 2
1481                            && w->spill_cost < web->spill_cost)
1482                     set_cand (5, w);
1483                   else if (web->spill_temp != 2
1484                            && (w->spill_temp == 2
1485                                || w->spill_cost < web->spill_cost))
1486                     set_cand (6, w);
1487                   continue;
1488                 }
1489               if (aw->type != COLORED)
1490                 continue;
1491               if (w->type == COLORED && !w->spill_temp && !w->is_coalesced
1492                   && w->was_spilled)
1493                 {
1494                   if (w->spill_cost < web->spill_cost)
1495                     set_cand (0, w);
1496                   else if (web->spill_temp)
1497                     set_cand (1, w);
1498                 }
1499               if (w->type == COLORED && !w->spill_temp && !w->is_coalesced
1500                   && !w->was_spilled)
1501                 {
1502                   if (w->spill_cost < web->spill_cost)
1503                     set_cand (2, w);
1504                   else if (web->spill_temp && web->spill_temp != 2)
1505                     set_cand (3, w);
1506                 }
1507               if (web->spill_temp)
1508                 {
1509                   if (w->type == COLORED && w->spill_temp == 2
1510                       && !w->is_coalesced
1511                       && (w->spill_cost < web->spill_cost
1512                           || web->spill_temp != 2))
1513                     set_cand (4, w);
1514                   if (!aw->spill_temp)
1515                     set_cand (5, aw);
1516                   if (aw->spill_temp == 2
1517                       && (aw->spill_cost < web->spill_cost
1518                           || web->spill_temp != 2))
1519                     set_cand (6, aw);
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)
1527                     set_cand (7, aw);
1528                 }
1529             }
1530           for (loop = 0; try == NULL && loop < 8; loop++)
1531             if (candidates[loop])
1532               try = candidates[loop];
1533 #undef set_cand
1534           if (try)
1535             {
1536               int old_c = try->color;
1537               if (try->type == COALESCED)
1538                 {
1539                   if (alias (try)->type != PRECOLORED)
1540                     abort ();
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);
1545                 }
1546               else
1547                 {
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
1552                      neighbors.  */
1553                   ra_debug_msg (DUMP_COLORIZE, "  trying to spill %d\n", try->id);
1554                   colorize_one_web (web, hard);
1555                   if (web->type != COLORED)
1556                     {
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);
1562                       try->color = old_c;
1563                       ra_debug_msg (DUMP_COLORIZE,
1564                                     "  spilling %d was useless\n", try->id);
1565                     }
1566                   else
1567                     {
1568                       ra_debug_msg (DUMP_COLORIZE,
1569                                     "  to spill %d was a good idea\n",
1570                                     try->id);
1571                       remove_list (try->dlink, &WEBS(SPILLED));
1572                       if (try->was_spilled)
1573                         colorize_one_web (try, 0);
1574                       else
1575                         colorize_one_web (try, hard - 1);
1576                     }
1577                 }
1578             }
1579           else
1580             /* No more chances to get a color, so give up hope and
1581                spill us.  */
1582             put_web (web, SPILLED);
1583         }
1584       else
1585         put_web (web, SPILLED);
1586     }
1587   else
1588     {
1589       put_web (web, COLORED);
1590       web->color = c;
1591       if (flag_ra_biased)
1592         {
1593           int nregs = hard_regno_nregs[c][GET_MODE (web->orig_x)];
1594           for (wl = web->conflict_list; wl; wl = wl->next)
1595             {
1596               struct web *ptarget = alias (wl->t);
1597               int i;
1598               for (i = 0; i < nregs; i++)
1599                 SET_HARD_REG_BIT (ptarget->bias_colors, c + i);
1600             }
1601         }
1602     }
1603   if (web->regno >= max_normal_pseudo && web->type == SPILLED)
1604     {
1605       web->color = an_unusable_color;
1606       remove_list (web->dlink, &WEBS(SPILLED));
1607       put_web (web, COLORED);
1608     }
1609   if (web->type == SPILLED && flag_ra_optimistic_coalescing
1610       && web->is_coalesced)
1611     {
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);
1619       web->color = -1;
1620     }
1621 }
1622
1623 /* Assign the colors to all nodes on the select stack.  And update the
1624    colors of coalesced webs.  */
1625
1626 static void
1627 assign_colors (void)
1628 {
1629   struct dlist *d;
1630
1631   while (WEBS(SELECT))
1632     {
1633       d = pop_list (&WEBS(SELECT));
1634       colorize_one_web (DLIST_WEB (d), 1);
1635     }
1636
1637   for (d = WEBS(COALESCED); d; d = d->next)
1638     {
1639       struct web *a = alias (DLIST_WEB (d));
1640       DLIST_WEB (d)->color = a->color;
1641     }
1642 }
1643
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.
1650
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.  */
1654
1655 static void
1656 try_recolor_web (struct web *web)
1657 {
1658   struct conflict_link *wl;
1659   unsigned HOST_WIDE_INT *cost_neighbors;
1660   unsigned int *min_color;
1661   int newcol, c;
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++)
1670     {
1671       int i, nregs;
1672       if (!HARD_REGNO_MODE_OK (c, GET_MODE (web->orig_x)))
1673         continue;
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))
1677           break;
1678       if (i < nregs || nregs == 0)
1679         continue;
1680       SET_HARD_REG_BIT (possible_begin, c);
1681       for (; nregs--;)
1682         if (!min_color[c + nregs])
1683           min_color[c + nregs] = 1 + c;
1684     }
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)
1689     {
1690       HARD_REG_SET dont_begin;
1691       struct web *web2 = alias (wl->t);
1692       struct conflict_link *nn;
1693       int c1, c2;
1694       int wide_p = 0;
1695       if (wl->t->type == COALESCED || web2->type != COLORED)
1696         {
1697           if (web2->type == PRECOLORED)
1698             {
1699               c1 = min_color[web2->color];
1700               c1 = (c1 == 0) ? web2->color : (c1 - 1);
1701               c2 = web2->color;
1702               for (; c1 <= c2; c1++)
1703                 SET_HARD_REG_BIT (precolored_neighbors, c1);
1704             }
1705           continue;
1706         }
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)
1715         wide_p = 1;
1716       for (nn = web2->conflict_list; nn && !wide_p; nn = nn->next)
1717         if (alias (nn->t)->add_hardregs)
1718           wide_p = 1;
1719       calculate_dont_begin (web2, &dont_begin);
1720       c1 = min_color[web2->color];
1721       /* Note that min_color[] contains 1-based values (zero means
1722          undef).  */
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))
1728           {
1729             int nregs;
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);
1733             for (; nregs--;)
1734               CLEAR_HARD_REG_BIT (colors, c1 + nregs);
1735             if (wide_p)
1736               SET_HARD_REG_BIT (wide_seen, c1);
1737             if (get_free_reg (dont_begin, colors,
1738                               GET_MODE (web2->orig_x)) < 0)
1739               {
1740                 if (web2->spill_temp)
1741                   SET_HARD_REG_BIT (spill_temps, c1);
1742                 else
1743                   cost_neighbors[c1] += web2->spill_cost;
1744               }
1745           }
1746     }
1747   newcol = -1;
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)
1752         && (newcol == -1
1753             || cost_neighbors[c] < cost_neighbors[newcol]))
1754       newcol = c;
1755   if (newcol >= 0 && cost_neighbors[newcol] < web->spill_cost)
1756     {
1757       int nregs = hard_regno_nregs[newcol][GET_MODE (web->orig_x)];
1758       unsigned HOST_WIDE_INT cost = 0;
1759       int *old_colors;
1760       struct conflict_link *wl_next;
1761       ra_debug_msg (DUMP_COLORIZE, "try to set web %d to color %d\n", web->id,
1762                  newcol);
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)
1768         {
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
1776              iteration.  */
1777           wl_next = wl->next;
1778           if (web2->type == COLORED)
1779             {
1780               int nregs2 = hard_regno_nregs[web2->color][GET_MODE
1781                                             (web2->orig_x)];
1782               if (web->color >= web2->color + nregs2
1783                   || web2->color >= web->color + nregs)
1784                 continue;
1785               old_colors[web2->id] = web2->color + 1;
1786               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;
1795             }
1796         }
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
1802          be colorable.  */
1803       if (cost > cost_neighbors[newcol]
1804           && nregs == 1 && !TEST_HARD_REG_BIT (wide_seen, newcol))
1805         abort ();
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)
1809         {
1810           ra_debug_msg (DUMP_COLORIZE,
1811                      "reset coloring of web %d, too expensive\n", web->id);
1812           remove_list (web->dlink, &WEBS(COLORED));
1813           web->color = -1;
1814           put_web (web, SPILLED);
1815           for (wl = web->conflict_list; wl; wl = wl->next)
1816             {
1817               struct web *web2 = alias (wl->t);
1818               if (old_colors[web2->id])
1819                 {
1820                   if (web2->type == SPILLED)
1821                     {
1822                       remove_list (web2->dlink, &WEBS(SPILLED));
1823                       web2->color = old_colors[web2->id] - 1;
1824                       put_web (web2, COLORED);
1825                     }
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
1836                        care.  */
1837                     ;
1838                   else
1839                     abort ();
1840                 }
1841             }
1842         }
1843       free (old_colors);
1844     }
1845   free (min_color);
1846   free (cost_neighbors);
1847 }
1848
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.  */
1855
1856 static void
1857 insert_coalesced_conflicts (void)
1858 {
1859   struct dlist *d;
1860   for (d = WEBS(COALESCED); 0 && d; d = d->next)
1861     {
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)
1866         {
1867           struct web *tweb = aweb;
1868           int i;
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++)
1873             {
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))
1890                 abort ();
1891               /*if (wl->sub == NULL)
1892                 record_conflict (tweb, wl->t);
1893               else
1894                 {
1895                   struct sub_conflict *sl;
1896                   for (sl = wl->sub; sl; sl = sl->next)
1897                     record_conflict (tweb, sl->t);
1898                 }*/
1899               if (aweb->type != PRECOLORED)
1900                 break;
1901             }
1902         }
1903     }
1904 }
1905
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.  */
1909
1910 static int
1911 comp_webs_maxcost (const void *w1, const void *w2)
1912 {
1913   struct web *web1 = *(struct web **)w1;
1914   struct web *web2 = *(struct web **)w2;
1915   if (web1->spill_cost > web2->spill_cost)
1916     return -1;
1917   else if (web1->spill_cost < web2->spill_cost)
1918     return 1;
1919   else
1920     return 0;
1921 }
1922
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.  */
1925
1926 static void
1927 recolor_spills (void)
1928 {
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++)
1934     {
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;
1940     }
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++)
1945     {
1946       struct web *web = order2web[i];
1947       if (web->type == SPILLED)
1948         try_recolor_web (web);
1949     }
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.  */
1955   assign_colors ();
1956   free (order2web);
1957 }
1958
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.  */
1962
1963 static void
1964 check_colors (void)
1965 {
1966   unsigned int i;
1967   for (i = 0; i < num_webs - num_subwebs; i++)
1968     {
1969       struct web *web = id2web[i];
1970       struct web *aweb = alias (web);
1971       struct conflict_link *wl;
1972       int nregs, c;
1973       if (aweb->type == SPILLED || web->regno >= max_normal_pseudo)
1974         continue;
1975       else if (aweb->type == COLORED)
1976         nregs = hard_regno_nregs[aweb->color][GET_MODE (web->orig_x)];
1977       else if (aweb->type == PRECOLORED)
1978         nregs = 1;
1979       else
1980         abort ();
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))
1984           abort ();
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
1989          color.  */
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)
1994           continue;
1995         else if (!wl->sub)
1996           {
1997             struct web *web2 = alias (wl->t);
1998             int nregs2;
1999             if (web2->type == COLORED)
2000               nregs2 = hard_regno_nregs[web2->color][GET_MODE (web2->orig_x)];
2001             else if (web2->type == PRECOLORED)
2002               nregs2 = 1;
2003             else
2004               continue;
2005             if (aweb->color >= web2->color + nregs2
2006                 || web2->color >= aweb->color + nregs)
2007               continue;
2008             abort ();
2009           }
2010         else
2011           {
2012             struct sub_conflict *sl;
2013             int scol = aweb->color;
2014             int tcol = alias (wl->t)->color;
2015             if (alias (wl->t)->type == SPILLED)
2016               continue;
2017             for (sl = wl->sub; sl; sl = sl->next)
2018               {
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))
2027                        >= UNITS_PER_WORD)
2028                   sofs = (SUBREG_BYTE (sl->s->orig_x) / UNITS_PER_WORD);
2029                 if ((tcol + tofs >= scol + sofs + ssize)
2030                     || (scol + sofs >= tcol + tofs + tsize))
2031                   continue;
2032                 abort ();
2033               }
2034           }
2035     }
2036 }
2037
2038 /* WEB was a coalesced web.  Make it unaliased again, and put it
2039    back onto SELECT stack.  */
2040
2041 static void
2042 unalias_web (struct web *web)
2043 {
2044   web->alias = NULL;
2045   web->is_coalesced = 0;
2046   web->color = -1;
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);
2059   else
2060     put_web_at_end (web, SELECT);
2061 }
2062
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.  */
2068
2069 static void
2070 break_aliases_to_web (struct web *web)
2071 {
2072   struct dlist *d, *d_next;
2073   if (web->type != SPILLED)
2074     abort ();
2075   for (d = WEBS(COALESCED); d; d = d_next)
2076     {
2077       struct web *other = DLIST_WEB (d);
2078       d_next = d->next;
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)
2083         {
2084           unalias_web (other);
2085           ra_debug_msg (DUMP_COLORIZE, " %d", other->id);
2086         }
2087     }
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;
2105 }
2106
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.  */
2110
2111 static void
2112 break_precolored_alias (struct web *web)
2113 {
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)
2119     abort ();
2120   unalias_web (web);
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)
2125     {
2126       struct web *x = wl->t;
2127       struct web *y;
2128       unsigned int i;
2129       struct conflict_link *wl2;
2130       struct conflict_link **pcl;
2131       HARD_REG_SET regs;
2132       if (!x->have_orig_conflicts)
2133         continue;
2134       /* First look at which colors can not go away, due to other coalesces
2135          still existing.  */
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.  */
2149       y = NULL;
2150       for (i = 0; i < nregs; i++)
2151         if (TEST_HARD_REG_BIT (regs, c + i))
2152           {
2153             struct web *sub;
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));
2160           }
2161       if (!y)
2162         continue;
2163       pcl = &(x->conflict_list);
2164       while (*pcl)
2165         {
2166           struct web *y = (*pcl)->t;
2167           if (y->type != PRECOLORED || !TEST_HARD_REG_BIT (regs, y->color))
2168             pcl = &((*pcl)->next);
2169           else
2170             *pcl = (*pcl)->next;
2171         }
2172     }
2173 }
2174
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.  */
2178
2179 static void
2180 restore_conflicts_from_coalesce (struct web *web)
2181 {
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)
2189     return;
2190   while (*pcl)
2191     {
2192       struct web *other = (*pcl)->t;
2193       for (wl = web->orig_conflict_list; wl; wl = wl->next)
2194         if (wl->t == other)
2195           break;
2196       if (wl)
2197         {
2198           /* We found this conflict also in the original list, so this
2199              was no new conflict.  */
2200           pcl = &((*pcl)->next);
2201         }
2202       else
2203         {
2204           /* This is a new conflict, so delete it from us and
2205              the neighbor.  */
2206           struct conflict_link **opcl;
2207           struct conflict_link *owl;
2208           struct sub_conflict *sl;
2209           wl = *pcl;
2210           *pcl = wl->next;
2211           if (!other->have_orig_conflicts && other->type != PRECOLORED)
2212             abort ();
2213           for (owl = other->orig_conflict_list; owl; owl = owl->next)
2214             if (owl->t == web)
2215               break;
2216           if (owl)
2217             abort ();
2218           opcl = &(other->conflict_list);
2219           while (*opcl)
2220             {
2221               if ((*opcl)->t == web)
2222                 {
2223                   owl = *opcl;
2224                   *opcl = owl->next;
2225                   break;
2226                 }
2227               else
2228                 {
2229                   opcl = &((*opcl)->next);
2230                 }
2231             }
2232           if (!owl && other->type != PRECOLORED)
2233             abort ();
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)
2241             {
2242               for (sl = owl->sub; sl; sl = sl->next)
2243                 RESET_BIT (igraph, igraph_index (sl->s->id, sl->t->id));
2244             }
2245         }
2246     }
2247
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
2253      coalesces.  */
2254   for (wl = web->conflict_list; wl; wl = wl->next)
2255     if (wl->t->type == COALESCED)
2256       {
2257         struct web *tweb;
2258         for (tweb = wl->t->alias; tweb; tweb = tweb->alias)
2259           {
2260             if (wl->sub == NULL)
2261               record_conflict (web, tweb);
2262             else
2263               {
2264                 struct sub_conflict *sl;
2265                 for (sl = wl->sub; sl; sl = sl->next)
2266                   {
2267                     struct web *sweb = NULL;
2268                     if (SUBWEB_P (sl->t))
2269                       sweb = find_subweb (tweb, sl->t->orig_x);
2270                     if (!sweb)
2271                       sweb = tweb;
2272                     record_conflict (sl->s, sweb);
2273                   }
2274               }
2275             if (tweb->type != COALESCED)
2276               break;
2277           }
2278       }
2279 }
2280
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.  */
2284
2285 static void
2286 break_coalesced_spills (void)
2287 {
2288   int changed = 0;
2289   while (1)
2290     {
2291       struct dlist *d;
2292       struct web *web;
2293       for (d = WEBS(SPILLED); d; d = d->next)
2294         if (DLIST_WEB (d)->is_coalesced)
2295           break;
2296       if (!d)
2297         break;
2298       changed = 1;
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);
2313       web->color = -1;
2314       while (WEBS(SELECT))
2315         {
2316           d = pop_list (&WEBS(SELECT));
2317           colorize_one_web (DLIST_WEB (d), 1);
2318         }
2319     }
2320   if (changed)
2321     {
2322       struct dlist *d;
2323       for (d = WEBS(COALESCED); d; d = d->next)
2324         {
2325           struct web *a = alias (DLIST_WEB (d));
2326           DLIST_WEB (d)->color = a->color;
2327         }
2328     }
2329   dump_graph_cost (DUMP_COSTS, "after alias-breaking");
2330 }
2331
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.  */
2335 struct web_pair
2336 {
2337   struct web_pair *next_hash;
2338   struct web_pair *next_list;
2339   struct web *smaller;
2340   struct web *larger;
2341   unsigned int conflicts;
2342   unsigned HOST_WIDE_INT cost;
2343 };
2344
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;
2350
2351 /* Clear the hash table of web pairs.  */
2352
2353 static void
2354 init_web_pairs (void)
2355 {
2356   memset (web_pair_hash, 0, sizeof web_pair_hash);
2357   num_web_pairs = 0;
2358   web_pair_list = NULL;
2359 }
2360
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.  */
2364
2365 static void
2366 add_web_pair_cost (struct web *web1, struct web *web2,
2367                    unsigned HOST_WIDE_INT cost, unsigned int conflicts)
2368 {
2369   unsigned int hash;
2370   struct web_pair *p;
2371   if (web1->id > web2->id)
2372     {
2373       struct web *h = web1;
2374       web1 = web2;
2375       web2 = h;
2376     }
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)
2380       {
2381         p->cost += cost;
2382         p->conflicts += conflicts;
2383         return;
2384       }
2385   p = ra_alloc (sizeof *p);
2386   p->next_hash = web_pair_hash[hash];
2387   p->next_list = web_pair_list;
2388   p->smaller = web1;
2389   p->larger = web2;
2390   p->conflicts = conflicts;
2391   p->cost = cost;
2392   web_pair_hash[hash] = p;
2393   web_pair_list = p;
2394   num_web_pairs++;
2395 }
2396
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.  */
2400
2401 static int
2402 comp_web_pairs (const void *w1, const void *w2)
2403 {
2404   struct web_pair *p1 = *(struct web_pair **)w1;
2405   struct web_pair *p2 = *(struct web_pair **)w2;
2406   if (p1->conflicts > p2->conflicts)
2407     return -1;
2408   else if (p1->conflicts < p2->conflicts)
2409     return 1;
2410   else if (p1->cost > p2->cost)
2411     return -1;
2412   else if (p1->cost < p2->cost)
2413     return 1;
2414   else
2415     return 0;
2416 }
2417
2418 /* Given the list of web pairs, begin to combine them from the one
2419    with the most savings.  */
2420
2421 static void
2422 sort_and_combine_web_pairs (int for_move)
2423 {
2424   unsigned int i;
2425   struct web_pair **sorted;
2426   struct web_pair *p;
2427   if (!num_web_pairs)
2428     return;
2429   sorted = xmalloc (num_web_pairs * sizeof (sorted[0]));
2430   for (p = web_pair_list, i = 0; p; p = p->next_list)
2431     sorted[i++] = p;
2432   if (i != num_web_pairs)
2433     abort ();
2434   qsort (sorted, num_web_pairs, sizeof (sorted[0]), comp_web_pairs);
2435
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
2438      pair.  Later.  */
2439   for (i = 0; i < num_web_pairs; i++)
2440     {
2441       struct web *w1, *w2;
2442       p = sorted[i];
2443       w1 = alias (p->smaller);
2444       w2 = alias (p->larger);
2445       if (!for_move && (w1->type == PRECOLORED || w2->type == PRECOLORED))
2446         continue;
2447       else if (w2->type == PRECOLORED)
2448         {
2449           struct web *h = w1;
2450           w1 = w2;
2451           w2 = h;
2452         }
2453       if (w1 != w2
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))
2458           {
2459             if (w1->type != PRECOLORED
2460                 || (w1->type == PRECOLORED && ok (w2, w1)))
2461               combine (w1, w2);
2462             else if (w1->type == PRECOLORED)
2463               SET_HARD_REG_BIT (w2->prefer_colors, w1->color);
2464           }
2465     }
2466   free (sorted);
2467 }
2468
2469 /* Returns nonzero if source/target reg classes are ok for coalesce.  */
2470
2471 static int
2472 ok_class (struct web *target, struct web *source)
2473 {
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)
2484     {
2485       if (num_free_regs[t_class] == 1)
2486         {
2487           if (num_free_regs[s_class] != 1)
2488             SET_HARD_REG_BIT (source->prefer_colors,
2489                               single_reg_in_regclass[t_class]);
2490           return 0;
2491         }
2492       else if (num_free_regs[s_class] == 1)
2493         {
2494             SET_HARD_REG_BIT (target->prefer_colors,
2495                               single_reg_in_regclass[s_class]);
2496           return 0;
2497         }
2498     }
2499   return 1;
2500 }
2501
2502 /* Greedily coalesce all moves possible.  Begin with the web pair
2503    giving the most saving if coalesced.  */
2504
2505 static void
2506 aggressive_coalesce (void)
2507 {
2508   struct dlist *d;
2509   struct move *m;
2510   init_web_pairs ();
2511   while ((d = pop_list (&mv_worklist)) != NULL)
2512     if ((m = DLIST_MOVE (d)))
2513       {
2514         struct web *s = alias (m->source_web);
2515         struct web *t = alias (m->target_web);
2516         if (t->type == PRECOLORED)
2517           {
2518             struct web *h = s;
2519             s = t;
2520             t = h;
2521           }
2522         if (s != t
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)
2526             && ok_class (t, s))
2527           {
2528             if ((s->type == PRECOLORED && ok (t, s))
2529                 || s->type != PRECOLORED)
2530               {
2531                 put_move (m, MV_COALESCED);
2532                 add_web_pair_cost (s, t, BLOCK_FOR_INSN (m->insn)->frequency,
2533                                    0);
2534               }
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.  */
2539               {
2540                 put_move (m, CONSTRAINED);
2541                 SET_HARD_REG_BIT (t->prefer_colors, s->color);
2542               }
2543           }
2544         else
2545           {
2546             put_move (m, CONSTRAINED);
2547           }
2548       }
2549   sort_and_combine_web_pairs (1);
2550 }
2551
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
2560    number).
2561
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.  */
2564
2565 static void
2566 extended_coalesce_2 (void)
2567 {
2568   rtx insn;
2569   struct ra_insn_info info;
2570   unsigned int n;
2571   init_web_pairs ();
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++)
2575         {
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)
2579             {
2580               unsigned int n2;
2581               for (n2 = 0; n2 < info.num_uses; n2++)
2582                 {
2583                   struct web *source = use2web[DF_REF_ID (info.uses[n2])];
2584                   source = alias (find_web_for_subweb (source));
2585                   if (source->type != PRECOLORED
2586                       && source != dest
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
2590                          of equal modes.  */
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,
2601                                        dest->num_conflicts
2602                                        + source->num_conflicts);
2603                 }
2604             }
2605         }
2606   sort_and_combine_web_pairs (0);
2607 }
2608
2609 /* Check if we forgot to coalesce some moves.  */
2610
2611 static void
2612 check_uncoalesced_moves (void)
2613 {
2614   struct move_list *ml;
2615   struct move *m;
2616   for (ml = wl_moves; ml; ml = ml->next)
2617     if ((m = ml->move))
2618       {
2619         struct web *s = alias (m->source_web);
2620         struct web *t = alias (m->target_web);
2621         if (t->type == PRECOLORED)
2622           {
2623             struct web *h = s;
2624             s = t;
2625             t = h;
2626           }
2627         if (s != t
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))
2637           abort ();
2638       }
2639 }
2640
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.  */
2644
2645 void
2646 ra_colorize_graph (struct df *df)
2647 {
2648   if (dump_file)
2649     dump_igraph (df);
2650   build_worklists (df);
2651
2652   /* With optimistic coalescing we coalesce everything we can.  */
2653   if (flag_ra_optimistic_coalescing)
2654     {
2655       aggressive_coalesce ();
2656       extended_coalesce_2 ();
2657     }
2658
2659   /* Now build the select stack.  */
2660   do
2661     {
2662       simplify ();
2663       if (mv_worklist)
2664         coalesce ();
2665       else if (WEBS(FREEZE))
2666         freeze ();
2667       else if (WEBS(SPILL))
2668         select_spill ();
2669     }
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 ();
2674
2675   /* Actually colorize the webs from the select stack.  */
2676   assign_colors ();
2677   check_colors ();
2678   dump_graph_cost (DUMP_COSTS, "initially");
2679   if (flag_ra_break_aliases)
2680     break_coalesced_spills ();
2681   check_colors ();
2682
2683   /* And try to improve the cost by recoloring spilled webs.  */
2684   recolor_spills ();
2685   dump_graph_cost (DUMP_COSTS, "after spill-recolor");
2686   check_colors ();
2687 }
2688
2689 /* Initialize this module.  */
2690
2691 void ra_colorize_init (void)
2692 {
2693   /* FIXME: Choose spill heuristic for platform if we have one */
2694   spill_heuristic = default_spill_heuristic;
2695 }
2696
2697 /* Free all memory.  (Note that we don't need to free any per pass
2698    memory).  */
2699
2700 void
2701 ra_colorize_free_all (void)
2702 {
2703   struct dlist *d;
2704   while ((d = pop_list (&WEBS(FREE))) != NULL)
2705     put_web (DLIST_WEB (d), INITIAL);
2706   while ((d = pop_list (&WEBS(INITIAL))) != NULL)
2707     {
2708       struct web *web = DLIST_WEB (d);
2709       struct web *wnext;
2710       web->orig_conflict_list = NULL;
2711       web->conflict_list = NULL;
2712       for (web = web->subreg_next; web; web = wnext)
2713         {
2714           wnext = web->subreg_next;
2715           free (web);
2716         }
2717       free (DLIST_WEB (d));
2718     }
2719 }
2720
2721 /*
2722 vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4:
2723 */