OSDN Git Service

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