OSDN Git Service

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