* cp-tree.h (DECL_LOCAL_FUCNTION_P): New macro.
[official-gcc.git] / gcc / global.c
blob7e0074cbd63df7dafab03a0a524d835e16f08b85
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 symmetric after the call to mirror_conflicts. */
119 static INT_TYPE *conflicts;
121 /* Number of ints require to hold max_allocno bits.
122 This is the length of a row in `conflicts'. */
124 static int allocno_row_words;
126 /* Two macros to test or store 1 in an element of `conflicts'. */
128 #define CONFLICTP(I, J) \
129 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
130 & ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
132 #define SET_CONFLICT(I, J) \
133 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
134 |= ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
136 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
137 and execute CODE. */
138 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
139 do { \
140 int i_; \
141 int allocno_; \
142 INT_TYPE *p_ = (ALLOCNO_SET); \
144 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
145 i_--, allocno_ += INT_BITS) \
147 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
149 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
151 if (word_ & 1) \
152 {CODE;} \
155 } while (0)
157 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
158 #if 0
159 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
160 the conflicting allocno, and execute CODE. This macro assumes that
161 mirror_conflicts has been run. */
162 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
163 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
164 OUT_ALLOCNO, (CODE))
165 #endif
167 /* Set of hard regs currently live (during scan of all insns). */
169 static HARD_REG_SET hard_regs_live;
171 /* Indexed by N, set of hard regs conflicting with allocno N. */
173 static HARD_REG_SET *hard_reg_conflicts;
175 /* Indexed by N, set of hard regs preferred by allocno N.
176 This is used to make allocnos go into regs that are copied to or from them,
177 when possible, to reduce register shuffling. */
179 static HARD_REG_SET *hard_reg_preferences;
181 /* Similar, but just counts register preferences made in simple copy
182 operations, rather than arithmetic. These are given priority because
183 we can always eliminate an insn by using these, but using a register
184 in the above list won't always eliminate an insn. */
186 static HARD_REG_SET *hard_reg_copy_preferences;
188 /* Similar to hard_reg_preferences, but includes bits for subsequent
189 registers when an allocno is multi-word. The above variable is used for
190 allocation while this is used to build reg_someone_prefers, below. */
192 static HARD_REG_SET *hard_reg_full_preferences;
194 /* Indexed by N, set of hard registers that some later allocno has a
195 preference for. */
197 static HARD_REG_SET *regs_someone_prefers;
199 /* Set of registers that global-alloc isn't supposed to use. */
201 static HARD_REG_SET no_global_alloc_regs;
203 /* Set of registers used so far. */
205 static HARD_REG_SET regs_used_so_far;
207 /* Number of calls crossed by each allocno. */
209 static int *allocno_calls_crossed;
211 /* Number of refs (weighted) to each allocno. */
213 static int *allocno_n_refs;
215 /* Guess at live length of each allocno.
216 This is actually the max of the live lengths of the regs. */
218 static int *allocno_live_length;
220 /* Number of refs (weighted) to each hard reg, as used by local alloc.
221 It is zero for a reg that contains global pseudos or is explicitly used. */
223 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
225 /* Guess at live length of each hard reg, as used by local alloc.
226 This is actually the sum of the live lengths of the specific regs. */
228 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
230 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
231 for vector element I, and hard register number J. */
233 #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)
235 /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
237 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)
239 /* Bit mask for allocnos live at current point in the scan. */
241 static INT_TYPE *allocnos_live;
243 /* Test, set or clear bit number I in allocnos_live,
244 a bit vector indexed by allocno. */
246 #define ALLOCNO_LIVE_P(I) \
247 (allocnos_live[(unsigned)(I) / INT_BITS] \
248 & ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
250 #define SET_ALLOCNO_LIVE(I) \
251 (allocnos_live[(unsigned)(I) / INT_BITS] \
252 |= ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
254 #define CLEAR_ALLOCNO_LIVE(I) \
255 (allocnos_live[(unsigned)(I) / INT_BITS] \
256 &= ~((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
258 /* This is turned off because it doesn't work right for DImode.
259 (And it is only used for DImode, so the other cases are worthless.)
260 The problem is that it isn't true that there is NO possibility of conflict;
261 only that there is no conflict if the two pseudos get the exact same regs.
262 If they were allocated with a partial overlap, there would be a conflict.
263 We can't safely turn off the conflict unless we have another way to
264 prevent the partial overlap.
266 Idea: change hard_reg_conflicts so that instead of recording which
267 hard regs the allocno may not overlap, it records where the allocno
268 may not start. Change both where it is used and where it is updated.
269 Then there is a way to record that (reg:DI 108) may start at 10
270 but not at 9 or 11. There is still the question of how to record
271 this semi-conflict between two pseudos. */
272 #if 0
273 /* Reg pairs for which conflict after the current insn
274 is inhibited by a REG_NO_CONFLICT note.
275 If the table gets full, we ignore any other notes--that is conservative. */
276 #define NUM_NO_CONFLICT_PAIRS 4
277 /* Number of pairs in use in this insn. */
278 int n_no_conflict_pairs;
279 static struct { int allocno1, allocno2;}
280 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
281 #endif /* 0 */
283 /* Record all regs that are set in any one insn.
284 Communication from mark_reg_{store,clobber} and global_conflicts. */
286 static rtx *regs_set;
287 static int n_regs_set;
289 /* All registers that can be eliminated. */
291 static HARD_REG_SET eliminable_regset;
293 static int allocno_compare PROTO((const PTR, const PTR));
294 static void global_conflicts PROTO((void));
295 static void mirror_conflicts PROTO((void));
296 static void expand_preferences PROTO((void));
297 static void prune_preferences PROTO((void));
298 static void find_reg PROTO((int, HARD_REG_SET, int, int, int));
299 static void record_one_conflict PROTO((int));
300 static void record_conflicts PROTO((int *, int));
301 static void mark_reg_store PROTO((rtx, rtx, void *));
302 static void mark_reg_clobber PROTO((rtx, rtx, void *));
303 static void mark_reg_conflicts PROTO((rtx));
304 static void mark_reg_death PROTO((rtx));
305 static void mark_reg_live_nc PROTO((int, enum machine_mode));
306 static void set_preference PROTO((rtx, rtx));
307 static void dump_conflicts PROTO((FILE *));
308 static void reg_becomes_live PROTO((rtx, rtx, void *));
309 static void reg_dies PROTO((int, enum machine_mode));
310 static void build_insn_chain PROTO((rtx));
312 /* Perform allocation of pseudo-registers not allocated by local_alloc.
313 FILE is a file to output debugging information on,
314 or zero if such output is not desired.
316 Return value is nonzero if reload failed
317 and we must not do any more for this function. */
320 global_alloc (file)
321 FILE *file;
323 int retval;
324 #ifdef ELIMINABLE_REGS
325 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
326 #endif
327 int need_fp
328 = (! flag_omit_frame_pointer
329 #ifdef EXIT_IGNORE_STACK
330 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
331 #endif
332 || FRAME_POINTER_REQUIRED);
334 register size_t i;
335 rtx x;
337 max_allocno = 0;
339 /* A machine may have certain hard registers that
340 are safe to use only within a basic block. */
342 CLEAR_HARD_REG_SET (no_global_alloc_regs);
343 #ifdef OVERLAPPING_REGNO_P
344 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
345 if (OVERLAPPING_REGNO_P (i))
346 SET_HARD_REG_BIT (no_global_alloc_regs, i);
347 #endif
349 /* Build the regset of all eliminable registers and show we can't use those
350 that we already know won't be eliminated. */
351 #ifdef ELIMINABLE_REGS
352 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
354 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
356 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
357 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
358 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
360 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
361 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
362 if (need_fp)
363 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
364 #endif
366 #else
367 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
368 if (need_fp)
369 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
370 #endif
372 /* Track which registers have already been used. Start with registers
373 explicitly in the rtl, then registers allocated by local register
374 allocation. */
376 CLEAR_HARD_REG_SET (regs_used_so_far);
377 #ifdef LEAF_REGISTERS
378 /* If we are doing the leaf function optimization, and this is a leaf
379 function, it means that the registers that take work to save are those
380 that need a register window. So prefer the ones that can be used in
381 a leaf function. */
383 char *cheap_regs;
384 static char leaf_regs[] = LEAF_REGISTERS;
386 if (only_leaf_regs_used () && leaf_function_p ())
387 cheap_regs = leaf_regs;
388 else
389 cheap_regs = call_used_regs;
390 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
391 if (regs_ever_live[i] || cheap_regs[i])
392 SET_HARD_REG_BIT (regs_used_so_far, i);
394 #else
395 /* We consider registers that do not have to be saved over calls as if
396 they were already used since there is no cost in using them. */
397 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
398 if (regs_ever_live[i] || call_used_regs[i])
399 SET_HARD_REG_BIT (regs_used_so_far, i);
400 #endif
402 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
403 if (reg_renumber[i] >= 0)
404 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
406 /* Establish mappings from register number to allocation number
407 and vice versa. In the process, count the allocnos. */
409 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
411 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
412 reg_allocno[i] = -1;
414 /* Initialize the shared-hard-reg mapping
415 from the list of pairs that may share. */
416 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
417 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
419 int r1 = REGNO (XEXP (x, 0));
420 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
421 if (r1 > r2)
422 reg_may_share[r1] = r2;
423 else
424 reg_may_share[r2] = r1;
427 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
428 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
429 that we are supposed to refrain from putting in a hard reg.
430 -2 means do make an allocno but don't allocate it. */
431 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
432 /* Don't allocate pseudos that cross calls,
433 if this function receives a nonlocal goto. */
434 && (! current_function_has_nonlocal_label
435 || REG_N_CALLS_CROSSED (i) == 0))
437 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
438 reg_allocno[i] = reg_allocno[reg_may_share[i]];
439 else
440 reg_allocno[i] = max_allocno++;
441 if (REG_LIVE_LENGTH (i) == 0)
442 abort ();
444 else
445 reg_allocno[i] = -1;
447 allocno_reg = (int *) xmalloc (max_allocno * sizeof (int));
448 allocno_size = (int *) xcalloc (max_allocno, sizeof (int));
449 allocno_calls_crossed = (int *) xcalloc (max_allocno, sizeof (int));
450 allocno_n_refs = (int *) xcalloc (max_allocno, sizeof (int));
451 allocno_live_length = (int *) xcalloc (max_allocno, sizeof (int));
453 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
454 if (reg_allocno[i] >= 0)
456 int allocno = reg_allocno[i];
457 allocno_reg[allocno] = i;
458 allocno_size[allocno] = PSEUDO_REGNO_SIZE (i);
459 allocno_calls_crossed[allocno] += REG_N_CALLS_CROSSED (i);
460 allocno_n_refs[allocno] += REG_N_REFS (i);
461 if (allocno_live_length[allocno] < REG_LIVE_LENGTH (i))
462 allocno_live_length[allocno] = REG_LIVE_LENGTH (i);
465 /* Calculate amount of usage of each hard reg by pseudos
466 allocated by local-alloc. This is to see if we want to
467 override it. */
468 bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
469 bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
470 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
471 if (reg_renumber[i] >= 0)
473 int regno = reg_renumber[i];
474 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
475 int j;
477 for (j = regno; j < endregno; j++)
479 local_reg_n_refs[j] += REG_N_REFS (i);
480 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
484 /* We can't override local-alloc for a reg used not just by local-alloc. */
485 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
486 if (regs_ever_live[i])
487 local_reg_n_refs[i] = 0;
489 /* Allocate the space for the conflict and preference tables and
490 initialize them. */
492 hard_reg_conflicts
493 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
494 hard_reg_preferences
495 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
496 hard_reg_copy_preferences
497 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
498 hard_reg_full_preferences
499 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
500 regs_someone_prefers
501 = (HARD_REG_SET *) xcalloc (max_allocno, sizeof (HARD_REG_SET));
503 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
505 /* We used to use alloca here, but the size of what it would try to
506 allocate would occasionally cause it to exceed the stack limit and
507 cause unpredictable core dumps. Some examples were > 2Mb in size. */
508 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
509 sizeof (INT_TYPE));
511 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
513 /* If there is work to be done (at least one reg to allocate),
514 perform global conflict analysis and allocate the regs. */
516 if (max_allocno > 0)
518 /* Scan all the insns and compute the conflicts among allocnos
519 and between allocnos and hard regs. */
521 global_conflicts ();
523 mirror_conflicts ();
525 /* Eliminate conflicts between pseudos and eliminable registers. If
526 the register is not eliminated, the pseudo won't really be able to
527 live in the eliminable register, so the conflict doesn't matter.
528 If we do eliminate the register, the conflict will no longer exist.
529 So in either case, we can ignore the conflict. Likewise for
530 preferences. */
532 for (i = 0; i < (size_t) max_allocno; i++)
534 AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
535 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
536 eliminable_regset);
537 AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
540 /* Try to expand the preferences by merging them between allocnos. */
542 expand_preferences ();
544 /* Determine the order to allocate the remaining pseudo registers. */
546 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
547 for (i = 0; i < (size_t) max_allocno; i++)
548 allocno_order[i] = i;
550 /* Default the size to 1, since allocno_compare uses it to divide by.
551 Also convert allocno_live_length of zero to -1. A length of zero
552 can occur when all the registers for that allocno have reg_live_length
553 equal to -2. In this case, we want to make an allocno, but not
554 allocate it. So avoid the divide-by-zero and set it to a low
555 priority. */
557 for (i = 0; i < (size_t) max_allocno; i++)
559 if (allocno_size[i] == 0)
560 allocno_size[i] = 1;
561 if (allocno_live_length[i] == 0)
562 allocno_live_length[i] = -1;
565 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
567 prune_preferences ();
569 if (file)
570 dump_conflicts (file);
572 /* Try allocating them, one by one, in that order,
573 except for parameters marked with reg_live_length[regno] == -2. */
575 for (i = 0; i < (size_t) max_allocno; i++)
576 if (reg_renumber[allocno_reg[allocno_order[i]]] < 0
577 && REG_LIVE_LENGTH (allocno_reg[allocno_order[i]]) >= 0)
579 /* If we have more than one register class,
580 first try allocating in the class that is cheapest
581 for this pseudo-reg. If that fails, try any reg. */
582 if (N_REG_CLASSES > 1)
584 find_reg (allocno_order[i], 0, 0, 0, 0);
585 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
586 continue;
588 if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
589 find_reg (allocno_order[i], 0, 1, 0, 0);
592 free (allocno_order);
595 /* Do the reloads now while the allocno data still exist, so that we can
596 try to assign new hard regs to any pseudo regs that are spilled. */
598 #if 0 /* We need to eliminate regs even if there is no rtl code,
599 for the sake of debugging information. */
600 if (n_basic_blocks > 0)
601 #endif
603 build_insn_chain (get_insns ());
604 retval = reload (get_insns (), 1, file);
607 /* Clean up. */
608 free (reg_allocno);
609 free (reg_may_share);
610 free (allocno_reg);
611 free (allocno_size);
612 free (allocno_calls_crossed);
613 free (allocno_n_refs);
614 free (allocno_live_length);
615 free (hard_reg_conflicts);
616 free (hard_reg_preferences);
617 free (hard_reg_copy_preferences);
618 free (hard_reg_full_preferences);
619 free (regs_someone_prefers);
620 free (conflicts);
621 free (allocnos_live);
623 return retval;
626 /* Sort predicate for ordering the allocnos.
627 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
629 static int
630 allocno_compare (v1p, v2p)
631 const PTR v1p;
632 const PTR v2p;
634 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
635 /* Note that the quotient will never be bigger than
636 the value of floor_log2 times the maximum number of
637 times a register can occur in one insn (surely less than 100).
638 Multiplying this by 10000 can't overflow. */
639 register int pri1
640 = (((double) (floor_log2 (allocno_n_refs[v1]) * allocno_n_refs[v1])
641 / allocno_live_length[v1])
642 * 10000 * allocno_size[v1]);
643 register int pri2
644 = (((double) (floor_log2 (allocno_n_refs[v2]) * allocno_n_refs[v2])
645 / allocno_live_length[v2])
646 * 10000 * allocno_size[v2]);
647 if (pri2 - pri1)
648 return pri2 - pri1;
650 /* If regs are equally good, sort by allocno,
651 so that the results of qsort leave nothing to chance. */
652 return v1 - v2;
655 /* Scan the rtl code and record all conflicts and register preferences in the
656 conflict matrices and preference tables. */
658 static void
659 global_conflicts ()
661 register int b, i;
662 register rtx insn;
663 int *block_start_allocnos;
665 /* Make a vector that mark_reg_{store,clobber} will store in. */
666 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
668 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
670 for (b = 0; b < n_basic_blocks; b++)
672 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
674 /* Initialize table of registers currently live
675 to the state at the beginning of this basic block.
676 This also marks the conflicts among hard registers
677 and any allocnos that are live.
679 For pseudo-regs, there is only one bit for each one
680 no matter how many hard regs it occupies.
681 This is ok; we know the size from PSEUDO_REGNO_SIZE.
682 For explicit hard regs, we cannot know the size that way
683 since one hard reg can be used with various sizes.
684 Therefore, we must require that all the hard regs
685 implicitly live as part of a multi-word hard reg
686 are explicitly marked in basic_block_live_at_start. */
689 register regset old = BASIC_BLOCK (b)->global_live_at_start;
690 int ax = 0;
692 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
693 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
695 register int a = reg_allocno[i];
696 if (a >= 0)
698 SET_ALLOCNO_LIVE (a);
699 block_start_allocnos[ax++] = a;
701 else if ((a = reg_renumber[i]) >= 0)
702 mark_reg_live_nc
703 (a, PSEUDO_REGNO_MODE (i));
706 /* Record that each allocno now live conflicts with each hard reg
707 now live.
709 It is not necessary to mark any conflicts between pseudos as
710 this point, even for pseudos which are live at the start of
711 the basic block.
713 Given two pseudos X and Y and any point in the CFG P.
715 On any path to point P where X and Y are live one of the
716 following conditions must be true:
718 1. X is live at some instruction on the path that
719 evaluates Y.
721 2. Y is live at some instruction on the path that
722 evaluates X.
724 3. Either X or Y is not evaluted on the path to P
725 (ie it is used uninitialized) and thus the
726 conflict can be ignored.
728 In cases #1 and #2 the conflict will be recorded when we
729 scan the instruction that makes either X or Y become live. */
730 record_conflicts (block_start_allocnos, ax);
732 #ifdef STACK_REGS
734 /* Pseudos can't go in stack regs at the start of a basic block
735 that is reached by an abnormal edge. */
737 edge e;
738 for (e = BASIC_BLOCK (b)->pred; e ; e = e->pred_next)
739 if (e->flags & EDGE_ABNORMAL)
740 break;
741 if (e != NULL)
742 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
743 record_one_conflict (ax);
745 #endif
748 insn = BLOCK_HEAD (b);
750 /* Scan the code of this basic block, noting which allocnos
751 and hard regs are born or die. When one is born,
752 record a conflict with all others currently live. */
754 while (1)
756 register RTX_CODE code = GET_CODE (insn);
757 register rtx link;
759 /* Make regs_set an empty set. */
761 n_regs_set = 0;
763 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
766 #if 0
767 int i = 0;
768 for (link = REG_NOTES (insn);
769 link && i < NUM_NO_CONFLICT_PAIRS;
770 link = XEXP (link, 1))
771 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
773 no_conflict_pairs[i].allocno1
774 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
775 no_conflict_pairs[i].allocno2
776 = reg_allocno[REGNO (XEXP (link, 0))];
777 i++;
779 #endif /* 0 */
781 /* Mark any registers clobbered by INSN as live,
782 so they conflict with the inputs. */
784 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
786 /* Mark any registers dead after INSN as dead now. */
788 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
789 if (REG_NOTE_KIND (link) == REG_DEAD)
790 mark_reg_death (XEXP (link, 0));
792 /* Mark any registers set in INSN as live,
793 and mark them as conflicting with all other live regs.
794 Clobbers are processed again, so they conflict with
795 the registers that are set. */
797 note_stores (PATTERN (insn), mark_reg_store, NULL);
799 #ifdef AUTO_INC_DEC
800 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
801 if (REG_NOTE_KIND (link) == REG_INC)
802 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
803 #endif
805 /* If INSN has multiple outputs, then any reg that dies here
806 and is used inside of an output
807 must conflict with the other outputs.
809 It is unsafe to use !single_set here since it will ignore an
810 unused output. Just because an output is unused does not mean
811 the compiler can assume the side effect will not occur.
812 Consider if REG appears in the address of an output and we
813 reload the output. If we allocate REG to the same hard
814 register as an unused output we could set the hard register
815 before the output reload insn. */
816 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
817 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
818 if (REG_NOTE_KIND (link) == REG_DEAD)
820 int used_in_output = 0;
821 int i;
822 rtx reg = XEXP (link, 0);
824 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
826 rtx set = XVECEXP (PATTERN (insn), 0, i);
827 if (GET_CODE (set) == SET
828 && GET_CODE (SET_DEST (set)) != REG
829 && !rtx_equal_p (reg, SET_DEST (set))
830 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
831 used_in_output = 1;
833 if (used_in_output)
834 mark_reg_conflicts (reg);
837 /* Mark any registers set in INSN and then never used. */
839 while (n_regs_set > 0)
840 if (find_regno_note (insn, REG_UNUSED,
841 REGNO (regs_set[--n_regs_set])))
842 mark_reg_death (regs_set[n_regs_set]);
845 if (insn == BLOCK_END (b))
846 break;
847 insn = NEXT_INSN (insn);
851 /* Clean up. */
852 free (block_start_allocnos);
853 free (regs_set);
855 /* Expand the preference information by looking for cases where one allocno
856 dies in an insn that sets an allocno. If those two allocnos don't conflict,
857 merge any preferences between those allocnos. */
859 static void
860 expand_preferences ()
862 rtx insn;
863 rtx link;
864 rtx set;
866 /* We only try to handle the most common cases here. Most of the cases
867 where this wins are reg-reg copies. */
869 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
870 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
871 && (set = single_set (insn)) != 0
872 && GET_CODE (SET_DEST (set)) == REG
873 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
874 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
875 if (REG_NOTE_KIND (link) == REG_DEAD
876 && GET_CODE (XEXP (link, 0)) == REG
877 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
878 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
879 reg_allocno[REGNO (XEXP (link, 0))]))
881 int a1 = reg_allocno[REGNO (SET_DEST (set))];
882 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
884 if (XEXP (link, 0) == SET_SRC (set))
886 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
887 hard_reg_copy_preferences[a2]);
888 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
889 hard_reg_copy_preferences[a1]);
892 IOR_HARD_REG_SET (hard_reg_preferences[a1],
893 hard_reg_preferences[a2]);
894 IOR_HARD_REG_SET (hard_reg_preferences[a2],
895 hard_reg_preferences[a1]);
896 IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
897 hard_reg_full_preferences[a2]);
898 IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
899 hard_reg_full_preferences[a1]);
903 /* Prune the preferences for global registers to exclude registers that cannot
904 be used.
906 Compute `regs_someone_prefers', which is a bitmask of the hard registers
907 that are preferred by conflicting registers of lower priority. If possible,
908 we will avoid using these registers. */
910 static void
911 prune_preferences ()
913 int i;
914 int allocno;
915 int *allocno_to_order = (int *) xmalloc (max_allocno * sizeof (int));
917 /* Scan least most important to most important.
918 For each allocno, remove from preferences registers that cannot be used,
919 either because of conflicts or register type. Then compute all registers
920 preferred by each lower-priority register that conflicts. */
922 for (i = max_allocno - 1; i >= 0; i--)
924 HARD_REG_SET temp;
926 allocno = allocno_order[i];
927 allocno_to_order[allocno] = i;
928 COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
930 if (allocno_calls_crossed[allocno] == 0)
931 IOR_HARD_REG_SET (temp, fixed_reg_set);
932 else
933 IOR_HARD_REG_SET (temp, call_used_reg_set);
935 IOR_COMPL_HARD_REG_SET
936 (temp,
937 reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
939 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
940 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
941 AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
944 for (i = max_allocno - 1; i >= 0; i--)
946 /* Merge in the preferences of lower-priority registers (they have
947 already been pruned). If we also prefer some of those registers,
948 don't exclude them unless we are of a smaller size (in which case
949 we want to give the lower-priority allocno the first chance for
950 these registers). */
951 HARD_REG_SET temp, temp2;
952 int allocno2;
954 allocno = allocno_order[i];
956 CLEAR_HARD_REG_SET (temp);
957 CLEAR_HARD_REG_SET (temp2);
959 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + allocno * allocno_row_words,
960 allocno2,
962 if (allocno_to_order[allocno2] > i)
964 if (allocno_size[allocno2] <= allocno_size[allocno])
965 IOR_HARD_REG_SET (temp, hard_reg_full_preferences[allocno2]);
966 else
967 IOR_HARD_REG_SET (temp2, hard_reg_full_preferences[allocno2]);
971 AND_COMPL_HARD_REG_SET (temp, hard_reg_full_preferences[allocno]);
972 IOR_HARD_REG_SET (temp, temp2);
973 COPY_HARD_REG_SET (regs_someone_prefers[allocno], temp);
975 free (allocno_to_order);
978 /* Assign a hard register to ALLOCNO; look for one that is the beginning
979 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
980 The registers marked in PREFREGS are tried first.
982 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
983 be used for this allocation.
985 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
986 Otherwise ignore that preferred class and use the alternate class.
988 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
989 will have to be saved and restored at calls.
991 RETRYING is nonzero if this is called from retry_global_alloc.
993 If we find one, record it in reg_renumber.
994 If not, do nothing. */
996 static void
997 find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
998 int allocno;
999 HARD_REG_SET losers;
1000 int alt_regs_p;
1001 int accept_call_clobbered;
1002 int retrying;
1004 register int i, best_reg, pass;
1005 #ifdef HARD_REG_SET
1006 register /* Declare it register if it's a scalar. */
1007 #endif
1008 HARD_REG_SET used, used1, used2;
1010 enum reg_class class = (alt_regs_p
1011 ? reg_alternate_class (allocno_reg[allocno])
1012 : reg_preferred_class (allocno_reg[allocno]));
1013 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
1015 if (accept_call_clobbered)
1016 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
1017 else if (allocno_calls_crossed[allocno] == 0)
1018 COPY_HARD_REG_SET (used1, fixed_reg_set);
1019 else
1020 COPY_HARD_REG_SET (used1, call_used_reg_set);
1022 /* Some registers should not be allocated in global-alloc. */
1023 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1024 if (losers)
1025 IOR_HARD_REG_SET (used1, losers);
1027 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1028 COPY_HARD_REG_SET (used2, used1);
1030 IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
1032 #ifdef CLASS_CANNOT_CHANGE_SIZE
1033 if (REG_CHANGES_SIZE (allocno_reg[allocno]))
1034 IOR_HARD_REG_SET (used1,
1035 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
1036 #endif
1038 /* Try each hard reg to see if it fits. Do this in two passes.
1039 In the first pass, skip registers that are preferred by some other pseudo
1040 to give it a better chance of getting one of those registers. Only if
1041 we can't get a register when excluding those do we take one of them.
1042 However, we never allocate a register for the first time in pass 0. */
1044 COPY_HARD_REG_SET (used, used1);
1045 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1046 IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
1048 best_reg = -1;
1049 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1050 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1051 pass++)
1053 if (pass == 1)
1054 COPY_HARD_REG_SET (used, used1);
1055 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1057 #ifdef REG_ALLOC_ORDER
1058 int regno = reg_alloc_order[i];
1059 #else
1060 int regno = i;
1061 #endif
1062 if (! TEST_HARD_REG_BIT (used, regno)
1063 && HARD_REGNO_MODE_OK (regno, mode)
1064 && (allocno_calls_crossed[allocno] == 0
1065 || accept_call_clobbered
1066 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1068 register int j;
1069 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
1070 for (j = regno + 1;
1071 (j < lim
1072 && ! TEST_HARD_REG_BIT (used, j));
1073 j++);
1074 if (j == lim)
1076 best_reg = regno;
1077 break;
1079 #ifndef REG_ALLOC_ORDER
1080 i = j; /* Skip starting points we know will lose */
1081 #endif
1086 /* See if there is a preferred register with the same class as the register
1087 we allocated above. Making this restriction prevents register
1088 preferencing from creating worse register allocation.
1090 Remove from the preferred registers and conflicting registers. Note that
1091 additional conflicts may have been added after `prune_preferences' was
1092 called.
1094 First do this for those register with copy preferences, then all
1095 preferred registers. */
1097 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
1098 GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
1099 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1101 if (best_reg >= 0)
1103 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1104 if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
1105 && HARD_REGNO_MODE_OK (i, mode)
1106 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1107 || reg_class_subset_p (REGNO_REG_CLASS (i),
1108 REGNO_REG_CLASS (best_reg))
1109 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1110 REGNO_REG_CLASS (i))))
1112 register int j;
1113 register int lim = i + HARD_REGNO_NREGS (i, mode);
1114 for (j = i + 1;
1115 (j < lim
1116 && ! TEST_HARD_REG_BIT (used, j)
1117 && (REGNO_REG_CLASS (j)
1118 == REGNO_REG_CLASS (best_reg + (j - i))
1119 || reg_class_subset_p (REGNO_REG_CLASS (j),
1120 REGNO_REG_CLASS (best_reg + (j - i)))
1121 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1122 REGNO_REG_CLASS (j))));
1123 j++);
1124 if (j == lim)
1126 best_reg = i;
1127 goto no_prefs;
1131 no_copy_prefs:
1133 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1134 GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1135 reg_class_contents[(int) NO_REGS], no_prefs);
1137 if (best_reg >= 0)
1139 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1140 if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1141 && HARD_REGNO_MODE_OK (i, mode)
1142 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1143 || reg_class_subset_p (REGNO_REG_CLASS (i),
1144 REGNO_REG_CLASS (best_reg))
1145 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1146 REGNO_REG_CLASS (i))))
1148 register int j;
1149 register int lim = i + HARD_REGNO_NREGS (i, mode);
1150 for (j = i + 1;
1151 (j < lim
1152 && ! TEST_HARD_REG_BIT (used, j)
1153 && (REGNO_REG_CLASS (j)
1154 == REGNO_REG_CLASS (best_reg + (j - i))
1155 || reg_class_subset_p (REGNO_REG_CLASS (j),
1156 REGNO_REG_CLASS (best_reg + (j - i)))
1157 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1158 REGNO_REG_CLASS (j))));
1159 j++);
1160 if (j == lim)
1162 best_reg = i;
1163 break;
1167 no_prefs:
1169 /* If we haven't succeeded yet, try with caller-saves.
1170 We need not check to see if the current function has nonlocal
1171 labels because we don't put any pseudos that are live over calls in
1172 registers in that case. */
1174 if (flag_caller_saves && best_reg < 0)
1176 /* Did not find a register. If it would be profitable to
1177 allocate a call-clobbered register and save and restore it
1178 around calls, do that. */
1179 if (! accept_call_clobbered
1180 && allocno_calls_crossed[allocno] != 0
1181 && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1182 allocno_calls_crossed[allocno]))
1184 HARD_REG_SET new_losers;
1185 if (! losers)
1186 CLEAR_HARD_REG_SET (new_losers);
1187 else
1188 COPY_HARD_REG_SET (new_losers, losers);
1190 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1191 find_reg (allocno, new_losers, alt_regs_p, 1, retrying);
1192 if (reg_renumber[allocno_reg[allocno]] >= 0)
1194 caller_save_needed = 1;
1195 return;
1200 /* If we haven't succeeded yet,
1201 see if some hard reg that conflicts with us
1202 was utilized poorly by local-alloc.
1203 If so, kick out the regs that were put there by local-alloc
1204 so we can use it instead. */
1205 if (best_reg < 0 && !retrying
1206 /* Let's not bother with multi-reg allocnos. */
1207 && allocno_size[allocno] == 1)
1209 /* Count from the end, to find the least-used ones first. */
1210 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1212 #ifdef REG_ALLOC_ORDER
1213 int regno = reg_alloc_order[i];
1214 #else
1215 int regno = i;
1216 #endif
1218 if (local_reg_n_refs[regno] != 0
1219 /* Don't use a reg no good for this pseudo. */
1220 && ! TEST_HARD_REG_BIT (used2, regno)
1221 && HARD_REGNO_MODE_OK (regno, mode)
1222 #ifdef CLASS_CANNOT_CHANGE_SIZE
1223 && ! (REG_CHANGES_SIZE (allocno_reg[allocno])
1224 && (TEST_HARD_REG_BIT
1225 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1226 regno)))
1227 #endif
1230 /* We explicitly evaluate the divide results into temporary
1231 variables so as to avoid excess precision problems that occur
1232 on a i386-unknown-sysv4.2 (unixware) host. */
1234 double tmp1 = ((double) local_reg_n_refs[regno]
1235 / local_reg_live_length[regno]);
1236 double tmp2 = ((double) allocno_n_refs[allocno]
1237 / allocno_live_length[allocno]);
1239 if (tmp1 < tmp2)
1241 /* Hard reg REGNO was used less in total by local regs
1242 than it would be used by this one allocno! */
1243 int k;
1244 for (k = 0; k < max_regno; k++)
1245 if (reg_renumber[k] >= 0)
1247 int r = reg_renumber[k];
1248 int endregno
1249 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1251 if (regno >= r && regno < endregno)
1252 reg_renumber[k] = -1;
1255 best_reg = regno;
1256 break;
1262 /* Did we find a register? */
1264 if (best_reg >= 0)
1266 register int lim, j;
1267 HARD_REG_SET this_reg;
1269 /* Yes. Record it as the hard register of this pseudo-reg. */
1270 reg_renumber[allocno_reg[allocno]] = best_reg;
1271 /* Also of any pseudo-regs that share with it. */
1272 if (reg_may_share[allocno_reg[allocno]])
1273 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1274 if (reg_allocno[j] == allocno)
1275 reg_renumber[j] = best_reg;
1277 /* Make a set of the hard regs being allocated. */
1278 CLEAR_HARD_REG_SET (this_reg);
1279 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1280 for (j = best_reg; j < lim; j++)
1282 SET_HARD_REG_BIT (this_reg, j);
1283 SET_HARD_REG_BIT (regs_used_so_far, j);
1284 /* This is no longer a reg used just by local regs. */
1285 local_reg_n_refs[j] = 0;
1287 /* For each other pseudo-reg conflicting with this one,
1288 mark it as conflicting with the hard regs this one occupies. */
1289 lim = allocno;
1290 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1292 IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1297 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1298 Perhaps it had previously seemed not worth a hard reg,
1299 or perhaps its old hard reg has been commandeered for reloads.
1300 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1301 they do not appear to be allocated.
1302 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1304 void
1305 retry_global_alloc (regno, forbidden_regs)
1306 int regno;
1307 HARD_REG_SET forbidden_regs;
1309 int allocno = reg_allocno[regno];
1310 if (allocno >= 0)
1312 /* If we have more than one register class,
1313 first try allocating in the class that is cheapest
1314 for this pseudo-reg. If that fails, try any reg. */
1315 if (N_REG_CLASSES > 1)
1316 find_reg (allocno, forbidden_regs, 0, 0, 1);
1317 if (reg_renumber[regno] < 0
1318 && reg_alternate_class (regno) != NO_REGS)
1319 find_reg (allocno, forbidden_regs, 1, 0, 1);
1321 /* If we found a register, modify the RTL for the register to
1322 show the hard register, and mark that register live. */
1323 if (reg_renumber[regno] >= 0)
1325 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1326 mark_home_live (regno);
1331 /* Record a conflict between register REGNO
1332 and everything currently live.
1333 REGNO must not be a pseudo reg that was allocated
1334 by local_alloc; such numbers must be translated through
1335 reg_renumber before calling here. */
1337 static void
1338 record_one_conflict (regno)
1339 int regno;
1341 register int j;
1343 if (regno < FIRST_PSEUDO_REGISTER)
1344 /* When a hard register becomes live,
1345 record conflicts with live pseudo regs. */
1346 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1348 SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1350 else
1351 /* When a pseudo-register becomes live,
1352 record conflicts first with hard regs,
1353 then with other pseudo regs. */
1355 register int ialloc = reg_allocno[regno];
1356 register int ialloc_prod = ialloc * allocno_row_words;
1357 IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1358 for (j = allocno_row_words - 1; j >= 0; j--)
1360 #if 0
1361 int k;
1362 for (k = 0; k < n_no_conflict_pairs; k++)
1363 if (! ((j == no_conflict_pairs[k].allocno1
1364 && ialloc == no_conflict_pairs[k].allocno2)
1366 (j == no_conflict_pairs[k].allocno2
1367 && ialloc == no_conflict_pairs[k].allocno1)))
1368 #endif /* 0 */
1369 conflicts[ialloc_prod + j] |= allocnos_live[j];
1374 /* Record all allocnos currently live as conflicting
1375 with all hard regs currently live.
1377 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1378 are currently live. Their bits are also flagged in allocnos_live. */
1380 static void
1381 record_conflicts (allocno_vec, len)
1382 register int *allocno_vec;
1383 register int len;
1385 register int allocno;
1386 register int j;
1387 register int ialloc_prod;
1389 while (--len >= 0)
1391 allocno = allocno_vec[len];
1392 ialloc_prod = allocno * allocno_row_words;
1393 IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1397 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1398 static void
1399 mirror_conflicts ()
1401 register int i, j;
1402 int rw = allocno_row_words;
1403 int rwb = rw * INT_BITS;
1404 INT_TYPE *p = conflicts;
1405 INT_TYPE *q0 = conflicts, *q1, *q2;
1406 unsigned INT_TYPE mask;
1408 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1410 if (! mask)
1412 mask = 1;
1413 q0++;
1415 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1417 unsigned INT_TYPE word;
1419 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1420 word >>= 1, q2 += rw)
1422 if (word & 1)
1423 *q2 |= mask;
1429 /* Handle the case where REG is set by the insn being scanned,
1430 during the forward scan to accumulate conflicts.
1431 Store a 1 in regs_live or allocnos_live for this register, record how many
1432 consecutive hardware registers it actually needs,
1433 and record a conflict with all other registers already live.
1435 Note that even if REG does not remain alive after this insn,
1436 we must mark it here as live, to ensure a conflict between
1437 REG and any other regs set in this insn that really do live.
1438 This is because those other regs could be considered after this.
1440 REG might actually be something other than a register;
1441 if so, we do nothing.
1443 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1444 a REG_INC note was found for it). */
1446 static void
1447 mark_reg_store (reg, setter, data)
1448 rtx reg, setter;
1449 void *data ATTRIBUTE_UNUSED;
1451 register int regno;
1453 /* WORD is which word of a multi-register group is being stored.
1454 For the case where the store is actually into a SUBREG of REG.
1455 Except we don't use it; I believe the entire REG needs to be
1456 made live. */
1457 int word = 0;
1459 if (GET_CODE (reg) == SUBREG)
1461 word = SUBREG_WORD (reg);
1462 reg = SUBREG_REG (reg);
1465 if (GET_CODE (reg) != REG)
1466 return;
1468 regs_set[n_regs_set++] = reg;
1470 if (setter && GET_CODE (setter) != CLOBBER)
1471 set_preference (reg, SET_SRC (setter));
1473 regno = REGNO (reg);
1475 /* Either this is one of the max_allocno pseudo regs not allocated,
1476 or it is or has a hardware reg. First handle the pseudo-regs. */
1477 if (regno >= FIRST_PSEUDO_REGISTER)
1479 if (reg_allocno[regno] >= 0)
1481 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1482 record_one_conflict (regno);
1486 if (reg_renumber[regno] >= 0)
1487 regno = reg_renumber[regno] /* + word */;
1489 /* Handle hardware regs (and pseudos allocated to hard regs). */
1490 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1492 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1493 while (regno < last)
1495 record_one_conflict (regno);
1496 SET_HARD_REG_BIT (hard_regs_live, regno);
1497 regno++;
1502 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1504 static void
1505 mark_reg_clobber (reg, setter, data)
1506 rtx reg, setter;
1507 void *data ATTRIBUTE_UNUSED;
1509 if (GET_CODE (setter) == CLOBBER)
1510 mark_reg_store (reg, setter, data);
1513 /* Record that REG has conflicts with all the regs currently live.
1514 Do not mark REG itself as live. */
1516 static void
1517 mark_reg_conflicts (reg)
1518 rtx reg;
1520 register int regno;
1522 if (GET_CODE (reg) == SUBREG)
1523 reg = SUBREG_REG (reg);
1525 if (GET_CODE (reg) != REG)
1526 return;
1528 regno = REGNO (reg);
1530 /* Either this is one of the max_allocno pseudo regs not allocated,
1531 or it is or has a hardware reg. First handle the pseudo-regs. */
1532 if (regno >= FIRST_PSEUDO_REGISTER)
1534 if (reg_allocno[regno] >= 0)
1535 record_one_conflict (regno);
1538 if (reg_renumber[regno] >= 0)
1539 regno = reg_renumber[regno];
1541 /* Handle hardware regs (and pseudos allocated to hard regs). */
1542 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1544 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1545 while (regno < last)
1547 record_one_conflict (regno);
1548 regno++;
1553 /* Mark REG as being dead (following the insn being scanned now).
1554 Store a 0 in regs_live or allocnos_live for this register. */
1556 static void
1557 mark_reg_death (reg)
1558 rtx reg;
1560 register int regno = REGNO (reg);
1562 /* Either this is one of the max_allocno pseudo regs not allocated,
1563 or it is a hardware reg. First handle the pseudo-regs. */
1564 if (regno >= FIRST_PSEUDO_REGISTER)
1566 if (reg_allocno[regno] >= 0)
1567 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1570 /* For pseudo reg, see if it has been assigned a hardware reg. */
1571 if (reg_renumber[regno] >= 0)
1572 regno = reg_renumber[regno];
1574 /* Handle hardware regs (and pseudos allocated to hard regs). */
1575 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1577 /* Pseudo regs already assigned hardware regs are treated
1578 almost the same as explicit hardware regs. */
1579 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1580 while (regno < last)
1582 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1583 regno++;
1588 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1589 for the value stored in it. MODE determines how many consecutive
1590 registers are actually in use. Do not record conflicts;
1591 it is assumed that the caller will do that. */
1593 static void
1594 mark_reg_live_nc (regno, mode)
1595 register int regno;
1596 enum machine_mode mode;
1598 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1599 while (regno < last)
1601 SET_HARD_REG_BIT (hard_regs_live, regno);
1602 regno++;
1606 /* Try to set a preference for an allocno to a hard register.
1607 We are passed DEST and SRC which are the operands of a SET. It is known
1608 that SRC is a register. If SRC or the first operand of SRC is a register,
1609 try to set a preference. If one of the two is a hard register and the other
1610 is a pseudo-register, mark the preference.
1612 Note that we are not as aggressive as local-alloc in trying to tie a
1613 pseudo-register to a hard register. */
1615 static void
1616 set_preference (dest, src)
1617 rtx dest, src;
1619 int src_regno, dest_regno;
1620 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1621 to compensate for subregs in SRC or DEST. */
1622 int offset = 0;
1623 int i;
1624 int copy = 1;
1626 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1627 src = XEXP (src, 0), copy = 0;
1629 /* Get the reg number for both SRC and DEST.
1630 If neither is a reg, give up. */
1632 if (GET_CODE (src) == REG)
1633 src_regno = REGNO (src);
1634 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1636 src_regno = REGNO (SUBREG_REG (src));
1637 offset += SUBREG_WORD (src);
1639 else
1640 return;
1642 if (GET_CODE (dest) == REG)
1643 dest_regno = REGNO (dest);
1644 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1646 dest_regno = REGNO (SUBREG_REG (dest));
1647 offset -= SUBREG_WORD (dest);
1649 else
1650 return;
1652 /* Convert either or both to hard reg numbers. */
1654 if (reg_renumber[src_regno] >= 0)
1655 src_regno = reg_renumber[src_regno];
1657 if (reg_renumber[dest_regno] >= 0)
1658 dest_regno = reg_renumber[dest_regno];
1660 /* Now if one is a hard reg and the other is a global pseudo
1661 then give the other a preference. */
1663 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1664 && reg_allocno[src_regno] >= 0)
1666 dest_regno -= offset;
1667 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1669 if (copy)
1670 SET_REGBIT (hard_reg_copy_preferences,
1671 reg_allocno[src_regno], dest_regno);
1673 SET_REGBIT (hard_reg_preferences,
1674 reg_allocno[src_regno], dest_regno);
1675 for (i = dest_regno;
1676 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1677 i++)
1678 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1682 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1683 && reg_allocno[dest_regno] >= 0)
1685 src_regno += offset;
1686 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1688 if (copy)
1689 SET_REGBIT (hard_reg_copy_preferences,
1690 reg_allocno[dest_regno], src_regno);
1692 SET_REGBIT (hard_reg_preferences,
1693 reg_allocno[dest_regno], src_regno);
1694 for (i = src_regno;
1695 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1696 i++)
1697 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1702 /* Indicate that hard register number FROM was eliminated and replaced with
1703 an offset from hard register number TO. The status of hard registers live
1704 at the start of a basic block is updated by replacing a use of FROM with
1705 a use of TO. */
1707 void
1708 mark_elimination (from, to)
1709 int from, to;
1711 int i;
1713 for (i = 0; i < n_basic_blocks; i++)
1715 register regset r = BASIC_BLOCK (i)->global_live_at_start;
1716 if (REGNO_REG_SET_P (r, from))
1718 CLEAR_REGNO_REG_SET (r, from);
1719 SET_REGNO_REG_SET (r, to);
1724 /* Used for communication between the following functions. Holds the
1725 current life information. */
1726 static regset live_relevant_regs;
1728 /* Record in live_relevant_regs that register REG became live. This
1729 is called via note_stores. */
1730 static void
1731 reg_becomes_live (reg, setter, data)
1732 rtx reg;
1733 rtx setter ATTRIBUTE_UNUSED;
1734 void *data ATTRIBUTE_UNUSED;
1736 int regno;
1738 if (GET_CODE (reg) == SUBREG)
1739 reg = SUBREG_REG (reg);
1741 if (GET_CODE (reg) != REG)
1742 return;
1744 regno = REGNO (reg);
1745 if (regno < FIRST_PSEUDO_REGISTER)
1747 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1748 while (nregs-- > 0)
1749 SET_REGNO_REG_SET (live_relevant_regs, regno++);
1751 else if (reg_renumber[regno] >= 0)
1752 SET_REGNO_REG_SET (live_relevant_regs, regno);
1755 /* Record in live_relevant_regs that register REGNO died. */
1756 static void
1757 reg_dies (regno, mode)
1758 int regno;
1759 enum machine_mode mode;
1761 if (regno < FIRST_PSEUDO_REGISTER)
1763 int nregs = HARD_REGNO_NREGS (regno, mode);
1764 while (nregs-- > 0)
1765 CLEAR_REGNO_REG_SET (live_relevant_regs, regno++);
1767 else
1768 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1771 /* Walk the insns of the current function and build reload_insn_chain,
1772 and record register life information. */
1773 static void
1774 build_insn_chain (first)
1775 rtx first;
1777 struct insn_chain **p = &reload_insn_chain;
1778 struct insn_chain *prev = 0;
1779 int b = 0;
1781 live_relevant_regs = ALLOCA_REG_SET ();
1783 for (; first; first = NEXT_INSN (first))
1785 struct insn_chain *c;
1787 if (first == BLOCK_HEAD (b))
1789 int i;
1791 CLEAR_REG_SET (live_relevant_regs);
1793 EXECUTE_IF_SET_IN_BITMAP
1794 (BASIC_BLOCK (b)->global_live_at_start, 0, i,
1796 if (i < FIRST_PSEUDO_REGISTER
1797 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1798 : reg_renumber[i] >= 0)
1799 SET_REGNO_REG_SET (live_relevant_regs, i);
1803 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1805 c = new_insn_chain ();
1806 c->prev = prev;
1807 prev = c;
1808 *p = c;
1809 p = &c->next;
1810 c->insn = first;
1811 c->block = b;
1813 COPY_REG_SET (c->live_before, live_relevant_regs);
1815 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1817 rtx link;
1819 /* Mark the death of everything that dies in this instruction. */
1821 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1822 if (REG_NOTE_KIND (link) == REG_DEAD
1823 && GET_CODE (XEXP (link, 0)) == REG)
1824 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1826 /* Mark everything born in this instruction as live. */
1828 note_stores (PATTERN (first), reg_becomes_live, NULL);
1831 /* Remember which registers are live at the end of the insn, before
1832 killing those with REG_UNUSED notes. */
1833 COPY_REG_SET (c->live_after, live_relevant_regs);
1835 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1837 rtx link;
1839 /* Mark anything that is set in this insn and then unused as dying. */
1841 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1842 if (REG_NOTE_KIND (link) == REG_UNUSED
1843 && GET_CODE (XEXP (link, 0)) == REG)
1844 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1848 if (first == BLOCK_END (b))
1849 b++;
1851 /* Stop after we pass the end of the last basic block. Verify that
1852 no real insns are after the end of the last basic block.
1854 We may want to reorganize the loop somewhat since this test should
1855 always be the right exit test. */
1856 if (b == n_basic_blocks)
1858 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1859 if (GET_RTX_CLASS (GET_CODE (first)) == 'i'
1860 && GET_CODE (PATTERN (first)) != USE)
1861 abort ();
1862 break;
1865 FREE_REG_SET (live_relevant_regs);
1866 *p = 0;
1869 /* Print debugging trace information if -dg switch is given,
1870 showing the information on which the allocation decisions are based. */
1872 static void
1873 dump_conflicts (file)
1874 FILE *file;
1876 register int i;
1877 register int has_preferences;
1878 register int nregs;
1879 nregs = 0;
1880 for (i = 0; i < max_allocno; i++)
1882 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
1883 continue;
1884 nregs++;
1886 fprintf (file, ";; %d regs to allocate:", nregs);
1887 for (i = 0; i < max_allocno; i++)
1889 int j;
1890 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
1891 continue;
1892 fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1893 for (j = 0; j < max_regno; j++)
1894 if (reg_allocno[j] == allocno_order[i]
1895 && j != allocno_reg[allocno_order[i]])
1896 fprintf (file, "+%d", j);
1897 if (allocno_size[allocno_order[i]] != 1)
1898 fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1900 fprintf (file, "\n");
1902 for (i = 0; i < max_allocno; i++)
1904 register int j;
1905 fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1906 for (j = 0; j < max_allocno; j++)
1907 if (CONFLICTP (j, i))
1908 fprintf (file, " %d", allocno_reg[j]);
1909 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1910 if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1911 fprintf (file, " %d", j);
1912 fprintf (file, "\n");
1914 has_preferences = 0;
1915 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1916 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1917 has_preferences = 1;
1919 if (! has_preferences)
1920 continue;
1921 fprintf (file, ";; %d preferences:", allocno_reg[i]);
1922 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1923 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1924 fprintf (file, " %d", j);
1925 fprintf (file, "\n");
1927 fprintf (file, "\n");
1930 void
1931 dump_global_regs (file)
1932 FILE *file;
1934 register int i, j;
1936 fprintf (file, ";; Register dispositions:\n");
1937 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1938 if (reg_renumber[i] >= 0)
1940 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1941 if (++j % 6 == 0)
1942 fprintf (file, "\n");
1945 fprintf (file, "\n\n;; Hard regs used: ");
1946 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1947 if (regs_ever_live[i])
1948 fprintf (file, " %d", i);
1949 fprintf (file, "\n\n");