OSDN Git Service

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