2008-07-14 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / global.c
blob5ac45a32c926fa529c0b3aaae4636a646418250c
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.
6 This file is part of GCC.
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.
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.
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/>. */
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"
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.
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.
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.
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):
73 1) BITNUM = f(HIGH) + LOW, where
74 f(HIGH) = (HIGH * (HIGH - 1)) / 2
76 2) BITNUM = f(LOW) + HIGH, where
77 f(LOW) = LOW * (max_allocno - LOW) + (LOW * (LOW - 1)) / 2 - LOW - 1
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.
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.
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.
95 4. For each basic block, walk backward through the block, recording
96 the preferred hardware registers for each pseudo-register.
98 5. Sort a table of the allocnos into order of desirability of the variables.
100 6. Allocate the variables in that order; each if possible into
101 a preferred register, else into another register. */
103 /* A vector of the integers from 0 to max_allocno-1,
104 sorted in the order of first-to-be-allocated first. */
106 static int *allocno_order;
108 /* Set of registers that global-alloc isn't supposed to use. */
110 static HARD_REG_SET no_global_alloc_regs;
112 /* Set of registers used so far. */
114 static HARD_REG_SET regs_used_so_far;
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. */
119 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
121 /* Frequency of uses of given hard reg. */
122 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
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. */
127 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
129 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
130 element I, and hard register number J. */
132 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
134 /* Return true if *LOC contains an asm. */
136 static int
137 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
139 if ( !*loc)
140 return 0;
141 if (GET_CODE (*loc) == ASM_OPERANDS)
142 return 1;
143 return 0;
147 /* Return true if INSN contains an ASM. */
149 static int
150 insn_contains_asm (rtx insn)
152 return for_each_rtx (&insn, insn_contains_asm_1, NULL);
156 static void
157 compute_regs_asm_clobbered (char *regs_asm_clobbered)
159 basic_block bb;
161 memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
163 FOR_EACH_BB (bb)
165 rtx insn;
166 FOR_BB_INSNS_REVERSE (bb, insn)
168 struct df_ref **def_rec;
169 if (insn_contains_asm (insn))
170 for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
172 struct df_ref *def = *def_rec;
173 unsigned int dregno = DF_REF_REGNO (def);
174 if (dregno < FIRST_PSEUDO_REGISTER)
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;
189 /* All registers that can be eliminated. */
191 HARD_REG_SET eliminable_regset;
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 *);
202 /* Look through the list of eliminable registers. Set ELIM_SET to the
203 set of registers which may be eliminated. Set NO_GLOBAL_SET to the
204 set of registers which may not be used across blocks.
206 This will normally be called with ELIM_SET as the file static
207 variable eliminable_regset, and NO_GLOBAL_SET as the file static
208 variable NO_GLOBAL_ALLOC_REGS.
210 It also initializes global flag frame_pointer_needed. */
212 static void
213 compute_regsets (HARD_REG_SET *elim_set,
214 HARD_REG_SET *no_global_set)
217 /* Like regs_ever_live, but 1 if a reg is set or clobbered from an asm.
218 Unlike regs_ever_live, elements of this array corresponding to
219 eliminable regs like the frame pointer are set if an asm sets them. */
220 char *regs_asm_clobbered = XALLOCAVEC (char, FIRST_PSEUDO_REGISTER);
222 #ifdef ELIMINABLE_REGS
223 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
224 size_t i;
225 #endif
227 /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
228 sp for alloca. So we can't eliminate the frame pointer in that
229 case. At some point, we should improve this by emitting the
230 sp-adjusting insns for this case. */
231 int need_fp
232 = (! flag_omit_frame_pointer
233 || (cfun->calls_alloca && EXIT_IGNORE_STACK)
234 || crtl->accesses_prior_frames
235 || FRAME_POINTER_REQUIRED);
237 frame_pointer_needed = need_fp;
239 max_regno = max_reg_num ();
240 compact_blocks ();
242 max_allocno = 0;
244 /* A machine may have certain hard registers that
245 are safe to use only within a basic block. */
247 CLEAR_HARD_REG_SET (*no_global_set);
248 CLEAR_HARD_REG_SET (*elim_set);
250 compute_regs_asm_clobbered (regs_asm_clobbered);
251 /* Build the regset of all eliminable registers and show we can't use those
252 that we already know won't be eliminated. */
253 #ifdef ELIMINABLE_REGS
254 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
256 bool cannot_elim
257 = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
258 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
260 if (!regs_asm_clobbered[eliminables[i].from])
262 SET_HARD_REG_BIT (*elim_set, eliminables[i].from);
264 if (cannot_elim)
265 SET_HARD_REG_BIT (*no_global_set, eliminables[i].from);
267 else if (cannot_elim)
268 error ("%s cannot be used in asm here",
269 reg_names[eliminables[i].from]);
270 else
271 df_set_regs_ever_live (eliminables[i].from, true);
273 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
274 if (!regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
276 SET_HARD_REG_BIT (*elim_set, HARD_FRAME_POINTER_REGNUM);
277 if (need_fp)
278 SET_HARD_REG_BIT (*no_global_set, HARD_FRAME_POINTER_REGNUM);
280 else if (need_fp)
281 error ("%s cannot be used in asm here",
282 reg_names[HARD_FRAME_POINTER_REGNUM]);
283 else
284 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
285 #endif
287 #else
288 if (!regs_asm_clobbered[FRAME_POINTER_REGNUM])
290 SET_HARD_REG_BIT (*elim_set, FRAME_POINTER_REGNUM);
291 if (need_fp)
292 SET_HARD_REG_BIT (*no_global_set, FRAME_POINTER_REGNUM);
294 else if (need_fp)
295 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
296 else
297 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
298 #endif
301 /* Perform allocation of pseudo-registers not allocated by local_alloc.
303 Return value is nonzero if reload failed
304 and we must not do any more for this function. */
306 static int
307 global_alloc (void)
309 int retval;
310 size_t i;
311 int max_blk;
312 int *num_allocnos_per_blk;
314 compute_regsets (&eliminable_regset, &no_global_alloc_regs);
316 /* Track which registers have already been used. Start with registers
317 explicitly in the rtl, then registers allocated by local register
318 allocation. */
320 CLEAR_HARD_REG_SET (regs_used_so_far);
321 #ifdef LEAF_REGISTERS
322 /* If we are doing the leaf function optimization, and this is a leaf
323 function, it means that the registers that take work to save are those
324 that need a register window. So prefer the ones that can be used in
325 a leaf function. */
327 const char *cheap_regs;
328 const char *const leaf_regs = LEAF_REGISTERS;
330 if (only_leaf_regs_used () && leaf_function_p ())
331 cheap_regs = leaf_regs;
332 else
333 cheap_regs = call_used_regs;
334 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
335 if (df_regs_ever_live_p (i) || cheap_regs[i])
336 SET_HARD_REG_BIT (regs_used_so_far, i);
338 #else
339 /* We consider registers that do not have to be saved over calls as if
340 they were already used since there is no cost in using them. */
341 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
342 if (df_regs_ever_live_p (i) || call_used_regs[i])
343 SET_HARD_REG_BIT (regs_used_so_far, i);
344 #endif
346 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
347 if (reg_renumber[i] >= 0)
348 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
350 /* Establish mappings from register number to allocation number
351 and vice versa. In the process, count the allocnos. */
353 reg_allocno = XNEWVEC (int, max_regno);
355 /* Initially fill the reg_allocno array with regno's... */
356 max_blk = 0;
357 max_allocno = 0;
358 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
359 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
360 that we are supposed to refrain from putting in a hard reg.
361 -2 means do make an allocno but don't allocate it. */
362 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
363 /* Don't allocate pseudos that cross calls,
364 if this function receives a nonlocal goto. */
365 && (! cfun->has_nonlocal_label
366 || REG_N_CALLS_CROSSED (i) == 0))
368 int blk = regno_basic_block (i);
369 reg_allocno[max_allocno++] = i;
370 if (blk > max_blk)
371 max_blk = blk;
372 gcc_assert (REG_LIVE_LENGTH (i));
375 allocno = XCNEWVEC (struct allocno, max_allocno);
376 partial_bitnum = XNEWVEC (HOST_WIDE_INT, max_allocno);
377 num_allocnos_per_blk = XCNEWVEC (int, max_blk + 1);
379 /* ...so we can sort them in the order we want them to receive
380 their allocnos. */
381 qsort (reg_allocno, max_allocno, sizeof (int), regno_compare);
383 for (i = 0; i < (size_t) max_allocno; i++)
385 int regno = reg_allocno[i];
386 int blk = regno_basic_block (regno);
387 num_allocnos_per_blk[blk]++;
388 allocno[i].reg = regno;
389 allocno[i].size = PSEUDO_REGNO_SIZE (regno);
390 allocno[i].calls_crossed += REG_N_CALLS_CROSSED (regno);
391 allocno[i].freq_calls_crossed += REG_FREQ_CALLS_CROSSED (regno);
392 allocno[i].throwing_calls_crossed
393 += REG_N_THROWING_CALLS_CROSSED (regno);
394 allocno[i].n_refs += REG_N_REFS (regno);
395 allocno[i].freq += REG_FREQ (regno);
396 if (allocno[i].live_length < REG_LIVE_LENGTH (regno))
397 allocno[i].live_length = REG_LIVE_LENGTH (regno);
400 /* The "global" block must contain all allocnos. */
401 num_allocnos_per_blk[0] = max_allocno;
403 /* Now reinitialize the reg_allocno array in terms of the
404 optimized regno to allocno mapping we created above. */
405 for (i = 0; i < (size_t) max_regno; i++)
406 reg_allocno[i] = -1;
408 max_bitnum = 0;
409 for (i = 0; i < (size_t) max_allocno; i++)
411 int regno = allocno[i].reg;
412 int blk = regno_basic_block (regno);
413 int row_size = --num_allocnos_per_blk[blk];
414 reg_allocno[regno] = (int) i;
415 partial_bitnum[i] = (row_size > 0) ? max_bitnum - ((int) i + 1) : -1;
416 max_bitnum += row_size;
419 #ifdef ENABLE_CHECKING
420 gcc_assert (max_bitnum <=
421 (((HOST_WIDE_INT) max_allocno *
422 ((HOST_WIDE_INT) max_allocno - 1)) / 2));
423 #endif
425 if (dump_file)
427 HOST_WIDE_INT num_bits, num_bytes, actual_bytes;
429 fprintf (dump_file, "## max_blk: %d\n", max_blk);
430 fprintf (dump_file, "## max_regno: %d\n", max_regno);
431 fprintf (dump_file, "## max_allocno: %d\n", max_allocno);
433 num_bits = max_bitnum;
434 num_bytes = CEIL (num_bits, 8);
435 actual_bytes = num_bytes;
436 fprintf (dump_file, "## Compressed triangular bitmatrix size: ");
437 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
438 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes\n", num_bytes);
440 num_bits = ((HOST_WIDE_INT) max_allocno *
441 ((HOST_WIDE_INT) max_allocno - 1)) / 2;
442 num_bytes = CEIL (num_bits, 8);
443 fprintf (dump_file, "## Standard triangular bitmatrix size: ");
444 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
445 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes [%.2f%%]\n",
446 num_bytes, 100.0 * ((double) actual_bytes / (double) num_bytes));
448 num_bits = (HOST_WIDE_INT) max_allocno * (HOST_WIDE_INT) max_allocno;
449 num_bytes = CEIL (num_bits, 8);
450 fprintf (dump_file, "## Square bitmatrix size: ");
451 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bits, ", num_bits);
452 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC " bytes [%.2f%%]\n",
453 num_bytes, 100.0 * ((double) actual_bytes / (double) num_bytes));
456 /* Calculate amount of usage of each hard reg by pseudos
457 allocated by local-alloc. This is to see if we want to
458 override it. */
459 memset (local_reg_live_length, 0, sizeof local_reg_live_length);
460 memset (local_reg_n_refs, 0, sizeof local_reg_n_refs);
461 memset (local_reg_freq, 0, sizeof local_reg_freq);
462 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
463 if (reg_renumber[i] >= 0)
465 int regno = reg_renumber[i];
466 int endregno = end_hard_regno (PSEUDO_REGNO_MODE (i), regno);
467 int j;
469 for (j = regno; j < endregno; j++)
471 local_reg_n_refs[j] += REG_N_REFS (i);
472 local_reg_freq[j] += REG_FREQ (i);
473 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
477 /* We can't override local-alloc for a reg used not just by local-alloc. */
478 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
479 if (df_regs_ever_live_p (i))
480 local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
482 if (dump_file)
484 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
486 fprintf (dump_file, "%d REG_N_REFS=%d, REG_FREQ=%d, REG_LIVE_LENGTH=%d\n",
487 (int)i, REG_N_REFS (i), REG_FREQ (i), REG_LIVE_LENGTH (i));
489 fprintf (dump_file, "regs_ever_live =");
490 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
491 if (df_regs_ever_live_p (i))
492 fprintf (dump_file, " %d", (int)i);
493 fprintf (dump_file, "\n");
496 conflicts = NULL;
497 adjacency = NULL;
498 adjacency_pool = NULL;
500 /* If there is work to be done (at least one reg to allocate),
501 perform global conflict analysis and allocate the regs. */
503 if (max_allocno > 0)
505 /* We used to use alloca here, but the size of what it would try to
506 allocate would occasionally cause it to exceed the stack limit and
507 cause unpredictable core dumps. Some examples were > 2Mb in size. */
508 conflicts = XCNEWVEC (HOST_WIDEST_FAST_INT,
509 CEIL(max_bitnum, HOST_BITS_PER_WIDEST_FAST_INT));
511 adjacency = XCNEWVEC (adjacency_t *, max_allocno);
512 adjacency_pool = create_alloc_pool ("global_alloc adjacency list pool",
513 sizeof (adjacency_t), 1024);
515 /* Scan all the insns and compute the conflicts among allocnos
516 and between allocnos and hard regs. */
518 global_conflicts ();
520 /* There is just too much going on in the register allocators to
521 keep things up to date. At the end we have to rescan anyway
522 because things change when the reload_completed flag is set.
523 So we just turn off scanning and we will rescan by hand.
525 However, we needed to do the rescanning before this point to
526 get the new insns scanned inserted by local_alloc scanned for
527 global_conflicts. */
528 df_set_flags (DF_NO_INSN_RESCAN);
530 /* Eliminate conflicts between pseudos and eliminable registers. If
531 the register is not eliminated, the pseudo won't really be able to
532 live in the eliminable register, so the conflict doesn't matter.
533 If we do eliminate the register, the conflict will no longer exist.
534 So in either case, we can ignore the conflict. Likewise for
535 preferences. */
537 set_preferences ();
539 for (i = 0; i < (size_t) max_allocno; i++)
541 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
542 eliminable_regset);
543 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
544 eliminable_regset);
545 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
546 eliminable_regset);
549 /* Try to expand the preferences by merging them between allocnos. */
551 expand_preferences ();
553 /* Determine the order to allocate the remaining pseudo registers. */
555 allocno_order = XNEWVEC (int, max_allocno);
556 for (i = 0; i < (size_t) max_allocno; i++)
557 allocno_order[i] = i;
559 /* Default the size to 1, since allocno_compare uses it to divide by.
560 Also convert allocno_live_length of zero to -1. A length of zero
561 can occur when all the registers for that allocno have reg_live_length
562 equal to -2. In this case, we want to make an allocno, but not
563 allocate it. So avoid the divide-by-zero and set it to a low
564 priority. */
566 for (i = 0; i < (size_t) max_allocno; i++)
568 if (allocno[i].size == 0)
569 allocno[i].size = 1;
570 if (allocno[i].live_length == 0)
571 allocno[i].live_length = -1;
574 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
576 prune_preferences ();
578 if (dump_file)
579 dump_conflicts (dump_file);
581 /* Try allocating them, one by one, in that order,
582 except for parameters marked with reg_live_length[regno] == -2. */
584 for (i = 0; i < (size_t) max_allocno; i++)
585 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
586 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
588 if (!dbg_cnt (global_alloc_at_reg))
589 break;
590 /* If we have more than one register class,
591 first try allocating in the class that is cheapest
592 for this pseudo-reg. If that fails, try any reg. */
593 if (N_REG_CLASSES > 1)
595 find_reg (allocno_order[i], 0, 0, 0, 0);
596 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
598 if (dump_file)
599 fprintf (dump_file, "Assign %d to %d\n",
600 reg_renumber[allocno[allocno_order[i]].reg],
601 allocno[allocno_order[i]].reg);
602 continue;
605 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
607 find_reg (allocno_order[i], 0, 1, 0, 0);
608 if (dump_file)
609 fprintf (dump_file, "Assign %d to %d\n",
610 reg_renumber[allocno[allocno_order[i]].reg],
611 allocno[allocno_order[i]].reg);
615 free (allocno_order);
616 free (conflicts);
619 /* Do the reloads now while the allocno data still exists, so that we can
620 try to assign new hard regs to any pseudo regs that are spilled. */
622 #if 0 /* We need to eliminate regs even if there is no rtl code,
623 for the sake of debugging information. */
624 if (n_basic_blocks > NUM_FIXED_BLOCKS)
625 #endif
627 build_insn_chain ();
628 retval = reload (get_insns (), 1);
631 /* Clean up. */
632 free (reg_allocno);
633 free (num_allocnos_per_blk);
634 free (partial_bitnum);
635 free (allocno);
636 if (adjacency != NULL)
638 free_alloc_pool (adjacency_pool);
639 free (adjacency);
642 return retval;
645 /* Sort predicate for ordering the regnos. We want the regno to allocno
646 mapping to have the property that all "global" regnos (ie, regnos that
647 are referenced in more than one basic block) have smaller allocno values
648 than "local" regnos (ie, regnos referenced in only one basic block).
649 In addition, for two basic blocks "i" and "j" with i < j, all regnos
650 local to basic block i should have smaller allocno values than regnos
651 local to basic block j.
652 Returns -1 (1) if *v1p should be allocated before (after) *v2p. */
654 static int
655 regno_compare (const void *v1p, const void *v2p)
657 int regno1 = *(const int *)v1p;
658 int regno2 = *(const int *)v2p;
659 int blk1 = REG_BASIC_BLOCK (regno1);
660 int blk2 = REG_BASIC_BLOCK (regno2);
662 /* Prefer lower numbered basic blocks. Note that global and unknown
663 blocks have negative values, giving them high precedence. */
664 if (blk1 - blk2)
665 return blk1 - blk2;
667 /* If both regs are referenced from the same block, sort by regno. */
668 return regno1 - regno2;
671 /* Sort predicate for ordering the allocnos.
672 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
674 static int
675 allocno_compare (const void *v1p, const void *v2p)
677 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
678 /* Note that the quotient will never be bigger than
679 the value of floor_log2 times the maximum number of
680 times a register can occur in one insn (surely less than 100)
681 weighted by the frequency (maximally REG_FREQ_MAX).
682 Multiplying this by 10000/REG_FREQ_MAX can't overflow. */
683 int pri1
684 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
685 / allocno[v1].live_length)
686 * (10000 / REG_FREQ_MAX) * allocno[v1].size);
687 int pri2
688 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
689 / allocno[v2].live_length)
690 * (10000 / REG_FREQ_MAX) * allocno[v2].size);
691 if (pri2 - pri1)
692 return pri2 - pri1;
694 /* If regs are equally good, sort by allocno,
695 so that the results of qsort leave nothing to chance. */
696 return v1 - v2;
699 /* Expand the preference information by looking for cases where one allocno
700 dies in an insn that sets an allocno. If those two allocnos don't conflict,
701 merge any preferences between those allocnos. */
703 static void
704 expand_preferences (void)
706 rtx insn;
707 rtx link;
708 rtx set;
710 /* We only try to handle the most common cases here. Most of the cases
711 where this wins are reg-reg copies. */
713 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
714 if (INSN_P (insn)
715 && (set = single_set (insn)) != 0
716 && REG_P (SET_DEST (set))
717 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
718 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
719 if (REG_NOTE_KIND (link) == REG_DEAD
720 && REG_P (XEXP (link, 0))
721 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
722 && ! conflict_p (reg_allocno[REGNO (SET_DEST (set))],
723 reg_allocno[REGNO (XEXP (link, 0))]))
725 int a1 = reg_allocno[REGNO (SET_DEST (set))];
726 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
728 if (XEXP (link, 0) == SET_SRC (set))
730 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
731 allocno[a2].hard_reg_copy_preferences);
732 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
733 allocno[a1].hard_reg_copy_preferences);
736 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
737 allocno[a2].hard_reg_preferences);
738 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
739 allocno[a1].hard_reg_preferences);
740 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
741 allocno[a2].hard_reg_full_preferences);
742 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
743 allocno[a1].hard_reg_full_preferences);
748 /* Try to set a preference for an allocno to a hard register.
749 We are passed DEST and SRC which are the operands of a SET. It is known
750 that SRC is a register. If SRC or the first operand of SRC is a register,
751 try to set a preference. If one of the two is a hard register and the other
752 is a pseudo-register, mark the preference.
754 Note that we are not as aggressive as local-alloc in trying to tie a
755 pseudo-register to a hard register. */
757 static void
758 set_preference (rtx dest, rtx src)
760 unsigned int src_regno, dest_regno, end_regno;
761 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
762 to compensate for subregs in SRC or DEST. */
763 int offset = 0;
764 unsigned int i;
765 int copy = 1;
767 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
768 src = XEXP (src, 0), copy = 0;
770 /* Get the reg number for both SRC and DEST.
771 If neither is a reg, give up. */
773 if (REG_P (src))
774 src_regno = REGNO (src);
775 else if (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src)))
777 src_regno = REGNO (SUBREG_REG (src));
779 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
780 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
781 GET_MODE (SUBREG_REG (src)),
782 SUBREG_BYTE (src),
783 GET_MODE (src));
784 else
785 offset += (SUBREG_BYTE (src)
786 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
788 else
789 return;
791 if (REG_P (dest))
792 dest_regno = REGNO (dest);
793 else if (GET_CODE (dest) == SUBREG && REG_P (SUBREG_REG (dest)))
795 dest_regno = REGNO (SUBREG_REG (dest));
797 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
798 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
799 GET_MODE (SUBREG_REG (dest)),
800 SUBREG_BYTE (dest),
801 GET_MODE (dest));
802 else
803 offset -= (SUBREG_BYTE (dest)
804 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
806 else
807 return;
809 /* Convert either or both to hard reg numbers. */
811 if (reg_renumber[src_regno] >= 0)
812 src_regno = reg_renumber[src_regno];
814 if (reg_renumber[dest_regno] >= 0)
815 dest_regno = reg_renumber[dest_regno];
817 /* Now if one is a hard reg and the other is a global pseudo
818 then give the other a preference. */
820 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
821 && reg_allocno[src_regno] >= 0)
823 dest_regno -= offset;
824 if (dest_regno < FIRST_PSEUDO_REGISTER)
826 if (copy)
827 SET_REGBIT (hard_reg_copy_preferences,
828 reg_allocno[src_regno], dest_regno);
830 SET_REGBIT (hard_reg_preferences,
831 reg_allocno[src_regno], dest_regno);
832 end_regno = end_hard_regno (GET_MODE (dest), dest_regno);
833 for (i = dest_regno; i < end_regno; i++)
834 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
838 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
839 && reg_allocno[dest_regno] >= 0)
841 src_regno += offset;
842 if (src_regno < FIRST_PSEUDO_REGISTER)
844 if (copy)
845 SET_REGBIT (hard_reg_copy_preferences,
846 reg_allocno[dest_regno], src_regno);
848 SET_REGBIT (hard_reg_preferences,
849 reg_allocno[dest_regno], src_regno);
850 end_regno = end_hard_regno (GET_MODE (src), src_regno);
851 for (i = src_regno; i < end_regno; i++)
852 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
857 /* Helper function for set_preferences. */
858 static void
859 set_preferences_1 (rtx reg, const_rtx setter, void *data ATTRIBUTE_UNUSED)
861 if (GET_CODE (reg) == SUBREG)
862 reg = SUBREG_REG (reg);
864 if (!REG_P (reg))
865 return;
867 gcc_assert (setter);
868 if (GET_CODE (setter) != CLOBBER)
869 set_preference (reg, SET_SRC (setter));
872 /* Scan all of the insns and initialize the preferences. */
874 static void
875 set_preferences (void)
877 basic_block bb;
878 rtx insn;
879 FOR_EACH_BB (bb)
880 FOR_BB_INSNS_REVERSE (bb, insn)
882 if (!INSN_P (insn))
883 continue;
885 note_stores (PATTERN (insn), set_preferences_1, NULL);
891 /* Prune the preferences for global registers to exclude registers that cannot
892 be used.
894 Compute `regs_someone_prefers', which is a bitmask of the hard registers
895 that are preferred by conflicting registers of lower priority. If possible,
896 we will avoid using these registers. */
898 static void
899 prune_preferences (void)
901 int i;
902 int num;
903 int *allocno_to_order = XNEWVEC (int, max_allocno);
905 /* Scan least most important to most important.
906 For each allocno, remove from preferences registers that cannot be used,
907 either because of conflicts or register type. Then compute all registers
908 preferred by each lower-priority register that conflicts. */
910 for (i = max_allocno - 1; i >= 0; i--)
912 HARD_REG_SET temp;
914 num = allocno_order[i];
915 allocno_to_order[num] = i;
916 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
918 if (allocno[num].calls_crossed == 0)
919 IOR_HARD_REG_SET (temp, fixed_reg_set);
920 else
921 IOR_HARD_REG_SET (temp, call_used_reg_set);
923 IOR_COMPL_HARD_REG_SET
924 (temp,
925 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
927 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
928 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
929 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
932 for (i = max_allocno - 1; i >= 0; i--)
934 /* Merge in the preferences of lower-priority registers (they have
935 already been pruned). If we also prefer some of those registers,
936 don't exclude them unless we are of a smaller size (in which case
937 we want to give the lower-priority allocno the first chance for
938 these registers). */
939 HARD_REG_SET temp, temp2;
940 int allocno2;
941 adjacency_iter ai;
943 num = allocno_order[i];
945 CLEAR_HARD_REG_SET (temp);
946 CLEAR_HARD_REG_SET (temp2);
948 FOR_EACH_CONFLICT (num, allocno2, ai)
950 if (allocno_to_order[allocno2] > i)
952 if (allocno[allocno2].size <= allocno[num].size)
953 IOR_HARD_REG_SET (temp,
954 allocno[allocno2].hard_reg_full_preferences);
955 else
956 IOR_HARD_REG_SET (temp2,
957 allocno[allocno2].hard_reg_full_preferences);
961 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
962 IOR_HARD_REG_SET (temp, temp2);
963 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
965 free (allocno_to_order);
968 /* Assign a hard register to allocno NUM; look for one that is the beginning
969 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
970 The registers marked in PREFREGS are tried first.
972 LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
973 be used for this allocation.
975 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
976 Otherwise ignore that preferred class and use the alternate class.
978 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
979 will have to be saved and restored at calls.
981 RETRYING is nonzero if this is called from retry_global_alloc.
983 If we find one, record it in reg_renumber.
984 If not, do nothing. */
986 static void
987 find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
989 int i, best_reg, pass;
990 HARD_REG_SET used, used1, used2;
992 enum reg_class class = (alt_regs_p
993 ? reg_alternate_class (allocno[num].reg)
994 : reg_preferred_class (allocno[num].reg));
995 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
997 if (accept_call_clobbered)
998 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
999 else if (allocno[num].calls_crossed == 0)
1000 COPY_HARD_REG_SET (used1, fixed_reg_set);
1001 else
1002 COPY_HARD_REG_SET (used1, call_used_reg_set);
1004 /* Some registers should not be allocated in global-alloc. */
1005 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1006 if (losers)
1007 IOR_HARD_REG_SET (used1, losers);
1009 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1011 #ifdef EH_RETURN_DATA_REGNO
1012 if (allocno[num].no_eh_reg)
1014 unsigned int j;
1015 for (j = 0; ; ++j)
1017 unsigned int regno = EH_RETURN_DATA_REGNO (j);
1018 if (regno == INVALID_REGNUM)
1019 break;
1020 SET_HARD_REG_BIT (used1, regno);
1023 #endif
1025 COPY_HARD_REG_SET (used2, used1);
1027 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1029 #ifdef CANNOT_CHANGE_MODE_CLASS
1030 cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1031 #endif
1033 /* Try each hard reg to see if it fits. Do this in two passes.
1034 In the first pass, skip registers that are preferred by some other pseudo
1035 to give it a better chance of getting one of those registers. Only if
1036 we can't get a register when excluding those do we take one of them.
1037 However, we never allocate a register for the first time in pass 0. */
1039 COPY_HARD_REG_SET (used, used1);
1040 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1041 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1043 best_reg = -1;
1044 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1045 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1046 pass++)
1048 if (pass == 1)
1049 COPY_HARD_REG_SET (used, used1);
1050 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1052 #ifdef REG_ALLOC_ORDER
1053 int regno = reg_alloc_order[i];
1054 #else
1055 int regno = i;
1056 #endif
1057 if (! TEST_HARD_REG_BIT (used, regno)
1058 && HARD_REGNO_MODE_OK (regno, mode)
1059 && (allocno[num].calls_crossed == 0
1060 || accept_call_clobbered
1061 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1063 int j;
1064 int lim = end_hard_regno (mode, regno);
1065 for (j = regno + 1;
1066 (j < lim
1067 && ! TEST_HARD_REG_BIT (used, j));
1068 j++);
1069 if (j == lim)
1071 best_reg = regno;
1072 break;
1074 #ifndef REG_ALLOC_ORDER
1075 i = j; /* Skip starting points we know will lose */
1076 #endif
1081 /* See if there is a preferred register with the same class as the register
1082 we allocated above. Making this restriction prevents register
1083 preferencing from creating worse register allocation.
1085 Remove from the preferred registers and conflicting registers. Note that
1086 additional conflicts may have been added after `prune_preferences' was
1087 called.
1089 First do this for those register with copy preferences, then all
1090 preferred registers. */
1092 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1093 if (!hard_reg_set_empty_p (allocno[num].hard_reg_copy_preferences)
1094 && best_reg >= 0)
1096 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1097 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1098 && HARD_REGNO_MODE_OK (i, mode)
1099 && (allocno[num].calls_crossed == 0
1100 || accept_call_clobbered
1101 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1102 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1103 || reg_class_subset_p (REGNO_REG_CLASS (i),
1104 REGNO_REG_CLASS (best_reg))
1105 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1106 REGNO_REG_CLASS (i))))
1108 int j;
1109 int lim = end_hard_regno (mode, i);
1110 for (j = i + 1;
1111 (j < lim
1112 && ! TEST_HARD_REG_BIT (used, j)
1113 && (REGNO_REG_CLASS (j)
1114 == REGNO_REG_CLASS (best_reg + (j - i))
1115 || reg_class_subset_p (REGNO_REG_CLASS (j),
1116 REGNO_REG_CLASS (best_reg + (j - i)))
1117 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1118 REGNO_REG_CLASS (j))));
1119 j++);
1120 if (j == lim)
1122 best_reg = i;
1123 goto no_prefs;
1128 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1129 if (!hard_reg_set_empty_p (allocno[num].hard_reg_preferences)
1130 && best_reg >= 0)
1132 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1133 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1134 && HARD_REGNO_MODE_OK (i, mode)
1135 && (allocno[num].calls_crossed == 0
1136 || accept_call_clobbered
1137 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1138 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1139 || reg_class_subset_p (REGNO_REG_CLASS (i),
1140 REGNO_REG_CLASS (best_reg))
1141 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1142 REGNO_REG_CLASS (i))))
1144 int j;
1145 int lim = end_hard_regno (mode, i);
1146 for (j = i + 1;
1147 (j < lim
1148 && ! TEST_HARD_REG_BIT (used, j)
1149 && (REGNO_REG_CLASS (j)
1150 == REGNO_REG_CLASS (best_reg + (j - i))
1151 || reg_class_subset_p (REGNO_REG_CLASS (j),
1152 REGNO_REG_CLASS (best_reg + (j - i)))
1153 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1154 REGNO_REG_CLASS (j))));
1155 j++);
1156 if (j == lim)
1158 best_reg = i;
1159 break;
1163 no_prefs:
1165 /* If we haven't succeeded yet, try with caller-saves.
1166 We need not check to see if the current function has nonlocal
1167 labels because we don't put any pseudos that are live over calls in
1168 registers in that case. */
1170 if (flag_caller_saves && best_reg < 0)
1172 /* Did not find a register. If it would be profitable to
1173 allocate a call-clobbered register and save and restore it
1174 around calls, do that. Don't do this if it crosses any calls
1175 that might throw. */
1176 if (! accept_call_clobbered
1177 && allocno[num].calls_crossed != 0
1178 && allocno[num].throwing_calls_crossed == 0
1179 && CALLER_SAVE_PROFITABLE (optimize_size ? allocno[num].n_refs : allocno[num].freq,
1180 optimize_size ? allocno[num].calls_crossed
1181 : allocno[num].freq_calls_crossed))
1183 HARD_REG_SET new_losers;
1184 if (! losers)
1185 CLEAR_HARD_REG_SET (new_losers);
1186 else
1187 COPY_HARD_REG_SET (new_losers, losers);
1189 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1190 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1191 if (reg_renumber[allocno[num].reg] >= 0)
1193 caller_save_needed = 1;
1194 return;
1199 /* If we haven't succeeded yet,
1200 see if some hard reg that conflicts with us
1201 was utilized poorly by local-alloc.
1202 If so, kick out the regs that were put there by local-alloc
1203 so we can use it instead. */
1204 if (best_reg < 0 && !retrying
1205 /* Let's not bother with multi-reg allocnos. */
1206 && allocno[num].size == 1
1207 && REG_BASIC_BLOCK (allocno[num].reg) == REG_BLOCK_GLOBAL)
1209 /* Count from the end, to find the least-used ones first. */
1210 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1212 #ifdef REG_ALLOC_ORDER
1213 int regno = reg_alloc_order[i];
1214 #else
1215 int regno = i;
1216 #endif
1218 if (local_reg_n_refs[regno] != 0
1219 /* Don't use a reg no good for this pseudo. */
1220 && ! TEST_HARD_REG_BIT (used2, regno)
1221 && HARD_REGNO_MODE_OK (regno, mode)
1222 /* The code below assumes that we need only a single
1223 register, but the check of allocno[num].size above
1224 was not enough. Sometimes we need more than one
1225 register for a single-word value. */
1226 && hard_regno_nregs[regno][mode] == 1
1227 && (allocno[num].calls_crossed == 0
1228 || accept_call_clobbered
1229 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1230 #ifdef CANNOT_CHANGE_MODE_CLASS
1231 && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1232 mode)
1233 #endif
1234 #ifdef STACK_REGS
1235 && (!allocno[num].no_stack_reg
1236 || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
1237 #endif
1240 /* We explicitly evaluate the divide results into temporary
1241 variables so as to avoid excess precision problems that occur
1242 on an i386-unknown-sysv4.2 (unixware) host. */
1244 double tmp1 = ((double) local_reg_freq[regno] * local_reg_n_refs[regno]
1245 / local_reg_live_length[regno]);
1246 double tmp2 = ((double) allocno[num].freq * allocno[num].n_refs
1247 / allocno[num].live_length);
1249 if (tmp1 < tmp2)
1251 /* Hard reg REGNO was used less in total by local regs
1252 than it would be used by this one allocno! */
1253 int k;
1254 if (dump_file)
1256 fprintf (dump_file, "Regno %d better for global %d, ",
1257 regno, allocno[num].reg);
1258 fprintf (dump_file, "fr:%d, ll:%d, nr:%d ",
1259 allocno[num].freq, allocno[num].live_length,
1260 allocno[num].n_refs);
1261 fprintf (dump_file, "(was: fr:%d, ll:%d, nr:%d)\n",
1262 local_reg_freq[regno],
1263 local_reg_live_length[regno],
1264 local_reg_n_refs[regno]);
1267 for (k = 0; k < max_regno; k++)
1268 if (reg_renumber[k] >= 0)
1270 int r = reg_renumber[k];
1271 int endregno
1272 = end_hard_regno (PSEUDO_REGNO_MODE (k), r);
1274 if (regno >= r && regno < endregno)
1276 if (dump_file)
1277 fprintf (dump_file,
1278 "Local Reg %d now on stack\n", k);
1279 reg_renumber[k] = -1;
1283 best_reg = regno;
1284 break;
1290 /* Did we find a register? */
1292 if (best_reg >= 0)
1294 int lim, j;
1295 HARD_REG_SET this_reg;
1296 adjacency_iter ai;
1298 /* Yes. Record it as the hard register of this pseudo-reg. */
1299 reg_renumber[allocno[num].reg] = best_reg;
1301 /* Make a set of the hard regs being allocated. */
1302 CLEAR_HARD_REG_SET (this_reg);
1303 lim = end_hard_regno (mode, best_reg);
1304 for (j = best_reg; j < lim; j++)
1306 SET_HARD_REG_BIT (this_reg, j);
1307 SET_HARD_REG_BIT (regs_used_so_far, j);
1308 /* This is no longer a reg used just by local regs. */
1309 local_reg_n_refs[j] = 0;
1310 local_reg_freq[j] = 0;
1312 /* For each other pseudo-reg conflicting with this one,
1313 mark it as conflicting with the hard regs this one occupies. */
1314 FOR_EACH_CONFLICT (num, j, ai)
1316 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1321 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1322 Perhaps it had previously seemed not worth a hard reg,
1323 or perhaps its old hard reg has been commandeered for reloads.
1324 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1325 they do not appear to be allocated.
1326 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1328 void
1329 retry_global_alloc (int regno, HARD_REG_SET forbidden_regs)
1331 int alloc_no = reg_allocno[regno];
1332 if (alloc_no >= 0)
1334 /* If we have more than one register class,
1335 first try allocating in the class that is cheapest
1336 for this pseudo-reg. If that fails, try any reg. */
1337 if (N_REG_CLASSES > 1)
1338 find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1339 if (reg_renumber[regno] < 0
1340 && reg_alternate_class (regno) != NO_REGS)
1341 find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1343 /* If we found a register, modify the RTL for the register to
1344 show the hard register, and mark that register live. */
1345 if (reg_renumber[regno] >= 0)
1347 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
1348 mark_home_live (regno);
1353 /* Indicate that hard register number FROM was eliminated and replaced with
1354 an offset from hard register number TO. The status of hard registers live
1355 at the start of a basic block is updated by replacing a use of FROM with
1356 a use of TO. */
1358 void
1359 mark_elimination (int from, int to)
1361 basic_block bb;
1363 FOR_EACH_BB (bb)
1365 /* We don't use LIVE info in IRA. */
1366 regset r = (flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb));
1367 if (REGNO_REG_SET_P (r, from))
1369 CLEAR_REGNO_REG_SET (r, from);
1370 SET_REGNO_REG_SET (r, to);
1375 /* Print chain C to FILE. */
1377 static void
1378 print_insn_chain (FILE *file, struct insn_chain *c)
1380 fprintf (file, "insn=%d, ", INSN_UID(c->insn));
1381 bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
1382 bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
1383 bitmap_print (file, &c->saved, "saved: ", "\n");
1387 /* Print all reload_insn_chains to FILE. */
1389 static void
1390 print_insn_chains (FILE *file)
1392 struct insn_chain *c;
1393 for (c = reload_insn_chain; c ; c = c->next)
1394 print_insn_chain (file, c);
1397 /* Return true if pseudo REGNO should be added to set live_throughout
1398 or dead_or_set of the insn chains for reload consideration. */
1400 static bool
1401 pseudo_for_reload_consideration_p (int regno)
1403 /* Consider spilled pseudos too for IRA because they still have a
1404 chance to get hard-registers in the reload when IRA is used. */
1405 return reg_renumber[regno] >= 0 || (flag_ira && optimize);
1408 /* Walk the insns of the current function and build reload_insn_chain,
1409 and record register life information. */
1411 void
1412 build_insn_chain (void)
1414 unsigned int i;
1415 struct insn_chain **p = &reload_insn_chain;
1416 basic_block bb;
1417 struct insn_chain *c = NULL;
1418 struct insn_chain *next = NULL;
1419 bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
1420 bitmap elim_regset = BITMAP_ALLOC (NULL);
1421 /* live_subregs is a vector used to keep accurate information about
1422 which hardregs are live in multiword pseudos. live_subregs and
1423 live_subregs_used are indexed by pseudo number. The live_subreg
1424 entry for a particular pseudo is only used if the corresponding
1425 element is non zero in live_subregs_used. The value in
1426 live_subregs_used is number of bytes that the pseudo can
1427 occupy. */
1428 sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
1429 int *live_subregs_used = XNEWVEC (int, max_regno);
1431 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1432 if (TEST_HARD_REG_BIT (eliminable_regset, i))
1433 bitmap_set_bit (elim_regset, i);
1434 FOR_EACH_BB_REVERSE (bb)
1436 bitmap_iterator bi;
1437 rtx insn;
1439 CLEAR_REG_SET (live_relevant_regs);
1440 memset (live_subregs_used, 0, max_regno * sizeof (int));
1442 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
1444 if (i >= FIRST_PSEUDO_REGISTER)
1445 break;
1446 bitmap_set_bit (live_relevant_regs, i);
1449 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), FIRST_PSEUDO_REGISTER, i, bi)
1451 if (pseudo_for_reload_consideration_p (i))
1452 bitmap_set_bit (live_relevant_regs, i);
1455 FOR_BB_INSNS_REVERSE (bb, insn)
1457 if (!NOTE_P (insn) && !BARRIER_P (insn))
1459 unsigned int uid = INSN_UID (insn);
1460 struct df_ref **def_rec;
1461 struct df_ref **use_rec;
1463 c = new_insn_chain ();
1464 c->next = next;
1465 next = c;
1466 *p = c;
1467 p = &c->prev;
1469 c->insn = insn;
1470 c->block = bb->index;
1472 if (INSN_P (insn))
1473 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
1475 struct df_ref *def = *def_rec;
1476 unsigned int regno = DF_REF_REGNO (def);
1478 /* Ignore may clobbers because these are generated
1479 from calls. However, every other kind of def is
1480 added to dead_or_set. */
1481 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1483 if (regno < FIRST_PSEUDO_REGISTER)
1485 if (!fixed_regs[regno])
1486 bitmap_set_bit (&c->dead_or_set, regno);
1488 else if (pseudo_for_reload_consideration_p (regno))
1489 bitmap_set_bit (&c->dead_or_set, regno);
1492 if ((regno < FIRST_PSEUDO_REGISTER
1493 || reg_renumber[regno] >= 0
1494 || (flag_ira && optimize))
1495 && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
1497 rtx reg = DF_REF_REG (def);
1499 /* We can model subregs, but not if they are
1500 wrapped in ZERO_EXTRACTS. */
1501 if (GET_CODE (reg) == SUBREG
1502 && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
1504 unsigned int start = SUBREG_BYTE (reg);
1505 unsigned int last = start
1506 + GET_MODE_SIZE (GET_MODE (reg));
1508 ra_init_live_subregs (bitmap_bit_p (live_relevant_regs,
1509 regno),
1510 live_subregs,
1511 live_subregs_used,
1512 regno, reg);
1514 if (!DF_REF_FLAGS_IS_SET
1515 (def, DF_REF_STRICT_LOW_PART))
1517 /* Expand the range to cover entire words.
1518 Bytes added here are "don't care". */
1519 start = start / UNITS_PER_WORD * UNITS_PER_WORD;
1520 last = ((last + UNITS_PER_WORD - 1)
1521 / UNITS_PER_WORD * UNITS_PER_WORD);
1524 /* Ignore the paradoxical bits. */
1525 if ((int)last > live_subregs_used[regno])
1526 last = live_subregs_used[regno];
1528 while (start < last)
1530 RESET_BIT (live_subregs[regno], start);
1531 start++;
1534 if (sbitmap_empty_p (live_subregs[regno]))
1536 live_subregs_used[regno] = 0;
1537 bitmap_clear_bit (live_relevant_regs, regno);
1539 else
1540 /* Set live_relevant_regs here because
1541 that bit has to be true to get us to
1542 look at the live_subregs fields. */
1543 bitmap_set_bit (live_relevant_regs, regno);
1545 else
1547 /* DF_REF_PARTIAL is generated for
1548 subregs, STRICT_LOW_PART, and
1549 ZERO_EXTRACT. We handle the subreg
1550 case above so here we have to keep from
1551 modeling the def as a killing def. */
1552 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
1554 bitmap_clear_bit (live_relevant_regs, regno);
1555 live_subregs_used[regno] = 0;
1561 bitmap_and_compl_into (live_relevant_regs, elim_regset);
1562 bitmap_copy (&c->live_throughout, live_relevant_regs);
1564 if (INSN_P (insn))
1565 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
1567 struct df_ref *use = *use_rec;
1568 unsigned int regno = DF_REF_REGNO (use);
1569 rtx reg = DF_REF_REG (use);
1571 /* DF_REF_READ_WRITE on a use means that this use
1572 is fabricated from a def that is a partial set
1573 to a multiword reg. Here, we only model the
1574 subreg case that is not wrapped in ZERO_EXTRACT
1575 precisely so we do not need to look at the
1576 fabricated use. */
1577 if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
1578 && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
1579 && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
1580 continue;
1582 /* Add the last use of each var to dead_or_set. */
1583 if (!bitmap_bit_p (live_relevant_regs, regno))
1585 if (regno < FIRST_PSEUDO_REGISTER)
1587 if (!fixed_regs[regno])
1588 bitmap_set_bit (&c->dead_or_set, regno);
1590 else if (pseudo_for_reload_consideration_p (regno))
1591 bitmap_set_bit (&c->dead_or_set, regno);
1594 if (regno < FIRST_PSEUDO_REGISTER
1595 || pseudo_for_reload_consideration_p (regno))
1597 if (GET_CODE (reg) == SUBREG
1598 && !DF_REF_FLAGS_IS_SET (use,
1599 DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT))
1601 unsigned int start = SUBREG_BYTE (reg);
1602 unsigned int last = start
1603 + GET_MODE_SIZE (GET_MODE (reg));
1605 ra_init_live_subregs (bitmap_bit_p (live_relevant_regs,
1606 regno),
1607 live_subregs,
1608 live_subregs_used,
1609 regno, reg);
1611 /* Ignore the paradoxical bits. */
1612 if ((int)last > live_subregs_used[regno])
1613 last = live_subregs_used[regno];
1615 while (start < last)
1617 SET_BIT (live_subregs[regno], start);
1618 start++;
1621 else
1622 /* Resetting the live_subregs_used is
1623 effectively saying do not use the subregs
1624 because we are reading the whole
1625 pseudo. */
1626 live_subregs_used[regno] = 0;
1627 bitmap_set_bit (live_relevant_regs, regno);
1633 /* FIXME!! The following code is a disaster. Reload needs to see the
1634 labels and jump tables that are just hanging out in between
1635 the basic blocks. See pr33676. */
1636 insn = BB_HEAD (bb);
1638 /* Skip over the barriers and cruft. */
1639 while (insn && (BARRIER_P (insn) || NOTE_P (insn)
1640 || BLOCK_FOR_INSN (insn) == bb))
1641 insn = PREV_INSN (insn);
1643 /* While we add anything except barriers and notes, the focus is
1644 to get the labels and jump tables into the
1645 reload_insn_chain. */
1646 while (insn)
1648 if (!NOTE_P (insn) && !BARRIER_P (insn))
1650 if (BLOCK_FOR_INSN (insn))
1651 break;
1653 c = new_insn_chain ();
1654 c->next = next;
1655 next = c;
1656 *p = c;
1657 p = &c->prev;
1659 /* The block makes no sense here, but it is what the old
1660 code did. */
1661 c->block = bb->index;
1662 c->insn = insn;
1663 bitmap_copy (&c->live_throughout, live_relevant_regs);
1665 insn = PREV_INSN (insn);
1669 for (i = 0; i < (unsigned int) max_regno; i++)
1670 if (live_subregs[i])
1671 free (live_subregs[i]);
1673 reload_insn_chain = c;
1674 *p = NULL;
1676 free (live_subregs);
1677 free (live_subregs_used);
1678 BITMAP_FREE (live_relevant_regs);
1679 BITMAP_FREE (elim_regset);
1681 if (dump_file)
1682 print_insn_chains (dump_file);
1685 /* Print debugging trace information if -dg switch is given,
1686 showing the information on which the allocation decisions are based. */
1688 static void
1689 dump_conflicts (FILE *file)
1691 int i;
1692 int regno;
1693 int has_preferences;
1694 int nregs;
1695 nregs = 0;
1696 for (i = 0; i < max_allocno; i++)
1698 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1699 continue;
1700 nregs++;
1702 fprintf (file, ";; %d regs to allocate:", nregs);
1703 for (regno = 0; regno < max_regno; regno++)
1704 if ((i = reg_allocno[regno]) >= 0)
1706 int j;
1707 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1708 continue;
1709 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1710 for (j = 0; j < max_regno; j++)
1711 if (reg_allocno[j] == allocno_order[i]
1712 && j != allocno[allocno_order[i]].reg)
1713 fprintf (file, "+%d", j);
1714 if (allocno[allocno_order[i]].size != 1)
1715 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1717 fprintf (file, "\n");
1719 for (regno = 0; regno < max_regno; regno++)
1720 if ((i = reg_allocno[regno]) >= 0)
1722 int j;
1723 adjacency_iter ai;
1724 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1725 FOR_EACH_CONFLICT (i, j, ai)
1727 fprintf (file, " %d", allocno[j].reg);
1729 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1730 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j)
1731 && !fixed_regs[j])
1732 fprintf (file, " %d", j);
1733 fprintf (file, "\n");
1735 has_preferences = 0;
1736 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1737 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1738 has_preferences = 1;
1740 if (!has_preferences)
1741 continue;
1742 fprintf (file, ";; %d preferences:", allocno[i].reg);
1743 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1744 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1745 fprintf (file, " %d", j);
1746 fprintf (file, "\n");
1748 fprintf (file, "\n");
1751 void
1752 dump_global_regs (FILE *file)
1754 int i, j;
1756 fprintf (file, ";; Register dispositions:\n");
1757 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1758 if (reg_renumber[i] >= 0)
1760 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1761 if (++j % 6 == 0)
1762 fprintf (file, "\n");
1765 fprintf (file, "\n\n;; Hard regs used: ");
1766 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1767 if (df_regs_ever_live_p (i))
1768 fprintf (file, " %d", i);
1769 fprintf (file, "\n\n");
1773 static bool
1774 gate_handle_global_alloc (void)
1776 return ! flag_ira;
1779 /* Run old register allocator. Return TRUE if we must exit
1780 rest_of_compilation upon return. */
1781 static unsigned int
1782 rest_of_handle_global_alloc (void)
1784 bool failure;
1786 /* If optimizing, allocate remaining pseudo-regs. Do the reload
1787 pass fixing up any insns that are invalid. */
1788 if (optimize && dbg_cnt (global_alloc_at_func))
1789 failure = global_alloc ();
1790 else
1792 /* There is just too much going on in the register allocators to
1793 keep things up to date. At the end we have to rescan anyway
1794 because things change when the reload_completed flag is set.
1795 So we just turn off scanning and we will rescan by hand. */
1796 df_set_flags (DF_NO_INSN_RESCAN);
1797 compute_regsets (&eliminable_regset, &no_global_alloc_regs);
1798 build_insn_chain ();
1799 df_set_flags (DF_NO_INSN_RESCAN);
1800 failure = reload (get_insns (), 0);
1803 if (dump_enabled_p (pass_global_alloc.pass.static_pass_number))
1805 timevar_push (TV_DUMP);
1806 dump_global_regs (dump_file);
1807 timevar_pop (TV_DUMP);
1810 /* FIXME: This appears on the surface to be wrong thing to be doing.
1811 So much of the compiler is designed to check reload_completed to
1812 see if it is running after reload that seems doomed to failure.
1813 We should be returning a value that says that we have found
1814 errors so that nothing but the cleanup passes are run
1815 afterwards. */
1816 gcc_assert (reload_completed || failure);
1817 reload_completed = !failure;
1819 /* The world has changed so much that at this point we might as well
1820 just rescan everything. Note that df_rescan_all_insns is not
1821 going to help here because it does not touch the artificial uses
1822 and defs. */
1823 df_finish_pass (true);
1824 if (optimize > 1)
1825 df_live_add_problem ();
1826 df_scan_alloc (NULL);
1827 df_scan_blocks ();
1829 if (optimize)
1830 df_analyze ();
1832 regstat_free_n_sets_and_refs ();
1833 regstat_free_ri ();
1834 return 0;
1837 struct rtl_opt_pass pass_global_alloc =
1840 RTL_PASS,
1841 "greg", /* name */
1842 gate_handle_global_alloc, /* gate */
1843 rest_of_handle_global_alloc, /* execute */
1844 NULL, /* sub */
1845 NULL, /* next */
1846 0, /* static_pass_number */
1847 TV_GLOBAL_ALLOC, /* tv_id */
1848 0, /* properties_required */
1849 0, /* properties_provided */
1850 0, /* properties_destroyed */
1851 0, /* todo_flags_start */
1852 TODO_dump_func | TODO_verify_rtl_sharing
1853 | TODO_ggc_collect /* todo_flags_finish */