OSDN Git Service

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