OSDN Git Service

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