OSDN Git Service

2004-07-16 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / global.c
1 /* Allocate registers for pseudo-registers that span basic blocks.
2    Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
3    1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 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
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27
28 #include "machmode.h"
29 #include "hard-reg-set.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "basic-block.h"
34 #include "regs.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "recog.h"
38 #include "reload.h"
39 #include "output.h"
40 #include "toplev.h"
41
42 /* This pass of the compiler performs global register allocation.
43    It assigns hard register numbers to all the pseudo registers
44    that were not handled in local_alloc.  Assignments are recorded
45    in the vector reg_renumber, not by changing the rtl code.
46    (Such changes are made by final).  The entry point is
47    the function global_alloc.
48
49    After allocation is complete, the reload pass is run as a subroutine
50    of this pass, so that when a pseudo reg loses its hard reg due to
51    spilling it is possible to make a second attempt to find a hard
52    reg for it.  The reload pass is independent in other respects
53    and it is run even when stupid register allocation is in use.
54
55    1. Assign allocation-numbers (allocnos) to the pseudo-registers
56    still needing allocations and to the pseudo-registers currently
57    allocated by local-alloc which may be spilled by reload.
58    Set up tables reg_allocno and allocno_reg to map
59    reg numbers to allocnos and vice versa.
60    max_allocno gets the number of allocnos in use.
61
62    2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
63    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
64    for conflicts between allocnos and explicit hard register use
65    (which includes use of pseudo-registers allocated by local_alloc).
66
67    3. For each basic block
68     walk forward through the block, recording which
69     pseudo-registers and which hardware registers are live.
70     Build the conflict matrix between the pseudo-registers
71     and another of pseudo-registers versus hardware registers.
72     Also record the preferred hardware registers
73     for each pseudo-register.
74
75    4. Sort a table of the allocnos into order of
76    desirability of the variables.
77
78    5. Allocate the variables in that order; each if possible into
79    a preferred register, else into another register.  */
80 \f
81 /* Number of pseudo-registers which are candidates for allocation.  */
82
83 static int max_allocno;
84
85 /* Indexed by (pseudo) reg number, gives the allocno, or -1
86    for pseudo registers which are not to be allocated.  */
87
88 static int *reg_allocno;
89
90 struct allocno
91 {
92   int reg;
93   /* Gives the number of consecutive hard registers needed by that
94      pseudo reg.  */
95   int size;
96
97   /* Number of calls crossed by each allocno.  */
98   int calls_crossed;
99
100   /* Number of refs to each allocno.  */
101   int n_refs;
102
103   /* Frequency of uses of each allocno.  */
104   int freq;
105
106   /* Guess at live length of each allocno.
107      This is actually the max of the live lengths of the regs.  */
108   int live_length;
109
110   /* Set of hard regs conflicting with allocno N.  */
111
112   HARD_REG_SET hard_reg_conflicts;
113
114   /* Set of hard regs preferred by allocno N.
115      This is used to make allocnos go into regs that are copied to or from them,
116      when possible, to reduce register shuffling.  */
117
118   HARD_REG_SET hard_reg_preferences;
119
120   /* Similar, but just counts register preferences made in simple copy
121      operations, rather than arithmetic.  These are given priority because
122      we can always eliminate an insn by using these, but using a register
123      in the above list won't always eliminate an insn.  */
124
125   HARD_REG_SET hard_reg_copy_preferences;
126
127   /* Similar to hard_reg_preferences, but includes bits for subsequent
128      registers when an allocno is multi-word.  The above variable is used for
129      allocation while this is used to build reg_someone_prefers, below.  */
130
131   HARD_REG_SET hard_reg_full_preferences;
132
133   /* Set of hard registers that some later allocno has a preference for.  */
134
135   HARD_REG_SET regs_someone_prefers;
136
137 #ifdef STACK_REGS
138   /* Set to true if allocno can't be allocated in the stack register.  */
139   bool no_stack_reg;
140 #endif
141 };
142
143 static struct allocno *allocno;
144
145 /* A vector of the integers from 0 to max_allocno-1,
146    sorted in the order of first-to-be-allocated first.  */
147
148 static int *allocno_order;
149
150 /* Indexed by (pseudo) reg number, gives the number of another
151    lower-numbered pseudo reg which can share a hard reg with this pseudo
152    *even if the two pseudos would otherwise appear to conflict*.  */
153
154 static int *reg_may_share;
155
156 /* Define the number of bits in each element of `conflicts' and what
157    type that element has.  We use the largest integer format on the
158    host machine.  */
159
160 #define INT_BITS HOST_BITS_PER_WIDE_INT
161 #define INT_TYPE HOST_WIDE_INT
162
163 /* max_allocno by max_allocno array of bits,
164    recording whether two allocno's conflict (can't go in the same
165    hardware register).
166
167    `conflicts' is symmetric after the call to mirror_conflicts.  */
168
169 static INT_TYPE *conflicts;
170
171 /* Number of ints require to hold max_allocno bits.
172    This is the length of a row in `conflicts'.  */
173
174 static int allocno_row_words;
175
176 /* Two macros to test or store 1 in an element of `conflicts'.  */
177
178 #define CONFLICTP(I, J) \
179  (conflicts[(I) * allocno_row_words + (unsigned) (J) / INT_BITS]        \
180   & ((INT_TYPE) 1 << ((unsigned) (J) % INT_BITS)))
181
182 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
183    and execute CODE.  */
184 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE)       \
185 do {                                                                    \
186   int i_;                                                               \
187   int allocno_;                                                         \
188   INT_TYPE *p_ = (ALLOCNO_SET);                                         \
189                                                                         \
190   for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0;               \
191        i_--, allocno_ += INT_BITS)                                      \
192     {                                                                   \
193       unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++;              \
194                                                                         \
195       for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++)       \
196         {                                                               \
197           if (word_ & 1)                                                \
198             {CODE;}                                                     \
199         }                                                               \
200     }                                                                   \
201 } while (0)
202
203 /* This doesn't work for non-GNU C due to the way CODE is macro expanded.  */
204 #if 0
205 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
206    the conflicting allocno, and execute CODE.  This macro assumes that
207    mirror_conflicts has been run.  */
208 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
209   EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
210                                  OUT_ALLOCNO, (CODE))
211 #endif
212
213 /* Set of hard regs currently live (during scan of all insns).  */
214
215 static HARD_REG_SET hard_regs_live;
216
217 /* Set of registers that global-alloc isn't supposed to use.  */
218
219 static HARD_REG_SET no_global_alloc_regs;
220
221 /* Set of registers used so far.  */
222
223 static HARD_REG_SET regs_used_so_far;
224
225 /* Number of refs to each hard reg, as used by local alloc.
226    It is zero for a reg that contains global pseudos or is explicitly used.  */
227
228 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
229
230 /* Frequency of uses of given hard reg.  */
231 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
232
233 /* Guess at live length of each hard reg, as used by local alloc.
234    This is actually the sum of the live lengths of the specific regs.  */
235
236 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
237
238 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
239    element I, and hard register number J.  */
240
241 #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (allocno[I].TABLE, J)
242
243 /* Bit mask for allocnos live at current point in the scan.  */
244
245 static INT_TYPE *allocnos_live;
246
247 /* Test, set or clear bit number I in allocnos_live,
248    a bit vector indexed by allocno.  */
249
250 #define SET_ALLOCNO_LIVE(I)                             \
251   (allocnos_live[(unsigned) (I) / INT_BITS]             \
252      |= ((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
253
254 #define CLEAR_ALLOCNO_LIVE(I)                           \
255   (allocnos_live[(unsigned) (I) / INT_BITS]             \
256      &= ~((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
257
258 /* This is turned off because it doesn't work right for DImode.
259    (And it is only used for DImode, so the other cases are worthless.)
260    The problem is that it isn't true that there is NO possibility of conflict;
261    only that there is no conflict if the two pseudos get the exact same regs.
262    If they were allocated with a partial overlap, there would be a conflict.
263    We can't safely turn off the conflict unless we have another way to
264    prevent the partial overlap.
265
266    Idea: change hard_reg_conflicts so that instead of recording which
267    hard regs the allocno may not overlap, it records where the allocno
268    may not start.  Change both where it is used and where it is updated.
269    Then there is a way to record that (reg:DI 108) may start at 10
270    but not at 9 or 11.  There is still the question of how to record
271    this semi-conflict between two pseudos.  */
272 #if 0
273 /* Reg pairs for which conflict after the current insn
274    is inhibited by a REG_NO_CONFLICT note.
275    If the table gets full, we ignore any other notes--that is conservative.  */
276 #define NUM_NO_CONFLICT_PAIRS 4
277 /* Number of pairs in use in this insn.  */
278 int n_no_conflict_pairs;
279 static struct { int allocno1, allocno2;}
280   no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
281 #endif /* 0 */
282
283 /* Record all regs that are set in any one insn.
284    Communication from mark_reg_{store,clobber} and global_conflicts.  */
285
286 static rtx *regs_set;
287 static int n_regs_set;
288
289 /* All registers that can be eliminated.  */
290
291 static HARD_REG_SET eliminable_regset;
292
293 static int allocno_compare (const void *, const void *);
294 static void global_conflicts (void);
295 static void mirror_conflicts (void);
296 static void expand_preferences (void);
297 static void prune_preferences (void);
298 static void find_reg (int, HARD_REG_SET, int, int, int);
299 static void record_one_conflict (int);
300 static void record_conflicts (int *, int);
301 static void mark_reg_store (rtx, rtx, void *);
302 static void mark_reg_clobber (rtx, rtx, void *);
303 static void mark_reg_conflicts (rtx);
304 static void mark_reg_death (rtx);
305 static void mark_reg_live_nc (int, enum machine_mode);
306 static void set_preference (rtx, rtx);
307 static void dump_conflicts (FILE *);
308 static void reg_becomes_live (rtx, rtx, void *);
309 static void reg_dies (int, enum machine_mode, struct insn_chain *);
310
311 static void allocate_bb_info (void);
312 static void free_bb_info (void);
313 static void check_earlyclobber (rtx);
314 static bool regclass_intersect (enum reg_class, enum reg_class);
315 static void mark_reg_use_for_earlyclobber_1 (rtx *, void *);
316 static int mark_reg_use_for_earlyclobber (rtx *, void *);
317 static void calculate_local_reg_bb_info (void);
318 static void set_up_bb_rts_numbers (void);
319 static int rpost_cmp (const void *, const void *);
320 static bool modify_bb_reg_pav (basic_block, basic_block, bool);
321 static void calculate_reg_pav (void);
322 static void make_accurate_live_analysis (void);
323
324 \f
325
326 /* Perform allocation of pseudo-registers not allocated by local_alloc.
327    FILE is a file to output debugging information on,
328    or zero if such output is not desired.
329
330    Return value is nonzero if reload failed
331    and we must not do any more for this function.  */
332
333 int
334 global_alloc (FILE *file)
335 {
336   int retval;
337 #ifdef ELIMINABLE_REGS
338   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
339 #endif
340   int need_fp
341     = (! flag_omit_frame_pointer
342        || (current_function_calls_alloca && EXIT_IGNORE_STACK)
343        || FRAME_POINTER_REQUIRED);
344
345   size_t i;
346   rtx x;
347
348   make_accurate_live_analysis ();
349
350   max_allocno = 0;
351
352   /* A machine may have certain hard registers that
353      are safe to use only within a basic block.  */
354
355   CLEAR_HARD_REG_SET (no_global_alloc_regs);
356
357   /* Build the regset of all eliminable registers and show we can't use those
358      that we already know won't be eliminated.  */
359 #ifdef ELIMINABLE_REGS
360   for (i = 0; i < ARRAY_SIZE (eliminables); i++)
361     {
362       bool cannot_elim
363         = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
364            || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
365
366       if (!regs_asm_clobbered[eliminables[i].from])
367         {
368           SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
369
370           if (cannot_elim)
371             SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
372         }
373       else if (cannot_elim)
374         error ("%s cannot be used in asm here",
375                reg_names[eliminables[i].from]);
376       else
377         regs_ever_live[eliminables[i].from] = 1;
378     }
379 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
380   if (!regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
381     {
382       SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
383       if (need_fp)
384         SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
385     }
386   else if (need_fp)
387     error ("%s cannot be used in asm here",
388            reg_names[HARD_FRAME_POINTER_REGNUM]);
389   else
390     regs_ever_live[HARD_FRAME_POINTER_REGNUM] = 1;
391 #endif
392
393 #else
394   if (!regs_asm_clobbered[FRAME_POINTER_REGNUM])
395     {
396       SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
397       if (need_fp)
398         SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
399     }
400   else if (need_fp)
401     error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
402   else
403     regs_ever_live[FRAME_POINTER_REGNUM] = 1;
404 #endif
405
406   /* Track which registers have already been used.  Start with registers
407      explicitly in the rtl, then registers allocated by local register
408      allocation.  */
409
410   CLEAR_HARD_REG_SET (regs_used_so_far);
411 #ifdef LEAF_REGISTERS
412   /* If we are doing the leaf function optimization, and this is a leaf
413      function, it means that the registers that take work to save are those
414      that need a register window.  So prefer the ones that can be used in
415      a leaf function.  */
416   {
417     const char *cheap_regs;
418     const char *const leaf_regs = LEAF_REGISTERS;
419
420     if (only_leaf_regs_used () && leaf_function_p ())
421       cheap_regs = leaf_regs;
422     else
423       cheap_regs = call_used_regs;
424     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
425       if (regs_ever_live[i] || cheap_regs[i])
426         SET_HARD_REG_BIT (regs_used_so_far, i);
427   }
428 #else
429   /* We consider registers that do not have to be saved over calls as if
430      they were already used since there is no cost in using them.  */
431   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
432     if (regs_ever_live[i] || call_used_regs[i])
433       SET_HARD_REG_BIT (regs_used_so_far, i);
434 #endif
435
436   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
437     if (reg_renumber[i] >= 0)
438       SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
439
440   /* Establish mappings from register number to allocation number
441      and vice versa.  In the process, count the allocnos.  */
442
443   reg_allocno = xmalloc (max_regno * sizeof (int));
444
445   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
446     reg_allocno[i] = -1;
447
448   /* Initialize the shared-hard-reg mapping
449      from the list of pairs that may share.  */
450   reg_may_share = xcalloc (max_regno, sizeof (int));
451   for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
452     {
453       int r1 = REGNO (XEXP (x, 0));
454       int r2 = REGNO (XEXP (XEXP (x, 1), 0));
455       if (r1 > r2)
456         reg_may_share[r1] = r2;
457       else
458         reg_may_share[r2] = r1;
459     }
460
461   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
462     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
463        that we are supposed to refrain from putting in a hard reg.
464        -2 means do make an allocno but don't allocate it.  */
465     if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
466         /* Don't allocate pseudos that cross calls,
467            if this function receives a nonlocal goto.  */
468         && (! current_function_has_nonlocal_label
469             || REG_N_CALLS_CROSSED (i) == 0))
470       {
471         if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
472           reg_allocno[i] = reg_allocno[reg_may_share[i]];
473         else
474           reg_allocno[i] = max_allocno++;
475         if (REG_LIVE_LENGTH (i) == 0)
476           abort ();
477       }
478     else
479       reg_allocno[i] = -1;
480
481   allocno = xcalloc (max_allocno, sizeof (struct allocno));
482
483   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
484     if (reg_allocno[i] >= 0)
485       {
486         int num = reg_allocno[i];
487         allocno[num].reg = i;
488         allocno[num].size = PSEUDO_REGNO_SIZE (i);
489         allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
490         allocno[num].n_refs += REG_N_REFS (i);
491         allocno[num].freq += REG_FREQ (i);
492         if (allocno[num].live_length < REG_LIVE_LENGTH (i))
493           allocno[num].live_length = REG_LIVE_LENGTH (i);
494       }
495
496   /* Calculate amount of usage of each hard reg by pseudos
497      allocated by local-alloc.  This is to see if we want to
498      override it.  */
499   memset (local_reg_live_length, 0, sizeof local_reg_live_length);
500   memset (local_reg_n_refs, 0, sizeof local_reg_n_refs);
501   memset (local_reg_freq, 0, sizeof local_reg_freq);
502   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
503     if (reg_renumber[i] >= 0)
504       {
505         int regno = reg_renumber[i];
506         int endregno = regno + hard_regno_nregs[regno][PSEUDO_REGNO_MODE (i)];
507         int j;
508
509         for (j = regno; j < endregno; j++)
510           {
511             local_reg_n_refs[j] += REG_N_REFS (i);
512             local_reg_freq[j] += REG_FREQ (i);
513             local_reg_live_length[j] += REG_LIVE_LENGTH (i);
514           }
515       }
516
517   /* We can't override local-alloc for a reg used not just by local-alloc.  */
518   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
519     if (regs_ever_live[i])
520       local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
521
522   allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
523
524   /* We used to use alloca here, but the size of what it would try to
525      allocate would occasionally cause it to exceed the stack limit and
526      cause unpredictable core dumps.  Some examples were > 2Mb in size.  */
527   conflicts = xcalloc (max_allocno * allocno_row_words, sizeof (INT_TYPE));
528
529   allocnos_live = xmalloc (allocno_row_words * sizeof (INT_TYPE));
530
531   /* If there is work to be done (at least one reg to allocate),
532      perform global conflict analysis and allocate the regs.  */
533
534   if (max_allocno > 0)
535     {
536       /* Scan all the insns and compute the conflicts among allocnos
537          and between allocnos and hard regs.  */
538
539       global_conflicts ();
540
541       mirror_conflicts ();
542
543       /* Eliminate conflicts between pseudos and eliminable registers.  If
544          the register is not eliminated, the pseudo won't really be able to
545          live in the eliminable register, so the conflict doesn't matter.
546          If we do eliminate the register, the conflict will no longer exist.
547          So in either case, we can ignore the conflict.  Likewise for
548          preferences.  */
549
550       for (i = 0; i < (size_t) max_allocno; i++)
551         {
552           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
553                                   eliminable_regset);
554           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
555                                   eliminable_regset);
556           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
557                                   eliminable_regset);
558         }
559
560       /* Try to expand the preferences by merging them between allocnos.  */
561
562       expand_preferences ();
563
564       /* Determine the order to allocate the remaining pseudo registers.  */
565
566       allocno_order = xmalloc (max_allocno * sizeof (int));
567       for (i = 0; i < (size_t) max_allocno; i++)
568         allocno_order[i] = i;
569
570       /* Default the size to 1, since allocno_compare uses it to divide by.
571          Also convert allocno_live_length of zero to -1.  A length of zero
572          can occur when all the registers for that allocno have reg_live_length
573          equal to -2.  In this case, we want to make an allocno, but not
574          allocate it.  So avoid the divide-by-zero and set it to a low
575          priority.  */
576
577       for (i = 0; i < (size_t) max_allocno; i++)
578         {
579           if (allocno[i].size == 0)
580             allocno[i].size = 1;
581           if (allocno[i].live_length == 0)
582             allocno[i].live_length = -1;
583         }
584
585       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
586
587       prune_preferences ();
588
589       if (file)
590         dump_conflicts (file);
591
592       /* Try allocating them, one by one, in that order,
593          except for parameters marked with reg_live_length[regno] == -2.  */
594
595       for (i = 0; i < (size_t) max_allocno; i++)
596         if (reg_renumber[allocno[allocno_order[i]].reg] < 0
597             && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
598           {
599             /* If we have more than one register class,
600                first try allocating in the class that is cheapest
601                for this pseudo-reg.  If that fails, try any reg.  */
602             if (N_REG_CLASSES > 1)
603               {
604                 find_reg (allocno_order[i], 0, 0, 0, 0);
605                 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
606                   continue;
607               }
608             if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
609               find_reg (allocno_order[i], 0, 1, 0, 0);
610           }
611
612       free (allocno_order);
613     }
614
615   /* Do the reloads now while the allocno data still exist, so that we can
616      try to assign new hard regs to any pseudo regs that are spilled.  */
617
618 #if 0 /* We need to eliminate regs even if there is no rtl code,
619          for the sake of debugging information.  */
620   if (n_basic_blocks > 0)
621 #endif
622     {
623       build_insn_chain (get_insns ());
624       retval = reload (get_insns (), 1);
625     }
626
627   /* Clean up.  */
628   free (reg_allocno);
629   free (reg_may_share);
630   free (allocno);
631   free (conflicts);
632   free (allocnos_live);
633
634   return retval;
635 }
636
637 /* Sort predicate for ordering the allocnos.
638    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
639
640 static int
641 allocno_compare (const void *v1p, const void *v2p)
642 {
643   int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
644   /* Note that the quotient will never be bigger than
645      the value of floor_log2 times the maximum number of
646      times a register can occur in one insn (surely less than 100)
647      weighted by the frequency (maximally REG_FREQ_MAX).
648      Multiplying this by 10000/REG_FREQ_MAX can't overflow.  */
649   int pri1
650     = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
651         / allocno[v1].live_length)
652        * (10000 / REG_FREQ_MAX) * allocno[v1].size);
653   int pri2
654     = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
655         / allocno[v2].live_length)
656        * (10000 / REG_FREQ_MAX) * allocno[v2].size);
657   if (pri2 - pri1)
658     return pri2 - pri1;
659
660   /* If regs are equally good, sort by allocno,
661      so that the results of qsort leave nothing to chance.  */
662   return v1 - v2;
663 }
664 \f
665 /* Scan the rtl code and record all conflicts and register preferences in the
666    conflict matrices and preference tables.  */
667
668 static void
669 global_conflicts (void)
670 {
671   int i;
672   basic_block b;
673   rtx insn;
674   int *block_start_allocnos;
675
676   /* Make a vector that mark_reg_{store,clobber} will store in.  */
677   regs_set = xmalloc (max_parallel * sizeof (rtx) * 2);
678
679   block_start_allocnos = xmalloc (max_allocno * sizeof (int));
680
681   FOR_EACH_BB (b)
682     {
683       memset (allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
684
685       /* Initialize table of registers currently live
686          to the state at the beginning of this basic block.
687          This also marks the conflicts among hard registers
688          and any allocnos that are live.
689
690          For pseudo-regs, there is only one bit for each one
691          no matter how many hard regs it occupies.
692          This is ok; we know the size from PSEUDO_REGNO_SIZE.
693          For explicit hard regs, we cannot know the size that way
694          since one hard reg can be used with various sizes.
695          Therefore, we must require that all the hard regs
696          implicitly live as part of a multi-word hard reg
697          are explicitly marked in basic_block_live_at_start.  */
698
699       {
700         regset old = b->global_live_at_start;
701         int ax = 0;
702
703         REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
704         EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
705                                    {
706                                      int a = reg_allocno[i];
707                                      if (a >= 0)
708                                        {
709                                          SET_ALLOCNO_LIVE (a);
710                                          block_start_allocnos[ax++] = a;
711                                        }
712                                      else if ((a = reg_renumber[i]) >= 0)
713                                        mark_reg_live_nc
714                                          (a, PSEUDO_REGNO_MODE (i));
715                                    });
716
717         /* Record that each allocno now live conflicts with each hard reg
718            now live.
719
720            It is not necessary to mark any conflicts between pseudos as
721            this point, even for pseudos which are live at the start of
722            the basic block.
723
724              Given two pseudos X and Y and any point in the CFG P.
725
726              On any path to point P where X and Y are live one of the
727              following conditions must be true:
728
729                 1. X is live at some instruction on the path that
730                    evaluates Y.
731
732                 2. Y is live at some instruction on the path that
733                    evaluates X.
734
735                 3. Either X or Y is not evaluated on the path to P
736                    (ie it is used uninitialized) and thus the
737                    conflict can be ignored.
738
739             In cases #1 and #2 the conflict will be recorded when we
740             scan the instruction that makes either X or Y become live.  */
741         record_conflicts (block_start_allocnos, ax);
742
743         /* Pseudos can't go in stack regs at the start of a basic block that
744            is reached by an abnormal edge. Likewise for call clobbered regs,
745            because because caller-save, fixup_abnormal_edges, and possibly
746            the table driven EH machinery are not quite ready to handle such
747            regs live across such edges.  */
748         {
749           edge e;
750
751           for (e = b->pred; e ; e = e->pred_next)
752             if (e->flags & EDGE_ABNORMAL)
753               break;
754
755           if (e != NULL)
756             {
757 #ifdef STACK_REGS
758               EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, ax,
759                                              {
760                                                allocno[ax].no_stack_reg = 1;
761                                              });
762               for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
763                 record_one_conflict (ax);
764 #endif
765
766               /* No need to record conflicts for call clobbered regs if we have
767                  nonlocal labels around, as we don't ever try to allocate such
768                  regs in this case.  */
769               if (! current_function_has_nonlocal_label)
770                 for (ax = 0; ax < FIRST_PSEUDO_REGISTER; ax++)
771                   if (call_used_regs [ax])
772                     record_one_conflict (ax);
773             }
774         }
775       }
776
777       insn = BB_HEAD (b);
778
779       /* Scan the code of this basic block, noting which allocnos
780          and hard regs are born or die.  When one is born,
781          record a conflict with all others currently live.  */
782
783       while (1)
784         {
785           RTX_CODE code = GET_CODE (insn);
786           rtx link;
787
788           /* Make regs_set an empty set.  */
789
790           n_regs_set = 0;
791
792           if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
793             {
794
795 #if 0
796               int i = 0;
797               for (link = REG_NOTES (insn);
798                    link && i < NUM_NO_CONFLICT_PAIRS;
799                    link = XEXP (link, 1))
800                 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
801                   {
802                     no_conflict_pairs[i].allocno1
803                       = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
804                     no_conflict_pairs[i].allocno2
805                       = reg_allocno[REGNO (XEXP (link, 0))];
806                     i++;
807                   }
808 #endif /* 0 */
809
810               /* Mark any registers clobbered by INSN as live,
811                  so they conflict with the inputs.  */
812
813               note_stores (PATTERN (insn), mark_reg_clobber, NULL);
814
815               /* Mark any registers dead after INSN as dead now.  */
816
817               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
818                 if (REG_NOTE_KIND (link) == REG_DEAD)
819                   mark_reg_death (XEXP (link, 0));
820
821               /* Mark any registers set in INSN as live,
822                  and mark them as conflicting with all other live regs.
823                  Clobbers are processed again, so they conflict with
824                  the registers that are set.  */
825
826               note_stores (PATTERN (insn), mark_reg_store, NULL);
827
828 #ifdef AUTO_INC_DEC
829               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
830                 if (REG_NOTE_KIND (link) == REG_INC)
831                   mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
832 #endif
833
834               /* If INSN has multiple outputs, then any reg that dies here
835                  and is used inside of an output
836                  must conflict with the other outputs.
837
838                  It is unsafe to use !single_set here since it will ignore an
839                  unused output.  Just because an output is unused does not mean
840                  the compiler can assume the side effect will not occur.
841                  Consider if REG appears in the address of an output and we
842                  reload the output.  If we allocate REG to the same hard
843                  register as an unused output we could set the hard register
844                  before the output reload insn.  */
845               if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
846                 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
847                   if (REG_NOTE_KIND (link) == REG_DEAD)
848                     {
849                       int used_in_output = 0;
850                       int i;
851                       rtx reg = XEXP (link, 0);
852
853                       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
854                         {
855                           rtx set = XVECEXP (PATTERN (insn), 0, i);
856                           if (GET_CODE (set) == SET
857                               && !REG_P (SET_DEST (set))
858                               && !rtx_equal_p (reg, SET_DEST (set))
859                               && reg_overlap_mentioned_p (reg, SET_DEST (set)))
860                             used_in_output = 1;
861                         }
862                       if (used_in_output)
863                         mark_reg_conflicts (reg);
864                     }
865
866               /* Mark any registers set in INSN and then never used.  */
867
868               while (n_regs_set-- > 0)
869                 {
870                   rtx note = find_regno_note (insn, REG_UNUSED,
871                                               REGNO (regs_set[n_regs_set]));
872                   if (note)
873                     mark_reg_death (XEXP (note, 0));
874                 }
875             }
876
877           if (insn == BB_END (b))
878             break;
879           insn = NEXT_INSN (insn);
880         }
881     }
882
883   /* Clean up.  */
884   free (block_start_allocnos);
885   free (regs_set);
886 }
887 /* Expand the preference information by looking for cases where one allocno
888    dies in an insn that sets an allocno.  If those two allocnos don't conflict,
889    merge any preferences between those allocnos.  */
890
891 static void
892 expand_preferences (void)
893 {
894   rtx insn;
895   rtx link;
896   rtx set;
897
898   /* We only try to handle the most common cases here.  Most of the cases
899      where this wins are reg-reg copies.  */
900
901   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
902     if (INSN_P (insn)
903         && (set = single_set (insn)) != 0
904         && REG_P (SET_DEST (set))
905         && reg_allocno[REGNO (SET_DEST (set))] >= 0)
906       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
907         if (REG_NOTE_KIND (link) == REG_DEAD
908             && REG_P (XEXP (link, 0))
909             && reg_allocno[REGNO (XEXP (link, 0))] >= 0
910             && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
911                             reg_allocno[REGNO (XEXP (link, 0))]))
912           {
913             int a1 = reg_allocno[REGNO (SET_DEST (set))];
914             int a2 = reg_allocno[REGNO (XEXP (link, 0))];
915
916             if (XEXP (link, 0) == SET_SRC (set))
917               {
918                 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
919                                   allocno[a2].hard_reg_copy_preferences);
920                 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
921                                   allocno[a1].hard_reg_copy_preferences);
922               }
923
924             IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
925                               allocno[a2].hard_reg_preferences);
926             IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
927                               allocno[a1].hard_reg_preferences);
928             IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
929                               allocno[a2].hard_reg_full_preferences);
930             IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
931                               allocno[a1].hard_reg_full_preferences);
932           }
933 }
934 \f
935 /* Prune the preferences for global registers to exclude registers that cannot
936    be used.
937
938    Compute `regs_someone_prefers', which is a bitmask of the hard registers
939    that are preferred by conflicting registers of lower priority.  If possible,
940    we will avoid using these registers.  */
941
942 static void
943 prune_preferences (void)
944 {
945   int i;
946   int num;
947   int *allocno_to_order = xmalloc (max_allocno * sizeof (int));
948
949   /* Scan least most important to most important.
950      For each allocno, remove from preferences registers that cannot be used,
951      either because of conflicts or register type.  Then compute all registers
952      preferred by each lower-priority register that conflicts.  */
953
954   for (i = max_allocno - 1; i >= 0; i--)
955     {
956       HARD_REG_SET temp;
957
958       num = allocno_order[i];
959       allocno_to_order[num] = i;
960       COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
961
962       if (allocno[num].calls_crossed == 0)
963         IOR_HARD_REG_SET (temp, fixed_reg_set);
964       else
965         IOR_HARD_REG_SET (temp, call_used_reg_set);
966
967       IOR_COMPL_HARD_REG_SET
968         (temp,
969          reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
970
971       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
972       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
973       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
974     }
975
976   for (i = max_allocno - 1; i >= 0; i--)
977     {
978       /* Merge in the preferences of lower-priority registers (they have
979          already been pruned).  If we also prefer some of those registers,
980          don't exclude them unless we are of a smaller size (in which case
981          we want to give the lower-priority allocno the first chance for
982          these registers).  */
983       HARD_REG_SET temp, temp2;
984       int allocno2;
985
986       num = allocno_order[i];
987
988       CLEAR_HARD_REG_SET (temp);
989       CLEAR_HARD_REG_SET (temp2);
990
991       EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
992                                      allocno2,
993         {
994           if (allocno_to_order[allocno2] > i)
995             {
996               if (allocno[allocno2].size <= allocno[num].size)
997                 IOR_HARD_REG_SET (temp,
998                                   allocno[allocno2].hard_reg_full_preferences);
999               else
1000                 IOR_HARD_REG_SET (temp2,
1001                                   allocno[allocno2].hard_reg_full_preferences);
1002             }
1003         });
1004
1005       AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
1006       IOR_HARD_REG_SET (temp, temp2);
1007       COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
1008     }
1009   free (allocno_to_order);
1010 }
1011 \f
1012 /* Assign a hard register to allocno NUM; look for one that is the beginning
1013    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
1014    The registers marked in PREFREGS are tried first.
1015
1016    LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
1017    be used for this allocation.
1018
1019    If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
1020    Otherwise ignore that preferred class and use the alternate class.
1021
1022    If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
1023    will have to be saved and restored at calls.
1024
1025    RETRYING is nonzero if this is called from retry_global_alloc.
1026
1027    If we find one, record it in reg_renumber.
1028    If not, do nothing.  */
1029
1030 static void
1031 find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
1032 {
1033   int i, best_reg, pass;
1034   HARD_REG_SET used, used1, used2;
1035
1036   enum reg_class class = (alt_regs_p
1037                           ? reg_alternate_class (allocno[num].reg)
1038                           : reg_preferred_class (allocno[num].reg));
1039   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
1040
1041   if (accept_call_clobbered)
1042     COPY_HARD_REG_SET (used1, call_fixed_reg_set);
1043   else if (allocno[num].calls_crossed == 0)
1044     COPY_HARD_REG_SET (used1, fixed_reg_set);
1045   else
1046     COPY_HARD_REG_SET (used1, call_used_reg_set);
1047
1048   /* Some registers should not be allocated in global-alloc.  */
1049   IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1050   if (losers)
1051     IOR_HARD_REG_SET (used1, losers);
1052
1053   IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1054   COPY_HARD_REG_SET (used2, used1);
1055
1056   IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1057
1058 #ifdef CANNOT_CHANGE_MODE_CLASS
1059   cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1060 #endif
1061
1062   /* Try each hard reg to see if it fits.  Do this in two passes.
1063      In the first pass, skip registers that are preferred by some other pseudo
1064      to give it a better chance of getting one of those registers.  Only if
1065      we can't get a register when excluding those do we take one of them.
1066      However, we never allocate a register for the first time in pass 0.  */
1067
1068   COPY_HARD_REG_SET (used, used1);
1069   IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1070   IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1071
1072   best_reg = -1;
1073   for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1074        pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1075        pass++)
1076     {
1077       if (pass == 1)
1078         COPY_HARD_REG_SET (used, used1);
1079       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1080         {
1081 #ifdef REG_ALLOC_ORDER
1082           int regno = reg_alloc_order[i];
1083 #else
1084           int regno = i;
1085 #endif
1086           if (! TEST_HARD_REG_BIT (used, regno)
1087               && HARD_REGNO_MODE_OK (regno, mode)
1088               && (allocno[num].calls_crossed == 0
1089                   || accept_call_clobbered
1090                   || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1091             {
1092               int j;
1093               int lim = regno + hard_regno_nregs[regno][mode];
1094               for (j = regno + 1;
1095                    (j < lim
1096                     && ! TEST_HARD_REG_BIT (used, j));
1097                    j++);
1098               if (j == lim)
1099                 {
1100                   best_reg = regno;
1101                   break;
1102                 }
1103 #ifndef REG_ALLOC_ORDER
1104               i = j;                    /* Skip starting points we know will lose */
1105 #endif
1106             }
1107           }
1108       }
1109
1110   /* See if there is a preferred register with the same class as the register
1111      we allocated above.  Making this restriction prevents register
1112      preferencing from creating worse register allocation.
1113
1114      Remove from the preferred registers and conflicting registers.  Note that
1115      additional conflicts may have been added after `prune_preferences' was
1116      called.
1117
1118      First do this for those register with copy preferences, then all
1119      preferred registers.  */
1120
1121   AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1122   GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1123                          reg_class_contents[(int) NO_REGS], no_copy_prefs);
1124
1125   if (best_reg >= 0)
1126     {
1127       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1128         if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1129             && HARD_REGNO_MODE_OK (i, mode)
1130             && (allocno[num].calls_crossed == 0
1131                 || accept_call_clobbered
1132                 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1133             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1134                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1135                                        REGNO_REG_CLASS (best_reg))
1136                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1137                                        REGNO_REG_CLASS (i))))
1138             {
1139               int j;
1140               int lim = i + hard_regno_nregs[i][mode];
1141               for (j = i + 1;
1142                    (j < lim
1143                     && ! TEST_HARD_REG_BIT (used, j)
1144                     && (REGNO_REG_CLASS (j)
1145                         == REGNO_REG_CLASS (best_reg + (j - i))
1146                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1147                                                REGNO_REG_CLASS (best_reg + (j - i)))
1148                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1149                                                REGNO_REG_CLASS (j))));
1150                    j++);
1151               if (j == lim)
1152                 {
1153                   best_reg = i;
1154                   goto no_prefs;
1155                 }
1156             }
1157     }
1158  no_copy_prefs:
1159
1160   AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1161   GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1162                          reg_class_contents[(int) NO_REGS], no_prefs);
1163
1164   if (best_reg >= 0)
1165     {
1166       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1167         if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1168             && HARD_REGNO_MODE_OK (i, mode)
1169             && (allocno[num].calls_crossed == 0
1170                 || accept_call_clobbered
1171                 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1172             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1173                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1174                                        REGNO_REG_CLASS (best_reg))
1175                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1176                                        REGNO_REG_CLASS (i))))
1177             {
1178               int j;
1179               int lim = i + hard_regno_nregs[i][mode];
1180               for (j = i + 1;
1181                    (j < lim
1182                     && ! TEST_HARD_REG_BIT (used, j)
1183                     && (REGNO_REG_CLASS (j)
1184                         == REGNO_REG_CLASS (best_reg + (j - i))
1185                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1186                                                REGNO_REG_CLASS (best_reg + (j - i)))
1187                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1188                                                REGNO_REG_CLASS (j))));
1189                    j++);
1190               if (j == lim)
1191                 {
1192                   best_reg = i;
1193                   break;
1194                 }
1195             }
1196     }
1197  no_prefs:
1198
1199   /* If we haven't succeeded yet, try with caller-saves.
1200      We need not check to see if the current function has nonlocal
1201      labels because we don't put any pseudos that are live over calls in
1202      registers in that case.  */
1203
1204   if (flag_caller_saves && best_reg < 0)
1205     {
1206       /* Did not find a register.  If it would be profitable to
1207          allocate a call-clobbered register and save and restore it
1208          around calls, do that.  */
1209       if (! accept_call_clobbered
1210           && allocno[num].calls_crossed != 0
1211           && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1212                                      allocno[num].calls_crossed))
1213         {
1214           HARD_REG_SET new_losers;
1215           if (! losers)
1216             CLEAR_HARD_REG_SET (new_losers);
1217           else
1218             COPY_HARD_REG_SET (new_losers, losers);
1219
1220           IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1221           find_reg (num, new_losers, alt_regs_p, 1, retrying);
1222           if (reg_renumber[allocno[num].reg] >= 0)
1223             {
1224               caller_save_needed = 1;
1225               return;
1226             }
1227         }
1228     }
1229
1230   /* If we haven't succeeded yet,
1231      see if some hard reg that conflicts with us
1232      was utilized poorly by local-alloc.
1233      If so, kick out the regs that were put there by local-alloc
1234      so we can use it instead.  */
1235   if (best_reg < 0 && !retrying
1236       /* Let's not bother with multi-reg allocnos.  */
1237       && allocno[num].size == 1)
1238     {
1239       /* Count from the end, to find the least-used ones first.  */
1240       for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1241         {
1242 #ifdef REG_ALLOC_ORDER
1243           int regno = reg_alloc_order[i];
1244 #else
1245           int regno = i;
1246 #endif
1247
1248           if (local_reg_n_refs[regno] != 0
1249               /* Don't use a reg no good for this pseudo.  */
1250               && ! TEST_HARD_REG_BIT (used2, regno)
1251               && HARD_REGNO_MODE_OK (regno, mode)
1252               /* The code below assumes that we need only a single
1253                  register, but the check of allocno[num].size above
1254                  was not enough.  Sometimes we need more than one
1255                  register for a single-word value.  */
1256               && hard_regno_nregs[regno][mode] == 1
1257               && (allocno[num].calls_crossed == 0
1258                   || accept_call_clobbered
1259                   || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1260 #ifdef CANNOT_CHANGE_MODE_CLASS
1261               && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1262                                           mode)
1263 #endif
1264 #ifdef STACK_REGS
1265              && (!allocno[num].no_stack_reg
1266                  || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
1267 #endif
1268               )
1269             {
1270               /* We explicitly evaluate the divide results into temporary
1271                  variables so as to avoid excess precision problems that occur
1272                  on an i386-unknown-sysv4.2 (unixware) host.  */
1273
1274               double tmp1 = ((double) local_reg_freq[regno]
1275                             / local_reg_live_length[regno]);
1276               double tmp2 = ((double) allocno[num].freq
1277                              / allocno[num].live_length);
1278
1279               if (tmp1 < tmp2)
1280                 {
1281                   /* Hard reg REGNO was used less in total by local regs
1282                      than it would be used by this one allocno!  */
1283                   int k;
1284                   for (k = 0; k < max_regno; k++)
1285                     if (reg_renumber[k] >= 0)
1286                       {
1287                         int r = reg_renumber[k];
1288                         int endregno
1289                           = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (k)];
1290
1291                         if (regno >= r && regno < endregno)
1292                           reg_renumber[k] = -1;
1293                       }
1294
1295                   best_reg = regno;
1296                   break;
1297                 }
1298             }
1299         }
1300     }
1301
1302   /* Did we find a register?  */
1303
1304   if (best_reg >= 0)
1305     {
1306       int lim, j;
1307       HARD_REG_SET this_reg;
1308
1309       /* Yes.  Record it as the hard register of this pseudo-reg.  */
1310       reg_renumber[allocno[num].reg] = best_reg;
1311       /* Also of any pseudo-regs that share with it.  */
1312       if (reg_may_share[allocno[num].reg])
1313         for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1314           if (reg_allocno[j] == num)
1315             reg_renumber[j] = best_reg;
1316
1317       /* Make a set of the hard regs being allocated.  */
1318       CLEAR_HARD_REG_SET (this_reg);
1319       lim = best_reg + hard_regno_nregs[best_reg][mode];
1320       for (j = best_reg; j < lim; j++)
1321         {
1322           SET_HARD_REG_BIT (this_reg, j);
1323           SET_HARD_REG_BIT (regs_used_so_far, j);
1324           /* This is no longer a reg used just by local regs.  */
1325           local_reg_n_refs[j] = 0;
1326           local_reg_freq[j] = 0;
1327         }
1328       /* For each other pseudo-reg conflicting with this one,
1329          mark it as conflicting with the hard regs this one occupies.  */
1330       lim = num;
1331       EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1332         {
1333           IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1334         });
1335     }
1336 }
1337 \f
1338 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1339    Perhaps it had previously seemed not worth a hard reg,
1340    or perhaps its old hard reg has been commandeered for reloads.
1341    FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1342    they do not appear to be allocated.
1343    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
1344
1345 void
1346 retry_global_alloc (int regno, HARD_REG_SET forbidden_regs)
1347 {
1348   int alloc_no = reg_allocno[regno];
1349   if (alloc_no >= 0)
1350     {
1351       /* If we have more than one register class,
1352          first try allocating in the class that is cheapest
1353          for this pseudo-reg.  If that fails, try any reg.  */
1354       if (N_REG_CLASSES > 1)
1355         find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1356       if (reg_renumber[regno] < 0
1357           && reg_alternate_class (regno) != NO_REGS)
1358         find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1359
1360       /* If we found a register, modify the RTL for the register to
1361          show the hard register, and mark that register live.  */
1362       if (reg_renumber[regno] >= 0)
1363         {
1364           REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1365           mark_home_live (regno);
1366         }
1367     }
1368 }
1369 \f
1370 /* Record a conflict between register REGNO
1371    and everything currently live.
1372    REGNO must not be a pseudo reg that was allocated
1373    by local_alloc; such numbers must be translated through
1374    reg_renumber before calling here.  */
1375
1376 static void
1377 record_one_conflict (int regno)
1378 {
1379   int j;
1380
1381   if (regno < FIRST_PSEUDO_REGISTER)
1382     /* When a hard register becomes live,
1383        record conflicts with live pseudo regs.  */
1384     EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1385       {
1386         SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1387       });
1388   else
1389     /* When a pseudo-register becomes live,
1390        record conflicts first with hard regs,
1391        then with other pseudo regs.  */
1392     {
1393       int ialloc = reg_allocno[regno];
1394       int ialloc_prod = ialloc * allocno_row_words;
1395
1396       IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1397       for (j = allocno_row_words - 1; j >= 0; j--)
1398         conflicts[ialloc_prod + j] |= allocnos_live[j];
1399     }
1400 }
1401
1402 /* Record all allocnos currently live as conflicting
1403    with all hard regs currently live.
1404
1405    ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1406    are currently live.  Their bits are also flagged in allocnos_live.  */
1407
1408 static void
1409 record_conflicts (int *allocno_vec, int len)
1410 {
1411   while (--len >= 0)
1412     IOR_HARD_REG_SET (allocno[allocno_vec[len]].hard_reg_conflicts,
1413                       hard_regs_live);
1414 }
1415
1416 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true.  */
1417 static void
1418 mirror_conflicts (void)
1419 {
1420   int i, j;
1421   int rw = allocno_row_words;
1422   int rwb = rw * INT_BITS;
1423   INT_TYPE *p = conflicts;
1424   INT_TYPE *q0 = conflicts, *q1, *q2;
1425   unsigned INT_TYPE mask;
1426
1427   for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1428     {
1429       if (! mask)
1430         {
1431           mask = 1;
1432           q0++;
1433         }
1434       for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1435         {
1436           unsigned INT_TYPE word;
1437
1438           for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1439                word >>= 1, q2 += rw)
1440             {
1441               if (word & 1)
1442                 *q2 |= mask;
1443             }
1444         }
1445     }
1446 }
1447 \f
1448 /* Handle the case where REG is set by the insn being scanned,
1449    during the forward scan to accumulate conflicts.
1450    Store a 1 in regs_live or allocnos_live for this register, record how many
1451    consecutive hardware registers it actually needs,
1452    and record a conflict with all other registers already live.
1453
1454    Note that even if REG does not remain alive after this insn,
1455    we must mark it here as live, to ensure a conflict between
1456    REG and any other regs set in this insn that really do live.
1457    This is because those other regs could be considered after this.
1458
1459    REG might actually be something other than a register;
1460    if so, we do nothing.
1461
1462    SETTER is 0 if this register was modified by an auto-increment (i.e.,
1463    a REG_INC note was found for it).  */
1464
1465 static void
1466 mark_reg_store (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
1467 {
1468   int regno;
1469
1470   if (GET_CODE (reg) == SUBREG)
1471     reg = SUBREG_REG (reg);
1472
1473   if (!REG_P (reg))
1474     return;
1475
1476   regs_set[n_regs_set++] = reg;
1477
1478   if (setter && GET_CODE (setter) != CLOBBER)
1479     set_preference (reg, SET_SRC (setter));
1480
1481   regno = REGNO (reg);
1482
1483   /* Either this is one of the max_allocno pseudo regs not allocated,
1484      or it is or has a hardware reg.  First handle the pseudo-regs.  */
1485   if (regno >= FIRST_PSEUDO_REGISTER)
1486     {
1487       if (reg_allocno[regno] >= 0)
1488         {
1489           SET_ALLOCNO_LIVE (reg_allocno[regno]);
1490           record_one_conflict (regno);
1491         }
1492     }
1493
1494   if (reg_renumber[regno] >= 0)
1495     regno = reg_renumber[regno];
1496
1497   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1498   if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1499     {
1500       int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1501       while (regno < last)
1502         {
1503           record_one_conflict (regno);
1504           SET_HARD_REG_BIT (hard_regs_live, regno);
1505           regno++;
1506         }
1507     }
1508 }
1509 \f
1510 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs.  */
1511
1512 static void
1513 mark_reg_clobber (rtx reg, rtx setter, void *data)
1514 {
1515   if (GET_CODE (setter) == CLOBBER)
1516     mark_reg_store (reg, setter, data);
1517 }
1518
1519 /* Record that REG has conflicts with all the regs currently live.
1520    Do not mark REG itself as live.  */
1521
1522 static void
1523 mark_reg_conflicts (rtx reg)
1524 {
1525   int regno;
1526
1527   if (GET_CODE (reg) == SUBREG)
1528     reg = SUBREG_REG (reg);
1529
1530   if (!REG_P (reg))
1531     return;
1532
1533   regno = REGNO (reg);
1534
1535   /* Either this is one of the max_allocno pseudo regs not allocated,
1536      or it is or has a hardware reg.  First handle the pseudo-regs.  */
1537   if (regno >= FIRST_PSEUDO_REGISTER)
1538     {
1539       if (reg_allocno[regno] >= 0)
1540         record_one_conflict (regno);
1541     }
1542
1543   if (reg_renumber[regno] >= 0)
1544     regno = reg_renumber[regno];
1545
1546   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1547   if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1548     {
1549       int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1550       while (regno < last)
1551         {
1552           record_one_conflict (regno);
1553           regno++;
1554         }
1555     }
1556 }
1557 \f
1558 /* Mark REG as being dead (following the insn being scanned now).
1559    Store a 0 in regs_live or allocnos_live for this register.  */
1560
1561 static void
1562 mark_reg_death (rtx reg)
1563 {
1564   int regno = REGNO (reg);
1565
1566   /* Either this is one of the max_allocno pseudo regs not allocated,
1567      or it is a hardware reg.  First handle the pseudo-regs.  */
1568   if (regno >= FIRST_PSEUDO_REGISTER)
1569     {
1570       if (reg_allocno[regno] >= 0)
1571         CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1572     }
1573
1574   /* For pseudo reg, see if it has been assigned a hardware reg.  */
1575   if (reg_renumber[regno] >= 0)
1576     regno = reg_renumber[regno];
1577
1578   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1579   if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1580     {
1581       /* Pseudo regs already assigned hardware regs are treated
1582          almost the same as explicit hardware regs.  */
1583       int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1584       while (regno < last)
1585         {
1586           CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1587           regno++;
1588         }
1589     }
1590 }
1591
1592 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1593    for the value stored in it.  MODE determines how many consecutive
1594    registers are actually in use.  Do not record conflicts;
1595    it is assumed that the caller will do that.  */
1596
1597 static void
1598 mark_reg_live_nc (int regno, enum machine_mode mode)
1599 {
1600   int last = regno + hard_regno_nregs[regno][mode];
1601   while (regno < last)
1602     {
1603       SET_HARD_REG_BIT (hard_regs_live, regno);
1604       regno++;
1605     }
1606 }
1607 \f
1608 /* Try to set a preference for an allocno to a hard register.
1609    We are passed DEST and SRC which are the operands of a SET.  It is known
1610    that SRC is a register.  If SRC or the first operand of SRC is a register,
1611    try to set a preference.  If one of the two is a hard register and the other
1612    is a pseudo-register, mark the preference.
1613
1614    Note that we are not as aggressive as local-alloc in trying to tie a
1615    pseudo-register to a hard register.  */
1616
1617 static void
1618 set_preference (rtx dest, rtx src)
1619 {
1620   unsigned int src_regno, dest_regno;
1621   /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1622      to compensate for subregs in SRC or DEST.  */
1623   int offset = 0;
1624   unsigned int i;
1625   int copy = 1;
1626
1627   if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1628     src = XEXP (src, 0), copy = 0;
1629
1630   /* Get the reg number for both SRC and DEST.
1631      If neither is a reg, give up.  */
1632
1633   if (REG_P (src))
1634     src_regno = REGNO (src);
1635   else if (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src)))
1636     {
1637       src_regno = REGNO (SUBREG_REG (src));
1638
1639       if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1640         offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1641                                        GET_MODE (SUBREG_REG (src)),
1642                                        SUBREG_BYTE (src),
1643                                        GET_MODE (src));
1644       else
1645         offset += (SUBREG_BYTE (src)
1646                    / REGMODE_NATURAL_SIZE (GET_MODE (src)));
1647     }
1648   else
1649     return;
1650
1651   if (REG_P (dest))
1652     dest_regno = REGNO (dest);
1653   else if (GET_CODE (dest) == SUBREG && REG_P (SUBREG_REG (dest)))
1654     {
1655       dest_regno = REGNO (SUBREG_REG (dest));
1656
1657       if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1658         offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1659                                        GET_MODE (SUBREG_REG (dest)),
1660                                        SUBREG_BYTE (dest),
1661                                        GET_MODE (dest));
1662       else
1663         offset -= (SUBREG_BYTE (dest)
1664                    / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
1665     }
1666   else
1667     return;
1668
1669   /* Convert either or both to hard reg numbers.  */
1670
1671   if (reg_renumber[src_regno] >= 0)
1672     src_regno = reg_renumber[src_regno];
1673
1674   if (reg_renumber[dest_regno] >= 0)
1675     dest_regno = reg_renumber[dest_regno];
1676
1677   /* Now if one is a hard reg and the other is a global pseudo
1678      then give the other a preference.  */
1679
1680   if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1681       && reg_allocno[src_regno] >= 0)
1682     {
1683       dest_regno -= offset;
1684       if (dest_regno < FIRST_PSEUDO_REGISTER)
1685         {
1686           if (copy)
1687             SET_REGBIT (hard_reg_copy_preferences,
1688                         reg_allocno[src_regno], dest_regno);
1689
1690           SET_REGBIT (hard_reg_preferences,
1691                       reg_allocno[src_regno], dest_regno);
1692           for (i = dest_regno;
1693                i < dest_regno + hard_regno_nregs[dest_regno][GET_MODE (dest)];
1694                i++)
1695             SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1696         }
1697     }
1698
1699   if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1700       && reg_allocno[dest_regno] >= 0)
1701     {
1702       src_regno += offset;
1703       if (src_regno < FIRST_PSEUDO_REGISTER)
1704         {
1705           if (copy)
1706             SET_REGBIT (hard_reg_copy_preferences,
1707                         reg_allocno[dest_regno], src_regno);
1708
1709           SET_REGBIT (hard_reg_preferences,
1710                       reg_allocno[dest_regno], src_regno);
1711           for (i = src_regno;
1712                i < src_regno + hard_regno_nregs[src_regno][GET_MODE (src)];
1713                i++)
1714             SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1715         }
1716     }
1717 }
1718 \f
1719 /* Indicate that hard register number FROM was eliminated and replaced with
1720    an offset from hard register number TO.  The status of hard registers live
1721    at the start of a basic block is updated by replacing a use of FROM with
1722    a use of TO.  */
1723
1724 void
1725 mark_elimination (int from, int to)
1726 {
1727   basic_block bb;
1728
1729   FOR_EACH_BB (bb)
1730     {
1731       regset r = bb->global_live_at_start;
1732       if (REGNO_REG_SET_P (r, from))
1733         {
1734           CLEAR_REGNO_REG_SET (r, from);
1735           SET_REGNO_REG_SET (r, to);
1736         }
1737     }
1738 }
1739 \f
1740 /* Used for communication between the following functions.  Holds the
1741    current life information.  */
1742 static regset live_relevant_regs;
1743
1744 /* Record in live_relevant_regs and REGS_SET that register REG became live.
1745    This is called via note_stores.  */
1746 static void
1747 reg_becomes_live (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *regs_set)
1748 {
1749   int regno;
1750
1751   if (GET_CODE (reg) == SUBREG)
1752     reg = SUBREG_REG (reg);
1753
1754   if (!REG_P (reg))
1755     return;
1756
1757   regno = REGNO (reg);
1758   if (regno < FIRST_PSEUDO_REGISTER)
1759     {
1760       int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1761       while (nregs-- > 0)
1762         {
1763           SET_REGNO_REG_SET (live_relevant_regs, regno);
1764           if (! fixed_regs[regno])
1765             SET_REGNO_REG_SET ((regset) regs_set, regno);
1766           regno++;
1767         }
1768     }
1769   else if (reg_renumber[regno] >= 0)
1770     {
1771       SET_REGNO_REG_SET (live_relevant_regs, regno);
1772       SET_REGNO_REG_SET ((regset) regs_set, regno);
1773     }
1774 }
1775
1776 /* Record in live_relevant_regs that register REGNO died.  */
1777 static void
1778 reg_dies (int regno, enum machine_mode mode, struct insn_chain *chain)
1779 {
1780   if (regno < FIRST_PSEUDO_REGISTER)
1781     {
1782       int nregs = hard_regno_nregs[regno][mode];
1783       while (nregs-- > 0)
1784         {
1785           CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1786           if (! fixed_regs[regno])
1787             SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1788           regno++;
1789         }
1790     }
1791   else
1792     {
1793       CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1794       if (reg_renumber[regno] >= 0)
1795         SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1796     }
1797 }
1798
1799 /* Walk the insns of the current function and build reload_insn_chain,
1800    and record register life information.  */
1801 void
1802 build_insn_chain (rtx first)
1803 {
1804   struct insn_chain **p = &reload_insn_chain;
1805   struct insn_chain *prev = 0;
1806   basic_block b = ENTRY_BLOCK_PTR->next_bb;
1807   regset_head live_relevant_regs_head;
1808
1809   live_relevant_regs = INITIALIZE_REG_SET (live_relevant_regs_head);
1810
1811   for (; first; first = NEXT_INSN (first))
1812     {
1813       struct insn_chain *c;
1814
1815       if (first == BB_HEAD (b))
1816         {
1817           int i;
1818
1819           CLEAR_REG_SET (live_relevant_regs);
1820
1821           EXECUTE_IF_SET_IN_BITMAP
1822             (b->global_live_at_start, 0, i,
1823              {
1824                if (i < FIRST_PSEUDO_REGISTER
1825                    ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1826                    : reg_renumber[i] >= 0)
1827                  SET_REGNO_REG_SET (live_relevant_regs, i);
1828              });
1829         }
1830
1831       if (!NOTE_P (first) && !BARRIER_P (first))
1832         {
1833           c = new_insn_chain ();
1834           c->prev = prev;
1835           prev = c;
1836           *p = c;
1837           p = &c->next;
1838           c->insn = first;
1839           c->block = b->index;
1840
1841           if (INSN_P (first))
1842             {
1843               rtx link;
1844
1845               /* Mark the death of everything that dies in this instruction.  */
1846
1847               for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1848                 if (REG_NOTE_KIND (link) == REG_DEAD
1849                     && REG_P (XEXP (link, 0)))
1850                   reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1851                             c);
1852
1853               COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1854
1855               /* Mark everything born in this instruction as live.  */
1856
1857               note_stores (PATTERN (first), reg_becomes_live,
1858                            &c->dead_or_set);
1859             }
1860           else
1861             COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1862
1863           if (INSN_P (first))
1864             {
1865               rtx link;
1866
1867               /* Mark anything that is set in this insn and then unused as dying.  */
1868
1869               for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1870                 if (REG_NOTE_KIND (link) == REG_UNUSED
1871                     && REG_P (XEXP (link, 0)))
1872                   reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1873                             c);
1874             }
1875         }
1876
1877       if (first == BB_END (b))
1878         b = b->next_bb;
1879
1880       /* Stop after we pass the end of the last basic block.  Verify that
1881          no real insns are after the end of the last basic block.
1882
1883          We may want to reorganize the loop somewhat since this test should
1884          always be the right exit test.  Allow an ADDR_VEC or ADDR_DIF_VEC if
1885          the previous real insn is a JUMP_INSN.  */
1886       if (b == EXIT_BLOCK_PTR)
1887         {
1888           for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1889             if (INSN_P (first)
1890                 && GET_CODE (PATTERN (first)) != USE
1891                 && ! ((GET_CODE (PATTERN (first)) == ADDR_VEC
1892                        || GET_CODE (PATTERN (first)) == ADDR_DIFF_VEC)
1893                       && prev_real_insn (first) != 0
1894                       && JUMP_P (prev_real_insn (first))))
1895               abort ();
1896           break;
1897         }
1898     }
1899   FREE_REG_SET (live_relevant_regs);
1900   *p = 0;
1901 }
1902 \f
1903 /* Print debugging trace information if -dg switch is given,
1904    showing the information on which the allocation decisions are based.  */
1905
1906 static void
1907 dump_conflicts (FILE *file)
1908 {
1909   int i;
1910   int has_preferences;
1911   int nregs;
1912   nregs = 0;
1913   for (i = 0; i < max_allocno; i++)
1914     {
1915       if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1916         continue;
1917       nregs++;
1918     }
1919   fprintf (file, ";; %d regs to allocate:", nregs);
1920   for (i = 0; i < max_allocno; i++)
1921     {
1922       int j;
1923       if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1924         continue;
1925       fprintf (file, " %d", allocno[allocno_order[i]].reg);
1926       for (j = 0; j < max_regno; j++)
1927         if (reg_allocno[j] == allocno_order[i]
1928             && j != allocno[allocno_order[i]].reg)
1929           fprintf (file, "+%d", j);
1930       if (allocno[allocno_order[i]].size != 1)
1931         fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1932     }
1933   fprintf (file, "\n");
1934
1935   for (i = 0; i < max_allocno; i++)
1936     {
1937       int j;
1938       fprintf (file, ";; %d conflicts:", allocno[i].reg);
1939       for (j = 0; j < max_allocno; j++)
1940         if (CONFLICTP (j, i))
1941           fprintf (file, " %d", allocno[j].reg);
1942       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1943         if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1944           fprintf (file, " %d", j);
1945       fprintf (file, "\n");
1946
1947       has_preferences = 0;
1948       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1949         if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1950           has_preferences = 1;
1951
1952       if (! has_preferences)
1953         continue;
1954       fprintf (file, ";; %d preferences:", allocno[i].reg);
1955       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1956         if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1957           fprintf (file, " %d", j);
1958       fprintf (file, "\n");
1959     }
1960   fprintf (file, "\n");
1961 }
1962
1963 void
1964 dump_global_regs (FILE *file)
1965 {
1966   int i, j;
1967
1968   fprintf (file, ";; Register dispositions:\n");
1969   for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1970     if (reg_renumber[i] >= 0)
1971       {
1972         fprintf (file, "%d in %d  ", i, reg_renumber[i]);
1973         if (++j % 6 == 0)
1974           fprintf (file, "\n");
1975       }
1976
1977   fprintf (file, "\n\n;; Hard regs used: ");
1978   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1979     if (regs_ever_live[i])
1980       fprintf (file, " %d", i);
1981   fprintf (file, "\n\n");
1982 }
1983
1984 \f
1985
1986 /* This page contains code to make live information more accurate.
1987    The accurate register liveness at program point P means:
1988      o there is a path from P to usage of the register and the
1989        register is not redefined or killed on the path.
1990      o register at P is partially available, i.e. there is a path from
1991        a register definition to the point P and the register is not
1992        killed (clobbered) on the path
1993
1994    The standard GCC live information means only the first condition.
1995    Without the partial availability, there will be more register
1996    conflicts and as a consequence worse register allocation.  The
1997    typical example where the information can be different is a
1998    register initialized in the loop at the basic block preceding the
1999    loop in CFG.  */
2000
2001 /* The following structure contains basic block data flow information
2002    used to calculate partial availability of registers.  */
2003
2004 struct bb_info
2005 {
2006   /* The basic block reverse post-order number.  */
2007   int rts_number;
2008   /* Registers used uninitialized in an insn in which there is an
2009      early clobbered register might get the same hard register.  */
2010   bitmap earlyclobber;
2011   /* Registers correspondingly killed (clobbered) and defined but not
2012      killed afterward in the basic block.  */
2013   bitmap killed, avloc;
2014   /* Registers partially available correspondingly at the start and
2015      end of the basic block.  */
2016   bitmap pavin, pavout;
2017 };
2018
2019 /* Macros for accessing data flow information of basic blocks.  */
2020
2021 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
2022 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
2023
2024 /* The function allocates the info structures of each basic block.  It
2025    also initialized PAVIN and PAVOUT as if all hard registers were
2026    partially available.  */
2027
2028 static void
2029 allocate_bb_info (void)
2030 {
2031   int i;
2032   basic_block bb;
2033   struct bb_info *bb_info;
2034   bitmap init;
2035
2036   alloc_aux_for_blocks (sizeof (struct bb_info));
2037   init = BITMAP_XMALLOC ();
2038   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2039     bitmap_set_bit (init, i);
2040   FOR_EACH_BB (bb)
2041     {
2042       bb_info = bb->aux;
2043       bb_info->earlyclobber = BITMAP_XMALLOC ();
2044       bb_info->avloc = BITMAP_XMALLOC ();
2045       bb_info->killed = BITMAP_XMALLOC ();
2046       bb_info->pavin = BITMAP_XMALLOC ();
2047       bb_info->pavout = BITMAP_XMALLOC ();
2048       bitmap_copy (bb_info->pavin, init);
2049       bitmap_copy (bb_info->pavout, init);
2050     }
2051   BITMAP_XFREE (init);
2052 }
2053
2054 /* The function frees the allocated info of all basic blocks.  */
2055
2056 static void
2057 free_bb_info (void)
2058 {
2059   basic_block bb;
2060   struct bb_info *bb_info;
2061
2062   FOR_EACH_BB (bb)
2063     {
2064       bb_info = BB_INFO (bb);
2065       BITMAP_XFREE (bb_info->pavout);
2066       BITMAP_XFREE (bb_info->pavin);
2067       BITMAP_XFREE (bb_info->killed);
2068       BITMAP_XFREE (bb_info->avloc);
2069       BITMAP_XFREE (bb_info->earlyclobber);
2070     }
2071   free_aux_for_blocks ();
2072 }
2073
2074 /* The function modifies local info for register REG being changed in
2075    SETTER.  DATA is used to pass the current basic block info.  */
2076
2077 static void
2078 mark_reg_change (rtx reg, rtx setter, void *data)
2079 {
2080   int regno;
2081   basic_block bb = data;
2082   struct bb_info *bb_info = BB_INFO (bb);
2083
2084   if (GET_CODE (reg) == SUBREG)
2085     reg = SUBREG_REG (reg);
2086
2087   if (!REG_P (reg))
2088     return;
2089
2090   regno = REGNO (reg);
2091   bitmap_set_bit (bb_info->killed, regno);
2092   
2093   if (GET_CODE (setter) != CLOBBER)
2094     bitmap_set_bit (bb_info->avloc, regno);
2095   else
2096     bitmap_clear_bit (bb_info->avloc, regno);
2097 }
2098
2099 /* Classes of registers which could be early clobbered in the current
2100    insn.  */
2101
2102 static varray_type earlyclobber_regclass;
2103
2104 /* The function stores classes of registers which could be early
2105    clobbered in INSN.  */
2106
2107 static void
2108 check_earlyclobber (rtx insn)
2109 {
2110   int opno;
2111
2112   extract_insn (insn);
2113
2114   VARRAY_POP_ALL (earlyclobber_regclass);
2115   for (opno = 0; opno < recog_data.n_operands; opno++)
2116     {
2117       char c;
2118       bool amp_p;
2119       int i;
2120       enum reg_class class;
2121       const char *p = recog_data.constraints[opno];
2122
2123       class = NO_REGS;
2124       amp_p = false;
2125       for (;;)
2126         {
2127           c = *p;
2128           switch (c)
2129             {
2130             case '=':  case '+':  case '?':
2131             case '#':  case '!':
2132             case '*':  case '%':
2133             case 'm':  case '<':  case '>':  case 'V':  case 'o':
2134             case 'E':  case 'F':  case 'G':  case 'H':
2135             case 's':  case 'i':  case 'n':
2136             case 'I':  case 'J':  case 'K':  case 'L':
2137             case 'M':  case 'N':  case 'O':  case 'P':
2138             case 'X':
2139             case '0': case '1':  case '2':  case '3':  case '4':
2140             case '5': case '6':  case '7':  case '8':  case '9':
2141               /* These don't say anything we care about.  */
2142               break;
2143
2144             case '&':
2145               amp_p = true;
2146               break;
2147             case '\0':
2148             case ',':
2149               if (amp_p && class != NO_REGS)
2150                 {
2151                   for (i = VARRAY_ACTIVE_SIZE (earlyclobber_regclass) - 1;
2152                        i >= 0; i--)
2153                     if (VARRAY_INT (earlyclobber_regclass, i) == (int) class)
2154                       break;
2155                   if (i < 0)
2156                     VARRAY_PUSH_INT (earlyclobber_regclass, (int) class);
2157                 }
2158               
2159               amp_p = false;
2160               class = NO_REGS;
2161               break;
2162
2163             case 'r':
2164               class = GENERAL_REGS;
2165               break;
2166
2167             default:
2168               class = REG_CLASS_FROM_CONSTRAINT (c, p);
2169               break;
2170             }
2171           if (c == '\0')
2172             break;
2173           p += CONSTRAINT_LEN (c, p);
2174         }
2175     }
2176 }
2177
2178 /* The function returns true if register classes C1 and C2 inetrsect.  */
2179
2180 static bool
2181 regclass_intersect (enum reg_class c1, enum reg_class c2)
2182 {
2183   HARD_REG_SET rs, zero;
2184
2185   CLEAR_HARD_REG_SET (zero);
2186   COPY_HARD_REG_SET(rs, reg_class_contents [c1]);
2187   AND_HARD_REG_SET (rs, reg_class_contents [c2]);
2188   GO_IF_HARD_REG_EQUAL (zero, rs, yes);
2189   return true;
2190  yes:
2191   return false;
2192 }
2193
2194 /* The function checks that pseudo-register *X has a class
2195    intersecting with the class of pseudo-register could be early
2196    clobbered in the same insn.  */
2197
2198 static int
2199 mark_reg_use_for_earlyclobber (rtx *x, void *data ATTRIBUTE_UNUSED)
2200 {
2201   enum reg_class pref_class, alt_class;
2202   int i, regno;
2203   basic_block bb = data;
2204   struct bb_info *bb_info = BB_INFO (bb);
2205
2206   if (GET_CODE (*x) == REG && REGNO (*x) >= FIRST_PSEUDO_REGISTER)
2207     {
2208       regno = REGNO (*x);
2209       if (bitmap_bit_p (bb_info->killed, regno)
2210           || bitmap_bit_p (bb_info->avloc, regno))
2211         return 0;
2212       pref_class = reg_preferred_class (regno);
2213       alt_class = reg_alternate_class (regno);
2214       for (i = VARRAY_ACTIVE_SIZE (earlyclobber_regclass) - 1; i >= 0; i--)
2215         if (regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2216                                 pref_class)
2217             || (VARRAY_INT (earlyclobber_regclass, i) != NO_REGS
2218                 && regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2219                                        alt_class)))
2220           {
2221             bitmap_set_bit (bb_info->earlyclobber, regno);
2222             break;
2223           }
2224     }
2225   return 0;
2226 }
2227
2228 /* The function processes all pseudo-registers in *X with the aid of
2229    previous function.  */
2230
2231 static void
2232 mark_reg_use_for_earlyclobber_1 (rtx *x, void *data)
2233 {
2234   for_each_rtx (x, mark_reg_use_for_earlyclobber, data);
2235 }
2236
2237 /* The function calculates local info for each basic block.  */
2238
2239 static void
2240 calculate_local_reg_bb_info (void)
2241 {
2242   basic_block bb;
2243   rtx insn, bound;
2244
2245   VARRAY_INT_INIT (earlyclobber_regclass, 20,
2246                    "classes of registers early clobbered in an insn");
2247   FOR_EACH_BB (bb)
2248     {
2249       bound = NEXT_INSN (BB_END (bb));
2250       for (insn = BB_HEAD (bb); insn != bound; insn = NEXT_INSN (insn))
2251         if (INSN_P (insn))
2252           {
2253             note_stores (PATTERN (insn), mark_reg_change, bb);
2254             check_earlyclobber (insn);
2255             note_uses (&PATTERN (insn), mark_reg_use_for_earlyclobber_1, bb);
2256           }
2257     }
2258 }
2259
2260 /* The function sets up reverse post-order number of each basic
2261    block.  */
2262
2263 static void
2264 set_up_bb_rts_numbers (void)
2265 {
2266   int i;
2267   int *rts_order;
2268   
2269   rts_order = xmalloc (sizeof (int) * n_basic_blocks);
2270   flow_reverse_top_sort_order_compute (rts_order);
2271   for (i = 0; i < n_basic_blocks; i++)
2272     BB_INFO_BY_INDEX (rts_order [i])->rts_number = i;
2273   free (rts_order);
2274 }
2275
2276 /* Compare function for sorting blocks in reverse postorder.  */
2277
2278 static int
2279 rpost_cmp (const void *bb1, const void *bb2)
2280 {
2281   basic_block b1 = *(basic_block *) bb1, b2 = *(basic_block *) bb2;
2282
2283   return BB_INFO (b2)->rts_number - BB_INFO (b1)->rts_number;
2284 }
2285
2286 /* The function calculates partial availability of registers.  The
2287    function calculates partial availability at the end of basic block
2288    BB by propagating partial availability at end of predecessor basic
2289    block PRED.  The function returns true if the partial availability
2290    at the end of BB has been changed or if CHANGED_P.  We have the
2291    following equations:
2292
2293      bb.pavin = empty for entry block | union (pavout of predecessors)
2294      bb.pavout = union (bb.pavin - b.killed, bb.avloc)  */
2295
2296 static bool
2297 modify_bb_reg_pav (basic_block bb, basic_block pred, bool changed_p)
2298 {
2299   struct bb_info *bb_info;
2300   bitmap bb_pavin, bb_pavout;
2301
2302   bb_info = BB_INFO (bb);
2303   bb_pavin = bb_info->pavin;
2304   bb_pavout = bb_info->pavout;
2305   if (pred->index != ENTRY_BLOCK)
2306     bitmap_a_or_b (bb_pavin, bb_pavin, BB_INFO (pred)->pavout);
2307   changed_p |= bitmap_union_of_diff (bb_pavout, bb_info->avloc,
2308                                      bb_pavin, bb_info->killed);
2309   return changed_p;
2310 }
2311
2312 /* The function calculates partial register availability.  */
2313
2314 static void
2315 calculate_reg_pav (void)
2316 {
2317   basic_block bb, succ;
2318   edge e;
2319   bool changed_p;
2320   int i, nel;
2321   varray_type bbs, new_bbs, temp;
2322   basic_block *bb_array;
2323   sbitmap wset;
2324
2325   VARRAY_BB_INIT (bbs, n_basic_blocks, "basic blocks");
2326   VARRAY_BB_INIT (new_bbs, n_basic_blocks, "basic blocks for the next iter.");
2327   FOR_EACH_BB (bb)
2328     {
2329       VARRAY_PUSH_BB (bbs, bb);
2330     }
2331   wset = sbitmap_alloc (n_basic_blocks + 1);
2332   while (VARRAY_ACTIVE_SIZE (bbs))
2333     {
2334       bb_array = &VARRAY_BB (bbs, 0);
2335       nel = VARRAY_ACTIVE_SIZE (bbs);
2336       qsort (bb_array, nel, sizeof (basic_block), rpost_cmp);
2337       sbitmap_zero (wset);
2338       for (i = 0; i < nel; i++)
2339         {
2340           bb = bb_array [i];
2341           changed_p = 0;
2342           for (e = bb->pred; e; e = e->pred_next)
2343             changed_p = modify_bb_reg_pav (bb, e->src, changed_p);
2344           if (changed_p)
2345             for (e = bb->succ; e; e = e->succ_next)
2346               {
2347                 succ = e->dest;
2348                 if (succ->index != EXIT_BLOCK && !TEST_BIT (wset, succ->index))
2349                   {
2350                     SET_BIT (wset, succ->index);
2351                     VARRAY_PUSH_BB (new_bbs, succ);
2352                   }
2353               }
2354         }
2355       temp = bbs;
2356       bbs = new_bbs;
2357       new_bbs = temp;
2358       VARRAY_POP_ALL (new_bbs);
2359     }
2360   sbitmap_free (wset);
2361 }
2362
2363 /* The following function makes live information more accurate by
2364    modifying global_live_at_start and global_live_at_end of basic
2365    blocks.  After the function call a register lives at a program
2366    point only if it is initialized on a path from CFG entry to the
2367    program point.  The standard GCC life analysis permits registers to
2368    live uninitialized.  */
2369
2370 static void
2371 make_accurate_live_analysis (void)
2372 {
2373   basic_block bb;
2374   struct bb_info *bb_info;
2375
2376   max_regno = max_reg_num ();
2377   compact_blocks ();
2378   allocate_bb_info ();
2379   calculate_local_reg_bb_info ();
2380   set_up_bb_rts_numbers ();
2381   calculate_reg_pav ();
2382   FOR_EACH_BB (bb)
2383     {
2384       bb_info = BB_INFO (bb);
2385       
2386       /* Reload can assign the same hard register to uninitialized
2387          pseudo-register and early clobbered pseudo-register in an
2388          insn if the pseudo-register is used first time in given BB
2389          and not lived at the BB start.  To prevent this we don't
2390          change life information for such pseudo-registers.  */
2391       bitmap_a_or_b (bb_info->pavin, bb_info->pavin, bb_info->earlyclobber);
2392       bitmap_a_and_b (bb->global_live_at_start, bb->global_live_at_start,
2393                       bb_info->pavin);
2394       bitmap_a_and_b (bb->global_live_at_end, bb->global_live_at_end,
2395                       bb_info->pavout);
2396     }
2397   free_bb_info ();
2398 }