OSDN Git Service

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