OSDN Git Service

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