]> gcc.gnu.org Git - gcc.git/blame - gcc/global.c
[multiple changes]
[gcc.git] / gcc / global.c
CommitLineData
38398762 1/* Allocate registers for pseudo-registers that span basic blocks.
1313ec9d 2 Copyright (C) 1987, 88, 91, 94, 96, 1997 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
ba3b3878
BK
489 /* We used to use alloca here, but the size of what it would try to
490 allocate would occasionally cause it to exceed the stack limit and
491 cause unpredictable core dumps. Some examples were > 2Mb in size. */
492 conflicts = (INT_TYPE *) xmalloc (max_allocno * allocno_row_words
493 * sizeof (INT_TYPE));
4c9a05bc
RK
494 bzero ((char *) conflicts,
495 max_allocno * allocno_row_words * sizeof (INT_TYPE));
38398762 496
b1ec3c92 497 allocnos_live = (INT_TYPE *) alloca (allocno_row_words * sizeof (INT_TYPE));
38398762
RK
498
499 /* If there is work to be done (at least one reg to allocate),
500 perform global conflict analysis and allocate the regs. */
501
502 if (max_allocno > 0)
503 {
504 /* Scan all the insns and compute the conflicts among allocnos
505 and between allocnos and hard regs. */
506
507 global_conflicts ();
508
509 /* Eliminate conflicts between pseudos and eliminable registers. If
510 the register is not eliminated, the pseudo won't really be able to
511 live in the eliminable register, so the conflict doesn't matter.
512 If we do eliminate the register, the conflict will no longer exist.
1d56e983
RS
513 So in either case, we can ignore the conflict. Likewise for
514 preferences. */
38398762
RK
515
516 for (i = 0; i < max_allocno; i++)
1d56e983
RS
517 {
518 AND_COMPL_HARD_REG_SET (hard_reg_conflicts[i], eliminable_regset);
519 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[i],
520 eliminable_regset);
521 AND_COMPL_HARD_REG_SET (hard_reg_preferences[i], eliminable_regset);
522 }
38398762
RK
523
524 /* Try to expand the preferences by merging them between allocnos. */
525
526 expand_preferences ();
527
528 /* Determine the order to allocate the remaining pseudo registers. */
529
530 allocno_order = (int *) alloca (max_allocno * sizeof (int));
531 for (i = 0; i < max_allocno; i++)
532 allocno_order[i] = i;
533
534 /* Default the size to 1, since allocno_compare uses it to divide by.
535 Also convert allocno_live_length of zero to -1. A length of zero
536 can occur when all the registers for that allocno have reg_live_length
537 equal to -2. In this case, we want to make an allocno, but not
538 allocate it. So avoid the divide-by-zero and set it to a low
539 priority. */
540
541 for (i = 0; i < max_allocno; i++)
542 {
543 if (allocno_size[i] == 0)
544 allocno_size[i] = 1;
545 if (allocno_live_length[i] == 0)
546 allocno_live_length[i] = -1;
547 }
548
549 qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
550
551 prune_preferences ();
552
553 if (file)
554 dump_conflicts (file);
555
556 /* Try allocating them, one by one, in that order,
557 except for parameters marked with reg_live_length[regno] == -2. */
558
559 for (i = 0; i < max_allocno; i++)
b1f21e0a 560 if (REG_LIVE_LENGTH (allocno_reg[allocno_order[i]]) >= 0)
38398762
RK
561 {
562 /* If we have more than one register class,
563 first try allocating in the class that is cheapest
564 for this pseudo-reg. If that fails, try any reg. */
565 if (N_REG_CLASSES > 1)
566 {
1d56e983 567 find_reg (allocno_order[i], HARD_CONST (0), 0, 0, 0);
38398762
RK
568 if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
569 continue;
570 }
b1ec3c92 571 if (reg_alternate_class (allocno_reg[allocno_order[i]]) != NO_REGS)
1d56e983 572 find_reg (allocno_order[i], HARD_CONST (0), 1, 0, 0);
38398762
RK
573 }
574 }
575
ba3b3878
BK
576 free (conflicts);
577
38398762
RK
578 /* Do the reloads now while the allocno data still exist, so that we can
579 try to assign new hard regs to any pseudo regs that are spilled. */
580
7e860cf7
RS
581#if 0 /* We need to eliminate regs even if there is no rtl code,
582 for the sake of debugging information. */
38398762 583 if (n_basic_blocks > 0)
7e860cf7 584#endif
ab40ad2b 585 return reload (get_insns (), 1, file);
38398762
RK
586}
587
588/* Sort predicate for ordering the allocnos.
589 Returns -1 (1) if *v1 should be allocated before (after) *v2. */
590
591static int
daa6f17d
RK
592allocno_compare (v1p, v2p)
593 const GENERIC_PTR v1p;
594 const GENERIC_PTR v2p;
38398762 595{
daa6f17d 596 int v1 = *(int *)v1p, v2 = *(int *)v2p;
38398762
RK
597 /* Note that the quotient will never be bigger than
598 the value of floor_log2 times the maximum number of
599 times a register can occur in one insn (surely less than 100).
600 Multiplying this by 10000 can't overflow. */
601 register int pri1
daa6f17d
RK
602 = (((double) (floor_log2 (allocno_n_refs[v1]) * allocno_n_refs[v1])
603 / allocno_live_length[v1])
604 * 10000 * allocno_size[v1]);
38398762 605 register int pri2
daa6f17d
RK
606 = (((double) (floor_log2 (allocno_n_refs[v2]) * allocno_n_refs[v2])
607 / allocno_live_length[v2])
608 * 10000 * allocno_size[v2]);
38398762
RK
609 if (pri2 - pri1)
610 return pri2 - pri1;
611
612 /* If regs are equally good, sort by allocno,
613 so that the results of qsort leave nothing to chance. */
daa6f17d 614 return v1 - v2;
38398762
RK
615}
616\f
617/* Scan the rtl code and record all conflicts and register preferences in the
618 conflict matrices and preference tables. */
619
620static void
621global_conflicts ()
622{
623 register int b, i;
624 register rtx insn;
625 short *block_start_allocnos;
626
627 /* Make a vector that mark_reg_{store,clobber} will store in. */
628 regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2);
629
630 block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
631
632 for (b = 0; b < n_basic_blocks; b++)
633 {
4c9a05bc 634 bzero ((char *) allocnos_live, allocno_row_words * sizeof (INT_TYPE));
38398762
RK
635
636 /* Initialize table of registers currently live
637 to the state at the beginning of this basic block.
638 This also marks the conflicts among them.
639
640 For pseudo-regs, there is only one bit for each one
641 no matter how many hard regs it occupies.
642 This is ok; we know the size from PSEUDO_REGNO_SIZE.
643 For explicit hard regs, we cannot know the size that way
644 since one hard reg can be used with various sizes.
645 Therefore, we must require that all the hard regs
646 implicitly live as part of a multi-word hard reg
647 are explicitly marked in basic_block_live_at_start. */
648
649 {
38398762
RK
650 register regset old = basic_block_live_at_start[b];
651 int ax = 0;
652
916b1701 653 REG_SET_TO_HARD_REG_SET (hard_regs_live, old);
1313ec9d 654 EXECUTE_IF_SET_IN_REG_SET (old, FIRST_PSEUDO_REGISTER, i,
916b1701
MM
655 {
656 register int a = reg_allocno[i];
657 if (a >= 0)
658 {
659 SET_ALLOCNO_LIVE (a);
660 block_start_allocnos[ax++] = a;
661 }
662 else if ((a = reg_renumber[i]) >= 0)
1313ec9d
RK
663 mark_reg_live_nc
664 (a, PSEUDO_REGNO_MODE (i));
916b1701 665 });
38398762
RK
666
667 /* Record that each allocno now live conflicts with each other
668 allocno now live, and with each hard reg now live. */
669
670 record_conflicts (block_start_allocnos, ax);
671 }
672
673 insn = basic_block_head[b];
674
675 /* Scan the code of this basic block, noting which allocnos
676 and hard regs are born or die. When one is born,
677 record a conflict with all others currently live. */
678
679 while (1)
680 {
681 register RTX_CODE code = GET_CODE (insn);
682 register rtx link;
683
684 /* Make regs_set an empty set. */
685
686 n_regs_set = 0;
687
688 if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
689 {
38398762
RK
690
691#if 0
2049526b 692 int i = 0;
38398762
RK
693 for (link = REG_NOTES (insn);
694 link && i < NUM_NO_CONFLICT_PAIRS;
695 link = XEXP (link, 1))
696 if (REG_NOTE_KIND (link) == REG_NO_CONFLICT)
697 {
698 no_conflict_pairs[i].allocno1
699 = reg_allocno[REGNO (SET_DEST (PATTERN (insn)))];
700 no_conflict_pairs[i].allocno2
701 = reg_allocno[REGNO (XEXP (link, 0))];
702 i++;
703 }
704#endif /* 0 */
705
706 /* Mark any registers clobbered by INSN as live,
707 so they conflict with the inputs. */
708
709 note_stores (PATTERN (insn), mark_reg_clobber);
710
711 /* Mark any registers dead after INSN as dead now. */
712
713 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
714 if (REG_NOTE_KIND (link) == REG_DEAD)
715 mark_reg_death (XEXP (link, 0));
716
717 /* Mark any registers set in INSN as live,
718 and mark them as conflicting with all other live regs.
719 Clobbers are processed again, so they conflict with
720 the registers that are set. */
721
722 note_stores (PATTERN (insn), mark_reg_store);
723
724#ifdef AUTO_INC_DEC
725 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
726 if (REG_NOTE_KIND (link) == REG_INC)
b1ec3c92 727 mark_reg_store (XEXP (link, 0), NULL_RTX);
38398762
RK
728#endif
729
333e0f7d
RS
730 /* If INSN has multiple outputs, then any reg that dies here
731 and is used inside of an output
732 must conflict with the other outputs. */
733
734 if (GET_CODE (PATTERN (insn)) == PARALLEL && !single_set (insn))
735 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
736 if (REG_NOTE_KIND (link) == REG_DEAD)
737 {
738 int used_in_output = 0;
739 int i;
740 rtx reg = XEXP (link, 0);
741
742 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
743 {
744 rtx set = XVECEXP (PATTERN (insn), 0, i);
745 if (GET_CODE (set) == SET
746 && GET_CODE (SET_DEST (set)) != REG
747 && !rtx_equal_p (reg, SET_DEST (set))
748 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
749 used_in_output = 1;
750 }
751 if (used_in_output)
752 mark_reg_conflicts (reg);
753 }
754
38398762
RK
755 /* Mark any registers set in INSN and then never used. */
756
757 while (n_regs_set > 0)
758 if (find_regno_note (insn, REG_UNUSED,
759 REGNO (regs_set[--n_regs_set])))
760 mark_reg_death (regs_set[n_regs_set]);
761 }
762
763 if (insn == basic_block_end[b])
764 break;
765 insn = NEXT_INSN (insn);
766 }
767 }
768}
769/* Expand the preference information by looking for cases where one allocno
770 dies in an insn that sets an allocno. If those two allocnos don't conflict,
771 merge any preferences between those allocnos. */
772
773static void
774expand_preferences ()
775{
776 rtx insn;
777 rtx link;
778 rtx set;
779
780 /* We only try to handle the most common cases here. Most of the cases
781 where this wins are reg-reg copies. */
782
783 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
784 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
785 && (set = single_set (insn)) != 0
786 && GET_CODE (SET_DEST (set)) == REG
787 && reg_allocno[REGNO (SET_DEST (set))] >= 0)
788 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
789 if (REG_NOTE_KIND (link) == REG_DEAD
790 && GET_CODE (XEXP (link, 0)) == REG
791 && reg_allocno[REGNO (XEXP (link, 0))] >= 0
792 && ! CONFLICTP (reg_allocno[REGNO (SET_DEST (set))],
793 reg_allocno[REGNO (XEXP (link, 0))])
794 && ! CONFLICTP (reg_allocno[REGNO (XEXP (link, 0))],
795 reg_allocno[REGNO (SET_DEST (set))]))
796 {
797 int a1 = reg_allocno[REGNO (SET_DEST (set))];
798 int a2 = reg_allocno[REGNO (XEXP (link, 0))];
799
800 if (XEXP (link, 0) == SET_SRC (set))
801 {
802 IOR_HARD_REG_SET (hard_reg_copy_preferences[a1],
803 hard_reg_copy_preferences[a2]);
804 IOR_HARD_REG_SET (hard_reg_copy_preferences[a2],
805 hard_reg_copy_preferences[a1]);
806 }
807
808 IOR_HARD_REG_SET (hard_reg_preferences[a1],
809 hard_reg_preferences[a2]);
810 IOR_HARD_REG_SET (hard_reg_preferences[a2],
811 hard_reg_preferences[a1]);
812 IOR_HARD_REG_SET (hard_reg_full_preferences[a1],
813 hard_reg_full_preferences[a2]);
814 IOR_HARD_REG_SET (hard_reg_full_preferences[a2],
815 hard_reg_full_preferences[a1]);
816 }
817}
818\f
819/* Prune the preferences for global registers to exclude registers that cannot
820 be used.
821
822 Compute `regs_someone_prefers', which is a bitmask of the hard registers
823 that are preferred by conflicting registers of lower priority. If possible,
824 we will avoid using these registers. */
825
826static void
827prune_preferences ()
828{
829 int i, j;
830 int allocno;
831
832 /* Scan least most important to most important.
833 For each allocno, remove from preferences registers that cannot be used,
834 either because of conflicts or register type. Then compute all registers
d45cf215 835 preferred by each lower-priority register that conflicts. */
38398762
RK
836
837 for (i = max_allocno - 1; i >= 0; i--)
838 {
839 HARD_REG_SET temp;
840
841 allocno = allocno_order[i];
842 COPY_HARD_REG_SET (temp, hard_reg_conflicts[allocno]);
843
844 if (allocno_calls_crossed[allocno] == 0)
845 IOR_HARD_REG_SET (temp, fixed_reg_set);
846 else
847 IOR_HARD_REG_SET (temp, call_used_reg_set);
848
849 IOR_COMPL_HARD_REG_SET
850 (temp,
851 reg_class_contents[(int) reg_preferred_class (allocno_reg[allocno])]);
852
853 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], temp);
854 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], temp);
855 AND_COMPL_HARD_REG_SET (hard_reg_full_preferences[allocno], temp);
856
857 CLEAR_HARD_REG_SET (regs_someone_prefers[allocno]);
858
859 /* Merge in the preferences of lower-priority registers (they have
860 already been pruned). If we also prefer some of those registers,
861 don't exclude them unless we are of a smaller size (in which case
862 we want to give the lower-priority allocno the first chance for
863 these registers). */
864 for (j = i + 1; j < max_allocno; j++)
99fd4012
RK
865 if (CONFLICTP (allocno, allocno_order[j])
866 || CONFLICTP (allocno_order[j], allocno))
38398762
RK
867 {
868 COPY_HARD_REG_SET (temp,
869 hard_reg_full_preferences[allocno_order[j]]);
870 if (allocno_size[allocno_order[j]] <= allocno_size[allocno])
871 AND_COMPL_HARD_REG_SET (temp,
872 hard_reg_full_preferences[allocno]);
873
874 IOR_HARD_REG_SET (regs_someone_prefers[allocno], temp);
875 }
876 }
877}
878\f
879/* Assign a hard register to ALLOCNO; look for one that is the beginning
880 of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
881 The registers marked in PREFREGS are tried first.
882
883 LOSERS, if non-zero, is a HARD_REG_SET indicating registers that cannot
884 be used for this allocation.
885
b1ec3c92
CH
886 If ALT_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
887 Otherwise ignore that preferred class and use the alternate class.
38398762
RK
888
889 If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
890 will have to be saved and restored at calls.
891
1d56e983
RS
892 RETRYING is nonzero if this is called from retry_global_alloc.
893
38398762
RK
894 If we find one, record it in reg_renumber.
895 If not, do nothing. */
896
897static void
b1ec3c92 898find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying)
38398762
RK
899 int allocno;
900 HARD_REG_SET losers;
b1ec3c92 901 int alt_regs_p;
38398762 902 int accept_call_clobbered;
1d56e983 903 int retrying;
38398762
RK
904{
905 register int i, best_reg, pass;
906#ifdef HARD_REG_SET
907 register /* Declare it register if it's a scalar. */
908#endif
1d56e983 909 HARD_REG_SET used, used1, used2;
38398762 910
b1ec3c92
CH
911 enum reg_class class = (alt_regs_p
912 ? reg_alternate_class (allocno_reg[allocno])
913 : reg_preferred_class (allocno_reg[allocno]));
38398762
RK
914 enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
915
916 if (accept_call_clobbered)
917 COPY_HARD_REG_SET (used1, call_fixed_reg_set);
918 else if (allocno_calls_crossed[allocno] == 0)
919 COPY_HARD_REG_SET (used1, fixed_reg_set);
920 else
921 COPY_HARD_REG_SET (used1, call_used_reg_set);
922
923 /* Some registers should not be allocated in global-alloc. */
924 IOR_HARD_REG_SET (used1, no_global_alloc_regs);
925 if (losers)
926 IOR_HARD_REG_SET (used1, losers);
927
928 IOR_COMPL_HARD_REG_SET (used1, reg_class_contents[(int) class]);
1d56e983
RS
929 COPY_HARD_REG_SET (used2, used1);
930
38398762
RK
931 IOR_HARD_REG_SET (used1, hard_reg_conflicts[allocno]);
932
d546b10a 933#ifdef CLASS_CANNOT_CHANGE_SIZE
b1f21e0a 934 if (REG_CHANGES_SIZE (allocno_reg[allocno]))
d546b10a
RK
935 IOR_HARD_REG_SET (used1,
936 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
937#endif
938
38398762 939 /* Try each hard reg to see if it fits. Do this in two passes.
d45cf215 940 In the first pass, skip registers that are preferred by some other pseudo
38398762
RK
941 to give it a better chance of getting one of those registers. Only if
942 we can't get a register when excluding those do we take one of them.
943 However, we never allocate a register for the first time in pass 0. */
944
945 COPY_HARD_REG_SET (used, used1);
946 IOR_COMPL_HARD_REG_SET (used, regs_used_so_far);
947 IOR_HARD_REG_SET (used, regs_someone_prefers[allocno]);
948
949 best_reg = -1;
950 for (i = FIRST_PSEUDO_REGISTER, pass = 0;
951 pass <= 1 && i >= FIRST_PSEUDO_REGISTER;
952 pass++)
953 {
954 if (pass == 1)
955 COPY_HARD_REG_SET (used, used1);
956 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
957 {
958#ifdef REG_ALLOC_ORDER
959 int regno = reg_alloc_order[i];
960#else
961 int regno = i;
962#endif
963 if (! TEST_HARD_REG_BIT (used, regno)
964 && HARD_REGNO_MODE_OK (regno, mode))
965 {
966 register int j;
967 register int lim = regno + HARD_REGNO_NREGS (regno, mode);
968 for (j = regno + 1;
969 (j < lim
970 && ! TEST_HARD_REG_BIT (used, j));
971 j++);
972 if (j == lim)
973 {
974 best_reg = regno;
975 break;
976 }
977#ifndef REG_ALLOC_ORDER
978 i = j; /* Skip starting points we know will lose */
979#endif
980 }
981 }
982 }
983
984 /* See if there is a preferred register with the same class as the register
985 we allocated above. Making this restriction prevents register
986 preferencing from creating worse register allocation.
987
988 Remove from the preferred registers and conflicting registers. Note that
989 additional conflicts may have been added after `prune_preferences' was
990 called.
991
992 First do this for those register with copy preferences, then all
993 preferred registers. */
994
995 AND_COMPL_HARD_REG_SET (hard_reg_copy_preferences[allocno], used);
996 GO_IF_HARD_REG_SUBSET (hard_reg_copy_preferences[allocno],
997 reg_class_contents[(int) NO_REGS], no_copy_prefs);
998
999 if (best_reg >= 0)
1000 {
1001 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1002 if (TEST_HARD_REG_BIT (hard_reg_copy_preferences[allocno], i)
1003 && HARD_REGNO_MODE_OK (i, mode)
1004 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1005 || reg_class_subset_p (REGNO_REG_CLASS (i),
1006 REGNO_REG_CLASS (best_reg))
1007 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1008 REGNO_REG_CLASS (i))))
1009 {
1010 register int j;
1011 register int lim = i + HARD_REGNO_NREGS (i, mode);
1012 for (j = i + 1;
1013 (j < lim
1014 && ! TEST_HARD_REG_BIT (used, j)
1015 && (REGNO_REG_CLASS (j)
1016 == REGNO_REG_CLASS (best_reg + (j - i))
1017 || reg_class_subset_p (REGNO_REG_CLASS (j),
1018 REGNO_REG_CLASS (best_reg + (j - i)))
1019 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1020 REGNO_REG_CLASS (j))));
1021 j++);
1022 if (j == lim)
1023 {
1024 best_reg = i;
1025 goto no_prefs;
1026 }
1027 }
1028 }
1029 no_copy_prefs:
1030
1031 AND_COMPL_HARD_REG_SET (hard_reg_preferences[allocno], used);
1032 GO_IF_HARD_REG_SUBSET (hard_reg_preferences[allocno],
1033 reg_class_contents[(int) NO_REGS], no_prefs);
1034
1035 if (best_reg >= 0)
1036 {
1037 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1038 if (TEST_HARD_REG_BIT (hard_reg_preferences[allocno], i)
1039 && HARD_REGNO_MODE_OK (i, mode)
1040 && (REGNO_REG_CLASS (i) == REGNO_REG_CLASS (best_reg)
1041 || reg_class_subset_p (REGNO_REG_CLASS (i),
1042 REGNO_REG_CLASS (best_reg))
1043 || reg_class_subset_p (REGNO_REG_CLASS (best_reg),
1044 REGNO_REG_CLASS (i))))
1045 {
1046 register int j;
1047 register int lim = i + HARD_REGNO_NREGS (i, mode);
1048 for (j = i + 1;
1049 (j < lim
1050 && ! TEST_HARD_REG_BIT (used, j)
1051 && (REGNO_REG_CLASS (j)
1052 == REGNO_REG_CLASS (best_reg + (j - i))
1053 || reg_class_subset_p (REGNO_REG_CLASS (j),
1054 REGNO_REG_CLASS (best_reg + (j - i)))
1055 || reg_class_subset_p (REGNO_REG_CLASS (best_reg + (j - i)),
1056 REGNO_REG_CLASS (j))));
1057 j++);
1058 if (j == lim)
1059 {
1060 best_reg = i;
1061 break;
1062 }
1063 }
1064 }
1065 no_prefs:
1066
cfcf04a6
RK
1067 /* If we haven't succeeded yet, try with caller-saves.
1068 We need not check to see if the current function has nonlocal
1069 labels because we don't put any pseudos that are live over calls in
1070 registers in that case. */
1071
1d56e983
RS
1072 if (flag_caller_saves && best_reg < 0)
1073 {
1074 /* Did not find a register. If it would be profitable to
1075 allocate a call-clobbered register and save and restore it
1076 around calls, do that. */
1077 if (! accept_call_clobbered
1078 && allocno_calls_crossed[allocno] != 0
1079 && CALLER_SAVE_PROFITABLE (allocno_n_refs[allocno],
1080 allocno_calls_crossed[allocno]))
1081 {
6cad67d2
JL
1082 HARD_REG_SET new_losers;
1083 if (! losers)
1084 CLEAR_HARD_REG_SET (new_losers);
1085 else
1086 COPY_HARD_REG_SET (new_losers, losers);
1087
1088 IOR_HARD_REG_SET(new_losers, losing_caller_save_reg_set);
1089 find_reg (allocno, new_losers, alt_regs_p, 1, retrying);
1d56e983
RS
1090 if (reg_renumber[allocno_reg[allocno]] >= 0)
1091 {
1092 caller_save_needed = 1;
1093 return;
1094 }
1095 }
1096 }
1097
1098 /* If we haven't succeeded yet,
1099 see if some hard reg that conflicts with us
1100 was utilized poorly by local-alloc.
1101 If so, kick out the regs that were put there by local-alloc
1102 so we can use it instead. */
1103 if (best_reg < 0 && !retrying
1104 /* Let's not bother with multi-reg allocnos. */
1105 && allocno_size[allocno] == 1)
1106 {
1107 /* Count from the end, to find the least-used ones first. */
1108 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
17a0a76d
RK
1109 {
1110#ifdef REG_ALLOC_ORDER
1111 int regno = reg_alloc_order[i];
1112#else
1113 int regno = i;
1114#endif
34e56753 1115
17a0a76d
RK
1116 if (local_reg_n_refs[regno] != 0
1117 /* Don't use a reg no good for this pseudo. */
1118 && ! TEST_HARD_REG_BIT (used2, regno)
d546b10a
RK
1119 && HARD_REGNO_MODE_OK (regno, mode)
1120#ifdef CLASS_CANNOT_CHANGE_SIZE
b1f21e0a 1121 && ! (REG_CHANGES_SIZE (allocno_reg[allocno])
d546b10a
RK
1122 && (TEST_HARD_REG_BIT
1123 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
1124 regno)))
1125#endif
1126 )
17a0a76d 1127 {
7a17c588
JW
1128 /* We explicitly evaluate the divide results into temporary
1129 variables so as to avoid excess precision problems that occur
1130 on a i386-unknown-sysv4.2 (unixware) host. */
1131
1132 double tmp1 = ((double) local_reg_n_refs[regno]
1133 / local_reg_live_length[regno]);
1134 double tmp2 = ((double) allocno_n_refs[allocno]
1135 / allocno_live_length[allocno]);
1136
1137 if (tmp1 < tmp2)
1138 {
1139 /* Hard reg REGNO was used less in total by local regs
1140 than it would be used by this one allocno! */
1141 int k;
1142 for (k = 0; k < max_regno; k++)
1143 if (reg_renumber[k] >= 0)
1144 {
1145 int r = reg_renumber[k];
1146 int endregno
1147 = r + HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (k));
34e56753 1148
7a17c588
JW
1149 if (regno >= r && regno < endregno)
1150 reg_renumber[k] = -1;
1151 }
17a0a76d 1152
7a17c588
JW
1153 best_reg = regno;
1154 break;
1155 }
17a0a76d
RK
1156 }
1157 }
1d56e983
RS
1158 }
1159
38398762
RK
1160 /* Did we find a register? */
1161
1162 if (best_reg >= 0)
1163 {
1164 register int lim, j;
1165 HARD_REG_SET this_reg;
1166
1167 /* Yes. Record it as the hard register of this pseudo-reg. */
1168 reg_renumber[allocno_reg[allocno]] = best_reg;
1169 /* Also of any pseudo-regs that share with it. */
1170 if (reg_may_share[allocno_reg[allocno]])
1171 for (j = FIRST_PSEUDO_REGISTER; j < max_regno; j++)
1172 if (reg_allocno[j] == allocno)
1173 reg_renumber[j] = best_reg;
1174
1175 /* Make a set of the hard regs being allocated. */
1176 CLEAR_HARD_REG_SET (this_reg);
1177 lim = best_reg + HARD_REGNO_NREGS (best_reg, mode);
1178 for (j = best_reg; j < lim; j++)
1179 {
1180 SET_HARD_REG_BIT (this_reg, j);
1181 SET_HARD_REG_BIT (regs_used_so_far, j);
1d56e983
RS
1182 /* This is no longer a reg used just by local regs. */
1183 local_reg_n_refs[j] = 0;
38398762
RK
1184 }
1185 /* For each other pseudo-reg conflicting with this one,
1186 mark it as conflicting with the hard regs this one occupies. */
1187 lim = allocno;
1188 for (j = 0; j < max_allocno; j++)
1189 if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
1190 {
1191 IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
1192 }
1193 }
38398762
RK
1194}
1195\f
1196/* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
1197 Perhaps it had previously seemed not worth a hard reg,
1198 or perhaps its old hard reg has been commandeered for reloads.
1199 FORBIDDEN_REGS indicates certain hard regs that may not be used, even if
1200 they do not appear to be allocated.
1201 If FORBIDDEN_REGS is zero, no regs are forbidden. */
1202
1203void
1204retry_global_alloc (regno, forbidden_regs)
1205 int regno;
1206 HARD_REG_SET forbidden_regs;
1207{
1208 int allocno = reg_allocno[regno];
1209 if (allocno >= 0)
1210 {
1211 /* If we have more than one register class,
1212 first try allocating in the class that is cheapest
1213 for this pseudo-reg. If that fails, try any reg. */
1214 if (N_REG_CLASSES > 1)
1d56e983 1215 find_reg (allocno, forbidden_regs, 0, 0, 1);
38398762 1216 if (reg_renumber[regno] < 0
b1ec3c92 1217 && reg_alternate_class (regno) != NO_REGS)
1d56e983 1218 find_reg (allocno, forbidden_regs, 1, 0, 1);
38398762
RK
1219
1220 /* If we found a register, modify the RTL for the register to
1221 show the hard register, and mark that register live. */
1222 if (reg_renumber[regno] >= 0)
1223 {
1224 REGNO (regno_reg_rtx[regno]) = reg_renumber[regno];
1225 mark_home_live (regno);
1226 }
1227 }
1228}
1229\f
1230/* Record a conflict between register REGNO
1231 and everything currently live.
1232 REGNO must not be a pseudo reg that was allocated
1233 by local_alloc; such numbers must be translated through
1234 reg_renumber before calling here. */
1235
1236static void
1237record_one_conflict (regno)
1238 int regno;
1239{
1240 register int j;
1241
1242 if (regno < FIRST_PSEUDO_REGISTER)
1243 /* When a hard register becomes live,
1244 record conflicts with live pseudo regs. */
1245 for (j = 0; j < max_allocno; j++)
1246 {
1247 if (ALLOCNO_LIVE_P (j))
1248 SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
1249 }
1250 else
1251 /* When a pseudo-register becomes live,
1252 record conflicts first with hard regs,
1253 then with other pseudo regs. */
1254 {
1255 register int ialloc = reg_allocno[regno];
1256 register int ialloc_prod = ialloc * allocno_row_words;
1257 IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
1258 for (j = allocno_row_words - 1; j >= 0; j--)
1259 {
1260#if 0
1261 int k;
1262 for (k = 0; k < n_no_conflict_pairs; k++)
1263 if (! ((j == no_conflict_pairs[k].allocno1
1264 && ialloc == no_conflict_pairs[k].allocno2)
1265 ||
1266 (j == no_conflict_pairs[k].allocno2
1267 && ialloc == no_conflict_pairs[k].allocno1)))
1268#endif /* 0 */
1269 conflicts[ialloc_prod + j] |= allocnos_live[j];
1270 }
1271 }
1272}
1273
1274/* Record all allocnos currently live as conflicting
1275 with each other and with all hard regs currently live.
1276 ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
1277 are currently live. Their bits are also flagged in allocnos_live. */
1278
1279static void
1280record_conflicts (allocno_vec, len)
1281 register short *allocno_vec;
1282 register int len;
1283{
1284 register int allocno;
1285 register int j;
1286 register int ialloc_prod;
1287
1288 while (--len >= 0)
1289 {
1290 allocno = allocno_vec[len];
1291 ialloc_prod = allocno * allocno_row_words;
1292 IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
1293 for (j = allocno_row_words - 1; j >= 0; j--)
1294 conflicts[ialloc_prod + j] |= allocnos_live[j];
1295 }
1296}
1297\f
1298/* Handle the case where REG is set by the insn being scanned,
1299 during the forward scan to accumulate conflicts.
1300 Store a 1 in regs_live or allocnos_live for this register, record how many
1301 consecutive hardware registers it actually needs,
1302 and record a conflict with all other registers already live.
1303
1304 Note that even if REG does not remain alive after this insn,
1305 we must mark it here as live, to ensure a conflict between
1306 REG and any other regs set in this insn that really do live.
1307 This is because those other regs could be considered after this.
1308
1309 REG might actually be something other than a register;
1310 if so, we do nothing.
1311
1312 SETTER is 0 if this register was modified by an auto-increment (i.e.,
1313 a REG_INC note was found for it).
1314
1315 CLOBBERs are processed here by calling mark_reg_clobber. */
1316
1317static void
1318mark_reg_store (orig_reg, setter)
1319 rtx orig_reg, setter;
1320{
1321 register int regno;
1322 register rtx reg = orig_reg;
1323
1324 /* WORD is which word of a multi-register group is being stored.
1325 For the case where the store is actually into a SUBREG of REG.
1326 Except we don't use it; I believe the entire REG needs to be
1327 made live. */
1328 int word = 0;
1329
1330 if (GET_CODE (reg) == SUBREG)
1331 {
1332 word = SUBREG_WORD (reg);
1333 reg = SUBREG_REG (reg);
1334 }
1335
1336 if (GET_CODE (reg) != REG)
1337 return;
1338
1339 if (setter && GET_CODE (setter) == CLOBBER)
1340 {
1341 /* A clobber of a register should be processed here too. */
1342 mark_reg_clobber (orig_reg, setter);
1343 return;
1344 }
1345
1346 regs_set[n_regs_set++] = reg;
1347
1348 if (setter)
1349 set_preference (reg, SET_SRC (setter));
1350
1351 regno = REGNO (reg);
1352
1353 if (reg_renumber[regno] >= 0)
1354 regno = reg_renumber[regno] /* + word */;
1355
1356 /* Either this is one of the max_allocno pseudo regs not allocated,
1357 or it is or has a hardware reg. First handle the pseudo-regs. */
1358 if (regno >= FIRST_PSEUDO_REGISTER)
1359 {
1360 if (reg_allocno[regno] >= 0)
1361 {
1362 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1363 record_one_conflict (regno);
1364 }
1365 }
1366 /* Handle hardware regs (and pseudos allocated to hard regs). */
1367 else if (! fixed_regs[regno])
1368 {
1369 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1370 while (regno < last)
1371 {
1372 record_one_conflict (regno);
1373 SET_HARD_REG_BIT (hard_regs_live, regno);
1374 regno++;
1375 }
1376 }
1377}
1378\f
1379/* Like mark_reg_set except notice just CLOBBERs; ignore SETs. */
1380
1381static void
1382mark_reg_clobber (reg, setter)
1383 rtx reg, setter;
1384{
1385 register int regno;
1386
1387 /* WORD is which word of a multi-register group is being stored.
1388 For the case where the store is actually into a SUBREG of REG.
1389 Except we don't use it; I believe the entire REG needs to be
1390 made live. */
1391 int word = 0;
1392
1393 if (GET_CODE (setter) != CLOBBER)
1394 return;
1395
1396 if (GET_CODE (reg) == SUBREG)
1397 {
1398 word = SUBREG_WORD (reg);
1399 reg = SUBREG_REG (reg);
1400 }
1401
1402 if (GET_CODE (reg) != REG)
1403 return;
1404
1405 regs_set[n_regs_set++] = reg;
1406
1407 regno = REGNO (reg);
1408
1409 if (reg_renumber[regno] >= 0)
1410 regno = reg_renumber[regno] /* + word */;
1411
1412 /* Either this is one of the max_allocno pseudo regs not allocated,
1413 or it is or has a hardware reg. First handle the pseudo-regs. */
1414 if (regno >= FIRST_PSEUDO_REGISTER)
1415 {
1416 if (reg_allocno[regno] >= 0)
1417 {
1418 SET_ALLOCNO_LIVE (reg_allocno[regno]);
1419 record_one_conflict (regno);
1420 }
1421 }
1422 /* Handle hardware regs (and pseudos allocated to hard regs). */
1423 else if (! fixed_regs[regno])
1424 {
1425 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1426 while (regno < last)
1427 {
1428 record_one_conflict (regno);
1429 SET_HARD_REG_BIT (hard_regs_live, regno);
1430 regno++;
1431 }
1432 }
333e0f7d
RS
1433}
1434
1435/* Record that REG has conflicts with all the regs currently live.
1436 Do not mark REG itself as live. */
1437
1438static void
1439mark_reg_conflicts (reg)
1440 rtx reg;
1441{
1442 register int regno;
1443
1444 if (GET_CODE (reg) == SUBREG)
1445 reg = SUBREG_REG (reg);
1446
1447 if (GET_CODE (reg) != REG)
1448 return;
1449
1450 regno = REGNO (reg);
1451
1452 if (reg_renumber[regno] >= 0)
1453 regno = reg_renumber[regno];
1454
1455 /* Either this is one of the max_allocno pseudo regs not allocated,
1456 or it is or has a hardware reg. First handle the pseudo-regs. */
1457 if (regno >= FIRST_PSEUDO_REGISTER)
1458 {
1459 if (reg_allocno[regno] >= 0)
1460 record_one_conflict (regno);
1461 }
1462 /* Handle hardware regs (and pseudos allocated to hard regs). */
1463 else if (! fixed_regs[regno])
1464 {
1465 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1466 while (regno < last)
1467 {
1468 record_one_conflict (regno);
1469 regno++;
1470 }
1471 }
38398762
RK
1472}
1473\f
1474/* Mark REG as being dead (following the insn being scanned now).
1475 Store a 0 in regs_live or allocnos_live for this register. */
1476
1477static void
1478mark_reg_death (reg)
1479 rtx reg;
1480{
1481 register int regno = REGNO (reg);
1482
1483 /* For pseudo reg, see if it has been assigned a hardware reg. */
1484 if (reg_renumber[regno] >= 0)
1485 regno = reg_renumber[regno];
1486
1487 /* Either this is one of the max_allocno pseudo regs not allocated,
1488 or it is a hardware reg. First handle the pseudo-regs. */
1489 if (regno >= FIRST_PSEUDO_REGISTER)
1490 {
1491 if (reg_allocno[regno] >= 0)
1492 CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
1493 }
1494 /* Handle hardware regs (and pseudos allocated to hard regs). */
1495 else if (! fixed_regs[regno])
1496 {
1497 /* Pseudo regs already assigned hardware regs are treated
1498 almost the same as explicit hardware regs. */
1499 register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1500 while (regno < last)
1501 {
1502 CLEAR_HARD_REG_BIT (hard_regs_live, regno);
1503 regno++;
1504 }
1505 }
1506}
1507
1508/* Mark hard reg REGNO as currently live, assuming machine mode MODE
1509 for the value stored in it. MODE determines how many consecutive
1510 registers are actually in use. Do not record conflicts;
1511 it is assumed that the caller will do that. */
1512
1513static void
1514mark_reg_live_nc (regno, mode)
1515 register int regno;
1516 enum machine_mode mode;
1517{
1518 register int last = regno + HARD_REGNO_NREGS (regno, mode);
1519 while (regno < last)
1520 {
1521 SET_HARD_REG_BIT (hard_regs_live, regno);
1522 regno++;
1523 }
1524}
1525\f
1526/* Try to set a preference for an allocno to a hard register.
1527 We are passed DEST and SRC which are the operands of a SET. It is known
1528 that SRC is a register. If SRC or the first operand of SRC is a register,
1529 try to set a preference. If one of the two is a hard register and the other
1530 is a pseudo-register, mark the preference.
1531
6dc42e49 1532 Note that we are not as aggressive as local-alloc in trying to tie a
38398762
RK
1533 pseudo-register to a hard register. */
1534
1535static void
1536set_preference (dest, src)
1537 rtx dest, src;
1538{
1539 int src_regno, dest_regno;
1540 /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
1541 to compensate for subregs in SRC or DEST. */
1542 int offset = 0;
1543 int i;
1544 int copy = 1;
1545
1546 if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
1547 src = XEXP (src, 0), copy = 0;
1548
1549 /* Get the reg number for both SRC and DEST.
1550 If neither is a reg, give up. */
1551
1552 if (GET_CODE (src) == REG)
1553 src_regno = REGNO (src);
1554 else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
1555 {
1556 src_regno = REGNO (SUBREG_REG (src));
1557 offset += SUBREG_WORD (src);
1558 }
1559 else
1560 return;
1561
1562 if (GET_CODE (dest) == REG)
1563 dest_regno = REGNO (dest);
1564 else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
1565 {
1566 dest_regno = REGNO (SUBREG_REG (dest));
1567 offset -= SUBREG_WORD (dest);
1568 }
1569 else
1570 return;
1571
1572 /* Convert either or both to hard reg numbers. */
1573
1574 if (reg_renumber[src_regno] >= 0)
1575 src_regno = reg_renumber[src_regno];
1576
1577 if (reg_renumber[dest_regno] >= 0)
1578 dest_regno = reg_renumber[dest_regno];
1579
1580 /* Now if one is a hard reg and the other is a global pseudo
1581 then give the other a preference. */
1582
1583 if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
1584 && reg_allocno[src_regno] >= 0)
1585 {
1586 dest_regno -= offset;
1587 if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
1588 {
1589 if (copy)
1590 SET_REGBIT (hard_reg_copy_preferences,
1591 reg_allocno[src_regno], dest_regno);
1592
1593 SET_REGBIT (hard_reg_preferences,
1594 reg_allocno[src_regno], dest_regno);
1595 for (i = dest_regno;
1596 i < dest_regno + HARD_REGNO_NREGS (dest_regno, GET_MODE (dest));
1597 i++)
1598 SET_REGBIT (hard_reg_full_preferences, reg_allocno[src_regno], i);
1599 }
1600 }
1601
1602 if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
1603 && reg_allocno[dest_regno] >= 0)
1604 {
1605 src_regno += offset;
1606 if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
1607 {
1608 if (copy)
1609 SET_REGBIT (hard_reg_copy_preferences,
1610 reg_allocno[dest_regno], src_regno);
1611
1612 SET_REGBIT (hard_reg_preferences,
1613 reg_allocno[dest_regno], src_regno);
1614 for (i = src_regno;
1615 i < src_regno + HARD_REGNO_NREGS (src_regno, GET_MODE (src));
1616 i++)
1617 SET_REGBIT (hard_reg_full_preferences, reg_allocno[dest_regno], i);
1618 }
1619 }
1620}
1621\f
1622/* Indicate that hard register number FROM was eliminated and replaced with
1623 an offset from hard register number TO. The status of hard registers live
1624 at the start of a basic block is updated by replacing a use of FROM with
1625 a use of TO. */
1626
1627void
1628mark_elimination (from, to)
1629 int from, to;
1630{
1631 int i;
1632
1633 for (i = 0; i < n_basic_blocks; i++)
916b1701 1634 if (REGNO_REG_SET_P (basic_block_live_at_start[i], from))
38398762 1635 {
916b1701
MM
1636 CLEAR_REGNO_REG_SET (basic_block_live_at_start[i], from);
1637 SET_REGNO_REG_SET (basic_block_live_at_start[i], to);
38398762
RK
1638 }
1639}
1640\f
1641/* Print debugging trace information if -greg switch is given,
1642 showing the information on which the allocation decisions are based. */
1643
1644static void
1645dump_conflicts (file)
1646 FILE *file;
1647{
1648 register int i;
1649 register int has_preferences;
1650 fprintf (file, ";; %d regs to allocate:", max_allocno);
1651 for (i = 0; i < max_allocno; i++)
1652 {
1653 int j;
1654 fprintf (file, " %d", allocno_reg[allocno_order[i]]);
1655 for (j = 0; j < max_regno; j++)
1656 if (reg_allocno[j] == allocno_order[i]
1657 && j != allocno_reg[allocno_order[i]])
1658 fprintf (file, "+%d", j);
1659 if (allocno_size[allocno_order[i]] != 1)
1660 fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
1661 }
1662 fprintf (file, "\n");
1663
1664 for (i = 0; i < max_allocno; i++)
1665 {
1666 register int j;
1667 fprintf (file, ";; %d conflicts:", allocno_reg[i]);
1668 for (j = 0; j < max_allocno; j++)
1669 if (CONFLICTP (i, j) || CONFLICTP (j, i))
1670 fprintf (file, " %d", allocno_reg[j]);
1671 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1672 if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
1673 fprintf (file, " %d", j);
1674 fprintf (file, "\n");
1675
1676 has_preferences = 0;
1677 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1678 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1679 has_preferences = 1;
1680
1681 if (! has_preferences)
1682 continue;
1683 fprintf (file, ";; %d preferences:", allocno_reg[i]);
1684 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1685 if (TEST_HARD_REG_BIT (hard_reg_preferences[i], j))
1686 fprintf (file, " %d", j);
1687 fprintf (file, "\n");
1688 }
1689 fprintf (file, "\n");
1690}
1691
1692void
1693dump_global_regs (file)
1694 FILE *file;
1695{
1696 register int i, j;
1697
1698 fprintf (file, ";; Register dispositions:\n");
1699 for (i = FIRST_PSEUDO_REGISTER, j = 0; i < max_regno; i++)
1700 if (reg_renumber[i] >= 0)
1701 {
1702 fprintf (file, "%d in %d ", i, reg_renumber[i]);
1703 if (++j % 6 == 0)
1704 fprintf (file, "\n");
1705 }
1706
1707 fprintf (file, "\n\n;; Hard regs used: ");
1708 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1709 if (regs_ever_live[i])
1710 fprintf (file, " %d", i);
1711 fprintf (file, "\n\n");
1712}
This page took 0.568143 seconds and 5 git commands to generate.