OSDN Git Service

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