OSDN Git Service

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