configure.in (GLIBCPP_ENABLE_CXX_FLAGS): Do not pass arguments, let the defaults...
[official-gcc.git] / gcc / global.c
blob3b2334fcbf62f49a8cbcb346cc8e0750bd581588
1 /* Allocate registers for pseudo-registers that span basic blocks.
2 Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
3 1999, 2000, 2002 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;
137 static struct allocno *allocno;
139 /* A vector of the integers from 0 to max_allocno-1,
140 sorted in the order of first-to-be-allocated first. */
142 static int *allocno_order;
144 /* Indexed by (pseudo) reg number, gives the number of another
145 lower-numbered pseudo reg which can share a hard reg with this pseudo
146 *even if the two pseudos would otherwise appear to conflict*. */
148 static int *reg_may_share;
150 /* Define the number of bits in each element of `conflicts' and what
151 type that element has. We use the largest integer format on the
152 host machine. */
154 #define INT_BITS HOST_BITS_PER_WIDE_INT
155 #define INT_TYPE HOST_WIDE_INT
157 /* max_allocno by max_allocno array of bits,
158 recording whether two allocno's conflict (can't go in the same
159 hardware register).
161 `conflicts' is symmetric after the call to mirror_conflicts. */
163 static INT_TYPE *conflicts;
165 /* Number of ints require to hold max_allocno bits.
166 This is the length of a row in `conflicts'. */
168 static int allocno_row_words;
170 /* Two macros to test or store 1 in an element of `conflicts'. */
172 #define CONFLICTP(I, J) \
173 (conflicts[(I) * allocno_row_words + (unsigned) (J) / INT_BITS] \
174 & ((INT_TYPE) 1 << ((unsigned) (J) % INT_BITS)))
176 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
177 and execute CODE. */
178 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
179 do { \
180 int i_; \
181 int allocno_; \
182 INT_TYPE *p_ = (ALLOCNO_SET); \
184 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
185 i_--, allocno_ += INT_BITS) \
187 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
189 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
191 if (word_ & 1) \
192 {CODE;} \
195 } while (0)
197 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
198 #if 0
199 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
200 the conflicting allocno, and execute CODE. This macro assumes that
201 mirror_conflicts has been run. */
202 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
203 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
204 OUT_ALLOCNO, (CODE))
205 #endif
207 /* Set of hard regs currently live (during scan of all insns). */
209 static HARD_REG_SET hard_regs_live;
211 /* Set of registers that global-alloc isn't supposed to use. */
213 static HARD_REG_SET no_global_alloc_regs;
215 /* Set of registers used so far. */
217 static HARD_REG_SET regs_used_so_far;
219 /* Number of refs to each hard reg, as used by local alloc.
220 It is zero for a reg that contains global pseudos or is explicitly used. */
222 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
224 /* Frequency of uses of given hard reg. */
225 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
227 /* Guess at live length of each hard reg, as used by local alloc.
228 This is actually the sum of the live lengths of the specific regs. */
230 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
232 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
233 element I, and hard register number J. */
235 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
237 /* Bit mask for allocnos live at current point in the scan. */
239 static INT_TYPE *allocnos_live;
241 /* Test, set or clear bit number I in allocnos_live,
242 a bit vector indexed by allocno. */
244 #define SET_ALLOCNO_LIVE(I) \
245 (allocnos_live[(unsigned) (I) / INT_BITS] \
246 |= ((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
248 #define CLEAR_ALLOCNO_LIVE(I) \
249 (allocnos_live[(unsigned) (I) / INT_BITS] \
250 &= ~((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
252 /* This is turned off because it doesn't work right for DImode.
253 (And it is only used for DImode, so the other cases are worthless.)
254 The problem is that it isn't true that there is NO possibility of conflict;
255 only that there is no conflict if the two pseudos get the exact same regs.
256 If they were allocated with a partial overlap, there would be a conflict.
257 We can't safely turn off the conflict unless we have another way to
258 prevent the partial overlap.
260 Idea: change hard_reg_conflicts so that instead of recording which
261 hard regs the allocno may not overlap, it records where the allocno
262 may not start. Change both where it is used and where it is updated.
263 Then there is a way to record that (reg:DI 108) may start at 10
264 but not at 9 or 11. There is still the question of how to record
265 this semi-conflict between two pseudos. */
266 #if 0
267 /* Reg pairs for which conflict after the current insn
268 is inhibited by a REG_NO_CONFLICT note.
269 If the table gets full, we ignore any other notes--that is conservative. */
270 #define NUM_NO_CONFLICT_PAIRS 4
271 /* Number of pairs in use in this insn. */
272 int n_no_conflict_pairs;
273 static struct { int allocno1, allocno2;}
274 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
275 #endif /* 0 */
277 /* Record all regs that are set in any one insn.
278 Communication from mark_reg_{store,clobber} and global_conflicts. */
280 static rtx *regs_set;
281 static int n_regs_set;
283 /* All registers that can be eliminated. */
285 static HARD_REG_SET eliminable_regset;
287 static int allocno_compare PARAMS ((const PTR, const PTR));
288 static void global_conflicts PARAMS ((void));
289 static void mirror_conflicts PARAMS ((void));
290 static void expand_preferences PARAMS ((void));
291 static void prune_preferences PARAMS ((void));
292 static void find_reg PARAMS ((int, HARD_REG_SET, int, int, int));
293 static void record_one_conflict PARAMS ((int));
294 static void record_conflicts PARAMS ((int *, int));
295 static void mark_reg_store PARAMS ((rtx, rtx, void *));
296 static void mark_reg_clobber PARAMS ((rtx, rtx, void *));
297 static void mark_reg_conflicts PARAMS ((rtx));
298 static void mark_reg_death PARAMS ((rtx));
299 static void mark_reg_live_nc PARAMS ((int, enum machine_mode));
300 static void set_preference PARAMS ((rtx, rtx));
301 static void dump_conflicts PARAMS ((FILE *));
302 static void reg_becomes_live PARAMS ((rtx, rtx, void *));
303 static void reg_dies PARAMS ((int, enum machine_mode,
304 struct insn_chain *));
306 /* Perform allocation of pseudo-registers not allocated by local_alloc.
307 FILE is a file to output debugging information on,
308 or zero if such output is not desired.
310 Return value is nonzero if reload failed
311 and we must not do any more for this function. */
314 global_alloc (file)
315 FILE *file;
317 int retval;
318 #ifdef ELIMINABLE_REGS
319 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
320 #endif
321 int need_fp
322 = (! flag_omit_frame_pointer
323 #ifdef EXIT_IGNORE_STACK
324 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
325 #endif
326 || FRAME_POINTER_REQUIRED);
328 size_t i;
329 rtx x;
331 max_allocno = 0;
333 /* A machine may have certain hard registers that
334 are safe to use only within a basic block. */
336 CLEAR_HARD_REG_SET (no_global_alloc_regs);
338 /* Build the regset of all eliminable registers and show we can't use those
339 that we already know won't be eliminated. */
340 #ifdef ELIMINABLE_REGS
341 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
343 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
345 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
346 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
347 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
349 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
350 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
351 if (need_fp)
352 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
353 #endif
355 #else
356 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
357 if (need_fp)
358 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
359 #endif
361 /* Track which registers have already been used. Start with registers
362 explicitly in the rtl, then registers allocated by local register
363 allocation. */
365 CLEAR_HARD_REG_SET (regs_used_so_far);
366 #ifdef LEAF_REGISTERS
367 /* If we are doing the leaf function optimization, and this is a leaf
368 function, it means that the registers that take work to save are those
369 that need a register window. So prefer the ones that can be used in
370 a leaf function. */
372 const char *cheap_regs;
373 const char *const leaf_regs = LEAF_REGISTERS;
375 if (only_leaf_regs_used () && leaf_function_p ())
376 cheap_regs = leaf_regs;
377 else
378 cheap_regs = call_used_regs;
379 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
380 if (regs_ever_live[i] || cheap_regs[i])
381 SET_HARD_REG_BIT (regs_used_so_far, i);
383 #else
384 /* We consider registers that do not have to be saved over calls as if
385 they were already used since there is no cost in using them. */
386 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
387 if (regs_ever_live[i] || call_used_regs[i])
388 SET_HARD_REG_BIT (regs_used_so_far, i);
389 #endif
391 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
392 if (reg_renumber[i] >= 0)
393 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
395 /* Establish mappings from register number to allocation number
396 and vice versa. In the process, count the allocnos. */
398 reg_allocno = (int *) xmalloc (max_regno * sizeof (int));
400 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
401 reg_allocno[i] = -1;
403 /* Initialize the shared-hard-reg mapping
404 from the list of pairs that may share. */
405 reg_may_share = (int *) xcalloc (max_regno, sizeof (int));
406 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
408 int r1 = REGNO (XEXP (x, 0));
409 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
410 if (r1 > r2)
411 reg_may_share[r1] = r2;
412 else
413 reg_may_share[r2] = r1;
416 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
417 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
418 that we are supposed to refrain from putting in a hard reg.
419 -2 means do make an allocno but don't allocate it. */
420 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
421 /* Don't allocate pseudos that cross calls,
422 if this function receives a nonlocal goto. */
423 && (! current_function_has_nonlocal_label
424 || REG_N_CALLS_CROSSED (i) == 0))
426 if (reg_renumber[i] < 0 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
427 reg_allocno[i] = reg_allocno[reg_may_share[i]];
428 else
429 reg_allocno[i] = max_allocno++;
430 if (REG_LIVE_LENGTH (i) == 0)
431 abort ();
433 else
434 reg_allocno[i] = -1;
436 allocno = (struct allocno *) xcalloc (max_allocno, sizeof (struct allocno));
438 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
439 if (reg_allocno[i] >= 0)
441 int num = reg_allocno[i];
442 allocno[num].reg = i;
443 allocno[num].size = PSEUDO_REGNO_SIZE (i);
444 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
445 allocno[num].n_refs += REG_N_REFS (i);
446 allocno[num].freq += REG_FREQ (i);
447 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
448 allocno[num].live_length = REG_LIVE_LENGTH (i);
451 /* Calculate amount of usage of each hard reg by pseudos
452 allocated by local-alloc. This is to see if we want to
453 override it. */
454 memset ((char *) local_reg_live_length, 0, sizeof local_reg_live_length);
455 memset ((char *) local_reg_n_refs, 0, sizeof local_reg_n_refs);
456 memset ((char *) local_reg_freq, 0, sizeof local_reg_freq);
457 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
458 if (reg_renumber[i] >= 0)
460 int regno = reg_renumber[i];
461 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
462 int j;
464 for (j = regno; j < endregno; j++)
466 local_reg_n_refs[j] += REG_N_REFS (i);
467 local_reg_freq[j] += REG_FREQ (i);
468 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
472 /* We can't override local-alloc for a reg used not just by local-alloc. */
473 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
474 if (regs_ever_live[i])
475 local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
477 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
479 /* We used to use alloca here, but the size of what it would try to
480 allocate would occasionally cause it to exceed the stack limit and
481 cause unpredictable core dumps. Some examples were > 2Mb in size. */
482 conflicts = (INT_TYPE *) xcalloc (max_allocno * allocno_row_words,
483 sizeof (INT_TYPE));
485 allocnos_live = (INT_TYPE *) xmalloc (allocno_row_words * sizeof (INT_TYPE));
487 /* If there is work to be done (at least one reg to allocate),
488 perform global conflict analysis and allocate the regs. */
490 if (max_allocno > 0)
492 /* Scan all the insns and compute the conflicts among allocnos
493 and between allocnos and hard regs. */
495 global_conflicts ();
497 mirror_conflicts ();
499 /* Eliminate conflicts between pseudos and eliminable registers. If
500 the register is not eliminated, the pseudo won't really be able to
501 live in the eliminable register, so the conflict doesn't matter.
502 If we do eliminate the register, the conflict will no longer exist.
503 So in either case, we can ignore the conflict. Likewise for
504 preferences. */
506 for (i = 0; i < (size_t) max_allocno; i++)
508 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
509 eliminable_regset);
510 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
511 eliminable_regset);
512 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
513 eliminable_regset);
516 /* Try to expand the preferences by merging them between allocnos. */
518 expand_preferences ();
520 /* Determine the order to allocate the remaining pseudo registers. */
522 allocno_order = (int *) xmalloc (max_allocno * sizeof (int));
523 for (i = 0; i < (size_t) max_allocno; i++)
524 allocno_order[i] = i;
526 /* Default the size to 1, since allocno_compare uses it to divide by.
527 Also convert allocno_live_length of zero to -1. A length of zero
528 can occur when all the registers for that allocno have reg_live_length
529 equal to -2. In this case, we want to make an allocno, but not
530 allocate it. So avoid the divide-by-zero and set it to a low
531 priority. */
533 for (i = 0; i < (size_t) max_allocno; i++)
535 if (allocno[i].size == 0)
536 allocno[i].size = 1;
537 if (allocno[i].live_length == 0)
538 allocno[i].live_length = -1;
541 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
543 prune_preferences ();
545 if (file)
546 dump_conflicts (file);
548 /* Try allocating them, one by one, in that order,
549 except for parameters marked with reg_live_length[regno] == -2. */
551 for (i = 0; i < (size_t) max_allocno; i++)
552 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
553 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
555 /* If we have more than one register class,
556 first try allocating in the class that is cheapest
557 for this pseudo-reg. If that fails, try any reg. */
558 if (N_REG_CLASSES > 1)
560 find_reg (allocno_order[i], 0, 0, 0, 0);
561 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
562 continue;
564 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
565 find_reg (allocno_order[i], 0, 1, 0, 0);
568 free (allocno_order);
571 /* Do the reloads now while the allocno data still exist, so that we can
572 try to assign new hard regs to any pseudo regs that are spilled. */
574 #if 0 /* We need to eliminate regs even if there is no rtl code,
575 for the sake of debugging information. */
576 if (n_basic_blocks > 0)
577 #endif
579 build_insn_chain (get_insns ());
580 retval = reload (get_insns (), 1);
583 /* Clean up. */
584 free (reg_allocno);
585 free (reg_may_share);
586 free (allocno);
587 free (conflicts);
588 free (allocnos_live);
590 return retval;
593 /* Sort predicate for ordering the allocnos.
594 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
596 static int
597 allocno_compare (v1p, v2p)
598 const PTR v1p;
599 const PTR v2p;
601 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
602 /* Note that the quotient will never be bigger than
603 the value of floor_log2 times the maximum number of
604 times a register can occur in one insn (surely less than 100)
605 weighted by the frequency (maximally REG_FREQ_MAX).
606 Multiplying this by 10000/REG_FREQ_MAX can't overflow. */
607 int pri1
608 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
609 / allocno[v1].live_length)
610 * (10000 / REG_FREQ_MAX) * allocno[v1].size);
611 int pri2
612 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
613 / allocno[v2].live_length)
614 * (10000 / REG_FREQ_MAX) * allocno[v2].size);
615 if (pri2 - pri1)
616 return pri2 - pri1;
618 /* If regs are equally good, sort by allocno,
619 so that the results of qsort leave nothing to chance. */
620 return v1 - v2;
623 /* Scan the rtl code and record all conflicts and register preferences in the
624 conflict matrices and preference tables. */
626 static void
627 global_conflicts ()
629 int i;
630 basic_block b;
631 rtx insn;
632 int *block_start_allocnos;
634 /* Make a vector that mark_reg_{store,clobber} will store in. */
635 regs_set = (rtx *) xmalloc (max_parallel * sizeof (rtx) * 2);
637 block_start_allocnos = (int *) xmalloc (max_allocno * sizeof (int));
639 FOR_EACH_BB (b)
641 memset ((char *) allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
643 /* Initialize table of registers currently live
644 to the state at the beginning of this basic block.
645 This also marks the conflicts among hard registers
646 and any allocnos that are live.
648 For pseudo-regs, there is only one bit for each one
649 no matter how many hard regs it occupies.
650 This is ok; we know the size from PSEUDO_REGNO_SIZE.
651 For explicit hard regs, we cannot know the size that way
652 since one hard reg can be used with various sizes.
653 Therefore, we must require that all the hard regs
654 implicitly live as part of a multi-word hard reg
655 are explicitly marked in basic_block_live_at_start. */
658 regset old = b->global_live_at_start;
659 int ax = 0;
661 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
662 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
664 int a = reg_allocno[i];
665 if (a >= 0)
667 SET_ALLOCNO_LIVE (a);
668 block_start_allocnos[ax++] = a;
670 else if ((a = reg_renumber[i]) >= 0)
671 mark_reg_live_nc
672 (a, PSEUDO_REGNO_MODE (i));
675 /* Record that each allocno now live conflicts with each hard reg
676 now live.
678 It is not necessary to mark any conflicts between pseudos as
679 this point, even for pseudos which are live at the start of
680 the basic block.
682 Given two pseudos X and Y and any point in the CFG P.
684 On any path to point P where X and Y are live one of the
685 following conditions must be true:
687 1. X is live at some instruction on the path that
688 evaluates Y.
690 2. Y is live at some instruction on the path that
691 evaluates X.
693 3. Either X or Y is not evaluted on the path to P
694 (ie it is used uninitialized) and thus the
695 conflict can be ignored.
697 In cases #1 and #2 the conflict will be recorded when we
698 scan the instruction that makes either X or Y become live. */
699 record_conflicts (block_start_allocnos, ax);
701 #ifdef STACK_REGS
703 /* Pseudos can't go in stack regs at the start of a basic block
704 that is reached by an abnormal edge. */
706 edge e;
707 for (e = b->pred; e ; e = e->pred_next)
708 if (e->flags & EDGE_ABNORMAL)
709 break;
710 if (e != NULL)
711 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
712 record_one_conflict (ax);
714 #endif
717 insn = b->head;
719 /* Scan the code of this basic block, noting which allocnos
720 and hard regs are born or die. When one is born,
721 record a conflict with all others currently live. */
723 while (1)
725 RTX_CODE code = GET_CODE (insn);
726 rtx link;
728 /* Make regs_set an empty set. */
730 n_regs_set = 0;
732 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
735 #if 0
736 int i = 0;
737 for (link = REG_NOTES (insn);
738 link && i < NUM_NO_CONFLICT_PAIRS;
739 link = XEXP (link, 1))
740 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
742 no_conflict_pairs[i].allocno1
743 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
744 no_conflict_pairs[i].allocno2
745 = reg_allocno[REGNO (XEXP (link, 0))];
746 i++;
748 #endif /* 0 */
750 /* Mark any registers clobbered by INSN as live,
751 so they conflict with the inputs. */
753 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
755 /* Mark any registers dead after INSN as dead now. */
757 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
758 if (REG_NOTE_KIND (link) == REG_DEAD)
759 mark_reg_death (XEXP (link, 0));
761 /* Mark any registers set in INSN as live,
762 and mark them as conflicting with all other live regs.
763 Clobbers are processed again, so they conflict with
764 the registers that are set. */
766 note_stores (PATTERN (insn), mark_reg_store, NULL);
768 #ifdef AUTO_INC_DEC
769 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
770 if (REG_NOTE_KIND (link) == REG_INC)
771 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
772 #endif
774 /* If INSN has multiple outputs, then any reg that dies here
775 and is used inside of an output
776 must conflict with the other outputs.
778 It is unsafe to use !single_set here since it will ignore an
779 unused output. Just because an output is unused does not mean
780 the compiler can assume the side effect will not occur.
781 Consider if REG appears in the address of an output and we
782 reload the output. If we allocate REG to the same hard
783 register as an unused output we could set the hard register
784 before the output reload insn. */
785 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
786 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
787 if (REG_NOTE_KIND (link) == REG_DEAD)
789 int used_in_output = 0;
790 int i;
791 rtx reg = XEXP (link, 0);
793 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
795 rtx set = XVECEXP (PATTERN (insn), 0, i);
796 if (GET_CODE (set) == SET
797 && GET_CODE (SET_DEST (set)) != REG
798 && !rtx_equal_p (reg, SET_DEST (set))
799 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
800 used_in_output = 1;
802 if (used_in_output)
803 mark_reg_conflicts (reg);
806 /* Mark any registers set in INSN and then never used. */
808 while (n_regs_set-- > 0)
810 rtx note = find_regno_note (insn, REG_UNUSED,
811 REGNO (regs_set[n_regs_set]));
812 if (note)
813 mark_reg_death (XEXP (note, 0));
817 if (insn == b->end)
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 (INSN_P (insn)
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 nonzero, 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 int i, best_reg, pass;
979 HARD_REG_SET used, used1, used2;
981 enum reg_class class = (alt_regs_p
982 ? reg_alternate_class (allocno[num].reg)
983 : reg_preferred_class (allocno[num].reg));
984 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
986 if (accept_call_clobbered)
987 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
988 else if (allocno[num].calls_crossed == 0)
989 COPY_HARD_REG_SET (used1, fixed_reg_set);
990 else
991 COPY_HARD_REG_SET (used1, call_used_reg_set);
993 /* Some registers should not be allocated in global-alloc. */
994 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
995 if (losers)
996 IOR_HARD_REG_SET (used1, losers);
998 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
999 COPY_HARD_REG_SET (used2, used1);
1001 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1003 #ifdef CANNOT_CHANGE_MODE_CLASS
1004 cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1005 #endif
1007 /* Try each hard reg to see if it fits. Do this in two passes.
1008 In the first pass, skip registers that are preferred by some other pseudo
1009 to give it a better chance of getting one of those registers. Only if
1010 we can't get a register when excluding those do we take one of them.
1011 However, we never allocate a register for the first time in pass 0. */
1013 COPY_HARD_REG_SET (used, used1);
1014 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1015 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1017 best_reg = -1;
1018 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1019 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1020 pass++)
1022 if (pass == 1)
1023 COPY_HARD_REG_SET (used, used1);
1024 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1026 #ifdef REG_ALLOC_ORDER
1027 int regno = reg_alloc_order[i];
1028 #else
1029 int regno = i;
1030 #endif
1031 if (! TEST_HARD_REG_BIT (used, regno)
1032 && HARD_REGNO_MODE_OK (regno, mode)
1033 && (allocno[num].calls_crossed == 0
1034 || accept_call_clobbered
1035 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1037 int j;
1038 int lim = regno + HARD_REGNO_NREGS (regno, mode);
1039 for (j = regno + 1;
1040 (j < lim
1041 && ! TEST_HARD_REG_BIT (used, j));
1042 j++);
1043 if (j == lim)
1045 best_reg = regno;
1046 break;
1048 #ifndef REG_ALLOC_ORDER
1049 i = j; /* Skip starting points we know will lose */
1050 #endif
1055 /* See if there is a preferred register with the same class as the register
1056 we allocated above. Making this restriction prevents register
1057 preferencing from creating worse register allocation.
1059 Remove from the preferred registers and conflicting registers. Note that
1060 additional conflicts may have been added after `prune_preferences' was
1061 called.
1063 First do this for those register with copy preferences, then all
1064 preferred registers. */
1066 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1067 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1068 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1070 if (best_reg >= 0)
1072 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1073 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1074 && HARD_REGNO_MODE_OK (i, mode)
1075 && (allocno[num].calls_crossed == 0
1076 || accept_call_clobbered
1077 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1078 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1079 || reg_class_subset_p (REGNO_REG_CLASS (i),
1080 REGNO_REG_CLASS (best_reg))
1081 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1082 REGNO_REG_CLASS (i))))
1084 int j;
1085 int lim = i + HARD_REGNO_NREGS (i, mode);
1086 for (j = i + 1;
1087 (j < lim
1088 && ! TEST_HARD_REG_BIT (used, j)
1089 && (REGNO_REG_CLASS (j)
1090 == REGNO_REG_CLASS (best_reg + (j - i))
1091 || reg_class_subset_p (REGNO_REG_CLASS (j),
1092 REGNO_REG_CLASS (best_reg + (j - i)))
1093 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1094 REGNO_REG_CLASS (j))));
1095 j++);
1096 if (j == lim)
1098 best_reg = i;
1099 goto no_prefs;
1103 no_copy_prefs:
1105 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1106 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1107 reg_class_contents[(int) NO_REGS], no_prefs);
1109 if (best_reg >= 0)
1111 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1112 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1113 && HARD_REGNO_MODE_OK (i, mode)
1114 && (allocno[num].calls_crossed == 0
1115 || accept_call_clobbered
1116 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1117 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1118 || reg_class_subset_p (REGNO_REG_CLASS (i),
1119 REGNO_REG_CLASS (best_reg))
1120 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1121 REGNO_REG_CLASS (i))))
1123 int j;
1124 int lim = i + HARD_REGNO_NREGS (i, mode);
1125 for (j = i + 1;
1126 (j < lim
1127 && ! TEST_HARD_REG_BIT (used, j)
1128 && (REGNO_REG_CLASS (j)
1129 == REGNO_REG_CLASS (best_reg + (j - i))
1130 || reg_class_subset_p (REGNO_REG_CLASS (j),
1131 REGNO_REG_CLASS (best_reg + (j - i)))
1132 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1133 REGNO_REG_CLASS (j))));
1134 j++);
1135 if (j == lim)
1137 best_reg = i;
1138 break;
1142 no_prefs:
1144 /* If we haven't succeeded yet, try with caller-saves.
1145 We need not check to see if the current function has nonlocal
1146 labels because we don't put any pseudos that are live over calls in
1147 registers in that case. */
1149 if (flag_caller_saves && best_reg < 0)
1151 /* Did not find a register. If it would be profitable to
1152 allocate a call-clobbered register and save and restore it
1153 around calls, do that. */
1154 if (! accept_call_clobbered
1155 && allocno[num].calls_crossed != 0
1156 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1157 allocno[num].calls_crossed))
1159 HARD_REG_SET new_losers;
1160 if (! losers)
1161 CLEAR_HARD_REG_SET (new_losers);
1162 else
1163 COPY_HARD_REG_SET (new_losers, losers);
1165 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1166 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1167 if (reg_renumber[allocno[num].reg] >= 0)
1169 caller_save_needed = 1;
1170 return;
1175 /* If we haven't succeeded yet,
1176 see if some hard reg that conflicts with us
1177 was utilized poorly by local-alloc.
1178 If so, kick out the regs that were put there by local-alloc
1179 so we can use it instead. */
1180 if (best_reg < 0 && !retrying
1181 /* Let's not bother with multi-reg allocnos. */
1182 && allocno[num].size == 1)
1184 /* Count from the end, to find the least-used ones first. */
1185 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1187 #ifdef REG_ALLOC_ORDER
1188 int regno = reg_alloc_order[i];
1189 #else
1190 int regno = i;
1191 #endif
1193 if (local_reg_n_refs[regno] != 0
1194 /* Don't use a reg no good for this pseudo. */
1195 && ! TEST_HARD_REG_BIT (used2, regno)
1196 && HARD_REGNO_MODE_OK (regno, mode)
1197 /* The code below assumes that we need only a single
1198 register, but the check of allocno[num].size above
1199 was not enough. Sometimes we need more than one
1200 register for a single-word value. */
1201 && HARD_REGNO_NREGS (regno, mode) == 1
1202 && (allocno[num].calls_crossed == 0
1203 || accept_call_clobbered
1204 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1205 #ifdef CANNOT_CHANGE_MODE_CLASS
1206 && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1207 mode)
1208 #endif
1211 /* We explicitly evaluate the divide results into temporary
1212 variables so as to avoid excess precision problems that occur
1213 on an i386-unknown-sysv4.2 (unixware) host. */
1215 double tmp1 = ((double) local_reg_freq[regno]
1216 / local_reg_live_length[regno]);
1217 double tmp2 = ((double) allocno[num].freq
1218 / allocno[num].live_length);
1220 if (tmp1 < tmp2)
1222 /* Hard reg REGNO was used less in total by local regs
1223 than it would be used by this one allocno! */
1224 int k;
1225 for (k = 0; k < max_regno; k++)
1226 if (reg_renumber[k] >= 0)
1228 int r = reg_renumber[k];
1229 int endregno
1230 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
1232 if (regno >= r && regno < endregno)
1233 reg_renumber[k] = -1;
1236 best_reg = regno;
1237 break;
1243 /* Did we find a register? */
1245 if (best_reg >= 0)
1247 int lim, j;
1248 HARD_REG_SET this_reg;
1250 /* Yes. Record it as the hard register of this pseudo-reg. */
1251 reg_renumber[allocno[num].reg] = best_reg;
1252 /* Also of any pseudo-regs that share with it. */
1253 if (reg_may_share[allocno[num].reg])
1254 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1255 if (reg_allocno[j] == num)
1256 reg_renumber[j] = best_reg;
1258 /* Make a set of the hard regs being allocated. */
1259 CLEAR_HARD_REG_SET (this_reg);
1260 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1261 for (j = best_reg; j < lim; j++)
1263 SET_HARD_REG_BIT (this_reg, j);
1264 SET_HARD_REG_BIT (regs_used_so_far, j);
1265 /* This is no longer a reg used just by local regs. */
1266 local_reg_n_refs[j] = 0;
1267 local_reg_freq[j] = 0;
1269 /* For each other pseudo-reg conflicting with this one,
1270 mark it as conflicting with the hard regs this one occupies. */
1271 lim = num;
1272 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1274 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1279 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1280 Perhaps it had previously seemed not worth a hard reg,
1281 or perhaps its old hard reg has been commandeered for reloads.
1282 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1283 they do not appear to be allocated.
1284 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1286 void
1287 retry_global_alloc (regno, forbidden_regs)
1288 int regno;
1289 HARD_REG_SET forbidden_regs;
1291 int alloc_no = reg_allocno[regno];
1292 if (alloc_no >= 0)
1294 /* If we have more than one register class,
1295 first try allocating in the class that is cheapest
1296 for this pseudo-reg. If that fails, try any reg. */
1297 if (N_REG_CLASSES > 1)
1298 find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1299 if (reg_renumber[regno] < 0
1300 && reg_alternate_class (regno) != NO_REGS)
1301 find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1303 /* If we found a register, modify the RTL for the register to
1304 show the hard register, and mark that register live. */
1305 if (reg_renumber[regno] >= 0)
1307 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1308 mark_home_live (regno);
1313 /* Record a conflict between register REGNO
1314 and everything currently live.
1315 REGNO must not be a pseudo reg that was allocated
1316 by local_alloc; such numbers must be translated through
1317 reg_renumber before calling here. */
1319 static void
1320 record_one_conflict (regno)
1321 int regno;
1323 int j;
1325 if (regno < FIRST_PSEUDO_REGISTER)
1326 /* When a hard register becomes live,
1327 record conflicts with live pseudo regs. */
1328 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1330 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1332 else
1333 /* When a pseudo-register becomes live,
1334 record conflicts first with hard regs,
1335 then with other pseudo regs. */
1337 int ialloc = reg_allocno[regno];
1338 int ialloc_prod = ialloc * allocno_row_words;
1340 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1341 for (j = allocno_row_words - 1; j >= 0; j--)
1343 #if 0
1344 int k;
1345 for (k = 0; k < n_no_conflict_pairs; k++)
1346 if (! ((j == no_conflict_pairs[k].allocno1
1347 && ialloc == no_conflict_pairs[k].allocno2)
1349 (j == no_conflict_pairs[k].allocno2
1350 && ialloc == no_conflict_pairs[k].allocno1)))
1351 #endif /* 0 */
1352 conflicts[ialloc_prod + j] |= allocnos_live[j];
1357 /* Record all allocnos currently live as conflicting
1358 with all hard regs currently live.
1360 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1361 are currently live. Their bits are also flagged in allocnos_live. */
1363 static void
1364 record_conflicts (allocno_vec, len)
1365 int *allocno_vec;
1366 int len;
1368 while (--len >= 0)
1369 IOR_HARD_REG_SET (allocno[allocno_vec[len]].hard_reg_conflicts,
1370 hard_regs_live);
1373 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1374 static void
1375 mirror_conflicts ()
1377 int i, j;
1378 int rw = allocno_row_words;
1379 int rwb = rw * INT_BITS;
1380 INT_TYPE *p = conflicts;
1381 INT_TYPE *q0 = conflicts, *q1, *q2;
1382 unsigned INT_TYPE mask;
1384 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1386 if (! mask)
1388 mask = 1;
1389 q0++;
1391 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1393 unsigned INT_TYPE word;
1395 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1396 word >>= 1, q2 += rw)
1398 if (word & 1)
1399 *q2 |= mask;
1405 /* Handle the case where REG is set by the insn being scanned,
1406 during the forward scan to accumulate conflicts.
1407 Store a 1 in regs_live or allocnos_live for this register, record how many
1408 consecutive hardware registers it actually needs,
1409 and record a conflict with all other registers already live.
1411 Note that even if REG does not remain alive after this insn,
1412 we must mark it here as live, to ensure a conflict between
1413 REG and any other regs set in this insn that really do live.
1414 This is because those other regs could be considered after this.
1416 REG might actually be something other than a register;
1417 if so, we do nothing.
1419 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1420 a REG_INC note was found for it). */
1422 static void
1423 mark_reg_store (reg, setter, data)
1424 rtx reg, setter;
1425 void *data ATTRIBUTE_UNUSED;
1427 int regno;
1429 if (GET_CODE (reg) == SUBREG)
1430 reg = SUBREG_REG (reg);
1432 if (GET_CODE (reg) != REG)
1433 return;
1435 regs_set[n_regs_set++] = reg;
1437 if (setter && GET_CODE (setter) != CLOBBER)
1438 set_preference (reg, SET_SRC (setter));
1440 regno = REGNO (reg);
1442 /* Either this is one of the max_allocno pseudo regs not allocated,
1443 or it is or has a hardware reg. First handle the pseudo-regs. */
1444 if (regno >= FIRST_PSEUDO_REGISTER)
1446 if (reg_allocno[regno] >= 0)
1448 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1449 record_one_conflict (regno);
1453 if (reg_renumber[regno] >= 0)
1454 regno = reg_renumber[regno];
1456 /* Handle hardware regs (and pseudos allocated to hard regs). */
1457 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1459 int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1460 while (regno < last)
1462 record_one_conflict (regno);
1463 SET_HARD_REG_BIT (hard_regs_live, regno);
1464 regno++;
1469 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1471 static void
1472 mark_reg_clobber (reg, setter, data)
1473 rtx reg, setter;
1474 void *data ATTRIBUTE_UNUSED;
1476 if (GET_CODE (setter) == CLOBBER)
1477 mark_reg_store (reg, setter, data);
1480 /* Record that REG has conflicts with all the regs currently live.
1481 Do not mark REG itself as live. */
1483 static void
1484 mark_reg_conflicts (reg)
1485 rtx reg;
1487 int regno;
1489 if (GET_CODE (reg) == SUBREG)
1490 reg = SUBREG_REG (reg);
1492 if (GET_CODE (reg) != REG)
1493 return;
1495 regno = REGNO (reg);
1497 /* Either this is one of the max_allocno pseudo regs not allocated,
1498 or it is or has a hardware reg. First handle the pseudo-regs. */
1499 if (regno >= FIRST_PSEUDO_REGISTER)
1501 if (reg_allocno[regno] >= 0)
1502 record_one_conflict (regno);
1505 if (reg_renumber[regno] >= 0)
1506 regno = reg_renumber[regno];
1508 /* Handle hardware regs (and pseudos allocated to hard regs). */
1509 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1511 int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1512 while (regno < last)
1514 record_one_conflict (regno);
1515 regno++;
1520 /* Mark REG as being dead (following the insn being scanned now).
1521 Store a 0 in regs_live or allocnos_live for this register. */
1523 static void
1524 mark_reg_death (reg)
1525 rtx reg;
1527 int regno = REGNO (reg);
1529 /* Either this is one of the max_allocno pseudo regs not allocated,
1530 or it is a hardware reg. First handle the pseudo-regs. */
1531 if (regno >= FIRST_PSEUDO_REGISTER)
1533 if (reg_allocno[regno] >= 0)
1534 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1537 /* For pseudo reg, see if it has been assigned a hardware reg. */
1538 if (reg_renumber[regno] >= 0)
1539 regno = reg_renumber[regno];
1541 /* Handle hardware regs (and pseudos allocated to hard regs). */
1542 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1544 /* Pseudo regs already assigned hardware regs are treated
1545 almost the same as explicit hardware regs. */
1546 int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1547 while (regno < last)
1549 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1550 regno++;
1555 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1556 for the value stored in it. MODE determines how many consecutive
1557 registers are actually in use. Do not record conflicts;
1558 it is assumed that the caller will do that. */
1560 static void
1561 mark_reg_live_nc (regno, mode)
1562 int regno;
1563 enum machine_mode mode;
1565 int last = regno + HARD_REGNO_NREGS (regno, mode);
1566 while (regno < last)
1568 SET_HARD_REG_BIT (hard_regs_live, regno);
1569 regno++;
1573 /* Try to set a preference for an allocno to a hard register.
1574 We are passed DEST and SRC which are the operands of a SET. It is known
1575 that SRC is a register. If SRC or the first operand of SRC is a register,
1576 try to set a preference. If one of the two is a hard register and the other
1577 is a pseudo-register, mark the preference.
1579 Note that we are not as aggressive as local-alloc in trying to tie a
1580 pseudo-register to a hard register. */
1582 static void
1583 set_preference (dest, src)
1584 rtx dest, src;
1586 unsigned int src_regno, dest_regno;
1587 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1588 to compensate for subregs in SRC or DEST. */
1589 int offset = 0;
1590 unsigned int i;
1591 int copy = 1;
1593 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1594 src = XEXP (src, 0), copy = 0;
1596 /* Get the reg number for both SRC and DEST.
1597 If neither is a reg, give up. */
1599 if (GET_CODE (src) == REG)
1600 src_regno = REGNO (src);
1601 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1603 src_regno = REGNO (SUBREG_REG (src));
1605 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1606 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1607 GET_MODE (SUBREG_REG (src)),
1608 SUBREG_BYTE (src),
1609 GET_MODE (src));
1610 else
1611 offset += (SUBREG_BYTE (src)
1612 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
1614 else
1615 return;
1617 if (GET_CODE (dest) == REG)
1618 dest_regno = REGNO (dest);
1619 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1621 dest_regno = REGNO (SUBREG_REG (dest));
1623 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1624 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1625 GET_MODE (SUBREG_REG (dest)),
1626 SUBREG_BYTE (dest),
1627 GET_MODE (dest));
1628 else
1629 offset -= (SUBREG_BYTE (dest)
1630 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
1632 else
1633 return;
1635 /* Convert either or both to hard reg numbers. */
1637 if (reg_renumber[src_regno] >= 0)
1638 src_regno = reg_renumber[src_regno];
1640 if (reg_renumber[dest_regno] >= 0)
1641 dest_regno = reg_renumber[dest_regno];
1643 /* Now if one is a hard reg and the other is a global pseudo
1644 then give the other a preference. */
1646 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1647 && reg_allocno[src_regno] >= 0)
1649 dest_regno -= offset;
1650 if (dest_regno < FIRST_PSEUDO_REGISTER)
1652 if (copy)
1653 SET_REGBIT (hard_reg_copy_preferences,
1654 reg_allocno[src_regno], dest_regno);
1656 SET_REGBIT (hard_reg_preferences,
1657 reg_allocno[src_regno], dest_regno);
1658 for (i = dest_regno;
1659 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1660 i++)
1661 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1665 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1666 && reg_allocno[dest_regno] >= 0)
1668 src_regno += offset;
1669 if (src_regno < FIRST_PSEUDO_REGISTER)
1671 if (copy)
1672 SET_REGBIT (hard_reg_copy_preferences,
1673 reg_allocno[dest_regno], src_regno);
1675 SET_REGBIT (hard_reg_preferences,
1676 reg_allocno[dest_regno], src_regno);
1677 for (i = src_regno;
1678 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1679 i++)
1680 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1685 /* Indicate that hard register number FROM was eliminated and replaced with
1686 an offset from hard register number TO. The status of hard registers live
1687 at the start of a basic block is updated by replacing a use of FROM with
1688 a use of TO. */
1690 void
1691 mark_elimination (from, to)
1692 int from, to;
1694 basic_block bb;
1696 FOR_EACH_BB (bb)
1698 regset r = bb->global_live_at_start;
1699 if (REGNO_REG_SET_P (r, from))
1701 CLEAR_REGNO_REG_SET (r, from);
1702 SET_REGNO_REG_SET (r, to);
1707 /* Used for communication between the following functions. Holds the
1708 current life information. */
1709 static regset live_relevant_regs;
1711 /* Record in live_relevant_regs and REGS_SET that register REG became live.
1712 This is called via note_stores. */
1713 static void
1714 reg_becomes_live (reg, setter, regs_set)
1715 rtx reg;
1716 rtx setter ATTRIBUTE_UNUSED;
1717 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 (regno, mode, chain)
1749 int regno;
1750 enum machine_mode mode;
1751 struct insn_chain *chain;
1753 if (regno < FIRST_PSEUDO_REGISTER)
1755 int nregs = HARD_REGNO_NREGS (regno, mode);
1756 while (nregs-- > 0)
1758 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1759 if (! fixed_regs[regno])
1760 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1761 regno++;
1764 else
1766 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1767 if (reg_renumber[regno] >= 0)
1768 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1772 /* Walk the insns of the current function and build reload_insn_chain,
1773 and record register life information. */
1774 void
1775 build_insn_chain (first)
1776 rtx first;
1778 struct insn_chain **p = &reload_insn_chain;
1779 struct insn_chain *prev = 0;
1780 basic_block b = ENTRY_BLOCK_PTR->next_bb;
1781 regset_head live_relevant_regs_head;
1783 live_relevant_regs = INITIALIZE_REG_SET (live_relevant_regs_head);
1785 for (; first; first = NEXT_INSN (first))
1787 struct insn_chain *c;
1789 if (first == b->head)
1791 int i;
1793 CLEAR_REG_SET (live_relevant_regs);
1795 EXECUTE_IF_SET_IN_BITMAP
1796 (b->global_live_at_start, 0, i,
1798 if (i < FIRST_PSEUDO_REGISTER
1799 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1800 : reg_renumber[i] >= 0)
1801 SET_REGNO_REG_SET (live_relevant_regs, i);
1805 if (GET_CODE (first) != NOTE && GET_CODE (first) != BARRIER)
1807 c = new_insn_chain ();
1808 c->prev = prev;
1809 prev = c;
1810 *p = c;
1811 p = &c->next;
1812 c->insn = first;
1813 c->block = b->index;
1815 if (INSN_P (first))
1817 rtx link;
1819 /* Mark the death of everything that dies in this instruction. */
1821 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1822 if (REG_NOTE_KIND (link) == REG_DEAD
1823 && GET_CODE (XEXP (link, 0)) == REG)
1824 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1827 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1829 /* Mark everything born in this instruction as live. */
1831 note_stores (PATTERN (first), reg_becomes_live,
1832 &c->dead_or_set);
1834 else
1835 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1837 if (INSN_P (first))
1839 rtx link;
1841 /* Mark anything that is set in this insn and then unused as dying. */
1843 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1844 if (REG_NOTE_KIND (link) == REG_UNUSED
1845 && GET_CODE (XEXP (link, 0)) == REG)
1846 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1851 if (first == b->end)
1852 b = b->next_bb;
1854 /* Stop after we pass the end of the last basic block. Verify that
1855 no real insns are after the end of the last basic block.
1857 We may want to reorganize the loop somewhat since this test should
1858 always be the right exit test. Allow an ADDR_VEC or ADDR_DIF_VEC if
1859 the previous real insn is a JUMP_INSN. */
1860 if (b == EXIT_BLOCK_PTR)
1862 for (first = NEXT_INSN (first) ; first; first = NEXT_INSN (first))
1863 if (INSN_P (first)
1864 && GET_CODE (PATTERN (first)) != USE
1865 && ! ((GET_CODE (PATTERN (first)) == ADDR_VEC
1866 || GET_CODE (PATTERN (first)) == ADDR_DIFF_VEC)
1867 && prev_real_insn (first) != 0
1868 && GET_CODE (prev_real_insn (first)) == JUMP_INSN))
1869 abort ();
1870 break;
1873 FREE_REG_SET (live_relevant_regs);
1874 *p = 0;
1877 /* Print debugging trace information if -dg switch is given,
1878 showing the information on which the allocation decisions are based. */
1880 static void
1881 dump_conflicts (file)
1882 FILE *file;
1884 int i;
1885 int has_preferences;
1886 int nregs;
1887 nregs = 0;
1888 for (i = 0; i < max_allocno; i++)
1890 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1891 continue;
1892 nregs++;
1894 fprintf (file, ";; %d regs to allocate:", nregs);
1895 for (i = 0; i < max_allocno; i++)
1897 int j;
1898 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1899 continue;
1900 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1901 for (j = 0; j < max_regno; j++)
1902 if (reg_allocno[j] == allocno_order[i]
1903 && j != allocno[allocno_order[i]].reg)
1904 fprintf (file, "+%d", j);
1905 if (allocno[allocno_order[i]].size != 1)
1906 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1908 fprintf (file, "\n");
1910 for (i = 0; i < max_allocno; i++)
1912 int j;
1913 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1914 for (j = 0; j < max_allocno; j++)
1915 if (CONFLICTP (j, i))
1916 fprintf (file, " %d", allocno[j].reg);
1917 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1918 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1919 fprintf (file, " %d", j);
1920 fprintf (file, "\n");
1922 has_preferences = 0;
1923 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1924 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1925 has_preferences = 1;
1927 if (! has_preferences)
1928 continue;
1929 fprintf (file, ";; %d preferences:", allocno[i].reg);
1930 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1931 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1932 fprintf (file, " %d", j);
1933 fprintf (file, "\n");
1935 fprintf (file, "\n");
1938 void
1939 dump_global_regs (file)
1940 FILE *file;
1942 int i, j;
1944 fprintf (file, ";; Register dispositions:\n");
1945 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1946 if (reg_renumber[i] >= 0)
1948 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1949 if (++j % 6 == 0)
1950 fprintf (file, "\n");
1953 fprintf (file, "\n\n;; Hard regs used: ");
1954 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1955 if (regs_ever_live[i])
1956 fprintf (file, " %d", i);
1957 fprintf (file, "\n\n");