* class.c (check_bitfield_decl): New function, split out from
[official-gcc.git] / gcc / global.c
blob2e460265a95b4958465e17b0a996121496c45aa2
1 /* Allocate registers for pseudo-registers that span basic blocks.
2 Copyright (C) 1987, 88, 91, 94, 96-98, 1999 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 "config.h"
23 #include "system.h"
25 #include "machmode.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "flags.h"
30 #include "basic-block.h"
31 #include "regs.h"
32 #include "function.h"
33 #include "insn-config.h"
34 #include "reload.h"
35 #include "output.h"
36 #include "toplev.h"
38 /* This pass of the compiler performs global register allocation.
39 It assigns hard register numbers to all the pseudo registers
40 that were not handled in local_alloc. Assignments are recorded
41 in the vector reg_renumber, not by changing the rtl code.
42 (Such changes are made by final). The entry point is
43 the function global_alloc.
45 After allocation is complete, the reload pass is run as a subroutine
46 of this pass, so that when a pseudo reg loses its hard reg due to
47 spilling it is possible to make a second attempt to find a hard
48 reg for it. The reload pass is independent in other respects
49 and it is run even when stupid register allocation is in use.
51 1. Assign allocation-numbers (allocnos) to the pseudo-registers
52 still needing allocations and to the pseudo-registers currently
53 allocated by local-alloc which may be spilled by reload.
54 Set up tables reg_allocno and allocno_reg to map
55 reg numbers to allocnos and vice versa.
56 max_allocno gets the number of allocnos in use.
58 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
59 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
60 for conflicts between allocnos and explicit hard register use
61 (which includes use of pseudo-registers allocated by local_alloc).
63 3. For each basic block
64 walk forward through the block, recording which
65 pseudo-registers and which hardware registers are live.
66 Build the conflict matrix between the pseudo-registers
67 and another of pseudo-registers versus hardware registers.
68 Also record the preferred hardware registers
69 for each pseudo-register.
71 4. Sort a table of the allocnos into order of
72 desirability of the variables.
74 5. Allocate the variables in that order; each if possible into
75 a preferred register, else into another register. */
77 /* Number of pseudo-registers which are candidates for allocation. */
79 static int max_allocno;
81 /* Indexed by (pseudo) reg number, gives the allocno, or -1
82 for pseudo registers which are not to be allocated. */
84 static int *reg_allocno;
86 /* Indexed by allocno, gives the reg number. */
88 static int *allocno_reg;
90 /* A vector of the integers from 0 to max_allocno-1,
91 sorted in the order of first-to-be-allocated first. */
93 static int *allocno_order;
95 /* Indexed by an allocno, gives the number of consecutive
96 hard registers needed by that pseudo reg. */
98 static int *allocno_size;
100 /* Indexed by (pseudo) reg number, gives the number of another
101 lower-numbered pseudo reg which can share a hard reg with this pseudo
102 *even if the two pseudos would otherwise appear to conflict*. */
104 static int *reg_may_share;
106 /* Define the number of bits in each element of `conflicts' and what
107 type that element has. We use the largest integer format on the
108 host machine. */
110 #define INT_BITS HOST_BITS_PER_WIDE_INT
111 #define INT_TYPE HOST_WIDE_INT
113 /* max_allocno by max_allocno array of bits,
114 recording whether two allocno's conflict (can't go in the same
115 hardware register).
117 `conflicts' is not symmetric; a conflict between allocno's i and j
118 is recorded either in element i,j or in element j,i. */
120 static INT_TYPE *conflicts;
122 /* Number of ints require to hold max_allocno bits.
123 This is the length of a row in `conflicts'. */
125 static int allocno_row_words;
127 /* Two macros to test or store 1 in an element of `conflicts'. */
129 #define CONFLICTP(I, J) \
130 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
131 & ((INT_TYPE) 1 << ((J) % INT_BITS)))
133 #define SET_CONFLICT(I, J) \
134 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
135 |= ((INT_TYPE) 1 << ((J) % INT_BITS)))
137 /* Set of hard regs currently live (during scan of all insns). */
139 static HARD_REG_SET hard_regs_live;
141 /* Indexed by N, set of hard regs conflicting with allocno N. */
143 static HARD_REG_SET *hard_reg_conflicts;
145 /* Indexed by N, set of hard regs preferred by allocno N.
146 This is used to make allocnos go into regs that are copied to or from them,
147 when possible, to reduce register shuffling. */
149 static HARD_REG_SET *hard_reg_preferences;
151 /* Similar, but just counts register preferences made in simple copy
152 operations, rather than arithmetic. These are given priority because
153 we can always eliminate an insn by using these, but using a register
154 in the above list won't always eliminate an insn. */
156 static HARD_REG_SET *hard_reg_copy_preferences;
158 /* Similar to hard_reg_preferences, but includes bits for subsequent
159 registers when an allocno is multi-word. The above variable is used for
160 allocation while this is used to build reg_someone_prefers, below. */
162 static HARD_REG_SET *hard_reg_full_preferences;
164 /* Indexed by N, set of hard registers that some later allocno has a
165 preference for. */
167 static HARD_REG_SET *regs_someone_prefers;
169 /* Set of registers that global-alloc isn't supposed to use. */
171 static HARD_REG_SET no_global_alloc_regs;
173 /* Set of registers used so far. */
175 static HARD_REG_SET regs_used_so_far;
177 /* Number of calls crossed by each allocno. */
179 static int *allocno_calls_crossed;
181 /* Number of refs (weighted) to each allocno. */
183 static int *allocno_n_refs;
185 /* Guess at live length of each allocno.
186 This is actually the max of the live lengths of the regs. */
188 static int *allocno_live_length;
190 /* Number of refs (weighted) to each hard reg, as used by local alloc.
191 It is zero for a reg that contains global pseudos or is explicitly used. */
193 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
195 /* Guess at live length of each hard reg, as used by local alloc.
196 This is actually the sum of the live lengths of the specific regs. */
198 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
200 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
201 for vector element I, and hard register number J. */
203 #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)
205 /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
207 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)
209 /* Bit mask for allocnos live at current point in the scan. */
211 static INT_TYPE *allocnos_live;
213 /* Test, set or clear bit number I in allocnos_live,
214 a bit vector indexed by allocno. */
216 #define ALLOCNO_LIVE_P(I) \
217 (allocnos_live[(I) / INT_BITS] & ((INT_TYPE) 1 << ((I) % INT_BITS)))
219 #define SET_ALLOCNO_LIVE(I) \
220 (allocnos_live[(I) / INT_BITS] |= ((INT_TYPE) 1 << ((I) % INT_BITS)))
222 #define CLEAR_ALLOCNO_LIVE(I) \
223 (allocnos_live[(I) / INT_BITS] &= ~((INT_TYPE) 1 << ((I) % INT_BITS)))
225 /* This is turned off because it doesn't work right for DImode.
226 (And it is only used for DImode, so the other cases are worthless.)
227 The problem is that it isn't true that there is NO possibility of conflict;
228 only that there is no conflict if the two pseudos get the exact same regs.
229 If they were allocated with a partial overlap, there would be a conflict.
230 We can't safely turn off the conflict unless we have another way to
231 prevent the partial overlap.
233 Idea: change hard_reg_conflicts so that instead of recording which
234 hard regs the allocno may not overlap, it records where the allocno
235 may not start. Change both where it is used and where it is updated.
236 Then there is a way to record that (reg:DI 108) may start at 10
237 but not at 9 or 11. There is still the question of how to record
238 this semi-conflict between two pseudos. */
239 #if 0
240 /* Reg pairs for which conflict after the current insn
241 is inhibited by a REG_NO_CONFLICT note.
242 If the table gets full, we ignore any other notes--that is conservative. */
243 #define NUM_NO_CONFLICT_PAIRS 4
244 /* Number of pairs in use in this insn. */
245 int n_no_conflict_pairs;
246 static struct { int allocno1, allocno2;}
247 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
248 #endif /* 0 */
250 /* Record all regs that are set in any one insn.
251 Communication from mark_reg_{store,clobber} and global_conflicts. */
253 static rtx *regs_set;
254 static int n_regs_set;
256 /* All registers that can be eliminated. */
258 static HARD_REG_SET eliminable_regset;
260 static int allocno_compare PROTO((const PTR, const PTR));
261 static void global_conflicts PROTO((void));
262 static void expand_preferences PROTO((void));
263 static void prune_preferences PROTO((void));
264 static void find_reg PROTO((int, HARD_REG_SET, int, int, int));
265 static void record_one_conflict PROTO((int));
266 static void record_conflicts PROTO((int *, int));
267 static void mark_reg_store PROTO((rtx, rtx, void *));
268 static void mark_reg_clobber PROTO((rtx, rtx, void *));
269 static void mark_reg_conflicts PROTO((rtx));
270 static void mark_reg_death PROTO((rtx));
271 static void mark_reg_live_nc PROTO((int, enum machine_mode));
272 static void set_preference PROTO((rtx, rtx));
273 static void dump_conflicts PROTO((FILE *));
274 static void reg_becomes_live PROTO((rtx, rtx, void *));
275 static void reg_dies PROTO((int, enum machine_mode));
276 static void build_insn_chain PROTO((rtx));
278 /* Perform allocation of pseudo-registers not allocated by local_alloc.
279 FILE is a file to output debugging information on,
280 or zero if such output is not desired.
282 Return value is nonzero if reload failed
283 and we must not do any more for this function. */
286 global_alloc (file)
287 FILE *file;
289 int retval;
290 #ifdef ELIMINABLE_REGS
291 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
292 #endif
293 int need_fp
294 = (! flag_omit_frame_pointer
295 #ifdef EXIT_IGNORE_STACK
296 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
297 #endif
298 || FRAME_POINTER_REQUIRED);
300 register size_t i;
301 rtx x;
303 max_allocno = 0;
305 /* A machine may have certain hard registers that
306 are safe to use only within a basic block. */
308 CLEAR_HARD_REG_SET (no_global_alloc_regs);
309 #ifdef OVERLAPPING_REGNO_P
310 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
311 if (OVERLAPPING_REGNO_P (i))
312 SET_HARD_REG_BIT (no_global_alloc_regs, i);
313 #endif
315 /* Build the regset of all eliminable registers and show we can't use those
316 that we already know won't be eliminated. */
317 #ifdef ELIMINABLE_REGS
318 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
320 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
322 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
323 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
324 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
326 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
327 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
328 if (need_fp)
329 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
330 #endif
332 #else
333 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
334 if (need_fp)
335 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
336 #endif
338 /* Track which registers have already been used. Start with registers
339 explicitly in the rtl, then registers allocated by local register
340 allocation. */
342 CLEAR_HARD_REG_SET (regs_used_so_far);
343 #ifdef LEAF_REGISTERS
344 /* If we are doing the leaf function optimization, and this is a leaf
345 function, it means that the registers that take work to save are those
346 that need a register window. So prefer the ones that can be used in
347 a leaf function. */
349 char *cheap_regs;
350 static char leaf_regs[] = LEAF_REGISTERS;
352 if (only_leaf_regs_used () && leaf_function_p ())
353 cheap_regs = leaf_regs;
354 else
355 cheap_regs = call_used_regs;
356 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
357 if (regs_ever_live[i] || cheap_regs[i])
358 SET_HARD_REG_BIT (regs_used_so_far, i);
360 #else
361 /* We consider registers that do not have to be saved over calls as if
362 they were already used since there is no cost in using them. */
363 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
364 if (regs_ever_live[i] || call_used_regs[i])
365 SET_HARD_REG_BIT (regs_used_so_far, i);
366 #endif
368 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
369 if (reg_renumber[i] >= 0)
370 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
372 /* Establish mappings from register number to allocation number
373 and vice versa. In the process, count the allocnos. */
375 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
377 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
378 reg_allocno[i] = -1;
380 /* Initialize the shared-hard-reg mapping
381 from the list of pairs that may share. */
382 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
383 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
385 int r1 = REGNO (XEXP (x, 0));
386 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
387 if (r1 > r2)
388 reg_may_share[r1] = r2;
389 else
390 reg_may_share[r2] = r1;
393 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
394 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
395 that we are supposed to refrain from putting in a hard reg.
396 -2 means do make an allocno but don't allocate it. */
397 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
398 /* Don't allocate pseudos that cross calls,
399 if this function receives a nonlocal goto. */
400 && (! current_function_has_nonlocal_label
401 || REG_N_CALLS_CROSSED (i) == 0))
403 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
404 reg_allocno[i] = reg_allocno[reg_may_share[i]];
405 else
406 reg_allocno[i] = max_allocno++;
407 if (REG_LIVE_LENGTH (i) == 0)
408 abort ();
410 else
411 reg_allocno[i] = -1;
413 allocno_reg = (int *) xmalloc (max_allocno * sizeof (int));
414 allocno_size = (int *) xcalloc (max_allocno, sizeof (int));
415 allocno_calls_crossed = (int *) xcalloc (max_allocno, sizeof (int));
416 allocno_n_refs = (int *) xcalloc (max_allocno, sizeof (int));
417 allocno_live_length = (int *) xcalloc (max_allocno, sizeof (int));
419 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
420 if (reg_allocno[i] >= 0)
422 int allocno = reg_allocno[i];
423 allocno_reg[allocno] = i;
424 allocno_size[allocno] = PSEUDO_REGNO_SIZE (i);
425 allocno_calls_crossed[allocno] += REG_N_CALLS_CROSSED (i);
426 allocno_n_refs[allocno] += REG_N_REFS (i);
427 if (allocno_live_length[allocno] < REG_LIVE_LENGTH (i))
428 allocno_live_length[allocno] = REG_LIVE_LENGTH (i);
431 /* Calculate amount of usage of each hard reg by pseudos
432 allocated by local-alloc. This is to see if we want to
433 override it. */
434 bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
435 bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
436 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
437 if (reg_renumber[i] >= 0)
439 int regno = reg_renumber[i];
440 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
441 int j;
443 for (j = regno; j < endregno; j++)
445 local_reg_n_refs[j] += REG_N_REFS (i);
446 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
450 /* We can't override local-alloc for a reg used not just by local-alloc. */
451 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
452 if (regs_ever_live[i])
453 local_reg_n_refs[i] = 0;
455 /* Allocate the space for the conflict and preference tables and
456 initialize them. */
458 hard_reg_conflicts
459 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
460 hard_reg_preferences
461 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
462 hard_reg_copy_preferences
463 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
464 hard_reg_full_preferences
465 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
466 regs_someone_prefers
467 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
469 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
471 /* We used to use alloca here, but the size of what it would try to
472 allocate would occasionally cause it to exceed the stack limit and
473 cause unpredictable core dumps. Some examples were > 2Mb in size. */
474 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
475 sizeof (INT_TYPE));
477 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
479 /* If there is work to be done (at least one reg to allocate),
480 perform global conflict analysis and allocate the regs. */
482 if (max_allocno > 0)
484 /* Scan all the insns and compute the conflicts among allocnos
485 and between allocnos and hard regs. */
487 global_conflicts ();
489 /* Eliminate conflicts between pseudos and eliminable registers. If
490 the register is not eliminated, the pseudo won't really be able to
491 live in the eliminable register, so the conflict doesn't matter.
492 If we do eliminate the register, the conflict will no longer exist.
493 So in either case, we can ignore the conflict. Likewise for
494 preferences. */
496 for (i = 0; i < (size_t) max_allocno; i++)
498 AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
499 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
500 eliminable_regset);
501 AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
504 /* Try to expand the preferences by merging them between allocnos. */
506 expand_preferences ();
508 /* Determine the order to allocate the remaining pseudo registers. */
510 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
511 for (i = 0; i < (size_t) max_allocno; i++)
512 allocno_order[i] = i;
514 /* Default the size to 1, since allocno_compare uses it to divide by.
515 Also convert allocno_live_length of zero to -1. A length of zero
516 can occur when all the registers for that allocno have reg_live_length
517 equal to -2. In this case, we want to make an allocno, but not
518 allocate it. So avoid the divide-by-zero and set it to a low
519 priority. */
521 for (i = 0; i < (size_t) max_allocno; i++)
523 if (allocno_size[i] == 0)
524 allocno_size[i] = 1;
525 if (allocno_live_length[i] == 0)
526 allocno_live_length[i] = -1;
529 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
531 prune_preferences ();
533 if (file)
534 dump_conflicts (file);
536 /* Try allocating them, one by one, in that order,
537 except for parameters marked with reg_live_length[regno] == -2. */
539 for (i = 0; i < (size_t) max_allocno; i++)
540 if (reg_renumber[allocno_reg[allocno_order[i]]] < 0
541 && REG_LIVE_LENGTH (allocno_reg[allocno_order[i]]) >= 0)
543 /* If we have more than one register class,
544 first try allocating in the class that is cheapest
545 for this pseudo-reg. If that fails, try any reg. */
546 if (N_REG_CLASSES > 1)
548 find_reg (allocno_order[i], 0, 0, 0, 0);
549 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
550 continue;
552 if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
553 find_reg (allocno_order[i], 0, 1, 0, 0);
556 free (allocno_order);
559 /* Do the reloads now while the allocno data still exist, so that we can
560 try to assign new hard regs to any pseudo regs that are spilled. */
562 #if 0 /* We need to eliminate regs even if there is no rtl code,
563 for the sake of debugging information. */
564 if (n_basic_blocks > 0)
565 #endif
567 build_insn_chain (get_insns ());
568 retval = reload (get_insns (), 1, file);
571 /* Clean up. */
572 free (reg_allocno);
573 free (reg_may_share);
574 free (allocno_reg);
575 free (allocno_size);
576 free (allocno_calls_crossed);
577 free (allocno_n_refs);
578 free (allocno_live_length);
579 free (hard_reg_conflicts);
580 free (hard_reg_preferences);
581 free (hard_reg_copy_preferences);
582 free (hard_reg_full_preferences);
583 free (regs_someone_prefers);
584 free (conflicts);
585 free (allocnos_live);
587 return retval;
590 /* Sort predicate for ordering the allocnos.
591 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
593 static int
594 allocno_compare (v1p, v2p)
595 const PTR v1p;
596 const PTR v2p;
598 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
599 /* Note that the quotient will never be bigger than
600 the value of floor_log2 times the maximum number of
601 times a register can occur in one insn (surely less than 100).
602 Multiplying this by 10000 can't overflow. */
603 register int pri1
604 = (((double) (floor_log2 (allocno_n_refs[v1]) * allocno_n_refs[v1])
605 / allocno_live_length[v1])
606 * 10000 * allocno_size[v1]);
607 register int pri2
608 = (((double) (floor_log2 (allocno_n_refs[v2]) * allocno_n_refs[v2])
609 / allocno_live_length[v2])
610 * 10000 * allocno_size[v2]);
611 if (pri2 - pri1)
612 return pri2 - pri1;
614 /* If regs are equally good, sort by allocno,
615 so that the results of qsort leave nothing to chance. */
616 return v1 - v2;
619 /* Scan the rtl code and record all conflicts and register preferences in the
620 conflict matrices and preference tables. */
622 static void
623 global_conflicts ()
625 register int b, i;
626 register rtx insn;
627 int *block_start_allocnos;
629 /* Make a vector that mark_reg_{store,clobber} will store in. */
630 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
632 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
634 for (b = 0; b < n_basic_blocks; b++)
636 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
638 /* Initialize table of registers currently live
639 to the state at the beginning of this basic block.
640 This also marks the conflicts among them.
642 For pseudo-regs, there is only one bit for each one
643 no matter how many hard regs it occupies.
644 This is ok; we know the size from PSEUDO_REGNO_SIZE.
645 For explicit hard regs, we cannot know the size that way
646 since one hard reg can be used with various sizes.
647 Therefore, we must require that all the hard regs
648 implicitly live as part of a multi-word hard reg
649 are explicitly marked in basic_block_live_at_start. */
652 register regset old = BASIC_BLOCK (b)->global_live_at_start;
653 int ax = 0;
655 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
656 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
658 register int a = reg_allocno[i];
659 if (a >= 0)
661 SET_ALLOCNO_LIVE (a);
662 block_start_allocnos[ax++] = a;
664 else if ((a = reg_renumber[i]) >= 0)
665 mark_reg_live_nc
666 (a, PSEUDO_REGNO_MODE (i));
669 /* Record that each allocno now live conflicts with each other
670 allocno now live, and with each hard reg now live. */
672 record_conflicts (block_start_allocnos, ax);
674 #ifdef STACK_REGS
676 /* Pseudos can't go in stack regs at the start of a basic block
677 that is reached by an abnormal edge. */
679 edge e;
680 for (e = BASIC_BLOCK (b)->pred; e ; e = e->pred_next)
681 if (e->flags & EDGE_ABNORMAL)
682 break;
683 if (e != NULL)
684 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
685 record_one_conflict (ax);
687 #endif
690 insn = BLOCK_HEAD (b);
692 /* Scan the code of this basic block, noting which allocnos
693 and hard regs are born or die. When one is born,
694 record a conflict with all others currently live. */
696 while (1)
698 register RTX_CODE code = GET_CODE (insn);
699 register rtx link;
701 /* Make regs_set an empty set. */
703 n_regs_set = 0;
705 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
708 #if 0
709 int i = 0;
710 for (link = REG_NOTES (insn);
711 link && i < NUM_NO_CONFLICT_PAIRS;
712 link = XEXP (link, 1))
713 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
715 no_conflict_pairs[i].allocno1
716 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
717 no_conflict_pairs[i].allocno2
718 = reg_allocno[REGNO (XEXP (link, 0))];
719 i++;
721 #endif /* 0 */
723 /* Mark any registers clobbered by INSN as live,
724 so they conflict with the inputs. */
726 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
728 /* Mark any registers dead after INSN as dead now. */
730 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
731 if (REG_NOTE_KIND (link) == REG_DEAD)
732 mark_reg_death (XEXP (link, 0));
734 /* Mark any registers set in INSN as live,
735 and mark them as conflicting with all other live regs.
736 Clobbers are processed again, so they conflict with
737 the registers that are set. */
739 note_stores (PATTERN (insn), mark_reg_store, NULL);
741 #ifdef AUTO_INC_DEC
742 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
743 if (REG_NOTE_KIND (link) == REG_INC)
744 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
745 #endif
747 /* If INSN has multiple outputs, then any reg that dies here
748 and is used inside of an output
749 must conflict with the other outputs.
751 It is unsafe to use !single_set here since it will ignore an
752 unused output. Just because an output is unused does not mean
753 the compiler can assume the side effect will not occur.
754 Consider if REG appears in the address of an output and we
755 reload the output. If we allocate REG to the same hard
756 register as an unused output we could set the hard register
757 before the output reload insn. */
758 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
759 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
760 if (REG_NOTE_KIND (link) == REG_DEAD)
762 int used_in_output = 0;
763 int i;
764 rtx reg = XEXP (link, 0);
766 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
768 rtx set = XVECEXP (PATTERN (insn), 0, i);
769 if (GET_CODE (set) == SET
770 && GET_CODE (SET_DEST (set)) != REG
771 && !rtx_equal_p (reg, SET_DEST (set))
772 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
773 used_in_output = 1;
775 if (used_in_output)
776 mark_reg_conflicts (reg);
779 /* Mark any registers set in INSN and then never used. */
781 while (n_regs_set > 0)
782 if (find_regno_note (insn, REG_UNUSED,
783 REGNO (regs_set[--n_regs_set])))
784 mark_reg_death (regs_set[n_regs_set]);
787 if (insn == BLOCK_END (b))
788 break;
789 insn = NEXT_INSN (insn);
793 /* Clean up. */
794 free (block_start_allocnos);
795 free (regs_set);
797 /* Expand the preference information by looking for cases where one allocno
798 dies in an insn that sets an allocno. If those two allocnos don't conflict,
799 merge any preferences between those allocnos. */
801 static void
802 expand_preferences ()
804 rtx insn;
805 rtx link;
806 rtx set;
808 /* We only try to handle the most common cases here. Most of the cases
809 where this wins are reg-reg copies. */
811 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
812 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
813 && (set = single_set (insn)) != 0
814 && GET_CODE (SET_DEST (set)) == REG
815 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
816 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
817 if (REG_NOTE_KIND (link) == REG_DEAD
818 && GET_CODE (XEXP (link, 0)) == REG
819 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
820 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
821 reg_allocno[REGNO (XEXP (link, 0))])
822 && ! CONFLICTP (reg_allocno[REGNO (XEXP (link, 0))],
823 reg_allocno[REGNO (SET_DEST (set))]))
825 int a1 = reg_allocno[REGNO (SET_DEST (set))];
826 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
828 if (XEXP (link, 0) == SET_SRC (set))
830 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
831 hard_reg_copy_preferences[a2]);
832 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
833 hard_reg_copy_preferences[a1]);
836 IOR_HARD_REG_SET (hard_reg_preferences[a1],
837 hard_reg_preferences[a2]);
838 IOR_HARD_REG_SET (hard_reg_preferences[a2],
839 hard_reg_preferences[a1]);
840 IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
841 hard_reg_full_preferences[a2]);
842 IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
843 hard_reg_full_preferences[a1]);
847 /* Prune the preferences for global registers to exclude registers that cannot
848 be used.
850 Compute `regs_someone_prefers', which is a bitmask of the hard registers
851 that are preferred by conflicting registers of lower priority. If possible,
852 we will avoid using these registers. */
854 static void
855 prune_preferences ()
857 int i, j;
858 int allocno;
860 /* Scan least most important to most important.
861 For each allocno, remove from preferences registers that cannot be used,
862 either because of conflicts or register type. Then compute all registers
863 preferred by each lower-priority register that conflicts. */
865 for (i = max_allocno - 1; i >= 0; i--)
867 HARD_REG_SET temp, temp2;
869 allocno = allocno_order[i];
870 COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
872 if (allocno_calls_crossed[allocno] == 0)
873 IOR_HARD_REG_SET (temp, fixed_reg_set);
874 else
875 IOR_HARD_REG_SET (temp, call_used_reg_set);
877 IOR_COMPL_HARD_REG_SET
878 (temp,
879 reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
881 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
882 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
883 AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
885 /* Merge in the preferences of lower-priority registers (they have
886 already been pruned). If we also prefer some of those registers,
887 don't exclude them unless we are of a smaller size (in which case
888 we want to give the lower-priority allocno the first chance for
889 these registers). */
890 CLEAR_HARD_REG_SET (temp);
891 CLEAR_HARD_REG_SET (temp2);
892 for (j = i + 1; j < max_allocno; j++)
893 if (CONFLICTP (allocno, allocno_order[j])
894 || CONFLICTP (allocno_order[j], allocno))
896 if (allocno_size[allocno_order[j]] <= allocno_size[allocno])
897 IOR_HARD_REG_SET (temp,
898 hard_reg_full_preferences[allocno_order[j]]);
899 else
900 IOR_HARD_REG_SET (temp2,
901 hard_reg_full_preferences[allocno_order[j]]);
903 AND_COMPL_HARD_REG_SET (temp, hard_reg_full_preferences[allocno]);
904 IOR_HARD_REG_SET (temp, temp2);
905 COPY_HARD_REG_SET (regs_someone_prefers[allocno], temp);
909 /* Assign a hard register to ALLOCNO; look for one that is the beginning
910 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
911 The registers marked in PREFREGS are tried first.
913 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
914 be used for this allocation.
916 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
917 Otherwise ignore that preferred class and use the alternate class.
919 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
920 will have to be saved and restored at calls.
922 RETRYING is nonzero if this is called from retry_global_alloc.
924 If we find one, record it in reg_renumber.
925 If not, do nothing. */
927 static void
928 find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
929 int allocno;
930 HARD_REG_SET losers;
931 int alt_regs_p;
932 int accept_call_clobbered;
933 int retrying;
935 register int i, best_reg, pass;
936 #ifdef HARD_REG_SET
937 register /* Declare it register if it's a scalar. */
938 #endif
939 HARD_REG_SET used, used1, used2;
941 enum reg_class class = (alt_regs_p
942 ? reg_alternate_class (allocno_reg[allocno])
943 : reg_preferred_class (allocno_reg[allocno]));
944 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
946 if (accept_call_clobbered)
947 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
948 else if (allocno_calls_crossed[allocno] == 0)
949 COPY_HARD_REG_SET (used1, fixed_reg_set);
950 else
951 COPY_HARD_REG_SET (used1, call_used_reg_set);
953 /* Some registers should not be allocated in global-alloc. */
954 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
955 if (losers)
956 IOR_HARD_REG_SET (used1, losers);
958 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
959 COPY_HARD_REG_SET (used2, used1);
961 IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
963 #ifdef CLASS_CANNOT_CHANGE_SIZE
964 if (REG_CHANGES_SIZE (allocno_reg[allocno]))
965 IOR_HARD_REG_SET (used1,
966 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
967 #endif
969 /* Try each hard reg to see if it fits. Do this in two passes.
970 In the first pass, skip registers that are preferred by some other pseudo
971 to give it a better chance of getting one of those registers. Only if
972 we can't get a register when excluding those do we take one of them.
973 However, we never allocate a register for the first time in pass 0. */
975 COPY_HARD_REG_SET (used, used1);
976 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
977 IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
979 best_reg = -1;
980 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
981 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
982 pass++)
984 if (pass == 1)
985 COPY_HARD_REG_SET (used, used1);
986 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
988 #ifdef REG_ALLOC_ORDER
989 int regno = reg_alloc_order[i];
990 #else
991 int regno = i;
992 #endif
993 if (! TEST_HARD_REG_BIT (used, regno)
994 && HARD_REGNO_MODE_OK (regno, mode)
995 && (allocno_calls_crossed[allocno] == 0
996 || accept_call_clobbered
997 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
999 register int j;
1000 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
1001 for (j = regno + 1;
1002 (j < lim
1003 && ! TEST_HARD_REG_BIT (used, j));
1004 j++);
1005 if (j == lim)
1007 best_reg = regno;
1008 break;
1010 #ifndef REG_ALLOC_ORDER
1011 i = j; /* Skip starting points we know will lose */
1012 #endif
1017 /* See if there is a preferred register with the same class as the register
1018 we allocated above. Making this restriction prevents register
1019 preferencing from creating worse register allocation.
1021 Remove from the preferred registers and conflicting registers. Note that
1022 additional conflicts may have been added after `prune_preferences' was
1023 called.
1025 First do this for those register with copy preferences, then all
1026 preferred registers. */
1028 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
1029 GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
1030 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1032 if (best_reg >= 0)
1034 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1035 if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
1036 && HARD_REGNO_MODE_OK (i, mode)
1037 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1038 || reg_class_subset_p (REGNO_REG_CLASS (i),
1039 REGNO_REG_CLASS (best_reg))
1040 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1041 REGNO_REG_CLASS (i))))
1043 register int j;
1044 register int lim = i + HARD_REGNO_NREGS (i, mode);
1045 for (j = i + 1;
1046 (j < lim
1047 && ! TEST_HARD_REG_BIT (used, j)
1048 && (REGNO_REG_CLASS (j)
1049 == REGNO_REG_CLASS (best_reg + (j - i))
1050 || reg_class_subset_p (REGNO_REG_CLASS (j),
1051 REGNO_REG_CLASS (best_reg + (j - i)))
1052 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1053 REGNO_REG_CLASS (j))));
1054 j++);
1055 if (j == lim)
1057 best_reg = i;
1058 goto no_prefs;
1062 no_copy_prefs:
1064 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1065 GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1066 reg_class_contents[(int) NO_REGS], no_prefs);
1068 if (best_reg >= 0)
1070 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1071 if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1072 && HARD_REGNO_MODE_OK (i, mode)
1073 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1074 || reg_class_subset_p (REGNO_REG_CLASS (i),
1075 REGNO_REG_CLASS (best_reg))
1076 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1077 REGNO_REG_CLASS (i))))
1079 register int j;
1080 register int lim = i + HARD_REGNO_NREGS (i, mode);
1081 for (j = i + 1;
1082 (j < lim
1083 && ! TEST_HARD_REG_BIT (used, j)
1084 && (REGNO_REG_CLASS (j)
1085 == REGNO_REG_CLASS (best_reg + (j - i))
1086 || reg_class_subset_p (REGNO_REG_CLASS (j),
1087 REGNO_REG_CLASS (best_reg + (j - i)))
1088 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1089 REGNO_REG_CLASS (j))));
1090 j++);
1091 if (j == lim)
1093 best_reg = i;
1094 break;
1098 no_prefs:
1100 /* If we haven't succeeded yet, try with caller-saves.
1101 We need not check to see if the current function has nonlocal
1102 labels because we don't put any pseudos that are live over calls in
1103 registers in that case. */
1105 if (flag_caller_saves && best_reg < 0)
1107 /* Did not find a register. If it would be profitable to
1108 allocate a call-clobbered register and save and restore it
1109 around calls, do that. */
1110 if (! accept_call_clobbered
1111 && allocno_calls_crossed[allocno] != 0
1112 && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1113 allocno_calls_crossed[allocno]))
1115 HARD_REG_SET new_losers;
1116 if (! losers)
1117 CLEAR_HARD_REG_SET (new_losers);
1118 else
1119 COPY_HARD_REG_SET (new_losers, losers);
1121 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1122 find_reg (allocno, new_losers, alt_regs_p, 1, retrying);
1123 if (reg_renumber[allocno_reg[allocno]] >= 0)
1125 caller_save_needed = 1;
1126 return;
1131 /* If we haven't succeeded yet,
1132 see if some hard reg that conflicts with us
1133 was utilized poorly by local-alloc.
1134 If so, kick out the regs that were put there by local-alloc
1135 so we can use it instead. */
1136 if (best_reg < 0 && !retrying
1137 /* Let's not bother with multi-reg allocnos. */
1138 && allocno_size[allocno] == 1)
1140 /* Count from the end, to find the least-used ones first. */
1141 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1143 #ifdef REG_ALLOC_ORDER
1144 int regno = reg_alloc_order[i];
1145 #else
1146 int regno = i;
1147 #endif
1149 if (local_reg_n_refs[regno] != 0
1150 /* Don't use a reg no good for this pseudo. */
1151 && ! TEST_HARD_REG_BIT (used2, regno)
1152 && HARD_REGNO_MODE_OK (regno, mode)
1153 #ifdef CLASS_CANNOT_CHANGE_SIZE
1154 && ! (REG_CHANGES_SIZE (allocno_reg[allocno])
1155 && (TEST_HARD_REG_BIT
1156 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1157 regno)))
1158 #endif
1161 /* We explicitly evaluate the divide results into temporary
1162 variables so as to avoid excess precision problems that occur
1163 on a i386-unknown-sysv4.2 (unixware) host. */
1165 double tmp1 = ((double) local_reg_n_refs[regno]
1166 / local_reg_live_length[regno]);
1167 double tmp2 = ((double) allocno_n_refs[allocno]
1168 / allocno_live_length[allocno]);
1170 if (tmp1 < tmp2)
1172 /* Hard reg REGNO was used less in total by local regs
1173 than it would be used by this one allocno! */
1174 int k;
1175 for (k = 0; k < max_regno; k++)
1176 if (reg_renumber[k] >= 0)
1178 int r = reg_renumber[k];
1179 int endregno
1180 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1182 if (regno >= r && regno < endregno)
1183 reg_renumber[k] = -1;
1186 best_reg = regno;
1187 break;
1193 /* Did we find a register? */
1195 if (best_reg >= 0)
1197 register int lim, j;
1198 HARD_REG_SET this_reg;
1200 /* Yes. Record it as the hard register of this pseudo-reg. */
1201 reg_renumber[allocno_reg[allocno]] = best_reg;
1202 /* Also of any pseudo-regs that share with it. */
1203 if (reg_may_share[allocno_reg[allocno]])
1204 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1205 if (reg_allocno[j] == allocno)
1206 reg_renumber[j] = best_reg;
1208 /* Make a set of the hard regs being allocated. */
1209 CLEAR_HARD_REG_SET (this_reg);
1210 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1211 for (j = best_reg; j < lim; j++)
1213 SET_HARD_REG_BIT (this_reg, j);
1214 SET_HARD_REG_BIT (regs_used_so_far, j);
1215 /* This is no longer a reg used just by local regs. */
1216 local_reg_n_refs[j] = 0;
1218 /* For each other pseudo-reg conflicting with this one,
1219 mark it as conflicting with the hard regs this one occupies. */
1220 lim = allocno;
1221 for (j = 0; j < max_allocno; j++)
1222 if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
1224 IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1229 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1230 Perhaps it had previously seemed not worth a hard reg,
1231 or perhaps its old hard reg has been commandeered for reloads.
1232 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1233 they do not appear to be allocated.
1234 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1236 void
1237 retry_global_alloc (regno, forbidden_regs)
1238 int regno;
1239 HARD_REG_SET forbidden_regs;
1241 int allocno = reg_allocno[regno];
1242 if (allocno >= 0)
1244 /* If we have more than one register class,
1245 first try allocating in the class that is cheapest
1246 for this pseudo-reg. If that fails, try any reg. */
1247 if (N_REG_CLASSES > 1)
1248 find_reg (allocno, forbidden_regs, 0, 0, 1);
1249 if (reg_renumber[regno] < 0
1250 && reg_alternate_class (regno) != NO_REGS)
1251 find_reg (allocno, forbidden_regs, 1, 0, 1);
1253 /* If we found a register, modify the RTL for the register to
1254 show the hard register, and mark that register live. */
1255 if (reg_renumber[regno] >= 0)
1257 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1258 mark_home_live (regno);
1263 /* Record a conflict between register REGNO
1264 and everything currently live.
1265 REGNO must not be a pseudo reg that was allocated
1266 by local_alloc; such numbers must be translated through
1267 reg_renumber before calling here. */
1269 static void
1270 record_one_conflict (regno)
1271 int regno;
1273 register int j;
1275 if (regno < FIRST_PSEUDO_REGISTER)
1276 /* When a hard register becomes live,
1277 record conflicts with live pseudo regs. */
1278 for (j = 0; j < max_allocno; j++)
1280 if (ALLOCNO_LIVE_P (j))
1281 SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1283 else
1284 /* When a pseudo-register becomes live,
1285 record conflicts first with hard regs,
1286 then with other pseudo regs. */
1288 register int ialloc = reg_allocno[regno];
1289 register int ialloc_prod = ialloc * allocno_row_words;
1290 IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1291 for (j = allocno_row_words - 1; j >= 0; j--)
1293 #if 0
1294 int k;
1295 for (k = 0; k < n_no_conflict_pairs; k++)
1296 if (! ((j == no_conflict_pairs[k].allocno1
1297 && ialloc == no_conflict_pairs[k].allocno2)
1299 (j == no_conflict_pairs[k].allocno2
1300 && ialloc == no_conflict_pairs[k].allocno1)))
1301 #endif /* 0 */
1302 conflicts[ialloc_prod + j] |= allocnos_live[j];
1307 /* Record all allocnos currently live as conflicting
1308 with each other and with all hard regs currently live.
1309 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1310 are currently live. Their bits are also flagged in allocnos_live. */
1312 static void
1313 record_conflicts (allocno_vec, len)
1314 register int *allocno_vec;
1315 register int len;
1317 register int allocno;
1318 register int j;
1319 register int ialloc_prod;
1321 while (--len >= 0)
1323 allocno = allocno_vec[len];
1324 ialloc_prod = allocno * allocno_row_words;
1325 IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1326 for (j = allocno_row_words - 1; j >= 0; j--)
1327 conflicts[ialloc_prod + j] |= allocnos_live[j];
1331 /* Handle the case where REG is set by the insn being scanned,
1332 during the forward scan to accumulate conflicts.
1333 Store a 1 in regs_live or allocnos_live for this register, record how many
1334 consecutive hardware registers it actually needs,
1335 and record a conflict with all other registers already live.
1337 Note that even if REG does not remain alive after this insn,
1338 we must mark it here as live, to ensure a conflict between
1339 REG and any other regs set in this insn that really do live.
1340 This is because those other regs could be considered after this.
1342 REG might actually be something other than a register;
1343 if so, we do nothing.
1345 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1346 a REG_INC note was found for it). */
1348 static void
1349 mark_reg_store (reg, setter, data)
1350 rtx reg, setter;
1351 void *data ATTRIBUTE_UNUSED;
1353 register int regno;
1355 /* WORD is which word of a multi-register group is being stored.
1356 For the case where the store is actually into a SUBREG of REG.
1357 Except we don't use it; I believe the entire REG needs to be
1358 made live. */
1359 int word = 0;
1361 if (GET_CODE (reg) == SUBREG)
1363 word = SUBREG_WORD (reg);
1364 reg = SUBREG_REG (reg);
1367 if (GET_CODE (reg) != REG)
1368 return;
1370 regs_set[n_regs_set++] = reg;
1372 if (setter && GET_CODE (setter) != CLOBBER)
1373 set_preference (reg, SET_SRC (setter));
1375 regno = REGNO (reg);
1377 /* Either this is one of the max_allocno pseudo regs not allocated,
1378 or it is or has a hardware reg. First handle the pseudo-regs. */
1379 if (regno >= FIRST_PSEUDO_REGISTER)
1381 if (reg_allocno[regno] >= 0)
1383 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1384 record_one_conflict (regno);
1388 if (reg_renumber[regno] >= 0)
1389 regno = reg_renumber[regno] /* + word */;
1391 /* Handle hardware regs (and pseudos allocated to hard regs). */
1392 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1394 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1395 while (regno < last)
1397 record_one_conflict (regno);
1398 SET_HARD_REG_BIT (hard_regs_live, regno);
1399 regno++;
1404 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1406 static void
1407 mark_reg_clobber (reg, setter, data)
1408 rtx reg, setter;
1409 void *data ATTRIBUTE_UNUSED;
1411 if (GET_CODE (setter) == CLOBBER)
1412 mark_reg_store (reg, setter, data);
1415 /* Record that REG has conflicts with all the regs currently live.
1416 Do not mark REG itself as live. */
1418 static void
1419 mark_reg_conflicts (reg)
1420 rtx reg;
1422 register int regno;
1424 if (GET_CODE (reg) == SUBREG)
1425 reg = SUBREG_REG (reg);
1427 if (GET_CODE (reg) != REG)
1428 return;
1430 regno = REGNO (reg);
1432 /* Either this is one of the max_allocno pseudo regs not allocated,
1433 or it is or has a hardware reg. First handle the pseudo-regs. */
1434 if (regno >= FIRST_PSEUDO_REGISTER)
1436 if (reg_allocno[regno] >= 0)
1437 record_one_conflict (regno);
1440 if (reg_renumber[regno] >= 0)
1441 regno = reg_renumber[regno];
1443 /* Handle hardware regs (and pseudos allocated to hard regs). */
1444 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1446 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1447 while (regno < last)
1449 record_one_conflict (regno);
1450 regno++;
1455 /* Mark REG as being dead (following the insn being scanned now).
1456 Store a 0 in regs_live or allocnos_live for this register. */
1458 static void
1459 mark_reg_death (reg)
1460 rtx reg;
1462 register int regno = REGNO (reg);
1464 /* Either this is one of the max_allocno pseudo regs not allocated,
1465 or it is a hardware reg. First handle the pseudo-regs. */
1466 if (regno >= FIRST_PSEUDO_REGISTER)
1468 if (reg_allocno[regno] >= 0)
1469 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1472 /* For pseudo reg, see if it has been assigned a hardware reg. */
1473 if (reg_renumber[regno] >= 0)
1474 regno = reg_renumber[regno];
1476 /* Handle hardware regs (and pseudos allocated to hard regs). */
1477 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1479 /* Pseudo regs already assigned hardware regs are treated
1480 almost the same as explicit hardware regs. */
1481 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1482 while (regno < last)
1484 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1485 regno++;
1490 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1491 for the value stored in it. MODE determines how many consecutive
1492 registers are actually in use. Do not record conflicts;
1493 it is assumed that the caller will do that. */
1495 static void
1496 mark_reg_live_nc (regno, mode)
1497 register int regno;
1498 enum machine_mode mode;
1500 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1501 while (regno < last)
1503 SET_HARD_REG_BIT (hard_regs_live, regno);
1504 regno++;
1508 /* Try to set a preference for an allocno to a hard register.
1509 We are passed DEST and SRC which are the operands of a SET. It is known
1510 that SRC is a register. If SRC or the first operand of SRC is a register,
1511 try to set a preference. If one of the two is a hard register and the other
1512 is a pseudo-register, mark the preference.
1514 Note that we are not as aggressive as local-alloc in trying to tie a
1515 pseudo-register to a hard register. */
1517 static void
1518 set_preference (dest, src)
1519 rtx dest, src;
1521 int src_regno, dest_regno;
1522 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1523 to compensate for subregs in SRC or DEST. */
1524 int offset = 0;
1525 int i;
1526 int copy = 1;
1528 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1529 src = XEXP (src, 0), copy = 0;
1531 /* Get the reg number for both SRC and DEST.
1532 If neither is a reg, give up. */
1534 if (GET_CODE (src) == REG)
1535 src_regno = REGNO (src);
1536 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1538 src_regno = REGNO (SUBREG_REG (src));
1539 offset += SUBREG_WORD (src);
1541 else
1542 return;
1544 if (GET_CODE (dest) == REG)
1545 dest_regno = REGNO (dest);
1546 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1548 dest_regno = REGNO (SUBREG_REG (dest));
1549 offset -= SUBREG_WORD (dest);
1551 else
1552 return;
1554 /* Convert either or both to hard reg numbers. */
1556 if (reg_renumber[src_regno] >= 0)
1557 src_regno = reg_renumber[src_regno];
1559 if (reg_renumber[dest_regno] >= 0)
1560 dest_regno = reg_renumber[dest_regno];
1562 /* Now if one is a hard reg and the other is a global pseudo
1563 then give the other a preference. */
1565 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1566 && reg_allocno[src_regno] >= 0)
1568 dest_regno -= offset;
1569 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1571 if (copy)
1572 SET_REGBIT (hard_reg_copy_preferences,
1573 reg_allocno[src_regno], dest_regno);
1575 SET_REGBIT (hard_reg_preferences,
1576 reg_allocno[src_regno], dest_regno);
1577 for (i = dest_regno;
1578 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1579 i++)
1580 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1584 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1585 && reg_allocno[dest_regno] >= 0)
1587 src_regno += offset;
1588 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1590 if (copy)
1591 SET_REGBIT (hard_reg_copy_preferences,
1592 reg_allocno[dest_regno], src_regno);
1594 SET_REGBIT (hard_reg_preferences,
1595 reg_allocno[dest_regno], src_regno);
1596 for (i = src_regno;
1597 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1598 i++)
1599 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1604 /* Indicate that hard register number FROM was eliminated and replaced with
1605 an offset from hard register number TO. The status of hard registers live
1606 at the start of a basic block is updated by replacing a use of FROM with
1607 a use of TO. */
1609 void
1610 mark_elimination (from, to)
1611 int from, to;
1613 int i;
1615 for (i = 0; i < n_basic_blocks; i++)
1617 register regset r = BASIC_BLOCK (i)->global_live_at_start;
1618 if (REGNO_REG_SET_P (r, from))
1620 CLEAR_REGNO_REG_SET (r, from);
1621 SET_REGNO_REG_SET (r, to);
1626 /* Used for communication between the following functions. Holds the
1627 current life information. */
1628 static regset live_relevant_regs;
1630 /* Record in live_relevant_regs that register REG became live. This
1631 is called via note_stores. */
1632 static void
1633 reg_becomes_live (reg, setter, data)
1634 rtx reg;
1635 rtx setter ATTRIBUTE_UNUSED;
1636 void *data ATTRIBUTE_UNUSED;
1638 int regno;
1640 if (GET_CODE (reg) == SUBREG)
1641 reg = SUBREG_REG (reg);
1643 if (GET_CODE (reg) != REG)
1644 return;
1646 regno = REGNO (reg);
1647 if (regno < FIRST_PSEUDO_REGISTER)
1649 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1650 while (nregs-- > 0)
1651 SET_REGNO_REG_SET (live_relevant_regs, regno++);
1653 else if (reg_renumber[regno] >= 0)
1654 SET_REGNO_REG_SET (live_relevant_regs, regno);
1657 /* Record in live_relevant_regs that register REGNO died. */
1658 static void
1659 reg_dies (regno, mode)
1660 int regno;
1661 enum machine_mode mode;
1663 if (regno < FIRST_PSEUDO_REGISTER)
1665 int nregs = HARD_REGNO_NREGS (regno, mode);
1666 while (nregs-- > 0)
1667 CLEAR_REGNO_REG_SET (live_relevant_regs, regno++);
1669 else
1670 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1673 /* Walk the insns of the current function and build reload_insn_chain,
1674 and record register life information. */
1675 static void
1676 build_insn_chain (first)
1677 rtx first;
1679 struct insn_chain **p = &reload_insn_chain;
1680 struct insn_chain *prev = 0;
1681 int b = 0;
1683 live_relevant_regs = ALLOCA_REG_SET ();
1685 for (; first; first = NEXT_INSN (first))
1687 struct insn_chain *c;
1689 if (first == BLOCK_HEAD (b))
1691 int i;
1692 CLEAR_REG_SET (live_relevant_regs);
1693 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1694 if (REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start, i)
1695 && ! TEST_HARD_REG_BIT (eliminable_regset, i))
1696 SET_REGNO_REG_SET (live_relevant_regs, i);
1698 for (; i < max_regno; i++)
1699 if (reg_renumber[i] >= 0
1700 && REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start, i))
1701 SET_REGNO_REG_SET (live_relevant_regs, i);
1704 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1706 c = new_insn_chain ();
1707 c->prev = prev;
1708 prev = c;
1709 *p = c;
1710 p = &c->next;
1711 c->insn = first;
1712 c->block = b;
1714 COPY_REG_SET (c->live_before, live_relevant_regs);
1716 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1718 rtx link;
1720 /* Mark the death of everything that dies in this instruction. */
1722 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1723 if (REG_NOTE_KIND (link) == REG_DEAD
1724 && GET_CODE (XEXP (link, 0)) == REG)
1725 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1727 /* Mark everything born in this instruction as live. */
1729 note_stores (PATTERN (first), reg_becomes_live, NULL);
1732 /* Remember which registers are live at the end of the insn, before
1733 killing those with REG_UNUSED notes. */
1734 COPY_REG_SET (c->live_after, live_relevant_regs);
1736 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1738 rtx link;
1740 /* Mark anything that is set in this insn and then unused as dying. */
1742 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1743 if (REG_NOTE_KIND (link) == REG_UNUSED
1744 && GET_CODE (XEXP (link, 0)) == REG)
1745 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1749 if (first == BLOCK_END (b))
1750 b++;
1752 /* Stop after we pass the end of the last basic block. Verify that
1753 no real insns are after the end of the last basic block.
1755 We may want to reorganize the loop somewhat since this test should
1756 always be the right exit test. */
1757 if (b == n_basic_blocks)
1759 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1760 if (GET_RTX_CLASS (GET_CODE (first)) == 'i'
1761 && GET_CODE (PATTERN (first)) != USE)
1762 abort ();
1763 break;
1766 FREE_REG_SET (live_relevant_regs);
1767 *p = 0;
1770 /* Print debugging trace information if -dg switch is given,
1771 showing the information on which the allocation decisions are based. */
1773 static void
1774 dump_conflicts (file)
1775 FILE *file;
1777 register int i;
1778 register int has_preferences;
1779 register int nregs;
1780 nregs = 0;
1781 for (i = 0; i < max_allocno; i++)
1783 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
1784 continue;
1785 nregs++;
1787 fprintf (file, ";; %d regs to allocate:", nregs);
1788 for (i = 0; i < max_allocno; i++)
1790 int j;
1791 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
1792 continue;
1793 fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1794 for (j = 0; j < max_regno; j++)
1795 if (reg_allocno[j] == allocno_order[i]
1796 && j != allocno_reg[allocno_order[i]])
1797 fprintf (file, "+%d", j);
1798 if (allocno_size[allocno_order[i]] != 1)
1799 fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1801 fprintf (file, "\n");
1803 for (i = 0; i < max_allocno; i++)
1805 register int j;
1806 fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1807 for (j = 0; j < max_allocno; j++)
1808 if (CONFLICTP (i, j) || CONFLICTP (j, i))
1809 fprintf (file, " %d", allocno_reg[j]);
1810 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1811 if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1812 fprintf (file, " %d", j);
1813 fprintf (file, "\n");
1815 has_preferences = 0;
1816 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1817 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1818 has_preferences = 1;
1820 if (! has_preferences)
1821 continue;
1822 fprintf (file, ";; %d preferences:", allocno_reg[i]);
1823 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1824 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1825 fprintf (file, " %d", j);
1826 fprintf (file, "\n");
1828 fprintf (file, "\n");
1831 void
1832 dump_global_regs (file)
1833 FILE *file;
1835 register int i, j;
1837 fprintf (file, ";; Register dispositions:\n");
1838 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1839 if (reg_renumber[i] >= 0)
1841 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1842 if (++j % 6 == 0)
1843 fprintf (file, "\n");
1846 fprintf (file, "\n\n;; Hard regs used: ");
1847 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1848 if (regs_ever_live[i])
1849 fprintf (file, " %d", i);
1850 fprintf (file, "\n\n");