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