OSDN Git Service

* global.c (global_alloc): Don't pass HARD_CONST (0) to find_reg,
[pf3gnuchains/gcc-fork.git] / gcc / global.c
1 /* Allocate registers for pseudo-registers that span basic blocks.
2    Copyright (C) 1987, 88, 91, 94, 96, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "rtl.h"
26 #include "flags.h"
27 #include "basic-block.h"
28 #include "hard-reg-set.h"
29 #include "regs.h"
30 #include "insn-config.h"
31 #include "output.h"
32
33 /* This pass of the compiler performs global register allocation.
34    It assigns hard register numbers to all the pseudo registers
35    that were not handled in local_alloc.  Assignments are recorded
36    in the vector reg_renumber, not by changing the rtl code.
37    (Such changes are made by final).  The entry point is
38    the function global_alloc.
39
40    After allocation is complete, the reload pass is run as a subroutine
41    of this pass, so that when a pseudo reg loses its hard reg due to
42    spilling it is possible to make a second attempt to find a hard
43    reg for it.  The reload pass is independent in other respects
44    and it is run even when stupid register allocation is in use.
45
46    1. count the pseudo-registers still needing allocation
47    and assign allocation-numbers (allocnos) to them.
48    Set up tables reg_allocno and allocno_reg to map 
49    reg numbers to allocnos and vice versa.
50    max_allocno gets the number of allocnos in use.
51
52    2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
53    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
54    for conflicts between allocnos and explicit hard register use
55    (which includes use of pseudo-registers allocated by local_alloc).
56
57    3. for each basic block
58     walk forward through the block, recording which
59     unallocated registers and which hardware registers are live.
60     Build the conflict matrix between the unallocated registers
61     and another of unallocated registers versus hardware registers.
62     Also record the preferred hardware registers
63     for each unallocated one.
64
65    4. Sort a table of the allocnos into order of
66    desirability of the variables.
67
68    5. Allocate the variables in that order; each if possible into
69    a preferred register, else into another register.  */
70 \f
71 /* Number of pseudo-registers still requiring allocation
72    (not allocated by local_allocate).  */
73
74 static int max_allocno;
75
76 /* Indexed by (pseudo) reg number, gives the allocno, or -1
77    for pseudo registers already allocated by local_allocate.  */
78
79 int *reg_allocno;
80
81 /* Indexed by allocno, gives the reg number.  */
82
83 static int *allocno_reg;
84
85 /* A vector of the integers from 0 to max_allocno-1,
86    sorted in the order of first-to-be-allocated first.  */
87
88 static int *allocno_order;
89
90 /* Indexed by an allocno, gives the number of consecutive
91    hard registers needed by that pseudo reg.  */
92
93 static int *allocno_size;
94
95 /* Indexed by (pseudo) reg number, gives the number of another
96    lower-numbered pseudo reg which can share a hard reg with this pseudo
97    *even if the two pseudos would otherwise appear to conflict*.  */
98
99 static int *reg_may_share;
100
101 /* Define the number of bits in each element of `conflicts' and what
102    type that element has.  We use the largest integer format on the
103    host machine.  */
104
105 #define INT_BITS HOST_BITS_PER_WIDE_INT
106 #define INT_TYPE HOST_WIDE_INT
107
108 /* max_allocno by max_allocno array of bits,
109    recording whether two allocno's conflict (can't go in the same
110    hardware register).
111
112    `conflicts' is not symmetric; a conflict between allocno's i and j
113    is recorded either in element i,j or in element j,i.  */
114
115 static INT_TYPE *conflicts;
116
117 /* Number of ints require to hold max_allocno bits.
118    This is the length of a row in `conflicts'.  */
119
120 static int allocno_row_words;
121
122 /* Two macros to test or store 1 in an element of `conflicts'.  */
123
124 #define CONFLICTP(I, J) \
125  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]   \
126   & ((INT_TYPE) 1 << ((J) % INT_BITS)))
127
128 #define SET_CONFLICT(I, J) \
129  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]   \
130   |= ((INT_TYPE) 1 << ((J) % INT_BITS)))
131
132 /* Set of hard regs currently live (during scan of all insns).  */
133
134 static HARD_REG_SET hard_regs_live;
135
136 /* Indexed by N, set of hard regs conflicting with allocno N.  */
137
138 static HARD_REG_SET *hard_reg_conflicts;
139
140 /* Indexed by N, set of hard regs preferred by allocno N.
141    This is used to make allocnos go into regs that are copied to or from them,
142    when possible, to reduce register shuffling.  */
143
144 static HARD_REG_SET *hard_reg_preferences;
145
146 /* Similar, but just counts register preferences made in simple copy
147    operations, rather than arithmetic.  These are given priority because
148    we can always eliminate an insn by using these, but using a register
149    in the above list won't always eliminate an insn.  */
150
151 static HARD_REG_SET *hard_reg_copy_preferences;
152
153 /* Similar to hard_reg_preferences, but includes bits for subsequent
154    registers when an allocno is multi-word.  The above variable is used for
155    allocation while this is used to build reg_someone_prefers, below.  */
156
157 static HARD_REG_SET *hard_reg_full_preferences;
158
159 /* Indexed by N, set of hard registers that some later allocno has a
160    preference for.  */
161
162 static HARD_REG_SET *regs_someone_prefers;
163
164 /* Set of registers that global-alloc isn't supposed to use.  */
165
166 static HARD_REG_SET no_global_alloc_regs;
167
168 /* Set of registers used so far.  */
169
170 static HARD_REG_SET regs_used_so_far;
171
172 /* Number of calls crossed by each allocno.  */
173
174 static int *allocno_calls_crossed;
175
176 /* Number of refs (weighted) to each allocno.  */
177
178 static int *allocno_n_refs;
179
180 /* Guess at live length of each allocno.
181    This is actually the max of the live lengths of the regs.  */
182
183 static int *allocno_live_length;
184
185 /* Number of refs (weighted) to each hard reg, as used by local alloc.
186    It is zero for a reg that contains global pseudos or is explicitly used.  */
187
188 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
189
190 /* Guess at live length of each hard reg, as used by local alloc.
191    This is actually the sum of the live lengths of the specific regs.  */
192
193 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
194
195 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
196    for vector element I, and hard register number J.  */
197
198 #define REGBITP(TABLE, I, J)     TEST_HARD_REG_BIT (TABLE[I], J)
199
200 /* Set to 1 a bit in a vector of HARD_REG_SETs.  Works like REGBITP.  */
201
202 #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (TABLE[I], J)
203
204 /* Bit mask for allocnos live at current point in the scan.  */
205
206 static INT_TYPE *allocnos_live;
207
208 /* Test, set or clear bit number I in allocnos_live,
209    a bit vector indexed by allocno.  */
210
211 #define ALLOCNO_LIVE_P(I) \
212   (allocnos_live[(I) / INT_BITS] & ((INT_TYPE) 1 << ((I) % INT_BITS)))
213
214 #define SET_ALLOCNO_LIVE(I) \
215   (allocnos_live[(I) / INT_BITS] |= ((INT_TYPE) 1 << ((I) % INT_BITS)))
216
217 #define CLEAR_ALLOCNO_LIVE(I) \
218   (allocnos_live[(I) / INT_BITS] &= ~((INT_TYPE) 1 << ((I) % INT_BITS)))
219
220 /* This is turned off because it doesn't work right for DImode.
221    (And it is only used for DImode, so the other cases are worthless.)
222    The problem is that it isn't true that there is NO possibility of conflict;
223    only that there is no conflict if the two pseudos get the exact same regs.
224    If they were allocated with a partial overlap, there would be a conflict.
225    We can't safely turn off the conflict unless we have another way to
226    prevent the partial overlap.
227
228    Idea: change hard_reg_conflicts so that instead of recording which
229    hard regs the allocno may not overlap, it records where the allocno
230    may not start.  Change both where it is used and where it is updated.
231    Then there is a way to record that (reg:DI 108) may start at 10
232    but not at 9 or 11.  There is still the question of how to record
233    this semi-conflict between two pseudos.  */
234 #if 0
235 /* Reg pairs for which conflict after the current insn
236    is inhibited by a REG_NO_CONFLICT note.
237    If the table gets full, we ignore any other notes--that is conservative.  */
238 #define NUM_NO_CONFLICT_PAIRS 4
239 /* Number of pairs in use in this insn.  */
240 int n_no_conflict_pairs;
241 static struct { int allocno1, allocno2;}
242   no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
243 #endif /* 0 */
244
245 /* Record all regs that are set in any one insn.
246    Communication from mark_reg_{store,clobber} and global_conflicts.  */
247
248 static rtx *regs_set;
249 static int n_regs_set;
250
251 /* All registers that can be eliminated.  */
252
253 static HARD_REG_SET eliminable_regset;
254
255 static int allocno_compare      PROTO((const GENERIC_PTR, const GENERIC_PTR));
256 static void global_conflicts    PROTO((void));
257 static void expand_preferences  PROTO((void));
258 static void prune_preferences   PROTO((void));
259 static void find_reg            PROTO((int, HARD_REG_SET, int, int, int));
260 static void record_one_conflict PROTO((int));
261 static void record_conflicts    PROTO((short *, int));
262 static void mark_reg_store      PROTO((rtx, rtx));
263 static void mark_reg_clobber    PROTO((rtx, rtx));
264 static void mark_reg_conflicts  PROTO((rtx));
265 static void mark_reg_death      PROTO((rtx));
266 static void mark_reg_live_nc    PROTO((int, enum machine_mode));
267 static void set_preference      PROTO((rtx, rtx));
268 static void dump_conflicts      PROTO((FILE *));
269 \f
270 /* Perform allocation of pseudo-registers not allocated by local_alloc.
271    FILE is a file to output debugging information on,
272    or zero if such output is not desired.
273
274    Return value is nonzero if reload failed
275    and we must not do any more for this function.  */
276
277 int
278 global_alloc (file)
279      FILE *file;
280 {
281   int retval;
282 #ifdef ELIMINABLE_REGS
283   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
284 #endif
285   int need_fp
286     = (! flag_omit_frame_pointer
287 #ifdef EXIT_IGNORE_STACK
288        || (current_function_calls_alloca && EXIT_IGNORE_STACK)
289 #endif
290        || FRAME_POINTER_REQUIRED);
291
292   register size_t i;
293   rtx x;
294
295   max_allocno = 0;
296
297   /* A machine may have certain hard registers that
298      are safe to use only within a basic block.  */
299
300   CLEAR_HARD_REG_SET (no_global_alloc_regs);
301 #ifdef OVERLAPPING_REGNO_P
302   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
303     if (OVERLAPPING_REGNO_P (i))
304       SET_HARD_REG_BIT (no_global_alloc_regs, i);
305 #endif
306
307   /* Build the regset of all eliminable registers and show we can't use those
308      that we already know won't be eliminated.  */
309 #ifdef ELIMINABLE_REGS
310   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
311     {
312       SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
313
314       if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
315           || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
316         SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
317     }
318 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
319   SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
320   if (need_fp)
321     SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
322 #endif
323
324 #else
325   SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
326   if (need_fp)
327     SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
328 #endif
329
330   /* Track which registers have already been used.  Start with registers
331      explicitly in the rtl, then registers allocated by local register
332      allocation.  */
333
334   CLEAR_HARD_REG_SET (regs_used_so_far);
335 #ifdef LEAF_REGISTERS
336   /* If we are doing the leaf function optimization, and this is a leaf
337      function, it means that the registers that take work to save are those
338      that need a register window.  So prefer the ones that can be used in
339      a leaf function.  */
340   {
341     char *cheap_regs;
342     static char leaf_regs[] = LEAF_REGISTERS;
343
344     if (only_leaf_regs_used () && leaf_function_p ())
345       cheap_regs = leaf_regs;
346     else
347       cheap_regs = call_used_regs;
348     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
349       if (regs_ever_live[i] || cheap_regs[i])
350         SET_HARD_REG_BIT (regs_used_so_far, i);
351   }
352 #else
353   /* We consider registers that do not have to be saved over calls as if
354      they were already used since there is no cost in using them.  */
355   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
356     if (regs_ever_live[i] || call_used_regs[i])
357       SET_HARD_REG_BIT (regs_used_so_far, i);
358 #endif
359
360   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
361     if (reg_renumber[i] >= 0)
362       SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
363
364   /* Establish mappings from register number to allocation number
365      and vice versa.  In the process, count the allocnos.  */
366
367   reg_allocno = (int *) alloca (max_regno * sizeof (int));
368
369   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
370     reg_allocno[i] = -1;
371
372   /* Initialize the shared-hard-reg mapping
373      from the list of pairs that may share.  */
374   reg_may_share = (int *) alloca (max_regno * sizeof (int));
375   bzero ((char *) reg_may_share, max_regno * sizeof (int));
376   for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
377     {
378       int r1 = REGNO (XEXP (x, 0));
379       int r2 = REGNO (XEXP (XEXP (x, 1), 0));
380       if (r1 > r2)
381         reg_may_share[r1] = r2;
382       else
383         reg_may_share[r2] = r1;
384     }
385
386   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
387     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
388        that we are supposed to refrain from putting in a hard reg.
389        -2 means do make an allocno but don't allocate it.  */
390     if (REG_N_REFS (i) != 0 && reg_renumber[i] < 0 && REG_LIVE_LENGTH (i) != -1
391         /* Don't allocate pseudos that cross calls,
392            if this function receives a nonlocal goto.  */
393         && (! current_function_has_nonlocal_label
394             || REG_N_CALLS_CROSSED (i) == 0))
395       {
396         if (reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
397           reg_allocno[i] = reg_allocno[reg_may_share[i]];
398         else
399           reg_allocno[i] = max_allocno++;
400         if (REG_LIVE_LENGTH (i) == 0)
401           abort ();
402       }
403     else
404       reg_allocno[i] = -1;
405
406   allocno_reg = (int *) alloca (max_allocno * sizeof (int));
407   allocno_size = (int *) alloca (max_allocno * sizeof (int));
408   allocno_calls_crossed = (int *) alloca (max_allocno * sizeof (int));
409   allocno_n_refs = (int *) alloca (max_allocno * sizeof (int));
410   allocno_live_length = (int *) alloca (max_allocno * sizeof (int));
411   bzero ((char *) allocno_size, max_allocno * sizeof (int));
412   bzero ((char *) allocno_calls_crossed, max_allocno * sizeof (int));
413   bzero ((char *) allocno_n_refs, max_allocno * sizeof (int));
414   bzero ((char *) allocno_live_length, max_allocno * sizeof (int));
415
416   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
417     if (reg_allocno[i] >= 0)
418       {
419         int allocno = reg_allocno[i];
420         allocno_reg[allocno] = i;
421         allocno_size[allocno] = PSEUDO_REGNO_SIZE (i);
422         allocno_calls_crossed[allocno] += REG_N_CALLS_CROSSED (i);
423         allocno_n_refs[allocno] += REG_N_REFS (i);
424         if (allocno_live_length[allocno] < REG_LIVE_LENGTH (i))
425           allocno_live_length[allocno] = REG_LIVE_LENGTH (i);
426       }
427
428   /* Calculate amount of usage of each hard reg by pseudos
429      allocated by local-alloc.  This is to see if we want to
430      override it.  */
431   bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
432   bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
433   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
434     if (reg_allocno[i] < 0 && reg_renumber[i] >= 0)
435       {
436         int regno = reg_renumber[i];
437         int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
438         int j;
439
440         for (j = regno; j < endregno; j++)
441           {
442             local_reg_n_refs[j] += REG_N_REFS (i);
443             local_reg_live_length[j] += REG_LIVE_LENGTH (i);
444           }
445       }
446
447   /* We can't override local-alloc for a reg used not just by local-alloc.  */
448   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
449     if (regs_ever_live[i])
450       local_reg_n_refs[i] = 0;
451
452   /* Likewise for regs used in a SCRATCH.  */
453   for (i = 0; i < scratch_list_length; i++)
454     if (scratch_list[i])
455       {
456         int regno = REGNO (scratch_list[i]);
457         int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch_list[i]));
458         int j;
459
460         for (j = regno; j < lim; j++)
461           local_reg_n_refs[j] = 0;
462       }
463         
464   /* Allocate the space for the conflict and preference tables and
465      initialize them.  */
466
467   hard_reg_conflicts
468     = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
469   bzero ((char *) hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
470
471   hard_reg_preferences
472     = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
473   bzero ((char *) hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
474   
475   hard_reg_copy_preferences
476     = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
477   bzero ((char *) hard_reg_copy_preferences,
478          max_allocno * sizeof (HARD_REG_SET));
479   
480   hard_reg_full_preferences
481     = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
482   bzero ((char *) hard_reg_full_preferences,
483          max_allocno * sizeof (HARD_REG_SET));
484   
485   regs_someone_prefers
486     = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
487   bzero ((char *) regs_someone_prefers, max_allocno * sizeof (HARD_REG_SET));
488
489   allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
490
491   /* We used to use alloca here, but the size of what it would try to
492      allocate would occasionally cause it to exceed the stack limit and
493      cause unpredictable core dumps.  Some examples were > 2Mb in size.  */
494   conflicts = (INT_TYPE *) xmalloc (max_allocno * allocno_row_words
495                                     * sizeof (INT_TYPE));
496   bzero ((char *) conflicts,
497          max_allocno * allocno_row_words * sizeof (INT_TYPE));
498
499   allocnos_live = (INT_TYPE *) alloca (allocno_row_words * sizeof (INT_TYPE));
500
501   /* If there is work to be done (at least one reg to allocate),
502      perform global conflict analysis and allocate the regs.  */
503
504   if (max_allocno > 0)
505     {
506       /* Scan all the insns and compute the conflicts among allocnos
507          and between allocnos and hard regs.  */
508
509       global_conflicts ();
510
511       /* Eliminate conflicts between pseudos and eliminable registers.  If
512          the register is not eliminated, the pseudo won't really be able to
513          live in the eliminable register, so the conflict doesn't matter.
514          If we do eliminate the register, the conflict will no longer exist.
515          So in either case, we can ignore the conflict.  Likewise for
516          preferences.  */
517
518       for (i = 0; i < max_allocno; i++)
519         {
520           AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
521           AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
522                                   eliminable_regset);
523           AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
524         }
525
526       /* Try to expand the preferences by merging them between allocnos.  */
527
528       expand_preferences ();
529
530       /* Determine the order to allocate the remaining pseudo registers.  */
531
532       allocno_order = (int *) alloca (max_allocno * sizeof (int));
533       for (i = 0; i < max_allocno; i++)
534         allocno_order[i] = i;
535
536       /* Default the size to 1, since allocno_compare uses it to divide by.
537          Also convert allocno_live_length of zero to -1.  A length of zero
538          can occur when all the registers for that allocno have reg_live_length
539          equal to -2.  In this case, we want to make an allocno, but not
540          allocate it.  So avoid the divide-by-zero and set it to a low
541          priority.  */
542
543       for (i = 0; i < max_allocno; i++)
544         {
545           if (allocno_size[i] == 0)
546             allocno_size[i] = 1;
547           if (allocno_live_length[i] == 0)
548             allocno_live_length[i] = -1;
549         }
550
551       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
552       
553       prune_preferences ();
554
555       if (file)
556         dump_conflicts (file);
557
558       /* Try allocating them, one by one, in that order,
559          except for parameters marked with reg_live_length[regno] == -2.  */
560
561       for (i = 0; i < max_allocno; i++)
562         if (REG_LIVE_LENGTH (allocno_reg[allocno_order[i]]) >= 0)
563           {
564             /* If we have more than one register class,
565                first try allocating in the class that is cheapest
566                for this pseudo-reg.  If that fails, try any reg.  */
567             if (N_REG_CLASSES > 1)
568               {
569                 find_reg (allocno_order[i], 0, 0, 0, 0);
570                 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
571                   continue;
572               }
573             if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
574               find_reg (allocno_order[i], 0, 1, 0, 0);
575           }
576     }
577
578   /* Do the reloads now while the allocno data still exist, so that we can
579      try to assign new hard regs to any pseudo regs that are spilled.  */
580
581 #if 0 /* We need to eliminate regs even if there is no rtl code,
582          for the sake of debugging information.  */
583   if (n_basic_blocks > 0)
584 #endif
585     retval = reload (get_insns (), 1, file);
586
587   free (conflicts);
588   return retval;
589 }
590
591 /* Sort predicate for ordering the allocnos.
592    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
593
594 static int
595 allocno_compare (v1p, v2p)
596      const GENERIC_PTR v1p;
597      const GENERIC_PTR v2p;
598 {
599   int v1 = *(int *)v1p, v2 = *(int *)v2p;
600   /* Note that the quotient will never be bigger than
601      the value of floor_log2 times the maximum number of
602      times a register can occur in one insn (surely less than 100).
603      Multiplying this by 10000 can't overflow.  */
604   register int pri1
605     = (((double) (floor_log2 (allocno_n_refs[v1]) * allocno_n_refs[v1])
606         / allocno_live_length[v1])
607        * 10000 * allocno_size[v1]);
608   register int pri2
609     = (((double) (floor_log2 (allocno_n_refs[v2]) * allocno_n_refs[v2])
610         / allocno_live_length[v2])
611        * 10000 * allocno_size[v2]);
612   if (pri2 - pri1)
613     return pri2 - pri1;
614
615   /* If regs are equally good, sort by allocno,
616      so that the results of qsort leave nothing to chance.  */
617   return v1 - v2;
618 }
619 \f
620 /* Scan the rtl code and record all conflicts and register preferences in the
621    conflict matrices and preference tables.  */
622
623 static void
624 global_conflicts ()
625 {
626   register int b, i;
627   register rtx insn;
628   short *block_start_allocnos;
629
630   /* Make a vector that mark_reg_{store,clobber} will store in.  */
631   regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2);
632
633   block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
634
635   for (b = 0; b < n_basic_blocks; b++)
636     {
637       bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
638
639       /* Initialize table of registers currently live
640          to the state at the beginning of this basic block.
641          This also marks the conflicts among them.
642
643          For pseudo-regs, there is only one bit for each one
644          no matter how many hard regs it occupies.
645          This is ok; we know the size from PSEUDO_REGNO_SIZE.
646          For explicit hard regs, we cannot know the size that way
647          since one hard reg can be used with various sizes.
648          Therefore, we must require that all the hard regs
649          implicitly live as part of a multi-word hard reg
650          are explicitly marked in basic_block_live_at_start.  */
651
652       {
653         register regset old = basic_block_live_at_start[b];
654         int ax = 0;
655
656         REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
657         EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
658                                    {
659                                      register int a = reg_allocno[i];
660                                      if (a >= 0)
661                                        {
662                                          SET_ALLOCNO_LIVE (a);
663                                          block_start_allocnos[ax++] = a;
664                                        }
665                                      else if ((a = reg_renumber[i]) >= 0)
666                                        mark_reg_live_nc
667                                          (a, PSEUDO_REGNO_MODE (i));
668                                    });
669
670         /* Record that each allocno now live conflicts with each other
671            allocno now live, and with each hard reg now live.  */
672
673         record_conflicts (block_start_allocnos, ax);
674
675 #ifdef STACK_REGS
676         /* Pseudos can't go in stack regs at the start of a basic block
677            that can be reached through a computed goto, since reg-stack
678            can't handle computed gotos.  */
679         if (basic_block_computed_jump_target[b])
680           for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
681             record_one_conflict (ax);
682 #endif
683       }
684
685       insn = basic_block_head[b];
686
687       /* Scan the code of this basic block, noting which allocnos
688          and hard regs are born or die.  When one is born,
689          record a conflict with all others currently live.  */
690
691       while (1)
692         {
693           register RTX_CODE code = GET_CODE (insn);
694           register rtx link;
695
696           /* Make regs_set an empty set.  */
697
698           n_regs_set = 0;
699
700           if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
701             {
702
703 #if 0
704               int i = 0;
705               for (link = REG_NOTES (insn);
706                    link && i < NUM_NO_CONFLICT_PAIRS;
707                    link = XEXP (link, 1))
708                 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
709                   {
710                     no_conflict_pairs[i].allocno1
711                       = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
712                     no_conflict_pairs[i].allocno2
713                       = reg_allocno[REGNO (XEXP (link, 0))];
714                     i++;
715                   }
716 #endif /* 0 */
717
718               /* Mark any registers clobbered by INSN as live,
719                  so they conflict with the inputs.  */
720
721               note_stores (PATTERN (insn), mark_reg_clobber);
722
723               /* Mark any registers dead after INSN as dead now.  */
724
725               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
726                 if (REG_NOTE_KIND (link) == REG_DEAD)
727                   mark_reg_death (XEXP (link, 0));
728
729               /* Mark any registers set in INSN as live,
730                  and mark them as conflicting with all other live regs.
731                  Clobbers are processed again, so they conflict with
732                  the registers that are set.  */
733
734               note_stores (PATTERN (insn), mark_reg_store);
735
736 #ifdef AUTO_INC_DEC
737               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
738                 if (REG_NOTE_KIND (link) == REG_INC)
739                   mark_reg_store (XEXP (link, 0), NULL_RTX);
740 #endif
741
742               /* If INSN has multiple outputs, then any reg that dies here
743                  and is used inside of an output
744                  must conflict with the other outputs.  */
745
746               if (GET_CODE (PATTERN (insn)) == PARALLEL && !single_set (insn))
747                 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
748                   if (REG_NOTE_KIND (link) == REG_DEAD)
749                     {
750                       int used_in_output = 0;
751                       int i;
752                       rtx reg = XEXP (link, 0);
753
754                       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
755                         {
756                           rtx set = XVECEXP (PATTERN (insn), 0, i);
757                           if (GET_CODE (set) == SET
758                               && GET_CODE (SET_DEST (set)) != REG
759                               && !rtx_equal_p (reg, SET_DEST (set))
760                               && reg_overlap_mentioned_p (reg, SET_DEST (set)))
761                             used_in_output = 1;
762                         }
763                       if (used_in_output)
764                         mark_reg_conflicts (reg);
765                     }
766
767               /* Mark any registers set in INSN and then never used.  */
768
769               while (n_regs_set > 0)
770                 if (find_regno_note (insn, REG_UNUSED,
771                                      REGNO (regs_set[--n_regs_set])))
772                   mark_reg_death (regs_set[n_regs_set]);
773             }
774
775           if (insn == basic_block_end[b])
776             break;
777           insn = NEXT_INSN (insn);
778         }
779     }
780 }
781 /* Expand the preference information by looking for cases where one allocno
782    dies in an insn that sets an allocno.  If those two allocnos don't conflict,
783    merge any preferences between those allocnos.  */
784
785 static void
786 expand_preferences ()
787 {
788   rtx insn;
789   rtx link;
790   rtx set;
791
792   /* We only try to handle the most common cases here.  Most of the cases
793      where this wins are reg-reg copies.  */
794
795   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
796     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
797         && (set = single_set (insn)) != 0
798         && GET_CODE (SET_DEST (set)) == REG
799         && reg_allocno[REGNO (SET_DEST (set))] >= 0)
800       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
801         if (REG_NOTE_KIND (link) == REG_DEAD
802             && GET_CODE (XEXP (link, 0)) == REG
803             && reg_allocno[REGNO (XEXP (link, 0))] >= 0
804             && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
805                             reg_allocno[REGNO (XEXP (link, 0))])
806             && ! CONFLICTP (reg_allocno[REGNO (XEXP (link, 0))],
807                             reg_allocno[REGNO (SET_DEST (set))]))
808           {
809             int a1 = reg_allocno[REGNO (SET_DEST (set))];
810             int a2 = reg_allocno[REGNO (XEXP (link, 0))];
811
812             if (XEXP (link, 0) == SET_SRC (set))
813               {
814                 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
815                                   hard_reg_copy_preferences[a2]);
816                 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
817                                   hard_reg_copy_preferences[a1]);
818               }
819
820             IOR_HARD_REG_SET (hard_reg_preferences[a1],
821                               hard_reg_preferences[a2]);
822             IOR_HARD_REG_SET (hard_reg_preferences[a2],
823                               hard_reg_preferences[a1]);
824             IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
825                               hard_reg_full_preferences[a2]);
826             IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
827                               hard_reg_full_preferences[a1]);
828           }
829 }
830 \f
831 /* Prune the preferences for global registers to exclude registers that cannot
832    be used.
833    
834    Compute `regs_someone_prefers', which is a bitmask of the hard registers
835    that are preferred by conflicting registers of lower priority.  If possible,
836    we will avoid using these registers.  */
837    
838 static void
839 prune_preferences ()
840 {
841   int i, j;
842   int allocno;
843   
844   /* Scan least most important to most important.
845      For each allocno, remove from preferences registers that cannot be used,
846      either because of conflicts or register type.  Then compute all registers
847      preferred by each lower-priority register that conflicts.  */
848
849   for (i = max_allocno - 1; i >= 0; i--)
850     {
851       HARD_REG_SET temp;
852
853       allocno = allocno_order[i];
854       COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
855
856       if (allocno_calls_crossed[allocno] == 0)
857         IOR_HARD_REG_SET (temp, fixed_reg_set);
858       else
859         IOR_HARD_REG_SET (temp, call_used_reg_set);
860
861       IOR_COMPL_HARD_REG_SET
862         (temp,
863          reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
864
865       AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
866       AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
867       AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
868
869       CLEAR_HARD_REG_SET (regs_someone_prefers[allocno]);
870
871       /* Merge in the preferences of lower-priority registers (they have
872          already been pruned).  If we also prefer some of those registers,
873          don't exclude them unless we are of a smaller size (in which case
874          we want to give the lower-priority allocno the first chance for
875          these registers).  */
876       for (j = i + 1; j < max_allocno; j++)
877         if (CONFLICTP (allocno, allocno_order[j])
878             || CONFLICTP (allocno_order[j], allocno))
879           {
880             COPY_HARD_REG_SET (temp,
881                                hard_reg_full_preferences[allocno_order[j]]);
882             if (allocno_size[allocno_order[j]] <= allocno_size[allocno])
883               AND_COMPL_HARD_REG_SET (temp,
884                                       hard_reg_full_preferences[allocno]);
885                                
886             IOR_HARD_REG_SET (regs_someone_prefers[allocno], temp);
887           }
888     }
889 }
890 \f
891 /* Assign a hard register to ALLOCNO; look for one that is the beginning
892    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
893    The registers marked in PREFREGS are tried first.
894
895    LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
896    be used for this allocation.
897
898    If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
899    Otherwise ignore that preferred class and use the alternate class.
900
901    If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
902    will have to be saved and restored at calls.
903
904    RETRYING is nonzero if this is called from retry_global_alloc.
905
906    If we find one, record it in reg_renumber.
907    If not, do nothing.  */
908
909 static void
910 find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
911      int allocno;
912      HARD_REG_SET losers;
913      int alt_regs_p;
914      int accept_call_clobbered;
915      int retrying;
916 {
917   register int i, best_reg, pass;
918 #ifdef HARD_REG_SET
919   register              /* Declare it register if it's a scalar.  */
920 #endif
921     HARD_REG_SET used, used1, used2;
922
923   enum reg_class class = (alt_regs_p
924                           ? reg_alternate_class (allocno_reg[allocno])
925                           : reg_preferred_class (allocno_reg[allocno]));
926   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
927
928   if (accept_call_clobbered)
929     COPY_HARD_REG_SET (used1, call_fixed_reg_set);
930   else if (allocno_calls_crossed[allocno] == 0)
931     COPY_HARD_REG_SET (used1, fixed_reg_set);
932   else
933     COPY_HARD_REG_SET (used1, call_used_reg_set);
934
935   /* Some registers should not be allocated in global-alloc.  */
936   IOR_HARD_REG_SET (used1, no_global_alloc_regs);
937   if (losers)
938     IOR_HARD_REG_SET (used1, losers);
939
940   IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
941   COPY_HARD_REG_SET (used2, used1);
942
943   IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
944
945 #ifdef CLASS_CANNOT_CHANGE_SIZE
946   if (REG_CHANGES_SIZE (allocno_reg[allocno]))
947     IOR_HARD_REG_SET (used1,
948                       reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
949 #endif
950
951   /* Try each hard reg to see if it fits.  Do this in two passes.
952      In the first pass, skip registers that are preferred by some other pseudo
953      to give it a better chance of getting one of those registers.  Only if
954      we can't get a register when excluding those do we take one of them.
955      However, we never allocate a register for the first time in pass 0.  */
956
957   COPY_HARD_REG_SET (used, used1);
958   IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
959   IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
960   
961   best_reg = -1;
962   for (i = FIRST_PSEUDO_REGISTER, pass = 0;
963        pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
964        pass++)
965     {
966       if (pass == 1)
967         COPY_HARD_REG_SET (used, used1);
968       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
969         {
970 #ifdef REG_ALLOC_ORDER
971           int regno = reg_alloc_order[i];
972 #else
973           int regno = i;
974 #endif
975           if (! TEST_HARD_REG_BIT (used, regno)
976               && HARD_REGNO_MODE_OK (regno, mode))
977             {
978               register int j;
979               register int lim = regno + HARD_REGNO_NREGS (regno, mode);
980               for (j = regno + 1;
981                    (j < lim
982                     && ! TEST_HARD_REG_BIT (used, j));
983                    j++);
984               if (j == lim)
985                 {
986                   best_reg = regno;
987                   break;
988                 }
989 #ifndef REG_ALLOC_ORDER
990               i = j;                    /* Skip starting points we know will lose */
991 #endif
992             }
993           }
994       }
995
996   /* See if there is a preferred register with the same class as the register
997      we allocated above.  Making this restriction prevents register
998      preferencing from creating worse register allocation.
999
1000      Remove from the preferred registers and conflicting registers.  Note that
1001      additional conflicts may have been added after `prune_preferences' was
1002      called. 
1003
1004      First do this for those register with copy preferences, then all
1005      preferred registers.  */
1006
1007   AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
1008   GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
1009                          reg_class_contents[(int) NO_REGS], no_copy_prefs);
1010
1011   if (best_reg >= 0)
1012     {
1013       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1014         if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
1015             && HARD_REGNO_MODE_OK (i, mode)
1016             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1017                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1018                                        REGNO_REG_CLASS (best_reg))
1019                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1020                                        REGNO_REG_CLASS (i))))
1021             {
1022               register int j;
1023               register int lim = i + HARD_REGNO_NREGS (i, mode);
1024               for (j = i + 1;
1025                    (j < lim
1026                     && ! TEST_HARD_REG_BIT (used, j)
1027                     && (REGNO_REG_CLASS (j)
1028                         == REGNO_REG_CLASS (best_reg + (j - i))
1029                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1030                                                REGNO_REG_CLASS (best_reg + (j - i)))
1031                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1032                                                REGNO_REG_CLASS (j))));
1033                    j++);
1034               if (j == lim)
1035                 {
1036                   best_reg = i;
1037                   goto no_prefs;
1038                 }
1039             }
1040     }
1041  no_copy_prefs:
1042
1043   AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1044   GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1045                          reg_class_contents[(int) NO_REGS], no_prefs);
1046
1047   if (best_reg >= 0)
1048     {
1049       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1050         if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1051             && HARD_REGNO_MODE_OK (i, mode)
1052             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1053                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1054                                        REGNO_REG_CLASS (best_reg))
1055                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1056                                        REGNO_REG_CLASS (i))))
1057             {
1058               register int j;
1059               register int lim = i + HARD_REGNO_NREGS (i, mode);
1060               for (j = i + 1;
1061                    (j < lim
1062                     && ! TEST_HARD_REG_BIT (used, j)
1063                     && (REGNO_REG_CLASS (j)
1064                         == REGNO_REG_CLASS (best_reg + (j - i))
1065                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1066                                                REGNO_REG_CLASS (best_reg + (j - i)))
1067                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1068                                                REGNO_REG_CLASS (j))));
1069                    j++);
1070               if (j == lim)
1071                 {
1072                   best_reg = i;
1073                   break;
1074                 }
1075             }
1076     }
1077  no_prefs:
1078
1079   /* If we haven't succeeded yet, try with caller-saves. 
1080      We need not check to see if the current function has nonlocal
1081      labels because we don't put any pseudos that are live over calls in
1082      registers in that case.  */
1083
1084   if (flag_caller_saves && best_reg < 0)
1085     {
1086       /* Did not find a register.  If it would be profitable to
1087          allocate a call-clobbered register and save and restore it
1088          around calls, do that.  */
1089       if (! accept_call_clobbered
1090           && allocno_calls_crossed[allocno] != 0
1091           && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1092                                      allocno_calls_crossed[allocno]))
1093         {
1094           HARD_REG_SET new_losers;
1095           if (! losers)
1096             CLEAR_HARD_REG_SET (new_losers);
1097           else
1098             COPY_HARD_REG_SET (new_losers, losers);
1099             
1100           IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1101           find_reg (allocno, new_losers, alt_regs_p, 1, retrying);
1102           if (reg_renumber[allocno_reg[allocno]] >= 0)
1103             {
1104               caller_save_needed = 1;
1105               return;
1106             }
1107         }
1108     }
1109
1110   /* If we haven't succeeded yet,
1111      see if some hard reg that conflicts with us
1112      was utilized poorly by local-alloc.
1113      If so, kick out the regs that were put there by local-alloc
1114      so we can use it instead.  */
1115   if (best_reg < 0 && !retrying
1116       /* Let's not bother with multi-reg allocnos.  */
1117       && allocno_size[allocno] == 1)
1118     {
1119       /* Count from the end, to find the least-used ones first.  */
1120       for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1121         {
1122 #ifdef REG_ALLOC_ORDER
1123           int regno = reg_alloc_order[i];
1124 #else
1125           int regno = i;
1126 #endif
1127
1128           if (local_reg_n_refs[regno] != 0
1129               /* Don't use a reg no good for this pseudo.  */
1130               && ! TEST_HARD_REG_BIT (used2, regno)
1131               && HARD_REGNO_MODE_OK (regno, mode)
1132 #ifdef CLASS_CANNOT_CHANGE_SIZE
1133               && ! (REG_CHANGES_SIZE (allocno_reg[allocno])
1134                     && (TEST_HARD_REG_BIT
1135                         (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1136                          regno)))
1137 #endif
1138               )
1139             {
1140               /* We explicitly evaluate the divide results into temporary
1141                  variables so as to avoid excess precision problems that occur
1142                  on a i386-unknown-sysv4.2 (unixware) host.  */
1143                  
1144               double tmp1 = ((double) local_reg_n_refs[regno]
1145                             / local_reg_live_length[regno]);
1146               double tmp2 = ((double) allocno_n_refs[allocno]
1147                              / allocno_live_length[allocno]);
1148
1149               if (tmp1 < tmp2)
1150                 {
1151                   /* Hard reg REGNO was used less in total by local regs
1152                      than it would be used by this one allocno!  */
1153                   int k;
1154                   for (k = 0; k < max_regno; k++)
1155                     if (reg_renumber[k] >= 0)
1156                       {
1157                         int r = reg_renumber[k];
1158                         int endregno
1159                           = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1160
1161                         if (regno >= r && regno < endregno)
1162                           reg_renumber[k] = -1;
1163                       }
1164
1165                   best_reg = regno;
1166                   break;
1167                 }
1168             }
1169         }
1170     }
1171
1172   /* Did we find a register?  */
1173
1174   if (best_reg >= 0)
1175     {
1176       register int lim, j;
1177       HARD_REG_SET this_reg;
1178
1179       /* Yes.  Record it as the hard register of this pseudo-reg.  */
1180       reg_renumber[allocno_reg[allocno]] = best_reg;
1181       /* Also of any pseudo-regs that share with it.  */
1182       if (reg_may_share[allocno_reg[allocno]])
1183         for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1184           if (reg_allocno[j] == allocno)
1185             reg_renumber[j] = best_reg;
1186
1187       /* Make a set of the hard regs being allocated.  */
1188       CLEAR_HARD_REG_SET (this_reg);
1189       lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1190       for (j = best_reg; j < lim; j++)
1191         {
1192           SET_HARD_REG_BIT (this_reg, j);
1193           SET_HARD_REG_BIT (regs_used_so_far, j);
1194           /* This is no longer a reg used just by local regs.  */
1195           local_reg_n_refs[j] = 0;
1196         }
1197       /* For each other pseudo-reg conflicting with this one,
1198          mark it as conflicting with the hard regs this one occupies.  */
1199       lim = allocno;
1200       for (j = 0; j < max_allocno; j++)
1201         if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
1202           {
1203             IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1204           }
1205     }
1206 }
1207 \f
1208 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1209    Perhaps it had previously seemed not worth a hard reg,
1210    or perhaps its old hard reg has been commandeered for reloads.
1211    FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1212    they do not appear to be allocated.
1213    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
1214
1215 void
1216 retry_global_alloc (regno, forbidden_regs)
1217      int regno;
1218      HARD_REG_SET forbidden_regs;
1219 {
1220   int allocno = reg_allocno[regno];
1221   if (allocno >= 0)
1222     {
1223       /* If we have more than one register class,
1224          first try allocating in the class that is cheapest
1225          for this pseudo-reg.  If that fails, try any reg.  */
1226       if (N_REG_CLASSES > 1)
1227         find_reg (allocno, forbidden_regs, 0, 0, 1);
1228       if (reg_renumber[regno] < 0
1229           && reg_alternate_class (regno) != NO_REGS)
1230         find_reg (allocno, forbidden_regs, 1, 0, 1);
1231
1232       /* If we found a register, modify the RTL for the register to
1233          show the hard register, and mark that register live.  */
1234       if (reg_renumber[regno] >= 0)
1235         {
1236           REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1237           mark_home_live (regno);
1238         }
1239     }
1240 }
1241 \f
1242 /* Record a conflict between register REGNO
1243    and everything currently live.
1244    REGNO must not be a pseudo reg that was allocated
1245    by local_alloc; such numbers must be translated through
1246    reg_renumber before calling here.  */
1247
1248 static void
1249 record_one_conflict (regno)
1250      int regno;
1251 {
1252   register int j;
1253
1254   if (regno < FIRST_PSEUDO_REGISTER)
1255     /* When a hard register becomes live,
1256        record conflicts with live pseudo regs.  */
1257     for (j = 0; j < max_allocno; j++)
1258       {
1259         if (ALLOCNO_LIVE_P (j))
1260           SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1261       }
1262   else
1263     /* When a pseudo-register becomes live,
1264        record conflicts first with hard regs,
1265        then with other pseudo regs.  */
1266     {
1267       register int ialloc = reg_allocno[regno];
1268       register int ialloc_prod = ialloc * allocno_row_words;
1269       IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1270       for (j = allocno_row_words - 1; j >= 0; j--)
1271         {
1272 #if 0
1273           int k;
1274           for (k = 0; k < n_no_conflict_pairs; k++)
1275             if (! ((j == no_conflict_pairs[k].allocno1
1276                     && ialloc == no_conflict_pairs[k].allocno2)
1277                    ||
1278                    (j == no_conflict_pairs[k].allocno2
1279                     && ialloc == no_conflict_pairs[k].allocno1)))
1280 #endif /* 0 */
1281               conflicts[ialloc_prod + j] |= allocnos_live[j];
1282         }
1283     }
1284 }
1285
1286 /* Record all allocnos currently live as conflicting
1287    with each other and with all hard regs currently live.
1288    ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1289    are currently live.  Their bits are also flagged in allocnos_live.  */
1290
1291 static void
1292 record_conflicts (allocno_vec, len)
1293      register short *allocno_vec;
1294      register int len;
1295 {
1296   register int allocno;
1297   register int j;
1298   register int ialloc_prod;
1299
1300   while (--len >= 0)
1301     {
1302       allocno = allocno_vec[len];
1303       ialloc_prod = allocno * allocno_row_words;
1304       IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1305       for (j = allocno_row_words - 1; j >= 0; j--)
1306         conflicts[ialloc_prod + j] |= allocnos_live[j];
1307     }
1308 }
1309 \f
1310 /* Handle the case where REG is set by the insn being scanned,
1311    during the forward scan to accumulate conflicts.
1312    Store a 1 in regs_live or allocnos_live for this register, record how many
1313    consecutive hardware registers it actually needs,
1314    and record a conflict with all other registers already live.
1315
1316    Note that even if REG does not remain alive after this insn,
1317    we must mark it here as live, to ensure a conflict between
1318    REG and any other regs set in this insn that really do live.
1319    This is because those other regs could be considered after this.
1320
1321    REG might actually be something other than a register;
1322    if so, we do nothing.
1323
1324    SETTER is 0 if this register was modified by an auto-increment (i.e.,
1325    a REG_INC note was found for it).
1326
1327    CLOBBERs are processed here by calling mark_reg_clobber.  */ 
1328
1329 static void
1330 mark_reg_store (orig_reg, setter)
1331      rtx orig_reg, setter;
1332 {
1333   register int regno;
1334   register rtx reg = orig_reg;
1335
1336   /* WORD is which word of a multi-register group is being stored.
1337      For the case where the store is actually into a SUBREG of REG.
1338      Except we don't use it; I believe the entire REG needs to be
1339      made live.  */
1340   int word = 0;
1341
1342   if (GET_CODE (reg) == SUBREG)
1343     {
1344       word = SUBREG_WORD (reg);
1345       reg = SUBREG_REG (reg);
1346     }
1347
1348   if (GET_CODE (reg) != REG)
1349     return;
1350
1351   if (setter && GET_CODE (setter) == CLOBBER)
1352     {
1353       /* A clobber of a register should be processed here too.  */
1354       mark_reg_clobber (orig_reg, setter);
1355       return;
1356     }
1357
1358   regs_set[n_regs_set++] = reg;
1359
1360   if (setter)
1361     set_preference (reg, SET_SRC (setter));
1362
1363   regno = REGNO (reg);
1364
1365   if (reg_renumber[regno] >= 0)
1366     regno = reg_renumber[regno] /* + word */;
1367
1368   /* Either this is one of the max_allocno pseudo regs not allocated,
1369      or it is or has a hardware reg.  First handle the pseudo-regs.  */
1370   if (regno >= FIRST_PSEUDO_REGISTER)
1371     {
1372       if (reg_allocno[regno] >= 0)
1373         {
1374           SET_ALLOCNO_LIVE (reg_allocno[regno]);
1375           record_one_conflict (regno);
1376         }
1377     }
1378   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1379   else if (! fixed_regs[regno])
1380     {
1381       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1382       while (regno < last)
1383         {
1384           record_one_conflict (regno);
1385           SET_HARD_REG_BIT (hard_regs_live, regno);
1386           regno++;
1387         }
1388     }
1389 }
1390 \f
1391 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs.  */
1392
1393 static void
1394 mark_reg_clobber (reg, setter)
1395      rtx reg, setter;
1396 {
1397   register int regno;
1398
1399   /* WORD is which word of a multi-register group is being stored.
1400      For the case where the store is actually into a SUBREG of REG.
1401      Except we don't use it; I believe the entire REG needs to be
1402      made live.  */
1403   int word = 0;
1404
1405   if (GET_CODE (setter) != CLOBBER)
1406     return;
1407
1408   if (GET_CODE (reg) == SUBREG)
1409     {
1410       word = SUBREG_WORD (reg);
1411       reg = SUBREG_REG (reg);
1412     }
1413
1414   if (GET_CODE (reg) != REG)
1415     return;
1416
1417   regs_set[n_regs_set++] = reg;
1418
1419   regno = REGNO (reg);
1420
1421   if (reg_renumber[regno] >= 0)
1422     regno = reg_renumber[regno] /* + word */;
1423
1424   /* Either this is one of the max_allocno pseudo regs not allocated,
1425      or it is or has a hardware reg.  First handle the pseudo-regs.  */
1426   if (regno >= FIRST_PSEUDO_REGISTER)
1427     {
1428       if (reg_allocno[regno] >= 0)
1429         {
1430           SET_ALLOCNO_LIVE (reg_allocno[regno]);
1431           record_one_conflict (regno);
1432         }
1433     }
1434   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1435   else if (! fixed_regs[regno])
1436     {
1437       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1438       while (regno < last)
1439         {
1440           record_one_conflict (regno);
1441           SET_HARD_REG_BIT (hard_regs_live, regno);
1442           regno++;
1443         }
1444     }
1445 }
1446
1447 /* Record that REG has conflicts with all the regs currently live.
1448    Do not mark REG itself as live.  */
1449
1450 static void
1451 mark_reg_conflicts (reg)
1452      rtx reg;
1453 {
1454   register int regno;
1455
1456   if (GET_CODE (reg) == SUBREG)
1457     reg = SUBREG_REG (reg);
1458
1459   if (GET_CODE (reg) != REG)
1460     return;
1461
1462   regno = REGNO (reg);
1463
1464   if (reg_renumber[regno] >= 0)
1465     regno = reg_renumber[regno];
1466
1467   /* Either this is one of the max_allocno pseudo regs not allocated,
1468      or it is or has a hardware reg.  First handle the pseudo-regs.  */
1469   if (regno >= FIRST_PSEUDO_REGISTER)
1470     {
1471       if (reg_allocno[regno] >= 0)
1472         record_one_conflict (regno);
1473     }
1474   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1475   else if (! fixed_regs[regno])
1476     {
1477       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1478       while (regno < last)
1479         {
1480           record_one_conflict (regno);
1481           regno++;
1482         }
1483     }
1484 }
1485 \f
1486 /* Mark REG as being dead (following the insn being scanned now).
1487    Store a 0 in regs_live or allocnos_live for this register.  */
1488
1489 static void
1490 mark_reg_death (reg)
1491      rtx reg;
1492 {
1493   register int regno = REGNO (reg);
1494
1495   /* For pseudo reg, see if it has been assigned a hardware reg.  */
1496   if (reg_renumber[regno] >= 0)
1497     regno = reg_renumber[regno];
1498
1499   /* Either this is one of the max_allocno pseudo regs not allocated,
1500      or it is a hardware reg.  First handle the pseudo-regs.  */
1501   if (regno >= FIRST_PSEUDO_REGISTER)
1502     {
1503       if (reg_allocno[regno] >= 0)
1504         CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1505     }
1506   /* Handle hardware regs (and pseudos allocated to hard regs).  */
1507   else if (! fixed_regs[regno])
1508     {
1509       /* Pseudo regs already assigned hardware regs are treated
1510          almost the same as explicit hardware regs.  */
1511       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1512       while (regno < last)
1513         {
1514           CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1515           regno++;
1516         }
1517     }
1518 }
1519
1520 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1521    for the value stored in it.  MODE determines how many consecutive
1522    registers are actually in use.  Do not record conflicts;
1523    it is assumed that the caller will do that.  */
1524
1525 static void
1526 mark_reg_live_nc (regno, mode)
1527      register int regno;
1528      enum machine_mode mode;
1529 {
1530   register int last = regno + HARD_REGNO_NREGS (regno, mode);
1531   while (regno < last)
1532     {
1533       SET_HARD_REG_BIT (hard_regs_live, regno);
1534       regno++;
1535     }
1536 }
1537 \f
1538 /* Try to set a preference for an allocno to a hard register.
1539    We are passed DEST and SRC which are the operands of a SET.  It is known
1540    that SRC is a register.  If SRC or the first operand of SRC is a register,
1541    try to set a preference.  If one of the two is a hard register and the other
1542    is a pseudo-register, mark the preference.
1543    
1544    Note that we are not as aggressive as local-alloc in trying to tie a
1545    pseudo-register to a hard register.  */
1546
1547 static void
1548 set_preference (dest, src)
1549      rtx dest, src;
1550 {
1551   int src_regno, dest_regno;
1552   /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1553      to compensate for subregs in SRC or DEST.  */
1554   int offset = 0;
1555   int i;
1556   int copy = 1;
1557
1558   if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1559     src = XEXP (src, 0), copy = 0;
1560
1561   /* Get the reg number for both SRC and DEST.
1562      If neither is a reg, give up.  */
1563
1564   if (GET_CODE (src) == REG)
1565     src_regno = REGNO (src);
1566   else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1567     {
1568       src_regno = REGNO (SUBREG_REG (src));
1569       offset += SUBREG_WORD (src);
1570     }
1571   else
1572     return;
1573
1574   if (GET_CODE (dest) == REG)
1575     dest_regno = REGNO (dest);
1576   else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1577     {
1578       dest_regno = REGNO (SUBREG_REG (dest));
1579       offset -= SUBREG_WORD (dest);
1580     }
1581   else
1582     return;
1583
1584   /* Convert either or both to hard reg numbers.  */
1585
1586   if (reg_renumber[src_regno] >= 0)
1587     src_regno = reg_renumber[src_regno];
1588
1589   if (reg_renumber[dest_regno] >= 0)
1590     dest_regno = reg_renumber[dest_regno];
1591
1592   /* Now if one is a hard reg and the other is a global pseudo
1593      then give the other a preference.  */
1594
1595   if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1596       && reg_allocno[src_regno] >= 0)
1597     {
1598       dest_regno -= offset;
1599       if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1600         {
1601           if (copy)
1602             SET_REGBIT (hard_reg_copy_preferences,
1603                         reg_allocno[src_regno], dest_regno);
1604
1605           SET_REGBIT (hard_reg_preferences,
1606                       reg_allocno[src_regno], dest_regno);
1607           for (i = dest_regno;
1608                i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1609                i++)
1610             SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1611         }
1612     }
1613
1614   if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1615       && reg_allocno[dest_regno] >= 0)
1616     {
1617       src_regno += offset;
1618       if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1619         {
1620           if (copy)
1621             SET_REGBIT (hard_reg_copy_preferences,
1622                         reg_allocno[dest_regno], src_regno);
1623
1624           SET_REGBIT (hard_reg_preferences,
1625                       reg_allocno[dest_regno], src_regno);
1626           for (i = src_regno;
1627                i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1628                i++)
1629             SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1630         }
1631     }
1632 }
1633 \f
1634 /* Indicate that hard register number FROM was eliminated and replaced with
1635    an offset from hard register number TO.  The status of hard registers live
1636    at the start of a basic block is updated by replacing a use of FROM with
1637    a use of TO.  */
1638
1639 void
1640 mark_elimination (from, to)
1641      int from, to;
1642 {
1643   int i;
1644
1645   for (i = 0; i < n_basic_blocks; i++)
1646     if (REGNO_REG_SET_P (basic_block_live_at_start[i], from))
1647       {
1648         CLEAR_REGNO_REG_SET (basic_block_live_at_start[i], from);
1649         SET_REGNO_REG_SET (basic_block_live_at_start[i], to);
1650       }
1651 }
1652 \f
1653 /* Print debugging trace information if -greg switch is given,
1654    showing the information on which the allocation decisions are based.  */
1655
1656 static void
1657 dump_conflicts (file)
1658      FILE *file;
1659 {
1660   register int i;
1661   register int has_preferences;
1662   fprintf (file, ";; %d regs to allocate:", max_allocno);
1663   for (i = 0; i < max_allocno; i++)
1664     {
1665       int j;
1666       fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1667       for (j = 0; j < max_regno; j++)
1668         if (reg_allocno[j] == allocno_order[i]
1669             && j != allocno_reg[allocno_order[i]])
1670           fprintf (file, "+%d", j);
1671       if (allocno_size[allocno_order[i]] != 1)
1672         fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1673     }
1674   fprintf (file, "\n");
1675
1676   for (i = 0; i < max_allocno; i++)
1677     {
1678       register int j;
1679       fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1680       for (j = 0; j < max_allocno; j++)
1681         if (CONFLICTP (i, j) || CONFLICTP (j, i))
1682           fprintf (file, " %d", allocno_reg[j]);
1683       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1684         if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1685           fprintf (file, " %d", j);
1686       fprintf (file, "\n");
1687
1688       has_preferences = 0;
1689       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1690         if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1691           has_preferences = 1;
1692
1693       if (! has_preferences)
1694         continue;
1695       fprintf (file, ";; %d preferences:", allocno_reg[i]);
1696       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1697         if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1698           fprintf (file, " %d", j);
1699       fprintf (file, "\n");
1700     }
1701   fprintf (file, "\n");
1702 }
1703
1704 void
1705 dump_global_regs (file)
1706      FILE *file;
1707 {
1708   register int i, j;
1709   
1710   fprintf (file, ";; Register dispositions:\n");
1711   for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1712     if (reg_renumber[i] >= 0)
1713       {
1714         fprintf (file, "%d in %d  ", i, reg_renumber[i]);
1715         if (++j % 6 == 0)
1716           fprintf (file, "\n");
1717       }
1718
1719   fprintf (file, "\n\n;; Hard regs used: ");
1720   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1721     if (regs_ever_live[i])
1722       fprintf (file, " %d", i);
1723   fprintf (file, "\n\n");
1724 }