OSDN Git Service

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