re PR rtl-optimization/48830 (unrecognized insn: storing invalid upper FP reg in...
[platform/upstream/gcc.git] / gcc / reload.c
1 /* Search an insn for pseudo regs that must be in hard regs and are not.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This file contains subroutines used only from the file reload1.c.
23    It knows how to scan one insn for operands and values
24    that need to be copied into registers to make valid code.
25    It also finds other operands and values which are valid
26    but for which equivalent values in registers exist and
27    ought to be used instead.
28
29    Before processing the first insn of the function, call `init_reload'.
30    init_reload actually has to be called earlier anyway.
31
32    To scan an insn, call `find_reloads'.  This does two things:
33    1. sets up tables describing which values must be reloaded
34    for this insn, and what kind of hard regs they must be reloaded into;
35    2. optionally record the locations where those values appear in
36    the data, so they can be replaced properly later.
37    This is done only if the second arg to `find_reloads' is nonzero.
38
39    The third arg to `find_reloads' specifies the number of levels
40    of indirect addressing supported by the machine.  If it is zero,
41    indirect addressing is not valid.  If it is one, (MEM (REG n))
42    is valid even if (REG n) did not get a hard register; if it is two,
43    (MEM (MEM (REG n))) is also valid even if (REG n) did not get a
44    hard register, and similarly for higher values.
45
46    Then you must choose the hard regs to reload those pseudo regs into,
47    and generate appropriate load insns before this insn and perhaps
48    also store insns after this insn.  Set up the array `reload_reg_rtx'
49    to contain the REG rtx's for the registers you used.  In some
50    cases `find_reloads' will return a nonzero value in `reload_reg_rtx'
51    for certain reloads.  Then that tells you which register to use,
52    so you do not need to allocate one.  But you still do need to add extra
53    instructions to copy the value into and out of that register.
54
55    Finally you must call `subst_reloads' to substitute the reload reg rtx's
56    into the locations already recorded.
57
58 NOTE SIDE EFFECTS:
59
60    find_reloads can alter the operands of the instruction it is called on.
61
62    1. Two operands of any sort may be interchanged, if they are in a
63    commutative instruction.
64    This happens only if find_reloads thinks the instruction will compile
65    better that way.
66
67    2. Pseudo-registers that are equivalent to constants are replaced
68    with those constants if they are not in hard registers.
69
70 1 happens every time find_reloads is called.
71 2 happens only when REPLACE is 1, which is only when
72 actually doing the reloads, not when just counting them.
73
74 Using a reload register for several reloads in one insn:
75
76 When an insn has reloads, it is considered as having three parts:
77 the input reloads, the insn itself after reloading, and the output reloads.
78 Reloads of values used in memory addresses are often needed for only one part.
79
80 When this is so, reload_when_needed records which part needs the reload.
81 Two reloads for different parts of the insn can share the same reload
82 register.
83
84 When a reload is used for addresses in multiple parts, or when it is
85 an ordinary operand, it is classified as RELOAD_OTHER, and cannot share
86 a register with any other reload.  */
87
88 #define REG_OK_STRICT
89
90 /* We do not enable this with ENABLE_CHECKING, since it is awfully slow.  */
91 #undef DEBUG_RELOAD
92
93 #include "config.h"
94 #include "system.h"
95 #include "coretypes.h"
96 #include "tm.h"
97 #include "rtl-error.h"
98 #include "tm_p.h"
99 #include "insn-config.h"
100 #include "expr.h"
101 #include "optabs.h"
102 #include "recog.h"
103 #include "df.h"
104 #include "reload.h"
105 #include "regs.h"
106 #include "addresses.h"
107 #include "hard-reg-set.h"
108 #include "flags.h"
109 #include "output.h"
110 #include "function.h"
111 #include "params.h"
112 #include "target.h"
113 #include "ira.h"
114
115 /* True if X is a constant that can be forced into the constant pool.
116    MODE is the mode of the operand, or VOIDmode if not known.  */
117 #define CONST_POOL_OK_P(MODE, X)                \
118   ((MODE) != VOIDmode                           \
119    && CONSTANT_P (X)                            \
120    && GET_CODE (X) != HIGH                      \
121    && !targetm.cannot_force_const_mem (MODE, X))
122
123 /* True if C is a non-empty register class that has too few registers
124    to be safely used as a reload target class.  */
125
126 static inline bool
127 small_register_class_p (reg_class_t rclass)
128 {
129   return (reg_class_size [(int) rclass] == 1
130           || (reg_class_size [(int) rclass] >= 1 
131               && targetm.class_likely_spilled_p (rclass)));
132 }
133
134 \f
135 /* All reloads of the current insn are recorded here.  See reload.h for
136    comments.  */
137 int n_reloads;
138 struct reload rld[MAX_RELOADS];
139
140 /* All the "earlyclobber" operands of the current insn
141    are recorded here.  */
142 int n_earlyclobbers;
143 rtx reload_earlyclobbers[MAX_RECOG_OPERANDS];
144
145 int reload_n_operands;
146
147 /* Replacing reloads.
148
149    If `replace_reloads' is nonzero, then as each reload is recorded
150    an entry is made for it in the table `replacements'.
151    Then later `subst_reloads' can look through that table and
152    perform all the replacements needed.  */
153
154 /* Nonzero means record the places to replace.  */
155 static int replace_reloads;
156
157 /* Each replacement is recorded with a structure like this.  */
158 struct replacement
159 {
160   rtx *where;                   /* Location to store in */
161   rtx *subreg_loc;              /* Location of SUBREG if WHERE is inside
162                                    a SUBREG; 0 otherwise.  */
163   int what;                     /* which reload this is for */
164   enum machine_mode mode;       /* mode it must have */
165 };
166
167 static struct replacement replacements[MAX_RECOG_OPERANDS * ((MAX_REGS_PER_ADDRESS * 2) + 1)];
168
169 /* Number of replacements currently recorded.  */
170 static int n_replacements;
171
172 /* Used to track what is modified by an operand.  */
173 struct decomposition
174 {
175   int reg_flag;         /* Nonzero if referencing a register.  */
176   int safe;             /* Nonzero if this can't conflict with anything.  */
177   rtx base;             /* Base address for MEM.  */
178   HOST_WIDE_INT start;  /* Starting offset or register number.  */
179   HOST_WIDE_INT end;    /* Ending offset or register number.  */
180 };
181
182 #ifdef SECONDARY_MEMORY_NEEDED
183
184 /* Save MEMs needed to copy from one class of registers to another.  One MEM
185    is used per mode, but normally only one or two modes are ever used.
186
187    We keep two versions, before and after register elimination.  The one
188    after register elimination is record separately for each operand.  This
189    is done in case the address is not valid to be sure that we separately
190    reload each.  */
191
192 static rtx secondary_memlocs[NUM_MACHINE_MODES];
193 static rtx secondary_memlocs_elim[NUM_MACHINE_MODES][MAX_RECOG_OPERANDS];
194 static int secondary_memlocs_elim_used = 0;
195 #endif
196
197 /* The instruction we are doing reloads for;
198    so we can test whether a register dies in it.  */
199 static rtx this_insn;
200
201 /* Nonzero if this instruction is a user-specified asm with operands.  */
202 static int this_insn_is_asm;
203
204 /* If hard_regs_live_known is nonzero,
205    we can tell which hard regs are currently live,
206    at least enough to succeed in choosing dummy reloads.  */
207 static int hard_regs_live_known;
208
209 /* Indexed by hard reg number,
210    element is nonnegative if hard reg has been spilled.
211    This vector is passed to `find_reloads' as an argument
212    and is not changed here.  */
213 static short *static_reload_reg_p;
214
215 /* Set to 1 in subst_reg_equivs if it changes anything.  */
216 static int subst_reg_equivs_changed;
217
218 /* On return from push_reload, holds the reload-number for the OUT
219    operand, which can be different for that from the input operand.  */
220 static int output_reloadnum;
221
222   /* Compare two RTX's.  */
223 #define MATCHES(x, y) \
224  (x == y || (x != 0 && (REG_P (x)                               \
225                         ? REG_P (y) && REGNO (x) == REGNO (y)   \
226                         : rtx_equal_p (x, y) && ! side_effects_p (x))))
227
228   /* Indicates if two reloads purposes are for similar enough things that we
229      can merge their reloads.  */
230 #define MERGABLE_RELOADS(when1, when2, op1, op2) \
231   ((when1) == RELOAD_OTHER || (when2) == RELOAD_OTHER   \
232    || ((when1) == (when2) && (op1) == (op2))            \
233    || ((when1) == RELOAD_FOR_INPUT && (when2) == RELOAD_FOR_INPUT) \
234    || ((when1) == RELOAD_FOR_OPERAND_ADDRESS            \
235        && (when2) == RELOAD_FOR_OPERAND_ADDRESS)        \
236    || ((when1) == RELOAD_FOR_OTHER_ADDRESS              \
237        && (when2) == RELOAD_FOR_OTHER_ADDRESS))
238
239   /* Nonzero if these two reload purposes produce RELOAD_OTHER when merged.  */
240 #define MERGE_TO_OTHER(when1, when2, op1, op2) \
241   ((when1) != (when2)                                   \
242    || ! ((op1) == (op2)                                 \
243          || (when1) == RELOAD_FOR_INPUT                 \
244          || (when1) == RELOAD_FOR_OPERAND_ADDRESS       \
245          || (when1) == RELOAD_FOR_OTHER_ADDRESS))
246
247   /* If we are going to reload an address, compute the reload type to
248      use.  */
249 #define ADDR_TYPE(type)                                 \
250   ((type) == RELOAD_FOR_INPUT_ADDRESS                   \
251    ? RELOAD_FOR_INPADDR_ADDRESS                         \
252    : ((type) == RELOAD_FOR_OUTPUT_ADDRESS               \
253       ? RELOAD_FOR_OUTADDR_ADDRESS                      \
254       : (type)))
255
256 static int push_secondary_reload (int, rtx, int, int, enum reg_class,
257                                   enum machine_mode, enum reload_type,
258                                   enum insn_code *, secondary_reload_info *);
259 static enum reg_class find_valid_class (enum machine_mode, enum machine_mode,
260                                         int, unsigned int);
261 static int reload_inner_reg_of_subreg (rtx, enum machine_mode, int);
262 static void push_replacement (rtx *, int, enum machine_mode);
263 static void dup_replacements (rtx *, rtx *);
264 static void combine_reloads (void);
265 static int find_reusable_reload (rtx *, rtx, enum reg_class,
266                                  enum reload_type, int, int);
267 static rtx find_dummy_reload (rtx, rtx, rtx *, rtx *, enum machine_mode,
268                               enum machine_mode, reg_class_t, int, int);
269 static int hard_reg_set_here_p (unsigned int, unsigned int, rtx);
270 static struct decomposition decompose (rtx);
271 static int immune_p (rtx, rtx, struct decomposition);
272 static bool alternative_allows_const_pool_ref (rtx, const char *, int);
273 static rtx find_reloads_toplev (rtx, int, enum reload_type, int, int, rtx,
274                                 int *);
275 static rtx make_memloc (rtx, int);
276 static int maybe_memory_address_addr_space_p (enum machine_mode, rtx,
277                                               addr_space_t, rtx *);
278 static int find_reloads_address (enum machine_mode, rtx *, rtx, rtx *,
279                                  int, enum reload_type, int, rtx);
280 static rtx subst_reg_equivs (rtx, rtx);
281 static rtx subst_indexed_address (rtx);
282 static void update_auto_inc_notes (rtx, int, int);
283 static int find_reloads_address_1 (enum machine_mode, rtx, int,
284                                    enum rtx_code, enum rtx_code, rtx *,
285                                    int, enum reload_type,int, rtx);
286 static void find_reloads_address_part (rtx, rtx *, enum reg_class,
287                                        enum machine_mode, int,
288                                        enum reload_type, int);
289 static rtx find_reloads_subreg_address (rtx, int, int, enum reload_type,
290                                         int, rtx, int *);
291 static void copy_replacements_1 (rtx *, rtx *, int);
292 static int find_inc_amount (rtx, rtx);
293 static int refers_to_mem_for_reload_p (rtx);
294 static int refers_to_regno_for_reload_p (unsigned int, unsigned int,
295                                          rtx, rtx *);
296
297 /* Add NEW to reg_equiv_alt_mem_list[REGNO] if it's not present in the
298    list yet.  */
299
300 static void
301 push_reg_equiv_alt_mem (int regno, rtx mem)
302 {
303   rtx it;
304
305   for (it = reg_equiv_alt_mem_list (regno); it; it = XEXP (it, 1))
306     if (rtx_equal_p (XEXP (it, 0), mem))
307       return;
308
309   reg_equiv_alt_mem_list (regno)
310     = alloc_EXPR_LIST (REG_EQUIV, mem,
311                        reg_equiv_alt_mem_list (regno));
312 }
313 \f
314 /* Determine if any secondary reloads are needed for loading (if IN_P is
315    nonzero) or storing (if IN_P is zero) X to or from a reload register of
316    register class RELOAD_CLASS in mode RELOAD_MODE.  If secondary reloads
317    are needed, push them.
318
319    Return the reload number of the secondary reload we made, or -1 if
320    we didn't need one.  *PICODE is set to the insn_code to use if we do
321    need a secondary reload.  */
322
323 static int
324 push_secondary_reload (int in_p, rtx x, int opnum, int optional,
325                        enum reg_class reload_class,
326                        enum machine_mode reload_mode, enum reload_type type,
327                        enum insn_code *picode, secondary_reload_info *prev_sri)
328 {
329   enum reg_class rclass = NO_REGS;
330   enum reg_class scratch_class;
331   enum machine_mode mode = reload_mode;
332   enum insn_code icode = CODE_FOR_nothing;
333   enum insn_code t_icode = CODE_FOR_nothing;
334   enum reload_type secondary_type;
335   int s_reload, t_reload = -1;
336   const char *scratch_constraint;
337   char letter;
338   secondary_reload_info sri;
339
340   if (type == RELOAD_FOR_INPUT_ADDRESS
341       || type == RELOAD_FOR_OUTPUT_ADDRESS
342       || type == RELOAD_FOR_INPADDR_ADDRESS
343       || type == RELOAD_FOR_OUTADDR_ADDRESS)
344     secondary_type = type;
345   else
346     secondary_type = in_p ? RELOAD_FOR_INPUT_ADDRESS : RELOAD_FOR_OUTPUT_ADDRESS;
347
348   *picode = CODE_FOR_nothing;
349
350   /* If X is a paradoxical SUBREG, use the inner value to determine both the
351      mode and object being reloaded.  */
352   if (GET_CODE (x) == SUBREG
353       && (GET_MODE_SIZE (GET_MODE (x))
354           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
355     {
356       x = SUBREG_REG (x);
357       reload_mode = GET_MODE (x);
358     }
359
360   /* If X is a pseudo-register that has an equivalent MEM (actually, if it
361      is still a pseudo-register by now, it *must* have an equivalent MEM
362      but we don't want to assume that), use that equivalent when seeing if
363      a secondary reload is needed since whether or not a reload is needed
364      might be sensitive to the form of the MEM.  */
365
366   if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER
367       && reg_equiv_mem (REGNO (x)))
368     x = reg_equiv_mem (REGNO (x));
369
370   sri.icode = CODE_FOR_nothing;
371   sri.prev_sri = prev_sri;
372   rclass = (enum reg_class) targetm.secondary_reload (in_p, x, reload_class,
373                                                       reload_mode, &sri);
374   icode = (enum insn_code) sri.icode;
375
376   /* If we don't need any secondary registers, done.  */
377   if (rclass == NO_REGS && icode == CODE_FOR_nothing)
378     return -1;
379
380   if (rclass != NO_REGS)
381     t_reload = push_secondary_reload (in_p, x, opnum, optional, rclass,
382                                       reload_mode, type, &t_icode, &sri);
383
384   /* If we will be using an insn, the secondary reload is for a
385      scratch register.  */
386
387   if (icode != CODE_FOR_nothing)
388     {
389       /* If IN_P is nonzero, the reload register will be the output in
390          operand 0.  If IN_P is zero, the reload register will be the input
391          in operand 1.  Outputs should have an initial "=", which we must
392          skip.  */
393
394       /* ??? It would be useful to be able to handle only two, or more than
395          three, operands, but for now we can only handle the case of having
396          exactly three: output, input and one temp/scratch.  */
397       gcc_assert (insn_data[(int) icode].n_operands == 3);
398
399       /* ??? We currently have no way to represent a reload that needs
400          an icode to reload from an intermediate tertiary reload register.
401          We should probably have a new field in struct reload to tag a
402          chain of scratch operand reloads onto.   */
403       gcc_assert (rclass == NO_REGS);
404
405       scratch_constraint = insn_data[(int) icode].operand[2].constraint;
406       gcc_assert (*scratch_constraint == '=');
407       scratch_constraint++;
408       if (*scratch_constraint == '&')
409         scratch_constraint++;
410       letter = *scratch_constraint;
411       scratch_class = (letter == 'r' ? GENERAL_REGS
412                        : REG_CLASS_FROM_CONSTRAINT ((unsigned char) letter,
413                                                    scratch_constraint));
414
415       rclass = scratch_class;
416       mode = insn_data[(int) icode].operand[2].mode;
417     }
418
419   /* This case isn't valid, so fail.  Reload is allowed to use the same
420      register for RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT reloads, but
421      in the case of a secondary register, we actually need two different
422      registers for correct code.  We fail here to prevent the possibility of
423      silently generating incorrect code later.
424
425      The convention is that secondary input reloads are valid only if the
426      secondary_class is different from class.  If you have such a case, you
427      can not use secondary reloads, you must work around the problem some
428      other way.
429
430      Allow this when a reload_in/out pattern is being used.  I.e. assume
431      that the generated code handles this case.  */
432
433   gcc_assert (!in_p || rclass != reload_class || icode != CODE_FOR_nothing
434               || t_icode != CODE_FOR_nothing);
435
436   /* See if we can reuse an existing secondary reload.  */
437   for (s_reload = 0; s_reload < n_reloads; s_reload++)
438     if (rld[s_reload].secondary_p
439         && (reg_class_subset_p (rclass, rld[s_reload].rclass)
440             || reg_class_subset_p (rld[s_reload].rclass, rclass))
441         && ((in_p && rld[s_reload].inmode == mode)
442             || (! in_p && rld[s_reload].outmode == mode))
443         && ((in_p && rld[s_reload].secondary_in_reload == t_reload)
444             || (! in_p && rld[s_reload].secondary_out_reload == t_reload))
445         && ((in_p && rld[s_reload].secondary_in_icode == t_icode)
446             || (! in_p && rld[s_reload].secondary_out_icode == t_icode))
447         && (small_register_class_p (rclass)
448             || targetm.small_register_classes_for_mode_p (VOIDmode))
449         && MERGABLE_RELOADS (secondary_type, rld[s_reload].when_needed,
450                              opnum, rld[s_reload].opnum))
451       {
452         if (in_p)
453           rld[s_reload].inmode = mode;
454         if (! in_p)
455           rld[s_reload].outmode = mode;
456
457         if (reg_class_subset_p (rclass, rld[s_reload].rclass))
458           rld[s_reload].rclass = rclass;
459
460         rld[s_reload].opnum = MIN (rld[s_reload].opnum, opnum);
461         rld[s_reload].optional &= optional;
462         rld[s_reload].secondary_p = 1;
463         if (MERGE_TO_OTHER (secondary_type, rld[s_reload].when_needed,
464                             opnum, rld[s_reload].opnum))
465           rld[s_reload].when_needed = RELOAD_OTHER;
466
467         break;
468       }
469
470   if (s_reload == n_reloads)
471     {
472 #ifdef SECONDARY_MEMORY_NEEDED
473       /* If we need a memory location to copy between the two reload regs,
474          set it up now.  Note that we do the input case before making
475          the reload and the output case after.  This is due to the
476          way reloads are output.  */
477
478       if (in_p && icode == CODE_FOR_nothing
479           && SECONDARY_MEMORY_NEEDED (rclass, reload_class, mode))
480         {
481           get_secondary_mem (x, reload_mode, opnum, type);
482
483           /* We may have just added new reloads.  Make sure we add
484              the new reload at the end.  */
485           s_reload = n_reloads;
486         }
487 #endif
488
489       /* We need to make a new secondary reload for this register class.  */
490       rld[s_reload].in = rld[s_reload].out = 0;
491       rld[s_reload].rclass = rclass;
492
493       rld[s_reload].inmode = in_p ? mode : VOIDmode;
494       rld[s_reload].outmode = ! in_p ? mode : VOIDmode;
495       rld[s_reload].reg_rtx = 0;
496       rld[s_reload].optional = optional;
497       rld[s_reload].inc = 0;
498       /* Maybe we could combine these, but it seems too tricky.  */
499       rld[s_reload].nocombine = 1;
500       rld[s_reload].in_reg = 0;
501       rld[s_reload].out_reg = 0;
502       rld[s_reload].opnum = opnum;
503       rld[s_reload].when_needed = secondary_type;
504       rld[s_reload].secondary_in_reload = in_p ? t_reload : -1;
505       rld[s_reload].secondary_out_reload = ! in_p ? t_reload : -1;
506       rld[s_reload].secondary_in_icode = in_p ? t_icode : CODE_FOR_nothing;
507       rld[s_reload].secondary_out_icode
508         = ! in_p ? t_icode : CODE_FOR_nothing;
509       rld[s_reload].secondary_p = 1;
510
511       n_reloads++;
512
513 #ifdef SECONDARY_MEMORY_NEEDED
514       if (! in_p && icode == CODE_FOR_nothing
515           && SECONDARY_MEMORY_NEEDED (reload_class, rclass, mode))
516         get_secondary_mem (x, mode, opnum, type);
517 #endif
518     }
519
520   *picode = icode;
521   return s_reload;
522 }
523
524 /* If a secondary reload is needed, return its class.  If both an intermediate
525    register and a scratch register is needed, we return the class of the
526    intermediate register.  */
527 reg_class_t
528 secondary_reload_class (bool in_p, reg_class_t rclass, enum machine_mode mode,
529                         rtx x)
530 {
531   enum insn_code icode;
532   secondary_reload_info sri;
533
534   sri.icode = CODE_FOR_nothing;
535   sri.prev_sri = NULL;
536   rclass
537     = (enum reg_class) targetm.secondary_reload (in_p, x, rclass, mode, &sri);
538   icode = (enum insn_code) sri.icode;
539
540   /* If there are no secondary reloads at all, we return NO_REGS.
541      If an intermediate register is needed, we return its class.  */
542   if (icode == CODE_FOR_nothing || rclass != NO_REGS)
543     return rclass;
544
545   /* No intermediate register is needed, but we have a special reload
546      pattern, which we assume for now needs a scratch register.  */
547   return scratch_reload_class (icode);
548 }
549
550 /* ICODE is the insn_code of a reload pattern.  Check that it has exactly
551    three operands, verify that operand 2 is an output operand, and return
552    its register class.
553    ??? We'd like to be able to handle any pattern with at least 2 operands,
554    for zero or more scratch registers, but that needs more infrastructure.  */
555 enum reg_class
556 scratch_reload_class (enum insn_code icode)
557 {
558   const char *scratch_constraint;
559   char scratch_letter;
560   enum reg_class rclass;
561
562   gcc_assert (insn_data[(int) icode].n_operands == 3);
563   scratch_constraint = insn_data[(int) icode].operand[2].constraint;
564   gcc_assert (*scratch_constraint == '=');
565   scratch_constraint++;
566   if (*scratch_constraint == '&')
567     scratch_constraint++;
568   scratch_letter = *scratch_constraint;
569   if (scratch_letter == 'r')
570     return GENERAL_REGS;
571   rclass = REG_CLASS_FROM_CONSTRAINT ((unsigned char) scratch_letter,
572                                      scratch_constraint);
573   gcc_assert (rclass != NO_REGS);
574   return rclass;
575 }
576 \f
577 #ifdef SECONDARY_MEMORY_NEEDED
578
579 /* Return a memory location that will be used to copy X in mode MODE.
580    If we haven't already made a location for this mode in this insn,
581    call find_reloads_address on the location being returned.  */
582
583 rtx
584 get_secondary_mem (rtx x ATTRIBUTE_UNUSED, enum machine_mode mode,
585                    int opnum, enum reload_type type)
586 {
587   rtx loc;
588   int mem_valid;
589
590   /* By default, if MODE is narrower than a word, widen it to a word.
591      This is required because most machines that require these memory
592      locations do not support short load and stores from all registers
593      (e.g., FP registers).  */
594
595 #ifdef SECONDARY_MEMORY_NEEDED_MODE
596   mode = SECONDARY_MEMORY_NEEDED_MODE (mode);
597 #else
598   if (GET_MODE_BITSIZE (mode) < BITS_PER_WORD && INTEGRAL_MODE_P (mode))
599     mode = mode_for_size (BITS_PER_WORD, GET_MODE_CLASS (mode), 0);
600 #endif
601
602   /* If we already have made a MEM for this operand in MODE, return it.  */
603   if (secondary_memlocs_elim[(int) mode][opnum] != 0)
604     return secondary_memlocs_elim[(int) mode][opnum];
605
606   /* If this is the first time we've tried to get a MEM for this mode,
607      allocate a new one.  `something_changed' in reload will get set
608      by noticing that the frame size has changed.  */
609
610   if (secondary_memlocs[(int) mode] == 0)
611     {
612 #ifdef SECONDARY_MEMORY_NEEDED_RTX
613       secondary_memlocs[(int) mode] = SECONDARY_MEMORY_NEEDED_RTX (mode);
614 #else
615       secondary_memlocs[(int) mode]
616         = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
617 #endif
618     }
619
620   /* Get a version of the address doing any eliminations needed.  If that
621      didn't give us a new MEM, make a new one if it isn't valid.  */
622
623   loc = eliminate_regs (secondary_memlocs[(int) mode], VOIDmode, NULL_RTX);
624   mem_valid = strict_memory_address_addr_space_p (mode, XEXP (loc, 0),
625                                                   MEM_ADDR_SPACE (loc));
626
627   if (! mem_valid && loc == secondary_memlocs[(int) mode])
628     loc = copy_rtx (loc);
629
630   /* The only time the call below will do anything is if the stack
631      offset is too large.  In that case IND_LEVELS doesn't matter, so we
632      can just pass a zero.  Adjust the type to be the address of the
633      corresponding object.  If the address was valid, save the eliminated
634      address.  If it wasn't valid, we need to make a reload each time, so
635      don't save it.  */
636
637   if (! mem_valid)
638     {
639       type =  (type == RELOAD_FOR_INPUT ? RELOAD_FOR_INPUT_ADDRESS
640                : type == RELOAD_FOR_OUTPUT ? RELOAD_FOR_OUTPUT_ADDRESS
641                : RELOAD_OTHER);
642
643       find_reloads_address (mode, &loc, XEXP (loc, 0), &XEXP (loc, 0),
644                             opnum, type, 0, 0);
645     }
646
647   secondary_memlocs_elim[(int) mode][opnum] = loc;
648   if (secondary_memlocs_elim_used <= (int)mode)
649     secondary_memlocs_elim_used = (int)mode + 1;
650   return loc;
651 }
652
653 /* Clear any secondary memory locations we've made.  */
654
655 void
656 clear_secondary_mem (void)
657 {
658   memset (secondary_memlocs, 0, sizeof secondary_memlocs);
659 }
660 #endif /* SECONDARY_MEMORY_NEEDED */
661 \f
662
663 /* Find the largest class which has at least one register valid in
664    mode INNER, and which for every such register, that register number
665    plus N is also valid in OUTER (if in range) and is cheap to move
666    into REGNO.  Such a class must exist.  */
667
668 static enum reg_class
669 find_valid_class (enum machine_mode outer ATTRIBUTE_UNUSED,
670                   enum machine_mode inner ATTRIBUTE_UNUSED, int n,
671                   unsigned int dest_regno ATTRIBUTE_UNUSED)
672 {
673   int best_cost = -1;
674   int rclass;
675   int regno;
676   enum reg_class best_class = NO_REGS;
677   enum reg_class dest_class ATTRIBUTE_UNUSED = REGNO_REG_CLASS (dest_regno);
678   unsigned int best_size = 0;
679   int cost;
680
681   for (rclass = 1; rclass < N_REG_CLASSES; rclass++)
682     {
683       int bad = 0;
684       int good = 0;
685       for (regno = 0; regno < FIRST_PSEUDO_REGISTER - n && ! bad; regno++)
686         if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno))
687           {
688             if (HARD_REGNO_MODE_OK (regno, inner))
689               {
690                 good = 1;
691                 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], regno + n)
692                     || ! HARD_REGNO_MODE_OK (regno + n, outer))
693                   bad = 1;
694               }
695           }
696
697       if (bad || !good)
698         continue;
699       cost = register_move_cost (outer, (enum reg_class) rclass, dest_class);
700
701       if ((reg_class_size[rclass] > best_size
702            && (best_cost < 0 || best_cost >= cost))
703           || best_cost > cost)
704         {
705           best_class = (enum reg_class) rclass;
706           best_size = reg_class_size[rclass];
707           best_cost = register_move_cost (outer, (enum reg_class) rclass,
708                                           dest_class);
709         }
710     }
711
712   gcc_assert (best_size != 0);
713
714   return best_class;
715 }
716 \f
717 /* Return the number of a previously made reload that can be combined with
718    a new one, or n_reloads if none of the existing reloads can be used.
719    OUT, RCLASS, TYPE and OPNUM are the same arguments as passed to
720    push_reload, they determine the kind of the new reload that we try to
721    combine.  P_IN points to the corresponding value of IN, which can be
722    modified by this function.
723    DONT_SHARE is nonzero if we can't share any input-only reload for IN.  */
724
725 static int
726 find_reusable_reload (rtx *p_in, rtx out, enum reg_class rclass,
727                       enum reload_type type, int opnum, int dont_share)
728 {
729   rtx in = *p_in;
730   int i;
731   /* We can't merge two reloads if the output of either one is
732      earlyclobbered.  */
733
734   if (earlyclobber_operand_p (out))
735     return n_reloads;
736
737   /* We can use an existing reload if the class is right
738      and at least one of IN and OUT is a match
739      and the other is at worst neutral.
740      (A zero compared against anything is neutral.)
741
742      For targets with small register classes, don't use existing reloads
743      unless they are for the same thing since that can cause us to need
744      more reload registers than we otherwise would.  */
745
746   for (i = 0; i < n_reloads; i++)
747     if ((reg_class_subset_p (rclass, rld[i].rclass)
748          || reg_class_subset_p (rld[i].rclass, rclass))
749         /* If the existing reload has a register, it must fit our class.  */
750         && (rld[i].reg_rtx == 0
751             || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
752                                   true_regnum (rld[i].reg_rtx)))
753         && ((in != 0 && MATCHES (rld[i].in, in) && ! dont_share
754              && (out == 0 || rld[i].out == 0 || MATCHES (rld[i].out, out)))
755             || (out != 0 && MATCHES (rld[i].out, out)
756                 && (in == 0 || rld[i].in == 0 || MATCHES (rld[i].in, in))))
757         && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
758         && (small_register_class_p (rclass)
759             || targetm.small_register_classes_for_mode_p (VOIDmode))
760         && MERGABLE_RELOADS (type, rld[i].when_needed, opnum, rld[i].opnum))
761       return i;
762
763   /* Reloading a plain reg for input can match a reload to postincrement
764      that reg, since the postincrement's value is the right value.
765      Likewise, it can match a preincrement reload, since we regard
766      the preincrementation as happening before any ref in this insn
767      to that register.  */
768   for (i = 0; i < n_reloads; i++)
769     if ((reg_class_subset_p (rclass, rld[i].rclass)
770          || reg_class_subset_p (rld[i].rclass, rclass))
771         /* If the existing reload has a register, it must fit our
772            class.  */
773         && (rld[i].reg_rtx == 0
774             || TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
775                                   true_regnum (rld[i].reg_rtx)))
776         && out == 0 && rld[i].out == 0 && rld[i].in != 0
777         && ((REG_P (in)
778              && GET_RTX_CLASS (GET_CODE (rld[i].in)) == RTX_AUTOINC
779              && MATCHES (XEXP (rld[i].in, 0), in))
780             || (REG_P (rld[i].in)
781                 && GET_RTX_CLASS (GET_CODE (in)) == RTX_AUTOINC
782                 && MATCHES (XEXP (in, 0), rld[i].in)))
783         && (rld[i].out == 0 || ! earlyclobber_operand_p (rld[i].out))
784         && (small_register_class_p (rclass)
785             || targetm.small_register_classes_for_mode_p (VOIDmode))
786         && MERGABLE_RELOADS (type, rld[i].when_needed,
787                              opnum, rld[i].opnum))
788       {
789         /* Make sure reload_in ultimately has the increment,
790            not the plain register.  */
791         if (REG_P (in))
792           *p_in = rld[i].in;
793         return i;
794       }
795   return n_reloads;
796 }
797
798 /* Return nonzero if X is a SUBREG which will require reloading of its
799    SUBREG_REG expression.  */
800
801 static int
802 reload_inner_reg_of_subreg (rtx x, enum machine_mode mode, int output)
803 {
804   rtx inner;
805
806   /* Only SUBREGs are problematical.  */
807   if (GET_CODE (x) != SUBREG)
808     return 0;
809
810   inner = SUBREG_REG (x);
811
812   /* If INNER is a constant or PLUS, then INNER must be reloaded.  */
813   if (CONSTANT_P (inner) || GET_CODE (inner) == PLUS)
814     return 1;
815
816   /* If INNER is not a hard register, then INNER will not need to
817      be reloaded.  */
818   if (!REG_P (inner)
819       || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
820     return 0;
821
822   /* If INNER is not ok for MODE, then INNER will need reloading.  */
823   if (! HARD_REGNO_MODE_OK (subreg_regno (x), mode))
824     return 1;
825
826   /* If the outer part is a word or smaller, INNER larger than a
827      word and the number of regs for INNER is not the same as the
828      number of words in INNER, then INNER will need reloading.  */
829   return (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
830           && output
831           && GET_MODE_SIZE (GET_MODE (inner)) > UNITS_PER_WORD
832           && ((GET_MODE_SIZE (GET_MODE (inner)) / UNITS_PER_WORD)
833               != (int) hard_regno_nregs[REGNO (inner)][GET_MODE (inner)]));
834 }
835
836 /* Return nonzero if IN can be reloaded into REGNO with mode MODE without
837    requiring an extra reload register.  The caller has already found that
838    IN contains some reference to REGNO, so check that we can produce the
839    new value in a single step.  E.g. if we have
840    (set (reg r13) (plus (reg r13) (const int 1))), and there is an
841    instruction that adds one to a register, this should succeed.
842    However, if we have something like
843    (set (reg r13) (plus (reg r13) (const int 999))), and the constant 999
844    needs to be loaded into a register first, we need a separate reload
845    register.
846    Such PLUS reloads are generated by find_reload_address_part.
847    The out-of-range PLUS expressions are usually introduced in the instruction
848    patterns by register elimination and substituting pseudos without a home
849    by their function-invariant equivalences.  */
850 static int
851 can_reload_into (rtx in, int regno, enum machine_mode mode)
852 {
853   rtx dst, test_insn;
854   int r = 0;
855   struct recog_data save_recog_data;
856
857   /* For matching constraints, we often get notional input reloads where
858      we want to use the original register as the reload register.  I.e.
859      technically this is a non-optional input-output reload, but IN is
860      already a valid register, and has been chosen as the reload register.
861      Speed this up, since it trivially works.  */
862   if (REG_P (in))
863     return 1;
864
865   /* To test MEMs properly, we'd have to take into account all the reloads
866      that are already scheduled, which can become quite complicated.
867      And since we've already handled address reloads for this MEM, it
868      should always succeed anyway.  */
869   if (MEM_P (in))
870     return 1;
871
872   /* If we can make a simple SET insn that does the job, everything should
873      be fine.  */
874   dst =  gen_rtx_REG (mode, regno);
875   test_insn = make_insn_raw (gen_rtx_SET (VOIDmode, dst, in));
876   save_recog_data = recog_data;
877   if (recog_memoized (test_insn) >= 0)
878     {
879       extract_insn (test_insn);
880       r = constrain_operands (1);
881     }
882   recog_data = save_recog_data;
883   return r;
884 }
885
886 /* Record one reload that needs to be performed.
887    IN is an rtx saying where the data are to be found before this instruction.
888    OUT says where they must be stored after the instruction.
889    (IN is zero for data not read, and OUT is zero for data not written.)
890    INLOC and OUTLOC point to the places in the instructions where
891    IN and OUT were found.
892    If IN and OUT are both nonzero, it means the same register must be used
893    to reload both IN and OUT.
894
895    RCLASS is a register class required for the reloaded data.
896    INMODE is the machine mode that the instruction requires
897    for the reg that replaces IN and OUTMODE is likewise for OUT.
898
899    If IN is zero, then OUT's location and mode should be passed as
900    INLOC and INMODE.
901
902    STRICT_LOW is the 1 if there is a containing STRICT_LOW_PART rtx.
903
904    OPTIONAL nonzero means this reload does not need to be performed:
905    it can be discarded if that is more convenient.
906
907    OPNUM and TYPE say what the purpose of this reload is.
908
909    The return value is the reload-number for this reload.
910
911    If both IN and OUT are nonzero, in some rare cases we might
912    want to make two separate reloads.  (Actually we never do this now.)
913    Therefore, the reload-number for OUT is stored in
914    output_reloadnum when we return; the return value applies to IN.
915    Usually (presently always), when IN and OUT are nonzero,
916    the two reload-numbers are equal, but the caller should be careful to
917    distinguish them.  */
918
919 int
920 push_reload (rtx in, rtx out, rtx *inloc, rtx *outloc,
921              enum reg_class rclass, enum machine_mode inmode,
922              enum machine_mode outmode, int strict_low, int optional,
923              int opnum, enum reload_type type)
924 {
925   int i;
926   int dont_share = 0;
927   int dont_remove_subreg = 0;
928   rtx *in_subreg_loc = 0, *out_subreg_loc = 0;
929   int secondary_in_reload = -1, secondary_out_reload = -1;
930   enum insn_code secondary_in_icode = CODE_FOR_nothing;
931   enum insn_code secondary_out_icode = CODE_FOR_nothing;
932
933   /* INMODE and/or OUTMODE could be VOIDmode if no mode
934      has been specified for the operand.  In that case,
935      use the operand's mode as the mode to reload.  */
936   if (inmode == VOIDmode && in != 0)
937     inmode = GET_MODE (in);
938   if (outmode == VOIDmode && out != 0)
939     outmode = GET_MODE (out);
940
941   /* If find_reloads and friends until now missed to replace a pseudo
942      with a constant of reg_equiv_constant something went wrong
943      beforehand.
944      Note that it can't simply be done here if we missed it earlier
945      since the constant might need to be pushed into the literal pool
946      and the resulting memref would probably need further
947      reloading.  */
948   if (in != 0 && REG_P (in))
949     {
950       int regno = REGNO (in);
951
952       gcc_assert (regno < FIRST_PSEUDO_REGISTER
953                   || reg_renumber[regno] >= 0
954                   || reg_equiv_constant (regno) == NULL_RTX);
955     }
956
957   /* reg_equiv_constant only contains constants which are obviously
958      not appropriate as destination.  So if we would need to replace
959      the destination pseudo with a constant we are in real
960      trouble.  */
961   if (out != 0 && REG_P (out))
962     {
963       int regno = REGNO (out);
964
965       gcc_assert (regno < FIRST_PSEUDO_REGISTER
966                   || reg_renumber[regno] >= 0
967                   || reg_equiv_constant (regno) == NULL_RTX);
968     }
969
970   /* If we have a read-write operand with an address side-effect,
971      change either IN or OUT so the side-effect happens only once.  */
972   if (in != 0 && out != 0 && MEM_P (in) && rtx_equal_p (in, out))
973     switch (GET_CODE (XEXP (in, 0)))
974       {
975       case POST_INC: case POST_DEC:   case POST_MODIFY:
976         in = replace_equiv_address_nv (in, XEXP (XEXP (in, 0), 0));
977         break;
978
979       case PRE_INC: case PRE_DEC: case PRE_MODIFY:
980         out = replace_equiv_address_nv (out, XEXP (XEXP (out, 0), 0));
981         break;
982
983       default:
984         break;
985       }
986
987   /* If we are reloading a (SUBREG constant ...), really reload just the
988      inside expression in its own mode.  Similarly for (SUBREG (PLUS ...)).
989      If we have (SUBREG:M1 (MEM:M2 ...) ...) (or an inner REG that is still
990      a pseudo and hence will become a MEM) with M1 wider than M2 and the
991      register is a pseudo, also reload the inside expression.
992      For machines that extend byte loads, do this for any SUBREG of a pseudo
993      where both M1 and M2 are a word or smaller, M1 is wider than M2, and
994      M2 is an integral mode that gets extended when loaded.
995      Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R where
996      either M1 is not valid for R or M2 is wider than a word but we only
997      need one word to store an M2-sized quantity in R.
998      (However, if OUT is nonzero, we need to reload the reg *and*
999      the subreg, so do nothing here, and let following statement handle it.)
1000
1001      Note that the case of (SUBREG (CONST_INT...)...) is handled elsewhere;
1002      we can't handle it here because CONST_INT does not indicate a mode.
1003
1004      Similarly, we must reload the inside expression if we have a
1005      STRICT_LOW_PART (presumably, in == out in this case).
1006
1007      Also reload the inner expression if it does not require a secondary
1008      reload but the SUBREG does.
1009
1010      Finally, reload the inner expression if it is a register that is in
1011      the class whose registers cannot be referenced in a different size
1012      and M1 is not the same size as M2.  If subreg_lowpart_p is false, we
1013      cannot reload just the inside since we might end up with the wrong
1014      register class.  But if it is inside a STRICT_LOW_PART, we have
1015      no choice, so we hope we do get the right register class there.  */
1016
1017   if (in != 0 && GET_CODE (in) == SUBREG
1018       && (subreg_lowpart_p (in) || strict_low)
1019 #ifdef CANNOT_CHANGE_MODE_CLASS
1020       && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (in)), inmode, rclass)
1021 #endif
1022       && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (in))]
1023       && (CONSTANT_P (SUBREG_REG (in))
1024           || GET_CODE (SUBREG_REG (in)) == PLUS
1025           || strict_low
1026           || (((REG_P (SUBREG_REG (in))
1027                 && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER)
1028                || MEM_P (SUBREG_REG (in)))
1029               && ((GET_MODE_SIZE (inmode)
1030                    > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
1031 #ifdef LOAD_EXTEND_OP
1032                   || (GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1033                       && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1034                           <= UNITS_PER_WORD)
1035                       && (GET_MODE_SIZE (inmode)
1036                           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
1037                       && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (in)))
1038                       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (in))) != UNKNOWN)
1039 #endif
1040 #ifdef WORD_REGISTER_OPERATIONS
1041                   || ((GET_MODE_SIZE (inmode)
1042                        < GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
1043                       && ((GET_MODE_SIZE (inmode) - 1) / UNITS_PER_WORD ==
1044                           ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))) - 1)
1045                            / UNITS_PER_WORD)))
1046 #endif
1047                   ))
1048           || (REG_P (SUBREG_REG (in))
1049               && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1050               /* The case where out is nonzero
1051                  is handled differently in the following statement.  */
1052               && (out == 0 || subreg_lowpart_p (in))
1053               && ((GET_MODE_SIZE (inmode) <= UNITS_PER_WORD
1054                    && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1055                        > UNITS_PER_WORD)
1056                    && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1057                         / UNITS_PER_WORD)
1058                        != (int) hard_regno_nregs[REGNO (SUBREG_REG (in))]
1059                                                 [GET_MODE (SUBREG_REG (in))]))
1060                   || ! HARD_REGNO_MODE_OK (subreg_regno (in), inmode)))
1061           || (secondary_reload_class (1, rclass, inmode, in) != NO_REGS
1062               && (secondary_reload_class (1, rclass, GET_MODE (SUBREG_REG (in)),
1063                                           SUBREG_REG (in))
1064                   == NO_REGS))
1065 #ifdef CANNOT_CHANGE_MODE_CLASS
1066           || (REG_P (SUBREG_REG (in))
1067               && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1068               && REG_CANNOT_CHANGE_MODE_P
1069               (REGNO (SUBREG_REG (in)), GET_MODE (SUBREG_REG (in)), inmode))
1070 #endif
1071           ))
1072     {
1073       in_subreg_loc = inloc;
1074       inloc = &SUBREG_REG (in);
1075       in = *inloc;
1076 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1077       if (MEM_P (in))
1078         /* This is supposed to happen only for paradoxical subregs made by
1079            combine.c.  (SUBREG (MEM)) isn't supposed to occur other ways.  */
1080         gcc_assert (GET_MODE_SIZE (GET_MODE (in)) <= GET_MODE_SIZE (inmode));
1081 #endif
1082       inmode = GET_MODE (in);
1083     }
1084
1085   /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R where
1086      either M1 is not valid for R or M2 is wider than a word but we only
1087      need one word to store an M2-sized quantity in R.
1088
1089      However, we must reload the inner reg *as well as* the subreg in
1090      that case.  */
1091
1092   /* Similar issue for (SUBREG constant ...) if it was not handled by the
1093      code above.  This can happen if SUBREG_BYTE != 0.  */
1094
1095   if (in != 0 && reload_inner_reg_of_subreg (in, inmode, 0))
1096     {
1097       enum reg_class in_class = rclass;
1098
1099       if (REG_P (SUBREG_REG (in)))
1100         in_class
1101           = find_valid_class (inmode, GET_MODE (SUBREG_REG (in)),
1102                               subreg_regno_offset (REGNO (SUBREG_REG (in)),
1103                                                    GET_MODE (SUBREG_REG (in)),
1104                                                    SUBREG_BYTE (in),
1105                                                    GET_MODE (in)),
1106                               REGNO (SUBREG_REG (in)));
1107
1108       /* This relies on the fact that emit_reload_insns outputs the
1109          instructions for input reloads of type RELOAD_OTHER in the same
1110          order as the reloads.  Thus if the outer reload is also of type
1111          RELOAD_OTHER, we are guaranteed that this inner reload will be
1112          output before the outer reload.  */
1113       push_reload (SUBREG_REG (in), NULL_RTX, &SUBREG_REG (in), (rtx *) 0,
1114                    in_class, VOIDmode, VOIDmode, 0, 0, opnum, type);
1115       dont_remove_subreg = 1;
1116     }
1117
1118   /* Similarly for paradoxical and problematical SUBREGs on the output.
1119      Note that there is no reason we need worry about the previous value
1120      of SUBREG_REG (out); even if wider than out,
1121      storing in a subreg is entitled to clobber it all
1122      (except in the case of STRICT_LOW_PART,
1123      and in that case the constraint should label it input-output.)  */
1124   if (out != 0 && GET_CODE (out) == SUBREG
1125       && (subreg_lowpart_p (out) || strict_low)
1126 #ifdef CANNOT_CHANGE_MODE_CLASS
1127       && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SUBREG_REG (out)), outmode, rclass)
1128 #endif
1129       && contains_reg_of_mode[(int) rclass][(int) GET_MODE (SUBREG_REG (out))]
1130       && (CONSTANT_P (SUBREG_REG (out))
1131           || strict_low
1132           || (((REG_P (SUBREG_REG (out))
1133                 && REGNO (SUBREG_REG (out)) >= FIRST_PSEUDO_REGISTER)
1134                || MEM_P (SUBREG_REG (out)))
1135               && ((GET_MODE_SIZE (outmode)
1136                    > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
1137 #ifdef WORD_REGISTER_OPERATIONS
1138                   || ((GET_MODE_SIZE (outmode)
1139                        < GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
1140                       && ((GET_MODE_SIZE (outmode) - 1) / UNITS_PER_WORD ==
1141                           ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))) - 1)
1142                            / UNITS_PER_WORD)))
1143 #endif
1144                   ))
1145           || (REG_P (SUBREG_REG (out))
1146               && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1147               && ((GET_MODE_SIZE (outmode) <= UNITS_PER_WORD
1148                    && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (out)))
1149                        > UNITS_PER_WORD)
1150                    && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (out)))
1151                         / UNITS_PER_WORD)
1152                        != (int) hard_regno_nregs[REGNO (SUBREG_REG (out))]
1153                                                 [GET_MODE (SUBREG_REG (out))]))
1154                   || ! HARD_REGNO_MODE_OK (subreg_regno (out), outmode)))
1155           || (secondary_reload_class (0, rclass, outmode, out) != NO_REGS
1156               && (secondary_reload_class (0, rclass, GET_MODE (SUBREG_REG (out)),
1157                                           SUBREG_REG (out))
1158                   == NO_REGS))
1159 #ifdef CANNOT_CHANGE_MODE_CLASS
1160           || (REG_P (SUBREG_REG (out))
1161               && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1162               && REG_CANNOT_CHANGE_MODE_P (REGNO (SUBREG_REG (out)),
1163                                            GET_MODE (SUBREG_REG (out)),
1164                                            outmode))
1165 #endif
1166           ))
1167     {
1168       out_subreg_loc = outloc;
1169       outloc = &SUBREG_REG (out);
1170       out = *outloc;
1171 #if ! defined (LOAD_EXTEND_OP) && ! defined (WORD_REGISTER_OPERATIONS)
1172       gcc_assert (!MEM_P (out)
1173                   || GET_MODE_SIZE (GET_MODE (out))
1174                      <= GET_MODE_SIZE (outmode));
1175 #endif
1176       outmode = GET_MODE (out);
1177     }
1178
1179   /* Similar issue for (SUBREG:M1 (REG:M2 ...) ...) for a hard register R where
1180      either M1 is not valid for R or M2 is wider than a word but we only
1181      need one word to store an M2-sized quantity in R.
1182
1183      However, we must reload the inner reg *as well as* the subreg in
1184      that case.  In this case, the inner reg is an in-out reload.  */
1185
1186   if (out != 0 && reload_inner_reg_of_subreg (out, outmode, 1))
1187     {
1188       /* This relies on the fact that emit_reload_insns outputs the
1189          instructions for output reloads of type RELOAD_OTHER in reverse
1190          order of the reloads.  Thus if the outer reload is also of type
1191          RELOAD_OTHER, we are guaranteed that this inner reload will be
1192          output after the outer reload.  */
1193       dont_remove_subreg = 1;
1194       push_reload (SUBREG_REG (out), SUBREG_REG (out), &SUBREG_REG (out),
1195                    &SUBREG_REG (out),
1196                    find_valid_class (outmode, GET_MODE (SUBREG_REG (out)),
1197                                      subreg_regno_offset (REGNO (SUBREG_REG (out)),
1198                                                           GET_MODE (SUBREG_REG (out)),
1199                                                           SUBREG_BYTE (out),
1200                                                           GET_MODE (out)),
1201                                      REGNO (SUBREG_REG (out))),
1202                    VOIDmode, VOIDmode, 0, 0,
1203                    opnum, RELOAD_OTHER);
1204     }
1205
1206   /* If IN appears in OUT, we can't share any input-only reload for IN.  */
1207   if (in != 0 && out != 0 && MEM_P (out)
1208       && (REG_P (in) || MEM_P (in) || GET_CODE (in) == PLUS)
1209       && reg_overlap_mentioned_for_reload_p (in, XEXP (out, 0)))
1210     dont_share = 1;
1211
1212   /* If IN is a SUBREG of a hard register, make a new REG.  This
1213      simplifies some of the cases below.  */
1214
1215   if (in != 0 && GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))
1216       && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER
1217       && ! dont_remove_subreg)
1218     in = gen_rtx_REG (GET_MODE (in), subreg_regno (in));
1219
1220   /* Similarly for OUT.  */
1221   if (out != 0 && GET_CODE (out) == SUBREG
1222       && REG_P (SUBREG_REG (out))
1223       && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER
1224       && ! dont_remove_subreg)
1225     out = gen_rtx_REG (GET_MODE (out), subreg_regno (out));
1226
1227   /* Narrow down the class of register wanted if that is
1228      desirable on this machine for efficiency.  */
1229   {
1230     reg_class_t preferred_class = rclass;
1231
1232     if (in != 0)
1233       preferred_class = targetm.preferred_reload_class (in, rclass);
1234
1235     /* Output reloads may need analogous treatment, different in detail.  */
1236     if (out != 0)
1237       preferred_class
1238         = targetm.preferred_output_reload_class (out, preferred_class);
1239
1240     /* Discard what the target said if we cannot do it.  */
1241     if (preferred_class != NO_REGS
1242         || (optional && type == RELOAD_FOR_OUTPUT))
1243       rclass = (enum reg_class) preferred_class;
1244   }
1245
1246   /* Make sure we use a class that can handle the actual pseudo
1247      inside any subreg.  For example, on the 386, QImode regs
1248      can appear within SImode subregs.  Although GENERAL_REGS
1249      can handle SImode, QImode needs a smaller class.  */
1250 #ifdef LIMIT_RELOAD_CLASS
1251   if (in_subreg_loc)
1252     rclass = LIMIT_RELOAD_CLASS (inmode, rclass);
1253   else if (in != 0 && GET_CODE (in) == SUBREG)
1254     rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (in)), rclass);
1255
1256   if (out_subreg_loc)
1257     rclass = LIMIT_RELOAD_CLASS (outmode, rclass);
1258   if (out != 0 && GET_CODE (out) == SUBREG)
1259     rclass = LIMIT_RELOAD_CLASS (GET_MODE (SUBREG_REG (out)), rclass);
1260 #endif
1261
1262   /* Verify that this class is at least possible for the mode that
1263      is specified.  */
1264   if (this_insn_is_asm)
1265     {
1266       enum machine_mode mode;
1267       if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
1268         mode = inmode;
1269       else
1270         mode = outmode;
1271       if (mode == VOIDmode)
1272         {
1273           error_for_asm (this_insn, "cannot reload integer constant "
1274                          "operand in %<asm%>");
1275           mode = word_mode;
1276           if (in != 0)
1277             inmode = word_mode;
1278           if (out != 0)
1279             outmode = word_mode;
1280         }
1281       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1282         if (HARD_REGNO_MODE_OK (i, mode)
1283             && in_hard_reg_set_p (reg_class_contents[(int) rclass], mode, i))
1284           break;
1285       if (i == FIRST_PSEUDO_REGISTER)
1286         {
1287           error_for_asm (this_insn, "impossible register constraint "
1288                          "in %<asm%>");
1289           /* Avoid further trouble with this insn.  */
1290           PATTERN (this_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
1291           /* We used to continue here setting class to ALL_REGS, but it triggers
1292              sanity check on i386 for:
1293              void foo(long double d)
1294              {
1295                asm("" :: "a" (d));
1296              }
1297              Returning zero here ought to be safe as we take care in
1298              find_reloads to not process the reloads when instruction was
1299              replaced by USE.  */
1300
1301           return 0;
1302         }
1303     }
1304
1305   /* Optional output reloads are always OK even if we have no register class,
1306      since the function of these reloads is only to have spill_reg_store etc.
1307      set, so that the storing insn can be deleted later.  */
1308   gcc_assert (rclass != NO_REGS
1309               || (optional != 0 && type == RELOAD_FOR_OUTPUT));
1310
1311   i = find_reusable_reload (&in, out, rclass, type, opnum, dont_share);
1312
1313   if (i == n_reloads)
1314     {
1315       /* See if we need a secondary reload register to move between CLASS
1316          and IN or CLASS and OUT.  Get the icode and push any required reloads
1317          needed for each of them if so.  */
1318
1319       if (in != 0)
1320         secondary_in_reload
1321           = push_secondary_reload (1, in, opnum, optional, rclass, inmode, type,
1322                                    &secondary_in_icode, NULL);
1323       if (out != 0 && GET_CODE (out) != SCRATCH)
1324         secondary_out_reload
1325           = push_secondary_reload (0, out, opnum, optional, rclass, outmode,
1326                                    type, &secondary_out_icode, NULL);
1327
1328       /* We found no existing reload suitable for re-use.
1329          So add an additional reload.  */
1330
1331 #ifdef SECONDARY_MEMORY_NEEDED
1332       /* If a memory location is needed for the copy, make one.  */
1333       if (in != 0
1334           && (REG_P (in)
1335               || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
1336           && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
1337           && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
1338                                       rclass, inmode))
1339         get_secondary_mem (in, inmode, opnum, type);
1340 #endif
1341
1342       i = n_reloads;
1343       rld[i].in = in;
1344       rld[i].out = out;
1345       rld[i].rclass = rclass;
1346       rld[i].inmode = inmode;
1347       rld[i].outmode = outmode;
1348       rld[i].reg_rtx = 0;
1349       rld[i].optional = optional;
1350       rld[i].inc = 0;
1351       rld[i].nocombine = 0;
1352       rld[i].in_reg = inloc ? *inloc : 0;
1353       rld[i].out_reg = outloc ? *outloc : 0;
1354       rld[i].opnum = opnum;
1355       rld[i].when_needed = type;
1356       rld[i].secondary_in_reload = secondary_in_reload;
1357       rld[i].secondary_out_reload = secondary_out_reload;
1358       rld[i].secondary_in_icode = secondary_in_icode;
1359       rld[i].secondary_out_icode = secondary_out_icode;
1360       rld[i].secondary_p = 0;
1361
1362       n_reloads++;
1363
1364 #ifdef SECONDARY_MEMORY_NEEDED
1365       if (out != 0
1366           && (REG_P (out)
1367               || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
1368           && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
1369           && SECONDARY_MEMORY_NEEDED (rclass,
1370                                       REGNO_REG_CLASS (reg_or_subregno (out)),
1371                                       outmode))
1372         get_secondary_mem (out, outmode, opnum, type);
1373 #endif
1374     }
1375   else
1376     {
1377       /* We are reusing an existing reload,
1378          but we may have additional information for it.
1379          For example, we may now have both IN and OUT
1380          while the old one may have just one of them.  */
1381
1382       /* The modes can be different.  If they are, we want to reload in
1383          the larger mode, so that the value is valid for both modes.  */
1384       if (inmode != VOIDmode
1385           && GET_MODE_SIZE (inmode) > GET_MODE_SIZE (rld[i].inmode))
1386         rld[i].inmode = inmode;
1387       if (outmode != VOIDmode
1388           && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (rld[i].outmode))
1389         rld[i].outmode = outmode;
1390       if (in != 0)
1391         {
1392           rtx in_reg = inloc ? *inloc : 0;
1393           /* If we merge reloads for two distinct rtl expressions that
1394              are identical in content, there might be duplicate address
1395              reloads.  Remove the extra set now, so that if we later find
1396              that we can inherit this reload, we can get rid of the
1397              address reloads altogether.
1398
1399              Do not do this if both reloads are optional since the result
1400              would be an optional reload which could potentially leave
1401              unresolved address replacements.
1402
1403              It is not sufficient to call transfer_replacements since
1404              choose_reload_regs will remove the replacements for address
1405              reloads of inherited reloads which results in the same
1406              problem.  */
1407           if (rld[i].in != in && rtx_equal_p (in, rld[i].in)
1408               && ! (rld[i].optional && optional))
1409             {
1410               /* We must keep the address reload with the lower operand
1411                  number alive.  */
1412               if (opnum > rld[i].opnum)
1413                 {
1414                   remove_address_replacements (in);
1415                   in = rld[i].in;
1416                   in_reg = rld[i].in_reg;
1417                 }
1418               else
1419                 remove_address_replacements (rld[i].in);
1420             }
1421           /* When emitting reloads we don't necessarily look at the in-
1422              and outmode, but also directly at the operands (in and out).
1423              So we can't simply overwrite them with whatever we have found
1424              for this (to-be-merged) reload, we have to "merge" that too.
1425              Reusing another reload already verified that we deal with the
1426              same operands, just possibly in different modes.  So we
1427              overwrite the operands only when the new mode is larger.
1428              See also PR33613.  */
1429           if (!rld[i].in
1430               || GET_MODE_SIZE (GET_MODE (in))
1431                    > GET_MODE_SIZE (GET_MODE (rld[i].in)))
1432             rld[i].in = in;
1433           if (!rld[i].in_reg
1434               || (in_reg
1435                   && GET_MODE_SIZE (GET_MODE (in_reg))
1436                      > GET_MODE_SIZE (GET_MODE (rld[i].in_reg))))
1437             rld[i].in_reg = in_reg;
1438         }
1439       if (out != 0)
1440         {
1441           if (!rld[i].out
1442               || (out
1443                   && GET_MODE_SIZE (GET_MODE (out))
1444                      > GET_MODE_SIZE (GET_MODE (rld[i].out))))
1445             rld[i].out = out;
1446           if (outloc
1447               && (!rld[i].out_reg
1448                   || GET_MODE_SIZE (GET_MODE (*outloc))
1449                      > GET_MODE_SIZE (GET_MODE (rld[i].out_reg))))
1450             rld[i].out_reg = *outloc;
1451         }
1452       if (reg_class_subset_p (rclass, rld[i].rclass))
1453         rld[i].rclass = rclass;
1454       rld[i].optional &= optional;
1455       if (MERGE_TO_OTHER (type, rld[i].when_needed,
1456                           opnum, rld[i].opnum))
1457         rld[i].when_needed = RELOAD_OTHER;
1458       rld[i].opnum = MIN (rld[i].opnum, opnum);
1459     }
1460
1461   /* If the ostensible rtx being reloaded differs from the rtx found
1462      in the location to substitute, this reload is not safe to combine
1463      because we cannot reliably tell whether it appears in the insn.  */
1464
1465   if (in != 0 && in != *inloc)
1466     rld[i].nocombine = 1;
1467
1468 #if 0
1469   /* This was replaced by changes in find_reloads_address_1 and the new
1470      function inc_for_reload, which go with a new meaning of reload_inc.  */
1471
1472   /* If this is an IN/OUT reload in an insn that sets the CC,
1473      it must be for an autoincrement.  It doesn't work to store
1474      the incremented value after the insn because that would clobber the CC.
1475      So we must do the increment of the value reloaded from,
1476      increment it, store it back, then decrement again.  */
1477   if (out != 0 && sets_cc0_p (PATTERN (this_insn)))
1478     {
1479       out = 0;
1480       rld[i].out = 0;
1481       rld[i].inc = find_inc_amount (PATTERN (this_insn), in);
1482       /* If we did not find a nonzero amount-to-increment-by,
1483          that contradicts the belief that IN is being incremented
1484          in an address in this insn.  */
1485       gcc_assert (rld[i].inc != 0);
1486     }
1487 #endif
1488
1489   /* If we will replace IN and OUT with the reload-reg,
1490      record where they are located so that substitution need
1491      not do a tree walk.  */
1492
1493   if (replace_reloads)
1494     {
1495       if (inloc != 0)
1496         {
1497           struct replacement *r = &replacements[n_replacements++];
1498           r->what = i;
1499           r->subreg_loc = in_subreg_loc;
1500           r->where = inloc;
1501           r->mode = inmode;
1502         }
1503       if (outloc != 0 && outloc != inloc)
1504         {
1505           struct replacement *r = &replacements[n_replacements++];
1506           r->what = i;
1507           r->where = outloc;
1508           r->subreg_loc = out_subreg_loc;
1509           r->mode = outmode;
1510         }
1511     }
1512
1513   /* If this reload is just being introduced and it has both
1514      an incoming quantity and an outgoing quantity that are
1515      supposed to be made to match, see if either one of the two
1516      can serve as the place to reload into.
1517
1518      If one of them is acceptable, set rld[i].reg_rtx
1519      to that one.  */
1520
1521   if (in != 0 && out != 0 && in != out && rld[i].reg_rtx == 0)
1522     {
1523       rld[i].reg_rtx = find_dummy_reload (in, out, inloc, outloc,
1524                                           inmode, outmode,
1525                                           rld[i].rclass, i,
1526                                           earlyclobber_operand_p (out));
1527
1528       /* If the outgoing register already contains the same value
1529          as the incoming one, we can dispense with loading it.
1530          The easiest way to tell the caller that is to give a phony
1531          value for the incoming operand (same as outgoing one).  */
1532       if (rld[i].reg_rtx == out
1533           && (REG_P (in) || CONSTANT_P (in))
1534           && 0 != find_equiv_reg (in, this_insn, NO_REGS, REGNO (out),
1535                                   static_reload_reg_p, i, inmode))
1536         rld[i].in = out;
1537     }
1538
1539   /* If this is an input reload and the operand contains a register that
1540      dies in this insn and is used nowhere else, see if it is the right class
1541      to be used for this reload.  Use it if so.  (This occurs most commonly
1542      in the case of paradoxical SUBREGs and in-out reloads).  We cannot do
1543      this if it is also an output reload that mentions the register unless
1544      the output is a SUBREG that clobbers an entire register.
1545
1546      Note that the operand might be one of the spill regs, if it is a
1547      pseudo reg and we are in a block where spilling has not taken place.
1548      But if there is no spilling in this block, that is OK.
1549      An explicitly used hard reg cannot be a spill reg.  */
1550
1551   if (rld[i].reg_rtx == 0 && in != 0 && hard_regs_live_known)
1552     {
1553       rtx note;
1554       int regno;
1555       enum machine_mode rel_mode = inmode;
1556
1557       if (out && GET_MODE_SIZE (outmode) > GET_MODE_SIZE (inmode))
1558         rel_mode = outmode;
1559
1560       for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1561         if (REG_NOTE_KIND (note) == REG_DEAD
1562             && REG_P (XEXP (note, 0))
1563             && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1564             && reg_mentioned_p (XEXP (note, 0), in)
1565             /* Check that a former pseudo is valid; see find_dummy_reload.  */
1566             && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1567                 || (! bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR),
1568                                     ORIGINAL_REGNO (XEXP (note, 0)))
1569                     && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1))
1570             && ! refers_to_regno_for_reload_p (regno,
1571                                                end_hard_regno (rel_mode,
1572                                                                regno),
1573                                                PATTERN (this_insn), inloc)
1574             /* If this is also an output reload, IN cannot be used as
1575                the reload register if it is set in this insn unless IN
1576                is also OUT.  */
1577             && (out == 0 || in == out
1578                 || ! hard_reg_set_here_p (regno,
1579                                           end_hard_regno (rel_mode, regno),
1580                                           PATTERN (this_insn)))
1581             /* ??? Why is this code so different from the previous?
1582                Is there any simple coherent way to describe the two together?
1583                What's going on here.  */
1584             && (in != out
1585                 || (GET_CODE (in) == SUBREG
1586                     && (((GET_MODE_SIZE (GET_MODE (in)) + (UNITS_PER_WORD - 1))
1587                          / UNITS_PER_WORD)
1588                         == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (in)))
1589                              + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
1590             /* Make sure the operand fits in the reg that dies.  */
1591             && (GET_MODE_SIZE (rel_mode)
1592                 <= GET_MODE_SIZE (GET_MODE (XEXP (note, 0))))
1593             && HARD_REGNO_MODE_OK (regno, inmode)
1594             && HARD_REGNO_MODE_OK (regno, outmode))
1595           {
1596             unsigned int offs;
1597             unsigned int nregs = MAX (hard_regno_nregs[regno][inmode],
1598                                       hard_regno_nregs[regno][outmode]);
1599
1600             for (offs = 0; offs < nregs; offs++)
1601               if (fixed_regs[regno + offs]
1602                   || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
1603                                           regno + offs))
1604                 break;
1605
1606             if (offs == nregs
1607                 && (! (refers_to_regno_for_reload_p
1608                        (regno, end_hard_regno (inmode, regno), in, (rtx *) 0))
1609                     || can_reload_into (in, regno, inmode)))
1610               {
1611                 rld[i].reg_rtx = gen_rtx_REG (rel_mode, regno);
1612                 break;
1613               }
1614           }
1615     }
1616
1617   if (out)
1618     output_reloadnum = i;
1619
1620   return i;
1621 }
1622
1623 /* Record an additional place we must replace a value
1624    for which we have already recorded a reload.
1625    RELOADNUM is the value returned by push_reload
1626    when the reload was recorded.
1627    This is used in insn patterns that use match_dup.  */
1628
1629 static void
1630 push_replacement (rtx *loc, int reloadnum, enum machine_mode mode)
1631 {
1632   if (replace_reloads)
1633     {
1634       struct replacement *r = &replacements[n_replacements++];
1635       r->what = reloadnum;
1636       r->where = loc;
1637       r->subreg_loc = 0;
1638       r->mode = mode;
1639     }
1640 }
1641
1642 /* Duplicate any replacement we have recorded to apply at
1643    location ORIG_LOC to also be performed at DUP_LOC.
1644    This is used in insn patterns that use match_dup.  */
1645
1646 static void
1647 dup_replacements (rtx *dup_loc, rtx *orig_loc)
1648 {
1649   int i, n = n_replacements;
1650
1651   for (i = 0; i < n; i++)
1652     {
1653       struct replacement *r = &replacements[i];
1654       if (r->where == orig_loc)
1655         push_replacement (dup_loc, r->what, r->mode);
1656     }
1657 }
1658 \f
1659 /* Transfer all replacements that used to be in reload FROM to be in
1660    reload TO.  */
1661
1662 void
1663 transfer_replacements (int to, int from)
1664 {
1665   int i;
1666
1667   for (i = 0; i < n_replacements; i++)
1668     if (replacements[i].what == from)
1669       replacements[i].what = to;
1670 }
1671 \f
1672 /* IN_RTX is the value loaded by a reload that we now decided to inherit,
1673    or a subpart of it.  If we have any replacements registered for IN_RTX,
1674    cancel the reloads that were supposed to load them.
1675    Return nonzero if we canceled any reloads.  */
1676 int
1677 remove_address_replacements (rtx in_rtx)
1678 {
1679   int i, j;
1680   char reload_flags[MAX_RELOADS];
1681   int something_changed = 0;
1682
1683   memset (reload_flags, 0, sizeof reload_flags);
1684   for (i = 0, j = 0; i < n_replacements; i++)
1685     {
1686       if (loc_mentioned_in_p (replacements[i].where, in_rtx))
1687         reload_flags[replacements[i].what] |= 1;
1688       else
1689         {
1690           replacements[j++] = replacements[i];
1691           reload_flags[replacements[i].what] |= 2;
1692         }
1693     }
1694   /* Note that the following store must be done before the recursive calls.  */
1695   n_replacements = j;
1696
1697   for (i = n_reloads - 1; i >= 0; i--)
1698     {
1699       if (reload_flags[i] == 1)
1700         {
1701           deallocate_reload_reg (i);
1702           remove_address_replacements (rld[i].in);
1703           rld[i].in = 0;
1704           something_changed = 1;
1705         }
1706     }
1707   return something_changed;
1708 }
1709 \f
1710 /* If there is only one output reload, and it is not for an earlyclobber
1711    operand, try to combine it with a (logically unrelated) input reload
1712    to reduce the number of reload registers needed.
1713
1714    This is safe if the input reload does not appear in
1715    the value being output-reloaded, because this implies
1716    it is not needed any more once the original insn completes.
1717
1718    If that doesn't work, see we can use any of the registers that
1719    die in this insn as a reload register.  We can if it is of the right
1720    class and does not appear in the value being output-reloaded.  */
1721
1722 static void
1723 combine_reloads (void)
1724 {
1725   int i, regno;
1726   int output_reload = -1;
1727   int secondary_out = -1;
1728   rtx note;
1729
1730   /* Find the output reload; return unless there is exactly one
1731      and that one is mandatory.  */
1732
1733   for (i = 0; i < n_reloads; i++)
1734     if (rld[i].out != 0)
1735       {
1736         if (output_reload >= 0)
1737           return;
1738         output_reload = i;
1739       }
1740
1741   if (output_reload < 0 || rld[output_reload].optional)
1742     return;
1743
1744   /* An input-output reload isn't combinable.  */
1745
1746   if (rld[output_reload].in != 0)
1747     return;
1748
1749   /* If this reload is for an earlyclobber operand, we can't do anything.  */
1750   if (earlyclobber_operand_p (rld[output_reload].out))
1751     return;
1752
1753   /* If there is a reload for part of the address of this operand, we would
1754      need to change it to RELOAD_FOR_OTHER_ADDRESS.  But that would extend
1755      its life to the point where doing this combine would not lower the
1756      number of spill registers needed.  */
1757   for (i = 0; i < n_reloads; i++)
1758     if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
1759          || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
1760         && rld[i].opnum == rld[output_reload].opnum)
1761       return;
1762
1763   /* Check each input reload; can we combine it?  */
1764
1765   for (i = 0; i < n_reloads; i++)
1766     if (rld[i].in && ! rld[i].optional && ! rld[i].nocombine
1767         /* Life span of this reload must not extend past main insn.  */
1768         && rld[i].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
1769         && rld[i].when_needed != RELOAD_FOR_OUTADDR_ADDRESS
1770         && rld[i].when_needed != RELOAD_OTHER
1771         && (CLASS_MAX_NREGS (rld[i].rclass, rld[i].inmode)
1772             == CLASS_MAX_NREGS (rld[output_reload].rclass,
1773                                 rld[output_reload].outmode))
1774         && rld[i].inc == 0
1775         && rld[i].reg_rtx == 0
1776 #ifdef SECONDARY_MEMORY_NEEDED
1777         /* Don't combine two reloads with different secondary
1778            memory locations.  */
1779         && (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum] == 0
1780             || secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] == 0
1781             || rtx_equal_p (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum],
1782                             secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum]))
1783 #endif
1784         && (targetm.small_register_classes_for_mode_p (VOIDmode)
1785             ? (rld[i].rclass == rld[output_reload].rclass)
1786             : (reg_class_subset_p (rld[i].rclass,
1787                                    rld[output_reload].rclass)
1788                || reg_class_subset_p (rld[output_reload].rclass,
1789                                       rld[i].rclass)))
1790         && (MATCHES (rld[i].in, rld[output_reload].out)
1791             /* Args reversed because the first arg seems to be
1792                the one that we imagine being modified
1793                while the second is the one that might be affected.  */
1794             || (! reg_overlap_mentioned_for_reload_p (rld[output_reload].out,
1795                                                       rld[i].in)
1796                 /* However, if the input is a register that appears inside
1797                    the output, then we also can't share.
1798                    Imagine (set (mem (reg 69)) (plus (reg 69) ...)).
1799                    If the same reload reg is used for both reg 69 and the
1800                    result to be stored in memory, then that result
1801                    will clobber the address of the memory ref.  */
1802                 && ! (REG_P (rld[i].in)
1803                       && reg_overlap_mentioned_for_reload_p (rld[i].in,
1804                                                              rld[output_reload].out))))
1805         && ! reload_inner_reg_of_subreg (rld[i].in, rld[i].inmode,
1806                                          rld[i].when_needed != RELOAD_FOR_INPUT)
1807         && (reg_class_size[(int) rld[i].rclass]
1808             || targetm.small_register_classes_for_mode_p (VOIDmode))
1809         /* We will allow making things slightly worse by combining an
1810            input and an output, but no worse than that.  */
1811         && (rld[i].when_needed == RELOAD_FOR_INPUT
1812             || rld[i].when_needed == RELOAD_FOR_OUTPUT))
1813       {
1814         int j;
1815
1816         /* We have found a reload to combine with!  */
1817         rld[i].out = rld[output_reload].out;
1818         rld[i].out_reg = rld[output_reload].out_reg;
1819         rld[i].outmode = rld[output_reload].outmode;
1820         /* Mark the old output reload as inoperative.  */
1821         rld[output_reload].out = 0;
1822         /* The combined reload is needed for the entire insn.  */
1823         rld[i].when_needed = RELOAD_OTHER;
1824         /* If the output reload had a secondary reload, copy it.  */
1825         if (rld[output_reload].secondary_out_reload != -1)
1826           {
1827             rld[i].secondary_out_reload
1828               = rld[output_reload].secondary_out_reload;
1829             rld[i].secondary_out_icode
1830               = rld[output_reload].secondary_out_icode;
1831           }
1832
1833 #ifdef SECONDARY_MEMORY_NEEDED
1834         /* Copy any secondary MEM.  */
1835         if (secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum] != 0)
1836           secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[i].opnum]
1837             = secondary_memlocs_elim[(int) rld[output_reload].outmode][rld[output_reload].opnum];
1838 #endif
1839         /* If required, minimize the register class.  */
1840         if (reg_class_subset_p (rld[output_reload].rclass,
1841                                 rld[i].rclass))
1842           rld[i].rclass = rld[output_reload].rclass;
1843
1844         /* Transfer all replacements from the old reload to the combined.  */
1845         for (j = 0; j < n_replacements; j++)
1846           if (replacements[j].what == output_reload)
1847             replacements[j].what = i;
1848
1849         return;
1850       }
1851
1852   /* If this insn has only one operand that is modified or written (assumed
1853      to be the first),  it must be the one corresponding to this reload.  It
1854      is safe to use anything that dies in this insn for that output provided
1855      that it does not occur in the output (we already know it isn't an
1856      earlyclobber.  If this is an asm insn, give up.  */
1857
1858   if (INSN_CODE (this_insn) == -1)
1859     return;
1860
1861   for (i = 1; i < insn_data[INSN_CODE (this_insn)].n_operands; i++)
1862     if (insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '='
1863         || insn_data[INSN_CODE (this_insn)].operand[i].constraint[0] == '+')
1864       return;
1865
1866   /* See if some hard register that dies in this insn and is not used in
1867      the output is the right class.  Only works if the register we pick
1868      up can fully hold our output reload.  */
1869   for (note = REG_NOTES (this_insn); note; note = XEXP (note, 1))
1870     if (REG_NOTE_KIND (note) == REG_DEAD
1871         && REG_P (XEXP (note, 0))
1872         && !reg_overlap_mentioned_for_reload_p (XEXP (note, 0),
1873                                                 rld[output_reload].out)
1874         && (regno = REGNO (XEXP (note, 0))) < FIRST_PSEUDO_REGISTER
1875         && HARD_REGNO_MODE_OK (regno, rld[output_reload].outmode)
1876         && TEST_HARD_REG_BIT (reg_class_contents[(int) rld[output_reload].rclass],
1877                               regno)
1878         && (hard_regno_nregs[regno][rld[output_reload].outmode]
1879             <= hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))])
1880         /* Ensure that a secondary or tertiary reload for this output
1881            won't want this register.  */
1882         && ((secondary_out = rld[output_reload].secondary_out_reload) == -1
1883             || (!(TEST_HARD_REG_BIT
1884                   (reg_class_contents[(int) rld[secondary_out].rclass], regno))
1885                 && ((secondary_out = rld[secondary_out].secondary_out_reload) == -1
1886                     || !(TEST_HARD_REG_BIT
1887                          (reg_class_contents[(int) rld[secondary_out].rclass],
1888                           regno)))))
1889         && !fixed_regs[regno]
1890         /* Check that a former pseudo is valid; see find_dummy_reload.  */
1891         && (ORIGINAL_REGNO (XEXP (note, 0)) < FIRST_PSEUDO_REGISTER
1892             || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR),
1893                                ORIGINAL_REGNO (XEXP (note, 0)))
1894                 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] == 1)))
1895       {
1896         rld[output_reload].reg_rtx
1897           = gen_rtx_REG (rld[output_reload].outmode, regno);
1898         return;
1899       }
1900 }
1901 \f
1902 /* Try to find a reload register for an in-out reload (expressions IN and OUT).
1903    See if one of IN and OUT is a register that may be used;
1904    this is desirable since a spill-register won't be needed.
1905    If so, return the register rtx that proves acceptable.
1906
1907    INLOC and OUTLOC are locations where IN and OUT appear in the insn.
1908    RCLASS is the register class required for the reload.
1909
1910    If FOR_REAL is >= 0, it is the number of the reload,
1911    and in some cases when it can be discovered that OUT doesn't need
1912    to be computed, clear out rld[FOR_REAL].out.
1913
1914    If FOR_REAL is -1, this should not be done, because this call
1915    is just to see if a register can be found, not to find and install it.
1916
1917    EARLYCLOBBER is nonzero if OUT is an earlyclobber operand.  This
1918    puts an additional constraint on being able to use IN for OUT since
1919    IN must not appear elsewhere in the insn (it is assumed that IN itself
1920    is safe from the earlyclobber).  */
1921
1922 static rtx
1923 find_dummy_reload (rtx real_in, rtx real_out, rtx *inloc, rtx *outloc,
1924                    enum machine_mode inmode, enum machine_mode outmode,
1925                    reg_class_t rclass, int for_real, int earlyclobber)
1926 {
1927   rtx in = real_in;
1928   rtx out = real_out;
1929   int in_offset = 0;
1930   int out_offset = 0;
1931   rtx value = 0;
1932
1933   /* If operands exceed a word, we can't use either of them
1934      unless they have the same size.  */
1935   if (GET_MODE_SIZE (outmode) != GET_MODE_SIZE (inmode)
1936       && (GET_MODE_SIZE (outmode) > UNITS_PER_WORD
1937           || GET_MODE_SIZE (inmode) > UNITS_PER_WORD))
1938     return 0;
1939
1940   /* Note that {in,out}_offset are needed only when 'in' or 'out'
1941      respectively refers to a hard register.  */
1942
1943   /* Find the inside of any subregs.  */
1944   while (GET_CODE (out) == SUBREG)
1945     {
1946       if (REG_P (SUBREG_REG (out))
1947           && REGNO (SUBREG_REG (out)) < FIRST_PSEUDO_REGISTER)
1948         out_offset += subreg_regno_offset (REGNO (SUBREG_REG (out)),
1949                                            GET_MODE (SUBREG_REG (out)),
1950                                            SUBREG_BYTE (out),
1951                                            GET_MODE (out));
1952       out = SUBREG_REG (out);
1953     }
1954   while (GET_CODE (in) == SUBREG)
1955     {
1956       if (REG_P (SUBREG_REG (in))
1957           && REGNO (SUBREG_REG (in)) < FIRST_PSEUDO_REGISTER)
1958         in_offset += subreg_regno_offset (REGNO (SUBREG_REG (in)),
1959                                           GET_MODE (SUBREG_REG (in)),
1960                                           SUBREG_BYTE (in),
1961                                           GET_MODE (in));
1962       in = SUBREG_REG (in);
1963     }
1964
1965   /* Narrow down the reg class, the same way push_reload will;
1966      otherwise we might find a dummy now, but push_reload won't.  */
1967   {
1968     reg_class_t preferred_class = targetm.preferred_reload_class (in, rclass);
1969     if (preferred_class != NO_REGS)
1970       rclass = (enum reg_class) preferred_class;
1971   }
1972
1973   /* See if OUT will do.  */
1974   if (REG_P (out)
1975       && REGNO (out) < FIRST_PSEUDO_REGISTER)
1976     {
1977       unsigned int regno = REGNO (out) + out_offset;
1978       unsigned int nwords = hard_regno_nregs[regno][outmode];
1979       rtx saved_rtx;
1980
1981       /* When we consider whether the insn uses OUT,
1982          ignore references within IN.  They don't prevent us
1983          from copying IN into OUT, because those refs would
1984          move into the insn that reloads IN.
1985
1986          However, we only ignore IN in its role as this reload.
1987          If the insn uses IN elsewhere and it contains OUT,
1988          that counts.  We can't be sure it's the "same" operand
1989          so it might not go through this reload.  */
1990       saved_rtx = *inloc;
1991       *inloc = const0_rtx;
1992
1993       if (regno < FIRST_PSEUDO_REGISTER
1994           && HARD_REGNO_MODE_OK (regno, outmode)
1995           && ! refers_to_regno_for_reload_p (regno, regno + nwords,
1996                                              PATTERN (this_insn), outloc))
1997         {
1998           unsigned int i;
1999
2000           for (i = 0; i < nwords; i++)
2001             if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2002                                      regno + i))
2003               break;
2004
2005           if (i == nwords)
2006             {
2007               if (REG_P (real_out))
2008                 value = real_out;
2009               else
2010                 value = gen_rtx_REG (outmode, regno);
2011             }
2012         }
2013
2014       *inloc = saved_rtx;
2015     }
2016
2017   /* Consider using IN if OUT was not acceptable
2018      or if OUT dies in this insn (like the quotient in a divmod insn).
2019      We can't use IN unless it is dies in this insn,
2020      which means we must know accurately which hard regs are live.
2021      Also, the result can't go in IN if IN is used within OUT,
2022      or if OUT is an earlyclobber and IN appears elsewhere in the insn.  */
2023   if (hard_regs_live_known
2024       && REG_P (in)
2025       && REGNO (in) < FIRST_PSEUDO_REGISTER
2026       && (value == 0
2027           || find_reg_note (this_insn, REG_UNUSED, real_out))
2028       && find_reg_note (this_insn, REG_DEAD, real_in)
2029       && !fixed_regs[REGNO (in)]
2030       && HARD_REGNO_MODE_OK (REGNO (in),
2031                              /* The only case where out and real_out might
2032                                 have different modes is where real_out
2033                                 is a subreg, and in that case, out
2034                                 has a real mode.  */
2035                              (GET_MODE (out) != VOIDmode
2036                               ? GET_MODE (out) : outmode))
2037       && (ORIGINAL_REGNO (in) < FIRST_PSEUDO_REGISTER
2038           /* However only do this if we can be sure that this input
2039              operand doesn't correspond with an uninitialized pseudo.
2040              global can assign some hardreg to it that is the same as
2041              the one assigned to a different, also live pseudo (as it
2042              can ignore the conflict).  We must never introduce writes
2043              to such hardregs, as they would clobber the other live
2044              pseudo.  See PR 20973.  */
2045           || (!bitmap_bit_p (DF_LR_OUT (ENTRY_BLOCK_PTR),
2046                              ORIGINAL_REGNO (in))
2047               /* Similarly, only do this if we can be sure that the death
2048                  note is still valid.  global can assign some hardreg to
2049                  the pseudo referenced in the note and simultaneously a
2050                  subword of this hardreg to a different, also live pseudo,
2051                  because only another subword of the hardreg is actually
2052                  used in the insn.  This cannot happen if the pseudo has
2053                  been assigned exactly one hardreg.  See PR 33732.  */
2054               && hard_regno_nregs[REGNO (in)][GET_MODE (in)] == 1)))
2055     {
2056       unsigned int regno = REGNO (in) + in_offset;
2057       unsigned int nwords = hard_regno_nregs[regno][inmode];
2058
2059       if (! refers_to_regno_for_reload_p (regno, regno + nwords, out, (rtx*) 0)
2060           && ! hard_reg_set_here_p (regno, regno + nwords,
2061                                     PATTERN (this_insn))
2062           && (! earlyclobber
2063               || ! refers_to_regno_for_reload_p (regno, regno + nwords,
2064                                                  PATTERN (this_insn), inloc)))
2065         {
2066           unsigned int i;
2067
2068           for (i = 0; i < nwords; i++)
2069             if (! TEST_HARD_REG_BIT (reg_class_contents[(int) rclass],
2070                                      regno + i))
2071               break;
2072
2073           if (i == nwords)
2074             {
2075               /* If we were going to use OUT as the reload reg
2076                  and changed our mind, it means OUT is a dummy that
2077                  dies here.  So don't bother copying value to it.  */
2078               if (for_real >= 0 && value == real_out)
2079                 rld[for_real].out = 0;
2080               if (REG_P (real_in))
2081                 value = real_in;
2082               else
2083                 value = gen_rtx_REG (inmode, regno);
2084             }
2085         }
2086     }
2087
2088   return value;
2089 }
2090 \f
2091 /* This page contains subroutines used mainly for determining
2092    whether the IN or an OUT of a reload can serve as the
2093    reload register.  */
2094
2095 /* Return 1 if X is an operand of an insn that is being earlyclobbered.  */
2096
2097 int
2098 earlyclobber_operand_p (rtx x)
2099 {
2100   int i;
2101
2102   for (i = 0; i < n_earlyclobbers; i++)
2103     if (reload_earlyclobbers[i] == x)
2104       return 1;
2105
2106   return 0;
2107 }
2108
2109 /* Return 1 if expression X alters a hard reg in the range
2110    from BEG_REGNO (inclusive) to END_REGNO (exclusive),
2111    either explicitly or in the guise of a pseudo-reg allocated to REGNO.
2112    X should be the body of an instruction.  */
2113
2114 static int
2115 hard_reg_set_here_p (unsigned int beg_regno, unsigned int end_regno, rtx x)
2116 {
2117   if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2118     {
2119       rtx op0 = SET_DEST (x);
2120
2121       while (GET_CODE (op0) == SUBREG)
2122         op0 = SUBREG_REG (op0);
2123       if (REG_P (op0))
2124         {
2125           unsigned int r = REGNO (op0);
2126
2127           /* See if this reg overlaps range under consideration.  */
2128           if (r < end_regno
2129               && end_hard_regno (GET_MODE (op0), r) > beg_regno)
2130             return 1;
2131         }
2132     }
2133   else if (GET_CODE (x) == PARALLEL)
2134     {
2135       int i = XVECLEN (x, 0) - 1;
2136
2137       for (; i >= 0; i--)
2138         if (hard_reg_set_here_p (beg_regno, end_regno, XVECEXP (x, 0, i)))
2139           return 1;
2140     }
2141
2142   return 0;
2143 }
2144
2145 /* Return 1 if ADDR is a valid memory address for mode MODE
2146    in address space AS, and check that each pseudo reg has the
2147    proper kind of hard reg.  */
2148
2149 int
2150 strict_memory_address_addr_space_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2151                                     rtx addr, addr_space_t as)
2152 {
2153 #ifdef GO_IF_LEGITIMATE_ADDRESS
2154   gcc_assert (ADDR_SPACE_GENERIC_P (as));
2155   GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2156   return 0;
2157
2158  win:
2159   return 1;
2160 #else
2161   return targetm.addr_space.legitimate_address_p (mode, addr, 1, as);
2162 #endif
2163 }
2164 \f
2165 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
2166    if they are the same hard reg, and has special hacks for
2167    autoincrement and autodecrement.
2168    This is specifically intended for find_reloads to use
2169    in determining whether two operands match.
2170    X is the operand whose number is the lower of the two.
2171
2172    The value is 2 if Y contains a pre-increment that matches
2173    a non-incrementing address in X.  */
2174
2175 /* ??? To be completely correct, we should arrange to pass
2176    for X the output operand and for Y the input operand.
2177    For now, we assume that the output operand has the lower number
2178    because that is natural in (SET output (... input ...)).  */
2179
2180 int
2181 operands_match_p (rtx x, rtx y)
2182 {
2183   int i;
2184   RTX_CODE code = GET_CODE (x);
2185   const char *fmt;
2186   int success_2;
2187
2188   if (x == y)
2189     return 1;
2190   if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
2191       && (REG_P (y) || (GET_CODE (y) == SUBREG
2192                                   && REG_P (SUBREG_REG (y)))))
2193     {
2194       int j;
2195
2196       if (code == SUBREG)
2197         {
2198           i = REGNO (SUBREG_REG (x));
2199           if (i >= FIRST_PSEUDO_REGISTER)
2200             goto slow;
2201           i += subreg_regno_offset (REGNO (SUBREG_REG (x)),
2202                                     GET_MODE (SUBREG_REG (x)),
2203                                     SUBREG_BYTE (x),
2204                                     GET_MODE (x));
2205         }
2206       else
2207         i = REGNO (x);
2208
2209       if (GET_CODE (y) == SUBREG)
2210         {
2211           j = REGNO (SUBREG_REG (y));
2212           if (j >= FIRST_PSEUDO_REGISTER)
2213             goto slow;
2214           j += subreg_regno_offset (REGNO (SUBREG_REG (y)),
2215                                     GET_MODE (SUBREG_REG (y)),
2216                                     SUBREG_BYTE (y),
2217                                     GET_MODE (y));
2218         }
2219       else
2220         j = REGNO (y);
2221
2222       /* On a WORDS_BIG_ENDIAN machine, point to the last register of a
2223          multiple hard register group of scalar integer registers, so that
2224          for example (reg:DI 0) and (reg:SI 1) will be considered the same
2225          register.  */
2226       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
2227           && SCALAR_INT_MODE_P (GET_MODE (x))
2228           && i < FIRST_PSEUDO_REGISTER)
2229         i += hard_regno_nregs[i][GET_MODE (x)] - 1;
2230       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (y)) > UNITS_PER_WORD
2231           && SCALAR_INT_MODE_P (GET_MODE (y))
2232           && j < FIRST_PSEUDO_REGISTER)
2233         j += hard_regno_nregs[j][GET_MODE (y)] - 1;
2234
2235       return i == j;
2236     }
2237   /* If two operands must match, because they are really a single
2238      operand of an assembler insn, then two postincrements are invalid
2239      because the assembler insn would increment only once.
2240      On the other hand, a postincrement matches ordinary indexing
2241      if the postincrement is the output operand.  */
2242   if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
2243     return operands_match_p (XEXP (x, 0), y);
2244   /* Two preincrements are invalid
2245      because the assembler insn would increment only once.
2246      On the other hand, a preincrement matches ordinary indexing
2247      if the preincrement is the input operand.
2248      In this case, return 2, since some callers need to do special
2249      things when this happens.  */
2250   if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
2251       || GET_CODE (y) == PRE_MODIFY)
2252     return operands_match_p (x, XEXP (y, 0)) ? 2 : 0;
2253
2254  slow:
2255
2256   /* Now we have disposed of all the cases in which different rtx codes
2257      can match.  */
2258   if (code != GET_CODE (y))
2259     return 0;
2260
2261   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
2262   if (GET_MODE (x) != GET_MODE (y))
2263     return 0;
2264
2265   /* MEMs refering to different address space are not equivalent.  */
2266   if (code == MEM && MEM_ADDR_SPACE (x) != MEM_ADDR_SPACE (y))
2267     return 0;
2268
2269   switch (code)
2270     {
2271     case CONST_INT:
2272     case CONST_DOUBLE:
2273     case CONST_FIXED:
2274       return 0;
2275
2276     case LABEL_REF:
2277       return XEXP (x, 0) == XEXP (y, 0);
2278     case SYMBOL_REF:
2279       return XSTR (x, 0) == XSTR (y, 0);
2280
2281     default:
2282       break;
2283     }
2284
2285   /* Compare the elements.  If any pair of corresponding elements
2286      fail to match, return 0 for the whole things.  */
2287
2288   success_2 = 0;
2289   fmt = GET_RTX_FORMAT (code);
2290   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2291     {
2292       int val, j;
2293       switch (fmt[i])
2294         {
2295         case 'w':
2296           if (XWINT (x, i) != XWINT (y, i))
2297             return 0;
2298           break;
2299
2300         case 'i':
2301           if (XINT (x, i) != XINT (y, i))
2302             return 0;
2303           break;
2304
2305         case 'e':
2306           val = operands_match_p (XEXP (x, i), XEXP (y, i));
2307           if (val == 0)
2308             return 0;
2309           /* If any subexpression returns 2,
2310              we should return 2 if we are successful.  */
2311           if (val == 2)
2312             success_2 = 1;
2313           break;
2314
2315         case '0':
2316           break;
2317
2318         case 'E':
2319           if (XVECLEN (x, i) != XVECLEN (y, i))
2320             return 0;
2321           for (j = XVECLEN (x, i) - 1; j >= 0; --j)
2322             {
2323               val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j));
2324               if (val == 0)
2325                 return 0;
2326               if (val == 2)
2327                 success_2 = 1;
2328             }
2329           break;
2330
2331           /* It is believed that rtx's at this level will never
2332              contain anything but integers and other rtx's,
2333              except for within LABEL_REFs and SYMBOL_REFs.  */
2334         default:
2335           gcc_unreachable ();
2336         }
2337     }
2338   return 1 + success_2;
2339 }
2340 \f
2341 /* Describe the range of registers or memory referenced by X.
2342    If X is a register, set REG_FLAG and put the first register
2343    number into START and the last plus one into END.
2344    If X is a memory reference, put a base address into BASE
2345    and a range of integer offsets into START and END.
2346    If X is pushing on the stack, we can assume it causes no trouble,
2347    so we set the SAFE field.  */
2348
2349 static struct decomposition
2350 decompose (rtx x)
2351 {
2352   struct decomposition val;
2353   int all_const = 0;
2354
2355   memset (&val, 0, sizeof (val));
2356
2357   switch (GET_CODE (x))
2358     {
2359     case MEM:
2360       {
2361         rtx base = NULL_RTX, offset = 0;
2362         rtx addr = XEXP (x, 0);
2363
2364         if (GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
2365             || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
2366           {
2367             val.base = XEXP (addr, 0);
2368             val.start = -GET_MODE_SIZE (GET_MODE (x));
2369             val.end = GET_MODE_SIZE (GET_MODE (x));
2370             val.safe = REGNO (val.base) == STACK_POINTER_REGNUM;
2371             return val;
2372           }
2373
2374         if (GET_CODE (addr) == PRE_MODIFY || GET_CODE (addr) == POST_MODIFY)
2375           {
2376             if (GET_CODE (XEXP (addr, 1)) == PLUS
2377                 && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
2378                 && CONSTANT_P (XEXP (XEXP (addr, 1), 1)))
2379               {
2380                 val.base  = XEXP (addr, 0);
2381                 val.start = -INTVAL (XEXP (XEXP (addr, 1), 1));
2382                 val.end   = INTVAL (XEXP (XEXP (addr, 1), 1));
2383                 val.safe  = REGNO (val.base) == STACK_POINTER_REGNUM;
2384                 return val;
2385               }
2386           }
2387
2388         if (GET_CODE (addr) == CONST)
2389           {
2390             addr = XEXP (addr, 0);
2391             all_const = 1;
2392           }
2393         if (GET_CODE (addr) == PLUS)
2394           {
2395             if (CONSTANT_P (XEXP (addr, 0)))
2396               {
2397                 base = XEXP (addr, 1);
2398                 offset = XEXP (addr, 0);
2399               }
2400             else if (CONSTANT_P (XEXP (addr, 1)))
2401               {
2402                 base = XEXP (addr, 0);
2403                 offset = XEXP (addr, 1);
2404               }
2405           }
2406
2407         if (offset == 0)
2408           {
2409             base = addr;
2410             offset = const0_rtx;
2411           }
2412         if (GET_CODE (offset) == CONST)
2413           offset = XEXP (offset, 0);
2414         if (GET_CODE (offset) == PLUS)
2415           {
2416             if (CONST_INT_P (XEXP (offset, 0)))
2417               {
2418                 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 1));
2419                 offset = XEXP (offset, 0);
2420               }
2421             else if (CONST_INT_P (XEXP (offset, 1)))
2422               {
2423                 base = gen_rtx_PLUS (GET_MODE (base), base, XEXP (offset, 0));
2424                 offset = XEXP (offset, 1);
2425               }
2426             else
2427               {
2428                 base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2429                 offset = const0_rtx;
2430               }
2431           }
2432         else if (!CONST_INT_P (offset))
2433           {
2434             base = gen_rtx_PLUS (GET_MODE (base), base, offset);
2435             offset = const0_rtx;
2436           }
2437
2438         if (all_const && GET_CODE (base) == PLUS)
2439           base = gen_rtx_CONST (GET_MODE (base), base);
2440
2441         gcc_assert (CONST_INT_P (offset));
2442
2443         val.start = INTVAL (offset);
2444         val.end = val.start + GET_MODE_SIZE (GET_MODE (x));
2445         val.base = base;
2446       }
2447       break;
2448
2449     case REG:
2450       val.reg_flag = 1;
2451       val.start = true_regnum (x);
2452       if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2453         {
2454           /* A pseudo with no hard reg.  */
2455           val.start = REGNO (x);
2456           val.end = val.start + 1;
2457         }
2458       else
2459         /* A hard reg.  */
2460         val.end = end_hard_regno (GET_MODE (x), val.start);
2461       break;
2462
2463     case SUBREG:
2464       if (!REG_P (SUBREG_REG (x)))
2465         /* This could be more precise, but it's good enough.  */
2466         return decompose (SUBREG_REG (x));
2467       val.reg_flag = 1;
2468       val.start = true_regnum (x);
2469       if (val.start < 0 || val.start >= FIRST_PSEUDO_REGISTER)
2470         return decompose (SUBREG_REG (x));
2471       else
2472         /* A hard reg.  */
2473         val.end = val.start + subreg_nregs (x);
2474       break;
2475
2476     case SCRATCH:
2477       /* This hasn't been assigned yet, so it can't conflict yet.  */
2478       val.safe = 1;
2479       break;
2480
2481     default:
2482       gcc_assert (CONSTANT_P (x));
2483       val.safe = 1;
2484       break;
2485     }
2486   return val;
2487 }
2488
2489 /* Return 1 if altering Y will not modify the value of X.
2490    Y is also described by YDATA, which should be decompose (Y).  */
2491
2492 static int
2493 immune_p (rtx x, rtx y, struct decomposition ydata)
2494 {
2495   struct decomposition xdata;
2496
2497   if (ydata.reg_flag)
2498     return !refers_to_regno_for_reload_p (ydata.start, ydata.end, x, (rtx*) 0);
2499   if (ydata.safe)
2500     return 1;
2501
2502   gcc_assert (MEM_P (y));
2503   /* If Y is memory and X is not, Y can't affect X.  */
2504   if (!MEM_P (x))
2505     return 1;
2506
2507   xdata = decompose (x);
2508
2509   if (! rtx_equal_p (xdata.base, ydata.base))
2510     {
2511       /* If bases are distinct symbolic constants, there is no overlap.  */
2512       if (CONSTANT_P (xdata.base) && CONSTANT_P (ydata.base))
2513         return 1;
2514       /* Constants and stack slots never overlap.  */
2515       if (CONSTANT_P (xdata.base)
2516           && (ydata.base == frame_pointer_rtx
2517               || ydata.base == hard_frame_pointer_rtx
2518               || ydata.base == stack_pointer_rtx))
2519         return 1;
2520       if (CONSTANT_P (ydata.base)
2521           && (xdata.base == frame_pointer_rtx
2522               || xdata.base == hard_frame_pointer_rtx
2523               || xdata.base == stack_pointer_rtx))
2524         return 1;
2525       /* If either base is variable, we don't know anything.  */
2526       return 0;
2527     }
2528
2529   return (xdata.start >= ydata.end || ydata.start >= xdata.end);
2530 }
2531
2532 /* Similar, but calls decompose.  */
2533
2534 int
2535 safe_from_earlyclobber (rtx op, rtx clobber)
2536 {
2537   struct decomposition early_data;
2538
2539   early_data = decompose (clobber);
2540   return immune_p (op, clobber, early_data);
2541 }
2542 \f
2543 /* Main entry point of this file: search the body of INSN
2544    for values that need reloading and record them with push_reload.
2545    REPLACE nonzero means record also where the values occur
2546    so that subst_reloads can be used.
2547
2548    IND_LEVELS says how many levels of indirection are supported by this
2549    machine; a value of zero means that a memory reference is not a valid
2550    memory address.
2551
2552    LIVE_KNOWN says we have valid information about which hard
2553    regs are live at each point in the program; this is true when
2554    we are called from global_alloc but false when stupid register
2555    allocation has been done.
2556
2557    RELOAD_REG_P if nonzero is a vector indexed by hard reg number
2558    which is nonnegative if the reg has been commandeered for reloading into.
2559    It is copied into STATIC_RELOAD_REG_P and referenced from there
2560    by various subroutines.
2561
2562    Return TRUE if some operands need to be changed, because of swapping
2563    commutative operands, reg_equiv_address substitution, or whatever.  */
2564
2565 int
2566 find_reloads (rtx insn, int replace, int ind_levels, int live_known,
2567               short *reload_reg_p)
2568 {
2569   int insn_code_number;
2570   int i, j;
2571   int noperands;
2572   /* These start out as the constraints for the insn
2573      and they are chewed up as we consider alternatives.  */
2574   const char *constraints[MAX_RECOG_OPERANDS];
2575   /* These are the preferred classes for an operand, or NO_REGS if it isn't
2576      a register.  */
2577   enum reg_class preferred_class[MAX_RECOG_OPERANDS];
2578   char pref_or_nothing[MAX_RECOG_OPERANDS];
2579   /* Nonzero for a MEM operand whose entire address needs a reload.
2580      May be -1 to indicate the entire address may or may not need a reload.  */
2581   int address_reloaded[MAX_RECOG_OPERANDS];
2582   /* Nonzero for an address operand that needs to be completely reloaded.
2583      May be -1 to indicate the entire operand may or may not need a reload.  */
2584   int address_operand_reloaded[MAX_RECOG_OPERANDS];
2585   /* Value of enum reload_type to use for operand.  */
2586   enum reload_type operand_type[MAX_RECOG_OPERANDS];
2587   /* Value of enum reload_type to use within address of operand.  */
2588   enum reload_type address_type[MAX_RECOG_OPERANDS];
2589   /* Save the usage of each operand.  */
2590   enum reload_usage { RELOAD_READ, RELOAD_READ_WRITE, RELOAD_WRITE } modified[MAX_RECOG_OPERANDS];
2591   int no_input_reloads = 0, no_output_reloads = 0;
2592   int n_alternatives;
2593   reg_class_t this_alternative[MAX_RECOG_OPERANDS];
2594   char this_alternative_match_win[MAX_RECOG_OPERANDS];
2595   char this_alternative_win[MAX_RECOG_OPERANDS];
2596   char this_alternative_offmemok[MAX_RECOG_OPERANDS];
2597   char this_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2598   int this_alternative_matches[MAX_RECOG_OPERANDS];
2599   int swapped;
2600   reg_class_t goal_alternative[MAX_RECOG_OPERANDS];
2601   int this_alternative_number;
2602   int goal_alternative_number = 0;
2603   int operand_reloadnum[MAX_RECOG_OPERANDS];
2604   int goal_alternative_matches[MAX_RECOG_OPERANDS];
2605   int goal_alternative_matched[MAX_RECOG_OPERANDS];
2606   char goal_alternative_match_win[MAX_RECOG_OPERANDS];
2607   char goal_alternative_win[MAX_RECOG_OPERANDS];
2608   char goal_alternative_offmemok[MAX_RECOG_OPERANDS];
2609   char goal_alternative_earlyclobber[MAX_RECOG_OPERANDS];
2610   int goal_alternative_swapped;
2611   int best;
2612   int commutative;
2613   char operands_match[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
2614   rtx substed_operand[MAX_RECOG_OPERANDS];
2615   rtx body = PATTERN (insn);
2616   rtx set = single_set (insn);
2617   int goal_earlyclobber = 0, this_earlyclobber;
2618   enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
2619   int retval = 0;
2620
2621   this_insn = insn;
2622   n_reloads = 0;
2623   n_replacements = 0;
2624   n_earlyclobbers = 0;
2625   replace_reloads = replace;
2626   hard_regs_live_known = live_known;
2627   static_reload_reg_p = reload_reg_p;
2628
2629   /* JUMP_INSNs and CALL_INSNs are not allowed to have any output reloads;
2630      neither are insns that SET cc0.  Insns that use CC0 are not allowed
2631      to have any input reloads.  */
2632   if (JUMP_P (insn) || CALL_P (insn))
2633     no_output_reloads = 1;
2634
2635 #ifdef HAVE_cc0
2636   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
2637     no_input_reloads = 1;
2638   if (reg_set_p (cc0_rtx, PATTERN (insn)))
2639     no_output_reloads = 1;
2640 #endif
2641
2642 #ifdef SECONDARY_MEMORY_NEEDED
2643   /* The eliminated forms of any secondary memory locations are per-insn, so
2644      clear them out here.  */
2645
2646   if (secondary_memlocs_elim_used)
2647     {
2648       memset (secondary_memlocs_elim, 0,
2649               sizeof (secondary_memlocs_elim[0]) * secondary_memlocs_elim_used);
2650       secondary_memlocs_elim_used = 0;
2651     }
2652 #endif
2653
2654   /* Dispose quickly of (set (reg..) (reg..)) if both have hard regs and it
2655      is cheap to move between them.  If it is not, there may not be an insn
2656      to do the copy, so we may need a reload.  */
2657   if (GET_CODE (body) == SET
2658       && REG_P (SET_DEST (body))
2659       && REGNO (SET_DEST (body)) < FIRST_PSEUDO_REGISTER
2660       && REG_P (SET_SRC (body))
2661       && REGNO (SET_SRC (body)) < FIRST_PSEUDO_REGISTER
2662       && register_move_cost (GET_MODE (SET_SRC (body)),
2663                              REGNO_REG_CLASS (REGNO (SET_SRC (body))),
2664                              REGNO_REG_CLASS (REGNO (SET_DEST (body)))) == 2)
2665     return 0;
2666
2667   extract_insn (insn);
2668
2669   noperands = reload_n_operands = recog_data.n_operands;
2670   n_alternatives = recog_data.n_alternatives;
2671
2672   /* Just return "no reloads" if insn has no operands with constraints.  */
2673   if (noperands == 0 || n_alternatives == 0)
2674     return 0;
2675
2676   insn_code_number = INSN_CODE (insn);
2677   this_insn_is_asm = insn_code_number < 0;
2678
2679   memcpy (operand_mode, recog_data.operand_mode,
2680           noperands * sizeof (enum machine_mode));
2681   memcpy (constraints, recog_data.constraints,
2682           noperands * sizeof (const char *));
2683
2684   commutative = -1;
2685
2686   /* If we will need to know, later, whether some pair of operands
2687      are the same, we must compare them now and save the result.
2688      Reloading the base and index registers will clobber them
2689      and afterward they will fail to match.  */
2690
2691   for (i = 0; i < noperands; i++)
2692     {
2693       const char *p;
2694       int c;
2695       char *end;
2696
2697       substed_operand[i] = recog_data.operand[i];
2698       p = constraints[i];
2699
2700       modified[i] = RELOAD_READ;
2701
2702       /* Scan this operand's constraint to see if it is an output operand,
2703          an in-out operand, is commutative, or should match another.  */
2704
2705       while ((c = *p))
2706         {
2707           p += CONSTRAINT_LEN (c, p);
2708           switch (c)
2709             {
2710             case '=':
2711               modified[i] = RELOAD_WRITE;
2712               break;
2713             case '+':
2714               modified[i] = RELOAD_READ_WRITE;
2715               break;
2716             case '%':
2717               {
2718                 /* The last operand should not be marked commutative.  */
2719                 gcc_assert (i != noperands - 1);
2720
2721                 /* We currently only support one commutative pair of
2722                    operands.  Some existing asm code currently uses more
2723                    than one pair.  Previously, that would usually work,
2724                    but sometimes it would crash the compiler.  We
2725                    continue supporting that case as well as we can by
2726                    silently ignoring all but the first pair.  In the
2727                    future we may handle it correctly.  */
2728                 if (commutative < 0)
2729                   commutative = i;
2730                 else
2731                   gcc_assert (this_insn_is_asm);
2732               }
2733               break;
2734             /* Use of ISDIGIT is tempting here, but it may get expensive because
2735                of locale support we don't want.  */
2736             case '0': case '1': case '2': case '3': case '4':
2737             case '5': case '6': case '7': case '8': case '9':
2738               {
2739                 c = strtoul (p - 1, &end, 10);
2740                 p = end;
2741
2742                 operands_match[c][i]
2743                   = operands_match_p (recog_data.operand[c],
2744                                       recog_data.operand[i]);
2745
2746                 /* An operand may not match itself.  */
2747                 gcc_assert (c != i);
2748
2749                 /* If C can be commuted with C+1, and C might need to match I,
2750                    then C+1 might also need to match I.  */
2751                 if (commutative >= 0)
2752                   {
2753                     if (c == commutative || c == commutative + 1)
2754                       {
2755                         int other = c + (c == commutative ? 1 : -1);
2756                         operands_match[other][i]
2757                           = operands_match_p (recog_data.operand[other],
2758                                               recog_data.operand[i]);
2759                       }
2760                     if (i == commutative || i == commutative + 1)
2761                       {
2762                         int other = i + (i == commutative ? 1 : -1);
2763                         operands_match[c][other]
2764                           = operands_match_p (recog_data.operand[c],
2765                                               recog_data.operand[other]);
2766                       }
2767                     /* Note that C is supposed to be less than I.
2768                        No need to consider altering both C and I because in
2769                        that case we would alter one into the other.  */
2770                   }
2771               }
2772             }
2773         }
2774     }
2775
2776   /* Examine each operand that is a memory reference or memory address
2777      and reload parts of the addresses into index registers.
2778      Also here any references to pseudo regs that didn't get hard regs
2779      but are equivalent to constants get replaced in the insn itself
2780      with those constants.  Nobody will ever see them again.
2781
2782      Finally, set up the preferred classes of each operand.  */
2783
2784   for (i = 0; i < noperands; i++)
2785     {
2786       RTX_CODE code = GET_CODE (recog_data.operand[i]);
2787
2788       address_reloaded[i] = 0;
2789       address_operand_reloaded[i] = 0;
2790       operand_type[i] = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT
2791                          : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT
2792                          : RELOAD_OTHER);
2793       address_type[i]
2794         = (modified[i] == RELOAD_READ ? RELOAD_FOR_INPUT_ADDRESS
2795            : modified[i] == RELOAD_WRITE ? RELOAD_FOR_OUTPUT_ADDRESS
2796            : RELOAD_OTHER);
2797
2798       if (*constraints[i] == 0)
2799         /* Ignore things like match_operator operands.  */
2800         ;
2801       else if (constraints[i][0] == 'p'
2802                || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i]))
2803         {
2804           address_operand_reloaded[i]
2805             = find_reloads_address (recog_data.operand_mode[i], (rtx*) 0,
2806                                     recog_data.operand[i],
2807                                     recog_data.operand_loc[i],
2808                                     i, operand_type[i], ind_levels, insn);
2809
2810           /* If we now have a simple operand where we used to have a
2811              PLUS or MULT, re-recognize and try again.  */
2812           if ((OBJECT_P (*recog_data.operand_loc[i])
2813                || GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
2814               && (GET_CODE (recog_data.operand[i]) == MULT
2815                   || GET_CODE (recog_data.operand[i]) == PLUS))
2816             {
2817               INSN_CODE (insn) = -1;
2818               retval = find_reloads (insn, replace, ind_levels, live_known,
2819                                      reload_reg_p);
2820               return retval;
2821             }
2822
2823           recog_data.operand[i] = *recog_data.operand_loc[i];
2824           substed_operand[i] = recog_data.operand[i];
2825
2826           /* Address operands are reloaded in their existing mode,
2827              no matter what is specified in the machine description.  */
2828           operand_mode[i] = GET_MODE (recog_data.operand[i]);
2829         }
2830       else if (code == MEM)
2831         {
2832           address_reloaded[i]
2833             = find_reloads_address (GET_MODE (recog_data.operand[i]),
2834                                     recog_data.operand_loc[i],
2835                                     XEXP (recog_data.operand[i], 0),
2836                                     &XEXP (recog_data.operand[i], 0),
2837                                     i, address_type[i], ind_levels, insn);
2838           recog_data.operand[i] = *recog_data.operand_loc[i];
2839           substed_operand[i] = recog_data.operand[i];
2840         }
2841       else if (code == SUBREG)
2842         {
2843           rtx reg = SUBREG_REG (recog_data.operand[i]);
2844           rtx op
2845             = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2846                                    ind_levels,
2847                                    set != 0
2848                                    && &SET_DEST (set) == recog_data.operand_loc[i],
2849                                    insn,
2850                                    &address_reloaded[i]);
2851
2852           /* If we made a MEM to load (a part of) the stackslot of a pseudo
2853              that didn't get a hard register, emit a USE with a REG_EQUAL
2854              note in front so that we might inherit a previous, possibly
2855              wider reload.  */
2856
2857           if (replace
2858               && MEM_P (op)
2859               && REG_P (reg)
2860               && (GET_MODE_SIZE (GET_MODE (reg))
2861                   >= GET_MODE_SIZE (GET_MODE (op)))
2862               && reg_equiv_constant (REGNO (reg)) == 0)
2863             set_unique_reg_note (emit_insn_before (gen_rtx_USE (VOIDmode, reg),
2864                                                    insn),
2865                                  REG_EQUAL, reg_equiv_memory_loc (REGNO (reg)));
2866
2867           substed_operand[i] = recog_data.operand[i] = op;
2868         }
2869       else if (code == PLUS || GET_RTX_CLASS (code) == RTX_UNARY)
2870         /* We can get a PLUS as an "operand" as a result of register
2871            elimination.  See eliminate_regs and gen_reload.  We handle
2872            a unary operator by reloading the operand.  */
2873         substed_operand[i] = recog_data.operand[i]
2874           = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2875                                  ind_levels, 0, insn,
2876                                  &address_reloaded[i]);
2877       else if (code == REG)
2878         {
2879           /* This is equivalent to calling find_reloads_toplev.
2880              The code is duplicated for speed.
2881              When we find a pseudo always equivalent to a constant,
2882              we replace it by the constant.  We must be sure, however,
2883              that we don't try to replace it in the insn in which it
2884              is being set.  */
2885           int regno = REGNO (recog_data.operand[i]);
2886           if (reg_equiv_constant (regno) != 0
2887               && (set == 0 || &SET_DEST (set) != recog_data.operand_loc[i]))
2888             {
2889               /* Record the existing mode so that the check if constants are
2890                  allowed will work when operand_mode isn't specified.  */
2891
2892               if (operand_mode[i] == VOIDmode)
2893                 operand_mode[i] = GET_MODE (recog_data.operand[i]);
2894
2895               substed_operand[i] = recog_data.operand[i]
2896                 = reg_equiv_constant (regno);
2897             }
2898           if (reg_equiv_memory_loc (regno) != 0
2899               && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
2900             /* We need not give a valid is_set_dest argument since the case
2901                of a constant equivalence was checked above.  */
2902             substed_operand[i] = recog_data.operand[i]
2903               = find_reloads_toplev (recog_data.operand[i], i, address_type[i],
2904                                      ind_levels, 0, insn,
2905                                      &address_reloaded[i]);
2906         }
2907       /* If the operand is still a register (we didn't replace it with an
2908          equivalent), get the preferred class to reload it into.  */
2909       code = GET_CODE (recog_data.operand[i]);
2910       preferred_class[i]
2911         = ((code == REG && REGNO (recog_data.operand[i])
2912             >= FIRST_PSEUDO_REGISTER)
2913            ? reg_preferred_class (REGNO (recog_data.operand[i]))
2914            : NO_REGS);
2915       pref_or_nothing[i]
2916         = (code == REG
2917            && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER
2918            && reg_alternate_class (REGNO (recog_data.operand[i])) == NO_REGS);
2919     }
2920
2921   /* If this is simply a copy from operand 1 to operand 0, merge the
2922      preferred classes for the operands.  */
2923   if (set != 0 && noperands >= 2 && recog_data.operand[0] == SET_DEST (set)
2924       && recog_data.operand[1] == SET_SRC (set))
2925     {
2926       preferred_class[0] = preferred_class[1]
2927         = reg_class_subunion[(int) preferred_class[0]][(int) preferred_class[1]];
2928       pref_or_nothing[0] |= pref_or_nothing[1];
2929       pref_or_nothing[1] |= pref_or_nothing[0];
2930     }
2931
2932   /* Now see what we need for pseudo-regs that didn't get hard regs
2933      or got the wrong kind of hard reg.  For this, we must consider
2934      all the operands together against the register constraints.  */
2935
2936   best = MAX_RECOG_OPERANDS * 2 + 600;
2937
2938   swapped = 0;
2939   goal_alternative_swapped = 0;
2940  try_swapped:
2941
2942   /* The constraints are made of several alternatives.
2943      Each operand's constraint looks like foo,bar,... with commas
2944      separating the alternatives.  The first alternatives for all
2945      operands go together, the second alternatives go together, etc.
2946
2947      First loop over alternatives.  */
2948
2949   for (this_alternative_number = 0;
2950        this_alternative_number < n_alternatives;
2951        this_alternative_number++)
2952     {
2953       /* Loop over operands for one constraint alternative.  */
2954       /* LOSERS counts those that don't fit this alternative
2955          and would require loading.  */
2956       int losers = 0;
2957       /* BAD is set to 1 if it some operand can't fit this alternative
2958          even after reloading.  */
2959       int bad = 0;
2960       /* REJECT is a count of how undesirable this alternative says it is
2961          if any reloading is required.  If the alternative matches exactly
2962          then REJECT is ignored, but otherwise it gets this much
2963          counted against it in addition to the reloading needed.  Each
2964          ? counts three times here since we want the disparaging caused by
2965          a bad register class to only count 1/3 as much.  */
2966       int reject = 0;
2967
2968       if (!recog_data.alternative_enabled_p[this_alternative_number])
2969         {
2970           int i;
2971
2972           for (i = 0; i < recog_data.n_operands; i++)
2973             constraints[i] = skip_alternative (constraints[i]);
2974
2975           continue;
2976         }
2977
2978       this_earlyclobber = 0;
2979
2980       for (i = 0; i < noperands; i++)
2981         {
2982           const char *p = constraints[i];
2983           char *end;
2984           int len;
2985           int win = 0;
2986           int did_match = 0;
2987           /* 0 => this operand can be reloaded somehow for this alternative.  */
2988           int badop = 1;
2989           /* 0 => this operand can be reloaded if the alternative allows regs.  */
2990           int winreg = 0;
2991           int c;
2992           int m;
2993           rtx operand = recog_data.operand[i];
2994           int offset = 0;
2995           /* Nonzero means this is a MEM that must be reloaded into a reg
2996              regardless of what the constraint says.  */
2997           int force_reload = 0;
2998           int offmemok = 0;
2999           /* Nonzero if a constant forced into memory would be OK for this
3000              operand.  */
3001           int constmemok = 0;
3002           int earlyclobber = 0;
3003
3004           /* If the predicate accepts a unary operator, it means that
3005              we need to reload the operand, but do not do this for
3006              match_operator and friends.  */
3007           if (UNARY_P (operand) && *p != 0)
3008             operand = XEXP (operand, 0);
3009
3010           /* If the operand is a SUBREG, extract
3011              the REG or MEM (or maybe even a constant) within.
3012              (Constants can occur as a result of reg_equiv_constant.)  */
3013
3014           while (GET_CODE (operand) == SUBREG)
3015             {
3016               /* Offset only matters when operand is a REG and
3017                  it is a hard reg.  This is because it is passed
3018                  to reg_fits_class_p if it is a REG and all pseudos
3019                  return 0 from that function.  */
3020               if (REG_P (SUBREG_REG (operand))
3021                   && REGNO (SUBREG_REG (operand)) < FIRST_PSEUDO_REGISTER)
3022                 {
3023                   if (simplify_subreg_regno (REGNO (SUBREG_REG (operand)),
3024                                              GET_MODE (SUBREG_REG (operand)),
3025                                              SUBREG_BYTE (operand),
3026                                              GET_MODE (operand)) < 0)
3027                     force_reload = 1;
3028                   offset += subreg_regno_offset (REGNO (SUBREG_REG (operand)),
3029                                                  GET_MODE (SUBREG_REG (operand)),
3030                                                  SUBREG_BYTE (operand),
3031                                                  GET_MODE (operand));
3032                 }
3033               operand = SUBREG_REG (operand);
3034               /* Force reload if this is a constant or PLUS or if there may
3035                  be a problem accessing OPERAND in the outer mode.  */
3036               if (CONSTANT_P (operand)
3037                   || GET_CODE (operand) == PLUS
3038                   /* We must force a reload of paradoxical SUBREGs
3039                      of a MEM because the alignment of the inner value
3040                      may not be enough to do the outer reference.  On
3041                      big-endian machines, it may also reference outside
3042                      the object.
3043
3044                      On machines that extend byte operations and we have a
3045                      SUBREG where both the inner and outer modes are no wider
3046                      than a word and the inner mode is narrower, is integral,
3047                      and gets extended when loaded from memory, combine.c has
3048                      made assumptions about the behavior of the machine in such
3049                      register access.  If the data is, in fact, in memory we
3050                      must always load using the size assumed to be in the
3051                      register and let the insn do the different-sized
3052                      accesses.
3053
3054                      This is doubly true if WORD_REGISTER_OPERATIONS.  In
3055                      this case eliminate_regs has left non-paradoxical
3056                      subregs for push_reload to see.  Make sure it does
3057                      by forcing the reload.
3058
3059                      ??? When is it right at this stage to have a subreg
3060                      of a mem that is _not_ to be handled specially?  IMO
3061                      those should have been reduced to just a mem.  */
3062                   || ((MEM_P (operand)
3063                        || (REG_P (operand)
3064                            && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
3065 #ifndef WORD_REGISTER_OPERATIONS
3066                       && (((GET_MODE_BITSIZE (GET_MODE (operand))
3067                             < BIGGEST_ALIGNMENT)
3068                            && (GET_MODE_SIZE (operand_mode[i])
3069                                > GET_MODE_SIZE (GET_MODE (operand))))
3070                           || BYTES_BIG_ENDIAN
3071 #ifdef LOAD_EXTEND_OP
3072                           || (GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3073                               && (GET_MODE_SIZE (GET_MODE (operand))
3074                                   <= UNITS_PER_WORD)
3075                               && (GET_MODE_SIZE (operand_mode[i])
3076                                   > GET_MODE_SIZE (GET_MODE (operand)))
3077                               && INTEGRAL_MODE_P (GET_MODE (operand))
3078                               && LOAD_EXTEND_OP (GET_MODE (operand)) != UNKNOWN)
3079 #endif
3080                           )
3081 #endif
3082                       )
3083                   )
3084                 force_reload = 1;
3085             }
3086
3087           this_alternative[i] = NO_REGS;
3088           this_alternative_win[i] = 0;
3089           this_alternative_match_win[i] = 0;
3090           this_alternative_offmemok[i] = 0;
3091           this_alternative_earlyclobber[i] = 0;
3092           this_alternative_matches[i] = -1;
3093
3094           /* An empty constraint or empty alternative
3095              allows anything which matched the pattern.  */
3096           if (*p == 0 || *p == ',')
3097             win = 1, badop = 0;
3098
3099           /* Scan this alternative's specs for this operand;
3100              set WIN if the operand fits any letter in this alternative.
3101              Otherwise, clear BADOP if this operand could
3102              fit some letter after reloads,
3103              or set WINREG if this operand could fit after reloads
3104              provided the constraint allows some registers.  */
3105
3106           do
3107             switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
3108               {
3109               case '\0':
3110                 len = 0;
3111                 break;
3112               case ',':
3113                 c = '\0';
3114                 break;
3115
3116               case '=':  case '+':  case '*':
3117                 break;
3118
3119               case '%':
3120                 /* We only support one commutative marker, the first
3121                    one.  We already set commutative above.  */
3122                 break;
3123
3124               case '?':
3125                 reject += 6;
3126                 break;
3127
3128               case '!':
3129                 reject = 600;
3130                 break;
3131
3132               case '#':
3133                 /* Ignore rest of this alternative as far as
3134                    reloading is concerned.  */
3135                 do
3136                   p++;
3137                 while (*p && *p != ',');
3138                 len = 0;
3139                 break;
3140
3141               case '0':  case '1':  case '2':  case '3':  case '4':
3142               case '5':  case '6':  case '7':  case '8':  case '9':
3143                 m = strtoul (p, &end, 10);
3144                 p = end;
3145                 len = 0;
3146
3147                 this_alternative_matches[i] = m;
3148                 /* We are supposed to match a previous operand.
3149                    If we do, we win if that one did.
3150                    If we do not, count both of the operands as losers.
3151                    (This is too conservative, since most of the time
3152                    only a single reload insn will be needed to make
3153                    the two operands win.  As a result, this alternative
3154                    may be rejected when it is actually desirable.)  */
3155                 if ((swapped && (m != commutative || i != commutative + 1))
3156                     /* If we are matching as if two operands were swapped,
3157                        also pretend that operands_match had been computed
3158                        with swapped.
3159                        But if I is the second of those and C is the first,
3160                        don't exchange them, because operands_match is valid
3161                        only on one side of its diagonal.  */
3162                     ? (operands_match
3163                        [(m == commutative || m == commutative + 1)
3164                        ? 2 * commutative + 1 - m : m]
3165                        [(i == commutative || i == commutative + 1)
3166                        ? 2 * commutative + 1 - i : i])
3167                     : operands_match[m][i])
3168                   {
3169                     /* If we are matching a non-offsettable address where an
3170                        offsettable address was expected, then we must reject
3171                        this combination, because we can't reload it.  */
3172                     if (this_alternative_offmemok[m]
3173                         && MEM_P (recog_data.operand[m])
3174                         && this_alternative[m] == NO_REGS
3175                         && ! this_alternative_win[m])
3176                       bad = 1;
3177
3178                     did_match = this_alternative_win[m];
3179                   }
3180                 else
3181                   {
3182                     /* Operands don't match.  */
3183                     rtx value;
3184                     int loc1, loc2;
3185                     /* Retroactively mark the operand we had to match
3186                        as a loser, if it wasn't already.  */
3187                     if (this_alternative_win[m])
3188                       losers++;
3189                     this_alternative_win[m] = 0;
3190                     if (this_alternative[m] == NO_REGS)
3191                       bad = 1;
3192                     /* But count the pair only once in the total badness of
3193                        this alternative, if the pair can be a dummy reload.
3194                        The pointers in operand_loc are not swapped; swap
3195                        them by hand if necessary.  */
3196                     if (swapped && i == commutative)
3197                       loc1 = commutative + 1;
3198                     else if (swapped && i == commutative + 1)
3199                       loc1 = commutative;
3200                     else
3201                       loc1 = i;
3202                     if (swapped && m == commutative)
3203                       loc2 = commutative + 1;
3204                     else if (swapped && m == commutative + 1)
3205                       loc2 = commutative;
3206                     else
3207                       loc2 = m;
3208                     value
3209                       = find_dummy_reload (recog_data.operand[i],
3210                                            recog_data.operand[m],
3211                                            recog_data.operand_loc[loc1],
3212                                            recog_data.operand_loc[loc2],
3213                                            operand_mode[i], operand_mode[m],
3214                                            this_alternative[m], -1,
3215                                            this_alternative_earlyclobber[m]);
3216
3217                     if (value != 0)
3218                       losers--;
3219                   }
3220                 /* This can be fixed with reloads if the operand
3221                    we are supposed to match can be fixed with reloads.  */
3222                 badop = 0;
3223                 this_alternative[i] = this_alternative[m];
3224
3225                 /* If we have to reload this operand and some previous
3226                    operand also had to match the same thing as this
3227                    operand, we don't know how to do that.  So reject this
3228                    alternative.  */
3229                 if (! did_match || force_reload)
3230                   for (j = 0; j < i; j++)
3231                     if (this_alternative_matches[j]
3232                         == this_alternative_matches[i])
3233                       badop = 1;
3234                 break;
3235
3236               case 'p':
3237                 /* All necessary reloads for an address_operand
3238                    were handled in find_reloads_address.  */
3239                 this_alternative[i] = base_reg_class (VOIDmode, ADDRESS,
3240                                                       SCRATCH);
3241                 win = 1;
3242                 badop = 0;
3243                 break;
3244
3245               case TARGET_MEM_CONSTRAINT:
3246                 if (force_reload)
3247                   break;
3248                 if (MEM_P (operand)
3249                     || (REG_P (operand)
3250                         && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3251                         && reg_renumber[REGNO (operand)] < 0))
3252                   win = 1;
3253                 if (CONST_POOL_OK_P (operand_mode[i], operand))
3254                   badop = 0;
3255                 constmemok = 1;
3256                 break;
3257
3258               case '<':
3259                 if (MEM_P (operand)
3260                     && ! address_reloaded[i]
3261                     && (GET_CODE (XEXP (operand, 0)) == PRE_DEC
3262                         || GET_CODE (XEXP (operand, 0)) == POST_DEC))
3263                   win = 1;
3264                 break;
3265
3266               case '>':
3267                 if (MEM_P (operand)
3268                     && ! address_reloaded[i]
3269                     && (GET_CODE (XEXP (operand, 0)) == PRE_INC
3270                         || GET_CODE (XEXP (operand, 0)) == POST_INC))
3271                   win = 1;
3272                 break;
3273
3274                 /* Memory operand whose address is not offsettable.  */
3275               case 'V':
3276                 if (force_reload)
3277                   break;
3278                 if (MEM_P (operand)
3279                     && ! (ind_levels ? offsettable_memref_p (operand)
3280                           : offsettable_nonstrict_memref_p (operand))
3281                     /* Certain mem addresses will become offsettable
3282                        after they themselves are reloaded.  This is important;
3283                        we don't want our own handling of unoffsettables
3284                        to override the handling of reg_equiv_address.  */
3285                     && !(REG_P (XEXP (operand, 0))
3286                          && (ind_levels == 0
3287                              || reg_equiv_address (REGNO (XEXP (operand, 0))) != 0)))
3288                   win = 1;
3289                 break;
3290
3291                 /* Memory operand whose address is offsettable.  */
3292               case 'o':
3293                 if (force_reload)
3294                   break;
3295                 if ((MEM_P (operand)
3296                      /* If IND_LEVELS, find_reloads_address won't reload a
3297                         pseudo that didn't get a hard reg, so we have to
3298                         reject that case.  */
3299                      && ((ind_levels ? offsettable_memref_p (operand)
3300                           : offsettable_nonstrict_memref_p (operand))
3301                          /* A reloaded address is offsettable because it is now
3302                             just a simple register indirect.  */
3303                          || address_reloaded[i] == 1))
3304                     || (REG_P (operand)
3305                         && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3306                         && reg_renumber[REGNO (operand)] < 0
3307                         /* If reg_equiv_address is nonzero, we will be
3308                            loading it into a register; hence it will be
3309                            offsettable, but we cannot say that reg_equiv_mem
3310                            is offsettable without checking.  */
3311                         && ((reg_equiv_mem (REGNO (operand)) != 0
3312                              && offsettable_memref_p (reg_equiv_mem (REGNO (operand))))
3313                             || (reg_equiv_address (REGNO (operand)) != 0))))
3314                   win = 1;
3315                 if (CONST_POOL_OK_P (operand_mode[i], operand)
3316                     || MEM_P (operand))
3317                   badop = 0;
3318                 constmemok = 1;
3319                 offmemok = 1;
3320                 break;
3321
3322               case '&':
3323                 /* Output operand that is stored before the need for the
3324                    input operands (and their index registers) is over.  */
3325                 earlyclobber = 1, this_earlyclobber = 1;
3326                 break;
3327
3328               case 'E':
3329               case 'F':
3330                 if (GET_CODE (operand) == CONST_DOUBLE
3331                     || (GET_CODE (operand) == CONST_VECTOR
3332                         && (GET_MODE_CLASS (GET_MODE (operand))
3333                             == MODE_VECTOR_FLOAT)))
3334                   win = 1;
3335                 break;
3336
3337               case 'G':
3338               case 'H':
3339                 if (GET_CODE (operand) == CONST_DOUBLE
3340                     && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (operand, c, p))
3341                   win = 1;
3342                 break;
3343
3344               case 's':
3345                 if (CONST_INT_P (operand)
3346                     || (GET_CODE (operand) == CONST_DOUBLE
3347                         && GET_MODE (operand) == VOIDmode))
3348                   break;
3349               case 'i':
3350                 if (CONSTANT_P (operand)
3351                     && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (operand)))
3352                   win = 1;
3353                 break;
3354
3355               case 'n':
3356                 if (CONST_INT_P (operand)
3357                     || (GET_CODE (operand) == CONST_DOUBLE
3358                         && GET_MODE (operand) == VOIDmode))
3359                   win = 1;
3360                 break;
3361
3362               case 'I':
3363               case 'J':
3364               case 'K':
3365               case 'L':
3366               case 'M':
3367               case 'N':
3368               case 'O':
3369               case 'P':
3370                 if (CONST_INT_P (operand)
3371                     && CONST_OK_FOR_CONSTRAINT_P (INTVAL (operand), c, p))
3372                   win = 1;
3373                 break;
3374
3375               case 'X':
3376                 force_reload = 0;
3377                 win = 1;
3378                 break;
3379
3380               case 'g':
3381                 if (! force_reload
3382                     /* A PLUS is never a valid operand, but reload can make
3383                        it from a register when eliminating registers.  */
3384                     && GET_CODE (operand) != PLUS
3385                     /* A SCRATCH is not a valid operand.  */
3386                     && GET_CODE (operand) != SCRATCH
3387                     && (! CONSTANT_P (operand)
3388                         || ! flag_pic
3389                         || LEGITIMATE_PIC_OPERAND_P (operand))
3390                     && (GENERAL_REGS == ALL_REGS
3391                         || !REG_P (operand)
3392                         || (REGNO (operand) >= FIRST_PSEUDO_REGISTER
3393                             && reg_renumber[REGNO (operand)] < 0)))
3394                   win = 1;
3395                 /* Drop through into 'r' case.  */
3396
3397               case 'r':
3398                 this_alternative[i]
3399                   = reg_class_subunion[this_alternative[i]][(int) GENERAL_REGS];
3400                 goto reg;
3401
3402               default:
3403                 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
3404                   {
3405 #ifdef EXTRA_CONSTRAINT_STR
3406                     if (EXTRA_MEMORY_CONSTRAINT (c, p))
3407                       {
3408                         if (force_reload)
3409                           break;
3410                         if (EXTRA_CONSTRAINT_STR (operand, c, p))
3411                           win = 1;
3412                         /* If the address was already reloaded,
3413                            we win as well.  */
3414                         else if (MEM_P (operand)
3415                                  && address_reloaded[i] == 1)
3416                           win = 1;
3417                         /* Likewise if the address will be reloaded because
3418                            reg_equiv_address is nonzero.  For reg_equiv_mem
3419                            we have to check.  */
3420                         else if (REG_P (operand)
3421                                  && REGNO (operand) >= FIRST_PSEUDO_REGISTER
3422                                  && reg_renumber[REGNO (operand)] < 0
3423                                  && ((reg_equiv_mem (REGNO (operand)) != 0
3424                                       && EXTRA_CONSTRAINT_STR (reg_equiv_mem (REGNO (operand)), c, p))
3425                                      || (reg_equiv_address (REGNO (operand)) != 0)))
3426                           win = 1;
3427
3428                         /* If we didn't already win, we can reload
3429                            constants via force_const_mem, and other
3430                            MEMs by reloading the address like for 'o'.  */
3431                         if (CONST_POOL_OK_P (operand_mode[i], operand)
3432                             || MEM_P (operand))
3433                           badop = 0;
3434                         constmemok = 1;
3435                         offmemok = 1;
3436                         break;
3437                       }
3438                     if (EXTRA_ADDRESS_CONSTRAINT (c, p))
3439                       {
3440                         if (EXTRA_CONSTRAINT_STR (operand, c, p))
3441                           win = 1;
3442
3443                         /* If we didn't already win, we can reload
3444                            the address into a base register.  */
3445                         this_alternative[i] = base_reg_class (VOIDmode,
3446                                                               ADDRESS,
3447                                                               SCRATCH);
3448                         badop = 0;
3449                         break;
3450                       }
3451
3452                     if (EXTRA_CONSTRAINT_STR (operand, c, p))
3453                       win = 1;
3454 #endif
3455                     break;
3456                   }
3457
3458                 this_alternative[i]
3459                   = (reg_class_subunion
3460                      [this_alternative[i]]
3461                      [(int) REG_CLASS_FROM_CONSTRAINT (c, p)]);
3462               reg:
3463                 if (GET_MODE (operand) == BLKmode)
3464                   break;
3465                 winreg = 1;
3466                 if (REG_P (operand)
3467                     && reg_fits_class_p (operand, this_alternative[i],
3468                                          offset, GET_MODE (recog_data.operand[i])))
3469                   win = 1;
3470                 break;
3471               }
3472           while ((p += len), c);
3473
3474           constraints[i] = p;
3475
3476           /* If this operand could be handled with a reg,
3477              and some reg is allowed, then this operand can be handled.  */
3478           if (winreg && this_alternative[i] != NO_REGS
3479               && (win || !class_only_fixed_regs[this_alternative[i]]))
3480             badop = 0;
3481
3482           /* Record which operands fit this alternative.  */
3483           this_alternative_earlyclobber[i] = earlyclobber;
3484           if (win && ! force_reload)
3485             this_alternative_win[i] = 1;
3486           else if (did_match && ! force_reload)
3487             this_alternative_match_win[i] = 1;
3488           else
3489             {
3490               int const_to_mem = 0;
3491
3492               this_alternative_offmemok[i] = offmemok;
3493               losers++;
3494               if (badop)
3495                 bad = 1;
3496               /* Alternative loses if it has no regs for a reg operand.  */
3497               if (REG_P (operand)
3498                   && this_alternative[i] == NO_REGS
3499                   && this_alternative_matches[i] < 0)
3500                 bad = 1;
3501
3502               /* If this is a constant that is reloaded into the desired
3503                  class by copying it to memory first, count that as another
3504                  reload.  This is consistent with other code and is
3505                  required to avoid choosing another alternative when
3506                  the constant is moved into memory by this function on
3507                  an early reload pass.  Note that the test here is
3508                  precisely the same as in the code below that calls
3509                  force_const_mem.  */
3510               if (CONST_POOL_OK_P (operand_mode[i], operand)
3511                   && ((targetm.preferred_reload_class (operand,
3512                                                        this_alternative[i])
3513                        == NO_REGS)
3514                       || no_input_reloads))
3515                 {
3516                   const_to_mem = 1;
3517                   if (this_alternative[i] != NO_REGS)
3518                     losers++;
3519                 }
3520
3521               /* Alternative loses if it requires a type of reload not
3522                  permitted for this insn.  We can always reload SCRATCH
3523                  and objects with a REG_UNUSED note.  */
3524               if (GET_CODE (operand) != SCRATCH
3525                        && modified[i] != RELOAD_READ && no_output_reloads
3526                        && ! find_reg_note (insn, REG_UNUSED, operand))
3527                 bad = 1;
3528               else if (modified[i] != RELOAD_WRITE && no_input_reloads
3529                        && ! const_to_mem)
3530                 bad = 1;
3531
3532               /* If we can't reload this value at all, reject this
3533                  alternative.  Note that we could also lose due to
3534                  LIMIT_RELOAD_CLASS, but we don't check that
3535                  here.  */
3536
3537               if (! CONSTANT_P (operand) && this_alternative[i] != NO_REGS)
3538                 {
3539                   if (targetm.preferred_reload_class (operand, this_alternative[i])
3540                       == NO_REGS)
3541                     reject = 600;
3542
3543                   if (operand_type[i] == RELOAD_FOR_OUTPUT
3544                       && (targetm.preferred_output_reload_class (operand,
3545                                                                  this_alternative[i])
3546                           == NO_REGS))
3547                     reject = 600;
3548                 }
3549
3550               /* We prefer to reload pseudos over reloading other things,
3551                  since such reloads may be able to be eliminated later.
3552                  If we are reloading a SCRATCH, we won't be generating any
3553                  insns, just using a register, so it is also preferred.
3554                  So bump REJECT in other cases.  Don't do this in the
3555                  case where we are forcing a constant into memory and
3556                  it will then win since we don't want to have a different
3557                  alternative match then.  */
3558               if (! (REG_P (operand)
3559                      && REGNO (operand) >= FIRST_PSEUDO_REGISTER)
3560                   && GET_CODE (operand) != SCRATCH
3561                   && ! (const_to_mem && constmemok))
3562                 reject += 2;
3563
3564               /* Input reloads can be inherited more often than output
3565                  reloads can be removed, so penalize output reloads.  */
3566               if (operand_type[i] != RELOAD_FOR_INPUT
3567                   && GET_CODE (operand) != SCRATCH)
3568                 reject++;
3569             }
3570
3571           /* If this operand is a pseudo register that didn't get a hard
3572              reg and this alternative accepts some register, see if the
3573              class that we want is a subset of the preferred class for this
3574              register.  If not, but it intersects that class, use the
3575              preferred class instead.  If it does not intersect the preferred
3576              class, show that usage of this alternative should be discouraged;
3577              it will be discouraged more still if the register is `preferred
3578              or nothing'.  We do this because it increases the chance of
3579              reusing our spill register in a later insn and avoiding a pair
3580              of memory stores and loads.
3581
3582              Don't bother with this if this alternative will accept this
3583              operand.
3584
3585              Don't do this for a multiword operand, since it is only a
3586              small win and has the risk of requiring more spill registers,
3587              which could cause a large loss.
3588
3589              Don't do this if the preferred class has only one register
3590              because we might otherwise exhaust the class.  */
3591
3592           if (! win && ! did_match
3593               && this_alternative[i] != NO_REGS
3594               && GET_MODE_SIZE (operand_mode[i]) <= UNITS_PER_WORD
3595               && reg_class_size [(int) preferred_class[i]] > 0
3596               && ! small_register_class_p (preferred_class[i]))
3597             {
3598               if (! reg_class_subset_p (this_alternative[i],
3599                                         preferred_class[i]))
3600                 {
3601                   /* Since we don't have a way of forming the intersection,
3602                      we just do something special if the preferred class
3603                      is a subset of the class we have; that's the most
3604                      common case anyway.  */
3605                   if (reg_class_subset_p (preferred_class[i],
3606                                           this_alternative[i]))
3607                     this_alternative[i] = preferred_class[i];
3608                   else
3609                     reject += (2 + 2 * pref_or_nothing[i]);
3610                 }
3611             }
3612         }
3613
3614       /* Now see if any output operands that are marked "earlyclobber"
3615          in this alternative conflict with any input operands
3616          or any memory addresses.  */
3617
3618       for (i = 0; i < noperands; i++)
3619         if (this_alternative_earlyclobber[i]
3620             && (this_alternative_win[i] || this_alternative_match_win[i]))
3621           {
3622             struct decomposition early_data;
3623
3624             early_data = decompose (recog_data.operand[i]);
3625
3626             gcc_assert (modified[i] != RELOAD_READ);
3627
3628             if (this_alternative[i] == NO_REGS)
3629               {
3630                 this_alternative_earlyclobber[i] = 0;
3631                 gcc_assert (this_insn_is_asm);
3632                 error_for_asm (this_insn,
3633                                "%<&%> constraint used with no register class");
3634               }
3635
3636             for (j = 0; j < noperands; j++)
3637               /* Is this an input operand or a memory ref?  */
3638               if ((MEM_P (recog_data.operand[j])
3639                    || modified[j] != RELOAD_WRITE)
3640                   && j != i
3641                   /* Ignore things like match_operator operands.  */
3642                   && !recog_data.is_operator[j]
3643                   /* Don't count an input operand that is constrained to match
3644                      the early clobber operand.  */
3645                   && ! (this_alternative_matches[j] == i
3646                         && rtx_equal_p (recog_data.operand[i],
3647                                         recog_data.operand[j]))
3648                   /* Is it altered by storing the earlyclobber operand?  */
3649                   && !immune_p (recog_data.operand[j], recog_data.operand[i],
3650                                 early_data))
3651                 {
3652                   /* If the output is in a non-empty few-regs class,
3653                      it's costly to reload it, so reload the input instead.  */
3654                   if (small_register_class_p (this_alternative[i])
3655                       && (REG_P (recog_data.operand[j])
3656                           || GET_CODE (recog_data.operand[j]) == SUBREG))
3657                     {
3658                       losers++;
3659                       this_alternative_win[j] = 0;
3660                       this_alternative_match_win[j] = 0;
3661                     }
3662                   else
3663                     break;
3664                 }
3665             /* If an earlyclobber operand conflicts with something,
3666                it must be reloaded, so request this and count the cost.  */
3667             if (j != noperands)
3668               {
3669                 losers++;
3670                 this_alternative_win[i] = 0;
3671                 this_alternative_match_win[j] = 0;
3672                 for (j = 0; j < noperands; j++)
3673                   if (this_alternative_matches[j] == i
3674                       && this_alternative_match_win[j])
3675                     {
3676                       this_alternative_win[j] = 0;
3677                       this_alternative_match_win[j] = 0;
3678                       losers++;
3679                     }
3680               }
3681           }
3682
3683       /* If one alternative accepts all the operands, no reload required,
3684          choose that alternative; don't consider the remaining ones.  */
3685       if (losers == 0)
3686         {
3687           /* Unswap these so that they are never swapped at `finish'.  */
3688           if (commutative >= 0)
3689             {
3690               recog_data.operand[commutative] = substed_operand[commutative];
3691               recog_data.operand[commutative + 1]
3692                 = substed_operand[commutative + 1];
3693             }
3694           for (i = 0; i < noperands; i++)
3695             {
3696               goal_alternative_win[i] = this_alternative_win[i];
3697               goal_alternative_match_win[i] = this_alternative_match_win[i];
3698               goal_alternative[i] = this_alternative[i];
3699               goal_alternative_offmemok[i] = this_alternative_offmemok[i];
3700               goal_alternative_matches[i] = this_alternative_matches[i];
3701               goal_alternative_earlyclobber[i]
3702                 = this_alternative_earlyclobber[i];
3703             }
3704           goal_alternative_number = this_alternative_number;
3705           goal_alternative_swapped = swapped;
3706           goal_earlyclobber = this_earlyclobber;
3707           goto finish;
3708         }
3709
3710       /* REJECT, set by the ! and ? constraint characters and when a register
3711          would be reloaded into a non-preferred class, discourages the use of
3712          this alternative for a reload goal.  REJECT is incremented by six
3713          for each ? and two for each non-preferred class.  */
3714       losers = losers * 6 + reject;
3715
3716       /* If this alternative can be made to work by reloading,
3717          and it needs less reloading than the others checked so far,
3718          record it as the chosen goal for reloading.  */
3719       if (! bad)
3720         {
3721           if (best > losers)
3722             {
3723               for (i = 0; i < noperands; i++)
3724                 {
3725                   goal_alternative[i] = this_alternative[i];
3726                   goal_alternative_win[i] = this_alternative_win[i];
3727                   goal_alternative_match_win[i]
3728                     = this_alternative_match_win[i];
3729                   goal_alternative_offmemok[i]
3730                     = this_alternative_offmemok[i];
3731                   goal_alternative_matches[i] = this_alternative_matches[i];
3732                   goal_alternative_earlyclobber[i]
3733                     = this_alternative_earlyclobber[i];
3734                 }
3735               goal_alternative_swapped = swapped;
3736               best = losers;
3737               goal_alternative_number = this_alternative_number;
3738               goal_earlyclobber = this_earlyclobber;
3739             }
3740         }
3741     }
3742
3743   /* If insn is commutative (it's safe to exchange a certain pair of operands)
3744      then we need to try each alternative twice,
3745      the second time matching those two operands
3746      as if we had exchanged them.
3747      To do this, really exchange them in operands.
3748
3749      If we have just tried the alternatives the second time,
3750      return operands to normal and drop through.  */
3751
3752   if (commutative >= 0)
3753     {
3754       swapped = !swapped;
3755       if (swapped)
3756         {
3757           enum reg_class tclass;
3758           int t;
3759
3760           recog_data.operand[commutative] = substed_operand[commutative + 1];
3761           recog_data.operand[commutative + 1] = substed_operand[commutative];
3762           /* Swap the duplicates too.  */
3763           for (i = 0; i < recog_data.n_dups; i++)
3764             if (recog_data.dup_num[i] == commutative
3765                 || recog_data.dup_num[i] == commutative + 1)
3766               *recog_data.dup_loc[i]
3767                  = recog_data.operand[(int) recog_data.dup_num[i]];
3768
3769           tclass = preferred_class[commutative];
3770           preferred_class[commutative] = preferred_class[commutative + 1];
3771           preferred_class[commutative + 1] = tclass;
3772
3773           t = pref_or_nothing[commutative];
3774           pref_or_nothing[commutative] = pref_or_nothing[commutative + 1];
3775           pref_or_nothing[commutative + 1] = t;
3776
3777           t = address_reloaded[commutative];
3778           address_reloaded[commutative] = address_reloaded[commutative + 1];
3779           address_reloaded[commutative + 1] = t;
3780
3781           memcpy (constraints, recog_data.constraints,
3782                   noperands * sizeof (const char *));
3783           goto try_swapped;
3784         }
3785       else
3786         {
3787           recog_data.operand[commutative] = substed_operand[commutative];
3788           recog_data.operand[commutative + 1]
3789             = substed_operand[commutative + 1];
3790           /* Unswap the duplicates too.  */
3791           for (i = 0; i < recog_data.n_dups; i++)
3792             if (recog_data.dup_num[i] == commutative
3793                 || recog_data.dup_num[i] == commutative + 1)
3794               *recog_data.dup_loc[i]
3795                  = recog_data.operand[(int) recog_data.dup_num[i]];
3796         }
3797     }
3798
3799   /* The operands don't meet the constraints.
3800      goal_alternative describes the alternative
3801      that we could reach by reloading the fewest operands.
3802      Reload so as to fit it.  */
3803
3804   if (best == MAX_RECOG_OPERANDS * 2 + 600)
3805     {
3806       /* No alternative works with reloads??  */
3807       if (insn_code_number >= 0)
3808         fatal_insn ("unable to generate reloads for:", insn);
3809       error_for_asm (insn, "inconsistent operand constraints in an %<asm%>");
3810       /* Avoid further trouble with this insn.  */
3811       PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3812       n_reloads = 0;
3813       return 0;
3814     }
3815
3816   /* Jump to `finish' from above if all operands are valid already.
3817      In that case, goal_alternative_win is all 1.  */
3818  finish:
3819
3820   /* Right now, for any pair of operands I and J that are required to match,
3821      with I < J,
3822      goal_alternative_matches[J] is I.
3823      Set up goal_alternative_matched as the inverse function:
3824      goal_alternative_matched[I] = J.  */
3825
3826   for (i = 0; i < noperands; i++)
3827     goal_alternative_matched[i] = -1;
3828
3829   for (i = 0; i < noperands; i++)
3830     if (! goal_alternative_win[i]
3831         && goal_alternative_matches[i] >= 0)
3832       goal_alternative_matched[goal_alternative_matches[i]] = i;
3833
3834   for (i = 0; i < noperands; i++)
3835     goal_alternative_win[i] |= goal_alternative_match_win[i];
3836
3837   /* If the best alternative is with operands 1 and 2 swapped,
3838      consider them swapped before reporting the reloads.  Update the
3839      operand numbers of any reloads already pushed.  */
3840
3841   if (goal_alternative_swapped)
3842     {
3843       rtx tem;
3844
3845       tem = substed_operand[commutative];
3846       substed_operand[commutative] = substed_operand[commutative + 1];
3847       substed_operand[commutative + 1] = tem;
3848       tem = recog_data.operand[commutative];
3849       recog_data.operand[commutative] = recog_data.operand[commutative + 1];
3850       recog_data.operand[commutative + 1] = tem;
3851       tem = *recog_data.operand_loc[commutative];
3852       *recog_data.operand_loc[commutative]
3853         = *recog_data.operand_loc[commutative + 1];
3854       *recog_data.operand_loc[commutative + 1] = tem;
3855
3856       for (i = 0; i < n_reloads; i++)
3857         {
3858           if (rld[i].opnum == commutative)
3859             rld[i].opnum = commutative + 1;
3860           else if (rld[i].opnum == commutative + 1)
3861             rld[i].opnum = commutative;
3862         }
3863     }
3864
3865   for (i = 0; i < noperands; i++)
3866     {
3867       operand_reloadnum[i] = -1;
3868
3869       /* If this is an earlyclobber operand, we need to widen the scope.
3870          The reload must remain valid from the start of the insn being
3871          reloaded until after the operand is stored into its destination.
3872          We approximate this with RELOAD_OTHER even though we know that we
3873          do not conflict with RELOAD_FOR_INPUT_ADDRESS reloads.
3874
3875          One special case that is worth checking is when we have an
3876          output that is earlyclobber but isn't used past the insn (typically
3877          a SCRATCH).  In this case, we only need have the reload live
3878          through the insn itself, but not for any of our input or output
3879          reloads.
3880          But we must not accidentally narrow the scope of an existing
3881          RELOAD_OTHER reload - leave these alone.
3882
3883          In any case, anything needed to address this operand can remain
3884          however they were previously categorized.  */
3885
3886       if (goal_alternative_earlyclobber[i] && operand_type[i] != RELOAD_OTHER)
3887         operand_type[i]
3888           = (find_reg_note (insn, REG_UNUSED, recog_data.operand[i])
3889              ? RELOAD_FOR_INSN : RELOAD_OTHER);
3890     }
3891
3892   /* Any constants that aren't allowed and can't be reloaded
3893      into registers are here changed into memory references.  */
3894   for (i = 0; i < noperands; i++)
3895     if (! goal_alternative_win[i])
3896       {
3897         rtx op = recog_data.operand[i];
3898         rtx subreg = NULL_RTX;
3899         rtx plus = NULL_RTX;
3900         enum machine_mode mode = operand_mode[i];
3901
3902         /* Reloads of SUBREGs of CONSTANT RTXs are handled later in
3903            push_reload so we have to let them pass here.  */
3904         if (GET_CODE (op) == SUBREG)
3905           {
3906             subreg = op;
3907             op = SUBREG_REG (op);
3908             mode = GET_MODE (op);
3909           }
3910
3911         if (GET_CODE (op) == PLUS)
3912           {
3913             plus = op;
3914             op = XEXP (op, 1);
3915           }
3916
3917         if (CONST_POOL_OK_P (mode, op)
3918             && ((targetm.preferred_reload_class (op, goal_alternative[i])
3919                  == NO_REGS)
3920                 || no_input_reloads))
3921           {
3922             int this_address_reloaded;
3923             rtx tem = force_const_mem (mode, op);
3924
3925             /* If we stripped a SUBREG or a PLUS above add it back.  */
3926             if (plus != NULL_RTX)
3927               tem = gen_rtx_PLUS (mode, XEXP (plus, 0), tem);
3928
3929             if (subreg != NULL_RTX)
3930               tem = gen_rtx_SUBREG (operand_mode[i], tem, SUBREG_BYTE (subreg));
3931
3932             this_address_reloaded = 0;
3933             substed_operand[i] = recog_data.operand[i]
3934               = find_reloads_toplev (tem, i, address_type[i], ind_levels,
3935                                      0, insn, &this_address_reloaded);
3936
3937             /* If the alternative accepts constant pool refs directly
3938                there will be no reload needed at all.  */
3939             if (plus == NULL_RTX
3940                 && subreg == NULL_RTX
3941                 && alternative_allows_const_pool_ref (this_address_reloaded == 0
3942                                                       ? substed_operand[i]
3943                                                       : NULL,
3944                                                       recog_data.constraints[i],
3945                                                       goal_alternative_number))
3946               goal_alternative_win[i] = 1;
3947           }
3948       }
3949
3950   /* Record the values of the earlyclobber operands for the caller.  */
3951   if (goal_earlyclobber)
3952     for (i = 0; i < noperands; i++)
3953       if (goal_alternative_earlyclobber[i])
3954         reload_earlyclobbers[n_earlyclobbers++] = recog_data.operand[i];
3955
3956   /* Now record reloads for all the operands that need them.  */
3957   for (i = 0; i < noperands; i++)
3958     if (! goal_alternative_win[i])
3959       {
3960         /* Operands that match previous ones have already been handled.  */
3961         if (goal_alternative_matches[i] >= 0)
3962           ;
3963         /* Handle an operand with a nonoffsettable address
3964            appearing where an offsettable address will do
3965            by reloading the address into a base register.
3966
3967            ??? We can also do this when the operand is a register and
3968            reg_equiv_mem is not offsettable, but this is a bit tricky,
3969            so we don't bother with it.  It may not be worth doing.  */
3970         else if (goal_alternative_matched[i] == -1
3971                  && goal_alternative_offmemok[i]
3972                  && MEM_P (recog_data.operand[i]))
3973           {
3974             /* If the address to be reloaded is a VOIDmode constant,
3975                use the default address mode as mode of the reload register,
3976                as would have been done by find_reloads_address.  */
3977             enum machine_mode address_mode;
3978             address_mode = GET_MODE (XEXP (recog_data.operand[i], 0));
3979             if (address_mode == VOIDmode)
3980               {
3981                 addr_space_t as = MEM_ADDR_SPACE (recog_data.operand[i]);
3982                 address_mode = targetm.addr_space.address_mode (as);
3983               }
3984
3985             operand_reloadnum[i]
3986               = push_reload (XEXP (recog_data.operand[i], 0), NULL_RTX,
3987                              &XEXP (recog_data.operand[i], 0), (rtx*) 0,
3988                              base_reg_class (VOIDmode, MEM, SCRATCH),
3989                              address_mode,
3990                              VOIDmode, 0, 0, i, RELOAD_FOR_INPUT);
3991             rld[operand_reloadnum[i]].inc
3992               = GET_MODE_SIZE (GET_MODE (recog_data.operand[i]));
3993
3994             /* If this operand is an output, we will have made any
3995                reloads for its address as RELOAD_FOR_OUTPUT_ADDRESS, but
3996                now we are treating part of the operand as an input, so
3997                we must change these to RELOAD_FOR_INPUT_ADDRESS.  */
3998
3999             if (modified[i] == RELOAD_WRITE)
4000               {
4001                 for (j = 0; j < n_reloads; j++)
4002                   {
4003                     if (rld[j].opnum == i)
4004                       {
4005                         if (rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS)
4006                           rld[j].when_needed = RELOAD_FOR_INPUT_ADDRESS;
4007                         else if (rld[j].when_needed
4008                                  == RELOAD_FOR_OUTADDR_ADDRESS)
4009                           rld[j].when_needed = RELOAD_FOR_INPADDR_ADDRESS;
4010                       }
4011                   }
4012               }
4013           }
4014         else if (goal_alternative_matched[i] == -1)
4015           {
4016             operand_reloadnum[i]
4017               = push_reload ((modified[i] != RELOAD_WRITE
4018                               ? recog_data.operand[i] : 0),
4019                              (modified[i] != RELOAD_READ
4020                               ? recog_data.operand[i] : 0),
4021                              (modified[i] != RELOAD_WRITE
4022                               ? recog_data.operand_loc[i] : 0),
4023                              (modified[i] != RELOAD_READ
4024                               ? recog_data.operand_loc[i] : 0),
4025                              (enum reg_class) goal_alternative[i],
4026                              (modified[i] == RELOAD_WRITE
4027                               ? VOIDmode : operand_mode[i]),
4028                              (modified[i] == RELOAD_READ
4029                               ? VOIDmode : operand_mode[i]),
4030                              (insn_code_number < 0 ? 0
4031                               : insn_data[insn_code_number].operand[i].strict_low),
4032                              0, i, operand_type[i]);
4033           }
4034         /* In a matching pair of operands, one must be input only
4035            and the other must be output only.
4036            Pass the input operand as IN and the other as OUT.  */
4037         else if (modified[i] == RELOAD_READ
4038                  && modified[goal_alternative_matched[i]] == RELOAD_WRITE)
4039           {
4040             operand_reloadnum[i]
4041               = push_reload (recog_data.operand[i],
4042                              recog_data.operand[goal_alternative_matched[i]],
4043                              recog_data.operand_loc[i],
4044                              recog_data.operand_loc[goal_alternative_matched[i]],
4045                              (enum reg_class) goal_alternative[i],
4046                              operand_mode[i],
4047                              operand_mode[goal_alternative_matched[i]],
4048                              0, 0, i, RELOAD_OTHER);
4049             operand_reloadnum[goal_alternative_matched[i]] = output_reloadnum;
4050           }
4051         else if (modified[i] == RELOAD_WRITE
4052                  && modified[goal_alternative_matched[i]] == RELOAD_READ)
4053           {
4054             operand_reloadnum[goal_alternative_matched[i]]
4055               = push_reload (recog_data.operand[goal_alternative_matched[i]],
4056                              recog_data.operand[i],
4057                              recog_data.operand_loc[goal_alternative_matched[i]],
4058                              recog_data.operand_loc[i],
4059                              (enum reg_class) goal_alternative[i],
4060                              operand_mode[goal_alternative_matched[i]],
4061                              operand_mode[i],
4062                              0, 0, i, RELOAD_OTHER);
4063             operand_reloadnum[i] = output_reloadnum;
4064           }
4065         else
4066           {
4067             gcc_assert (insn_code_number < 0);
4068             error_for_asm (insn, "inconsistent operand constraints "
4069                            "in an %<asm%>");
4070             /* Avoid further trouble with this insn.  */
4071             PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
4072             n_reloads = 0;
4073             return 0;
4074           }
4075       }
4076     else if (goal_alternative_matched[i] < 0
4077              && goal_alternative_matches[i] < 0
4078              && address_operand_reloaded[i] != 1
4079              && optimize)
4080       {
4081         /* For each non-matching operand that's a MEM or a pseudo-register
4082            that didn't get a hard register, make an optional reload.
4083            This may get done even if the insn needs no reloads otherwise.  */
4084
4085         rtx operand = recog_data.operand[i];
4086
4087         while (GET_CODE (operand) == SUBREG)
4088           operand = SUBREG_REG (operand);
4089         if ((MEM_P (operand)
4090              || (REG_P (operand)
4091                  && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4092             /* If this is only for an output, the optional reload would not
4093                actually cause us to use a register now, just note that
4094                something is stored here.  */
4095             && (goal_alternative[i] != NO_REGS
4096                 || modified[i] == RELOAD_WRITE)
4097             && ! no_input_reloads
4098             /* An optional output reload might allow to delete INSN later.
4099                We mustn't make in-out reloads on insns that are not permitted
4100                output reloads.
4101                If this is an asm, we can't delete it; we must not even call
4102                push_reload for an optional output reload in this case,
4103                because we can't be sure that the constraint allows a register,
4104                and push_reload verifies the constraints for asms.  */
4105             && (modified[i] == RELOAD_READ
4106                 || (! no_output_reloads && ! this_insn_is_asm)))
4107           operand_reloadnum[i]
4108             = push_reload ((modified[i] != RELOAD_WRITE
4109                             ? recog_data.operand[i] : 0),
4110                            (modified[i] != RELOAD_READ
4111                             ? recog_data.operand[i] : 0),
4112                            (modified[i] != RELOAD_WRITE
4113                             ? recog_data.operand_loc[i] : 0),
4114                            (modified[i] != RELOAD_READ
4115                             ? recog_data.operand_loc[i] : 0),
4116                            (enum reg_class) goal_alternative[i],
4117                            (modified[i] == RELOAD_WRITE
4118                             ? VOIDmode : operand_mode[i]),
4119                            (modified[i] == RELOAD_READ
4120                             ? VOIDmode : operand_mode[i]),
4121                            (insn_code_number < 0 ? 0
4122                             : insn_data[insn_code_number].operand[i].strict_low),
4123                            1, i, operand_type[i]);
4124         /* If a memory reference remains (either as a MEM or a pseudo that
4125            did not get a hard register), yet we can't make an optional
4126            reload, check if this is actually a pseudo register reference;
4127            we then need to emit a USE and/or a CLOBBER so that reload
4128            inheritance will do the right thing.  */
4129         else if (replace
4130                  && (MEM_P (operand)
4131                      || (REG_P (operand)
4132                          && REGNO (operand) >= FIRST_PSEUDO_REGISTER
4133                          && reg_renumber [REGNO (operand)] < 0)))
4134           {
4135             operand = *recog_data.operand_loc[i];
4136
4137             while (GET_CODE (operand) == SUBREG)
4138               operand = SUBREG_REG (operand);
4139             if (REG_P (operand))
4140               {
4141                 if (modified[i] != RELOAD_WRITE)
4142                   /* We mark the USE with QImode so that we recognize
4143                      it as one that can be safely deleted at the end
4144                      of reload.  */
4145                   PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, operand),
4146                                               insn), QImode);
4147                 if (modified[i] != RELOAD_READ)
4148                   emit_insn_after (gen_clobber (operand), insn);
4149               }
4150           }
4151       }
4152     else if (goal_alternative_matches[i] >= 0
4153              && goal_alternative_win[goal_alternative_matches[i]]
4154              && modified[i] == RELOAD_READ
4155              && modified[goal_alternative_matches[i]] == RELOAD_WRITE
4156              && ! no_input_reloads && ! no_output_reloads
4157              && optimize)
4158       {
4159         /* Similarly, make an optional reload for a pair of matching
4160            objects that are in MEM or a pseudo that didn't get a hard reg.  */
4161
4162         rtx operand = recog_data.operand[i];
4163
4164         while (GET_CODE (operand) == SUBREG)
4165           operand = SUBREG_REG (operand);
4166         if ((MEM_P (operand)
4167              || (REG_P (operand)
4168                  && REGNO (operand) >= FIRST_PSEUDO_REGISTER))
4169             && (goal_alternative[goal_alternative_matches[i]] != NO_REGS))
4170           operand_reloadnum[i] = operand_reloadnum[goal_alternative_matches[i]]
4171             = push_reload (recog_data.operand[goal_alternative_matches[i]],
4172                            recog_data.operand[i],
4173                            recog_data.operand_loc[goal_alternative_matches[i]],
4174                            recog_data.operand_loc[i],
4175                            (enum reg_class) goal_alternative[goal_alternative_matches[i]],
4176                            operand_mode[goal_alternative_matches[i]],
4177                            operand_mode[i],
4178                            0, 1, goal_alternative_matches[i], RELOAD_OTHER);
4179       }
4180
4181   /* Perform whatever substitutions on the operands we are supposed
4182      to make due to commutativity or replacement of registers
4183      with equivalent constants or memory slots.  */
4184
4185   for (i = 0; i < noperands; i++)
4186     {
4187       /* We only do this on the last pass through reload, because it is
4188          possible for some data (like reg_equiv_address) to be changed during
4189          later passes.  Moreover, we lose the opportunity to get a useful
4190          reload_{in,out}_reg when we do these replacements.  */
4191
4192       if (replace)
4193         {
4194           rtx substitution = substed_operand[i];
4195
4196           *recog_data.operand_loc[i] = substitution;
4197
4198           /* If we're replacing an operand with a LABEL_REF, we need to
4199              make sure that there's a REG_LABEL_OPERAND note attached to
4200              this instruction.  */
4201           if (GET_CODE (substitution) == LABEL_REF
4202               && !find_reg_note (insn, REG_LABEL_OPERAND,
4203                                  XEXP (substitution, 0))
4204               /* For a JUMP_P, if it was a branch target it must have
4205                  already been recorded as such.  */
4206               && (!JUMP_P (insn)
4207                   || !label_is_jump_target_p (XEXP (substitution, 0),
4208                                               insn)))
4209             add_reg_note (insn, REG_LABEL_OPERAND, XEXP (substitution, 0));
4210         }
4211       else
4212         retval |= (substed_operand[i] != *recog_data.operand_loc[i]);
4213     }
4214
4215   /* If this insn pattern contains any MATCH_DUP's, make sure that
4216      they will be substituted if the operands they match are substituted.
4217      Also do now any substitutions we already did on the operands.
4218
4219      Don't do this if we aren't making replacements because we might be
4220      propagating things allocated by frame pointer elimination into places
4221      it doesn't expect.  */
4222
4223   if (insn_code_number >= 0 && replace)
4224     for (i = insn_data[insn_code_number].n_dups - 1; i >= 0; i--)
4225       {
4226         int opno = recog_data.dup_num[i];
4227         *recog_data.dup_loc[i] = *recog_data.operand_loc[opno];
4228         dup_replacements (recog_data.dup_loc[i], recog_data.operand_loc[opno]);
4229       }
4230
4231 #if 0
4232   /* This loses because reloading of prior insns can invalidate the equivalence
4233      (or at least find_equiv_reg isn't smart enough to find it any more),
4234      causing this insn to need more reload regs than it needed before.
4235      It may be too late to make the reload regs available.
4236      Now this optimization is done safely in choose_reload_regs.  */
4237
4238   /* For each reload of a reg into some other class of reg,
4239      search for an existing equivalent reg (same value now) in the right class.
4240      We can use it as long as we don't need to change its contents.  */
4241   for (i = 0; i < n_reloads; i++)
4242     if (rld[i].reg_rtx == 0
4243         && rld[i].in != 0
4244         && REG_P (rld[i].in)
4245         && rld[i].out == 0)
4246       {
4247         rld[i].reg_rtx
4248           = find_equiv_reg (rld[i].in, insn, rld[i].rclass, -1,
4249                             static_reload_reg_p, 0, rld[i].inmode);
4250         /* Prevent generation of insn to load the value
4251            because the one we found already has the value.  */
4252         if (rld[i].reg_rtx)
4253           rld[i].in = rld[i].reg_rtx;
4254       }
4255 #endif
4256
4257   /* If we detected error and replaced asm instruction by USE, forget about the
4258      reloads.  */
4259   if (GET_CODE (PATTERN (insn)) == USE
4260       && CONST_INT_P (XEXP (PATTERN (insn), 0)))
4261     n_reloads = 0;
4262
4263   /* Perhaps an output reload can be combined with another
4264      to reduce needs by one.  */
4265   if (!goal_earlyclobber)
4266     combine_reloads ();
4267
4268   /* If we have a pair of reloads for parts of an address, they are reloading
4269      the same object, the operands themselves were not reloaded, and they
4270      are for two operands that are supposed to match, merge the reloads and
4271      change the type of the surviving reload to RELOAD_FOR_OPERAND_ADDRESS.  */
4272
4273   for (i = 0; i < n_reloads; i++)
4274     {
4275       int k;
4276
4277       for (j = i + 1; j < n_reloads; j++)
4278         if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4279              || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4280              || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4281              || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4282             && (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
4283                 || rld[j].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4284                 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4285                 || rld[j].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4286             && rtx_equal_p (rld[i].in, rld[j].in)
4287             && (operand_reloadnum[rld[i].opnum] < 0
4288                 || rld[operand_reloadnum[rld[i].opnum]].optional)
4289             && (operand_reloadnum[rld[j].opnum] < 0
4290                 || rld[operand_reloadnum[rld[j].opnum]].optional)
4291             && (goal_alternative_matches[rld[i].opnum] == rld[j].opnum
4292                 || (goal_alternative_matches[rld[j].opnum]
4293                     == rld[i].opnum)))
4294           {
4295             for (k = 0; k < n_replacements; k++)
4296               if (replacements[k].what == j)
4297                 replacements[k].what = i;
4298
4299             if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4300                 || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4301               rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4302             else
4303               rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4304             rld[j].in = 0;
4305           }
4306     }
4307
4308   /* Scan all the reloads and update their type.
4309      If a reload is for the address of an operand and we didn't reload
4310      that operand, change the type.  Similarly, change the operand number
4311      of a reload when two operands match.  If a reload is optional, treat it
4312      as though the operand isn't reloaded.
4313
4314      ??? This latter case is somewhat odd because if we do the optional
4315      reload, it means the object is hanging around.  Thus we need only
4316      do the address reload if the optional reload was NOT done.
4317
4318      Change secondary reloads to be the address type of their operand, not
4319      the normal type.
4320
4321      If an operand's reload is now RELOAD_OTHER, change any
4322      RELOAD_FOR_INPUT_ADDRESS reloads of that operand to
4323      RELOAD_FOR_OTHER_ADDRESS.  */
4324
4325   for (i = 0; i < n_reloads; i++)
4326     {
4327       if (rld[i].secondary_p
4328           && rld[i].when_needed == operand_type[rld[i].opnum])
4329         rld[i].when_needed = address_type[rld[i].opnum];
4330
4331       if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4332            || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4333            || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4334            || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4335           && (operand_reloadnum[rld[i].opnum] < 0
4336               || rld[operand_reloadnum[rld[i].opnum]].optional))
4337         {
4338           /* If we have a secondary reload to go along with this reload,
4339              change its type to RELOAD_FOR_OPADDR_ADDR.  */
4340
4341           if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4342                || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4343               && rld[i].secondary_in_reload != -1)
4344             {
4345               int secondary_in_reload = rld[i].secondary_in_reload;
4346
4347               rld[secondary_in_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4348
4349               /* If there's a tertiary reload we have to change it also.  */
4350               if (secondary_in_reload > 0
4351                   && rld[secondary_in_reload].secondary_in_reload != -1)
4352                 rld[rld[secondary_in_reload].secondary_in_reload].when_needed
4353                   = RELOAD_FOR_OPADDR_ADDR;
4354             }
4355
4356           if ((rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS
4357                || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4358               && rld[i].secondary_out_reload != -1)
4359             {
4360               int secondary_out_reload = rld[i].secondary_out_reload;
4361
4362               rld[secondary_out_reload].when_needed = RELOAD_FOR_OPADDR_ADDR;
4363
4364               /* If there's a tertiary reload we have to change it also.  */
4365               if (secondary_out_reload
4366                   && rld[secondary_out_reload].secondary_out_reload != -1)
4367                 rld[rld[secondary_out_reload].secondary_out_reload].when_needed
4368                   = RELOAD_FOR_OPADDR_ADDR;
4369             }
4370
4371           if (rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS
4372               || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS)
4373             rld[i].when_needed = RELOAD_FOR_OPADDR_ADDR;
4374           else
4375             rld[i].when_needed = RELOAD_FOR_OPERAND_ADDRESS;
4376         }
4377
4378       if ((rld[i].when_needed == RELOAD_FOR_INPUT_ADDRESS
4379            || rld[i].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
4380           && operand_reloadnum[rld[i].opnum] >= 0
4381           && (rld[operand_reloadnum[rld[i].opnum]].when_needed
4382               == RELOAD_OTHER))
4383         rld[i].when_needed = RELOAD_FOR_OTHER_ADDRESS;
4384
4385       if (goal_alternative_matches[rld[i].opnum] >= 0)
4386         rld[i].opnum = goal_alternative_matches[rld[i].opnum];
4387     }
4388
4389   /* Scan all the reloads, and check for RELOAD_FOR_OPERAND_ADDRESS reloads.
4390      If we have more than one, then convert all RELOAD_FOR_OPADDR_ADDR
4391      reloads to RELOAD_FOR_OPERAND_ADDRESS reloads.
4392
4393      choose_reload_regs assumes that RELOAD_FOR_OPADDR_ADDR reloads never
4394      conflict with RELOAD_FOR_OPERAND_ADDRESS reloads.  This is true for a
4395      single pair of RELOAD_FOR_OPADDR_ADDR/RELOAD_FOR_OPERAND_ADDRESS reloads.
4396      However, if there is more than one RELOAD_FOR_OPERAND_ADDRESS reload,
4397      then a RELOAD_FOR_OPADDR_ADDR reload conflicts with all
4398      RELOAD_FOR_OPERAND_ADDRESS reloads other than the one that uses it.
4399      This is complicated by the fact that a single operand can have more
4400      than one RELOAD_FOR_OPERAND_ADDRESS reload.  It is very difficult to fix
4401      choose_reload_regs without affecting code quality, and cases that
4402      actually fail are extremely rare, so it turns out to be better to fix
4403      the problem here by not generating cases that choose_reload_regs will
4404      fail for.  */
4405   /* There is a similar problem with RELOAD_FOR_INPUT_ADDRESS /
4406      RELOAD_FOR_OUTPUT_ADDRESS when there is more than one of a kind for
4407      a single operand.
4408      We can reduce the register pressure by exploiting that a
4409      RELOAD_FOR_X_ADDR_ADDR that precedes all RELOAD_FOR_X_ADDRESS reloads
4410      does not conflict with any of them, if it is only used for the first of
4411      the RELOAD_FOR_X_ADDRESS reloads.  */
4412   {
4413     int first_op_addr_num = -2;
4414     int first_inpaddr_num[MAX_RECOG_OPERANDS];
4415     int first_outpaddr_num[MAX_RECOG_OPERANDS];
4416     int need_change = 0;
4417     /* We use last_op_addr_reload and the contents of the above arrays
4418        first as flags - -2 means no instance encountered, -1 means exactly
4419        one instance encountered.
4420        If more than one instance has been encountered, we store the reload
4421        number of the first reload of the kind in question; reload numbers
4422        are known to be non-negative.  */
4423     for (i = 0; i < noperands; i++)
4424       first_inpaddr_num[i] = first_outpaddr_num[i] = -2;
4425     for (i = n_reloads - 1; i >= 0; i--)
4426       {
4427         switch (rld[i].when_needed)
4428           {
4429           case RELOAD_FOR_OPERAND_ADDRESS:
4430             if (++first_op_addr_num >= 0)
4431               {
4432                 first_op_addr_num = i;
4433                 need_change = 1;
4434               }
4435             break;
4436           case RELOAD_FOR_INPUT_ADDRESS:
4437             if (++first_inpaddr_num[rld[i].opnum] >= 0)
4438               {
4439                 first_inpaddr_num[rld[i].opnum] = i;
4440                 need_change = 1;
4441               }
4442             break;
4443           case RELOAD_FOR_OUTPUT_ADDRESS:
4444             if (++first_outpaddr_num[rld[i].opnum] >= 0)
4445               {
4446                 first_outpaddr_num[rld[i].opnum] = i;
4447                 need_change = 1;
4448               }
4449             break;
4450           default:
4451             break;
4452           }
4453       }
4454
4455     if (need_change)
4456       {
4457         for (i = 0; i < n_reloads; i++)
4458           {
4459             int first_num;
4460             enum reload_type type;
4461
4462             switch (rld[i].when_needed)
4463               {
4464               case RELOAD_FOR_OPADDR_ADDR:
4465                 first_num = first_op_addr_num;
4466                 type = RELOAD_FOR_OPERAND_ADDRESS;
4467                 break;
4468               case RELOAD_FOR_INPADDR_ADDRESS:
4469                 first_num = first_inpaddr_num[rld[i].opnum];
4470                 type = RELOAD_FOR_INPUT_ADDRESS;
4471                 break;
4472               case RELOAD_FOR_OUTADDR_ADDRESS:
4473                 first_num = first_outpaddr_num[rld[i].opnum];
4474                 type = RELOAD_FOR_OUTPUT_ADDRESS;
4475                 break;
4476               default:
4477                 continue;
4478               }
4479             if (first_num < 0)
4480               continue;
4481             else if (i > first_num)
4482               rld[i].when_needed = type;
4483             else
4484               {
4485                 /* Check if the only TYPE reload that uses reload I is
4486                    reload FIRST_NUM.  */
4487                 for (j = n_reloads - 1; j > first_num; j--)
4488                   {
4489                     if (rld[j].when_needed == type
4490                         && (rld[i].secondary_p
4491                             ? rld[j].secondary_in_reload == i
4492                             : reg_mentioned_p (rld[i].in, rld[j].in)))
4493                       {
4494                         rld[i].when_needed = type;
4495                         break;
4496                       }
4497                   }
4498               }
4499           }
4500       }
4501   }
4502
4503   /* See if we have any reloads that are now allowed to be merged
4504      because we've changed when the reload is needed to
4505      RELOAD_FOR_OPERAND_ADDRESS or RELOAD_FOR_OTHER_ADDRESS.  Only
4506      check for the most common cases.  */
4507
4508   for (i = 0; i < n_reloads; i++)
4509     if (rld[i].in != 0 && rld[i].out == 0
4510         && (rld[i].when_needed == RELOAD_FOR_OPERAND_ADDRESS
4511             || rld[i].when_needed == RELOAD_FOR_OPADDR_ADDR
4512             || rld[i].when_needed == RELOAD_FOR_OTHER_ADDRESS))
4513       for (j = 0; j < n_reloads; j++)
4514         if (i != j && rld[j].in != 0 && rld[j].out == 0
4515             && rld[j].when_needed == rld[i].when_needed
4516             && MATCHES (rld[i].in, rld[j].in)
4517             && rld[i].rclass == rld[j].rclass
4518             && !rld[i].nocombine && !rld[j].nocombine
4519             && rld[i].reg_rtx == rld[j].reg_rtx)
4520           {
4521             rld[i].opnum = MIN (rld[i].opnum, rld[j].opnum);
4522             transfer_replacements (i, j);
4523             rld[j].in = 0;
4524           }
4525
4526 #ifdef HAVE_cc0
4527   /* If we made any reloads for addresses, see if they violate a
4528      "no input reloads" requirement for this insn.  But loads that we
4529      do after the insn (such as for output addresses) are fine.  */
4530   if (no_input_reloads)
4531     for (i = 0; i < n_reloads; i++)
4532       gcc_assert (rld[i].in == 0
4533                   || rld[i].when_needed == RELOAD_FOR_OUTADDR_ADDRESS
4534                   || rld[i].when_needed == RELOAD_FOR_OUTPUT_ADDRESS);
4535 #endif
4536
4537   /* Compute reload_mode and reload_nregs.  */
4538   for (i = 0; i < n_reloads; i++)
4539     {
4540       rld[i].mode
4541         = (rld[i].inmode == VOIDmode
4542            || (GET_MODE_SIZE (rld[i].outmode)
4543                > GET_MODE_SIZE (rld[i].inmode)))
4544           ? rld[i].outmode : rld[i].inmode;
4545
4546       rld[i].nregs = CLASS_MAX_NREGS (rld[i].rclass, rld[i].mode);
4547     }
4548
4549   /* Special case a simple move with an input reload and a
4550      destination of a hard reg, if the hard reg is ok, use it.  */
4551   for (i = 0; i < n_reloads; i++)
4552     if (rld[i].when_needed == RELOAD_FOR_INPUT
4553         && GET_CODE (PATTERN (insn)) == SET
4554         && REG_P (SET_DEST (PATTERN (insn)))
4555         && (SET_SRC (PATTERN (insn)) == rld[i].in
4556             || SET_SRC (PATTERN (insn)) == rld[i].in_reg)
4557         && !elimination_target_reg_p (SET_DEST (PATTERN (insn))))
4558       {
4559         rtx dest = SET_DEST (PATTERN (insn));
4560         unsigned int regno = REGNO (dest);
4561
4562         if (regno < FIRST_PSEUDO_REGISTER
4563             && TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno)
4564             && HARD_REGNO_MODE_OK (regno, rld[i].mode))
4565           {
4566             int nr = hard_regno_nregs[regno][rld[i].mode];
4567             int ok = 1, nri;
4568
4569             for (nri = 1; nri < nr; nri ++)
4570               if (! TEST_HARD_REG_BIT (reg_class_contents[rld[i].rclass], regno + nri))
4571                 ok = 0;
4572
4573             if (ok)
4574               rld[i].reg_rtx = dest;
4575           }
4576       }
4577
4578   return retval;
4579 }
4580
4581 /* Return true if alternative number ALTNUM in constraint-string
4582    CONSTRAINT is guaranteed to accept a reloaded constant-pool reference.
4583    MEM gives the reference if it didn't need any reloads, otherwise it
4584    is null.  */
4585
4586 static bool
4587 alternative_allows_const_pool_ref (rtx mem ATTRIBUTE_UNUSED,
4588                                    const char *constraint, int altnum)
4589 {
4590   int c;
4591
4592   /* Skip alternatives before the one requested.  */
4593   while (altnum > 0)
4594     {
4595       while (*constraint++ != ',');
4596       altnum--;
4597     }
4598   /* Scan the requested alternative for TARGET_MEM_CONSTRAINT or 'o'.
4599      If one of them is present, this alternative accepts the result of
4600      passing a constant-pool reference through find_reloads_toplev.
4601
4602      The same is true of extra memory constraints if the address
4603      was reloaded into a register.  However, the target may elect
4604      to disallow the original constant address, forcing it to be
4605      reloaded into a register instead.  */
4606   for (; (c = *constraint) && c != ',' && c != '#';
4607        constraint += CONSTRAINT_LEN (c, constraint))
4608     {
4609       if (c == TARGET_MEM_CONSTRAINT || c == 'o')
4610         return true;
4611 #ifdef EXTRA_CONSTRAINT_STR
4612       if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
4613           && (mem == NULL || EXTRA_CONSTRAINT_STR (mem, c, constraint)))
4614         return true;
4615 #endif
4616     }
4617   return false;
4618 }
4619 \f
4620 /* Scan X for memory references and scan the addresses for reloading.
4621    Also checks for references to "constant" regs that we want to eliminate
4622    and replaces them with the values they stand for.
4623    We may alter X destructively if it contains a reference to such.
4624    If X is just a constant reg, we return the equivalent value
4625    instead of X.
4626
4627    IND_LEVELS says how many levels of indirect addressing this machine
4628    supports.
4629
4630    OPNUM and TYPE identify the purpose of the reload.
4631
4632    IS_SET_DEST is true if X is the destination of a SET, which is not
4633    appropriate to be replaced by a constant.
4634
4635    INSN, if nonzero, is the insn in which we do the reload.  It is used
4636    to determine if we may generate output reloads, and where to put USEs
4637    for pseudos that we have to replace with stack slots.
4638
4639    ADDRESS_RELOADED.  If nonzero, is a pointer to where we put the
4640    result of find_reloads_address.  */
4641
4642 static rtx
4643 find_reloads_toplev (rtx x, int opnum, enum reload_type type,
4644                      int ind_levels, int is_set_dest, rtx insn,
4645                      int *address_reloaded)
4646 {
4647   RTX_CODE code = GET_CODE (x);
4648
4649   const char *fmt = GET_RTX_FORMAT (code);
4650   int i;
4651   int copied;
4652
4653   if (code == REG)
4654     {
4655       /* This code is duplicated for speed in find_reloads.  */
4656       int regno = REGNO (x);
4657       if (reg_equiv_constant (regno) != 0 && !is_set_dest)
4658         x = reg_equiv_constant (regno);
4659 #if 0
4660       /*  This creates (subreg (mem...)) which would cause an unnecessary
4661           reload of the mem.  */
4662       else if (reg_equiv_mem (regno) != 0)
4663         x = reg_equiv_mem (regno);
4664 #endif
4665       else if (reg_equiv_memory_loc (regno)
4666                && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
4667         {
4668           rtx mem = make_memloc (x, regno);
4669           if (reg_equiv_address (regno)
4670               || ! rtx_equal_p (mem, reg_equiv_mem (regno)))
4671             {
4672               /* If this is not a toplevel operand, find_reloads doesn't see
4673                  this substitution.  We have to emit a USE of the pseudo so
4674                  that delete_output_reload can see it.  */
4675               if (replace_reloads && recog_data.operand[opnum] != x)
4676                 /* We mark the USE with QImode so that we recognize it
4677                    as one that can be safely deleted at the end of
4678                    reload.  */
4679                 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, x), insn),
4680                           QImode);
4681               x = mem;
4682               i = find_reloads_address (GET_MODE (x), &x, XEXP (x, 0), &XEXP (x, 0),
4683                                         opnum, type, ind_levels, insn);
4684               if (!rtx_equal_p (x, mem))
4685                 push_reg_equiv_alt_mem (regno, x);
4686               if (address_reloaded)
4687                 *address_reloaded = i;
4688             }
4689         }
4690       return x;
4691     }
4692   if (code == MEM)
4693     {
4694       rtx tem = x;
4695
4696       i = find_reloads_address (GET_MODE (x), &tem, XEXP (x, 0), &XEXP (x, 0),
4697                                 opnum, type, ind_levels, insn);
4698       if (address_reloaded)
4699         *address_reloaded = i;
4700
4701       return tem;
4702     }
4703
4704   if (code == SUBREG && REG_P (SUBREG_REG (x)))
4705     {
4706       /* Check for SUBREG containing a REG that's equivalent to a
4707          constant.  If the constant has a known value, truncate it
4708          right now.  Similarly if we are extracting a single-word of a
4709          multi-word constant.  If the constant is symbolic, allow it
4710          to be substituted normally.  push_reload will strip the
4711          subreg later.  The constant must not be VOIDmode, because we
4712          will lose the mode of the register (this should never happen
4713          because one of the cases above should handle it).  */
4714
4715       int regno = REGNO (SUBREG_REG (x));
4716       rtx tem;
4717
4718       if (regno >= FIRST_PSEUDO_REGISTER
4719           && reg_renumber[regno] < 0
4720           && reg_equiv_constant (regno) != 0)
4721         {
4722           tem =
4723             simplify_gen_subreg (GET_MODE (x), reg_equiv_constant (regno),
4724                                  GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
4725           gcc_assert (tem);
4726           if (CONSTANT_P (tem)
4727               && !targetm.legitimate_constant_p (GET_MODE (x), tem))
4728             {
4729               tem = force_const_mem (GET_MODE (x), tem);
4730               i = find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4731                                         &XEXP (tem, 0), opnum, type,
4732                                         ind_levels, insn);
4733               if (address_reloaded)
4734                 *address_reloaded = i;
4735             }
4736           return tem;
4737         }
4738
4739       /* If the subreg contains a reg that will be converted to a mem,
4740          convert the subreg to a narrower memref now.
4741          Otherwise, we would get (subreg (mem ...) ...),
4742          which would force reload of the mem.
4743
4744          We also need to do this if there is an equivalent MEM that is
4745          not offsettable.  In that case, alter_subreg would produce an
4746          invalid address on big-endian machines.
4747
4748          For machines that extend byte loads, we must not reload using
4749          a wider mode if we have a paradoxical SUBREG.  find_reloads will
4750          force a reload in that case.  So we should not do anything here.  */
4751
4752       if (regno >= FIRST_PSEUDO_REGISTER
4753 #ifdef LOAD_EXTEND_OP
4754                && (GET_MODE_SIZE (GET_MODE (x))
4755                    <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4756 #endif
4757                && (reg_equiv_address (regno) != 0
4758                    || (reg_equiv_mem (regno) != 0
4759                        && (! strict_memory_address_addr_space_p
4760                                (GET_MODE (x), XEXP (reg_equiv_mem (regno), 0),
4761                                 MEM_ADDR_SPACE (reg_equiv_mem (regno)))
4762                            || ! offsettable_memref_p (reg_equiv_mem (regno))
4763                            || num_not_at_initial_offset))))
4764         x = find_reloads_subreg_address (x, 1, opnum, type, ind_levels,
4765                                            insn, address_reloaded);
4766     }
4767
4768   for (copied = 0, i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4769     {
4770       if (fmt[i] == 'e')
4771         {
4772           rtx new_part = find_reloads_toplev (XEXP (x, i), opnum, type,
4773                                               ind_levels, is_set_dest, insn,
4774                                               address_reloaded);
4775           /* If we have replaced a reg with it's equivalent memory loc -
4776              that can still be handled here e.g. if it's in a paradoxical
4777              subreg - we must make the change in a copy, rather than using
4778              a destructive change.  This way, find_reloads can still elect
4779              not to do the change.  */
4780           if (new_part != XEXP (x, i) && ! CONSTANT_P (new_part) && ! copied)
4781             {
4782               x = shallow_copy_rtx (x);
4783               copied = 1;
4784             }
4785           XEXP (x, i) = new_part;
4786         }
4787     }
4788   return x;
4789 }
4790
4791 /* Return a mem ref for the memory equivalent of reg REGNO.
4792    This mem ref is not shared with anything.  */
4793
4794 static rtx
4795 make_memloc (rtx ad, int regno)
4796 {
4797   /* We must rerun eliminate_regs, in case the elimination
4798      offsets have changed.  */
4799   rtx tem
4800     = XEXP (eliminate_regs (reg_equiv_memory_loc (regno), VOIDmode, NULL_RTX),
4801             0);
4802
4803   /* If TEM might contain a pseudo, we must copy it to avoid
4804      modifying it when we do the substitution for the reload.  */
4805   if (rtx_varies_p (tem, 0))
4806     tem = copy_rtx (tem);
4807
4808   tem = replace_equiv_address_nv (reg_equiv_memory_loc (regno), tem);
4809   tem = adjust_address_nv (tem, GET_MODE (ad), 0);
4810
4811   /* Copy the result if it's still the same as the equivalence, to avoid
4812      modifying it when we do the substitution for the reload.  */
4813   if (tem == reg_equiv_memory_loc (regno))
4814     tem = copy_rtx (tem);
4815   return tem;
4816 }
4817
4818 /* Returns true if AD could be turned into a valid memory reference
4819    to mode MODE in address space AS by reloading the part pointed to
4820    by PART into a register.  */
4821
4822 static int
4823 maybe_memory_address_addr_space_p (enum machine_mode mode, rtx ad,
4824                                    addr_space_t as, rtx *part)
4825 {
4826   int retv;
4827   rtx tem = *part;
4828   rtx reg = gen_rtx_REG (GET_MODE (tem), max_reg_num ());
4829
4830   *part = reg;
4831   retv = memory_address_addr_space_p (mode, ad, as);
4832   *part = tem;
4833
4834   return retv;
4835 }
4836
4837 /* Record all reloads needed for handling memory address AD
4838    which appears in *LOC in a memory reference to mode MODE
4839    which itself is found in location  *MEMREFLOC.
4840    Note that we take shortcuts assuming that no multi-reg machine mode
4841    occurs as part of an address.
4842
4843    OPNUM and TYPE specify the purpose of this reload.
4844
4845    IND_LEVELS says how many levels of indirect addressing this machine
4846    supports.
4847
4848    INSN, if nonzero, is the insn in which we do the reload.  It is used
4849    to determine if we may generate output reloads, and where to put USEs
4850    for pseudos that we have to replace with stack slots.
4851
4852    Value is one if this address is reloaded or replaced as a whole; it is
4853    zero if the top level of this address was not reloaded or replaced, and
4854    it is -1 if it may or may not have been reloaded or replaced.
4855
4856    Note that there is no verification that the address will be valid after
4857    this routine does its work.  Instead, we rely on the fact that the address
4858    was valid when reload started.  So we need only undo things that reload
4859    could have broken.  These are wrong register types, pseudos not allocated
4860    to a hard register, and frame pointer elimination.  */
4861
4862 static int
4863 find_reloads_address (enum machine_mode mode, rtx *memrefloc, rtx ad,
4864                       rtx *loc, int opnum, enum reload_type type,
4865                       int ind_levels, rtx insn)
4866 {
4867   addr_space_t as = memrefloc? MEM_ADDR_SPACE (*memrefloc)
4868                              : ADDR_SPACE_GENERIC;
4869   int regno;
4870   int removed_and = 0;
4871   int op_index;
4872   rtx tem;
4873
4874   /* If the address is a register, see if it is a legitimate address and
4875      reload if not.  We first handle the cases where we need not reload
4876      or where we must reload in a non-standard way.  */
4877
4878   if (REG_P (ad))
4879     {
4880       regno = REGNO (ad);
4881
4882       if (reg_equiv_constant (regno) != 0)
4883         {
4884           find_reloads_address_part (reg_equiv_constant (regno), loc,
4885                                      base_reg_class (mode, MEM, SCRATCH),
4886                                      GET_MODE (ad), opnum, type, ind_levels);
4887           return 1;
4888         }
4889
4890       tem = reg_equiv_memory_loc (regno);
4891       if (tem != 0)
4892         {
4893           if (reg_equiv_address (regno) != 0 || num_not_at_initial_offset)
4894             {
4895               tem = make_memloc (ad, regno);
4896               if (! strict_memory_address_addr_space_p (GET_MODE (tem),
4897                                                         XEXP (tem, 0),
4898                                                         MEM_ADDR_SPACE (tem)))
4899                 {
4900                   rtx orig = tem;
4901
4902                   find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
4903                                         &XEXP (tem, 0), opnum,
4904                                         ADDR_TYPE (type), ind_levels, insn);
4905                   if (!rtx_equal_p (tem, orig))
4906                     push_reg_equiv_alt_mem (regno, tem);
4907                 }
4908               /* We can avoid a reload if the register's equivalent memory
4909                  expression is valid as an indirect memory address.
4910                  But not all addresses are valid in a mem used as an indirect
4911                  address: only reg or reg+constant.  */
4912
4913               if (ind_levels > 0
4914                   && strict_memory_address_addr_space_p (mode, tem, as)
4915                   && (REG_P (XEXP (tem, 0))
4916                       || (GET_CODE (XEXP (tem, 0)) == PLUS
4917                           && REG_P (XEXP (XEXP (tem, 0), 0))
4918                           && CONSTANT_P (XEXP (XEXP (tem, 0), 1)))))
4919                 {
4920                   /* TEM is not the same as what we'll be replacing the
4921                      pseudo with after reload, put a USE in front of INSN
4922                      in the final reload pass.  */
4923                   if (replace_reloads
4924                       && num_not_at_initial_offset
4925                       && ! rtx_equal_p (tem, reg_equiv_mem (regno)))
4926                     {
4927                       *loc = tem;
4928                       /* We mark the USE with QImode so that we
4929                          recognize it as one that can be safely
4930                          deleted at the end of reload.  */
4931                       PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad),
4932                                                   insn), QImode);
4933
4934                       /* This doesn't really count as replacing the address
4935                          as a whole, since it is still a memory access.  */
4936                     }
4937                   return 0;
4938                 }
4939               ad = tem;
4940             }
4941         }
4942
4943       /* The only remaining case where we can avoid a reload is if this is a
4944          hard register that is valid as a base register and which is not the
4945          subject of a CLOBBER in this insn.  */
4946
4947       else if (regno < FIRST_PSEUDO_REGISTER
4948                && regno_ok_for_base_p (regno, mode, MEM, SCRATCH)
4949                && ! regno_clobbered_p (regno, this_insn, mode, 0))
4950         return 0;
4951
4952       /* If we do not have one of the cases above, we must do the reload.  */
4953       push_reload (ad, NULL_RTX, loc, (rtx*) 0, base_reg_class (mode, MEM, SCRATCH),
4954                    GET_MODE (ad), VOIDmode, 0, 0, opnum, type);
4955       return 1;
4956     }
4957
4958   if (strict_memory_address_addr_space_p (mode, ad, as))
4959     {
4960       /* The address appears valid, so reloads are not needed.
4961          But the address may contain an eliminable register.
4962          This can happen because a machine with indirect addressing
4963          may consider a pseudo register by itself a valid address even when
4964          it has failed to get a hard reg.
4965          So do a tree-walk to find and eliminate all such regs.  */
4966
4967       /* But first quickly dispose of a common case.  */
4968       if (GET_CODE (ad) == PLUS
4969           && CONST_INT_P (XEXP (ad, 1))
4970           && REG_P (XEXP (ad, 0))
4971           && reg_equiv_constant (REGNO (XEXP (ad, 0))) == 0)
4972         return 0;
4973
4974       subst_reg_equivs_changed = 0;
4975       *loc = subst_reg_equivs (ad, insn);
4976
4977       if (! subst_reg_equivs_changed)
4978         return 0;
4979
4980       /* Check result for validity after substitution.  */
4981       if (strict_memory_address_addr_space_p (mode, ad, as))
4982         return 0;
4983     }
4984
4985 #ifdef LEGITIMIZE_RELOAD_ADDRESS
4986   do
4987     {
4988       if (memrefloc && ADDR_SPACE_GENERIC_P (as))
4989         {
4990           LEGITIMIZE_RELOAD_ADDRESS (ad, GET_MODE (*memrefloc), opnum, type,
4991                                      ind_levels, win);
4992         }
4993       break;
4994     win:
4995       *memrefloc = copy_rtx (*memrefloc);
4996       XEXP (*memrefloc, 0) = ad;
4997       move_replacements (&ad, &XEXP (*memrefloc, 0));
4998       return -1;
4999     }
5000   while (0);
5001 #endif
5002
5003   /* The address is not valid.  We have to figure out why.  First see if
5004      we have an outer AND and remove it if so.  Then analyze what's inside.  */
5005
5006   if (GET_CODE (ad) == AND)
5007     {
5008       removed_and = 1;
5009       loc = &XEXP (ad, 0);
5010       ad = *loc;
5011     }
5012
5013   /* One possibility for why the address is invalid is that it is itself
5014      a MEM.  This can happen when the frame pointer is being eliminated, a
5015      pseudo is not allocated to a hard register, and the offset between the
5016      frame and stack pointers is not its initial value.  In that case the
5017      pseudo will have been replaced by a MEM referring to the
5018      stack pointer.  */
5019   if (MEM_P (ad))
5020     {
5021       /* First ensure that the address in this MEM is valid.  Then, unless
5022          indirect addresses are valid, reload the MEM into a register.  */
5023       tem = ad;
5024       find_reloads_address (GET_MODE (ad), &tem, XEXP (ad, 0), &XEXP (ad, 0),
5025                             opnum, ADDR_TYPE (type),
5026                             ind_levels == 0 ? 0 : ind_levels - 1, insn);
5027
5028       /* If tem was changed, then we must create a new memory reference to
5029          hold it and store it back into memrefloc.  */
5030       if (tem != ad && memrefloc)
5031         {
5032           *memrefloc = copy_rtx (*memrefloc);
5033           copy_replacements (tem, XEXP (*memrefloc, 0));
5034           loc = &XEXP (*memrefloc, 0);
5035           if (removed_and)
5036             loc = &XEXP (*loc, 0);
5037         }
5038
5039       /* Check similar cases as for indirect addresses as above except
5040          that we can allow pseudos and a MEM since they should have been
5041          taken care of above.  */
5042
5043       if (ind_levels == 0
5044           || (GET_CODE (XEXP (tem, 0)) == SYMBOL_REF && ! indirect_symref_ok)
5045           || MEM_P (XEXP (tem, 0))
5046           || ! (REG_P (XEXP (tem, 0))
5047                 || (GET_CODE (XEXP (tem, 0)) == PLUS
5048                     && REG_P (XEXP (XEXP (tem, 0), 0))
5049                     && CONST_INT_P (XEXP (XEXP (tem, 0), 1)))))
5050         {
5051           /* Must use TEM here, not AD, since it is the one that will
5052              have any subexpressions reloaded, if needed.  */
5053           push_reload (tem, NULL_RTX, loc, (rtx*) 0,
5054                        base_reg_class (mode, MEM, SCRATCH), GET_MODE (tem),
5055                        VOIDmode, 0,
5056                        0, opnum, type);
5057           return ! removed_and;
5058         }
5059       else
5060         return 0;
5061     }
5062
5063   /* If we have address of a stack slot but it's not valid because the
5064      displacement is too large, compute the sum in a register.
5065      Handle all base registers here, not just fp/ap/sp, because on some
5066      targets (namely SH) we can also get too large displacements from
5067      big-endian corrections.  */
5068   else if (GET_CODE (ad) == PLUS
5069            && REG_P (XEXP (ad, 0))
5070            && REGNO (XEXP (ad, 0)) < FIRST_PSEUDO_REGISTER
5071            && CONST_INT_P (XEXP (ad, 1))
5072            && (regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode, PLUS,
5073                                     CONST_INT)
5074                /* Similarly, if we were to reload the base register and the
5075                   mem+offset address is still invalid, then we want to reload
5076                   the whole address, not just the base register.  */
5077                || ! maybe_memory_address_addr_space_p
5078                      (mode, ad, as, &(XEXP (ad, 0)))))
5079
5080     {
5081       /* Unshare the MEM rtx so we can safely alter it.  */
5082       if (memrefloc)
5083         {
5084           *memrefloc = copy_rtx (*memrefloc);
5085           loc = &XEXP (*memrefloc, 0);
5086           if (removed_and)
5087             loc = &XEXP (*loc, 0);
5088         }
5089
5090       if (double_reg_address_ok
5091           && regno_ok_for_base_p (REGNO (XEXP (ad, 0)), mode,
5092                                   PLUS, CONST_INT))
5093         {
5094           /* Unshare the sum as well.  */
5095           *loc = ad = copy_rtx (ad);
5096
5097           /* Reload the displacement into an index reg.
5098              We assume the frame pointer or arg pointer is a base reg.  */
5099           find_reloads_address_part (XEXP (ad, 1), &XEXP (ad, 1),
5100                                      INDEX_REG_CLASS, GET_MODE (ad), opnum,
5101                                      type, ind_levels);
5102           return 0;
5103         }
5104       else
5105         {
5106           /* If the sum of two regs is not necessarily valid,
5107              reload the sum into a base reg.
5108              That will at least work.  */
5109           find_reloads_address_part (ad, loc,
5110                                      base_reg_class (mode, MEM, SCRATCH),
5111                                      GET_MODE (ad), opnum, type, ind_levels);
5112         }
5113       return ! removed_and;
5114     }
5115
5116   /* If we have an indexed stack slot, there are three possible reasons why
5117      it might be invalid: The index might need to be reloaded, the address
5118      might have been made by frame pointer elimination and hence have a
5119      constant out of range, or both reasons might apply.
5120
5121      We can easily check for an index needing reload, but even if that is the
5122      case, we might also have an invalid constant.  To avoid making the
5123      conservative assumption and requiring two reloads, we see if this address
5124      is valid when not interpreted strictly.  If it is, the only problem is
5125      that the index needs a reload and find_reloads_address_1 will take care
5126      of it.
5127
5128      Handle all base registers here, not just fp/ap/sp, because on some
5129      targets (namely SPARC) we can also get invalid addresses from preventive
5130      subreg big-endian corrections made by find_reloads_toplev.  We
5131      can also get expressions involving LO_SUM (rather than PLUS) from
5132      find_reloads_subreg_address.
5133
5134      If we decide to do something, it must be that `double_reg_address_ok'
5135      is true.  We generate a reload of the base register + constant and
5136      rework the sum so that the reload register will be added to the index.
5137      This is safe because we know the address isn't shared.
5138
5139      We check for the base register as both the first and second operand of
5140      the innermost PLUS and/or LO_SUM.  */
5141
5142   for (op_index = 0; op_index < 2; ++op_index)
5143     {
5144       rtx operand, addend;
5145       enum rtx_code inner_code;
5146
5147       if (GET_CODE (ad) != PLUS)
5148           continue;
5149
5150       inner_code = GET_CODE (XEXP (ad, 0));
5151       if (!(GET_CODE (ad) == PLUS
5152             && CONST_INT_P (XEXP (ad, 1))
5153             && (inner_code == PLUS || inner_code == LO_SUM)))
5154         continue;
5155
5156       operand = XEXP (XEXP (ad, 0), op_index);
5157       if (!REG_P (operand) || REGNO (operand) >= FIRST_PSEUDO_REGISTER)
5158         continue;
5159
5160       addend = XEXP (XEXP (ad, 0), 1 - op_index);
5161
5162       if ((regno_ok_for_base_p (REGNO (operand), mode, inner_code,
5163                                 GET_CODE (addend))
5164            || operand == frame_pointer_rtx
5165 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
5166            || operand == hard_frame_pointer_rtx
5167 #endif
5168 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5169            || operand == arg_pointer_rtx
5170 #endif
5171            || operand == stack_pointer_rtx)
5172           && ! maybe_memory_address_addr_space_p
5173                 (mode, ad, as, &XEXP (XEXP (ad, 0), 1 - op_index)))
5174         {
5175           rtx offset_reg;
5176           enum reg_class cls;
5177
5178           offset_reg = plus_constant (operand, INTVAL (XEXP (ad, 1)));
5179
5180           /* Form the adjusted address.  */
5181           if (GET_CODE (XEXP (ad, 0)) == PLUS)
5182             ad = gen_rtx_PLUS (GET_MODE (ad),
5183                                op_index == 0 ? offset_reg : addend,
5184                                op_index == 0 ? addend : offset_reg);
5185           else
5186             ad = gen_rtx_LO_SUM (GET_MODE (ad),
5187                                  op_index == 0 ? offset_reg : addend,
5188                                  op_index == 0 ? addend : offset_reg);
5189           *loc = ad;
5190
5191           cls = base_reg_class (mode, MEM, GET_CODE (addend));
5192           find_reloads_address_part (XEXP (ad, op_index),
5193                                      &XEXP (ad, op_index), cls,
5194                                      GET_MODE (ad), opnum, type, ind_levels);
5195           find_reloads_address_1 (mode,
5196                                   XEXP (ad, 1 - op_index), 1, GET_CODE (ad),
5197                                   GET_CODE (XEXP (ad, op_index)),
5198                                   &XEXP (ad, 1 - op_index), opnum,
5199                                   type, 0, insn);
5200
5201           return 0;
5202         }
5203     }
5204
5205   /* See if address becomes valid when an eliminable register
5206      in a sum is replaced.  */
5207
5208   tem = ad;
5209   if (GET_CODE (ad) == PLUS)
5210     tem = subst_indexed_address (ad);
5211   if (tem != ad && strict_memory_address_addr_space_p (mode, tem, as))
5212     {
5213       /* Ok, we win that way.  Replace any additional eliminable
5214          registers.  */
5215
5216       subst_reg_equivs_changed = 0;
5217       tem = subst_reg_equivs (tem, insn);
5218
5219       /* Make sure that didn't make the address invalid again.  */
5220
5221       if (! subst_reg_equivs_changed
5222           || strict_memory_address_addr_space_p (mode, tem, as))
5223         {
5224           *loc = tem;
5225           return 0;
5226         }
5227     }
5228
5229   /* If constants aren't valid addresses, reload the constant address
5230      into a register.  */
5231   if (CONSTANT_P (ad) && ! strict_memory_address_addr_space_p (mode, ad, as))
5232     {
5233       enum machine_mode address_mode = GET_MODE (ad);
5234       if (address_mode == VOIDmode)
5235         address_mode = targetm.addr_space.address_mode (as);
5236
5237       /* If AD is an address in the constant pool, the MEM rtx may be shared.
5238          Unshare it so we can safely alter it.  */
5239       if (memrefloc && GET_CODE (ad) == SYMBOL_REF
5240           && CONSTANT_POOL_ADDRESS_P (ad))
5241         {
5242           *memrefloc = copy_rtx (*memrefloc);
5243           loc = &XEXP (*memrefloc, 0);
5244           if (removed_and)
5245             loc = &XEXP (*loc, 0);
5246         }
5247
5248       find_reloads_address_part (ad, loc, base_reg_class (mode, MEM, SCRATCH),
5249                                  address_mode, opnum, type, ind_levels);
5250       return ! removed_and;
5251     }
5252
5253   return find_reloads_address_1 (mode, ad, 0, MEM, SCRATCH, loc, opnum, type,
5254                                  ind_levels, insn);
5255 }
5256 \f
5257 /* Find all pseudo regs appearing in AD
5258    that are eliminable in favor of equivalent values
5259    and do not have hard regs; replace them by their equivalents.
5260    INSN, if nonzero, is the insn in which we do the reload.  We put USEs in
5261    front of it for pseudos that we have to replace with stack slots.  */
5262
5263 static rtx
5264 subst_reg_equivs (rtx ad, rtx insn)
5265 {
5266   RTX_CODE code = GET_CODE (ad);
5267   int i;
5268   const char *fmt;
5269
5270   switch (code)
5271     {
5272     case HIGH:
5273     case CONST_INT:
5274     case CONST:
5275     case CONST_DOUBLE:
5276     case CONST_FIXED:
5277     case CONST_VECTOR:
5278     case SYMBOL_REF:
5279     case LABEL_REF:
5280     case PC:
5281     case CC0:
5282       return ad;
5283
5284     case REG:
5285       {
5286         int regno = REGNO (ad);
5287
5288         if (reg_equiv_constant (regno) != 0)
5289           {
5290             subst_reg_equivs_changed = 1;
5291             return reg_equiv_constant (regno);
5292           }
5293         if (reg_equiv_memory_loc (regno) && num_not_at_initial_offset)
5294           {
5295             rtx mem = make_memloc (ad, regno);
5296             if (! rtx_equal_p (mem, reg_equiv_mem (regno)))
5297               {
5298                 subst_reg_equivs_changed = 1;
5299                 /* We mark the USE with QImode so that we recognize it
5300                    as one that can be safely deleted at the end of
5301                    reload.  */
5302                 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode, ad), insn),
5303                           QImode);
5304                 return mem;
5305               }
5306           }
5307       }
5308       return ad;
5309
5310     case PLUS:
5311       /* Quickly dispose of a common case.  */
5312       if (XEXP (ad, 0) == frame_pointer_rtx
5313           && CONST_INT_P (XEXP (ad, 1)))
5314         return ad;
5315       break;
5316
5317     default:
5318       break;
5319     }
5320
5321   fmt = GET_RTX_FORMAT (code);
5322   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5323     if (fmt[i] == 'e')
5324       XEXP (ad, i) = subst_reg_equivs (XEXP (ad, i), insn);
5325   return ad;
5326 }
5327 \f
5328 /* Compute the sum of X and Y, making canonicalizations assumed in an
5329    address, namely: sum constant integers, surround the sum of two
5330    constants with a CONST, put the constant as the second operand, and
5331    group the constant on the outermost sum.
5332
5333    This routine assumes both inputs are already in canonical form.  */
5334
5335 rtx
5336 form_sum (enum machine_mode mode, rtx x, rtx y)
5337 {
5338   rtx tem;
5339
5340   gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
5341   gcc_assert (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode);
5342
5343   if (CONST_INT_P (x))
5344     return plus_constant (y, INTVAL (x));
5345   else if (CONST_INT_P (y))
5346     return plus_constant (x, INTVAL (y));
5347   else if (CONSTANT_P (x))
5348     tem = x, x = y, y = tem;
5349
5350   if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
5351     return form_sum (mode, XEXP (x, 0), form_sum (mode, XEXP (x, 1), y));
5352
5353   /* Note that if the operands of Y are specified in the opposite
5354      order in the recursive calls below, infinite recursion will occur.  */
5355   if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
5356     return form_sum (mode, form_sum (mode, x, XEXP (y, 0)), XEXP (y, 1));
5357
5358   /* If both constant, encapsulate sum.  Otherwise, just form sum.  A
5359      constant will have been placed second.  */
5360   if (CONSTANT_P (x) && CONSTANT_P (y))
5361     {
5362       if (GET_CODE (x) == CONST)
5363         x = XEXP (x, 0);
5364       if (GET_CODE (y) == CONST)
5365         y = XEXP (y, 0);
5366
5367       return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
5368     }
5369
5370   return gen_rtx_PLUS (mode, x, y);
5371 }
5372 \f
5373 /* If ADDR is a sum containing a pseudo register that should be
5374    replaced with a constant (from reg_equiv_constant),
5375    return the result of doing so, and also apply the associative
5376    law so that the result is more likely to be a valid address.
5377    (But it is not guaranteed to be one.)
5378
5379    Note that at most one register is replaced, even if more are
5380    replaceable.  Also, we try to put the result into a canonical form
5381    so it is more likely to be a valid address.
5382
5383    In all other cases, return ADDR.  */
5384
5385 static rtx
5386 subst_indexed_address (rtx addr)
5387 {
5388   rtx op0 = 0, op1 = 0, op2 = 0;
5389   rtx tem;
5390   int regno;
5391
5392   if (GET_CODE (addr) == PLUS)
5393     {
5394       /* Try to find a register to replace.  */
5395       op0 = XEXP (addr, 0), op1 = XEXP (addr, 1), op2 = 0;
5396       if (REG_P (op0)
5397           && (regno = REGNO (op0)) >= FIRST_PSEUDO_REGISTER
5398           && reg_renumber[regno] < 0
5399           && reg_equiv_constant (regno) != 0)
5400         op0 = reg_equiv_constant (regno);
5401       else if (REG_P (op1)
5402                && (regno = REGNO (op1)) >= FIRST_PSEUDO_REGISTER
5403                && reg_renumber[regno] < 0
5404                && reg_equiv_constant (regno) != 0)
5405         op1 = reg_equiv_constant (regno);
5406       else if (GET_CODE (op0) == PLUS
5407                && (tem = subst_indexed_address (op0)) != op0)
5408         op0 = tem;
5409       else if (GET_CODE (op1) == PLUS
5410                && (tem = subst_indexed_address (op1)) != op1)
5411         op1 = tem;
5412       else
5413         return addr;
5414
5415       /* Pick out up to three things to add.  */
5416       if (GET_CODE (op1) == PLUS)
5417         op2 = XEXP (op1, 1), op1 = XEXP (op1, 0);
5418       else if (GET_CODE (op0) == PLUS)
5419         op2 = op1, op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5420
5421       /* Compute the sum.  */
5422       if (op2 != 0)
5423         op1 = form_sum (GET_MODE (addr), op1, op2);
5424       if (op1 != 0)
5425         op0 = form_sum (GET_MODE (addr), op0, op1);
5426
5427       return op0;
5428     }
5429   return addr;
5430 }
5431 \f
5432 /* Update the REG_INC notes for an insn.  It updates all REG_INC
5433    notes for the instruction which refer to REGNO the to refer
5434    to the reload number.
5435
5436    INSN is the insn for which any REG_INC notes need updating.
5437
5438    REGNO is the register number which has been reloaded.
5439
5440    RELOADNUM is the reload number.  */
5441
5442 static void
5443 update_auto_inc_notes (rtx insn ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED,
5444                        int reloadnum ATTRIBUTE_UNUSED)
5445 {
5446 #ifdef AUTO_INC_DEC
5447   rtx link;
5448
5449   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
5450     if (REG_NOTE_KIND (link) == REG_INC
5451         && (int) REGNO (XEXP (link, 0)) == regno)
5452       push_replacement (&XEXP (link, 0), reloadnum, VOIDmode);
5453 #endif
5454 }
5455 \f
5456 /* Record the pseudo registers we must reload into hard registers in a
5457    subexpression of a would-be memory address, X referring to a value
5458    in mode MODE.  (This function is not called if the address we find
5459    is strictly valid.)
5460
5461    CONTEXT = 1 means we are considering regs as index regs,
5462    = 0 means we are considering them as base regs.
5463    OUTER_CODE is the code of the enclosing RTX, typically a MEM, a PLUS,
5464    or an autoinc code.
5465    If CONTEXT == 0 and OUTER_CODE is a PLUS or LO_SUM, then INDEX_CODE
5466    is the code of the index part of the address.  Otherwise, pass SCRATCH
5467    for this argument.
5468    OPNUM and TYPE specify the purpose of any reloads made.
5469
5470    IND_LEVELS says how many levels of indirect addressing are
5471    supported at this point in the address.
5472
5473    INSN, if nonzero, is the insn in which we do the reload.  It is used
5474    to determine if we may generate output reloads.
5475
5476    We return nonzero if X, as a whole, is reloaded or replaced.  */
5477
5478 /* Note that we take shortcuts assuming that no multi-reg machine mode
5479    occurs as part of an address.
5480    Also, this is not fully machine-customizable; it works for machines
5481    such as VAXen and 68000's and 32000's, but other possible machines
5482    could have addressing modes that this does not handle right.
5483    If you add push_reload calls here, you need to make sure gen_reload
5484    handles those cases gracefully.  */
5485
5486 static int
5487 find_reloads_address_1 (enum machine_mode mode, rtx x, int context,
5488                         enum rtx_code outer_code, enum rtx_code index_code,
5489                         rtx *loc, int opnum, enum reload_type type,
5490                         int ind_levels, rtx insn)
5491 {
5492 #define REG_OK_FOR_CONTEXT(CONTEXT, REGNO, MODE, OUTER, INDEX)          \
5493   ((CONTEXT) == 0                                                       \
5494    ? regno_ok_for_base_p (REGNO, MODE, OUTER, INDEX)                    \
5495    : REGNO_OK_FOR_INDEX_P (REGNO))
5496
5497   enum reg_class context_reg_class;
5498   RTX_CODE code = GET_CODE (x);
5499
5500   if (context == 1)
5501     context_reg_class = INDEX_REG_CLASS;
5502   else
5503     context_reg_class = base_reg_class (mode, outer_code, index_code);
5504
5505   switch (code)
5506     {
5507     case PLUS:
5508       {
5509         rtx orig_op0 = XEXP (x, 0);
5510         rtx orig_op1 = XEXP (x, 1);
5511         RTX_CODE code0 = GET_CODE (orig_op0);
5512         RTX_CODE code1 = GET_CODE (orig_op1);
5513         rtx op0 = orig_op0;
5514         rtx op1 = orig_op1;
5515
5516         if (GET_CODE (op0) == SUBREG)
5517           {
5518             op0 = SUBREG_REG (op0);
5519             code0 = GET_CODE (op0);
5520             if (code0 == REG && REGNO (op0) < FIRST_PSEUDO_REGISTER)
5521               op0 = gen_rtx_REG (word_mode,
5522                                  (REGNO (op0) +
5523                                   subreg_regno_offset (REGNO (SUBREG_REG (orig_op0)),
5524                                                        GET_MODE (SUBREG_REG (orig_op0)),
5525                                                        SUBREG_BYTE (orig_op0),
5526                                                        GET_MODE (orig_op0))));
5527           }
5528
5529         if (GET_CODE (op1) == SUBREG)
5530           {
5531             op1 = SUBREG_REG (op1);
5532             code1 = GET_CODE (op1);
5533             if (code1 == REG && REGNO (op1) < FIRST_PSEUDO_REGISTER)
5534               /* ??? Why is this given op1's mode and above for
5535                  ??? op0 SUBREGs we use word_mode?  */
5536               op1 = gen_rtx_REG (GET_MODE (op1),
5537                                  (REGNO (op1) +
5538                                   subreg_regno_offset (REGNO (SUBREG_REG (orig_op1)),
5539                                                        GET_MODE (SUBREG_REG (orig_op1)),
5540                                                        SUBREG_BYTE (orig_op1),
5541                                                        GET_MODE (orig_op1))));
5542           }
5543         /* Plus in the index register may be created only as a result of
5544            register rematerialization for expression like &localvar*4.  Reload it.
5545            It may be possible to combine the displacement on the outer level,
5546            but it is probably not worthwhile to do so.  */
5547         if (context == 1)
5548           {
5549             find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5550                                   opnum, ADDR_TYPE (type), ind_levels, insn);
5551             push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5552                          context_reg_class,
5553                          GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5554             return 1;
5555           }
5556
5557         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
5558             || code0 == ZERO_EXTEND || code1 == MEM)
5559           {
5560             find_reloads_address_1 (mode, orig_op0, 1, PLUS, SCRATCH,
5561                                     &XEXP (x, 0), opnum, type, ind_levels,
5562                                     insn);
5563             find_reloads_address_1 (mode, orig_op1, 0, PLUS, code0,
5564                                     &XEXP (x, 1), opnum, type, ind_levels,
5565                                     insn);
5566           }
5567
5568         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
5569                  || code1 == ZERO_EXTEND || code0 == MEM)
5570           {
5571             find_reloads_address_1 (mode, orig_op0, 0, PLUS, code1,
5572                                     &XEXP (x, 0), opnum, type, ind_levels,
5573                                     insn);
5574             find_reloads_address_1 (mode, orig_op1, 1, PLUS, SCRATCH,
5575                                     &XEXP (x, 1), opnum, type, ind_levels,
5576                                     insn);
5577           }
5578
5579         else if (code0 == CONST_INT || code0 == CONST
5580                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
5581           find_reloads_address_1 (mode, orig_op1, 0, PLUS, code0,
5582                                   &XEXP (x, 1), opnum, type, ind_levels,
5583                                   insn);
5584
5585         else if (code1 == CONST_INT || code1 == CONST
5586                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
5587           find_reloads_address_1 (mode, orig_op0, 0, PLUS, code1,
5588                                   &XEXP (x, 0), opnum, type, ind_levels,
5589                                   insn);
5590
5591         else if (code0 == REG && code1 == REG)
5592           {
5593             if (REGNO_OK_FOR_INDEX_P (REGNO (op1))
5594                 && regno_ok_for_base_p (REGNO (op0), mode, PLUS, REG))
5595               return 0;
5596             else if (REGNO_OK_FOR_INDEX_P (REGNO (op0))
5597                      && regno_ok_for_base_p (REGNO (op1), mode, PLUS, REG))
5598               return 0;
5599             else if (regno_ok_for_base_p (REGNO (op0), mode, PLUS, REG))
5600               find_reloads_address_1 (mode, orig_op1, 1, PLUS, SCRATCH,
5601                                       &XEXP (x, 1), opnum, type, ind_levels,
5602                                       insn);
5603             else if (REGNO_OK_FOR_INDEX_P (REGNO (op1)))
5604               find_reloads_address_1 (mode, orig_op0, 0, PLUS, REG,
5605                                       &XEXP (x, 0), opnum, type, ind_levels,
5606                                       insn);
5607             else if (regno_ok_for_base_p (REGNO (op1), mode, PLUS, REG))
5608               find_reloads_address_1 (mode, orig_op0, 1, PLUS, SCRATCH,
5609                                       &XEXP (x, 0), opnum, type, ind_levels,
5610                                       insn);
5611             else if (REGNO_OK_FOR_INDEX_P (REGNO (op0)))
5612               find_reloads_address_1 (mode, orig_op1, 0, PLUS, REG,
5613                                       &XEXP (x, 1), opnum, type, ind_levels,
5614                                       insn);
5615             else
5616               {
5617                 find_reloads_address_1 (mode, orig_op0, 0, PLUS, REG,
5618                                         &XEXP (x, 0), opnum, type, ind_levels,
5619                                         insn);
5620                 find_reloads_address_1 (mode, orig_op1, 1, PLUS, SCRATCH,
5621                                         &XEXP (x, 1), opnum, type, ind_levels,
5622                                         insn);
5623               }
5624           }
5625
5626         else if (code0 == REG)
5627           {
5628             find_reloads_address_1 (mode, orig_op0, 1, PLUS, SCRATCH,
5629                                     &XEXP (x, 0), opnum, type, ind_levels,
5630                                     insn);
5631             find_reloads_address_1 (mode, orig_op1, 0, PLUS, REG,
5632                                     &XEXP (x, 1), opnum, type, ind_levels,
5633                                     insn);
5634           }
5635
5636         else if (code1 == REG)
5637           {
5638             find_reloads_address_1 (mode, orig_op1, 1, PLUS, SCRATCH,
5639                                     &XEXP (x, 1), opnum, type, ind_levels,
5640                                     insn);
5641             find_reloads_address_1 (mode, orig_op0, 0, PLUS, REG,
5642                                     &XEXP (x, 0), opnum, type, ind_levels,
5643                                     insn);
5644           }
5645       }
5646
5647       return 0;
5648
5649     case POST_MODIFY:
5650     case PRE_MODIFY:
5651       {
5652         rtx op0 = XEXP (x, 0);
5653         rtx op1 = XEXP (x, 1);
5654         enum rtx_code index_code;
5655         int regno;
5656         int reloadnum;
5657
5658         if (GET_CODE (op1) != PLUS && GET_CODE (op1) != MINUS)
5659           return 0;
5660
5661         /* Currently, we only support {PRE,POST}_MODIFY constructs
5662            where a base register is {inc,dec}remented by the contents
5663            of another register or by a constant value.  Thus, these
5664            operands must match.  */
5665         gcc_assert (op0 == XEXP (op1, 0));
5666
5667         /* Require index register (or constant).  Let's just handle the
5668            register case in the meantime... If the target allows
5669            auto-modify by a constant then we could try replacing a pseudo
5670            register with its equivalent constant where applicable.
5671
5672            We also handle the case where the register was eliminated
5673            resulting in a PLUS subexpression.
5674
5675            If we later decide to reload the whole PRE_MODIFY or
5676            POST_MODIFY, inc_for_reload might clobber the reload register
5677            before reading the index.  The index register might therefore
5678            need to live longer than a TYPE reload normally would, so be
5679            conservative and class it as RELOAD_OTHER.  */
5680         if ((REG_P (XEXP (op1, 1))
5681              && !REGNO_OK_FOR_INDEX_P (REGNO (XEXP (op1, 1))))
5682             || GET_CODE (XEXP (op1, 1)) == PLUS)
5683           find_reloads_address_1 (mode, XEXP (op1, 1), 1, code, SCRATCH,
5684                                   &XEXP (op1, 1), opnum, RELOAD_OTHER,
5685                                   ind_levels, insn);
5686
5687         gcc_assert (REG_P (XEXP (op1, 0)));
5688
5689         regno = REGNO (XEXP (op1, 0));
5690         index_code = GET_CODE (XEXP (op1, 1));
5691
5692         /* A register that is incremented cannot be constant!  */
5693         gcc_assert (regno < FIRST_PSEUDO_REGISTER
5694                     || reg_equiv_constant (regno) == 0);
5695
5696         /* Handle a register that is equivalent to a memory location
5697             which cannot be addressed directly.  */
5698         if (reg_equiv_memory_loc (regno) != 0
5699             && (reg_equiv_address (regno) != 0
5700                 || num_not_at_initial_offset))
5701           {
5702             rtx tem = make_memloc (XEXP (x, 0), regno);
5703
5704             if (reg_equiv_address (regno)
5705                 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5706               {
5707                 rtx orig = tem;
5708
5709                 /* First reload the memory location's address.
5710                     We can't use ADDR_TYPE (type) here, because we need to
5711                     write back the value after reading it, hence we actually
5712                     need two registers.  */
5713                 find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5714                                       &XEXP (tem, 0), opnum,
5715                                       RELOAD_OTHER,
5716                                       ind_levels, insn);
5717
5718                 if (!rtx_equal_p (tem, orig))
5719                   push_reg_equiv_alt_mem (regno, tem);
5720
5721                 /* Then reload the memory location into a base
5722                    register.  */
5723                 reloadnum = push_reload (tem, tem, &XEXP (x, 0),
5724                                          &XEXP (op1, 0),
5725                                          base_reg_class (mode, code,
5726                                                          index_code),
5727                                          GET_MODE (x), GET_MODE (x), 0,
5728                                          0, opnum, RELOAD_OTHER);
5729
5730                 update_auto_inc_notes (this_insn, regno, reloadnum);
5731                 return 0;
5732               }
5733           }
5734
5735         if (reg_renumber[regno] >= 0)
5736           regno = reg_renumber[regno];
5737
5738         /* We require a base register here...  */
5739         if (!regno_ok_for_base_p (regno, GET_MODE (x), code, index_code))
5740           {
5741             reloadnum = push_reload (XEXP (op1, 0), XEXP (x, 0),
5742                                      &XEXP (op1, 0), &XEXP (x, 0),
5743                                      base_reg_class (mode, code, index_code),
5744                                      GET_MODE (x), GET_MODE (x), 0, 0,
5745                                      opnum, RELOAD_OTHER);
5746
5747             update_auto_inc_notes (this_insn, regno, reloadnum);
5748             return 0;
5749           }
5750       }
5751       return 0;
5752
5753     case POST_INC:
5754     case POST_DEC:
5755     case PRE_INC:
5756     case PRE_DEC:
5757       if (REG_P (XEXP (x, 0)))
5758         {
5759           int regno = REGNO (XEXP (x, 0));
5760           int value = 0;
5761           rtx x_orig = x;
5762
5763           /* A register that is incremented cannot be constant!  */
5764           gcc_assert (regno < FIRST_PSEUDO_REGISTER
5765                       || reg_equiv_constant (regno) == 0);
5766
5767           /* Handle a register that is equivalent to a memory location
5768              which cannot be addressed directly.  */
5769           if (reg_equiv_memory_loc (regno) != 0
5770               && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5771             {
5772               rtx tem = make_memloc (XEXP (x, 0), regno);
5773               if (reg_equiv_address (regno)
5774                   || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5775                 {
5776                   rtx orig = tem;
5777
5778                   /* First reload the memory location's address.
5779                      We can't use ADDR_TYPE (type) here, because we need to
5780                      write back the value after reading it, hence we actually
5781                      need two registers.  */
5782                   find_reloads_address (GET_MODE (tem), &tem, XEXP (tem, 0),
5783                                         &XEXP (tem, 0), opnum, type,
5784                                         ind_levels, insn);
5785                   if (!rtx_equal_p (tem, orig))
5786                     push_reg_equiv_alt_mem (regno, tem);
5787                   /* Put this inside a new increment-expression.  */
5788                   x = gen_rtx_fmt_e (GET_CODE (x), GET_MODE (x), tem);
5789                   /* Proceed to reload that, as if it contained a register.  */
5790                 }
5791             }
5792
5793           /* If we have a hard register that is ok in this incdec context,
5794              don't make a reload.  If the register isn't nice enough for
5795              autoincdec, we can reload it.  But, if an autoincrement of a
5796              register that we here verified as playing nice, still outside
5797              isn't "valid", it must be that no autoincrement is "valid".
5798              If that is true and something made an autoincrement anyway,
5799              this must be a special context where one is allowed.
5800              (For example, a "push" instruction.)
5801              We can't improve this address, so leave it alone.  */
5802
5803           /* Otherwise, reload the autoincrement into a suitable hard reg
5804              and record how much to increment by.  */
5805
5806           if (reg_renumber[regno] >= 0)
5807             regno = reg_renumber[regno];
5808           if (regno >= FIRST_PSEUDO_REGISTER
5809               || !REG_OK_FOR_CONTEXT (context, regno, mode, code,
5810                                       index_code))
5811             {
5812               int reloadnum;
5813
5814               /* If we can output the register afterwards, do so, this
5815                  saves the extra update.
5816                  We can do so if we have an INSN - i.e. no JUMP_INSN nor
5817                  CALL_INSN - and it does not set CC0.
5818                  But don't do this if we cannot directly address the
5819                  memory location, since this will make it harder to
5820                  reuse address reloads, and increases register pressure.
5821                  Also don't do this if we can probably update x directly.  */
5822               rtx equiv = (MEM_P (XEXP (x, 0))
5823                            ? XEXP (x, 0)
5824                            : reg_equiv_mem (regno));
5825               enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5826               if (insn && NONJUMP_INSN_P (insn) && equiv
5827                   && memory_operand (equiv, GET_MODE (equiv))
5828 #ifdef HAVE_cc0
5829                   && ! sets_cc0_p (PATTERN (insn))
5830 #endif
5831                   && ! (icode != CODE_FOR_nothing
5832                         && insn_operand_matches (icode, 0, equiv)
5833                         && insn_operand_matches (icode, 1, equiv)))
5834                 {
5835                   /* We use the original pseudo for loc, so that
5836                      emit_reload_insns() knows which pseudo this
5837                      reload refers to and updates the pseudo rtx, not
5838                      its equivalent memory location, as well as the
5839                      corresponding entry in reg_last_reload_reg.  */
5840                   loc = &XEXP (x_orig, 0);
5841                   x = XEXP (x, 0);
5842                   reloadnum
5843                     = push_reload (x, x, loc, loc,
5844                                    context_reg_class,
5845                                    GET_MODE (x), GET_MODE (x), 0, 0,
5846                                    opnum, RELOAD_OTHER);
5847                 }
5848               else
5849                 {
5850                   reloadnum
5851                     = push_reload (x, x, loc, (rtx*) 0,
5852                                    context_reg_class,
5853                                    GET_MODE (x), GET_MODE (x), 0, 0,
5854                                    opnum, type);
5855                   rld[reloadnum].inc
5856                     = find_inc_amount (PATTERN (this_insn), XEXP (x_orig, 0));
5857
5858                   value = 1;
5859                 }
5860
5861               update_auto_inc_notes (this_insn, REGNO (XEXP (x_orig, 0)),
5862                                      reloadnum);
5863             }
5864           return value;
5865         }
5866       return 0;
5867
5868     case TRUNCATE:
5869     case SIGN_EXTEND:
5870     case ZERO_EXTEND:
5871       /* Look for parts to reload in the inner expression and reload them
5872          too, in addition to this operation.  Reloading all inner parts in
5873          addition to this one shouldn't be necessary, but at this point,
5874          we don't know if we can possibly omit any part that *can* be
5875          reloaded.  Targets that are better off reloading just either part
5876          (or perhaps even a different part of an outer expression), should
5877          define LEGITIMIZE_RELOAD_ADDRESS.  */
5878       find_reloads_address_1 (GET_MODE (XEXP (x, 0)), XEXP (x, 0),
5879                               context, code, SCRATCH, &XEXP (x, 0), opnum,
5880                               type, ind_levels, insn);
5881       push_reload (x, NULL_RTX, loc, (rtx*) 0,
5882                    context_reg_class,
5883                    GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5884       return 1;
5885
5886     case MEM:
5887       /* This is probably the result of a substitution, by eliminate_regs, of
5888          an equivalent address for a pseudo that was not allocated to a hard
5889          register.  Verify that the specified address is valid and reload it
5890          into a register.
5891
5892          Since we know we are going to reload this item, don't decrement for
5893          the indirection level.
5894
5895          Note that this is actually conservative:  it would be slightly more
5896          efficient to use the value of SPILL_INDIRECT_LEVELS from
5897          reload1.c here.  */
5898
5899       find_reloads_address (GET_MODE (x), loc, XEXP (x, 0), &XEXP (x, 0),
5900                             opnum, ADDR_TYPE (type), ind_levels, insn);
5901       push_reload (*loc, NULL_RTX, loc, (rtx*) 0,
5902                    context_reg_class,
5903                    GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5904       return 1;
5905
5906     case REG:
5907       {
5908         int regno = REGNO (x);
5909
5910         if (reg_equiv_constant (regno) != 0)
5911           {
5912             find_reloads_address_part (reg_equiv_constant (regno), loc,
5913                                        context_reg_class,
5914                                        GET_MODE (x), opnum, type, ind_levels);
5915             return 1;
5916           }
5917
5918 #if 0 /* This might screw code in reload1.c to delete prior output-reload
5919          that feeds this insn.  */
5920         if (reg_equiv_mem (regno) != 0)
5921           {
5922             push_reload (reg_equiv_mem (regno), NULL_RTX, loc, (rtx*) 0,
5923                          context_reg_class,
5924                          GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5925             return 1;
5926           }
5927 #endif
5928
5929         if (reg_equiv_memory_loc (regno)
5930             && (reg_equiv_address (regno) != 0 || num_not_at_initial_offset))
5931           {
5932             rtx tem = make_memloc (x, regno);
5933             if (reg_equiv_address (regno) != 0
5934                 || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
5935               {
5936                 x = tem;
5937                 find_reloads_address (GET_MODE (x), &x, XEXP (x, 0),
5938                                       &XEXP (x, 0), opnum, ADDR_TYPE (type),
5939                                       ind_levels, insn);
5940                 if (!rtx_equal_p (x, tem))
5941                   push_reg_equiv_alt_mem (regno, x);
5942               }
5943           }
5944
5945         if (reg_renumber[regno] >= 0)
5946           regno = reg_renumber[regno];
5947
5948         if (regno >= FIRST_PSEUDO_REGISTER
5949             || !REG_OK_FOR_CONTEXT (context, regno, mode, outer_code,
5950                                     index_code))
5951           {
5952             push_reload (x, NULL_RTX, loc, (rtx*) 0,
5953                          context_reg_class,
5954                          GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5955             return 1;
5956           }
5957
5958         /* If a register appearing in an address is the subject of a CLOBBER
5959            in this insn, reload it into some other register to be safe.
5960            The CLOBBER is supposed to make the register unavailable
5961            from before this insn to after it.  */
5962         if (regno_clobbered_p (regno, this_insn, GET_MODE (x), 0))
5963           {
5964             push_reload (x, NULL_RTX, loc, (rtx*) 0,
5965                          context_reg_class,
5966                          GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5967             return 1;
5968           }
5969       }
5970       return 0;
5971
5972     case SUBREG:
5973       if (REG_P (SUBREG_REG (x)))
5974         {
5975           /* If this is a SUBREG of a hard register and the resulting register
5976              is of the wrong class, reload the whole SUBREG.  This avoids
5977              needless copies if SUBREG_REG is multi-word.  */
5978           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
5979             {
5980               int regno ATTRIBUTE_UNUSED = subreg_regno (x);
5981
5982               if (!REG_OK_FOR_CONTEXT (context, regno, mode, outer_code,
5983                                        index_code))
5984                 {
5985                   push_reload (x, NULL_RTX, loc, (rtx*) 0,
5986                                context_reg_class,
5987                                GET_MODE (x), VOIDmode, 0, 0, opnum, type);
5988                   return 1;
5989                 }
5990             }
5991           /* If this is a SUBREG of a pseudo-register, and the pseudo-register
5992              is larger than the class size, then reload the whole SUBREG.  */
5993           else
5994             {
5995               enum reg_class rclass = context_reg_class;
5996               if ((unsigned) CLASS_MAX_NREGS (rclass, GET_MODE (SUBREG_REG (x)))
5997                   > reg_class_size[rclass])
5998                 {
5999                   x = find_reloads_subreg_address (x, 0, opnum,
6000                                                    ADDR_TYPE (type),
6001                                                    ind_levels, insn, NULL);
6002                   push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6003                                GET_MODE (x), VOIDmode, 0, 0, opnum, type);
6004                   return 1;
6005                 }
6006             }
6007         }
6008       break;
6009
6010     default:
6011       break;
6012     }
6013
6014   {
6015     const char *fmt = GET_RTX_FORMAT (code);
6016     int i;
6017
6018     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6019       {
6020         if (fmt[i] == 'e')
6021           /* Pass SCRATCH for INDEX_CODE, since CODE can never be a PLUS once
6022              we get here.  */
6023           find_reloads_address_1 (mode, XEXP (x, i), context, code, SCRATCH,
6024                                   &XEXP (x, i), opnum, type, ind_levels, insn);
6025       }
6026   }
6027
6028 #undef REG_OK_FOR_CONTEXT
6029   return 0;
6030 }
6031 \f
6032 /* X, which is found at *LOC, is a part of an address that needs to be
6033    reloaded into a register of class RCLASS.  If X is a constant, or if
6034    X is a PLUS that contains a constant, check that the constant is a
6035    legitimate operand and that we are supposed to be able to load
6036    it into the register.
6037
6038    If not, force the constant into memory and reload the MEM instead.
6039
6040    MODE is the mode to use, in case X is an integer constant.
6041
6042    OPNUM and TYPE describe the purpose of any reloads made.
6043
6044    IND_LEVELS says how many levels of indirect addressing this machine
6045    supports.  */
6046
6047 static void
6048 find_reloads_address_part (rtx x, rtx *loc, enum reg_class rclass,
6049                            enum machine_mode mode, int opnum,
6050                            enum reload_type type, int ind_levels)
6051 {
6052   if (CONSTANT_P (x)
6053       && (!targetm.legitimate_constant_p (mode, x)
6054           || targetm.preferred_reload_class (x, rclass) == NO_REGS))
6055     {
6056       x = force_const_mem (mode, x);
6057       find_reloads_address (mode, &x, XEXP (x, 0), &XEXP (x, 0),
6058                             opnum, type, ind_levels, 0);
6059     }
6060
6061   else if (GET_CODE (x) == PLUS
6062            && CONSTANT_P (XEXP (x, 1))
6063            && (!targetm.legitimate_constant_p (GET_MODE (x), XEXP (x, 1))
6064                || targetm.preferred_reload_class (XEXP (x, 1), rclass)
6065                    == NO_REGS))
6066     {
6067       rtx tem;
6068
6069       tem = force_const_mem (GET_MODE (x), XEXP (x, 1));
6070       x = gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0), tem);
6071       find_reloads_address (mode, &XEXP (x, 1), XEXP (tem, 0), &XEXP (tem, 0),
6072                             opnum, type, ind_levels, 0);
6073     }
6074
6075   push_reload (x, NULL_RTX, loc, (rtx*) 0, rclass,
6076                mode, VOIDmode, 0, 0, opnum, type);
6077 }
6078 \f
6079 /* X, a subreg of a pseudo, is a part of an address that needs to be
6080    reloaded.
6081
6082    If the pseudo is equivalent to a memory location that cannot be directly
6083    addressed, make the necessary address reloads.
6084
6085    If address reloads have been necessary, or if the address is changed
6086    by register elimination, return the rtx of the memory location;
6087    otherwise, return X.
6088
6089    If FORCE_REPLACE is nonzero, unconditionally replace the subreg with the
6090    memory location.
6091
6092    OPNUM and TYPE identify the purpose of the reload.
6093
6094    IND_LEVELS says how many levels of indirect addressing are
6095    supported at this point in the address.
6096
6097    INSN, if nonzero, is the insn in which we do the reload.  It is used
6098    to determine where to put USEs for pseudos that we have to replace with
6099    stack slots.  */
6100
6101 static rtx
6102 find_reloads_subreg_address (rtx x, int force_replace, int opnum,
6103                              enum reload_type type, int ind_levels, rtx insn,
6104                              int *address_reloaded)
6105 {
6106   int regno = REGNO (SUBREG_REG (x));
6107   int reloaded = 0;
6108
6109   if (reg_equiv_memory_loc (regno))
6110     {
6111       /* If the address is not directly addressable, or if the address is not
6112          offsettable, then it must be replaced.  */
6113       if (! force_replace
6114           && (reg_equiv_address (regno)
6115               || ! offsettable_memref_p (reg_equiv_mem (regno))))
6116         force_replace = 1;
6117
6118       if (force_replace || num_not_at_initial_offset)
6119         {
6120           rtx tem = make_memloc (SUBREG_REG (x), regno);
6121
6122           /* If the address changes because of register elimination, then
6123              it must be replaced.  */
6124           if (force_replace
6125               || ! rtx_equal_p (tem, reg_equiv_mem (regno)))
6126             {
6127               unsigned outer_size = GET_MODE_SIZE (GET_MODE (x));
6128               unsigned inner_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
6129               int offset;
6130               rtx orig = tem;
6131
6132               /* For big-endian paradoxical subregs, SUBREG_BYTE does not
6133                  hold the correct (negative) byte offset.  */
6134               if (BYTES_BIG_ENDIAN && outer_size > inner_size)
6135                 offset = inner_size - outer_size;
6136               else
6137                 offset = SUBREG_BYTE (x);
6138
6139               XEXP (tem, 0) = plus_constant (XEXP (tem, 0), offset);
6140               PUT_MODE (tem, GET_MODE (x));
6141               if (MEM_OFFSET (tem))
6142                 set_mem_offset (tem, plus_constant (MEM_OFFSET (tem), offset));
6143               if (MEM_SIZE (tem)
6144                   && INTVAL (MEM_SIZE (tem)) != (HOST_WIDE_INT) outer_size)
6145                 set_mem_size (tem, GEN_INT (outer_size));
6146
6147               /* If this was a paradoxical subreg that we replaced, the
6148                  resulting memory must be sufficiently aligned to allow
6149                  us to widen the mode of the memory.  */
6150               if (outer_size > inner_size)
6151                 {
6152                   rtx base;
6153
6154                   base = XEXP (tem, 0);
6155                   if (GET_CODE (base) == PLUS)
6156                     {
6157                       if (CONST_INT_P (XEXP (base, 1))
6158                           && INTVAL (XEXP (base, 1)) % outer_size != 0)
6159                         return x;
6160                       base = XEXP (base, 0);
6161                     }
6162                   if (!REG_P (base)
6163                       || (REGNO_POINTER_ALIGN (REGNO (base))
6164                           < outer_size * BITS_PER_UNIT))
6165                     return x;
6166                 }
6167
6168               reloaded = find_reloads_address (GET_MODE (tem), &tem,
6169                                                XEXP (tem, 0), &XEXP (tem, 0),
6170                                                opnum, type, ind_levels, insn);
6171               /* ??? Do we need to handle nonzero offsets somehow?  */
6172               if (!offset && !rtx_equal_p (tem, orig))
6173                 push_reg_equiv_alt_mem (regno, tem);
6174
6175               /* For some processors an address may be valid in the
6176                  original mode but not in a smaller mode.  For
6177                  example, ARM accepts a scaled index register in
6178                  SImode but not in HImode.  Note that this is only
6179                  a problem if the address in reg_equiv_mem is already
6180                  invalid in the new mode; other cases would be fixed
6181                  by find_reloads_address as usual.
6182
6183                  ??? We attempt to handle such cases here by doing an
6184                  additional reload of the full address after the
6185                  usual processing by find_reloads_address.  Note that
6186                  this may not work in the general case, but it seems
6187                  to cover the cases where this situation currently
6188                  occurs.  A more general fix might be to reload the
6189                  *value* instead of the address, but this would not
6190                  be expected by the callers of this routine as-is.
6191
6192                  If find_reloads_address already completed replaced
6193                  the address, there is nothing further to do.  */
6194               if (reloaded == 0
6195                   && reg_equiv_mem (regno) != 0
6196                   && !strict_memory_address_addr_space_p
6197                         (GET_MODE (x), XEXP (reg_equiv_mem (regno), 0),
6198                          MEM_ADDR_SPACE (reg_equiv_mem (regno))))
6199                 {
6200                   push_reload (XEXP (tem, 0), NULL_RTX, &XEXP (tem, 0), (rtx*) 0,
6201                                base_reg_class (GET_MODE (tem), MEM, SCRATCH),
6202                                GET_MODE (XEXP (tem, 0)), VOIDmode, 0, 0,
6203                                opnum, type);
6204                   reloaded = 1;
6205                 }
6206               /* If this is not a toplevel operand, find_reloads doesn't see
6207                  this substitution.  We have to emit a USE of the pseudo so
6208                  that delete_output_reload can see it.  */
6209               if (replace_reloads && recog_data.operand[opnum] != x)
6210                 /* We mark the USE with QImode so that we recognize it
6211                    as one that can be safely deleted at the end of
6212                    reload.  */
6213                 PUT_MODE (emit_insn_before (gen_rtx_USE (VOIDmode,
6214                                                          SUBREG_REG (x)),
6215                                             insn), QImode);
6216               x = tem;
6217             }
6218         }
6219     }
6220   if (reloaded && address_reloaded)
6221     *address_reloaded = 1;
6222
6223   return x;
6224 }
6225 \f
6226 /* Substitute into the current INSN the registers into which we have reloaded
6227    the things that need reloading.  The array `replacements'
6228    contains the locations of all pointers that must be changed
6229    and says what to replace them with.
6230
6231    Return the rtx that X translates into; usually X, but modified.  */
6232
6233 void
6234 subst_reloads (rtx insn)
6235 {
6236   int i;
6237
6238   for (i = 0; i < n_replacements; i++)
6239     {
6240       struct replacement *r = &replacements[i];
6241       rtx reloadreg = rld[r->what].reg_rtx;
6242       if (reloadreg)
6243         {
6244 #ifdef DEBUG_RELOAD
6245           /* This checking takes a very long time on some platforms
6246              causing the gcc.c-torture/compile/limits-fnargs.c test
6247              to time out during testing.  See PR 31850.
6248
6249              Internal consistency test.  Check that we don't modify
6250              anything in the equivalence arrays.  Whenever something from
6251              those arrays needs to be reloaded, it must be unshared before
6252              being substituted into; the equivalence must not be modified.
6253              Otherwise, if the equivalence is used after that, it will
6254              have been modified, and the thing substituted (probably a
6255              register) is likely overwritten and not a usable equivalence.  */
6256           int check_regno;
6257
6258           for (check_regno = 0; check_regno < max_regno; check_regno++)
6259             {
6260 #define CHECK_MODF(ARRAY)                                               \
6261               gcc_assert (!VEC_index (reg_equivs_t, reg_equivs, check_regno).ARRAY              \
6262                           || !loc_mentioned_in_p (r->where,             \
6263                                                   VEC_index (reg_equivs_t, reg_equivs, check_regno).ARRAY))
6264
6265               CHECK_MODF (equiv_constant);
6266               CHECK_MODF (equiv_memory_loc);
6267               CHECK_MODF (equiv_address);
6268               CHECK_MODF (equiv_mem);
6269 #undef CHECK_MODF
6270             }
6271 #endif /* DEBUG_RELOAD */
6272
6273           /* If we're replacing a LABEL_REF with a register, there must
6274              already be an indication (to e.g. flow) which label this
6275              register refers to.  */
6276           gcc_assert (GET_CODE (*r->where) != LABEL_REF
6277                       || !JUMP_P (insn)
6278                       || find_reg_note (insn,
6279                                         REG_LABEL_OPERAND,
6280                                         XEXP (*r->where, 0))
6281                       || label_is_jump_target_p (XEXP (*r->where, 0), insn));
6282
6283           /* Encapsulate RELOADREG so its machine mode matches what
6284              used to be there.  Note that gen_lowpart_common will
6285              do the wrong thing if RELOADREG is multi-word.  RELOADREG
6286              will always be a REG here.  */
6287           if (GET_MODE (reloadreg) != r->mode && r->mode != VOIDmode)
6288             reloadreg = reload_adjust_reg_for_mode (reloadreg, r->mode);
6289
6290           /* If we are putting this into a SUBREG and RELOADREG is a
6291              SUBREG, we would be making nested SUBREGs, so we have to fix
6292              this up.  Note that r->where == &SUBREG_REG (*r->subreg_loc).  */
6293
6294           if (r->subreg_loc != 0 && GET_CODE (reloadreg) == SUBREG)
6295             {
6296               if (GET_MODE (*r->subreg_loc)
6297                   == GET_MODE (SUBREG_REG (reloadreg)))
6298                 *r->subreg_loc = SUBREG_REG (reloadreg);
6299               else
6300                 {
6301                   int final_offset =
6302                     SUBREG_BYTE (*r->subreg_loc) + SUBREG_BYTE (reloadreg);
6303
6304                   /* When working with SUBREGs the rule is that the byte
6305                      offset must be a multiple of the SUBREG's mode.  */
6306                   final_offset = (final_offset /
6307                                   GET_MODE_SIZE (GET_MODE (*r->subreg_loc)));
6308                   final_offset = (final_offset *
6309                                   GET_MODE_SIZE (GET_MODE (*r->subreg_loc)));
6310
6311                   *r->where = SUBREG_REG (reloadreg);
6312                   SUBREG_BYTE (*r->subreg_loc) = final_offset;
6313                 }
6314             }
6315           else
6316             *r->where = reloadreg;
6317         }
6318       /* If reload got no reg and isn't optional, something's wrong.  */
6319       else
6320         gcc_assert (rld[r->what].optional);
6321     }
6322 }
6323 \f
6324 /* Make a copy of any replacements being done into X and move those
6325    copies to locations in Y, a copy of X.  */
6326
6327 void
6328 copy_replacements (rtx x, rtx y)
6329 {
6330   /* We can't support X being a SUBREG because we might then need to know its
6331      location if something inside it was replaced.  */
6332   gcc_assert (GET_CODE (x) != SUBREG);
6333
6334   copy_replacements_1 (&x, &y, n_replacements);
6335 }
6336
6337 static void
6338 copy_replacements_1 (rtx *px, rtx *py, int orig_replacements)
6339 {
6340   int i, j;
6341   rtx x, y;
6342   struct replacement *r;
6343   enum rtx_code code;
6344   const char *fmt;
6345
6346   for (j = 0; j < orig_replacements; j++)
6347     {
6348       if (replacements[j].subreg_loc == px)
6349         {
6350           r = &replacements[n_replacements++];
6351           r->where = replacements[j].where;
6352           r->subreg_loc = py;
6353           r->what = replacements[j].what;
6354           r->mode = replacements[j].mode;
6355         }
6356       else if (replacements[j].where == px)
6357         {
6358           r = &replacements[n_replacements++];
6359           r->where = py;
6360           r->subreg_loc = 0;
6361           r->what = replacements[j].what;
6362           r->mode = replacements[j].mode;
6363         }
6364     }
6365
6366   x = *px;
6367   y = *py;
6368   code = GET_CODE (x);
6369   fmt = GET_RTX_FORMAT (code);
6370
6371   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6372     {
6373       if (fmt[i] == 'e')
6374         copy_replacements_1 (&XEXP (x, i), &XEXP (y, i), orig_replacements);
6375       else if (fmt[i] == 'E')
6376         for (j = XVECLEN (x, i); --j >= 0; )
6377           copy_replacements_1 (&XVECEXP (x, i, j), &XVECEXP (y, i, j),
6378                                orig_replacements);
6379     }
6380 }
6381
6382 /* Change any replacements being done to *X to be done to *Y.  */
6383
6384 void
6385 move_replacements (rtx *x, rtx *y)
6386 {
6387   int i;
6388
6389   for (i = 0; i < n_replacements; i++)
6390     if (replacements[i].subreg_loc == x)
6391       replacements[i].subreg_loc = y;
6392     else if (replacements[i].where == x)
6393       {
6394         replacements[i].where = y;
6395         replacements[i].subreg_loc = 0;
6396       }
6397 }
6398 \f
6399 /* If LOC was scheduled to be replaced by something, return the replacement.
6400    Otherwise, return *LOC.  */
6401
6402 rtx
6403 find_replacement (rtx *loc)
6404 {
6405   struct replacement *r;
6406
6407   for (r = &replacements[0]; r < &replacements[n_replacements]; r++)
6408     {
6409       rtx reloadreg = rld[r->what].reg_rtx;
6410
6411       if (reloadreg && r->where == loc)
6412         {
6413           if (r->mode != VOIDmode && GET_MODE (reloadreg) != r->mode)
6414             reloadreg = gen_rtx_REG (r->mode, REGNO (reloadreg));
6415
6416           return reloadreg;
6417         }
6418       else if (reloadreg && r->subreg_loc == loc)
6419         {
6420           /* RELOADREG must be either a REG or a SUBREG.
6421
6422              ??? Is it actually still ever a SUBREG?  If so, why?  */
6423
6424           if (REG_P (reloadreg))
6425             return gen_rtx_REG (GET_MODE (*loc),
6426                                 (REGNO (reloadreg) +
6427                                  subreg_regno_offset (REGNO (SUBREG_REG (*loc)),
6428                                                       GET_MODE (SUBREG_REG (*loc)),
6429                                                       SUBREG_BYTE (*loc),
6430                                                       GET_MODE (*loc))));
6431           else if (GET_MODE (reloadreg) == GET_MODE (*loc))
6432             return reloadreg;
6433           else
6434             {
6435               int final_offset = SUBREG_BYTE (reloadreg) + SUBREG_BYTE (*loc);
6436
6437               /* When working with SUBREGs the rule is that the byte
6438                  offset must be a multiple of the SUBREG's mode.  */
6439               final_offset = (final_offset / GET_MODE_SIZE (GET_MODE (*loc)));
6440               final_offset = (final_offset * GET_MODE_SIZE (GET_MODE (*loc)));
6441               return gen_rtx_SUBREG (GET_MODE (*loc), SUBREG_REG (reloadreg),
6442                                      final_offset);
6443             }
6444         }
6445     }
6446
6447   /* If *LOC is a PLUS, MINUS, or MULT, see if a replacement is scheduled for
6448      what's inside and make a new rtl if so.  */
6449   if (GET_CODE (*loc) == PLUS || GET_CODE (*loc) == MINUS
6450       || GET_CODE (*loc) == MULT)
6451     {
6452       rtx x = find_replacement (&XEXP (*loc, 0));
6453       rtx y = find_replacement (&XEXP (*loc, 1));
6454
6455       if (x != XEXP (*loc, 0) || y != XEXP (*loc, 1))
6456         return gen_rtx_fmt_ee (GET_CODE (*loc), GET_MODE (*loc), x, y);
6457     }
6458
6459   return *loc;
6460 }
6461 \f
6462 /* Return nonzero if register in range [REGNO, ENDREGNO)
6463    appears either explicitly or implicitly in X
6464    other than being stored into (except for earlyclobber operands).
6465
6466    References contained within the substructure at LOC do not count.
6467    LOC may be zero, meaning don't ignore anything.
6468
6469    This is similar to refers_to_regno_p in rtlanal.c except that we
6470    look at equivalences for pseudos that didn't get hard registers.  */
6471
6472 static int
6473 refers_to_regno_for_reload_p (unsigned int regno, unsigned int endregno,
6474                               rtx x, rtx *loc)
6475 {
6476   int i;
6477   unsigned int r;
6478   RTX_CODE code;
6479   const char *fmt;
6480
6481   if (x == 0)
6482     return 0;
6483
6484  repeat:
6485   code = GET_CODE (x);
6486
6487   switch (code)
6488     {
6489     case REG:
6490       r = REGNO (x);
6491
6492       /* If this is a pseudo, a hard register must not have been allocated.
6493          X must therefore either be a constant or be in memory.  */
6494       if (r >= FIRST_PSEUDO_REGISTER)
6495         {
6496           if (reg_equiv_memory_loc (r))
6497             return refers_to_regno_for_reload_p (regno, endregno,
6498                                                  reg_equiv_memory_loc (r),
6499                                                  (rtx*) 0);
6500
6501           gcc_assert (reg_equiv_constant (r) || reg_equiv_invariant (r));
6502           return 0;
6503         }
6504
6505       return (endregno > r
6506               && regno < r + (r < FIRST_PSEUDO_REGISTER
6507                               ? hard_regno_nregs[r][GET_MODE (x)]
6508                               : 1));
6509
6510     case SUBREG:
6511       /* If this is a SUBREG of a hard reg, we can see exactly which
6512          registers are being modified.  Otherwise, handle normally.  */
6513       if (REG_P (SUBREG_REG (x))
6514           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
6515         {
6516           unsigned int inner_regno = subreg_regno (x);
6517           unsigned int inner_endregno
6518             = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
6519                              ? subreg_nregs (x) : 1);
6520
6521           return endregno > inner_regno && regno < inner_endregno;
6522         }
6523       break;
6524
6525     case CLOBBER:
6526     case SET:
6527       if (&SET_DEST (x) != loc
6528           /* Note setting a SUBREG counts as referring to the REG it is in for
6529              a pseudo but not for hard registers since we can
6530              treat each word individually.  */
6531           && ((GET_CODE (SET_DEST (x)) == SUBREG
6532                && loc != &SUBREG_REG (SET_DEST (x))
6533                && REG_P (SUBREG_REG (SET_DEST (x)))
6534                && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
6535                && refers_to_regno_for_reload_p (regno, endregno,
6536                                                 SUBREG_REG (SET_DEST (x)),
6537                                                 loc))
6538               /* If the output is an earlyclobber operand, this is
6539                  a conflict.  */
6540               || ((!REG_P (SET_DEST (x))
6541                    || earlyclobber_operand_p (SET_DEST (x)))
6542                   && refers_to_regno_for_reload_p (regno, endregno,
6543                                                    SET_DEST (x), loc))))
6544         return 1;
6545
6546       if (code == CLOBBER || loc == &SET_SRC (x))
6547         return 0;
6548       x = SET_SRC (x);
6549       goto repeat;
6550
6551     default:
6552       break;
6553     }
6554
6555   /* X does not match, so try its subexpressions.  */
6556
6557   fmt = GET_RTX_FORMAT (code);
6558   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6559     {
6560       if (fmt[i] == 'e' && loc != &XEXP (x, i))
6561         {
6562           if (i == 0)
6563             {
6564               x = XEXP (x, 0);
6565               goto repeat;
6566             }
6567           else
6568             if (refers_to_regno_for_reload_p (regno, endregno,
6569                                               XEXP (x, i), loc))
6570               return 1;
6571         }
6572       else if (fmt[i] == 'E')
6573         {
6574           int j;
6575           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6576             if (loc != &XVECEXP (x, i, j)
6577                 && refers_to_regno_for_reload_p (regno, endregno,
6578                                                  XVECEXP (x, i, j), loc))
6579               return 1;
6580         }
6581     }
6582   return 0;
6583 }
6584
6585 /* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
6586    we check if any register number in X conflicts with the relevant register
6587    numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
6588    contains a MEM (we don't bother checking for memory addresses that can't
6589    conflict because we expect this to be a rare case.
6590
6591    This function is similar to reg_overlap_mentioned_p in rtlanal.c except
6592    that we look at equivalences for pseudos that didn't get hard registers.  */
6593
6594 int
6595 reg_overlap_mentioned_for_reload_p (rtx x, rtx in)
6596 {
6597   int regno, endregno;
6598
6599   /* Overly conservative.  */
6600   if (GET_CODE (x) == STRICT_LOW_PART
6601       || GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
6602     x = XEXP (x, 0);
6603
6604   /* If either argument is a constant, then modifying X can not affect IN.  */
6605   if (CONSTANT_P (x) || CONSTANT_P (in))
6606     return 0;
6607   else if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
6608     return refers_to_mem_for_reload_p (in);
6609   else if (GET_CODE (x) == SUBREG)
6610     {
6611       regno = REGNO (SUBREG_REG (x));
6612       if (regno < FIRST_PSEUDO_REGISTER)
6613         regno += subreg_regno_offset (REGNO (SUBREG_REG (x)),
6614                                       GET_MODE (SUBREG_REG (x)),
6615                                       SUBREG_BYTE (x),
6616                                       GET_MODE (x));
6617       endregno = regno + (regno < FIRST_PSEUDO_REGISTER
6618                           ? subreg_nregs (x) : 1);
6619
6620       return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6621     }
6622   else if (REG_P (x))
6623     {
6624       regno = REGNO (x);
6625
6626       /* If this is a pseudo, it must not have been assigned a hard register.
6627          Therefore, it must either be in memory or be a constant.  */
6628
6629       if (regno >= FIRST_PSEUDO_REGISTER)
6630         {
6631           if (reg_equiv_memory_loc (regno))
6632             return refers_to_mem_for_reload_p (in);
6633           gcc_assert (reg_equiv_constant (regno));
6634           return 0;
6635         }
6636
6637       endregno = END_HARD_REGNO (x);
6638
6639       return refers_to_regno_for_reload_p (regno, endregno, in, (rtx*) 0);
6640     }
6641   else if (MEM_P (x))
6642     return refers_to_mem_for_reload_p (in);
6643   else if (GET_CODE (x) == SCRATCH || GET_CODE (x) == PC
6644            || GET_CODE (x) == CC0)
6645     return reg_mentioned_p (x, in);
6646   else
6647     {
6648       gcc_assert (GET_CODE (x) == PLUS);
6649
6650       /* We actually want to know if X is mentioned somewhere inside IN.
6651          We must not say that (plus (sp) (const_int 124)) is in
6652          (plus (sp) (const_int 64)), since that can lead to incorrect reload
6653          allocation when spuriously changing a RELOAD_FOR_OUTPUT_ADDRESS
6654          into a RELOAD_OTHER on behalf of another RELOAD_OTHER.  */
6655       while (MEM_P (in))
6656         in = XEXP (in, 0);
6657       if (REG_P (in))
6658         return 0;
6659       else if (GET_CODE (in) == PLUS)
6660         return (rtx_equal_p (x, in)
6661                 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 0))
6662                 || reg_overlap_mentioned_for_reload_p (x, XEXP (in, 1)));
6663       else return (reg_overlap_mentioned_for_reload_p (XEXP (x, 0), in)
6664                    || reg_overlap_mentioned_for_reload_p (XEXP (x, 1), in));
6665     }
6666
6667   gcc_unreachable ();
6668 }
6669
6670 /* Return nonzero if anything in X contains a MEM.  Look also for pseudo
6671    registers.  */
6672
6673 static int
6674 refers_to_mem_for_reload_p (rtx x)
6675 {
6676   const char *fmt;
6677   int i;
6678
6679   if (MEM_P (x))
6680     return 1;
6681
6682   if (REG_P (x))
6683     return (REGNO (x) >= FIRST_PSEUDO_REGISTER
6684             && reg_equiv_memory_loc (REGNO (x)));
6685
6686   fmt = GET_RTX_FORMAT (GET_CODE (x));
6687   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6688     if (fmt[i] == 'e'
6689         && (MEM_P (XEXP (x, i))
6690             || refers_to_mem_for_reload_p (XEXP (x, i))))
6691       return 1;
6692
6693   return 0;
6694 }
6695 \f
6696 /* Check the insns before INSN to see if there is a suitable register
6697    containing the same value as GOAL.
6698    If OTHER is -1, look for a register in class RCLASS.
6699    Otherwise, just see if register number OTHER shares GOAL's value.
6700
6701    Return an rtx for the register found, or zero if none is found.
6702
6703    If RELOAD_REG_P is (short *)1,
6704    we reject any hard reg that appears in reload_reg_rtx
6705    because such a hard reg is also needed coming into this insn.
6706
6707    If RELOAD_REG_P is any other nonzero value,
6708    it is a vector indexed by hard reg number
6709    and we reject any hard reg whose element in the vector is nonnegative
6710    as well as any that appears in reload_reg_rtx.
6711
6712    If GOAL is zero, then GOALREG is a register number; we look
6713    for an equivalent for that register.
6714
6715    MODE is the machine mode of the value we want an equivalence for.
6716    If GOAL is nonzero and not VOIDmode, then it must have mode MODE.
6717
6718    This function is used by jump.c as well as in the reload pass.
6719
6720    If GOAL is the sum of the stack pointer and a constant, we treat it
6721    as if it were a constant except that sp is required to be unchanging.  */
6722
6723 rtx
6724 find_equiv_reg (rtx goal, rtx insn, enum reg_class rclass, int other,
6725                 short *reload_reg_p, int goalreg, enum machine_mode mode)
6726 {
6727   rtx p = insn;
6728   rtx goaltry, valtry, value, where;
6729   rtx pat;
6730   int regno = -1;
6731   int valueno;
6732   int goal_mem = 0;
6733   int goal_const = 0;
6734   int goal_mem_addr_varies = 0;
6735   int need_stable_sp = 0;
6736   int nregs;
6737   int valuenregs;
6738   int num = 0;
6739
6740   if (goal == 0)
6741     regno = goalreg;
6742   else if (REG_P (goal))
6743     regno = REGNO (goal);
6744   else if (MEM_P (goal))
6745     {
6746       enum rtx_code code = GET_CODE (XEXP (goal, 0));
6747       if (MEM_VOLATILE_P (goal))
6748         return 0;
6749       if (flag_float_store && SCALAR_FLOAT_MODE_P (GET_MODE (goal)))
6750         return 0;
6751       /* An address with side effects must be reexecuted.  */
6752       switch (code)
6753         {
6754         case POST_INC:
6755         case PRE_INC:
6756         case POST_DEC:
6757         case PRE_DEC:
6758         case POST_MODIFY:
6759         case PRE_MODIFY:
6760           return 0;
6761         default:
6762           break;
6763         }
6764       goal_mem = 1;
6765     }
6766   else if (CONSTANT_P (goal))
6767     goal_const = 1;
6768   else if (GET_CODE (goal) == PLUS
6769            && XEXP (goal, 0) == stack_pointer_rtx
6770            && CONSTANT_P (XEXP (goal, 1)))
6771     goal_const = need_stable_sp = 1;
6772   else if (GET_CODE (goal) == PLUS
6773            && XEXP (goal, 0) == frame_pointer_rtx
6774            && CONSTANT_P (XEXP (goal, 1)))
6775     goal_const = 1;
6776   else
6777     return 0;
6778
6779   num = 0;
6780   /* Scan insns back from INSN, looking for one that copies
6781      a value into or out of GOAL.
6782      Stop and give up if we reach a label.  */
6783
6784   while (1)
6785     {
6786       p = PREV_INSN (p);
6787       if (p && DEBUG_INSN_P (p))
6788         continue;
6789       num++;
6790       if (p == 0 || LABEL_P (p)
6791           || num > PARAM_VALUE (PARAM_MAX_RELOAD_SEARCH_INSNS))
6792         return 0;
6793
6794       if (NONJUMP_INSN_P (p)
6795           /* If we don't want spill regs ...  */
6796           && (! (reload_reg_p != 0
6797                  && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6798               /* ... then ignore insns introduced by reload; they aren't
6799                  useful and can cause results in reload_as_needed to be
6800                  different from what they were when calculating the need for
6801                  spills.  If we notice an input-reload insn here, we will
6802                  reject it below, but it might hide a usable equivalent.
6803                  That makes bad code.  It may even fail: perhaps no reg was
6804                  spilled for this insn because it was assumed we would find
6805                  that equivalent.  */
6806               || INSN_UID (p) < reload_first_uid))
6807         {
6808           rtx tem;
6809           pat = single_set (p);
6810
6811           /* First check for something that sets some reg equal to GOAL.  */
6812           if (pat != 0
6813               && ((regno >= 0
6814                    && true_regnum (SET_SRC (pat)) == regno
6815                    && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6816                   ||
6817                   (regno >= 0
6818                    && true_regnum (SET_DEST (pat)) == regno
6819                    && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0)
6820                   ||
6821                   (goal_const && rtx_equal_p (SET_SRC (pat), goal)
6822                    /* When looking for stack pointer + const,
6823                       make sure we don't use a stack adjust.  */
6824                    && !reg_overlap_mentioned_for_reload_p (SET_DEST (pat), goal)
6825                    && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0)
6826                   || (goal_mem
6827                       && (valueno = true_regnum (valtry = SET_DEST (pat))) >= 0
6828                       && rtx_renumbered_equal_p (goal, SET_SRC (pat)))
6829                   || (goal_mem
6830                       && (valueno = true_regnum (valtry = SET_SRC (pat))) >= 0
6831                       && rtx_renumbered_equal_p (goal, SET_DEST (pat)))
6832                   /* If we are looking for a constant,
6833                      and something equivalent to that constant was copied
6834                      into a reg, we can use that reg.  */
6835                   || (goal_const && REG_NOTES (p) != 0
6836                       && (tem = find_reg_note (p, REG_EQUIV, NULL_RTX))
6837                       && ((rtx_equal_p (XEXP (tem, 0), goal)
6838                            && (valueno
6839                                = true_regnum (valtry = SET_DEST (pat))) >= 0)
6840                           || (REG_P (SET_DEST (pat))
6841                               && GET_CODE (XEXP (tem, 0)) == CONST_DOUBLE
6842                               && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6843                               && CONST_INT_P (goal)
6844                               && 0 != (goaltry
6845                                        = operand_subword (XEXP (tem, 0), 0, 0,
6846                                                           VOIDmode))
6847                               && rtx_equal_p (goal, goaltry)
6848                               && (valtry
6849                                   = operand_subword (SET_DEST (pat), 0, 0,
6850                                                      VOIDmode))
6851                               && (valueno = true_regnum (valtry)) >= 0)))
6852                   || (goal_const && (tem = find_reg_note (p, REG_EQUIV,
6853                                                           NULL_RTX))
6854                       && REG_P (SET_DEST (pat))
6855                       && GET_CODE (XEXP (tem, 0)) == CONST_DOUBLE
6856                       && SCALAR_FLOAT_MODE_P (GET_MODE (XEXP (tem, 0)))
6857                       && CONST_INT_P (goal)
6858                       && 0 != (goaltry = operand_subword (XEXP (tem, 0), 1, 0,
6859                                                           VOIDmode))
6860                       && rtx_equal_p (goal, goaltry)
6861                       && (valtry
6862                           = operand_subword (SET_DEST (pat), 1, 0, VOIDmode))
6863                       && (valueno = true_regnum (valtry)) >= 0)))
6864             {
6865               if (other >= 0)
6866                 {
6867                   if (valueno != other)
6868                     continue;
6869                 }
6870               else if ((unsigned) valueno >= FIRST_PSEUDO_REGISTER)
6871                 continue;
6872               else if (!in_hard_reg_set_p (reg_class_contents[(int) rclass],
6873                                           mode, valueno))
6874                 continue;
6875               value = valtry;
6876               where = p;
6877               break;
6878             }
6879         }
6880     }
6881
6882   /* We found a previous insn copying GOAL into a suitable other reg VALUE
6883      (or copying VALUE into GOAL, if GOAL is also a register).
6884      Now verify that VALUE is really valid.  */
6885
6886   /* VALUENO is the register number of VALUE; a hard register.  */
6887
6888   /* Don't try to re-use something that is killed in this insn.  We want
6889      to be able to trust REG_UNUSED notes.  */
6890   if (REG_NOTES (where) != 0 && find_reg_note (where, REG_UNUSED, value))
6891     return 0;
6892
6893   /* If we propose to get the value from the stack pointer or if GOAL is
6894      a MEM based on the stack pointer, we need a stable SP.  */
6895   if (valueno == STACK_POINTER_REGNUM || regno == STACK_POINTER_REGNUM
6896       || (goal_mem && reg_overlap_mentioned_for_reload_p (stack_pointer_rtx,
6897                                                           goal)))
6898     need_stable_sp = 1;
6899
6900   /* Reject VALUE if the copy-insn moved the wrong sort of datum.  */
6901   if (GET_MODE (value) != mode)
6902     return 0;
6903
6904   /* Reject VALUE if it was loaded from GOAL
6905      and is also a register that appears in the address of GOAL.  */
6906
6907   if (goal_mem && value == SET_DEST (single_set (where))
6908       && refers_to_regno_for_reload_p (valueno, end_hard_regno (mode, valueno),
6909                                        goal, (rtx*) 0))
6910     return 0;
6911
6912   /* Reject registers that overlap GOAL.  */
6913
6914   if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6915     nregs = hard_regno_nregs[regno][mode];
6916   else
6917     nregs = 1;
6918   valuenregs = hard_regno_nregs[valueno][mode];
6919
6920   if (!goal_mem && !goal_const
6921       && regno + nregs > valueno && regno < valueno + valuenregs)
6922     return 0;
6923
6924   /* Reject VALUE if it is one of the regs reserved for reloads.
6925      Reload1 knows how to reuse them anyway, and it would get
6926      confused if we allocated one without its knowledge.
6927      (Now that insns introduced by reload are ignored above,
6928      this case shouldn't happen, but I'm not positive.)  */
6929
6930   if (reload_reg_p != 0 && reload_reg_p != (short *) (HOST_WIDE_INT) 1)
6931     {
6932       int i;
6933       for (i = 0; i < valuenregs; ++i)
6934         if (reload_reg_p[valueno + i] >= 0)
6935           return 0;
6936     }
6937
6938   /* Reject VALUE if it is a register being used for an input reload
6939      even if it is not one of those reserved.  */
6940
6941   if (reload_reg_p != 0)
6942     {
6943       int i;
6944       for (i = 0; i < n_reloads; i++)
6945         if (rld[i].reg_rtx != 0 && rld[i].in)
6946           {
6947             int regno1 = REGNO (rld[i].reg_rtx);
6948             int nregs1 = hard_regno_nregs[regno1]
6949                                          [GET_MODE (rld[i].reg_rtx)];
6950             if (regno1 < valueno + valuenregs
6951                 && regno1 + nregs1 > valueno)
6952               return 0;
6953           }
6954     }
6955
6956   if (goal_mem)
6957     /* We must treat frame pointer as varying here,
6958        since it can vary--in a nonlocal goto as generated by expand_goto.  */
6959     goal_mem_addr_varies = !CONSTANT_ADDRESS_P (XEXP (goal, 0));
6960
6961   /* Now verify that the values of GOAL and VALUE remain unaltered
6962      until INSN is reached.  */
6963
6964   p = insn;
6965   while (1)
6966     {
6967       p = PREV_INSN (p);
6968       if (p == where)
6969         return value;
6970
6971       /* Don't trust the conversion past a function call
6972          if either of the two is in a call-clobbered register, or memory.  */
6973       if (CALL_P (p))
6974         {
6975           int i;
6976
6977           if (goal_mem || need_stable_sp)
6978             return 0;
6979
6980           if (regno >= 0 && regno < FIRST_PSEUDO_REGISTER)
6981             for (i = 0; i < nregs; ++i)
6982               if (call_used_regs[regno + i]
6983                   || HARD_REGNO_CALL_PART_CLOBBERED (regno + i, mode))
6984                 return 0;
6985
6986           if (valueno >= 0 && valueno < FIRST_PSEUDO_REGISTER)
6987             for (i = 0; i < valuenregs; ++i)
6988               if (call_used_regs[valueno + i]
6989                   || HARD_REGNO_CALL_PART_CLOBBERED (valueno + i, mode))
6990                 return 0;
6991         }
6992
6993       if (INSN_P (p))
6994         {
6995           pat = PATTERN (p);
6996
6997           /* Watch out for unspec_volatile, and volatile asms.  */
6998           if (volatile_insn_p (pat))
6999             return 0;
7000
7001           /* If this insn P stores in either GOAL or VALUE, return 0.
7002              If GOAL is a memory ref and this insn writes memory, return 0.
7003              If GOAL is a memory ref and its address is not constant,
7004              and this insn P changes a register used in GOAL, return 0.  */
7005
7006           if (GET_CODE (pat) == COND_EXEC)
7007             pat = COND_EXEC_CODE (pat);
7008           if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
7009             {
7010               rtx dest = SET_DEST (pat);
7011               while (GET_CODE (dest) == SUBREG
7012                      || GET_CODE (dest) == ZERO_EXTRACT
7013                      || GET_CODE (dest) == STRICT_LOW_PART)
7014                 dest = XEXP (dest, 0);
7015               if (REG_P (dest))
7016                 {
7017                   int xregno = REGNO (dest);
7018                   int xnregs;
7019                   if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
7020                     xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
7021                   else
7022                     xnregs = 1;
7023                   if (xregno < regno + nregs && xregno + xnregs > regno)
7024                     return 0;
7025                   if (xregno < valueno + valuenregs
7026                       && xregno + xnregs > valueno)
7027                     return 0;
7028                   if (goal_mem_addr_varies
7029                       && reg_overlap_mentioned_for_reload_p (dest, goal))
7030                     return 0;
7031                   if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
7032                     return 0;
7033                 }
7034               else if (goal_mem && MEM_P (dest)
7035                        && ! push_operand (dest, GET_MODE (dest)))
7036                 return 0;
7037               else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
7038                        && reg_equiv_memory_loc (regno) != 0)
7039                 return 0;
7040               else if (need_stable_sp && push_operand (dest, GET_MODE (dest)))
7041                 return 0;
7042             }
7043           else if (GET_CODE (pat) == PARALLEL)
7044             {
7045               int i;
7046               for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
7047                 {
7048                   rtx v1 = XVECEXP (pat, 0, i);
7049                   if (GET_CODE (v1) == COND_EXEC)
7050                     v1 = COND_EXEC_CODE (v1);
7051                   if (GET_CODE (v1) == SET || GET_CODE (v1) == CLOBBER)
7052                     {
7053                       rtx dest = SET_DEST (v1);
7054                       while (GET_CODE (dest) == SUBREG
7055                              || GET_CODE (dest) == ZERO_EXTRACT
7056                              || GET_CODE (dest) == STRICT_LOW_PART)
7057                         dest = XEXP (dest, 0);
7058                       if (REG_P (dest))
7059                         {
7060                           int xregno = REGNO (dest);
7061                           int xnregs;
7062                           if (REGNO (dest) < FIRST_PSEUDO_REGISTER)
7063                             xnregs = hard_regno_nregs[xregno][GET_MODE (dest)];
7064                           else
7065                             xnregs = 1;
7066                           if (xregno < regno + nregs
7067                               && xregno + xnregs > regno)
7068                             return 0;
7069                           if (xregno < valueno + valuenregs
7070                               && xregno + xnregs > valueno)
7071                             return 0;
7072                           if (goal_mem_addr_varies
7073                               && reg_overlap_mentioned_for_reload_p (dest,
7074                                                                      goal))
7075                             return 0;
7076                           if (xregno == STACK_POINTER_REGNUM && need_stable_sp)
7077                             return 0;
7078                         }
7079                       else if (goal_mem && MEM_P (dest)
7080                                && ! push_operand (dest, GET_MODE (dest)))
7081                         return 0;
7082                       else if (MEM_P (dest) && regno >= FIRST_PSEUDO_REGISTER
7083                                && reg_equiv_memory_loc (regno) != 0)
7084                         return 0;
7085                       else if (need_stable_sp
7086                                && push_operand (dest, GET_MODE (dest)))
7087                         return 0;
7088                     }
7089                 }
7090             }
7091
7092           if (CALL_P (p) && CALL_INSN_FUNCTION_USAGE (p))
7093             {
7094               rtx link;
7095
7096               for (link = CALL_INSN_FUNCTION_USAGE (p); XEXP (link, 1) != 0;
7097                    link = XEXP (link, 1))
7098                 {
7099                   pat = XEXP (link, 0);
7100                   if (GET_CODE (pat) == CLOBBER)
7101                     {
7102                       rtx dest = SET_DEST (pat);
7103
7104                       if (REG_P (dest))
7105                         {
7106                           int xregno = REGNO (dest);
7107                           int xnregs
7108                             = hard_regno_nregs[xregno][GET_MODE (dest)];
7109
7110                           if (xregno < regno + nregs
7111                               && xregno + xnregs > regno)
7112                             return 0;
7113                           else if (xregno < valueno + valuenregs
7114                                    && xregno + xnregs > valueno)
7115                             return 0;
7116                           else if (goal_mem_addr_varies
7117                                    && reg_overlap_mentioned_for_reload_p (dest,
7118                                                                      goal))
7119                             return 0;
7120                         }
7121
7122                       else if (goal_mem && MEM_P (dest)
7123                                && ! push_operand (dest, GET_MODE (dest)))
7124                         return 0;
7125                       else if (need_stable_sp
7126                                && push_operand (dest, GET_MODE (dest)))
7127                         return 0;
7128                     }
7129                 }
7130             }
7131
7132 #ifdef AUTO_INC_DEC
7133           /* If this insn auto-increments or auto-decrements
7134              either regno or valueno, return 0 now.
7135              If GOAL is a memory ref and its address is not constant,
7136              and this insn P increments a register used in GOAL, return 0.  */
7137           {
7138             rtx link;
7139
7140             for (link = REG_NOTES (p); link; link = XEXP (link, 1))
7141               if (REG_NOTE_KIND (link) == REG_INC
7142                   && REG_P (XEXP (link, 0)))
7143                 {
7144                   int incno = REGNO (XEXP (link, 0));
7145                   if (incno < regno + nregs && incno >= regno)
7146                     return 0;
7147                   if (incno < valueno + valuenregs && incno >= valueno)
7148                     return 0;
7149                   if (goal_mem_addr_varies
7150                       && reg_overlap_mentioned_for_reload_p (XEXP (link, 0),
7151                                                              goal))
7152                     return 0;
7153                 }
7154           }
7155 #endif
7156         }
7157     }
7158 }
7159 \f
7160 /* Find a place where INCED appears in an increment or decrement operator
7161    within X, and return the amount INCED is incremented or decremented by.
7162    The value is always positive.  */
7163
7164 static int
7165 find_inc_amount (rtx x, rtx inced)
7166 {
7167   enum rtx_code code = GET_CODE (x);
7168   const char *fmt;
7169   int i;
7170
7171   if (code == MEM)
7172     {
7173       rtx addr = XEXP (x, 0);
7174       if ((GET_CODE (addr) == PRE_DEC
7175            || GET_CODE (addr) == POST_DEC
7176            || GET_CODE (addr) == PRE_INC
7177            || GET_CODE (addr) == POST_INC)
7178           && XEXP (addr, 0) == inced)
7179         return GET_MODE_SIZE (GET_MODE (x));
7180       else if ((GET_CODE (addr) == PRE_MODIFY
7181                 || GET_CODE (addr) == POST_MODIFY)
7182                && GET_CODE (XEXP (addr, 1)) == PLUS
7183                && XEXP (addr, 0) == XEXP (XEXP (addr, 1), 0)
7184                && XEXP (addr, 0) == inced
7185                && CONST_INT_P (XEXP (XEXP (addr, 1), 1)))
7186         {
7187           i = INTVAL (XEXP (XEXP (addr, 1), 1));
7188           return i < 0 ? -i : i;
7189         }
7190     }
7191
7192   fmt = GET_RTX_FORMAT (code);
7193   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7194     {
7195       if (fmt[i] == 'e')
7196         {
7197           int tem = find_inc_amount (XEXP (x, i), inced);
7198           if (tem != 0)
7199             return tem;
7200         }
7201       if (fmt[i] == 'E')
7202         {
7203           int j;
7204           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7205             {
7206               int tem = find_inc_amount (XVECEXP (x, i, j), inced);
7207               if (tem != 0)
7208                 return tem;
7209             }
7210         }
7211     }
7212
7213   return 0;
7214 }
7215 \f
7216 /* Return 1 if registers from REGNO to ENDREGNO are the subjects of a
7217    REG_INC note in insn INSN.  REGNO must refer to a hard register.  */
7218
7219 #ifdef AUTO_INC_DEC
7220 static int
7221 reg_inc_found_and_valid_p (unsigned int regno, unsigned int endregno,
7222                            rtx insn)
7223 {
7224   rtx link;
7225
7226   gcc_assert (insn);
7227
7228   if (! INSN_P (insn))
7229     return 0;
7230
7231   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
7232     if (REG_NOTE_KIND (link) == REG_INC)
7233       {
7234         unsigned int test = (int) REGNO (XEXP (link, 0));
7235         if (test >= regno && test < endregno)
7236           return 1;
7237       }
7238   return 0;
7239 }
7240 #else
7241
7242 #define reg_inc_found_and_valid_p(regno,endregno,insn) 0
7243
7244 #endif
7245
7246 /* Return 1 if register REGNO is the subject of a clobber in insn INSN.
7247    If SETS is 1, also consider SETs.  If SETS is 2, enable checking
7248    REG_INC.  REGNO must refer to a hard register.  */
7249
7250 int
7251 regno_clobbered_p (unsigned int regno, rtx insn, enum machine_mode mode,
7252                    int sets)
7253 {
7254   unsigned int nregs, endregno;
7255
7256   /* regno must be a hard register.  */
7257   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
7258
7259   nregs = hard_regno_nregs[regno][mode];
7260   endregno = regno + nregs;
7261
7262   if ((GET_CODE (PATTERN (insn)) == CLOBBER
7263        || (sets == 1 && GET_CODE (PATTERN (insn)) == SET))
7264       && REG_P (XEXP (PATTERN (insn), 0)))
7265     {
7266       unsigned int test = REGNO (XEXP (PATTERN (insn), 0));
7267
7268       return test >= regno && test < endregno;
7269     }
7270
7271   if (sets == 2 && reg_inc_found_and_valid_p (regno, endregno, insn))
7272     return 1;
7273
7274   if (GET_CODE (PATTERN (insn)) == PARALLEL)
7275     {
7276       int i = XVECLEN (PATTERN (insn), 0) - 1;
7277
7278       for (; i >= 0; i--)
7279         {
7280           rtx elt = XVECEXP (PATTERN (insn), 0, i);
7281           if ((GET_CODE (elt) == CLOBBER
7282                || (sets == 1 && GET_CODE (PATTERN (insn)) == SET))
7283               && REG_P (XEXP (elt, 0)))
7284             {
7285               unsigned int test = REGNO (XEXP (elt, 0));
7286
7287               if (test >= regno && test < endregno)
7288                 return 1;
7289             }
7290           if (sets == 2
7291               && reg_inc_found_and_valid_p (regno, endregno, elt))
7292             return 1;
7293         }
7294     }
7295
7296   return 0;
7297 }
7298
7299 /* Find the low part, with mode MODE, of a hard regno RELOADREG.  */
7300 rtx
7301 reload_adjust_reg_for_mode (rtx reloadreg, enum machine_mode mode)
7302 {
7303   int regno;
7304
7305   if (GET_MODE (reloadreg) == mode)
7306     return reloadreg;
7307
7308   regno = REGNO (reloadreg);
7309
7310   if (WORDS_BIG_ENDIAN)
7311     regno += (int) hard_regno_nregs[regno][GET_MODE (reloadreg)]
7312       - (int) hard_regno_nregs[regno][mode];
7313
7314   return gen_rtx_REG (mode, regno);
7315 }
7316
7317 static const char *const reload_when_needed_name[] =
7318 {
7319   "RELOAD_FOR_INPUT",
7320   "RELOAD_FOR_OUTPUT",
7321   "RELOAD_FOR_INSN",
7322   "RELOAD_FOR_INPUT_ADDRESS",
7323   "RELOAD_FOR_INPADDR_ADDRESS",
7324   "RELOAD_FOR_OUTPUT_ADDRESS",
7325   "RELOAD_FOR_OUTADDR_ADDRESS",
7326   "RELOAD_FOR_OPERAND_ADDRESS",
7327   "RELOAD_FOR_OPADDR_ADDR",
7328   "RELOAD_OTHER",
7329   "RELOAD_FOR_OTHER_ADDRESS"
7330 };
7331
7332 /* These functions are used to print the variables set by 'find_reloads' */
7333
7334 DEBUG_FUNCTION void
7335 debug_reload_to_stream (FILE *f)
7336 {
7337   int r;
7338   const char *prefix;
7339
7340   if (! f)
7341     f = stderr;
7342   for (r = 0; r < n_reloads; r++)
7343     {
7344       fprintf (f, "Reload %d: ", r);
7345
7346       if (rld[r].in != 0)
7347         {
7348           fprintf (f, "reload_in (%s) = ",
7349                    GET_MODE_NAME (rld[r].inmode));
7350           print_inline_rtx (f, rld[r].in, 24);
7351           fprintf (f, "\n\t");
7352         }
7353
7354       if (rld[r].out != 0)
7355         {
7356           fprintf (f, "reload_out (%s) = ",
7357                    GET_MODE_NAME (rld[r].outmode));
7358           print_inline_rtx (f, rld[r].out, 24);
7359           fprintf (f, "\n\t");
7360         }
7361
7362       fprintf (f, "%s, ", reg_class_names[(int) rld[r].rclass]);
7363
7364       fprintf (f, "%s (opnum = %d)",
7365                reload_when_needed_name[(int) rld[r].when_needed],
7366                rld[r].opnum);
7367
7368       if (rld[r].optional)
7369         fprintf (f, ", optional");
7370
7371       if (rld[r].nongroup)
7372         fprintf (f, ", nongroup");
7373
7374       if (rld[r].inc != 0)
7375         fprintf (f, ", inc by %d", rld[r].inc);
7376
7377       if (rld[r].nocombine)
7378         fprintf (f, ", can't combine");
7379
7380       if (rld[r].secondary_p)
7381         fprintf (f, ", secondary_reload_p");
7382
7383       if (rld[r].in_reg != 0)
7384         {
7385           fprintf (f, "\n\treload_in_reg: ");
7386           print_inline_rtx (f, rld[r].in_reg, 24);
7387         }
7388
7389       if (rld[r].out_reg != 0)
7390         {
7391           fprintf (f, "\n\treload_out_reg: ");
7392           print_inline_rtx (f, rld[r].out_reg, 24);
7393         }
7394
7395       if (rld[r].reg_rtx != 0)
7396         {
7397           fprintf (f, "\n\treload_reg_rtx: ");
7398           print_inline_rtx (f, rld[r].reg_rtx, 24);
7399         }
7400
7401       prefix = "\n\t";
7402       if (rld[r].secondary_in_reload != -1)
7403         {
7404           fprintf (f, "%ssecondary_in_reload = %d",
7405                    prefix, rld[r].secondary_in_reload);
7406           prefix = ", ";
7407         }
7408
7409       if (rld[r].secondary_out_reload != -1)
7410         fprintf (f, "%ssecondary_out_reload = %d\n",
7411                  prefix, rld[r].secondary_out_reload);
7412
7413       prefix = "\n\t";
7414       if (rld[r].secondary_in_icode != CODE_FOR_nothing)
7415         {
7416           fprintf (f, "%ssecondary_in_icode = %s", prefix,
7417                    insn_data[rld[r].secondary_in_icode].name);
7418           prefix = ", ";
7419         }
7420
7421       if (rld[r].secondary_out_icode != CODE_FOR_nothing)
7422         fprintf (f, "%ssecondary_out_icode = %s", prefix,
7423                  insn_data[rld[r].secondary_out_icode].name);
7424
7425       fprintf (f, "\n");
7426     }
7427 }
7428
7429 DEBUG_FUNCTION void
7430 debug_reload (void)
7431 {
7432   debug_reload_to_stream (stderr);
7433 }