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