* configure.in: Add ${libgcj} to noconfigdirs for xtensa-*-* targets.
[official-gcc.git] / gcc / global.c
blob0573eeb285f2a62c18faba80372ac40418890f42
1 /* Allocate registers for pseudo-registers that span basic blocks.
2 Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
3 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
28 #include "machmode.h"
29 #include "hard-reg-set.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "basic-block.h"
34 #include "regs.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "reload.h"
38 #include "output.h"
39 #include "toplev.h"
41 /* This pass of the compiler performs global register allocation.
42 It assigns hard register numbers to all the pseudo registers
43 that were not handled in local_alloc. Assignments are recorded
44 in the vector reg_renumber, not by changing the rtl code.
45 (Such changes are made by final). The entry point is
46 the function global_alloc.
48 After allocation is complete, the reload pass is run as a subroutine
49 of this pass, so that when a pseudo reg loses its hard reg due to
50 spilling it is possible to make a second attempt to find a hard
51 reg for it. The reload pass is independent in other respects
52 and it is run even when stupid register allocation is in use.
54 1. Assign allocation-numbers (allocnos) to the pseudo-registers
55 still needing allocations and to the pseudo-registers currently
56 allocated by local-alloc which may be spilled by reload.
57 Set up tables reg_allocno and allocno_reg to map
58 reg numbers to allocnos and vice versa.
59 max_allocno gets the number of allocnos in use.
61 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
62 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
63 for conflicts between allocnos and explicit hard register use
64 (which includes use of pseudo-registers allocated by local_alloc).
66 3. For each basic block
67 walk forward through the block, recording which
68 pseudo-registers and which hardware registers are live.
69 Build the conflict matrix between the pseudo-registers
70 and another of pseudo-registers versus hardware registers.
71 Also record the preferred hardware registers
72 for each pseudo-register.
74 4. Sort a table of the allocnos into order of
75 desirability of the variables.
77 5. Allocate the variables in that order; each if possible into
78 a preferred register, else into another register. */
80 /* Number of pseudo-registers which are candidates for allocation. */
82 static int max_allocno;
84 /* Indexed by (pseudo) reg number, gives the allocno, or -1
85 for pseudo registers which are not to be allocated. */
87 static int *reg_allocno;
89 struct allocno
91 int reg;
92 /* Gives the number of consecutive hard registers needed by that
93 pseudo reg. */
94 int size;
96 /* Number of calls crossed by each allocno. */
97 int calls_crossed;
99 /* Number of refs to each allocno. */
100 int n_refs;
102 /* Frequency of uses of each allocno. */
103 int freq;
105 /* Guess at live length of each allocno.
106 This is actually the max of the live lengths of the regs. */
107 int live_length;
109 /* Set of hard regs conflicting with allocno N. */
111 HARD_REG_SET hard_reg_conflicts;
113 /* Set of hard regs preferred by allocno N.
114 This is used to make allocnos go into regs that are copied to or from them,
115 when possible, to reduce register shuffling. */
117 HARD_REG_SET hard_reg_preferences;
119 /* Similar, but just counts register preferences made in simple copy
120 operations, rather than arithmetic. These are given priority because
121 we can always eliminate an insn by using these, but using a register
122 in the above list won't always eliminate an insn. */
124 HARD_REG_SET hard_reg_copy_preferences;
126 /* Similar to hard_reg_preferences, but includes bits for subsequent
127 registers when an allocno is multi-word. The above variable is used for
128 allocation while this is used to build reg_someone_prefers, below. */
130 HARD_REG_SET hard_reg_full_preferences;
132 /* Set of hard registers that some later allocno has a preference for. */
134 HARD_REG_SET regs_someone_prefers;
136 #ifdef STACK_REGS
137 /* Set to true if allocno can't be allocated in the stack register. */
138 bool no_stack_reg;
139 #endif
142 static struct allocno *allocno;
144 /* A vector of the integers from 0 to max_allocno-1,
145 sorted in the order of first-to-be-allocated first. */
147 static int *allocno_order;
149 /* Indexed by (pseudo) reg number, gives the number of another
150 lower-numbered pseudo reg which can share a hard reg with this pseudo
151 *even if the two pseudos would otherwise appear to conflict*. */
153 static int *reg_may_share;
155 /* Define the number of bits in each element of `conflicts' and what
156 type that element has. We use the largest integer format on the
157 host machine. */
159 #define INT_BITS HOST_BITS_PER_WIDE_INT
160 #define INT_TYPE HOST_WIDE_INT
162 /* max_allocno by max_allocno array of bits,
163 recording whether two allocno's conflict (can't go in the same
164 hardware register).
166 `conflicts' is symmetric after the call to mirror_conflicts. */
168 static INT_TYPE *conflicts;
170 /* Number of ints require to hold max_allocno bits.
171 This is the length of a row in `conflicts'. */
173 static int allocno_row_words;
175 /* Two macros to test or store 1 in an element of `conflicts'. */
177 #define CONFLICTP(I, J) \
178 (conflicts[(I) * allocno_row_words + (unsigned) (J) / INT_BITS] \
179 & ((INT_TYPE) 1 << ((unsigned) (J) % INT_BITS)))
181 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
182 and execute CODE. */
183 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
184 do { \
185 int i_; \
186 int allocno_; \
187 INT_TYPE *p_ = (ALLOCNO_SET); \
189 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
190 i_--, allocno_ += INT_BITS) \
192 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
194 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
196 if (word_ & 1) \
197 {CODE;} \
200 } while (0)
202 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
203 #if 0
204 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
205 the conflicting allocno, and execute CODE. This macro assumes that
206 mirror_conflicts has been run. */
207 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
208 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
209 OUT_ALLOCNO, (CODE))
210 #endif
212 /* Set of hard regs currently live (during scan of all insns). */
214 static HARD_REG_SET hard_regs_live;
216 /* Set of registers that global-alloc isn't supposed to use. */
218 static HARD_REG_SET no_global_alloc_regs;
220 /* Set of registers used so far. */
222 static HARD_REG_SET regs_used_so_far;
224 /* Number of refs to each hard reg, as used by local alloc.
225 It is zero for a reg that contains global pseudos or is explicitly used. */
227 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
229 /* Frequency of uses of given hard reg. */
230 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
232 /* Guess at live length of each hard reg, as used by local alloc.
233 This is actually the sum of the live lengths of the specific regs. */
235 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
237 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
238 element I, and hard register number J. */
240 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
242 /* Bit mask for allocnos live at current point in the scan. */
244 static INT_TYPE *allocnos_live;
246 /* Test, set or clear bit number I in allocnos_live,
247 a bit vector indexed by allocno. */
249 #define SET_ALLOCNO_LIVE(I) \
250 (allocnos_live[(unsigned) (I) / INT_BITS] \
251 |= ((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
253 #define CLEAR_ALLOCNO_LIVE(I) \
254 (allocnos_live[(unsigned) (I) / INT_BITS] \
255 &= ~((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
257 /* This is turned off because it doesn't work right for DImode.
258 (And it is only used for DImode, so the other cases are worthless.)
259 The problem is that it isn't true that there is NO possibility of conflict;
260 only that there is no conflict if the two pseudos get the exact same regs.
261 If they were allocated with a partial overlap, there would be a conflict.
262 We can't safely turn off the conflict unless we have another way to
263 prevent the partial overlap.
265 Idea: change hard_reg_conflicts so that instead of recording which
266 hard regs the allocno may not overlap, it records where the allocno
267 may not start. Change both where it is used and where it is updated.
268 Then there is a way to record that (reg:DI 108) may start at 10
269 but not at 9 or 11. There is still the question of how to record
270 this semi-conflict between two pseudos. */
271 #if 0
272 /* Reg pairs for which conflict after the current insn
273 is inhibited by a REG_NO_CONFLICT note.
274 If the table gets full, we ignore any other notes--that is conservative. */
275 #define NUM_NO_CONFLICT_PAIRS 4
276 /* Number of pairs in use in this insn. */
277 int n_no_conflict_pairs;
278 static struct { int allocno1, allocno2;}
279 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
280 #endif /* 0 */
282 /* Record all regs that are set in any one insn.
283 Communication from mark_reg_{store,clobber} and global_conflicts. */
285 static rtx *regs_set;
286 static int n_regs_set;
288 /* All registers that can be eliminated. */
290 static HARD_REG_SET eliminable_regset;
292 static int allocno_compare (const void *, const void *);
293 static void global_conflicts (void);
294 static void mirror_conflicts (void);
295 static void expand_preferences (void);
296 static void prune_preferences (void);
297 static void find_reg (int, HARD_REG_SET, int, int, int);
298 static void record_one_conflict (int);
299 static void record_conflicts (int *, int);
300 static void mark_reg_store (rtx, rtx, void *);
301 static void mark_reg_clobber (rtx, rtx, void *);
302 static void mark_reg_conflicts (rtx);
303 static void mark_reg_death (rtx);
304 static void mark_reg_live_nc (int, enum machine_mode);
305 static void set_preference (rtx, rtx);
306 static void dump_conflicts (FILE *);
307 static void reg_becomes_live (rtx, rtx, void *);
308 static void reg_dies (int, enum machine_mode, struct insn_chain *);
310 /* Perform allocation of pseudo-registers not allocated by local_alloc.
311 FILE is a file to output debugging information on,
312 or zero if such output is not desired.
314 Return value is nonzero if reload failed
315 and we must not do any more for this function. */
318 global_alloc (FILE *file)
320 int retval;
321 #ifdef ELIMINABLE_REGS
322 static const struct {const 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 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);
341 /* Build the regset of all eliminable registers and show we can't use those
342 that we already know won't be eliminated. */
343 #ifdef ELIMINABLE_REGS
344 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
346 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
348 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
349 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
350 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
352 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
353 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
354 if (need_fp)
355 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
356 #endif
358 #else
359 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
360 if (need_fp)
361 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
362 #endif
364 /* Track which registers have already been used. Start with registers
365 explicitly in the rtl, then registers allocated by local register
366 allocation. */
368 CLEAR_HARD_REG_SET (regs_used_so_far);
369 #ifdef LEAF_REGISTERS
370 /* If we are doing the leaf function optimization, and this is a leaf
371 function, it means that the registers that take work to save are those
372 that need a register window. So prefer the ones that can be used in
373 a leaf function. */
375 const char *cheap_regs;
376 const char *const leaf_regs = LEAF_REGISTERS;
378 if (only_leaf_regs_used () && leaf_function_p ())
379 cheap_regs = leaf_regs;
380 else
381 cheap_regs = call_used_regs;
382 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
383 if (regs_ever_live[i] || cheap_regs[i])
384 SET_HARD_REG_BIT (regs_used_so_far, i);
386 #else
387 /* We consider registers that do not have to be saved over calls as if
388 they were already used since there is no cost in using them. */
389 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
390 if (regs_ever_live[i] || call_used_regs[i])
391 SET_HARD_REG_BIT (regs_used_so_far, i);
392 #endif
394 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
395 if (reg_renumber[i] >= 0)
396 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
398 /* Establish mappings from register number to allocation number
399 and vice versa. In the process, count the allocnos. */
401 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
403 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
404 reg_allocno[i] = -1;
406 /* Initialize the shared-hard-reg mapping
407 from the list of pairs that may share. */
408 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
409 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
411 int r1 = REGNO (XEXP (x, 0));
412 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
413 if (r1 > r2)
414 reg_may_share[r1] = r2;
415 else
416 reg_may_share[r2] = r1;
419 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
420 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
421 that we are supposed to refrain from putting in a hard reg.
422 -2 means do make an allocno but don't allocate it. */
423 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
424 /* Don't allocate pseudos that cross calls,
425 if this function receives a nonlocal goto. */
426 && (! current_function_has_nonlocal_label
427 || REG_N_CALLS_CROSSED (i) == 0))
429 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
430 reg_allocno[i] = reg_allocno[reg_may_share[i]];
431 else
432 reg_allocno[i] = max_allocno++;
433 if (REG_LIVE_LENGTH (i) == 0)
434 abort ();
436 else
437 reg_allocno[i] = -1;
439 allocno = (struct allocno *) xcalloc (max_allocno, sizeof (struct allocno));
441 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
442 if (reg_allocno[i] >= 0)
444 int num = reg_allocno[i];
445 allocno[num].reg = i;
446 allocno[num].size = PSEUDO_REGNO_SIZE (i);
447 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
448 allocno[num].n_refs += REG_N_REFS (i);
449 allocno[num].freq += REG_FREQ (i);
450 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
451 allocno[num].live_length = REG_LIVE_LENGTH (i);
454 /* Calculate amount of usage of each hard reg by pseudos
455 allocated by local-alloc. This is to see if we want to
456 override it. */
457 memset ((char *) local_reg_live_length, 0, sizeof local_reg_live_length);
458 memset ((char *) local_reg_n_refs, 0, sizeof local_reg_n_refs);
459 memset ((char *) local_reg_freq, 0, sizeof local_reg_freq);
460 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
461 if (reg_renumber[i] >= 0)
463 int regno = reg_renumber[i];
464 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
465 int j;
467 for (j = regno; j < endregno; j++)
469 local_reg_n_refs[j] += REG_N_REFS (i);
470 local_reg_freq[j] += REG_FREQ (i);
471 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
475 /* We can't override local-alloc for a reg used not just by local-alloc. */
476 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477 if (regs_ever_live[i])
478 local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
480 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
482 /* We used to use alloca here, but the size of what it would try to
483 allocate would occasionally cause it to exceed the stack limit and
484 cause unpredictable core dumps. Some examples were > 2Mb in size. */
485 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
486 sizeof (INT_TYPE));
488 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
490 /* If there is work to be done (at least one reg to allocate),
491 perform global conflict analysis and allocate the regs. */
493 if (max_allocno > 0)
495 /* Scan all the insns and compute the conflicts among allocnos
496 and between allocnos and hard regs. */
498 global_conflicts ();
500 mirror_conflicts ();
502 /* Eliminate conflicts between pseudos and eliminable registers. If
503 the register is not eliminated, the pseudo won't really be able to
504 live in the eliminable register, so the conflict doesn't matter.
505 If we do eliminate the register, the conflict will no longer exist.
506 So in either case, we can ignore the conflict. Likewise for
507 preferences. */
509 for (i = 0; i < (size_t) max_allocno; i++)
511 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
512 eliminable_regset);
513 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
514 eliminable_regset);
515 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
516 eliminable_regset);
519 /* Try to expand the preferences by merging them between allocnos. */
521 expand_preferences ();
523 /* Determine the order to allocate the remaining pseudo registers. */
525 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
526 for (i = 0; i < (size_t) max_allocno; i++)
527 allocno_order[i] = i;
529 /* Default the size to 1, since allocno_compare uses it to divide by.
530 Also convert allocno_live_length of zero to -1. A length of zero
531 can occur when all the registers for that allocno have reg_live_length
532 equal to -2. In this case, we want to make an allocno, but not
533 allocate it. So avoid the divide-by-zero and set it to a low
534 priority. */
536 for (i = 0; i < (size_t) max_allocno; i++)
538 if (allocno[i].size == 0)
539 allocno[i].size = 1;
540 if (allocno[i].live_length == 0)
541 allocno[i].live_length = -1;
544 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
546 prune_preferences ();
548 if (file)
549 dump_conflicts (file);
551 /* Try allocating them, one by one, in that order,
552 except for parameters marked with reg_live_length[regno] == -2. */
554 for (i = 0; i < (size_t) max_allocno; i++)
555 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
556 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
558 /* If we have more than one register class,
559 first try allocating in the class that is cheapest
560 for this pseudo-reg. If that fails, try any reg. */
561 if (N_REG_CLASSES > 1)
563 find_reg (allocno_order[i], 0, 0, 0, 0);
564 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
565 continue;
567 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
568 find_reg (allocno_order[i], 0, 1, 0, 0);
571 free (allocno_order);
574 /* Do the reloads now while the allocno data still exist, so that we can
575 try to assign new hard regs to any pseudo regs that are spilled. */
577 #if 0 /* We need to eliminate regs even if there is no rtl code,
578 for the sake of debugging information. */
579 if (n_basic_blocks > 0)
580 #endif
582 build_insn_chain (get_insns ());
583 retval = reload (get_insns (), 1);
586 /* Clean up. */
587 free (reg_allocno);
588 free (reg_may_share);
589 free (allocno);
590 free (conflicts);
591 free (allocnos_live);
593 return retval;
596 /* Sort predicate for ordering the allocnos.
597 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
599 static int
600 allocno_compare (const void *v1p, const void *v2p)
602 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
603 /* Note that the quotient will never be bigger than
604 the value of floor_log2 times the maximum number of
605 times a register can occur in one insn (surely less than 100)
606 weighted by the frequency (maximally REG_FREQ_MAX).
607 Multiplying this by 10000/REG_FREQ_MAX can't overflow. */
608 int pri1
609 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
610 / allocno[v1].live_length)
611 * (10000 / REG_FREQ_MAX) * allocno[v1].size);
612 int pri2
613 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
614 / allocno[v2].live_length)
615 * (10000 / REG_FREQ_MAX) * allocno[v2].size);
616 if (pri2 - pri1)
617 return pri2 - pri1;
619 /* If regs are equally good, sort by allocno,
620 so that the results of qsort leave nothing to chance. */
621 return v1 - v2;
624 /* Scan the rtl code and record all conflicts and register preferences in the
625 conflict matrices and preference tables. */
627 static void
628 global_conflicts (void)
630 int i;
631 basic_block b;
632 rtx insn;
633 int *block_start_allocnos;
635 /* Make a vector that mark_reg_{store,clobber} will store in. */
636 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
638 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
640 FOR_EACH_BB (b)
642 memset ((char *) allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
644 /* Initialize table of registers currently live
645 to the state at the beginning of this basic block.
646 This also marks the conflicts among hard registers
647 and any allocnos that are live.
649 For pseudo-regs, there is only one bit for each one
650 no matter how many hard regs it occupies.
651 This is ok; we know the size from PSEUDO_REGNO_SIZE.
652 For explicit hard regs, we cannot know the size that way
653 since one hard reg can be used with various sizes.
654 Therefore, we must require that all the hard regs
655 implicitly live as part of a multi-word hard reg
656 are explicitly marked in basic_block_live_at_start. */
659 regset old = b->global_live_at_start;
660 int ax = 0;
662 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
663 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
665 int a = reg_allocno[i];
666 if (a >= 0)
668 SET_ALLOCNO_LIVE (a);
669 block_start_allocnos[ax++] = a;
671 else if ((a = reg_renumber[i]) >= 0)
672 mark_reg_live_nc
673 (a, PSEUDO_REGNO_MODE (i));
676 /* Record that each allocno now live conflicts with each hard reg
677 now live.
679 It is not necessary to mark any conflicts between pseudos as
680 this point, even for pseudos which are live at the start of
681 the basic block.
683 Given two pseudos X and Y and any point in the CFG P.
685 On any path to point P where X and Y are live one of the
686 following conditions must be true:
688 1. X is live at some instruction on the path that
689 evaluates Y.
691 2. Y is live at some instruction on the path that
692 evaluates X.
694 3. Either X or Y is not evaluated on the path to P
695 (ie it is used uninitialized) and thus the
696 conflict can be ignored.
698 In cases #1 and #2 the conflict will be recorded when we
699 scan the instruction that makes either X or Y become live. */
700 record_conflicts (block_start_allocnos, ax);
702 /* Pseudos can't go in stack regs at the start of a basic block that
703 is reached by an abnormal edge. Likewise for call clobbered regs,
704 because because caller-save, fixup_abnormal_edges, and possibly
705 the table driven EH machinery are not quite ready to handle such
706 regs live across such edges. */
708 edge e;
710 for (e = b->pred; e ; e = e->pred_next)
711 if (e->flags & EDGE_ABNORMAL)
712 break;
714 if (e != NULL)
716 #ifdef STACK_REGS
717 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, ax,
719 allocno[ax].no_stack_reg = 1;
721 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
722 record_one_conflict (ax);
723 #endif
725 /* No need to record conflicts for call clobbered regs if we have
726 nonlocal labels around, as we don't ever try to allocate such
727 regs in this case. */
728 if (! current_function_has_nonlocal_label)
729 for (ax = 0; ax < FIRST_PSEUDO_REGISTER; ax++)
730 if (call_used_regs [ax])
731 record_one_conflict (ax);
736 insn = b->head;
738 /* Scan the code of this basic block, noting which allocnos
739 and hard regs are born or die. When one is born,
740 record a conflict with all others currently live. */
742 while (1)
744 RTX_CODE code = GET_CODE (insn);
745 rtx link;
747 /* Make regs_set an empty set. */
749 n_regs_set = 0;
751 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
754 #if 0
755 int i = 0;
756 for (link = REG_NOTES (insn);
757 link && i < NUM_NO_CONFLICT_PAIRS;
758 link = XEXP (link, 1))
759 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
761 no_conflict_pairs[i].allocno1
762 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
763 no_conflict_pairs[i].allocno2
764 = reg_allocno[REGNO (XEXP (link, 0))];
765 i++;
767 #endif /* 0 */
769 /* Mark any registers clobbered by INSN as live,
770 so they conflict with the inputs. */
772 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
774 /* Mark any registers dead after INSN as dead now. */
776 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
777 if (REG_NOTE_KIND (link) == REG_DEAD)
778 mark_reg_death (XEXP (link, 0));
780 /* Mark any registers set in INSN as live,
781 and mark them as conflicting with all other live regs.
782 Clobbers are processed again, so they conflict with
783 the registers that are set. */
785 note_stores (PATTERN (insn), mark_reg_store, NULL);
787 #ifdef AUTO_INC_DEC
788 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
789 if (REG_NOTE_KIND (link) == REG_INC)
790 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
791 #endif
793 /* If INSN has multiple outputs, then any reg that dies here
794 and is used inside of an output
795 must conflict with the other outputs.
797 It is unsafe to use !single_set here since it will ignore an
798 unused output. Just because an output is unused does not mean
799 the compiler can assume the side effect will not occur.
800 Consider if REG appears in the address of an output and we
801 reload the output. If we allocate REG to the same hard
802 register as an unused output we could set the hard register
803 before the output reload insn. */
804 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
805 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
806 if (REG_NOTE_KIND (link) == REG_DEAD)
808 int used_in_output = 0;
809 int i;
810 rtx reg = XEXP (link, 0);
812 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
814 rtx set = XVECEXP (PATTERN (insn), 0, i);
815 if (GET_CODE (set) == SET
816 && GET_CODE (SET_DEST (set)) != REG
817 && !rtx_equal_p (reg, SET_DEST (set))
818 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
819 used_in_output = 1;
821 if (used_in_output)
822 mark_reg_conflicts (reg);
825 /* Mark any registers set in INSN and then never used. */
827 while (n_regs_set-- > 0)
829 rtx note = find_regno_note (insn, REG_UNUSED,
830 REGNO (regs_set[n_regs_set]));
831 if (note)
832 mark_reg_death (XEXP (note, 0));
836 if (insn == b->end)
837 break;
838 insn = NEXT_INSN (insn);
842 /* Clean up. */
843 free (block_start_allocnos);
844 free (regs_set);
846 /* Expand the preference information by looking for cases where one allocno
847 dies in an insn that sets an allocno. If those two allocnos don't conflict,
848 merge any preferences between those allocnos. */
850 static void
851 expand_preferences (void)
853 rtx insn;
854 rtx link;
855 rtx set;
857 /* We only try to handle the most common cases here. Most of the cases
858 where this wins are reg-reg copies. */
860 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
861 if (INSN_P (insn)
862 && (set = single_set (insn)) != 0
863 && GET_CODE (SET_DEST (set)) == REG
864 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
865 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
866 if (REG_NOTE_KIND (link) == REG_DEAD
867 && GET_CODE (XEXP (link, 0)) == REG
868 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
869 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
870 reg_allocno[REGNO (XEXP (link, 0))]))
872 int a1 = reg_allocno[REGNO (SET_DEST (set))];
873 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
875 if (XEXP (link, 0) == SET_SRC (set))
877 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
878 allocno[a2].hard_reg_copy_preferences);
879 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
880 allocno[a1].hard_reg_copy_preferences);
883 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
884 allocno[a2].hard_reg_preferences);
885 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
886 allocno[a1].hard_reg_preferences);
887 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
888 allocno[a2].hard_reg_full_preferences);
889 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
890 allocno[a1].hard_reg_full_preferences);
894 /* Prune the preferences for global registers to exclude registers that cannot
895 be used.
897 Compute `regs_someone_prefers', which is a bitmask of the hard registers
898 that are preferred by conflicting registers of lower priority. If possible,
899 we will avoid using these registers. */
901 static void
902 prune_preferences (void)
904 int i;
905 int num;
906 int *allocno_to_order = (int *) xmalloc (max_allocno * sizeof (int));
908 /* Scan least most important to most important.
909 For each allocno, remove from preferences registers that cannot be used,
910 either because of conflicts or register type. Then compute all registers
911 preferred by each lower-priority register that conflicts. */
913 for (i = max_allocno - 1; i >= 0; i--)
915 HARD_REG_SET temp;
917 num = allocno_order[i];
918 allocno_to_order[num] = i;
919 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
921 if (allocno[num].calls_crossed == 0)
922 IOR_HARD_REG_SET (temp, fixed_reg_set);
923 else
924 IOR_HARD_REG_SET (temp, call_used_reg_set);
926 IOR_COMPL_HARD_REG_SET
927 (temp,
928 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
930 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
931 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
932 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
935 for (i = max_allocno - 1; i >= 0; i--)
937 /* Merge in the preferences of lower-priority registers (they have
938 already been pruned). If we also prefer some of those registers,
939 don't exclude them unless we are of a smaller size (in which case
940 we want to give the lower-priority allocno the first chance for
941 these registers). */
942 HARD_REG_SET temp, temp2;
943 int allocno2;
945 num = allocno_order[i];
947 CLEAR_HARD_REG_SET (temp);
948 CLEAR_HARD_REG_SET (temp2);
950 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
951 allocno2,
953 if (allocno_to_order[allocno2] > i)
955 if (allocno[allocno2].size <= allocno[num].size)
956 IOR_HARD_REG_SET (temp,
957 allocno[allocno2].hard_reg_full_preferences);
958 else
959 IOR_HARD_REG_SET (temp2,
960 allocno[allocno2].hard_reg_full_preferences);
964 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
965 IOR_HARD_REG_SET (temp, temp2);
966 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
968 free (allocno_to_order);
971 /* Assign a hard register to allocno NUM; look for one that is the beginning
972 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
973 The registers marked in PREFREGS are tried first.
975 LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
976 be used for this allocation.
978 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
979 Otherwise ignore that preferred class and use the alternate class.
981 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
982 will have to be saved and restored at calls.
984 RETRYING is nonzero if this is called from retry_global_alloc.
986 If we find one, record it in reg_renumber.
987 If not, do nothing. */
989 static void
990 find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
992 int i, best_reg, pass;
993 HARD_REG_SET used, used1, used2;
995 enum reg_class class = (alt_regs_p
996 ? reg_alternate_class (allocno[num].reg)
997 : reg_preferred_class (allocno[num].reg));
998 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
1000 if (accept_call_clobbered)
1001 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
1002 else if (allocno[num].calls_crossed == 0)
1003 COPY_HARD_REG_SET (used1, fixed_reg_set);
1004 else
1005 COPY_HARD_REG_SET (used1, call_used_reg_set);
1007 /* Some registers should not be allocated in global-alloc. */
1008 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1009 if (losers)
1010 IOR_HARD_REG_SET (used1, losers);
1012 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1013 COPY_HARD_REG_SET (used2, used1);
1015 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1017 #ifdef CANNOT_CHANGE_MODE_CLASS
1018 cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1019 #endif
1021 /* Try each hard reg to see if it fits. Do this in two passes.
1022 In the first pass, skip registers that are preferred by some other pseudo
1023 to give it a better chance of getting one of those registers. Only if
1024 we can't get a register when excluding those do we take one of them.
1025 However, we never allocate a register for the first time in pass 0. */
1027 COPY_HARD_REG_SET (used, used1);
1028 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1029 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1031 best_reg = -1;
1032 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1033 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1034 pass++)
1036 if (pass == 1)
1037 COPY_HARD_REG_SET (used, used1);
1038 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1040 #ifdef REG_ALLOC_ORDER
1041 int regno = reg_alloc_order[i];
1042 #else
1043 int regno = i;
1044 #endif
1045 if (! TEST_HARD_REG_BIT (used, regno)
1046 && HARD_REGNO_MODE_OK (regno, mode)
1047 && (allocno[num].calls_crossed == 0
1048 || accept_call_clobbered
1049 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1051 int j;
1052 int lim = regno + HARD_REGNO_NREGS (regno, mode);
1053 for (j = regno + 1;
1054 (j < lim
1055 && ! TEST_HARD_REG_BIT (used, j));
1056 j++);
1057 if (j == lim)
1059 best_reg = regno;
1060 break;
1062 #ifndef REG_ALLOC_ORDER
1063 i = j; /* Skip starting points we know will lose */
1064 #endif
1069 /* See if there is a preferred register with the same class as the register
1070 we allocated above. Making this restriction prevents register
1071 preferencing from creating worse register allocation.
1073 Remove from the preferred registers and conflicting registers. Note that
1074 additional conflicts may have been added after `prune_preferences' was
1075 called.
1077 First do this for those register with copy preferences, then all
1078 preferred registers. */
1080 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1081 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1082 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1084 if (best_reg >= 0)
1086 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1087 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1088 && HARD_REGNO_MODE_OK (i, mode)
1089 && (allocno[num].calls_crossed == 0
1090 || accept_call_clobbered
1091 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1092 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1093 || reg_class_subset_p (REGNO_REG_CLASS (i),
1094 REGNO_REG_CLASS (best_reg))
1095 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1096 REGNO_REG_CLASS (i))))
1098 int j;
1099 int lim = i + HARD_REGNO_NREGS (i, mode);
1100 for (j = i + 1;
1101 (j < lim
1102 && ! TEST_HARD_REG_BIT (used, j)
1103 && (REGNO_REG_CLASS (j)
1104 == REGNO_REG_CLASS (best_reg + (j - i))
1105 || reg_class_subset_p (REGNO_REG_CLASS (j),
1106 REGNO_REG_CLASS (best_reg + (j - i)))
1107 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1108 REGNO_REG_CLASS (j))));
1109 j++);
1110 if (j == lim)
1112 best_reg = i;
1113 goto no_prefs;
1117 no_copy_prefs:
1119 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1120 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1121 reg_class_contents[(int) NO_REGS], no_prefs);
1123 if (best_reg >= 0)
1125 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1126 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1127 && HARD_REGNO_MODE_OK (i, mode)
1128 && (allocno[num].calls_crossed == 0
1129 || accept_call_clobbered
1130 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1131 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1132 || reg_class_subset_p (REGNO_REG_CLASS (i),
1133 REGNO_REG_CLASS (best_reg))
1134 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1135 REGNO_REG_CLASS (i))))
1137 int j;
1138 int lim = i + HARD_REGNO_NREGS (i, mode);
1139 for (j = i + 1;
1140 (j < lim
1141 && ! TEST_HARD_REG_BIT (used, j)
1142 && (REGNO_REG_CLASS (j)
1143 == REGNO_REG_CLASS (best_reg + (j - i))
1144 || reg_class_subset_p (REGNO_REG_CLASS (j),
1145 REGNO_REG_CLASS (best_reg + (j - i)))
1146 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1147 REGNO_REG_CLASS (j))));
1148 j++);
1149 if (j == lim)
1151 best_reg = i;
1152 break;
1156 no_prefs:
1158 /* If we haven't succeeded yet, try with caller-saves.
1159 We need not check to see if the current function has nonlocal
1160 labels because we don't put any pseudos that are live over calls in
1161 registers in that case. */
1163 if (flag_caller_saves && best_reg < 0)
1165 /* Did not find a register. If it would be profitable to
1166 allocate a call-clobbered register and save and restore it
1167 around calls, do that. */
1168 if (! accept_call_clobbered
1169 && allocno[num].calls_crossed != 0
1170 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1171 allocno[num].calls_crossed))
1173 HARD_REG_SET new_losers;
1174 if (! losers)
1175 CLEAR_HARD_REG_SET (new_losers);
1176 else
1177 COPY_HARD_REG_SET (new_losers, losers);
1179 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1180 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1181 if (reg_renumber[allocno[num].reg] >= 0)
1183 caller_save_needed = 1;
1184 return;
1189 /* If we haven't succeeded yet,
1190 see if some hard reg that conflicts with us
1191 was utilized poorly by local-alloc.
1192 If so, kick out the regs that were put there by local-alloc
1193 so we can use it instead. */
1194 if (best_reg < 0 && !retrying
1195 /* Let's not bother with multi-reg allocnos. */
1196 && allocno[num].size == 1)
1198 /* Count from the end, to find the least-used ones first. */
1199 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1201 #ifdef REG_ALLOC_ORDER
1202 int regno = reg_alloc_order[i];
1203 #else
1204 int regno = i;
1205 #endif
1207 if (local_reg_n_refs[regno] != 0
1208 /* Don't use a reg no good for this pseudo. */
1209 && ! TEST_HARD_REG_BIT (used2, regno)
1210 && HARD_REGNO_MODE_OK (regno, mode)
1211 /* The code below assumes that we need only a single
1212 register, but the check of allocno[num].size above
1213 was not enough. Sometimes we need more than one
1214 register for a single-word value. */
1215 && HARD_REGNO_NREGS (regno, mode) == 1
1216 && (allocno[num].calls_crossed == 0
1217 || accept_call_clobbered
1218 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1219 #ifdef CANNOT_CHANGE_MODE_CLASS
1220 && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1221 mode)
1222 #endif
1223 #ifdef STACK_REGS
1224 && (!allocno[num].no_stack_reg
1225 || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
1226 #endif
1229 /* We explicitly evaluate the divide results into temporary
1230 variables so as to avoid excess precision problems that occur
1231 on an i386-unknown-sysv4.2 (unixware) host. */
1233 double tmp1 = ((double) local_reg_freq[regno]
1234 / local_reg_live_length[regno]);
1235 double tmp2 = ((double) allocno[num].freq
1236 / allocno[num].live_length);
1238 if (tmp1 < tmp2)
1240 /* Hard reg REGNO was used less in total by local regs
1241 than it would be used by this one allocno! */
1242 int k;
1243 for (k = 0; k < max_regno; k++)
1244 if (reg_renumber[k] >= 0)
1246 int r = reg_renumber[k];
1247 int endregno
1248 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1250 if (regno >= r && regno < endregno)
1251 reg_renumber[k] = -1;
1254 best_reg = regno;
1255 break;
1261 /* Did we find a register? */
1263 if (best_reg >= 0)
1265 int lim, j;
1266 HARD_REG_SET this_reg;
1268 /* Yes. Record it as the hard register of this pseudo-reg. */
1269 reg_renumber[allocno[num].reg] = best_reg;
1270 /* Also of any pseudo-regs that share with it. */
1271 if (reg_may_share[allocno[num].reg])
1272 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1273 if (reg_allocno[j] == num)
1274 reg_renumber[j] = best_reg;
1276 /* Make a set of the hard regs being allocated. */
1277 CLEAR_HARD_REG_SET (this_reg);
1278 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1279 for (j = best_reg; j < lim; j++)
1281 SET_HARD_REG_BIT (this_reg, j);
1282 SET_HARD_REG_BIT (regs_used_so_far, j);
1283 /* This is no longer a reg used just by local regs. */
1284 local_reg_n_refs[j] = 0;
1285 local_reg_freq[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 = num;
1290 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1292 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, 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 (int regno, HARD_REG_SET forbidden_regs)
1307 int alloc_no = reg_allocno[regno];
1308 if (alloc_no >= 0)
1310 /* If we have more than one register class,
1311 first try allocating in the class that is cheapest
1312 for this pseudo-reg. If that fails, try any reg. */
1313 if (N_REG_CLASSES > 1)
1314 find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1315 if (reg_renumber[regno] < 0
1316 && reg_alternate_class (regno) != NO_REGS)
1317 find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1319 /* If we found a register, modify the RTL for the register to
1320 show the hard register, and mark that register live. */
1321 if (reg_renumber[regno] >= 0)
1323 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1324 mark_home_live (regno);
1329 /* Record a conflict between register REGNO
1330 and everything currently live.
1331 REGNO must not be a pseudo reg that was allocated
1332 by local_alloc; such numbers must be translated through
1333 reg_renumber before calling here. */
1335 static void
1336 record_one_conflict (int regno)
1338 int j;
1340 if (regno < FIRST_PSEUDO_REGISTER)
1341 /* When a hard register becomes live,
1342 record conflicts with live pseudo regs. */
1343 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1345 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1347 else
1348 /* When a pseudo-register becomes live,
1349 record conflicts first with hard regs,
1350 then with other pseudo regs. */
1352 int ialloc = reg_allocno[regno];
1353 int ialloc_prod = ialloc * allocno_row_words;
1355 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1356 for (j = allocno_row_words - 1; j >= 0; j--)
1358 #if 0
1359 int k;
1360 for (k = 0; k < n_no_conflict_pairs; k++)
1361 if (! ((j == no_conflict_pairs[k].allocno1
1362 && ialloc == no_conflict_pairs[k].allocno2)
1364 (j == no_conflict_pairs[k].allocno2
1365 && ialloc == no_conflict_pairs[k].allocno1)))
1366 #endif /* 0 */
1367 conflicts[ialloc_prod + j] |= allocnos_live[j];
1372 /* Record all allocnos currently live as conflicting
1373 with all hard regs currently live.
1375 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1376 are currently live. Their bits are also flagged in allocnos_live. */
1378 static void
1379 record_conflicts (int *allocno_vec, int len)
1381 while (--len >= 0)
1382 IOR_HARD_REG_SET (allocno[allocno_vec[len]].hard_reg_conflicts,
1383 hard_regs_live);
1386 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1387 static void
1388 mirror_conflicts (void)
1390 int i, j;
1391 int rw = allocno_row_words;
1392 int rwb = rw * INT_BITS;
1393 INT_TYPE *p = conflicts;
1394 INT_TYPE *q0 = conflicts, *q1, *q2;
1395 unsigned INT_TYPE mask;
1397 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1399 if (! mask)
1401 mask = 1;
1402 q0++;
1404 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1406 unsigned INT_TYPE word;
1408 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1409 word >>= 1, q2 += rw)
1411 if (word & 1)
1412 *q2 |= mask;
1418 /* Handle the case where REG is set by the insn being scanned,
1419 during the forward scan to accumulate conflicts.
1420 Store a 1 in regs_live or allocnos_live for this register, record how many
1421 consecutive hardware registers it actually needs,
1422 and record a conflict with all other registers already live.
1424 Note that even if REG does not remain alive after this insn,
1425 we must mark it here as live, to ensure a conflict between
1426 REG and any other regs set in this insn that really do live.
1427 This is because those other regs could be considered after this.
1429 REG might actually be something other than a register;
1430 if so, we do nothing.
1432 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1433 a REG_INC note was found for it). */
1435 static void
1436 mark_reg_store (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
1438 int regno;
1440 if (GET_CODE (reg) == SUBREG)
1441 reg = SUBREG_REG (reg);
1443 if (GET_CODE (reg) != REG)
1444 return;
1446 regs_set[n_regs_set++] = reg;
1448 if (setter && GET_CODE (setter) != CLOBBER)
1449 set_preference (reg, SET_SRC (setter));
1451 regno = REGNO (reg);
1453 /* Either this is one of the max_allocno pseudo regs not allocated,
1454 or it is or has a hardware reg. First handle the pseudo-regs. */
1455 if (regno >= FIRST_PSEUDO_REGISTER)
1457 if (reg_allocno[regno] >= 0)
1459 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1460 record_one_conflict (regno);
1464 if (reg_renumber[regno] >= 0)
1465 regno = reg_renumber[regno];
1467 /* Handle hardware regs (and pseudos allocated to hard regs). */
1468 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1470 int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1471 while (regno < last)
1473 record_one_conflict (regno);
1474 SET_HARD_REG_BIT (hard_regs_live, regno);
1475 regno++;
1480 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1482 static void
1483 mark_reg_clobber (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
1485 if (GET_CODE (setter) == CLOBBER)
1486 mark_reg_store (reg, setter, data);
1489 /* Record that REG has conflicts with all the regs currently live.
1490 Do not mark REG itself as live. */
1492 static void
1493 mark_reg_conflicts (rtx reg)
1495 int regno;
1497 if (GET_CODE (reg) == SUBREG)
1498 reg = SUBREG_REG (reg);
1500 if (GET_CODE (reg) != REG)
1501 return;
1503 regno = REGNO (reg);
1505 /* Either this is one of the max_allocno pseudo regs not allocated,
1506 or it is or has a hardware reg. First handle the pseudo-regs. */
1507 if (regno >= FIRST_PSEUDO_REGISTER)
1509 if (reg_allocno[regno] >= 0)
1510 record_one_conflict (regno);
1513 if (reg_renumber[regno] >= 0)
1514 regno = reg_renumber[regno];
1516 /* Handle hardware regs (and pseudos allocated to hard regs). */
1517 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1519 int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1520 while (regno < last)
1522 record_one_conflict (regno);
1523 regno++;
1528 /* Mark REG as being dead (following the insn being scanned now).
1529 Store a 0 in regs_live or allocnos_live for this register. */
1531 static void
1532 mark_reg_death (rtx reg)
1534 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 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 (int regno, enum machine_mode mode)
1570 int last = regno + HARD_REGNO_NREGS (regno, mode);
1571 while (regno < last)
1573 SET_HARD_REG_BIT (hard_regs_live, regno);
1574 regno++;
1578 /* Try to set a preference for an allocno to a hard register.
1579 We are passed DEST and SRC which are the operands of a SET. It is known
1580 that SRC is a register. If SRC or the first operand of SRC is a register,
1581 try to set a preference. If one of the two is a hard register and the other
1582 is a pseudo-register, mark the preference.
1584 Note that we are not as aggressive as local-alloc in trying to tie a
1585 pseudo-register to a hard register. */
1587 static void
1588 set_preference (rtx dest, rtx src)
1590 unsigned int src_regno, dest_regno;
1591 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1592 to compensate for subregs in SRC or DEST. */
1593 int offset = 0;
1594 unsigned int i;
1595 int copy = 1;
1597 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1598 src = XEXP (src, 0), copy = 0;
1600 /* Get the reg number for both SRC and DEST.
1601 If neither is a reg, give up. */
1603 if (GET_CODE (src) == REG)
1604 src_regno = REGNO (src);
1605 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1607 src_regno = REGNO (SUBREG_REG (src));
1609 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1610 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1611 GET_MODE (SUBREG_REG (src)),
1612 SUBREG_BYTE (src),
1613 GET_MODE (src));
1614 else
1615 offset += (SUBREG_BYTE (src)
1616 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
1618 else
1619 return;
1621 if (GET_CODE (dest) == REG)
1622 dest_regno = REGNO (dest);
1623 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1625 dest_regno = REGNO (SUBREG_REG (dest));
1627 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1628 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1629 GET_MODE (SUBREG_REG (dest)),
1630 SUBREG_BYTE (dest),
1631 GET_MODE (dest));
1632 else
1633 offset -= (SUBREG_BYTE (dest)
1634 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
1636 else
1637 return;
1639 /* Convert either or both to hard reg numbers. */
1641 if (reg_renumber[src_regno] >= 0)
1642 src_regno = reg_renumber[src_regno];
1644 if (reg_renumber[dest_regno] >= 0)
1645 dest_regno = reg_renumber[dest_regno];
1647 /* Now if one is a hard reg and the other is a global pseudo
1648 then give the other a preference. */
1650 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1651 && reg_allocno[src_regno] >= 0)
1653 dest_regno -= offset;
1654 if (dest_regno < FIRST_PSEUDO_REGISTER)
1656 if (copy)
1657 SET_REGBIT (hard_reg_copy_preferences,
1658 reg_allocno[src_regno], dest_regno);
1660 SET_REGBIT (hard_reg_preferences,
1661 reg_allocno[src_regno], dest_regno);
1662 for (i = dest_regno;
1663 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1664 i++)
1665 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1669 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1670 && reg_allocno[dest_regno] >= 0)
1672 src_regno += offset;
1673 if (src_regno < FIRST_PSEUDO_REGISTER)
1675 if (copy)
1676 SET_REGBIT (hard_reg_copy_preferences,
1677 reg_allocno[dest_regno], src_regno);
1679 SET_REGBIT (hard_reg_preferences,
1680 reg_allocno[dest_regno], src_regno);
1681 for (i = src_regno;
1682 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1683 i++)
1684 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1689 /* Indicate that hard register number FROM was eliminated and replaced with
1690 an offset from hard register number TO. The status of hard registers live
1691 at the start of a basic block is updated by replacing a use of FROM with
1692 a use of TO. */
1694 void
1695 mark_elimination (int from, int to)
1697 basic_block bb;
1699 FOR_EACH_BB (bb)
1701 regset r = bb->global_live_at_start;
1702 if (REGNO_REG_SET_P (r, from))
1704 CLEAR_REGNO_REG_SET (r, from);
1705 SET_REGNO_REG_SET (r, to);
1710 /* Used for communication between the following functions. Holds the
1711 current life information. */
1712 static regset live_relevant_regs;
1714 /* Record in live_relevant_regs and REGS_SET that register REG became live.
1715 This is called via note_stores. */
1716 static void
1717 reg_becomes_live (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *regs_set)
1719 int regno;
1721 if (GET_CODE (reg) == SUBREG)
1722 reg = SUBREG_REG (reg);
1724 if (GET_CODE (reg) != REG)
1725 return;
1727 regno = REGNO (reg);
1728 if (regno < FIRST_PSEUDO_REGISTER)
1730 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1731 while (nregs-- > 0)
1733 SET_REGNO_REG_SET (live_relevant_regs, regno);
1734 if (! fixed_regs[regno])
1735 SET_REGNO_REG_SET ((regset) regs_set, regno);
1736 regno++;
1739 else if (reg_renumber[regno] >= 0)
1741 SET_REGNO_REG_SET (live_relevant_regs, regno);
1742 SET_REGNO_REG_SET ((regset) regs_set, regno);
1746 /* Record in live_relevant_regs that register REGNO died. */
1747 static void
1748 reg_dies (int regno, enum machine_mode mode, struct insn_chain *chain)
1750 if (regno < FIRST_PSEUDO_REGISTER)
1752 int nregs = HARD_REGNO_NREGS (regno, mode);
1753 while (nregs-- > 0)
1755 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1756 if (! fixed_regs[regno])
1757 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1758 regno++;
1761 else
1763 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1764 if (reg_renumber[regno] >= 0)
1765 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1769 /* Walk the insns of the current function and build reload_insn_chain,
1770 and record register life information. */
1771 void
1772 build_insn_chain (rtx first)
1774 struct insn_chain **p = &reload_insn_chain;
1775 struct insn_chain *prev = 0;
1776 basic_block b = ENTRY_BLOCK_PTR->next_bb;
1777 regset_head live_relevant_regs_head;
1779 live_relevant_regs = INITIALIZE_REG_SET (live_relevant_regs_head);
1781 for (; first; first = NEXT_INSN (first))
1783 struct insn_chain *c;
1785 if (first == b->head)
1787 int i;
1789 CLEAR_REG_SET (live_relevant_regs);
1791 EXECUTE_IF_SET_IN_BITMAP
1792 (b->global_live_at_start, 0, i,
1794 if (i < FIRST_PSEUDO_REGISTER
1795 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1796 : reg_renumber[i] >= 0)
1797 SET_REGNO_REG_SET (live_relevant_regs, i);
1801 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1803 c = new_insn_chain ();
1804 c->prev = prev;
1805 prev = c;
1806 *p = c;
1807 p = &c->next;
1808 c->insn = first;
1809 c->block = b->index;
1811 if (INSN_P (first))
1813 rtx link;
1815 /* Mark the death of everything that dies in this instruction. */
1817 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1818 if (REG_NOTE_KIND (link) == REG_DEAD
1819 && GET_CODE (XEXP (link, 0)) == REG)
1820 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1823 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1825 /* Mark everything born in this instruction as live. */
1827 note_stores (PATTERN (first), reg_becomes_live,
1828 &c->dead_or_set);
1830 else
1831 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1833 if (INSN_P (first))
1835 rtx link;
1837 /* Mark anything that is set in this insn and then unused as dying. */
1839 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1840 if (REG_NOTE_KIND (link) == REG_UNUSED
1841 && GET_CODE (XEXP (link, 0)) == REG)
1842 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1847 if (first == b->end)
1848 b = b->next_bb;
1850 /* Stop after we pass the end of the last basic block. Verify that
1851 no real insns are after the end of the last basic block.
1853 We may want to reorganize the loop somewhat since this test should
1854 always be the right exit test. Allow an ADDR_VEC or ADDR_DIF_VEC if
1855 the previous real insn is a JUMP_INSN. */
1856 if (b == EXIT_BLOCK_PTR)
1858 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1859 if (INSN_P (first)
1860 && GET_CODE (PATTERN (first)) != USE
1861 && ! ((GET_CODE (PATTERN (first)) == ADDR_VEC
1862 || GET_CODE (PATTERN (first)) == ADDR_DIFF_VEC)
1863 && prev_real_insn (first) != 0
1864 && GET_CODE (prev_real_insn (first)) == JUMP_INSN))
1865 abort ();
1866 break;
1869 FREE_REG_SET (live_relevant_regs);
1870 *p = 0;
1873 /* Print debugging trace information if -dg switch is given,
1874 showing the information on which the allocation decisions are based. */
1876 static void
1877 dump_conflicts (FILE *file)
1879 int i;
1880 int has_preferences;
1881 int nregs;
1882 nregs = 0;
1883 for (i = 0; i < max_allocno; i++)
1885 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1886 continue;
1887 nregs++;
1889 fprintf (file, ";; %d regs to allocate:", nregs);
1890 for (i = 0; i < max_allocno; i++)
1892 int j;
1893 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1894 continue;
1895 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1896 for (j = 0; j < max_regno; j++)
1897 if (reg_allocno[j] == allocno_order[i]
1898 && j != allocno[allocno_order[i]].reg)
1899 fprintf (file, "+%d", j);
1900 if (allocno[allocno_order[i]].size != 1)
1901 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1903 fprintf (file, "\n");
1905 for (i = 0; i < max_allocno; i++)
1907 int j;
1908 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1909 for (j = 0; j < max_allocno; j++)
1910 if (CONFLICTP (j, i))
1911 fprintf (file, " %d", allocno[j].reg);
1912 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1913 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1914 fprintf (file, " %d", j);
1915 fprintf (file, "\n");
1917 has_preferences = 0;
1918 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1919 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1920 has_preferences = 1;
1922 if (! has_preferences)
1923 continue;
1924 fprintf (file, ";; %d preferences:", allocno[i].reg);
1925 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1926 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1927 fprintf (file, " %d", j);
1928 fprintf (file, "\n");
1930 fprintf (file, "\n");
1933 void
1934 dump_global_regs (FILE *file)
1936 int i, j;
1938 fprintf (file, ";; Register dispositions:\n");
1939 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1940 if (reg_renumber[i] >= 0)
1942 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1943 if (++j % 6 == 0)
1944 fprintf (file, "\n");
1947 fprintf (file, "\n\n;; Hard regs used: ");
1948 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1949 if (regs_ever_live[i])
1950 fprintf (file, " %d", i);
1951 fprintf (file, "\n\n");