2000-05-02 Jeff Sturm <jsturm@one-point.com>
[official-gcc.git] / gcc / global.c
blob2f757bd9ade77394e2ca378e94af03ffe9b2e1d9
1 /* Allocate registers for pseudo-registers that span basic blocks.
2 Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
26 #include "machmode.h"
27 #include "hard-reg-set.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "regs.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "reload.h"
36 #include "output.h"
37 #include "toplev.h"
39 /* This pass of the compiler performs global register allocation.
40 It assigns hard register numbers to all the pseudo registers
41 that were not handled in local_alloc. Assignments are recorded
42 in the vector reg_renumber, not by changing the rtl code.
43 (Such changes are made by final). The entry point is
44 the function global_alloc.
46 After allocation is complete, the reload pass is run as a subroutine
47 of this pass, so that when a pseudo reg loses its hard reg due to
48 spilling it is possible to make a second attempt to find a hard
49 reg for it. The reload pass is independent in other respects
50 and it is run even when stupid register allocation is in use.
52 1. Assign allocation-numbers (allocnos) to the pseudo-registers
53 still needing allocations and to the pseudo-registers currently
54 allocated by local-alloc which may be spilled by reload.
55 Set up tables reg_allocno and allocno_reg to map
56 reg numbers to allocnos and vice versa.
57 max_allocno gets the number of allocnos in use.
59 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
60 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
61 for conflicts between allocnos and explicit hard register use
62 (which includes use of pseudo-registers allocated by local_alloc).
64 3. For each basic block
65 walk forward through the block, recording which
66 pseudo-registers and which hardware registers are live.
67 Build the conflict matrix between the pseudo-registers
68 and another of pseudo-registers versus hardware registers.
69 Also record the preferred hardware registers
70 for each pseudo-register.
72 4. Sort a table of the allocnos into order of
73 desirability of the variables.
75 5. Allocate the variables in that order; each if possible into
76 a preferred register, else into another register. */
78 /* Number of pseudo-registers which are candidates for allocation. */
80 static int max_allocno;
82 /* Indexed by (pseudo) reg number, gives the allocno, or -1
83 for pseudo registers which are not to be allocated. */
85 static int *reg_allocno;
87 struct allocno
89 int reg;
90 /* Gives the number of consecutive hard registers needed by that
91 pseudo reg. */
92 int size;
94 /* Number of calls crossed by each allocno. */
95 int calls_crossed;
97 /* Number of refs (weighted) to each allocno. */
98 int n_refs;
100 /* Guess at live length of each allocno.
101 This is actually the max of the live lengths of the regs. */
102 int live_length;
104 /* Set of hard regs conflicting with allocno N. */
106 HARD_REG_SET hard_reg_conflicts;
108 /* Set of hard regs preferred by allocno N.
109 This is used to make allocnos go into regs that are copied to or from them,
110 when possible, to reduce register shuffling. */
112 HARD_REG_SET hard_reg_preferences;
114 /* Similar, but just counts register preferences made in simple copy
115 operations, rather than arithmetic. These are given priority because
116 we can always eliminate an insn by using these, but using a register
117 in the above list won't always eliminate an insn. */
119 HARD_REG_SET hard_reg_copy_preferences;
121 /* Similar to hard_reg_preferences, but includes bits for subsequent
122 registers when an allocno is multi-word. The above variable is used for
123 allocation while this is used to build reg_someone_prefers, below. */
125 HARD_REG_SET hard_reg_full_preferences;
127 /* Set of hard registers that some later allocno has a preference for. */
129 HARD_REG_SET regs_someone_prefers;
132 static struct allocno *allocno;
134 /* A vector of the integers from 0 to max_allocno-1,
135 sorted in the order of first-to-be-allocated first. */
137 static int *allocno_order;
139 /* Indexed by (pseudo) reg number, gives the number of another
140 lower-numbered pseudo reg which can share a hard reg with this pseudo
141 *even if the two pseudos would otherwise appear to conflict*. */
143 static int *reg_may_share;
145 /* Define the number of bits in each element of `conflicts' and what
146 type that element has. We use the largest integer format on the
147 host machine. */
149 #define INT_BITS HOST_BITS_PER_WIDE_INT
150 #define INT_TYPE HOST_WIDE_INT
152 /* max_allocno by max_allocno array of bits,
153 recording whether two allocno's conflict (can't go in the same
154 hardware register).
156 `conflicts' is symmetric after the call to mirror_conflicts. */
158 static INT_TYPE *conflicts;
160 /* Number of ints require to hold max_allocno bits.
161 This is the length of a row in `conflicts'. */
163 static int allocno_row_words;
165 /* Two macros to test or store 1 in an element of `conflicts'. */
167 #define CONFLICTP(I, J) \
168 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
169 & ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
171 #define SET_CONFLICT(I, J) \
172 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
173 |= ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
175 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
176 and execute CODE. */
177 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
178 do { \
179 int i_; \
180 int allocno_; \
181 INT_TYPE *p_ = (ALLOCNO_SET); \
183 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
184 i_--, allocno_ += INT_BITS) \
186 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
188 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
190 if (word_ & 1) \
191 {CODE;} \
194 } while (0)
196 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
197 #if 0
198 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
199 the conflicting allocno, and execute CODE. This macro assumes that
200 mirror_conflicts has been run. */
201 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
202 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
203 OUT_ALLOCNO, (CODE))
204 #endif
206 /* Set of hard regs currently live (during scan of all insns). */
208 static HARD_REG_SET hard_regs_live;
210 /* Set of registers that global-alloc isn't supposed to use. */
212 static HARD_REG_SET no_global_alloc_regs;
214 /* Set of registers used so far. */
216 static HARD_REG_SET regs_used_so_far;
218 /* Number of refs (weighted) to each hard reg, as used by local alloc.
219 It is zero for a reg that contains global pseudos or is explicitly used. */
221 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
223 /* Guess at live length of each hard reg, as used by local alloc.
224 This is actually the sum of the live lengths of the specific regs. */
226 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
228 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
229 for vector element I, and hard register number J. */
231 #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (allocno[I].TABLE, J)
233 /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
235 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
237 /* Bit mask for allocnos live at current point in the scan. */
239 static INT_TYPE *allocnos_live;
241 /* Test, set or clear bit number I in allocnos_live,
242 a bit vector indexed by allocno. */
244 #define ALLOCNO_LIVE_P(I) \
245 (allocnos_live[(unsigned)(I) / INT_BITS] \
246 & ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
248 #define SET_ALLOCNO_LIVE(I) \
249 (allocnos_live[(unsigned)(I) / INT_BITS] \
250 |= ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
252 #define CLEAR_ALLOCNO_LIVE(I) \
253 (allocnos_live[(unsigned)(I) / INT_BITS] \
254 &= ~((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
256 /* This is turned off because it doesn't work right for DImode.
257 (And it is only used for DImode, so the other cases are worthless.)
258 The problem is that it isn't true that there is NO possibility of conflict;
259 only that there is no conflict if the two pseudos get the exact same regs.
260 If they were allocated with a partial overlap, there would be a conflict.
261 We can't safely turn off the conflict unless we have another way to
262 prevent the partial overlap.
264 Idea: change hard_reg_conflicts so that instead of recording which
265 hard regs the allocno may not overlap, it records where the allocno
266 may not start. Change both where it is used and where it is updated.
267 Then there is a way to record that (reg:DI 108) may start at 10
268 but not at 9 or 11. There is still the question of how to record
269 this semi-conflict between two pseudos. */
270 #if 0
271 /* Reg pairs for which conflict after the current insn
272 is inhibited by a REG_NO_CONFLICT note.
273 If the table gets full, we ignore any other notes--that is conservative. */
274 #define NUM_NO_CONFLICT_PAIRS 4
275 /* Number of pairs in use in this insn. */
276 int n_no_conflict_pairs;
277 static struct { int allocno1, allocno2;}
278 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
279 #endif /* 0 */
281 /* Record all regs that are set in any one insn.
282 Communication from mark_reg_{store,clobber} and global_conflicts. */
284 static rtx *regs_set;
285 static int n_regs_set;
287 /* All registers that can be eliminated. */
289 static HARD_REG_SET eliminable_regset;
291 static int allocno_compare PARAMS ((const PTR, const PTR));
292 static void global_conflicts PARAMS ((void));
293 static void mirror_conflicts PARAMS ((void));
294 static void expand_preferences PARAMS ((void));
295 static void prune_preferences PARAMS ((void));
296 static void find_reg PARAMS ((int, HARD_REG_SET, int, int, int));
297 static void record_one_conflict PARAMS ((int));
298 static void record_conflicts PARAMS ((int *, int));
299 static void mark_reg_store PARAMS ((rtx, rtx, void *));
300 static void mark_reg_clobber PARAMS ((rtx, rtx, void *));
301 static void mark_reg_conflicts PARAMS ((rtx));
302 static void mark_reg_death PARAMS ((rtx));
303 static void mark_reg_live_nc PARAMS ((int, enum machine_mode));
304 static void set_preference PARAMS ((rtx, rtx));
305 static void dump_conflicts PARAMS ((FILE *));
306 static void reg_becomes_live PARAMS ((rtx, rtx, void *));
307 static void reg_dies PARAMS ((int, enum machine_mode,
308 struct insn_chain *));
310 /* Perform allocation of pseudo-registers not allocated by local_alloc.
311 FILE is a file to output debugging information on,
312 or zero if such output is not desired.
314 Return value is nonzero if reload failed
315 and we must not do any more for this function. */
318 global_alloc (file)
319 FILE *file;
321 int retval;
322 #ifdef ELIMINABLE_REGS
323 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
324 #endif
325 int need_fp
326 = (! flag_omit_frame_pointer
327 #ifdef EXIT_IGNORE_STACK
328 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
329 #endif
330 || FRAME_POINTER_REQUIRED);
332 register size_t i;
333 rtx x;
335 max_allocno = 0;
337 /* A machine may have certain hard registers that
338 are safe to use only within a basic block. */
340 CLEAR_HARD_REG_SET (no_global_alloc_regs);
342 /* Build the regset of all eliminable registers and show we can't use those
343 that we already know won't be eliminated. */
344 #ifdef ELIMINABLE_REGS
345 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
347 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
349 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
350 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
351 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
353 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
354 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
355 if (need_fp)
356 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
357 #endif
359 #else
360 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
361 if (need_fp)
362 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
363 #endif
365 /* Track which registers have already been used. Start with registers
366 explicitly in the rtl, then registers allocated by local register
367 allocation. */
369 CLEAR_HARD_REG_SET (regs_used_so_far);
370 #ifdef LEAF_REGISTERS
371 /* If we are doing the leaf function optimization, and this is a leaf
372 function, it means that the registers that take work to save are those
373 that need a register window. So prefer the ones that can be used in
374 a leaf function. */
376 char *cheap_regs;
377 char *leaf_regs = LEAF_REGISTERS;
379 if (only_leaf_regs_used () && leaf_function_p ())
380 cheap_regs = leaf_regs;
381 else
382 cheap_regs = call_used_regs;
383 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
384 if (regs_ever_live[i] || cheap_regs[i])
385 SET_HARD_REG_BIT (regs_used_so_far, i);
387 #else
388 /* We consider registers that do not have to be saved over calls as if
389 they were already used since there is no cost in using them. */
390 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
391 if (regs_ever_live[i] || call_used_regs[i])
392 SET_HARD_REG_BIT (regs_used_so_far, i);
393 #endif
395 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
396 if (reg_renumber[i] >= 0)
397 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
399 /* Establish mappings from register number to allocation number
400 and vice versa. In the process, count the allocnos. */
402 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
404 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
405 reg_allocno[i] = -1;
407 /* Initialize the shared-hard-reg mapping
408 from the list of pairs that may share. */
409 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
410 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
412 int r1 = REGNO (XEXP (x, 0));
413 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
414 if (r1 > r2)
415 reg_may_share[r1] = r2;
416 else
417 reg_may_share[r2] = r1;
420 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
421 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
422 that we are supposed to refrain from putting in a hard reg.
423 -2 means do make an allocno but don't allocate it. */
424 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
425 /* Don't allocate pseudos that cross calls,
426 if this function receives a nonlocal goto. */
427 && (! current_function_has_nonlocal_label
428 || REG_N_CALLS_CROSSED (i) == 0))
430 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
431 reg_allocno[i] = reg_allocno[reg_may_share[i]];
432 else
433 reg_allocno[i] = max_allocno++;
434 if (REG_LIVE_LENGTH (i) == 0)
435 abort ();
437 else
438 reg_allocno[i] = -1;
440 allocno = (struct allocno *) xcalloc (max_allocno, sizeof (struct allocno));
442 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
443 if (reg_allocno[i] >= 0)
445 int num = reg_allocno[i];
446 allocno[num].reg = i;
447 allocno[num].size = PSEUDO_REGNO_SIZE (i);
448 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
449 allocno[num].n_refs += REG_N_REFS (i);
450 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
451 allocno[num].live_length = REG_LIVE_LENGTH (i);
454 /* Calculate amount of usage of each hard reg by pseudos
455 allocated by local-alloc. This is to see if we want to
456 override it. */
457 memset ((char *) local_reg_live_length, 0, sizeof local_reg_live_length);
458 memset ((char *) local_reg_n_refs, 0, sizeof local_reg_n_refs);
459 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
460 if (reg_renumber[i] >= 0)
462 int regno = reg_renumber[i];
463 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
464 int j;
466 for (j = regno; j < endregno; j++)
468 local_reg_n_refs[j] += REG_N_REFS (i);
469 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
473 /* We can't override local-alloc for a reg used not just by local-alloc. */
474 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
475 if (regs_ever_live[i])
476 local_reg_n_refs[i] = 0;
478 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
480 /* We used to use alloca here, but the size of what it would try to
481 allocate would occasionally cause it to exceed the stack limit and
482 cause unpredictable core dumps. Some examples were > 2Mb in size. */
483 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
484 sizeof (INT_TYPE));
486 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
488 /* If there is work to be done (at least one reg to allocate),
489 perform global conflict analysis and allocate the regs. */
491 if (max_allocno > 0)
493 /* Scan all the insns and compute the conflicts among allocnos
494 and between allocnos and hard regs. */
496 global_conflicts ();
498 mirror_conflicts ();
500 /* Eliminate conflicts between pseudos and eliminable registers. If
501 the register is not eliminated, the pseudo won't really be able to
502 live in the eliminable register, so the conflict doesn't matter.
503 If we do eliminate the register, the conflict will no longer exist.
504 So in either case, we can ignore the conflict. Likewise for
505 preferences. */
507 for (i = 0; i < (size_t) max_allocno; i++)
509 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
510 eliminable_regset);
511 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
512 eliminable_regset);
513 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
514 eliminable_regset);
517 /* Try to expand the preferences by merging them between allocnos. */
519 expand_preferences ();
521 /* Determine the order to allocate the remaining pseudo registers. */
523 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
524 for (i = 0; i < (size_t) max_allocno; i++)
525 allocno_order[i] = i;
527 /* Default the size to 1, since allocno_compare uses it to divide by.
528 Also convert allocno_live_length of zero to -1. A length of zero
529 can occur when all the registers for that allocno have reg_live_length
530 equal to -2. In this case, we want to make an allocno, but not
531 allocate it. So avoid the divide-by-zero and set it to a low
532 priority. */
534 for (i = 0; i < (size_t) max_allocno; i++)
536 if (allocno[i].size == 0)
537 allocno[i].size = 1;
538 if (allocno[i].live_length == 0)
539 allocno[i].live_length = -1;
542 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
544 prune_preferences ();
546 if (file)
547 dump_conflicts (file);
549 /* Try allocating them, one by one, in that order,
550 except for parameters marked with reg_live_length[regno] == -2. */
552 for (i = 0; i < (size_t) max_allocno; i++)
553 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
554 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
556 /* If we have more than one register class,
557 first try allocating in the class that is cheapest
558 for this pseudo-reg. If that fails, try any reg. */
559 if (N_REG_CLASSES > 1)
561 find_reg (allocno_order[i], 0, 0, 0, 0);
562 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
563 continue;
565 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
566 find_reg (allocno_order[i], 0, 1, 0, 0);
569 free (allocno_order);
572 /* Do the reloads now while the allocno data still exist, so that we can
573 try to assign new hard regs to any pseudo regs that are spilled. */
575 #if 0 /* We need to eliminate regs even if there is no rtl code,
576 for the sake of debugging information. */
577 if (n_basic_blocks > 0)
578 #endif
580 build_insn_chain (get_insns ());
581 retval = reload (get_insns (), 1);
584 /* Clean up. */
585 free (reg_allocno);
586 free (reg_may_share);
587 free (allocno);
588 free (conflicts);
589 free (allocnos_live);
591 return retval;
594 /* Sort predicate for ordering the allocnos.
595 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
597 static int
598 allocno_compare (v1p, v2p)
599 const PTR v1p;
600 const PTR v2p;
602 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
603 /* Note that the quotient will never be bigger than
604 the value of floor_log2 times the maximum number of
605 times a register can occur in one insn (surely less than 100).
606 Multiplying this by 10000 can't overflow. */
607 register int pri1
608 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].n_refs)
609 / allocno[v1].live_length)
610 * 10000 * allocno[v1].size);
611 register int pri2
612 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].n_refs)
613 / allocno[v2].live_length)
614 * 10000 * allocno[v2].size);
615 if (pri2 - pri1)
616 return pri2 - pri1;
618 /* If regs are equally good, sort by allocno,
619 so that the results of qsort leave nothing to chance. */
620 return v1 - v2;
623 /* Scan the rtl code and record all conflicts and register preferences in the
624 conflict matrices and preference tables. */
626 static void
627 global_conflicts ()
629 register int b, i;
630 register rtx insn;
631 int *block_start_allocnos;
633 /* Make a vector that mark_reg_{store,clobber} will store in. */
634 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
636 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
638 for (b = 0; b < n_basic_blocks; b++)
640 memset ((char *) allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
642 /* Initialize table of registers currently live
643 to the state at the beginning of this basic block.
644 This also marks the conflicts among hard registers
645 and any allocnos that are live.
647 For pseudo-regs, there is only one bit for each one
648 no matter how many hard regs it occupies.
649 This is ok; we know the size from PSEUDO_REGNO_SIZE.
650 For explicit hard regs, we cannot know the size that way
651 since one hard reg can be used with various sizes.
652 Therefore, we must require that all the hard regs
653 implicitly live as part of a multi-word hard reg
654 are explicitly marked in basic_block_live_at_start. */
657 register regset old = BASIC_BLOCK (b)->global_live_at_start;
658 int ax = 0;
660 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
661 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
663 register int a = reg_allocno[i];
664 if (a >= 0)
666 SET_ALLOCNO_LIVE (a);
667 block_start_allocnos[ax++] = a;
669 else if ((a = reg_renumber[i]) >= 0)
670 mark_reg_live_nc
671 (a, PSEUDO_REGNO_MODE (i));
674 /* Record that each allocno now live conflicts with each hard reg
675 now live.
677 It is not necessary to mark any conflicts between pseudos as
678 this point, even for pseudos which are live at the start of
679 the basic block.
681 Given two pseudos X and Y and any point in the CFG P.
683 On any path to point P where X and Y are live one of the
684 following conditions must be true:
686 1. X is live at some instruction on the path that
687 evaluates Y.
689 2. Y is live at some instruction on the path that
690 evaluates X.
692 3. Either X or Y is not evaluted on the path to P
693 (ie it is used uninitialized) and thus the
694 conflict can be ignored.
696 In cases #1 and #2 the conflict will be recorded when we
697 scan the instruction that makes either X or Y become live. */
698 record_conflicts (block_start_allocnos, ax);
700 #ifdef STACK_REGS
702 /* Pseudos can't go in stack regs at the start of a basic block
703 that is reached by an abnormal edge. */
705 edge e;
706 for (e = BASIC_BLOCK (b)->pred; e ; e = e->pred_next)
707 if (e->flags & EDGE_ABNORMAL)
708 break;
709 if (e != NULL)
710 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
711 record_one_conflict (ax);
713 #endif
716 insn = BLOCK_HEAD (b);
718 /* Scan the code of this basic block, noting which allocnos
719 and hard regs are born or die. When one is born,
720 record a conflict with all others currently live. */
722 while (1)
724 register RTX_CODE code = GET_CODE (insn);
725 register rtx link;
727 /* Make regs_set an empty set. */
729 n_regs_set = 0;
731 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
734 #if 0
735 int i = 0;
736 for (link = REG_NOTES (insn);
737 link && i < NUM_NO_CONFLICT_PAIRS;
738 link = XEXP (link, 1))
739 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
741 no_conflict_pairs[i].allocno1
742 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
743 no_conflict_pairs[i].allocno2
744 = reg_allocno[REGNO (XEXP (link, 0))];
745 i++;
747 #endif /* 0 */
749 /* Mark any registers clobbered by INSN as live,
750 so they conflict with the inputs. */
752 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
754 /* Mark any registers dead after INSN as dead now. */
756 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
757 if (REG_NOTE_KIND (link) == REG_DEAD)
758 mark_reg_death (XEXP (link, 0));
760 /* Mark any registers set in INSN as live,
761 and mark them as conflicting with all other live regs.
762 Clobbers are processed again, so they conflict with
763 the registers that are set. */
765 note_stores (PATTERN (insn), mark_reg_store, NULL);
767 #ifdef AUTO_INC_DEC
768 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
769 if (REG_NOTE_KIND (link) == REG_INC)
770 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
771 #endif
773 /* If INSN has multiple outputs, then any reg that dies here
774 and is used inside of an output
775 must conflict with the other outputs.
777 It is unsafe to use !single_set here since it will ignore an
778 unused output. Just because an output is unused does not mean
779 the compiler can assume the side effect will not occur.
780 Consider if REG appears in the address of an output and we
781 reload the output. If we allocate REG to the same hard
782 register as an unused output we could set the hard register
783 before the output reload insn. */
784 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
785 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
786 if (REG_NOTE_KIND (link) == REG_DEAD)
788 int used_in_output = 0;
789 int i;
790 rtx reg = XEXP (link, 0);
792 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
794 rtx set = XVECEXP (PATTERN (insn), 0, i);
795 if (GET_CODE (set) == SET
796 && GET_CODE (SET_DEST (set)) != REG
797 && !rtx_equal_p (reg, SET_DEST (set))
798 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
799 used_in_output = 1;
801 if (used_in_output)
802 mark_reg_conflicts (reg);
805 /* Mark any registers set in INSN and then never used. */
807 while (n_regs_set-- > 0)
809 rtx note = find_regno_note (insn, REG_UNUSED,
810 REGNO (regs_set[n_regs_set]));
811 if (note)
812 mark_reg_death (XEXP (note, 0));
816 if (insn == BLOCK_END (b))
817 break;
818 insn = NEXT_INSN (insn);
822 /* Clean up. */
823 free (block_start_allocnos);
824 free (regs_set);
826 /* Expand the preference information by looking for cases where one allocno
827 dies in an insn that sets an allocno. If those two allocnos don't conflict,
828 merge any preferences between those allocnos. */
830 static void
831 expand_preferences ()
833 rtx insn;
834 rtx link;
835 rtx set;
837 /* We only try to handle the most common cases here. Most of the cases
838 where this wins are reg-reg copies. */
840 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
841 if (INSN_P (insn)
842 && (set = single_set (insn)) != 0
843 && GET_CODE (SET_DEST (set)) == REG
844 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
845 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
846 if (REG_NOTE_KIND (link) == REG_DEAD
847 && GET_CODE (XEXP (link, 0)) == REG
848 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
849 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
850 reg_allocno[REGNO (XEXP (link, 0))]))
852 int a1 = reg_allocno[REGNO (SET_DEST (set))];
853 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
855 if (XEXP (link, 0) == SET_SRC (set))
857 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
858 allocno[a2].hard_reg_copy_preferences);
859 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
860 allocno[a1].hard_reg_copy_preferences);
863 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
864 allocno[a2].hard_reg_preferences);
865 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
866 allocno[a1].hard_reg_preferences);
867 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
868 allocno[a2].hard_reg_full_preferences);
869 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
870 allocno[a1].hard_reg_full_preferences);
874 /* Prune the preferences for global registers to exclude registers that cannot
875 be used.
877 Compute `regs_someone_prefers', which is a bitmask of the hard registers
878 that are preferred by conflicting registers of lower priority. If possible,
879 we will avoid using these registers. */
881 static void
882 prune_preferences ()
884 int i;
885 int num;
886 int *allocno_to_order = (int *) xmalloc (max_allocno * sizeof (int));
888 /* Scan least most important to most important.
889 For each allocno, remove from preferences registers that cannot be used,
890 either because of conflicts or register type. Then compute all registers
891 preferred by each lower-priority register that conflicts. */
893 for (i = max_allocno - 1; i >= 0; i--)
895 HARD_REG_SET temp;
897 num = allocno_order[i];
898 allocno_to_order[num] = i;
899 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
901 if (allocno[num].calls_crossed == 0)
902 IOR_HARD_REG_SET (temp, fixed_reg_set);
903 else
904 IOR_HARD_REG_SET (temp, call_used_reg_set);
906 IOR_COMPL_HARD_REG_SET
907 (temp,
908 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
910 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
911 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
912 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
915 for (i = max_allocno - 1; i >= 0; i--)
917 /* Merge in the preferences of lower-priority registers (they have
918 already been pruned). If we also prefer some of those registers,
919 don't exclude them unless we are of a smaller size (in which case
920 we want to give the lower-priority allocno the first chance for
921 these registers). */
922 HARD_REG_SET temp, temp2;
923 int allocno2;
925 num = allocno_order[i];
927 CLEAR_HARD_REG_SET (temp);
928 CLEAR_HARD_REG_SET (temp2);
930 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
931 allocno2,
933 if (allocno_to_order[allocno2] > i)
935 if (allocno[allocno2].size <= allocno[num].size)
936 IOR_HARD_REG_SET (temp,
937 allocno[allocno2].hard_reg_full_preferences);
938 else
939 IOR_HARD_REG_SET (temp2,
940 allocno[allocno2].hard_reg_full_preferences);
944 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
945 IOR_HARD_REG_SET (temp, temp2);
946 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
948 free (allocno_to_order);
951 /* Assign a hard register to allocno NUM; look for one that is the beginning
952 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
953 The registers marked in PREFREGS are tried first.
955 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
956 be used for this allocation.
958 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
959 Otherwise ignore that preferred class and use the alternate class.
961 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
962 will have to be saved and restored at calls.
964 RETRYING is nonzero if this is called from retry_global_alloc.
966 If we find one, record it in reg_renumber.
967 If not, do nothing. */
969 static void
970 find_reg (num, losers, alt_regs_p, accept_call_clobbered, retrying)
971 int num;
972 HARD_REG_SET losers;
973 int alt_regs_p;
974 int accept_call_clobbered;
975 int retrying;
977 register int i, best_reg, pass;
978 #ifdef HARD_REG_SET
979 register /* Declare it register if it's a scalar. */
980 #endif
981 HARD_REG_SET used, used1, used2;
983 enum reg_class class = (alt_regs_p
984 ? reg_alternate_class (allocno[num].reg)
985 : reg_preferred_class (allocno[num].reg));
986 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
988 if (accept_call_clobbered)
989 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
990 else if (allocno[num].calls_crossed == 0)
991 COPY_HARD_REG_SET (used1, fixed_reg_set);
992 else
993 COPY_HARD_REG_SET (used1, call_used_reg_set);
995 /* Some registers should not be allocated in global-alloc. */
996 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
997 if (losers)
998 IOR_HARD_REG_SET (used1, losers);
1000 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1001 COPY_HARD_REG_SET (used2, used1);
1003 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1005 #ifdef CLASS_CANNOT_CHANGE_MODE
1006 if (REG_CHANGES_MODE (allocno[num].reg))
1007 IOR_HARD_REG_SET (used1,
1008 reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE]);
1009 #endif
1011 /* Try each hard reg to see if it fits. Do this in two passes.
1012 In the first pass, skip registers that are preferred by some other pseudo
1013 to give it a better chance of getting one of those registers. Only if
1014 we can't get a register when excluding those do we take one of them.
1015 However, we never allocate a register for the first time in pass 0. */
1017 COPY_HARD_REG_SET (used, used1);
1018 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1019 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1021 best_reg = -1;
1022 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1023 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1024 pass++)
1026 if (pass == 1)
1027 COPY_HARD_REG_SET (used, used1);
1028 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1030 #ifdef REG_ALLOC_ORDER
1031 int regno = reg_alloc_order[i];
1032 #else
1033 int regno = i;
1034 #endif
1035 if (! TEST_HARD_REG_BIT (used, regno)
1036 && HARD_REGNO_MODE_OK (regno, mode)
1037 && (allocno[num].calls_crossed == 0
1038 || accept_call_clobbered
1039 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1041 register int j;
1042 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
1043 for (j = regno + 1;
1044 (j < lim
1045 && ! TEST_HARD_REG_BIT (used, j));
1046 j++);
1047 if (j == lim)
1049 best_reg = regno;
1050 break;
1052 #ifndef REG_ALLOC_ORDER
1053 i = j; /* Skip starting points we know will lose */
1054 #endif
1059 /* See if there is a preferred register with the same class as the register
1060 we allocated above. Making this restriction prevents register
1061 preferencing from creating worse register allocation.
1063 Remove from the preferred registers and conflicting registers. Note that
1064 additional conflicts may have been added after `prune_preferences' was
1065 called.
1067 First do this for those register with copy preferences, then all
1068 preferred registers. */
1070 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1071 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1072 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1074 if (best_reg >= 0)
1076 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1077 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1078 && HARD_REGNO_MODE_OK (i, mode)
1079 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1080 || reg_class_subset_p (REGNO_REG_CLASS (i),
1081 REGNO_REG_CLASS (best_reg))
1082 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1083 REGNO_REG_CLASS (i))))
1085 register int j;
1086 register int lim = i + HARD_REGNO_NREGS (i, mode);
1087 for (j = i + 1;
1088 (j < lim
1089 && ! TEST_HARD_REG_BIT (used, j)
1090 && (REGNO_REG_CLASS (j)
1091 == REGNO_REG_CLASS (best_reg + (j - i))
1092 || reg_class_subset_p (REGNO_REG_CLASS (j),
1093 REGNO_REG_CLASS (best_reg + (j - i)))
1094 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1095 REGNO_REG_CLASS (j))));
1096 j++);
1097 if (j == lim)
1099 best_reg = i;
1100 goto no_prefs;
1104 no_copy_prefs:
1106 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1107 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1108 reg_class_contents[(int) NO_REGS], no_prefs);
1110 if (best_reg >= 0)
1112 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1113 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1114 && HARD_REGNO_MODE_OK (i, mode)
1115 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1116 || reg_class_subset_p (REGNO_REG_CLASS (i),
1117 REGNO_REG_CLASS (best_reg))
1118 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1119 REGNO_REG_CLASS (i))))
1121 register int j;
1122 register int lim = i + HARD_REGNO_NREGS (i, mode);
1123 for (j = i + 1;
1124 (j < lim
1125 && ! TEST_HARD_REG_BIT (used, j)
1126 && (REGNO_REG_CLASS (j)
1127 == REGNO_REG_CLASS (best_reg + (j - i))
1128 || reg_class_subset_p (REGNO_REG_CLASS (j),
1129 REGNO_REG_CLASS (best_reg + (j - i)))
1130 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1131 REGNO_REG_CLASS (j))));
1132 j++);
1133 if (j == lim)
1135 best_reg = i;
1136 break;
1140 no_prefs:
1142 /* If we haven't succeeded yet, try with caller-saves.
1143 We need not check to see if the current function has nonlocal
1144 labels because we don't put any pseudos that are live over calls in
1145 registers in that case. */
1147 if (flag_caller_saves && best_reg < 0)
1149 /* Did not find a register. If it would be profitable to
1150 allocate a call-clobbered register and save and restore it
1151 around calls, do that. */
1152 if (! accept_call_clobbered
1153 && allocno[num].calls_crossed != 0
1154 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1155 allocno[num].calls_crossed))
1157 HARD_REG_SET new_losers;
1158 if (! losers)
1159 CLEAR_HARD_REG_SET (new_losers);
1160 else
1161 COPY_HARD_REG_SET (new_losers, losers);
1163 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1164 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1165 if (reg_renumber[allocno[num].reg] >= 0)
1167 caller_save_needed = 1;
1168 return;
1173 /* If we haven't succeeded yet,
1174 see if some hard reg that conflicts with us
1175 was utilized poorly by local-alloc.
1176 If so, kick out the regs that were put there by local-alloc
1177 so we can use it instead. */
1178 if (best_reg < 0 && !retrying
1179 /* Let's not bother with multi-reg allocnos. */
1180 && allocno[num].size == 1)
1182 /* Count from the end, to find the least-used ones first. */
1183 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1185 #ifdef REG_ALLOC_ORDER
1186 int regno = reg_alloc_order[i];
1187 #else
1188 int regno = i;
1189 #endif
1191 if (local_reg_n_refs[regno] != 0
1192 /* Don't use a reg no good for this pseudo. */
1193 && ! TEST_HARD_REG_BIT (used2, regno)
1194 && HARD_REGNO_MODE_OK (regno, mode)
1195 #ifdef CLASS_CANNOT_CHANGE_MODE
1196 && ! (REG_CHANGES_MODE (allocno[num].reg)
1197 && (TEST_HARD_REG_BIT
1198 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE],
1199 regno)))
1200 #endif
1203 /* We explicitly evaluate the divide results into temporary
1204 variables so as to avoid excess precision problems that occur
1205 on a i386-unknown-sysv4.2 (unixware) host. */
1207 double tmp1 = ((double) local_reg_n_refs[regno]
1208 / local_reg_live_length[regno]);
1209 double tmp2 = ((double) allocno[num].n_refs
1210 / allocno[num].live_length);
1212 if (tmp1 < tmp2)
1214 /* Hard reg REGNO was used less in total by local regs
1215 than it would be used by this one allocno! */
1216 int k;
1217 for (k = 0; k < max_regno; k++)
1218 if (reg_renumber[k] >= 0)
1220 int r = reg_renumber[k];
1221 int endregno
1222 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1224 if (regno >= r && regno < endregno)
1225 reg_renumber[k] = -1;
1228 best_reg = regno;
1229 break;
1235 /* Did we find a register? */
1237 if (best_reg >= 0)
1239 register int lim, j;
1240 HARD_REG_SET this_reg;
1242 /* Yes. Record it as the hard register of this pseudo-reg. */
1243 reg_renumber[allocno[num].reg] = best_reg;
1244 /* Also of any pseudo-regs that share with it. */
1245 if (reg_may_share[allocno[num].reg])
1246 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1247 if (reg_allocno[j] == num)
1248 reg_renumber[j] = best_reg;
1250 /* Make a set of the hard regs being allocated. */
1251 CLEAR_HARD_REG_SET (this_reg);
1252 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1253 for (j = best_reg; j < lim; j++)
1255 SET_HARD_REG_BIT (this_reg, j);
1256 SET_HARD_REG_BIT (regs_used_so_far, j);
1257 /* This is no longer a reg used just by local regs. */
1258 local_reg_n_refs[j] = 0;
1260 /* For each other pseudo-reg conflicting with this one,
1261 mark it as conflicting with the hard regs this one occupies. */
1262 lim = num;
1263 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1265 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1270 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1271 Perhaps it had previously seemed not worth a hard reg,
1272 or perhaps its old hard reg has been commandeered for reloads.
1273 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1274 they do not appear to be allocated.
1275 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1277 void
1278 retry_global_alloc (regno, forbidden_regs)
1279 int regno;
1280 HARD_REG_SET forbidden_regs;
1282 int allocno = reg_allocno[regno];
1283 if (allocno >= 0)
1285 /* If we have more than one register class,
1286 first try allocating in the class that is cheapest
1287 for this pseudo-reg. If that fails, try any reg. */
1288 if (N_REG_CLASSES > 1)
1289 find_reg (allocno, forbidden_regs, 0, 0, 1);
1290 if (reg_renumber[regno] < 0
1291 && reg_alternate_class (regno) != NO_REGS)
1292 find_reg (allocno, forbidden_regs, 1, 0, 1);
1294 /* If we found a register, modify the RTL for the register to
1295 show the hard register, and mark that register live. */
1296 if (reg_renumber[regno] >= 0)
1298 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1299 mark_home_live (regno);
1304 /* Record a conflict between register REGNO
1305 and everything currently live.
1306 REGNO must not be a pseudo reg that was allocated
1307 by local_alloc; such numbers must be translated through
1308 reg_renumber before calling here. */
1310 static void
1311 record_one_conflict (regno)
1312 int regno;
1314 register int j;
1316 if (regno < FIRST_PSEUDO_REGISTER)
1317 /* When a hard register becomes live,
1318 record conflicts with live pseudo regs. */
1319 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1321 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1323 else
1324 /* When a pseudo-register becomes live,
1325 record conflicts first with hard regs,
1326 then with other pseudo regs. */
1328 register int ialloc = reg_allocno[regno];
1329 register int ialloc_prod = ialloc * allocno_row_words;
1330 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1331 for (j = allocno_row_words - 1; j >= 0; j--)
1333 #if 0
1334 int k;
1335 for (k = 0; k < n_no_conflict_pairs; k++)
1336 if (! ((j == no_conflict_pairs[k].allocno1
1337 && ialloc == no_conflict_pairs[k].allocno2)
1339 (j == no_conflict_pairs[k].allocno2
1340 && ialloc == no_conflict_pairs[k].allocno1)))
1341 #endif /* 0 */
1342 conflicts[ialloc_prod + j] |= allocnos_live[j];
1347 /* Record all allocnos currently live as conflicting
1348 with all hard regs currently live.
1350 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1351 are currently live. Their bits are also flagged in allocnos_live. */
1353 static void
1354 record_conflicts (allocno_vec, len)
1355 register int *allocno_vec;
1356 register int len;
1358 register int num;
1359 register int ialloc_prod;
1361 while (--len >= 0)
1363 num = allocno_vec[len];
1364 ialloc_prod = num * allocno_row_words;
1365 IOR_HARD_REG_SET (allocno[num].hard_reg_conflicts, hard_regs_live);
1369 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1370 static void
1371 mirror_conflicts ()
1373 register int i, j;
1374 int rw = allocno_row_words;
1375 int rwb = rw * INT_BITS;
1376 INT_TYPE *p = conflicts;
1377 INT_TYPE *q0 = conflicts, *q1, *q2;
1378 unsigned INT_TYPE mask;
1380 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1382 if (! mask)
1384 mask = 1;
1385 q0++;
1387 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1389 unsigned INT_TYPE word;
1391 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1392 word >>= 1, q2 += rw)
1394 if (word & 1)
1395 *q2 |= mask;
1401 /* Handle the case where REG is set by the insn being scanned,
1402 during the forward scan to accumulate conflicts.
1403 Store a 1 in regs_live or allocnos_live for this register, record how many
1404 consecutive hardware registers it actually needs,
1405 and record a conflict with all other registers already live.
1407 Note that even if REG does not remain alive after this insn,
1408 we must mark it here as live, to ensure a conflict between
1409 REG and any other regs set in this insn that really do live.
1410 This is because those other regs could be considered after this.
1412 REG might actually be something other than a register;
1413 if so, we do nothing.
1415 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1416 a REG_INC note was found for it). */
1418 static void
1419 mark_reg_store (reg, setter, data)
1420 rtx reg, setter;
1421 void *data ATTRIBUTE_UNUSED;
1423 register int regno;
1425 if (GET_CODE (reg) == SUBREG)
1426 reg = SUBREG_REG (reg);
1428 if (GET_CODE (reg) != REG)
1429 return;
1431 regs_set[n_regs_set++] = reg;
1433 if (setter && GET_CODE (setter) != CLOBBER)
1434 set_preference (reg, SET_SRC (setter));
1436 regno = REGNO (reg);
1438 /* Either this is one of the max_allocno pseudo regs not allocated,
1439 or it is or has a hardware reg. First handle the pseudo-regs. */
1440 if (regno >= FIRST_PSEUDO_REGISTER)
1442 if (reg_allocno[regno] >= 0)
1444 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1445 record_one_conflict (regno);
1449 if (reg_renumber[regno] >= 0)
1450 regno = reg_renumber[regno];
1452 /* Handle hardware regs (and pseudos allocated to hard regs). */
1453 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1455 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1456 while (regno < last)
1458 record_one_conflict (regno);
1459 SET_HARD_REG_BIT (hard_regs_live, regno);
1460 regno++;
1465 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1467 static void
1468 mark_reg_clobber (reg, setter, data)
1469 rtx reg, setter;
1470 void *data ATTRIBUTE_UNUSED;
1472 if (GET_CODE (setter) == CLOBBER)
1473 mark_reg_store (reg, setter, data);
1476 /* Record that REG has conflicts with all the regs currently live.
1477 Do not mark REG itself as live. */
1479 static void
1480 mark_reg_conflicts (reg)
1481 rtx reg;
1483 register int regno;
1485 if (GET_CODE (reg) == SUBREG)
1486 reg = SUBREG_REG (reg);
1488 if (GET_CODE (reg) != REG)
1489 return;
1491 regno = REGNO (reg);
1493 /* Either this is one of the max_allocno pseudo regs not allocated,
1494 or it is or has a hardware reg. First handle the pseudo-regs. */
1495 if (regno >= FIRST_PSEUDO_REGISTER)
1497 if (reg_allocno[regno] >= 0)
1498 record_one_conflict (regno);
1501 if (reg_renumber[regno] >= 0)
1502 regno = reg_renumber[regno];
1504 /* Handle hardware regs (and pseudos allocated to hard regs). */
1505 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1507 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1508 while (regno < last)
1510 record_one_conflict (regno);
1511 regno++;
1516 /* Mark REG as being dead (following the insn being scanned now).
1517 Store a 0 in regs_live or allocnos_live for this register. */
1519 static void
1520 mark_reg_death (reg)
1521 rtx reg;
1523 register int regno = REGNO (reg);
1525 /* Either this is one of the max_allocno pseudo regs not allocated,
1526 or it is a hardware reg. First handle the pseudo-regs. */
1527 if (regno >= FIRST_PSEUDO_REGISTER)
1529 if (reg_allocno[regno] >= 0)
1530 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1533 /* For pseudo reg, see if it has been assigned a hardware reg. */
1534 if (reg_renumber[regno] >= 0)
1535 regno = reg_renumber[regno];
1537 /* Handle hardware regs (and pseudos allocated to hard regs). */
1538 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1540 /* Pseudo regs already assigned hardware regs are treated
1541 almost the same as explicit hardware regs. */
1542 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1543 while (regno < last)
1545 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1546 regno++;
1551 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1552 for the value stored in it. MODE determines how many consecutive
1553 registers are actually in use. Do not record conflicts;
1554 it is assumed that the caller will do that. */
1556 static void
1557 mark_reg_live_nc (regno, mode)
1558 register int regno;
1559 enum machine_mode mode;
1561 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1562 while (regno < last)
1564 SET_HARD_REG_BIT (hard_regs_live, regno);
1565 regno++;
1569 /* Try to set a preference for an allocno to a hard register.
1570 We are passed DEST and SRC which are the operands of a SET. It is known
1571 that SRC is a register. If SRC or the first operand of SRC is a register,
1572 try to set a preference. If one of the two is a hard register and the other
1573 is a pseudo-register, mark the preference.
1575 Note that we are not as aggressive as local-alloc in trying to tie a
1576 pseudo-register to a hard register. */
1578 static void
1579 set_preference (dest, src)
1580 rtx dest, src;
1582 unsigned int src_regno, dest_regno;
1583 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1584 to compensate for subregs in SRC or DEST. */
1585 int offset = 0;
1586 unsigned int i;
1587 int copy = 1;
1589 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1590 src = XEXP (src, 0), copy = 0;
1592 /* Get the reg number for both SRC and DEST.
1593 If neither is a reg, give up. */
1595 if (GET_CODE (src) == REG)
1596 src_regno = REGNO (src);
1597 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1599 src_regno = REGNO (SUBREG_REG (src));
1601 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1602 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1603 GET_MODE (SUBREG_REG (src)),
1604 SUBREG_BYTE (src),
1605 GET_MODE (src));
1606 else
1607 offset += (SUBREG_BYTE (src)
1608 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
1610 else
1611 return;
1613 if (GET_CODE (dest) == REG)
1614 dest_regno = REGNO (dest);
1615 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1617 dest_regno = REGNO (SUBREG_REG (dest));
1619 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1620 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1621 GET_MODE (SUBREG_REG (dest)),
1622 SUBREG_BYTE (dest),
1623 GET_MODE (dest));
1624 else
1625 offset -= (SUBREG_BYTE (dest)
1626 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
1628 else
1629 return;
1631 /* Convert either or both to hard reg numbers. */
1633 if (reg_renumber[src_regno] >= 0)
1634 src_regno = reg_renumber[src_regno];
1636 if (reg_renumber[dest_regno] >= 0)
1637 dest_regno = reg_renumber[dest_regno];
1639 /* Now if one is a hard reg and the other is a global pseudo
1640 then give the other a preference. */
1642 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1643 && reg_allocno[src_regno] >= 0)
1645 dest_regno -= offset;
1646 if (dest_regno < FIRST_PSEUDO_REGISTER)
1648 if (copy)
1649 SET_REGBIT (hard_reg_copy_preferences,
1650 reg_allocno[src_regno], dest_regno);
1652 SET_REGBIT (hard_reg_preferences,
1653 reg_allocno[src_regno], dest_regno);
1654 for (i = dest_regno;
1655 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1656 i++)
1657 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1661 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1662 && reg_allocno[dest_regno] >= 0)
1664 src_regno += offset;
1665 if (src_regno < FIRST_PSEUDO_REGISTER)
1667 if (copy)
1668 SET_REGBIT (hard_reg_copy_preferences,
1669 reg_allocno[dest_regno], src_regno);
1671 SET_REGBIT (hard_reg_preferences,
1672 reg_allocno[dest_regno], src_regno);
1673 for (i = src_regno;
1674 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1675 i++)
1676 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1681 /* Indicate that hard register number FROM was eliminated and replaced with
1682 an offset from hard register number TO. The status of hard registers live
1683 at the start of a basic block is updated by replacing a use of FROM with
1684 a use of TO. */
1686 void
1687 mark_elimination (from, to)
1688 int from, to;
1690 int i;
1692 for (i = 0; i < n_basic_blocks; i++)
1694 register regset r = BASIC_BLOCK (i)->global_live_at_start;
1695 if (REGNO_REG_SET_P (r, from))
1697 CLEAR_REGNO_REG_SET (r, from);
1698 SET_REGNO_REG_SET (r, to);
1703 /* Used for communication between the following functions. Holds the
1704 current life information. */
1705 static regset live_relevant_regs;
1707 /* Record in live_relevant_regs and REGS_SET that register REG became live.
1708 This is called via note_stores. */
1709 static void
1710 reg_becomes_live (reg, setter, regs_set)
1711 rtx reg;
1712 rtx setter ATTRIBUTE_UNUSED;
1713 void *regs_set;
1715 int regno;
1717 if (GET_CODE (reg) == SUBREG)
1718 reg = SUBREG_REG (reg);
1720 if (GET_CODE (reg) != REG)
1721 return;
1723 regno = REGNO (reg);
1724 if (regno < FIRST_PSEUDO_REGISTER)
1726 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1727 while (nregs-- > 0)
1729 SET_REGNO_REG_SET (live_relevant_regs, regno);
1730 if (! fixed_regs[regno])
1731 SET_REGNO_REG_SET ((regset) regs_set, regno);
1732 regno++;
1735 else if (reg_renumber[regno] >= 0)
1737 SET_REGNO_REG_SET (live_relevant_regs, regno);
1738 SET_REGNO_REG_SET ((regset) regs_set, regno);
1742 /* Record in live_relevant_regs that register REGNO died. */
1743 static void
1744 reg_dies (regno, mode, chain)
1745 int regno;
1746 enum machine_mode mode;
1747 struct insn_chain *chain;
1749 if (regno < FIRST_PSEUDO_REGISTER)
1751 int nregs = HARD_REGNO_NREGS (regno, mode);
1752 while (nregs-- > 0)
1754 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1755 if (! fixed_regs[regno])
1756 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1757 regno++;
1760 else
1762 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1763 if (reg_renumber[regno] >= 0)
1764 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1768 /* Walk the insns of the current function and build reload_insn_chain,
1769 and record register life information. */
1770 void
1771 build_insn_chain (first)
1772 rtx first;
1774 struct insn_chain **p = &reload_insn_chain;
1775 struct insn_chain *prev = 0;
1776 int b = 0;
1777 regset_head live_relevant_regs_head;
1779 live_relevant_regs = INITIALIZE_REG_SET (live_relevant_regs_head);
1781 for (; first; first = NEXT_INSN (first))
1783 struct insn_chain *c;
1785 if (first == BLOCK_HEAD (b))
1787 int i;
1789 CLEAR_REG_SET (live_relevant_regs);
1791 EXECUTE_IF_SET_IN_BITMAP
1792 (BASIC_BLOCK (b)->global_live_at_start, 0, i,
1794 if (i < FIRST_PSEUDO_REGISTER
1795 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1796 : reg_renumber[i] >= 0)
1797 SET_REGNO_REG_SET (live_relevant_regs, i);
1801 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1803 c = new_insn_chain ();
1804 c->prev = prev;
1805 prev = c;
1806 *p = c;
1807 p = &c->next;
1808 c->insn = first;
1809 c->block = b;
1811 if (INSN_P (first))
1813 rtx link;
1815 /* Mark the death of everything that dies in this instruction. */
1817 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1818 if (REG_NOTE_KIND (link) == REG_DEAD
1819 && GET_CODE (XEXP (link, 0)) == REG)
1820 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1823 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1825 /* Mark everything born in this instruction as live. */
1827 note_stores (PATTERN (first), reg_becomes_live,
1828 &c->dead_or_set);
1830 else
1831 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1833 if (INSN_P (first))
1835 rtx link;
1837 /* Mark anything that is set in this insn and then unused as dying. */
1839 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1840 if (REG_NOTE_KIND (link) == REG_UNUSED
1841 && GET_CODE (XEXP (link, 0)) == REG)
1842 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1847 if (first == BLOCK_END (b))
1848 b++;
1850 /* Stop after we pass the end of the last basic block. Verify that
1851 no real insns are after the end of the last basic block.
1853 We may want to reorganize the loop somewhat since this test should
1854 always be the right exit test. */
1855 if (b == n_basic_blocks)
1857 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1858 if (INSN_P (first) && GET_CODE (PATTERN (first)) != USE)
1859 abort ();
1860 break;
1863 FREE_REG_SET (live_relevant_regs);
1864 *p = 0;
1867 /* Print debugging trace information if -dg switch is given,
1868 showing the information on which the allocation decisions are based. */
1870 static void
1871 dump_conflicts (file)
1872 FILE *file;
1874 register int i;
1875 register int has_preferences;
1876 register int nregs;
1877 nregs = 0;
1878 for (i = 0; i < max_allocno; i++)
1880 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1881 continue;
1882 nregs++;
1884 fprintf (file, ";; %d regs to allocate:", nregs);
1885 for (i = 0; i < max_allocno; i++)
1887 int j;
1888 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1889 continue;
1890 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1891 for (j = 0; j < max_regno; j++)
1892 if (reg_allocno[j] == allocno_order[i]
1893 && j != allocno[allocno_order[i]].reg)
1894 fprintf (file, "+%d", j);
1895 if (allocno[allocno_order[i]].size != 1)
1896 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1898 fprintf (file, "\n");
1900 for (i = 0; i < max_allocno; i++)
1902 register int j;
1903 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1904 for (j = 0; j < max_allocno; j++)
1905 if (CONFLICTP (j, i))
1906 fprintf (file, " %d", allocno[j].reg);
1907 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1908 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1909 fprintf (file, " %d", j);
1910 fprintf (file, "\n");
1912 has_preferences = 0;
1913 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1914 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1915 has_preferences = 1;
1917 if (! has_preferences)
1918 continue;
1919 fprintf (file, ";; %d preferences:", allocno[i].reg);
1920 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1921 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1922 fprintf (file, " %d", j);
1923 fprintf (file, "\n");
1925 fprintf (file, "\n");
1928 void
1929 dump_global_regs (file)
1930 FILE *file;
1932 register int i, j;
1934 fprintf (file, ";; Register dispositions:\n");
1935 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1936 if (reg_renumber[i] >= 0)
1938 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1939 if (++j % 6 == 0)
1940 fprintf (file, "\n");
1943 fprintf (file, "\n\n;; Hard regs used: ");
1944 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1945 if (regs_ever_live[i])
1946 fprintf (file, " %d", i);
1947 fprintf (file, "\n\n");