OSDN Git Service

Merge from gcc-2.8
[pf3gnuchains/gcc-fork.git] / gcc / reload1.c
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2    Copyright (C) 1987, 88, 89, 92-6, 1997 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include <stdio.h>
24 #include "rtl.h"
25 #include "obstack.h"
26 #include "insn-config.h"
27 #include "insn-flags.h"
28 #include "insn-codes.h"
29 #include "flags.h"
30 #include "expr.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "reload.h"
34 #include "recog.h"
35 #include "basic-block.h"
36 #include "output.h"
37 #include "real.h"
38
39 /* This file contains the reload pass of the compiler, which is
40    run after register allocation has been done.  It checks that
41    each insn is valid (operands required to be in registers really
42    are in registers of the proper class) and fixes up invalid ones
43    by copying values temporarily into registers for the insns
44    that need them.
45
46    The results of register allocation are described by the vector
47    reg_renumber; the insns still contain pseudo regs, but reg_renumber
48    can be used to find which hard reg, if any, a pseudo reg is in.
49
50    The technique we always use is to free up a few hard regs that are
51    called ``reload regs'', and for each place where a pseudo reg
52    must be in a hard reg, copy it temporarily into one of the reload regs.
53
54    All the pseudos that were formerly allocated to the hard regs that
55    are now in use as reload regs must be ``spilled''.  This means
56    that they go to other hard regs, or to stack slots if no other
57    available hard regs can be found.  Spilling can invalidate more
58    insns, requiring additional need for reloads, so we must keep checking
59    until the process stabilizes.
60
61    For machines with different classes of registers, we must keep track
62    of the register class needed for each reload, and make sure that
63    we allocate enough reload registers of each class.
64
65    The file reload.c contains the code that checks one insn for
66    validity and reports the reloads that it needs.  This file
67    is in charge of scanning the entire rtl code, accumulating the
68    reload needs, spilling, assigning reload registers to use for
69    fixing up each insn, and generating the new insns to copy values
70    into the reload registers.  */
71
72
73 #ifndef REGISTER_MOVE_COST
74 #define REGISTER_MOVE_COST(x, y) 2
75 #endif
76
77 #ifndef MEMORY_MOVE_COST
78 #define MEMORY_MOVE_COST(x) 4
79 #endif
80 \f
81 /* During reload_as_needed, element N contains a REG rtx for the hard reg
82    into which reg N has been reloaded (perhaps for a previous insn).  */
83 static rtx *reg_last_reload_reg;
84
85 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
86    for an output reload that stores into reg N.  */
87 static char *reg_has_output_reload;
88
89 /* Indicates which hard regs are reload-registers for an output reload
90    in the current insn.  */
91 static HARD_REG_SET reg_is_output_reload;
92
93 /* Element N is the constant value to which pseudo reg N is equivalent,
94    or zero if pseudo reg N is not equivalent to a constant.
95    find_reloads looks at this in order to replace pseudo reg N
96    with the constant it stands for.  */
97 rtx *reg_equiv_constant;
98
99 /* Element N is a memory location to which pseudo reg N is equivalent,
100    prior to any register elimination (such as frame pointer to stack
101    pointer).  Depending on whether or not it is a valid address, this value
102    is transferred to either reg_equiv_address or reg_equiv_mem.  */
103 rtx *reg_equiv_memory_loc;
104
105 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
106    This is used when the address is not valid as a memory address
107    (because its displacement is too big for the machine.)  */
108 rtx *reg_equiv_address;
109
110 /* Element N is the memory slot to which pseudo reg N is equivalent,
111    or zero if pseudo reg N is not equivalent to a memory slot.  */
112 rtx *reg_equiv_mem;
113
114 /* Widest width in which each pseudo reg is referred to (via subreg).  */
115 static int *reg_max_ref_width;
116
117 /* Element N is the insn that initialized reg N from its equivalent
118    constant or memory slot.  */
119 static rtx *reg_equiv_init;
120
121 /* During reload_as_needed, element N contains the last pseudo regno
122    reloaded into the Nth reload register.  This vector is in parallel
123    with spill_regs.  If that pseudo reg occupied more than one register,
124    reg_reloaded_contents points to that pseudo for each spill register in
125    use; all of these must remain set for an inheritance to occur.  */
126 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
127
128 /* During reload_as_needed, element N contains the insn for which
129    the Nth reload register was last used.  This vector is in parallel
130    with spill_regs, and its contents are significant only when
131    reg_reloaded_contents is significant.  */
132 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
133
134 /* Number of spill-regs so far; number of valid elements of spill_regs.  */
135 static int n_spills;
136
137 /* In parallel with spill_regs, contains REG rtx's for those regs.
138    Holds the last rtx used for any given reg, or 0 if it has never
139    been used for spilling yet.  This rtx is reused, provided it has
140    the proper mode.  */
141 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
142
143 /* In parallel with spill_regs, contains nonzero for a spill reg
144    that was stored after the last time it was used.
145    The precise value is the insn generated to do the store.  */
146 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
147
148 /* This table is the inverse mapping of spill_regs:
149    indexed by hard reg number,
150    it contains the position of that reg in spill_regs,
151    or -1 for something that is not in spill_regs.  */
152 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
153
154 /* This reg set indicates registers that may not be used for retrying global
155    allocation.  The registers that may not be used include all spill registers
156    and the frame pointer (if we are using one).  */
157 HARD_REG_SET forbidden_regs;
158
159 /* This reg set indicates registers that are not good for spill registers.
160    They will not be used to complete groups of spill registers.  This includes
161    all fixed registers, registers that may be eliminated, and, if
162    SMALL_REGISTER_CLASSES is zero, registers explicitly used in the rtl.
163
164    (spill_reg_order prevents these registers from being used to start a
165    group.)  */
166 static HARD_REG_SET bad_spill_regs;
167
168 /* Describes order of use of registers for reloading
169    of spilled pseudo-registers.  `spills' is the number of
170    elements that are actually valid; new ones are added at the end.  */
171 static short spill_regs[FIRST_PSEUDO_REGISTER];
172
173 /* This reg set indicates those registers that have been used a spill
174    registers.  This information is used in reorg.c, to help figure out
175    what registers are live at any point.  It is assumed that all spill_regs
176    are dead at every CODE_LABEL.  */
177
178 HARD_REG_SET used_spill_regs;
179
180 /* Index of last register assigned as a spill register.  We allocate in
181    a round-robin fashion.  */
182
183 static int last_spill_reg;
184
185 /* Describes order of preference for putting regs into spill_regs.
186    Contains the numbers of all the hard regs, in order most preferred first.
187    This order is different for each function.
188    It is set up by order_regs_for_reload.
189    Empty elements at the end contain -1.  */
190 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
191
192 /* 1 for a hard register that appears explicitly in the rtl
193    (for example, function value registers, special registers
194    used by insns, structure value pointer registers).  */
195 static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
196
197 /* Indicates if a register was counted against the need for
198    groups.  0 means it can count against max_nongroup instead.  */
199 static HARD_REG_SET counted_for_groups;
200
201 /* Indicates if a register was counted against the need for
202    non-groups.  0 means it can become part of a new group.
203    During choose_reload_regs, 1 here means don't use this reg
204    as part of a group, even if it seems to be otherwise ok.  */
205 static HARD_REG_SET counted_for_nongroups;
206
207 /* Indexed by pseudo reg number N,
208    says may not delete stores into the real (memory) home of pseudo N.
209    This is set if we already substituted a memory equivalent in some uses,
210    which happens when we have to eliminate the fp from it.  */
211 static char *cannot_omit_stores;
212
213 /* Nonzero if indirect addressing is supported on the machine; this means
214    that spilling (REG n) does not require reloading it into a register in
215    order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))).  The
216    value indicates the level of indirect addressing supported, e.g., two
217    means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
218    a hard register.  */
219
220 static char spill_indirect_levels;
221
222 /* Nonzero if indirect addressing is supported when the innermost MEM is
223    of the form (MEM (SYMBOL_REF sym)).  It is assumed that the level to
224    which these are valid is the same as spill_indirect_levels, above.   */
225
226 char indirect_symref_ok;
227
228 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid.  */
229
230 char double_reg_address_ok;
231
232 /* Record the stack slot for each spilled hard register.  */
233
234 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
235
236 /* Width allocated so far for that stack slot.  */
237
238 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
239
240 /* Indexed by register class and basic block number, nonzero if there is
241    any need for a spill register of that class in that basic block.
242    The pointer is 0 if we did stupid allocation and don't know
243    the structure of basic blocks.  */
244
245 char *basic_block_needs[N_REG_CLASSES];
246
247 /* First uid used by insns created by reload in this function.
248    Used in find_equiv_reg.  */
249 int reload_first_uid;
250
251 /* Flag set by local-alloc or global-alloc if anything is live in
252    a call-clobbered reg across calls.  */
253
254 int caller_save_needed;
255
256 /* The register class to use for a base register when reloading an
257    address.  This is normally BASE_REG_CLASS, but it may be different
258    when using SMALL_REGISTER_CLASSES and passing parameters in
259    registers.  */
260 enum reg_class reload_address_base_reg_class;
261
262 /* The register class to use for an index register when reloading an
263    address.  This is normally INDEX_REG_CLASS, but it may be different
264    when using SMALL_REGISTER_CLASSES and passing parameters in
265    registers.  */
266 enum reg_class reload_address_index_reg_class;
267
268 /* Set to 1 while reload_as_needed is operating.
269    Required by some machines to handle any generated moves differently.  */
270
271 int reload_in_progress = 0;
272
273 /* These arrays record the insn_code of insns that may be needed to
274    perform input and output reloads of special objects.  They provide a
275    place to pass a scratch register.  */
276
277 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
278 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
279
280 /* This obstack is used for allocation of rtl during register elimination.
281    The allocated storage can be freed once find_reloads has processed the
282    insn.  */
283
284 struct obstack reload_obstack;
285 char *reload_firstobj;
286
287 #define obstack_chunk_alloc xmalloc
288 #define obstack_chunk_free free
289
290 /* List of labels that must never be deleted.  */
291 extern rtx forced_labels;
292
293 /* Allocation number table from global register allocation.  */
294 extern int *reg_allocno;
295 \f
296 /* This structure is used to record information about register eliminations.
297    Each array entry describes one possible way of eliminating a register
298    in favor of another.   If there is more than one way of eliminating a
299    particular register, the most preferred should be specified first.  */
300
301 static struct elim_table
302 {
303   int from;                     /* Register number to be eliminated.  */
304   int to;                       /* Register number used as replacement.  */
305   int initial_offset;           /* Initial difference between values.  */
306   int can_eliminate;            /* Non-zero if this elimination can be done.  */
307   int can_eliminate_previous;   /* Value of CAN_ELIMINATE in previous scan over
308                                    insns made by reload.  */
309   int offset;                   /* Current offset between the two regs.  */
310   int max_offset;               /* Maximum offset between the two regs.  */
311   int previous_offset;          /* Offset at end of previous insn.  */
312   int ref_outside_mem;          /* "to" has been referenced outside a MEM.  */
313   rtx from_rtx;                 /* REG rtx for the register to be eliminated.
314                                    We cannot simply compare the number since
315                                    we might then spuriously replace a hard
316                                    register corresponding to a pseudo
317                                    assigned to the reg to be eliminated.  */
318   rtx to_rtx;                   /* REG rtx for the replacement.  */
319 } reg_eliminate[] =
320
321 /* If a set of eliminable registers was specified, define the table from it.
322    Otherwise, default to the normal case of the frame pointer being
323    replaced by the stack pointer.  */
324
325 #ifdef ELIMINABLE_REGS
326   ELIMINABLE_REGS;
327 #else
328   {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
329 #endif
330
331 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
332
333 /* Record the number of pending eliminations that have an offset not equal
334    to their initial offset.  If non-zero, we use a new copy of each
335    replacement result in any insns encountered.  */
336 static int num_not_at_initial_offset;
337
338 /* Count the number of registers that we may be able to eliminate.  */
339 static int num_eliminable;
340
341 /* For each label, we record the offset of each elimination.  If we reach
342    a label by more than one path and an offset differs, we cannot do the
343    elimination.  This information is indexed by the number of the label.
344    The first table is an array of flags that records whether we have yet
345    encountered a label and the second table is an array of arrays, one
346    entry in the latter array for each elimination.  */
347
348 static char *offsets_known_at;
349 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
350
351 /* Number of labels in the current function.  */
352
353 static int num_labels;
354
355 struct hard_reg_n_uses { int regno; int uses; };
356 \f
357 static int possible_group_p             PROTO((int, int *));
358 static void count_possible_groups       PROTO((int *, enum machine_mode *,
359                                                int *, int));
360 static int modes_equiv_for_class_p      PROTO((enum machine_mode,
361                                                enum machine_mode,
362                                                enum reg_class));
363 static void spill_failure               PROTO((rtx));
364 static int new_spill_reg                PROTO((int, int, int *, int *, int,
365                                                FILE *));
366 static void delete_dead_insn            PROTO((rtx));
367 static void alter_reg                   PROTO((int, int));
368 static void mark_scratch_live           PROTO((rtx));
369 static void set_label_offsets           PROTO((rtx, rtx, int));
370 static int eliminate_regs_in_insn       PROTO((rtx, int));
371 static void mark_not_eliminable         PROTO((rtx, rtx));
372 static int spill_hard_reg               PROTO((int, int, FILE *, int));
373 static void scan_paradoxical_subregs    PROTO((rtx));
374 static int hard_reg_use_compare         PROTO((const GENERIC_PTR, const GENERIC_PTR));
375 static void order_regs_for_reload       PROTO((int));
376 static int compare_spill_regs           PROTO((const GENERIC_PTR, const GENERIC_PTR));
377 static void reload_as_needed            PROTO((rtx, int));
378 static void forget_old_reloads_1        PROTO((rtx, rtx));
379 static int reload_reg_class_lower       PROTO((const GENERIC_PTR, const GENERIC_PTR));
380 static void mark_reload_reg_in_use      PROTO((int, int, enum reload_type,
381                                                enum machine_mode));
382 static void clear_reload_reg_in_use     PROTO((int, int, enum reload_type,
383                                                enum machine_mode));
384 static int reload_reg_free_p            PROTO((int, int, enum reload_type));
385 static int reload_reg_free_before_p     PROTO((int, int, enum reload_type));
386 static int reload_reg_reaches_end_p     PROTO((int, int, enum reload_type));
387 static int reloads_conflict             PROTO((int, int));
388 static int allocate_reload_reg          PROTO((int, rtx, int, int));
389 static void choose_reload_regs          PROTO((rtx, rtx));
390 static void merge_assigned_reloads      PROTO((rtx));
391 static void emit_reload_insns           PROTO((rtx));
392 static void delete_output_reload        PROTO((rtx, int, rtx));
393 static void inc_for_reload              PROTO((rtx, rtx, int));
394 static int constraint_accepts_reg_p     PROTO((char *, rtx));
395 static int count_occurrences            PROTO((rtx, rtx));
396 static void reload_cse_invalidate_regno PROTO((int, enum machine_mode, int));
397 static int reload_cse_mem_conflict_p    PROTO((rtx, rtx));
398 static void reload_cse_invalidate_mem   PROTO((rtx));
399 static void reload_cse_invalidate_rtx   PROTO((rtx, rtx));
400 static int reload_cse_regno_equal_p     PROTO((int, rtx, enum machine_mode));
401 static int reload_cse_noop_set_p        PROTO((rtx, rtx));
402 static int reload_cse_simplify_set      PROTO((rtx, rtx));
403 static int reload_cse_simplify_operands PROTO((rtx));
404 static void reload_cse_check_clobber    PROTO((rtx, rtx));
405 static void reload_cse_record_set       PROTO((rtx, rtx));
406 static void reload_cse_delete_death_notes       PROTO((rtx));
407 static void reload_cse_no_longer_dead   PROTO((int, enum machine_mode));
408 \f
409 /* Initialize the reload pass once per compilation.  */
410
411 void
412 init_reload ()
413 {
414   register int i;
415
416   /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
417      Set spill_indirect_levels to the number of levels such addressing is
418      permitted, zero if it is not permitted at all.  */
419
420   register rtx tem
421     = gen_rtx (MEM, Pmode,
422                gen_rtx (PLUS, Pmode,
423                         gen_rtx (REG, Pmode, LAST_VIRTUAL_REGISTER + 1),
424                         GEN_INT (4)));
425   spill_indirect_levels = 0;
426
427   while (memory_address_p (QImode, tem))
428     {
429       spill_indirect_levels++;
430       tem = gen_rtx (MEM, Pmode, tem);
431     }
432
433   /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)).  */
434
435   tem = gen_rtx (MEM, Pmode, gen_rtx (SYMBOL_REF, Pmode, "foo"));
436   indirect_symref_ok = memory_address_p (QImode, tem);
437
438   /* See if reg+reg is a valid (and offsettable) address.  */
439
440   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
441     {
442       tem = gen_rtx (PLUS, Pmode,
443                      gen_rtx (REG, Pmode, HARD_FRAME_POINTER_REGNUM),
444                      gen_rtx (REG, Pmode, i));
445       /* This way, we make sure that reg+reg is an offsettable address.  */
446       tem = plus_constant (tem, 4);
447
448       if (memory_address_p (QImode, tem))
449         {
450           double_reg_address_ok = 1;
451           break;
452         }
453     }
454
455   /* Initialize obstack for our rtl allocation.  */
456   gcc_obstack_init (&reload_obstack);
457   reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
458
459   /* Decide which register class should be used when reloading
460      addresses.  If we are using SMALL_REGISTER_CLASSES, and any
461      parameters are passed in registers, then we do not want to use
462      those registers when reloading an address.  Otherwise, if a
463      function argument needs a reload, we may wind up clobbering
464      another argument to the function which was already computed.  If
465      we find a subset class which simply avoids those registers, we
466      use it instead.  ??? It would be better to only use the
467      restricted class when we actually are loading function arguments,
468      but that is hard to determine.  */
469   reload_address_base_reg_class = BASE_REG_CLASS;
470   reload_address_index_reg_class = INDEX_REG_CLASS;
471   if (SMALL_REGISTER_CLASSES)
472     {
473       int regno;
474       HARD_REG_SET base, index;
475       enum reg_class *p;
476
477       COPY_HARD_REG_SET (base, reg_class_contents[BASE_REG_CLASS]);
478       COPY_HARD_REG_SET (index, reg_class_contents[INDEX_REG_CLASS]);
479       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
480         {
481           if (FUNCTION_ARG_REGNO_P (regno))
482             {
483               CLEAR_HARD_REG_BIT (base, regno);
484               CLEAR_HARD_REG_BIT (index, regno);
485             }
486         }
487       
488       GO_IF_HARD_REG_EQUAL (base, reg_class_contents[BASE_REG_CLASS],
489                             baseok);
490       for (p = reg_class_subclasses[BASE_REG_CLASS];
491            *p != LIM_REG_CLASSES;
492            p++)
493         {
494           GO_IF_HARD_REG_EQUAL (base, reg_class_contents[*p], usebase);
495           continue;
496         usebase:
497           reload_address_base_reg_class = *p;
498           break;
499         }
500     baseok:;
501
502       GO_IF_HARD_REG_EQUAL (index, reg_class_contents[INDEX_REG_CLASS],
503                             indexok);
504       for (p = reg_class_subclasses[INDEX_REG_CLASS];
505            *p != LIM_REG_CLASSES;
506            p++)
507         {
508           GO_IF_HARD_REG_EQUAL (index, reg_class_contents[*p], useindex);
509           continue;
510         useindex:
511           reload_address_index_reg_class = *p;
512           break;
513         }
514     indexok:;
515     }
516 }
517
518 /* Main entry point for the reload pass.
519
520    FIRST is the first insn of the function being compiled.
521
522    GLOBAL nonzero means we were called from global_alloc
523    and should attempt to reallocate any pseudoregs that we
524    displace from hard regs we will use for reloads.
525    If GLOBAL is zero, we do not have enough information to do that,
526    so any pseudo reg that is spilled must go to the stack.
527
528    DUMPFILE is the global-reg debugging dump file stream, or 0.
529    If it is nonzero, messages are written to it to describe
530    which registers are seized as reload regs, which pseudo regs
531    are spilled from them, and where the pseudo regs are reallocated to.
532
533    Return value is nonzero if reload failed
534    and we must not do any more for this function.  */
535
536 int
537 reload (first, global, dumpfile)
538      rtx first;
539      int global;
540      FILE *dumpfile;
541 {
542   register int class;
543   register int i, j, k;
544   register rtx insn;
545   register struct elim_table *ep;
546
547   /* The two pointers used to track the true location of the memory used
548      for label offsets.  */
549   char *real_known_ptr = NULL_PTR;
550   int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
551
552   int something_changed;
553   int something_needs_reloads;
554   int something_needs_elimination;
555   int new_basic_block_needs;
556   enum reg_class caller_save_spill_class = NO_REGS;
557   int caller_save_group_size = 1;
558
559   /* Nonzero means we couldn't get enough spill regs.  */
560   int failure = 0;
561
562   /* The basic block number currently being processed for INSN.  */
563   int this_block;
564
565   /* Make sure even insns with volatile mem refs are recognizable.  */
566   init_recog ();
567
568   /* Enable find_equiv_reg to distinguish insns made by reload.  */
569   reload_first_uid = get_max_uid ();
570
571   for (i = 0; i < N_REG_CLASSES; i++)
572     basic_block_needs[i] = 0;
573
574 #ifdef SECONDARY_MEMORY_NEEDED
575   /* Initialize the secondary memory table.  */
576   clear_secondary_mem ();
577 #endif
578
579   /* Remember which hard regs appear explicitly
580      before we merge into `regs_ever_live' the ones in which
581      pseudo regs have been allocated.  */
582   bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
583
584   /* We don't have a stack slot for any spill reg yet.  */
585   bzero ((char *) spill_stack_slot, sizeof spill_stack_slot);
586   bzero ((char *) spill_stack_slot_width, sizeof spill_stack_slot_width);
587
588   /* Initialize the save area information for caller-save, in case some
589      are needed.  */
590   init_save_areas ();
591
592   /* Compute which hard registers are now in use
593      as homes for pseudo registers.
594      This is done here rather than (eg) in global_alloc
595      because this point is reached even if not optimizing.  */
596   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
597     mark_home_live (i);
598
599   /* A function that receives a nonlocal goto must save all call-saved
600      registers.  */
601   if (current_function_has_nonlocal_label)
602     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
603       {
604         if (! call_used_regs[i] && ! fixed_regs[i])
605           regs_ever_live[i] = 1;
606       }
607
608   for (i = 0; i < scratch_list_length; i++)
609     if (scratch_list[i])
610       mark_scratch_live (scratch_list[i]);
611
612   /* Make sure that the last insn in the chain
613      is not something that needs reloading.  */
614   emit_note (NULL_PTR, NOTE_INSN_DELETED);
615
616   /* Find all the pseudo registers that didn't get hard regs
617      but do have known equivalent constants or memory slots.
618      These include parameters (known equivalent to parameter slots)
619      and cse'd or loop-moved constant memory addresses.
620
621      Record constant equivalents in reg_equiv_constant
622      so they will be substituted by find_reloads.
623      Record memory equivalents in reg_mem_equiv so they can
624      be substituted eventually by altering the REG-rtx's.  */
625
626   reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
627   bzero ((char *) reg_equiv_constant, max_regno * sizeof (rtx));
628   reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
629   bzero ((char *) reg_equiv_memory_loc, max_regno * sizeof (rtx));
630   reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
631   bzero ((char *) reg_equiv_mem, max_regno * sizeof (rtx));
632   reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
633   bzero ((char *) reg_equiv_init, max_regno * sizeof (rtx));
634   reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
635   bzero ((char *) reg_equiv_address, max_regno * sizeof (rtx));
636   reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
637   bzero ((char *) reg_max_ref_width, max_regno * sizeof (int));
638   cannot_omit_stores = (char *) alloca (max_regno);
639   bzero (cannot_omit_stores, max_regno);
640
641   if (SMALL_REGISTER_CLASSES)
642     CLEAR_HARD_REG_SET (forbidden_regs);
643
644   /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
645      Also find all paradoxical subregs and find largest such for each pseudo.
646      On machines with small register classes, record hard registers that
647      are used for user variables.  These can never be used for spills. 
648      Also look for a "constant" NOTE_INSN_SETJMP.  This means that all
649      caller-saved registers must be marked live.  */
650
651   for (insn = first; insn; insn = NEXT_INSN (insn))
652     {
653       rtx set = single_set (insn);
654
655       if (GET_CODE (insn) == NOTE && CONST_CALL_P (insn)
656           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
657         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
658           if (! call_used_regs[i])
659             regs_ever_live[i] = 1;
660
661       if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
662         {
663           rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
664           if (note
665 #ifdef LEGITIMATE_PIC_OPERAND_P
666               && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
667                   || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
668 #endif
669               )
670             {
671               rtx x = XEXP (note, 0);
672               i = REGNO (SET_DEST (set));
673               if (i > LAST_VIRTUAL_REGISTER)
674                 {
675                   if (GET_CODE (x) == MEM)
676                     {
677                       /* If the operand is a PLUS, the MEM may be shared,
678                          so make sure we have an unshared copy here.  */
679                       if (GET_CODE (XEXP (x, 0)) == PLUS)
680                         x = copy_rtx (x);
681
682                       reg_equiv_memory_loc[i] = x;
683                     }
684                   else if (CONSTANT_P (x))
685                     {
686                       if (LEGITIMATE_CONSTANT_P (x))
687                         reg_equiv_constant[i] = x;
688                       else
689                         reg_equiv_memory_loc[i]
690                           = force_const_mem (GET_MODE (SET_DEST (set)), x);
691                     }
692                   else
693                     continue;
694
695                   /* If this register is being made equivalent to a MEM
696                      and the MEM is not SET_SRC, the equivalencing insn
697                      is one with the MEM as a SET_DEST and it occurs later.
698                      So don't mark this insn now.  */
699                   if (GET_CODE (x) != MEM
700                       || rtx_equal_p (SET_SRC (set), x))
701                     reg_equiv_init[i] = insn;
702                 }
703             }
704         }
705
706       /* If this insn is setting a MEM from a register equivalent to it,
707          this is the equivalencing insn.  */
708       else if (set && GET_CODE (SET_DEST (set)) == MEM
709                && GET_CODE (SET_SRC (set)) == REG
710                && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
711                && rtx_equal_p (SET_DEST (set),
712                                reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
713         reg_equiv_init[REGNO (SET_SRC (set))] = insn;
714
715       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
716         scan_paradoxical_subregs (PATTERN (insn));
717     }
718
719   /* Does this function require a frame pointer?  */
720
721   frame_pointer_needed = (! flag_omit_frame_pointer
722 #ifdef EXIT_IGNORE_STACK
723                           /* ?? If EXIT_IGNORE_STACK is set, we will not save
724                              and restore sp for alloca.  So we can't eliminate
725                              the frame pointer in that case.  At some point,
726                              we should improve this by emitting the
727                              sp-adjusting insns for this case.  */
728                           || (current_function_calls_alloca
729                               && EXIT_IGNORE_STACK)
730 #endif
731                           || FRAME_POINTER_REQUIRED);
732
733   num_eliminable = 0;
734
735   /* Initialize the table of registers to eliminate.  The way we do this
736      depends on how the eliminable registers were defined.  */
737 #ifdef ELIMINABLE_REGS
738   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
739     {
740       ep->can_eliminate = ep->can_eliminate_previous
741         = (CAN_ELIMINATE (ep->from, ep->to)
742            && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
743     }
744 #else
745   reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
746     = ! frame_pointer_needed;
747 #endif
748
749   /* Count the number of eliminable registers and build the FROM and TO
750      REG rtx's.  Note that code in gen_rtx will cause, e.g.,
751      gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
752      We depend on this.  */
753   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
754     {
755       num_eliminable += ep->can_eliminate;
756       ep->from_rtx = gen_rtx (REG, Pmode, ep->from);
757       ep->to_rtx = gen_rtx (REG, Pmode, ep->to);
758     }
759
760   num_labels = max_label_num () - get_first_label_num ();
761
762   /* Allocate the tables used to store offset information at labels.  */
763   /* We used to use alloca here, but the size of what it would try to
764      allocate would occasionally cause it to exceed the stack limit and
765      cause a core dump.  */
766   real_known_ptr = xmalloc (num_labels);
767   real_at_ptr
768     = (int (*)[NUM_ELIMINABLE_REGS])
769     xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
770
771   offsets_known_at = real_known_ptr - get_first_label_num ();
772   offsets_at
773     = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
774
775   /* Alter each pseudo-reg rtx to contain its hard reg number.
776      Assign stack slots to the pseudos that lack hard regs or equivalents.
777      Do not touch virtual registers.  */
778
779   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
780     alter_reg (i, -1);
781
782   /* If we have some registers we think can be eliminated, scan all insns to
783      see if there is an insn that sets one of these registers to something
784      other than itself plus a constant.  If so, the register cannot be
785      eliminated.  Doing this scan here eliminates an extra pass through the
786      main reload loop in the most common case where register elimination
787      cannot be done.  */
788   for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
789     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
790         || GET_CODE (insn) == CALL_INSN)
791       note_stores (PATTERN (insn), mark_not_eliminable);
792
793 #ifndef REGISTER_CONSTRAINTS
794   /* If all the pseudo regs have hard regs,
795      except for those that are never referenced,
796      we know that no reloads are needed.  */
797   /* But that is not true if there are register constraints, since
798      in that case some pseudos might be in the wrong kind of hard reg.  */
799
800   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
801     if (reg_renumber[i] == -1 && REG_N_REFS (i) != 0)
802       break;
803
804   if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
805     {
806       free (real_known_ptr);
807       free (real_at_ptr);
808       return;
809     }
810 #endif
811
812   /* Compute the order of preference for hard registers to spill.
813      Store them by decreasing preference in potential_reload_regs.  */
814
815   order_regs_for_reload (global);
816
817   /* So far, no hard regs have been spilled.  */
818   n_spills = 0;
819   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
820     spill_reg_order[i] = -1;
821
822   /* Initialize to -1, which means take the first spill register.  */
823   last_spill_reg = -1;
824
825   /* On most machines, we can't use any register explicitly used in the
826      rtl as a spill register.  But on some, we have to.  Those will have
827      taken care to keep the life of hard regs as short as possible.  */
828
829   if (! SMALL_REGISTER_CLASSES)
830     COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
831
832   /* Spill any hard regs that we know we can't eliminate.  */
833   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
834     if (! ep->can_eliminate)
835       spill_hard_reg (ep->from, global, dumpfile, 1);
836
837 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
838   if (frame_pointer_needed)
839     spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
840 #endif
841
842   if (global)
843     for (i = 0; i < N_REG_CLASSES; i++)
844       {
845         basic_block_needs[i] = (char *) alloca (n_basic_blocks);
846         bzero (basic_block_needs[i], n_basic_blocks);
847       }
848
849   /* From now on, we need to emit any moves without making new pseudos.  */
850   reload_in_progress = 1;
851
852   /* This loop scans the entire function each go-round
853      and repeats until one repetition spills no additional hard regs.  */
854
855   /* This flag is set when a pseudo reg is spilled,
856      to require another pass.  Note that getting an additional reload
857      reg does not necessarily imply any pseudo reg was spilled;
858      sometimes we find a reload reg that no pseudo reg was allocated in.  */
859   something_changed = 1;
860   /* This flag is set if there are any insns that require reloading.  */
861   something_needs_reloads = 0;
862   /* This flag is set if there are any insns that require register
863      eliminations.  */
864   something_needs_elimination = 0;
865   while (something_changed)
866     {
867       rtx after_call = 0;
868
869       /* For each class, number of reload regs needed in that class.
870          This is the maximum over all insns of the needs in that class
871          of the individual insn.  */
872       int max_needs[N_REG_CLASSES];
873       /* For each class, size of group of consecutive regs
874          that is needed for the reloads of this class.  */
875       int group_size[N_REG_CLASSES];
876       /* For each class, max number of consecutive groups needed.
877          (Each group contains group_size[CLASS] consecutive registers.)  */
878       int max_groups[N_REG_CLASSES];
879       /* For each class, max number needed of regs that don't belong
880          to any of the groups.  */
881       int max_nongroups[N_REG_CLASSES];
882       /* For each class, the machine mode which requires consecutive
883          groups of regs of that class.
884          If two different modes ever require groups of one class,
885          they must be the same size and equally restrictive for that class,
886          otherwise we can't handle the complexity.  */
887       enum machine_mode group_mode[N_REG_CLASSES];
888       /* Record the insn where each maximum need is first found.  */
889       rtx max_needs_insn[N_REG_CLASSES];
890       rtx max_groups_insn[N_REG_CLASSES];
891       rtx max_nongroups_insn[N_REG_CLASSES];
892       rtx x;
893       HOST_WIDE_INT starting_frame_size;
894       int previous_frame_pointer_needed = frame_pointer_needed;
895       static char *reg_class_names[] = REG_CLASS_NAMES;
896
897       something_changed = 0;
898       bzero ((char *) max_needs, sizeof max_needs);
899       bzero ((char *) max_groups, sizeof max_groups);
900       bzero ((char *) max_nongroups, sizeof max_nongroups);
901       bzero ((char *) max_needs_insn, sizeof max_needs_insn);
902       bzero ((char *) max_groups_insn, sizeof max_groups_insn);
903       bzero ((char *) max_nongroups_insn, sizeof max_nongroups_insn);
904       bzero ((char *) group_size, sizeof group_size);
905       for (i = 0; i < N_REG_CLASSES; i++)
906         group_mode[i] = VOIDmode;
907
908       /* Keep track of which basic blocks are needing the reloads.  */
909       this_block = 0;
910
911       /* Remember whether any element of basic_block_needs
912          changes from 0 to 1 in this pass.  */
913       new_basic_block_needs = 0;
914
915       /* Round size of stack frame to BIGGEST_ALIGNMENT.  This must be done
916          here because the stack size may be a part of the offset computation
917          for register elimination, and there might have been new stack slots
918          created in the last iteration of this loop.   */
919       assign_stack_local (BLKmode, 0, 0);
920
921       starting_frame_size = get_frame_size ();
922
923       /* Reset all offsets on eliminable registers to their initial values.  */
924 #ifdef ELIMINABLE_REGS
925       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
926         {
927           INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
928           ep->previous_offset = ep->offset
929             = ep->max_offset = ep->initial_offset;
930         }
931 #else
932 #ifdef INITIAL_FRAME_POINTER_OFFSET
933       INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
934 #else
935       if (!FRAME_POINTER_REQUIRED)
936         abort ();
937       reg_eliminate[0].initial_offset = 0;
938 #endif
939       reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
940         = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
941 #endif
942
943       num_not_at_initial_offset = 0;
944
945       bzero ((char *) &offsets_known_at[get_first_label_num ()], num_labels);
946
947       /* Set a known offset for each forced label to be at the initial offset
948          of each elimination.  We do this because we assume that all
949          computed jumps occur from a location where each elimination is
950          at its initial offset.  */
951
952       for (x = forced_labels; x; x = XEXP (x, 1))
953         if (XEXP (x, 0))
954           set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
955
956       /* For each pseudo register that has an equivalent location defined,
957          try to eliminate any eliminable registers (such as the frame pointer)
958          assuming initial offsets for the replacement register, which
959          is the normal case.
960
961          If the resulting location is directly addressable, substitute
962          the MEM we just got directly for the old REG.
963
964          If it is not addressable but is a constant or the sum of a hard reg
965          and constant, it is probably not addressable because the constant is
966          out of range, in that case record the address; we will generate
967          hairy code to compute the address in a register each time it is
968          needed.  Similarly if it is a hard register, but one that is not
969          valid as an address register.
970
971          If the location is not addressable, but does not have one of the
972          above forms, assign a stack slot.  We have to do this to avoid the
973          potential of producing lots of reloads if, e.g., a location involves
974          a pseudo that didn't get a hard register and has an equivalent memory
975          location that also involves a pseudo that didn't get a hard register.
976
977          Perhaps at some point we will improve reload_when_needed handling
978          so this problem goes away.  But that's very hairy.  */
979
980       for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
981         if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
982           {
983             rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX, 0);
984
985             if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
986                                          XEXP (x, 0)))
987               reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
988             else if (CONSTANT_P (XEXP (x, 0))
989                      || (GET_CODE (XEXP (x, 0)) == REG
990                          && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
991                      || (GET_CODE (XEXP (x, 0)) == PLUS
992                          && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
993                          && (REGNO (XEXP (XEXP (x, 0), 0))
994                              < FIRST_PSEUDO_REGISTER)
995                          && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
996               reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
997             else
998               {
999                 /* Make a new stack slot.  Then indicate that something
1000                    changed so we go back and recompute offsets for
1001                    eliminable registers because the allocation of memory
1002                    below might change some offset.  reg_equiv_{mem,address}
1003                    will be set up for this pseudo on the next pass around
1004                    the loop.  */
1005                 reg_equiv_memory_loc[i] = 0;
1006                 reg_equiv_init[i] = 0;
1007                 alter_reg (i, -1);
1008                 something_changed = 1;
1009               }
1010           }
1011
1012       /* If we allocated another pseudo to the stack, redo elimination
1013          bookkeeping.  */
1014       if (something_changed)
1015         continue;
1016
1017       /* If caller-saves needs a group, initialize the group to include
1018          the size and mode required for caller-saves.  */
1019
1020       if (caller_save_group_size > 1)
1021         {
1022           group_mode[(int) caller_save_spill_class] = Pmode;
1023           group_size[(int) caller_save_spill_class] = caller_save_group_size;
1024         }
1025
1026       /* Compute the most additional registers needed by any instruction.
1027          Collect information separately for each class of regs.  */
1028
1029       for (insn = first; insn; insn = NEXT_INSN (insn))
1030         {
1031           if (global && this_block + 1 < n_basic_blocks
1032               && insn == basic_block_head[this_block+1])
1033             ++this_block;
1034
1035           /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
1036              might include REG_LABEL), we need to see what effects this
1037              has on the known offsets at labels.  */
1038
1039           if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1040               || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1041                   && REG_NOTES (insn) != 0))
1042             set_label_offsets (insn, insn, 0);
1043
1044           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1045             {
1046               /* Nonzero means don't use a reload reg that overlaps
1047                  the place where a function value can be returned.  */
1048               rtx avoid_return_reg = 0;
1049
1050               rtx old_body = PATTERN (insn);
1051               int old_code = INSN_CODE (insn);
1052               rtx old_notes = REG_NOTES (insn);
1053               int did_elimination = 0;
1054
1055               /* To compute the number of reload registers of each class 
1056                  needed for an insn, we must simulate what choose_reload_regs
1057                  can do.  We do this by splitting an insn into an "input" and
1058                  an "output" part.  RELOAD_OTHER reloads are used in both. 
1059                  The input part uses those reloads, RELOAD_FOR_INPUT reloads,
1060                  which must be live over the entire input section of reloads,
1061                  and the maximum of all the RELOAD_FOR_INPUT_ADDRESS and
1062                  RELOAD_FOR_OPERAND_ADDRESS reloads, which conflict with the
1063                  inputs.
1064
1065                  The registers needed for output are RELOAD_OTHER and
1066                  RELOAD_FOR_OUTPUT, which are live for the entire output
1067                  portion, and the maximum of all the RELOAD_FOR_OUTPUT_ADDRESS
1068                  reloads for each operand.
1069
1070                  The total number of registers needed is the maximum of the
1071                  inputs and outputs.  */
1072
1073               struct needs
1074                 {
1075                   /* [0] is normal, [1] is nongroup.  */
1076                   int regs[2][N_REG_CLASSES];
1077                   int groups[N_REG_CLASSES];
1078                 };
1079
1080               /* Each `struct needs' corresponds to one RELOAD_... type.  */
1081               struct {
1082                 struct needs other;
1083                 struct needs input;
1084                 struct needs output;
1085                 struct needs insn;
1086                 struct needs other_addr;
1087                 struct needs op_addr;
1088                 struct needs op_addr_reload;
1089                 struct needs in_addr[MAX_RECOG_OPERANDS];
1090                 struct needs in_addr_addr[MAX_RECOG_OPERANDS];
1091                 struct needs out_addr[MAX_RECOG_OPERANDS];
1092                 struct needs out_addr_addr[MAX_RECOG_OPERANDS];
1093               } insn_needs;
1094
1095               /* If needed, eliminate any eliminable registers.  */
1096               if (num_eliminable)
1097                 did_elimination = eliminate_regs_in_insn (insn, 0);
1098
1099               /* Set avoid_return_reg if this is an insn
1100                  that might use the value of a function call.  */
1101               if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
1102                 {
1103                   if (GET_CODE (PATTERN (insn)) == SET)
1104                     after_call = SET_DEST (PATTERN (insn));
1105                   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1106                            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1107                     after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1108                   else
1109                     after_call = 0;
1110                 }
1111               else if (SMALL_REGISTER_CLASSES && after_call != 0
1112                        && !(GET_CODE (PATTERN (insn)) == SET
1113                             && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
1114                 {
1115                   if (reg_referenced_p (after_call, PATTERN (insn)))
1116                     avoid_return_reg = after_call;
1117                   after_call = 0;
1118                 }
1119
1120               /* Analyze the instruction.  */
1121               find_reloads (insn, 0, spill_indirect_levels, global,
1122                             spill_reg_order);
1123
1124               /* Remember for later shortcuts which insns had any reloads or
1125                  register eliminations.
1126
1127                  One might think that it would be worthwhile to mark insns
1128                  that need register replacements but not reloads, but this is
1129                  not safe because find_reloads may do some manipulation of
1130                  the insn (such as swapping commutative operands), which would
1131                  be lost when we restore the old pattern after register
1132                  replacement.  So the actions of find_reloads must be redone in
1133                  subsequent passes or in reload_as_needed.
1134
1135                  However, it is safe to mark insns that need reloads
1136                  but not register replacement.  */
1137
1138               PUT_MODE (insn, (did_elimination ? QImode
1139                                : n_reloads ? HImode
1140                                : GET_MODE (insn) == DImode ? DImode
1141                                : VOIDmode));
1142
1143               /* Discard any register replacements done.  */
1144               if (did_elimination)
1145                 {
1146                   obstack_free (&reload_obstack, reload_firstobj);
1147                   PATTERN (insn) = old_body;
1148                   INSN_CODE (insn) = old_code;
1149                   REG_NOTES (insn) = old_notes;
1150                   something_needs_elimination = 1;
1151                 }
1152
1153               /* If this insn has no reloads, we need not do anything except
1154                  in the case of a CALL_INSN when we have caller-saves and
1155                  caller-save needs reloads.  */
1156
1157               if (n_reloads == 0
1158                   && ! (GET_CODE (insn) == CALL_INSN
1159                         && caller_save_spill_class != NO_REGS))
1160                 continue;
1161
1162               something_needs_reloads = 1;
1163               bzero ((char *) &insn_needs, sizeof insn_needs);
1164
1165               /* Count each reload once in every class
1166                  containing the reload's own class.  */
1167
1168               for (i = 0; i < n_reloads; i++)
1169                 {
1170                   register enum reg_class *p;
1171                   enum reg_class class = reload_reg_class[i];
1172                   int size;
1173                   enum machine_mode mode;
1174                   int nongroup_need;
1175                   struct needs *this_needs;
1176
1177                   /* Don't count the dummy reloads, for which one of the
1178                      regs mentioned in the insn can be used for reloading.
1179                      Don't count optional reloads.
1180                      Don't count reloads that got combined with others.  */
1181                   if (reload_reg_rtx[i] != 0
1182                       || reload_optional[i] != 0
1183                       || (reload_out[i] == 0 && reload_in[i] == 0
1184                           && ! reload_secondary_p[i]))
1185                     continue;
1186
1187                   /* Show that a reload register of this class is needed
1188                      in this basic block.  We do not use insn_needs and
1189                      insn_groups because they are overly conservative for
1190                      this purpose.  */
1191                   if (global && ! basic_block_needs[(int) class][this_block])
1192                     {
1193                       basic_block_needs[(int) class][this_block] = 1;
1194                       new_basic_block_needs = 1;
1195                     }
1196
1197                   mode = reload_inmode[i];
1198                   if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1199                     mode = reload_outmode[i];
1200                   size = CLASS_MAX_NREGS (class, mode);
1201
1202                   /* If this class doesn't want a group, determine if we have
1203                      a nongroup need or a regular need.  We have a nongroup
1204                      need if this reload conflicts with a group reload whose
1205                      class intersects with this reload's class.  */
1206
1207                   nongroup_need = 0;
1208                   if (size == 1)
1209                     for (j = 0; j < n_reloads; j++)
1210                       if ((CLASS_MAX_NREGS (reload_reg_class[j],
1211                                             (GET_MODE_SIZE (reload_outmode[j])
1212                                              > GET_MODE_SIZE (reload_inmode[j]))
1213                                             ? reload_outmode[j]
1214                                             : reload_inmode[j])
1215                            > 1)
1216                           && (!reload_optional[j])
1217                           && (reload_in[j] != 0 || reload_out[j] != 0
1218                               || reload_secondary_p[j])
1219                           && reloads_conflict (i, j)
1220                           && reg_classes_intersect_p (class,
1221                                                       reload_reg_class[j]))
1222                         {
1223                           nongroup_need = 1;
1224                           break;
1225                         }
1226
1227                   /* Decide which time-of-use to count this reload for.  */
1228                   switch (reload_when_needed[i])
1229                     {
1230                     case RELOAD_OTHER:
1231                       this_needs = &insn_needs.other;
1232                       break;
1233                     case RELOAD_FOR_INPUT:
1234                       this_needs = &insn_needs.input;
1235                       break;
1236                     case RELOAD_FOR_OUTPUT:
1237                       this_needs = &insn_needs.output;
1238                       break;
1239                     case RELOAD_FOR_INSN:
1240                       this_needs = &insn_needs.insn;
1241                       break;
1242                     case RELOAD_FOR_OTHER_ADDRESS:
1243                       this_needs = &insn_needs.other_addr;
1244                       break;
1245                     case RELOAD_FOR_INPUT_ADDRESS:
1246                       this_needs = &insn_needs.in_addr[reload_opnum[i]];
1247                       break;
1248                     case RELOAD_FOR_INPADDR_ADDRESS:
1249                       this_needs = &insn_needs.in_addr_addr[reload_opnum[i]];
1250                       break;
1251                     case RELOAD_FOR_OUTPUT_ADDRESS:
1252                       this_needs = &insn_needs.out_addr[reload_opnum[i]];
1253                       break;
1254                     case RELOAD_FOR_OUTADDR_ADDRESS:
1255                       this_needs = &insn_needs.out_addr_addr[reload_opnum[i]];
1256                       break;
1257                     case RELOAD_FOR_OPERAND_ADDRESS:
1258                       this_needs = &insn_needs.op_addr;
1259                       break;
1260                     case RELOAD_FOR_OPADDR_ADDR:
1261                       this_needs = &insn_needs.op_addr_reload;
1262                       break;
1263                     }
1264
1265                   if (size > 1)
1266                     {
1267                       enum machine_mode other_mode, allocate_mode;
1268
1269                       /* Count number of groups needed separately from
1270                          number of individual regs needed.  */
1271                       this_needs->groups[(int) class]++;
1272                       p = reg_class_superclasses[(int) class];
1273                       while (*p != LIM_REG_CLASSES)
1274                         this_needs->groups[(int) *p++]++;
1275
1276                       /* Record size and mode of a group of this class.  */
1277                       /* If more than one size group is needed,
1278                          make all groups the largest needed size.  */
1279                       if (group_size[(int) class] < size)
1280                         {
1281                           other_mode = group_mode[(int) class];
1282                           allocate_mode = mode;
1283
1284                           group_size[(int) class] = size;
1285                           group_mode[(int) class] = mode;
1286                         }
1287                       else
1288                         {
1289                           other_mode = mode;
1290                           allocate_mode = group_mode[(int) class];
1291                         }
1292
1293                       /* Crash if two dissimilar machine modes both need
1294                          groups of consecutive regs of the same class.  */
1295
1296                       if (other_mode != VOIDmode && other_mode != allocate_mode
1297                           && ! modes_equiv_for_class_p (allocate_mode,
1298                                                         other_mode, class))
1299                         fatal_insn ("Two dissimilar machine modes both need groups of consecutive regs of the same class",
1300                                     insn);
1301                     }
1302                   else if (size == 1)
1303                     {
1304                       this_needs->regs[nongroup_need][(int) class] += 1;
1305                       p = reg_class_superclasses[(int) class];
1306                       while (*p != LIM_REG_CLASSES)
1307                         this_needs->regs[nongroup_need][(int) *p++] += 1;
1308                     }
1309                   else
1310                     abort ();
1311                 }
1312
1313               /* All reloads have been counted for this insn;
1314                  now merge the various times of use.
1315                  This sets insn_needs, etc., to the maximum total number
1316                  of registers needed at any point in this insn.  */
1317
1318               for (i = 0; i < N_REG_CLASSES; i++)
1319                 {
1320                   int in_max, out_max;
1321
1322                   /* Compute normal and nongroup needs.  */
1323                   for (j = 0; j <= 1; j++)
1324                     {
1325                       for (in_max = 0, out_max = 0, k = 0;
1326                            k < reload_n_operands; k++)
1327                         {
1328                           in_max
1329                             = MAX (in_max,
1330                                    (insn_needs.in_addr[k].regs[j][i]
1331                                     + insn_needs.in_addr_addr[k].regs[j][i]));
1332                           out_max
1333                             = MAX (out_max, insn_needs.out_addr[k].regs[j][i]);
1334                           out_max
1335                             = MAX (out_max,
1336                                    insn_needs.out_addr_addr[k].regs[j][i]);
1337                         }
1338
1339                       /* RELOAD_FOR_INSN reloads conflict with inputs, outputs,
1340                          and operand addresses but not things used to reload
1341                          them.  Similarly, RELOAD_FOR_OPERAND_ADDRESS reloads
1342                          don't conflict with things needed to reload inputs or
1343                          outputs.  */
1344
1345                       in_max = MAX (MAX (insn_needs.op_addr.regs[j][i],
1346                                          insn_needs.op_addr_reload.regs[j][i]),
1347                                     in_max);
1348
1349                       out_max = MAX (out_max, insn_needs.insn.regs[j][i]);
1350
1351                       insn_needs.input.regs[j][i]
1352                         = MAX (insn_needs.input.regs[j][i]
1353                                + insn_needs.op_addr.regs[j][i]
1354                                + insn_needs.insn.regs[j][i],
1355                                in_max + insn_needs.input.regs[j][i]);
1356
1357                       insn_needs.output.regs[j][i] += out_max;
1358                       insn_needs.other.regs[j][i]
1359                         += MAX (MAX (insn_needs.input.regs[j][i],
1360                                      insn_needs.output.regs[j][i]),
1361                                 insn_needs.other_addr.regs[j][i]);
1362
1363                     }
1364
1365                   /* Now compute group needs.  */
1366                   for (in_max = 0, out_max = 0, j = 0;
1367                        j < reload_n_operands; j++)
1368                     {
1369                       in_max = MAX (in_max, insn_needs.in_addr[j].groups[i]);
1370                       in_max = MAX (in_max,
1371                                      insn_needs.in_addr_addr[j].groups[i]);
1372                       out_max
1373                         = MAX (out_max, insn_needs.out_addr[j].groups[i]);
1374                       out_max
1375                         = MAX (out_max, insn_needs.out_addr_addr[j].groups[i]);
1376                     }
1377
1378                   in_max = MAX (MAX (insn_needs.op_addr.groups[i],
1379                                      insn_needs.op_addr_reload.groups[i]),
1380                                 in_max);
1381                   out_max = MAX (out_max, insn_needs.insn.groups[i]);
1382
1383                   insn_needs.input.groups[i]
1384                     = MAX (insn_needs.input.groups[i]
1385                            + insn_needs.op_addr.groups[i]
1386                            + insn_needs.insn.groups[i],
1387                            in_max + insn_needs.input.groups[i]);
1388
1389                   insn_needs.output.groups[i] += out_max;
1390                   insn_needs.other.groups[i]
1391                     += MAX (MAX (insn_needs.input.groups[i],
1392                                  insn_needs.output.groups[i]),
1393                             insn_needs.other_addr.groups[i]);
1394                 }
1395
1396               /* If this is a CALL_INSN and caller-saves will need
1397                  a spill register, act as if the spill register is
1398                  needed for this insn.   However, the spill register
1399                  can be used by any reload of this insn, so we only
1400                  need do something if no need for that class has
1401                  been recorded.
1402
1403                  The assumption that every CALL_INSN will trigger a
1404                  caller-save is highly conservative, however, the number
1405                  of cases where caller-saves will need a spill register but
1406                  a block containing a CALL_INSN won't need a spill register
1407                  of that class should be quite rare.
1408
1409                  If a group is needed, the size and mode of the group will
1410                  have been set up at the beginning of this loop.  */
1411
1412               if (GET_CODE (insn) == CALL_INSN
1413                   && caller_save_spill_class != NO_REGS)
1414                 {
1415                   /* See if this register would conflict with any reload
1416                      that needs a group.  */
1417                   int nongroup_need = 0;
1418                   int *caller_save_needs;
1419
1420                   for (j = 0; j < n_reloads; j++)
1421                     if ((CLASS_MAX_NREGS (reload_reg_class[j],
1422                                           (GET_MODE_SIZE (reload_outmode[j])
1423                                            > GET_MODE_SIZE (reload_inmode[j]))
1424                                           ? reload_outmode[j]
1425                                           : reload_inmode[j])
1426                          > 1)
1427                         && reg_classes_intersect_p (caller_save_spill_class,
1428                                                     reload_reg_class[j]))
1429                       {
1430                         nongroup_need = 1;
1431                         break;
1432                       }
1433
1434                   caller_save_needs 
1435                     = (caller_save_group_size > 1
1436                        ? insn_needs.other.groups
1437                        : insn_needs.other.regs[nongroup_need]); 
1438
1439                   if (caller_save_needs[(int) caller_save_spill_class] == 0)
1440                     {
1441                       register enum reg_class *p
1442                         = reg_class_superclasses[(int) caller_save_spill_class];
1443
1444                       caller_save_needs[(int) caller_save_spill_class]++;
1445
1446                       while (*p != LIM_REG_CLASSES)
1447                         caller_save_needs[(int) *p++] += 1;
1448                     }
1449
1450                   /* Show that this basic block will need a register of
1451                    this class.  */
1452
1453                   if (global
1454                       && ! (basic_block_needs[(int) caller_save_spill_class]
1455                             [this_block]))
1456                     {
1457                       basic_block_needs[(int) caller_save_spill_class]
1458                         [this_block] = 1;
1459                       new_basic_block_needs = 1;
1460                     }
1461                 }
1462
1463               /* If this insn stores the value of a function call,
1464                  and that value is in a register that has been spilled,
1465                  and if the insn needs a reload in a class
1466                  that might use that register as the reload register,
1467                  then add add an extra need in that class.
1468                  This makes sure we have a register available that does
1469                  not overlap the return value.  */
1470
1471               if (SMALL_REGISTER_CLASSES && avoid_return_reg)
1472                 {
1473                   int regno = REGNO (avoid_return_reg);
1474                   int nregs
1475                     = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1476                   int r;
1477                   int basic_needs[N_REG_CLASSES], basic_groups[N_REG_CLASSES];
1478
1479                   /* First compute the "basic needs", which counts a
1480                      need only in the smallest class in which it
1481                      is required.  */
1482
1483                   bcopy ((char *) insn_needs.other.regs[0],
1484                          (char *) basic_needs, sizeof basic_needs);
1485                   bcopy ((char *) insn_needs.other.groups,
1486                          (char *) basic_groups, sizeof basic_groups);
1487
1488                   for (i = 0; i < N_REG_CLASSES; i++)
1489                     {
1490                       enum reg_class *p;
1491
1492                       if (basic_needs[i] >= 0)
1493                         for (p = reg_class_superclasses[i];
1494                              *p != LIM_REG_CLASSES; p++)
1495                           basic_needs[(int) *p] -= basic_needs[i];
1496
1497                       if (basic_groups[i] >= 0)
1498                         for (p = reg_class_superclasses[i];
1499                              *p != LIM_REG_CLASSES; p++)
1500                           basic_groups[(int) *p] -= basic_groups[i];
1501                     }
1502
1503                   /* Now count extra regs if there might be a conflict with
1504                      the return value register.  */
1505
1506                   for (r = regno; r < regno + nregs; r++)
1507                     if (spill_reg_order[r] >= 0)
1508                       for (i = 0; i < N_REG_CLASSES; i++)
1509                         if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1510                           {
1511                             if (basic_needs[i] > 0)
1512                               {
1513                                 enum reg_class *p;
1514
1515                                 insn_needs.other.regs[0][i]++;
1516                                 p = reg_class_superclasses[i];
1517                                 while (*p != LIM_REG_CLASSES)
1518                                   insn_needs.other.regs[0][(int) *p++]++;
1519                               }
1520                             if (basic_groups[i] > 0)
1521                               {
1522                                 enum reg_class *p;
1523
1524                                 insn_needs.other.groups[i]++;
1525                                 p = reg_class_superclasses[i];
1526                                 while (*p != LIM_REG_CLASSES)
1527                                   insn_needs.other.groups[(int) *p++]++;
1528                               }
1529                           }
1530                 }
1531
1532               /* For each class, collect maximum need of any insn.  */
1533
1534               for (i = 0; i < N_REG_CLASSES; i++)
1535                 {
1536                   if (max_needs[i] < insn_needs.other.regs[0][i])
1537                     {
1538                       max_needs[i] = insn_needs.other.regs[0][i];
1539                       max_needs_insn[i] = insn;
1540                     }
1541                   if (max_groups[i] < insn_needs.other.groups[i])
1542                     {
1543                       max_groups[i] = insn_needs.other.groups[i];
1544                       max_groups_insn[i] = insn;
1545                     }
1546                   if (max_nongroups[i] < insn_needs.other.regs[1][i])
1547                     {
1548                       max_nongroups[i] = insn_needs.other.regs[1][i];
1549                       max_nongroups_insn[i] = insn;
1550                     }
1551                 }
1552             }
1553           /* Note that there is a continue statement above.  */
1554         }
1555
1556       /* If we allocated any new memory locations, make another pass
1557          since it might have changed elimination offsets.  */
1558       if (starting_frame_size != get_frame_size ())
1559         something_changed = 1;
1560
1561       if (dumpfile)
1562         for (i = 0; i < N_REG_CLASSES; i++)
1563           {
1564             if (max_needs[i] > 0)
1565               fprintf (dumpfile,
1566                          ";; Need %d reg%s of class %s (for insn %d).\n",
1567                        max_needs[i], max_needs[i] == 1 ? "" : "s",
1568                        reg_class_names[i], INSN_UID (max_needs_insn[i]));
1569             if (max_nongroups[i] > 0)
1570               fprintf (dumpfile,
1571                        ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1572                        max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1573                        reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1574             if (max_groups[i] > 0)
1575               fprintf (dumpfile,
1576                        ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1577                        max_groups[i], max_groups[i] == 1 ? "" : "s",
1578                        mode_name[(int) group_mode[i]],
1579                        reg_class_names[i], INSN_UID (max_groups_insn[i]));
1580           }
1581                          
1582       /* If we have caller-saves, set up the save areas and see if caller-save
1583          will need a spill register.  */
1584
1585       if (caller_save_needed)
1586         {
1587           /* Set the offsets for setup_save_areas.  */
1588           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1589                ep++)
1590             ep->previous_offset = ep->max_offset;
1591
1592           if ( ! setup_save_areas (&something_changed)
1593               && caller_save_spill_class  == NO_REGS)
1594             {
1595               /* The class we will need depends on whether the machine
1596                  supports the sum of two registers for an address; see
1597               find_address_reloads for details.  */
1598
1599               caller_save_spill_class
1600                 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1601               caller_save_group_size
1602                 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1603               something_changed = 1;
1604             }
1605         }
1606
1607       /* See if anything that happened changes which eliminations are valid.
1608          For example, on the Sparc, whether or not the frame pointer can
1609          be eliminated can depend on what registers have been used.  We need
1610          not check some conditions again (such as flag_omit_frame_pointer)
1611          since they can't have changed.  */
1612
1613       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1614         if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
1615 #ifdef ELIMINABLE_REGS
1616             || ! CAN_ELIMINATE (ep->from, ep->to)
1617 #endif
1618             )
1619           ep->can_eliminate = 0;
1620
1621       /* Look for the case where we have discovered that we can't replace
1622          register A with register B and that means that we will now be
1623          trying to replace register A with register C.  This means we can
1624          no longer replace register C with register B and we need to disable
1625          such an elimination, if it exists.  This occurs often with A == ap,
1626          B == sp, and C == fp.  */
1627
1628       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1629         {
1630           struct elim_table *op;
1631           register int new_to = -1;
1632
1633           if (! ep->can_eliminate && ep->can_eliminate_previous)
1634             {
1635               /* Find the current elimination for ep->from, if there is a
1636                  new one.  */
1637               for (op = reg_eliminate;
1638                    op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1639                 if (op->from == ep->from && op->can_eliminate)
1640                   {
1641                     new_to = op->to;
1642                     break;
1643                   }
1644
1645               /* See if there is an elimination of NEW_TO -> EP->TO.  If so,
1646                  disable it.  */
1647               for (op = reg_eliminate;
1648                    op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1649                 if (op->from == new_to && op->to == ep->to)
1650                   op->can_eliminate = 0;
1651             }
1652         }
1653
1654       /* See if any registers that we thought we could eliminate the previous
1655          time are no longer eliminable.  If so, something has changed and we
1656          must spill the register.  Also, recompute the number of eliminable
1657          registers and see if the frame pointer is needed; it is if there is
1658          no elimination of the frame pointer that we can perform.  */
1659
1660       frame_pointer_needed = 1;
1661       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1662         {
1663           if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
1664               && ep->to != HARD_FRAME_POINTER_REGNUM)
1665             frame_pointer_needed = 0;
1666
1667           if (! ep->can_eliminate && ep->can_eliminate_previous)
1668             {
1669               ep->can_eliminate_previous = 0;
1670               spill_hard_reg (ep->from, global, dumpfile, 1);
1671               something_changed = 1;
1672               num_eliminable--;
1673             }
1674         }
1675
1676 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1677       /* If we didn't need a frame pointer last time, but we do now, spill
1678          the hard frame pointer.  */
1679       if (frame_pointer_needed && ! previous_frame_pointer_needed)
1680         {
1681           spill_hard_reg (HARD_FRAME_POINTER_REGNUM, global, dumpfile, 1);
1682           something_changed = 1;
1683         }
1684 #endif
1685
1686       /* If all needs are met, we win.  */
1687
1688       for (i = 0; i < N_REG_CLASSES; i++)
1689         if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1690           break;
1691       if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1692         break;
1693
1694       /* Not all needs are met; must spill some hard regs.  */
1695
1696       /* Put all registers spilled so far back in potential_reload_regs, but
1697          put them at the front, since we've already spilled most of the
1698          pseudos in them (we might have left some pseudos unspilled if they
1699          were in a block that didn't need any spill registers of a conflicting
1700          class.  We used to try to mark off the need for those registers,
1701          but doing so properly is very complex and reallocating them is the
1702          simpler approach.  First, "pack" potential_reload_regs by pushing 
1703          any nonnegative entries towards the end.  That will leave room 
1704          for the registers we already spilled.
1705
1706          Also, undo the marking of the spill registers from the last time
1707          around in FORBIDDEN_REGS since we will be probably be allocating
1708          them again below.
1709
1710          ??? It is theoretically possible that we might end up not using one
1711          of our previously-spilled registers in this allocation, even though
1712          they are at the head of the list.  It's not clear what to do about
1713          this, but it was no better before, when we marked off the needs met
1714          by the previously-spilled registers.  With the current code, globals
1715          can be allocated into these registers, but locals cannot.  */
1716
1717       if (n_spills)
1718         {
1719           for (i = j = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
1720             if (potential_reload_regs[i] != -1)
1721               potential_reload_regs[j--] = potential_reload_regs[i];
1722
1723           for (i = 0; i < n_spills; i++)
1724             {
1725               potential_reload_regs[i] = spill_regs[i];
1726               spill_reg_order[spill_regs[i]] = -1;
1727               CLEAR_HARD_REG_BIT (forbidden_regs, spill_regs[i]);
1728             }
1729
1730           n_spills = 0;
1731         }
1732
1733       /* Now find more reload regs to satisfy the remaining need
1734          Do it by ascending class number, since otherwise a reg
1735          might be spilled for a big class and might fail to count
1736          for a smaller class even though it belongs to that class.
1737
1738          Count spilled regs in `spills', and add entries to
1739          `spill_regs' and `spill_reg_order'.
1740
1741          ??? Note there is a problem here.
1742          When there is a need for a group in a high-numbered class,
1743          and also need for non-group regs that come from a lower class,
1744          the non-group regs are chosen first.  If there aren't many regs,
1745          they might leave no room for a group.
1746
1747          This was happening on the 386.  To fix it, we added the code
1748          that calls possible_group_p, so that the lower class won't
1749          break up the last possible group.
1750
1751          Really fixing the problem would require changes above
1752          in counting the regs already spilled, and in choose_reload_regs.
1753          It might be hard to avoid introducing bugs there.  */
1754
1755       CLEAR_HARD_REG_SET (counted_for_groups);
1756       CLEAR_HARD_REG_SET (counted_for_nongroups);
1757
1758       for (class = 0; class < N_REG_CLASSES; class++)
1759         {
1760           /* First get the groups of registers.
1761              If we got single registers first, we might fragment
1762              possible groups.  */
1763           while (max_groups[class] > 0)
1764             {
1765               /* If any single spilled regs happen to form groups,
1766                  count them now.  Maybe we don't really need
1767                  to spill another group.  */
1768               count_possible_groups (group_size, group_mode, max_groups,
1769                                      class);
1770
1771               if (max_groups[class] <= 0)
1772                 break;
1773
1774               /* Groups of size 2 (the only groups used on most machines)
1775                  are treated specially.  */
1776               if (group_size[class] == 2)
1777                 {
1778                   /* First, look for a register that will complete a group.  */
1779                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1780                     {
1781                       int other;
1782
1783                       j = potential_reload_regs[i];
1784                       if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1785                           &&
1786                           ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1787                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1788                             && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1789                             && HARD_REGNO_MODE_OK (other, group_mode[class])
1790                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1791                                                     other)
1792                             /* We don't want one part of another group.
1793                                We could get "two groups" that overlap!  */
1794                             && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1795                            ||
1796                            (j < FIRST_PSEUDO_REGISTER - 1
1797                             && (other = j + 1, spill_reg_order[other] >= 0)
1798                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1799                             && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1800                             && HARD_REGNO_MODE_OK (j, group_mode[class])
1801                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1802                                                     other)
1803                             && ! TEST_HARD_REG_BIT (counted_for_groups,
1804                                                     other))))
1805                         {
1806                           register enum reg_class *p;
1807
1808                           /* We have found one that will complete a group,
1809                              so count off one group as provided.  */
1810                           max_groups[class]--;
1811                           p = reg_class_superclasses[class];
1812                           while (*p != LIM_REG_CLASSES)
1813                             {
1814                               if (group_size [(int) *p] <= group_size [class])
1815                                 max_groups[(int) *p]--;
1816                               p++;
1817                             }
1818
1819                           /* Indicate both these regs are part of a group.  */
1820                           SET_HARD_REG_BIT (counted_for_groups, j);
1821                           SET_HARD_REG_BIT (counted_for_groups, other);
1822                           break;
1823                         }
1824                     }
1825                   /* We can't complete a group, so start one.  */
1826                   /* Look for a pair neither of which is explicitly used.  */
1827                   if (SMALL_REGISTER_CLASSES && i == FIRST_PSEUDO_REGISTER)
1828                     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1829                       {
1830                         int k;
1831                         j = potential_reload_regs[i];
1832                         /* Verify that J+1 is a potential reload reg.  */
1833                         for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1834                           if (potential_reload_regs[k] == j + 1)
1835                             break;
1836                         if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1837                             && k < FIRST_PSEUDO_REGISTER
1838                             && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1839                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1840                             && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1841                             && HARD_REGNO_MODE_OK (j, group_mode[class])
1842                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1843                                                     j + 1)
1844                             && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1)
1845                             /* Reject J at this stage
1846                                if J+1 was explicitly used.  */
1847                             && ! regs_explicitly_used[j + 1])
1848                           break;
1849                       }
1850                   /* Now try any group at all
1851                      whose registers are not in bad_spill_regs.  */
1852                   if (i == FIRST_PSEUDO_REGISTER)
1853                     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1854                       {
1855                         int k;
1856                         j = potential_reload_regs[i];
1857                         /* Verify that J+1 is a potential reload reg.  */
1858                         for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
1859                           if (potential_reload_regs[k] == j + 1)
1860                             break;
1861                         if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1862                             && k < FIRST_PSEUDO_REGISTER
1863                             && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1864                             && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1865                             && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1866                             && HARD_REGNO_MODE_OK (j, group_mode[class])
1867                             && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1868                                                     j + 1)
1869                             && ! TEST_HARD_REG_BIT (bad_spill_regs, j + 1))
1870                           break;
1871                       }
1872
1873                   /* I should be the index in potential_reload_regs
1874                      of the new reload reg we have found.  */
1875
1876                   if (i >= FIRST_PSEUDO_REGISTER)
1877                     {
1878                       /* There are no groups left to spill.  */
1879                       spill_failure (max_groups_insn[class]);
1880                       failure = 1;
1881                       goto failed;
1882                     }
1883                   else
1884                     something_changed
1885                       |= new_spill_reg (i, class, max_needs, NULL_PTR,
1886                                         global, dumpfile);
1887                 }
1888               else
1889                 {
1890                   /* For groups of more than 2 registers,
1891                      look for a sufficient sequence of unspilled registers,
1892                      and spill them all at once.  */
1893                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1894                     {
1895                       int k;
1896
1897                       j = potential_reload_regs[i];
1898                       if (j >= 0
1899                           && j + group_size[class] <= FIRST_PSEUDO_REGISTER
1900                           && HARD_REGNO_MODE_OK (j, group_mode[class]))
1901                         {
1902                           /* Check each reg in the sequence.  */
1903                           for (k = 0; k < group_size[class]; k++)
1904                             if (! (spill_reg_order[j + k] < 0
1905                                    && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1906                                    && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1907                               break;
1908                           /* We got a full sequence, so spill them all.  */
1909                           if (k == group_size[class])
1910                             {
1911                               register enum reg_class *p;
1912                               for (k = 0; k < group_size[class]; k++)
1913                                 {
1914                                   int idx;
1915                                   SET_HARD_REG_BIT (counted_for_groups, j + k);
1916                                   for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1917                                     if (potential_reload_regs[idx] == j + k)
1918                                       break;
1919                                   something_changed
1920                                     |= new_spill_reg (idx, class,
1921                                                       max_needs, NULL_PTR,
1922                                                       global, dumpfile);
1923                                 }
1924
1925                               /* We have found one that will complete a group,
1926                                  so count off one group as provided.  */
1927                               max_groups[class]--;
1928                               p = reg_class_superclasses[class];
1929                               while (*p != LIM_REG_CLASSES)
1930                                 {
1931                                   if (group_size [(int) *p]
1932                                       <= group_size [class])
1933                                     max_groups[(int) *p]--;
1934                                   p++;
1935                                 }
1936                               break;
1937                             }
1938                         }
1939                     }
1940                   /* We couldn't find any registers for this reload.
1941                      Avoid going into an infinite loop.  */
1942                   if (i >= FIRST_PSEUDO_REGISTER)
1943                     {
1944                       /* There are no groups left.  */
1945                       spill_failure (max_groups_insn[class]);
1946                       failure = 1;
1947                       goto failed;
1948                     }
1949                 }
1950             }
1951
1952           /* Now similarly satisfy all need for single registers.  */
1953
1954           while (max_needs[class] > 0 || max_nongroups[class] > 0)
1955             {
1956               /* If we spilled enough regs, but they weren't counted
1957                  against the non-group need, see if we can count them now.
1958                  If so, we can avoid some actual spilling.  */
1959               if (max_needs[class] <= 0 && max_nongroups[class] > 0)
1960                 for (i = 0; i < n_spills; i++)
1961                   if (TEST_HARD_REG_BIT (reg_class_contents[class],
1962                                          spill_regs[i])
1963                       && !TEST_HARD_REG_BIT (counted_for_groups,
1964                                              spill_regs[i])
1965                       && !TEST_HARD_REG_BIT (counted_for_nongroups,
1966                                              spill_regs[i])
1967                       && max_nongroups[class] > 0)
1968                     {
1969                       register enum reg_class *p;
1970
1971                       SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1972                       max_nongroups[class]--;
1973                       p = reg_class_superclasses[class];
1974                       while (*p != LIM_REG_CLASSES)
1975                         max_nongroups[(int) *p++]--;
1976                     }
1977               if (max_needs[class] <= 0 && max_nongroups[class] <= 0)
1978                 break;
1979
1980               /* Consider the potential reload regs that aren't
1981                  yet in use as reload regs, in order of preference.
1982                  Find the most preferred one that's in this class.  */
1983
1984               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1985                 if (potential_reload_regs[i] >= 0
1986                     && TEST_HARD_REG_BIT (reg_class_contents[class],
1987                                           potential_reload_regs[i])
1988                     /* If this reg will not be available for groups,
1989                        pick one that does not foreclose possible groups.
1990                        This is a kludge, and not very general,
1991                        but it should be sufficient to make the 386 work,
1992                        and the problem should not occur on machines with
1993                        more registers.  */
1994                     && (max_nongroups[class] == 0
1995                         || possible_group_p (potential_reload_regs[i], max_groups)))
1996                   break;
1997
1998               /* If we couldn't get a register, try to get one even if we
1999                  might foreclose possible groups.  This may cause problems
2000                  later, but that's better than aborting now, since it is
2001                  possible that we will, in fact, be able to form the needed
2002                  group even with this allocation.  */
2003
2004               if (i >= FIRST_PSEUDO_REGISTER
2005                   && (asm_noperands (max_needs[class] > 0
2006                                      ? max_needs_insn[class]
2007                                      : max_nongroups_insn[class])
2008                       < 0))
2009                 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2010                   if (potential_reload_regs[i] >= 0
2011                       && TEST_HARD_REG_BIT (reg_class_contents[class],
2012                                             potential_reload_regs[i]))
2013                     break;
2014
2015               /* I should be the index in potential_reload_regs
2016                  of the new reload reg we have found.  */
2017
2018               if (i >= FIRST_PSEUDO_REGISTER)
2019                 {
2020                   /* There are no possible registers left to spill.  */
2021                   spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
2022                                  : max_nongroups_insn[class]);
2023                   failure = 1;
2024                   goto failed;
2025                 }
2026               else
2027                 something_changed
2028                   |= new_spill_reg (i, class, max_needs, max_nongroups,
2029                                     global, dumpfile);
2030             }
2031         }
2032     }
2033
2034   /* If global-alloc was run, notify it of any register eliminations we have
2035      done.  */
2036   if (global)
2037     for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2038       if (ep->can_eliminate)
2039         mark_elimination (ep->from, ep->to);
2040
2041   /* Insert code to save and restore call-clobbered hard regs
2042      around calls.  Tell if what mode to use so that we will process
2043      those insns in reload_as_needed if we have to.  */
2044
2045   if (caller_save_needed)
2046     save_call_clobbered_regs (num_eliminable ? QImode
2047                               : caller_save_spill_class != NO_REGS ? HImode
2048                               : VOIDmode);
2049
2050   /* If a pseudo has no hard reg, delete the insns that made the equivalence.
2051      If that insn didn't set the register (i.e., it copied the register to
2052      memory), just delete that insn instead of the equivalencing insn plus
2053      anything now dead.  If we call delete_dead_insn on that insn, we may
2054      delete the insn that actually sets the register if the register die
2055      there and that is incorrect.  */
2056
2057   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2058     if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
2059         && GET_CODE (reg_equiv_init[i]) != NOTE)
2060       {
2061         if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
2062           delete_dead_insn (reg_equiv_init[i]);
2063         else
2064           {
2065             PUT_CODE (reg_equiv_init[i], NOTE);
2066             NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
2067             NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
2068           }
2069       }
2070
2071   /* Use the reload registers where necessary
2072      by generating move instructions to move the must-be-register
2073      values into or out of the reload registers.  */
2074
2075   if (something_needs_reloads || something_needs_elimination
2076       || (caller_save_needed && num_eliminable)
2077       || caller_save_spill_class != NO_REGS)
2078     reload_as_needed (first, global);
2079
2080   /* If we were able to eliminate the frame pointer, show that it is no
2081      longer live at the start of any basic block.  If it ls live by
2082      virtue of being in a pseudo, that pseudo will be marked live
2083      and hence the frame pointer will be known to be live via that
2084      pseudo.  */
2085
2086   if (! frame_pointer_needed)
2087     for (i = 0; i < n_basic_blocks; i++)
2088       CLEAR_REGNO_REG_SET (basic_block_live_at_start[i],
2089                            HARD_FRAME_POINTER_REGNUM);
2090
2091   /* Come here (with failure set nonzero) if we can't get enough spill regs
2092      and we decide not to abort about it.  */
2093  failed:
2094
2095   reload_in_progress = 0;
2096
2097   /* Now eliminate all pseudo regs by modifying them into
2098      their equivalent memory references.
2099      The REG-rtx's for the pseudos are modified in place,
2100      so all insns that used to refer to them now refer to memory.
2101
2102      For a reg that has a reg_equiv_address, all those insns
2103      were changed by reloading so that no insns refer to it any longer;
2104      but the DECL_RTL of a variable decl may refer to it,
2105      and if so this causes the debugging info to mention the variable.  */
2106
2107   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2108     {
2109       rtx addr = 0;
2110       int in_struct = 0;
2111       if (reg_equiv_mem[i])
2112         {
2113           addr = XEXP (reg_equiv_mem[i], 0);
2114           in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
2115         }
2116       if (reg_equiv_address[i])
2117         addr = reg_equiv_address[i];
2118       if (addr)
2119         {
2120           if (reg_renumber[i] < 0)
2121             {
2122               rtx reg = regno_reg_rtx[i];
2123               XEXP (reg, 0) = addr;
2124               REG_USERVAR_P (reg) = 0;
2125               MEM_IN_STRUCT_P (reg) = in_struct;
2126               PUT_CODE (reg, MEM);
2127             }
2128           else if (reg_equiv_mem[i])
2129             XEXP (reg_equiv_mem[i], 0) = addr;
2130         }
2131     }
2132
2133 #ifdef PRESERVE_DEATH_INFO_REGNO_P
2134   /* Make a pass over all the insns and remove death notes for things that
2135      are no longer registers or no longer die in the insn (e.g., an input
2136      and output pseudo being tied).  */
2137
2138   for (insn = first; insn; insn = NEXT_INSN (insn))
2139     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2140       {
2141         rtx note, next;
2142
2143         for (note = REG_NOTES (insn); note; note = next)
2144           {
2145             next = XEXP (note, 1);
2146             if (REG_NOTE_KIND (note) == REG_DEAD
2147                 && (GET_CODE (XEXP (note, 0)) != REG
2148                     || reg_set_p (XEXP (note, 0), PATTERN (insn))))
2149               remove_note (insn, note);
2150           }
2151       }
2152 #endif
2153
2154   /* If we are doing stack checking, give a warning if this function's
2155      frame size is larger than we expect.  */
2156   if (flag_stack_check && ! STACK_CHECK_BUILTIN)
2157     {
2158       HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
2159
2160       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2161         if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
2162           size += UNITS_PER_WORD;
2163
2164       if (size > STACK_CHECK_MAX_FRAME_SIZE)
2165         warning ("frame size too large for reliable stack checking");
2166     }
2167         
2168   /* Indicate that we no longer have known memory locations or constants.  */
2169   reg_equiv_constant = 0;
2170   reg_equiv_memory_loc = 0;
2171
2172   if (real_known_ptr)
2173     free (real_known_ptr);
2174   if (real_at_ptr)
2175     free (real_at_ptr);
2176
2177   if (scratch_list)
2178     free (scratch_list);
2179   scratch_list = 0;
2180   if (scratch_block)
2181     free (scratch_block);
2182   scratch_block = 0;
2183
2184   CLEAR_HARD_REG_SET (used_spill_regs);
2185   for (i = 0; i < n_spills; i++)
2186     SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
2187
2188   return failure;
2189 }
2190 \f
2191 /* Nonzero if, after spilling reg REGNO for non-groups,
2192    it will still be possible to find a group if we still need one.  */
2193
2194 static int
2195 possible_group_p (regno, max_groups)
2196      int regno;
2197      int *max_groups;
2198 {
2199   int i;
2200   int class = (int) NO_REGS;
2201
2202   for (i = 0; i < (int) N_REG_CLASSES; i++)
2203     if (max_groups[i] > 0)
2204       {
2205         class = i;
2206         break;
2207       }
2208
2209   if (class == (int) NO_REGS)
2210     return 1;
2211
2212   /* Consider each pair of consecutive registers.  */
2213   for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
2214     {
2215       /* Ignore pairs that include reg REGNO.  */
2216       if (i == regno || i + 1 == regno)
2217         continue;
2218
2219       /* Ignore pairs that are outside the class that needs the group.
2220          ??? Here we fail to handle the case where two different classes
2221          independently need groups.  But this never happens with our
2222          current machine descriptions.  */
2223       if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
2224              && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
2225         continue;
2226
2227       /* A pair of consecutive regs we can still spill does the trick.  */
2228       if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
2229           && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2230           && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
2231         return 1;
2232
2233       /* A pair of one already spilled and one we can spill does it
2234          provided the one already spilled is not otherwise reserved.  */
2235       if (spill_reg_order[i] < 0
2236           && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
2237           && spill_reg_order[i + 1] >= 0
2238           && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
2239           && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
2240         return 1;
2241       if (spill_reg_order[i + 1] < 0
2242           && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
2243           && spill_reg_order[i] >= 0
2244           && ! TEST_HARD_REG_BIT (counted_for_groups, i)
2245           && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
2246         return 1;
2247     }
2248
2249   return 0;
2250 }
2251 \f
2252 /* Count any groups of CLASS that can be formed from the registers recently
2253    spilled.  */
2254
2255 static void
2256 count_possible_groups (group_size, group_mode, max_groups, class)
2257      int *group_size;
2258      enum machine_mode *group_mode;
2259      int *max_groups;
2260      int class;
2261 {
2262   HARD_REG_SET new;
2263   int i, j;
2264
2265   /* Now find all consecutive groups of spilled registers
2266      and mark each group off against the need for such groups.
2267      But don't count them against ordinary need, yet.  */
2268
2269   if (group_size[class] == 0)
2270     return;
2271
2272   CLEAR_HARD_REG_SET (new);
2273
2274   /* Make a mask of all the regs that are spill regs in class I.  */
2275   for (i = 0; i < n_spills; i++)
2276     if (TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
2277         && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i])
2278         && ! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
2279       SET_HARD_REG_BIT (new, spill_regs[i]);
2280
2281   /* Find each consecutive group of them.  */
2282   for (i = 0; i < FIRST_PSEUDO_REGISTER && max_groups[class] > 0; i++)
2283     if (TEST_HARD_REG_BIT (new, i)
2284         && i + group_size[class] <= FIRST_PSEUDO_REGISTER
2285         && HARD_REGNO_MODE_OK (i, group_mode[class]))
2286       {
2287         for (j = 1; j < group_size[class]; j++)
2288           if (! TEST_HARD_REG_BIT (new, i + j))
2289             break;
2290
2291         if (j == group_size[class])
2292           {
2293             /* We found a group.  Mark it off against this class's need for
2294                groups, and against each superclass too.  */
2295             register enum reg_class *p;
2296
2297             max_groups[class]--;
2298             p = reg_class_superclasses[class];
2299             while (*p != LIM_REG_CLASSES)
2300               {
2301                 if (group_size [(int) *p] <= group_size [class])
2302                   max_groups[(int) *p]--;
2303                 p++;
2304               }
2305
2306             /* Don't count these registers again.  */
2307             for (j = 0; j < group_size[class]; j++)
2308               SET_HARD_REG_BIT (counted_for_groups, i + j);
2309           }
2310
2311         /* Skip to the last reg in this group.  When i is incremented above,
2312            it will then point to the first reg of the next possible group.  */
2313         i += j - 1;
2314       }
2315 }
2316 \f
2317 /* ALLOCATE_MODE is a register mode that needs to be reloaded.  OTHER_MODE is
2318    another mode that needs to be reloaded for the same register class CLASS.
2319    If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2320    ALLOCATE_MODE will never be smaller than OTHER_MODE.
2321
2322    This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2323    ALLOCATE_MODE.  This test is unnecessary, because we will never try to put
2324    something of mode ALLOCATE_MODE into an OTHER_MODE register.  Testing this
2325    causes unnecessary failures on machines requiring alignment of register
2326    groups when the two modes are different sizes, because the larger mode has
2327    more strict alignment rules than the smaller mode.  */
2328
2329 static int
2330 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2331      enum machine_mode allocate_mode, other_mode;
2332      enum reg_class class;
2333 {
2334   register int regno;
2335   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2336     {
2337       if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2338           && HARD_REGNO_MODE_OK (regno, allocate_mode)
2339           && ! HARD_REGNO_MODE_OK (regno, other_mode))
2340         return 0;
2341     }
2342   return 1;
2343 }
2344
2345 /* Handle the failure to find a register to spill.
2346    INSN should be one of the insns which needed this particular spill reg.  */
2347
2348 static void
2349 spill_failure (insn)
2350      rtx insn;
2351 {
2352   if (asm_noperands (PATTERN (insn)) >= 0)
2353     error_for_asm (insn, "`asm' needs too many reloads");
2354   else
2355     fatal_insn ("Unable to find a register to spill.", insn);
2356 }
2357
2358 /* Add a new register to the tables of available spill-registers
2359     (as well as spilling all pseudos allocated to the register).
2360    I is the index of this register in potential_reload_regs.
2361    CLASS is the regclass whose need is being satisfied.
2362    MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2363     so that this register can count off against them.
2364     MAX_NONGROUPS is 0 if this register is part of a group.
2365    GLOBAL and DUMPFILE are the same as the args that `reload' got.  */
2366
2367 static int
2368 new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2369      int i;
2370      int class;
2371      int *max_needs;
2372      int *max_nongroups;
2373      int global;
2374      FILE *dumpfile;
2375 {
2376   register enum reg_class *p;
2377   int val;
2378   int regno = potential_reload_regs[i];
2379
2380   if (i >= FIRST_PSEUDO_REGISTER)
2381     abort ();   /* Caller failed to find any register.  */
2382
2383   if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
2384     {
2385       static char *reg_class_names[] = REG_CLASS_NAMES;
2386       fatal ("fixed or forbidden register %d (%s) was spilled for class %s.\n\
2387 This may be due to a compiler bug or to impossible asm\n\
2388 statements or clauses.", regno, reg_names[regno], reg_class_names[class]);
2389     }
2390
2391   /* Make reg REGNO an additional reload reg.  */
2392
2393   potential_reload_regs[i] = -1;
2394   spill_regs[n_spills] = regno;
2395   spill_reg_order[regno] = n_spills;
2396   if (dumpfile)
2397     fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2398
2399   /* Clear off the needs we just satisfied.  */
2400
2401   max_needs[class]--;
2402   p = reg_class_superclasses[class];
2403   while (*p != LIM_REG_CLASSES)
2404     max_needs[(int) *p++]--;
2405
2406   if (max_nongroups && max_nongroups[class] > 0)
2407     {
2408       SET_HARD_REG_BIT (counted_for_nongroups, regno);
2409       max_nongroups[class]--;
2410       p = reg_class_superclasses[class];
2411       while (*p != LIM_REG_CLASSES)
2412         max_nongroups[(int) *p++]--;
2413     }
2414
2415   /* Spill every pseudo reg that was allocated to this reg
2416      or to something that overlaps this reg.  */
2417
2418   val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2419
2420   /* If there are some registers still to eliminate and this register
2421      wasn't ever used before, additional stack space may have to be
2422      allocated to store this register.  Thus, we may have changed the offset
2423      between the stack and frame pointers, so mark that something has changed.
2424      (If new pseudos were spilled, thus requiring more space, VAL would have
2425      been set non-zero by the call to spill_hard_reg above since additional
2426      reloads may be needed in that case.
2427
2428      One might think that we need only set VAL to 1 if this is a call-used
2429      register.  However, the set of registers that must be saved by the
2430      prologue is not identical to the call-used set.  For example, the
2431      register used by the call insn for the return PC is a call-used register,
2432      but must be saved by the prologue.  */
2433   if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2434     val = 1;
2435
2436   regs_ever_live[spill_regs[n_spills]] = 1;
2437   n_spills++;
2438
2439   return val;
2440 }
2441 \f
2442 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2443    data that is dead in INSN.  */
2444
2445 static void
2446 delete_dead_insn (insn)
2447      rtx insn;
2448 {
2449   rtx prev = prev_real_insn (insn);
2450   rtx prev_dest;
2451
2452   /* If the previous insn sets a register that dies in our insn, delete it
2453      too.  */
2454   if (prev && GET_CODE (PATTERN (prev)) == SET
2455       && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2456       && reg_mentioned_p (prev_dest, PATTERN (insn))
2457       && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2458     delete_dead_insn (prev);
2459
2460   PUT_CODE (insn, NOTE);
2461   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2462   NOTE_SOURCE_FILE (insn) = 0;
2463 }
2464
2465 /* Modify the home of pseudo-reg I.
2466    The new home is present in reg_renumber[I].
2467
2468    FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2469    or it may be -1, meaning there is none or it is not relevant.
2470    This is used so that all pseudos spilled from a given hard reg
2471    can share one stack slot.  */
2472
2473 static void
2474 alter_reg (i, from_reg)
2475      register int i;
2476      int from_reg;
2477 {
2478   /* When outputting an inline function, this can happen
2479      for a reg that isn't actually used.  */
2480   if (regno_reg_rtx[i] == 0)
2481     return;
2482
2483   /* If the reg got changed to a MEM at rtl-generation time,
2484      ignore it.  */
2485   if (GET_CODE (regno_reg_rtx[i]) != REG)
2486     return;
2487
2488   /* Modify the reg-rtx to contain the new hard reg
2489      number or else to contain its pseudo reg number.  */
2490   REGNO (regno_reg_rtx[i])
2491     = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2492
2493   /* If we have a pseudo that is needed but has no hard reg or equivalent,
2494      allocate a stack slot for it.  */
2495
2496   if (reg_renumber[i] < 0
2497       && REG_N_REFS (i) > 0
2498       && reg_equiv_constant[i] == 0
2499       && reg_equiv_memory_loc[i] == 0)
2500     {
2501       register rtx x;
2502       int inherent_size = PSEUDO_REGNO_BYTES (i);
2503       int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2504       int adjust = 0;
2505
2506       /* Each pseudo reg has an inherent size which comes from its own mode,
2507          and a total size which provides room for paradoxical subregs
2508          which refer to the pseudo reg in wider modes.
2509
2510          We can use a slot already allocated if it provides both
2511          enough inherent space and enough total space.
2512          Otherwise, we allocate a new slot, making sure that it has no less
2513          inherent space, and no less total space, then the previous slot.  */
2514       if (from_reg == -1)
2515         {
2516           /* No known place to spill from => no slot to reuse.  */
2517           x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
2518                                   inherent_size == total_size ? 0 : -1);
2519           if (BYTES_BIG_ENDIAN)
2520             /* Cancel the  big-endian correction done in assign_stack_local.
2521                Get the address of the beginning of the slot.
2522                This is so we can do a big-endian correction unconditionally
2523                below.  */
2524             adjust = inherent_size - total_size;
2525
2526           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2527         }
2528       /* Reuse a stack slot if possible.  */
2529       else if (spill_stack_slot[from_reg] != 0
2530                && spill_stack_slot_width[from_reg] >= total_size
2531                && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2532                    >= inherent_size))
2533         x = spill_stack_slot[from_reg];
2534       /* Allocate a bigger slot.  */
2535       else
2536         {
2537           /* Compute maximum size needed, both for inherent size
2538              and for total size.  */
2539           enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2540           rtx stack_slot;
2541           if (spill_stack_slot[from_reg])
2542             {
2543               if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2544                   > inherent_size)
2545                 mode = GET_MODE (spill_stack_slot[from_reg]);
2546               if (spill_stack_slot_width[from_reg] > total_size)
2547                 total_size = spill_stack_slot_width[from_reg];
2548             }
2549           /* Make a slot with that size.  */
2550           x = assign_stack_local (mode, total_size,
2551                                   inherent_size == total_size ? 0 : -1);
2552           stack_slot = x;
2553           if (BYTES_BIG_ENDIAN)
2554             {
2555               /* Cancel the  big-endian correction done in assign_stack_local.
2556                  Get the address of the beginning of the slot.
2557                  This is so we can do a big-endian correction unconditionally
2558                  below.  */
2559               adjust = GET_MODE_SIZE (mode) - total_size;
2560               if (adjust)
2561                 stack_slot = gen_rtx (MEM, mode_for_size (total_size
2562                                                           * BITS_PER_UNIT,
2563                                                           MODE_INT, 1),
2564                                       plus_constant (XEXP (x, 0), adjust));
2565             }
2566           spill_stack_slot[from_reg] = stack_slot;
2567           spill_stack_slot_width[from_reg] = total_size;
2568         }
2569
2570       /* On a big endian machine, the "address" of the slot
2571          is the address of the low part that fits its inherent mode.  */
2572       if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2573         adjust += (total_size - inherent_size);
2574
2575       /* If we have any adjustment to make, or if the stack slot is the
2576          wrong mode, make a new stack slot.  */
2577       if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2578         {
2579           x = gen_rtx (MEM, GET_MODE (regno_reg_rtx[i]),
2580                        plus_constant (XEXP (x, 0), adjust));
2581           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2582         }
2583
2584       /* Save the stack slot for later.   */
2585       reg_equiv_memory_loc[i] = x;
2586     }
2587 }
2588
2589 /* Mark the slots in regs_ever_live for the hard regs
2590    used by pseudo-reg number REGNO.  */
2591
2592 void
2593 mark_home_live (regno)
2594      int regno;
2595 {
2596   register int i, lim;
2597   i = reg_renumber[regno];
2598   if (i < 0)
2599     return;
2600   lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2601   while (i < lim)
2602     regs_ever_live[i++] = 1;
2603 }
2604
2605 /* Mark the registers used in SCRATCH as being live.  */
2606
2607 static void
2608 mark_scratch_live (scratch)
2609      rtx scratch;
2610 {
2611   register int i;
2612   int regno = REGNO (scratch);
2613   int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (scratch));
2614
2615   for (i = regno; i < lim; i++)
2616     regs_ever_live[i] = 1;
2617 }
2618 \f
2619 /* This function handles the tracking of elimination offsets around branches.
2620
2621    X is a piece of RTL being scanned.
2622
2623    INSN is the insn that it came from, if any.
2624
2625    INITIAL_P is non-zero if we are to set the offset to be the initial
2626    offset and zero if we are setting the offset of the label to be the
2627    current offset.  */
2628
2629 static void
2630 set_label_offsets (x, insn, initial_p)
2631      rtx x;
2632      rtx insn;
2633      int initial_p;
2634 {
2635   enum rtx_code code = GET_CODE (x);
2636   rtx tem;
2637   int i;
2638   struct elim_table *p;
2639
2640   switch (code)
2641     {
2642     case LABEL_REF:
2643       if (LABEL_REF_NONLOCAL_P (x))
2644         return;
2645
2646       x = XEXP (x, 0);
2647
2648       /* ... fall through ...  */
2649
2650     case CODE_LABEL:
2651       /* If we know nothing about this label, set the desired offsets.  Note
2652          that this sets the offset at a label to be the offset before a label
2653          if we don't know anything about the label.  This is not correct for
2654          the label after a BARRIER, but is the best guess we can make.  If
2655          we guessed wrong, we will suppress an elimination that might have
2656          been possible had we been able to guess correctly.  */
2657
2658       if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2659         {
2660           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2661             offsets_at[CODE_LABEL_NUMBER (x)][i]
2662               = (initial_p ? reg_eliminate[i].initial_offset
2663                  : reg_eliminate[i].offset);
2664           offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2665         }
2666
2667       /* Otherwise, if this is the definition of a label and it is
2668          preceded by a BARRIER, set our offsets to the known offset of
2669          that label.  */
2670
2671       else if (x == insn
2672                && (tem = prev_nonnote_insn (insn)) != 0
2673                && GET_CODE (tem) == BARRIER)
2674         {
2675           num_not_at_initial_offset = 0;
2676           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2677             {
2678               reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2679                 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2680               if (reg_eliminate[i].can_eliminate
2681                   && (reg_eliminate[i].offset
2682                       != reg_eliminate[i].initial_offset))
2683                 num_not_at_initial_offset++;
2684             }
2685         }
2686
2687       else
2688         /* If neither of the above cases is true, compare each offset
2689            with those previously recorded and suppress any eliminations
2690            where the offsets disagree.  */
2691
2692         for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2693           if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2694               != (initial_p ? reg_eliminate[i].initial_offset
2695                   : reg_eliminate[i].offset))
2696             reg_eliminate[i].can_eliminate = 0;
2697
2698       return;
2699
2700     case JUMP_INSN:
2701       set_label_offsets (PATTERN (insn), insn, initial_p);
2702
2703       /* ... fall through ...  */
2704
2705     case INSN:
2706     case CALL_INSN:
2707       /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2708          and hence must have all eliminations at their initial offsets.  */
2709       for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2710         if (REG_NOTE_KIND (tem) == REG_LABEL)
2711           set_label_offsets (XEXP (tem, 0), insn, 1);
2712       return;
2713
2714     case ADDR_VEC:
2715     case ADDR_DIFF_VEC:
2716       /* Each of the labels in the address vector must be at their initial
2717          offsets.  We want the first first for ADDR_VEC and the second
2718          field for ADDR_DIFF_VEC.  */
2719
2720       for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2721         set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2722                            insn, initial_p);
2723       return;
2724
2725     case SET:
2726       /* We only care about setting PC.  If the source is not RETURN,
2727          IF_THEN_ELSE, or a label, disable any eliminations not at
2728          their initial offsets.  Similarly if any arm of the IF_THEN_ELSE
2729          isn't one of those possibilities.  For branches to a label,
2730          call ourselves recursively.
2731
2732          Note that this can disable elimination unnecessarily when we have
2733          a non-local goto since it will look like a non-constant jump to
2734          someplace in the current function.  This isn't a significant
2735          problem since such jumps will normally be when all elimination
2736          pairs are back to their initial offsets.  */
2737
2738       if (SET_DEST (x) != pc_rtx)
2739         return;
2740
2741       switch (GET_CODE (SET_SRC (x)))
2742         {
2743         case PC:
2744         case RETURN:
2745           return;
2746
2747         case LABEL_REF:
2748           set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2749           return;
2750
2751         case IF_THEN_ELSE:
2752           tem = XEXP (SET_SRC (x), 1);
2753           if (GET_CODE (tem) == LABEL_REF)
2754             set_label_offsets (XEXP (tem, 0), insn, initial_p);
2755           else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2756             break;
2757
2758           tem = XEXP (SET_SRC (x), 2);
2759           if (GET_CODE (tem) == LABEL_REF)
2760             set_label_offsets (XEXP (tem, 0), insn, initial_p);
2761           else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2762             break;
2763           return;
2764
2765         default:
2766           break;
2767         }
2768
2769       /* If we reach here, all eliminations must be at their initial
2770          offset because we are doing a jump to a variable address.  */
2771       for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2772         if (p->offset != p->initial_offset)
2773           p->can_eliminate = 0;
2774       break;
2775       
2776     default:
2777       break;
2778     }
2779 }
2780 \f
2781 /* Used for communication between the next two function to properly share
2782    the vector for an ASM_OPERANDS.  */
2783
2784 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2785
2786 /* Scan X and replace any eliminable registers (such as fp) with a
2787    replacement (such as sp), plus an offset.
2788
2789    MEM_MODE is the mode of an enclosing MEM.  We need this to know how
2790    much to adjust a register for, e.g., PRE_DEC.  Also, if we are inside a
2791    MEM, we are allowed to replace a sum of a register and the constant zero
2792    with the register, which we cannot do outside a MEM.  In addition, we need
2793    to record the fact that a register is referenced outside a MEM.
2794
2795    If INSN is an insn, it is the insn containing X.  If we replace a REG
2796    in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2797    CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2798    that the REG is being modified.
2799
2800    Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2801    That's used when we eliminate in expressions stored in notes.
2802    This means, do not set ref_outside_mem even if the reference
2803    is outside of MEMs.
2804
2805    If we see a modification to a register we know about, take the
2806    appropriate action (see case SET, below).
2807
2808    REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2809    replacements done assuming all offsets are at their initial values.  If
2810    they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2811    encounter, return the actual location so that find_reloads will do
2812    the proper thing.  */
2813
2814 rtx
2815 eliminate_regs (x, mem_mode, insn, storing)
2816      rtx x;
2817      enum machine_mode mem_mode;
2818      rtx insn;
2819      int storing;
2820 {
2821   enum rtx_code code = GET_CODE (x);
2822   struct elim_table *ep;
2823   int regno;
2824   rtx new;
2825   int i, j;
2826   char *fmt;
2827   int copied = 0;
2828
2829   switch (code)
2830     {
2831     case CONST_INT:
2832     case CONST_DOUBLE:
2833     case CONST:
2834     case SYMBOL_REF:
2835     case CODE_LABEL:
2836     case PC:
2837     case CC0:
2838     case ASM_INPUT:
2839     case ADDR_VEC:
2840     case ADDR_DIFF_VEC:
2841     case RETURN:
2842       return x;
2843
2844     case ADDRESSOF:
2845       /* This is only for the benefit of the debugging backends, which call
2846          eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2847          removed after CSE.  */
2848       new = eliminate_regs (XEXP (x, 0), 0, insn, 0);
2849       if (GET_CODE (new) == MEM)
2850         return XEXP (new, 0);
2851       return x;
2852
2853     case REG:
2854       regno = REGNO (x);
2855
2856       /* First handle the case where we encounter a bare register that
2857          is eliminable.  Replace it with a PLUS.  */
2858       if (regno < FIRST_PSEUDO_REGISTER)
2859         {
2860           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2861                ep++)
2862             if (ep->from_rtx == x && ep->can_eliminate)
2863               {
2864                 if (! mem_mode
2865                     /* Refs inside notes don't count for this purpose.  */
2866                     && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2867                                         || GET_CODE (insn) == INSN_LIST)))
2868                   ep->ref_outside_mem = 1;
2869                 return plus_constant (ep->to_rtx, ep->previous_offset);
2870               }
2871
2872         }
2873       else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2874                && (reg_equiv_address[regno] || num_not_at_initial_offset))
2875         {
2876           /* In this case, find_reloads would attempt to either use an
2877              incorrect address (if something is not at its initial offset)
2878              or substitute an replaced address into an insn (which loses
2879              if the offset is changed by some later action).  So we simply
2880              return the replaced stack slot (assuming it is changed by
2881              elimination) and ignore the fact that this is actually a
2882              reference to the pseudo.  Ensure we make a copy of the
2883              address in case it is shared.  */
2884           new = eliminate_regs (reg_equiv_memory_loc[regno],
2885                                 mem_mode, insn, 0);
2886           if (new != reg_equiv_memory_loc[regno])
2887             {
2888               cannot_omit_stores[regno] = 1;
2889               return copy_rtx (new);
2890             }
2891         }
2892       return x;
2893
2894     case PLUS:
2895       /* If this is the sum of an eliminable register and a constant, rework
2896          the sum.   */
2897       if (GET_CODE (XEXP (x, 0)) == REG
2898           && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2899           && CONSTANT_P (XEXP (x, 1)))
2900         {
2901           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2902                ep++)
2903             if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2904               {
2905                 if (! mem_mode
2906                     /* Refs inside notes don't count for this purpose.  */
2907                     && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2908                                         || GET_CODE (insn) == INSN_LIST)))
2909                   ep->ref_outside_mem = 1;
2910
2911                 /* The only time we want to replace a PLUS with a REG (this
2912                    occurs when the constant operand of the PLUS is the negative
2913                    of the offset) is when we are inside a MEM.  We won't want
2914                    to do so at other times because that would change the
2915                    structure of the insn in a way that reload can't handle.
2916                    We special-case the commonest situation in
2917                    eliminate_regs_in_insn, so just replace a PLUS with a
2918                    PLUS here, unless inside a MEM.  */
2919                 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2920                     && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2921                   return ep->to_rtx;
2922                 else
2923                   return gen_rtx (PLUS, Pmode, ep->to_rtx,
2924                                   plus_constant (XEXP (x, 1),
2925                                                  ep->previous_offset));
2926               }
2927
2928           /* If the register is not eliminable, we are done since the other
2929              operand is a constant.  */
2930           return x;
2931         }
2932
2933       /* If this is part of an address, we want to bring any constant to the
2934          outermost PLUS.  We will do this by doing register replacement in
2935          our operands and seeing if a constant shows up in one of them.
2936
2937          We assume here this is part of an address (or a "load address" insn)
2938          since an eliminable register is not likely to appear in any other
2939          context.
2940
2941          If we have (plus (eliminable) (reg)), we want to produce
2942          (plus (plus (replacement) (reg) (const))).  If this was part of a
2943          normal add insn, (plus (replacement) (reg)) will be pushed as a
2944          reload.  This is the desired action.  */
2945
2946       {
2947         rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
2948         rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn, 0);
2949
2950         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2951           {
2952             /* If one side is a PLUS and the other side is a pseudo that
2953                didn't get a hard register but has a reg_equiv_constant,
2954                we must replace the constant here since it may no longer
2955                be in the position of any operand.  */
2956             if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2957                 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2958                 && reg_renumber[REGNO (new1)] < 0
2959                 && reg_equiv_constant != 0
2960                 && reg_equiv_constant[REGNO (new1)] != 0)
2961               new1 = reg_equiv_constant[REGNO (new1)];
2962             else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2963                      && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2964                      && reg_renumber[REGNO (new0)] < 0
2965                      && reg_equiv_constant[REGNO (new0)] != 0)
2966               new0 = reg_equiv_constant[REGNO (new0)];
2967
2968             new = form_sum (new0, new1);
2969
2970             /* As above, if we are not inside a MEM we do not want to
2971                turn a PLUS into something else.  We might try to do so here
2972                for an addition of 0 if we aren't optimizing.  */
2973             if (! mem_mode && GET_CODE (new) != PLUS)
2974               return gen_rtx (PLUS, GET_MODE (x), new, const0_rtx);
2975             else
2976               return new;
2977           }
2978       }
2979       return x;
2980
2981     case MULT:
2982       /* If this is the product of an eliminable register and a 
2983          constant, apply the distribute law and move the constant out
2984          so that we have (plus (mult ..) ..).  This is needed in order
2985          to keep load-address insns valid.   This case is pathological.
2986          We ignore the possibility of overflow here.  */
2987       if (GET_CODE (XEXP (x, 0)) == REG
2988           && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2989           && GET_CODE (XEXP (x, 1)) == CONST_INT)
2990         for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2991              ep++)
2992           if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2993             {
2994               if (! mem_mode
2995                   /* Refs inside notes don't count for this purpose.  */
2996                   && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2997                                       || GET_CODE (insn) == INSN_LIST)))
2998                 ep->ref_outside_mem = 1;
2999
3000               return
3001                 plus_constant (gen_rtx (MULT, Pmode, ep->to_rtx, XEXP (x, 1)),
3002                                ep->previous_offset * INTVAL (XEXP (x, 1)));
3003             }
3004
3005       /* ... fall through ...  */
3006
3007     case CALL:
3008     case COMPARE:
3009     case MINUS:
3010     case DIV:      case UDIV:
3011     case MOD:      case UMOD:
3012     case AND:      case IOR:      case XOR:
3013     case ROTATERT: case ROTATE:
3014     case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3015     case NE:       case EQ:
3016     case GE:       case GT:       case GEU:    case GTU:
3017     case LE:       case LT:       case LEU:    case LTU:
3018       {
3019         rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
3020         rtx new1
3021           = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn, 0) : 0;
3022
3023         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
3024           return gen_rtx (code, GET_MODE (x), new0, new1);
3025       }
3026       return x;
3027
3028     case EXPR_LIST:
3029       /* If we have something in XEXP (x, 0), the usual case, eliminate it.  */
3030       if (XEXP (x, 0))
3031         {
3032           new = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
3033           if (new != XEXP (x, 0))
3034             x = gen_rtx (EXPR_LIST, REG_NOTE_KIND (x), new, XEXP (x, 1));
3035         }
3036
3037       /* ... fall through ...  */
3038
3039     case INSN_LIST:
3040       /* Now do eliminations in the rest of the chain.  If this was
3041          an EXPR_LIST, this might result in allocating more memory than is
3042          strictly needed, but it simplifies the code.  */
3043       if (XEXP (x, 1))
3044         {
3045           new = eliminate_regs (XEXP (x, 1), mem_mode, insn, 0);
3046           if (new != XEXP (x, 1))
3047             return gen_rtx (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
3048         }
3049       return x;
3050
3051     case PRE_INC:
3052     case POST_INC:
3053     case PRE_DEC:
3054     case POST_DEC:
3055       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3056         if (ep->to_rtx == XEXP (x, 0))
3057           {
3058             int size = GET_MODE_SIZE (mem_mode);
3059
3060             /* If more bytes than MEM_MODE are pushed, account for them.  */
3061 #ifdef PUSH_ROUNDING
3062             if (ep->to_rtx == stack_pointer_rtx)
3063               size = PUSH_ROUNDING (size);
3064 #endif
3065             if (code == PRE_DEC || code == POST_DEC)
3066               ep->offset += size;
3067             else
3068               ep->offset -= size;
3069           }
3070
3071       /* Fall through to generic unary operation case.  */
3072     case STRICT_LOW_PART:
3073     case NEG:          case NOT:
3074     case SIGN_EXTEND:  case ZERO_EXTEND:
3075     case TRUNCATE:     case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3076     case FLOAT:        case FIX:
3077     case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3078     case ABS:
3079     case SQRT:
3080     case FFS:
3081       new = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
3082       if (new != XEXP (x, 0))
3083         return gen_rtx (code, GET_MODE (x), new);
3084       return x;
3085
3086     case SUBREG:
3087       /* Similar to above processing, but preserve SUBREG_WORD.
3088          Convert (subreg (mem)) to (mem) if not paradoxical.
3089          Also, if we have a non-paradoxical (subreg (pseudo)) and the
3090          pseudo didn't get a hard reg, we must replace this with the
3091          eliminated version of the memory location because push_reloads
3092          may do the replacement in certain circumstances.  */
3093       if (GET_CODE (SUBREG_REG (x)) == REG
3094           && (GET_MODE_SIZE (GET_MODE (x))
3095               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3096           && reg_equiv_memory_loc != 0
3097           && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3098         {
3099           new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
3100                                 mem_mode, insn, 0);
3101
3102           /* If we didn't change anything, we must retain the pseudo.  */
3103           if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
3104             new = SUBREG_REG (x);
3105           else
3106             {
3107               /* Otherwise, ensure NEW isn't shared in case we have to reload
3108                  it.  */
3109               new = copy_rtx (new);
3110
3111               /* In this case, we must show that the pseudo is used in this
3112                  insn so that delete_output_reload will do the right thing.  */
3113               if (insn != 0 && GET_CODE (insn) != EXPR_LIST
3114                   && GET_CODE (insn) != INSN_LIST)
3115                 emit_insn_before (gen_rtx (USE, VOIDmode, SUBREG_REG (x)),
3116                                   insn);
3117             }
3118         }
3119       else
3120         new = eliminate_regs (SUBREG_REG (x), mem_mode, insn, 0);
3121
3122       if (new != XEXP (x, 0))
3123         {
3124           int x_size = GET_MODE_SIZE (GET_MODE (x));
3125           int new_size = GET_MODE_SIZE (GET_MODE (new));
3126
3127           /* When asked to spill a partial word subreg, we need to go
3128              ahead and spill the whole thing against the possibility
3129              that we reload the whole reg and find garbage at the top.  */
3130           if (storing
3131               && GET_CODE (new) == MEM
3132               && x_size < new_size
3133               && ((x_size + UNITS_PER_WORD-1) / UNITS_PER_WORD
3134                   == (new_size + UNITS_PER_WORD-1) / UNITS_PER_WORD))
3135             return new;
3136           else if (GET_CODE (new) == MEM
3137                    && x_size <= new_size
3138 #ifdef LOAD_EXTEND_OP
3139                    /* On these machines we will be reloading what is
3140                       inside the SUBREG if it originally was a pseudo and
3141                       the inner and outer modes are both a word or
3142                       smaller.  So leave the SUBREG then.  */
3143                    && ! (GET_CODE (SUBREG_REG (x)) == REG
3144                          && x_size <= UNITS_PER_WORD
3145                          && new_size <= UNITS_PER_WORD
3146                          && x_size > new_size
3147                          && INTEGRAL_MODE_P (GET_MODE (new))
3148                          && LOAD_EXTEND_OP (GET_MODE (new)) != NIL)
3149 #endif
3150                    )
3151             {
3152               int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
3153               enum machine_mode mode = GET_MODE (x);
3154
3155               if (BYTES_BIG_ENDIAN)
3156                 offset += (MIN (UNITS_PER_WORD,
3157                                 GET_MODE_SIZE (GET_MODE (new)))
3158                            - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
3159
3160               PUT_MODE (new, mode);
3161               XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
3162               return new;
3163             }
3164           else
3165             return gen_rtx (SUBREG, GET_MODE (x), new, SUBREG_WORD (x));
3166         }
3167
3168       return x;
3169
3170     case USE:
3171       /* If using a register that is the source of an eliminate we still
3172          think can be performed, note it cannot be performed since we don't
3173          know how this register is used.  */
3174       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3175         if (ep->from_rtx == XEXP (x, 0))
3176           ep->can_eliminate = 0;
3177
3178       new = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
3179       if (new != XEXP (x, 0))
3180         return gen_rtx (code, GET_MODE (x), new);
3181       return x;
3182
3183     case CLOBBER:
3184       /* If clobbering a register that is the replacement register for an
3185          elimination we still think can be performed, note that it cannot
3186          be performed.  Otherwise, we need not be concerned about it.  */
3187       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3188         if (ep->to_rtx == XEXP (x, 0))
3189           ep->can_eliminate = 0;
3190
3191       new = eliminate_regs (XEXP (x, 0), mem_mode, insn, 0);
3192       if (new != XEXP (x, 0))
3193         return gen_rtx (code, GET_MODE (x), new);
3194       return x;
3195
3196     case ASM_OPERANDS:
3197       {
3198         rtx *temp_vec;
3199         /* Properly handle sharing input and constraint vectors.  */
3200         if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
3201           {
3202             /* When we come to a new vector not seen before,
3203                scan all its elements; keep the old vector if none
3204                of them changes; otherwise, make a copy.  */
3205             old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
3206             temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
3207             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3208               temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
3209                                             mem_mode, insn, 0);
3210
3211             for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
3212               if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
3213                 break;
3214
3215             if (i == ASM_OPERANDS_INPUT_LENGTH (x))
3216               new_asm_operands_vec = old_asm_operands_vec;
3217             else
3218               new_asm_operands_vec
3219                 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
3220           }
3221
3222         /* If we had to copy the vector, copy the entire ASM_OPERANDS.  */
3223         if (new_asm_operands_vec == old_asm_operands_vec)
3224           return x;
3225
3226         new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
3227                        ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
3228                        ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
3229                        ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
3230                        ASM_OPERANDS_SOURCE_FILE (x),
3231                        ASM_OPERANDS_SOURCE_LINE (x));
3232         new->volatil = x->volatil;
3233         return new;
3234       }
3235
3236     case SET:
3237       /* Check for setting a register that we know about.  */
3238       if (GET_CODE (SET_DEST (x)) == REG)
3239         {
3240           /* See if this is setting the replacement register for an
3241              elimination.
3242
3243              If DEST is the hard frame pointer, we do nothing because we
3244              assume that all assignments to the frame pointer are for
3245              non-local gotos and are being done at a time when they are valid
3246              and do not disturb anything else.  Some machines want to
3247              eliminate a fake argument pointer (or even a fake frame pointer)
3248              with either the real frame or the stack pointer.  Assignments to
3249              the hard frame pointer must not prevent this elimination.  */
3250
3251           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3252                ep++)
3253             if (ep->to_rtx == SET_DEST (x)
3254                 && SET_DEST (x) != hard_frame_pointer_rtx)
3255               {
3256                 /* If it is being incremented, adjust the offset.  Otherwise,
3257                    this elimination can't be done.  */
3258                 rtx src = SET_SRC (x);
3259
3260                 if (GET_CODE (src) == PLUS
3261                     && XEXP (src, 0) == SET_DEST (x)
3262                     && GET_CODE (XEXP (src, 1)) == CONST_INT)
3263                   ep->offset -= INTVAL (XEXP (src, 1));
3264                 else
3265                   ep->can_eliminate = 0;
3266               }
3267
3268           /* Now check to see we are assigning to a register that can be
3269              eliminated.  If so, it must be as part of a PARALLEL, since we
3270              will not have been called if this is a single SET.  So indicate
3271              that we can no longer eliminate this reg.  */
3272           for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3273                ep++)
3274             if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
3275               ep->can_eliminate = 0;
3276         }
3277
3278       /* Now avoid the loop below in this common case.  */
3279       {
3280         rtx new0 = eliminate_regs (SET_DEST (x), 0, insn, 1);
3281         rtx new1 = eliminate_regs (SET_SRC (x), 0, insn, 0);
3282
3283         /* If SET_DEST changed from a REG to a MEM and INSN is an insn,
3284            write a CLOBBER insn.  */
3285         if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
3286             && insn != 0 && GET_CODE (insn) != EXPR_LIST
3287             && GET_CODE (insn) != INSN_LIST)
3288           emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
3289
3290         /* If SET_DEST was a partial-word subreg, NEW0 may have been widened
3291            to spill the entire register (see SUBREG case above).  If the 
3292            widths of SET_DEST and NEW0 no longer match, adjust NEW1.  */
3293         if (GET_MODE (SET_DEST (x)) != GET_MODE (new0))
3294           new1 = gen_rtx (SUBREG, GET_MODE (new0), new1, 0);
3295
3296         if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
3297           return gen_rtx (SET, VOIDmode, new0, new1);
3298       }
3299
3300       return x;
3301
3302     case MEM:
3303       /* This is only for the benefit of the debugging backends, which call
3304          eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
3305          removed after CSE.  */
3306       if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
3307         return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn, 0);
3308
3309       /* Our only special processing is to pass the mode of the MEM to our
3310          recursive call and copy the flags.  While we are here, handle this
3311          case more efficiently.  */
3312       new = eliminate_regs (XEXP (x, 0), GET_MODE (x), insn, 0);
3313       if (new != XEXP (x, 0))
3314         {
3315           new = gen_rtx (MEM, GET_MODE (x), new);
3316           new->volatil = x->volatil;
3317           new->unchanging = x->unchanging;
3318           new->in_struct = x->in_struct;
3319           return new;
3320         }
3321       else
3322         return x;
3323       
3324     default:
3325       break;
3326     }
3327
3328   /* Process each of our operands recursively.  If any have changed, make a
3329      copy of the rtx.  */
3330   fmt = GET_RTX_FORMAT (code);
3331   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3332     {
3333       if (*fmt == 'e')
3334         {
3335           new = eliminate_regs (XEXP (x, i), mem_mode, insn, 0);
3336           if (new != XEXP (x, i) && ! copied)
3337             {
3338               rtx new_x = rtx_alloc (code);
3339               bcopy ((char *) x, (char *) new_x,
3340                      (sizeof (*new_x) - sizeof (new_x->fld)
3341                       + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
3342               x = new_x;
3343               copied = 1;
3344             }
3345           XEXP (x, i) = new;
3346         }
3347       else if (*fmt == 'E')
3348         {
3349           int copied_vec = 0;
3350           for (j = 0; j < XVECLEN (x, i); j++)
3351             {
3352               new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn, 0);
3353               if (new != XVECEXP (x, i, j) && ! copied_vec)
3354                 {
3355                   rtvec new_v = gen_rtvec_vv (XVECLEN (x, i),
3356                                               XVEC (x, i)->elem);
3357                   if (! copied)
3358                     {
3359                       rtx new_x = rtx_alloc (code);
3360                       bcopy ((char *) x, (char *) new_x,
3361                              (sizeof (*new_x) - sizeof (new_x->fld)
3362                               + (sizeof (new_x->fld[0])
3363                                  * GET_RTX_LENGTH (code))));
3364                       x = new_x;
3365                       copied = 1;
3366                     }
3367                   XVEC (x, i) = new_v;
3368                   copied_vec = 1;
3369                 }
3370               XVECEXP (x, i, j) = new;
3371             }
3372         }
3373     }
3374
3375   return x;
3376 }
3377 \f
3378 /* Scan INSN and eliminate all eliminable registers in it.
3379
3380    If REPLACE is nonzero, do the replacement destructively.  Also
3381    delete the insn as dead it if it is setting an eliminable register.
3382
3383    If REPLACE is zero, do all our allocations in reload_obstack.
3384
3385    If no eliminations were done and this insn doesn't require any elimination
3386    processing (these are not identical conditions: it might be updating sp,
3387    but not referencing fp; this needs to be seen during reload_as_needed so
3388    that the offset between fp and sp can be taken into consideration), zero
3389    is returned.  Otherwise, 1 is returned.  */
3390
3391 static int
3392 eliminate_regs_in_insn (insn, replace)
3393      rtx insn;
3394      int replace;
3395 {
3396   rtx old_body = PATTERN (insn);
3397   rtx old_set = single_set (insn);
3398   rtx new_body;
3399   int val = 0;
3400   struct elim_table *ep;
3401
3402   if (! replace)
3403     push_obstacks (&reload_obstack, &reload_obstack);
3404
3405   if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
3406       && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3407     {
3408       /* Check for setting an eliminable register.  */
3409       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3410         if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3411           {
3412 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3413             /* If this is setting the frame pointer register to the
3414                hardware frame pointer register and this is an elimination
3415                that will be done (tested above), this insn is really
3416                adjusting the frame pointer downward to compensate for
3417                the adjustment done before a nonlocal goto.  */
3418             if (ep->from == FRAME_POINTER_REGNUM
3419                 && ep->to == HARD_FRAME_POINTER_REGNUM)
3420               {
3421                 rtx src = SET_SRC (old_set);
3422                 int offset, ok = 0;
3423                 rtx prev_insn, prev_set;
3424
3425                 if (src == ep->to_rtx)
3426                   offset = 0, ok = 1;
3427                 else if (GET_CODE (src) == PLUS
3428                          && GET_CODE (XEXP (src, 0)) == CONST_INT)
3429                   offset = INTVAL (XEXP (src, 0)), ok = 1;
3430                 else if ((prev_insn = prev_nonnote_insn (insn)) != 0
3431                          && (prev_set = single_set (prev_insn)) != 0
3432                          && rtx_equal_p (SET_DEST (prev_set), src))
3433                   {
3434                     src = SET_SRC (prev_set);
3435                     if (src == ep->to_rtx)
3436                       offset = 0, ok = 1;
3437                     else if (GET_CODE (src) == PLUS
3438                              && GET_CODE (XEXP (src, 0)) == CONST_INT
3439                              && XEXP (src, 1) == ep->to_rtx)
3440                       offset = INTVAL (XEXP (src, 0)), ok = 1;
3441                     else if (GET_CODE (src) == PLUS
3442                              && GET_CODE (XEXP (src, 1)) == CONST_INT
3443                              && XEXP (src, 0) == ep->to_rtx)
3444                       offset = INTVAL (XEXP (src, 1)), ok = 1;
3445                   }
3446
3447                 if (ok)
3448                   {
3449                     if (replace)
3450                       {
3451                         rtx src
3452                           = plus_constant (ep->to_rtx, offset - ep->offset);
3453
3454                         /* First see if this insn remains valid when we
3455                            make the change.  If not, keep the INSN_CODE
3456                            the same and let reload fit it up.  */
3457                         validate_change (insn, &SET_SRC (old_set), src, 1);
3458                         validate_change (insn, &SET_DEST (old_set),
3459                                          ep->to_rtx, 1);
3460                         if (! apply_change_group ())
3461                           {
3462                             SET_SRC (old_set) = src;
3463                             SET_DEST (old_set) = ep->to_rtx;
3464                           }
3465                       }
3466
3467                     val = 1;
3468                     goto done;
3469                   }
3470               }
3471 #endif
3472
3473             /* In this case this insn isn't serving a useful purpose.  We
3474                will delete it in reload_as_needed once we know that this
3475                elimination is, in fact, being done.
3476
3477                If REPLACE isn't set, we can't delete this insn, but needn't
3478                process it since it won't be used unless something changes.  */
3479             if (replace)
3480               delete_dead_insn (insn);
3481             val = 1;
3482             goto done;
3483           }
3484
3485       /* Check for (set (reg) (plus (reg from) (offset))) where the offset
3486          in the insn is the negative of the offset in FROM.  Substitute
3487          (set (reg) (reg to)) for the insn and change its code.
3488
3489          We have to do this here, rather than in eliminate_regs, do that we can
3490          change the insn code.  */
3491
3492       if (GET_CODE (SET_SRC (old_set)) == PLUS
3493           && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3494           && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT)
3495         for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3496              ep++)
3497           if (ep->from_rtx == XEXP (SET_SRC (old_set), 0)
3498               && ep->can_eliminate)
3499             {
3500               /* We must stop at the first elimination that will be used.
3501                  If this one would replace the PLUS with a REG, do it
3502                  now.  Otherwise, quit the loop and let eliminate_regs
3503                  do its normal replacement.  */
3504               if (ep->offset == - INTVAL (XEXP (SET_SRC (old_set), 1)))
3505                 {
3506                   /* We assume here that we don't need a PARALLEL of
3507                      any CLOBBERs for this assignment.  There's not
3508                      much we can do if we do need it.  */
3509                   PATTERN (insn) = gen_rtx (SET, VOIDmode,
3510                                             SET_DEST (old_set), ep->to_rtx);
3511                   INSN_CODE (insn) = -1;
3512                   val = 1;
3513                   goto done;
3514                 }
3515
3516               break;
3517             }
3518     }
3519
3520   old_asm_operands_vec = 0;
3521
3522   /* Replace the body of this insn with a substituted form.  If we changed
3523      something, return non-zero.  
3524
3525      If we are replacing a body that was a (set X (plus Y Z)), try to
3526      re-recognize the insn.  We do this in case we had a simple addition
3527      but now can do this as a load-address.  This saves an insn in this
3528      common case.  */
3529
3530   new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX, 0);
3531   if (new_body != old_body)
3532     {
3533       /* If we aren't replacing things permanently and we changed something,
3534          make another copy to ensure that all the RTL is new.  Otherwise
3535          things can go wrong if find_reload swaps commutative operands
3536          and one is inside RTL that has been copied while the other is not.  */
3537
3538       /* Don't copy an asm_operands because (1) there's no need and (2)
3539          copy_rtx can't do it properly when there are multiple outputs.  */
3540       if (! replace && asm_noperands (old_body) < 0)
3541         new_body = copy_rtx (new_body);
3542
3543       /* If we had a move insn but now we don't, rerecognize it.  This will
3544          cause spurious re-recognition if the old move had a PARALLEL since
3545          the new one still will, but we can't call single_set without
3546          having put NEW_BODY into the insn and the re-recognition won't
3547          hurt in this rare case.  */
3548       if (old_set != 0
3549           && ((GET_CODE (SET_SRC (old_set)) == REG
3550                && (GET_CODE (new_body) != SET
3551                    || GET_CODE (SET_SRC (new_body)) != REG))
3552               /* If this was a load from or store to memory, compare
3553                  the MEM in recog_operand to the one in the insn.  If they
3554                  are not equal, then rerecognize the insn.  */
3555               || (old_set != 0
3556                   && ((GET_CODE (SET_SRC (old_set)) == MEM
3557                        && SET_SRC (old_set) != recog_operand[1])
3558                       || (GET_CODE (SET_DEST (old_set)) == MEM
3559                           && SET_DEST (old_set) != recog_operand[0])))
3560               /* If this was an add insn before, rerecognize.  */
3561               || GET_CODE (SET_SRC (old_set)) == PLUS))
3562         {
3563           if (! validate_change (insn, &PATTERN (insn), new_body, 0))
3564             /* If recognition fails, store the new body anyway.
3565                It's normal to have recognition failures here
3566                due to bizarre memory addresses; reloading will fix them.  */
3567             PATTERN (insn) = new_body;
3568         }
3569       else
3570         PATTERN (insn) = new_body;
3571
3572       val = 1;
3573     }
3574
3575   /* Loop through all elimination pairs.  See if any have changed and
3576      recalculate the number not at initial offset.
3577
3578      Compute the maximum offset (minimum offset if the stack does not
3579      grow downward) for each elimination pair.
3580
3581      We also detect a cases where register elimination cannot be done,
3582      namely, if a register would be both changed and referenced outside a MEM
3583      in the resulting insn since such an insn is often undefined and, even if
3584      not, we cannot know what meaning will be given to it.  Note that it is
3585      valid to have a register used in an address in an insn that changes it
3586      (presumably with a pre- or post-increment or decrement).
3587
3588      If anything changes, return nonzero.  */
3589
3590   num_not_at_initial_offset = 0;
3591   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3592     {
3593       if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3594         ep->can_eliminate = 0;
3595
3596       ep->ref_outside_mem = 0;
3597
3598       if (ep->previous_offset != ep->offset)
3599         val = 1;
3600
3601       ep->previous_offset = ep->offset;
3602       if (ep->can_eliminate && ep->offset != ep->initial_offset)
3603         num_not_at_initial_offset++;
3604
3605 #ifdef STACK_GROWS_DOWNWARD
3606       ep->max_offset = MAX (ep->max_offset, ep->offset);
3607 #else
3608       ep->max_offset = MIN (ep->max_offset, ep->offset);
3609 #endif
3610     }
3611
3612  done:
3613   /* If we changed something, perform elimination in REG_NOTES.  This is
3614      needed even when REPLACE is zero because a REG_DEAD note might refer
3615      to a register that we eliminate and could cause a different number
3616      of spill registers to be needed in the final reload pass than in
3617      the pre-passes.  */
3618   if (val && REG_NOTES (insn) != 0)
3619     REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn), 0);
3620
3621   if (! replace)
3622     pop_obstacks ();
3623
3624   return val;
3625 }
3626
3627 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3628    replacement we currently believe is valid, mark it as not eliminable if X
3629    modifies DEST in any way other than by adding a constant integer to it.
3630
3631    If DEST is the frame pointer, we do nothing because we assume that
3632    all assignments to the hard frame pointer are nonlocal gotos and are being
3633    done at a time when they are valid and do not disturb anything else.
3634    Some machines want to eliminate a fake argument pointer with either the
3635    frame or stack pointer.  Assignments to the hard frame pointer must not
3636    prevent this elimination.
3637
3638    Called via note_stores from reload before starting its passes to scan
3639    the insns of the function.  */
3640
3641 static void
3642 mark_not_eliminable (dest, x)
3643      rtx dest;
3644      rtx x;
3645 {
3646   register int i;
3647
3648   /* A SUBREG of a hard register here is just changing its mode.  We should
3649      not see a SUBREG of an eliminable hard register, but check just in
3650      case.  */
3651   if (GET_CODE (dest) == SUBREG)
3652     dest = SUBREG_REG (dest);
3653
3654   if (dest == hard_frame_pointer_rtx)
3655     return;
3656
3657   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3658     if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3659         && (GET_CODE (x) != SET
3660             || GET_CODE (SET_SRC (x)) != PLUS
3661             || XEXP (SET_SRC (x), 0) != dest
3662             || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3663       {
3664         reg_eliminate[i].can_eliminate_previous
3665           = reg_eliminate[i].can_eliminate = 0;
3666         num_eliminable--;
3667       }
3668 }
3669 \f
3670 /* Kick all pseudos out of hard register REGNO.
3671    If GLOBAL is nonzero, try to find someplace else to put them.
3672    If DUMPFILE is nonzero, log actions taken on that file.
3673
3674    If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3675    because we found we can't eliminate some register.  In the case, no pseudos
3676    are allowed to be in the register, even if they are only in a block that
3677    doesn't require spill registers, unlike the case when we are spilling this
3678    hard reg to produce another spill register.
3679
3680    Return nonzero if any pseudos needed to be kicked out.  */
3681
3682 static int
3683 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3684      register int regno;
3685      int global;
3686      FILE *dumpfile;
3687      int cant_eliminate;
3688 {
3689   enum reg_class class = REGNO_REG_CLASS (regno);
3690   int something_changed = 0;
3691   register int i;
3692
3693   SET_HARD_REG_BIT (forbidden_regs, regno);
3694
3695   if (cant_eliminate)
3696     regs_ever_live[regno] = 1;
3697
3698   /* Spill every pseudo reg that was allocated to this reg
3699      or to something that overlaps this reg.  */
3700
3701   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3702     if (reg_renumber[i] >= 0
3703         && reg_renumber[i] <= regno
3704         && (reg_renumber[i]
3705             + HARD_REGNO_NREGS (reg_renumber[i],
3706                                 PSEUDO_REGNO_MODE (i))
3707             > regno))
3708       {
3709         /* If this register belongs solely to a basic block which needed no
3710            spilling of any class that this register is contained in,
3711            leave it be, unless we are spilling this register because
3712            it was a hard register that can't be eliminated.   */
3713
3714         if (! cant_eliminate
3715             && basic_block_needs[0]
3716             && REG_BASIC_BLOCK (i) >= 0
3717             && basic_block_needs[(int) class][REG_BASIC_BLOCK (i)] == 0)
3718           {
3719             enum reg_class *p;
3720
3721             for (p = reg_class_superclasses[(int) class];
3722                  *p != LIM_REG_CLASSES; p++)
3723               if (basic_block_needs[(int) *p][REG_BASIC_BLOCK (i)] > 0)
3724                 break;
3725
3726             if (*p == LIM_REG_CLASSES)
3727               continue;
3728           }
3729
3730         /* Mark it as no longer having a hard register home.  */
3731         reg_renumber[i] = -1;
3732         /* We will need to scan everything again.  */
3733         something_changed = 1;
3734         if (global)
3735           retry_global_alloc (i, forbidden_regs);
3736
3737         alter_reg (i, regno);
3738         if (dumpfile)
3739           {
3740             if (reg_renumber[i] == -1)
3741               fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3742             else
3743               fprintf (dumpfile, " Register %d now in %d.\n\n",
3744                        i, reg_renumber[i]);
3745           }
3746       }
3747   for (i = 0; i < scratch_list_length; i++)
3748     {
3749       if (scratch_list[i] && REGNO (scratch_list[i]) == regno)
3750         {
3751           if (! cant_eliminate && basic_block_needs[0]
3752               && ! basic_block_needs[(int) class][scratch_block[i]])
3753             {
3754               enum reg_class *p;
3755
3756               for (p = reg_class_superclasses[(int) class];
3757                    *p != LIM_REG_CLASSES; p++)
3758                 if (basic_block_needs[(int) *p][scratch_block[i]] > 0)
3759                   break;
3760
3761               if (*p == LIM_REG_CLASSES)
3762                 continue;
3763             }
3764           PUT_CODE (scratch_list[i], SCRATCH);
3765           scratch_list[i] = 0;
3766           something_changed = 1;
3767           continue;
3768         }
3769     }
3770
3771   return something_changed;
3772 }
3773 \f
3774 /* Find all paradoxical subregs within X and update reg_max_ref_width. 
3775    Also mark any hard registers used to store user variables as
3776    forbidden from being used for spill registers.  */
3777
3778 static void
3779 scan_paradoxical_subregs (x)
3780      register rtx x;
3781 {
3782   register int i;
3783   register char *fmt;
3784   register enum rtx_code code = GET_CODE (x);
3785
3786   switch (code)
3787     {
3788     case REG:
3789       if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3790           && REG_USERVAR_P (x))
3791         SET_HARD_REG_BIT (forbidden_regs, REGNO (x));
3792       return;
3793
3794     case CONST_INT:
3795     case CONST:
3796     case SYMBOL_REF:
3797     case LABEL_REF:
3798     case CONST_DOUBLE:
3799     case CC0:
3800     case PC:
3801     case USE:
3802     case CLOBBER:
3803       return;
3804
3805     case SUBREG:
3806       if (GET_CODE (SUBREG_REG (x)) == REG
3807           && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3808         reg_max_ref_width[REGNO (SUBREG_REG (x))]
3809           = GET_MODE_SIZE (GET_MODE (x));
3810       return;
3811       
3812     default:
3813       break;
3814     }
3815
3816   fmt = GET_RTX_FORMAT (code);
3817   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3818     {
3819       if (fmt[i] == 'e')
3820         scan_paradoxical_subregs (XEXP (x, i));
3821       else if (fmt[i] == 'E')
3822         {
3823           register int j;
3824           for (j = XVECLEN (x, i) - 1; j >=0; j--)
3825             scan_paradoxical_subregs (XVECEXP (x, i, j));
3826         }
3827     }
3828 }
3829 \f
3830 static int
3831 hard_reg_use_compare (p1p, p2p)
3832   const GENERIC_PTR p1p;
3833   const GENERIC_PTR p2p;
3834 {
3835   struct hard_reg_n_uses *p1 = (struct hard_reg_n_uses *)p1p,
3836                          *p2 = (struct hard_reg_n_uses *)p2p;
3837   int tem = p1->uses - p2->uses;
3838   if (tem != 0) return tem;
3839   /* If regs are equally good, sort by regno,
3840      so that the results of qsort leave nothing to chance.  */
3841   return p1->regno - p2->regno;
3842 }
3843
3844 /* Choose the order to consider regs for use as reload registers
3845    based on how much trouble would be caused by spilling one.
3846    Store them in order of decreasing preference in potential_reload_regs.  */
3847
3848 static void
3849 order_regs_for_reload (global)
3850      int global;
3851 {
3852   register int i;
3853   register int o = 0;
3854   int large = 0;
3855
3856   struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3857
3858   CLEAR_HARD_REG_SET (bad_spill_regs);
3859
3860   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3861     potential_reload_regs[i] = -1;
3862
3863   /* Count number of uses of each hard reg by pseudo regs allocated to it
3864      and then order them by decreasing use.  */
3865
3866   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3867     {
3868       hard_reg_n_uses[i].uses = 0;
3869       hard_reg_n_uses[i].regno = i;
3870     }
3871
3872   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3873     {
3874       int regno = reg_renumber[i];
3875       if (regno >= 0)
3876         {
3877           int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3878           while (regno < lim)
3879             {
3880               /* If allocated by local-alloc, show more uses since
3881                  we're not going to be able to reallocate it, but
3882                  we might if allocated by global alloc.  */
3883               if (global && reg_allocno[i] < 0)
3884                 hard_reg_n_uses[regno].uses += (REG_N_REFS (i) + 1) / 2;
3885
3886               hard_reg_n_uses[regno++].uses += REG_N_REFS (i);
3887             }
3888         }
3889       large += REG_N_REFS (i);
3890     }
3891
3892   /* Now fixed registers (which cannot safely be used for reloading)
3893      get a very high use count so they will be considered least desirable.
3894      Registers used explicitly in the rtl code are almost as bad.  */
3895
3896   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3897     {
3898       if (fixed_regs[i])
3899         {
3900           hard_reg_n_uses[i].uses += 2 * large + 2;
3901           SET_HARD_REG_BIT (bad_spill_regs, i);
3902         }
3903       else if (regs_explicitly_used[i])
3904         {
3905           hard_reg_n_uses[i].uses += large + 1;
3906           if (! SMALL_REGISTER_CLASSES)
3907             /* ??? We are doing this here because of the potential
3908              that bad code may be generated if a register explicitly
3909              used in an insn was used as a spill register for that
3910              insn.  But not using these are spill registers may lose
3911              on some machine.  We'll have to see how this works out.  */
3912             SET_HARD_REG_BIT (bad_spill_regs, i);
3913         }
3914     }
3915   hard_reg_n_uses[HARD_FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3916   SET_HARD_REG_BIT (bad_spill_regs, HARD_FRAME_POINTER_REGNUM);
3917
3918 #ifdef ELIMINABLE_REGS
3919   /* If registers other than the frame pointer are eliminable, mark them as
3920      poor choices.  */
3921   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3922     {
3923       hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3924       SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3925     }
3926 #endif
3927
3928   /* Prefer registers not so far used, for use in temporary loading.
3929      Among them, if REG_ALLOC_ORDER is defined, use that order.
3930      Otherwise, prefer registers not preserved by calls.  */
3931
3932 #ifdef REG_ALLOC_ORDER
3933   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3934     {
3935       int regno = reg_alloc_order[i];
3936
3937       if (hard_reg_n_uses[regno].uses == 0)
3938         potential_reload_regs[o++] = regno;
3939     }
3940 #else
3941   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3942     {
3943       if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3944         potential_reload_regs[o++] = i;
3945     }
3946   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3947     {
3948       if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3949         potential_reload_regs[o++] = i;
3950     }
3951 #endif
3952
3953   qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3954          sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3955
3956   /* Now add the regs that are already used,
3957      preferring those used less often.  The fixed and otherwise forbidden
3958      registers will be at the end of this list.  */
3959
3960   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3961     if (hard_reg_n_uses[i].uses != 0)
3962       potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3963 }
3964 \f
3965 /* Used in reload_as_needed to sort the spilled regs.  */
3966
3967 static int
3968 compare_spill_regs (r1p, r2p)
3969      const GENERIC_PTR r1p;
3970      const GENERIC_PTR r2p;
3971 {
3972   short r1 = *(short *)r1p, r2 = *(short *)r2p;
3973   return r1 - r2;
3974 }
3975
3976 /* Reload pseudo-registers into hard regs around each insn as needed.
3977    Additional register load insns are output before the insn that needs it
3978    and perhaps store insns after insns that modify the reloaded pseudo reg.
3979
3980    reg_last_reload_reg and reg_reloaded_contents keep track of
3981    which registers are already available in reload registers.
3982    We update these for the reloads that we perform,
3983    as the insns are scanned.  */
3984
3985 static void
3986 reload_as_needed (first, live_known)
3987      rtx first;
3988      int live_known;
3989 {
3990   register rtx insn;
3991   register int i;
3992   int this_block = 0;
3993   rtx x;
3994   rtx after_call = 0;
3995
3996   bzero ((char *) spill_reg_rtx, sizeof spill_reg_rtx);
3997   bzero ((char *) spill_reg_store, sizeof spill_reg_store);
3998   reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3999   bzero ((char *) reg_last_reload_reg, max_regno * sizeof (rtx));
4000   reg_has_output_reload = (char *) alloca (max_regno);
4001   for (i = 0; i < n_spills; i++)
4002     {
4003       reg_reloaded_contents[i] = -1;
4004       reg_reloaded_insn[i] = 0;
4005     }
4006
4007   /* Reset all offsets on eliminable registers to their initial values.  */
4008 #ifdef ELIMINABLE_REGS
4009   for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
4010     {
4011       INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
4012                                   reg_eliminate[i].initial_offset);
4013       reg_eliminate[i].previous_offset
4014         = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
4015     }
4016 #else
4017   INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
4018   reg_eliminate[0].previous_offset
4019     = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
4020 #endif
4021
4022   num_not_at_initial_offset = 0;
4023
4024   /* Order the spilled regs, so that allocate_reload_regs can guarantee to
4025      pack registers with group needs.  */
4026   if (n_spills > 1)
4027     {
4028       qsort (spill_regs, n_spills, sizeof (short), compare_spill_regs);
4029       for (i = 0; i < n_spills; i++)
4030         spill_reg_order[spill_regs[i]] = i;
4031     }
4032
4033   for (insn = first; insn;)
4034     {
4035       register rtx next = NEXT_INSN (insn);
4036
4037       /* Notice when we move to a new basic block.  */
4038       if (live_known && this_block + 1 < n_basic_blocks
4039           && insn == basic_block_head[this_block+1])
4040         ++this_block;
4041
4042       /* If we pass a label, copy the offsets from the label information
4043          into the current offsets of each elimination.  */
4044       if (GET_CODE (insn) == CODE_LABEL)
4045         {
4046           num_not_at_initial_offset = 0;
4047           for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
4048             {
4049               reg_eliminate[i].offset = reg_eliminate[i].previous_offset
4050                 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
4051               if (reg_eliminate[i].can_eliminate
4052                   && (reg_eliminate[i].offset
4053                       != reg_eliminate[i].initial_offset))
4054                 num_not_at_initial_offset++;
4055             }
4056         }
4057
4058       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4059         {
4060           rtx avoid_return_reg = 0;
4061           rtx oldpat = PATTERN (insn);
4062
4063           /* Set avoid_return_reg if this is an insn
4064              that might use the value of a function call.  */
4065           if (SMALL_REGISTER_CLASSES && GET_CODE (insn) == CALL_INSN)
4066             {
4067               if (GET_CODE (PATTERN (insn)) == SET)
4068                 after_call = SET_DEST (PATTERN (insn));
4069               else if (GET_CODE (PATTERN (insn)) == PARALLEL
4070                        && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
4071                 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
4072               else
4073                 after_call = 0;
4074             }
4075           else if (SMALL_REGISTER_CLASSES && after_call != 0
4076                    && !(GET_CODE (PATTERN (insn)) == SET
4077                         && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
4078             {
4079               if (reg_referenced_p (after_call, PATTERN (insn)))
4080                 avoid_return_reg = after_call;
4081               after_call = 0;
4082             }
4083
4084           /* If this is a USE and CLOBBER of a MEM, ensure that any
4085              references to eliminable registers have been removed.  */
4086
4087           if ((GET_CODE (PATTERN (insn)) == USE
4088                || GET_CODE (PATTERN (insn)) == CLOBBER)
4089               && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
4090             XEXP (XEXP (PATTERN (insn), 0), 0)
4091               = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4092                                 GET_MODE (XEXP (PATTERN (insn), 0)),
4093                                 NULL_RTX, 0);
4094
4095           /* If we need to do register elimination processing, do so.
4096              This might delete the insn, in which case we are done.  */
4097           if (num_eliminable && GET_MODE (insn) == QImode)
4098             {
4099               eliminate_regs_in_insn (insn, 1);
4100               if (GET_CODE (insn) == NOTE)
4101                 {
4102                   insn = next;
4103                   continue;
4104                 }
4105             }
4106
4107           if (GET_MODE (insn) == VOIDmode)
4108             n_reloads = 0;
4109           /* First find the pseudo regs that must be reloaded for this insn.
4110              This info is returned in the tables reload_... (see reload.h).
4111              Also modify the body of INSN by substituting RELOAD
4112              rtx's for those pseudo regs.  */
4113           else
4114             {
4115               bzero (reg_has_output_reload, max_regno);
4116               CLEAR_HARD_REG_SET (reg_is_output_reload);
4117
4118               find_reloads (insn, 1, spill_indirect_levels, live_known,
4119                             spill_reg_order);
4120             }
4121
4122           if (n_reloads > 0)
4123             {
4124               rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
4125               rtx p;
4126               int class;
4127
4128               /* If this block has not had spilling done for a
4129                  particular clas and we have any non-optionals that need a
4130                  spill reg in that class, abort.  */
4131
4132               for (class = 0; class < N_REG_CLASSES; class++)
4133                 if (basic_block_needs[class] != 0
4134                     && basic_block_needs[class][this_block] == 0)
4135                   for (i = 0; i < n_reloads; i++)
4136                     if (class == (int) reload_reg_class[i]
4137                         && reload_reg_rtx[i] == 0
4138                         && ! reload_optional[i]
4139                         && (reload_in[i] != 0 || reload_out[i] != 0
4140                             || reload_secondary_p[i] != 0))
4141                       fatal_insn ("Non-optional registers need a spill register", insn);
4142
4143               /* Now compute which reload regs to reload them into.  Perhaps
4144                  reusing reload regs from previous insns, or else output
4145                  load insns to reload them.  Maybe output store insns too.
4146                  Record the choices of reload reg in reload_reg_rtx.  */
4147               choose_reload_regs (insn, avoid_return_reg);
4148
4149               /* Merge any reloads that we didn't combine for fear of 
4150                  increasing the number of spill registers needed but now
4151                  discover can be safely merged.  */
4152               if (SMALL_REGISTER_CLASSES)
4153                 merge_assigned_reloads (insn);
4154
4155               /* Generate the insns to reload operands into or out of
4156                  their reload regs.  */
4157               emit_reload_insns (insn);
4158
4159               /* Substitute the chosen reload regs from reload_reg_rtx
4160                  into the insn's body (or perhaps into the bodies of other
4161                  load and store insn that we just made for reloading
4162                  and that we moved the structure into).  */
4163               subst_reloads ();
4164
4165               /* If this was an ASM, make sure that all the reload insns
4166                  we have generated are valid.  If not, give an error
4167                  and delete them.  */
4168
4169               if (asm_noperands (PATTERN (insn)) >= 0)
4170                 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4171                   if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
4172                       && (recog_memoized (p) < 0
4173                           || (insn_extract (p),
4174                               ! constrain_operands (INSN_CODE (p), 1))))
4175                     {
4176                       error_for_asm (insn,
4177                                      "`asm' operand requires impossible reload");
4178                       PUT_CODE (p, NOTE);
4179                       NOTE_SOURCE_FILE (p) = 0;
4180                       NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
4181                     }
4182             }
4183           /* Any previously reloaded spilled pseudo reg, stored in this insn,
4184              is no longer validly lying around to save a future reload.
4185              Note that this does not detect pseudos that were reloaded
4186              for this insn in order to be stored in
4187              (obeying register constraints).  That is correct; such reload
4188              registers ARE still valid.  */
4189           note_stores (oldpat, forget_old_reloads_1);
4190
4191           /* There may have been CLOBBER insns placed after INSN.  So scan
4192              between INSN and NEXT and use them to forget old reloads.  */
4193           for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
4194             if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
4195               note_stores (PATTERN (x), forget_old_reloads_1);
4196
4197 #ifdef AUTO_INC_DEC
4198           /* Likewise for regs altered by auto-increment in this insn.
4199              But note that the reg-notes are not changed by reloading:
4200              they still contain the pseudo-regs, not the spill regs.  */
4201           for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4202             if (REG_NOTE_KIND (x) == REG_INC)
4203               {
4204                 /* See if this pseudo reg was reloaded in this insn.
4205                    If so, its last-reload info is still valid
4206                    because it is based on this insn's reload.  */
4207                 for (i = 0; i < n_reloads; i++)
4208                   if (reload_out[i] == XEXP (x, 0))
4209                     break;
4210
4211                 if (i == n_reloads)
4212                   forget_old_reloads_1 (XEXP (x, 0), NULL_RTX);
4213               }
4214 #endif
4215         }
4216       /* A reload reg's contents are unknown after a label.  */
4217       if (GET_CODE (insn) == CODE_LABEL)
4218         for (i = 0; i < n_spills; i++)
4219           {
4220             reg_reloaded_contents[i] = -1;
4221             reg_reloaded_insn[i] = 0;
4222           }
4223
4224       /* Don't assume a reload reg is still good after a call insn
4225          if it is a call-used reg.  */
4226       else if (GET_CODE (insn) == CALL_INSN)
4227         for (i = 0; i < n_spills; i++)
4228           if (call_used_regs[spill_regs[i]])
4229             {
4230               reg_reloaded_contents[i] = -1;
4231               reg_reloaded_insn[i] = 0;
4232             }
4233
4234       /* In case registers overlap, allow certain insns to invalidate
4235          particular hard registers.  */
4236
4237 #ifdef INSN_CLOBBERS_REGNO_P
4238       for (i = 0 ; i < n_spills ; i++)
4239         if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
4240           {
4241             reg_reloaded_contents[i] = -1;
4242             reg_reloaded_insn[i] = 0;
4243           }
4244 #endif
4245
4246       insn = next;
4247
4248 #ifdef USE_C_ALLOCA
4249       alloca (0);
4250 #endif
4251     }
4252 }
4253
4254 /* Discard all record of any value reloaded from X,
4255    or reloaded in X from someplace else;
4256    unless X is an output reload reg of the current insn.
4257
4258    X may be a hard reg (the reload reg)
4259    or it may be a pseudo reg that was reloaded from.  */
4260
4261 static void
4262 forget_old_reloads_1 (x, ignored)
4263      rtx x;
4264      rtx ignored;
4265 {
4266   register int regno;
4267   int nr;
4268   int offset = 0;
4269
4270   /* note_stores does give us subregs of hard regs.  */
4271   while (GET_CODE (x) == SUBREG)
4272     {
4273       offset += SUBREG_WORD (x);
4274       x = SUBREG_REG (x);
4275     }
4276
4277   if (GET_CODE (x) != REG)
4278     return;
4279
4280   regno = REGNO (x) + offset;
4281
4282   if (regno >= FIRST_PSEUDO_REGISTER)
4283     nr = 1;
4284   else
4285     {
4286       int i;
4287       nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4288       /* Storing into a spilled-reg invalidates its contents.
4289          This can happen if a block-local pseudo is allocated to that reg
4290          and it wasn't spilled because this block's total need is 0.
4291          Then some insn might have an optional reload and use this reg.  */
4292       for (i = 0; i < nr; i++)
4293         if (spill_reg_order[regno + i] >= 0
4294             /* But don't do this if the reg actually serves as an output
4295                reload reg in the current instruction.  */
4296             && (n_reloads == 0
4297                 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
4298           {
4299             reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
4300             reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
4301           }
4302     }
4303
4304   /* Since value of X has changed,
4305      forget any value previously copied from it.  */
4306
4307   while (nr-- > 0)
4308     /* But don't forget a copy if this is the output reload
4309        that establishes the copy's validity.  */
4310     if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4311       reg_last_reload_reg[regno + nr] = 0;
4312 }
4313 \f
4314 /* For each reload, the mode of the reload register.  */
4315 static enum machine_mode reload_mode[MAX_RELOADS];
4316
4317 /* For each reload, the largest number of registers it will require.  */
4318 static int reload_nregs[MAX_RELOADS];
4319
4320 /* Comparison function for qsort to decide which of two reloads
4321    should be handled first.  *P1 and *P2 are the reload numbers.  */
4322
4323 static int
4324 reload_reg_class_lower (r1p, r2p)
4325      const GENERIC_PTR r1p;
4326      const GENERIC_PTR r2p;
4327 {
4328   register int r1 = *(short *)r1p, r2 = *(short *)r2p;
4329   register int t;
4330
4331   /* Consider required reloads before optional ones.  */
4332   t = reload_optional[r1] - reload_optional[r2];
4333   if (t != 0)
4334     return t;
4335
4336   /* Count all solitary classes before non-solitary ones.  */
4337   t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
4338        - (reg_class_size[(int) reload_reg_class[r1]] == 1));
4339   if (t != 0)
4340     return t;
4341
4342   /* Aside from solitaires, consider all multi-reg groups first.  */
4343   t = reload_nregs[r2] - reload_nregs[r1];
4344   if (t != 0)
4345     return t;
4346
4347   /* Consider reloads in order of increasing reg-class number.  */
4348   t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
4349   if (t != 0)
4350     return t;
4351
4352   /* If reloads are equally urgent, sort by reload number,
4353      so that the results of qsort leave nothing to chance.  */
4354   return r1 - r2;
4355 }
4356 \f
4357 /* The following HARD_REG_SETs indicate when each hard register is
4358    used for a reload of various parts of the current insn.  */
4359
4360 /* If reg is in use as a reload reg for a RELOAD_OTHER reload.  */
4361 static HARD_REG_SET reload_reg_used;
4362 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I.  */
4363 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4364 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I.  */
4365 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4366 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I.  */
4367 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4368 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I.  */
4369 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4370 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I.  */
4371 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4372 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I.  */
4373 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4374 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload.  */
4375 static HARD_REG_SET reload_reg_used_in_op_addr;
4376 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload.  */
4377 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4378 /* If reg is in use for a RELOAD_FOR_INSN reload.  */
4379 static HARD_REG_SET reload_reg_used_in_insn;
4380 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload.  */
4381 static HARD_REG_SET reload_reg_used_in_other_addr;
4382
4383 /* If reg is in use as a reload reg for any sort of reload.  */
4384 static HARD_REG_SET reload_reg_used_at_all;
4385
4386 /* If reg is use as an inherited reload.  We just mark the first register
4387    in the group.  */
4388 static HARD_REG_SET reload_reg_used_for_inherit;
4389
4390 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4391    TYPE. MODE is used to indicate how many consecutive regs are
4392    actually used.  */
4393
4394 static void
4395 mark_reload_reg_in_use (regno, opnum, type, mode)
4396      int regno;
4397      int opnum;
4398      enum reload_type type;
4399      enum machine_mode mode;
4400 {
4401   int nregs = HARD_REGNO_NREGS (regno, mode);
4402   int i;
4403
4404   for (i = regno; i < nregs + regno; i++)
4405     {
4406       switch (type)
4407         {
4408         case RELOAD_OTHER:
4409           SET_HARD_REG_BIT (reload_reg_used, i);
4410           break;
4411
4412         case RELOAD_FOR_INPUT_ADDRESS:
4413           SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4414           break;
4415
4416         case RELOAD_FOR_INPADDR_ADDRESS:
4417           SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4418           break;
4419
4420         case RELOAD_FOR_OUTPUT_ADDRESS:
4421           SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4422           break;
4423
4424         case RELOAD_FOR_OUTADDR_ADDRESS:
4425           SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4426           break;
4427
4428         case RELOAD_FOR_OPERAND_ADDRESS:
4429           SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4430           break;
4431
4432         case RELOAD_FOR_OPADDR_ADDR:
4433           SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4434           break;
4435
4436         case RELOAD_FOR_OTHER_ADDRESS:
4437           SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4438           break;
4439
4440         case RELOAD_FOR_INPUT:
4441           SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4442           break;
4443
4444         case RELOAD_FOR_OUTPUT:
4445           SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4446           break;
4447
4448         case RELOAD_FOR_INSN:
4449           SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4450           break;
4451         }
4452
4453       SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4454     }
4455 }
4456
4457 /* Similarly, but show REGNO is no longer in use for a reload.  */
4458
4459 static void
4460 clear_reload_reg_in_use (regno, opnum, type, mode)
4461      int regno;
4462      int opnum;
4463      enum reload_type type;
4464      enum machine_mode mode;
4465 {
4466   int nregs = HARD_REGNO_NREGS (regno, mode);
4467   int i;
4468
4469   for (i = regno; i < nregs + regno; i++)
4470     {
4471       switch (type)
4472         {
4473         case RELOAD_OTHER:
4474           CLEAR_HARD_REG_BIT (reload_reg_used, i);
4475           break;
4476
4477         case RELOAD_FOR_INPUT_ADDRESS:
4478           CLEAR_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4479           break;
4480
4481         case RELOAD_FOR_INPADDR_ADDRESS:
4482           CLEAR_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4483           break;
4484
4485         case RELOAD_FOR_OUTPUT_ADDRESS:
4486           CLEAR_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4487           break;
4488
4489         case RELOAD_FOR_OUTADDR_ADDRESS:
4490           CLEAR_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4491           break;
4492
4493         case RELOAD_FOR_OPERAND_ADDRESS:
4494           CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4495           break;
4496
4497         case RELOAD_FOR_OPADDR_ADDR:
4498           CLEAR_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4499           break;
4500
4501         case RELOAD_FOR_OTHER_ADDRESS:
4502           CLEAR_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4503           break;
4504
4505         case RELOAD_FOR_INPUT:
4506           CLEAR_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4507           break;
4508
4509         case RELOAD_FOR_OUTPUT:
4510           CLEAR_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4511           break;
4512
4513         case RELOAD_FOR_INSN:
4514           CLEAR_HARD_REG_BIT (reload_reg_used_in_insn, i);
4515           break;
4516         }
4517     }
4518 }
4519
4520 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4521    specified by OPNUM and TYPE.  */
4522
4523 static int
4524 reload_reg_free_p (regno, opnum, type)
4525      int regno;
4526      int opnum;
4527      enum reload_type type;
4528 {
4529   int i;
4530
4531   /* In use for a RELOAD_OTHER means it's not available for anything.  */
4532   if (TEST_HARD_REG_BIT (reload_reg_used, regno))
4533     return 0;
4534
4535   switch (type)
4536     {
4537     case RELOAD_OTHER:
4538       /* In use for anything means we can't use it for RELOAD_OTHER.  */
4539       if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4540           || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4541           || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4542         return 0;
4543
4544       for (i = 0; i < reload_n_operands; i++)
4545         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4546             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4547             || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4548             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4549             || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4550             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4551           return 0;
4552
4553       return 1;
4554
4555     case RELOAD_FOR_INPUT:
4556       if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4557           || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4558         return 0;
4559
4560       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4561         return 0;
4562
4563       /* If it is used for some other input, can't use it.  */
4564       for (i = 0; i < reload_n_operands; i++)
4565         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4566           return 0;
4567
4568       /* If it is used in a later operand's address, can't use it.  */
4569       for (i = opnum + 1; i < reload_n_operands; i++)
4570         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4571             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4572           return 0;
4573
4574       return 1;
4575
4576     case RELOAD_FOR_INPUT_ADDRESS:
4577       /* Can't use a register if it is used for an input address for this
4578          operand or used as an input in an earlier one.  */
4579       if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4580           || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4581         return 0;
4582
4583       for (i = 0; i < opnum; i++)
4584         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4585           return 0;
4586
4587       return 1;
4588
4589     case RELOAD_FOR_INPADDR_ADDRESS:
4590       /* Can't use a register if it is used for an input address
4591          address for this operand or used as an input in an earlier
4592          one.  */
4593       if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4594         return 0;
4595
4596       for (i = 0; i < opnum; i++)
4597         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4598           return 0;
4599
4600       return 1;
4601
4602     case RELOAD_FOR_OUTPUT_ADDRESS:
4603       /* Can't use a register if it is used for an output address for this
4604          operand or used as an output in this or a later operand.  */
4605       if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4606         return 0;
4607
4608       for (i = opnum; i < reload_n_operands; i++)
4609         if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4610           return 0;
4611
4612       return 1;
4613
4614     case RELOAD_FOR_OUTADDR_ADDRESS:
4615       /* Can't use a register if it is used for an output address
4616          address for this operand or used as an output in this or a
4617          later operand.  */
4618       if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4619         return 0;
4620
4621       for (i = opnum; i < reload_n_operands; i++)
4622         if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4623           return 0;
4624
4625       return 1;
4626
4627     case RELOAD_FOR_OPERAND_ADDRESS:
4628       for (i = 0; i < reload_n_operands; i++)
4629         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4630           return 0;
4631
4632       return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4633               && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4634
4635     case RELOAD_FOR_OPADDR_ADDR:
4636       for (i = 0; i < reload_n_operands; i++)
4637         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4638           return 0;
4639
4640       return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4641
4642     case RELOAD_FOR_OUTPUT:
4643       /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4644          outputs, or an operand address for this or an earlier output.  */
4645       if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4646         return 0;
4647
4648       for (i = 0; i < reload_n_operands; i++)
4649         if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4650           return 0;
4651
4652       for (i = 0; i <= opnum; i++)
4653         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4654             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4655           return 0;
4656
4657       return 1;
4658
4659     case RELOAD_FOR_INSN:
4660       for (i = 0; i < reload_n_operands; i++)
4661         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4662             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4663           return 0;
4664
4665       return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4666               && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4667
4668     case RELOAD_FOR_OTHER_ADDRESS:
4669       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4670     }
4671   abort ();
4672 }
4673
4674 /* Return 1 if the value in reload reg REGNO, as used by a reload
4675    needed for the part of the insn specified by OPNUM and TYPE,
4676    is not in use for a reload in any prior part of the insn.
4677
4678    We can assume that the reload reg was already tested for availability
4679    at the time it is needed, and we should not check this again,
4680    in case the reg has already been marked in use.  */
4681
4682 static int
4683 reload_reg_free_before_p (regno, opnum, type)
4684      int regno;
4685      int opnum;
4686      enum reload_type type;
4687 {
4688   int i;
4689
4690   switch (type)
4691     {
4692     case RELOAD_FOR_OTHER_ADDRESS:
4693       /* These always come first.  */
4694       return 1;
4695
4696     case RELOAD_OTHER:
4697       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4698
4699       /* If this use is for part of the insn,
4700          check the reg is not in use for any prior part.  It is tempting
4701          to try to do this by falling through from objecs that occur
4702          later in the insn to ones that occur earlier, but that will not
4703          correctly take into account the fact that here we MUST ignore
4704          things that would prevent the register from being allocated in
4705          the first place, since we know that it was allocated.  */
4706
4707     case RELOAD_FOR_OUTPUT_ADDRESS:
4708     case RELOAD_FOR_OUTADDR_ADDRESS:
4709       /* Earlier reloads are for earlier outputs or their addresses,
4710          any RELOAD_FOR_INSN reloads, any inputs or their addresses, or any
4711          RELOAD_FOR_OTHER_ADDRESS reloads (we know it can't conflict with
4712          RELOAD_OTHER)..  */
4713       for (i = 0; i < opnum; i++)
4714         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4715             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4716             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4717           return 0;
4718
4719       if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4720         return 0;
4721
4722       for (i = 0; i < reload_n_operands; i++)
4723         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4724             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4725             || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4726           return 0;
4727
4728       return (! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4729               && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4730               && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4731                                    
4732     case RELOAD_FOR_OUTPUT:
4733       /* This can't be used in the output address for this operand and
4734          anything that can't be used for it, except that we've already
4735          tested for RELOAD_FOR_INSN objects.  */
4736
4737       if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno)
4738           || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4739         return 0;
4740
4741       for (i = 0; i < opnum; i++)
4742         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4743             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4744             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4745           return 0;
4746
4747       for (i = 0; i < reload_n_operands; i++)
4748         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4749             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4750             || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4751             || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4752           return 0;
4753
4754       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4755
4756     case RELOAD_FOR_OPERAND_ADDRESS:
4757       /* Earlier reloads include RELOAD_FOR_OPADDR_ADDR reloads.  */
4758       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4759         return 0;
4760
4761       /* ... fall through ...  */
4762
4763     case RELOAD_FOR_OPADDR_ADDR:
4764     case RELOAD_FOR_INSN:
4765       /* These can't conflict with inputs, or each other, so all we have to
4766          test is input addresses and the addresses of OTHER items.  */
4767
4768       for (i = 0; i < reload_n_operands; i++)
4769         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4770             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4771           return 0;
4772
4773       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4774
4775     case RELOAD_FOR_INPUT:
4776       /* The only things earlier are the address for this and
4777          earlier inputs, other inputs (which we know we don't conflict
4778          with), and addresses of RELOAD_OTHER objects.  */
4779
4780       for (i = 0; i <= opnum; i++)
4781         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4782             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4783           return 0;
4784
4785       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4786
4787     case RELOAD_FOR_INPUT_ADDRESS:
4788     case RELOAD_FOR_INPADDR_ADDRESS:
4789       /* Similarly, all we have to check is for use in earlier inputs'
4790          addresses.  */
4791       for (i = 0; i < opnum; i++)
4792         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4793             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4794           return 0;
4795
4796       return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4797     }
4798   abort ();
4799 }
4800
4801 /* Return 1 if the value in reload reg REGNO, as used by a reload
4802    needed for the part of the insn specified by OPNUM and TYPE,
4803    is still available in REGNO at the end of the insn.
4804
4805    We can assume that the reload reg was already tested for availability
4806    at the time it is needed, and we should not check this again,
4807    in case the reg has already been marked in use.  */
4808
4809 static int
4810 reload_reg_reaches_end_p (regno, opnum, type)
4811      int regno;
4812      int opnum;
4813      enum reload_type type;
4814 {
4815   int i;
4816
4817   switch (type)
4818     {
4819     case RELOAD_OTHER:
4820       /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4821          its value must reach the end.  */
4822       return 1;
4823
4824       /* If this use is for part of the insn,
4825          its value reaches if no subsequent part uses the same register. 
4826          Just like the above function, don't try to do this with lots
4827          of fallthroughs.  */
4828
4829     case RELOAD_FOR_OTHER_ADDRESS:
4830       /* Here we check for everything else, since these don't conflict
4831          with anything else and everything comes later.  */
4832
4833       for (i = 0; i < reload_n_operands; i++)
4834         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4835             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4836             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4837             || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4838             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4839             || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4840           return 0;
4841
4842       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4843               && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4844               && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4845
4846     case RELOAD_FOR_INPUT_ADDRESS:
4847     case RELOAD_FOR_INPADDR_ADDRESS:
4848       /* Similar, except that we check only for this and subsequent inputs
4849          and the address of only subsequent inputs and we do not need
4850          to check for RELOAD_OTHER objects since they are known not to
4851          conflict.  */
4852
4853       for (i = opnum; i < reload_n_operands; i++)
4854         if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4855           return 0;
4856
4857       for (i = opnum + 1; i < reload_n_operands; i++)
4858         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4859             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4860           return 0;
4861
4862       for (i = 0; i < reload_n_operands; i++)
4863         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4864             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4865             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4866           return 0;
4867
4868       if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4869         return 0;
4870
4871       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4872               && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4873
4874     case RELOAD_FOR_INPUT:
4875       /* Similar to input address, except we start at the next operand for
4876          both input and input address and we do not check for 
4877          RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4878          would conflict.  */
4879
4880       for (i = opnum + 1; i < reload_n_operands; i++)
4881         if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4882             || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4883             || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4884           return 0;
4885
4886       /* ... fall through ...  */
4887
4888     case RELOAD_FOR_OPERAND_ADDRESS:
4889       /* Check outputs and their addresses.  */
4890
4891       for (i = 0; i < reload_n_operands; i++)
4892         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4893             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4894             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4895           return 0;
4896
4897       return 1;
4898
4899     case RELOAD_FOR_OPADDR_ADDR:
4900       for (i = 0; i < reload_n_operands; i++)
4901         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4902             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4903             || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4904           return 0;
4905
4906       return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4907               && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno));
4908
4909     case RELOAD_FOR_INSN:
4910       /* These conflict with other outputs with RELOAD_OTHER.  So
4911          we need only check for output addresses.  */
4912
4913       opnum = -1;
4914
4915       /* ... fall through ...  */
4916
4917     case RELOAD_FOR_OUTPUT:
4918     case RELOAD_FOR_OUTPUT_ADDRESS:
4919     case RELOAD_FOR_OUTADDR_ADDRESS:
4920       /* We already know these can't conflict with a later output.  So the
4921          only thing to check are later output addresses.  */
4922       for (i = opnum + 1; i < reload_n_operands; i++)
4923         if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4924             || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4925           return 0;
4926
4927       return 1;
4928     }
4929
4930   abort ();
4931 }
4932 \f
4933 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4934    Return 0 otherwise.
4935
4936    This function uses the same algorithm as reload_reg_free_p above.  */
4937
4938 static int
4939 reloads_conflict (r1, r2)
4940      int r1, r2;
4941 {
4942   enum reload_type r1_type = reload_when_needed[r1];
4943   enum reload_type r2_type = reload_when_needed[r2];
4944   int r1_opnum = reload_opnum[r1];
4945   int r2_opnum = reload_opnum[r2];
4946
4947   /* RELOAD_OTHER conflicts with everything.  */
4948   if (r2_type == RELOAD_OTHER)
4949     return 1;
4950
4951   /* Otherwise, check conflicts differently for each type.  */
4952
4953   switch (r1_type)
4954     {
4955     case RELOAD_FOR_INPUT:
4956       return (r2_type == RELOAD_FOR_INSN 
4957               || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4958               || r2_type == RELOAD_FOR_OPADDR_ADDR
4959               || r2_type == RELOAD_FOR_INPUT
4960               || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4961                    || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4962                   && r2_opnum > r1_opnum));
4963
4964     case RELOAD_FOR_INPUT_ADDRESS:
4965       return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4966               || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4967
4968     case RELOAD_FOR_INPADDR_ADDRESS:
4969       return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4970               || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4971
4972     case RELOAD_FOR_OUTPUT_ADDRESS:
4973       return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4974               || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4975
4976     case RELOAD_FOR_OUTADDR_ADDRESS:
4977       return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4978               || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum >= r1_opnum));
4979
4980     case RELOAD_FOR_OPERAND_ADDRESS:
4981       return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4982               || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4983
4984     case RELOAD_FOR_OPADDR_ADDR:
4985       return (r2_type == RELOAD_FOR_INPUT 
4986               || r2_type == RELOAD_FOR_OPADDR_ADDR);
4987
4988     case RELOAD_FOR_OUTPUT:
4989       return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4990               || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4991                    || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4992                   && r2_opnum >= r1_opnum));
4993
4994     case RELOAD_FOR_INSN:
4995       return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4996               || r2_type == RELOAD_FOR_INSN
4997               || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4998
4999     case RELOAD_FOR_OTHER_ADDRESS:
5000       return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5001
5002     case RELOAD_OTHER:
5003       return 1;
5004
5005     default:
5006       abort ();
5007     }
5008 }
5009 \f
5010 /* Vector of reload-numbers showing the order in which the reloads should
5011    be processed.  */
5012 short reload_order[MAX_RELOADS];
5013
5014 /* Indexed by reload number, 1 if incoming value
5015    inherited from previous insns.  */
5016 char reload_inherited[MAX_RELOADS];
5017
5018 /* For an inherited reload, this is the insn the reload was inherited from,
5019    if we know it.  Otherwise, this is 0.  */
5020 rtx reload_inheritance_insn[MAX_RELOADS];
5021
5022 /* If non-zero, this is a place to get the value of the reload,
5023    rather than using reload_in.  */
5024 rtx reload_override_in[MAX_RELOADS];
5025
5026 /* For each reload, the index in spill_regs of the spill register used,
5027    or -1 if we did not need one of the spill registers for this reload.  */
5028 int reload_spill_index[MAX_RELOADS];
5029
5030 /* Find a spill register to use as a reload register for reload R.
5031    LAST_RELOAD is non-zero if this is the last reload for the insn being
5032    processed.
5033
5034    Set reload_reg_rtx[R] to the register allocated.
5035
5036    If NOERROR is nonzero, we return 1 if successful,
5037    or 0 if we couldn't find a spill reg and we didn't change anything.  */
5038
5039 static int
5040 allocate_reload_reg (r, insn, last_reload, noerror)
5041      int r;
5042      rtx insn;
5043      int last_reload;
5044      int noerror;
5045 {
5046   int i;
5047   int pass;
5048   int count;
5049   rtx new;
5050   int regno;
5051
5052   /* If we put this reload ahead, thinking it is a group,
5053      then insist on finding a group.  Otherwise we can grab a
5054      reg that some other reload needs.
5055      (That can happen when we have a 68000 DATA_OR_FP_REG
5056      which is a group of data regs or one fp reg.)
5057      We need not be so restrictive if there are no more reloads
5058      for this insn.
5059
5060      ??? Really it would be nicer to have smarter handling
5061      for that kind of reg class, where a problem like this is normal.
5062      Perhaps those classes should be avoided for reloading
5063      by use of more alternatives.  */
5064
5065   int force_group = reload_nregs[r] > 1 && ! last_reload;
5066
5067   /* If we want a single register and haven't yet found one,
5068      take any reg in the right class and not in use.
5069      If we want a consecutive group, here is where we look for it.
5070
5071      We use two passes so we can first look for reload regs to
5072      reuse, which are already in use for other reloads in this insn,
5073      and only then use additional registers.
5074      I think that maximizing reuse is needed to make sure we don't
5075      run out of reload regs.  Suppose we have three reloads, and
5076      reloads A and B can share regs.  These need two regs.
5077      Suppose A and B are given different regs.
5078      That leaves none for C.  */
5079   for (pass = 0; pass < 2; pass++)
5080     {
5081       /* I is the index in spill_regs.
5082          We advance it round-robin between insns to use all spill regs
5083          equally, so that inherited reloads have a chance
5084          of leapfrogging each other.  Don't do this, however, when we have
5085          group needs and failure would be fatal; if we only have a relatively
5086          small number of spill registers, and more than one of them has
5087          group needs, then by starting in the middle, we may end up 
5088          allocating the first one in such a way that we are not left with
5089          sufficient groups to handle the rest.  */
5090
5091       if (noerror || ! force_group)
5092         i = last_spill_reg;
5093       else
5094         i = -1;
5095           
5096       for (count = 0; count < n_spills; count++)
5097         {
5098           int class = (int) reload_reg_class[r];
5099
5100           i = (i + 1) % n_spills;
5101
5102           if (reload_reg_free_p (spill_regs[i], reload_opnum[r],
5103                                  reload_when_needed[r])
5104               && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
5105               && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
5106               /* Look first for regs to share, then for unshared.  But
5107                  don't share regs used for inherited reloads; they are
5108                  the ones we want to preserve.  */
5109               && (pass
5110                   || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5111                                          spill_regs[i])
5112                       && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5113                                               spill_regs[i]))))
5114             {
5115               int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5116               /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5117                  (on 68000) got us two FP regs.  If NR is 1,
5118                  we would reject both of them.  */
5119               if (force_group)
5120                 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
5121               /* If we need only one reg, we have already won.  */
5122               if (nr == 1)
5123                 {
5124                   /* But reject a single reg if we demand a group.  */
5125                   if (force_group)
5126                     continue;
5127                   break;
5128                 }
5129               /* Otherwise check that as many consecutive regs as we need
5130                  are available here.
5131                  Also, don't use for a group registers that are
5132                  needed for nongroups.  */
5133               if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
5134                 while (nr > 1)
5135                   {
5136                     regno = spill_regs[i] + nr - 1;
5137                     if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5138                           && spill_reg_order[regno] >= 0
5139                           && reload_reg_free_p (regno, reload_opnum[r],
5140                                                 reload_when_needed[r])
5141                           && ! TEST_HARD_REG_BIT (counted_for_nongroups,
5142                                                   regno)))
5143                       break;
5144                     nr--;
5145                   }
5146               if (nr == 1)
5147                 break;
5148             }
5149         }
5150
5151       /* If we found something on pass 1, omit pass 2.  */
5152       if (count < n_spills)
5153         break;
5154     }
5155
5156   /* We should have found a spill register by now.  */
5157   if (count == n_spills)
5158     {
5159       if (noerror)
5160         return 0;
5161       goto failure;
5162     }
5163
5164   /* I is the index in SPILL_REG_RTX of the reload register we are to
5165      allocate.  Get an rtx for it and find its register number.  */
5166
5167   new = spill_reg_rtx[i];
5168
5169   if (new == 0 || GET_MODE (new) != reload_mode[r])
5170     spill_reg_rtx[i] = new
5171       = gen_rtx (REG, reload_mode[r], spill_regs[i]);
5172             
5173   regno = true_regnum (new);
5174
5175   /* Detect when the reload reg can't hold the reload mode.
5176      This used to be one `if', but Sequent compiler can't handle that.  */
5177   if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5178     {
5179       enum machine_mode test_mode = VOIDmode;
5180       if (reload_in[r])
5181         test_mode = GET_MODE (reload_in[r]);
5182       /* If reload_in[r] has VOIDmode, it means we will load it
5183          in whatever mode the reload reg has: to wit, reload_mode[r].
5184          We have already tested that for validity.  */
5185       /* Aside from that, we need to test that the expressions
5186          to reload from or into have modes which are valid for this
5187          reload register.  Otherwise the reload insns would be invalid.  */
5188       if (! (reload_in[r] != 0 && test_mode != VOIDmode
5189              && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5190         if (! (reload_out[r] != 0
5191                && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
5192           {
5193             /* The reg is OK.  */
5194             last_spill_reg = i;
5195
5196             /* Mark as in use for this insn the reload regs we use
5197                for this.  */
5198             mark_reload_reg_in_use (spill_regs[i], reload_opnum[r],
5199                                     reload_when_needed[r], reload_mode[r]);
5200
5201             reload_reg_rtx[r] = new;
5202             reload_spill_index[r] = i;
5203             return 1;
5204           }
5205     }
5206
5207   /* The reg is not OK.  */
5208   if (noerror)
5209     return 0;
5210
5211  failure:
5212   if (asm_noperands (PATTERN (insn)) < 0)
5213     /* It's the compiler's fault.  */
5214     fatal_insn ("Could not find a spill register", insn);
5215
5216   /* It's the user's fault; the operand's mode and constraint
5217      don't match.  Disable this reload so we don't crash in final.  */
5218   error_for_asm (insn,
5219                  "`asm' operand constraint incompatible with operand size");
5220   reload_in[r] = 0;
5221   reload_out[r] = 0;
5222   reload_reg_rtx[r] = 0;
5223   reload_optional[r] = 1;
5224   reload_secondary_p[r] = 1;
5225
5226   return 1;
5227 }
5228 \f
5229 /* Assign hard reg targets for the pseudo-registers we must reload
5230    into hard regs for this insn.
5231    Also output the instructions to copy them in and out of the hard regs.
5232
5233    For machines with register classes, we are responsible for
5234    finding a reload reg in the proper class.  */
5235
5236 static void
5237 choose_reload_regs (insn, avoid_return_reg)
5238      rtx insn;
5239      rtx avoid_return_reg;
5240 {
5241   register int i, j;
5242   int max_group_size = 1;
5243   enum reg_class group_class = NO_REGS;
5244   int inheritance;
5245
5246   rtx save_reload_reg_rtx[MAX_RELOADS];
5247   char save_reload_inherited[MAX_RELOADS];
5248   rtx save_reload_inheritance_insn[MAX_RELOADS];
5249   rtx save_reload_override_in[MAX_RELOADS];
5250   int save_reload_spill_index[MAX_RELOADS];
5251   HARD_REG_SET save_reload_reg_used;
5252   HARD_REG_SET save_reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5253   HARD_REG_SET save_reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5254   HARD_REG_SET save_reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5255   HARD_REG_SET save_reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5256   HARD_REG_SET save_reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5257   HARD_REG_SET save_reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5258   HARD_REG_SET save_reload_reg_used_in_op_addr;
5259   HARD_REG_SET save_reload_reg_used_in_op_addr_reload;
5260   HARD_REG_SET save_reload_reg_used_in_insn;
5261   HARD_REG_SET save_reload_reg_used_in_other_addr;
5262   HARD_REG_SET save_reload_reg_used_at_all;
5263
5264   bzero (reload_inherited, MAX_RELOADS);
5265   bzero ((char *) reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
5266   bzero ((char *) reload_override_in, MAX_RELOADS * sizeof (rtx));
5267
5268   CLEAR_HARD_REG_SET (reload_reg_used);
5269   CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5270   CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5271   CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5272   CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5273   CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5274
5275   for (i = 0; i < reload_n_operands; i++)
5276     {
5277       CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5278       CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5279       CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5280       CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5281       CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5282       CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5283     }
5284
5285   /* Don't bother with avoiding the return reg
5286      if we have no mandatory reload that could use it.  */
5287   if (SMALL_REGISTER_CLASSES && avoid_return_reg)
5288     {
5289       int do_avoid = 0;
5290       int regno = REGNO (avoid_return_reg);
5291       int nregs
5292         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5293       int r;
5294
5295       for (r = regno; r < regno + nregs; r++)
5296         if (spill_reg_order[r] >= 0)
5297           for (j = 0; j < n_reloads; j++)
5298             if (!reload_optional[j] && reload_reg_rtx[j] == 0
5299                 && (reload_in[j] != 0 || reload_out[j] != 0
5300                     || reload_secondary_p[j])
5301                 &&
5302                 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
5303               do_avoid = 1;
5304       if (!do_avoid)
5305         avoid_return_reg = 0;
5306     }
5307
5308 #if 0  /* Not needed, now that we can always retry without inheritance.  */
5309   /* See if we have more mandatory reloads than spill regs.
5310      If so, then we cannot risk optimizations that could prevent
5311      reloads from sharing one spill register.
5312
5313      Since we will try finding a better register than reload_reg_rtx
5314      unless it is equal to reload_in or reload_out, count such reloads.  */
5315
5316   {
5317     int tem = SMALL_REGISTER_CLASSES? (avoid_return_reg != 0): 0;
5318     for (j = 0; j < n_reloads; j++)
5319       if (! reload_optional[j]
5320           && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
5321           && (reload_reg_rtx[j] == 0
5322               || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
5323                   && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
5324         tem++;
5325     if (tem > n_spills)
5326       must_reuse = 1;
5327   }
5328 #endif
5329
5330   /* Don't use the subroutine call return reg for a reload
5331      if we are supposed to avoid it.  */
5332   if (SMALL_REGISTER_CLASSES && avoid_return_reg)
5333     {
5334       int regno = REGNO (avoid_return_reg);
5335       int nregs
5336         = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
5337       int r;
5338
5339       for (r = regno; r < regno + nregs; r++)
5340         if (spill_reg_order[r] >= 0)
5341           SET_HARD_REG_BIT (reload_reg_used, r);
5342     }
5343
5344   /* In order to be certain of getting the registers we need,
5345      we must sort the reloads into order of increasing register class.
5346      Then our grabbing of reload registers will parallel the process
5347      that provided the reload registers.
5348
5349      Also note whether any of the reloads wants a consecutive group of regs.
5350      If so, record the maximum size of the group desired and what
5351      register class contains all the groups needed by this insn.  */
5352
5353   for (j = 0; j < n_reloads; j++)
5354     {
5355       reload_order[j] = j;
5356       reload_spill_index[j] = -1;
5357
5358       reload_mode[j]
5359         = (reload_inmode[j] == VOIDmode
5360            || (GET_MODE_SIZE (reload_outmode[j])
5361                > GET_MODE_SIZE (reload_inmode[j])))
5362           ? reload_outmode[j] : reload_inmode[j];
5363
5364       reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
5365
5366       if (reload_nregs[j] > 1)
5367         {
5368           max_group_size = MAX (reload_nregs[j], max_group_size);
5369           group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
5370         }
5371
5372       /* If we have already decided to use a certain register,
5373          don't use it in another way.  */
5374       if (reload_reg_rtx[j])
5375         mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]), reload_opnum[j],
5376                                 reload_when_needed[j], reload_mode[j]);
5377     }
5378
5379   if (n_reloads > 1)
5380     qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5381
5382   bcopy ((char *) reload_reg_rtx, (char *) save_reload_reg_rtx,
5383          sizeof reload_reg_rtx);
5384   bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
5385   bcopy ((char *) reload_inheritance_insn,
5386          (char *) save_reload_inheritance_insn,
5387          sizeof reload_inheritance_insn);
5388   bcopy ((char *) reload_override_in, (char *) save_reload_override_in,
5389          sizeof reload_override_in);
5390   bcopy ((char *) reload_spill_index, (char *) save_reload_spill_index,
5391          sizeof reload_spill_index);
5392   COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
5393   COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
5394   COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
5395                      reload_reg_used_in_op_addr);
5396
5397   COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr_reload,
5398                      reload_reg_used_in_op_addr_reload);
5399
5400   COPY_HARD_REG_SET (save_reload_reg_used_in_insn,
5401                      reload_reg_used_in_insn);
5402   COPY_HARD_REG_SET (save_reload_reg_used_in_other_addr,
5403                      reload_reg_used_in_other_addr);
5404
5405   for (i = 0; i < reload_n_operands; i++)
5406     {
5407       COPY_HARD_REG_SET (save_reload_reg_used_in_output[i],
5408                          reload_reg_used_in_output[i]);
5409       COPY_HARD_REG_SET (save_reload_reg_used_in_input[i],
5410                          reload_reg_used_in_input[i]);
5411       COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr[i],
5412                          reload_reg_used_in_input_addr[i]);
5413       COPY_HARD_REG_SET (save_reload_reg_used_in_inpaddr_addr[i],
5414                          reload_reg_used_in_inpaddr_addr[i]);
5415       COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr[i],
5416                          reload_reg_used_in_output_addr[i]);
5417       COPY_HARD_REG_SET (save_reload_reg_used_in_outaddr_addr[i],
5418                          reload_reg_used_in_outaddr_addr[i]);
5419     }
5420
5421   /* If -O, try first with inheritance, then turning it off.
5422      If not -O, don't do inheritance.
5423      Using inheritance when not optimizing leads to paradoxes
5424      with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5425      because one side of the comparison might be inherited.  */
5426
5427   for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5428     {
5429       /* Process the reloads in order of preference just found.
5430          Beyond this point, subregs can be found in reload_reg_rtx.
5431
5432          This used to look for an existing reloaded home for all
5433          of the reloads, and only then perform any new reloads.
5434          But that could lose if the reloads were done out of reg-class order
5435          because a later reload with a looser constraint might have an old
5436          home in a register needed by an earlier reload with a tighter constraint.
5437
5438          To solve this, we make two passes over the reloads, in the order
5439          described above.  In the first pass we try to inherit a reload
5440          from a previous insn.  If there is a later reload that needs a
5441          class that is a proper subset of the class being processed, we must
5442          also allocate a spill register during the first pass.
5443
5444          Then make a second pass over the reloads to allocate any reloads
5445          that haven't been given registers yet.  */
5446
5447       CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5448
5449       for (j = 0; j < n_reloads; j++)
5450         {
5451           register int r = reload_order[j];
5452
5453           /* Ignore reloads that got marked inoperative.  */
5454           if (reload_out[r] == 0 && reload_in[r] == 0
5455               && ! reload_secondary_p[r])
5456             continue;
5457
5458           /* If find_reloads chose a to use reload_in or reload_out as a reload
5459              register, we don't need to chose one.  Otherwise, try even if it
5460              found one since we might save an insn if we find the value lying
5461              around.  */
5462           if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
5463               && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
5464                   || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
5465             continue;
5466
5467 #if 0 /* No longer needed for correct operation.
5468          It might give better code, or might not; worth an experiment?  */
5469           /* If this is an optional reload, we can't inherit from earlier insns
5470              until we are sure that any non-optional reloads have been allocated.
5471              The following code takes advantage of the fact that optional reloads
5472              are at the end of reload_order.  */
5473           if (reload_optional[r] != 0)
5474             for (i = 0; i < j; i++)
5475               if ((reload_out[reload_order[i]] != 0
5476                    || reload_in[reload_order[i]] != 0
5477                    || reload_secondary_p[reload_order[i]])
5478                   && ! reload_optional[reload_order[i]]
5479                   && reload_reg_rtx[reload_order[i]] == 0)
5480                 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
5481 #endif
5482
5483           /* First see if this pseudo is already available as reloaded
5484              for a previous insn.  We cannot try to inherit for reloads
5485              that are smaller than the maximum number of registers needed
5486              for groups unless the register we would allocate cannot be used
5487              for the groups.
5488
5489              We could check here to see if this is a secondary reload for
5490              an object that is already in a register of the desired class.
5491              This would avoid the need for the secondary reload register.
5492              But this is complex because we can't easily determine what
5493              objects might want to be loaded via this reload.  So let a
5494              register be allocated here.  In `emit_reload_insns' we suppress
5495              one of the loads in the case described above.  */
5496
5497           if (inheritance)
5498             {
5499               register int regno = -1;
5500               enum machine_mode mode;
5501
5502               if (reload_in[r] == 0)
5503                 ;
5504               else if (GET_CODE (reload_in[r]) == REG)
5505                 {
5506                   regno = REGNO (reload_in[r]);
5507                   mode = GET_MODE (reload_in[r]);
5508                 }
5509               else if (GET_CODE (reload_in_reg[r]) == REG)
5510                 {
5511                   regno = REGNO (reload_in_reg[r]);
5512                   mode = GET_MODE (reload_in_reg[r]);
5513                 }
5514 #if 0
5515               /* This won't work, since REGNO can be a pseudo reg number.
5516                  Also, it takes much more hair to keep track of all the things
5517                  that can invalidate an inherited reload of part of a pseudoreg.  */
5518               else if (GET_CODE (reload_in[r]) == SUBREG
5519                        && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
5520                 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
5521 #endif
5522
5523               if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5524                 {
5525                   i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
5526
5527                   if (reg_reloaded_contents[i] == regno
5528                       && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
5529                           >= GET_MODE_SIZE (mode))
5530                       && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
5531                       && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5532                                             spill_regs[i])
5533                       && (reload_nregs[r] == max_group_size
5534                           || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5535                                                   spill_regs[i]))
5536                       && reload_reg_free_p (spill_regs[i], reload_opnum[r],
5537                                             reload_when_needed[r])
5538                       && reload_reg_free_before_p (spill_regs[i],
5539                                                    reload_opnum[r],
5540                                                    reload_when_needed[r]))
5541                     {
5542                       /* If a group is needed, verify that all the subsequent
5543                          registers still have their values intact.  */
5544                       int nr
5545                         = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5546                       int k;
5547
5548                       for (k = 1; k < nr; k++)
5549                         if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5550                             != regno)
5551                           break;
5552
5553                       if (k == nr)
5554                         {
5555                           int i1;
5556
5557                           /* We found a register that contains the
5558                              value we need.  If this register is the
5559                              same as an `earlyclobber' operand of the
5560                              current insn, just mark it as a place to
5561                              reload from since we can't use it as the
5562                              reload register itself.  */
5563
5564                           for (i1 = 0; i1 < n_earlyclobbers; i1++)
5565                             if (reg_overlap_mentioned_for_reload_p
5566                                 (reg_last_reload_reg[regno],
5567                                  reload_earlyclobbers[i1]))
5568                               break;
5569
5570                           if (i1 != n_earlyclobbers
5571                               /* Don't really use the inherited spill reg
5572                                  if we need it wider than we've got it.  */
5573                               || (GET_MODE_SIZE (reload_mode[r])
5574                                   > GET_MODE_SIZE (mode)))
5575                             reload_override_in[r] = reg_last_reload_reg[regno];
5576                           else
5577                             {
5578                               int k;
5579                               /* We can use this as a reload reg.  */
5580                               /* Mark the register as in use for this part of
5581                                  the insn.  */
5582                               mark_reload_reg_in_use (spill_regs[i],
5583                                                       reload_opnum[r],
5584                                                       reload_when_needed[r],
5585                                                       reload_mode[r]);
5586                               reload_reg_rtx[r] = reg_last_reload_reg[regno];
5587                               reload_inherited[r] = 1;
5588                               reload_inheritance_insn[r]
5589                                 = reg_reloaded_insn[i];
5590                               reload_spill_index[r] = i;
5591                               for (k = 0; k < nr; k++)
5592                                 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5593                                                   spill_regs[i + k]);
5594                             }
5595                         }
5596                     }
5597                 }
5598             }
5599
5600           /* Here's another way to see if the value is already lying around.  */
5601           if (inheritance
5602               && reload_in[r] != 0
5603               && ! reload_inherited[r]
5604               && reload_out[r] == 0
5605               && (CONSTANT_P (reload_in[r])
5606                   || GET_CODE (reload_in[r]) == PLUS
5607                   || GET_CODE (reload_in[r]) == REG
5608                   || GET_CODE (reload_in[r]) == MEM)
5609               && (reload_nregs[r] == max_group_size
5610                   || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
5611             {
5612               register rtx equiv
5613                 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
5614                                   -1, NULL_PTR, 0, reload_mode[r]);
5615               int regno;
5616
5617               if (equiv != 0)
5618                 {
5619                   if (GET_CODE (equiv) == REG)
5620                     regno = REGNO (equiv);
5621                   else if (GET_CODE (equiv) == SUBREG)
5622                     {
5623                       /* This must be a SUBREG of a hard register.
5624                          Make a new REG since this might be used in an
5625                          address and not all machines support SUBREGs
5626                          there.  */
5627                       regno = REGNO (SUBREG_REG (equiv)) + SUBREG_WORD (equiv);
5628                       equiv = gen_rtx (REG, reload_mode[r], regno);
5629                     }
5630                   else
5631                     abort ();
5632                 }
5633
5634               /* If we found a spill reg, reject it unless it is free
5635                  and of the desired class.  */
5636               if (equiv != 0
5637                   && ((spill_reg_order[regno] >= 0
5638                        && ! reload_reg_free_before_p (regno, reload_opnum[r],
5639                                                       reload_when_needed[r]))
5640                       || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
5641                                               regno)))
5642                 equiv = 0;
5643
5644               if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
5645                 equiv = 0;
5646
5647               if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
5648                 equiv = 0;
5649
5650               /* We found a register that contains the value we need.
5651                  If this register is the same as an `earlyclobber' operand
5652                  of the current insn, just mark it as a place to reload from
5653                  since we can't use it as the reload register itself.  */
5654
5655               if (equiv != 0)
5656                 for (i = 0; i < n_earlyclobbers; i++)
5657                   if (reg_overlap_mentioned_for_reload_p (equiv,
5658                                                           reload_earlyclobbers[i]))
5659                     {
5660                       reload_override_in[r] = equiv;
5661                       equiv = 0;
5662                       break;
5663                     }
5664
5665               /* JRV: If the equiv register we have found is
5666                  explicitly clobbered in the current insn, mark but
5667                  don't use, as above.  */
5668
5669               if (equiv != 0 && regno_clobbered_p (regno, insn))
5670                 {
5671                   reload_override_in[r] = equiv;
5672                   equiv = 0;
5673                 }
5674
5675               /* If we found an equivalent reg, say no code need be generated
5676                  to load it, and use it as our reload reg.  */
5677               if (equiv != 0 && regno != HARD_FRAME_POINTER_REGNUM)
5678                 {
5679                   int nr = HARD_REGNO_NREGS (regno, reload_mode[r]);
5680                   int k;
5681                   reload_reg_rtx[r] = equiv;
5682                   reload_inherited[r] = 1;
5683
5684                   /* If any of the hard registers in EQUIV are spill
5685                      registers, mark them as in use for this insn.  */
5686                   for (k = 0; k < nr; k++)
5687                     {
5688                       i = spill_reg_order[regno + k];
5689                       if (i >= 0)
5690                         {
5691                           mark_reload_reg_in_use (regno, reload_opnum[r],
5692                                                   reload_when_needed[r],
5693                                                   reload_mode[r]);
5694                           SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5695                                             regno + k);
5696                         }
5697                     }
5698                 }
5699             }
5700
5701           /* If we found a register to use already, or if this is an optional
5702              reload, we are done.  */
5703           if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
5704             continue;
5705
5706 #if 0 /* No longer needed for correct operation.  Might or might not
5707          give better code on the average.  Want to experiment?  */
5708
5709           /* See if there is a later reload that has a class different from our
5710              class that intersects our class or that requires less register
5711              than our reload.  If so, we must allocate a register to this
5712              reload now, since that reload might inherit a previous reload
5713              and take the only available register in our class.  Don't do this
5714              for optional reloads since they will force all previous reloads
5715              to be allocated.  Also don't do this for reloads that have been
5716              turned off.  */
5717
5718           for (i = j + 1; i < n_reloads; i++)
5719             {
5720               int s = reload_order[i];
5721
5722               if ((reload_in[s] == 0 && reload_out[s] == 0
5723                    && ! reload_secondary_p[s])
5724                   || reload_optional[s])
5725                 continue;
5726
5727               if ((reload_reg_class[s] != reload_reg_class[r]
5728                    && reg_classes_intersect_p (reload_reg_class[r],
5729                                                reload_reg_class[s]))
5730                   || reload_nregs[s] < reload_nregs[r])
5731               break;
5732             }
5733
5734           if (i == n_reloads)
5735             continue;
5736
5737           allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
5738 #endif
5739         }
5740
5741       /* Now allocate reload registers for anything non-optional that
5742          didn't get one yet.  */
5743       for (j = 0; j < n_reloads; j++)
5744         {
5745           register int r = reload_order[j];
5746
5747           /* Ignore reloads that got marked inoperative.  */
5748           if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
5749             continue;
5750
5751           /* Skip reloads that already have a register allocated or are
5752              optional.  */
5753           if (reload_reg_rtx[r] != 0 || reload_optional[r])
5754             continue;
5755
5756           if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
5757             break;
5758         }
5759
5760       /* If that loop got all the way, we have won.  */
5761       if (j == n_reloads)
5762         break;
5763
5764     fail:
5765       /* Loop around and try without any inheritance.  */
5766       /* First undo everything done by the failed attempt
5767          to allocate with inheritance.  */
5768       bcopy ((char *) save_reload_reg_rtx, (char *) reload_reg_rtx,
5769              sizeof reload_reg_rtx);
5770       bcopy ((char *) save_reload_inherited, (char *) reload_inherited,
5771              sizeof reload_inherited);
5772       bcopy ((char *) save_reload_inheritance_insn,
5773              (char *) reload_inheritance_insn,
5774              sizeof reload_inheritance_insn);
5775       bcopy ((char *) save_reload_override_in, (char *) reload_override_in,
5776              sizeof reload_override_in);
5777       bcopy ((char *) save_reload_spill_index, (char *) reload_spill_index,
5778              sizeof reload_spill_index);
5779       COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
5780       COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
5781       COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
5782                          save_reload_reg_used_in_op_addr);
5783       COPY_HARD_REG_SET (reload_reg_used_in_op_addr_reload,
5784                          save_reload_reg_used_in_op_addr_reload);
5785       COPY_HARD_REG_SET (reload_reg_used_in_insn,
5786                          save_reload_reg_used_in_insn);
5787       COPY_HARD_REG_SET (reload_reg_used_in_other_addr,
5788                          save_reload_reg_used_in_other_addr);
5789
5790       for (i = 0; i < reload_n_operands; i++)
5791         {
5792           COPY_HARD_REG_SET (reload_reg_used_in_input[i],
5793                              save_reload_reg_used_in_input[i]);
5794           COPY_HARD_REG_SET (reload_reg_used_in_output[i],
5795                              save_reload_reg_used_in_output[i]);
5796           COPY_HARD_REG_SET (reload_reg_used_in_input_addr[i],
5797                              save_reload_reg_used_in_input_addr[i]);
5798           COPY_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i],
5799                              save_reload_reg_used_in_inpaddr_addr[i]);
5800           COPY_HARD_REG_SET (reload_reg_used_in_output_addr[i],
5801                              save_reload_reg_used_in_output_addr[i]);
5802           COPY_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i],
5803                              save_reload_reg_used_in_outaddr_addr[i]);
5804         }
5805     }
5806
5807   /* If we thought we could inherit a reload, because it seemed that
5808      nothing else wanted the same reload register earlier in the insn,
5809      verify that assumption, now that all reloads have been assigned.  */
5810
5811   for (j = 0; j < n_reloads; j++)
5812     {
5813       register int r = reload_order[j];
5814
5815       if (reload_inherited[r] && reload_reg_rtx[r] != 0
5816           && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
5817                                          reload_opnum[r],
5818                                          reload_when_needed[r]))
5819         reload_inherited[r] = 0;
5820
5821       /* If we found a better place to reload from,
5822          validate it in the same fashion, if it is a reload reg.  */
5823       if (reload_override_in[r]
5824           && (GET_CODE (reload_override_in[r]) == REG
5825               || GET_CODE (reload_override_in[r]) == SUBREG))
5826         {
5827           int regno = true_regnum (reload_override_in[r]);
5828           if (spill_reg_order[regno] >= 0
5829               && ! reload_reg_free_before_p (regno, reload_opnum[r],
5830                                              reload_when_needed[r]))
5831             reload_override_in[r] = 0;
5832         }
5833     }
5834
5835   /* Now that reload_override_in is known valid,
5836      actually override reload_in.  */
5837   for (j = 0; j < n_reloads; j++)
5838     if (reload_override_in[j])
5839       reload_in[j] = reload_override_in[j];
5840
5841   /* If this reload won't be done because it has been cancelled or is
5842      optional and not inherited, clear reload_reg_rtx so other
5843      routines (such as subst_reloads) don't get confused.  */
5844   for (j = 0; j < n_reloads; j++)
5845     if (reload_reg_rtx[j] != 0
5846         && ((reload_optional[j] && ! reload_inherited[j])
5847             || (reload_in[j] == 0 && reload_out[j] == 0
5848                 && ! reload_secondary_p[j])))
5849       {
5850         int regno = true_regnum (reload_reg_rtx[j]);
5851
5852         if (spill_reg_order[regno] >= 0)
5853           clear_reload_reg_in_use (regno, reload_opnum[j],
5854                                    reload_when_needed[j], reload_mode[j]);
5855         reload_reg_rtx[j] = 0;
5856       }
5857
5858   /* Record which pseudos and which spill regs have output reloads.  */
5859   for (j = 0; j < n_reloads; j++)
5860     {
5861       register int r = reload_order[j];
5862
5863       i = reload_spill_index[r];
5864
5865       /* I is nonneg if this reload used one of the spill regs.
5866          If reload_reg_rtx[r] is 0, this is an optional reload
5867          that we opted to ignore.  */
5868       if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
5869           && reload_reg_rtx[r] != 0)
5870         {
5871           register int nregno = REGNO (reload_out[r]);
5872           int nr = 1;
5873
5874           if (nregno < FIRST_PSEUDO_REGISTER)
5875             nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
5876
5877           while (--nr >= 0)
5878             reg_has_output_reload[nregno + nr] = 1;
5879
5880           if (i >= 0)
5881             {
5882               nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
5883               while (--nr >= 0)
5884                 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
5885             }
5886
5887           if (reload_when_needed[r] != RELOAD_OTHER
5888               && reload_when_needed[r] != RELOAD_FOR_OUTPUT
5889               && reload_when_needed[r] != RELOAD_FOR_INSN)
5890             abort ();
5891         }
5892     }
5893 }
5894 \f
5895 /* If SMALL_REGISTER_CLASSES is non-zero, we may not have merged two
5896    reloads of the same item for fear that we might not have enough reload
5897    registers. However, normally they will get the same reload register
5898    and hence actually need not be loaded twice.  
5899
5900    Here we check for the most common case of this phenomenon: when we have
5901    a number of reloads for the same object, each of which were allocated
5902    the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5903    reload, and is not modified in the insn itself.  If we find such,
5904    merge all the reloads and set the resulting reload to RELOAD_OTHER.
5905    This will not increase the number of spill registers needed and will
5906    prevent redundant code.  */
5907
5908 static void
5909 merge_assigned_reloads (insn)
5910      rtx insn;
5911 {
5912   int i, j;
5913
5914   /* Scan all the reloads looking for ones that only load values and
5915      are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5916      assigned and not modified by INSN.  */
5917
5918   for (i = 0; i < n_reloads; i++)
5919     {
5920       if (reload_in[i] == 0 || reload_when_needed[i] == RELOAD_OTHER
5921           || reload_out[i] != 0 || reload_reg_rtx[i] == 0
5922           || reg_set_p (reload_reg_rtx[i], insn))
5923         continue;
5924
5925       /* Look at all other reloads.  Ensure that the only use of this
5926          reload_reg_rtx is in a reload that just loads the same value
5927          as we do.  Note that any secondary reloads must be of the identical
5928          class since the values, modes, and result registers are the
5929          same, so we need not do anything with any secondary reloads.  */
5930
5931       for (j = 0; j < n_reloads; j++)
5932         {
5933           if (i == j || reload_reg_rtx[j] == 0
5934               || ! reg_overlap_mentioned_p (reload_reg_rtx[j],
5935                                             reload_reg_rtx[i]))
5936             continue;
5937
5938           /* If the reload regs aren't exactly the same (e.g, different modes)
5939              or if the values are different, we can't merge anything with this
5940              reload register.  */
5941
5942           if (! rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j])
5943               || reload_out[j] != 0 || reload_in[j] == 0
5944               || ! rtx_equal_p (reload_in[i], reload_in[j]))
5945             break;
5946         }
5947
5948       /* If all is OK, merge the reloads.  Only set this to RELOAD_OTHER if
5949          we, in fact, found any matching reloads.  */
5950
5951       if (j == n_reloads)
5952         {
5953           for (j = 0; j < n_reloads; j++)
5954             if (i != j && reload_reg_rtx[j] != 0
5955                 && rtx_equal_p (reload_reg_rtx[i], reload_reg_rtx[j]))
5956               {
5957                 reload_when_needed[i] = RELOAD_OTHER;
5958                 reload_in[j] = 0;
5959                 transfer_replacements (i, j);
5960               }
5961
5962           /* If this is now RELOAD_OTHER, look for any reloads that load
5963              parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
5964              if they were for inputs, RELOAD_OTHER for outputs.  Note that
5965              this test is equivalent to looking for reloads for this operand
5966              number.  */
5967
5968           if (reload_when_needed[i] == RELOAD_OTHER)
5969             for (j = 0; j < n_reloads; j++)
5970               if (reload_in[j] != 0
5971                   && reload_when_needed[i] != RELOAD_OTHER
5972                   && reg_overlap_mentioned_for_reload_p (reload_in[j],
5973                                                          reload_in[i]))
5974                 reload_when_needed[j]
5975                   = ((reload_when_needed[i] == RELOAD_FOR_INPUT_ADDRESS
5976                       || reload_when_needed[i] == RELOAD_FOR_INPADDR_ADDRESS)
5977                      ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
5978         }
5979     }
5980 }           
5981
5982 \f
5983 /* Output insns to reload values in and out of the chosen reload regs.  */
5984
5985 static void
5986 emit_reload_insns (insn)
5987      rtx insn;
5988 {
5989   register int j;
5990   rtx input_reload_insns[MAX_RECOG_OPERANDS];
5991   rtx other_input_address_reload_insns = 0;
5992   rtx other_input_reload_insns = 0;
5993   rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
5994   rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
5995   rtx output_reload_insns[MAX_RECOG_OPERANDS];
5996   rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
5997   rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
5998   rtx operand_reload_insns = 0;
5999   rtx other_operand_reload_insns = 0;
6000   rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6001   rtx following_insn = NEXT_INSN (insn);
6002   rtx before_insn = insn;
6003   int special;
6004   /* Values to be put in spill_reg_store are put here first.  */
6005   rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6006
6007   for (j = 0; j < reload_n_operands; j++)
6008     input_reload_insns[j] = input_address_reload_insns[j]
6009       = inpaddr_address_reload_insns[j]
6010       = output_reload_insns[j] = output_address_reload_insns[j]
6011       = outaddr_address_reload_insns[j]
6012       = other_output_reload_insns[j] = 0;
6013
6014   /* Now output the instructions to copy the data into and out of the
6015      reload registers.  Do these in the order that the reloads were reported,
6016      since reloads of base and index registers precede reloads of operands
6017      and the operands may need the base and index registers reloaded.  */
6018
6019   for (j = 0; j < n_reloads; j++)
6020     {
6021       register rtx old;
6022       rtx oldequiv_reg = 0;
6023       rtx this_reload_insn = 0;
6024
6025       if (reload_spill_index[j] >= 0)
6026         new_spill_reg_store[reload_spill_index[j]] = 0;
6027
6028       old = reload_in[j];
6029       if (old != 0 && ! reload_inherited[j]
6030           && ! rtx_equal_p (reload_reg_rtx[j], old)
6031           && reload_reg_rtx[j] != 0)
6032         {
6033           register rtx reloadreg = reload_reg_rtx[j];
6034           rtx oldequiv = 0;
6035           enum machine_mode mode;
6036           rtx *where;
6037
6038           /* Determine the mode to reload in.
6039              This is very tricky because we have three to choose from.
6040              There is the mode the insn operand wants (reload_inmode[J]).
6041              There is the mode of the reload register RELOADREG.
6042              There is the intrinsic mode of the operand, which we could find
6043              by stripping some SUBREGs.
6044              It turns out that RELOADREG's mode is irrelevant:
6045              we can change that arbitrarily.
6046
6047              Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6048              then the reload reg may not support QImode moves, so use SImode.
6049              If foo is in memory due to spilling a pseudo reg, this is safe,
6050              because the QImode value is in the least significant part of a
6051              slot big enough for a SImode.  If foo is some other sort of
6052              memory reference, then it is impossible to reload this case,
6053              so previous passes had better make sure this never happens.
6054
6055              Then consider a one-word union which has SImode and one of its
6056              members is a float, being fetched as (SUBREG:SF union:SI).
6057              We must fetch that as SFmode because we could be loading into
6058              a float-only register.  In this case OLD's mode is correct.
6059
6060              Consider an immediate integer: it has VOIDmode.  Here we need
6061              to get a mode from something else.
6062
6063              In some cases, there is a fourth mode, the operand's
6064              containing mode.  If the insn specifies a containing mode for
6065              this operand, it overrides all others.
6066
6067              I am not sure whether the algorithm here is always right,
6068              but it does the right things in those cases.  */
6069
6070           mode = GET_MODE (old);
6071           if (mode == VOIDmode)
6072             mode = reload_inmode[j];
6073
6074 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6075           /* If we need a secondary register for this operation, see if
6076              the value is already in a register in that class.  Don't
6077              do this if the secondary register will be used as a scratch
6078              register.  */
6079
6080           if (reload_secondary_in_reload[j] >= 0
6081               && reload_secondary_in_icode[j] == CODE_FOR_nothing
6082               && optimize)
6083             oldequiv
6084               = find_equiv_reg (old, insn,
6085                                 reload_reg_class[reload_secondary_in_reload[j]],
6086                                 -1, NULL_PTR, 0, mode);
6087 #endif
6088
6089           /* If reloading from memory, see if there is a register
6090              that already holds the same value.  If so, reload from there.
6091              We can pass 0 as the reload_reg_p argument because
6092              any other reload has either already been emitted,
6093              in which case find_equiv_reg will see the reload-insn,
6094              or has yet to be emitted, in which case it doesn't matter
6095              because we will use this equiv reg right away.  */
6096
6097           if (oldequiv == 0 && optimize
6098               && (GET_CODE (old) == MEM
6099                   || (GET_CODE (old) == REG
6100                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
6101                       && reg_renumber[REGNO (old)] < 0)))
6102             oldequiv = find_equiv_reg (old, insn, ALL_REGS,
6103                                        -1, NULL_PTR, 0, mode);
6104
6105           if (oldequiv)
6106             {
6107               int regno = true_regnum (oldequiv);
6108
6109               /* If OLDEQUIV is a spill register, don't use it for this
6110                  if any other reload needs it at an earlier stage of this insn
6111                  or at this stage.  */
6112               if (spill_reg_order[regno] >= 0
6113                   && (! reload_reg_free_p (regno, reload_opnum[j],
6114                                            reload_when_needed[j])
6115                       || ! reload_reg_free_before_p (regno, reload_opnum[j],
6116                                                      reload_when_needed[j])))
6117                 oldequiv = 0;
6118
6119               /* If OLDEQUIV is not a spill register,
6120                  don't use it if any other reload wants it.  */
6121               if (spill_reg_order[regno] < 0)
6122                 {
6123                   int k;
6124                   for (k = 0; k < n_reloads; k++)
6125                     if (reload_reg_rtx[k] != 0 && k != j
6126                         && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
6127                                                                oldequiv))
6128                       {
6129                         oldequiv = 0;
6130                         break;
6131                       }
6132                 }
6133
6134               /* If it is no cheaper to copy from OLDEQUIV into the
6135                  reload register than it would be to move from memory,
6136                  don't use it. Likewise, if we need a secondary register
6137                  or memory.   */
6138
6139               if (oldequiv != 0
6140                   && ((REGNO_REG_CLASS (regno) != reload_reg_class[j]
6141                        && (REGISTER_MOVE_COST (REGNO_REG_CLASS (regno),
6142                                                reload_reg_class[j])
6143                            >= MEMORY_MOVE_COST (mode)))
6144 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6145                       || (SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
6146                                                         mode, oldequiv)
6147                           != NO_REGS)
6148 #endif
6149 #ifdef SECONDARY_MEMORY_NEEDED
6150                       || SECONDARY_MEMORY_NEEDED (reload_reg_class[j],
6151                                                   REGNO_REG_CLASS (regno),
6152                                                   mode)
6153 #endif
6154                       ))
6155                 oldequiv = 0;
6156             }
6157
6158           if (oldequiv == 0)
6159             oldequiv = old;
6160           else if (GET_CODE (oldequiv) == REG)
6161             oldequiv_reg = oldequiv;
6162           else if (GET_CODE (oldequiv) == SUBREG)
6163             oldequiv_reg = SUBREG_REG (oldequiv);
6164
6165           /* If we are reloading from a register that was recently stored in
6166              with an output-reload, see if we can prove there was
6167              actually no need to store the old value in it.  */
6168
6169           if (optimize && GET_CODE (oldequiv) == REG
6170               && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6171               && spill_reg_order[REGNO (oldequiv)] >= 0
6172               && spill_reg_store[spill_reg_order[REGNO (oldequiv)]] != 0
6173               && find_reg_note (insn, REG_DEAD, reload_in[j])
6174               /* This is unsafe if operand occurs more than once in current
6175                  insn.  Perhaps some occurrences weren't reloaded.  */
6176               && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
6177             delete_output_reload
6178               (insn, j, spill_reg_store[spill_reg_order[REGNO (oldequiv)]]);
6179
6180           /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6181              then load RELOADREG from OLDEQUIV.  Note that we cannot use
6182              gen_lowpart_common since it can do the wrong thing when
6183              RELOADREG has a multi-word mode.  Note that RELOADREG
6184              must always be a REG here.  */
6185
6186           if (GET_MODE (reloadreg) != mode)
6187             reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
6188           while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6189             oldequiv = SUBREG_REG (oldequiv);
6190           if (GET_MODE (oldequiv) != VOIDmode
6191               && mode != GET_MODE (oldequiv))
6192             oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
6193
6194           /* Switch to the right place to emit the reload insns.  */
6195           switch (reload_when_needed[j])
6196             {
6197             case RELOAD_OTHER:
6198               where = &other_input_reload_insns;
6199               break;
6200             case RELOAD_FOR_INPUT:
6201               where = &input_reload_insns[reload_opnum[j]];
6202               break;
6203             case RELOAD_FOR_INPUT_ADDRESS:
6204               where = &input_address_reload_insns[reload_opnum[j]];
6205               break;
6206             case RELOAD_FOR_INPADDR_ADDRESS:
6207               where = &inpaddr_address_reload_insns[reload_opnum[j]];
6208               break;
6209             case RELOAD_FOR_OUTPUT_ADDRESS:
6210               where = &output_address_reload_insns[reload_opnum[j]];
6211               break;
6212             case RELOAD_FOR_OUTADDR_ADDRESS:
6213               where = &outaddr_address_reload_insns[reload_opnum[j]];
6214               break;
6215             case RELOAD_FOR_OPERAND_ADDRESS:
6216               where = &operand_reload_insns;
6217               break;
6218             case RELOAD_FOR_OPADDR_ADDR:
6219               where = &other_operand_reload_insns;
6220               break;
6221             case RELOAD_FOR_OTHER_ADDRESS:
6222               where = &other_input_address_reload_insns;
6223               break;
6224             default:
6225               abort ();
6226             }
6227
6228           push_to_sequence (*where);
6229           special = 0;
6230
6231           /* Auto-increment addresses must be reloaded in a special way.  */
6232           if (GET_CODE (oldequiv) == POST_INC
6233               || GET_CODE (oldequiv) == POST_DEC
6234               || GET_CODE (oldequiv) == PRE_INC
6235               || GET_CODE (oldequiv) == PRE_DEC)
6236             {
6237               /* We are not going to bother supporting the case where a
6238                  incremented register can't be copied directly from
6239                  OLDEQUIV since this seems highly unlikely.  */
6240               if (reload_secondary_in_reload[j] >= 0)
6241                 abort ();
6242               /* Prevent normal processing of this reload.  */
6243               special = 1;
6244               /* Output a special code sequence for this case.  */
6245               inc_for_reload (reloadreg, oldequiv, reload_inc[j]);
6246             }
6247
6248           /* If we are reloading a pseudo-register that was set by the previous
6249              insn, see if we can get rid of that pseudo-register entirely
6250              by redirecting the previous insn into our reload register.  */
6251
6252           else if (optimize && GET_CODE (old) == REG
6253                    && REGNO (old) >= FIRST_PSEUDO_REGISTER
6254                    && dead_or_set_p (insn, old)
6255                    /* This is unsafe if some other reload
6256                       uses the same reg first.  */
6257                    && reload_reg_free_before_p (REGNO (reloadreg),
6258                                                 reload_opnum[j],
6259                                                 reload_when_needed[j]))
6260             {
6261               rtx temp = PREV_INSN (insn);
6262               while (temp && GET_CODE (temp) == NOTE)
6263                 temp = PREV_INSN (temp);
6264               if (temp
6265                   && GET_CODE (temp) == INSN
6266                   && GET_CODE (PATTERN (temp)) == SET
6267                   && SET_DEST (PATTERN (temp)) == old
6268                   /* Make sure we can access insn_operand_constraint.  */
6269                   && asm_noperands (PATTERN (temp)) < 0
6270                   /* This is unsafe if prev insn rejects our reload reg.  */
6271                   && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
6272                                                reloadreg)
6273                   /* This is unsafe if operand occurs more than once in current
6274                      insn.  Perhaps some occurrences aren't reloaded.  */
6275                   && count_occurrences (PATTERN (insn), old) == 1
6276                   /* Don't risk splitting a matching pair of operands.  */
6277                   && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
6278                 {
6279                   /* Store into the reload register instead of the pseudo.  */
6280                   SET_DEST (PATTERN (temp)) = reloadreg;
6281                   /* If these are the only uses of the pseudo reg,
6282                      pretend for GDB it lives in the reload reg we used.  */
6283                   if (REG_N_DEATHS (REGNO (old)) == 1
6284                       && REG_N_SETS (REGNO (old)) == 1)
6285                     {
6286                       reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
6287                       alter_reg (REGNO (old), -1);
6288                     }
6289                   special = 1;
6290                 }
6291             }
6292
6293           /* We can't do that, so output an insn to load RELOADREG.  */
6294
6295           if (! special)
6296             {
6297 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6298               rtx second_reload_reg = 0;
6299               enum insn_code icode;
6300
6301               /* If we have a secondary reload, pick up the secondary register
6302                  and icode, if any.  If OLDEQUIV and OLD are different or
6303                  if this is an in-out reload, recompute whether or not we
6304                  still need a secondary register and what the icode should
6305                  be.  If we still need a secondary register and the class or
6306                  icode is different, go back to reloading from OLD if using
6307                  OLDEQUIV means that we got the wrong type of register.  We
6308                  cannot have different class or icode due to an in-out reload
6309                  because we don't make such reloads when both the input and
6310                  output need secondary reload registers.  */
6311
6312               if (reload_secondary_in_reload[j] >= 0)
6313                 {
6314                   int secondary_reload = reload_secondary_in_reload[j];
6315                   rtx real_oldequiv = oldequiv;
6316                   rtx real_old = old;
6317
6318                   /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6319                      and similarly for OLD.
6320                      See comments in get_secondary_reload in reload.c.  */
6321                   if (GET_CODE (oldequiv) == REG
6322                       && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6323                       && reg_equiv_mem[REGNO (oldequiv)] != 0)
6324                     real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
6325
6326                   if (GET_CODE (old) == REG
6327                       && REGNO (old) >= FIRST_PSEUDO_REGISTER
6328                       && reg_equiv_mem[REGNO (old)] != 0)
6329                     real_old = reg_equiv_mem[REGNO (old)];
6330
6331                   second_reload_reg = reload_reg_rtx[secondary_reload];
6332                   icode = reload_secondary_in_icode[j];
6333
6334                   if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6335                       || (reload_in[j] != 0 && reload_out[j] != 0))
6336                     {
6337                       enum reg_class new_class
6338                         = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
6339                                                         mode, real_oldequiv);
6340
6341                       if (new_class == NO_REGS)
6342                         second_reload_reg = 0;
6343                       else
6344                         {
6345                           enum insn_code new_icode;
6346                           enum machine_mode new_mode;
6347
6348                           if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6349                                                    REGNO (second_reload_reg)))
6350                             oldequiv = old, real_oldequiv = real_old;
6351                           else
6352                             {
6353                               new_icode = reload_in_optab[(int) mode];
6354                               if (new_icode != CODE_FOR_nothing
6355                                   && ((insn_operand_predicate[(int) new_icode][0]
6356                                        && ! ((*insn_operand_predicate[(int) new_icode][0])
6357                                              (reloadreg, mode)))
6358                                       || (insn_operand_predicate[(int) new_icode][1]
6359                                           && ! ((*insn_operand_predicate[(int) new_icode][1])
6360                                                 (real_oldequiv, mode)))))
6361                                 new_icode = CODE_FOR_nothing;
6362
6363                               if (new_icode == CODE_FOR_nothing)
6364                                 new_mode = mode;
6365                               else
6366                                 new_mode = insn_operand_mode[(int) new_icode][2];
6367
6368                               if (GET_MODE (second_reload_reg) != new_mode)
6369                                 {
6370                                   if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6371                                                            new_mode))
6372                                     oldequiv = old, real_oldequiv = real_old;
6373                                   else
6374                                     second_reload_reg
6375                                       = gen_rtx (REG, new_mode,
6376                                                  REGNO (second_reload_reg));
6377                                 }
6378                             }
6379                         }
6380                     }
6381
6382                   /* If we still need a secondary reload register, check
6383                      to see if it is being used as a scratch or intermediate
6384                      register and generate code appropriately.  If we need
6385                      a scratch register, use REAL_OLDEQUIV since the form of
6386                      the insn may depend on the actual address if it is 
6387                      a MEM.  */
6388
6389                   if (second_reload_reg)
6390                     {
6391                       if (icode != CODE_FOR_nothing)
6392                         {
6393                           emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6394                                                       second_reload_reg));
6395                           special = 1;
6396                         }
6397                       else
6398                         {
6399                           /* See if we need a scratch register to load the
6400                              intermediate register (a tertiary reload).  */
6401                           enum insn_code tertiary_icode
6402                             = reload_secondary_in_icode[secondary_reload];
6403
6404                           if (tertiary_icode != CODE_FOR_nothing)
6405                             {
6406                               rtx third_reload_reg
6407                                 = reload_reg_rtx[reload_secondary_in_reload[secondary_reload]];
6408
6409                               emit_insn ((GEN_FCN (tertiary_icode)
6410                                           (second_reload_reg, real_oldequiv,
6411                                            third_reload_reg)));
6412                             }
6413                           else
6414                             gen_reload (second_reload_reg, oldequiv,
6415                                         reload_opnum[j],
6416                                         reload_when_needed[j]);
6417
6418                           oldequiv = second_reload_reg;
6419                         }
6420                     }
6421                 }
6422 #endif
6423
6424               if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6425                 gen_reload (reloadreg, oldequiv, reload_opnum[j],
6426                             reload_when_needed[j]);
6427
6428 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
6429               /* We may have to make a REG_DEAD note for the secondary reload
6430                  register in the insns we just made.  Find the last insn that
6431                  mentioned the register.  */
6432               if (! special && second_reload_reg
6433                   && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
6434                 {
6435                   rtx prev;
6436
6437                   for (prev = get_last_insn (); prev;
6438                        prev = PREV_INSN (prev))
6439                     if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
6440                         && reg_overlap_mentioned_for_reload_p (second_reload_reg,
6441                                                                PATTERN (prev)))
6442                       {
6443                         REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
6444                                                     second_reload_reg,
6445                                                     REG_NOTES (prev));
6446                         break;
6447                       }
6448                 }
6449 #endif
6450             }
6451
6452           this_reload_insn = get_last_insn ();
6453           /* End this sequence.  */
6454           *where = get_insns ();
6455           end_sequence ();
6456         }
6457
6458       /* Add a note saying the input reload reg
6459          dies in this insn, if anyone cares.  */
6460 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6461       if (old != 0
6462           && reload_reg_rtx[j] != old
6463           && reload_reg_rtx[j] != 0
6464           && reload_out[j] == 0
6465           && ! reload_inherited[j]
6466           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
6467         {
6468           register rtx reloadreg = reload_reg_rtx[j];
6469
6470 #if 0
6471           /* We can't abort here because we need to support this for sched.c.
6472              It's not terrible to miss a REG_DEAD note, but we should try
6473              to figure out how to do this correctly.  */
6474           /* The code below is incorrect for address-only reloads.  */
6475           if (reload_when_needed[j] != RELOAD_OTHER
6476               && reload_when_needed[j] != RELOAD_FOR_INPUT)
6477             abort ();
6478 #endif
6479
6480           /* Add a death note to this insn, for an input reload.  */
6481
6482           if ((reload_when_needed[j] == RELOAD_OTHER
6483                || reload_when_needed[j] == RELOAD_FOR_INPUT)
6484               && ! dead_or_set_p (insn, reloadreg))
6485             REG_NOTES (insn)
6486               = gen_rtx (EXPR_LIST, REG_DEAD,
6487                          reloadreg, REG_NOTES (insn));
6488         }
6489
6490       /* When we inherit a reload, the last marked death of the reload reg
6491          may no longer really be a death.  */
6492       if (reload_reg_rtx[j] != 0
6493           && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
6494           && reload_inherited[j])
6495         {
6496           /* Handle inheriting an output reload.
6497              Remove the death note from the output reload insn.  */
6498           if (reload_spill_index[j] >= 0
6499               && GET_CODE (reload_in[j]) == REG
6500               && spill_reg_store[reload_spill_index[j]] != 0
6501               && find_regno_note (spill_reg_store[reload_spill_index[j]],
6502                                   REG_DEAD, REGNO (reload_reg_rtx[j])))
6503             remove_death (REGNO (reload_reg_rtx[j]),
6504                           spill_reg_store[reload_spill_index[j]]);
6505           /* Likewise for input reloads that were inherited.  */
6506           else if (reload_spill_index[j] >= 0
6507                    && GET_CODE (reload_in[j]) == REG
6508                    && spill_reg_store[reload_spill_index[j]] == 0
6509                    && reload_inheritance_insn[j] != 0
6510                    && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
6511                                        REGNO (reload_reg_rtx[j])))
6512             remove_death (REGNO (reload_reg_rtx[j]),
6513                           reload_inheritance_insn[j]);
6514           else
6515             {
6516               rtx prev;
6517
6518               /* We got this register from find_equiv_reg.
6519                  Search back for its last death note and get rid of it.
6520                  But don't search back too far.
6521                  Don't go past a place where this reg is set,
6522                  since a death note before that remains valid.  */
6523               for (prev = PREV_INSN (insn);
6524                    prev && GET_CODE (prev) != CODE_LABEL;
6525                    prev = PREV_INSN (prev))
6526                 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6527                     && dead_or_set_p (prev, reload_reg_rtx[j]))
6528                   {
6529                     if (find_regno_note (prev, REG_DEAD,
6530                                          REGNO (reload_reg_rtx[j])))
6531                       remove_death (REGNO (reload_reg_rtx[j]), prev);
6532                     break;
6533                   }
6534             }
6535         }
6536
6537       /* We might have used find_equiv_reg above to choose an alternate
6538          place from which to reload.  If so, and it died, we need to remove
6539          that death and move it to one of the insns we just made.  */
6540
6541       if (oldequiv_reg != 0
6542           && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
6543         {
6544           rtx prev, prev1;
6545
6546           for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
6547                prev = PREV_INSN (prev))
6548             if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
6549                 && dead_or_set_p (prev, oldequiv_reg))
6550               {
6551                 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
6552                   {
6553                     for (prev1 = this_reload_insn;
6554                          prev1; prev1 = PREV_INSN (prev1))
6555                       if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
6556                         && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
6557                                                                PATTERN (prev1)))
6558                       {
6559                         REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
6560                                                      oldequiv_reg,
6561                                                      REG_NOTES (prev1));
6562                         break;
6563                       }
6564                     remove_death (REGNO (oldequiv_reg), prev);
6565                   }
6566                 break;
6567               }
6568         }
6569 #endif
6570
6571       /* If we are reloading a register that was recently stored in with an
6572          output-reload, see if we can prove there was
6573          actually no need to store the old value in it.  */
6574
6575       if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
6576           && reload_in[j] != 0
6577           && GET_CODE (reload_in[j]) == REG
6578 #if 0
6579           /* There doesn't seem to be any reason to restrict this to pseudos
6580              and doing so loses in the case where we are copying from a
6581              register of the wrong class.  */
6582           && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
6583 #endif
6584           && spill_reg_store[reload_spill_index[j]] != 0
6585           /* This is unsafe if some other reload uses the same reg first.  */
6586           && reload_reg_free_before_p (spill_regs[reload_spill_index[j]],
6587                                        reload_opnum[j], reload_when_needed[j])
6588           && dead_or_set_p (insn, reload_in[j])
6589           /* This is unsafe if operand occurs more than once in current
6590              insn.  Perhaps some occurrences weren't reloaded.  */
6591           && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
6592         delete_output_reload (insn, j,
6593                               spill_reg_store[reload_spill_index[j]]);
6594
6595       /* Input-reloading is done.  Now do output-reloading,
6596          storing the value from the reload-register after the main insn
6597          if reload_out[j] is nonzero.
6598
6599          ??? At some point we need to support handling output reloads of
6600          JUMP_INSNs or insns that set cc0.  */
6601       old = reload_out[j];
6602       if (old != 0
6603           && reload_reg_rtx[j] != old
6604           && reload_reg_rtx[j] != 0)
6605         {
6606           register rtx reloadreg = reload_reg_rtx[j];
6607           register rtx second_reloadreg = 0;
6608           rtx note, p;
6609           enum machine_mode mode;
6610           int special = 0;
6611
6612           /* An output operand that dies right away does need a reload,
6613              but need not be copied from it.  Show the new location in the
6614              REG_UNUSED note.  */
6615           if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6616               && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6617             {
6618               XEXP (note, 0) = reload_reg_rtx[j];
6619               continue;
6620             }
6621           /* Likewise for a SUBREG of an operand that dies.  */
6622           else if (GET_CODE (old) == SUBREG
6623                    && GET_CODE (SUBREG_REG (old)) == REG
6624                    && 0 != (note = find_reg_note (insn, REG_UNUSED,
6625                                                   SUBREG_REG (old))))
6626             {
6627               XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6628                                                    reload_reg_rtx[j]);
6629               continue;
6630             }
6631           else if (GET_CODE (old) == SCRATCH)
6632             /* If we aren't optimizing, there won't be a REG_UNUSED note,
6633                but we don't want to make an output reload.  */
6634             continue;
6635
6636 #if 0
6637           /* Strip off of OLD any size-increasing SUBREGs such as
6638              (SUBREG:SI foo:QI 0).  */
6639
6640           while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
6641                  && (GET_MODE_SIZE (GET_MODE (old))
6642                      > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
6643             old = SUBREG_REG (old);
6644 #endif
6645
6646           /* If is a JUMP_INSN, we can't support output reloads yet.  */
6647           if (GET_CODE (insn) == JUMP_INSN)
6648             abort ();
6649
6650           if (reload_when_needed[j] == RELOAD_OTHER)
6651             start_sequence ();
6652           else
6653             push_to_sequence (output_reload_insns[reload_opnum[j]]);
6654
6655           /* Determine the mode to reload in.
6656              See comments above (for input reloading).  */
6657
6658           mode = GET_MODE (old);
6659           if (mode == VOIDmode)
6660             {
6661               /* VOIDmode should never happen for an output.  */
6662               if (asm_noperands (PATTERN (insn)) < 0)
6663                 /* It's the compiler's fault.  */
6664                 fatal_insn ("VOIDmode on an output", insn);
6665               error_for_asm (insn, "output operand is constant in `asm'");
6666               /* Prevent crash--use something we know is valid.  */
6667               mode = word_mode;
6668               old = gen_rtx (REG, mode, REGNO (reloadreg));
6669             }
6670
6671           if (GET_MODE (reloadreg) != mode)
6672             reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
6673
6674 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6675
6676           /* If we need two reload regs, set RELOADREG to the intermediate
6677              one, since it will be stored into OLD.  We might need a secondary
6678              register only for an input reload, so check again here.  */
6679
6680           if (reload_secondary_out_reload[j] >= 0)
6681             {
6682               rtx real_old = old;
6683
6684               if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6685                   && reg_equiv_mem[REGNO (old)] != 0)
6686                 real_old = reg_equiv_mem[REGNO (old)];
6687
6688               if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
6689                                                  mode, real_old)
6690                   != NO_REGS))
6691                 {
6692                   second_reloadreg = reloadreg;
6693                   reloadreg = reload_reg_rtx[reload_secondary_out_reload[j]];
6694
6695                   /* See if RELOADREG is to be used as a scratch register
6696                      or as an intermediate register.  */
6697                   if (reload_secondary_out_icode[j] != CODE_FOR_nothing)
6698                     {
6699                       emit_insn ((GEN_FCN (reload_secondary_out_icode[j])
6700                                   (real_old, second_reloadreg, reloadreg)));
6701                       special = 1;
6702                     }
6703                   else
6704                     {
6705                       /* See if we need both a scratch and intermediate reload
6706                          register.  */
6707
6708                       int secondary_reload = reload_secondary_out_reload[j];
6709                       enum insn_code tertiary_icode
6710                         = reload_secondary_out_icode[secondary_reload];
6711
6712                       if (GET_MODE (reloadreg) != mode)
6713                         reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
6714
6715                       if (tertiary_icode != CODE_FOR_nothing)
6716                         {
6717                           rtx third_reloadreg
6718                             = reload_reg_rtx[reload_secondary_out_reload[secondary_reload]];
6719                           rtx tem;
6720
6721                           /* Copy primary reload reg to secondary reload reg.
6722                              (Note that these have been swapped above, then
6723                              secondary reload reg to OLD using our insn.  */
6724
6725                           /* If REAL_OLD is a paradoxical SUBREG, remove it
6726                              and try to put the opposite SUBREG on
6727                              RELOADREG.  */
6728                           if (GET_CODE (real_old) == SUBREG
6729                               && (GET_MODE_SIZE (GET_MODE (real_old))
6730                                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6731                               && 0 != (tem = gen_lowpart_common
6732                                        (GET_MODE (SUBREG_REG (real_old)),
6733                                         reloadreg)))
6734                             real_old = SUBREG_REG (real_old), reloadreg = tem;
6735
6736                           gen_reload (reloadreg, second_reloadreg,
6737                                       reload_opnum[j], reload_when_needed[j]);
6738                           emit_insn ((GEN_FCN (tertiary_icode)
6739                                       (real_old, reloadreg, third_reloadreg)));
6740                           special = 1;
6741                         }
6742
6743                       else
6744                         /* Copy between the reload regs here and then to
6745                            OUT later.  */
6746
6747                         gen_reload (reloadreg, second_reloadreg,
6748                                     reload_opnum[j], reload_when_needed[j]);
6749                     }
6750                 }
6751             }
6752 #endif
6753
6754           /* Output the last reload insn.  */
6755           if (! special)
6756             gen_reload (old, reloadreg, reload_opnum[j],
6757                         reload_when_needed[j]);
6758
6759 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6760           /* If final will look at death notes for this reg,
6761              put one on the last output-reload insn to use it.  Similarly
6762              for any secondary register.  */
6763           if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
6764             for (p = get_last_insn (); p; p = PREV_INSN (p))
6765               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6766                   && reg_overlap_mentioned_for_reload_p (reloadreg,
6767                                                          PATTERN (p)))
6768                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
6769                                          reloadreg, REG_NOTES (p));
6770
6771 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6772           if (! special && second_reloadreg
6773               && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
6774             for (p = get_last_insn (); p; p = PREV_INSN (p))
6775               if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6776                   && reg_overlap_mentioned_for_reload_p (second_reloadreg,
6777                                                          PATTERN (p)))
6778                 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
6779                                          second_reloadreg, REG_NOTES (p));
6780 #endif
6781 #endif
6782           /* Look at all insns we emitted, just to be safe.  */
6783           for (p = get_insns (); p; p = NEXT_INSN (p))
6784             if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6785               {
6786                 /* If this output reload doesn't come from a spill reg,
6787                    clear any memory of reloaded copies of the pseudo reg.
6788                    If this output reload comes from a spill reg,
6789                    reg_has_output_reload will make this do nothing.  */
6790                 note_stores (PATTERN (p), forget_old_reloads_1);
6791
6792                 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p))
6793                     && reload_spill_index[j] >= 0)
6794                   new_spill_reg_store[reload_spill_index[j]] = p;
6795               }
6796
6797           if (reload_when_needed[j] == RELOAD_OTHER)
6798             {
6799               emit_insns (other_output_reload_insns[reload_opnum[j]]);
6800               other_output_reload_insns[reload_opnum[j]] = get_insns ();
6801             }
6802           else
6803             output_reload_insns[reload_opnum[j]] = get_insns ();
6804
6805           end_sequence ();
6806         }
6807     }
6808
6809   /* Now write all the insns we made for reloads in the order expected by
6810      the allocation functions.  Prior to the insn being reloaded, we write
6811      the following reloads:
6812
6813      RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
6814
6815      RELOAD_OTHER reloads.
6816
6817      For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
6818      by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
6819      RELOAD_FOR_INPUT reload for the operand.
6820
6821      RELOAD_FOR_OPADDR_ADDRS reloads.
6822
6823      RELOAD_FOR_OPERAND_ADDRESS reloads.
6824
6825      After the insn being reloaded, we write the following:
6826
6827      For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
6828      by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
6829      RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
6830      reloads for the operand.  The RELOAD_OTHER output reloads are
6831      output in descending order by reload number.  */
6832
6833   emit_insns_before (other_input_address_reload_insns, before_insn);
6834   emit_insns_before (other_input_reload_insns, before_insn);
6835
6836   for (j = 0; j < reload_n_operands; j++)
6837     {
6838       emit_insns_before (inpaddr_address_reload_insns[j], before_insn);
6839       emit_insns_before (input_address_reload_insns[j], before_insn);
6840       emit_insns_before (input_reload_insns[j], before_insn);
6841     }
6842
6843   emit_insns_before (other_operand_reload_insns, before_insn);
6844   emit_insns_before (operand_reload_insns, before_insn);
6845
6846   for (j = 0; j < reload_n_operands; j++)
6847     {
6848       emit_insns_before (outaddr_address_reload_insns[j], following_insn);
6849       emit_insns_before (output_address_reload_insns[j], following_insn);
6850       emit_insns_before (output_reload_insns[j], following_insn);
6851       emit_insns_before (other_output_reload_insns[j], following_insn);
6852     }
6853
6854   /* Move death notes from INSN
6855      to output-operand-address and output reload insns.  */
6856 #ifdef PRESERVE_DEATH_INFO_REGNO_P
6857   {
6858     rtx insn1;
6859     /* Loop over those insns, last ones first.  */
6860     for (insn1 = PREV_INSN (following_insn); insn1 != insn;
6861          insn1 = PREV_INSN (insn1))
6862       if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
6863         {
6864           rtx source = SET_SRC (PATTERN (insn1));
6865           rtx dest = SET_DEST (PATTERN (insn1));
6866
6867           /* The note we will examine next.  */
6868           rtx reg_notes = REG_NOTES (insn);
6869           /* The place that pointed to this note.  */
6870           rtx *prev_reg_note = &REG_NOTES (insn);
6871
6872           /* If the note is for something used in the source of this
6873              reload insn, or in the output address, move the note.  */
6874           while (reg_notes)
6875             {
6876               rtx next_reg_notes = XEXP (reg_notes, 1);
6877               if (REG_NOTE_KIND (reg_notes) == REG_DEAD
6878                   && GET_CODE (XEXP (reg_notes, 0)) == REG
6879                   && ((GET_CODE (dest) != REG
6880                        && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6881                                                               dest))
6882                       || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
6883                                                              source)))
6884                 {
6885                   *prev_reg_note = next_reg_notes;
6886                   XEXP (reg_notes, 1) = REG_NOTES (insn1);
6887                   REG_NOTES (insn1) = reg_notes;
6888                 }
6889               else
6890                 prev_reg_note = &XEXP (reg_notes, 1);
6891
6892               reg_notes = next_reg_notes;
6893             }
6894         }
6895   }
6896 #endif
6897
6898   /* For all the spill regs newly reloaded in this instruction,
6899      record what they were reloaded from, so subsequent instructions
6900      can inherit the reloads.
6901
6902      Update spill_reg_store for the reloads of this insn.
6903      Copy the elements that were updated in the loop above.  */
6904
6905   for (j = 0; j < n_reloads; j++)
6906     {
6907       register int r = reload_order[j];
6908       register int i = reload_spill_index[r];
6909
6910       /* I is nonneg if this reload used one of the spill regs.
6911          If reload_reg_rtx[r] is 0, this is an optional reload
6912          that we opted to ignore.  */
6913
6914       if (i >= 0 && reload_reg_rtx[r] != 0)
6915         {
6916           int nr
6917             = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
6918           int k;
6919           int part_reaches_end = 0;
6920           int all_reaches_end = 1;
6921
6922           /* For a multi register reload, we need to check if all or part
6923              of the value lives to the end.  */
6924           for (k = 0; k < nr; k++)
6925             {
6926               if (reload_reg_reaches_end_p (spill_regs[i] + k, reload_opnum[r],
6927                                             reload_when_needed[r]))
6928                 part_reaches_end = 1;
6929               else
6930                 all_reaches_end = 0;
6931             }
6932
6933           /* Ignore reloads that don't reach the end of the insn in
6934              entirety.  */
6935           if (all_reaches_end)
6936             {
6937               /* First, clear out memory of what used to be in this spill reg.
6938                  If consecutive registers are used, clear them all.  */
6939
6940               for (k = 0; k < nr; k++)
6941                 {
6942                   reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
6943                   reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
6944                 }
6945
6946               /* Maybe the spill reg contains a copy of reload_out.  */
6947               if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
6948                 {
6949                   register int nregno = REGNO (reload_out[r]);
6950                   int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
6951                              : HARD_REGNO_NREGS (nregno,
6952                                                  GET_MODE (reload_reg_rtx[r])));
6953
6954                   spill_reg_store[i] = new_spill_reg_store[i];
6955                   reg_last_reload_reg[nregno] = reload_reg_rtx[r];
6956
6957                   /* If NREGNO is a hard register, it may occupy more than
6958                      one register.  If it does, say what is in the 
6959                      rest of the registers assuming that both registers
6960                      agree on how many words the object takes.  If not,
6961                      invalidate the subsequent registers.  */
6962
6963                   if (nregno < FIRST_PSEUDO_REGISTER)
6964                     for (k = 1; k < nnr; k++)
6965                       reg_last_reload_reg[nregno + k]
6966                         = (nr == nnr
6967                            ? gen_rtx (REG,
6968                                       reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
6969                                       REGNO (reload_reg_rtx[r]) + k)
6970                            : 0);
6971
6972                   /* Now do the inverse operation.  */
6973                   for (k = 0; k < nr; k++)
6974                     {
6975                       reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
6976                         = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
6977                            ? nregno
6978                            : nregno + k);
6979                       reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
6980                     }
6981                 }
6982
6983               /* Maybe the spill reg contains a copy of reload_in.  Only do
6984                  something if there will not be an output reload for
6985                  the register being reloaded.  */
6986               else if (reload_out[r] == 0
6987                        && reload_in[r] != 0
6988                        && ((GET_CODE (reload_in[r]) == REG
6989                             && ! reg_has_output_reload[REGNO (reload_in[r])])
6990                            || (GET_CODE (reload_in_reg[r]) == REG
6991                                && ! reg_has_output_reload[REGNO (reload_in_reg[r])])))
6992                 {
6993                   register int nregno;
6994                   int nnr;
6995
6996                   if (GET_CODE (reload_in[r]) == REG)
6997                     nregno = REGNO (reload_in[r]);
6998                   else
6999                     nregno = REGNO (reload_in_reg[r]);
7000
7001                   nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7002                          : HARD_REGNO_NREGS (nregno,
7003                                              GET_MODE (reload_reg_rtx[r])));
7004                   
7005                   reg_last_reload_reg[nregno] = reload_reg_rtx[r];
7006
7007                   if (nregno < FIRST_PSEUDO_REGISTER)
7008                     for (k = 1; k < nnr; k++)
7009                       reg_last_reload_reg[nregno + k]
7010                         = (nr == nnr
7011                            ? gen_rtx (REG,
7012                                       reg_raw_mode[REGNO (reload_reg_rtx[r]) + k],
7013                                       REGNO (reload_reg_rtx[r]) + k)
7014                            : 0);
7015
7016                   /* Unless we inherited this reload, show we haven't
7017                      recently done a store.  */
7018                   if (! reload_inherited[r])
7019                     spill_reg_store[i] = 0;
7020
7021                   for (k = 0; k < nr; k++)
7022                     {
7023                       reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
7024                         = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7025                            ? nregno
7026                            : nregno + k);
7027                       reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
7028                         = insn;
7029                     }
7030                 }
7031             }
7032
7033           /* However, if part of the reload reaches the end, then we must
7034              invalidate the old info for the part that survives to the end.  */
7035           else if (part_reaches_end)
7036             {
7037               for (k = 0; k < nr; k++)
7038                 if (reload_reg_reaches_end_p (spill_regs[i] + k,
7039                                               reload_opnum[r],
7040                                               reload_when_needed[r]))
7041                   {
7042                     reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
7043                     reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
7044                   }
7045             }
7046         }
7047
7048       /* The following if-statement was #if 0'd in 1.34 (or before...).
7049          It's reenabled in 1.35 because supposedly nothing else
7050          deals with this problem.  */
7051
7052       /* If a register gets output-reloaded from a non-spill register,
7053          that invalidates any previous reloaded copy of it.
7054          But forget_old_reloads_1 won't get to see it, because
7055          it thinks only about the original insn.  So invalidate it here.  */
7056       if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
7057         {
7058           register int nregno = REGNO (reload_out[r]);
7059           if (nregno >= FIRST_PSEUDO_REGISTER)
7060             reg_last_reload_reg[nregno] = 0;
7061           else
7062             {
7063               int num_regs = HARD_REGNO_NREGS (nregno,GET_MODE (reload_out[r]));
7064
7065               while (num_regs-- > 0)
7066                 reg_last_reload_reg[nregno + num_regs] = 0;
7067             }
7068         }
7069     }
7070 }
7071 \f
7072 /* Emit code to perform a reload from IN (which may be a reload register) to
7073    OUT (which may also be a reload register).  IN or OUT is from operand
7074    OPNUM with reload type TYPE. 
7075
7076    Returns first insn emitted.  */
7077
7078 rtx
7079 gen_reload (out, in, opnum, type)
7080      rtx out;
7081      rtx in;
7082      int opnum;
7083      enum reload_type type;
7084 {
7085   rtx last = get_last_insn ();
7086   rtx tem;
7087
7088   /* If IN is a paradoxical SUBREG, remove it and try to put the
7089      opposite SUBREG on OUT.  Likewise for a paradoxical SUBREG on OUT.  */
7090   if (GET_CODE (in) == SUBREG
7091       && (GET_MODE_SIZE (GET_MODE (in))
7092           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7093       && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7094     in = SUBREG_REG (in), out = tem;
7095   else if (GET_CODE (out) == SUBREG
7096       && (GET_MODE_SIZE (GET_MODE (out))
7097           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7098       && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7099     out = SUBREG_REG (out), in = tem;
7100
7101   /* How to do this reload can get quite tricky.  Normally, we are being
7102      asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7103      register that didn't get a hard register.  In that case we can just
7104      call emit_move_insn.
7105
7106      We can also be asked to reload a PLUS that adds a register or a MEM to
7107      another register, constant or MEM.  This can occur during frame pointer
7108      elimination and while reloading addresses.  This case is handled by
7109      trying to emit a single insn to perform the add.  If it is not valid,
7110      we use a two insn sequence.
7111
7112      Finally, we could be called to handle an 'o' constraint by putting
7113      an address into a register.  In that case, we first try to do this
7114      with a named pattern of "reload_load_address".  If no such pattern
7115      exists, we just emit a SET insn and hope for the best (it will normally
7116      be valid on machines that use 'o').
7117
7118      This entire process is made complex because reload will never
7119      process the insns we generate here and so we must ensure that
7120      they will fit their constraints and also by the fact that parts of
7121      IN might be being reloaded separately and replaced with spill registers.
7122      Because of this, we are, in some sense, just guessing the right approach
7123      here.  The one listed above seems to work.
7124
7125      ??? At some point, this whole thing needs to be rethought.  */
7126
7127   if (GET_CODE (in) == PLUS
7128       && (GET_CODE (XEXP (in, 0)) == REG
7129           || GET_CODE (XEXP (in, 0)) == SUBREG
7130           || GET_CODE (XEXP (in, 0)) == MEM)
7131       && (GET_CODE (XEXP (in, 1)) == REG
7132           || GET_CODE (XEXP (in, 1)) == SUBREG
7133           || CONSTANT_P (XEXP (in, 1))
7134           || GET_CODE (XEXP (in, 1)) == MEM))
7135     {
7136       /* We need to compute the sum of a register or a MEM and another
7137          register, constant, or MEM, and put it into the reload
7138          register.  The best possible way of doing this is if the machine
7139          has a three-operand ADD insn that accepts the required operands.
7140
7141          The simplest approach is to try to generate such an insn and see if it
7142          is recognized and matches its constraints.  If so, it can be used.
7143
7144          It might be better not to actually emit the insn unless it is valid,
7145          but we need to pass the insn as an operand to `recog' and
7146          `insn_extract' and it is simpler to emit and then delete the insn if
7147          not valid than to dummy things up.  */
7148
7149       rtx op0, op1, tem, insn;
7150       int code;
7151
7152       op0 = find_replacement (&XEXP (in, 0));
7153       op1 = find_replacement (&XEXP (in, 1));
7154
7155       /* Since constraint checking is strict, commutativity won't be
7156          checked, so we need to do that here to avoid spurious failure
7157          if the add instruction is two-address and the second operand
7158          of the add is the same as the reload reg, which is frequently
7159          the case.  If the insn would be A = B + A, rearrange it so
7160          it will be A = A + B as constrain_operands expects.  */
7161
7162       if (GET_CODE (XEXP (in, 1)) == REG
7163           && REGNO (out) == REGNO (XEXP (in, 1)))
7164         tem = op0, op0 = op1, op1 = tem;
7165
7166       if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7167         in = gen_rtx (PLUS, GET_MODE (in), op0, op1);
7168
7169       insn = emit_insn (gen_rtx (SET, VOIDmode, out, in));
7170       code = recog_memoized (insn);
7171
7172       if (code >= 0)
7173         {
7174           insn_extract (insn);
7175           /* We want constrain operands to treat this insn strictly in
7176              its validity determination, i.e., the way it would after reload
7177              has completed.  */
7178           if (constrain_operands (code, 1))
7179             return insn;
7180         }
7181
7182       delete_insns_since (last);
7183
7184       /* If that failed, we must use a conservative two-insn sequence.
7185          use move to copy constant, MEM, or pseudo register to the reload
7186          register since "move" will be able to handle an arbitrary operand,
7187          unlike add which can't, in general.  Then add the registers.
7188
7189          If there is another way to do this for a specific machine, a
7190          DEFINE_PEEPHOLE should be specified that recognizes the sequence
7191          we emit below.  */
7192
7193       if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7194           || (GET_CODE (op1) == REG
7195               && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
7196         tem = op0, op0 = op1, op1 = tem;
7197
7198       gen_reload (out, op0, opnum, type);
7199
7200       /* If OP0 and OP1 are the same, we can use OUT for OP1.
7201          This fixes a problem on the 32K where the stack pointer cannot
7202          be used as an operand of an add insn.  */
7203
7204       if (rtx_equal_p (op0, op1))
7205         op1 = out;
7206
7207       insn = emit_insn (gen_add2_insn (out, op1));
7208
7209       /* If that failed, copy the address register to the reload register.
7210          Then add the constant to the reload register.  */
7211
7212       code = recog_memoized (insn);
7213
7214       if (code >= 0)
7215         {
7216           insn_extract (insn);
7217           /* We want constrain operands to treat this insn strictly in
7218              its validity determination, i.e., the way it would after reload
7219              has completed.  */
7220           if (constrain_operands (code, 1))
7221             return insn;
7222         }
7223
7224       delete_insns_since (last);
7225
7226       gen_reload (out, op1, opnum, type);
7227       emit_insn (gen_add2_insn (out, op0));
7228     }
7229
7230 #ifdef SECONDARY_MEMORY_NEEDED
7231   /* If we need a memory location to do the move, do it that way.  */
7232   else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
7233            && GET_CODE (out) == REG && REGNO (out) < FIRST_PSEUDO_REGISTER
7234            && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
7235                                        REGNO_REG_CLASS (REGNO (out)),
7236                                        GET_MODE (out)))
7237     {
7238       /* Get the memory to use and rewrite both registers to its mode.  */
7239       rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7240
7241       if (GET_MODE (loc) != GET_MODE (out))
7242         out = gen_rtx (REG, GET_MODE (loc), REGNO (out));
7243
7244       if (GET_MODE (loc) != GET_MODE (in))
7245         in = gen_rtx (REG, GET_MODE (loc), REGNO (in));
7246
7247       gen_reload (loc, in, opnum, type);
7248       gen_reload (out, loc, opnum, type);
7249     }
7250 #endif
7251
7252   /* If IN is a simple operand, use gen_move_insn.  */
7253   else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7254     emit_insn (gen_move_insn (out, in));
7255
7256 #ifdef HAVE_reload_load_address
7257   else if (HAVE_reload_load_address)
7258     emit_insn (gen_reload_load_address (out, in));
7259 #endif
7260
7261   /* Otherwise, just write (set OUT IN) and hope for the best.  */
7262   else
7263     emit_insn (gen_rtx (SET, VOIDmode, out, in));
7264
7265   /* Return the first insn emitted.
7266      We can not just return get_last_insn, because there may have
7267      been multiple instructions emitted.  Also note that gen_move_insn may
7268      emit more than one insn itself, so we can not assume that there is one
7269      insn emitted per emit_insn_before call.  */
7270
7271   return last ? NEXT_INSN (last) : get_insns ();
7272 }
7273 \f
7274 /* Delete a previously made output-reload
7275    whose result we now believe is not needed.
7276    First we double-check.
7277
7278    INSN is the insn now being processed.
7279    OUTPUT_RELOAD_INSN is the insn of the output reload.
7280    J is the reload-number for this insn.  */
7281
7282 static void
7283 delete_output_reload (insn, j, output_reload_insn)
7284      rtx insn;
7285      int j;
7286      rtx output_reload_insn;
7287 {
7288   register rtx i1;
7289
7290   /* Get the raw pseudo-register referred to.  */
7291
7292   rtx reg = reload_in[j];
7293   while (GET_CODE (reg) == SUBREG)
7294     reg = SUBREG_REG (reg);
7295
7296   /* If the pseudo-reg we are reloading is no longer referenced
7297      anywhere between the store into it and here,
7298      and no jumps or labels intervene, then the value can get
7299      here through the reload reg alone.
7300      Otherwise, give up--return.  */
7301   for (i1 = NEXT_INSN (output_reload_insn);
7302        i1 != insn; i1 = NEXT_INSN (i1))
7303     {
7304       if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7305         return;
7306       if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7307           && reg_mentioned_p (reg, PATTERN (i1)))
7308         return;
7309     }
7310
7311   if (cannot_omit_stores[REGNO (reg)])
7312     return;
7313
7314   /* If this insn will store in the pseudo again,
7315      the previous store can be removed.  */
7316   if (reload_out[j] == reload_in[j])
7317     delete_insn (output_reload_insn);
7318
7319   /* See if the pseudo reg has been completely replaced
7320      with reload regs.  If so, delete the store insn
7321      and forget we had a stack slot for the pseudo.  */
7322   else if (REG_N_DEATHS (REGNO (reg)) == 1
7323            && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7324            && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7325     {
7326       rtx i2;
7327
7328       /* We know that it was used only between here
7329          and the beginning of the current basic block.
7330          (We also know that the last use before INSN was
7331          the output reload we are thinking of deleting, but never mind that.)
7332          Search that range; see if any ref remains.  */
7333       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7334         {
7335           rtx set = single_set (i2);
7336
7337           /* Uses which just store in the pseudo don't count,
7338              since if they are the only uses, they are dead.  */
7339           if (set != 0 && SET_DEST (set) == reg)
7340             continue;
7341           if (GET_CODE (i2) == CODE_LABEL
7342               || GET_CODE (i2) == JUMP_INSN)
7343             break;
7344           if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7345               && reg_mentioned_p (reg, PATTERN (i2)))
7346             /* Some other ref remains;
7347                we can't do anything.  */
7348             return;
7349         }
7350
7351       /* Delete the now-dead stores into this pseudo.  */
7352       for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7353         {
7354           rtx set = single_set (i2);
7355
7356           if (set != 0 && SET_DEST (set) == reg)
7357             {
7358               /* This might be a basic block head,
7359                  thus don't use delete_insn.  */
7360               PUT_CODE (i2, NOTE);
7361               NOTE_SOURCE_FILE (i2) = 0;
7362               NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
7363             }
7364           if (GET_CODE (i2) == CODE_LABEL
7365               || GET_CODE (i2) == JUMP_INSN)
7366             break;
7367         }
7368
7369       /* For the debugging info,
7370          say the pseudo lives in this reload reg.  */
7371       reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
7372       alter_reg (REGNO (reg), -1);
7373     }
7374 }
7375 \f
7376 /* Output reload-insns to reload VALUE into RELOADREG.
7377    VALUE is an autoincrement or autodecrement RTX whose operand
7378    is a register or memory location;
7379    so reloading involves incrementing that location.
7380
7381    INC_AMOUNT is the number to increment or decrement by (always positive).
7382    This cannot be deduced from VALUE.  */
7383
7384 static void
7385 inc_for_reload (reloadreg, value, inc_amount)
7386      rtx reloadreg;
7387      rtx value;
7388      int inc_amount;
7389 {
7390   /* REG or MEM to be copied and incremented.  */
7391   rtx incloc = XEXP (value, 0);
7392   /* Nonzero if increment after copying.  */
7393   int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7394   rtx last;
7395   rtx inc;
7396   rtx add_insn;
7397   int code;
7398
7399   /* No hard register is equivalent to this register after
7400      inc/dec operation.  If REG_LAST_RELOAD_REG were non-zero,
7401      we could inc/dec that register as well (maybe even using it for
7402      the source), but I'm not sure it's worth worrying about.  */
7403   if (GET_CODE (incloc) == REG)
7404     reg_last_reload_reg[REGNO (incloc)] = 0;
7405
7406   if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7407     inc_amount = - inc_amount;
7408
7409   inc = GEN_INT (inc_amount);
7410
7411   /* If this is post-increment, first copy the location to the reload reg.  */
7412   if (post)
7413     emit_insn (gen_move_insn (reloadreg, incloc));
7414
7415   /* See if we can directly increment INCLOC.  Use a method similar to that
7416      in gen_reload.  */
7417
7418   last = get_last_insn ();
7419   add_insn = emit_insn (gen_rtx (SET, VOIDmode, incloc,
7420                                  gen_rtx (PLUS, GET_MODE (incloc),
7421                                           incloc, inc)));
7422                                                           
7423   code = recog_memoized (add_insn);
7424   if (code >= 0)
7425     {
7426       insn_extract (add_insn);
7427       if (constrain_operands (code, 1))
7428         {
7429           /* If this is a pre-increment and we have incremented the value
7430              where it lives, copy the incremented value to RELOADREG to
7431              be used as an address.  */
7432
7433           if (! post)
7434             emit_insn (gen_move_insn (reloadreg, incloc));
7435
7436           return;
7437         }
7438     }
7439
7440   delete_insns_since (last);
7441
7442   /* If couldn't do the increment directly, must increment in RELOADREG.
7443      The way we do this depends on whether this is pre- or post-increment.
7444      For pre-increment, copy INCLOC to the reload register, increment it
7445      there, then save back.  */
7446
7447   if (! post)
7448     {
7449       emit_insn (gen_move_insn (reloadreg, incloc));
7450       emit_insn (gen_add2_insn (reloadreg, inc));
7451       emit_insn (gen_move_insn (incloc, reloadreg));
7452     }
7453   else
7454     {
7455       /* Postincrement.
7456          Because this might be a jump insn or a compare, and because RELOADREG
7457          may not be available after the insn in an input reload, we must do
7458          the incrementation before the insn being reloaded for.
7459
7460          We have already copied INCLOC to RELOADREG.  Increment the copy in
7461          RELOADREG, save that back, then decrement RELOADREG so it has
7462          the original value.  */
7463
7464       emit_insn (gen_add2_insn (reloadreg, inc));
7465       emit_insn (gen_move_insn (incloc, reloadreg));
7466       emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7467     }
7468
7469   return;
7470 }
7471 \f
7472 /* Return 1 if we are certain that the constraint-string STRING allows
7473    the hard register REG.  Return 0 if we can't be sure of this.  */
7474
7475 static int
7476 constraint_accepts_reg_p (string, reg)
7477      char *string;
7478      rtx reg;
7479 {
7480   int value = 0;
7481   int regno = true_regnum (reg);
7482   int c;
7483
7484   /* Initialize for first alternative.  */
7485   value = 0;
7486   /* Check that each alternative contains `g' or `r'.  */
7487   while (1)
7488     switch (c = *string++)
7489       {
7490       case 0:
7491         /* If an alternative lacks `g' or `r', we lose.  */
7492         return value;
7493       case ',':
7494         /* If an alternative lacks `g' or `r', we lose.  */
7495         if (value == 0)
7496           return 0;
7497         /* Initialize for next alternative.  */
7498         value = 0;
7499         break;
7500       case 'g':
7501       case 'r':
7502         /* Any general reg wins for this alternative.  */
7503         if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
7504           value = 1;
7505         break;
7506       default:
7507         /* Any reg in specified class wins for this alternative.  */
7508         {
7509           enum reg_class class = REG_CLASS_FROM_LETTER (c);
7510
7511           if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
7512             value = 1;
7513         }
7514       }
7515 }
7516 \f
7517 /* Return the number of places FIND appears within X, but don't count
7518    an occurrence if some SET_DEST is FIND.  */
7519
7520 static int
7521 count_occurrences (x, find)
7522      register rtx x, find;
7523 {
7524   register int i, j;
7525   register enum rtx_code code;
7526   register char *format_ptr;
7527   int count;
7528
7529   if (x == find)
7530     return 1;
7531   if (x == 0)
7532     return 0;
7533
7534   code = GET_CODE (x);
7535
7536   switch (code)
7537     {
7538     case REG:
7539     case QUEUED:
7540     case CONST_INT:
7541     case CONST_DOUBLE:
7542     case SYMBOL_REF:
7543     case CODE_LABEL:
7544     case PC:
7545     case CC0:
7546       return 0;
7547
7548     case SET:
7549       if (SET_DEST (x) == find)
7550         return count_occurrences (SET_SRC (x), find);
7551       break;
7552       
7553     default:
7554       break;
7555     }
7556
7557   format_ptr = GET_RTX_FORMAT (code);
7558   count = 0;
7559
7560   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7561     {
7562       switch (*format_ptr++)
7563         {
7564         case 'e':
7565           count += count_occurrences (XEXP (x, i), find);
7566           break;
7567
7568         case 'E':
7569           if (XVEC (x, i) != NULL)
7570             {
7571               for (j = 0; j < XVECLEN (x, i); j++)
7572                 count += count_occurrences (XVECEXP (x, i, j), find);
7573             }
7574           break;
7575         }
7576     }
7577   return count;
7578 }
7579 \f
7580 /* This array holds values which are equivalent to a hard register
7581    during reload_cse_regs.  Each array element is an EXPR_LIST of
7582    values.  Each time a hard register is set, we set the corresponding
7583    array element to the value.  Each time a hard register is copied
7584    into memory, we add the memory location to the corresponding array
7585    element.  We don't store values or memory addresses with side
7586    effects in this array.
7587
7588    If the value is a CONST_INT, then the mode of the containing
7589    EXPR_LIST is the mode in which that CONST_INT was referenced.
7590
7591    We sometimes clobber a specific entry in a list.  In that case, we
7592    just set XEXP (list-entry, 0) to 0.  */
7593
7594 static rtx *reg_values;
7595
7596 /* This is a preallocated REG rtx which we use as a temporary in
7597    reload_cse_invalidate_regno, so that we don't need to allocate a
7598    new one each time through a loop in that function.  */
7599
7600 static rtx invalidate_regno_rtx;
7601
7602 /* This is a set of registers for which we must remove REG_DEAD notes in
7603    previous insns, because our modifications made them invalid.  That can
7604    happen if we introduced the register into the current insn, or we deleted
7605    the current insn which used to set the register.  */
7606
7607 static HARD_REG_SET no_longer_dead_regs;
7608
7609 /* Invalidate any entries in reg_values which depend on REGNO,
7610    including those for REGNO itself.  This is called if REGNO is
7611    changing.  If CLOBBER is true, then always forget anything we
7612    currently know about REGNO.  MODE is the mode of the assignment to
7613    REGNO, which is used to determine how many hard registers are being
7614    changed.  If MODE is VOIDmode, then only REGNO is being changed;
7615    this is used when invalidating call clobbered registers across a
7616    call.  */
7617
7618 static void
7619 reload_cse_invalidate_regno (regno, mode, clobber)
7620      int regno;
7621      enum machine_mode mode;
7622      int clobber;
7623 {
7624   int endregno;
7625   register int i;
7626
7627   /* Our callers don't always go through true_regnum; we may see a
7628      pseudo-register here from a CLOBBER or the like.  We probably
7629      won't ever see a pseudo-register that has a real register number,
7630      for we check anyhow for safety.  */
7631   if (regno >= FIRST_PSEUDO_REGISTER)
7632     regno = reg_renumber[regno];
7633   if (regno < 0)
7634     return;
7635
7636   if (mode == VOIDmode)
7637     endregno = regno + 1;
7638   else
7639     endregno = regno + HARD_REGNO_NREGS (regno, mode);
7640
7641   if (clobber)
7642     for (i = regno; i < endregno; i++)
7643       reg_values[i] = 0;
7644
7645   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7646     {
7647       rtx x;
7648
7649       for (x = reg_values[i]; x; x = XEXP (x, 1))
7650         {
7651           if (XEXP (x, 0) != 0
7652               && refers_to_regno_p (regno, endregno, XEXP (x, 0), NULL_PTR))
7653             {
7654               /* If this is the only entry on the list, clear
7655                  reg_values[i].  Otherwise, just clear this entry on
7656                  the list.  */
7657               if (XEXP (x, 1) == 0 && x == reg_values[i])
7658                 {
7659                   reg_values[i] = 0;
7660                   break;
7661                 }
7662               XEXP (x, 0) = 0;
7663             }
7664         }
7665     }
7666
7667   /* We must look at earlier registers, in case REGNO is part of a
7668      multi word value but is not the first register.  If an earlier
7669      register has a value in a mode which overlaps REGNO, then we must
7670      invalidate that earlier register.  Note that we do not need to
7671      check REGNO or later registers (we must not check REGNO itself,
7672      because we would incorrectly conclude that there was a conflict).  */
7673
7674   for (i = 0; i < regno; i++)
7675     {
7676       rtx x;
7677
7678       for (x = reg_values[i]; x; x = XEXP (x, 1))
7679         {
7680           if (XEXP (x, 0) != 0)
7681             {
7682               PUT_MODE (invalidate_regno_rtx, GET_MODE (x));
7683               REGNO (invalidate_regno_rtx) = i;
7684               if (refers_to_regno_p (regno, endregno, invalidate_regno_rtx,
7685                                      NULL_PTR))
7686                 {
7687                   reload_cse_invalidate_regno (i, VOIDmode, 1);
7688                   break;
7689                 }
7690             }
7691         }
7692     }
7693 }
7694
7695 /* The memory at address MEM_BASE is being changed.
7696    Return whether this change will invalidate VAL.  */
7697
7698 static int
7699 reload_cse_mem_conflict_p (mem_base, val)
7700      rtx mem_base;
7701      rtx val;
7702 {
7703   enum rtx_code code;
7704   char *fmt;
7705   int i;
7706
7707   code = GET_CODE (val);
7708   switch (code)
7709     {
7710       /* Get rid of a few simple cases quickly. */
7711     case REG:
7712     case PC:
7713     case CC0:
7714     case SCRATCH:
7715     case CONST:
7716     case CONST_INT:
7717     case CONST_DOUBLE:
7718     case SYMBOL_REF:
7719     case LABEL_REF:
7720       return 0;
7721
7722     case MEM:
7723       if (GET_MODE (mem_base) == BLKmode
7724           || GET_MODE (val) == BLKmode)
7725         return 1;
7726       if (anti_dependence (val, mem_base))
7727         return 1;
7728       /* The address may contain nested MEMs.  */
7729       break;
7730
7731     default:
7732       break;
7733     }
7734
7735   fmt = GET_RTX_FORMAT (code);
7736
7737   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7738     {
7739       if (fmt[i] == 'e')
7740         {
7741           if (reload_cse_mem_conflict_p (mem_base, XEXP (val, i)))
7742             return 1;
7743         }
7744       else if (fmt[i] == 'E')
7745         {
7746           int j;
7747
7748           for (j = 0; j < XVECLEN (val, i); j++)
7749             if (reload_cse_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
7750               return 1;
7751         }
7752     }
7753
7754   return 0;
7755 }
7756
7757 /* Invalidate any entries in reg_values which are changed because of a
7758    store to MEM_RTX.  If this is called because of a non-const call
7759    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
7760
7761 static void
7762 reload_cse_invalidate_mem (mem_rtx)
7763      rtx mem_rtx;
7764 {
7765   register int i;
7766
7767   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7768     {
7769       rtx x;
7770
7771       for (x = reg_values[i]; x; x = XEXP (x, 1))
7772         {
7773           if (XEXP (x, 0) != 0
7774               && reload_cse_mem_conflict_p (mem_rtx, XEXP (x, 0)))
7775             {
7776               /* If this is the only entry on the list, clear
7777                  reg_values[i].  Otherwise, just clear this entry on
7778                  the list.  */
7779               if (XEXP (x, 1) == 0 && x == reg_values[i])
7780                 {
7781                   reg_values[i] = 0;
7782                   break;
7783                 }
7784               XEXP (x, 0) = 0;
7785             }
7786         }
7787     }
7788 }
7789
7790 /* Invalidate DEST, which is being assigned to or clobbered.  The
7791    second parameter exists so that this function can be passed to
7792    note_stores; it is ignored.  */
7793
7794 static void
7795 reload_cse_invalidate_rtx (dest, ignore)
7796      rtx dest;
7797      rtx ignore;
7798 {
7799   while (GET_CODE (dest) == STRICT_LOW_PART
7800          || GET_CODE (dest) == SIGN_EXTRACT
7801          || GET_CODE (dest) == ZERO_EXTRACT
7802          || GET_CODE (dest) == SUBREG)
7803     dest = XEXP (dest, 0);
7804
7805   if (GET_CODE (dest) == REG)
7806     reload_cse_invalidate_regno (REGNO (dest), GET_MODE (dest), 1);
7807   else if (GET_CODE (dest) == MEM)
7808     reload_cse_invalidate_mem (dest);
7809 }
7810
7811 /* Possibly delete death notes on the insns before INSN if modifying INSN
7812    extended the lifespan of the registers.  */
7813
7814 static void
7815 reload_cse_delete_death_notes (insn)
7816      rtx insn;
7817 {
7818   int dreg;
7819
7820   for (dreg = 0; dreg < FIRST_PSEUDO_REGISTER; dreg++)
7821     {
7822       rtx trial;
7823
7824       if (! TEST_HARD_REG_BIT (no_longer_dead_regs, dreg))
7825         continue;
7826
7827       for (trial = prev_nonnote_insn (insn);
7828            (trial
7829             && GET_CODE (trial) != CODE_LABEL
7830             && GET_CODE (trial) != BARRIER);
7831            trial = prev_nonnote_insn (trial))
7832         {
7833           if (find_regno_note (trial, REG_DEAD, dreg))
7834             {
7835               remove_death (dreg, trial);
7836               break;
7837             }
7838         }
7839     }
7840 }
7841
7842 /* Record that the current insn uses hard reg REGNO in mode MODE.  This
7843    will be used in reload_cse_delete_death_notes to delete prior REG_DEAD
7844    notes for this register.  */
7845
7846 static void
7847 reload_cse_no_longer_dead (regno, mode)
7848      int regno;
7849      enum machine_mode mode;
7850 {
7851   int nregs = HARD_REGNO_NREGS (regno, mode);
7852   while (nregs-- > 0)
7853     {
7854       SET_HARD_REG_BIT (no_longer_dead_regs, regno);
7855       regno++;
7856     }
7857 }
7858
7859
7860 /* Do a very simple CSE pass over the hard registers.
7861
7862    This function detects no-op moves where we happened to assign two
7863    different pseudo-registers to the same hard register, and then
7864    copied one to the other.  Reload will generate a useless
7865    instruction copying a register to itself.
7866
7867    This function also detects cases where we load a value from memory
7868    into two different registers, and (if memory is more expensive than
7869    registers) changes it to simply copy the first register into the
7870    second register.  
7871
7872    Another optimization is performed that scans the operands of each
7873    instruction to see whether the value is already available in a
7874    hard register.  It then replaces the operand with the hard register
7875    if possible, much like an optional reload would.  */
7876
7877 void
7878 reload_cse_regs (first)
7879      rtx first;
7880 {
7881   char *firstobj;
7882   rtx callmem;
7883   register int i;
7884   rtx insn;
7885
7886   init_alias_analysis ();
7887
7888   reg_values = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
7889   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7890     reg_values[i] = 0;
7891
7892   /* Create our EXPR_LIST structures on reload_obstack, so that we can
7893      free them when we are done.  */
7894   push_obstacks (&reload_obstack, &reload_obstack);
7895   firstobj = (char *) obstack_alloc (&reload_obstack, 0);
7896
7897   /* We pass this to reload_cse_invalidate_mem to invalidate all of
7898      memory for a non-const call instruction.  */
7899   callmem = gen_rtx (MEM, BLKmode, const0_rtx);
7900
7901   /* This is used in reload_cse_invalidate_regno to avoid consing a
7902      new REG in a loop in that function.  */
7903   invalidate_regno_rtx = gen_rtx (REG, VOIDmode, 0);
7904
7905   for (insn = first; insn; insn = NEXT_INSN (insn))
7906     {
7907       rtx body;
7908
7909       if (GET_CODE (insn) == CODE_LABEL)
7910         {
7911           /* Forget all the register values at a code label.  We don't
7912              try to do anything clever around jumps.  */
7913           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7914             reg_values[i] = 0;
7915
7916           continue;
7917         }
7918
7919 #ifdef NON_SAVING_SETJMP 
7920       if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
7921           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
7922         {
7923           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7924             reg_values[i] = 0;
7925
7926           continue;
7927         }
7928 #endif
7929
7930       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7931         continue;
7932
7933       CLEAR_HARD_REG_SET (no_longer_dead_regs);
7934
7935       /* If this is a call instruction, forget anything stored in a
7936          call clobbered register, or, if this is not a const call, in
7937          memory.  */
7938       if (GET_CODE (insn) == CALL_INSN)
7939         {
7940           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7941             if (call_used_regs[i])
7942               reload_cse_invalidate_regno (i, VOIDmode, 1);
7943
7944           if (! CONST_CALL_P (insn))
7945             reload_cse_invalidate_mem (callmem);
7946         }
7947
7948       body = PATTERN (insn);
7949       if (GET_CODE (body) == SET)
7950         {
7951           int count = 0;
7952           if (reload_cse_noop_set_p (body, insn))
7953             {
7954               PUT_CODE (insn, NOTE);
7955               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7956               NOTE_SOURCE_FILE (insn) = 0;
7957               reload_cse_delete_death_notes (insn);
7958
7959               /* We're done with this insn.  */
7960               continue;
7961             }
7962
7963           /* It's not a no-op, but we can try to simplify it.  */
7964           CLEAR_HARD_REG_SET (no_longer_dead_regs);
7965           count += reload_cse_simplify_set (body, insn);
7966
7967           if (count > 0 && apply_change_group ())
7968             reload_cse_delete_death_notes (insn);
7969           else if (reload_cse_simplify_operands (insn))
7970             reload_cse_delete_death_notes (insn);
7971             
7972           reload_cse_record_set (body, body);
7973         }
7974       else if (GET_CODE (body) == PARALLEL)
7975         {
7976           int count = 0;
7977
7978           /* If every action in a PARALLEL is a noop, we can delete
7979              the entire PARALLEL.  */
7980           for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
7981             if ((GET_CODE (XVECEXP (body, 0, i)) != SET
7982                  || ! reload_cse_noop_set_p (XVECEXP (body, 0, i), insn))
7983                 && GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
7984               break;
7985           if (i < 0)
7986             {
7987               PUT_CODE (insn, NOTE);
7988               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7989               NOTE_SOURCE_FILE (insn) = 0;
7990               reload_cse_delete_death_notes (insn);
7991
7992               /* We're done with this insn.  */
7993               continue;
7994             }
7995           
7996           /* It's not a no-op, but we can try to simplify it.  */
7997           CLEAR_HARD_REG_SET (no_longer_dead_regs);
7998           for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
7999             if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8000               count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8001
8002           if (count > 0 && apply_change_group ())
8003             reload_cse_delete_death_notes (insn);
8004           else if (reload_cse_simplify_operands (insn))
8005             reload_cse_delete_death_notes (insn);
8006
8007           /* Look through the PARALLEL and record the values being
8008              set, if possible.  Also handle any CLOBBERs.  */
8009           for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8010             {
8011               rtx x = XVECEXP (body, 0, i);
8012
8013               if (GET_CODE (x) == SET)
8014                 reload_cse_record_set (x, body);
8015               else
8016                 note_stores (x, reload_cse_invalidate_rtx);
8017             }
8018         }
8019       else
8020         note_stores (body, reload_cse_invalidate_rtx);
8021
8022 #ifdef AUTO_INC_DEC
8023       /* Clobber any registers which appear in REG_INC notes.  We
8024          could keep track of the changes to their values, but it is
8025          unlikely to help.  */
8026       {
8027         rtx x;
8028
8029         for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
8030           if (REG_NOTE_KIND (x) == REG_INC)
8031             reload_cse_invalidate_rtx (XEXP (x, 0), NULL_RTX);
8032       }
8033 #endif
8034
8035       /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
8036          after we have processed the insn.  */
8037       if (GET_CODE (insn) == CALL_INSN)
8038         {
8039           rtx x;
8040
8041           for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
8042             if (GET_CODE (XEXP (x, 0)) == CLOBBER)
8043               reload_cse_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX);
8044         }
8045     }
8046
8047   /* Free all the temporary structures we created, and go back to the
8048      regular obstacks.  */
8049   obstack_free (&reload_obstack, firstobj);
8050   pop_obstacks ();
8051 }
8052
8053 /* Return whether the values known for REGNO are equal to VAL.  MODE
8054    is the mode of the object that VAL is being copied to; this matters
8055    if VAL is a CONST_INT.  */
8056
8057 static int
8058 reload_cse_regno_equal_p (regno, val, mode)
8059      int regno;
8060      rtx val;
8061      enum machine_mode mode;
8062 {
8063   rtx x;
8064
8065   if (val == 0)
8066     return 0;
8067
8068   for (x = reg_values[regno]; x; x = XEXP (x, 1))
8069     if (XEXP (x, 0) != 0
8070         && rtx_equal_p (XEXP (x, 0), val)
8071         && (GET_CODE (val) != CONST_INT
8072             || mode == GET_MODE (x)
8073             || (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8074                 /* On a big endian machine if the value spans more than
8075                    one register then this register holds the high part of
8076                    it and we can't use it.
8077
8078                    ??? We should also compare with the high part of the
8079                    value.  */
8080                 && !(WORDS_BIG_ENDIAN
8081                      && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
8082                 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8083                                           GET_MODE_BITSIZE (GET_MODE (x))))))
8084       return 1;
8085
8086   return 0;
8087 }
8088
8089 /* See whether a single set is a noop.  SET is the set instruction we
8090    are should check, and INSN is the instruction from which it came.  */
8091
8092 static int
8093 reload_cse_noop_set_p (set, insn)
8094      rtx set;
8095      rtx insn;
8096 {
8097   rtx src, dest;
8098   enum machine_mode dest_mode;
8099   int dreg, sreg;
8100   int ret;
8101
8102   src = SET_SRC (set);
8103   dest = SET_DEST (set);
8104   dest_mode = GET_MODE (dest);
8105
8106   if (side_effects_p (src))
8107     return 0;
8108
8109   dreg = true_regnum (dest);
8110   sreg = true_regnum (src);
8111
8112   /* Check for setting a register to itself.  In this case, we don't
8113      have to worry about REG_DEAD notes.  */
8114   if (dreg >= 0 && dreg == sreg)
8115     return 1;
8116
8117   ret = 0;
8118   if (dreg >= 0)
8119     {
8120       /* Check for setting a register to itself.  */
8121       if (dreg == sreg)
8122         ret = 1;
8123
8124       /* Check for setting a register to a value which we already know
8125          is in the register.  */
8126       else if (reload_cse_regno_equal_p (dreg, src, dest_mode))
8127         ret = 1;
8128
8129       /* Check for setting a register DREG to another register SREG
8130          where SREG is equal to a value which is already in DREG.  */
8131       else if (sreg >= 0)
8132         {
8133           rtx x;
8134
8135           for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8136             {
8137               rtx tmp;
8138
8139               if (XEXP (x, 0) == 0)
8140                 continue;
8141
8142               if (dest_mode == GET_MODE (x))
8143                 tmp = XEXP (x, 0);
8144               else if (GET_MODE_BITSIZE (dest_mode)
8145                        < GET_MODE_BITSIZE (GET_MODE (x)))
8146                 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8147               else
8148                 continue;
8149
8150               if (tmp
8151                   && reload_cse_regno_equal_p (dreg, tmp, dest_mode))
8152                 {
8153                   ret = 1;
8154                   break;
8155                 }
8156             }
8157         }
8158     }
8159   else if (GET_CODE (dest) == MEM)
8160     {
8161       /* Check for storing a register to memory when we know that the
8162          register is equivalent to the memory location. */
8163       if (sreg >= 0
8164           && reload_cse_regno_equal_p (sreg, dest, dest_mode)
8165           && ! side_effects_p (dest))
8166         ret = 1;
8167     }
8168
8169   /* If we can delete this SET, then we need to look for an earlier
8170      REG_DEAD note on DREG, and remove it if it exists.  */
8171   if (ret && dreg >= 0)
8172     {
8173       if (! find_regno_note (insn, REG_UNUSED, dreg))
8174         reload_cse_no_longer_dead (dreg, dest_mode);
8175     }
8176
8177   return ret;
8178 }
8179
8180 /* Try to simplify a single SET instruction.  SET is the set pattern.
8181    INSN is the instruction it came from.
8182    This function only handles one case: if we set a register to a value
8183    which is not a register, we try to find that value in some other register
8184    and change the set into a register copy.  */
8185
8186 static int
8187 reload_cse_simplify_set (set, insn)
8188      rtx set;
8189      rtx insn;
8190 {
8191   int dreg;
8192   rtx src;
8193   enum machine_mode dest_mode;
8194   enum reg_class dclass;
8195   register int i;
8196
8197   dreg = true_regnum (SET_DEST (set));
8198   if (dreg < 0)
8199     return 0;
8200
8201   src = SET_SRC (set);
8202   if (side_effects_p (src) || true_regnum (src) >= 0)
8203     return 0;
8204
8205   /* If memory loads are cheaper than register copies, don't change
8206      them.  */
8207   if (GET_CODE (src) == MEM && MEMORY_MOVE_COST (GET_MODE (src)) < 2)
8208     return 0;
8209
8210   dest_mode = GET_MODE (SET_DEST (set));
8211   dclass = REGNO_REG_CLASS (dreg);
8212   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8213     {
8214       if (i != dreg
8215           && REGISTER_MOVE_COST (REGNO_REG_CLASS (i), dclass) == 2
8216           && reload_cse_regno_equal_p (i, src, dest_mode))
8217         {
8218           int validated;
8219
8220           /* Pop back to the real obstacks while changing the insn.  */
8221           pop_obstacks ();
8222
8223           validated = validate_change (insn, &SET_SRC (set),
8224                                        gen_rtx (REG, dest_mode, i), 1);
8225
8226           /* Go back to the obstack we are using for temporary
8227              storage.  */
8228           push_obstacks (&reload_obstack, &reload_obstack);
8229
8230           if (validated && ! find_regno_note (insn, REG_UNUSED, i))
8231             {
8232               reload_cse_no_longer_dead (i, dest_mode);
8233               return 1;
8234             }
8235         }
8236     }
8237   return 0;
8238 }
8239
8240 /* Try to replace operands in INSN with equivalent values that are already
8241    in registers.  This can be viewed as optional reloading.  
8242  
8243    For each non-register operand in the insn, see if any hard regs are
8244    known to be equivalent to that operand.  Record the alternatives which
8245    can accept these hard registers.  Among all alternatives, select the
8246    ones which are better or equal to the one currently matching, where
8247    "better" is in terms of '?' and '!' constraints.  Among the remaining
8248    alternatives, select the one which replaces most operands with
8249    hard registers.  */
8250
8251 static int
8252 reload_cse_simplify_operands (insn)
8253      rtx insn;
8254 {
8255 #ifdef REGISTER_CONSTRAINTS
8256   int insn_code_number, n_operands, n_alternatives;
8257   int i,j;
8258
8259   char *constraints[MAX_RECOG_OPERANDS];
8260   
8261   /* Vector recording how bad an alternative is.  */
8262   int *alternative_reject;
8263   /* Vector recording how many registers can be introduced by choosing
8264      this alternative.  */
8265   int *alternative_nregs;
8266   /* Array of vectors recording, for each operand and each alternative,
8267      which hard register to substitute, or -1 if the operand should be
8268      left as it is.  */
8269   int *op_alt_regno[MAX_RECOG_OPERANDS];
8270   /* Array of alternatives, sorted in order of decreasing desirability.  */
8271   int *alternative_order;
8272   
8273   /* Find out some information about this insn.  */
8274   insn_code_number = recog_memoized (insn);
8275   /* We don't modify asm instructions.  */
8276   if (insn_code_number < 0)
8277     return 0;
8278
8279   n_operands = insn_n_operands[insn_code_number];
8280   n_alternatives = insn_n_alternatives[insn_code_number];
8281   
8282   if (n_alternatives == 0 || n_operands == 0)
8283     return;
8284   insn_extract (insn);
8285
8286   /* Figure out which alternative currently matches.  */
8287   if (! constrain_operands (insn_code_number, 1))
8288     abort ();
8289
8290   alternative_reject = (int *) alloca (n_alternatives * sizeof (int));
8291   alternative_nregs = (int *) alloca (n_alternatives * sizeof (int));
8292   alternative_order = (int *) alloca (n_alternatives * sizeof (int));
8293   bzero ((char *)alternative_reject, n_alternatives * sizeof (int));
8294   bzero ((char *)alternative_nregs, n_alternatives * sizeof (int));
8295
8296   for (i = 0; i < n_operands; i++)
8297     {
8298       enum machine_mode mode;
8299       int regno;
8300       char *p;
8301
8302       op_alt_regno[i] = (int *) alloca (n_alternatives * sizeof (int));
8303       for (j = 0; j < n_alternatives; j++)
8304         op_alt_regno[i][j] = -1;
8305
8306       p = constraints[i] = insn_operand_constraint[insn_code_number][i];
8307       mode = insn_operand_mode[insn_code_number][i];
8308
8309       /* Add the reject values for each alternative given by the constraints
8310          for this operand.  */
8311       j = 0;
8312       while (*p != '\0')
8313         {
8314           char c = *p++;
8315           if (c == ',')
8316             j++;
8317           else if (c == '?')
8318             alternative_reject[j] += 3;
8319           else if (c == '!')
8320             alternative_reject[j] += 300;
8321         }
8322
8323       /* We won't change operands which are already registers.  We
8324          also don't want to modify output operands.  */
8325       regno = true_regnum (recog_operand[i]);
8326       if (regno >= 0
8327           || constraints[i][0] == '='
8328           || constraints[i][0] == '+')
8329         continue;
8330
8331       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8332         {
8333           int class = (int) NO_REGS;
8334
8335           if (! reload_cse_regno_equal_p (regno, recog_operand[i], mode))
8336             continue;
8337
8338           /* We found a register equal to this operand.  Now look for all
8339              alternatives that can accept this register and have not been
8340              assigned a register they can use yet.  */
8341           j = 0;
8342           p = constraints[i];
8343           for (;;)
8344             {
8345               char c = *p++;
8346               
8347               switch (c)
8348                 {
8349                 case '=':  case '+':  case '?':
8350                 case '#':  case '&':  case '!':
8351                 case '*':  case '%':  
8352                 case '0':  case '1':  case '2':  case '3':  case '4':
8353                 case 'm':  case '<':  case '>':  case 'V':  case 'o':
8354                 case 'E':  case 'F':  case 'G':  case 'H':
8355                 case 's':  case 'i':  case 'n':
8356                 case 'I':  case 'J':  case 'K':  case 'L':
8357                 case 'M':  case 'N':  case 'O':  case 'P':
8358 #ifdef EXTRA_CONSTRAINT
8359                 case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
8360 #endif
8361                 case 'p': case 'X':
8362                   /* These don't say anything we care about.  */
8363                   break;
8364
8365                 case 'g': case 'r':
8366                   class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8367                   break;
8368
8369                 default:
8370                   class
8371                     = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
8372                   break;
8373
8374                 case ',': case '\0':
8375                   /* See if REGNO fits this alternative, and set it up as the
8376                      replacement register if we don't have one for this
8377                      alternative yet.  */
8378                   if (op_alt_regno[i][j] == -1
8379                       && reg_fits_class_p (gen_rtx (REG, mode, regno), class,
8380                                            0, mode))
8381                     {
8382                       alternative_nregs[j]++;
8383                       op_alt_regno[i][j] = regno;
8384                     }
8385                   j++;
8386                   break;
8387                 }
8388
8389               if (c == '\0')
8390                 break;
8391             }
8392         }
8393     }
8394
8395   /* Record all alternatives which are better or equal to the currently
8396      matching one in the alternative_order array.  */
8397   for (i = j = 0; i < n_alternatives; i++)
8398     if (alternative_reject[i] <= alternative_reject[which_alternative])
8399       alternative_order[j++] = i;
8400   n_alternatives = j;
8401
8402   /* Sort it.  Given a small number of alternatives, a dumb algorithm
8403      won't hurt too much.  */
8404   for (i = 0; i < n_alternatives - 1; i++)
8405     {
8406       int best = i;
8407       int best_reject = alternative_reject[alternative_order[i]];
8408       int best_nregs = alternative_nregs[alternative_order[i]];
8409       int tmp;
8410
8411       for (j = i + 1; j < n_alternatives; j++)
8412         {
8413           int this_reject = alternative_reject[alternative_order[j]];
8414           int this_nregs = alternative_nregs[alternative_order[j]];
8415
8416           if (this_reject < best_reject
8417               || (this_reject == best_reject && this_nregs < best_nregs))
8418             {
8419               best = j;
8420               best_reject = this_reject;
8421               best_nregs = this_nregs;
8422             }
8423         }
8424       
8425       tmp = alternative_order[best];
8426       alternative_order[best] = alternative_order[i];
8427       alternative_order[i] = tmp;
8428     }
8429   
8430   /* Substitute the operands as determined by op_alt_regno for the best
8431      alternative.  */
8432   j = alternative_order[0];
8433   CLEAR_HARD_REG_SET (no_longer_dead_regs);
8434
8435   /* Pop back to the real obstacks while changing the insn.  */
8436   pop_obstacks ();
8437
8438   for (i = 0; i < n_operands; i++)
8439     {
8440       enum machine_mode mode = insn_operand_mode[insn_code_number][i];
8441       if (op_alt_regno[i][j] == -1)
8442         continue;
8443
8444       reload_cse_no_longer_dead (op_alt_regno[i][j], mode);
8445       validate_change (insn, recog_operand_loc[i],
8446                        gen_rtx (REG, mode, op_alt_regno[i][j]), 1);
8447     }
8448
8449   for (i = insn_n_dups[insn_code_number] - 1; i >= 0; i--)
8450     {
8451       int op = recog_dup_num[i];
8452       enum machine_mode mode = insn_operand_mode[insn_code_number][op];
8453
8454       if (op_alt_regno[op][j] == -1)
8455         continue;
8456
8457       reload_cse_no_longer_dead (op_alt_regno[op][j], mode);
8458       validate_change (insn, recog_dup_loc[i],
8459                        gen_rtx (REG, mode, op_alt_regno[op][j]), 1);
8460     }
8461
8462   /* Go back to the obstack we are using for temporary
8463      storage.  */
8464   push_obstacks (&reload_obstack, &reload_obstack);
8465
8466   return apply_change_group ();
8467 #else
8468   return 0;
8469 #endif
8470 }
8471
8472 /* These two variables are used to pass information from
8473    reload_cse_record_set to reload_cse_check_clobber.  */
8474
8475 static int reload_cse_check_clobbered;
8476 static rtx reload_cse_check_src;
8477
8478 /* See if DEST overlaps with RELOAD_CSE_CHECK_SRC. If it does, set
8479    RELOAD_CSE_CHECK_CLOBBERED.  This is called via note_stores.  The
8480    second argument, which is passed by note_stores, is ignored.  */
8481
8482 static void
8483 reload_cse_check_clobber (dest, ignore)
8484      rtx dest;
8485      rtx ignore;
8486 {
8487   if (reg_overlap_mentioned_p (dest, reload_cse_check_src))
8488     reload_cse_check_clobbered = 1;
8489 }
8490
8491 /* Record the result of a SET instruction.  SET is the set pattern.
8492    BODY is the pattern of the insn that it came from.  */
8493
8494 static void
8495 reload_cse_record_set (set, body)
8496      rtx set;
8497      rtx body;
8498 {
8499   rtx dest, src, x;
8500   int dreg, sreg;
8501   enum machine_mode dest_mode;
8502
8503   dest = SET_DEST (set);
8504   src = SET_SRC (set);
8505   dreg = true_regnum (dest);
8506   sreg = true_regnum (src);
8507   dest_mode = GET_MODE (dest);
8508
8509   /* Some machines don't define AUTO_INC_DEC, but they still use push
8510      instructions.  We need to catch that case here in order to
8511      invalidate the stack pointer correctly.  Note that invalidating
8512      the stack pointer is different from invalidating DEST.  */
8513   x = dest;
8514   while (GET_CODE (x) == SUBREG
8515          || GET_CODE (x) == ZERO_EXTRACT
8516          || GET_CODE (x) == SIGN_EXTRACT
8517          || GET_CODE (x) == STRICT_LOW_PART)
8518     x = XEXP (x, 0);
8519   if (push_operand (x, GET_MODE (x)))
8520     {
8521       reload_cse_invalidate_rtx (stack_pointer_rtx, NULL_RTX);
8522       reload_cse_invalidate_rtx (dest, NULL_RTX);
8523       return;
8524     }
8525
8526   /* We can only handle an assignment to a register, or a store of a
8527      register to a memory location.  For other cases, we just clobber
8528      the destination.  We also have to just clobber if there are side
8529      effects in SRC or DEST.  */
8530   if ((dreg < 0 && GET_CODE (dest) != MEM)
8531       || side_effects_p (src)
8532       || side_effects_p (dest))
8533     {
8534       reload_cse_invalidate_rtx (dest, NULL_RTX);
8535       return;
8536     }
8537
8538 #ifdef HAVE_cc0
8539   /* We don't try to handle values involving CC, because it's a pain
8540      to keep track of when they have to be invalidated.  */
8541   if (reg_mentioned_p (cc0_rtx, src)
8542       || reg_mentioned_p (cc0_rtx, dest))
8543     {
8544       reload_cse_invalidate_rtx (dest, NULL_RTX);
8545       return;
8546     }
8547 #endif
8548
8549   /* If BODY is a PARALLEL, then we need to see whether the source of
8550      SET is clobbered by some other instruction in the PARALLEL.  */
8551   if (GET_CODE (body) == PARALLEL)
8552     {
8553       int i;
8554
8555       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8556         {
8557           rtx x;
8558
8559           x = XVECEXP (body, 0, i);
8560           if (x == set)
8561             continue;
8562
8563           reload_cse_check_clobbered = 0;
8564           reload_cse_check_src = src;
8565           note_stores (x, reload_cse_check_clobber);
8566           if (reload_cse_check_clobbered)
8567             {
8568               reload_cse_invalidate_rtx (dest, NULL_RTX);
8569               return;
8570             }
8571         }
8572     }
8573
8574   if (dreg >= 0)
8575     {
8576       int i;
8577
8578       /* This is an assignment to a register.  Update the value we
8579          have stored for the register.  */
8580       if (sreg >= 0)
8581         {
8582           rtx x;
8583
8584           /* This is a copy from one register to another.  Any values
8585              which were valid for SREG are now valid for DREG.  If the
8586              mode changes, we use gen_lowpart_common to extract only
8587              the part of the value that is copied.  */
8588           reg_values[dreg] = 0;
8589           for (x = reg_values[sreg]; x; x = XEXP (x, 1))
8590             {
8591               rtx tmp;
8592
8593               if (XEXP (x, 0) == 0)
8594                 continue;
8595               if (dest_mode == GET_MODE (XEXP (x, 0)))
8596                 tmp = XEXP (x, 0);
8597               else if (GET_MODE_BITSIZE (dest_mode)
8598                         > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
8599                 continue;
8600               else
8601                 tmp = gen_lowpart_common (dest_mode, XEXP (x, 0));
8602               if (tmp)
8603                 reg_values[dreg] = gen_rtx (EXPR_LIST, dest_mode, tmp,
8604                                             reg_values[dreg]);
8605             }         
8606         }
8607       else
8608         reg_values[dreg] = gen_rtx (EXPR_LIST, dest_mode, src, NULL_RTX);
8609
8610       /* We've changed DREG, so invalidate any values held by other
8611          registers that depend upon it.  */
8612       reload_cse_invalidate_regno (dreg, dest_mode, 0);
8613
8614       /* If this assignment changes more than one hard register,
8615          forget anything we know about the others.  */
8616       for (i = 1; i < HARD_REGNO_NREGS (dreg, dest_mode); i++)
8617         reg_values[dreg + i] = 0;
8618     }
8619   else if (GET_CODE (dest) == MEM)
8620     {
8621       /* Invalidate conflicting memory locations.  */
8622       reload_cse_invalidate_mem (dest);
8623
8624       /* If we're storing a register to memory, add DEST to the list
8625          in REG_VALUES.  */
8626       if (sreg >= 0 && ! side_effects_p (dest))
8627         reg_values[sreg] = gen_rtx (EXPR_LIST, dest_mode, dest,
8628                                     reg_values[sreg]);
8629     }
8630   else
8631     {
8632       /* We should have bailed out earlier.  */
8633       abort ();
8634     }
8635 }