OSDN Git Service

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