OSDN Git Service

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