(ASM_SPEC): Change {% to %{.
[official-gcc.git] / gcc / local-alloc.c
blob156fe92d53748dd097a46ec5088f20315768a4ee
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 88, 91, 93, 94, 95, 1996 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* Allocation of hard register numbers to pseudo registers is done in
23 two passes. In this pass we consider only regs that are born and
24 die once within one basic block. We do this one basic block at a
25 time. Then the next pass allocates the registers that remain.
26 Two passes are used because this pass uses methods that work only
27 on linear code, but that do a better job than the general methods
28 used in global_alloc, and more quickly too.
30 The assignments made are recorded in the vector reg_renumber
31 whose space is allocated here. The rtl code itself is not altered.
33 We assign each instruction in the basic block a number
34 which is its order from the beginning of the block.
35 Then we can represent the lifetime of a pseudo register with
36 a pair of numbers, and check for conflicts easily.
37 We can record the availability of hard registers with a
38 HARD_REG_SET for each instruction. The HARD_REG_SET
39 contains 0 or 1 for each hard reg.
41 To avoid register shuffling, we tie registers together when one
42 dies by being copied into another, or dies in an instruction that
43 does arithmetic to produce another. The tied registers are
44 allocated as one. Registers with different reg class preferences
45 can never be tied unless the class preferred by one is a subclass
46 of the one preferred by the other.
48 Tying is represented with "quantity numbers".
49 A non-tied register is given a new quantity number.
50 Tied registers have the same quantity number.
52 We have provision to exempt registers, even when they are contained
53 within the block, that can be tied to others that are not contained in it.
54 This is so that global_alloc could process them both and tie them then.
55 But this is currently disabled since tying in global_alloc is not
56 yet implemented. */
58 /* Pseudos allocated here cannot be reallocated by global.c if the hard
59 register is used as a spill register. So we don't allocate such pseudos
60 here if their preferred class is likely to be used by spills. */
62 #include <stdio.h>
63 #include "config.h"
64 #include "rtl.h"
65 #include "flags.h"
66 #include "basic-block.h"
67 #include "regs.h"
68 #include "hard-reg-set.h"
69 #include "insn-config.h"
70 #include "recog.h"
71 #include "output.h"
73 /* Next quantity number available for allocation. */
75 static int next_qty;
77 /* In all the following vectors indexed by quantity number. */
79 /* Element Q is the hard reg number chosen for quantity Q,
80 or -1 if none was found. */
82 static short *qty_phys_reg;
84 /* We maintain two hard register sets that indicate suggested hard registers
85 for each quantity. The first, qty_phys_copy_sugg, contains hard registers
86 that are tied to the quantity by a simple copy. The second contains all
87 hard registers that are tied to the quantity via an arithmetic operation.
89 The former register set is given priority for allocation. This tends to
90 eliminate copy insns. */
92 /* Element Q is a set of hard registers that are suggested for quantity Q by
93 copy insns. */
95 static HARD_REG_SET *qty_phys_copy_sugg;
97 /* Element Q is a set of hard registers that are suggested for quantity Q by
98 arithmetic insns. */
100 static HARD_REG_SET *qty_phys_sugg;
102 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
104 static short *qty_phys_num_copy_sugg;
106 /* Element Q is the number of suggested registers in qty_phys_sugg. */
108 static short *qty_phys_num_sugg;
110 /* Element Q is the number of refs to quantity Q. */
112 static int *qty_n_refs;
114 /* Element Q is a reg class contained in (smaller than) the
115 preferred classes of all the pseudo regs that are tied in quantity Q.
116 This is the preferred class for allocating that quantity. */
118 static enum reg_class *qty_min_class;
120 /* Insn number (counting from head of basic block)
121 where quantity Q was born. -1 if birth has not been recorded. */
123 static int *qty_birth;
125 /* Insn number (counting from head of basic block)
126 where quantity Q died. Due to the way tying is done,
127 and the fact that we consider in this pass only regs that die but once,
128 a quantity can die only once. Each quantity's life span
129 is a set of consecutive insns. -1 if death has not been recorded. */
131 static int *qty_death;
133 /* Number of words needed to hold the data in quantity Q.
134 This depends on its machine mode. It is used for these purposes:
135 1. It is used in computing the relative importances of qtys,
136 which determines the order in which we look for regs for them.
137 2. It is used in rules that prevent tying several registers of
138 different sizes in a way that is geometrically impossible
139 (see combine_regs). */
141 static int *qty_size;
143 /* This holds the mode of the registers that are tied to qty Q,
144 or VOIDmode if registers with differing modes are tied together. */
146 static enum machine_mode *qty_mode;
148 /* Number of times a reg tied to qty Q lives across a CALL_INSN. */
150 static int *qty_n_calls_crossed;
152 /* Register class within which we allocate qty Q if we can't get
153 its preferred class. */
155 static enum reg_class *qty_alternate_class;
157 /* Element Q is the SCRATCH expression for which this quantity is being
158 allocated or 0 if this quantity is allocating registers. */
160 static rtx *qty_scratch_rtx;
162 /* Element Q is nonzero if this quantity has been used in a SUBREG
163 that changes its size. */
165 static char *qty_changes_size;
167 /* Element Q is the register number of one pseudo register whose
168 reg_qty value is Q, or -1 is this quantity is for a SCRATCH. This
169 register should be the head of the chain maintained in reg_next_in_qty. */
171 static int *qty_first_reg;
173 /* If (REG N) has been assigned a quantity number, is a register number
174 of another register assigned the same quantity number, or -1 for the
175 end of the chain. qty_first_reg point to the head of this chain. */
177 static int *reg_next_in_qty;
179 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
180 if it is >= 0,
181 of -1 if this register cannot be allocated by local-alloc,
182 or -2 if not known yet.
184 Note that if we see a use or death of pseudo register N with
185 reg_qty[N] == -2, register N must be local to the current block. If
186 it were used in more than one block, we would have reg_qty[N] == -1.
187 This relies on the fact that if reg_basic_block[N] is >= 0, register N
188 will not appear in any other block. We save a considerable number of
189 tests by exploiting this.
191 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
192 be referenced. */
194 static int *reg_qty;
196 /* The offset (in words) of register N within its quantity.
197 This can be nonzero if register N is SImode, and has been tied
198 to a subreg of a DImode register. */
200 static char *reg_offset;
202 /* Vector of substitutions of register numbers,
203 used to map pseudo regs into hardware regs.
204 This is set up as a result of register allocation.
205 Element N is the hard reg assigned to pseudo reg N,
206 or is -1 if no hard reg was assigned.
207 If N is a hard reg number, element N is N. */
209 short *reg_renumber;
211 /* Set of hard registers live at the current point in the scan
212 of the instructions in a basic block. */
214 static HARD_REG_SET regs_live;
216 /* Each set of hard registers indicates registers live at a particular
217 point in the basic block. For N even, regs_live_at[N] says which
218 hard registers are needed *after* insn N/2 (i.e., they may not
219 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
221 If an object is to conflict with the inputs of insn J but not the
222 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
223 if it is to conflict with the outputs of insn J but not the inputs of
224 insn J + 1, it is said to die at index J*2 + 1. */
226 static HARD_REG_SET *regs_live_at;
228 int *scratch_block;
229 rtx *scratch_list;
230 int scratch_list_length;
231 static int scratch_index;
233 /* Communicate local vars `insn_number' and `insn'
234 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
235 static int this_insn_number;
236 static rtx this_insn;
238 /* Used to communicate changes made by update_equiv_regs to
239 memref_referenced_p. */
240 static rtx *reg_equiv_replacement;
242 static void alloc_qty PROTO((int, enum machine_mode, int, int));
243 static void alloc_qty_for_scratch PROTO((rtx, int, rtx, int, int));
244 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
245 static int validate_equiv_mem PROTO((rtx, rtx, rtx));
246 static int memref_referenced_p PROTO((rtx, rtx));
247 static int memref_used_between_p PROTO((rtx, rtx, rtx));
248 static void optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
249 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
250 static void update_equiv_regs PROTO((void));
251 static void block_alloc PROTO((int));
252 static int qty_sugg_compare PROTO((int, int));
253 static int qty_sugg_compare_1 PROTO((int *, int *));
254 static int qty_compare PROTO((int, int));
255 static int qty_compare_1 PROTO((int *, int *));
256 static int combine_regs PROTO((rtx, rtx, int, int, rtx, int));
257 static int reg_meets_class_p PROTO((int, enum reg_class));
258 static int reg_classes_overlap_p PROTO((enum reg_class, enum reg_class,
259 int));
260 static void update_qty_class PROTO((int, int));
261 static void reg_is_set PROTO((rtx, rtx));
262 static void reg_is_born PROTO((rtx, int));
263 static void wipe_dead_reg PROTO((rtx, int));
264 static int find_free_reg PROTO((enum reg_class, enum machine_mode,
265 int, int, int, int, int));
266 static void mark_life PROTO((int, enum machine_mode, int));
267 static void post_mark_life PROTO((int, enum machine_mode, int, int, int));
268 static int no_conflict_p PROTO((rtx, rtx, rtx));
269 static int requires_inout PROTO((char *));
271 /* Allocate a new quantity (new within current basic block)
272 for register number REGNO which is born at index BIRTH
273 within the block. MODE and SIZE are info on reg REGNO. */
275 static void
276 alloc_qty (regno, mode, size, birth)
277 int regno;
278 enum machine_mode mode;
279 int size, birth;
281 register int qty = next_qty++;
283 reg_qty[regno] = qty;
284 reg_offset[regno] = 0;
285 reg_next_in_qty[regno] = -1;
287 qty_first_reg[qty] = regno;
288 qty_size[qty] = size;
289 qty_mode[qty] = mode;
290 qty_birth[qty] = birth;
291 qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
292 qty_min_class[qty] = reg_preferred_class (regno);
293 qty_alternate_class[qty] = reg_alternate_class (regno);
294 qty_n_refs[qty] = reg_n_refs[regno];
295 qty_changes_size[qty] = reg_changes_size[regno];
298 /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
299 used as operand N in INSN. We assume here that the SCRATCH is used in
300 a CLOBBER. */
302 static void
303 alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
304 rtx scratch;
305 int n;
306 rtx insn;
307 int insn_code_num, insn_number;
309 register int qty;
310 enum reg_class class;
311 char *p, c;
312 int i;
314 #ifdef REGISTER_CONSTRAINTS
315 /* If we haven't yet computed which alternative will be used, do so now.
316 Then set P to the constraints for that alternative. */
317 if (which_alternative == -1)
318 if (! constrain_operands (insn_code_num, 0))
319 return;
321 for (p = insn_operand_constraint[insn_code_num][n], i = 0;
322 *p && i < which_alternative; p++)
323 if (*p == ',')
324 i++;
326 /* Compute the class required for this SCRATCH. If we don't need a
327 register, the class will remain NO_REGS. If we guessed the alternative
328 number incorrectly, reload will fix things up for us. */
330 class = NO_REGS;
331 while ((c = *p++) != '\0' && c != ',')
332 switch (c)
334 case '=': case '+': case '?':
335 case '#': case '&': case '!':
336 case '*': case '%':
337 case '0': case '1': case '2': case '3': case '4':
338 case 'm': case '<': case '>': case 'V': case 'o':
339 case 'E': case 'F': case 'G': case 'H':
340 case 's': case 'i': case 'n':
341 case 'I': case 'J': case 'K': case 'L':
342 case 'M': case 'N': case 'O': case 'P':
343 #ifdef EXTRA_CONSTRAINT
344 case 'Q': case 'R': case 'S': case 'T': case 'U':
345 #endif
346 case 'p':
347 /* These don't say anything we care about. */
348 break;
350 case 'X':
351 /* We don't need to allocate this SCRATCH. */
352 return;
354 case 'g': case 'r':
355 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
356 break;
358 default:
359 class
360 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
361 break;
364 if (class == NO_REGS)
365 return;
367 #else /* REGISTER_CONSTRAINTS */
369 class = GENERAL_REGS;
370 #endif
373 qty = next_qty++;
375 qty_first_reg[qty] = -1;
376 qty_scratch_rtx[qty] = scratch;
377 qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
378 qty_mode[qty] = GET_MODE (scratch);
379 qty_birth[qty] = 2 * insn_number - 1;
380 qty_death[qty] = 2 * insn_number + 1;
381 qty_n_calls_crossed[qty] = 0;
382 qty_min_class[qty] = class;
383 qty_alternate_class[qty] = NO_REGS;
384 qty_n_refs[qty] = 1;
385 qty_changes_size[qty] = 0;
388 /* Main entry point of this file. */
390 void
391 local_alloc ()
393 register int b, i;
394 int max_qty;
396 /* Leaf functions and non-leaf functions have different needs.
397 If defined, let the machine say what kind of ordering we
398 should use. */
399 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
400 ORDER_REGS_FOR_LOCAL_ALLOC;
401 #endif
403 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
404 registers. */
405 update_equiv_regs ();
407 /* This sets the maximum number of quantities we can have. Quantity
408 numbers start at zero and we can have one for each pseudo plus the
409 number of SCRATCHes in the largest block, in the worst case. */
410 max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
412 /* Allocate vectors of temporary data.
413 See the declarations of these variables, above,
414 for what they mean. */
416 /* There can be up to MAX_SCRATCH * N_BASIC_BLOCKS SCRATCHes to allocate.
417 Instead of allocating this much memory from now until the end of
418 reload, only allocate space for MAX_QTY SCRATCHes. If there are more
419 reload will allocate them. */
421 scratch_list_length = max_qty;
422 scratch_list = (rtx *) xmalloc (scratch_list_length * sizeof (rtx));
423 bzero ((char *) scratch_list, scratch_list_length * sizeof (rtx));
424 scratch_block = (int *) xmalloc (scratch_list_length * sizeof (int));
425 bzero ((char *) scratch_block, scratch_list_length * sizeof (int));
426 scratch_index = 0;
428 qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
429 qty_phys_copy_sugg
430 = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
431 qty_phys_num_copy_sugg = (short *) alloca (max_qty * sizeof (short));
432 qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
433 qty_phys_num_sugg = (short *) alloca (max_qty * sizeof (short));
434 qty_birth = (int *) alloca (max_qty * sizeof (int));
435 qty_death = (int *) alloca (max_qty * sizeof (int));
436 qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
437 qty_first_reg = (int *) alloca (max_qty * sizeof (int));
438 qty_size = (int *) alloca (max_qty * sizeof (int));
439 qty_mode
440 = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
441 qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
442 qty_min_class
443 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
444 qty_alternate_class
445 = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
446 qty_n_refs = (int *) alloca (max_qty * sizeof (int));
447 qty_changes_size = (char *) alloca (max_qty * sizeof (char));
449 reg_qty = (int *) alloca (max_regno * sizeof (int));
450 reg_offset = (char *) alloca (max_regno * sizeof (char));
451 reg_next_in_qty = (int *) alloca (max_regno * sizeof (int));
453 reg_renumber = (short *) oballoc (max_regno * sizeof (short));
454 for (i = 0; i < max_regno; i++)
455 reg_renumber[i] = -1;
457 /* Determine which pseudo-registers can be allocated by local-alloc.
458 In general, these are the registers used only in a single block and
459 which only die once. However, if a register's preferred class has only
460 a few entries, don't allocate this register here unless it is preferred
461 or nothing since retry_global_alloc won't be able to move it to
462 GENERAL_REGS if a reload register of this class is needed.
464 We need not be concerned with which block actually uses the register
465 since we will never see it outside that block. */
467 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
469 if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
470 && (reg_alternate_class (i) == NO_REGS
471 || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
472 reg_qty[i] = -2;
473 else
474 reg_qty[i] = -1;
477 /* Force loop below to initialize entire quantity array. */
478 next_qty = max_qty;
480 /* Allocate each block's local registers, block by block. */
482 for (b = 0; b < n_basic_blocks; b++)
484 /* NEXT_QTY indicates which elements of the `qty_...'
485 vectors might need to be initialized because they were used
486 for the previous block; it is set to the entire array before
487 block 0. Initialize those, with explicit loop if there are few,
488 else with bzero and bcopy. Do not initialize vectors that are
489 explicit set by `alloc_qty'. */
491 if (next_qty < 6)
493 for (i = 0; i < next_qty; i++)
495 qty_scratch_rtx[i] = 0;
496 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
497 qty_phys_num_copy_sugg[i] = 0;
498 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
499 qty_phys_num_sugg[i] = 0;
502 else
504 #define CLEAR(vector) \
505 bzero ((char *) (vector), (sizeof (*(vector))) * next_qty);
507 CLEAR (qty_scratch_rtx);
508 CLEAR (qty_phys_copy_sugg);
509 CLEAR (qty_phys_num_copy_sugg);
510 CLEAR (qty_phys_sugg);
511 CLEAR (qty_phys_num_sugg);
514 next_qty = 0;
516 block_alloc (b);
517 #ifdef USE_C_ALLOCA
518 alloca (0);
519 #endif
523 /* Depth of loops we are in while in update_equiv_regs. */
524 static int loop_depth;
526 /* Used for communication between the following two functions: contains
527 a MEM that we wish to ensure remains unchanged. */
528 static rtx equiv_mem;
530 /* Set nonzero if EQUIV_MEM is modified. */
531 static int equiv_mem_modified;
533 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
534 Called via note_stores. */
536 static void
537 validate_equiv_mem_from_store (dest, set)
538 rtx dest;
539 rtx set;
541 if ((GET_CODE (dest) == REG
542 && reg_overlap_mentioned_p (dest, equiv_mem))
543 || (GET_CODE (dest) == MEM
544 && true_dependence (dest, equiv_mem)))
545 equiv_mem_modified = 1;
548 /* Verify that no store between START and the death of REG invalidates
549 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
550 by storing into an overlapping memory location, or with a non-const
551 CALL_INSN.
553 Return 1 if MEMREF remains valid. */
555 static int
556 validate_equiv_mem (start, reg, memref)
557 rtx start;
558 rtx reg;
559 rtx memref;
561 rtx insn;
562 rtx note;
564 equiv_mem = memref;
565 equiv_mem_modified = 0;
567 /* If the memory reference has side effects or is volatile, it isn't a
568 valid equivalence. */
569 if (side_effects_p (memref))
570 return 0;
572 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
574 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
575 continue;
577 if (find_reg_note (insn, REG_DEAD, reg))
578 return 1;
580 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
581 && ! CONST_CALL_P (insn))
582 return 0;
584 note_stores (PATTERN (insn), validate_equiv_mem_from_store);
586 /* If a register mentioned in MEMREF is modified via an
587 auto-increment, we lose the equivalence. Do the same if one
588 dies; although we could extend the life, it doesn't seem worth
589 the trouble. */
591 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
592 if ((REG_NOTE_KIND (note) == REG_INC
593 || REG_NOTE_KIND (note) == REG_DEAD)
594 && GET_CODE (XEXP (note, 0)) == REG
595 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
596 return 0;
599 return 0;
602 /* TRUE if X references a memory location that would be affected by a store
603 to MEMREF. */
605 static int
606 memref_referenced_p (memref, x)
607 rtx x;
608 rtx memref;
610 int i, j;
611 char *fmt;
612 enum rtx_code code = GET_CODE (x);
614 switch (code)
616 case CONST_INT:
617 case CONST:
618 case LABEL_REF:
619 case SYMBOL_REF:
620 case CONST_DOUBLE:
621 case PC:
622 case CC0:
623 case HIGH:
624 case LO_SUM:
625 return 0;
627 case REG:
628 return (reg_equiv_replacement[REGNO (x)]
629 && memref_referenced_p (memref,
630 reg_equiv_replacement[REGNO (x)]));
632 case MEM:
633 if (true_dependence (memref, x))
634 return 1;
635 break;
637 case SET:
638 /* If we are setting a MEM, it doesn't count (its address does), but any
639 other SET_DEST that has a MEM in it is referencing the MEM. */
640 if (GET_CODE (SET_DEST (x)) == MEM)
642 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
643 return 1;
645 else if (memref_referenced_p (memref, SET_DEST (x)))
646 return 1;
648 return memref_referenced_p (memref, SET_SRC (x));
651 fmt = GET_RTX_FORMAT (code);
652 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
653 switch (fmt[i])
655 case 'e':
656 if (memref_referenced_p (memref, XEXP (x, i)))
657 return 1;
658 break;
659 case 'E':
660 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
661 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
662 return 1;
663 break;
666 return 0;
669 /* TRUE if some insn in the range (START, END] references a memory location
670 that would be affected by a store to MEMREF. */
672 static int
673 memref_used_between_p (memref, start, end)
674 rtx memref;
675 rtx start;
676 rtx end;
678 rtx insn;
680 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
681 insn = NEXT_INSN (insn))
682 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
683 && memref_referenced_p (memref, PATTERN (insn)))
684 return 1;
686 return 0;
689 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
690 in INSN.
692 Search forward to see if SRC dies before either it or DEST is modified,
693 but don't scan past the end of a basic block. If so, we can replace SRC
694 with DEST and let SRC die in INSN.
696 This will reduce the number of registers live in that range and may enable
697 DEST to be tied to SRC, thus often saving one register in addition to a
698 register-register copy. */
700 static void
701 optimize_reg_copy_1 (insn, dest, src)
702 rtx insn;
703 rtx dest;
704 rtx src;
706 rtx p, q;
707 rtx note;
708 rtx dest_death = 0;
709 int sregno = REGNO (src);
710 int dregno = REGNO (dest);
712 if (sregno == dregno
713 #ifdef SMALL_REGISTER_CLASSES
714 /* We don't want to mess with hard regs if register classes are small. */
715 || sregno < FIRST_PSEUDO_REGISTER || dregno < FIRST_PSEUDO_REGISTER
716 #endif
717 /* We don't see all updates to SP if they are in an auto-inc memory
718 reference, so we must disallow this optimization on them. */
719 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
720 return;
722 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
724 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
725 || (GET_CODE (p) == NOTE
726 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
727 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
728 break;
730 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
731 continue;
733 if (reg_set_p (src, p) || reg_set_p (dest, p)
734 /* Don't change a USE of a register. */
735 || (GET_CODE (PATTERN (p)) == USE
736 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
737 break;
739 /* See if all of SRC dies in P. This test is slightly more
740 conservative than it needs to be. */
741 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
742 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
744 int failed = 0;
745 int length = 0;
746 int d_length = 0;
747 int n_calls = 0;
748 int d_n_calls = 0;
750 /* We can do the optimization. Scan forward from INSN again,
751 replacing regs as we go. Set FAILED if a replacement can't
752 be done. In that case, we can't move the death note for SRC.
753 This should be rare. */
755 /* Set to stop at next insn. */
756 for (q = next_real_insn (insn);
757 q != next_real_insn (p);
758 q = next_real_insn (q))
760 if (reg_overlap_mentioned_p (src, PATTERN (q)))
762 /* If SRC is a hard register, we might miss some
763 overlapping registers with validate_replace_rtx,
764 so we would have to undo it. We can't if DEST is
765 present in the insn, so fail in that combination
766 of cases. */
767 if (sregno < FIRST_PSEUDO_REGISTER
768 && reg_mentioned_p (dest, PATTERN (q)))
769 failed = 1;
771 /* Replace all uses and make sure that the register
772 isn't still present. */
773 else if (validate_replace_rtx (src, dest, q)
774 && (sregno >= FIRST_PSEUDO_REGISTER
775 || ! reg_overlap_mentioned_p (src,
776 PATTERN (q))))
778 /* We assume that a register is used exactly once per
779 insn in the updates below. If this is not correct,
780 no great harm is done. */
781 if (sregno >= FIRST_PSEUDO_REGISTER)
782 reg_n_refs[sregno] -= loop_depth;
783 if (dregno >= FIRST_PSEUDO_REGISTER)
784 reg_n_refs[dregno] += loop_depth;
786 else
788 validate_replace_rtx (dest, src, q);
789 failed = 1;
793 /* Count the insns and CALL_INSNs passed. If we passed the
794 death note of DEST, show increased live length. */
795 length++;
796 if (dest_death)
797 d_length++;
799 /* If the insn in which SRC dies is a CALL_INSN, don't count it
800 as a call that has been crossed. Otherwise, count it. */
801 if (q != p && GET_CODE (q) == CALL_INSN)
803 n_calls++;
804 if (dest_death)
805 d_n_calls++;
808 /* If DEST dies here, remove the death note and save it for
809 later. Make sure ALL of DEST dies here; again, this is
810 overly conservative. */
811 if (dest_death == 0
812 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
813 && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
814 remove_note (q, dest_death);
817 if (! failed)
819 if (sregno >= FIRST_PSEUDO_REGISTER)
821 if (reg_live_length[sregno] >= 0)
823 reg_live_length[sregno] -= length;
824 /* reg_live_length is only an approximation after
825 combine if sched is not run, so make sure that we
826 still have a reasonable value. */
827 if (reg_live_length[sregno] < 2)
828 reg_live_length[sregno] = 2;
831 reg_n_calls_crossed[sregno] -= n_calls;
834 if (dregno >= FIRST_PSEUDO_REGISTER)
836 if (reg_live_length[dregno] >= 0)
837 reg_live_length[dregno] += d_length;
839 reg_n_calls_crossed[dregno] += d_n_calls;
842 /* Move death note of SRC from P to INSN. */
843 remove_note (p, note);
844 XEXP (note, 1) = REG_NOTES (insn);
845 REG_NOTES (insn) = note;
848 /* Put death note of DEST on P if we saw it die. */
849 if (dest_death)
851 XEXP (dest_death, 1) = REG_NOTES (p);
852 REG_NOTES (p) = dest_death;
855 return;
858 /* If SRC is a hard register which is set or killed in some other
859 way, we can't do this optimization. */
860 else if (sregno < FIRST_PSEUDO_REGISTER
861 && dead_or_set_p (p, src))
862 break;
866 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
867 a sequence of insns that modify DEST followed by an insn that sets
868 SRC to DEST in which DEST dies, with no prior modification of DEST.
869 (There is no need to check if the insns in between actually modify
870 DEST. We should not have cases where DEST is not modified, but
871 the optimization is safe if no such modification is detected.)
872 In that case, we can replace all uses of DEST, starting with INSN and
873 ending with the set of SRC to DEST, with SRC. We do not do this
874 optimization if a CALL_INSN is crossed unless SRC already crosses a
875 call or if DEST dies before the copy back to SRC.
877 It is assumed that DEST and SRC are pseudos; it is too complicated to do
878 this for hard registers since the substitutions we may make might fail. */
880 static void
881 optimize_reg_copy_2 (insn, dest, src)
882 rtx insn;
883 rtx dest;
884 rtx src;
886 rtx p, q;
887 rtx set;
888 int sregno = REGNO (src);
889 int dregno = REGNO (dest);
891 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
893 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
894 || (GET_CODE (p) == NOTE
895 && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
896 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
897 break;
899 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
900 continue;
902 set = single_set (p);
903 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
904 && find_reg_note (p, REG_DEAD, dest))
906 /* We can do the optimization. Scan forward from INSN again,
907 replacing regs as we go. */
909 /* Set to stop at next insn. */
910 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
911 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
913 if (reg_mentioned_p (dest, PATTERN (q)))
915 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
917 /* We assume that a register is used exactly once per
918 insn in the updates below. If this is not correct,
919 no great harm is done. */
920 reg_n_refs[dregno] -= loop_depth;
921 reg_n_refs[sregno] += loop_depth;
925 if (GET_CODE (q) == CALL_INSN)
927 reg_n_calls_crossed[dregno]--;
928 reg_n_calls_crossed[sregno]++;
932 remove_note (p, find_reg_note (p, REG_DEAD, dest));
933 reg_n_deaths[dregno]--;
934 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
935 reg_n_deaths[sregno]--;
936 return;
939 if (reg_set_p (src, p)
940 || find_reg_note (p, REG_DEAD, dest)
941 || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
942 break;
946 /* Find registers that are equivalent to a single value throughout the
947 compilation (either because they can be referenced in memory or are set once
948 from a single constant). Lower their priority for a register.
950 If such a register is only referenced once, try substituting its value
951 into the using insn. If it succeeds, we can eliminate the register
952 completely. */
954 static void
955 update_equiv_regs ()
957 rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
958 rtx insn;
960 reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
962 bzero ((char *) reg_equiv_init_insn, max_regno * sizeof (rtx *));
963 bzero ((char *) reg_equiv_replacement, max_regno * sizeof (rtx *));
965 init_alias_analysis ();
967 loop_depth = 1;
969 /* Scan the insns and find which registers have equivalences. Do this
970 in a separate scan of the insns because (due to -fcse-follow-jumps)
971 a register can be set below its use. */
972 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
974 rtx note;
975 rtx set = single_set (insn);
976 rtx dest, src;
977 int regno;
979 if (GET_CODE (insn) == NOTE)
981 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
982 loop_depth++;
983 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
984 loop_depth--;
987 /* If this insn contains more (or less) than a single SET, ignore it. */
988 if (set == 0)
989 continue;
991 dest = SET_DEST (set);
992 src = SET_SRC (set);
994 /* If this sets a MEM to the contents of a REG that is only used
995 in a single basic block, see if the register is always equivalent
996 to that memory location and if moving the store from INSN to the
997 insn that set REG is safe. If so, put a REG_EQUIV note on the
998 initializing insn. */
1000 if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
1001 && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
1002 && reg_basic_block[regno] >= 0
1003 && reg_equiv_init_insn[regno] != 0
1004 && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
1005 dest)
1006 && ! memref_used_between_p (SET_DEST (set),
1007 reg_equiv_init_insn[regno], insn))
1008 REG_NOTES (reg_equiv_init_insn[regno])
1009 = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
1010 REG_NOTES (reg_equiv_init_insn[regno]));
1012 /* If this is a register-register copy where SRC is not dead, see if we
1013 can optimize it. */
1014 if (flag_expensive_optimizations && GET_CODE (dest) == REG
1015 && GET_CODE (SET_SRC (set)) == REG
1016 && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1017 optimize_reg_copy_1 (insn, dest, SET_SRC (set));
1019 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1020 else if (flag_expensive_optimizations && GET_CODE (dest) == REG
1021 && REGNO (dest) >= FIRST_PSEUDO_REGISTER
1022 && GET_CODE (SET_SRC (set)) == REG
1023 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
1024 && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1025 optimize_reg_copy_2 (insn, dest, SET_SRC (set));
1027 /* Otherwise, we only handle the case of a pseudo register being set
1028 once and only if neither the source nor the destination are
1029 in a register class that's likely to be spilled. */
1030 if (GET_CODE (dest) != REG
1031 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
1032 || reg_n_sets[regno] != 1
1033 || CLASS_LIKELY_SPILLED_P (reg_preferred_class (REGNO (dest)))
1034 || (GET_CODE (src) == REG
1035 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1036 && CLASS_LIKELY_SPILLED_P (reg_preferred_class (REGNO (src)))))
1037 continue;
1039 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1041 /* Record this insn as initializing this register. */
1042 reg_equiv_init_insn[regno] = insn;
1044 /* If this register is known to be equal to a constant, record that
1045 it is always equivalent to the constant. */
1046 if (note && CONSTANT_P (XEXP (note, 0)))
1047 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
1049 /* If this insn introduces a "constant" register, decrease the priority
1050 of that register. Record this insn if the register is only used once
1051 more and the equivalence value is the same as our source.
1053 The latter condition is checked for two reasons: First, it is an
1054 indication that it may be more efficient to actually emit the insn
1055 as written (if no registers are available, reload will substitute
1056 the equivalence). Secondly, it avoids problems with any registers
1057 dying in this insn whose death notes would be missed.
1059 If we don't have a REG_EQUIV note, see if this insn is loading
1060 a register used only in one basic block from a MEM. If so, and the
1061 MEM remains unchanged for the life of the register, add a REG_EQUIV
1062 note. */
1064 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1066 if (note == 0 && reg_basic_block[regno] >= 0
1067 && GET_CODE (SET_SRC (set)) == MEM
1068 && validate_equiv_mem (insn, dest, SET_SRC (set)))
1069 REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
1070 REG_NOTES (insn));
1072 /* Don't mess with things live during setjmp. */
1073 if (note && reg_live_length[regno] >= 0)
1075 int regno = REGNO (dest);
1077 /* Note that the statement below does not affect the priority
1078 in local-alloc! */
1079 reg_live_length[regno] *= 2;
1081 /* If the register is referenced exactly twice, meaning it is set
1082 once and used once, indicate that the reference may be replaced
1083 by the equivalence we computed above. If the register is only
1084 used in one basic block, this can't succeed or combine would
1085 have done it.
1087 It would be nice to use "loop_depth * 2" in the compare
1088 below. Unfortunately, LOOP_DEPTH need not be constant within
1089 a basic block so this would be too complicated.
1091 This case normally occurs when a parameter is read from memory
1092 and then used exactly once, not in a loop. */
1094 if (reg_n_refs[regno] == 2
1095 && reg_basic_block[regno] < 0
1096 && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
1097 reg_equiv_replacement[regno] = SET_SRC (set);
1101 /* Now scan all regs killed in an insn to see if any of them are registers
1102 only used that once. If so, see if we can replace the reference with
1103 the equivalent from. If we can, delete the initializing reference
1104 and this register will go away. */
1105 for (insn = next_active_insn (get_insns ());
1106 insn;
1107 insn = next_active_insn (insn))
1109 rtx link;
1111 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1112 if (REG_NOTE_KIND (link) == REG_DEAD
1113 /* Make sure this insn still refers to the register. */
1114 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1116 int regno = REGNO (XEXP (link, 0));
1118 if (reg_equiv_replacement[regno]
1119 && validate_replace_rtx (regno_reg_rtx[regno],
1120 reg_equiv_replacement[regno], insn))
1122 rtx equiv_insn = reg_equiv_init_insn[regno];
1124 remove_death (regno, insn);
1125 reg_n_refs[regno] = 0;
1126 PUT_CODE (equiv_insn, NOTE);
1127 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1128 NOTE_SOURCE_FILE (equiv_insn) = 0;
1134 /* Allocate hard regs to the pseudo regs used only within block number B.
1135 Only the pseudos that die but once can be handled. */
1137 static void
1138 block_alloc (b)
1139 int b;
1141 register int i, q;
1142 register rtx insn;
1143 rtx note;
1144 int insn_number = 0;
1145 int insn_count = 0;
1146 int max_uid = get_max_uid ();
1147 int *qty_order;
1148 int no_conflict_combined_regno = -1;
1149 /* Counter to prevent allocating more SCRATCHes than can be stored
1150 in SCRATCH_LIST. */
1151 int scratches_allocated = scratch_index;
1153 /* Count the instructions in the basic block. */
1155 insn = basic_block_end[b];
1156 while (1)
1158 if (GET_CODE (insn) != NOTE)
1159 if (++insn_count > max_uid)
1160 abort ();
1161 if (insn == basic_block_head[b])
1162 break;
1163 insn = PREV_INSN (insn);
1166 /* +2 to leave room for a post_mark_life at the last insn and for
1167 the birth of a CLOBBER in the first insn. */
1168 regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1169 * sizeof (HARD_REG_SET));
1170 bzero ((char *) regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1172 /* Initialize table of hardware registers currently live. */
1174 #ifdef HARD_REG_SET
1175 regs_live = *basic_block_live_at_start[b];
1176 #else
1177 COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1178 #endif
1180 /* This loop scans the instructions of the basic block
1181 and assigns quantities to registers.
1182 It computes which registers to tie. */
1184 insn = basic_block_head[b];
1185 while (1)
1187 register rtx body = PATTERN (insn);
1189 if (GET_CODE (insn) != NOTE)
1190 insn_number++;
1192 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1194 register rtx link, set;
1195 register int win = 0;
1196 register rtx r0, r1;
1197 int combined_regno = -1;
1198 int i;
1199 int insn_code_number = recog_memoized (insn);
1201 this_insn_number = insn_number;
1202 this_insn = insn;
1204 if (insn_code_number >= 0)
1205 insn_extract (insn);
1206 which_alternative = -1;
1208 /* Is this insn suitable for tying two registers?
1209 If so, try doing that.
1210 Suitable insns are those with at least two operands and where
1211 operand 0 is an output that is a register that is not
1212 earlyclobber.
1214 We can tie operand 0 with some operand that dies in this insn.
1215 First look for operands that are required to be in the same
1216 register as operand 0. If we find such, only try tying that
1217 operand or one that can be put into that operand if the
1218 operation is commutative. If we don't find an operand
1219 that is required to be in the same register as operand 0,
1220 we can tie with any operand.
1222 Subregs in place of regs are also ok.
1224 If tying is done, WIN is set nonzero. */
1226 if (insn_code_number >= 0
1227 #ifdef REGISTER_CONSTRAINTS
1228 && insn_n_operands[insn_code_number] > 1
1229 && insn_operand_constraint[insn_code_number][0][0] == '='
1230 && insn_operand_constraint[insn_code_number][0][1] != '&'
1231 #else
1232 && GET_CODE (PATTERN (insn)) == SET
1233 && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1234 #endif
1237 #ifdef REGISTER_CONSTRAINTS
1238 /* If non-negative, is an operand that must match operand 0. */
1239 int must_match_0 = -1;
1240 /* Counts number of alternatives that require a match with
1241 operand 0. */
1242 int n_matching_alts = 0;
1244 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1246 char *p = insn_operand_constraint[insn_code_number][i];
1247 int this_match = (requires_inout (p));
1249 n_matching_alts += this_match;
1250 if (this_match == insn_n_alternatives[insn_code_number])
1251 must_match_0 = i;
1253 #endif
1255 r0 = recog_operand[0];
1256 for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1258 #ifdef REGISTER_CONSTRAINTS
1259 /* Skip this operand if we found an operand that
1260 must match operand 0 and this operand isn't it
1261 and can't be made to be it by commutativity. */
1263 if (must_match_0 >= 0 && i != must_match_0
1264 && ! (i == must_match_0 + 1
1265 && insn_operand_constraint[insn_code_number][i-1][0] == '%')
1266 && ! (i == must_match_0 - 1
1267 && insn_operand_constraint[insn_code_number][i][0] == '%'))
1268 continue;
1270 /* Likewise if each alternative has some operand that
1271 must match operand zero. In that case, skip any
1272 operand that doesn't list operand 0 since we know that
1273 the operand always conflicts with operand 0. We
1274 ignore commutatity in this case to keep things simple. */
1275 if (n_matching_alts == insn_n_alternatives[insn_code_number]
1276 && (0 == requires_inout
1277 (insn_operand_constraint[insn_code_number][i])))
1278 continue;
1279 #endif
1281 r1 = recog_operand[i];
1283 /* If the operand is an address, find a register in it.
1284 There may be more than one register, but we only try one
1285 of them. */
1286 if (
1287 #ifdef REGISTER_CONSTRAINTS
1288 insn_operand_constraint[insn_code_number][i][0] == 'p'
1289 #else
1290 insn_operand_address_p[insn_code_number][i]
1291 #endif
1293 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1294 r1 = XEXP (r1, 0);
1296 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1298 /* We have two priorities for hard register preferences.
1299 If we have a move insn or an insn whose first input
1300 can only be in the same register as the output, give
1301 priority to an equivalence found from that insn. */
1302 int may_save_copy
1303 = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1304 #ifdef REGISTER_CONSTRAINTS
1305 || (r1 == recog_operand[i] && must_match_0 >= 0)
1306 #endif
1309 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1310 win = combine_regs (r1, r0, may_save_copy,
1311 insn_number, insn, 0);
1313 if (win)
1314 break;
1318 /* Recognize an insn sequence with an ultimate result
1319 which can safely overlap one of the inputs.
1320 The sequence begins with a CLOBBER of its result,
1321 and ends with an insn that copies the result to itself
1322 and has a REG_EQUAL note for an equivalent formula.
1323 That note indicates what the inputs are.
1324 The result and the input can overlap if each insn in
1325 the sequence either doesn't mention the input
1326 or has a REG_NO_CONFLICT note to inhibit the conflict.
1328 We do the combining test at the CLOBBER so that the
1329 destination register won't have had a quantity number
1330 assigned, since that would prevent combining. */
1332 if (GET_CODE (PATTERN (insn)) == CLOBBER
1333 && (r0 = XEXP (PATTERN (insn), 0),
1334 GET_CODE (r0) == REG)
1335 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1336 && XEXP (link, 0) != 0
1337 && GET_CODE (XEXP (link, 0)) == INSN
1338 && (set = single_set (XEXP (link, 0))) != 0
1339 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1340 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1341 NULL_RTX)) != 0)
1343 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1344 /* Check that we have such a sequence. */
1345 && no_conflict_p (insn, r0, r1))
1346 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1347 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1348 && (r1 = XEXP (XEXP (note, 0), 0),
1349 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1350 && no_conflict_p (insn, r0, r1))
1351 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1353 /* Here we care if the operation to be computed is
1354 commutative. */
1355 else if ((GET_CODE (XEXP (note, 0)) == EQ
1356 || GET_CODE (XEXP (note, 0)) == NE
1357 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1358 && (r1 = XEXP (XEXP (note, 0), 1),
1359 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1360 && no_conflict_p (insn, r0, r1))
1361 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1363 /* If we did combine something, show the register number
1364 in question so that we know to ignore its death. */
1365 if (win)
1366 no_conflict_combined_regno = REGNO (r1);
1369 /* If registers were just tied, set COMBINED_REGNO
1370 to the number of the register used in this insn
1371 that was tied to the register set in this insn.
1372 This register's qty should not be "killed". */
1374 if (win)
1376 while (GET_CODE (r1) == SUBREG)
1377 r1 = SUBREG_REG (r1);
1378 combined_regno = REGNO (r1);
1381 /* Mark the death of everything that dies in this instruction,
1382 except for anything that was just combined. */
1384 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1385 if (REG_NOTE_KIND (link) == REG_DEAD
1386 && GET_CODE (XEXP (link, 0)) == REG
1387 && combined_regno != REGNO (XEXP (link, 0))
1388 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1389 || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1390 wipe_dead_reg (XEXP (link, 0), 0);
1392 /* Allocate qty numbers for all registers local to this block
1393 that are born (set) in this instruction.
1394 A pseudo that already has a qty is not changed. */
1396 note_stores (PATTERN (insn), reg_is_set);
1398 /* If anything is set in this insn and then unused, mark it as dying
1399 after this insn, so it will conflict with our outputs. This
1400 can't match with something that combined, and it doesn't matter
1401 if it did. Do this after the calls to reg_is_set since these
1402 die after, not during, the current insn. */
1404 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1405 if (REG_NOTE_KIND (link) == REG_UNUSED
1406 && GET_CODE (XEXP (link, 0)) == REG)
1407 wipe_dead_reg (XEXP (link, 0), 1);
1409 /* Allocate quantities for any SCRATCH operands of this insn. */
1411 if (insn_code_number >= 0)
1412 for (i = 0; i < insn_n_operands[insn_code_number]; i++)
1413 if (GET_CODE (recog_operand[i]) == SCRATCH
1414 && scratches_allocated++ < scratch_list_length)
1415 alloc_qty_for_scratch (recog_operand[i], i, insn,
1416 insn_code_number, insn_number);
1418 /* If this is an insn that has a REG_RETVAL note pointing at a
1419 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1420 block, so clear any register number that combined within it. */
1421 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1422 && GET_CODE (XEXP (note, 0)) == INSN
1423 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1424 no_conflict_combined_regno = -1;
1427 /* Set the registers live after INSN_NUMBER. Note that we never
1428 record the registers live before the block's first insn, since no
1429 pseudos we care about are live before that insn. */
1431 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1432 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1434 if (insn == basic_block_end[b])
1435 break;
1437 insn = NEXT_INSN (insn);
1440 /* Now every register that is local to this basic block
1441 should have been given a quantity, or else -1 meaning ignore it.
1442 Every quantity should have a known birth and death.
1444 Order the qtys so we assign them registers in order of the
1445 number of suggested registers they need so we allocate those with
1446 the most restrictive needs first. */
1448 qty_order = (int *) alloca (next_qty * sizeof (int));
1449 for (i = 0; i < next_qty; i++)
1450 qty_order[i] = i;
1452 #define EXCHANGE(I1, I2) \
1453 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1455 switch (next_qty)
1457 case 3:
1458 /* Make qty_order[2] be the one to allocate last. */
1459 if (qty_sugg_compare (0, 1) > 0)
1460 EXCHANGE (0, 1);
1461 if (qty_sugg_compare (1, 2) > 0)
1462 EXCHANGE (2, 1);
1464 /* ... Fall through ... */
1465 case 2:
1466 /* Put the best one to allocate in qty_order[0]. */
1467 if (qty_sugg_compare (0, 1) > 0)
1468 EXCHANGE (0, 1);
1470 /* ... Fall through ... */
1472 case 1:
1473 case 0:
1474 /* Nothing to do here. */
1475 break;
1477 default:
1478 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1481 /* Try to put each quantity in a suggested physical register, if it has one.
1482 This may cause registers to be allocated that otherwise wouldn't be, but
1483 this seems acceptable in local allocation (unlike global allocation). */
1484 for (i = 0; i < next_qty; i++)
1486 q = qty_order[i];
1487 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1488 qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1489 0, 1, qty_birth[q], qty_death[q]);
1490 else
1491 qty_phys_reg[q] = -1;
1494 /* Order the qtys so we assign them registers in order of
1495 decreasing length of life. Normally call qsort, but if we
1496 have only a very small number of quantities, sort them ourselves. */
1498 for (i = 0; i < next_qty; i++)
1499 qty_order[i] = i;
1501 #define EXCHANGE(I1, I2) \
1502 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1504 switch (next_qty)
1506 case 3:
1507 /* Make qty_order[2] be the one to allocate last. */
1508 if (qty_compare (0, 1) > 0)
1509 EXCHANGE (0, 1);
1510 if (qty_compare (1, 2) > 0)
1511 EXCHANGE (2, 1);
1513 /* ... Fall through ... */
1514 case 2:
1515 /* Put the best one to allocate in qty_order[0]. */
1516 if (qty_compare (0, 1) > 0)
1517 EXCHANGE (0, 1);
1519 /* ... Fall through ... */
1521 case 1:
1522 case 0:
1523 /* Nothing to do here. */
1524 break;
1526 default:
1527 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1530 /* Now for each qty that is not a hardware register,
1531 look for a hardware register to put it in.
1532 First try the register class that is cheapest for this qty,
1533 if there is more than one class. */
1535 for (i = 0; i < next_qty; i++)
1537 q = qty_order[i];
1538 if (qty_phys_reg[q] < 0)
1540 if (N_REG_CLASSES > 1)
1542 qty_phys_reg[q] = find_free_reg (qty_min_class[q],
1543 qty_mode[q], q, 0, 0,
1544 qty_birth[q], qty_death[q]);
1545 if (qty_phys_reg[q] >= 0)
1546 continue;
1549 if (qty_alternate_class[q] != NO_REGS)
1550 qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1551 qty_mode[q], q, 0, 0,
1552 qty_birth[q], qty_death[q]);
1556 /* Now propagate the register assignments
1557 to the pseudo regs belonging to the qtys. */
1559 for (q = 0; q < next_qty; q++)
1560 if (qty_phys_reg[q] >= 0)
1562 for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1563 reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1564 if (qty_scratch_rtx[q])
1566 if (GET_CODE (qty_scratch_rtx[q]) == REG)
1567 abort ();
1568 PUT_CODE (qty_scratch_rtx[q], REG);
1569 REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
1571 scratch_block[scratch_index] = b;
1572 scratch_list[scratch_index++] = qty_scratch_rtx[q];
1574 /* Must clear the USED field, because it will have been set by
1575 copy_rtx_if_shared, but the leaf_register code expects that
1576 it is zero in all REG rtx. copy_rtx_if_shared does not set the
1577 used bit for REGs, but does for SCRATCHes. */
1578 qty_scratch_rtx[q]->used = 0;
1583 /* Compare two quantities' priority for getting real registers.
1584 We give shorter-lived quantities higher priority.
1585 Quantities with more references are also preferred, as are quantities that
1586 require multiple registers. This is the identical prioritization as
1587 done by global-alloc.
1589 We used to give preference to registers with *longer* lives, but using
1590 the same algorithm in both local- and global-alloc can speed up execution
1591 of some programs by as much as a factor of three! */
1593 static int
1594 qty_compare (q1, q2)
1595 int q1, q2;
1597 /* Note that the quotient will never be bigger than
1598 the value of floor_log2 times the maximum number of
1599 times a register can occur in one insn (surely less than 100).
1600 Multiplying this by 10000 can't overflow. */
1601 register int pri1
1602 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1603 / (qty_death[q1] - qty_birth[q1]))
1604 * 10000);
1605 register int pri2
1606 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1607 / (qty_death[q2] - qty_birth[q2]))
1608 * 10000);
1609 return pri2 - pri1;
1612 static int
1613 qty_compare_1 (q1, q2)
1614 int *q1, *q2;
1616 register int tem;
1618 /* Note that the quotient will never be bigger than
1619 the value of floor_log2 times the maximum number of
1620 times a register can occur in one insn (surely less than 100).
1621 Multiplying this by 10000 can't overflow. */
1622 register int pri1
1623 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1624 * qty_size[*q1])
1625 / (qty_death[*q1] - qty_birth[*q1]))
1626 * 10000);
1627 register int pri2
1628 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1629 * qty_size[*q2])
1630 / (qty_death[*q2] - qty_birth[*q2]))
1631 * 10000);
1633 tem = pri2 - pri1;
1634 if (tem != 0) return tem;
1635 /* If qtys are equally good, sort by qty number,
1636 so that the results of qsort leave nothing to chance. */
1637 return *q1 - *q2;
1640 /* Compare two quantities' priority for getting real registers. This version
1641 is called for quantities that have suggested hard registers. First priority
1642 goes to quantities that have copy preferences, then to those that have
1643 normal preferences. Within those groups, quantities with the lower
1644 number of preferences have the highest priority. Of those, we use the same
1645 algorithm as above. */
1647 static int
1648 qty_sugg_compare (q1, q2)
1649 int q1, q2;
1651 register int sugg1 = (qty_phys_num_copy_sugg[q1]
1652 ? qty_phys_num_copy_sugg[q1]
1653 : qty_phys_num_sugg[q1] * FIRST_PSEUDO_REGISTER);
1654 register int sugg2 = (qty_phys_num_copy_sugg[q2]
1655 ? qty_phys_num_copy_sugg[q2]
1656 : qty_phys_num_sugg[q2] * FIRST_PSEUDO_REGISTER);
1657 /* Note that the quotient will never be bigger than
1658 the value of floor_log2 times the maximum number of
1659 times a register can occur in one insn (surely less than 100).
1660 Multiplying this by 10000 can't overflow. */
1661 register int pri1
1662 = (((double) (floor_log2 (qty_n_refs[q1]) * qty_n_refs[q1] * qty_size[q1])
1663 / (qty_death[q1] - qty_birth[q1]))
1664 * 10000);
1665 register int pri2
1666 = (((double) (floor_log2 (qty_n_refs[q2]) * qty_n_refs[q2] * qty_size[q2])
1667 / (qty_death[q2] - qty_birth[q2]))
1668 * 10000);
1670 if (sugg1 != sugg2)
1671 return sugg1 - sugg2;
1673 return pri2 - pri1;
1676 static int
1677 qty_sugg_compare_1 (q1, q2)
1678 int *q1, *q2;
1680 register int sugg1 = (qty_phys_num_copy_sugg[*q1]
1681 ? qty_phys_num_copy_sugg[*q1]
1682 : qty_phys_num_sugg[*q1] * FIRST_PSEUDO_REGISTER);
1683 register int sugg2 = (qty_phys_num_copy_sugg[*q2]
1684 ? qty_phys_num_copy_sugg[*q2]
1685 : qty_phys_num_sugg[*q2] * FIRST_PSEUDO_REGISTER);
1687 /* Note that the quotient will never be bigger than
1688 the value of floor_log2 times the maximum number of
1689 times a register can occur in one insn (surely less than 100).
1690 Multiplying this by 10000 can't overflow. */
1691 register int pri1
1692 = (((double) (floor_log2 (qty_n_refs[*q1]) * qty_n_refs[*q1]
1693 * qty_size[*q1])
1694 / (qty_death[*q1] - qty_birth[*q1]))
1695 * 10000);
1696 register int pri2
1697 = (((double) (floor_log2 (qty_n_refs[*q2]) * qty_n_refs[*q2]
1698 * qty_size[*q2])
1699 / (qty_death[*q2] - qty_birth[*q2]))
1700 * 10000);
1702 if (sugg1 != sugg2)
1703 return sugg1 - sugg2;
1705 if (pri1 != pri2)
1706 return pri2 - pri1;
1708 /* If qtys are equally good, sort by qty number,
1709 so that the results of qsort leave nothing to chance. */
1710 return *q1 - *q2;
1713 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1714 Returns 1 if have done so, or 0 if cannot.
1716 Combining registers means marking them as having the same quantity
1717 and adjusting the offsets within the quantity if either of
1718 them is a SUBREG).
1720 We don't actually combine a hard reg with a pseudo; instead
1721 we just record the hard reg as the suggestion for the pseudo's quantity.
1722 If we really combined them, we could lose if the pseudo lives
1723 across an insn that clobbers the hard reg (eg, movstr).
1725 ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1726 there is no REG_DEAD note on INSN. This occurs during the processing
1727 of REG_NO_CONFLICT blocks.
1729 MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1730 SETREG or if the input and output must share a register.
1731 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1733 There are elaborate checks for the validity of combining. */
1736 static int
1737 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1738 rtx usedreg, setreg;
1739 int may_save_copy;
1740 int insn_number;
1741 rtx insn;
1742 int already_dead;
1744 register int ureg, sreg;
1745 register int offset = 0;
1746 int usize, ssize;
1747 register int sqty;
1749 /* Determine the numbers and sizes of registers being used. If a subreg
1750 is present that does not change the entire register, don't consider
1751 this a copy insn. */
1753 while (GET_CODE (usedreg) == SUBREG)
1755 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1756 may_save_copy = 0;
1757 offset += SUBREG_WORD (usedreg);
1758 usedreg = SUBREG_REG (usedreg);
1760 if (GET_CODE (usedreg) != REG)
1761 return 0;
1762 ureg = REGNO (usedreg);
1763 usize = REG_SIZE (usedreg);
1765 while (GET_CODE (setreg) == SUBREG)
1767 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1768 may_save_copy = 0;
1769 offset -= SUBREG_WORD (setreg);
1770 setreg = SUBREG_REG (setreg);
1772 if (GET_CODE (setreg) != REG)
1773 return 0;
1774 sreg = REGNO (setreg);
1775 ssize = REG_SIZE (setreg);
1777 /* If UREG is a pseudo-register that hasn't already been assigned a
1778 quantity number, it means that it is not local to this block or dies
1779 more than once. In either event, we can't do anything with it. */
1780 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1781 /* Do not combine registers unless one fits within the other. */
1782 || (offset > 0 && usize + offset > ssize)
1783 || (offset < 0 && usize + offset < ssize)
1784 /* Do not combine with a smaller already-assigned object
1785 if that smaller object is already combined with something bigger. */
1786 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1787 && usize < qty_size[reg_qty[ureg]])
1788 /* Can't combine if SREG is not a register we can allocate. */
1789 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1790 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1791 These have already been taken care of. This probably wouldn't
1792 combine anyway, but don't take any chances. */
1793 || (ureg >= FIRST_PSEUDO_REGISTER
1794 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1795 /* Don't tie something to itself. In most cases it would make no
1796 difference, but it would screw up if the reg being tied to itself
1797 also dies in this insn. */
1798 || ureg == sreg
1799 /* Don't try to connect two different hardware registers. */
1800 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1801 /* Don't connect two different machine modes if they have different
1802 implications as to which registers may be used. */
1803 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1804 return 0;
1806 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1807 qty_phys_sugg for the pseudo instead of tying them.
1809 Return "failure" so that the lifespan of UREG is terminated here;
1810 that way the two lifespans will be disjoint and nothing will prevent
1811 the pseudo reg from being given this hard reg. */
1813 if (ureg < FIRST_PSEUDO_REGISTER)
1815 /* Allocate a quantity number so we have a place to put our
1816 suggestions. */
1817 if (reg_qty[sreg] == -2)
1818 reg_is_born (setreg, 2 * insn_number);
1820 if (reg_qty[sreg] >= 0)
1822 if (may_save_copy
1823 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1825 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1826 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1828 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1830 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1831 qty_phys_num_sugg[reg_qty[sreg]]++;
1834 return 0;
1837 /* Similarly for SREG a hard register and UREG a pseudo register. */
1839 if (sreg < FIRST_PSEUDO_REGISTER)
1841 if (may_save_copy
1842 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1844 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1845 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1847 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1849 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1850 qty_phys_num_sugg[reg_qty[ureg]]++;
1852 return 0;
1855 /* At this point we know that SREG and UREG are both pseudos.
1856 Do nothing if SREG already has a quantity or is a register that we
1857 don't allocate. */
1858 if (reg_qty[sreg] >= -1
1859 /* If we are not going to let any regs live across calls,
1860 don't tie a call-crossing reg to a non-call-crossing reg. */
1861 || (current_function_has_nonlocal_label
1862 && ((reg_n_calls_crossed[ureg] > 0)
1863 != (reg_n_calls_crossed[sreg] > 0))))
1864 return 0;
1866 /* We don't already know about SREG, so tie it to UREG
1867 if this is the last use of UREG, provided the classes they want
1868 are compatible. */
1870 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1871 && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1873 /* Add SREG to UREG's quantity. */
1874 sqty = reg_qty[ureg];
1875 reg_qty[sreg] = sqty;
1876 reg_offset[sreg] = reg_offset[ureg] + offset;
1877 reg_next_in_qty[sreg] = qty_first_reg[sqty];
1878 qty_first_reg[sqty] = sreg;
1880 /* If SREG's reg class is smaller, set qty_min_class[SQTY]. */
1881 update_qty_class (sqty, sreg);
1883 /* Update info about quantity SQTY. */
1884 qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
1885 qty_n_refs[sqty] += reg_n_refs[sreg];
1886 if (usize < ssize)
1888 register int i;
1890 for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1891 reg_offset[i] -= offset;
1893 qty_size[sqty] = ssize;
1894 qty_mode[sqty] = GET_MODE (setreg);
1897 else
1898 return 0;
1900 return 1;
1903 /* Return 1 if the preferred class of REG allows it to be tied
1904 to a quantity or register whose class is CLASS.
1905 True if REG's reg class either contains or is contained in CLASS. */
1907 static int
1908 reg_meets_class_p (reg, class)
1909 int reg;
1910 enum reg_class class;
1912 register enum reg_class rclass = reg_preferred_class (reg);
1913 return (reg_class_subset_p (rclass, class)
1914 || reg_class_subset_p (class, rclass));
1917 /* Return 1 if the two specified classes have registers in common.
1918 If CALL_SAVED, then consider only call-saved registers. */
1920 static int
1921 reg_classes_overlap_p (c1, c2, call_saved)
1922 register enum reg_class c1;
1923 register enum reg_class c2;
1924 int call_saved;
1926 HARD_REG_SET c;
1927 int i;
1929 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1930 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1932 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1933 if (TEST_HARD_REG_BIT (c, i)
1934 && (! call_saved || ! call_used_regs[i]))
1935 return 1;
1937 return 0;
1940 /* Update the class of QTY assuming that REG is being tied to it. */
1942 static void
1943 update_qty_class (qty, reg)
1944 int qty;
1945 int reg;
1947 enum reg_class rclass = reg_preferred_class (reg);
1948 if (reg_class_subset_p (rclass, qty_min_class[qty]))
1949 qty_min_class[qty] = rclass;
1951 rclass = reg_alternate_class (reg);
1952 if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1953 qty_alternate_class[qty] = rclass;
1955 if (reg_changes_size[reg])
1956 qty_changes_size[qty] = 1;
1959 /* Handle something which alters the value of an rtx REG.
1961 REG is whatever is set or clobbered. SETTER is the rtx that
1962 is modifying the register.
1964 If it is not really a register, we do nothing.
1965 The file-global variables `this_insn' and `this_insn_number'
1966 carry info from `block_alloc'. */
1968 static void
1969 reg_is_set (reg, setter)
1970 rtx reg;
1971 rtx setter;
1973 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1974 a hard register. These may actually not exist any more. */
1976 if (GET_CODE (reg) != SUBREG
1977 && GET_CODE (reg) != REG)
1978 return;
1980 /* Mark this register as being born. If it is used in a CLOBBER, mark
1981 it as being born halfway between the previous insn and this insn so that
1982 it conflicts with our inputs but not the outputs of the previous insn. */
1984 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1987 /* Handle beginning of the life of register REG.
1988 BIRTH is the index at which this is happening. */
1990 static void
1991 reg_is_born (reg, birth)
1992 rtx reg;
1993 int birth;
1995 register int regno;
1997 if (GET_CODE (reg) == SUBREG)
1998 regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1999 else
2000 regno = REGNO (reg);
2002 if (regno < FIRST_PSEUDO_REGISTER)
2004 mark_life (regno, GET_MODE (reg), 1);
2006 /* If the register was to have been born earlier that the present
2007 insn, mark it as live where it is actually born. */
2008 if (birth < 2 * this_insn_number)
2009 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
2011 else
2013 if (reg_qty[regno] == -2)
2014 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
2016 /* If this register has a quantity number, show that it isn't dead. */
2017 if (reg_qty[regno] >= 0)
2018 qty_death[reg_qty[regno]] = -1;
2022 /* Record the death of REG in the current insn. If OUTPUT_P is non-zero,
2023 REG is an output that is dying (i.e., it is never used), otherwise it
2024 is an input (the normal case).
2025 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
2027 static void
2028 wipe_dead_reg (reg, output_p)
2029 register rtx reg;
2030 int output_p;
2032 register int regno = REGNO (reg);
2034 /* If this insn has multiple results,
2035 and the dead reg is used in one of the results,
2036 extend its life to after this insn,
2037 so it won't get allocated together with any other result of this insn. */
2038 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2039 && !single_set (this_insn))
2041 int i;
2042 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2044 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2045 if (GET_CODE (set) == SET
2046 && GET_CODE (SET_DEST (set)) != REG
2047 && !rtx_equal_p (reg, SET_DEST (set))
2048 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2049 output_p = 1;
2053 /* If this register is used in an auto-increment address, then extend its
2054 life to after this insn, so that it won't get allocated together with
2055 the result of this insn. */
2056 if (! output_p && find_regno_note (this_insn, REG_INC, regno))
2057 output_p = 1;
2059 if (regno < FIRST_PSEUDO_REGISTER)
2061 mark_life (regno, GET_MODE (reg), 0);
2063 /* If a hard register is dying as an output, mark it as in use at
2064 the beginning of this insn (the above statement would cause this
2065 not to happen). */
2066 if (output_p)
2067 post_mark_life (regno, GET_MODE (reg), 1,
2068 2 * this_insn_number, 2 * this_insn_number+ 1);
2071 else if (reg_qty[regno] >= 0)
2072 qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
2075 /* Find a block of SIZE words of hard regs in reg_class CLASS
2076 that can hold something of machine-mode MODE
2077 (but actually we test only the first of the block for holding MODE)
2078 and still free between insn BORN_INDEX and insn DEAD_INDEX,
2079 and return the number of the first of them.
2080 Return -1 if such a block cannot be found.
2081 If QTY crosses calls, insist on a register preserved by calls,
2082 unless ACCEPT_CALL_CLOBBERED is nonzero.
2084 If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
2085 register is available. If not, return -1. */
2087 static int
2088 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
2089 born_index, dead_index)
2090 enum reg_class class;
2091 enum machine_mode mode;
2092 int qty;
2093 int accept_call_clobbered;
2094 int just_try_suggested;
2095 int born_index, dead_index;
2097 register int i, ins;
2098 #ifdef HARD_REG_SET
2099 register /* Declare it register if it's a scalar. */
2100 #endif
2101 HARD_REG_SET used, first_used;
2102 #ifdef ELIMINABLE_REGS
2103 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2104 #endif
2106 /* Validate our parameters. */
2107 if (born_index < 0 || born_index > dead_index)
2108 abort ();
2110 /* Don't let a pseudo live in a reg across a function call
2111 if we might get a nonlocal goto. */
2112 if (current_function_has_nonlocal_label
2113 && qty_n_calls_crossed[qty] > 0)
2114 return -1;
2116 if (accept_call_clobbered)
2117 COPY_HARD_REG_SET (used, call_fixed_reg_set);
2118 else if (qty_n_calls_crossed[qty] == 0)
2119 COPY_HARD_REG_SET (used, fixed_reg_set);
2120 else
2121 COPY_HARD_REG_SET (used, call_used_reg_set);
2123 if (accept_call_clobbered)
2124 IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
2126 for (ins = born_index; ins < dead_index; ins++)
2127 IOR_HARD_REG_SET (used, regs_live_at[ins]);
2129 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2131 /* Don't use the frame pointer reg in local-alloc even if
2132 we may omit the frame pointer, because if we do that and then we
2133 need a frame pointer, reload won't know how to move the pseudo
2134 to another hard reg. It can move only regs made by global-alloc.
2136 This is true of any register that can be eliminated. */
2137 #ifdef ELIMINABLE_REGS
2138 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2139 SET_HARD_REG_BIT (used, eliminables[i].from);
2140 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2141 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2142 that it might be eliminated into. */
2143 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2144 #endif
2145 #else
2146 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2147 #endif
2149 #ifdef CLASS_CANNOT_CHANGE_SIZE
2150 if (qty_changes_size[qty])
2151 IOR_HARD_REG_SET (used,
2152 reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
2153 #endif
2155 /* Normally, the registers that can be used for the first register in
2156 a multi-register quantity are the same as those that can be used for
2157 subsequent registers. However, if just trying suggested registers,
2158 restrict our consideration to them. If there are copy-suggested
2159 register, try them. Otherwise, try the arithmetic-suggested
2160 registers. */
2161 COPY_HARD_REG_SET (first_used, used);
2163 if (just_try_suggested)
2165 if (qty_phys_num_copy_sugg[qty] != 0)
2166 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2167 else
2168 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2171 /* If all registers are excluded, we can't do anything. */
2172 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2174 /* If at least one would be suitable, test each hard reg. */
2176 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2178 #ifdef REG_ALLOC_ORDER
2179 int regno = reg_alloc_order[i];
2180 #else
2181 int regno = i;
2182 #endif
2183 if (! TEST_HARD_REG_BIT (first_used, regno)
2184 && HARD_REGNO_MODE_OK (regno, mode))
2186 register int j;
2187 register int size1 = HARD_REGNO_NREGS (regno, mode);
2188 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2189 if (j == size1)
2191 /* Mark that this register is in use between its birth and death
2192 insns. */
2193 post_mark_life (regno, mode, 1, born_index, dead_index);
2194 return regno;
2196 #ifndef REG_ALLOC_ORDER
2197 i += j; /* Skip starting points we know will lose */
2198 #endif
2202 fail:
2204 /* If we are just trying suggested register, we have just tried copy-
2205 suggested registers, and there are arithmetic-suggested registers,
2206 try them. */
2208 /* If it would be profitable to allocate a call-clobbered register
2209 and save and restore it around calls, do that. */
2210 if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2211 && qty_phys_num_sugg[qty] != 0)
2213 /* Don't try the copy-suggested regs again. */
2214 qty_phys_num_copy_sugg[qty] = 0;
2215 return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2216 born_index, dead_index);
2219 /* We need not check to see if the current function has nonlocal
2220 labels because we don't put any pseudos that are live over calls in
2221 registers in that case. */
2223 if (! accept_call_clobbered
2224 && flag_caller_saves
2225 && ! just_try_suggested
2226 && qty_n_calls_crossed[qty] != 0
2227 && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2229 i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2230 if (i >= 0)
2231 caller_save_needed = 1;
2232 return i;
2234 return -1;
2237 /* Mark that REGNO with machine-mode MODE is live starting from the current
2238 insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2239 is zero). */
2241 static void
2242 mark_life (regno, mode, life)
2243 register int regno;
2244 enum machine_mode mode;
2245 int life;
2247 register int j = HARD_REGNO_NREGS (regno, mode);
2248 if (life)
2249 while (--j >= 0)
2250 SET_HARD_REG_BIT (regs_live, regno + j);
2251 else
2252 while (--j >= 0)
2253 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2256 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2257 is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2258 to insn number DEATH (exclusive). */
2260 static void
2261 post_mark_life (regno, mode, life, birth, death)
2262 int regno;
2263 enum machine_mode mode;
2264 int life, birth, death;
2266 register int j = HARD_REGNO_NREGS (regno, mode);
2267 #ifdef HARD_REG_SET
2268 register /* Declare it register if it's a scalar. */
2269 #endif
2270 HARD_REG_SET this_reg;
2272 CLEAR_HARD_REG_SET (this_reg);
2273 while (--j >= 0)
2274 SET_HARD_REG_BIT (this_reg, regno + j);
2276 if (life)
2277 while (birth < death)
2279 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2280 birth++;
2282 else
2283 while (birth < death)
2285 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2286 birth++;
2290 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2291 is the register being clobbered, and R1 is a register being used in
2292 the equivalent expression.
2294 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2295 in which it is used, return 1.
2297 Otherwise, return 0. */
2299 static int
2300 no_conflict_p (insn, r0, r1)
2301 rtx insn, r0, r1;
2303 int ok = 0;
2304 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2305 rtx p, last;
2307 /* If R1 is a hard register, return 0 since we handle this case
2308 when we scan the insns that actually use it. */
2310 if (note == 0
2311 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2312 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2313 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2314 return 0;
2316 last = XEXP (note, 0);
2318 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2319 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2321 if (find_reg_note (p, REG_DEAD, r1))
2322 ok = 1;
2324 if (reg_mentioned_p (r1, PATTERN (p))
2325 && ! find_reg_note (p, REG_NO_CONFLICT, r1))
2326 return 0;
2329 return ok;
2332 #ifdef REGISTER_CONSTRAINTS
2334 /* Return the number of alternatives for which the constraint string P
2335 indicates that the operand must be equal to operand 0 and that no register
2336 is acceptable. */
2338 static int
2339 requires_inout (p)
2340 char *p;
2342 char c;
2343 int found_zero = 0;
2344 int reg_allowed = 0;
2345 int num_matching_alts = 0;
2347 while (c = *p++)
2348 switch (c)
2350 case '=': case '+': case '?':
2351 case '#': case '&': case '!':
2352 case '*': case '%':
2353 case '1': case '2': case '3': case '4':
2354 case 'm': case '<': case '>': case 'V': case 'o':
2355 case 'E': case 'F': case 'G': case 'H':
2356 case 's': case 'i': case 'n':
2357 case 'I': case 'J': case 'K': case 'L':
2358 case 'M': case 'N': case 'O': case 'P':
2359 #ifdef EXTRA_CONSTRAINT
2360 case 'Q': case 'R': case 'S': case 'T': case 'U':
2361 #endif
2362 case 'X':
2363 /* These don't say anything we care about. */
2364 break;
2366 case ',':
2367 if (found_zero && ! reg_allowed)
2368 num_matching_alts++;
2370 found_zero = reg_allowed = 0;
2371 break;
2373 case '0':
2374 found_zero = 1;
2375 break;
2377 case 'p':
2378 case 'g': case 'r':
2379 default:
2380 reg_allowed = 1;
2381 break;
2384 if (found_zero && ! reg_allowed)
2385 num_matching_alts++;
2387 return num_matching_alts;
2389 #endif /* REGISTER_CONSTRAINTS */
2391 void
2392 dump_local_alloc (file)
2393 FILE *file;
2395 register int i;
2396 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2397 if (reg_renumber[i] != -1)
2398 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);