OSDN Git Service

2008-06-07 Xinliang David Li <davidxl@google.com>
[pf3gnuchains/gcc-fork.git] / gcc / global.c
1 /* Allocate registers for pseudo-registers that span basic blocks.
2    Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
3    1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "tree-pass.h"
40 #include "timevar.h"
41 #include "df.h"
42 #include "vecprim.h"
43 #include "dbgcnt.h"
44 #include "ra.h"
45
46 /* This pass of the compiler performs global register allocation.
47    It assigns hard register numbers to all the pseudo registers
48    that were not handled in local_alloc.  Assignments are recorded
49    in the vector reg_renumber, not by changing the rtl code.
50    (Such changes are made by final).  The entry point is
51    the function global_alloc.
52
53    After allocation is complete, the reload pass is run as a subroutine
54    of this pass, so that when a pseudo reg loses its hard reg due to
55    spilling it is possible to make a second attempt to find a hard
56    reg for it.  The reload pass is independent in other respects
57    and it is run even when stupid register allocation is in use.
58
59    1. Assign allocation-numbers (allocnos) to the pseudo-registers
60    still needing allocations and to the pseudo-registers currently
61    allocated by local-alloc which may be spilled by reload.
62    Set up tables reg_allocno and allocno_reg to map
63    reg numbers to allocnos and vice versa.
64    max_allocno gets the number of allocnos in use.
65
66    2. Allocate a max_allocno by max_allocno compressed triangular conflict
67    bit matrix (a triangular bit matrix with portions removed for which we
68    can guarantee there are no conflicts, example: two local pseudos that
69    live in different basic blocks) and clear it.  This is called "conflict".
70    Note that for triangular bit matrices, there are two possible equations
71    for computing the bit number for two allocnos: LOW and HIGH (LOW < HIGH):
72
73      1) BITNUM = f(HIGH) + LOW, where
74        f(HIGH) = (HIGH * (HIGH - 1)) / 2
75
76      2) BITNUM = f(LOW) + HIGH, where
77        f(LOW) = LOW * (max_allocno - LOW) + (LOW * (LOW - 1)) / 2 - LOW - 1
78
79    We use the second (and less common) equation as this gives us better
80    cache locality for local allocnos that are live within the same basic
81    block.  Also note that f(HIGH) and f(LOW) can be precalculated for all
82    values of HIGH and LOW, so all that is necessary to compute the bit
83    number for two allocnos LOW and HIGH is a load followed by an addition.
84
85    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix for
86    conflicts between allocnos and explicit hard register use (which
87    includes use of pseudo-registers allocated by local_alloc).  This
88    is the hard_reg_conflicts inside each allocno.
89
90    3. For each basic block, walk backward through the block, recording
91    which pseudo-registers and which hardware registers are live.
92    Build the conflict matrix between the pseudo-registers and another of
93    pseudo-registers versus hardware registers.
94
95    4. For each basic block, walk backward through the block, recording
96    the preferred hardware registers for each pseudo-register.
97
98    5. Sort a table of the allocnos into order of desirability of the variables.
99
100    6. Allocate the variables in that order; each if possible into
101    a preferred register, else into another register.  */
102 \f
103 /* A vector of the integers from 0 to max_allocno-1,
104    sorted in the order of first-to-be-allocated first.  */
105
106 static int *allocno_order;
107
108 /* Set of registers that global-alloc isn't supposed to use.  */
109
110 static HARD_REG_SET no_global_alloc_regs;
111
112 /* Set of registers used so far.  */
113
114 static HARD_REG_SET regs_used_so_far;
115
116 /* Number of refs to each hard reg, as used by local alloc.
117    It is zero for a reg that contains global pseudos or is explicitly used.  */
118
119 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
120
121 /* Frequency of uses of given hard reg.  */
122 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
123
124 /* Guess at live length of each hard reg, as used by local alloc.
125    This is actually the sum of the live lengths of the specific regs.  */
126
127 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
128
129 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
130    element I, and hard register number J.  */
131
132 #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (allocno[I].TABLE, J)
133
134 /* Return true if *LOC contains an asm.  */
135
136 static int
137 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
138 {
139   if ( !*loc)
140     return 0;
141   if (GET_CODE (*loc) == ASM_OPERANDS)
142     return 1;
143   return 0;
144 }
145
146
147 /* Return true if INSN contains an ASM.  */
148
149 static int
150 insn_contains_asm (rtx insn)
151 {
152   return for_each_rtx (&insn, insn_contains_asm_1, NULL);
153 }
154
155
156 static void
157 compute_regs_asm_clobbered (char *regs_asm_clobbered)
158 {
159   basic_block bb;
160
161   memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
162   
163   FOR_EACH_BB (bb)
164     {
165       rtx insn;
166       FOR_BB_INSNS_REVERSE (bb, insn)
167         {
168           struct df_ref **def_rec;
169           if (insn_contains_asm (insn))
170             for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
171               {
172                 struct df_ref *def = *def_rec;
173                 unsigned int dregno = DF_REF_REGNO (def);
174                 if (dregno < FIRST_PSEUDO_REGISTER)
175                   {
176                     unsigned int i;
177                     enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
178                     unsigned int end = dregno 
179                       + hard_regno_nregs[dregno][mode] - 1;
180                     for (i = dregno; i <= end; ++i)
181                       regs_asm_clobbered[i] = 1;
182                   }
183               }
184         }
185     }
186 }
187
188
189 /* All registers that can be eliminated.  */
190
191 static HARD_REG_SET eliminable_regset;
192
193 static int regno_compare (const void *, const void *);
194 static int allocno_compare (const void *, const void *);
195 static void expand_preferences (void);
196 static void prune_preferences (void);
197 static void set_preferences (void);
198 static void find_reg (int, HARD_REG_SET, int, int, int);
199 static void dump_conflicts (FILE *);
200 static void build_insn_chain (void);
201 \f
202
203 /* Look through the list of eliminable registers.  Set ELIM_SET to the
204    set of registers which may be eliminated.  Set NO_GLOBAL_SET to the
205    set of registers which may not be used across blocks.
206
207    This will normally be called with ELIM_SET as the file static
208    variable eliminable_regset, and NO_GLOBAL_SET as the file static
209    variable NO_GLOBAL_ALLOC_REGS.  */
210
211 static void
212 compute_regsets (HARD_REG_SET *elim_set, 
213                  HARD_REG_SET *no_global_set)
214 {
215
216 /* Like regs_ever_live, but 1 if a reg is set or clobbered from an asm.
217    Unlike regs_ever_live, elements of this array corresponding to
218    eliminable regs like the frame pointer are set if an asm sets them.  */
219   char *regs_asm_clobbered = alloca (FIRST_PSEUDO_REGISTER * sizeof (char));
220
221 #ifdef ELIMINABLE_REGS
222   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
223   size_t i;
224 #endif
225   int need_fp
226     = (! flag_omit_frame_pointer
227        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
228        || FRAME_POINTER_REQUIRED);
229
230   max_regno = max_reg_num ();
231   compact_blocks ();
232
233   max_allocno = 0;
234
235   /* A machine may have certain hard registers that
236      are safe to use only within a basic block.  */
237
238   CLEAR_HARD_REG_SET (*no_global_set);
239   CLEAR_HARD_REG_SET (*elim_set);
240
241   compute_regs_asm_clobbered (regs_asm_clobbered);
242   /* Build the regset of all eliminable registers and show we can't use those
243      that we already know won't be eliminated.  */
244 #ifdef ELIMINABLE_REGS
245   for (i = 0; i < ARRAY_SIZE (eliminables); i++)
246     {
247       bool cannot_elim
248         = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
249            || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
250
251       if (!regs_asm_clobbered[eliminables[i].from])
252         {
253           SET_HARD_REG_BIT (*elim_set, eliminables[i].from);
254
255           if (cannot_elim)
256             SET_HARD_REG_BIT (*no_global_set, eliminables[i].from);
257         }
258       else if (cannot_elim)
259         error ("%s cannot be used in asm here",
260                reg_names[eliminables[i].from]);
261       else
262         df_set_regs_ever_live (eliminables[i].from, true);
263     }
264 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
265   if (!regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
266     {
267       SET_HARD_REG_BIT (*elim_set, HARD_FRAME_POINTER_REGNUM);
268       if (need_fp)
269         SET_HARD_REG_BIT (*no_global_set, HARD_FRAME_POINTER_REGNUM);
270     }
271   else if (need_fp)
272     error ("%s cannot be used in asm here",
273            reg_names[HARD_FRAME_POINTER_REGNUM]);
274   else
275     df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
276 #endif
277
278 #else
279   if (!regs_asm_clobbered[FRAME_POINTER_REGNUM])
280     {
281       SET_HARD_REG_BIT (*elim_set, FRAME_POINTER_REGNUM);
282       if (need_fp)
283         SET_HARD_REG_BIT (*no_global_set, FRAME_POINTER_REGNUM);
284     }
285   else if (need_fp)
286     error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
287   else
288     df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
289 #endif
290 }
291
292 /* Perform allocation of pseudo-registers not allocated by local_alloc.
293
294    Return value is nonzero if reload failed
295    and we must not do any more for this function.  */
296
297 static int
298 global_alloc (void)
299 {
300   int retval;
301   size_t i;
302   int max_blk;
303   int *num_allocnos_per_blk;
304
305   compute_regsets (&eliminable_regset, &no_global_alloc_regs);
306
307   /* Track which registers have already been used.  Start with registers
308      explicitly in the rtl, then registers allocated by local register
309      allocation.  */
310
311   CLEAR_HARD_REG_SET (regs_used_so_far);
312 #ifdef LEAF_REGISTERS
313   /* If we are doing the leaf function optimization, and this is a leaf
314      function, it means that the registers that take work to save are those
315      that need a register window.  So prefer the ones that can be used in
316      a leaf function.  */
317   {
318     const char *cheap_regs;
319     const char *const leaf_regs = LEAF_REGISTERS;
320
321     if (only_leaf_regs_used () && leaf_function_p ())
322       cheap_regs = leaf_regs;
323     else
324       cheap_regs = call_used_regs;
325     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
326       if (df_regs_ever_live_p (i) || cheap_regs[i])
327         SET_HARD_REG_BIT (regs_used_so_far, i);
328   }
329 #else
330   /* We consider registers that do not have to be saved over calls as if
331      they were already used since there is no cost in using them.  */
332   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
333     if (df_regs_ever_live_p (i) || call_used_regs[i])
334       SET_HARD_REG_BIT (regs_used_so_far, i);
335 #endif
336
337   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
338     if (reg_renumber[i] >= 0)
339       SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
340
341   /* Establish mappings from register number to allocation number
342      and vice versa.  In the process, count the allocnos.  */
343
344   reg_allocno = XNEWVEC (int, max_regno);
345
346   /* Initially fill the reg_allocno array with regno's...  */
347   max_blk = 0;
348   max_allocno = 0;
349   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
350     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
351        that we are supposed to refrain from putting in a hard reg.
352        -2 means do make an allocno but don't allocate it.  */
353     if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
354         /* Don't allocate pseudos that cross calls,
355            if this function receives a nonlocal goto.  */
356         && (! cfun->has_nonlocal_label
357             || REG_N_CALLS_CROSSED (i) == 0))
358       {
359         int blk = regno_basic_block (i);
360         reg_allocno[max_allocno++] = i;
361         if (blk > max_blk)
362           max_blk = blk;
363         gcc_assert (REG_LIVE_LENGTH (i));
364       }
365
366   allocno = XCNEWVEC (struct allocno, max_allocno);
367   partial_bitnum = XNEWVEC (HOST_WIDE_INT, max_allocno);
368   num_allocnos_per_blk = XCNEWVEC (int, max_blk + 1);
369
370   /* ...so we can sort them in the order we want them to receive
371      their allocnos.  */
372   qsort (reg_allocno, max_allocno, sizeof (int), regno_compare);
373
374   for (i = 0; i < (size_t) max_allocno; i++)
375     {
376       int regno = reg_allocno[i];
377       int blk = regno_basic_block (regno);
378       num_allocnos_per_blk[blk]++;
379       allocno[i].reg = regno;
380       allocno[i].size = PSEUDO_REGNO_SIZE (regno);
381       allocno[i].calls_crossed += REG_N_CALLS_CROSSED (regno);
382       allocno[i].freq_calls_crossed += REG_FREQ_CALLS_CROSSED (regno);
383       allocno[i].throwing_calls_crossed
384         += REG_N_THROWING_CALLS_CROSSED (regno);
385       allocno[i].n_refs += REG_N_REFS (regno);
386       allocno[i].freq += REG_FREQ (regno);
387       if (allocno[i].live_length < REG_LIVE_LENGTH (regno))
388         allocno[i].live_length = REG_LIVE_LENGTH (regno);
389     }
390
391   /* The "global" block must contain all allocnos.  */
392   num_allocnos_per_blk[0] = max_allocno;
393
394   /* Now reinitialize the reg_allocno array in terms of the
395      optimized regno to allocno mapping we created above.  */
396   for (i = 0; i < (size_t) max_regno; i++)
397     reg_allocno[i] = -1;
398
399   max_bitnum = 0;
400   for (i = 0; i < (size_t) max_allocno; i++)
401     {
402       int regno = allocno[i].reg;
403       int blk = regno_basic_block (regno);
404       int row_size = --num_allocnos_per_blk[blk];
405       reg_allocno[regno] = (int) i;
406       partial_bitnum[i] = (row_size > 0) ? max_bitnum - ((int) i + 1) : -1;
407       max_bitnum += row_size;
408     }
409
410 #ifdef ENABLE_CHECKING
411   gcc_assert (max_bitnum <=
412               (((HOST_WIDE_INT) max_allocno *
413                 ((HOST_WIDE_INT) max_allocno - 1)) / 2));
414 #endif
415
416   if (dump_file)
417     {
418       HOST_WIDE_INT num_bits, num_bytes, actual_bytes;
419
420       fprintf (dump_file, "## max_blk:     %d\n", max_blk);
421       fprintf (dump_file, "## max_regno:   %d\n", max_regno);
422       fprintf (dump_file, "## max_allocno: %d\n", max_allocno);
423
424       num_bits = max_bitnum;
425       num_bytes = CEIL (num_bits, 8);
426       actual_bytes = num_bytes;
427       fprintf (dump_file, "## Compressed triangular bitmatrix size: ");
428       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
429       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes\n", num_bytes);
430
431       num_bits = ((HOST_WIDE_INT) max_allocno *
432                   ((HOST_WIDE_INT) max_allocno - 1)) / 2;
433       num_bytes = CEIL (num_bits, 8);
434       fprintf (dump_file, "## Standard triangular bitmatrix size:   ");
435       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
436       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes [%.2f%%]\n",
437                num_bytes, 100.0 * ((double) actual_bytes / (double) num_bytes));
438
439       num_bits = (HOST_WIDE_INT) max_allocno * (HOST_WIDE_INT) max_allocno;
440       num_bytes = CEIL (num_bits, 8);
441       fprintf (dump_file, "## Square bitmatrix size:                ");
442       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
443       fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes [%.2f%%]\n",
444                num_bytes, 100.0 * ((double) actual_bytes / (double) num_bytes));
445     }
446
447   /* Calculate amount of usage of each hard reg by pseudos
448      allocated by local-alloc.  This is to see if we want to
449      override it.  */
450   memset (local_reg_live_length, 0, sizeof local_reg_live_length);
451   memset (local_reg_n_refs, 0, sizeof local_reg_n_refs);
452   memset (local_reg_freq, 0, sizeof local_reg_freq);
453   for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
454     if (reg_renumber[i] >= 0)
455       {
456         int regno = reg_renumber[i];
457         int endregno = end_hard_regno (PSEUDO_REGNO_MODE (i), regno);
458         int j;
459
460         for (j = regno; j < endregno; j++)
461           {
462             local_reg_n_refs[j] += REG_N_REFS (i);
463             local_reg_freq[j] += REG_FREQ (i);
464             local_reg_live_length[j] += REG_LIVE_LENGTH (i);
465           }
466       }
467
468   /* We can't override local-alloc for a reg used not just by local-alloc.  */
469   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470     if (df_regs_ever_live_p (i))
471       local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
472
473   if (dump_file)
474     {
475       for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
476         {
477           fprintf (dump_file, "%d REG_N_REFS=%d, REG_FREQ=%d, REG_LIVE_LENGTH=%d\n", 
478                    (int)i, REG_N_REFS (i), REG_FREQ (i), REG_LIVE_LENGTH (i));
479         }
480       fprintf (dump_file, "regs_ever_live =");
481       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
482         if (df_regs_ever_live_p (i))
483           fprintf (dump_file, " %d", (int)i);
484       fprintf (dump_file, "\n");
485     }
486
487   conflicts = NULL;
488   adjacency = NULL;
489   adjacency_pool = NULL;
490
491   /* If there is work to be done (at least one reg to allocate),
492      perform global conflict analysis and allocate the regs.  */
493
494   if (max_allocno > 0)
495     {
496       /* We used to use alloca here, but the size of what it would try to
497          allocate would occasionally cause it to exceed the stack limit and
498          cause unpredictable core dumps.  Some examples were > 2Mb in size.  */
499       conflicts = XCNEWVEC (HOST_WIDEST_FAST_INT,
500                             CEIL(max_bitnum, HOST_BITS_PER_WIDEST_FAST_INT));
501
502       adjacency = XCNEWVEC (adjacency_t *, max_allocno);
503       adjacency_pool = create_alloc_pool ("global_alloc adjacency list pool",
504                                           sizeof (adjacency_t), 1024);
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       /* There is just too much going on in the register allocators to
512          keep things up to date.  At the end we have to rescan anyway
513          because things change when the reload_completed flag is set.  
514          So we just turn off scanning and we will rescan by hand.  
515
516          However, we needed to do the rescanning before this point to
517          get the new insns scanned inserted by local_alloc scanned for
518          global_conflicts.  */
519       df_set_flags (DF_NO_INSN_RESCAN);
520
521       /* Eliminate conflicts between pseudos and eliminable registers.  If
522          the register is not eliminated, the pseudo won't really be able to
523          live in the eliminable register, so the conflict doesn't matter.
524          If we do eliminate the register, the conflict will no longer exist.
525          So in either case, we can ignore the conflict.  Likewise for
526          preferences.  */
527
528       set_preferences ();
529
530       for (i = 0; i < (size_t) max_allocno; i++)
531         {
532           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
533                                   eliminable_regset);
534           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
535                                   eliminable_regset);
536           AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
537                                   eliminable_regset);
538         }
539
540       /* Try to expand the preferences by merging them between allocnos.  */
541
542       expand_preferences ();
543
544       /* Determine the order to allocate the remaining pseudo registers.  */
545
546       allocno_order = XNEWVEC (int, max_allocno);
547       for (i = 0; i < (size_t) max_allocno; i++)
548         allocno_order[i] = i;
549
550       /* Default the size to 1, since allocno_compare uses it to divide by.
551          Also convert allocno_live_length of zero to -1.  A length of zero
552          can occur when all the registers for that allocno have reg_live_length
553          equal to -2.  In this case, we want to make an allocno, but not
554          allocate it.  So avoid the divide-by-zero and set it to a low
555          priority.  */
556
557       for (i = 0; i < (size_t) max_allocno; i++)
558         {
559           if (allocno[i].size == 0)
560             allocno[i].size = 1;
561           if (allocno[i].live_length == 0)
562             allocno[i].live_length = -1;
563         }
564
565       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
566
567       prune_preferences ();
568
569       if (dump_file)
570         dump_conflicts (dump_file);
571
572       /* Try allocating them, one by one, in that order,
573          except for parameters marked with reg_live_length[regno] == -2.  */
574
575       for (i = 0; i < (size_t) max_allocno; i++)
576         if (reg_renumber[allocno[allocno_order[i]].reg] < 0
577             && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
578           {
579             if (!dbg_cnt (global_alloc_at_reg))
580               break;
581             /* If we have more than one register class,
582                first try allocating in the class that is cheapest
583                for this pseudo-reg.  If that fails, try any reg.  */
584             if (N_REG_CLASSES > 1)
585               {
586                 find_reg (allocno_order[i], 0, 0, 0, 0);
587                 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
588                   continue;
589               }
590             if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
591               find_reg (allocno_order[i], 0, 1, 0, 0);
592           }
593
594       free (allocno_order);
595       free (conflicts);
596     }
597
598   /* Do the reloads now while the allocno data still exists, so that we can
599      try to assign new hard regs to any pseudo regs that are spilled.  */
600
601 #if 0 /* We need to eliminate regs even if there is no rtl code,
602          for the sake of debugging information.  */
603   if (n_basic_blocks > NUM_FIXED_BLOCKS)
604 #endif
605     {
606       build_insn_chain ();
607       retval = reload (get_insns (), 1);
608     }
609
610   /* Clean up.  */
611   free (reg_allocno);
612   free (num_allocnos_per_blk);
613   free (partial_bitnum);
614   free (allocno);
615   if (adjacency != NULL)
616     {
617       free_alloc_pool (adjacency_pool);
618       free (adjacency);
619     }
620
621   return retval;
622 }
623
624 /* Sort predicate for ordering the regnos.  We want the regno to allocno
625    mapping to have the property that all "global" regnos (ie, regnos that
626    are referenced in more than one basic block) have smaller allocno values
627    than "local" regnos (ie, regnos referenced in only one basic block).
628    In addition, for two basic blocks "i" and "j" with i < j, all regnos
629    local to basic block i should have smaller allocno values than regnos
630    local to basic block j.
631    Returns -1 (1) if *v1p should be allocated before (after) *v2p.  */
632
633 static int
634 regno_compare (const void *v1p, const void *v2p)
635 {
636   int regno1 = *(const int *)v1p;
637   int regno2 = *(const int *)v2p;
638   int blk1 = REG_BASIC_BLOCK (regno1);
639   int blk2 = REG_BASIC_BLOCK (regno2);
640
641   /* Prefer lower numbered basic blocks.  Note that global and unknown
642      blocks have negative values, giving them high precedence.  */
643   if (blk1 - blk2)
644     return blk1 - blk2;
645
646   /* If both regs are referenced from the same block, sort by regno.  */
647   return regno1 - regno2;
648 }
649
650 /* Sort predicate for ordering the allocnos.
651    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
652
653 static int
654 allocno_compare (const void *v1p, const void *v2p)
655 {
656   int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
657   /* Note that the quotient will never be bigger than
658      the value of floor_log2 times the maximum number of
659      times a register can occur in one insn (surely less than 100)
660      weighted by the frequency (maximally REG_FREQ_MAX).
661      Multiplying this by 10000/REG_FREQ_MAX can't overflow.  */
662   int pri1
663     = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
664         / allocno[v1].live_length)
665        * (10000 / REG_FREQ_MAX) * allocno[v1].size);
666   int pri2
667     = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
668         / allocno[v2].live_length)
669        * (10000 / REG_FREQ_MAX) * allocno[v2].size);
670   if (pri2 - pri1)
671     return pri2 - pri1;
672
673   /* If regs are equally good, sort by allocno,
674      so that the results of qsort leave nothing to chance.  */
675   return v1 - v2;
676 }
677 \f
678 /* Expand the preference information by looking for cases where one allocno
679    dies in an insn that sets an allocno.  If those two allocnos don't conflict,
680    merge any preferences between those allocnos.  */
681
682 static void
683 expand_preferences (void)
684 {
685   rtx insn;
686   rtx link;
687   rtx set;
688
689   /* We only try to handle the most common cases here.  Most of the cases
690      where this wins are reg-reg copies.  */
691
692   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
693     if (INSN_P (insn)
694         && (set = single_set (insn)) != 0
695         && REG_P (SET_DEST (set))
696         && reg_allocno[REGNO (SET_DEST (set))] >= 0)
697       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
698         if (REG_NOTE_KIND (link) == REG_DEAD
699             && REG_P (XEXP (link, 0))
700             && reg_allocno[REGNO (XEXP (link, 0))] >= 0
701             && ! conflict_p (reg_allocno[REGNO (SET_DEST (set))],
702                              reg_allocno[REGNO (XEXP (link, 0))]))
703           {
704             int a1 = reg_allocno[REGNO (SET_DEST (set))];
705             int a2 = reg_allocno[REGNO (XEXP (link, 0))];
706
707             if (XEXP (link, 0) == SET_SRC (set))
708               {
709                 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
710                                   allocno[a2].hard_reg_copy_preferences);
711                 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
712                                   allocno[a1].hard_reg_copy_preferences);
713               }
714
715             IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
716                               allocno[a2].hard_reg_preferences);
717             IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
718                               allocno[a1].hard_reg_preferences);
719             IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
720                               allocno[a2].hard_reg_full_preferences);
721             IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
722                               allocno[a1].hard_reg_full_preferences);
723           }
724 }
725
726
727 /* Try to set a preference for an allocno to a hard register.
728    We are passed DEST and SRC which are the operands of a SET.  It is known
729    that SRC is a register.  If SRC or the first operand of SRC is a register,
730    try to set a preference.  If one of the two is a hard register and the other
731    is a pseudo-register, mark the preference.
732
733    Note that we are not as aggressive as local-alloc in trying to tie a
734    pseudo-register to a hard register.  */
735
736 static void
737 set_preference (rtx dest, rtx src)
738 {
739   unsigned int src_regno, dest_regno, end_regno;
740   /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
741      to compensate for subregs in SRC or DEST.  */
742   int offset = 0;
743   unsigned int i;
744   int copy = 1;
745
746   if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
747     src = XEXP (src, 0), copy = 0;
748
749   /* Get the reg number for both SRC and DEST.
750      If neither is a reg, give up.  */
751
752   if (REG_P (src))
753     src_regno = REGNO (src);
754   else if (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src)))
755     {
756       src_regno = REGNO (SUBREG_REG (src));
757
758       if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
759         offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
760                                        GET_MODE (SUBREG_REG (src)),
761                                        SUBREG_BYTE (src),
762                                        GET_MODE (src));
763       else
764         offset += (SUBREG_BYTE (src)
765                    / REGMODE_NATURAL_SIZE (GET_MODE (src)));
766     }
767   else
768     return;
769
770   if (REG_P (dest))
771     dest_regno = REGNO (dest);
772   else if (GET_CODE (dest) == SUBREG && REG_P (SUBREG_REG (dest)))
773     {
774       dest_regno = REGNO (SUBREG_REG (dest));
775
776       if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
777         offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
778                                        GET_MODE (SUBREG_REG (dest)),
779                                        SUBREG_BYTE (dest),
780                                        GET_MODE (dest));
781       else
782         offset -= (SUBREG_BYTE (dest)
783                    / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
784     }
785   else
786     return;
787
788   /* Convert either or both to hard reg numbers.  */
789
790   if (reg_renumber[src_regno] >= 0)
791     src_regno = reg_renumber[src_regno];
792
793   if (reg_renumber[dest_regno] >= 0)
794     dest_regno = reg_renumber[dest_regno];
795
796   /* Now if one is a hard reg and the other is a global pseudo
797      then give the other a preference.  */
798
799   if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
800       && reg_allocno[src_regno] >= 0)
801     {
802       dest_regno -= offset;
803       if (dest_regno < FIRST_PSEUDO_REGISTER)
804         {
805           if (copy)
806             SET_REGBIT (hard_reg_copy_preferences,
807                         reg_allocno[src_regno], dest_regno);
808
809           SET_REGBIT (hard_reg_preferences,
810                       reg_allocno[src_regno], dest_regno);
811           end_regno = end_hard_regno (GET_MODE (dest), dest_regno);
812           for (i = dest_regno; i < end_regno; i++)
813             SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
814         }
815     }
816
817   if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
818       && reg_allocno[dest_regno] >= 0)
819     {
820       src_regno += offset;
821       if (src_regno < FIRST_PSEUDO_REGISTER)
822         {
823           if (copy)
824             SET_REGBIT (hard_reg_copy_preferences,
825                         reg_allocno[dest_regno], src_regno);
826
827           SET_REGBIT (hard_reg_preferences,
828                       reg_allocno[dest_regno], src_regno);
829           end_regno = end_hard_regno (GET_MODE (src), src_regno);
830           for (i = src_regno; i < end_regno; i++)
831             SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
832         }
833     }
834 }
835 \f
836 /* Helper function for set_preferences.  */
837 static void
838 set_preferences_1 (rtx reg, const_rtx setter, void *data ATTRIBUTE_UNUSED)
839 {
840   if (GET_CODE (reg) == SUBREG)
841     reg = SUBREG_REG (reg);
842
843   if (!REG_P (reg))
844     return;
845
846   gcc_assert (setter);
847   if (GET_CODE (setter) != CLOBBER)
848     set_preference (reg, SET_SRC (setter));
849 }
850 \f
851 /* Scan all of the insns and initialize the preferences.  */
852
853 static void 
854 set_preferences (void)
855 {
856   basic_block bb;
857   rtx insn;
858   FOR_EACH_BB (bb)
859     FOR_BB_INSNS_REVERSE (bb, insn)
860       {
861         if (!INSN_P (insn))
862           continue;     
863
864         note_stores (PATTERN (insn), set_preferences_1, NULL);
865       }
866 }
867
868
869 \f
870 /* Prune the preferences for global registers to exclude registers that cannot
871    be used.
872
873    Compute `regs_someone_prefers', which is a bitmask of the hard registers
874    that are preferred by conflicting registers of lower priority.  If possible,
875    we will avoid using these registers.  */
876
877 static void
878 prune_preferences (void)
879 {
880   int i;
881   int num;
882   int *allocno_to_order = XNEWVEC (int, max_allocno);
883
884   /* Scan least most important to most important.
885      For each allocno, remove from preferences registers that cannot be used,
886      either because of conflicts or register type.  Then compute all registers
887      preferred by each lower-priority register that conflicts.  */
888
889   for (i = max_allocno - 1; i >= 0; i--)
890     {
891       HARD_REG_SET temp;
892
893       num = allocno_order[i];
894       allocno_to_order[num] = i;
895       COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
896
897       if (allocno[num].calls_crossed == 0)
898         IOR_HARD_REG_SET (temp, fixed_reg_set);
899       else
900         IOR_HARD_REG_SET (temp, call_used_reg_set);
901
902       IOR_COMPL_HARD_REG_SET
903         (temp,
904          reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
905
906       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
907       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
908       AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
909     }
910
911   for (i = max_allocno - 1; i >= 0; i--)
912     {
913       /* Merge in the preferences of lower-priority registers (they have
914          already been pruned).  If we also prefer some of those registers,
915          don't exclude them unless we are of a smaller size (in which case
916          we want to give the lower-priority allocno the first chance for
917          these registers).  */
918       HARD_REG_SET temp, temp2;
919       int allocno2;
920       adjacency_iter ai;
921
922       num = allocno_order[i];
923
924       CLEAR_HARD_REG_SET (temp);
925       CLEAR_HARD_REG_SET (temp2);
926
927       FOR_EACH_CONFLICT (num, allocno2, ai)
928         {
929           if (allocno_to_order[allocno2] > i)
930             {
931               if (allocno[allocno2].size <= allocno[num].size)
932                 IOR_HARD_REG_SET (temp,
933                                   allocno[allocno2].hard_reg_full_preferences);
934               else
935                 IOR_HARD_REG_SET (temp2,
936                                   allocno[allocno2].hard_reg_full_preferences);
937             }
938         }
939
940       AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
941       IOR_HARD_REG_SET (temp, temp2);
942       COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
943     }
944   free (allocno_to_order);
945 }
946 \f
947 /* Assign a hard register to allocno NUM; look for one that is the beginning
948    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
949    The registers marked in PREFREGS are tried first.
950
951    LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
952    be used for this allocation.
953
954    If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
955    Otherwise ignore that preferred class and use the alternate class.
956
957    If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
958    will have to be saved and restored at calls.
959
960    RETRYING is nonzero if this is called from retry_global_alloc.
961
962    If we find one, record it in reg_renumber.
963    If not, do nothing.  */
964
965 static void
966 find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
967 {
968   int i, best_reg, pass;
969   HARD_REG_SET used, used1, used2;
970
971   enum reg_class class = (alt_regs_p
972                           ? reg_alternate_class (allocno[num].reg)
973                           : reg_preferred_class (allocno[num].reg));
974   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
975
976   if (accept_call_clobbered)
977     COPY_HARD_REG_SET (used1, call_fixed_reg_set);
978   else if (allocno[num].calls_crossed == 0)
979     COPY_HARD_REG_SET (used1, fixed_reg_set);
980   else
981     COPY_HARD_REG_SET (used1, call_used_reg_set);
982
983   /* Some registers should not be allocated in global-alloc.  */
984   IOR_HARD_REG_SET (used1, no_global_alloc_regs);
985   if (losers)
986     IOR_HARD_REG_SET (used1, losers);
987
988   IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
989
990 #ifdef EH_RETURN_DATA_REGNO
991   if (allocno[num].no_eh_reg)
992     {
993       unsigned int j;
994       for (j = 0; ; ++j)
995         {
996           unsigned int regno = EH_RETURN_DATA_REGNO (j);
997           if (regno == INVALID_REGNUM)
998             break;
999           SET_HARD_REG_BIT (used1, regno);
1000         }
1001     }
1002 #endif
1003
1004   COPY_HARD_REG_SET (used2, used1);
1005
1006   IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1007
1008 #ifdef CANNOT_CHANGE_MODE_CLASS
1009   cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1010 #endif
1011
1012   /* Try each hard reg to see if it fits.  Do this in two passes.
1013      In the first pass, skip registers that are preferred by some other pseudo
1014      to give it a better chance of getting one of those registers.  Only if
1015      we can't get a register when excluding those do we take one of them.
1016      However, we never allocate a register for the first time in pass 0.  */
1017
1018   COPY_HARD_REG_SET (used, used1);
1019   IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1020   IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1021
1022   best_reg = -1;
1023   for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1024        pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1025        pass++)
1026     {
1027       if (pass == 1)
1028         COPY_HARD_REG_SET (used, used1);
1029       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1030         {
1031 #ifdef REG_ALLOC_ORDER
1032           int regno = reg_alloc_order[i];
1033 #else
1034           int regno = i;
1035 #endif
1036           if (! TEST_HARD_REG_BIT (used, regno)
1037               && HARD_REGNO_MODE_OK (regno, mode)
1038               && (allocno[num].calls_crossed == 0
1039                   || accept_call_clobbered
1040                   || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1041             {
1042               int j;
1043               int lim = end_hard_regno (mode, regno);
1044               for (j = regno + 1;
1045                    (j < lim
1046                     && ! TEST_HARD_REG_BIT (used, j));
1047                    j++);
1048               if (j == lim)
1049                 {
1050                   best_reg = regno;
1051                   break;
1052                 }
1053 #ifndef REG_ALLOC_ORDER
1054               i = j;                    /* Skip starting points we know will lose */
1055 #endif
1056             }
1057           }
1058       }
1059
1060   /* See if there is a preferred register with the same class as the register
1061      we allocated above.  Making this restriction prevents register
1062      preferencing from creating worse register allocation.
1063
1064      Remove from the preferred registers and conflicting registers.  Note that
1065      additional conflicts may have been added after `prune_preferences' was
1066      called.
1067
1068      First do this for those register with copy preferences, then all
1069      preferred registers.  */
1070
1071   AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1072   if (!hard_reg_set_empty_p (allocno[num].hard_reg_copy_preferences)
1073       && best_reg >= 0)
1074     {
1075       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1076         if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1077             && HARD_REGNO_MODE_OK (i, mode)
1078             && (allocno[num].calls_crossed == 0
1079                 || accept_call_clobbered
1080                 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1081             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1082                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1083                                        REGNO_REG_CLASS (best_reg))
1084                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1085                                        REGNO_REG_CLASS (i))))
1086             {
1087               int j;
1088               int lim = end_hard_regno (mode, i);
1089               for (j = i + 1;
1090                    (j < lim
1091                     && ! TEST_HARD_REG_BIT (used, j)
1092                     && (REGNO_REG_CLASS (j)
1093                         == REGNO_REG_CLASS (best_reg + (j - i))
1094                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1095                                                REGNO_REG_CLASS (best_reg + (j - i)))
1096                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1097                                                REGNO_REG_CLASS (j))));
1098                    j++);
1099               if (j == lim)
1100                 {
1101                   best_reg = i;
1102                   goto no_prefs;
1103                 }
1104             }
1105     }
1106
1107   AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1108   if (!hard_reg_set_empty_p (allocno[num].hard_reg_preferences)
1109       && best_reg >= 0)
1110     {
1111       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1112         if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1113             && HARD_REGNO_MODE_OK (i, mode)
1114             && (allocno[num].calls_crossed == 0
1115                 || accept_call_clobbered
1116                 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1117             && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1118                 || reg_class_subset_p (REGNO_REG_CLASS (i),
1119                                        REGNO_REG_CLASS (best_reg))
1120                 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1121                                        REGNO_REG_CLASS (i))))
1122             {
1123               int j;
1124               int lim = end_hard_regno (mode, i);
1125               for (j = i + 1;
1126                    (j < lim
1127                     && ! TEST_HARD_REG_BIT (used, j)
1128                     && (REGNO_REG_CLASS (j)
1129                         == REGNO_REG_CLASS (best_reg + (j - i))
1130                         || reg_class_subset_p (REGNO_REG_CLASS (j),
1131                                                REGNO_REG_CLASS (best_reg + (j - i)))
1132                         || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1133                                                REGNO_REG_CLASS (j))));
1134                    j++);
1135               if (j == lim)
1136                 {
1137                   best_reg = i;
1138                   break;
1139                 }
1140             }
1141     }
1142  no_prefs:
1143
1144   /* If we haven't succeeded yet, try with caller-saves.
1145      We need not check to see if the current function has nonlocal
1146      labels because we don't put any pseudos that are live over calls in
1147      registers in that case.  */
1148
1149   if (flag_caller_saves && best_reg < 0)
1150     {
1151       /* Did not find a register.  If it would be profitable to
1152          allocate a call-clobbered register and save and restore it
1153          around calls, do that.  Don't do this if it crosses any calls
1154          that might throw.  */
1155       if (! accept_call_clobbered
1156           && allocno[num].calls_crossed != 0
1157           && allocno[num].throwing_calls_crossed == 0
1158           && CALLER_SAVE_PROFITABLE (optimize_size ? allocno[num].n_refs : allocno[num].freq,
1159                                      optimize_size ? allocno[num].calls_crossed
1160                                      : allocno[num].freq_calls_crossed))
1161         {
1162           HARD_REG_SET new_losers;
1163           if (! losers)
1164             CLEAR_HARD_REG_SET (new_losers);
1165           else
1166             COPY_HARD_REG_SET (new_losers, losers);
1167
1168           IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1169           find_reg (num, new_losers, alt_regs_p, 1, retrying);
1170           if (reg_renumber[allocno[num].reg] >= 0)
1171             {
1172               caller_save_needed = 1;
1173               return;
1174             }
1175         }
1176     }
1177
1178   /* If we haven't succeeded yet,
1179      see if some hard reg that conflicts with us
1180      was utilized poorly by local-alloc.
1181      If so, kick out the regs that were put there by local-alloc
1182      so we can use it instead.  */
1183   if (best_reg < 0 && !retrying
1184       /* Let's not bother with multi-reg allocnos.  */
1185       && allocno[num].size == 1
1186       && REG_BASIC_BLOCK (allocno[num].reg) == REG_BLOCK_GLOBAL)
1187     {
1188       /* Count from the end, to find the least-used ones first.  */
1189       for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1190         {
1191 #ifdef REG_ALLOC_ORDER
1192           int regno = reg_alloc_order[i];
1193 #else
1194           int regno = i;
1195 #endif
1196
1197           if (local_reg_n_refs[regno] != 0
1198               /* Don't use a reg no good for this pseudo.  */
1199               && ! TEST_HARD_REG_BIT (used2, regno)
1200               && HARD_REGNO_MODE_OK (regno, mode)
1201               /* The code below assumes that we need only a single
1202                  register, but the check of allocno[num].size above
1203                  was not enough.  Sometimes we need more than one
1204                  register for a single-word value.  */
1205               && hard_regno_nregs[regno][mode] == 1
1206               && (allocno[num].calls_crossed == 0
1207                   || accept_call_clobbered
1208                   || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1209 #ifdef CANNOT_CHANGE_MODE_CLASS
1210               && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1211                                           mode)
1212 #endif
1213 #ifdef STACK_REGS
1214              && (!allocno[num].no_stack_reg
1215                  || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
1216 #endif
1217               )
1218             {
1219               /* We explicitly evaluate the divide results into temporary
1220                  variables so as to avoid excess precision problems that occur
1221                  on an i386-unknown-sysv4.2 (unixware) host.  */
1222
1223               double tmp1 = ((double) local_reg_freq[regno] * local_reg_n_refs[regno]
1224                             / local_reg_live_length[regno]);
1225               double tmp2 = ((double) allocno[num].freq * allocno[num].n_refs
1226                              / allocno[num].live_length);
1227
1228               if (tmp1 < tmp2)
1229                 {
1230                   /* Hard reg REGNO was used less in total by local regs
1231                      than it would be used by this one allocno!  */
1232                   int k;
1233                   if (dump_file)
1234                     {
1235                       fprintf (dump_file, "Regno %d better for global %d, ",
1236                                regno, allocno[num].reg);
1237                       fprintf (dump_file, "fr:%d, ll:%d, nr:%d ",
1238                                allocno[num].freq, allocno[num].live_length,
1239                                allocno[num].n_refs);
1240                       fprintf (dump_file, "(was: fr:%d, ll:%d, nr:%d)\n",
1241                                local_reg_freq[regno],
1242                                local_reg_live_length[regno],
1243                                local_reg_n_refs[regno]);
1244                     }
1245
1246                   for (k = 0; k < max_regno; k++)
1247                     if (reg_renumber[k] >= 0)
1248                       {
1249                         int r = reg_renumber[k];
1250                         int endregno
1251                           = end_hard_regno (PSEUDO_REGNO_MODE (k), r);
1252
1253                         if (regno >= r && regno < endregno)
1254                           {
1255                             if (dump_file)
1256                               fprintf (dump_file,
1257                                        "Local Reg %d now on stack\n", k);
1258                             reg_renumber[k] = -1;
1259                           }
1260                       }
1261
1262                   best_reg = regno;
1263                   break;
1264                 }
1265             }
1266         }
1267     }
1268
1269   /* Did we find a register?  */
1270
1271   if (best_reg >= 0)
1272     {
1273       int lim, j;
1274       HARD_REG_SET this_reg;
1275       adjacency_iter ai;
1276
1277       /* Yes.  Record it as the hard register of this pseudo-reg.  */
1278       reg_renumber[allocno[num].reg] = best_reg;
1279
1280       /* Make a set of the hard regs being allocated.  */
1281       CLEAR_HARD_REG_SET (this_reg);
1282       lim = end_hard_regno (mode, best_reg);
1283       for (j = best_reg; j < lim; j++)
1284         {
1285           SET_HARD_REG_BIT (this_reg, j);
1286           SET_HARD_REG_BIT (regs_used_so_far, j);
1287           /* This is no longer a reg used just by local regs.  */
1288           local_reg_n_refs[j] = 0;
1289           local_reg_freq[j] = 0;
1290         }
1291       /* For each other pseudo-reg conflicting with this one,
1292          mark it as conflicting with the hard regs this one occupies.  */
1293       FOR_EACH_CONFLICT (num, j, ai)
1294         {
1295           IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1296         }
1297     }
1298 }
1299 \f
1300 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1301    Perhaps it had previously seemed not worth a hard reg,
1302    or perhaps its old hard reg has been commandeered for reloads.
1303    FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1304    they do not appear to be allocated.
1305    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
1306
1307 void
1308 retry_global_alloc (int regno, HARD_REG_SET forbidden_regs)
1309 {
1310   int alloc_no = reg_allocno[regno];
1311   if (alloc_no >= 0)
1312     {
1313       /* If we have more than one register class,
1314          first try allocating in the class that is cheapest
1315          for this pseudo-reg.  If that fails, try any reg.  */
1316       if (N_REG_CLASSES > 1)
1317         find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1318       if (reg_renumber[regno] < 0
1319           && reg_alternate_class (regno) != NO_REGS)
1320         find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1321
1322       /* If we found a register, modify the RTL for the register to
1323          show the hard register, and mark that register live.  */
1324       if (reg_renumber[regno] >= 0)
1325         {
1326           SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
1327           mark_home_live (regno);
1328         }
1329     }
1330 }
1331 \f
1332 /* Indicate that hard register number FROM was eliminated and replaced with
1333    an offset from hard register number TO.  The status of hard registers live
1334    at the start of a basic block is updated by replacing a use of FROM with
1335    a use of TO.  */
1336
1337 void
1338 mark_elimination (int from, int to)
1339 {
1340   basic_block bb;
1341
1342   FOR_EACH_BB (bb)
1343     {
1344       regset r = DF_LIVE_IN (bb);
1345       if (REGNO_REG_SET_P (r, from))
1346         {
1347           CLEAR_REGNO_REG_SET (r, from);
1348           SET_REGNO_REG_SET (r, to);
1349         }
1350     }
1351 }
1352 \f
1353 /* Print chain C to FILE.  */
1354
1355 static void
1356 print_insn_chain (FILE *file, struct insn_chain *c)
1357 {
1358   fprintf (file, "insn=%d, ", INSN_UID(c->insn));
1359   bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
1360   bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
1361 }
1362
1363
1364 /* Print all reload_insn_chains to FILE.  */
1365
1366 static void
1367 print_insn_chains (FILE *file)
1368 {
1369   struct insn_chain *c;
1370   for (c = reload_insn_chain; c ; c = c->next)
1371     print_insn_chain (file, c);
1372 }
1373
1374
1375 /* Walk the insns of the current function and build reload_insn_chain,
1376    and record register life information.  */
1377
1378 static void
1379 build_insn_chain (void)
1380 {
1381   unsigned int i;
1382   struct insn_chain **p = &reload_insn_chain;
1383   basic_block bb;
1384   struct insn_chain *c = NULL;
1385   struct insn_chain *next = NULL;
1386   bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
1387   bitmap elim_regset = BITMAP_ALLOC (NULL);
1388   /* live_subregs is a vector used to keep accurate information about
1389      which hardregs are live in multiword pseudos.  live_subregs and
1390      live_subregs_used are indexed by pseudo number.  The live_subreg
1391      entry for a particular pseudo is only used if the corresponding
1392      element is non zero in live_subregs_used.  The value in
1393      live_subregs_used is number of bytes that the pseudo can
1394      occupy.  */
1395   sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
1396   int *live_subregs_used = XNEWVEC (int, max_regno);
1397
1398   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1399     if (TEST_HARD_REG_BIT (eliminable_regset, i))
1400       bitmap_set_bit (elim_regset, i);
1401
1402   FOR_EACH_BB_REVERSE (bb)
1403     {
1404       bitmap_iterator bi;
1405       rtx insn;
1406       
1407       CLEAR_REG_SET (live_relevant_regs);
1408       memset (live_subregs_used, 0, max_regno * sizeof (int));
1409       
1410       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
1411         {
1412           if (i >= FIRST_PSEUDO_REGISTER)
1413             break;
1414           bitmap_set_bit (live_relevant_regs, i);
1415         }
1416
1417       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), FIRST_PSEUDO_REGISTER, i, bi)
1418         {
1419           if (reg_renumber[i] >= 0)
1420             bitmap_set_bit (live_relevant_regs, i);
1421         }
1422
1423       FOR_BB_INSNS_REVERSE (bb, insn)
1424         {
1425           if (!NOTE_P (insn) && !BARRIER_P (insn))
1426             {
1427               unsigned int uid = INSN_UID (insn);
1428               struct df_ref **def_rec;
1429               struct df_ref **use_rec;
1430
1431               c = new_insn_chain ();
1432               c->next = next;
1433               next = c;
1434               *p = c;
1435               p = &c->prev;
1436               
1437               c->insn = insn;
1438               c->block = bb->index;
1439
1440               if (INSN_P (insn))
1441                 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
1442                   {
1443                     struct df_ref *def = *def_rec;
1444                     unsigned int regno = DF_REF_REGNO (def);
1445                     
1446                     /* Ignore may clobbers because these are generated
1447                        from calls. However, every other kind of def is
1448                        added to dead_or_set.  */
1449                     if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1450                       {
1451                         if (regno < FIRST_PSEUDO_REGISTER)
1452                           {
1453                             if (!fixed_regs[regno])
1454                               bitmap_set_bit (&c->dead_or_set, regno);
1455                           }
1456                         else if (reg_renumber[regno] >= 0)
1457                           bitmap_set_bit (&c->dead_or_set, regno);
1458                       }
1459
1460                     if ((regno < FIRST_PSEUDO_REGISTER || reg_renumber[regno] >= 0)
1461                         && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
1462                       {
1463                         rtx reg = DF_REF_REG (def);
1464
1465                         /* We can model subregs, but not if they are
1466                            wrapped in ZERO_EXTRACTS.  */
1467                         if (GET_CODE (reg) == SUBREG
1468                             && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
1469                           {
1470                             unsigned int start = SUBREG_BYTE (reg);
1471                             unsigned int last = start 
1472                               + GET_MODE_SIZE (GET_MODE (reg));
1473
1474                             ra_init_live_subregs (bitmap_bit_p (live_relevant_regs, 
1475                                                                 regno), 
1476                                                   live_subregs, 
1477                                                   live_subregs_used,
1478                                                   regno, reg);
1479
1480                             if (!DF_REF_FLAGS_IS_SET
1481                                 (def, DF_REF_STRICT_LOW_PART))
1482                               {
1483                                 /* Expand the range to cover entire words.
1484                                    Bytes added here are "don't care".  */
1485                                 start = start / UNITS_PER_WORD * UNITS_PER_WORD;
1486                                 last = ((last + UNITS_PER_WORD - 1)
1487                                         / UNITS_PER_WORD * UNITS_PER_WORD);
1488                               }
1489
1490                             /* Ignore the paradoxical bits.  */
1491                             if ((int)last > live_subregs_used[regno])
1492                               last = live_subregs_used[regno];
1493
1494                             while (start < last)
1495                               {
1496                                 RESET_BIT (live_subregs[regno], start);
1497                                 start++;
1498                               }
1499                             
1500                             if (sbitmap_empty_p (live_subregs[regno]))
1501                               {
1502                                 live_subregs_used[regno] = 0;
1503                                 bitmap_clear_bit (live_relevant_regs, regno);
1504                               }
1505                             else
1506                               /* Set live_relevant_regs here because
1507                                  that bit has to be true to get us to
1508                                  look at the live_subregs fields.  */
1509                               bitmap_set_bit (live_relevant_regs, regno);
1510                           }
1511                         else
1512                           {
1513                             /* DF_REF_PARTIAL is generated for
1514                                subregs, STRICT_LOW_PART, and
1515                                ZERO_EXTRACT.  We handle the subreg
1516                                case above so here we have to keep from
1517                                modeling the def as a killing def.  */
1518                             if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
1519                               {
1520                                 bitmap_clear_bit (live_relevant_regs, regno);
1521                                 live_subregs_used[regno] = 0;
1522                               }
1523                           }
1524                       }
1525                   }
1526           
1527               bitmap_and_compl_into (live_relevant_regs, elim_regset);
1528               bitmap_copy (&c->live_throughout, live_relevant_regs);
1529
1530               if (INSN_P (insn))
1531                 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
1532                   {
1533                     struct df_ref *use = *use_rec;
1534                     unsigned int regno = DF_REF_REGNO (use);
1535                     rtx reg = DF_REF_REG (use);
1536                     
1537                     /* DF_REF_READ_WRITE on a use means that this use
1538                        is fabricated from a def that is a partial set
1539                        to a multiword reg.  Here, we only model the
1540                        subreg case that is not wrapped in ZERO_EXTRACT
1541                        precisely so we do not need to look at the
1542                        fabricated use. */
1543                     if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE) 
1544                         && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT) 
1545                         && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
1546                       continue;
1547                     
1548                     /* Add the last use of each var to dead_or_set.  */
1549                     if (!bitmap_bit_p (live_relevant_regs, regno))
1550                       {
1551                         if (regno < FIRST_PSEUDO_REGISTER)
1552                           {
1553                             if (!fixed_regs[regno])
1554                               bitmap_set_bit (&c->dead_or_set, regno);
1555                           }
1556                         else if (reg_renumber[regno] >= 0)
1557                           bitmap_set_bit (&c->dead_or_set, regno);
1558                       }
1559                     
1560                     if (regno < FIRST_PSEUDO_REGISTER || reg_renumber[regno] >= 0)
1561                       {
1562                         if (GET_CODE (reg) == SUBREG
1563                             && !DF_REF_FLAGS_IS_SET (use,
1564                                                      DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT)) 
1565                           {
1566                             unsigned int start = SUBREG_BYTE (reg);
1567                             unsigned int last = start 
1568                               + GET_MODE_SIZE (GET_MODE (reg));
1569                             
1570                             ra_init_live_subregs (bitmap_bit_p (live_relevant_regs, 
1571                                                                 regno), 
1572                                                   live_subregs, 
1573                                                   live_subregs_used,
1574                                                   regno, reg);
1575                             
1576                             /* Ignore the paradoxical bits.  */
1577                             if ((int)last > live_subregs_used[regno])
1578                               last = live_subregs_used[regno];
1579
1580                             while (start < last)
1581                               {
1582                                 SET_BIT (live_subregs[regno], start);
1583                                 start++;
1584                               }
1585                           }
1586                         else
1587                           /* Resetting the live_subregs_used is
1588                              effectively saying do not use the subregs
1589                              because we are reading the whole
1590                              pseudo.  */
1591                           live_subregs_used[regno] = 0;
1592                         bitmap_set_bit (live_relevant_regs, regno);
1593                       }
1594                   }
1595             }
1596         }
1597
1598       /* FIXME!! The following code is a disaster.  Reload needs to see the
1599          labels and jump tables that are just hanging out in between
1600          the basic blocks.  See pr33676.  */
1601       insn = BB_HEAD (bb);
1602       
1603       /* Skip over the barriers and cruft.  */
1604       while (insn && (BARRIER_P (insn) || NOTE_P (insn) 
1605                       || BLOCK_FOR_INSN (insn) == bb))
1606         insn = PREV_INSN (insn);
1607       
1608       /* While we add anything except barriers and notes, the focus is
1609          to get the labels and jump tables into the
1610          reload_insn_chain.  */
1611       while (insn)
1612         {
1613           if (!NOTE_P (insn) && !BARRIER_P (insn))
1614             {
1615               if (BLOCK_FOR_INSN (insn))
1616                 break;
1617               
1618               c = new_insn_chain ();
1619               c->next = next;
1620               next = c;
1621               *p = c;
1622               p = &c->prev;
1623               
1624               /* The block makes no sense here, but it is what the old
1625                  code did.  */
1626               c->block = bb->index;
1627               c->insn = insn;
1628               bitmap_copy (&c->live_throughout, live_relevant_regs);
1629             }     
1630           insn = PREV_INSN (insn);
1631         }
1632     }
1633
1634   for (i = 0; i < (unsigned int) max_regno; i++)
1635     if (live_subregs[i])
1636       free (live_subregs[i]);
1637
1638   reload_insn_chain = c;
1639   *p = NULL;
1640
1641   free (live_subregs);
1642   free (live_subregs_used);
1643   BITMAP_FREE (live_relevant_regs);
1644   BITMAP_FREE (elim_regset);
1645
1646   if (dump_file)
1647     print_insn_chains (dump_file);
1648 }
1649 \f
1650 /* Print debugging trace information if -dg switch is given,
1651    showing the information on which the allocation decisions are based.  */
1652
1653 static void
1654 dump_conflicts (FILE *file)
1655 {
1656   int i;
1657   int regno;
1658   int has_preferences;
1659   int nregs;
1660   nregs = 0;
1661   for (i = 0; i < max_allocno; i++)
1662     {
1663       if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1664         continue;
1665       nregs++;
1666     }
1667   fprintf (file, ";; %d regs to allocate:", nregs);
1668   for (regno = 0; regno < max_regno; regno++)
1669     if ((i = reg_allocno[regno]) >= 0)
1670       {
1671         int j;
1672         if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1673           continue;
1674         fprintf (file, " %d", allocno[allocno_order[i]].reg);
1675         for (j = 0; j < max_regno; j++)
1676           if (reg_allocno[j] == allocno_order[i]
1677               && j != allocno[allocno_order[i]].reg)
1678             fprintf (file, "+%d", j);
1679         if (allocno[allocno_order[i]].size != 1)
1680           fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1681       }
1682   fprintf (file, "\n");
1683
1684   for (regno = 0; regno < max_regno; regno++)
1685     if ((i = reg_allocno[regno]) >= 0)
1686       {
1687         int j;
1688         adjacency_iter ai;
1689         fprintf (file, ";; %d conflicts:", allocno[i].reg);
1690         FOR_EACH_CONFLICT (i, j, ai)
1691           {
1692             fprintf (file, " %d", allocno[j].reg);
1693           }
1694         for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1695           if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j)
1696               && !fixed_regs[j])
1697             fprintf (file, " %d", j);
1698         fprintf (file, "\n");
1699
1700         has_preferences = 0;
1701         for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1702           if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1703             has_preferences = 1;
1704
1705         if (!has_preferences)
1706           continue;
1707         fprintf (file, ";; %d preferences:", allocno[i].reg);
1708         for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1709           if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1710             fprintf (file, " %d", j);
1711         fprintf (file, "\n");
1712       }
1713   fprintf (file, "\n");
1714 }
1715
1716 void
1717 dump_global_regs (FILE *file)
1718 {
1719   int i, j;
1720
1721   fprintf (file, ";; Register dispositions:\n");
1722   for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1723     if (reg_renumber[i] >= 0)
1724       {
1725         fprintf (file, "%d in %d  ", i, reg_renumber[i]);
1726         if (++j % 6 == 0)
1727           fprintf (file, "\n");
1728       }
1729
1730   fprintf (file, "\n\n;; Hard regs used: ");
1731   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1732     if (df_regs_ever_live_p (i))
1733       fprintf (file, " %d", i);
1734   fprintf (file, "\n\n");
1735 }
1736
1737 /* Run old register allocator.  Return TRUE if we must exit
1738    rest_of_compilation upon return.  */
1739 static unsigned int
1740 rest_of_handle_global_alloc (void)
1741 {
1742   bool failure;
1743
1744   /* If optimizing, allocate remaining pseudo-regs.  Do the reload
1745      pass fixing up any insns that are invalid.  */
1746   if (optimize && dbg_cnt (global_alloc_at_func))
1747     failure = global_alloc ();
1748   else
1749     {
1750       /* There is just too much going on in the register allocators to
1751          keep things up to date.  At the end we have to rescan anyway
1752          because things change when the reload_completed flag is set.  
1753          So we just turn off scanning and we will rescan by hand.  */
1754       df_set_flags (DF_NO_INSN_RESCAN);
1755       compute_regsets (&eliminable_regset, &no_global_alloc_regs);
1756       build_insn_chain ();
1757       df_set_flags (DF_NO_INSN_RESCAN);
1758       failure = reload (get_insns (), 0);
1759     }
1760
1761   if (dump_enabled_p (pass_global_alloc.pass.static_pass_number))
1762     {
1763       timevar_push (TV_DUMP);
1764       dump_global_regs (dump_file);
1765       timevar_pop (TV_DUMP);
1766     }
1767
1768   /* FIXME: This appears on the surface to be wrong thing to be doing.
1769      So much of the compiler is designed to check reload_completed to
1770      see if it is running after reload that seems doomed to failure.
1771      We should be returning a value that says that we have found
1772      errors so that nothing but the cleanup passes are run
1773      afterwards.  */
1774   gcc_assert (reload_completed || failure);
1775   reload_completed = !failure;
1776
1777   /* The world has changed so much that at this point we might as well
1778      just rescan everything.  Note that df_rescan_all_insns is not
1779      going to help here because it does not touch the artificial uses
1780      and defs.  */
1781   df_finish_pass (true);
1782   if (optimize > 1)
1783     df_live_add_problem ();
1784   df_scan_alloc (NULL);
1785   df_scan_blocks ();
1786
1787   if (optimize)
1788     df_analyze ();
1789
1790   regstat_free_n_sets_and_refs ();
1791   regstat_free_ri ();
1792   return 0;
1793 }
1794
1795 struct rtl_opt_pass pass_global_alloc =
1796 {
1797  {
1798   RTL_PASS,
1799   "greg",                               /* name */
1800   NULL,                                 /* gate */
1801   rest_of_handle_global_alloc,          /* execute */
1802   NULL,                                 /* sub */
1803   NULL,                                 /* next */
1804   0,                                    /* static_pass_number */
1805   TV_GLOBAL_ALLOC,                      /* tv_id */
1806   0,                                    /* properties_required */
1807   0,                                    /* properties_provided */
1808   0,                                    /* properties_destroyed */
1809   0,                                    /* todo_flags_start */
1810   TODO_dump_func | TODO_verify_rtl_sharing
1811   | TODO_ggc_collect                    /* todo_flags_finish */
1812  }
1813 };
1814