OSDN Git Service

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