OSDN Git Service

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