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