(alloc_qty_for_scratch): Can allocate likely-spilled register classes for SCRATCH.
[gcc.git] / gcc / local-alloc.c
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 1988, 1991, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 /* Allocation of hard register numbers to pseudo registers is done in
22 two passes. In this pass we consider only regs that are born and
23 die once within one basic block. We do this one basic block at a
24 time. Then the next pass allocates the registers that remain.
25 Two passes are used because this pass uses methods that work only
26 on linear code, but that do a better job than the general methods
27 used in global_alloc, and more quickly too.
28
29 The assignments made are recorded in the vector reg_renumber
30 whose space is allocated here. The rtl code itself is not altered.
31
32 We assign each instruction in the basic block a number
33 which is its order from the beginning of the block.
34 Then we can represent the lifetime of a pseudo register with
35 a pair of numbers, and check for conflicts easily.
36 We can record the availability of hard registers with a
37 HARD_REG_SET for each instruction. The HARD_REG_SET
38 contains 0 or 1 for each hard reg.
39
40 To avoid register shuffling, we tie registers together when one
41 dies by being copied into another, or dies in an instruction that
42 does arithmetic to produce another. The tied registers are
43 allocated as one. Registers with different reg class preferences
44 can never be tied unless the class preferred by one is a subclass
45 of the one preferred by the other.
46
47 Tying is represented with "quantity numbers".
48 A non-tied register is given a new quantity number.
49 Tied registers have the same quantity number.
50
51 We have provision to exempt registers, even when they are contained
52 within the block, that can be tied to others that are not contained in it.
53 This is so that global_alloc could process them both and tie them then.
54 But this is currently disabled since tying in global_alloc is not
55 yet implemented. */
56
57 #include <stdio.h>
58 #include "config.h"
59 #include "rtl.h"
60 #include "flags.h"
61 #include "basic-block.h"
62 #include "regs.h"
63 #include "hard-reg-set.h"
64 #include "insn-config.h"
65 #include "recog.h"
66 #include "output.h"
67 \f
68 /* Pseudos allocated here cannot be reallocated by global.c if the hard
69 register is used as a spill register. So we don't allocate such pseudos
70 here if their preferred class is likely to be used by spills.
71
72 On most machines, the appropriate test is if the class has one
73 register, so we default to that. */
74
75 #ifndef CLASS_LIKELY_SPILLED_P
76 #define CLASS_LIKELY_SPILLED_P(CLASS) (reg_class_size[(int) (CLASS)] == 1)
77 #endif
78
79 /* Next quantity number available for allocation. */
80
81 static int next_qty;
82
83 /* In all the following vectors indexed by quantity number. */
84
85 /* Element Q is the hard reg number chosen for quantity Q,
86 or -1 if none was found. */
87
88 static short *qty_phys_reg;
89
90 /* We maintain two hard register sets that indicate suggested hard registers
91 for each quantity. The first, qty_phys_copy_sugg, contains hard registers
92 that are tied to the quantity by a simple copy. The second contains all
93 hard registers that are tied to the quantity via an arithmetic operation.
94
95 The former register set is given priority for allocation. This tends to
96 eliminate copy insns. */
97
98 /* Element Q is a set of hard registers that are suggested for quantity Q by
99 copy insns. */
100
101 static HARD_REG_SET *qty_phys_copy_sugg;
102
103 /* Element Q is a set of hard registers that are suggested for quantity Q by
104 arithmetic insns. */
105
106 static HARD_REG_SET *qty_phys_sugg;
107
108 /* Element Q is non-zero if there is a suggested register in
109 qty_phys_copy_sugg. */
110
111 static char *qty_phys_has_copy_sugg;
112
113 /* Element Q is non-zero if there is a suggested register in qty_phys_sugg. */
114
115 static char *qty_phys_has_sugg;
116
117 /* Element Q is the number of refs to quantity Q. */
118
119 static int *qty_n_refs;
120
121 /* Element Q is a reg class contained in (smaller than) the
122 preferred classes of all the pseudo regs that are tied in quantity Q.
123 This is the preferred class for allocating that quantity. */
124
125 static enum reg_class *qty_min_class;
126
127 /* Insn number (counting from head of basic block)
128 where quantity Q was born. -1 if birth has not been recorded. */
129
130 static int *qty_birth;
131
132 /* Insn number (counting from head of basic block)
133 where quantity Q died. Due to the way tying is done,
134 and the fact that we consider in this pass only regs that die but once,
135 a quantity can die only once. Each quantity's life span
136 is a set of consecutive insns. -1 if death has not been recorded. */
137
138 static int *qty_death;
139
140 /* Number of words needed to hold the data in quantity Q.
141 This depends on its machine mode. It is used for these purposes:
142 1. It is used in computing the relative importances of qtys,
143 which determines the order in which we look for regs for them.
144 2. It is used in rules that prevent tying several registers of
145 different sizes in a way that is geometrically impossible
146 (see combine_regs). */
147
148 static int *qty_size;
149
150 /* This holds the mode of the registers that are tied to qty Q,
151 or VOIDmode if registers with differing modes are tied together. */
152
153 static enum machine_mode *qty_mode;
154
155 /* Number of times a reg tied to qty Q lives across a CALL_INSN. */
156
157 static int *qty_n_calls_crossed;
158
159 /* Register class within which we allocate qty Q if we can't get
160 its preferred class. */
161
162 static enum reg_class *qty_alternate_class;
163
164 /* Element Q is the SCRATCH expression for which this quantity is being
165 allocated or 0 if this quantity is allocating registers. */
166
167 static rtx *qty_scratch_rtx;
168
169 /* Element Q is the register number of one pseudo register whose
170 reg_qty value is Q, or -1 is this quantity is for a SCRATCH. This
171 register should be the head of the chain maintained in reg_next_in_qty. */
172
173 static int *qty_first_reg;
174
175 /* If (REG N) has been assigned a quantity number, is a register number
176 of another register assigned the same quantity number, or -1 for the
177 end of the chain. qty_first_reg point to the head of this chain. */
178
179 static int *reg_next_in_qty;
180
181 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
182 if it is >= 0,
183 of -1 if this register cannot be allocated by local-alloc,
184 or -2 if not known yet.
185
186 Note that if we see a use or death of pseudo register N with
187 reg_qty[N] == -2, register N must be local to the current block. If
188 it were used in more than one block, we would have reg_qty[N] == -1.
189 This relies on the fact that if reg_basic_block[N] is >= 0, register N
190 will not appear in any other block. We save a considerable number of
191 tests by exploiting this.
192
193 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
194 be referenced. */
195
196 static int *reg_qty;
197
198 /* The offset (in words) of register N within its quantity.
199 This can be nonzero if register N is SImode, and has been tied
200 to a subreg of a DImode register. */
201
202 static char *reg_offset;
203
204 /* Vector of substitutions of register numbers,
205 used to map pseudo regs into hardware regs.
206 This is set up as a result of register allocation.
207 Element N is the hard reg assigned to pseudo reg N,
208 or is -1 if no hard reg was assigned.
209 If N is a hard reg number, element N is N. */
210
211 short *reg_renumber;
212
213 /* Set of hard registers live at the current point in the scan
214 of the instructions in a basic block. */
215
216 static HARD_REG_SET regs_live;
217
218 /* Each set of hard registers indicates registers live at a particular
219 point in the basic block. For N even, regs_live_at[N] says which
220 hard registers are needed *after* insn N/2 (i.e., they may not
221 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
222
223 If an object is to conflict with the inputs of insn J but not the
224 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
225 if it is to conflict with the outputs of insn J but not the inputs of
226 insn J + 1, it is said to die at index J*2 + 1. */
227
228 static HARD_REG_SET *regs_live_at;
229
230 int *scratch_block;
231 rtx *scratch_list;
232 int scratch_list_length;
233 static int scratch_index;
234
235 /* Communicate local vars `insn_number' and `insn'
236 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
237 static int this_insn_number;
238 static rtx this_insn;
239
240 static void block_alloc ();
241 static void update_equiv_regs ();
242 static int no_conflict_p ();
243 static int combine_regs ();
244 static void wipe_dead_reg ();
245 static int find_free_reg ();
246 static void reg_is_born ();
247 static void reg_is_set ();
248 static void mark_life ();
249 static void post_mark_life ();
250 static int qty_compare ();
251 static int qty_compare_1 ();
252 static int reg_meets_class_p ();
253 static void update_qty_class ();
254 static int requires_inout_p ();
255 \f
256 /* Allocate a new quantity (new within current basic block)
257 for register number REGNO which is born at index BIRTH
258 within the block. MODE and SIZE are info on reg REGNO. */
259
260 static void
261 alloc_qty (regno, mode, size, birth)
262 int regno;
263 enum machine_mode mode;
264 int size, birth;
265 {
266 register int qty = next_qty++;
267
268 reg_qty[regno] = qty;
269 reg_offset[regno] = 0;
270 reg_next_in_qty[regno] = -1;
271
272 qty_first_reg[qty] = regno;
273 qty_size[qty] = size;
274 qty_mode[qty] = mode;
275 qty_birth[qty] = birth;
276 qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
277 qty_min_class[qty] = reg_preferred_class (regno);
278 qty_alternate_class[qty] = reg_alternate_class (regno);
279 qty_n_refs[qty] = reg_n_refs[regno];
280 }
281 \f
282 /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
283 used as operand N in INSN. We assume here that the SCRATCH is used in
284 a CLOBBER. */
285
286 static void
287 alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
288 rtx scratch;
289 int n;
290 rtx insn;
291 int insn_code_num, insn_number;
292 {
293 register int qty;
294 enum reg_class class;
295 char *p, c;
296 int i;
297
298 #ifdef REGISTER_CONSTRAINTS
299 /* If we haven't yet computed which alternative will be used, do so now.
300 Then set P to the constraints for that alternative. */
301 if (which_alternative == -1)
302 if (! constrain_operands (insn_code_num, 0))
303 return;
304
305 for (p = insn_operand_constraint[insn_code_num][n], i = 0;
306 *p && i < which_alternative; p++)
307 if (*p == ',')
308 i++;
309
310 /* Compute the class required for this SCRATCH. If we don't need a
311 register, the class will remain NO_REGS. If we guessed the alternative
312 number incorrectly, reload will fix things up for us. */
313
314 class = NO_REGS;
315 while ((c = *p++) != '\0' && c != ',')
316 switch (c)
317 {
318 case '=': case '+': case '?':
319 case '#': case '&': case '!':
320 case '*': case '%':
321 case '0': case '1': case '2': case '3': case '4':
322 case 'm': case '<': case '>': case 'V': case 'o':
323 case 'E': case 'F': case 'G': case 'H':
324 case 's': case 'i': case 'n':
325 case 'I': case 'J': case 'K': case 'L':
326 case 'M': case 'N': case 'O': case 'P':
327 #ifdef EXTRA_CONSTRAINT
328 case 'Q': case 'R': case 'S': case 'T': case 'U':
329 #endif
330 case 'p':
331 /* These don't say anything we care about. */
332 break;
333
334 case 'X':
335 /* We don't need to allocate this SCRATCH. */
336 return;
337
338 case 'g': case 'r':
339 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
340 break;
341
342 default:
343 class
344 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
345 break;
346 }
347
348 if (class == NO_REGS)
349 return;
350
351 #else /* REGISTER_CONSTRAINTS */
352
353 class = GENERAL_REGS;
354 #endif
355
356
357 qty = next_qty++;
358
359 qty_first_reg[qty] = -1;
360 qty_scratch_rtx[qty] = scratch;
361 qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
362 qty_mode[qty] = GET_MODE (scratch);
363 qty_birth[qty] = 2 * insn_number - 1;
364 qty_death[qty] = 2 * insn_number + 1;
365 qty_n_calls_crossed[qty] = 0;
366 qty_min_class[qty] = class;
367 qty_alternate_class[qty] = NO_REGS;
368 qty_n_refs[qty] = 1;
369 }
370 \f
371 /* Main entry point of this file. */
372
373 void
374 local_alloc ()
375 {
376 register int b, i;
377 int max_qty;
378
379 /* Leaf functions and non-leaf functions have different needs.
380 If defined, let the machine say what kind of ordering we
381 should use. */
382 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
383 ORDER_REGS_FOR_LOCAL_ALLOC;
384 #endif
385
386 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
387 registers. */
388 update_equiv_regs ();
389
390 /* This sets the maximum number of quantities we can have. Quantity
391 numbers start at zero and we can have one for each pseudo plus the
392 number of SCRATCHes in the largest block, in the worst case. */
393 max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
394
395 /* Allocate vectors of temporary data.
396 See the declarations of these variables, above,
397 for what they mean. */
398
399 /* There can be up to MAX_SCRATCH * N_BASIC_BLOCKS SCRATCHes to allocate.
400 Instead of allocating this much memory from now until the end of
401 reload, only allocate space for MAX_QTY SCRATCHes. If there are more
402 reload will allocate them. */
403
404 scratch_list_length = max_qty;
405 scratch_list = (rtx *) xmalloc (scratch_list_length * sizeof (rtx));
406 bzero (scratch_list, scratch_list_length * sizeof (rtx));
407 scratch_block = (int *) xmalloc (scratch_list_length * sizeof (int));
408 bzero (scratch_block, scratch_list_length * sizeof (int));
409 scratch_index = 0;
410
411 qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
412 qty_phys_copy_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
413 qty_phys_has_copy_sugg = (char *) alloca (max_qty * sizeof (char));
414 qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
415 qty_phys_has_sugg = (char *) alloca (max_qty * sizeof (char));
416 qty_birth = (int *) alloca (max_qty * sizeof (int));
417 qty_death = (int *) alloca (max_qty * sizeof (int));
418 qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
419 qty_first_reg = (int *) alloca (max_qty * sizeof (int));
420 qty_size = (int *) alloca (max_qty * sizeof (int));
421 qty_mode = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
422 qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
423 qty_min_class = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
424 qty_alternate_class = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
425 qty_n_refs = (int *) alloca (max_qty * sizeof (int));
426
427 reg_qty = (int *) alloca (max_regno * sizeof (int));
428 reg_offset = (char *) alloca (max_regno * sizeof (char));
429 reg_next_in_qty = (int *) alloca (max_regno * sizeof (int));
430
431 reg_renumber = (short *) oballoc (max_regno * sizeof (short));
432 for (i = 0; i < max_regno; i++)
433 reg_renumber[i] = -1;
434
435 /* Determine which pseudo-registers can be allocated by local-alloc.
436 In general, these are the registers used only in a single block and
437 which only die once. However, if a register's preferred class has only
438 a few entries, don't allocate this register here unless it is preferred
439 or nothing since retry_global_alloc won't be able to move it to
440 GENERAL_REGS if a reload register of this class is needed.
441
442 We need not be concerned with which block actually uses the register
443 since we will never see it outside that block. */
444
445 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
446 {
447 if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
448 && (reg_alternate_class (i) == NO_REGS
449 || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
450 reg_qty[i] = -2;
451 else
452 reg_qty[i] = -1;
453 }
454
455 /* Force loop below to initialize entire quantity array. */
456 next_qty = max_qty;
457
458 /* Allocate each block's local registers, block by block. */
459
460 for (b = 0; b < n_basic_blocks; b++)
461 {
462 /* NEXT_QTY indicates which elements of the `qty_...'
463 vectors might need to be initialized because they were used
464 for the previous block; it is set to the entire array before
465 block 0. Initialize those, with explicit loop if there are few,
466 else with bzero and bcopy. Do not initialize vectors that are
467 explicit set by `alloc_qty'. */
468
469 if (next_qty < 6)
470 {
471 for (i = 0; i < next_qty; i++)
472 {
473 qty_scratch_rtx[i] = 0;
474 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
475 qty_phys_has_copy_sugg[i] = 0;
476 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
477 qty_phys_has_sugg[i] = 0;
478 }
479 }
480 else
481 {
482 #define CLEAR(vector) \
483 bzero ((vector), (sizeof (*(vector))) * next_qty);
484
485 CLEAR (qty_scratch_rtx);
486 CLEAR (qty_phys_copy_sugg);
487 CLEAR (qty_phys_has_copy_sugg);
488 CLEAR (qty_phys_sugg);
489 CLEAR (qty_phys_has_sugg);
490 }
491
492 next_qty = 0;
493
494 block_alloc (b);
495 #ifdef USE_C_ALLOCA
496 alloca (0);
497 #endif
498 }
499 }
500 \f
501 /* Depth of loops we are in while in update_equiv_regs. */
502 static int loop_depth;
503
504 /* Used for communication between the following two functions: contains
505 a MEM that we wish to ensure remains unchanged. */
506 static rtx equiv_mem;
507
508 /* Set nonzero if EQUIV_MEM is modified. */
509 static int equiv_mem_modified;
510
511 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
512 Called via note_stores. */
513
514 static void
515 validate_equiv_mem_from_store (dest, set)
516 rtx dest;
517 rtx set;
518 {
519 if ((GET_CODE (dest) == REG
520 && reg_overlap_mentioned_p (dest, equiv_mem))
521 || (GET_CODE (dest) == MEM
522 && true_dependence (dest, equiv_mem)))
523 equiv_mem_modified = 1;
524 }
525
526 /* Verify that no store between START and the death of REG invalidates
527 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
528 by storing into an overlapping memory location, or with a non-const
529 CALL_INSN.
530
531 Return 1 if MEMREF remains valid. */
532
533 static int
534 validate_equiv_mem (start, reg, memref)
535 rtx start;
536 rtx reg;
537 rtx memref;
538 {
539 rtx insn;
540 rtx note;
541
542 equiv_mem = memref;
543 equiv_mem_modified = 0;
544
545 /* If the memory reference has side effects or is volatile, it isn't a
546 valid equivalence. */
547 if (side_effects_p (memref))
548 return 0;
549
550 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
551 {
552 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
553 continue;
554
555 if (find_reg_note (insn, REG_DEAD, reg))
556 return 1;
557
558 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
559 && ! CONST_CALL_P (insn))
560 return 0;
561
562 note_stores (PATTERN (insn), validate_equiv_mem_from_store);
563
564 /* If a register mentioned in MEMREF is modified via an
565 auto-increment, we lose the equivalence. Do the same if one
566 dies; although we could extend the life, it doesn't seem worth
567 the trouble. */
568
569 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
570 if ((REG_NOTE_KIND (note) == REG_INC
571 || REG_NOTE_KIND (note) == REG_DEAD)
572 && GET_CODE (XEXP (note, 0)) == REG
573 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
574 return 0;
575 }
576
577 return 0;
578 }
579 \f
580 /* TRUE if X references a memory location that would be affected by a store
581 to MEMREF. */
582
583 static int
584 memref_referenced_p (memref, x)
585 rtx x;
586 rtx memref;
587 {
588 int i, j;
589 char *fmt;
590 enum rtx_code code = GET_CODE (x);
591
592 switch (code)
593 {
594 case REG:
595 case CONST_INT:
596 case CONST:
597 case LABEL_REF:
598 case SYMBOL_REF:
599 case CONST_DOUBLE:
600 case PC:
601 case CC0:
602 case HIGH:
603 case LO_SUM:
604 return 0;
605
606 case MEM:
607 if (true_dependence (memref, x))
608 return 1;
609 break;
610
611 case SET:
612 /* If we are setting a MEM, it doesn't count (its address does), but any
613 other SET_DEST that has a MEM in it is referencing the MEM. */
614 if (GET_CODE (SET_DEST (x)) == MEM)
615 {
616 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
617 return 1;
618 }
619 else if (memref_referenced_p (memref, SET_DEST (x)))
620 return 1;
621
622 return memref_referenced_p (memref, SET_SRC (x));
623 }
624
625 fmt = GET_RTX_FORMAT (code);
626 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
627 switch (fmt[i])
628 {
629 case 'e':
630 if (memref_referenced_p (memref, XEXP (x, i)))
631 return 1;
632 break;
633 case 'E':
634 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
635 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
636 return 1;
637 break;
638 }
639
640 return 0;
641 }
642
643 /* TRUE if some insn in the range (START, END] references a memory location
644 that would be affected by a store to MEMREF. */
645
646 static int
647 memref_used_between_p (memref, start, end)
648 rtx memref;
649 rtx start;
650 rtx end;
651 {
652 rtx insn;
653
654 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
655 insn = NEXT_INSN (insn))
656 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
657 && memref_referenced_p (memref, PATTERN (insn)))
658 return 1;
659
660 return 0;
661 }
662 \f
663 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
664 in INSN.
665
666 Search forward to see if SRC dies before either it or DEST is modified,
667 but don't scan past the end of a basic block. If so, we can replace SRC
668 with DEST and let SRC die in INSN.
669
670 This will reduce the number of registers live in that range and may enable
671 DEST to be tied to SRC, thus often saving one register in addition to a
672 register-register copy. */
673
674 static void
675 optimize_reg_copy_1 (insn, dest, src)
676 rtx insn;
677 rtx dest;
678 rtx src;
679 {
680 rtx p, q;
681 rtx note;
682 rtx dest_death = 0;
683 int sregno = REGNO (src);
684 int dregno = REGNO (dest);
685
686 if (sregno == dregno
687 #ifdef SMALL_REGISTER_CLASSES
688 /* We don't want to mess with hard regs if register classes are small. */
689 || sregno < FIRST_PSEUDO_REGISTER || dregno < FIRST_PSEUDO_REGISTER
690 #endif
691 /* We don't see all updates to SP if they are in an auto-inc memory
692 reference, so we must disallow this optimization on them. */
693 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
694 return;
695
696 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
697 {
698 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
699 || (GET_CODE (p) == NOTE
700 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
701 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
702 break;
703
704 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
705 continue;
706
707 if (reg_set_p (src, p) || reg_set_p (dest, p)
708 /* Don't change a USE of a register. */
709 || (GET_CODE (PATTERN (p)) == USE
710 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
711 break;
712
713 /* See if all of SRC dies in P. This test is slightly more
714 conservative than it needs to be. */
715 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
716 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
717 {
718 int failed = 0;
719 int length = 0;
720 int d_length = 0;
721 int n_calls = 0;
722 int d_n_calls = 0;
723
724 /* We can do the optimization. Scan forward from INSN again,
725 replacing regs as we go. Set FAILED if a replacement can't
726 be done. In that case, we can't move the death note for SRC.
727 This should be rare. */
728
729 /* Set to stop at next insn. */
730 for (q = next_real_insn (insn);
731 q != next_real_insn (p);
732 q = next_real_insn (q))
733 {
734 if (reg_overlap_mentioned_p (src, PATTERN (q)))
735 {
736 /* If SRC is a hard register, we might miss some
737 overlapping registers with validate_replace_rtx,
738 so we would have to undo it. We can't if DEST is
739 present in the insn, so fail in that combination
740 of cases. */
741 if (sregno < FIRST_PSEUDO_REGISTER
742 && reg_mentioned_p (dest, PATTERN (q)))
743 failed = 1;
744
745 /* Replace all uses and make sure that the register
746 isn't still present. */
747 else if (validate_replace_rtx (src, dest, q)
748 && (sregno >= FIRST_PSEUDO_REGISTER
749 || ! reg_overlap_mentioned_p (src,
750 PATTERN (q))))
751 {
752 /* We assume that a register is used exactly once per
753 insn in the updates below. If this is not correct,
754 no great harm is done. */
755 if (sregno >= FIRST_PSEUDO_REGISTER)
756 reg_n_refs[sregno] -= loop_depth;
757 if (dregno >= FIRST_PSEUDO_REGISTER)
758 reg_n_refs[dregno] += loop_depth;
759 }
760 else
761 {
762 validate_replace_rtx (dest, src, q);
763 failed = 1;
764 }
765 }
766
767 /* Count the insns and CALL_INSNs passed. If we passed the
768 death note of DEST, show increased live length. */
769 length++;
770 if (dest_death)
771 d_length++;
772
773 if (GET_CODE (q) == CALL_INSN)
774 {
775 n_calls++;
776 if (dest_death)
777 d_n_calls++;
778 }
779
780 /* If DEST dies here, remove the death note and save it for
781 later. Make sure ALL of DEST dies here; again, this is
782 overly conservative. */
783 if (dest_death == 0
784 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
785 && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
786 remove_note (q, dest_death);
787 }
788
789 if (! failed)
790 {
791 if (sregno >= FIRST_PSEUDO_REGISTER)
792 {
793 reg_live_length[sregno] -= length;
794 reg_n_calls_crossed[sregno] -= n_calls;
795 }
796
797 if (dregno >= FIRST_PSEUDO_REGISTER)
798 {
799 reg_live_length[dregno] += d_length;
800 reg_n_calls_crossed[dregno] += d_n_calls;
801 }
802
803 /* Move death note of SRC from P to INSN. */
804 remove_note (p, note);
805 XEXP (note, 1) = REG_NOTES (insn);
806 REG_NOTES (insn) = note;
807 }
808
809 /* Put death note of DEST on P if we saw it die. */
810 if (dest_death)
811 {
812 XEXP (dest_death, 1) = REG_NOTES (p);
813 REG_NOTES (p) = dest_death;
814 }
815
816 return;
817 }
818
819 /* If SRC is a hard register which is set or killed in some other
820 way, we can't do this optimization. */
821 else if (sregno < FIRST_PSEUDO_REGISTER
822 && dead_or_set_p (p, src))
823 break;
824 }
825 }
826 \f
827 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
828 a sequence of insns that modify DEST followed by an insn that sets
829 SRC to DEST in which DEST dies, with no prior modification of DEST.
830 (There is no need to check if the insns in between actually modify
831 DEST. We should not have cases where DEST is not modified, but
832 the optimization is safe if no such modification is detected.)
833 In that case, we can replace all uses of DEST, starting with INSN and
834 ending with the set of SRC to DEST, with SRC. We do not do this
835 optimization if a CALL_INSN is crossed unless SRC already crosses a
836 call.
837
838 It is assumed that DEST and SRC are pseudos; it is too complicated to do
839 this for hard registers since the substitutions we may make might fail. */
840
841 static void
842 optimize_reg_copy_2 (insn, dest, src)
843 rtx insn;
844 rtx dest;
845 rtx src;
846 {
847 rtx p, q;
848 rtx set;
849 int sregno = REGNO (src);
850 int dregno = REGNO (dest);
851
852 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
853 {
854 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
855 || (GET_CODE (p) == NOTE
856 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
857 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
858 break;
859
860 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
861 continue;
862
863 set = single_set (p);
864 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
865 && find_reg_note (p, REG_DEAD, dest))
866 {
867 /* We can do the optimization. Scan forward from INSN again,
868 replacing regs as we go. */
869
870 /* Set to stop at next insn. */
871 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
872 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
873 {
874 if (reg_mentioned_p (dest, PATTERN (q)))
875 {
876 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
877
878 /* We assume that a register is used exactly once per
879 insn in the updates below. If this is not correct,
880 no great harm is done. */
881 reg_n_refs[dregno] -= loop_depth;
882 reg_n_refs[sregno] += loop_depth;
883 }
884
885
886 if (GET_CODE (q) == CALL_INSN)
887 {
888 reg_n_calls_crossed[dregno]--;
889 reg_n_calls_crossed[sregno]++;
890 }
891 }
892
893 remove_note (p, find_reg_note (p, REG_DEAD, dest));
894 reg_n_deaths[dregno]--;
895 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
896 reg_n_deaths[sregno]--;
897 return;
898 }
899
900 if (reg_set_p (src, p)
901 || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
902 break;
903 }
904 }
905 \f
906 /* Find registers that are equivalent to a single value throughout the
907 compilation (either because they can be referenced in memory or are set once
908 from a single constant). Lower their priority for a register.
909
910 If such a register is only referenced once, try substituting its value
911 into the using insn. If it succeeds, we can eliminate the register
912 completely. */
913
914 static void
915 update_equiv_regs ()
916 {
917 rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
918 rtx *reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
919 rtx insn;
920
921 bzero (reg_equiv_init_insn, max_regno * sizeof (rtx *));
922 bzero (reg_equiv_replacement, max_regno * sizeof (rtx *));
923
924 init_alias_analysis ();
925
926 loop_depth = 1;
927
928 /* Scan the insns and find which registers have equivalences. Do this
929 in a separate scan of the insns because (due to -fcse-follow-jumps)
930 a register can be set below its use. */
931 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
932 {
933 rtx note;
934 rtx set = single_set (insn);
935 rtx dest;
936 int regno;
937
938 if (GET_CODE (insn) == NOTE)
939 {
940 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
941 loop_depth++;
942 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
943 loop_depth--;
944 }
945
946 /* If this insn contains more (or less) than a single SET, ignore it. */
947 if (set == 0)
948 continue;
949
950 dest = SET_DEST (set);
951
952 /* If this sets a MEM to the contents of a REG that is only used
953 in a single basic block, see if the register is always equivalent
954 to that memory location and if moving the store from INSN to the
955 insn that set REG is safe. If so, put a REG_EQUIV note on the
956 initializing insn. */
957
958 if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
959 && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
960 && reg_basic_block[regno] >= 0
961 && reg_equiv_init_insn[regno] != 0
962 && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
963 dest)
964 && ! memref_used_between_p (SET_DEST (set),
965 reg_equiv_init_insn[regno], insn))
966 REG_NOTES (reg_equiv_init_insn[regno])
967 = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
968 REG_NOTES (reg_equiv_init_insn[regno]));
969
970 /* If this is a register-register copy where SRC is not dead, see if we
971 can optimize it. */
972 if (flag_expensive_optimizations && GET_CODE (dest) == REG
973 && GET_CODE (SET_SRC (set)) == REG
974 && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
975 optimize_reg_copy_1 (insn, dest, SET_SRC (set));
976
977 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
978 else if (flag_expensive_optimizations && GET_CODE (dest) == REG
979 && REGNO (dest) >= FIRST_PSEUDO_REGISTER
980 && GET_CODE (SET_SRC (set)) == REG
981 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
982 && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
983 optimize_reg_copy_2 (insn, dest, SET_SRC (set));
984
985 /* Otherwise, we only handle the case of a pseudo register being set
986 once. */
987 if (GET_CODE (dest) != REG
988 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
989 || reg_n_sets[regno] != 1)
990 continue;
991
992 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
993
994 /* Record this insn as initializing this register. */
995 reg_equiv_init_insn[regno] = insn;
996
997 /* If this register is known to be equal to a constant, record that
998 it is always equivalent to the constant. */
999 if (note && CONSTANT_P (XEXP (note, 0)))
1000 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
1001
1002 /* If this insn introduces a "constant" register, decrease the priority
1003 of that register. Record this insn if the register is only used once
1004 more and the equivalence value is the same as our source.
1005
1006 The latter condition is checked for two reasons: First, it is an
1007 indication that it may be more efficient to actually emit the insn
1008 as written (if no registers are available, reload will substitute
1009 the equivalence). Secondly, it avoids problems with any registers
1010 dying in this insn whose death notes would be missed.
1011
1012 If we don't have a REG_EQUIV note, see if this insn is loading
1013 a register used only in one basic block from a MEM. If so, and the
1014 MEM remains unchanged for the life of the register, add a REG_EQUIV
1015 note. */
1016
1017 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1018
1019 if (note == 0 && reg_basic_block[regno] >= 0
1020 && GET_CODE (SET_SRC (set)) == MEM
1021 && validate_equiv_mem (insn, dest, SET_SRC (set)))
1022 REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
1023 REG_NOTES (insn));
1024
1025 /* Don't mess with things live during setjmp. */
1026 if (note && reg_live_length[regno] >= 0)
1027 {
1028 int regno = REGNO (dest);
1029
1030 /* Note that the statement below does not affect the priority
1031 in local-alloc! */
1032 reg_live_length[regno] *= 2;
1033
1034 /* If the register is referenced exactly twice, meaning it is set
1035 once and used once, indicate that the reference may be replaced
1036 by the equivalence we computed above. If the register is only
1037 used in one basic block, this can't succeed or combine would
1038 have done it.
1039
1040 It would be nice to use "loop_depth * 2" in the compare
1041 below. Unfortunately, LOOP_DEPTH need not be constant within
1042 a basic block so this would be too complicated.
1043
1044 This case normally occurs when a parameter is read from memory
1045 and then used exactly once, not in a loop. */
1046
1047 if (reg_n_refs[regno] == 2
1048 && reg_basic_block[regno] < 0
1049 && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
1050 reg_equiv_replacement[regno] = SET_SRC (set);
1051 }
1052 }
1053
1054 /* Now scan all regs killed in an insn to see if any of them are registers
1055 only used that once. If so, see if we can replace the reference with
1056 the equivalent from. If we can, delete the initializing reference
1057 and this register will go away. */
1058 for (insn = next_active_insn (get_insns ());
1059 insn;
1060 insn = next_active_insn (insn))
1061 {
1062 rtx link;
1063
1064 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1065 if (REG_NOTE_KIND (link) == REG_DEAD
1066 /* Make sure this insn still refers to the register. */
1067 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1068 {
1069 int regno = REGNO (XEXP (link, 0));
1070
1071 if (reg_equiv_replacement[regno]
1072 && validate_replace_rtx (regno_reg_rtx[regno],
1073 reg_equiv_replacement[regno], insn))
1074 {
1075 rtx equiv_insn = reg_equiv_init_insn[regno];
1076
1077 remove_death (regno, insn);
1078 reg_n_refs[regno] = 0;
1079 PUT_CODE (equiv_insn, NOTE);
1080 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1081 NOTE_SOURCE_FILE (equiv_insn) = 0;
1082 }
1083 }
1084 }
1085 }
1086 \f
1087 /* Allocate hard regs to the pseudo regs used only within block number B.
1088 Only the pseudos that die but once can be handled. */
1089
1090 static void
1091 block_alloc (b)
1092 int b;
1093 {
1094 register int i, q;
1095 register rtx insn;
1096 rtx note;
1097 int insn_number = 0;
1098 int insn_count = 0;
1099 int max_uid = get_max_uid ();
1100 int *qty_order;
1101 int no_conflict_combined_regno = -1;
1102
1103 /* Count the instructions in the basic block. */
1104
1105 insn = basic_block_end[b];
1106 while (1)
1107 {
1108 if (GET_CODE (insn) != NOTE)
1109 if (++insn_count > max_uid)
1110 abort ();
1111 if (insn == basic_block_head[b])
1112 break;
1113 insn = PREV_INSN (insn);
1114 }
1115
1116 /* +2 to leave room for a post_mark_life at the last insn and for
1117 the birth of a CLOBBER in the first insn. */
1118 regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1119 * sizeof (HARD_REG_SET));
1120 bzero (regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1121
1122 /* Initialize table of hardware registers currently live. */
1123
1124 #ifdef HARD_REG_SET
1125 regs_live = *basic_block_live_at_start[b];
1126 #else
1127 COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1128 #endif
1129
1130 /* This loop scans the instructions of the basic block
1131 and assigns quantities to registers.
1132 It computes which registers to tie. */
1133
1134 insn = basic_block_head[b];
1135 while (1)
1136 {
1137 register rtx body = PATTERN (insn);
1138
1139 if (GET_CODE (insn) != NOTE)
1140 insn_number++;
1141
1142 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1143 {
1144 register rtx link, set;
1145 register int win = 0;
1146 register rtx r0, r1;
1147 int combined_regno = -1;
1148 int i;
1149 int insn_code_number = recog_memoized (insn);
1150
1151 this_insn_number = insn_number;
1152 this_insn = insn;
1153
1154 if (insn_code_number >= 0)
1155 insn_extract (insn);
1156 which_alternative = -1;
1157
1158 /* Is this insn suitable for tying two registers?
1159 If so, try doing that.
1160 Suitable insns are those with at least two operands and where
1161 operand 0 is an output that is a register that is not
1162 earlyclobber.
1163
1164 We can tie operand 0 with some operand that dies in this insn.
1165 First look for operands that are required to be in the same
1166 register as operand 0. If we find such, only try tying that
1167 operand or one that can be put into that operand if the
1168 operation is commutative. If we don't find an operand
1169 that is required to be in the same register as operand 0,
1170 we can tie with any operand.
1171
1172 Subregs in place of regs are also ok.
1173
1174 If tying is done, WIN is set nonzero. */
1175
1176 if (insn_code_number >= 0
1177 #ifdef REGISTER_CONSTRAINTS
1178 && insn_n_operands[insn_code_number] > 1
1179 && insn_operand_constraint[insn_code_number][0][0] == '='
1180 && insn_operand_constraint[insn_code_number][0][1] != '&'
1181 #else
1182 && GET_CODE (PATTERN (insn)) == SET
1183 && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1184 #endif
1185 )
1186 {
1187 #ifdef REGISTER_CONSTRAINTS
1188 int must_match_0 = -1;
1189
1190
1191 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1192 if (requires_inout_p
1193 (insn_operand_constraint[insn_code_number][i]))
1194 must_match_0 = i;
1195 #endif
1196
1197 r0 = recog_operand[0];
1198 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1199 {
1200 #ifdef REGISTER_CONSTRAINTS
1201 /* Skip this operand if we found an operand that
1202 must match operand 0 and this operand isn't it
1203 and can't be made to be it by commutativity. */
1204
1205 if (must_match_0 >= 0 && i != must_match_0
1206 && ! (i == must_match_0 + 1
1207 && insn_operand_constraint[insn_code_number][i-1][0] == '%')
1208 && ! (i == must_match_0 - 1
1209 && insn_operand_constraint[insn_code_number][i][0] == '%'))
1210 continue;
1211 #endif
1212
1213 r1 = recog_operand[i];
1214
1215 /* If the operand is an address, find a register in it.
1216 There may be more than one register, but we only try one
1217 of them. */
1218 if (
1219 #ifdef REGISTER_CONSTRAINTS
1220 insn_operand_constraint[insn_code_number][i][0] == 'p'
1221 #else
1222 insn_operand_address_p[insn_code_number][i]
1223 #endif
1224 )
1225 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1226 r1 = XEXP (r1, 0);
1227
1228 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1229 {
1230 /* We have two priorities for hard register preferences.
1231 If we have a move insn or an insn whose first input
1232 can only be in the same register as the output, give
1233 priority to an equivalence found from that insn. */
1234 int may_save_copy
1235 = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1236 #ifdef REGISTER_CONSTRAINTS
1237 || (r1 == recog_operand[i] && must_match_0 >= 0)
1238 #endif
1239 );
1240
1241 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1242 win = combine_regs (r1, r0, may_save_copy,
1243 insn_number, insn, 0);
1244 }
1245 }
1246 }
1247
1248 /* Recognize an insn sequence with an ultimate result
1249 which can safely overlap one of the inputs.
1250 The sequence begins with a CLOBBER of its result,
1251 and ends with an insn that copies the result to itself
1252 and has a REG_EQUAL note for an equivalent formula.
1253 That note indicates what the inputs are.
1254 The result and the input can overlap if each insn in
1255 the sequence either doesn't mention the input
1256 or has a REG_NO_CONFLICT note to inhibit the conflict.
1257
1258 We do the combining test at the CLOBBER so that the
1259 destination register won't have had a quantity number
1260 assigned, since that would prevent combining. */
1261
1262 if (GET_CODE (PATTERN (insn)) == CLOBBER
1263 && (r0 = XEXP (PATTERN (insn), 0),
1264 GET_CODE (r0) == REG)
1265 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1266 && XEXP (link, 0) != 0
1267 && GET_CODE (XEXP (link, 0)) == INSN
1268 && (set = single_set (XEXP (link, 0))) != 0
1269 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1270 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1271 NULL_RTX)) != 0)
1272 {
1273 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1274 /* Check that we have such a sequence. */
1275 && no_conflict_p (insn, r0, r1))
1276 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1277 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1278 && (r1 = XEXP (XEXP (note, 0), 0),
1279 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1280 && no_conflict_p (insn, r0, r1))
1281 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1282
1283 /* Here we care if the operation to be computed is
1284 commutative. */
1285 else if ((GET_CODE (XEXP (note, 0)) == EQ
1286 || GET_CODE (XEXP (note, 0)) == NE
1287 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1288 && (r1 = XEXP (XEXP (note, 0), 1),
1289 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1290 && no_conflict_p (insn, r0, r1))
1291 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1292
1293 /* If we did combine something, show the register number
1294 in question so that we know to ignore its death. */
1295 if (win)
1296 no_conflict_combined_regno = REGNO (r1);
1297 }
1298
1299 /* If registers were just tied, set COMBINED_REGNO
1300 to the number of the register used in this insn
1301 that was tied to the register set in this insn.
1302 This register's qty should not be "killed". */
1303
1304 if (win)
1305 {
1306 while (GET_CODE (r1) == SUBREG)
1307 r1 = SUBREG_REG (r1);
1308 combined_regno = REGNO (r1);
1309 }
1310
1311 /* Mark the death of everything that dies in this instruction,
1312 except for anything that was just combined. */
1313
1314 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1315 if (REG_NOTE_KIND (link) == REG_DEAD
1316 && GET_CODE (XEXP (link, 0)) == REG
1317 && combined_regno != REGNO (XEXP (link, 0))
1318 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1319 || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1320 wipe_dead_reg (XEXP (link, 0), 0);
1321
1322 /* Allocate qty numbers for all registers local to this block
1323 that are born (set) in this instruction.
1324 A pseudo that already has a qty is not changed. */
1325
1326 note_stores (PATTERN (insn), reg_is_set);
1327
1328 /* If anything is set in this insn and then unused, mark it as dying
1329 after this insn, so it will conflict with our outputs. This
1330 can't match with something that combined, and it doesn't matter
1331 if it did. Do this after the calls to reg_is_set since these
1332 die after, not during, the current insn. */
1333
1334 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1335 if (REG_NOTE_KIND (link) == REG_UNUSED
1336 && GET_CODE (XEXP (link, 0)) == REG)
1337 wipe_dead_reg (XEXP (link, 0), 1);
1338
1339 /* Allocate quantities for any SCRATCH operands of this insn. */
1340
1341 if (insn_code_number >= 0)
1342 for (i = 0; i < insn_n_operands[insn_code_number]; i++)
1343 if (GET_CODE (recog_operand[i]) == SCRATCH
1344 && scratch_index < scratch_list_length - 1)
1345 alloc_qty_for_scratch (recog_operand[i], i, insn,
1346 insn_code_number, insn_number);
1347
1348 /* If this is an insn that has a REG_RETVAL note pointing at a
1349 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1350 block, so clear any register number that combined within it. */
1351 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1352 && GET_CODE (XEXP (note, 0)) == INSN
1353 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1354 no_conflict_combined_regno = -1;
1355 }
1356
1357 /* Set the registers live after INSN_NUMBER. Note that we never
1358 record the registers live before the block's first insn, since no
1359 pseudos we care about are live before that insn. */
1360
1361 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1362 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1363
1364 if (insn == basic_block_end[b])
1365 break;
1366
1367 insn = NEXT_INSN (insn);
1368 }
1369
1370 /* Now every register that is local to this basic block
1371 should have been given a quantity, or else -1 meaning ignore it.
1372 Every quantity should have a known birth and death.
1373
1374 Order the qtys so we assign them registers in order of
1375 decreasing length of life. Normally call qsort, but if we
1376 have only a very small number of quantities, sort them ourselves. */
1377
1378 qty_order = (int *) alloca (next_qty * sizeof (int));
1379 for (i = 0; i < next_qty; i++)
1380 qty_order[i] = i;
1381
1382 #define EXCHANGE(I1, I2) \
1383 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1384
1385 switch (next_qty)
1386 {
1387 case 3:
1388 /* Make qty_order[2] be the one to allocate last. */
1389 if (qty_compare (0, 1) > 0)
1390 EXCHANGE (0, 1);
1391 if (qty_compare (1, 2) > 0)
1392 EXCHANGE (2, 1);
1393
1394 /* ... Fall through ... */
1395 case 2:
1396 /* Put the best one to allocate in qty_order[0]. */
1397 if (qty_compare (0, 1) > 0)
1398 EXCHANGE (0, 1);
1399
1400 /* ... Fall through ... */
1401
1402 case 1:
1403 case 0:
1404 /* Nothing to do here. */
1405 break;
1406
1407 default:
1408 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1409 }
1410
1411 /* Try to put each quantity in a suggested physical register, if it has one.
1412 This may cause registers to be allocated that otherwise wouldn't be, but
1413 this seems acceptable in local allocation (unlike global allocation). */
1414 for (i = 0; i < next_qty; i++)
1415 {
1416 q = qty_order[i];
1417 if (qty_phys_has_sugg[q] || qty_phys_has_copy_sugg[q])
1418 qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1419 0, 1, qty_birth[q], qty_death[q]);
1420 else
1421 qty_phys_reg[q] = -1;
1422 }
1423
1424 /* Now for each qty that is not a hardware register,
1425 look for a hardware register to put it in.
1426 First try the register class that is cheapest for this qty,
1427 if there is more than one class. */
1428
1429 for (i = 0; i < next_qty; i++)
1430 {
1431 q = qty_order[i];
1432 if (qty_phys_reg[q] < 0)
1433 {
1434 if (N_REG_CLASSES > 1)
1435 {
1436 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1437 qty_mode[q], q, 0, 0,
1438 qty_birth[q], qty_death[q]);
1439 if (qty_phys_reg[q] >= 0)
1440 continue;
1441 }
1442
1443 if (qty_alternate_class[q] != NO_REGS)
1444 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1445 qty_mode[q], q, 0, 0,
1446 qty_birth[q], qty_death[q]);
1447 }
1448 }
1449
1450 /* Now propagate the register assignments
1451 to the pseudo regs belonging to the qtys. */
1452
1453 for (q = 0; q < next_qty; q++)
1454 if (qty_phys_reg[q] >= 0)
1455 {
1456 for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1457 reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1458 if (qty_scratch_rtx[q])
1459 {
1460 if (GET_CODE (qty_scratch_rtx[q]) == REG)
1461 abort ();
1462 PUT_CODE (qty_scratch_rtx[q], REG);
1463 REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
1464
1465 scratch_block[scratch_index] = b;
1466 scratch_list[scratch_index++] = qty_scratch_rtx[q];
1467
1468 /* Must clear the USED field, because it will have been set by
1469 copy_rtx_if_shared, but the leaf_register code expects that
1470 it is zero in all REG rtx. copy_rtx_if_shared does not set the
1471 used bit for REGs, but does for SCRATCHes. */
1472 qty_scratch_rtx[q]->used = 0;
1473 }
1474 }
1475 }
1476 \f
1477 /* Compare two quantities' priority for getting real registers.
1478 We give shorter-lived quantities higher priority.
1479 Quantities with more references are also preferred, as are quantities that
1480 require multiple registers. This is the identical prioritization as
1481 done by global-alloc.
1482
1483 We used to give preference to registers with *longer* lives, but using
1484 the same algorithm in both local- and global-alloc can speed up execution
1485 of some programs by as much as a factor of three! */
1486
1487 static int
1488 qty_compare (q1, q2)
1489 int q1, q2;
1490 {
1491 /* Note that the quotient will never be bigger than
1492 the value of floor_log2 times the maximum number of
1493 times a register can occur in one insn (surely less than 100).
1494 Multiplying this by 10000 can't overflow. */
1495 register int pri1
1496 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1])
1497 / ((qty_death[q1] - qty_birth[q1]) * qty_size[q1]))
1498 * 10000);
1499 register int pri2
1500 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2])
1501 / ((qty_death[q2] - qty_birth[q2]) * qty_size[q2]))
1502 * 10000);
1503 return pri2 - pri1;
1504 }
1505
1506 static int
1507 qty_compare_1 (q1, q2)
1508 int *q1, *q2;
1509 {
1510 register int tem;
1511
1512 /* Note that the quotient will never be bigger than
1513 the value of floor_log2 times the maximum number of
1514 times a register can occur in one insn (surely less than 100).
1515 Multiplying this by 10000 can't overflow. */
1516 register int pri1
1517 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1])
1518 / ((qty_death[*q1] - qty_birth[*q1]) * qty_size[*q1]))
1519 * 10000);
1520 register int pri2
1521 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2])
1522 / ((qty_death[*q2] - qty_birth[*q2]) * qty_size[*q2]))
1523 * 10000);
1524
1525 tem = pri2 - pri1;
1526 if (tem != 0) return tem;
1527 /* If qtys are equally good, sort by qty number,
1528 so that the results of qsort leave nothing to chance. */
1529 return *q1 - *q2;
1530 }
1531 \f
1532 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1533 Returns 1 if have done so, or 0 if cannot.
1534
1535 Combining registers means marking them as having the same quantity
1536 and adjusting the offsets within the quantity if either of
1537 them is a SUBREG).
1538
1539 We don't actually combine a hard reg with a pseudo; instead
1540 we just record the hard reg as the suggestion for the pseudo's quantity.
1541 If we really combined them, we could lose if the pseudo lives
1542 across an insn that clobbers the hard reg (eg, movstr).
1543
1544 ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1545 there is no REG_DEAD note on INSN. This occurs during the processing
1546 of REG_NO_CONFLICT blocks.
1547
1548 MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1549 SETREG or if the input and output must share a register.
1550 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1551
1552 There are elaborate checks for the validity of combining. */
1553
1554
1555 static int
1556 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1557 rtx usedreg, setreg;
1558 int may_save_copy;
1559 int insn_number;
1560 rtx insn;
1561 int already_dead;
1562 {
1563 register int ureg, sreg;
1564 register int offset = 0;
1565 int usize, ssize;
1566 register int sqty;
1567
1568 /* Determine the numbers and sizes of registers being used. If a subreg
1569 is present that does not change the entire register, don't consider
1570 this a copy insn. */
1571
1572 while (GET_CODE (usedreg) == SUBREG)
1573 {
1574 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1575 may_save_copy = 0;
1576 offset += SUBREG_WORD (usedreg);
1577 usedreg = SUBREG_REG (usedreg);
1578 }
1579 if (GET_CODE (usedreg) != REG)
1580 return 0;
1581 ureg = REGNO (usedreg);
1582 usize = REG_SIZE (usedreg);
1583
1584 while (GET_CODE (setreg) == SUBREG)
1585 {
1586 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1587 may_save_copy = 0;
1588 offset -= SUBREG_WORD (setreg);
1589 setreg = SUBREG_REG (setreg);
1590 }
1591 if (GET_CODE (setreg) != REG)
1592 return 0;
1593 sreg = REGNO (setreg);
1594 ssize = REG_SIZE (setreg);
1595
1596 /* If UREG is a pseudo-register that hasn't already been assigned a
1597 quantity number, it means that it is not local to this block or dies
1598 more than once. In either event, we can't do anything with it. */
1599 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1600 /* Do not combine registers unless one fits within the other. */
1601 || (offset > 0 && usize + offset > ssize)
1602 || (offset < 0 && usize + offset < ssize)
1603 /* Do not combine with a smaller already-assigned object
1604 if that smaller object is already combined with something bigger. */
1605 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1606 && usize < qty_size[reg_qty[ureg]])
1607 /* Can't combine if SREG is not a register we can allocate. */
1608 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1609 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1610 These have already been taken care of. This probably wouldn't
1611 combine anyway, but don't take any chances. */
1612 || (ureg >= FIRST_PSEUDO_REGISTER
1613 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1614 /* Don't tie something to itself. In most cases it would make no
1615 difference, but it would screw up if the reg being tied to itself
1616 also dies in this insn. */
1617 || ureg == sreg
1618 /* Don't try to connect two different hardware registers. */
1619 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1620 /* Don't connect two different machine modes if they have different
1621 implications as to which registers may be used. */
1622 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1623 return 0;
1624
1625 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1626 qty_phys_sugg for the pseudo instead of tying them.
1627
1628 Return "failure" so that the lifespan of UREG is terminated here;
1629 that way the two lifespans will be disjoint and nothing will prevent
1630 the pseudo reg from being given this hard reg. */
1631
1632 if (ureg < FIRST_PSEUDO_REGISTER)
1633 {
1634 /* Allocate a quantity number so we have a place to put our
1635 suggestions. */
1636 if (reg_qty[sreg] == -2)
1637 reg_is_born (setreg, 2 * insn_number);
1638
1639 if (reg_qty[sreg] >= 0)
1640 {
1641 if (may_save_copy)
1642 {
1643 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1644 qty_phys_has_copy_sugg[reg_qty[sreg]] = 1;
1645 }
1646 else
1647 {
1648 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1649 qty_phys_has_sugg[reg_qty[sreg]] = 1;
1650 }
1651 }
1652 return 0;
1653 }
1654
1655 /* Similarly for SREG a hard register and UREG a pseudo register. */
1656
1657 if (sreg < FIRST_PSEUDO_REGISTER)
1658 {
1659 if (may_save_copy)
1660 {
1661 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1662 qty_phys_has_copy_sugg[reg_qty[ureg]] = 1;
1663 }
1664 else
1665 {
1666 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1667 qty_phys_has_sugg[reg_qty[ureg]] = 1;
1668 }
1669 return 0;
1670 }
1671
1672 /* At this point we know that SREG and UREG are both pseudos.
1673 Do nothing if SREG already has a quantity or is a register that we
1674 don't allocate. */
1675 if (reg_qty[sreg] >= -1
1676 /* If we are not going to let any regs live across calls,
1677 don't tie a call-crossing reg to a non-call-crossing reg. */
1678 || (current_function_has_nonlocal_label
1679 && ((reg_n_calls_crossed[ureg] > 0)
1680 != (reg_n_calls_crossed[sreg] > 0))))
1681 return 0;
1682
1683 /* We don't already know about SREG, so tie it to UREG
1684 if this is the last use of UREG, provided the classes they want
1685 are compatible. */
1686
1687 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1688 && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1689 {
1690 /* Add SREG to UREG's quantity. */
1691 sqty = reg_qty[ureg];
1692 reg_qty[sreg] = sqty;
1693 reg_offset[sreg] = reg_offset[ureg] + offset;
1694 reg_next_in_qty[sreg] = qty_first_reg[sqty];
1695 qty_first_reg[sqty] = sreg;
1696
1697 /* If SREG's reg class is smaller, set qty_min_class[SQTY]. */
1698 update_qty_class (sqty, sreg);
1699
1700 /* Update info about quantity SQTY. */
1701 qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
1702 qty_n_refs[sqty] += reg_n_refs[sreg];
1703 if (usize < ssize)
1704 {
1705 register int i;
1706
1707 for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1708 reg_offset[i] -= offset;
1709
1710 qty_size[sqty] = ssize;
1711 qty_mode[sqty] = GET_MODE (setreg);
1712 }
1713 }
1714 else
1715 return 0;
1716
1717 return 1;
1718 }
1719 \f
1720 /* Return 1 if the preferred class of REG allows it to be tied
1721 to a quantity or register whose class is CLASS.
1722 True if REG's reg class either contains or is contained in CLASS. */
1723
1724 static int
1725 reg_meets_class_p (reg, class)
1726 int reg;
1727 enum reg_class class;
1728 {
1729 register enum reg_class rclass = reg_preferred_class (reg);
1730 return (reg_class_subset_p (rclass, class)
1731 || reg_class_subset_p (class, rclass));
1732 }
1733
1734 /* Return 1 if the two specified classes have registers in common.
1735 If CALL_SAVED, then consider only call-saved registers. */
1736
1737 static int
1738 reg_classes_overlap_p (c1, c2, call_saved)
1739 register enum reg_class c1;
1740 register enum reg_class c2;
1741 int call_saved;
1742 {
1743 HARD_REG_SET c;
1744 int i;
1745
1746 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1747 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1748
1749 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1750 if (TEST_HARD_REG_BIT (c, i)
1751 && (! call_saved || ! call_used_regs[i]))
1752 return 1;
1753
1754 return 0;
1755 }
1756
1757 /* Update the class of QTY assuming that REG is being tied to it. */
1758
1759 static void
1760 update_qty_class (qty, reg)
1761 int qty;
1762 int reg;
1763 {
1764 enum reg_class rclass = reg_preferred_class (reg);
1765 if (reg_class_subset_p (rclass, qty_min_class[qty]))
1766 qty_min_class[qty] = rclass;
1767
1768 rclass = reg_alternate_class (reg);
1769 if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1770 qty_alternate_class[qty] = rclass;
1771 }
1772 \f
1773 /* Handle something which alters the value of an rtx REG.
1774
1775 REG is whatever is set or clobbered. SETTER is the rtx that
1776 is modifying the register.
1777
1778 If it is not really a register, we do nothing.
1779 The file-global variables `this_insn' and `this_insn_number'
1780 carry info from `block_alloc'. */
1781
1782 static void
1783 reg_is_set (reg, setter)
1784 rtx reg;
1785 rtx setter;
1786 {
1787 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1788 a hard register. These may actually not exist any more. */
1789
1790 if (GET_CODE (reg) != SUBREG
1791 && GET_CODE (reg) != REG)
1792 return;
1793
1794 /* Mark this register as being born. If it is used in a CLOBBER, mark
1795 it as being born halfway between the previous insn and this insn so that
1796 it conflicts with our inputs but not the outputs of the previous insn. */
1797
1798 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1799 }
1800 \f
1801 /* Handle beginning of the life of register REG.
1802 BIRTH is the index at which this is happening. */
1803
1804 static void
1805 reg_is_born (reg, birth)
1806 rtx reg;
1807 int birth;
1808 {
1809 register int regno;
1810
1811 if (GET_CODE (reg) == SUBREG)
1812 regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1813 else
1814 regno = REGNO (reg);
1815
1816 if (regno < FIRST_PSEUDO_REGISTER)
1817 {
1818 mark_life (regno, GET_MODE (reg), 1);
1819
1820 /* If the register was to have been born earlier that the present
1821 insn, mark it as live where it is actually born. */
1822 if (birth < 2 * this_insn_number)
1823 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
1824 }
1825 else
1826 {
1827 if (reg_qty[regno] == -2)
1828 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
1829
1830 /* If this register has a quantity number, show that it isn't dead. */
1831 if (reg_qty[regno] >= 0)
1832 qty_death[reg_qty[regno]] = -1;
1833 }
1834 }
1835
1836 /* Record the death of REG in the current insn. If OUTPUT_P is non-zero,
1837 REG is an output that is dying (i.e., it is never used), otherwise it
1838 is an input (the normal case).
1839 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
1840
1841 static void
1842 wipe_dead_reg (reg, output_p)
1843 register rtx reg;
1844 int output_p;
1845 {
1846 register int regno = REGNO (reg);
1847
1848 /* If this insn has multiple results,
1849 and the dead reg is used in one of the results,
1850 extend its life to after this insn,
1851 so it won't get allocated together with any other result of this insn. */
1852 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
1853 && !single_set (this_insn))
1854 {
1855 int i;
1856 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
1857 {
1858 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
1859 if (GET_CODE (set) == SET
1860 && GET_CODE (SET_DEST (set)) != REG
1861 && !rtx_equal_p (reg, SET_DEST (set))
1862 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1863 output_p = 1;
1864 }
1865 }
1866
1867 if (regno < FIRST_PSEUDO_REGISTER)
1868 {
1869 mark_life (regno, GET_MODE (reg), 0);
1870
1871 /* If a hard register is dying as an output, mark it as in use at
1872 the beginning of this insn (the above statement would cause this
1873 not to happen). */
1874 if (output_p)
1875 post_mark_life (regno, GET_MODE (reg), 1,
1876 2 * this_insn_number, 2 * this_insn_number+ 1);
1877 }
1878
1879 else if (reg_qty[regno] >= 0)
1880 qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
1881 }
1882 \f
1883 /* Find a block of SIZE words of hard regs in reg_class CLASS
1884 that can hold something of machine-mode MODE
1885 (but actually we test only the first of the block for holding MODE)
1886 and still free between insn BORN_INDEX and insn DEAD_INDEX,
1887 and return the number of the first of them.
1888 Return -1 if such a block cannot be found.
1889 If QTY crosses calls, insist on a register preserved by calls,
1890 unless ACCEPT_CALL_CLOBBERED is nonzero.
1891
1892 If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
1893 register is available. If not, return -1. */
1894
1895 static int
1896 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
1897 born_index, dead_index)
1898 enum reg_class class;
1899 enum machine_mode mode;
1900 int accept_call_clobbered;
1901 int just_try_suggested;
1902 int qty;
1903 int born_index, dead_index;
1904 {
1905 register int i, ins;
1906 #ifdef HARD_REG_SET
1907 register /* Declare it register if it's a scalar. */
1908 #endif
1909 HARD_REG_SET used, first_used;
1910 #ifdef ELIMINABLE_REGS
1911 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
1912 #endif
1913
1914 /* Validate our parameters. */
1915 if (born_index < 0 || born_index > dead_index)
1916 abort ();
1917
1918 /* Don't let a pseudo live in a reg across a function call
1919 if we might get a nonlocal goto. */
1920 if (current_function_has_nonlocal_label
1921 && qty_n_calls_crossed[qty] > 0)
1922 return -1;
1923
1924 if (accept_call_clobbered)
1925 COPY_HARD_REG_SET (used, call_fixed_reg_set);
1926 else if (qty_n_calls_crossed[qty] == 0)
1927 COPY_HARD_REG_SET (used, fixed_reg_set);
1928 else
1929 COPY_HARD_REG_SET (used, call_used_reg_set);
1930
1931 for (ins = born_index; ins < dead_index; ins++)
1932 IOR_HARD_REG_SET (used, regs_live_at[ins]);
1933
1934 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
1935
1936 /* Don't use the frame pointer reg in local-alloc even if
1937 we may omit the frame pointer, because if we do that and then we
1938 need a frame pointer, reload won't know how to move the pseudo
1939 to another hard reg. It can move only regs made by global-alloc.
1940
1941 This is true of any register that can be eliminated. */
1942 #ifdef ELIMINABLE_REGS
1943 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
1944 SET_HARD_REG_BIT (used, eliminables[i].from);
1945 #else
1946 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
1947 #endif
1948
1949 /* Normally, the registers that can be used for the first register in
1950 a multi-register quantity are the same as those that can be used for
1951 subsequent registers. However, if just trying suggested registers,
1952 restrict our consideration to them. If there are copy-suggested
1953 register, try them. Otherwise, try the arithmetic-suggested
1954 registers. */
1955 COPY_HARD_REG_SET (first_used, used);
1956
1957 if (just_try_suggested)
1958 {
1959 if (qty_phys_has_copy_sugg[qty])
1960 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
1961 else
1962 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
1963 }
1964
1965 /* If all registers are excluded, we can't do anything. */
1966 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
1967
1968 /* If at least one would be suitable, test each hard reg. */
1969
1970 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1971 {
1972 #ifdef REG_ALLOC_ORDER
1973 int regno = reg_alloc_order[i];
1974 #else
1975 int regno = i;
1976 #endif
1977 if (! TEST_HARD_REG_BIT (first_used, regno)
1978 && HARD_REGNO_MODE_OK (regno, mode))
1979 {
1980 register int j;
1981 register int size1 = HARD_REGNO_NREGS (regno, mode);
1982 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
1983 if (j == size1)
1984 {
1985 /* Mark that this register is in use between its birth and death
1986 insns. */
1987 post_mark_life (regno, mode, 1, born_index, dead_index);
1988 return regno;
1989 }
1990 #ifndef REG_ALLOC_ORDER
1991 i += j; /* Skip starting points we know will lose */
1992 #endif
1993 }
1994 }
1995
1996 fail:
1997
1998 /* If we are just trying suggested register, we have just tried copy-
1999 suggested registers, and there are arithmetic-suggested registers,
2000 try them. */
2001
2002 /* If it would be profitable to allocate a call-clobbered register
2003 and save and restore it around calls, do that. */
2004 if (just_try_suggested && qty_phys_has_copy_sugg[qty]
2005 && qty_phys_has_sugg[qty])
2006 {
2007 /* Don't try the copy-suggested regs again. */
2008 qty_phys_has_copy_sugg[qty] = 0;
2009 return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2010 born_index, dead_index);
2011 }
2012
2013 /* We need not check to see if the current function has nonlocal
2014 labels because we don't put any pseudos that are live over calls in
2015 registers in that case. */
2016
2017 if (! accept_call_clobbered
2018 && flag_caller_saves
2019 && ! just_try_suggested
2020 && qty_n_calls_crossed[qty] != 0
2021 && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2022 {
2023 i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2024 if (i >= 0)
2025 caller_save_needed = 1;
2026 return i;
2027 }
2028 return -1;
2029 }
2030 \f
2031 /* Mark that REGNO with machine-mode MODE is live starting from the current
2032 insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2033 is zero). */
2034
2035 static void
2036 mark_life (regno, mode, life)
2037 register int regno;
2038 enum machine_mode mode;
2039 int life;
2040 {
2041 register int j = HARD_REGNO_NREGS (regno, mode);
2042 if (life)
2043 while (--j >= 0)
2044 SET_HARD_REG_BIT (regs_live, regno + j);
2045 else
2046 while (--j >= 0)
2047 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2048 }
2049
2050 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2051 is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2052 to insn number DEATH (exclusive). */
2053
2054 static void
2055 post_mark_life (regno, mode, life, birth, death)
2056 register int regno, life, birth;
2057 enum machine_mode mode;
2058 int death;
2059 {
2060 register int j = HARD_REGNO_NREGS (regno, mode);
2061 #ifdef HARD_REG_SET
2062 register /* Declare it register if it's a scalar. */
2063 #endif
2064 HARD_REG_SET this_reg;
2065
2066 CLEAR_HARD_REG_SET (this_reg);
2067 while (--j >= 0)
2068 SET_HARD_REG_BIT (this_reg, regno + j);
2069
2070 if (life)
2071 while (birth < death)
2072 {
2073 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2074 birth++;
2075 }
2076 else
2077 while (birth < death)
2078 {
2079 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2080 birth++;
2081 }
2082 }
2083 \f
2084 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2085 is the register being clobbered, and R1 is a register being used in
2086 the equivalent expression.
2087
2088 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2089 in which it is used, return 1.
2090
2091 Otherwise, return 0. */
2092
2093 static int
2094 no_conflict_p (insn, r0, r1)
2095 rtx insn, r0, r1;
2096 {
2097 int ok = 0;
2098 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2099 rtx p, last;
2100
2101 /* If R1 is a hard register, return 0 since we handle this case
2102 when we scan the insns that actually use it. */
2103
2104 if (note == 0
2105 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2106 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2107 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2108 return 0;
2109
2110 last = XEXP (note, 0);
2111
2112 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2113 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2114 {
2115 if (find_reg_note (p, REG_DEAD, r1))
2116 ok = 1;
2117
2118 if (reg_mentioned_p (r1, PATTERN (p))
2119 && ! find_reg_note (p, REG_NO_CONFLICT, r1))
2120 return 0;
2121 }
2122
2123 return ok;
2124 }
2125 \f
2126 #ifdef REGISTER_CONSTRAINTS
2127
2128 /* Return 1 if the constraint string P indicates that the a the operand
2129 must be equal to operand 0 and that no register is acceptable. */
2130
2131 static int
2132 requires_inout_p (p)
2133 char *p;
2134 {
2135 char c;
2136 int found_zero = 0;
2137
2138 while (c = *p++)
2139 switch (c)
2140 {
2141 case '0':
2142 found_zero = 1;
2143 break;
2144
2145 case '=': case '+': case '?':
2146 case '#': case '&': case '!':
2147 case '*': case '%': case ',':
2148 case '1': case '2': case '3': case '4':
2149 case 'm': case '<': case '>': case 'V': case 'o':
2150 case 'E': case 'F': case 'G': case 'H':
2151 case 's': case 'i': case 'n':
2152 case 'I': case 'J': case 'K': case 'L':
2153 case 'M': case 'N': case 'O': case 'P':
2154 #ifdef EXTRA_CONSTRAINT
2155 case 'Q': case 'R': case 'S': case 'T': case 'U':
2156 #endif
2157 case 'X':
2158 /* These don't say anything we care about. */
2159 break;
2160
2161 case 'p':
2162 case 'g': case 'r':
2163 default:
2164 /* These mean a register is allowed. Fail if so. */
2165 return 0;
2166 }
2167
2168 return found_zero;
2169 }
2170 #endif /* REGISTER_CONSTRAINTS */
2171 \f
2172 void
2173 dump_local_alloc (file)
2174 FILE *file;
2175 {
2176 register int i;
2177 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2178 if (reg_renumber[i] != -1)
2179 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
2180 }