* pa-protos.h (get_deferred_plabel): New prototype.
[official-gcc.git] / gcc / local-alloc.c
blob4ee4991d0cd5b8a6007ca6113c7beee91571466f
1 /* Allocate registers within a basic block, for GNU compiler.
2 Copyright (C) 1987, 1988, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 can be reallocated by global.c if the hard register
59 is used as a spill register. Currently we don't allocate such pseudos
60 here if their preferred class is likely to be used by spills. */
62 #include "config.h"
63 #include "system.h"
64 #include "coretypes.h"
65 #include "tm.h"
66 #include "hard-reg-set.h"
67 #include "rtl.h"
68 #include "tm_p.h"
69 #include "flags.h"
70 #include "regs.h"
71 #include "function.h"
72 #include "insn-config.h"
73 #include "insn-attr.h"
74 #include "recog.h"
75 #include "output.h"
76 #include "toplev.h"
77 #include "except.h"
78 #include "integrate.h"
79 #include "reload.h"
80 #include "ggc.h"
81 #include "timevar.h"
82 #include "tree-pass.h"
84 /* Next quantity number available for allocation. */
86 static int next_qty;
88 /* Information we maintain about each quantity. */
89 struct qty
91 /* The number of refs to quantity Q. */
93 int n_refs;
95 /* The frequency of uses of quantity Q. */
97 int freq;
99 /* Insn number (counting from head of basic block)
100 where quantity Q was born. -1 if birth has not been recorded. */
102 int birth;
104 /* Insn number (counting from head of basic block)
105 where given quantity died. Due to the way tying is done,
106 and the fact that we consider in this pass only regs that die but once,
107 a quantity can die only once. Each quantity's life span
108 is a set of consecutive insns. -1 if death has not been recorded. */
110 int death;
112 /* Number of words needed to hold the data in given quantity.
113 This depends on its machine mode. It is used for these purposes:
114 1. It is used in computing the relative importance of qtys,
115 which determines the order in which we look for regs for them.
116 2. It is used in rules that prevent tying several registers of
117 different sizes in a way that is geometrically impossible
118 (see combine_regs). */
120 int size;
122 /* Number of times a reg tied to given qty lives across a CALL_INSN. */
124 int n_calls_crossed;
126 /* Number of times a reg tied to given qty lives across a CALL_INSN
127 that might throw. */
129 int n_throwing_calls_crossed;
131 /* The register number of one pseudo register whose reg_qty value is Q.
132 This register should be the head of the chain
133 maintained in reg_next_in_qty. */
135 int first_reg;
137 /* Reg class contained in (smaller than) the preferred classes of all
138 the pseudo regs that are tied in given quantity.
139 This is the preferred class for allocating that quantity. */
141 enum reg_class min_class;
143 /* Register class within which we allocate given qty if we can't get
144 its preferred class. */
146 enum reg_class alternate_class;
148 /* This holds the mode of the registers that are tied to given qty,
149 or VOIDmode if registers with differing modes are tied together. */
151 enum machine_mode mode;
153 /* the hard reg number chosen for given quantity,
154 or -1 if none was found. */
156 short phys_reg;
159 static struct qty *qty;
161 /* These fields are kept separately to speedup their clearing. */
163 /* We maintain two hard register sets that indicate suggested hard registers
164 for each quantity. The first, phys_copy_sugg, contains hard registers
165 that are tied to the quantity by a simple copy. The second contains all
166 hard registers that are tied to the quantity via an arithmetic operation.
168 The former register set is given priority for allocation. This tends to
169 eliminate copy insns. */
171 /* Element Q is a set of hard registers that are suggested for quantity Q by
172 copy insns. */
174 static HARD_REG_SET *qty_phys_copy_sugg;
176 /* Element Q is a set of hard registers that are suggested for quantity Q by
177 arithmetic insns. */
179 static HARD_REG_SET *qty_phys_sugg;
181 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
183 static short *qty_phys_num_copy_sugg;
185 /* Element Q is the number of suggested registers in qty_phys_sugg. */
187 static short *qty_phys_num_sugg;
189 /* If (REG N) has been assigned a quantity number, is a register number
190 of another register assigned the same quantity number, or -1 for the
191 end of the chain. qty->first_reg point to the head of this chain. */
193 static int *reg_next_in_qty;
195 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
196 if it is >= 0,
197 of -1 if this register cannot be allocated by local-alloc,
198 or -2 if not known yet.
200 Note that if we see a use or death of pseudo register N with
201 reg_qty[N] == -2, register N must be local to the current block. If
202 it were used in more than one block, we would have reg_qty[N] == -1.
203 This relies on the fact that if reg_basic_block[N] is >= 0, register N
204 will not appear in any other block. We save a considerable number of
205 tests by exploiting this.
207 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
208 be referenced. */
210 static int *reg_qty;
212 /* The offset (in words) of register N within its quantity.
213 This can be nonzero if register N is SImode, and has been tied
214 to a subreg of a DImode register. */
216 static char *reg_offset;
218 /* Vector of substitutions of register numbers,
219 used to map pseudo regs into hardware regs.
220 This is set up as a result of register allocation.
221 Element N is the hard reg assigned to pseudo reg N,
222 or is -1 if no hard reg was assigned.
223 If N is a hard reg number, element N is N. */
225 short *reg_renumber;
227 /* Set of hard registers live at the current point in the scan
228 of the instructions in a basic block. */
230 static HARD_REG_SET regs_live;
232 /* Each set of hard registers indicates registers live at a particular
233 point in the basic block. For N even, regs_live_at[N] says which
234 hard registers are needed *after* insn N/2 (i.e., they may not
235 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
237 If an object is to conflict with the inputs of insn J but not the
238 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
239 if it is to conflict with the outputs of insn J but not the inputs of
240 insn J + 1, it is said to die at index J*2 + 1. */
242 static HARD_REG_SET *regs_live_at;
244 /* Communicate local vars `insn_number' and `insn'
245 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
246 static int this_insn_number;
247 static rtx this_insn;
249 struct equivalence
251 /* Set when an attempt should be made to replace a register
252 with the associated src_p entry. */
254 char replace;
256 /* Set when a REG_EQUIV note is found or created. Use to
257 keep track of what memory accesses might be created later,
258 e.g. by reload. */
260 rtx replacement;
262 rtx *src_p;
264 /* Loop depth is used to recognize equivalences which appear
265 to be present within the same loop (or in an inner loop). */
267 int loop_depth;
269 /* The list of each instruction which initializes this register. */
271 rtx init_insns;
273 /* Nonzero if this had a preexisting REG_EQUIV note. */
275 int is_arg_equivalence;
278 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
279 structure for that register. */
281 static struct equivalence *reg_equiv;
283 /* Nonzero if we recorded an equivalence for a LABEL_REF. */
284 static int recorded_label_ref;
286 static void alloc_qty (int, enum machine_mode, int, int);
287 static void validate_equiv_mem_from_store (rtx, rtx, void *);
288 static int validate_equiv_mem (rtx, rtx, rtx);
289 static int equiv_init_varies_p (rtx);
290 static int equiv_init_movable_p (rtx, int);
291 static int contains_replace_regs (rtx);
292 static int memref_referenced_p (rtx, rtx);
293 static int memref_used_between_p (rtx, rtx, rtx);
294 static void update_equiv_regs (void);
295 static void no_equiv (rtx, rtx, void *);
296 static void block_alloc (int);
297 static int qty_sugg_compare (int, int);
298 static int qty_sugg_compare_1 (const void *, const void *);
299 static int qty_compare (int, int);
300 static int qty_compare_1 (const void *, const void *);
301 static int combine_regs (rtx, rtx, int, int, rtx, int);
302 static int reg_meets_class_p (int, enum reg_class);
303 static void update_qty_class (int, int);
304 static void reg_is_set (rtx, rtx, void *);
305 static void reg_is_born (rtx, int);
306 static void wipe_dead_reg (rtx, int);
307 static int find_free_reg (enum reg_class, enum machine_mode, int, int, int,
308 int, int);
309 static void mark_life (int, enum machine_mode, int);
310 static void post_mark_life (int, enum machine_mode, int, int, int);
311 static int no_conflict_p (rtx, rtx, rtx);
312 static int requires_inout (const char *);
314 /* Allocate a new quantity (new within current basic block)
315 for register number REGNO which is born at index BIRTH
316 within the block. MODE and SIZE are info on reg REGNO. */
318 static void
319 alloc_qty (int regno, enum machine_mode mode, int size, int birth)
321 int qtyno = next_qty++;
323 reg_qty[regno] = qtyno;
324 reg_offset[regno] = 0;
325 reg_next_in_qty[regno] = -1;
327 qty[qtyno].first_reg = regno;
328 qty[qtyno].size = size;
329 qty[qtyno].mode = mode;
330 qty[qtyno].birth = birth;
331 qty[qtyno].n_calls_crossed = REG_N_CALLS_CROSSED (regno);
332 qty[qtyno].n_throwing_calls_crossed = REG_N_THROWING_CALLS_CROSSED (regno);
333 qty[qtyno].min_class = reg_preferred_class (regno);
334 qty[qtyno].alternate_class = reg_alternate_class (regno);
335 qty[qtyno].n_refs = REG_N_REFS (regno);
336 qty[qtyno].freq = REG_FREQ (regno);
339 /* Main entry point of this file. */
342 local_alloc (void)
344 int i;
345 int max_qty;
346 basic_block b;
348 /* We need to keep track of whether or not we recorded a LABEL_REF so
349 that we know if the jump optimizer needs to be rerun. */
350 recorded_label_ref = 0;
352 /* Leaf functions and non-leaf functions have different needs.
353 If defined, let the machine say what kind of ordering we
354 should use. */
355 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
356 ORDER_REGS_FOR_LOCAL_ALLOC;
357 #endif
359 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
360 registers. */
361 update_equiv_regs ();
363 /* This sets the maximum number of quantities we can have. Quantity
364 numbers start at zero and we can have one for each pseudo. */
365 max_qty = (max_regno - FIRST_PSEUDO_REGISTER);
367 /* Allocate vectors of temporary data.
368 See the declarations of these variables, above,
369 for what they mean. */
371 qty = xmalloc (max_qty * sizeof (struct qty));
372 qty_phys_copy_sugg = xmalloc (max_qty * sizeof (HARD_REG_SET));
373 qty_phys_num_copy_sugg = xmalloc (max_qty * sizeof (short));
374 qty_phys_sugg = xmalloc (max_qty * sizeof (HARD_REG_SET));
375 qty_phys_num_sugg = xmalloc (max_qty * sizeof (short));
377 reg_qty = xmalloc (max_regno * sizeof (int));
378 reg_offset = xmalloc (max_regno * sizeof (char));
379 reg_next_in_qty = xmalloc (max_regno * sizeof (int));
381 /* Determine which pseudo-registers can be allocated by local-alloc.
382 In general, these are the registers used only in a single block and
383 which only die once.
385 We need not be concerned with which block actually uses the register
386 since we will never see it outside that block. */
388 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
390 if (REG_BASIC_BLOCK (i) >= 0 && REG_N_DEATHS (i) == 1)
391 reg_qty[i] = -2;
392 else
393 reg_qty[i] = -1;
396 /* Force loop below to initialize entire quantity array. */
397 next_qty = max_qty;
399 /* Allocate each block's local registers, block by block. */
401 FOR_EACH_BB (b)
403 /* NEXT_QTY indicates which elements of the `qty_...'
404 vectors might need to be initialized because they were used
405 for the previous block; it is set to the entire array before
406 block 0. Initialize those, with explicit loop if there are few,
407 else with bzero and bcopy. Do not initialize vectors that are
408 explicit set by `alloc_qty'. */
410 if (next_qty < 6)
412 for (i = 0; i < next_qty; i++)
414 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
415 qty_phys_num_copy_sugg[i] = 0;
416 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
417 qty_phys_num_sugg[i] = 0;
420 else
422 #define CLEAR(vector) \
423 memset ((vector), 0, (sizeof (*(vector))) * next_qty);
425 CLEAR (qty_phys_copy_sugg);
426 CLEAR (qty_phys_num_copy_sugg);
427 CLEAR (qty_phys_sugg);
428 CLEAR (qty_phys_num_sugg);
431 next_qty = 0;
433 block_alloc (b->index);
436 free (qty);
437 free (qty_phys_copy_sugg);
438 free (qty_phys_num_copy_sugg);
439 free (qty_phys_sugg);
440 free (qty_phys_num_sugg);
442 free (reg_qty);
443 free (reg_offset);
444 free (reg_next_in_qty);
446 return recorded_label_ref;
449 /* Used for communication between the following two functions: contains
450 a MEM that we wish to ensure remains unchanged. */
451 static rtx equiv_mem;
453 /* Set nonzero if EQUIV_MEM is modified. */
454 static int equiv_mem_modified;
456 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
457 Called via note_stores. */
459 static void
460 validate_equiv_mem_from_store (rtx dest, rtx set ATTRIBUTE_UNUSED,
461 void *data ATTRIBUTE_UNUSED)
463 if ((REG_P (dest)
464 && reg_overlap_mentioned_p (dest, equiv_mem))
465 || (MEM_P (dest)
466 && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
467 equiv_mem_modified = 1;
470 /* Verify that no store between START and the death of REG invalidates
471 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
472 by storing into an overlapping memory location, or with a non-const
473 CALL_INSN.
475 Return 1 if MEMREF remains valid. */
477 static int
478 validate_equiv_mem (rtx start, rtx reg, rtx memref)
480 rtx insn;
481 rtx note;
483 equiv_mem = memref;
484 equiv_mem_modified = 0;
486 /* If the memory reference has side effects or is volatile, it isn't a
487 valid equivalence. */
488 if (side_effects_p (memref))
489 return 0;
491 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
493 if (! INSN_P (insn))
494 continue;
496 if (find_reg_note (insn, REG_DEAD, reg))
497 return 1;
499 if (CALL_P (insn) && ! MEM_READONLY_P (memref)
500 && ! CONST_OR_PURE_CALL_P (insn))
501 return 0;
503 note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
505 /* If a register mentioned in MEMREF is modified via an
506 auto-increment, we lose the equivalence. Do the same if one
507 dies; although we could extend the life, it doesn't seem worth
508 the trouble. */
510 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
511 if ((REG_NOTE_KIND (note) == REG_INC
512 || REG_NOTE_KIND (note) == REG_DEAD)
513 && REG_P (XEXP (note, 0))
514 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
515 return 0;
518 return 0;
521 /* Returns zero if X is known to be invariant. */
523 static int
524 equiv_init_varies_p (rtx x)
526 RTX_CODE code = GET_CODE (x);
527 int i;
528 const char *fmt;
530 switch (code)
532 case MEM:
533 return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
535 case CONST:
536 case CONST_INT:
537 case CONST_DOUBLE:
538 case CONST_VECTOR:
539 case SYMBOL_REF:
540 case LABEL_REF:
541 return 0;
543 case REG:
544 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
546 case ASM_OPERANDS:
547 if (MEM_VOLATILE_P (x))
548 return 1;
550 /* Fall through. */
552 default:
553 break;
556 fmt = GET_RTX_FORMAT (code);
557 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
558 if (fmt[i] == 'e')
560 if (equiv_init_varies_p (XEXP (x, i)))
561 return 1;
563 else if (fmt[i] == 'E')
565 int j;
566 for (j = 0; j < XVECLEN (x, i); j++)
567 if (equiv_init_varies_p (XVECEXP (x, i, j)))
568 return 1;
571 return 0;
574 /* Returns nonzero if X (used to initialize register REGNO) is movable.
575 X is only movable if the registers it uses have equivalent initializations
576 which appear to be within the same loop (or in an inner loop) and movable
577 or if they are not candidates for local_alloc and don't vary. */
579 static int
580 equiv_init_movable_p (rtx x, int regno)
582 int i, j;
583 const char *fmt;
584 enum rtx_code code = GET_CODE (x);
586 switch (code)
588 case SET:
589 return equiv_init_movable_p (SET_SRC (x), regno);
591 case CC0:
592 case CLOBBER:
593 return 0;
595 case PRE_INC:
596 case PRE_DEC:
597 case POST_INC:
598 case POST_DEC:
599 case PRE_MODIFY:
600 case POST_MODIFY:
601 return 0;
603 case REG:
604 return (reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
605 && reg_equiv[REGNO (x)].replace)
606 || (REG_BASIC_BLOCK (REGNO (x)) < 0 && ! rtx_varies_p (x, 0));
608 case UNSPEC_VOLATILE:
609 return 0;
611 case ASM_OPERANDS:
612 if (MEM_VOLATILE_P (x))
613 return 0;
615 /* Fall through. */
617 default:
618 break;
621 fmt = GET_RTX_FORMAT (code);
622 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
623 switch (fmt[i])
625 case 'e':
626 if (! equiv_init_movable_p (XEXP (x, i), regno))
627 return 0;
628 break;
629 case 'E':
630 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
631 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
632 return 0;
633 break;
636 return 1;
639 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is true. */
641 static int
642 contains_replace_regs (rtx x)
644 int i, j;
645 const char *fmt;
646 enum rtx_code code = GET_CODE (x);
648 switch (code)
650 case CONST_INT:
651 case CONST:
652 case LABEL_REF:
653 case SYMBOL_REF:
654 case CONST_DOUBLE:
655 case CONST_VECTOR:
656 case PC:
657 case CC0:
658 case HIGH:
659 return 0;
661 case REG:
662 return reg_equiv[REGNO (x)].replace;
664 default:
665 break;
668 fmt = GET_RTX_FORMAT (code);
669 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
670 switch (fmt[i])
672 case 'e':
673 if (contains_replace_regs (XEXP (x, i)))
674 return 1;
675 break;
676 case 'E':
677 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
678 if (contains_replace_regs (XVECEXP (x, i, j)))
679 return 1;
680 break;
683 return 0;
686 /* TRUE if X references a memory location that would be affected by a store
687 to MEMREF. */
689 static int
690 memref_referenced_p (rtx memref, rtx x)
692 int i, j;
693 const char *fmt;
694 enum rtx_code code = GET_CODE (x);
696 switch (code)
698 case CONST_INT:
699 case CONST:
700 case LABEL_REF:
701 case SYMBOL_REF:
702 case CONST_DOUBLE:
703 case CONST_VECTOR:
704 case PC:
705 case CC0:
706 case HIGH:
707 case LO_SUM:
708 return 0;
710 case REG:
711 return (reg_equiv[REGNO (x)].replacement
712 && memref_referenced_p (memref,
713 reg_equiv[REGNO (x)].replacement));
715 case MEM:
716 if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
717 return 1;
718 break;
720 case SET:
721 /* If we are setting a MEM, it doesn't count (its address does), but any
722 other SET_DEST that has a MEM in it is referencing the MEM. */
723 if (MEM_P (SET_DEST (x)))
725 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
726 return 1;
728 else if (memref_referenced_p (memref, SET_DEST (x)))
729 return 1;
731 return memref_referenced_p (memref, SET_SRC (x));
733 default:
734 break;
737 fmt = GET_RTX_FORMAT (code);
738 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
739 switch (fmt[i])
741 case 'e':
742 if (memref_referenced_p (memref, XEXP (x, i)))
743 return 1;
744 break;
745 case 'E':
746 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
747 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
748 return 1;
749 break;
752 return 0;
755 /* TRUE if some insn in the range (START, END] references a memory location
756 that would be affected by a store to MEMREF. */
758 static int
759 memref_used_between_p (rtx memref, rtx start, rtx end)
761 rtx insn;
763 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
764 insn = NEXT_INSN (insn))
765 if (INSN_P (insn) && memref_referenced_p (memref, PATTERN (insn)))
766 return 1;
768 return 0;
771 /* Find registers that are equivalent to a single value throughout the
772 compilation (either because they can be referenced in memory or are set once
773 from a single constant). Lower their priority for a register.
775 If such a register is only referenced once, try substituting its value
776 into the using insn. If it succeeds, we can eliminate the register
777 completely.
779 Initialize the REG_EQUIV_INIT array of initializing insns. */
781 static void
782 update_equiv_regs (void)
784 rtx insn;
785 basic_block bb;
786 int loop_depth;
787 regset_head cleared_regs;
788 int clear_regnos = 0;
790 reg_equiv = xcalloc (max_regno, sizeof *reg_equiv);
791 INIT_REG_SET (&cleared_regs);
792 reg_equiv_init = ggc_alloc_cleared (max_regno * sizeof (rtx));
793 reg_equiv_init_size = max_regno;
795 init_alias_analysis ();
797 /* Scan the insns and find which registers have equivalences. Do this
798 in a separate scan of the insns because (due to -fcse-follow-jumps)
799 a register can be set below its use. */
800 FOR_EACH_BB (bb)
802 loop_depth = bb->loop_depth;
804 for (insn = BB_HEAD (bb);
805 insn != NEXT_INSN (BB_END (bb));
806 insn = NEXT_INSN (insn))
808 rtx note;
809 rtx set;
810 rtx dest, src;
811 int regno;
813 if (! INSN_P (insn))
814 continue;
816 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
817 if (REG_NOTE_KIND (note) == REG_INC)
818 no_equiv (XEXP (note, 0), note, NULL);
820 set = single_set (insn);
822 /* If this insn contains more (or less) than a single SET,
823 only mark all destinations as having no known equivalence. */
824 if (set == 0)
826 note_stores (PATTERN (insn), no_equiv, NULL);
827 continue;
829 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
831 int i;
833 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
835 rtx part = XVECEXP (PATTERN (insn), 0, i);
836 if (part != set)
837 note_stores (part, no_equiv, NULL);
841 dest = SET_DEST (set);
842 src = SET_SRC (set);
844 /* See if this is setting up the equivalence between an argument
845 register and its stack slot. */
846 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
847 if (note)
849 gcc_assert (REG_P (dest));
850 regno = REGNO (dest);
852 /* Note that we don't want to clear reg_equiv_init even if there
853 are multiple sets of this register. */
854 reg_equiv[regno].is_arg_equivalence = 1;
856 /* Record for reload that this is an equivalencing insn. */
857 if (rtx_equal_p (src, XEXP (note, 0)))
858 reg_equiv_init[regno]
859 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
861 /* Continue normally in case this is a candidate for
862 replacements. */
865 if (!optimize)
866 continue;
868 /* We only handle the case of a pseudo register being set
869 once, or always to the same value. */
870 /* ??? The mn10200 port breaks if we add equivalences for
871 values that need an ADDRESS_REGS register and set them equivalent
872 to a MEM of a pseudo. The actual problem is in the over-conservative
873 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
874 calculate_needs, but we traditionally work around this problem
875 here by rejecting equivalences when the destination is in a register
876 that's likely spilled. This is fragile, of course, since the
877 preferred class of a pseudo depends on all instructions that set
878 or use it. */
880 if (!REG_P (dest)
881 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
882 || reg_equiv[regno].init_insns == const0_rtx
883 || (CLASS_LIKELY_SPILLED_P (reg_preferred_class (regno))
884 && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
886 /* This might be setting a SUBREG of a pseudo, a pseudo that is
887 also set somewhere else to a constant. */
888 note_stores (set, no_equiv, NULL);
889 continue;
892 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
894 /* cse sometimes generates function invariants, but doesn't put a
895 REG_EQUAL note on the insn. Since this note would be redundant,
896 there's no point creating it earlier than here. */
897 if (! note && ! rtx_varies_p (src, 0))
898 note = set_unique_reg_note (insn, REG_EQUAL, src);
900 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
901 since it represents a function call */
902 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
903 note = NULL_RTX;
905 if (REG_N_SETS (regno) != 1
906 && (! note
907 || rtx_varies_p (XEXP (note, 0), 0)
908 || (reg_equiv[regno].replacement
909 && ! rtx_equal_p (XEXP (note, 0),
910 reg_equiv[regno].replacement))))
912 no_equiv (dest, set, NULL);
913 continue;
915 /* Record this insn as initializing this register. */
916 reg_equiv[regno].init_insns
917 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
919 /* If this register is known to be equal to a constant, record that
920 it is always equivalent to the constant. */
921 if (note && ! rtx_varies_p (XEXP (note, 0), 0))
922 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
924 /* If this insn introduces a "constant" register, decrease the priority
925 of that register. Record this insn if the register is only used once
926 more and the equivalence value is the same as our source.
928 The latter condition is checked for two reasons: First, it is an
929 indication that it may be more efficient to actually emit the insn
930 as written (if no registers are available, reload will substitute
931 the equivalence). Secondly, it avoids problems with any registers
932 dying in this insn whose death notes would be missed.
934 If we don't have a REG_EQUIV note, see if this insn is loading
935 a register used only in one basic block from a MEM. If so, and the
936 MEM remains unchanged for the life of the register, add a REG_EQUIV
937 note. */
939 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
941 if (note == 0 && REG_BASIC_BLOCK (regno) >= 0
942 && MEM_P (SET_SRC (set))
943 && validate_equiv_mem (insn, dest, SET_SRC (set)))
944 REG_NOTES (insn) = note = gen_rtx_EXPR_LIST (REG_EQUIV, SET_SRC (set),
945 REG_NOTES (insn));
947 if (note)
949 int regno = REGNO (dest);
950 rtx x = XEXP (note, 0);
952 /* If we haven't done so, record for reload that this is an
953 equivalencing insn. */
954 if (!reg_equiv[regno].is_arg_equivalence
955 && (!MEM_P (x) || rtx_equal_p (src, x)))
956 reg_equiv_init[regno]
957 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
959 /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
960 We might end up substituting the LABEL_REF for uses of the
961 pseudo here or later. That kind of transformation may turn an
962 indirect jump into a direct jump, in which case we must rerun the
963 jump optimizer to ensure that the JUMP_LABEL fields are valid. */
964 if (GET_CODE (x) == LABEL_REF
965 || (GET_CODE (x) == CONST
966 && GET_CODE (XEXP (x, 0)) == PLUS
967 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
968 recorded_label_ref = 1;
970 reg_equiv[regno].replacement = x;
971 reg_equiv[regno].src_p = &SET_SRC (set);
972 reg_equiv[regno].loop_depth = loop_depth;
974 /* Don't mess with things live during setjmp. */
975 if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
977 /* Note that the statement below does not affect the priority
978 in local-alloc! */
979 REG_LIVE_LENGTH (regno) *= 2;
981 /* If the register is referenced exactly twice, meaning it is
982 set once and used once, indicate that the reference may be
983 replaced by the equivalence we computed above. Do this
984 even if the register is only used in one block so that
985 dependencies can be handled where the last register is
986 used in a different block (i.e. HIGH / LO_SUM sequences)
987 and to reduce the number of registers alive across
988 calls. */
990 if (REG_N_REFS (regno) == 2
991 && (rtx_equal_p (x, src)
992 || ! equiv_init_varies_p (src))
993 && NONJUMP_INSN_P (insn)
994 && equiv_init_movable_p (PATTERN (insn), regno))
995 reg_equiv[regno].replace = 1;
1001 if (!optimize)
1002 goto out;
1004 /* A second pass, to gather additional equivalences with memory. This needs
1005 to be done after we know which registers we are going to replace. */
1007 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1009 rtx set, src, dest;
1010 unsigned regno;
1012 if (! INSN_P (insn))
1013 continue;
1015 set = single_set (insn);
1016 if (! set)
1017 continue;
1019 dest = SET_DEST (set);
1020 src = SET_SRC (set);
1022 /* If this sets a MEM to the contents of a REG that is only used
1023 in a single basic block, see if the register is always equivalent
1024 to that memory location and if moving the store from INSN to the
1025 insn that set REG is safe. If so, put a REG_EQUIV note on the
1026 initializing insn.
1028 Don't add a REG_EQUIV note if the insn already has one. The existing
1029 REG_EQUIV is likely more useful than the one we are adding.
1031 If one of the regs in the address has reg_equiv[REGNO].replace set,
1032 then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
1033 optimization may move the set of this register immediately before
1034 insn, which puts it after reg_equiv[REGNO].init_insns, and hence
1035 the mention in the REG_EQUIV note would be to an uninitialized
1036 pseudo. */
1038 if (MEM_P (dest) && REG_P (src)
1039 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
1040 && REG_BASIC_BLOCK (regno) >= 0
1041 && REG_N_SETS (regno) == 1
1042 && reg_equiv[regno].init_insns != 0
1043 && reg_equiv[regno].init_insns != const0_rtx
1044 && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
1045 REG_EQUIV, NULL_RTX)
1046 && ! contains_replace_regs (XEXP (dest, 0)))
1048 rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
1049 if (validate_equiv_mem (init_insn, src, dest)
1050 && ! memref_used_between_p (dest, init_insn, insn))
1052 REG_NOTES (init_insn)
1053 = gen_rtx_EXPR_LIST (REG_EQUIV, dest,
1054 REG_NOTES (init_insn));
1055 /* This insn makes the equivalence, not the one initializing
1056 the register. */
1057 reg_equiv_init[regno]
1058 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
1063 /* Now scan all regs killed in an insn to see if any of them are
1064 registers only used that once. If so, see if we can replace the
1065 reference with the equivalent form. If we can, delete the
1066 initializing reference and this register will go away. If we
1067 can't replace the reference, and the initializing reference is
1068 within the same loop (or in an inner loop), then move the register
1069 initialization just before the use, so that they are in the same
1070 basic block. */
1071 FOR_EACH_BB_REVERSE (bb)
1073 loop_depth = bb->loop_depth;
1074 for (insn = BB_END (bb);
1075 insn != PREV_INSN (BB_HEAD (bb));
1076 insn = PREV_INSN (insn))
1078 rtx link;
1080 if (! INSN_P (insn))
1081 continue;
1083 /* Don't substitute into a non-local goto, this confuses CFG. */
1084 if (JUMP_P (insn)
1085 && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
1086 continue;
1088 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1090 if (REG_NOTE_KIND (link) == REG_DEAD
1091 /* Make sure this insn still refers to the register. */
1092 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1094 int regno = REGNO (XEXP (link, 0));
1095 rtx equiv_insn;
1097 if (! reg_equiv[regno].replace
1098 || reg_equiv[regno].loop_depth < loop_depth)
1099 continue;
1101 /* reg_equiv[REGNO].replace gets set only when
1102 REG_N_REFS[REGNO] is 2, i.e. the register is set
1103 once and used once. (If it were only set, but not used,
1104 flow would have deleted the setting insns.) Hence
1105 there can only be one insn in reg_equiv[REGNO].init_insns. */
1106 gcc_assert (reg_equiv[regno].init_insns
1107 && !XEXP (reg_equiv[regno].init_insns, 1));
1108 equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
1110 /* We may not move instructions that can throw, since
1111 that changes basic block boundaries and we are not
1112 prepared to adjust the CFG to match. */
1113 if (can_throw_internal (equiv_insn))
1114 continue;
1116 if (asm_noperands (PATTERN (equiv_insn)) < 0
1117 && validate_replace_rtx (regno_reg_rtx[regno],
1118 *(reg_equiv[regno].src_p), insn))
1120 rtx equiv_link;
1121 rtx last_link;
1122 rtx note;
1124 /* Find the last note. */
1125 for (last_link = link; XEXP (last_link, 1);
1126 last_link = XEXP (last_link, 1))
1129 /* Append the REG_DEAD notes from equiv_insn. */
1130 equiv_link = REG_NOTES (equiv_insn);
1131 while (equiv_link)
1133 note = equiv_link;
1134 equiv_link = XEXP (equiv_link, 1);
1135 if (REG_NOTE_KIND (note) == REG_DEAD)
1137 remove_note (equiv_insn, note);
1138 XEXP (last_link, 1) = note;
1139 XEXP (note, 1) = NULL_RTX;
1140 last_link = note;
1144 remove_death (regno, insn);
1145 REG_N_REFS (regno) = 0;
1146 REG_FREQ (regno) = 0;
1147 delete_insn (equiv_insn);
1149 reg_equiv[regno].init_insns
1150 = XEXP (reg_equiv[regno].init_insns, 1);
1152 /* Remember to clear REGNO from all basic block's live
1153 info. */
1154 SET_REGNO_REG_SET (&cleared_regs, regno);
1155 clear_regnos++;
1156 reg_equiv_init[regno] = NULL_RTX;
1158 /* Move the initialization of the register to just before
1159 INSN. Update the flow information. */
1160 else if (PREV_INSN (insn) != equiv_insn)
1162 rtx new_insn;
1164 new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
1165 REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
1166 REG_NOTES (equiv_insn) = 0;
1168 /* Make sure this insn is recognized before
1169 reload begins, otherwise
1170 eliminate_regs_in_insn will die. */
1171 INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
1173 delete_insn (equiv_insn);
1175 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
1177 REG_BASIC_BLOCK (regno) = bb->index;
1178 REG_N_CALLS_CROSSED (regno) = 0;
1179 REG_N_THROWING_CALLS_CROSSED (regno) = 0;
1180 REG_LIVE_LENGTH (regno) = 2;
1182 if (insn == BB_HEAD (bb))
1183 BB_HEAD (bb) = PREV_INSN (insn);
1185 /* Remember to clear REGNO from all basic block's live
1186 info. */
1187 SET_REGNO_REG_SET (&cleared_regs, regno);
1188 clear_regnos++;
1189 reg_equiv_init[regno]
1190 = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
1197 /* Clear all dead REGNOs from all basic block's live info. */
1198 if (clear_regnos)
1200 unsigned j;
1202 if (clear_regnos > 8)
1204 FOR_EACH_BB (bb)
1206 AND_COMPL_REG_SET (bb->il.rtl->global_live_at_start,
1207 &cleared_regs);
1208 AND_COMPL_REG_SET (bb->il.rtl->global_live_at_end,
1209 &cleared_regs);
1212 else
1214 reg_set_iterator rsi;
1215 EXECUTE_IF_SET_IN_REG_SET (&cleared_regs, 0, j, rsi)
1217 FOR_EACH_BB (bb)
1219 CLEAR_REGNO_REG_SET (bb->il.rtl->global_live_at_start, j);
1220 CLEAR_REGNO_REG_SET (bb->il.rtl->global_live_at_end, j);
1226 out:
1227 /* Clean up. */
1228 end_alias_analysis ();
1229 CLEAR_REG_SET (&cleared_regs);
1230 free (reg_equiv);
1233 /* Mark REG as having no known equivalence.
1234 Some instructions might have been processed before and furnished
1235 with REG_EQUIV notes for this register; these notes will have to be
1236 removed.
1237 STORE is the piece of RTL that does the non-constant / conflicting
1238 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
1239 but needs to be there because this function is called from note_stores. */
1240 static void
1241 no_equiv (rtx reg, rtx store ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
1243 int regno;
1244 rtx list;
1246 if (!REG_P (reg))
1247 return;
1248 regno = REGNO (reg);
1249 list = reg_equiv[regno].init_insns;
1250 if (list == const0_rtx)
1251 return;
1252 reg_equiv[regno].init_insns = const0_rtx;
1253 reg_equiv[regno].replacement = NULL_RTX;
1254 /* This doesn't matter for equivalences made for argument registers, we
1255 should keep their initialization insns. */
1256 if (reg_equiv[regno].is_arg_equivalence)
1257 return;
1258 reg_equiv_init[regno] = NULL_RTX;
1259 for (; list; list = XEXP (list, 1))
1261 rtx insn = XEXP (list, 0);
1262 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
1266 /* Allocate hard regs to the pseudo regs used only within block number B.
1267 Only the pseudos that die but once can be handled. */
1269 static void
1270 block_alloc (int b)
1272 int i, q;
1273 rtx insn;
1274 rtx note, hard_reg;
1275 int insn_number = 0;
1276 int insn_count = 0;
1277 int max_uid = get_max_uid ();
1278 int *qty_order;
1279 int no_conflict_combined_regno = -1;
1281 /* Count the instructions in the basic block. */
1283 insn = BB_END (BASIC_BLOCK (b));
1284 while (1)
1286 if (!NOTE_P (insn))
1288 ++insn_count;
1289 gcc_assert (insn_count <= max_uid);
1291 if (insn == BB_HEAD (BASIC_BLOCK (b)))
1292 break;
1293 insn = PREV_INSN (insn);
1296 /* +2 to leave room for a post_mark_life at the last insn and for
1297 the birth of a CLOBBER in the first insn. */
1298 regs_live_at = xcalloc ((2 * insn_count + 2), sizeof (HARD_REG_SET));
1300 /* Initialize table of hardware registers currently live. */
1302 REG_SET_TO_HARD_REG_SET (regs_live,
1303 BASIC_BLOCK (b)->il.rtl->global_live_at_start);
1305 /* This loop scans the instructions of the basic block
1306 and assigns quantities to registers.
1307 It computes which registers to tie. */
1309 insn = BB_HEAD (BASIC_BLOCK (b));
1310 while (1)
1312 if (!NOTE_P (insn))
1313 insn_number++;
1315 if (INSN_P (insn))
1317 rtx link, set;
1318 int win = 0;
1319 rtx r0, r1 = NULL_RTX;
1320 int combined_regno = -1;
1321 int i;
1323 this_insn_number = insn_number;
1324 this_insn = insn;
1326 extract_insn (insn);
1327 which_alternative = -1;
1329 /* Is this insn suitable for tying two registers?
1330 If so, try doing that.
1331 Suitable insns are those with at least two operands and where
1332 operand 0 is an output that is a register that is not
1333 earlyclobber.
1335 We can tie operand 0 with some operand that dies in this insn.
1336 First look for operands that are required to be in the same
1337 register as operand 0. If we find such, only try tying that
1338 operand or one that can be put into that operand if the
1339 operation is commutative. If we don't find an operand
1340 that is required to be in the same register as operand 0,
1341 we can tie with any operand.
1343 Subregs in place of regs are also ok.
1345 If tying is done, WIN is set nonzero. */
1347 if (optimize
1348 && recog_data.n_operands > 1
1349 && recog_data.constraints[0][0] == '='
1350 && recog_data.constraints[0][1] != '&')
1352 /* If non-negative, is an operand that must match operand 0. */
1353 int must_match_0 = -1;
1354 /* Counts number of alternatives that require a match with
1355 operand 0. */
1356 int n_matching_alts = 0;
1358 for (i = 1; i < recog_data.n_operands; i++)
1360 const char *p = recog_data.constraints[i];
1361 int this_match = requires_inout (p);
1363 n_matching_alts += this_match;
1364 if (this_match == recog_data.n_alternatives)
1365 must_match_0 = i;
1368 r0 = recog_data.operand[0];
1369 for (i = 1; i < recog_data.n_operands; i++)
1371 /* Skip this operand if we found an operand that
1372 must match operand 0 and this operand isn't it
1373 and can't be made to be it by commutativity. */
1375 if (must_match_0 >= 0 && i != must_match_0
1376 && ! (i == must_match_0 + 1
1377 && recog_data.constraints[i-1][0] == '%')
1378 && ! (i == must_match_0 - 1
1379 && recog_data.constraints[i][0] == '%'))
1380 continue;
1382 /* Likewise if each alternative has some operand that
1383 must match operand zero. In that case, skip any
1384 operand that doesn't list operand 0 since we know that
1385 the operand always conflicts with operand 0. We
1386 ignore commutativity in this case to keep things simple. */
1387 if (n_matching_alts == recog_data.n_alternatives
1388 && 0 == requires_inout (recog_data.constraints[i]))
1389 continue;
1391 r1 = recog_data.operand[i];
1393 /* If the operand is an address, find a register in it.
1394 There may be more than one register, but we only try one
1395 of them. */
1396 if (recog_data.constraints[i][0] == 'p'
1397 || EXTRA_ADDRESS_CONSTRAINT (recog_data.constraints[i][0],
1398 recog_data.constraints[i]))
1399 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1400 r1 = XEXP (r1, 0);
1402 /* Avoid making a call-saved register unnecessarily
1403 clobbered. */
1404 hard_reg = get_hard_reg_initial_reg (cfun, r1);
1405 if (hard_reg != NULL_RTX)
1407 if (REG_P (hard_reg)
1408 && REGNO (hard_reg) < FIRST_PSEUDO_REGISTER
1409 && !call_used_regs[REGNO (hard_reg)])
1410 continue;
1413 if (REG_P (r0) || GET_CODE (r0) == SUBREG)
1415 /* We have two priorities for hard register preferences.
1416 If we have a move insn or an insn whose first input
1417 can only be in the same register as the output, give
1418 priority to an equivalence found from that insn. */
1419 int may_save_copy
1420 = (r1 == recog_data.operand[i] && must_match_0 >= 0);
1422 if (REG_P (r1) || GET_CODE (r1) == SUBREG)
1423 win = combine_regs (r1, r0, may_save_copy,
1424 insn_number, insn, 0);
1426 if (win)
1427 break;
1431 /* Recognize an insn sequence with an ultimate result
1432 which can safely overlap one of the inputs.
1433 The sequence begins with a CLOBBER of its result,
1434 and ends with an insn that copies the result to itself
1435 and has a REG_EQUAL note for an equivalent formula.
1436 That note indicates what the inputs are.
1437 The result and the input can overlap if each insn in
1438 the sequence either doesn't mention the input
1439 or has a REG_NO_CONFLICT note to inhibit the conflict.
1441 We do the combining test at the CLOBBER so that the
1442 destination register won't have had a quantity number
1443 assigned, since that would prevent combining. */
1445 if (optimize
1446 && GET_CODE (PATTERN (insn)) == CLOBBER
1447 && (r0 = XEXP (PATTERN (insn), 0),
1448 REG_P (r0))
1449 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1450 && XEXP (link, 0) != 0
1451 && NONJUMP_INSN_P (XEXP (link, 0))
1452 && (set = single_set (XEXP (link, 0))) != 0
1453 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1454 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1455 NULL_RTX)) != 0)
1457 if (r1 = XEXP (note, 0), REG_P (r1)
1458 /* Check that we have such a sequence. */
1459 && no_conflict_p (insn, r0, r1))
1460 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1461 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1462 && (r1 = XEXP (XEXP (note, 0), 0),
1463 REG_P (r1) || GET_CODE (r1) == SUBREG)
1464 && no_conflict_p (insn, r0, r1))
1465 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1467 /* Here we care if the operation to be computed is
1468 commutative. */
1469 else if (COMMUTATIVE_P (XEXP (note, 0))
1470 && (r1 = XEXP (XEXP (note, 0), 1),
1471 (REG_P (r1) || GET_CODE (r1) == SUBREG))
1472 && no_conflict_p (insn, r0, r1))
1473 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1475 /* If we did combine something, show the register number
1476 in question so that we know to ignore its death. */
1477 if (win)
1478 no_conflict_combined_regno = REGNO (r1);
1481 /* If registers were just tied, set COMBINED_REGNO
1482 to the number of the register used in this insn
1483 that was tied to the register set in this insn.
1484 This register's qty should not be "killed". */
1486 if (win)
1488 while (GET_CODE (r1) == SUBREG)
1489 r1 = SUBREG_REG (r1);
1490 combined_regno = REGNO (r1);
1493 /* Mark the death of everything that dies in this instruction,
1494 except for anything that was just combined. */
1496 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1497 if (REG_NOTE_KIND (link) == REG_DEAD
1498 && REG_P (XEXP (link, 0))
1499 && combined_regno != (int) REGNO (XEXP (link, 0))
1500 && (no_conflict_combined_regno != (int) REGNO (XEXP (link, 0))
1501 || ! find_reg_note (insn, REG_NO_CONFLICT,
1502 XEXP (link, 0))))
1503 wipe_dead_reg (XEXP (link, 0), 0);
1505 /* Allocate qty numbers for all registers local to this block
1506 that are born (set) in this instruction.
1507 A pseudo that already has a qty is not changed. */
1509 note_stores (PATTERN (insn), reg_is_set, NULL);
1511 /* If anything is set in this insn and then unused, mark it as dying
1512 after this insn, so it will conflict with our outputs. This
1513 can't match with something that combined, and it doesn't matter
1514 if it did. Do this after the calls to reg_is_set since these
1515 die after, not during, the current insn. */
1517 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1518 if (REG_NOTE_KIND (link) == REG_UNUSED
1519 && REG_P (XEXP (link, 0)))
1520 wipe_dead_reg (XEXP (link, 0), 1);
1522 /* If this is an insn that has a REG_RETVAL note pointing at a
1523 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1524 block, so clear any register number that combined within it. */
1525 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1526 && NONJUMP_INSN_P (XEXP (note, 0))
1527 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1528 no_conflict_combined_regno = -1;
1531 /* Set the registers live after INSN_NUMBER. Note that we never
1532 record the registers live before the block's first insn, since no
1533 pseudos we care about are live before that insn. */
1535 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1536 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1538 if (insn == BB_END (BASIC_BLOCK (b)))
1539 break;
1541 insn = NEXT_INSN (insn);
1544 /* Now every register that is local to this basic block
1545 should have been given a quantity, or else -1 meaning ignore it.
1546 Every quantity should have a known birth and death.
1548 Order the qtys so we assign them registers in order of the
1549 number of suggested registers they need so we allocate those with
1550 the most restrictive needs first. */
1552 qty_order = xmalloc (next_qty * sizeof (int));
1553 for (i = 0; i < next_qty; i++)
1554 qty_order[i] = i;
1556 #define EXCHANGE(I1, I2) \
1557 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1559 switch (next_qty)
1561 case 3:
1562 /* Make qty_order[2] be the one to allocate last. */
1563 if (qty_sugg_compare (0, 1) > 0)
1564 EXCHANGE (0, 1);
1565 if (qty_sugg_compare (1, 2) > 0)
1566 EXCHANGE (2, 1);
1568 /* ... Fall through ... */
1569 case 2:
1570 /* Put the best one to allocate in qty_order[0]. */
1571 if (qty_sugg_compare (0, 1) > 0)
1572 EXCHANGE (0, 1);
1574 /* ... Fall through ... */
1576 case 1:
1577 case 0:
1578 /* Nothing to do here. */
1579 break;
1581 default:
1582 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1585 /* Try to put each quantity in a suggested physical register, if it has one.
1586 This may cause registers to be allocated that otherwise wouldn't be, but
1587 this seems acceptable in local allocation (unlike global allocation). */
1588 for (i = 0; i < next_qty; i++)
1590 q = qty_order[i];
1591 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1592 qty[q].phys_reg = find_free_reg (qty[q].min_class, qty[q].mode, q,
1593 0, 1, qty[q].birth, qty[q].death);
1594 else
1595 qty[q].phys_reg = -1;
1598 /* Order the qtys so we assign them registers in order of
1599 decreasing length of life. Normally call qsort, but if we
1600 have only a very small number of quantities, sort them ourselves. */
1602 for (i = 0; i < next_qty; i++)
1603 qty_order[i] = i;
1605 #define EXCHANGE(I1, I2) \
1606 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1608 switch (next_qty)
1610 case 3:
1611 /* Make qty_order[2] be the one to allocate last. */
1612 if (qty_compare (0, 1) > 0)
1613 EXCHANGE (0, 1);
1614 if (qty_compare (1, 2) > 0)
1615 EXCHANGE (2, 1);
1617 /* ... Fall through ... */
1618 case 2:
1619 /* Put the best one to allocate in qty_order[0]. */
1620 if (qty_compare (0, 1) > 0)
1621 EXCHANGE (0, 1);
1623 /* ... Fall through ... */
1625 case 1:
1626 case 0:
1627 /* Nothing to do here. */
1628 break;
1630 default:
1631 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1634 /* Now for each qty that is not a hardware register,
1635 look for a hardware register to put it in.
1636 First try the register class that is cheapest for this qty,
1637 if there is more than one class. */
1639 for (i = 0; i < next_qty; i++)
1641 q = qty_order[i];
1642 if (qty[q].phys_reg < 0)
1644 #ifdef INSN_SCHEDULING
1645 /* These values represent the adjusted lifetime of a qty so
1646 that it conflicts with qtys which appear near the start/end
1647 of this qty's lifetime.
1649 The purpose behind extending the lifetime of this qty is to
1650 discourage the register allocator from creating false
1651 dependencies.
1653 The adjustment value is chosen to indicate that this qty
1654 conflicts with all the qtys in the instructions immediately
1655 before and after the lifetime of this qty.
1657 Experiments have shown that higher values tend to hurt
1658 overall code performance.
1660 If allocation using the extended lifetime fails we will try
1661 again with the qty's unadjusted lifetime. */
1662 int fake_birth = MAX (0, qty[q].birth - 2 + qty[q].birth % 2);
1663 int fake_death = MIN (insn_number * 2 + 1,
1664 qty[q].death + 2 - qty[q].death % 2);
1665 #endif
1667 if (N_REG_CLASSES > 1)
1669 #ifdef INSN_SCHEDULING
1670 /* We try to avoid using hard registers allocated to qtys which
1671 are born immediately after this qty or die immediately before
1672 this qty.
1674 This optimization is only appropriate when we will run
1675 a scheduling pass after reload and we are not optimizing
1676 for code size. */
1677 if (flag_schedule_insns_after_reload
1678 && !optimize_size
1679 && !SMALL_REGISTER_CLASSES)
1681 qty[q].phys_reg = find_free_reg (qty[q].min_class,
1682 qty[q].mode, q, 0, 0,
1683 fake_birth, fake_death);
1684 if (qty[q].phys_reg >= 0)
1685 continue;
1687 #endif
1688 qty[q].phys_reg = find_free_reg (qty[q].min_class,
1689 qty[q].mode, q, 0, 0,
1690 qty[q].birth, qty[q].death);
1691 if (qty[q].phys_reg >= 0)
1692 continue;
1695 #ifdef INSN_SCHEDULING
1696 /* Similarly, avoid false dependencies. */
1697 if (flag_schedule_insns_after_reload
1698 && !optimize_size
1699 && !SMALL_REGISTER_CLASSES
1700 && qty[q].alternate_class != NO_REGS)
1701 qty[q].phys_reg = find_free_reg (qty[q].alternate_class,
1702 qty[q].mode, q, 0, 0,
1703 fake_birth, fake_death);
1704 #endif
1705 if (qty[q].alternate_class != NO_REGS)
1706 qty[q].phys_reg = find_free_reg (qty[q].alternate_class,
1707 qty[q].mode, q, 0, 0,
1708 qty[q].birth, qty[q].death);
1712 /* Now propagate the register assignments
1713 to the pseudo regs belonging to the qtys. */
1715 for (q = 0; q < next_qty; q++)
1716 if (qty[q].phys_reg >= 0)
1718 for (i = qty[q].first_reg; i >= 0; i = reg_next_in_qty[i])
1719 reg_renumber[i] = qty[q].phys_reg + reg_offset[i];
1722 /* Clean up. */
1723 free (regs_live_at);
1724 free (qty_order);
1727 /* Compare two quantities' priority for getting real registers.
1728 We give shorter-lived quantities higher priority.
1729 Quantities with more references are also preferred, as are quantities that
1730 require multiple registers. This is the identical prioritization as
1731 done by global-alloc.
1733 We used to give preference to registers with *longer* lives, but using
1734 the same algorithm in both local- and global-alloc can speed up execution
1735 of some programs by as much as a factor of three! */
1737 /* Note that the quotient will never be bigger than
1738 the value of floor_log2 times the maximum number of
1739 times a register can occur in one insn (surely less than 100)
1740 weighted by frequency (max REG_FREQ_MAX).
1741 Multiplying this by 10000/REG_FREQ_MAX can't overflow.
1742 QTY_CMP_PRI is also used by qty_sugg_compare. */
1744 #define QTY_CMP_PRI(q) \
1745 ((int) (((double) (floor_log2 (qty[q].n_refs) * qty[q].freq * qty[q].size) \
1746 / (qty[q].death - qty[q].birth)) * (10000 / REG_FREQ_MAX)))
1748 static int
1749 qty_compare (int q1, int q2)
1751 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1754 static int
1755 qty_compare_1 (const void *q1p, const void *q2p)
1757 int q1 = *(const int *) q1p, q2 = *(const int *) q2p;
1758 int tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1760 if (tem != 0)
1761 return tem;
1763 /* If qtys are equally good, sort by qty number,
1764 so that the results of qsort leave nothing to chance. */
1765 return q1 - q2;
1768 /* Compare two quantities' priority for getting real registers. This version
1769 is called for quantities that have suggested hard registers. First priority
1770 goes to quantities that have copy preferences, then to those that have
1771 normal preferences. Within those groups, quantities with the lower
1772 number of preferences have the highest priority. Of those, we use the same
1773 algorithm as above. */
1775 #define QTY_CMP_SUGG(q) \
1776 (qty_phys_num_copy_sugg[q] \
1777 ? qty_phys_num_copy_sugg[q] \
1778 : qty_phys_num_sugg[q] * FIRST_PSEUDO_REGISTER)
1780 static int
1781 qty_sugg_compare (int q1, int q2)
1783 int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1785 if (tem != 0)
1786 return tem;
1788 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1791 static int
1792 qty_sugg_compare_1 (const void *q1p, const void *q2p)
1794 int q1 = *(const int *) q1p, q2 = *(const int *) q2p;
1795 int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1797 if (tem != 0)
1798 return tem;
1800 tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1801 if (tem != 0)
1802 return tem;
1804 /* If qtys are equally good, sort by qty number,
1805 so that the results of qsort leave nothing to chance. */
1806 return q1 - q2;
1809 #undef QTY_CMP_SUGG
1810 #undef QTY_CMP_PRI
1812 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1813 Returns 1 if have done so, or 0 if cannot.
1815 Combining registers means marking them as having the same quantity
1816 and adjusting the offsets within the quantity if either of
1817 them is a SUBREG.
1819 We don't actually combine a hard reg with a pseudo; instead
1820 we just record the hard reg as the suggestion for the pseudo's quantity.
1821 If we really combined them, we could lose if the pseudo lives
1822 across an insn that clobbers the hard reg (eg, movmem).
1824 ALREADY_DEAD is nonzero if USEDREG is known to be dead even though
1825 there is no REG_DEAD note on INSN. This occurs during the processing
1826 of REG_NO_CONFLICT blocks.
1828 MAY_SAVE_COPY is nonzero if this insn is simply copying USEDREG to
1829 SETREG or if the input and output must share a register.
1830 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1832 There are elaborate checks for the validity of combining. */
1834 static int
1835 combine_regs (rtx usedreg, rtx setreg, int may_save_copy, int insn_number,
1836 rtx insn, int already_dead)
1838 int ureg, sreg;
1839 int offset = 0;
1840 int usize, ssize;
1841 int sqty;
1843 /* Determine the numbers and sizes of registers being used. If a subreg
1844 is present that does not change the entire register, don't consider
1845 this a copy insn. */
1847 while (GET_CODE (usedreg) == SUBREG)
1849 rtx subreg = SUBREG_REG (usedreg);
1851 if (REG_P (subreg))
1853 if (GET_MODE_SIZE (GET_MODE (subreg)) > UNITS_PER_WORD)
1854 may_save_copy = 0;
1856 if (REGNO (subreg) < FIRST_PSEUDO_REGISTER)
1857 offset += subreg_regno_offset (REGNO (subreg),
1858 GET_MODE (subreg),
1859 SUBREG_BYTE (usedreg),
1860 GET_MODE (usedreg));
1861 else
1862 offset += (SUBREG_BYTE (usedreg)
1863 / REGMODE_NATURAL_SIZE (GET_MODE (usedreg)));
1866 usedreg = subreg;
1869 if (!REG_P (usedreg))
1870 return 0;
1872 ureg = REGNO (usedreg);
1873 if (ureg < FIRST_PSEUDO_REGISTER)
1874 usize = hard_regno_nregs[ureg][GET_MODE (usedreg)];
1875 else
1876 usize = ((GET_MODE_SIZE (GET_MODE (usedreg))
1877 + (REGMODE_NATURAL_SIZE (GET_MODE (usedreg)) - 1))
1878 / REGMODE_NATURAL_SIZE (GET_MODE (usedreg)));
1880 while (GET_CODE (setreg) == SUBREG)
1882 rtx subreg = SUBREG_REG (setreg);
1884 if (REG_P (subreg))
1886 if (GET_MODE_SIZE (GET_MODE (subreg)) > UNITS_PER_WORD)
1887 may_save_copy = 0;
1889 if (REGNO (subreg) < FIRST_PSEUDO_REGISTER)
1890 offset -= subreg_regno_offset (REGNO (subreg),
1891 GET_MODE (subreg),
1892 SUBREG_BYTE (setreg),
1893 GET_MODE (setreg));
1894 else
1895 offset -= (SUBREG_BYTE (setreg)
1896 / REGMODE_NATURAL_SIZE (GET_MODE (setreg)));
1899 setreg = subreg;
1902 if (!REG_P (setreg))
1903 return 0;
1905 sreg = REGNO (setreg);
1906 if (sreg < FIRST_PSEUDO_REGISTER)
1907 ssize = hard_regno_nregs[sreg][GET_MODE (setreg)];
1908 else
1909 ssize = ((GET_MODE_SIZE (GET_MODE (setreg))
1910 + (REGMODE_NATURAL_SIZE (GET_MODE (setreg)) - 1))
1911 / REGMODE_NATURAL_SIZE (GET_MODE (setreg)));
1913 /* If UREG is a pseudo-register that hasn't already been assigned a
1914 quantity number, it means that it is not local to this block or dies
1915 more than once. In either event, we can't do anything with it. */
1916 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1917 /* Do not combine registers unless one fits within the other. */
1918 || (offset > 0 && usize + offset > ssize)
1919 || (offset < 0 && usize + offset < ssize)
1920 /* Do not combine with a smaller already-assigned object
1921 if that smaller object is already combined with something bigger. */
1922 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1923 && usize < qty[reg_qty[ureg]].size)
1924 /* Can't combine if SREG is not a register we can allocate. */
1925 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1926 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1927 These have already been taken care of. This probably wouldn't
1928 combine anyway, but don't take any chances. */
1929 || (ureg >= FIRST_PSEUDO_REGISTER
1930 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1931 /* Don't tie something to itself. In most cases it would make no
1932 difference, but it would screw up if the reg being tied to itself
1933 also dies in this insn. */
1934 || ureg == sreg
1935 /* Don't try to connect two different hardware registers. */
1936 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1937 /* Don't connect two different machine modes if they have different
1938 implications as to which registers may be used. */
1939 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1940 return 0;
1942 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1943 qty_phys_sugg for the pseudo instead of tying them.
1945 Return "failure" so that the lifespan of UREG is terminated here;
1946 that way the two lifespans will be disjoint and nothing will prevent
1947 the pseudo reg from being given this hard reg. */
1949 if (ureg < FIRST_PSEUDO_REGISTER)
1951 /* Allocate a quantity number so we have a place to put our
1952 suggestions. */
1953 if (reg_qty[sreg] == -2)
1954 reg_is_born (setreg, 2 * insn_number);
1956 if (reg_qty[sreg] >= 0)
1958 if (may_save_copy
1959 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1961 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1962 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1964 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1966 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1967 qty_phys_num_sugg[reg_qty[sreg]]++;
1970 return 0;
1973 /* Similarly for SREG a hard register and UREG a pseudo register. */
1975 if (sreg < FIRST_PSEUDO_REGISTER)
1977 if (may_save_copy
1978 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1980 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1981 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1983 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1985 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1986 qty_phys_num_sugg[reg_qty[ureg]]++;
1988 return 0;
1991 /* At this point we know that SREG and UREG are both pseudos.
1992 Do nothing if SREG already has a quantity or is a register that we
1993 don't allocate. */
1994 if (reg_qty[sreg] >= -1
1995 /* If we are not going to let any regs live across calls,
1996 don't tie a call-crossing reg to a non-call-crossing reg. */
1997 || (current_function_has_nonlocal_label
1998 && ((REG_N_CALLS_CROSSED (ureg) > 0)
1999 != (REG_N_CALLS_CROSSED (sreg) > 0))))
2000 return 0;
2002 /* We don't already know about SREG, so tie it to UREG
2003 if this is the last use of UREG, provided the classes they want
2004 are compatible. */
2006 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
2007 && reg_meets_class_p (sreg, qty[reg_qty[ureg]].min_class))
2009 /* Add SREG to UREG's quantity. */
2010 sqty = reg_qty[ureg];
2011 reg_qty[sreg] = sqty;
2012 reg_offset[sreg] = reg_offset[ureg] + offset;
2013 reg_next_in_qty[sreg] = qty[sqty].first_reg;
2014 qty[sqty].first_reg = sreg;
2016 /* If SREG's reg class is smaller, set qty[SQTY].min_class. */
2017 update_qty_class (sqty, sreg);
2019 /* Update info about quantity SQTY. */
2020 qty[sqty].n_calls_crossed += REG_N_CALLS_CROSSED (sreg);
2021 qty[sqty].n_throwing_calls_crossed
2022 += REG_N_THROWING_CALLS_CROSSED (sreg);
2023 qty[sqty].n_refs += REG_N_REFS (sreg);
2024 qty[sqty].freq += REG_FREQ (sreg);
2025 if (usize < ssize)
2027 int i;
2029 for (i = qty[sqty].first_reg; i >= 0; i = reg_next_in_qty[i])
2030 reg_offset[i] -= offset;
2032 qty[sqty].size = ssize;
2033 qty[sqty].mode = GET_MODE (setreg);
2036 else
2037 return 0;
2039 return 1;
2042 /* Return 1 if the preferred class of REG allows it to be tied
2043 to a quantity or register whose class is CLASS.
2044 True if REG's reg class either contains or is contained in CLASS. */
2046 static int
2047 reg_meets_class_p (int reg, enum reg_class class)
2049 enum reg_class rclass = reg_preferred_class (reg);
2050 return (reg_class_subset_p (rclass, class)
2051 || reg_class_subset_p (class, rclass));
2054 /* Update the class of QTYNO assuming that REG is being tied to it. */
2056 static void
2057 update_qty_class (int qtyno, int reg)
2059 enum reg_class rclass = reg_preferred_class (reg);
2060 if (reg_class_subset_p (rclass, qty[qtyno].min_class))
2061 qty[qtyno].min_class = rclass;
2063 rclass = reg_alternate_class (reg);
2064 if (reg_class_subset_p (rclass, qty[qtyno].alternate_class))
2065 qty[qtyno].alternate_class = rclass;
2068 /* Handle something which alters the value of an rtx REG.
2070 REG is whatever is set or clobbered. SETTER is the rtx that
2071 is modifying the register.
2073 If it is not really a register, we do nothing.
2074 The file-global variables `this_insn' and `this_insn_number'
2075 carry info from `block_alloc'. */
2077 static void
2078 reg_is_set (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
2080 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
2081 a hard register. These may actually not exist any more. */
2083 if (GET_CODE (reg) != SUBREG
2084 && !REG_P (reg))
2085 return;
2087 /* Mark this register as being born. If it is used in a CLOBBER, mark
2088 it as being born halfway between the previous insn and this insn so that
2089 it conflicts with our inputs but not the outputs of the previous insn. */
2091 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
2094 /* Handle beginning of the life of register REG.
2095 BIRTH is the index at which this is happening. */
2097 static void
2098 reg_is_born (rtx reg, int birth)
2100 int regno;
2102 if (GET_CODE (reg) == SUBREG)
2104 regno = REGNO (SUBREG_REG (reg));
2105 if (regno < FIRST_PSEUDO_REGISTER)
2106 regno = subreg_regno (reg);
2108 else
2109 regno = REGNO (reg);
2111 if (regno < FIRST_PSEUDO_REGISTER)
2113 mark_life (regno, GET_MODE (reg), 1);
2115 /* If the register was to have been born earlier that the present
2116 insn, mark it as live where it is actually born. */
2117 if (birth < 2 * this_insn_number)
2118 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
2120 else
2122 if (reg_qty[regno] == -2)
2123 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
2125 /* If this register has a quantity number, show that it isn't dead. */
2126 if (reg_qty[regno] >= 0)
2127 qty[reg_qty[regno]].death = -1;
2131 /* Record the death of REG in the current insn. If OUTPUT_P is nonzero,
2132 REG is an output that is dying (i.e., it is never used), otherwise it
2133 is an input (the normal case).
2134 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
2136 static void
2137 wipe_dead_reg (rtx reg, int output_p)
2139 int regno = REGNO (reg);
2141 /* If this insn has multiple results,
2142 and the dead reg is used in one of the results,
2143 extend its life to after this insn,
2144 so it won't get allocated together with any other result of this insn.
2146 It is unsafe to use !single_set here since it will ignore an unused
2147 output. Just because an output is unused does not mean the compiler
2148 can assume the side effect will not occur. Consider if REG appears
2149 in the address of an output and we reload the output. If we allocate
2150 REG to the same hard register as an unused output we could set the hard
2151 register before the output reload insn. */
2152 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2153 && multiple_sets (this_insn))
2155 int i;
2156 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2158 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2159 if (GET_CODE (set) == SET
2160 && !REG_P (SET_DEST (set))
2161 && !rtx_equal_p (reg, SET_DEST (set))
2162 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2163 output_p = 1;
2167 /* If this register is used in an auto-increment address, then extend its
2168 life to after this insn, so that it won't get allocated together with
2169 the result of this insn. */
2170 if (! output_p && find_regno_note (this_insn, REG_INC, regno))
2171 output_p = 1;
2173 if (regno < FIRST_PSEUDO_REGISTER)
2175 mark_life (regno, GET_MODE (reg), 0);
2177 /* If a hard register is dying as an output, mark it as in use at
2178 the beginning of this insn (the above statement would cause this
2179 not to happen). */
2180 if (output_p)
2181 post_mark_life (regno, GET_MODE (reg), 1,
2182 2 * this_insn_number, 2 * this_insn_number + 1);
2185 else if (reg_qty[regno] >= 0)
2186 qty[reg_qty[regno]].death = 2 * this_insn_number + output_p;
2189 /* Find a block of SIZE words of hard regs in reg_class CLASS
2190 that can hold something of machine-mode MODE
2191 (but actually we test only the first of the block for holding MODE)
2192 and still free between insn BORN_INDEX and insn DEAD_INDEX,
2193 and return the number of the first of them.
2194 Return -1 if such a block cannot be found.
2195 If QTYNO crosses calls, insist on a register preserved by calls,
2196 unless ACCEPT_CALL_CLOBBERED is nonzero.
2198 If JUST_TRY_SUGGESTED is nonzero, only try to see if the suggested
2199 register is available. If not, return -1. */
2201 static int
2202 find_free_reg (enum reg_class class, enum machine_mode mode, int qtyno,
2203 int accept_call_clobbered, int just_try_suggested,
2204 int born_index, int dead_index)
2206 int i, ins;
2207 HARD_REG_SET first_used, used;
2208 #ifdef ELIMINABLE_REGS
2209 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2210 #endif
2212 /* Validate our parameters. */
2213 gcc_assert (born_index >= 0 && born_index <= dead_index);
2215 /* Don't let a pseudo live in a reg across a function call
2216 if we might get a nonlocal goto. */
2217 if (current_function_has_nonlocal_label
2218 && qty[qtyno].n_calls_crossed > 0)
2219 return -1;
2221 if (accept_call_clobbered)
2222 COPY_HARD_REG_SET (used, call_fixed_reg_set);
2223 else if (qty[qtyno].n_calls_crossed == 0)
2224 COPY_HARD_REG_SET (used, fixed_reg_set);
2225 else
2226 COPY_HARD_REG_SET (used, call_used_reg_set);
2228 if (accept_call_clobbered)
2229 IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
2231 for (ins = born_index; ins < dead_index; ins++)
2232 IOR_HARD_REG_SET (used, regs_live_at[ins]);
2234 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2236 /* Don't use the frame pointer reg in local-alloc even if
2237 we may omit the frame pointer, because if we do that and then we
2238 need a frame pointer, reload won't know how to move the pseudo
2239 to another hard reg. It can move only regs made by global-alloc.
2241 This is true of any register that can be eliminated. */
2242 #ifdef ELIMINABLE_REGS
2243 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2244 SET_HARD_REG_BIT (used, eliminables[i].from);
2245 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2246 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2247 that it might be eliminated into. */
2248 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2249 #endif
2250 #else
2251 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2252 #endif
2254 #ifdef CANNOT_CHANGE_MODE_CLASS
2255 cannot_change_mode_set_regs (&used, mode, qty[qtyno].first_reg);
2256 #endif
2258 /* Normally, the registers that can be used for the first register in
2259 a multi-register quantity are the same as those that can be used for
2260 subsequent registers. However, if just trying suggested registers,
2261 restrict our consideration to them. If there are copy-suggested
2262 register, try them. Otherwise, try the arithmetic-suggested
2263 registers. */
2264 COPY_HARD_REG_SET (first_used, used);
2266 if (just_try_suggested)
2268 if (qty_phys_num_copy_sugg[qtyno] != 0)
2269 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qtyno]);
2270 else
2271 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qtyno]);
2274 /* If all registers are excluded, we can't do anything. */
2275 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2277 /* If at least one would be suitable, test each hard reg. */
2279 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2281 #ifdef REG_ALLOC_ORDER
2282 int regno = reg_alloc_order[i];
2283 #else
2284 int regno = i;
2285 #endif
2286 if (! TEST_HARD_REG_BIT (first_used, regno)
2287 && HARD_REGNO_MODE_OK (regno, mode)
2288 && (qty[qtyno].n_calls_crossed == 0
2289 || accept_call_clobbered
2290 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
2292 int j;
2293 int size1 = hard_regno_nregs[regno][mode];
2294 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2295 if (j == size1)
2297 /* Mark that this register is in use between its birth and death
2298 insns. */
2299 post_mark_life (regno, mode, 1, born_index, dead_index);
2300 return regno;
2302 #ifndef REG_ALLOC_ORDER
2303 /* Skip starting points we know will lose. */
2304 i += j;
2305 #endif
2309 fail:
2310 /* If we are just trying suggested register, we have just tried copy-
2311 suggested registers, and there are arithmetic-suggested registers,
2312 try them. */
2314 /* If it would be profitable to allocate a call-clobbered register
2315 and save and restore it around calls, do that. */
2316 if (just_try_suggested && qty_phys_num_copy_sugg[qtyno] != 0
2317 && qty_phys_num_sugg[qtyno] != 0)
2319 /* Don't try the copy-suggested regs again. */
2320 qty_phys_num_copy_sugg[qtyno] = 0;
2321 return find_free_reg (class, mode, qtyno, accept_call_clobbered, 1,
2322 born_index, dead_index);
2325 /* We need not check to see if the current function has nonlocal
2326 labels because we don't put any pseudos that are live over calls in
2327 registers in that case. Avoid putting pseudos crossing calls that
2328 might throw into call used registers. */
2330 if (! accept_call_clobbered
2331 && flag_caller_saves
2332 && ! just_try_suggested
2333 && qty[qtyno].n_calls_crossed != 0
2334 && qty[qtyno].n_throwing_calls_crossed == 0
2335 && CALLER_SAVE_PROFITABLE (qty[qtyno].n_refs,
2336 qty[qtyno].n_calls_crossed))
2338 i = find_free_reg (class, mode, qtyno, 1, 0, born_index, dead_index);
2339 if (i >= 0)
2340 caller_save_needed = 1;
2341 return i;
2343 return -1;
2346 /* Mark that REGNO with machine-mode MODE is live starting from the current
2347 insn (if LIFE is nonzero) or dead starting at the current insn (if LIFE
2348 is zero). */
2350 static void
2351 mark_life (int regno, enum machine_mode mode, int life)
2353 int j = hard_regno_nregs[regno][mode];
2354 if (life)
2355 while (--j >= 0)
2356 SET_HARD_REG_BIT (regs_live, regno + j);
2357 else
2358 while (--j >= 0)
2359 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2362 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2363 is nonzero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2364 to insn number DEATH (exclusive). */
2366 static void
2367 post_mark_life (int regno, enum machine_mode mode, int life, int birth,
2368 int death)
2370 int j = hard_regno_nregs[regno][mode];
2371 HARD_REG_SET this_reg;
2373 CLEAR_HARD_REG_SET (this_reg);
2374 while (--j >= 0)
2375 SET_HARD_REG_BIT (this_reg, regno + j);
2377 if (life)
2378 while (birth < death)
2380 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2381 birth++;
2383 else
2384 while (birth < death)
2386 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2387 birth++;
2391 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2392 is the register being clobbered, and R1 is a register being used in
2393 the equivalent expression.
2395 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2396 in which it is used, return 1.
2398 Otherwise, return 0. */
2400 static int
2401 no_conflict_p (rtx insn, rtx r0 ATTRIBUTE_UNUSED, rtx r1)
2403 int ok = 0;
2404 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2405 rtx p, last;
2407 /* If R1 is a hard register, return 0 since we handle this case
2408 when we scan the insns that actually use it. */
2410 if (note == 0
2411 || (REG_P (r1) && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2412 || (GET_CODE (r1) == SUBREG && REG_P (SUBREG_REG (r1))
2413 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2414 return 0;
2416 last = XEXP (note, 0);
2418 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2419 if (INSN_P (p))
2421 if (find_reg_note (p, REG_DEAD, r1))
2422 ok = 1;
2424 /* There must be a REG_NO_CONFLICT note on every insn, otherwise
2425 some earlier optimization pass has inserted instructions into
2426 the sequence, and it is not safe to perform this optimization.
2427 Note that emit_no_conflict_block always ensures that this is
2428 true when these sequences are created. */
2429 if (! find_reg_note (p, REG_NO_CONFLICT, r1))
2430 return 0;
2433 return ok;
2436 /* Return the number of alternatives for which the constraint string P
2437 indicates that the operand must be equal to operand 0 and that no register
2438 is acceptable. */
2440 static int
2441 requires_inout (const char *p)
2443 char c;
2444 int found_zero = 0;
2445 int reg_allowed = 0;
2446 int num_matching_alts = 0;
2447 int len;
2449 for ( ; (c = *p); p += len)
2451 len = CONSTRAINT_LEN (c, p);
2452 switch (c)
2454 case '=': case '+': case '?':
2455 case '#': case '&': case '!':
2456 case '*': case '%':
2457 case 'm': case '<': case '>': case 'V': case 'o':
2458 case 'E': case 'F': case 'G': case 'H':
2459 case 's': case 'i': case 'n':
2460 case 'I': case 'J': case 'K': case 'L':
2461 case 'M': case 'N': case 'O': case 'P':
2462 case 'X':
2463 /* These don't say anything we care about. */
2464 break;
2466 case ',':
2467 if (found_zero && ! reg_allowed)
2468 num_matching_alts++;
2470 found_zero = reg_allowed = 0;
2471 break;
2473 case '0':
2474 found_zero = 1;
2475 break;
2477 case '1': case '2': case '3': case '4': case '5':
2478 case '6': case '7': case '8': case '9':
2479 /* Skip the balance of the matching constraint. */
2481 p++;
2482 while (ISDIGIT (*p));
2483 len = 0;
2484 break;
2486 default:
2487 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS
2488 && !EXTRA_ADDRESS_CONSTRAINT (c, p))
2489 break;
2490 /* Fall through. */
2491 case 'p':
2492 case 'g': case 'r':
2493 reg_allowed = 1;
2494 break;
2498 if (found_zero && ! reg_allowed)
2499 num_matching_alts++;
2501 return num_matching_alts;
2504 void
2505 dump_local_alloc (FILE *file)
2507 int i;
2508 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2509 if (reg_renumber[i] != -1)
2510 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
2513 /* Run old register allocator. Return TRUE if we must exit
2514 rest_of_compilation upon return. */
2515 static void
2516 rest_of_handle_local_alloc (void)
2518 int rebuild_notes;
2520 /* Determine if the current function is a leaf before running reload
2521 since this can impact optimizations done by the prologue and
2522 epilogue thus changing register elimination offsets. */
2523 current_function_is_leaf = leaf_function_p ();
2525 /* Allocate the reg_renumber array. */
2526 allocate_reg_info (max_regno, FALSE, TRUE);
2528 /* And the reg_equiv_memory_loc array. */
2529 VARRAY_GROW (reg_equiv_memory_loc_varray, max_regno);
2530 reg_equiv_memory_loc = &VARRAY_RTX (reg_equiv_memory_loc_varray, 0);
2532 allocate_initial_values (reg_equiv_memory_loc);
2534 regclass (get_insns (), max_reg_num (), dump_file);
2535 rebuild_notes = local_alloc ();
2537 /* Local allocation may have turned an indirect jump into a direct
2538 jump. If so, we must rebuild the JUMP_LABEL fields of jumping
2539 instructions. */
2540 if (rebuild_notes)
2542 timevar_push (TV_JUMP);
2544 rebuild_jump_labels (get_insns ());
2545 purge_all_dead_edges ();
2546 delete_unreachable_blocks ();
2548 timevar_pop (TV_JUMP);
2551 if (dump_enabled_p (pass_local_alloc.static_pass_number))
2553 timevar_push (TV_DUMP);
2554 dump_flow_info (dump_file);
2555 dump_local_alloc (dump_file);
2556 timevar_pop (TV_DUMP);
2560 struct tree_opt_pass pass_local_alloc =
2562 "lreg", /* name */
2563 NULL, /* gate */
2564 rest_of_handle_local_alloc, /* execute */
2565 NULL, /* sub */
2566 NULL, /* next */
2567 0, /* static_pass_number */
2568 TV_LOCAL_ALLOC, /* tv_id */
2569 0, /* properties_required */
2570 0, /* properties_provided */
2571 0, /* properties_destroyed */
2572 0, /* todo_flags_start */
2573 TODO_dump_func |
2574 TODO_ggc_collect, /* todo_flags_finish */
2575 'l' /* letter */