OSDN Git Service

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