(distribute_notes, case REG_DEAD): If a call uses a
[official-gcc.git] / gcc / global.c
blobc3dc7493ee52c2f9fae51318701b8877c58c0e6c
1 /* Allocate registers for pseudo-registers that span basic blocks.
2 Copyright (C) 1987, 1988, 1991, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdio.h>
22 #include "config.h"
23 #include "rtl.h"
24 #include "flags.h"
25 #include "basic-block.h"
26 #include "hard-reg-set.h"
27 #include "regs.h"
28 #include "insn-config.h"
29 #include "output.h"
31 /* This pass of the compiler performs global register allocation.
32 It assigns hard register numbers to all the pseudo registers
33 that were not handled in local_alloc. Assignments are recorded
34 in the vector reg_renumber, not by changing the rtl code.
35 (Such changes are made by final). The entry point is
36 the function global_alloc.
38 After allocation is complete, the reload pass is run as a subroutine
39 of this pass, so that when a pseudo reg loses its hard reg due to
40 spilling it is possible to make a second attempt to find a hard
41 reg for it. The reload pass is independent in other respects
42 and it is run even when stupid register allocation is in use.
44 1. count the pseudo-registers still needing allocation
45 and assign allocation-numbers (allocnos) to them.
46 Set up tables reg_allocno and allocno_reg to map
47 reg numbers to allocnos and vice versa.
48 max_allocno gets the number of allocnos in use.
50 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
51 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
52 for conflicts between allocnos and explicit hard register use
53 (which includes use of pseudo-registers allocated by local_alloc).
55 3. for each basic block
56 walk forward through the block, recording which
57 unallocated registers and which hardware registers are live.
58 Build the conflict matrix between the unallocated registers
59 and another of unallocated registers versus hardware registers.
60 Also record the preferred hardware registers
61 for each unallocated one.
63 4. Sort a table of the allocnos into order of
64 desirability of the variables.
66 5. Allocate the variables in that order; each if possible into
67 a preferred register, else into another register. */
69 /* Number of pseudo-registers still requiring allocation
70 (not allocated by local_allocate). */
72 static int max_allocno;
74 /* Indexed by (pseudo) reg number, gives the allocno, or -1
75 for pseudo registers already allocated by local_allocate. */
77 static int *reg_allocno;
79 /* Indexed by allocno, gives the reg number. */
81 static int *allocno_reg;
83 /* A vector of the integers from 0 to max_allocno-1,
84 sorted in the order of first-to-be-allocated first. */
86 static int *allocno_order;
88 /* Indexed by an allocno, gives the number of consecutive
89 hard registers needed by that pseudo reg. */
91 static int *allocno_size;
93 /* Indexed by (pseudo) reg number, gives the number of another
94 lower-numbered pseudo reg which can share a hard reg with this pseudo
95 *even if the two pseudos would otherwise appear to conflict*. */
97 static int *reg_may_share;
99 /* Define the number of bits in each element of `conflicts' and what
100 type that element has. We use the largest integer format on the
101 host machine. */
103 #define INT_BITS HOST_BITS_PER_WIDE_INT
104 #define INT_TYPE HOST_WIDE_INT
106 /* max_allocno by max_allocno array of bits,
107 recording whether two allocno's conflict (can't go in the same
108 hardware register).
110 `conflicts' is not symmetric; a conflict between allocno's i and j
111 is recorded either in element i,j or in element j,i. */
113 static INT_TYPE *conflicts;
115 /* Number of ints require to hold max_allocno bits.
116 This is the length of a row in `conflicts'. */
118 static int allocno_row_words;
120 /* Two macros to test or store 1 in an element of `conflicts'. */
122 #define CONFLICTP(I, J) \
123 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
124 & ((INT_TYPE) 1 << ((J) % INT_BITS)))
126 #define SET_CONFLICT(I, J) \
127 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
128 |= ((INT_TYPE) 1 << ((J) % INT_BITS)))
130 /* Set of hard regs currently live (during scan of all insns). */
132 static HARD_REG_SET hard_regs_live;
134 /* Indexed by N, set of hard regs conflicting with allocno N. */
136 static HARD_REG_SET *hard_reg_conflicts;
138 /* Indexed by N, set of hard regs preferred by allocno N.
139 This is used to make allocnos go into regs that are copied to or from them,
140 when possible, to reduce register shuffling. */
142 static HARD_REG_SET *hard_reg_preferences;
144 /* Similar, but just counts register preferences made in simple copy
145 operations, rather than arithmetic. These are given priority because
146 we can always eliminate an insn by using these, but using a register
147 in the above list won't always eliminate an insn. */
149 static HARD_REG_SET *hard_reg_copy_preferences;
151 /* Similar to hard_reg_preferences, but includes bits for subsequent
152 registers when an allocno is multi-word. The above variable is used for
153 allocation while this is used to build reg_someone_prefers, below. */
155 static HARD_REG_SET *hard_reg_full_preferences;
157 /* Indexed by N, set of hard registers that some later allocno has a
158 preference for. */
160 static HARD_REG_SET *regs_someone_prefers;
162 /* Set of registers that global-alloc isn't supposed to use. */
164 static HARD_REG_SET no_global_alloc_regs;
166 /* Set of registers used so far. */
168 static HARD_REG_SET regs_used_so_far;
170 /* Number of calls crossed by each allocno. */
172 static int *allocno_calls_crossed;
174 /* Number of refs (weighted) to each allocno. */
176 static int *allocno_n_refs;
178 /* Guess at live length of each allocno.
179 This is actually the max of the live lengths of the regs. */
181 static int *allocno_live_length;
183 /* Number of refs (weighted) to each hard reg, as used by local alloc.
184 It is zero for a reg that contains global pseudos or is explicitly used. */
186 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
188 /* Guess at live length of each hard reg, as used by local alloc.
189 This is actually the sum of the live lengths of the specific regs. */
191 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
193 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
194 for vector element I, and hard register number J. */
196 #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)
198 /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
200 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)
202 /* Bit mask for allocnos live at current point in the scan. */
204 static INT_TYPE *allocnos_live;
206 /* Test, set or clear bit number I in allocnos_live,
207 a bit vector indexed by allocno. */
209 #define ALLOCNO_LIVE_P(I) \
210 (allocnos_live[(I) / INT_BITS] & ((INT_TYPE) 1 << ((I) % INT_BITS)))
212 #define SET_ALLOCNO_LIVE(I) \
213 (allocnos_live[(I) / INT_BITS] |= ((INT_TYPE) 1 << ((I) % INT_BITS)))
215 #define CLEAR_ALLOCNO_LIVE(I) \
216 (allocnos_live[(I) / INT_BITS] &= ~((INT_TYPE) 1 << ((I) % INT_BITS)))
218 /* This is turned off because it doesn't work right for DImode.
219 (And it is only used for DImode, so the other cases are worthless.)
220 The problem is that it isn't true that there is NO possibility of conflict;
221 only that there is no conflict if the two pseudos get the exact same regs.
222 If they were allocated with a partial overlap, there would be a conflict.
223 We can't safely turn off the conflict unless we have another way to
224 prevent the partial overlap.
226 Idea: change hard_reg_conflicts so that instead of recording which
227 hard regs the allocno may not overlap, it records where the allocno
228 may not start. Change both where it is used and where it is updated.
229 Then there is a way to record that (reg:DI 108) may start at 10
230 but not at 9 or 11. There is still the question of how to record
231 this semi-conflict between two pseudos. */
232 #if 0
233 /* Reg pairs for which conflict after the current insn
234 is inhibited by a REG_NO_CONFLICT note.
235 If the table gets full, we ignore any other notes--that is conservative. */
236 #define NUM_NO_CONFLICT_PAIRS 4
237 /* Number of pairs in use in this insn. */
238 int n_no_conflict_pairs;
239 static struct { int allocno1, allocno2;}
240 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
241 #endif /* 0 */
243 /* Record all regs that are set in any one insn.
244 Communication from mark_reg_{store,clobber} and global_conflicts. */
246 static rtx *regs_set;
247 static int n_regs_set;
249 /* All register that can be eliminated. */
251 static HARD_REG_SET eliminable_regset;
253 static int allocno_compare PROTO((int *, int *));
254 static void global_conflicts PROTO((void));
255 static void expand_preferences PROTO((void));
256 static void prune_preferences PROTO((void));
257 static void find_reg PROTO((int, HARD_REG_SET, int, int, int));
258 static void record_one_conflict PROTO((int));
259 static void record_conflicts PROTO((short *, int));
260 static void mark_reg_store PROTO((rtx, rtx));
261 static void mark_reg_clobber PROTO((rtx, rtx));
262 static void mark_reg_conflicts PROTO((rtx));
263 static void mark_reg_death PROTO((rtx));
264 static void mark_reg_live_nc PROTO((int, enum machine_mode));
265 static void set_preference PROTO((rtx, rtx));
266 static void dump_conflicts PROTO((FILE *));
268 /* Perform allocation of pseudo-registers not allocated by local_alloc.
269 FILE is a file to output debugging information on,
270 or zero if such output is not desired.
272 Return value is nonzero if reload failed
273 and we must not do any more for this function. */
276 global_alloc (file)
277 FILE *file;
279 #ifdef ELIMINABLE_REGS
280 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
281 #endif
282 register int i;
283 rtx x;
285 max_allocno = 0;
287 /* A machine may have certain hard registers that
288 are safe to use only within a basic block. */
290 CLEAR_HARD_REG_SET (no_global_alloc_regs);
291 #ifdef OVERLAPPING_REGNO_P
292 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
293 if (OVERLAPPING_REGNO_P (i))
294 SET_HARD_REG_BIT (no_global_alloc_regs, i);
295 #endif
297 /* Build the regset of all eliminable registers and show we can't use those
298 that we already know won't be eliminated. */
299 #ifdef ELIMINABLE_REGS
300 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
302 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
304 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
305 || (eliminables[i].from == HARD_FRAME_POINTER_REGNUM
306 && (! flag_omit_frame_pointer || FRAME_POINTER_REQUIRED)))
307 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
309 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
310 if (!flag_omit_frame_pointer || FRAME_POINTER_REQUIRED)
311 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
312 #endif
313 #else
314 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
316 /* If we know we will definitely not be eliminating the frame pointer,
317 don't allocate it. */
318 if (! flag_omit_frame_pointer || FRAME_POINTER_REQUIRED)
319 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
320 #endif
322 /* Track which registers have already been used. Start with registers
323 explicitly in the rtl, then registers allocated by local register
324 allocation. */
326 CLEAR_HARD_REG_SET (regs_used_so_far);
327 #ifdef LEAF_REGISTERS
328 /* If we are doing the leaf function optimization, and this is a leaf
329 function, it means that the registers that take work to save are those
330 that need a register window. So prefer the ones that can be used in
331 a leaf function. */
333 char *cheap_regs;
334 static char leaf_regs[] = LEAF_REGISTERS;
336 if (only_leaf_regs_used () && leaf_function_p ())
337 cheap_regs = leaf_regs;
338 else
339 cheap_regs = call_used_regs;
340 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
341 if (regs_ever_live[i] || cheap_regs[i])
342 SET_HARD_REG_BIT (regs_used_so_far, i);
344 #else
345 /* We consider registers that do not have to be saved over calls as if
346 they were already used since there is no cost in using them. */
347 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
348 if (regs_ever_live[i] || call_used_regs[i])
349 SET_HARD_REG_BIT (regs_used_so_far, i);
350 #endif
352 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
353 if (reg_renumber[i] >= 0)
354 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
356 /* Establish mappings from register number to allocation number
357 and vice versa. In the process, count the allocnos. */
359 reg_allocno = (int *) alloca (max_regno * sizeof (int));
361 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
362 reg_allocno[i] = -1;
364 /* Initialize the shared-hard-reg mapping
365 from the list of pairs that may share. */
366 reg_may_share = (int *) alloca (max_regno * sizeof (int));
367 bzero ((char *) reg_may_share, max_regno * sizeof (int));
368 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
370 int r1 = REGNO (XEXP (x, 0));
371 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
372 if (r1 > r2)
373 reg_may_share[r1] = r2;
374 else
375 reg_may_share[r2] = r1;
378 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
379 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
380 that we are supposed to refrain from putting in a hard reg.
381 -2 means do make an allocno but don't allocate it. */
382 if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] != -1
383 /* Don't allocate pseudos that cross calls,
384 if this function receives a nonlocal goto. */
385 && (! current_function_has_nonlocal_label
386 || reg_n_calls_crossed[i] == 0))
388 if (reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
389 reg_allocno[i] = reg_allocno[reg_may_share[i]];
390 else
391 reg_allocno[i] = max_allocno++;
392 if (reg_live_length[i] == 0)
393 abort ();
395 else
396 reg_allocno[i] = -1;
398 allocno_reg = (int *) alloca (max_allocno * sizeof (int));
399 allocno_size = (int *) alloca (max_allocno * sizeof (int));
400 allocno_calls_crossed = (int *) alloca (max_allocno * sizeof (int));
401 allocno_n_refs = (int *) alloca (max_allocno * sizeof (int));
402 allocno_live_length = (int *) alloca (max_allocno * sizeof (int));
403 bzero ((char *) allocno_size, max_allocno * sizeof (int));
404 bzero ((char *) allocno_calls_crossed, max_allocno * sizeof (int));
405 bzero ((char *) allocno_n_refs, max_allocno * sizeof (int));
406 bzero ((char *) allocno_live_length, max_allocno * sizeof (int));
408 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
409 if (reg_allocno[i] >= 0)
411 int allocno = reg_allocno[i];
412 allocno_reg[allocno] = i;
413 allocno_size[allocno] = PSEUDO_REGNO_SIZE (i);
414 allocno_calls_crossed[allocno] += reg_n_calls_crossed[i];
415 allocno_n_refs[allocno] += reg_n_refs[i];
416 if (allocno_live_length[allocno] < reg_live_length[i])
417 allocno_live_length[allocno] = reg_live_length[i];
420 /* Calculate amount of usage of each hard reg by pseudos
421 allocated by local-alloc. This is to see if we want to
422 override it. */
423 bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
424 bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
425 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
426 if (reg_allocno[i] < 0 && reg_renumber[i] >= 0)
428 int regno = reg_renumber[i];
429 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
430 int j;
432 for (j = regno; j < endregno; j++)
434 local_reg_n_refs[j] += reg_n_refs[i];
435 local_reg_live_length[j] += reg_live_length[i];
439 /* We can't override local-alloc for a reg used not just by local-alloc. */
440 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
441 if (regs_ever_live[i])
442 local_reg_n_refs[i] = 0;
444 /* Allocate the space for the conflict and preference tables and
445 initialize them. */
447 hard_reg_conflicts
448 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
449 bzero ((char *) hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
451 hard_reg_preferences
452 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
453 bzero ((char *) hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
455 hard_reg_copy_preferences
456 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
457 bzero ((char *) hard_reg_copy_preferences,
458 max_allocno * sizeof (HARD_REG_SET));
460 hard_reg_full_preferences
461 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
462 bzero ((char *) hard_reg_full_preferences,
463 max_allocno * sizeof (HARD_REG_SET));
465 regs_someone_prefers
466 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
467 bzero ((char *) regs_someone_prefers, max_allocno * sizeof (HARD_REG_SET));
469 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
471 conflicts = (INT_TYPE *) alloca (max_allocno * allocno_row_words
472 * sizeof (INT_TYPE));
473 bzero ((char *) conflicts,
474 max_allocno * allocno_row_words * sizeof (INT_TYPE));
476 allocnos_live = (INT_TYPE *) alloca (allocno_row_words * sizeof (INT_TYPE));
478 /* If there is work to be done (at least one reg to allocate),
479 perform global conflict analysis and allocate the regs. */
481 if (max_allocno > 0)
483 /* Scan all the insns and compute the conflicts among allocnos
484 and between allocnos and hard regs. */
486 global_conflicts ();
488 /* Eliminate conflicts between pseudos and eliminable registers. If
489 the register is not eliminated, the pseudo won't really be able to
490 live in the eliminable register, so the conflict doesn't matter.
491 If we do eliminate the register, the conflict will no longer exist.
492 So in either case, we can ignore the conflict. Likewise for
493 preferences. */
495 for (i = 0; i < max_allocno; i++)
497 AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
498 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
499 eliminable_regset);
500 AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
503 /* Try to expand the preferences by merging them between allocnos. */
505 expand_preferences ();
507 /* Determine the order to allocate the remaining pseudo registers. */
509 allocno_order = (int *) alloca (max_allocno * sizeof (int));
510 for (i = 0; i < max_allocno; i++)
511 allocno_order[i] = i;
513 /* Default the size to 1, since allocno_compare uses it to divide by.
514 Also convert allocno_live_length of zero to -1. A length of zero
515 can occur when all the registers for that allocno have reg_live_length
516 equal to -2. In this case, we want to make an allocno, but not
517 allocate it. So avoid the divide-by-zero and set it to a low
518 priority. */
520 for (i = 0; i < max_allocno; i++)
522 if (allocno_size[i] == 0)
523 allocno_size[i] = 1;
524 if (allocno_live_length[i] == 0)
525 allocno_live_length[i] = -1;
528 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
530 prune_preferences ();
532 if (file)
533 dump_conflicts (file);
535 /* Try allocating them, one by one, in that order,
536 except for parameters marked with reg_live_length[regno] == -2. */
538 for (i = 0; i < max_allocno; i++)
539 if (reg_live_length[allocno_reg[allocno_order[i]]] >= 0)
541 /* If we have more than one register class,
542 first try allocating in the class that is cheapest
543 for this pseudo-reg. If that fails, try any reg. */
544 if (N_REG_CLASSES > 1)
546 find_reg (allocno_order[i], HARD_CONST (0), 0, 0, 0);
547 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
548 continue;
550 if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
551 find_reg (allocno_order[i], HARD_CONST (0), 1, 0, 0);
555 /* Do the reloads now while the allocno data still exist, so that we can
556 try to assign new hard regs to any pseudo regs that are spilled. */
558 #if 0 /* We need to eliminate regs even if there is no rtl code,
559 for the sake of debugging information. */
560 if (n_basic_blocks > 0)
561 #endif
562 return reload (get_insns (), 1, file);
565 /* Sort predicate for ordering the allocnos.
566 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
568 static int
569 allocno_compare (v1, v2)
570 int *v1, *v2;
572 /* Note that the quotient will never be bigger than
573 the value of floor_log2 times the maximum number of
574 times a register can occur in one insn (surely less than 100).
575 Multiplying this by 10000 can't overflow. */
576 register int pri1
577 = (((double) (floor_log2 (allocno_n_refs[*v1]) * allocno_n_refs[*v1])
578 / allocno_live_length[*v1])
579 * 10000 * allocno_size[*v1]);
580 register int pri2
581 = (((double) (floor_log2 (allocno_n_refs[*v2]) * allocno_n_refs[*v2])
582 / allocno_live_length[*v2])
583 * 10000 * allocno_size[*v2]);
584 if (pri2 - pri1)
585 return pri2 - pri1;
587 /* If regs are equally good, sort by allocno,
588 so that the results of qsort leave nothing to chance. */
589 return *v1 - *v2;
592 /* Scan the rtl code and record all conflicts and register preferences in the
593 conflict matrices and preference tables. */
595 static void
596 global_conflicts ()
598 register int b, i;
599 register rtx insn;
600 short *block_start_allocnos;
602 /* Make a vector that mark_reg_{store,clobber} will store in. */
603 regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2);
605 block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
607 for (b = 0; b < n_basic_blocks; b++)
609 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
611 /* Initialize table of registers currently live
612 to the state at the beginning of this basic block.
613 This also marks the conflicts among them.
615 For pseudo-regs, there is only one bit for each one
616 no matter how many hard regs it occupies.
617 This is ok; we know the size from PSEUDO_REGNO_SIZE.
618 For explicit hard regs, we cannot know the size that way
619 since one hard reg can be used with various sizes.
620 Therefore, we must require that all the hard regs
621 implicitly live as part of a multi-word hard reg
622 are explicitly marked in basic_block_live_at_start. */
625 register int offset;
626 REGSET_ELT_TYPE bit;
627 register regset old = basic_block_live_at_start[b];
628 int ax = 0;
630 #ifdef HARD_REG_SET
631 hard_regs_live = old[0];
632 #else
633 COPY_HARD_REG_SET (hard_regs_live, old);
634 #endif
635 for (offset = 0, i = 0; offset < regset_size; offset++)
636 if (old[offset] == 0)
637 i += REGSET_ELT_BITS;
638 else
639 for (bit = 1; bit; bit <<= 1, i++)
641 if (i >= max_regno)
642 break;
643 if (old[offset] & bit)
645 register int a = reg_allocno[i];
646 if (a >= 0)
648 SET_ALLOCNO_LIVE (a);
649 block_start_allocnos[ax++] = a;
651 else if ((a = reg_renumber[i]) >= 0)
652 mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
656 /* Record that each allocno now live conflicts with each other
657 allocno now live, and with each hard reg now live. */
659 record_conflicts (block_start_allocnos, ax);
662 insn = basic_block_head[b];
664 /* Scan the code of this basic block, noting which allocnos
665 and hard regs are born or die. When one is born,
666 record a conflict with all others currently live. */
668 while (1)
670 register RTX_CODE code = GET_CODE (insn);
671 register rtx link;
673 /* Make regs_set an empty set. */
675 n_regs_set = 0;
677 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
680 #if 0
681 int i = 0;
682 for (link = REG_NOTES (insn);
683 link && i < NUM_NO_CONFLICT_PAIRS;
684 link = XEXP (link, 1))
685 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
687 no_conflict_pairs[i].allocno1
688 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
689 no_conflict_pairs[i].allocno2
690 = reg_allocno[REGNO (XEXP (link, 0))];
691 i++;
693 #endif /* 0 */
695 /* Mark any registers clobbered by INSN as live,
696 so they conflict with the inputs. */
698 note_stores (PATTERN (insn), mark_reg_clobber);
700 /* Mark any registers dead after INSN as dead now. */
702 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
703 if (REG_NOTE_KIND (link) == REG_DEAD)
704 mark_reg_death (XEXP (link, 0));
706 /* Mark any registers set in INSN as live,
707 and mark them as conflicting with all other live regs.
708 Clobbers are processed again, so they conflict with
709 the registers that are set. */
711 note_stores (PATTERN (insn), mark_reg_store);
713 #ifdef AUTO_INC_DEC
714 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
715 if (REG_NOTE_KIND (link) == REG_INC)
716 mark_reg_store (XEXP (link, 0), NULL_RTX);
717 #endif
719 /* If INSN has multiple outputs, then any reg that dies here
720 and is used inside of an output
721 must conflict with the other outputs. */
723 if (GET_CODE (PATTERN (insn)) == PARALLEL && !single_set (insn))
724 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
725 if (REG_NOTE_KIND (link) == REG_DEAD)
727 int used_in_output = 0;
728 int i;
729 rtx reg = XEXP (link, 0);
731 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
733 rtx set = XVECEXP (PATTERN (insn), 0, i);
734 if (GET_CODE (set) == SET
735 && GET_CODE (SET_DEST (set)) != REG
736 && !rtx_equal_p (reg, SET_DEST (set))
737 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
738 used_in_output = 1;
740 if (used_in_output)
741 mark_reg_conflicts (reg);
744 /* Mark any registers set in INSN and then never used. */
746 while (n_regs_set > 0)
747 if (find_regno_note (insn, REG_UNUSED,
748 REGNO (regs_set[--n_regs_set])))
749 mark_reg_death (regs_set[n_regs_set]);
752 if (insn == basic_block_end[b])
753 break;
754 insn = NEXT_INSN (insn);
758 /* Expand the preference information by looking for cases where one allocno
759 dies in an insn that sets an allocno. If those two allocnos don't conflict,
760 merge any preferences between those allocnos. */
762 static void
763 expand_preferences ()
765 rtx insn;
766 rtx link;
767 rtx set;
769 /* We only try to handle the most common cases here. Most of the cases
770 where this wins are reg-reg copies. */
772 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
773 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
774 && (set = single_set (insn)) != 0
775 && GET_CODE (SET_DEST (set)) == REG
776 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
777 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
778 if (REG_NOTE_KIND (link) == REG_DEAD
779 && GET_CODE (XEXP (link, 0)) == REG
780 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
781 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
782 reg_allocno[REGNO (XEXP (link, 0))])
783 && ! CONFLICTP (reg_allocno[REGNO (XEXP (link, 0))],
784 reg_allocno[REGNO (SET_DEST (set))]))
786 int a1 = reg_allocno[REGNO (SET_DEST (set))];
787 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
789 if (XEXP (link, 0) == SET_SRC (set))
791 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
792 hard_reg_copy_preferences[a2]);
793 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
794 hard_reg_copy_preferences[a1]);
797 IOR_HARD_REG_SET (hard_reg_preferences[a1],
798 hard_reg_preferences[a2]);
799 IOR_HARD_REG_SET (hard_reg_preferences[a2],
800 hard_reg_preferences[a1]);
801 IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
802 hard_reg_full_preferences[a2]);
803 IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
804 hard_reg_full_preferences[a1]);
808 /* Prune the preferences for global registers to exclude registers that cannot
809 be used.
811 Compute `regs_someone_prefers', which is a bitmask of the hard registers
812 that are preferred by conflicting registers of lower priority. If possible,
813 we will avoid using these registers. */
815 static void
816 prune_preferences ()
818 int i, j;
819 int allocno;
821 /* Scan least most important to most important.
822 For each allocno, remove from preferences registers that cannot be used,
823 either because of conflicts or register type. Then compute all registers
824 preferred by each lower-priority register that conflicts. */
826 for (i = max_allocno - 1; i >= 0; i--)
828 HARD_REG_SET temp;
830 allocno = allocno_order[i];
831 COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
833 if (allocno_calls_crossed[allocno] == 0)
834 IOR_HARD_REG_SET (temp, fixed_reg_set);
835 else
836 IOR_HARD_REG_SET (temp, call_used_reg_set);
838 IOR_COMPL_HARD_REG_SET
839 (temp,
840 reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
842 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
843 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
844 AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
846 CLEAR_HARD_REG_SET (regs_someone_prefers[allocno]);
848 /* Merge in the preferences of lower-priority registers (they have
849 already been pruned). If we also prefer some of those registers,
850 don't exclude them unless we are of a smaller size (in which case
851 we want to give the lower-priority allocno the first chance for
852 these registers). */
853 for (j = i + 1; j < max_allocno; j++)
854 if (CONFLICTP (allocno, allocno_order[j]))
856 COPY_HARD_REG_SET (temp,
857 hard_reg_full_preferences[allocno_order[j]]);
858 if (allocno_size[allocno_order[j]] <= allocno_size[allocno])
859 AND_COMPL_HARD_REG_SET (temp,
860 hard_reg_full_preferences[allocno]);
862 IOR_HARD_REG_SET (regs_someone_prefers[allocno], temp);
867 /* Assign a hard register to ALLOCNO; look for one that is the beginning
868 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
869 The registers marked in PREFREGS are tried first.
871 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
872 be used for this allocation.
874 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
875 Otherwise ignore that preferred class and use the alternate class.
877 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
878 will have to be saved and restored at calls.
880 RETRYING is nonzero if this is called from retry_global_alloc.
882 If we find one, record it in reg_renumber.
883 If not, do nothing. */
885 static void
886 find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
887 int allocno;
888 HARD_REG_SET losers;
889 int alt_regs_p;
890 int accept_call_clobbered;
891 int retrying;
893 register int i, best_reg, pass;
894 #ifdef HARD_REG_SET
895 register /* Declare it register if it's a scalar. */
896 #endif
897 HARD_REG_SET used, used1, used2;
899 enum reg_class class = (alt_regs_p
900 ? reg_alternate_class (allocno_reg[allocno])
901 : reg_preferred_class (allocno_reg[allocno]));
902 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
904 if (accept_call_clobbered)
905 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
906 else if (allocno_calls_crossed[allocno] == 0)
907 COPY_HARD_REG_SET (used1, fixed_reg_set);
908 else
909 COPY_HARD_REG_SET (used1, call_used_reg_set);
911 /* Some registers should not be allocated in global-alloc. */
912 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
913 if (losers)
914 IOR_HARD_REG_SET (used1, losers);
916 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
917 COPY_HARD_REG_SET (used2, used1);
919 IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
921 /* Try each hard reg to see if it fits. Do this in two passes.
922 In the first pass, skip registers that are preferred by some other pseudo
923 to give it a better chance of getting one of those registers. Only if
924 we can't get a register when excluding those do we take one of them.
925 However, we never allocate a register for the first time in pass 0. */
927 COPY_HARD_REG_SET (used, used1);
928 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
929 IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
931 best_reg = -1;
932 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
933 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
934 pass++)
936 if (pass == 1)
937 COPY_HARD_REG_SET (used, used1);
938 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
940 #ifdef REG_ALLOC_ORDER
941 int regno = reg_alloc_order[i];
942 #else
943 int regno = i;
944 #endif
945 if (! TEST_HARD_REG_BIT (used, regno)
946 && HARD_REGNO_MODE_OK (regno, mode))
948 register int j;
949 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
950 for (j = regno + 1;
951 (j < lim
952 && ! TEST_HARD_REG_BIT (used, j));
953 j++);
954 if (j == lim)
956 best_reg = regno;
957 break;
959 #ifndef REG_ALLOC_ORDER
960 i = j; /* Skip starting points we know will lose */
961 #endif
966 /* See if there is a preferred register with the same class as the register
967 we allocated above. Making this restriction prevents register
968 preferencing from creating worse register allocation.
970 Remove from the preferred registers and conflicting registers. Note that
971 additional conflicts may have been added after `prune_preferences' was
972 called.
974 First do this for those register with copy preferences, then all
975 preferred registers. */
977 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
978 GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
979 reg_class_contents[(int) NO_REGS], no_copy_prefs);
981 if (best_reg >= 0)
983 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
984 if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
985 && HARD_REGNO_MODE_OK (i, mode)
986 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
987 || reg_class_subset_p (REGNO_REG_CLASS (i),
988 REGNO_REG_CLASS (best_reg))
989 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
990 REGNO_REG_CLASS (i))))
992 register int j;
993 register int lim = i + HARD_REGNO_NREGS (i, mode);
994 for (j = i + 1;
995 (j < lim
996 && ! TEST_HARD_REG_BIT (used, j)
997 && (REGNO_REG_CLASS (j)
998 == REGNO_REG_CLASS (best_reg + (j - i))
999 || reg_class_subset_p (REGNO_REG_CLASS (j),
1000 REGNO_REG_CLASS (best_reg + (j - i)))
1001 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1002 REGNO_REG_CLASS (j))));
1003 j++);
1004 if (j == lim)
1006 best_reg = i;
1007 goto no_prefs;
1011 no_copy_prefs:
1013 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1014 GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1015 reg_class_contents[(int) NO_REGS], no_prefs);
1017 if (best_reg >= 0)
1019 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1020 if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1021 && HARD_REGNO_MODE_OK (i, mode)
1022 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1023 || reg_class_subset_p (REGNO_REG_CLASS (i),
1024 REGNO_REG_CLASS (best_reg))
1025 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1026 REGNO_REG_CLASS (i))))
1028 register int j;
1029 register int lim = i + HARD_REGNO_NREGS (i, mode);
1030 for (j = i + 1;
1031 (j < lim
1032 && ! TEST_HARD_REG_BIT (used, j)
1033 && (REGNO_REG_CLASS (j)
1034 == REGNO_REG_CLASS (best_reg + (j - i))
1035 || reg_class_subset_p (REGNO_REG_CLASS (j),
1036 REGNO_REG_CLASS (best_reg + (j - i)))
1037 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1038 REGNO_REG_CLASS (j))));
1039 j++);
1040 if (j == lim)
1042 best_reg = i;
1043 break;
1047 no_prefs:
1049 /* If we haven't succeeded yet, try with caller-saves.
1050 We need not check to see if the current function has nonlocal
1051 labels because we don't put any pseudos that are live over calls in
1052 registers in that case. */
1054 if (flag_caller_saves && best_reg < 0)
1056 /* Did not find a register. If it would be profitable to
1057 allocate a call-clobbered register and save and restore it
1058 around calls, do that. */
1059 if (! accept_call_clobbered
1060 && allocno_calls_crossed[allocno] != 0
1061 && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1062 allocno_calls_crossed[allocno]))
1064 find_reg (allocno, losers, alt_regs_p, 1, retrying);
1065 if (reg_renumber[allocno_reg[allocno]] >= 0)
1067 caller_save_needed = 1;
1068 return;
1073 /* If we haven't succeeded yet,
1074 see if some hard reg that conflicts with us
1075 was utilized poorly by local-alloc.
1076 If so, kick out the regs that were put there by local-alloc
1077 so we can use it instead. */
1078 if (best_reg < 0 && !retrying
1079 /* Let's not bother with multi-reg allocnos. */
1080 && allocno_size[allocno] == 1)
1082 /* Count from the end, to find the least-used ones first. */
1083 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1085 #ifdef REG_ALLOC_ORDER
1086 int regno = reg_alloc_order[i];
1087 #else
1088 int regno = i;
1089 #endif
1091 if (local_reg_n_refs[regno] != 0
1092 /* Don't use a reg no good for this pseudo. */
1093 && ! TEST_HARD_REG_BIT (used2, regno)
1094 && HARD_REGNO_MODE_OK (regno, mode)
1095 && (((double) local_reg_n_refs[regno]
1096 / local_reg_live_length[regno])
1097 < ((double) allocno_n_refs[allocno]
1098 / allocno_live_length[allocno])))
1100 /* Hard reg REGNO was used less in total by local regs
1101 than it would be used by this one allocno! */
1102 int k;
1103 for (k = 0; k < max_regno; k++)
1104 if (reg_renumber[k] >= 0)
1106 int r = reg_renumber[k];
1107 int endregno
1108 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1110 if (regno >= r && regno < endregno)
1111 reg_renumber[k] = -1;
1114 best_reg = regno;
1115 break;
1120 /* Did we find a register? */
1122 if (best_reg >= 0)
1124 register int lim, j;
1125 HARD_REG_SET this_reg;
1127 /* Yes. Record it as the hard register of this pseudo-reg. */
1128 reg_renumber[allocno_reg[allocno]] = best_reg;
1129 /* Also of any pseudo-regs that share with it. */
1130 if (reg_may_share[allocno_reg[allocno]])
1131 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1132 if (reg_allocno[j] == allocno)
1133 reg_renumber[j] = best_reg;
1135 /* Make a set of the hard regs being allocated. */
1136 CLEAR_HARD_REG_SET (this_reg);
1137 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1138 for (j = best_reg; j < lim; j++)
1140 SET_HARD_REG_BIT (this_reg, j);
1141 SET_HARD_REG_BIT (regs_used_so_far, j);
1142 /* This is no longer a reg used just by local regs. */
1143 local_reg_n_refs[j] = 0;
1145 /* For each other pseudo-reg conflicting with this one,
1146 mark it as conflicting with the hard regs this one occupies. */
1147 lim = allocno;
1148 for (j = 0; j < max_allocno; j++)
1149 if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
1151 IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1156 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1157 Perhaps it had previously seemed not worth a hard reg,
1158 or perhaps its old hard reg has been commandeered for reloads.
1159 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1160 they do not appear to be allocated.
1161 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1163 void
1164 retry_global_alloc (regno, forbidden_regs)
1165 int regno;
1166 HARD_REG_SET forbidden_regs;
1168 int allocno = reg_allocno[regno];
1169 if (allocno >= 0)
1171 /* If we have more than one register class,
1172 first try allocating in the class that is cheapest
1173 for this pseudo-reg. If that fails, try any reg. */
1174 if (N_REG_CLASSES > 1)
1175 find_reg (allocno, forbidden_regs, 0, 0, 1);
1176 if (reg_renumber[regno] < 0
1177 && reg_alternate_class (regno) != NO_REGS)
1178 find_reg (allocno, forbidden_regs, 1, 0, 1);
1180 /* If we found a register, modify the RTL for the register to
1181 show the hard register, and mark that register live. */
1182 if (reg_renumber[regno] >= 0)
1184 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1185 mark_home_live (regno);
1190 /* Record a conflict between register REGNO
1191 and everything currently live.
1192 REGNO must not be a pseudo reg that was allocated
1193 by local_alloc; such numbers must be translated through
1194 reg_renumber before calling here. */
1196 static void
1197 record_one_conflict (regno)
1198 int regno;
1200 register int j;
1202 if (regno < FIRST_PSEUDO_REGISTER)
1203 /* When a hard register becomes live,
1204 record conflicts with live pseudo regs. */
1205 for (j = 0; j < max_allocno; j++)
1207 if (ALLOCNO_LIVE_P (j))
1208 SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1210 else
1211 /* When a pseudo-register becomes live,
1212 record conflicts first with hard regs,
1213 then with other pseudo regs. */
1215 register int ialloc = reg_allocno[regno];
1216 register int ialloc_prod = ialloc * allocno_row_words;
1217 IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1218 for (j = allocno_row_words - 1; j >= 0; j--)
1220 #if 0
1221 int k;
1222 for (k = 0; k < n_no_conflict_pairs; k++)
1223 if (! ((j == no_conflict_pairs[k].allocno1
1224 && ialloc == no_conflict_pairs[k].allocno2)
1226 (j == no_conflict_pairs[k].allocno2
1227 && ialloc == no_conflict_pairs[k].allocno1)))
1228 #endif /* 0 */
1229 conflicts[ialloc_prod + j] |= allocnos_live[j];
1234 /* Record all allocnos currently live as conflicting
1235 with each other and with all hard regs currently live.
1236 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1237 are currently live. Their bits are also flagged in allocnos_live. */
1239 static void
1240 record_conflicts (allocno_vec, len)
1241 register short *allocno_vec;
1242 register int len;
1244 register int allocno;
1245 register int j;
1246 register int ialloc_prod;
1248 while (--len >= 0)
1250 allocno = allocno_vec[len];
1251 ialloc_prod = allocno * allocno_row_words;
1252 IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1253 for (j = allocno_row_words - 1; j >= 0; j--)
1254 conflicts[ialloc_prod + j] |= allocnos_live[j];
1258 /* Handle the case where REG is set by the insn being scanned,
1259 during the forward scan to accumulate conflicts.
1260 Store a 1 in regs_live or allocnos_live for this register, record how many
1261 consecutive hardware registers it actually needs,
1262 and record a conflict with all other registers already live.
1264 Note that even if REG does not remain alive after this insn,
1265 we must mark it here as live, to ensure a conflict between
1266 REG and any other regs set in this insn that really do live.
1267 This is because those other regs could be considered after this.
1269 REG might actually be something other than a register;
1270 if so, we do nothing.
1272 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1273 a REG_INC note was found for it).
1275 CLOBBERs are processed here by calling mark_reg_clobber. */
1277 static void
1278 mark_reg_store (orig_reg, setter)
1279 rtx orig_reg, setter;
1281 register int regno;
1282 register rtx reg = orig_reg;
1284 /* WORD is which word of a multi-register group is being stored.
1285 For the case where the store is actually into a SUBREG of REG.
1286 Except we don't use it; I believe the entire REG needs to be
1287 made live. */
1288 int word = 0;
1290 if (GET_CODE (reg) == SUBREG)
1292 word = SUBREG_WORD (reg);
1293 reg = SUBREG_REG (reg);
1296 if (GET_CODE (reg) != REG)
1297 return;
1299 if (setter && GET_CODE (setter) == CLOBBER)
1301 /* A clobber of a register should be processed here too. */
1302 mark_reg_clobber (orig_reg, setter);
1303 return;
1306 regs_set[n_regs_set++] = reg;
1308 if (setter)
1309 set_preference (reg, SET_SRC (setter));
1311 regno = REGNO (reg);
1313 if (reg_renumber[regno] >= 0)
1314 regno = reg_renumber[regno] /* + word */;
1316 /* Either this is one of the max_allocno pseudo regs not allocated,
1317 or it is or has a hardware reg. First handle the pseudo-regs. */
1318 if (regno >= FIRST_PSEUDO_REGISTER)
1320 if (reg_allocno[regno] >= 0)
1322 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1323 record_one_conflict (regno);
1326 /* Handle hardware regs (and pseudos allocated to hard regs). */
1327 else if (! fixed_regs[regno])
1329 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1330 while (regno < last)
1332 record_one_conflict (regno);
1333 SET_HARD_REG_BIT (hard_regs_live, regno);
1334 regno++;
1339 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1341 static void
1342 mark_reg_clobber (reg, setter)
1343 rtx reg, setter;
1345 register int regno;
1347 /* WORD is which word of a multi-register group is being stored.
1348 For the case where the store is actually into a SUBREG of REG.
1349 Except we don't use it; I believe the entire REG needs to be
1350 made live. */
1351 int word = 0;
1353 if (GET_CODE (setter) != CLOBBER)
1354 return;
1356 if (GET_CODE (reg) == SUBREG)
1358 word = SUBREG_WORD (reg);
1359 reg = SUBREG_REG (reg);
1362 if (GET_CODE (reg) != REG)
1363 return;
1365 regs_set[n_regs_set++] = reg;
1367 regno = REGNO (reg);
1369 if (reg_renumber[regno] >= 0)
1370 regno = reg_renumber[regno] /* + word */;
1372 /* Either this is one of the max_allocno pseudo regs not allocated,
1373 or it is or has a hardware reg. First handle the pseudo-regs. */
1374 if (regno >= FIRST_PSEUDO_REGISTER)
1376 if (reg_allocno[regno] >= 0)
1378 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1379 record_one_conflict (regno);
1382 /* Handle hardware regs (and pseudos allocated to hard regs). */
1383 else if (! fixed_regs[regno])
1385 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1386 while (regno < last)
1388 record_one_conflict (regno);
1389 SET_HARD_REG_BIT (hard_regs_live, regno);
1390 regno++;
1395 /* Record that REG has conflicts with all the regs currently live.
1396 Do not mark REG itself as live. */
1398 static void
1399 mark_reg_conflicts (reg)
1400 rtx reg;
1402 register int regno;
1404 if (GET_CODE (reg) == SUBREG)
1405 reg = SUBREG_REG (reg);
1407 if (GET_CODE (reg) != REG)
1408 return;
1410 regno = REGNO (reg);
1412 if (reg_renumber[regno] >= 0)
1413 regno = reg_renumber[regno];
1415 /* Either this is one of the max_allocno pseudo regs not allocated,
1416 or it is or has a hardware reg. First handle the pseudo-regs. */
1417 if (regno >= FIRST_PSEUDO_REGISTER)
1419 if (reg_allocno[regno] >= 0)
1420 record_one_conflict (regno);
1422 /* Handle hardware regs (and pseudos allocated to hard regs). */
1423 else if (! fixed_regs[regno])
1425 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1426 while (regno < last)
1428 record_one_conflict (regno);
1429 regno++;
1434 /* Mark REG as being dead (following the insn being scanned now).
1435 Store a 0 in regs_live or allocnos_live for this register. */
1437 static void
1438 mark_reg_death (reg)
1439 rtx reg;
1441 register int regno = REGNO (reg);
1443 /* For pseudo reg, see if it has been assigned a hardware reg. */
1444 if (reg_renumber[regno] >= 0)
1445 regno = reg_renumber[regno];
1447 /* Either this is one of the max_allocno pseudo regs not allocated,
1448 or it is a hardware reg. First handle the pseudo-regs. */
1449 if (regno >= FIRST_PSEUDO_REGISTER)
1451 if (reg_allocno[regno] >= 0)
1452 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1454 /* Handle hardware regs (and pseudos allocated to hard regs). */
1455 else if (! fixed_regs[regno])
1457 /* Pseudo regs already assigned hardware regs are treated
1458 almost the same as explicit hardware regs. */
1459 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1460 while (regno < last)
1462 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1463 regno++;
1468 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1469 for the value stored in it. MODE determines how many consecutive
1470 registers are actually in use. Do not record conflicts;
1471 it is assumed that the caller will do that. */
1473 static void
1474 mark_reg_live_nc (regno, mode)
1475 register int regno;
1476 enum machine_mode mode;
1478 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1479 while (regno < last)
1481 SET_HARD_REG_BIT (hard_regs_live, regno);
1482 regno++;
1486 /* Try to set a preference for an allocno to a hard register.
1487 We are passed DEST and SRC which are the operands of a SET. It is known
1488 that SRC is a register. If SRC or the first operand of SRC is a register,
1489 try to set a preference. If one of the two is a hard register and the other
1490 is a pseudo-register, mark the preference.
1492 Note that we are not as aggressive as local-alloc in trying to tie a
1493 pseudo-register to a hard register. */
1495 static void
1496 set_preference (dest, src)
1497 rtx dest, src;
1499 int src_regno, dest_regno;
1500 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1501 to compensate for subregs in SRC or DEST. */
1502 int offset = 0;
1503 int i;
1504 int copy = 1;
1506 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1507 src = XEXP (src, 0), copy = 0;
1509 /* Get the reg number for both SRC and DEST.
1510 If neither is a reg, give up. */
1512 if (GET_CODE (src) == REG)
1513 src_regno = REGNO (src);
1514 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1516 src_regno = REGNO (SUBREG_REG (src));
1517 offset += SUBREG_WORD (src);
1519 else
1520 return;
1522 if (GET_CODE (dest) == REG)
1523 dest_regno = REGNO (dest);
1524 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1526 dest_regno = REGNO (SUBREG_REG (dest));
1527 offset -= SUBREG_WORD (dest);
1529 else
1530 return;
1532 /* Convert either or both to hard reg numbers. */
1534 if (reg_renumber[src_regno] >= 0)
1535 src_regno = reg_renumber[src_regno];
1537 if (reg_renumber[dest_regno] >= 0)
1538 dest_regno = reg_renumber[dest_regno];
1540 /* Now if one is a hard reg and the other is a global pseudo
1541 then give the other a preference. */
1543 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1544 && reg_allocno[src_regno] >= 0)
1546 dest_regno -= offset;
1547 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1549 if (copy)
1550 SET_REGBIT (hard_reg_copy_preferences,
1551 reg_allocno[src_regno], dest_regno);
1553 SET_REGBIT (hard_reg_preferences,
1554 reg_allocno[src_regno], dest_regno);
1555 for (i = dest_regno;
1556 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1557 i++)
1558 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1562 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1563 && reg_allocno[dest_regno] >= 0)
1565 src_regno += offset;
1566 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1568 if (copy)
1569 SET_REGBIT (hard_reg_copy_preferences,
1570 reg_allocno[dest_regno], src_regno);
1572 SET_REGBIT (hard_reg_preferences,
1573 reg_allocno[dest_regno], src_regno);
1574 for (i = src_regno;
1575 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1576 i++)
1577 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1582 /* Indicate that hard register number FROM was eliminated and replaced with
1583 an offset from hard register number TO. The status of hard registers live
1584 at the start of a basic block is updated by replacing a use of FROM with
1585 a use of TO. */
1587 void
1588 mark_elimination (from, to)
1589 int from, to;
1591 int i;
1593 for (i = 0; i < n_basic_blocks; i++)
1594 if ((basic_block_live_at_start[i][from / REGSET_ELT_BITS]
1595 & ((REGSET_ELT_TYPE) 1 << (from % REGSET_ELT_BITS))) != 0)
1597 basic_block_live_at_start[i][from / REGSET_ELT_BITS]
1598 &= ~ ((REGSET_ELT_TYPE) 1 << (from % REGSET_ELT_BITS));
1599 basic_block_live_at_start[i][to / REGSET_ELT_BITS]
1600 |= ((REGSET_ELT_TYPE) 1 << (to % REGSET_ELT_BITS));
1604 /* Print debugging trace information if -greg switch is given,
1605 showing the information on which the allocation decisions are based. */
1607 static void
1608 dump_conflicts (file)
1609 FILE *file;
1611 register int i;
1612 register int has_preferences;
1613 fprintf (file, ";; %d regs to allocate:", max_allocno);
1614 for (i = 0; i < max_allocno; i++)
1616 int j;
1617 fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1618 for (j = 0; j < max_regno; j++)
1619 if (reg_allocno[j] == allocno_order[i]
1620 && j != allocno_reg[allocno_order[i]])
1621 fprintf (file, "+%d", j);
1622 if (allocno_size[allocno_order[i]] != 1)
1623 fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1625 fprintf (file, "\n");
1627 for (i = 0; i < max_allocno; i++)
1629 register int j;
1630 fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1631 for (j = 0; j < max_allocno; j++)
1632 if (CONFLICTP (i, j) || CONFLICTP (j, i))
1633 fprintf (file, " %d", allocno_reg[j]);
1634 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1635 if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1636 fprintf (file, " %d", j);
1637 fprintf (file, "\n");
1639 has_preferences = 0;
1640 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1641 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1642 has_preferences = 1;
1644 if (! has_preferences)
1645 continue;
1646 fprintf (file, ";; %d preferences:", allocno_reg[i]);
1647 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1648 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1649 fprintf (file, " %d", j);
1650 fprintf (file, "\n");
1652 fprintf (file, "\n");
1655 void
1656 dump_global_regs (file)
1657 FILE *file;
1659 register int i, j;
1661 fprintf (file, ";; Register dispositions:\n");
1662 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1663 if (reg_renumber[i] >= 0)
1665 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1666 if (++j % 6 == 0)
1667 fprintf (file, "\n");
1670 fprintf (file, "\n\n;; Hard regs used: ");
1671 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1672 if (regs_ever_live[i])
1673 fprintf (file, " %d", i);
1674 fprintf (file, "\n\n");