* cp-tree.def (FUNCTION_NAME): New tree node.
[official-gcc.git] / gcc / global.c
blob2c95f571e2cd5787e50e0ef090424372a16e2b15
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 struct allocno
88 int reg;
89 /* Gives the number of consecutive hard registers needed by that
90 pseudo reg. */
91 int size;
93 /* Number of calls crossed by each allocno. */
94 int calls_crossed;
96 /* Number of refs (weighted) to each allocno. */
97 int n_refs;
99 /* Guess at live length of each allocno.
100 This is actually the max of the live lengths of the regs. */
101 int live_length;
103 /* Set of hard regs conflicting with allocno N. */
105 HARD_REG_SET hard_reg_conflicts;
107 /* Set of hard regs preferred by allocno N.
108 This is used to make allocnos go into regs that are copied to or from them,
109 when possible, to reduce register shuffling. */
111 HARD_REG_SET hard_reg_preferences;
113 /* Similar, but just counts register preferences made in simple copy
114 operations, rather than arithmetic. These are given priority because
115 we can always eliminate an insn by using these, but using a register
116 in the above list won't always eliminate an insn. */
118 HARD_REG_SET hard_reg_copy_preferences;
120 /* Similar to hard_reg_preferences, but includes bits for subsequent
121 registers when an allocno is multi-word. The above variable is used for
122 allocation while this is used to build reg_someone_prefers, below. */
124 HARD_REG_SET hard_reg_full_preferences;
126 /* Set of hard registers that some later allocno has a preference for. */
128 HARD_REG_SET regs_someone_prefers;
131 static struct allocno *allocno;
133 /* A vector of the integers from 0 to max_allocno-1,
134 sorted in the order of first-to-be-allocated first. */
136 static int *allocno_order;
138 /* Indexed by (pseudo) reg number, gives the number of another
139 lower-numbered pseudo reg which can share a hard reg with this pseudo
140 *even if the two pseudos would otherwise appear to conflict*. */
142 static int *reg_may_share;
144 /* Define the number of bits in each element of `conflicts' and what
145 type that element has. We use the largest integer format on the
146 host machine. */
148 #define INT_BITS HOST_BITS_PER_WIDE_INT
149 #define INT_TYPE HOST_WIDE_INT
151 /* max_allocno by max_allocno array of bits,
152 recording whether two allocno's conflict (can't go in the same
153 hardware register).
155 `conflicts' is symmetric after the call to mirror_conflicts. */
157 static INT_TYPE *conflicts;
159 /* Number of ints require to hold max_allocno bits.
160 This is the length of a row in `conflicts'. */
162 static int allocno_row_words;
164 /* Two macros to test or store 1 in an element of `conflicts'. */
166 #define CONFLICTP(I, J) \
167 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
168 & ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
170 #define SET_CONFLICT(I, J) \
171 (conflicts[(I) * allocno_row_words + (unsigned)(J) / INT_BITS] \
172 |= ((INT_TYPE) 1 << ((unsigned)(J) % INT_BITS)))
174 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
175 and execute CODE. */
176 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
177 do { \
178 int i_; \
179 int allocno_; \
180 INT_TYPE *p_ = (ALLOCNO_SET); \
182 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
183 i_--, allocno_ += INT_BITS) \
185 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
187 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
189 if (word_ & 1) \
190 {CODE;} \
193 } while (0)
195 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
196 #if 0
197 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
198 the conflicting allocno, and execute CODE. This macro assumes that
199 mirror_conflicts has been run. */
200 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
201 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
202 OUT_ALLOCNO, (CODE))
203 #endif
205 /* Set of hard regs currently live (during scan of all insns). */
207 static HARD_REG_SET hard_regs_live;
209 /* Set of registers that global-alloc isn't supposed to use. */
211 static HARD_REG_SET no_global_alloc_regs;
213 /* Set of registers used so far. */
215 static HARD_REG_SET regs_used_so_far;
217 /* Number of refs (weighted) to each hard reg, as used by local alloc.
218 It is zero for a reg that contains global pseudos or is explicitly used. */
220 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
222 /* Guess at live length of each hard reg, as used by local alloc.
223 This is actually the sum of the live lengths of the specific regs. */
225 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
227 /* Test a bit in TABLE, a vector of HARD_REG_SETs,
228 for vector element I, and hard register number J. */
230 #define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (allocno[I].TABLE, J)
232 /* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
234 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
236 /* Bit mask for allocnos live at current point in the scan. */
238 static INT_TYPE *allocnos_live;
240 /* Test, set or clear bit number I in allocnos_live,
241 a bit vector indexed by allocno. */
243 #define ALLOCNO_LIVE_P(I) \
244 (allocnos_live[(unsigned)(I) / INT_BITS] \
245 & ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
247 #define SET_ALLOCNO_LIVE(I) \
248 (allocnos_live[(unsigned)(I) / INT_BITS] \
249 |= ((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
251 #define CLEAR_ALLOCNO_LIVE(I) \
252 (allocnos_live[(unsigned)(I) / INT_BITS] \
253 &= ~((INT_TYPE) 1 << ((unsigned)(I) % INT_BITS)))
255 /* This is turned off because it doesn't work right for DImode.
256 (And it is only used for DImode, so the other cases are worthless.)
257 The problem is that it isn't true that there is NO possibility of conflict;
258 only that there is no conflict if the two pseudos get the exact same regs.
259 If they were allocated with a partial overlap, there would be a conflict.
260 We can't safely turn off the conflict unless we have another way to
261 prevent the partial overlap.
263 Idea: change hard_reg_conflicts so that instead of recording which
264 hard regs the allocno may not overlap, it records where the allocno
265 may not start. Change both where it is used and where it is updated.
266 Then there is a way to record that (reg:DI 108) may start at 10
267 but not at 9 or 11. There is still the question of how to record
268 this semi-conflict between two pseudos. */
269 #if 0
270 /* Reg pairs for which conflict after the current insn
271 is inhibited by a REG_NO_CONFLICT note.
272 If the table gets full, we ignore any other notes--that is conservative. */
273 #define NUM_NO_CONFLICT_PAIRS 4
274 /* Number of pairs in use in this insn. */
275 int n_no_conflict_pairs;
276 static struct { int allocno1, allocno2;}
277 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
278 #endif /* 0 */
280 /* Record all regs that are set in any one insn.
281 Communication from mark_reg_{store,clobber} and global_conflicts. */
283 static rtx *regs_set;
284 static int n_regs_set;
286 /* All registers that can be eliminated. */
288 static HARD_REG_SET eliminable_regset;
290 static int allocno_compare PROTO((const PTR, const PTR));
291 static void global_conflicts PROTO((void));
292 static void mirror_conflicts PROTO((void));
293 static void expand_preferences PROTO((void));
294 static void prune_preferences PROTO((void));
295 static void find_reg PROTO((int, HARD_REG_SET, int, int, int));
296 static void record_one_conflict PROTO((int));
297 static void record_conflicts PROTO((int *, int));
298 static void mark_reg_store PROTO((rtx, rtx, void *));
299 static void mark_reg_clobber PROTO((rtx, rtx, void *));
300 static void mark_reg_conflicts PROTO((rtx));
301 static void mark_reg_death PROTO((rtx));
302 static void mark_reg_live_nc PROTO((int, enum machine_mode));
303 static void set_preference PROTO((rtx, rtx));
304 static void dump_conflicts PROTO((FILE *));
305 static void reg_becomes_live PROTO((rtx, rtx, void *));
306 static void reg_dies PROTO((int, enum machine_mode));
307 static void build_insn_chain PROTO((rtx));
309 /* Perform allocation of pseudo-registers not allocated by local_alloc.
310 FILE is a file to output debugging information on,
311 or zero if such output is not desired.
313 Return value is nonzero if reload failed
314 and we must not do any more for this function. */
317 global_alloc (file)
318 FILE *file;
320 int retval;
321 #ifdef ELIMINABLE_REGS
322 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
323 #endif
324 int need_fp
325 = (! flag_omit_frame_pointer
326 #ifdef EXIT_IGNORE_STACK
327 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
328 #endif
329 || FRAME_POINTER_REQUIRED);
331 register size_t i;
332 rtx x;
334 max_allocno = 0;
336 /* A machine may have certain hard registers that
337 are safe to use only within a basic block. */
339 CLEAR_HARD_REG_SET (no_global_alloc_regs);
340 #ifdef OVERLAPPING_REGNO_P
341 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
342 if (OVERLAPPING_REGNO_P (i))
343 SET_HARD_REG_BIT (no_global_alloc_regs, i);
344 #endif
346 /* Build the regset of all eliminable registers and show we can't use those
347 that we already know won't be eliminated. */
348 #ifdef ELIMINABLE_REGS
349 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
351 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
353 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
354 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
355 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
357 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
358 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
359 if (need_fp)
360 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
361 #endif
363 #else
364 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
365 if (need_fp)
366 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
367 #endif
369 /* Track which registers have already been used. Start with registers
370 explicitly in the rtl, then registers allocated by local register
371 allocation. */
373 CLEAR_HARD_REG_SET (regs_used_so_far);
374 #ifdef LEAF_REGISTERS
375 /* If we are doing the leaf function optimization, and this is a leaf
376 function, it means that the registers that take work to save are those
377 that need a register window. So prefer the ones that can be used in
378 a leaf function. */
380 char *cheap_regs;
381 static char leaf_regs[] = LEAF_REGISTERS;
383 if (only_leaf_regs_used () && leaf_function_p ())
384 cheap_regs = leaf_regs;
385 else
386 cheap_regs = call_used_regs;
387 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
388 if (regs_ever_live[i] || cheap_regs[i])
389 SET_HARD_REG_BIT (regs_used_so_far, i);
391 #else
392 /* We consider registers that do not have to be saved over calls as if
393 they were already used since there is no cost in using them. */
394 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
395 if (regs_ever_live[i] || call_used_regs[i])
396 SET_HARD_REG_BIT (regs_used_so_far, i);
397 #endif
399 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
400 if (reg_renumber[i] >= 0)
401 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
403 /* Establish mappings from register number to allocation number
404 and vice versa. In the process, count the allocnos. */
406 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
408 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
409 reg_allocno[i] = -1;
411 /* Initialize the shared-hard-reg mapping
412 from the list of pairs that may share. */
413 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
414 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
416 int r1 = REGNO (XEXP (x, 0));
417 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
418 if (r1 > r2)
419 reg_may_share[r1] = r2;
420 else
421 reg_may_share[r2] = r1;
424 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
425 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
426 that we are supposed to refrain from putting in a hard reg.
427 -2 means do make an allocno but don't allocate it. */
428 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
429 /* Don't allocate pseudos that cross calls,
430 if this function receives a nonlocal goto. */
431 && (! current_function_has_nonlocal_label
432 || REG_N_CALLS_CROSSED (i) == 0))
434 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
435 reg_allocno[i] = reg_allocno[reg_may_share[i]];
436 else
437 reg_allocno[i] = max_allocno++;
438 if (REG_LIVE_LENGTH (i) == 0)
439 abort ();
441 else
442 reg_allocno[i] = -1;
444 allocno = (struct allocno *) xcalloc (max_allocno, sizeof (struct allocno));
446 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
447 if (reg_allocno[i] >= 0)
449 int num = reg_allocno[i];
450 allocno[num].reg = i;
451 allocno[num].size = PSEUDO_REGNO_SIZE (i);
452 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
453 allocno[num].n_refs += REG_N_REFS (i);
454 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
455 allocno[num].live_length = REG_LIVE_LENGTH (i);
458 /* Calculate amount of usage of each hard reg by pseudos
459 allocated by local-alloc. This is to see if we want to
460 override it. */
461 bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
462 bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
463 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
464 if (reg_renumber[i] >= 0)
466 int regno = reg_renumber[i];
467 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
468 int j;
470 for (j = regno; j < endregno; j++)
472 local_reg_n_refs[j] += REG_N_REFS (i);
473 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
477 /* We can't override local-alloc for a reg used not just by local-alloc. */
478 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
479 if (regs_ever_live[i])
480 local_reg_n_refs[i] = 0;
482 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
484 /* We used to use alloca here, but the size of what it would try to
485 allocate would occasionally cause it to exceed the stack limit and
486 cause unpredictable core dumps. Some examples were > 2Mb in size. */
487 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
488 sizeof (INT_TYPE));
490 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
492 /* If there is work to be done (at least one reg to allocate),
493 perform global conflict analysis and allocate the regs. */
495 if (max_allocno > 0)
497 /* Scan all the insns and compute the conflicts among allocnos
498 and between allocnos and hard regs. */
500 global_conflicts ();
502 mirror_conflicts ();
504 /* Eliminate conflicts between pseudos and eliminable registers. If
505 the register is not eliminated, the pseudo won't really be able to
506 live in the eliminable register, so the conflict doesn't matter.
507 If we do eliminate the register, the conflict will no longer exist.
508 So in either case, we can ignore the conflict. Likewise for
509 preferences. */
511 for (i = 0; i < (size_t) max_allocno; i++)
513 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
514 eliminable_regset);
515 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
516 eliminable_regset);
517 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
518 eliminable_regset);
521 /* Try to expand the preferences by merging them between allocnos. */
523 expand_preferences ();
525 /* Determine the order to allocate the remaining pseudo registers. */
527 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
528 for (i = 0; i < (size_t) max_allocno; i++)
529 allocno_order[i] = i;
531 /* Default the size to 1, since allocno_compare uses it to divide by.
532 Also convert allocno_live_length of zero to -1. A length of zero
533 can occur when all the registers for that allocno have reg_live_length
534 equal to -2. In this case, we want to make an allocno, but not
535 allocate it. So avoid the divide-by-zero and set it to a low
536 priority. */
538 for (i = 0; i < (size_t) max_allocno; i++)
540 if (allocno[i].size == 0)
541 allocno[i].size = 1;
542 if (allocno[i].live_length == 0)
543 allocno[i].live_length = -1;
546 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
548 prune_preferences ();
550 if (file)
551 dump_conflicts (file);
553 /* Try allocating them, one by one, in that order,
554 except for parameters marked with reg_live_length[regno] == -2. */
556 for (i = 0; i < (size_t) max_allocno; i++)
557 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
558 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
560 /* If we have more than one register class,
561 first try allocating in the class that is cheapest
562 for this pseudo-reg. If that fails, try any reg. */
563 if (N_REG_CLASSES > 1)
565 find_reg (allocno_order[i], 0, 0, 0, 0);
566 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
567 continue;
569 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
570 find_reg (allocno_order[i], 0, 1, 0, 0);
573 free (allocno_order);
576 /* Do the reloads now while the allocno data still exist, so that we can
577 try to assign new hard regs to any pseudo regs that are spilled. */
579 #if 0 /* We need to eliminate regs even if there is no rtl code,
580 for the sake of debugging information. */
581 if (n_basic_blocks > 0)
582 #endif
584 build_insn_chain (get_insns ());
585 retval = reload (get_insns (), 1, file);
588 /* Clean up. */
589 free (reg_allocno);
590 free (reg_may_share);
591 free (allocno);
592 free (conflicts);
593 free (allocnos_live);
595 return retval;
598 /* Sort predicate for ordering the allocnos.
599 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
601 static int
602 allocno_compare (v1p, v2p)
603 const PTR v1p;
604 const PTR v2p;
606 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
607 /* Note that the quotient will never be bigger than
608 the value of floor_log2 times the maximum number of
609 times a register can occur in one insn (surely less than 100).
610 Multiplying this by 10000 can't overflow. */
611 register int pri1
612 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].n_refs)
613 / allocno[v1].live_length)
614 * 10000 * allocno[v1].size);
615 register int pri2
616 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].n_refs)
617 / allocno[v2].live_length)
618 * 10000 * allocno[v2].size);
619 if (pri2 - pri1)
620 return pri2 - pri1;
622 /* If regs are equally good, sort by allocno,
623 so that the results of qsort leave nothing to chance. */
624 return v1 - v2;
627 /* Scan the rtl code and record all conflicts and register preferences in the
628 conflict matrices and preference tables. */
630 static void
631 global_conflicts ()
633 register int b, i;
634 register rtx insn;
635 int *block_start_allocnos;
637 /* Make a vector that mark_reg_{store,clobber} will store in. */
638 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
640 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
642 for (b = 0; b < n_basic_blocks; b++)
644 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
646 /* Initialize table of registers currently live
647 to the state at the beginning of this basic block.
648 This also marks the conflicts among hard registers
649 and any allocnos that are live.
651 For pseudo-regs, there is only one bit for each one
652 no matter how many hard regs it occupies.
653 This is ok; we know the size from PSEUDO_REGNO_SIZE.
654 For explicit hard regs, we cannot know the size that way
655 since one hard reg can be used with various sizes.
656 Therefore, we must require that all the hard regs
657 implicitly live as part of a multi-word hard reg
658 are explicitly marked in basic_block_live_at_start. */
661 register regset old = BASIC_BLOCK (b)->global_live_at_start;
662 int ax = 0;
664 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
665 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
667 register int a = reg_allocno[i];
668 if (a >= 0)
670 SET_ALLOCNO_LIVE (a);
671 block_start_allocnos[ax++] = a;
673 else if ((a = reg_renumber[i]) >= 0)
674 mark_reg_live_nc
675 (a, PSEUDO_REGNO_MODE (i));
678 /* Record that each allocno now live conflicts with each hard reg
679 now live.
681 It is not necessary to mark any conflicts between pseudos as
682 this point, even for pseudos which are live at the start of
683 the basic block.
685 Given two pseudos X and Y and any point in the CFG P.
687 On any path to point P where X and Y are live one of the
688 following conditions must be true:
690 1. X is live at some instruction on the path that
691 evaluates Y.
693 2. Y is live at some instruction on the path that
694 evaluates X.
696 3. Either X or Y is not evaluted on the path to P
697 (ie it is used uninitialized) and thus the
698 conflict can be ignored.
700 In cases #1 and #2 the conflict will be recorded when we
701 scan the instruction that makes either X or Y become live. */
702 record_conflicts (block_start_allocnos, ax);
704 #ifdef STACK_REGS
706 /* Pseudos can't go in stack regs at the start of a basic block
707 that is reached by an abnormal edge. */
709 edge e;
710 for (e = BASIC_BLOCK (b)->pred; e ; e = e->pred_next)
711 if (e->flags & EDGE_ABNORMAL)
712 break;
713 if (e != NULL)
714 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
715 record_one_conflict (ax);
717 #endif
720 insn = BLOCK_HEAD (b);
722 /* Scan the code of this basic block, noting which allocnos
723 and hard regs are born or die. When one is born,
724 record a conflict with all others currently live. */
726 while (1)
728 register RTX_CODE code = GET_CODE (insn);
729 register rtx link;
731 /* Make regs_set an empty set. */
733 n_regs_set = 0;
735 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
738 #if 0
739 int i = 0;
740 for (link = REG_NOTES (insn);
741 link && i < NUM_NO_CONFLICT_PAIRS;
742 link = XEXP (link, 1))
743 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
745 no_conflict_pairs[i].allocno1
746 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
747 no_conflict_pairs[i].allocno2
748 = reg_allocno[REGNO (XEXP (link, 0))];
749 i++;
751 #endif /* 0 */
753 /* Mark any registers clobbered by INSN as live,
754 so they conflict with the inputs. */
756 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
758 /* Mark any registers dead after INSN as dead now. */
760 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
761 if (REG_NOTE_KIND (link) == REG_DEAD)
762 mark_reg_death (XEXP (link, 0));
764 /* Mark any registers set in INSN as live,
765 and mark them as conflicting with all other live regs.
766 Clobbers are processed again, so they conflict with
767 the registers that are set. */
769 note_stores (PATTERN (insn), mark_reg_store, NULL);
771 #ifdef AUTO_INC_DEC
772 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
773 if (REG_NOTE_KIND (link) == REG_INC)
774 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
775 #endif
777 /* If INSN has multiple outputs, then any reg that dies here
778 and is used inside of an output
779 must conflict with the other outputs.
781 It is unsafe to use !single_set here since it will ignore an
782 unused output. Just because an output is unused does not mean
783 the compiler can assume the side effect will not occur.
784 Consider if REG appears in the address of an output and we
785 reload the output. If we allocate REG to the same hard
786 register as an unused output we could set the hard register
787 before the output reload insn. */
788 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
789 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
790 if (REG_NOTE_KIND (link) == REG_DEAD)
792 int used_in_output = 0;
793 int i;
794 rtx reg = XEXP (link, 0);
796 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
798 rtx set = XVECEXP (PATTERN (insn), 0, i);
799 if (GET_CODE (set) == SET
800 && GET_CODE (SET_DEST (set)) != REG
801 && !rtx_equal_p (reg, SET_DEST (set))
802 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
803 used_in_output = 1;
805 if (used_in_output)
806 mark_reg_conflicts (reg);
809 /* Mark any registers set in INSN and then never used. */
811 while (n_regs_set > 0)
812 if (find_regno_note (insn, REG_UNUSED,
813 REGNO (regs_set[--n_regs_set])))
814 mark_reg_death (regs_set[n_regs_set]);
817 if (insn == BLOCK_END (b))
818 break;
819 insn = NEXT_INSN (insn);
823 /* Clean up. */
824 free (block_start_allocnos);
825 free (regs_set);
827 /* Expand the preference information by looking for cases where one allocno
828 dies in an insn that sets an allocno. If those two allocnos don't conflict,
829 merge any preferences between those allocnos. */
831 static void
832 expand_preferences ()
834 rtx insn;
835 rtx link;
836 rtx set;
838 /* We only try to handle the most common cases here. Most of the cases
839 where this wins are reg-reg copies. */
841 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
842 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
843 && (set = single_set (insn)) != 0
844 && GET_CODE (SET_DEST (set)) == REG
845 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
846 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
847 if (REG_NOTE_KIND (link) == REG_DEAD
848 && GET_CODE (XEXP (link, 0)) == REG
849 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
850 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
851 reg_allocno[REGNO (XEXP (link, 0))]))
853 int a1 = reg_allocno[REGNO (SET_DEST (set))];
854 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
856 if (XEXP (link, 0) == SET_SRC (set))
858 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
859 allocno[a2].hard_reg_copy_preferences);
860 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
861 allocno[a1].hard_reg_copy_preferences);
864 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
865 allocno[a2].hard_reg_preferences);
866 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
867 allocno[a1].hard_reg_preferences);
868 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
869 allocno[a2].hard_reg_full_preferences);
870 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
871 allocno[a1].hard_reg_full_preferences);
875 /* Prune the preferences for global registers to exclude registers that cannot
876 be used.
878 Compute `regs_someone_prefers', which is a bitmask of the hard registers
879 that are preferred by conflicting registers of lower priority. If possible,
880 we will avoid using these registers. */
882 static void
883 prune_preferences ()
885 int i;
886 int num;
887 int *allocno_to_order = (int *) xmalloc (max_allocno * sizeof (int));
889 /* Scan least most important to most important.
890 For each allocno, remove from preferences registers that cannot be used,
891 either because of conflicts or register type. Then compute all registers
892 preferred by each lower-priority register that conflicts. */
894 for (i = max_allocno - 1; i >= 0; i--)
896 HARD_REG_SET temp;
898 num = allocno_order[i];
899 allocno_to_order[num] = i;
900 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
902 if (allocno[num].calls_crossed == 0)
903 IOR_HARD_REG_SET (temp, fixed_reg_set);
904 else
905 IOR_HARD_REG_SET (temp, call_used_reg_set);
907 IOR_COMPL_HARD_REG_SET
908 (temp,
909 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
911 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
912 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
913 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
916 for (i = max_allocno - 1; i >= 0; i--)
918 /* Merge in the preferences of lower-priority registers (they have
919 already been pruned). If we also prefer some of those registers,
920 don't exclude them unless we are of a smaller size (in which case
921 we want to give the lower-priority allocno the first chance for
922 these registers). */
923 HARD_REG_SET temp, temp2;
924 int allocno2;
926 num = allocno_order[i];
928 CLEAR_HARD_REG_SET (temp);
929 CLEAR_HARD_REG_SET (temp2);
931 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
932 allocno2,
934 if (allocno_to_order[allocno2] > i)
936 if (allocno[allocno2].size <= allocno[num].size)
937 IOR_HARD_REG_SET (temp,
938 allocno[allocno2].hard_reg_full_preferences);
939 else
940 IOR_HARD_REG_SET (temp2,
941 allocno[allocno2].hard_reg_full_preferences);
945 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
946 IOR_HARD_REG_SET (temp, temp2);
947 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
949 free (allocno_to_order);
952 /* Assign a hard register to allocno NUM; look for one that is the beginning
953 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
954 The registers marked in PREFREGS are tried first.
956 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
957 be used for this allocation.
959 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
960 Otherwise ignore that preferred class and use the alternate class.
962 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
963 will have to be saved and restored at calls.
965 RETRYING is nonzero if this is called from retry_global_alloc.
967 If we find one, record it in reg_renumber.
968 If not, do nothing. */
970 static void
971 find_reg (num, losers, alt_regs_p, accept_call_clobbered, retrying)
972 int num;
973 HARD_REG_SET losers;
974 int alt_regs_p;
975 int accept_call_clobbered;
976 int retrying;
978 register int i, best_reg, pass;
979 #ifdef HARD_REG_SET
980 register /* Declare it register if it's a scalar. */
981 #endif
982 HARD_REG_SET used, used1, used2;
984 enum reg_class class = (alt_regs_p
985 ? reg_alternate_class (allocno[num].reg)
986 : reg_preferred_class (allocno[num].reg));
987 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
989 if (accept_call_clobbered)
990 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
991 else if (allocno[num].calls_crossed == 0)
992 COPY_HARD_REG_SET (used1, fixed_reg_set);
993 else
994 COPY_HARD_REG_SET (used1, call_used_reg_set);
996 /* Some registers should not be allocated in global-alloc. */
997 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
998 if (losers)
999 IOR_HARD_REG_SET (used1, losers);
1001 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1002 COPY_HARD_REG_SET (used2, used1);
1004 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1006 #ifdef CLASS_CANNOT_CHANGE_SIZE
1007 if (REG_CHANGES_SIZE (allocno[num].reg))
1008 IOR_HARD_REG_SET (used1,
1009 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
1010 #endif
1012 /* Try each hard reg to see if it fits. Do this in two passes.
1013 In the first pass, skip registers that are preferred by some other pseudo
1014 to give it a better chance of getting one of those registers. Only if
1015 we can't get a register when excluding those do we take one of them.
1016 However, we never allocate a register for the first time in pass 0. */
1018 COPY_HARD_REG_SET (used, used1);
1019 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1020 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1022 best_reg = -1;
1023 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1024 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1025 pass++)
1027 if (pass == 1)
1028 COPY_HARD_REG_SET (used, used1);
1029 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1031 #ifdef REG_ALLOC_ORDER
1032 int regno = reg_alloc_order[i];
1033 #else
1034 int regno = i;
1035 #endif
1036 if (! TEST_HARD_REG_BIT (used, regno)
1037 && HARD_REGNO_MODE_OK (regno, mode)
1038 && (allocno[num].calls_crossed == 0
1039 || accept_call_clobbered
1040 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1042 register int j;
1043 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
1044 for (j = regno + 1;
1045 (j < lim
1046 && ! TEST_HARD_REG_BIT (used, j));
1047 j++);
1048 if (j == lim)
1050 best_reg = regno;
1051 break;
1053 #ifndef REG_ALLOC_ORDER
1054 i = j; /* Skip starting points we know will lose */
1055 #endif
1060 /* See if there is a preferred register with the same class as the register
1061 we allocated above. Making this restriction prevents register
1062 preferencing from creating worse register allocation.
1064 Remove from the preferred registers and conflicting registers. Note that
1065 additional conflicts may have been added after `prune_preferences' was
1066 called.
1068 First do this for those register with copy preferences, then all
1069 preferred registers. */
1071 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1072 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1073 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1075 if (best_reg >= 0)
1077 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1078 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1079 && HARD_REGNO_MODE_OK (i, mode)
1080 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1081 || reg_class_subset_p (REGNO_REG_CLASS (i),
1082 REGNO_REG_CLASS (best_reg))
1083 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1084 REGNO_REG_CLASS (i))))
1086 register int j;
1087 register int lim = i + HARD_REGNO_NREGS (i, mode);
1088 for (j = i + 1;
1089 (j < lim
1090 && ! TEST_HARD_REG_BIT (used, j)
1091 && (REGNO_REG_CLASS (j)
1092 == REGNO_REG_CLASS (best_reg + (j - i))
1093 || reg_class_subset_p (REGNO_REG_CLASS (j),
1094 REGNO_REG_CLASS (best_reg + (j - i)))
1095 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1096 REGNO_REG_CLASS (j))));
1097 j++);
1098 if (j == lim)
1100 best_reg = i;
1101 goto no_prefs;
1105 no_copy_prefs:
1107 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1108 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1109 reg_class_contents[(int) NO_REGS], no_prefs);
1111 if (best_reg >= 0)
1113 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1114 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1115 && HARD_REGNO_MODE_OK (i, mode)
1116 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1117 || reg_class_subset_p (REGNO_REG_CLASS (i),
1118 REGNO_REG_CLASS (best_reg))
1119 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1120 REGNO_REG_CLASS (i))))
1122 register int j;
1123 register int lim = i + HARD_REGNO_NREGS (i, mode);
1124 for (j = i + 1;
1125 (j < lim
1126 && ! TEST_HARD_REG_BIT (used, j)
1127 && (REGNO_REG_CLASS (j)
1128 == REGNO_REG_CLASS (best_reg + (j - i))
1129 || reg_class_subset_p (REGNO_REG_CLASS (j),
1130 REGNO_REG_CLASS (best_reg + (j - i)))
1131 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1132 REGNO_REG_CLASS (j))));
1133 j++);
1134 if (j == lim)
1136 best_reg = i;
1137 break;
1141 no_prefs:
1143 /* If we haven't succeeded yet, try with caller-saves.
1144 We need not check to see if the current function has nonlocal
1145 labels because we don't put any pseudos that are live over calls in
1146 registers in that case. */
1148 if (flag_caller_saves && best_reg < 0)
1150 /* Did not find a register. If it would be profitable to
1151 allocate a call-clobbered register and save and restore it
1152 around calls, do that. */
1153 if (! accept_call_clobbered
1154 && allocno[num].calls_crossed != 0
1155 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1156 allocno[num].calls_crossed))
1158 HARD_REG_SET new_losers;
1159 if (! losers)
1160 CLEAR_HARD_REG_SET (new_losers);
1161 else
1162 COPY_HARD_REG_SET (new_losers, losers);
1164 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1165 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1166 if (reg_renumber[allocno[num].reg] >= 0)
1168 caller_save_needed = 1;
1169 return;
1174 /* If we haven't succeeded yet,
1175 see if some hard reg that conflicts with us
1176 was utilized poorly by local-alloc.
1177 If so, kick out the regs that were put there by local-alloc
1178 so we can use it instead. */
1179 if (best_reg < 0 && !retrying
1180 /* Let's not bother with multi-reg allocnos. */
1181 && allocno[num].size == 1)
1183 /* Count from the end, to find the least-used ones first. */
1184 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1186 #ifdef REG_ALLOC_ORDER
1187 int regno = reg_alloc_order[i];
1188 #else
1189 int regno = i;
1190 #endif
1192 if (local_reg_n_refs[regno] != 0
1193 /* Don't use a reg no good for this pseudo. */
1194 && ! TEST_HARD_REG_BIT (used2, regno)
1195 && HARD_REGNO_MODE_OK (regno, mode)
1196 #ifdef CLASS_CANNOT_CHANGE_SIZE
1197 && ! (REG_CHANGES_SIZE (allocno[num].reg)
1198 && (TEST_HARD_REG_BIT
1199 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1200 regno)))
1201 #endif
1204 /* We explicitly evaluate the divide results into temporary
1205 variables so as to avoid excess precision problems that occur
1206 on a i386-unknown-sysv4.2 (unixware) host. */
1208 double tmp1 = ((double) local_reg_n_refs[regno]
1209 / local_reg_live_length[regno]);
1210 double tmp2 = ((double) allocno[num].n_refs
1211 / allocno[num].live_length);
1213 if (tmp1 < tmp2)
1215 /* Hard reg REGNO was used less in total by local regs
1216 than it would be used by this one allocno! */
1217 int k;
1218 for (k = 0; k < max_regno; k++)
1219 if (reg_renumber[k] >= 0)
1221 int r = reg_renumber[k];
1222 int endregno
1223 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1225 if (regno >= r && regno < endregno)
1226 reg_renumber[k] = -1;
1229 best_reg = regno;
1230 break;
1236 /* Did we find a register? */
1238 if (best_reg >= 0)
1240 register int lim, j;
1241 HARD_REG_SET this_reg;
1243 /* Yes. Record it as the hard register of this pseudo-reg. */
1244 reg_renumber[allocno[num].reg] = best_reg;
1245 /* Also of any pseudo-regs that share with it. */
1246 if (reg_may_share[allocno[num].reg])
1247 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1248 if (reg_allocno[j] == num)
1249 reg_renumber[j] = best_reg;
1251 /* Make a set of the hard regs being allocated. */
1252 CLEAR_HARD_REG_SET (this_reg);
1253 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1254 for (j = best_reg; j < lim; j++)
1256 SET_HARD_REG_BIT (this_reg, j);
1257 SET_HARD_REG_BIT (regs_used_so_far, j);
1258 /* This is no longer a reg used just by local regs. */
1259 local_reg_n_refs[j] = 0;
1261 /* For each other pseudo-reg conflicting with this one,
1262 mark it as conflicting with the hard regs this one occupies. */
1263 lim = num;
1264 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1266 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1271 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1272 Perhaps it had previously seemed not worth a hard reg,
1273 or perhaps its old hard reg has been commandeered for reloads.
1274 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1275 they do not appear to be allocated.
1276 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1278 void
1279 retry_global_alloc (regno, forbidden_regs)
1280 int regno;
1281 HARD_REG_SET forbidden_regs;
1283 int allocno = reg_allocno[regno];
1284 if (allocno >= 0)
1286 /* If we have more than one register class,
1287 first try allocating in the class that is cheapest
1288 for this pseudo-reg. If that fails, try any reg. */
1289 if (N_REG_CLASSES > 1)
1290 find_reg (allocno, forbidden_regs, 0, 0, 1);
1291 if (reg_renumber[regno] < 0
1292 && reg_alternate_class (regno) != NO_REGS)
1293 find_reg (allocno, forbidden_regs, 1, 0, 1);
1295 /* If we found a register, modify the RTL for the register to
1296 show the hard register, and mark that register live. */
1297 if (reg_renumber[regno] >= 0)
1299 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1300 mark_home_live (regno);
1305 /* Record a conflict between register REGNO
1306 and everything currently live.
1307 REGNO must not be a pseudo reg that was allocated
1308 by local_alloc; such numbers must be translated through
1309 reg_renumber before calling here. */
1311 static void
1312 record_one_conflict (regno)
1313 int regno;
1315 register int j;
1317 if (regno < FIRST_PSEUDO_REGISTER)
1318 /* When a hard register becomes live,
1319 record conflicts with live pseudo regs. */
1320 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1322 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1324 else
1325 /* When a pseudo-register becomes live,
1326 record conflicts first with hard regs,
1327 then with other pseudo regs. */
1329 register int ialloc = reg_allocno[regno];
1330 register int ialloc_prod = ialloc * allocno_row_words;
1331 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1332 for (j = allocno_row_words - 1; j >= 0; j--)
1334 #if 0
1335 int k;
1336 for (k = 0; k < n_no_conflict_pairs; k++)
1337 if (! ((j == no_conflict_pairs[k].allocno1
1338 && ialloc == no_conflict_pairs[k].allocno2)
1340 (j == no_conflict_pairs[k].allocno2
1341 && ialloc == no_conflict_pairs[k].allocno1)))
1342 #endif /* 0 */
1343 conflicts[ialloc_prod + j] |= allocnos_live[j];
1348 /* Record all allocnos currently live as conflicting
1349 with all hard regs currently live.
1351 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1352 are currently live. Their bits are also flagged in allocnos_live. */
1354 static void
1355 record_conflicts (allocno_vec, len)
1356 register int *allocno_vec;
1357 register int len;
1359 register int num;
1360 register int j;
1361 register int ialloc_prod;
1363 while (--len >= 0)
1365 num = allocno_vec[len];
1366 ialloc_prod = num * allocno_row_words;
1367 IOR_HARD_REG_SET (allocno[num].hard_reg_conflicts, hard_regs_live);
1371 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1372 static void
1373 mirror_conflicts ()
1375 register int i, j;
1376 int rw = allocno_row_words;
1377 int rwb = rw * INT_BITS;
1378 INT_TYPE *p = conflicts;
1379 INT_TYPE *q0 = conflicts, *q1, *q2;
1380 unsigned INT_TYPE mask;
1382 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1384 if (! mask)
1386 mask = 1;
1387 q0++;
1389 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1391 unsigned INT_TYPE word;
1393 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1394 word >>= 1, q2 += rw)
1396 if (word & 1)
1397 *q2 |= mask;
1403 /* Handle the case where REG is set by the insn being scanned,
1404 during the forward scan to accumulate conflicts.
1405 Store a 1 in regs_live or allocnos_live for this register, record how many
1406 consecutive hardware registers it actually needs,
1407 and record a conflict with all other registers already live.
1409 Note that even if REG does not remain alive after this insn,
1410 we must mark it here as live, to ensure a conflict between
1411 REG and any other regs set in this insn that really do live.
1412 This is because those other regs could be considered after this.
1414 REG might actually be something other than a register;
1415 if so, we do nothing.
1417 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1418 a REG_INC note was found for it). */
1420 static void
1421 mark_reg_store (reg, setter, data)
1422 rtx reg, setter;
1423 void *data ATTRIBUTE_UNUSED;
1425 register int regno;
1427 /* WORD is which word of a multi-register group is being stored.
1428 For the case where the store is actually into a SUBREG of REG.
1429 Except we don't use it; I believe the entire REG needs to be
1430 made live. */
1431 int word = 0;
1433 if (GET_CODE (reg) == SUBREG)
1435 word = SUBREG_WORD (reg);
1436 reg = SUBREG_REG (reg);
1439 if (GET_CODE (reg) != REG)
1440 return;
1442 regs_set[n_regs_set++] = reg;
1444 if (setter && GET_CODE (setter) != CLOBBER)
1445 set_preference (reg, SET_SRC (setter));
1447 regno = REGNO (reg);
1449 /* Either this is one of the max_allocno pseudo regs not allocated,
1450 or it is or has a hardware reg. First handle the pseudo-regs. */
1451 if (regno >= FIRST_PSEUDO_REGISTER)
1453 if (reg_allocno[regno] >= 0)
1455 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1456 record_one_conflict (regno);
1460 if (reg_renumber[regno] >= 0)
1461 regno = reg_renumber[regno] /* + word */;
1463 /* Handle hardware regs (and pseudos allocated to hard regs). */
1464 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1466 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1467 while (regno < last)
1469 record_one_conflict (regno);
1470 SET_HARD_REG_BIT (hard_regs_live, regno);
1471 regno++;
1476 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1478 static void
1479 mark_reg_clobber (reg, setter, data)
1480 rtx reg, setter;
1481 void *data ATTRIBUTE_UNUSED;
1483 if (GET_CODE (setter) == CLOBBER)
1484 mark_reg_store (reg, setter, data);
1487 /* Record that REG has conflicts with all the regs currently live.
1488 Do not mark REG itself as live. */
1490 static void
1491 mark_reg_conflicts (reg)
1492 rtx reg;
1494 register int regno;
1496 if (GET_CODE (reg) == SUBREG)
1497 reg = SUBREG_REG (reg);
1499 if (GET_CODE (reg) != REG)
1500 return;
1502 regno = REGNO (reg);
1504 /* Either this is one of the max_allocno pseudo regs not allocated,
1505 or it is or has a hardware reg. First handle the pseudo-regs. */
1506 if (regno >= FIRST_PSEUDO_REGISTER)
1508 if (reg_allocno[regno] >= 0)
1509 record_one_conflict (regno);
1512 if (reg_renumber[regno] >= 0)
1513 regno = reg_renumber[regno];
1515 /* Handle hardware regs (and pseudos allocated to hard regs). */
1516 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1518 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1519 while (regno < last)
1521 record_one_conflict (regno);
1522 regno++;
1527 /* Mark REG as being dead (following the insn being scanned now).
1528 Store a 0 in regs_live or allocnos_live for this register. */
1530 static void
1531 mark_reg_death (reg)
1532 rtx reg;
1534 register int regno = REGNO (reg);
1536 /* Either this is one of the max_allocno pseudo regs not allocated,
1537 or it is a hardware reg. First handle the pseudo-regs. */
1538 if (regno >= FIRST_PSEUDO_REGISTER)
1540 if (reg_allocno[regno] >= 0)
1541 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1544 /* For pseudo reg, see if it has been assigned a hardware reg. */
1545 if (reg_renumber[regno] >= 0)
1546 regno = reg_renumber[regno];
1548 /* Handle hardware regs (and pseudos allocated to hard regs). */
1549 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1551 /* Pseudo regs already assigned hardware regs are treated
1552 almost the same as explicit hardware regs. */
1553 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1554 while (regno < last)
1556 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1557 regno++;
1562 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1563 for the value stored in it. MODE determines how many consecutive
1564 registers are actually in use. Do not record conflicts;
1565 it is assumed that the caller will do that. */
1567 static void
1568 mark_reg_live_nc (regno, mode)
1569 register int regno;
1570 enum machine_mode mode;
1572 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1573 while (regno < last)
1575 SET_HARD_REG_BIT (hard_regs_live, regno);
1576 regno++;
1580 /* Try to set a preference for an allocno to a hard register.
1581 We are passed DEST and SRC which are the operands of a SET. It is known
1582 that SRC is a register. If SRC or the first operand of SRC is a register,
1583 try to set a preference. If one of the two is a hard register and the other
1584 is a pseudo-register, mark the preference.
1586 Note that we are not as aggressive as local-alloc in trying to tie a
1587 pseudo-register to a hard register. */
1589 static void
1590 set_preference (dest, src)
1591 rtx dest, src;
1593 int src_regno, dest_regno;
1594 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1595 to compensate for subregs in SRC or DEST. */
1596 int offset = 0;
1597 int i;
1598 int copy = 1;
1600 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1601 src = XEXP (src, 0), copy = 0;
1603 /* Get the reg number for both SRC and DEST.
1604 If neither is a reg, give up. */
1606 if (GET_CODE (src) == REG)
1607 src_regno = REGNO (src);
1608 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1610 src_regno = REGNO (SUBREG_REG (src));
1611 offset += SUBREG_WORD (src);
1613 else
1614 return;
1616 if (GET_CODE (dest) == REG)
1617 dest_regno = REGNO (dest);
1618 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1620 dest_regno = REGNO (SUBREG_REG (dest));
1621 offset -= SUBREG_WORD (dest);
1623 else
1624 return;
1626 /* Convert either or both to hard reg numbers. */
1628 if (reg_renumber[src_regno] >= 0)
1629 src_regno = reg_renumber[src_regno];
1631 if (reg_renumber[dest_regno] >= 0)
1632 dest_regno = reg_renumber[dest_regno];
1634 /* Now if one is a hard reg and the other is a global pseudo
1635 then give the other a preference. */
1637 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1638 && reg_allocno[src_regno] >= 0)
1640 dest_regno -= offset;
1641 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1643 if (copy)
1644 SET_REGBIT (hard_reg_copy_preferences,
1645 reg_allocno[src_regno], dest_regno);
1647 SET_REGBIT (hard_reg_preferences,
1648 reg_allocno[src_regno], dest_regno);
1649 for (i = dest_regno;
1650 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1651 i++)
1652 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1656 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1657 && reg_allocno[dest_regno] >= 0)
1659 src_regno += offset;
1660 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1662 if (copy)
1663 SET_REGBIT (hard_reg_copy_preferences,
1664 reg_allocno[dest_regno], src_regno);
1666 SET_REGBIT (hard_reg_preferences,
1667 reg_allocno[dest_regno], src_regno);
1668 for (i = src_regno;
1669 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1670 i++)
1671 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1676 /* Indicate that hard register number FROM was eliminated and replaced with
1677 an offset from hard register number TO. The status of hard registers live
1678 at the start of a basic block is updated by replacing a use of FROM with
1679 a use of TO. */
1681 void
1682 mark_elimination (from, to)
1683 int from, to;
1685 int i;
1687 for (i = 0; i < n_basic_blocks; i++)
1689 register regset r = BASIC_BLOCK (i)->global_live_at_start;
1690 if (REGNO_REG_SET_P (r, from))
1692 CLEAR_REGNO_REG_SET (r, from);
1693 SET_REGNO_REG_SET (r, to);
1698 /* Used for communication between the following functions. Holds the
1699 current life information. */
1700 static regset live_relevant_regs;
1702 /* Record in live_relevant_regs that register REG became live. This
1703 is called via note_stores. */
1704 static void
1705 reg_becomes_live (reg, setter, data)
1706 rtx reg;
1707 rtx setter ATTRIBUTE_UNUSED;
1708 void *data ATTRIBUTE_UNUSED;
1710 int regno;
1712 if (GET_CODE (reg) == SUBREG)
1713 reg = SUBREG_REG (reg);
1715 if (GET_CODE (reg) != REG)
1716 return;
1718 regno = REGNO (reg);
1719 if (regno < FIRST_PSEUDO_REGISTER)
1721 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1722 while (nregs-- > 0)
1723 SET_REGNO_REG_SET (live_relevant_regs, regno++);
1725 else if (reg_renumber[regno] >= 0)
1726 SET_REGNO_REG_SET (live_relevant_regs, regno);
1729 /* Record in live_relevant_regs that register REGNO died. */
1730 static void
1731 reg_dies (regno, mode)
1732 int regno;
1733 enum machine_mode mode;
1735 if (regno < FIRST_PSEUDO_REGISTER)
1737 int nregs = HARD_REGNO_NREGS (regno, mode);
1738 while (nregs-- > 0)
1739 CLEAR_REGNO_REG_SET (live_relevant_regs, regno++);
1741 else
1742 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1745 /* Walk the insns of the current function and build reload_insn_chain,
1746 and record register life information. */
1747 static void
1748 build_insn_chain (first)
1749 rtx first;
1751 struct insn_chain **p = &reload_insn_chain;
1752 struct insn_chain *prev = 0;
1753 int b = 0;
1755 live_relevant_regs = ALLOCA_REG_SET ();
1757 for (; first; first = NEXT_INSN (first))
1759 struct insn_chain *c;
1761 if (first == BLOCK_HEAD (b))
1763 int i;
1765 CLEAR_REG_SET (live_relevant_regs);
1767 EXECUTE_IF_SET_IN_BITMAP
1768 (BASIC_BLOCK (b)->global_live_at_start, 0, i,
1770 if (i < FIRST_PSEUDO_REGISTER
1771 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1772 : reg_renumber[i] >= 0)
1773 SET_REGNO_REG_SET (live_relevant_regs, i);
1777 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1779 c = new_insn_chain ();
1780 c->prev = prev;
1781 prev = c;
1782 *p = c;
1783 p = &c->next;
1784 c->insn = first;
1785 c->block = b;
1787 COPY_REG_SET (c->live_before, live_relevant_regs);
1789 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1791 rtx link;
1793 /* Mark the death of everything that dies in this instruction. */
1795 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1796 if (REG_NOTE_KIND (link) == REG_DEAD
1797 && GET_CODE (XEXP (link, 0)) == REG)
1798 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1800 /* Mark everything born in this instruction as live. */
1802 note_stores (PATTERN (first), reg_becomes_live, NULL);
1805 /* Remember which registers are live at the end of the insn, before
1806 killing those with REG_UNUSED notes. */
1807 COPY_REG_SET (c->live_after, live_relevant_regs);
1809 if (GET_RTX_CLASS (GET_CODE (first)) == 'i')
1811 rtx link;
1813 /* Mark anything that is set in this insn and then unused as dying. */
1815 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1816 if (REG_NOTE_KIND (link) == REG_UNUSED
1817 && GET_CODE (XEXP (link, 0)) == REG)
1818 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)));
1822 if (first == BLOCK_END (b))
1823 b++;
1825 /* Stop after we pass the end of the last basic block. Verify that
1826 no real insns are after the end of the last basic block.
1828 We may want to reorganize the loop somewhat since this test should
1829 always be the right exit test. */
1830 if (b == n_basic_blocks)
1832 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1833 if (GET_RTX_CLASS (GET_CODE (first)) == 'i'
1834 && GET_CODE (PATTERN (first)) != USE)
1835 abort ();
1836 break;
1839 FREE_REG_SET (live_relevant_regs);
1840 *p = 0;
1843 /* Print debugging trace information if -dg switch is given,
1844 showing the information on which the allocation decisions are based. */
1846 static void
1847 dump_conflicts (file)
1848 FILE *file;
1850 register int i;
1851 register int has_preferences;
1852 register int nregs;
1853 nregs = 0;
1854 for (i = 0; i < max_allocno; i++)
1856 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1857 continue;
1858 nregs++;
1860 fprintf (file, ";; %d regs to allocate:", nregs);
1861 for (i = 0; i < max_allocno; i++)
1863 int j;
1864 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1865 continue;
1866 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1867 for (j = 0; j < max_regno; j++)
1868 if (reg_allocno[j] == allocno_order[i]
1869 && j != allocno[allocno_order[i]].reg)
1870 fprintf (file, "+%d", j);
1871 if (allocno[allocno_order[i]].size != 1)
1872 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1874 fprintf (file, "\n");
1876 for (i = 0; i < max_allocno; i++)
1878 register int j;
1879 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1880 for (j = 0; j < max_allocno; j++)
1881 if (CONFLICTP (j, i))
1882 fprintf (file, " %d", allocno[j].reg);
1883 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1884 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1885 fprintf (file, " %d", j);
1886 fprintf (file, "\n");
1888 has_preferences = 0;
1889 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1890 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1891 has_preferences = 1;
1893 if (! has_preferences)
1894 continue;
1895 fprintf (file, ";; %d preferences:", allocno[i].reg);
1896 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1897 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1898 fprintf (file, " %d", j);
1899 fprintf (file, "\n");
1901 fprintf (file, "\n");
1904 void
1905 dump_global_regs (file)
1906 FILE *file;
1908 register int i, j;
1910 fprintf (file, ";; Register dispositions:\n");
1911 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1912 if (reg_renumber[i] >= 0)
1914 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1915 if (++j % 6 == 0)
1916 fprintf (file, "\n");
1919 fprintf (file, "\n\n;; Hard regs used: ");
1920 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1921 if (regs_ever_live[i])
1922 fprintf (file, " %d", i);
1923 fprintf (file, "\n\n");