2004-12-15 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / global.c
blob86448171ea26e62963261391b9b4c86d45ac1ff2
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, 2004 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"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "output.h"
38 #include "toplev.h"
40 /* This pass of the compiler performs global register allocation.
41 It assigns hard register numbers to all the pseudo registers
42 that were not handled in local_alloc. Assignments are recorded
43 in the vector reg_renumber, not by changing the rtl code.
44 (Such changes are made by final). The entry point is
45 the function global_alloc.
47 After allocation is complete, the reload pass is run as a subroutine
48 of this pass, so that when a pseudo reg loses its hard reg due to
49 spilling it is possible to make a second attempt to find a hard
50 reg for it. The reload pass is independent in other respects
51 and it is run even when stupid register allocation is in use.
53 1. Assign allocation-numbers (allocnos) to the pseudo-registers
54 still needing allocations and to the pseudo-registers currently
55 allocated by local-alloc which may be spilled by reload.
56 Set up tables reg_allocno and allocno_reg to map
57 reg numbers to allocnos and vice versa.
58 max_allocno gets the number of allocnos in use.
60 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
61 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
62 for conflicts between allocnos and explicit hard register use
63 (which includes use of pseudo-registers allocated by local_alloc).
65 3. For each basic block
66 walk forward through the block, recording which
67 pseudo-registers and which hardware registers are live.
68 Build the conflict matrix between the pseudo-registers
69 and another of pseudo-registers versus hardware registers.
70 Also record the preferred hardware registers
71 for each pseudo-register.
73 4. Sort a table of the allocnos into order of
74 desirability of the variables.
76 5. Allocate the variables in that order; each if possible into
77 a preferred register, else into another register. */
79 /* Number of pseudo-registers which are candidates for allocation. */
81 static int max_allocno;
83 /* Indexed by (pseudo) reg number, gives the allocno, or -1
84 for pseudo registers which are not to be allocated. */
86 static int *reg_allocno;
88 struct allocno
90 int reg;
91 /* Gives the number of consecutive hard registers needed by that
92 pseudo reg. */
93 int size;
95 /* Number of calls crossed by each allocno. */
96 int calls_crossed;
98 /* Number of refs to each allocno. */
99 int n_refs;
101 /* Frequency of uses of each allocno. */
102 int freq;
104 /* Guess at live length of each allocno.
105 This is actually the max of the live lengths of the regs. */
106 int live_length;
108 /* Set of hard regs conflicting with allocno N. */
110 HARD_REG_SET hard_reg_conflicts;
112 /* Set of hard regs preferred by allocno N.
113 This is used to make allocnos go into regs that are copied to or from them,
114 when possible, to reduce register shuffling. */
116 HARD_REG_SET hard_reg_preferences;
118 /* Similar, but just counts register preferences made in simple copy
119 operations, rather than arithmetic. These are given priority because
120 we can always eliminate an insn by using these, but using a register
121 in the above list won't always eliminate an insn. */
123 HARD_REG_SET hard_reg_copy_preferences;
125 /* Similar to hard_reg_preferences, but includes bits for subsequent
126 registers when an allocno is multi-word. The above variable is used for
127 allocation while this is used to build reg_someone_prefers, below. */
129 HARD_REG_SET hard_reg_full_preferences;
131 /* Set of hard registers that some later allocno has a preference for. */
133 HARD_REG_SET regs_someone_prefers;
135 #ifdef STACK_REGS
136 /* Set to true if allocno can't be allocated in the stack register. */
137 bool no_stack_reg;
138 #endif
141 static struct allocno *allocno;
143 /* A vector of the integers from 0 to max_allocno-1,
144 sorted in the order of first-to-be-allocated first. */
146 static int *allocno_order;
148 /* Indexed by (pseudo) reg number, gives the number of another
149 lower-numbered pseudo reg which can share a hard reg with this pseudo
150 *even if the two pseudos would otherwise appear to conflict*. */
152 static int *reg_may_share;
154 /* Define the number of bits in each element of `conflicts' and what
155 type that element has. We use the largest integer format on the
156 host machine. */
158 #define INT_BITS HOST_BITS_PER_WIDE_INT
159 #define INT_TYPE HOST_WIDE_INT
161 /* max_allocno by max_allocno array of bits,
162 recording whether two allocno's conflict (can't go in the same
163 hardware register).
165 `conflicts' is symmetric after the call to mirror_conflicts. */
167 static INT_TYPE *conflicts;
169 /* Number of ints require to hold max_allocno bits.
170 This is the length of a row in `conflicts'. */
172 static int allocno_row_words;
174 /* Two macros to test or store 1 in an element of `conflicts'. */
176 #define CONFLICTP(I, J) \
177 (conflicts[(I) * allocno_row_words + (unsigned) (J) / INT_BITS] \
178 & ((INT_TYPE) 1 << ((unsigned) (J) % INT_BITS)))
180 /* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
181 and execute CODE. */
182 #define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
183 do { \
184 int i_; \
185 int allocno_; \
186 INT_TYPE *p_ = (ALLOCNO_SET); \
188 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
189 i_--, allocno_ += INT_BITS) \
191 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
193 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
195 if (word_ & 1) \
196 {CODE;} \
199 } while (0)
201 /* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
202 #if 0
203 /* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
204 the conflicting allocno, and execute CODE. This macro assumes that
205 mirror_conflicts has been run. */
206 #define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
207 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
208 OUT_ALLOCNO, (CODE))
209 #endif
211 /* Set of hard regs currently live (during scan of all insns). */
213 static HARD_REG_SET hard_regs_live;
215 /* Set of registers that global-alloc isn't supposed to use. */
217 static HARD_REG_SET no_global_alloc_regs;
219 /* Set of registers used so far. */
221 static HARD_REG_SET regs_used_so_far;
223 /* Number of refs to each hard reg, as used by local alloc.
224 It is zero for a reg that contains global pseudos or is explicitly used. */
226 static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
228 /* Frequency of uses of given hard reg. */
229 static int local_reg_freq[FIRST_PSEUDO_REGISTER];
231 /* Guess at live length of each hard reg, as used by local alloc.
232 This is actually the sum of the live lengths of the specific regs. */
234 static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
236 /* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
237 element I, and hard register number J. */
239 #define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
241 /* Bit mask for allocnos live at current point in the scan. */
243 static INT_TYPE *allocnos_live;
245 /* Test, set or clear bit number I in allocnos_live,
246 a bit vector indexed by allocno. */
248 #define SET_ALLOCNO_LIVE(I) \
249 (allocnos_live[(unsigned) (I) / INT_BITS] \
250 |= ((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
252 #define CLEAR_ALLOCNO_LIVE(I) \
253 (allocnos_live[(unsigned) (I) / INT_BITS] \
254 &= ~((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
256 /* This is turned off because it doesn't work right for DImode.
257 (And it is only used for DImode, so the other cases are worthless.)
258 The problem is that it isn't true that there is NO possibility of conflict;
259 only that there is no conflict if the two pseudos get the exact same regs.
260 If they were allocated with a partial overlap, there would be a conflict.
261 We can't safely turn off the conflict unless we have another way to
262 prevent the partial overlap.
264 Idea: change hard_reg_conflicts so that instead of recording which
265 hard regs the allocno may not overlap, it records where the allocno
266 may not start. Change both where it is used and where it is updated.
267 Then there is a way to record that (reg:DI 108) may start at 10
268 but not at 9 or 11. There is still the question of how to record
269 this semi-conflict between two pseudos. */
270 #if 0
271 /* Reg pairs for which conflict after the current insn
272 is inhibited by a REG_NO_CONFLICT note.
273 If the table gets full, we ignore any other notes--that is conservative. */
274 #define NUM_NO_CONFLICT_PAIRS 4
275 /* Number of pairs in use in this insn. */
276 int n_no_conflict_pairs;
277 static struct { int allocno1, allocno2;}
278 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
279 #endif /* 0 */
281 /* Record all regs that are set in any one insn.
282 Communication from mark_reg_{store,clobber} and global_conflicts. */
284 static rtx *regs_set;
285 static int n_regs_set;
287 /* All registers that can be eliminated. */
289 static HARD_REG_SET eliminable_regset;
291 static int allocno_compare (const void *, const void *);
292 static void global_conflicts (void);
293 static void mirror_conflicts (void);
294 static void expand_preferences (void);
295 static void prune_preferences (void);
296 static void find_reg (int, HARD_REG_SET, int, int, int);
297 static void record_one_conflict (int);
298 static void record_conflicts (int *, int);
299 static void mark_reg_store (rtx, rtx, void *);
300 static void mark_reg_clobber (rtx, rtx, void *);
301 static void mark_reg_conflicts (rtx);
302 static void mark_reg_death (rtx);
303 static void mark_reg_live_nc (int, enum machine_mode);
304 static void set_preference (rtx, rtx);
305 static void dump_conflicts (FILE *);
306 static void reg_becomes_live (rtx, rtx, void *);
307 static void reg_dies (int, enum machine_mode, struct insn_chain *);
309 static void allocate_bb_info (void);
310 static void free_bb_info (void);
311 static bool check_earlyclobber (rtx);
312 static bool regclass_intersect (enum reg_class, enum reg_class);
313 static void mark_reg_use_for_earlyclobber_1 (rtx *, void *);
314 static int mark_reg_use_for_earlyclobber (rtx *, void *);
315 static void calculate_local_reg_bb_info (void);
316 static void set_up_bb_rts_numbers (void);
317 static int rpost_cmp (const void *, const void *);
318 static void calculate_reg_pav (void);
319 static void modify_reg_pav (void);
320 static void make_accurate_live_analysis (void);
324 /* Perform allocation of pseudo-registers not allocated by local_alloc.
325 FILE is a file to output debugging information on,
326 or zero if such output is not desired.
328 Return value is nonzero if reload failed
329 and we must not do any more for this function. */
332 global_alloc (FILE *file)
334 int retval;
335 #ifdef ELIMINABLE_REGS
336 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
337 #endif
338 int need_fp
339 = (! flag_omit_frame_pointer
340 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
341 || FRAME_POINTER_REQUIRED);
343 size_t i;
344 rtx x;
346 make_accurate_live_analysis ();
348 max_allocno = 0;
350 /* A machine may have certain hard registers that
351 are safe to use only within a basic block. */
353 CLEAR_HARD_REG_SET (no_global_alloc_regs);
355 /* Build the regset of all eliminable registers and show we can't use those
356 that we already know won't be eliminated. */
357 #ifdef ELIMINABLE_REGS
358 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
360 bool cannot_elim
361 = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
362 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
364 if (!regs_asm_clobbered[eliminables[i].from])
366 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
368 if (cannot_elim)
369 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
371 else if (cannot_elim)
372 error ("%s cannot be used in asm here",
373 reg_names[eliminables[i].from]);
374 else
375 regs_ever_live[eliminables[i].from] = 1;
377 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
378 if (!regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
380 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
381 if (need_fp)
382 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
384 else if (need_fp)
385 error ("%s cannot be used in asm here",
386 reg_names[HARD_FRAME_POINTER_REGNUM]);
387 else
388 regs_ever_live[HARD_FRAME_POINTER_REGNUM] = 1;
389 #endif
391 #else
392 if (!regs_asm_clobbered[FRAME_POINTER_REGNUM])
394 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
395 if (need_fp)
396 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
398 else if (need_fp)
399 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
400 else
401 regs_ever_live[FRAME_POINTER_REGNUM] = 1;
402 #endif
404 /* Track which registers have already been used. Start with registers
405 explicitly in the rtl, then registers allocated by local register
406 allocation. */
408 CLEAR_HARD_REG_SET (regs_used_so_far);
409 #ifdef LEAF_REGISTERS
410 /* If we are doing the leaf function optimization, and this is a leaf
411 function, it means that the registers that take work to save are those
412 that need a register window. So prefer the ones that can be used in
413 a leaf function. */
415 const char *cheap_regs;
416 const char *const leaf_regs = LEAF_REGISTERS;
418 if (only_leaf_regs_used () && leaf_function_p ())
419 cheap_regs = leaf_regs;
420 else
421 cheap_regs = call_used_regs;
422 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
423 if (regs_ever_live[i] || cheap_regs[i])
424 SET_HARD_REG_BIT (regs_used_so_far, i);
426 #else
427 /* We consider registers that do not have to be saved over calls as if
428 they were already used since there is no cost in using them. */
429 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
430 if (regs_ever_live[i] || call_used_regs[i])
431 SET_HARD_REG_BIT (regs_used_so_far, i);
432 #endif
434 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
435 if (reg_renumber[i] >= 0)
436 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
438 /* Establish mappings from register number to allocation number
439 and vice versa. In the process, count the allocnos. */
441 reg_allocno = xmalloc (max_regno * sizeof (int));
443 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
444 reg_allocno[i] = -1;
446 /* Initialize the shared-hard-reg mapping
447 from the list of pairs that may share. */
448 reg_may_share = xcalloc (max_regno, sizeof (int));
449 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
451 int r1 = REGNO (XEXP (x, 0));
452 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
453 if (r1 > r2)
454 reg_may_share[r1] = r2;
455 else
456 reg_may_share[r2] = r1;
459 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
460 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
461 that we are supposed to refrain from putting in a hard reg.
462 -2 means do make an allocno but don't allocate it. */
463 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
464 /* Don't allocate pseudos that cross calls,
465 if this function receives a nonlocal goto. */
466 && (! current_function_has_nonlocal_label
467 || REG_N_CALLS_CROSSED (i) == 0))
469 if (reg_renumber[i] < 0
470 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
471 reg_allocno[i] = reg_allocno[reg_may_share[i]];
472 else
473 reg_allocno[i] = max_allocno++;
474 gcc_assert (REG_LIVE_LENGTH (i));
476 else
477 reg_allocno[i] = -1;
479 allocno = xcalloc (max_allocno, sizeof (struct allocno));
481 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
482 if (reg_allocno[i] >= 0)
484 int num = reg_allocno[i];
485 allocno[num].reg = i;
486 allocno[num].size = PSEUDO_REGNO_SIZE (i);
487 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
488 allocno[num].n_refs += REG_N_REFS (i);
489 allocno[num].freq += REG_FREQ (i);
490 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
491 allocno[num].live_length = REG_LIVE_LENGTH (i);
494 /* Calculate amount of usage of each hard reg by pseudos
495 allocated by local-alloc. This is to see if we want to
496 override it. */
497 memset (local_reg_live_length, 0, sizeof local_reg_live_length);
498 memset (local_reg_n_refs, 0, sizeof local_reg_n_refs);
499 memset (local_reg_freq, 0, sizeof local_reg_freq);
500 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
501 if (reg_renumber[i] >= 0)
503 int regno = reg_renumber[i];
504 int endregno = regno + hard_regno_nregs[regno][PSEUDO_REGNO_MODE (i)];
505 int j;
507 for (j = regno; j < endregno; j++)
509 local_reg_n_refs[j] += REG_N_REFS (i);
510 local_reg_freq[j] += REG_FREQ (i);
511 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
515 /* We can't override local-alloc for a reg used not just by local-alloc. */
516 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
517 if (regs_ever_live[i])
518 local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
520 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
522 /* We used to use alloca here, but the size of what it would try to
523 allocate would occasionally cause it to exceed the stack limit and
524 cause unpredictable core dumps. Some examples were > 2Mb in size. */
525 conflicts = xcalloc (max_allocno * allocno_row_words, sizeof (INT_TYPE));
527 allocnos_live = xmalloc (allocno_row_words * sizeof (INT_TYPE));
529 /* If there is work to be done (at least one reg to allocate),
530 perform global conflict analysis and allocate the regs. */
532 if (max_allocno > 0)
534 /* Scan all the insns and compute the conflicts among allocnos
535 and between allocnos and hard regs. */
537 global_conflicts ();
539 mirror_conflicts ();
541 /* Eliminate conflicts between pseudos and eliminable registers. If
542 the register is not eliminated, the pseudo won't really be able to
543 live in the eliminable register, so the conflict doesn't matter.
544 If we do eliminate the register, the conflict will no longer exist.
545 So in either case, we can ignore the conflict. Likewise for
546 preferences. */
548 for (i = 0; i < (size_t) max_allocno; i++)
550 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
551 eliminable_regset);
552 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
553 eliminable_regset);
554 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
555 eliminable_regset);
558 /* Try to expand the preferences by merging them between allocnos. */
560 expand_preferences ();
562 /* Determine the order to allocate the remaining pseudo registers. */
564 allocno_order = xmalloc (max_allocno * sizeof (int));
565 for (i = 0; i < (size_t) max_allocno; i++)
566 allocno_order[i] = i;
568 /* Default the size to 1, since allocno_compare uses it to divide by.
569 Also convert allocno_live_length of zero to -1. A length of zero
570 can occur when all the registers for that allocno have reg_live_length
571 equal to -2. In this case, we want to make an allocno, but not
572 allocate it. So avoid the divide-by-zero and set it to a low
573 priority. */
575 for (i = 0; i < (size_t) max_allocno; i++)
577 if (allocno[i].size == 0)
578 allocno[i].size = 1;
579 if (allocno[i].live_length == 0)
580 allocno[i].live_length = -1;
583 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
585 prune_preferences ();
587 if (file)
588 dump_conflicts (file);
590 /* Try allocating them, one by one, in that order,
591 except for parameters marked with reg_live_length[regno] == -2. */
593 for (i = 0; i < (size_t) max_allocno; i++)
594 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
595 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
597 /* If we have more than one register class,
598 first try allocating in the class that is cheapest
599 for this pseudo-reg. If that fails, try any reg. */
600 if (N_REG_CLASSES > 1)
602 find_reg (allocno_order[i], 0, 0, 0, 0);
603 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
604 continue;
606 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
607 find_reg (allocno_order[i], 0, 1, 0, 0);
610 free (allocno_order);
613 /* Do the reloads now while the allocno data still exist, so that we can
614 try to assign new hard regs to any pseudo regs that are spilled. */
616 #if 0 /* We need to eliminate regs even if there is no rtl code,
617 for the sake of debugging information. */
618 if (n_basic_blocks > 0)
619 #endif
621 build_insn_chain (get_insns ());
622 retval = reload (get_insns (), 1);
625 /* Clean up. */
626 free (reg_allocno);
627 free (reg_may_share);
628 free (allocno);
629 free (conflicts);
630 free (allocnos_live);
632 return retval;
635 /* Sort predicate for ordering the allocnos.
636 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
638 static int
639 allocno_compare (const void *v1p, const void *v2p)
641 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
642 /* Note that the quotient will never be bigger than
643 the value of floor_log2 times the maximum number of
644 times a register can occur in one insn (surely less than 100)
645 weighted by the frequency (maximally REG_FREQ_MAX).
646 Multiplying this by 10000/REG_FREQ_MAX can't overflow. */
647 int pri1
648 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
649 / allocno[v1].live_length)
650 * (10000 / REG_FREQ_MAX) * allocno[v1].size);
651 int pri2
652 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
653 / allocno[v2].live_length)
654 * (10000 / REG_FREQ_MAX) * allocno[v2].size);
655 if (pri2 - pri1)
656 return pri2 - pri1;
658 /* If regs are equally good, sort by allocno,
659 so that the results of qsort leave nothing to chance. */
660 return v1 - v2;
663 /* Scan the rtl code and record all conflicts and register preferences in the
664 conflict matrices and preference tables. */
666 static void
667 global_conflicts (void)
669 unsigned i;
670 basic_block b;
671 rtx insn;
672 int *block_start_allocnos;
674 /* Make a vector that mark_reg_{store,clobber} will store in. */
675 regs_set = xmalloc (max_parallel * sizeof (rtx) * 2);
677 block_start_allocnos = xmalloc (max_allocno * sizeof (int));
679 FOR_EACH_BB (b)
681 memset (allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
683 /* Initialize table of registers currently live
684 to the state at the beginning of this basic block.
685 This also marks the conflicts among hard registers
686 and any allocnos that are live.
688 For pseudo-regs, there is only one bit for each one
689 no matter how many hard regs it occupies.
690 This is ok; we know the size from PSEUDO_REGNO_SIZE.
691 For explicit hard regs, we cannot know the size that way
692 since one hard reg can be used with various sizes.
693 Therefore, we must require that all the hard regs
694 implicitly live as part of a multi-word hard reg
695 be explicitly marked in basic_block_live_at_start. */
698 regset old = b->global_live_at_start;
699 int ax = 0;
700 reg_set_iterator rsi;
702 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
703 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i, rsi)
705 int a = reg_allocno[i];
706 if (a >= 0)
708 SET_ALLOCNO_LIVE (a);
709 block_start_allocnos[ax++] = a;
711 else if ((a = reg_renumber[i]) >= 0)
712 mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
715 /* Record that each allocno now live conflicts with each hard reg
716 now live.
718 It is not necessary to mark any conflicts between pseudos as
719 this point, even for pseudos which are live at the start of
720 the basic block.
722 Given two pseudos X and Y and any point in the CFG P.
724 On any path to point P where X and Y are live one of the
725 following conditions must be true:
727 1. X is live at some instruction on the path that
728 evaluates Y.
730 2. Y is live at some instruction on the path that
731 evaluates X.
733 3. Either X or Y is not evaluated on the path to P
734 (i.e. it is used uninitialized) and thus the
735 conflict can be ignored.
737 In cases #1 and #2 the conflict will be recorded when we
738 scan the instruction that makes either X or Y become live. */
739 record_conflicts (block_start_allocnos, ax);
741 /* Pseudos can't go in stack regs at the start of a basic block that
742 is reached by an abnormal edge. Likewise for call clobbered regs,
743 because because caller-save, fixup_abnormal_edges, and possibly
744 the table driven EH machinery are not quite ready to handle such
745 regs live across such edges. */
747 edge e;
748 edge_iterator ei;
750 FOR_EACH_EDGE (e, ei, b->preds)
751 if (e->flags & EDGE_ABNORMAL)
752 break;
754 if (e != NULL)
756 #ifdef STACK_REGS
757 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, ax,
759 allocno[ax].no_stack_reg = 1;
761 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
762 record_one_conflict (ax);
763 #endif
765 /* No need to record conflicts for call clobbered regs if we have
766 nonlocal labels around, as we don't ever try to allocate such
767 regs in this case. */
768 if (! current_function_has_nonlocal_label)
769 for (ax = 0; ax < FIRST_PSEUDO_REGISTER; ax++)
770 if (call_used_regs [ax])
771 record_one_conflict (ax);
776 insn = BB_HEAD (b);
778 /* Scan the code of this basic block, noting which allocnos
779 and hard regs are born or die. When one is born,
780 record a conflict with all others currently live. */
782 while (1)
784 RTX_CODE code = GET_CODE (insn);
785 rtx link;
787 /* Make regs_set an empty set. */
789 n_regs_set = 0;
791 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
794 #if 0
795 int i = 0;
796 for (link = REG_NOTES (insn);
797 link && i < NUM_NO_CONFLICT_PAIRS;
798 link = XEXP (link, 1))
799 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
801 no_conflict_pairs[i].allocno1
802 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
803 no_conflict_pairs[i].allocno2
804 = reg_allocno[REGNO (XEXP (link, 0))];
805 i++;
807 #endif /* 0 */
809 /* Mark any registers clobbered by INSN as live,
810 so they conflict with the inputs. */
812 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
814 /* Mark any registers dead after INSN as dead now. */
816 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
817 if (REG_NOTE_KIND (link) == REG_DEAD)
818 mark_reg_death (XEXP (link, 0));
820 /* Mark any registers set in INSN as live,
821 and mark them as conflicting with all other live regs.
822 Clobbers are processed again, so they conflict with
823 the registers that are set. */
825 note_stores (PATTERN (insn), mark_reg_store, NULL);
827 #ifdef AUTO_INC_DEC
828 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
829 if (REG_NOTE_KIND (link) == REG_INC)
830 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
831 #endif
833 /* If INSN has multiple outputs, then any reg that dies here
834 and is used inside of an output
835 must conflict with the other outputs.
837 It is unsafe to use !single_set here since it will ignore an
838 unused output. Just because an output is unused does not mean
839 the compiler can assume the side effect will not occur.
840 Consider if REG appears in the address of an output and we
841 reload the output. If we allocate REG to the same hard
842 register as an unused output we could set the hard register
843 before the output reload insn. */
844 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
845 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
846 if (REG_NOTE_KIND (link) == REG_DEAD)
848 int used_in_output = 0;
849 int i;
850 rtx reg = XEXP (link, 0);
852 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
854 rtx set = XVECEXP (PATTERN (insn), 0, i);
855 if (GET_CODE (set) == SET
856 && !REG_P (SET_DEST (set))
857 && !rtx_equal_p (reg, SET_DEST (set))
858 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
859 used_in_output = 1;
861 if (used_in_output)
862 mark_reg_conflicts (reg);
865 /* Mark any registers set in INSN and then never used. */
867 while (n_regs_set-- > 0)
869 rtx note = find_regno_note (insn, REG_UNUSED,
870 REGNO (regs_set[n_regs_set]));
871 if (note)
872 mark_reg_death (XEXP (note, 0));
876 if (insn == BB_END (b))
877 break;
878 insn = NEXT_INSN (insn);
882 /* Clean up. */
883 free (block_start_allocnos);
884 free (regs_set);
886 /* Expand the preference information by looking for cases where one allocno
887 dies in an insn that sets an allocno. If those two allocnos don't conflict,
888 merge any preferences between those allocnos. */
890 static void
891 expand_preferences (void)
893 rtx insn;
894 rtx link;
895 rtx set;
897 /* We only try to handle the most common cases here. Most of the cases
898 where this wins are reg-reg copies. */
900 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
901 if (INSN_P (insn)
902 && (set = single_set (insn)) != 0
903 && REG_P (SET_DEST (set))
904 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
905 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
906 if (REG_NOTE_KIND (link) == REG_DEAD
907 && REG_P (XEXP (link, 0))
908 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
909 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
910 reg_allocno[REGNO (XEXP (link, 0))]))
912 int a1 = reg_allocno[REGNO (SET_DEST (set))];
913 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
915 if (XEXP (link, 0) == SET_SRC (set))
917 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
918 allocno[a2].hard_reg_copy_preferences);
919 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
920 allocno[a1].hard_reg_copy_preferences);
923 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
924 allocno[a2].hard_reg_preferences);
925 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
926 allocno[a1].hard_reg_preferences);
927 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
928 allocno[a2].hard_reg_full_preferences);
929 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
930 allocno[a1].hard_reg_full_preferences);
934 /* Prune the preferences for global registers to exclude registers that cannot
935 be used.
937 Compute `regs_someone_prefers', which is a bitmask of the hard registers
938 that are preferred by conflicting registers of lower priority. If possible,
939 we will avoid using these registers. */
941 static void
942 prune_preferences (void)
944 int i;
945 int num;
946 int *allocno_to_order = xmalloc (max_allocno * sizeof (int));
948 /* Scan least most important to most important.
949 For each allocno, remove from preferences registers that cannot be used,
950 either because of conflicts or register type. Then compute all registers
951 preferred by each lower-priority register that conflicts. */
953 for (i = max_allocno - 1; i >= 0; i--)
955 HARD_REG_SET temp;
957 num = allocno_order[i];
958 allocno_to_order[num] = i;
959 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
961 if (allocno[num].calls_crossed == 0)
962 IOR_HARD_REG_SET (temp, fixed_reg_set);
963 else
964 IOR_HARD_REG_SET (temp, call_used_reg_set);
966 IOR_COMPL_HARD_REG_SET
967 (temp,
968 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
970 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
971 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
972 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
975 for (i = max_allocno - 1; i >= 0; i--)
977 /* Merge in the preferences of lower-priority registers (they have
978 already been pruned). If we also prefer some of those registers,
979 don't exclude them unless we are of a smaller size (in which case
980 we want to give the lower-priority allocno the first chance for
981 these registers). */
982 HARD_REG_SET temp, temp2;
983 int allocno2;
985 num = allocno_order[i];
987 CLEAR_HARD_REG_SET (temp);
988 CLEAR_HARD_REG_SET (temp2);
990 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
991 allocno2,
993 if (allocno_to_order[allocno2] > i)
995 if (allocno[allocno2].size <= allocno[num].size)
996 IOR_HARD_REG_SET (temp,
997 allocno[allocno2].hard_reg_full_preferences);
998 else
999 IOR_HARD_REG_SET (temp2,
1000 allocno[allocno2].hard_reg_full_preferences);
1004 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
1005 IOR_HARD_REG_SET (temp, temp2);
1006 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
1008 free (allocno_to_order);
1011 /* Assign a hard register to allocno NUM; look for one that is the beginning
1012 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
1013 The registers marked in PREFREGS are tried first.
1015 LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
1016 be used for this allocation.
1018 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
1019 Otherwise ignore that preferred class and use the alternate class.
1021 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
1022 will have to be saved and restored at calls.
1024 RETRYING is nonzero if this is called from retry_global_alloc.
1026 If we find one, record it in reg_renumber.
1027 If not, do nothing. */
1029 static void
1030 find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
1032 int i, best_reg, pass;
1033 HARD_REG_SET used, used1, used2;
1035 enum reg_class class = (alt_regs_p
1036 ? reg_alternate_class (allocno[num].reg)
1037 : reg_preferred_class (allocno[num].reg));
1038 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
1040 if (accept_call_clobbered)
1041 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
1042 else if (allocno[num].calls_crossed == 0)
1043 COPY_HARD_REG_SET (used1, fixed_reg_set);
1044 else
1045 COPY_HARD_REG_SET (used1, call_used_reg_set);
1047 /* Some registers should not be allocated in global-alloc. */
1048 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1049 if (losers)
1050 IOR_HARD_REG_SET (used1, losers);
1052 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1053 COPY_HARD_REG_SET (used2, used1);
1055 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
1057 #ifdef CANNOT_CHANGE_MODE_CLASS
1058 cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
1059 #endif
1061 /* Try each hard reg to see if it fits. Do this in two passes.
1062 In the first pass, skip registers that are preferred by some other pseudo
1063 to give it a better chance of getting one of those registers. Only if
1064 we can't get a register when excluding those do we take one of them.
1065 However, we never allocate a register for the first time in pass 0. */
1067 COPY_HARD_REG_SET (used, used1);
1068 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
1069 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
1071 best_reg = -1;
1072 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1073 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1074 pass++)
1076 if (pass == 1)
1077 COPY_HARD_REG_SET (used, used1);
1078 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1080 #ifdef REG_ALLOC_ORDER
1081 int regno = reg_alloc_order[i];
1082 #else
1083 int regno = i;
1084 #endif
1085 if (! TEST_HARD_REG_BIT (used, regno)
1086 && HARD_REGNO_MODE_OK (regno, mode)
1087 && (allocno[num].calls_crossed == 0
1088 || accept_call_clobbered
1089 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
1091 int j;
1092 int lim = regno + hard_regno_nregs[regno][mode];
1093 for (j = regno + 1;
1094 (j < lim
1095 && ! TEST_HARD_REG_BIT (used, j));
1096 j++);
1097 if (j == lim)
1099 best_reg = regno;
1100 break;
1102 #ifndef REG_ALLOC_ORDER
1103 i = j; /* Skip starting points we know will lose */
1104 #endif
1109 /* See if there is a preferred register with the same class as the register
1110 we allocated above. Making this restriction prevents register
1111 preferencing from creating worse register allocation.
1113 Remove from the preferred registers and conflicting registers. Note that
1114 additional conflicts may have been added after `prune_preferences' was
1115 called.
1117 First do this for those register with copy preferences, then all
1118 preferred registers. */
1120 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1121 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
1122 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1124 if (best_reg >= 0)
1126 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1127 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
1128 && HARD_REGNO_MODE_OK (i, mode)
1129 && (allocno[num].calls_crossed == 0
1130 || accept_call_clobbered
1131 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1132 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1133 || reg_class_subset_p (REGNO_REG_CLASS (i),
1134 REGNO_REG_CLASS (best_reg))
1135 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1136 REGNO_REG_CLASS (i))))
1138 int j;
1139 int lim = i + hard_regno_nregs[i][mode];
1140 for (j = i + 1;
1141 (j < lim
1142 && ! TEST_HARD_REG_BIT (used, j)
1143 && (REGNO_REG_CLASS (j)
1144 == REGNO_REG_CLASS (best_reg + (j - i))
1145 || reg_class_subset_p (REGNO_REG_CLASS (j),
1146 REGNO_REG_CLASS (best_reg + (j - i)))
1147 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1148 REGNO_REG_CLASS (j))));
1149 j++);
1150 if (j == lim)
1152 best_reg = i;
1153 goto no_prefs;
1157 no_copy_prefs:
1159 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1160 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
1161 reg_class_contents[(int) NO_REGS], no_prefs);
1163 if (best_reg >= 0)
1165 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1166 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
1167 && HARD_REGNO_MODE_OK (i, mode)
1168 && (allocno[num].calls_crossed == 0
1169 || accept_call_clobbered
1170 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
1171 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1172 || reg_class_subset_p (REGNO_REG_CLASS (i),
1173 REGNO_REG_CLASS (best_reg))
1174 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1175 REGNO_REG_CLASS (i))))
1177 int j;
1178 int lim = i + hard_regno_nregs[i][mode];
1179 for (j = i + 1;
1180 (j < lim
1181 && ! TEST_HARD_REG_BIT (used, j)
1182 && (REGNO_REG_CLASS (j)
1183 == REGNO_REG_CLASS (best_reg + (j - i))
1184 || reg_class_subset_p (REGNO_REG_CLASS (j),
1185 REGNO_REG_CLASS (best_reg + (j - i)))
1186 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1187 REGNO_REG_CLASS (j))));
1188 j++);
1189 if (j == lim)
1191 best_reg = i;
1192 break;
1196 no_prefs:
1198 /* If we haven't succeeded yet, try with caller-saves.
1199 We need not check to see if the current function has nonlocal
1200 labels because we don't put any pseudos that are live over calls in
1201 registers in that case. */
1203 if (flag_caller_saves && best_reg < 0)
1205 /* Did not find a register. If it would be profitable to
1206 allocate a call-clobbered register and save and restore it
1207 around calls, do that. */
1208 if (! accept_call_clobbered
1209 && allocno[num].calls_crossed != 0
1210 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1211 allocno[num].calls_crossed))
1213 HARD_REG_SET new_losers;
1214 if (! losers)
1215 CLEAR_HARD_REG_SET (new_losers);
1216 else
1217 COPY_HARD_REG_SET (new_losers, losers);
1219 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1220 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1221 if (reg_renumber[allocno[num].reg] >= 0)
1223 caller_save_needed = 1;
1224 return;
1229 /* If we haven't succeeded yet,
1230 see if some hard reg that conflicts with us
1231 was utilized poorly by local-alloc.
1232 If so, kick out the regs that were put there by local-alloc
1233 so we can use it instead. */
1234 if (best_reg < 0 && !retrying
1235 /* Let's not bother with multi-reg allocnos. */
1236 && allocno[num].size == 1)
1238 /* Count from the end, to find the least-used ones first. */
1239 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1241 #ifdef REG_ALLOC_ORDER
1242 int regno = reg_alloc_order[i];
1243 #else
1244 int regno = i;
1245 #endif
1247 if (local_reg_n_refs[regno] != 0
1248 /* Don't use a reg no good for this pseudo. */
1249 && ! TEST_HARD_REG_BIT (used2, regno)
1250 && HARD_REGNO_MODE_OK (regno, mode)
1251 /* The code below assumes that we need only a single
1252 register, but the check of allocno[num].size above
1253 was not enough. Sometimes we need more than one
1254 register for a single-word value. */
1255 && hard_regno_nregs[regno][mode] == 1
1256 && (allocno[num].calls_crossed == 0
1257 || accept_call_clobbered
1258 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1259 #ifdef CANNOT_CHANGE_MODE_CLASS
1260 && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1261 mode)
1262 #endif
1263 #ifdef STACK_REGS
1264 && (!allocno[num].no_stack_reg
1265 || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
1266 #endif
1269 /* We explicitly evaluate the divide results into temporary
1270 variables so as to avoid excess precision problems that occur
1271 on an i386-unknown-sysv4.2 (unixware) host. */
1273 double tmp1 = ((double) local_reg_freq[regno]
1274 / local_reg_live_length[regno]);
1275 double tmp2 = ((double) allocno[num].freq
1276 / allocno[num].live_length);
1278 if (tmp1 < tmp2)
1280 /* Hard reg REGNO was used less in total by local regs
1281 than it would be used by this one allocno! */
1282 int k;
1283 for (k = 0; k < max_regno; k++)
1284 if (reg_renumber[k] >= 0)
1286 int r = reg_renumber[k];
1287 int endregno
1288 = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (k)];
1290 if (regno >= r && regno < endregno)
1291 reg_renumber[k] = -1;
1294 best_reg = regno;
1295 break;
1301 /* Did we find a register? */
1303 if (best_reg >= 0)
1305 int lim, j;
1306 HARD_REG_SET this_reg;
1308 /* Yes. Record it as the hard register of this pseudo-reg. */
1309 reg_renumber[allocno[num].reg] = best_reg;
1310 /* Also of any pseudo-regs that share with it. */
1311 if (reg_may_share[allocno[num].reg])
1312 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1313 if (reg_allocno[j] == num)
1314 reg_renumber[j] = best_reg;
1316 /* Make a set of the hard regs being allocated. */
1317 CLEAR_HARD_REG_SET (this_reg);
1318 lim = best_reg + hard_regno_nregs[best_reg][mode];
1319 for (j = best_reg; j < lim; j++)
1321 SET_HARD_REG_BIT (this_reg, j);
1322 SET_HARD_REG_BIT (regs_used_so_far, j);
1323 /* This is no longer a reg used just by local regs. */
1324 local_reg_n_refs[j] = 0;
1325 local_reg_freq[j] = 0;
1327 /* For each other pseudo-reg conflicting with this one,
1328 mark it as conflicting with the hard regs this one occupies. */
1329 lim = num;
1330 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
1332 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
1337 /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1338 Perhaps it had previously seemed not worth a hard reg,
1339 or perhaps its old hard reg has been commandeered for reloads.
1340 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1341 they do not appear to be allocated.
1342 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1344 void
1345 retry_global_alloc (int regno, HARD_REG_SET forbidden_regs)
1347 int alloc_no = reg_allocno[regno];
1348 if (alloc_no >= 0)
1350 /* If we have more than one register class,
1351 first try allocating in the class that is cheapest
1352 for this pseudo-reg. If that fails, try any reg. */
1353 if (N_REG_CLASSES > 1)
1354 find_reg (alloc_no, forbidden_regs, 0, 0, 1);
1355 if (reg_renumber[regno] < 0
1356 && reg_alternate_class (regno) != NO_REGS)
1357 find_reg (alloc_no, forbidden_regs, 1, 0, 1);
1359 /* If we found a register, modify the RTL for the register to
1360 show the hard register, and mark that register live. */
1361 if (reg_renumber[regno] >= 0)
1363 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1364 mark_home_live (regno);
1369 /* Record a conflict between register REGNO
1370 and everything currently live.
1371 REGNO must not be a pseudo reg that was allocated
1372 by local_alloc; such numbers must be translated through
1373 reg_renumber before calling here. */
1375 static void
1376 record_one_conflict (int regno)
1378 int j;
1380 if (regno < FIRST_PSEUDO_REGISTER)
1381 /* When a hard register becomes live,
1382 record conflicts with live pseudo regs. */
1383 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
1385 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
1387 else
1388 /* When a pseudo-register becomes live,
1389 record conflicts first with hard regs,
1390 then with other pseudo regs. */
1392 int ialloc = reg_allocno[regno];
1393 int ialloc_prod = ialloc * allocno_row_words;
1395 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
1396 for (j = allocno_row_words - 1; j >= 0; j--)
1397 conflicts[ialloc_prod + j] |= allocnos_live[j];
1401 /* Record all allocnos currently live as conflicting
1402 with all hard regs currently live.
1404 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1405 are currently live. Their bits are also flagged in allocnos_live. */
1407 static void
1408 record_conflicts (int *allocno_vec, int len)
1410 while (--len >= 0)
1411 IOR_HARD_REG_SET (allocno[allocno_vec[len]].hard_reg_conflicts,
1412 hard_regs_live);
1415 /* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1416 static void
1417 mirror_conflicts (void)
1419 int i, j;
1420 int rw = allocno_row_words;
1421 int rwb = rw * INT_BITS;
1422 INT_TYPE *p = conflicts;
1423 INT_TYPE *q0 = conflicts, *q1, *q2;
1424 unsigned INT_TYPE mask;
1426 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1428 if (! mask)
1430 mask = 1;
1431 q0++;
1433 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1435 unsigned INT_TYPE word;
1437 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1438 word >>= 1, q2 += rw)
1440 if (word & 1)
1441 *q2 |= mask;
1447 /* Handle the case where REG is set by the insn being scanned,
1448 during the forward scan to accumulate conflicts.
1449 Store a 1 in regs_live or allocnos_live for this register, record how many
1450 consecutive hardware registers it actually needs,
1451 and record a conflict with all other registers already live.
1453 Note that even if REG does not remain alive after this insn,
1454 we must mark it here as live, to ensure a conflict between
1455 REG and any other regs set in this insn that really do live.
1456 This is because those other regs could be considered after this.
1458 REG might actually be something other than a register;
1459 if so, we do nothing.
1461 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1462 a REG_INC note was found for it). */
1464 static void
1465 mark_reg_store (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
1467 int regno;
1469 if (GET_CODE (reg) == SUBREG)
1470 reg = SUBREG_REG (reg);
1472 if (!REG_P (reg))
1473 return;
1475 regs_set[n_regs_set++] = reg;
1477 if (setter && GET_CODE (setter) != CLOBBER)
1478 set_preference (reg, SET_SRC (setter));
1480 regno = REGNO (reg);
1482 /* Either this is one of the max_allocno pseudo regs not allocated,
1483 or it is or has a hardware reg. First handle the pseudo-regs. */
1484 if (regno >= FIRST_PSEUDO_REGISTER)
1486 if (reg_allocno[regno] >= 0)
1488 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1489 record_one_conflict (regno);
1493 if (reg_renumber[regno] >= 0)
1494 regno = reg_renumber[regno];
1496 /* Handle hardware regs (and pseudos allocated to hard regs). */
1497 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1499 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1500 while (regno < last)
1502 record_one_conflict (regno);
1503 SET_HARD_REG_BIT (hard_regs_live, regno);
1504 regno++;
1509 /* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1511 static void
1512 mark_reg_clobber (rtx reg, rtx setter, void *data)
1514 if (GET_CODE (setter) == CLOBBER)
1515 mark_reg_store (reg, setter, data);
1518 /* Record that REG has conflicts with all the regs currently live.
1519 Do not mark REG itself as live. */
1521 static void
1522 mark_reg_conflicts (rtx reg)
1524 int regno;
1526 if (GET_CODE (reg) == SUBREG)
1527 reg = SUBREG_REG (reg);
1529 if (!REG_P (reg))
1530 return;
1532 regno = REGNO (reg);
1534 /* Either this is one of the max_allocno pseudo regs not allocated,
1535 or it is or has a hardware reg. First handle the pseudo-regs. */
1536 if (regno >= FIRST_PSEUDO_REGISTER)
1538 if (reg_allocno[regno] >= 0)
1539 record_one_conflict (regno);
1542 if (reg_renumber[regno] >= 0)
1543 regno = reg_renumber[regno];
1545 /* Handle hardware regs (and pseudos allocated to hard regs). */
1546 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1548 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1549 while (regno < last)
1551 record_one_conflict (regno);
1552 regno++;
1557 /* Mark REG as being dead (following the insn being scanned now).
1558 Store a 0 in regs_live or allocnos_live for this register. */
1560 static void
1561 mark_reg_death (rtx reg)
1563 int regno = REGNO (reg);
1565 /* Either this is one of the max_allocno pseudo regs not allocated,
1566 or it is a hardware reg. First handle the pseudo-regs. */
1567 if (regno >= FIRST_PSEUDO_REGISTER)
1569 if (reg_allocno[regno] >= 0)
1570 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1573 /* For pseudo reg, see if it has been assigned a hardware reg. */
1574 if (reg_renumber[regno] >= 0)
1575 regno = reg_renumber[regno];
1577 /* Handle hardware regs (and pseudos allocated to hard regs). */
1578 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
1580 /* Pseudo regs already assigned hardware regs are treated
1581 almost the same as explicit hardware regs. */
1582 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
1583 while (regno < last)
1585 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1586 regno++;
1591 /* Mark hard reg REGNO as currently live, assuming machine mode MODE
1592 for the value stored in it. MODE determines how many consecutive
1593 registers are actually in use. Do not record conflicts;
1594 it is assumed that the caller will do that. */
1596 static void
1597 mark_reg_live_nc (int regno, enum machine_mode mode)
1599 int last = regno + hard_regno_nregs[regno][mode];
1600 while (regno < last)
1602 SET_HARD_REG_BIT (hard_regs_live, regno);
1603 regno++;
1607 /* Try to set a preference for an allocno to a hard register.
1608 We are passed DEST and SRC which are the operands of a SET. It is known
1609 that SRC is a register. If SRC or the first operand of SRC is a register,
1610 try to set a preference. If one of the two is a hard register and the other
1611 is a pseudo-register, mark the preference.
1613 Note that we are not as aggressive as local-alloc in trying to tie a
1614 pseudo-register to a hard register. */
1616 static void
1617 set_preference (rtx dest, rtx src)
1619 unsigned int src_regno, dest_regno;
1620 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1621 to compensate for subregs in SRC or DEST. */
1622 int offset = 0;
1623 unsigned int i;
1624 int copy = 1;
1626 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1627 src = XEXP (src, 0), copy = 0;
1629 /* Get the reg number for both SRC and DEST.
1630 If neither is a reg, give up. */
1632 if (REG_P (src))
1633 src_regno = REGNO (src);
1634 else if (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src)))
1636 src_regno = REGNO (SUBREG_REG (src));
1638 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1639 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1640 GET_MODE (SUBREG_REG (src)),
1641 SUBREG_BYTE (src),
1642 GET_MODE (src));
1643 else
1644 offset += (SUBREG_BYTE (src)
1645 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
1647 else
1648 return;
1650 if (REG_P (dest))
1651 dest_regno = REGNO (dest);
1652 else if (GET_CODE (dest) == SUBREG && REG_P (SUBREG_REG (dest)))
1654 dest_regno = REGNO (SUBREG_REG (dest));
1656 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1657 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1658 GET_MODE (SUBREG_REG (dest)),
1659 SUBREG_BYTE (dest),
1660 GET_MODE (dest));
1661 else
1662 offset -= (SUBREG_BYTE (dest)
1663 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
1665 else
1666 return;
1668 /* Convert either or both to hard reg numbers. */
1670 if (reg_renumber[src_regno] >= 0)
1671 src_regno = reg_renumber[src_regno];
1673 if (reg_renumber[dest_regno] >= 0)
1674 dest_regno = reg_renumber[dest_regno];
1676 /* Now if one is a hard reg and the other is a global pseudo
1677 then give the other a preference. */
1679 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1680 && reg_allocno[src_regno] >= 0)
1682 dest_regno -= offset;
1683 if (dest_regno < FIRST_PSEUDO_REGISTER)
1685 if (copy)
1686 SET_REGBIT (hard_reg_copy_preferences,
1687 reg_allocno[src_regno], dest_regno);
1689 SET_REGBIT (hard_reg_preferences,
1690 reg_allocno[src_regno], dest_regno);
1691 for (i = dest_regno;
1692 i < dest_regno + hard_regno_nregs[dest_regno][GET_MODE (dest)];
1693 i++)
1694 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1698 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1699 && reg_allocno[dest_regno] >= 0)
1701 src_regno += offset;
1702 if (src_regno < FIRST_PSEUDO_REGISTER)
1704 if (copy)
1705 SET_REGBIT (hard_reg_copy_preferences,
1706 reg_allocno[dest_regno], src_regno);
1708 SET_REGBIT (hard_reg_preferences,
1709 reg_allocno[dest_regno], src_regno);
1710 for (i = src_regno;
1711 i < src_regno + hard_regno_nregs[src_regno][GET_MODE (src)];
1712 i++)
1713 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1718 /* Indicate that hard register number FROM was eliminated and replaced with
1719 an offset from hard register number TO. The status of hard registers live
1720 at the start of a basic block is updated by replacing a use of FROM with
1721 a use of TO. */
1723 void
1724 mark_elimination (int from, int to)
1726 basic_block bb;
1728 FOR_EACH_BB (bb)
1730 regset r = bb->global_live_at_start;
1731 if (REGNO_REG_SET_P (r, from))
1733 CLEAR_REGNO_REG_SET (r, from);
1734 SET_REGNO_REG_SET (r, to);
1739 /* Used for communication between the following functions. Holds the
1740 current life information. */
1741 static regset live_relevant_regs;
1743 /* Record in live_relevant_regs and REGS_SET that register REG became live.
1744 This is called via note_stores. */
1745 static void
1746 reg_becomes_live (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *regs_set)
1748 int regno;
1750 if (GET_CODE (reg) == SUBREG)
1751 reg = SUBREG_REG (reg);
1753 if (!REG_P (reg))
1754 return;
1756 regno = REGNO (reg);
1757 if (regno < FIRST_PSEUDO_REGISTER)
1759 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1760 while (nregs-- > 0)
1762 SET_REGNO_REG_SET (live_relevant_regs, regno);
1763 if (! fixed_regs[regno])
1764 SET_REGNO_REG_SET ((regset) regs_set, regno);
1765 regno++;
1768 else if (reg_renumber[regno] >= 0)
1770 SET_REGNO_REG_SET (live_relevant_regs, regno);
1771 SET_REGNO_REG_SET ((regset) regs_set, regno);
1775 /* Record in live_relevant_regs that register REGNO died. */
1776 static void
1777 reg_dies (int regno, enum machine_mode mode, struct insn_chain *chain)
1779 if (regno < FIRST_PSEUDO_REGISTER)
1781 int nregs = hard_regno_nregs[regno][mode];
1782 while (nregs-- > 0)
1784 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1785 if (! fixed_regs[regno])
1786 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1787 regno++;
1790 else
1792 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1793 if (reg_renumber[regno] >= 0)
1794 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
1798 /* Walk the insns of the current function and build reload_insn_chain,
1799 and record register life information. */
1800 void
1801 build_insn_chain (rtx first)
1803 struct insn_chain **p = &reload_insn_chain;
1804 struct insn_chain *prev = 0;
1805 basic_block b = ENTRY_BLOCK_PTR->next_bb;
1807 live_relevant_regs = ALLOC_REG_SET (&reg_obstack);
1809 for (; first; first = NEXT_INSN (first))
1811 struct insn_chain *c;
1813 if (first == BB_HEAD (b))
1815 unsigned i;
1816 bitmap_iterator bi;
1818 CLEAR_REG_SET (live_relevant_regs);
1820 EXECUTE_IF_SET_IN_BITMAP (b->global_live_at_start, 0, i, bi)
1822 if (i < FIRST_PSEUDO_REGISTER
1823 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1824 : reg_renumber[i] >= 0)
1825 SET_REGNO_REG_SET (live_relevant_regs, i);
1829 if (!NOTE_P (first) && !BARRIER_P (first))
1831 c = new_insn_chain ();
1832 c->prev = prev;
1833 prev = c;
1834 *p = c;
1835 p = &c->next;
1836 c->insn = first;
1837 c->block = b->index;
1839 if (INSN_P (first))
1841 rtx link;
1843 /* Mark the death of everything that dies in this instruction. */
1845 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1846 if (REG_NOTE_KIND (link) == REG_DEAD
1847 && REG_P (XEXP (link, 0)))
1848 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1851 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1853 /* Mark everything born in this instruction as live. */
1855 note_stores (PATTERN (first), reg_becomes_live,
1856 &c->dead_or_set);
1858 else
1859 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
1861 if (INSN_P (first))
1863 rtx link;
1865 /* Mark anything that is set in this insn and then unused as dying. */
1867 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1868 if (REG_NOTE_KIND (link) == REG_UNUSED
1869 && REG_P (XEXP (link, 0)))
1870 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1875 if (first == BB_END (b))
1876 b = b->next_bb;
1878 /* Stop after we pass the end of the last basic block. Verify that
1879 no real insns are after the end of the last basic block.
1881 We may want to reorganize the loop somewhat since this test should
1882 always be the right exit test. Allow an ADDR_VEC or ADDR_DIF_VEC if
1883 the previous real insn is a JUMP_INSN. */
1884 if (b == EXIT_BLOCK_PTR)
1886 #ifdef ENABLE_CHECKING
1887 for (first = NEXT_INSN (first); first; first = NEXT_INSN (first))
1888 gcc_assert (!INSN_P (first)
1889 || GET_CODE (PATTERN (first)) == USE
1890 || ((GET_CODE (PATTERN (first)) == ADDR_VEC
1891 || GET_CODE (PATTERN (first)) == ADDR_DIFF_VEC)
1892 && prev_real_insn (first) != 0
1893 && JUMP_P (prev_real_insn (first))));
1894 #endif
1895 break;
1898 FREE_REG_SET (live_relevant_regs);
1899 *p = 0;
1902 /* Print debugging trace information if -dg switch is given,
1903 showing the information on which the allocation decisions are based. */
1905 static void
1906 dump_conflicts (FILE *file)
1908 int i;
1909 int has_preferences;
1910 int nregs;
1911 nregs = 0;
1912 for (i = 0; i < max_allocno; i++)
1914 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1915 continue;
1916 nregs++;
1918 fprintf (file, ";; %d regs to allocate:", nregs);
1919 for (i = 0; i < max_allocno; i++)
1921 int j;
1922 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
1923 continue;
1924 fprintf (file, " %d", allocno[allocno_order[i]].reg);
1925 for (j = 0; j < max_regno; j++)
1926 if (reg_allocno[j] == allocno_order[i]
1927 && j != allocno[allocno_order[i]].reg)
1928 fprintf (file, "+%d", j);
1929 if (allocno[allocno_order[i]].size != 1)
1930 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
1932 fprintf (file, "\n");
1934 for (i = 0; i < max_allocno; i++)
1936 int j;
1937 fprintf (file, ";; %d conflicts:", allocno[i].reg);
1938 for (j = 0; j < max_allocno; j++)
1939 if (CONFLICTP (j, i))
1940 fprintf (file, " %d", allocno[j].reg);
1941 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1942 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
1943 fprintf (file, " %d", j);
1944 fprintf (file, "\n");
1946 has_preferences = 0;
1947 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1948 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1949 has_preferences = 1;
1951 if (! has_preferences)
1952 continue;
1953 fprintf (file, ";; %d preferences:", allocno[i].reg);
1954 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1955 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
1956 fprintf (file, " %d", j);
1957 fprintf (file, "\n");
1959 fprintf (file, "\n");
1962 void
1963 dump_global_regs (FILE *file)
1965 int i, j;
1967 fprintf (file, ";; Register dispositions:\n");
1968 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1969 if (reg_renumber[i] >= 0)
1971 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1972 if (++j % 6 == 0)
1973 fprintf (file, "\n");
1976 fprintf (file, "\n\n;; Hard regs used: ");
1977 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1978 if (regs_ever_live[i])
1979 fprintf (file, " %d", i);
1980 fprintf (file, "\n\n");
1985 /* This page contains code to make live information more accurate.
1986 The accurate register liveness at program point P means:
1987 o there is a path from P to usage of the register and the
1988 register is not redefined or killed on the path.
1989 o register at P is partially available, i.e. there is a path from
1990 a register definition to the point P and the register is not
1991 killed (clobbered) on the path
1993 The standard GCC live information means only the first condition.
1994 Without the partial availability, there will be more register
1995 conflicts and as a consequence worse register allocation. The
1996 typical example where the information can be different is a
1997 register initialized in the loop at the basic block preceding the
1998 loop in CFG. */
2000 /* The following structure contains basic block data flow information
2001 used to calculate partial availability of registers. */
2003 struct bb_info
2005 /* The basic block reverse post-order number. */
2006 int rts_number;
2007 /* Registers used uninitialized in an insn in which there is an
2008 early clobbered register might get the same hard register. */
2009 bitmap earlyclobber;
2010 /* Registers correspondingly killed (clobbered) and defined but not
2011 killed afterward in the basic block. */
2012 bitmap killed, avloc;
2013 /* Registers partially available and living (in other words whose
2014 values were calclualted and used) correspondingly at the start
2015 and end of the basic block. */
2016 bitmap live_pavin, live_pavout;
2019 /* Macros for accessing data flow information of basic blocks. */
2021 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
2022 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
2024 /* The function allocates the info structures of each basic block. It
2025 also initialized LIVE_PAVIN and LIVE_PAVOUT as if all hard
2026 registers were partially available. */
2028 static void
2029 allocate_bb_info (void)
2031 int i;
2032 basic_block bb;
2033 struct bb_info *bb_info;
2034 bitmap init;
2036 alloc_aux_for_blocks (sizeof (struct bb_info));
2037 init = BITMAP_XMALLOC ();
2038 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2039 bitmap_set_bit (init, i);
2040 FOR_EACH_BB (bb)
2042 bb_info = bb->aux;
2043 bb_info->earlyclobber = BITMAP_XMALLOC ();
2044 bb_info->avloc = BITMAP_XMALLOC ();
2045 bb_info->killed = BITMAP_XMALLOC ();
2046 bb_info->live_pavin = BITMAP_XMALLOC ();
2047 bb_info->live_pavout = BITMAP_XMALLOC ();
2048 bitmap_copy (bb_info->live_pavin, init);
2049 bitmap_copy (bb_info->live_pavout, init);
2051 BITMAP_XFREE (init);
2054 /* The function frees the allocated info of all basic blocks. */
2056 static void
2057 free_bb_info (void)
2059 basic_block bb;
2060 struct bb_info *bb_info;
2062 FOR_EACH_BB (bb)
2064 bb_info = BB_INFO (bb);
2065 BITMAP_XFREE (bb_info->live_pavout);
2066 BITMAP_XFREE (bb_info->live_pavin);
2067 BITMAP_XFREE (bb_info->killed);
2068 BITMAP_XFREE (bb_info->avloc);
2069 BITMAP_XFREE (bb_info->earlyclobber);
2071 free_aux_for_blocks ();
2074 /* The function modifies local info for register REG being changed in
2075 SETTER. DATA is used to pass the current basic block info. */
2077 static void
2078 mark_reg_change (rtx reg, rtx setter, void *data)
2080 int regno;
2081 basic_block bb = data;
2082 struct bb_info *bb_info = BB_INFO (bb);
2084 if (GET_CODE (reg) == SUBREG)
2085 reg = SUBREG_REG (reg);
2087 if (!REG_P (reg))
2088 return;
2090 regno = REGNO (reg);
2091 bitmap_set_bit (bb_info->killed, regno);
2093 if (GET_CODE (setter) != CLOBBER)
2094 bitmap_set_bit (bb_info->avloc, regno);
2095 else
2096 bitmap_clear_bit (bb_info->avloc, regno);
2099 /* Classes of registers which could be early clobbered in the current
2100 insn. */
2102 static varray_type earlyclobber_regclass;
2104 /* This function finds and stores register classes that could be early
2105 clobbered in INSN. If any earlyclobber classes are found, the function
2106 returns TRUE, in all other cases it returns FALSE. */
2108 static bool
2109 check_earlyclobber (rtx insn)
2111 int opno;
2112 bool found = false;
2114 extract_insn (insn);
2116 VARRAY_POP_ALL (earlyclobber_regclass);
2117 for (opno = 0; opno < recog_data.n_operands; opno++)
2119 char c;
2120 bool amp_p;
2121 int i;
2122 enum reg_class class;
2123 const char *p = recog_data.constraints[opno];
2125 class = NO_REGS;
2126 amp_p = false;
2127 for (;;)
2129 c = *p;
2130 switch (c)
2132 case '=': case '+': case '?':
2133 case '#': case '!':
2134 case '*': case '%':
2135 case 'm': case '<': case '>': case 'V': case 'o':
2136 case 'E': case 'F': case 'G': case 'H':
2137 case 's': case 'i': case 'n':
2138 case 'I': case 'J': case 'K': case 'L':
2139 case 'M': case 'N': case 'O': case 'P':
2140 case 'X':
2141 case '0': case '1': case '2': case '3': case '4':
2142 case '5': case '6': case '7': case '8': case '9':
2143 /* These don't say anything we care about. */
2144 break;
2146 case '&':
2147 amp_p = true;
2148 break;
2149 case '\0':
2150 case ',':
2151 if (amp_p && class != NO_REGS)
2153 found = true;
2154 for (i = VARRAY_ACTIVE_SIZE (earlyclobber_regclass) - 1;
2155 i >= 0; i--)
2156 if (VARRAY_INT (earlyclobber_regclass, i) == (int) class)
2157 break;
2158 if (i < 0)
2159 VARRAY_PUSH_INT (earlyclobber_regclass, (int) class);
2162 amp_p = false;
2163 class = NO_REGS;
2164 break;
2166 case 'r':
2167 class = GENERAL_REGS;
2168 break;
2170 default:
2171 class = REG_CLASS_FROM_CONSTRAINT (c, p);
2172 break;
2174 if (c == '\0')
2175 break;
2176 p += CONSTRAINT_LEN (c, p);
2180 return found;
2183 /* The function returns true if register classes C1 and C2 intersect. */
2185 static bool
2186 regclass_intersect (enum reg_class c1, enum reg_class c2)
2188 HARD_REG_SET rs, zero;
2190 CLEAR_HARD_REG_SET (zero);
2191 COPY_HARD_REG_SET(rs, reg_class_contents [c1]);
2192 AND_HARD_REG_SET (rs, reg_class_contents [c2]);
2193 GO_IF_HARD_REG_EQUAL (zero, rs, yes);
2194 return true;
2195 yes:
2196 return false;
2199 /* The function checks that pseudo-register *X has a class
2200 intersecting with the class of pseudo-register could be early
2201 clobbered in the same insn.
2202 This function is a no-op if earlyclobber_regclass is empty. */
2204 static int
2205 mark_reg_use_for_earlyclobber (rtx *x, void *data ATTRIBUTE_UNUSED)
2207 enum reg_class pref_class, alt_class;
2208 int i, regno;
2209 basic_block bb = data;
2210 struct bb_info *bb_info = BB_INFO (bb);
2212 if (GET_CODE (*x) == REG && REGNO (*x) >= FIRST_PSEUDO_REGISTER)
2214 regno = REGNO (*x);
2215 if (bitmap_bit_p (bb_info->killed, regno)
2216 || bitmap_bit_p (bb_info->avloc, regno))
2217 return 0;
2218 pref_class = reg_preferred_class (regno);
2219 alt_class = reg_alternate_class (regno);
2220 for (i = VARRAY_ACTIVE_SIZE (earlyclobber_regclass) - 1; i >= 0; i--)
2221 if (regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2222 pref_class)
2223 || (VARRAY_INT (earlyclobber_regclass, i) != NO_REGS
2224 && regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2225 alt_class)))
2227 bitmap_set_bit (bb_info->earlyclobber, regno);
2228 break;
2231 return 0;
2234 /* The function processes all pseudo-registers in *X with the aid of
2235 previous function. */
2237 static void
2238 mark_reg_use_for_earlyclobber_1 (rtx *x, void *data)
2240 for_each_rtx (x, mark_reg_use_for_earlyclobber, data);
2243 /* The function calculates local info for each basic block. */
2245 static void
2246 calculate_local_reg_bb_info (void)
2248 basic_block bb;
2249 rtx insn, bound;
2251 VARRAY_INT_INIT (earlyclobber_regclass, 20,
2252 "classes of registers early clobbered in an insn");
2253 FOR_EACH_BB (bb)
2255 bound = NEXT_INSN (BB_END (bb));
2256 for (insn = BB_HEAD (bb); insn != bound; insn = NEXT_INSN (insn))
2257 if (INSN_P (insn))
2259 note_stores (PATTERN (insn), mark_reg_change, bb);
2260 if (check_earlyclobber (insn))
2261 note_uses (&PATTERN (insn), mark_reg_use_for_earlyclobber_1, bb);
2266 /* The function sets up reverse post-order number of each basic
2267 block. */
2269 static void
2270 set_up_bb_rts_numbers (void)
2272 int i;
2273 int *rts_order;
2275 rts_order = xmalloc (sizeof (int) * n_basic_blocks);
2276 flow_reverse_top_sort_order_compute (rts_order);
2277 for (i = 0; i < n_basic_blocks; i++)
2278 BB_INFO_BY_INDEX (rts_order [i])->rts_number = i;
2279 free (rts_order);
2282 /* Compare function for sorting blocks in reverse postorder. */
2284 static int
2285 rpost_cmp (const void *bb1, const void *bb2)
2287 basic_block b1 = *(basic_block *) bb1, b2 = *(basic_block *) bb2;
2289 return BB_INFO (b2)->rts_number - BB_INFO (b1)->rts_number;
2292 /* Temporary bitmap used for live_pavin, live_pavout calculation. */
2293 static bitmap temp_bitmap;
2295 /* The function calculates partial register availability according to
2296 the following equations:
2298 bb.live_pavin
2299 = empty for entry block
2300 | union (live_pavout of predecessors) & global_live_at_start
2301 bb.live_pavout = union (bb.live_pavin - bb.killed, bb.avloc)
2302 & global_live_at_end */
2304 static void
2305 calculate_reg_pav (void)
2307 basic_block bb, succ;
2308 edge e;
2309 int i, nel;
2310 varray_type bbs, new_bbs, temp;
2311 basic_block *bb_array;
2312 sbitmap wset;
2314 VARRAY_BB_INIT (bbs, n_basic_blocks, "basic blocks");
2315 VARRAY_BB_INIT (new_bbs, n_basic_blocks, "basic blocks for the next iter.");
2316 temp_bitmap = BITMAP_XMALLOC ();
2317 FOR_EACH_BB (bb)
2319 VARRAY_PUSH_BB (bbs, bb);
2321 wset = sbitmap_alloc (n_basic_blocks + 1);
2322 while (VARRAY_ACTIVE_SIZE (bbs))
2324 bb_array = &VARRAY_BB (bbs, 0);
2325 nel = VARRAY_ACTIVE_SIZE (bbs);
2326 qsort (bb_array, nel, sizeof (basic_block), rpost_cmp);
2327 sbitmap_zero (wset);
2328 for (i = 0; i < nel; i++)
2330 edge_iterator ei;
2331 struct bb_info *bb_info;
2332 bitmap bb_live_pavin, bb_live_pavout;
2334 bb = bb_array [i];
2335 bb_info = BB_INFO (bb);
2336 bb_live_pavin = bb_info->live_pavin;
2337 bb_live_pavout = bb_info->live_pavout;
2338 FOR_EACH_EDGE (e, ei, bb->preds)
2340 basic_block pred = e->src;
2342 if (pred->index != ENTRY_BLOCK)
2343 bitmap_ior_into (bb_live_pavin, BB_INFO (pred)->live_pavout);
2345 bitmap_and_into (bb_live_pavin, bb->global_live_at_start);
2346 bitmap_ior_and_compl (temp_bitmap, bb_info->avloc,
2347 bb_live_pavin, bb_info->killed);
2348 bitmap_and_into (temp_bitmap, bb->global_live_at_end);
2349 if (! bitmap_equal_p (temp_bitmap, bb_live_pavout))
2351 bitmap_copy (bb_live_pavout, temp_bitmap);
2352 FOR_EACH_EDGE (e, ei, bb->succs)
2354 succ = e->dest;
2355 if (succ->index != EXIT_BLOCK
2356 && !TEST_BIT (wset, succ->index))
2358 SET_BIT (wset, succ->index);
2359 VARRAY_PUSH_BB (new_bbs, succ);
2364 temp = bbs;
2365 bbs = new_bbs;
2366 new_bbs = temp;
2367 VARRAY_POP_ALL (new_bbs);
2369 sbitmap_free (wset);
2370 BITMAP_XFREE (temp_bitmap);
2373 /* The function modifies partial availability information for two
2374 special cases to prevent incorrect work of the subsequent passes
2375 with the accurate live information based on the partial
2376 availability. */
2378 static void
2379 modify_reg_pav (void)
2381 basic_block bb;
2382 struct bb_info *bb_info;
2383 #ifdef STACK_REGS
2384 int i;
2385 HARD_REG_SET zero, stack_hard_regs, used;
2386 bitmap stack_regs;
2388 CLEAR_HARD_REG_SET (zero);
2389 CLEAR_HARD_REG_SET (stack_hard_regs);
2390 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
2391 SET_HARD_REG_BIT(stack_hard_regs, i);
2392 stack_regs = BITMAP_XMALLOC ();
2393 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2395 COPY_HARD_REG_SET (used, reg_class_contents[reg_preferred_class (i)]);
2396 IOR_HARD_REG_SET (used, reg_class_contents[reg_alternate_class (i)]);
2397 AND_HARD_REG_SET (used, stack_hard_regs);
2398 GO_IF_HARD_REG_EQUAL(used, zero, skip);
2399 bitmap_set_bit (stack_regs, i);
2400 skip:
2403 #endif
2404 FOR_EACH_BB (bb)
2406 bb_info = BB_INFO (bb);
2408 /* Reload can assign the same hard register to uninitialized
2409 pseudo-register and early clobbered pseudo-register in an
2410 insn if the pseudo-register is used first time in given BB
2411 and not lived at the BB start. To prevent this we don't
2412 change life information for such pseudo-registers. */
2413 bitmap_ior_into (bb_info->live_pavin, bb_info->earlyclobber);
2414 #ifdef STACK_REGS
2415 /* We can not use the same stack register for uninitialized
2416 pseudo-register and another living pseudo-register because if the
2417 uninitialized pseudo-register dies, subsequent pass reg-stack
2418 will be confused (it will believe that the other register
2419 dies). */
2420 bitmap_ior_into (bb_info->live_pavin, stack_regs);
2421 #endif
2423 #ifdef STACK_REGS
2424 BITMAP_XFREE (stack_regs);
2425 #endif
2428 /* The following function makes live information more accurate by
2429 modifying global_live_at_start and global_live_at_end of basic
2430 blocks.
2432 The standard GCC life analysis permits registers to live
2433 uninitialized, for example:
2435 R is never used
2436 .....
2437 Loop:
2438 R is defined
2440 R is used.
2442 With normal life_analysis, R would be live before "Loop:".
2443 The result is that R causes many interferences that do not
2444 serve any purpose.
2446 After the function call a register lives at a program point
2447 only if it is initialized on a path from CFG entry to the
2448 program point. */
2450 static void
2451 make_accurate_live_analysis (void)
2453 basic_block bb;
2454 struct bb_info *bb_info;
2456 max_regno = max_reg_num ();
2457 compact_blocks ();
2458 allocate_bb_info ();
2459 calculate_local_reg_bb_info ();
2460 set_up_bb_rts_numbers ();
2461 calculate_reg_pav ();
2462 modify_reg_pav ();
2463 FOR_EACH_BB (bb)
2465 bb_info = BB_INFO (bb);
2467 bitmap_and_into (bb->global_live_at_start, bb_info->live_pavin);
2468 bitmap_and_into (bb->global_live_at_end, bb_info->live_pavout);
2470 free_bb_info ();