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