OSDN Git Service

* gcc.c-torture/compile/20021120-1.c: New test.
[pf3gnuchains/gcc-fork.git] / gcc / ra-colorize.c
1 /* Graph coloring register allocator
2    Copyright (C) 2001, 2002 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 "rtl.h"
24 #include "tm_p.h"
25 #include "function.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "df.h"
30 #include "output.h"
31 #include "ra.h"
32
33 /* This file is part of the graph coloring register allocator.
34    It contains the graph colorizer.  Given an interference graph
35    as set up in ra-build.c the toplevel function in this file
36    (ra_colorize_graph) colorizes the graph, leaving a list
37    of colored, coalesced and spilled nodes.
38
39    The algorithm used is a merge of George & Appels iterative coalescing
40    and optimistic coalescing, switchable at runtime.  The current default
41    is "optimistic coalescing +", which is based on the normal Briggs/Cooper
42    framework.  We can also use biased coloring.  Most of the structure
43    here follows the different papers.
44
45    Additionally there is a custom step to locally improve the overall
46    spill cost of the colored graph (recolor_spills).  */
47
48 static void push_list PARAMS ((struct dlist *, struct dlist **));
49 static void push_list_end PARAMS ((struct dlist *, struct dlist **));
50 static void free_dlist PARAMS ((struct dlist **));
51 static void put_web_at_end PARAMS ((struct web *, enum node_type));
52 static void put_move PARAMS ((struct move *, enum move_type));
53 static void build_worklists PARAMS ((struct df *));
54 static void enable_move PARAMS ((struct web *));
55 static void decrement_degree PARAMS ((struct web *, int));
56 static void simplify PARAMS ((void));
57 static void remove_move_1 PARAMS ((struct web *, struct move *));
58 static void remove_move PARAMS ((struct web *, struct move *));
59 static void add_worklist PARAMS ((struct web *));
60 static int ok PARAMS ((struct web *, struct web *));
61 static int conservative PARAMS ((struct web *, struct web *));
62 static inline unsigned int simplify_p PARAMS ((enum node_type));
63 static void combine PARAMS ((struct web *, struct web *));
64 static void coalesce PARAMS ((void));
65 static void freeze_moves PARAMS ((struct web *));
66 static void freeze PARAMS ((void));
67 static void select_spill PARAMS ((void));
68 static int color_usable_p PARAMS ((int, HARD_REG_SET, HARD_REG_SET,
69                                    enum machine_mode));
70 int get_free_reg PARAMS ((HARD_REG_SET, HARD_REG_SET, enum machine_mode));
71 static int get_biased_reg PARAMS ((HARD_REG_SET, HARD_REG_SET, HARD_REG_SET,
72                                    HARD_REG_SET, enum machine_mode));
73 static int count_long_blocks PARAMS ((HARD_REG_SET, int));
74 static char * hardregset_to_string PARAMS ((HARD_REG_SET));
75 static void calculate_dont_begin PARAMS ((struct web *, HARD_REG_SET *));
76 static void colorize_one_web PARAMS ((struct web *, int));
77 static void assign_colors PARAMS ((void));
78 static void try_recolor_web PARAMS ((struct web *));
79 static void insert_coalesced_conflicts PARAMS ((void));
80 static int comp_webs_maxcost PARAMS ((const void *, const void *));
81 static void recolor_spills PARAMS ((void));
82 static void check_colors PARAMS ((void));
83 static void restore_conflicts_from_coalesce PARAMS ((struct web *));
84 static void break_coalesced_spills PARAMS ((void));
85 static void unalias_web PARAMS ((struct web *));
86 static void break_aliases_to_web PARAMS ((struct web *));
87 static void break_precolored_alias PARAMS ((struct web *));
88 static void init_web_pairs PARAMS ((void));
89 static void add_web_pair_cost PARAMS ((struct web *, struct web *,
90                                        unsigned HOST_WIDE_INT, unsigned int));
91 static int comp_web_pairs PARAMS ((const void *, const void *));
92 static void sort_and_combine_web_pairs PARAMS ((int));
93 static void aggressive_coalesce PARAMS ((void));
94 static void extended_coalesce_2 PARAMS ((void));
95 static void check_uncoalesced_moves PARAMS ((void));
96
97 static struct dlist *mv_worklist, *mv_coalesced, *mv_constrained;
98 static struct dlist *mv_frozen, *mv_active;
99
100 /* Push a node onto the front of the list.  */
101
102 static void
103 push_list (x, list)
104      struct dlist *x;
105      struct dlist **list;
106 {
107   if (x->next || x->prev)
108     abort ();
109   x->next = *list;
110   if (*list)
111     (*list)->prev = x;
112   *list = x;
113 }
114
115 static void
116 push_list_end (x, list)
117      struct dlist *x;
118      struct dlist **list;
119 {
120   if (x->prev || x->next)
121     abort ();
122   if (!*list)
123     {
124       *list = x;
125       return;
126     }
127   while ((*list)->next)
128     list = &((*list)->next);
129   x->prev = *list;
130   (*list)->next = x;
131 }
132
133 /* Remove a node from the list.  */
134
135 void
136 remove_list (x, list)
137      struct dlist *x;
138      struct dlist **list;
139 {
140   struct dlist *y = x->prev;
141   if (y)
142     y->next = x->next;
143   else
144     *list = x->next;
145   y = x->next;
146   if (y)
147     y->prev = x->prev;
148   x->next = x->prev = NULL;
149 }
150
151 /* Pop the front of the list.  */
152
153 struct dlist *
154 pop_list (list)
155      struct dlist **list;
156 {
157   struct dlist *r = *list;
158   if (r)
159     remove_list (r, list);
160   return r;
161 }
162
163 /* Free the given double linked list.  */
164
165 static void
166 free_dlist (list)
167      struct dlist **list;
168 {
169   *list = NULL;
170 }
171
172 /* The web WEB should get the given new TYPE.  Put it onto the
173    appropriate list.
174    Inline, because it's called with constant TYPE every time.  */
175
176 inline void
177 put_web (web, type)
178      struct web *web;
179      enum node_type type;
180 {
181   switch (type)
182     {
183       case INITIAL:
184       case FREE:
185       case FREEZE:
186       case SPILL:
187       case SPILLED:
188       case COALESCED:
189       case COLORED:
190       case SELECT:
191         push_list (web->dlink, &WEBS(type));
192         break;
193       case PRECOLORED:
194         push_list (web->dlink, &WEBS(INITIAL));
195         break;
196       case SIMPLIFY:
197         if (web->spill_temp)
198           push_list (web->dlink, &WEBS(type = SIMPLIFY_SPILL));
199         else if (web->add_hardregs)
200           push_list (web->dlink, &WEBS(type = SIMPLIFY_FAT));
201         else
202           push_list (web->dlink, &WEBS(SIMPLIFY));
203         break;
204       default:
205         abort ();
206     }
207   web->type = type;
208 }
209
210 /* After we are done with the whole pass of coloring/spilling,
211    we reset the lists of webs, in preparation of the next pass.
212    The spilled webs become free, colored webs go to the initial list,
213    coalesced webs become free or initial, according to what type of web
214    they are coalesced to.  */
215
216 void
217 reset_lists ()
218 {
219   struct dlist *d;
220   unsigned int i;
221   if (WEBS(SIMPLIFY) || WEBS(SIMPLIFY_SPILL) || WEBS(SIMPLIFY_FAT)
222       || WEBS(FREEZE) || WEBS(SPILL) || WEBS(SELECT))
223     abort ();
224
225   while ((d = pop_list (&WEBS(COALESCED))) != NULL)
226     {
227       struct web *web = DLIST_WEB (d);
228       struct web *aweb = alias (web);
229       /* Note, how alias() becomes invalid through the two put_web()'s
230          below.  It might set the type of a web to FREE (from COALESCED),
231          which itself is a target of aliasing (i.e. in the middle of
232          an alias chain).  We can handle this by checking also for
233          type == FREE.  Note nevertheless, that alias() is invalid
234          henceforth.  */
235       if (aweb->type == SPILLED || aweb->type == FREE)
236         put_web (web, FREE);
237       else
238         put_web (web, INITIAL);
239     }
240   while ((d = pop_list (&WEBS(SPILLED))) != NULL)
241     put_web (DLIST_WEB (d), FREE);
242   while ((d = pop_list (&WEBS(COLORED))) != NULL)
243     put_web (DLIST_WEB (d), INITIAL);
244
245   /* All free webs have no conflicts anymore.  */
246   for (d = WEBS(FREE); d; d = d->next)
247     {
248       struct web *web = DLIST_WEB (d);
249       BITMAP_XFREE (web->useless_conflicts);
250       web->useless_conflicts = NULL;
251     }
252
253   /* Sanity check, that we only have free, initial or precolored webs.  */
254   for (i = 0; i < num_webs; i++)
255     {
256       struct web *web = ID2WEB (i);
257       if (web->type != INITIAL && web->type != FREE && web->type != PRECOLORED)
258         abort ();
259     }
260   free_dlist (&mv_worklist);
261   free_dlist (&mv_coalesced);
262   free_dlist (&mv_constrained);
263   free_dlist (&mv_frozen);
264   free_dlist (&mv_active);
265 }
266
267 /* Similar to put_web(), but add the web to the end of the appropriate
268    list.  Additionally TYPE may not be SIMPLIFY.  */
269
270 static void
271 put_web_at_end (web, type)
272      struct web *web;
273      enum node_type type;
274 {
275   if (type == PRECOLORED)
276     type = INITIAL;
277   else if (type == SIMPLIFY)
278     abort ();
279   push_list_end (web->dlink, &WEBS(type));
280   web->type = type;
281 }
282
283 /* Unlink WEB from the list it's currently on (which corresponds to
284    its current type).  */
285
286 void
287 remove_web_from_list (web)
288      struct web *web;
289 {
290   if (web->type == PRECOLORED)
291     remove_list (web->dlink, &WEBS(INITIAL));
292   else
293     remove_list (web->dlink, &WEBS(web->type));
294 }
295
296 /* Give MOVE the TYPE, and link it into the correct list.  */
297
298 static inline void
299 put_move (move, type)
300      struct move *move;
301      enum move_type type;
302 {
303   switch (type)
304     {
305       case WORKLIST:
306         push_list (move->dlink, &mv_worklist);
307         break;
308       case MV_COALESCED:
309         push_list (move->dlink, &mv_coalesced);
310         break;
311       case CONSTRAINED:
312         push_list (move->dlink, &mv_constrained);
313         break;
314       case FROZEN:
315         push_list (move->dlink, &mv_frozen);
316         break;
317       case ACTIVE:
318         push_list (move->dlink, &mv_active);
319         break;
320       default:
321         abort ();
322     }
323   move->type = type;
324 }
325
326 /* Build the worklists we are going to process.  */
327
328 static void
329 build_worklists (df)
330      struct df *df ATTRIBUTE_UNUSED;
331 {
332   struct dlist *d, *d_next;
333   struct move_list *ml;
334
335   /* If we are not the first pass, put all stackwebs (which are still
336      backed by a new pseudo, but conceptually can stand for a stackslot,
337      i.e. it doesn't really matter if they get a color or not), on
338      the SELECT stack first, those with lowest cost first.  This way
339      they will be colored last, so do not contrain the coloring of the
340      normal webs.  But still those with the highest count are colored
341      before, i.e. get a color more probable.  The use of stackregs is
342      a pure optimization, and all would work, if we used real stackslots
343      from the begin.  */
344   if (ra_pass > 1)
345     {
346       unsigned int i, num, max_num;
347       struct web **order2web;
348       max_num = num_webs - num_subwebs;
349       order2web = (struct web **) xmalloc (max_num * sizeof (order2web[0]));
350       for (i = 0, num = 0; i < max_num; i++)
351         if (id2web[i]->regno >= max_normal_pseudo)
352           order2web[num++] = id2web[i];
353       if (num)
354         {
355           qsort (order2web, num, sizeof (order2web[0]), comp_webs_maxcost);
356           for (i = num - 1;; i--)
357             {
358               struct web *web = order2web[i];
359               struct conflict_link *wl;
360               remove_list (web->dlink, &WEBS(INITIAL));
361               put_web (web, SELECT);
362               for (wl = web->conflict_list; wl; wl = wl->next)
363                 {
364                   struct web *pweb = wl->t;
365                   pweb->num_conflicts -= 1 + web->add_hardregs;
366                 }
367               if (i == 0)
368                 break;
369             }
370         }
371       free (order2web);
372     }
373
374   /* For all remaining initial webs, classify them.  */
375   for (d = WEBS(INITIAL); d; d = d_next)
376     {
377       struct web *web = DLIST_WEB (d);
378       d_next = d->next;
379       if (web->type == PRECOLORED)
380         continue;
381
382       remove_list (d, &WEBS(INITIAL));
383       if (web->num_conflicts >= NUM_REGS (web))
384         put_web (web, SPILL);
385       else if (web->moves)
386         put_web (web, FREEZE);
387       else
388         put_web (web, SIMPLIFY);
389     }
390
391   /* And put all moves on the worklist for iterated coalescing.
392      Note, that if iterated coalescing is off, then wl_moves doesn't
393      contain any moves.  */
394   for (ml = wl_moves; ml; ml = ml->next)
395     if (ml->move)
396       {
397         struct move *m = ml->move;
398         d = (struct dlist *) ra_calloc (sizeof (struct dlist));
399         DLIST_MOVE (d) = m;
400         m->dlink = d;
401         put_move (m, WORKLIST);
402       }
403 }
404
405 /* Enable the active moves, in which WEB takes part, to be processed.  */
406
407 static void
408 enable_move (web)
409      struct web *web;
410 {
411   struct move_list *ml;
412   for (ml = web->moves; ml; ml = ml->next)
413     if (ml->move->type == ACTIVE)
414       {
415         remove_list (ml->move->dlink, &mv_active);
416         put_move (ml->move, WORKLIST);
417       }
418 }
419
420 /* Decrement the degree of node WEB by the amount DEC.
421    Possibly change the type of WEB, if the number of conflicts is
422    now smaller than its freedom.  */
423
424 static void
425 decrement_degree (web, dec)
426      struct web *web;
427      int dec;
428 {
429   int before = web->num_conflicts;
430   web->num_conflicts -= dec;
431   if (web->num_conflicts < NUM_REGS (web) && before >= NUM_REGS (web))
432     {
433       struct conflict_link *a;
434       enable_move (web);
435       for (a = web->conflict_list; a; a = a->next)
436         {
437           struct web *aweb = a->t;
438           if (aweb->type != SELECT && aweb->type != COALESCED)
439             enable_move (aweb);
440         }
441       if (web->type != FREEZE)
442         {
443           remove_web_from_list (web);
444           if (web->moves)
445             put_web (web, FREEZE);
446           else
447             put_web (web, SIMPLIFY);
448         }
449     }
450 }
451
452 /* Repeatedly simplify the nodes on the simplify worklists.  */
453
454 static void
455 simplify ()
456 {
457   struct dlist *d;
458   struct web *web;
459   struct conflict_link *wl;
460   while (1)
461     {
462       /* We try hard to color all the webs resulting from spills first.
463          Without that on register starved machines (x86 e.g) with some live
464          DImode pseudos, -fPIC, and an asm requiring %edx, it might be, that
465          we do rounds over rounds, because the conflict graph says, we can
466          simplify those short webs, but later due to irregularities we can't
467          color those pseudos.  So we have to spill them, which in later rounds
468          leads to other spills.  */
469       d = pop_list (&WEBS(SIMPLIFY));
470       if (!d)
471         d = pop_list (&WEBS(SIMPLIFY_FAT));
472       if (!d)
473         d = pop_list (&WEBS(SIMPLIFY_SPILL));
474       if (!d)
475         break;
476       web = DLIST_WEB (d);
477       ra_debug_msg (DUMP_PROCESS, " simplifying web %3d, conflicts = %d\n",
478                  web->id, web->num_conflicts);
479       put_web (web, SELECT);
480       for (wl = web->conflict_list; wl; wl = wl->next)
481         {
482           struct web *pweb = wl->t;
483           if (pweb->type != SELECT && pweb->type != COALESCED)
484             {
485               decrement_degree (pweb, 1 + web->add_hardregs);
486             }
487         }
488     }
489 }
490
491 /* Helper function to remove a move from the movelist of the web.  */
492
493 static void
494 remove_move_1 (web, move)
495      struct web *web;
496      struct move *move;
497 {
498   struct move_list *ml = web->moves;
499   if (!ml)
500     return;
501   if (ml->move == move)
502     {
503       web->moves = ml->next;
504       return;
505     }
506   for (; ml->next && ml->next->move != move; ml = ml->next) ;
507   if (!ml->next)
508     return;
509   ml->next = ml->next->next;
510 }
511
512 /* Remove a move from the movelist of the web.  Actually this is just a
513    wrapper around remove_move_1(), making sure, the removed move really is
514    not in the list anymore.  */
515
516 static void
517 remove_move (web, move)
518      struct web *web;
519      struct move *move;
520 {
521   struct move_list *ml;
522   remove_move_1 (web, move);
523   for (ml = web->moves; ml; ml = ml->next)
524     if (ml->move == move)
525       abort ();
526 }
527
528 /* Merge the moves for the two webs into the first web's movelist.  */
529
530 void
531 merge_moves (u, v)
532      struct web *u, *v;
533 {
534   regset seen;
535   struct move_list *ml;
536
537   seen = BITMAP_XMALLOC ();
538   for (ml = u->moves; ml; ml = ml->next)
539     bitmap_set_bit (seen, INSN_UID (ml->move->insn));
540   for (ml = v->moves; ml; ml = ml->next)
541     {
542       if (! bitmap_bit_p (seen, INSN_UID (ml->move->insn)))
543         {
544           ml->next = u->moves;
545           u->moves = ml;
546         }
547     }
548   BITMAP_XFREE (seen);
549   v->moves = NULL;
550 }
551
552 /* Add a web to the simplify worklist, from the freeze worklist.  */
553
554 static void
555 add_worklist (web)
556      struct web *web;
557 {
558   if (web->type != PRECOLORED && !web->moves
559       && web->num_conflicts < NUM_REGS (web))
560     {
561       remove_list (web->dlink, &WEBS(FREEZE));
562       put_web (web, SIMPLIFY);
563     }
564 }
565
566 /* Precolored node coalescing heuristic.  */
567
568 static int
569 ok (target, source)
570      struct web *target, *source;
571 {
572   struct conflict_link *wl;
573   int i;
574   int color = source->color;
575   int size;
576
577   /* Normally one would think, the next test wouldn't be needed.
578      We try to coalesce S and T, and S has already a color, and we checked
579      when processing the insns, that both have the same mode.  So naively
580      we could conclude, that of course that mode was valid for this color.
581      Hah.  But there is sparc.  Before reload there are copy insns
582      (e.g. the ones copying arguments to locals) which happily refer to
583      colors in invalid modes.  We can't coalesce those things.  */
584   if (! HARD_REGNO_MODE_OK (source->color, GET_MODE (target->orig_x)))
585     return 0;
586
587   /* Sanity for funny modes.  */
588   size = HARD_REGNO_NREGS (color, GET_MODE (target->orig_x));
589   if (!size)
590     return 0;
591
592   /* We can't coalesce target with a precolored register which isn't in
593      usable_regs.  */
594   for (i = size; i--;)
595     if (TEST_HARD_REG_BIT (never_use_colors, color + i)
596         || !TEST_HARD_REG_BIT (target->usable_regs, color + i)
597         /* Before usually calling ok() at all, we already test, if the
598            candidates conflict in sup_igraph.  But when wide webs are
599            coalesced to hardregs, we only test the hardweb coalesced into.
600            This is only the begin color.  When actually coalescing both,
601            it will also take the following size colors, i.e. their webs.
602            We nowhere checked if the candidate possibly conflicts with
603            one of _those_, which is possible with partial conflicts,
604            so we simply do it here (this does one bit-test more than
605            necessary, the first color).  Note, that if X is precolored
606            bit [X*num_webs + Y] can't be set (see add_conflict_edge()).  */
607         || TEST_BIT (sup_igraph,
608                      target->id * num_webs + hardreg2web[color + i]->id))
609       return 0;
610
611   for (wl = target->conflict_list; wl; wl = wl->next)
612     {
613       struct web *pweb = wl->t;
614       if (pweb->type == SELECT || pweb->type == COALESCED)
615         continue;
616
617       /* Coalescing target (T) and source (S) is o.k, if for
618          all conflicts C of T it is true, that:
619           1) C will be colored, or
620           2) C is a hardreg (precolored), or
621           3) C already conflicts with S too, or
622           4) a web which contains C conflicts already with S.
623          XXX: we handle here only the special case of 4), that C is
624          a subreg, and the containing thing is the reg itself, i.e.
625          we dont handle the situation, were T conflicts with
626          (subreg:SI x 1), and S conflicts with (subreg:DI x 0), which
627          would be allowed also, as the S-conflict overlaps
628          the T-conflict.
629          So, we first test the whole web for any of these conditions, and
630          continue with the next C, if 1, 2 or 3 is true.  */
631       if (pweb->num_conflicts < NUM_REGS (pweb)
632           || pweb->type == PRECOLORED
633           || TEST_BIT (igraph, igraph_index (source->id, pweb->id)) )
634         continue;
635
636       /* This is reached, if not one of 1, 2 or 3 was true.  In the case C has
637          no subwebs, 4 can't be true either, so we can't coalesce S and T.  */
638       if (wl->sub == NULL)
639         return 0;
640       else
641         {
642           /* The main webs do _not_ conflict, only some parts of both.  This
643              means, that 4 is possibly true, so we need to check this too.
644              For this we go thru all sub conflicts between T and C, and see if
645              the target part of C already conflicts with S.  When this is not
646              the case we disallow coalescing.  */
647           struct sub_conflict *sl;
648           for (sl = wl->sub; sl; sl = sl->next)
649             {
650               if (!TEST_BIT (igraph, igraph_index (source->id, sl->t->id)))
651                 return 0;
652             }
653         }
654     }
655   return 1;
656 }
657
658 /* Non-precolored node coalescing heuristic.  */
659
660 static int
661 conservative (target, source)
662      struct web *target, *source;
663 {
664   unsigned int k;
665   unsigned int loop;
666   regset seen;
667   struct conflict_link *wl;
668   unsigned int num_regs = NUM_REGS (target); /* XXX */
669
670   /* k counts the resulting conflict weight, if target and source
671      would be merged, and all low-degree neighbors would be
672      removed.  */
673   k = 0 * MAX (target->add_hardregs, source->add_hardregs);
674   seen = BITMAP_XMALLOC ();
675   for (loop = 0; loop < 2; loop++)
676     for (wl = ((loop == 0) ? target : source)->conflict_list;
677          wl; wl = wl->next)
678       {
679         struct web *pweb = wl->t;
680         if (pweb->type != SELECT && pweb->type != COALESCED
681             && pweb->num_conflicts >= NUM_REGS (pweb)
682             && ! REGNO_REG_SET_P (seen, pweb->id))
683           {
684             SET_REGNO_REG_SET (seen, pweb->id);
685             k += 1 + pweb->add_hardregs;
686           }
687       }
688   BITMAP_XFREE (seen);
689
690   if (k >= num_regs)
691     return 0;
692   return 1;
693 }
694
695 /* If the web is coalesced, return it's alias.  Otherwise, return what
696    was passed in.  */
697
698 struct web *
699 alias (web)
700      struct web *web;
701 {
702   while (web->type == COALESCED)
703     web = web->alias;
704   return web;
705 }
706
707 /* Returns nonzero, if the TYPE belongs to one of those representing
708    SIMPLIFY types.  */
709
710 static inline unsigned int
711 simplify_p (type)
712      enum node_type type;
713 {
714   return type == SIMPLIFY || type == SIMPLIFY_SPILL || type == SIMPLIFY_FAT;
715 }
716
717 /* Actually combine two webs, that can be coalesced.  */
718
719 static void
720 combine (u, v)
721      struct web *u, *v;
722 {
723   int i;
724   struct conflict_link *wl;
725   if (u == v || v->type == COALESCED)
726     abort ();
727   if ((u->regno >= max_normal_pseudo) != (v->regno >= max_normal_pseudo))
728     abort ();
729   remove_web_from_list (v);
730   put_web (v, COALESCED);
731   v->alias = u;
732   u->is_coalesced = 1;
733   v->is_coalesced = 1;
734   u->num_aliased += 1 + v->num_aliased;
735   if (flag_ra_merge_spill_costs && u->type != PRECOLORED)
736     u->spill_cost += v->spill_cost;
737     /*u->spill_cost = MAX (u->spill_cost, v->spill_cost);*/
738   merge_moves (u, v);
739   /* combine add_hardregs's of U and V.  */
740
741   for (wl = v->conflict_list; wl; wl = wl->next)
742     {
743       struct web *pweb = wl->t;
744       /* We don't strictly need to move conflicts between webs which are
745          already coalesced or selected, if we do iterated coalescing, or
746          better if we need not to be able to break aliases again.
747          I.e. normally we would use the condition
748          (pweb->type != SELECT && pweb->type != COALESCED).
749          But for now we simply merge all conflicts.  It doesn't take that
750          much time.  */
751       if (1)
752         {
753           struct web *web = u;
754           int nregs = 1 + v->add_hardregs;
755           if (u->type == PRECOLORED)
756             nregs = HARD_REGNO_NREGS (u->color, GET_MODE (v->orig_x));
757
758           /* For precolored U's we need to make conflicts between V's
759              neighbors and as many hardregs from U as V needed if it gets
760              color U.  For now we approximate this by V->add_hardregs, which
761              could be too much in multi-length classes.  We should really
762              count how many hardregs are needed for V with color U.  When U
763              isn't precolored this loop breaks out after one iteration.  */
764           for (i = 0; i < nregs; i++)
765             {
766               if (u->type == PRECOLORED)
767                 web = hardreg2web[i + u->color];
768               if (wl->sub == NULL)
769                 record_conflict (web, pweb);
770               else
771                 {
772                   struct sub_conflict *sl;
773                   /* So, between V and PWEB there are sub_conflicts.  We
774                      need to relocate those conflicts to be between WEB (==
775                      U when it wasn't precolored) and PWEB.  In the case
776                      only a part of V conflicted with (part of) PWEB we
777                      nevertheless make the new conflict between the whole U
778                      and the (part of) PWEB.  Later we might try to find in
779                      U the correct subpart corresponding (by size and
780                      offset) to the part of V (sl->s) which was the source
781                      of the conflict.  */
782                   for (sl = wl->sub; sl; sl = sl->next)
783                     {
784                       /* Beware: sl->s is no subweb of web (== U) but of V.
785                          We try to search a corresponding subpart of U.
786                          If we found none we let it conflict with the whole U.
787                          Note that find_subweb() only looks for mode and
788                          subreg_byte of the REG rtx but not for the pseudo
789                          reg number (otherwise it would be guaranteed to
790                          _not_ find any subpart).  */
791                       struct web *sweb = NULL;
792                       if (SUBWEB_P (sl->s))
793                         sweb = find_subweb (web, sl->s->orig_x);
794                       if (!sweb)
795                         sweb = web;
796                       record_conflict (sweb, sl->t);
797                     }
798                 }
799               if (u->type != PRECOLORED)
800                 break;
801             }
802           if (pweb->type != SELECT && pweb->type != COALESCED)
803             decrement_degree (pweb, 1 + v->add_hardregs);
804         }
805     }
806
807   /* Now merge the usable_regs together.  */
808   /* XXX That merging might normally make it necessary to
809      adjust add_hardregs, which also means to adjust neighbors.  This can
810      result in making some more webs trivially colorable, (or the opposite,
811      if this increases our add_hardregs).  Because we intersect the
812      usable_regs it should only be possible to decrease add_hardregs.  So a
813      conservative solution for now is to simply don't change it.  */
814   u->use_my_regs = 1;
815   AND_HARD_REG_SET (u->usable_regs, v->usable_regs);
816   u->regclass = reg_class_subunion[u->regclass][v->regclass];
817   /* Count number of possible hardregs.  This might make U a spillweb,
818      but that could also happen, if U and V together had too many
819      conflicts.  */
820   u->num_freedom = hard_regs_count (u->usable_regs);
821   u->num_freedom -= u->add_hardregs;
822   /* The next would mean an invalid coalesced move (both webs have no
823      possible hardreg in common), so abort.  */
824   if (!u->num_freedom)
825     abort();
826
827   if (u->num_conflicts >= NUM_REGS (u)
828       && (u->type == FREEZE || simplify_p (u->type)))
829     {
830       remove_web_from_list (u);
831       put_web (u, SPILL);
832     }
833
834   /* We want the most relaxed combination of spill_temp state.
835      I.e. if any was no spilltemp or a spilltemp2, the result is so too,
836      otherwise if any is short, the result is too.  It remains, when both
837      are normal spilltemps.  */
838   if (v->spill_temp == 0)
839     u->spill_temp = 0;
840   else if (v->spill_temp == 2 && u->spill_temp != 0)
841     u->spill_temp = 2;
842   else if (v->spill_temp == 3 && u->spill_temp == 1)
843     u->spill_temp = 3;
844 }
845
846 /* Attempt to coalesce the first thing on the move worklist.
847    This is used only for iterated coalescing.  */
848
849 static void
850 coalesce ()
851 {
852   struct dlist *d = pop_list (&mv_worklist);
853   struct move *m = DLIST_MOVE (d);
854   struct web *source = alias (m->source_web);
855   struct web *target = alias (m->target_web);
856
857   if (target->type == PRECOLORED)
858     {
859       struct web *h = source;
860       source = target;
861       target = h;
862     }
863   if (source == target)
864     {
865       remove_move (source, m);
866       put_move (m, MV_COALESCED);
867       add_worklist (source);
868     }
869   else if (target->type == PRECOLORED
870            || TEST_BIT (sup_igraph, source->id * num_webs + target->id)
871            || TEST_BIT (sup_igraph, target->id * num_webs + source->id))
872     {
873       remove_move (source, m);
874       remove_move (target, m);
875       put_move (m, CONSTRAINED);
876       add_worklist (source);
877       add_worklist (target);
878     }
879   else if ((source->type == PRECOLORED && ok (target, source))
880            || (source->type != PRECOLORED
881                && conservative (target, source)))
882     {
883       remove_move (source, m);
884       remove_move (target, m);
885       put_move (m, MV_COALESCED);
886       combine (source, target);
887       add_worklist (source);
888     }
889   else
890     put_move (m, ACTIVE);
891 }
892
893 /* Freeze the moves associated with the web.  Used for iterated coalescing.  */
894
895 static void
896 freeze_moves (web)
897      struct web *web;
898 {
899   struct move_list *ml, *ml_next;
900   for (ml = web->moves; ml; ml = ml_next)
901     {
902       struct move *m = ml->move;
903       struct web *src, *dest;
904       ml_next = ml->next;
905       if (m->type == ACTIVE)
906         remove_list (m->dlink, &mv_active);
907       else
908         remove_list (m->dlink, &mv_worklist);
909       put_move (m, FROZEN);
910       remove_move (web, m);
911       src = alias (m->source_web);
912       dest = alias (m->target_web);
913       src = (src == web) ? dest : src;
914       remove_move (src, m);
915       /* XXX GA use the original v, instead of alias(v) */
916       if (!src->moves && src->num_conflicts < NUM_REGS (src))
917         {
918           remove_list (src->dlink, &WEBS(FREEZE));
919           put_web (src, SIMPLIFY);
920         }
921     }
922 }
923
924 /* Freeze the first thing on the freeze worklist (only for iterated
925    coalescing).  */
926
927 static void
928 freeze ()
929 {
930   struct dlist *d = pop_list (&WEBS(FREEZE));
931   put_web (DLIST_WEB (d), SIMPLIFY);
932   freeze_moves (DLIST_WEB (d));
933 }
934
935 /* The current spill heuristic.  Returns a number for a WEB.
936    Webs with higher numbers are selected later.  */
937
938 static unsigned HOST_WIDE_INT (*spill_heuristic) PARAMS ((struct web *));
939
940 static unsigned HOST_WIDE_INT default_spill_heuristic PARAMS ((struct web *));
941
942 /* Our default heuristic is similar to spill_cost / num_conflicts.
943    Just scaled for integer arithmetic, and it favors coalesced webs,
944    and webs which span more insns with deaths.  */
945
946 static unsigned HOST_WIDE_INT
947 default_spill_heuristic (web)
948      struct web *web;
949 {
950   unsigned HOST_WIDE_INT ret;
951   unsigned int divisor = 1;
952   /* Make coalesce targets cheaper to spill, because they will be broken
953      up again into smaller parts.  */
954   if (flag_ra_break_aliases)
955     divisor += web->num_aliased;
956   divisor += web->num_conflicts;
957   ret = ((web->spill_cost << 8) + divisor - 1) / divisor;
958   /* It is better to spill webs that span more insns (deaths in our
959      case) than other webs with the otherwise same spill_cost.  So make
960      them a little bit cheaper.  Remember that spill_cost is unsigned.  */
961   if (web->span_deaths < ret)
962     ret -= web->span_deaths;
963   return ret;
964 }
965
966 /* Select the cheapest spill to be potentially spilled (we don't
967    *actually* spill until we need to).  */
968
969 static void
970 select_spill ()
971 {
972   unsigned HOST_WIDE_INT best = (unsigned HOST_WIDE_INT) -1;
973   struct dlist *bestd = NULL;
974   unsigned HOST_WIDE_INT best2 = (unsigned HOST_WIDE_INT) -1;
975   struct dlist *bestd2 = NULL;
976   struct dlist *d;
977   for (d = WEBS(SPILL); d; d = d->next)
978     {
979       struct web *w = DLIST_WEB (d);
980       unsigned HOST_WIDE_INT cost = spill_heuristic (w);
981       if ((!w->spill_temp) && cost < best)
982         {
983           best = cost;
984           bestd = d;
985         }
986       /* Specially marked spill temps can be spilled.  Also coalesce
987          targets can.  Eventually they will be broken up later in the
988          colorizing process, so if we have nothing better take that.  */
989       else if ((w->spill_temp == 2 || w->is_coalesced) && cost < best2)
990         {
991           best2 = cost;
992           bestd2 = d;
993         }
994     }
995   if (!bestd)
996     {
997       bestd = bestd2;
998       best = best2;
999     }
1000   if (!bestd)
1001     abort ();
1002
1003   /* Note the potential spill.  */
1004   DLIST_WEB (bestd)->was_spilled = 1;
1005   remove_list (bestd, &WEBS(SPILL));
1006   put_web (DLIST_WEB (bestd), SIMPLIFY);
1007   freeze_moves (DLIST_WEB (bestd));
1008   ra_debug_msg (DUMP_PROCESS, " potential spill web %3d, conflicts = %d\n",
1009              DLIST_WEB (bestd)->id, DLIST_WEB (bestd)->num_conflicts);
1010 }
1011
1012 /* Given a set of forbidden colors to begin at, and a set of still
1013    free colors, and MODE, returns nonzero of color C is still usable.  */
1014
1015 static int
1016 color_usable_p (c, dont_begin_colors, free_colors, mode)
1017      int c;
1018      HARD_REG_SET dont_begin_colors, free_colors;
1019      enum machine_mode mode;
1020 {
1021   if (!TEST_HARD_REG_BIT (dont_begin_colors, c)
1022       && TEST_HARD_REG_BIT (free_colors, c)
1023       && HARD_REGNO_MODE_OK (c, mode))
1024     {
1025       int i, size;
1026       size = HARD_REGNO_NREGS (c, mode);
1027       for (i = 1; i < size && TEST_HARD_REG_BIT (free_colors, c + i); i++);
1028       if (i == size)
1029         return 1;
1030     }
1031   return 0;
1032 }
1033
1034 /* I don't want to clutter up the actual code with ifdef's.  */
1035 #ifdef REG_ALLOC_ORDER
1036 #define INV_REG_ALLOC_ORDER(c) inv_reg_alloc_order[c]
1037 #else
1038 #define INV_REG_ALLOC_ORDER(c) c
1039 #endif
1040
1041 /* Searches in FREE_COLORS for a block of hardregs of the right length
1042    for MODE, which doesn't begin at a hardreg mentioned in DONT_BEGIN_COLORS.
1043    If it needs more than one hardreg it prefers blocks beginning
1044    at an even hardreg, and only gives an odd begin reg if no other
1045    block could be found.  */
1046
1047 int
1048 get_free_reg (dont_begin_colors, free_colors, mode)
1049      HARD_REG_SET dont_begin_colors, free_colors;
1050      enum machine_mode mode;
1051 {
1052   int c;
1053   int last_resort_reg = -1;
1054   int pref_reg = -1;
1055   int pref_reg_order = INT_MAX;
1056   int last_resort_reg_order = INT_MAX;
1057
1058   for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
1059     if (!TEST_HARD_REG_BIT (dont_begin_colors, c)
1060         && TEST_HARD_REG_BIT (free_colors, c)
1061         && HARD_REGNO_MODE_OK (c, mode))
1062       {
1063         int i, size;
1064         size = HARD_REGNO_NREGS (c, mode);
1065         for (i = 1; i < size && TEST_HARD_REG_BIT (free_colors, c + i); i++);
1066         if (i != size)
1067           {
1068             c += i;
1069             continue;
1070           }
1071         if (i == size)
1072           {
1073             if (size < 2 || (c & 1) == 0)
1074               {
1075                 if (INV_REG_ALLOC_ORDER (c) < pref_reg_order)
1076                   {
1077                     pref_reg = c;
1078                     pref_reg_order = INV_REG_ALLOC_ORDER (c);
1079                   }
1080               }
1081             else if (INV_REG_ALLOC_ORDER (c) < last_resort_reg_order)
1082               {
1083                 last_resort_reg = c;
1084                 last_resort_reg_order = INV_REG_ALLOC_ORDER (c);
1085               }
1086           }
1087         else
1088           c += i;
1089       }
1090   return pref_reg >= 0 ? pref_reg : last_resort_reg;
1091 }
1092
1093 /* Similar to get_free_reg(), but first search in colors provided
1094    by BIAS _and_ PREFER_COLORS, then in BIAS alone, then in PREFER_COLORS
1095    alone, and only then for any free color.  If flag_ra_biased is zero
1096    only do the last two steps.  */
1097
1098 static int
1099 get_biased_reg (dont_begin_colors, bias, prefer_colors, free_colors, mode)
1100      HARD_REG_SET dont_begin_colors, bias, prefer_colors, free_colors;
1101      enum machine_mode mode;
1102 {
1103   int c = -1;
1104   HARD_REG_SET s;
1105   if (flag_ra_biased)
1106     {
1107       COPY_HARD_REG_SET (s, dont_begin_colors);
1108       IOR_COMPL_HARD_REG_SET (s, bias);
1109       IOR_COMPL_HARD_REG_SET (s, prefer_colors);
1110       c = get_free_reg (s, free_colors, mode);
1111       if (c >= 0)
1112         return c;
1113       COPY_HARD_REG_SET (s, dont_begin_colors);
1114       IOR_COMPL_HARD_REG_SET (s, bias);
1115       c = get_free_reg (s, free_colors, mode);
1116       if (c >= 0)
1117         return c;
1118     }
1119   COPY_HARD_REG_SET (s, dont_begin_colors);
1120   IOR_COMPL_HARD_REG_SET (s, prefer_colors);
1121   c = get_free_reg (s, free_colors, mode);
1122   if (c >= 0)
1123       return c;
1124   c = get_free_reg (dont_begin_colors, free_colors, mode);
1125   return c;
1126 }
1127
1128 /* Counts the number of non-overlapping bitblocks of length LEN
1129    in FREE_COLORS.  */
1130
1131 static int
1132 count_long_blocks (free_colors, len)
1133      HARD_REG_SET free_colors;
1134      int len;
1135 {
1136   int i, j;
1137   int count = 0;
1138   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1139     {
1140       if (!TEST_HARD_REG_BIT (free_colors, i))
1141         continue;
1142       for (j = 1; j < len; j++)
1143         if (!TEST_HARD_REG_BIT (free_colors, i + j))
1144           break;
1145       /* Bits [i .. i+j-1] are free.  */
1146       if (j == len)
1147         count++;
1148       i += j - 1;
1149     }
1150   return count;
1151 }
1152
1153 /* Given a hardreg set S, return a string representing it.
1154    Either as 0/1 string, or as hex value depending on the implementation
1155    of hardreg sets.  Note that this string is statically allocated.  */
1156
1157 static char *
1158 hardregset_to_string (s)
1159      HARD_REG_SET s;
1160 {
1161   static char string[/*FIRST_PSEUDO_REGISTER + 30*/1024];
1162 #if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDE_INT
1163   sprintf (string, HOST_WIDE_INT_PRINT_HEX, s);
1164 #else
1165   char *c = string;
1166   int i,j;
1167   c += sprintf (c, "{ ");
1168   for (i = 0;i < HARD_REG_SET_LONGS; i++)
1169     {
1170       for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
1171           c += sprintf (c, "%s", ( 1 << j) & s[i] ? "1" : "0");
1172       c += sprintf (c, "%s", i ? ", " : "");
1173     }
1174   c += sprintf (c, " }");
1175 #endif
1176   return string;
1177 }
1178
1179 /* For WEB, look at its already colored neighbors, and calculate
1180    the set of hardregs which is not allowed as color for WEB.  Place
1181    that set int *RESULT.  Note that the set of forbidden begin colors
1182    is not the same as all colors taken up by neighbors.  E.g. suppose
1183    two DImode webs, but only the lo-part from one conflicts with the
1184    hipart from the other, and suppose the other gets colors 2 and 3
1185    (it needs two SImode hardregs).  Now the first can take also color
1186    1 or 2, although in those cases there's a partial overlap.  Only
1187    3 can't be used as begin color.  */
1188
1189 static void
1190 calculate_dont_begin (web, result)
1191      struct web *web;
1192      HARD_REG_SET *result;
1193 {
1194   struct conflict_link *wl;
1195   HARD_REG_SET dont_begin;
1196   /* The bits set in dont_begin correspond to the hardregs, at which
1197      WEB may not begin.  This differs from the set of _all_ hardregs which
1198      are taken by WEB's conflicts in the presence of wide webs, where only
1199      some parts conflict with others.  */
1200   CLEAR_HARD_REG_SET (dont_begin);
1201   for (wl = web->conflict_list; wl; wl = wl->next)
1202     {
1203       struct web *w;
1204       struct web *ptarget = alias (wl->t);
1205       struct sub_conflict *sl = wl->sub;
1206       w = sl ? sl->t : wl->t;
1207       while (w)
1208         {
1209           if (ptarget->type == COLORED || ptarget->type == PRECOLORED)
1210             {
1211               struct web *source = (sl) ? sl->s : web;
1212               unsigned int tsize = HARD_REGNO_NREGS (ptarget->color,
1213                                                      GET_MODE (w->orig_x));
1214               /* ssize is only a first guess for the size.  */
1215               unsigned int ssize = HARD_REGNO_NREGS (ptarget->color, GET_MODE
1216                                                      (source->orig_x));
1217               unsigned int tofs = 0;
1218               unsigned int sofs = 0;
1219               /* C1 and C2 can become negative, so unsigned
1220                  would be wrong.  */
1221               int c1, c2;
1222
1223               if (SUBWEB_P (w)
1224                   && GET_MODE_SIZE (GET_MODE (w->orig_x)) >= UNITS_PER_WORD)
1225                 tofs = (SUBREG_BYTE (w->orig_x) / UNITS_PER_WORD);
1226               if (SUBWEB_P (source)
1227                   && GET_MODE_SIZE (GET_MODE (source->orig_x))
1228                      >= UNITS_PER_WORD)
1229                 sofs = (SUBREG_BYTE (source->orig_x) / UNITS_PER_WORD);
1230               c1 = ptarget->color + tofs - sofs - ssize + 1;
1231               c2 = ptarget->color + tofs + tsize - 1 - sofs;
1232               if (c2 >= 0)
1233                 {
1234                   if (c1 < 0)
1235                     c1 = 0;
1236                   /* Because ssize was only guessed above, which influenced our
1237                      begin color (c1), we need adjustment, if for that color
1238                      another size would be needed.  This is done by moving
1239                      c1 to a place, where the last of sources hardregs does not
1240                      overlap the first of targets colors.  */
1241                   while (c1 + sofs
1242                          + HARD_REGNO_NREGS (c1, GET_MODE (source->orig_x)) - 1
1243                          < ptarget->color + tofs)
1244                     c1++;
1245                   while (c1 > 0 && c1 + sofs
1246                          + HARD_REGNO_NREGS (c1, GET_MODE (source->orig_x)) - 1
1247                          > ptarget->color + tofs)
1248                     c1--;
1249                   for (; c1 <= c2; c1++)
1250                     SET_HARD_REG_BIT (dont_begin, c1);
1251                 }
1252             }
1253           /* The next if() only gets true, if there was no wl->sub at all, in
1254              which case we are only making one go thru this loop with W being
1255              a whole web.  */
1256           if (!sl)
1257             break;
1258           sl = sl->next;
1259           w = sl ? sl->t : NULL;
1260         }
1261     }
1262   COPY_HARD_REG_SET (*result, dont_begin);
1263 }
1264
1265 /* Try to assign a color to WEB.  If HARD if nonzero, we try many
1266    tricks to get it one color, including respilling already colored
1267    neighbors.
1268
1269    We also trie very hard, to not constrain the uncolored non-spill
1270    neighbors, which need more hardregs than we.  Consider a situation, 2
1271    hardregs free for us (0 and 1), and one of our neighbors needs 2
1272    hardregs, and only conflicts with us.  There are 3 hardregs at all.  Now
1273    a simple minded method might choose 1 as color for us.  Then our neighbor
1274    has two free colors (0 and 2) as it should, but they are not consecutive,
1275    so coloring it later would fail.  This leads to nasty problems on
1276    register starved machines, so we try to avoid this.  */
1277
1278 static void
1279 colorize_one_web (web, hard)
1280      struct web *web;
1281      int hard;
1282 {
1283   struct conflict_link *wl;
1284   HARD_REG_SET colors, dont_begin;
1285   int c = -1;
1286   int bestc = -1;
1287   int neighbor_needs= 0;
1288   struct web *fat_neighbor = NULL;
1289   struct web *fats_parent = NULL;
1290   int num_fat = 0;
1291   int long_blocks = 0;
1292   int best_long_blocks = -1;
1293   HARD_REG_SET fat_colors;
1294   HARD_REG_SET bias;
1295
1296   if (web->regno >= max_normal_pseudo)
1297     hard = 0;
1298
1299   /* First we want to know the colors at which we can't begin.  */
1300   calculate_dont_begin (web, &dont_begin);
1301   CLEAR_HARD_REG_SET (bias);
1302
1303   /* Now setup the set of colors used by our neighbors neighbors,
1304      and search the biggest noncolored neighbor.  */
1305   neighbor_needs = web->add_hardregs + 1;
1306   for (wl = web->conflict_list; wl; wl = wl->next)
1307     {
1308       struct web *w;
1309       struct web *ptarget = alias (wl->t);
1310       struct sub_conflict *sl = wl->sub;
1311       IOR_HARD_REG_SET (bias, ptarget->bias_colors);
1312       w = sl ? sl->t : wl->t;
1313       if (ptarget->type != COLORED && ptarget->type != PRECOLORED
1314           && !ptarget->was_spilled)
1315         while (w)
1316           {
1317             if (find_web_for_subweb (w)->type != COALESCED
1318                 && w->add_hardregs >= neighbor_needs)
1319               {
1320                 neighbor_needs = w->add_hardregs;
1321                 fat_neighbor = w;
1322                 fats_parent = ptarget;
1323                 num_fat++;
1324               }
1325             if (!sl)
1326               break;
1327             sl = sl->next;
1328             w = sl ? sl->t : NULL;
1329           }
1330     }
1331
1332   ra_debug_msg (DUMP_COLORIZE, "colorize web %d [don't begin at %s]", web->id,
1333              hardregset_to_string (dont_begin));
1334
1335   /* If there are some fat neighbors, remember their usable regs,
1336      and how many blocks are free in it for that neighbor.  */
1337   if (num_fat)
1338     {
1339       COPY_HARD_REG_SET (fat_colors, fats_parent->usable_regs);
1340       long_blocks = count_long_blocks (fat_colors, neighbor_needs + 1);
1341     }
1342
1343   /* We break out, if we found a color which doesn't constrain
1344      neighbors, or if we can't find any colors.  */
1345   while (1)
1346     {
1347       HARD_REG_SET call_clobbered;
1348
1349       /* Here we choose a hard-reg for the current web.  For non spill
1350          temporaries we first search in the hardregs for it's prefered
1351          class, then, if we found nothing appropriate, in those of the
1352          alternate class.  For spill temporaries we only search in
1353          usable_regs of this web (which is probably larger than that of
1354          the preferred or alternate class).  All searches first try to
1355          find a non-call-clobbered hard-reg.
1356          XXX this should be more finegraned... First look into preferred
1357          non-callclobbered hardregs, then _if_ the web crosses calls, in
1358          alternate non-cc hardregs, and only _then_ also in preferred cc
1359          hardregs (and alternate ones).  Currently we don't track the number
1360          of calls crossed for webs.  We should.  */
1361       if (web->use_my_regs)
1362         {
1363           COPY_HARD_REG_SET (colors, web->usable_regs);
1364           AND_HARD_REG_SET (colors,
1365                             usable_regs[reg_preferred_class (web->regno)]);
1366         }
1367       else
1368         COPY_HARD_REG_SET (colors,
1369                            usable_regs[reg_preferred_class (web->regno)]);
1370 #ifdef CLASS_CANNOT_CHANGE_MODE
1371       if (web->mode_changed)
1372         AND_COMPL_HARD_REG_SET (colors, reg_class_contents[
1373                                   (int) CLASS_CANNOT_CHANGE_MODE]);
1374 #endif
1375       COPY_HARD_REG_SET (call_clobbered, colors);
1376       AND_HARD_REG_SET (call_clobbered, call_used_reg_set);
1377
1378       /* If this web got a color in the last pass, try to give it the
1379          same color again.  This will to much better colorization
1380          down the line, as we spilled for a certain coloring last time.  */
1381       if (web->old_color)
1382         {
1383           c = web->old_color - 1;
1384           if (!color_usable_p (c, dont_begin, colors,
1385                                PSEUDO_REGNO_MODE (web->regno)))
1386             c = -1;
1387         }
1388       else
1389         c = -1;
1390       if (c < 0)
1391         c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1392                             call_clobbered, PSEUDO_REGNO_MODE (web->regno));
1393       if (c < 0)
1394         c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1395                           colors, PSEUDO_REGNO_MODE (web->regno));
1396
1397       if (c < 0)
1398         {
1399           if (web->use_my_regs)
1400             IOR_HARD_REG_SET (colors, web->usable_regs);
1401           else
1402             IOR_HARD_REG_SET (colors, usable_regs
1403                               [reg_alternate_class (web->regno)]);
1404 #ifdef CLASS_CANNOT_CHANGE_MODE
1405           if (web->mode_changed)
1406             AND_COMPL_HARD_REG_SET (colors, reg_class_contents[
1407                                       (int) CLASS_CANNOT_CHANGE_MODE]);
1408 #endif
1409           COPY_HARD_REG_SET (call_clobbered, colors);
1410           AND_HARD_REG_SET (call_clobbered, call_used_reg_set);
1411
1412           c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1413                             call_clobbered, PSEUDO_REGNO_MODE (web->regno));
1414           if (c < 0)
1415             c = get_biased_reg (dont_begin, bias, web->prefer_colors,
1416                               colors, PSEUDO_REGNO_MODE (web->regno));
1417         }
1418       if (c < 0)
1419         break;
1420       if (bestc < 0)
1421         bestc = c;
1422       /* If one of the yet uncolored neighbors, which is not a potential
1423          spill needs a block of hardregs be sure, not to destroy such a block
1424          by coloring one reg in the middle.  */
1425       if (num_fat)
1426         {
1427           int i;
1428           int new_long;
1429           HARD_REG_SET colors1;
1430           COPY_HARD_REG_SET (colors1, fat_colors);
1431           for (i = 0; i < 1 + web->add_hardregs; i++)
1432             CLEAR_HARD_REG_BIT (colors1, c + i);
1433           new_long = count_long_blocks (colors1, neighbor_needs + 1);
1434           /* If we changed the number of long blocks, and it's now smaller
1435              than needed, we try to avoid this color.  */
1436           if (long_blocks != new_long && new_long < num_fat)
1437             {
1438               if (new_long > best_long_blocks)
1439                 {
1440                   best_long_blocks = new_long;
1441                   bestc = c;
1442                 }
1443               SET_HARD_REG_BIT (dont_begin, c);
1444               ra_debug_msg (DUMP_COLORIZE, " avoid %d", c);
1445             }
1446           else
1447             /* We found a color which doesn't destroy a block.  */
1448             break;
1449         }
1450       /* If we havee no fat neighbors, the current color won't become
1451          "better", so we've found it.  */
1452       else
1453         break;
1454     }
1455   ra_debug_msg (DUMP_COLORIZE, " --> got %d", c < 0 ? bestc : c);
1456   if (bestc >= 0 && c < 0 && !web->was_spilled)
1457     {
1458       /* This is a non-potential-spill web, which got a color, which did
1459          destroy a hardreg block for one of it's neighbors.  We color
1460          this web anyway and hope for the best for the neighbor, if we are
1461          a spill temp.  */
1462       if (1 || web->spill_temp)
1463         c = bestc;
1464       ra_debug_msg (DUMP_COLORIZE, " [constrains neighbors]");
1465     }
1466   ra_debug_msg (DUMP_COLORIZE, "\n");
1467
1468   if (c < 0)
1469     {
1470       /* Guard against a simplified node being spilled.  */
1471       /* Don't abort.  This can happen, when e.g. enough registers
1472          are available in colors, but they are not consecutive.  This is a
1473          very serious issue if this web is a short live one, because
1474          even if we spill this one here, the situation won't become better
1475          in the next iteration.  It probably will have the same conflicts,
1476          those will have the same colors, and we would come here again, for
1477          all parts, in which this one gets splitted by the spill.  This
1478          can result in endless iteration spilling the same register again and
1479          again.  That's why we try to find a neighbor, which spans more
1480          instructions that ourself, and got a color, and try to spill _that_.
1481
1482          if (DLIST_WEB (d)->was_spilled < 0)
1483          abort (); */
1484       if (hard && (!web->was_spilled || web->spill_temp))
1485         {
1486           unsigned int loop;
1487           struct web *try = NULL;
1488           struct web *candidates[8];
1489
1490           ra_debug_msg (DUMP_COLORIZE, "  *** %d spilled, although %s ***\n",
1491                      web->id, web->spill_temp ? "spilltemp" : "non-spill");
1492           /* We make multiple passes over our conflicts, first trying to
1493              spill those webs, which only got a color by chance, but
1494              were potential spill ones, and if that isn't enough, in a second
1495              pass also to spill normal colored webs.  If we still didn't find
1496              a candidate, but we are a spill-temp, we make a third pass
1497              and include also webs, which were targets for coalescing, and
1498              spill those.  */
1499           memset (candidates, 0, sizeof candidates);
1500 #define set_cand(i, w) \
1501           do { \
1502               if (!candidates[(i)] \
1503                   || (candidates[(i)]->spill_cost < (w)->spill_cost)) \
1504                 candidates[(i)] = (w); \
1505           } while (0)
1506           for (wl = web->conflict_list; wl; wl = wl->next)
1507             {
1508               struct web *w = wl->t;
1509               struct web *aw = alias (w);
1510               /* If we are a spill-temp, we also look at webs coalesced
1511                  to precolored ones.  Otherwise we only look at webs which
1512                  themself were colored, or coalesced to one.  */
1513               if (aw->type == PRECOLORED && w != aw && web->spill_temp
1514                   && flag_ra_optimistic_coalescing)
1515                 {
1516                   if (!w->spill_temp)
1517                     set_cand (4, w);
1518                   else if (web->spill_temp == 2
1519                            && w->spill_temp == 2
1520                            && w->spill_cost < web->spill_cost)
1521                     set_cand (5, w);
1522                   else if (web->spill_temp != 2
1523                            && (w->spill_temp == 2
1524                                || w->spill_cost < web->spill_cost))
1525                     set_cand (6, w);
1526                   continue;
1527                 }
1528               if (aw->type != COLORED)
1529                 continue;
1530               if (w->type == COLORED && !w->spill_temp && !w->is_coalesced
1531                   && w->was_spilled)
1532                 {
1533                   if (w->spill_cost < web->spill_cost)
1534                     set_cand (0, w);
1535                   else if (web->spill_temp)
1536                     set_cand (1, w);
1537                 }
1538               if (w->type == COLORED && !w->spill_temp && !w->is_coalesced
1539                   && !w->was_spilled)
1540                 {
1541                   if (w->spill_cost < web->spill_cost)
1542                     set_cand (2, w);
1543                   else if (web->spill_temp && web->spill_temp != 2)
1544                     set_cand (3, w);
1545                 }
1546               if (web->spill_temp)
1547                 {
1548                   if (w->type == COLORED && w->spill_temp == 2
1549                       && !w->is_coalesced
1550                       && (w->spill_cost < web->spill_cost
1551                           || web->spill_temp != 2))
1552                     set_cand (4, w);
1553                   if (!aw->spill_temp)
1554                     set_cand (5, aw);
1555                   if (aw->spill_temp == 2
1556                       && (aw->spill_cost < web->spill_cost
1557                           || web->spill_temp != 2))
1558                     set_cand (6, aw);
1559                   /* For boehm-gc/misc.c.  If we are a difficult spilltemp,
1560                      also coalesced neighbors are a chance, _even_ if they
1561                      too are spilltemps.  At least their coalscing can be
1562                      broken up, which may be reset usable_regs, and makes
1563                      it easier colorable.  */
1564                   if (web->spill_temp != 2 && aw->is_coalesced
1565                       && flag_ra_optimistic_coalescing)
1566                     set_cand (7, aw);
1567                 }
1568             }
1569           for (loop = 0; try == NULL && loop < 8; loop++)
1570             if (candidates[loop])
1571               try = candidates[loop];
1572 #undef set_cand
1573           if (try)
1574             {
1575               int old_c = try->color;
1576               if (try->type == COALESCED)
1577                 {
1578                   if (alias (try)->type != PRECOLORED)
1579                     abort ();
1580                   ra_debug_msg (DUMP_COLORIZE, "  breaking alias %d -> %d\n",
1581                              try->id, alias (try)->id);
1582                   break_precolored_alias (try);
1583                   colorize_one_web (web, hard);
1584                 }
1585               else
1586                 {
1587                   remove_list (try->dlink, &WEBS(COLORED));
1588                   put_web (try, SPILLED);
1589                   /* Now try to colorize us again.  Can recursively make other
1590                      webs also spill, until there are no more unspilled
1591                      neighbors.  */
1592                   ra_debug_msg (DUMP_COLORIZE, "  trying to spill %d\n", try->id);
1593                   colorize_one_web (web, hard);
1594                   if (web->type != COLORED)
1595                     {
1596                       /* We tried recursively to spill all already colored
1597                          neighbors, but we are still uncolorable.  So it made
1598                          no sense to spill those neighbors.  Recolor them.  */
1599                       remove_list (try->dlink, &WEBS(SPILLED));
1600                       put_web (try, COLORED);
1601                       try->color = old_c;
1602                       ra_debug_msg (DUMP_COLORIZE,
1603                                     "  spilling %d was useless\n", try->id);
1604                     }
1605                   else
1606                     {
1607                       ra_debug_msg (DUMP_COLORIZE,
1608                                     "  to spill %d was a good idea\n",
1609                                     try->id);
1610                       remove_list (try->dlink, &WEBS(SPILLED));
1611                       if (try->was_spilled)
1612                         colorize_one_web (try, 0);
1613                       else
1614                         colorize_one_web (try, hard - 1);
1615                     }
1616                 }
1617             }
1618           else
1619             /* No more chances to get a color, so give up hope and
1620                spill us.  */
1621             put_web (web, SPILLED);
1622         }
1623       else
1624         put_web (web, SPILLED);
1625     }
1626   else
1627     {
1628       put_web (web, COLORED);
1629       web->color = c;
1630       if (flag_ra_biased)
1631         {
1632           int nregs = HARD_REGNO_NREGS (c, GET_MODE (web->orig_x));
1633           for (wl = web->conflict_list; wl; wl = wl->next)
1634             {
1635               struct web *ptarget = alias (wl->t);
1636               int i;
1637               for (i = 0; i < nregs; i++)
1638                 SET_HARD_REG_BIT (ptarget->bias_colors, c + i);
1639             }
1640         }
1641     }
1642   if (web->regno >= max_normal_pseudo && web->type == SPILLED)
1643     {
1644       web->color = an_unusable_color;
1645       remove_list (web->dlink, &WEBS(SPILLED));
1646       put_web (web, COLORED);
1647     }
1648   if (web->type == SPILLED && flag_ra_optimistic_coalescing
1649       && web->is_coalesced)
1650     {
1651       ra_debug_msg (DUMP_COLORIZE, "breaking aliases to web %d:", web->id);
1652       restore_conflicts_from_coalesce (web);
1653       break_aliases_to_web (web);
1654       insert_coalesced_conflicts ();
1655       ra_debug_msg (DUMP_COLORIZE, "\n");
1656       remove_list (web->dlink, &WEBS(SPILLED));
1657       put_web (web, SELECT);
1658       web->color = -1;
1659     }
1660 }
1661
1662 /* Assign the colors to all nodes on the select stack.  And update the
1663    colors of coalesced webs.  */
1664
1665 static void
1666 assign_colors ()
1667 {
1668   struct dlist *d;
1669
1670   while (WEBS(SELECT))
1671     {
1672       struct web *web;
1673       d = pop_list (&WEBS(SELECT));
1674       web = DLIST_WEB (d);
1675       colorize_one_web (DLIST_WEB (d), 1);
1676     }
1677
1678   for (d = WEBS(COALESCED); d; d = d->next)
1679     {
1680       struct web *a = alias (DLIST_WEB (d));
1681       DLIST_WEB (d)->color = a->color;
1682     }
1683 }
1684
1685 /* WEB is a spilled web.  Look if we can improve the cost of the graph,
1686    by coloring WEB, even if we then need to spill some of it's neighbors.
1687    For this we calculate the cost for each color C, that results when we
1688    _would_ give WEB color C (i.e. the cost of the then spilled neighbors).
1689    If the lowest cost among them is smaller than the spillcost of WEB, we
1690    do that recoloring, and instead spill the neighbors.
1691
1692    This can sometime help, when due to irregularities in register file,
1693    and due to multi word pseudos, the colorization is suboptimal.  But
1694    be aware, that currently this pass is quite slow.  */
1695
1696 static void
1697 try_recolor_web (web)
1698      struct web *web;
1699 {
1700   struct conflict_link *wl;
1701   unsigned HOST_WIDE_INT *cost_neighbors;
1702   unsigned int *min_color;
1703   int newcol, c;
1704   HARD_REG_SET precolored_neighbors, spill_temps;
1705   HARD_REG_SET possible_begin, wide_seen;
1706   cost_neighbors = (unsigned HOST_WIDE_INT *)
1707     xcalloc (FIRST_PSEUDO_REGISTER, sizeof (cost_neighbors[0]));
1708   /* For each hard-regs count the number of preceding hardregs, which
1709      would overlap this color, if used in WEB's mode.  */
1710   min_color = (unsigned int *) xcalloc (FIRST_PSEUDO_REGISTER, sizeof (int));
1711   CLEAR_HARD_REG_SET (possible_begin);
1712   for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
1713     {
1714       int i, nregs;
1715       if (!HARD_REGNO_MODE_OK (c, GET_MODE (web->orig_x)))
1716         continue;
1717       nregs = HARD_REGNO_NREGS (c, GET_MODE (web->orig_x));
1718       for (i = 0; i < nregs; i++)
1719         if (!TEST_HARD_REG_BIT (web->usable_regs, c + i))
1720           break;
1721       if (i < nregs || nregs == 0)
1722         continue;
1723       SET_HARD_REG_BIT (possible_begin, c);
1724       for (; nregs--;)
1725         if (!min_color[c + nregs])
1726           min_color[c + nregs] = 1 + c;
1727     }
1728   CLEAR_HARD_REG_SET (precolored_neighbors);
1729   CLEAR_HARD_REG_SET (spill_temps);
1730   CLEAR_HARD_REG_SET (wide_seen);
1731   for (wl = web->conflict_list; wl; wl = wl->next)
1732     {
1733       HARD_REG_SET dont_begin;
1734       struct web *web2 = alias (wl->t);
1735       struct conflict_link *nn;
1736       int c1, c2;
1737       int wide_p = 0;
1738       if (wl->t->type == COALESCED || web2->type != COLORED)
1739         {
1740           if (web2->type == PRECOLORED)
1741             {
1742               c1 = min_color[web2->color];
1743               c1 = (c1 == 0) ? web2->color : (c1 - 1);
1744               c2 = web2->color;
1745               for (; c1 <= c2; c1++)
1746                 SET_HARD_REG_BIT (precolored_neighbors, c1);
1747             }
1748           continue;
1749         }
1750       /* Mark colors for which some wide webs are involved.  For
1751          those the independent sets are not simply one-node graphs, so
1752          they can't be recolored independ from their neighborhood.  This
1753          means, that our cost calculation can be incorrect (assuming it
1754          can avoid spilling a web because it thinks some colors are available,
1755          although it's neighbors which itself need recoloring might take
1756          away exactly those colors).  */
1757       if (web2->add_hardregs)
1758         wide_p = 1;
1759       for (nn = web2->conflict_list; nn && !wide_p; nn = nn->next)
1760         if (alias (nn->t)->add_hardregs)
1761           wide_p = 1;
1762       calculate_dont_begin (web2, &dont_begin);
1763       c1 = min_color[web2->color];
1764       /* Note that min_color[] contains 1-based values (zero means
1765          undef).  */
1766       c1 = c1 == 0 ? web2->color : (c1 - 1);
1767       c2 = web2->color + HARD_REGNO_NREGS (web2->color, GET_MODE
1768                                            (web2->orig_x)) - 1;
1769       for (; c1 <= c2; c1++)
1770         if (TEST_HARD_REG_BIT (possible_begin, c1))
1771           {
1772             int nregs;
1773             HARD_REG_SET colors;
1774             nregs = HARD_REGNO_NREGS (c1, GET_MODE (web->orig_x));
1775             COPY_HARD_REG_SET (colors, web2->usable_regs);
1776             for (; nregs--;)
1777               CLEAR_HARD_REG_BIT (colors, c1 + nregs);
1778             if (wide_p)
1779               SET_HARD_REG_BIT (wide_seen, c1);
1780             if (get_free_reg (dont_begin, colors,
1781                               GET_MODE (web2->orig_x)) < 0)
1782               {
1783                 if (web2->spill_temp)
1784                   SET_HARD_REG_BIT (spill_temps, c1);
1785                 else
1786                   cost_neighbors[c1] += web2->spill_cost;
1787               }
1788           }
1789     }
1790   newcol = -1;
1791   for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
1792     if (TEST_HARD_REG_BIT (possible_begin, c)
1793         && !TEST_HARD_REG_BIT (precolored_neighbors, c)
1794         && !TEST_HARD_REG_BIT (spill_temps, c)
1795         && (newcol == -1
1796             || cost_neighbors[c] < cost_neighbors[newcol]))
1797       newcol = c;
1798   if (newcol >= 0 && cost_neighbors[newcol] < web->spill_cost)
1799     {
1800       int nregs = HARD_REGNO_NREGS (newcol, GET_MODE (web->orig_x));
1801       unsigned HOST_WIDE_INT cost = 0;
1802       int *old_colors;
1803       struct conflict_link *wl_next;
1804       ra_debug_msg (DUMP_COLORIZE, "try to set web %d to color %d\n", web->id,
1805                  newcol);
1806       remove_list (web->dlink, &WEBS(SPILLED));
1807       put_web (web, COLORED);
1808       web->color = newcol;
1809       old_colors = (int *) xcalloc (num_webs, sizeof (int));
1810       for (wl = web->conflict_list; wl; wl = wl_next)
1811         {
1812           struct web *web2 = alias (wl->t);
1813           /* If web2 is a coalesce-target, and will become spilled
1814              below in colorize_one_web(), and the current conflict wl
1815              between web and web2 was only the result of that coalescing
1816              this conflict will be deleted, making wl invalid.  So save
1817              the next conflict right now.  Note that if web2 has indeed
1818              such state, then wl->next can not be deleted in this
1819              iteration.  */
1820           wl_next = wl->next;
1821           if (web2->type == COLORED)
1822             {
1823               int nregs2 = HARD_REGNO_NREGS (web2->color, GET_MODE
1824                                              (web2->orig_x));
1825               if (web->color >= web2->color + nregs2
1826                   || web2->color >= web->color + nregs)
1827                 continue;
1828               old_colors[web2->id] = web2->color + 1;
1829               web2->color = -1;
1830               remove_list (web2->dlink, &WEBS(COLORED));
1831               web2->type = SELECT;
1832               /* Allow webs to be spilled.  */
1833               if (web2->spill_temp == 0 || web2->spill_temp == 2)
1834                 web2->was_spilled = 1;
1835               colorize_one_web (web2, 1);
1836               if (web2->type == SPILLED)
1837                 cost += web2->spill_cost;
1838             }
1839         }
1840       /* The actual cost may be smaller than the guessed one, because
1841          partial conflicts could result in some conflicting webs getting
1842          a color, where we assumed it must be spilled.  See the comment
1843          above what happens, when wide webs are involved, and why in that
1844          case there might actually be some webs spilled although thought to
1845          be colorable.  */
1846       if (cost > cost_neighbors[newcol]
1847           && nregs == 1 && !TEST_HARD_REG_BIT (wide_seen, newcol))
1848         abort ();
1849       /* But if the new spill-cost is higher than our own, then really loose.
1850          Respill us and recolor neighbors as before.  */
1851       if (cost > web->spill_cost)
1852         {
1853           ra_debug_msg (DUMP_COLORIZE,
1854                      "reset coloring of web %d, too expensive\n", web->id);
1855           remove_list (web->dlink, &WEBS(COLORED));
1856           web->color = -1;
1857           put_web (web, SPILLED);
1858           for (wl = web->conflict_list; wl; wl = wl->next)
1859             {
1860               struct web *web2 = alias (wl->t);
1861               if (old_colors[web2->id])
1862                 {
1863                   if (web2->type == SPILLED)
1864                     {
1865                       remove_list (web2->dlink, &WEBS(SPILLED));
1866                       web2->color = old_colors[web2->id] - 1;
1867                       put_web (web2, COLORED);
1868                     }
1869                   else if (web2->type == COLORED)
1870                     web2->color = old_colors[web2->id] - 1;
1871                   else if (web2->type == SELECT)
1872                     /* This means, that WEB2 once was a part of a coalesced
1873                        web, which got spilled in the above colorize_one_web()
1874                        call, and whose parts then got splitted and put back
1875                        onto the SELECT stack.  As the cause for that splitting
1876                        (the coloring of WEB) was worthless, we should again
1877                        coalesce the parts, as they were before.  For now we
1878                        simply leave them SELECTed, for our caller to take
1879                        care.  */
1880                     ;
1881                   else
1882                     abort ();
1883                 }
1884             }
1885         }
1886       free (old_colors);
1887     }
1888   free (min_color);
1889   free (cost_neighbors);
1890 }
1891
1892 /* This ensures that all conflicts of coalesced webs are seen from
1893    the webs coalesced into.  combine() only adds the conflicts which
1894    at the time of combining were not already SELECTed or COALESCED
1895    to not destroy num_conflicts.  Here we add all remaining conflicts
1896    and thereby destroy num_conflicts.  This should be used when num_conflicts
1897    isn't used anymore, e.g. on a completely colored graph.  */
1898
1899 static void
1900 insert_coalesced_conflicts ()
1901 {
1902   struct dlist *d;
1903   for (d = WEBS(COALESCED); 0 && d; d = d->next)
1904     {
1905       struct web *web = DLIST_WEB (d);
1906       struct web *aweb = alias (web);
1907       struct conflict_link *wl;
1908       for (wl = web->conflict_list; wl; wl = wl->next)
1909         {
1910           struct web *tweb = aweb;
1911           int i;
1912           int nregs = 1 + web->add_hardregs;
1913           if (aweb->type == PRECOLORED)
1914             nregs = HARD_REGNO_NREGS (aweb->color, GET_MODE (web->orig_x));
1915           for (i = 0; i < nregs; i++)
1916             {
1917               if (aweb->type == PRECOLORED)
1918                 tweb = hardreg2web[i + aweb->color];
1919               /* There might be some conflict edges laying around
1920                  where the usable_regs don't intersect.  This can happen
1921                  when first some webs were coalesced and conflicts
1922                  propagated, then some combining narrowed usable_regs and
1923                  further coalescing ignored those conflicts.  Now there are
1924                  some edges to COALESCED webs but not to it's alias.
1925                  So abort only when they really should conflict.  */
1926               if ((!(tweb->type == PRECOLORED
1927                      || TEST_BIT (sup_igraph, tweb->id * num_webs + wl->t->id))
1928                    || !(wl->t->type == PRECOLORED
1929                         || TEST_BIT (sup_igraph,
1930                                      wl->t->id * num_webs + tweb->id)))
1931                   && hard_regs_intersect_p (&tweb->usable_regs,
1932                                             &wl->t->usable_regs))
1933                 abort ();
1934               /*if (wl->sub == NULL)
1935                 record_conflict (tweb, wl->t);
1936               else
1937                 {
1938                   struct sub_conflict *sl;
1939                   for (sl = wl->sub; sl; sl = sl->next)
1940                     record_conflict (tweb, sl->t);
1941                 }*/
1942               if (aweb->type != PRECOLORED)
1943                 break;
1944             }
1945         }
1946     }
1947 }
1948
1949 /* A function suitable to pass to qsort().  Compare the spill costs
1950    of webs W1 and W2.  When used by qsort, this would order webs with
1951    largest cost first.  */
1952
1953 static int
1954 comp_webs_maxcost (w1, w2)
1955      const void *w1, *w2;
1956 {
1957   struct web *web1 = *(struct web **)w1;
1958   struct web *web2 = *(struct web **)w2;
1959   if (web1->spill_cost > web2->spill_cost)
1960     return -1;
1961   else if (web1->spill_cost < web2->spill_cost)
1962     return 1;
1963   else
1964     return 0;
1965 }
1966
1967 /* This tries to recolor all spilled webs.  See try_recolor_web()
1968    how this is done.  This just calls it for each spilled web.  */
1969
1970 static void
1971 recolor_spills ()
1972 {
1973   unsigned int i, num;
1974   struct web **order2web;
1975   num = num_webs - num_subwebs;
1976   order2web = (struct web **) xmalloc (num * sizeof (order2web[0]));
1977   for (i = 0; i < num; i++)
1978     {
1979       order2web[i] = id2web[i];
1980       /* If we aren't breaking aliases, combine() wasn't merging the
1981          spill_costs.  So do that here to have sane measures.  */
1982       if (!flag_ra_merge_spill_costs && id2web[i]->type == COALESCED)
1983         alias (id2web[i])->spill_cost += id2web[i]->spill_cost;
1984     }
1985   qsort (order2web, num, sizeof (order2web[0]), comp_webs_maxcost);
1986   insert_coalesced_conflicts ();
1987   dump_graph_cost (DUMP_COSTS, "before spill-recolor");
1988   for (i = 0; i < num; i++)
1989     {
1990       struct web *web = order2web[i];
1991       if (web->type == SPILLED)
1992         try_recolor_web (web);
1993     }
1994   /* It might have been decided in try_recolor_web() (in colorize_one_web())
1995      that a coalesced web should be spilled, so it was put on the
1996      select stack.  Those webs need recoloring again, and all remaining
1997      coalesced webs might need their color updated, so simply call
1998      assign_colors() again.  */
1999   assign_colors ();
2000   free (order2web);
2001 }
2002
2003 /* This checks the current color assignment for obvious errors,
2004    like two conflicting webs overlapping in colors, or the used colors
2005    not being in usable regs.  */
2006
2007 static void
2008 check_colors ()
2009 {
2010   unsigned int i;
2011   for (i = 0; i < num_webs - num_subwebs; i++)
2012     {
2013       struct web *web = id2web[i];
2014       struct web *aweb = alias (web);
2015       struct conflict_link *wl;
2016       int nregs, c;
2017       if (aweb->type == SPILLED || web->regno >= max_normal_pseudo)
2018         continue;
2019       else if (aweb->type == COLORED)
2020         nregs = HARD_REGNO_NREGS (aweb->color, GET_MODE (web->orig_x));
2021       else if (aweb->type == PRECOLORED)
2022         nregs = 1;
2023       else
2024         abort ();
2025       /* The color must be valid for the original usable_regs.  */
2026       for (c = 0; c < nregs; c++)
2027         if (!TEST_HARD_REG_BIT (web->usable_regs, aweb->color + c))
2028           abort ();
2029       /* Search the original (pre-coalesce) conflict list.  In the current
2030          one some inprecise conflicts may be noted (due to combine() or
2031          insert_coalesced_conflicts() relocating partial conflicts) making
2032          it look like some wide webs are in conflict and having the same
2033          color.  */
2034       wl = (web->have_orig_conflicts ? web->orig_conflict_list
2035             : web->conflict_list);
2036       for (; wl; wl = wl->next)
2037         if (wl->t->regno >= max_normal_pseudo)
2038           continue;
2039         else if (!wl->sub)
2040           {
2041             struct web *web2 = alias (wl->t);
2042             int nregs2;
2043             if (web2->type == COLORED)
2044               nregs2 = HARD_REGNO_NREGS (web2->color, GET_MODE (web2->orig_x));
2045             else if (web2->type == PRECOLORED)
2046               nregs2 = 1;
2047             else
2048               continue;
2049             if (aweb->color >= web2->color + nregs2
2050                 || web2->color >= aweb->color + nregs)
2051               continue;
2052             abort ();
2053           }
2054         else
2055           {
2056             struct sub_conflict *sl;
2057             int scol = aweb->color;
2058             int tcol = alias (wl->t)->color;
2059             if (alias (wl->t)->type == SPILLED)
2060               continue;
2061             for (sl = wl->sub; sl; sl = sl->next)
2062               {
2063                 int ssize = HARD_REGNO_NREGS (scol, GET_MODE (sl->s->orig_x));
2064                 int tsize = HARD_REGNO_NREGS (tcol, GET_MODE (sl->t->orig_x));
2065                 int sofs = 0, tofs = 0;
2066                 if (SUBWEB_P (sl->t)
2067                     && GET_MODE_SIZE (GET_MODE (sl->t->orig_x)) >= UNITS_PER_WORD)
2068                   tofs = (SUBREG_BYTE (sl->t->orig_x) / UNITS_PER_WORD);
2069                 if (SUBWEB_P (sl->s)
2070                     && GET_MODE_SIZE (GET_MODE (sl->s->orig_x))
2071                        >= UNITS_PER_WORD)
2072                   sofs = (SUBREG_BYTE (sl->s->orig_x) / UNITS_PER_WORD);
2073                 if ((tcol + tofs >= scol + sofs + ssize)
2074                     || (scol + sofs >= tcol + tofs + tsize))
2075                   continue;
2076                 abort ();
2077               }
2078           }
2079     }
2080 }
2081
2082 /* WEB was a coalesced web.  Make it unaliased again, and put it
2083    back onto SELECT stack.  */
2084
2085 static void
2086 unalias_web (web)
2087      struct web *web;
2088 {
2089   web->alias = NULL;
2090   web->is_coalesced = 0;
2091   web->color = -1;
2092   /* Well, initially everything was spilled, so it isn't incorrect,
2093      that also the individual parts can be spilled.
2094      XXX this isn't entirely correct, as we also relaxed the
2095      spill_temp flag in combine(), which might have made components
2096      spill, although they were a short or spilltemp web.  */
2097   web->was_spilled = 1;
2098   remove_list (web->dlink, &WEBS(COALESCED));
2099   /* Spilltemps must be colored right now (i.e. as early as possible),
2100      other webs can be deferred to the end (the code building the
2101      stack assumed that in this stage only one web was colored).  */
2102   if (web->spill_temp && web->spill_temp != 2)
2103     put_web (web, SELECT);
2104   else
2105     put_web_at_end (web, SELECT);
2106 }
2107
2108 /* WEB is a _target_ for coalescing which got spilled.
2109    Break all aliases to WEB, and restore some of its member to the state
2110    they were before coalescing.  Due to the suboptimal structure of
2111    the interference graph we need to go through all coalesced webs.
2112    Somewhen we'll change this to be more sane.  */
2113
2114 static void
2115 break_aliases_to_web (web)
2116      struct web *web;
2117 {
2118   struct dlist *d, *d_next;
2119   if (web->type != SPILLED)
2120     abort ();
2121   for (d = WEBS(COALESCED); d; d = d_next)
2122     {
2123       struct web *other = DLIST_WEB (d);
2124       d_next = d->next;
2125       /* Beware: Don't use alias() here.  We really want to check only
2126          one level of aliasing, i.e. only break up webs directly
2127          aliased to WEB, not also those aliased through other webs.  */
2128       if (other->alias == web)
2129         {
2130           unalias_web (other);
2131           ra_debug_msg (DUMP_COLORIZE, " %d", other->id);
2132         }
2133     }
2134   web->spill_temp = web->orig_spill_temp;
2135   web->spill_cost = web->orig_spill_cost;
2136   /* Beware: The following possibly widens usable_regs again.  While
2137      it was narrower there might have been some conflicts added which got
2138      ignored because of non-intersecting hardregsets.  All those conflicts
2139      would now matter again.  Fortunately we only add conflicts when
2140      coalescing, which is also the time of narrowing.  And we remove all
2141      those added conflicts again now that we unalias this web.
2142      Therefore this is safe to do.  */
2143   COPY_HARD_REG_SET (web->usable_regs, web->orig_usable_regs);
2144   web->is_coalesced = 0;
2145   web->num_aliased = 0;
2146   web->was_spilled = 1;
2147   /* Reset is_coalesced flag for webs which itself are target of coalescing.
2148      It was cleared above if it was coalesced to WEB.  */
2149   for (d = WEBS(COALESCED); d; d = d->next)
2150     DLIST_WEB (d)->alias->is_coalesced = 1;
2151 }
2152
2153 /* WEB is a web coalesced into a precolored one.  Break that alias,
2154    making WEB SELECTed again.  Also restores the conflicts which resulted
2155    from initially coalescing both.  */
2156
2157 static void
2158 break_precolored_alias (web)
2159      struct web *web;
2160 {
2161   struct web *pre = web->alias;
2162   struct conflict_link *wl;
2163   unsigned int c = pre->color;
2164   unsigned int nregs = HARD_REGNO_NREGS (c, GET_MODE (web->orig_x));
2165   if (pre->type != PRECOLORED)
2166     abort ();
2167   unalias_web (web);
2168   /* Now we need to look at each conflict X of WEB, if it conflicts
2169      with [PRE, PRE+nregs), and remove such conflicts, of X has not other
2170      conflicts, which are coalesced into those precolored webs.  */
2171   for (wl = web->conflict_list; wl; wl = wl->next)
2172     {
2173       struct web *x = wl->t;
2174       struct web *y;
2175       unsigned int i;
2176       struct conflict_link *wl2;
2177       struct conflict_link **pcl;
2178       HARD_REG_SET regs;
2179       if (!x->have_orig_conflicts)
2180         continue;
2181       /* First look at which colors can not go away, due to other coalesces
2182          still existing.  */
2183       CLEAR_HARD_REG_SET (regs);
2184       for (i = 0; i < nregs; i++)
2185         SET_HARD_REG_BIT (regs, c + i);
2186       for (wl2 = x->conflict_list; wl2; wl2 = wl2->next)
2187         if (wl2->t->type == COALESCED && alias (wl2->t)->type == PRECOLORED)
2188           CLEAR_HARD_REG_BIT (regs, alias (wl2->t)->color);
2189       /* Now also remove the colors of those conflicts which already
2190          were there before coalescing at all.  */
2191       for (wl2 = x->orig_conflict_list; wl2; wl2 = wl2->next)
2192         if (wl2->t->type == PRECOLORED)
2193           CLEAR_HARD_REG_BIT (regs, wl2->t->color);
2194       /* The colors now still set are those for which WEB was the last
2195          cause, i.e. those which can be removed.  */
2196       y = NULL;
2197       for (i = 0; i < nregs; i++)
2198         if (TEST_HARD_REG_BIT (regs, c + i))
2199           {
2200             struct web *sub;
2201             y = hardreg2web[c + i];
2202             RESET_BIT (sup_igraph, x->id * num_webs + y->id);
2203             RESET_BIT (sup_igraph, y->id * num_webs + x->id);
2204             RESET_BIT (igraph, igraph_index (x->id, y->id));
2205             for (sub = x->subreg_next; sub; sub = sub->subreg_next)
2206               RESET_BIT (igraph, igraph_index (sub->id, y->id));
2207           }
2208       if (!y)
2209         continue;
2210       pcl = &(x->conflict_list);
2211       while (*pcl)
2212         {
2213           struct web *y = (*pcl)->t;
2214           if (y->type != PRECOLORED || !TEST_HARD_REG_BIT (regs, y->color))
2215             pcl = &((*pcl)->next);
2216           else
2217             *pcl = (*pcl)->next;
2218         }
2219     }
2220 }
2221
2222 /* WEB is a spilled web which was target for coalescing.
2223    Delete all interference edges which were added due to that coalescing,
2224    and break up the coalescing.  */
2225
2226 static void
2227 restore_conflicts_from_coalesce (web)
2228      struct web *web;
2229 {
2230   struct conflict_link **pcl;
2231   struct conflict_link *wl;
2232   pcl = &(web->conflict_list);
2233   /* No original conflict list means no conflict was added at all
2234      after building the graph.  So neither we nor any neighbors have
2235      conflicts due to this coalescing.  */
2236   if (!web->have_orig_conflicts)
2237     return;
2238   while (*pcl)
2239     {
2240       struct web *other = (*pcl)->t;
2241       for (wl = web->orig_conflict_list; wl; wl = wl->next)
2242         if (wl->t == other)
2243           break;
2244       if (wl)
2245         {
2246           /* We found this conflict also in the original list, so this
2247              was no new conflict.  */
2248           pcl = &((*pcl)->next);
2249         }
2250       else
2251         {
2252           /* This is a new conflict, so delete it from us and
2253              the neighbor.  */
2254           struct conflict_link **opcl;
2255           struct conflict_link *owl;
2256           struct sub_conflict *sl;
2257           wl = *pcl;
2258           *pcl = wl->next;
2259           if (!other->have_orig_conflicts && other->type != PRECOLORED)
2260             abort ();
2261           for (owl = other->orig_conflict_list; owl; owl = owl->next)
2262             if (owl->t == web)
2263               break;
2264           if (owl)
2265             abort ();
2266           opcl = &(other->conflict_list);
2267           while (*opcl)
2268             {
2269               if ((*opcl)->t == web)
2270                 {
2271                   owl = *opcl;
2272                   *opcl = owl->next;
2273                   break;
2274                 }
2275               else
2276                 {
2277                   opcl = &((*opcl)->next);
2278                 }
2279             }
2280           if (!owl && other->type != PRECOLORED)
2281             abort ();
2282           /* wl and owl contain the edge data to be deleted.  */
2283           RESET_BIT (sup_igraph, web->id * num_webs + other->id);
2284           RESET_BIT (sup_igraph, other->id * num_webs + web->id);
2285           RESET_BIT (igraph, igraph_index (web->id, other->id));
2286           for (sl = wl->sub; sl; sl = sl->next)
2287             RESET_BIT (igraph, igraph_index (sl->s->id, sl->t->id));
2288           if (other->type != PRECOLORED)
2289             {
2290               for (sl = owl->sub; sl; sl = sl->next)
2291                 RESET_BIT (igraph, igraph_index (sl->s->id, sl->t->id));
2292             }
2293         }
2294     }
2295
2296   /* We must restore usable_regs because record_conflict will use it.  */
2297   COPY_HARD_REG_SET (web->usable_regs, web->orig_usable_regs);
2298   /* We might have deleted some conflicts above, which really are still
2299      there (diamond pattern coalescing).  This is because we don't reference
2300      count interference edges but some of them were the result of different
2301      coalesces.  */
2302   for (wl = web->conflict_list; wl; wl = wl->next)
2303     if (wl->t->type == COALESCED)
2304       {
2305         struct web *tweb;
2306         for (tweb = wl->t->alias; tweb; tweb = tweb->alias)
2307           {
2308             if (wl->sub == NULL)
2309               record_conflict (web, tweb);
2310             else
2311               {
2312                 struct sub_conflict *sl;
2313                 for (sl = wl->sub; sl; sl = sl->next)
2314                   {
2315                     struct web *sweb = NULL;
2316                     if (SUBWEB_P (sl->t))
2317                       sweb = find_subweb (tweb, sl->t->orig_x);
2318                     if (!sweb)
2319                       sweb = tweb;
2320                     record_conflict (sl->s, sweb);
2321                   }
2322               }
2323             if (tweb->type != COALESCED)
2324               break;
2325           }
2326       }
2327 }
2328
2329 /* Repeatedly break aliases for spilled webs, which were target for
2330    coalescing, and recolorize the resulting parts.  Do this as long as
2331    there are any spilled coalesce targets.  */
2332
2333 static void
2334 break_coalesced_spills ()
2335 {
2336   int changed = 0;
2337   while (1)
2338     {
2339       struct dlist *d;
2340       struct web *web;
2341       for (d = WEBS(SPILLED); d; d = d->next)
2342         if (DLIST_WEB (d)->is_coalesced)
2343           break;
2344       if (!d)
2345         break;
2346       changed = 1;
2347       web = DLIST_WEB (d);
2348       ra_debug_msg (DUMP_COLORIZE, "breaking aliases to web %d:", web->id);
2349       restore_conflicts_from_coalesce (web);
2350       break_aliases_to_web (web);
2351       /* WEB was a spilled web and isn't anymore.  Everything coalesced
2352          to WEB is now SELECTed and might potentially get a color.
2353          If those other webs were itself targets of coalescing it might be
2354          that there are still some conflicts from aliased webs missing,
2355          because they were added in combine() right into the now
2356          SELECTed web.  So we need to add those missing conflicts here.  */
2357       insert_coalesced_conflicts ();
2358       ra_debug_msg (DUMP_COLORIZE, "\n");
2359       remove_list (d, &WEBS(SPILLED));
2360       put_web (web, SELECT);
2361       web->color = -1;
2362       while (WEBS(SELECT))
2363         {
2364           d = pop_list (&WEBS(SELECT));
2365           colorize_one_web (DLIST_WEB (d), 1);
2366         }
2367     }
2368   if (changed)
2369     {
2370       struct dlist *d;
2371       for (d = WEBS(COALESCED); d; d = d->next)
2372         {
2373           struct web *a = alias (DLIST_WEB (d));
2374           DLIST_WEB (d)->color = a->color;
2375         }
2376     }
2377   dump_graph_cost (DUMP_COSTS, "after alias-breaking");
2378 }
2379
2380 /* A structure for fast hashing of a pair of webs.
2381    Used to cumulate savings (from removing copy insns) for coalesced webs.
2382    All the pairs are also put into a single linked list.  */
2383 struct web_pair
2384 {
2385   struct web_pair *next_hash;
2386   struct web_pair *next_list;
2387   struct web *smaller;
2388   struct web *larger;
2389   unsigned int conflicts;
2390   unsigned HOST_WIDE_INT cost;
2391 };
2392
2393 /* The actual hash table.  */
2394 #define WEB_PAIR_HASH_SIZE 8192
2395 static struct web_pair *web_pair_hash[WEB_PAIR_HASH_SIZE];
2396 static struct web_pair *web_pair_list;
2397 static unsigned int num_web_pairs;
2398
2399 /* Clear the hash table of web pairs.  */
2400
2401 static void
2402 init_web_pairs ()
2403 {
2404   memset (web_pair_hash, 0, sizeof web_pair_hash);
2405   num_web_pairs = 0;
2406   web_pair_list = NULL;
2407 }
2408
2409 /* Given two webs connected by a move with cost COST which together
2410    have CONFLICTS conflicts, add that pair to the hash table, or if
2411    already in, cumulate the costs and conflict number.  */
2412
2413 static void
2414 add_web_pair_cost (web1, web2, cost, conflicts)
2415      struct web *web1, *web2;
2416      unsigned HOST_WIDE_INT cost;
2417      unsigned int conflicts;
2418 {
2419   unsigned int hash;
2420   struct web_pair *p;
2421   if (web1->id > web2->id)
2422     {
2423       struct web *h = web1;
2424       web1 = web2;
2425       web2 = h;
2426     }
2427   hash = (web1->id * num_webs + web2->id) % WEB_PAIR_HASH_SIZE;
2428   for (p = web_pair_hash[hash]; p; p = p->next_hash)
2429     if (p->smaller == web1 && p->larger == web2)
2430       {
2431         p->cost += cost;
2432         p->conflicts += conflicts;
2433         return;
2434       }
2435   p = (struct web_pair *) ra_alloc (sizeof *p);
2436   p->next_hash = web_pair_hash[hash];
2437   p->next_list = web_pair_list;
2438   p->smaller = web1;
2439   p->larger = web2;
2440   p->conflicts = conflicts;
2441   p->cost = cost;
2442   web_pair_hash[hash] = p;
2443   web_pair_list = p;
2444   num_web_pairs++;
2445 }
2446
2447 /* Suitable to be passed to qsort().  Sort web pairs so, that those
2448    with more conflicts and higher cost (which actually is a saving
2449    when the moves are removed) come first.  */
2450
2451 static int
2452 comp_web_pairs (w1, w2)
2453      const void *w1, *w2;
2454 {
2455   struct web_pair *p1 = *(struct web_pair **)w1;
2456   struct web_pair *p2 = *(struct web_pair **)w2;
2457   if (p1->conflicts > p2->conflicts)
2458     return -1;
2459   else if (p1->conflicts < p2->conflicts)
2460     return 1;
2461   else if (p1->cost > p2->cost)
2462     return -1;
2463   else if (p1->cost < p2->cost)
2464     return 1;
2465   else
2466     return 0;
2467 }
2468
2469 /* Given the list of web pairs, begin to combine them from the one
2470    with the most savings.  */
2471
2472 static void
2473 sort_and_combine_web_pairs (for_move)
2474      int for_move;
2475 {
2476   unsigned int i;
2477   struct web_pair **sorted;
2478   struct web_pair *p;
2479   if (!num_web_pairs)
2480     return;
2481   sorted = (struct web_pair **) xmalloc (num_web_pairs * sizeof (sorted[0]));
2482   for (p = web_pair_list, i = 0; p; p = p->next_list)
2483     sorted[i++] = p;
2484   if (i != num_web_pairs)
2485     abort ();
2486   qsort (sorted, num_web_pairs, sizeof (sorted[0]), comp_web_pairs);
2487
2488   /* After combining one pair, we actually should adjust the savings
2489      of the other pairs, if they are connected to one of the just coalesced
2490      pair.  Later.  */
2491   for (i = 0; i < num_web_pairs; i++)
2492     {
2493       struct web *w1, *w2;
2494       p = sorted[i];
2495       w1 = alias (p->smaller);
2496       w2 = alias (p->larger);
2497       if (!for_move && (w1->type == PRECOLORED || w2->type == PRECOLORED))
2498         continue;
2499       else if (w2->type == PRECOLORED)
2500         {
2501           struct web *h = w1;
2502           w1 = w2;
2503           w2 = h;
2504         }
2505       if (w1 != w2
2506           && !TEST_BIT (sup_igraph, w1->id * num_webs + w2->id)
2507           && !TEST_BIT (sup_igraph, w2->id * num_webs + w1->id)
2508           && w2->type != PRECOLORED
2509           && hard_regs_intersect_p (&w1->usable_regs, &w2->usable_regs))
2510           {
2511             if (w1->type != PRECOLORED
2512                 || (w1->type == PRECOLORED && ok (w2, w1)))
2513               combine (w1, w2);
2514             else if (w1->type == PRECOLORED)
2515               SET_HARD_REG_BIT (w2->prefer_colors, w1->color);
2516           }
2517     }
2518   free (sorted);
2519 }
2520
2521 /* Greedily coalesce all moves possible.  Begin with the web pair
2522    giving the most saving if coalesced.  */
2523
2524 static void
2525 aggressive_coalesce ()
2526 {
2527   struct dlist *d;
2528   struct move *m;
2529   init_web_pairs ();
2530   while ((d = pop_list (&mv_worklist)) != NULL)
2531     if ((m = DLIST_MOVE (d)))
2532       {
2533         struct web *s = alias (m->source_web);
2534         struct web *t = alias (m->target_web);
2535         if (t->type == PRECOLORED)
2536           {
2537             struct web *h = s;
2538             s = t;
2539             t = h;
2540           }
2541         if (s != t
2542             && t->type != PRECOLORED
2543             && !TEST_BIT (sup_igraph, s->id * num_webs + t->id)
2544             && !TEST_BIT (sup_igraph, t->id * num_webs + s->id))
2545           {
2546             if ((s->type == PRECOLORED && ok (t, s))
2547                 || s->type != PRECOLORED)
2548               {
2549                 put_move (m, MV_COALESCED);
2550                 add_web_pair_cost (s, t, BLOCK_FOR_INSN (m->insn)->frequency,
2551                                    0);
2552               }
2553             else if (s->type == PRECOLORED)
2554               /* It is !ok(t, s).  But later when coloring the graph it might
2555                  be possible to take that color.  So we remember the preferred
2556                  color to try that first.  */
2557               {
2558                 put_move (m, CONSTRAINED);
2559                 SET_HARD_REG_BIT (t->prefer_colors, s->color);
2560               }
2561           }
2562         else
2563           {
2564             put_move (m, CONSTRAINED);
2565           }
2566       }
2567   sort_and_combine_web_pairs (1);
2568 }
2569
2570 /* This is the difference between optimistic coalescing and
2571    optimistic coalescing+.  Extended coalesce tries to coalesce also
2572    non-conflicting nodes, not related by a move.  The criteria here is,
2573    the one web must be a source, the other a destination of the same insn.
2574    This actually makes sense, as (because they are in the same insn) they
2575    share many of their neighbors, and if they are coalesced, reduce the
2576    number of conflicts of those neighbors by one.  For this we sort the
2577    candidate pairs again according to savings (and this time also conflict
2578    number).
2579
2580    This is also a comparatively slow operation, as we need to go through
2581    all insns, and for each insn, through all defs and uses.  */
2582
2583 static void
2584 extended_coalesce_2 ()
2585 {
2586   rtx insn;
2587   struct ra_insn_info info;
2588   unsigned int n;
2589   init_web_pairs ();
2590   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2591     if (INSN_P (insn) && (info = insn_df[INSN_UID (insn)]).num_defs)
2592       for (n = 0; n < info.num_defs; n++)
2593         {
2594           struct web *dest = def2web[DF_REF_ID (info.defs[n])];
2595           dest = alias (find_web_for_subweb (dest));
2596           if (dest->type != PRECOLORED && dest->regno < max_normal_pseudo)
2597             {
2598               unsigned int n2;
2599               for (n2 = 0; n2 < info.num_uses; n2++)
2600                 {
2601                   struct web *source = use2web[DF_REF_ID (info.uses[n2])];
2602                   source = alias (find_web_for_subweb (source));
2603                   if (source->type != PRECOLORED
2604                       && source != dest
2605                       && source->regno < max_normal_pseudo
2606                       /* Coalesced webs end up using the same REG rtx in
2607                          emit_colors().  So we can only coalesce something
2608                          of equal modes.  */
2609                       && GET_MODE (source->orig_x) == GET_MODE (dest->orig_x)
2610                       && !TEST_BIT (sup_igraph,
2611                                     dest->id * num_webs + source->id)
2612                       && !TEST_BIT (sup_igraph,
2613                                     source->id * num_webs + dest->id)
2614                       && hard_regs_intersect_p (&source->usable_regs,
2615                                                 &dest->usable_regs))
2616                     add_web_pair_cost (dest, source,
2617                                        BLOCK_FOR_INSN (insn)->frequency,
2618                                        dest->num_conflicts
2619                                        + source->num_conflicts);
2620                 }
2621             }
2622         }
2623   sort_and_combine_web_pairs (0);
2624 }
2625
2626 /* Check if we forgot to coalesce some moves.  */
2627
2628 static void
2629 check_uncoalesced_moves ()
2630 {
2631   struct move_list *ml;
2632   struct move *m;
2633   for (ml = wl_moves; ml; ml = ml->next)
2634     if ((m = ml->move))
2635       {
2636         struct web *s = alias (m->source_web);
2637         struct web *t = alias (m->target_web);
2638         if (t->type == PRECOLORED)
2639           {
2640             struct web *h = s;
2641             s = t;
2642             t = h;
2643           }
2644         if (s != t
2645             && m->type != CONSTRAINED
2646             /* Following can happen when a move was coalesced, but later
2647                broken up again.  Then s!=t, but m is still MV_COALESCED.  */
2648             && m->type != MV_COALESCED
2649             && t->type != PRECOLORED
2650             && ((s->type == PRECOLORED && ok (t, s))
2651                 || s->type != PRECOLORED)
2652             && !TEST_BIT (sup_igraph, s->id * num_webs + t->id)
2653             && !TEST_BIT (sup_igraph, t->id * num_webs + s->id))
2654           abort ();
2655       }
2656 }
2657
2658 /* The toplevel function in this file.  Precondition is, that
2659    the interference graph is built completely by ra-build.c.  This
2660    produces a list of spilled, colored and coalesced nodes.  */
2661
2662 void
2663 ra_colorize_graph (df)
2664      struct df *df;
2665 {
2666   if (rtl_dump_file)
2667     dump_igraph (df);
2668   build_worklists (df);
2669
2670   /* With optimistic coalescing we coalesce everything we can.  */
2671   if (flag_ra_optimistic_coalescing)
2672     {
2673       aggressive_coalesce ();
2674       extended_coalesce_2 ();
2675     }
2676
2677   /* Now build the select stack.  */
2678   do
2679     {
2680       simplify ();
2681       if (mv_worklist)
2682         coalesce ();
2683       else if (WEBS(FREEZE))
2684         freeze ();
2685       else if (WEBS(SPILL))
2686         select_spill ();
2687     }
2688   while (WEBS(SIMPLIFY) || WEBS(SIMPLIFY_FAT) || WEBS(SIMPLIFY_SPILL)
2689          || mv_worklist || WEBS(FREEZE) || WEBS(SPILL));
2690   if (flag_ra_optimistic_coalescing)
2691     check_uncoalesced_moves ();
2692
2693   /* Actually colorize the webs from the select stack.  */
2694   assign_colors ();
2695   check_colors ();
2696   dump_graph_cost (DUMP_COSTS, "initially");
2697   if (flag_ra_break_aliases)
2698     break_coalesced_spills ();
2699   check_colors ();
2700
2701   /* And try to improve the cost by recoloring spilled webs.  */
2702   recolor_spills ();
2703   dump_graph_cost (DUMP_COSTS, "after spill-recolor");
2704   check_colors ();
2705 }
2706
2707 /* Initialize this module.  */
2708
2709 void ra_colorize_init ()
2710 {
2711   /* FIXME: Choose spill heuristic for platform if we have one */
2712   spill_heuristic = default_spill_heuristic;
2713 }
2714
2715 /* Free all memory.  (Note that we don't need to free any per pass
2716    memory).  */
2717
2718 void
2719 ra_colorize_free_all ()
2720 {
2721   struct dlist *d;
2722   while ((d = pop_list (&WEBS(FREE))) != NULL)
2723     put_web (DLIST_WEB (d), INITIAL);
2724   while ((d = pop_list (&WEBS(INITIAL))) != NULL)
2725     {
2726       struct web *web =DLIST_WEB (d);
2727       struct web *wnext;
2728       web->orig_conflict_list = NULL;
2729       web->conflict_list = NULL;
2730       for (web = web->subreg_next; web; web = wnext)
2731         {
2732           wnext = web->subreg_next;
2733           free (web);
2734         }
2735       free (DLIST_WEB (d));
2736     }
2737 }
2738
2739 /*
2740 vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4:
2741 */