OSDN Git Service

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