OSDN Git Service

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