OSDN Git Service

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