OSDN Git Service

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