OSDN Git Service

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