OSDN Git Service

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