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