OSDN Git Service

(NEEDS_UNTYPED_CALL): Define.
[pf3gnuchains/gcc-fork.git] / gcc / reload1.c
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
19
20
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"
36
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.
43
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.
47
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.
51
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.
58
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.
62
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.  */
69 \f
70 /* During reload_as_needed, element N contains a REG rtx for the hard reg
71    into which pseudo reg N has been reloaded (perhaps for a previous insn). */
72 static rtx *reg_last_reload_reg;
73
74 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
75    for an output reload that stores into reg N.  */
76 static char *reg_has_output_reload;
77
78 /* Indicates which hard regs are reload-registers for an output reload
79    in the current insn.  */
80 static HARD_REG_SET reg_is_output_reload;
81
82 /* Element N is the constant value to which pseudo reg N is equivalent,
83    or zero if pseudo reg N is not equivalent to a constant.
84    find_reloads looks at this in order to replace pseudo reg N
85    with the constant it stands for.  */
86 rtx *reg_equiv_constant;
87
88 /* Element N is a memory location to which pseudo reg N is equivalent,
89    prior to any register elimination (such as frame pointer to stack
90    pointer).  Depending on whether or not it is a valid address, this value
91    is transferred to either reg_equiv_address or reg_equiv_mem.  */
92 rtx *reg_equiv_memory_loc;
93
94 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
95    This is used when the address is not valid as a memory address
96    (because its displacement is too big for the machine.)  */
97 rtx *reg_equiv_address;
98
99 /* Element N is the memory slot to which pseudo reg N is equivalent,
100    or zero if pseudo reg N is not equivalent to a memory slot.  */
101 rtx *reg_equiv_mem;
102
103 /* Widest width in which each pseudo reg is referred to (via subreg).  */
104 static int *reg_max_ref_width;
105
106 /* Element N is the insn that initialized reg N from its equivalent
107    constant or memory slot.  */
108 static rtx *reg_equiv_init;
109
110 /* During reload_as_needed, element N contains the last pseudo regno
111    reloaded into the Nth reload register.  This vector is in parallel
112    with spill_regs.  If that pseudo reg occupied more than one register,
113    reg_reloaded_contents points to that pseudo for each spill register in
114    use; all of these must remain set for an inheritance to occur.  */
115 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
116
117 /* During reload_as_needed, element N contains the insn for which
118    the Nth reload register was last used.  This vector is in parallel
119    with spill_regs, and its contents are significant only when
120    reg_reloaded_contents is significant.  */
121 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
122
123 /* Number of spill-regs so far; number of valid elements of spill_regs.  */
124 static int n_spills;
125
126 /* In parallel with spill_regs, contains REG rtx's for those regs.
127    Holds the last rtx used for any given reg, or 0 if it has never
128    been used for spilling yet.  This rtx is reused, provided it has
129    the proper mode.  */
130 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
131
132 /* In parallel with spill_regs, contains nonzero for a spill reg
133    that was stored after the last time it was used.
134    The precise value is the insn generated to do the store.  */
135 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
136
137 /* This table is the inverse mapping of spill_regs:
138    indexed by hard reg number,
139    it contains the position of that reg in spill_regs,
140    or -1 for something that is not in spill_regs.  */
141 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
142
143 /* This reg set indicates registers that may not be used for retrying global
144    allocation.  The registers that may not be used include all spill registers
145    and the frame pointer (if we are using one).  */
146 HARD_REG_SET forbidden_regs;
147
148 /* This reg set indicates registers that are not good for spill registers.
149    They will not be used to complete groups of spill registers.  This includes
150    all fixed registers, registers that may be eliminated, and registers
151    explicitly used in the rtl.
152
153    (spill_reg_order prevents these registers from being used to start a
154    group.)  */
155 static HARD_REG_SET bad_spill_regs;
156
157 /* Describes order of use of registers for reloading
158    of spilled pseudo-registers.  `spills' is the number of
159    elements that are actually valid; new ones are added at the end.  */
160 static short spill_regs[FIRST_PSEUDO_REGISTER];
161
162 /* Describes order of preference for putting regs into spill_regs.
163    Contains the numbers of all the hard regs, in order most preferred first.
164    This order is different for each function.
165    It is set up by order_regs_for_reload.
166    Empty elements at the end contain -1.  */
167 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
168
169 /* 1 for a hard register that appears explicitly in the rtl
170    (for example, function value registers, special registers
171    used by insns, structure value pointer registers).  */
172 static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
173
174 /* Indicates if a register was counted against the need for
175    groups.  0 means it can count against max_nongroup instead.  */
176 static HARD_REG_SET counted_for_groups;
177
178 /* Indicates if a register was counted against the need for
179    non-groups.  0 means it can become part of a new group.
180    During choose_reload_regs, 1 here means don't use this reg
181    as part of a group, even if it seems to be otherwise ok.  */
182 static HARD_REG_SET counted_for_nongroups;
183
184 /* Nonzero if indirect addressing is supported on the machine; this means
185    that spilling (REG n) does not require reloading it into a register in
186    order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))).  The
187    value indicates the level of indirect addressing supported, e.g., two
188    means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
189    a hard register.  */
190
191 static char spill_indirect_levels;
192
193 /* Nonzero if indirect addressing is supported when the innermost MEM is
194    of the form (MEM (SYMBOL_REF sym)).  It is assumed that the level to
195    which these are valid is the same as spill_indirect_levels, above.   */
196
197 char indirect_symref_ok;
198
199 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid.  */
200
201 char double_reg_address_ok;
202
203 /* Record the stack slot for each spilled hard register.  */
204
205 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
206
207 /* Width allocated so far for that stack slot.  */
208
209 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
210
211 /* Indexed by register class and basic block number, nonzero if there is
212    any need for a spill register of that class in that basic block.
213    The pointer is 0 if we did stupid allocation and don't know
214    the structure of basic blocks.  */
215
216 char *basic_block_needs[N_REG_CLASSES];
217
218 /* First uid used by insns created by reload in this function.
219    Used in find_equiv_reg.  */
220 int reload_first_uid;
221
222 /* Flag set by local-alloc or global-alloc if anything is live in
223    a call-clobbered reg across calls.  */
224
225 int caller_save_needed;
226
227 /* Set to 1 while reload_as_needed is operating.
228    Required by some machines to handle any generated moves differently.  */
229
230 int reload_in_progress = 0;
231
232 /* These arrays record the insn_code of insns that may be needed to
233    perform input and output reloads of special objects.  They provide a
234    place to pass a scratch register.  */
235
236 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
237 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
238
239 /* This obstack is used for allocation of rtl during register elimination.
240    The allocated storage can be freed once find_reloads has processed the
241    insn.  */
242
243 struct obstack reload_obstack;
244 char *reload_firstobj;
245
246 #define obstack_chunk_alloc xmalloc
247 #define obstack_chunk_free free
248
249 /* List of labels that must never be deleted.  */
250 extern rtx forced_labels;
251 \f
252 /* This structure is used to record information about register eliminations.
253    Each array entry describes one possible way of eliminating a register
254    in favor of another.   If there is more than one way of eliminating a
255    particular register, the most preferred should be specified first.  */
256
257 static struct elim_table
258 {
259   int from;                     /* Register number to be eliminated. */
260   int to;                       /* Register number used as replacement. */
261   int initial_offset;           /* Initial difference between values. */
262   int can_eliminate;            /* Non-zero if this elimination can be done. */
263   int can_eliminate_previous;   /* Value of CAN_ELIMINATE in previous scan over
264                                    insns made by reload. */
265   int offset;                   /* Current offset between the two regs. */
266   int max_offset;               /* Maximum offset between the two regs. */
267   int previous_offset;          /* Offset at end of previous insn. */
268   int ref_outside_mem;          /* "to" has been referenced outside a MEM. */
269   rtx from_rtx;                 /* REG rtx for the register to be eliminated.
270                                    We cannot simply compare the number since
271                                    we might then spuriously replace a hard
272                                    register corresponding to a pseudo
273                                    assigned to the reg to be eliminated. */
274   rtx to_rtx;                   /* REG rtx for the replacement. */
275 } reg_eliminate[] =
276
277 /* If a set of eliminable registers was specified, define the table from it.
278    Otherwise, default to the normal case of the frame pointer being
279    replaced by the stack pointer.  */
280
281 #ifdef ELIMINABLE_REGS
282   ELIMINABLE_REGS;
283 #else
284   {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
285 #endif
286
287 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
288
289 /* Record the number of pending eliminations that have an offset not equal
290    to their initial offset.  If non-zero, we use a new copy of each
291    replacement result in any insns encountered.  */
292 static int num_not_at_initial_offset;
293
294 /* Count the number of registers that we may be able to eliminate.  */
295 static int num_eliminable;
296
297 /* For each label, we record the offset of each elimination.  If we reach
298    a label by more than one path and an offset differs, we cannot do the
299    elimination.  This information is indexed by the number of the label.
300    The first table is an array of flags that records whether we have yet
301    encountered a label and the second table is an array of arrays, one
302    entry in the latter array for each elimination.  */
303
304 static char *offsets_known_at;
305 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
306
307 /* Number of labels in the current function.  */
308
309 static int num_labels;
310 \f
311 void mark_home_live ();
312 static void count_possible_groups ();
313 static int possible_group_p ();
314 static void scan_paradoxical_subregs ();
315 static void reload_as_needed ();
316 static int modes_equiv_for_class_p ();
317 static void alter_reg ();
318 static void delete_dead_insn ();
319 static void spill_failure ();
320 static int new_spill_reg();
321 static void set_label_offsets ();
322 static int eliminate_regs_in_insn ();
323 static void mark_not_eliminable ();
324 static int spill_hard_reg ();
325 static void choose_reload_regs ();
326 static void emit_reload_insns ();
327 static void delete_output_reload ();
328 static void forget_old_reloads_1 ();
329 static void order_regs_for_reload ();
330 static rtx inc_for_reload ();
331 static int constraint_accepts_reg_p ();
332 static int count_occurrences ();
333
334 extern void remove_death ();
335 extern rtx adj_offsettable_operand ();
336 extern rtx form_sum ();
337 \f
338 void
339 init_reload ()
340 {
341   register int i;
342
343   /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
344      Set spill_indirect_levels to the number of levels such addressing is
345      permitted, zero if it is not permitted at all.  */
346
347   register rtx tem
348     = gen_rtx (MEM, Pmode,
349                gen_rtx (PLUS, Pmode,
350                         gen_rtx (REG, Pmode, LAST_VIRTUAL_REGISTER + 1),
351                         GEN_INT (4)));
352   spill_indirect_levels = 0;
353
354   while (memory_address_p (QImode, tem))
355     {
356       spill_indirect_levels++;
357       tem = gen_rtx (MEM, Pmode, tem);
358     }
359
360   /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)).  */
361
362   tem = gen_rtx (MEM, Pmode, gen_rtx (SYMBOL_REF, Pmode, "foo"));
363   indirect_symref_ok = memory_address_p (QImode, tem);
364
365   /* See if reg+reg is a valid (and offsettable) address.  */
366
367   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
368     {
369       tem = gen_rtx (PLUS, Pmode,
370                      gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
371                      gen_rtx (REG, Pmode, i));
372       /* This way, we make sure that reg+reg is an offsettable address.  */
373       tem = plus_constant (tem, 4);
374
375       if (memory_address_p (QImode, tem))
376         {
377           double_reg_address_ok = 1;
378           break;
379         }
380     }
381
382   /* Initialize obstack for our rtl allocation. */
383   gcc_obstack_init (&reload_obstack);
384   reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
385
386 #ifdef HAVE_SECONDARY_RELOADS
387
388   /* Initialize the optabs for doing special input and output reloads.  */
389
390   for (i = 0; i < NUM_MACHINE_MODES; i++)
391     reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
392
393 #ifdef HAVE_reload_inqi
394   if (HAVE_reload_inqi)
395     reload_in_optab[(int) QImode] = CODE_FOR_reload_inqi;
396 #endif
397 #ifdef HAVE_reload_inhi
398   if (HAVE_reload_inhi)
399     reload_in_optab[(int) HImode] = CODE_FOR_reload_inhi;
400 #endif
401 #ifdef HAVE_reload_insi
402   if (HAVE_reload_insi)
403     reload_in_optab[(int) SImode] = CODE_FOR_reload_insi;
404 #endif
405 #ifdef HAVE_reload_indi
406   if (HAVE_reload_indi)
407     reload_in_optab[(int) DImode] = CODE_FOR_reload_indi;
408 #endif
409 #ifdef HAVE_reload_inti
410   if (HAVE_reload_inti)
411     reload_in_optab[(int) TImode] = CODE_FOR_reload_inti;
412 #endif
413 #ifdef HAVE_reload_inqf
414   if (HAVE_reload_inqf)
415     reload_in_optab[(int) QFmode] = CODE_FOR_reload_inqf;
416 #endif
417 #ifdef HAVE_reload_inhf
418   if (HAVE_reload_inhf)
419     reload_in_optab[(int) HFmode] = CODE_FOR_reload_inhf;
420 #endif
421 #ifdef HAVE_reload_insf
422   if (HAVE_reload_insf)
423     reload_in_optab[(int) SFmode] = CODE_FOR_reload_insf;
424 #endif
425 #ifdef HAVE_reload_indf
426   if (HAVE_reload_indf)
427     reload_in_optab[(int) DFmode] = CODE_FOR_reload_indf;
428 #endif
429 #ifdef HAVE_reload_inxf
430   if (HAVE_reload_inxf)
431     reload_in_optab[(int) XFmode] = CODE_FOR_reload_inxf;
432 #endif
433 #ifdef HAVE_reload_intf
434   if (HAVE_reload_intf)
435     reload_in_optab[(int) TFmode] = CODE_FOR_reload_intf;
436 #endif
437
438 #ifdef HAVE_reload_outqi
439   if (HAVE_reload_outqi)
440     reload_out_optab[(int) QImode] = CODE_FOR_reload_outqi;
441 #endif
442 #ifdef HAVE_reload_outhi
443   if (HAVE_reload_outhi)
444     reload_out_optab[(int) HImode] = CODE_FOR_reload_outhi;
445 #endif
446 #ifdef HAVE_reload_outsi
447   if (HAVE_reload_outsi)
448     reload_out_optab[(int) SImode] = CODE_FOR_reload_outsi;
449 #endif
450 #ifdef HAVE_reload_outdi
451   if (HAVE_reload_outdi)
452     reload_out_optab[(int) DImode] = CODE_FOR_reload_outdi;
453 #endif
454 #ifdef HAVE_reload_outti
455   if (HAVE_reload_outti)
456     reload_out_optab[(int) TImode] = CODE_FOR_reload_outti;
457 #endif
458 #ifdef HAVE_reload_outqf
459   if (HAVE_reload_outqf)
460     reload_out_optab[(int) QFmode] = CODE_FOR_reload_outqf;
461 #endif
462 #ifdef HAVE_reload_outhf
463   if (HAVE_reload_outhf)
464     reload_out_optab[(int) HFmode] = CODE_FOR_reload_outhf;
465 #endif
466 #ifdef HAVE_reload_outsf
467   if (HAVE_reload_outsf)
468     reload_out_optab[(int) SFmode] = CODE_FOR_reload_outsf;
469 #endif
470 #ifdef HAVE_reload_outdf
471   if (HAVE_reload_outdf)
472     reload_out_optab[(int) DFmode] = CODE_FOR_reload_outdf;
473 #endif
474 #ifdef HAVE_reload_outxf
475   if (HAVE_reload_outxf)
476     reload_out_optab[(int) XFmode] = CODE_FOR_reload_outxf;
477 #endif
478 #ifdef HAVE_reload_outtf
479   if (HAVE_reload_outtf)
480     reload_out_optab[(int) TFmode] = CODE_FOR_reload_outtf;
481 #endif
482
483 #endif /* HAVE_SECONDARY_RELOADS */
484
485 }
486
487 /* Main entry point for the reload pass, and only entry point
488    in this file.
489
490    FIRST is the first insn of the function being compiled.
491
492    GLOBAL nonzero means we were called from global_alloc
493    and should attempt to reallocate any pseudoregs that we
494    displace from hard regs we will use for reloads.
495    If GLOBAL is zero, we do not have enough information to do that,
496    so any pseudo reg that is spilled must go to the stack.
497
498    DUMPFILE is the global-reg debugging dump file stream, or 0.
499    If it is nonzero, messages are written to it to describe
500    which registers are seized as reload regs, which pseudo regs
501    are spilled from them, and where the pseudo regs are reallocated to.
502
503    Return value is nonzero if reload failed
504    and we must not do any more for this function.  */
505
506 int
507 reload (first, global, dumpfile)
508      rtx first;
509      int global;
510      FILE *dumpfile;
511 {
512   register int class;
513   register int i;
514   register rtx insn;
515   register struct elim_table *ep;
516
517   int something_changed;
518   int something_needs_reloads;
519   int something_needs_elimination;
520   int new_basic_block_needs;
521   enum reg_class caller_save_spill_class = NO_REGS;
522   int caller_save_group_size = 1;
523
524   /* Nonzero means we couldn't get enough spill regs.  */
525   int failure = 0;
526
527   /* The basic block number currently being processed for INSN.  */
528   int this_block;
529
530   /* Make sure even insns with volatile mem refs are recognizable.  */
531   init_recog ();
532
533   /* Enable find_equiv_reg to distinguish insns made by reload.  */
534   reload_first_uid = get_max_uid ();
535
536   for (i = 0; i < N_REG_CLASSES; i++)
537     basic_block_needs[i] = 0;
538
539 #ifdef SECONDARY_MEMORY_NEEDED
540   /* Initialize the secondary memory table.  */
541   clear_secondary_mem ();
542 #endif
543
544   /* Remember which hard regs appear explicitly
545      before we merge into `regs_ever_live' the ones in which
546      pseudo regs have been allocated.  */
547   bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
548
549   /* We don't have a stack slot for any spill reg yet.  */
550   bzero (spill_stack_slot, sizeof spill_stack_slot);
551   bzero (spill_stack_slot_width, sizeof spill_stack_slot_width);
552
553   /* Initialize the save area information for caller-save, in case some
554      are needed.  */
555   init_save_areas ();
556
557   /* Compute which hard registers are now in use
558      as homes for pseudo registers.
559      This is done here rather than (eg) in global_alloc
560      because this point is reached even if not optimizing.  */
561
562   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
563     mark_home_live (i);
564
565   /* Make sure that the last insn in the chain
566      is not something that needs reloading.  */
567   emit_note (NULL_PTR, NOTE_INSN_DELETED);
568
569   /* Find all the pseudo registers that didn't get hard regs
570      but do have known equivalent constants or memory slots.
571      These include parameters (known equivalent to parameter slots)
572      and cse'd or loop-moved constant memory addresses.
573
574      Record constant equivalents in reg_equiv_constant
575      so they will be substituted by find_reloads.
576      Record memory equivalents in reg_mem_equiv so they can
577      be substituted eventually by altering the REG-rtx's.  */
578
579   reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
580   bzero (reg_equiv_constant, max_regno * sizeof (rtx));
581   reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
582   bzero (reg_equiv_memory_loc, max_regno * sizeof (rtx));
583   reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
584   bzero (reg_equiv_mem, max_regno * sizeof (rtx));
585   reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
586   bzero (reg_equiv_init, max_regno * sizeof (rtx));
587   reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
588   bzero (reg_equiv_address, max_regno * sizeof (rtx));
589   reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
590   bzero (reg_max_ref_width, max_regno * sizeof (int));
591
592   /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
593      Also find all paradoxical subregs
594      and find largest such for each pseudo.  */
595
596   for (insn = first; insn; insn = NEXT_INSN (insn))
597     {
598       rtx set = single_set (insn);
599
600       if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
601         {
602           rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
603           if (note
604 #ifdef LEGITIMATE_PIC_OPERAND_P
605               && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
606                   || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
607 #endif
608               )
609             {
610               rtx x = XEXP (note, 0);
611               i = REGNO (SET_DEST (set));
612               if (i > LAST_VIRTUAL_REGISTER)
613                 {
614                   if (GET_CODE (x) == MEM)
615                     reg_equiv_memory_loc[i] = x;
616                   else if (CONSTANT_P (x))
617                     {
618                       if (LEGITIMATE_CONSTANT_P (x))
619                         reg_equiv_constant[i] = x;
620                       else
621                         reg_equiv_memory_loc[i]
622                           = force_const_mem (GET_MODE (SET_DEST (set)), x);
623                     }
624                   else
625                     continue;
626
627                   /* If this register is being made equivalent to a MEM
628                      and the MEM is not SET_SRC, the equivalencing insn
629                      is one with the MEM as a SET_DEST and it occurs later.
630                      So don't mark this insn now.  */
631                   if (GET_CODE (x) != MEM
632                       || rtx_equal_p (SET_SRC (set), x))
633                     reg_equiv_init[i] = insn;
634                 }
635             }
636         }
637
638       /* If this insn is setting a MEM from a register equivalent to it,
639          this is the equivalencing insn.  */
640       else if (set && GET_CODE (SET_DEST (set)) == MEM
641                && GET_CODE (SET_SRC (set)) == REG
642                && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
643                && rtx_equal_p (SET_DEST (set),
644                                reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
645         reg_equiv_init[REGNO (SET_SRC (set))] = insn;
646
647       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
648         scan_paradoxical_subregs (PATTERN (insn));
649     }
650
651   /* Does this function require a frame pointer?  */
652
653   frame_pointer_needed = (! flag_omit_frame_pointer
654 #ifdef EXIT_IGNORE_STACK
655                           /* ?? If EXIT_IGNORE_STACK is set, we will not save
656                              and restore sp for alloca.  So we can't eliminate
657                              the frame pointer in that case.  At some point,
658                              we should improve this by emitting the
659                              sp-adjusting insns for this case.  */
660                           || (current_function_calls_alloca
661                               && EXIT_IGNORE_STACK)
662 #endif
663                           || FRAME_POINTER_REQUIRED);
664
665   num_eliminable = 0;
666
667   /* Initialize the table of registers to eliminate.  The way we do this
668      depends on how the eliminable registers were defined.  */
669 #ifdef ELIMINABLE_REGS
670   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
671     {
672       ep->can_eliminate = ep->can_eliminate_previous
673         = (CAN_ELIMINATE (ep->from, ep->to)
674            && (ep->from != FRAME_POINTER_REGNUM || ! frame_pointer_needed));
675     }
676 #else
677   reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
678     = ! frame_pointer_needed;
679 #endif
680
681   /* Count the number of eliminable registers and build the FROM and TO
682      REG rtx's.  Note that code in gen_rtx will cause, e.g.,
683      gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
684      We depend on this.  */
685   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
686     {
687       num_eliminable += ep->can_eliminate;
688       ep->from_rtx = gen_rtx (REG, Pmode, ep->from);
689       ep->to_rtx = gen_rtx (REG, Pmode, ep->to);
690     }
691
692   num_labels = max_label_num () - get_first_label_num ();
693
694   /* Allocate the tables used to store offset information at labels.  */
695   offsets_known_at = (char *) alloca (num_labels);
696   offsets_at
697     = (int (*)[NUM_ELIMINABLE_REGS])
698       alloca (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
699
700   offsets_known_at -= get_first_label_num ();
701   offsets_at -= get_first_label_num ();
702
703   /* Alter each pseudo-reg rtx to contain its hard reg number.
704      Assign stack slots to the pseudos that lack hard regs or equivalents.
705      Do not touch virtual registers.  */
706
707   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
708     alter_reg (i, -1);
709
710   /* Round size of stack frame to BIGGEST_ALIGNMENT.  This must be done here
711      because the stack size may be a part of the offset computation for
712      register elimination.   */
713   assign_stack_local (BLKmode, 0, 0);
714
715   /* If we have some registers we think can be eliminated, scan all insns to
716      see if there is an insn that sets one of these registers to something
717      other than itself plus a constant.  If so, the register cannot be
718      eliminated.  Doing this scan here eliminates an extra pass through the
719      main reload loop in the most common case where register elimination
720      cannot be done.  */
721   for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
722     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
723         || GET_CODE (insn) == CALL_INSN)
724       note_stores (PATTERN (insn), mark_not_eliminable);
725
726 #ifndef REGISTER_CONSTRAINTS
727   /* If all the pseudo regs have hard regs,
728      except for those that are never referenced,
729      we know that no reloads are needed.  */
730   /* But that is not true if there are register constraints, since
731      in that case some pseudos might be in the wrong kind of hard reg.  */
732
733   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
734     if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
735       break;
736
737   if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
738     return;
739 #endif
740
741   /* Compute the order of preference for hard registers to spill.
742      Store them by decreasing preference in potential_reload_regs.  */
743
744   order_regs_for_reload ();
745
746   /* So far, no hard regs have been spilled.  */
747   n_spills = 0;
748   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
749     spill_reg_order[i] = -1;
750
751   /* On most machines, we can't use any register explicitly used in the
752      rtl as a spill register.  But on some, we have to.  Those will have
753      taken care to keep the life of hard regs as short as possible.  */
754
755 #ifdef SMALL_REGISTER_CLASSES
756   CLEAR_HARD_REG_SET (forbidden_regs);
757 #else
758   COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
759 #endif
760
761   /* Spill any hard regs that we know we can't eliminate.  */
762   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
763     if (! ep->can_eliminate)
764       {
765         spill_hard_reg (ep->from, global, dumpfile, 1);
766         regs_ever_live[ep->from] = 1;
767       }
768
769   if (global)
770     for (i = 0; i < N_REG_CLASSES; i++)
771       {
772         basic_block_needs[i] = (char *)alloca (n_basic_blocks);
773         bzero (basic_block_needs[i], n_basic_blocks);
774       }
775
776   /* From now on, we need to emit any moves without making new pseudos.  */
777   reload_in_progress = 1;
778
779   /* This loop scans the entire function each go-round
780      and repeats until one repetition spills no additional hard regs.  */
781
782   /* This flag is set when a pseudo reg is spilled,
783      to require another pass.  Note that getting an additional reload
784      reg does not necessarily imply any pseudo reg was spilled;
785      sometimes we find a reload reg that no pseudo reg was allocated in.  */
786   something_changed = 1;
787   /* This flag is set if there are any insns that require reloading.  */
788   something_needs_reloads = 0;
789   /* This flag is set if there are any insns that require register
790      eliminations.  */
791   something_needs_elimination = 0;
792   while (something_changed)
793     {
794       rtx after_call = 0;
795
796       /* For each class, number of reload regs needed in that class.
797          This is the maximum over all insns of the needs in that class
798          of the individual insn.  */
799       int max_needs[N_REG_CLASSES];
800       /* For each class, size of group of consecutive regs
801          that is needed for the reloads of this class.  */
802       int group_size[N_REG_CLASSES];
803       /* For each class, max number of consecutive groups needed.
804          (Each group contains group_size[CLASS] consecutive registers.)  */
805       int max_groups[N_REG_CLASSES];
806       /* For each class, max number needed of regs that don't belong
807          to any of the groups.  */
808       int max_nongroups[N_REG_CLASSES];
809       /* For each class, the machine mode which requires consecutive
810          groups of regs of that class.
811          If two different modes ever require groups of one class,
812          they must be the same size and equally restrictive for that class,
813          otherwise we can't handle the complexity.  */
814       enum machine_mode group_mode[N_REG_CLASSES];
815       /* Record the insn where each maximum need is first found.  */
816       rtx max_needs_insn[N_REG_CLASSES];
817       rtx max_groups_insn[N_REG_CLASSES];
818       rtx max_nongroups_insn[N_REG_CLASSES];
819       rtx x;
820       int starting_frame_size = get_frame_size ();
821       static char *reg_class_names[] = REG_CLASS_NAMES;
822
823       something_changed = 0;
824       bzero (max_needs, sizeof max_needs);
825       bzero (max_groups, sizeof max_groups);
826       bzero (max_nongroups, sizeof max_nongroups);
827       bzero (max_needs_insn, sizeof max_needs_insn);
828       bzero (max_groups_insn, sizeof max_groups_insn);
829       bzero (max_nongroups_insn, sizeof max_nongroups_insn);
830       bzero (group_size, sizeof group_size);
831       for (i = 0; i < N_REG_CLASSES; i++)
832         group_mode[i] = VOIDmode;
833
834       /* Keep track of which basic blocks are needing the reloads.  */
835       this_block = 0;
836
837       /* Remember whether any element of basic_block_needs
838          changes from 0 to 1 in this pass.  */
839       new_basic_block_needs = 0;
840
841       /* Reset all offsets on eliminable registers to their initial values.  */
842 #ifdef ELIMINABLE_REGS
843       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
844         {
845           INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
846           ep->previous_offset = ep->offset
847             = ep->max_offset = ep->initial_offset;
848         }
849 #else
850 #ifdef INITIAL_FRAME_POINTER_OFFSET
851       INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
852 #else
853       if (!FRAME_POINTER_REQUIRED)
854         abort ();
855       reg_eliminate[0].initial_offset = 0;
856 #endif
857       reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
858         = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
859 #endif
860
861       num_not_at_initial_offset = 0;
862
863       bzero (&offsets_known_at[get_first_label_num ()], num_labels);
864
865       /* Set a known offset for each forced label to be at the initial offset
866          of each elimination.  We do this because we assume that all
867          computed jumps occur from a location where each elimination is
868          at its initial offset.  */
869
870       for (x = forced_labels; x; x = XEXP (x, 1))
871         if (XEXP (x, 0))
872           set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
873
874       /* For each pseudo register that has an equivalent location defined,
875          try to eliminate any eliminable registers (such as the frame pointer)
876          assuming initial offsets for the replacement register, which
877          is the normal case.
878
879          If the resulting location is directly addressable, substitute
880          the MEM we just got directly for the old REG.
881
882          If it is not addressable but is a constant or the sum of a hard reg
883          and constant, it is probably not addressable because the constant is
884          out of range, in that case record the address; we will generate
885          hairy code to compute the address in a register each time it is
886          needed.
887
888          If the location is not addressable, but does not have one of the
889          above forms, assign a stack slot.  We have to do this to avoid the
890          potential of producing lots of reloads if, e.g., a location involves
891          a pseudo that didn't get a hard register and has an equivalent memory
892          location that also involves a pseudo that didn't get a hard register.
893
894          Perhaps at some point we will improve reload_when_needed handling
895          so this problem goes away.  But that's very hairy.  */
896
897       for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
898         if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
899           {
900             rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
901
902             if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
903                                          XEXP (x, 0)))
904               reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
905             else if (CONSTANT_P (XEXP (x, 0))
906                      || (GET_CODE (XEXP (x, 0)) == PLUS
907                          && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
908                          && (REGNO (XEXP (XEXP (x, 0), 0))
909                              < FIRST_PSEUDO_REGISTER)
910                          && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
911               reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
912             else
913               {
914                 /* Make a new stack slot.  Then indicate that something
915                    changed so we go back and recompute offsets for
916                    eliminable registers because the allocation of memory
917                    below might change some offset.  reg_equiv_{mem,address}
918                    will be set up for this pseudo on the next pass around
919                    the loop.  */
920                 reg_equiv_memory_loc[i] = 0;
921                 reg_equiv_init[i] = 0;
922                 alter_reg (i, -1);
923                 something_changed = 1;
924               }
925           }
926
927       /* If we allocated another pseudo to the stack, redo elimination
928          bookkeeping.  */
929       if (something_changed)
930         continue;
931
932       /* If caller-saves needs a group, initialize the group to include
933          the size and mode required for caller-saves.  */
934
935       if (caller_save_group_size > 1)
936         {
937           group_mode[(int) caller_save_spill_class] = Pmode;
938           group_size[(int) caller_save_spill_class] = caller_save_group_size;
939         }
940
941       /* Compute the most additional registers needed by any instruction.
942          Collect information separately for each class of regs.  */
943
944       for (insn = first; insn; insn = NEXT_INSN (insn))
945         {
946           if (global && this_block + 1 < n_basic_blocks
947               && insn == basic_block_head[this_block+1])
948             ++this_block;
949
950           /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
951              might include REG_LABEL), we need to see what effects this
952              has on the known offsets at labels.  */
953
954           if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
955               || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
956                   && REG_NOTES (insn) != 0))
957             set_label_offsets (insn, insn, 0);
958
959           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
960             {
961               /* Nonzero means don't use a reload reg that overlaps
962                  the place where a function value can be returned.  */
963               rtx avoid_return_reg = 0;
964
965               rtx old_body = PATTERN (insn);
966               int old_code = INSN_CODE (insn);
967               rtx old_notes = REG_NOTES (insn);
968               int did_elimination = 0;
969
970               /* Initially, count RELOAD_OTHER reloads.
971                  Later, merge in the other kinds.  */
972               int insn_needs[N_REG_CLASSES];
973               int insn_groups[N_REG_CLASSES];
974               int insn_total_groups = 0;
975
976               /* Count RELOAD_FOR_INPUT_RELOAD_ADDRESS reloads.  */
977               int insn_needs_for_inputs[N_REG_CLASSES];
978               int insn_groups_for_inputs[N_REG_CLASSES];
979               int insn_total_groups_for_inputs = 0;
980
981               /* Count RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reloads.  */
982               int insn_needs_for_outputs[N_REG_CLASSES];
983               int insn_groups_for_outputs[N_REG_CLASSES];
984               int insn_total_groups_for_outputs = 0;
985
986               /* Count RELOAD_FOR_OPERAND_ADDRESS reloads.  */
987               int insn_needs_for_operands[N_REG_CLASSES];
988               int insn_groups_for_operands[N_REG_CLASSES];
989               int insn_total_groups_for_operands = 0;
990
991 #if 0  /* This wouldn't work nowadays, since optimize_bit_field
992           looks for non-strict memory addresses.  */
993               /* Optimization: a bit-field instruction whose field
994                  happens to be a byte or halfword in memory
995                  can be changed to a move instruction.  */
996
997               if (GET_CODE (PATTERN (insn)) == SET)
998                 {
999                   rtx dest = SET_DEST (PATTERN (insn));
1000                   rtx src = SET_SRC (PATTERN (insn));
1001
1002                   if (GET_CODE (dest) == ZERO_EXTRACT
1003                       || GET_CODE (dest) == SIGN_EXTRACT)
1004                     optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
1005                   if (GET_CODE (src) == ZERO_EXTRACT
1006                       || GET_CODE (src) == SIGN_EXTRACT)
1007                     optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
1008                 }
1009 #endif
1010
1011               /* If needed, eliminate any eliminable registers.  */
1012               if (num_eliminable)
1013                 did_elimination = eliminate_regs_in_insn (insn, 0);
1014
1015 #ifdef SMALL_REGISTER_CLASSES
1016               /* Set avoid_return_reg if this is an insn
1017                  that might use the value of a function call.  */
1018               if (GET_CODE (insn) == CALL_INSN)
1019                 {
1020                   if (GET_CODE (PATTERN (insn)) == SET)
1021                     after_call = SET_DEST (PATTERN (insn));
1022                   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1023                            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1024                     after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1025                   else
1026                     after_call = 0;
1027                 }
1028               else if (after_call != 0
1029                        && !(GET_CODE (PATTERN (insn)) == SET
1030                             && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
1031                 {
1032                   if (reg_mentioned_p (after_call, PATTERN (insn)))
1033                     avoid_return_reg = after_call;
1034                   after_call = 0;
1035                 }
1036 #endif /* SMALL_REGISTER_CLASSES */
1037
1038               /* Analyze the instruction.  */
1039               find_reloads (insn, 0, spill_indirect_levels, global,
1040                             spill_reg_order);
1041
1042               /* Remember for later shortcuts which insns had any reloads or
1043                  register eliminations.
1044
1045                  One might think that it would be worthwhile to mark insns
1046                  that need register replacements but not reloads, but this is
1047                  not safe because find_reloads may do some manipulation of
1048                  the insn (such as swapping commutative operands), which would
1049                  be lost when we restore the old pattern after register
1050                  replacement.  So the actions of find_reloads must be redone in
1051                  subsequent passes or in reload_as_needed.
1052
1053                  However, it is safe to mark insns that need reloads
1054                  but not register replacement.  */
1055
1056               PUT_MODE (insn, (did_elimination ? QImode
1057                                : n_reloads ? HImode
1058                                : VOIDmode));
1059
1060               /* Discard any register replacements done.  */
1061               if (did_elimination)
1062                 {
1063                   obstack_free (&reload_obstack, reload_firstobj);
1064                   PATTERN (insn) = old_body;
1065                   INSN_CODE (insn) = old_code;
1066                   REG_NOTES (insn) = old_notes;
1067                   something_needs_elimination = 1;
1068                 }
1069
1070               /* If this insn has no reloads, we need not do anything except
1071                  in the case of a CALL_INSN when we have caller-saves and
1072                  caller-save needs reloads.  */
1073
1074               if (n_reloads == 0
1075                   && ! (GET_CODE (insn) == CALL_INSN
1076                         && caller_save_spill_class != NO_REGS))
1077                 continue;
1078
1079               something_needs_reloads = 1;
1080
1081               for (i = 0; i < N_REG_CLASSES; i++)
1082                 {
1083                   insn_needs[i] = 0, insn_groups[i] = 0;
1084                   insn_needs_for_inputs[i] = 0, insn_groups_for_inputs[i] = 0;
1085                   insn_needs_for_outputs[i] = 0, insn_groups_for_outputs[i] = 0;
1086                   insn_needs_for_operands[i] = 0, insn_groups_for_operands[i] = 0;
1087                 }
1088
1089               /* Count each reload once in every class
1090                  containing the reload's own class.  */
1091
1092               for (i = 0; i < n_reloads; i++)
1093                 {
1094                   register enum reg_class *p;
1095                   enum reg_class class = reload_reg_class[i];
1096                   int size;
1097                   enum machine_mode mode;
1098                   int *this_groups;
1099                   int *this_needs;
1100                   int *this_total_groups;
1101
1102                   /* Don't count the dummy reloads, for which one of the
1103                      regs mentioned in the insn can be used for reloading.
1104                      Don't count optional reloads.
1105                      Don't count reloads that got combined with others.  */
1106                   if (reload_reg_rtx[i] != 0
1107                       || reload_optional[i] != 0
1108                       || (reload_out[i] == 0 && reload_in[i] == 0
1109                           && ! reload_secondary_p[i]))
1110                     continue;
1111
1112                   /* Show that a reload register of this class is needed
1113                      in this basic block.  We do not use insn_needs and
1114                      insn_groups because they are overly conservative for
1115                      this purpose.  */
1116                   if (global && ! basic_block_needs[(int) class][this_block])
1117                     {
1118                       basic_block_needs[(int) class][this_block] = 1;
1119                       new_basic_block_needs = 1;
1120                     }
1121
1122                   /* Decide which time-of-use to count this reload for.  */
1123                   switch (reload_when_needed[i])
1124                     {
1125                     case RELOAD_OTHER:
1126                     case RELOAD_FOR_OUTPUT:
1127                     case RELOAD_FOR_INPUT:
1128                       this_needs = insn_needs;
1129                       this_groups = insn_groups;
1130                       this_total_groups = &insn_total_groups;
1131                       break;
1132
1133                     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
1134                       this_needs = insn_needs_for_inputs;
1135                       this_groups = insn_groups_for_inputs;
1136                       this_total_groups = &insn_total_groups_for_inputs;
1137                       break;
1138
1139                     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
1140                       this_needs = insn_needs_for_outputs;
1141                       this_groups = insn_groups_for_outputs;
1142                       this_total_groups = &insn_total_groups_for_outputs;
1143                       break;
1144
1145                     case RELOAD_FOR_OPERAND_ADDRESS:
1146                       this_needs = insn_needs_for_operands;
1147                       this_groups = insn_groups_for_operands;
1148                       this_total_groups = &insn_total_groups_for_operands;
1149                       break;
1150                     }
1151
1152                   mode = reload_inmode[i];
1153                   if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1154                     mode = reload_outmode[i];
1155                   size = CLASS_MAX_NREGS (class, mode);
1156                   if (size > 1)
1157                     {
1158                       enum machine_mode other_mode, allocate_mode;
1159
1160                       /* Count number of groups needed separately from
1161                          number of individual regs needed.  */
1162                       this_groups[(int) class]++;
1163                       p = reg_class_superclasses[(int) class];
1164                       while (*p != LIM_REG_CLASSES)
1165                         this_groups[(int) *p++]++;
1166                       (*this_total_groups)++;
1167
1168                       /* Record size and mode of a group of this class.  */
1169                       /* If more than one size group is needed,
1170                          make all groups the largest needed size.  */
1171                       if (group_size[(int) class] < size)
1172                         {
1173                           other_mode = group_mode[(int) class];
1174                           allocate_mode = mode;
1175
1176                           group_size[(int) class] = size;
1177                           group_mode[(int) class] = mode;
1178                         }
1179                       else
1180                         {
1181                           other_mode = mode;
1182                           allocate_mode = group_mode[(int) class];
1183                         }
1184
1185                       /* Crash if two dissimilar machine modes both need
1186                          groups of consecutive regs of the same class.  */
1187
1188                       if (other_mode != VOIDmode
1189                           && other_mode != allocate_mode
1190                           && ! modes_equiv_for_class_p (allocate_mode,
1191                                                         other_mode,
1192                                                         class))
1193                         abort ();
1194                     }
1195                   else if (size == 1)
1196                     {
1197                       this_needs[(int) class] += 1;
1198                       p = reg_class_superclasses[(int) class];
1199                       while (*p != LIM_REG_CLASSES)
1200                         this_needs[(int) *p++] += 1;
1201                     }
1202                   else
1203                     abort ();
1204                 }
1205
1206               /* All reloads have been counted for this insn;
1207                  now merge the various times of use.
1208                  This sets insn_needs, etc., to the maximum total number
1209                  of registers needed at any point in this insn.  */
1210
1211               for (i = 0; i < N_REG_CLASSES; i++)
1212                 {
1213                   int this_max;
1214                   this_max = insn_needs_for_inputs[i];
1215                   if (insn_needs_for_outputs[i] > this_max)
1216                     this_max = insn_needs_for_outputs[i];
1217                   if (insn_needs_for_operands[i] > this_max)
1218                     this_max = insn_needs_for_operands[i];
1219                   insn_needs[i] += this_max;
1220                   this_max = insn_groups_for_inputs[i];
1221                   if (insn_groups_for_outputs[i] > this_max)
1222                     this_max = insn_groups_for_outputs[i];
1223                   if (insn_groups_for_operands[i] > this_max)
1224                     this_max = insn_groups_for_operands[i];
1225                   insn_groups[i] += this_max;
1226                 }
1227
1228               insn_total_groups += MAX (insn_total_groups_for_inputs,
1229                                         MAX (insn_total_groups_for_outputs,
1230                                              insn_total_groups_for_operands));
1231
1232               /* If this is a CALL_INSN and caller-saves will need
1233                  a spill register, act as if the spill register is
1234                  needed for this insn.   However, the spill register
1235                  can be used by any reload of this insn, so we only
1236                  need do something if no need for that class has
1237                  been recorded.
1238
1239                  The assumption that every CALL_INSN will trigger a
1240                  caller-save is highly conservative, however, the number
1241                  of cases where caller-saves will need a spill register but
1242                  a block containing a CALL_INSN won't need a spill register
1243                  of that class should be quite rare.
1244
1245                  If a group is needed, the size and mode of the group will
1246                  have been set up at the beginning of this loop.  */
1247
1248               if (GET_CODE (insn) == CALL_INSN
1249                   && caller_save_spill_class != NO_REGS)
1250                 {
1251                   int *caller_save_needs
1252                     = (caller_save_group_size > 1 ? insn_groups : insn_needs);
1253
1254                   if (caller_save_needs[(int) caller_save_spill_class] == 0)
1255                     {
1256                       register enum reg_class *p
1257                         = reg_class_superclasses[(int) caller_save_spill_class];
1258
1259                       caller_save_needs[(int) caller_save_spill_class]++;
1260
1261                       while (*p != LIM_REG_CLASSES)
1262                         caller_save_needs[(int) *p++] += 1;
1263                     }
1264
1265                   if (caller_save_group_size > 1)
1266                     insn_total_groups = MAX (insn_total_groups, 1);
1267
1268
1269                 /* Show that this basic block will need a register of
1270                    this class.  */
1271
1272                 if (global
1273                     && ! (basic_block_needs[(int) caller_save_spill_class]
1274                           [this_block]))
1275                   {
1276                     basic_block_needs[(int) caller_save_spill_class]
1277                       [this_block] = 1;
1278                     new_basic_block_needs = 1;
1279                   }
1280                 }
1281
1282 #ifdef SMALL_REGISTER_CLASSES
1283               /* If this insn stores the value of a function call,
1284                  and that value is in a register that has been spilled,
1285                  and if the insn needs a reload in a class
1286                  that might use that register as the reload register,
1287                  then add add an extra need in that class.
1288                  This makes sure we have a register available that does
1289                  not overlap the return value.  */
1290               if (avoid_return_reg)
1291                 {
1292                   int regno = REGNO (avoid_return_reg);
1293                   int nregs
1294                     = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1295                   int r;
1296                   for (r = regno; r < regno + nregs; r++)
1297                     if (spill_reg_order[r] >= 0)
1298                       for (i = 0; i < N_REG_CLASSES; i++)
1299                         if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1300                           {
1301                             /* ??? It's not clear what is really
1302                                right to do if this insn needs a group.
1303                                But maybe that cannot happen.  */
1304                             if (insn_needs[i] > 0 || insn_groups[i] > 0)
1305                               insn_needs[i]++;
1306                           }
1307                 }
1308 #endif /* SMALL_REGISTER_CLASSES */
1309
1310               /* For each class, collect maximum need of any insn.  */
1311
1312               for (i = 0; i < N_REG_CLASSES; i++)
1313                 {
1314                   if (max_needs[i] < insn_needs[i])
1315                     {
1316                       max_needs[i] = insn_needs[i];
1317                       max_needs_insn[i] = insn;
1318                     }
1319                   if (max_groups[i] < insn_groups[i])
1320                     {
1321                       max_groups[i] = insn_groups[i];
1322                       max_groups_insn[i] = insn;
1323                     }
1324                   if (insn_total_groups > 0)
1325                     if (max_nongroups[i] < insn_needs[i])
1326                       {
1327                         max_nongroups[i] = insn_needs[i];
1328                         max_nongroups_insn[i] = insn;
1329                       }
1330                 }
1331             }
1332           /* Note that there is a continue statement above.  */
1333         }
1334
1335       /* If we allocated any new memory locations, make another pass
1336          since it might have changed elimination offsets.  */
1337       if (starting_frame_size != get_frame_size ())
1338         something_changed = 1;
1339
1340       if (dumpfile)
1341         for (i = 0; i < N_REG_CLASSES; i++)
1342           {
1343             if (max_needs[i] > 0)
1344               fprintf (dumpfile,
1345                          ";; Need %d reg%s of class %s (for insn %d).\n",
1346                        max_needs[i], max_needs[i] == 1 ? "" : "s",
1347                        reg_class_names[i], INSN_UID (max_needs_insn[i]));
1348             if (max_nongroups[i] > 0)
1349               fprintf (dumpfile,
1350                        ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1351                        max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1352                        reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1353             if (max_groups[i] > 0)
1354               fprintf (dumpfile,
1355                        ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1356                        max_groups[i], max_groups[i] == 1 ? "" : "s",
1357                        mode_name[(int) group_mode[i]],
1358                        reg_class_names[i], INSN_UID (max_groups_insn[i]));
1359           }
1360                          
1361       /* If we have caller-saves, set up the save areas and see if caller-save
1362          will need a spill register.  */
1363
1364       if (caller_save_needed
1365           && ! setup_save_areas (&something_changed)
1366           && caller_save_spill_class  == NO_REGS)
1367         {
1368           /* The class we will need depends on whether the machine
1369              supports the sum of two registers for an address; see
1370              find_address_reloads for details.  */
1371
1372           caller_save_spill_class
1373             = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1374           caller_save_group_size
1375             = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1376           something_changed = 1;
1377         }
1378
1379       /* Now deduct from the needs for the registers already
1380          available (already spilled).  */
1381
1382       CLEAR_HARD_REG_SET (counted_for_groups);
1383       CLEAR_HARD_REG_SET (counted_for_nongroups);
1384
1385       /* First find all regs alone in their class
1386          and count them (if desired) for non-groups.
1387          We would be screwed if a group took the only reg in a class
1388          for which a non-group reload is needed.
1389          (Note there is still a bug; if a class has 2 regs,
1390          both could be stolen by groups and we would lose the same way.
1391          With luck, no machine will need a nongroup in a 2-reg class.)  */
1392
1393       for (i = 0; i < n_spills; i++)
1394         {
1395           register enum reg_class *p;
1396           class = (int) REGNO_REG_CLASS (spill_regs[i]);
1397
1398           if (reg_class_size[class] == 1 && max_nongroups[class] > 0)
1399             {
1400               max_needs[class]--;
1401               p = reg_class_superclasses[class];
1402               while (*p != LIM_REG_CLASSES)
1403                 max_needs[(int) *p++]--;
1404
1405               SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1406               max_nongroups[class]--;
1407               p = reg_class_superclasses[class];
1408               while (*p != LIM_REG_CLASSES)
1409                 {
1410                   if (max_nongroups[(int) *p] > 0)
1411                     SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1412                   max_nongroups[(int) *p++]--;
1413                 }
1414             }
1415         }
1416
1417       /* Now find all consecutive groups of spilled registers
1418          and mark each group off against the need for such groups.
1419          But don't count them against ordinary need, yet.  */
1420
1421       count_possible_groups (group_size, group_mode, max_groups);
1422
1423       /* Now count all spill regs against the individual need,
1424          This includes those counted above for groups,
1425          but not those previously counted for nongroups.
1426
1427          Those that weren't counted_for_groups can also count against
1428          the not-in-group need.  */
1429
1430       for (i = 0; i < n_spills; i++)
1431         {
1432           register enum reg_class *p;
1433           class = (int) REGNO_REG_CLASS (spill_regs[i]);
1434
1435           /* Those counted at the beginning shouldn't be counted twice.  */
1436           if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
1437             {
1438               max_needs[class]--;
1439               p = reg_class_superclasses[class];
1440               while (*p != LIM_REG_CLASSES)
1441                 max_needs[(int) *p++]--;
1442
1443               if (! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i]))
1444                 {
1445                   if (max_nongroups[class] > 0)
1446                     SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1447                   max_nongroups[class]--;
1448                   p = reg_class_superclasses[class];
1449                   while (*p != LIM_REG_CLASSES)
1450                     {
1451                       if (max_nongroups[(int) *p] > 0)
1452                         SET_HARD_REG_BIT (counted_for_nongroups,
1453                                           spill_regs[i]);
1454                       max_nongroups[(int) *p++]--;
1455                     }
1456                 }
1457             }
1458         }
1459
1460       /* See if anything that happened changes which eliminations are valid.
1461          For example, on the Sparc, whether or not the frame pointer can
1462          be eliminated can depend on what registers have been used.  We need
1463          not check some conditions again (such as flag_omit_frame_pointer)
1464          since they can't have changed.  */
1465
1466       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1467         if ((ep->from == FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
1468 #ifdef ELIMINABLE_REGS
1469             || ! CAN_ELIMINATE (ep->from, ep->to)
1470 #endif
1471             )
1472           ep->can_eliminate = 0;
1473
1474       /* Look for the case where we have discovered that we can't replace
1475          register A with register B and that means that we will now be
1476          trying to replace register A with register C.  This means we can
1477          no longer replace register C with register B and we need to disable
1478          such an elimination, if it exists.  This occurs often with A == ap,
1479          B == sp, and C == fp.  */
1480
1481       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1482         {
1483           struct elim_table *op;
1484           register int new_to = -1;
1485
1486           if (! ep->can_eliminate && ep->can_eliminate_previous)
1487             {
1488               /* Find the current elimination for ep->from, if there is a
1489                  new one.  */
1490               for (op = reg_eliminate;
1491                    op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1492                 if (op->from == ep->from && op->can_eliminate)
1493                   {
1494                     new_to = op->to;
1495                     break;
1496                   }
1497
1498               /* See if there is an elimination of NEW_TO -> EP->TO.  If so,
1499                  disable it.  */
1500               for (op = reg_eliminate;
1501                    op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1502                 if (op->from == new_to && op->to == ep->to)
1503                   op->can_eliminate = 0;
1504             }
1505         }
1506
1507       /* See if any registers that we thought we could eliminate the previous
1508          time are no longer eliminable.  If so, something has changed and we
1509          must spill the register.  Also, recompute the number of eliminable
1510          registers and see if the frame pointer is needed; it is if there is
1511          no elimination of the frame pointer that we can perform.  */
1512
1513       frame_pointer_needed = 1;
1514       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1515         {
1516           if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM)
1517             frame_pointer_needed = 0;
1518
1519           if (! ep->can_eliminate && ep->can_eliminate_previous)
1520             {
1521               ep->can_eliminate_previous = 0;
1522               spill_hard_reg (ep->from, global, dumpfile, 1);
1523               regs_ever_live[ep->from] = 1;
1524               something_changed = 1;
1525               num_eliminable--;
1526             }
1527         }
1528
1529       /* If all needs are met, we win.  */
1530
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;
1536
1537       /* Not all needs are met; must spill more hard regs.  */
1538
1539       /* If any element of basic_block_needs changed from 0 to 1,
1540          re-spill all the regs already spilled.  This may spill
1541          additional pseudos that didn't spill before.  */
1542
1543       if (new_basic_block_needs)
1544         for (i = 0; i < n_spills; i++)
1545           something_changed
1546             |= spill_hard_reg (spill_regs[i], global, dumpfile, 0);
1547
1548       /* Now find more reload regs to satisfy the remaining need
1549          Do it by ascending class number, since otherwise a reg
1550          might be spilled for a big class and might fail to count
1551          for a smaller class even though it belongs to that class.
1552
1553          Count spilled regs in `spills', and add entries to
1554          `spill_regs' and `spill_reg_order'.
1555
1556          ??? Note there is a problem here.
1557          When there is a need for a group in a high-numbered class,
1558          and also need for non-group regs that come from a lower class,
1559          the non-group regs are chosen first.  If there aren't many regs,
1560          they might leave no room for a group.
1561
1562          This was happening on the 386.  To fix it, we added the code
1563          that calls possible_group_p, so that the lower class won't
1564          break up the last possible group.
1565
1566          Really fixing the problem would require changes above
1567          in counting the regs already spilled, and in choose_reload_regs.
1568          It might be hard to avoid introducing bugs there.  */
1569
1570       for (class = 0; class < N_REG_CLASSES; class++)
1571         {
1572           /* First get the groups of registers.
1573              If we got single registers first, we might fragment
1574              possible groups.  */
1575           while (max_groups[class] > 0)
1576             {
1577               /* If any single spilled regs happen to form groups,
1578                  count them now.  Maybe we don't really need
1579                  to spill another group.  */
1580               count_possible_groups (group_size, group_mode, max_groups);
1581
1582               if (max_groups[class] <= 0)
1583                 break;
1584
1585               /* Groups of size 2 (the only groups used on most machines)
1586                  are treated specially.  */
1587               if (group_size[class] == 2)
1588                 {
1589                   /* First, look for a register that will complete a group.  */
1590                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1591                     {
1592                       int j = potential_reload_regs[i];
1593                       int other;
1594                       if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1595                           &&
1596                           ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1597                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1598                             && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1599                             && HARD_REGNO_MODE_OK (other, group_mode[class])
1600                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1601                                                     other)
1602                             /* We don't want one part of another group.
1603                                We could get "two groups" that overlap!  */
1604                             && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1605                            ||
1606                            (j < FIRST_PSEUDO_REGISTER - 1
1607                             && (other = j + 1, spill_reg_order[other] >= 0)
1608                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1609                             && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1610                             && HARD_REGNO_MODE_OK (j, group_mode[class])
1611                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1612                                                     other)
1613                             && ! TEST_HARD_REG_BIT (counted_for_groups,
1614                                                     other))))
1615                         {
1616                           register enum reg_class *p;
1617
1618                           /* We have found one that will complete a group,
1619                              so count off one group as provided.  */
1620                           max_groups[class]--;
1621                           p = reg_class_superclasses[class];
1622                           while (*p != LIM_REG_CLASSES)
1623                             max_groups[(int) *p++]--;
1624
1625                           /* Indicate both these regs are part of a group.  */
1626                           SET_HARD_REG_BIT (counted_for_groups, j);
1627                           SET_HARD_REG_BIT (counted_for_groups, other);
1628                           break;
1629                         }
1630                     }
1631                   /* We can't complete a group, so start one.  */
1632                   if (i == FIRST_PSEUDO_REGISTER)
1633                     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1634                       {
1635                         int j = potential_reload_regs[i];
1636                         if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1637                             && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1638                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1639                             && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1640                             && HARD_REGNO_MODE_OK (j, group_mode[class])
1641                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1642                                                     j + 1))
1643                           break;
1644                       }
1645
1646                   /* I should be the index in potential_reload_regs
1647                      of the new reload reg we have found.  */
1648
1649                   if (i >= FIRST_PSEUDO_REGISTER)
1650                     {
1651                       /* There are no groups left to spill.  */
1652                       spill_failure (max_groups_insn[class]);
1653                       failure = 1;
1654                       goto failed;
1655                     }
1656                   else
1657                     something_changed
1658                       |= new_spill_reg (i, class, max_needs, NULL_PTR,
1659                                         global, dumpfile);
1660                 }
1661               else
1662                 {
1663                   /* For groups of more than 2 registers,
1664                      look for a sufficient sequence of unspilled registers,
1665                      and spill them all at once.  */
1666                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1667                     {
1668                       int j = potential_reload_regs[i];
1669                       int k;
1670                       if (j >= 0
1671                           && j + group_size[class] <= FIRST_PSEUDO_REGISTER
1672                           && HARD_REGNO_MODE_OK (j, group_mode[class]))
1673                         {
1674                           /* Check each reg in the sequence.  */
1675                           for (k = 0; k < group_size[class]; k++)
1676                             if (! (spill_reg_order[j + k] < 0
1677                                    && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1678                                    && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1679                               break;
1680                           /* We got a full sequence, so spill them all.  */
1681                           if (k == group_size[class])
1682                             {
1683                               register enum reg_class *p;
1684                               for (k = 0; k < group_size[class]; k++)
1685                                 {
1686                                   int idx;
1687                                   SET_HARD_REG_BIT (counted_for_groups, j + k);
1688                                   for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1689                                     if (potential_reload_regs[idx] == j + k)
1690                                       break;
1691                                   something_changed
1692                                     |= new_spill_reg (idx, class,
1693                                                       max_needs, NULL_PTR,
1694                                                       global, dumpfile);
1695                                 }
1696
1697                               /* We have found one that will complete a group,
1698                                  so count off one group as provided.  */
1699                               max_groups[class]--;
1700                               p = reg_class_superclasses[class];
1701                               while (*p != LIM_REG_CLASSES)
1702                                 max_groups[(int) *p++]--;
1703
1704                               break;
1705                             }
1706                         }
1707                     }
1708                   /* We couldn't find any registers for this reload.
1709                      Avoid going into an infinite loop.  */
1710                   if (i >= FIRST_PSEUDO_REGISTER)
1711                     {
1712                       /* There are no groups left.  */
1713                       spill_failure (max_groups_insn[class]);
1714                       failure = 1;
1715                       goto failed;
1716                     }
1717                 }
1718             }
1719
1720           /* Now similarly satisfy all need for single registers.  */
1721
1722           while (max_needs[class] > 0 || max_nongroups[class] > 0)
1723             {
1724               /* Consider the potential reload regs that aren't
1725                  yet in use as reload regs, in order of preference.
1726                  Find the most preferred one that's in this class.  */
1727
1728               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1729                 if (potential_reload_regs[i] >= 0
1730                     && TEST_HARD_REG_BIT (reg_class_contents[class],
1731                                           potential_reload_regs[i])
1732                     /* If this reg will not be available for groups,
1733                        pick one that does not foreclose possible groups.
1734                        This is a kludge, and not very general,
1735                        but it should be sufficient to make the 386 work,
1736                        and the problem should not occur on machines with
1737                        more registers.  */
1738                     && (max_nongroups[class] == 0
1739                         || possible_group_p (potential_reload_regs[i], max_groups)))
1740                   break;
1741
1742               /* If we couldn't get a register, try to get one even if we
1743                  might foreclose possible groups.  This may cause problems
1744                  later, but that's better than aborting now, since it is
1745                  possible that we will, in fact, be able to form the needed
1746                  group even with this allocation.  */
1747
1748               if (i >= FIRST_PSEUDO_REGISTER
1749                   && (asm_noperands (max_needs[class] > 0
1750                                      ? max_needs_insn[class]
1751                                      : max_nongroups_insn[class])
1752                       < 0))
1753                 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1754                   if (potential_reload_regs[i] >= 0
1755                       && TEST_HARD_REG_BIT (reg_class_contents[class],
1756                                             potential_reload_regs[i]))
1757                     break;
1758
1759               /* I should be the index in potential_reload_regs
1760                  of the new reload reg we have found.  */
1761
1762               if (i >= FIRST_PSEUDO_REGISTER)
1763                 {
1764                   /* There are no possible registers left to spill.  */
1765                   spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
1766                                  : max_nongroups_insn[class]);
1767                   failure = 1;
1768                   goto failed;
1769                 }
1770               else
1771                 something_changed
1772                   |= new_spill_reg (i, class, max_needs, max_nongroups,
1773                                     global, dumpfile);
1774             }
1775         }
1776     }
1777
1778   /* If global-alloc was run, notify it of any register eliminations we have
1779      done.  */
1780   if (global)
1781     for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1782       if (ep->can_eliminate)
1783         mark_elimination (ep->from, ep->to);
1784
1785   /* Insert code to save and restore call-clobbered hard regs
1786      around calls.  Tell if what mode to use so that we will process
1787      those insns in reload_as_needed if we have to.  */
1788
1789   if (caller_save_needed)
1790     save_call_clobbered_regs (num_eliminable ? QImode
1791                               : caller_save_spill_class != NO_REGS ? HImode
1792                               : VOIDmode);
1793
1794   /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1795      If that insn didn't set the register (i.e., it copied the register to
1796      memory), just delete that insn instead of the equivalencing insn plus
1797      anything now dead.  If we call delete_dead_insn on that insn, we may
1798      delete the insn that actually sets the register if the register die
1799      there and that is incorrect.  */
1800
1801   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1802     if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
1803         && GET_CODE (reg_equiv_init[i]) != NOTE)
1804       {
1805         if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
1806           delete_dead_insn (reg_equiv_init[i]);
1807         else
1808           {
1809             PUT_CODE (reg_equiv_init[i], NOTE);
1810             NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
1811             NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
1812           }
1813       }
1814
1815   /* Use the reload registers where necessary
1816      by generating move instructions to move the must-be-register
1817      values into or out of the reload registers.  */
1818
1819   if (something_needs_reloads || something_needs_elimination
1820       || (caller_save_needed && num_eliminable)
1821       || caller_save_spill_class != NO_REGS)
1822     reload_as_needed (first, global);
1823
1824   /* If we were able to eliminate the frame pointer, show that it is no
1825      longer live at the start of any basic block.  If it is live by
1826      virtue of being in a pseudo, that pseudo will be marked live
1827      and hence the frame pointer will be known to be live via that
1828      pseudo.  */
1829
1830   if (! frame_pointer_needed)
1831     for (i = 0; i < n_basic_blocks; i++)
1832       basic_block_live_at_start[i][FRAME_POINTER_REGNUM / REGSET_ELT_BITS]
1833         &= ~ ((REGSET_ELT_TYPE) 1 << (FRAME_POINTER_REGNUM % REGSET_ELT_BITS));
1834
1835   /* Come here (with failure set nonzero) if we can't get enough spill regs
1836      and we decide not to abort about it.  */
1837  failed:
1838
1839   reload_in_progress = 0;
1840
1841   /* Now eliminate all pseudo regs by modifying them into
1842      their equivalent memory references.
1843      The REG-rtx's for the pseudos are modified in place,
1844      so all insns that used to refer to them now refer to memory.
1845
1846      For a reg that has a reg_equiv_address, all those insns
1847      were changed by reloading so that no insns refer to it any longer;
1848      but the DECL_RTL of a variable decl may refer to it,
1849      and if so this causes the debugging info to mention the variable.  */
1850
1851   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1852     {
1853       rtx addr = 0;
1854       int in_struct = 0;
1855       if (reg_equiv_mem[i])
1856         {
1857           addr = XEXP (reg_equiv_mem[i], 0);
1858           in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
1859         }
1860       if (reg_equiv_address[i])
1861         addr = reg_equiv_address[i];
1862       if (addr)
1863         {
1864           if (reg_renumber[i] < 0)
1865             {
1866               rtx reg = regno_reg_rtx[i];
1867               XEXP (reg, 0) = addr;
1868               REG_USERVAR_P (reg) = 0;
1869               MEM_IN_STRUCT_P (reg) = in_struct;
1870               PUT_CODE (reg, MEM);
1871             }
1872           else if (reg_equiv_mem[i])
1873             XEXP (reg_equiv_mem[i], 0) = addr;
1874         }
1875     }
1876
1877 #ifdef PRESERVE_DEATH_INFO_REGNO_P
1878   /* Make a pass over all the insns and remove death notes for things that
1879      are no longer registers or no longer die in the insn (e.g., an input
1880      and output pseudo being tied).  */
1881
1882   for (insn = first; insn; insn = NEXT_INSN (insn))
1883     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1884       {
1885         rtx note, next;
1886
1887         for (note = REG_NOTES (insn); note; note = next)
1888           {
1889             next = XEXP (note, 1);
1890             if (REG_NOTE_KIND (note) == REG_DEAD
1891                 && (GET_CODE (XEXP (note, 0)) != REG
1892                     || reg_set_p (XEXP (note, 0), PATTERN (insn))))
1893               remove_note (insn, note);
1894           }
1895       }
1896 #endif
1897
1898   /* Indicate that we no longer have known memory locations or constants.  */
1899   reg_equiv_constant = 0;
1900   reg_equiv_memory_loc = 0;
1901
1902   return failure;
1903 }
1904 \f
1905 /* Nonzero if, after spilling reg REGNO for non-groups,
1906    it will still be possible to find a group if we still need one.  */
1907
1908 static int
1909 possible_group_p (regno, max_groups)
1910      int regno;
1911      int *max_groups;
1912 {
1913   int i;
1914   int class = (int) NO_REGS;
1915
1916   for (i = 0; i < (int) N_REG_CLASSES; i++)
1917     if (max_groups[i] > 0)
1918       {
1919         class = i;
1920         break;
1921       }
1922
1923   if (class == (int) NO_REGS)
1924     return 1;
1925
1926   /* Consider each pair of consecutive registers.  */
1927   for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
1928     {
1929       /* Ignore pairs that include reg REGNO.  */
1930       if (i == regno || i + 1 == regno)
1931         continue;
1932
1933       /* Ignore pairs that are outside the class that needs the group.
1934          ??? Here we fail to handle the case where two different classes
1935          independently need groups.  But this never happens with our
1936          current machine descriptions.  */
1937       if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1938              && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
1939         continue;
1940
1941       /* A pair of consecutive regs we can still spill does the trick.  */
1942       if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
1943           && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1944           && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
1945         return 1;
1946
1947       /* A pair of one already spilled and one we can spill does it
1948          provided the one already spilled is not otherwise reserved.  */
1949       if (spill_reg_order[i] < 0
1950           && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1951           && spill_reg_order[i + 1] >= 0
1952           && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
1953           && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
1954         return 1;
1955       if (spill_reg_order[i + 1] < 0
1956           && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
1957           && spill_reg_order[i] >= 0
1958           && ! TEST_HARD_REG_BIT (counted_for_groups, i)
1959           && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
1960         return 1;
1961     }
1962
1963   return 0;
1964 }
1965 \f
1966 /* Count any groups that can be formed from the registers recently spilled.
1967    This is done class by class, in order of ascending class number.  */
1968
1969 static void
1970 count_possible_groups (group_size, group_mode, max_groups)
1971      int *group_size, *max_groups;
1972      enum machine_mode *group_mode;
1973 {
1974   int i;
1975   /* Now find all consecutive groups of spilled registers
1976      and mark each group off against the need for such groups.
1977      But don't count them against ordinary need, yet.  */
1978
1979   for (i = 0; i < N_REG_CLASSES; i++)
1980     if (group_size[i] > 1)
1981       {
1982         HARD_REG_SET new;
1983         int j;
1984
1985         CLEAR_HARD_REG_SET (new);
1986
1987         /* Make a mask of all the regs that are spill regs in class I.  */
1988         for (j = 0; j < n_spills; j++)
1989           if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
1990               && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[j])
1991               && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1992                                       spill_regs[j]))
1993             SET_HARD_REG_BIT (new, spill_regs[j]);
1994
1995         /* Find each consecutive group of them.  */
1996         for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
1997           if (TEST_HARD_REG_BIT (new, j)
1998               && j + group_size[i] <= FIRST_PSEUDO_REGISTER
1999               /* Next line in case group-mode for this class
2000                  demands an even-odd pair.  */
2001               && HARD_REGNO_MODE_OK (j, group_mode[i]))
2002             {
2003               int k;
2004               for (k = 1; k < group_size[i]; k++)
2005                 if (! TEST_HARD_REG_BIT (new, j + k))
2006                   break;
2007               if (k == group_size[i])
2008                 {
2009                   /* We found a group.  Mark it off against this class's
2010                      need for groups, and against each superclass too.  */
2011                   register enum reg_class *p;
2012                   max_groups[i]--;
2013                   p = reg_class_superclasses[i];
2014                   while (*p != LIM_REG_CLASSES)
2015                     max_groups[(int) *p++]--;
2016                   /* Don't count these registers again.  */
2017                   for (k = 0; k < group_size[i]; k++)
2018                     SET_HARD_REG_BIT (counted_for_groups, j + k);
2019                 }
2020               /* Skip to the last reg in this group.  When j is incremented
2021                  above, it will then point to the first reg of the next
2022                  possible group.  */
2023               j += k - 1;
2024             }
2025       }
2026
2027 }
2028 \f
2029 /* ALLOCATE_MODE is a register mode that needs to be reloaded.  OTHER_MODE is
2030    another mode that needs to be reloaded for the same register class CLASS.
2031    If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2032    ALLOCATE_MODE will never be smaller than OTHER_MODE.
2033
2034    This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2035    ALLOCATE_MODE.  This test is unnecessary, because we will never try to put
2036    something of mode ALLOCATE_MODE into an OTHER_MODE register.  Testing this
2037    causes unnecessary failures on machines requiring alignment of register
2038    groups when the two modes are different sizes, because the larger mode has
2039    more strict alignment rules than the smaller mode.  */
2040
2041 static int
2042 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2043      enum machine_mode allocate_mode, other_mode;
2044      enum reg_class class;
2045 {
2046   register int regno;
2047   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2048     {
2049       if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2050           && HARD_REGNO_MODE_OK (regno, allocate_mode)
2051           && ! HARD_REGNO_MODE_OK (regno, other_mode))
2052         return 0;
2053     }
2054   return 1;
2055 }
2056
2057 /* Handle the failure to find a register to spill.
2058    INSN should be one of the insns which needed this particular spill reg.  */
2059
2060 static void
2061 spill_failure (insn)
2062      rtx insn;
2063 {
2064   if (asm_noperands (PATTERN (insn)) >= 0)
2065     error_for_asm (insn, "`asm' needs too many reloads");
2066   else
2067     abort ();
2068 }
2069
2070 /* Add a new register to the tables of available spill-registers
2071     (as well as spilling all pseudos allocated to the register).
2072    I is the index of this register in potential_reload_regs.
2073    CLASS is the regclass whose need is being satisfied.
2074    MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2075     so that this register can count off against them.
2076     MAX_NONGROUPS is 0 if this register is part of a group.
2077    GLOBAL and DUMPFILE are the same as the args that `reload' got.  */
2078
2079 static int
2080 new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2081      int i;
2082      int class;
2083      int *max_needs;
2084      int *max_nongroups;
2085      int global;
2086      FILE *dumpfile;
2087 {
2088   register enum reg_class *p;
2089   int val;
2090   int regno = potential_reload_regs[i];
2091
2092   if (i >= FIRST_PSEUDO_REGISTER)
2093     abort ();   /* Caller failed to find any register.  */
2094
2095   if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
2096     fatal ("fixed or forbidden register was spilled.\n\
2097 This may be due to a compiler bug or to impossible asm statements.");
2098
2099   /* Make reg REGNO an additional reload reg.  */
2100
2101   potential_reload_regs[i] = -1;
2102   spill_regs[n_spills] = regno;
2103   spill_reg_order[regno] = n_spills;
2104   if (dumpfile)
2105     fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2106
2107   /* Clear off the needs we just satisfied.  */
2108
2109   max_needs[class]--;
2110   p = reg_class_superclasses[class];
2111   while (*p != LIM_REG_CLASSES)
2112     max_needs[(int) *p++]--;
2113
2114   if (max_nongroups && max_nongroups[class] > 0)
2115     {
2116       SET_HARD_REG_BIT (counted_for_nongroups, regno);
2117       max_nongroups[class]--;
2118       p = reg_class_superclasses[class];
2119       while (*p != LIM_REG_CLASSES)
2120         max_nongroups[(int) *p++]--;
2121     }
2122
2123   /* Spill every pseudo reg that was allocated to this reg
2124      or to something that overlaps this reg.  */
2125
2126   val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2127
2128   /* If there are some registers still to eliminate and this register
2129      wasn't ever used before, additional stack space may have to be
2130      allocated to store this register.  Thus, we may have changed the offset
2131      between the stack and frame pointers, so mark that something has changed.
2132      (If new pseudos were spilled, thus requiring more space, VAL would have
2133      been set non-zero by the call to spill_hard_reg above since additional
2134      reloads may be needed in that case.
2135
2136      One might think that we need only set VAL to 1 if this is a call-used
2137      register.  However, the set of registers that must be saved by the
2138      prologue is not identical to the call-used set.  For example, the
2139      register used by the call insn for the return PC is a call-used register,
2140      but must be saved by the prologue.  */
2141   if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2142     val = 1;
2143
2144   regs_ever_live[spill_regs[n_spills]] = 1;
2145   n_spills++;
2146
2147   return val;
2148 }
2149 \f
2150 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2151    data that is dead in INSN.  */
2152
2153 static void
2154 delete_dead_insn (insn)
2155      rtx insn;
2156 {
2157   rtx prev = prev_real_insn (insn);
2158   rtx prev_dest;
2159
2160   /* If the previous insn sets a register that dies in our insn, delete it
2161      too.  */
2162   if (prev && GET_CODE (PATTERN (prev)) == SET
2163       && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2164       && reg_mentioned_p (prev_dest, PATTERN (insn))
2165       && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2166     delete_dead_insn (prev);
2167
2168   PUT_CODE (insn, NOTE);
2169   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2170   NOTE_SOURCE_FILE (insn) = 0;
2171 }
2172
2173 /* Modify the home of pseudo-reg I.
2174    The new home is present in reg_renumber[I].
2175
2176    FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2177    or it may be -1, meaning there is none or it is not relevant.
2178    This is used so that all pseudos spilled from a given hard reg
2179    can share one stack slot.  */
2180
2181 static void
2182 alter_reg (i, from_reg)
2183      register int i;
2184      int from_reg;
2185 {
2186   /* When outputting an inline function, this can happen
2187      for a reg that isn't actually used.  */
2188   if (regno_reg_rtx[i] == 0)
2189     return;
2190
2191   /* If the reg got changed to a MEM at rtl-generation time,
2192      ignore it.  */
2193   if (GET_CODE (regno_reg_rtx[i]) != REG)
2194     return;
2195
2196   /* Modify the reg-rtx to contain the new hard reg
2197      number or else to contain its pseudo reg number.  */
2198   REGNO (regno_reg_rtx[i])
2199     = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2200
2201   /* If we have a pseudo that is needed but has no hard reg or equivalent,
2202      allocate a stack slot for it.  */
2203
2204   if (reg_renumber[i] < 0
2205       && reg_n_refs[i] > 0
2206       && reg_equiv_constant[i] == 0
2207       && reg_equiv_memory_loc[i] == 0)
2208     {
2209       register rtx x;
2210       int inherent_size = PSEUDO_REGNO_BYTES (i);
2211       int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2212       int adjust = 0;
2213
2214       /* Each pseudo reg has an inherent size which comes from its own mode,
2215          and a total size which provides room for paradoxical subregs
2216          which refer to the pseudo reg in wider modes.
2217
2218          We can use a slot already allocated if it provides both
2219          enough inherent space and enough total space.
2220          Otherwise, we allocate a new slot, making sure that it has no less
2221          inherent space, and no less total space, then the previous slot.  */
2222       if (from_reg == -1)
2223         {
2224           /* No known place to spill from => no slot to reuse.  */
2225           x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size, -1);
2226 #if BYTES_BIG_ENDIAN
2227           /* Cancel the  big-endian correction done in assign_stack_local.
2228              Get the address of the beginning of the slot.
2229              This is so we can do a big-endian correction unconditionally
2230              below.  */
2231           adjust = inherent_size - total_size;
2232 #endif
2233         }
2234       /* Reuse a stack slot if possible.  */
2235       else if (spill_stack_slot[from_reg] != 0
2236                && spill_stack_slot_width[from_reg] >= total_size
2237                && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2238                    >= inherent_size))
2239         x = spill_stack_slot[from_reg];
2240       /* Allocate a bigger slot.  */
2241       else
2242         {
2243           /* Compute maximum size needed, both for inherent size
2244              and for total size.  */
2245           enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2246           if (spill_stack_slot[from_reg])
2247             {
2248               if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2249                   > inherent_size)
2250                 mode = GET_MODE (spill_stack_slot[from_reg]);
2251               if (spill_stack_slot_width[from_reg] > total_size)
2252                 total_size = spill_stack_slot_width[from_reg];
2253             }
2254           /* Make a slot with that size.  */
2255           x = assign_stack_local (mode, total_size, -1);
2256 #if BYTES_BIG_ENDIAN
2257           /* Cancel the  big-endian correction done in assign_stack_local.
2258              Get the address of the beginning of the slot.
2259              This is so we can do a big-endian correction unconditionally
2260              below.  */
2261           adjust = GET_MODE_SIZE (mode) - total_size;
2262 #endif
2263           spill_stack_slot[from_reg] = x;
2264           spill_stack_slot_width[from_reg] = total_size;
2265         }
2266
2267 #if BYTES_BIG_ENDIAN
2268       /* On a big endian machine, the "address" of the slot
2269          is the address of the low part that fits its inherent mode.  */
2270       if (inherent_size < total_size)
2271         adjust += (total_size - inherent_size);
2272 #endif /* BYTES_BIG_ENDIAN */
2273
2274       /* If we have any adjustment to make, or if the stack slot is the
2275          wrong mode, make a new stack slot.  */
2276       if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2277         {
2278           x = gen_rtx (MEM, GET_MODE (regno_reg_rtx[i]),
2279                        plus_constant (XEXP (x, 0), adjust));
2280           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2281         }
2282
2283       /* Save the stack slot for later.   */
2284       reg_equiv_memory_loc[i] = x;
2285     }
2286 }
2287
2288 /* Mark the slots in regs_ever_live for the hard regs
2289    used by pseudo-reg number REGNO.  */
2290
2291 void
2292 mark_home_live (regno)
2293      int regno;
2294 {
2295   register int i, lim;
2296   i = reg_renumber[regno];
2297   if (i < 0)
2298     return;
2299   lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2300   while (i < lim)
2301     regs_ever_live[i++] = 1;
2302 }
2303 \f
2304 /* This function handles the tracking of elimination offsets around branches.
2305
2306    X is a piece of RTL being scanned.
2307
2308    INSN is the insn that it came from, if any.
2309
2310    INITIAL_P is non-zero if we are to set the offset to be the initial
2311    offset and zero if we are setting the offset of the label to be the
2312    current offset.  */
2313
2314 static void
2315 set_label_offsets (x, insn, initial_p)
2316      rtx x;
2317      rtx insn;
2318      int initial_p;
2319 {
2320   enum rtx_code code = GET_CODE (x);
2321   rtx tem;
2322   int i;
2323   struct elim_table *p;
2324
2325   switch (code)
2326     {
2327     case LABEL_REF:
2328       if (LABEL_REF_NONLOCAL_P (x))
2329         return;
2330
2331       x = XEXP (x, 0);
2332
2333       /* ... fall through ... */
2334
2335     case CODE_LABEL:
2336       /* If we know nothing about this label, set the desired offsets.  Note
2337          that this sets the offset at a label to be the offset before a label
2338          if we don't know anything about the label.  This is not correct for
2339          the label after a BARRIER, but is the best guess we can make.  If
2340          we guessed wrong, we will suppress an elimination that might have
2341          been possible had we been able to guess correctly.  */
2342
2343       if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2344         {
2345           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2346             offsets_at[CODE_LABEL_NUMBER (x)][i]
2347               = (initial_p ? reg_eliminate[i].initial_offset
2348                  : reg_eliminate[i].offset);
2349           offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2350         }
2351
2352       /* Otherwise, if this is the definition of a label and it is
2353          preceded by a BARRIER, set our offsets to the known offset of
2354          that label.  */
2355
2356       else if (x == insn
2357                && (tem = prev_nonnote_insn (insn)) != 0
2358                && GET_CODE (tem) == BARRIER)
2359         {
2360           num_not_at_initial_offset = 0;
2361           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2362             {
2363               reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2364                 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2365               if (reg_eliminate[i].can_eliminate
2366                   && (reg_eliminate[i].offset
2367                       != reg_eliminate[i].initial_offset))
2368                 num_not_at_initial_offset++;
2369             }
2370         }
2371
2372       else
2373         /* If neither of the above cases is true, compare each offset
2374            with those previously recorded and suppress any eliminations
2375            where the offsets disagree.  */
2376
2377         for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2378           if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2379               != (initial_p ? reg_eliminate[i].initial_offset
2380                   : reg_eliminate[i].offset))
2381             reg_eliminate[i].can_eliminate = 0;
2382
2383       return;
2384
2385     case JUMP_INSN:
2386       set_label_offsets (PATTERN (insn), insn, initial_p);
2387
2388       /* ... fall through ... */
2389
2390     case INSN:
2391     case CALL_INSN:
2392       /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2393          and hence must have all eliminations at their initial offsets.  */
2394       for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2395         if (REG_NOTE_KIND (tem) == REG_LABEL)
2396           set_label_offsets (XEXP (tem, 0), insn, 1);
2397       return;
2398
2399     case ADDR_VEC:
2400     case ADDR_DIFF_VEC:
2401       /* Each of the labels in the address vector must be at their initial
2402          offsets.  We want the first first for ADDR_VEC and the second
2403          field for ADDR_DIFF_VEC.  */
2404
2405       for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2406         set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2407                            insn, initial_p);
2408       return;
2409
2410     case SET:
2411       /* We only care about setting PC.  If the source is not RETURN,
2412          IF_THEN_ELSE, or a label, disable any eliminations not at
2413          their initial offsets.  Similarly if any arm of the IF_THEN_ELSE
2414          isn't one of those possibilities.  For branches to a label,
2415          call ourselves recursively.
2416
2417          Note that this can disable elimination unnecessarily when we have
2418          a non-local goto since it will look like a non-constant jump to
2419          someplace in the current function.  This isn't a significant
2420          problem since such jumps will normally be when all elimination
2421          pairs are back to their initial offsets.  */
2422
2423       if (SET_DEST (x) != pc_rtx)
2424         return;
2425
2426       switch (GET_CODE (SET_SRC (x)))
2427         {
2428         case PC:
2429         case RETURN:
2430           return;
2431
2432         case LABEL_REF:
2433           set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2434           return;
2435
2436         case IF_THEN_ELSE:
2437           tem = XEXP (SET_SRC (x), 1);
2438           if (GET_CODE (tem) == LABEL_REF)
2439             set_label_offsets (XEXP (tem, 0), insn, initial_p);
2440           else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2441             break;
2442
2443           tem = XEXP (SET_SRC (x), 2);
2444           if (GET_CODE (tem) == LABEL_REF)
2445             set_label_offsets (XEXP (tem, 0), insn, initial_p);
2446           else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2447             break;
2448           return;
2449         }
2450
2451       /* If we reach here, all eliminations must be at their initial
2452          offset because we are doing a jump to a variable address.  */
2453       for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2454         if (p->offset != p->initial_offset)
2455           p->can_eliminate = 0;
2456     }
2457 }
2458 \f
2459 /* Used for communication between the next two function to properly share
2460    the vector for an ASM_OPERANDS.  */
2461
2462 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2463
2464 /* Scan X and replace any eliminable registers (such as fp) with a
2465    replacement (such as sp), plus an offset.
2466
2467    MEM_MODE is the mode of an enclosing MEM.  We need this to know how
2468    much to adjust a register for, e.g., PRE_DEC.  Also, if we are inside a
2469    MEM, we are allowed to replace a sum of a register and the constant zero
2470    with the register, which we cannot do outside a MEM.  In addition, we need
2471    to record the fact that a register is referenced outside a MEM.
2472
2473    If INSN is nonzero, it is the insn containing X.  If we replace a REG
2474    in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2475    CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2476    that the REG is being modified.
2477
2478    If we see a modification to a register we know about, take the
2479    appropriate action (see case SET, below).
2480
2481    REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2482    replacements done assuming all offsets are at their initial values.  If
2483    they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2484    encounter, return the actual location so that find_reloads will do
2485    the proper thing.  */
2486
2487 rtx
2488 eliminate_regs (x, mem_mode, insn)
2489      rtx x;
2490      enum machine_mode mem_mode;
2491      rtx insn;
2492 {
2493   enum rtx_code code = GET_CODE (x);
2494   struct elim_table *ep;
2495   int regno;
2496   rtx new;
2497   int i, j;
2498   char *fmt;
2499   int copied = 0;
2500
2501   switch (code)
2502     {
2503     case CONST_INT:
2504     case CONST_DOUBLE:
2505     case CONST:
2506     case SYMBOL_REF:
2507     case CODE_LABEL:
2508     case PC:
2509     case CC0:
2510     case ASM_INPUT:
2511     case ADDR_VEC:
2512     case ADDR_DIFF_VEC:
2513     case RETURN:
2514       return x;
2515
2516     case REG:
2517       regno = REGNO (x);
2518
2519       /* First handle the case where we encounter a bare register that
2520          is eliminable.  Replace it with a PLUS.  */
2521       if (regno < FIRST_PSEUDO_REGISTER)
2522         {
2523           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2524                ep++)
2525             if (ep->from_rtx == x && ep->can_eliminate)
2526               {
2527                 if (! mem_mode)
2528                   ep->ref_outside_mem = 1;
2529                 return plus_constant (ep->to_rtx, ep->previous_offset);
2530               }
2531
2532         }
2533       else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2534                && (reg_equiv_address[regno] || num_not_at_initial_offset))
2535         {
2536           /* In this case, find_reloads would attempt to either use an
2537              incorrect address (if something is not at its initial offset)
2538              or substitute an replaced address into an insn (which loses
2539              if the offset is changed by some later action).  So we simply
2540              return the replaced stack slot (assuming it is changed by
2541              elimination) and ignore the fact that this is actually a
2542              reference to the pseudo.  Ensure we make a copy of the
2543              address in case it is shared.  */
2544           new = eliminate_regs (reg_equiv_memory_loc[regno],
2545                                 mem_mode, NULL_RTX);
2546           if (new != reg_equiv_memory_loc[regno])
2547             return copy_rtx (new);
2548         }
2549       return x;
2550
2551     case PLUS:
2552       /* If this is the sum of an eliminable register and a constant, rework
2553          the sum.   */
2554       if (GET_CODE (XEXP (x, 0)) == REG
2555           && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2556           && CONSTANT_P (XEXP (x, 1)))
2557         {
2558           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2559                ep++)
2560             if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2561               {
2562                 if (! mem_mode)
2563                   ep->ref_outside_mem = 1;
2564
2565                 /* The only time we want to replace a PLUS with a REG (this
2566                    occurs when the constant operand of the PLUS is the negative
2567                    of the offset) is when we are inside a MEM.  We won't want
2568                    to do so at other times because that would change the
2569                    structure of the insn in a way that reload can't handle.
2570                    We special-case the commonest situation in
2571                    eliminate_regs_in_insn, so just replace a PLUS with a
2572                    PLUS here, unless inside a MEM.  */
2573                 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2574                     && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2575                   return ep->to_rtx;
2576                 else
2577                   return gen_rtx (PLUS, Pmode, ep->to_rtx,
2578                                   plus_constant (XEXP (x, 1),
2579                                                  ep->previous_offset));
2580               }
2581
2582           /* If the register is not eliminable, we are done since the other
2583              operand is a constant.  */
2584           return x;
2585         }
2586
2587       /* If this is part of an address, we want to bring any constant to the
2588          outermost PLUS.  We will do this by doing register replacement in
2589          our operands and seeing if a constant shows up in one of them.
2590
2591          We assume here this is part of an address (or a "load address" insn)
2592          since an eliminable register is not likely to appear in any other
2593          context.
2594
2595          If we have (plus (eliminable) (reg)), we want to produce
2596          (plus (plus (replacement) (reg) (const))).  If this was part of a
2597          normal add insn, (plus (replacement) (reg)) will be pushed as a
2598          reload.  This is the desired action.  */
2599
2600       {
2601         rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2602         rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX);
2603
2604         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2605           {
2606             /* If one side is a PLUS and the other side is a pseudo that
2607                didn't get a hard register but has a reg_equiv_constant,
2608                we must replace the constant here since it may no longer
2609                be in the position of any operand.  */
2610             if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2611                 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2612                 && reg_renumber[REGNO (new1)] < 0
2613                 && reg_equiv_constant != 0
2614                 && reg_equiv_constant[REGNO (new1)] != 0)
2615               new1 = reg_equiv_constant[REGNO (new1)];
2616             else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2617                      && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2618                      && reg_renumber[REGNO (new0)] < 0
2619                      && reg_equiv_constant[REGNO (new0)] != 0)
2620               new0 = reg_equiv_constant[REGNO (new0)];
2621
2622             new = form_sum (new0, new1);
2623
2624             /* As above, if we are not inside a MEM we do not want to
2625                turn a PLUS into something else.  We might try to do so here
2626                for an addition of 0 if we aren't optimizing.  */
2627             if (! mem_mode && GET_CODE (new) != PLUS)
2628               return gen_rtx (PLUS, GET_MODE (x), new, const0_rtx);
2629             else
2630               return new;
2631           }
2632       }
2633       return x;
2634
2635     case EXPR_LIST:
2636       /* If we have something in XEXP (x, 0), the usual case, eliminate it.  */
2637       if (XEXP (x, 0))
2638         {
2639           new = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2640           if (new != XEXP (x, 0))
2641             x = gen_rtx (EXPR_LIST, REG_NOTE_KIND (x), new, XEXP (x, 1));
2642         }
2643
2644       /* ... fall through ... */
2645
2646     case INSN_LIST:
2647       /* Now do eliminations in the rest of the chain.  If this was
2648          an EXPR_LIST, this might result in allocating more memory than is
2649          strictly needed, but it simplifies the code.  */
2650       if (XEXP (x, 1))
2651         {
2652           new = eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX);
2653           if (new != XEXP (x, 1))
2654             return gen_rtx (INSN_LIST, GET_MODE (x), XEXP (x, 0), new);
2655         }
2656       return x;
2657
2658     case CALL:
2659     case COMPARE:
2660     case MINUS:
2661     case MULT:
2662     case DIV:      case UDIV:
2663     case MOD:      case UMOD:
2664     case AND:      case IOR:      case XOR:
2665     case LSHIFT:   case ASHIFT:   case ROTATE:
2666     case ASHIFTRT: case LSHIFTRT: case ROTATERT:
2667     case NE:       case EQ:
2668     case GE:       case GT:       case GEU:    case GTU:
2669     case LE:       case LT:       case LEU:    case LTU:
2670       {
2671         rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2672         rtx new1
2673           = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX) : 0;
2674
2675         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2676           return gen_rtx (code, GET_MODE (x), new0, new1);
2677       }
2678       return x;
2679
2680     case PRE_INC:
2681     case POST_INC:
2682     case PRE_DEC:
2683     case POST_DEC:
2684       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2685         if (ep->to_rtx == XEXP (x, 0))
2686           {
2687             if (code == PRE_DEC || code == POST_DEC)
2688               ep->offset += GET_MODE_SIZE (mem_mode);
2689             else
2690               ep->offset -= GET_MODE_SIZE (mem_mode);
2691           }
2692
2693       /* Fall through to generic unary operation case.  */
2694     case USE:
2695     case STRICT_LOW_PART:
2696     case NEG:          case NOT:
2697     case SIGN_EXTEND:  case ZERO_EXTEND:
2698     case TRUNCATE:     case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2699     case FLOAT:        case FIX:
2700     case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2701     case ABS:
2702     case SQRT:
2703     case FFS:
2704       new = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2705       if (new != XEXP (x, 0))
2706         return gen_rtx (code, GET_MODE (x), new);
2707       return x;
2708
2709     case SUBREG:
2710       /* Similar to above processing, but preserve SUBREG_WORD.
2711          Convert (subreg (mem)) to (mem) if not paradoxical.
2712          Also, if we have a non-paradoxical (subreg (pseudo)) and the
2713          pseudo didn't get a hard reg, we must replace this with the
2714          eliminated version of the memory location because push_reloads
2715          may do the replacement in certain circumstances.  */
2716       if (GET_CODE (SUBREG_REG (x)) == REG
2717           && (GET_MODE_SIZE (GET_MODE (x))
2718               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2719           && reg_equiv_memory_loc != 0
2720           && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2721         {
2722           new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
2723                                 mem_mode, NULL_RTX);
2724
2725           /* If we didn't change anything, we must retain the pseudo.  */
2726           if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
2727             new = XEXP (x, 0);
2728           else
2729             /* Otherwise, ensure NEW isn't shared in case we have to reload
2730                it.  */
2731             new = copy_rtx (new);
2732         }
2733       else
2734         new = eliminate_regs (SUBREG_REG (x), mem_mode, NULL_RTX);
2735
2736       if (new != XEXP (x, 0))
2737         {
2738           if (GET_CODE (new) == MEM
2739               && (GET_MODE_SIZE (GET_MODE (x))
2740                   <= GET_MODE_SIZE (GET_MODE (new))))
2741             {
2742               int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2743               enum machine_mode mode = GET_MODE (x);
2744
2745 #if BYTES_BIG_ENDIAN
2746               offset += (MIN (UNITS_PER_WORD,
2747                               GET_MODE_SIZE (GET_MODE (new)))
2748                          - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2749 #endif
2750
2751               PUT_MODE (new, mode);
2752               XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2753               return new;
2754             }
2755           else
2756             return gen_rtx (SUBREG, GET_MODE (x), new, SUBREG_WORD (x));
2757         }
2758
2759       return x;
2760
2761     case CLOBBER:
2762       /* If clobbering a register that is the replacement register for an
2763          elimination we still think can be performed, note that it cannot
2764          be performed.  Otherwise, we need not be concerned about it.  */
2765       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2766         if (ep->to_rtx == XEXP (x, 0))
2767           ep->can_eliminate = 0;
2768
2769       new = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2770       if (new != XEXP (x, 0))
2771         return gen_rtx (code, GET_MODE (x), new);
2772       return x;
2773
2774     case ASM_OPERANDS:
2775       {
2776         rtx *temp_vec;
2777         /* Properly handle sharing input and constraint vectors.  */
2778         if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
2779           {
2780             /* When we come to a new vector not seen before,
2781                scan all its elements; keep the old vector if none
2782                of them changes; otherwise, make a copy.  */
2783             old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
2784             temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
2785             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2786               temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
2787                                             mem_mode, NULL_RTX);
2788
2789             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2790               if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
2791                 break;
2792
2793             if (i == ASM_OPERANDS_INPUT_LENGTH (x))
2794               new_asm_operands_vec = old_asm_operands_vec;
2795             else
2796               new_asm_operands_vec
2797                 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
2798           }
2799
2800         /* If we had to copy the vector, copy the entire ASM_OPERANDS.  */
2801         if (new_asm_operands_vec == old_asm_operands_vec)
2802           return x;
2803
2804         new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
2805                        ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2806                        ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
2807                        ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
2808                        ASM_OPERANDS_SOURCE_FILE (x),
2809                        ASM_OPERANDS_SOURCE_LINE (x));
2810         new->volatil = x->volatil;
2811         return new;
2812       }
2813
2814     case SET:
2815       /* Check for setting a register that we know about.  */
2816       if (GET_CODE (SET_DEST (x)) == REG)
2817         {
2818           /* See if this is setting the replacement register for an
2819              elimination.
2820
2821              If DEST is the frame pointer, we do nothing because we assume that
2822              all assignments to the frame pointer are for non-local gotos and
2823              are being done at a time when they are valid and do not disturb
2824              anything else.  Some machines want to eliminate a fake argument
2825              pointer with either the frame or stack pointer.  Assignments to
2826              the frame pointer must not prevent this elimination.  */
2827
2828           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2829                ep++)
2830             if (ep->to_rtx == SET_DEST (x)
2831                 && SET_DEST (x) != frame_pointer_rtx)
2832               {
2833                 /* If it is being incremented, adjust the offset.  Otherwise,
2834                    this elimination can't be done.  */
2835                 rtx src = SET_SRC (x);
2836
2837                 if (GET_CODE (src) == PLUS
2838                     && XEXP (src, 0) == SET_DEST (x)
2839                     && GET_CODE (XEXP (src, 1)) == CONST_INT)
2840                   ep->offset -= INTVAL (XEXP (src, 1));
2841                 else
2842                   ep->can_eliminate = 0;
2843               }
2844
2845           /* Now check to see we are assigning to a register that can be
2846              eliminated.  If so, it must be as part of a PARALLEL, since we
2847              will not have been called if this is a single SET.  So indicate
2848              that we can no longer eliminate this reg.  */
2849           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2850                ep++)
2851             if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
2852               ep->can_eliminate = 0;
2853         }
2854
2855       /* Now avoid the loop below in this common case.  */
2856       {
2857         rtx new0 = eliminate_regs (SET_DEST (x), 0, NULL_RTX);
2858         rtx new1 = eliminate_regs (SET_SRC (x), 0, NULL_RTX);
2859
2860         /* If SET_DEST changed from a REG to a MEM and INSN is non-zero,
2861            write a CLOBBER insn.  */
2862         if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
2863             && insn != 0)
2864           emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
2865
2866         if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
2867           return gen_rtx (SET, VOIDmode, new0, new1);
2868       }
2869
2870       return x;
2871
2872     case MEM:
2873       /* Our only special processing is to pass the mode of the MEM to our
2874          recursive call and copy the flags.  While we are here, handle this
2875          case more efficiently.  */
2876       new = eliminate_regs (XEXP (x, 0), GET_MODE (x), NULL_RTX);
2877       if (new != XEXP (x, 0))
2878         {
2879           new = gen_rtx (MEM, GET_MODE (x), new);
2880           new->volatil = x->volatil;
2881           new->unchanging = x->unchanging;
2882           new->in_struct = x->in_struct;
2883           return new;
2884         }
2885       else
2886         return x;
2887     }
2888
2889   /* Process each of our operands recursively.  If any have changed, make a
2890      copy of the rtx.  */
2891   fmt = GET_RTX_FORMAT (code);
2892   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2893     {
2894       if (*fmt == 'e')
2895         {
2896           new = eliminate_regs (XEXP (x, i), mem_mode, NULL_RTX);
2897           if (new != XEXP (x, i) && ! copied)
2898             {
2899               rtx new_x = rtx_alloc (code);
2900               bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2901                                 + (sizeof (new_x->fld[0])
2902                                    * GET_RTX_LENGTH (code))));
2903               x = new_x;
2904               copied = 1;
2905             }
2906           XEXP (x, i) = new;
2907         }
2908       else if (*fmt == 'E')
2909         {
2910           int copied_vec = 0;
2911           for (j = 0; j < XVECLEN (x, i); j++)
2912             {
2913               new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2914               if (new != XVECEXP (x, i, j) && ! copied_vec)
2915                 {
2916                   rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2917                                              &XVECEXP (x, i, 0));
2918                   if (! copied)
2919                     {
2920                       rtx new_x = rtx_alloc (code);
2921                       bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2922                                         + (sizeof (new_x->fld[0])
2923                                            * GET_RTX_LENGTH (code))));
2924                       x = new_x;
2925                       copied = 1;
2926                     }
2927                   XVEC (x, i) = new_v;
2928                   copied_vec = 1;
2929                 }
2930               XVECEXP (x, i, j) = new;
2931             }
2932         }
2933     }
2934
2935   return x;
2936 }
2937 \f
2938 /* Scan INSN and eliminate all eliminable registers in it.
2939
2940    If REPLACE is nonzero, do the replacement destructively.  Also
2941    delete the insn as dead it if it is setting an eliminable register.
2942
2943    If REPLACE is zero, do all our allocations in reload_obstack.
2944
2945    If no eliminations were done and this insn doesn't require any elimination
2946    processing (these are not identical conditions: it might be updating sp,
2947    but not referencing fp; this needs to be seen during reload_as_needed so
2948    that the offset between fp and sp can be taken into consideration), zero
2949    is returned.  Otherwise, 1 is returned.  */
2950
2951 static int
2952 eliminate_regs_in_insn (insn, replace)
2953      rtx insn;
2954      int replace;
2955 {
2956   rtx old_body = PATTERN (insn);
2957   rtx new_body;
2958   int val = 0;
2959   struct elim_table *ep;
2960
2961   if (! replace)
2962     push_obstacks (&reload_obstack, &reload_obstack);
2963
2964   if (GET_CODE (old_body) == SET && GET_CODE (SET_DEST (old_body)) == REG
2965       && REGNO (SET_DEST (old_body)) < FIRST_PSEUDO_REGISTER)
2966     {
2967       /* Check for setting an eliminable register.  */
2968       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2969         if (ep->from_rtx == SET_DEST (old_body) && ep->can_eliminate)
2970           {
2971             /* In this case this insn isn't serving a useful purpose.  We
2972                will delete it in reload_as_needed once we know that this
2973                elimination is, in fact, being done.
2974
2975                If REPLACE isn't set, we can't delete this insn, but neededn't
2976                process it since it won't be used unless something changes.  */
2977             if (replace)
2978               delete_dead_insn (insn);
2979             val = 1;
2980             goto done;
2981           }
2982
2983       /* Check for (set (reg) (plus (reg from) (offset))) where the offset
2984          in the insn is the negative of the offset in FROM.  Substitute
2985          (set (reg) (reg to)) for the insn and change its code.
2986
2987          We have to do this here, rather than in eliminate_regs, do that we can
2988          change the insn code.  */
2989
2990       if (GET_CODE (SET_SRC (old_body)) == PLUS
2991           && GET_CODE (XEXP (SET_SRC (old_body), 0)) == REG
2992           && GET_CODE (XEXP (SET_SRC (old_body), 1)) == CONST_INT)
2993         for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2994              ep++)
2995           if (ep->from_rtx == XEXP (SET_SRC (old_body), 0)
2996               && ep->can_eliminate
2997               && ep->offset == - INTVAL (XEXP (SET_SRC (old_body), 1)))
2998             {
2999               PATTERN (insn) = gen_rtx (SET, VOIDmode,
3000                                         SET_DEST (old_body), ep->to_rtx);
3001               INSN_CODE (insn) = -1;
3002               val = 1;
3003               goto done;
3004             }
3005     }
3006
3007   old_asm_operands_vec = 0;
3008
3009   /* Replace the body of this insn with a substituted form.  If we changed
3010      something, return non-zero.  If this is the final call for this
3011      insn (REPLACE is non-zero), do the elimination in REG_NOTES as well.
3012
3013      If we are replacing a body that was a (set X (plus Y Z)), try to
3014      re-recognize the insn.  We do this in case we had a simple addition
3015      but now can do this as a load-address.  This saves an insn in this
3016      common case. */
3017
3018   new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
3019   if (new_body != old_body)
3020     {
3021       /* If we aren't replacing things permanently and we changed something,
3022          make another copy to ensure that all the RTL is new.  Otherwise
3023          things can go wrong if find_reload swaps commutative operands
3024          and one is inside RTL that has been copied while the other is not. */
3025
3026       /* Don't copy an asm_operands because (1) there's no need and (2)
3027          copy_rtx can't do it properly when there are multiple outputs.  */
3028       if (! replace && asm_noperands (old_body) < 0)
3029         new_body = copy_rtx (new_body);
3030
3031       /* If we had a move insn but now we don't, rerecognize it.  */
3032       if ((GET_CODE (old_body) == SET && GET_CODE (SET_SRC (old_body)) == REG
3033            && (GET_CODE (new_body) != SET
3034                || GET_CODE (SET_SRC (new_body)) != REG))
3035           /* If this was an add insn before, rerecognize.  */
3036           ||
3037           (GET_CODE (old_body) == SET
3038            && GET_CODE (SET_SRC (old_body)) == PLUS))
3039         {
3040           if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3041             /* If recognition fails, store the new body anyway.
3042                It's normal to have recognition failures here
3043                due to bizarre memory addresses; reloading will fix them.  */
3044             PATTERN (insn) = new_body;
3045         }
3046       else
3047         PATTERN (insn) = new_body;
3048
3049       if (replace && REG_NOTES (insn))
3050         REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, NULL_RTX);
3051       val = 1;
3052     }
3053
3054   /* Loop through all elimination pairs.  See if any have changed and
3055      recalculate the number not at initial offset.
3056
3057      Compute the maximum offset (minimum offset if the stack does not
3058      grow downward) for each elimination pair.
3059
3060      We also detect a cases where register elimination cannot be done,
3061      namely, if a register would be both changed and referenced outside a MEM
3062      in the resulting insn since such an insn is often undefined and, even if
3063      not, we cannot know what meaning will be given to it.  Note that it is
3064      valid to have a register used in an address in an insn that changes it
3065      (presumably with a pre- or post-increment or decrement).
3066
3067      If anything changes, return nonzero.  */
3068
3069   num_not_at_initial_offset = 0;
3070   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3071     {
3072       if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3073         ep->can_eliminate = 0;
3074
3075       ep->ref_outside_mem = 0;
3076
3077       if (ep->previous_offset != ep->offset)
3078         val = 1;
3079
3080       ep->previous_offset = ep->offset;
3081       if (ep->can_eliminate && ep->offset != ep->initial_offset)
3082         num_not_at_initial_offset++;
3083
3084 #ifdef STACK_GROWS_DOWNWARD
3085       ep->max_offset = MAX (ep->max_offset, ep->offset);
3086 #else
3087       ep->max_offset = MIN (ep->max_offset, ep->offset);
3088 #endif
3089     }
3090
3091  done:
3092   if (! replace)
3093     pop_obstacks ();
3094
3095   return val;
3096 }
3097
3098 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3099    replacement we currently believe is valid, mark it as not eliminable if X
3100    modifies DEST in any way other than by adding a constant integer to it.
3101
3102    If DEST is the frame pointer, we do nothing because we assume that
3103    all assignments to the frame pointer are nonlocal gotos and are being done
3104    at a time when they are valid and do not disturb anything else.
3105    Some machines want to eliminate a fake argument pointer with either the
3106    frame or stack pointer.  Assignments to the frame pointer must not prevent
3107    this elimination.
3108
3109    Called via note_stores from reload before starting its passes to scan
3110    the insns of the function.  */
3111
3112 static void
3113 mark_not_eliminable (dest, x)
3114      rtx dest;
3115      rtx x;
3116 {
3117   register int i;
3118
3119   /* A SUBREG of a hard register here is just changing its mode.  We should
3120      not see a SUBREG of an eliminable hard register, but check just in
3121      case.  */
3122   if (GET_CODE (dest) == SUBREG)
3123     dest = SUBREG_REG (dest);
3124
3125   if (dest == frame_pointer_rtx)
3126     return;
3127
3128   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3129     if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3130         && (GET_CODE (x) != SET
3131             || GET_CODE (SET_SRC (x)) != PLUS
3132             || XEXP (SET_SRC (x), 0) != dest
3133             || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3134       {
3135         reg_eliminate[i].can_eliminate_previous
3136           = reg_eliminate[i].can_eliminate = 0;
3137         num_eliminable--;
3138       }
3139 }
3140 \f
3141 /* Kick all pseudos out of hard register REGNO.
3142    If GLOBAL is nonzero, try to find someplace else to put them.
3143    If DUMPFILE is nonzero, log actions taken on that file.
3144
3145    If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3146    because we found we can't eliminate some register.  In the case, no pseudos
3147    are allowed to be in the register, even if they are only in a block that
3148    doesn't require spill registers, unlike the case when we are spilling this
3149    hard reg to produce another spill register.
3150
3151    Return nonzero if any pseudos needed to be kicked out.  */
3152
3153 static int
3154 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3155      register int regno;
3156      int global;
3157      FILE *dumpfile;
3158      int cant_eliminate;
3159 {
3160   int something_changed = 0;
3161   register int i;
3162
3163   SET_HARD_REG_BIT (forbidden_regs, regno);
3164
3165   /* Spill every pseudo reg that was allocated to this reg
3166      or to something that overlaps this reg.  */
3167
3168   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3169     if (reg_renumber[i] >= 0
3170         && reg_renumber[i] <= regno
3171         && (reg_renumber[i]
3172             + HARD_REGNO_NREGS (reg_renumber[i],
3173                                 PSEUDO_REGNO_MODE (i))
3174             > regno))
3175       {
3176         enum reg_class class = REGNO_REG_CLASS (regno);
3177
3178         /* If this register belongs solely to a basic block which needed no
3179            spilling of any class that this register is contained in,
3180            leave it be, unless we are spilling this register because
3181            it was a hard register that can't be eliminated.   */
3182
3183         if (! cant_eliminate
3184             && basic_block_needs[0]
3185             && reg_basic_block[i] >= 0
3186             && basic_block_needs[(int) class][reg_basic_block[i]] == 0)
3187           {
3188             enum reg_class *p;
3189
3190             for (p = reg_class_superclasses[(int) class];
3191                  *p != LIM_REG_CLASSES; p++)
3192               if (basic_block_needs[(int) *p][reg_basic_block[i]] > 0)
3193                 break;
3194
3195             if (*p == LIM_REG_CLASSES)
3196               continue;
3197           }
3198
3199         /* Mark it as no longer having a hard register home.  */
3200         reg_renumber[i] = -1;
3201         /* We will need to scan everything again.  */
3202         something_changed = 1;
3203         if (global)
3204             retry_global_alloc (i, forbidden_regs);
3205
3206         alter_reg (i, regno);
3207         if (dumpfile)
3208           {
3209             if (reg_renumber[i] == -1)
3210               fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3211             else
3212               fprintf (dumpfile, " Register %d now in %d.\n\n",
3213                        i, reg_renumber[i]);
3214           }
3215       }
3216
3217   return something_changed;
3218 }
3219 \f
3220 /* Find all paradoxical subregs within X and update reg_max_ref_width.  */
3221
3222 static void
3223 scan_paradoxical_subregs (x)
3224      register rtx x;
3225 {
3226   register int i;
3227   register char *fmt;
3228   register enum rtx_code code = GET_CODE (x);
3229
3230   switch (code)
3231     {
3232     case CONST_INT:
3233     case CONST:
3234     case SYMBOL_REF:
3235     case LABEL_REF:
3236     case CONST_DOUBLE:
3237     case CC0:
3238     case PC:
3239     case REG:
3240     case USE:
3241     case CLOBBER:
3242       return;
3243
3244     case SUBREG:
3245       if (GET_CODE (SUBREG_REG (x)) == REG
3246           && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3247         reg_max_ref_width[REGNO (SUBREG_REG (x))]
3248           = GET_MODE_SIZE (GET_MODE (x));
3249       return;
3250     }
3251
3252   fmt = GET_RTX_FORMAT (code);
3253   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3254     {
3255       if (fmt[i] == 'e')
3256         scan_paradoxical_subregs (XEXP (x, i));
3257       else if (fmt[i] == 'E')
3258         {
3259           register int j;
3260           for (j = XVECLEN (x, i) - 1; j >=0; j--)
3261             scan_paradoxical_subregs (XVECEXP (x, i, j));
3262         }
3263     }
3264 }
3265 \f
3266 struct hard_reg_n_uses { int regno; int uses; };
3267
3268 static int
3269 hard_reg_use_compare (p1, p2)
3270      struct hard_reg_n_uses *p1, *p2;
3271 {
3272   int tem = p1->uses - p2->uses;
3273   if (tem != 0) return tem;
3274   /* If regs are equally good, sort by regno,
3275      so that the results of qsort leave nothing to chance.  */
3276   return p1->regno - p2->regno;
3277 }
3278
3279 /* Choose the order to consider regs for use as reload registers
3280    based on how much trouble would be caused by spilling one.
3281    Store them in order of decreasing preference in potential_reload_regs.  */
3282
3283 static void
3284 order_regs_for_reload ()
3285 {
3286   register int i;
3287   register int o = 0;
3288   int large = 0;
3289
3290   struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3291
3292   CLEAR_HARD_REG_SET (bad_spill_regs);
3293
3294   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3295     potential_reload_regs[i] = -1;
3296
3297   /* Count number of uses of each hard reg by pseudo regs allocated to it
3298      and then order them by decreasing use.  */
3299
3300   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3301     {
3302       hard_reg_n_uses[i].uses = 0;
3303       hard_reg_n_uses[i].regno = i;
3304     }
3305
3306   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3307     {
3308       int regno = reg_renumber[i];
3309       if (regno >= 0)
3310         {
3311           int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3312           while (regno < lim)
3313             hard_reg_n_uses[regno++].uses += reg_n_refs[i];
3314         }
3315       large += reg_n_refs[i];
3316     }
3317
3318   /* Now fixed registers (which cannot safely be used for reloading)
3319      get a very high use count so they will be considered least desirable.
3320      Registers used explicitly in the rtl code are almost as bad.  */
3321
3322   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3323     {
3324       if (fixed_regs[i])
3325         {
3326           hard_reg_n_uses[i].uses += 2 * large + 2;
3327           SET_HARD_REG_BIT (bad_spill_regs, i);
3328         }
3329       else if (regs_explicitly_used[i])
3330         {
3331           hard_reg_n_uses[i].uses += large + 1;
3332           /* ??? We are doing this here because of the potential that
3333              bad code may be generated if a register explicitly used in
3334              an insn was used as a spill register for that insn.  But
3335              not using these are spill registers may lose on some machine.
3336              We'll have to see how this works out.  */
3337           SET_HARD_REG_BIT (bad_spill_regs, i);
3338         }
3339     }
3340   hard_reg_n_uses[FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3341   SET_HARD_REG_BIT (bad_spill_regs, FRAME_POINTER_REGNUM);
3342
3343 #ifdef ELIMINABLE_REGS
3344   /* If registers other than the frame pointer are eliminable, mark them as
3345      poor choices.  */
3346   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3347     {
3348       hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3349       SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3350     }
3351 #endif
3352
3353   /* Prefer registers not so far used, for use in temporary loading.
3354      Among them, if REG_ALLOC_ORDER is defined, use that order.
3355      Otherwise, prefer registers not preserved by calls.  */
3356
3357 #ifdef REG_ALLOC_ORDER
3358   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3359     {
3360       int regno = reg_alloc_order[i];
3361
3362       if (hard_reg_n_uses[regno].uses == 0)
3363         potential_reload_regs[o++] = regno;
3364     }
3365 #else
3366   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3367     {
3368       if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3369         potential_reload_regs[o++] = i;
3370     }
3371   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3372     {
3373       if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3374         potential_reload_regs[o++] = i;
3375     }
3376 #endif
3377
3378   qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3379          sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3380
3381   /* Now add the regs that are already used,
3382      preferring those used less often.  The fixed and otherwise forbidden
3383      registers will be at the end of this list.  */
3384
3385   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3386     if (hard_reg_n_uses[i].uses != 0)
3387       potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3388 }
3389 \f
3390 /* Reload pseudo-registers into hard regs around each insn as needed.
3391    Additional register load insns are output before the insn that needs it
3392    and perhaps store insns after insns that modify the reloaded pseudo reg.
3393
3394    reg_last_reload_reg and reg_reloaded_contents keep track of
3395    which pseudo-registers are already available in reload registers.
3396    We update these for the reloads that we perform,
3397    as the insns are scanned.  */
3398
3399 static void
3400 reload_as_needed (first, live_known)
3401      rtx first;
3402      int live_known;
3403 {
3404   register rtx insn;
3405   register int i;
3406   int this_block = 0;
3407   rtx x;
3408   rtx after_call = 0;
3409
3410   bzero (spill_reg_rtx, sizeof spill_reg_rtx);
3411   reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3412   bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
3413   reg_has_output_reload = (char *) alloca (max_regno);
3414   for (i = 0; i < n_spills; i++)
3415     {
3416       reg_reloaded_contents[i] = -1;
3417       reg_reloaded_insn[i] = 0;
3418     }
3419
3420   /* Reset all offsets on eliminable registers to their initial values.  */
3421 #ifdef ELIMINABLE_REGS
3422   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3423     {
3424       INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
3425                                   reg_eliminate[i].initial_offset);
3426       reg_eliminate[i].previous_offset
3427         = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3428     }
3429 #else
3430   INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3431   reg_eliminate[0].previous_offset
3432     = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3433 #endif
3434
3435   num_not_at_initial_offset = 0;
3436
3437   for (insn = first; insn;)
3438     {
3439       register rtx next = NEXT_INSN (insn);
3440
3441       /* Notice when we move to a new basic block.  */
3442       if (live_known && this_block + 1 < n_basic_blocks
3443           && insn == basic_block_head[this_block+1])
3444         ++this_block;
3445
3446       /* If we pass a label, copy the offsets from the label information
3447          into the current offsets of each elimination.  */
3448       if (GET_CODE (insn) == CODE_LABEL)
3449         {
3450           num_not_at_initial_offset = 0;
3451           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3452             {
3453               reg_eliminate[i].offset = reg_eliminate[i].previous_offset
3454                 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
3455               if (reg_eliminate[i].can_eliminate
3456                   && (reg_eliminate[i].offset
3457                       != reg_eliminate[i].initial_offset))
3458                 num_not_at_initial_offset++;
3459             }
3460         }
3461
3462       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3463         {
3464           rtx avoid_return_reg = 0;
3465
3466 #ifdef SMALL_REGISTER_CLASSES
3467           /* Set avoid_return_reg if this is an insn
3468              that might use the value of a function call.  */
3469           if (GET_CODE (insn) == CALL_INSN)
3470             {
3471               if (GET_CODE (PATTERN (insn)) == SET)
3472                 after_call = SET_DEST (PATTERN (insn));
3473               else if (GET_CODE (PATTERN (insn)) == PARALLEL
3474                        && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
3475                 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
3476               else
3477                 after_call = 0;
3478             }
3479           else if (after_call != 0
3480                    && !(GET_CODE (PATTERN (insn)) == SET
3481                         && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
3482             {
3483               if (reg_mentioned_p (after_call, PATTERN (insn)))
3484                 avoid_return_reg = after_call;
3485               after_call = 0;
3486             }
3487 #endif /* SMALL_REGISTER_CLASSES */
3488
3489           /* If this is a USE and CLOBBER of a MEM, ensure that any
3490              references to eliminable registers have been removed.  */
3491
3492           if ((GET_CODE (PATTERN (insn)) == USE
3493                || GET_CODE (PATTERN (insn)) == CLOBBER)
3494               && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3495             XEXP (XEXP (PATTERN (insn), 0), 0)
3496               = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3497                                 GET_MODE (XEXP (PATTERN (insn), 0)), NULL_RTX);
3498
3499           /* If we need to do register elimination processing, do so.
3500              This might delete the insn, in which case we are done.  */
3501           if (num_eliminable && GET_MODE (insn) == QImode)
3502             {
3503               eliminate_regs_in_insn (insn, 1);
3504               if (GET_CODE (insn) == NOTE)
3505                 {
3506                   insn = next;
3507                   continue;
3508                 }
3509             }
3510
3511           if (GET_MODE (insn) == VOIDmode)
3512             n_reloads = 0;
3513           /* First find the pseudo regs that must be reloaded for this insn.
3514              This info is returned in the tables reload_... (see reload.h).
3515              Also modify the body of INSN by substituting RELOAD
3516              rtx's for those pseudo regs.  */
3517           else
3518             {
3519               bzero (reg_has_output_reload, max_regno);
3520               CLEAR_HARD_REG_SET (reg_is_output_reload);
3521
3522               find_reloads (insn, 1, spill_indirect_levels, live_known,
3523                             spill_reg_order);
3524             }
3525
3526           if (n_reloads > 0)
3527             {
3528               rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
3529               rtx p;
3530               int class;
3531
3532               /* If this block has not had spilling done for a
3533                  particular class, deactivate any optional reloads
3534                  of that class lest they try to use a spill-reg which isn't
3535                  available here.  If we have any non-optionals that need a
3536                  spill reg, abort.  */
3537
3538               for (class = 0; class < N_REG_CLASSES; class++)
3539                 if (basic_block_needs[class] != 0
3540                     && basic_block_needs[class][this_block] == 0)
3541                   for (i = 0; i < n_reloads; i++)
3542                     if (class == (int) reload_reg_class[i])
3543                       {
3544                         if (reload_optional[i])
3545                           {
3546                             reload_in[i] = reload_out[i] = 0;
3547                             reload_secondary_p[i] = 0;
3548                           }
3549                         else if (reload_reg_rtx[i] == 0
3550                                  && (reload_in[i] != 0 || reload_out[i] != 0
3551                                      || reload_secondary_p[i] != 0))
3552                           abort ();
3553                       }
3554
3555               /* Now compute which reload regs to reload them into.  Perhaps
3556                  reusing reload regs from previous insns, or else output
3557                  load insns to reload them.  Maybe output store insns too.
3558                  Record the choices of reload reg in reload_reg_rtx.  */
3559               choose_reload_regs (insn, avoid_return_reg);
3560
3561               /* Generate the insns to reload operands into or out of
3562                  their reload regs.  */
3563               emit_reload_insns (insn);
3564
3565               /* Substitute the chosen reload regs from reload_reg_rtx
3566                  into the insn's body (or perhaps into the bodies of other
3567                  load and store insn that we just made for reloading
3568                  and that we moved the structure into).  */
3569               subst_reloads ();
3570
3571               /* If this was an ASM, make sure that all the reload insns
3572                  we have generated are valid.  If not, give an error
3573                  and delete them.  */
3574
3575               if (asm_noperands (PATTERN (insn)) >= 0)
3576                 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3577                   if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
3578                       && (recog_memoized (p) < 0
3579                           || (insn_extract (p),
3580                               ! constrain_operands (INSN_CODE (p), 1))))
3581                     {
3582                       error_for_asm (insn,
3583                                      "`asm' operand requires impossible reload");
3584                       PUT_CODE (p, NOTE);
3585                       NOTE_SOURCE_FILE (p) = 0;
3586                       NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3587                     }
3588             }
3589           /* Any previously reloaded spilled pseudo reg, stored in this insn,
3590              is no longer validly lying around to save a future reload.
3591              Note that this does not detect pseudos that were reloaded
3592              for this insn in order to be stored in
3593              (obeying register constraints).  That is correct; such reload
3594              registers ARE still valid.  */
3595           note_stores (PATTERN (insn), forget_old_reloads_1);
3596
3597           /* There may have been CLOBBER insns placed after INSN.  So scan
3598              between INSN and NEXT and use them to forget old reloads.  */
3599           for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
3600             if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3601               note_stores (PATTERN (x), forget_old_reloads_1);
3602
3603 #ifdef AUTO_INC_DEC
3604           /* Likewise for regs altered by auto-increment in this insn.
3605              But note that the reg-notes are not changed by reloading:
3606              they still contain the pseudo-regs, not the spill regs.  */
3607           for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3608             if (REG_NOTE_KIND (x) == REG_INC)
3609               {
3610                 /* See if this pseudo reg was reloaded in this insn.
3611                    If so, its last-reload info is still valid
3612                    because it is based on this insn's reload.  */
3613                 for (i = 0; i < n_reloads; i++)
3614                   if (reload_out[i] == XEXP (x, 0))
3615                     break;
3616
3617                 if (i == n_reloads)
3618                   forget_old_reloads_1 (XEXP (x, 0));
3619               }
3620 #endif
3621         }
3622       /* A reload reg's contents are unknown after a label.  */
3623       if (GET_CODE (insn) == CODE_LABEL)
3624         for (i = 0; i < n_spills; i++)
3625           {
3626             reg_reloaded_contents[i] = -1;
3627             reg_reloaded_insn[i] = 0;
3628           }
3629
3630       /* Don't assume a reload reg is still good after a call insn
3631          if it is a call-used reg.  */
3632       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
3633         for (i = 0; i < n_spills; i++)
3634           if (call_used_regs[spill_regs[i]])
3635             {
3636               reg_reloaded_contents[i] = -1;
3637               reg_reloaded_insn[i] = 0;
3638             }
3639
3640       /* In case registers overlap, allow certain insns to invalidate
3641          particular hard registers.  */
3642
3643 #ifdef INSN_CLOBBERS_REGNO_P
3644       for (i = 0 ; i < n_spills ; i++)
3645         if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
3646           {
3647             reg_reloaded_contents[i] = -1;
3648             reg_reloaded_insn[i] = 0;
3649           }
3650 #endif
3651
3652       insn = next;
3653
3654 #ifdef USE_C_ALLOCA
3655       alloca (0);
3656 #endif
3657     }
3658 }
3659
3660 /* Discard all record of any value reloaded from X,
3661    or reloaded in X from someplace else;
3662    unless X is an output reload reg of the current insn.
3663
3664    X may be a hard reg (the reload reg)
3665    or it may be a pseudo reg that was reloaded from.  */
3666
3667 static void
3668 forget_old_reloads_1 (x)
3669      rtx x;
3670 {
3671   register int regno;
3672   int nr;
3673   int offset = 0;
3674
3675   /* note_stores does give us subregs of hard regs.  */
3676   while (GET_CODE (x) == SUBREG)
3677     {
3678       offset += SUBREG_WORD (x);
3679       x = SUBREG_REG (x);
3680     }
3681
3682   if (GET_CODE (x) != REG)
3683     return;
3684
3685   regno = REGNO (x) + offset;
3686
3687   if (regno >= FIRST_PSEUDO_REGISTER)
3688     nr = 1;
3689   else
3690     {
3691       int i;
3692       nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
3693       /* Storing into a spilled-reg invalidates its contents.
3694          This can happen if a block-local pseudo is allocated to that reg
3695          and it wasn't spilled because this block's total need is 0.
3696          Then some insn might have an optional reload and use this reg.  */
3697       for (i = 0; i < nr; i++)
3698         if (spill_reg_order[regno + i] >= 0
3699             /* But don't do this if the reg actually serves as an output
3700                reload reg in the current instruction.  */
3701             && (n_reloads == 0
3702                 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
3703           {
3704             reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
3705             reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
3706           }
3707     }
3708
3709   /* Since value of X has changed,
3710      forget any value previously copied from it.  */
3711
3712   while (nr-- > 0)
3713     /* But don't forget a copy if this is the output reload
3714        that establishes the copy's validity.  */
3715     if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
3716       reg_last_reload_reg[regno + nr] = 0;
3717 }
3718 \f
3719 /* For each reload, the mode of the reload register.  */
3720 static enum machine_mode reload_mode[MAX_RELOADS];
3721
3722 /* For each reload, the largest number of registers it will require.  */
3723 static int reload_nregs[MAX_RELOADS];
3724
3725 /* Comparison function for qsort to decide which of two reloads
3726    should be handled first.  *P1 and *P2 are the reload numbers.  */
3727
3728 static int
3729 reload_reg_class_lower (p1, p2)
3730      short *p1, *p2;
3731 {
3732   register int r1 = *p1, r2 = *p2;
3733   register int t;
3734
3735   /* Consider required reloads before optional ones.  */
3736   t = reload_optional[r1] - reload_optional[r2];
3737   if (t != 0)
3738     return t;
3739
3740   /* Count all solitary classes before non-solitary ones.  */
3741   t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
3742        - (reg_class_size[(int) reload_reg_class[r1]] == 1));
3743   if (t != 0)
3744     return t;
3745
3746   /* Aside from solitaires, consider all multi-reg groups first.  */
3747   t = reload_nregs[r2] - reload_nregs[r1];
3748   if (t != 0)
3749     return t;
3750
3751   /* Consider reloads in order of increasing reg-class number.  */
3752   t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
3753   if (t != 0)
3754     return t;
3755
3756   /* If reloads are equally urgent, sort by reload number,
3757      so that the results of qsort leave nothing to chance.  */
3758   return r1 - r2;
3759 }
3760 \f
3761 /* The following HARD_REG_SETs indicate when each hard register is
3762    used for a reload of various parts of the current insn.  */
3763
3764 /* If reg is in use as a reload reg for a RELOAD_OTHER reload.  */
3765 static HARD_REG_SET reload_reg_used;
3766 /* If reg is in use for a RELOAD_FOR_INPUT_RELOAD_ADDRESS reload.  */
3767 static HARD_REG_SET reload_reg_used_in_input_addr;
3768 /* If reg is in use for a RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reload.  */
3769 static HARD_REG_SET reload_reg_used_in_output_addr;
3770 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload.  */
3771 static HARD_REG_SET reload_reg_used_in_op_addr;
3772 /* If reg is in use for a RELOAD_FOR_INPUT reload.  */
3773 static HARD_REG_SET reload_reg_used_in_input;
3774 /* If reg is in use for a RELOAD_FOR_OUTPUT reload.  */
3775 static HARD_REG_SET reload_reg_used_in_output;
3776
3777 /* If reg is in use as a reload reg for any sort of reload.  */
3778 static HARD_REG_SET reload_reg_used_at_all;
3779
3780 /* Mark reg REGNO as in use for a reload of the sort spec'd by WHEN_NEEDED.
3781    MODE is used to indicate how many consecutive regs are actually used.  */
3782
3783 static void
3784 mark_reload_reg_in_use (regno, when_needed, mode)
3785      int regno;
3786      enum reload_when_needed when_needed;
3787      enum machine_mode mode;
3788 {
3789   int nregs = HARD_REGNO_NREGS (regno, mode);
3790   int i;
3791
3792   for (i = regno; i < nregs + regno; i++)
3793     {
3794       switch (when_needed)
3795         {
3796         case RELOAD_OTHER:
3797           SET_HARD_REG_BIT (reload_reg_used, i);
3798           break;
3799
3800         case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3801           SET_HARD_REG_BIT (reload_reg_used_in_input_addr, i);
3802           break;
3803
3804         case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3805           SET_HARD_REG_BIT (reload_reg_used_in_output_addr, i);
3806           break;
3807
3808         case RELOAD_FOR_OPERAND_ADDRESS:
3809           SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
3810           break;
3811
3812         case RELOAD_FOR_INPUT:
3813           SET_HARD_REG_BIT (reload_reg_used_in_input, i);
3814           break;
3815
3816         case RELOAD_FOR_OUTPUT:
3817           SET_HARD_REG_BIT (reload_reg_used_in_output, i);
3818           break;
3819         }
3820
3821       SET_HARD_REG_BIT (reload_reg_used_at_all, i);
3822     }
3823 }
3824
3825 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
3826    specified by WHEN_NEEDED.  */
3827
3828 static int
3829 reload_reg_free_p (regno, when_needed)
3830      int regno;
3831      enum reload_when_needed when_needed;
3832 {
3833   /* In use for a RELOAD_OTHER means it's not available for anything.  */
3834   if (TEST_HARD_REG_BIT (reload_reg_used, regno))
3835     return 0;
3836   switch (when_needed)
3837     {
3838     case RELOAD_OTHER:
3839       /* In use for anything means not available for a RELOAD_OTHER.  */
3840       return ! TEST_HARD_REG_BIT (reload_reg_used_at_all, regno);
3841
3842       /* The other kinds of use can sometimes share a register.  */
3843     case RELOAD_FOR_INPUT:
3844       return (! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3845               && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3846               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno));
3847     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3848       return (! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno)
3849               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno));
3850     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3851       return (! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3852               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3853     case RELOAD_FOR_OPERAND_ADDRESS:
3854       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3855               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3856               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3857     case RELOAD_FOR_OUTPUT:
3858       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3859               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3860               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3861     }
3862   abort ();
3863 }
3864
3865 /* Return 1 if the value in reload reg REGNO, as used by a reload
3866    needed for the part of the insn specified by WHEN_NEEDED,
3867    is not in use for a reload in any prior part of the insn.
3868
3869    We can assume that the reload reg was already tested for availability
3870    at the time it is needed, and we should not check this again,
3871    in case the reg has already been marked in use.  */
3872
3873 static int
3874 reload_reg_free_before_p (regno, when_needed)
3875      int regno;
3876      enum reload_when_needed when_needed;
3877 {
3878   switch (when_needed)
3879     {
3880     case RELOAD_OTHER:
3881       /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3882          its use starts from the beginning, so nothing can use it earlier.  */
3883       return 1;
3884
3885       /* If this use is for part of the insn,
3886          check the reg is not in use for any prior part.  */
3887     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3888       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
3889         return 0;
3890     case RELOAD_FOR_OUTPUT:
3891       if (TEST_HARD_REG_BIT (reload_reg_used_in_input, regno))
3892         return 0;
3893     case RELOAD_FOR_OPERAND_ADDRESS:
3894       if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno))
3895         return 0;
3896     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3897     case RELOAD_FOR_INPUT:
3898       return 1;
3899     }
3900   abort ();
3901 }
3902
3903 /* Return 1 if the value in reload reg REGNO, as used by a reload
3904    needed for the part of the insn specified by WHEN_NEEDED,
3905    is still available in REGNO at the end of the insn.
3906
3907    We can assume that the reload reg was already tested for availability
3908    at the time it is needed, and we should not check this again,
3909    in case the reg has already been marked in use.  */
3910
3911 static int
3912 reload_reg_reaches_end_p (regno, when_needed)
3913      int regno;
3914      enum reload_when_needed when_needed;
3915 {
3916   switch (when_needed)
3917     {
3918     case RELOAD_OTHER:
3919       /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3920          its value must reach the end.  */
3921       return 1;
3922
3923       /* If this use is for part of the insn,
3924          its value reaches if no subsequent part uses the same register.  */
3925     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3926     case RELOAD_FOR_INPUT:
3927       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3928           || TEST_HARD_REG_BIT (reload_reg_used_in_output, regno))
3929         return 0;
3930     case RELOAD_FOR_OPERAND_ADDRESS:
3931       if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno))
3932         return 0;
3933     case RELOAD_FOR_OUTPUT:
3934     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3935       return 1;
3936     }
3937   abort ();
3938 }
3939 \f
3940 /* Vector of reload-numbers showing the order in which the reloads should
3941    be processed.  */
3942 short reload_order[MAX_RELOADS];
3943
3944 /* Indexed by reload number, 1 if incoming value
3945    inherited from previous insns.  */
3946 char reload_inherited[MAX_RELOADS];
3947
3948 /* For an inherited reload, this is the insn the reload was inherited from,
3949    if we know it.  Otherwise, this is 0.  */
3950 rtx reload_inheritance_insn[MAX_RELOADS];
3951
3952 /* If non-zero, this is a place to get the value of the reload,
3953    rather than using reload_in.  */
3954 rtx reload_override_in[MAX_RELOADS];
3955
3956 /* For each reload, the index in spill_regs of the spill register used,
3957    or -1 if we did not need one of the spill registers for this reload.  */
3958 int reload_spill_index[MAX_RELOADS];
3959
3960 /* Index of last register assigned as a spill register.  We allocate in
3961    a round-robin fashio.  */
3962
3963 static last_spill_reg = 0;
3964
3965 /* Find a spill register to use as a reload register for reload R.
3966    LAST_RELOAD is non-zero if this is the last reload for the insn being
3967    processed.
3968
3969    Set reload_reg_rtx[R] to the register allocated.
3970
3971    If NOERROR is nonzero, we return 1 if successful,
3972    or 0 if we couldn't find a spill reg and we didn't change anything.  */
3973
3974 static int
3975 allocate_reload_reg (r, insn, last_reload, noerror)
3976      int r;
3977      rtx insn;
3978      int last_reload;
3979      int noerror;
3980 {
3981   int i;
3982   int pass;
3983   int count;
3984   rtx new;
3985   int regno;
3986
3987   /* If we put this reload ahead, thinking it is a group,
3988      then insist on finding a group.  Otherwise we can grab a
3989      reg that some other reload needs.
3990      (That can happen when we have a 68000 DATA_OR_FP_REG
3991      which is a group of data regs or one fp reg.)
3992      We need not be so restrictive if there are no more reloads
3993      for this insn.
3994
3995      ??? Really it would be nicer to have smarter handling
3996      for that kind of reg class, where a problem like this is normal.
3997      Perhaps those classes should be avoided for reloading
3998      by use of more alternatives.  */
3999
4000   int force_group = reload_nregs[r] > 1 && ! last_reload;
4001
4002   /* If we want a single register and haven't yet found one,
4003      take any reg in the right class and not in use.
4004      If we want a consecutive group, here is where we look for it.
4005
4006      We use two passes so we can first look for reload regs to
4007      reuse, which are already in use for other reloads in this insn,
4008      and only then use additional registers.
4009      I think that maximizing reuse is needed to make sure we don't
4010      run out of reload regs.  Suppose we have three reloads, and
4011      reloads A and B can share regs.  These need two regs.
4012      Suppose A and B are given different regs.
4013      That leaves none for C.  */
4014   for (pass = 0; pass < 2; pass++)
4015     {
4016       /* I is the index in spill_regs.
4017          We advance it round-robin between insns to use all spill regs
4018          equally, so that inherited reloads have a chance
4019          of leapfrogging each other.  */
4020
4021       for (count = 0, i = last_spill_reg; count < n_spills; count++)
4022         {
4023           int class = (int) reload_reg_class[r];
4024
4025           i = (i + 1) % n_spills;
4026
4027           if (reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4028               && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
4029               && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4030               /* Look first for regs to share, then for unshared.  */
4031               && (pass || TEST_HARD_REG_BIT (reload_reg_used_at_all,
4032                                              spill_regs[i])))
4033             {
4034               int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4035               /* Avoid the problem where spilling a GENERAL_OR_FP_REG
4036                  (on 68000) got us two FP regs.  If NR is 1,
4037                  we would reject both of them.  */
4038               if (force_group)
4039                 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
4040               /* If we need only one reg, we have already won.  */
4041               if (nr == 1)
4042                 {
4043                   /* But reject a single reg if we demand a group.  */
4044                   if (force_group)
4045                     continue;
4046                   break;
4047                 }
4048               /* Otherwise check that as many consecutive regs as we need
4049                  are available here.
4050                  Also, don't use for a group registers that are
4051                  needed for nongroups.  */
4052               if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
4053                 while (nr > 1)
4054                   {
4055                     regno = spill_regs[i] + nr - 1;
4056                     if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
4057                           && spill_reg_order[regno] >= 0
4058                           && reload_reg_free_p (regno, reload_when_needed[r])
4059                           && ! TEST_HARD_REG_BIT (counted_for_nongroups,
4060                                                   regno)))
4061                       break;
4062                     nr--;
4063                   }
4064               if (nr == 1)
4065                 break;
4066             }
4067         }
4068
4069       /* If we found something on pass 1, omit pass 2.  */
4070       if (count < n_spills)
4071         break;
4072     }
4073
4074   /* We should have found a spill register by now.  */
4075   if (count == n_spills)
4076     {
4077       if (noerror)
4078         return 0;
4079       goto failure;
4080     }
4081
4082   last_spill_reg = i;
4083
4084   /* Mark as in use for this insn the reload regs we use for this.  */
4085   mark_reload_reg_in_use (spill_regs[i], reload_when_needed[r],
4086                           reload_mode[r]);
4087
4088   new = spill_reg_rtx[i];
4089
4090   if (new == 0 || GET_MODE (new) != reload_mode[r])
4091     spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode[r], spill_regs[i]);
4092
4093   reload_reg_rtx[r] = new;
4094   reload_spill_index[r] = i;
4095   regno = true_regnum (new);
4096
4097   /* Detect when the reload reg can't hold the reload mode.
4098      This used to be one `if', but Sequent compiler can't handle that.  */
4099   if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4100     {
4101       enum machine_mode test_mode = VOIDmode;
4102       if (reload_in[r])
4103         test_mode = GET_MODE (reload_in[r]);
4104       /* If reload_in[r] has VOIDmode, it means we will load it
4105          in whatever mode the reload reg has: to wit, reload_mode[r].
4106          We have already tested that for validity.  */
4107       /* Aside from that, we need to test that the expressions
4108          to reload from or into have modes which are valid for this
4109          reload register.  Otherwise the reload insns would be invalid.  */
4110       if (! (reload_in[r] != 0 && test_mode != VOIDmode
4111              && ! HARD_REGNO_MODE_OK (regno, test_mode)))
4112         if (! (reload_out[r] != 0
4113                && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
4114           /* The reg is OK.  */
4115           return 1;
4116     }
4117
4118   /* The reg is not OK.  */
4119   if (noerror)
4120     return 0;
4121
4122  failure:
4123   if (asm_noperands (PATTERN (insn)) < 0)
4124     /* It's the compiler's fault.  */
4125     abort ();
4126
4127   /* It's the user's fault; the operand's mode and constraint
4128      don't match.  Disable this reload so we don't crash in final.  */
4129   error_for_asm (insn,
4130                  "`asm' operand constraint incompatible with operand size");
4131   reload_in[r] = 0;
4132   reload_out[r] = 0;
4133   reload_reg_rtx[r] = 0;
4134   reload_optional[r] = 1;
4135   reload_secondary_p[r] = 1;
4136
4137   return 1;
4138 }
4139 \f
4140 /* Assign hard reg targets for the pseudo-registers we must reload
4141    into hard regs for this insn.
4142    Also output the instructions to copy them in and out of the hard regs.
4143
4144    For machines with register classes, we are responsible for
4145    finding a reload reg in the proper class.  */
4146
4147 static void
4148 choose_reload_regs (insn, avoid_return_reg)
4149      rtx insn;
4150      /* This argument is currently ignored.  */
4151      rtx avoid_return_reg;
4152 {
4153   register int i, j;
4154   int max_group_size = 1;
4155   enum reg_class group_class = NO_REGS;
4156   int inheritance;
4157
4158   rtx save_reload_reg_rtx[MAX_RELOADS];
4159   char save_reload_inherited[MAX_RELOADS];
4160   rtx save_reload_inheritance_insn[MAX_RELOADS];
4161   rtx save_reload_override_in[MAX_RELOADS];
4162   int save_reload_spill_index[MAX_RELOADS];
4163   HARD_REG_SET save_reload_reg_used;
4164   HARD_REG_SET save_reload_reg_used_in_input_addr;
4165   HARD_REG_SET save_reload_reg_used_in_output_addr;
4166   HARD_REG_SET save_reload_reg_used_in_op_addr;
4167   HARD_REG_SET save_reload_reg_used_in_input;
4168   HARD_REG_SET save_reload_reg_used_in_output;
4169   HARD_REG_SET save_reload_reg_used_at_all;
4170
4171   bzero (reload_inherited, MAX_RELOADS);
4172   bzero (reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
4173   bzero (reload_override_in, MAX_RELOADS * sizeof (rtx));
4174
4175   CLEAR_HARD_REG_SET (reload_reg_used);
4176   CLEAR_HARD_REG_SET (reload_reg_used_at_all);
4177   CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr);
4178   CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr);
4179   CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
4180   CLEAR_HARD_REG_SET (reload_reg_used_in_output);
4181   CLEAR_HARD_REG_SET (reload_reg_used_in_input);
4182
4183   /* Distinguish output-only and input-only reloads
4184      because they can overlap with other things.  */
4185   for (j = 0; j < n_reloads; j++)
4186     if (reload_when_needed[j] == RELOAD_OTHER
4187         && ! reload_needed_for_multiple[j])
4188       {
4189         if (reload_in[j] == 0 && reload_out[j] != 0)
4190           {
4191             /* But earlyclobber operands must stay as RELOAD_OTHER.  */
4192             for (i = 0; i < n_earlyclobbers; i++)
4193               {
4194                 if (GET_CODE (reload_earlyclobbers[i]) == SUBREG
4195                     && reg_overlap_mentioned_for_reload_p (reload_out[j],
4196                                     SUBREG_REG (reload_earlyclobbers[i])))
4197                   break;
4198                 if (rtx_equal_p (reload_out[j], reload_earlyclobbers[i]))
4199                   break;
4200               }
4201             if (i == n_earlyclobbers)
4202               reload_when_needed[j] = RELOAD_FOR_OUTPUT;
4203           }
4204         if (reload_out[j] == 0)
4205           reload_when_needed[j] = RELOAD_FOR_INPUT;
4206
4207         if (reload_secondary_reload[j] >= 0
4208             && ! reload_needed_for_multiple[reload_secondary_reload[j]])
4209           reload_when_needed[reload_secondary_reload[j]]
4210             = reload_when_needed[j];
4211       }
4212
4213 #ifdef SMALL_REGISTER_CLASSES
4214   /* Don't bother with avoiding the return reg
4215      if we have no mandatory reload that could use it.  */
4216   if (avoid_return_reg)
4217     {
4218       int do_avoid = 0;
4219       int regno = REGNO (avoid_return_reg);
4220       int nregs
4221         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4222       int r;
4223
4224       for (r = regno; r < regno + nregs; r++)
4225         if (spill_reg_order[r] >= 0)
4226           for (j = 0; j < n_reloads; j++)
4227             if (!reload_optional[j] && reload_reg_rtx[j] == 0
4228                 && (reload_in[j] != 0 || reload_out[j] != 0
4229                     || reload_secondary_p[j])
4230                 &&
4231                 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
4232               do_avoid = 1;
4233       if (!do_avoid)
4234         avoid_return_reg = 0;
4235     }
4236 #endif /* SMALL_REGISTER_CLASSES */
4237
4238 #if 0  /* Not needed, now that we can always retry without inheritance.  */
4239   /* See if we have more mandatory reloads than spill regs.
4240      If so, then we cannot risk optimizations that could prevent
4241      reloads from sharing one spill register.
4242
4243      Since we will try finding a better register than reload_reg_rtx
4244      unless it is equal to reload_in or reload_out, count such reloads.  */
4245
4246   {
4247     int tem = 0;
4248 #ifdef SMALL_REGISTER_CLASSES
4249     int tem = (avoid_return_reg != 0);
4250 #endif
4251     for (j = 0; j < n_reloads; j++)
4252       if (! reload_optional[j]
4253           && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
4254           && (reload_reg_rtx[j] == 0
4255               || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
4256                   && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
4257         tem++;
4258     if (tem > n_spills)
4259       must_reuse = 1;
4260   }
4261 #endif
4262
4263 #ifdef SMALL_REGISTER_CLASSES
4264   /* Don't use the subroutine call return reg for a reload
4265      if we are supposed to avoid it.  */
4266   if (avoid_return_reg)
4267     {
4268       int regno = REGNO (avoid_return_reg);
4269       int nregs
4270         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4271       int r;
4272
4273       for (r = regno; r < regno + nregs; r++)
4274         if (spill_reg_order[r] >= 0)
4275           SET_HARD_REG_BIT (reload_reg_used, r);
4276     }
4277 #endif /* SMALL_REGISTER_CLASSES */
4278
4279   /* In order to be certain of getting the registers we need,
4280      we must sort the reloads into order of increasing register class.
4281      Then our grabbing of reload registers will parallel the process
4282      that provided the reload registers.
4283
4284      Also note whether any of the reloads wants a consecutive group of regs.
4285      If so, record the maximum size of the group desired and what
4286      register class contains all the groups needed by this insn.  */
4287
4288   for (j = 0; j < n_reloads; j++)
4289     {
4290       reload_order[j] = j;
4291       reload_spill_index[j] = -1;
4292
4293       reload_mode[j]
4294         = (reload_strict_low[j] && reload_out[j]
4295            ? GET_MODE (SUBREG_REG (reload_out[j]))
4296            : (reload_inmode[j] == VOIDmode
4297               || (GET_MODE_SIZE (reload_outmode[j])
4298                   > GET_MODE_SIZE (reload_inmode[j])))
4299            ? reload_outmode[j] : reload_inmode[j]);
4300
4301       reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
4302
4303       if (reload_nregs[j] > 1)
4304         {
4305           max_group_size = MAX (reload_nregs[j], max_group_size);
4306           group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
4307         }
4308
4309       /* If we have already decided to use a certain register,
4310          don't use it in another way.  */
4311       if (reload_reg_rtx[j])
4312         mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]),
4313                                 reload_when_needed[j], reload_mode[j]);
4314     }
4315
4316   if (n_reloads > 1)
4317     qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
4318
4319   bcopy (reload_reg_rtx, save_reload_reg_rtx, sizeof reload_reg_rtx);
4320   bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
4321   bcopy (reload_inheritance_insn, save_reload_inheritance_insn,
4322          sizeof reload_inheritance_insn);
4323   bcopy (reload_override_in, save_reload_override_in,
4324          sizeof reload_override_in);
4325   bcopy (reload_spill_index, save_reload_spill_index,
4326          sizeof reload_spill_index);
4327   COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
4328   COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
4329   COPY_HARD_REG_SET (save_reload_reg_used_in_output,
4330                      reload_reg_used_in_output);
4331   COPY_HARD_REG_SET (save_reload_reg_used_in_input,
4332                      reload_reg_used_in_input);
4333   COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr,
4334                      reload_reg_used_in_input_addr);
4335   COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr,
4336                      reload_reg_used_in_output_addr);
4337   COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
4338                      reload_reg_used_in_op_addr);
4339
4340   /* If -O, try first with inheritance, then turning it off.
4341      If not -O, don't do inheritance.
4342      Using inheritance when not optimizing leads to paradoxes
4343      with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
4344      because one side of the comparison might be inherited.  */
4345
4346   for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
4347     {
4348       /* Process the reloads in order of preference just found.
4349          Beyond this point, subregs can be found in reload_reg_rtx.
4350
4351          This used to look for an existing reloaded home for all
4352          of the reloads, and only then perform any new reloads.
4353          But that could lose if the reloads were done out of reg-class order
4354          because a later reload with a looser constraint might have an old
4355          home in a register needed by an earlier reload with a tighter constraint.
4356
4357          To solve this, we make two passes over the reloads, in the order
4358          described above.  In the first pass we try to inherit a reload
4359          from a previous insn.  If there is a later reload that needs a
4360          class that is a proper subset of the class being processed, we must
4361          also allocate a spill register during the first pass.
4362
4363          Then make a second pass over the reloads to allocate any reloads
4364          that haven't been given registers yet.  */
4365
4366       for (j = 0; j < n_reloads; j++)
4367         {
4368           register int r = reload_order[j];
4369
4370           /* Ignore reloads that got marked inoperative.  */
4371           if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4372             continue;
4373
4374           /* If find_reloads chose a to use reload_in or reload_out as a reload
4375              register, we don't need to chose one.  Otherwise, try even if it found
4376              one since we might save an insn if we find the value lying around.  */
4377           if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
4378               && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
4379                   || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
4380             continue;
4381
4382 #if 0 /* No longer needed for correct operation.
4383          It might give better code, or might not; worth an experiment?  */
4384           /* If this is an optional reload, we can't inherit from earlier insns
4385              until we are sure that any non-optional reloads have been allocated.
4386              The following code takes advantage of the fact that optional reloads
4387              are at the end of reload_order.  */
4388           if (reload_optional[r] != 0)
4389             for (i = 0; i < j; i++)
4390               if ((reload_out[reload_order[i]] != 0
4391                    || reload_in[reload_order[i]] != 0
4392                    || reload_secondary_p[reload_order[i]])
4393                   && ! reload_optional[reload_order[i]]
4394                   && reload_reg_rtx[reload_order[i]] == 0)
4395                 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
4396 #endif
4397
4398           /* First see if this pseudo is already available as reloaded
4399              for a previous insn.  We cannot try to inherit for reloads
4400              that are smaller than the maximum number of registers needed
4401              for groups unless the register we would allocate cannot be used
4402              for the groups.
4403
4404              We could check here to see if this is a secondary reload for
4405              an object that is already in a register of the desired class.
4406              This would avoid the need for the secondary reload register.
4407              But this is complex because we can't easily determine what
4408              objects might want to be loaded via this reload.  So let a register
4409              be allocated here.  In `emit_reload_insns' we suppress one of the
4410              loads in the case described above.  */
4411
4412           if (inheritance)
4413             {
4414               register int regno = -1;
4415               enum machine_mode mode;
4416
4417               if (reload_in[r] == 0)
4418                 ;
4419               else if (GET_CODE (reload_in[r]) == REG)
4420                 {
4421                   regno = REGNO (reload_in[r]);
4422                   mode = GET_MODE (reload_in[r]);
4423                 }
4424               else if (GET_CODE (reload_in_reg[r]) == REG)
4425                 {
4426                   regno = REGNO (reload_in_reg[r]);
4427                   mode = GET_MODE (reload_in_reg[r]);
4428                 }
4429 #if 0
4430               /* This won't work, since REGNO can be a pseudo reg number.
4431                  Also, it takes much more hair to keep track of all the things
4432                  that can invalidate an inherited reload of part of a pseudoreg.  */
4433               else if (GET_CODE (reload_in[r]) == SUBREG
4434                        && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
4435                 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
4436 #endif
4437
4438               if (regno >= 0 && reg_last_reload_reg[regno] != 0)
4439                 {
4440                   i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
4441
4442                   if (reg_reloaded_contents[i] == regno
4443                       && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
4444                           >= GET_MODE_SIZE (mode))
4445                       && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4446                       && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4447                                             spill_regs[i])
4448                       && (reload_nregs[r] == max_group_size
4449                           || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
4450                                                   spill_regs[i]))
4451                       && reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4452                       && reload_reg_free_before_p (spill_regs[i],
4453                                                    reload_when_needed[r]))
4454                     {
4455                       /* If a group is needed, verify that all the subsequent
4456                          registers still have their values intact. */
4457                       int nr
4458                         = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4459                       int k;
4460
4461                       for (k = 1; k < nr; k++)
4462                         if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
4463                             != regno)
4464                           break;
4465
4466                       if (k == nr)
4467                         {
4468                           /* Mark the register as in use for this part of
4469                              the insn.  */
4470                           mark_reload_reg_in_use (spill_regs[i],
4471                                                   reload_when_needed[r],
4472                                                   reload_mode[r]);
4473                           reload_reg_rtx[r] = reg_last_reload_reg[regno];
4474                           reload_inherited[r] = 1;
4475                           reload_inheritance_insn[r] = reg_reloaded_insn[i];
4476                           reload_spill_index[r] = i;
4477                         }
4478                     }
4479                 }
4480             }
4481
4482           /* Here's another way to see if the value is already lying around.  */
4483           if (inheritance
4484               && reload_in[r] != 0
4485               && ! reload_inherited[r]
4486               && reload_out[r] == 0
4487               && (CONSTANT_P (reload_in[r])
4488                   || GET_CODE (reload_in[r]) == PLUS
4489                   || GET_CODE (reload_in[r]) == REG
4490                   || GET_CODE (reload_in[r]) == MEM)
4491               && (reload_nregs[r] == max_group_size
4492                   || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
4493             {
4494               register rtx equiv
4495                 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
4496                                   -1, NULL_PTR, 0, reload_mode[r]);
4497               int regno;
4498
4499               if (equiv != 0)
4500                 {
4501                   if (GET_CODE (equiv) == REG)
4502                     regno = REGNO (equiv);
4503                   else if (GET_CODE (equiv) == SUBREG)
4504                     {
4505                       regno = REGNO (SUBREG_REG (equiv));
4506                       if (regno < FIRST_PSEUDO_REGISTER)
4507                         regno += SUBREG_WORD (equiv);
4508                     }
4509                   else
4510                     abort ();
4511                 }
4512
4513               /* If we found a spill reg, reject it unless it is free
4514                  and of the desired class.  */
4515               if (equiv != 0
4516                   && ((spill_reg_order[regno] >= 0
4517                        && ! reload_reg_free_before_p (regno,
4518                                                       reload_when_needed[r]))
4519                       || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4520                                               regno)))
4521                 equiv = 0;
4522
4523               if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
4524                 equiv = 0;
4525
4526               if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4527                 equiv = 0;
4528
4529               /* We found a register that contains the value we need.
4530                  If this register is the same as an `earlyclobber' operand
4531                  of the current insn, just mark it as a place to reload from
4532                  since we can't use it as the reload register itself.  */
4533
4534               if (equiv != 0)
4535                 for (i = 0; i < n_earlyclobbers; i++)
4536                   if (reg_overlap_mentioned_for_reload_p (equiv,
4537                                                           reload_earlyclobbers[i]))
4538                     {
4539                       reload_override_in[r] = equiv;
4540                       equiv = 0;
4541                       break;
4542                     }
4543
4544               /* JRV: If the equiv register we have found is explicitly
4545                  clobbered in the current insn, mark but don't use, as above. */
4546
4547               if (equiv != 0 && regno_clobbered_p (regno, insn))
4548                 {
4549                   reload_override_in[r] = equiv;
4550                   equiv = 0;
4551                 }
4552
4553               /* If we found an equivalent reg, say no code need be generated
4554                  to load it, and use it as our reload reg.  */
4555               if (equiv != 0 && regno != FRAME_POINTER_REGNUM)
4556                 {
4557                   reload_reg_rtx[r] = equiv;
4558                   reload_inherited[r] = 1;
4559                   /* If it is a spill reg,
4560                      mark the spill reg as in use for this insn.  */
4561                   i = spill_reg_order[regno];
4562                   if (i >= 0)
4563                     mark_reload_reg_in_use (regno, reload_when_needed[r],
4564                                             reload_mode[r]);
4565                 }
4566             }
4567
4568           /* If we found a register to use already, or if this is an optional
4569              reload, we are done.  */
4570           if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
4571             continue;
4572
4573 #if 0 /* No longer needed for correct operation.  Might or might not
4574          give better code on the average.  Want to experiment?  */
4575
4576           /* See if there is a later reload that has a class different from our
4577              class that intersects our class or that requires less register
4578              than our reload.  If so, we must allocate a register to this
4579              reload now, since that reload might inherit a previous reload
4580              and take the only available register in our class.  Don't do this
4581              for optional reloads since they will force all previous reloads
4582              to be allocated.  Also don't do this for reloads that have been
4583              turned off.  */
4584
4585           for (i = j + 1; i < n_reloads; i++)
4586             {
4587               int s = reload_order[i];
4588
4589               if ((reload_in[s] == 0 && reload_out[s] == 0
4590                    && ! reload_secondary_p[s])
4591                   || reload_optional[s])
4592                 continue;
4593
4594               if ((reload_reg_class[s] != reload_reg_class[r]
4595                    && reg_classes_intersect_p (reload_reg_class[r],
4596                                                reload_reg_class[s]))
4597                   || reload_nregs[s] < reload_nregs[r])
4598               break;
4599             }
4600
4601           if (i == n_reloads)
4602             continue;
4603
4604           allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
4605 #endif
4606         }
4607
4608       /* Now allocate reload registers for anything non-optional that
4609          didn't get one yet.  */
4610       for (j = 0; j < n_reloads; j++)
4611         {
4612           register int r = reload_order[j];
4613
4614           /* Ignore reloads that got marked inoperative.  */
4615           if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4616             continue;
4617
4618           /* Skip reloads that already have a register allocated or are
4619              optional. */
4620           if (reload_reg_rtx[r] != 0 || reload_optional[r])
4621             continue;
4622
4623           if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
4624             break;
4625         }
4626
4627       /* If that loop got all the way, we have won.  */
4628       if (j == n_reloads)
4629         break;
4630
4631     fail:
4632       /* Loop around and try without any inheritance.  */
4633       /* First undo everything done by the failed attempt
4634          to allocate with inheritance.  */
4635       bcopy (save_reload_reg_rtx, reload_reg_rtx, sizeof reload_reg_rtx);
4636       bcopy (save_reload_inherited, reload_inherited, sizeof reload_inherited);
4637       bcopy (save_reload_inheritance_insn, reload_inheritance_insn,
4638              sizeof reload_inheritance_insn);
4639       bcopy (save_reload_override_in, reload_override_in,
4640              sizeof reload_override_in);
4641       bcopy (save_reload_spill_index, reload_spill_index,
4642              sizeof reload_spill_index);
4643       COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
4644       COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
4645       COPY_HARD_REG_SET (reload_reg_used_in_input,
4646                          save_reload_reg_used_in_input);
4647       COPY_HARD_REG_SET (reload_reg_used_in_output,
4648                          save_reload_reg_used_in_output);
4649       COPY_HARD_REG_SET (reload_reg_used_in_input_addr,
4650                          save_reload_reg_used_in_input_addr);
4651       COPY_HARD_REG_SET (reload_reg_used_in_output_addr,
4652                          save_reload_reg_used_in_output_addr);
4653       COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
4654                          save_reload_reg_used_in_op_addr);
4655     }
4656
4657   /* If we thought we could inherit a reload, because it seemed that
4658      nothing else wanted the same reload register earlier in the insn,
4659      verify that assumption, now that all reloads have been assigned.  */
4660
4661   for (j = 0; j < n_reloads; j++)
4662     {
4663       register int r = reload_order[j];
4664
4665       if (reload_inherited[r] && reload_reg_rtx[r] != 0
4666           && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
4667                                          reload_when_needed[r]))
4668         reload_inherited[r] = 0;
4669
4670       /* If we found a better place to reload from,
4671          validate it in the same fashion, if it is a reload reg.  */
4672       if (reload_override_in[r]
4673           && (GET_CODE (reload_override_in[r]) == REG
4674               || GET_CODE (reload_override_in[r]) == SUBREG))
4675         {
4676           int regno = true_regnum (reload_override_in[r]);
4677           if (spill_reg_order[regno] >= 0
4678               && ! reload_reg_free_before_p (regno, reload_when_needed[r]))
4679             reload_override_in[r] = 0;
4680         }
4681     }
4682
4683   /* Now that reload_override_in is known valid,
4684      actually override reload_in.  */
4685   for (j = 0; j < n_reloads; j++)
4686     if (reload_override_in[j])
4687       reload_in[j] = reload_override_in[j];
4688
4689   /* If this reload won't be done because it has been cancelled or is
4690      optional and not inherited, clear reload_reg_rtx so other
4691      routines (such as subst_reloads) don't get confused.  */
4692   for (j = 0; j < n_reloads; j++)
4693     if ((reload_optional[j] && ! reload_inherited[j])
4694         || (reload_in[j] == 0 && reload_out[j] == 0
4695             && ! reload_secondary_p[j]))
4696       reload_reg_rtx[j] = 0;
4697
4698   /* Record which pseudos and which spill regs have output reloads.  */
4699   for (j = 0; j < n_reloads; j++)
4700     {
4701       register int r = reload_order[j];
4702
4703       i = reload_spill_index[r];
4704
4705       /* I is nonneg if this reload used one of the spill regs.
4706          If reload_reg_rtx[r] is 0, this is an optional reload
4707          that we opted to ignore.  */
4708       if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
4709           && reload_reg_rtx[r] != 0)
4710         {
4711           register int nregno = REGNO (reload_out[r]);
4712           int nr = 1;
4713
4714           if (nregno < FIRST_PSEUDO_REGISTER)
4715             nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
4716
4717           while (--nr >= 0)
4718             reg_has_output_reload[nregno + nr] = 1;
4719
4720           if (i >= 0)
4721             {
4722               nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4723               while (--nr >= 0)
4724                 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
4725             }
4726
4727           if (reload_when_needed[r] != RELOAD_OTHER
4728               && reload_when_needed[r] != RELOAD_FOR_OUTPUT)
4729             abort ();
4730         }
4731     }
4732 }
4733 \f
4734 /* Output insns to reload values in and out of the chosen reload regs.  */
4735
4736 static void
4737 emit_reload_insns (insn)
4738      rtx insn;
4739 {
4740   register int j;
4741   rtx following_insn = NEXT_INSN (insn);
4742   rtx before_insn = insn;
4743   rtx first_output_reload_insn = NEXT_INSN (insn);
4744   rtx first_other_reload_insn = insn;
4745   rtx first_operand_address_reload_insn = insn;
4746   int special;
4747   /* Values to be put in spill_reg_store are put here first.  */
4748   rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
4749
4750   /* If this is a CALL_INSN preceded by USE insns, any reload insns
4751      must go in front of the first USE insn, not in front of INSN.  */
4752
4753   if (GET_CODE (insn) == CALL_INSN && GET_CODE (PREV_INSN (insn)) == INSN
4754       && GET_CODE (PATTERN (PREV_INSN (insn))) == USE)
4755     while (GET_CODE (PREV_INSN (before_insn)) == INSN
4756            && GET_CODE (PATTERN (PREV_INSN (before_insn))) == USE)
4757       first_other_reload_insn = first_operand_address_reload_insn
4758         = before_insn = PREV_INSN (before_insn);
4759
4760   /* Now output the instructions to copy the data into and out of the
4761      reload registers.  Do these in the order that the reloads were reported,
4762      since reloads of base and index registers precede reloads of operands
4763      and the operands may need the base and index registers reloaded.  */
4764
4765   for (j = 0; j < n_reloads; j++)
4766     {
4767       register rtx old;
4768       rtx oldequiv_reg = 0;
4769       rtx this_reload_insn = 0;
4770       rtx store_insn = 0;
4771
4772       old = reload_in[j];
4773       if (old != 0 && ! reload_inherited[j]
4774           && ! rtx_equal_p (reload_reg_rtx[j], old)
4775           && reload_reg_rtx[j] != 0)
4776         {
4777           register rtx reloadreg = reload_reg_rtx[j];
4778           rtx oldequiv = 0;
4779           enum machine_mode mode;
4780           rtx where;
4781           rtx reload_insn;
4782
4783           /* Determine the mode to reload in.
4784              This is very tricky because we have three to choose from.
4785              There is the mode the insn operand wants (reload_inmode[J]).
4786              There is the mode of the reload register RELOADREG.
4787              There is the intrinsic mode of the operand, which we could find
4788              by stripping some SUBREGs.
4789              It turns out that RELOADREG's mode is irrelevant:
4790              we can change that arbitrarily.
4791
4792              Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
4793              then the reload reg may not support QImode moves, so use SImode.
4794              If foo is in memory due to spilling a pseudo reg, this is safe,
4795              because the QImode value is in the least significant part of a
4796              slot big enough for a SImode.  If foo is some other sort of
4797              memory reference, then it is impossible to reload this case,
4798              so previous passes had better make sure this never happens.
4799
4800              Then consider a one-word union which has SImode and one of its
4801              members is a float, being fetched as (SUBREG:SF union:SI).
4802              We must fetch that as SFmode because we could be loading into
4803              a float-only register.  In this case OLD's mode is correct.
4804
4805              Consider an immediate integer: it has VOIDmode.  Here we need
4806              to get a mode from something else.
4807
4808              In some cases, there is a fourth mode, the operand's
4809              containing mode.  If the insn specifies a containing mode for
4810              this operand, it overrides all others.
4811
4812              I am not sure whether the algorithm here is always right,
4813              but it does the right things in those cases.  */
4814
4815           mode = GET_MODE (old);
4816           if (mode == VOIDmode)
4817             mode = reload_inmode[j];
4818           if (reload_strict_low[j])
4819             mode = GET_MODE (SUBREG_REG (reload_in[j]));
4820
4821 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4822           /* If we need a secondary register for this operation, see if
4823              the value is already in a register in that class.  Don't
4824              do this if the secondary register will be used as a scratch
4825              register.  */
4826
4827           if (reload_secondary_reload[j] >= 0
4828               && reload_secondary_icode[j] == CODE_FOR_nothing
4829               && optimize)
4830             oldequiv
4831               = find_equiv_reg (old, insn,
4832                                 reload_reg_class[reload_secondary_reload[j]],
4833                                 -1, NULL_PTR, 0, mode);
4834 #endif
4835
4836           /* If reloading from memory, see if there is a register
4837              that already holds the same value.  If so, reload from there.
4838              We can pass 0 as the reload_reg_p argument because
4839              any other reload has either already been emitted,
4840              in which case find_equiv_reg will see the reload-insn,
4841              or has yet to be emitted, in which case it doesn't matter
4842              because we will use this equiv reg right away.  */
4843
4844           if (oldequiv == 0 && optimize
4845               && (GET_CODE (old) == MEM
4846                   || (GET_CODE (old) == REG
4847                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
4848                       && reg_renumber[REGNO (old)] < 0)))
4849             oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
4850                                        -1, NULL_PTR, 0, mode);
4851
4852           if (oldequiv)
4853             {
4854               int regno = true_regnum (oldequiv);
4855
4856               /* If OLDEQUIV is a spill register, don't use it for this
4857                  if any other reload needs it at an earlier stage of this insn
4858                  or at this stage.  */
4859               if (spill_reg_order[regno] >= 0
4860                   && (! reload_reg_free_p (regno, reload_when_needed[j])
4861                       || ! reload_reg_free_before_p (regno,
4862                                                      reload_when_needed[j])))
4863                 oldequiv = 0;
4864
4865               /* If OLDEQUIV is not a spill register,
4866                  don't use it if any other reload wants it.  */
4867               if (spill_reg_order[regno] < 0)
4868                 {
4869                   int k;
4870                   for (k = 0; k < n_reloads; k++)
4871                     if (reload_reg_rtx[k] != 0 && k != j
4872                         && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
4873                                                                oldequiv))
4874                       {
4875                         oldequiv = 0;
4876                         break;
4877                       }
4878                 }
4879             }
4880
4881           if (oldequiv == 0)
4882             oldequiv = old;
4883           else if (GET_CODE (oldequiv) == REG)
4884             oldequiv_reg = oldequiv;
4885           else if (GET_CODE (oldequiv) == SUBREG)
4886             oldequiv_reg = SUBREG_REG (oldequiv);
4887
4888           /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
4889              then load RELOADREG from OLDEQUIV.  */
4890
4891           if (GET_MODE (reloadreg) != mode)
4892             reloadreg = gen_lowpart_common (mode, reloadreg);
4893           while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
4894             oldequiv = SUBREG_REG (oldequiv);
4895           if (GET_MODE (oldequiv) != VOIDmode
4896               && mode != GET_MODE (oldequiv))
4897             oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
4898
4899           /* Decide where to put reload insn for this reload.  */
4900           switch (reload_when_needed[j])
4901             {
4902             case RELOAD_FOR_INPUT:
4903             case RELOAD_OTHER:
4904               where = first_operand_address_reload_insn;
4905               break;
4906             case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
4907               where = first_other_reload_insn;
4908               break;
4909             case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
4910               where = first_output_reload_insn;
4911               break;
4912             case RELOAD_FOR_OPERAND_ADDRESS:
4913               where = before_insn;
4914             }
4915
4916           special = 0;
4917
4918           /* Auto-increment addresses must be reloaded in a special way.  */
4919           if (GET_CODE (oldequiv) == POST_INC
4920               || GET_CODE (oldequiv) == POST_DEC
4921               || GET_CODE (oldequiv) == PRE_INC
4922               || GET_CODE (oldequiv) == PRE_DEC)
4923             {
4924               /* We are not going to bother supporting the case where a
4925                  incremented register can't be copied directly from
4926                  OLDEQUIV since this seems highly unlikely.  */
4927               if (reload_secondary_reload[j] >= 0)
4928                 abort ();
4929               /* Prevent normal processing of this reload.  */
4930               special = 1;
4931               /* Output a special code sequence for this case.  */
4932               this_reload_insn
4933                 = inc_for_reload (reloadreg, oldequiv, reload_inc[j], where);
4934             }
4935
4936           /* If we are reloading a pseudo-register that was set by the previous
4937              insn, see if we can get rid of that pseudo-register entirely
4938              by redirecting the previous insn into our reload register.  */
4939
4940           else if (optimize && GET_CODE (old) == REG
4941                    && REGNO (old) >= FIRST_PSEUDO_REGISTER
4942                    && dead_or_set_p (insn, old)
4943                    /* This is unsafe if some other reload
4944                       uses the same reg first.  */
4945                    && (reload_when_needed[j] == RELOAD_OTHER
4946                        || reload_when_needed[j] == RELOAD_FOR_INPUT
4947                        || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS))
4948             {
4949               rtx temp = PREV_INSN (insn);
4950               while (temp && GET_CODE (temp) == NOTE)
4951                 temp = PREV_INSN (temp);
4952               if (temp
4953                   && GET_CODE (temp) == INSN
4954                   && GET_CODE (PATTERN (temp)) == SET
4955                   && SET_DEST (PATTERN (temp)) == old
4956                   /* Make sure we can access insn_operand_constraint.  */
4957                   && asm_noperands (PATTERN (temp)) < 0
4958                   /* This is unsafe if prev insn rejects our reload reg.  */
4959                   && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
4960                                                reloadreg)
4961                   /* This is unsafe if operand occurs more than once in current
4962                      insn.  Perhaps some occurrences aren't reloaded.  */
4963                   && count_occurrences (PATTERN (insn), old) == 1
4964                   /* Don't risk splitting a matching pair of operands.  */
4965                   && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
4966                 {
4967                   /* Store into the reload register instead of the pseudo.  */
4968                   SET_DEST (PATTERN (temp)) = reloadreg;
4969                   /* If these are the only uses of the pseudo reg,
4970                      pretend for GDB it lives in the reload reg we used.  */
4971                   if (reg_n_deaths[REGNO (old)] == 1
4972                       && reg_n_sets[REGNO (old)] == 1)
4973                     {
4974                       reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
4975                       alter_reg (REGNO (old), -1);
4976                     }
4977                   special = 1;
4978                 }
4979             }
4980
4981           /* We can't do that, so output an insn to load RELOADREG.
4982              Keep them in the following order:
4983              all reloads for input reload addresses,
4984              all reloads for ordinary input operands,
4985              all reloads for addresses of non-reloaded operands,
4986              the insn being reloaded,
4987              all reloads for addresses of output reloads,
4988              the output reloads.  */
4989           if (! special)
4990             {
4991 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4992               rtx second_reload_reg = 0;
4993               enum insn_code icode;
4994
4995               /* If we have a secondary reload, pick up the secondary register
4996                  and icode, if any.  If OLDEQUIV and OLD are different or
4997                  if this is an in-out reload, recompute whether or not we
4998                  still need a secondary register and what the icode should
4999                  be.  If we still need a secondary register and the class or
5000                  icode is different, go back to reloading from OLD if using
5001                  OLDEQUIV means that we got the wrong type of register.  We
5002                  cannot have different class or icode due to an in-out reload
5003                  because we don't make such reloads when both the input and
5004                  output need secondary reload registers.  */
5005
5006               if (reload_secondary_reload[j] >= 0)
5007                 {
5008                   int secondary_reload = reload_secondary_reload[j];
5009                   rtx real_oldequiv = oldequiv;
5010                   rtx real_old = old;
5011
5012                   /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
5013                      and similarly for OLD.
5014                      See comments in find_secondary_reload in reload.c.  */
5015                   if (GET_CODE (oldequiv) == REG
5016                       && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
5017                       && reg_equiv_mem[REGNO (oldequiv)] != 0)
5018                     real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
5019
5020                   if (GET_CODE (old) == REG
5021                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
5022                       && reg_equiv_mem[REGNO (old)] != 0)
5023                     real_old = reg_equiv_mem[REGNO (old)];
5024
5025                   second_reload_reg = reload_reg_rtx[secondary_reload];
5026                   icode = reload_secondary_icode[j];
5027
5028                   if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
5029                       || (reload_in[j] != 0 && reload_out[j] != 0))
5030                     {
5031                       enum reg_class new_class
5032                         = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
5033                                                         mode, real_oldequiv);
5034
5035                       if (new_class == NO_REGS)
5036                         second_reload_reg = 0;
5037                       else
5038                         {
5039                           enum insn_code new_icode;
5040                           enum machine_mode new_mode;
5041
5042                           if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
5043                                                    REGNO (second_reload_reg)))
5044                             oldequiv = old, real_oldequiv = real_old;
5045                           else
5046                             {
5047                               new_icode = reload_in_optab[(int) mode];
5048                               if (new_icode != CODE_FOR_nothing
5049                                   && ((insn_operand_predicate[(int) new_icode][0]
5050                                        && ! ((*insn_operand_predicate[(int) new_icode][0])
5051                                              (reloadreg, mode)))
5052                                       || (insn_operand_predicate[(int) new_icode][1]
5053                                           && ! ((*insn_operand_predicate[(int) new_icode][1])
5054                                                 (real_oldequiv, mode)))))
5055                                 new_icode = CODE_FOR_nothing;
5056
5057                               if (new_icode == CODE_FOR_nothing)
5058                                 new_mode = mode;
5059                               else
5060                                 new_mode = insn_operand_mode[new_icode][2];
5061
5062                               if (GET_MODE (second_reload_reg) != new_mode)
5063                                 {
5064                                   if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
5065                                                            new_mode))
5066                                     oldequiv = old, real_oldequiv = real_old;
5067                                   else
5068                                     second_reload_reg
5069                                       = gen_rtx (REG, new_mode,
5070                                                  REGNO (second_reload_reg));
5071                                 }
5072                             }
5073                         }
5074                     }
5075
5076                   /* If we still need a secondary reload register, check
5077                      to see if it is being used as a scratch or intermediate
5078                      register and generate code appropriately.  If we need
5079                      a scratch register, use REAL_OLDEQUIV since the form of
5080                      the insn may depend on the actual address if it is 
5081                      a MEM.  */
5082
5083                   if (second_reload_reg)
5084                     {
5085                       if (icode != CODE_FOR_nothing)
5086                         {
5087                           reload_insn = emit_insn_before (GEN_FCN (icode)
5088                                                           (reloadreg,
5089                                                            real_oldequiv,
5090                                                            second_reload_reg),
5091                                                           where);
5092                           if (this_reload_insn == 0)
5093                             this_reload_insn = reload_insn;
5094                           special = 1;
5095                         }
5096                       else
5097                         {
5098                           /* See if we need a scratch register to load the
5099                              intermediate register (a tertiary reload).  */
5100                           enum insn_code tertiary_icode
5101                             = reload_secondary_icode[secondary_reload];
5102
5103                           if (tertiary_icode != CODE_FOR_nothing)
5104                             {
5105                               rtx third_reload_reg
5106                                 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5107
5108                               reload_insn
5109                                 = emit_insn_before ((GEN_FCN (tertiary_icode)
5110                                                      (second_reload_reg,
5111                                                       real_oldequiv,
5112                                                       third_reload_reg)),
5113                                                     where);
5114                               if (this_reload_insn == 0)
5115                                 this_reload_insn = reload_insn;
5116                             }
5117                           else
5118                             {
5119                               reload_insn
5120                                 = gen_input_reload (second_reload_reg,
5121                                                     oldequiv, where);
5122                               if (this_reload_insn == 0)
5123                                 this_reload_insn = reload_insn;
5124                               oldequiv = second_reload_reg;
5125                             }
5126                         }
5127                     }
5128                 }
5129 #endif
5130
5131               if (! special)
5132                 {
5133                   reload_insn = gen_input_reload (reloadreg, oldequiv, where);
5134                   if (this_reload_insn == 0)
5135                     this_reload_insn = reload_insn;
5136                 }
5137
5138 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
5139               /* We may have to make a REG_DEAD note for the secondary reload
5140                  register in the insns we just made.  Find the last insn that
5141                  mentioned the register.  */
5142               if (! special && second_reload_reg
5143                   && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
5144                 {
5145                   rtx prev;
5146
5147                   for (prev = where;
5148                        prev != PREV_INSN (this_reload_insn);
5149                        prev = PREV_INSN (prev))
5150                     if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
5151                         && reg_overlap_mentioned_for_reload_p (second_reload_reg,
5152                                                                PATTERN (prev)))
5153                       {
5154                         REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
5155                                                     second_reload_reg,
5156                                                     REG_NOTES (prev));
5157                         break;
5158                       }
5159                 }
5160 #endif
5161             }
5162
5163           /* Update where to put other reload insns.  */
5164           if (this_reload_insn)
5165             switch (reload_when_needed[j])
5166               {
5167               case RELOAD_FOR_INPUT:
5168               case RELOAD_OTHER:
5169                 if (first_other_reload_insn == first_operand_address_reload_insn)
5170                   first_other_reload_insn = this_reload_insn;
5171                 break;
5172               case RELOAD_FOR_OPERAND_ADDRESS:
5173                 if (first_operand_address_reload_insn == before_insn)
5174                   first_operand_address_reload_insn = this_reload_insn;
5175                 if (first_other_reload_insn == before_insn)
5176                   first_other_reload_insn = this_reload_insn;
5177               }
5178
5179           /* reload_inc[j] was formerly processed here.  */
5180         }
5181
5182       /* Add a note saying the input reload reg
5183          dies in this insn, if anyone cares.  */
5184 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5185       if (old != 0
5186           && reload_reg_rtx[j] != old
5187           && reload_reg_rtx[j] != 0
5188           && reload_out[j] == 0
5189           && ! reload_inherited[j]
5190           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
5191         {
5192           register rtx reloadreg = reload_reg_rtx[j];
5193
5194 #if 0
5195           /* We can't abort here because we need to support this for sched.c.
5196              It's not terrible to miss a REG_DEAD note, but we should try
5197              to figure out how to do this correctly.  */
5198           /* The code below is incorrect for address-only reloads.  */
5199           if (reload_when_needed[j] != RELOAD_OTHER
5200               && reload_when_needed[j] != RELOAD_FOR_INPUT)
5201             abort ();
5202 #endif
5203
5204           /* Add a death note to this insn, for an input reload.  */
5205
5206           if ((reload_when_needed[j] == RELOAD_OTHER
5207                || reload_when_needed[j] == RELOAD_FOR_INPUT)
5208               && ! dead_or_set_p (insn, reloadreg))
5209             REG_NOTES (insn)
5210               = gen_rtx (EXPR_LIST, REG_DEAD,
5211                          reloadreg, REG_NOTES (insn));
5212         }
5213
5214       /* When we inherit a reload, the last marked death of the reload reg
5215          may no longer really be a death.  */
5216       if (reload_reg_rtx[j] != 0
5217           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
5218           && reload_inherited[j])
5219         {
5220           /* Handle inheriting an output reload.
5221              Remove the death note from the output reload insn.  */
5222           if (reload_spill_index[j] >= 0
5223               && GET_CODE (reload_in[j]) == REG
5224               && spill_reg_store[reload_spill_index[j]] != 0
5225               && find_regno_note (spill_reg_store[reload_spill_index[j]],
5226                                   REG_DEAD, REGNO (reload_reg_rtx[j])))
5227             remove_death (REGNO (reload_reg_rtx[j]),
5228                           spill_reg_store[reload_spill_index[j]]);
5229           /* Likewise for input reloads that were inherited.  */
5230           else if (reload_spill_index[j] >= 0
5231                    && GET_CODE (reload_in[j]) == REG
5232                    && spill_reg_store[reload_spill_index[j]] == 0
5233                    && reload_inheritance_insn[j] != 0
5234                    && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
5235                                        REGNO (reload_reg_rtx[j])))
5236             remove_death (REGNO (reload_reg_rtx[j]),
5237                           reload_inheritance_insn[j]);
5238           else
5239             {
5240               rtx prev;
5241
5242               /* We got this register from find_equiv_reg.
5243                  Search back for its last death note and get rid of it.
5244                  But don't search back too far.
5245                  Don't go past a place where this reg is set,
5246                  since a death note before that remains valid.  */
5247               for (prev = PREV_INSN (insn);
5248                    prev && GET_CODE (prev) != CODE_LABEL;
5249                    prev = PREV_INSN (prev))
5250                 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5251                     && dead_or_set_p (prev, reload_reg_rtx[j]))
5252                   {
5253                     if (find_regno_note (prev, REG_DEAD,
5254                                          REGNO (reload_reg_rtx[j])))
5255                       remove_death (REGNO (reload_reg_rtx[j]), prev);
5256                     break;
5257                   }
5258             }
5259         }
5260
5261       /* We might have used find_equiv_reg above to choose an alternate
5262          place from which to reload.  If so, and it died, we need to remove
5263          that death and move it to one of the insns we just made.  */
5264
5265       if (oldequiv_reg != 0
5266           && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
5267         {
5268           rtx prev, prev1;
5269
5270           for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
5271                prev = PREV_INSN (prev))
5272             if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5273                 && dead_or_set_p (prev, oldequiv_reg))
5274               {
5275                 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
5276                   {
5277                     for (prev1 = this_reload_insn;
5278                          prev1; prev1 = PREV_INSN (prev1))
5279                       if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
5280                         && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
5281                                                                PATTERN (prev1)))
5282                       {
5283                         REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
5284                                                      oldequiv_reg,
5285                                                      REG_NOTES (prev1));
5286                         break;
5287                       }
5288                     remove_death (REGNO (oldequiv_reg), prev);
5289                   }
5290                 break;
5291               }
5292         }
5293 #endif
5294
5295       /* If we are reloading a register that was recently stored in with an
5296          output-reload, see if we can prove there was
5297          actually no need to store the old value in it.  */
5298
5299       if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
5300           /* This is unsafe if some other reload uses the same reg first.  */
5301           && (reload_when_needed[j] == RELOAD_OTHER
5302               || reload_when_needed[j] == RELOAD_FOR_INPUT
5303               || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS)
5304           && GET_CODE (reload_in[j]) == REG
5305 #if 0
5306           /* There doesn't seem to be any reason to restrict this to pseudos
5307              and doing so loses in the case where we are copying from a
5308              register of the wrong class.  */
5309           && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
5310 #endif
5311           && spill_reg_store[reload_spill_index[j]] != 0
5312           && dead_or_set_p (insn, reload_in[j])
5313           /* This is unsafe if operand occurs more than once in current
5314              insn.  Perhaps some occurrences weren't reloaded.  */
5315           && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
5316         delete_output_reload (insn, j,
5317                               spill_reg_store[reload_spill_index[j]]);
5318
5319       /* Input-reloading is done.  Now do output-reloading,
5320          storing the value from the reload-register after the main insn
5321          if reload_out[j] is nonzero.
5322
5323          ??? At some point we need to support handling output reloads of
5324          JUMP_INSNs or insns that set cc0.  */
5325       old = reload_out[j];
5326       if (old != 0
5327           && reload_reg_rtx[j] != old
5328           && reload_reg_rtx[j] != 0)
5329         {
5330           register rtx reloadreg = reload_reg_rtx[j];
5331           register rtx second_reloadreg = 0;
5332           rtx prev_insn = PREV_INSN (first_output_reload_insn);
5333           rtx note, p;
5334           enum machine_mode mode;
5335           int special = 0;
5336
5337           /* An output operand that dies right away does need a reload,
5338              but need not be copied from it.  Show the new location in the
5339              REG_UNUSED note.  */
5340           if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
5341               && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
5342             {
5343               XEXP (note, 0) = reload_reg_rtx[j];
5344               continue;
5345             }
5346           else if (GET_CODE (old) == SCRATCH)
5347             /* If we aren't optimizing, there won't be a REG_UNUSED note,
5348                but we don't want to make an output reload.  */
5349             continue;
5350
5351 #if 0
5352           /* Strip off of OLD any size-increasing SUBREGs such as
5353              (SUBREG:SI foo:QI 0).  */
5354
5355           while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
5356                  && (GET_MODE_SIZE (GET_MODE (old))
5357                      > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
5358             old = SUBREG_REG (old);
5359 #endif
5360
5361           /* If is a JUMP_INSN, we can't support output reloads yet.  */
5362           if (GET_CODE (insn) == JUMP_INSN)
5363             abort ();
5364
5365           /* Determine the mode to reload in.
5366              See comments above (for input reloading).  */
5367
5368           mode = GET_MODE (old);
5369           if (mode == VOIDmode)
5370             {
5371               /* VOIDmode should never happen for an output.  */
5372               if (asm_noperands (PATTERN (insn)) < 0)
5373                 /* It's the compiler's fault.  */
5374                 abort ();
5375               error_for_asm (insn, "output operand is constant in `asm'");
5376               /* Prevent crash--use something we know is valid.  */
5377               mode = word_mode;
5378               old = gen_rtx (REG, mode, REGNO (reloadreg));
5379             }
5380
5381           /* A strict-low-part output operand needs to be reloaded
5382              in the mode of the entire value.  */
5383           if (reload_strict_low[j])
5384             {
5385               mode = GET_MODE (SUBREG_REG (reload_out[j]));
5386               /* Encapsulate OLD into that mode.  */
5387               /* If OLD is a subreg, then strip it, since the subreg will
5388                  be altered by this very reload.  */
5389               while (GET_CODE (old) == SUBREG && GET_MODE (old) != mode)
5390                 old = SUBREG_REG (old);
5391               if (GET_MODE (old) != VOIDmode
5392                   && mode != GET_MODE (old))
5393                 old = gen_rtx (SUBREG, mode, old, 0);
5394             }
5395
5396           if (GET_MODE (reloadreg) != mode)
5397             reloadreg = gen_lowpart_common (mode, reloadreg);
5398
5399 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5400
5401           /* If we need two reload regs, set RELOADREG to the intermediate
5402              one, since it will be stored into OUT.  We might need a secondary
5403              register only for an input reload, so check again here.  */
5404
5405           if (reload_secondary_reload[j] >= 0)
5406             {
5407               rtx real_old = old;
5408
5409               if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
5410                   && reg_equiv_mem[REGNO (old)] != 0)
5411                 real_old = reg_equiv_mem[REGNO (old)];
5412
5413               if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
5414                                                  mode, real_old)
5415                   != NO_REGS))
5416                 {
5417                   second_reloadreg = reloadreg;
5418                   reloadreg = reload_reg_rtx[reload_secondary_reload[j]];
5419
5420                   /* See if RELOADREG is to be used as a scratch register
5421                      or as an intermediate register.  */
5422                   if (reload_secondary_icode[j] != CODE_FOR_nothing)
5423                     {
5424                       emit_insn_before ((GEN_FCN (reload_secondary_icode[j])
5425                                          (real_old, second_reloadreg,
5426                                           reloadreg)),
5427                                         first_output_reload_insn);
5428                       special = 1;
5429                     }
5430                   else
5431                     {
5432                       /* See if we need both a scratch and intermediate reload
5433                          register.  */
5434                       int secondary_reload = reload_secondary_reload[j];
5435                       enum insn_code tertiary_icode
5436                         = reload_secondary_icode[secondary_reload];
5437                       rtx pat;
5438
5439                       if (GET_MODE (reloadreg) != mode)
5440                         reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5441
5442                       if (tertiary_icode != CODE_FOR_nothing)
5443                         {
5444                           rtx third_reloadreg
5445                             = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5446                           pat = (GEN_FCN (tertiary_icode)
5447                                  (reloadreg, second_reloadreg, third_reloadreg));
5448                         }
5449 #ifdef SECONDARY_MEMORY_NEEDED
5450                       /* If we need a memory location to do the move, do it that way.  */
5451                       else if (GET_CODE (reloadreg) == REG
5452                                && REGNO (reloadreg) < FIRST_PSEUDO_REGISTER
5453                                && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (reloadreg)),
5454                                            REGNO_REG_CLASS (REGNO (second_reloadreg)),
5455                                            GET_MODE (second_reloadreg)))
5456                         {
5457                           /* Get the memory to use and rewrite both registers
5458                              to its mode.  */
5459                           rtx loc = get_secondary_mem (reloadreg,
5460                                                        GET_MODE (second_reloadreg));
5461                           rtx tmp_reloadreg;
5462                             
5463                           if (GET_MODE (loc) != GET_MODE (second_reloadreg))
5464                             second_reloadreg = gen_rtx (REG, GET_MODE (loc),
5465                                                         REGNO (second_reloadreg));
5466                           
5467                           if (GET_MODE (loc) != GET_MODE (reloadreg))
5468                             tmp_reloadreg = gen_rtx (REG, GET_MODE (loc),
5469                                                      REGNO (reloadreg));
5470                           else
5471                             tmp_reloadreg = reloadreg;
5472                           
5473                           emit_insn_before (gen_move_insn (loc, second_reloadreg),
5474                                             first_output_reload_insn);
5475                           pat = gen_move_insn (tmp_reloadreg, loc);
5476                         }
5477 #endif
5478                       else
5479                         pat = gen_move_insn (reloadreg, second_reloadreg);
5480
5481                       emit_insn_before (pat, first_output_reload_insn);
5482                     }
5483                 }
5484             }
5485 #endif
5486
5487           /* Output the last reload insn.  */
5488           if (! special)
5489             {
5490 #ifdef SECONDARY_MEMORY_NEEDED
5491               /* If we need a memory location to do the move, do it that way.  */
5492               if (GET_CODE (old) == REG && REGNO (old) < FIRST_PSEUDO_REGISTER
5493                   && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (old)),
5494                                               REGNO_REG_CLASS (REGNO (reloadreg)),
5495                                               GET_MODE (reloadreg)))
5496                 {
5497                   /* Get the memory to use and rewrite both registers to
5498                      its mode.  */
5499                   rtx loc = get_secondary_mem (old, GET_MODE (reloadreg));
5500
5501                   if (GET_MODE (loc) != GET_MODE (reloadreg))
5502                     reloadreg = gen_rtx (REG, GET_MODE (loc),
5503                                          REGNO (reloadreg));
5504
5505                   if (GET_MODE (loc) != GET_MODE (old))
5506                     old = gen_rtx (REG, GET_MODE (loc), REGNO (old));
5507
5508                   emit_insn_before (gen_move_insn (loc, reloadreg),
5509                                     first_output_reload_insn);
5510                   emit_insn_before (gen_move_insn (old, loc),
5511                                     first_output_reload_insn);
5512                 }
5513               else
5514 #endif
5515                 emit_insn_before (gen_move_insn (old, reloadreg),
5516                                   first_output_reload_insn);
5517             }
5518
5519 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5520           /* If final will look at death notes for this reg,
5521              put one on the last output-reload insn to use it.  Similarly
5522              for any secondary register.  */
5523           if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
5524             for (p = PREV_INSN (first_output_reload_insn);
5525                  p != prev_insn; p = PREV_INSN (p))
5526               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5527                   && reg_overlap_mentioned_for_reload_p (reloadreg,
5528                                                          PATTERN (p)))
5529                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5530                                          reloadreg, REG_NOTES (p));
5531
5532 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5533           if (! special
5534               && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
5535             for (p = PREV_INSN (first_output_reload_insn);
5536                  p != prev_insn; p = PREV_INSN (p))
5537               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5538                   && reg_overlap_mentioned_for_reload_p (second_reloadreg,
5539                                                          PATTERN (p)))
5540                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5541                                          second_reloadreg, REG_NOTES (p));
5542 #endif
5543 #endif
5544           /* Look at all insns we emitted, just to be safe.  */
5545           for (p = NEXT_INSN (prev_insn); p != first_output_reload_insn;
5546                p = NEXT_INSN (p))
5547             if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5548               {
5549                 /* If this output reload doesn't come from a spill reg,
5550                    clear any memory of reloaded copies of the pseudo reg.
5551                    If this output reload comes from a spill reg,
5552                    reg_has_output_reload will make this do nothing.  */
5553                 note_stores (PATTERN (p), forget_old_reloads_1);
5554
5555                 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p)))
5556                   store_insn = p;
5557               }
5558
5559           first_output_reload_insn = NEXT_INSN (prev_insn);
5560         }
5561
5562       if (reload_spill_index[j] >= 0)
5563         new_spill_reg_store[reload_spill_index[j]] = store_insn;
5564     }
5565
5566   /* Move death notes from INSN
5567      to output-operand-address and output reload insns.  */
5568 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5569   {
5570     rtx insn1;
5571     /* Loop over those insns, last ones first.  */
5572     for (insn1 = PREV_INSN (following_insn); insn1 != insn;
5573          insn1 = PREV_INSN (insn1))
5574       if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
5575         {
5576           rtx source = SET_SRC (PATTERN (insn1));
5577           rtx dest = SET_DEST (PATTERN (insn1));
5578
5579           /* The note we will examine next.  */
5580           rtx reg_notes = REG_NOTES (insn);
5581           /* The place that pointed to this note.  */
5582           rtx *prev_reg_note = &REG_NOTES (insn);
5583
5584           /* If the note is for something used in the source of this
5585              reload insn, or in the output address, move the note.  */
5586           while (reg_notes)
5587             {
5588               rtx next_reg_notes = XEXP (reg_notes, 1);
5589               if (REG_NOTE_KIND (reg_notes) == REG_DEAD
5590                   && GET_CODE (XEXP (reg_notes, 0)) == REG
5591                   && ((GET_CODE (dest) != REG
5592                        && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5593                                                               dest))
5594                       || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5595                                                              source)))
5596                 {
5597                   *prev_reg_note = next_reg_notes;
5598                   XEXP (reg_notes, 1) = REG_NOTES (insn1);
5599                   REG_NOTES (insn1) = reg_notes;
5600                 }
5601               else
5602                 prev_reg_note = &XEXP (reg_notes, 1);
5603
5604               reg_notes = next_reg_notes;
5605             }
5606         }
5607   }
5608 #endif
5609
5610   /* For all the spill regs newly reloaded in this instruction,
5611      record what they were reloaded from, so subsequent instructions
5612      can inherit the reloads.
5613
5614      Update spill_reg_store for the reloads of this insn.
5615      Copy the elements that were updated in the loop above.  */
5616
5617   for (j = 0; j < n_reloads; j++)
5618     {
5619       register int r = reload_order[j];
5620       register int i = reload_spill_index[r];
5621
5622       /* I is nonneg if this reload used one of the spill regs.
5623          If reload_reg_rtx[r] is 0, this is an optional reload
5624          that we opted to ignore.  */
5625
5626       if (i >= 0 && reload_reg_rtx[r] != 0)
5627         {
5628           /* First, clear out memory of what used to be in this spill reg.
5629              If consecutive registers are used, clear them all.  */
5630           int nr
5631             = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
5632           int k;
5633
5634           for (k = 0; k < nr; k++)
5635             {
5636               reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
5637               reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
5638             }
5639
5640           /* Maybe the spill reg contains a copy of reload_out.  */
5641           if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5642             {
5643               register int nregno = REGNO (reload_out[r]);
5644
5645               spill_reg_store[i] = new_spill_reg_store[i];
5646               reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5647
5648               for (k = 0; k < nr; k++)
5649                 {
5650                   reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5651                     = nregno;
5652                   reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
5653                 }
5654             }
5655
5656           /* Maybe the spill reg contains a copy of reload_in.  */
5657           else if (reload_out[r] == 0
5658                    && reload_in[r] != 0
5659                    && (GET_CODE (reload_in[r]) == REG
5660                        || GET_CODE (reload_in_reg[r]) == REG))
5661             {
5662               register int nregno;
5663               if (GET_CODE (reload_in[r]) == REG)
5664                 nregno = REGNO (reload_in[r]);
5665               else
5666                 nregno = REGNO (reload_in_reg[r]);
5667
5668               /* If there are two separate reloads (one in and one out)
5669                  for the same (hard or pseudo) reg,
5670                  leave reg_last_reload_reg set
5671                  based on the output reload.
5672                  Otherwise, set it from this input reload.  */
5673               if (!reg_has_output_reload[nregno]
5674                   /* But don't do so if another input reload
5675                      will clobber this one's value.  */
5676                   && reload_reg_reaches_end_p (spill_regs[i],
5677                                                reload_when_needed[r]))
5678                 {
5679                   reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5680
5681                   /* Unless we inherited this reload, show we haven't
5682                      recently done a store.  */
5683                   if (! reload_inherited[r])
5684                     spill_reg_store[i] = 0;
5685
5686                   for (k = 0; k < nr; k++)
5687                     {
5688                       reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5689                         = nregno;
5690                       reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
5691                         = insn;
5692                     }
5693                 }
5694             }
5695         }
5696
5697       /* The following if-statement was #if 0'd in 1.34 (or before...).
5698          It's reenabled in 1.35 because supposedly nothing else
5699          deals with this problem.  */
5700
5701       /* If a register gets output-reloaded from a non-spill register,
5702          that invalidates any previous reloaded copy of it.
5703          But forget_old_reloads_1 won't get to see it, because
5704          it thinks only about the original insn.  So invalidate it here.  */
5705       if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5706         {
5707           register int nregno = REGNO (reload_out[r]);
5708           reg_last_reload_reg[nregno] = 0;
5709         }
5710     }
5711 }
5712 \f
5713 /* Emit code before BEFORE_INSN to perform an input reload of IN to RELOADREG.
5714    Returns first insn emitted.  */
5715
5716 rtx
5717 gen_input_reload (reloadreg, in, before_insn)
5718      rtx reloadreg;
5719      rtx in;
5720      rtx before_insn;
5721 {
5722   register rtx prev_insn = PREV_INSN (before_insn);
5723
5724   /* How to do this reload can get quite tricky.  Normally, we are being
5725      asked to reload a simple operand, such as a MEM, a constant, or a pseudo
5726      register that didn't get a hard register.  In that case we can just
5727      call emit_move_insn.
5728
5729      We can also be asked to reload a PLUS that adds either two registers, or
5730      a register and a constant or MEM, or a MEM and a constant.  This can
5731      occur during frame pointer elimination and while reloading addresses.
5732      This case is handled by trying to emit a single insn
5733      to perform the add.  If it is not valid, we use a two insn sequence.
5734
5735      Finally, we could be called to handle an 'o' constraint by putting
5736      an address into a register.  In that case, we first try to do this
5737      with a named pattern of "reload_load_address".  If no such pattern
5738      exists, we just emit a SET insn and hope for the best (it will normally
5739      be valid on machines that use 'o').
5740
5741      This entire process is made complex because reload will never
5742      process the insns we generate here and so we must ensure that
5743      they will fit their constraints and also by the fact that parts of
5744      IN might be being reloaded separately and replaced with spill registers.
5745      Because of this, we are, in some sense, just guessing the right approach
5746      here.  The one listed above seems to work.
5747
5748      ??? At some point, this whole thing needs to be rethought.  */
5749
5750   if (GET_CODE (in) == PLUS
5751       && ((GET_CODE (XEXP (in, 0)) == REG
5752            && (GET_CODE (XEXP (in, 1)) == REG
5753                || CONSTANT_P (XEXP (in, 1))
5754                || GET_CODE (XEXP (in, 1)) == MEM))
5755           || (GET_CODE (XEXP (in, 0)) == MEM
5756               && CONSTANT_P (XEXP (in, 1)))))
5757     {
5758       /* We need to compute the sum of what is either a register and a
5759          constant, a register and memory, a hard register and a pseudo
5760          register, or memory and a constant and put it into the reload
5761          register.  The best possible way of doing this is if the machine
5762          has a three-operand ADD insn that accepts the required operands.
5763
5764          The simplest approach is to try to generate such an insn and see if it
5765          is recognized and matches its constraints.  If so, it can be used.
5766
5767          It might be better not to actually emit the insn unless it is valid,
5768          but we need to pass the insn as an operand to `recog' and
5769          `insn_extract' and it is simpler to emit and then delete the insn if
5770          not valid than to dummy things up.  */
5771
5772       rtx op0, op1, tem, insn;
5773       int code;
5774
5775       op0 = find_replacement (&XEXP (in, 0));
5776       op1 = find_replacement (&XEXP (in, 1));
5777
5778       /* Since constraint checking is strict, commutativity won't be
5779          checked, so we need to do that here to avoid spurious failure
5780          if the add instruction is two-address and the second operand
5781          of the add is the same as the reload reg, which is frequently
5782          the case.  If the insn would be A = B + A, rearrange it so
5783          it will be A = A + B as constrain_operands expects. */
5784
5785       if (GET_CODE (XEXP (in, 1)) == REG
5786           && REGNO (reloadreg) == REGNO (XEXP (in, 1)))
5787         tem = op0, op0 = op1, op1 = tem;
5788
5789       if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
5790         in = gen_rtx (PLUS, GET_MODE (in), op0, op1);
5791
5792       insn = emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in),
5793                                    before_insn);
5794       code = recog_memoized (insn);
5795
5796       if (code >= 0)
5797         {
5798           insn_extract (insn);
5799           /* We want constrain operands to treat this insn strictly in
5800              its validity determination, i.e., the way it would after reload
5801              has completed.  */
5802           if (constrain_operands (code, 1))
5803             return insn;
5804         }
5805
5806       if (PREV_INSN (insn))
5807         NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
5808       if (NEXT_INSN (insn))
5809         PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
5810
5811       /* If that failed, we must use a conservative two-insn sequence.
5812          use move to copy constant, MEM, or pseudo register to the reload
5813          register since "move" will be able to handle an arbitrary operand,
5814          unlike add which can't, in general.  Then add the registers.
5815
5816          If there is another way to do this for a specific machine, a
5817          DEFINE_PEEPHOLE should be specified that recognizes the sequence
5818          we emit below.  */
5819
5820       if (CONSTANT_P (op1) || GET_CODE (op1) == MEM
5821           || (GET_CODE (op1) == REG
5822               && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
5823         tem = op0, op0 = op1, op1 = tem;
5824
5825       emit_insn_before (gen_move_insn (reloadreg, op0), before_insn);
5826
5827       /* If OP0 and OP1 are the same, we can use RELOADREG for OP1.
5828          This fixes a problem on the 32K where the stack pointer cannot
5829          be used as an operand of an add insn.  */
5830
5831       if (rtx_equal_p (op0, op1))
5832         op1 = reloadreg;
5833
5834       emit_insn_before (gen_add2_insn (reloadreg, op1), before_insn);
5835     }
5836
5837 #ifdef SECONDARY_MEMORY_NEEDED
5838   /* If we need a memory location to do the move, do it that way.  */
5839   else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
5840            && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
5841                                        REGNO_REG_CLASS (REGNO (reloadreg)),
5842                                        GET_MODE (reloadreg)))
5843     {
5844       /* Get the memory to use and rewrite both registers to its mode.  */
5845       rtx loc = get_secondary_mem (in, GET_MODE (reloadreg));
5846
5847       if (GET_MODE (loc) != GET_MODE (reloadreg))
5848         reloadreg = gen_rtx (REG, GET_MODE (loc), REGNO (reloadreg));
5849
5850       if (GET_MODE (loc) != GET_MODE (in))
5851         in = gen_rtx (REG, GET_MODE (loc), REGNO (in));
5852
5853       emit_insn_before (gen_move_insn (loc, in), before_insn);
5854       emit_insn_before (gen_move_insn (reloadreg, loc), before_insn);
5855     }
5856 #endif
5857
5858   /* If IN is a simple operand, use gen_move_insn.  */
5859   else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5860     emit_insn_before (gen_move_insn (reloadreg, in), before_insn);
5861
5862 #ifdef HAVE_reload_load_address
5863   else if (HAVE_reload_load_address)
5864     emit_insn_before (gen_reload_load_address (reloadreg, in), before_insn);
5865 #endif
5866
5867   /* Otherwise, just write (set REGLOADREG IN) and hope for the best.  */
5868   else
5869     emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in), before_insn);
5870
5871   /* Return the first insn emitted.
5872      We can not just return PREV_INSN (before_insn), because there may have
5873      been multiple instructions emitted.  Also note that gen_move_insn may
5874      emit more than one insn itself, so we can not assume that there is one
5875      insn emitted per emit_insn_before call.  */
5876
5877   return NEXT_INSN (prev_insn);
5878 }
5879 \f
5880 /* Delete a previously made output-reload
5881    whose result we now believe is not needed.
5882    First we double-check.
5883
5884    INSN is the insn now being processed.
5885    OUTPUT_RELOAD_INSN is the insn of the output reload.
5886    J is the reload-number for this insn.  */
5887
5888 static void
5889 delete_output_reload (insn, j, output_reload_insn)
5890      rtx insn;
5891      int j;
5892      rtx output_reload_insn;
5893 {
5894   register rtx i1;
5895
5896   /* Get the raw pseudo-register referred to.  */
5897
5898   rtx reg = reload_in[j];
5899   while (GET_CODE (reg) == SUBREG)
5900     reg = SUBREG_REG (reg);
5901
5902   /* If the pseudo-reg we are reloading is no longer referenced
5903      anywhere between the store into it and here,
5904      and no jumps or labels intervene, then the value can get
5905      here through the reload reg alone.
5906      Otherwise, give up--return.  */
5907   for (i1 = NEXT_INSN (output_reload_insn);
5908        i1 != insn; i1 = NEXT_INSN (i1))
5909     {
5910       if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
5911         return;
5912       if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
5913           && reg_mentioned_p (reg, PATTERN (i1)))
5914         return;
5915     }
5916
5917   /* If this insn will store in the pseudo again,
5918      the previous store can be removed.  */
5919   if (reload_out[j] == reload_in[j])
5920     delete_insn (output_reload_insn);
5921
5922   /* See if the pseudo reg has been completely replaced
5923      with reload regs.  If so, delete the store insn
5924      and forget we had a stack slot for the pseudo.  */
5925   else if (reg_n_deaths[REGNO (reg)] == 1
5926            && reg_basic_block[REGNO (reg)] >= 0
5927            && find_regno_note (insn, REG_DEAD, REGNO (reg)))
5928     {
5929       rtx i2;
5930
5931       /* We know that it was used only between here
5932          and the beginning of the current basic block.
5933          (We also know that the last use before INSN was
5934          the output reload we are thinking of deleting, but never mind that.)
5935          Search that range; see if any ref remains.  */
5936       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5937         {
5938           rtx set = single_set (i2);
5939
5940           /* Uses which just store in the pseudo don't count,
5941              since if they are the only uses, they are dead.  */
5942           if (set != 0 && SET_DEST (set) == reg)
5943             continue;
5944           if (GET_CODE (i2) == CODE_LABEL
5945               || GET_CODE (i2) == JUMP_INSN)
5946             break;
5947           if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
5948               && reg_mentioned_p (reg, PATTERN (i2)))
5949             /* Some other ref remains;
5950                we can't do anything.  */
5951             return;
5952         }
5953
5954       /* Delete the now-dead stores into this pseudo.  */
5955       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5956         {
5957           rtx set = single_set (i2);
5958
5959           if (set != 0 && SET_DEST (set) == reg)
5960             delete_insn (i2);
5961           if (GET_CODE (i2) == CODE_LABEL
5962               || GET_CODE (i2) == JUMP_INSN)
5963             break;
5964         }
5965
5966       /* For the debugging info,
5967          say the pseudo lives in this reload reg.  */
5968       reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
5969       alter_reg (REGNO (reg), -1);
5970     }
5971 }
5972
5973 \f
5974 /* Output reload-insns to reload VALUE into RELOADREG.
5975    VALUE is an autoincrement or autodecrement RTX whose operand
5976    is a register or memory location;
5977    so reloading involves incrementing that location.
5978
5979    INC_AMOUNT is the number to increment or decrement by (always positive).
5980    This cannot be deduced from VALUE.
5981
5982    INSN is the insn before which the new insns should be emitted.
5983
5984    The return value is the first of the insns emitted.  */
5985
5986 static rtx
5987 inc_for_reload (reloadreg, value, inc_amount, insn)
5988      rtx reloadreg;
5989      rtx value;
5990      int inc_amount;
5991      rtx insn;
5992 {
5993   /* REG or MEM to be copied and incremented.  */
5994   rtx incloc = XEXP (value, 0);
5995   /* Nonzero if increment after copying.  */
5996   int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
5997   rtx prev = PREV_INSN (insn);
5998   rtx inc;
5999   rtx add_insn;
6000   int code;
6001
6002   /* No hard register is equivalent to this register after
6003      inc/dec operation.  If REG_LAST_RELOAD_REG were non-zero,
6004      we could inc/dec that register as well (maybe even using it for
6005      the source), but I'm not sure it's worth worrying about.  */
6006   if (GET_CODE (incloc) == REG)
6007     reg_last_reload_reg[REGNO (incloc)] = 0;
6008
6009   if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
6010     inc_amount = - inc_amount;
6011
6012   inc = GEN_INT (inc_amount);
6013
6014   /* If this is post-increment, first copy the location to the reload reg.  */
6015   if (post)
6016     emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6017
6018   /* See if we can directly increment INCLOC.  Use a method similar to that
6019      in gen_input_reload.  */
6020
6021   add_insn = emit_insn_before (gen_rtx (SET, VOIDmode, incloc,
6022                                         gen_rtx (PLUS, GET_MODE (incloc),
6023                                                  incloc, inc)), insn);
6024                                                           
6025   code = recog_memoized (add_insn);
6026   if (code >= 0)
6027     {
6028       insn_extract (add_insn);
6029       if (constrain_operands (code, 1))
6030         {
6031           /* If this is a pre-increment and we have incremented the value
6032              where it lives, copy the incremented value to RELOADREG to
6033              be used as an address.  */
6034
6035           if (! post)
6036             emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6037           return NEXT_INSN (prev);
6038         }
6039     }
6040
6041   if (PREV_INSN (add_insn))
6042     NEXT_INSN (PREV_INSN (add_insn)) = NEXT_INSN (add_insn);
6043   if (NEXT_INSN (add_insn))
6044     PREV_INSN (NEXT_INSN (add_insn)) = PREV_INSN (add_insn);
6045
6046   /* If couldn't do the increment directly, must increment in RELOADREG.
6047      The way we do this depends on whether this is pre- or post-increment.
6048      For pre-increment, copy INCLOC to the reload register, increment it
6049      there, then save back.  */
6050
6051   if (! post)
6052     {
6053       emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6054       emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
6055       emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
6056     }
6057   else
6058     {
6059       /* Postincrement.
6060          Because this might be a jump insn or a compare, and because RELOADREG
6061          may not be available after the insn in an input reload, we must do
6062          the incrementation before the insn being reloaded for.
6063
6064          We have already copied INCLOC to RELOADREG.  Increment the copy in
6065          RELOADREG, save that back, then decrement RELOADREG so it has
6066          the original value.  */
6067
6068       emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
6069       emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
6070       emit_insn_before (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)),
6071                         insn);
6072     }
6073
6074   return NEXT_INSN (prev);
6075 }
6076 \f
6077 /* Return 1 if we are certain that the constraint-string STRING allows
6078    the hard register REG.  Return 0 if we can't be sure of this.  */
6079
6080 static int
6081 constraint_accepts_reg_p (string, reg)
6082      char *string;
6083      rtx reg;
6084 {
6085   int value = 0;
6086   int regno = true_regnum (reg);
6087   int c;
6088
6089   /* Initialize for first alternative.  */
6090   value = 0;
6091   /* Check that each alternative contains `g' or `r'.  */
6092   while (1)
6093     switch (c = *string++)
6094       {
6095       case 0:
6096         /* If an alternative lacks `g' or `r', we lose.  */
6097         return value;
6098       case ',':
6099         /* If an alternative lacks `g' or `r', we lose.  */
6100         if (value == 0)
6101           return 0;
6102         /* Initialize for next alternative.  */
6103         value = 0;
6104         break;
6105       case 'g':
6106       case 'r':
6107         /* Any general reg wins for this alternative.  */
6108         if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
6109           value = 1;
6110         break;
6111       default:
6112         /* Any reg in specified class wins for this alternative.  */
6113         {
6114           enum reg_class class = REG_CLASS_FROM_LETTER (c);
6115
6116           if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
6117             value = 1;
6118         }
6119       }
6120 }
6121 \f
6122 /* Return the number of places FIND appears within X, but don't count
6123    an occurrence if some SET_DEST is FIND.  */
6124
6125 static int
6126 count_occurrences (x, find)
6127      register rtx x, find;
6128 {
6129   register int i, j;
6130   register enum rtx_code code;
6131   register char *format_ptr;
6132   int count;
6133
6134   if (x == find)
6135     return 1;
6136   if (x == 0)
6137     return 0;
6138
6139   code = GET_CODE (x);
6140
6141   switch (code)
6142     {
6143     case REG:
6144     case QUEUED:
6145     case CONST_INT:
6146     case CONST_DOUBLE:
6147     case SYMBOL_REF:
6148     case CODE_LABEL:
6149     case PC:
6150     case CC0:
6151       return 0;
6152
6153     case SET:
6154       if (SET_DEST (x) == find)
6155         return count_occurrences (SET_SRC (x), find);
6156       break;
6157     }
6158
6159   format_ptr = GET_RTX_FORMAT (code);
6160   count = 0;
6161
6162   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6163     {
6164       switch (*format_ptr++)
6165         {
6166         case 'e':
6167           count += count_occurrences (XEXP (x, i), find);
6168           break;
6169
6170         case 'E':
6171           if (XVEC (x, i) != NULL)
6172             {
6173               for (j = 0; j < XVECLEN (x, i); j++)
6174                 count += count_occurrences (XVECEXP (x, i, j), find);
6175             }
6176           break;
6177         }
6178     }
6179   return count;
6180 }