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