OSDN Git Service

(choose_reload_regs): Handle SUBREG in reload_earlyclobber
[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       return x;
2770
2771     case ASM_OPERANDS:
2772       {
2773         rtx *temp_vec;
2774         /* Properly handle sharing input and constraint vectors.  */
2775         if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
2776           {
2777             /* When we come to a new vector not seen before,
2778                scan all its elements; keep the old vector if none
2779                of them changes; otherwise, make a copy.  */
2780             old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
2781             temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
2782             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2783               temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
2784                                             mem_mode, NULL_RTX);
2785
2786             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2787               if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
2788                 break;
2789
2790             if (i == ASM_OPERANDS_INPUT_LENGTH (x))
2791               new_asm_operands_vec = old_asm_operands_vec;
2792             else
2793               new_asm_operands_vec
2794                 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
2795           }
2796
2797         /* If we had to copy the vector, copy the entire ASM_OPERANDS.  */
2798         if (new_asm_operands_vec == old_asm_operands_vec)
2799           return x;
2800
2801         new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
2802                        ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2803                        ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
2804                        ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
2805                        ASM_OPERANDS_SOURCE_FILE (x),
2806                        ASM_OPERANDS_SOURCE_LINE (x));
2807         new->volatil = x->volatil;
2808         return new;
2809       }
2810
2811     case SET:
2812       /* Check for setting a register that we know about.  */
2813       if (GET_CODE (SET_DEST (x)) == REG)
2814         {
2815           /* See if this is setting the replacement register for an
2816              elimination.
2817
2818              If DEST is the frame pointer, we do nothing because we assume that
2819              all assignments to the frame pointer are for non-local gotos and
2820              are being done at a time when they are valid and do not disturb
2821              anything else.  Some machines want to eliminate a fake argument
2822              pointer with either the frame or stack pointer.  Assignments to
2823              the frame pointer must not prevent this elimination.  */
2824
2825           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2826                ep++)
2827             if (ep->to_rtx == SET_DEST (x)
2828                 && SET_DEST (x) != frame_pointer_rtx)
2829               {
2830                 /* If it is being incremented, adjust the offset.  Otherwise,
2831                    this elimination can't be done.  */
2832                 rtx src = SET_SRC (x);
2833
2834                 if (GET_CODE (src) == PLUS
2835                     && XEXP (src, 0) == SET_DEST (x)
2836                     && GET_CODE (XEXP (src, 1)) == CONST_INT)
2837                   ep->offset -= INTVAL (XEXP (src, 1));
2838                 else
2839                   ep->can_eliminate = 0;
2840               }
2841
2842           /* Now check to see we are assigning to a register that can be
2843              eliminated.  If so, it must be as part of a PARALLEL, since we
2844              will not have been called if this is a single SET.  So indicate
2845              that we can no longer eliminate this reg.  */
2846           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2847                ep++)
2848             if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
2849               ep->can_eliminate = 0;
2850         }
2851
2852       /* Now avoid the loop below in this common case.  */
2853       {
2854         rtx new0 = eliminate_regs (SET_DEST (x), 0, NULL_RTX);
2855         rtx new1 = eliminate_regs (SET_SRC (x), 0, NULL_RTX);
2856
2857         /* If SET_DEST changed from a REG to a MEM and INSN is non-zero,
2858            write a CLOBBER insn.  */
2859         if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
2860             && insn != 0)
2861           emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
2862
2863         if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
2864           return gen_rtx (SET, VOIDmode, new0, new1);
2865       }
2866
2867       return x;
2868
2869     case MEM:
2870       /* Our only special processing is to pass the mode of the MEM to our
2871          recursive call and copy the flags.  While we are here, handle this
2872          case more efficiently.  */
2873       new = eliminate_regs (XEXP (x, 0), GET_MODE (x), NULL_RTX);
2874       if (new != XEXP (x, 0))
2875         {
2876           new = gen_rtx (MEM, GET_MODE (x), new);
2877           new->volatil = x->volatil;
2878           new->unchanging = x->unchanging;
2879           new->in_struct = x->in_struct;
2880           return new;
2881         }
2882       else
2883         return x;
2884     }
2885
2886   /* Process each of our operands recursively.  If any have changed, make a
2887      copy of the rtx.  */
2888   fmt = GET_RTX_FORMAT (code);
2889   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2890     {
2891       if (*fmt == 'e')
2892         {
2893           new = eliminate_regs (XEXP (x, i), mem_mode, NULL_RTX);
2894           if (new != XEXP (x, i) && ! copied)
2895             {
2896               rtx new_x = rtx_alloc (code);
2897               bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2898                                 + (sizeof (new_x->fld[0])
2899                                    * GET_RTX_LENGTH (code))));
2900               x = new_x;
2901               copied = 1;
2902             }
2903           XEXP (x, i) = new;
2904         }
2905       else if (*fmt == 'E')
2906         {
2907           int copied_vec = 0;
2908           for (j = 0; j < XVECLEN (x, i); j++)
2909             {
2910               new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2911               if (new != XVECEXP (x, i, j) && ! copied_vec)
2912                 {
2913                   rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2914                                              &XVECEXP (x, i, 0));
2915                   if (! copied)
2916                     {
2917                       rtx new_x = rtx_alloc (code);
2918                       bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2919                                         + (sizeof (new_x->fld[0])
2920                                            * GET_RTX_LENGTH (code))));
2921                       x = new_x;
2922                       copied = 1;
2923                     }
2924                   XVEC (x, i) = new_v;
2925                   copied_vec = 1;
2926                 }
2927               XVECEXP (x, i, j) = new;
2928             }
2929         }
2930     }
2931
2932   return x;
2933 }
2934 \f
2935 /* Scan INSN and eliminate all eliminable registers in it.
2936
2937    If REPLACE is nonzero, do the replacement destructively.  Also
2938    delete the insn as dead it if it is setting an eliminable register.
2939
2940    If REPLACE is zero, do all our allocations in reload_obstack.
2941
2942    If no eliminations were done and this insn doesn't require any elimination
2943    processing (these are not identical conditions: it might be updating sp,
2944    but not referencing fp; this needs to be seen during reload_as_needed so
2945    that the offset between fp and sp can be taken into consideration), zero
2946    is returned.  Otherwise, 1 is returned.  */
2947
2948 static int
2949 eliminate_regs_in_insn (insn, replace)
2950      rtx insn;
2951      int replace;
2952 {
2953   rtx old_body = PATTERN (insn);
2954   rtx new_body;
2955   int val = 0;
2956   struct elim_table *ep;
2957
2958   if (! replace)
2959     push_obstacks (&reload_obstack, &reload_obstack);
2960
2961   if (GET_CODE (old_body) == SET && GET_CODE (SET_DEST (old_body)) == REG
2962       && REGNO (SET_DEST (old_body)) < FIRST_PSEUDO_REGISTER)
2963     {
2964       /* Check for setting an eliminable register.  */
2965       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2966         if (ep->from_rtx == SET_DEST (old_body) && ep->can_eliminate)
2967           {
2968             /* In this case this insn isn't serving a useful purpose.  We
2969                will delete it in reload_as_needed once we know that this
2970                elimination is, in fact, being done.
2971
2972                If REPLACE isn't set, we can't delete this insn, but neededn't
2973                process it since it won't be used unless something changes.  */
2974             if (replace)
2975               delete_dead_insn (insn);
2976             val = 1;
2977             goto done;
2978           }
2979
2980       /* Check for (set (reg) (plus (reg from) (offset))) where the offset
2981          in the insn is the negative of the offset in FROM.  Substitute
2982          (set (reg) (reg to)) for the insn and change its code.
2983
2984          We have to do this here, rather than in eliminate_regs, do that we can
2985          change the insn code.  */
2986
2987       if (GET_CODE (SET_SRC (old_body)) == PLUS
2988           && GET_CODE (XEXP (SET_SRC (old_body), 0)) == REG
2989           && GET_CODE (XEXP (SET_SRC (old_body), 1)) == CONST_INT)
2990         for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2991              ep++)
2992           if (ep->from_rtx == XEXP (SET_SRC (old_body), 0)
2993               && ep->can_eliminate
2994               && ep->offset == - INTVAL (XEXP (SET_SRC (old_body), 1)))
2995             {
2996               PATTERN (insn) = gen_rtx (SET, VOIDmode,
2997                                         SET_DEST (old_body), ep->to_rtx);
2998               INSN_CODE (insn) = -1;
2999               val = 1;
3000               goto done;
3001             }
3002     }
3003
3004   old_asm_operands_vec = 0;
3005
3006   /* Replace the body of this insn with a substituted form.  If we changed
3007      something, return non-zero.  If this is the final call for this
3008      insn (REPLACE is non-zero), do the elimination in REG_NOTES as well.
3009
3010      If we are replacing a body that was a (set X (plus Y Z)), try to
3011      re-recognize the insn.  We do this in case we had a simple addition
3012      but now can do this as a load-address.  This saves an insn in this
3013      common case. */
3014
3015   new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
3016   if (new_body != old_body)
3017     {
3018       /* If we aren't replacing things permanently and we changed something,
3019          make another copy to ensure that all the RTL is new.  Otherwise
3020          things can go wrong if find_reload swaps commutative operands
3021          and one is inside RTL that has been copied while the other is not. */
3022
3023       /* Don't copy an asm_operands because (1) there's no need and (2)
3024          copy_rtx can't do it properly when there are multiple outputs.  */
3025       if (! replace && asm_noperands (old_body) < 0)
3026         new_body = copy_rtx (new_body);
3027
3028       /* If we had a move insn but now we don't, rerecognize it.  */
3029       if ((GET_CODE (old_body) == SET && GET_CODE (SET_SRC (old_body)) == REG
3030            && (GET_CODE (new_body) != SET
3031                || GET_CODE (SET_SRC (new_body)) != REG))
3032           /* If this was an add insn before, rerecognize.  */
3033           ||
3034           (GET_CODE (old_body) == SET
3035            && GET_CODE (SET_SRC (old_body)) == PLUS))
3036         {
3037           if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3038             /* If recognition fails, store the new body anyway.
3039                It's normal to have recognition failures here
3040                due to bizarre memory addresses; reloading will fix them.  */
3041             PATTERN (insn) = new_body;
3042         }
3043       else
3044         PATTERN (insn) = new_body;
3045
3046       if (replace && REG_NOTES (insn))
3047         REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, NULL_RTX);
3048       val = 1;
3049     }
3050
3051   /* Loop through all elimination pairs.  See if any have changed and
3052      recalculate the number not at initial offset.
3053
3054      Compute the maximum offset (minimum offset if the stack does not
3055      grow downward) for each elimination pair.
3056
3057      We also detect a cases where register elimination cannot be done,
3058      namely, if a register would be both changed and referenced outside a MEM
3059      in the resulting insn since such an insn is often undefined and, even if
3060      not, we cannot know what meaning will be given to it.  Note that it is
3061      valid to have a register used in an address in an insn that changes it
3062      (presumably with a pre- or post-increment or decrement).
3063
3064      If anything changes, return nonzero.  */
3065
3066   num_not_at_initial_offset = 0;
3067   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3068     {
3069       if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3070         ep->can_eliminate = 0;
3071
3072       ep->ref_outside_mem = 0;
3073
3074       if (ep->previous_offset != ep->offset)
3075         val = 1;
3076
3077       ep->previous_offset = ep->offset;
3078       if (ep->can_eliminate && ep->offset != ep->initial_offset)
3079         num_not_at_initial_offset++;
3080
3081 #ifdef STACK_GROWS_DOWNWARD
3082       ep->max_offset = MAX (ep->max_offset, ep->offset);
3083 #else
3084       ep->max_offset = MIN (ep->max_offset, ep->offset);
3085 #endif
3086     }
3087
3088  done:
3089   if (! replace)
3090     pop_obstacks ();
3091
3092   return val;
3093 }
3094
3095 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3096    replacement we currently believe is valid, mark it as not eliminable if X
3097    modifies DEST in any way other than by adding a constant integer to it.
3098
3099    If DEST is the frame pointer, we do nothing because we assume that
3100    all assignments to the frame pointer are nonlocal gotos and are being done
3101    at a time when they are valid and do not disturb anything else.
3102    Some machines want to eliminate a fake argument pointer with either the
3103    frame or stack pointer.  Assignments to the frame pointer must not prevent
3104    this elimination.
3105
3106    Called via note_stores from reload before starting its passes to scan
3107    the insns of the function.  */
3108
3109 static void
3110 mark_not_eliminable (dest, x)
3111      rtx dest;
3112      rtx x;
3113 {
3114   register int i;
3115
3116   /* A SUBREG of a hard register here is just changing its mode.  We should
3117      not see a SUBREG of an eliminable hard register, but check just in
3118      case.  */
3119   if (GET_CODE (dest) == SUBREG)
3120     dest = SUBREG_REG (dest);
3121
3122   if (dest == frame_pointer_rtx)
3123     return;
3124
3125   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3126     if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3127         && (GET_CODE (x) != SET
3128             || GET_CODE (SET_SRC (x)) != PLUS
3129             || XEXP (SET_SRC (x), 0) != dest
3130             || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3131       {
3132         reg_eliminate[i].can_eliminate_previous
3133           = reg_eliminate[i].can_eliminate = 0;
3134         num_eliminable--;
3135       }
3136 }
3137 \f
3138 /* Kick all pseudos out of hard register REGNO.
3139    If GLOBAL is nonzero, try to find someplace else to put them.
3140    If DUMPFILE is nonzero, log actions taken on that file.
3141
3142    If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3143    because we found we can't eliminate some register.  In the case, no pseudos
3144    are allowed to be in the register, even if they are only in a block that
3145    doesn't require spill registers, unlike the case when we are spilling this
3146    hard reg to produce another spill register.
3147
3148    Return nonzero if any pseudos needed to be kicked out.  */
3149
3150 static int
3151 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3152      register int regno;
3153      int global;
3154      FILE *dumpfile;
3155      int cant_eliminate;
3156 {
3157   int something_changed = 0;
3158   register int i;
3159
3160   SET_HARD_REG_BIT (forbidden_regs, regno);
3161
3162   /* Spill every pseudo reg that was allocated to this reg
3163      or to something that overlaps this reg.  */
3164
3165   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3166     if (reg_renumber[i] >= 0
3167         && reg_renumber[i] <= regno
3168         && (reg_renumber[i]
3169             + HARD_REGNO_NREGS (reg_renumber[i],
3170                                 PSEUDO_REGNO_MODE (i))
3171             > regno))
3172       {
3173         enum reg_class class = REGNO_REG_CLASS (regno);
3174
3175         /* If this register belongs solely to a basic block which needed no
3176            spilling of any class that this register is contained in,
3177            leave it be, unless we are spilling this register because
3178            it was a hard register that can't be eliminated.   */
3179
3180         if (! cant_eliminate
3181             && basic_block_needs[0]
3182             && reg_basic_block[i] >= 0
3183             && basic_block_needs[(int) class][reg_basic_block[i]] == 0)
3184           {
3185             enum reg_class *p;
3186
3187             for (p = reg_class_superclasses[(int) class];
3188                  *p != LIM_REG_CLASSES; p++)
3189               if (basic_block_needs[(int) *p][reg_basic_block[i]] > 0)
3190                 break;
3191
3192             if (*p == LIM_REG_CLASSES)
3193               continue;
3194           }
3195
3196         /* Mark it as no longer having a hard register home.  */
3197         reg_renumber[i] = -1;
3198         /* We will need to scan everything again.  */
3199         something_changed = 1;
3200         if (global)
3201             retry_global_alloc (i, forbidden_regs);
3202
3203         alter_reg (i, regno);
3204         if (dumpfile)
3205           {
3206             if (reg_renumber[i] == -1)
3207               fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3208             else
3209               fprintf (dumpfile, " Register %d now in %d.\n\n",
3210                        i, reg_renumber[i]);
3211           }
3212       }
3213
3214   return something_changed;
3215 }
3216 \f
3217 /* Find all paradoxical subregs within X and update reg_max_ref_width.  */
3218
3219 static void
3220 scan_paradoxical_subregs (x)
3221      register rtx x;
3222 {
3223   register int i;
3224   register char *fmt;
3225   register enum rtx_code code = GET_CODE (x);
3226
3227   switch (code)
3228     {
3229     case CONST_INT:
3230     case CONST:
3231     case SYMBOL_REF:
3232     case LABEL_REF:
3233     case CONST_DOUBLE:
3234     case CC0:
3235     case PC:
3236     case REG:
3237     case USE:
3238     case CLOBBER:
3239       return;
3240
3241     case SUBREG:
3242       if (GET_CODE (SUBREG_REG (x)) == REG
3243           && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3244         reg_max_ref_width[REGNO (SUBREG_REG (x))]
3245           = GET_MODE_SIZE (GET_MODE (x));
3246       return;
3247     }
3248
3249   fmt = GET_RTX_FORMAT (code);
3250   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3251     {
3252       if (fmt[i] == 'e')
3253         scan_paradoxical_subregs (XEXP (x, i));
3254       else if (fmt[i] == 'E')
3255         {
3256           register int j;
3257           for (j = XVECLEN (x, i) - 1; j >=0; j--)
3258             scan_paradoxical_subregs (XVECEXP (x, i, j));
3259         }
3260     }
3261 }
3262 \f
3263 struct hard_reg_n_uses { int regno; int uses; };
3264
3265 static int
3266 hard_reg_use_compare (p1, p2)
3267      struct hard_reg_n_uses *p1, *p2;
3268 {
3269   int tem = p1->uses - p2->uses;
3270   if (tem != 0) return tem;
3271   /* If regs are equally good, sort by regno,
3272      so that the results of qsort leave nothing to chance.  */
3273   return p1->regno - p2->regno;
3274 }
3275
3276 /* Choose the order to consider regs for use as reload registers
3277    based on how much trouble would be caused by spilling one.
3278    Store them in order of decreasing preference in potential_reload_regs.  */
3279
3280 static void
3281 order_regs_for_reload ()
3282 {
3283   register int i;
3284   register int o = 0;
3285   int large = 0;
3286
3287   struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3288
3289   CLEAR_HARD_REG_SET (bad_spill_regs);
3290
3291   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3292     potential_reload_regs[i] = -1;
3293
3294   /* Count number of uses of each hard reg by pseudo regs allocated to it
3295      and then order them by decreasing use.  */
3296
3297   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3298     {
3299       hard_reg_n_uses[i].uses = 0;
3300       hard_reg_n_uses[i].regno = i;
3301     }
3302
3303   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3304     {
3305       int regno = reg_renumber[i];
3306       if (regno >= 0)
3307         {
3308           int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3309           while (regno < lim)
3310             hard_reg_n_uses[regno++].uses += reg_n_refs[i];
3311         }
3312       large += reg_n_refs[i];
3313     }
3314
3315   /* Now fixed registers (which cannot safely be used for reloading)
3316      get a very high use count so they will be considered least desirable.
3317      Registers used explicitly in the rtl code are almost as bad.  */
3318
3319   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3320     {
3321       if (fixed_regs[i])
3322         {
3323           hard_reg_n_uses[i].uses += 2 * large + 2;
3324           SET_HARD_REG_BIT (bad_spill_regs, i);
3325         }
3326       else if (regs_explicitly_used[i])
3327         {
3328           hard_reg_n_uses[i].uses += large + 1;
3329           /* ??? We are doing this here because of the potential that
3330              bad code may be generated if a register explicitly used in
3331              an insn was used as a spill register for that insn.  But
3332              not using these are spill registers may lose on some machine.
3333              We'll have to see how this works out.  */
3334           SET_HARD_REG_BIT (bad_spill_regs, i);
3335         }
3336     }
3337   hard_reg_n_uses[FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3338   SET_HARD_REG_BIT (bad_spill_regs, FRAME_POINTER_REGNUM);
3339
3340 #ifdef ELIMINABLE_REGS
3341   /* If registers other than the frame pointer are eliminable, mark them as
3342      poor choices.  */
3343   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3344     {
3345       hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3346       SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3347     }
3348 #endif
3349
3350   /* Prefer registers not so far used, for use in temporary loading.
3351      Among them, if REG_ALLOC_ORDER is defined, use that order.
3352      Otherwise, prefer registers not preserved by calls.  */
3353
3354 #ifdef REG_ALLOC_ORDER
3355   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3356     {
3357       int regno = reg_alloc_order[i];
3358
3359       if (hard_reg_n_uses[regno].uses == 0)
3360         potential_reload_regs[o++] = regno;
3361     }
3362 #else
3363   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3364     {
3365       if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3366         potential_reload_regs[o++] = i;
3367     }
3368   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3369     {
3370       if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3371         potential_reload_regs[o++] = i;
3372     }
3373 #endif
3374
3375   qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3376          sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3377
3378   /* Now add the regs that are already used,
3379      preferring those used less often.  The fixed and otherwise forbidden
3380      registers will be at the end of this list.  */
3381
3382   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3383     if (hard_reg_n_uses[i].uses != 0)
3384       potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3385 }
3386 \f
3387 /* Reload pseudo-registers into hard regs around each insn as needed.
3388    Additional register load insns are output before the insn that needs it
3389    and perhaps store insns after insns that modify the reloaded pseudo reg.
3390
3391    reg_last_reload_reg and reg_reloaded_contents keep track of
3392    which pseudo-registers are already available in reload registers.
3393    We update these for the reloads that we perform,
3394    as the insns are scanned.  */
3395
3396 static void
3397 reload_as_needed (first, live_known)
3398      rtx first;
3399      int live_known;
3400 {
3401   register rtx insn;
3402   register int i;
3403   int this_block = 0;
3404   rtx x;
3405   rtx after_call = 0;
3406
3407   bzero (spill_reg_rtx, sizeof spill_reg_rtx);
3408   reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3409   bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
3410   reg_has_output_reload = (char *) alloca (max_regno);
3411   for (i = 0; i < n_spills; i++)
3412     {
3413       reg_reloaded_contents[i] = -1;
3414       reg_reloaded_insn[i] = 0;
3415     }
3416
3417   /* Reset all offsets on eliminable registers to their initial values.  */
3418 #ifdef ELIMINABLE_REGS
3419   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3420     {
3421       INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
3422                                   reg_eliminate[i].initial_offset);
3423       reg_eliminate[i].previous_offset
3424         = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3425     }
3426 #else
3427   INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3428   reg_eliminate[0].previous_offset
3429     = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3430 #endif
3431
3432   num_not_at_initial_offset = 0;
3433
3434   for (insn = first; insn;)
3435     {
3436       register rtx next = NEXT_INSN (insn);
3437
3438       /* Notice when we move to a new basic block.  */
3439       if (live_known && this_block + 1 < n_basic_blocks
3440           && insn == basic_block_head[this_block+1])
3441         ++this_block;
3442
3443       /* If we pass a label, copy the offsets from the label information
3444          into the current offsets of each elimination.  */
3445       if (GET_CODE (insn) == CODE_LABEL)
3446         {
3447           num_not_at_initial_offset = 0;
3448           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3449             {
3450               reg_eliminate[i].offset = reg_eliminate[i].previous_offset
3451                 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
3452               if (reg_eliminate[i].can_eliminate
3453                   && (reg_eliminate[i].offset
3454                       != reg_eliminate[i].initial_offset))
3455                 num_not_at_initial_offset++;
3456             }
3457         }
3458
3459       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3460         {
3461           rtx avoid_return_reg = 0;
3462
3463 #ifdef SMALL_REGISTER_CLASSES
3464           /* Set avoid_return_reg if this is an insn
3465              that might use the value of a function call.  */
3466           if (GET_CODE (insn) == CALL_INSN)
3467             {
3468               if (GET_CODE (PATTERN (insn)) == SET)
3469                 after_call = SET_DEST (PATTERN (insn));
3470               else if (GET_CODE (PATTERN (insn)) == PARALLEL
3471                        && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
3472                 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
3473               else
3474                 after_call = 0;
3475             }
3476           else if (after_call != 0
3477                    && !(GET_CODE (PATTERN (insn)) == SET
3478                         && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
3479             {
3480               if (reg_mentioned_p (after_call, PATTERN (insn)))
3481                 avoid_return_reg = after_call;
3482               after_call = 0;
3483             }
3484 #endif /* SMALL_REGISTER_CLASSES */
3485
3486           /* If this is a USE and CLOBBER of a MEM, ensure that any
3487              references to eliminable registers have been removed.  */
3488
3489           if ((GET_CODE (PATTERN (insn)) == USE
3490                || GET_CODE (PATTERN (insn)) == CLOBBER)
3491               && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3492             XEXP (XEXP (PATTERN (insn), 0), 0)
3493               = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3494                                 GET_MODE (XEXP (PATTERN (insn), 0)), NULL_RTX);
3495
3496           /* If we need to do register elimination processing, do so.
3497              This might delete the insn, in which case we are done.  */
3498           if (num_eliminable && GET_MODE (insn) == QImode)
3499             {
3500               eliminate_regs_in_insn (insn, 1);
3501               if (GET_CODE (insn) == NOTE)
3502                 {
3503                   insn = next;
3504                   continue;
3505                 }
3506             }
3507
3508           if (GET_MODE (insn) == VOIDmode)
3509             n_reloads = 0;
3510           /* First find the pseudo regs that must be reloaded for this insn.
3511              This info is returned in the tables reload_... (see reload.h).
3512              Also modify the body of INSN by substituting RELOAD
3513              rtx's for those pseudo regs.  */
3514           else
3515             {
3516               bzero (reg_has_output_reload, max_regno);
3517               CLEAR_HARD_REG_SET (reg_is_output_reload);
3518
3519               find_reloads (insn, 1, spill_indirect_levels, live_known,
3520                             spill_reg_order);
3521             }
3522
3523           if (n_reloads > 0)
3524             {
3525               rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
3526               rtx p;
3527               int class;
3528
3529               /* If this block has not had spilling done for a
3530                  particular class, deactivate any optional reloads
3531                  of that class lest they try to use a spill-reg which isn't
3532                  available here.  If we have any non-optionals that need a
3533                  spill reg, abort.  */
3534
3535               for (class = 0; class < N_REG_CLASSES; class++)
3536                 if (basic_block_needs[class] != 0
3537                     && basic_block_needs[class][this_block] == 0)
3538                   for (i = 0; i < n_reloads; i++)
3539                     if (class == (int) reload_reg_class[i])
3540                       {
3541                         if (reload_optional[i])
3542                           {
3543                             reload_in[i] = reload_out[i] = 0;
3544                             reload_secondary_p[i] = 0;
3545                           }
3546                         else if (reload_reg_rtx[i] == 0
3547                                  && (reload_in[i] != 0 || reload_out[i] != 0
3548                                      || reload_secondary_p[i] != 0))
3549                           abort ();
3550                       }
3551
3552               /* Now compute which reload regs to reload them into.  Perhaps
3553                  reusing reload regs from previous insns, or else output
3554                  load insns to reload them.  Maybe output store insns too.
3555                  Record the choices of reload reg in reload_reg_rtx.  */
3556               choose_reload_regs (insn, avoid_return_reg);
3557
3558               /* Generate the insns to reload operands into or out of
3559                  their reload regs.  */
3560               emit_reload_insns (insn);
3561
3562               /* Substitute the chosen reload regs from reload_reg_rtx
3563                  into the insn's body (or perhaps into the bodies of other
3564                  load and store insn that we just made for reloading
3565                  and that we moved the structure into).  */
3566               subst_reloads ();
3567
3568               /* If this was an ASM, make sure that all the reload insns
3569                  we have generated are valid.  If not, give an error
3570                  and delete them.  */
3571
3572               if (asm_noperands (PATTERN (insn)) >= 0)
3573                 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3574                   if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
3575                       && (recog_memoized (p) < 0
3576                           || (insn_extract (p),
3577                               ! constrain_operands (INSN_CODE (p), 1))))
3578                     {
3579                       error_for_asm (insn,
3580                                      "`asm' operand requires impossible reload");
3581                       PUT_CODE (p, NOTE);
3582                       NOTE_SOURCE_FILE (p) = 0;
3583                       NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3584                     }
3585             }
3586           /* Any previously reloaded spilled pseudo reg, stored in this insn,
3587              is no longer validly lying around to save a future reload.
3588              Note that this does not detect pseudos that were reloaded
3589              for this insn in order to be stored in
3590              (obeying register constraints).  That is correct; such reload
3591              registers ARE still valid.  */
3592           note_stores (PATTERN (insn), forget_old_reloads_1);
3593
3594           /* There may have been CLOBBER insns placed after INSN.  So scan
3595              between INSN and NEXT and use them to forget old reloads.  */
3596           for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
3597             if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3598               note_stores (PATTERN (x), forget_old_reloads_1);
3599
3600 #ifdef AUTO_INC_DEC
3601           /* Likewise for regs altered by auto-increment in this insn.
3602              But note that the reg-notes are not changed by reloading:
3603              they still contain the pseudo-regs, not the spill regs.  */
3604           for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3605             if (REG_NOTE_KIND (x) == REG_INC)
3606               {
3607                 /* See if this pseudo reg was reloaded in this insn.
3608                    If so, its last-reload info is still valid
3609                    because it is based on this insn's reload.  */
3610                 for (i = 0; i < n_reloads; i++)
3611                   if (reload_out[i] == XEXP (x, 0))
3612                     break;
3613
3614                 if (i == n_reloads)
3615                   forget_old_reloads_1 (XEXP (x, 0));
3616               }
3617 #endif
3618         }
3619       /* A reload reg's contents are unknown after a label.  */
3620       if (GET_CODE (insn) == CODE_LABEL)
3621         for (i = 0; i < n_spills; i++)
3622           {
3623             reg_reloaded_contents[i] = -1;
3624             reg_reloaded_insn[i] = 0;
3625           }
3626
3627       /* Don't assume a reload reg is still good after a call insn
3628          if it is a call-used reg.  */
3629       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
3630         for (i = 0; i < n_spills; i++)
3631           if (call_used_regs[spill_regs[i]])
3632             {
3633               reg_reloaded_contents[i] = -1;
3634               reg_reloaded_insn[i] = 0;
3635             }
3636
3637       /* In case registers overlap, allow certain insns to invalidate
3638          particular hard registers.  */
3639
3640 #ifdef INSN_CLOBBERS_REGNO_P
3641       for (i = 0 ; i < n_spills ; i++)
3642         if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
3643           {
3644             reg_reloaded_contents[i] = -1;
3645             reg_reloaded_insn[i] = 0;
3646           }
3647 #endif
3648
3649       insn = next;
3650
3651 #ifdef USE_C_ALLOCA
3652       alloca (0);
3653 #endif
3654     }
3655 }
3656
3657 /* Discard all record of any value reloaded from X,
3658    or reloaded in X from someplace else;
3659    unless X is an output reload reg of the current insn.
3660
3661    X may be a hard reg (the reload reg)
3662    or it may be a pseudo reg that was reloaded from.  */
3663
3664 static void
3665 forget_old_reloads_1 (x)
3666      rtx x;
3667 {
3668   register int regno;
3669   int nr;
3670   int offset = 0;
3671
3672   /* note_stores does give us subregs of hard regs.  */
3673   while (GET_CODE (x) == SUBREG)
3674     {
3675       offset += SUBREG_WORD (x);
3676       x = SUBREG_REG (x);
3677     }
3678
3679   if (GET_CODE (x) != REG)
3680     return;
3681
3682   regno = REGNO (x) + offset;
3683
3684   if (regno >= FIRST_PSEUDO_REGISTER)
3685     nr = 1;
3686   else
3687     {
3688       int i;
3689       nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
3690       /* Storing into a spilled-reg invalidates its contents.
3691          This can happen if a block-local pseudo is allocated to that reg
3692          and it wasn't spilled because this block's total need is 0.
3693          Then some insn might have an optional reload and use this reg.  */
3694       for (i = 0; i < nr; i++)
3695         if (spill_reg_order[regno + i] >= 0
3696             /* But don't do this if the reg actually serves as an output
3697                reload reg in the current instruction.  */
3698             && (n_reloads == 0
3699                 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
3700           {
3701             reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
3702             reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
3703           }
3704     }
3705
3706   /* Since value of X has changed,
3707      forget any value previously copied from it.  */
3708
3709   while (nr-- > 0)
3710     /* But don't forget a copy if this is the output reload
3711        that establishes the copy's validity.  */
3712     if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
3713       reg_last_reload_reg[regno + nr] = 0;
3714 }
3715 \f
3716 /* For each reload, the mode of the reload register.  */
3717 static enum machine_mode reload_mode[MAX_RELOADS];
3718
3719 /* For each reload, the largest number of registers it will require.  */
3720 static int reload_nregs[MAX_RELOADS];
3721
3722 /* Comparison function for qsort to decide which of two reloads
3723    should be handled first.  *P1 and *P2 are the reload numbers.  */
3724
3725 static int
3726 reload_reg_class_lower (p1, p2)
3727      short *p1, *p2;
3728 {
3729   register int r1 = *p1, r2 = *p2;
3730   register int t;
3731
3732   /* Consider required reloads before optional ones.  */
3733   t = reload_optional[r1] - reload_optional[r2];
3734   if (t != 0)
3735     return t;
3736
3737   /* Count all solitary classes before non-solitary ones.  */
3738   t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
3739        - (reg_class_size[(int) reload_reg_class[r1]] == 1));
3740   if (t != 0)
3741     return t;
3742
3743   /* Aside from solitaires, consider all multi-reg groups first.  */
3744   t = reload_nregs[r2] - reload_nregs[r1];
3745   if (t != 0)
3746     return t;
3747
3748   /* Consider reloads in order of increasing reg-class number.  */
3749   t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
3750   if (t != 0)
3751     return t;
3752
3753   /* If reloads are equally urgent, sort by reload number,
3754      so that the results of qsort leave nothing to chance.  */
3755   return r1 - r2;
3756 }
3757 \f
3758 /* The following HARD_REG_SETs indicate when each hard register is
3759    used for a reload of various parts of the current insn.  */
3760
3761 /* If reg is in use as a reload reg for a RELOAD_OTHER reload.  */
3762 static HARD_REG_SET reload_reg_used;
3763 /* If reg is in use for a RELOAD_FOR_INPUT_RELOAD_ADDRESS reload.  */
3764 static HARD_REG_SET reload_reg_used_in_input_addr;
3765 /* If reg is in use for a RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reload.  */
3766 static HARD_REG_SET reload_reg_used_in_output_addr;
3767 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload.  */
3768 static HARD_REG_SET reload_reg_used_in_op_addr;
3769 /* If reg is in use for a RELOAD_FOR_INPUT reload.  */
3770 static HARD_REG_SET reload_reg_used_in_input;
3771 /* If reg is in use for a RELOAD_FOR_OUTPUT reload.  */
3772 static HARD_REG_SET reload_reg_used_in_output;
3773
3774 /* If reg is in use as a reload reg for any sort of reload.  */
3775 static HARD_REG_SET reload_reg_used_at_all;
3776
3777 /* Mark reg REGNO as in use for a reload of the sort spec'd by WHEN_NEEDED.
3778    MODE is used to indicate how many consecutive regs are actually used.  */
3779
3780 static void
3781 mark_reload_reg_in_use (regno, when_needed, mode)
3782      int regno;
3783      enum reload_when_needed when_needed;
3784      enum machine_mode mode;
3785 {
3786   int nregs = HARD_REGNO_NREGS (regno, mode);
3787   int i;
3788
3789   for (i = regno; i < nregs + regno; i++)
3790     {
3791       switch (when_needed)
3792         {
3793         case RELOAD_OTHER:
3794           SET_HARD_REG_BIT (reload_reg_used, i);
3795           break;
3796
3797         case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3798           SET_HARD_REG_BIT (reload_reg_used_in_input_addr, i);
3799           break;
3800
3801         case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3802           SET_HARD_REG_BIT (reload_reg_used_in_output_addr, i);
3803           break;
3804
3805         case RELOAD_FOR_OPERAND_ADDRESS:
3806           SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
3807           break;
3808
3809         case RELOAD_FOR_INPUT:
3810           SET_HARD_REG_BIT (reload_reg_used_in_input, i);
3811           break;
3812
3813         case RELOAD_FOR_OUTPUT:
3814           SET_HARD_REG_BIT (reload_reg_used_in_output, i);
3815           break;
3816         }
3817
3818       SET_HARD_REG_BIT (reload_reg_used_at_all, i);
3819     }
3820 }
3821
3822 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
3823    specified by WHEN_NEEDED.  */
3824
3825 static int
3826 reload_reg_free_p (regno, when_needed)
3827      int regno;
3828      enum reload_when_needed when_needed;
3829 {
3830   /* In use for a RELOAD_OTHER means it's not available for anything.  */
3831   if (TEST_HARD_REG_BIT (reload_reg_used, regno))
3832     return 0;
3833   switch (when_needed)
3834     {
3835     case RELOAD_OTHER:
3836       /* In use for anything means not available for a RELOAD_OTHER.  */
3837       return ! TEST_HARD_REG_BIT (reload_reg_used_at_all, regno);
3838
3839       /* The other kinds of use can sometimes share a register.  */
3840     case RELOAD_FOR_INPUT:
3841       return (! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3842               && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3843               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno));
3844     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3845       return (! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno)
3846               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno));
3847     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3848       return (! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3849               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3850     case RELOAD_FOR_OPERAND_ADDRESS:
3851       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3852               && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3853               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3854     case RELOAD_FOR_OUTPUT:
3855       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3856               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3857               && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3858     }
3859   abort ();
3860 }
3861
3862 /* Return 1 if the value in reload reg REGNO, as used by a reload
3863    needed for the part of the insn specified by WHEN_NEEDED,
3864    is not in use for a reload in any prior part of the insn.
3865
3866    We can assume that the reload reg was already tested for availability
3867    at the time it is needed, and we should not check this again,
3868    in case the reg has already been marked in use.  */
3869
3870 static int
3871 reload_reg_free_before_p (regno, when_needed)
3872      int regno;
3873      enum reload_when_needed when_needed;
3874 {
3875   switch (when_needed)
3876     {
3877     case RELOAD_OTHER:
3878       /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3879          its use starts from the beginning, so nothing can use it earlier.  */
3880       return 1;
3881
3882       /* If this use is for part of the insn,
3883          check the reg is not in use for any prior part.  */
3884     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3885       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
3886         return 0;
3887     case RELOAD_FOR_OUTPUT:
3888       if (TEST_HARD_REG_BIT (reload_reg_used_in_input, regno))
3889         return 0;
3890     case RELOAD_FOR_OPERAND_ADDRESS:
3891       if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno))
3892         return 0;
3893     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3894     case RELOAD_FOR_INPUT:
3895       return 1;
3896     }
3897   abort ();
3898 }
3899
3900 /* Return 1 if the value in reload reg REGNO, as used by a reload
3901    needed for the part of the insn specified by WHEN_NEEDED,
3902    is still available in REGNO at the end of the insn.
3903
3904    We can assume that the reload reg was already tested for availability
3905    at the time it is needed, and we should not check this again,
3906    in case the reg has already been marked in use.  */
3907
3908 static int
3909 reload_reg_reaches_end_p (regno, when_needed)
3910      int regno;
3911      enum reload_when_needed when_needed;
3912 {
3913   switch (when_needed)
3914     {
3915     case RELOAD_OTHER:
3916       /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3917          its value must reach the end.  */
3918       return 1;
3919
3920       /* If this use is for part of the insn,
3921          its value reaches if no subsequent part uses the same register.  */
3922     case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3923     case RELOAD_FOR_INPUT:
3924       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3925           || TEST_HARD_REG_BIT (reload_reg_used_in_output, regno))
3926         return 0;
3927     case RELOAD_FOR_OPERAND_ADDRESS:
3928       if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno))
3929         return 0;
3930     case RELOAD_FOR_OUTPUT:
3931     case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3932       return 1;
3933     }
3934   abort ();
3935 }
3936 \f
3937 /* Vector of reload-numbers showing the order in which the reloads should
3938    be processed.  */
3939 short reload_order[MAX_RELOADS];
3940
3941 /* Indexed by reload number, 1 if incoming value
3942    inherited from previous insns.  */
3943 char reload_inherited[MAX_RELOADS];
3944
3945 /* For an inherited reload, this is the insn the reload was inherited from,
3946    if we know it.  Otherwise, this is 0.  */
3947 rtx reload_inheritance_insn[MAX_RELOADS];
3948
3949 /* If non-zero, this is a place to get the value of the reload,
3950    rather than using reload_in.  */
3951 rtx reload_override_in[MAX_RELOADS];
3952
3953 /* For each reload, the index in spill_regs of the spill register used,
3954    or -1 if we did not need one of the spill registers for this reload.  */
3955 int reload_spill_index[MAX_RELOADS];
3956
3957 /* Index of last register assigned as a spill register.  We allocate in
3958    a round-robin fashio.  */
3959
3960 static last_spill_reg = 0;
3961
3962 /* Find a spill register to use as a reload register for reload R.
3963    LAST_RELOAD is non-zero if this is the last reload for the insn being
3964    processed.
3965
3966    Set reload_reg_rtx[R] to the register allocated.
3967
3968    If NOERROR is nonzero, we return 1 if successful,
3969    or 0 if we couldn't find a spill reg and we didn't change anything.  */
3970
3971 static int
3972 allocate_reload_reg (r, insn, last_reload, noerror)
3973      int r;
3974      rtx insn;
3975      int last_reload;
3976      int noerror;
3977 {
3978   int i;
3979   int pass;
3980   int count;
3981   rtx new;
3982   int regno;
3983
3984   /* If we put this reload ahead, thinking it is a group,
3985      then insist on finding a group.  Otherwise we can grab a
3986      reg that some other reload needs.
3987      (That can happen when we have a 68000 DATA_OR_FP_REG
3988      which is a group of data regs or one fp reg.)
3989      We need not be so restrictive if there are no more reloads
3990      for this insn.
3991
3992      ??? Really it would be nicer to have smarter handling
3993      for that kind of reg class, where a problem like this is normal.
3994      Perhaps those classes should be avoided for reloading
3995      by use of more alternatives.  */
3996
3997   int force_group = reload_nregs[r] > 1 && ! last_reload;
3998
3999   /* If we want a single register and haven't yet found one,
4000      take any reg in the right class and not in use.
4001      If we want a consecutive group, here is where we look for it.
4002
4003      We use two passes so we can first look for reload regs to
4004      reuse, which are already in use for other reloads in this insn,
4005      and only then use additional registers.
4006      I think that maximizing reuse is needed to make sure we don't
4007      run out of reload regs.  Suppose we have three reloads, and
4008      reloads A and B can share regs.  These need two regs.
4009      Suppose A and B are given different regs.
4010      That leaves none for C.  */
4011   for (pass = 0; pass < 2; pass++)
4012     {
4013       /* I is the index in spill_regs.
4014          We advance it round-robin between insns to use all spill regs
4015          equally, so that inherited reloads have a chance
4016          of leapfrogging each other.  */
4017
4018       for (count = 0, i = last_spill_reg; count < n_spills; count++)
4019         {
4020           int class = (int) reload_reg_class[r];
4021
4022           i = (i + 1) % n_spills;
4023
4024           if (reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4025               && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
4026               && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4027               /* Look first for regs to share, then for unshared.  */
4028               && (pass || TEST_HARD_REG_BIT (reload_reg_used_at_all,
4029                                              spill_regs[i])))
4030             {
4031               int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4032               /* Avoid the problem where spilling a GENERAL_OR_FP_REG
4033                  (on 68000) got us two FP regs.  If NR is 1,
4034                  we would reject both of them.  */
4035               if (force_group)
4036                 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
4037               /* If we need only one reg, we have already won.  */
4038               if (nr == 1)
4039                 {
4040                   /* But reject a single reg if we demand a group.  */
4041                   if (force_group)
4042                     continue;
4043                   break;
4044                 }
4045               /* Otherwise check that as many consecutive regs as we need
4046                  are available here.
4047                  Also, don't use for a group registers that are
4048                  needed for nongroups.  */
4049               if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
4050                 while (nr > 1)
4051                   {
4052                     regno = spill_regs[i] + nr - 1;
4053                     if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
4054                           && spill_reg_order[regno] >= 0
4055                           && reload_reg_free_p (regno, reload_when_needed[r])
4056                           && ! TEST_HARD_REG_BIT (counted_for_nongroups,
4057                                                   regno)))
4058                       break;
4059                     nr--;
4060                   }
4061               if (nr == 1)
4062                 break;
4063             }
4064         }
4065
4066       /* If we found something on pass 1, omit pass 2.  */
4067       if (count < n_spills)
4068         break;
4069     }
4070
4071   /* We should have found a spill register by now.  */
4072   if (count == n_spills)
4073     {
4074       if (noerror)
4075         return 0;
4076       goto failure;
4077     }
4078
4079   last_spill_reg = i;
4080
4081   /* Mark as in use for this insn the reload regs we use for this.  */
4082   mark_reload_reg_in_use (spill_regs[i], reload_when_needed[r],
4083                           reload_mode[r]);
4084
4085   new = spill_reg_rtx[i];
4086
4087   if (new == 0 || GET_MODE (new) != reload_mode[r])
4088     spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode[r], spill_regs[i]);
4089
4090   reload_reg_rtx[r] = new;
4091   reload_spill_index[r] = i;
4092   regno = true_regnum (new);
4093
4094   /* Detect when the reload reg can't hold the reload mode.
4095      This used to be one `if', but Sequent compiler can't handle that.  */
4096   if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4097     {
4098       enum machine_mode test_mode = VOIDmode;
4099       if (reload_in[r])
4100         test_mode = GET_MODE (reload_in[r]);
4101       /* If reload_in[r] has VOIDmode, it means we will load it
4102          in whatever mode the reload reg has: to wit, reload_mode[r].
4103          We have already tested that for validity.  */
4104       /* Aside from that, we need to test that the expressions
4105          to reload from or into have modes which are valid for this
4106          reload register.  Otherwise the reload insns would be invalid.  */
4107       if (! (reload_in[r] != 0 && test_mode != VOIDmode
4108              && ! HARD_REGNO_MODE_OK (regno, test_mode)))
4109         if (! (reload_out[r] != 0
4110                && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
4111           /* The reg is OK.  */
4112           return 1;
4113     }
4114
4115   /* The reg is not OK.  */
4116   if (noerror)
4117     return 0;
4118
4119  failure:
4120   if (asm_noperands (PATTERN (insn)) < 0)
4121     /* It's the compiler's fault.  */
4122     abort ();
4123
4124   /* It's the user's fault; the operand's mode and constraint
4125      don't match.  Disable this reload so we don't crash in final.  */
4126   error_for_asm (insn,
4127                  "`asm' operand constraint incompatible with operand size");
4128   reload_in[r] = 0;
4129   reload_out[r] = 0;
4130   reload_reg_rtx[r] = 0;
4131   reload_optional[r] = 1;
4132   reload_secondary_p[r] = 1;
4133
4134   return 1;
4135 }
4136 \f
4137 /* Assign hard reg targets for the pseudo-registers we must reload
4138    into hard regs for this insn.
4139    Also output the instructions to copy them in and out of the hard regs.
4140
4141    For machines with register classes, we are responsible for
4142    finding a reload reg in the proper class.  */
4143
4144 static void
4145 choose_reload_regs (insn, avoid_return_reg)
4146      rtx insn;
4147      /* This argument is currently ignored.  */
4148      rtx avoid_return_reg;
4149 {
4150   register int i, j;
4151   int max_group_size = 1;
4152   enum reg_class group_class = NO_REGS;
4153   int inheritance;
4154
4155   rtx save_reload_reg_rtx[MAX_RELOADS];
4156   char save_reload_inherited[MAX_RELOADS];
4157   rtx save_reload_inheritance_insn[MAX_RELOADS];
4158   rtx save_reload_override_in[MAX_RELOADS];
4159   int save_reload_spill_index[MAX_RELOADS];
4160   HARD_REG_SET save_reload_reg_used;
4161   HARD_REG_SET save_reload_reg_used_in_input_addr;
4162   HARD_REG_SET save_reload_reg_used_in_output_addr;
4163   HARD_REG_SET save_reload_reg_used_in_op_addr;
4164   HARD_REG_SET save_reload_reg_used_in_input;
4165   HARD_REG_SET save_reload_reg_used_in_output;
4166   HARD_REG_SET save_reload_reg_used_at_all;
4167
4168   bzero (reload_inherited, MAX_RELOADS);
4169   bzero (reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
4170   bzero (reload_override_in, MAX_RELOADS * sizeof (rtx));
4171
4172   CLEAR_HARD_REG_SET (reload_reg_used);
4173   CLEAR_HARD_REG_SET (reload_reg_used_at_all);
4174   CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr);
4175   CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr);
4176   CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
4177   CLEAR_HARD_REG_SET (reload_reg_used_in_output);
4178   CLEAR_HARD_REG_SET (reload_reg_used_in_input);
4179
4180   /* Distinguish output-only and input-only reloads
4181      because they can overlap with other things.  */
4182   for (j = 0; j < n_reloads; j++)
4183     if (reload_when_needed[j] == RELOAD_OTHER
4184         && ! reload_needed_for_multiple[j])
4185       {
4186         if (reload_in[j] == 0)
4187           {
4188             /* But earlyclobber operands must stay as RELOAD_OTHER.  */
4189             for (i = 0; i < n_earlyclobbers; i++)
4190               {
4191                 if (GET_CODE (reload_earlyclobbers[i]) == SUBREG
4192                     && reg_overlap_mentioned_for_reload_p (reload_out[j],
4193                                     SUBREG_REG (reload_earlyclobbers[i])))
4194                   break;
4195                 if (rtx_equal_p (reload_out[j], reload_earlyclobbers[i]))
4196                   break;
4197               }
4198             if (i == n_earlyclobbers)
4199               reload_when_needed[j] = RELOAD_FOR_OUTPUT;
4200           }
4201         if (reload_out[j] == 0)
4202           reload_when_needed[j] = RELOAD_FOR_INPUT;
4203
4204         if (reload_secondary_reload[j] >= 0
4205             && ! reload_needed_for_multiple[reload_secondary_reload[j]])
4206           reload_when_needed[reload_secondary_reload[j]]
4207             = reload_when_needed[j];
4208       }
4209
4210 #ifdef SMALL_REGISTER_CLASSES
4211   /* Don't bother with avoiding the return reg
4212      if we have no mandatory reload that could use it.  */
4213   if (avoid_return_reg)
4214     {
4215       int do_avoid = 0;
4216       int regno = REGNO (avoid_return_reg);
4217       int nregs
4218         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4219       int r;
4220
4221       for (r = regno; r < regno + nregs; r++)
4222         if (spill_reg_order[r] >= 0)
4223           for (j = 0; j < n_reloads; j++)
4224             if (!reload_optional[j] && reload_reg_rtx[j] == 0
4225                 && (reload_in[j] != 0 || reload_out[j] != 0
4226                     || reload_secondary_p[j])
4227                 &&
4228                 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
4229               do_avoid = 1;
4230       if (!do_avoid)
4231         avoid_return_reg = 0;
4232     }
4233 #endif /* SMALL_REGISTER_CLASSES */
4234
4235 #if 0  /* Not needed, now that we can always retry without inheritance.  */
4236   /* See if we have more mandatory reloads than spill regs.
4237      If so, then we cannot risk optimizations that could prevent
4238      reloads from sharing one spill register.
4239
4240      Since we will try finding a better register than reload_reg_rtx
4241      unless it is equal to reload_in or reload_out, count such reloads.  */
4242
4243   {
4244     int tem = 0;
4245 #ifdef SMALL_REGISTER_CLASSES
4246     int tem = (avoid_return_reg != 0);
4247 #endif
4248     for (j = 0; j < n_reloads; j++)
4249       if (! reload_optional[j]
4250           && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
4251           && (reload_reg_rtx[j] == 0
4252               || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
4253                   && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
4254         tem++;
4255     if (tem > n_spills)
4256       must_reuse = 1;
4257   }
4258 #endif
4259
4260 #ifdef SMALL_REGISTER_CLASSES
4261   /* Don't use the subroutine call return reg for a reload
4262      if we are supposed to avoid it.  */
4263   if (avoid_return_reg)
4264     {
4265       int regno = REGNO (avoid_return_reg);
4266       int nregs
4267         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4268       int r;
4269
4270       for (r = regno; r < regno + nregs; r++)
4271         if (spill_reg_order[r] >= 0)
4272           SET_HARD_REG_BIT (reload_reg_used, r);
4273     }
4274 #endif /* SMALL_REGISTER_CLASSES */
4275
4276   /* In order to be certain of getting the registers we need,
4277      we must sort the reloads into order of increasing register class.
4278      Then our grabbing of reload registers will parallel the process
4279      that provided the reload registers.
4280
4281      Also note whether any of the reloads wants a consecutive group of regs.
4282      If so, record the maximum size of the group desired and what
4283      register class contains all the groups needed by this insn.  */
4284
4285   for (j = 0; j < n_reloads; j++)
4286     {
4287       reload_order[j] = j;
4288       reload_spill_index[j] = -1;
4289
4290       reload_mode[j]
4291         = (reload_strict_low[j] && reload_out[j]
4292            ? GET_MODE (SUBREG_REG (reload_out[j]))
4293            : (reload_inmode[j] == VOIDmode
4294               || (GET_MODE_SIZE (reload_outmode[j])
4295                   > GET_MODE_SIZE (reload_inmode[j])))
4296            ? reload_outmode[j] : reload_inmode[j]);
4297
4298       reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
4299
4300       if (reload_nregs[j] > 1)
4301         {
4302           max_group_size = MAX (reload_nregs[j], max_group_size);
4303           group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
4304         }
4305
4306       /* If we have already decided to use a certain register,
4307          don't use it in another way.  */
4308       if (reload_reg_rtx[j])
4309         mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]),
4310                                 reload_when_needed[j], reload_mode[j]);
4311     }
4312
4313   if (n_reloads > 1)
4314     qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
4315
4316   bcopy (reload_reg_rtx, save_reload_reg_rtx, sizeof reload_reg_rtx);
4317   bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
4318   bcopy (reload_inheritance_insn, save_reload_inheritance_insn,
4319          sizeof reload_inheritance_insn);
4320   bcopy (reload_override_in, save_reload_override_in,
4321          sizeof reload_override_in);
4322   bcopy (reload_spill_index, save_reload_spill_index,
4323          sizeof reload_spill_index);
4324   COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
4325   COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
4326   COPY_HARD_REG_SET (save_reload_reg_used_in_output,
4327                      reload_reg_used_in_output);
4328   COPY_HARD_REG_SET (save_reload_reg_used_in_input,
4329                      reload_reg_used_in_input);
4330   COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr,
4331                      reload_reg_used_in_input_addr);
4332   COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr,
4333                      reload_reg_used_in_output_addr);
4334   COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
4335                      reload_reg_used_in_op_addr);
4336
4337   /* If -O, try first with inheritance, then turning it off.
4338      If not -O, don't do inheritance.
4339      Using inheritance when not optimizing leads to paradoxes
4340      with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
4341      because one side of the comparison might be inherited.  */
4342
4343   for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
4344     {
4345       /* Process the reloads in order of preference just found.
4346          Beyond this point, subregs can be found in reload_reg_rtx.
4347
4348          This used to look for an existing reloaded home for all
4349          of the reloads, and only then perform any new reloads.
4350          But that could lose if the reloads were done out of reg-class order
4351          because a later reload with a looser constraint might have an old
4352          home in a register needed by an earlier reload with a tighter constraint.
4353
4354          To solve this, we make two passes over the reloads, in the order
4355          described above.  In the first pass we try to inherit a reload
4356          from a previous insn.  If there is a later reload that needs a
4357          class that is a proper subset of the class being processed, we must
4358          also allocate a spill register during the first pass.
4359
4360          Then make a second pass over the reloads to allocate any reloads
4361          that haven't been given registers yet.  */
4362
4363       for (j = 0; j < n_reloads; j++)
4364         {
4365           register int r = reload_order[j];
4366
4367           /* Ignore reloads that got marked inoperative.  */
4368           if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4369             continue;
4370
4371           /* If find_reloads chose a to use reload_in or reload_out as a reload
4372              register, we don't need to chose one.  Otherwise, try even if it found
4373              one since we might save an insn if we find the value lying around.  */
4374           if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
4375               && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
4376                   || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
4377             continue;
4378
4379 #if 0 /* No longer needed for correct operation.
4380          It might give better code, or might not; worth an experiment?  */
4381           /* If this is an optional reload, we can't inherit from earlier insns
4382              until we are sure that any non-optional reloads have been allocated.
4383              The following code takes advantage of the fact that optional reloads
4384              are at the end of reload_order.  */
4385           if (reload_optional[r] != 0)
4386             for (i = 0; i < j; i++)
4387               if ((reload_out[reload_order[i]] != 0
4388                    || reload_in[reload_order[i]] != 0
4389                    || reload_secondary_p[reload_order[i]])
4390                   && ! reload_optional[reload_order[i]]
4391                   && reload_reg_rtx[reload_order[i]] == 0)
4392                 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
4393 #endif
4394
4395           /* First see if this pseudo is already available as reloaded
4396              for a previous insn.  We cannot try to inherit for reloads
4397              that are smaller than the maximum number of registers needed
4398              for groups unless the register we would allocate cannot be used
4399              for the groups.
4400
4401              We could check here to see if this is a secondary reload for
4402              an object that is already in a register of the desired class.
4403              This would avoid the need for the secondary reload register.
4404              But this is complex because we can't easily determine what
4405              objects might want to be loaded via this reload.  So let a register
4406              be allocated here.  In `emit_reload_insns' we suppress one of the
4407              loads in the case described above.  */
4408
4409           if (inheritance)
4410             {
4411               register int regno = -1;
4412               enum machine_mode mode;
4413
4414               if (reload_in[r] == 0)
4415                 ;
4416               else if (GET_CODE (reload_in[r]) == REG)
4417                 {
4418                   regno = REGNO (reload_in[r]);
4419                   mode = GET_MODE (reload_in[r]);
4420                 }
4421               else if (GET_CODE (reload_in_reg[r]) == REG)
4422                 {
4423                   regno = REGNO (reload_in_reg[r]);
4424                   mode = GET_MODE (reload_in_reg[r]);
4425                 }
4426 #if 0
4427               /* This won't work, since REGNO can be a pseudo reg number.
4428                  Also, it takes much more hair to keep track of all the things
4429                  that can invalidate an inherited reload of part of a pseudoreg.  */
4430               else if (GET_CODE (reload_in[r]) == SUBREG
4431                        && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
4432                 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
4433 #endif
4434
4435               if (regno >= 0 && reg_last_reload_reg[regno] != 0)
4436                 {
4437                   i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
4438
4439                   if (reg_reloaded_contents[i] == regno
4440                       && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
4441                           >= GET_MODE_SIZE (mode))
4442                       && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4443                       && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4444                                             spill_regs[i])
4445                       && (reload_nregs[r] == max_group_size
4446                           || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
4447                                                   spill_regs[i]))
4448                       && reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4449                       && reload_reg_free_before_p (spill_regs[i],
4450                                                    reload_when_needed[r]))
4451                     {
4452                       /* If a group is needed, verify that all the subsequent
4453                          registers still have their values intact. */
4454                       int nr
4455                         = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4456                       int k;
4457
4458                       for (k = 1; k < nr; k++)
4459                         if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
4460                             != regno)
4461                           break;
4462
4463                       if (k == nr)
4464                         {
4465                           /* Mark the register as in use for this part of
4466                              the insn.  */
4467                           mark_reload_reg_in_use (spill_regs[i],
4468                                                   reload_when_needed[r],
4469                                                   reload_mode[r]);
4470                           reload_reg_rtx[r] = reg_last_reload_reg[regno];
4471                           reload_inherited[r] = 1;
4472                           reload_inheritance_insn[r] = reg_reloaded_insn[i];
4473                           reload_spill_index[r] = i;
4474                         }
4475                     }
4476                 }
4477             }
4478
4479           /* Here's another way to see if the value is already lying around.  */
4480           if (inheritance
4481               && reload_in[r] != 0
4482               && ! reload_inherited[r]
4483               && reload_out[r] == 0
4484               && (CONSTANT_P (reload_in[r])
4485                   || GET_CODE (reload_in[r]) == PLUS
4486                   || GET_CODE (reload_in[r]) == REG
4487                   || GET_CODE (reload_in[r]) == MEM)
4488               && (reload_nregs[r] == max_group_size
4489                   || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
4490             {
4491               register rtx equiv
4492                 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
4493                                   -1, NULL_PTR, 0, reload_mode[r]);
4494               int regno;
4495
4496               if (equiv != 0)
4497                 {
4498                   if (GET_CODE (equiv) == REG)
4499                     regno = REGNO (equiv);
4500                   else if (GET_CODE (equiv) == SUBREG)
4501                     {
4502                       regno = REGNO (SUBREG_REG (equiv));
4503                       if (regno < FIRST_PSEUDO_REGISTER)
4504                         regno += SUBREG_WORD (equiv);
4505                     }
4506                   else
4507                     abort ();
4508                 }
4509
4510               /* If we found a spill reg, reject it unless it is free
4511                  and of the desired class.  */
4512               if (equiv != 0
4513                   && ((spill_reg_order[regno] >= 0
4514                        && ! reload_reg_free_before_p (regno,
4515                                                       reload_when_needed[r]))
4516                       || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4517                                               regno)))
4518                 equiv = 0;
4519
4520               if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
4521                 equiv = 0;
4522
4523               if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4524                 equiv = 0;
4525
4526               /* We found a register that contains the value we need.
4527                  If this register is the same as an `earlyclobber' operand
4528                  of the current insn, just mark it as a place to reload from
4529                  since we can't use it as the reload register itself.  */
4530
4531               if (equiv != 0)
4532                 for (i = 0; i < n_earlyclobbers; i++)
4533                   if (reg_overlap_mentioned_for_reload_p (equiv,
4534                                                           reload_earlyclobbers[i]))
4535                     {
4536                       reload_override_in[r] = equiv;
4537                       equiv = 0;
4538                       break;
4539                     }
4540
4541               /* JRV: If the equiv register we have found is explicitly
4542                  clobbered in the current insn, mark but don't use, as above. */
4543
4544               if (equiv != 0 && regno_clobbered_p (regno, insn))
4545                 {
4546                   reload_override_in[r] = equiv;
4547                   equiv = 0;
4548                 }
4549
4550               /* If we found an equivalent reg, say no code need be generated
4551                  to load it, and use it as our reload reg.  */
4552               if (equiv != 0 && regno != FRAME_POINTER_REGNUM)
4553                 {
4554                   reload_reg_rtx[r] = equiv;
4555                   reload_inherited[r] = 1;
4556                   /* If it is a spill reg,
4557                      mark the spill reg as in use for this insn.  */
4558                   i = spill_reg_order[regno];
4559                   if (i >= 0)
4560                     mark_reload_reg_in_use (regno, reload_when_needed[r],
4561                                             reload_mode[r]);
4562                 }
4563             }
4564
4565           /* If we found a register to use already, or if this is an optional
4566              reload, we are done.  */
4567           if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
4568             continue;
4569
4570 #if 0 /* No longer needed for correct operation.  Might or might not
4571          give better code on the average.  Want to experiment?  */
4572
4573           /* See if there is a later reload that has a class different from our
4574              class that intersects our class or that requires less register
4575              than our reload.  If so, we must allocate a register to this
4576              reload now, since that reload might inherit a previous reload
4577              and take the only available register in our class.  Don't do this
4578              for optional reloads since they will force all previous reloads
4579              to be allocated.  Also don't do this for reloads that have been
4580              turned off.  */
4581
4582           for (i = j + 1; i < n_reloads; i++)
4583             {
4584               int s = reload_order[i];
4585
4586               if ((reload_in[s] == 0 && reload_out[s] == 0
4587                    && ! reload_secondary_p[s])
4588                   || reload_optional[s])
4589                 continue;
4590
4591               if ((reload_reg_class[s] != reload_reg_class[r]
4592                    && reg_classes_intersect_p (reload_reg_class[r],
4593                                                reload_reg_class[s]))
4594                   || reload_nregs[s] < reload_nregs[r])
4595               break;
4596             }
4597
4598           if (i == n_reloads)
4599             continue;
4600
4601           allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
4602 #endif
4603         }
4604
4605       /* Now allocate reload registers for anything non-optional that
4606          didn't get one yet.  */
4607       for (j = 0; j < n_reloads; j++)
4608         {
4609           register int r = reload_order[j];
4610
4611           /* Ignore reloads that got marked inoperative.  */
4612           if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4613             continue;
4614
4615           /* Skip reloads that already have a register allocated or are
4616              optional. */
4617           if (reload_reg_rtx[r] != 0 || reload_optional[r])
4618             continue;
4619
4620           if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
4621             break;
4622         }
4623
4624       /* If that loop got all the way, we have won.  */
4625       if (j == n_reloads)
4626         break;
4627
4628     fail:
4629       /* Loop around and try without any inheritance.  */
4630       /* First undo everything done by the failed attempt
4631          to allocate with inheritance.  */
4632       bcopy (save_reload_reg_rtx, reload_reg_rtx, sizeof reload_reg_rtx);
4633       bcopy (save_reload_inherited, reload_inherited, sizeof reload_inherited);
4634       bcopy (save_reload_inheritance_insn, reload_inheritance_insn,
4635              sizeof reload_inheritance_insn);
4636       bcopy (save_reload_override_in, reload_override_in,
4637              sizeof reload_override_in);
4638       bcopy (save_reload_spill_index, reload_spill_index,
4639              sizeof reload_spill_index);
4640       COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
4641       COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
4642       COPY_HARD_REG_SET (reload_reg_used_in_input,
4643                          save_reload_reg_used_in_input);
4644       COPY_HARD_REG_SET (reload_reg_used_in_output,
4645                          save_reload_reg_used_in_output);
4646       COPY_HARD_REG_SET (reload_reg_used_in_input_addr,
4647                          save_reload_reg_used_in_input_addr);
4648       COPY_HARD_REG_SET (reload_reg_used_in_output_addr,
4649                          save_reload_reg_used_in_output_addr);
4650       COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
4651                          save_reload_reg_used_in_op_addr);
4652     }
4653
4654   /* If we thought we could inherit a reload, because it seemed that
4655      nothing else wanted the same reload register earlier in the insn,
4656      verify that assumption, now that all reloads have been assigned.  */
4657
4658   for (j = 0; j < n_reloads; j++)
4659     {
4660       register int r = reload_order[j];
4661
4662       if (reload_inherited[r] && reload_reg_rtx[r] != 0
4663           && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
4664                                          reload_when_needed[r]))
4665         reload_inherited[r] = 0;
4666
4667       /* If we found a better place to reload from,
4668          validate it in the same fashion, if it is a reload reg.  */
4669       if (reload_override_in[r]
4670           && (GET_CODE (reload_override_in[r]) == REG
4671               || GET_CODE (reload_override_in[r]) == SUBREG))
4672         {
4673           int regno = true_regnum (reload_override_in[r]);
4674           if (spill_reg_order[regno] >= 0
4675               && ! reload_reg_free_before_p (regno, reload_when_needed[r]))
4676             reload_override_in[r] = 0;
4677         }
4678     }
4679
4680   /* Now that reload_override_in is known valid,
4681      actually override reload_in.  */
4682   for (j = 0; j < n_reloads; j++)
4683     if (reload_override_in[j])
4684       reload_in[j] = reload_override_in[j];
4685
4686   /* If this reload won't be done because it has been cancelled or is
4687      optional and not inherited, clear reload_reg_rtx so other
4688      routines (such as subst_reloads) don't get confused.  */
4689   for (j = 0; j < n_reloads; j++)
4690     if ((reload_optional[j] && ! reload_inherited[j])
4691         || (reload_in[j] == 0 && reload_out[j] == 0
4692             && ! reload_secondary_p[j]))
4693       reload_reg_rtx[j] = 0;
4694
4695   /* Record which pseudos and which spill regs have output reloads.  */
4696   for (j = 0; j < n_reloads; j++)
4697     {
4698       register int r = reload_order[j];
4699
4700       i = reload_spill_index[r];
4701
4702       /* I is nonneg if this reload used one of the spill regs.
4703          If reload_reg_rtx[r] is 0, this is an optional reload
4704          that we opted to ignore.  */
4705       if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
4706           && reload_reg_rtx[r] != 0)
4707         {
4708           register int nregno = REGNO (reload_out[r]);
4709           int nr = 1;
4710
4711           if (nregno < FIRST_PSEUDO_REGISTER)
4712             nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
4713
4714           while (--nr >= 0)
4715             reg_has_output_reload[nregno + nr] = 1;
4716
4717           if (i >= 0)
4718             {
4719               nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4720               while (--nr >= 0)
4721                 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
4722             }
4723
4724           if (reload_when_needed[r] != RELOAD_OTHER
4725               && reload_when_needed[r] != RELOAD_FOR_OUTPUT)
4726             abort ();
4727         }
4728     }
4729 }
4730 \f
4731 /* Output insns to reload values in and out of the chosen reload regs.  */
4732
4733 static void
4734 emit_reload_insns (insn)
4735      rtx insn;
4736 {
4737   register int j;
4738   rtx following_insn = NEXT_INSN (insn);
4739   rtx before_insn = insn;
4740   rtx first_output_reload_insn = NEXT_INSN (insn);
4741   rtx first_other_reload_insn = insn;
4742   rtx first_operand_address_reload_insn = insn;
4743   int special;
4744   /* Values to be put in spill_reg_store are put here first.  */
4745   rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
4746
4747   /* If this is a CALL_INSN preceded by USE insns, any reload insns
4748      must go in front of the first USE insn, not in front of INSN.  */
4749
4750   if (GET_CODE (insn) == CALL_INSN && GET_CODE (PREV_INSN (insn)) == INSN
4751       && GET_CODE (PATTERN (PREV_INSN (insn))) == USE)
4752     while (GET_CODE (PREV_INSN (before_insn)) == INSN
4753            && GET_CODE (PATTERN (PREV_INSN (before_insn))) == USE)
4754       first_other_reload_insn = first_operand_address_reload_insn
4755         = before_insn = PREV_INSN (before_insn);
4756
4757   /* Now output the instructions to copy the data into and out of the
4758      reload registers.  Do these in the order that the reloads were reported,
4759      since reloads of base and index registers precede reloads of operands
4760      and the operands may need the base and index registers reloaded.  */
4761
4762   for (j = 0; j < n_reloads; j++)
4763     {
4764       register rtx old;
4765       rtx oldequiv_reg = 0;
4766       rtx this_reload_insn = 0;
4767       rtx store_insn = 0;
4768
4769       old = reload_in[j];
4770       if (old != 0 && ! reload_inherited[j]
4771           && ! rtx_equal_p (reload_reg_rtx[j], old)
4772           && reload_reg_rtx[j] != 0)
4773         {
4774           register rtx reloadreg = reload_reg_rtx[j];
4775           rtx oldequiv = 0;
4776           enum machine_mode mode;
4777           rtx where;
4778           rtx reload_insn;
4779
4780           /* Determine the mode to reload in.
4781              This is very tricky because we have three to choose from.
4782              There is the mode the insn operand wants (reload_inmode[J]).
4783              There is the mode of the reload register RELOADREG.
4784              There is the intrinsic mode of the operand, which we could find
4785              by stripping some SUBREGs.
4786              It turns out that RELOADREG's mode is irrelevant:
4787              we can change that arbitrarily.
4788
4789              Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
4790              then the reload reg may not support QImode moves, so use SImode.
4791              If foo is in memory due to spilling a pseudo reg, this is safe,
4792              because the QImode value is in the least significant part of a
4793              slot big enough for a SImode.  If foo is some other sort of
4794              memory reference, then it is impossible to reload this case,
4795              so previous passes had better make sure this never happens.
4796
4797              Then consider a one-word union which has SImode and one of its
4798              members is a float, being fetched as (SUBREG:SF union:SI).
4799              We must fetch that as SFmode because we could be loading into
4800              a float-only register.  In this case OLD's mode is correct.
4801
4802              Consider an immediate integer: it has VOIDmode.  Here we need
4803              to get a mode from something else.
4804
4805              In some cases, there is a fourth mode, the operand's
4806              containing mode.  If the insn specifies a containing mode for
4807              this operand, it overrides all others.
4808
4809              I am not sure whether the algorithm here is always right,
4810              but it does the right things in those cases.  */
4811
4812           mode = GET_MODE (old);
4813           if (mode == VOIDmode)
4814             mode = reload_inmode[j];
4815           if (reload_strict_low[j])
4816             mode = GET_MODE (SUBREG_REG (reload_in[j]));
4817
4818 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4819           /* If we need a secondary register for this operation, see if
4820              the value is already in a register in that class.  Don't
4821              do this if the secondary register will be used as a scratch
4822              register.  */
4823
4824           if (reload_secondary_reload[j] >= 0
4825               && reload_secondary_icode[j] == CODE_FOR_nothing
4826               && optimize)
4827             oldequiv
4828               = find_equiv_reg (old, insn,
4829                                 reload_reg_class[reload_secondary_reload[j]],
4830                                 -1, NULL_PTR, 0, mode);
4831 #endif
4832
4833           /* If reloading from memory, see if there is a register
4834              that already holds the same value.  If so, reload from there.
4835              We can pass 0 as the reload_reg_p argument because
4836              any other reload has either already been emitted,
4837              in which case find_equiv_reg will see the reload-insn,
4838              or has yet to be emitted, in which case it doesn't matter
4839              because we will use this equiv reg right away.  */
4840
4841           if (oldequiv == 0 && optimize
4842               && (GET_CODE (old) == MEM
4843                   || (GET_CODE (old) == REG
4844                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
4845                       && reg_renumber[REGNO (old)] < 0)))
4846             oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
4847                                        -1, NULL_PTR, 0, mode);
4848
4849           if (oldequiv)
4850             {
4851               int regno = true_regnum (oldequiv);
4852
4853               /* If OLDEQUIV is a spill register, don't use it for this
4854                  if any other reload needs it at an earlier stage of this insn
4855                  or at this stage.  */
4856               if (spill_reg_order[regno] >= 0
4857                   && (! reload_reg_free_p (regno, reload_when_needed[j])
4858                       || ! reload_reg_free_before_p (regno,
4859                                                      reload_when_needed[j])))
4860                 oldequiv = 0;
4861
4862               /* If OLDEQUIV is not a spill register,
4863                  don't use it if any other reload wants it.  */
4864               if (spill_reg_order[regno] < 0)
4865                 {
4866                   int k;
4867                   for (k = 0; k < n_reloads; k++)
4868                     if (reload_reg_rtx[k] != 0 && k != j
4869                         && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
4870                                                                oldequiv))
4871                       {
4872                         oldequiv = 0;
4873                         break;
4874                       }
4875                 }
4876             }
4877
4878           if (oldequiv == 0)
4879             oldequiv = old;
4880           else if (GET_CODE (oldequiv) == REG)
4881             oldequiv_reg = oldequiv;
4882           else if (GET_CODE (oldequiv) == SUBREG)
4883             oldequiv_reg = SUBREG_REG (oldequiv);
4884
4885           /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
4886              then load RELOADREG from OLDEQUIV.  */
4887
4888           if (GET_MODE (reloadreg) != mode)
4889             reloadreg = gen_lowpart_common (mode, reloadreg);
4890           while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
4891             oldequiv = SUBREG_REG (oldequiv);
4892           if (GET_MODE (oldequiv) != VOIDmode
4893               && mode != GET_MODE (oldequiv))
4894             oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
4895
4896           /* Decide where to put reload insn for this reload.  */
4897           switch (reload_when_needed[j])
4898             {
4899             case RELOAD_FOR_INPUT:
4900             case RELOAD_OTHER:
4901               where = first_operand_address_reload_insn;
4902               break;
4903             case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
4904               where = first_other_reload_insn;
4905               break;
4906             case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
4907               where = first_output_reload_insn;
4908               break;
4909             case RELOAD_FOR_OPERAND_ADDRESS:
4910               where = before_insn;
4911             }
4912
4913           special = 0;
4914
4915           /* Auto-increment addresses must be reloaded in a special way.  */
4916           if (GET_CODE (oldequiv) == POST_INC
4917               || GET_CODE (oldequiv) == POST_DEC
4918               || GET_CODE (oldequiv) == PRE_INC
4919               || GET_CODE (oldequiv) == PRE_DEC)
4920             {
4921               /* We are not going to bother supporting the case where a
4922                  incremented register can't be copied directly from
4923                  OLDEQUIV since this seems highly unlikely.  */
4924               if (reload_secondary_reload[j] >= 0)
4925                 abort ();
4926               /* Prevent normal processing of this reload.  */
4927               special = 1;
4928               /* Output a special code sequence for this case.  */
4929               this_reload_insn
4930                 = inc_for_reload (reloadreg, oldequiv, reload_inc[j], where);
4931             }
4932
4933           /* If we are reloading a pseudo-register that was set by the previous
4934              insn, see if we can get rid of that pseudo-register entirely
4935              by redirecting the previous insn into our reload register.  */
4936
4937           else if (optimize && GET_CODE (old) == REG
4938                    && REGNO (old) >= FIRST_PSEUDO_REGISTER
4939                    && dead_or_set_p (insn, old)
4940                    /* This is unsafe if some other reload
4941                       uses the same reg first.  */
4942                    && (reload_when_needed[j] == RELOAD_OTHER
4943                        || reload_when_needed[j] == RELOAD_FOR_INPUT
4944                        || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS))
4945             {
4946               rtx temp = PREV_INSN (insn);
4947               while (temp && GET_CODE (temp) == NOTE)
4948                 temp = PREV_INSN (temp);
4949               if (temp
4950                   && GET_CODE (temp) == INSN
4951                   && GET_CODE (PATTERN (temp)) == SET
4952                   && SET_DEST (PATTERN (temp)) == old
4953                   /* Make sure we can access insn_operand_constraint.  */
4954                   && asm_noperands (PATTERN (temp)) < 0
4955                   /* This is unsafe if prev insn rejects our reload reg.  */
4956                   && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
4957                                                reloadreg)
4958                   /* This is unsafe if operand occurs more than once in current
4959                      insn.  Perhaps some occurrences aren't reloaded.  */
4960                   && count_occurrences (PATTERN (insn), old) == 1
4961                   /* Don't risk splitting a matching pair of operands.  */
4962                   && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
4963                 {
4964                   /* Store into the reload register instead of the pseudo.  */
4965                   SET_DEST (PATTERN (temp)) = reloadreg;
4966                   /* If these are the only uses of the pseudo reg,
4967                      pretend for GDB it lives in the reload reg we used.  */
4968                   if (reg_n_deaths[REGNO (old)] == 1
4969                       && reg_n_sets[REGNO (old)] == 1)
4970                     {
4971                       reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
4972                       alter_reg (REGNO (old), -1);
4973                     }
4974                   special = 1;
4975                 }
4976             }
4977
4978           /* We can't do that, so output an insn to load RELOADREG.
4979              Keep them in the following order:
4980              all reloads for input reload addresses,
4981              all reloads for ordinary input operands,
4982              all reloads for addresses of non-reloaded operands,
4983              the insn being reloaded,
4984              all reloads for addresses of output reloads,
4985              the output reloads.  */
4986           if (! special)
4987             {
4988 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4989               rtx second_reload_reg = 0;
4990               enum insn_code icode;
4991
4992               /* If we have a secondary reload, pick up the secondary register
4993                  and icode, if any.  If OLDEQUIV and OLD are different or
4994                  if this is an in-out reload, recompute whether or not we
4995                  still need a secondary register and what the icode should
4996                  be.  If we still need a secondary register and the class or
4997                  icode is different, go back to reloading from OLD if using
4998                  OLDEQUIV means that we got the wrong type of register.  We
4999                  cannot have different class or icode due to an in-out reload
5000                  because we don't make such reloads when both the input and
5001                  output need secondary reload registers.  */
5002
5003               if (reload_secondary_reload[j] >= 0)
5004                 {
5005                   int secondary_reload = reload_secondary_reload[j];
5006                   rtx real_oldequiv = oldequiv;
5007                   rtx real_old = old;
5008
5009                   /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
5010                      and similarly for OLD.
5011                      See comments in find_secondary_reload in reload.c.  */
5012                   if (GET_CODE (oldequiv) == REG
5013                       && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
5014                       && reg_equiv_mem[REGNO (oldequiv)] != 0)
5015                     real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
5016
5017                   if (GET_CODE (old) == REG
5018                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
5019                       && reg_equiv_mem[REGNO (old)] != 0)
5020                     real_old = reg_equiv_mem[REGNO (old)];
5021
5022                   second_reload_reg = reload_reg_rtx[secondary_reload];
5023                   icode = reload_secondary_icode[j];
5024
5025                   if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
5026                       || (reload_in[j] != 0 && reload_out[j] != 0))
5027                     {
5028                       enum reg_class new_class
5029                         = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
5030                                                         mode, real_oldequiv);
5031
5032                       if (new_class == NO_REGS)
5033                         second_reload_reg = 0;
5034                       else
5035                         {
5036                           enum insn_code new_icode;
5037                           enum machine_mode new_mode;
5038
5039                           if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
5040                                                    REGNO (second_reload_reg)))
5041                             oldequiv = old, real_oldequiv = real_old;
5042                           else
5043                             {
5044                               new_icode = reload_in_optab[(int) mode];
5045                               if (new_icode != CODE_FOR_nothing
5046                                   && ((insn_operand_predicate[(int) new_icode][0]
5047                                        && ! ((*insn_operand_predicate[(int) new_icode][0])
5048                                              (reloadreg, mode)))
5049                                       || (insn_operand_predicate[(int) new_icode][1]
5050                                           && ! ((*insn_operand_predicate[(int) new_icode][1])
5051                                                 (real_oldequiv, mode)))))
5052                                 new_icode = CODE_FOR_nothing;
5053
5054                               if (new_icode == CODE_FOR_nothing)
5055                                 new_mode = mode;
5056                               else
5057                                 new_mode = insn_operand_mode[new_icode][2];
5058
5059                               if (GET_MODE (second_reload_reg) != new_mode)
5060                                 {
5061                                   if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
5062                                                            new_mode))
5063                                     oldequiv = old, real_oldequiv = real_old;
5064                                   else
5065                                     second_reload_reg
5066                                       = gen_rtx (REG, new_mode,
5067                                                  REGNO (second_reload_reg));
5068                                 }
5069                             }
5070                         }
5071                     }
5072
5073                   /* If we still need a secondary reload register, check
5074                      to see if it is being used as a scratch or intermediate
5075                      register and generate code appropriately.  If we need
5076                      a scratch register, use REAL_OLDEQUIV since the form of
5077                      the insn may depend on the actual address if it is 
5078                      a MEM.  */
5079
5080                   if (second_reload_reg)
5081                     {
5082                       if (icode != CODE_FOR_nothing)
5083                         {
5084                           reload_insn = emit_insn_before (GEN_FCN (icode)
5085                                                           (reloadreg,
5086                                                            real_oldequiv,
5087                                                            second_reload_reg),
5088                                                           where);
5089                           if (this_reload_insn == 0)
5090                             this_reload_insn = reload_insn;
5091                           special = 1;
5092                         }
5093                       else
5094                         {
5095                           /* See if we need a scratch register to load the
5096                              intermediate register (a tertiary reload).  */
5097                           enum insn_code tertiary_icode
5098                             = reload_secondary_icode[secondary_reload];
5099
5100                           if (tertiary_icode != CODE_FOR_nothing)
5101                             {
5102                               rtx third_reload_reg
5103                                 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5104
5105                               reload_insn
5106                                 = emit_insn_before ((GEN_FCN (tertiary_icode)
5107                                                      (second_reload_reg,
5108                                                       real_oldequiv,
5109                                                       third_reload_reg)),
5110                                                     where);
5111                               if (this_reload_insn == 0)
5112                                 this_reload_insn = reload_insn;
5113                             }
5114                           else
5115                             {
5116                               reload_insn
5117                                 = gen_input_reload (second_reload_reg,
5118                                                     oldequiv, where);
5119                               if (this_reload_insn == 0)
5120                                 this_reload_insn = reload_insn;
5121                               oldequiv = second_reload_reg;
5122                             }
5123                         }
5124                     }
5125                 }
5126 #endif
5127
5128               if (! special)
5129                 {
5130                   reload_insn = gen_input_reload (reloadreg, oldequiv, where);
5131                   if (this_reload_insn == 0)
5132                     this_reload_insn = reload_insn;
5133                 }
5134
5135 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
5136               /* We may have to make a REG_DEAD note for the secondary reload
5137                  register in the insns we just made.  Find the last insn that
5138                  mentioned the register.  */
5139               if (! special && second_reload_reg
5140                   && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
5141                 {
5142                   rtx prev;
5143
5144                   for (prev = where;
5145                        prev != PREV_INSN (this_reload_insn);
5146                        prev = PREV_INSN (prev))
5147                     if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
5148                         && reg_overlap_mentioned_for_reload_p (second_reload_reg,
5149                                                                PATTERN (prev)))
5150                       {
5151                         REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
5152                                                     second_reload_reg,
5153                                                     REG_NOTES (prev));
5154                         break;
5155                       }
5156                 }
5157 #endif
5158             }
5159
5160           /* Update where to put other reload insns.  */
5161           if (this_reload_insn)
5162             switch (reload_when_needed[j])
5163               {
5164               case RELOAD_FOR_INPUT:
5165               case RELOAD_OTHER:
5166                 if (first_other_reload_insn == first_operand_address_reload_insn)
5167                   first_other_reload_insn = this_reload_insn;
5168                 break;
5169               case RELOAD_FOR_OPERAND_ADDRESS:
5170                 if (first_operand_address_reload_insn == before_insn)
5171                   first_operand_address_reload_insn = this_reload_insn;
5172                 if (first_other_reload_insn == before_insn)
5173                   first_other_reload_insn = this_reload_insn;
5174               }
5175
5176           /* reload_inc[j] was formerly processed here.  */
5177         }
5178
5179       /* Add a note saying the input reload reg
5180          dies in this insn, if anyone cares.  */
5181 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5182       if (old != 0
5183           && reload_reg_rtx[j] != old
5184           && reload_reg_rtx[j] != 0
5185           && reload_out[j] == 0
5186           && ! reload_inherited[j]
5187           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
5188         {
5189           register rtx reloadreg = reload_reg_rtx[j];
5190
5191 #if 0
5192           /* We can't abort here because we need to support this for sched.c.
5193              It's not terrible to miss a REG_DEAD note, but we should try
5194              to figure out how to do this correctly.  */
5195           /* The code below is incorrect for address-only reloads.  */
5196           if (reload_when_needed[j] != RELOAD_OTHER
5197               && reload_when_needed[j] != RELOAD_FOR_INPUT)
5198             abort ();
5199 #endif
5200
5201           /* Add a death note to this insn, for an input reload.  */
5202
5203           if ((reload_when_needed[j] == RELOAD_OTHER
5204                || reload_when_needed[j] == RELOAD_FOR_INPUT)
5205               && ! dead_or_set_p (insn, reloadreg))
5206             REG_NOTES (insn)
5207               = gen_rtx (EXPR_LIST, REG_DEAD,
5208                          reloadreg, REG_NOTES (insn));
5209         }
5210
5211       /* When we inherit a reload, the last marked death of the reload reg
5212          may no longer really be a death.  */
5213       if (reload_reg_rtx[j] != 0
5214           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
5215           && reload_inherited[j])
5216         {
5217           /* Handle inheriting an output reload.
5218              Remove the death note from the output reload insn.  */
5219           if (reload_spill_index[j] >= 0
5220               && GET_CODE (reload_in[j]) == REG
5221               && spill_reg_store[reload_spill_index[j]] != 0
5222               && find_regno_note (spill_reg_store[reload_spill_index[j]],
5223                                   REG_DEAD, REGNO (reload_reg_rtx[j])))
5224             remove_death (REGNO (reload_reg_rtx[j]),
5225                           spill_reg_store[reload_spill_index[j]]);
5226           /* Likewise for input reloads that were inherited.  */
5227           else if (reload_spill_index[j] >= 0
5228                    && GET_CODE (reload_in[j]) == REG
5229                    && spill_reg_store[reload_spill_index[j]] == 0
5230                    && reload_inheritance_insn[j] != 0
5231                    && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
5232                                        REGNO (reload_reg_rtx[j])))
5233             remove_death (REGNO (reload_reg_rtx[j]),
5234                           reload_inheritance_insn[j]);
5235           else
5236             {
5237               rtx prev;
5238
5239               /* We got this register from find_equiv_reg.
5240                  Search back for its last death note and get rid of it.
5241                  But don't search back too far.
5242                  Don't go past a place where this reg is set,
5243                  since a death note before that remains valid.  */
5244               for (prev = PREV_INSN (insn);
5245                    prev && GET_CODE (prev) != CODE_LABEL;
5246                    prev = PREV_INSN (prev))
5247                 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5248                     && dead_or_set_p (prev, reload_reg_rtx[j]))
5249                   {
5250                     if (find_regno_note (prev, REG_DEAD,
5251                                          REGNO (reload_reg_rtx[j])))
5252                       remove_death (REGNO (reload_reg_rtx[j]), prev);
5253                     break;
5254                   }
5255             }
5256         }
5257
5258       /* We might have used find_equiv_reg above to choose an alternate
5259          place from which to reload.  If so, and it died, we need to remove
5260          that death and move it to one of the insns we just made.  */
5261
5262       if (oldequiv_reg != 0
5263           && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
5264         {
5265           rtx prev, prev1;
5266
5267           for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
5268                prev = PREV_INSN (prev))
5269             if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5270                 && dead_or_set_p (prev, oldequiv_reg))
5271               {
5272                 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
5273                   {
5274                     for (prev1 = this_reload_insn;
5275                          prev1; prev1 = PREV_INSN (prev1))
5276                       if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
5277                         && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
5278                                                                PATTERN (prev1)))
5279                       {
5280                         REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
5281                                                      oldequiv_reg,
5282                                                      REG_NOTES (prev1));
5283                         break;
5284                       }
5285                     remove_death (REGNO (oldequiv_reg), prev);
5286                   }
5287                 break;
5288               }
5289         }
5290 #endif
5291
5292       /* If we are reloading a register that was recently stored in with an
5293          output-reload, see if we can prove there was
5294          actually no need to store the old value in it.  */
5295
5296       if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
5297           /* This is unsafe if some other reload uses the same reg first.  */
5298           && (reload_when_needed[j] == RELOAD_OTHER
5299               || reload_when_needed[j] == RELOAD_FOR_INPUT
5300               || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS)
5301           && GET_CODE (reload_in[j]) == REG
5302 #if 0
5303           /* There doesn't seem to be any reason to restrict this to pseudos
5304              and doing so loses in the case where we are copying from a
5305              register of the wrong class.  */
5306           && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
5307 #endif
5308           && spill_reg_store[reload_spill_index[j]] != 0
5309           && dead_or_set_p (insn, reload_in[j])
5310           /* This is unsafe if operand occurs more than once in current
5311              insn.  Perhaps some occurrences weren't reloaded.  */
5312           && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
5313         delete_output_reload (insn, j,
5314                               spill_reg_store[reload_spill_index[j]]);
5315
5316       /* Input-reloading is done.  Now do output-reloading,
5317          storing the value from the reload-register after the main insn
5318          if reload_out[j] is nonzero.
5319
5320          ??? At some point we need to support handling output reloads of
5321          JUMP_INSNs or insns that set cc0.  */
5322       old = reload_out[j];
5323       if (old != 0
5324           && reload_reg_rtx[j] != old
5325           && reload_reg_rtx[j] != 0)
5326         {
5327           register rtx reloadreg = reload_reg_rtx[j];
5328           register rtx second_reloadreg = 0;
5329           rtx prev_insn = PREV_INSN (first_output_reload_insn);
5330           rtx note, p;
5331           enum machine_mode mode;
5332           int special = 0;
5333
5334           /* An output operand that dies right away does need a reload,
5335              but need not be copied from it.  Show the new location in the
5336              REG_UNUSED note.  */
5337           if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
5338               && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
5339             {
5340               XEXP (note, 0) = reload_reg_rtx[j];
5341               continue;
5342             }
5343           else if (GET_CODE (old) == SCRATCH)
5344             /* If we aren't optimizing, there won't be a REG_UNUSED note,
5345                but we don't want to make an output reload.  */
5346             continue;
5347
5348 #if 0
5349           /* Strip off of OLD any size-increasing SUBREGs such as
5350              (SUBREG:SI foo:QI 0).  */
5351
5352           while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
5353                  && (GET_MODE_SIZE (GET_MODE (old))
5354                      > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
5355             old = SUBREG_REG (old);
5356 #endif
5357
5358           /* If is a JUMP_INSN, we can't support output reloads yet.  */
5359           if (GET_CODE (insn) == JUMP_INSN)
5360             abort ();
5361
5362           /* Determine the mode to reload in.
5363              See comments above (for input reloading).  */
5364
5365           mode = GET_MODE (old);
5366           if (mode == VOIDmode)
5367             {
5368               /* VOIDmode should never happen for an output.  */
5369               if (asm_noperands (PATTERN (insn)) < 0)
5370                 /* It's the compiler's fault.  */
5371                 abort ();
5372               error_for_asm (insn, "output operand is constant in `asm'");
5373               /* Prevent crash--use something we know is valid.  */
5374               mode = word_mode;
5375               old = gen_rtx (REG, mode, REGNO (reloadreg));
5376             }
5377
5378           /* A strict-low-part output operand needs to be reloaded
5379              in the mode of the entire value.  */
5380           if (reload_strict_low[j])
5381             {
5382               mode = GET_MODE (SUBREG_REG (reload_out[j]));
5383               /* Encapsulate OLD into that mode.  */
5384               /* If OLD is a subreg, then strip it, since the subreg will
5385                  be altered by this very reload.  */
5386               while (GET_CODE (old) == SUBREG && GET_MODE (old) != mode)
5387                 old = SUBREG_REG (old);
5388               if (GET_MODE (old) != VOIDmode
5389                   && mode != GET_MODE (old))
5390                 old = gen_rtx (SUBREG, mode, old, 0);
5391             }
5392
5393           if (GET_MODE (reloadreg) != mode)
5394             reloadreg = gen_lowpart_common (mode, reloadreg);
5395
5396 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5397
5398           /* If we need two reload regs, set RELOADREG to the intermediate
5399              one, since it will be stored into OUT.  We might need a secondary
5400              register only for an input reload, so check again here.  */
5401
5402           if (reload_secondary_reload[j] >= 0)
5403             {
5404               rtx real_old = old;
5405
5406               if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
5407                   && reg_equiv_mem[REGNO (old)] != 0)
5408                 real_old = reg_equiv_mem[REGNO (old)];
5409
5410               if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
5411                                                  mode, real_old)
5412                   != NO_REGS))
5413                 {
5414                   second_reloadreg = reloadreg;
5415                   reloadreg = reload_reg_rtx[reload_secondary_reload[j]];
5416
5417                   /* See if RELOADREG is to be used as a scratch register
5418                      or as an intermediate register.  */
5419                   if (reload_secondary_icode[j] != CODE_FOR_nothing)
5420                     {
5421                       emit_insn_before ((GEN_FCN (reload_secondary_icode[j])
5422                                          (real_old, second_reloadreg,
5423                                           reloadreg)),
5424                                         first_output_reload_insn);
5425                       special = 1;
5426                     }
5427                   else
5428                     {
5429                       /* See if we need both a scratch and intermediate reload
5430                          register.  */
5431                       int secondary_reload = reload_secondary_reload[j];
5432                       enum insn_code tertiary_icode
5433                         = reload_secondary_icode[secondary_reload];
5434                       rtx pat;
5435
5436                       if (GET_MODE (reloadreg) != mode)
5437                         reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5438
5439                       if (tertiary_icode != CODE_FOR_nothing)
5440                         {
5441                           rtx third_reloadreg
5442                             = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5443                           pat = (GEN_FCN (tertiary_icode)
5444                                  (reloadreg, second_reloadreg, third_reloadreg));
5445                         }
5446 #ifdef SECONDARY_MEMORY_NEEDED
5447                       /* If we need a memory location to do the move, do it that way.  */
5448                       else if (GET_CODE (reloadreg) == REG
5449                                && REGNO (reloadreg) < FIRST_PSEUDO_REGISTER
5450                                && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (reloadreg)),
5451                                            REGNO_REG_CLASS (REGNO (second_reloadreg)),
5452                                            GET_MODE (second_reloadreg)))
5453                         {
5454                           /* Get the memory to use and rewrite both registers
5455                              to its mode.  */
5456                           rtx loc = get_secondary_mem (reloadreg,
5457                                                        GET_MODE (second_reloadreg));
5458                           rtx tmp_reloadreg;
5459                             
5460                           if (GET_MODE (loc) != GET_MODE (second_reloadreg))
5461                             second_reloadreg = gen_rtx (REG, GET_MODE (loc),
5462                                                         REGNO (second_reloadreg));
5463                           
5464                           if (GET_MODE (loc) != GET_MODE (reloadreg))
5465                             tmp_reloadreg = gen_rtx (REG, GET_MODE (loc),
5466                                                      REGNO (reloadreg));
5467                           else
5468                             tmp_reloadreg = reloadreg;
5469                           
5470                           emit_insn_before (gen_move_insn (loc, second_reloadreg),
5471                                             first_output_reload_insn);
5472                           pat = gen_move_insn (tmp_reloadreg, loc);
5473                         }
5474 #endif
5475                       else
5476                         pat = gen_move_insn (reloadreg, second_reloadreg);
5477
5478                       emit_insn_before (pat, first_output_reload_insn);
5479                     }
5480                 }
5481             }
5482 #endif
5483
5484           /* Output the last reload insn.  */
5485           if (! special)
5486             {
5487 #ifdef SECONDARY_MEMORY_NEEDED
5488               /* If we need a memory location to do the move, do it that way.  */
5489               if (GET_CODE (old) == REG && REGNO (old) < FIRST_PSEUDO_REGISTER
5490                   && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (old)),
5491                                               REGNO_REG_CLASS (REGNO (reloadreg)),
5492                                               GET_MODE (reloadreg)))
5493                 {
5494                   /* Get the memory to use and rewrite both registers to
5495                      its mode.  */
5496                   rtx loc = get_secondary_mem (old, GET_MODE (reloadreg));
5497
5498                   if (GET_MODE (loc) != GET_MODE (reloadreg))
5499                     reloadreg = gen_rtx (REG, GET_MODE (loc),
5500                                          REGNO (reloadreg));
5501
5502                   if (GET_MODE (loc) != GET_MODE (old))
5503                     old = gen_rtx (REG, GET_MODE (loc), REGNO (old));
5504
5505                   emit_insn_before (gen_move_insn (loc, reloadreg),
5506                                     first_output_reload_insn);
5507                   emit_insn_before (gen_move_insn (old, loc),
5508                                     first_output_reload_insn);
5509                 }
5510               else
5511 #endif
5512                 emit_insn_before (gen_move_insn (old, reloadreg),
5513                                   first_output_reload_insn);
5514             }
5515
5516 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5517           /* If final will look at death notes for this reg,
5518              put one on the last output-reload insn to use it.  Similarly
5519              for any secondary register.  */
5520           if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
5521             for (p = PREV_INSN (first_output_reload_insn);
5522                  p != prev_insn; p = PREV_INSN (p))
5523               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5524                   && reg_overlap_mentioned_for_reload_p (reloadreg,
5525                                                          PATTERN (p)))
5526                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5527                                          reloadreg, REG_NOTES (p));
5528
5529 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5530           if (! special
5531               && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
5532             for (p = PREV_INSN (first_output_reload_insn);
5533                  p != prev_insn; p = PREV_INSN (p))
5534               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5535                   && reg_overlap_mentioned_for_reload_p (second_reloadreg,
5536                                                          PATTERN (p)))
5537                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5538                                          second_reloadreg, REG_NOTES (p));
5539 #endif
5540 #endif
5541           /* Look at all insns we emitted, just to be safe.  */
5542           for (p = NEXT_INSN (prev_insn); p != first_output_reload_insn;
5543                p = NEXT_INSN (p))
5544             if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5545               {
5546                 /* If this output reload doesn't come from a spill reg,
5547                    clear any memory of reloaded copies of the pseudo reg.
5548                    If this output reload comes from a spill reg,
5549                    reg_has_output_reload will make this do nothing.  */
5550                 note_stores (PATTERN (p), forget_old_reloads_1);
5551
5552                 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p)))
5553                   store_insn = p;
5554               }
5555
5556           first_output_reload_insn = NEXT_INSN (prev_insn);
5557         }
5558
5559       if (reload_spill_index[j] >= 0)
5560         new_spill_reg_store[reload_spill_index[j]] = store_insn;
5561     }
5562
5563   /* Move death notes from INSN
5564      to output-operand-address and output reload insns.  */
5565 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5566   {
5567     rtx insn1;
5568     /* Loop over those insns, last ones first.  */
5569     for (insn1 = PREV_INSN (following_insn); insn1 != insn;
5570          insn1 = PREV_INSN (insn1))
5571       if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
5572         {
5573           rtx source = SET_SRC (PATTERN (insn1));
5574           rtx dest = SET_DEST (PATTERN (insn1));
5575
5576           /* The note we will examine next.  */
5577           rtx reg_notes = REG_NOTES (insn);
5578           /* The place that pointed to this note.  */
5579           rtx *prev_reg_note = &REG_NOTES (insn);
5580
5581           /* If the note is for something used in the source of this
5582              reload insn, or in the output address, move the note.  */
5583           while (reg_notes)
5584             {
5585               rtx next_reg_notes = XEXP (reg_notes, 1);
5586               if (REG_NOTE_KIND (reg_notes) == REG_DEAD
5587                   && GET_CODE (XEXP (reg_notes, 0)) == REG
5588                   && ((GET_CODE (dest) != REG
5589                        && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5590                                                               dest))
5591                       || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5592                                                              source)))
5593                 {
5594                   *prev_reg_note = next_reg_notes;
5595                   XEXP (reg_notes, 1) = REG_NOTES (insn1);
5596                   REG_NOTES (insn1) = reg_notes;
5597                 }
5598               else
5599                 prev_reg_note = &XEXP (reg_notes, 1);
5600
5601               reg_notes = next_reg_notes;
5602             }
5603         }
5604   }
5605 #endif
5606
5607   /* For all the spill regs newly reloaded in this instruction,
5608      record what they were reloaded from, so subsequent instructions
5609      can inherit the reloads.
5610
5611      Update spill_reg_store for the reloads of this insn.
5612      Copy the elements that were updated in the loop above.  */
5613
5614   for (j = 0; j < n_reloads; j++)
5615     {
5616       register int r = reload_order[j];
5617       register int i = reload_spill_index[r];
5618
5619       /* I is nonneg if this reload used one of the spill regs.
5620          If reload_reg_rtx[r] is 0, this is an optional reload
5621          that we opted to ignore.  */
5622
5623       if (i >= 0 && reload_reg_rtx[r] != 0)
5624         {
5625           /* First, clear out memory of what used to be in this spill reg.
5626              If consecutive registers are used, clear them all.  */
5627           int nr
5628             = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
5629           int k;
5630
5631           for (k = 0; k < nr; k++)
5632             {
5633               reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
5634               reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
5635             }
5636
5637           /* Maybe the spill reg contains a copy of reload_out.  */
5638           if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5639             {
5640               register int nregno = REGNO (reload_out[r]);
5641
5642               spill_reg_store[i] = new_spill_reg_store[i];
5643               reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5644
5645               for (k = 0; k < nr; k++)
5646                 {
5647                   reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5648                     = nregno;
5649                   reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
5650                 }
5651             }
5652
5653           /* Maybe the spill reg contains a copy of reload_in.  */
5654           else if (reload_out[r] == 0
5655                    && reload_in[r] != 0
5656                    && (GET_CODE (reload_in[r]) == REG
5657                        || GET_CODE (reload_in_reg[r]) == REG))
5658             {
5659               register int nregno;
5660               if (GET_CODE (reload_in[r]) == REG)
5661                 nregno = REGNO (reload_in[r]);
5662               else
5663                 nregno = REGNO (reload_in_reg[r]);
5664
5665               /* If there are two separate reloads (one in and one out)
5666                  for the same (hard or pseudo) reg,
5667                  leave reg_last_reload_reg set
5668                  based on the output reload.
5669                  Otherwise, set it from this input reload.  */
5670               if (!reg_has_output_reload[nregno]
5671                   /* But don't do so if another input reload
5672                      will clobber this one's value.  */
5673                   && reload_reg_reaches_end_p (spill_regs[i],
5674                                                reload_when_needed[r]))
5675                 {
5676                   reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5677
5678                   /* Unless we inherited this reload, show we haven't
5679                      recently done a store.  */
5680                   if (! reload_inherited[r])
5681                     spill_reg_store[i] = 0;
5682
5683                   for (k = 0; k < nr; k++)
5684                     {
5685                       reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5686                         = nregno;
5687                       reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
5688                         = insn;
5689                     }
5690                 }
5691             }
5692         }
5693
5694       /* The following if-statement was #if 0'd in 1.34 (or before...).
5695          It's reenabled in 1.35 because supposedly nothing else
5696          deals with this problem.  */
5697
5698       /* If a register gets output-reloaded from a non-spill register,
5699          that invalidates any previous reloaded copy of it.
5700          But forget_old_reloads_1 won't get to see it, because
5701          it thinks only about the original insn.  So invalidate it here.  */
5702       if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5703         {
5704           register int nregno = REGNO (reload_out[r]);
5705           reg_last_reload_reg[nregno] = 0;
5706         }
5707     }
5708 }
5709 \f
5710 /* Emit code before BEFORE_INSN to perform an input reload of IN to RELOADREG.
5711    Returns first insn emitted.  */
5712
5713 rtx
5714 gen_input_reload (reloadreg, in, before_insn)
5715      rtx reloadreg;
5716      rtx in;
5717      rtx before_insn;
5718 {
5719   register rtx prev_insn = PREV_INSN (before_insn);
5720
5721   /* How to do this reload can get quite tricky.  Normally, we are being
5722      asked to reload a simple operand, such as a MEM, a constant, or a pseudo
5723      register that didn't get a hard register.  In that case we can just
5724      call emit_move_insn.
5725
5726      We can also be asked to reload a PLUS that adds either two registers, or
5727      a register and a constant or MEM, or a MEM and a constant.  This can
5728      occur during frame pointer elimination and while reloading addresses.
5729      This case is handled by trying to emit a single insn
5730      to perform the add.  If it is not valid, we use a two insn sequence.
5731
5732      Finally, we could be called to handle an 'o' constraint by putting
5733      an address into a register.  In that case, we first try to do this
5734      with a named pattern of "reload_load_address".  If no such pattern
5735      exists, we just emit a SET insn and hope for the best (it will normally
5736      be valid on machines that use 'o').
5737
5738      This entire process is made complex because reload will never
5739      process the insns we generate here and so we must ensure that
5740      they will fit their constraints and also by the fact that parts of
5741      IN might be being reloaded separately and replaced with spill registers.
5742      Because of this, we are, in some sense, just guessing the right approach
5743      here.  The one listed above seems to work.
5744
5745      ??? At some point, this whole thing needs to be rethought.  */
5746
5747   if (GET_CODE (in) == PLUS
5748       && ((GET_CODE (XEXP (in, 0)) == REG
5749            && (GET_CODE (XEXP (in, 1)) == REG
5750                || CONSTANT_P (XEXP (in, 1))
5751                || GET_CODE (XEXP (in, 1)) == MEM))
5752           || (GET_CODE (XEXP (in, 0)) == MEM
5753               && CONSTANT_P (XEXP (in, 1)))))
5754     {
5755       /* We need to compute the sum of what is either a register and a
5756          constant, a register and memory, a hard register and a pseudo
5757          register, or memory and a constant and put it into the reload
5758          register.  The best possible way of doing this is if the machine
5759          has a three-operand ADD insn that accepts the required operands.
5760
5761          The simplest approach is to try to generate such an insn and see if it
5762          is recognized and matches its constraints.  If so, it can be used.
5763
5764          It might be better not to actually emit the insn unless it is valid,
5765          but we need to pass the insn as an operand to `recog' and
5766          `insn_extract' and it is simpler to emit and then delete the insn if
5767          not valid than to dummy things up.  */
5768
5769       rtx op0, op1, tem, insn;
5770       int code;
5771
5772       op0 = find_replacement (&XEXP (in, 0));
5773       op1 = find_replacement (&XEXP (in, 1));
5774
5775       /* Since constraint checking is strict, commutativity won't be
5776          checked, so we need to do that here to avoid spurious failure
5777          if the add instruction is two-address and the second operand
5778          of the add is the same as the reload reg, which is frequently
5779          the case.  If the insn would be A = B + A, rearrange it so
5780          it will be A = A + B as constrain_operands expects. */
5781
5782       if (GET_CODE (XEXP (in, 1)) == REG
5783           && REGNO (reloadreg) == REGNO (XEXP (in, 1)))
5784         tem = op0, op0 = op1, op1 = tem;
5785
5786       if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
5787         in = gen_rtx (PLUS, GET_MODE (in), op0, op1);
5788
5789       insn = emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in),
5790                                    before_insn);
5791       code = recog_memoized (insn);
5792
5793       if (code >= 0)
5794         {
5795           insn_extract (insn);
5796           /* We want constrain operands to treat this insn strictly in
5797              its validity determination, i.e., the way it would after reload
5798              has completed.  */
5799           if (constrain_operands (code, 1))
5800             return insn;
5801         }
5802
5803       if (PREV_INSN (insn))
5804         NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
5805       if (NEXT_INSN (insn))
5806         PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
5807
5808       /* If that failed, we must use a conservative two-insn sequence.
5809          use move to copy constant, MEM, or pseudo register to the reload
5810          register since "move" will be able to handle an arbitrary operand,
5811          unlike add which can't, in general.  Then add the registers.
5812
5813          If there is another way to do this for a specific machine, a
5814          DEFINE_PEEPHOLE should be specified that recognizes the sequence
5815          we emit below.  */
5816
5817       if (CONSTANT_P (op1) || GET_CODE (op1) == MEM
5818           || (GET_CODE (op1) == REG
5819               && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
5820         tem = op0, op0 = op1, op1 = tem;
5821
5822       emit_insn_before (gen_move_insn (reloadreg, op0), before_insn);
5823
5824       /* If OP0 and OP1 are the same, we can use RELOADREG for OP1.
5825          This fixes a problem on the 32K where the stack pointer cannot
5826          be used as an operand of an add insn.  */
5827
5828       if (rtx_equal_p (op0, op1))
5829         op1 = reloadreg;
5830
5831       emit_insn_before (gen_add2_insn (reloadreg, op1), before_insn);
5832     }
5833
5834 #ifdef SECONDARY_MEMORY_NEEDED
5835   /* If we need a memory location to do the move, do it that way.  */
5836   else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
5837            && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
5838                                        REGNO_REG_CLASS (REGNO (reloadreg)),
5839                                        GET_MODE (reloadreg)))
5840     {
5841       /* Get the memory to use and rewrite both registers to its mode.  */
5842       rtx loc = get_secondary_mem (in, GET_MODE (reloadreg));
5843
5844       if (GET_MODE (loc) != GET_MODE (reloadreg))
5845         reloadreg = gen_rtx (REG, GET_MODE (loc), REGNO (reloadreg));
5846
5847       if (GET_MODE (loc) != GET_MODE (in))
5848         in = gen_rtx (REG, GET_MODE (loc), REGNO (in));
5849
5850       emit_insn_before (gen_move_insn (loc, in), before_insn);
5851       emit_insn_before (gen_move_insn (reloadreg, loc), before_insn);
5852     }
5853 #endif
5854
5855   /* If IN is a simple operand, use gen_move_insn.  */
5856   else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5857     emit_insn_before (gen_move_insn (reloadreg, in), before_insn);
5858
5859 #ifdef HAVE_reload_load_address
5860   else if (HAVE_reload_load_address)
5861     emit_insn_before (gen_reload_load_address (reloadreg, in), before_insn);
5862 #endif
5863
5864   /* Otherwise, just write (set REGLOADREG IN) and hope for the best.  */
5865   else
5866     emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in), before_insn);
5867
5868   /* Return the first insn emitted.
5869      We can not just return PREV_INSN (before_insn), because there may have
5870      been multiple instructions emitted.  Also note that gen_move_insn may
5871      emit more than one insn itself, so we can not assume that there is one
5872      insn emitted per emit_insn_before call.  */
5873
5874   return NEXT_INSN (prev_insn);
5875 }
5876 \f
5877 /* Delete a previously made output-reload
5878    whose result we now believe is not needed.
5879    First we double-check.
5880
5881    INSN is the insn now being processed.
5882    OUTPUT_RELOAD_INSN is the insn of the output reload.
5883    J is the reload-number for this insn.  */
5884
5885 static void
5886 delete_output_reload (insn, j, output_reload_insn)
5887      rtx insn;
5888      int j;
5889      rtx output_reload_insn;
5890 {
5891   register rtx i1;
5892
5893   /* Get the raw pseudo-register referred to.  */
5894
5895   rtx reg = reload_in[j];
5896   while (GET_CODE (reg) == SUBREG)
5897     reg = SUBREG_REG (reg);
5898
5899   /* If the pseudo-reg we are reloading is no longer referenced
5900      anywhere between the store into it and here,
5901      and no jumps or labels intervene, then the value can get
5902      here through the reload reg alone.
5903      Otherwise, give up--return.  */
5904   for (i1 = NEXT_INSN (output_reload_insn);
5905        i1 != insn; i1 = NEXT_INSN (i1))
5906     {
5907       if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
5908         return;
5909       if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
5910           && reg_mentioned_p (reg, PATTERN (i1)))
5911         return;
5912     }
5913
5914   /* If this insn will store in the pseudo again,
5915      the previous store can be removed.  */
5916   if (reload_out[j] == reload_in[j])
5917     delete_insn (output_reload_insn);
5918
5919   /* See if the pseudo reg has been completely replaced
5920      with reload regs.  If so, delete the store insn
5921      and forget we had a stack slot for the pseudo.  */
5922   else if (reg_n_deaths[REGNO (reg)] == 1
5923            && reg_basic_block[REGNO (reg)] >= 0
5924            && find_regno_note (insn, REG_DEAD, REGNO (reg)))
5925     {
5926       rtx i2;
5927
5928       /* We know that it was used only between here
5929          and the beginning of the current basic block.
5930          (We also know that the last use before INSN was
5931          the output reload we are thinking of deleting, but never mind that.)
5932          Search that range; see if any ref remains.  */
5933       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5934         {
5935           rtx set = single_set (i2);
5936
5937           /* Uses which just store in the pseudo don't count,
5938              since if they are the only uses, they are dead.  */
5939           if (set != 0 && SET_DEST (set) == reg)
5940             continue;
5941           if (GET_CODE (i2) == CODE_LABEL
5942               || GET_CODE (i2) == JUMP_INSN)
5943             break;
5944           if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
5945               && reg_mentioned_p (reg, PATTERN (i2)))
5946             /* Some other ref remains;
5947                we can't do anything.  */
5948             return;
5949         }
5950
5951       /* Delete the now-dead stores into this pseudo.  */
5952       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5953         {
5954           rtx set = single_set (i2);
5955
5956           if (set != 0 && SET_DEST (set) == reg)
5957             delete_insn (i2);
5958           if (GET_CODE (i2) == CODE_LABEL
5959               || GET_CODE (i2) == JUMP_INSN)
5960             break;
5961         }
5962
5963       /* For the debugging info,
5964          say the pseudo lives in this reload reg.  */
5965       reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
5966       alter_reg (REGNO (reg), -1);
5967     }
5968 }
5969
5970 \f
5971 /* Output reload-insns to reload VALUE into RELOADREG.
5972    VALUE is an autoincrement or autodecrement RTX whose operand
5973    is a register or memory location;
5974    so reloading involves incrementing that location.
5975
5976    INC_AMOUNT is the number to increment or decrement by (always positive).
5977    This cannot be deduced from VALUE.
5978
5979    INSN is the insn before which the new insns should be emitted.
5980
5981    The return value is the first of the insns emitted.  */
5982
5983 static rtx
5984 inc_for_reload (reloadreg, value, inc_amount, insn)
5985      rtx reloadreg;
5986      rtx value;
5987      int inc_amount;
5988      rtx insn;
5989 {
5990   /* REG or MEM to be copied and incremented.  */
5991   rtx incloc = XEXP (value, 0);
5992   /* Nonzero if increment after copying.  */
5993   int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
5994   rtx prev = PREV_INSN (insn);
5995   rtx inc;
5996   rtx add_insn;
5997   int code;
5998
5999   /* No hard register is equivalent to this register after
6000      inc/dec operation.  If REG_LAST_RELOAD_REG were non-zero,
6001      we could inc/dec that register as well (maybe even using it for
6002      the source), but I'm not sure it's worth worrying about.  */
6003   if (GET_CODE (incloc) == REG)
6004     reg_last_reload_reg[REGNO (incloc)] = 0;
6005
6006   if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
6007     inc_amount = - inc_amount;
6008
6009   inc = GEN_INT (inc_amount);
6010
6011   /* If this is post-increment, first copy the location to the reload reg.  */
6012   if (post)
6013     emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6014
6015   /* See if we can directly increment INCLOC.  Use a method similar to that
6016      in gen_input_reload.  */
6017
6018   add_insn = emit_insn_before (gen_rtx (SET, VOIDmode, incloc,
6019                                         gen_rtx (PLUS, GET_MODE (incloc),
6020                                                  incloc, inc)), insn);
6021                                                           
6022   code = recog_memoized (add_insn);
6023   if (code >= 0)
6024     {
6025       insn_extract (add_insn);
6026       if (constrain_operands (code, 1))
6027         {
6028           /* If this is a pre-increment and we have incremented the value
6029              where it lives, copy the incremented value to RELOADREG to
6030              be used as an address.  */
6031
6032           if (! post)
6033             emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6034           return NEXT_INSN (prev);
6035         }
6036     }
6037
6038   if (PREV_INSN (add_insn))
6039     NEXT_INSN (PREV_INSN (add_insn)) = NEXT_INSN (add_insn);
6040   if (NEXT_INSN (add_insn))
6041     PREV_INSN (NEXT_INSN (add_insn)) = PREV_INSN (add_insn);
6042
6043   /* If couldn't do the increment directly, must increment in RELOADREG.
6044      The way we do this depends on whether this is pre- or post-increment.
6045      For pre-increment, copy INCLOC to the reload register, increment it
6046      there, then save back.  */
6047
6048   if (! post)
6049     {
6050       emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
6051       emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
6052       emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
6053     }
6054   else
6055     {
6056       /* Postincrement.
6057          Because this might be a jump insn or a compare, and because RELOADREG
6058          may not be available after the insn in an input reload, we must do
6059          the incrementation before the insn being reloaded for.
6060
6061          We have already copied INCLOC to RELOADREG.  Increment the copy in
6062          RELOADREG, save that back, then decrement RELOADREG so it has
6063          the original value.  */
6064
6065       emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
6066       emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
6067       emit_insn_before (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)),
6068                         insn);
6069     }
6070
6071   return NEXT_INSN (prev);
6072 }
6073 \f
6074 /* Return 1 if we are certain that the constraint-string STRING allows
6075    the hard register REG.  Return 0 if we can't be sure of this.  */
6076
6077 static int
6078 constraint_accepts_reg_p (string, reg)
6079      char *string;
6080      rtx reg;
6081 {
6082   int value = 0;
6083   int regno = true_regnum (reg);
6084   int c;
6085
6086   /* Initialize for first alternative.  */
6087   value = 0;
6088   /* Check that each alternative contains `g' or `r'.  */
6089   while (1)
6090     switch (c = *string++)
6091       {
6092       case 0:
6093         /* If an alternative lacks `g' or `r', we lose.  */
6094         return value;
6095       case ',':
6096         /* If an alternative lacks `g' or `r', we lose.  */
6097         if (value == 0)
6098           return 0;
6099         /* Initialize for next alternative.  */
6100         value = 0;
6101         break;
6102       case 'g':
6103       case 'r':
6104         /* Any general reg wins for this alternative.  */
6105         if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
6106           value = 1;
6107         break;
6108       default:
6109         /* Any reg in specified class wins for this alternative.  */
6110         {
6111           enum reg_class class = REG_CLASS_FROM_LETTER (c);
6112
6113           if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
6114             value = 1;
6115         }
6116       }
6117 }
6118 \f
6119 /* Return the number of places FIND appears within X, but don't count
6120    an occurrence if some SET_DEST is FIND.  */
6121
6122 static int
6123 count_occurrences (x, find)
6124      register rtx x, find;
6125 {
6126   register int i, j;
6127   register enum rtx_code code;
6128   register char *format_ptr;
6129   int count;
6130
6131   if (x == find)
6132     return 1;
6133   if (x == 0)
6134     return 0;
6135
6136   code = GET_CODE (x);
6137
6138   switch (code)
6139     {
6140     case REG:
6141     case QUEUED:
6142     case CONST_INT:
6143     case CONST_DOUBLE:
6144     case SYMBOL_REF:
6145     case CODE_LABEL:
6146     case PC:
6147     case CC0:
6148       return 0;
6149
6150     case SET:
6151       if (SET_DEST (x) == find)
6152         return count_occurrences (SET_SRC (x), find);
6153       break;
6154     }
6155
6156   format_ptr = GET_RTX_FORMAT (code);
6157   count = 0;
6158
6159   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6160     {
6161       switch (*format_ptr++)
6162         {
6163         case 'e':
6164           count += count_occurrences (XEXP (x, i), find);
6165           break;
6166
6167         case 'E':
6168           if (XVEC (x, i) != NULL)
6169             {
6170               for (j = 0; j < XVECLEN (x, i); j++)
6171                 count += count_occurrences (XVECEXP (x, i, j), find);
6172             }
6173           break;
6174         }
6175     }
6176   return count;
6177 }