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