PR c++/11509
[official-gcc.git] / gcc / local-alloc.c
blob3f240fafd1f47e8409c91918eb774305275b3214
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 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, 59 Temple Place - Suite 330, Boston, MA
20 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 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 "basic-block.h"
71 #include "regs.h"
72 #include "function.h"
73 #include "insn-config.h"
74 #include "insn-attr.h"
75 #include "recog.h"
76 #include "output.h"
77 #include "toplev.h"
78 #include "except.h"
79 #include "integrate.h"
81 /* Next quantity number available for allocation. */
83 static int next_qty;
85 /* Information we maintain about each quantity. */
86 struct qty
88 /* The number of refs to quantity Q. */
90 int n_refs;
92 /* The frequency of uses of quantity Q. */
94 int freq;
96 /* Insn number (counting from head of basic block)
97 where quantity Q was born. -1 if birth has not been recorded. */
99 int birth;
101 /* Insn number (counting from head of basic block)
102 where given quantity died. Due to the way tying is done,
103 and the fact that we consider in this pass only regs that die but once,
104 a quantity can die only once. Each quantity's life span
105 is a set of consecutive insns. -1 if death has not been recorded. */
107 int death;
109 /* Number of words needed to hold the data in given quantity.
110 This depends on its machine mode. It is used for these purposes:
111 1. It is used in computing the relative importances of qtys,
112 which determines the order in which we look for regs for them.
113 2. It is used in rules that prevent tying several registers of
114 different sizes in a way that is geometrically impossible
115 (see combine_regs). */
117 int size;
119 /* Number of times a reg tied to given qty lives across a CALL_INSN. */
121 int n_calls_crossed;
123 /* The register number of one pseudo register whose reg_qty value is Q.
124 This register should be the head of the chain
125 maintained in reg_next_in_qty. */
127 int first_reg;
129 /* Reg class contained in (smaller than) the preferred classes of all
130 the pseudo regs that are tied in given quantity.
131 This is the preferred class for allocating that quantity. */
133 enum reg_class min_class;
135 /* Register class within which we allocate given qty if we can't get
136 its preferred class. */
138 enum reg_class alternate_class;
140 /* This holds the mode of the registers that are tied to given qty,
141 or VOIDmode if registers with differing modes are tied together. */
143 enum machine_mode mode;
145 /* the hard reg number chosen for given quantity,
146 or -1 if none was found. */
148 short phys_reg;
151 static struct qty *qty;
153 /* These fields are kept separately to speedup their clearing. */
155 /* We maintain two hard register sets that indicate suggested hard registers
156 for each quantity. The first, phys_copy_sugg, contains hard registers
157 that are tied to the quantity by a simple copy. The second contains all
158 hard registers that are tied to the quantity via an arithmetic operation.
160 The former register set is given priority for allocation. This tends to
161 eliminate copy insns. */
163 /* Element Q is a set of hard registers that are suggested for quantity Q by
164 copy insns. */
166 static HARD_REG_SET *qty_phys_copy_sugg;
168 /* Element Q is a set of hard registers that are suggested for quantity Q by
169 arithmetic insns. */
171 static HARD_REG_SET *qty_phys_sugg;
173 /* Element Q is the number of suggested registers in qty_phys_copy_sugg. */
175 static short *qty_phys_num_copy_sugg;
177 /* Element Q is the number of suggested registers in qty_phys_sugg. */
179 static short *qty_phys_num_sugg;
181 /* If (REG N) has been assigned a quantity number, is a register number
182 of another register assigned the same quantity number, or -1 for the
183 end of the chain. qty->first_reg point to the head of this chain. */
185 static int *reg_next_in_qty;
187 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
188 if it is >= 0,
189 of -1 if this register cannot be allocated by local-alloc,
190 or -2 if not known yet.
192 Note that if we see a use or death of pseudo register N with
193 reg_qty[N] == -2, register N must be local to the current block. If
194 it were used in more than one block, we would have reg_qty[N] == -1.
195 This relies on the fact that if reg_basic_block[N] is >= 0, register N
196 will not appear in any other block. We save a considerable number of
197 tests by exploiting this.
199 If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
200 be referenced. */
202 static int *reg_qty;
204 /* The offset (in words) of register N within its quantity.
205 This can be nonzero if register N is SImode, and has been tied
206 to a subreg of a DImode register. */
208 static char *reg_offset;
210 /* Vector of substitutions of register numbers,
211 used to map pseudo regs into hardware regs.
212 This is set up as a result of register allocation.
213 Element N is the hard reg assigned to pseudo reg N,
214 or is -1 if no hard reg was assigned.
215 If N is a hard reg number, element N is N. */
217 short *reg_renumber;
219 /* Set of hard registers live at the current point in the scan
220 of the instructions in a basic block. */
222 static HARD_REG_SET regs_live;
224 /* Each set of hard registers indicates registers live at a particular
225 point in the basic block. For N even, regs_live_at[N] says which
226 hard registers are needed *after* insn N/2 (i.e., they may not
227 conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
229 If an object is to conflict with the inputs of insn J but not the
230 outputs of insn J + 1, we say it is born at index J*2 - 1. Similarly,
231 if it is to conflict with the outputs of insn J but not the inputs of
232 insn J + 1, it is said to die at index J*2 + 1. */
234 static HARD_REG_SET *regs_live_at;
236 /* Communicate local vars `insn_number' and `insn'
237 from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'. */
238 static int this_insn_number;
239 static rtx this_insn;
241 struct equivalence
243 /* Set when an attempt should be made to replace a register
244 with the associated src_p entry. */
246 char replace;
248 /* Set when a REG_EQUIV note is found or created. Use to
249 keep track of what memory accesses might be created later,
250 e.g. by reload. */
252 rtx replacement;
254 rtx *src_p;
256 /* Loop depth is used to recognize equivalences which appear
257 to be present within the same loop (or in an inner loop). */
259 int loop_depth;
261 /* The list of each instruction which initializes this register. */
263 rtx init_insns;
266 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
267 structure for that register. */
269 static struct equivalence *reg_equiv;
271 /* Nonzero if we recorded an equivalence for a LABEL_REF. */
272 static int recorded_label_ref;
274 static void alloc_qty (int, enum machine_mode, int, int);
275 static void validate_equiv_mem_from_store (rtx, rtx, void *);
276 static int validate_equiv_mem (rtx, rtx, rtx);
277 static int equiv_init_varies_p (rtx);
278 static int equiv_init_movable_p (rtx, int);
279 static int contains_replace_regs (rtx);
280 static int memref_referenced_p (rtx, rtx);
281 static int memref_used_between_p (rtx, rtx, rtx);
282 static void update_equiv_regs (void);
283 static void no_equiv (rtx, rtx, void *);
284 static void block_alloc (int);
285 static int qty_sugg_compare (int, int);
286 static int qty_sugg_compare_1 (const void *, const void *);
287 static int qty_compare (int, int);
288 static int qty_compare_1 (const void *, const void *);
289 static int combine_regs (rtx, rtx, int, int, rtx, int);
290 static int reg_meets_class_p (int, enum reg_class);
291 static void update_qty_class (int, int);
292 static void reg_is_set (rtx, rtx, void *);
293 static void reg_is_born (rtx, int);
294 static void wipe_dead_reg (rtx, int);
295 static int find_free_reg (enum reg_class, enum machine_mode, int, int, int,
296 int, int);
297 static void mark_life (int, enum machine_mode, int);
298 static void post_mark_life (int, enum machine_mode, int, int, int);
299 static int no_conflict_p (rtx, rtx, rtx);
300 static int requires_inout (const char *);
302 /* Allocate a new quantity (new within current basic block)
303 for register number REGNO which is born at index BIRTH
304 within the block. MODE and SIZE are info on reg REGNO. */
306 static void
307 alloc_qty (int regno, enum machine_mode mode, int size, int birth)
309 int qtyno = next_qty++;
311 reg_qty[regno] = qtyno;
312 reg_offset[regno] = 0;
313 reg_next_in_qty[regno] = -1;
315 qty[qtyno].first_reg = regno;
316 qty[qtyno].size = size;
317 qty[qtyno].mode = mode;
318 qty[qtyno].birth = birth;
319 qty[qtyno].n_calls_crossed = REG_N_CALLS_CROSSED (regno);
320 qty[qtyno].min_class = reg_preferred_class (regno);
321 qty[qtyno].alternate_class = reg_alternate_class (regno);
322 qty[qtyno].n_refs = REG_N_REFS (regno);
323 qty[qtyno].freq = REG_FREQ (regno);
326 /* Main entry point of this file. */
329 local_alloc (void)
331 int i;
332 int max_qty;
333 basic_block b;
335 /* We need to keep track of whether or not we recorded a LABEL_REF so
336 that we know if the jump optimizer needs to be rerun. */
337 recorded_label_ref = 0;
339 /* Leaf functions and non-leaf functions have different needs.
340 If defined, let the machine say what kind of ordering we
341 should use. */
342 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
343 ORDER_REGS_FOR_LOCAL_ALLOC;
344 #endif
346 /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
347 registers. */
348 if (optimize)
349 update_equiv_regs ();
351 /* This sets the maximum number of quantities we can have. Quantity
352 numbers start at zero and we can have one for each pseudo. */
353 max_qty = (max_regno - FIRST_PSEUDO_REGISTER);
355 /* Allocate vectors of temporary data.
356 See the declarations of these variables, above,
357 for what they mean. */
359 qty = (struct qty *) xmalloc (max_qty * sizeof (struct qty));
360 qty_phys_copy_sugg
361 = (HARD_REG_SET *) xmalloc (max_qty * sizeof (HARD_REG_SET));
362 qty_phys_num_copy_sugg = (short *) xmalloc (max_qty * sizeof (short));
363 qty_phys_sugg = (HARD_REG_SET *) xmalloc (max_qty * sizeof (HARD_REG_SET));
364 qty_phys_num_sugg = (short *) xmalloc (max_qty * sizeof (short));
366 reg_qty = (int *) xmalloc (max_regno * sizeof (int));
367 reg_offset = (char *) xmalloc (max_regno * sizeof (char));
368 reg_next_in_qty = (int *) xmalloc (max_regno * sizeof (int));
370 /* Determine which pseudo-registers can be allocated by local-alloc.
371 In general, these are the registers used only in a single block and
372 which only die once.
374 We need not be concerned with which block actually uses the register
375 since we will never see it outside that block. */
377 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
379 if (REG_BASIC_BLOCK (i) >= 0 && REG_N_DEATHS (i) == 1)
380 reg_qty[i] = -2;
381 else
382 reg_qty[i] = -1;
385 /* Force loop below to initialize entire quantity array. */
386 next_qty = max_qty;
388 /* Allocate each block's local registers, block by block. */
390 FOR_EACH_BB (b)
392 /* NEXT_QTY indicates which elements of the `qty_...'
393 vectors might need to be initialized because they were used
394 for the previous block; it is set to the entire array before
395 block 0. Initialize those, with explicit loop if there are few,
396 else with bzero and bcopy. Do not initialize vectors that are
397 explicit set by `alloc_qty'. */
399 if (next_qty < 6)
401 for (i = 0; i < next_qty; i++)
403 CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
404 qty_phys_num_copy_sugg[i] = 0;
405 CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
406 qty_phys_num_sugg[i] = 0;
409 else
411 #define CLEAR(vector) \
412 memset ((char *) (vector), 0, (sizeof (*(vector))) * next_qty);
414 CLEAR (qty_phys_copy_sugg);
415 CLEAR (qty_phys_num_copy_sugg);
416 CLEAR (qty_phys_sugg);
417 CLEAR (qty_phys_num_sugg);
420 next_qty = 0;
422 block_alloc (b->index);
425 free (qty);
426 free (qty_phys_copy_sugg);
427 free (qty_phys_num_copy_sugg);
428 free (qty_phys_sugg);
429 free (qty_phys_num_sugg);
431 free (reg_qty);
432 free (reg_offset);
433 free (reg_next_in_qty);
435 return recorded_label_ref;
438 /* Used for communication between the following two functions: contains
439 a MEM that we wish to ensure remains unchanged. */
440 static rtx equiv_mem;
442 /* Set nonzero if EQUIV_MEM is modified. */
443 static int equiv_mem_modified;
445 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
446 Called via note_stores. */
448 static void
449 validate_equiv_mem_from_store (rtx dest, rtx set ATTRIBUTE_UNUSED,
450 void *data ATTRIBUTE_UNUSED)
452 if ((GET_CODE (dest) == REG
453 && reg_overlap_mentioned_p (dest, equiv_mem))
454 || (GET_CODE (dest) == MEM
455 && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
456 equiv_mem_modified = 1;
459 /* Verify that no store between START and the death of REG invalidates
460 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
461 by storing into an overlapping memory location, or with a non-const
462 CALL_INSN.
464 Return 1 if MEMREF remains valid. */
466 static int
467 validate_equiv_mem (rtx start, rtx reg, rtx memref)
469 rtx insn;
470 rtx note;
472 equiv_mem = memref;
473 equiv_mem_modified = 0;
475 /* If the memory reference has side effects or is volatile, it isn't a
476 valid equivalence. */
477 if (side_effects_p (memref))
478 return 0;
480 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
482 if (! INSN_P (insn))
483 continue;
485 if (find_reg_note (insn, REG_DEAD, reg))
486 return 1;
488 if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
489 && ! CONST_OR_PURE_CALL_P (insn))
490 return 0;
492 note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
494 /* If a register mentioned in MEMREF is modified via an
495 auto-increment, we lose the equivalence. Do the same if one
496 dies; although we could extend the life, it doesn't seem worth
497 the trouble. */
499 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
500 if ((REG_NOTE_KIND (note) == REG_INC
501 || REG_NOTE_KIND (note) == REG_DEAD)
502 && GET_CODE (XEXP (note, 0)) == REG
503 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
504 return 0;
507 return 0;
510 /* Returns zero if X is known to be invariant. */
512 static int
513 equiv_init_varies_p (rtx x)
515 RTX_CODE code = GET_CODE (x);
516 int i;
517 const char *fmt;
519 switch (code)
521 case MEM:
522 return ! RTX_UNCHANGING_P (x) || equiv_init_varies_p (XEXP (x, 0));
524 case QUEUED:
525 return 1;
527 case CONST:
528 case CONST_INT:
529 case CONST_DOUBLE:
530 case CONST_VECTOR:
531 case SYMBOL_REF:
532 case LABEL_REF:
533 return 0;
535 case REG:
536 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
538 case ASM_OPERANDS:
539 if (MEM_VOLATILE_P (x))
540 return 1;
542 /* FALLTHROUGH */
544 default:
545 break;
548 fmt = GET_RTX_FORMAT (code);
549 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
550 if (fmt[i] == 'e')
552 if (equiv_init_varies_p (XEXP (x, i)))
553 return 1;
555 else if (fmt[i] == 'E')
557 int j;
558 for (j = 0; j < XVECLEN (x, i); j++)
559 if (equiv_init_varies_p (XVECEXP (x, i, j)))
560 return 1;
563 return 0;
566 /* Returns nonzero if X (used to initialize register REGNO) is movable.
567 X is only movable if the registers it uses have equivalent initializations
568 which appear to be within the same loop (or in an inner loop) and movable
569 or if they are not candidates for local_alloc and don't vary. */
571 static int
572 equiv_init_movable_p (rtx x, int regno)
574 int i, j;
575 const char *fmt;
576 enum rtx_code code = GET_CODE (x);
578 switch (code)
580 case SET:
581 return equiv_init_movable_p (SET_SRC (x), regno);
583 case CC0:
584 case CLOBBER:
585 return 0;
587 case PRE_INC:
588 case PRE_DEC:
589 case POST_INC:
590 case POST_DEC:
591 case PRE_MODIFY:
592 case POST_MODIFY:
593 return 0;
595 case REG:
596 return (reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
597 && reg_equiv[REGNO (x)].replace)
598 || (REG_BASIC_BLOCK (REGNO (x)) < 0 && ! rtx_varies_p (x, 0));
600 case UNSPEC_VOLATILE:
601 return 0;
603 case ASM_OPERANDS:
604 if (MEM_VOLATILE_P (x))
605 return 0;
607 /* FALLTHROUGH */
609 default:
610 break;
613 fmt = GET_RTX_FORMAT (code);
614 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
615 switch (fmt[i])
617 case 'e':
618 if (! equiv_init_movable_p (XEXP (x, i), regno))
619 return 0;
620 break;
621 case 'E':
622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
623 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
624 return 0;
625 break;
628 return 1;
631 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is true. */
633 static int
634 contains_replace_regs (rtx x)
636 int i, j;
637 const char *fmt;
638 enum rtx_code code = GET_CODE (x);
640 switch (code)
642 case CONST_INT:
643 case CONST:
644 case LABEL_REF:
645 case SYMBOL_REF:
646 case CONST_DOUBLE:
647 case CONST_VECTOR:
648 case PC:
649 case CC0:
650 case HIGH:
651 return 0;
653 case REG:
654 return reg_equiv[REGNO (x)].replace;
656 default:
657 break;
660 fmt = GET_RTX_FORMAT (code);
661 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
662 switch (fmt[i])
664 case 'e':
665 if (contains_replace_regs (XEXP (x, i)))
666 return 1;
667 break;
668 case 'E':
669 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
670 if (contains_replace_regs (XVECEXP (x, i, j)))
671 return 1;
672 break;
675 return 0;
678 /* TRUE if X references a memory location that would be affected by a store
679 to MEMREF. */
681 static int
682 memref_referenced_p (rtx memref, rtx x)
684 int i, j;
685 const char *fmt;
686 enum rtx_code code = GET_CODE (x);
688 switch (code)
690 case CONST_INT:
691 case CONST:
692 case LABEL_REF:
693 case SYMBOL_REF:
694 case CONST_DOUBLE:
695 case CONST_VECTOR:
696 case PC:
697 case CC0:
698 case HIGH:
699 case LO_SUM:
700 return 0;
702 case REG:
703 return (reg_equiv[REGNO (x)].replacement
704 && memref_referenced_p (memref,
705 reg_equiv[REGNO (x)].replacement));
707 case MEM:
708 if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
709 return 1;
710 break;
712 case SET:
713 /* If we are setting a MEM, it doesn't count (its address does), but any
714 other SET_DEST that has a MEM in it is referencing the MEM. */
715 if (GET_CODE (SET_DEST (x)) == MEM)
717 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
718 return 1;
720 else if (memref_referenced_p (memref, SET_DEST (x)))
721 return 1;
723 return memref_referenced_p (memref, SET_SRC (x));
725 default:
726 break;
729 fmt = GET_RTX_FORMAT (code);
730 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
731 switch (fmt[i])
733 case 'e':
734 if (memref_referenced_p (memref, XEXP (x, i)))
735 return 1;
736 break;
737 case 'E':
738 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
739 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
740 return 1;
741 break;
744 return 0;
747 /* TRUE if some insn in the range (START, END] references a memory location
748 that would be affected by a store to MEMREF. */
750 static int
751 memref_used_between_p (rtx memref, rtx start, rtx end)
753 rtx insn;
755 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
756 insn = NEXT_INSN (insn))
757 if (INSN_P (insn) && memref_referenced_p (memref, PATTERN (insn)))
758 return 1;
760 return 0;
763 /* Return nonzero if the rtx X is invariant over the current function. */
764 /* ??? Actually, the places this is used in reload expect exactly what
765 is tested here, and not everything that is function invariant. In
766 particular, the frame pointer and arg pointer are special cased;
767 pic_offset_table_rtx is not, and this will cause aborts when we
768 go to spill these things to memory. */
771 function_invariant_p (rtx x)
773 if (CONSTANT_P (x))
774 return 1;
775 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
776 return 1;
777 if (GET_CODE (x) == PLUS
778 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
779 && CONSTANT_P (XEXP (x, 1)))
780 return 1;
781 return 0;
784 /* Find registers that are equivalent to a single value throughout the
785 compilation (either because they can be referenced in memory or are set once
786 from a single constant). Lower their priority for a register.
788 If such a register is only referenced once, try substituting its value
789 into the using insn. If it succeeds, we can eliminate the register
790 completely. */
792 static void
793 update_equiv_regs (void)
795 rtx insn;
796 basic_block bb;
797 int loop_depth;
798 regset_head cleared_regs;
799 int clear_regnos = 0;
801 reg_equiv = (struct equivalence *) xcalloc (max_regno, sizeof *reg_equiv);
802 INIT_REG_SET (&cleared_regs);
804 init_alias_analysis ();
806 /* Scan the insns and find which registers have equivalences. Do this
807 in a separate scan of the insns because (due to -fcse-follow-jumps)
808 a register can be set below its use. */
809 FOR_EACH_BB (bb)
811 loop_depth = bb->loop_depth;
813 for (insn = bb->head; insn != NEXT_INSN (bb->end); insn = NEXT_INSN (insn))
815 rtx note;
816 rtx set;
817 rtx dest, src;
818 int regno;
820 if (! INSN_P (insn))
821 continue;
823 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
824 if (REG_NOTE_KIND (note) == REG_INC)
825 no_equiv (XEXP (note, 0), note, NULL);
827 set = single_set (insn);
829 /* If this insn contains more (or less) than a single SET,
830 only mark all destinations as having no known equivalence. */
831 if (set == 0)
833 note_stores (PATTERN (insn), no_equiv, NULL);
834 continue;
836 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
838 int i;
840 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
842 rtx part = XVECEXP (PATTERN (insn), 0, i);
843 if (part != set)
844 note_stores (part, no_equiv, NULL);
848 dest = SET_DEST (set);
849 src = SET_SRC (set);
851 /* If this sets a MEM to the contents of a REG that is only used
852 in a single basic block, see if the register is always equivalent
853 to that memory location and if moving the store from INSN to the
854 insn that set REG is safe. If so, put a REG_EQUIV note on the
855 initializing insn.
857 Don't add a REG_EQUIV note if the insn already has one. The existing
858 REG_EQUIV is likely more useful than the one we are adding.
860 If one of the regs in the address has reg_equiv[REGNO].replace set,
861 then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
862 optimization may move the set of this register immediately before
863 insn, which puts it after reg_equiv[REGNO].init_insns, and hence
864 the mention in the REG_EQUIV note would be to an uninitialized
865 pseudo. */
866 /* ????? This test isn't good enough; we might see a MEM with a use of
867 a pseudo register before we see its setting insn that will cause
868 reg_equiv[].replace for that pseudo to be set.
869 Equivalences to MEMs should be made in another pass, after the
870 reg_equiv[].replace information has been gathered. */
872 if (GET_CODE (dest) == MEM && GET_CODE (src) == REG
873 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
874 && REG_BASIC_BLOCK (regno) >= 0
875 && REG_N_SETS (regno) == 1
876 && reg_equiv[regno].init_insns != 0
877 && reg_equiv[regno].init_insns != const0_rtx
878 && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
879 REG_EQUIV, NULL_RTX)
880 && ! contains_replace_regs (XEXP (dest, 0)))
882 rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
883 if (validate_equiv_mem (init_insn, src, dest)
884 && ! memref_used_between_p (dest, init_insn, insn))
885 REG_NOTES (init_insn)
886 = gen_rtx_EXPR_LIST (REG_EQUIV, dest, REG_NOTES (init_insn));
889 /* We only handle the case of a pseudo register being set
890 once, or always to the same value. */
891 /* ??? The mn10200 port breaks if we add equivalences for
892 values that need an ADDRESS_REGS register and set them equivalent
893 to a MEM of a pseudo. The actual problem is in the over-conservative
894 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
895 calculate_needs, but we traditionally work around this problem
896 here by rejecting equivalences when the destination is in a register
897 that's likely spilled. This is fragile, of course, since the
898 preferred class of a pseudo depends on all instructions that set
899 or use it. */
901 if (GET_CODE (dest) != REG
902 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
903 || reg_equiv[regno].init_insns == const0_rtx
904 || (CLASS_LIKELY_SPILLED_P (reg_preferred_class (regno))
905 && GET_CODE (src) == MEM))
907 /* This might be setting a SUBREG of a pseudo, a pseudo that is
908 also set somewhere else to a constant. */
909 note_stores (set, no_equiv, NULL);
910 continue;
913 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
915 /* cse sometimes generates function invariants, but doesn't put a
916 REG_EQUAL note on the insn. Since this note would be redundant,
917 there's no point creating it earlier than here. */
918 if (! note && ! rtx_varies_p (src, 0))
919 note = set_unique_reg_note (insn, REG_EQUAL, src);
921 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
922 since it represents a function call */
923 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
924 note = NULL_RTX;
926 if (REG_N_SETS (regno) != 1
927 && (! note
928 || rtx_varies_p (XEXP (note, 0), 0)
929 || (reg_equiv[regno].replacement
930 && ! rtx_equal_p (XEXP (note, 0),
931 reg_equiv[regno].replacement))))
933 no_equiv (dest, set, NULL);
934 continue;
936 /* Record this insn as initializing this register. */
937 reg_equiv[regno].init_insns
938 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
940 /* If this register is known to be equal to a constant, record that
941 it is always equivalent to the constant. */
942 if (note && ! rtx_varies_p (XEXP (note, 0), 0))
943 PUT_MODE (note, (enum machine_mode) REG_EQUIV);
945 /* If this insn introduces a "constant" register, decrease the priority
946 of that register. Record this insn if the register is only used once
947 more and the equivalence value is the same as our source.
949 The latter condition is checked for two reasons: First, it is an
950 indication that it may be more efficient to actually emit the insn
951 as written (if no registers are available, reload will substitute
952 the equivalence). Secondly, it avoids problems with any registers
953 dying in this insn whose death notes would be missed.
955 If we don't have a REG_EQUIV note, see if this insn is loading
956 a register used only in one basic block from a MEM. If so, and the
957 MEM remains unchanged for the life of the register, add a REG_EQUIV
958 note. */
960 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
962 if (note == 0 && REG_BASIC_BLOCK (regno) >= 0
963 && GET_CODE (SET_SRC (set)) == MEM
964 && validate_equiv_mem (insn, dest, SET_SRC (set)))
965 REG_NOTES (insn) = note = gen_rtx_EXPR_LIST (REG_EQUIV, SET_SRC (set),
966 REG_NOTES (insn));
968 if (note)
970 int regno = REGNO (dest);
972 /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
973 We might end up substituting the LABEL_REF for uses of the
974 pseudo here or later. That kind of transformation may turn an
975 indirect jump into a direct jump, in which case we must rerun the
976 jump optimizer to ensure that the JUMP_LABEL fields are valid. */
977 if (GET_CODE (XEXP (note, 0)) == LABEL_REF
978 || (GET_CODE (XEXP (note, 0)) == CONST
979 && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
980 && (GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0))
981 == LABEL_REF)))
982 recorded_label_ref = 1;
984 reg_equiv[regno].replacement = XEXP (note, 0);
985 reg_equiv[regno].src_p = &SET_SRC (set);
986 reg_equiv[regno].loop_depth = loop_depth;
988 /* Don't mess with things live during setjmp. */
989 if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
991 /* Note that the statement below does not affect the priority
992 in local-alloc! */
993 REG_LIVE_LENGTH (regno) *= 2;
996 /* If the register is referenced exactly twice, meaning it is
997 set once and used once, indicate that the reference may be
998 replaced by the equivalence we computed above. Do this
999 even if the register is only used in one block so that
1000 dependencies can be handled where the last register is
1001 used in a different block (i.e. HIGH / LO_SUM sequences)
1002 and to reduce the number of registers alive across
1003 calls. */
1005 if (REG_N_REFS (regno) == 2
1006 && (rtx_equal_p (XEXP (note, 0), src)
1007 || ! equiv_init_varies_p (src))
1008 && GET_CODE (insn) == INSN
1009 && equiv_init_movable_p (PATTERN (insn), regno))
1010 reg_equiv[regno].replace = 1;
1016 /* Now scan all regs killed in an insn to see if any of them are
1017 registers only used that once. If so, see if we can replace the
1018 reference with the equivalent from. If we can, delete the
1019 initializing reference and this register will go away. If we
1020 can't replace the reference, and the initializing reference is
1021 within the same loop (or in an inner loop), then move the register
1022 initialization just before the use, so that they are in the same
1023 basic block. */
1024 FOR_EACH_BB_REVERSE (bb)
1026 loop_depth = bb->loop_depth;
1027 for (insn = bb->end; insn != PREV_INSN (bb->head); insn = PREV_INSN (insn))
1029 rtx link;
1031 if (! INSN_P (insn))
1032 continue;
1034 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1036 if (REG_NOTE_KIND (link) == REG_DEAD
1037 /* Make sure this insn still refers to the register. */
1038 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1040 int regno = REGNO (XEXP (link, 0));
1041 rtx equiv_insn;
1043 if (! reg_equiv[regno].replace
1044 || reg_equiv[regno].loop_depth < loop_depth)
1045 continue;
1047 /* reg_equiv[REGNO].replace gets set only when
1048 REG_N_REFS[REGNO] is 2, i.e. the register is set
1049 once and used once. (If it were only set, but not used,
1050 flow would have deleted the setting insns.) Hence
1051 there can only be one insn in reg_equiv[REGNO].init_insns. */
1052 if (reg_equiv[regno].init_insns == NULL_RTX
1053 || XEXP (reg_equiv[regno].init_insns, 1) != NULL_RTX)
1054 abort ();
1055 equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
1057 /* We may not move instructions that can throw, since
1058 that changes basic block boundaries and we are not
1059 prepared to adjust the CFG to match. */
1060 if (can_throw_internal (equiv_insn))
1061 continue;
1063 if (asm_noperands (PATTERN (equiv_insn)) < 0
1064 && validate_replace_rtx (regno_reg_rtx[regno],
1065 *(reg_equiv[regno].src_p), insn))
1067 rtx equiv_link;
1068 rtx last_link;
1069 rtx note;
1071 /* Find the last note. */
1072 for (last_link = link; XEXP (last_link, 1);
1073 last_link = XEXP (last_link, 1))
1076 /* Append the REG_DEAD notes from equiv_insn. */
1077 equiv_link = REG_NOTES (equiv_insn);
1078 while (equiv_link)
1080 note = equiv_link;
1081 equiv_link = XEXP (equiv_link, 1);
1082 if (REG_NOTE_KIND (note) == REG_DEAD)
1084 remove_note (equiv_insn, note);
1085 XEXP (last_link, 1) = note;
1086 XEXP (note, 1) = NULL_RTX;
1087 last_link = note;
1091 remove_death (regno, insn);
1092 REG_N_REFS (regno) = 0;
1093 REG_FREQ (regno) = 0;
1094 delete_insn (equiv_insn);
1096 reg_equiv[regno].init_insns
1097 = XEXP (reg_equiv[regno].init_insns, 1);
1099 /* Move the initialization of the register to just before
1100 INSN. Update the flow information. */
1101 else if (PREV_INSN (insn) != equiv_insn)
1103 rtx new_insn;
1105 new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
1106 REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
1107 REG_NOTES (equiv_insn) = 0;
1109 /* Make sure this insn is recognized before reload begins,
1110 otherwise eliminate_regs_in_insn will abort. */
1111 INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
1113 delete_insn (equiv_insn);
1115 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
1117 REG_BASIC_BLOCK (regno) = bb->index;
1118 REG_N_CALLS_CROSSED (regno) = 0;
1119 REG_LIVE_LENGTH (regno) = 2;
1121 if (insn == bb->head)
1122 bb->head = PREV_INSN (insn);
1124 /* Remember to clear REGNO from all basic block's live
1125 info. */
1126 SET_REGNO_REG_SET (&cleared_regs, regno);
1127 clear_regnos++;
1134 /* Clear all dead REGNOs from all basic block's live info. */
1135 if (clear_regnos)
1137 int j;
1138 if (clear_regnos > 8)
1140 FOR_EACH_BB (bb)
1142 AND_COMPL_REG_SET (bb->global_live_at_start, &cleared_regs);
1143 AND_COMPL_REG_SET (bb->global_live_at_end, &cleared_regs);
1146 else
1147 EXECUTE_IF_SET_IN_REG_SET (&cleared_regs, 0, j,
1149 FOR_EACH_BB (bb)
1151 CLEAR_REGNO_REG_SET (bb->global_live_at_start, j);
1152 CLEAR_REGNO_REG_SET (bb->global_live_at_end, j);
1157 /* Clean up. */
1158 end_alias_analysis ();
1159 CLEAR_REG_SET (&cleared_regs);
1160 free (reg_equiv);
1163 /* Mark REG as having no known equivalence.
1164 Some instructions might have been processed before and furnished
1165 with REG_EQUIV notes for this register; these notes will have to be
1166 removed.
1167 STORE is the piece of RTL that does the non-constant / conflicting
1168 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
1169 but needs to be there because this function is called from note_stores. */
1170 static void
1171 no_equiv (rtx reg, rtx store ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
1173 int regno;
1174 rtx list;
1176 if (GET_CODE (reg) != REG)
1177 return;
1178 regno = REGNO (reg);
1179 list = reg_equiv[regno].init_insns;
1180 if (list == const0_rtx)
1181 return;
1182 for (; list; list = XEXP (list, 1))
1184 rtx insn = XEXP (list, 0);
1185 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
1187 reg_equiv[regno].init_insns = const0_rtx;
1188 reg_equiv[regno].replacement = NULL_RTX;
1191 /* Allocate hard regs to the pseudo regs used only within block number B.
1192 Only the pseudos that die but once can be handled. */
1194 static void
1195 block_alloc (int b)
1197 int i, q;
1198 rtx insn;
1199 rtx note, hard_reg;
1200 int insn_number = 0;
1201 int insn_count = 0;
1202 int max_uid = get_max_uid ();
1203 int *qty_order;
1204 int no_conflict_combined_regno = -1;
1206 /* Count the instructions in the basic block. */
1208 insn = BLOCK_END (b);
1209 while (1)
1211 if (GET_CODE (insn) != NOTE)
1212 if (++insn_count > max_uid)
1213 abort ();
1214 if (insn == BLOCK_HEAD (b))
1215 break;
1216 insn = PREV_INSN (insn);
1219 /* +2 to leave room for a post_mark_life at the last insn and for
1220 the birth of a CLOBBER in the first insn. */
1221 regs_live_at = (HARD_REG_SET *) xcalloc ((2 * insn_count + 2),
1222 sizeof (HARD_REG_SET));
1224 /* Initialize table of hardware registers currently live. */
1226 REG_SET_TO_HARD_REG_SET (regs_live, BASIC_BLOCK (b)->global_live_at_start);
1228 /* This loop scans the instructions of the basic block
1229 and assigns quantities to registers.
1230 It computes which registers to tie. */
1232 insn = BLOCK_HEAD (b);
1233 while (1)
1235 if (GET_CODE (insn) != NOTE)
1236 insn_number++;
1238 if (INSN_P (insn))
1240 rtx link, set;
1241 int win = 0;
1242 rtx r0, r1 = NULL_RTX;
1243 int combined_regno = -1;
1244 int i;
1246 this_insn_number = insn_number;
1247 this_insn = insn;
1249 extract_insn (insn);
1250 which_alternative = -1;
1252 /* Is this insn suitable for tying two registers?
1253 If so, try doing that.
1254 Suitable insns are those with at least two operands and where
1255 operand 0 is an output that is a register that is not
1256 earlyclobber.
1258 We can tie operand 0 with some operand that dies in this insn.
1259 First look for operands that are required to be in the same
1260 register as operand 0. If we find such, only try tying that
1261 operand or one that can be put into that operand if the
1262 operation is commutative. If we don't find an operand
1263 that is required to be in the same register as operand 0,
1264 we can tie with any operand.
1266 Subregs in place of regs are also ok.
1268 If tying is done, WIN is set nonzero. */
1270 if (optimize
1271 && recog_data.n_operands > 1
1272 && recog_data.constraints[0][0] == '='
1273 && recog_data.constraints[0][1] != '&')
1275 /* If non-negative, is an operand that must match operand 0. */
1276 int must_match_0 = -1;
1277 /* Counts number of alternatives that require a match with
1278 operand 0. */
1279 int n_matching_alts = 0;
1281 for (i = 1; i < recog_data.n_operands; i++)
1283 const char *p = recog_data.constraints[i];
1284 int this_match = requires_inout (p);
1286 n_matching_alts += this_match;
1287 if (this_match == recog_data.n_alternatives)
1288 must_match_0 = i;
1291 r0 = recog_data.operand[0];
1292 for (i = 1; i < recog_data.n_operands; i++)
1294 /* Skip this operand if we found an operand that
1295 must match operand 0 and this operand isn't it
1296 and can't be made to be it by commutativity. */
1298 if (must_match_0 >= 0 && i != must_match_0
1299 && ! (i == must_match_0 + 1
1300 && recog_data.constraints[i-1][0] == '%')
1301 && ! (i == must_match_0 - 1
1302 && recog_data.constraints[i][0] == '%'))
1303 continue;
1305 /* Likewise if each alternative has some operand that
1306 must match operand zero. In that case, skip any
1307 operand that doesn't list operand 0 since we know that
1308 the operand always conflicts with operand 0. We
1309 ignore commutativity in this case to keep things simple. */
1310 if (n_matching_alts == recog_data.n_alternatives
1311 && 0 == requires_inout (recog_data.constraints[i]))
1312 continue;
1314 r1 = recog_data.operand[i];
1316 /* If the operand is an address, find a register in it.
1317 There may be more than one register, but we only try one
1318 of them. */
1319 if (recog_data.constraints[i][0] == 'p'
1320 || EXTRA_ADDRESS_CONSTRAINT (recog_data.constraints[i][0],
1321 recog_data.constraints[i]))
1322 while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1323 r1 = XEXP (r1, 0);
1325 /* Avoid making a call-saved register unnecessarily
1326 clobbered. */
1327 hard_reg = get_hard_reg_initial_reg (cfun, r1);
1328 if (hard_reg != NULL_RTX)
1330 if (GET_CODE (hard_reg) == REG
1331 && IN_RANGE (REGNO (hard_reg),
1332 0, FIRST_PSEUDO_REGISTER - 1)
1333 && ! call_used_regs[REGNO (hard_reg)])
1334 continue;
1337 if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1339 /* We have two priorities for hard register preferences.
1340 If we have a move insn or an insn whose first input
1341 can only be in the same register as the output, give
1342 priority to an equivalence found from that insn. */
1343 int may_save_copy
1344 = (r1 == recog_data.operand[i] && must_match_0 >= 0);
1346 if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1347 win = combine_regs (r1, r0, may_save_copy,
1348 insn_number, insn, 0);
1350 if (win)
1351 break;
1355 /* Recognize an insn sequence with an ultimate result
1356 which can safely overlap one of the inputs.
1357 The sequence begins with a CLOBBER of its result,
1358 and ends with an insn that copies the result to itself
1359 and has a REG_EQUAL note for an equivalent formula.
1360 That note indicates what the inputs are.
1361 The result and the input can overlap if each insn in
1362 the sequence either doesn't mention the input
1363 or has a REG_NO_CONFLICT note to inhibit the conflict.
1365 We do the combining test at the CLOBBER so that the
1366 destination register won't have had a quantity number
1367 assigned, since that would prevent combining. */
1369 if (optimize
1370 && GET_CODE (PATTERN (insn)) == CLOBBER
1371 && (r0 = XEXP (PATTERN (insn), 0),
1372 GET_CODE (r0) == REG)
1373 && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1374 && XEXP (link, 0) != 0
1375 && GET_CODE (XEXP (link, 0)) == INSN
1376 && (set = single_set (XEXP (link, 0))) != 0
1377 && SET_DEST (set) == r0 && SET_SRC (set) == r0
1378 && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1379 NULL_RTX)) != 0)
1381 if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1382 /* Check that we have such a sequence. */
1383 && no_conflict_p (insn, r0, r1))
1384 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1385 else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1386 && (r1 = XEXP (XEXP (note, 0), 0),
1387 GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1388 && no_conflict_p (insn, r0, r1))
1389 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1391 /* Here we care if the operation to be computed is
1392 commutative. */
1393 else if ((GET_CODE (XEXP (note, 0)) == EQ
1394 || GET_CODE (XEXP (note, 0)) == NE
1395 || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1396 && (r1 = XEXP (XEXP (note, 0), 1),
1397 (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1398 && no_conflict_p (insn, r0, r1))
1399 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1401 /* If we did combine something, show the register number
1402 in question so that we know to ignore its death. */
1403 if (win)
1404 no_conflict_combined_regno = REGNO (r1);
1407 /* If registers were just tied, set COMBINED_REGNO
1408 to the number of the register used in this insn
1409 that was tied to the register set in this insn.
1410 This register's qty should not be "killed". */
1412 if (win)
1414 while (GET_CODE (r1) == SUBREG)
1415 r1 = SUBREG_REG (r1);
1416 combined_regno = REGNO (r1);
1419 /* Mark the death of everything that dies in this instruction,
1420 except for anything that was just combined. */
1422 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1423 if (REG_NOTE_KIND (link) == REG_DEAD
1424 && GET_CODE (XEXP (link, 0)) == REG
1425 && combined_regno != (int) REGNO (XEXP (link, 0))
1426 && (no_conflict_combined_regno != (int) REGNO (XEXP (link, 0))
1427 || ! find_reg_note (insn, REG_NO_CONFLICT,
1428 XEXP (link, 0))))
1429 wipe_dead_reg (XEXP (link, 0), 0);
1431 /* Allocate qty numbers for all registers local to this block
1432 that are born (set) in this instruction.
1433 A pseudo that already has a qty is not changed. */
1435 note_stores (PATTERN (insn), reg_is_set, NULL);
1437 /* If anything is set in this insn and then unused, mark it as dying
1438 after this insn, so it will conflict with our outputs. This
1439 can't match with something that combined, and it doesn't matter
1440 if it did. Do this after the calls to reg_is_set since these
1441 die after, not during, the current insn. */
1443 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1444 if (REG_NOTE_KIND (link) == REG_UNUSED
1445 && GET_CODE (XEXP (link, 0)) == REG)
1446 wipe_dead_reg (XEXP (link, 0), 1);
1448 /* If this is an insn that has a REG_RETVAL note pointing at a
1449 CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1450 block, so clear any register number that combined within it. */
1451 if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1452 && GET_CODE (XEXP (note, 0)) == INSN
1453 && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1454 no_conflict_combined_regno = -1;
1457 /* Set the registers live after INSN_NUMBER. Note that we never
1458 record the registers live before the block's first insn, since no
1459 pseudos we care about are live before that insn. */
1461 IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1462 IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1464 if (insn == BLOCK_END (b))
1465 break;
1467 insn = NEXT_INSN (insn);
1470 /* Now every register that is local to this basic block
1471 should have been given a quantity, or else -1 meaning ignore it.
1472 Every quantity should have a known birth and death.
1474 Order the qtys so we assign them registers in order of the
1475 number of suggested registers they need so we allocate those with
1476 the most restrictive needs first. */
1478 qty_order = (int *) xmalloc (next_qty * sizeof (int));
1479 for (i = 0; i < next_qty; i++)
1480 qty_order[i] = i;
1482 #define EXCHANGE(I1, I2) \
1483 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1485 switch (next_qty)
1487 case 3:
1488 /* Make qty_order[2] be the one to allocate last. */
1489 if (qty_sugg_compare (0, 1) > 0)
1490 EXCHANGE (0, 1);
1491 if (qty_sugg_compare (1, 2) > 0)
1492 EXCHANGE (2, 1);
1494 /* ... Fall through ... */
1495 case 2:
1496 /* Put the best one to allocate in qty_order[0]. */
1497 if (qty_sugg_compare (0, 1) > 0)
1498 EXCHANGE (0, 1);
1500 /* ... Fall through ... */
1502 case 1:
1503 case 0:
1504 /* Nothing to do here. */
1505 break;
1507 default:
1508 qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1511 /* Try to put each quantity in a suggested physical register, if it has one.
1512 This may cause registers to be allocated that otherwise wouldn't be, but
1513 this seems acceptable in local allocation (unlike global allocation). */
1514 for (i = 0; i < next_qty; i++)
1516 q = qty_order[i];
1517 if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1518 qty[q].phys_reg = find_free_reg (qty[q].min_class, qty[q].mode, q,
1519 0, 1, qty[q].birth, qty[q].death);
1520 else
1521 qty[q].phys_reg = -1;
1524 /* Order the qtys so we assign them registers in order of
1525 decreasing length of life. Normally call qsort, but if we
1526 have only a very small number of quantities, sort them ourselves. */
1528 for (i = 0; i < next_qty; i++)
1529 qty_order[i] = i;
1531 #define EXCHANGE(I1, I2) \
1532 { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1534 switch (next_qty)
1536 case 3:
1537 /* Make qty_order[2] be the one to allocate last. */
1538 if (qty_compare (0, 1) > 0)
1539 EXCHANGE (0, 1);
1540 if (qty_compare (1, 2) > 0)
1541 EXCHANGE (2, 1);
1543 /* ... Fall through ... */
1544 case 2:
1545 /* Put the best one to allocate in qty_order[0]. */
1546 if (qty_compare (0, 1) > 0)
1547 EXCHANGE (0, 1);
1549 /* ... Fall through ... */
1551 case 1:
1552 case 0:
1553 /* Nothing to do here. */
1554 break;
1556 default:
1557 qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1560 /* Now for each qty that is not a hardware register,
1561 look for a hardware register to put it in.
1562 First try the register class that is cheapest for this qty,
1563 if there is more than one class. */
1565 for (i = 0; i < next_qty; i++)
1567 q = qty_order[i];
1568 if (qty[q].phys_reg < 0)
1570 #ifdef INSN_SCHEDULING
1571 /* These values represent the adjusted lifetime of a qty so
1572 that it conflicts with qtys which appear near the start/end
1573 of this qty's lifetime.
1575 The purpose behind extending the lifetime of this qty is to
1576 discourage the register allocator from creating false
1577 dependencies.
1579 The adjustment value is chosen to indicate that this qty
1580 conflicts with all the qtys in the instructions immediately
1581 before and after the lifetime of this qty.
1583 Experiments have shown that higher values tend to hurt
1584 overall code performance.
1586 If allocation using the extended lifetime fails we will try
1587 again with the qty's unadjusted lifetime. */
1588 int fake_birth = MAX (0, qty[q].birth - 2 + qty[q].birth % 2);
1589 int fake_death = MIN (insn_number * 2 + 1,
1590 qty[q].death + 2 - qty[q].death % 2);
1591 #endif
1593 if (N_REG_CLASSES > 1)
1595 #ifdef INSN_SCHEDULING
1596 /* We try to avoid using hard registers allocated to qtys which
1597 are born immediately after this qty or die immediately before
1598 this qty.
1600 This optimization is only appropriate when we will run
1601 a scheduling pass after reload and we are not optimizing
1602 for code size. */
1603 if (flag_schedule_insns_after_reload
1604 && !optimize_size
1605 && !SMALL_REGISTER_CLASSES)
1607 qty[q].phys_reg = find_free_reg (qty[q].min_class,
1608 qty[q].mode, q, 0, 0,
1609 fake_birth, fake_death);
1610 if (qty[q].phys_reg >= 0)
1611 continue;
1613 #endif
1614 qty[q].phys_reg = find_free_reg (qty[q].min_class,
1615 qty[q].mode, q, 0, 0,
1616 qty[q].birth, qty[q].death);
1617 if (qty[q].phys_reg >= 0)
1618 continue;
1621 #ifdef INSN_SCHEDULING
1622 /* Similarly, avoid false dependencies. */
1623 if (flag_schedule_insns_after_reload
1624 && !optimize_size
1625 && !SMALL_REGISTER_CLASSES
1626 && qty[q].alternate_class != NO_REGS)
1627 qty[q].phys_reg = find_free_reg (qty[q].alternate_class,
1628 qty[q].mode, q, 0, 0,
1629 fake_birth, fake_death);
1630 #endif
1631 if (qty[q].alternate_class != NO_REGS)
1632 qty[q].phys_reg = find_free_reg (qty[q].alternate_class,
1633 qty[q].mode, q, 0, 0,
1634 qty[q].birth, qty[q].death);
1638 /* Now propagate the register assignments
1639 to the pseudo regs belonging to the qtys. */
1641 for (q = 0; q < next_qty; q++)
1642 if (qty[q].phys_reg >= 0)
1644 for (i = qty[q].first_reg; i >= 0; i = reg_next_in_qty[i])
1645 reg_renumber[i] = qty[q].phys_reg + reg_offset[i];
1648 /* Clean up. */
1649 free (regs_live_at);
1650 free (qty_order);
1653 /* Compare two quantities' priority for getting real registers.
1654 We give shorter-lived quantities higher priority.
1655 Quantities with more references are also preferred, as are quantities that
1656 require multiple registers. This is the identical prioritization as
1657 done by global-alloc.
1659 We used to give preference to registers with *longer* lives, but using
1660 the same algorithm in both local- and global-alloc can speed up execution
1661 of some programs by as much as a factor of three! */
1663 /* Note that the quotient will never be bigger than
1664 the value of floor_log2 times the maximum number of
1665 times a register can occur in one insn (surely less than 100)
1666 weighted by frequency (max REG_FREQ_MAX).
1667 Multiplying this by 10000/REG_FREQ_MAX can't overflow.
1668 QTY_CMP_PRI is also used by qty_sugg_compare. */
1670 #define QTY_CMP_PRI(q) \
1671 ((int) (((double) (floor_log2 (qty[q].n_refs) * qty[q].freq * qty[q].size) \
1672 / (qty[q].death - qty[q].birth)) * (10000 / REG_FREQ_MAX)))
1674 static int
1675 qty_compare (int q1, int q2)
1677 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1680 static int
1681 qty_compare_1 (const void *q1p, const void *q2p)
1683 int q1 = *(const int *) q1p, q2 = *(const int *) q2p;
1684 int tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1686 if (tem != 0)
1687 return tem;
1689 /* If qtys are equally good, sort by qty number,
1690 so that the results of qsort leave nothing to chance. */
1691 return q1 - q2;
1694 /* Compare two quantities' priority for getting real registers. This version
1695 is called for quantities that have suggested hard registers. First priority
1696 goes to quantities that have copy preferences, then to those that have
1697 normal preferences. Within those groups, quantities with the lower
1698 number of preferences have the highest priority. Of those, we use the same
1699 algorithm as above. */
1701 #define QTY_CMP_SUGG(q) \
1702 (qty_phys_num_copy_sugg[q] \
1703 ? qty_phys_num_copy_sugg[q] \
1704 : qty_phys_num_sugg[q] * FIRST_PSEUDO_REGISTER)
1706 static int
1707 qty_sugg_compare (int q1, int q2)
1709 int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1711 if (tem != 0)
1712 return tem;
1714 return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1717 static int
1718 qty_sugg_compare_1 (const void *q1p, const void *q2p)
1720 int q1 = *(const int *) q1p, q2 = *(const int *) q2p;
1721 int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1723 if (tem != 0)
1724 return tem;
1726 tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1727 if (tem != 0)
1728 return tem;
1730 /* If qtys are equally good, sort by qty number,
1731 so that the results of qsort leave nothing to chance. */
1732 return q1 - q2;
1735 #undef QTY_CMP_SUGG
1736 #undef QTY_CMP_PRI
1738 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1739 Returns 1 if have done so, or 0 if cannot.
1741 Combining registers means marking them as having the same quantity
1742 and adjusting the offsets within the quantity if either of
1743 them is a SUBREG).
1745 We don't actually combine a hard reg with a pseudo; instead
1746 we just record the hard reg as the suggestion for the pseudo's quantity.
1747 If we really combined them, we could lose if the pseudo lives
1748 across an insn that clobbers the hard reg (eg, movstr).
1750 ALREADY_DEAD is nonzero if USEDREG is known to be dead even though
1751 there is no REG_DEAD note on INSN. This occurs during the processing
1752 of REG_NO_CONFLICT blocks.
1754 MAY_SAVE_COPYCOPY is nonzero if this insn is simply copying USEDREG to
1755 SETREG or if the input and output must share a register.
1756 In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1758 There are elaborate checks for the validity of combining. */
1760 static int
1761 combine_regs (rtx usedreg, rtx setreg, int may_save_copy, int insn_number,
1762 rtx insn, int already_dead)
1764 int ureg, sreg;
1765 int offset = 0;
1766 int usize, ssize;
1767 int sqty;
1769 /* Determine the numbers and sizes of registers being used. If a subreg
1770 is present that does not change the entire register, don't consider
1771 this a copy insn. */
1773 while (GET_CODE (usedreg) == SUBREG)
1775 rtx subreg = SUBREG_REG (usedreg);
1777 if (GET_CODE (subreg) == REG)
1779 if (GET_MODE_SIZE (GET_MODE (subreg)) > UNITS_PER_WORD)
1780 may_save_copy = 0;
1782 if (REGNO (subreg) < FIRST_PSEUDO_REGISTER)
1783 offset += subreg_regno_offset (REGNO (subreg),
1784 GET_MODE (subreg),
1785 SUBREG_BYTE (usedreg),
1786 GET_MODE (usedreg));
1787 else
1788 offset += (SUBREG_BYTE (usedreg)
1789 / REGMODE_NATURAL_SIZE (GET_MODE (usedreg)));
1792 usedreg = subreg;
1795 if (GET_CODE (usedreg) != REG)
1796 return 0;
1798 ureg = REGNO (usedreg);
1799 if (ureg < FIRST_PSEUDO_REGISTER)
1800 usize = HARD_REGNO_NREGS (ureg, GET_MODE (usedreg));
1801 else
1802 usize = ((GET_MODE_SIZE (GET_MODE (usedreg))
1803 + (REGMODE_NATURAL_SIZE (GET_MODE (usedreg)) - 1))
1804 / REGMODE_NATURAL_SIZE (GET_MODE (usedreg)));
1806 while (GET_CODE (setreg) == SUBREG)
1808 rtx subreg = SUBREG_REG (setreg);
1810 if (GET_CODE (subreg) == REG)
1812 if (GET_MODE_SIZE (GET_MODE (subreg)) > UNITS_PER_WORD)
1813 may_save_copy = 0;
1815 if (REGNO (subreg) < FIRST_PSEUDO_REGISTER)
1816 offset -= subreg_regno_offset (REGNO (subreg),
1817 GET_MODE (subreg),
1818 SUBREG_BYTE (setreg),
1819 GET_MODE (setreg));
1820 else
1821 offset -= (SUBREG_BYTE (setreg)
1822 / REGMODE_NATURAL_SIZE (GET_MODE (setreg)));
1825 setreg = subreg;
1828 if (GET_CODE (setreg) != REG)
1829 return 0;
1831 sreg = REGNO (setreg);
1832 if (sreg < FIRST_PSEUDO_REGISTER)
1833 ssize = HARD_REGNO_NREGS (sreg, GET_MODE (setreg));
1834 else
1835 ssize = ((GET_MODE_SIZE (GET_MODE (setreg))
1836 + (REGMODE_NATURAL_SIZE (GET_MODE (setreg)) - 1))
1837 / REGMODE_NATURAL_SIZE (GET_MODE (setreg)));
1839 /* If UREG is a pseudo-register that hasn't already been assigned a
1840 quantity number, it means that it is not local to this block or dies
1841 more than once. In either event, we can't do anything with it. */
1842 if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1843 /* Do not combine registers unless one fits within the other. */
1844 || (offset > 0 && usize + offset > ssize)
1845 || (offset < 0 && usize + offset < ssize)
1846 /* Do not combine with a smaller already-assigned object
1847 if that smaller object is already combined with something bigger. */
1848 || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1849 && usize < qty[reg_qty[ureg]].size)
1850 /* Can't combine if SREG is not a register we can allocate. */
1851 || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1852 /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1853 These have already been taken care of. This probably wouldn't
1854 combine anyway, but don't take any chances. */
1855 || (ureg >= FIRST_PSEUDO_REGISTER
1856 && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1857 /* Don't tie something to itself. In most cases it would make no
1858 difference, but it would screw up if the reg being tied to itself
1859 also dies in this insn. */
1860 || ureg == sreg
1861 /* Don't try to connect two different hardware registers. */
1862 || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1863 /* Don't connect two different machine modes if they have different
1864 implications as to which registers may be used. */
1865 || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1866 return 0;
1868 /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1869 qty_phys_sugg for the pseudo instead of tying them.
1871 Return "failure" so that the lifespan of UREG is terminated here;
1872 that way the two lifespans will be disjoint and nothing will prevent
1873 the pseudo reg from being given this hard reg. */
1875 if (ureg < FIRST_PSEUDO_REGISTER)
1877 /* Allocate a quantity number so we have a place to put our
1878 suggestions. */
1879 if (reg_qty[sreg] == -2)
1880 reg_is_born (setreg, 2 * insn_number);
1882 if (reg_qty[sreg] >= 0)
1884 if (may_save_copy
1885 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1887 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1888 qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1890 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1892 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1893 qty_phys_num_sugg[reg_qty[sreg]]++;
1896 return 0;
1899 /* Similarly for SREG a hard register and UREG a pseudo register. */
1901 if (sreg < FIRST_PSEUDO_REGISTER)
1903 if (may_save_copy
1904 && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1906 SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1907 qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1909 else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1911 SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1912 qty_phys_num_sugg[reg_qty[ureg]]++;
1914 return 0;
1917 /* At this point we know that SREG and UREG are both pseudos.
1918 Do nothing if SREG already has a quantity or is a register that we
1919 don't allocate. */
1920 if (reg_qty[sreg] >= -1
1921 /* If we are not going to let any regs live across calls,
1922 don't tie a call-crossing reg to a non-call-crossing reg. */
1923 || (current_function_has_nonlocal_label
1924 && ((REG_N_CALLS_CROSSED (ureg) > 0)
1925 != (REG_N_CALLS_CROSSED (sreg) > 0))))
1926 return 0;
1928 /* We don't already know about SREG, so tie it to UREG
1929 if this is the last use of UREG, provided the classes they want
1930 are compatible. */
1932 if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1933 && reg_meets_class_p (sreg, qty[reg_qty[ureg]].min_class))
1935 /* Add SREG to UREG's quantity. */
1936 sqty = reg_qty[ureg];
1937 reg_qty[sreg] = sqty;
1938 reg_offset[sreg] = reg_offset[ureg] + offset;
1939 reg_next_in_qty[sreg] = qty[sqty].first_reg;
1940 qty[sqty].first_reg = sreg;
1942 /* If SREG's reg class is smaller, set qty[SQTY].min_class. */
1943 update_qty_class (sqty, sreg);
1945 /* Update info about quantity SQTY. */
1946 qty[sqty].n_calls_crossed += REG_N_CALLS_CROSSED (sreg);
1947 qty[sqty].n_refs += REG_N_REFS (sreg);
1948 qty[sqty].freq += REG_FREQ (sreg);
1949 if (usize < ssize)
1951 int i;
1953 for (i = qty[sqty].first_reg; i >= 0; i = reg_next_in_qty[i])
1954 reg_offset[i] -= offset;
1956 qty[sqty].size = ssize;
1957 qty[sqty].mode = GET_MODE (setreg);
1960 else
1961 return 0;
1963 return 1;
1966 /* Return 1 if the preferred class of REG allows it to be tied
1967 to a quantity or register whose class is CLASS.
1968 True if REG's reg class either contains or is contained in CLASS. */
1970 static int
1971 reg_meets_class_p (int reg, enum reg_class class)
1973 enum reg_class rclass = reg_preferred_class (reg);
1974 return (reg_class_subset_p (rclass, class)
1975 || reg_class_subset_p (class, rclass));
1978 /* Update the class of QTYNO assuming that REG is being tied to it. */
1980 static void
1981 update_qty_class (int qtyno, int reg)
1983 enum reg_class rclass = reg_preferred_class (reg);
1984 if (reg_class_subset_p (rclass, qty[qtyno].min_class))
1985 qty[qtyno].min_class = rclass;
1987 rclass = reg_alternate_class (reg);
1988 if (reg_class_subset_p (rclass, qty[qtyno].alternate_class))
1989 qty[qtyno].alternate_class = rclass;
1992 /* Handle something which alters the value of an rtx REG.
1994 REG is whatever is set or clobbered. SETTER is the rtx that
1995 is modifying the register.
1997 If it is not really a register, we do nothing.
1998 The file-global variables `this_insn' and `this_insn_number'
1999 carry info from `block_alloc'. */
2001 static void
2002 reg_is_set (rtx reg, rtx setter, void *data ATTRIBUTE_UNUSED)
2004 /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
2005 a hard register. These may actually not exist any more. */
2007 if (GET_CODE (reg) != SUBREG
2008 && GET_CODE (reg) != REG)
2009 return;
2011 /* Mark this register as being born. If it is used in a CLOBBER, mark
2012 it as being born halfway between the previous insn and this insn so that
2013 it conflicts with our inputs but not the outputs of the previous insn. */
2015 reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
2018 /* Handle beginning of the life of register REG.
2019 BIRTH is the index at which this is happening. */
2021 static void
2022 reg_is_born (rtx reg, int birth)
2024 int regno;
2026 if (GET_CODE (reg) == SUBREG)
2028 regno = REGNO (SUBREG_REG (reg));
2029 if (regno < FIRST_PSEUDO_REGISTER)
2030 regno = subreg_hard_regno (reg, 1);
2032 else
2033 regno = REGNO (reg);
2035 if (regno < FIRST_PSEUDO_REGISTER)
2037 mark_life (regno, GET_MODE (reg), 1);
2039 /* If the register was to have been born earlier that the present
2040 insn, mark it as live where it is actually born. */
2041 if (birth < 2 * this_insn_number)
2042 post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
2044 else
2046 if (reg_qty[regno] == -2)
2047 alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
2049 /* If this register has a quantity number, show that it isn't dead. */
2050 if (reg_qty[regno] >= 0)
2051 qty[reg_qty[regno]].death = -1;
2055 /* Record the death of REG in the current insn. If OUTPUT_P is nonzero,
2056 REG is an output that is dying (i.e., it is never used), otherwise it
2057 is an input (the normal case).
2058 If OUTPUT_P is 1, then we extend the life past the end of this insn. */
2060 static void
2061 wipe_dead_reg (rtx reg, int output_p)
2063 int regno = REGNO (reg);
2065 /* If this insn has multiple results,
2066 and the dead reg is used in one of the results,
2067 extend its life to after this insn,
2068 so it won't get allocated together with any other result of this insn.
2070 It is unsafe to use !single_set here since it will ignore an unused
2071 output. Just because an output is unused does not mean the compiler
2072 can assume the side effect will not occur. Consider if REG appears
2073 in the address of an output and we reload the output. If we allocate
2074 REG to the same hard register as an unused output we could set the hard
2075 register before the output reload insn. */
2076 if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2077 && multiple_sets (this_insn))
2079 int i;
2080 for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2082 rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2083 if (GET_CODE (set) == SET
2084 && GET_CODE (SET_DEST (set)) != REG
2085 && !rtx_equal_p (reg, SET_DEST (set))
2086 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2087 output_p = 1;
2091 /* If this register is used in an auto-increment address, then extend its
2092 life to after this insn, so that it won't get allocated together with
2093 the result of this insn. */
2094 if (! output_p && find_regno_note (this_insn, REG_INC, regno))
2095 output_p = 1;
2097 if (regno < FIRST_PSEUDO_REGISTER)
2099 mark_life (regno, GET_MODE (reg), 0);
2101 /* If a hard register is dying as an output, mark it as in use at
2102 the beginning of this insn (the above statement would cause this
2103 not to happen). */
2104 if (output_p)
2105 post_mark_life (regno, GET_MODE (reg), 1,
2106 2 * this_insn_number, 2 * this_insn_number + 1);
2109 else if (reg_qty[regno] >= 0)
2110 qty[reg_qty[regno]].death = 2 * this_insn_number + output_p;
2113 /* Find a block of SIZE words of hard regs in reg_class CLASS
2114 that can hold something of machine-mode MODE
2115 (but actually we test only the first of the block for holding MODE)
2116 and still free between insn BORN_INDEX and insn DEAD_INDEX,
2117 and return the number of the first of them.
2118 Return -1 if such a block cannot be found.
2119 If QTYNO crosses calls, insist on a register preserved by calls,
2120 unless ACCEPT_CALL_CLOBBERED is nonzero.
2122 If JUST_TRY_SUGGESTED is nonzero, only try to see if the suggested
2123 register is available. If not, return -1. */
2125 static int
2126 find_free_reg (enum reg_class class, enum machine_mode mode, int qtyno,
2127 int accept_call_clobbered, int just_try_suggested,
2128 int born_index, int dead_index)
2130 int i, ins;
2131 HARD_REG_SET first_used, used;
2132 #ifdef ELIMINABLE_REGS
2133 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2134 #endif
2136 /* Validate our parameters. */
2137 if (born_index < 0 || born_index > dead_index)
2138 abort ();
2140 /* Don't let a pseudo live in a reg across a function call
2141 if we might get a nonlocal goto. */
2142 if (current_function_has_nonlocal_label
2143 && qty[qtyno].n_calls_crossed > 0)
2144 return -1;
2146 if (accept_call_clobbered)
2147 COPY_HARD_REG_SET (used, call_fixed_reg_set);
2148 else if (qty[qtyno].n_calls_crossed == 0)
2149 COPY_HARD_REG_SET (used, fixed_reg_set);
2150 else
2151 COPY_HARD_REG_SET (used, call_used_reg_set);
2153 if (accept_call_clobbered)
2154 IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
2156 for (ins = born_index; ins < dead_index; ins++)
2157 IOR_HARD_REG_SET (used, regs_live_at[ins]);
2159 IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2161 /* Don't use the frame pointer reg in local-alloc even if
2162 we may omit the frame pointer, because if we do that and then we
2163 need a frame pointer, reload won't know how to move the pseudo
2164 to another hard reg. It can move only regs made by global-alloc.
2166 This is true of any register that can be eliminated. */
2167 #ifdef ELIMINABLE_REGS
2168 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2169 SET_HARD_REG_BIT (used, eliminables[i].from);
2170 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2171 /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2172 that it might be eliminated into. */
2173 SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2174 #endif
2175 #else
2176 SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2177 #endif
2179 #ifdef CANNOT_CHANGE_MODE_CLASS
2180 cannot_change_mode_set_regs (&used, mode, qty[qtyno].first_reg);
2181 #endif
2183 /* Normally, the registers that can be used for the first register in
2184 a multi-register quantity are the same as those that can be used for
2185 subsequent registers. However, if just trying suggested registers,
2186 restrict our consideration to them. If there are copy-suggested
2187 register, try them. Otherwise, try the arithmetic-suggested
2188 registers. */
2189 COPY_HARD_REG_SET (first_used, used);
2191 if (just_try_suggested)
2193 if (qty_phys_num_copy_sugg[qtyno] != 0)
2194 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qtyno]);
2195 else
2196 IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qtyno]);
2199 /* If all registers are excluded, we can't do anything. */
2200 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2202 /* If at least one would be suitable, test each hard reg. */
2204 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2206 #ifdef REG_ALLOC_ORDER
2207 int regno = reg_alloc_order[i];
2208 #else
2209 int regno = i;
2210 #endif
2211 if (! TEST_HARD_REG_BIT (first_used, regno)
2212 && HARD_REGNO_MODE_OK (regno, mode)
2213 && (qty[qtyno].n_calls_crossed == 0
2214 || accept_call_clobbered
2215 || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
2217 int j;
2218 int size1 = HARD_REGNO_NREGS (regno, mode);
2219 for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2220 if (j == size1)
2222 /* Mark that this register is in use between its birth and death
2223 insns. */
2224 post_mark_life (regno, mode, 1, born_index, dead_index);
2225 return regno;
2227 #ifndef REG_ALLOC_ORDER
2228 /* Skip starting points we know will lose. */
2229 i += j;
2230 #endif
2234 fail:
2235 /* If we are just trying suggested register, we have just tried copy-
2236 suggested registers, and there are arithmetic-suggested registers,
2237 try them. */
2239 /* If it would be profitable to allocate a call-clobbered register
2240 and save and restore it around calls, do that. */
2241 if (just_try_suggested && qty_phys_num_copy_sugg[qtyno] != 0
2242 && qty_phys_num_sugg[qtyno] != 0)
2244 /* Don't try the copy-suggested regs again. */
2245 qty_phys_num_copy_sugg[qtyno] = 0;
2246 return find_free_reg (class, mode, qtyno, accept_call_clobbered, 1,
2247 born_index, dead_index);
2250 /* We need not check to see if the current function has nonlocal
2251 labels because we don't put any pseudos that are live over calls in
2252 registers in that case. */
2254 if (! accept_call_clobbered
2255 && flag_caller_saves
2256 && ! just_try_suggested
2257 && qty[qtyno].n_calls_crossed != 0
2258 && CALLER_SAVE_PROFITABLE (qty[qtyno].n_refs,
2259 qty[qtyno].n_calls_crossed))
2261 i = find_free_reg (class, mode, qtyno, 1, 0, born_index, dead_index);
2262 if (i >= 0)
2263 caller_save_needed = 1;
2264 return i;
2266 return -1;
2269 /* Mark that REGNO with machine-mode MODE is live starting from the current
2270 insn (if LIFE is nonzero) or dead starting at the current insn (if LIFE
2271 is zero). */
2273 static void
2274 mark_life (int regno, enum machine_mode mode, int life)
2276 int j = HARD_REGNO_NREGS (regno, mode);
2277 if (life)
2278 while (--j >= 0)
2279 SET_HARD_REG_BIT (regs_live, regno + j);
2280 else
2281 while (--j >= 0)
2282 CLEAR_HARD_REG_BIT (regs_live, regno + j);
2285 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2286 is nonzero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2287 to insn number DEATH (exclusive). */
2289 static void
2290 post_mark_life (int regno, enum machine_mode mode, int life, int birth,
2291 int death)
2293 int j = HARD_REGNO_NREGS (regno, mode);
2294 #ifdef HARD_REG_SET
2295 /* Declare it register if it's a scalar. */
2296 register
2297 #endif
2298 HARD_REG_SET this_reg;
2300 CLEAR_HARD_REG_SET (this_reg);
2301 while (--j >= 0)
2302 SET_HARD_REG_BIT (this_reg, regno + j);
2304 if (life)
2305 while (birth < death)
2307 IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2308 birth++;
2310 else
2311 while (birth < death)
2313 AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2314 birth++;
2318 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2319 is the register being clobbered, and R1 is a register being used in
2320 the equivalent expression.
2322 If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2323 in which it is used, return 1.
2325 Otherwise, return 0. */
2327 static int
2328 no_conflict_p (rtx insn, rtx r0 ATTRIBUTE_UNUSED, rtx r1)
2330 int ok = 0;
2331 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2332 rtx p, last;
2334 /* If R1 is a hard register, return 0 since we handle this case
2335 when we scan the insns that actually use it. */
2337 if (note == 0
2338 || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2339 || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2340 && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2341 return 0;
2343 last = XEXP (note, 0);
2345 for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2346 if (INSN_P (p))
2348 if (find_reg_note (p, REG_DEAD, r1))
2349 ok = 1;
2351 /* There must be a REG_NO_CONFLICT note on every insn, otherwise
2352 some earlier optimization pass has inserted instructions into
2353 the sequence, and it is not safe to perform this optimization.
2354 Note that emit_no_conflict_block always ensures that this is
2355 true when these sequences are created. */
2356 if (! find_reg_note (p, REG_NO_CONFLICT, r1))
2357 return 0;
2360 return ok;
2363 /* Return the number of alternatives for which the constraint string P
2364 indicates that the operand must be equal to operand 0 and that no register
2365 is acceptable. */
2367 static int
2368 requires_inout (const char *p)
2370 char c;
2371 int found_zero = 0;
2372 int reg_allowed = 0;
2373 int num_matching_alts = 0;
2374 int len;
2376 for ( ; (c = *p); p += len)
2378 len = CONSTRAINT_LEN (c, p);
2379 switch (c)
2381 case '=': case '+': case '?':
2382 case '#': case '&': case '!':
2383 case '*': case '%':
2384 case 'm': case '<': case '>': case 'V': case 'o':
2385 case 'E': case 'F': case 'G': case 'H':
2386 case 's': case 'i': case 'n':
2387 case 'I': case 'J': case 'K': case 'L':
2388 case 'M': case 'N': case 'O': case 'P':
2389 case 'X':
2390 /* These don't say anything we care about. */
2391 break;
2393 case ',':
2394 if (found_zero && ! reg_allowed)
2395 num_matching_alts++;
2397 found_zero = reg_allowed = 0;
2398 break;
2400 case '0':
2401 found_zero = 1;
2402 break;
2404 case '1': case '2': case '3': case '4': case '5':
2405 case '6': case '7': case '8': case '9':
2406 /* Skip the balance of the matching constraint. */
2408 p++;
2409 while (ISDIGIT (*p));
2410 len = 0;
2411 break;
2413 default:
2414 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS
2415 && !EXTRA_ADDRESS_CONSTRAINT (c, p))
2416 break;
2417 /* FALLTHRU */
2418 case 'p':
2419 case 'g': case 'r':
2420 reg_allowed = 1;
2421 break;
2425 if (found_zero && ! reg_allowed)
2426 num_matching_alts++;
2428 return num_matching_alts;
2431 void
2432 dump_local_alloc (FILE *file)
2434 int i;
2435 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2436 if (reg_renumber[i] != -1)
2437 fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);