(LIB1FUNCS): Delete _lshlsi3.
[official-gcc.git] / gcc / reload1.c
blob13dd43b95c8736bfe41d4ad241e73f4c4b2ab5b6
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 88, 89, 92, 93, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdio.h>
22 #include "config.h"
23 #include "rtl.h"
24 #include "obstack.h"
25 #include "insn-config.h"
26 #include "insn-flags.h"
27 #include "insn-codes.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "reload.h"
33 #include "recog.h"
34 #include "basic-block.h"
35 #include "output.h"
37 /* This file contains the reload pass of the compiler, which is
38 run after register allocation has been done. It checks that
39 each insn is valid (operands required to be in registers really
40 are in registers of the proper class) and fixes up invalid ones
41 by copying values temporarily into registers for the insns
42 that need them.
44 The results of register allocation are described by the vector
45 reg_renumber; the insns still contain pseudo regs, but reg_renumber
46 can be used to find which hard reg, if any, a pseudo reg is in.
48 The technique we always use is to free up a few hard regs that are
49 called ``reload regs'', and for each place where a pseudo reg
50 must be in a hard reg, copy it temporarily into one of the reload regs.
52 All the pseudos that were formerly allocated to the hard regs that
53 are now in use as reload regs must be ``spilled''. This means
54 that they go to other hard regs, or to stack slots if no other
55 available hard regs can be found. Spilling can invalidate more
56 insns, requiring additional need for reloads, so we must keep checking
57 until the process stabilizes.
59 For machines with different classes of registers, we must keep track
60 of the register class needed for each reload, and make sure that
61 we allocate enough reload registers of each class.
63 The file reload.c contains the code that checks one insn for
64 validity and reports the reloads that it needs. This file
65 is in charge of scanning the entire rtl code, accumulating the
66 reload needs, spilling, assigning reload registers to use for
67 fixing up each insn, and generating the new insns to copy values
68 into the reload registers. */
71 #ifndef REGISTER_MOVE_COST
72 #define REGISTER_MOVE_COST(x, y) 2
73 #endif
75 #ifndef MEMORY_MOVE_COST
76 #define MEMORY_MOVE_COST(x) 4
77 #endif
79 /* During reload_as_needed, element N contains a REG rtx for the hard reg
80 into which reg N has been reloaded (perhaps for a previous insn). */
81 static rtx *reg_last_reload_reg;
83 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
84 for an output reload that stores into reg N. */
85 static char *reg_has_output_reload;
87 /* Indicates which hard regs are reload-registers for an output reload
88 in the current insn. */
89 static HARD_REG_SET reg_is_output_reload;
91 /* Element N is the constant value to which pseudo reg N is equivalent,
92 or zero if pseudo reg N is not equivalent to a constant.
93 find_reloads looks at this in order to replace pseudo reg N
94 with the constant it stands for. */
95 rtx *reg_equiv_constant;
97 /* Element N is a memory location to which pseudo reg N is equivalent,
98 prior to any register elimination (such as frame pointer to stack
99 pointer). Depending on whether or not it is a valid address, this value
100 is transferred to either reg_equiv_address or reg_equiv_mem. */
101 rtx *reg_equiv_memory_loc;
103 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
104 This is used when the address is not valid as a memory address
105 (because its displacement is too big for the machine.) */
106 rtx *reg_equiv_address;
108 /* Element N is the memory slot to which pseudo reg N is equivalent,
109 or zero if pseudo reg N is not equivalent to a memory slot. */
110 rtx *reg_equiv_mem;
112 /* Widest width in which each pseudo reg is referred to (via subreg). */
113 static int *reg_max_ref_width;
115 /* Element N is the insn that initialized reg N from its equivalent
116 constant or memory slot. */
117 static rtx *reg_equiv_init;
119 /* During reload_as_needed, element N contains the last pseudo regno
120 reloaded into the Nth reload register. This vector is in parallel
121 with spill_regs. If that pseudo reg occupied more than one register,
122 reg_reloaded_contents points to that pseudo for each spill register in
123 use; all of these must remain set for an inheritance to occur. */
124 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
126 /* During reload_as_needed, element N contains the insn for which
127 the Nth reload register was last used. This vector is in parallel
128 with spill_regs, and its contents are significant only when
129 reg_reloaded_contents is significant. */
130 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
132 /* Number of spill-regs so far; number of valid elements of spill_regs. */
133 static int n_spills;
135 /* In parallel with spill_regs, contains REG rtx's for those regs.
136 Holds the last rtx used for any given reg, or 0 if it has never
137 been used for spilling yet. This rtx is reused, provided it has
138 the proper mode. */
139 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
141 /* In parallel with spill_regs, contains nonzero for a spill reg
142 that was stored after the last time it was used.
143 The precise value is the insn generated to do the store. */
144 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
146 /* This table is the inverse mapping of spill_regs:
147 indexed by hard reg number,
148 it contains the position of that reg in spill_regs,
149 or -1 for something that is not in spill_regs. */
150 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
152 /* This reg set indicates registers that may not be used for retrying global
153 allocation. The registers that may not be used include all spill registers
154 and the frame pointer (if we are using one). */
155 HARD_REG_SET forbidden_regs;
157 /* This reg set indicates registers that are not good for spill registers.
158 They will not be used to complete groups of spill registers. This includes
159 all fixed registers, registers that may be eliminated, and, if
160 SMALL_REGISTER_CLASSES is not defined, registers explicitly used in the rtl.
162 (spill_reg_order prevents these registers from being used to start a
163 group.) */
164 static HARD_REG_SET bad_spill_regs;
166 /* Describes order of use of registers for reloading
167 of spilled pseudo-registers. `spills' is the number of
168 elements that are actually valid; new ones are added at the end. */
169 static short spill_regs[FIRST_PSEUDO_REGISTER];
171 /* Describes order of preference for putting regs into spill_regs.
172 Contains the numbers of all the hard regs, in order most preferred first.
173 This order is different for each function.
174 It is set up by order_regs_for_reload.
175 Empty elements at the end contain -1. */
176 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
178 /* 1 for a hard register that appears explicitly in the rtl
179 (for example, function value registers, special registers
180 used by insns, structure value pointer registers). */
181 static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
183 /* Indicates if a register was counted against the need for
184 groups. 0 means it can count against max_nongroup instead. */
185 static HARD_REG_SET counted_for_groups;
187 /* Indicates if a register was counted against the need for
188 non-groups. 0 means it can become part of a new group.
189 During choose_reload_regs, 1 here means don't use this reg
190 as part of a group, even if it seems to be otherwise ok. */
191 static HARD_REG_SET counted_for_nongroups;
193 /* Indexed by pseudo reg number N,
194 says may not delete stores into the real (memory) home of pseudo N.
195 This is set if we already substituted a memory equivalent in some uses,
196 which happens when we have to eliminate the fp from it. */
197 static char *cannot_omit_stores;
199 /* Nonzero if indirect addressing is supported on the machine; this means
200 that spilling (REG n) does not require reloading it into a register in
201 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
202 value indicates the level of indirect addressing supported, e.g., two
203 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
204 a hard register. */
206 static char spill_indirect_levels;
208 /* Nonzero if indirect addressing is supported when the innermost MEM is
209 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
210 which these are valid is the same as spill_indirect_levels, above. */
212 char indirect_symref_ok;
214 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
216 char double_reg_address_ok;
218 /* Record the stack slot for each spilled hard register. */
220 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
222 /* Width allocated so far for that stack slot. */
224 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
226 /* Indexed by register class and basic block number, nonzero if there is
227 any need for a spill register of that class in that basic block.
228 The pointer is 0 if we did stupid allocation and don't know
229 the structure of basic blocks. */
231 char *basic_block_needs[N_REG_CLASSES];
233 /* First uid used by insns created by reload in this function.
234 Used in find_equiv_reg. */
235 int reload_first_uid;
237 /* Flag set by local-alloc or global-alloc if anything is live in
238 a call-clobbered reg across calls. */
240 int caller_save_needed;
242 /* Set to 1 while reload_as_needed is operating.
243 Required by some machines to handle any generated moves differently. */
245 int reload_in_progress = 0;
247 /* These arrays record the insn_code of insns that may be needed to
248 perform input and output reloads of special objects. They provide a
249 place to pass a scratch register. */
251 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
252 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
254 /* This obstack is used for allocation of rtl during register elimination.
255 The allocated storage can be freed once find_reloads has processed the
256 insn. */
258 struct obstack reload_obstack;
259 char *reload_firstobj;
261 #define obstack_chunk_alloc xmalloc
262 #define obstack_chunk_free free
264 /* List of labels that must never be deleted. */
265 extern rtx forced_labels;
267 /* This structure is used to record information about register eliminations.
268 Each array entry describes one possible way of eliminating a register
269 in favor of another. If there is more than one way of eliminating a
270 particular register, the most preferred should be specified first. */
272 static struct elim_table
274 int from; /* Register number to be eliminated. */
275 int to; /* Register number used as replacement. */
276 int initial_offset; /* Initial difference between values. */
277 int can_eliminate; /* Non-zero if this elimination can be done. */
278 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
279 insns made by reload. */
280 int offset; /* Current offset between the two regs. */
281 int max_offset; /* Maximum offset between the two regs. */
282 int previous_offset; /* Offset at end of previous insn. */
283 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
284 rtx from_rtx; /* REG rtx for the register to be eliminated.
285 We cannot simply compare the number since
286 we might then spuriously replace a hard
287 register corresponding to a pseudo
288 assigned to the reg to be eliminated. */
289 rtx to_rtx; /* REG rtx for the replacement. */
290 } reg_eliminate[] =
292 /* If a set of eliminable registers was specified, define the table from it.
293 Otherwise, default to the normal case of the frame pointer being
294 replaced by the stack pointer. */
296 #ifdef ELIMINABLE_REGS
297 ELIMINABLE_REGS;
298 #else
299 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
300 #endif
302 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
304 /* Record the number of pending eliminations that have an offset not equal
305 to their initial offset. If non-zero, we use a new copy of each
306 replacement result in any insns encountered. */
307 static int num_not_at_initial_offset;
309 /* Count the number of registers that we may be able to eliminate. */
310 static int num_eliminable;
312 /* For each label, we record the offset of each elimination. If we reach
313 a label by more than one path and an offset differs, we cannot do the
314 elimination. This information is indexed by the number of the label.
315 The first table is an array of flags that records whether we have yet
316 encountered a label and the second table is an array of arrays, one
317 entry in the latter array for each elimination. */
319 static char *offsets_known_at;
320 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
322 /* Number of labels in the current function. */
324 static int num_labels;
326 struct hard_reg_n_uses { int regno; int uses; };
328 static int possible_group_p PROTO((int, int *));
329 static void count_possible_groups PROTO((int *, enum machine_mode *,
330 int *, int));
331 static int modes_equiv_for_class_p PROTO((enum machine_mode,
332 enum machine_mode,
333 enum reg_class));
334 static void spill_failure PROTO((rtx));
335 static int new_spill_reg PROTO((int, int, int *, int *, int,
336 FILE *));
337 static void delete_dead_insn PROTO((rtx));
338 static void alter_reg PROTO((int, int));
339 static void mark_scratch_live PROTO((rtx));
340 static void set_label_offsets PROTO((rtx, rtx, int));
341 static int eliminate_regs_in_insn PROTO((rtx, int));
342 static void mark_not_eliminable PROTO((rtx, rtx));
343 static int spill_hard_reg PROTO((int, int, FILE *, int));
344 static void scan_paradoxical_subregs PROTO((rtx));
345 static int hard_reg_use_compare PROTO((struct hard_reg_n_uses *,
346 struct hard_reg_n_uses *));
347 static void order_regs_for_reload PROTO((void));
348 static int compare_spill_regs PROTO((short *, short *));
349 static void reload_as_needed PROTO((rtx, int));
350 static void forget_old_reloads_1 PROTO((rtx, rtx));
351 static int reload_reg_class_lower PROTO((short *, short *));
352 static void mark_reload_reg_in_use PROTO((int, int, enum reload_type,
353 enum machine_mode));
354 static void clear_reload_reg_in_use PROTO((int, int, enum reload_type,
355 enum machine_mode));
356 static int reload_reg_free_p PROTO((int, int, enum reload_type));
357 static int reload_reg_free_before_p PROTO((int, int, enum reload_type));
358 static int reload_reg_reaches_end_p PROTO((int, int, enum reload_type));
359 static int reloads_conflict PROTO((int, int));
360 static int allocate_reload_reg PROTO((int, rtx, int, int));
361 static void choose_reload_regs PROTO((rtx, rtx));
362 static void merge_assigned_reloads PROTO((rtx));
363 static void emit_reload_insns PROTO((rtx));
364 static void delete_output_reload PROTO((rtx, int, rtx));
365 static void inc_for_reload PROTO((rtx, rtx, int));
366 static int constraint_accepts_reg_p PROTO((char *, rtx));
367 static int count_occurrences PROTO((rtx, rtx));
369 /* Initialize the reload pass once per compilation. */
371 void
372 init_reload ()
374 register int i;
376 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
377 Set spill_indirect_levels to the number of levels such addressing is
378 permitted, zero if it is not permitted at all. */
380 register rtx tem
381 = gen_rtx (MEM, Pmode,
382 gen_rtx (PLUS, Pmode,
383 gen_rtx (REG, Pmode, LAST_VIRTUAL_REGISTER + 1),
384 GEN_INT (4)));
385 spill_indirect_levels = 0;
387 while (memory_address_p (QImode, tem))
389 spill_indirect_levels++;
390 tem = gen_rtx (MEM, Pmode, tem);
393 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
395 tem = gen_rtx (MEM, Pmode, gen_rtx (SYMBOL_REF, Pmode, "foo"));
396 indirect_symref_ok = memory_address_p (QImode, tem);
398 /* See if reg+reg is a valid (and offsettable) address. */
400 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
402 tem = gen_rtx (PLUS, Pmode,
403 gen_rtx (REG, Pmode, HARD_FRAME_POINTER_REGNUM),
404 gen_rtx (REG, Pmode, i));
405 /* This way, we make sure that reg+reg is an offsettable address. */
406 tem = plus_constant (tem, 4);
408 if (memory_address_p (QImode, tem))
410 double_reg_address_ok = 1;
411 break;
415 /* Initialize obstack for our rtl allocation. */
416 gcc_obstack_init (&reload_obstack);
417 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
420 /* Main entry point for the reload pass.
422 FIRST is the first insn of the function being compiled.
424 GLOBAL nonzero means we were called from global_alloc
425 and should attempt to reallocate any pseudoregs that we
426 displace from hard regs we will use for reloads.
427 If GLOBAL is zero, we do not have enough information to do that,
428 so any pseudo reg that is spilled must go to the stack.
430 DUMPFILE is the global-reg debugging dump file stream, or 0.
431 If it is nonzero, messages are written to it to describe
432 which registers are seized as reload regs, which pseudo regs
433 are spilled from them, and where the pseudo regs are reallocated to.
435 Return value is nonzero if reload failed
436 and we must not do any more for this function. */
439 reload (first, global, dumpfile)
440 rtx first;
441 int global;
442 FILE *dumpfile;
444 register int class;
445 register int i, j, k;
446 register rtx insn;
447 register struct elim_table *ep;
449 int something_changed;
450 int something_needs_reloads;
451 int something_needs_elimination;
452 int new_basic_block_needs;
453 enum reg_class caller_save_spill_class = NO_REGS;
454 int caller_save_group_size = 1;
456 /* Nonzero means we couldn't get enough spill regs. */
457 int failure = 0;
459 /* The basic block number currently being processed for INSN. */
460 int this_block;
462 /* Make sure even insns with volatile mem refs are recognizable. */
463 init_recog ();
465 /* Enable find_equiv_reg to distinguish insns made by reload. */
466 reload_first_uid = get_max_uid ();
468 for (i = 0; i < N_REG_CLASSES; i++)
469 basic_block_needs[i] = 0;
471 #ifdef SECONDARY_MEMORY_NEEDED
472 /* Initialize the secondary memory table. */
473 clear_secondary_mem ();
474 #endif
476 /* Remember which hard regs appear explicitly
477 before we merge into `regs_ever_live' the ones in which
478 pseudo regs have been allocated. */
479 bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
481 /* We don't have a stack slot for any spill reg yet. */
482 bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
483 bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
485 /* Initialize the save area information for caller-save, in case some
486 are needed. */
487 init_save_areas ();
489 /* Compute which hard registers are now in use
490 as homes for pseudo registers.
491 This is done here rather than (eg) in global_alloc
492 because this point is reached even if not optimizing. */
494 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
495 mark_home_live (i);
497 for (i = 0; i < scratch_list_length; i++)
498 if (scratch_list[i])
499 mark_scratch_live (scratch_list[i]);
501 /* Make sure that the last insn in the chain
502 is not something that needs reloading. */
503 emit_note (NULL_PTR, NOTE_INSN_DELETED);
505 /* Find all the pseudo registers that didn't get hard regs
506 but do have known equivalent constants or memory slots.
507 These include parameters (known equivalent to parameter slots)
508 and cse'd or loop-moved constant memory addresses.
510 Record constant equivalents in reg_equiv_constant
511 so they will be substituted by find_reloads.
512 Record memory equivalents in reg_mem_equiv so they can
513 be substituted eventually by altering the REG-rtx's. */
515 reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
516 bzero ((char *) reg_equiv_constant, max_regno * sizeof (rtx));
517 reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
518 bzero ((char *) reg_equiv_memory_loc, max_regno * sizeof (rtx));
519 reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
520 bzero ((char *) reg_equiv_mem, max_regno * sizeof (rtx));
521 reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
522 bzero ((char *) reg_equiv_init, max_regno * sizeof (rtx));
523 reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
524 bzero ((char *) reg_equiv_address, max_regno * sizeof (rtx));
525 reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
526 bzero ((char *) reg_max_ref_width, max_regno * sizeof (int));
527 cannot_omit_stores = (char *) alloca (max_regno);
528 bzero (cannot_omit_stores, max_regno);
530 #ifdef SMALL_REGISTER_CLASSES
531 CLEAR_HARD_REG_SET (forbidden_regs);
532 #endif
534 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
535 Also find all paradoxical subregs and find largest such for each pseudo.
536 On machines with small register classes, record hard registers that
537 are used for user variables. These can never be used for spills. */
539 for (insn = first; insn; insn = NEXT_INSN (insn))
541 rtx set = single_set (insn);
543 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
545 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
546 if (note
547 #ifdef LEGITIMATE_PIC_OPERAND_P
548 && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
549 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
550 #endif
553 rtx x = XEXP (note, 0);
554 i = REGNO (SET_DEST (set));
555 if (i > LAST_VIRTUAL_REGISTER)
557 if (GET_CODE (x) == MEM)
558 reg_equiv_memory_loc[i] = x;
559 else if (CONSTANT_P (x))
561 if (LEGITIMATE_CONSTANT_P (x))
562 reg_equiv_constant[i] = x;
563 else
564 reg_equiv_memory_loc[i]
565 = force_const_mem (GET_MODE (SET_DEST (set)), x);
567 else
568 continue;
570 /* If this register is being made equivalent to a MEM
571 and the MEM is not SET_SRC, the equivalencing insn
572 is one with the MEM as a SET_DEST and it occurs later.
573 So don't mark this insn now. */
574 if (GET_CODE (x) != MEM
575 || rtx_equal_p (SET_SRC (set), x))
576 reg_equiv_init[i] = insn;
581 /* If this insn is setting a MEM from a register equivalent to it,
582 this is the equivalencing insn. */
583 else if (set && GET_CODE (SET_DEST (set)) == MEM
584 && GET_CODE (SET_SRC (set)) == REG
585 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
586 && rtx_equal_p (SET_DEST (set),
587 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
588 reg_equiv_init[REGNO (SET_SRC (set))] = insn;
590 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
591 scan_paradoxical_subregs (PATTERN (insn));
594 /* Does this function require a frame pointer? */
596 frame_pointer_needed = (! flag_omit_frame_pointer
597 #ifdef EXIT_IGNORE_STACK
598 /* ?? If EXIT_IGNORE_STACK is set, we will not save
599 and restore sp for alloca. So we can't eliminate
600 the frame pointer in that case. At some point,
601 we should improve this by emitting the
602 sp-adjusting insns for this case. */
603 || (current_function_calls_alloca
604 && EXIT_IGNORE_STACK)
605 #endif
606 || FRAME_POINTER_REQUIRED);
608 num_eliminable = 0;
610 /* Initialize the table of registers to eliminate. The way we do this
611 depends on how the eliminable registers were defined. */
612 #ifdef ELIMINABLE_REGS
613 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
615 ep->can_eliminate = ep->can_eliminate_previous
616 = (CAN_ELIMINATE (ep->from, ep->to)
617 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
619 #else
620 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
621 = ! frame_pointer_needed;
622 #endif
624 /* Count the number of eliminable registers and build the FROM and TO
625 REG rtx's. Note that code in gen_rtx will cause, e.g.,
626 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
627 We depend on this. */
628 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
630 num_eliminable += ep->can_eliminate;
631 ep->from_rtx = gen_rtx (REG, Pmode, ep->from);
632 ep->to_rtx = gen_rtx (REG, Pmode, ep->to);
635 num_labels = max_label_num () - get_first_label_num ();
637 /* Allocate the tables used to store offset information at labels. */
638 offsets_known_at = (char *) alloca (num_labels);
639 offsets_at
640 = (int (*)[NUM_ELIMINABLE_REGS])
641 alloca (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
643 offsets_known_at -= get_first_label_num ();
644 offsets_at -= get_first_label_num ();
646 /* Alter each pseudo-reg rtx to contain its hard reg number.
647 Assign stack slots to the pseudos that lack hard regs or equivalents.
648 Do not touch virtual registers. */
650 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
651 alter_reg (i, -1);
653 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done here
654 because the stack size may be a part of the offset computation for
655 register elimination. */
656 assign_stack_local (BLKmode, 0, 0);
658 /* If we have some registers we think can be eliminated, scan all insns to
659 see if there is an insn that sets one of these registers to something
660 other than itself plus a constant. If so, the register cannot be
661 eliminated. Doing this scan here eliminates an extra pass through the
662 main reload loop in the most common case where register elimination
663 cannot be done. */
664 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
665 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
666 || GET_CODE (insn) == CALL_INSN)
667 note_stores (PATTERN (insn), mark_not_eliminable);
669 #ifndef REGISTER_CONSTRAINTS
670 /* If all the pseudo regs have hard regs,
671 except for those that are never referenced,
672 we know that no reloads are needed. */
673 /* But that is not true if there are register constraints, since
674 in that case some pseudos might be in the wrong kind of hard reg. */
676 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
677 if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
678 break;
680 if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
681 return;
682 #endif
684 /* Compute the order of preference for hard registers to spill.
685 Store them by decreasing preference in potential_reload_regs. */
687 order_regs_for_reload ();
689 /* So far, no hard regs have been spilled. */
690 n_spills = 0;
691 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
692 spill_reg_order[i] = -1;
694 /* On most machines, we can't use any register explicitly used in the
695 rtl as a spill register. But on some, we have to. Those will have
696 taken care to keep the life of hard regs as short as possible. */
698 #ifndef SMALL_REGISTER_CLASSES
699 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
700 #endif
702 /* Spill any hard regs that we know we can't eliminate. */
703 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
704 if (! ep->can_eliminate)
705 spill_hard_reg (ep->from, global, dumpfile, 1);
707 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
708 if (frame_pointer_needed)
709 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
710 #endif
712 if (global)
713 for (i = 0; i < N_REG_CLASSES; i++)
715 basic_block_needs[i] = (char *) alloca (n_basic_blocks);
716 bzero (basic_block_needs[i], n_basic_blocks);
719 /* From now on, we need to emit any moves without making new pseudos. */
720 reload_in_progress = 1;
722 /* This loop scans the entire function each go-round
723 and repeats until one repetition spills no additional hard regs. */
725 /* This flag is set when a pseudo reg is spilled,
726 to require another pass. Note that getting an additional reload
727 reg does not necessarily imply any pseudo reg was spilled;
728 sometimes we find a reload reg that no pseudo reg was allocated in. */
729 something_changed = 1;
730 /* This flag is set if there are any insns that require reloading. */
731 something_needs_reloads = 0;
732 /* This flag is set if there are any insns that require register
733 eliminations. */
734 something_needs_elimination = 0;
735 while (something_changed)
737 rtx after_call = 0;
739 /* For each class, number of reload regs needed in that class.
740 This is the maximum over all insns of the needs in that class
741 of the individual insn. */
742 int max_needs[N_REG_CLASSES];
743 /* For each class, size of group of consecutive regs
744 that is needed for the reloads of this class. */
745 int group_size[N_REG_CLASSES];
746 /* For each class, max number of consecutive groups needed.
747 (Each group contains group_size[CLASS] consecutive registers.) */
748 int max_groups[N_REG_CLASSES];
749 /* For each class, max number needed of regs that don't belong
750 to any of the groups. */
751 int max_nongroups[N_REG_CLASSES];
752 /* For each class, the machine mode which requires consecutive
753 groups of regs of that class.
754 If two different modes ever require groups of one class,
755 they must be the same size and equally restrictive for that class,
756 otherwise we can't handle the complexity. */
757 enum machine_mode group_mode[N_REG_CLASSES];
758 /* Record the insn where each maximum need is first found. */
759 rtx max_needs_insn[N_REG_CLASSES];
760 rtx max_groups_insn[N_REG_CLASSES];
761 rtx max_nongroups_insn[N_REG_CLASSES];
762 rtx x;
763 int starting_frame_size = get_frame_size ();
764 int previous_frame_pointer_needed = frame_pointer_needed;
765 static char *reg_class_names[] = REG_CLASS_NAMES;
767 something_changed = 0;
768 bzero ((char *) max_needs, sizeof max_needs);
769 bzero ((char *) max_groups, sizeof max_groups);
770 bzero ((char *) max_nongroups, sizeof max_nongroups);
771 bzero ((char *) max_needs_insn, sizeof max_needs_insn);
772 bzero ((char *) max_groups_insn, sizeof max_groups_insn);
773 bzero ((char *) max_nongroups_insn, sizeof max_nongroups_insn);
774 bzero ((char *) group_size, sizeof group_size);
775 for (i = 0; i < N_REG_CLASSES; i++)
776 group_mode[i] = VOIDmode;
778 /* Keep track of which basic blocks are needing the reloads. */
779 this_block = 0;
781 /* Remember whether any element of basic_block_needs
782 changes from 0 to 1 in this pass. */
783 new_basic_block_needs = 0;
785 /* Reset all offsets on eliminable registers to their initial values. */
786 #ifdef ELIMINABLE_REGS
787 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
789 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
790 ep->previous_offset = ep->offset
791 = ep->max_offset = ep->initial_offset;
793 #else
794 #ifdef INITIAL_FRAME_POINTER_OFFSET
795 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
796 #else
797 if (!FRAME_POINTER_REQUIRED)
798 abort ();
799 reg_eliminate[0].initial_offset = 0;
800 #endif
801 reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
802 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
803 #endif
805 num_not_at_initial_offset = 0;
807 bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
809 /* Set a known offset for each forced label to be at the initial offset
810 of each elimination. We do this because we assume that all
811 computed jumps occur from a location where each elimination is
812 at its initial offset. */
814 for (x = forced_labels; x; x = XEXP (x, 1))
815 if (XEXP (x, 0))
816 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
818 /* For each pseudo register that has an equivalent location defined,
819 try to eliminate any eliminable registers (such as the frame pointer)
820 assuming initial offsets for the replacement register, which
821 is the normal case.
823 If the resulting location is directly addressable, substitute
824 the MEM we just got directly for the old REG.
826 If it is not addressable but is a constant or the sum of a hard reg
827 and constant, it is probably not addressable because the constant is
828 out of range, in that case record the address; we will generate
829 hairy code to compute the address in a register each time it is
830 needed. Similarly if it is a hard register, but one that is not
831 valid as an address register.
833 If the location is not addressable, but does not have one of the
834 above forms, assign a stack slot. We have to do this to avoid the
835 potential of producing lots of reloads if, e.g., a location involves
836 a pseudo that didn't get a hard register and has an equivalent memory
837 location that also involves a pseudo that didn't get a hard register.
839 Perhaps at some point we will improve reload_when_needed handling
840 so this problem goes away. But that's very hairy. */
842 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
843 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
845 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
847 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
848 XEXP (x, 0)))
849 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
850 else if (CONSTANT_P (XEXP (x, 0))
851 || (GET_CODE (XEXP (x, 0)) == REG
852 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
853 || (GET_CODE (XEXP (x, 0)) == PLUS
854 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
855 && (REGNO (XEXP (XEXP (x, 0), 0))
856 < FIRST_PSEUDO_REGISTER)
857 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
858 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
859 else
861 /* Make a new stack slot. Then indicate that something
862 changed so we go back and recompute offsets for
863 eliminable registers because the allocation of memory
864 below might change some offset. reg_equiv_{mem,address}
865 will be set up for this pseudo on the next pass around
866 the loop. */
867 reg_equiv_memory_loc[i] = 0;
868 reg_equiv_init[i] = 0;
869 alter_reg (i, -1);
870 something_changed = 1;
874 /* If we allocated another pseudo to the stack, redo elimination
875 bookkeeping. */
876 if (something_changed)
877 continue;
879 /* If caller-saves needs a group, initialize the group to include
880 the size and mode required for caller-saves. */
882 if (caller_save_group_size > 1)
884 group_mode[(int) caller_save_spill_class] = Pmode;
885 group_size[(int) caller_save_spill_class] = caller_save_group_size;
888 /* Compute the most additional registers needed by any instruction.
889 Collect information separately for each class of regs. */
891 for (insn = first; insn; insn = NEXT_INSN (insn))
893 if (global && this_block + 1 < n_basic_blocks
894 && insn == basic_block_head[this_block+1])
895 ++this_block;
897 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
898 might include REG_LABEL), we need to see what effects this
899 has on the known offsets at labels. */
901 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
902 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
903 && REG_NOTES (insn) != 0))
904 set_label_offsets (insn, insn, 0);
906 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
908 /* Nonzero means don't use a reload reg that overlaps
909 the place where a function value can be returned. */
910 rtx avoid_return_reg = 0;
912 rtx old_body = PATTERN (insn);
913 int old_code = INSN_CODE (insn);
914 rtx old_notes = REG_NOTES (insn);
915 int did_elimination = 0;
917 /* To compute the number of reload registers of each class
918 needed for an insn, we must similate what choose_reload_regs
919 can do. We do this by splitting an insn into an "input" and
920 an "output" part. RELOAD_OTHER reloads are used in both.
921 The input part uses those reloads, RELOAD_FOR_INPUT reloads,
922 which must be live over the entire input section of reloads,
923 and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
924 RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the
925 inputs.
927 The registers needed for output are RELOAD_OTHER and
928 RELOAD_FOR_OUTPUT, which are live for the entire output
929 portion, and the maximum of all the RELOAD_FOR_OUTPUT_ADDRESS
930 reloads for each operand.
932 The total number of registers needed is the maximum of the
933 inputs and outputs. */
935 struct needs
937 /* [0] is normal, [1] is nongroup. */
938 int regs[2][N_REG_CLASSES];
939 int groups[N_REG_CLASSES];
942 /* Each `struct needs' corresponds to one RELOAD_... type. */
943 struct {
944 struct needs other;
945 struct needs input;
946 struct needs output;
947 struct needs insn;
948 struct needs other_addr;
949 struct needs op_addr;
950 struct needs op_addr_reload;
951 struct needs in_addr[MAX_RECOG_OPERANDS];
952 struct needs out_addr[MAX_RECOG_OPERANDS];
953 } insn_needs;
955 /* If needed, eliminate any eliminable registers. */
956 if (num_eliminable)
957 did_elimination = eliminate_regs_in_insn (insn, 0);
959 #ifdef SMALL_REGISTER_CLASSES
960 /* Set avoid_return_reg if this is an insn
961 that might use the value of a function call. */
962 if (GET_CODE (insn) == CALL_INSN)
964 if (GET_CODE (PATTERN (insn)) == SET)
965 after_call = SET_DEST (PATTERN (insn));
966 else if (GET_CODE (PATTERN (insn)) == PARALLEL
967 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
968 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
969 else
970 after_call = 0;
972 else if (after_call != 0
973 && !(GET_CODE (PATTERN (insn)) == SET
974 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
976 if (reg_referenced_p (after_call, PATTERN (insn)))
977 avoid_return_reg = after_call;
978 after_call = 0;
980 #endif /* SMALL_REGISTER_CLASSES */
982 /* Analyze the instruction. */
983 find_reloads (insn, 0, spill_indirect_levels, global,
984 spill_reg_order);
986 /* Remember for later shortcuts which insns had any reloads or
987 register eliminations.
989 One might think that it would be worthwhile to mark insns
990 that need register replacements but not reloads, but this is
991 not safe because find_reloads may do some manipulation of
992 the insn (such as swapping commutative operands), which would
993 be lost when we restore the old pattern after register
994 replacement. So the actions of find_reloads must be redone in
995 subsequent passes or in reload_as_needed.
997 However, it is safe to mark insns that need reloads
998 but not register replacement. */
1000 PUT_MODE (insn, (did_elimination ? QImode
1001 : n_reloads ? HImode
1002 : GET_MODE (insn) == DImode ? DImode
1003 : VOIDmode));
1005 /* Discard any register replacements done. */
1006 if (did_elimination)
1008 obstack_free (&reload_obstack, reload_firstobj);
1009 PATTERN (insn) = old_body;
1010 INSN_CODE (insn) = old_code;
1011 REG_NOTES (insn) = old_notes;
1012 something_needs_elimination = 1;
1015 /* If this insn has no reloads, we need not do anything except
1016 in the case of a CALL_INSN when we have caller-saves and
1017 caller-save needs reloads. */
1019 if (n_reloads == 0
1020 && ! (GET_CODE (insn) == CALL_INSN
1021 && caller_save_spill_class != NO_REGS))
1022 continue;
1024 something_needs_reloads = 1;
1025 bzero ((char *) &insn_needs, sizeof insn_needs);
1027 /* Count each reload once in every class
1028 containing the reload's own class. */
1030 for (i = 0; i < n_reloads; i++)
1032 register enum reg_class *p;
1033 enum reg_class class = reload_reg_class[i];
1034 int size;
1035 enum machine_mode mode;
1036 int nongroup_need;
1037 struct needs *this_needs;
1039 /* Don't count the dummy reloads, for which one of the
1040 regs mentioned in the insn can be used for reloading.
1041 Don't count optional reloads.
1042 Don't count reloads that got combined with others. */
1043 if (reload_reg_rtx[i] != 0
1044 || reload_optional[i] != 0
1045 || (reload_out[i] == 0 && reload_in[i] == 0
1046 && ! reload_secondary_p[i]))
1047 continue;
1049 /* Show that a reload register of this class is needed
1050 in this basic block. We do not use insn_needs and
1051 insn_groups because they are overly conservative for
1052 this purpose. */
1053 if (global && ! basic_block_needs[(int) class][this_block])
1055 basic_block_needs[(int) class][this_block] = 1;
1056 new_basic_block_needs = 1;
1060 mode = reload_inmode[i];
1061 if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1062 mode = reload_outmode[i];
1063 size = CLASS_MAX_NREGS (class, mode);
1065 /* If this class doesn't want a group, determine if we have
1066 a nongroup need or a regular need. We have a nongroup
1067 need if this reload conflicts with a group reload whose
1068 class intersects with this reload's class. */
1070 nongroup_need = 0;
1071 if (size == 1)
1072 for (j = 0; j < n_reloads; j++)
1073 if ((CLASS_MAX_NREGS (reload_reg_class[j],
1074 (GET_MODE_SIZE (reload_outmode[j])
1075 > GET_MODE_SIZE (reload_inmode[j]))
1076 ? reload_outmode[j]
1077 : reload_inmode[j])
1078 > 1)
1079 && (!reload_optional[j])
1080 && (reload_in[j] != 0 || reload_out[j] != 0
1081 || reload_secondary_p[j])
1082 && reloads_conflict (i, j)
1083 && reg_classes_intersect_p (class,
1084 reload_reg_class[j]))
1086 nongroup_need = 1;
1087 break;
1090 /* Decide which time-of-use to count this reload for. */
1091 switch (reload_when_needed[i])
1093 case RELOAD_OTHER:
1094 this_needs = &insn_needs.other;
1095 break;
1096 case RELOAD_FOR_INPUT:
1097 this_needs = &insn_needs.input;
1098 break;
1099 case RELOAD_FOR_OUTPUT:
1100 this_needs = &insn_needs.output;
1101 break;
1102 case RELOAD_FOR_INSN:
1103 this_needs = &insn_needs.insn;
1104 break;
1105 case RELOAD_FOR_OTHER_ADDRESS:
1106 this_needs = &insn_needs.other_addr;
1107 break;
1108 case RELOAD_FOR_INPUT_ADDRESS:
1109 this_needs = &insn_needs.in_addr[reload_opnum[i]];
1110 break;
1111 case RELOAD_FOR_OUTPUT_ADDRESS:
1112 this_needs = &insn_needs.out_addr[reload_opnum[i]];
1113 break;
1114 case RELOAD_FOR_OPERAND_ADDRESS:
1115 this_needs = &insn_needs.op_addr;
1116 break;
1117 case RELOAD_FOR_OPADDR_ADDR:
1118 this_needs = &insn_needs.op_addr_reload;
1119 break;
1122 if (size > 1)
1124 enum machine_mode other_mode, allocate_mode;
1126 /* Count number of groups needed separately from
1127 number of individual regs needed. */
1128 this_needs->groups[(int) class]++;
1129 p = reg_class_superclasses[(int) class];
1130 while (*p != LIM_REG_CLASSES)
1131 this_needs->groups[(int) *p++]++;
1133 /* Record size and mode of a group of this class. */
1134 /* If more than one size group is needed,
1135 make all groups the largest needed size. */
1136 if (group_size[(int) class] < size)
1138 other_mode = group_mode[(int) class];
1139 allocate_mode = mode;
1141 group_size[(int) class] = size;
1142 group_mode[(int) class] = mode;
1144 else
1146 other_mode = mode;
1147 allocate_mode = group_mode[(int) class];
1150 /* Crash if two dissimilar machine modes both need
1151 groups of consecutive regs of the same class. */
1153 if (other_mode != VOIDmode && other_mode != allocate_mode
1154 && ! modes_equiv_for_class_p (allocate_mode,
1155 other_mode, class))
1156 fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1157 insn);
1159 else if (size == 1)
1161 this_needs->regs[nongroup_need][(int) class] += 1;
1162 p = reg_class_superclasses[(int) class];
1163 while (*p != LIM_REG_CLASSES)
1164 this_needs->regs[nongroup_need][(int) *p++] += 1;
1166 else
1167 abort ();
1170 /* All reloads have been counted for this insn;
1171 now merge the various times of use.
1172 This sets insn_needs, etc., to the maximum total number
1173 of registers needed at any point in this insn. */
1175 for (i = 0; i < N_REG_CLASSES; i++)
1177 int in_max, out_max;
1179 /* Compute normal and nongroup needs. */
1180 for (j = 0; j <= 1; j++)
1182 for (in_max = 0, out_max = 0, k = 0;
1183 k < reload_n_operands; k++)
1185 in_max
1186 = MAX (in_max, insn_needs.in_addr[k].regs[j][i]);
1187 out_max
1188 = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1191 /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1192 and operand addresses but not things used to reload
1193 them. Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1194 don't conflict with things needed to reload inputs or
1195 outputs. */
1197 in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1198 insn_needs.op_addr_reload.regs[j][i]),
1199 in_max);
1201 out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1203 insn_needs.input.regs[j][i]
1204 = MAX (insn_needs.input.regs[j][i]
1205 + insn_needs.op_addr.regs[j][i]
1206 + insn_needs.insn.regs[j][i],
1207 in_max + insn_needs.input.regs[j][i]);
1209 insn_needs.output.regs[j][i] += out_max;
1210 insn_needs.other.regs[j][i]
1211 += MAX (MAX (insn_needs.input.regs[j][i],
1212 insn_needs.output.regs[j][i]),
1213 insn_needs.other_addr.regs[j][i]);
1217 /* Now compute group needs. */
1218 for (in_max = 0, out_max = 0, j = 0;
1219 j < reload_n_operands; j++)
1221 in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1222 out_max
1223 = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1226 in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1227 insn_needs.op_addr_reload.groups[i]),
1228 in_max);
1229 out_max = MAX (out_max, insn_needs.insn.groups[i]);
1231 insn_needs.input.groups[i]
1232 = MAX (insn_needs.input.groups[i]
1233 + insn_needs.op_addr.groups[i]
1234 + insn_needs.insn.groups[i],
1235 in_max + insn_needs.input.groups[i]);
1237 insn_needs.output.groups[i] += out_max;
1238 insn_needs.other.groups[i]
1239 += MAX (MAX (insn_needs.input.groups[i],
1240 insn_needs.output.groups[i]),
1241 insn_needs.other_addr.groups[i]);
1244 /* If this is a CALL_INSN and caller-saves will need
1245 a spill register, act as if the spill register is
1246 needed for this insn. However, the spill register
1247 can be used by any reload of this insn, so we only
1248 need do something if no need for that class has
1249 been recorded.
1251 The assumption that every CALL_INSN will trigger a
1252 caller-save is highly conservative, however, the number
1253 of cases where caller-saves will need a spill register but
1254 a block containing a CALL_INSN won't need a spill register
1255 of that class should be quite rare.
1257 If a group is needed, the size and mode of the group will
1258 have been set up at the beginning of this loop. */
1260 if (GET_CODE (insn) == CALL_INSN
1261 && caller_save_spill_class != NO_REGS)
1263 /* See if this register would conflict with any reload
1264 that needs a group. */
1265 int nongroup_need = 0;
1266 int *caller_save_needs;
1268 for (j = 0; j < n_reloads; j++)
1269 if ((CLASS_MAX_NREGS (reload_reg_class[j],
1270 (GET_MODE_SIZE (reload_outmode[j])
1271 > GET_MODE_SIZE (reload_inmode[j]))
1272 ? reload_outmode[j]
1273 : reload_inmode[j])
1274 > 1)
1275 && reg_classes_intersect_p (caller_save_spill_class,
1276 reload_reg_class[j]))
1278 nongroup_need = 1;
1279 break;
1282 caller_save_needs
1283 = (caller_save_group_size > 1
1284 ? insn_needs.other.groups
1285 : insn_needs.other.regs[nongroup_need]);
1287 if (caller_save_needs[(int) caller_save_spill_class] == 0)
1289 register enum reg_class *p
1290 = reg_class_superclasses[(int) caller_save_spill_class];
1292 caller_save_needs[(int) caller_save_spill_class]++;
1294 while (*p != LIM_REG_CLASSES)
1295 caller_save_needs[(int) *p++] += 1;
1298 /* Show that this basic block will need a register of
1299 this class. */
1301 if (global
1302 && ! (basic_block_needs[(int) caller_save_spill_class]
1303 [this_block]))
1305 basic_block_needs[(int) caller_save_spill_class]
1306 [this_block] = 1;
1307 new_basic_block_needs = 1;
1311 #ifdef SMALL_REGISTER_CLASSES
1312 /* If this insn stores the value of a function call,
1313 and that value is in a register that has been spilled,
1314 and if the insn needs a reload in a class
1315 that might use that register as the reload register,
1316 then add add an extra need in that class.
1317 This makes sure we have a register available that does
1318 not overlap the return value. */
1320 if (avoid_return_reg)
1322 int regno = REGNO (avoid_return_reg);
1323 int nregs
1324 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1325 int r;
1326 int basic_needs[N_REG_CLASSES], basic_groups[N_REG_CLASSES];
1328 /* First compute the "basic needs", which counts a
1329 need only in the smallest class in which it
1330 is required. */
1332 bcopy ((char *) insn_needs.other.regs[0],
1333 (char *) basic_needs, sizeof basic_needs);
1334 bcopy ((char *) insn_needs.other.groups,
1335 (char *) basic_groups, sizeof basic_groups);
1337 for (i = 0; i < N_REG_CLASSES; i++)
1339 enum reg_class *p;
1341 if (basic_needs[i] >= 0)
1342 for (p = reg_class_superclasses[i];
1343 *p != LIM_REG_CLASSES; p++)
1344 basic_needs[(int) *p] -= basic_needs[i];
1346 if (basic_groups[i] >= 0)
1347 for (p = reg_class_superclasses[i];
1348 *p != LIM_REG_CLASSES; p++)
1349 basic_groups[(int) *p] -= basic_groups[i];
1352 /* Now count extra regs if there might be a conflict with
1353 the return value register. */
1355 for (r = regno; r < regno + nregs; r++)
1356 if (spill_reg_order[r] >= 0)
1357 for (i = 0; i < N_REG_CLASSES; i++)
1358 if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1360 if (basic_needs[i] > 0)
1362 enum reg_class *p;
1364 insn_needs.other.regs[0][i]++;
1365 p = reg_class_superclasses[i];
1366 while (*p != LIM_REG_CLASSES)
1367 insn_needs.other.regs[0][(int) *p++]++;
1369 if (basic_groups[i] > 0)
1371 enum reg_class *p;
1373 insn_needs.other.groups[i]++;
1374 p = reg_class_superclasses[i];
1375 while (*p != LIM_REG_CLASSES)
1376 insn_needs.other.groups[(int) *p++]++;
1380 #endif /* SMALL_REGISTER_CLASSES */
1382 /* For each class, collect maximum need of any insn. */
1384 for (i = 0; i < N_REG_CLASSES; i++)
1386 if (max_needs[i] < insn_needs.other.regs[0][i])
1388 max_needs[i] = insn_needs.other.regs[0][i];
1389 max_needs_insn[i] = insn;
1391 if (max_groups[i] < insn_needs.other.groups[i])
1393 max_groups[i] = insn_needs.other.groups[i];
1394 max_groups_insn[i] = insn;
1396 if (max_nongroups[i] < insn_needs.other.regs[1][i])
1398 max_nongroups[i] = insn_needs.other.regs[1][i];
1399 max_nongroups_insn[i] = insn;
1403 /* Note that there is a continue statement above. */
1406 /* If we allocated any new memory locations, make another pass
1407 since it might have changed elimination offsets. */
1408 if (starting_frame_size != get_frame_size ())
1409 something_changed = 1;
1411 if (dumpfile)
1412 for (i = 0; i < N_REG_CLASSES; i++)
1414 if (max_needs[i] > 0)
1415 fprintf (dumpfile,
1416 ";; Need %d reg%s of class %s (for insn %d).\n",
1417 max_needs[i], max_needs[i] == 1 ? "" : "s",
1418 reg_class_names[i], INSN_UID (max_needs_insn[i]));
1419 if (max_nongroups[i] > 0)
1420 fprintf (dumpfile,
1421 ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1422 max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1423 reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1424 if (max_groups[i] > 0)
1425 fprintf (dumpfile,
1426 ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1427 max_groups[i], max_groups[i] == 1 ? "" : "s",
1428 mode_name[(int) group_mode[i]],
1429 reg_class_names[i], INSN_UID (max_groups_insn[i]));
1432 /* If we have caller-saves, set up the save areas and see if caller-save
1433 will need a spill register. */
1435 if (caller_save_needed
1436 && ! setup_save_areas (&something_changed)
1437 && caller_save_spill_class == NO_REGS)
1439 /* The class we will need depends on whether the machine
1440 supports the sum of two registers for an address; see
1441 find_address_reloads for details. */
1443 caller_save_spill_class
1444 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1445 caller_save_group_size
1446 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1447 something_changed = 1;
1450 /* See if anything that happened changes which eliminations are valid.
1451 For example, on the Sparc, whether or not the frame pointer can
1452 be eliminated can depend on what registers have been used. We need
1453 not check some conditions again (such as flag_omit_frame_pointer)
1454 since they can't have changed. */
1456 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1457 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
1458 #ifdef ELIMINABLE_REGS
1459 || ! CAN_ELIMINATE (ep->from, ep->to)
1460 #endif
1462 ep->can_eliminate = 0;
1464 /* Look for the case where we have discovered that we can't replace
1465 register A with register B and that means that we will now be
1466 trying to replace register A with register C. This means we can
1467 no longer replace register C with register B and we need to disable
1468 such an elimination, if it exists. This occurs often with A == ap,
1469 B == sp, and C == fp. */
1471 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1473 struct elim_table *op;
1474 register int new_to = -1;
1476 if (! ep->can_eliminate && ep->can_eliminate_previous)
1478 /* Find the current elimination for ep->from, if there is a
1479 new one. */
1480 for (op = reg_eliminate;
1481 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1482 if (op->from == ep->from && op->can_eliminate)
1484 new_to = op->to;
1485 break;
1488 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
1489 disable it. */
1490 for (op = reg_eliminate;
1491 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1492 if (op->from == new_to && op->to == ep->to)
1493 op->can_eliminate = 0;
1497 /* See if any registers that we thought we could eliminate the previous
1498 time are no longer eliminable. If so, something has changed and we
1499 must spill the register. Also, recompute the number of eliminable
1500 registers and see if the frame pointer is needed; it is if there is
1501 no elimination of the frame pointer that we can perform. */
1503 frame_pointer_needed = 1;
1504 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1506 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
1507 && ep->to != HARD_FRAME_POINTER_REGNUM)
1508 frame_pointer_needed = 0;
1510 if (! ep->can_eliminate && ep->can_eliminate_previous)
1512 ep->can_eliminate_previous = 0;
1513 spill_hard_reg (ep->from, global, dumpfile, 1);
1514 something_changed = 1;
1515 num_eliminable--;
1519 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1520 /* If we didn't need a frame pointer last time, but we do now, spill
1521 the hard frame pointer. */
1522 if (frame_pointer_needed && ! previous_frame_pointer_needed)
1524 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
1525 something_changed = 1;
1527 #endif
1529 /* If all needs are met, we win. */
1531 for (i = 0; i < N_REG_CLASSES; i++)
1532 if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1533 break;
1534 if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1535 break;
1537 /* Not all needs are met; must spill some hard regs. */
1539 /* Put all registers spilled so far back in potential_reload_regs, but
1540 put them at the front, since we've already spilled most of the
1541 psuedos in them (we might have left some pseudos unspilled if they
1542 were in a block that didn't need any spill registers of a conflicting
1543 class. We used to try to mark off the need for those registers,
1544 but doing so properly is very complex and reallocating them is the
1545 simpler approach. First, "pack" potential_reload_regs by pushing
1546 any nonnegative entries towards the end. That will leave room
1547 for the registers we already spilled.
1549 Also, undo the marking of the spill registers from the last time
1550 around in FORBIDDEN_REGS since we will be probably be allocating
1551 them again below.
1553 ??? It is theoretically possible that we might end up not using one
1554 of our previously-spilled registers in this allocation, even though
1555 they are at the head of the list. It's not clear what to do about
1556 this, but it was no better before, when we marked off the needs met
1557 by the previously-spilled registers. With the current code, globals
1558 can be allocated into these registers, but locals cannot. */
1560 if (n_spills)
1562 for (i = j = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1563 if (potential_reload_regs[i] != -1)
1564 potential_reload_regs[j--] = potential_reload_regs[i];
1566 for (i = 0; i < n_spills; i++)
1568 potential_reload_regs[i] = spill_regs[i];
1569 spill_reg_order[spill_regs[i]] = -1;
1570 CLEAR_HARD_REG_BIT (forbidden_regs, spill_regs[i]);
1573 n_spills = 0;
1576 /* Now find more reload regs to satisfy the remaining need
1577 Do it by ascending class number, since otherwise a reg
1578 might be spilled for a big class and might fail to count
1579 for a smaller class even though it belongs to that class.
1581 Count spilled regs in `spills', and add entries to
1582 `spill_regs' and `spill_reg_order'.
1584 ??? Note there is a problem here.
1585 When there is a need for a group in a high-numbered class,
1586 and also need for non-group regs that come from a lower class,
1587 the non-group regs are chosen first. If there aren't many regs,
1588 they might leave no room for a group.
1590 This was happening on the 386. To fix it, we added the code
1591 that calls possible_group_p, so that the lower class won't
1592 break up the last possible group.
1594 Really fixing the problem would require changes above
1595 in counting the regs already spilled, and in choose_reload_regs.
1596 It might be hard to avoid introducing bugs there. */
1598 CLEAR_HARD_REG_SET (counted_for_groups);
1599 CLEAR_HARD_REG_SET (counted_for_nongroups);
1601 for (class = 0; class < N_REG_CLASSES; class++)
1603 /* First get the groups of registers.
1604 If we got single registers first, we might fragment
1605 possible groups. */
1606 while (max_groups[class] > 0)
1608 /* If any single spilled regs happen to form groups,
1609 count them now. Maybe we don't really need
1610 to spill another group. */
1611 count_possible_groups (group_size, group_mode, max_groups,
1612 class);
1614 if (max_groups[class] <= 0)
1615 break;
1617 /* Groups of size 2 (the only groups used on most machines)
1618 are treated specially. */
1619 if (group_size[class] == 2)
1621 /* First, look for a register that will complete a group. */
1622 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1624 int other;
1626 j = potential_reload_regs[i];
1627 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1629 ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1630 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1631 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1632 && HARD_REGNO_MODE_OK (other, group_mode[class])
1633 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1634 other)
1635 /* We don't want one part of another group.
1636 We could get "two groups" that overlap! */
1637 && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1639 (j < FIRST_PSEUDO_REGISTER - 1
1640 && (other = j + 1, spill_reg_order[other] >= 0)
1641 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1642 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1643 && HARD_REGNO_MODE_OK (j, group_mode[class])
1644 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1645 other)
1646 && ! TEST_HARD_REG_BIT (counted_for_groups,
1647 other))))
1649 register enum reg_class *p;
1651 /* We have found one that will complete a group,
1652 so count off one group as provided. */
1653 max_groups[class]--;
1654 p = reg_class_superclasses[class];
1655 while (*p != LIM_REG_CLASSES)
1657 if (group_size [(int) *p] <= group_size [class])
1658 max_groups[(int) *p]--;
1659 p++;
1662 /* Indicate both these regs are part of a group. */
1663 SET_HARD_REG_BIT (counted_for_groups, j);
1664 SET_HARD_REG_BIT (counted_for_groups, other);
1665 break;
1668 /* We can't complete a group, so start one. */
1669 #ifdef SMALL_REGISTER_CLASSES
1670 /* Look for a pair neither of which is explicitly used. */
1671 if (i == FIRST_PSEUDO_REGISTER)
1672 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1674 int k;
1675 j = potential_reload_regs[i];
1676 /* Verify that J+1 is a potential reload reg. */
1677 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1678 if (potential_reload_regs[k] == j + 1)
1679 break;
1680 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1681 && k < FIRST_PSEUDO_REGISTER
1682 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1683 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1684 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1685 && HARD_REGNO_MODE_OK (j, group_mode[class])
1686 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1687 j + 1)
1688 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1)
1689 /* Reject J at this stage
1690 if J+1 was explicitly used. */
1691 && ! regs_explicitly_used[j + 1])
1692 break;
1694 #endif
1695 /* Now try any group at all
1696 whose registers are not in bad_spill_regs. */
1697 if (i == FIRST_PSEUDO_REGISTER)
1698 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1700 int k;
1701 j = potential_reload_regs[i];
1702 /* Verify that J+1 is a potential reload reg. */
1703 for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1704 if (potential_reload_regs[k] == j + 1)
1705 break;
1706 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1707 && k < FIRST_PSEUDO_REGISTER
1708 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1709 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1710 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1711 && HARD_REGNO_MODE_OK (j, group_mode[class])
1712 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1713 j + 1)
1714 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1715 break;
1718 /* I should be the index in potential_reload_regs
1719 of the new reload reg we have found. */
1721 if (i >= FIRST_PSEUDO_REGISTER)
1723 /* There are no groups left to spill. */
1724 spill_failure (max_groups_insn[class]);
1725 failure = 1;
1726 goto failed;
1728 else
1729 something_changed
1730 |= new_spill_reg (i, class, max_needs, NULL_PTR,
1731 global, dumpfile);
1733 else
1735 /* For groups of more than 2 registers,
1736 look for a sufficient sequence of unspilled registers,
1737 and spill them all at once. */
1738 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1740 int k;
1742 j = potential_reload_regs[i];
1743 if (j >= 0
1744 && j + group_size[class] <= FIRST_PSEUDO_REGISTER
1745 && HARD_REGNO_MODE_OK (j, group_mode[class]))
1747 /* Check each reg in the sequence. */
1748 for (k = 0; k < group_size[class]; k++)
1749 if (! (spill_reg_order[j + k] < 0
1750 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1751 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1752 break;
1753 /* We got a full sequence, so spill them all. */
1754 if (k == group_size[class])
1756 register enum reg_class *p;
1757 for (k = 0; k < group_size[class]; k++)
1759 int idx;
1760 SET_HARD_REG_BIT (counted_for_groups, j + k);
1761 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1762 if (potential_reload_regs[idx] == j + k)
1763 break;
1764 something_changed
1765 |= new_spill_reg (idx, class,
1766 max_needs, NULL_PTR,
1767 global, dumpfile);
1770 /* We have found one that will complete a group,
1771 so count off one group as provided. */
1772 max_groups[class]--;
1773 p = reg_class_superclasses[class];
1774 while (*p != LIM_REG_CLASSES)
1776 if (group_size [(int) *p]
1777 <= group_size [class])
1778 max_groups[(int) *p]--;
1779 p++;
1781 break;
1785 /* We couldn't find any registers for this reload.
1786 Avoid going into an infinite loop. */
1787 if (i >= FIRST_PSEUDO_REGISTER)
1789 /* There are no groups left. */
1790 spill_failure (max_groups_insn[class]);
1791 failure = 1;
1792 goto failed;
1797 /* Now similarly satisfy all need for single registers. */
1799 while (max_needs[class] > 0 || max_nongroups[class] > 0)
1801 #ifdef SMALL_REGISTER_CLASSES
1802 /* This should be right for all machines, but only the 386
1803 is known to need it, so this conditional plays safe.
1804 ??? For 2.5, try making this unconditional. */
1805 /* If we spilled enough regs, but they weren't counted
1806 against the non-group need, see if we can count them now.
1807 If so, we can avoid some actual spilling. */
1808 if (max_needs[class] <= 0 && max_nongroups[class] > 0)
1809 for (i = 0; i < n_spills; i++)
1810 if (TEST_HARD_REG_BIT (reg_class_contents[class],
1811 spill_regs[i])
1812 && !TEST_HARD_REG_BIT (counted_for_groups,
1813 spill_regs[i])
1814 && !TEST_HARD_REG_BIT (counted_for_nongroups,
1815 spill_regs[i])
1816 && max_nongroups[class] > 0)
1818 register enum reg_class *p;
1820 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1821 max_nongroups[class]--;
1822 p = reg_class_superclasses[class];
1823 while (*p != LIM_REG_CLASSES)
1824 max_nongroups[(int) *p++]--;
1826 if (max_needs[class] <= 0 && max_nongroups[class] <= 0)
1827 break;
1828 #endif
1830 /* Consider the potential reload regs that aren't
1831 yet in use as reload regs, in order of preference.
1832 Find the most preferred one that's in this class. */
1834 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1835 if (potential_reload_regs[i] >= 0
1836 && TEST_HARD_REG_BIT (reg_class_contents[class],
1837 potential_reload_regs[i])
1838 /* If this reg will not be available for groups,
1839 pick one that does not foreclose possible groups.
1840 This is a kludge, and not very general,
1841 but it should be sufficient to make the 386 work,
1842 and the problem should not occur on machines with
1843 more registers. */
1844 && (max_nongroups[class] == 0
1845 || possible_group_p (potential_reload_regs[i], max_groups)))
1846 break;
1848 /* If we couldn't get a register, try to get one even if we
1849 might foreclose possible groups. This may cause problems
1850 later, but that's better than aborting now, since it is
1851 possible that we will, in fact, be able to form the needed
1852 group even with this allocation. */
1854 if (i >= FIRST_PSEUDO_REGISTER
1855 && (asm_noperands (max_needs[class] > 0
1856 ? max_needs_insn[class]
1857 : max_nongroups_insn[class])
1858 < 0))
1859 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1860 if (potential_reload_regs[i] >= 0
1861 && TEST_HARD_REG_BIT (reg_class_contents[class],
1862 potential_reload_regs[i]))
1863 break;
1865 /* I should be the index in potential_reload_regs
1866 of the new reload reg we have found. */
1868 if (i >= FIRST_PSEUDO_REGISTER)
1870 /* There are no possible registers left to spill. */
1871 spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
1872 : max_nongroups_insn[class]);
1873 failure = 1;
1874 goto failed;
1876 else
1877 something_changed
1878 |= new_spill_reg (i, class, max_needs, max_nongroups,
1879 global, dumpfile);
1884 /* If global-alloc was run, notify it of any register eliminations we have
1885 done. */
1886 if (global)
1887 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1888 if (ep->can_eliminate)
1889 mark_elimination (ep->from, ep->to);
1891 /* Insert code to save and restore call-clobbered hard regs
1892 around calls. Tell if what mode to use so that we will process
1893 those insns in reload_as_needed if we have to. */
1895 if (caller_save_needed)
1896 save_call_clobbered_regs (num_eliminable ? QImode
1897 : caller_save_spill_class != NO_REGS ? HImode
1898 : VOIDmode);
1900 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1901 If that insn didn't set the register (i.e., it copied the register to
1902 memory), just delete that insn instead of the equivalencing insn plus
1903 anything now dead. If we call delete_dead_insn on that insn, we may
1904 delete the insn that actually sets the register if the register die
1905 there and that is incorrect. */
1907 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1908 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
1909 && GET_CODE (reg_equiv_init[i]) != NOTE)
1911 if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
1912 delete_dead_insn (reg_equiv_init[i]);
1913 else
1915 PUT_CODE (reg_equiv_init[i], NOTE);
1916 NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
1917 NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
1921 /* Use the reload registers where necessary
1922 by generating move instructions to move the must-be-register
1923 values into or out of the reload registers. */
1925 if (something_needs_reloads || something_needs_elimination
1926 || (caller_save_needed && num_eliminable)
1927 || caller_save_spill_class != NO_REGS)
1928 reload_as_needed (first, global);
1930 /* If we were able to eliminate the frame pointer, show that it is no
1931 longer live at the start of any basic block. If it ls live by
1932 virtue of being in a pseudo, that pseudo will be marked live
1933 and hence the frame pointer will be known to be live via that
1934 pseudo. */
1936 if (! frame_pointer_needed)
1937 for (i = 0; i < n_basic_blocks; i++)
1938 basic_block_live_at_start[i][HARD_FRAME_POINTER_REGNUM / REGSET_ELT_BITS]
1939 &= ~ ((REGSET_ELT_TYPE) 1 << (HARD_FRAME_POINTER_REGNUM
1940 % REGSET_ELT_BITS));
1942 /* Come here (with failure set nonzero) if we can't get enough spill regs
1943 and we decide not to abort about it. */
1944 failed:
1946 reload_in_progress = 0;
1948 /* Now eliminate all pseudo regs by modifying them into
1949 their equivalent memory references.
1950 The REG-rtx's for the pseudos are modified in place,
1951 so all insns that used to refer to them now refer to memory.
1953 For a reg that has a reg_equiv_address, all those insns
1954 were changed by reloading so that no insns refer to it any longer;
1955 but the DECL_RTL of a variable decl may refer to it,
1956 and if so this causes the debugging info to mention the variable. */
1958 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1960 rtx addr = 0;
1961 int in_struct = 0;
1962 if (reg_equiv_mem[i])
1964 addr = XEXP (reg_equiv_mem[i], 0);
1965 in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
1967 if (reg_equiv_address[i])
1968 addr = reg_equiv_address[i];
1969 if (addr)
1971 if (reg_renumber[i] < 0)
1973 rtx reg = regno_reg_rtx[i];
1974 XEXP (reg, 0) = addr;
1975 REG_USERVAR_P (reg) = 0;
1976 MEM_IN_STRUCT_P (reg) = in_struct;
1977 PUT_CODE (reg, MEM);
1979 else if (reg_equiv_mem[i])
1980 XEXP (reg_equiv_mem[i], 0) = addr;
1984 #ifdef PRESERVE_DEATH_INFO_REGNO_P
1985 /* Make a pass over all the insns and remove death notes for things that
1986 are no longer registers or no longer die in the insn (e.g., an input
1987 and output pseudo being tied). */
1989 for (insn = first; insn; insn = NEXT_INSN (insn))
1990 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1992 rtx note, next;
1994 for (note = REG_NOTES (insn); note; note = next)
1996 next = XEXP (note, 1);
1997 if (REG_NOTE_KIND (note) == REG_DEAD
1998 && (GET_CODE (XEXP (note, 0)) != REG
1999 || reg_set_p (XEXP (note, 0), PATTERN (insn))))
2000 remove_note (insn, note);
2003 #endif
2005 /* Indicate that we no longer have known memory locations or constants. */
2006 reg_equiv_constant = 0;
2007 reg_equiv_memory_loc = 0;
2009 if (scratch_list)
2010 free (scratch_list);
2011 scratch_list = 0;
2012 if (scratch_block)
2013 free (scratch_block);
2014 scratch_block = 0;
2016 return failure;
2019 /* Nonzero if, after spilling reg REGNO for non-groups,
2020 it will still be possible to find a group if we still need one. */
2022 static int
2023 possible_group_p (regno, max_groups)
2024 int regno;
2025 int *max_groups;
2027 int i;
2028 int class = (int) NO_REGS;
2030 for (i = 0; i < (int) N_REG_CLASSES; i++)
2031 if (max_groups[i] > 0)
2033 class = i;
2034 break;
2037 if (class == (int) NO_REGS)
2038 return 1;
2040 /* Consider each pair of consecutive registers. */
2041 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2043 /* Ignore pairs that include reg REGNO. */
2044 if (i == regno || i + 1 == regno)
2045 continue;
2047 /* Ignore pairs that are outside the class that needs the group.
2048 ??? Here we fail to handle the case where two different classes
2049 independently need groups. But this never happens with our
2050 current machine descriptions. */
2051 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2052 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2053 continue;
2055 /* A pair of consecutive regs we can still spill does the trick. */
2056 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2057 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2058 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2059 return 1;
2061 /* A pair of one already spilled and one we can spill does it
2062 provided the one already spilled is not otherwise reserved. */
2063 if (spill_reg_order[i] < 0
2064 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2065 && spill_reg_order[i + 1] >= 0
2066 && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
2067 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
2068 return 1;
2069 if (spill_reg_order[i + 1] < 0
2070 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2071 && spill_reg_order[i] >= 0
2072 && ! TEST_HARD_REG_BIT (counted_for_groups, i)
2073 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
2074 return 1;
2077 return 0;
2080 /* Count any groups of CLASS that can be formed from the registers recently
2081 spilled. */
2083 static void
2084 count_possible_groups (group_size, group_mode, max_groups, class)
2085 int *group_size;
2086 enum machine_mode *group_mode;
2087 int *max_groups;
2088 int class;
2090 HARD_REG_SET new;
2091 int i, j;
2093 /* Now find all consecutive groups of spilled registers
2094 and mark each group off against the need for such groups.
2095 But don't count them against ordinary need, yet. */
2097 if (group_size[class] == 0)
2098 return;
2100 CLEAR_HARD_REG_SET (new);
2102 /* Make a mask of all the regs that are spill regs in class I. */
2103 for (i = 0; i < n_spills; i++)
2104 if (TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
2105 && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i])
2106 && ! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
2107 SET_HARD_REG_BIT (new, spill_regs[i]);
2109 /* Find each consecutive group of them. */
2110 for (i = 0; i < FIRST_PSEUDO_REGISTER && max_groups[class] > 0; i++)
2111 if (TEST_HARD_REG_BIT (new, i)
2112 && i + group_size[class] <= FIRST_PSEUDO_REGISTER
2113 && HARD_REGNO_MODE_OK (i, group_mode[class]))
2115 for (j = 1; j < group_size[class]; j++)
2116 if (! TEST_HARD_REG_BIT (new, i + j))
2117 break;
2119 if (j == group_size[class])
2121 /* We found a group. Mark it off against this class's need for
2122 groups, and against each superclass too. */
2123 register enum reg_class *p;
2125 max_groups[class]--;
2126 p = reg_class_superclasses[class];
2127 while (*p != LIM_REG_CLASSES)
2129 if (group_size [(int) *p] <= group_size [class])
2130 max_groups[(int) *p]--;
2131 p++;
2134 /* Don't count these registers again. */
2135 for (j = 0; j < group_size[class]; j++)
2136 SET_HARD_REG_BIT (counted_for_groups, i + j);
2139 /* Skip to the last reg in this group. When i is incremented above,
2140 it will then point to the first reg of the next possible group. */
2141 i += j - 1;
2145 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2146 another mode that needs to be reloaded for the same register class CLASS.
2147 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2148 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2150 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2151 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2152 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2153 causes unnecessary failures on machines requiring alignment of register
2154 groups when the two modes are different sizes, because the larger mode has
2155 more strict alignment rules than the smaller mode. */
2157 static int
2158 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2159 enum machine_mode allocate_mode, other_mode;
2160 enum reg_class class;
2162 register int regno;
2163 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2165 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2166 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2167 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2168 return 0;
2170 return 1;
2173 /* Handle the failure to find a register to spill.
2174 INSN should be one of the insns which needed this particular spill reg. */
2176 static void
2177 spill_failure (insn)
2178 rtx insn;
2180 if (asm_noperands (PATTERN (insn)) >= 0)
2181 error_for_asm (insn, "`asm' needs too many reloads");
2182 else
2183 fatal_insn ("Unable to find a register to spill.", insn);
2186 /* Add a new register to the tables of available spill-registers
2187 (as well as spilling all pseudos allocated to the register).
2188 I is the index of this register in potential_reload_regs.
2189 CLASS is the regclass whose need is being satisfied.
2190 MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2191 so that this register can count off against them.
2192 MAX_NONGROUPS is 0 if this register is part of a group.
2193 GLOBAL and DUMPFILE are the same as the args that `reload' got. */
2195 static int
2196 new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2197 int i;
2198 int class;
2199 int *max_needs;
2200 int *max_nongroups;
2201 int global;
2202 FILE *dumpfile;
2204 register enum reg_class *p;
2205 int val;
2206 int regno = potential_reload_regs[i];
2208 if (i >= FIRST_PSEUDO_REGISTER)
2209 abort (); /* Caller failed to find any register. */
2211 if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
2212 fatal ("fixed or forbidden register was spilled.\n\
2213 This may be due to a compiler bug or to impossible asm\n\
2214 statements or clauses.");
2216 /* Make reg REGNO an additional reload reg. */
2218 potential_reload_regs[i] = -1;
2219 spill_regs[n_spills] = regno;
2220 spill_reg_order[regno] = n_spills;
2221 if (dumpfile)
2222 fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2224 /* Clear off the needs we just satisfied. */
2226 max_needs[class]--;
2227 p = reg_class_superclasses[class];
2228 while (*p != LIM_REG_CLASSES)
2229 max_needs[(int) *p++]--;
2231 if (max_nongroups && max_nongroups[class] > 0)
2233 SET_HARD_REG_BIT (counted_for_nongroups, regno);
2234 max_nongroups[class]--;
2235 p = reg_class_superclasses[class];
2236 while (*p != LIM_REG_CLASSES)
2237 max_nongroups[(int) *p++]--;
2240 /* Spill every pseudo reg that was allocated to this reg
2241 or to something that overlaps this reg. */
2243 val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2245 /* If there are some registers still to eliminate and this register
2246 wasn't ever used before, additional stack space may have to be
2247 allocated to store this register. Thus, we may have changed the offset
2248 between the stack and frame pointers, so mark that something has changed.
2249 (If new pseudos were spilled, thus requiring more space, VAL would have
2250 been set non-zero by the call to spill_hard_reg above since additional
2251 reloads may be needed in that case.
2253 One might think that we need only set VAL to 1 if this is a call-used
2254 register. However, the set of registers that must be saved by the
2255 prologue is not identical to the call-used set. For example, the
2256 register used by the call insn for the return PC is a call-used register,
2257 but must be saved by the prologue. */
2258 if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2259 val = 1;
2261 regs_ever_live[spill_regs[n_spills]] = 1;
2262 n_spills++;
2264 return val;
2267 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2268 data that is dead in INSN. */
2270 static void
2271 delete_dead_insn (insn)
2272 rtx insn;
2274 rtx prev = prev_real_insn (insn);
2275 rtx prev_dest;
2277 /* If the previous insn sets a register that dies in our insn, delete it
2278 too. */
2279 if (prev && GET_CODE (PATTERN (prev)) == SET
2280 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2281 && reg_mentioned_p (prev_dest, PATTERN (insn))
2282 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2283 delete_dead_insn (prev);
2285 PUT_CODE (insn, NOTE);
2286 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2287 NOTE_SOURCE_FILE (insn) = 0;
2290 /* Modify the home of pseudo-reg I.
2291 The new home is present in reg_renumber[I].
2293 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2294 or it may be -1, meaning there is none or it is not relevant.
2295 This is used so that all pseudos spilled from a given hard reg
2296 can share one stack slot. */
2298 static void
2299 alter_reg (i, from_reg)
2300 register int i;
2301 int from_reg;
2303 /* When outputting an inline function, this can happen
2304 for a reg that isn't actually used. */
2305 if (regno_reg_rtx[i] == 0)
2306 return;
2308 /* If the reg got changed to a MEM at rtl-generation time,
2309 ignore it. */
2310 if (GET_CODE (regno_reg_rtx[i]) != REG)
2311 return;
2313 /* Modify the reg-rtx to contain the new hard reg
2314 number or else to contain its pseudo reg number. */
2315 REGNO (regno_reg_rtx[i])
2316 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2318 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2319 allocate a stack slot for it. */
2321 if (reg_renumber[i] < 0
2322 && reg_n_refs[i] > 0
2323 && reg_equiv_constant[i] == 0
2324 && reg_equiv_memory_loc[i] == 0)
2326 register rtx x;
2327 int inherent_size = PSEUDO_REGNO_BYTES (i);
2328 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2329 int adjust = 0;
2331 /* Each pseudo reg has an inherent size which comes from its own mode,
2332 and a total size which provides room for paradoxical subregs
2333 which refer to the pseudo reg in wider modes.
2335 We can use a slot already allocated if it provides both
2336 enough inherent space and enough total space.
2337 Otherwise, we allocate a new slot, making sure that it has no less
2338 inherent space, and no less total space, then the previous slot. */
2339 if (from_reg == -1)
2341 /* No known place to spill from => no slot to reuse. */
2342 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size, -1);
2343 if (BYTES_BIG_ENDIAN)
2345 /* Cancel the big-endian correction done in assign_stack_local.
2346 Get the address of the beginning of the slot.
2347 This is so we can do a big-endian correction unconditionally
2348 below. */
2349 adjust = inherent_size - total_size;
2352 /* Reuse a stack slot if possible. */
2353 else if (spill_stack_slot[from_reg] != 0
2354 && spill_stack_slot_width[from_reg] >= total_size
2355 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2356 >= inherent_size))
2357 x = spill_stack_slot[from_reg];
2358 /* Allocate a bigger slot. */
2359 else
2361 /* Compute maximum size needed, both for inherent size
2362 and for total size. */
2363 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2364 if (spill_stack_slot[from_reg])
2366 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2367 > inherent_size)
2368 mode = GET_MODE (spill_stack_slot[from_reg]);
2369 if (spill_stack_slot_width[from_reg] > total_size)
2370 total_size = spill_stack_slot_width[from_reg];
2372 /* Make a slot with that size. */
2373 x = assign_stack_local (mode, total_size, -1);
2374 if (BYTES_BIG_ENDIAN)
2376 /* Cancel the big-endian correction done in assign_stack_local.
2377 Get the address of the beginning of the slot.
2378 This is so we can do a big-endian correction unconditionally
2379 below. */
2380 adjust = GET_MODE_SIZE (mode) - total_size;
2382 spill_stack_slot[from_reg] = x;
2383 spill_stack_slot_width[from_reg] = total_size;
2386 /* On a big endian machine, the "address" of the slot
2387 is the address of the low part that fits its inherent mode. */
2388 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2389 adjust += (total_size - inherent_size);
2391 /* If we have any adjustment to make, or if the stack slot is the
2392 wrong mode, make a new stack slot. */
2393 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2395 x = gen_rtx (MEM, GET_MODE (regno_reg_rtx[i]),
2396 plus_constant (XEXP (x, 0), adjust));
2397 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2400 /* Save the stack slot for later. */
2401 reg_equiv_memory_loc[i] = x;
2405 /* Mark the slots in regs_ever_live for the hard regs
2406 used by pseudo-reg number REGNO. */
2408 void
2409 mark_home_live (regno)
2410 int regno;
2412 register int i, lim;
2413 i = reg_renumber[regno];
2414 if (i < 0)
2415 return;
2416 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2417 while (i < lim)
2418 regs_ever_live[i++] = 1;
2421 /* Mark the registers used in SCRATCH as being live. */
2423 static void
2424 mark_scratch_live (scratch)
2425 rtx scratch;
2427 register int i;
2428 int regno = REGNO (scratch);
2429 int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch));
2431 for (i = regno; i < lim; i++)
2432 regs_ever_live[i] = 1;
2435 /* This function handles the tracking of elimination offsets around branches.
2437 X is a piece of RTL being scanned.
2439 INSN is the insn that it came from, if any.
2441 INITIAL_P is non-zero if we are to set the offset to be the initial
2442 offset and zero if we are setting the offset of the label to be the
2443 current offset. */
2445 static void
2446 set_label_offsets (x, insn, initial_p)
2447 rtx x;
2448 rtx insn;
2449 int initial_p;
2451 enum rtx_code code = GET_CODE (x);
2452 rtx tem;
2453 int i;
2454 struct elim_table *p;
2456 switch (code)
2458 case LABEL_REF:
2459 if (LABEL_REF_NONLOCAL_P (x))
2460 return;
2462 x = XEXP (x, 0);
2464 /* ... fall through ... */
2466 case CODE_LABEL:
2467 /* If we know nothing about this label, set the desired offsets. Note
2468 that this sets the offset at a label to be the offset before a label
2469 if we don't know anything about the label. This is not correct for
2470 the label after a BARRIER, but is the best guess we can make. If
2471 we guessed wrong, we will suppress an elimination that might have
2472 been possible had we been able to guess correctly. */
2474 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2476 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2477 offsets_at[CODE_LABEL_NUMBER (x)][i]
2478 = (initial_p ? reg_eliminate[i].initial_offset
2479 : reg_eliminate[i].offset);
2480 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2483 /* Otherwise, if this is the definition of a label and it is
2484 preceded by a BARRIER, set our offsets to the known offset of
2485 that label. */
2487 else if (x == insn
2488 && (tem = prev_nonnote_insn (insn)) != 0
2489 && GET_CODE (tem) == BARRIER)
2491 num_not_at_initial_offset = 0;
2492 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2494 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2495 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2496 if (reg_eliminate[i].can_eliminate
2497 && (reg_eliminate[i].offset
2498 != reg_eliminate[i].initial_offset))
2499 num_not_at_initial_offset++;
2503 else
2504 /* If neither of the above cases is true, compare each offset
2505 with those previously recorded and suppress any eliminations
2506 where the offsets disagree. */
2508 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2509 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2510 != (initial_p ? reg_eliminate[i].initial_offset
2511 : reg_eliminate[i].offset))
2512 reg_eliminate[i].can_eliminate = 0;
2514 return;
2516 case JUMP_INSN:
2517 set_label_offsets (PATTERN (insn), insn, initial_p);
2519 /* ... fall through ... */
2521 case INSN:
2522 case CALL_INSN:
2523 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2524 and hence must have all eliminations at their initial offsets. */
2525 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2526 if (REG_NOTE_KIND (tem) == REG_LABEL)
2527 set_label_offsets (XEXP (tem, 0), insn, 1);
2528 return;
2530 case ADDR_VEC:
2531 case ADDR_DIFF_VEC:
2532 /* Each of the labels in the address vector must be at their initial
2533 offsets. We want the first first for ADDR_VEC and the second
2534 field for ADDR_DIFF_VEC. */
2536 for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2537 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2538 insn, initial_p);
2539 return;
2541 case SET:
2542 /* We only care about setting PC. If the source is not RETURN,
2543 IF_THEN_ELSE, or a label, disable any eliminations not at
2544 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2545 isn't one of those possibilities. For branches to a label,
2546 call ourselves recursively.
2548 Note that this can disable elimination unnecessarily when we have
2549 a non-local goto since it will look like a non-constant jump to
2550 someplace in the current function. This isn't a significant
2551 problem since such jumps will normally be when all elimination
2552 pairs are back to their initial offsets. */
2554 if (SET_DEST (x) != pc_rtx)
2555 return;
2557 switch (GET_CODE (SET_SRC (x)))
2559 case PC:
2560 case RETURN:
2561 return;
2563 case LABEL_REF:
2564 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2565 return;
2567 case IF_THEN_ELSE:
2568 tem = XEXP (SET_SRC (x), 1);
2569 if (GET_CODE (tem) == LABEL_REF)
2570 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2571 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2572 break;
2574 tem = XEXP (SET_SRC (x), 2);
2575 if (GET_CODE (tem) == LABEL_REF)
2576 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2577 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2578 break;
2579 return;
2582 /* If we reach here, all eliminations must be at their initial
2583 offset because we are doing a jump to a variable address. */
2584 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2585 if (p->offset != p->initial_offset)
2586 p->can_eliminate = 0;
2590 /* Used for communication between the next two function to properly share
2591 the vector for an ASM_OPERANDS. */
2593 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2595 /* Scan X and replace any eliminable registers (such as fp) with a
2596 replacement (such as sp), plus an offset.
2598 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2599 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2600 MEM, we are allowed to replace a sum of a register and the constant zero
2601 with the register, which we cannot do outside a MEM. In addition, we need
2602 to record the fact that a register is referenced outside a MEM.
2604 If INSN is an insn, it is the insn containing X. If we replace a REG
2605 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2606 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2607 that the REG is being modified.
2609 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2610 That's used when we eliminate in expressions stored in notes.
2611 This means, do not set ref_outside_mem even if the reference
2612 is outside of MEMs.
2614 If we see a modification to a register we know about, take the
2615 appropriate action (see case SET, below).
2617 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2618 replacements done assuming all offsets are at their initial values. If
2619 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2620 encounter, return the actual location so that find_reloads will do
2621 the proper thing. */
2624 eliminate_regs (x, mem_mode, insn)
2625 rtx x;
2626 enum machine_mode mem_mode;
2627 rtx insn;
2629 enum rtx_code code = GET_CODE (x);
2630 struct elim_table *ep;
2631 int regno;
2632 rtx new;
2633 int i, j;
2634 char *fmt;
2635 int copied = 0;
2637 switch (code)
2639 case CONST_INT:
2640 case CONST_DOUBLE:
2641 case CONST:
2642 case SYMBOL_REF:
2643 case CODE_LABEL:
2644 case PC:
2645 case CC0:
2646 case ASM_INPUT:
2647 case ADDR_VEC:
2648 case ADDR_DIFF_VEC:
2649 case RETURN:
2650 return x;
2652 case REG:
2653 regno = REGNO (x);
2655 /* First handle the case where we encounter a bare register that
2656 is eliminable. Replace it with a PLUS. */
2657 if (regno < FIRST_PSEUDO_REGISTER)
2659 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2660 ep++)
2661 if (ep->from_rtx == x && ep->can_eliminate)
2663 if (! mem_mode
2664 /* Refs inside notes don't count for this purpose. */
2665 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2666 || GET_CODE (insn) == INSN_LIST)))
2667 ep->ref_outside_mem = 1;
2668 return plus_constant (ep->to_rtx, ep->previous_offset);
2672 else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2673 && (reg_equiv_address[regno] || num_not_at_initial_offset))
2675 /* In this case, find_reloads would attempt to either use an
2676 incorrect address (if something is not at its initial offset)
2677 or substitute an replaced address into an insn (which loses
2678 if the offset is changed by some later action). So we simply
2679 return the replaced stack slot (assuming it is changed by
2680 elimination) and ignore the fact that this is actually a
2681 reference to the pseudo. Ensure we make a copy of the
2682 address in case it is shared. */
2683 new = eliminate_regs (reg_equiv_memory_loc[regno],
2684 mem_mode, insn);
2685 if (new != reg_equiv_memory_loc[regno])
2687 cannot_omit_stores[regno] = 1;
2688 return copy_rtx (new);
2691 return x;
2693 case PLUS:
2694 /* If this is the sum of an eliminable register and a constant, rework
2695 the sum. */
2696 if (GET_CODE (XEXP (x, 0)) == REG
2697 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2698 && CONSTANT_P (XEXP (x, 1)))
2700 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2701 ep++)
2702 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2704 if (! mem_mode
2705 /* Refs inside notes don't count for this purpose. */
2706 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2707 || GET_CODE (insn) == INSN_LIST)))
2708 ep->ref_outside_mem = 1;
2710 /* The only time we want to replace a PLUS with a REG (this
2711 occurs when the constant operand of the PLUS is the negative
2712 of the offset) is when we are inside a MEM. We won't want
2713 to do so at other times because that would change the
2714 structure of the insn in a way that reload can't handle.
2715 We special-case the commonest situation in
2716 eliminate_regs_in_insn, so just replace a PLUS with a
2717 PLUS here, unless inside a MEM. */
2718 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2719 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2720 return ep->to_rtx;
2721 else
2722 return gen_rtx (PLUS, Pmode, ep->to_rtx,
2723 plus_constant (XEXP (x, 1),
2724 ep->previous_offset));
2727 /* If the register is not eliminable, we are done since the other
2728 operand is a constant. */
2729 return x;
2732 /* If this is part of an address, we want to bring any constant to the
2733 outermost PLUS. We will do this by doing register replacement in
2734 our operands and seeing if a constant shows up in one of them.
2736 We assume here this is part of an address (or a "load address" insn)
2737 since an eliminable register is not likely to appear in any other
2738 context.
2740 If we have (plus (eliminable) (reg)), we want to produce
2741 (plus (plus (replacement) (reg) (const))). If this was part of a
2742 normal add insn, (plus (replacement) (reg)) will be pushed as a
2743 reload. This is the desired action. */
2746 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2747 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2749 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2751 /* If one side is a PLUS and the other side is a pseudo that
2752 didn't get a hard register but has a reg_equiv_constant,
2753 we must replace the constant here since it may no longer
2754 be in the position of any operand. */
2755 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2756 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2757 && reg_renumber[REGNO (new1)] < 0
2758 && reg_equiv_constant != 0
2759 && reg_equiv_constant[REGNO (new1)] != 0)
2760 new1 = reg_equiv_constant[REGNO (new1)];
2761 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2762 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2763 && reg_renumber[REGNO (new0)] < 0
2764 && reg_equiv_constant[REGNO (new0)] != 0)
2765 new0 = reg_equiv_constant[REGNO (new0)];
2767 new = form_sum (new0, new1);
2769 /* As above, if we are not inside a MEM we do not want to
2770 turn a PLUS into something else. We might try to do so here
2771 for an addition of 0 if we aren't optimizing. */
2772 if (! mem_mode && GET_CODE (new) != PLUS)
2773 return gen_rtx (PLUS, GET_MODE (x), new, const0_rtx);
2774 else
2775 return new;
2778 return x;
2780 case MULT:
2781 /* If this is the product of an eliminable register and a
2782 constant, apply the distribute law and move the constant out
2783 so that we have (plus (mult ..) ..). This is needed in order
2784 to keep load-address insns valid. This case is pathalogical.
2785 We ignore the possibility of overflow here. */
2786 if (GET_CODE (XEXP (x, 0)) == REG
2787 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2788 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2789 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2790 ep++)
2791 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2793 if (! mem_mode
2794 /* Refs inside notes don't count for this purpose. */
2795 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2796 || GET_CODE (insn) == INSN_LIST)))
2797 ep->ref_outside_mem = 1;
2799 return
2800 plus_constant (gen_rtx (MULT, Pmode, ep->to_rtx, XEXP (x, 1)),
2801 ep->previous_offset * INTVAL (XEXP (x, 1)));
2804 /* ... fall through ... */
2806 case CALL:
2807 case COMPARE:
2808 case MINUS:
2809 case DIV: case UDIV:
2810 case MOD: case UMOD:
2811 case AND: case IOR: case XOR:
2812 case ROTATERT: case ROTATE:
2813 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2814 case NE: case EQ:
2815 case GE: case GT: case GEU: case GTU:
2816 case LE: case LT: case LEU: case LTU:
2818 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2819 rtx new1
2820 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2822 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2823 return gen_rtx (code, GET_MODE (x), new0, new1);
2825 return x;
2827 case EXPR_LIST:
2828 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2829 if (XEXP (x, 0))
2831 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2832 if (new != XEXP (x, 0))
2833 x = gen_rtx (EXPR_LIST, REG_NOTE_KIND (x), new, XEXP (x, 1));
2836 /* ... fall through ... */
2838 case INSN_LIST:
2839 /* Now do eliminations in the rest of the chain. If this was
2840 an EXPR_LIST, this might result in allocating more memory than is
2841 strictly needed, but it simplifies the code. */
2842 if (XEXP (x, 1))
2844 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2845 if (new != XEXP (x, 1))
2846 return gen_rtx (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2848 return x;
2850 case PRE_INC:
2851 case POST_INC:
2852 case PRE_DEC:
2853 case POST_DEC:
2854 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2855 if (ep->to_rtx == XEXP (x, 0))
2857 int size = GET_MODE_SIZE (mem_mode);
2859 /* If more bytes than MEM_MODE are pushed, account for them. */
2860 #ifdef PUSH_ROUNDING
2861 if (ep->to_rtx == stack_pointer_rtx)
2862 size = PUSH_ROUNDING (size);
2863 #endif
2864 if (code == PRE_DEC || code == POST_DEC)
2865 ep->offset += size;
2866 else
2867 ep->offset -= size;
2870 /* Fall through to generic unary operation case. */
2871 case USE:
2872 case STRICT_LOW_PART:
2873 case NEG: case NOT:
2874 case SIGN_EXTEND: case ZERO_EXTEND:
2875 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2876 case FLOAT: case FIX:
2877 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2878 case ABS:
2879 case SQRT:
2880 case FFS:
2881 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2882 if (new != XEXP (x, 0))
2883 return gen_rtx (code, GET_MODE (x), new);
2884 return x;
2886 case SUBREG:
2887 /* Similar to above processing, but preserve SUBREG_WORD.
2888 Convert (subreg (mem)) to (mem) if not paradoxical.
2889 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2890 pseudo didn't get a hard reg, we must replace this with the
2891 eliminated version of the memory location because push_reloads
2892 may do the replacement in certain circumstances. */
2893 if (GET_CODE (SUBREG_REG (x)) == REG
2894 && (GET_MODE_SIZE (GET_MODE (x))
2895 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2896 && reg_equiv_memory_loc != 0
2897 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2899 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
2900 mem_mode, insn);
2902 /* If we didn't change anything, we must retain the pseudo. */
2903 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
2904 new = SUBREG_REG (x);
2905 else
2907 /* Otherwise, ensure NEW isn't shared in case we have to reload
2908 it. */
2909 new = copy_rtx (new);
2911 /* In this case, we must show that the pseudo is used in this
2912 insn so that delete_output_reload will do the right thing. */
2913 if (insn != 0 && GET_CODE (insn) != EXPR_LIST
2914 && GET_CODE (insn) != INSN_LIST)
2915 emit_insn_before (gen_rtx (USE, VOIDmode, SUBREG_REG (x)),
2916 insn);
2919 else
2920 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2922 if (new != XEXP (x, 0))
2924 if (GET_CODE (new) == MEM
2925 && (GET_MODE_SIZE (GET_MODE (x))
2926 <= GET_MODE_SIZE (GET_MODE (new)))
2927 #ifdef LOAD_EXTEND_OP
2928 /* On these machines we will be reloading what is
2929 inside the SUBREG if it originally was a pseudo and
2930 the inner and outer modes are both a word or
2931 smaller. So leave the SUBREG then. */
2932 && ! (GET_CODE (SUBREG_REG (x)) == REG
2933 && GET_MODE_SIZE (GET_MODE (x)) <= UNITS_PER_WORD
2934 && GET_MODE_SIZE (GET_MODE (new)) <= UNITS_PER_WORD
2935 && (GET_MODE_SIZE (GET_MODE (x))
2936 > GET_MODE_SIZE (GET_MODE (new)))
2937 && INTEGRAL_MODE_P (GET_MODE (new))
2938 && LOAD_EXTEND_OP (GET_MODE (new)) != NIL)
2939 #endif
2942 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2943 enum machine_mode mode = GET_MODE (x);
2945 if (BYTES_BIG_ENDIAN)
2946 offset += (MIN (UNITS_PER_WORD,
2947 GET_MODE_SIZE (GET_MODE (new)))
2948 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2950 PUT_MODE (new, mode);
2951 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2952 return new;
2954 else
2955 return gen_rtx (SUBREG, GET_MODE (x), new, SUBREG_WORD (x));
2958 return x;
2960 case CLOBBER:
2961 /* If clobbering a register that is the replacement register for an
2962 elimination we still think can be performed, note that it cannot
2963 be performed. Otherwise, we need not be concerned about it. */
2964 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2965 if (ep->to_rtx == XEXP (x, 0))
2966 ep->can_eliminate = 0;
2968 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2969 if (new != XEXP (x, 0))
2970 return gen_rtx (code, GET_MODE (x), new);
2971 return x;
2973 case ASM_OPERANDS:
2975 rtx *temp_vec;
2976 /* Properly handle sharing input and constraint vectors. */
2977 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
2979 /* When we come to a new vector not seen before,
2980 scan all its elements; keep the old vector if none
2981 of them changes; otherwise, make a copy. */
2982 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
2983 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
2984 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2985 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
2986 mem_mode, insn);
2988 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2989 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
2990 break;
2992 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
2993 new_asm_operands_vec = old_asm_operands_vec;
2994 else
2995 new_asm_operands_vec
2996 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
2999 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
3000 if (new_asm_operands_vec == old_asm_operands_vec)
3001 return x;
3003 new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
3004 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
3005 ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
3006 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
3007 ASM_OPERANDS_SOURCE_FILE (x),
3008 ASM_OPERANDS_SOURCE_LINE (x));
3009 new->volatil = x->volatil;
3010 return new;
3013 case SET:
3014 /* Check for setting a register that we know about. */
3015 if (GET_CODE (SET_DEST (x)) == REG)
3017 /* See if this is setting the replacement register for an
3018 elimination.
3020 If DEST is the hard frame pointer, we do nothing because we
3021 assume that all assignments to the frame pointer are for
3022 non-local gotos and are being done at a time when they are valid
3023 and do not disturb anything else. Some machines want to
3024 eliminate a fake argument pointer (or even a fake frame pointer)
3025 with either the real frame or the stack pointer. Assignments to
3026 the hard frame pointer must not prevent this elimination. */
3028 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3029 ep++)
3030 if (ep->to_rtx == SET_DEST (x)
3031 && SET_DEST (x) != hard_frame_pointer_rtx)
3033 /* If it is being incremented, adjust the offset. Otherwise,
3034 this elimination can't be done. */
3035 rtx src = SET_SRC (x);
3037 if (GET_CODE (src) == PLUS
3038 && XEXP (src, 0) == SET_DEST (x)
3039 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3040 ep->offset -= INTVAL (XEXP (src, 1));
3041 else
3042 ep->can_eliminate = 0;
3045 /* Now check to see we are assigning to a register that can be
3046 eliminated. If so, it must be as part of a PARALLEL, since we
3047 will not have been called if this is a single SET. So indicate
3048 that we can no longer eliminate this reg. */
3049 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3050 ep++)
3051 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
3052 ep->can_eliminate = 0;
3055 /* Now avoid the loop below in this common case. */
3057 rtx new0 = eliminate_regs (SET_DEST (x), 0, insn);
3058 rtx new1 = eliminate_regs (SET_SRC (x), 0, insn);
3060 /* If SET_DEST changed from a REG to a MEM and INSN is an insn,
3061 write a CLOBBER insn. */
3062 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
3063 && insn != 0 && GET_CODE (insn) != EXPR_LIST
3064 && GET_CODE (insn) != INSN_LIST)
3065 emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
3067 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
3068 return gen_rtx (SET, VOIDmode, new0, new1);
3071 return x;
3073 case MEM:
3074 /* Our only special processing is to pass the mode of the MEM to our
3075 recursive call and copy the flags. While we are here, handle this
3076 case more efficiently. */
3077 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn);
3078 if (new != XEXP (x, 0))
3080 new = gen_rtx (MEM, GET_MODE (x), new);
3081 new->volatil = x->volatil;
3082 new->unchanging = x->unchanging;
3083 new->in_struct = x->in_struct;
3084 return new;
3086 else
3087 return x;
3090 /* Process each of our operands recursively. If any have changed, make a
3091 copy of the rtx. */
3092 fmt = GET_RTX_FORMAT (code);
3093 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3095 if (*fmt == 'e')
3097 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
3098 if (new != XEXP (x, i) && ! copied)
3100 rtx new_x = rtx_alloc (code);
3101 bcopy ((char *) x, (char *) new_x,
3102 (sizeof (*new_x) - sizeof (new_x->fld)
3103 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3104 x = new_x;
3105 copied = 1;
3107 XEXP (x, i) = new;
3109 else if (*fmt == 'E')
3111 int copied_vec = 0;
3112 for (j = 0; j < XVECLEN (x, i); j++)
3114 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
3115 if (new != XVECEXP (x, i, j) && ! copied_vec)
3117 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
3118 &XVECEXP (x, i, 0));
3119 if (! copied)
3121 rtx new_x = rtx_alloc (code);
3122 bcopy ((char *) x, (char *) new_x,
3123 (sizeof (*new_x) - sizeof (new_x->fld)
3124 + (sizeof (new_x->fld[0])
3125 * GET_RTX_LENGTH (code))));
3126 x = new_x;
3127 copied = 1;
3129 XVEC (x, i) = new_v;
3130 copied_vec = 1;
3132 XVECEXP (x, i, j) = new;
3137 return x;
3140 /* Scan INSN and eliminate all eliminable registers in it.
3142 If REPLACE is nonzero, do the replacement destructively. Also
3143 delete the insn as dead it if it is setting an eliminable register.
3145 If REPLACE is zero, do all our allocations in reload_obstack.
3147 If no eliminations were done and this insn doesn't require any elimination
3148 processing (these are not identical conditions: it might be updating sp,
3149 but not referencing fp; this needs to be seen during reload_as_needed so
3150 that the offset between fp and sp can be taken into consideration), zero
3151 is returned. Otherwise, 1 is returned. */
3153 static int
3154 eliminate_regs_in_insn (insn, replace)
3155 rtx insn;
3156 int replace;
3158 rtx old_body = PATTERN (insn);
3159 rtx old_set = single_set (insn);
3160 rtx new_body;
3161 int val = 0;
3162 struct elim_table *ep;
3164 if (! replace)
3165 push_obstacks (&reload_obstack, &reload_obstack);
3167 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3168 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3170 /* Check for setting an eliminable register. */
3171 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3172 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3174 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3175 /* If this is setting the frame pointer register to the
3176 hardware frame pointer register and this is an elimination
3177 that will be done (tested above), this insn is really
3178 adjusting the frame pointer downward to compensate for
3179 the adjustment done before a nonlocal goto. */
3180 if (ep->from == FRAME_POINTER_REGNUM
3181 && ep->to == HARD_FRAME_POINTER_REGNUM)
3183 rtx src = SET_SRC (old_set);
3184 int offset, ok = 0;
3186 if (src == ep->to_rtx)
3187 offset = 0, ok = 1;
3188 else if (GET_CODE (src) == PLUS
3189 && GET_CODE (XEXP (src, 0)) == CONST_INT)
3190 offset = INTVAL (XEXP (src, 0)), ok = 1;
3192 if (ok)
3194 if (replace)
3196 rtx src
3197 = plus_constant (ep->to_rtx, offset - ep->offset);
3199 /* First see if this insn remains valid when we
3200 make the change. If not, keep the INSN_CODE
3201 the same and let reload fit it up. */
3202 validate_change (insn, &SET_SRC (old_set), src, 1);
3203 validate_change (insn, &SET_DEST (old_set),
3204 ep->to_rtx, 1);
3205 if (! apply_change_group ())
3207 SET_SRC (old_set) = src;
3208 SET_DEST (old_set) = ep->to_rtx;
3212 val = 1;
3213 goto done;
3216 #endif
3218 /* In this case this insn isn't serving a useful purpose. We
3219 will delete it in reload_as_needed once we know that this
3220 elimination is, in fact, being done.
3222 If REPLACE isn't set, we can't delete this insn, but neededn't
3223 process it since it won't be used unless something changes. */
3224 if (replace)
3225 delete_dead_insn (insn);
3226 val = 1;
3227 goto done;
3230 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3231 in the insn is the negative of the offset in FROM. Substitute
3232 (set (reg) (reg to)) for the insn and change its code.
3234 We have to do this here, rather than in eliminate_regs, do that we can
3235 change the insn code. */
3237 if (GET_CODE (SET_SRC (old_set)) == PLUS
3238 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3239 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3240 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3241 ep++)
3242 if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3243 && ep->can_eliminate)
3245 /* We must stop at the first elimination that will be used.
3246 If this one would replace the PLUS with a REG, do it
3247 now. Otherwise, quit the loop and let eliminate_regs
3248 do its normal replacement. */
3249 if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3251 /* We assume here that we don't need a PARALLEL of
3252 any CLOBBERs for this assignment. There's not
3253 much we can do if we do need it. */
3254 PATTERN (insn) = gen_rtx (SET, VOIDmode,
3255 SET_DEST (old_set), ep->to_rtx);
3256 INSN_CODE (insn) = -1;
3257 val = 1;
3258 goto done;
3261 break;
3265 old_asm_operands_vec = 0;
3267 /* Replace the body of this insn with a substituted form. If we changed
3268 something, return non-zero.
3270 If we are replacing a body that was a (set X (plus Y Z)), try to
3271 re-recognize the insn. We do this in case we had a simple addition
3272 but now can do this as a load-address. This saves an insn in this
3273 common case. */
3275 new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
3276 if (new_body != old_body)
3278 /* If we aren't replacing things permanently and we changed something,
3279 make another copy to ensure that all the RTL is new. Otherwise
3280 things can go wrong if find_reload swaps commutative operands
3281 and one is inside RTL that has been copied while the other is not. */
3283 /* Don't copy an asm_operands because (1) there's no need and (2)
3284 copy_rtx can't do it properly when there are multiple outputs. */
3285 if (! replace && asm_noperands (old_body) < 0)
3286 new_body = copy_rtx (new_body);
3288 /* If we had a move insn but now we don't, rerecognize it. This will
3289 cause spurious re-recognition if the old move had a PARALLEL since
3290 the new one still will, but we can't call single_set without
3291 having put NEW_BODY into the insn and the re-recognition won't
3292 hurt in this rare case. */
3293 if (old_set != 0
3294 && ((GET_CODE (SET_SRC (old_set)) == REG
3295 && (GET_CODE (new_body) != SET
3296 || GET_CODE (SET_SRC (new_body)) != REG))
3297 /* If this was a load from or store to memory, compare
3298 the MEM in recog_operand to the one in the insn. If they
3299 are not equal, then rerecognize the insn. */
3300 || (old_set != 0
3301 && ((GET_CODE (SET_SRC (old_set)) == MEM
3302 && SET_SRC (old_set) != recog_operand[1])
3303 || (GET_CODE (SET_DEST (old_set)) == MEM
3304 && SET_DEST (old_set) != recog_operand[0])))
3305 /* If this was an add insn before, rerecognize. */
3306 || GET_CODE (SET_SRC (old_set)) == PLUS))
3308 if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3309 /* If recognition fails, store the new body anyway.
3310 It's normal to have recognition failures here
3311 due to bizarre memory addresses; reloading will fix them. */
3312 PATTERN (insn) = new_body;
3314 else
3315 PATTERN (insn) = new_body;
3317 val = 1;
3320 /* Loop through all elimination pairs. See if any have changed and
3321 recalculate the number not at initial offset.
3323 Compute the maximum offset (minimum offset if the stack does not
3324 grow downward) for each elimination pair.
3326 We also detect a cases where register elimination cannot be done,
3327 namely, if a register would be both changed and referenced outside a MEM
3328 in the resulting insn since such an insn is often undefined and, even if
3329 not, we cannot know what meaning will be given to it. Note that it is
3330 valid to have a register used in an address in an insn that changes it
3331 (presumably with a pre- or post-increment or decrement).
3333 If anything changes, return nonzero. */
3335 num_not_at_initial_offset = 0;
3336 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3338 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3339 ep->can_eliminate = 0;
3341 ep->ref_outside_mem = 0;
3343 if (ep->previous_offset != ep->offset)
3344 val = 1;
3346 ep->previous_offset = ep->offset;
3347 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3348 num_not_at_initial_offset++;
3350 #ifdef STACK_GROWS_DOWNWARD
3351 ep->max_offset = MAX (ep->max_offset, ep->offset);
3352 #else
3353 ep->max_offset = MIN (ep->max_offset, ep->offset);
3354 #endif
3357 done:
3358 /* If we changed something, perform elmination in REG_NOTES. This is
3359 needed even when REPLACE is zero because a REG_DEAD note might refer
3360 to a register that we eliminate and could cause a different number
3361 of spill registers to be needed in the final reload pass than in
3362 the pre-passes. */
3363 if (val && REG_NOTES (insn) != 0)
3364 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3366 if (! replace)
3367 pop_obstacks ();
3369 return val;
3372 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3373 replacement we currently believe is valid, mark it as not eliminable if X
3374 modifies DEST in any way other than by adding a constant integer to it.
3376 If DEST is the frame pointer, we do nothing because we assume that
3377 all assignments to the hard frame pointer are nonlocal gotos and are being
3378 done at a time when they are valid and do not disturb anything else.
3379 Some machines want to eliminate a fake argument pointer with either the
3380 frame or stack pointer. Assignments to the hard frame pointer must not
3381 prevent this elimination.
3383 Called via note_stores from reload before starting its passes to scan
3384 the insns of the function. */
3386 static void
3387 mark_not_eliminable (dest, x)
3388 rtx dest;
3389 rtx x;
3391 register int i;
3393 /* A SUBREG of a hard register here is just changing its mode. We should
3394 not see a SUBREG of an eliminable hard register, but check just in
3395 case. */
3396 if (GET_CODE (dest) == SUBREG)
3397 dest = SUBREG_REG (dest);
3399 if (dest == hard_frame_pointer_rtx)
3400 return;
3402 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3403 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3404 && (GET_CODE (x) != SET
3405 || GET_CODE (SET_SRC (x)) != PLUS
3406 || XEXP (SET_SRC (x), 0) != dest
3407 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3409 reg_eliminate[i].can_eliminate_previous
3410 = reg_eliminate[i].can_eliminate = 0;
3411 num_eliminable--;
3415 /* Kick all pseudos out of hard register REGNO.
3416 If GLOBAL is nonzero, try to find someplace else to put them.
3417 If DUMPFILE is nonzero, log actions taken on that file.
3419 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3420 because we found we can't eliminate some register. In the case, no pseudos
3421 are allowed to be in the register, even if they are only in a block that
3422 doesn't require spill registers, unlike the case when we are spilling this
3423 hard reg to produce another spill register.
3425 Return nonzero if any pseudos needed to be kicked out. */
3427 static int
3428 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3429 register int regno;
3430 int global;
3431 FILE *dumpfile;
3432 int cant_eliminate;
3434 enum reg_class class = REGNO_REG_CLASS (regno);
3435 int something_changed = 0;
3436 register int i;
3438 SET_HARD_REG_BIT (forbidden_regs, regno);
3440 if (cant_eliminate)
3441 regs_ever_live[regno] = 1;
3443 /* Spill every pseudo reg that was allocated to this reg
3444 or to something that overlaps this reg. */
3446 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3447 if (reg_renumber[i] >= 0
3448 && reg_renumber[i] <= regno
3449 && (reg_renumber[i]
3450 + HARD_REGNO_NREGS (reg_renumber[i],
3451 PSEUDO_REGNO_MODE (i))
3452 > regno))
3454 /* If this register belongs solely to a basic block which needed no
3455 spilling of any class that this register is contained in,
3456 leave it be, unless we are spilling this register because
3457 it was a hard register that can't be eliminated. */
3459 if (! cant_eliminate
3460 && basic_block_needs[0]
3461 && reg_basic_block[i] >= 0
3462 && basic_block_needs[(int) class][reg_basic_block[i]] == 0)
3464 enum reg_class *p;
3466 for (p = reg_class_superclasses[(int) class];
3467 *p != LIM_REG_CLASSES; p++)
3468 if (basic_block_needs[(int) *p][reg_basic_block[i]] > 0)
3469 break;
3471 if (*p == LIM_REG_CLASSES)
3472 continue;
3475 /* Mark it as no longer having a hard register home. */
3476 reg_renumber[i] = -1;
3477 /* We will need to scan everything again. */
3478 something_changed = 1;
3479 if (global)
3480 retry_global_alloc (i, forbidden_regs);
3482 alter_reg (i, regno);
3483 if (dumpfile)
3485 if (reg_renumber[i] == -1)
3486 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3487 else
3488 fprintf (dumpfile, " Register %d now in %d.\n\n",
3489 i, reg_renumber[i]);
3492 for (i = 0; i < scratch_list_length; i++)
3494 if (scratch_list[i] && REGNO (scratch_list[i]) == regno)
3496 if (! cant_eliminate && basic_block_needs[0]
3497 && ! basic_block_needs[(int) class][scratch_block[i]])
3499 enum reg_class *p;
3501 for (p = reg_class_superclasses[(int) class];
3502 *p != LIM_REG_CLASSES; p++)
3503 if (basic_block_needs[(int) *p][scratch_block[i]] > 0)
3504 break;
3506 if (*p == LIM_REG_CLASSES)
3507 continue;
3509 PUT_CODE (scratch_list[i], SCRATCH);
3510 scratch_list[i] = 0;
3511 something_changed = 1;
3512 continue;
3516 return something_changed;
3519 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3520 Also mark any hard registers used to store user variables as
3521 forbidden from being used for spill registers. */
3523 static void
3524 scan_paradoxical_subregs (x)
3525 register rtx x;
3527 register int i;
3528 register char *fmt;
3529 register enum rtx_code code = GET_CODE (x);
3531 switch (code)
3533 case REG:
3534 #ifdef SMALL_REGISTER_CLASSES
3535 if (REGNO (x) < FIRST_PSEUDO_REGISTER && REG_USERVAR_P (x))
3536 SET_HARD_REG_BIT (forbidden_regs, REGNO (x));
3537 #endif
3538 return;
3540 case CONST_INT:
3541 case CONST:
3542 case SYMBOL_REF:
3543 case LABEL_REF:
3544 case CONST_DOUBLE:
3545 case CC0:
3546 case PC:
3547 case USE:
3548 case CLOBBER:
3549 return;
3551 case SUBREG:
3552 if (GET_CODE (SUBREG_REG (x)) == REG
3553 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3554 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3555 = GET_MODE_SIZE (GET_MODE (x));
3556 return;
3559 fmt = GET_RTX_FORMAT (code);
3560 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3562 if (fmt[i] == 'e')
3563 scan_paradoxical_subregs (XEXP (x, i));
3564 else if (fmt[i] == 'E')
3566 register int j;
3567 for (j = XVECLEN (x, i) - 1; j >=0; j--)
3568 scan_paradoxical_subregs (XVECEXP (x, i, j));
3573 static int
3574 hard_reg_use_compare (p1, p2)
3575 struct hard_reg_n_uses *p1, *p2;
3577 int tem = p1->uses - p2->uses;
3578 if (tem != 0) return tem;
3579 /* If regs are equally good, sort by regno,
3580 so that the results of qsort leave nothing to chance. */
3581 return p1->regno - p2->regno;
3584 /* Choose the order to consider regs for use as reload registers
3585 based on how much trouble would be caused by spilling one.
3586 Store them in order of decreasing preference in potential_reload_regs. */
3588 static void
3589 order_regs_for_reload ()
3591 register int i;
3592 register int o = 0;
3593 int large = 0;
3595 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3597 CLEAR_HARD_REG_SET (bad_spill_regs);
3599 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3600 potential_reload_regs[i] = -1;
3602 /* Count number of uses of each hard reg by pseudo regs allocated to it
3603 and then order them by decreasing use. */
3605 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3607 hard_reg_n_uses[i].uses = 0;
3608 hard_reg_n_uses[i].regno = i;
3611 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3613 int regno = reg_renumber[i];
3614 if (regno >= 0)
3616 int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3617 while (regno < lim)
3618 hard_reg_n_uses[regno++].uses += reg_n_refs[i];
3620 large += reg_n_refs[i];
3623 /* Now fixed registers (which cannot safely be used for reloading)
3624 get a very high use count so they will be considered least desirable.
3625 Registers used explicitly in the rtl code are almost as bad. */
3627 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3629 if (fixed_regs[i])
3631 hard_reg_n_uses[i].uses += 2 * large + 2;
3632 SET_HARD_REG_BIT (bad_spill_regs, i);
3634 else if (regs_explicitly_used[i])
3636 hard_reg_n_uses[i].uses += large + 1;
3637 #ifndef SMALL_REGISTER_CLASSES
3638 /* ??? We are doing this here because of the potential that
3639 bad code may be generated if a register explicitly used in
3640 an insn was used as a spill register for that insn. But
3641 not using these are spill registers may lose on some machine.
3642 We'll have to see how this works out. */
3643 SET_HARD_REG_BIT (bad_spill_regs, i);
3644 #endif
3647 hard_reg_n_uses[HARD_FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3648 SET_HARD_REG_BIT (bad_spill_regs, HARD_FRAME_POINTER_REGNUM);
3650 #ifdef ELIMINABLE_REGS
3651 /* If registers other than the frame pointer are eliminable, mark them as
3652 poor choices. */
3653 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3655 hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3656 SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3658 #endif
3660 /* Prefer registers not so far used, for use in temporary loading.
3661 Among them, if REG_ALLOC_ORDER is defined, use that order.
3662 Otherwise, prefer registers not preserved by calls. */
3664 #ifdef REG_ALLOC_ORDER
3665 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3667 int regno = reg_alloc_order[i];
3669 if (hard_reg_n_uses[regno].uses == 0)
3670 potential_reload_regs[o++] = regno;
3672 #else
3673 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3675 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3676 potential_reload_regs[o++] = i;
3678 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3680 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3681 potential_reload_regs[o++] = i;
3683 #endif
3685 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3686 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3688 /* Now add the regs that are already used,
3689 preferring those used less often. The fixed and otherwise forbidden
3690 registers will be at the end of this list. */
3692 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3693 if (hard_reg_n_uses[i].uses != 0)
3694 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3697 /* Used in reload_as_needed to sort the spilled regs. */
3699 static int
3700 compare_spill_regs (r1, r2)
3701 short *r1, *r2;
3703 return *r1 - *r2;
3706 /* Reload pseudo-registers into hard regs around each insn as needed.
3707 Additional register load insns are output before the insn that needs it
3708 and perhaps store insns after insns that modify the reloaded pseudo reg.
3710 reg_last_reload_reg and reg_reloaded_contents keep track of
3711 which registers are already available in reload registers.
3712 We update these for the reloads that we perform,
3713 as the insns are scanned. */
3715 static void
3716 reload_as_needed (first, live_known)
3717 rtx first;
3718 int live_known;
3720 register rtx insn;
3721 register int i;
3722 int this_block = 0;
3723 rtx x;
3724 rtx after_call = 0;
3726 bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
3727 bzero ((char *) spill_reg_store, sizeof spill_reg_store);
3728 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3729 bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
3730 reg_has_output_reload = (char *) alloca (max_regno);
3731 for (i = 0; i < n_spills; i++)
3733 reg_reloaded_contents[i] = -1;
3734 reg_reloaded_insn[i] = 0;
3737 /* Reset all offsets on eliminable registers to their initial values. */
3738 #ifdef ELIMINABLE_REGS
3739 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3741 INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
3742 reg_eliminate[i].initial_offset);
3743 reg_eliminate[i].previous_offset
3744 = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3746 #else
3747 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3748 reg_eliminate[0].previous_offset
3749 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3750 #endif
3752 num_not_at_initial_offset = 0;
3754 /* Order the spilled regs, so that allocate_reload_regs can guarantee to
3755 pack registers with group needs. */
3756 if (n_spills > 1)
3758 qsort (spill_regs, n_spills, sizeof (short), compare_spill_regs);
3759 for (i = 0; i < n_spills; i++)
3760 spill_reg_order[spill_regs[i]] = i;
3763 for (insn = first; insn;)
3765 register rtx next = NEXT_INSN (insn);
3767 /* Notice when we move to a new basic block. */
3768 if (live_known && this_block + 1 < n_basic_blocks
3769 && insn == basic_block_head[this_block+1])
3770 ++this_block;
3772 /* If we pass a label, copy the offsets from the label information
3773 into the current offsets of each elimination. */
3774 if (GET_CODE (insn) == CODE_LABEL)
3776 num_not_at_initial_offset = 0;
3777 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3779 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
3780 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
3781 if (reg_eliminate[i].can_eliminate
3782 && (reg_eliminate[i].offset
3783 != reg_eliminate[i].initial_offset))
3784 num_not_at_initial_offset++;
3788 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3790 rtx avoid_return_reg = 0;
3792 #ifdef SMALL_REGISTER_CLASSES
3793 /* Set avoid_return_reg if this is an insn
3794 that might use the value of a function call. */
3795 if (GET_CODE (insn) == CALL_INSN)
3797 if (GET_CODE (PATTERN (insn)) == SET)
3798 after_call = SET_DEST (PATTERN (insn));
3799 else if (GET_CODE (PATTERN (insn)) == PARALLEL
3800 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
3801 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
3802 else
3803 after_call = 0;
3805 else if (after_call != 0
3806 && !(GET_CODE (PATTERN (insn)) == SET
3807 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
3809 if (reg_referenced_p (after_call, PATTERN (insn)))
3810 avoid_return_reg = after_call;
3811 after_call = 0;
3813 #endif /* SMALL_REGISTER_CLASSES */
3815 /* If this is a USE and CLOBBER of a MEM, ensure that any
3816 references to eliminable registers have been removed. */
3818 if ((GET_CODE (PATTERN (insn)) == USE
3819 || GET_CODE (PATTERN (insn)) == CLOBBER)
3820 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3821 XEXP (XEXP (PATTERN (insn), 0), 0)
3822 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3823 GET_MODE (XEXP (PATTERN (insn), 0)), NULL_RTX);
3825 /* If we need to do register elimination processing, do so.
3826 This might delete the insn, in which case we are done. */
3827 if (num_eliminable && GET_MODE (insn) == QImode)
3829 eliminate_regs_in_insn (insn, 1);
3830 if (GET_CODE (insn) == NOTE)
3832 insn = next;
3833 continue;
3837 if (GET_MODE (insn) == VOIDmode)
3838 n_reloads = 0;
3839 /* First find the pseudo regs that must be reloaded for this insn.
3840 This info is returned in the tables reload_... (see reload.h).
3841 Also modify the body of INSN by substituting RELOAD
3842 rtx's for those pseudo regs. */
3843 else
3845 bzero (reg_has_output_reload, max_regno);
3846 CLEAR_HARD_REG_SET (reg_is_output_reload);
3848 find_reloads (insn, 1, spill_indirect_levels, live_known,
3849 spill_reg_order);
3852 if (n_reloads > 0)
3854 rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
3855 rtx p;
3856 int class;
3858 /* If this block has not had spilling done for a
3859 particular clas and we have any non-optionals that need a
3860 spill reg in that class, abort. */
3862 for (class = 0; class < N_REG_CLASSES; class++)
3863 if (basic_block_needs[class] != 0
3864 && basic_block_needs[class][this_block] == 0)
3865 for (i = 0; i < n_reloads; i++)
3866 if (class == (int) reload_reg_class[i]
3867 && reload_reg_rtx[i] == 0
3868 && ! reload_optional[i]
3869 && (reload_in[i] != 0 || reload_out[i] != 0
3870 || reload_secondary_p[i] != 0))
3871 fatal_insn ("Non-optional registers need a spill register", insn);
3873 /* Now compute which reload regs to reload them into. Perhaps
3874 reusing reload regs from previous insns, or else output
3875 load insns to reload them. Maybe output store insns too.
3876 Record the choices of reload reg in reload_reg_rtx. */
3877 choose_reload_regs (insn, avoid_return_reg);
3879 #ifdef SMALL_REGISTER_CLASSES
3880 /* Merge any reloads that we didn't combine for fear of
3881 increasing the number of spill registers needed but now
3882 discover can be safely merged. */
3883 merge_assigned_reloads (insn);
3884 #endif
3886 /* Generate the insns to reload operands into or out of
3887 their reload regs. */
3888 emit_reload_insns (insn);
3890 /* Substitute the chosen reload regs from reload_reg_rtx
3891 into the insn's body (or perhaps into the bodies of other
3892 load and store insn that we just made for reloading
3893 and that we moved the structure into). */
3894 subst_reloads ();
3896 /* If this was an ASM, make sure that all the reload insns
3897 we have generated are valid. If not, give an error
3898 and delete them. */
3900 if (asm_noperands (PATTERN (insn)) >= 0)
3901 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3902 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
3903 && (recog_memoized (p) < 0
3904 || (insn_extract (p),
3905 ! constrain_operands (INSN_CODE (p), 1))))
3907 error_for_asm (insn,
3908 "`asm' operand requires impossible reload");
3909 PUT_CODE (p, NOTE);
3910 NOTE_SOURCE_FILE (p) = 0;
3911 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3914 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3915 is no longer validly lying around to save a future reload.
3916 Note that this does not detect pseudos that were reloaded
3917 for this insn in order to be stored in
3918 (obeying register constraints). That is correct; such reload
3919 registers ARE still valid. */
3920 note_stores (PATTERN (insn), forget_old_reloads_1);
3922 /* There may have been CLOBBER insns placed after INSN. So scan
3923 between INSN and NEXT and use them to forget old reloads. */
3924 for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
3925 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3926 note_stores (PATTERN (x), forget_old_reloads_1);
3928 #ifdef AUTO_INC_DEC
3929 /* Likewise for regs altered by auto-increment in this insn.
3930 But note that the reg-notes are not changed by reloading:
3931 they still contain the pseudo-regs, not the spill regs. */
3932 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3933 if (REG_NOTE_KIND (x) == REG_INC)
3935 /* See if this pseudo reg was reloaded in this insn.
3936 If so, its last-reload info is still valid
3937 because it is based on this insn's reload. */
3938 for (i = 0; i < n_reloads; i++)
3939 if (reload_out[i] == XEXP (x, 0))
3940 break;
3942 if (i == n_reloads)
3943 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
3945 #endif
3947 /* A reload reg's contents are unknown after a label. */
3948 if (GET_CODE (insn) == CODE_LABEL)
3949 for (i = 0; i < n_spills; i++)
3951 reg_reloaded_contents[i] = -1;
3952 reg_reloaded_insn[i] = 0;
3955 /* Don't assume a reload reg is still good after a call insn
3956 if it is a call-used reg. */
3957 else if (GET_CODE (insn) == CALL_INSN)
3958 for (i = 0; i < n_spills; i++)
3959 if (call_used_regs[spill_regs[i]])
3961 reg_reloaded_contents[i] = -1;
3962 reg_reloaded_insn[i] = 0;
3965 /* In case registers overlap, allow certain insns to invalidate
3966 particular hard registers. */
3968 #ifdef INSN_CLOBBERS_REGNO_P
3969 for (i = 0 ; i < n_spills ; i++)
3970 if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
3972 reg_reloaded_contents[i] = -1;
3973 reg_reloaded_insn[i] = 0;
3975 #endif
3977 insn = next;
3979 #ifdef USE_C_ALLOCA
3980 alloca (0);
3981 #endif
3985 /* Discard all record of any value reloaded from X,
3986 or reloaded in X from someplace else;
3987 unless X is an output reload reg of the current insn.
3989 X may be a hard reg (the reload reg)
3990 or it may be a pseudo reg that was reloaded from. */
3992 static void
3993 forget_old_reloads_1 (x, ignored)
3994 rtx x;
3995 rtx ignored;
3997 register int regno;
3998 int nr;
3999 int offset = 0;
4001 /* note_stores does give us subregs of hard regs. */
4002 while (GET_CODE (x) == SUBREG)
4004 offset += SUBREG_WORD (x);
4005 x = SUBREG_REG (x);
4008 if (GET_CODE (x) != REG)
4009 return;
4011 regno = REGNO (x) + offset;
4013 if (regno >= FIRST_PSEUDO_REGISTER)
4014 nr = 1;
4015 else
4017 int i;
4018 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4019 /* Storing into a spilled-reg invalidates its contents.
4020 This can happen if a block-local pseudo is allocated to that reg
4021 and it wasn't spilled because this block's total need is 0.
4022 Then some insn might have an optional reload and use this reg. */
4023 for (i = 0; i < nr; i++)
4024 if (spill_reg_order[regno + i] >= 0
4025 /* But don't do this if the reg actually serves as an output
4026 reload reg in the current instruction. */
4027 && (n_reloads == 0
4028 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
4030 reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
4031 reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
4035 /* Since value of X has changed,
4036 forget any value previously copied from it. */
4038 while (nr-- > 0)
4039 /* But don't forget a copy if this is the output reload
4040 that establishes the copy's validity. */
4041 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4042 reg_last_reload_reg[regno + nr] = 0;
4045 /* For each reload, the mode of the reload register. */
4046 static enum machine_mode reload_mode[MAX_RELOADS];
4048 /* For each reload, the largest number of registers it will require. */
4049 static int reload_nregs[MAX_RELOADS];
4051 /* Comparison function for qsort to decide which of two reloads
4052 should be handled first. *P1 and *P2 are the reload numbers. */
4054 static int
4055 reload_reg_class_lower (p1, p2)
4056 short *p1, *p2;
4058 register int r1 = *p1, r2 = *p2;
4059 register int t;
4061 /* Consider required reloads before optional ones. */
4062 t = reload_optional[r1] - reload_optional[r2];
4063 if (t != 0)
4064 return t;
4066 /* Count all solitary classes before non-solitary ones. */
4067 t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
4068 - (reg_class_size[(int) reload_reg_class[r1]] == 1));
4069 if (t != 0)
4070 return t;
4072 /* Aside from solitaires, consider all multi-reg groups first. */
4073 t = reload_nregs[r2] - reload_nregs[r1];
4074 if (t != 0)
4075 return t;
4077 /* Consider reloads in order of increasing reg-class number. */
4078 t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
4079 if (t != 0)
4080 return t;
4082 /* If reloads are equally urgent, sort by reload number,
4083 so that the results of qsort leave nothing to chance. */
4084 return r1 - r2;
4087 /* The following HARD_REG_SETs indicate when each hard register is
4088 used for a reload of various parts of the current insn. */
4090 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4091 static HARD_REG_SET reload_reg_used;
4092 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4093 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4094 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4095 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4096 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4097 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4098 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4099 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4100 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4101 static HARD_REG_SET reload_reg_used_in_op_addr;
4102 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4103 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4104 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4105 static HARD_REG_SET reload_reg_used_in_insn;
4106 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4107 static HARD_REG_SET reload_reg_used_in_other_addr;
4109 /* If reg is in use as a reload reg for any sort of reload. */
4110 static HARD_REG_SET reload_reg_used_at_all;
4112 /* If reg is use as an inherited reload. We just mark the first register
4113 in the group. */
4114 static HARD_REG_SET reload_reg_used_for_inherit;
4116 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4117 TYPE. MODE is used to indicate how many consecutive regs are
4118 actually used. */
4120 static void
4121 mark_reload_reg_in_use (regno, opnum, type, mode)
4122 int regno;
4123 int opnum;
4124 enum reload_type type;
4125 enum machine_mode mode;
4127 int nregs = HARD_REGNO_NREGS (regno, mode);
4128 int i;
4130 for (i = regno; i < nregs + regno; i++)
4132 switch (type)
4134 case RELOAD_OTHER:
4135 SET_HARD_REG_BIT (reload_reg_used, i);
4136 break;
4138 case RELOAD_FOR_INPUT_ADDRESS:
4139 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4140 break;
4142 case RELOAD_FOR_OUTPUT_ADDRESS:
4143 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4144 break;
4146 case RELOAD_FOR_OPERAND_ADDRESS:
4147 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4148 break;
4150 case RELOAD_FOR_OPADDR_ADDR:
4151 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4152 break;
4154 case RELOAD_FOR_OTHER_ADDRESS:
4155 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4156 break;
4158 case RELOAD_FOR_INPUT:
4159 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4160 break;
4162 case RELOAD_FOR_OUTPUT:
4163 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4164 break;
4166 case RELOAD_FOR_INSN:
4167 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4168 break;
4171 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4175 /* Similarly, but show REGNO is no longer in use for a reload. */
4177 static void
4178 clear_reload_reg_in_use (regno, opnum, type, mode)
4179 int regno;
4180 int opnum;
4181 enum reload_type type;
4182 enum machine_mode mode;
4184 int nregs = HARD_REGNO_NREGS (regno, mode);
4185 int i;
4187 for (i = regno; i < nregs + regno; i++)
4189 switch (type)
4191 case RELOAD_OTHER:
4192 CLEAR_HARD_REG_BIT (reload_reg_used, i);
4193 break;
4195 case RELOAD_FOR_INPUT_ADDRESS:
4196 CLEAR_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4197 break;
4199 case RELOAD_FOR_OUTPUT_ADDRESS:
4200 CLEAR_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4201 break;
4203 case RELOAD_FOR_OPERAND_ADDRESS:
4204 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4205 break;
4207 case RELOAD_FOR_OPADDR_ADDR:
4208 CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4209 break;
4211 case RELOAD_FOR_OTHER_ADDRESS:
4212 CLEAR_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4213 break;
4215 case RELOAD_FOR_INPUT:
4216 CLEAR_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4217 break;
4219 case RELOAD_FOR_OUTPUT:
4220 CLEAR_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4221 break;
4223 case RELOAD_FOR_INSN:
4224 CLEAR_HARD_REG_BIT (reload_reg_used_in_insn, i);
4225 break;
4230 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4231 specified by OPNUM and TYPE. */
4233 static int
4234 reload_reg_free_p (regno, opnum, type)
4235 int regno;
4236 int opnum;
4237 enum reload_type type;
4239 int i;
4241 /* In use for a RELOAD_OTHER means it's not available for anything except
4242 RELOAD_FOR_OTHER_ADDRESS. Recall that RELOAD_FOR_OTHER_ADDRESS is known
4243 to be used only for inputs. */
4245 if (type != RELOAD_FOR_OTHER_ADDRESS
4246 && TEST_HARD_REG_BIT (reload_reg_used, regno))
4247 return 0;
4249 switch (type)
4251 case RELOAD_OTHER:
4252 /* In use for anything except RELOAD_FOR_OTHER_ADDRESS means
4253 we can't use it for RELOAD_OTHER. */
4254 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4255 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4256 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4257 return 0;
4259 for (i = 0; i < reload_n_operands; i++)
4260 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4261 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4262 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4263 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4264 return 0;
4266 return 1;
4268 case RELOAD_FOR_INPUT:
4269 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4270 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4271 return 0;
4273 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4274 return 0;
4276 /* If it is used for some other input, can't use it. */
4277 for (i = 0; i < reload_n_operands; i++)
4278 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4279 return 0;
4281 /* If it is used in a later operand's address, can't use it. */
4282 for (i = opnum + 1; i < reload_n_operands; i++)
4283 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno))
4284 return 0;
4286 return 1;
4288 case RELOAD_FOR_INPUT_ADDRESS:
4289 /* Can't use a register if it is used for an input address for this
4290 operand or used as an input in an earlier one. */
4291 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno))
4292 return 0;
4294 for (i = 0; i < opnum; i++)
4295 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4296 return 0;
4298 return 1;
4300 case RELOAD_FOR_OUTPUT_ADDRESS:
4301 /* Can't use a register if it is used for an output address for this
4302 operand or used as an output in this or a later operand. */
4303 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4304 return 0;
4306 for (i = opnum; i < reload_n_operands; i++)
4307 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4308 return 0;
4310 return 1;
4312 case RELOAD_FOR_OPERAND_ADDRESS:
4313 for (i = 0; i < reload_n_operands; i++)
4314 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4315 return 0;
4317 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4318 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4320 case RELOAD_FOR_OPADDR_ADDR:
4321 for (i = 0; i < reload_n_operands; i++)
4322 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4323 return 0;
4325 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4327 case RELOAD_FOR_OUTPUT:
4328 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4329 outputs, or an operand address for this or an earlier output. */
4330 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4331 return 0;
4333 for (i = 0; i < reload_n_operands; i++)
4334 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4335 return 0;
4337 for (i = 0; i <= opnum; i++)
4338 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno))
4339 return 0;
4341 return 1;
4343 case RELOAD_FOR_INSN:
4344 for (i = 0; i < reload_n_operands; i++)
4345 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4346 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4347 return 0;
4349 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4350 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4352 case RELOAD_FOR_OTHER_ADDRESS:
4353 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4355 abort ();
4358 /* Return 1 if the value in reload reg REGNO, as used by a reload
4359 needed for the part of the insn specified by OPNUM and TYPE,
4360 is not in use for a reload in any prior part of the insn.
4362 We can assume that the reload reg was already tested for availability
4363 at the time it is needed, and we should not check this again,
4364 in case the reg has already been marked in use. */
4366 static int
4367 reload_reg_free_before_p (regno, opnum, type)
4368 int regno;
4369 int opnum;
4370 enum reload_type type;
4372 int i;
4374 switch (type)
4376 case RELOAD_FOR_OTHER_ADDRESS:
4377 /* These always come first. */
4378 return 1;
4380 case RELOAD_OTHER:
4381 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4383 /* If this use is for part of the insn,
4384 check the reg is not in use for any prior part. It is tempting
4385 to try to do this by falling through from objecs that occur
4386 later in the insn to ones that occur earlier, but that will not
4387 correctly take into account the fact that here we MUST ignore
4388 things that would prevent the register from being allocated in
4389 the first place, since we know that it was allocated. */
4391 case RELOAD_FOR_OUTPUT_ADDRESS:
4392 /* Earlier reloads are for earlier outputs or their addresses,
4393 any RELOAD_FOR_INSN reloads, any inputs or their addresses, or any
4394 RELOAD_FOR_OTHER_ADDRESS reloads (we know it can't conflict with
4395 RELOAD_OTHER).. */
4396 for (i = 0; i < opnum; i++)
4397 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4398 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4399 return 0;
4401 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4402 return 0;
4404 for (i = 0; i < reload_n_operands; i++)
4405 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4406 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4407 return 0;
4409 return (! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4410 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4411 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4413 case RELOAD_FOR_OUTPUT:
4414 /* This can't be used in the output address for this operand and
4415 anything that can't be used for it, except that we've already
4416 tested for RELOAD_FOR_INSN objects. */
4418 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4419 return 0;
4421 for (i = 0; i < opnum; i++)
4422 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4423 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4424 return 0;
4426 for (i = 0; i < reload_n_operands; i++)
4427 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4428 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4429 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4430 return 0;
4432 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4434 case RELOAD_FOR_OPERAND_ADDRESS:
4435 case RELOAD_FOR_OPADDR_ADDR:
4436 case RELOAD_FOR_INSN:
4437 /* These can't conflict with inputs, or each other, so all we have to
4438 test is input addresses and the addresses of OTHER items. */
4440 for (i = 0; i < reload_n_operands; i++)
4441 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno))
4442 return 0;
4444 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4446 case RELOAD_FOR_INPUT:
4447 /* The only things earlier are the address for this and
4448 earlier inputs, other inputs (which we know we don't conflict
4449 with), and addresses of RELOAD_OTHER objects. */
4451 for (i = 0; i <= opnum; i++)
4452 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno))
4453 return 0;
4455 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4457 case RELOAD_FOR_INPUT_ADDRESS:
4458 /* Similarly, all we have to check is for use in earlier inputs'
4459 addresses. */
4460 for (i = 0; i < opnum; i++)
4461 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno))
4462 return 0;
4464 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4466 abort ();
4469 /* Return 1 if the value in reload reg REGNO, as used by a reload
4470 needed for the part of the insn specified by OPNUM and TYPE,
4471 is still available in REGNO at the end of the insn.
4473 We can assume that the reload reg was already tested for availability
4474 at the time it is needed, and we should not check this again,
4475 in case the reg has already been marked in use. */
4477 static int
4478 reload_reg_reaches_end_p (regno, opnum, type)
4479 int regno;
4480 int opnum;
4481 enum reload_type type;
4483 int i;
4485 switch (type)
4487 case RELOAD_OTHER:
4488 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4489 its value must reach the end. */
4490 return 1;
4492 /* If this use is for part of the insn,
4493 its value reaches if no subsequent part uses the same register.
4494 Just like the above function, don't try to do this with lots
4495 of fallthroughs. */
4497 case RELOAD_FOR_OTHER_ADDRESS:
4498 /* Here we check for everything else, since these don't conflict
4499 with anything else and everything comes later. */
4501 for (i = 0; i < reload_n_operands; i++)
4502 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4503 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4504 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4505 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4506 return 0;
4508 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4509 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4510 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4512 case RELOAD_FOR_INPUT_ADDRESS:
4513 /* Similar, except that we check only for this and subsequent inputs
4514 and the address of only subsequent inputs and we do not need
4515 to check for RELOAD_OTHER objects since they are known not to
4516 conflict. */
4518 for (i = opnum; i < reload_n_operands; i++)
4519 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4520 return 0;
4522 for (i = opnum + 1; i < reload_n_operands; i++)
4523 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno))
4524 return 0;
4526 for (i = 0; i < reload_n_operands; i++)
4527 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4528 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4529 return 0;
4531 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4532 return 0;
4534 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4535 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4537 case RELOAD_FOR_INPUT:
4538 /* Similar to input address, except we start at the next operand for
4539 both input and input address and we do not check for
4540 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4541 would conflict. */
4543 for (i = opnum + 1; i < reload_n_operands; i++)
4544 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4545 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4546 return 0;
4548 /* ... fall through ... */
4550 case RELOAD_FOR_OPERAND_ADDRESS:
4551 /* Check outputs and their addresses. */
4553 for (i = 0; i < reload_n_operands; i++)
4554 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4555 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4556 return 0;
4558 return 1;
4560 case RELOAD_FOR_OPADDR_ADDR:
4561 for (i = 0; i < reload_n_operands; i++)
4562 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4563 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4564 return 0;
4566 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4567 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4569 case RELOAD_FOR_INSN:
4570 /* These conflict with other outputs with RELOAD_OTHER. So
4571 we need only check for output addresses. */
4573 opnum = -1;
4575 /* ... fall through ... */
4577 case RELOAD_FOR_OUTPUT:
4578 case RELOAD_FOR_OUTPUT_ADDRESS:
4579 /* We already know these can't conflict with a later output. So the
4580 only thing to check are later output addresses. */
4581 for (i = opnum + 1; i < reload_n_operands; i++)
4582 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno))
4583 return 0;
4585 return 1;
4588 abort ();
4591 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4592 Return 0 otherwise.
4594 This function uses the same algorithm as reload_reg_free_p above. */
4596 static int
4597 reloads_conflict (r1, r2)
4598 int r1, r2;
4600 enum reload_type r1_type = reload_when_needed[r1];
4601 enum reload_type r2_type = reload_when_needed[r2];
4602 int r1_opnum = reload_opnum[r1];
4603 int r2_opnum = reload_opnum[r2];
4605 /* RELOAD_OTHER conflicts with everything except RELOAD_FOR_OTHER_ADDRESS. */
4607 if (r2_type == RELOAD_OTHER && r1_type != RELOAD_FOR_OTHER_ADDRESS)
4608 return 1;
4610 /* Otherwise, check conflicts differently for each type. */
4612 switch (r1_type)
4614 case RELOAD_FOR_INPUT:
4615 return (r2_type == RELOAD_FOR_INSN
4616 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4617 || r2_type == RELOAD_FOR_OPADDR_ADDR
4618 || r2_type == RELOAD_FOR_INPUT
4619 || (r2_type == RELOAD_FOR_INPUT_ADDRESS && r2_opnum > r1_opnum));
4621 case RELOAD_FOR_INPUT_ADDRESS:
4622 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4623 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4625 case RELOAD_FOR_OUTPUT_ADDRESS:
4626 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4627 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4629 case RELOAD_FOR_OPERAND_ADDRESS:
4630 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4631 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4633 case RELOAD_FOR_OPADDR_ADDR:
4634 return (r2_type == RELOAD_FOR_INPUT
4635 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4637 case RELOAD_FOR_OUTPUT:
4638 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4639 || (r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4640 && r2_opnum >= r1_opnum));
4642 case RELOAD_FOR_INSN:
4643 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4644 || r2_type == RELOAD_FOR_INSN
4645 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4647 case RELOAD_FOR_OTHER_ADDRESS:
4648 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4650 case RELOAD_OTHER:
4651 return r2_type != RELOAD_FOR_OTHER_ADDRESS;
4653 default:
4654 abort ();
4658 /* Vector of reload-numbers showing the order in which the reloads should
4659 be processed. */
4660 short reload_order[MAX_RELOADS];
4662 /* Indexed by reload number, 1 if incoming value
4663 inherited from previous insns. */
4664 char reload_inherited[MAX_RELOADS];
4666 /* For an inherited reload, this is the insn the reload was inherited from,
4667 if we know it. Otherwise, this is 0. */
4668 rtx reload_inheritance_insn[MAX_RELOADS];
4670 /* If non-zero, this is a place to get the value of the reload,
4671 rather than using reload_in. */
4672 rtx reload_override_in[MAX_RELOADS];
4674 /* For each reload, the index in spill_regs of the spill register used,
4675 or -1 if we did not need one of the spill registers for this reload. */
4676 int reload_spill_index[MAX_RELOADS];
4678 /* Index of last register assigned as a spill register. We allocate in
4679 a round-robin fashio. */
4681 static int last_spill_reg = 0;
4683 /* Find a spill register to use as a reload register for reload R.
4684 LAST_RELOAD is non-zero if this is the last reload for the insn being
4685 processed.
4687 Set reload_reg_rtx[R] to the register allocated.
4689 If NOERROR is nonzero, we return 1 if successful,
4690 or 0 if we couldn't find a spill reg and we didn't change anything. */
4692 static int
4693 allocate_reload_reg (r, insn, last_reload, noerror)
4694 int r;
4695 rtx insn;
4696 int last_reload;
4697 int noerror;
4699 int i;
4700 int pass;
4701 int count;
4702 rtx new;
4703 int regno;
4705 /* If we put this reload ahead, thinking it is a group,
4706 then insist on finding a group. Otherwise we can grab a
4707 reg that some other reload needs.
4708 (That can happen when we have a 68000 DATA_OR_FP_REG
4709 which is a group of data regs or one fp reg.)
4710 We need not be so restrictive if there are no more reloads
4711 for this insn.
4713 ??? Really it would be nicer to have smarter handling
4714 for that kind of reg class, where a problem like this is normal.
4715 Perhaps those classes should be avoided for reloading
4716 by use of more alternatives. */
4718 int force_group = reload_nregs[r] > 1 && ! last_reload;
4720 /* If we want a single register and haven't yet found one,
4721 take any reg in the right class and not in use.
4722 If we want a consecutive group, here is where we look for it.
4724 We use two passes so we can first look for reload regs to
4725 reuse, which are already in use for other reloads in this insn,
4726 and only then use additional registers.
4727 I think that maximizing reuse is needed to make sure we don't
4728 run out of reload regs. Suppose we have three reloads, and
4729 reloads A and B can share regs. These need two regs.
4730 Suppose A and B are given different regs.
4731 That leaves none for C. */
4732 for (pass = 0; pass < 2; pass++)
4734 /* I is the index in spill_regs.
4735 We advance it round-robin between insns to use all spill regs
4736 equally, so that inherited reloads have a chance
4737 of leapfrogging each other. Don't do this, however, when we have
4738 group needs and failure would be fatal; if we only have a relatively
4739 small number of spill registers, and more than one of them has
4740 group needs, then by starting in the middle, we may end up
4741 allocating the first one in such a way that we are not left with
4742 sufficient groups to handle the rest. */
4744 if (noerror || ! force_group)
4745 i = last_spill_reg;
4746 else
4747 i = -1;
4749 for (count = 0; count < n_spills; count++)
4751 int class = (int) reload_reg_class[r];
4753 i = (i + 1) % n_spills;
4755 if (reload_reg_free_p (spill_regs[i], reload_opnum[r],
4756 reload_when_needed[r])
4757 && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
4758 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4759 /* Look first for regs to share, then for unshared. But
4760 don't share regs used for inherited reloads; they are
4761 the ones we want to preserve. */
4762 && (pass
4763 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
4764 spill_regs[i])
4765 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
4766 spill_regs[i]))))
4768 int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4769 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
4770 (on 68000) got us two FP regs. If NR is 1,
4771 we would reject both of them. */
4772 if (force_group)
4773 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
4774 /* If we need only one reg, we have already won. */
4775 if (nr == 1)
4777 /* But reject a single reg if we demand a group. */
4778 if (force_group)
4779 continue;
4780 break;
4782 /* Otherwise check that as many consecutive regs as we need
4783 are available here.
4784 Also, don't use for a group registers that are
4785 needed for nongroups. */
4786 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
4787 while (nr > 1)
4789 regno = spill_regs[i] + nr - 1;
4790 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
4791 && spill_reg_order[regno] >= 0
4792 && reload_reg_free_p (regno, reload_opnum[r],
4793 reload_when_needed[r])
4794 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
4795 regno)))
4796 break;
4797 nr--;
4799 if (nr == 1)
4800 break;
4804 /* If we found something on pass 1, omit pass 2. */
4805 if (count < n_spills)
4806 break;
4809 /* We should have found a spill register by now. */
4810 if (count == n_spills)
4812 if (noerror)
4813 return 0;
4814 goto failure;
4817 /* I is the index in SPILL_REG_RTX of the reload register we are to
4818 allocate. Get an rtx for it and find its register number. */
4820 new = spill_reg_rtx[i];
4822 if (new == 0 || GET_MODE (new) != reload_mode[r])
4823 spill_reg_rtx[i] = new
4824 = gen_rtx (REG, reload_mode[r], spill_regs[i]);
4826 regno = true_regnum (new);
4828 /* Detect when the reload reg can't hold the reload mode.
4829 This used to be one `if', but Sequent compiler can't handle that. */
4830 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4832 enum machine_mode test_mode = VOIDmode;
4833 if (reload_in[r])
4834 test_mode = GET_MODE (reload_in[r]);
4835 /* If reload_in[r] has VOIDmode, it means we will load it
4836 in whatever mode the reload reg has: to wit, reload_mode[r].
4837 We have already tested that for validity. */
4838 /* Aside from that, we need to test that the expressions
4839 to reload from or into have modes which are valid for this
4840 reload register. Otherwise the reload insns would be invalid. */
4841 if (! (reload_in[r] != 0 && test_mode != VOIDmode
4842 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
4843 if (! (reload_out[r] != 0
4844 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
4846 /* The reg is OK. */
4847 last_spill_reg = i;
4849 /* Mark as in use for this insn the reload regs we use
4850 for this. */
4851 mark_reload_reg_in_use (spill_regs[i], reload_opnum[r],
4852 reload_when_needed[r], reload_mode[r]);
4854 reload_reg_rtx[r] = new;
4855 reload_spill_index[r] = i;
4856 return 1;
4860 /* The reg is not OK. */
4861 if (noerror)
4862 return 0;
4864 failure:
4865 if (asm_noperands (PATTERN (insn)) < 0)
4866 /* It's the compiler's fault. */
4867 fatal_insn ("Could not find a spill register", insn);
4869 /* It's the user's fault; the operand's mode and constraint
4870 don't match. Disable this reload so we don't crash in final. */
4871 error_for_asm (insn,
4872 "`asm' operand constraint incompatible with operand size");
4873 reload_in[r] = 0;
4874 reload_out[r] = 0;
4875 reload_reg_rtx[r] = 0;
4876 reload_optional[r] = 1;
4877 reload_secondary_p[r] = 1;
4879 return 1;
4882 /* Assign hard reg targets for the pseudo-registers we must reload
4883 into hard regs for this insn.
4884 Also output the instructions to copy them in and out of the hard regs.
4886 For machines with register classes, we are responsible for
4887 finding a reload reg in the proper class. */
4889 static void
4890 choose_reload_regs (insn, avoid_return_reg)
4891 rtx insn;
4892 rtx avoid_return_reg;
4894 register int i, j;
4895 int max_group_size = 1;
4896 enum reg_class group_class = NO_REGS;
4897 int inheritance;
4899 rtx save_reload_reg_rtx[MAX_RELOADS];
4900 char save_reload_inherited[MAX_RELOADS];
4901 rtx save_reload_inheritance_insn[MAX_RELOADS];
4902 rtx save_reload_override_in[MAX_RELOADS];
4903 int save_reload_spill_index[MAX_RELOADS];
4904 HARD_REG_SET save_reload_reg_used;
4905 HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4906 HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4907 HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4908 HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4909 HARD_REG_SET save_reload_reg_used_in_op_addr;
4910 HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
4911 HARD_REG_SET save_reload_reg_used_in_insn;
4912 HARD_REG_SET save_reload_reg_used_in_other_addr;
4913 HARD_REG_SET save_reload_reg_used_at_all;
4915 bzero (reload_inherited, MAX_RELOADS);
4916 bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
4917 bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
4919 CLEAR_HARD_REG_SET (reload_reg_used);
4920 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
4921 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
4922 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
4923 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
4924 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
4926 for (i = 0; i < reload_n_operands; i++)
4928 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
4929 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
4930 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
4931 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
4934 #ifdef SMALL_REGISTER_CLASSES
4935 /* Don't bother with avoiding the return reg
4936 if we have no mandatory reload that could use it. */
4937 if (avoid_return_reg)
4939 int do_avoid = 0;
4940 int regno = REGNO (avoid_return_reg);
4941 int nregs
4942 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4943 int r;
4945 for (r = regno; r < regno + nregs; r++)
4946 if (spill_reg_order[r] >= 0)
4947 for (j = 0; j < n_reloads; j++)
4948 if (!reload_optional[j] && reload_reg_rtx[j] == 0
4949 && (reload_in[j] != 0 || reload_out[j] != 0
4950 || reload_secondary_p[j])
4952 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
4953 do_avoid = 1;
4954 if (!do_avoid)
4955 avoid_return_reg = 0;
4957 #endif /* SMALL_REGISTER_CLASSES */
4959 #if 0 /* Not needed, now that we can always retry without inheritance. */
4960 /* See if we have more mandatory reloads than spill regs.
4961 If so, then we cannot risk optimizations that could prevent
4962 reloads from sharing one spill register.
4964 Since we will try finding a better register than reload_reg_rtx
4965 unless it is equal to reload_in or reload_out, count such reloads. */
4968 int tem = 0;
4969 #ifdef SMALL_REGISTER_CLASSES
4970 int tem = (avoid_return_reg != 0);
4971 #endif
4972 for (j = 0; j < n_reloads; j++)
4973 if (! reload_optional[j]
4974 && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
4975 && (reload_reg_rtx[j] == 0
4976 || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
4977 && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
4978 tem++;
4979 if (tem > n_spills)
4980 must_reuse = 1;
4982 #endif
4984 #ifdef SMALL_REGISTER_CLASSES
4985 /* Don't use the subroutine call return reg for a reload
4986 if we are supposed to avoid it. */
4987 if (avoid_return_reg)
4989 int regno = REGNO (avoid_return_reg);
4990 int nregs
4991 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4992 int r;
4994 for (r = regno; r < regno + nregs; r++)
4995 if (spill_reg_order[r] >= 0)
4996 SET_HARD_REG_BIT (reload_reg_used, r);
4998 #endif /* SMALL_REGISTER_CLASSES */
5000 /* In order to be certain of getting the registers we need,
5001 we must sort the reloads into order of increasing register class.
5002 Then our grabbing of reload registers will parallel the process
5003 that provided the reload registers.
5005 Also note whether any of the reloads wants a consecutive group of regs.
5006 If so, record the maximum size of the group desired and what
5007 register class contains all the groups needed by this insn. */
5009 for (j = 0; j < n_reloads; j++)
5011 reload_order[j] = j;
5012 reload_spill_index[j] = -1;
5014 reload_mode[j]
5015 = (reload_inmode[j] == VOIDmode
5016 || (GET_MODE_SIZE (reload_outmode[j])
5017 > GET_MODE_SIZE (reload_inmode[j])))
5018 ? reload_outmode[j] : reload_inmode[j];
5020 reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
5022 if (reload_nregs[j] > 1)
5024 max_group_size = MAX (reload_nregs[j], max_group_size);
5025 group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
5028 /* If we have already decided to use a certain register,
5029 don't use it in another way. */
5030 if (reload_reg_rtx[j])
5031 mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]), reload_opnum[j],
5032 reload_when_needed[j], reload_mode[j]);
5035 if (n_reloads > 1)
5036 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5038 bcopy ((char *) reload_reg_rtx, (char *) save_reload_reg_rtx,
5039 sizeof reload_reg_rtx);
5040 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
5041 bcopy ((char *) reload_inheritance_insn,
5042 (char *) save_reload_inheritance_insn,
5043 sizeof reload_inheritance_insn);
5044 bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
5045 sizeof reload_override_in);
5046 bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
5047 sizeof reload_spill_index);
5048 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5049 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
5050 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5051 reload_reg_used_in_op_addr);
5053 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5054 reload_reg_used_in_op_addr_reload);
5056 COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5057 reload_reg_used_in_insn);
5058 COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5059 reload_reg_used_in_other_addr);
5061 for (i = 0; i < reload_n_operands; i++)
5063 COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5064 reload_reg_used_in_output[i]);
5065 COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5066 reload_reg_used_in_input[i]);
5067 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5068 reload_reg_used_in_input_addr[i]);
5069 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5070 reload_reg_used_in_output_addr[i]);
5073 /* If -O, try first with inheritance, then turning it off.
5074 If not -O, don't do inheritance.
5075 Using inheritance when not optimizing leads to paradoxes
5076 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5077 because one side of the comparison might be inherited. */
5079 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5081 /* Process the reloads in order of preference just found.
5082 Beyond this point, subregs can be found in reload_reg_rtx.
5084 This used to look for an existing reloaded home for all
5085 of the reloads, and only then perform any new reloads.
5086 But that could lose if the reloads were done out of reg-class order
5087 because a later reload with a looser constraint might have an old
5088 home in a register needed by an earlier reload with a tighter constraint.
5090 To solve this, we make two passes over the reloads, in the order
5091 described above. In the first pass we try to inherit a reload
5092 from a previous insn. If there is a later reload that needs a
5093 class that is a proper subset of the class being processed, we must
5094 also allocate a spill register during the first pass.
5096 Then make a second pass over the reloads to allocate any reloads
5097 that haven't been given registers yet. */
5099 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5101 for (j = 0; j < n_reloads; j++)
5103 register int r = reload_order[j];
5105 /* Ignore reloads that got marked inoperative. */
5106 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
5107 continue;
5109 /* If find_reloads chose a to use reload_in or reload_out as a reload
5110 register, we don't need to chose one. Otherwise, try even if it found
5111 one since we might save an insn if we find the value lying around. */
5112 if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
5113 && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
5114 || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
5115 continue;
5117 #if 0 /* No longer needed for correct operation.
5118 It might give better code, or might not; worth an experiment? */
5119 /* If this is an optional reload, we can't inherit from earlier insns
5120 until we are sure that any non-optional reloads have been allocated.
5121 The following code takes advantage of the fact that optional reloads
5122 are at the end of reload_order. */
5123 if (reload_optional[r] != 0)
5124 for (i = 0; i < j; i++)
5125 if ((reload_out[reload_order[i]] != 0
5126 || reload_in[reload_order[i]] != 0
5127 || reload_secondary_p[reload_order[i]])
5128 && ! reload_optional[reload_order[i]]
5129 && reload_reg_rtx[reload_order[i]] == 0)
5130 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
5131 #endif
5133 /* First see if this pseudo is already available as reloaded
5134 for a previous insn. We cannot try to inherit for reloads
5135 that are smaller than the maximum number of registers needed
5136 for groups unless the register we would allocate cannot be used
5137 for the groups.
5139 We could check here to see if this is a secondary reload for
5140 an object that is already in a register of the desired class.
5141 This would avoid the need for the secondary reload register.
5142 But this is complex because we can't easily determine what
5143 objects might want to be loaded via this reload. So let a register
5144 be allocated here. In `emit_reload_insns' we suppress one of the
5145 loads in the case described above. */
5147 if (inheritance)
5149 register int regno = -1;
5150 enum machine_mode mode;
5152 if (reload_in[r] == 0)
5154 else if (GET_CODE (reload_in[r]) == REG)
5156 regno = REGNO (reload_in[r]);
5157 mode = GET_MODE (reload_in[r]);
5159 else if (GET_CODE (reload_in_reg[r]) == REG)
5161 regno = REGNO (reload_in_reg[r]);
5162 mode = GET_MODE (reload_in_reg[r]);
5164 #if 0
5165 /* This won't work, since REGNO can be a pseudo reg number.
5166 Also, it takes much more hair to keep track of all the things
5167 that can invalidate an inherited reload of part of a pseudoreg. */
5168 else if (GET_CODE (reload_in[r]) == SUBREG
5169 && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
5170 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
5171 #endif
5173 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5175 i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
5177 if (reg_reloaded_contents[i] == regno
5178 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
5179 >= GET_MODE_SIZE (mode))
5180 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
5181 && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5182 spill_regs[i])
5183 && (reload_nregs[r] == max_group_size
5184 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5185 spill_regs[i]))
5186 && reload_reg_free_p (spill_regs[i], reload_opnum[r],
5187 reload_when_needed[r])
5188 && reload_reg_free_before_p (spill_regs[i],
5189 reload_opnum[r],
5190 reload_when_needed[r]))
5192 /* If a group is needed, verify that all the subsequent
5193 registers still have their values intact. */
5194 int nr
5195 = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5196 int k;
5198 for (k = 1; k < nr; k++)
5199 if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5200 != regno)
5201 break;
5203 if (k == nr)
5205 int i1;
5207 /* We found a register that contains the
5208 value we need. If this register is the
5209 same as an `earlyclobber' operand of the
5210 current insn, just mark it as a place to
5211 reload from since we can't use it as the
5212 reload register itself. */
5214 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5215 if (reg_overlap_mentioned_for_reload_p
5216 (reg_last_reload_reg[regno],
5217 reload_earlyclobbers[i1]))
5218 break;
5220 if (i1 != n_earlyclobbers
5221 /* Don't really use the inherited spill reg
5222 if we need it wider than we've got it. */
5223 || (GET_MODE_SIZE (reload_mode[r])
5224 > GET_MODE_SIZE (mode)))
5225 reload_override_in[r] = reg_last_reload_reg[regno];
5226 else
5228 int k;
5229 /* We can use this as a reload reg. */
5230 /* Mark the register as in use for this part of
5231 the insn. */
5232 mark_reload_reg_in_use (spill_regs[i],
5233 reload_opnum[r],
5234 reload_when_needed[r],
5235 reload_mode[r]);
5236 reload_reg_rtx[r] = reg_last_reload_reg[regno];
5237 reload_inherited[r] = 1;
5238 reload_inheritance_insn[r]
5239 = reg_reloaded_insn[i];
5240 reload_spill_index[r] = i;
5241 for (k = 0; k < nr; k++)
5242 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5243 spill_regs[i + k]);
5250 /* Here's another way to see if the value is already lying around. */
5251 if (inheritance
5252 && reload_in[r] != 0
5253 && ! reload_inherited[r]
5254 && reload_out[r] == 0
5255 && (CONSTANT_P (reload_in[r])
5256 || GET_CODE (reload_in[r]) == PLUS
5257 || GET_CODE (reload_in[r]) == REG
5258 || GET_CODE (reload_in[r]) == MEM)
5259 && (reload_nregs[r] == max_group_size
5260 || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
5262 register rtx equiv
5263 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
5264 -1, NULL_PTR, 0, reload_mode[r]);
5265 int regno;
5267 if (equiv != 0)
5269 if (GET_CODE (equiv) == REG)
5270 regno = REGNO (equiv);
5271 else if (GET_CODE (equiv) == SUBREG)
5273 /* This must be a SUBREG of a hard register.
5274 Make a new REG since this might be used in an
5275 address and not all machines support SUBREGs
5276 there. */
5277 regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
5278 equiv = gen_rtx (REG, reload_mode[r], regno);
5280 else
5281 abort ();
5284 /* If we found a spill reg, reject it unless it is free
5285 and of the desired class. */
5286 if (equiv != 0
5287 && ((spill_reg_order[regno] >= 0
5288 && ! reload_reg_free_before_p (regno, reload_opnum[r],
5289 reload_when_needed[r]))
5290 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5291 regno)))
5292 equiv = 0;
5294 if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
5295 equiv = 0;
5297 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5298 equiv = 0;
5300 /* We found a register that contains the value we need.
5301 If this register is the same as an `earlyclobber' operand
5302 of the current insn, just mark it as a place to reload from
5303 since we can't use it as the reload register itself. */
5305 if (equiv != 0)
5306 for (i = 0; i < n_earlyclobbers; i++)
5307 if (reg_overlap_mentioned_for_reload_p (equiv,
5308 reload_earlyclobbers[i]))
5310 reload_override_in[r] = equiv;
5311 equiv = 0;
5312 break;
5315 /* JRV: If the equiv register we have found is explicitly
5316 clobbered in the current insn, mark but don't use, as above. */
5318 if (equiv != 0 && regno_clobbered_p (regno, insn))
5320 reload_override_in[r] = equiv;
5321 equiv = 0;
5324 /* If we found an equivalent reg, say no code need be generated
5325 to load it, and use it as our reload reg. */
5326 if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
5328 reload_reg_rtx[r] = equiv;
5329 reload_inherited[r] = 1;
5330 /* If it is a spill reg,
5331 mark the spill reg as in use for this insn. */
5332 i = spill_reg_order[regno];
5333 if (i >= 0)
5335 int nr = HARD_REGNO_NREGS (regno, reload_mode[r]);
5336 int k;
5337 mark_reload_reg_in_use (regno, reload_opnum[r],
5338 reload_when_needed[r],
5339 reload_mode[r]);
5340 for (k = 0; k < nr; k++)
5341 SET_HARD_REG_BIT (reload_reg_used_for_inherit, regno + k);
5346 /* If we found a register to use already, or if this is an optional
5347 reload, we are done. */
5348 if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
5349 continue;
5351 #if 0 /* No longer needed for correct operation. Might or might not
5352 give better code on the average. Want to experiment? */
5354 /* See if there is a later reload that has a class different from our
5355 class that intersects our class or that requires less register
5356 than our reload. If so, we must allocate a register to this
5357 reload now, since that reload might inherit a previous reload
5358 and take the only available register in our class. Don't do this
5359 for optional reloads since they will force all previous reloads
5360 to be allocated. Also don't do this for reloads that have been
5361 turned off. */
5363 for (i = j + 1; i < n_reloads; i++)
5365 int s = reload_order[i];
5367 if ((reload_in[s] == 0 && reload_out[s] == 0
5368 && ! reload_secondary_p[s])
5369 || reload_optional[s])
5370 continue;
5372 if ((reload_reg_class[s] != reload_reg_class[r]
5373 && reg_classes_intersect_p (reload_reg_class[r],
5374 reload_reg_class[s]))
5375 || reload_nregs[s] < reload_nregs[r])
5376 break;
5379 if (i == n_reloads)
5380 continue;
5382 allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
5383 #endif
5386 /* Now allocate reload registers for anything non-optional that
5387 didn't get one yet. */
5388 for (j = 0; j < n_reloads; j++)
5390 register int r = reload_order[j];
5392 /* Ignore reloads that got marked inoperative. */
5393 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
5394 continue;
5396 /* Skip reloads that already have a register allocated or are
5397 optional. */
5398 if (reload_reg_rtx[r] != 0 || reload_optional[r])
5399 continue;
5401 if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
5402 break;
5405 /* If that loop got all the way, we have won. */
5406 if (j == n_reloads)
5407 break;
5409 fail:
5410 /* Loop around and try without any inheritance. */
5411 /* First undo everything done by the failed attempt
5412 to allocate with inheritance. */
5413 bcopy ((char *) save_reload_reg_rtx, (char *) reload_reg_rtx,
5414 sizeof reload_reg_rtx);
5415 bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
5416 sizeof reload_inherited);
5417 bcopy ((char *) save_reload_inheritance_insn,
5418 (char *) reload_inheritance_insn,
5419 sizeof reload_inheritance_insn);
5420 bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
5421 sizeof reload_override_in);
5422 bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
5423 sizeof reload_spill_index);
5424 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
5425 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
5426 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
5427 save_reload_reg_used_in_op_addr);
5428 COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
5429 save_reload_reg_used_in_op_addr_reload);
5430 COPY_HARD_REG_SET (reload_reg_used_in_insn,
5431 save_reload_reg_used_in_insn);
5432 COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
5433 save_reload_reg_used_in_other_addr);
5435 for (i = 0; i < reload_n_operands; i++)
5437 COPY_HARD_REG_SET (reload_reg_used_in_input[i],
5438 save_reload_reg_used_in_input[i]);
5439 COPY_HARD_REG_SET (reload_reg_used_in_output[i],
5440 save_reload_reg_used_in_output[i]);
5441 COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
5442 save_reload_reg_used_in_input_addr[i]);
5443 COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
5444 save_reload_reg_used_in_output_addr[i]);
5448 /* If we thought we could inherit a reload, because it seemed that
5449 nothing else wanted the same reload register earlier in the insn,
5450 verify that assumption, now that all reloads have been assigned. */
5452 for (j = 0; j < n_reloads; j++)
5454 register int r = reload_order[j];
5456 if (reload_inherited[r] && reload_reg_rtx[r] != 0
5457 && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
5458 reload_opnum[r],
5459 reload_when_needed[r]))
5460 reload_inherited[r] = 0;
5462 /* If we found a better place to reload from,
5463 validate it in the same fashion, if it is a reload reg. */
5464 if (reload_override_in[r]
5465 && (GET_CODE (reload_override_in[r]) == REG
5466 || GET_CODE (reload_override_in[r]) == SUBREG))
5468 int regno = true_regnum (reload_override_in[r]);
5469 if (spill_reg_order[regno] >= 0
5470 && ! reload_reg_free_before_p (regno, reload_opnum[r],
5471 reload_when_needed[r]))
5472 reload_override_in[r] = 0;
5476 /* Now that reload_override_in is known valid,
5477 actually override reload_in. */
5478 for (j = 0; j < n_reloads; j++)
5479 if (reload_override_in[j])
5480 reload_in[j] = reload_override_in[j];
5482 /* If this reload won't be done because it has been cancelled or is
5483 optional and not inherited, clear reload_reg_rtx so other
5484 routines (such as subst_reloads) don't get confused. */
5485 for (j = 0; j < n_reloads; j++)
5486 if (reload_reg_rtx[j] != 0
5487 && ((reload_optional[j] && ! reload_inherited[j])
5488 || (reload_in[j] == 0 && reload_out[j] == 0
5489 && ! reload_secondary_p[j])))
5491 int regno = true_regnum (reload_reg_rtx[j]);
5493 if (spill_reg_order[regno] >= 0)
5494 clear_reload_reg_in_use (regno, reload_opnum[j],
5495 reload_when_needed[j], reload_mode[j]);
5496 reload_reg_rtx[j] = 0;
5499 /* Record which pseudos and which spill regs have output reloads. */
5500 for (j = 0; j < n_reloads; j++)
5502 register int r = reload_order[j];
5504 i = reload_spill_index[r];
5506 /* I is nonneg if this reload used one of the spill regs.
5507 If reload_reg_rtx[r] is 0, this is an optional reload
5508 that we opted to ignore. */
5509 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
5510 && reload_reg_rtx[r] != 0)
5512 register int nregno = REGNO (reload_out[r]);
5513 int nr = 1;
5515 if (nregno < FIRST_PSEUDO_REGISTER)
5516 nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
5518 while (--nr >= 0)
5519 reg_has_output_reload[nregno + nr] = 1;
5521 if (i >= 0)
5523 nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5524 while (--nr >= 0)
5525 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
5528 if (reload_when_needed[r] != RELOAD_OTHER
5529 && reload_when_needed[r] != RELOAD_FOR_OUTPUT
5530 && reload_when_needed[r] != RELOAD_FOR_INSN)
5531 abort ();
5536 /* If SMALL_REGISTER_CLASSES are defined, we may not have merged two
5537 reloads of the same item for fear that we might not have enough reload
5538 registers. However, normally they will get the same reload register
5539 and hence actually need not be loaded twice.
5541 Here we check for the most common case of this phenomenon: when we have
5542 a number of reloads for the same object, each of which were allocated
5543 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5544 reload, and is not modified in the insn itself. If we find such,
5545 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5546 This will not increase the number of spill registers needed and will
5547 prevent redundant code. */
5549 #ifdef SMALL_REGISTER_CLASSES
5551 static void
5552 merge_assigned_reloads (insn)
5553 rtx insn;
5555 int i, j;
5557 /* Scan all the reloads looking for ones that only load values and
5558 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5559 assigned and not modified by INSN. */
5561 for (i = 0; i < n_reloads; i++)
5563 if (reload_in[i] == 0 || reload_when_needed[i] == RELOAD_OTHER
5564 || reload_out[i] != 0 || reload_reg_rtx[i] == 0
5565 || reg_set_p (reload_reg_rtx[i], insn))
5566 continue;
5568 /* Look at all other reloads. Ensure that the only use of this
5569 reload_reg_rtx is in a reload that just loads the same value
5570 as we do. Note that any secondary reloads must be of the identical
5571 class since the values, modes, and result registers are the
5572 same, so we need not do anything with any secondary reloads. */
5574 for (j = 0; j < n_reloads; j++)
5576 if (i == j || reload_reg_rtx[j] == 0
5577 || ! reg_overlap_mentioned_p (reload_reg_rtx[j],
5578 reload_reg_rtx[i]))
5579 continue;
5581 /* If the reload regs aren't exactly the same (e.g, different modes)
5582 or if the values are different, we can't merge anything with this
5583 reload register. */
5585 if (! rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j])
5586 || reload_out[j] != 0 || reload_in[j] == 0
5587 || ! rtx_equal_p (reload_in[i], reload_in[j]))
5588 break;
5591 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
5592 we, in fact, found any matching reloads. */
5594 if (j == n_reloads)
5596 for (j = 0; j < n_reloads; j++)
5597 if (i != j && reload_reg_rtx[j] != 0
5598 && rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j]))
5600 reload_when_needed[i] = RELOAD_OTHER;
5601 reload_in[j] = 0;
5602 transfer_replacements (i, j);
5605 /* If this is now RELOAD_OTHER, look for any reloads that load
5606 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
5607 if they were for inputs, RELOAD_OTHER for outputs. Note that
5608 this test is equivalent to looking for reloads for this operand
5609 number. */
5611 if (reload_when_needed[i] == RELOAD_OTHER)
5612 for (j = 0; j < n_reloads; j++)
5613 if (reload_in[j] != 0
5614 && reload_when_needed[i] != RELOAD_OTHER
5615 && reg_overlap_mentioned_for_reload_p (reload_in[j],
5616 reload_in[i]))
5617 reload_when_needed[j]
5618 = reload_when_needed[i] == RELOAD_FOR_INPUT_ADDRESS
5619 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER;
5623 #endif /* SMALL_RELOAD_CLASSES */
5625 /* Output insns to reload values in and out of the chosen reload regs. */
5627 static void
5628 emit_reload_insns (insn)
5629 rtx insn;
5631 register int j;
5632 rtx input_reload_insns[MAX_RECOG_OPERANDS];
5633 rtx other_input_address_reload_insns = 0;
5634 rtx other_input_reload_insns = 0;
5635 rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
5636 rtx output_reload_insns[MAX_RECOG_OPERANDS];
5637 rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
5638 rtx operand_reload_insns = 0;
5639 rtx other_operand_reload_insns = 0;
5640 rtx following_insn = NEXT_INSN (insn);
5641 rtx before_insn = insn;
5642 int special;
5643 /* Values to be put in spill_reg_store are put here first. */
5644 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
5646 for (j = 0; j < reload_n_operands; j++)
5647 input_reload_insns[j] = input_address_reload_insns[j]
5648 = output_reload_insns[j] = output_address_reload_insns[j] = 0;
5650 /* Now output the instructions to copy the data into and out of the
5651 reload registers. Do these in the order that the reloads were reported,
5652 since reloads of base and index registers precede reloads of operands
5653 and the operands may need the base and index registers reloaded. */
5655 for (j = 0; j < n_reloads; j++)
5657 register rtx old;
5658 rtx oldequiv_reg = 0;
5660 if (reload_spill_index[j] >= 0)
5661 new_spill_reg_store[reload_spill_index[j]] = 0;
5663 old = reload_in[j];
5664 if (old != 0 && ! reload_inherited[j]
5665 && ! rtx_equal_p (reload_reg_rtx[j], old)
5666 && reload_reg_rtx[j] != 0)
5668 register rtx reloadreg = reload_reg_rtx[j];
5669 rtx oldequiv = 0;
5670 enum machine_mode mode;
5671 rtx *where;
5673 /* Determine the mode to reload in.
5674 This is very tricky because we have three to choose from.
5675 There is the mode the insn operand wants (reload_inmode[J]).
5676 There is the mode of the reload register RELOADREG.
5677 There is the intrinsic mode of the operand, which we could find
5678 by stripping some SUBREGs.
5679 It turns out that RELOADREG's mode is irrelevant:
5680 we can change that arbitrarily.
5682 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
5683 then the reload reg may not support QImode moves, so use SImode.
5684 If foo is in memory due to spilling a pseudo reg, this is safe,
5685 because the QImode value is in the least significant part of a
5686 slot big enough for a SImode. If foo is some other sort of
5687 memory reference, then it is impossible to reload this case,
5688 so previous passes had better make sure this never happens.
5690 Then consider a one-word union which has SImode and one of its
5691 members is a float, being fetched as (SUBREG:SF union:SI).
5692 We must fetch that as SFmode because we could be loading into
5693 a float-only register. In this case OLD's mode is correct.
5695 Consider an immediate integer: it has VOIDmode. Here we need
5696 to get a mode from something else.
5698 In some cases, there is a fourth mode, the operand's
5699 containing mode. If the insn specifies a containing mode for
5700 this operand, it overrides all others.
5702 I am not sure whether the algorithm here is always right,
5703 but it does the right things in those cases. */
5705 mode = GET_MODE (old);
5706 if (mode == VOIDmode)
5707 mode = reload_inmode[j];
5709 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5710 /* If we need a secondary register for this operation, see if
5711 the value is already in a register in that class. Don't
5712 do this if the secondary register will be used as a scratch
5713 register. */
5715 if (reload_secondary_in_reload[j] >= 0
5716 && reload_secondary_in_icode[j] == CODE_FOR_nothing
5717 && optimize)
5718 oldequiv
5719 = find_equiv_reg (old, insn,
5720 reload_reg_class[reload_secondary_in_reload[j]],
5721 -1, NULL_PTR, 0, mode);
5722 #endif
5724 /* If reloading from memory, see if there is a register
5725 that already holds the same value. If so, reload from there.
5726 We can pass 0 as the reload_reg_p argument because
5727 any other reload has either already been emitted,
5728 in which case find_equiv_reg will see the reload-insn,
5729 or has yet to be emitted, in which case it doesn't matter
5730 because we will use this equiv reg right away. */
5732 if (oldequiv == 0 && optimize
5733 && (GET_CODE (old) == MEM
5734 || (GET_CODE (old) == REG
5735 && REGNO (old) >= FIRST_PSEUDO_REGISTER
5736 && reg_renumber[REGNO (old)] < 0)))
5737 oldequiv = find_equiv_reg (old, insn, ALL_REGS,
5738 -1, NULL_PTR, 0, mode);
5740 if (oldequiv)
5742 int regno = true_regnum (oldequiv);
5744 /* If OLDEQUIV is a spill register, don't use it for this
5745 if any other reload needs it at an earlier stage of this insn
5746 or at this stage. */
5747 if (spill_reg_order[regno] >= 0
5748 && (! reload_reg_free_p (regno, reload_opnum[j],
5749 reload_when_needed[j])
5750 || ! reload_reg_free_before_p (regno, reload_opnum[j],
5751 reload_when_needed[j])))
5752 oldequiv = 0;
5754 /* If OLDEQUIV is not a spill register,
5755 don't use it if any other reload wants it. */
5756 if (spill_reg_order[regno] < 0)
5758 int k;
5759 for (k = 0; k < n_reloads; k++)
5760 if (reload_reg_rtx[k] != 0 && k != j
5761 && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
5762 oldequiv))
5764 oldequiv = 0;
5765 break;
5769 /* If it is no cheaper to copy from OLDEQUIV into the
5770 reload register than it would be to move from memory,
5771 don't use it. Likewise, if we need a secondary register
5772 or memory. */
5774 if (oldequiv != 0
5775 && ((REGNO_REG_CLASS (regno) != reload_reg_class[j]
5776 && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
5777 reload_reg_class[j])
5778 >= MEMORY_MOVE_COST (mode)))
5779 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5780 || (SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
5781 mode, oldequiv)
5782 != NO_REGS)
5783 #endif
5784 #ifdef SECONDARY_MEMORY_NEEDED
5785 || SECONDARY_MEMORY_NEEDED (reload_reg_class[j],
5786 REGNO_REG_CLASS (regno),
5787 mode)
5788 #endif
5790 oldequiv = 0;
5793 if (oldequiv == 0)
5794 oldequiv = old;
5795 else if (GET_CODE (oldequiv) == REG)
5796 oldequiv_reg = oldequiv;
5797 else if (GET_CODE (oldequiv) == SUBREG)
5798 oldequiv_reg = SUBREG_REG (oldequiv);
5800 /* If we are reloading from a register that was recently stored in
5801 with an output-reload, see if we can prove there was
5802 actually no need to store the old value in it. */
5804 if (optimize && GET_CODE (oldequiv) == REG
5805 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
5806 && spill_reg_order[REGNO (oldequiv)] >= 0
5807 && spill_reg_store[spill_reg_order[REGNO (oldequiv)]] != 0
5808 && find_reg_note (insn, REG_DEAD, reload_in[j])
5809 /* This is unsafe if operand occurs more than once in current
5810 insn. Perhaps some occurrences weren't reloaded. */
5811 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
5812 delete_output_reload
5813 (insn, j, spill_reg_store[spill_reg_order[REGNO (oldequiv)]]);
5815 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
5816 then load RELOADREG from OLDEQUIV. Note that we cannot use
5817 gen_lowpart_common since it can do the wrong thing when
5818 RELOADREG has a multi-word mode. Note that RELOADREG
5819 must always be a REG here. */
5821 if (GET_MODE (reloadreg) != mode)
5822 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5823 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
5824 oldequiv = SUBREG_REG (oldequiv);
5825 if (GET_MODE (oldequiv) != VOIDmode
5826 && mode != GET_MODE (oldequiv))
5827 oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
5829 /* Switch to the right place to emit the reload insns. */
5830 switch (reload_when_needed[j])
5832 case RELOAD_OTHER:
5833 where = &other_input_reload_insns;
5834 break;
5835 case RELOAD_FOR_INPUT:
5836 where = &input_reload_insns[reload_opnum[j]];
5837 break;
5838 case RELOAD_FOR_INPUT_ADDRESS:
5839 where = &input_address_reload_insns[reload_opnum[j]];
5840 break;
5841 case RELOAD_FOR_OUTPUT_ADDRESS:
5842 where = &output_address_reload_insns[reload_opnum[j]];
5843 break;
5844 case RELOAD_FOR_OPERAND_ADDRESS:
5845 where = &operand_reload_insns;
5846 break;
5847 case RELOAD_FOR_OPADDR_ADDR:
5848 where = &other_operand_reload_insns;
5849 break;
5850 case RELOAD_FOR_OTHER_ADDRESS:
5851 where = &other_input_address_reload_insns;
5852 break;
5853 default:
5854 abort ();
5857 push_to_sequence (*where);
5858 special = 0;
5860 /* Auto-increment addresses must be reloaded in a special way. */
5861 if (GET_CODE (oldequiv) == POST_INC
5862 || GET_CODE (oldequiv) == POST_DEC
5863 || GET_CODE (oldequiv) == PRE_INC
5864 || GET_CODE (oldequiv) == PRE_DEC)
5866 /* We are not going to bother supporting the case where a
5867 incremented register can't be copied directly from
5868 OLDEQUIV since this seems highly unlikely. */
5869 if (reload_secondary_in_reload[j] >= 0)
5870 abort ();
5871 /* Prevent normal processing of this reload. */
5872 special = 1;
5873 /* Output a special code sequence for this case. */
5874 inc_for_reload (reloadreg, oldequiv, reload_inc[j]);
5877 /* If we are reloading a pseudo-register that was set by the previous
5878 insn, see if we can get rid of that pseudo-register entirely
5879 by redirecting the previous insn into our reload register. */
5881 else if (optimize && GET_CODE (old) == REG
5882 && REGNO (old) >= FIRST_PSEUDO_REGISTER
5883 && dead_or_set_p (insn, old)
5884 /* This is unsafe if some other reload
5885 uses the same reg first. */
5886 && reload_reg_free_before_p (REGNO (reloadreg),
5887 reload_opnum[j],
5888 reload_when_needed[j]))
5890 rtx temp = PREV_INSN (insn);
5891 while (temp && GET_CODE (temp) == NOTE)
5892 temp = PREV_INSN (temp);
5893 if (temp
5894 && GET_CODE (temp) == INSN
5895 && GET_CODE (PATTERN (temp)) == SET
5896 && SET_DEST (PATTERN (temp)) == old
5897 /* Make sure we can access insn_operand_constraint. */
5898 && asm_noperands (PATTERN (temp)) < 0
5899 /* This is unsafe if prev insn rejects our reload reg. */
5900 && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
5901 reloadreg)
5902 /* This is unsafe if operand occurs more than once in current
5903 insn. Perhaps some occurrences aren't reloaded. */
5904 && count_occurrences (PATTERN (insn), old) == 1
5905 /* Don't risk splitting a matching pair of operands. */
5906 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
5908 /* Store into the reload register instead of the pseudo. */
5909 SET_DEST (PATTERN (temp)) = reloadreg;
5910 /* If these are the only uses of the pseudo reg,
5911 pretend for GDB it lives in the reload reg we used. */
5912 if (reg_n_deaths[REGNO (old)] == 1
5913 && reg_n_sets[REGNO (old)] == 1)
5915 reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
5916 alter_reg (REGNO (old), -1);
5918 special = 1;
5922 /* We can't do that, so output an insn to load RELOADREG. */
5924 if (! special)
5926 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5927 rtx second_reload_reg = 0;
5928 enum insn_code icode;
5930 /* If we have a secondary reload, pick up the secondary register
5931 and icode, if any. If OLDEQUIV and OLD are different or
5932 if this is an in-out reload, recompute whether or not we
5933 still need a secondary register and what the icode should
5934 be. If we still need a secondary register and the class or
5935 icode is different, go back to reloading from OLD if using
5936 OLDEQUIV means that we got the wrong type of register. We
5937 cannot have different class or icode due to an in-out reload
5938 because we don't make such reloads when both the input and
5939 output need secondary reload registers. */
5941 if (reload_secondary_in_reload[j] >= 0)
5943 int secondary_reload = reload_secondary_in_reload[j];
5944 rtx real_oldequiv = oldequiv;
5945 rtx real_old = old;
5947 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
5948 and similarly for OLD.
5949 See comments in get_secondary_reload in reload.c. */
5950 if (GET_CODE (oldequiv) == REG
5951 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
5952 && reg_equiv_mem[REGNO (oldequiv)] != 0)
5953 real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
5955 if (GET_CODE (old) == REG
5956 && REGNO (old) >= FIRST_PSEUDO_REGISTER
5957 && reg_equiv_mem[REGNO (old)] != 0)
5958 real_old = reg_equiv_mem[REGNO (old)];
5960 second_reload_reg = reload_reg_rtx[secondary_reload];
5961 icode = reload_secondary_in_icode[j];
5963 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
5964 || (reload_in[j] != 0 && reload_out[j] != 0))
5966 enum reg_class new_class
5967 = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
5968 mode, real_oldequiv);
5970 if (new_class == NO_REGS)
5971 second_reload_reg = 0;
5972 else
5974 enum insn_code new_icode;
5975 enum machine_mode new_mode;
5977 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
5978 REGNO (second_reload_reg)))
5979 oldequiv = old, real_oldequiv = real_old;
5980 else
5982 new_icode = reload_in_optab[(int) mode];
5983 if (new_icode != CODE_FOR_nothing
5984 && ((insn_operand_predicate[(int) new_icode][0]
5985 && ! ((*insn_operand_predicate[(int) new_icode][0])
5986 (reloadreg, mode)))
5987 || (insn_operand_predicate[(int) new_icode][1]
5988 && ! ((*insn_operand_predicate[(int) new_icode][1])
5989 (real_oldequiv, mode)))))
5990 new_icode = CODE_FOR_nothing;
5992 if (new_icode == CODE_FOR_nothing)
5993 new_mode = mode;
5994 else
5995 new_mode = insn_operand_mode[(int) new_icode][2];
5997 if (GET_MODE (second_reload_reg) != new_mode)
5999 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6000 new_mode))
6001 oldequiv = old, real_oldequiv = real_old;
6002 else
6003 second_reload_reg
6004 = gen_rtx (REG, new_mode,
6005 REGNO (second_reload_reg));
6011 /* If we still need a secondary reload register, check
6012 to see if it is being used as a scratch or intermediate
6013 register and generate code appropriately. If we need
6014 a scratch register, use REAL_OLDEQUIV since the form of
6015 the insn may depend on the actual address if it is
6016 a MEM. */
6018 if (second_reload_reg)
6020 if (icode != CODE_FOR_nothing)
6022 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6023 second_reload_reg));
6024 special = 1;
6026 else
6028 /* See if we need a scratch register to load the
6029 intermediate register (a tertiary reload). */
6030 enum insn_code tertiary_icode
6031 = reload_secondary_in_icode[secondary_reload];
6033 if (tertiary_icode != CODE_FOR_nothing)
6035 rtx third_reload_reg
6036 = reload_reg_rtx[reload_secondary_in_reload[secondary_reload]];
6038 emit_insn ((GEN_FCN (tertiary_icode)
6039 (second_reload_reg, real_oldequiv,
6040 third_reload_reg)));
6042 else
6043 gen_reload (second_reload_reg, oldequiv,
6044 reload_opnum[j],
6045 reload_when_needed[j]);
6047 oldequiv = second_reload_reg;
6051 #endif
6053 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6054 gen_reload (reloadreg, oldequiv, reload_opnum[j],
6055 reload_when_needed[j]);
6057 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
6058 /* We may have to make a REG_DEAD note for the secondary reload
6059 register in the insns we just made. Find the last insn that
6060 mentioned the register. */
6061 if (! special && second_reload_reg
6062 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
6064 rtx prev;
6066 for (prev = get_last_insn (); prev;
6067 prev = PREV_INSN (prev))
6068 if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
6069 && reg_overlap_mentioned_for_reload_p (second_reload_reg,
6070 PATTERN (prev)))
6072 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
6073 second_reload_reg,
6074 REG_NOTES (prev));
6075 break;
6078 #endif
6081 /* End this sequence. */
6082 *where = get_insns ();
6083 end_sequence ();
6086 /* Add a note saying the input reload reg
6087 dies in this insn, if anyone cares. */
6088 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6089 if (old != 0
6090 && reload_reg_rtx[j] != old
6091 && reload_reg_rtx[j] != 0
6092 && reload_out[j] == 0
6093 && ! reload_inherited[j]
6094 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
6096 register rtx reloadreg = reload_reg_rtx[j];
6098 #if 0
6099 /* We can't abort here because we need to support this for sched.c.
6100 It's not terrible to miss a REG_DEAD note, but we should try
6101 to figure out how to do this correctly. */
6102 /* The code below is incorrect for address-only reloads. */
6103 if (reload_when_needed[j] != RELOAD_OTHER
6104 && reload_when_needed[j] != RELOAD_FOR_INPUT)
6105 abort ();
6106 #endif
6108 /* Add a death note to this insn, for an input reload. */
6110 if ((reload_when_needed[j] == RELOAD_OTHER
6111 || reload_when_needed[j] == RELOAD_FOR_INPUT)
6112 && ! dead_or_set_p (insn, reloadreg))
6113 REG_NOTES (insn)
6114 = gen_rtx (EXPR_LIST, REG_DEAD,
6115 reloadreg, REG_NOTES (insn));
6118 /* When we inherit a reload, the last marked death of the reload reg
6119 may no longer really be a death. */
6120 if (reload_reg_rtx[j] != 0
6121 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
6122 && reload_inherited[j])
6124 /* Handle inheriting an output reload.
6125 Remove the death note from the output reload insn. */
6126 if (reload_spill_index[j] >= 0
6127 && GET_CODE (reload_in[j]) == REG
6128 && spill_reg_store[reload_spill_index[j]] != 0
6129 && find_regno_note (spill_reg_store[reload_spill_index[j]],
6130 REG_DEAD, REGNO (reload_reg_rtx[j])))
6131 remove_death (REGNO (reload_reg_rtx[j]),
6132 spill_reg_store[reload_spill_index[j]]);
6133 /* Likewise for input reloads that were inherited. */
6134 else if (reload_spill_index[j] >= 0
6135 && GET_CODE (reload_in[j]) == REG
6136 && spill_reg_store[reload_spill_index[j]] == 0
6137 && reload_inheritance_insn[j] != 0
6138 && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
6139 REGNO (reload_reg_rtx[j])))
6140 remove_death (REGNO (reload_reg_rtx[j]),
6141 reload_inheritance_insn[j]);
6142 else
6144 rtx prev;
6146 /* We got this register from find_equiv_reg.
6147 Search back for its last death note and get rid of it.
6148 But don't search back too far.
6149 Don't go past a place where this reg is set,
6150 since a death note before that remains valid. */
6151 for (prev = PREV_INSN (insn);
6152 prev && GET_CODE (prev) != CODE_LABEL;
6153 prev = PREV_INSN (prev))
6154 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6155 && dead_or_set_p (prev, reload_reg_rtx[j]))
6157 if (find_regno_note (prev, REG_DEAD,
6158 REGNO (reload_reg_rtx[j])))
6159 remove_death (REGNO (reload_reg_rtx[j]), prev);
6160 break;
6165 /* We might have used find_equiv_reg above to choose an alternate
6166 place from which to reload. If so, and it died, we need to remove
6167 that death and move it to one of the insns we just made. */
6169 if (oldequiv_reg != 0
6170 && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
6172 rtx prev, prev1;
6174 for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
6175 prev = PREV_INSN (prev))
6176 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6177 && dead_or_set_p (prev, oldequiv_reg))
6179 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
6181 for (prev1 = this_reload_insn;
6182 prev1; prev1 = PREV_INSN (prev1))
6183 if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
6184 && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
6185 PATTERN (prev1)))
6187 REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
6188 oldequiv_reg,
6189 REG_NOTES (prev1));
6190 break;
6192 remove_death (REGNO (oldequiv_reg), prev);
6194 break;
6197 #endif
6199 /* If we are reloading a register that was recently stored in with an
6200 output-reload, see if we can prove there was
6201 actually no need to store the old value in it. */
6203 if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
6204 && reload_in[j] != 0
6205 && GET_CODE (reload_in[j]) == REG
6206 #if 0
6207 /* There doesn't seem to be any reason to restrict this to pseudos
6208 and doing so loses in the case where we are copying from a
6209 register of the wrong class. */
6210 && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
6211 #endif
6212 && spill_reg_store[reload_spill_index[j]] != 0
6213 /* This is unsafe if some other reload uses the same reg first. */
6214 && reload_reg_free_before_p (spill_regs[reload_spill_index[j]],
6215 reload_opnum[j], reload_when_needed[j])
6216 && dead_or_set_p (insn, reload_in[j])
6217 /* This is unsafe if operand occurs more than once in current
6218 insn. Perhaps some occurrences weren't reloaded. */
6219 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
6220 delete_output_reload (insn, j,
6221 spill_reg_store[reload_spill_index[j]]);
6223 /* Input-reloading is done. Now do output-reloading,
6224 storing the value from the reload-register after the main insn
6225 if reload_out[j] is nonzero.
6227 ??? At some point we need to support handling output reloads of
6228 JUMP_INSNs or insns that set cc0. */
6229 old = reload_out[j];
6230 if (old != 0
6231 && reload_reg_rtx[j] != old
6232 && reload_reg_rtx[j] != 0)
6234 register rtx reloadreg = reload_reg_rtx[j];
6235 register rtx second_reloadreg = 0;
6236 rtx note, p;
6237 enum machine_mode mode;
6238 int special = 0;
6240 /* An output operand that dies right away does need a reload,
6241 but need not be copied from it. Show the new location in the
6242 REG_UNUSED note. */
6243 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6244 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6246 XEXP (note, 0) = reload_reg_rtx[j];
6247 continue;
6249 else if (GET_CODE (old) == SCRATCH)
6250 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6251 but we don't want to make an output reload. */
6252 continue;
6254 #if 0
6255 /* Strip off of OLD any size-increasing SUBREGs such as
6256 (SUBREG:SI foo:QI 0). */
6258 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
6259 && (GET_MODE_SIZE (GET_MODE (old))
6260 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
6261 old = SUBREG_REG (old);
6262 #endif
6264 /* If is a JUMP_INSN, we can't support output reloads yet. */
6265 if (GET_CODE (insn) == JUMP_INSN)
6266 abort ();
6268 push_to_sequence (output_reload_insns[reload_opnum[j]]);
6270 /* Determine the mode to reload in.
6271 See comments above (for input reloading). */
6273 mode = GET_MODE (old);
6274 if (mode == VOIDmode)
6276 /* VOIDmode should never happen for an output. */
6277 if (asm_noperands (PATTERN (insn)) < 0)
6278 /* It's the compiler's fault. */
6279 fatal_insn ("VOIDmode on an output", insn);
6280 error_for_asm (insn, "output operand is constant in `asm'");
6281 /* Prevent crash--use something we know is valid. */
6282 mode = word_mode;
6283 old = gen_rtx (REG, mode, REGNO (reloadreg));
6286 if (GET_MODE (reloadreg) != mode)
6287 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
6289 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6291 /* If we need two reload regs, set RELOADREG to the intermediate
6292 one, since it will be stored into OLD. We might need a secondary
6293 register only for an input reload, so check again here. */
6295 if (reload_secondary_out_reload[j] >= 0)
6297 rtx real_old = old;
6299 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6300 && reg_equiv_mem[REGNO (old)] != 0)
6301 real_old = reg_equiv_mem[REGNO (old)];
6303 if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
6304 mode, real_old)
6305 != NO_REGS))
6307 second_reloadreg = reloadreg;
6308 reloadreg = reload_reg_rtx[reload_secondary_out_reload[j]];
6310 /* See if RELOADREG is to be used as a scratch register
6311 or as an intermediate register. */
6312 if (reload_secondary_out_icode[j] != CODE_FOR_nothing)
6314 emit_insn ((GEN_FCN (reload_secondary_out_icode[j])
6315 (real_old, second_reloadreg, reloadreg)));
6316 special = 1;
6318 else
6320 /* See if we need both a scratch and intermediate reload
6321 register. */
6323 int secondary_reload = reload_secondary_out_reload[j];
6324 enum insn_code tertiary_icode
6325 = reload_secondary_out_icode[secondary_reload];
6327 if (GET_MODE (reloadreg) != mode)
6328 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
6330 if (tertiary_icode != CODE_FOR_nothing)
6332 rtx third_reloadreg
6333 = reload_reg_rtx[reload_secondary_out_reload[secondary_reload]];
6335 /* Copy primary reload reg to secondary reload reg.
6336 (Note that these have been swapped above, then
6337 secondary reload reg to OLD using our insn. */
6339 gen_reload (reloadreg, second_reloadreg,
6340 reload_opnum[j], reload_when_needed[j]);
6341 emit_insn ((GEN_FCN (tertiary_icode)
6342 (real_old, reloadreg, third_reloadreg)));
6343 special = 1;
6346 else
6347 /* Copy between the reload regs here and then to
6348 OUT later. */
6350 gen_reload (reloadreg, second_reloadreg,
6351 reload_opnum[j], reload_when_needed[j]);
6355 #endif
6357 /* Output the last reload insn. */
6358 if (! special)
6359 gen_reload (old, reloadreg, reload_opnum[j],
6360 reload_when_needed[j]);
6362 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6363 /* If final will look at death notes for this reg,
6364 put one on the last output-reload insn to use it. Similarly
6365 for any secondary register. */
6366 if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
6367 for (p = get_last_insn (); p; p = PREV_INSN (p))
6368 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6369 && reg_overlap_mentioned_for_reload_p (reloadreg,
6370 PATTERN (p)))
6371 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
6372 reloadreg, REG_NOTES (p));
6374 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6375 if (! special
6376 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
6377 for (p = get_last_insn (); p; p = PREV_INSN (p))
6378 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6379 && reg_overlap_mentioned_for_reload_p (second_reloadreg,
6380 PATTERN (p)))
6381 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
6382 second_reloadreg, REG_NOTES (p));
6383 #endif
6384 #endif
6385 /* Look at all insns we emitted, just to be safe. */
6386 for (p = get_insns (); p; p = NEXT_INSN (p))
6387 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6389 /* If this output reload doesn't come from a spill reg,
6390 clear any memory of reloaded copies of the pseudo reg.
6391 If this output reload comes from a spill reg,
6392 reg_has_output_reload will make this do nothing. */
6393 note_stores (PATTERN (p), forget_old_reloads_1);
6395 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p))
6396 && reload_spill_index[j] >= 0)
6397 new_spill_reg_store[reload_spill_index[j]] = p;
6400 output_reload_insns[reload_opnum[j]] = get_insns ();
6401 end_sequence ();
6405 /* Now write all the insns we made for reloads in the order expected by
6406 the allocation functions. Prior to the insn being reloaded, we write
6407 the following reloads:
6409 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
6411 RELOAD_OTHER reloads.
6413 For each operand, any RELOAD_FOR_INPUT_ADDRESS reloads followed by
6414 the RELOAD_FOR_INPUT reload for the operand.
6416 RELOAD_FOR_OPADDR_ADDRS reloads.
6418 RELOAD_FOR_OPERAND_ADDRESS reloads.
6420 After the insn being reloaded, we write the following:
6422 For each operand, any RELOAD_FOR_OUTPUT_ADDRESS reload followed by
6423 the RELOAD_FOR_OUTPUT reload for that operand. */
6425 emit_insns_before (other_input_address_reload_insns, before_insn);
6426 emit_insns_before (other_input_reload_insns, before_insn);
6428 for (j = 0; j < reload_n_operands; j++)
6430 emit_insns_before (input_address_reload_insns[j], before_insn);
6431 emit_insns_before (input_reload_insns[j], before_insn);
6434 emit_insns_before (other_operand_reload_insns, before_insn);
6435 emit_insns_before (operand_reload_insns, before_insn);
6437 for (j = 0; j < reload_n_operands; j++)
6439 emit_insns_before (output_address_reload_insns[j], following_insn);
6440 emit_insns_before (output_reload_insns[j], following_insn);
6443 /* Move death notes from INSN
6444 to output-operand-address and output reload insns. */
6445 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6447 rtx insn1;
6448 /* Loop over those insns, last ones first. */
6449 for (insn1 = PREV_INSN (following_insn); insn1 != insn;
6450 insn1 = PREV_INSN (insn1))
6451 if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
6453 rtx source = SET_SRC (PATTERN (insn1));
6454 rtx dest = SET_DEST (PATTERN (insn1));
6456 /* The note we will examine next. */
6457 rtx reg_notes = REG_NOTES (insn);
6458 /* The place that pointed to this note. */
6459 rtx *prev_reg_note = &REG_NOTES (insn);
6461 /* If the note is for something used in the source of this
6462 reload insn, or in the output address, move the note. */
6463 while (reg_notes)
6465 rtx next_reg_notes = XEXP (reg_notes, 1);
6466 if (REG_NOTE_KIND (reg_notes) == REG_DEAD
6467 && GET_CODE (XEXP (reg_notes, 0)) == REG
6468 && ((GET_CODE (dest) != REG
6469 && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6470 dest))
6471 || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6472 source)))
6474 *prev_reg_note = next_reg_notes;
6475 XEXP (reg_notes, 1) = REG_NOTES (insn1);
6476 REG_NOTES (insn1) = reg_notes;
6478 else
6479 prev_reg_note = &XEXP (reg_notes, 1);
6481 reg_notes = next_reg_notes;
6485 #endif
6487 /* For all the spill regs newly reloaded in this instruction,
6488 record what they were reloaded from, so subsequent instructions
6489 can inherit the reloads.
6491 Update spill_reg_store for the reloads of this insn.
6492 Copy the elements that were updated in the loop above. */
6494 for (j = 0; j < n_reloads; j++)
6496 register int r = reload_order[j];
6497 register int i = reload_spill_index[r];
6499 /* I is nonneg if this reload used one of the spill regs.
6500 If reload_reg_rtx[r] is 0, this is an optional reload
6501 that we opted to ignore.
6503 Also ignore reloads that don't reach the end of the insn,
6504 since we will eventually see the one that does. */
6506 if (i >= 0 && reload_reg_rtx[r] != 0
6507 && reload_reg_reaches_end_p (spill_regs[i], reload_opnum[r],
6508 reload_when_needed[r]))
6510 /* First, clear out memory of what used to be in this spill reg.
6511 If consecutive registers are used, clear them all. */
6512 int nr
6513 = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
6514 int k;
6516 for (k = 0; k < nr; k++)
6518 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
6519 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
6522 /* Maybe the spill reg contains a copy of reload_out. */
6523 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
6525 register int nregno = REGNO (reload_out[r]);
6526 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
6527 : HARD_REGNO_NREGS (nregno,
6528 GET_MODE (reload_reg_rtx[r])));
6530 spill_reg_store[i] = new_spill_reg_store[i];
6531 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
6533 /* If NREGNO is a hard register, it may occupy more than
6534 one register. If it does, say what is in the
6535 rest of the registers assuming that both registers
6536 agree on how many words the object takes. If not,
6537 invalidate the subsequent registers. */
6539 if (nregno < FIRST_PSEUDO_REGISTER)
6540 for (k = 1; k < nnr; k++)
6541 reg_last_reload_reg[nregno + k]
6542 = (nr == nnr ? gen_rtx (REG,
6543 reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
6544 REGNO (reload_reg_rtx[r]) + k)
6545 : 0);
6547 /* Now do the inverse operation. */
6548 for (k = 0; k < nr; k++)
6550 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
6551 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr ? nregno
6552 : nregno + k);
6553 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
6557 /* Maybe the spill reg contains a copy of reload_in. Only do
6558 something if there will not be an output reload for
6559 the register being reloaded. */
6560 else if (reload_out[r] == 0
6561 && reload_in[r] != 0
6562 && ((GET_CODE (reload_in[r]) == REG
6563 && ! reg_has_output_reload[REGNO (reload_in[r])]
6564 || (GET_CODE (reload_in_reg[r]) == REG
6565 && ! reg_has_output_reload[REGNO (reload_in_reg[r])]))))
6567 register int nregno;
6568 int nnr;
6570 if (GET_CODE (reload_in[r]) == REG)
6571 nregno = REGNO (reload_in[r]);
6572 else
6573 nregno = REGNO (reload_in_reg[r]);
6575 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
6576 : HARD_REGNO_NREGS (nregno,
6577 GET_MODE (reload_reg_rtx[r])));
6579 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
6581 if (nregno < FIRST_PSEUDO_REGISTER)
6582 for (k = 1; k < nnr; k++)
6583 reg_last_reload_reg[nregno + k]
6584 = (nr == nnr ? gen_rtx (REG,
6585 reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
6586 REGNO (reload_reg_rtx[r]) + k)
6587 : 0);
6589 /* Unless we inherited this reload, show we haven't
6590 recently done a store. */
6591 if (! reload_inherited[r])
6592 spill_reg_store[i] = 0;
6594 for (k = 0; k < nr; k++)
6596 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
6597 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr ? nregno
6598 : nregno + k);
6599 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
6600 = insn;
6605 /* The following if-statement was #if 0'd in 1.34 (or before...).
6606 It's reenabled in 1.35 because supposedly nothing else
6607 deals with this problem. */
6609 /* If a register gets output-reloaded from a non-spill register,
6610 that invalidates any previous reloaded copy of it.
6611 But forget_old_reloads_1 won't get to see it, because
6612 it thinks only about the original insn. So invalidate it here. */
6613 if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
6615 register int nregno = REGNO (reload_out[r]);
6616 int num_regs = HARD_REGNO_NREGS (nregno, GET_MODE (reload_out[r]));
6618 while (num_regs-- > 0)
6619 reg_last_reload_reg[nregno + num_regs] = 0;
6624 /* Emit code to perform a reload from IN (which may be a reload register) to
6625 OUT (which may also be a reload register). IN or OUT is from operand
6626 OPNUM with reload type TYPE.
6628 Returns first insn emitted. */
6631 gen_reload (out, in, opnum, type)
6632 rtx out;
6633 rtx in;
6634 int opnum;
6635 enum reload_type type;
6637 rtx last = get_last_insn ();
6639 /* How to do this reload can get quite tricky. Normally, we are being
6640 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
6641 register that didn't get a hard register. In that case we can just
6642 call emit_move_insn.
6644 We can also be asked to reload a PLUS that adds a register or a MEM to
6645 another register, constant or MEM. This can occur during frame pointer
6646 elimination and while reloading addresses. This case is handled by
6647 trying to emit a single insn to perform the add. If it is not valid,
6648 we use a two insn sequence.
6650 Finally, we could be called to handle an 'o' constraint by putting
6651 an address into a register. In that case, we first try to do this
6652 with a named pattern of "reload_load_address". If no such pattern
6653 exists, we just emit a SET insn and hope for the best (it will normally
6654 be valid on machines that use 'o').
6656 This entire process is made complex because reload will never
6657 process the insns we generate here and so we must ensure that
6658 they will fit their constraints and also by the fact that parts of
6659 IN might be being reloaded separately and replaced with spill registers.
6660 Because of this, we are, in some sense, just guessing the right approach
6661 here. The one listed above seems to work.
6663 ??? At some point, this whole thing needs to be rethought. */
6665 if (GET_CODE (in) == PLUS
6666 && (GET_CODE (XEXP (in, 0)) == REG
6667 || GET_CODE (XEXP (in, 0)) == MEM)
6668 && (GET_CODE (XEXP (in, 1)) == REG
6669 || CONSTANT_P (XEXP (in, 1))
6670 || GET_CODE (XEXP (in, 1)) == MEM))
6672 /* We need to compute the sum of a register or a MEM and another
6673 register, constant, or MEM, and put it into the reload
6674 register. The best possible way of doing this is if the machine
6675 has a three-operand ADD insn that accepts the required operands.
6677 The simplest approach is to try to generate such an insn and see if it
6678 is recognized and matches its constraints. If so, it can be used.
6680 It might be better not to actually emit the insn unless it is valid,
6681 but we need to pass the insn as an operand to `recog' and
6682 `insn_extract' and it is simpler to emit and then delete the insn if
6683 not valid than to dummy things up. */
6685 rtx op0, op1, tem, insn;
6686 int code;
6688 op0 = find_replacement (&XEXP (in, 0));
6689 op1 = find_replacement (&XEXP (in, 1));
6691 /* Since constraint checking is strict, commutativity won't be
6692 checked, so we need to do that here to avoid spurious failure
6693 if the add instruction is two-address and the second operand
6694 of the add is the same as the reload reg, which is frequently
6695 the case. If the insn would be A = B + A, rearrange it so
6696 it will be A = A + B as constrain_operands expects. */
6698 if (GET_CODE (XEXP (in, 1)) == REG
6699 && REGNO (out) == REGNO (XEXP (in, 1)))
6700 tem = op0, op0 = op1, op1 = tem;
6702 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
6703 in = gen_rtx (PLUS, GET_MODE (in), op0, op1);
6705 insn = emit_insn (gen_rtx (SET, VOIDmode, out, in));
6706 code = recog_memoized (insn);
6708 if (code >= 0)
6710 insn_extract (insn);
6711 /* We want constrain operands to treat this insn strictly in
6712 its validity determination, i.e., the way it would after reload
6713 has completed. */
6714 if (constrain_operands (code, 1))
6715 return insn;
6718 delete_insns_since (last);
6720 /* If that failed, we must use a conservative two-insn sequence.
6721 use move to copy constant, MEM, or pseudo register to the reload
6722 register since "move" will be able to handle an arbitrary operand,
6723 unlike add which can't, in general. Then add the registers.
6725 If there is another way to do this for a specific machine, a
6726 DEFINE_PEEPHOLE should be specified that recognizes the sequence
6727 we emit below. */
6729 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM
6730 || (GET_CODE (op1) == REG
6731 && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
6732 tem = op0, op0 = op1, op1 = tem;
6734 emit_insn (gen_move_insn (out, op0));
6736 /* If OP0 and OP1 are the same, we can use OUT for OP1.
6737 This fixes a problem on the 32K where the stack pointer cannot
6738 be used as an operand of an add insn. */
6740 if (rtx_equal_p (op0, op1))
6741 op1 = out;
6743 insn = emit_insn (gen_add2_insn (out, op1));
6745 /* If that failed, copy the address register to the reload register.
6746 Then add the constant to the reload register. */
6748 code = recog_memoized (insn);
6750 if (code >= 0)
6752 insn_extract (insn);
6753 /* We want constrain operands to treat this insn strictly in
6754 its validity determination, i.e., the way it would after reload
6755 has completed. */
6756 if (constrain_operands (code, 1))
6757 return insn;
6760 delete_insns_since (last);
6762 emit_insn (gen_move_insn (out, op1));
6763 emit_insn (gen_add2_insn (out, op0));
6766 #ifdef SECONDARY_MEMORY_NEEDED
6767 /* If we need a memory location to do the move, do it that way. */
6768 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
6769 && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
6770 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
6771 REGNO_REG_CLASS (REGNO (out)),
6772 GET_MODE (out)))
6774 /* Get the memory to use and rewrite both registers to its mode. */
6775 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
6777 if (GET_MODE (loc) != GET_MODE (out))
6778 out = gen_rtx (REG, GET_MODE (loc), REGNO (out));
6780 if (GET_MODE (loc) != GET_MODE (in))
6781 in = gen_rtx (REG, GET_MODE (loc), REGNO (in));
6783 emit_insn (gen_move_insn (loc, in));
6784 emit_insn (gen_move_insn (out, loc));
6786 #endif
6788 /* If IN is a simple operand, use gen_move_insn. */
6789 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
6790 emit_insn (gen_move_insn (out, in));
6792 #ifdef HAVE_reload_load_address
6793 else if (HAVE_reload_load_address)
6794 emit_insn (gen_reload_load_address (out, in));
6795 #endif
6797 /* Otherwise, just write (set OUT IN) and hope for the best. */
6798 else
6799 emit_insn (gen_rtx (SET, VOIDmode, out, in));
6801 /* Return the first insn emitted.
6802 We can not just return get_last_insn, because there may have
6803 been multiple instructions emitted. Also note that gen_move_insn may
6804 emit more than one insn itself, so we can not assume that there is one
6805 insn emitted per emit_insn_before call. */
6807 return last ? NEXT_INSN (last) : get_insns ();
6810 /* Delete a previously made output-reload
6811 whose result we now believe is not needed.
6812 First we double-check.
6814 INSN is the insn now being processed.
6815 OUTPUT_RELOAD_INSN is the insn of the output reload.
6816 J is the reload-number for this insn. */
6818 static void
6819 delete_output_reload (insn, j, output_reload_insn)
6820 rtx insn;
6821 int j;
6822 rtx output_reload_insn;
6824 register rtx i1;
6826 /* Get the raw pseudo-register referred to. */
6828 rtx reg = reload_in[j];
6829 while (GET_CODE (reg) == SUBREG)
6830 reg = SUBREG_REG (reg);
6832 /* If the pseudo-reg we are reloading is no longer referenced
6833 anywhere between the store into it and here,
6834 and no jumps or labels intervene, then the value can get
6835 here through the reload reg alone.
6836 Otherwise, give up--return. */
6837 for (i1 = NEXT_INSN (output_reload_insn);
6838 i1 != insn; i1 = NEXT_INSN (i1))
6840 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
6841 return;
6842 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
6843 && reg_mentioned_p (reg, PATTERN (i1)))
6844 return;
6847 if (cannot_omit_stores[REGNO (reg)])
6848 return;
6850 /* If this insn will store in the pseudo again,
6851 the previous store can be removed. */
6852 if (reload_out[j] == reload_in[j])
6853 delete_insn (output_reload_insn);
6855 /* See if the pseudo reg has been completely replaced
6856 with reload regs. If so, delete the store insn
6857 and forget we had a stack slot for the pseudo. */
6858 else if (reg_n_deaths[REGNO (reg)] == 1
6859 && reg_basic_block[REGNO (reg)] >= 0
6860 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
6862 rtx i2;
6864 /* We know that it was used only between here
6865 and the beginning of the current basic block.
6866 (We also know that the last use before INSN was
6867 the output reload we are thinking of deleting, but never mind that.)
6868 Search that range; see if any ref remains. */
6869 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
6871 rtx set = single_set (i2);
6873 /* Uses which just store in the pseudo don't count,
6874 since if they are the only uses, they are dead. */
6875 if (set != 0 && SET_DEST (set) == reg)
6876 continue;
6877 if (GET_CODE (i2) == CODE_LABEL
6878 || GET_CODE (i2) == JUMP_INSN)
6879 break;
6880 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
6881 && reg_mentioned_p (reg, PATTERN (i2)))
6882 /* Some other ref remains;
6883 we can't do anything. */
6884 return;
6887 /* Delete the now-dead stores into this pseudo. */
6888 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
6890 rtx set = single_set (i2);
6892 if (set != 0 && SET_DEST (set) == reg)
6893 delete_insn (i2);
6894 if (GET_CODE (i2) == CODE_LABEL
6895 || GET_CODE (i2) == JUMP_INSN)
6896 break;
6899 /* For the debugging info,
6900 say the pseudo lives in this reload reg. */
6901 reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
6902 alter_reg (REGNO (reg), -1);
6906 /* Output reload-insns to reload VALUE into RELOADREG.
6907 VALUE is an autoincrement or autodecrement RTX whose operand
6908 is a register or memory location;
6909 so reloading involves incrementing that location.
6911 INC_AMOUNT is the number to increment or decrement by (always positive).
6912 This cannot be deduced from VALUE. */
6914 static void
6915 inc_for_reload (reloadreg, value, inc_amount)
6916 rtx reloadreg;
6917 rtx value;
6918 int inc_amount;
6920 /* REG or MEM to be copied and incremented. */
6921 rtx incloc = XEXP (value, 0);
6922 /* Nonzero if increment after copying. */
6923 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
6924 rtx last;
6925 rtx inc;
6926 rtx add_insn;
6927 int code;
6929 /* No hard register is equivalent to this register after
6930 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
6931 we could inc/dec that register as well (maybe even using it for
6932 the source), but I'm not sure it's worth worrying about. */
6933 if (GET_CODE (incloc) == REG)
6934 reg_last_reload_reg[REGNO (incloc)] = 0;
6936 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
6937 inc_amount = - inc_amount;
6939 inc = GEN_INT (inc_amount);
6941 /* If this is post-increment, first copy the location to the reload reg. */
6942 if (post)
6943 emit_insn (gen_move_insn (reloadreg, incloc));
6945 /* See if we can directly increment INCLOC. Use a method similar to that
6946 in gen_reload. */
6948 last = get_last_insn ();
6949 add_insn = emit_insn (gen_rtx (SET, VOIDmode, incloc,
6950 gen_rtx (PLUS, GET_MODE (incloc),
6951 incloc, inc)));
6953 code = recog_memoized (add_insn);
6954 if (code >= 0)
6956 insn_extract (add_insn);
6957 if (constrain_operands (code, 1))
6959 /* If this is a pre-increment and we have incremented the value
6960 where it lives, copy the incremented value to RELOADREG to
6961 be used as an address. */
6963 if (! post)
6964 emit_insn (gen_move_insn (reloadreg, incloc));
6966 return;
6970 delete_insns_since (last);
6972 /* If couldn't do the increment directly, must increment in RELOADREG.
6973 The way we do this depends on whether this is pre- or post-increment.
6974 For pre-increment, copy INCLOC to the reload register, increment it
6975 there, then save back. */
6977 if (! post)
6979 emit_insn (gen_move_insn (reloadreg, incloc));
6980 emit_insn (gen_add2_insn (reloadreg, inc));
6981 emit_insn (gen_move_insn (incloc, reloadreg));
6983 else
6985 /* Postincrement.
6986 Because this might be a jump insn or a compare, and because RELOADREG
6987 may not be available after the insn in an input reload, we must do
6988 the incrementation before the insn being reloaded for.
6990 We have already copied INCLOC to RELOADREG. Increment the copy in
6991 RELOADREG, save that back, then decrement RELOADREG so it has
6992 the original value. */
6994 emit_insn (gen_add2_insn (reloadreg, inc));
6995 emit_insn (gen_move_insn (incloc, reloadreg));
6996 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
6999 return;
7002 /* Return 1 if we are certain that the constraint-string STRING allows
7003 the hard register REG. Return 0 if we can't be sure of this. */
7005 static int
7006 constraint_accepts_reg_p (string, reg)
7007 char *string;
7008 rtx reg;
7010 int value = 0;
7011 int regno = true_regnum (reg);
7012 int c;
7014 /* Initialize for first alternative. */
7015 value = 0;
7016 /* Check that each alternative contains `g' or `r'. */
7017 while (1)
7018 switch (c = *string++)
7020 case 0:
7021 /* If an alternative lacks `g' or `r', we lose. */
7022 return value;
7023 case ',':
7024 /* If an alternative lacks `g' or `r', we lose. */
7025 if (value == 0)
7026 return 0;
7027 /* Initialize for next alternative. */
7028 value = 0;
7029 break;
7030 case 'g':
7031 case 'r':
7032 /* Any general reg wins for this alternative. */
7033 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
7034 value = 1;
7035 break;
7036 default:
7037 /* Any reg in specified class wins for this alternative. */
7039 enum reg_class class = REG_CLASS_FROM_LETTER (c);
7041 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
7042 value = 1;
7047 /* Return the number of places FIND appears within X, but don't count
7048 an occurrence if some SET_DEST is FIND. */
7050 static int
7051 count_occurrences (x, find)
7052 register rtx x, find;
7054 register int i, j;
7055 register enum rtx_code code;
7056 register char *format_ptr;
7057 int count;
7059 if (x == find)
7060 return 1;
7061 if (x == 0)
7062 return 0;
7064 code = GET_CODE (x);
7066 switch (code)
7068 case REG:
7069 case QUEUED:
7070 case CONST_INT:
7071 case CONST_DOUBLE:
7072 case SYMBOL_REF:
7073 case CODE_LABEL:
7074 case PC:
7075 case CC0:
7076 return 0;
7078 case SET:
7079 if (SET_DEST (x) == find)
7080 return count_occurrences (SET_SRC (x), find);
7081 break;
7084 format_ptr = GET_RTX_FORMAT (code);
7085 count = 0;
7087 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7089 switch (*format_ptr++)
7091 case 'e':
7092 count += count_occurrences (XEXP (x, i), find);
7093 break;
7095 case 'E':
7096 if (XVEC (x, i) != NULL)
7098 for (j = 0; j < XVECLEN (x, i); j++)
7099 count += count_occurrences (XVECEXP (x, i, j), find);
7101 break;
7104 return count;