(decl_attributes): Added argument.
[official-gcc.git] / gcc / local-alloc.c
blobe62a5a61953b13c11ff1d1302e8700a8a50a63ce
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 1988, 1991, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
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.
29 The assignments made are recorded in the vector reg_renumber
30 whose space is allocated here. The rtl code itself is not altered.
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.
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.
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.
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. */
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"
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.
72 On most machines, the appropriate test is if the class has one
73 register, so we default to that. */
75 #ifndef CLASS_LIKELY_SPILLED_P
76 #define CLASS_LIKELY_SPILLED_P(CLASS) (reg_class_size[(int) (CLASS)] == 1)
77 #endif
79 /* Next quantity number available for allocation. */
81 static int next_qty;
83 /* In all the following vectors indexed by quantity number. */
85 /* Element Q is the hard reg number chosen for quantity Q,
86 or -1 if none was found. */
88 static short *qty_phys_reg;
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.
95 The former register set is given priority for allocation. This tends to
96 eliminate copy insns. */
98 /* Element Q is a set of hard registers that are suggested for quantity Q by
99 copy insns. */
101 static HARD_REG_SET *qty_phys_copy_sugg;
103 /* Element Q is a set of hard registers that are suggested for quantity Q by
104 arithmetic insns. */
106 static HARD_REG_SET *qty_phys_sugg;
108 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
110 static short *qty_phys_num_copy_sugg;
112 /* Element Q is the number of suggested registers in qty_phys_sugg. */
114 static short *qty_phys_num_sugg;
116 /* Element Q is the number of refs to quantity Q. */
118 static int *qty_n_refs;
120 /* Element Q is a reg class contained in (smaller than) the
121 preferred classes of all the pseudo regs that are tied in quantity Q.
122 This is the preferred class for allocating that quantity. */
124 static enum reg_class *qty_min_class;
126 /* Insn number (counting from head of basic block)
127 where quantity Q was born. -1 if birth has not been recorded. */
129 static int *qty_birth;
131 /* Insn number (counting from head of basic block)
132 where quantity Q died. Due to the way tying is done,
133 and the fact that we consider in this pass only regs that die but once,
134 a quantity can die only once. Each quantity's life span
135 is a set of consecutive insns. -1 if death has not been recorded. */
137 static int *qty_death;
139 /* Number of words needed to hold the data in quantity Q.
140 This depends on its machine mode. It is used for these purposes:
141 1. It is used in computing the relative importances of qtys,
142 which determines the order in which we look for regs for them.
143 2. It is used in rules that prevent tying several registers of
144 different sizes in a way that is geometrically impossible
145 (see combine_regs). */
147 static int *qty_size;
149 /* This holds the mode of the registers that are tied to qty Q,
150 or VOIDmode if registers with differing modes are tied together. */
152 static enum machine_mode *qty_mode;
154 /* Number of times a reg tied to qty Q lives across a CALL_INSN. */
156 static int *qty_n_calls_crossed;
158 /* Register class within which we allocate qty Q if we can't get
159 its preferred class. */
161 static enum reg_class *qty_alternate_class;
163 /* Element Q is the SCRATCH expression for which this quantity is being
164 allocated or 0 if this quantity is allocating registers. */
166 static rtx *qty_scratch_rtx;
168 /* Element Q is nonzero if this quantity has been used in a SUBREG
169 that changes its size. */
171 static char *qty_changes_size;
173 /* Element Q is the register number of one pseudo register whose
174 reg_qty value is Q, or -1 is this quantity is for a SCRATCH. This
175 register should be the head of the chain maintained in reg_next_in_qty. */
177 static int *qty_first_reg;
179 /* If (REG N) has been assigned a quantity number, is a register number
180 of another register assigned the same quantity number, or -1 for the
181 end of the chain. qty_first_reg point to the head of this chain. */
183 static int *reg_next_in_qty;
185 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
186 if it is >= 0,
187 of -1 if this register cannot be allocated by local-alloc,
188 or -2 if not known yet.
190 Note that if we see a use or death of pseudo register N with
191 reg_qty[N] == -2, register N must be local to the current block. If
192 it were used in more than one block, we would have reg_qty[N] == -1.
193 This relies on the fact that if reg_basic_block[N] is >= 0, register N
194 will not appear in any other block. We save a considerable number of
195 tests by exploiting this.
197 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
198 be referenced. */
200 static int *reg_qty;
202 /* The offset (in words) of register N within its quantity.
203 This can be nonzero if register N is SImode, and has been tied
204 to a subreg of a DImode register. */
206 static char *reg_offset;
208 /* Vector of substitutions of register numbers,
209 used to map pseudo regs into hardware regs.
210 This is set up as a result of register allocation.
211 Element N is the hard reg assigned to pseudo reg N,
212 or is -1 if no hard reg was assigned.
213 If N is a hard reg number, element N is N. */
215 short *reg_renumber;
217 /* Set of hard registers live at the current point in the scan
218 of the instructions in a basic block. */
220 static HARD_REG_SET regs_live;
222 /* Each set of hard registers indicates registers live at a particular
223 point in the basic block. For N even, regs_live_at[N] says which
224 hard registers are needed *after* insn N/2 (i.e., they may not
225 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
227 If an object is to conflict with the inputs of insn J but not the
228 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
229 if it is to conflict with the outputs of insn J but not the inputs of
230 insn J + 1, it is said to die at index J*2 + 1. */
232 static HARD_REG_SET *regs_live_at;
234 int *scratch_block;
235 rtx *scratch_list;
236 int scratch_list_length;
237 static int scratch_index;
239 /* Communicate local vars `insn_number' and `insn'
240 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
241 static int this_insn_number;
242 static rtx this_insn;
244 static void alloc_qty PROTO((int, enum machine_mode, int, int));
245 static void alloc_qty_for_scratch PROTO((rtx, int, rtx, int, int));
246 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
247 static int validate_equiv_mem PROTO((rtx, rtx, rtx));
248 static int memref_referenced_p PROTO((rtx, rtx));
249 static int memref_used_between_p PROTO((rtx, rtx, rtx));
250 static void optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
251 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
252 static void update_equiv_regs PROTO((void));
253 static void block_alloc PROTO((int));
254 static int qty_sugg_compare PROTO((int, int));
255 static int qty_sugg_compare_1 PROTO((int *, int *));
256 static int qty_compare PROTO((int, int));
257 static int qty_compare_1 PROTO((int *, int *));
258 static int combine_regs PROTO((rtx, rtx, int, int, rtx, int));
259 static int reg_meets_class_p PROTO((int, enum reg_class));
260 static int reg_classes_overlap_p PROTO((enum reg_class, enum reg_class,
261 int));
262 static void update_qty_class PROTO((int, int));
263 static void reg_is_set PROTO((rtx, rtx));
264 static void reg_is_born PROTO((rtx, int));
265 static void wipe_dead_reg PROTO((rtx, int));
266 static int find_free_reg PROTO((enum reg_class, enum machine_mode,
267 int, int, int, int, int));
268 static void mark_life PROTO((int, enum machine_mode, int));
269 static void post_mark_life PROTO((int, enum machine_mode, int, int, int));
270 static int no_conflict_p PROTO((rtx, rtx, rtx));
271 static int requires_inout PROTO((char *));
273 /* Allocate a new quantity (new within current basic block)
274 for register number REGNO which is born at index BIRTH
275 within the block. MODE and SIZE are info on reg REGNO. */
277 static void
278 alloc_qty (regno, mode, size, birth)
279 int regno;
280 enum machine_mode mode;
281 int size, birth;
283 register int qty = next_qty++;
285 reg_qty[regno] = qty;
286 reg_offset[regno] = 0;
287 reg_next_in_qty[regno] = -1;
289 qty_first_reg[qty] = regno;
290 qty_size[qty] = size;
291 qty_mode[qty] = mode;
292 qty_birth[qty] = birth;
293 qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
294 qty_min_class[qty] = reg_preferred_class (regno);
295 qty_alternate_class[qty] = reg_alternate_class (regno);
296 qty_n_refs[qty] = reg_n_refs[regno];
297 qty_changes_size[qty] = reg_changes_size[regno];
300 /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
301 used as operand N in INSN. We assume here that the SCRATCH is used in
302 a CLOBBER. */
304 static void
305 alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
306 rtx scratch;
307 int n;
308 rtx insn;
309 int insn_code_num, insn_number;
311 register int qty;
312 enum reg_class class;
313 char *p, c;
314 int i;
316 #ifdef REGISTER_CONSTRAINTS
317 /* If we haven't yet computed which alternative will be used, do so now.
318 Then set P to the constraints for that alternative. */
319 if (which_alternative == -1)
320 if (! constrain_operands (insn_code_num, 0))
321 return;
323 for (p = insn_operand_constraint[insn_code_num][n], i = 0;
324 *p && i < which_alternative; p++)
325 if (*p == ',')
326 i++;
328 /* Compute the class required for this SCRATCH. If we don't need a
329 register, the class will remain NO_REGS. If we guessed the alternative
330 number incorrectly, reload will fix things up for us. */
332 class = NO_REGS;
333 while ((c = *p++) != '\0' && c != ',')
334 switch (c)
336 case '=': case '+': case '?':
337 case '#': case '&': case '!':
338 case '*': case '%':
339 case '0': case '1': case '2': case '3': case '4':
340 case 'm': case '<': case '>': case 'V': case 'o':
341 case 'E': case 'F': case 'G': case 'H':
342 case 's': case 'i': case 'n':
343 case 'I': case 'J': case 'K': case 'L':
344 case 'M': case 'N': case 'O': case 'P':
345 #ifdef EXTRA_CONSTRAINT
346 case 'Q': case 'R': case 'S': case 'T': case 'U':
347 #endif
348 case 'p':
349 /* These don't say anything we care about. */
350 break;
352 case 'X':
353 /* We don't need to allocate this SCRATCH. */
354 return;
356 case 'g': case 'r':
357 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
358 break;
360 default:
361 class
362 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
363 break;
366 if (class == NO_REGS)
367 return;
369 #else /* REGISTER_CONSTRAINTS */
371 class = GENERAL_REGS;
372 #endif
375 qty = next_qty++;
377 qty_first_reg[qty] = -1;
378 qty_scratch_rtx[qty] = scratch;
379 qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
380 qty_mode[qty] = GET_MODE (scratch);
381 qty_birth[qty] = 2 * insn_number - 1;
382 qty_death[qty] = 2 * insn_number + 1;
383 qty_n_calls_crossed[qty] = 0;
384 qty_min_class[qty] = class;
385 qty_alternate_class[qty] = NO_REGS;
386 qty_n_refs[qty] = 1;
387 qty_changes_size[qty] = 0;
390 /* Main entry point of this file. */
392 void
393 local_alloc ()
395 register int b, i;
396 int max_qty;
398 /* Leaf functions and non-leaf functions have different needs.
399 If defined, let the machine say what kind of ordering we
400 should use. */
401 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
402 ORDER_REGS_FOR_LOCAL_ALLOC;
403 #endif
405 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
406 registers. */
407 update_equiv_regs ();
409 /* This sets the maximum number of quantities we can have. Quantity
410 numbers start at zero and we can have one for each pseudo plus the
411 number of SCRATCHes in the largest block, in the worst case. */
412 max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
414 /* Allocate vectors of temporary data.
415 See the declarations of these variables, above,
416 for what they mean. */
418 /* There can be up to MAX_SCRATCH * N_BASIC_BLOCKS SCRATCHes to allocate.
419 Instead of allocating this much memory from now until the end of
420 reload, only allocate space for MAX_QTY SCRATCHes. If there are more
421 reload will allocate them. */
423 scratch_list_length = max_qty;
424 scratch_list = (rtx *) xmalloc (scratch_list_length * sizeof (rtx));
425 bzero ((char *) scratch_list, scratch_list_length * sizeof (rtx));
426 scratch_block = (int *) xmalloc (scratch_list_length * sizeof (int));
427 bzero ((char *) scratch_block, scratch_list_length * sizeof (int));
428 scratch_index = 0;
430 qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
431 qty_phys_copy_sugg
432 = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
433 qty_phys_num_copy_sugg = (short *) alloca (max_qty * sizeof (short));
434 qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
435 qty_phys_num_sugg = (short *) alloca (max_qty * sizeof (short));
436 qty_birth = (int *) alloca (max_qty * sizeof (int));
437 qty_death = (int *) alloca (max_qty * sizeof (int));
438 qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
439 qty_first_reg = (int *) alloca (max_qty * sizeof (int));
440 qty_size = (int *) alloca (max_qty * sizeof (int));
441 qty_mode
442 = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
443 qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
444 qty_min_class
445 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
446 qty_alternate_class
447 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
448 qty_n_refs = (int *) alloca (max_qty * sizeof (int));
449 qty_changes_size = (char *) alloca (max_qty * sizeof (char));
451 reg_qty = (int *) alloca (max_regno * sizeof (int));
452 reg_offset = (char *) alloca (max_regno * sizeof (char));
453 reg_next_in_qty = (int *) alloca (max_regno * sizeof (int));
455 reg_renumber = (short *) oballoc (max_regno * sizeof (short));
456 for (i = 0; i < max_regno; i++)
457 reg_renumber[i] = -1;
459 /* Determine which pseudo-registers can be allocated by local-alloc.
460 In general, these are the registers used only in a single block and
461 which only die once. However, if a register's preferred class has only
462 a few entries, don't allocate this register here unless it is preferred
463 or nothing since retry_global_alloc won't be able to move it to
464 GENERAL_REGS if a reload register of this class is needed.
466 We need not be concerned with which block actually uses the register
467 since we will never see it outside that block. */
469 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
471 if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
472 && (reg_alternate_class (i) == NO_REGS
473 || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
474 reg_qty[i] = -2;
475 else
476 reg_qty[i] = -1;
479 /* Force loop below to initialize entire quantity array. */
480 next_qty = max_qty;
482 /* Allocate each block's local registers, block by block. */
484 for (b = 0; b < n_basic_blocks; b++)
486 /* NEXT_QTY indicates which elements of the `qty_...'
487 vectors might need to be initialized because they were used
488 for the previous block; it is set to the entire array before
489 block 0. Initialize those, with explicit loop if there are few,
490 else with bzero and bcopy. Do not initialize vectors that are
491 explicit set by `alloc_qty'. */
493 if (next_qty < 6)
495 for (i = 0; i < next_qty; i++)
497 qty_scratch_rtx[i] = 0;
498 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
499 qty_phys_num_copy_sugg[i] = 0;
500 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
501 qty_phys_num_sugg[i] = 0;
504 else
506 #define CLEAR(vector) \
507 bzero ((char *) (vector), (sizeof (*(vector))) * next_qty);
509 CLEAR (qty_scratch_rtx);
510 CLEAR (qty_phys_copy_sugg);
511 CLEAR (qty_phys_num_copy_sugg);
512 CLEAR (qty_phys_sugg);
513 CLEAR (qty_phys_num_sugg);
516 next_qty = 0;
518 block_alloc (b);
519 #ifdef USE_C_ALLOCA
520 alloca (0);
521 #endif
525 /* Depth of loops we are in while in update_equiv_regs. */
526 static int loop_depth;
528 /* Used for communication between the following two functions: contains
529 a MEM that we wish to ensure remains unchanged. */
530 static rtx equiv_mem;
532 /* Set nonzero if EQUIV_MEM is modified. */
533 static int equiv_mem_modified;
535 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
536 Called via note_stores. */
538 static void
539 validate_equiv_mem_from_store (dest, set)
540 rtx dest;
541 rtx set;
543 if ((GET_CODE (dest) == REG
544 && reg_overlap_mentioned_p (dest, equiv_mem))
545 || (GET_CODE (dest) == MEM
546 && true_dependence (dest, equiv_mem)))
547 equiv_mem_modified = 1;
550 /* Verify that no store between START and the death of REG invalidates
551 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
552 by storing into an overlapping memory location, or with a non-const
553 CALL_INSN.
555 Return 1 if MEMREF remains valid. */
557 static int
558 validate_equiv_mem (start, reg, memref)
559 rtx start;
560 rtx reg;
561 rtx memref;
563 rtx insn;
564 rtx note;
566 equiv_mem = memref;
567 equiv_mem_modified = 0;
569 /* If the memory reference has side effects or is volatile, it isn't a
570 valid equivalence. */
571 if (side_effects_p (memref))
572 return 0;
574 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
576 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
577 continue;
579 if (find_reg_note (insn, REG_DEAD, reg))
580 return 1;
582 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
583 && ! CONST_CALL_P (insn))
584 return 0;
586 note_stores (PATTERN (insn), validate_equiv_mem_from_store);
588 /* If a register mentioned in MEMREF is modified via an
589 auto-increment, we lose the equivalence. Do the same if one
590 dies; although we could extend the life, it doesn't seem worth
591 the trouble. */
593 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
594 if ((REG_NOTE_KIND (note) == REG_INC
595 || REG_NOTE_KIND (note) == REG_DEAD)
596 && GET_CODE (XEXP (note, 0)) == REG
597 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
598 return 0;
601 return 0;
604 /* TRUE if X references a memory location that would be affected by a store
605 to MEMREF. */
607 static int
608 memref_referenced_p (memref, x)
609 rtx x;
610 rtx memref;
612 int i, j;
613 char *fmt;
614 enum rtx_code code = GET_CODE (x);
616 switch (code)
618 case REG:
619 case CONST_INT:
620 case CONST:
621 case LABEL_REF:
622 case SYMBOL_REF:
623 case CONST_DOUBLE:
624 case PC:
625 case CC0:
626 case HIGH:
627 case LO_SUM:
628 return 0;
630 case MEM:
631 if (true_dependence (memref, x))
632 return 1;
633 break;
635 case SET:
636 /* If we are setting a MEM, it doesn't count (its address does), but any
637 other SET_DEST that has a MEM in it is referencing the MEM. */
638 if (GET_CODE (SET_DEST (x)) == MEM)
640 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
641 return 1;
643 else if (memref_referenced_p (memref, SET_DEST (x)))
644 return 1;
646 return memref_referenced_p (memref, SET_SRC (x));
649 fmt = GET_RTX_FORMAT (code);
650 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
651 switch (fmt[i])
653 case 'e':
654 if (memref_referenced_p (memref, XEXP (x, i)))
655 return 1;
656 break;
657 case 'E':
658 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
659 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
660 return 1;
661 break;
664 return 0;
667 /* TRUE if some insn in the range (START, END] references a memory location
668 that would be affected by a store to MEMREF. */
670 static int
671 memref_used_between_p (memref, start, end)
672 rtx memref;
673 rtx start;
674 rtx end;
676 rtx insn;
678 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
679 insn = NEXT_INSN (insn))
680 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
681 && memref_referenced_p (memref, PATTERN (insn)))
682 return 1;
684 return 0;
687 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
688 in INSN.
690 Search forward to see if SRC dies before either it or DEST is modified,
691 but don't scan past the end of a basic block. If so, we can replace SRC
692 with DEST and let SRC die in INSN.
694 This will reduce the number of registers live in that range and may enable
695 DEST to be tied to SRC, thus often saving one register in addition to a
696 register-register copy. */
698 static void
699 optimize_reg_copy_1 (insn, dest, src)
700 rtx insn;
701 rtx dest;
702 rtx src;
704 rtx p, q;
705 rtx note;
706 rtx dest_death = 0;
707 int sregno = REGNO (src);
708 int dregno = REGNO (dest);
710 if (sregno == dregno
711 #ifdef SMALL_REGISTER_CLASSES
712 /* We don't want to mess with hard regs if register classes are small. */
713 || sregno < FIRST_PSEUDO_REGISTER || dregno < FIRST_PSEUDO_REGISTER
714 #endif
715 /* We don't see all updates to SP if they are in an auto-inc memory
716 reference, so we must disallow this optimization on them. */
717 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
718 return;
720 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
722 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
723 || (GET_CODE (p) == NOTE
724 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
725 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
726 break;
728 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
729 continue;
731 if (reg_set_p (src, p) || reg_set_p (dest, p)
732 /* Don't change a USE of a register. */
733 || (GET_CODE (PATTERN (p)) == USE
734 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
735 break;
737 /* See if all of SRC dies in P. This test is slightly more
738 conservative than it needs to be. */
739 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
740 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
742 int failed = 0;
743 int length = 0;
744 int d_length = 0;
745 int n_calls = 0;
746 int d_n_calls = 0;
748 /* We can do the optimization. Scan forward from INSN again,
749 replacing regs as we go. Set FAILED if a replacement can't
750 be done. In that case, we can't move the death note for SRC.
751 This should be rare. */
753 /* Set to stop at next insn. */
754 for (q = next_real_insn (insn);
755 q != next_real_insn (p);
756 q = next_real_insn (q))
758 if (reg_overlap_mentioned_p (src, PATTERN (q)))
760 /* If SRC is a hard register, we might miss some
761 overlapping registers with validate_replace_rtx,
762 so we would have to undo it. We can't if DEST is
763 present in the insn, so fail in that combination
764 of cases. */
765 if (sregno < FIRST_PSEUDO_REGISTER
766 && reg_mentioned_p (dest, PATTERN (q)))
767 failed = 1;
769 /* Replace all uses and make sure that the register
770 isn't still present. */
771 else if (validate_replace_rtx (src, dest, q)
772 && (sregno >= FIRST_PSEUDO_REGISTER
773 || ! reg_overlap_mentioned_p (src,
774 PATTERN (q))))
776 /* We assume that a register is used exactly once per
777 insn in the updates below. If this is not correct,
778 no great harm is done. */
779 if (sregno >= FIRST_PSEUDO_REGISTER)
780 reg_n_refs[sregno] -= loop_depth;
781 if (dregno >= FIRST_PSEUDO_REGISTER)
782 reg_n_refs[dregno] += loop_depth;
784 else
786 validate_replace_rtx (dest, src, q);
787 failed = 1;
791 /* Count the insns and CALL_INSNs passed. If we passed the
792 death note of DEST, show increased live length. */
793 length++;
794 if (dest_death)
795 d_length++;
797 /* If the insn in which SRC dies is a CALL_INSN, don't count it
798 as a call that has been crossed. Otherwise, count it. */
799 if (q != p && GET_CODE (q) == CALL_INSN)
801 n_calls++;
802 if (dest_death)
803 d_n_calls++;
806 /* If DEST dies here, remove the death note and save it for
807 later. Make sure ALL of DEST dies here; again, this is
808 overly conservative. */
809 if (dest_death == 0
810 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
811 && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
812 remove_note (q, dest_death);
815 if (! failed)
817 if (sregno >= FIRST_PSEUDO_REGISTER)
819 reg_live_length[sregno] -= length;
820 /* reg_live_length is only an approximation after combine
821 if sched is not run, so make sure that we still have
822 a reasonable value. */
823 if (reg_live_length[sregno] < 2)
824 reg_live_length[sregno] = 2;
825 reg_n_calls_crossed[sregno] -= n_calls;
828 if (dregno >= FIRST_PSEUDO_REGISTER)
830 reg_live_length[dregno] += d_length;
831 reg_n_calls_crossed[dregno] += d_n_calls;
834 /* Move death note of SRC from P to INSN. */
835 remove_note (p, note);
836 XEXP (note, 1) = REG_NOTES (insn);
837 REG_NOTES (insn) = note;
840 /* Put death note of DEST on P if we saw it die. */
841 if (dest_death)
843 XEXP (dest_death, 1) = REG_NOTES (p);
844 REG_NOTES (p) = dest_death;
847 return;
850 /* If SRC is a hard register which is set or killed in some other
851 way, we can't do this optimization. */
852 else if (sregno < FIRST_PSEUDO_REGISTER
853 && dead_or_set_p (p, src))
854 break;
858 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
859 a sequence of insns that modify DEST followed by an insn that sets
860 SRC to DEST in which DEST dies, with no prior modification of DEST.
861 (There is no need to check if the insns in between actually modify
862 DEST. We should not have cases where DEST is not modified, but
863 the optimization is safe if no such modification is detected.)
864 In that case, we can replace all uses of DEST, starting with INSN and
865 ending with the set of SRC to DEST, with SRC. We do not do this
866 optimization if a CALL_INSN is crossed unless SRC already crosses a
867 call.
869 It is assumed that DEST and SRC are pseudos; it is too complicated to do
870 this for hard registers since the substitutions we may make might fail. */
872 static void
873 optimize_reg_copy_2 (insn, dest, src)
874 rtx insn;
875 rtx dest;
876 rtx src;
878 rtx p, q;
879 rtx set;
880 int sregno = REGNO (src);
881 int dregno = REGNO (dest);
883 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
885 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
886 || (GET_CODE (p) == NOTE
887 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
888 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
889 break;
891 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
892 continue;
894 set = single_set (p);
895 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
896 && find_reg_note (p, REG_DEAD, dest))
898 /* We can do the optimization. Scan forward from INSN again,
899 replacing regs as we go. */
901 /* Set to stop at next insn. */
902 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
903 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
905 if (reg_mentioned_p (dest, PATTERN (q)))
907 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
909 /* We assume that a register is used exactly once per
910 insn in the updates below. If this is not correct,
911 no great harm is done. */
912 reg_n_refs[dregno] -= loop_depth;
913 reg_n_refs[sregno] += loop_depth;
917 if (GET_CODE (q) == CALL_INSN)
919 reg_n_calls_crossed[dregno]--;
920 reg_n_calls_crossed[sregno]++;
924 remove_note (p, find_reg_note (p, REG_DEAD, dest));
925 reg_n_deaths[dregno]--;
926 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
927 reg_n_deaths[sregno]--;
928 return;
931 if (reg_set_p (src, p)
932 || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
933 break;
937 /* Find registers that are equivalent to a single value throughout the
938 compilation (either because they can be referenced in memory or are set once
939 from a single constant). Lower their priority for a register.
941 If such a register is only referenced once, try substituting its value
942 into the using insn. If it succeeds, we can eliminate the register
943 completely. */
945 static void
946 update_equiv_regs ()
948 rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
949 rtx *reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
950 rtx insn;
952 bzero ((char *) reg_equiv_init_insn, max_regno * sizeof (rtx *));
953 bzero ((char *) reg_equiv_replacement, max_regno * sizeof (rtx *));
955 init_alias_analysis ();
957 loop_depth = 1;
959 /* Scan the insns and find which registers have equivalences. Do this
960 in a separate scan of the insns because (due to -fcse-follow-jumps)
961 a register can be set below its use. */
962 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
964 rtx note;
965 rtx set = single_set (insn);
966 rtx dest;
967 int regno;
969 if (GET_CODE (insn) == NOTE)
971 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
972 loop_depth++;
973 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
974 loop_depth--;
977 /* If this insn contains more (or less) than a single SET, ignore it. */
978 if (set == 0)
979 continue;
981 dest = SET_DEST (set);
983 /* If this sets a MEM to the contents of a REG that is only used
984 in a single basic block, see if the register is always equivalent
985 to that memory location and if moving the store from INSN to the
986 insn that set REG is safe. If so, put a REG_EQUIV note on the
987 initializing insn. */
989 if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
990 && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
991 && reg_basic_block[regno] >= 0
992 && reg_equiv_init_insn[regno] != 0
993 && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
994 dest)
995 && ! memref_used_between_p (SET_DEST (set),
996 reg_equiv_init_insn[regno], insn))
997 REG_NOTES (reg_equiv_init_insn[regno])
998 = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
999 REG_NOTES (reg_equiv_init_insn[regno]));
1001 /* If this is a register-register copy where SRC is not dead, see if we
1002 can optimize it. */
1003 if (flag_expensive_optimizations && GET_CODE (dest) == REG
1004 && GET_CODE (SET_SRC (set)) == REG
1005 && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1006 optimize_reg_copy_1 (insn, dest, SET_SRC (set));
1008 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1009 else if (flag_expensive_optimizations && GET_CODE (dest) == REG
1010 && REGNO (dest) >= FIRST_PSEUDO_REGISTER
1011 && GET_CODE (SET_SRC (set)) == REG
1012 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
1013 && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1014 optimize_reg_copy_2 (insn, dest, SET_SRC (set));
1016 /* Otherwise, we only handle the case of a pseudo register being set
1017 once. */
1018 if (GET_CODE (dest) != REG
1019 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
1020 || reg_n_sets[regno] != 1)
1021 continue;
1023 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1025 /* Record this insn as initializing this register. */
1026 reg_equiv_init_insn[regno] = insn;
1028 /* If this register is known to be equal to a constant, record that
1029 it is always equivalent to the constant. */
1030 if (note && CONSTANT_P (XEXP (note, 0)))
1031 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
1033 /* If this insn introduces a "constant" register, decrease the priority
1034 of that register. Record this insn if the register is only used once
1035 more and the equivalence value is the same as our source.
1037 The latter condition is checked for two reasons: First, it is an
1038 indication that it may be more efficient to actually emit the insn
1039 as written (if no registers are available, reload will substitute
1040 the equivalence). Secondly, it avoids problems with any registers
1041 dying in this insn whose death notes would be missed.
1043 If we don't have a REG_EQUIV note, see if this insn is loading
1044 a register used only in one basic block from a MEM. If so, and the
1045 MEM remains unchanged for the life of the register, add a REG_EQUIV
1046 note. */
1048 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1050 if (note == 0 && reg_basic_block[regno] >= 0
1051 && GET_CODE (SET_SRC (set)) == MEM
1052 && validate_equiv_mem (insn, dest, SET_SRC (set)))
1053 REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
1054 REG_NOTES (insn));
1056 /* Don't mess with things live during setjmp. */
1057 if (note && reg_live_length[regno] >= 0)
1059 int regno = REGNO (dest);
1061 /* Note that the statement below does not affect the priority
1062 in local-alloc! */
1063 reg_live_length[regno] *= 2;
1065 /* If the register is referenced exactly twice, meaning it is set
1066 once and used once, indicate that the reference may be replaced
1067 by the equivalence we computed above. If the register is only
1068 used in one basic block, this can't succeed or combine would
1069 have done it.
1071 It would be nice to use "loop_depth * 2" in the compare
1072 below. Unfortunately, LOOP_DEPTH need not be constant within
1073 a basic block so this would be too complicated.
1075 This case normally occurs when a parameter is read from memory
1076 and then used exactly once, not in a loop. */
1078 if (reg_n_refs[regno] == 2
1079 && reg_basic_block[regno] < 0
1080 && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
1081 reg_equiv_replacement[regno] = SET_SRC (set);
1085 /* Now scan all regs killed in an insn to see if any of them are registers
1086 only used that once. If so, see if we can replace the reference with
1087 the equivalent from. If we can, delete the initializing reference
1088 and this register will go away. */
1089 for (insn = next_active_insn (get_insns ());
1090 insn;
1091 insn = next_active_insn (insn))
1093 rtx link;
1095 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1096 if (REG_NOTE_KIND (link) == REG_DEAD
1097 /* Make sure this insn still refers to the register. */
1098 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1100 int regno = REGNO (XEXP (link, 0));
1102 if (reg_equiv_replacement[regno]
1103 && validate_replace_rtx (regno_reg_rtx[regno],
1104 reg_equiv_replacement[regno], insn))
1106 rtx equiv_insn = reg_equiv_init_insn[regno];
1108 remove_death (regno, insn);
1109 reg_n_refs[regno] = 0;
1110 PUT_CODE (equiv_insn, NOTE);
1111 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1112 NOTE_SOURCE_FILE (equiv_insn) = 0;
1118 /* Allocate hard regs to the pseudo regs used only within block number B.
1119 Only the pseudos that die but once can be handled. */
1121 static void
1122 block_alloc (b)
1123 int b;
1125 register int i, q;
1126 register rtx insn;
1127 rtx note;
1128 int insn_number = 0;
1129 int insn_count = 0;
1130 int max_uid = get_max_uid ();
1131 int *qty_order;
1132 int no_conflict_combined_regno = -1;
1133 /* Counter to prevent allocating more SCRATCHes than can be stored
1134 in SCRATCH_LIST. */
1135 int scratches_allocated = scratch_index;
1137 /* Count the instructions in the basic block. */
1139 insn = basic_block_end[b];
1140 while (1)
1142 if (GET_CODE (insn) != NOTE)
1143 if (++insn_count > max_uid)
1144 abort ();
1145 if (insn == basic_block_head[b])
1146 break;
1147 insn = PREV_INSN (insn);
1150 /* +2 to leave room for a post_mark_life at the last insn and for
1151 the birth of a CLOBBER in the first insn. */
1152 regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1153 * sizeof (HARD_REG_SET));
1154 bzero ((char *) regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1156 /* Initialize table of hardware registers currently live. */
1158 #ifdef HARD_REG_SET
1159 regs_live = *basic_block_live_at_start[b];
1160 #else
1161 COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1162 #endif
1164 /* This loop scans the instructions of the basic block
1165 and assigns quantities to registers.
1166 It computes which registers to tie. */
1168 insn = basic_block_head[b];
1169 while (1)
1171 register rtx body = PATTERN (insn);
1173 if (GET_CODE (insn) != NOTE)
1174 insn_number++;
1176 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1178 register rtx link, set;
1179 register int win = 0;
1180 register rtx r0, r1;
1181 int combined_regno = -1;
1182 int i;
1183 int insn_code_number = recog_memoized (insn);
1185 this_insn_number = insn_number;
1186 this_insn = insn;
1188 if (insn_code_number >= 0)
1189 insn_extract (insn);
1190 which_alternative = -1;
1192 /* Is this insn suitable for tying two registers?
1193 If so, try doing that.
1194 Suitable insns are those with at least two operands and where
1195 operand 0 is an output that is a register that is not
1196 earlyclobber.
1198 We can tie operand 0 with some operand that dies in this insn.
1199 First look for operands that are required to be in the same
1200 register as operand 0. If we find such, only try tying that
1201 operand or one that can be put into that operand if the
1202 operation is commutative. If we don't find an operand
1203 that is required to be in the same register as operand 0,
1204 we can tie with any operand.
1206 Subregs in place of regs are also ok.
1208 If tying is done, WIN is set nonzero. */
1210 if (insn_code_number >= 0
1211 #ifdef REGISTER_CONSTRAINTS
1212 && insn_n_operands[insn_code_number] > 1
1213 && insn_operand_constraint[insn_code_number][0][0] == '='
1214 && insn_operand_constraint[insn_code_number][0][1] != '&'
1215 #else
1216 && GET_CODE (PATTERN (insn)) == SET
1217 && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1218 #endif
1221 #ifdef REGISTER_CONSTRAINTS
1222 /* If non-negative, is an operand that must match operand 0. */
1223 int must_match_0 = -1;
1224 /* Counts number of alternatives that require a match with
1225 operand 0. */
1226 int n_matching_alts = 0;
1228 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1230 char *p = insn_operand_constraint[insn_code_number][i];
1231 int this_match = (requires_inout (p));
1233 n_matching_alts += this_match;
1234 if (this_match == insn_n_alternatives[insn_code_number])
1235 must_match_0 = i;
1237 #endif
1239 r0 = recog_operand[0];
1240 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1242 #ifdef REGISTER_CONSTRAINTS
1243 /* Skip this operand if we found an operand that
1244 must match operand 0 and this operand isn't it
1245 and can't be made to be it by commutativity. */
1247 if (must_match_0 >= 0 && i != must_match_0
1248 && ! (i == must_match_0 + 1
1249 && insn_operand_constraint[insn_code_number][i-1][0] == '%')
1250 && ! (i == must_match_0 - 1
1251 && insn_operand_constraint[insn_code_number][i][0] == '%'))
1252 continue;
1254 /* Likewise if each alternative has some operand that
1255 must match operand zero. In that case, skip any
1256 operand that doesn't list operand 0 since we know that
1257 the operand always conflicts with operand 0. We
1258 ignore commutatity in this case to keep things simple. */
1259 if (n_matching_alts == insn_n_alternatives[insn_code_number]
1260 && (0 == requires_inout
1261 (insn_operand_constraint[insn_code_number][i])))
1262 continue;
1263 #endif
1265 r1 = recog_operand[i];
1267 /* If the operand is an address, find a register in it.
1268 There may be more than one register, but we only try one
1269 of them. */
1270 if (
1271 #ifdef REGISTER_CONSTRAINTS
1272 insn_operand_constraint[insn_code_number][i][0] == 'p'
1273 #else
1274 insn_operand_address_p[insn_code_number][i]
1275 #endif
1277 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1278 r1 = XEXP (r1, 0);
1280 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1282 /* We have two priorities for hard register preferences.
1283 If we have a move insn or an insn whose first input
1284 can only be in the same register as the output, give
1285 priority to an equivalence found from that insn. */
1286 int may_save_copy
1287 = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1288 #ifdef REGISTER_CONSTRAINTS
1289 || (r1 == recog_operand[i] && must_match_0 >= 0)
1290 #endif
1293 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1294 win = combine_regs (r1, r0, may_save_copy,
1295 insn_number, insn, 0);
1297 if (win)
1298 break;
1302 /* Recognize an insn sequence with an ultimate result
1303 which can safely overlap one of the inputs.
1304 The sequence begins with a CLOBBER of its result,
1305 and ends with an insn that copies the result to itself
1306 and has a REG_EQUAL note for an equivalent formula.
1307 That note indicates what the inputs are.
1308 The result and the input can overlap if each insn in
1309 the sequence either doesn't mention the input
1310 or has a REG_NO_CONFLICT note to inhibit the conflict.
1312 We do the combining test at the CLOBBER so that the
1313 destination register won't have had a quantity number
1314 assigned, since that would prevent combining. */
1316 if (GET_CODE (PATTERN (insn)) == CLOBBER
1317 && (r0 = XEXP (PATTERN (insn), 0),
1318 GET_CODE (r0) == REG)
1319 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1320 && XEXP (link, 0) != 0
1321 && GET_CODE (XEXP (link, 0)) == INSN
1322 && (set = single_set (XEXP (link, 0))) != 0
1323 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1324 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1325 NULL_RTX)) != 0)
1327 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1328 /* Check that we have such a sequence. */
1329 && no_conflict_p (insn, r0, r1))
1330 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1331 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1332 && (r1 = XEXP (XEXP (note, 0), 0),
1333 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1334 && no_conflict_p (insn, r0, r1))
1335 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1337 /* Here we care if the operation to be computed is
1338 commutative. */
1339 else if ((GET_CODE (XEXP (note, 0)) == EQ
1340 || GET_CODE (XEXP (note, 0)) == NE
1341 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1342 && (r1 = XEXP (XEXP (note, 0), 1),
1343 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1344 && no_conflict_p (insn, r0, r1))
1345 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1347 /* If we did combine something, show the register number
1348 in question so that we know to ignore its death. */
1349 if (win)
1350 no_conflict_combined_regno = REGNO (r1);
1353 /* If registers were just tied, set COMBINED_REGNO
1354 to the number of the register used in this insn
1355 that was tied to the register set in this insn.
1356 This register's qty should not be "killed". */
1358 if (win)
1360 while (GET_CODE (r1) == SUBREG)
1361 r1 = SUBREG_REG (r1);
1362 combined_regno = REGNO (r1);
1365 /* Mark the death of everything that dies in this instruction,
1366 except for anything that was just combined. */
1368 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1369 if (REG_NOTE_KIND (link) == REG_DEAD
1370 && GET_CODE (XEXP (link, 0)) == REG
1371 && combined_regno != REGNO (XEXP (link, 0))
1372 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1373 || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1374 wipe_dead_reg (XEXP (link, 0), 0);
1376 /* Allocate qty numbers for all registers local to this block
1377 that are born (set) in this instruction.
1378 A pseudo that already has a qty is not changed. */
1380 note_stores (PATTERN (insn), reg_is_set);
1382 /* If anything is set in this insn and then unused, mark it as dying
1383 after this insn, so it will conflict with our outputs. This
1384 can't match with something that combined, and it doesn't matter
1385 if it did. Do this after the calls to reg_is_set since these
1386 die after, not during, the current insn. */
1388 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1389 if (REG_NOTE_KIND (link) == REG_UNUSED
1390 && GET_CODE (XEXP (link, 0)) == REG)
1391 wipe_dead_reg (XEXP (link, 0), 1);
1393 /* Allocate quantities for any SCRATCH operands of this insn. */
1395 if (insn_code_number >= 0)
1396 for (i = 0; i < insn_n_operands[insn_code_number]; i++)
1397 if (GET_CODE (recog_operand[i]) == SCRATCH
1398 && scratches_allocated++ < scratch_list_length)
1399 alloc_qty_for_scratch (recog_operand[i], i, insn,
1400 insn_code_number, insn_number);
1402 /* If this is an insn that has a REG_RETVAL note pointing at a
1403 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1404 block, so clear any register number that combined within it. */
1405 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1406 && GET_CODE (XEXP (note, 0)) == INSN
1407 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1408 no_conflict_combined_regno = -1;
1411 /* Set the registers live after INSN_NUMBER. Note that we never
1412 record the registers live before the block's first insn, since no
1413 pseudos we care about are live before that insn. */
1415 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1416 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1418 if (insn == basic_block_end[b])
1419 break;
1421 insn = NEXT_INSN (insn);
1424 /* Now every register that is local to this basic block
1425 should have been given a quantity, or else -1 meaning ignore it.
1426 Every quantity should have a known birth and death.
1428 Order the qtys so we assign them registers in order of the
1429 number of suggested registers they need so we allocate those with
1430 the most restrictive needs first. */
1432 qty_order = (int *) alloca (next_qty * sizeof (int));
1433 for (i = 0; i < next_qty; i++)
1434 qty_order[i] = i;
1436 #define EXCHANGE(I1, I2) \
1437 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1439 switch (next_qty)
1441 case 3:
1442 /* Make qty_order[2] be the one to allocate last. */
1443 if (qty_sugg_compare (0, 1) > 0)
1444 EXCHANGE (0, 1);
1445 if (qty_sugg_compare (1, 2) > 0)
1446 EXCHANGE (2, 1);
1448 /* ... Fall through ... */
1449 case 2:
1450 /* Put the best one to allocate in qty_order[0]. */
1451 if (qty_sugg_compare (0, 1) > 0)
1452 EXCHANGE (0, 1);
1454 /* ... Fall through ... */
1456 case 1:
1457 case 0:
1458 /* Nothing to do here. */
1459 break;
1461 default:
1462 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1465 /* Try to put each quantity in a suggested physical register, if it has one.
1466 This may cause registers to be allocated that otherwise wouldn't be, but
1467 this seems acceptable in local allocation (unlike global allocation). */
1468 for (i = 0; i < next_qty; i++)
1470 q = qty_order[i];
1471 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1472 qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1473 0, 1, qty_birth[q], qty_death[q]);
1474 else
1475 qty_phys_reg[q] = -1;
1478 /* Order the qtys so we assign them registers in order of
1479 decreasing length of life. Normally call qsort, but if we
1480 have only a very small number of quantities, sort them ourselves. */
1482 for (i = 0; i < next_qty; i++)
1483 qty_order[i] = i;
1485 #define EXCHANGE(I1, I2) \
1486 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1488 switch (next_qty)
1490 case 3:
1491 /* Make qty_order[2] be the one to allocate last. */
1492 if (qty_compare (0, 1) > 0)
1493 EXCHANGE (0, 1);
1494 if (qty_compare (1, 2) > 0)
1495 EXCHANGE (2, 1);
1497 /* ... Fall through ... */
1498 case 2:
1499 /* Put the best one to allocate in qty_order[0]. */
1500 if (qty_compare (0, 1) > 0)
1501 EXCHANGE (0, 1);
1503 /* ... Fall through ... */
1505 case 1:
1506 case 0:
1507 /* Nothing to do here. */
1508 break;
1510 default:
1511 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1514 /* Now for each qty that is not a hardware register,
1515 look for a hardware register to put it in.
1516 First try the register class that is cheapest for this qty,
1517 if there is more than one class. */
1519 for (i = 0; i < next_qty; i++)
1521 q = qty_order[i];
1522 if (qty_phys_reg[q] < 0)
1524 if (N_REG_CLASSES > 1)
1526 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1527 qty_mode[q], q, 0, 0,
1528 qty_birth[q], qty_death[q]);
1529 if (qty_phys_reg[q] >= 0)
1530 continue;
1533 if (qty_alternate_class[q] != NO_REGS)
1534 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1535 qty_mode[q], q, 0, 0,
1536 qty_birth[q], qty_death[q]);
1540 /* Now propagate the register assignments
1541 to the pseudo regs belonging to the qtys. */
1543 for (q = 0; q < next_qty; q++)
1544 if (qty_phys_reg[q] >= 0)
1546 for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1547 reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1548 if (qty_scratch_rtx[q])
1550 if (GET_CODE (qty_scratch_rtx[q]) == REG)
1551 abort ();
1552 PUT_CODE (qty_scratch_rtx[q], REG);
1553 REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
1555 scratch_block[scratch_index] = b;
1556 scratch_list[scratch_index++] = qty_scratch_rtx[q];
1558 /* Must clear the USED field, because it will have been set by
1559 copy_rtx_if_shared, but the leaf_register code expects that
1560 it is zero in all REG rtx. copy_rtx_if_shared does not set the
1561 used bit for REGs, but does for SCRATCHes. */
1562 qty_scratch_rtx[q]->used = 0;
1567 /* Compare two quantities' priority for getting real registers.
1568 We give shorter-lived quantities higher priority.
1569 Quantities with more references are also preferred, as are quantities that
1570 require multiple registers. This is the identical prioritization as
1571 done by global-alloc.
1573 We used to give preference to registers with *longer* lives, but using
1574 the same algorithm in both local- and global-alloc can speed up execution
1575 of some programs by as much as a factor of three! */
1577 static int
1578 qty_compare (q1, q2)
1579 int q1, q2;
1581 /* Note that the quotient will never be bigger than
1582 the value of floor_log2 times the maximum number of
1583 times a register can occur in one insn (surely less than 100).
1584 Multiplying this by 10000 can't overflow. */
1585 register int pri1
1586 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1587 / (qty_death[q1] - qty_birth[q1]))
1588 * 10000);
1589 register int pri2
1590 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1591 / (qty_death[q2] - qty_birth[q2]))
1592 * 10000);
1593 return pri2 - pri1;
1596 static int
1597 qty_compare_1 (q1, q2)
1598 int *q1, *q2;
1600 register int tem;
1602 /* Note that the quotient will never be bigger than
1603 the value of floor_log2 times the maximum number of
1604 times a register can occur in one insn (surely less than 100).
1605 Multiplying this by 10000 can't overflow. */
1606 register int pri1
1607 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1608 * qty_size[*q1])
1609 / (qty_death[*q1] - qty_birth[*q1]))
1610 * 10000);
1611 register int pri2
1612 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1613 * qty_size[*q2])
1614 / (qty_death[*q2] - qty_birth[*q2]))
1615 * 10000);
1617 tem = pri2 - pri1;
1618 if (tem != 0) return tem;
1619 /* If qtys are equally good, sort by qty number,
1620 so that the results of qsort leave nothing to chance. */
1621 return *q1 - *q2;
1624 /* Compare two quantities' priority for getting real registers. This version
1625 is called for quantities that have suggested hard registers. First priority
1626 goes to quantities that have copy preferences, then to those that have
1627 normal preferences. Within those groups, quantities with the lower
1628 number of preferenes have the highest priority. Of those, we use the same
1629 algorithm as above. */
1631 static int
1632 qty_sugg_compare (q1, q2)
1633 int q1, q2;
1635 register int sugg1 = (qty_phys_num_copy_sugg[q1]
1636 ? qty_phys_num_copy_sugg[q1]
1637 : qty_phys_num_sugg[q1] * FIRST_PSEUDO_REGISTER);
1638 register int sugg2 = (qty_phys_num_copy_sugg[q2]
1639 ? qty_phys_num_copy_sugg[q2]
1640 : qty_phys_num_sugg[q2] * FIRST_PSEUDO_REGISTER);
1641 /* Note that the quotient will never be bigger than
1642 the value of floor_log2 times the maximum number of
1643 times a register can occur in one insn (surely less than 100).
1644 Multiplying this by 10000 can't overflow. */
1645 register int pri1
1646 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1647 / (qty_death[q1] - qty_birth[q1]))
1648 * 10000);
1649 register int pri2
1650 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1651 / (qty_death[q2] - qty_birth[q2]))
1652 * 10000);
1654 if (sugg1 != sugg2)
1655 return sugg1 - sugg2;
1657 return pri2 - pri1;
1660 static int
1661 qty_sugg_compare_1 (q1, q2)
1662 int *q1, *q2;
1664 register int sugg1 = (qty_phys_num_copy_sugg[*q1]
1665 ? qty_phys_num_copy_sugg[*q1]
1666 : qty_phys_num_sugg[*q1] * FIRST_PSEUDO_REGISTER);
1667 register int sugg2 = (qty_phys_num_copy_sugg[*q2]
1668 ? qty_phys_num_copy_sugg[*q2]
1669 : qty_phys_num_sugg[*q2] * FIRST_PSEUDO_REGISTER);
1671 /* Note that the quotient will never be bigger than
1672 the value of floor_log2 times the maximum number of
1673 times a register can occur in one insn (surely less than 100).
1674 Multiplying this by 10000 can't overflow. */
1675 register int pri1
1676 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1677 * qty_size[*q1])
1678 / (qty_death[*q1] - qty_birth[*q1]))
1679 * 10000);
1680 register int pri2
1681 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1682 * qty_size[*q2])
1683 / (qty_death[*q2] - qty_birth[*q2]))
1684 * 10000);
1686 if (sugg1 != sugg2)
1687 return sugg1 - sugg2;
1689 if (pri1 != pri2)
1690 return pri2 - pri1;
1692 /* If qtys are equally good, sort by qty number,
1693 so that the results of qsort leave nothing to chance. */
1694 return *q1 - *q2;
1697 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1698 Returns 1 if have done so, or 0 if cannot.
1700 Combining registers means marking them as having the same quantity
1701 and adjusting the offsets within the quantity if either of
1702 them is a SUBREG).
1704 We don't actually combine a hard reg with a pseudo; instead
1705 we just record the hard reg as the suggestion for the pseudo's quantity.
1706 If we really combined them, we could lose if the pseudo lives
1707 across an insn that clobbers the hard reg (eg, movstr).
1709 ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1710 there is no REG_DEAD note on INSN. This occurs during the processing
1711 of REG_NO_CONFLICT blocks.
1713 MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1714 SETREG or if the input and output must share a register.
1715 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1717 There are elaborate checks for the validity of combining. */
1720 static int
1721 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1722 rtx usedreg, setreg;
1723 int may_save_copy;
1724 int insn_number;
1725 rtx insn;
1726 int already_dead;
1728 register int ureg, sreg;
1729 register int offset = 0;
1730 int usize, ssize;
1731 register int sqty;
1733 /* Determine the numbers and sizes of registers being used. If a subreg
1734 is present that does not change the entire register, don't consider
1735 this a copy insn. */
1737 while (GET_CODE (usedreg) == SUBREG)
1739 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1740 may_save_copy = 0;
1741 offset += SUBREG_WORD (usedreg);
1742 usedreg = SUBREG_REG (usedreg);
1744 if (GET_CODE (usedreg) != REG)
1745 return 0;
1746 ureg = REGNO (usedreg);
1747 usize = REG_SIZE (usedreg);
1749 while (GET_CODE (setreg) == SUBREG)
1751 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1752 may_save_copy = 0;
1753 offset -= SUBREG_WORD (setreg);
1754 setreg = SUBREG_REG (setreg);
1756 if (GET_CODE (setreg) != REG)
1757 return 0;
1758 sreg = REGNO (setreg);
1759 ssize = REG_SIZE (setreg);
1761 /* If UREG is a pseudo-register that hasn't already been assigned a
1762 quantity number, it means that it is not local to this block or dies
1763 more than once. In either event, we can't do anything with it. */
1764 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1765 /* Do not combine registers unless one fits within the other. */
1766 || (offset > 0 && usize + offset > ssize)
1767 || (offset < 0 && usize + offset < ssize)
1768 /* Do not combine with a smaller already-assigned object
1769 if that smaller object is already combined with something bigger. */
1770 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1771 && usize < qty_size[reg_qty[ureg]])
1772 /* Can't combine if SREG is not a register we can allocate. */
1773 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1774 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1775 These have already been taken care of. This probably wouldn't
1776 combine anyway, but don't take any chances. */
1777 || (ureg >= FIRST_PSEUDO_REGISTER
1778 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1779 /* Don't tie something to itself. In most cases it would make no
1780 difference, but it would screw up if the reg being tied to itself
1781 also dies in this insn. */
1782 || ureg == sreg
1783 /* Don't try to connect two different hardware registers. */
1784 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1785 /* Don't connect two different machine modes if they have different
1786 implications as to which registers may be used. */
1787 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1788 return 0;
1790 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1791 qty_phys_sugg for the pseudo instead of tying them.
1793 Return "failure" so that the lifespan of UREG is terminated here;
1794 that way the two lifespans will be disjoint and nothing will prevent
1795 the pseudo reg from being given this hard reg. */
1797 if (ureg < FIRST_PSEUDO_REGISTER)
1799 /* Allocate a quantity number so we have a place to put our
1800 suggestions. */
1801 if (reg_qty[sreg] == -2)
1802 reg_is_born (setreg, 2 * insn_number);
1804 if (reg_qty[sreg] >= 0)
1806 if (may_save_copy
1807 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1809 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1810 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1812 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1814 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1815 qty_phys_num_sugg[reg_qty[sreg]]++;
1818 return 0;
1821 /* Similarly for SREG a hard register and UREG a pseudo register. */
1823 if (sreg < FIRST_PSEUDO_REGISTER)
1825 if (may_save_copy
1826 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1828 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1829 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1831 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1833 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1834 qty_phys_num_sugg[reg_qty[ureg]]++;
1836 return 0;
1839 /* At this point we know that SREG and UREG are both pseudos.
1840 Do nothing if SREG already has a quantity or is a register that we
1841 don't allocate. */
1842 if (reg_qty[sreg] >= -1
1843 /* If we are not going to let any regs live across calls,
1844 don't tie a call-crossing reg to a non-call-crossing reg. */
1845 || (current_function_has_nonlocal_label
1846 && ((reg_n_calls_crossed[ureg] > 0)
1847 != (reg_n_calls_crossed[sreg] > 0))))
1848 return 0;
1850 /* We don't already know about SREG, so tie it to UREG
1851 if this is the last use of UREG, provided the classes they want
1852 are compatible. */
1854 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1855 && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1857 /* Add SREG to UREG's quantity. */
1858 sqty = reg_qty[ureg];
1859 reg_qty[sreg] = sqty;
1860 reg_offset[sreg] = reg_offset[ureg] + offset;
1861 reg_next_in_qty[sreg] = qty_first_reg[sqty];
1862 qty_first_reg[sqty] = sreg;
1864 /* If SREG's reg class is smaller, set qty_min_class[SQTY]. */
1865 update_qty_class (sqty, sreg);
1867 /* Update info about quantity SQTY. */
1868 qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
1869 qty_n_refs[sqty] += reg_n_refs[sreg];
1870 if (usize < ssize)
1872 register int i;
1874 for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1875 reg_offset[i] -= offset;
1877 qty_size[sqty] = ssize;
1878 qty_mode[sqty] = GET_MODE (setreg);
1881 else
1882 return 0;
1884 return 1;
1887 /* Return 1 if the preferred class of REG allows it to be tied
1888 to a quantity or register whose class is CLASS.
1889 True if REG's reg class either contains or is contained in CLASS. */
1891 static int
1892 reg_meets_class_p (reg, class)
1893 int reg;
1894 enum reg_class class;
1896 register enum reg_class rclass = reg_preferred_class (reg);
1897 return (reg_class_subset_p (rclass, class)
1898 || reg_class_subset_p (class, rclass));
1901 /* Return 1 if the two specified classes have registers in common.
1902 If CALL_SAVED, then consider only call-saved registers. */
1904 static int
1905 reg_classes_overlap_p (c1, c2, call_saved)
1906 register enum reg_class c1;
1907 register enum reg_class c2;
1908 int call_saved;
1910 HARD_REG_SET c;
1911 int i;
1913 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1914 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1916 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1917 if (TEST_HARD_REG_BIT (c, i)
1918 && (! call_saved || ! call_used_regs[i]))
1919 return 1;
1921 return 0;
1924 /* Update the class of QTY assuming that REG is being tied to it. */
1926 static void
1927 update_qty_class (qty, reg)
1928 int qty;
1929 int reg;
1931 enum reg_class rclass = reg_preferred_class (reg);
1932 if (reg_class_subset_p (rclass, qty_min_class[qty]))
1933 qty_min_class[qty] = rclass;
1935 rclass = reg_alternate_class (reg);
1936 if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1937 qty_alternate_class[qty] = rclass;
1939 if (reg_changes_size[reg])
1940 qty_changes_size[qty] = 1;
1943 /* Handle something which alters the value of an rtx REG.
1945 REG is whatever is set or clobbered. SETTER is the rtx that
1946 is modifying the register.
1948 If it is not really a register, we do nothing.
1949 The file-global variables `this_insn' and `this_insn_number'
1950 carry info from `block_alloc'. */
1952 static void
1953 reg_is_set (reg, setter)
1954 rtx reg;
1955 rtx setter;
1957 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1958 a hard register. These may actually not exist any more. */
1960 if (GET_CODE (reg) != SUBREG
1961 && GET_CODE (reg) != REG)
1962 return;
1964 /* Mark this register as being born. If it is used in a CLOBBER, mark
1965 it as being born halfway between the previous insn and this insn so that
1966 it conflicts with our inputs but not the outputs of the previous insn. */
1968 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1971 /* Handle beginning of the life of register REG.
1972 BIRTH is the index at which this is happening. */
1974 static void
1975 reg_is_born (reg, birth)
1976 rtx reg;
1977 int birth;
1979 register int regno;
1981 if (GET_CODE (reg) == SUBREG)
1982 regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1983 else
1984 regno = REGNO (reg);
1986 if (regno < FIRST_PSEUDO_REGISTER)
1988 mark_life (regno, GET_MODE (reg), 1);
1990 /* If the register was to have been born earlier that the present
1991 insn, mark it as live where it is actually born. */
1992 if (birth < 2 * this_insn_number)
1993 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
1995 else
1997 if (reg_qty[regno] == -2)
1998 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
2000 /* If this register has a quantity number, show that it isn't dead. */
2001 if (reg_qty[regno] >= 0)
2002 qty_death[reg_qty[regno]] = -1;
2006 /* Record the death of REG in the current insn. If OUTPUT_P is non-zero,
2007 REG is an output that is dying (i.e., it is never used), otherwise it
2008 is an input (the normal case).
2009 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
2011 static void
2012 wipe_dead_reg (reg, output_p)
2013 register rtx reg;
2014 int output_p;
2016 register int regno = REGNO (reg);
2018 /* If this insn has multiple results,
2019 and the dead reg is used in one of the results,
2020 extend its life to after this insn,
2021 so it won't get allocated together with any other result of this insn. */
2022 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2023 && !single_set (this_insn))
2025 int i;
2026 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2028 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2029 if (GET_CODE (set) == SET
2030 && GET_CODE (SET_DEST (set)) != REG
2031 && !rtx_equal_p (reg, SET_DEST (set))
2032 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2033 output_p = 1;
2037 if (regno < FIRST_PSEUDO_REGISTER)
2039 mark_life (regno, GET_MODE (reg), 0);
2041 /* If a hard register is dying as an output, mark it as in use at
2042 the beginning of this insn (the above statement would cause this
2043 not to happen). */
2044 if (output_p)
2045 post_mark_life (regno, GET_MODE (reg), 1,
2046 2 * this_insn_number, 2 * this_insn_number+ 1);
2049 else if (reg_qty[regno] >= 0)
2050 qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
2053 /* Find a block of SIZE words of hard regs in reg_class CLASS
2054 that can hold something of machine-mode MODE
2055 (but actually we test only the first of the block for holding MODE)
2056 and still free between insn BORN_INDEX and insn DEAD_INDEX,
2057 and return the number of the first of them.
2058 Return -1 if such a block cannot be found.
2059 If QTY crosses calls, insist on a register preserved by calls,
2060 unless ACCEPT_CALL_CLOBBERED is nonzero.
2062 If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
2063 register is available. If not, return -1. */
2065 static int
2066 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
2067 born_index, dead_index)
2068 enum reg_class class;
2069 enum machine_mode mode;
2070 int qty;
2071 int accept_call_clobbered;
2072 int just_try_suggested;
2073 int born_index, dead_index;
2075 register int i, ins;
2076 #ifdef HARD_REG_SET
2077 register /* Declare it register if it's a scalar. */
2078 #endif
2079 HARD_REG_SET used, first_used;
2080 #ifdef ELIMINABLE_REGS
2081 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2082 #endif
2084 /* Validate our parameters. */
2085 if (born_index < 0 || born_index > dead_index)
2086 abort ();
2088 /* Don't let a pseudo live in a reg across a function call
2089 if we might get a nonlocal goto. */
2090 if (current_function_has_nonlocal_label
2091 && qty_n_calls_crossed[qty] > 0)
2092 return -1;
2094 if (accept_call_clobbered)
2095 COPY_HARD_REG_SET (used, call_fixed_reg_set);
2096 else if (qty_n_calls_crossed[qty] == 0)
2097 COPY_HARD_REG_SET (used, fixed_reg_set);
2098 else
2099 COPY_HARD_REG_SET (used, call_used_reg_set);
2101 for (ins = born_index; ins < dead_index; ins++)
2102 IOR_HARD_REG_SET (used, regs_live_at[ins]);
2104 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2106 /* Don't use the frame pointer reg in local-alloc even if
2107 we may omit the frame pointer, because if we do that and then we
2108 need a frame pointer, reload won't know how to move the pseudo
2109 to another hard reg. It can move only regs made by global-alloc.
2111 This is true of any register that can be eliminated. */
2112 #ifdef ELIMINABLE_REGS
2113 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2114 SET_HARD_REG_BIT (used, eliminables[i].from);
2115 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2116 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2117 that it might be eliminated into. */
2118 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2119 #endif
2120 #else
2121 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2122 #endif
2124 #ifdef CLASS_CANNOT_CHANGE_SIZE
2125 if (qty_changes_size[qty])
2126 IOR_HARD_REG_SET (used,
2127 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
2128 #endif
2130 /* Normally, the registers that can be used for the first register in
2131 a multi-register quantity are the same as those that can be used for
2132 subsequent registers. However, if just trying suggested registers,
2133 restrict our consideration to them. If there are copy-suggested
2134 register, try them. Otherwise, try the arithmetic-suggested
2135 registers. */
2136 COPY_HARD_REG_SET (first_used, used);
2138 if (just_try_suggested)
2140 if (qty_phys_num_copy_sugg[qty] != 0)
2141 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2142 else
2143 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2146 /* If all registers are excluded, we can't do anything. */
2147 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2149 /* If at least one would be suitable, test each hard reg. */
2151 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2153 #ifdef REG_ALLOC_ORDER
2154 int regno = reg_alloc_order[i];
2155 #else
2156 int regno = i;
2157 #endif
2158 if (! TEST_HARD_REG_BIT (first_used, regno)
2159 && HARD_REGNO_MODE_OK (regno, mode))
2161 register int j;
2162 register int size1 = HARD_REGNO_NREGS (regno, mode);
2163 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2164 if (j == size1)
2166 /* Mark that this register is in use between its birth and death
2167 insns. */
2168 post_mark_life (regno, mode, 1, born_index, dead_index);
2169 return regno;
2171 #ifndef REG_ALLOC_ORDER
2172 i += j; /* Skip starting points we know will lose */
2173 #endif
2177 fail:
2179 /* If we are just trying suggested register, we have just tried copy-
2180 suggested registers, and there are arithmetic-suggested registers,
2181 try them. */
2183 /* If it would be profitable to allocate a call-clobbered register
2184 and save and restore it around calls, do that. */
2185 if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2186 && qty_phys_num_sugg[qty] != 0)
2188 /* Don't try the copy-suggested regs again. */
2189 qty_phys_num_copy_sugg[qty] = 0;
2190 return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2191 born_index, dead_index);
2194 /* We need not check to see if the current function has nonlocal
2195 labels because we don't put any pseudos that are live over calls in
2196 registers in that case. */
2198 if (! accept_call_clobbered
2199 && flag_caller_saves
2200 && ! just_try_suggested
2201 && qty_n_calls_crossed[qty] != 0
2202 && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2204 i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2205 if (i >= 0)
2206 caller_save_needed = 1;
2207 return i;
2209 return -1;
2212 /* Mark that REGNO with machine-mode MODE is live starting from the current
2213 insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2214 is zero). */
2216 static void
2217 mark_life (regno, mode, life)
2218 register int regno;
2219 enum machine_mode mode;
2220 int life;
2222 register int j = HARD_REGNO_NREGS (regno, mode);
2223 if (life)
2224 while (--j >= 0)
2225 SET_HARD_REG_BIT (regs_live, regno + j);
2226 else
2227 while (--j >= 0)
2228 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2231 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2232 is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2233 to insn number DEATH (exclusive). */
2235 static void
2236 post_mark_life (regno, mode, life, birth, death)
2237 int regno;
2238 enum machine_mode mode;
2239 int life, birth, death;
2241 register int j = HARD_REGNO_NREGS (regno, mode);
2242 #ifdef HARD_REG_SET
2243 register /* Declare it register if it's a scalar. */
2244 #endif
2245 HARD_REG_SET this_reg;
2247 CLEAR_HARD_REG_SET (this_reg);
2248 while (--j >= 0)
2249 SET_HARD_REG_BIT (this_reg, regno + j);
2251 if (life)
2252 while (birth < death)
2254 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2255 birth++;
2257 else
2258 while (birth < death)
2260 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2261 birth++;
2265 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2266 is the register being clobbered, and R1 is a register being used in
2267 the equivalent expression.
2269 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2270 in which it is used, return 1.
2272 Otherwise, return 0. */
2274 static int
2275 no_conflict_p (insn, r0, r1)
2276 rtx insn, r0, r1;
2278 int ok = 0;
2279 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2280 rtx p, last;
2282 /* If R1 is a hard register, return 0 since we handle this case
2283 when we scan the insns that actually use it. */
2285 if (note == 0
2286 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2287 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2288 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2289 return 0;
2291 last = XEXP (note, 0);
2293 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2294 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2296 if (find_reg_note (p, REG_DEAD, r1))
2297 ok = 1;
2299 if (reg_mentioned_p (r1, PATTERN (p))
2300 && ! find_reg_note (p, REG_NO_CONFLICT, r1))
2301 return 0;
2304 return ok;
2307 #ifdef REGISTER_CONSTRAINTS
2309 /* Return the number of alternatives for which the constraint string P
2310 indicates that the operand must be equal to operand 0 and that no register
2311 is acceptable. */
2313 static int
2314 requires_inout (p)
2315 char *p;
2317 char c;
2318 int found_zero = 0;
2319 int reg_allowed = 0;
2320 int num_matching_alts = 0;
2322 while (c = *p++)
2323 switch (c)
2325 case '=': case '+': case '?':
2326 case '#': case '&': case '!':
2327 case '*': case '%':
2328 case '1': case '2': case '3': case '4':
2329 case 'm': case '<': case '>': case 'V': case 'o':
2330 case 'E': case 'F': case 'G': case 'H':
2331 case 's': case 'i': case 'n':
2332 case 'I': case 'J': case 'K': case 'L':
2333 case 'M': case 'N': case 'O': case 'P':
2334 #ifdef EXTRA_CONSTRAINT
2335 case 'Q': case 'R': case 'S': case 'T': case 'U':
2336 #endif
2337 case 'X':
2338 /* These don't say anything we care about. */
2339 break;
2341 case ',':
2342 if (found_zero && ! reg_allowed)
2343 num_matching_alts++;
2345 found_zero = reg_allowed = 0;
2346 break;
2348 case '0':
2349 found_zero = 1;
2350 break;
2352 case 'p':
2353 case 'g': case 'r':
2354 default:
2355 reg_allowed = 1;
2356 break;
2359 if (found_zero && ! reg_allowed)
2360 num_matching_alts++;
2362 return num_matching_alts;
2364 #endif /* REGISTER_CONSTRAINTS */
2366 void
2367 dump_local_alloc (file)
2368 FILE *file;
2370 register int i;
2371 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2372 if (reg_renumber[i] != -1)
2373 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);