OSDN Git Service

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