OSDN Git Service

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