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