]> gcc.gnu.org Git - gcc.git/blame - gcc/global.c
check.c (gfc_check_getcwd_sub): Fix seg fault.
[gcc.git] / gcc / global.c
CommitLineData
38398762 1/* Allocate registers for pseudo-registers that span basic blocks.
d050d723 2 Copyright (C) 1987, 1988, 1991, 1994, 1996, 1997, 1998,
e146f815 3 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
38398762 4
1322177d 5This file is part of GCC.
38398762 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
38398762 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
38398762
RK
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
38398762
RK
21
22
38398762 23#include "config.h"
670ee920 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
ccd043a9 27
f6781658
L
28#include "machmode.h"
29#include "hard-reg-set.h"
38398762 30#include "rtl.h"
6baf1cc8 31#include "tm_p.h"
38398762
RK
32#include "flags.h"
33#include "basic-block.h"
38398762 34#include "regs.h"
49ad7cfa 35#include "function.h"
38398762 36#include "insn-config.h"
fdbda73f 37#include "recog.h"
cad6f7d0 38#include "reload.h"
38398762 39#include "output.h"
2e107e9e 40#include "toplev.h"
38398762
RK
41
42/* This pass of the compiler performs global register allocation.
43 It assigns hard register numbers to all the pseudo registers
44 that were not handled in local_alloc. Assignments are recorded
45 in the vector reg_renumber, not by changing the rtl code.
46 (Such changes are made by final). The entry point is
47 the function global_alloc.
48
49 After allocation is complete, the reload pass is run as a subroutine
50 of this pass, so that when a pseudo reg loses its hard reg due to
51 spilling it is possible to make a second attempt to find a hard
52 reg for it. The reload pass is independent in other respects
53 and it is run even when stupid register allocation is in use.
54
a300b8d9
JW
55 1. Assign allocation-numbers (allocnos) to the pseudo-registers
56 still needing allocations and to the pseudo-registers currently
57 allocated by local-alloc which may be spilled by reload.
589005ff 58 Set up tables reg_allocno and allocno_reg to map
38398762
RK
59 reg numbers to allocnos and vice versa.
60 max_allocno gets the number of allocnos in use.
61
62 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
63 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
64 for conflicts between allocnos and explicit hard register use
65 (which includes use of pseudo-registers allocated by local_alloc).
66
a300b8d9 67 3. For each basic block
38398762 68 walk forward through the block, recording which
a300b8d9
JW
69 pseudo-registers and which hardware registers are live.
70 Build the conflict matrix between the pseudo-registers
71 and another of pseudo-registers versus hardware registers.
38398762 72 Also record the preferred hardware registers
a300b8d9 73 for each pseudo-register.
38398762
RK
74
75 4. Sort a table of the allocnos into order of
76 desirability of the variables.
77
78 5. Allocate the variables in that order; each if possible into
79 a preferred register, else into another register. */
80\f
dc297297 81/* Number of pseudo-registers which are candidates for allocation. */
38398762
RK
82
83static int max_allocno;
84
85/* Indexed by (pseudo) reg number, gives the allocno, or -1
a300b8d9 86 for pseudo registers which are not to be allocated. */
38398762 87
f15ebf65 88static int *reg_allocno;
38398762 89
5c0ecffe
JH
90struct allocno
91{
92 int reg;
93 /* Gives the number of consecutive hard registers needed by that
94 pseudo reg. */
95 int size;
96
97 /* Number of calls crossed by each allocno. */
98 int calls_crossed;
99
b2aec5c0 100 /* Number of refs to each allocno. */
5c0ecffe
JH
101 int n_refs;
102
b2aec5c0
JH
103 /* Frequency of uses of each allocno. */
104 int freq;
105
5c0ecffe
JH
106 /* Guess at live length of each allocno.
107 This is actually the max of the live lengths of the regs. */
108 int live_length;
109
110 /* Set of hard regs conflicting with allocno N. */
111
112 HARD_REG_SET hard_reg_conflicts;
113
114 /* Set of hard regs preferred by allocno N.
115 This is used to make allocnos go into regs that are copied to or from them,
116 when possible, to reduce register shuffling. */
117
118 HARD_REG_SET hard_reg_preferences;
119
120 /* Similar, but just counts register preferences made in simple copy
121 operations, rather than arithmetic. These are given priority because
122 we can always eliminate an insn by using these, but using a register
123 in the above list won't always eliminate an insn. */
38398762 124
5c0ecffe
JH
125 HARD_REG_SET hard_reg_copy_preferences;
126
127 /* Similar to hard_reg_preferences, but includes bits for subsequent
128 registers when an allocno is multi-word. The above variable is used for
129 allocation while this is used to build reg_someone_prefers, below. */
130
131 HARD_REG_SET hard_reg_full_preferences;
132
133 /* Set of hard registers that some later allocno has a preference for. */
134
135 HARD_REG_SET regs_someone_prefers;
b1a6f8db
JH
136
137#ifdef STACK_REGS
138 /* Set to true if allocno can't be allocated in the stack register. */
139 bool no_stack_reg;
140#endif
5c0ecffe
JH
141};
142
143static struct allocno *allocno;
38398762
RK
144
145/* A vector of the integers from 0 to max_allocno-1,
146 sorted in the order of first-to-be-allocated first. */
147
148static int *allocno_order;
149
38398762 150/* Indexed by (pseudo) reg number, gives the number of another
6dc42e49 151 lower-numbered pseudo reg which can share a hard reg with this pseudo
38398762
RK
152 *even if the two pseudos would otherwise appear to conflict*. */
153
154static int *reg_may_share;
155
b1ec3c92
CH
156/* Define the number of bits in each element of `conflicts' and what
157 type that element has. We use the largest integer format on the
158 host machine. */
159
160#define INT_BITS HOST_BITS_PER_WIDE_INT
161#define INT_TYPE HOST_WIDE_INT
162
38398762
RK
163/* max_allocno by max_allocno array of bits,
164 recording whether two allocno's conflict (can't go in the same
165 hardware register).
166
267cf808 167 `conflicts' is symmetric after the call to mirror_conflicts. */
38398762 168
b1ec3c92 169static INT_TYPE *conflicts;
38398762
RK
170
171/* Number of ints require to hold max_allocno bits.
172 This is the length of a row in `conflicts'. */
173
174static int allocno_row_words;
175
176/* Two macros to test or store 1 in an element of `conflicts'. */
177
178#define CONFLICTP(I, J) \
c4f2c499
KH
179 (conflicts[(I) * allocno_row_words + (unsigned) (J) / INT_BITS] \
180 & ((INT_TYPE) 1 << ((unsigned) (J) % INT_BITS)))
38398762 181
32c8d1bc
R
182/* For any allocno set in ALLOCNO_SET, set ALLOCNO to that allocno,
183 and execute CODE. */
184#define EXECUTE_IF_SET_IN_ALLOCNO_SET(ALLOCNO_SET, ALLOCNO, CODE) \
185do { \
186 int i_; \
187 int allocno_; \
5c0ecffe 188 INT_TYPE *p_ = (ALLOCNO_SET); \
32c8d1bc
R
189 \
190 for (i_ = allocno_row_words - 1, allocno_ = 0; i_ >= 0; \
191 i_--, allocno_ += INT_BITS) \
192 { \
193 unsigned INT_TYPE word_ = (unsigned INT_TYPE) *p_++; \
194 \
195 for ((ALLOCNO) = allocno_; word_; word_ >>= 1, (ALLOCNO)++) \
196 { \
197 if (word_ & 1) \
36013ffc 198 {CODE;} \
32c8d1bc
R
199 } \
200 } \
201} while (0)
202
312618c7
R
203/* This doesn't work for non-GNU C due to the way CODE is macro expanded. */
204#if 0
32c8d1bc
R
205/* For any allocno that conflicts with IN_ALLOCNO, set OUT_ALLOCNO to
206 the conflicting allocno, and execute CODE. This macro assumes that
207 mirror_conflicts has been run. */
208#define EXECUTE_IF_CONFLICT(IN_ALLOCNO, OUT_ALLOCNO, CODE)\
209 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + (IN_ALLOCNO) * allocno_row_words,\
36013ffc 210 OUT_ALLOCNO, (CODE))
312618c7 211#endif
32c8d1bc 212
38398762
RK
213/* Set of hard regs currently live (during scan of all insns). */
214
215static HARD_REG_SET hard_regs_live;
216
38398762
RK
217/* Set of registers that global-alloc isn't supposed to use. */
218
219static HARD_REG_SET no_global_alloc_regs;
220
221/* Set of registers used so far. */
222
223static HARD_REG_SET regs_used_so_far;
224
b2aec5c0 225/* Number of refs to each hard reg, as used by local alloc.
1d56e983
RS
226 It is zero for a reg that contains global pseudos or is explicitly used. */
227
228static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
229
b2aec5c0
JH
230/* Frequency of uses of given hard reg. */
231static int local_reg_freq[FIRST_PSEUDO_REGISTER];
232
1d56e983
RS
233/* Guess at live length of each hard reg, as used by local alloc.
234 This is actually the sum of the live lengths of the specific regs. */
235
236static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
237
060a58c5
NB
238/* Set to 1 a bit in a vector TABLE of HARD_REG_SETs, for vector
239 element I, and hard register number J. */
38398762 240
5c0ecffe 241#define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (allocno[I].TABLE, J)
38398762
RK
242
243/* Bit mask for allocnos live at current point in the scan. */
244
b1ec3c92 245static INT_TYPE *allocnos_live;
38398762
RK
246
247/* Test, set or clear bit number I in allocnos_live,
248 a bit vector indexed by allocno. */
249
32c8d1bc 250#define SET_ALLOCNO_LIVE(I) \
c4f2c499
KH
251 (allocnos_live[(unsigned) (I) / INT_BITS] \
252 |= ((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
38398762 253
32c8d1bc 254#define CLEAR_ALLOCNO_LIVE(I) \
c4f2c499
KH
255 (allocnos_live[(unsigned) (I) / INT_BITS] \
256 &= ~((INT_TYPE) 1 << ((unsigned) (I) % INT_BITS)))
38398762
RK
257
258/* This is turned off because it doesn't work right for DImode.
259 (And it is only used for DImode, so the other cases are worthless.)
260 The problem is that it isn't true that there is NO possibility of conflict;
261 only that there is no conflict if the two pseudos get the exact same regs.
262 If they were allocated with a partial overlap, there would be a conflict.
263 We can't safely turn off the conflict unless we have another way to
264 prevent the partial overlap.
265
266 Idea: change hard_reg_conflicts so that instead of recording which
267 hard regs the allocno may not overlap, it records where the allocno
268 may not start. Change both where it is used and where it is updated.
269 Then there is a way to record that (reg:DI 108) may start at 10
270 but not at 9 or 11. There is still the question of how to record
271 this semi-conflict between two pseudos. */
272#if 0
273/* Reg pairs for which conflict after the current insn
274 is inhibited by a REG_NO_CONFLICT note.
275 If the table gets full, we ignore any other notes--that is conservative. */
276#define NUM_NO_CONFLICT_PAIRS 4
277/* Number of pairs in use in this insn. */
278int n_no_conflict_pairs;
279static struct { int allocno1, allocno2;}
280 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
281#endif /* 0 */
282
283/* Record all regs that are set in any one insn.
284 Communication from mark_reg_{store,clobber} and global_conflicts. */
285
286static rtx *regs_set;
287static int n_regs_set;
288
daf55ac6 289/* All registers that can be eliminated. */
38398762
RK
290
291static HARD_REG_SET eliminable_regset;
292
1d088dee
AJ
293static int allocno_compare (const void *, const void *);
294static void global_conflicts (void);
295static void mirror_conflicts (void);
296static void expand_preferences (void);
297static void prune_preferences (void);
298static void find_reg (int, HARD_REG_SET, int, int, int);
299static void record_one_conflict (int);
300static void record_conflicts (int *, int);
301static void mark_reg_store (rtx, rtx, void *);
302static void mark_reg_clobber (rtx, rtx, void *);
303static void mark_reg_conflicts (rtx);
304static void mark_reg_death (rtx);
305static void mark_reg_live_nc (int, enum machine_mode);
306static void set_preference (rtx, rtx);
307static void dump_conflicts (FILE *);
308static void reg_becomes_live (rtx, rtx, void *);
309static void reg_dies (int, enum machine_mode, struct insn_chain *);
9abe5d07
VM
310
311static void allocate_bb_info (void);
312static void free_bb_info (void);
fdbda73f
VM
313static void check_earlyclobber (rtx);
314static bool regclass_intersect (enum reg_class, enum reg_class);
315static void mark_reg_use_for_earlyclobber_1 (rtx *, void *);
316static int mark_reg_use_for_earlyclobber (rtx *, void *);
9abe5d07
VM
317static void calculate_local_reg_bb_info (void);
318static void set_up_bb_rts_numbers (void);
319static int rpost_cmp (const void *, const void *);
320static bool modify_bb_reg_pav (basic_block, basic_block, bool);
321static void calculate_reg_pav (void);
d6df6ae2 322static void modify_reg_pav (void);
9abe5d07
VM
323static void make_accurate_live_analysis (void);
324
38398762 325\f
9abe5d07 326
38398762
RK
327/* Perform allocation of pseudo-registers not allocated by local_alloc.
328 FILE is a file to output debugging information on,
ab40ad2b 329 or zero if such output is not desired.
38398762 330
ab40ad2b
RS
331 Return value is nonzero if reload failed
332 and we must not do any more for this function. */
333
334int
1d088dee 335global_alloc (FILE *file)
38398762 336{
8c316ae2 337 int retval;
38398762 338#ifdef ELIMINABLE_REGS
8b60264b 339 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
38398762 340#endif
daf55ac6
RK
341 int need_fp
342 = (! flag_omit_frame_pointer
daf55ac6 343 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
daf55ac6
RK
344 || FRAME_POINTER_REQUIRED);
345
b3694847 346 size_t i;
38398762
RK
347 rtx x;
348
9abe5d07
VM
349 make_accurate_live_analysis ();
350
38398762
RK
351 max_allocno = 0;
352
353 /* A machine may have certain hard registers that
354 are safe to use only within a basic block. */
355
356 CLEAR_HARD_REG_SET (no_global_alloc_regs);
38398762
RK
357
358 /* Build the regset of all eliminable registers and show we can't use those
359 that we already know won't be eliminated. */
360#ifdef ELIMINABLE_REGS
b6a1cbae 361 for (i = 0; i < ARRAY_SIZE (eliminables); i++)
38398762 362 {
df2ef49b
AM
363 bool cannot_elim
364 = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
365 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
366
367 if (!regs_asm_clobbered[eliminables[i].from])
368 {
369 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
370
371 if (cannot_elim)
372 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
373 }
374 else if (cannot_elim)
375 error ("%s cannot be used in asm here",
376 reg_names[eliminables[i].from]);
a06f01ba
JW
377 else
378 regs_ever_live[eliminables[i].from] = 1;
38398762 379 }
7b0957a7 380#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
df2ef49b
AM
381 if (!regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
382 {
383 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
384 if (need_fp)
385 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
386 }
387 else if (need_fp)
388 error ("%s cannot be used in asm here",
389 reg_names[HARD_FRAME_POINTER_REGNUM]);
a06f01ba
JW
390 else
391 regs_ever_live[HARD_FRAME_POINTER_REGNUM] = 1;
7b0957a7 392#endif
daf55ac6 393
38398762 394#else
df2ef49b
AM
395 if (!regs_asm_clobbered[FRAME_POINTER_REGNUM])
396 {
397 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
398 if (need_fp)
399 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
400 }
401 else if (need_fp)
402 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
a06f01ba
JW
403 else
404 regs_ever_live[FRAME_POINTER_REGNUM] = 1;
38398762
RK
405#endif
406
407 /* Track which registers have already been used. Start with registers
408 explicitly in the rtl, then registers allocated by local register
b4b4db94 409 allocation. */
38398762
RK
410
411 CLEAR_HARD_REG_SET (regs_used_so_far);
b4b4db94
RS
412#ifdef LEAF_REGISTERS
413 /* If we are doing the leaf function optimization, and this is a leaf
414 function, it means that the registers that take work to save are those
415 that need a register window. So prefer the ones that can be used in
416 a leaf function. */
417 {
e0c32c62
KG
418 const char *cheap_regs;
419 const char *const leaf_regs = LEAF_REGISTERS;
b4b4db94
RS
420
421 if (only_leaf_regs_used () && leaf_function_p ())
422 cheap_regs = leaf_regs;
423 else
424 cheap_regs = call_used_regs;
425 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
426 if (regs_ever_live[i] || cheap_regs[i])
427 SET_HARD_REG_BIT (regs_used_so_far, i);
428 }
429#else
430 /* We consider registers that do not have to be saved over calls as if
431 they were already used since there is no cost in using them. */
38398762
RK
432 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
433 if (regs_ever_live[i] || call_used_regs[i])
434 SET_HARD_REG_BIT (regs_used_so_far, i);
b4b4db94 435#endif
38398762 436
e51712db 437 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
38398762
RK
438 if (reg_renumber[i] >= 0)
439 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
440
441 /* Establish mappings from register number to allocation number
442 and vice versa. In the process, count the allocnos. */
443
703ad42b 444 reg_allocno = xmalloc (max_regno * sizeof (int));
38398762
RK
445
446 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
447 reg_allocno[i] = -1;
448
449 /* Initialize the shared-hard-reg mapping
450 from the list of pairs that may share. */
703ad42b 451 reg_may_share = xcalloc (max_regno, sizeof (int));
38398762
RK
452 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
453 {
454 int r1 = REGNO (XEXP (x, 0));
455 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
456 if (r1 > r2)
457 reg_may_share[r1] = r2;
458 else
459 reg_may_share[r2] = r1;
460 }
461
e51712db 462 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
38398762
RK
463 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
464 that we are supposed to refrain from putting in a hard reg.
465 -2 means do make an allocno but don't allocate it. */
a300b8d9 466 if (REG_N_REFS (i) != 0 && REG_LIVE_LENGTH (i) != -1
38398762
RK
467 /* Don't allocate pseudos that cross calls,
468 if this function receives a nonlocal goto. */
469 && (! current_function_has_nonlocal_label
b1f21e0a 470 || REG_N_CALLS_CROSSED (i) == 0))
38398762 471 {
282899df
NS
472 if (reg_renumber[i] < 0
473 && reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
38398762
RK
474 reg_allocno[i] = reg_allocno[reg_may_share[i]];
475 else
476 reg_allocno[i] = max_allocno++;
282899df 477 gcc_assert (REG_LIVE_LENGTH (i));
38398762
RK
478 }
479 else
480 reg_allocno[i] = -1;
481
703ad42b 482 allocno = xcalloc (max_allocno, sizeof (struct allocno));
38398762 483
e51712db 484 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
38398762
RK
485 if (reg_allocno[i] >= 0)
486 {
5c0ecffe
JH
487 int num = reg_allocno[i];
488 allocno[num].reg = i;
489 allocno[num].size = PSEUDO_REGNO_SIZE (i);
490 allocno[num].calls_crossed += REG_N_CALLS_CROSSED (i);
491 allocno[num].n_refs += REG_N_REFS (i);
b2aec5c0 492 allocno[num].freq += REG_FREQ (i);
5c0ecffe
JH
493 if (allocno[num].live_length < REG_LIVE_LENGTH (i))
494 allocno[num].live_length = REG_LIVE_LENGTH (i);
38398762
RK
495 }
496
1d56e983
RS
497 /* Calculate amount of usage of each hard reg by pseudos
498 allocated by local-alloc. This is to see if we want to
499 override it. */
703ad42b
KG
500 memset (local_reg_live_length, 0, sizeof local_reg_live_length);
501 memset (local_reg_n_refs, 0, sizeof local_reg_n_refs);
502 memset (local_reg_freq, 0, sizeof local_reg_freq);
e51712db 503 for (i = FIRST_PSEUDO_REGISTER; i < (size_t) max_regno; i++)
a300b8d9 504 if (reg_renumber[i] >= 0)
1d56e983 505 {
34e56753 506 int regno = reg_renumber[i];
66fd46b6 507 int endregno = regno + hard_regno_nregs[regno][PSEUDO_REGNO_MODE (i)];
34e56753
RS
508 int j;
509
510 for (j = regno; j < endregno; j++)
511 {
b1f21e0a 512 local_reg_n_refs[j] += REG_N_REFS (i);
b2aec5c0 513 local_reg_freq[j] += REG_FREQ (i);
b1f21e0a 514 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
34e56753 515 }
1d56e983 516 }
34e56753 517
1d56e983
RS
518 /* We can't override local-alloc for a reg used not just by local-alloc. */
519 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
520 if (regs_ever_live[i])
b2aec5c0 521 local_reg_n_refs[i] = 0, local_reg_freq[i] = 0;
589005ff 522
38398762
RK
523 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
524
ba3b3878
BK
525 /* We used to use alloca here, but the size of what it would try to
526 allocate would occasionally cause it to exceed the stack limit and
527 cause unpredictable core dumps. Some examples were > 2Mb in size. */
703ad42b 528 conflicts = xcalloc (max_allocno * allocno_row_words, sizeof (INT_TYPE));
38398762 529
703ad42b 530 allocnos_live = xmalloc (allocno_row_words * sizeof (INT_TYPE));
38398762
RK
531
532 /* If there is work to be done (at least one reg to allocate),
533 perform global conflict analysis and allocate the regs. */
534
535 if (max_allocno > 0)
536 {
537 /* Scan all the insns and compute the conflicts among allocnos
538 and between allocnos and hard regs. */
539
540 global_conflicts ();
541
267cf808
JL
542 mirror_conflicts ();
543
38398762
RK
544 /* Eliminate conflicts between pseudos and eliminable registers. If
545 the register is not eliminated, the pseudo won't really be able to
546 live in the eliminable register, so the conflict doesn't matter.
547 If we do eliminate the register, the conflict will no longer exist.
1d56e983
RS
548 So in either case, we can ignore the conflict. Likewise for
549 preferences. */
38398762 550
e51712db 551 for (i = 0; i < (size_t) max_allocno; i++)
1d56e983 552 {
5c0ecffe
JH
553 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_conflicts,
554 eliminable_regset);
555 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_copy_preferences,
556 eliminable_regset);
557 AND_COMPL_HARD_REG_SET (allocno[i].hard_reg_preferences,
1d56e983 558 eliminable_regset);
1d56e983 559 }
38398762
RK
560
561 /* Try to expand the preferences by merging them between allocnos. */
562
563 expand_preferences ();
564
565 /* Determine the order to allocate the remaining pseudo registers. */
566
703ad42b 567 allocno_order = xmalloc (max_allocno * sizeof (int));
e51712db 568 for (i = 0; i < (size_t) max_allocno; i++)
38398762
RK
569 allocno_order[i] = i;
570
571 /* Default the size to 1, since allocno_compare uses it to divide by.
572 Also convert allocno_live_length of zero to -1. A length of zero
573 can occur when all the registers for that allocno have reg_live_length
574 equal to -2. In this case, we want to make an allocno, but not
575 allocate it. So avoid the divide-by-zero and set it to a low
576 priority. */
577
e51712db 578 for (i = 0; i < (size_t) max_allocno; i++)
38398762 579 {
5c0ecffe
JH
580 if (allocno[i].size == 0)
581 allocno[i].size = 1;
582 if (allocno[i].live_length == 0)
583 allocno[i].live_length = -1;
38398762
RK
584 }
585
586 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
589005ff 587
38398762
RK
588 prune_preferences ();
589
590 if (file)
591 dump_conflicts (file);
592
593 /* Try allocating them, one by one, in that order,
594 except for parameters marked with reg_live_length[regno] == -2. */
595
e51712db 596 for (i = 0; i < (size_t) max_allocno; i++)
5c0ecffe
JH
597 if (reg_renumber[allocno[allocno_order[i]].reg] < 0
598 && REG_LIVE_LENGTH (allocno[allocno_order[i]].reg) >= 0)
38398762
RK
599 {
600 /* If we have more than one register class,
601 first try allocating in the class that is cheapest
602 for this pseudo-reg. If that fails, try any reg. */
603 if (N_REG_CLASSES > 1)
604 {
ea8693a4 605 find_reg (allocno_order[i], 0, 0, 0, 0);
5c0ecffe 606 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
38398762
RK
607 continue;
608 }
5c0ecffe 609 if (reg_alternate_class (allocno[allocno_order[i]].reg) != NO_REGS)
ea8693a4 610 find_reg (allocno_order[i], 0, 1, 0, 0);
38398762 611 }
67289ea6
MM
612
613 free (allocno_order);
38398762
RK
614 }
615
616 /* Do the reloads now while the allocno data still exist, so that we can
617 try to assign new hard regs to any pseudo regs that are spilled. */
618
7e860cf7
RS
619#if 0 /* We need to eliminate regs even if there is no rtl code,
620 for the sake of debugging information. */
0b17ab2f 621 if (n_basic_blocks > 0)
7e860cf7 622#endif
cad6f7d0 623 {
108c535a 624 build_insn_chain (get_insns ());
e04ca094 625 retval = reload (get_insns (), 1);
cad6f7d0 626 }
8c316ae2 627
67289ea6
MM
628 /* Clean up. */
629 free (reg_allocno);
630 free (reg_may_share);
5c0ecffe 631 free (allocno);
8c316ae2 632 free (conflicts);
67289ea6
MM
633 free (allocnos_live);
634
8c316ae2 635 return retval;
38398762
RK
636}
637
638/* Sort predicate for ordering the allocnos.
639 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
640
641static int
1d088dee 642allocno_compare (const void *v1p, const void *v2p)
38398762 643{
ec0ce6e2 644 int v1 = *(const int *)v1p, v2 = *(const int *)v2p;
38398762
RK
645 /* Note that the quotient will never be bigger than
646 the value of floor_log2 times the maximum number of
a08b2604
JH
647 times a register can occur in one insn (surely less than 100)
648 weighted by the frequency (maximally REG_FREQ_MAX).
649 Multiplying this by 10000/REG_FREQ_MAX can't overflow. */
b3694847 650 int pri1
b2aec5c0 651 = (((double) (floor_log2 (allocno[v1].n_refs) * allocno[v1].freq)
5c0ecffe 652 / allocno[v1].live_length)
a08b2604 653 * (10000 / REG_FREQ_MAX) * allocno[v1].size);
b3694847 654 int pri2
b2aec5c0 655 = (((double) (floor_log2 (allocno[v2].n_refs) * allocno[v2].freq)
5c0ecffe 656 / allocno[v2].live_length)
a08b2604 657 * (10000 / REG_FREQ_MAX) * allocno[v2].size);
38398762
RK
658 if (pri2 - pri1)
659 return pri2 - pri1;
660
661 /* If regs are equally good, sort by allocno,
662 so that the results of qsort leave nothing to chance. */
daa6f17d 663 return v1 - v2;
38398762
RK
664}
665\f
666/* Scan the rtl code and record all conflicts and register preferences in the
667 conflict matrices and preference tables. */
668
669static void
1d088dee 670global_conflicts (void)
38398762 671{
3cd8c58a 672 unsigned i;
e0082a72 673 basic_block b;
b3694847 674 rtx insn;
8d4c79be 675 int *block_start_allocnos;
38398762
RK
676
677 /* Make a vector that mark_reg_{store,clobber} will store in. */
703ad42b 678 regs_set = xmalloc (max_parallel * sizeof (rtx) * 2);
38398762 679
703ad42b 680 block_start_allocnos = xmalloc (max_allocno * sizeof (int));
38398762 681
e0082a72 682 FOR_EACH_BB (b)
38398762 683 {
703ad42b 684 memset (allocnos_live, 0, allocno_row_words * sizeof (INT_TYPE));
38398762
RK
685
686 /* Initialize table of registers currently live
687 to the state at the beginning of this basic block.
bdc24974
JL
688 This also marks the conflicts among hard registers
689 and any allocnos that are live.
38398762
RK
690
691 For pseudo-regs, there is only one bit for each one
692 no matter how many hard regs it occupies.
693 This is ok; we know the size from PSEUDO_REGNO_SIZE.
694 For explicit hard regs, we cannot know the size that way
695 since one hard reg can be used with various sizes.
696 Therefore, we must require that all the hard regs
697 implicitly live as part of a multi-word hard reg
3a7c155d 698 be explicitly marked in basic_block_live_at_start. */
38398762
RK
699
700 {
e0082a72 701 regset old = b->global_live_at_start;
38398762 702 int ax = 0;
a2041967 703 reg_set_iterator rsi;
38398762 704
916b1701 705 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
a2041967
KH
706 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i, rsi)
707 {
708 int a = reg_allocno[i];
709 if (a >= 0)
710 {
711 SET_ALLOCNO_LIVE (a);
712 block_start_allocnos[ax++] = a;
713 }
714 else if ((a = reg_renumber[i]) >= 0)
715 mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
716 }
38398762 717
bdc24974
JL
718 /* Record that each allocno now live conflicts with each hard reg
719 now live.
38398762 720
bdc24974
JL
721 It is not necessary to mark any conflicts between pseudos as
722 this point, even for pseudos which are live at the start of
723 the basic block.
724
725 Given two pseudos X and Y and any point in the CFG P.
726
727 On any path to point P where X and Y are live one of the
728 following conditions must be true:
729
730 1. X is live at some instruction on the path that
731 evaluates Y.
732
733 2. Y is live at some instruction on the path that
734 evaluates X.
735
fbe5a4a6 736 3. Either X or Y is not evaluated on the path to P
454ff5cb 737 (i.e. it is used uninitialized) and thus the
bdc24974
JL
738 conflict can be ignored.
739
740 In cases #1 and #2 the conflict will be recorded when we
741 scan the instruction that makes either X or Y become live. */
38398762 742 record_conflicts (block_start_allocnos, ax);
4d1d8045 743
1d088dee
AJ
744 /* Pseudos can't go in stack regs at the start of a basic block that
745 is reached by an abnormal edge. Likewise for call clobbered regs,
746 because because caller-save, fixup_abnormal_edges, and possibly
747 the table driven EH machinery are not quite ready to handle such
748 regs live across such edges. */
e881bb1b 749 {
e881bb1b 750 edge e;
628f6a4e 751 edge_iterator ei;
4147232b 752
628f6a4e 753 FOR_EACH_EDGE (e, ei, b->preds)
e881bb1b
RH
754 if (e->flags & EDGE_ABNORMAL)
755 break;
4147232b 756
e881bb1b 757 if (e != NULL)
b1a6f8db 758 {
4147232b 759#ifdef STACK_REGS
b1a6f8db 760 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, ax,
4147232b
OH
761 {
762 allocno[ax].no_stack_reg = 1;
763 });
b1a6f8db
JH
764 for (ax = FIRST_STACK_REG; ax <= LAST_STACK_REG; ax++)
765 record_one_conflict (ax);
4147232b
OH
766#endif
767
768 /* No need to record conflicts for call clobbered regs if we have
769 nonlocal labels around, as we don't ever try to allocate such
770 regs in this case. */
771 if (! current_function_has_nonlocal_label)
772 for (ax = 0; ax < FIRST_PSEUDO_REGISTER; ax++)
773 if (call_used_regs [ax])
774 record_one_conflict (ax);
b1a6f8db 775 }
e881bb1b 776 }
38398762
RK
777 }
778
a813c111 779 insn = BB_HEAD (b);
38398762
RK
780
781 /* Scan the code of this basic block, noting which allocnos
782 and hard regs are born or die. When one is born,
783 record a conflict with all others currently live. */
784
785 while (1)
786 {
b3694847
SS
787 RTX_CODE code = GET_CODE (insn);
788 rtx link;
38398762
RK
789
790 /* Make regs_set an empty set. */
791
792 n_regs_set = 0;
793
794 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
795 {
38398762
RK
796
797#if 0
2049526b 798 int i = 0;
38398762
RK
799 for (link = REG_NOTES (insn);
800 link && i < NUM_NO_CONFLICT_PAIRS;
801 link = XEXP (link, 1))
802 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
803 {
804 no_conflict_pairs[i].allocno1
805 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
806 no_conflict_pairs[i].allocno2
807 = reg_allocno[REGNO (XEXP (link, 0))];
808 i++;
809 }
810#endif /* 0 */
811
812 /* Mark any registers clobbered by INSN as live,
813 so they conflict with the inputs. */
814
84832317 815 note_stores (PATTERN (insn), mark_reg_clobber, NULL);
38398762
RK
816
817 /* Mark any registers dead after INSN as dead now. */
818
819 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
820 if (REG_NOTE_KIND (link) == REG_DEAD)
821 mark_reg_death (XEXP (link, 0));
822
823 /* Mark any registers set in INSN as live,
824 and mark them as conflicting with all other live regs.
825 Clobbers are processed again, so they conflict with
826 the registers that are set. */
827
84832317 828 note_stores (PATTERN (insn), mark_reg_store, NULL);
38398762
RK
829
830#ifdef AUTO_INC_DEC
831 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
832 if (REG_NOTE_KIND (link) == REG_INC)
84832317 833 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
38398762
RK
834#endif
835
333e0f7d
RS
836 /* If INSN has multiple outputs, then any reg that dies here
837 and is used inside of an output
941c63ac
JL
838 must conflict with the other outputs.
839
840 It is unsafe to use !single_set here since it will ignore an
841 unused output. Just because an output is unused does not mean
842 the compiler can assume the side effect will not occur.
843 Consider if REG appears in the address of an output and we
844 reload the output. If we allocate REG to the same hard
845 register as an unused output we could set the hard register
846 before the output reload insn. */
847 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
333e0f7d
RS
848 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
849 if (REG_NOTE_KIND (link) == REG_DEAD)
850 {
851 int used_in_output = 0;
852 int i;
853 rtx reg = XEXP (link, 0);
854
855 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
856 {
857 rtx set = XVECEXP (PATTERN (insn), 0, i);
858 if (GET_CODE (set) == SET
f8cfc6aa 859 && !REG_P (SET_DEST (set))
333e0f7d
RS
860 && !rtx_equal_p (reg, SET_DEST (set))
861 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
862 used_in_output = 1;
863 }
864 if (used_in_output)
865 mark_reg_conflicts (reg);
866 }
867
38398762
RK
868 /* Mark any registers set in INSN and then never used. */
869
06a3a6db
GK
870 while (n_regs_set-- > 0)
871 {
872 rtx note = find_regno_note (insn, REG_UNUSED,
873 REGNO (regs_set[n_regs_set]));
874 if (note)
875 mark_reg_death (XEXP (note, 0));
876 }
38398762
RK
877 }
878
a813c111 879 if (insn == BB_END (b))
38398762
RK
880 break;
881 insn = NEXT_INSN (insn);
882 }
883 }
67289ea6
MM
884
885 /* Clean up. */
886 free (block_start_allocnos);
887 free (regs_set);
38398762
RK
888}
889/* Expand the preference information by looking for cases where one allocno
890 dies in an insn that sets an allocno. If those two allocnos don't conflict,
891 merge any preferences between those allocnos. */
892
893static void
1d088dee 894expand_preferences (void)
38398762
RK
895{
896 rtx insn;
897 rtx link;
898 rtx set;
899
900 /* We only try to handle the most common cases here. Most of the cases
901 where this wins are reg-reg copies. */
902
903 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2c3c49de 904 if (INSN_P (insn)
38398762 905 && (set = single_set (insn)) != 0
f8cfc6aa 906 && REG_P (SET_DEST (set))
38398762
RK
907 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
908 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
909 if (REG_NOTE_KIND (link) == REG_DEAD
f8cfc6aa 910 && REG_P (XEXP (link, 0))
38398762
RK
911 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
912 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
267cf808 913 reg_allocno[REGNO (XEXP (link, 0))]))
38398762
RK
914 {
915 int a1 = reg_allocno[REGNO (SET_DEST (set))];
916 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
917
918 if (XEXP (link, 0) == SET_SRC (set))
919 {
5c0ecffe
JH
920 IOR_HARD_REG_SET (allocno[a1].hard_reg_copy_preferences,
921 allocno[a2].hard_reg_copy_preferences);
922 IOR_HARD_REG_SET (allocno[a2].hard_reg_copy_preferences,
923 allocno[a1].hard_reg_copy_preferences);
38398762
RK
924 }
925
5c0ecffe
JH
926 IOR_HARD_REG_SET (allocno[a1].hard_reg_preferences,
927 allocno[a2].hard_reg_preferences);
928 IOR_HARD_REG_SET (allocno[a2].hard_reg_preferences,
929 allocno[a1].hard_reg_preferences);
930 IOR_HARD_REG_SET (allocno[a1].hard_reg_full_preferences,
931 allocno[a2].hard_reg_full_preferences);
932 IOR_HARD_REG_SET (allocno[a2].hard_reg_full_preferences,
933 allocno[a1].hard_reg_full_preferences);
38398762
RK
934 }
935}
936\f
937/* Prune the preferences for global registers to exclude registers that cannot
938 be used.
589005ff 939
38398762
RK
940 Compute `regs_someone_prefers', which is a bitmask of the hard registers
941 that are preferred by conflicting registers of lower priority. If possible,
942 we will avoid using these registers. */
589005ff 943
38398762 944static void
1d088dee 945prune_preferences (void)
38398762 946{
32c8d1bc 947 int i;
5c0ecffe 948 int num;
703ad42b 949 int *allocno_to_order = xmalloc (max_allocno * sizeof (int));
589005ff 950
38398762
RK
951 /* Scan least most important to most important.
952 For each allocno, remove from preferences registers that cannot be used,
953 either because of conflicts or register type. Then compute all registers
d45cf215 954 preferred by each lower-priority register that conflicts. */
38398762
RK
955
956 for (i = max_allocno - 1; i >= 0; i--)
957 {
267cf808 958 HARD_REG_SET temp;
38398762 959
5c0ecffe
JH
960 num = allocno_order[i];
961 allocno_to_order[num] = i;
962 COPY_HARD_REG_SET (temp, allocno[num].hard_reg_conflicts);
38398762 963
5c0ecffe 964 if (allocno[num].calls_crossed == 0)
38398762
RK
965 IOR_HARD_REG_SET (temp, fixed_reg_set);
966 else
967 IOR_HARD_REG_SET (temp, call_used_reg_set);
968
969 IOR_COMPL_HARD_REG_SET
970 (temp,
5c0ecffe 971 reg_class_contents[(int) reg_preferred_class (allocno[num].reg)]);
38398762 972
5c0ecffe
JH
973 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, temp);
974 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, temp);
975 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_full_preferences, temp);
267cf808 976 }
38398762 977
267cf808
JL
978 for (i = max_allocno - 1; i >= 0; i--)
979 {
38398762
RK
980 /* Merge in the preferences of lower-priority registers (they have
981 already been pruned). If we also prefer some of those registers,
982 don't exclude them unless we are of a smaller size (in which case
983 we want to give the lower-priority allocno the first chance for
984 these registers). */
267cf808 985 HARD_REG_SET temp, temp2;
32c8d1bc 986 int allocno2;
267cf808 987
5c0ecffe 988 num = allocno_order[i];
267cf808 989
ea1637e9
R
990 CLEAR_HARD_REG_SET (temp);
991 CLEAR_HARD_REG_SET (temp2);
267cf808 992
5c0ecffe 993 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + num * allocno_row_words,
312618c7 994 allocno2,
267cf808 995 {
32c8d1bc 996 if (allocno_to_order[allocno2] > i)
267cf808 997 {
5c0ecffe
JH
998 if (allocno[allocno2].size <= allocno[num].size)
999 IOR_HARD_REG_SET (temp,
1000 allocno[allocno2].hard_reg_full_preferences);
32c8d1bc 1001 else
5c0ecffe
JH
1002 IOR_HARD_REG_SET (temp2,
1003 allocno[allocno2].hard_reg_full_preferences);
267cf808 1004 }
32c8d1bc 1005 });
267cf808 1006
5c0ecffe 1007 AND_COMPL_HARD_REG_SET (temp, allocno[num].hard_reg_full_preferences);
ea1637e9 1008 IOR_HARD_REG_SET (temp, temp2);
5c0ecffe 1009 COPY_HARD_REG_SET (allocno[num].regs_someone_prefers, temp);
38398762 1010 }
267cf808 1011 free (allocno_to_order);
38398762
RK
1012}
1013\f
5c0ecffe 1014/* Assign a hard register to allocno NUM; look for one that is the beginning
38398762
RK
1015 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
1016 The registers marked in PREFREGS are tried first.
1017
cc2902df 1018 LOSERS, if nonzero, is a HARD_REG_SET indicating registers that cannot
38398762
RK
1019 be used for this allocation.
1020
b1ec3c92
CH
1021 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
1022 Otherwise ignore that preferred class and use the alternate class.
38398762
RK
1023
1024 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
1025 will have to be saved and restored at calls.
1026
1d56e983
RS
1027 RETRYING is nonzero if this is called from retry_global_alloc.
1028
38398762
RK
1029 If we find one, record it in reg_renumber.
1030 If not, do nothing. */
1031
1032static void
1d088dee 1033find_reg (int num, HARD_REG_SET losers, int alt_regs_p, int accept_call_clobbered, int retrying)
38398762 1034{
b3694847 1035 int i, best_reg, pass;
cff9f8d5 1036 HARD_REG_SET used, used1, used2;
38398762 1037
b1ec3c92 1038 enum reg_class class = (alt_regs_p
5c0ecffe
JH
1039 ? reg_alternate_class (allocno[num].reg)
1040 : reg_preferred_class (allocno[num].reg));
1041 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno[num].reg);
38398762
RK
1042
1043 if (accept_call_clobbered)
1044 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
5c0ecffe 1045 else if (allocno[num].calls_crossed == 0)
38398762
RK
1046 COPY_HARD_REG_SET (used1, fixed_reg_set);
1047 else
1048 COPY_HARD_REG_SET (used1, call_used_reg_set);
1049
1050 /* Some registers should not be allocated in global-alloc. */
1051 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
1052 if (losers)
1053 IOR_HARD_REG_SET (used1, losers);
1054
1055 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1d56e983
RS
1056 COPY_HARD_REG_SET (used2, used1);
1057
5c0ecffe 1058 IOR_HARD_REG_SET (used1, allocno[num].hard_reg_conflicts);
38398762 1059
cff9f8d5
AH
1060#ifdef CANNOT_CHANGE_MODE_CLASS
1061 cannot_change_mode_set_regs (&used1, mode, allocno[num].reg);
d546b10a
RK
1062#endif
1063
38398762 1064 /* Try each hard reg to see if it fits. Do this in two passes.
d45cf215 1065 In the first pass, skip registers that are preferred by some other pseudo
38398762
RK
1066 to give it a better chance of getting one of those registers. Only if
1067 we can't get a register when excluding those do we take one of them.
1068 However, we never allocate a register for the first time in pass 0. */
1069
1070 COPY_HARD_REG_SET (used, used1);
1071 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
5c0ecffe 1072 IOR_HARD_REG_SET (used, allocno[num].regs_someone_prefers);
589005ff 1073
38398762
RK
1074 best_reg = -1;
1075 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
1076 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
1077 pass++)
1078 {
1079 if (pass == 1)
1080 COPY_HARD_REG_SET (used, used1);
1081 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1082 {
1083#ifdef REG_ALLOC_ORDER
1084 int regno = reg_alloc_order[i];
1085#else
1086 int regno = i;
1087#endif
1088 if (! TEST_HARD_REG_BIT (used, regno)
1e326708 1089 && HARD_REGNO_MODE_OK (regno, mode)
5c0ecffe 1090 && (allocno[num].calls_crossed == 0
1e326708
MH
1091 || accept_call_clobbered
1092 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
38398762 1093 {
b3694847 1094 int j;
66fd46b6 1095 int lim = regno + hard_regno_nregs[regno][mode];
38398762
RK
1096 for (j = regno + 1;
1097 (j < lim
1098 && ! TEST_HARD_REG_BIT (used, j));
1099 j++);
1100 if (j == lim)
1101 {
1102 best_reg = regno;
1103 break;
1104 }
1105#ifndef REG_ALLOC_ORDER
1106 i = j; /* Skip starting points we know will lose */
1107#endif
1108 }
1109 }
1110 }
1111
1112 /* See if there is a preferred register with the same class as the register
1113 we allocated above. Making this restriction prevents register
1114 preferencing from creating worse register allocation.
1115
1116 Remove from the preferred registers and conflicting registers. Note that
1117 additional conflicts may have been added after `prune_preferences' was
589005ff 1118 called.
38398762
RK
1119
1120 First do this for those register with copy preferences, then all
1121 preferred registers. */
1122
5c0ecffe
JH
1123 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_copy_preferences, used);
1124 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_copy_preferences,
38398762
RK
1125 reg_class_contents[(int) NO_REGS], no_copy_prefs);
1126
1127 if (best_reg >= 0)
1128 {
1129 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5c0ecffe 1130 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_copy_preferences, i)
38398762 1131 && HARD_REGNO_MODE_OK (i, mode)
cf11ac55
HB
1132 && (allocno[num].calls_crossed == 0
1133 || accept_call_clobbered
1134 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
38398762
RK
1135 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1136 || reg_class_subset_p (REGNO_REG_CLASS (i),
1137 REGNO_REG_CLASS (best_reg))
1138 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1139 REGNO_REG_CLASS (i))))
1140 {
b3694847 1141 int j;
66fd46b6 1142 int lim = i + hard_regno_nregs[i][mode];
38398762
RK
1143 for (j = i + 1;
1144 (j < lim
1145 && ! TEST_HARD_REG_BIT (used, j)
1146 && (REGNO_REG_CLASS (j)
1d088dee 1147 == REGNO_REG_CLASS (best_reg + (j - i))
38398762
RK
1148 || reg_class_subset_p (REGNO_REG_CLASS (j),
1149 REGNO_REG_CLASS (best_reg + (j - i)))
1150 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1151 REGNO_REG_CLASS (j))));
1152 j++);
1153 if (j == lim)
1154 {
1155 best_reg = i;
1156 goto no_prefs;
1157 }
1158 }
1159 }
1160 no_copy_prefs:
1161
5c0ecffe
JH
1162 AND_COMPL_HARD_REG_SET (allocno[num].hard_reg_preferences, used);
1163 GO_IF_HARD_REG_SUBSET (allocno[num].hard_reg_preferences,
38398762
RK
1164 reg_class_contents[(int) NO_REGS], no_prefs);
1165
1166 if (best_reg >= 0)
1167 {
1168 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5c0ecffe 1169 if (TEST_HARD_REG_BIT (allocno[num].hard_reg_preferences, i)
38398762 1170 && HARD_REGNO_MODE_OK (i, mode)
cf11ac55
HB
1171 && (allocno[num].calls_crossed == 0
1172 || accept_call_clobbered
1173 || ! HARD_REGNO_CALL_PART_CLOBBERED (i, mode))
38398762
RK
1174 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1175 || reg_class_subset_p (REGNO_REG_CLASS (i),
1176 REGNO_REG_CLASS (best_reg))
1177 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1178 REGNO_REG_CLASS (i))))
1179 {
b3694847 1180 int j;
66fd46b6 1181 int lim = i + hard_regno_nregs[i][mode];
38398762
RK
1182 for (j = i + 1;
1183 (j < lim
1184 && ! TEST_HARD_REG_BIT (used, j)
1185 && (REGNO_REG_CLASS (j)
1d088dee 1186 == REGNO_REG_CLASS (best_reg + (j - i))
38398762
RK
1187 || reg_class_subset_p (REGNO_REG_CLASS (j),
1188 REGNO_REG_CLASS (best_reg + (j - i)))
1189 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1190 REGNO_REG_CLASS (j))));
1191 j++);
1192 if (j == lim)
1193 {
1194 best_reg = i;
1195 break;
1196 }
1197 }
1198 }
1199 no_prefs:
1200
589005ff 1201 /* If we haven't succeeded yet, try with caller-saves.
cfcf04a6
RK
1202 We need not check to see if the current function has nonlocal
1203 labels because we don't put any pseudos that are live over calls in
1204 registers in that case. */
1205
1d56e983
RS
1206 if (flag_caller_saves && best_reg < 0)
1207 {
1208 /* Did not find a register. If it would be profitable to
1209 allocate a call-clobbered register and save and restore it
1210 around calls, do that. */
1211 if (! accept_call_clobbered
5c0ecffe
JH
1212 && allocno[num].calls_crossed != 0
1213 && CALLER_SAVE_PROFITABLE (allocno[num].n_refs,
1214 allocno[num].calls_crossed))
1d56e983 1215 {
6cad67d2
JL
1216 HARD_REG_SET new_losers;
1217 if (! losers)
1218 CLEAR_HARD_REG_SET (new_losers);
1219 else
1220 COPY_HARD_REG_SET (new_losers, losers);
589005ff 1221
6cad67d2 1222 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
5c0ecffe
JH
1223 find_reg (num, new_losers, alt_regs_p, 1, retrying);
1224 if (reg_renumber[allocno[num].reg] >= 0)
1d56e983
RS
1225 {
1226 caller_save_needed = 1;
1227 return;
1228 }
1229 }
1230 }
1231
1232 /* If we haven't succeeded yet,
1233 see if some hard reg that conflicts with us
1234 was utilized poorly by local-alloc.
1235 If so, kick out the regs that were put there by local-alloc
1236 so we can use it instead. */
1237 if (best_reg < 0 && !retrying
1238 /* Let's not bother with multi-reg allocnos. */
5c0ecffe 1239 && allocno[num].size == 1)
1d56e983
RS
1240 {
1241 /* Count from the end, to find the least-used ones first. */
1242 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
17a0a76d
RK
1243 {
1244#ifdef REG_ALLOC_ORDER
1245 int regno = reg_alloc_order[i];
1246#else
1247 int regno = i;
1248#endif
34e56753 1249
17a0a76d
RK
1250 if (local_reg_n_refs[regno] != 0
1251 /* Don't use a reg no good for this pseudo. */
1252 && ! TEST_HARD_REG_BIT (used2, regno)
d546b10a 1253 && HARD_REGNO_MODE_OK (regno, mode)
3748bd9e
RS
1254 /* The code below assumes that we need only a single
1255 register, but the check of allocno[num].size above
1256 was not enough. Sometimes we need more than one
1257 register for a single-word value. */
66fd46b6 1258 && hard_regno_nregs[regno][mode] == 1
cf11ac55
HB
1259 && (allocno[num].calls_crossed == 0
1260 || accept_call_clobbered
1261 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
cff9f8d5
AH
1262#ifdef CANNOT_CHANGE_MODE_CLASS
1263 && ! invalid_mode_change_p (regno, REGNO_REG_CLASS (regno),
1264 mode)
b1a6f8db
JH
1265#endif
1266#ifdef STACK_REGS
1267 && (!allocno[num].no_stack_reg
1268 || regno < FIRST_STACK_REG || regno > LAST_STACK_REG)
d546b10a
RK
1269#endif
1270 )
17a0a76d 1271 {
7a17c588
JW
1272 /* We explicitly evaluate the divide results into temporary
1273 variables so as to avoid excess precision problems that occur
4fe9b91c 1274 on an i386-unknown-sysv4.2 (unixware) host. */
589005ff 1275
b2aec5c0 1276 double tmp1 = ((double) local_reg_freq[regno]
7a17c588 1277 / local_reg_live_length[regno]);
b2aec5c0 1278 double tmp2 = ((double) allocno[num].freq
5c0ecffe 1279 / allocno[num].live_length);
7a17c588
JW
1280
1281 if (tmp1 < tmp2)
1282 {
1283 /* Hard reg REGNO was used less in total by local regs
1284 than it would be used by this one allocno! */
1285 int k;
1286 for (k = 0; k < max_regno; k++)
1287 if (reg_renumber[k] >= 0)
1288 {
1289 int r = reg_renumber[k];
1290 int endregno
66fd46b6 1291 = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (k)];
34e56753 1292
7a17c588
JW
1293 if (regno >= r && regno < endregno)
1294 reg_renumber[k] = -1;
1295 }
17a0a76d 1296
7a17c588
JW
1297 best_reg = regno;
1298 break;
1299 }
17a0a76d
RK
1300 }
1301 }
1d56e983
RS
1302 }
1303
38398762
RK
1304 /* Did we find a register? */
1305
1306 if (best_reg >= 0)
1307 {
b3694847 1308 int lim, j;
38398762
RK
1309 HARD_REG_SET this_reg;
1310
1311 /* Yes. Record it as the hard register of this pseudo-reg. */
5c0ecffe 1312 reg_renumber[allocno[num].reg] = best_reg;
38398762 1313 /* Also of any pseudo-regs that share with it. */
5c0ecffe 1314 if (reg_may_share[allocno[num].reg])
38398762 1315 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
5c0ecffe 1316 if (reg_allocno[j] == num)
38398762
RK
1317 reg_renumber[j] = best_reg;
1318
1319 /* Make a set of the hard regs being allocated. */
1320 CLEAR_HARD_REG_SET (this_reg);
66fd46b6 1321 lim = best_reg + hard_regno_nregs[best_reg][mode];
38398762
RK
1322 for (j = best_reg; j < lim; j++)
1323 {
1324 SET_HARD_REG_BIT (this_reg, j);
1325 SET_HARD_REG_BIT (regs_used_so_far, j);
1d56e983
RS
1326 /* This is no longer a reg used just by local regs. */
1327 local_reg_n_refs[j] = 0;
b2aec5c0 1328 local_reg_freq[j] = 0;
38398762
RK
1329 }
1330 /* For each other pseudo-reg conflicting with this one,
1331 mark it as conflicting with the hard regs this one occupies. */
5c0ecffe 1332 lim = num;
312618c7 1333 EXECUTE_IF_SET_IN_ALLOCNO_SET (conflicts + lim * allocno_row_words, j,
32c8d1bc 1334 {
5c0ecffe 1335 IOR_HARD_REG_SET (allocno[j].hard_reg_conflicts, this_reg);
32c8d1bc 1336 });
38398762 1337 }
38398762
RK
1338}
1339\f
1340/* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1341 Perhaps it had previously seemed not worth a hard reg,
1342 or perhaps its old hard reg has been commandeered for reloads.
1343 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1344 they do not appear to be allocated.
1345 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1346
1347void
1d088dee 1348retry_global_alloc (int regno, HARD_REG_SET forbidden_regs)
38398762 1349{
35e17f7e
FS
1350 int alloc_no = reg_allocno[regno];
1351 if (alloc_no >= 0)
38398762
RK
1352 {
1353 /* If we have more than one register class,
1354 first try allocating in the class that is cheapest
1355 for this pseudo-reg. If that fails, try any reg. */
1356 if (N_REG_CLASSES > 1)
35e17f7e 1357 find_reg (alloc_no, forbidden_regs, 0, 0, 1);
38398762 1358 if (reg_renumber[regno] < 0
b1ec3c92 1359 && reg_alternate_class (regno) != NO_REGS)
35e17f7e 1360 find_reg (alloc_no, forbidden_regs, 1, 0, 1);
38398762
RK
1361
1362 /* If we found a register, modify the RTL for the register to
1363 show the hard register, and mark that register live. */
1364 if (reg_renumber[regno] >= 0)
1365 {
1366 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1367 mark_home_live (regno);
1368 }
1369 }
1370}
1371\f
1372/* Record a conflict between register REGNO
1373 and everything currently live.
1374 REGNO must not be a pseudo reg that was allocated
1375 by local_alloc; such numbers must be translated through
1376 reg_renumber before calling here. */
1377
1378static void
1d088dee 1379record_one_conflict (int regno)
38398762 1380{
b3694847 1381 int j;
38398762
RK
1382
1383 if (regno < FIRST_PSEUDO_REGISTER)
1384 /* When a hard register becomes live,
1385 record conflicts with live pseudo regs. */
32c8d1bc 1386 EXECUTE_IF_SET_IN_ALLOCNO_SET (allocnos_live, j,
38398762 1387 {
5c0ecffe 1388 SET_HARD_REG_BIT (allocno[j].hard_reg_conflicts, regno);
32c8d1bc 1389 });
38398762
RK
1390 else
1391 /* When a pseudo-register becomes live,
1392 record conflicts first with hard regs,
1393 then with other pseudo regs. */
1394 {
b3694847
SS
1395 int ialloc = reg_allocno[regno];
1396 int ialloc_prod = ialloc * allocno_row_words;
1397
5c0ecffe 1398 IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, hard_regs_live);
38398762 1399 for (j = allocno_row_words - 1; j >= 0; j--)
9abe5d07 1400 conflicts[ialloc_prod + j] |= allocnos_live[j];
38398762
RK
1401 }
1402}
1403
1404/* Record all allocnos currently live as conflicting
bdc24974
JL
1405 with all hard regs currently live.
1406
38398762
RK
1407 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1408 are currently live. Their bits are also flagged in allocnos_live. */
1409
1410static void
1d088dee 1411record_conflicts (int *allocno_vec, int len)
38398762 1412{
38398762 1413 while (--len >= 0)
4977bab6
ZW
1414 IOR_HARD_REG_SET (allocno[allocno_vec[len]].hard_reg_conflicts,
1415 hard_regs_live);
38398762 1416}
267cf808
JL
1417
1418/* If CONFLICTP (i, j) is true, make sure CONFLICTP (j, i) is also true. */
1419static void
1d088dee 1420mirror_conflicts (void)
267cf808 1421{
b3694847 1422 int i, j;
267cf808
JL
1423 int rw = allocno_row_words;
1424 int rwb = rw * INT_BITS;
1425 INT_TYPE *p = conflicts;
1426 INT_TYPE *q0 = conflicts, *q1, *q2;
1427 unsigned INT_TYPE mask;
1428
1429 for (i = max_allocno - 1, mask = 1; i >= 0; i--, mask <<= 1)
1430 {
1431 if (! mask)
1432 {
1433 mask = 1;
1434 q0++;
1435 }
1436 for (j = allocno_row_words - 1, q1 = q0; j >= 0; j--, q1 += rwb)
1437 {
1438 unsigned INT_TYPE word;
1439
1440 for (word = (unsigned INT_TYPE) *p++, q2 = q1; word;
1441 word >>= 1, q2 += rw)
1442 {
1443 if (word & 1)
1444 *q2 |= mask;
1445 }
1446 }
1447 }
1448}
38398762
RK
1449\f
1450/* Handle the case where REG is set by the insn being scanned,
1451 during the forward scan to accumulate conflicts.
1452 Store a 1 in regs_live or allocnos_live for this register, record how many
1453 consecutive hardware registers it actually needs,
1454 and record a conflict with all other registers already live.
1455
1456 Note that even if REG does not remain alive after this insn,
1457 we must mark it here as live, to ensure a conflict between
1458 REG and any other regs set in this insn that really do live.
1459 This is because those other regs could be considered after this.
1460
1461 REG might actually be something other than a register;
1462 if so, we do nothing.
1463
1464 SETTER is 0 if this register was modified by an auto-increment (i.e.,
a4c3ddd8 1465 a REG_INC note was found for it). */
38398762
RK
1466
1467static void
1d088dee 1468mark_reg_store (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
38398762 1469{
b3694847 1470 int regno;
38398762 1471
38398762 1472 if (GET_CODE (reg) == SUBREG)
ddef6bc7 1473 reg = SUBREG_REG (reg);
38398762 1474
f8cfc6aa 1475 if (!REG_P (reg))
38398762
RK
1476 return;
1477
38398762
RK
1478 regs_set[n_regs_set++] = reg;
1479
a4c3ddd8 1480 if (setter && GET_CODE (setter) != CLOBBER)
38398762
RK
1481 set_preference (reg, SET_SRC (setter));
1482
1483 regno = REGNO (reg);
1484
38398762
RK
1485 /* Either this is one of the max_allocno pseudo regs not allocated,
1486 or it is or has a hardware reg. First handle the pseudo-regs. */
1487 if (regno >= FIRST_PSEUDO_REGISTER)
1488 {
1489 if (reg_allocno[regno] >= 0)
1490 {
1491 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1492 record_one_conflict (regno);
1493 }
1494 }
a300b8d9
JW
1495
1496 if (reg_renumber[regno] >= 0)
ddef6bc7 1497 regno = reg_renumber[regno];
a300b8d9 1498
38398762 1499 /* Handle hardware regs (and pseudos allocated to hard regs). */
a300b8d9 1500 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
38398762 1501 {
66fd46b6 1502 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
38398762
RK
1503 while (regno < last)
1504 {
1505 record_one_conflict (regno);
1506 SET_HARD_REG_BIT (hard_regs_live, regno);
1507 regno++;
1508 }
1509 }
1510}
1511\f
1512/* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1513
1514static void
9abe5d07 1515mark_reg_clobber (rtx reg, rtx setter, void *data)
38398762 1516{
a4c3ddd8 1517 if (GET_CODE (setter) == CLOBBER)
84832317 1518 mark_reg_store (reg, setter, data);
333e0f7d
RS
1519}
1520
1521/* Record that REG has conflicts with all the regs currently live.
1522 Do not mark REG itself as live. */
1523
1524static void
1d088dee 1525mark_reg_conflicts (rtx reg)
333e0f7d 1526{
b3694847 1527 int regno;
333e0f7d
RS
1528
1529 if (GET_CODE (reg) == SUBREG)
1530 reg = SUBREG_REG (reg);
1531
f8cfc6aa 1532 if (!REG_P (reg))
333e0f7d
RS
1533 return;
1534
1535 regno = REGNO (reg);
1536
333e0f7d
RS
1537 /* Either this is one of the max_allocno pseudo regs not allocated,
1538 or it is or has a hardware reg. First handle the pseudo-regs. */
1539 if (regno >= FIRST_PSEUDO_REGISTER)
1540 {
1541 if (reg_allocno[regno] >= 0)
1542 record_one_conflict (regno);
1543 }
a300b8d9
JW
1544
1545 if (reg_renumber[regno] >= 0)
1546 regno = reg_renumber[regno];
1547
333e0f7d 1548 /* Handle hardware regs (and pseudos allocated to hard regs). */
a300b8d9 1549 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
333e0f7d 1550 {
66fd46b6 1551 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
333e0f7d
RS
1552 while (regno < last)
1553 {
1554 record_one_conflict (regno);
1555 regno++;
1556 }
1557 }
38398762
RK
1558}
1559\f
1560/* Mark REG as being dead (following the insn being scanned now).
1561 Store a 0 in regs_live or allocnos_live for this register. */
1562
1563static void
1d088dee 1564mark_reg_death (rtx reg)
38398762 1565{
b3694847 1566 int regno = REGNO (reg);
38398762 1567
38398762
RK
1568 /* Either this is one of the max_allocno pseudo regs not allocated,
1569 or it is a hardware reg. First handle the pseudo-regs. */
1570 if (regno >= FIRST_PSEUDO_REGISTER)
1571 {
1572 if (reg_allocno[regno] >= 0)
1573 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1574 }
a300b8d9
JW
1575
1576 /* For pseudo reg, see if it has been assigned a hardware reg. */
1577 if (reg_renumber[regno] >= 0)
1578 regno = reg_renumber[regno];
1579
38398762 1580 /* Handle hardware regs (and pseudos allocated to hard regs). */
a300b8d9 1581 if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
38398762
RK
1582 {
1583 /* Pseudo regs already assigned hardware regs are treated
1584 almost the same as explicit hardware regs. */
66fd46b6 1585 int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
38398762
RK
1586 while (regno < last)
1587 {
1588 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1589 regno++;
1590 }
1591 }
1592}
1593
1594/* Mark hard reg REGNO as currently live, assuming machine mode MODE
1595 for the value stored in it. MODE determines how many consecutive
1596 registers are actually in use. Do not record conflicts;
1597 it is assumed that the caller will do that. */
1598
1599static void
1d088dee 1600mark_reg_live_nc (int regno, enum machine_mode mode)
38398762 1601{
66fd46b6 1602 int last = regno + hard_regno_nregs[regno][mode];
38398762
RK
1603 while (regno < last)
1604 {
1605 SET_HARD_REG_BIT (hard_regs_live, regno);
1606 regno++;
1607 }
1608}
1609\f
1610/* Try to set a preference for an allocno to a hard register.
1611 We are passed DEST and SRC which are the operands of a SET. It is known
1612 that SRC is a register. If SRC or the first operand of SRC is a register,
1613 try to set a preference. If one of the two is a hard register and the other
1614 is a pseudo-register, mark the preference.
589005ff 1615
6dc42e49 1616 Note that we are not as aggressive as local-alloc in trying to tie a
38398762
RK
1617 pseudo-register to a hard register. */
1618
1619static void
1d088dee 1620set_preference (rtx dest, rtx src)
38398762 1621{
770ae6cc 1622 unsigned int src_regno, dest_regno;
38398762
RK
1623 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1624 to compensate for subregs in SRC or DEST. */
1625 int offset = 0;
770ae6cc 1626 unsigned int i;
38398762
RK
1627 int copy = 1;
1628
1629 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1630 src = XEXP (src, 0), copy = 0;
1631
1632 /* Get the reg number for both SRC and DEST.
1633 If neither is a reg, give up. */
1634
f8cfc6aa 1635 if (REG_P (src))
38398762 1636 src_regno = REGNO (src);
f8cfc6aa 1637 else if (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src)))
38398762
RK
1638 {
1639 src_regno = REGNO (SUBREG_REG (src));
ddef6bc7
JJ
1640
1641 if (REGNO (SUBREG_REG (src)) < FIRST_PSEUDO_REGISTER)
1642 offset += subreg_regno_offset (REGNO (SUBREG_REG (src)),
1643 GET_MODE (SUBREG_REG (src)),
1644 SUBREG_BYTE (src),
1645 GET_MODE (src));
1646 else
1647 offset += (SUBREG_BYTE (src)
1648 / REGMODE_NATURAL_SIZE (GET_MODE (src)));
38398762
RK
1649 }
1650 else
1651 return;
1652
f8cfc6aa 1653 if (REG_P (dest))
38398762 1654 dest_regno = REGNO (dest);
f8cfc6aa 1655 else if (GET_CODE (dest) == SUBREG && REG_P (SUBREG_REG (dest)))
38398762
RK
1656 {
1657 dest_regno = REGNO (SUBREG_REG (dest));
ddef6bc7
JJ
1658
1659 if (REGNO (SUBREG_REG (dest)) < FIRST_PSEUDO_REGISTER)
1660 offset -= subreg_regno_offset (REGNO (SUBREG_REG (dest)),
1661 GET_MODE (SUBREG_REG (dest)),
1662 SUBREG_BYTE (dest),
1663 GET_MODE (dest));
1664 else
1665 offset -= (SUBREG_BYTE (dest)
1666 / REGMODE_NATURAL_SIZE (GET_MODE (dest)));
38398762
RK
1667 }
1668 else
1669 return;
1670
1671 /* Convert either or both to hard reg numbers. */
1672
1673 if (reg_renumber[src_regno] >= 0)
1674 src_regno = reg_renumber[src_regno];
1675
1676 if (reg_renumber[dest_regno] >= 0)
1677 dest_regno = reg_renumber[dest_regno];
1678
1679 /* Now if one is a hard reg and the other is a global pseudo
1680 then give the other a preference. */
1681
1682 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1683 && reg_allocno[src_regno] >= 0)
1684 {
1685 dest_regno -= offset;
770ae6cc 1686 if (dest_regno < FIRST_PSEUDO_REGISTER)
38398762
RK
1687 {
1688 if (copy)
1689 SET_REGBIT (hard_reg_copy_preferences,
1690 reg_allocno[src_regno], dest_regno);
1691
1692 SET_REGBIT (hard_reg_preferences,
1693 reg_allocno[src_regno], dest_regno);
1694 for (i = dest_regno;
66fd46b6 1695 i < dest_regno + hard_regno_nregs[dest_regno][GET_MODE (dest)];
38398762
RK
1696 i++)
1697 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1698 }
1699 }
1700
1701 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1702 && reg_allocno[dest_regno] >= 0)
1703 {
1704 src_regno += offset;
770ae6cc 1705 if (src_regno < FIRST_PSEUDO_REGISTER)
38398762
RK
1706 {
1707 if (copy)
1708 SET_REGBIT (hard_reg_copy_preferences,
1709 reg_allocno[dest_regno], src_regno);
1710
1711 SET_REGBIT (hard_reg_preferences,
1712 reg_allocno[dest_regno], src_regno);
1713 for (i = src_regno;
66fd46b6 1714 i < src_regno + hard_regno_nregs[src_regno][GET_MODE (src)];
38398762
RK
1715 i++)
1716 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1717 }
1718 }
1719}
1720\f
1721/* Indicate that hard register number FROM was eliminated and replaced with
1722 an offset from hard register number TO. The status of hard registers live
1723 at the start of a basic block is updated by replacing a use of FROM with
1724 a use of TO. */
1725
1726void
1d088dee 1727mark_elimination (int from, int to)
38398762 1728{
e0082a72 1729 basic_block bb;
38398762 1730
e0082a72 1731 FOR_EACH_BB (bb)
e881bb1b 1732 {
589005ff 1733 regset r = bb->global_live_at_start;
e881bb1b
RH
1734 if (REGNO_REG_SET_P (r, from))
1735 {
1736 CLEAR_REGNO_REG_SET (r, from);
1737 SET_REGNO_REG_SET (r, to);
1738 }
1739 }
38398762
RK
1740}
1741\f
cad6f7d0
BS
1742/* Used for communication between the following functions. Holds the
1743 current life information. */
1744static regset live_relevant_regs;
1745
285f3cf0
R
1746/* Record in live_relevant_regs and REGS_SET that register REG became live.
1747 This is called via note_stores. */
cad6f7d0 1748static void
1d088dee 1749reg_becomes_live (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *regs_set)
cad6f7d0
BS
1750{
1751 int regno;
1752
1753 if (GET_CODE (reg) == SUBREG)
1754 reg = SUBREG_REG (reg);
1755
f8cfc6aa 1756 if (!REG_P (reg))
cad6f7d0 1757 return;
589005ff 1758
cad6f7d0
BS
1759 regno = REGNO (reg);
1760 if (regno < FIRST_PSEUDO_REGISTER)
1761 {
66fd46b6 1762 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
cad6f7d0 1763 while (nregs-- > 0)
285f3cf0
R
1764 {
1765 SET_REGNO_REG_SET (live_relevant_regs, regno);
1766 if (! fixed_regs[regno])
1767 SET_REGNO_REG_SET ((regset) regs_set, regno);
1768 regno++;
1769 }
cad6f7d0
BS
1770 }
1771 else if (reg_renumber[regno] >= 0)
285f3cf0
R
1772 {
1773 SET_REGNO_REG_SET (live_relevant_regs, regno);
1774 SET_REGNO_REG_SET ((regset) regs_set, regno);
1775 }
cad6f7d0
BS
1776}
1777
1778/* Record in live_relevant_regs that register REGNO died. */
1779static void
1d088dee 1780reg_dies (int regno, enum machine_mode mode, struct insn_chain *chain)
cad6f7d0
BS
1781{
1782 if (regno < FIRST_PSEUDO_REGISTER)
1783 {
66fd46b6 1784 int nregs = hard_regno_nregs[regno][mode];
cad6f7d0 1785 while (nregs-- > 0)
285f3cf0
R
1786 {
1787 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1788 if (! fixed_regs[regno])
239a0f5b 1789 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
285f3cf0
R
1790 regno++;
1791 }
cad6f7d0
BS
1792 }
1793 else
285f3cf0
R
1794 {
1795 CLEAR_REGNO_REG_SET (live_relevant_regs, regno);
1796 if (reg_renumber[regno] >= 0)
239a0f5b 1797 SET_REGNO_REG_SET (&chain->dead_or_set, regno);
285f3cf0 1798 }
cad6f7d0
BS
1799}
1800
1801/* Walk the insns of the current function and build reload_insn_chain,
1802 and record register life information. */
d29c259b 1803void
1d088dee 1804build_insn_chain (rtx first)
cad6f7d0
BS
1805{
1806 struct insn_chain **p = &reload_insn_chain;
1807 struct insn_chain *prev = 0;
f6366fc7 1808 basic_block b = ENTRY_BLOCK_PTR->next_bb;
ee25a7a5 1809 regset_head live_relevant_regs_head;
cad6f7d0 1810
ee25a7a5 1811 live_relevant_regs = INITIALIZE_REG_SET (live_relevant_regs_head);
cad6f7d0 1812
108c535a 1813 for (; first; first = NEXT_INSN (first))
cad6f7d0 1814 {
108c535a 1815 struct insn_chain *c;
cad6f7d0 1816
a813c111 1817 if (first == BB_HEAD (b))
cad6f7d0 1818 {
3cd8c58a 1819 unsigned i;
87c476a2 1820 bitmap_iterator bi;
267cf808 1821
108c535a 1822 CLEAR_REG_SET (live_relevant_regs);
267cf808 1823
87c476a2
ZD
1824 EXECUTE_IF_SET_IN_BITMAP (b->global_live_at_start, 0, i, bi)
1825 {
1826 if (i < FIRST_PSEUDO_REGISTER
1827 ? ! TEST_HARD_REG_BIT (eliminable_regset, i)
1828 : reg_renumber[i] >= 0)
1829 SET_REGNO_REG_SET (live_relevant_regs, i);
1830 }
589005ff 1831 }
108c535a 1832
4b4bf941 1833 if (!NOTE_P (first) && !BARRIER_P (first))
021d1677 1834 {
108c535a
RH
1835 c = new_insn_chain ();
1836 c->prev = prev;
1837 prev = c;
1838 *p = c;
1839 p = &c->next;
1840 c->insn = first;
f6366fc7 1841 c->block = b->index;
108c535a 1842
2c3c49de 1843 if (INSN_P (first))
cad6f7d0 1844 {
108c535a 1845 rtx link;
cad6f7d0 1846
108c535a 1847 /* Mark the death of everything that dies in this instruction. */
cad6f7d0 1848
108c535a
RH
1849 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1850 if (REG_NOTE_KIND (link) == REG_DEAD
f8cfc6aa 1851 && REG_P (XEXP (link, 0)))
285f3cf0
R
1852 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1853 c);
1854
239a0f5b 1855 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
cad6f7d0 1856
108c535a 1857 /* Mark everything born in this instruction as live. */
cad6f7d0 1858
285f3cf0 1859 note_stores (PATTERN (first), reg_becomes_live,
239a0f5b 1860 &c->dead_or_set);
cad6f7d0 1861 }
285f3cf0 1862 else
239a0f5b 1863 COPY_REG_SET (&c->live_throughout, live_relevant_regs);
108c535a 1864
2c3c49de 1865 if (INSN_P (first))
108c535a
RH
1866 {
1867 rtx link;
1868
1869 /* Mark anything that is set in this insn and then unused as dying. */
1870
1871 for (link = REG_NOTES (first); link; link = XEXP (link, 1))
1872 if (REG_NOTE_KIND (link) == REG_UNUSED
f8cfc6aa 1873 && REG_P (XEXP (link, 0)))
285f3cf0
R
1874 reg_dies (REGNO (XEXP (link, 0)), GET_MODE (XEXP (link, 0)),
1875 c);
108c535a 1876 }
3663a304 1877 }
021d1677 1878
a813c111 1879 if (first == BB_END (b))
f6366fc7 1880 b = b->next_bb;
108c535a
RH
1881
1882 /* Stop after we pass the end of the last basic block. Verify that
1883 no real insns are after the end of the last basic block.
1884
1885 We may want to reorganize the loop somewhat since this test should
28bf9c88
RK
1886 always be the right exit test. Allow an ADDR_VEC or ADDR_DIF_VEC if
1887 the previous real insn is a JUMP_INSN. */
f6366fc7 1888 if (b == EXIT_BLOCK_PTR)
108c535a 1889 {
282899df
NS
1890#ifdef ENABLE_CHECKING
1891 for (first = NEXT_INSN (first); first; first = NEXT_INSN (first))
1892 gcc_assert (!INSN_P (first)
1893 || GET_CODE (PATTERN (first)) == USE
1894 || ((GET_CODE (PATTERN (first)) == ADDR_VEC
1895 || GET_CODE (PATTERN (first)) == ADDR_DIFF_VEC)
1896 && prev_real_insn (first) != 0
1897 && JUMP_P (prev_real_insn (first))));
1898#endif
108c535a
RH
1899 break;
1900 }
1901 }
cad6f7d0
BS
1902 FREE_REG_SET (live_relevant_regs);
1903 *p = 0;
1904}
1905\f
318e4b56 1906/* Print debugging trace information if -dg switch is given,
38398762
RK
1907 showing the information on which the allocation decisions are based. */
1908
1909static void
1d088dee 1910dump_conflicts (FILE *file)
38398762 1911{
b3694847
SS
1912 int i;
1913 int has_preferences;
1914 int nregs;
a300b8d9
JW
1915 nregs = 0;
1916 for (i = 0; i < max_allocno; i++)
1917 {
5c0ecffe 1918 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
589005ff 1919 continue;
a300b8d9
JW
1920 nregs++;
1921 }
1922 fprintf (file, ";; %d regs to allocate:", nregs);
38398762
RK
1923 for (i = 0; i < max_allocno; i++)
1924 {
1925 int j;
5c0ecffe 1926 if (reg_renumber[allocno[allocno_order[i]].reg] >= 0)
a300b8d9 1927 continue;
5c0ecffe 1928 fprintf (file, " %d", allocno[allocno_order[i]].reg);
38398762
RK
1929 for (j = 0; j < max_regno; j++)
1930 if (reg_allocno[j] == allocno_order[i]
5c0ecffe 1931 && j != allocno[allocno_order[i]].reg)
38398762 1932 fprintf (file, "+%d", j);
5c0ecffe
JH
1933 if (allocno[allocno_order[i]].size != 1)
1934 fprintf (file, " (%d)", allocno[allocno_order[i]].size);
38398762
RK
1935 }
1936 fprintf (file, "\n");
1937
1938 for (i = 0; i < max_allocno; i++)
1939 {
b3694847 1940 int j;
5c0ecffe 1941 fprintf (file, ";; %d conflicts:", allocno[i].reg);
38398762 1942 for (j = 0; j < max_allocno; j++)
267cf808 1943 if (CONFLICTP (j, i))
5c0ecffe 1944 fprintf (file, " %d", allocno[j].reg);
38398762 1945 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5c0ecffe 1946 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_conflicts, j))
38398762
RK
1947 fprintf (file, " %d", j);
1948 fprintf (file, "\n");
1949
1950 has_preferences = 0;
1951 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5c0ecffe 1952 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
38398762
RK
1953 has_preferences = 1;
1954
1955 if (! has_preferences)
1956 continue;
5c0ecffe 1957 fprintf (file, ";; %d preferences:", allocno[i].reg);
38398762 1958 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
5c0ecffe 1959 if (TEST_HARD_REG_BIT (allocno[i].hard_reg_preferences, j))
38398762
RK
1960 fprintf (file, " %d", j);
1961 fprintf (file, "\n");
1962 }
1963 fprintf (file, "\n");
1964}
1965
1966void
1d088dee 1967dump_global_regs (FILE *file)
38398762 1968{
b3694847 1969 int i, j;
589005ff 1970
38398762
RK
1971 fprintf (file, ";; Register dispositions:\n");
1972 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1973 if (reg_renumber[i] >= 0)
1974 {
1975 fprintf (file, "%d in %d ", i, reg_renumber[i]);
589005ff 1976 if (++j % 6 == 0)
38398762
RK
1977 fprintf (file, "\n");
1978 }
1979
1980 fprintf (file, "\n\n;; Hard regs used: ");
1981 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1982 if (regs_ever_live[i])
1983 fprintf (file, " %d", i);
1984 fprintf (file, "\n\n");
1985}
9abe5d07
VM
1986
1987\f
1988
1989/* This page contains code to make live information more accurate.
1990 The accurate register liveness at program point P means:
1991 o there is a path from P to usage of the register and the
1992 register is not redefined or killed on the path.
1993 o register at P is partially available, i.e. there is a path from
1994 a register definition to the point P and the register is not
1995 killed (clobbered) on the path
1996
1997 The standard GCC live information means only the first condition.
1998 Without the partial availability, there will be more register
1999 conflicts and as a consequence worse register allocation. The
2000 typical example where the information can be different is a
2001 register initialized in the loop at the basic block preceding the
9cf737f8 2002 loop in CFG. */
9abe5d07
VM
2003
2004/* The following structure contains basic block data flow information
2005 used to calculate partial availability of registers. */
2006
2007struct bb_info
2008{
2009 /* The basic block reverse post-order number. */
2010 int rts_number;
fdbda73f
VM
2011 /* Registers used uninitialized in an insn in which there is an
2012 early clobbered register might get the same hard register. */
2013 bitmap earlyclobber;
9abe5d07
VM
2014 /* Registers correspondingly killed (clobbered) and defined but not
2015 killed afterward in the basic block. */
2016 bitmap killed, avloc;
2017 /* Registers partially available correspondingly at the start and
2018 end of the basic block. */
2019 bitmap pavin, pavout;
2020};
2021
2022/* Macros for accessing data flow information of basic blocks. */
2023
2024#define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
2025#define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
2026
2027/* The function allocates the info structures of each basic block. It
2028 also initialized PAVIN and PAVOUT as if all hard registers were
2029 partially available. */
2030
2031static void
2032allocate_bb_info (void)
2033{
2034 int i;
2035 basic_block bb;
2036 struct bb_info *bb_info;
2037 bitmap init;
2038
2039 alloc_aux_for_blocks (sizeof (struct bb_info));
2040 init = BITMAP_XMALLOC ();
2041 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2042 bitmap_set_bit (init, i);
2043 FOR_EACH_BB (bb)
2044 {
2045 bb_info = bb->aux;
fdbda73f 2046 bb_info->earlyclobber = BITMAP_XMALLOC ();
9abe5d07
VM
2047 bb_info->avloc = BITMAP_XMALLOC ();
2048 bb_info->killed = BITMAP_XMALLOC ();
2049 bb_info->pavin = BITMAP_XMALLOC ();
2050 bb_info->pavout = BITMAP_XMALLOC ();
2051 bitmap_copy (bb_info->pavin, init);
2052 bitmap_copy (bb_info->pavout, init);
2053 }
2054 BITMAP_XFREE (init);
2055}
2056
2057/* The function frees the allocated info of all basic blocks. */
2058
2059static void
2060free_bb_info (void)
2061{
2062 basic_block bb;
2063 struct bb_info *bb_info;
2064
2065 FOR_EACH_BB (bb)
2066 {
2067 bb_info = BB_INFO (bb);
2068 BITMAP_XFREE (bb_info->pavout);
2069 BITMAP_XFREE (bb_info->pavin);
2070 BITMAP_XFREE (bb_info->killed);
2071 BITMAP_XFREE (bb_info->avloc);
fdbda73f 2072 BITMAP_XFREE (bb_info->earlyclobber);
9abe5d07
VM
2073 }
2074 free_aux_for_blocks ();
2075}
2076
2077/* The function modifies local info for register REG being changed in
2078 SETTER. DATA is used to pass the current basic block info. */
2079
2080static void
2081mark_reg_change (rtx reg, rtx setter, void *data)
2082{
2083 int regno;
2084 basic_block bb = data;
2085 struct bb_info *bb_info = BB_INFO (bb);
2086
2087 if (GET_CODE (reg) == SUBREG)
2088 reg = SUBREG_REG (reg);
2089
f8cfc6aa 2090 if (!REG_P (reg))
9abe5d07
VM
2091 return;
2092
2093 regno = REGNO (reg);
2094 bitmap_set_bit (bb_info->killed, regno);
2095
2096 if (GET_CODE (setter) != CLOBBER)
2097 bitmap_set_bit (bb_info->avloc, regno);
2098 else
2099 bitmap_clear_bit (bb_info->avloc, regno);
2100}
2101
fdbda73f
VM
2102/* Classes of registers which could be early clobbered in the current
2103 insn. */
2104
2105static varray_type earlyclobber_regclass;
2106
2107/* The function stores classes of registers which could be early
2108 clobbered in INSN. */
2109
2110static void
2111check_earlyclobber (rtx insn)
2112{
2113 int opno;
2114
2115 extract_insn (insn);
2116
2117 VARRAY_POP_ALL (earlyclobber_regclass);
2118 for (opno = 0; opno < recog_data.n_operands; opno++)
2119 {
2120 char c;
2121 bool amp_p;
2122 int i;
2123 enum reg_class class;
2124 const char *p = recog_data.constraints[opno];
2125
2126 class = NO_REGS;
2127 amp_p = false;
2128 for (;;)
2129 {
2130 c = *p;
2131 switch (c)
2132 {
2133 case '=': case '+': case '?':
2134 case '#': case '!':
2135 case '*': case '%':
2136 case 'm': case '<': case '>': case 'V': case 'o':
2137 case 'E': case 'F': case 'G': case 'H':
2138 case 's': case 'i': case 'n':
2139 case 'I': case 'J': case 'K': case 'L':
2140 case 'M': case 'N': case 'O': case 'P':
2141 case 'X':
2142 case '0': case '1': case '2': case '3': case '4':
2143 case '5': case '6': case '7': case '8': case '9':
2144 /* These don't say anything we care about. */
2145 break;
2146
2147 case '&':
2148 amp_p = true;
2149 break;
2150 case '\0':
2151 case ',':
2152 if (amp_p && class != NO_REGS)
2153 {
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);
2160 }
2161
2162 amp_p = false;
2163 class = NO_REGS;
2164 break;
2165
2166 case 'r':
2167 class = GENERAL_REGS;
2168 break;
2169
2170 default:
2171 class = REG_CLASS_FROM_CONSTRAINT (c, p);
2172 break;
2173 }
2174 if (c == '\0')
2175 break;
2176 p += CONSTRAINT_LEN (c, p);
2177 }
2178 }
2179}
2180
2a7e31df 2181/* The function returns true if register classes C1 and C2 intersect. */
fdbda73f
VM
2182
2183static bool
2184regclass_intersect (enum reg_class c1, enum reg_class c2)
2185{
2186 HARD_REG_SET rs, zero;
2187
2188 CLEAR_HARD_REG_SET (zero);
2189 COPY_HARD_REG_SET(rs, reg_class_contents [c1]);
2190 AND_HARD_REG_SET (rs, reg_class_contents [c2]);
2191 GO_IF_HARD_REG_EQUAL (zero, rs, yes);
2192 return true;
2193 yes:
2194 return false;
2195}
2196
2197/* The function checks that pseudo-register *X has a class
2198 intersecting with the class of pseudo-register could be early
2199 clobbered in the same insn. */
2200
2201static int
2202mark_reg_use_for_earlyclobber (rtx *x, void *data ATTRIBUTE_UNUSED)
2203{
2204 enum reg_class pref_class, alt_class;
2205 int i, regno;
2206 basic_block bb = data;
2207 struct bb_info *bb_info = BB_INFO (bb);
2208
2209 if (GET_CODE (*x) == REG && REGNO (*x) >= FIRST_PSEUDO_REGISTER)
2210 {
2211 regno = REGNO (*x);
2212 if (bitmap_bit_p (bb_info->killed, regno)
2213 || bitmap_bit_p (bb_info->avloc, regno))
2214 return 0;
2215 pref_class = reg_preferred_class (regno);
2216 alt_class = reg_alternate_class (regno);
2217 for (i = VARRAY_ACTIVE_SIZE (earlyclobber_regclass) - 1; i >= 0; i--)
2218 if (regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2219 pref_class)
2220 || (VARRAY_INT (earlyclobber_regclass, i) != NO_REGS
2221 && regclass_intersect (VARRAY_INT (earlyclobber_regclass, i),
2222 alt_class)))
2223 {
2224 bitmap_set_bit (bb_info->earlyclobber, regno);
2225 break;
2226 }
2227 }
2228 return 0;
2229}
2230
2231/* The function processes all pseudo-registers in *X with the aid of
2232 previous function. */
2233
2234static void
2235mark_reg_use_for_earlyclobber_1 (rtx *x, void *data)
2236{
2237 for_each_rtx (x, mark_reg_use_for_earlyclobber, data);
2238}
2239
9abe5d07
VM
2240/* The function calculates local info for each basic block. */
2241
2242static void
2243calculate_local_reg_bb_info (void)
2244{
2245 basic_block bb;
2246 rtx insn, bound;
2247
fdbda73f
VM
2248 VARRAY_INT_INIT (earlyclobber_regclass, 20,
2249 "classes of registers early clobbered in an insn");
9abe5d07
VM
2250 FOR_EACH_BB (bb)
2251 {
2252 bound = NEXT_INSN (BB_END (bb));
2253 for (insn = BB_HEAD (bb); insn != bound; insn = NEXT_INSN (insn))
2254 if (INSN_P (insn))
fdbda73f
VM
2255 {
2256 note_stores (PATTERN (insn), mark_reg_change, bb);
2257 check_earlyclobber (insn);
2258 note_uses (&PATTERN (insn), mark_reg_use_for_earlyclobber_1, bb);
2259 }
9abe5d07
VM
2260 }
2261}
2262
2263/* The function sets up reverse post-order number of each basic
2264 block. */
2265
2266static void
2267set_up_bb_rts_numbers (void)
2268{
2269 int i;
2270 int *rts_order;
2271
2272 rts_order = xmalloc (sizeof (int) * n_basic_blocks);
2273 flow_reverse_top_sort_order_compute (rts_order);
2274 for (i = 0; i < n_basic_blocks; i++)
2275 BB_INFO_BY_INDEX (rts_order [i])->rts_number = i;
2276 free (rts_order);
2277}
2278
2279/* Compare function for sorting blocks in reverse postorder. */
2280
2281static int
2282rpost_cmp (const void *bb1, const void *bb2)
2283{
2284 basic_block b1 = *(basic_block *) bb1, b2 = *(basic_block *) bb2;
2285
2286 return BB_INFO (b2)->rts_number - BB_INFO (b1)->rts_number;
2287}
2288
2289/* The function calculates partial availability of registers. The
2290 function calculates partial availability at the end of basic block
2291 BB by propagating partial availability at end of predecessor basic
2292 block PRED. The function returns true if the partial availability
2293 at the end of BB has been changed or if CHANGED_P. We have the
2294 following equations:
2295
2296 bb.pavin = empty for entry block | union (pavout of predecessors)
2297 bb.pavout = union (bb.pavin - b.killed, bb.avloc) */
2298
2299static bool
2300modify_bb_reg_pav (basic_block bb, basic_block pred, bool changed_p)
2301{
2302 struct bb_info *bb_info;
2303 bitmap bb_pavin, bb_pavout;
2304
2305 bb_info = BB_INFO (bb);
2306 bb_pavin = bb_info->pavin;
2307 bb_pavout = bb_info->pavout;
2308 if (pred->index != ENTRY_BLOCK)
67299d91 2309 bitmap_ior_into (bb_pavin, BB_INFO (pred)->pavout);
7ef7b345 2310 changed_p |= bitmap_ior_and_compl (bb_pavout, bb_info->avloc,
9abe5d07
VM
2311 bb_pavin, bb_info->killed);
2312 return changed_p;
2313}
2314
2315/* The function calculates partial register availability. */
2316
2317static void
2318calculate_reg_pav (void)
2319{
2320 basic_block bb, succ;
2321 edge e;
2322 bool changed_p;
2323 int i, nel;
2324 varray_type bbs, new_bbs, temp;
2325 basic_block *bb_array;
2326 sbitmap wset;
2327
2328 VARRAY_BB_INIT (bbs, n_basic_blocks, "basic blocks");
2329 VARRAY_BB_INIT (new_bbs, n_basic_blocks, "basic blocks for the next iter.");
2330 FOR_EACH_BB (bb)
2331 {
2332 VARRAY_PUSH_BB (bbs, bb);
2333 }
2334 wset = sbitmap_alloc (n_basic_blocks + 1);
2335 while (VARRAY_ACTIVE_SIZE (bbs))
2336 {
2337 bb_array = &VARRAY_BB (bbs, 0);
2338 nel = VARRAY_ACTIVE_SIZE (bbs);
2339 qsort (bb_array, nel, sizeof (basic_block), rpost_cmp);
2340 sbitmap_zero (wset);
2341 for (i = 0; i < nel; i++)
2342 {
628f6a4e
BE
2343 edge_iterator ei;
2344
9abe5d07
VM
2345 bb = bb_array [i];
2346 changed_p = 0;
628f6a4e 2347 FOR_EACH_EDGE (e, ei, bb->preds)
9abe5d07
VM
2348 changed_p = modify_bb_reg_pav (bb, e->src, changed_p);
2349 if (changed_p)
628f6a4e 2350 FOR_EACH_EDGE (e, ei, bb->succs)
9abe5d07
VM
2351 {
2352 succ = e->dest;
2353 if (succ->index != EXIT_BLOCK && !TEST_BIT (wset, succ->index))
2354 {
2355 SET_BIT (wset, succ->index);
2356 VARRAY_PUSH_BB (new_bbs, succ);
2357 }
2358 }
2359 }
2360 temp = bbs;
2361 bbs = new_bbs;
2362 new_bbs = temp;
2363 VARRAY_POP_ALL (new_bbs);
2364 }
2365 sbitmap_free (wset);
2366}
2367
d6df6ae2
VM
2368/* The function modifies partial availability information for two
2369 special cases to prevent incorrect work of the subsequent passes
2370 with the accurate live information based on the partial
2371 availability. */
2372
2373static void
2374modify_reg_pav (void)
2375{
2376 basic_block bb;
2377 struct bb_info *bb_info;
2378#ifdef STACK_REGS
2379 int i;
2380 HARD_REG_SET zero, stack_hard_regs, used;
2381 bitmap stack_regs;
2382
2383 CLEAR_HARD_REG_SET (zero);
2384 CLEAR_HARD_REG_SET (stack_hard_regs);
2385 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
2386 SET_HARD_REG_BIT(stack_hard_regs, i);
2387 stack_regs = BITMAP_XMALLOC ();
2388 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2389 {
2390 COPY_HARD_REG_SET (used, reg_class_contents[reg_preferred_class (i)]);
2391 IOR_HARD_REG_SET (used, reg_class_contents[reg_alternate_class (i)]);
2392 AND_HARD_REG_SET (used, stack_hard_regs);
2393 GO_IF_HARD_REG_EQUAL(used, zero, skip);
2394 bitmap_set_bit (stack_regs, i);
2395 skip:
2396 ;
2397 }
2398#endif
2399 FOR_EACH_BB (bb)
2400 {
2401 bb_info = BB_INFO (bb);
2402
2403 /* Reload can assign the same hard register to uninitialized
2404 pseudo-register and early clobbered pseudo-register in an
2405 insn if the pseudo-register is used first time in given BB
2406 and not lived at the BB start. To prevent this we don't
2407 change life information for such pseudo-registers. */
67299d91 2408 bitmap_ior_into (bb_info->pavin, bb_info->earlyclobber);
d6df6ae2
VM
2409#ifdef STACK_REGS
2410 /* We can not use the same stack register for uninitialized
2411 pseudo-register and another living pseudo-register because if the
2412 uninitialized pseudo-register dies, subsequent pass reg-stack
2413 will be confused (it will believe that the other register
2414 dies). */
67299d91 2415 bitmap_ior_into (bb_info->pavin, stack_regs);
d6df6ae2
VM
2416#endif
2417 }
2418#ifdef STACK_REGS
2419 BITMAP_XFREE (stack_regs);
2420#endif
2421}
2422
9abe5d07
VM
2423/* The following function makes live information more accurate by
2424 modifying global_live_at_start and global_live_at_end of basic
2425 blocks. After the function call a register lives at a program
2426 point only if it is initialized on a path from CFG entry to the
2427 program point. The standard GCC life analysis permits registers to
9cf737f8 2428 live uninitialized. */
9abe5d07
VM
2429
2430static void
2431make_accurate_live_analysis (void)
2432{
2433 basic_block bb;
2434 struct bb_info *bb_info;
2435
2436 max_regno = max_reg_num ();
2437 compact_blocks ();
2438 allocate_bb_info ();
2439 calculate_local_reg_bb_info ();
2440 set_up_bb_rts_numbers ();
2441 calculate_reg_pav ();
d6df6ae2 2442 modify_reg_pav ();
9abe5d07
VM
2443 FOR_EACH_BB (bb)
2444 {
2445 bb_info = BB_INFO (bb);
2446
67299d91
NS
2447 bitmap_and_into (bb->global_live_at_start, bb_info->pavin);
2448 bitmap_and_into (bb->global_live_at_end, bb_info->pavout);
9abe5d07
VM
2449 }
2450 free_bb_info ();
2451}
This page took 2.284477 seconds and 5 git commands to generate.