]> gcc.gnu.org Git - gcc.git/blame - gcc/global.c
Minor reformatting.
[gcc.git] / gcc / global.c
CommitLineData
38398762 1/* Allocate registers for pseudo-registers that span basic blocks.
f7627ef4 2 Copyright (C) 1987, 1988, 1991, 1994, 1996 Free Software Foundation, Inc.
38398762
RK
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
a35311b0
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
38398762
RK
20
21
22#include <stdio.h>
23#include "config.h"
24#include "rtl.h"
25#include "flags.h"
26#include "basic-block.h"
27#include "hard-reg-set.h"
28#include "regs.h"
29#include "insn-config.h"
30#include "output.h"
31
32/* This pass of the compiler performs global register allocation.
33 It assigns hard register numbers to all the pseudo registers
34 that were not handled in local_alloc. Assignments are recorded
35 in the vector reg_renumber, not by changing the rtl code.
36 (Such changes are made by final). The entry point is
37 the function global_alloc.
38
39 After allocation is complete, the reload pass is run as a subroutine
40 of this pass, so that when a pseudo reg loses its hard reg due to
41 spilling it is possible to make a second attempt to find a hard
42 reg for it. The reload pass is independent in other respects
43 and it is run even when stupid register allocation is in use.
44
45 1. count the pseudo-registers still needing allocation
46 and assign allocation-numbers (allocnos) to them.
47 Set up tables reg_allocno and allocno_reg to map
48 reg numbers to allocnos and vice versa.
49 max_allocno gets the number of allocnos in use.
50
51 2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
52 Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
53 for conflicts between allocnos and explicit hard register use
54 (which includes use of pseudo-registers allocated by local_alloc).
55
56 3. for each basic block
57 walk forward through the block, recording which
58 unallocated registers and which hardware registers are live.
59 Build the conflict matrix between the unallocated registers
60 and another of unallocated registers versus hardware registers.
61 Also record the preferred hardware registers
62 for each unallocated one.
63
64 4. Sort a table of the allocnos into order of
65 desirability of the variables.
66
67 5. Allocate the variables in that order; each if possible into
68 a preferred register, else into another register. */
69\f
70/* Number of pseudo-registers still requiring allocation
71 (not allocated by local_allocate). */
72
73static int max_allocno;
74
75/* Indexed by (pseudo) reg number, gives the allocno, or -1
76 for pseudo registers already allocated by local_allocate. */
77
f7627ef4 78int *reg_allocno;
38398762
RK
79
80/* Indexed by allocno, gives the reg number. */
81
82static int *allocno_reg;
83
84/* A vector of the integers from 0 to max_allocno-1,
85 sorted in the order of first-to-be-allocated first. */
86
87static int *allocno_order;
88
89/* Indexed by an allocno, gives the number of consecutive
90 hard registers needed by that pseudo reg. */
91
92static int *allocno_size;
93
94/* Indexed by (pseudo) reg number, gives the number of another
6dc42e49 95 lower-numbered pseudo reg which can share a hard reg with this pseudo
38398762
RK
96 *even if the two pseudos would otherwise appear to conflict*. */
97
98static int *reg_may_share;
99
b1ec3c92
CH
100/* Define the number of bits in each element of `conflicts' and what
101 type that element has. We use the largest integer format on the
102 host machine. */
103
104#define INT_BITS HOST_BITS_PER_WIDE_INT
105#define INT_TYPE HOST_WIDE_INT
106
38398762
RK
107/* max_allocno by max_allocno array of bits,
108 recording whether two allocno's conflict (can't go in the same
109 hardware register).
110
111 `conflicts' is not symmetric; a conflict between allocno's i and j
112 is recorded either in element i,j or in element j,i. */
113
b1ec3c92 114static INT_TYPE *conflicts;
38398762
RK
115
116/* Number of ints require to hold max_allocno bits.
117 This is the length of a row in `conflicts'. */
118
119static int allocno_row_words;
120
121/* Two macros to test or store 1 in an element of `conflicts'. */
122
123#define CONFLICTP(I, J) \
124 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
b1ec3c92 125 & ((INT_TYPE) 1 << ((J) % INT_BITS)))
38398762
RK
126
127#define SET_CONFLICT(I, J) \
128 (conflicts[(I) * allocno_row_words + (J) / INT_BITS] \
b1ec3c92 129 |= ((INT_TYPE) 1 << ((J) % INT_BITS)))
38398762
RK
130
131/* Set of hard regs currently live (during scan of all insns). */
132
133static HARD_REG_SET hard_regs_live;
134
135/* Indexed by N, set of hard regs conflicting with allocno N. */
136
137static HARD_REG_SET *hard_reg_conflicts;
138
139/* Indexed by N, set of hard regs preferred by allocno N.
140 This is used to make allocnos go into regs that are copied to or from them,
141 when possible, to reduce register shuffling. */
142
143static HARD_REG_SET *hard_reg_preferences;
144
145/* Similar, but just counts register preferences made in simple copy
146 operations, rather than arithmetic. These are given priority because
147 we can always eliminate an insn by using these, but using a register
148 in the above list won't always eliminate an insn. */
149
150static HARD_REG_SET *hard_reg_copy_preferences;
151
152/* Similar to hard_reg_preferences, but includes bits for subsequent
153 registers when an allocno is multi-word. The above variable is used for
154 allocation while this is used to build reg_someone_prefers, below. */
155
156static HARD_REG_SET *hard_reg_full_preferences;
157
158/* Indexed by N, set of hard registers that some later allocno has a
159 preference for. */
160
161static HARD_REG_SET *regs_someone_prefers;
162
163/* Set of registers that global-alloc isn't supposed to use. */
164
165static HARD_REG_SET no_global_alloc_regs;
166
167/* Set of registers used so far. */
168
169static HARD_REG_SET regs_used_so_far;
170
171/* Number of calls crossed by each allocno. */
172
173static int *allocno_calls_crossed;
174
175/* Number of refs (weighted) to each allocno. */
176
177static int *allocno_n_refs;
178
179/* Guess at live length of each allocno.
180 This is actually the max of the live lengths of the regs. */
181
182static int *allocno_live_length;
183
1d56e983
RS
184/* Number of refs (weighted) to each hard reg, as used by local alloc.
185 It is zero for a reg that contains global pseudos or is explicitly used. */
186
187static int local_reg_n_refs[FIRST_PSEUDO_REGISTER];
188
189/* Guess at live length of each hard reg, as used by local alloc.
190 This is actually the sum of the live lengths of the specific regs. */
191
192static int local_reg_live_length[FIRST_PSEUDO_REGISTER];
193
38398762
RK
194/* Test a bit in TABLE, a vector of HARD_REG_SETs,
195 for vector element I, and hard register number J. */
196
197#define REGBITP(TABLE, I, J) TEST_HARD_REG_BIT (TABLE[I], J)
198
199/* Set to 1 a bit in a vector of HARD_REG_SETs. Works like REGBITP. */
200
201#define SET_REGBIT(TABLE, I, J) SET_HARD_REG_BIT (TABLE[I], J)
202
203/* Bit mask for allocnos live at current point in the scan. */
204
b1ec3c92 205static INT_TYPE *allocnos_live;
38398762
RK
206
207/* Test, set or clear bit number I in allocnos_live,
208 a bit vector indexed by allocno. */
209
210#define ALLOCNO_LIVE_P(I) \
b1ec3c92 211 (allocnos_live[(I) / INT_BITS] & ((INT_TYPE) 1 << ((I) % INT_BITS)))
38398762
RK
212
213#define SET_ALLOCNO_LIVE(I) \
b1ec3c92 214 (allocnos_live[(I) / INT_BITS] |= ((INT_TYPE) 1 << ((I) % INT_BITS)))
38398762
RK
215
216#define CLEAR_ALLOCNO_LIVE(I) \
b1ec3c92 217 (allocnos_live[(I) / INT_BITS] &= ~((INT_TYPE) 1 << ((I) % INT_BITS)))
38398762
RK
218
219/* This is turned off because it doesn't work right for DImode.
220 (And it is only used for DImode, so the other cases are worthless.)
221 The problem is that it isn't true that there is NO possibility of conflict;
222 only that there is no conflict if the two pseudos get the exact same regs.
223 If they were allocated with a partial overlap, there would be a conflict.
224 We can't safely turn off the conflict unless we have another way to
225 prevent the partial overlap.
226
227 Idea: change hard_reg_conflicts so that instead of recording which
228 hard regs the allocno may not overlap, it records where the allocno
229 may not start. Change both where it is used and where it is updated.
230 Then there is a way to record that (reg:DI 108) may start at 10
231 but not at 9 or 11. There is still the question of how to record
232 this semi-conflict between two pseudos. */
233#if 0
234/* Reg pairs for which conflict after the current insn
235 is inhibited by a REG_NO_CONFLICT note.
236 If the table gets full, we ignore any other notes--that is conservative. */
237#define NUM_NO_CONFLICT_PAIRS 4
238/* Number of pairs in use in this insn. */
239int n_no_conflict_pairs;
240static struct { int allocno1, allocno2;}
241 no_conflict_pairs[NUM_NO_CONFLICT_PAIRS];
242#endif /* 0 */
243
244/* Record all regs that are set in any one insn.
245 Communication from mark_reg_{store,clobber} and global_conflicts. */
246
247static rtx *regs_set;
248static int n_regs_set;
249
daf55ac6 250/* All registers that can be eliminated. */
38398762
RK
251
252static HARD_REG_SET eliminable_regset;
253
daa6f17d 254static int allocno_compare PROTO((const GENERIC_PTR, const GENERIC_PTR));
82c68a78
RK
255static void global_conflicts PROTO((void));
256static void expand_preferences PROTO((void));
257static void prune_preferences PROTO((void));
258static void find_reg PROTO((int, HARD_REG_SET, int, int, int));
259static void record_one_conflict PROTO((int));
260static void record_conflicts PROTO((short *, int));
261static void mark_reg_store PROTO((rtx, rtx));
262static void mark_reg_clobber PROTO((rtx, rtx));
263static void mark_reg_conflicts PROTO((rtx));
264static void mark_reg_death PROTO((rtx));
265static void mark_reg_live_nc PROTO((int, enum machine_mode));
266static void set_preference PROTO((rtx, rtx));
267static void dump_conflicts PROTO((FILE *));
38398762
RK
268\f
269/* Perform allocation of pseudo-registers not allocated by local_alloc.
270 FILE is a file to output debugging information on,
ab40ad2b 271 or zero if such output is not desired.
38398762 272
ab40ad2b
RS
273 Return value is nonzero if reload failed
274 and we must not do any more for this function. */
275
276int
38398762
RK
277global_alloc (file)
278 FILE *file;
279{
280#ifdef ELIMINABLE_REGS
281 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
282#endif
daf55ac6
RK
283 int need_fp
284 = (! flag_omit_frame_pointer
285#ifdef EXIT_IGNORE_STACK
286 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
287#endif
288 || FRAME_POINTER_REQUIRED);
289
38398762
RK
290 register int i;
291 rtx x;
292
293 max_allocno = 0;
294
295 /* A machine may have certain hard registers that
296 are safe to use only within a basic block. */
297
298 CLEAR_HARD_REG_SET (no_global_alloc_regs);
299#ifdef OVERLAPPING_REGNO_P
300 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
301 if (OVERLAPPING_REGNO_P (i))
302 SET_HARD_REG_BIT (no_global_alloc_regs, i);
303#endif
304
305 /* Build the regset of all eliminable registers and show we can't use those
306 that we already know won't be eliminated. */
307#ifdef ELIMINABLE_REGS
308 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
309 {
310 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
311
312 if (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
daf55ac6 313 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp))
38398762
RK
314 SET_HARD_REG_BIT (no_global_alloc_regs, eliminables[i].from);
315 }
7b0957a7 316#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
daf55ac6
RK
317 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
318 if (need_fp)
7b0957a7
DE
319 SET_HARD_REG_BIT (no_global_alloc_regs, HARD_FRAME_POINTER_REGNUM);
320#endif
daf55ac6 321
38398762
RK
322#else
323 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
daf55ac6 324 if (need_fp)
38398762
RK
325 SET_HARD_REG_BIT (no_global_alloc_regs, FRAME_POINTER_REGNUM);
326#endif
327
328 /* Track which registers have already been used. Start with registers
329 explicitly in the rtl, then registers allocated by local register
b4b4db94 330 allocation. */
38398762
RK
331
332 CLEAR_HARD_REG_SET (regs_used_so_far);
b4b4db94
RS
333#ifdef LEAF_REGISTERS
334 /* If we are doing the leaf function optimization, and this is a leaf
335 function, it means that the registers that take work to save are those
336 that need a register window. So prefer the ones that can be used in
337 a leaf function. */
338 {
339 char *cheap_regs;
340 static char leaf_regs[] = LEAF_REGISTERS;
341
342 if (only_leaf_regs_used () && leaf_function_p ())
343 cheap_regs = leaf_regs;
344 else
345 cheap_regs = call_used_regs;
346 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
347 if (regs_ever_live[i] || cheap_regs[i])
348 SET_HARD_REG_BIT (regs_used_so_far, i);
349 }
350#else
351 /* We consider registers that do not have to be saved over calls as if
352 they were already used since there is no cost in using them. */
38398762
RK
353 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
354 if (regs_ever_live[i] || call_used_regs[i])
355 SET_HARD_REG_BIT (regs_used_so_far, i);
b4b4db94 356#endif
38398762
RK
357
358 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
359 if (reg_renumber[i] >= 0)
360 SET_HARD_REG_BIT (regs_used_so_far, reg_renumber[i]);
361
362 /* Establish mappings from register number to allocation number
363 and vice versa. In the process, count the allocnos. */
364
365 reg_allocno = (int *) alloca (max_regno * sizeof (int));
366
367 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
368 reg_allocno[i] = -1;
369
370 /* Initialize the shared-hard-reg mapping
371 from the list of pairs that may share. */
372 reg_may_share = (int *) alloca (max_regno * sizeof (int));
4c9a05bc 373 bzero ((char *) reg_may_share, max_regno * sizeof (int));
38398762
RK
374 for (x = regs_may_share; x; x = XEXP (XEXP (x, 1), 1))
375 {
376 int r1 = REGNO (XEXP (x, 0));
377 int r2 = REGNO (XEXP (XEXP (x, 1), 0));
378 if (r1 > r2)
379 reg_may_share[r1] = r2;
380 else
381 reg_may_share[r2] = r1;
382 }
383
384 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
385 /* Note that reg_live_length[i] < 0 indicates a "constant" reg
386 that we are supposed to refrain from putting in a hard reg.
387 -2 means do make an allocno but don't allocate it. */
b1f21e0a 388 if (REG_N_REFS (i) != 0 && reg_renumber[i] < 0 && REG_LIVE_LENGTH (i) != -1
38398762
RK
389 /* Don't allocate pseudos that cross calls,
390 if this function receives a nonlocal goto. */
391 && (! current_function_has_nonlocal_label
b1f21e0a 392 || REG_N_CALLS_CROSSED (i) == 0))
38398762
RK
393 {
394 if (reg_may_share[i] && reg_allocno[reg_may_share[i]] >= 0)
395 reg_allocno[i] = reg_allocno[reg_may_share[i]];
396 else
397 reg_allocno[i] = max_allocno++;
b1f21e0a 398 if (REG_LIVE_LENGTH (i) == 0)
38398762
RK
399 abort ();
400 }
401 else
402 reg_allocno[i] = -1;
403
404 allocno_reg = (int *) alloca (max_allocno * sizeof (int));
405 allocno_size = (int *) alloca (max_allocno * sizeof (int));
406 allocno_calls_crossed = (int *) alloca (max_allocno * sizeof (int));
407 allocno_n_refs = (int *) alloca (max_allocno * sizeof (int));
408 allocno_live_length = (int *) alloca (max_allocno * sizeof (int));
4c9a05bc
RK
409 bzero ((char *) allocno_size, max_allocno * sizeof (int));
410 bzero ((char *) allocno_calls_crossed, max_allocno * sizeof (int));
411 bzero ((char *) allocno_n_refs, max_allocno * sizeof (int));
412 bzero ((char *) allocno_live_length, max_allocno * sizeof (int));
38398762
RK
413
414 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
415 if (reg_allocno[i] >= 0)
416 {
417 int allocno = reg_allocno[i];
418 allocno_reg[allocno] = i;
419 allocno_size[allocno] = PSEUDO_REGNO_SIZE (i);
b1f21e0a
MM
420 allocno_calls_crossed[allocno] += REG_N_CALLS_CROSSED (i);
421 allocno_n_refs[allocno] += REG_N_REFS (i);
422 if (allocno_live_length[allocno] < REG_LIVE_LENGTH (i))
423 allocno_live_length[allocno] = REG_LIVE_LENGTH (i);
38398762
RK
424 }
425
1d56e983
RS
426 /* Calculate amount of usage of each hard reg by pseudos
427 allocated by local-alloc. This is to see if we want to
428 override it. */
4c9a05bc
RK
429 bzero ((char *) local_reg_live_length, sizeof local_reg_live_length);
430 bzero ((char *) local_reg_n_refs, sizeof local_reg_n_refs);
1d56e983
RS
431 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
432 if (reg_allocno[i] < 0 && reg_renumber[i] >= 0)
433 {
34e56753
RS
434 int regno = reg_renumber[i];
435 int endregno = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
436 int j;
437
438 for (j = regno; j < endregno; j++)
439 {
b1f21e0a
MM
440 local_reg_n_refs[j] += REG_N_REFS (i);
441 local_reg_live_length[j] += REG_LIVE_LENGTH (i);
34e56753 442 }
1d56e983 443 }
34e56753 444
1d56e983
RS
445 /* We can't override local-alloc for a reg used not just by local-alloc. */
446 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
447 if (regs_ever_live[i])
448 local_reg_n_refs[i] = 0;
449
0dfa4517
RK
450 /* Likewise for regs used in a SCRATCH. */
451 for (i = 0; i < scratch_list_length; i++)
452 if (scratch_list[i])
453 {
454 int regno = REGNO (scratch_list[i]);
455 int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch_list[i]));
456 int j;
457
458 for (j = regno; j < lim; j++)
459 local_reg_n_refs[j] = 0;
460 }
461
38398762
RK
462 /* Allocate the space for the conflict and preference tables and
463 initialize them. */
464
465 hard_reg_conflicts
466 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
4c9a05bc 467 bzero ((char *) hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
38398762
RK
468
469 hard_reg_preferences
470 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
4c9a05bc 471 bzero ((char *) hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
38398762
RK
472
473 hard_reg_copy_preferences
474 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
4c9a05bc
RK
475 bzero ((char *) hard_reg_copy_preferences,
476 max_allocno * sizeof (HARD_REG_SET));
38398762
RK
477
478 hard_reg_full_preferences
479 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
4c9a05bc
RK
480 bzero ((char *) hard_reg_full_preferences,
481 max_allocno * sizeof (HARD_REG_SET));
38398762
RK
482
483 regs_someone_prefers
484 = (HARD_REG_SET *) alloca (max_allocno * sizeof (HARD_REG_SET));
4c9a05bc 485 bzero ((char *) regs_someone_prefers, max_allocno * sizeof (HARD_REG_SET));
38398762
RK
486
487 allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
488
b1ec3c92
CH
489 conflicts = (INT_TYPE *) alloca (max_allocno * allocno_row_words
490 * sizeof (INT_TYPE));
4c9a05bc
RK
491 bzero ((char *) conflicts,
492 max_allocno * allocno_row_words * sizeof (INT_TYPE));
38398762 493
b1ec3c92 494 allocnos_live = (INT_TYPE *) alloca (allocno_row_words * sizeof (INT_TYPE));
38398762
RK
495
496 /* If there is work to be done (at least one reg to allocate),
497 perform global conflict analysis and allocate the regs. */
498
499 if (max_allocno > 0)
500 {
501 /* Scan all the insns and compute the conflicts among allocnos
502 and between allocnos and hard regs. */
503
504 global_conflicts ();
505
506 /* Eliminate conflicts between pseudos and eliminable registers. If
507 the register is not eliminated, the pseudo won't really be able to
508 live in the eliminable register, so the conflict doesn't matter.
509 If we do eliminate the register, the conflict will no longer exist.
1d56e983
RS
510 So in either case, we can ignore the conflict. Likewise for
511 preferences. */
38398762
RK
512
513 for (i = 0; i < max_allocno; i++)
1d56e983
RS
514 {
515 AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
516 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
517 eliminable_regset);
518 AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
519 }
38398762
RK
520
521 /* Try to expand the preferences by merging them between allocnos. */
522
523 expand_preferences ();
524
525 /* Determine the order to allocate the remaining pseudo registers. */
526
527 allocno_order = (int *) alloca (max_allocno * sizeof (int));
528 for (i = 0; i < max_allocno; i++)
529 allocno_order[i] = i;
530
531 /* Default the size to 1, since allocno_compare uses it to divide by.
532 Also convert allocno_live_length of zero to -1. A length of zero
533 can occur when all the registers for that allocno have reg_live_length
534 equal to -2. In this case, we want to make an allocno, but not
535 allocate it. So avoid the divide-by-zero and set it to a low
536 priority. */
537
538 for (i = 0; i < max_allocno; i++)
539 {
540 if (allocno_size[i] == 0)
541 allocno_size[i] = 1;
542 if (allocno_live_length[i] == 0)
543 allocno_live_length[i] = -1;
544 }
545
546 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
547
548 prune_preferences ();
549
550 if (file)
551 dump_conflicts (file);
552
553 /* Try allocating them, one by one, in that order,
554 except for parameters marked with reg_live_length[regno] == -2. */
555
556 for (i = 0; i < max_allocno; i++)
b1f21e0a 557 if (REG_LIVE_LENGTH (allocno_reg[allocno_order[i]]) >= 0)
38398762
RK
558 {
559 /* If we have more than one register class,
560 first try allocating in the class that is cheapest
561 for this pseudo-reg. If that fails, try any reg. */
562 if (N_REG_CLASSES > 1)
563 {
1d56e983 564 find_reg (allocno_order[i], HARD_CONST (0), 0, 0, 0);
38398762
RK
565 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
566 continue;
567 }
b1ec3c92 568 if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
1d56e983 569 find_reg (allocno_order[i], HARD_CONST (0), 1, 0, 0);
38398762
RK
570 }
571 }
572
573 /* Do the reloads now while the allocno data still exist, so that we can
574 try to assign new hard regs to any pseudo regs that are spilled. */
575
7e860cf7
RS
576#if 0 /* We need to eliminate regs even if there is no rtl code,
577 for the sake of debugging information. */
38398762 578 if (n_basic_blocks > 0)
7e860cf7 579#endif
ab40ad2b 580 return reload (get_insns (), 1, file);
38398762
RK
581}
582
583/* Sort predicate for ordering the allocnos.
584 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
585
586static int
daa6f17d
RK
587allocno_compare (v1p, v2p)
588 const GENERIC_PTR v1p;
589 const GENERIC_PTR v2p;
38398762 590{
daa6f17d 591 int v1 = *(int *)v1p, v2 = *(int *)v2p;
38398762
RK
592 /* Note that the quotient will never be bigger than
593 the value of floor_log2 times the maximum number of
594 times a register can occur in one insn (surely less than 100).
595 Multiplying this by 10000 can't overflow. */
596 register int pri1
daa6f17d
RK
597 = (((double) (floor_log2 (allocno_n_refs[v1]) * allocno_n_refs[v1])
598 / allocno_live_length[v1])
599 * 10000 * allocno_size[v1]);
38398762 600 register int pri2
daa6f17d
RK
601 = (((double) (floor_log2 (allocno_n_refs[v2]) * allocno_n_refs[v2])
602 / allocno_live_length[v2])
603 * 10000 * allocno_size[v2]);
38398762
RK
604 if (pri2 - pri1)
605 return pri2 - pri1;
606
607 /* If regs are equally good, sort by allocno,
608 so that the results of qsort leave nothing to chance. */
daa6f17d 609 return v1 - v2;
38398762
RK
610}
611\f
612/* Scan the rtl code and record all conflicts and register preferences in the
613 conflict matrices and preference tables. */
614
615static void
616global_conflicts ()
617{
618 register int b, i;
619 register rtx insn;
620 short *block_start_allocnos;
621
622 /* Make a vector that mark_reg_{store,clobber} will store in. */
623 regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2);
624
625 block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
626
627 for (b = 0; b < n_basic_blocks; b++)
628 {
4c9a05bc 629 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
38398762
RK
630
631 /* Initialize table of registers currently live
632 to the state at the beginning of this basic block.
633 This also marks the conflicts among them.
634
635 For pseudo-regs, there is only one bit for each one
636 no matter how many hard regs it occupies.
637 This is ok; we know the size from PSEUDO_REGNO_SIZE.
638 For explicit hard regs, we cannot know the size that way
639 since one hard reg can be used with various sizes.
640 Therefore, we must require that all the hard regs
641 implicitly live as part of a multi-word hard reg
642 are explicitly marked in basic_block_live_at_start. */
643
644 {
38398762
RK
645 register regset old = basic_block_live_at_start[b];
646 int ax = 0;
647
916b1701
MM
648 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
649 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
650 {
651 register int a = reg_allocno[i];
652 if (a >= 0)
653 {
654 SET_ALLOCNO_LIVE (a);
655 block_start_allocnos[ax++] = a;
656 }
657 else if ((a = reg_renumber[i]) >= 0)
658 mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
659 });
38398762
RK
660
661 /* Record that each allocno now live conflicts with each other
662 allocno now live, and with each hard reg now live. */
663
664 record_conflicts (block_start_allocnos, ax);
665 }
666
667 insn = basic_block_head[b];
668
669 /* Scan the code of this basic block, noting which allocnos
670 and hard regs are born or die. When one is born,
671 record a conflict with all others currently live. */
672
673 while (1)
674 {
675 register RTX_CODE code = GET_CODE (insn);
676 register rtx link;
677
678 /* Make regs_set an empty set. */
679
680 n_regs_set = 0;
681
682 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
683 {
38398762
RK
684
685#if 0
2049526b 686 int i = 0;
38398762
RK
687 for (link = REG_NOTES (insn);
688 link && i < NUM_NO_CONFLICT_PAIRS;
689 link = XEXP (link, 1))
690 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
691 {
692 no_conflict_pairs[i].allocno1
693 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
694 no_conflict_pairs[i].allocno2
695 = reg_allocno[REGNO (XEXP (link, 0))];
696 i++;
697 }
698#endif /* 0 */
699
700 /* Mark any registers clobbered by INSN as live,
701 so they conflict with the inputs. */
702
703 note_stores (PATTERN (insn), mark_reg_clobber);
704
705 /* Mark any registers dead after INSN as dead now. */
706
707 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
708 if (REG_NOTE_KIND (link) == REG_DEAD)
709 mark_reg_death (XEXP (link, 0));
710
711 /* Mark any registers set in INSN as live,
712 and mark them as conflicting with all other live regs.
713 Clobbers are processed again, so they conflict with
714 the registers that are set. */
715
716 note_stores (PATTERN (insn), mark_reg_store);
717
718#ifdef AUTO_INC_DEC
719 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
720 if (REG_NOTE_KIND (link) == REG_INC)
b1ec3c92 721 mark_reg_store (XEXP (link, 0), NULL_RTX);
38398762
RK
722#endif
723
333e0f7d
RS
724 /* If INSN has multiple outputs, then any reg that dies here
725 and is used inside of an output
726 must conflict with the other outputs. */
727
728 if (GET_CODE (PATTERN (insn)) == PARALLEL && !single_set (insn))
729 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
730 if (REG_NOTE_KIND (link) == REG_DEAD)
731 {
732 int used_in_output = 0;
733 int i;
734 rtx reg = XEXP (link, 0);
735
736 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
737 {
738 rtx set = XVECEXP (PATTERN (insn), 0, i);
739 if (GET_CODE (set) == SET
740 && GET_CODE (SET_DEST (set)) != REG
741 && !rtx_equal_p (reg, SET_DEST (set))
742 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
743 used_in_output = 1;
744 }
745 if (used_in_output)
746 mark_reg_conflicts (reg);
747 }
748
38398762
RK
749 /* Mark any registers set in INSN and then never used. */
750
751 while (n_regs_set > 0)
752 if (find_regno_note (insn, REG_UNUSED,
753 REGNO (regs_set[--n_regs_set])))
754 mark_reg_death (regs_set[n_regs_set]);
755 }
756
757 if (insn == basic_block_end[b])
758 break;
759 insn = NEXT_INSN (insn);
760 }
761 }
762}
763/* Expand the preference information by looking for cases where one allocno
764 dies in an insn that sets an allocno. If those two allocnos don't conflict,
765 merge any preferences between those allocnos. */
766
767static void
768expand_preferences ()
769{
770 rtx insn;
771 rtx link;
772 rtx set;
773
774 /* We only try to handle the most common cases here. Most of the cases
775 where this wins are reg-reg copies. */
776
777 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
778 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
779 && (set = single_set (insn)) != 0
780 && GET_CODE (SET_DEST (set)) == REG
781 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
782 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
783 if (REG_NOTE_KIND (link) == REG_DEAD
784 && GET_CODE (XEXP (link, 0)) == REG
785 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
786 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
787 reg_allocno[REGNO (XEXP (link, 0))])
788 && ! CONFLICTP (reg_allocno[REGNO (XEXP (link, 0))],
789 reg_allocno[REGNO (SET_DEST (set))]))
790 {
791 int a1 = reg_allocno[REGNO (SET_DEST (set))];
792 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
793
794 if (XEXP (link, 0) == SET_SRC (set))
795 {
796 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
797 hard_reg_copy_preferences[a2]);
798 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
799 hard_reg_copy_preferences[a1]);
800 }
801
802 IOR_HARD_REG_SET (hard_reg_preferences[a1],
803 hard_reg_preferences[a2]);
804 IOR_HARD_REG_SET (hard_reg_preferences[a2],
805 hard_reg_preferences[a1]);
806 IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
807 hard_reg_full_preferences[a2]);
808 IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
809 hard_reg_full_preferences[a1]);
810 }
811}
812\f
813/* Prune the preferences for global registers to exclude registers that cannot
814 be used.
815
816 Compute `regs_someone_prefers', which is a bitmask of the hard registers
817 that are preferred by conflicting registers of lower priority. If possible,
818 we will avoid using these registers. */
819
820static void
821prune_preferences ()
822{
823 int i, j;
824 int allocno;
825
826 /* Scan least most important to most important.
827 For each allocno, remove from preferences registers that cannot be used,
828 either because of conflicts or register type. Then compute all registers
d45cf215 829 preferred by each lower-priority register that conflicts. */
38398762
RK
830
831 for (i = max_allocno - 1; i >= 0; i--)
832 {
833 HARD_REG_SET temp;
834
835 allocno = allocno_order[i];
836 COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
837
838 if (allocno_calls_crossed[allocno] == 0)
839 IOR_HARD_REG_SET (temp, fixed_reg_set);
840 else
841 IOR_HARD_REG_SET (temp, call_used_reg_set);
842
843 IOR_COMPL_HARD_REG_SET
844 (temp,
845 reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
846
847 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
848 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
849 AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
850
851 CLEAR_HARD_REG_SET (regs_someone_prefers[allocno]);
852
853 /* Merge in the preferences of lower-priority registers (they have
854 already been pruned). If we also prefer some of those registers,
855 don't exclude them unless we are of a smaller size (in which case
856 we want to give the lower-priority allocno the first chance for
857 these registers). */
858 for (j = i + 1; j < max_allocno; j++)
99fd4012
RK
859 if (CONFLICTP (allocno, allocno_order[j])
860 || CONFLICTP (allocno_order[j], allocno))
38398762
RK
861 {
862 COPY_HARD_REG_SET (temp,
863 hard_reg_full_preferences[allocno_order[j]]);
864 if (allocno_size[allocno_order[j]] <= allocno_size[allocno])
865 AND_COMPL_HARD_REG_SET (temp,
866 hard_reg_full_preferences[allocno]);
867
868 IOR_HARD_REG_SET (regs_someone_prefers[allocno], temp);
869 }
870 }
871}
872\f
873/* Assign a hard register to ALLOCNO; look for one that is the beginning
874 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
875 The registers marked in PREFREGS are tried first.
876
877 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
878 be used for this allocation.
879
b1ec3c92
CH
880 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
881 Otherwise ignore that preferred class and use the alternate class.
38398762
RK
882
883 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
884 will have to be saved and restored at calls.
885
1d56e983
RS
886 RETRYING is nonzero if this is called from retry_global_alloc.
887
38398762
RK
888 If we find one, record it in reg_renumber.
889 If not, do nothing. */
890
891static void
b1ec3c92 892find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
38398762
RK
893 int allocno;
894 HARD_REG_SET losers;
b1ec3c92 895 int alt_regs_p;
38398762 896 int accept_call_clobbered;
1d56e983 897 int retrying;
38398762
RK
898{
899 register int i, best_reg, pass;
900#ifdef HARD_REG_SET
901 register /* Declare it register if it's a scalar. */
902#endif
1d56e983 903 HARD_REG_SET used, used1, used2;
38398762 904
b1ec3c92
CH
905 enum reg_class class = (alt_regs_p
906 ? reg_alternate_class (allocno_reg[allocno])
907 : reg_preferred_class (allocno_reg[allocno]));
38398762
RK
908 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
909
910 if (accept_call_clobbered)
911 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
912 else if (allocno_calls_crossed[allocno] == 0)
913 COPY_HARD_REG_SET (used1, fixed_reg_set);
914 else
915 COPY_HARD_REG_SET (used1, call_used_reg_set);
916
917 /* Some registers should not be allocated in global-alloc. */
918 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
919 if (losers)
920 IOR_HARD_REG_SET (used1, losers);
921
922 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1d56e983
RS
923 COPY_HARD_REG_SET (used2, used1);
924
38398762
RK
925 IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
926
d546b10a 927#ifdef CLASS_CANNOT_CHANGE_SIZE
b1f21e0a 928 if (REG_CHANGES_SIZE (allocno_reg[allocno]))
d546b10a
RK
929 IOR_HARD_REG_SET (used1,
930 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
931#endif
932
38398762 933 /* Try each hard reg to see if it fits. Do this in two passes.
d45cf215 934 In the first pass, skip registers that are preferred by some other pseudo
38398762
RK
935 to give it a better chance of getting one of those registers. Only if
936 we can't get a register when excluding those do we take one of them.
937 However, we never allocate a register for the first time in pass 0. */
938
939 COPY_HARD_REG_SET (used, used1);
940 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
941 IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
942
943 best_reg = -1;
944 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
945 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
946 pass++)
947 {
948 if (pass == 1)
949 COPY_HARD_REG_SET (used, used1);
950 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
951 {
952#ifdef REG_ALLOC_ORDER
953 int regno = reg_alloc_order[i];
954#else
955 int regno = i;
956#endif
957 if (! TEST_HARD_REG_BIT (used, regno)
958 && HARD_REGNO_MODE_OK (regno, mode))
959 {
960 register int j;
961 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
962 for (j = regno + 1;
963 (j < lim
964 && ! TEST_HARD_REG_BIT (used, j));
965 j++);
966 if (j == lim)
967 {
968 best_reg = regno;
969 break;
970 }
971#ifndef REG_ALLOC_ORDER
972 i = j; /* Skip starting points we know will lose */
973#endif
974 }
975 }
976 }
977
978 /* See if there is a preferred register with the same class as the register
979 we allocated above. Making this restriction prevents register
980 preferencing from creating worse register allocation.
981
982 Remove from the preferred registers and conflicting registers. Note that
983 additional conflicts may have been added after `prune_preferences' was
984 called.
985
986 First do this for those register with copy preferences, then all
987 preferred registers. */
988
989 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
990 GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
991 reg_class_contents[(int) NO_REGS], no_copy_prefs);
992
993 if (best_reg >= 0)
994 {
995 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
996 if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
997 && HARD_REGNO_MODE_OK (i, mode)
998 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
999 || reg_class_subset_p (REGNO_REG_CLASS (i),
1000 REGNO_REG_CLASS (best_reg))
1001 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1002 REGNO_REG_CLASS (i))))
1003 {
1004 register int j;
1005 register int lim = i + HARD_REGNO_NREGS (i, mode);
1006 for (j = i + 1;
1007 (j < lim
1008 && ! TEST_HARD_REG_BIT (used, j)
1009 && (REGNO_REG_CLASS (j)
1010 == REGNO_REG_CLASS (best_reg + (j - i))
1011 || reg_class_subset_p (REGNO_REG_CLASS (j),
1012 REGNO_REG_CLASS (best_reg + (j - i)))
1013 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1014 REGNO_REG_CLASS (j))));
1015 j++);
1016 if (j == lim)
1017 {
1018 best_reg = i;
1019 goto no_prefs;
1020 }
1021 }
1022 }
1023 no_copy_prefs:
1024
1025 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1026 GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1027 reg_class_contents[(int) NO_REGS], no_prefs);
1028
1029 if (best_reg >= 0)
1030 {
1031 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1032 if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1033 && HARD_REGNO_MODE_OK (i, mode)
1034 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1035 || reg_class_subset_p (REGNO_REG_CLASS (i),
1036 REGNO_REG_CLASS (best_reg))
1037 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1038 REGNO_REG_CLASS (i))))
1039 {
1040 register int j;
1041 register int lim = i + HARD_REGNO_NREGS (i, mode);
1042 for (j = i + 1;
1043 (j < lim
1044 && ! TEST_HARD_REG_BIT (used, j)
1045 && (REGNO_REG_CLASS (j)
1046 == REGNO_REG_CLASS (best_reg + (j - i))
1047 || reg_class_subset_p (REGNO_REG_CLASS (j),
1048 REGNO_REG_CLASS (best_reg + (j - i)))
1049 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1050 REGNO_REG_CLASS (j))));
1051 j++);
1052 if (j == lim)
1053 {
1054 best_reg = i;
1055 break;
1056 }
1057 }
1058 }
1059 no_prefs:
1060
cfcf04a6
RK
1061 /* If we haven't succeeded yet, try with caller-saves.
1062 We need not check to see if the current function has nonlocal
1063 labels because we don't put any pseudos that are live over calls in
1064 registers in that case. */
1065
1d56e983
RS
1066 if (flag_caller_saves && best_reg < 0)
1067 {
1068 /* Did not find a register. If it would be profitable to
1069 allocate a call-clobbered register and save and restore it
1070 around calls, do that. */
1071 if (! accept_call_clobbered
1072 && allocno_calls_crossed[allocno] != 0
1073 && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1074 allocno_calls_crossed[allocno]))
1075 {
6cad67d2
JL
1076 HARD_REG_SET new_losers;
1077 if (! losers)
1078 CLEAR_HARD_REG_SET (new_losers);
1079 else
1080 COPY_HARD_REG_SET (new_losers, losers);
1081
1082 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1083 find_reg (allocno, new_losers, alt_regs_p, 1, retrying);
1d56e983
RS
1084 if (reg_renumber[allocno_reg[allocno]] >= 0)
1085 {
1086 caller_save_needed = 1;
1087 return;
1088 }
1089 }
1090 }
1091
1092 /* If we haven't succeeded yet,
1093 see if some hard reg that conflicts with us
1094 was utilized poorly by local-alloc.
1095 If so, kick out the regs that were put there by local-alloc
1096 so we can use it instead. */
1097 if (best_reg < 0 && !retrying
1098 /* Let's not bother with multi-reg allocnos. */
1099 && allocno_size[allocno] == 1)
1100 {
1101 /* Count from the end, to find the least-used ones first. */
1102 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
17a0a76d
RK
1103 {
1104#ifdef REG_ALLOC_ORDER
1105 int regno = reg_alloc_order[i];
1106#else
1107 int regno = i;
1108#endif
34e56753 1109
17a0a76d
RK
1110 if (local_reg_n_refs[regno] != 0
1111 /* Don't use a reg no good for this pseudo. */
1112 && ! TEST_HARD_REG_BIT (used2, regno)
d546b10a
RK
1113 && HARD_REGNO_MODE_OK (regno, mode)
1114#ifdef CLASS_CANNOT_CHANGE_SIZE
b1f21e0a 1115 && ! (REG_CHANGES_SIZE (allocno_reg[allocno])
d546b10a
RK
1116 && (TEST_HARD_REG_BIT
1117 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1118 regno)))
1119#endif
1120 )
17a0a76d 1121 {
7a17c588
JW
1122 /* We explicitly evaluate the divide results into temporary
1123 variables so as to avoid excess precision problems that occur
1124 on a i386-unknown-sysv4.2 (unixware) host. */
1125
1126 double tmp1 = ((double) local_reg_n_refs[regno]
1127 / local_reg_live_length[regno]);
1128 double tmp2 = ((double) allocno_n_refs[allocno]
1129 / allocno_live_length[allocno]);
1130
1131 if (tmp1 < tmp2)
1132 {
1133 /* Hard reg REGNO was used less in total by local regs
1134 than it would be used by this one allocno! */
1135 int k;
1136 for (k = 0; k < max_regno; k++)
1137 if (reg_renumber[k] >= 0)
1138 {
1139 int r = reg_renumber[k];
1140 int endregno
1141 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
34e56753 1142
7a17c588
JW
1143 if (regno >= r && regno < endregno)
1144 reg_renumber[k] = -1;
1145 }
17a0a76d 1146
7a17c588
JW
1147 best_reg = regno;
1148 break;
1149 }
17a0a76d
RK
1150 }
1151 }
1d56e983
RS
1152 }
1153
38398762
RK
1154 /* Did we find a register? */
1155
1156 if (best_reg >= 0)
1157 {
1158 register int lim, j;
1159 HARD_REG_SET this_reg;
1160
1161 /* Yes. Record it as the hard register of this pseudo-reg. */
1162 reg_renumber[allocno_reg[allocno]] = best_reg;
1163 /* Also of any pseudo-regs that share with it. */
1164 if (reg_may_share[allocno_reg[allocno]])
1165 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1166 if (reg_allocno[j] == allocno)
1167 reg_renumber[j] = best_reg;
1168
1169 /* Make a set of the hard regs being allocated. */
1170 CLEAR_HARD_REG_SET (this_reg);
1171 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1172 for (j = best_reg; j < lim; j++)
1173 {
1174 SET_HARD_REG_BIT (this_reg, j);
1175 SET_HARD_REG_BIT (regs_used_so_far, j);
1d56e983
RS
1176 /* This is no longer a reg used just by local regs. */
1177 local_reg_n_refs[j] = 0;
38398762
RK
1178 }
1179 /* For each other pseudo-reg conflicting with this one,
1180 mark it as conflicting with the hard regs this one occupies. */
1181 lim = allocno;
1182 for (j = 0; j < max_allocno; j++)
1183 if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
1184 {
1185 IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1186 }
1187 }
38398762
RK
1188}
1189\f
1190/* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1191 Perhaps it had previously seemed not worth a hard reg,
1192 or perhaps its old hard reg has been commandeered for reloads.
1193 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1194 they do not appear to be allocated.
1195 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1196
1197void
1198retry_global_alloc (regno, forbidden_regs)
1199 int regno;
1200 HARD_REG_SET forbidden_regs;
1201{
1202 int allocno = reg_allocno[regno];
1203 if (allocno >= 0)
1204 {
1205 /* If we have more than one register class,
1206 first try allocating in the class that is cheapest
1207 for this pseudo-reg. If that fails, try any reg. */
1208 if (N_REG_CLASSES > 1)
1d56e983 1209 find_reg (allocno, forbidden_regs, 0, 0, 1);
38398762 1210 if (reg_renumber[regno] < 0
b1ec3c92 1211 && reg_alternate_class (regno) != NO_REGS)
1d56e983 1212 find_reg (allocno, forbidden_regs, 1, 0, 1);
38398762
RK
1213
1214 /* If we found a register, modify the RTL for the register to
1215 show the hard register, and mark that register live. */
1216 if (reg_renumber[regno] >= 0)
1217 {
1218 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1219 mark_home_live (regno);
1220 }
1221 }
1222}
1223\f
1224/* Record a conflict between register REGNO
1225 and everything currently live.
1226 REGNO must not be a pseudo reg that was allocated
1227 by local_alloc; such numbers must be translated through
1228 reg_renumber before calling here. */
1229
1230static void
1231record_one_conflict (regno)
1232 int regno;
1233{
1234 register int j;
1235
1236 if (regno < FIRST_PSEUDO_REGISTER)
1237 /* When a hard register becomes live,
1238 record conflicts with live pseudo regs. */
1239 for (j = 0; j < max_allocno; j++)
1240 {
1241 if (ALLOCNO_LIVE_P (j))
1242 SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1243 }
1244 else
1245 /* When a pseudo-register becomes live,
1246 record conflicts first with hard regs,
1247 then with other pseudo regs. */
1248 {
1249 register int ialloc = reg_allocno[regno];
1250 register int ialloc_prod = ialloc * allocno_row_words;
1251 IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1252 for (j = allocno_row_words - 1; j >= 0; j--)
1253 {
1254#if 0
1255 int k;
1256 for (k = 0; k < n_no_conflict_pairs; k++)
1257 if (! ((j == no_conflict_pairs[k].allocno1
1258 && ialloc == no_conflict_pairs[k].allocno2)
1259 ||
1260 (j == no_conflict_pairs[k].allocno2
1261 && ialloc == no_conflict_pairs[k].allocno1)))
1262#endif /* 0 */
1263 conflicts[ialloc_prod + j] |= allocnos_live[j];
1264 }
1265 }
1266}
1267
1268/* Record all allocnos currently live as conflicting
1269 with each other and with all hard regs currently live.
1270 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1271 are currently live. Their bits are also flagged in allocnos_live. */
1272
1273static void
1274record_conflicts (allocno_vec, len)
1275 register short *allocno_vec;
1276 register int len;
1277{
1278 register int allocno;
1279 register int j;
1280 register int ialloc_prod;
1281
1282 while (--len >= 0)
1283 {
1284 allocno = allocno_vec[len];
1285 ialloc_prod = allocno * allocno_row_words;
1286 IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1287 for (j = allocno_row_words - 1; j >= 0; j--)
1288 conflicts[ialloc_prod + j] |= allocnos_live[j];
1289 }
1290}
1291\f
1292/* Handle the case where REG is set by the insn being scanned,
1293 during the forward scan to accumulate conflicts.
1294 Store a 1 in regs_live or allocnos_live for this register, record how many
1295 consecutive hardware registers it actually needs,
1296 and record a conflict with all other registers already live.
1297
1298 Note that even if REG does not remain alive after this insn,
1299 we must mark it here as live, to ensure a conflict between
1300 REG and any other regs set in this insn that really do live.
1301 This is because those other regs could be considered after this.
1302
1303 REG might actually be something other than a register;
1304 if so, we do nothing.
1305
1306 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1307 a REG_INC note was found for it).
1308
1309 CLOBBERs are processed here by calling mark_reg_clobber. */
1310
1311static void
1312mark_reg_store (orig_reg, setter)
1313 rtx orig_reg, setter;
1314{
1315 register int regno;
1316 register rtx reg = orig_reg;
1317
1318 /* WORD is which word of a multi-register group is being stored.
1319 For the case where the store is actually into a SUBREG of REG.
1320 Except we don't use it; I believe the entire REG needs to be
1321 made live. */
1322 int word = 0;
1323
1324 if (GET_CODE (reg) == SUBREG)
1325 {
1326 word = SUBREG_WORD (reg);
1327 reg = SUBREG_REG (reg);
1328 }
1329
1330 if (GET_CODE (reg) != REG)
1331 return;
1332
1333 if (setter && GET_CODE (setter) == CLOBBER)
1334 {
1335 /* A clobber of a register should be processed here too. */
1336 mark_reg_clobber (orig_reg, setter);
1337 return;
1338 }
1339
1340 regs_set[n_regs_set++] = reg;
1341
1342 if (setter)
1343 set_preference (reg, SET_SRC (setter));
1344
1345 regno = REGNO (reg);
1346
1347 if (reg_renumber[regno] >= 0)
1348 regno = reg_renumber[regno] /* + word */;
1349
1350 /* Either this is one of the max_allocno pseudo regs not allocated,
1351 or it is or has a hardware reg. First handle the pseudo-regs. */
1352 if (regno >= FIRST_PSEUDO_REGISTER)
1353 {
1354 if (reg_allocno[regno] >= 0)
1355 {
1356 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1357 record_one_conflict (regno);
1358 }
1359 }
1360 /* Handle hardware regs (and pseudos allocated to hard regs). */
1361 else if (! fixed_regs[regno])
1362 {
1363 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1364 while (regno < last)
1365 {
1366 record_one_conflict (regno);
1367 SET_HARD_REG_BIT (hard_regs_live, regno);
1368 regno++;
1369 }
1370 }
1371}
1372\f
1373/* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1374
1375static void
1376mark_reg_clobber (reg, setter)
1377 rtx reg, setter;
1378{
1379 register int regno;
1380
1381 /* WORD is which word of a multi-register group is being stored.
1382 For the case where the store is actually into a SUBREG of REG.
1383 Except we don't use it; I believe the entire REG needs to be
1384 made live. */
1385 int word = 0;
1386
1387 if (GET_CODE (setter) != CLOBBER)
1388 return;
1389
1390 if (GET_CODE (reg) == SUBREG)
1391 {
1392 word = SUBREG_WORD (reg);
1393 reg = SUBREG_REG (reg);
1394 }
1395
1396 if (GET_CODE (reg) != REG)
1397 return;
1398
1399 regs_set[n_regs_set++] = reg;
1400
1401 regno = REGNO (reg);
1402
1403 if (reg_renumber[regno] >= 0)
1404 regno = reg_renumber[regno] /* + word */;
1405
1406 /* Either this is one of the max_allocno pseudo regs not allocated,
1407 or it is or has a hardware reg. First handle the pseudo-regs. */
1408 if (regno >= FIRST_PSEUDO_REGISTER)
1409 {
1410 if (reg_allocno[regno] >= 0)
1411 {
1412 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1413 record_one_conflict (regno);
1414 }
1415 }
1416 /* Handle hardware regs (and pseudos allocated to hard regs). */
1417 else if (! fixed_regs[regno])
1418 {
1419 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1420 while (regno < last)
1421 {
1422 record_one_conflict (regno);
1423 SET_HARD_REG_BIT (hard_regs_live, regno);
1424 regno++;
1425 }
1426 }
333e0f7d
RS
1427}
1428
1429/* Record that REG has conflicts with all the regs currently live.
1430 Do not mark REG itself as live. */
1431
1432static void
1433mark_reg_conflicts (reg)
1434 rtx reg;
1435{
1436 register int regno;
1437
1438 if (GET_CODE (reg) == SUBREG)
1439 reg = SUBREG_REG (reg);
1440
1441 if (GET_CODE (reg) != REG)
1442 return;
1443
1444 regno = REGNO (reg);
1445
1446 if (reg_renumber[regno] >= 0)
1447 regno = reg_renumber[regno];
1448
1449 /* Either this is one of the max_allocno pseudo regs not allocated,
1450 or it is or has a hardware reg. First handle the pseudo-regs. */
1451 if (regno >= FIRST_PSEUDO_REGISTER)
1452 {
1453 if (reg_allocno[regno] >= 0)
1454 record_one_conflict (regno);
1455 }
1456 /* Handle hardware regs (and pseudos allocated to hard regs). */
1457 else if (! fixed_regs[regno])
1458 {
1459 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1460 while (regno < last)
1461 {
1462 record_one_conflict (regno);
1463 regno++;
1464 }
1465 }
38398762
RK
1466}
1467\f
1468/* Mark REG as being dead (following the insn being scanned now).
1469 Store a 0 in regs_live or allocnos_live for this register. */
1470
1471static void
1472mark_reg_death (reg)
1473 rtx reg;
1474{
1475 register int regno = REGNO (reg);
1476
1477 /* For pseudo reg, see if it has been assigned a hardware reg. */
1478 if (reg_renumber[regno] >= 0)
1479 regno = reg_renumber[regno];
1480
1481 /* Either this is one of the max_allocno pseudo regs not allocated,
1482 or it is a hardware reg. First handle the pseudo-regs. */
1483 if (regno >= FIRST_PSEUDO_REGISTER)
1484 {
1485 if (reg_allocno[regno] >= 0)
1486 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1487 }
1488 /* Handle hardware regs (and pseudos allocated to hard regs). */
1489 else if (! fixed_regs[regno])
1490 {
1491 /* Pseudo regs already assigned hardware regs are treated
1492 almost the same as explicit hardware regs. */
1493 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1494 while (regno < last)
1495 {
1496 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1497 regno++;
1498 }
1499 }
1500}
1501
1502/* Mark hard reg REGNO as currently live, assuming machine mode MODE
1503 for the value stored in it. MODE determines how many consecutive
1504 registers are actually in use. Do not record conflicts;
1505 it is assumed that the caller will do that. */
1506
1507static void
1508mark_reg_live_nc (regno, mode)
1509 register int regno;
1510 enum machine_mode mode;
1511{
1512 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1513 while (regno < last)
1514 {
1515 SET_HARD_REG_BIT (hard_regs_live, regno);
1516 regno++;
1517 }
1518}
1519\f
1520/* Try to set a preference for an allocno to a hard register.
1521 We are passed DEST and SRC which are the operands of a SET. It is known
1522 that SRC is a register. If SRC or the first operand of SRC is a register,
1523 try to set a preference. If one of the two is a hard register and the other
1524 is a pseudo-register, mark the preference.
1525
6dc42e49 1526 Note that we are not as aggressive as local-alloc in trying to tie a
38398762
RK
1527 pseudo-register to a hard register. */
1528
1529static void
1530set_preference (dest, src)
1531 rtx dest, src;
1532{
1533 int src_regno, dest_regno;
1534 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1535 to compensate for subregs in SRC or DEST. */
1536 int offset = 0;
1537 int i;
1538 int copy = 1;
1539
1540 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1541 src = XEXP (src, 0), copy = 0;
1542
1543 /* Get the reg number for both SRC and DEST.
1544 If neither is a reg, give up. */
1545
1546 if (GET_CODE (src) == REG)
1547 src_regno = REGNO (src);
1548 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1549 {
1550 src_regno = REGNO (SUBREG_REG (src));
1551 offset += SUBREG_WORD (src);
1552 }
1553 else
1554 return;
1555
1556 if (GET_CODE (dest) == REG)
1557 dest_regno = REGNO (dest);
1558 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1559 {
1560 dest_regno = REGNO (SUBREG_REG (dest));
1561 offset -= SUBREG_WORD (dest);
1562 }
1563 else
1564 return;
1565
1566 /* Convert either or both to hard reg numbers. */
1567
1568 if (reg_renumber[src_regno] >= 0)
1569 src_regno = reg_renumber[src_regno];
1570
1571 if (reg_renumber[dest_regno] >= 0)
1572 dest_regno = reg_renumber[dest_regno];
1573
1574 /* Now if one is a hard reg and the other is a global pseudo
1575 then give the other a preference. */
1576
1577 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1578 && reg_allocno[src_regno] >= 0)
1579 {
1580 dest_regno -= offset;
1581 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1582 {
1583 if (copy)
1584 SET_REGBIT (hard_reg_copy_preferences,
1585 reg_allocno[src_regno], dest_regno);
1586
1587 SET_REGBIT (hard_reg_preferences,
1588 reg_allocno[src_regno], dest_regno);
1589 for (i = dest_regno;
1590 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1591 i++)
1592 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1593 }
1594 }
1595
1596 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1597 && reg_allocno[dest_regno] >= 0)
1598 {
1599 src_regno += offset;
1600 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1601 {
1602 if (copy)
1603 SET_REGBIT (hard_reg_copy_preferences,
1604 reg_allocno[dest_regno], src_regno);
1605
1606 SET_REGBIT (hard_reg_preferences,
1607 reg_allocno[dest_regno], src_regno);
1608 for (i = src_regno;
1609 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1610 i++)
1611 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1612 }
1613 }
1614}
1615\f
1616/* Indicate that hard register number FROM was eliminated and replaced with
1617 an offset from hard register number TO. The status of hard registers live
1618 at the start of a basic block is updated by replacing a use of FROM with
1619 a use of TO. */
1620
1621void
1622mark_elimination (from, to)
1623 int from, to;
1624{
1625 int i;
1626
1627 for (i = 0; i < n_basic_blocks; i++)
916b1701 1628 if (REGNO_REG_SET_P (basic_block_live_at_start[i], from))
38398762 1629 {
916b1701
MM
1630 CLEAR_REGNO_REG_SET (basic_block_live_at_start[i], from);
1631 SET_REGNO_REG_SET (basic_block_live_at_start[i], to);
38398762
RK
1632 }
1633}
1634\f
1635/* Print debugging trace information if -greg switch is given,
1636 showing the information on which the allocation decisions are based. */
1637
1638static void
1639dump_conflicts (file)
1640 FILE *file;
1641{
1642 register int i;
1643 register int has_preferences;
1644 fprintf (file, ";; %d regs to allocate:", max_allocno);
1645 for (i = 0; i < max_allocno; i++)
1646 {
1647 int j;
1648 fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1649 for (j = 0; j < max_regno; j++)
1650 if (reg_allocno[j] == allocno_order[i]
1651 && j != allocno_reg[allocno_order[i]])
1652 fprintf (file, "+%d", j);
1653 if (allocno_size[allocno_order[i]] != 1)
1654 fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1655 }
1656 fprintf (file, "\n");
1657
1658 for (i = 0; i < max_allocno; i++)
1659 {
1660 register int j;
1661 fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1662 for (j = 0; j < max_allocno; j++)
1663 if (CONFLICTP (i, j) || CONFLICTP (j, i))
1664 fprintf (file, " %d", allocno_reg[j]);
1665 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1666 if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1667 fprintf (file, " %d", j);
1668 fprintf (file, "\n");
1669
1670 has_preferences = 0;
1671 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1672 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1673 has_preferences = 1;
1674
1675 if (! has_preferences)
1676 continue;
1677 fprintf (file, ";; %d preferences:", allocno_reg[i]);
1678 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1679 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1680 fprintf (file, " %d", j);
1681 fprintf (file, "\n");
1682 }
1683 fprintf (file, "\n");
1684}
1685
1686void
1687dump_global_regs (file)
1688 FILE *file;
1689{
1690 register int i, j;
1691
1692 fprintf (file, ";; Register dispositions:\n");
1693 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1694 if (reg_renumber[i] >= 0)
1695 {
1696 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1697 if (++j % 6 == 0)
1698 fprintf (file, "\n");
1699 }
1700
1701 fprintf (file, "\n\n;; Hard regs used: ");
1702 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1703 if (regs_ever_live[i])
1704 fprintf (file, " %d", i);
1705 fprintf (file, "\n\n");
1706}
This page took 0.54522 seconds and 5 git commands to generate.